f8b22e4fdad97064af4bafc8628d92274108179c
[oota-llvm.git] / include / llvm / GlobalVariable.h
1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the GlobalVariable class, which
11 // represents a single global variable (or constant) in the VM.
12 //
13 // Global variables are constant pointers that refer to hunks of space that are
14 // allocated by either the VM, or by the linker in a static compiler.  A global
15 // variable may have an intial value, which is copied into the executables .data
16 // area.  Global Constants are required to have initializers.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_GLOBAL_VARIABLE_H
21 #define LLVM_GLOBAL_VARIABLE_H
22
23 #include "llvm/GlobalValue.h"
24
25 namespace llvm {
26
27 class Module;
28 class Constant;
29 class PointerType;
30 template<typename SC> struct ilist_traits;
31 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
32          typename SubClass> class SymbolTableListTraits;
33
34 class GlobalVariable : public GlobalValue {
35   friend class SymbolTableListTraits<GlobalVariable, Module, Module,
36                                      ilist_traits<GlobalVariable> >;
37   void setParent(Module *parent);
38
39   GlobalVariable *Prev, *Next;
40   void setNext(GlobalVariable *N) { Next = N; }
41   void setPrev(GlobalVariable *N) { Prev = N; }
42
43   bool isConstantGlobal;               // Is this a global constant?
44   Use Initializer;
45
46 public:
47   /// GlobalVariable ctor - If a parent module is specified, the global is
48   /// automatically inserted into the end of the specified modules global list.
49   ///
50   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
51                  Constant *Initializer = 0, const std::string &Name = "",
52                  Module *Parent = 0);
53
54   /// isExternal - Is this global variable lacking an initializer?  If so, the
55   /// global variable is defined in some other translation unit, and is thus
56   /// externally defined here.
57   ///
58   virtual bool isExternal() const { return getNumOperands() == 0; }
59
60   /// hasInitializer - Unless a global variable isExternal(), it has an
61   /// initializer.  The initializer for the global variable/constant is held by
62   /// Initializer if an initializer is specified.
63   ///
64   inline bool hasInitializer() const { return !isExternal(); }
65
66   /// getInitializer - Return the initializer for this global variable.  It is
67   /// illegal to call this method if the global is external, because we cannot
68   /// tell what the value is initialized to!
69   ///
70   inline Constant *getInitializer() const {
71     assert(hasInitializer() && "GV doesn't have initializer!");
72     return reinterpret_cast<Constant*>(Initializer.get());
73   }
74   inline Constant *getInitializer() {
75     assert(hasInitializer() && "GV doesn't have initializer!");
76     return reinterpret_cast<Constant*>(Initializer.get());
77   }
78   inline void setInitializer(Constant *CPV) {
79     if (CPV == 0) {
80       if (hasInitializer()) {
81         Initializer.set(0);
82         NumOperands = 0;
83       }
84     } else {
85       if (!hasInitializer())
86         NumOperands = 1;
87       Initializer.set(CPV);
88     }
89   }
90
91   // getNext/Prev - Return the next or previous global variable in the list.
92         GlobalVariable *getNext()       { return Next; }
93   const GlobalVariable *getNext() const { return Next; }
94         GlobalVariable *getPrev()       { return Prev; }
95   const GlobalVariable *getPrev() const { return Prev; }
96
97   /// If the value is a global constant, its value is immutable throughout the
98   /// runtime execution of the program.  Assigning a value into the constant
99   /// leads to undefined behavior.
100   ///
101   bool isConstant() const { return isConstantGlobal; }
102   void setConstant(bool Value) { isConstantGlobal = Value; }
103
104   /// removeFromParent - This method unlinks 'this' from the containing module,
105   /// but does not delete it.
106   ///
107   void removeFromParent();
108
109   /// eraseFromParent - This method unlinks 'this' from the containing module
110   /// and deletes it.
111   ///
112   void eraseFromParent();
113
114   /// Override Constant's implementation of this method so we can
115   /// replace constant initializers.
116   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
117                                            bool DisableChecking = false);
118
119   virtual void print(std::ostream &OS) const;
120
121   // Methods for support type inquiry through isa, cast, and dyn_cast:
122   static inline bool classof(const GlobalVariable *) { return true; }
123   static inline bool classof(const Value *V) {
124     return V->getValueType() == Value::GlobalVariableVal;
125   }
126 };
127
128 } // End llvm namespace
129
130 #endif