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 operator=(const GlobalVariable &); // Do not implement 00038 GlobalVariable(const GlobalVariable &); // Do not implement 00039 00040 void setParent(Module *parent); 00041 00042 GlobalVariable *Prev, *Next; 00043 void setNext(GlobalVariable *N) { Next = N; } 00044 void setPrev(GlobalVariable *N) { Prev = N; } 00045 00046 bool isConstantGlobal; // Is this a global constant? 00047 Use Initializer; 00048 00049 public: 00050 /// GlobalVariable ctor - If a parent module is specified, the global is 00051 /// automatically inserted into the end of the specified modules global list. 00052 /// 00053 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage, 00054 Constant *Initializer = 0, const std::string &Name = "", 00055 Module *Parent = 0); 00056 00057 /// isExternal - Is this global variable lacking an initializer? If so, the 00058 /// global variable is defined in some other translation unit, and is thus 00059 /// externally defined here. 00060 /// 00061 virtual bool isExternal() const { return getNumOperands() == 0; } 00062 00063 /// hasInitializer - Unless a global variable isExternal(), it has an 00064 /// initializer. The initializer for the global variable/constant is held by 00065 /// Initializer if an initializer is specified. 00066 /// 00067 inline bool hasInitializer() const { return !isExternal(); } 00068 00069 /// getInitializer - Return the initializer for this global variable. It is 00070 /// illegal to call this method if the global is external, because we cannot 00071 /// tell what the value is initialized to! 00072 /// 00073 inline Constant *getInitializer() const { 00074 assert(hasInitializer() && "GV doesn't have initializer!"); 00075 return reinterpret_cast<Constant*>(Initializer.get()); 00076 } 00077 inline Constant *getInitializer() { 00078 assert(hasInitializer() && "GV doesn't have initializer!"); 00079 return reinterpret_cast<Constant*>(Initializer.get()); 00080 } 00081 inline void setInitializer(Constant *CPV) { 00082 if (CPV == 0) { 00083 if (hasInitializer()) { 00084 Initializer.set(0); 00085 NumOperands = 0; 00086 } 00087 } else { 00088 if (!hasInitializer()) 00089 NumOperands = 1; 00090 Initializer.set(CPV); 00091 } 00092 } 00093 00094 // getNext/Prev - Return the next or previous global variable in the list. 00095 GlobalVariable *getNext() { return Next; } 00096 const GlobalVariable *getNext() const { return Next; } 00097 GlobalVariable *getPrev() { return Prev; } 00098 const GlobalVariable *getPrev() const { return Prev; } 00099 00100 /// If the value is a global constant, its value is immutable throughout the 00101 /// runtime execution of the program. Assigning a value into the constant 00102 /// leads to undefined behavior. 00103 /// 00104 bool isConstant() const { return isConstantGlobal; } 00105 void setConstant(bool Value) { isConstantGlobal = Value; } 00106 00107 /// removeFromParent - This method unlinks 'this' from the containing module, 00108 /// but does not delete it. 00109 /// 00110 void removeFromParent(); 00111 00112 /// eraseFromParent - This method unlinks 'this' from the containing module 00113 /// and deletes it. 00114 /// 00115 void eraseFromParent(); 00116 00117 /// Override Constant's implementation of this method so we can 00118 /// replace constant initializers. 00119 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U); 00120 00121 virtual void print(std::ostream &OS) const; 00122 00123 // Methods for support type inquiry through isa, cast, and dyn_cast: 00124 static inline bool classof(const GlobalVariable *) { return true; } 00125 static inline bool classof(const Value *V) { 00126 return V->getValueType() == Value::GlobalVariableVal; 00127 } 00128 }; 00129 00130 } // End llvm namespace 00131 00132 #endif