1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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 ValueSubClass, typename ItemParentClass>
31 class SymbolTableListTraits;
33 class GlobalVariable : public GlobalValue {
34 friend class SymbolTableListTraits<GlobalVariable, Module>;
35 void *operator new(size_t, unsigned); // Do not implement
36 void operator=(const GlobalVariable &); // Do not implement
37 GlobalVariable(const GlobalVariable &); // Do not implement
39 void setParent(Module *parent);
41 GlobalVariable *Prev, *Next;
42 void setNext(GlobalVariable *N) { Next = N; }
43 void setPrev(GlobalVariable *N) { Prev = N; }
45 bool isConstantGlobal : 1; // Is this a global constant?
46 bool isThreadLocalSymbol : 1; // Is this symbol "Thread Local"?
50 // allocate space for exactly zero operands
51 void *operator new(size_t s) {
52 return User::operator new(s, 0);
54 /// GlobalVariable ctor - If a parent module is specified, the global is
55 /// automatically inserted into the end of the specified modules global list.
56 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
57 Constant *Initializer = 0, const std::string &Name = "",
58 Module *Parent = 0, bool ThreadLocal = false,
59 unsigned AddressSpace = 0);
60 /// GlobalVariable ctor - This creates a global and inserts it before the
61 /// specified other global.
62 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
63 Constant *Initializer, const std::string &Name,
64 GlobalVariable *InsertBefore, bool ThreadLocal = false,
65 unsigned AddressSpace = 0);
67 /// isDeclaration - Is this global variable lacking an initializer? If so,
68 /// the global variable is defined in some other translation unit, and is thus
69 /// only a declaration here.
70 virtual bool isDeclaration() const { return getNumOperands() == 0; }
72 /// hasInitializer - Unless a global variable isExternal(), it has an
73 /// initializer. The initializer for the global variable/constant is held by
74 /// Initializer if an initializer is specified.
76 inline bool hasInitializer() const { return !isDeclaration(); }
78 /// getInitializer - Return the initializer for this global variable. It is
79 /// illegal to call this method if the global is external, because we cannot
80 /// tell what the value is initialized to!
82 inline Constant *getInitializer() const {
83 assert(hasInitializer() && "GV doesn't have initializer!");
84 return reinterpret_cast<Constant*>(Initializer.get());
86 inline Constant *getInitializer() {
87 assert(hasInitializer() && "GV doesn't have initializer!");
88 return reinterpret_cast<Constant*>(Initializer.get());
90 inline void setInitializer(Constant *CPV) {
92 if (hasInitializer()) {
97 if (!hasInitializer())
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 /// If the value is "Thread Local", its value isn't shared by the threads.
111 bool isThreadLocal() const { return isThreadLocalSymbol; }
112 void setThreadLocal(bool Value) { isThreadLocalSymbol = Value; }
114 /// removeFromParent - This method unlinks 'this' from the containing module,
115 /// but does not delete it.
117 void removeFromParent();
119 /// eraseFromParent - This method unlinks 'this' from the containing module
122 void eraseFromParent();
124 /// Override Constant's implementation of this method so we can
125 /// replace constant initializers.
126 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
128 virtual void print(std::ostream &OS) const;
129 void print(std::ostream *OS) const { if (OS) print(*OS); }
131 // Methods for support type inquiry through isa, cast, and dyn_cast:
132 static inline bool classof(const GlobalVariable *) { return true; }
133 static inline bool classof(const Value *V) {
134 return V->getValueID() == Value::GlobalVariableVal;
137 // getNext/Prev - Return the next or previous global variable in the list.
138 GlobalVariable *getNext() { return Next; }
139 const GlobalVariable *getNext() const { return Next; }
140 GlobalVariable *getPrev() { return Prev; }
141 const GlobalVariable *getPrev() const { return Prev; }
144 } // End llvm namespace