remove a bunch of extraneous LLVMContext arguments
[oota-llvm.git] / include / llvm / GlobalVariable.h
1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 #include "llvm/OperandTraits.h"
25 #include "llvm/ADT/ilist_node.h"
26
27 namespace llvm {
28
29 class Module;
30 class Constant;
31 template<typename ValueSubClass, typename ItemParentClass>
32   class SymbolTableListTraits;
33
34 class GlobalVariable : public GlobalValue, public ilist_node<GlobalVariable> {
35   friend class SymbolTableListTraits<GlobalVariable, Module>;
36   void *operator new(size_t, unsigned);       // Do not implement
37   void operator=(const GlobalVariable &);     // Do not implement
38   GlobalVariable(const GlobalVariable &);     // Do not implement
39
40   void setParent(Module *parent);
41
42   bool isConstantGlobal : 1;           // Is this a global constant?
43   bool isThreadLocalSymbol : 1;        // Is this symbol "Thread Local"?
44
45 public:
46   // allocate space for exactly one operand
47   void *operator new(size_t s) {
48     return User::operator new(s, 1);
49   }
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 Twine &Name = "",
54                  bool ThreadLocal = false, unsigned AddressSpace = 0);
55   /// GlobalVariable ctor - This creates a global and inserts it before the
56   /// specified other global.
57   GlobalVariable(Module &M, const Type *Ty, bool isConstant,
58                  LinkageTypes Linkage, Constant *Initializer,
59                  const Twine &Name,
60                  GlobalVariable *InsertBefore = 0, bool ThreadLocal = false,
61                  unsigned AddressSpace = 0);
62
63   ~GlobalVariable() {
64     NumOperands = 1; // FIXME: needed by operator delete
65   }
66
67   /// Provide fast operand accessors
68   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
69
70   /// isDeclaration - Is this global variable lacking an initializer?  If so, 
71   /// the global variable is defined in some other translation unit, and is thus
72   /// only a declaration here.
73   virtual bool isDeclaration() const { return getNumOperands() == 0; }
74
75   /// hasInitializer - Unless a global variable isExternal(), it has an
76   /// initializer.  The initializer for the global variable/constant is held by
77   /// Initializer if an initializer is specified.
78   ///
79   inline bool hasInitializer() const { return !isDeclaration(); }
80
81   /// hasDefinitiveInitializer - Whether the global variable has an initializer,
82   /// and this is the initializer that will be used in the final executable.
83   inline bool hasDefinitiveInitializer() const {
84     return hasInitializer() &&
85       // The initializer of a global variable with weak linkage may change at
86       // link time.
87       !mayBeOverridden();
88   }
89
90   /// getInitializer - Return the initializer for this global variable.  It is
91   /// illegal to call this method if the global is external, because we cannot
92   /// tell what the value is initialized to!
93   ///
94   inline /*const FIXME*/ Constant *getInitializer() const {
95     assert(hasInitializer() && "GV doesn't have initializer!");
96     return static_cast<Constant*>(Op<0>().get());
97   }
98   inline Constant *getInitializer() {
99     assert(hasInitializer() && "GV doesn't have initializer!");
100     return static_cast<Constant*>(Op<0>().get());
101   }
102   inline void setInitializer(Constant *CPV) {
103     if (CPV == 0) {
104       if (hasInitializer()) {
105         Op<0>().set(0);
106         NumOperands = 0;
107       }
108     } else {
109       if (!hasInitializer())
110         NumOperands = 1;
111       Op<0>().set(CPV);
112     }
113   }
114
115   /// If the value is a global constant, its value is immutable throughout the
116   /// runtime execution of the program.  Assigning a value into the constant
117   /// leads to undefined behavior.
118   ///
119   bool isConstant() const { return isConstantGlobal; }
120   void setConstant(bool Val) { isConstantGlobal = Val; }
121
122   /// If the value is "Thread Local", its value isn't shared by the threads.
123   bool isThreadLocal() const { return isThreadLocalSymbol; }
124   void setThreadLocal(bool Val) { isThreadLocalSymbol = Val; }
125
126   /// copyAttributesFrom - copy all additional attributes (those not needed to
127   /// create a GlobalVariable) from the GlobalVariable Src to this one.
128   void copyAttributesFrom(const GlobalValue *Src);
129
130   /// removeFromParent - This method unlinks 'this' from the containing module,
131   /// but does not delete it.
132   ///
133   virtual void removeFromParent();
134
135   /// eraseFromParent - This method unlinks 'this' from the containing module
136   /// and deletes it.
137   ///
138   virtual void eraseFromParent();
139
140   /// Override Constant's implementation of this method so we can
141   /// replace constant initializers.
142   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
143
144   // Methods for support type inquiry through isa, cast, and dyn_cast:
145   static inline bool classof(const GlobalVariable *) { return true; }
146   static inline bool classof(const Value *V) {
147     return V->getValueID() == Value::GlobalVariableVal;
148   }
149 };
150
151 template <>
152 struct OperandTraits<GlobalVariable> : public OptionalOperandTraits<> {
153 };
154
155 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
156
157 } // End llvm namespace
158
159 #endif