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