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