Add the Instruction::Select enum
[oota-llvm.git] / include / llvm / GlobalVariable.h
1 //===-- llvm/Global.h - Class to represent a global variable ----*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declaration of the GlobalVariable class, which
11 // represents a single global variable (or constant) in the VM.
12 //
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.
17 //
18 //===----------------------------------------------------------------------===//
19
20 #ifndef LLVM_GLOBAL_VARIABLE_H
21 #define LLVM_GLOBAL_VARIABLE_H
22
23 #include "llvm/GlobalValue.h"
24
25 namespace llvm {
26
27 class Module;
28 class Constant;
29 class PointerType;
30 template<typename SC> struct ilist_traits;
31 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
32          typename SubClass> class SymbolTableListTraits;
33
34 class GlobalVariable : public GlobalValue {
35   friend class SymbolTableListTraits<GlobalVariable, Module, Module,
36                                      ilist_traits<GlobalVariable> >;
37   void setParent(Module *parent);
38
39   GlobalVariable *Prev, *Next;
40   void setNext(GlobalVariable *N) { Next = N; }
41   void setPrev(GlobalVariable *N) { Prev = N; }
42
43   bool isConstantGlobal;               // Is this a global constant?
44 public:
45   /// GlobalVariable ctor - If a parent module is specified, the global is
46   /// automatically inserted into the end of the specified modules global list.
47   ///
48   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
49                  Constant *Initializer = 0, const std::string &Name = "",
50                  Module *Parent = 0);
51
52   // Specialize setName to handle symbol table majik...
53   virtual void setName(const std::string &name, SymbolTable *ST = 0);
54
55   /// isExternal - Is this global variable lacking an initializer?  If so, the
56   /// global variable is defined in some other translation unit, and is thus
57   /// externally defined here.
58   ///
59   virtual bool isExternal() const { return Operands.empty(); }
60
61   /// hasInitializer - Unless a global variable isExternal(), it has an
62   /// initializer.  The initializer for the global variable/constant is held by
63   /// Operands[0] if an initializer is specified.
64   ///
65   inline bool hasInitializer() const { return !isExternal(); }
66
67   /// getInitializer - Return the initializer for this global variable.  It is
68   /// illegal to call this method if the global is external, because we cannot
69   /// tell what the value is initialized to!
70   ///
71   inline Constant *getInitializer() const {
72     assert(hasInitializer() && "GV doesn't have initializer!");
73     return reinterpret_cast<Constant*>(Operands[0].get());
74   }
75   inline Constant *getInitializer() {
76     assert(hasInitializer() && "GV doesn't have initializer!");
77     return reinterpret_cast<Constant*>(Operands[0].get());
78   }
79   inline void setInitializer(Constant *CPV) {
80     if (CPV == 0) {
81       if (hasInitializer()) Operands.pop_back();
82     } else {
83       if (!hasInitializer()) Operands.push_back(Use(0, this));
84       Operands[0] = reinterpret_cast<Value*>(CPV);
85     }
86   }
87
88   // getNext/Prev - Return the next or previous global variable in the list.
89         GlobalVariable *getNext()       { return Next; }
90   const GlobalVariable *getNext() const { return Next; }
91         GlobalVariable *getPrev()       { return Prev; }
92   const GlobalVariable *getPrev() const { return Prev; }
93
94   /// If the value is a global constant, its value is immutable throughout the
95   /// runtime execution of the program.  Assigning a value into the constant
96   /// leads to undefined behavior.
97   ///
98   bool isConstant() const { return isConstantGlobal; }
99   void setConstant(bool Value) { isConstantGlobal = Value; }
100   
101   virtual void print(std::ostream &OS) const;
102
103   // Methods for support type inquiry through isa, cast, and dyn_cast:
104   static inline bool classof(const GlobalVariable *) { return true; }
105   static inline bool classof(const Value *V) {
106     return V->getValueType() == Value::GlobalVariableVal;
107   }
108 };
109
110 } // End llvm namespace
111
112 #endif