1 //===-- llvm/Global.h - Class to represent a global variable ----*- 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"
28 template<typename SC> struct ilist_traits;
29 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
30 typename SubClass> class SymbolTableListTraits;
32 class GlobalVariable : public GlobalValue {
33 friend class SymbolTableListTraits<GlobalVariable, Module, Module,
34 ilist_traits<GlobalVariable> >;
35 void setParent(Module *parent);
37 GlobalVariable *Prev, *Next;
38 void setNext(GlobalVariable *N) { Next = N; }
39 void setPrev(GlobalVariable *N) { Prev = N; }
41 bool isConstantGlobal; // Is this a global constant?
43 /// GlobalVariable ctor - If a parent module is specified, the global is
44 /// automatically inserted into the end of the specified modules global list.
46 GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
47 Constant *Initializer = 0, const std::string &Name = "",
50 // Specialize setName to handle symbol table majik...
51 virtual void setName(const std::string &name, SymbolTable *ST = 0);
53 /// isExternal - Is this global variable lacking an initializer? If so, the
54 /// global variable is defined in some other translation unit, and is thus
55 /// externally defined here.
57 virtual bool isExternal() const { return Operands.empty(); }
59 /// hasInitializer - Unless a global variable isExternal(), it has an
60 /// initializer. The initializer for the global variable/constant is held by
61 /// Operands[0] if an initializer is specified.
63 inline bool hasInitializer() const { return !isExternal(); }
65 /// getInitializer - Return the initializer for this global variable. It is
66 /// illegal to call this method if the global is external, because we cannot
67 /// tell what the value is initialized to!
69 inline Constant *getInitializer() const {
70 assert(hasInitializer() && "GV doesn't have initializer!");
71 return (Constant*)Operands[0].get();
73 inline Constant *getInitializer() {
74 assert(hasInitializer() && "GV doesn't have initializer!");
75 return (Constant*)Operands[0].get();
77 inline void setInitializer(Constant *CPV) {
79 if (hasInitializer()) Operands.pop_back();
81 if (!hasInitializer()) Operands.push_back(Use(0, this));
82 Operands[0] = (Value*)CPV;
86 // getNext/Prev - Return the next or previous global variable in the list.
87 GlobalVariable *getNext() { return Next; }
88 const GlobalVariable *getNext() const { return Next; }
89 GlobalVariable *getPrev() { return Prev; }
90 const GlobalVariable *getPrev() const { return Prev; }
92 /// If the value is a global constant, its value is immutable throughout the
93 /// runtime execution of the program. Assigning a value into the constant
94 /// leads to undefined behavior.
96 bool isConstant() const { return isConstantGlobal; }
97 void setConstant(bool Value) { isConstantGlobal = Value; }
99 virtual void print(std::ostream &OS) const;
101 // Methods for support type inquiry through isa, cast, and dyn_cast:
102 static inline bool classof(const GlobalVariable *) { return true; }
103 static inline bool classof(const Value *V) {
104 return V->getValueType() == Value::GlobalVariableVal;