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=(const GlobalVariable &); // Do not implement
36 GlobalVariable(const GlobalVariable &); // Do not implement
38 void setParent(Module *parent);
40 GlobalVariable *Prev, *Next;
41 void setNext(GlobalVariable *N) { Next = N; }
42 void setPrev(GlobalVariable *N) { Prev = N; }
44 bool isConstantGlobal : 1; // Is this a global constant?
45 bool isThreadLocalSymbol : 1; // Is this symbol "Thread Local"?
49 /// GlobalVariable ctor - If a parent module is specified, the global is
50 /// automatically inserted into the end of the specified modules global list.
51 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
52 Constant *Initializer = 0, const std::string &Name = "",
53 Module *Parent = 0, bool ThreadLocal = false,
54 unsigned AddressSpace = 0);
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, bool ThreadLocal = false,
60 unsigned AddressSpace = 0);
62 /// isDeclaration - Is this global variable lacking an initializer? If so,
63 /// the global variable is defined in some other translation unit, and is thus
64 /// only a declaration here.
65 virtual bool isDeclaration() const { return getNumOperands() == 0; }
67 /// hasInitializer - Unless a global variable isExternal(), it has an
68 /// initializer. The initializer for the global variable/constant is held by
69 /// Initializer if an initializer is specified.
71 inline bool hasInitializer() const { return !isDeclaration(); }
73 /// getInitializer - Return the initializer for this global variable. It is
74 /// illegal to call this method if the global is external, because we cannot
75 /// tell what the value is initialized to!
77 inline Constant *getInitializer() const {
78 assert(hasInitializer() && "GV doesn't have initializer!");
79 return reinterpret_cast<Constant*>(Initializer.get());
81 inline Constant *getInitializer() {
82 assert(hasInitializer() && "GV doesn't have initializer!");
83 return reinterpret_cast<Constant*>(Initializer.get());
85 inline void setInitializer(Constant *CPV) {
87 if (hasInitializer()) {
92 if (!hasInitializer())
98 /// If the value is a global constant, its value is immutable throughout the
99 /// runtime execution of the program. Assigning a value into the constant
100 /// leads to undefined behavior.
102 bool isConstant() const { return isConstantGlobal; }
103 void setConstant(bool Value) { isConstantGlobal = Value; }
105 /// If the value is "Thread Local", its value isn't shared by the threads.
106 bool isThreadLocal() const { return isThreadLocalSymbol; }
107 void setThreadLocal(bool Value) { isThreadLocalSymbol = Value; }
109 /// removeFromParent - This method unlinks 'this' from the containing module,
110 /// but does not delete it.
112 void removeFromParent();
114 /// eraseFromParent - This method unlinks 'this' from the containing module
117 void eraseFromParent();
119 /// Override Constant's implementation of this method so we can
120 /// replace constant initializers.
121 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
123 virtual void print(std::ostream &OS) const;
124 void print(std::ostream *OS) const { if (OS) print(*OS); }
126 // Methods for support type inquiry through isa, cast, and dyn_cast:
127 static inline bool classof(const GlobalVariable *) { return true; }
128 static inline bool classof(const Value *V) {
129 return V->getValueID() == Value::GlobalVariableVal;
132 // getNext/Prev - Return the next or previous global variable in the list.
133 GlobalVariable *getNext() { return Next; }
134 const GlobalVariable *getNext() const { return Next; }
135 GlobalVariable *getPrev() { return Prev; }
136 const GlobalVariable *getPrev() const { return Prev; }
139 } // End llvm namespace