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