Oops! didn't mean to put this in there yet.
[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 operator=(const GlobalVariable &);     // Do not implement
38   GlobalVariable(const GlobalVariable &);     // Do not implement
39
40   void setParent(Module *parent);
41
42   GlobalVariable *Prev, *Next;
43   void setNext(GlobalVariable *N) { Next = N; }
44   void setPrev(GlobalVariable *N) { Prev = N; }
45
46   bool isConstantGlobal;               // Is this a global constant?
47   Use Initializer;
48
49 public:
50   /// GlobalVariable ctor - If a parent module is specified, the global is
51   /// automatically inserted into the end of the specified modules global list.
52   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
53                  Constant *Initializer = 0, const std::string &Name = "",
54                  Module *Parent = 0);
55   /// GlobalVariable ctor - This creates a global and inserts it before the
56   /// specified other global.
57   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
58                  Constant *Initializer, const std::string &Name,
59                  GlobalVariable *InsertBefore);
60   
61   /// isExternal - Is this global variable lacking an initializer?  If so, the
62   /// global variable is defined in some other translation unit, and is thus
63   /// externally defined here.
64   ///
65   virtual bool isExternal() const { return getNumOperands() == 0; }
66
67   /// hasInitializer - Unless a global variable isExternal(), it has an
68   /// initializer.  The initializer for the global variable/constant is held by
69   /// Initializer if an initializer is specified.
70   ///
71   inline bool hasInitializer() const { return !isExternal(); }
72
73   /// getInitializer - Return the initializer for this global variable.  It is
74   /// illegal to call this method if the global is external, because we cannot
75   /// tell what the value is initialized to!
76   ///
77   inline Constant *getInitializer() const {
78     assert(hasInitializer() && "GV doesn't have initializer!");
79     return reinterpret_cast<Constant*>(Initializer.get());
80   }
81   inline Constant *getInitializer() {
82     assert(hasInitializer() && "GV doesn't have initializer!");
83     return reinterpret_cast<Constant*>(Initializer.get());
84   }
85   inline void setInitializer(Constant *CPV) {
86     if (CPV == 0) {
87       if (hasInitializer()) {
88         Initializer.set(0);
89         NumOperands = 0;
90       }
91     } else {
92       if (!hasInitializer())
93         NumOperands = 1;
94       Initializer.set(CPV);
95     }
96   }
97
98   // getNext/Prev - Return the next or previous global variable in the list.
99         GlobalVariable *getNext()       { return Next; }
100   const GlobalVariable *getNext() const { return Next; }
101         GlobalVariable *getPrev()       { return Prev; }
102   const GlobalVariable *getPrev() const { return Prev; }
103
104   /// If the value is a global constant, its value is immutable throughout the
105   /// runtime execution of the program.  Assigning a value into the constant
106   /// leads to undefined behavior.
107   ///
108   bool isConstant() const { return isConstantGlobal; }
109   void setConstant(bool Value) { isConstantGlobal = Value; }
110
111   /// removeFromParent - This method unlinks 'this' from the containing module,
112   /// but does not delete it.
113   ///
114   void removeFromParent();
115
116   /// eraseFromParent - This method unlinks 'this' from the containing module
117   /// and deletes it.
118   ///
119   void eraseFromParent();
120
121   /// Override Constant's implementation of this method so we can
122   /// replace constant initializers.
123   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
124
125   virtual void print(std::ostream &OS) const;
126
127   // Methods for support type inquiry through isa, cast, and dyn_cast:
128   static inline bool classof(const GlobalVariable *) { return true; }
129   static inline bool classof(const Value *V) {
130     return V->getValueType() == Value::GlobalVariableVal;
131   }
132 };
133
134 } // End llvm namespace
135
136 #endif