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