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);
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 ctor - If a parent module is specified, the global is
36 /// automatically inserted into the end of the specified modules global list.
38 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
39 Constant *Initializer = 0, const std::string &Name = "",
42 // Specialize setName to handle symbol table majik...
43 virtual void setName(const std::string &name, SymbolTable *ST = 0);
45 /// isExternal - Is this global variable lacking an initializer? If so, the
46 /// global variable is defined in some other translation unit, and is thus
47 /// externally defined here.
49 virtual bool isExternal() const { return Operands.empty(); }
51 /// hasInitializer - Unless a global variable isExternal(), it has an
52 /// initializer. The initializer for the global variable/constant is held by
53 /// Operands[0] if an initializer is specified.
55 inline bool hasInitializer() const { return !isExternal(); }
57 /// getInitializer - Return the initializer for this global variable. It is
58 /// illegal to call this method if the global is external, because we cannot
59 /// tell what the value is initialized to!
61 inline Constant *getInitializer() const {
62 assert(hasInitializer() && "GV doesn't have initializer!");
63 return (Constant*)Operands[0].get();
65 inline Constant *getInitializer() {
66 assert(hasInitializer() && "GV doesn't have initializer!");
67 return (Constant*)Operands[0].get();
69 inline void setInitializer(Constant *CPV) {
71 if (hasInitializer()) Operands.pop_back();
73 if (!hasInitializer()) Operands.push_back(Use(0, this));
74 Operands[0] = (Value*)CPV;
78 // getNext/Prev - Return the next or previous global variable in the list.
79 GlobalVariable *getNext() { return Next; }
80 const GlobalVariable *getNext() const { return Next; }
81 GlobalVariable *getPrev() { return Prev; }
82 const GlobalVariable *getPrev() const { return Prev; }
84 /// If the value is a global constant, its value is immutable throughout the
85 /// runtime execution of the program. Assigning a value into the constant
86 /// leads to undefined behavior.
88 bool isConstant() const { return isConstantGlobal; }
89 void setConstant(bool Value) { isConstantGlobal = Value; }
91 virtual void print(std::ostream &OS) const;
93 // Methods for support type inquiry through isa, cast, and dyn_cast:
94 static inline bool classof(const GlobalVariable *) { return true; }
95 static inline bool classof(const Value *V) {
96 return V->getValueType() == Value::GlobalVariableVal;