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