1 : //
2 : // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
3 : // Use of this source code is governed by a BSD-style license that can be
4 : // found in the LICENSE file.
5 : //
6 :
7 : #ifndef _CONSTANT_UNION_INCLUDED_
8 : #define _CONSTANT_UNION_INCLUDED_
9 :
10 : #include <assert.h>
11 :
12 : class ConstantUnion {
13 : public:
14 :
15 0 : POOL_ALLOCATOR_NEW_DELETE(GlobalPoolAllocator)
16 0 : void setIConst(int i) {iConst = i; type = EbtInt; }
17 0 : void setFConst(float f) {fConst = f; type = EbtFloat; }
18 0 : void setBConst(bool b) {bConst = b; type = EbtBool; }
19 :
20 0 : int getIConst() { return iConst; }
21 0 : float getFConst() { return fConst; }
22 0 : bool getBConst() { return bConst; }
23 0 : int getIConst() const { return iConst; }
24 0 : float getFConst() const { return fConst; }
25 0 : bool getBConst() const { return bConst; }
26 :
27 0 : bool operator==(const int i) const
28 : {
29 0 : return i == iConst;
30 : }
31 :
32 0 : bool operator==(const float f) const
33 : {
34 0 : return f == fConst;
35 : }
36 :
37 : bool operator==(const bool b) const
38 : {
39 : return b == bConst;
40 : }
41 :
42 0 : bool operator==(const ConstantUnion& constant) const
43 : {
44 0 : if (constant.type != type)
45 0 : return false;
46 :
47 0 : switch (type) {
48 : case EbtInt:
49 0 : return constant.iConst == iConst;
50 : case EbtFloat:
51 0 : return constant.fConst == fConst;
52 : case EbtBool:
53 0 : return constant.bConst == bConst;
54 : default:
55 0 : return false;
56 : }
57 :
58 : return false;
59 : }
60 :
61 : bool operator!=(const int i) const
62 : {
63 : return !operator==(i);
64 : }
65 :
66 : bool operator!=(const float f) const
67 : {
68 : return !operator==(f);
69 : }
70 :
71 : bool operator!=(const bool b) const
72 : {
73 : return !operator==(b);
74 : }
75 :
76 0 : bool operator!=(const ConstantUnion& constant) const
77 : {
78 0 : return !operator==(constant);
79 : }
80 :
81 0 : bool operator>(const ConstantUnion& constant) const
82 : {
83 0 : assert(type == constant.type);
84 0 : switch (type) {
85 : case EbtInt:
86 0 : return iConst > constant.iConst;
87 : case EbtFloat:
88 0 : return fConst > constant.fConst;
89 : default:
90 0 : return false; // Invalid operation, handled at semantic analysis
91 : }
92 :
93 : return false;
94 : }
95 :
96 0 : bool operator<(const ConstantUnion& constant) const
97 : {
98 0 : assert(type == constant.type);
99 0 : switch (type) {
100 : case EbtInt:
101 0 : return iConst < constant.iConst;
102 : case EbtFloat:
103 0 : return fConst < constant.fConst;
104 : default:
105 0 : return false; // Invalid operation, handled at semantic analysis
106 : }
107 :
108 : return false;
109 : }
110 :
111 0 : ConstantUnion operator+(const ConstantUnion& constant) const
112 : {
113 : ConstantUnion returnValue;
114 0 : assert(type == constant.type);
115 0 : switch (type) {
116 0 : case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
117 0 : case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
118 0 : default: assert(false && "Default missing");
119 : }
120 :
121 : return returnValue;
122 : }
123 :
124 0 : ConstantUnion operator-(const ConstantUnion& constant) const
125 : {
126 : ConstantUnion returnValue;
127 0 : assert(type == constant.type);
128 0 : switch (type) {
129 0 : case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
130 0 : case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break;
131 0 : default: assert(false && "Default missing");
132 : }
133 :
134 : return returnValue;
135 : }
136 :
137 0 : ConstantUnion operator*(const ConstantUnion& constant) const
138 : {
139 : ConstantUnion returnValue;
140 0 : assert(type == constant.type);
141 0 : switch (type) {
142 0 : case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
143 0 : case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break;
144 0 : default: assert(false && "Default missing");
145 : }
146 :
147 : return returnValue;
148 : }
149 :
150 : ConstantUnion operator%(const ConstantUnion& constant) const
151 : {
152 : ConstantUnion returnValue;
153 : assert(type == constant.type);
154 : switch (type) {
155 : case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
156 : default: assert(false && "Default missing");
157 : }
158 :
159 : return returnValue;
160 : }
161 :
162 : ConstantUnion operator>>(const ConstantUnion& constant) const
163 : {
164 : ConstantUnion returnValue;
165 : assert(type == constant.type);
166 : switch (type) {
167 : case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
168 : default: assert(false && "Default missing");
169 : }
170 :
171 : return returnValue;
172 : }
173 :
174 : ConstantUnion operator<<(const ConstantUnion& constant) const
175 : {
176 : ConstantUnion returnValue;
177 : assert(type == constant.type);
178 : switch (type) {
179 : case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
180 : default: assert(false && "Default missing");
181 : }
182 :
183 : return returnValue;
184 : }
185 :
186 : ConstantUnion operator&(const ConstantUnion& constant) const
187 : {
188 : ConstantUnion returnValue;
189 : assert(type == constant.type);
190 : switch (type) {
191 : case EbtInt: returnValue.setIConst(iConst & constant.iConst); break;
192 : default: assert(false && "Default missing");
193 : }
194 :
195 : return returnValue;
196 : }
197 :
198 : ConstantUnion operator|(const ConstantUnion& constant) const
199 : {
200 : ConstantUnion returnValue;
201 : assert(type == constant.type);
202 : switch (type) {
203 : case EbtInt: returnValue.setIConst(iConst | constant.iConst); break;
204 : default: assert(false && "Default missing");
205 : }
206 :
207 : return returnValue;
208 : }
209 :
210 : ConstantUnion operator^(const ConstantUnion& constant) const
211 : {
212 : ConstantUnion returnValue;
213 : assert(type == constant.type);
214 : switch (type) {
215 : case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break;
216 : default: assert(false && "Default missing");
217 : }
218 :
219 : return returnValue;
220 : }
221 :
222 0 : ConstantUnion operator&&(const ConstantUnion& constant) const
223 : {
224 : ConstantUnion returnValue;
225 0 : assert(type == constant.type);
226 0 : switch (type) {
227 0 : case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
228 0 : default: assert(false && "Default missing");
229 : }
230 :
231 : return returnValue;
232 : }
233 :
234 0 : ConstantUnion operator||(const ConstantUnion& constant) const
235 : {
236 : ConstantUnion returnValue;
237 0 : assert(type == constant.type);
238 0 : switch (type) {
239 0 : case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
240 0 : default: assert(false && "Default missing");
241 : }
242 :
243 : return returnValue;
244 : }
245 :
246 0 : TBasicType getType() const { return type; }
247 : private:
248 :
249 : union {
250 : int iConst; // used for ivec, scalar ints
251 : bool bConst; // used for bvec, scalar bools
252 : float fConst; // used for vec, mat, scalar floats
253 : } ;
254 :
255 : TBasicType type;
256 : };
257 :
258 : #endif // _CONSTANT_UNION_INCLUDED_
|