1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the GlobalVariable class, which
11 // represents a single global variable (or constant) in the VM.
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.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_GLOBAL_VARIABLE_H
21 #define LLVM_GLOBAL_VARIABLE_H
23 #include "llvm/GlobalValue.h"
30 template<typename SC> struct ilist_traits;
31 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
32 typename SubClass> class SymbolTableListTraits;
34 class GlobalVariable : public GlobalValue {
35 friend class SymbolTableListTraits<GlobalVariable, Module, Module,
36 ilist_traits<GlobalVariable> >;
37 void setParent(Module *parent);
39 GlobalVariable *Prev, *Next;
40 void setNext(GlobalVariable *N) { Next = N; }
41 void setPrev(GlobalVariable *N) { Prev = N; }
43 bool isConstantGlobal; // Is this a global constant?
47 /// GlobalVariable ctor - If a parent module is specified, the global is
48 /// automatically inserted into the end of the specified modules global list.
50 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
51 Constant *Initializer = 0, const std::string &Name = "",
54 /// isExternal - Is this global variable lacking an initializer? If so, the
55 /// global variable is defined in some other translation unit, and is thus
56 /// externally defined here.
58 virtual bool isExternal() const { return getNumOperands() == 0; }
60 /// hasInitializer - Unless a global variable isExternal(), it has an
61 /// initializer. The initializer for the global variable/constant is held by
62 /// Initializer if an initializer is specified.
64 inline bool hasInitializer() const { return !isExternal(); }
66 /// getInitializer - Return the initializer for this global variable. It is
67 /// illegal to call this method if the global is external, because we cannot
68 /// tell what the value is initialized to!
70 inline Constant *getInitializer() const {
71 assert(hasInitializer() && "GV doesn't have initializer!");
72 return reinterpret_cast<Constant*>(Initializer.get());
74 inline Constant *getInitializer() {
75 assert(hasInitializer() && "GV doesn't have initializer!");
76 return reinterpret_cast<Constant*>(Initializer.get());
78 inline void setInitializer(Constant *CPV) {
80 if (hasInitializer()) {
85 if (!hasInitializer())
91 // getNext/Prev - Return the next or previous global variable in the list.
92 GlobalVariable *getNext() { return Next; }
93 const GlobalVariable *getNext() const { return Next; }
94 GlobalVariable *getPrev() { return Prev; }
95 const GlobalVariable *getPrev() const { return Prev; }
97 /// If the value is a global constant, its value is immutable throughout the
98 /// runtime execution of the program. Assigning a value into the constant
99 /// leads to undefined behavior.
101 bool isConstant() const { return isConstantGlobal; }
102 void setConstant(bool Value) { isConstantGlobal = Value; }
104 /// removeFromParent - This method unlinks 'this' from the containing module,
105 /// but does not delete it.
107 void removeFromParent();
109 /// eraseFromParent - This method unlinks 'this' from the containing module
112 void eraseFromParent();
114 /// Override Constant's implementation of this method so we can
115 /// replace constant initializers.
116 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
117 bool DisableChecking = false);
119 virtual void print(std::ostream &OS) const;
121 // Methods for support type inquiry through isa, cast, and dyn_cast:
122 static inline bool classof(const GlobalVariable *) { return true; }
123 static inline bool classof(const Value *V) {
124 return V->getValueType() == Value::GlobalVariableVal;
128 } // End llvm namespace