Add support for global constants, and for initializers for constants
[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/User.h"
17 class Module;
18 class ConstPoolVal;
19 class PointerType;
20
21 class GlobalVariable : public User {
22   Module *Parent;                  // The module that contains this method
23
24   friend class ValueHolder<GlobalVariable, Module, Module>;
25   void setParent(Module *parent) { Parent = parent; }
26
27   bool Constant;                   // Is this a global constant?
28 public:
29   GlobalVariable(const Type *Ty, bool isConstant, ConstPoolVal *Initializer = 0,
30                  const string &Name = "");
31   ~GlobalVariable() {}
32
33   // getType - Global variables are always pointers
34   inline const PointerType *getType() const {
35     return (const PointerType*)User::getType();
36   }
37
38   // Specialize setName to handle symbol table majik...
39   virtual void setName(const string &name, SymbolTable *ST = 0);
40
41   inline       Module *getParent()       { return Parent; }
42   inline const Module *getParent() const { return Parent; }
43
44   // The initializer for the global variable/constant is held by Operands[0] if
45   // an initializer is specified.
46   //
47   inline bool hasInitializer() const { return !Operands.empty(); }
48   inline const ConstPoolVal *getInitializer() const {
49     return Operands[0]->castConstantAsserting();
50   }
51   inline ConstPoolVal *getInitializer() {
52     return Operands[0]->castConstantAsserting();
53   }
54   inline void setInitializer(ConstPoolVal *CPV) { Operands[0] = (Value*)CPV; }
55
56
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.
60   //
61   inline bool isConstant() const { return Constant; }
62 };
63
64 #endif