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