Style fix: The name of variables starts with an upper case letter.
[oota-llvm.git] / include / llvm / IR / 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_IR_GLOBALVALUE_H
19 #define LLVM_IR_GLOBALVALUE_H
20
21 #include "llvm/IR/Constant.h"
22 #include "llvm/IR/DerivedTypes.h"
23
24 namespace llvm {
25
26 class PointerType;
27 class Module;
28
29 class GlobalValue : public Constant {
30   GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION;
31 public:
32   /// @brief An enumeration for the kinds of linkage for global values.
33   enum LinkageTypes {
34     ExternalLinkage = 0,///< Externally visible function
35     AvailableExternallyLinkage, ///< Available for inspection, not emission.
36     LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
37     LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
38     WeakAnyLinkage,     ///< Keep one copy of named function when linking (weak)
39     WeakODRLinkage,     ///< Same, but only replaced by something equivalent.
40     AppendingLinkage,   ///< Special purpose, only applies to global arrays
41     InternalLinkage,    ///< Rename collisions when linking (static functions).
42     PrivateLinkage,     ///< Like Internal, but omit from symbol table.
43     ExternalWeakLinkage,///< ExternalWeak linkage description.
44     CommonLinkage       ///< Tentative definitions.
45   };
46
47   /// @brief An enumeration for the kinds of visibility of global values.
48   enum VisibilityTypes {
49     DefaultVisibility = 0,  ///< The GV is visible
50     HiddenVisibility,       ///< The GV is hidden
51     ProtectedVisibility     ///< The GV is protected
52   };
53
54   /// @brief Storage classes of global values for PE targets.
55   enum DLLStorageClassTypes {
56     DefaultStorageClass   = 0,
57     DLLImportStorageClass = 1, ///< Function to be imported from DLL
58     DLLExportStorageClass = 2  ///< Function to be accessible from DLL.
59   };
60
61 protected:
62   GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
63               LinkageTypes Linkage, const Twine &Name)
64       : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage),
65         Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0),
66         DllStorageClass(DefaultStorageClass), Parent(nullptr) {
67     setName(Name);
68   }
69
70   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
71   // Linkage and Visibility from turning into negative values.
72   LinkageTypes Linkage : 5;   // The linkage of this global
73   unsigned Visibility : 2;    // The visibility style of this global
74   unsigned Alignment : 16;    // Alignment of this symbol, must be power of two
75   unsigned UnnamedAddr : 1;   // This value's address is not significant
76   unsigned DllStorageClass : 2; // DLL storage class
77   Module *Parent;             // The containing module.
78   std::string Section;        // Section to emit this into, empty mean default
79 public:
80   ~GlobalValue() {
81     removeDeadConstantUsers();   // remove any dead constants using this.
82   }
83
84   unsigned getAlignment() const;
85   void setAlignment(unsigned Align);
86
87   bool hasUnnamedAddr() const { return UnnamedAddr; }
88   void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
89
90   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
91   bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
92   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
93   bool hasProtectedVisibility() const {
94     return Visibility == ProtectedVisibility;
95   }
96   void setVisibility(VisibilityTypes V) {
97     assert((!hasLocalLinkage() || V == DefaultVisibility) &&
98            "local linkage requires default visibility");
99     Visibility = V;
100   }
101
102   DLLStorageClassTypes getDLLStorageClass() const {
103     return DLLStorageClassTypes(DllStorageClass);
104   }
105   bool hasDLLImportStorageClass() const {
106     return DllStorageClass == DLLImportStorageClass;
107   }
108   bool hasDLLExportStorageClass() const {
109     return DllStorageClass == DLLExportStorageClass;
110   }
111   void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
112
113   bool hasSection() const { return !getSection().empty(); }
114   const std::string &getSection() const;
115   void setSection(StringRef S);
116
117   /// Global values are always pointers.
118   inline PointerType *getType() const {
119     return cast<PointerType>(User::getType());
120   }
121
122   static LinkageTypes getLinkOnceLinkage(bool ODR) {
123     return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
124   }
125   static LinkageTypes getWeakLinkage(bool ODR) {
126     return ODR ? WeakODRLinkage : WeakAnyLinkage;
127   }
128
129   static bool isExternalLinkage(LinkageTypes Linkage) {
130     return Linkage == ExternalLinkage;
131   }
132   static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
133     return Linkage == AvailableExternallyLinkage;
134   }
135   static bool isLinkOnceLinkage(LinkageTypes Linkage) {
136     return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
137   }
138   static bool isWeakAnyLinkage(LinkageTypes Linkage) {
139     return Linkage == WeakAnyLinkage;
140   }
141   static bool isWeakODRLinkage(LinkageTypes Linkage) {
142     return Linkage == WeakODRLinkage;
143   }
144   static bool isWeakLinkage(LinkageTypes Linkage) {
145     return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
146   }
147   static bool isAppendingLinkage(LinkageTypes Linkage) {
148     return Linkage == AppendingLinkage;
149   }
150   static bool isInternalLinkage(LinkageTypes Linkage) {
151     return Linkage == InternalLinkage;
152   }
153   static bool isPrivateLinkage(LinkageTypes Linkage) {
154     return Linkage == PrivateLinkage;
155   }
156   static bool isLocalLinkage(LinkageTypes Linkage) {
157     return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
158   }
159   static bool isExternalWeakLinkage(LinkageTypes Linkage) {
160     return Linkage == ExternalWeakLinkage;
161   }
162   static bool isCommonLinkage(LinkageTypes Linkage) {
163     return Linkage == CommonLinkage;
164   }
165
166   /// Whether the definition of this global may be discarded if it is not used
167   /// in its compilation unit.
168   static bool isDiscardableIfUnused(LinkageTypes Linkage) {
169     return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
170   }
171
172   /// Whether the definition of this global may be replaced by something
173   /// non-equivalent at link time. For example, if a function has weak linkage
174   /// then the code defining it may be replaced by different code.
175   static bool mayBeOverridden(LinkageTypes Linkage) {
176     return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
177            Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
178   }
179
180   /// Whether the definition of this global may be replaced at link time.  NB:
181   /// Using this method outside of the code generators is almost always a
182   /// mistake: when working at the IR level use mayBeOverridden instead as it
183   /// knows about ODR semantics.
184   static bool isWeakForLinker(LinkageTypes Linkage)  {
185     return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage ||
186            Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage ||
187            Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage ||
188            Linkage == ExternalWeakLinkage;
189   }
190
191   bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
192   bool hasAvailableExternallyLinkage() const {
193     return isAvailableExternallyLinkage(Linkage);
194   }
195   bool hasLinkOnceLinkage() const {
196     return isLinkOnceLinkage(Linkage);
197   }
198   bool hasWeakLinkage() const {
199     return isWeakLinkage(Linkage);
200   }
201   bool hasWeakAnyLinkage() const {
202     return isWeakAnyLinkage(Linkage);
203   }
204   bool hasWeakODRLinkage() const {
205     return isWeakODRLinkage(Linkage);
206   }
207   bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
208   bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
209   bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
210   bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
211   bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
212   bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
213
214   void setLinkage(LinkageTypes LT) {
215     assert((!isLocalLinkage(LT) || hasDefaultVisibility()) &&
216            "local linkage requires default visibility");
217     Linkage = LT;
218   }
219   LinkageTypes getLinkage() const { return Linkage; }
220
221   bool isDiscardableIfUnused() const {
222     return isDiscardableIfUnused(Linkage);
223   }
224
225   bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
226
227   bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
228
229   /// Copy all additional attributes (those not needed to create a GlobalValue)
230   /// from the GlobalValue Src to this one.
231   virtual void copyAttributesFrom(const GlobalValue *Src);
232
233   /// If special LLVM prefix that is used to inform the asm printer to not emit
234   /// usual symbol prefix before the symbol name is used then return linkage
235   /// name after skipping this special LLVM prefix.
236   static StringRef getRealLinkageName(StringRef Name) {
237     if (!Name.empty() && Name[0] == '\1')
238       return Name.substr(1);
239     return Name;
240   }
241
242 /// @name Materialization
243 /// Materialization is used to construct functions only as they're needed. This
244 /// is useful to reduce memory usage in LLVM or parsing work done by the
245 /// BitcodeReader to load the Module.
246 /// @{
247
248   /// If this function's Module is being lazily streamed in functions from disk
249   /// or some other source, this method can be used to check to see if the
250   /// function has been read in yet or not.
251   bool isMaterializable() const;
252
253   /// Returns true if this function was loaded from a GVMaterializer that's
254   /// still attached to its Module and that knows how to dematerialize the
255   /// function.
256   bool isDematerializable() const;
257
258   /// Make sure this GlobalValue is fully read. If the module is corrupt, this
259   /// returns true and fills in the optional string with information about the
260   /// problem.  If successful, this returns false.
261   bool Materialize(std::string *ErrInfo = nullptr);
262
263   /// If this GlobalValue is read in, and if the GVMaterializer supports it,
264   /// release the memory for the function, and set it up to be materialized
265   /// lazily. If !isDematerializable(), this method is a noop.
266   void Dematerialize();
267
268 /// @}
269
270   /// Override from Constant class.
271   void destroyConstant() override;
272
273   /// Return true if the primary definition of this global value is outside of
274   /// the current translation unit.
275   bool isDeclaration() const;
276
277   /// This method unlinks 'this' from the containing module, but does not delete
278   /// it.
279   virtual void removeFromParent() = 0;
280
281   /// This method unlinks 'this' from the containing module and deletes it.
282   virtual void eraseFromParent() = 0;
283
284   /// Get the module that this global value is contained inside of...
285   inline Module *getParent() { return Parent; }
286   inline const Module *getParent() const { return Parent; }
287
288   const DataLayout *getDataLayout() const;
289
290   // Methods for support type inquiry through isa, cast, and dyn_cast:
291   static inline bool classof(const Value *V) {
292     return V->getValueID() == Value::FunctionVal ||
293            V->getValueID() == Value::GlobalVariableVal ||
294            V->getValueID() == Value::GlobalAliasVal;
295   }
296 };
297
298 } // End llvm namespace
299
300 #endif