d7337984793ce9a6c66b7836a8f79c5bd9928c59
[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=(const GlobalVariable &);     // Do not implement
36   GlobalVariable(const GlobalVariable &);     // Do not implement
37
38   void setParent(Module *parent);
39
40   GlobalVariable *Prev, *Next;
41   void setNext(GlobalVariable *N) { Next = N; }
42   void setPrev(GlobalVariable *N) { Prev = N; }
43
44   bool isConstantGlobal : 1;           // Is this a global constant?
45   bool isThreadLocalSymbol : 1;        // Is this symbol "Thread Local"?
46   Use Initializer;
47
48 public:
49   /// GlobalVariable ctor - If a parent module is specified, the global is
50   /// automatically inserted into the end of the specified modules global list.
51   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
52                  Constant *Initializer = 0, const std::string &Name = "",
53                  Module *Parent = 0, bool ThreadLocal = false, 
54                  unsigned AddressSpace = 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, bool ThreadLocal = false, 
60                  unsigned AddressSpace = 0);
61   
62   /// isDeclaration - Is this global variable lacking an initializer?  If so, 
63   /// the global variable is defined in some other translation unit, and is thus
64   /// only a declaration here.
65   virtual bool isDeclaration() 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 !isDeclaration(); }
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   /// If the value is a global constant, its value is immutable throughout the
99   /// runtime execution of the program.  Assigning a value into the constant
100   /// leads to undefined behavior.
101   ///
102   bool isConstant() const { return isConstantGlobal; }
103   void setConstant(bool Value) { isConstantGlobal = Value; }
104
105   /// If the value is "Thread Local", its value isn't shared by the threads.
106   bool isThreadLocal() const { return isThreadLocalSymbol; }
107   void setThreadLocal(bool Value) { isThreadLocalSymbol = Value; }
108
109   /// removeFromParent - This method unlinks 'this' from the containing module,
110   /// but does not delete it.
111   ///
112   void removeFromParent();
113
114   /// eraseFromParent - This method unlinks 'this' from the containing module
115   /// and deletes it.
116   ///
117   void eraseFromParent();
118
119   /// Override Constant's implementation of this method so we can
120   /// replace constant initializers.
121   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
122
123   virtual void print(std::ostream &OS) const;
124   void print(std::ostream *OS) const { if (OS) print(*OS); }
125
126   // Methods for support type inquiry through isa, cast, and dyn_cast:
127   static inline bool classof(const GlobalVariable *) { return true; }
128   static inline bool classof(const Value *V) {
129     return V->getValueID() == Value::GlobalVariableVal;
130   }
131 private:
132   // getNext/Prev - Return the next or previous global variable in the list.
133         GlobalVariable *getNext()       { return Next; }
134   const GlobalVariable *getNext() const { return Next; }
135         GlobalVariable *getPrev()       { return Prev; }
136   const GlobalVariable *getPrev() const { return Prev; }
137 };
138
139 } // End llvm namespace
140
141 #endif