1 //===-- llvm/Global.h - Class to represent a global variable -----*- C++ -*--=//
3 // This file contains the declaration of the GlobalVariable class, which
4 // represents a single global variable (or constant) in the VM.
6 // Global variables are constant pointers that refer to hunks of space that are
7 // allocated by either the VM, or by the linker in a static compiler. A global
8 // variable may have an intial value, which is copied into the executables .data
9 // area. Global Constants are required to have initializers.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_GLOBAL_VARIABLE_H
14 #define LLVM_GLOBAL_VARIABLE_H
16 #include "llvm/GlobalValue.h"
21 template<typename SC> struct ilist_traits;
22 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
23 typename SubClass> class SymbolTableListTraits;
25 class GlobalVariable : public GlobalValue {
26 friend class SymbolTableListTraits<GlobalVariable, Module, Module,
27 ilist_traits<GlobalVariable> >;
28 void setParent(Module *parent);
30 GlobalVariable *Prev, *Next;
31 void setNext(GlobalVariable *N) { Next = N; }
32 void setPrev(GlobalVariable *N) { Prev = N; }
34 bool isConstantGlobal; // Is this a global constant?
36 /// GlobalVariable ctor - If a parent module is specified, the global is
37 /// automatically inserted into the end of the specified modules global list.
39 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
40 Constant *Initializer = 0, const std::string &Name = "",
43 // Specialize setName to handle symbol table majik...
44 virtual void setName(const std::string &name, SymbolTable *ST = 0);
46 /// isExternal - Is this global variable lacking an initializer? If so, the
47 /// global variable is defined in some other translation unit, and is thus
48 /// externally defined here.
50 virtual bool isExternal() const { return Operands.empty(); }
52 /// hasInitializer - Unless a global variable isExternal(), it has an
53 /// initializer. The initializer for the global variable/constant is held by
54 /// Operands[0] if an initializer is specified.
56 inline bool hasInitializer() const { return !isExternal(); }
58 /// getInitializer - Return the initializer for this global variable. It is
59 /// illegal to call this method if the global is external, because we cannot
60 /// tell what the value is initialized to!
62 inline Constant *getInitializer() const {
63 assert(hasInitializer() && "GV doesn't have initializer!");
64 return (Constant*)Operands[0].get();
66 inline Constant *getInitializer() {
67 assert(hasInitializer() && "GV doesn't have initializer!");
68 return (Constant*)Operands[0].get();
70 inline void setInitializer(Constant *CPV) {
72 if (hasInitializer()) Operands.pop_back();
74 if (!hasInitializer()) Operands.push_back(Use(0, this));
75 Operands[0] = (Value*)CPV;
79 // getNext/Prev - Return the next or previous global variable in the list.
80 GlobalVariable *getNext() { return Next; }
81 const GlobalVariable *getNext() const { return Next; }
82 GlobalVariable *getPrev() { return Prev; }
83 const GlobalVariable *getPrev() const { return Prev; }
85 /// If the value is a global constant, its value is immutable throughout the
86 /// runtime execution of the program. Assigning a value into the constant
87 /// leads to undefined behavior.
89 bool isConstant() const { return isConstantGlobal; }
90 void setConstant(bool Value) { isConstantGlobal = Value; }
92 virtual void print(std::ostream &OS) const;
94 // Methods for support type inquiry through isa, cast, and dyn_cast:
95 static inline bool classof(const GlobalVariable *) { return true; }
96 static inline bool classof(const Value *V) {
97 return V->getValueType() == Value::GlobalVariableVal;