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