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 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 /// GlobalVariable ctor - This creates a global and inserts it before the
55 /// specified other global.
56 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
57 Constant *Initializer, const std::string &Name,
58 GlobalVariable *InsertBefore, bool ThreadLocal = false);
60 /// isDeclaration - Is this global variable lacking an initializer? If so,
61 /// the global variable is defined in some other translation unit, and is thus
62 /// only a declaration here.
63 virtual bool isDeclaration() const { return getNumOperands() == 0; }
65 /// hasInitializer - Unless a global variable isExternal(), it has an
66 /// initializer. The initializer for the global variable/constant is held by
67 /// Initializer if an initializer is specified.
69 inline bool hasInitializer() const { return !isDeclaration(); }
71 /// getInitializer - Return the initializer for this global variable. It is
72 /// illegal to call this method if the global is external, because we cannot
73 /// tell what the value is initialized to!
75 inline Constant *getInitializer() const {
76 assert(hasInitializer() && "GV doesn't have initializer!");
77 return reinterpret_cast<Constant*>(Initializer.get());
79 inline Constant *getInitializer() {
80 assert(hasInitializer() && "GV doesn't have initializer!");
81 return reinterpret_cast<Constant*>(Initializer.get());
83 inline void setInitializer(Constant *CPV) {
85 if (hasInitializer()) {
90 if (!hasInitializer())
96 /// If the value is a global constant, its value is immutable throughout the
97 /// runtime execution of the program. Assigning a value into the constant
98 /// leads to undefined behavior.
100 bool isConstant() const { return isConstantGlobal; }
101 void setConstant(bool Value) { isConstantGlobal = Value; }
103 /// If the value is "Thread Local", its value isn't shared by the threads.
104 bool isThreadLocal() const { return isThreadLocalSymbol; }
105 void setThreadLocal(bool Value) { isThreadLocalSymbol = 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;
122 void print(std::ostream *OS) const { if (OS) print(*OS); }
124 // Methods for support type inquiry through isa, cast, and dyn_cast:
125 static inline bool classof(const GlobalVariable *) { return true; }
126 static inline bool classof(const Value *V) {
127 return V->getValueID() == Value::GlobalVariableVal;
130 // getNext/Prev - Return the next or previous global variable in the list.
131 GlobalVariable *getNext() { return Next; }
132 const GlobalVariable *getNext() const { return Next; }
133 GlobalVariable *getPrev() { return Prev; }
134 const GlobalVariable *getPrev() const { return Prev; }
137 } // End llvm namespace