LLVM API Documentation
00001 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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 00025 namespace llvm { 00026 00027 class Module; 00028 class Constant; 00029 class PointerType; 00030 template<typename SC> struct ilist_traits; 00031 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass, 00032 typename SubClass> class SymbolTableListTraits; 00033 00034 class GlobalVariable : public GlobalValue { 00035 friend class SymbolTableListTraits<GlobalVariable, Module, Module, 00036 ilist_traits<GlobalVariable> >; 00037 void setParent(Module *parent); 00038 00039 GlobalVariable *Prev, *Next; 00040 void setNext(GlobalVariable *N) { Next = N; } 00041 void setPrev(GlobalVariable *N) { Prev = N; } 00042 00043 bool isConstantGlobal; // Is this a global constant? 00044 public: 00045 /// GlobalVariable ctor - If a parent module is specified, the global is 00046 /// automatically inserted into the end of the specified modules global list. 00047 /// 00048 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage, 00049 Constant *Initializer = 0, const std::string &Name = "", 00050 Module *Parent = 0); 00051 00052 // Specialize setName to handle symbol table majik... 00053 virtual void setName(const std::string &name, SymbolTable *ST = 0); 00054 00055 /// isExternal - Is this global variable lacking an initializer? If so, the 00056 /// global variable is defined in some other translation unit, and is thus 00057 /// externally defined here. 00058 /// 00059 virtual bool isExternal() const { return Operands.empty(); } 00060 00061 /// hasInitializer - Unless a global variable isExternal(), it has an 00062 /// initializer. The initializer for the global variable/constant is held by 00063 /// Operands[0] if an initializer is specified. 00064 /// 00065 inline bool hasInitializer() const { return !isExternal(); } 00066 00067 /// getInitializer - Return the initializer for this global variable. It is 00068 /// illegal to call this method if the global is external, because we cannot 00069 /// tell what the value is initialized to! 00070 /// 00071 inline Constant *getInitializer() const { 00072 assert(hasInitializer() && "GV doesn't have initializer!"); 00073 return reinterpret_cast<Constant*>(Operands[0].get()); 00074 } 00075 inline Constant *getInitializer() { 00076 assert(hasInitializer() && "GV doesn't have initializer!"); 00077 return reinterpret_cast<Constant*>(Operands[0].get()); 00078 } 00079 inline void setInitializer(Constant *CPV) { 00080 if (CPV == 0) { 00081 if (hasInitializer()) Operands.pop_back(); 00082 } else { 00083 if (!hasInitializer()) Operands.push_back(Use(0, this)); 00084 Operands[0] = reinterpret_cast<Value*>(CPV); 00085 } 00086 } 00087 00088 // getNext/Prev - Return the next or previous global variable in the list. 00089 GlobalVariable *getNext() { return Next; } 00090 const GlobalVariable *getNext() const { return Next; } 00091 GlobalVariable *getPrev() { return Prev; } 00092 const GlobalVariable *getPrev() const { return Prev; } 00093 00094 /// If the value is a global constant, its value is immutable throughout the 00095 /// runtime execution of the program. Assigning a value into the constant 00096 /// leads to undefined behavior. 00097 /// 00098 bool isConstant() const { return isConstantGlobal; } 00099 void setConstant(bool Value) { isConstantGlobal = Value; } 00100 00101 /// removeFromParent - This method unlinks 'this' from the containing module, 00102 /// but does not delete it. 00103 /// 00104 void removeFromParent(); 00105 00106 /// eraseFromParent - This method unlinks 'this' from the containing module 00107 /// and deletes it. 00108 /// 00109 void eraseFromParent(); 00110 00111 /// Override Constant's implementation of this method so we can 00112 /// replace constant initializers. 00113 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, 00114 bool DisableChecking = false); 00115 00116 virtual void print(std::ostream &OS) const; 00117 00118 // Methods for support type inquiry through isa, cast, and dyn_cast: 00119 static inline bool classof(const GlobalVariable *) { return true; } 00120 static inline bool classof(const Value *V) { 00121 return V->getValueType() == Value::GlobalVariableVal; 00122 } 00123 }; 00124 00125 } // End llvm namespace 00126 00127 #endif