LLVM API Documentation
00001 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file is a common base class of all globally definable objects. As such, 00011 // it is subclassed by GlobalVariable, GlobalAlias and by Function. This is 00012 // used because you can do certain things with these global objects that you 00013 // can't do to anything else. For example, use the address of one as a 00014 // constant. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_GLOBALVALUE_H 00019 #define LLVM_GLOBALVALUE_H 00020 00021 #include "llvm/Constant.h" 00022 00023 namespace llvm { 00024 00025 class PointerType; 00026 class Module; 00027 00028 class GlobalValue : public Constant { 00029 GlobalValue(const GlobalValue &); // do not implement 00030 public: 00031 /// @brief An enumeration for the kinds of linkage for global values. 00032 enum LinkageTypes { 00033 ExternalLinkage = 0,///< Externally visible function 00034 AvailableExternallyLinkage, ///< Available for inspection, not emission. 00035 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline) 00036 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent. 00037 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak) 00038 WeakODRLinkage, ///< Same, but only replaced by something equivalent. 00039 AppendingLinkage, ///< Special purpose, only applies to global arrays 00040 InternalLinkage, ///< Rename collisions when linking (static functions). 00041 PrivateLinkage, ///< Like Internal, but omit from symbol table. 00042 LinkerPrivateLinkage, ///< Like Private, but linker removes. 00043 DLLImportLinkage, ///< Function to be imported from DLL 00044 DLLExportLinkage, ///< Function to be accessible from DLL. 00045 ExternalWeakLinkage,///< ExternalWeak linkage description. 00046 GhostLinkage, ///< Stand-in functions for streaming fns from BC files. 00047 CommonLinkage ///< Tentative definitions. 00048 }; 00049 00050 /// @brief An enumeration for the kinds of visibility of global values. 00051 enum VisibilityTypes { 00052 DefaultVisibility = 0, ///< The GV is visible 00053 HiddenVisibility, ///< The GV is hidden 00054 ProtectedVisibility ///< The GV is protected 00055 }; 00056 00057 protected: 00058 GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps, 00059 LinkageTypes linkage, const Twine &Name = "") 00060 : Constant(ty, vty, Ops, NumOps), Parent(0), 00061 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) { 00062 setName(Name); 00063 } 00064 00065 Module *Parent; 00066 // Note: VC++ treats enums as signed, so an extra bit is required to prevent 00067 // Linkage and Visibility from turning into negative values. 00068 LinkageTypes Linkage : 5; // The linkage of this global 00069 unsigned Visibility : 2; // The visibility style of this global 00070 unsigned Alignment : 16; // Alignment of this symbol, must be power of two 00071 std::string Section; // Section to emit this into, empty mean default 00072 public: 00073 ~GlobalValue() { 00074 removeDeadConstantUsers(); // remove any dead constants using this. 00075 } 00076 00077 unsigned getAlignment() const { return Alignment; } 00078 void setAlignment(unsigned Align) { 00079 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 00080 Alignment = Align; 00081 } 00082 00083 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); } 00084 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; } 00085 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; } 00086 bool hasProtectedVisibility() const { 00087 return Visibility == ProtectedVisibility; 00088 } 00089 void setVisibility(VisibilityTypes V) { Visibility = V; } 00090 00091 bool hasSection() const { return !Section.empty(); } 00092 const std::string &getSection() const { return Section; } 00093 void setSection(const StringRef &S) { Section = S; } 00094 00095 /// If the usage is empty (except transitively dead constants), then this 00096 /// global value can can be safely deleted since the destructor will 00097 /// delete the dead constants as well. 00098 /// @brief Determine if the usage of this global value is empty except 00099 /// for transitively dead constants. 00100 bool use_empty_except_constants(); 00101 00102 /// getType - Global values are always pointers. 00103 inline const PointerType *getType() const { 00104 return reinterpret_cast<const PointerType*>(User::getType()); 00105 } 00106 00107 static LinkageTypes getLinkOnceLinkage(bool ODR) { 00108 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage; 00109 } 00110 static LinkageTypes getWeakLinkage(bool ODR) { 00111 return ODR ? WeakODRLinkage : WeakAnyLinkage; 00112 } 00113 00114 bool hasExternalLinkage() const { return Linkage == ExternalLinkage; } 00115 bool hasAvailableExternallyLinkage() const { 00116 return Linkage == AvailableExternallyLinkage; 00117 } 00118 bool hasLinkOnceLinkage() const { 00119 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage; 00120 } 00121 bool hasWeakLinkage() const { 00122 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage; 00123 } 00124 bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; } 00125 bool hasInternalLinkage() const { return Linkage == InternalLinkage; } 00126 bool hasPrivateLinkage() const { return Linkage == PrivateLinkage; } 00127 bool hasLinkerPrivateLinkage() const { return Linkage==LinkerPrivateLinkage; } 00128 bool hasLocalLinkage() const { 00129 return hasInternalLinkage() || hasPrivateLinkage() || 00130 hasLinkerPrivateLinkage(); 00131 } 00132 bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; } 00133 bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; } 00134 bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; } 00135 bool hasGhostLinkage() const { return Linkage == GhostLinkage; } 00136 bool hasCommonLinkage() const { return Linkage == CommonLinkage; } 00137 00138 void setLinkage(LinkageTypes LT) { Linkage = LT; } 00139 LinkageTypes getLinkage() const { return Linkage; } 00140 00141 /// mayBeOverridden - Whether the definition of this global may be replaced 00142 /// by something non-equivalent at link time. For example, if a function has 00143 /// weak linkage then the code defining it may be replaced by different code. 00144 bool mayBeOverridden() const { 00145 return (Linkage == WeakAnyLinkage || 00146 Linkage == LinkOnceAnyLinkage || 00147 Linkage == CommonLinkage || 00148 Linkage == ExternalWeakLinkage); 00149 } 00150 00151 /// isWeakForLinker - Whether the definition of this global may be replaced at 00152 /// link time. 00153 bool isWeakForLinker() const { 00154 return (Linkage == AvailableExternallyLinkage || 00155 Linkage == WeakAnyLinkage || 00156 Linkage == WeakODRLinkage || 00157 Linkage == LinkOnceAnyLinkage || 00158 Linkage == LinkOnceODRLinkage || 00159 Linkage == CommonLinkage || 00160 Linkage == ExternalWeakLinkage); 00161 } 00162 00163 /// copyAttributesFrom - copy all additional attributes (those not needed to 00164 /// create a GlobalValue) from the GlobalValue Src to this one. 00165 virtual void copyAttributesFrom(const GlobalValue *Src); 00166 00167 /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily 00168 /// stream in functions from disk, this method can be used to check to see if 00169 /// the function has been read in yet or not. Unless you are working on the 00170 /// JIT or something else that streams stuff in lazily, you don't need to 00171 /// worry about this. 00172 bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; } 00173 00174 /// Override from Constant class. No GlobalValue's are null values so this 00175 /// always returns false. 00176 virtual bool isNullValue() const { return false; } 00177 00178 /// Override from Constant class. 00179 virtual void destroyConstant(); 00180 00181 /// isDeclaration - Return true if the primary definition of this global 00182 /// value is outside of the current translation unit... 00183 virtual bool isDeclaration() const = 0; 00184 00185 /// removeFromParent - This method unlinks 'this' from the containing module, 00186 /// but does not delete it. 00187 virtual void removeFromParent() = 0; 00188 00189 /// eraseFromParent - This method unlinks 'this' from the containing module 00190 /// and deletes it. 00191 virtual void eraseFromParent() = 0; 00192 00193 /// getParent - Get the module that this global value is contained inside 00194 /// of... 00195 inline Module *getParent() { return Parent; } 00196 inline const Module *getParent() const { return Parent; } 00197 00198 /// removeDeadConstantUsers - If there are any dead constant users dangling 00199 /// off of this global value, remove them. This method is useful for clients 00200 /// that want to check to see if a global is unused, but don't want to deal 00201 /// with potentially dead constants hanging off of the globals. 00202 void removeDeadConstantUsers() const; 00203 00204 // Methods for support type inquiry through isa, cast, and dyn_cast: 00205 static inline bool classof(const GlobalValue *) { return true; } 00206 static inline bool classof(const Value *V) { 00207 return V->getValueID() == Value::FunctionVal || 00208 V->getValueID() == Value::GlobalVariableVal || 00209 V->getValueID() == Value::GlobalAliasVal; 00210 } 00211 }; 00212 00213 } // End llvm namespace 00214 00215 #endif