LLVM API Documentation
00001 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- 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 contains the declaration of the GlobalVariable class, which 00011 // represents a single global variable (or constant) in the VM. 00012 // 00013 // Global variables are constant pointers that refer to hunks of space that are 00014 // allocated by either the VM, or by the linker in a static compiler. A global 00015 // variable may have an intial value, which is copied into the executables .data 00016 // area. Global Constants are required to have initializers. 00017 // 00018 //===----------------------------------------------------------------------===// 00019 00020 #ifndef LLVM_GLOBAL_VARIABLE_H 00021 #define LLVM_GLOBAL_VARIABLE_H 00022 00023 #include "llvm/GlobalValue.h" 00024 #include "llvm/OperandTraits.h" 00025 #include "llvm/ADT/ilist_node.h" 00026 00027 namespace llvm { 00028 00029 class Module; 00030 class Constant; 00031 class LLVMContext; 00032 template<typename ValueSubClass, typename ItemParentClass> 00033 class SymbolTableListTraits; 00034 00035 class GlobalVariable : public GlobalValue, public ilist_node<GlobalVariable> { 00036 friend class SymbolTableListTraits<GlobalVariable, Module>; 00037 void *operator new(size_t, unsigned); // Do not implement 00038 void operator=(const GlobalVariable &); // Do not implement 00039 GlobalVariable(const GlobalVariable &); // Do not implement 00040 00041 void setParent(Module *parent); 00042 00043 bool isConstantGlobal : 1; // Is this a global constant? 00044 bool isThreadLocalSymbol : 1; // Is this symbol "Thread Local"? 00045 00046 public: 00047 // allocate space for exactly one operand 00048 void *operator new(size_t s) { 00049 return User::operator new(s, 1); 00050 } 00051 /// GlobalVariable ctor - If a parent module is specified, the global is 00052 /// automatically inserted into the end of the specified modules global list. 00053 GlobalVariable(LLVMContext &Context, const Type *Ty, bool isConstant, 00054 LinkageTypes Linkage, 00055 Constant *Initializer = 0, const Twine &Name = "", 00056 bool ThreadLocal = false, unsigned AddressSpace = 0); 00057 /// GlobalVariable ctor - This creates a global and inserts it before the 00058 /// specified other global. 00059 GlobalVariable(Module &M, const Type *Ty, bool isConstant, 00060 LinkageTypes Linkage, Constant *Initializer, 00061 const Twine &Name, 00062 GlobalVariable *InsertBefore = 0, bool ThreadLocal = false, 00063 unsigned AddressSpace = 0); 00064 00065 ~GlobalVariable() { 00066 NumOperands = 1; // FIXME: needed by operator delete 00067 } 00068 00069 /// Provide fast operand accessors 00070 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); 00071 00072 /// isDeclaration - Is this global variable lacking an initializer? If so, 00073 /// the global variable is defined in some other translation unit, and is thus 00074 /// only a declaration here. 00075 virtual bool isDeclaration() const { return getNumOperands() == 0; } 00076 00077 /// hasInitializer - Unless a global variable isExternal(), it has an 00078 /// initializer. The initializer for the global variable/constant is held by 00079 /// Initializer if an initializer is specified. 00080 /// 00081 inline bool hasInitializer() const { return !isDeclaration(); } 00082 00083 /// hasDefinitiveInitializer - Whether the global variable has an initializer, 00084 /// and this is the initializer that will be used in the final executable. 00085 inline bool hasDefinitiveInitializer() const { 00086 return hasInitializer() && 00087 // The initializer of a global variable with weak linkage may change at 00088 // link time. 00089 !mayBeOverridden(); 00090 } 00091 00092 /// getInitializer - Return the initializer for this global variable. It is 00093 /// illegal to call this method if the global is external, because we cannot 00094 /// tell what the value is initialized to! 00095 /// 00096 inline /*const FIXME*/ Constant *getInitializer() const { 00097 assert(hasInitializer() && "GV doesn't have initializer!"); 00098 return static_cast<Constant*>(Op<0>().get()); 00099 } 00100 inline Constant *getInitializer() { 00101 assert(hasInitializer() && "GV doesn't have initializer!"); 00102 return static_cast<Constant*>(Op<0>().get()); 00103 } 00104 inline void setInitializer(Constant *CPV) { 00105 if (CPV == 0) { 00106 if (hasInitializer()) { 00107 Op<0>().set(0); 00108 NumOperands = 0; 00109 } 00110 } else { 00111 if (!hasInitializer()) 00112 NumOperands = 1; 00113 Op<0>().set(CPV); 00114 } 00115 } 00116 00117 /// If the value is a global constant, its value is immutable throughout the 00118 /// runtime execution of the program. Assigning a value into the constant 00119 /// leads to undefined behavior. 00120 /// 00121 bool isConstant() const { return isConstantGlobal; } 00122 void setConstant(bool Val) { isConstantGlobal = Val; } 00123 00124 /// If the value is "Thread Local", its value isn't shared by the threads. 00125 bool isThreadLocal() const { return isThreadLocalSymbol; } 00126 void setThreadLocal(bool Val) { isThreadLocalSymbol = Val; } 00127 00128 /// copyAttributesFrom - copy all additional attributes (those not needed to 00129 /// create a GlobalVariable) from the GlobalVariable Src to this one. 00130 void copyAttributesFrom(const GlobalValue *Src); 00131 00132 /// removeFromParent - This method unlinks 'this' from the containing module, 00133 /// but does not delete it. 00134 /// 00135 virtual void removeFromParent(); 00136 00137 /// eraseFromParent - This method unlinks 'this' from the containing module 00138 /// and deletes it. 00139 /// 00140 virtual void eraseFromParent(); 00141 00142 /// Override Constant's implementation of this method so we can 00143 /// replace constant initializers. 00144 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U); 00145 00146 // Methods for support type inquiry through isa, cast, and dyn_cast: 00147 static inline bool classof(const GlobalVariable *) { return true; } 00148 static inline bool classof(const Value *V) { 00149 return V->getValueID() == Value::GlobalVariableVal; 00150 } 00151 }; 00152 00153 template <> 00154 struct OperandTraits<GlobalVariable> : OptionalOperandTraits<> { 00155 }; 00156 00157 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value) 00158 00159 } // End llvm namespace 00160 00161 #endif