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/User.h"
21 class GlobalVariable : public User {
22 Module *Parent; // The module that contains this method
24 friend class ValueHolder<GlobalVariable, Module, Module>;
25 void setParent(Module *parent) { Parent = parent; }
27 bool Constant; // Is this a global constant?
29 GlobalVariable(const Type *Ty, bool isConstant, ConstPoolVal *Initializer = 0,
30 const string &Name = "");
33 // getType - Global variables are always pointers
34 inline const PointerType *getType() const {
35 return (const PointerType*)User::getType();
38 // Specialize setName to handle symbol table majik...
39 virtual void setName(const string &name, SymbolTable *ST = 0);
41 inline Module *getParent() { return Parent; }
42 inline const Module *getParent() const { return Parent; }
44 // The initializer for the global variable/constant is held by Operands[0] if
45 // an initializer is specified.
47 inline bool hasInitializer() const { return !Operands.empty(); }
48 inline const ConstPoolVal *getInitializer() const {
49 return (const ConstPoolVal*)Operands[0].get();
51 inline ConstPoolVal *getInitializer() {
52 return (ConstPoolVal*)Operands[0].get();
54 inline void setInitializer(ConstPoolVal *CPV) { Operands[0] = (Value*)CPV; }
57 // If the value is a global constant, its value is immutable throughout the
58 // runtime execution of the program. Assigning a value into the constant
59 // leads to undefined behavior.
61 inline bool isConstant() const { return Constant; }
63 // Methods for support type inquiry through isa, cast, and dyn_cast:
64 static inline bool isa(const GlobalVariable *) { return true; }
65 static inline bool isa(const Value *V) {
66 return V->getValueType() == Value::GlobalVal;