blob: 5e86c64805c41d9a6ac8bb064539ae7376540ab1 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens01df23e2014-06-11 10:56:03 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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
alokp@chromium.org4e4facd2010-06-02 15:21:22 +000010#include <assert.h>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +000012class ConstantUnion {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000013public:
Alok Priyadarshi8156b6b2013-09-23 14:56:58 -040014 POOL_ALLOCATOR_NEW_DELETE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +000015 ConstantUnion()
16 {
17 iConst = 0;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +000018 type = EbtVoid;
apatrick@chromium.orga1d80592012-01-25 21:52:10 +000019 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020
Nicolas Capens01df23e2014-06-11 10:56:03 -040021 bool cast(TBasicType newType, const ConstantUnion &constant)
22 {
23 switch (newType)
24 {
25 case EbtFloat:
26 switch (constant.type)
27 {
28 case EbtInt: setFConst(static_cast<float>(constant.getIConst())); break;
29 case EbtUInt: setFConst(static_cast<float>(constant.getUConst())); break;
30 case EbtBool: setFConst(static_cast<float>(constant.getBConst())); break;
31 case EbtFloat: setFConst(static_cast<float>(constant.getFConst())); break;
32 default: return false;
33 }
34 break;
35 case EbtInt:
36 switch (constant.type)
37 {
38 case EbtInt: setIConst(static_cast<int>(constant.getIConst())); break;
39 case EbtUInt: setIConst(static_cast<int>(constant.getUConst())); break;
40 case EbtBool: setIConst(static_cast<int>(constant.getBConst())); break;
41 case EbtFloat: setIConst(static_cast<int>(constant.getFConst())); break;
42 default: return false;
43 }
44 break;
45 case EbtUInt:
46 switch (constant.type)
47 {
48 case EbtInt: setUConst(static_cast<unsigned int>(constant.getIConst())); break;
49 case EbtUInt: setUConst(static_cast<unsigned int>(constant.getUConst())); break;
50 case EbtBool: setUConst(static_cast<unsigned int>(constant.getBConst())); break;
51 case EbtFloat: setUConst(static_cast<unsigned int>(constant.getFConst())); break;
52 default: return false;
53 }
54 break;
55 case EbtBool:
56 switch (constant.type)
57 {
58 case EbtInt: setBConst(constant.getIConst() != 0); break;
59 case EbtUInt: setBConst(constant.getUConst() != 0); break;
60 case EbtBool: setBConst(constant.getBConst()); break;
61 case EbtFloat: setBConst(constant.getFConst() != 0.0f); break;
62 default: return false;
63 }
64 break;
65 case EbtStruct: // Struct fields don't get cast
66 switch (constant.type)
67 {
68 case EbtInt: setIConst(constant.getIConst()); break;
69 case EbtUInt: setUConst(constant.getUConst()); break;
70 case EbtBool: setBConst(constant.getBConst()); break;
71 case EbtFloat: setFConst(constant.getFConst()); break;
72 default: return false;
73 }
74 break;
75 default:
76 return false;
77 }
78
79 return true;
80 }
81
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000082 void setIConst(int i) {iConst = i; type = EbtInt; }
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +000083 void setUConst(unsigned int u) { uConst = u; type = EbtUInt; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000084 void setFConst(float f) {fConst = f; type = EbtFloat; }
85 void setBConst(bool b) {bConst = b; type = EbtBool; }
86
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087 int getIConst() const { return iConst; }
Nicolas Capensc0f7c612013-06-05 11:46:09 -040088 unsigned int getUConst() const { return uConst; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000089 float getFConst() const { return fConst; }
90 bool getBConst() const { return bConst; }
91
92 bool operator==(const int i) const
93 {
daniel@transgaming.come90a0d52011-02-18 02:52:06 +000094 return i == iConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095 }
96
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +000097 bool operator==(const unsigned int u) const
98 {
99 return u == uConst;
100 }
101
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102 bool operator==(const float f) const
103 {
daniel@transgaming.come90a0d52011-02-18 02:52:06 +0000104 return f == fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105 }
106
107 bool operator==(const bool b) const
108 {
daniel@transgaming.come90a0d52011-02-18 02:52:06 +0000109 return b == bConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110 }
111
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000112 bool operator==(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113 {
114 if (constant.type != type)
115 return false;
116
117 switch (type) {
118 case EbtInt:
daniel@transgaming.come90a0d52011-02-18 02:52:06 +0000119 return constant.iConst == iConst;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000120 case EbtUInt:
121 return constant.uConst == uConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122 case EbtFloat:
daniel@transgaming.come90a0d52011-02-18 02:52:06 +0000123 return constant.fConst == fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124 case EbtBool:
daniel@transgaming.come90a0d52011-02-18 02:52:06 +0000125 return constant.bConst == bConst;
daniel@transgaming.comcd3a1b92011-03-15 18:23:46 +0000126 default:
127 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129 }
130
131 bool operator!=(const int i) const
132 {
133 return !operator==(i);
134 }
135
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000136 bool operator!=(const unsigned int u) const
137 {
138 return !operator==(u);
139 }
140
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141 bool operator!=(const float f) const
142 {
143 return !operator==(f);
144 }
145
146 bool operator!=(const bool b) const
147 {
148 return !operator==(b);
149 }
150
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000151 bool operator!=(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152 {
153 return !operator==(constant);
154 }
155
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000156 bool operator>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157 {
158 assert(type == constant.type);
159 switch (type) {
160 case EbtInt:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000161 return iConst > constant.iConst;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000162 case EbtUInt:
163 return uConst > constant.uConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000164 case EbtFloat:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000165 return fConst > constant.fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000166 default:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000167 return false; // Invalid operation, handled at semantic analysis
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000169 }
170
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000171 bool operator<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172 {
173 assert(type == constant.type);
174 switch (type) {
175 case EbtInt:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000176 return iConst < constant.iConst;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000177 case EbtUInt:
178 return uConst < constant.uConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000179 case EbtFloat:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000180 return fConst < constant.fConst;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181 default:
daniel@transgaming.comb31f35a2011-02-11 13:19:35 +0000182 return false; // Invalid operation, handled at semantic analysis
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184 }
185
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000186 ConstantUnion operator+(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000188 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189 assert(type == constant.type);
190 switch (type) {
191 case EbtInt: returnValue.setIConst(iConst + constant.iConst); break;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000192 case EbtUInt: returnValue.setUConst(uConst + constant.uConst); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193 case EbtFloat: returnValue.setFConst(fConst + constant.fConst); break;
194 default: assert(false && "Default missing");
195 }
196
197 return returnValue;
198 }
199
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000200 ConstantUnion operator-(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000202 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203 assert(type == constant.type);
204 switch (type) {
205 case EbtInt: returnValue.setIConst(iConst - constant.iConst); break;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000206 case EbtUInt: returnValue.setUConst(uConst - constant.uConst); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207 case EbtFloat: returnValue.setFConst(fConst - constant.fConst); break;
208 default: assert(false && "Default missing");
209 }
210
211 return returnValue;
212 }
213
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000214 ConstantUnion operator*(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000215 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000216 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217 assert(type == constant.type);
218 switch (type) {
219 case EbtInt: returnValue.setIConst(iConst * constant.iConst); break;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000220 case EbtUInt: returnValue.setUConst(uConst * constant.uConst); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221 case EbtFloat: returnValue.setFConst(fConst * constant.fConst); break;
222 default: assert(false && "Default missing");
223 }
224
225 return returnValue;
226 }
227
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000228 ConstantUnion operator%(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000230 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231 assert(type == constant.type);
232 switch (type) {
233 case EbtInt: returnValue.setIConst(iConst % constant.iConst); break;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000234 case EbtUInt: returnValue.setUConst(uConst % constant.uConst); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235 default: assert(false && "Default missing");
236 }
237
238 return returnValue;
239 }
240
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000241 ConstantUnion operator>>(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000242 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000243 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244 assert(type == constant.type);
245 switch (type) {
246 case EbtInt: returnValue.setIConst(iConst >> constant.iConst); break;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000247 case EbtUInt: returnValue.setUConst(uConst >> constant.uConst); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248 default: assert(false && "Default missing");
249 }
250
251 return returnValue;
252 }
253
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000254 ConstantUnion operator<<(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000256 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257 assert(type == constant.type);
258 switch (type) {
259 case EbtInt: returnValue.setIConst(iConst << constant.iConst); break;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000260 case EbtUInt: returnValue.setUConst(uConst << constant.uConst); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261 default: assert(false && "Default missing");
262 }
263
264 return returnValue;
265 }
266
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000267 ConstantUnion operator&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000269 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270 assert(type == constant.type);
271 switch (type) {
272 case EbtInt: returnValue.setIConst(iConst & constant.iConst); break;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000273 case EbtUInt: returnValue.setUConst(uConst & constant.uConst); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274 default: assert(false && "Default missing");
275 }
276
277 return returnValue;
278 }
279
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000280 ConstantUnion operator|(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000282 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283 assert(type == constant.type);
284 switch (type) {
285 case EbtInt: returnValue.setIConst(iConst | constant.iConst); break;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000286 case EbtUInt: returnValue.setUConst(uConst | constant.uConst); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287 default: assert(false && "Default missing");
288 }
289
290 return returnValue;
291 }
292
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000293 ConstantUnion operator^(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000295 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296 assert(type == constant.type);
297 switch (type) {
298 case EbtInt: returnValue.setIConst(iConst ^ constant.iConst); break;
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000299 case EbtUInt: returnValue.setUConst(uConst ^ constant.uConst); break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300 default: assert(false && "Default missing");
301 }
302
303 return returnValue;
304 }
305
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000306 ConstantUnion operator&&(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000307 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000308 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309 assert(type == constant.type);
310 switch (type) {
311 case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
312 default: assert(false && "Default missing");
313 }
314
315 return returnValue;
316 }
317
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000318 ConstantUnion operator||(const ConstantUnion& constant) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000319 {
alokp@chromium.org6ff56fd2010-05-05 16:37:50 +0000320 ConstantUnion returnValue;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000321 assert(type == constant.type);
322 switch (type) {
323 case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
324 default: assert(false && "Default missing");
325 }
326
327 return returnValue;
328 }
329
alokp@chromium.org76b82082010-03-24 17:59:39 +0000330 TBasicType getType() const { return type; }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331private:
332
333 union {
334 int iConst; // used for ivec, scalar ints
shannonwoods@chromium.org3c9d95a2013-05-30 00:19:46 +0000335 unsigned int uConst; // used for uvec, scalar uints
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 bool bConst; // used for bvec, scalar bools
337 float fConst; // used for vec, mat, scalar floats
338 } ;
339
340 TBasicType type;
341};
342
343#endif // _CONSTANT_UNION_INCLUDED_