Remove a ton of extraneous #includes
[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, LinkageTypes Linkage,
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   /// isExternal - Is this global variable lacking an initializer?  If so, the
46   /// global variable is defined in some other translation unit, and is thus
47   /// externally defined here.
48   ///
49   virtual bool isExternal() const { return Operands.empty(); }
50
51   /// hasInitializer - Unless a global variable isExternal(), it has an
52   /// initializer.  The initializer for the global variable/constant is held by
53   /// Operands[0] if an initializer is specified.
54   ///
55   inline bool hasInitializer() const { return !isExternal(); }
56
57   /// getInitializer - Return the initializer for this global variable.  It is
58   /// illegal to call this method if the global is external, because we cannot
59   /// tell what the value is initialized to!
60   ///
61   inline Constant *getInitializer() const {
62     assert(hasInitializer() && "GV doesn't have initializer!");
63     return (Constant*)Operands[0].get();
64   }
65   inline Constant *getInitializer() {
66     assert(hasInitializer() && "GV doesn't have initializer!");
67     return (Constant*)Operands[0].get();
68   }
69   inline void setInitializer(Constant *CPV) {
70     if (CPV == 0) {
71       if (hasInitializer()) Operands.pop_back();
72     } else {
73       if (!hasInitializer()) Operands.push_back(Use(0, this));
74       Operands[0] = (Value*)CPV;
75     }
76   }
77
78   // getNext/Prev - Return the next or previous global variable in the list.
79         GlobalVariable *getNext()       { return Next; }
80   const GlobalVariable *getNext() const { return Next; }
81         GlobalVariable *getPrev()       { return Prev; }
82   const GlobalVariable *getPrev() const { return Prev; }
83
84   /// If the value is a global constant, its value is immutable throughout the
85   /// runtime execution of the program.  Assigning a value into the constant
86   /// leads to undefined behavior.
87   ///
88   bool isConstant() const { return isConstantGlobal; }
89   void setConstant(bool Value) { isConstantGlobal = Value; }
90   
91   virtual void print(std::ostream &OS) const;
92
93   // Methods for support type inquiry through isa, cast, and dyn_cast:
94   static inline bool classof(const GlobalVariable *) { return true; }
95   static inline bool classof(const Value *V) {
96     return V->getValueType() == Value::GlobalVariableVal;
97   }
98 };
99
100 #endif