dbdda62f92dbb8bd45afc0e0abc75a4ac40c0826
[oota-llvm.git] / include / llvm / ConstPoolVals.h
1 //===-- llvm/ConstPoolVals.h - Constant Value nodes --------------*- C++ -*--=//
2 //
3 // This file contains the declarations for the ConstPoolVal class and all of
4 // its subclasses, which represent the different type of constant pool values
5 //
6 //===----------------------------------------------------------------------===//
7
8 #ifndef LLVM_CONSTPOOLVALS_H
9 #define LLVM_CONSTPOOLVALS_H
10
11 #include "llvm/User.h"
12 #include "llvm/SymTabValue.h"
13 #include "llvm/Tools/DataTypes.h"
14 #include <vector>
15
16 class ArrayType;
17 class StructType;
18
19 //===----------------------------------------------------------------------===//
20 //                            ConstPoolVal Class
21 //===----------------------------------------------------------------------===//
22
23 class ConstPoolVal;
24 typedef UseTy<ConstPoolVal> ConstPoolUse;
25
26 class ConstPoolVal : public User {
27   SymTabValue *Parent;
28
29   friend class ValueHolder<ConstPoolVal, SymTabValue>;
30   inline void setParent(SymTabValue *parent) { 
31     Parent = parent;
32   }
33
34 public:
35   inline ConstPoolVal(const Type *Ty, const string &Name = "") 
36     : User(Ty, Value::ConstantVal, Name) { Parent = 0; }
37
38   // Specialize setName to handle symbol table majik...
39   virtual void setName(const string &name);
40
41   // Static constructor to create a '0' constant of arbitrary type...
42   static ConstPoolVal *getNullConstant(const Type *Ty);
43
44   // clone() - Create a copy of 'this' value that is identical in all ways
45   // except the following:
46   //   * The value has no parent
47   //   * The value has no name
48   //
49   virtual ConstPoolVal *clone() const = 0;
50
51   virtual string getStrValue() const = 0;
52   virtual bool equals(const ConstPoolVal *V) const = 0;
53
54   inline const SymTabValue *getParent() const { return Parent; }
55   inline       SymTabValue *getParent()       { return Parent; }
56
57   // if i > the number of operands, then getOperand() returns 0, and setOperand
58   // returns false.  setOperand() may also return false if the operand is of
59   // the wrong type.
60   //
61   // Note that some subclasses may change this default no argument behavior
62   //
63   virtual Value *getOperand(unsigned i) { return 0; }
64   virtual const Value *getOperand(unsigned i) const { return 0; }
65   virtual bool setOperand(unsigned i, Value *Val) { return false; }
66   virtual void dropAllReferences() {}
67 };
68
69
70
71 //===----------------------------------------------------------------------===//
72 //              Classes to represent constant pool variable defs
73 //===----------------------------------------------------------------------===//
74
75 //===---------------------------------------------------------------------------
76 // ConstPoolBool - Boolean Values
77 //
78 class ConstPoolBool : public ConstPoolVal {
79   bool Val;
80   ConstPoolBool(const ConstPoolBool &CP);
81 public:
82   ConstPoolBool(bool V, const string &Name = "");
83
84   virtual string getStrValue() const;
85   virtual bool equals(const ConstPoolVal *V) const;
86
87   virtual ConstPoolVal *clone() const { return new ConstPoolBool(*this); }
88
89   inline bool getValue() const { return Val; }
90
91   // setValue - Be careful... if there is more than one 'use' of this node, then
92   // they will ALL see the value that you set...
93   //
94   inline void setValue(bool v) { Val = v; } 
95 };
96
97
98 //===---------------------------------------------------------------------------
99 // ConstPoolSInt - Signed Integer Values [sbyte, short, int, long]
100 //
101 class ConstPoolSInt : public ConstPoolVal {
102   int64_t Val;
103   ConstPoolSInt(const ConstPoolSInt &CP);
104 public:
105   ConstPoolSInt(const Type *Ty, int64_t V, const string &Name = "");
106
107   virtual ConstPoolVal *clone() const { return new ConstPoolSInt(*this); }
108
109   virtual string getStrValue() const;
110   virtual bool equals(const ConstPoolVal *V) const;
111
112   static bool isValueValidForType(const Type *Ty, int64_t V);
113   inline int64_t getValue() const { return Val; }
114 };
115
116
117 //===---------------------------------------------------------------------------
118 // ConstPoolUInt - Unsigned Integer Values [ubyte, ushort, uint, ulong]
119 //
120 class ConstPoolUInt : public ConstPoolVal {
121   uint64_t Val;
122   ConstPoolUInt(const ConstPoolUInt &CP);
123 public:
124   ConstPoolUInt(const Type *Ty, uint64_t V, const string &Name = "");
125
126   virtual ConstPoolVal *clone() const { return new ConstPoolUInt(*this); }
127
128   virtual string getStrValue() const;
129   virtual bool equals(const ConstPoolVal *V) const;
130
131   static bool isValueValidForType(const Type *Ty, uint64_t V);
132   inline uint64_t getValue() const { return Val; }
133 };
134
135
136 //===---------------------------------------------------------------------------
137 // ConstPoolFP - Floating Point Values [float, double]
138 //
139 class ConstPoolFP : public ConstPoolVal {
140   double Val;
141   ConstPoolFP(const ConstPoolFP &CP);
142 public:
143   ConstPoolFP(const Type *Ty, double V, const string &Name = "");
144
145   virtual ConstPoolVal *clone() const { return new ConstPoolFP(*this); }
146   virtual string getStrValue() const;
147   virtual bool equals(const ConstPoolVal *V) const;
148
149   static bool isValueValidForType(const Type *Ty, double V);
150   inline double getValue() const { return Val; }
151 };
152
153
154 //===---------------------------------------------------------------------------
155 // ConstPoolType - Type Declarations
156 //
157 class ConstPoolType : public ConstPoolVal {
158   const Type *Val;
159   ConstPoolType(const ConstPoolType &CPT);
160 public:
161   ConstPoolType(const Type *V, const string &Name = "");
162
163   virtual ConstPoolVal *clone() const { return new ConstPoolType(*this); }
164   virtual string getStrValue() const;
165   virtual bool equals(const ConstPoolVal *V) const;
166
167   inline const Type *getValue() const { return Val; }
168 };
169
170
171 //===---------------------------------------------------------------------------
172 // ConstPoolArray - Constant Array Declarations
173 //
174 class ConstPoolArray : public ConstPoolVal {
175   vector<ConstPoolUse> Val;
176   ConstPoolArray(const ConstPoolArray &CPT);
177 public:
178   ConstPoolArray(const ArrayType *T, vector<ConstPoolVal*> &V, 
179                  const string &Name = "");
180   inline ~ConstPoolArray() { dropAllReferences(); }
181
182   virtual ConstPoolVal *clone() const { return new ConstPoolArray(*this); }
183   virtual string getStrValue() const;
184   virtual bool equals(const ConstPoolVal *V) const;
185
186   inline const vector<ConstPoolUse> &getValues() const { return Val; }
187
188   // Implement User stuff...
189   //
190   virtual Value *getOperand(unsigned i) { 
191     return (i < Val.size()) ? Val[i] : 0; 
192   }
193   virtual const Value *getOperand(unsigned i) const {
194     return (i < Val.size()) ? Val[i] : 0; 
195   }
196
197   // setOperand fails! You can't change a constant!
198   virtual bool setOperand(unsigned i, Value *Val) { return false; }
199   virtual void dropAllReferences() { Val.clear(); }
200 };
201
202
203 //===---------------------------------------------------------------------------
204 // ConstPoolStruct - Constant Struct Declarations
205 //
206 class ConstPoolStruct : public ConstPoolVal {
207   vector<ConstPoolUse> Val;
208   ConstPoolStruct(const ConstPoolStruct &CPT);
209 public:
210   ConstPoolStruct(const StructType *T, vector<ConstPoolVal*> &V, 
211                   const string &Name = "");
212   inline ~ConstPoolStruct() { dropAllReferences(); }
213
214   virtual ConstPoolVal *clone() const { return new ConstPoolStruct(*this); }
215   virtual string getStrValue() const;
216   virtual bool equals(const ConstPoolVal *V) const;
217
218   inline const vector<ConstPoolUse> &getValues() const { return Val; }
219
220   // Implement User stuff...
221   //
222   virtual Value *getOperand(unsigned i) { 
223     return (i < Val.size()) ? Val[i] : 0; 
224   }
225   virtual const Value *getOperand(unsigned i) const {
226     return (i < Val.size()) ? Val[i] : 0; 
227   }
228
229   // setOperand fails! You can't change a constant!
230   virtual bool setOperand(unsigned i, Value *Val) { return false; }
231   virtual void dropAllReferences() { Val.clear(); }
232 };
233
234 #endif