It makes no sense to have a ODR version of common
[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     LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
35     LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
36     WeakAnyLinkage,     ///< Keep one copy of named function when linking (weak)
37     WeakODRLinkage,     ///< Same, but only replaced by something equivalent.
38     AppendingLinkage,   ///< Special purpose, only applies to global arrays
39     InternalLinkage,    ///< Rename collisions when linking (static functions)
40     PrivateLinkage,     ///< Like Internal, but omit from symbol table
41     DLLImportLinkage,   ///< Function to be imported from DLL
42     DLLExportLinkage,   ///< Function to be accessible from DLL
43     ExternalWeakLinkage,///< ExternalWeak linkage description
44     GhostLinkage,       ///< Stand-in functions for streaming fns from BC files
45     CommonLinkage       ///< Tentative definitions
46   };
47
48   /// @brief An enumeration for the kinds of visibility of global values.
49   enum VisibilityTypes {
50     DefaultVisibility = 0,  ///< The GV is visible
51     HiddenVisibility,       ///< The GV is hidden
52     ProtectedVisibility     ///< The GV is protected
53   };
54
55 protected:
56   GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
57               LinkageTypes linkage, const std::string &name = "")
58     : Constant(ty, vty, Ops, NumOps), Parent(0),
59       Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
60     if (!name.empty()) setName(name);
61   }
62
63   Module *Parent;
64   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
65   // Linkage and Visibility from turning into negative values.
66   LinkageTypes Linkage : 5;   // The linkage of this global
67   unsigned Visibility : 2;    // The visibility style of this global
68   unsigned Alignment : 16;    // Alignment of this symbol, must be power of two
69   std::string Section;        // Section to emit this into, empty mean default
70 public:
71   ~GlobalValue() {
72     removeDeadConstantUsers();   // remove any dead constants using this.
73   }
74
75   unsigned getAlignment() const { return Alignment; }
76   void setAlignment(unsigned Align) {
77     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
78     Alignment = Align;
79   }
80
81   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
82   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
83   bool hasProtectedVisibility() const {
84     return Visibility == ProtectedVisibility;
85   }
86   void setVisibility(VisibilityTypes V) { Visibility = V; }
87   
88   bool hasSection() const { return !Section.empty(); }
89   const std::string &getSection() const { return Section; }
90   void setSection(const std::string &S) { Section = S; }
91   
92   /// If the usage is empty (except transitively dead constants), then this
93   /// global value can can be safely deleted since the destructor will
94   /// delete the dead constants as well.
95   /// @brief Determine if the usage of this global value is empty except
96   /// for transitively dead constants.
97   bool use_empty_except_constants();
98
99   /// getType - Global values are always pointers.
100   inline const PointerType *getType() const {
101     return reinterpret_cast<const PointerType*>(User::getType());
102   }
103
104   static LinkageTypes getLinkOnceLinkage(bool ODR) {
105     return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
106   }
107   static LinkageTypes getWeakLinkage(bool ODR) {
108     return ODR ? WeakODRLinkage : WeakAnyLinkage;
109   }
110
111   bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
112   bool hasLinkOnceLinkage() const {
113     return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
114   }
115   bool hasWeakLinkage() const {
116     return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
117   }
118   bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
119   bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
120   bool hasPrivateLinkage() const { return Linkage == PrivateLinkage; }
121   bool hasLocalLinkage() const {
122     return Linkage == InternalLinkage || Linkage == PrivateLinkage;
123   }
124   bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; }
125   bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; }
126   bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
127   bool hasGhostLinkage() const { return Linkage == GhostLinkage; }
128   bool hasCommonLinkage() const { return Linkage == CommonLinkage; }
129
130   void setLinkage(LinkageTypes LT) { Linkage = LT; }
131   LinkageTypes getLinkage() const { return Linkage; }
132
133   /// mayBeOverridden - Whether the definition of this global may be replaced
134   /// by something non-equivalent at link time.  For example, if a function has
135   /// weak linkage then the code defining it may be replaced by different code.
136   bool mayBeOverridden() const {
137     return (Linkage == WeakAnyLinkage ||
138             Linkage == LinkOnceAnyLinkage ||
139             Linkage == CommonLinkage ||
140             Linkage == ExternalWeakLinkage);
141   }
142
143   /// isWeakForLinker - Whether the definition of this global may be replaced at
144   /// link time, whether the replacement is equivalent to the original or not.
145   bool isWeakForLinker() const {
146     return (Linkage == WeakAnyLinkage ||
147             Linkage == WeakODRLinkage ||
148             Linkage == LinkOnceAnyLinkage ||
149             Linkage == LinkOnceODRLinkage ||
150             Linkage == CommonLinkage ||
151             Linkage == ExternalWeakLinkage);
152   }
153
154   /// copyAttributesFrom - copy all additional attributes (those not needed to
155   /// create a GlobalValue) from the GlobalValue Src to this one.
156   virtual void copyAttributesFrom(const GlobalValue *Src);
157
158   /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
159   /// stream in functions from disk, this method can be used to check to see if
160   /// the function has been read in yet or not.  Unless you are working on the
161   /// JIT or something else that streams stuff in lazily, you don't need to
162   /// worry about this.
163   bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; }
164
165   /// Override from Constant class. No GlobalValue's are null values so this
166   /// always returns false.
167   virtual bool isNullValue() const { return false; }
168
169   /// Override from Constant class.
170   virtual void destroyConstant();
171
172   /// isDeclaration - Return true if the primary definition of this global 
173   /// value is outside of the current translation unit...
174   virtual bool isDeclaration() const = 0;
175
176   /// removeFromParent - This method unlinks 'this' from the containing module,
177   /// but does not delete it.
178   virtual void removeFromParent() = 0;
179
180   /// eraseFromParent - This method unlinks 'this' from the containing module
181   /// and deletes it.
182   virtual void eraseFromParent() = 0;
183
184   /// getParent - Get the module that this global value is contained inside
185   /// of...
186   inline Module *getParent() { return Parent; }
187   inline const Module *getParent() const { return Parent; }
188
189   /// removeDeadConstantUsers - If there are any dead constant users dangling
190   /// off of this global value, remove them.  This method is useful for clients
191   /// that want to check to see if a global is unused, but don't want to deal
192   /// with potentially dead constants hanging off of the globals.
193   void removeDeadConstantUsers() const;
194
195   // Methods for support type inquiry through isa, cast, and dyn_cast:
196   static inline bool classof(const GlobalValue *) { return true; }
197   static inline bool classof(const Value *V) {
198     return V->getValueID() == Value::FunctionVal ||
199            V->getValueID() == Value::GlobalVariableVal ||
200            V->getValueID() == Value::GlobalAliasVal;
201   }
202 };
203
204 } // End llvm namespace
205
206 #endif