daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 7 | #include "compiler/ParseHelper.h" |
| 8 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 9 | #include <stdarg.h> |
apatrick@chromium.org | 8187fa8 | 2010-06-15 22:09:28 +0000 | [diff] [blame] | 10 | #include <stdio.h> |
daniel@transgaming.com | bbf56f7 | 2010-04-20 18:52:13 +0000 | [diff] [blame] | 11 | |
| 12 | #include "compiler/osinclude.h" |
| 13 | #include "compiler/InitializeParseContext.h" |
| 14 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 15 | /////////////////////////////////////////////////////////////////////// |
| 16 | // |
| 17 | // Sub- vector and matrix fields |
| 18 | // |
| 19 | //////////////////////////////////////////////////////////////////////// |
| 20 | |
| 21 | // |
| 22 | // Look at a '.' field selector string and change it into offsets |
| 23 | // for a vector. |
| 24 | // |
| 25 | bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, int line) |
| 26 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 27 | fields.num = (int) compString.size(); |
| 28 | if (fields.num > 4) { |
| 29 | error(line, "illegal vector field selection", compString.c_str(), ""); |
| 30 | return false; |
| 31 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 32 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 33 | enum { |
| 34 | exyzw, |
| 35 | ergba, |
| 36 | estpq, |
| 37 | } fieldSet[4]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 38 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 39 | for (int i = 0; i < fields.num; ++i) { |
| 40 | switch (compString[i]) { |
| 41 | case 'x': |
| 42 | fields.offsets[i] = 0; |
| 43 | fieldSet[i] = exyzw; |
| 44 | break; |
| 45 | case 'r': |
| 46 | fields.offsets[i] = 0; |
| 47 | fieldSet[i] = ergba; |
| 48 | break; |
| 49 | case 's': |
| 50 | fields.offsets[i] = 0; |
| 51 | fieldSet[i] = estpq; |
| 52 | break; |
| 53 | case 'y': |
| 54 | fields.offsets[i] = 1; |
| 55 | fieldSet[i] = exyzw; |
| 56 | break; |
| 57 | case 'g': |
| 58 | fields.offsets[i] = 1; |
| 59 | fieldSet[i] = ergba; |
| 60 | break; |
| 61 | case 't': |
| 62 | fields.offsets[i] = 1; |
| 63 | fieldSet[i] = estpq; |
| 64 | break; |
| 65 | case 'z': |
| 66 | fields.offsets[i] = 2; |
| 67 | fieldSet[i] = exyzw; |
| 68 | break; |
| 69 | case 'b': |
| 70 | fields.offsets[i] = 2; |
| 71 | fieldSet[i] = ergba; |
| 72 | break; |
| 73 | case 'p': |
| 74 | fields.offsets[i] = 2; |
| 75 | fieldSet[i] = estpq; |
| 76 | break; |
| 77 | |
| 78 | case 'w': |
| 79 | fields.offsets[i] = 3; |
| 80 | fieldSet[i] = exyzw; |
| 81 | break; |
| 82 | case 'a': |
| 83 | fields.offsets[i] = 3; |
| 84 | fieldSet[i] = ergba; |
| 85 | break; |
| 86 | case 'q': |
| 87 | fields.offsets[i] = 3; |
| 88 | fieldSet[i] = estpq; |
| 89 | break; |
| 90 | default: |
| 91 | error(line, "illegal vector field selection", compString.c_str(), ""); |
| 92 | return false; |
| 93 | } |
| 94 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 95 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 96 | for (int i = 0; i < fields.num; ++i) { |
| 97 | if (fields.offsets[i] >= vecSize) { |
| 98 | error(line, "vector field selection out of range", compString.c_str(), ""); |
| 99 | return false; |
| 100 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 101 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 102 | if (i > 0) { |
| 103 | if (fieldSet[i] != fieldSet[i-1]) { |
| 104 | error(line, "illegal - vector component fields not from the same set", compString.c_str(), ""); |
| 105 | return false; |
| 106 | } |
| 107 | } |
| 108 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 109 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 110 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | // |
| 115 | // Look at a '.' field selector string and change it into offsets |
| 116 | // for a matrix. |
| 117 | // |
| 118 | bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line) |
| 119 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 120 | fields.wholeRow = false; |
| 121 | fields.wholeCol = false; |
| 122 | fields.row = -1; |
| 123 | fields.col = -1; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 124 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 125 | if (compString.size() != 2) { |
| 126 | error(line, "illegal length of matrix field selection", compString.c_str(), ""); |
| 127 | return false; |
| 128 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 129 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 130 | if (compString[0] == '_') { |
| 131 | if (compString[1] < '0' || compString[1] > '3') { |
| 132 | error(line, "illegal matrix field selection", compString.c_str(), ""); |
| 133 | return false; |
| 134 | } |
| 135 | fields.wholeCol = true; |
| 136 | fields.col = compString[1] - '0'; |
| 137 | } else if (compString[1] == '_') { |
| 138 | if (compString[0] < '0' || compString[0] > '3') { |
| 139 | error(line, "illegal matrix field selection", compString.c_str(), ""); |
| 140 | return false; |
| 141 | } |
| 142 | fields.wholeRow = true; |
| 143 | fields.row = compString[0] - '0'; |
| 144 | } else { |
| 145 | if (compString[0] < '0' || compString[0] > '3' || |
| 146 | compString[1] < '0' || compString[1] > '3') { |
| 147 | error(line, "illegal matrix field selection", compString.c_str(), ""); |
| 148 | return false; |
| 149 | } |
| 150 | fields.row = compString[0] - '0'; |
| 151 | fields.col = compString[1] - '0'; |
| 152 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 153 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 154 | if (fields.row >= matSize || fields.col >= matSize) { |
| 155 | error(line, "matrix field selection out of range", compString.c_str(), ""); |
| 156 | return false; |
| 157 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 158 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 159 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /////////////////////////////////////////////////////////////////////// |
| 163 | // |
| 164 | // Errors |
| 165 | // |
| 166 | //////////////////////////////////////////////////////////////////////// |
| 167 | |
| 168 | // |
| 169 | // Track whether errors have occurred. |
| 170 | // |
| 171 | void TParseContext::recover() |
| 172 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 173 | recoveredFromError = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // |
| 177 | // Used by flex/bison to output all syntax and parsing errors. |
| 178 | // |
alokp@chromium.org | c54bf50 | 2010-07-22 16:49:09 +0000 | [diff] [blame] | 179 | void TParseContext::error(TSourceLoc nLine, const char *szReason, const char *szToken, |
| 180 | const char *szExtraInfoFormat, ...) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 181 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 182 | char szExtraInfo[400]; |
| 183 | va_list marker; |
alokp@chromium.org | ff42c63 | 2010-05-10 15:14:30 +0000 | [diff] [blame] | 184 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 185 | va_start(marker, szExtraInfoFormat); |
alokp@chromium.org | ff42c63 | 2010-05-10 15:14:30 +0000 | [diff] [blame] | 186 | |
| 187 | vsnprintf(szExtraInfo, sizeof(szExtraInfo), szExtraInfoFormat, marker); |
| 188 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 189 | /* VC++ format: file(linenum) : error #: 'token' : extrainfo */ |
| 190 | infoSink.info.prefix(EPrefixError); |
| 191 | infoSink.info.location(nLine); |
| 192 | infoSink.info << "'" << szToken << "' : " << szReason << " " << szExtraInfo << "\n"; |
alokp@chromium.org | ff42c63 | 2010-05-10 15:14:30 +0000 | [diff] [blame] | 193 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 194 | va_end(marker); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 195 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 196 | ++numErrors; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // |
| 200 | // Same error message for all places assignments don't work. |
| 201 | // |
| 202 | void TParseContext::assignError(int line, const char* op, TString left, TString right) |
| 203 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 204 | error(line, "", op, "cannot convert from '%s' to '%s'", |
| 205 | right.c_str(), left.c_str()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // |
| 209 | // Same error message for all places unary operations don't work. |
| 210 | // |
| 211 | void TParseContext::unaryOpError(int line, const char* op, TString operand) |
| 212 | { |
| 213 | error(line, " wrong operand type", op, |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 214 | "no operation '%s' exists that takes an operand of type %s (or there is no acceptable conversion)", |
| 215 | op, operand.c_str()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // |
| 219 | // Same error message for all binary operations don't work. |
| 220 | // |
| 221 | void TParseContext::binaryOpError(int line, const char* op, TString left, TString right) |
| 222 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 223 | error(line, " wrong operand types ", op, |
| 224 | "no operation '%s' exists that takes a left-hand operand of type '%s' and " |
| 225 | "a right operand of type '%s' (or there is no acceptable conversion)", |
| 226 | op, left.c_str(), right.c_str()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 227 | } |
| 228 | |
daniel@transgaming.com | a5d7623 | 2010-05-17 09:58:47 +0000 | [diff] [blame] | 229 | bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){ |
| 230 | switch( type ){ |
| 231 | case EbtFloat: |
| 232 | if( precision == EbpUndefined ){ |
| 233 | error( line, "No precision specified for (float)", "", "" ); |
| 234 | return true; |
| 235 | } |
| 236 | break; |
| 237 | case EbtInt: |
| 238 | if( precision == EbpUndefined ){ |
| 239 | error( line, "No precision specified (int)", "", "" ); |
| 240 | return true; |
| 241 | } |
| 242 | break; |
| 243 | } |
| 244 | return false; |
| 245 | } |
| 246 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 247 | // |
| 248 | // Both test and if necessary, spit out an error, to see if the node is really |
| 249 | // an l-value that can be operated on this way. |
| 250 | // |
| 251 | // Returns true if the was an error. |
| 252 | // |
| 253 | bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node) |
| 254 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 255 | TIntermSymbol* symNode = node->getAsSymbolNode(); |
| 256 | TIntermBinary* binaryNode = node->getAsBinaryNode(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 257 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 258 | if (binaryNode) { |
| 259 | bool errorReturn; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 260 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 261 | switch(binaryNode->getOp()) { |
| 262 | case EOpIndexDirect: |
| 263 | case EOpIndexIndirect: |
| 264 | case EOpIndexDirectStruct: |
| 265 | return lValueErrorCheck(line, op, binaryNode->getLeft()); |
| 266 | case EOpVectorSwizzle: |
| 267 | errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft()); |
| 268 | if (!errorReturn) { |
| 269 | int offset[4] = {0,0,0,0}; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 270 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 271 | TIntermTyped* rightNode = binaryNode->getRight(); |
| 272 | TIntermAggregate *aggrNode = rightNode->getAsAggregate(); |
| 273 | |
| 274 | for (TIntermSequence::iterator p = aggrNode->getSequence().begin(); |
| 275 | p != aggrNode->getSequence().end(); p++) { |
| 276 | int value = (*p)->getAsTyped()->getAsConstantUnion()->getUnionArrayPointer()->getIConst(); |
| 277 | offset[value]++; |
| 278 | if (offset[value] > 1) { |
| 279 | error(line, " l-value of swizzle cannot have duplicate components", op, "", ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 280 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | } |
| 284 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 285 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 286 | return errorReturn; |
| 287 | default: |
| 288 | break; |
| 289 | } |
| 290 | error(line, " l-value required", op, "", ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 291 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 292 | return true; |
| 293 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 294 | |
| 295 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 296 | const char* symbol = 0; |
| 297 | if (symNode != 0) |
| 298 | symbol = symNode->getSymbol().c_str(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 299 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 300 | const char* message = 0; |
| 301 | switch (node->getQualifier()) { |
| 302 | case EvqConst: message = "can't modify a const"; break; |
| 303 | case EvqConstReadOnly: message = "can't modify a const"; break; |
| 304 | case EvqAttribute: message = "can't modify an attribute"; break; |
| 305 | case EvqUniform: message = "can't modify a uniform"; break; |
| 306 | case EvqVaryingIn: message = "can't modify a varying"; break; |
| 307 | case EvqInput: message = "can't modify an input"; break; |
| 308 | case EvqFragCoord: message = "can't modify gl_FragCoord"; break; |
| 309 | case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break; |
| 310 | case EvqPointCoord: message = "can't modify gl_PointCoord"; break; |
| 311 | default: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 312 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 313 | // |
| 314 | // Type that can't be written to? |
| 315 | // |
| 316 | switch (node->getBasicType()) { |
| 317 | case EbtSampler2D: |
| 318 | case EbtSamplerCube: |
| 319 | message = "can't modify a sampler"; |
| 320 | break; |
| 321 | case EbtVoid: |
| 322 | message = "can't modify void"; |
| 323 | break; |
| 324 | default: |
| 325 | break; |
| 326 | } |
| 327 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 328 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 329 | if (message == 0 && binaryNode == 0 && symNode == 0) { |
| 330 | error(line, " l-value required", op, "", ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 331 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 332 | return true; |
| 333 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 334 | |
| 335 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 336 | // |
| 337 | // Everything else is okay, no error. |
| 338 | // |
| 339 | if (message == 0) |
| 340 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 341 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 342 | // |
| 343 | // If we get here, we have an error and a message. |
| 344 | // |
| 345 | if (symNode) |
| 346 | error(line, " l-value required", op, "\"%s\" (%s)", symbol, message); |
| 347 | else |
| 348 | error(line, " l-value required", op, "(%s)", message); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 349 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 350 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // |
| 354 | // Both test, and if necessary spit out an error, to see if the node is really |
| 355 | // a constant. |
| 356 | // |
| 357 | // Returns true if the was an error. |
| 358 | // |
| 359 | bool TParseContext::constErrorCheck(TIntermTyped* node) |
| 360 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 361 | if (node->getQualifier() == EvqConst) |
| 362 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 363 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 364 | error(node->getLine(), "constant expression required", "", ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 365 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 366 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | // |
| 370 | // Both test, and if necessary spit out an error, to see if the node is really |
| 371 | // an integer. |
| 372 | // |
| 373 | // Returns true if the was an error. |
| 374 | // |
| 375 | bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token) |
| 376 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 377 | if (node->getBasicType() == EbtInt && node->getNominalSize() == 1) |
| 378 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 379 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 380 | error(node->getLine(), "integer expression required", token, ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 381 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 382 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // |
| 386 | // Both test, and if necessary spit out an error, to see if we are currently |
| 387 | // globally scoped. |
| 388 | // |
| 389 | // Returns true if the was an error. |
| 390 | // |
| 391 | bool TParseContext::globalErrorCheck(int line, bool global, const char* token) |
| 392 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 393 | if (global) |
| 394 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 395 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 396 | error(line, "only allowed at global scope", token, ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 397 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 398 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | // |
| 402 | // For now, keep it simple: if it starts "gl_", it's reserved, independent |
| 403 | // of scope. Except, if the symbol table is at the built-in push-level, |
| 404 | // which is when we are parsing built-ins. |
alokp@chromium.org | 613ef31 | 2010-07-21 18:54:22 +0000 | [diff] [blame] | 405 | // Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a |
| 406 | // webgl shader. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 407 | // |
| 408 | // Returns true if there was an error. |
| 409 | // |
| 410 | bool TParseContext::reservedErrorCheck(int line, const TString& identifier) |
| 411 | { |
alokp@chromium.org | 613ef31 | 2010-07-21 18:54:22 +0000 | [diff] [blame] | 412 | static const char* reservedErrMsg = "reserved built-in name"; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 413 | if (!symbolTable.atBuiltInLevel()) { |
| 414 | if (identifier.substr(0, 3) == TString("gl_")) { |
alokp@chromium.org | 613ef31 | 2010-07-21 18:54:22 +0000 | [diff] [blame] | 415 | error(line, reservedErrMsg, "gl_", ""); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 416 | return true; |
| 417 | } |
alokp@chromium.org | 613ef31 | 2010-07-21 18:54:22 +0000 | [diff] [blame] | 418 | if (spec == EShSpecWebGL) { |
| 419 | if (identifier.substr(0, 6) == TString("webgl_")) { |
| 420 | error(line, reservedErrMsg, "webgl_", ""); |
| 421 | return true; |
| 422 | } |
| 423 | if (identifier.substr(0, 7) == TString("_webgl_")) { |
| 424 | error(line, reservedErrMsg, "_webgl_", ""); |
| 425 | return true; |
| 426 | } |
| 427 | } |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 428 | if (identifier.find("__") != TString::npos) { |
| 429 | //error(line, "Two consecutive underscores are reserved for future use.", identifier.c_str(), "", ""); |
| 430 | //return true; |
| 431 | infoSink.info.message(EPrefixWarning, "Two consecutive underscores are reserved for future use.", line); |
| 432 | return false; |
| 433 | } |
| 434 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 435 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 436 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | // |
| 440 | // Make sure there is enough data provided to the constructor to build |
| 441 | // something of the type of the constructor. Also returns the type of |
| 442 | // the constructor. |
| 443 | // |
| 444 | // Returns true if there was an error in construction. |
| 445 | // |
| 446 | bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type) |
| 447 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 448 | *type = function.getReturnType(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 449 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 450 | bool constructingMatrix = false; |
| 451 | switch(op) { |
| 452 | case EOpConstructMat2: |
| 453 | case EOpConstructMat3: |
| 454 | case EOpConstructMat4: |
| 455 | constructingMatrix = true; |
| 456 | break; |
| 457 | default: |
| 458 | break; |
| 459 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 460 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 461 | // |
| 462 | // Note: It's okay to have too many components available, but not okay to have unused |
| 463 | // arguments. 'full' will go to true when enough args have been seen. If we loop |
| 464 | // again, there is an extra argument, so 'overfull' will become true. |
| 465 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 466 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 467 | int size = 0; |
| 468 | bool constType = true; |
| 469 | bool full = false; |
| 470 | bool overFull = false; |
| 471 | bool matrixInMatrix = false; |
| 472 | bool arrayArg = false; |
| 473 | for (int i = 0; i < function.getParamCount(); ++i) { |
alokp@chromium.org | b19403a | 2010-09-08 17:56:26 +0000 | [diff] [blame] | 474 | const TParameter& param = function.getParam(i); |
| 475 | size += param.type->getObjectSize(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 476 | |
alokp@chromium.org | b19403a | 2010-09-08 17:56:26 +0000 | [diff] [blame] | 477 | if (constructingMatrix && param.type->isMatrix()) |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 478 | matrixInMatrix = true; |
| 479 | if (full) |
| 480 | overFull = true; |
| 481 | if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize()) |
| 482 | full = true; |
alokp@chromium.org | b19403a | 2010-09-08 17:56:26 +0000 | [diff] [blame] | 483 | if (param.type->getQualifier() != EvqConst) |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 484 | constType = false; |
alokp@chromium.org | b19403a | 2010-09-08 17:56:26 +0000 | [diff] [blame] | 485 | if (param.type->isArray()) |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 486 | arrayArg = true; |
| 487 | } |
| 488 | |
| 489 | if (constType) |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 490 | type->setQualifier(EvqConst); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 491 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 492 | if (type->isArray() && type->getArraySize() != function.getParamCount()) { |
| 493 | error(line, "array constructor needs one argument per array element", "constructor", ""); |
| 494 | return true; |
| 495 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 496 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 497 | if (arrayArg && op != EOpConstructStruct) { |
| 498 | error(line, "constructing from a non-dereferenced array", "constructor", ""); |
| 499 | return true; |
| 500 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 501 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 502 | if (matrixInMatrix && !type->isArray()) { |
daniel@transgaming.com | bef0b6d | 2010-04-29 03:32:39 +0000 | [diff] [blame] | 503 | if (function.getParamCount() != 1) { |
| 504 | error(line, "constructing matrix from matrix can only take one argument", "constructor", ""); |
| 505 | return true; |
| 506 | } |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 507 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 508 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 509 | if (overFull) { |
| 510 | error(line, "too many arguments", "constructor", ""); |
| 511 | return true; |
| 512 | } |
| 513 | |
| 514 | if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->size() != function.getParamCount()) { |
| 515 | error(line, "Number of constructor parameters does not match the number of structure fields", "constructor", ""); |
| 516 | return true; |
| 517 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 518 | |
daniel@transgaming.com | bef0b6d | 2010-04-29 03:32:39 +0000 | [diff] [blame] | 519 | if (!type->isMatrix()) { |
| 520 | if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) || |
| 521 | (op == EOpConstructStruct && size < type->getObjectSize())) { |
| 522 | error(line, "not enough data provided for construction", "constructor", ""); |
| 523 | return true; |
| 524 | } |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 525 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 526 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 527 | TIntermTyped* typed = node->getAsTyped(); |
| 528 | if (typed == 0) { |
| 529 | error(line, "constructor argument does not have a type", "constructor", ""); |
| 530 | return true; |
| 531 | } |
| 532 | if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) { |
| 533 | error(line, "cannot convert a sampler", "constructor", ""); |
| 534 | return true; |
| 535 | } |
| 536 | if (typed->getBasicType() == EbtVoid) { |
| 537 | error(line, "cannot convert a void", "constructor", ""); |
| 538 | return true; |
| 539 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 540 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 541 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | // This function checks to see if a void variable has been declared and raise an error message for such a case |
| 545 | // |
| 546 | // returns true in case of an error |
| 547 | // |
| 548 | bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType) |
| 549 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 550 | if (pubType.type == EbtVoid) { |
| 551 | error(line, "illegal use of type 'void'", identifier.c_str(), ""); |
| 552 | return true; |
| 553 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 554 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 555 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // This function checks to see if the node (for the expression) contains a scalar boolean expression or not |
| 559 | // |
| 560 | // returns true in case of an error |
| 561 | // |
| 562 | bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type) |
| 563 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 564 | if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) { |
| 565 | error(line, "boolean expression expected", "", ""); |
| 566 | return true; |
| 567 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 568 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 569 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // This function checks to see if the node (for the expression) contains a scalar boolean expression or not |
| 573 | // |
| 574 | // returns true in case of an error |
| 575 | // |
| 576 | bool TParseContext::boolErrorCheck(int line, const TPublicType& pType) |
| 577 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 578 | if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) { |
| 579 | error(line, "boolean expression expected", "", ""); |
| 580 | return true; |
| 581 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 582 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 583 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason) |
| 587 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 588 | if (pType.type == EbtStruct) { |
| 589 | if (containsSampler(*pType.userDef)) { |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 590 | error(line, reason, getBasicString(pType.type), "(structure contains a sampler)"); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 591 | |
| 592 | return true; |
| 593 | } |
| 594 | |
| 595 | return false; |
| 596 | } else if (IsSampler(pType.type)) { |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 597 | error(line, reason, getBasicString(pType.type), ""); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 598 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 599 | return true; |
| 600 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 601 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 602 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType) |
| 606 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 607 | if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) && |
| 608 | pType.type == EbtStruct) { |
| 609 | error(line, "cannot be used with a structure", getQualifierString(pType.qualifier), ""); |
| 610 | |
| 611 | return true; |
| 612 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 613 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 614 | if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform")) |
| 615 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 616 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 617 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type) |
| 621 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 622 | if ((qualifier == EvqOut || qualifier == EvqInOut) && |
| 623 | type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) { |
| 624 | error(line, "samplers cannot be output parameters", type.getBasicString(), ""); |
| 625 | return true; |
| 626 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 627 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 628 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | bool TParseContext::containsSampler(TType& type) |
| 632 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 633 | if (IsSampler(type.getBasicType())) |
| 634 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 635 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 636 | if (type.getBasicType() == EbtStruct) { |
| 637 | TTypeList& structure = *type.getStruct(); |
| 638 | for (unsigned int i = 0; i < structure.size(); ++i) { |
| 639 | if (containsSampler(*structure[i].type)) |
| 640 | return true; |
| 641 | } |
| 642 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 643 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 644 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | // |
| 648 | // Do size checking for an array type's size. |
| 649 | // |
| 650 | // Returns true if there was an error. |
| 651 | // |
| 652 | bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size) |
| 653 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 654 | TIntermConstantUnion* constant = expr->getAsConstantUnion(); |
| 655 | if (constant == 0 || constant->getBasicType() != EbtInt) { |
| 656 | error(line, "array size must be a constant integer expression", "", ""); |
| 657 | return true; |
| 658 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 659 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 660 | size = constant->getUnionArrayPointer()->getIConst(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 661 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 662 | if (size <= 0) { |
| 663 | error(line, "array size must be a positive integer", "", ""); |
| 664 | size = 1; |
| 665 | return true; |
| 666 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 667 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 668 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | // |
| 672 | // See if this qualifier can be an array. |
| 673 | // |
| 674 | // Returns true if there is an error. |
| 675 | // |
| 676 | bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type) |
| 677 | { |
alokp@chromium.org | 8f0f24a | 2010-09-01 21:06:24 +0000 | [diff] [blame] | 678 | if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 679 | error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str(), ""); |
| 680 | return true; |
| 681 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 682 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 683 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | // |
| 687 | // See if this type can be an array. |
| 688 | // |
| 689 | // Returns true if there is an error. |
| 690 | // |
| 691 | bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type) |
| 692 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 693 | // |
| 694 | // Can the type be an array? |
| 695 | // |
| 696 | if (type.array) { |
| 697 | error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str(), ""); |
| 698 | return true; |
| 699 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 700 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 701 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | // |
| 705 | // Do all the semantic checking for declaring an array, with and |
| 706 | // without a size, and make the right changes to the symbol table. |
| 707 | // |
| 708 | // size == 0 means no specified size. |
| 709 | // |
| 710 | // Returns true if there was an error. |
| 711 | // |
| 712 | bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable) |
| 713 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 714 | // |
| 715 | // Don't check for reserved word use until after we know it's not in the symbol table, |
| 716 | // because reserved arrays can be redeclared. |
| 717 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 718 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 719 | bool builtIn = false; |
| 720 | bool sameScope = false; |
| 721 | TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope); |
| 722 | if (symbol == 0 || !sameScope) { |
| 723 | if (reservedErrorCheck(line, identifier)) |
| 724 | return true; |
| 725 | |
| 726 | variable = new TVariable(&identifier, TType(type)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 727 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 728 | if (type.arraySize) |
| 729 | variable->getType().setArraySize(type.arraySize); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 730 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 731 | if (! symbolTable.insert(*variable)) { |
| 732 | delete variable; |
| 733 | error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str(), ""); |
| 734 | return true; |
| 735 | } |
| 736 | } else { |
| 737 | if (! symbol->isVariable()) { |
| 738 | error(line, "variable expected", identifier.c_str(), ""); |
| 739 | return true; |
| 740 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 741 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 742 | variable = static_cast<TVariable*>(symbol); |
| 743 | if (! variable->getType().isArray()) { |
| 744 | error(line, "redeclaring non-array as array", identifier.c_str(), ""); |
| 745 | return true; |
| 746 | } |
| 747 | if (variable->getType().getArraySize() > 0) { |
| 748 | error(line, "redeclaration of array with size", identifier.c_str(), ""); |
| 749 | return true; |
| 750 | } |
| 751 | |
| 752 | if (! variable->getType().sameElementType(TType(type))) { |
| 753 | error(line, "redeclaration of array with a different type", identifier.c_str(), ""); |
| 754 | return true; |
| 755 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 756 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 757 | TType* t = variable->getArrayInformationType(); |
| 758 | while (t != 0) { |
| 759 | if (t->getMaxArraySize() > type.arraySize) { |
| 760 | error(line, "higher index value already used for the array", identifier.c_str(), ""); |
| 761 | return true; |
| 762 | } |
| 763 | t->setArraySize(type.arraySize); |
| 764 | t = t->getArrayInformationType(); |
| 765 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 766 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 767 | if (type.arraySize) |
| 768 | variable->getType().setArraySize(type.arraySize); |
| 769 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 770 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 771 | if (voidErrorCheck(line, identifier, type)) |
| 772 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 773 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 774 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line) |
| 778 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 779 | bool builtIn = false; |
| 780 | TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn); |
| 781 | if (symbol == 0) { |
| 782 | error(line, " undeclared identifier", node->getSymbol().c_str(), ""); |
| 783 | return true; |
| 784 | } |
| 785 | TVariable* variable = static_cast<TVariable*>(symbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 786 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 787 | type->setArrayInformationType(variable->getArrayInformationType()); |
| 788 | variable->updateArrayInformationType(type); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 789 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 790 | // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers |
| 791 | // its an error |
| 792 | if (node->getSymbol() == "gl_FragData") { |
| 793 | TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn); |
| 794 | if (fragData == 0) { |
| 795 | infoSink.info.message(EPrefixInternalError, "gl_MaxDrawBuffers not defined", line); |
| 796 | return true; |
| 797 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 798 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 799 | int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst(); |
| 800 | if (fragDataValue <= size) { |
| 801 | error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers", ""); |
| 802 | return true; |
| 803 | } |
| 804 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 805 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 806 | // we dont want to update the maxArraySize when this flag is not set, we just want to include this |
| 807 | // node type in the chain of node types so that its updated when a higher maxArraySize comes in. |
| 808 | if (!updateFlag) |
| 809 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 810 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 811 | size++; |
| 812 | variable->getType().setMaxArraySize(size); |
| 813 | type->setMaxArraySize(size); |
| 814 | TType* tt = type; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 815 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 816 | while(tt->getArrayInformationType() != 0) { |
| 817 | tt = tt->getArrayInformationType(); |
| 818 | tt->setMaxArraySize(size); |
| 819 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 820 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 821 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | // |
| 825 | // Enforce non-initializer type/qualifier rules. |
| 826 | // |
| 827 | // Returns true if there was an error. |
| 828 | // |
| 829 | bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type) |
| 830 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 831 | // |
| 832 | // Make the qualifier make sense. |
| 833 | // |
| 834 | if (type.qualifier == EvqConst) { |
| 835 | type.qualifier = EvqTemporary; |
| 836 | error(line, "variables with qualifier 'const' must be initialized", identifier.c_str(), ""); |
| 837 | return true; |
| 838 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 839 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 840 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | // |
| 844 | // Do semantic checking for a variable declaration that has no initializer, |
| 845 | // and update the symbol table. |
| 846 | // |
| 847 | // Returns true if there was an error. |
| 848 | // |
| 849 | bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type) |
| 850 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 851 | if (reservedErrorCheck(line, identifier)) |
| 852 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 853 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 854 | TVariable* variable = new TVariable(&identifier, TType(type)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 855 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 856 | if (! symbolTable.insert(*variable)) { |
| 857 | error(line, "redefinition", variable->getName().c_str(), ""); |
| 858 | delete variable; |
| 859 | return true; |
| 860 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 861 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 862 | if (voidErrorCheck(line, identifier, type)) |
| 863 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 864 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 865 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type) |
| 869 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 870 | if (qualifier != EvqConst && qualifier != EvqTemporary) { |
| 871 | error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier), ""); |
| 872 | return true; |
| 873 | } |
| 874 | if (qualifier == EvqConst && paramQualifier != EvqIn) { |
| 875 | error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier)); |
| 876 | return true; |
| 877 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 878 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 879 | if (qualifier == EvqConst) |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 880 | type->setQualifier(EvqConstReadOnly); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 881 | else |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 882 | type->setQualifier(paramQualifier); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 883 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 884 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 885 | } |
| 886 | |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 887 | bool TParseContext::extensionErrorCheck(int line, const TString& extension) |
| 888 | { |
| 889 | TExtensionBehavior::const_iterator iter = extensionBehavior.find(extension); |
| 890 | if (iter == extensionBehavior.end()) { |
| 891 | error(line, "extension", extension.c_str(), "is not supported"); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 892 | return true; |
| 893 | } |
alokp@chromium.org | 8815d7f | 2010-09-09 17:30:03 +0000 | [diff] [blame] | 894 | if (iter->second == EBhDisable) { |
| 895 | error(line, "extension", extension.c_str(), "is disabled"); |
| 896 | return true; |
| 897 | } |
| 898 | if (iter->second == EBhWarn) { |
| 899 | TString msg = "extension " + extension + " is being used"; |
| 900 | infoSink.info.message(EPrefixWarning, msg.c_str(), line); |
| 901 | return false; |
| 902 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 903 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 904 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | ///////////////////////////////////////////////////////////////////////////////// |
| 908 | // |
| 909 | // Non-Errors. |
| 910 | // |
| 911 | ///////////////////////////////////////////////////////////////////////////////// |
| 912 | |
| 913 | // |
| 914 | // Look up a function name in the symbol table, and make sure it is a function. |
| 915 | // |
| 916 | // Return the function symbol if found, otherwise 0. |
| 917 | // |
| 918 | const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn) |
| 919 | { |
alokp@chromium.org | 0a57618 | 2010-08-09 17:16:27 +0000 | [diff] [blame] | 920 | // First find by unmangled name to check whether the function name has been |
| 921 | // hidden by a variable name or struct typename. |
| 922 | const TSymbol* symbol = symbolTable.find(call->getName(), builtIn); |
| 923 | if (symbol == 0) { |
| 924 | symbol = symbolTable.find(call->getMangledName(), builtIn); |
| 925 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 926 | |
alokp@chromium.org | 0a57618 | 2010-08-09 17:16:27 +0000 | [diff] [blame] | 927 | if (symbol == 0) { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 928 | error(line, "no matching overloaded function found", call->getName().c_str(), ""); |
| 929 | return 0; |
| 930 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 931 | |
alokp@chromium.org | 0a57618 | 2010-08-09 17:16:27 +0000 | [diff] [blame] | 932 | if (!symbol->isFunction()) { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 933 | error(line, "function name expected", call->getName().c_str(), ""); |
| 934 | return 0; |
| 935 | } |
alokp@chromium.org | 0a57618 | 2010-08-09 17:16:27 +0000 | [diff] [blame] | 936 | |
| 937 | return static_cast<const TFunction*>(symbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 938 | } |
| 939 | |
| 940 | // |
| 941 | // Initializers show up in several places in the grammar. Have one set of |
| 942 | // code to handle them here. |
| 943 | // |
| 944 | bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType, |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 945 | TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 946 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 947 | TType type = TType(pType); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 948 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 949 | if (variable == 0) { |
| 950 | if (reservedErrorCheck(line, identifier)) |
| 951 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 952 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 953 | if (voidErrorCheck(line, identifier, pType)) |
| 954 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 955 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 956 | // |
| 957 | // add variable to symbol table |
| 958 | // |
| 959 | variable = new TVariable(&identifier, type); |
| 960 | if (! symbolTable.insert(*variable)) { |
| 961 | error(line, "redefinition", variable->getName().c_str(), ""); |
| 962 | return true; |
| 963 | // don't delete variable, it's used by error recovery, and the pool |
| 964 | // pop will take care of the memory |
| 965 | } |
| 966 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 967 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 968 | // |
| 969 | // identifier must be of type constant, a global, or a temporary |
| 970 | // |
| 971 | TQualifier qualifier = variable->getType().getQualifier(); |
| 972 | if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) { |
| 973 | error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString(), ""); |
| 974 | return true; |
| 975 | } |
| 976 | // |
| 977 | // test for and propagate constant |
| 978 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 979 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 980 | if (qualifier == EvqConst) { |
| 981 | if (qualifier != initializer->getType().getQualifier()) { |
| 982 | error(line, " assigning non-constant to", "=", "'%s'", variable->getType().getCompleteString().c_str()); |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 983 | variable->getType().setQualifier(EvqTemporary); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 984 | return true; |
| 985 | } |
| 986 | if (type != initializer->getType()) { |
| 987 | error(line, " non-matching types for const initializer ", |
| 988 | variable->getType().getQualifierString(), ""); |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 989 | variable->getType().setQualifier(EvqTemporary); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 990 | return true; |
| 991 | } |
| 992 | if (initializer->getAsConstantUnion()) { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 993 | ConstantUnion* unionArray = variable->getConstPointer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 994 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 995 | if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) { |
| 996 | *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0]; |
| 997 | } else { |
| 998 | variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer()); |
| 999 | } |
| 1000 | } else if (initializer->getAsSymbolNode()) { |
| 1001 | const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol()); |
| 1002 | const TVariable* tVar = static_cast<const TVariable*>(symbol); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1003 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1004 | ConstantUnion* constArray = tVar->getConstPointer(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1005 | variable->shareConstPointer(constArray); |
| 1006 | } else { |
| 1007 | error(line, " cannot assign to", "=", "'%s'", variable->getType().getCompleteString().c_str()); |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 1008 | variable->getType().setQualifier(EvqTemporary); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1009 | return true; |
| 1010 | } |
| 1011 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1012 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1013 | if (qualifier != EvqConst) { |
| 1014 | TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line); |
| 1015 | intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line); |
| 1016 | if (intermNode == 0) { |
| 1017 | assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString()); |
| 1018 | return true; |
| 1019 | } |
| 1020 | } else |
| 1021 | intermNode = 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1022 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1023 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode) |
| 1027 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1028 | if (!aggrNode->isConstructor()) |
| 1029 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1030 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1031 | bool allConstant = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1032 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1033 | // check if all the child nodes are constants so that they can be inserted into |
| 1034 | // the parent node |
| 1035 | if (aggrNode) { |
| 1036 | TIntermSequence &childSequenceVector = aggrNode->getSequence() ; |
| 1037 | for (TIntermSequence::iterator p = childSequenceVector.begin(); |
| 1038 | p != childSequenceVector.end(); p++) { |
| 1039 | if (!(*p)->getAsTyped()->getAsConstantUnion()) |
| 1040 | return false; |
| 1041 | } |
| 1042 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1043 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1044 | return allConstant; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | // This function is used to test for the correctness of the parameters passed to various constructor functions |
| 1048 | // and also convert them to the right datatype if it is allowed and required. |
| 1049 | // |
| 1050 | // Returns 0 for an error or the constructed node (aggregate or typed) for no error. |
| 1051 | // |
| 1052 | TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line) |
| 1053 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1054 | if (node == 0) |
| 1055 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1056 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1057 | TIntermAggregate* aggrNode = node->getAsAggregate(); |
| 1058 | |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 1059 | TTypeList::const_iterator memberTypes; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1060 | if (op == EOpConstructStruct) |
| 1061 | memberTypes = type->getStruct()->begin(); |
| 1062 | |
| 1063 | TType elementType = *type; |
| 1064 | if (type->isArray()) |
| 1065 | elementType.clearArrayness(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1066 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1067 | bool singleArg; |
| 1068 | if (aggrNode) { |
| 1069 | if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1) |
| 1070 | singleArg = true; |
| 1071 | else |
| 1072 | singleArg = false; |
| 1073 | } else |
| 1074 | singleArg = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1075 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1076 | TIntermTyped *newNode; |
| 1077 | if (singleArg) { |
| 1078 | // If structure constructor or array constructor is being called |
| 1079 | // for only one parameter inside the structure, we need to call constructStruct function once. |
| 1080 | if (type->isArray()) |
| 1081 | newNode = constructStruct(node, &elementType, 1, node->getLine(), false); |
| 1082 | else if (op == EOpConstructStruct) |
| 1083 | newNode = constructStruct(node, (*memberTypes).type, 1, node->getLine(), false); |
| 1084 | else |
| 1085 | newNode = constructBuiltIn(type, op, node, node->getLine(), false); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1086 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1087 | if (newNode && newNode->getAsAggregate()) { |
| 1088 | TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type); |
| 1089 | if (constConstructor) |
| 1090 | return constConstructor; |
| 1091 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1092 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1093 | return newNode; |
| 1094 | } |
| 1095 | |
| 1096 | // |
| 1097 | // Handle list of arguments. |
| 1098 | // |
| 1099 | TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor |
| 1100 | // if the structure constructor contains more than one parameter, then construct |
| 1101 | // each parameter |
| 1102 | |
| 1103 | int paramCount = 0; // keeps a track of the constructor parameter number being checked |
| 1104 | |
| 1105 | // for each parameter to the constructor call, check to see if the right type is passed or convert them |
| 1106 | // to the right type if possible (and allowed). |
| 1107 | // for structure constructors, just check if the right type is passed, no conversion is allowed. |
| 1108 | |
| 1109 | for (TIntermSequence::iterator p = sequenceVector.begin(); |
| 1110 | p != sequenceVector.end(); p++, paramCount++) { |
| 1111 | if (type->isArray()) |
| 1112 | newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true); |
| 1113 | else if (op == EOpConstructStruct) |
| 1114 | newNode = constructStruct(*p, (memberTypes[paramCount]).type, paramCount+1, node->getLine(), true); |
| 1115 | else |
| 1116 | newNode = constructBuiltIn(type, op, *p, node->getLine(), true); |
| 1117 | |
| 1118 | if (newNode) { |
| 1119 | *p = newNode; |
| 1120 | } |
| 1121 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1122 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1123 | TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line); |
| 1124 | TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type); |
| 1125 | if (constConstructor) |
| 1126 | return constConstructor; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1127 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1128 | return constructor; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type) |
| 1132 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1133 | bool canBeFolded = areAllChildConst(aggrNode); |
| 1134 | aggrNode->setType(type); |
| 1135 | if (canBeFolded) { |
| 1136 | bool returnVal = false; |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1137 | ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()]; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1138 | if (aggrNode->getSequence().size() == 1) { |
| 1139 | returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true); |
| 1140 | } |
| 1141 | else { |
| 1142 | returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type); |
| 1143 | } |
| 1144 | if (returnVal) |
| 1145 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1146 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1147 | return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine()); |
| 1148 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1149 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1150 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | // Function for constructor implementation. Calls addUnaryMath with appropriate EOp value |
| 1154 | // for the parameter to the constructor (passed to this function). Essentially, it converts |
| 1155 | // the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a |
| 1156 | // float, then float is converted to int. |
| 1157 | // |
| 1158 | // Returns 0 for an error or the constructed node. |
| 1159 | // |
| 1160 | TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, TSourceLoc line, bool subset) |
| 1161 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1162 | TIntermTyped* newNode; |
| 1163 | TOperator basicOp; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1164 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1165 | // |
| 1166 | // First, convert types as needed. |
| 1167 | // |
| 1168 | switch (op) { |
| 1169 | case EOpConstructVec2: |
| 1170 | case EOpConstructVec3: |
| 1171 | case EOpConstructVec4: |
| 1172 | case EOpConstructMat2: |
| 1173 | case EOpConstructMat3: |
| 1174 | case EOpConstructMat4: |
| 1175 | case EOpConstructFloat: |
| 1176 | basicOp = EOpConstructFloat; |
| 1177 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1178 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1179 | case EOpConstructIVec2: |
| 1180 | case EOpConstructIVec3: |
| 1181 | case EOpConstructIVec4: |
| 1182 | case EOpConstructInt: |
| 1183 | basicOp = EOpConstructInt; |
| 1184 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1185 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1186 | case EOpConstructBVec2: |
| 1187 | case EOpConstructBVec3: |
| 1188 | case EOpConstructBVec4: |
| 1189 | case EOpConstructBool: |
| 1190 | basicOp = EOpConstructBool; |
| 1191 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1192 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1193 | default: |
| 1194 | error(line, "unsupported construction", "", ""); |
| 1195 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1196 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1197 | return 0; |
| 1198 | } |
| 1199 | newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable); |
| 1200 | if (newNode == 0) { |
| 1201 | error(line, "can't convert", "constructor", ""); |
| 1202 | return 0; |
| 1203 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1204 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1205 | // |
| 1206 | // Now, if there still isn't an operation to do the construction, and we need one, add one. |
| 1207 | // |
| 1208 | |
| 1209 | // Otherwise, skip out early. |
| 1210 | if (subset || (newNode != node && newNode->getType() == *type)) |
| 1211 | return newNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1212 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1213 | // setAggregateOperator will insert a new node for the constructor, as needed. |
| 1214 | return intermediate.setAggregateOperator(newNode, op, line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | // This function tests for the type of the parameters to the structures constructors. Raises |
| 1218 | // an error message if the expected type does not match the parameter passed to the constructor. |
| 1219 | // |
| 1220 | // Returns 0 for an error or the input node itself if the expected and the given parameter types match. |
| 1221 | // |
| 1222 | TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, TSourceLoc line, bool subset) |
| 1223 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1224 | if (*type == node->getAsTyped()->getType()) { |
| 1225 | if (subset) |
| 1226 | return node->getAsTyped(); |
| 1227 | else |
| 1228 | return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line); |
| 1229 | } else { |
| 1230 | error(line, "", "constructor", "cannot convert parameter %d from '%s' to '%s'", paramCount, |
| 1231 | node->getAsTyped()->getType().getBasicString(), type->getBasicString()); |
| 1232 | recover(); |
| 1233 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1234 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1235 | return 0; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | // |
| 1239 | // This function returns the tree representation for the vector field(s) being accessed from contant vector. |
| 1240 | // If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is |
| 1241 | // returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol |
| 1242 | // node or it could be the intermediate tree representation of accessing fields in a constant structure or column of |
| 1243 | // a constant matrix. |
| 1244 | // |
| 1245 | TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line) |
| 1246 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1247 | TIntermTyped* typedNode; |
| 1248 | TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1249 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1250 | ConstantUnion *unionArray; |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1251 | if (tempConstantNode) { |
| 1252 | unionArray = tempConstantNode->getUnionArrayPointer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1253 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1254 | if (!unionArray) { // this error message should never be raised |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1255 | infoSink.info.message(EPrefixInternalError, "ConstantUnion not initialized in addConstVectorNode function", line); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1256 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1257 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1258 | return node; |
| 1259 | } |
| 1260 | } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error |
| 1261 | error(line, "Cannot offset into the vector", "Error", ""); |
| 1262 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1263 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1266 | |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1267 | ConstantUnion* constArray = new ConstantUnion[fields.num]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1268 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1269 | for (int i = 0; i < fields.num; i++) { |
| 1270 | if (fields.offsets[i] >= node->getType().getObjectSize()) { |
| 1271 | error(line, "", "[", "vector field selection out of range '%d'", fields.offsets[i]); |
| 1272 | recover(); |
| 1273 | fields.offsets[i] = 0; |
| 1274 | } |
| 1275 | |
| 1276 | constArray[i] = unionArray[fields.offsets[i]]; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1277 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1278 | } |
| 1279 | typedNode = intermediate.addConstantUnion(constArray, node->getType(), line); |
| 1280 | return typedNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | // |
| 1284 | // This function returns the column being accessed from a constant matrix. The values are retrieved from |
| 1285 | // the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input |
| 1286 | // to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a |
| 1287 | // constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure) |
| 1288 | // |
| 1289 | TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line) |
| 1290 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1291 | TIntermTyped* typedNode; |
| 1292 | TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1293 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1294 | if (index >= node->getType().getNominalSize()) { |
| 1295 | error(line, "", "[", "matrix field selection out of range '%d'", index); |
| 1296 | recover(); |
| 1297 | index = 0; |
| 1298 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1299 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1300 | if (tempConstantNode) { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1301 | ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1302 | int size = tempConstantNode->getType().getNominalSize(); |
| 1303 | typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line); |
| 1304 | } else { |
| 1305 | error(line, "Cannot offset into the matrix", "Error", ""); |
| 1306 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1307 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1308 | return 0; |
| 1309 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1310 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1311 | return typedNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | |
| 1315 | // |
| 1316 | // This function returns an element of an array accessed from a constant array. The values are retrieved from |
| 1317 | // the symbol table and parse-tree is built for the type of the element. The input |
| 1318 | // to the function could either be a symbol node (a[0] where a is a constant array)that represents a |
| 1319 | // constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure) |
| 1320 | // |
| 1321 | TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line) |
| 1322 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1323 | TIntermTyped* typedNode; |
| 1324 | TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); |
| 1325 | TType arrayElementType = node->getType(); |
| 1326 | arrayElementType.clearArrayness(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1327 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1328 | if (index >= node->getType().getArraySize()) { |
| 1329 | error(line, "", "[", "array field selection out of range '%d'", index); |
| 1330 | recover(); |
| 1331 | index = 0; |
| 1332 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1333 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1334 | int arrayElementSize = arrayElementType.getObjectSize(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1335 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1336 | if (tempConstantNode) { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1337 | ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1338 | typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line); |
| 1339 | } else { |
| 1340 | error(line, "Cannot offset into the array", "Error", ""); |
| 1341 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1342 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1343 | return 0; |
| 1344 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1345 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1346 | return typedNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | |
| 1350 | // |
| 1351 | // This function returns the value of a particular field inside a constant structure from the symbol table. |
| 1352 | // If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr |
| 1353 | // function and returns the parse-tree with the values of the embedded/nested struct. |
| 1354 | // |
| 1355 | TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line) |
| 1356 | { |
alokp@chromium.org | 58e5429 | 2010-08-24 21:40:03 +0000 | [diff] [blame] | 1357 | const TTypeList* fields = node->getType().getStruct(); |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1358 | TIntermTyped *typedNode; |
| 1359 | int instanceSize = 0; |
| 1360 | unsigned int index = 0; |
| 1361 | TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1362 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1363 | for ( index = 0; index < fields->size(); ++index) { |
| 1364 | if ((*fields)[index].type->getFieldName() == identifier) { |
| 1365 | break; |
| 1366 | } else { |
| 1367 | instanceSize += (*fields)[index].type->getObjectSize(); |
| 1368 | } |
| 1369 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1370 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1371 | if (tempConstantNode) { |
alokp@chromium.org | 6ff56fd | 2010-05-05 16:37:50 +0000 | [diff] [blame] | 1372 | ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1373 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1374 | typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function |
| 1375 | } else { |
| 1376 | error(line, "Cannot offset into the structure", "Error", ""); |
| 1377 | recover(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1378 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1379 | return 0; |
| 1380 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1381 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1382 | return typedNode; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1385 | OS_TLSIndex GlobalParseContextIndex = OS_INVALID_TLS_INDEX; |
| 1386 | |
| 1387 | bool InitializeParseContextIndex() |
| 1388 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1389 | if (GlobalParseContextIndex != OS_INVALID_TLS_INDEX) { |
| 1390 | assert(0 && "InitializeParseContextIndex(): Parse Context already initalised"); |
| 1391 | return false; |
| 1392 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1393 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1394 | // |
| 1395 | // Allocate a TLS index. |
| 1396 | // |
| 1397 | GlobalParseContextIndex = OS_AllocTLSIndex(); |
| 1398 | |
| 1399 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 1400 | assert(0 && "InitializeParseContextIndex(): Parse Context already initalised"); |
| 1401 | return false; |
| 1402 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1403 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1404 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1405 | } |
| 1406 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1407 | bool FreeParseContextIndex() |
| 1408 | { |
| 1409 | OS_TLSIndex tlsiIndex = GlobalParseContextIndex; |
| 1410 | |
| 1411 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 1412 | assert(0 && "FreeParseContextIndex(): Parse Context index not initalised"); |
| 1413 | return false; |
| 1414 | } |
| 1415 | |
| 1416 | GlobalParseContextIndex = OS_INVALID_TLS_INDEX; |
| 1417 | |
| 1418 | return OS_FreeTLSIndex(tlsiIndex); |
| 1419 | } |
| 1420 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1421 | bool InitializeGlobalParseContext() |
| 1422 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1423 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 1424 | assert(0 && "InitializeGlobalParseContext(): Parse Context index not initalised"); |
| 1425 | return false; |
| 1426 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1427 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1428 | TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex)); |
| 1429 | if (lpParseContext != 0) { |
| 1430 | assert(0 && "InitializeParseContextIndex(): Parse Context already initalised"); |
| 1431 | return false; |
| 1432 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1433 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1434 | TThreadParseContext *lpThreadData = new TThreadParseContext(); |
| 1435 | if (lpThreadData == 0) { |
| 1436 | assert(0 && "InitializeGlobalParseContext(): Unable to create thread parse context"); |
| 1437 | return false; |
| 1438 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1439 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1440 | lpThreadData->lpGlobalParseContext = 0; |
| 1441 | OS_SetTLSValue(GlobalParseContextIndex, lpThreadData); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1442 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1443 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1446 | bool FreeParseContext() |
| 1447 | { |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1448 | if (GlobalParseContextIndex == OS_INVALID_TLS_INDEX) { |
| 1449 | assert(0 && "FreeParseContext(): Parse Context index not initalised"); |
| 1450 | return false; |
| 1451 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1452 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1453 | TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex)); |
| 1454 | if (lpParseContext) |
| 1455 | delete lpParseContext; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1456 | |
daniel@transgaming.com | ea15b0e | 2010-04-29 03:32:36 +0000 | [diff] [blame] | 1457 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1460 | TParseContextPointer& GetGlobalParseContext() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1461 | { |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1462 | // |
| 1463 | // Minimal error checking for speed |
| 1464 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1465 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1466 | TThreadParseContext *lpParseContext = static_cast<TThreadParseContext *>(OS_GetTLSValue(GlobalParseContextIndex)); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1467 | |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1468 | return lpParseContext->lpGlobalParseContext; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1469 | } |
alokp@chromium.org | 34b99cd | 2010-07-27 18:37:55 +0000 | [diff] [blame] | 1470 | |