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