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 class GlobalVariable : public GlobalValue {
22 friend class ValueHolder<GlobalVariable, Module, Module>;
23 void setParent(Module *parent) { Parent = parent; }
25 bool isConstantGlobal; // Is this a global constant?
27 GlobalVariable(const Type *Ty, bool isConstant, bool isInternal,
28 Constant *Initializer = 0, const std::string &Name = "");
31 // Specialize setName to handle symbol table majik...
32 virtual void setName(const std::string &name, SymbolTable *ST = 0);
34 // The initializer for the global variable/constant is held by Operands[0] if
35 // an initializer is specified.
37 inline bool hasInitializer() const { return !Operands.empty(); }
38 inline Constant *getInitializer() const {
39 assert(hasInitializer() && "GV doesn't have initializer!");
40 return (Constant*)Operands[0].get();
42 inline Constant *getInitializer() {
43 assert(hasInitializer() && "GV doesn't have initializer!");
44 return (Constant*)Operands[0].get();
46 inline void setInitializer(Constant *CPV) {
48 if (hasInitializer()) Operands.pop_back();
50 if (!hasInitializer()) Operands.push_back(Use(0, this));
51 Operands[0] = (Value*)CPV;
56 // If the value is a global constant, its value is immutable throughout the
57 // runtime execution of the program. Assigning a value into the constant
58 // leads to undefined behavior.
60 inline bool isConstant() const { return isConstantGlobal; }
62 virtual void print(std::ostream &OS) const;
64 // Methods for support type inquiry through isa, cast, and dyn_cast:
65 static inline bool classof(const GlobalVariable *) { return true; }
66 static inline bool classof(const Value *V) {
67 return V->getValueType() == Value::GlobalVariableVal;