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?
45 /// GlobalVariable ctor - If a parent module is specified, the global is
46 /// automatically inserted into the end of the specified modules global list.
48 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
49 Constant *Initializer = 0, const std::string &Name = "",
52 // Specialize setName to handle symbol table majik...
53 virtual void setName(const std::string &name, SymbolTable *ST = 0);
55 /// isExternal - Is this global variable lacking an initializer? If so, the
56 /// global variable is defined in some other translation unit, and is thus
57 /// externally defined here.
59 virtual bool isExternal() const { return Operands.empty(); }
61 /// hasInitializer - Unless a global variable isExternal(), it has an
62 /// initializer. The initializer for the global variable/constant is held by
63 /// Operands[0] if an initializer is specified.
65 inline bool hasInitializer() const { return !isExternal(); }
67 /// getInitializer - Return the initializer for this global variable. It is
68 /// illegal to call this method if the global is external, because we cannot
69 /// tell what the value is initialized to!
71 inline Constant *getInitializer() const {
72 assert(hasInitializer() && "GV doesn't have initializer!");
73 return reinterpret_cast<Constant*>(Operands[0].get());
75 inline Constant *getInitializer() {
76 assert(hasInitializer() && "GV doesn't have initializer!");
77 return reinterpret_cast<Constant*>(Operands[0].get());
79 inline void setInitializer(Constant *CPV) {
81 if (hasInitializer()) Operands.pop_back();
83 if (!hasInitializer()) Operands.push_back(Use(0, this));
84 Operands[0] = reinterpret_cast<Value*>(CPV);
88 // getNext/Prev - Return the next or previous global variable in the list.
89 GlobalVariable *getNext() { return Next; }
90 const GlobalVariable *getNext() const { return Next; }
91 GlobalVariable *getPrev() { return Prev; }
92 const GlobalVariable *getPrev() const { return Prev; }
94 /// If the value is a global constant, its value is immutable throughout the
95 /// runtime execution of the program. Assigning a value into the constant
96 /// leads to undefined behavior.
98 bool isConstant() const { return isConstantGlobal; }
99 void setConstant(bool Value) { isConstantGlobal = Value; }
101 /// Override Constant's implementation of this method so we can
102 /// replace constant initializers.
103 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To,
104 bool DisableChecking = false);
106 virtual void print(std::ostream &OS) const;
108 // Methods for support type inquiry through isa, cast, and dyn_cast:
109 static inline bool classof(const GlobalVariable *) { return true; }
110 static inline bool classof(const Value *V) {
111 return V->getValueType() == Value::GlobalVariableVal;
115 } // End llvm namespace