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