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"
20 template<typename SC> struct ilist_traits;
21 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
22 typename SubClass> class SymbolTableListTraits;
24 class GlobalVariable : public GlobalValue {
25 friend class SymbolTableListTraits<GlobalVariable, Module, Module,
26 ilist_traits<GlobalVariable> >;
27 void setParent(Module *parent) { Parent = parent; }
29 GlobalVariable *Prev, *Next;
30 void setNext(GlobalVariable *N) { Next = N; }
31 void setPrev(GlobalVariable *N) { Prev = N; }
33 bool isConstantGlobal; // Is this a global constant?
35 GlobalVariable(const Type *Ty, bool isConstant, bool isInternal,
36 Constant *Initializer = 0, const std::string &Name = "");
39 // Specialize setName to handle symbol table majik...
40 virtual void setName(const std::string &name, SymbolTable *ST = 0);
42 // The initializer for the global variable/constant is held by Operands[0] if
43 // an initializer is specified.
45 inline bool hasInitializer() const { return !Operands.empty(); }
46 inline Constant *getInitializer() const {
47 assert(hasInitializer() && "GV doesn't have initializer!");
48 return (Constant*)Operands[0].get();
50 inline Constant *getInitializer() {
51 assert(hasInitializer() && "GV doesn't have initializer!");
52 return (Constant*)Operands[0].get();
54 inline void setInitializer(Constant *CPV) {
56 if (hasInitializer()) Operands.pop_back();
58 if (!hasInitializer()) Operands.push_back(Use(0, this));
59 Operands[0] = (Value*)CPV;
63 // getNext/Prev - Return the next or previous instruction in the list. The
64 // last node in the list is a terminator instruction.
65 GlobalVariable *getNext() { return Next; }
66 const GlobalVariable *getNext() const { return Next; }
67 GlobalVariable *getPrev() { return Prev; }
68 const GlobalVariable *getPrev() const { return Prev; }
70 // If the value is a global constant, its value is immutable throughout the
71 // runtime execution of the program. Assigning a value into the constant
72 // leads to undefined behavior.
74 inline bool isConstant() const { return isConstantGlobal; }
76 virtual void print(std::ostream &OS) const;
78 // Methods for support type inquiry through isa, cast, and dyn_cast:
79 static inline bool classof(const GlobalVariable *) { return true; }
80 static inline bool classof(const Value *V) {
81 return V->getValueType() == Value::GlobalVariableVal;