Remove warnings about unused parameters and shadowed variables.
[oota-llvm.git] / include / llvm / GlobalVariable.h
1 //===-- llvm/GlobalVariable.h - GlobalVariable class ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 #include "llvm/OperandTraits.h"
25
26 namespace llvm {
27
28 class Module;
29 class Constant;
30 class PointerType;
31 template<typename ValueSubClass, typename ItemParentClass>
32   class SymbolTableListTraits;
33
34 class GlobalVariable : public GlobalValue {
35   friend class SymbolTableListTraits<GlobalVariable, Module>;
36   void *operator new(size_t, unsigned);       // Do not implement
37   void operator=(const GlobalVariable &);     // Do not implement
38   GlobalVariable(const GlobalVariable &);     // Do not implement
39
40   void setParent(Module *parent);
41
42   GlobalVariable *Prev, *Next;
43   void setNext(GlobalVariable *N) { Next = N; }
44   void setPrev(GlobalVariable *N) { Prev = N; }
45
46   bool isConstantGlobal : 1;           // Is this a global constant?
47   bool isThreadLocalSymbol : 1;        // Is this symbol "Thread Local"?
48
49 public:
50   // allocate space for exactly one operand
51   void *operator new(size_t s) {
52     return User::operator new(s, 1);
53   }
54   /// GlobalVariable ctor - If a parent module is specified, the global is
55   /// automatically inserted into the end of the specified modules global list.
56   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
57                  Constant *Initializer = 0, const std::string &Name = "",
58                  Module *Parent = 0, bool ThreadLocal = false,
59                  unsigned AddressSpace = 0);
60   /// GlobalVariable ctor - This creates a global and inserts it before the
61   /// specified other global.
62   GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage,
63                  Constant *Initializer, const std::string &Name,
64                  GlobalVariable *InsertBefore, bool ThreadLocal = false,
65                  unsigned AddressSpace = 0);
66
67   ~GlobalVariable() {
68     NumOperands = 1; // FIXME: needed by operator delete
69   }
70
71   /// Provide fast operand accessors
72   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
73
74   /// isDeclaration - Is this global variable lacking an initializer?  If so, 
75   /// the global variable is defined in some other translation unit, and is thus
76   /// only a declaration here.
77   virtual bool isDeclaration() const { return getNumOperands() == 0; }
78
79   /// hasInitializer - Unless a global variable isExternal(), it has an
80   /// initializer.  The initializer for the global variable/constant is held by
81   /// Initializer if an initializer is specified.
82   ///
83   inline bool hasInitializer() const { return !isDeclaration(); }
84
85   /// getInitializer - Return the initializer for this global variable.  It is
86   /// illegal to call this method if the global is external, because we cannot
87   /// tell what the value is initialized to!
88   ///
89   inline /*const FIXME*/ Constant *getInitializer() const {
90     assert(hasInitializer() && "GV doesn't have initializer!");
91     return static_cast<Constant*>(Op<0>().get());
92   }
93   inline Constant *getInitializer() {
94     assert(hasInitializer() && "GV doesn't have initializer!");
95     return static_cast<Constant*>(Op<0>().get());
96   }
97   inline void setInitializer(Constant *CPV) {
98     if (CPV == 0) {
99       if (hasInitializer()) {
100         Op<0>().set(0);
101         NumOperands = 0;
102       }
103     } else {
104       if (!hasInitializer())
105         NumOperands = 1;
106       Op<0>().set(CPV);
107     }
108   }
109
110   /// If the value is a global constant, its value is immutable throughout the
111   /// runtime execution of the program.  Assigning a value into the constant
112   /// leads to undefined behavior.
113   ///
114   bool isConstant() const { return isConstantGlobal; }
115   void setConstant(bool Val) { isConstantGlobal = Val; }
116
117   /// If the value is "Thread Local", its value isn't shared by the threads.
118   bool isThreadLocal() const { return isThreadLocalSymbol; }
119   void setThreadLocal(bool Val) { isThreadLocalSymbol = Val; }
120
121   /// removeFromParent - This method unlinks 'this' from the containing module,
122   /// but does not delete it.
123   ///
124   void removeFromParent();
125
126   /// eraseFromParent - This method unlinks 'this' from the containing module
127   /// and deletes it.
128   ///
129   void eraseFromParent();
130
131   /// Override Constant's implementation of this method so we can
132   /// replace constant initializers.
133   virtual void replaceUsesOfWithOnConstant(Value *From, Value *To, Use *U);
134
135   virtual void print(std::ostream &OS) const;
136   void print(std::ostream *OS) const { if (OS) print(*OS); }
137
138   // Methods for support type inquiry through isa, cast, and dyn_cast:
139   static inline bool classof(const GlobalVariable *) { return true; }
140   static inline bool classof(const Value *V) {
141     return V->getValueID() == Value::GlobalVariableVal;
142   }
143 private:
144   // getNext/Prev - Return the next or previous global variable in the list.
145         GlobalVariable *getNext()       { return Next; }
146   const GlobalVariable *getNext() const { return Next; }
147         GlobalVariable *getPrev()       { return Prev; }
148   const GlobalVariable *getPrev() const { return Prev; }
149 };
150
151 template <>
152 struct OperandTraits<GlobalVariable> : OptionalOperandTraits<> {
153 };
154
155 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GlobalVariable, Value)
156
157 } // End llvm namespace
158
159 #endif