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 operator=(const GlobalVariable &); // Do not implement
38 GlobalVariable(const GlobalVariable &); // Do not implement
40 void setParent(Module *parent);
42 GlobalVariable *Prev, *Next;
43 void setNext(GlobalVariable *N) { Next = N; }
44 void setPrev(GlobalVariable *N) { Prev = N; }
46 bool isConstantGlobal; // Is this a global constant?
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 = "",
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);
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; }
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.
70 inline bool hasInitializer() const { return !isDeclaration(); }
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!
76 inline Constant *getInitializer() const {
77 assert(hasInitializer() && "GV doesn't have initializer!");
78 return reinterpret_cast<Constant*>(Initializer.get());
80 inline Constant *getInitializer() {
81 assert(hasInitializer() && "GV doesn't have initializer!");
82 return reinterpret_cast<Constant*>(Initializer.get());
84 inline void setInitializer(Constant *CPV) {
86 if (hasInitializer()) {
91 if (!hasInitializer())
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; }
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.
107 bool isConstant() const { return isConstantGlobal; }
108 void setConstant(bool Value) { isConstantGlobal = Value; }
110 /// removeFromParent - This method unlinks 'this' from the containing module,
111 /// but does not delete it.
113 void removeFromParent();
115 /// eraseFromParent - This method unlinks 'this' from the containing module
118 void eraseFromParent();
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);
124 virtual void print(std::ostream &OS) const;
125 void print(std::ostream *OS) const { if (OS) print(*OS); }
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;
134 } // End llvm namespace