michael@0: // michael@0: // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: // michael@0: michael@0: #include "compiler/ParseHelper.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "compiler/glslang.h" michael@0: #include "compiler/preprocessor/SourceLocation.h" michael@0: michael@0: /////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // Sub- vector and matrix fields michael@0: // michael@0: //////////////////////////////////////////////////////////////////////// michael@0: michael@0: // michael@0: // Look at a '.' field selector string and change it into offsets michael@0: // for a vector. michael@0: // michael@0: bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line) michael@0: { michael@0: fields.num = (int) compString.size(); michael@0: if (fields.num > 4) { michael@0: error(line, "illegal vector field selection", compString.c_str()); michael@0: return false; michael@0: } michael@0: michael@0: enum { michael@0: exyzw, michael@0: ergba, michael@0: estpq michael@0: } fieldSet[4]; michael@0: michael@0: for (int i = 0; i < fields.num; ++i) { michael@0: switch (compString[i]) { michael@0: case 'x': michael@0: fields.offsets[i] = 0; michael@0: fieldSet[i] = exyzw; michael@0: break; michael@0: case 'r': michael@0: fields.offsets[i] = 0; michael@0: fieldSet[i] = ergba; michael@0: break; michael@0: case 's': michael@0: fields.offsets[i] = 0; michael@0: fieldSet[i] = estpq; michael@0: break; michael@0: case 'y': michael@0: fields.offsets[i] = 1; michael@0: fieldSet[i] = exyzw; michael@0: break; michael@0: case 'g': michael@0: fields.offsets[i] = 1; michael@0: fieldSet[i] = ergba; michael@0: break; michael@0: case 't': michael@0: fields.offsets[i] = 1; michael@0: fieldSet[i] = estpq; michael@0: break; michael@0: case 'z': michael@0: fields.offsets[i] = 2; michael@0: fieldSet[i] = exyzw; michael@0: break; michael@0: case 'b': michael@0: fields.offsets[i] = 2; michael@0: fieldSet[i] = ergba; michael@0: break; michael@0: case 'p': michael@0: fields.offsets[i] = 2; michael@0: fieldSet[i] = estpq; michael@0: break; michael@0: michael@0: case 'w': michael@0: fields.offsets[i] = 3; michael@0: fieldSet[i] = exyzw; michael@0: break; michael@0: case 'a': michael@0: fields.offsets[i] = 3; michael@0: fieldSet[i] = ergba; michael@0: break; michael@0: case 'q': michael@0: fields.offsets[i] = 3; michael@0: fieldSet[i] = estpq; michael@0: break; michael@0: default: michael@0: error(line, "illegal vector field selection", compString.c_str()); michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: for (int i = 0; i < fields.num; ++i) { michael@0: if (fields.offsets[i] >= vecSize) { michael@0: error(line, "vector field selection out of range", compString.c_str()); michael@0: return false; michael@0: } michael@0: michael@0: if (i > 0) { michael@0: if (fieldSet[i] != fieldSet[i-1]) { michael@0: error(line, "illegal - vector component fields not from the same set", compString.c_str()); michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // michael@0: // Look at a '.' field selector string and change it into offsets michael@0: // for a matrix. michael@0: // michael@0: bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, const TSourceLoc& line) michael@0: { michael@0: fields.wholeRow = false; michael@0: fields.wholeCol = false; michael@0: fields.row = -1; michael@0: fields.col = -1; michael@0: michael@0: if (compString.size() != 2) { michael@0: error(line, "illegal length of matrix field selection", compString.c_str()); michael@0: return false; michael@0: } michael@0: michael@0: if (compString[0] == '_') { michael@0: if (compString[1] < '0' || compString[1] > '3') { michael@0: error(line, "illegal matrix field selection", compString.c_str()); michael@0: return false; michael@0: } michael@0: fields.wholeCol = true; michael@0: fields.col = compString[1] - '0'; michael@0: } else if (compString[1] == '_') { michael@0: if (compString[0] < '0' || compString[0] > '3') { michael@0: error(line, "illegal matrix field selection", compString.c_str()); michael@0: return false; michael@0: } michael@0: fields.wholeRow = true; michael@0: fields.row = compString[0] - '0'; michael@0: } else { michael@0: if (compString[0] < '0' || compString[0] > '3' || michael@0: compString[1] < '0' || compString[1] > '3') { michael@0: error(line, "illegal matrix field selection", compString.c_str()); michael@0: return false; michael@0: } michael@0: fields.row = compString[0] - '0'; michael@0: fields.col = compString[1] - '0'; michael@0: } michael@0: michael@0: if (fields.row >= matSize || fields.col >= matSize) { michael@0: error(line, "matrix field selection out of range", compString.c_str()); michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // Errors michael@0: // michael@0: //////////////////////////////////////////////////////////////////////// michael@0: michael@0: // michael@0: // Track whether errors have occurred. michael@0: // michael@0: void TParseContext::recover() michael@0: { michael@0: } michael@0: michael@0: // michael@0: // Used by flex/bison to output all syntax and parsing errors. michael@0: // michael@0: void TParseContext::error(const TSourceLoc& loc, michael@0: const char* reason, const char* token, michael@0: const char* extraInfo) michael@0: { michael@0: pp::SourceLocation srcLoc; michael@0: srcLoc.file = loc.first_file; michael@0: srcLoc.line = loc.first_line; michael@0: diagnostics.writeInfo(pp::Diagnostics::ERROR, michael@0: srcLoc, reason, token, extraInfo); michael@0: michael@0: } michael@0: michael@0: void TParseContext::warning(const TSourceLoc& loc, michael@0: const char* reason, const char* token, michael@0: const char* extraInfo) { michael@0: pp::SourceLocation srcLoc; michael@0: srcLoc.file = loc.first_file; michael@0: srcLoc.line = loc.first_line; michael@0: diagnostics.writeInfo(pp::Diagnostics::WARNING, michael@0: srcLoc, reason, token, extraInfo); michael@0: } michael@0: michael@0: void TParseContext::trace(const char* str) michael@0: { michael@0: diagnostics.writeDebug(str); michael@0: } michael@0: michael@0: // michael@0: // Same error message for all places assignments don't work. michael@0: // michael@0: void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right) michael@0: { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, "", op, extraInfo.c_str()); michael@0: } michael@0: michael@0: // michael@0: // Same error message for all places unary operations don't work. michael@0: // michael@0: void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand) michael@0: { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand michael@0: << " (or there is no acceptable conversion)"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, " wrong operand type", op, extraInfo.c_str()); michael@0: } michael@0: michael@0: // michael@0: // Same error message for all binary operations don't work. michael@0: // michael@0: void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right) michael@0: { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left michael@0: << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, " wrong operand types ", op, extraInfo.c_str()); michael@0: } michael@0: michael@0: bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){ michael@0: if (!checksPrecisionErrors) michael@0: return false; michael@0: switch( type ){ michael@0: case EbtFloat: michael@0: if( precision == EbpUndefined ){ michael@0: error( line, "No precision specified for (float)", "" ); michael@0: return true; michael@0: } michael@0: break; michael@0: case EbtInt: michael@0: if( precision == EbpUndefined ){ michael@0: error( line, "No precision specified (int)", "" ); michael@0: return true; michael@0: } michael@0: break; michael@0: default: michael@0: return false; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // Both test and if necessary, spit out an error, to see if the node is really michael@0: // an l-value that can be operated on this way. michael@0: // michael@0: // Returns true if the was an error. michael@0: // michael@0: bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node) michael@0: { michael@0: TIntermSymbol* symNode = node->getAsSymbolNode(); michael@0: TIntermBinary* binaryNode = node->getAsBinaryNode(); michael@0: michael@0: if (binaryNode) { michael@0: bool errorReturn; michael@0: michael@0: switch(binaryNode->getOp()) { michael@0: case EOpIndexDirect: michael@0: case EOpIndexIndirect: michael@0: case EOpIndexDirectStruct: michael@0: return lValueErrorCheck(line, op, binaryNode->getLeft()); michael@0: case EOpVectorSwizzle: michael@0: errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft()); michael@0: if (!errorReturn) { michael@0: int offset[4] = {0,0,0,0}; michael@0: michael@0: TIntermTyped* rightNode = binaryNode->getRight(); michael@0: TIntermAggregate *aggrNode = rightNode->getAsAggregate(); michael@0: michael@0: for (TIntermSequence::iterator p = aggrNode->getSequence().begin(); michael@0: p != aggrNode->getSequence().end(); p++) { michael@0: int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0); michael@0: offset[value]++; michael@0: if (offset[value] > 1) { michael@0: error(line, " l-value of swizzle cannot have duplicate components", op); michael@0: michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return errorReturn; michael@0: default: michael@0: break; michael@0: } michael@0: error(line, " l-value required", op); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: const char* symbol = 0; michael@0: if (symNode != 0) michael@0: symbol = symNode->getSymbol().c_str(); michael@0: michael@0: const char* message = 0; michael@0: switch (node->getQualifier()) { michael@0: case EvqConst: message = "can't modify a const"; break; michael@0: case EvqConstReadOnly: message = "can't modify a const"; break; michael@0: case EvqAttribute: message = "can't modify an attribute"; break; michael@0: case EvqUniform: message = "can't modify a uniform"; break; michael@0: case EvqVaryingIn: message = "can't modify a varying"; break; michael@0: case EvqFragCoord: message = "can't modify gl_FragCoord"; break; michael@0: case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break; michael@0: case EvqPointCoord: message = "can't modify gl_PointCoord"; break; michael@0: default: michael@0: michael@0: // michael@0: // Type that can't be written to? michael@0: // michael@0: switch (node->getBasicType()) { michael@0: case EbtSampler2D: michael@0: case EbtSamplerCube: michael@0: message = "can't modify a sampler"; michael@0: break; michael@0: case EbtVoid: michael@0: message = "can't modify void"; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (message == 0 && binaryNode == 0 && symNode == 0) { michael@0: error(line, " l-value required", op); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: michael@0: // michael@0: // Everything else is okay, no error. michael@0: // michael@0: if (message == 0) michael@0: return false; michael@0: michael@0: // michael@0: // If we get here, we have an error and a message. michael@0: // michael@0: if (symNode) { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "\"" << symbol << "\" (" << message << ")"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, " l-value required", op, extraInfo.c_str()); michael@0: } michael@0: else { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "(" << message << ")"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, " l-value required", op, extraInfo.c_str()); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // michael@0: // Both test, and if necessary spit out an error, to see if the node is really michael@0: // a constant. michael@0: // michael@0: // Returns true if the was an error. michael@0: // michael@0: bool TParseContext::constErrorCheck(TIntermTyped* node) michael@0: { michael@0: if (node->getQualifier() == EvqConst) michael@0: return false; michael@0: michael@0: error(node->getLine(), "constant expression required", ""); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // michael@0: // Both test, and if necessary spit out an error, to see if the node is really michael@0: // an integer. michael@0: // michael@0: // Returns true if the was an error. michael@0: // michael@0: bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token) michael@0: { michael@0: if (node->getBasicType() == EbtInt && node->getNominalSize() == 1) michael@0: return false; michael@0: michael@0: error(node->getLine(), "integer expression required", token); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // michael@0: // Both test, and if necessary spit out an error, to see if we are currently michael@0: // globally scoped. michael@0: // michael@0: // Returns true if the was an error. michael@0: // michael@0: bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token) michael@0: { michael@0: if (global) michael@0: return false; michael@0: michael@0: error(line, "only allowed at global scope", token); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // michael@0: // For now, keep it simple: if it starts "gl_", it's reserved, independent michael@0: // of scope. Except, if the symbol table is at the built-in push-level, michael@0: // which is when we are parsing built-ins. michael@0: // Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a michael@0: // webgl shader. michael@0: // michael@0: // Returns true if there was an error. michael@0: // michael@0: bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier) michael@0: { michael@0: static const char* reservedErrMsg = "reserved built-in name"; michael@0: if (!symbolTable.atBuiltInLevel()) { michael@0: if (identifier.compare(0, 3, "gl_") == 0) { michael@0: error(line, reservedErrMsg, "gl_"); michael@0: return true; michael@0: } michael@0: if (isWebGLBasedSpec(shaderSpec)) { michael@0: if (identifier.compare(0, 6, "webgl_") == 0) { michael@0: error(line, reservedErrMsg, "webgl_"); michael@0: return true; michael@0: } michael@0: if (identifier.compare(0, 7, "_webgl_") == 0) { michael@0: error(line, reservedErrMsg, "_webgl_"); michael@0: return true; michael@0: } michael@0: if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) { michael@0: error(line, reservedErrMsg, "css_"); michael@0: return true; michael@0: } michael@0: } michael@0: if (identifier.find("__") != TString::npos) { michael@0: error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str()); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // Make sure there is enough data provided to the constructor to build michael@0: // something of the type of the constructor. Also returns the type of michael@0: // the constructor. michael@0: // michael@0: // Returns true if there was an error in construction. michael@0: // michael@0: bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type) michael@0: { michael@0: *type = function.getReturnType(); michael@0: michael@0: bool constructingMatrix = false; michael@0: switch(op) { michael@0: case EOpConstructMat2: michael@0: case EOpConstructMat3: michael@0: case EOpConstructMat4: michael@0: constructingMatrix = true; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: // michael@0: // Note: It's okay to have too many components available, but not okay to have unused michael@0: // arguments. 'full' will go to true when enough args have been seen. If we loop michael@0: // again, there is an extra argument, so 'overfull' will become true. michael@0: // michael@0: michael@0: size_t size = 0; michael@0: bool constType = true; michael@0: bool full = false; michael@0: bool overFull = false; michael@0: bool matrixInMatrix = false; michael@0: bool arrayArg = false; michael@0: for (size_t i = 0; i < function.getParamCount(); ++i) { michael@0: const TParameter& param = function.getParam(i); michael@0: size += param.type->getObjectSize(); michael@0: michael@0: if (constructingMatrix && param.type->isMatrix()) michael@0: matrixInMatrix = true; michael@0: if (full) michael@0: overFull = true; michael@0: if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize()) michael@0: full = true; michael@0: if (param.type->getQualifier() != EvqConst) michael@0: constType = false; michael@0: if (param.type->isArray()) michael@0: arrayArg = true; michael@0: } michael@0: michael@0: if (constType) michael@0: type->setQualifier(EvqConst); michael@0: michael@0: if (type->isArray() && static_cast(type->getArraySize()) != function.getParamCount()) { michael@0: error(line, "array constructor needs one argument per array element", "constructor"); michael@0: return true; michael@0: } michael@0: michael@0: if (arrayArg && op != EOpConstructStruct) { michael@0: error(line, "constructing from a non-dereferenced array", "constructor"); michael@0: return true; michael@0: } michael@0: michael@0: if (matrixInMatrix && !type->isArray()) { michael@0: if (function.getParamCount() != 1) { michael@0: error(line, "constructing matrix from matrix can only take one argument", "constructor"); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: if (overFull) { michael@0: error(line, "too many arguments", "constructor"); michael@0: return true; michael@0: } michael@0: michael@0: if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->fields().size()) != function.getParamCount()) { michael@0: error(line, "Number of constructor parameters does not match the number of structure fields", "constructor"); michael@0: return true; michael@0: } michael@0: michael@0: if (!type->isMatrix() || !matrixInMatrix) { michael@0: if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) || michael@0: (op == EOpConstructStruct && size < type->getObjectSize())) { michael@0: error(line, "not enough data provided for construction", "constructor"); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: TIntermTyped *typed = node ? node->getAsTyped() : 0; michael@0: if (typed == 0) { michael@0: error(line, "constructor argument does not have a type", "constructor"); michael@0: return true; michael@0: } michael@0: if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) { michael@0: error(line, "cannot convert a sampler", "constructor"); michael@0: return true; michael@0: } michael@0: if (typed->getBasicType() == EbtVoid) { michael@0: error(line, "cannot convert a void", "constructor"); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // This function checks to see if a void variable has been declared and raise an error message for such a case michael@0: // michael@0: // returns true in case of an error michael@0: // michael@0: bool TParseContext::voidErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& pubType) michael@0: { michael@0: if (pubType.type == EbtVoid) { michael@0: error(line, "illegal use of type 'void'", identifier.c_str()); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // This function checks to see if the node (for the expression) contains a scalar boolean expression or not michael@0: // michael@0: // returns true in case of an error michael@0: // michael@0: bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type) michael@0: { michael@0: if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) { michael@0: error(line, "boolean expression expected", ""); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // This function checks to see if the node (for the expression) contains a scalar boolean expression or not michael@0: // michael@0: // returns true in case of an error michael@0: // michael@0: bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType) michael@0: { michael@0: if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) { michael@0: error(line, "boolean expression expected", ""); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason) michael@0: { michael@0: if (pType.type == EbtStruct) { michael@0: if (containsSampler(*pType.userDef)) { michael@0: error(line, reason, getBasicString(pType.type), "(structure contains a sampler)"); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } else if (IsSampler(pType.type)) { michael@0: error(line, reason, getBasicString(pType.type)); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool TParseContext::structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType) michael@0: { michael@0: if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) && michael@0: pType.type == EbtStruct) { michael@0: error(line, "cannot be used with a structure", getQualifierString(pType.qualifier)); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform")) michael@0: return true; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type) michael@0: { michael@0: if ((qualifier == EvqOut || qualifier == EvqInOut) && michael@0: type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) { michael@0: error(line, "samplers cannot be output parameters", type.getBasicString()); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool TParseContext::containsSampler(TType& type) michael@0: { michael@0: if (IsSampler(type.getBasicType())) michael@0: return true; michael@0: michael@0: if (type.getBasicType() == EbtStruct) { michael@0: const TFieldList& fields = type.getStruct()->fields(); michael@0: for (unsigned int i = 0; i < fields.size(); ++i) { michael@0: if (containsSampler(*fields[i]->type())) michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // Do size checking for an array type's size. michael@0: // michael@0: // Returns true if there was an error. michael@0: // michael@0: bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size) michael@0: { michael@0: TIntermConstantUnion* constant = expr->getAsConstantUnion(); michael@0: if (constant == 0 || constant->getBasicType() != EbtInt) { michael@0: error(line, "array size must be a constant integer expression", ""); michael@0: return true; michael@0: } michael@0: michael@0: size = constant->getIConst(0); michael@0: michael@0: if (size <= 0) { michael@0: error(line, "array size must be a positive integer", ""); michael@0: size = 1; michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // See if this qualifier can be an array. michael@0: // michael@0: // Returns true if there is an error. michael@0: // michael@0: bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type) michael@0: { michael@0: if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) { michael@0: error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str()); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // See if this type can be an array. michael@0: // michael@0: // Returns true if there is an error. michael@0: // michael@0: bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type) michael@0: { michael@0: // michael@0: // Can the type be an array? michael@0: // michael@0: if (type.array) { michael@0: error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str()); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // Do all the semantic checking for declaring an array, with and michael@0: // without a size, and make the right changes to the symbol table. michael@0: // michael@0: // size == 0 means no specified size. michael@0: // michael@0: // Returns true if there was an error. michael@0: // michael@0: bool TParseContext::arrayErrorCheck(const TSourceLoc& line, TString& identifier, TPublicType type, TVariable*& variable) michael@0: { michael@0: // michael@0: // Don't check for reserved word use until after we know it's not in the symbol table, michael@0: // because reserved arrays can be redeclared. michael@0: // michael@0: michael@0: bool builtIn = false; michael@0: bool sameScope = false; michael@0: TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope); michael@0: if (symbol == 0 || !sameScope) { michael@0: if (reservedErrorCheck(line, identifier)) michael@0: return true; michael@0: michael@0: variable = new TVariable(&identifier, TType(type)); michael@0: michael@0: if (type.arraySize) michael@0: variable->getType().setArraySize(type.arraySize); michael@0: michael@0: if (! symbolTable.insert(*variable)) { michael@0: delete variable; michael@0: error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str()); michael@0: return true; michael@0: } michael@0: } else { michael@0: if (! symbol->isVariable()) { michael@0: error(line, "variable expected", identifier.c_str()); michael@0: return true; michael@0: } michael@0: michael@0: variable = static_cast(symbol); michael@0: if (! variable->getType().isArray()) { michael@0: error(line, "redeclaring non-array as array", identifier.c_str()); michael@0: return true; michael@0: } michael@0: if (variable->getType().getArraySize() > 0) { michael@0: error(line, "redeclaration of array with size", identifier.c_str()); michael@0: return true; michael@0: } michael@0: michael@0: if (! variable->getType().sameElementType(TType(type))) { michael@0: error(line, "redeclaration of array with a different type", identifier.c_str()); michael@0: return true; michael@0: } michael@0: michael@0: if (type.arraySize) michael@0: variable->getType().setArraySize(type.arraySize); michael@0: } michael@0: michael@0: if (voidErrorCheck(line, identifier, type)) michael@0: return true; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // Enforce non-initializer type/qualifier rules. michael@0: // michael@0: // Returns true if there was an error. michael@0: // michael@0: bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, TString& identifier, TPublicType& type, bool array) michael@0: { michael@0: if (type.qualifier == EvqConst) michael@0: { michael@0: // Make the qualifier make sense. michael@0: type.qualifier = EvqTemporary; michael@0: michael@0: if (array) michael@0: { michael@0: error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str()); michael@0: } michael@0: else if (type.isStructureContainingArrays()) michael@0: { michael@0: error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str()); michael@0: } michael@0: else michael@0: { michael@0: error(line, "variables with qualifier 'const' must be initialized", identifier.c_str()); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // Do semantic checking for a variable declaration that has no initializer, michael@0: // and update the symbol table. michael@0: // michael@0: // Returns true if there was an error. michael@0: // michael@0: bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, TString& identifier, TPublicType& type, TVariable*& variable) michael@0: { michael@0: if (reservedErrorCheck(line, identifier)) michael@0: recover(); michael@0: michael@0: variable = new TVariable(&identifier, TType(type)); michael@0: michael@0: if (! symbolTable.insert(*variable)) { michael@0: error(line, "redefinition", variable->getName().c_str()); michael@0: delete variable; michael@0: variable = 0; michael@0: return true; michael@0: } michael@0: michael@0: if (voidErrorCheck(line, identifier, type)) michael@0: return true; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type) michael@0: { michael@0: if (qualifier != EvqConst && qualifier != EvqTemporary) { michael@0: error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier)); michael@0: return true; michael@0: } michael@0: if (qualifier == EvqConst && paramQualifier != EvqIn) { michael@0: error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier)); michael@0: return true; michael@0: } michael@0: michael@0: if (qualifier == EvqConst) michael@0: type->setQualifier(EvqConstReadOnly); michael@0: else michael@0: type->setQualifier(paramQualifier); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension) michael@0: { michael@0: const TExtensionBehavior& extBehavior = extensionBehavior(); michael@0: TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str()); michael@0: if (iter == extBehavior.end()) { michael@0: error(line, "extension", extension.c_str(), "is not supported"); michael@0: return true; michael@0: } michael@0: // In GLSL ES, an extension's default behavior is "disable". michael@0: if (iter->second == EBhDisable || iter->second == EBhUndefined) { michael@0: error(line, "extension", extension.c_str(), "is disabled"); michael@0: return true; michael@0: } michael@0: if (iter->second == EBhWarn) { michael@0: warning(line, "extension", extension.c_str(), "is being used"); michael@0: return false; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool TParseContext::supportsExtension(const char* extension) michael@0: { michael@0: const TExtensionBehavior& extbehavior = extensionBehavior(); michael@0: TExtensionBehavior::const_iterator iter = extbehavior.find(extension); michael@0: return (iter != extbehavior.end()); michael@0: } michael@0: michael@0: bool TParseContext::isExtensionEnabled(const char* extension) const michael@0: { michael@0: const TExtensionBehavior& extbehavior = extensionBehavior(); michael@0: TExtensionBehavior::const_iterator iter = extbehavior.find(extension); michael@0: michael@0: if (iter == extbehavior.end()) michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: return (iter->second == EBhEnable || iter->second == EBhRequire); michael@0: } michael@0: michael@0: ///////////////////////////////////////////////////////////////////////////////// michael@0: // michael@0: // Non-Errors. michael@0: // michael@0: ///////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: // michael@0: // Look up a function name in the symbol table, and make sure it is a function. michael@0: // michael@0: // Return the function symbol if found, otherwise 0. michael@0: // michael@0: const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, bool *builtIn) michael@0: { michael@0: // First find by unmangled name to check whether the function name has been michael@0: // hidden by a variable name or struct typename. michael@0: // If a function is found, check for one with a matching argument list. michael@0: const TSymbol* symbol = symbolTable.find(call->getName(), builtIn); michael@0: if (symbol == 0 || symbol->isFunction()) { michael@0: symbol = symbolTable.find(call->getMangledName(), builtIn); michael@0: } michael@0: michael@0: if (symbol == 0) { michael@0: error(line, "no matching overloaded function found", call->getName().c_str()); michael@0: return 0; michael@0: } michael@0: michael@0: if (!symbol->isFunction()) { michael@0: error(line, "function name expected", call->getName().c_str()); michael@0: return 0; michael@0: } michael@0: michael@0: return static_cast(symbol); michael@0: } michael@0: michael@0: // michael@0: // Initializers show up in several places in the grammar. Have one set of michael@0: // code to handle them here. michael@0: // michael@0: bool TParseContext::executeInitializer(const TSourceLoc& line, TString& identifier, TPublicType& pType, michael@0: TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable) michael@0: { michael@0: TType type = TType(pType); michael@0: michael@0: if (variable == 0) { michael@0: if (reservedErrorCheck(line, identifier)) michael@0: return true; michael@0: michael@0: if (voidErrorCheck(line, identifier, pType)) michael@0: return true; michael@0: michael@0: // michael@0: // add variable to symbol table michael@0: // michael@0: variable = new TVariable(&identifier, type); michael@0: if (! symbolTable.insert(*variable)) { michael@0: error(line, "redefinition", variable->getName().c_str()); michael@0: return true; michael@0: // don't delete variable, it's used by error recovery, and the pool michael@0: // pop will take care of the memory michael@0: } michael@0: } michael@0: michael@0: // michael@0: // identifier must be of type constant, a global, or a temporary michael@0: // michael@0: TQualifier qualifier = variable->getType().getQualifier(); michael@0: if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) { michael@0: error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString()); michael@0: return true; michael@0: } michael@0: // michael@0: // test for and propagate constant michael@0: // michael@0: michael@0: if (qualifier == EvqConst) { michael@0: if (qualifier != initializer->getType().getQualifier()) { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "'" << variable->getType().getCompleteString() << "'"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, " assigning non-constant to", "=", extraInfo.c_str()); michael@0: variable->getType().setQualifier(EvqTemporary); michael@0: return true; michael@0: } michael@0: if (type != initializer->getType()) { michael@0: error(line, " non-matching types for const initializer ", michael@0: variable->getType().getQualifierString()); michael@0: variable->getType().setQualifier(EvqTemporary); michael@0: return true; michael@0: } michael@0: if (initializer->getAsConstantUnion()) { michael@0: variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer()); michael@0: } else if (initializer->getAsSymbolNode()) { michael@0: const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol()); michael@0: const TVariable* tVar = static_cast(symbol); michael@0: michael@0: ConstantUnion* constArray = tVar->getConstPointer(); michael@0: variable->shareConstPointer(constArray); michael@0: } else { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "'" << variable->getType().getCompleteString() << "'"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, " cannot assign to", "=", extraInfo.c_str()); michael@0: variable->getType().setQualifier(EvqTemporary); michael@0: return true; michael@0: } michael@0: } michael@0: michael@0: if (qualifier != EvqConst) { michael@0: TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line); michael@0: intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line); michael@0: if (intermNode == 0) { michael@0: assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString()); michael@0: return true; michael@0: } michael@0: } else michael@0: intermNode = 0; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode) michael@0: { michael@0: ASSERT(aggrNode != NULL); michael@0: if (!aggrNode->isConstructor()) michael@0: return false; michael@0: michael@0: bool allConstant = true; michael@0: michael@0: // check if all the child nodes are constants so that they can be inserted into michael@0: // the parent node michael@0: TIntermSequence &sequence = aggrNode->getSequence() ; michael@0: for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) { michael@0: if (!(*p)->getAsTyped()->getAsConstantUnion()) michael@0: return false; michael@0: } michael@0: michael@0: return allConstant; michael@0: } michael@0: michael@0: // This function is used to test for the correctness of the parameters passed to various constructor functions michael@0: // and also convert them to the right datatype if it is allowed and required. michael@0: // michael@0: // Returns 0 for an error or the constructed node (aggregate or typed) for no error. michael@0: // michael@0: TIntermTyped* TParseContext::addConstructor(TIntermNode* node, const TType* type, TOperator op, TFunction* fnCall, const TSourceLoc& line) michael@0: { michael@0: if (node == 0) michael@0: return 0; michael@0: michael@0: TIntermAggregate* aggrNode = node->getAsAggregate(); michael@0: michael@0: TFieldList::const_iterator memberFields; michael@0: if (op == EOpConstructStruct) michael@0: memberFields = type->getStruct()->fields().begin(); michael@0: michael@0: TType elementType = *type; michael@0: if (type->isArray()) michael@0: elementType.clearArrayness(); michael@0: michael@0: bool singleArg; michael@0: if (aggrNode) { michael@0: if (aggrNode->getOp() != EOpNull || aggrNode->getSequence().size() == 1) michael@0: singleArg = true; michael@0: else michael@0: singleArg = false; michael@0: } else michael@0: singleArg = true; michael@0: michael@0: TIntermTyped *newNode; michael@0: if (singleArg) { michael@0: // If structure constructor or array constructor is being called michael@0: // for only one parameter inside the structure, we need to call constructStruct function once. michael@0: if (type->isArray()) michael@0: newNode = constructStruct(node, &elementType, 1, node->getLine(), false); michael@0: else if (op == EOpConstructStruct) michael@0: newNode = constructStruct(node, (*memberFields)->type(), 1, node->getLine(), false); michael@0: else michael@0: newNode = constructBuiltIn(type, op, node, node->getLine(), false); michael@0: michael@0: if (newNode && newNode->getAsAggregate()) { michael@0: TIntermTyped* constConstructor = foldConstConstructor(newNode->getAsAggregate(), *type); michael@0: if (constConstructor) michael@0: return constConstructor; michael@0: } michael@0: michael@0: return newNode; michael@0: } michael@0: michael@0: // michael@0: // Handle list of arguments. michael@0: // michael@0: TIntermSequence &sequenceVector = aggrNode->getSequence() ; // Stores the information about the parameter to the constructor michael@0: // if the structure constructor contains more than one parameter, then construct michael@0: // each parameter michael@0: michael@0: int paramCount = 0; // keeps a track of the constructor parameter number being checked michael@0: michael@0: // for each parameter to the constructor call, check to see if the right type is passed or convert them michael@0: // to the right type if possible (and allowed). michael@0: // for structure constructors, just check if the right type is passed, no conversion is allowed. michael@0: michael@0: for (TIntermSequence::iterator p = sequenceVector.begin(); michael@0: p != sequenceVector.end(); p++, paramCount++) { michael@0: if (type->isArray()) michael@0: newNode = constructStruct(*p, &elementType, paramCount+1, node->getLine(), true); michael@0: else if (op == EOpConstructStruct) michael@0: newNode = constructStruct(*p, memberFields[paramCount]->type(), paramCount+1, node->getLine(), true); michael@0: else michael@0: newNode = constructBuiltIn(type, op, *p, node->getLine(), true); michael@0: michael@0: if (newNode) { michael@0: *p = newNode; michael@0: } michael@0: } michael@0: michael@0: TIntermTyped* constructor = intermediate.setAggregateOperator(aggrNode, op, line); michael@0: TIntermTyped* constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type); michael@0: if (constConstructor) michael@0: return constConstructor; michael@0: michael@0: return constructor; michael@0: } michael@0: michael@0: TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type) michael@0: { michael@0: bool canBeFolded = areAllChildConst(aggrNode); michael@0: aggrNode->setType(type); michael@0: if (canBeFolded) { michael@0: bool returnVal = false; michael@0: ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()]; michael@0: if (aggrNode->getSequence().size() == 1) { michael@0: returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type, true); michael@0: } michael@0: else { michael@0: returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), symbolTable, type); michael@0: } michael@0: if (returnVal) michael@0: return 0; michael@0: michael@0: return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine()); michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: // Function for constructor implementation. Calls addUnaryMath with appropriate EOp value michael@0: // for the parameter to the constructor (passed to this function). Essentially, it converts michael@0: // the parameter types correctly. If a constructor expects an int (like ivec2) and is passed a michael@0: // float, then float is converted to int. michael@0: // michael@0: // Returns 0 for an error or the constructed node. michael@0: // michael@0: TIntermTyped* TParseContext::constructBuiltIn(const TType* type, TOperator op, TIntermNode* node, const TSourceLoc& line, bool subset) michael@0: { michael@0: TIntermTyped* newNode; michael@0: TOperator basicOp; michael@0: michael@0: // michael@0: // First, convert types as needed. michael@0: // michael@0: switch (op) { michael@0: case EOpConstructVec2: michael@0: case EOpConstructVec3: michael@0: case EOpConstructVec4: michael@0: case EOpConstructMat2: michael@0: case EOpConstructMat3: michael@0: case EOpConstructMat4: michael@0: case EOpConstructFloat: michael@0: basicOp = EOpConstructFloat; michael@0: break; michael@0: michael@0: case EOpConstructIVec2: michael@0: case EOpConstructIVec3: michael@0: case EOpConstructIVec4: michael@0: case EOpConstructInt: michael@0: basicOp = EOpConstructInt; michael@0: break; michael@0: michael@0: case EOpConstructBVec2: michael@0: case EOpConstructBVec3: michael@0: case EOpConstructBVec4: michael@0: case EOpConstructBool: michael@0: basicOp = EOpConstructBool; michael@0: break; michael@0: michael@0: default: michael@0: error(line, "unsupported construction", ""); michael@0: recover(); michael@0: michael@0: return 0; michael@0: } michael@0: newNode = intermediate.addUnaryMath(basicOp, node, node->getLine(), symbolTable); michael@0: if (newNode == 0) { michael@0: error(line, "can't convert", "constructor"); michael@0: return 0; michael@0: } michael@0: michael@0: // michael@0: // Now, if there still isn't an operation to do the construction, and we need one, add one. michael@0: // michael@0: michael@0: // Otherwise, skip out early. michael@0: if (subset || (newNode != node && newNode->getType() == *type)) michael@0: return newNode; michael@0: michael@0: // setAggregateOperator will insert a new node for the constructor, as needed. michael@0: return intermediate.setAggregateOperator(newNode, op, line); michael@0: } michael@0: michael@0: // This function tests for the type of the parameters to the structures constructors. Raises michael@0: // an error message if the expected type does not match the parameter passed to the constructor. michael@0: // michael@0: // Returns 0 for an error or the input node itself if the expected and the given parameter types match. michael@0: // michael@0: TIntermTyped* TParseContext::constructStruct(TIntermNode* node, TType* type, int paramCount, const TSourceLoc& line, bool subset) michael@0: { michael@0: if (*type == node->getAsTyped()->getType()) { michael@0: if (subset) michael@0: return node->getAsTyped(); michael@0: else michael@0: return intermediate.setAggregateOperator(node->getAsTyped(), EOpConstructStruct, line); michael@0: } else { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "cannot convert parameter " << paramCount michael@0: << " from '" << node->getAsTyped()->getType().getBasicString() michael@0: << "' to '" << type->getBasicString() << "'"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, "", "constructor", extraInfo.c_str()); michael@0: recover(); michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: // michael@0: // This function returns the tree representation for the vector field(s) being accessed from contant vector. michael@0: // 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: // returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol michael@0: // node or it could be the intermediate tree representation of accessing fields in a constant structure or column of michael@0: // a constant matrix. michael@0: // michael@0: TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line) michael@0: { michael@0: TIntermTyped* typedNode; michael@0: TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); michael@0: michael@0: ConstantUnion *unionArray; michael@0: if (tempConstantNode) { michael@0: unionArray = tempConstantNode->getUnionArrayPointer(); michael@0: michael@0: if (!unionArray) { michael@0: return node; michael@0: } michael@0: } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error michael@0: error(line, "Cannot offset into the vector", "Error"); michael@0: recover(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: ConstantUnion* constArray = new ConstantUnion[fields.num]; michael@0: michael@0: for (int i = 0; i < fields.num; i++) { michael@0: if (fields.offsets[i] >= node->getType().getNominalSize()) { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, "", "[", extraInfo.c_str()); michael@0: recover(); michael@0: fields.offsets[i] = 0; michael@0: } michael@0: michael@0: constArray[i] = unionArray[fields.offsets[i]]; michael@0: michael@0: } michael@0: typedNode = intermediate.addConstantUnion(constArray, node->getType(), line); michael@0: return typedNode; michael@0: } michael@0: michael@0: // michael@0: // This function returns the column being accessed from a constant matrix. The values are retrieved from michael@0: // the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input michael@0: // to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a michael@0: // constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure) michael@0: // michael@0: TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line) michael@0: { michael@0: TIntermTyped* typedNode; michael@0: TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); michael@0: michael@0: if (index >= node->getType().getNominalSize()) { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "matrix field selection out of range '" << index << "'"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, "", "[", extraInfo.c_str()); michael@0: recover(); michael@0: index = 0; michael@0: } michael@0: michael@0: if (tempConstantNode) { michael@0: ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer(); michael@0: int size = tempConstantNode->getType().getNominalSize(); michael@0: typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line); michael@0: } else { michael@0: error(line, "Cannot offset into the matrix", "Error"); michael@0: recover(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: return typedNode; michael@0: } michael@0: michael@0: michael@0: // michael@0: // This function returns an element of an array accessed from a constant array. The values are retrieved from michael@0: // the symbol table and parse-tree is built for the type of the element. The input michael@0: // to the function could either be a symbol node (a[0] where a is a constant array)that represents a michael@0: // constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure) michael@0: // michael@0: TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line) michael@0: { michael@0: TIntermTyped* typedNode; michael@0: TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); michael@0: TType arrayElementType = node->getType(); michael@0: arrayElementType.clearArrayness(); michael@0: michael@0: if (index >= node->getType().getArraySize()) { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "array field selection out of range '" << index << "'"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, "", "[", extraInfo.c_str()); michael@0: recover(); michael@0: index = 0; michael@0: } michael@0: michael@0: if (tempConstantNode) { michael@0: size_t arrayElementSize = arrayElementType.getObjectSize(); michael@0: ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer(); michael@0: typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line); michael@0: } else { michael@0: error(line, "Cannot offset into the array", "Error"); michael@0: recover(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: return typedNode; michael@0: } michael@0: michael@0: michael@0: // michael@0: // This function returns the value of a particular field inside a constant structure from the symbol table. michael@0: // If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr michael@0: // function and returns the parse-tree with the values of the embedded/nested struct. michael@0: // michael@0: TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, const TSourceLoc& line) michael@0: { michael@0: const TFieldList& fields = node->getType().getStruct()->fields(); michael@0: michael@0: size_t instanceSize = 0; michael@0: for (size_t index = 0; index < fields.size(); ++index) { michael@0: if (fields[index]->name() == identifier) { michael@0: break; michael@0: } else { michael@0: instanceSize += fields[index]->type()->getObjectSize(); michael@0: } michael@0: } michael@0: michael@0: TIntermTyped* typedNode = 0; michael@0: TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion(); michael@0: if (tempConstantNode) { michael@0: ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer(); michael@0: michael@0: typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function michael@0: } else { michael@0: error(line, "Cannot offset into the structure", "Error"); michael@0: recover(); michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: return typedNode; michael@0: } michael@0: michael@0: bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier) michael@0: { michael@0: ++structNestingLevel; michael@0: michael@0: // Embedded structure definitions are not supported per GLSL ES spec. michael@0: // They aren't allowed in GLSL either, but we need to detect this here michael@0: // so we don't rely on the GLSL compiler to catch it. michael@0: if (structNestingLevel > 1) { michael@0: error(line, "", "Embedded struct definitions are not allowed"); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void TParseContext::exitStructDeclaration() michael@0: { michael@0: --structNestingLevel; michael@0: } michael@0: michael@0: namespace { michael@0: michael@0: const int kWebGLMaxStructNesting = 4; michael@0: michael@0: } // namespace michael@0: michael@0: bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field) michael@0: { michael@0: if (!isWebGLBasedSpec(shaderSpec)) { michael@0: return false; michael@0: } michael@0: michael@0: if (field.type()->getBasicType() != EbtStruct) { michael@0: return false; michael@0: } michael@0: michael@0: // We're already inside a structure definition at this point, so add michael@0: // one to the field's struct nesting. michael@0: if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "Reference of struct type " << field.name() michael@0: << " exceeds maximum struct nesting of " << kWebGLMaxStructNesting; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(line, "", "", extraInfo.c_str()); michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // michael@0: // Parse an array index expression michael@0: // michael@0: TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression) michael@0: { michael@0: TIntermTyped *indexedExpression = NULL; michael@0: michael@0: if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector()) michael@0: { michael@0: if (baseExpression->getAsSymbolNode()) michael@0: { michael@0: error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str()); michael@0: } michael@0: else michael@0: { michael@0: error(location, " left of '[' is not of type array, matrix, or vector ", "expression"); michael@0: } michael@0: recover(); michael@0: } michael@0: michael@0: if (indexExpression->getQualifier() == EvqConst) michael@0: { michael@0: int index = indexExpression->getAsConstantUnion()->getIConst(0); michael@0: if (index < 0) michael@0: { michael@0: std::stringstream infoStream; michael@0: infoStream << index; michael@0: std::string info = infoStream.str(); michael@0: error(location, "negative index", info.c_str()); michael@0: recover(); michael@0: index = 0; michael@0: } michael@0: if (baseExpression->getType().getQualifier() == EvqConst) michael@0: { michael@0: if (baseExpression->isArray()) michael@0: { michael@0: // constant folding for arrays michael@0: indexedExpression = addConstArrayNode(index, baseExpression, location); michael@0: } michael@0: else if (baseExpression->isVector()) michael@0: { michael@0: // constant folding for vectors michael@0: TVectorFields fields; michael@0: fields.num = 1; michael@0: fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array michael@0: indexedExpression = addConstVectorNode(fields, baseExpression, location); michael@0: } michael@0: else if (baseExpression->isMatrix()) michael@0: { michael@0: // constant folding for matrices michael@0: indexedExpression = addConstMatrixNode(index, baseExpression, location); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if (baseExpression->isArray()) michael@0: { michael@0: if (index >= baseExpression->getType().getArraySize()) michael@0: { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "array index out of range '" << index << "'"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(location, "", "[", extraInfo.c_str()); michael@0: recover(); michael@0: index = baseExpression->getType().getArraySize() - 1; michael@0: } michael@0: else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers")) michael@0: { michael@0: error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled"); michael@0: recover(); michael@0: index = 0; michael@0: } michael@0: } michael@0: else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index) michael@0: { michael@0: std::stringstream extraInfoStream; michael@0: extraInfoStream << "field selection out of range '" << index << "'"; michael@0: std::string extraInfo = extraInfoStream.str(); michael@0: error(location, "", "[", extraInfo.c_str()); michael@0: recover(); michael@0: index = baseExpression->getType().getNominalSize() - 1; michael@0: } michael@0: michael@0: indexExpression->getAsConstantUnion()->getUnionArrayPointer()->setIConst(index); michael@0: indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location); michael@0: } michael@0: } michael@0: else michael@0: { michael@0: indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location); michael@0: } michael@0: michael@0: if (indexedExpression == 0) michael@0: { michael@0: ConstantUnion *unionArray = new ConstantUnion[1]; michael@0: unionArray->setFConst(0.0f); michael@0: indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location); michael@0: } michael@0: else if (baseExpression->isArray()) michael@0: { michael@0: const TType &baseType = baseExpression->getType(); michael@0: if (baseType.getStruct()) michael@0: { michael@0: TType copyOfType(baseType.getStruct()); michael@0: indexedExpression->setType(copyOfType); michael@0: } michael@0: else michael@0: { michael@0: indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->isMatrix())); michael@0: } michael@0: michael@0: if (baseExpression->getType().getQualifier() == EvqConst) michael@0: { michael@0: indexedExpression->getTypePointer()->setQualifier(EvqConst); michael@0: } michael@0: } michael@0: else if (baseExpression->isMatrix()) michael@0: { michael@0: TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary; michael@0: indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getNominalSize())); michael@0: } michael@0: else if (baseExpression->isVector()) michael@0: { michael@0: TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary; michael@0: indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier)); michael@0: } michael@0: else michael@0: { michael@0: indexedExpression->setType(baseExpression->getType()); michael@0: } michael@0: michael@0: return indexedExpression; michael@0: } michael@0: michael@0: // michael@0: // Parse an array of strings using yyparse. michael@0: // michael@0: // Returns 0 for success. michael@0: // michael@0: int PaParseStrings(size_t count, const char* const string[], const int length[], michael@0: TParseContext* context) { michael@0: if ((count == 0) || (string == NULL)) michael@0: return 1; michael@0: michael@0: if (glslang_initialize(context)) michael@0: return 1; michael@0: michael@0: int error = glslang_scan(count, string, length, context); michael@0: if (!error) michael@0: error = glslang_parse(context); michael@0: michael@0: glslang_finalize(context); michael@0: michael@0: return (error == 0) && (context->numErrors() == 0) ? 0 : 1; michael@0: } michael@0: michael@0: michael@0: