Add the private linkage.
[oota-llvm.git] / include / llvm / GlobalValue.h
1 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a common base class of all globally definable objects.  As such,
11 // it is subclassed by GlobalVariable, GlobalAlias and by Function.  This is
12 // used because you can do certain things with these global objects that you
13 // can't do to anything else.  For example, use the address of one as a
14 // constant.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_GLOBALVALUE_H
19 #define LLVM_GLOBALVALUE_H
20
21 #include "llvm/Constant.h"
22
23 namespace llvm {
24
25 class PointerType;
26 class Module;
27
28 class GlobalValue : public Constant {
29   GlobalValue(const GlobalValue &);             // do not implement
30 public:
31   /// @brief An enumeration for the kinds of linkage for global values.
32   enum LinkageTypes {
33     ExternalLinkage = 0,///< Externally visible function
34     LinkOnceLinkage,    ///< Keep one copy of function when linking (inline)
35     WeakLinkage,        ///< Keep one copy of named function when linking (weak)
36     AppendingLinkage,   ///< Special purpose, only applies to global arrays
37     InternalLinkage,    ///< Rename collisions when linking (static functions)
38     PrivateLinkage,     ///< Like Internal, but omit from symbol table
39     DLLImportLinkage,   ///< Function to be imported from DLL
40     DLLExportLinkage,   ///< Function to be accessible from DLL
41     ExternalWeakLinkage,///< ExternalWeak linkage description
42     GhostLinkage,       ///< Stand-in functions for streaming fns from BC files
43     CommonLinkage       ///< Tentative definitions
44   };
45
46   /// @brief An enumeration for the kinds of visibility of global values.
47   enum VisibilityTypes {
48     DefaultVisibility = 0,  ///< The GV is visible
49     HiddenVisibility,       ///< The GV is hidden
50     ProtectedVisibility     ///< The GV is protected
51   };
52
53 protected:
54   GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
55               LinkageTypes linkage, const std::string &name = "")
56     : Constant(ty, vty, Ops, NumOps), Parent(0),
57       Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
58     if (!name.empty()) setName(name);
59   }
60
61   Module *Parent;
62   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
63   // Linkage and Visibility from turning into negative values.
64   LinkageTypes Linkage : 5;   // The linkage of this global
65   unsigned Visibility : 2;    // The visibility style of this global
66   unsigned Alignment : 16;    // Alignment of this symbol, must be power of two
67   std::string Section;        // Section to emit this into, empty mean default
68 public:
69   ~GlobalValue() {
70     removeDeadConstantUsers();   // remove any dead constants using this.
71   }
72
73   unsigned getAlignment() const { return Alignment; }
74   void setAlignment(unsigned Align) {
75     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
76     Alignment = Align;
77   }
78
79   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
80   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
81   bool hasProtectedVisibility() const {
82     return Visibility == ProtectedVisibility;
83   }
84   void setVisibility(VisibilityTypes V) { Visibility = V; }
85   
86   bool hasSection() const { return !Section.empty(); }
87   const std::string &getSection() const { return Section; }
88   void setSection(const std::string &S) { Section = S; }
89   
90   /// If the usage is empty (except transitively dead constants), then this
91   /// global value can can be safely deleted since the destructor will
92   /// delete the dead constants as well.
93   /// @brief Determine if the usage of this global value is empty except
94   /// for transitively dead constants.
95   bool use_empty_except_constants();
96
97   /// getType - Global values are always pointers.
98   inline const PointerType *getType() const {
99     return reinterpret_cast<const PointerType*>(User::getType());
100   }
101
102   bool hasExternalLinkage()   const { return Linkage == ExternalLinkage; }
103   bool hasLinkOnceLinkage()   const { return Linkage == LinkOnceLinkage; }
104   bool hasWeakLinkage()       const { return Linkage == WeakLinkage; }
105   bool hasCommonLinkage()     const { return Linkage == CommonLinkage; }
106   bool hasAppendingLinkage()  const { return Linkage == AppendingLinkage; }
107   bool hasInternalLinkage()   const { return Linkage == InternalLinkage; }
108   bool hasPrivateLinkage()    const { return Linkage == PrivateLinkage; }
109   bool hasLocalLinkage()      const {
110     return Linkage == InternalLinkage || Linkage == PrivateLinkage;
111   }
112   bool hasDLLImportLinkage()  const { return Linkage == DLLImportLinkage; }
113   bool hasDLLExportLinkage()  const { return Linkage == DLLExportLinkage; }
114   bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
115   bool hasGhostLinkage()      const { return Linkage == GhostLinkage; }
116   void setLinkage(LinkageTypes LT) { Linkage = LT; }
117   LinkageTypes getLinkage() const { return Linkage; }
118
119   /// mayBeOverridden - Whether the definition of this global may be replaced
120   /// at link time.  For example, if a function has weak linkage then the code
121   /// defining it may be replaced by different code.
122   bool mayBeOverridden() const {
123     return (Linkage == WeakLinkage ||
124             Linkage == LinkOnceLinkage ||
125             Linkage == CommonLinkage ||
126             Linkage == ExternalWeakLinkage);
127   }
128
129   /// copyAttributesFrom - copy all additional attributes (those not needed to
130   /// create a GlobalValue) from the GlobalValue Src to this one.
131   virtual void copyAttributesFrom(const GlobalValue *Src);
132
133   /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
134   /// stream in functions from disk, this method can be used to check to see if
135   /// the function has been read in yet or not.  Unless you are working on the
136   /// JIT or something else that streams stuff in lazily, you don't need to
137   /// worry about this.
138   bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; }
139
140   /// Override from Constant class. No GlobalValue's are null values so this
141   /// always returns false.
142   virtual bool isNullValue() const { return false; }
143
144   /// Override from Constant class.
145   virtual void destroyConstant();
146
147   /// isDeclaration - Return true if the primary definition of this global 
148   /// value is outside of the current translation unit...
149   virtual bool isDeclaration() const = 0;
150
151   /// removeFromParent - This method unlinks 'this' from the containing module,
152   /// but does not delete it.
153   virtual void removeFromParent() = 0;
154
155   /// eraseFromParent - This method unlinks 'this' from the containing module
156   /// and deletes it.
157   virtual void eraseFromParent() = 0;
158
159   /// getParent - Get the module that this global value is contained inside
160   /// of...
161   inline Module *getParent() { return Parent; }
162   inline const Module *getParent() const { return Parent; }
163
164   /// removeDeadConstantUsers - If there are any dead constant users dangling
165   /// off of this global value, remove them.  This method is useful for clients
166   /// that want to check to see if a global is unused, but don't want to deal
167   /// with potentially dead constants hanging off of the globals.
168   void removeDeadConstantUsers();
169
170   // Methods for support type inquiry through isa, cast, and dyn_cast:
171   static inline bool classof(const GlobalValue *) { return true; }
172   static inline bool classof(const Value *V) {
173     return V->getValueID() == Value::FunctionVal ||
174            V->getValueID() == Value::GlobalVariableVal ||
175            V->getValueID() == Value::GlobalAliasVal;
176   }
177 };
178
179 } // End llvm namespace
180
181 #endif