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