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