1 //
2 // Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
4 // Copyright (C) 2017 ARM Limited.
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions
10 // are met:
11 //
12 //    Redistributions of source code must retain the above copyright
13 //    notice, this list of conditions and the following disclaimer.
14 //
15 //    Redistributions in binary form must reproduce the above
16 //    copyright notice, this list of conditions and the following
17 //    disclaimer in the documentation and/or other materials provided
18 //    with the distribution.
19 //
20 //    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21 //    contributors may be used to endorse or promote products derived
22 //    from this software without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 // POSSIBILITY OF SUCH DAMAGE.
36 //
37 
38 #ifndef _CONSTANT_UNION_INCLUDED_
39 #define _CONSTANT_UNION_INCLUDED_
40 
41 #include "../Include/Common.h"
42 #include "../Include/BaseTypes.h"
43 
44 namespace glslang {
45 
46 class TConstUnion {
47 public:
POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator ())48     POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
49 
50     TConstUnion() : iConst(0), type(EbtInt) { }
51 
setI8Const(signed char i)52     void setI8Const(signed char i)
53     {
54        i8Const = i;
55        type = EbtInt8;
56     }
57 
setU8Const(unsigned char u)58     void setU8Const(unsigned char u)
59     {
60        u8Const = u;
61        type = EbtUint8;
62     }
63 
setI16Const(signed short i)64     void setI16Const(signed short i)
65     {
66        i16Const = i;
67        type = EbtInt16;
68     }
69 
setU16Const(unsigned short u)70     void setU16Const(unsigned short u)
71     {
72        u16Const = u;
73        type = EbtUint16;
74     }
75 
setIConst(int i)76     void setIConst(int i)
77     {
78         iConst = i;
79         type = EbtInt;
80     }
81 
setUConst(unsigned int u)82     void setUConst(unsigned int u)
83     {
84         uConst = u;
85         type = EbtUint;
86     }
87 
setI64Const(long long i64)88     void setI64Const(long long i64)
89     {
90         i64Const = i64;
91         type = EbtInt64;
92     }
93 
setU64Const(unsigned long long u64)94     void setU64Const(unsigned long long u64)
95     {
96         u64Const = u64;
97         type = EbtUint64;
98     }
99 
setDConst(double d)100     void setDConst(double d)
101     {
102         dConst = d;
103         type = EbtDouble;
104     }
105 
setBConst(bool b)106     void setBConst(bool b)
107     {
108         bConst = b;
109         type = EbtBool;
110     }
111 
setSConst(const TString * s)112     void setSConst(const TString* s)
113     {
114         sConst = s;
115         type = EbtString;
116     }
117 
getI8Const()118     signed char        getI8Const() const  { return i8Const; }
getU8Const()119     unsigned char      getU8Const() const  { return u8Const; }
getI16Const()120     signed short       getI16Const() const { return i16Const; }
getU16Const()121     unsigned short     getU16Const() const { return u16Const; }
getIConst()122     int                getIConst() const   { return iConst; }
getUConst()123     unsigned int       getUConst() const   { return uConst; }
getI64Const()124     long long          getI64Const() const { return i64Const; }
getU64Const()125     unsigned long long getU64Const() const { return u64Const; }
getDConst()126     double             getDConst() const   { return dConst; }
getBConst()127     bool               getBConst() const   { return bConst; }
getSConst()128     const TString*     getSConst() const   { return sConst; }
129 
130     bool operator==(const signed char i) const
131     {
132         if (i == i8Const)
133             return true;
134 
135         return false;
136     }
137 
138     bool operator==(const unsigned char u) const
139     {
140         if (u == u8Const)
141             return true;
142 
143         return false;
144     }
145 
146    bool operator==(const signed short i) const
147     {
148         if (i == i16Const)
149             return true;
150 
151         return false;
152     }
153 
154     bool operator==(const unsigned short u) const
155     {
156         if (u == u16Const)
157             return true;
158 
159         return false;
160     }
161 
162     bool operator==(const int i) const
163     {
164         if (i == iConst)
165             return true;
166 
167         return false;
168     }
169 
170     bool operator==(const unsigned int u) const
171     {
172         if (u == uConst)
173             return true;
174 
175         return false;
176     }
177 
178     bool operator==(const long long i64) const
179     {
180         if (i64 == i64Const)
181             return true;
182 
183         return false;
184     }
185 
186     bool operator==(const unsigned long long u64) const
187     {
188         if (u64 == u64Const)
189             return true;
190 
191         return false;
192     }
193 
194     bool operator==(const double d) const
195     {
196         if (d == dConst)
197             return true;
198 
199         return false;
200     }
201 
202     bool operator==(const bool b) const
203     {
204         if (b == bConst)
205             return true;
206 
207         return false;
208     }
209 
210     bool operator==(const TConstUnion& constant) const
211     {
212         if (constant.type != type)
213             return false;
214 
215         switch (type) {
216         case EbtInt:
217             if (constant.iConst == iConst)
218                 return true;
219 
220             break;
221         case EbtUint:
222             if (constant.uConst == uConst)
223                 return true;
224 
225             break;
226         case EbtBool:
227             if (constant.bConst == bConst)
228                 return true;
229 
230             break;
231         case EbtDouble:
232             if (constant.dConst == dConst)
233                 return true;
234 
235             break;
236 
237 #ifndef GLSLANG_WEB
238         case EbtInt16:
239             if (constant.i16Const == i16Const)
240                 return true;
241 
242             break;
243          case EbtUint16:
244             if (constant.u16Const == u16Const)
245                 return true;
246 
247             break;
248         case EbtInt8:
249             if (constant.i8Const == i8Const)
250                 return true;
251 
252             break;
253          case EbtUint8:
254             if (constant.u8Const == u8Const)
255                 return true;
256 
257             break;
258         case EbtInt64:
259             if (constant.i64Const == i64Const)
260                 return true;
261 
262             break;
263         case EbtUint64:
264             if (constant.u64Const == u64Const)
265                 return true;
266 
267             break;
268 #endif
269         default:
270             assert(false && "Default missing");
271         }
272 
273         return false;
274     }
275 
276     bool operator!=(const signed char i) const
277     {
278         return !operator==(i);
279     }
280 
281     bool operator!=(const unsigned char u) const
282     {
283         return !operator==(u);
284     }
285 
286     bool operator!=(const signed short i) const
287     {
288         return !operator==(i);
289     }
290 
291     bool operator!=(const unsigned short u) const
292     {
293         return !operator==(u);
294     }
295 
296     bool operator!=(const int i) const
297     {
298         return !operator==(i);
299     }
300 
301     bool operator!=(const unsigned int u) const
302     {
303         return !operator==(u);
304     }
305 
306     bool operator!=(const long long i) const
307     {
308         return !operator==(i);
309     }
310 
311     bool operator!=(const unsigned long long u) const
312     {
313         return !operator==(u);
314     }
315 
316     bool operator!=(const float f) const
317     {
318         return !operator==(f);
319     }
320 
321     bool operator!=(const bool b) const
322     {
323         return !operator==(b);
324     }
325 
326     bool operator!=(const TConstUnion& constant) const
327     {
328         return !operator==(constant);
329     }
330 
331     bool operator>(const TConstUnion& constant) const
332     {
333         assert(type == constant.type);
334         switch (type) {
335         case EbtInt:
336             if (iConst > constant.iConst)
337                 return true;
338 
339             return false;
340         case EbtUint:
341             if (uConst > constant.uConst)
342                 return true;
343 
344             return false;
345         case EbtDouble:
346             if (dConst > constant.dConst)
347                 return true;
348 
349             return false;
350 #ifndef GLSLANG_WEB
351         case EbtInt8:
352             if (i8Const > constant.i8Const)
353                 return true;
354 
355             return false;
356         case EbtUint8:
357             if (u8Const > constant.u8Const)
358                 return true;
359 
360             return false;
361         case EbtInt16:
362             if (i16Const > constant.i16Const)
363                 return true;
364 
365             return false;
366         case EbtUint16:
367             if (u16Const > constant.u16Const)
368                 return true;
369 
370             return false;
371         case EbtInt64:
372             if (i64Const > constant.i64Const)
373                 return true;
374 
375             return false;
376         case EbtUint64:
377             if (u64Const > constant.u64Const)
378                 return true;
379 
380             return false;
381 #endif
382         default:
383             assert(false && "Default missing");
384             return false;
385         }
386     }
387 
388     bool operator<(const TConstUnion& constant) const
389     {
390         assert(type == constant.type);
391         switch (type) {
392 #ifndef GLSLANG_WEB
393         case EbtInt8:
394             if (i8Const < constant.i8Const)
395                 return true;
396 
397             return false;
398         case EbtUint8:
399             if (u8Const < constant.u8Const)
400                 return true;
401 
402             return false;
403         case EbtInt16:
404             if (i16Const < constant.i16Const)
405                 return true;
406 
407             return false;
408         case EbtUint16:
409             if (u16Const < constant.u16Const)
410                 return true;
411             return false;
412         case EbtInt64:
413             if (i64Const < constant.i64Const)
414                 return true;
415 
416             return false;
417         case EbtUint64:
418             if (u64Const < constant.u64Const)
419                 return true;
420 
421             return false;
422 #endif
423         case EbtDouble:
424             if (dConst < constant.dConst)
425                 return true;
426 
427             return false;
428         case EbtInt:
429             if (iConst < constant.iConst)
430                 return true;
431 
432             return false;
433         case EbtUint:
434             if (uConst < constant.uConst)
435                 return true;
436 
437             return false;
438         default:
439             assert(false && "Default missing");
440             return false;
441         }
442     }
443 
444     TConstUnion operator+(const TConstUnion& constant) const
445     {
446         TConstUnion returnValue;
447         assert(type == constant.type);
448         switch (type) {
449         case EbtInt:    returnValue.setIConst(iConst + constant.iConst); break;
450         case EbtUint:   returnValue.setUConst(uConst + constant.uConst); break;
451         case EbtDouble: returnValue.setDConst(dConst + constant.dConst); break;
452 #ifndef GLSLANG_WEB
453         case EbtInt8:   returnValue.setI8Const(i8Const + constant.i8Const); break;
454         case EbtInt16:  returnValue.setI16Const(i16Const + constant.i16Const); break;
455         case EbtInt64:  returnValue.setI64Const(i64Const + constant.i64Const); break;
456         case EbtUint8:  returnValue.setU8Const(u8Const + constant.u8Const); break;
457         case EbtUint16: returnValue.setU16Const(u16Const + constant.u16Const); break;
458         case EbtUint64: returnValue.setU64Const(u64Const + constant.u64Const); break;
459 #endif
460         default: assert(false && "Default missing");
461         }
462 
463         return returnValue;
464     }
465 
466     TConstUnion operator-(const TConstUnion& constant) const
467     {
468         TConstUnion returnValue;
469         assert(type == constant.type);
470         switch (type) {
471         case EbtInt:    returnValue.setIConst(iConst - constant.iConst); break;
472         case EbtUint:   returnValue.setUConst(uConst - constant.uConst); break;
473         case EbtDouble: returnValue.setDConst(dConst - constant.dConst); break;
474 #ifndef GLSLANG_WEB
475         case EbtInt8:   returnValue.setI8Const(i8Const - constant.i8Const); break;
476         case EbtInt16:  returnValue.setI16Const(i16Const - constant.i16Const); break;
477         case EbtInt64:  returnValue.setI64Const(i64Const - constant.i64Const); break;
478         case EbtUint8:  returnValue.setU8Const(u8Const - constant.u8Const); break;
479         case EbtUint16: returnValue.setU16Const(u16Const - constant.u16Const); break;
480         case EbtUint64: returnValue.setU64Const(u64Const - constant.u64Const); break;
481 #endif
482         default: assert(false && "Default missing");
483         }
484 
485         return returnValue;
486     }
487 
488     TConstUnion operator*(const TConstUnion& constant) const
489     {
490         TConstUnion returnValue;
491         assert(type == constant.type);
492         switch (type) {
493         case EbtInt:    returnValue.setIConst(iConst * constant.iConst); break;
494         case EbtUint:   returnValue.setUConst(uConst * constant.uConst); break;
495         case EbtDouble: returnValue.setDConst(dConst * constant.dConst); break;
496 #ifndef GLSLANG_WEB
497         case EbtInt8:   returnValue.setI8Const(i8Const * constant.i8Const); break;
498         case EbtInt16:  returnValue.setI16Const(i16Const * constant.i16Const); break;
499         case EbtInt64:  returnValue.setI64Const(i64Const * constant.i64Const); break;
500         case EbtUint8:  returnValue.setU8Const(u8Const * constant.u8Const); break;
501         case EbtUint16: returnValue.setU16Const(u16Const * constant.u16Const); break;
502         case EbtUint64: returnValue.setU64Const(u64Const * constant.u64Const); break;
503 #endif
504         default: assert(false && "Default missing");
505         }
506 
507         return returnValue;
508     }
509 
510     TConstUnion operator%(const TConstUnion& constant) const
511     {
512         TConstUnion returnValue;
513         assert(type == constant.type);
514         switch (type) {
515         case EbtInt:    returnValue.setIConst(iConst % constant.iConst); break;
516         case EbtUint:   returnValue.setUConst(uConst % constant.uConst); break;
517 #ifndef GLSLANG_WEB
518         case EbtInt8:   returnValue.setI8Const(i8Const % constant.i8Const); break;
519         case EbtInt16:  returnValue.setI8Const(i8Const % constant.i16Const); break;
520         case EbtInt64:  returnValue.setI64Const(i64Const % constant.i64Const); break;
521         case EbtUint8:  returnValue.setU8Const(u8Const % constant.u8Const); break;
522         case EbtUint16: returnValue.setU16Const(u16Const % constant.u16Const); break;
523         case EbtUint64: returnValue.setU64Const(u64Const % constant.u64Const); break;
524 #endif
525         default:     assert(false && "Default missing");
526         }
527 
528         return returnValue;
529     }
530 
531     TConstUnion operator>>(const TConstUnion& constant) const
532     {
533         TConstUnion returnValue;
534         switch (type) {
535 #ifndef GLSLANG_WEB
536         case EbtInt8:
537             switch (constant.type) {
538             case EbtInt8:   returnValue.setI8Const(i8Const >> constant.i8Const);  break;
539             case EbtUint8:  returnValue.setI8Const(i8Const >> constant.u8Const);  break;
540             case EbtInt16:  returnValue.setI8Const(i8Const >> constant.i16Const); break;
541             case EbtUint16: returnValue.setI8Const(i8Const >> constant.u16Const); break;
542             case EbtInt:    returnValue.setI8Const(i8Const >> constant.iConst);   break;
543             case EbtUint:   returnValue.setI8Const(i8Const >> constant.uConst);   break;
544             case EbtInt64:  returnValue.setI8Const(i8Const >> constant.i64Const); break;
545             case EbtUint64: returnValue.setI8Const(i8Const >> constant.u64Const); break;
546             default:       assert(false && "Default missing");
547             }
548             break;
549         case EbtUint8:
550             switch (constant.type) {
551             case EbtInt8:   returnValue.setU8Const(u8Const >> constant.i8Const);  break;
552             case EbtUint8:  returnValue.setU8Const(u8Const >> constant.u8Const);  break;
553             case EbtInt16:  returnValue.setU8Const(u8Const >> constant.i16Const); break;
554             case EbtUint16: returnValue.setU8Const(u8Const >> constant.u16Const); break;
555             case EbtInt:    returnValue.setU8Const(u8Const >> constant.iConst);   break;
556             case EbtUint:   returnValue.setU8Const(u8Const >> constant.uConst);   break;
557             case EbtInt64:  returnValue.setU8Const(u8Const >> constant.i64Const); break;
558             case EbtUint64: returnValue.setU8Const(u8Const >> constant.u64Const); break;
559             default:       assert(false && "Default missing");
560             }
561             break;
562         case EbtInt16:
563             switch (constant.type) {
564             case EbtInt8:   returnValue.setI16Const(i16Const >> constant.i8Const);  break;
565             case EbtUint8:  returnValue.setI16Const(i16Const >> constant.u8Const);  break;
566             case EbtInt16:  returnValue.setI16Const(i16Const >> constant.i16Const); break;
567             case EbtUint16: returnValue.setI16Const(i16Const >> constant.u16Const); break;
568             case EbtInt:    returnValue.setI16Const(i16Const >> constant.iConst);   break;
569             case EbtUint:   returnValue.setI16Const(i16Const >> constant.uConst);   break;
570             case EbtInt64:  returnValue.setI16Const(i16Const >> constant.i64Const); break;
571             case EbtUint64: returnValue.setI16Const(i16Const >> constant.u64Const); break;
572             default:       assert(false && "Default missing");
573             }
574             break;
575         case EbtUint16:
576             switch (constant.type) {
577             case EbtInt8:   returnValue.setU16Const(u16Const >> constant.i8Const);  break;
578             case EbtUint8:  returnValue.setU16Const(u16Const >> constant.u8Const);  break;
579             case EbtInt16:  returnValue.setU16Const(u16Const >> constant.i16Const); break;
580             case EbtUint16: returnValue.setU16Const(u16Const >> constant.u16Const); break;
581             case EbtInt:    returnValue.setU16Const(u16Const >> constant.iConst);   break;
582             case EbtUint:   returnValue.setU16Const(u16Const >> constant.uConst);   break;
583             case EbtInt64:  returnValue.setU16Const(u16Const >> constant.i64Const); break;
584             case EbtUint64: returnValue.setU16Const(u16Const >> constant.u64Const); break;
585             default:       assert(false && "Default missing");
586             }
587             break;
588 #endif
589         case EbtInt:
590             switch (constant.type) {
591             case EbtInt:    returnValue.setIConst(iConst >> constant.iConst);   break;
592             case EbtUint:   returnValue.setIConst(iConst >> constant.uConst);   break;
593 #ifndef GLSLANG_WEB
594             case EbtInt8:   returnValue.setIConst(iConst >> constant.i8Const);  break;
595             case EbtUint8:  returnValue.setIConst(iConst >> constant.u8Const);  break;
596             case EbtInt16:  returnValue.setIConst(iConst >> constant.i16Const); break;
597             case EbtUint16: returnValue.setIConst(iConst >> constant.u16Const); break;
598             case EbtInt64:  returnValue.setIConst(iConst >> constant.i64Const); break;
599             case EbtUint64: returnValue.setIConst(iConst >> constant.u64Const); break;
600 #endif
601             default:       assert(false && "Default missing");
602             }
603             break;
604         case EbtUint:
605             switch (constant.type) {
606             case EbtInt:    returnValue.setUConst(uConst >> constant.iConst);   break;
607             case EbtUint:   returnValue.setUConst(uConst >> constant.uConst);   break;
608 #ifndef GLSLANG_WEB
609             case EbtInt8:   returnValue.setUConst(uConst >> constant.i8Const);  break;
610             case EbtUint8:  returnValue.setUConst(uConst >> constant.u8Const);  break;
611             case EbtInt16:  returnValue.setUConst(uConst >> constant.i16Const); break;
612             case EbtUint16: returnValue.setUConst(uConst >> constant.u16Const); break;
613             case EbtInt64:  returnValue.setUConst(uConst >> constant.i64Const); break;
614             case EbtUint64: returnValue.setUConst(uConst >> constant.u64Const); break;
615 #endif
616             default:       assert(false && "Default missing");
617             }
618             break;
619 #ifndef GLSLANG_WEB
620          case EbtInt64:
621             switch (constant.type) {
622             case EbtInt8:   returnValue.setI64Const(i64Const >> constant.i8Const);  break;
623             case EbtUint8:  returnValue.setI64Const(i64Const >> constant.u8Const);  break;
624             case EbtInt16:  returnValue.setI64Const(i64Const >> constant.i16Const); break;
625             case EbtUint16: returnValue.setI64Const(i64Const >> constant.u16Const); break;
626             case EbtInt:    returnValue.setI64Const(i64Const >> constant.iConst);   break;
627             case EbtUint:   returnValue.setI64Const(i64Const >> constant.uConst);   break;
628             case EbtInt64:  returnValue.setI64Const(i64Const >> constant.i64Const); break;
629             case EbtUint64: returnValue.setI64Const(i64Const >> constant.u64Const); break;
630             default:       assert(false && "Default missing");
631             }
632             break;
633         case EbtUint64:
634             switch (constant.type) {
635             case EbtInt8:   returnValue.setU64Const(u64Const >> constant.i8Const);  break;
636             case EbtUint8:  returnValue.setU64Const(u64Const >> constant.u8Const);  break;
637             case EbtInt16:  returnValue.setU64Const(u64Const >> constant.i16Const); break;
638             case EbtUint16: returnValue.setU64Const(u64Const >> constant.u16Const); break;
639             case EbtInt:    returnValue.setU64Const(u64Const >> constant.iConst);   break;
640             case EbtUint:   returnValue.setU64Const(u64Const >> constant.uConst);   break;
641             case EbtInt64:  returnValue.setU64Const(u64Const >> constant.i64Const); break;
642             case EbtUint64: returnValue.setU64Const(u64Const >> constant.u64Const); break;
643             default:       assert(false && "Default missing");
644             }
645             break;
646 #endif
647         default:     assert(false && "Default missing");
648         }
649 
650         return returnValue;
651     }
652 
653     TConstUnion operator<<(const TConstUnion& constant) const
654     {
655         TConstUnion returnValue;
656         switch (type) {
657 #ifndef GLSLANG_WEB
658         case EbtInt8:
659             switch (constant.type) {
660             case EbtInt8:   returnValue.setI8Const(i8Const << constant.i8Const);  break;
661             case EbtUint8:  returnValue.setI8Const(i8Const << constant.u8Const);  break;
662             case EbtInt16:  returnValue.setI8Const(i8Const << constant.i16Const); break;
663             case EbtUint16: returnValue.setI8Const(i8Const << constant.u16Const); break;
664             case EbtInt:    returnValue.setI8Const(i8Const << constant.iConst);   break;
665             case EbtUint:   returnValue.setI8Const(i8Const << constant.uConst);   break;
666             case EbtInt64:  returnValue.setI8Const(i8Const << constant.i64Const); break;
667             case EbtUint64: returnValue.setI8Const(i8Const << constant.u64Const); break;
668             default:       assert(false && "Default missing");
669             }
670             break;
671         case EbtUint8:
672             switch (constant.type) {
673             case EbtInt8:   returnValue.setU8Const(u8Const << constant.i8Const);  break;
674             case EbtUint8:  returnValue.setU8Const(u8Const << constant.u8Const);  break;
675             case EbtInt16:  returnValue.setU8Const(u8Const << constant.i16Const); break;
676             case EbtUint16: returnValue.setU8Const(u8Const << constant.u16Const); break;
677             case EbtInt:    returnValue.setU8Const(u8Const << constant.iConst);   break;
678             case EbtUint:   returnValue.setU8Const(u8Const << constant.uConst);   break;
679             case EbtInt64:  returnValue.setU8Const(u8Const << constant.i64Const); break;
680             case EbtUint64: returnValue.setU8Const(u8Const << constant.u64Const); break;
681             default:       assert(false && "Default missing");
682             }
683             break;
684         case EbtInt16:
685             switch (constant.type) {
686             case EbtInt8:   returnValue.setI16Const(i16Const << constant.i8Const);  break;
687             case EbtUint8:  returnValue.setI16Const(i16Const << constant.u8Const);  break;
688             case EbtInt16:  returnValue.setI16Const(i16Const << constant.i16Const); break;
689             case EbtUint16: returnValue.setI16Const(i16Const << constant.u16Const); break;
690             case EbtInt:    returnValue.setI16Const(i16Const << constant.iConst);   break;
691             case EbtUint:   returnValue.setI16Const(i16Const << constant.uConst);   break;
692             case EbtInt64:  returnValue.setI16Const(i16Const << constant.i64Const); break;
693             case EbtUint64: returnValue.setI16Const(i16Const << constant.u64Const); break;
694             default:       assert(false && "Default missing");
695             }
696             break;
697         case EbtUint16:
698             switch (constant.type) {
699             case EbtInt8:   returnValue.setU16Const(u16Const << constant.i8Const);  break;
700             case EbtUint8:  returnValue.setU16Const(u16Const << constant.u8Const);  break;
701             case EbtInt16:  returnValue.setU16Const(u16Const << constant.i16Const); break;
702             case EbtUint16: returnValue.setU16Const(u16Const << constant.u16Const); break;
703             case EbtInt:    returnValue.setU16Const(u16Const << constant.iConst);   break;
704             case EbtUint:   returnValue.setU16Const(u16Const << constant.uConst);   break;
705             case EbtInt64:  returnValue.setU16Const(u16Const << constant.i64Const); break;
706             case EbtUint64: returnValue.setU16Const(u16Const << constant.u64Const); break;
707             default:       assert(false && "Default missing");
708             }
709             break;
710          case EbtInt64:
711             switch (constant.type) {
712             case EbtInt8:   returnValue.setI64Const(i64Const << constant.i8Const);  break;
713             case EbtUint8:  returnValue.setI64Const(i64Const << constant.u8Const);  break;
714             case EbtInt16:  returnValue.setI64Const(i64Const << constant.i16Const); break;
715             case EbtUint16: returnValue.setI64Const(i64Const << constant.u16Const); break;
716             case EbtInt:    returnValue.setI64Const(i64Const << constant.iConst);   break;
717             case EbtUint:   returnValue.setI64Const(i64Const << constant.uConst);   break;
718             case EbtInt64:  returnValue.setI64Const(i64Const << constant.i64Const); break;
719             case EbtUint64: returnValue.setI64Const(i64Const << constant.u64Const); break;
720             default:       assert(false && "Default missing");
721             }
722             break;
723         case EbtUint64:
724             switch (constant.type) {
725             case EbtInt8:   returnValue.setU64Const(u64Const << constant.i8Const);  break;
726             case EbtUint8:  returnValue.setU64Const(u64Const << constant.u8Const);  break;
727             case EbtInt16:  returnValue.setU64Const(u64Const << constant.i16Const); break;
728             case EbtUint16: returnValue.setU64Const(u64Const << constant.u16Const); break;
729             case EbtInt:    returnValue.setU64Const(u64Const << constant.iConst);   break;
730             case EbtUint:   returnValue.setU64Const(u64Const << constant.uConst);   break;
731             case EbtInt64:  returnValue.setU64Const(u64Const << constant.i64Const); break;
732             case EbtUint64: returnValue.setU64Const(u64Const << constant.u64Const); break;
733             default:       assert(false && "Default missing");
734             }
735             break;
736 #endif
737         case EbtInt:
738             switch (constant.type) {
739             case EbtInt:    returnValue.setIConst(iConst << constant.iConst);   break;
740             case EbtUint:   returnValue.setIConst(iConst << constant.uConst);   break;
741 #ifndef GLSLANG_WEB
742             case EbtInt8:   returnValue.setIConst(iConst << constant.i8Const);  break;
743             case EbtUint8:  returnValue.setIConst(iConst << constant.u8Const);  break;
744             case EbtInt16:  returnValue.setIConst(iConst << constant.i16Const); break;
745             case EbtUint16: returnValue.setIConst(iConst << constant.u16Const); break;
746             case EbtInt64:  returnValue.setIConst(iConst << constant.i64Const); break;
747             case EbtUint64: returnValue.setIConst(iConst << constant.u64Const); break;
748 #endif
749             default:       assert(false && "Default missing");
750             }
751             break;
752         case EbtUint:
753             switch (constant.type) {
754             case EbtInt:    returnValue.setUConst(uConst << constant.iConst);   break;
755             case EbtUint:   returnValue.setUConst(uConst << constant.uConst);   break;
756 #ifndef GLSLANG_WEB
757             case EbtInt8:   returnValue.setUConst(uConst << constant.i8Const);  break;
758             case EbtUint8:  returnValue.setUConst(uConst << constant.u8Const);  break;
759             case EbtInt16:  returnValue.setUConst(uConst << constant.i16Const); break;
760             case EbtUint16: returnValue.setUConst(uConst << constant.u16Const); break;
761             case EbtInt64:  returnValue.setUConst(uConst << constant.i64Const); break;
762             case EbtUint64: returnValue.setUConst(uConst << constant.u64Const); break;
763 #endif
764             default:       assert(false && "Default missing");
765             }
766             break;
767         default:     assert(false && "Default missing");
768         }
769 
770         return returnValue;
771     }
772 
773     TConstUnion operator&(const TConstUnion& constant) const
774     {
775         TConstUnion returnValue;
776         assert(type == constant.type);
777         switch (type) {
778         case EbtInt:    returnValue.setIConst(iConst & constant.iConst); break;
779         case EbtUint:   returnValue.setUConst(uConst & constant.uConst); break;
780 #ifndef GLSLANG_WEB
781         case EbtInt8:   returnValue.setI8Const(i8Const & constant.i8Const); break;
782         case EbtUint8:  returnValue.setU8Const(u8Const & constant.u8Const); break;
783         case EbtInt16:  returnValue.setI16Const(i16Const & constant.i16Const); break;
784         case EbtUint16: returnValue.setU16Const(u16Const & constant.u16Const); break;
785         case EbtInt64:  returnValue.setI64Const(i64Const & constant.i64Const); break;
786         case EbtUint64: returnValue.setU64Const(u64Const & constant.u64Const); break;
787 #endif
788         default:     assert(false && "Default missing");
789         }
790 
791         return returnValue;
792     }
793 
794     TConstUnion operator|(const TConstUnion& constant) const
795     {
796         TConstUnion returnValue;
797         assert(type == constant.type);
798         switch (type) {
799         case EbtInt:    returnValue.setIConst(iConst | constant.iConst); break;
800         case EbtUint:   returnValue.setUConst(uConst | constant.uConst); break;
801 #ifndef GLSLANG_WEB
802         case EbtInt8:   returnValue.setI8Const(i8Const | constant.i8Const); break;
803         case EbtUint8:  returnValue.setU8Const(u8Const | constant.u8Const); break;
804         case EbtInt16:  returnValue.setI16Const(i16Const | constant.i16Const); break;
805         case EbtUint16: returnValue.setU16Const(u16Const | constant.u16Const); break;
806         case EbtInt64:  returnValue.setI64Const(i64Const | constant.i64Const); break;
807         case EbtUint64: returnValue.setU64Const(u64Const | constant.u64Const); break;
808 #endif
809         default:     assert(false && "Default missing");
810         }
811 
812         return returnValue;
813     }
814 
815     TConstUnion operator^(const TConstUnion& constant) const
816     {
817         TConstUnion returnValue;
818         assert(type == constant.type);
819         switch (type) {
820         case EbtInt:    returnValue.setIConst(iConst ^ constant.iConst); break;
821         case EbtUint:   returnValue.setUConst(uConst ^ constant.uConst); break;
822 #ifndef GLSLANG_WEB
823         case EbtInt8:   returnValue.setI8Const(i8Const ^ constant.i8Const); break;
824         case EbtUint8:  returnValue.setU8Const(u8Const ^ constant.u8Const); break;
825         case EbtInt16:  returnValue.setI16Const(i16Const ^ constant.i16Const); break;
826         case EbtUint16: returnValue.setU16Const(u16Const ^ constant.u16Const); break;
827         case EbtInt64:  returnValue.setI64Const(i64Const ^ constant.i64Const); break;
828         case EbtUint64: returnValue.setU64Const(u64Const ^ constant.u64Const); break;
829 #endif
830         default:     assert(false && "Default missing");
831         }
832 
833         return returnValue;
834     }
835 
836     TConstUnion operator~() const
837     {
838         TConstUnion returnValue;
839         switch (type) {
840         case EbtInt:    returnValue.setIConst(~iConst); break;
841         case EbtUint:   returnValue.setUConst(~uConst); break;
842 #ifndef GLSLANG_WEB
843         case EbtInt8:   returnValue.setI8Const(~i8Const); break;
844         case EbtUint8:  returnValue.setU8Const(~u8Const); break;
845         case EbtInt16:  returnValue.setI16Const(~i16Const); break;
846         case EbtUint16: returnValue.setU16Const(~u16Const); break;
847         case EbtInt64:  returnValue.setI64Const(~i64Const); break;
848         case EbtUint64: returnValue.setU64Const(~u64Const); break;
849 #endif
850         default:     assert(false && "Default missing");
851         }
852 
853         return returnValue;
854     }
855 
856     TConstUnion operator&&(const TConstUnion& constant) const
857     {
858         TConstUnion returnValue;
859         assert(type == constant.type);
860         switch (type) {
861         case EbtBool: returnValue.setBConst(bConst && constant.bConst); break;
862         default:     assert(false && "Default missing");
863         }
864 
865         return returnValue;
866     }
867 
868     TConstUnion operator||(const TConstUnion& constant) const
869     {
870         TConstUnion returnValue;
871         assert(type == constant.type);
872         switch (type) {
873         case EbtBool: returnValue.setBConst(bConst || constant.bConst); break;
874         default:     assert(false && "Default missing");
875         }
876 
877         return returnValue;
878     }
879 
getType()880     TBasicType getType() const { return type; }
881 
882 private:
883     union  {
884         signed char        i8Const;     // used for i8vec, scalar int8s
885         unsigned char      u8Const;     // used for u8vec, scalar uint8s
886         signed short       i16Const;    // used for i16vec, scalar int16s
887         unsigned short     u16Const;    // used for u16vec, scalar uint16s
888         int                iConst;      // used for ivec, scalar ints
889         unsigned int       uConst;      // used for uvec, scalar uints
890         long long          i64Const;    // used for i64vec, scalar int64s
891         unsigned long long u64Const;    // used for u64vec, scalar uint64s
892         bool               bConst;      // used for bvec, scalar bools
893         double             dConst;      // used for vec, dvec, mat, dmat, scalar floats and doubles
894         const TString*     sConst;      // string constant
895     };
896 
897     TBasicType type;
898 };
899 
900 // Encapsulate having a pointer to an array of TConstUnion,
901 // which only needs to be allocated if its size is going to be
902 // bigger than 0.
903 //
904 // One convenience is being able to use [] to go inside the array, instead
905 // of C++ assuming it as an array of pointers to vectors.
906 //
907 // General usage is that the size is known up front, and it is
908 // created once with the proper size.
909 //
910 class TConstUnionArray {
911 public:
POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator ())912     POOL_ALLOCATOR_NEW_DELETE(GetThreadPoolAllocator())
913 
914     TConstUnionArray() : unionArray(nullptr) { }
~TConstUnionArray()915     virtual ~TConstUnionArray() { }
916 
TConstUnionArray(int size)917     explicit TConstUnionArray(int size)
918     {
919         if (size == 0)
920             unionArray = nullptr;
921         else
922             unionArray =  new TConstUnionVector(size);
923     }
924     TConstUnionArray(const TConstUnionArray& a) = default;
TConstUnionArray(const TConstUnionArray & a,int start,int size)925     TConstUnionArray(const TConstUnionArray& a, int start, int size)
926     {
927         unionArray = new TConstUnionVector(size);
928         for (int i = 0; i < size; ++i)
929             (*unionArray)[i] = a[start + i];
930     }
931 
932     // Use this constructor for a smear operation
TConstUnionArray(int size,const TConstUnion & val)933     TConstUnionArray(int size, const TConstUnion& val)
934     {
935         unionArray = new TConstUnionVector(size, val);
936     }
937 
size()938     int size() const { return unionArray ? (int)unionArray->size() : 0; }
939     TConstUnion& operator[](size_t index) { return (*unionArray)[index]; }
940     const TConstUnion& operator[](size_t index) const { return (*unionArray)[index]; }
941     bool operator==(const TConstUnionArray& rhs) const
942     {
943         // this includes the case that both are unallocated
944         if (unionArray == rhs.unionArray)
945             return true;
946 
947         if (! unionArray || ! rhs.unionArray)
948             return false;
949 
950         return *unionArray == *rhs.unionArray;
951     }
952     bool operator!=(const TConstUnionArray& rhs) const { return ! operator==(rhs); }
953 
dot(const TConstUnionArray & rhs)954     double dot(const TConstUnionArray& rhs)
955     {
956         assert(rhs.unionArray->size() == unionArray->size());
957         double sum = 0.0;
958 
959         for (size_t comp = 0; comp < unionArray->size(); ++comp)
960             sum += (*this)[comp].getDConst() * rhs[comp].getDConst();
961 
962         return sum;
963     }
964 
empty()965     bool empty() const { return unionArray == nullptr; }
966 
967 protected:
968     typedef TVector<TConstUnion> TConstUnionVector;
969     TConstUnionVector* unionArray;
970 };
971 
972 } // end namespace glslang
973 
974 #endif // _CONSTANT_UNION_INCLUDED_
975