Added LLVM copyright header (for lack of a better term).
[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 class Module;
26 class Constant;
27 class PointerType;
28 template<typename SC> struct ilist_traits;
29 template<typename ValueSubClass, typename ItemParentClass, typename SymTabClass,
30          typename SubClass> class SymbolTableListTraits;
31
32 class GlobalVariable : public GlobalValue {
33   friend class SymbolTableListTraits<GlobalVariable, Module, Module,
34                                      ilist_traits<GlobalVariable> >;
35   void setParent(Module *parent);
36
37   GlobalVariable *Prev, *Next;
38   void setNext(GlobalVariable *N) { Next = N; }
39   void setPrev(GlobalVariable *N) { Prev = N; }
40
41   bool isConstantGlobal;               // Is this a global constant?
42 public:
43   /// GlobalVariable ctor - If a parent module is specified, the global is
44   /// automatically inserted into the end of the specified modules global list.
45   ///
46   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
47                  Constant *Initializer = 0, const std::string &Name = "",
48                  Module *Parent = 0);
49
50   // Specialize setName to handle symbol table majik...
51   virtual void setName(const std::string &name, SymbolTable *ST = 0);
52
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.
56   ///
57   virtual bool isExternal() const { return Operands.empty(); }
58
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.
62   ///
63   inline bool hasInitializer() const { return !isExternal(); }
64
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!
68   ///
69   inline Constant *getInitializer() const {
70     assert(hasInitializer() && "GV doesn't have initializer!");
71     return (Constant*)Operands[0].get();
72   }
73   inline Constant *getInitializer() {
74     assert(hasInitializer() && "GV doesn't have initializer!");
75     return (Constant*)Operands[0].get();
76   }
77   inline void setInitializer(Constant *CPV) {
78     if (CPV == 0) {
79       if (hasInitializer()) Operands.pop_back();
80     } else {
81       if (!hasInitializer()) Operands.push_back(Use(0, this));
82       Operands[0] = (Value*)CPV;
83     }
84   }
85
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; }
91
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.
95   ///
96   bool isConstant() const { return isConstantGlobal; }
97   void setConstant(bool Value) { isConstantGlobal = Value; }
98   
99   virtual void print(std::ostream &OS) const;
100
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;
105   }
106 };
107
108 #endif