For PR1136: Rename GlobalVariable::isExternal as isDeclaration to avoid
[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 SC> struct ilist_traits;
31 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
32          typename SubClass> class SymbolTableListTraits;
33
34 class GlobalVariable : public GlobalValue {
35   friend class SymbolTableListTraits<GlobalVariable, Module, Module,
36                                      ilist_traits<GlobalVariable> >;
37   void operator=(const GlobalVariable &);     // Do not implement
38   GlobalVariable(const GlobalVariable &);     // Do not implement
39
40   void setParent(Module *parent);
41
42   GlobalVariable *Prev, *Next;
43   void setNext(GlobalVariable *N) { Next = N; }
44   void setPrev(GlobalVariable *N) { Prev = N; }
45
46   bool isConstantGlobal;               // Is this a global constant?
47   Use Initializer;
48
49 public:
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 std::string &Name = "",
54                  Module *Parent = 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);
60   
61   /// isDeclaration - Is this global variable lacking an initializer?  If so, 
62   /// the global variable is defined in some other translation unit, and is thus
63   /// only a declaration here.
64   virtual bool isDeclaration() const { return getNumOperands() == 0; }
65
66   /// hasInitializer - Unless a global variable isExternal(), it has an
67   /// initializer.  The initializer for the global variable/constant is held by
68   /// Initializer if an initializer is specified.
69   ///
70   inline bool hasInitializer() const { return !isDeclaration(); }
71
72   /// getInitializer - Return the initializer for this global variable.  It is
73   /// illegal to call this method if the global is external, because we cannot
74   /// tell what the value is initialized to!
75   ///
76   inline Constant *getInitializer() const {
77     assert(hasInitializer() && "GV doesn't have initializer!");
78     return reinterpret_cast<Constant*>(Initializer.get());
79   }
80   inline Constant *getInitializer() {
81     assert(hasInitializer() && "GV doesn't have initializer!");
82     return reinterpret_cast<Constant*>(Initializer.get());
83   }
84   inline void setInitializer(Constant *CPV) {
85     if (CPV == 0) {
86       if (hasInitializer()) {
87         Initializer.set(0);
88         NumOperands = 0;
89       }
90     } else {
91       if (!hasInitializer())
92         NumOperands = 1;
93       Initializer.set(CPV);
94     }
95   }
96
97   // getNext/Prev - Return the next or previous global variable in the list.
98         GlobalVariable *getNext()       { return Next; }
99   const GlobalVariable *getNext() const { return Next; }
100         GlobalVariable *getPrev()       { return Prev; }
101   const GlobalVariable *getPrev() const { return Prev; }
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   /// removeFromParent - This method unlinks 'this' from the containing module,
111   /// but does not delete it.
112   ///
113   void removeFromParent();
114
115   /// eraseFromParent - This method unlinks 'this' from the containing module
116   /// and deletes it.
117   ///
118   void eraseFromParent();
119
120   /// Override Constant's implementation of this method so we can
121   /// replace constant initializers.
122   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
123
124   virtual void print(std::ostream &OS) const;
125   void print(std::ostream *OS) const { if (OS) print(*OS); }
126
127   // Methods for support type inquiry through isa, cast, and dyn_cast:
128   static inline bool classof(const GlobalVariable *) { return true; }
129   static inline bool classof(const Value *V) {
130     return V->getValueType() == Value::GlobalVariableVal;
131   }
132 };
133
134 } // End llvm namespace
135
136 #endif