2e298058f5245a086036588eb72159a98e8c1556
[oota-llvm.git] / include / llvm / GlobalVariable.h
1 //===-- llvm/Global.h - Class to represent a global variable -----*- C++ -*--=//
2 //
3 // This file contains the declaration of the GlobalVariable class, which
4 // represents a single global variable (or constant) in the VM.
5 //
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.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_GLOBAL_VARIABLE_H
14 #define LLVM_GLOBAL_VARIABLE_H
15
16 #include "llvm/GlobalValue.h"
17 class Module;
18 class ConstPoolVal;
19 class PointerType;
20
21 class GlobalVariable : public GlobalValue {
22   friend class ValueHolder<GlobalVariable, Module, Module>;
23   void setParent(Module *parent) { Parent = parent; }
24
25   bool Constant;                   // Is this a global constant?
26 public:
27   GlobalVariable(const Type *Ty, bool isConstant, ConstPoolVal *Initializer = 0,
28                  const string &Name = "");
29   ~GlobalVariable() {}
30
31   // Specialize setName to handle symbol table majik...
32   virtual void setName(const string &name, SymbolTable *ST = 0);
33
34   // The initializer for the global variable/constant is held by Operands[0] if
35   // an initializer is specified.
36   //
37   inline bool hasInitializer() const { return !Operands.empty(); }
38   inline const ConstPoolVal *getInitializer() const {
39     return (const ConstPoolVal*)Operands[0].get();
40   }
41   inline ConstPoolVal *getInitializer() {
42     return (ConstPoolVal*)Operands[0].get();
43   }
44   inline void setInitializer(ConstPoolVal *CPV) {
45     if (CPV == 0) {
46       if (hasInitializer()) Operands.pop_back();
47     } else {
48       if (!hasInitializer()) Operands.push_back(Use(0, this));
49       Operands[0] = (Value*)CPV;
50     }
51   }
52
53
54   // If the value is a global constant, its value is immutable throughout the
55   // runtime execution of the program.  Assigning a value into the constant
56   // leads to undefined behavior.
57   //
58   inline bool isConstant() const { return Constant; }
59
60   // Methods for support type inquiry through isa, cast, and dyn_cast:
61   static inline bool classof(const GlobalVariable *) { return true; }
62   static inline bool classof(const Value *V) {
63     return V->getValueType() == Value::GlobalVariableVal;
64   }
65 };
66
67 #endif