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 LinkOnceLinkage, ///< Keep one copy of function when linking (inline) 00035 WeakLinkage, ///< Keep one copy of named function when linking (weak) 00036 AppendingLinkage, ///< Special purpose, only applies to global arrays 00037 InternalLinkage, ///< Rename collisions when linking (static functions) 00038 PrivateLinkage, ///< Like Internal, but omit from symbol table 00039 DLLImportLinkage, ///< Function to be imported from DLL 00040 DLLExportLinkage, ///< Function to be accessible from DLL 00041 ExternalWeakLinkage,///< ExternalWeak linkage description 00042 GhostLinkage, ///< Stand-in functions for streaming fns from BC files 00043 CommonLinkage ///< Tentative definitions 00044 }; 00045 00046 /// @brief An enumeration for the kinds of visibility of global values. 00047 enum VisibilityTypes { 00048 DefaultVisibility = 0, ///< The GV is visible 00049 HiddenVisibility, ///< The GV is hidden 00050 ProtectedVisibility ///< The GV is protected 00051 }; 00052 00053 protected: 00054 GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps, 00055 LinkageTypes linkage, const std::string &name = "") 00056 : Constant(ty, vty, Ops, NumOps), Parent(0), 00057 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) { 00058 if (!name.empty()) setName(name); 00059 } 00060 00061 Module *Parent; 00062 // Note: VC++ treats enums as signed, so an extra bit is required to prevent 00063 // Linkage and Visibility from turning into negative values. 00064 LinkageTypes Linkage : 5; // The linkage of this global 00065 unsigned Visibility : 2; // The visibility style of this global 00066 unsigned Alignment : 16; // Alignment of this symbol, must be power of two 00067 std::string Section; // Section to emit this into, empty mean default 00068 public: 00069 ~GlobalValue() { 00070 removeDeadConstantUsers(); // remove any dead constants using this. 00071 } 00072 00073 unsigned getAlignment() const { return Alignment; } 00074 void setAlignment(unsigned Align) { 00075 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); 00076 Alignment = Align; 00077 } 00078 00079 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); } 00080 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; } 00081 bool hasProtectedVisibility() const { 00082 return Visibility == ProtectedVisibility; 00083 } 00084 void setVisibility(VisibilityTypes V) { Visibility = V; } 00085 00086 bool hasSection() const { return !Section.empty(); } 00087 const std::string &getSection() const { return Section; } 00088 void setSection(const std::string &S) { Section = S; } 00089 00090 /// If the usage is empty (except transitively dead constants), then this 00091 /// global value can can be safely deleted since the destructor will 00092 /// delete the dead constants as well. 00093 /// @brief Determine if the usage of this global value is empty except 00094 /// for transitively dead constants. 00095 bool use_empty_except_constants(); 00096 00097 /// getType - Global values are always pointers. 00098 inline const PointerType *getType() const { 00099 return reinterpret_cast<const PointerType*>(User::getType()); 00100 } 00101 00102 bool hasExternalLinkage() const { return Linkage == ExternalLinkage; } 00103 bool hasLinkOnceLinkage() const { return Linkage == LinkOnceLinkage; } 00104 bool hasWeakLinkage() const { return Linkage == WeakLinkage; } 00105 bool hasCommonLinkage() const { return Linkage == CommonLinkage; } 00106 bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; } 00107 bool hasInternalLinkage() const { return Linkage == InternalLinkage; } 00108 bool hasPrivateLinkage() const { return Linkage == PrivateLinkage; } 00109 bool hasLocalLinkage() const { 00110 return Linkage == InternalLinkage || Linkage == PrivateLinkage; 00111 } 00112 bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; } 00113 bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; } 00114 bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; } 00115 bool hasGhostLinkage() const { return Linkage == GhostLinkage; } 00116 void setLinkage(LinkageTypes LT) { Linkage = LT; } 00117 LinkageTypes getLinkage() const { return Linkage; } 00118 00119 /// mayBeOverridden - Whether the definition of this global may be replaced 00120 /// at link time. For example, if a function has weak linkage then the code 00121 /// defining it may be replaced by different code. 00122 bool mayBeOverridden() const { 00123 return (Linkage == WeakLinkage || 00124 Linkage == LinkOnceLinkage || 00125 Linkage == CommonLinkage || 00126 Linkage == ExternalWeakLinkage); 00127 } 00128 00129 /// copyAttributesFrom - copy all additional attributes (those not needed to 00130 /// create a GlobalValue) from the GlobalValue Src to this one. 00131 virtual void copyAttributesFrom(const GlobalValue *Src); 00132 00133 /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily 00134 /// stream in functions from disk, this method can be used to check to see if 00135 /// the function has been read in yet or not. Unless you are working on the 00136 /// JIT or something else that streams stuff in lazily, you don't need to 00137 /// worry about this. 00138 bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; } 00139 00140 /// Override from Constant class. No GlobalValue's are null values so this 00141 /// always returns false. 00142 virtual bool isNullValue() const { return false; } 00143 00144 /// Override from Constant class. 00145 virtual void destroyConstant(); 00146 00147 /// isDeclaration - Return true if the primary definition of this global 00148 /// value is outside of the current translation unit... 00149 virtual bool isDeclaration() const = 0; 00150 00151 /// removeFromParent - This method unlinks 'this' from the containing module, 00152 /// but does not delete it. 00153 virtual void removeFromParent() = 0; 00154 00155 /// eraseFromParent - This method unlinks 'this' from the containing module 00156 /// and deletes it. 00157 virtual void eraseFromParent() = 0; 00158 00159 /// getParent - Get the module that this global value is contained inside 00160 /// of... 00161 inline Module *getParent() { return Parent; } 00162 inline const Module *getParent() const { return Parent; } 00163 00164 /// removeDeadConstantUsers - If there are any dead constant users dangling 00165 /// off of this global value, remove them. This method is useful for clients 00166 /// that want to check to see if a global is unused, but don't want to deal 00167 /// with potentially dead constants hanging off of the globals. 00168 void removeDeadConstantUsers(); 00169 00170 // Methods for support type inquiry through isa, cast, and dyn_cast: 00171 static inline bool classof(const GlobalValue *) { return true; } 00172 static inline bool classof(const Value *V) { 00173 return V->getValueID() == Value::FunctionVal || 00174 V->getValueID() == Value::GlobalVariableVal || 00175 V->getValueID() == Value::GlobalAliasVal; 00176 } 00177 }; 00178 00179 } // End llvm namespace 00180 00181 #endif