064d3ef6d00c14caa9d292aa1ebaa4b1c294879f
[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
18 class Module;
19 class Constant;
20 class PointerType;
21 template<typename SC> struct ilist_traits;
22 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
23          typename SubClass> class SymbolTableListTraits;
24
25 class GlobalVariable : public GlobalValue {
26   friend class SymbolTableListTraits<GlobalVariable, Module, Module,
27                                      ilist_traits<GlobalVariable> >;
28   void setParent(Module *parent);
29
30   GlobalVariable *Prev, *Next;
31   void setNext(GlobalVariable *N) { Next = N; }
32   void setPrev(GlobalVariable *N) { Prev = N; }
33
34   bool isConstantGlobal;               // Is this a global constant?
35 public:
36   /// GlobalVariable ctor - If a parent module is specified, the global is
37   /// automatically inserted into the end of the specified modules global list.
38   ///
39   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
40                  Constant *Initializer = 0, const std::string &Name = "",
41                  Module *Parent = 0);
42
43   // Specialize setName to handle symbol table majik...
44   virtual void setName(const std::string &name, SymbolTable *ST = 0);
45
46   /// isExternal - Is this global variable lacking an initializer?  If so, the
47   /// global variable is defined in some other translation unit, and is thus
48   /// externally defined here.
49   ///
50   virtual bool isExternal() const { return Operands.empty(); }
51
52   /// hasInitializer - Unless a global variable isExternal(), it has an
53   /// initializer.  The initializer for the global variable/constant is held by
54   /// Operands[0] if an initializer is specified.
55   ///
56   inline bool hasInitializer() const { return !isExternal(); }
57
58   /// getInitializer - Return the initializer for this global variable.  It is
59   /// illegal to call this method if the global is external, because we cannot
60   /// tell what the value is initialized to!
61   ///
62   inline Constant *getInitializer() const {
63     assert(hasInitializer() && "GV doesn't have initializer!");
64     return (Constant*)Operands[0].get();
65   }
66   inline Constant *getInitializer() {
67     assert(hasInitializer() && "GV doesn't have initializer!");
68     return (Constant*)Operands[0].get();
69   }
70   inline void setInitializer(Constant *CPV) {
71     if (CPV == 0) {
72       if (hasInitializer()) Operands.pop_back();
73     } else {
74       if (!hasInitializer()) Operands.push_back(Use(0, this));
75       Operands[0] = (Value*)CPV;
76     }
77   }
78
79   // getNext/Prev - Return the next or previous global variable in the list.
80         GlobalVariable *getNext()       { return Next; }
81   const GlobalVariable *getNext() const { return Next; }
82         GlobalVariable *getPrev()       { return Prev; }
83   const GlobalVariable *getPrev() const { return Prev; }
84
85   /// If the value is a global constant, its value is immutable throughout the
86   /// runtime execution of the program.  Assigning a value into the constant
87   /// leads to undefined behavior.
88   ///
89   bool isConstant() const { return isConstantGlobal; }
90   void setConstant(bool Value) { isConstantGlobal = Value; }
91   
92   virtual void print(std::ostream &OS) const;
93
94   // Methods for support type inquiry through isa, cast, and dyn_cast:
95   static inline bool classof(const GlobalVariable *) { return true; }
96   static inline bool classof(const Value *V) {
97     return V->getValueType() == Value::GlobalVariableVal;
98   }
99 };
100
101 #endif