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?
47 bool isThreadLocalSymbol; // Is this symbol "Thread Local"?
51 /// GlobalVariable ctor - If a parent module is specified, the global is
52 /// 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 = "",
55 Module *Parent = 0, bool ThreadLocal = false);
56 /// GlobalVariable ctor - This creates a global and inserts it before the
57 /// specified other global.
58 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
59 Constant *Initializer, const std::string &Name,
60 GlobalVariable *InsertBefore, bool ThreadLocal = false);
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 // getNext/Prev - Return the next or previous global variable in the list.
99 GlobalVariable *getNext() { return Next; }
100 const GlobalVariable *getNext() const { return Next; }
101 GlobalVariable *getPrev() { return Prev; }
102 const GlobalVariable *getPrev() const { return Prev; }
104 /// If the value is a global constant, its value is immutable throughout the
105 /// runtime execution of the program. Assigning a value into the constant
106 /// leads to undefined behavior.
108 bool isConstant() const { return isConstantGlobal; }
109 void setConstant(bool Value) { isConstantGlobal = Value; }
111 /// If the value is "Thread Local", its value isn't shared by the threads.
112 bool isThreadLocal() const { return isThreadLocalSymbol; }
113 void setThreadLocal(bool Value) { isThreadLocalSymbol = Value; }
115 /// removeFromParent - This method unlinks 'this' from the containing module,
116 /// but does not delete it.
118 void removeFromParent();
120 /// eraseFromParent - This method unlinks 'this' from the containing module
123 void eraseFromParent();
125 /// Override Constant's implementation of this method so we can
126 /// replace constant initializers.
127 virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
129 virtual void print(std::ostream &OS) const;
130 void print(std::ostream *OS) const { if (OS) print(*OS); }
132 // Methods for support type inquiry through isa, cast, and dyn_cast:
133 static inline bool classof(const GlobalVariable *) { return true; }
134 static inline bool classof(const Value *V) {
135 return V->getValueID() == Value::GlobalVariableVal;
139 } // End llvm namespace