gfx/angle/src/compiler/ParseHelper.cpp

Wed, 31 Dec 2014 07:16:47 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:16:47 +0100
branch
TOR_BUG_9701
changeset 3
141e0f1194b1
permissions
-rw-r--r--

Revert simplistic fix pending revisit of Mozilla integration attempt.

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

mercurial