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.
53 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
54 Constant *Initializer = 0, const std::string &Name = "",
57 /// isExternal - Is this global variable lacking an initializer? If so, the
58 /// global variable is defined in some other translation unit, and is thus
59 /// externally defined here.
61 virtual bool isExternal() const { return getNumOperands() == 0; }
63 /// hasInitializer - Unless a global variable isExternal(), it has an
64 /// initializer. The initializer for the global variable/constant is held by
65 /// Initializer if an initializer is specified.
67 inline bool hasInitializer() const { return !isExternal(); }
69 /// getInitializer - Return the initializer for this global variable. It is
70 /// illegal to call this method if the global is external, because we cannot
71 /// tell what the value is initialized to!
73 inline Constant *getInitializer() const {
74 assert(hasInitializer() && "GV doesn't have initializer!");
75 return reinterpret_cast<Constant*>(Initializer.get());
77 inline Constant *getInitializer() {
78 assert(hasInitializer() && "GV doesn't have initializer!");
79 return reinterpret_cast<Constant*>(Initializer.get());
81 inline void setInitializer(Constant *CPV) {
83 if (hasInitializer()) {
88 if (!hasInitializer())
94 // getNext/Prev - Return the next or previous global variable in the list.
95 GlobalVariable *getNext() { return Next; }
96 const GlobalVariable *getNext() const { return Next; }
97 GlobalVariable *getPrev() { return Prev; }
98 const GlobalVariable *getPrev() const { return Prev; }
100 /// If the value is a global constant, its value is immutable throughout the
101 /// runtime execution of the program. Assigning a value into the constant
102 /// leads to undefined behavior.
104 bool isConstant() const { return isConstantGlobal; }
105 void setConstant(bool Value) { isConstantGlobal = Value; }
107 /// removeFromParent - This method unlinks 'this' from the containing module,
108 /// but does not delete it.
110 void removeFromParent();
112 /// eraseFromParent - This method unlinks 'this' from the containing module
115 void eraseFromParent();
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);
121 virtual void print(std::ostream &OS) const;
123 // Methods for support type inquiry through isa, cast, and dyn_cast:
124 static inline bool classof(const GlobalVariable *) { return true; }
125 static inline bool classof(const Value *V) {
126 return V->getValueType() == Value::GlobalVariableVal;
130 } // End llvm namespace