2 // Copyright (c) 2002-2014 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.
7 #include "compiler/translator/ParseContext.h"
12 #include "compiler/translator/glslang.h"
13 #include "compiler/preprocessor/SourceLocation.h"
15 ///////////////////////////////////////////////////////////////////////
17 // Sub- vector and matrix fields
19 ////////////////////////////////////////////////////////////////////////
22 // Look at a '.' field selector string and change it into offsets
25 bool TParseContext::parseVectorFields(const TString& compString, int vecSize, TVectorFields& fields, const TSourceLoc& line)
27 fields.num = (int) compString.size();
29 error(line, "illegal vector field selection", compString.c_str());
39 for (int i = 0; i < fields.num; ++i) {
40 switch (compString[i]) {
42 fields.offsets[i] = 0;
46 fields.offsets[i] = 0;
50 fields.offsets[i] = 0;
54 fields.offsets[i] = 1;
58 fields.offsets[i] = 1;
62 fields.offsets[i] = 1;
66 fields.offsets[i] = 2;
70 fields.offsets[i] = 2;
74 fields.offsets[i] = 2;
79 fields.offsets[i] = 3;
83 fields.offsets[i] = 3;
87 fields.offsets[i] = 3;
91 error(line, "illegal vector field selection", compString.c_str());
96 for (int i = 0; i < fields.num; ++i) {
97 if (fields.offsets[i] >= vecSize) {
98 error(line, "vector field selection out of range", compString.c_str());
103 if (fieldSet[i] != fieldSet[i-1]) {
104 error(line, "illegal - vector component fields not from the same set", compString.c_str());
115 // Look at a '.' field selector string and change it into offsets
118 bool TParseContext::parseMatrixFields(const TString& compString, int matCols, int matRows, TMatrixFields& fields, const TSourceLoc& line)
120 fields.wholeRow = false;
121 fields.wholeCol = false;
125 if (compString.size() != 2) {
126 error(line, "illegal length of matrix field selection", compString.c_str());
130 if (compString[0] == '_') {
131 if (compString[1] < '0' || compString[1] > '3') {
132 error(line, "illegal matrix field selection", compString.c_str());
135 fields.wholeCol = true;
136 fields.col = compString[1] - '0';
137 } else if (compString[1] == '_') {
138 if (compString[0] < '0' || compString[0] > '3') {
139 error(line, "illegal matrix field selection", compString.c_str());
142 fields.wholeRow = true;
143 fields.row = compString[0] - '0';
145 if (compString[0] < '0' || compString[0] > '3' ||
146 compString[1] < '0' || compString[1] > '3') {
147 error(line, "illegal matrix field selection", compString.c_str());
150 fields.row = compString[0] - '0';
151 fields.col = compString[1] - '0';
154 if (fields.row >= matRows || fields.col >= matCols) {
155 error(line, "matrix field selection out of range", compString.c_str());
162 ///////////////////////////////////////////////////////////////////////
166 ////////////////////////////////////////////////////////////////////////
169 // Track whether errors have occurred.
171 void TParseContext::recover()
176 // Used by flex/bison to output all syntax and parsing errors.
178 void TParseContext::error(const TSourceLoc& loc,
179 const char* reason, const char* token,
180 const char* extraInfo)
182 pp::SourceLocation srcLoc;
183 srcLoc.file = loc.first_file;
184 srcLoc.line = loc.first_line;
185 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
186 srcLoc, reason, token, extraInfo);
190 void TParseContext::warning(const TSourceLoc& loc,
191 const char* reason, const char* token,
192 const char* extraInfo) {
193 pp::SourceLocation srcLoc;
194 srcLoc.file = loc.first_file;
195 srcLoc.line = loc.first_line;
196 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
197 srcLoc, reason, token, extraInfo);
200 void TParseContext::trace(const char* str)
202 diagnostics.writeDebug(str);
206 // Same error message for all places assignments don't work.
208 void TParseContext::assignError(const TSourceLoc& line, const char* op, TString left, TString right)
210 std::stringstream extraInfoStream;
211 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
212 std::string extraInfo = extraInfoStream.str();
213 error(line, "", op, extraInfo.c_str());
217 // Same error message for all places unary operations don't work.
219 void TParseContext::unaryOpError(const TSourceLoc& line, const char* op, TString operand)
221 std::stringstream extraInfoStream;
222 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
223 << " (or there is no acceptable conversion)";
224 std::string extraInfo = extraInfoStream.str();
225 error(line, " wrong operand type", op, extraInfo.c_str());
229 // Same error message for all binary operations don't work.
231 void TParseContext::binaryOpError(const TSourceLoc& line, const char* op, TString left, TString right)
233 std::stringstream extraInfoStream;
234 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
235 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
236 std::string extraInfo = extraInfoStream.str();
237 error(line, " wrong operand types ", op, extraInfo.c_str());
240 bool TParseContext::precisionErrorCheck(const TSourceLoc& line, TPrecision precision, TBasicType type){
241 if (!checksPrecisionErrors)
245 if( precision == EbpUndefined ){
246 error( line, "No precision specified for (float)", "" );
251 if( precision == EbpUndefined ){
252 error( line, "No precision specified (int)", "" );
263 // Both test and if necessary, spit out an error, to see if the node is really
264 // an l-value that can be operated on this way.
266 // Returns true if the was an error.
268 bool TParseContext::lValueErrorCheck(const TSourceLoc& line, const char* op, TIntermTyped* node)
270 TIntermSymbol* symNode = node->getAsSymbolNode();
271 TIntermBinary* binaryNode = node->getAsBinaryNode();
276 switch(binaryNode->getOp()) {
278 case EOpIndexIndirect:
279 case EOpIndexDirectStruct:
280 case EOpIndexDirectInterfaceBlock:
281 return lValueErrorCheck(line, op, binaryNode->getLeft());
282 case EOpVectorSwizzle:
283 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
285 int offset[4] = {0,0,0,0};
287 TIntermTyped* rightNode = binaryNode->getRight();
288 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
290 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
291 p != aggrNode->getSequence()->end(); p++) {
292 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
294 if (offset[value] > 1) {
295 error(line, " l-value of swizzle cannot have duplicate components", op);
306 error(line, " l-value required", op);
312 const char* symbol = 0;
314 symbol = symNode->getSymbol().c_str();
316 const char* message = 0;
317 switch (node->getQualifier()) {
318 case EvqConst: message = "can't modify a const"; break;
319 case EvqConstReadOnly: message = "can't modify a const"; break;
320 case EvqAttribute: message = "can't modify an attribute"; break;
321 case EvqFragmentIn: message = "can't modify an input"; break;
322 case EvqVertexIn: message = "can't modify an input"; break;
323 case EvqUniform: message = "can't modify a uniform"; break;
324 case EvqVaryingIn: message = "can't modify a varying"; break;
325 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
326 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
327 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
331 // Type that can't be written to?
333 if (node->getBasicType() == EbtVoid) {
334 message = "can't modify void";
336 if (IsSampler(node->getBasicType())) {
337 message = "can't modify a sampler";
341 if (message == 0 && binaryNode == 0 && symNode == 0) {
342 error(line, " l-value required", op);
349 // Everything else is okay, no error.
355 // If we get here, we have an error and a message.
358 std::stringstream extraInfoStream;
359 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
360 std::string extraInfo = extraInfoStream.str();
361 error(line, " l-value required", op, extraInfo.c_str());
364 std::stringstream extraInfoStream;
365 extraInfoStream << "(" << message << ")";
366 std::string extraInfo = extraInfoStream.str();
367 error(line, " l-value required", op, extraInfo.c_str());
374 // Both test, and if necessary spit out an error, to see if the node is really
377 // Returns true if the was an error.
379 bool TParseContext::constErrorCheck(TIntermTyped* node)
381 if (node->getQualifier() == EvqConst)
384 error(node->getLine(), "constant expression required", "");
390 // Both test, and if necessary spit out an error, to see if the node is really
393 // Returns true if the was an error.
395 bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
397 if (node->isScalarInt())
400 error(node->getLine(), "integer expression required", token);
406 // Both test, and if necessary spit out an error, to see if we are currently
409 // Returns true if the was an error.
411 bool TParseContext::globalErrorCheck(const TSourceLoc& line, bool global, const char* token)
416 error(line, "only allowed at global scope", token);
422 // For now, keep it simple: if it starts "gl_", it's reserved, independent
423 // of scope. Except, if the symbol table is at the built-in push-level,
424 // which is when we are parsing built-ins.
425 // Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
428 // Returns true if there was an error.
430 bool TParseContext::reservedErrorCheck(const TSourceLoc& line, const TString& identifier)
432 static const char* reservedErrMsg = "reserved built-in name";
433 if (!symbolTable.atBuiltInLevel()) {
434 if (identifier.compare(0, 3, "gl_") == 0) {
435 error(line, reservedErrMsg, "gl_");
438 if (IsWebGLBasedSpec(shaderSpec)) {
439 if (identifier.compare(0, 6, "webgl_") == 0) {
440 error(line, reservedErrMsg, "webgl_");
443 if (identifier.compare(0, 7, "_webgl_") == 0) {
444 error(line, reservedErrMsg, "_webgl_");
447 if (shaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0) {
448 error(line, reservedErrMsg, "css_");
452 if (identifier.find("__") != TString::npos) {
453 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
462 // Make sure there is enough data provided to the constructor to build
463 // something of the type of the constructor. Also returns the type of
466 // Returns true if there was an error in construction.
468 bool TParseContext::constructorErrorCheck(const TSourceLoc& line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
470 *type = function.getReturnType();
472 bool constructingMatrix = false;
474 case EOpConstructMat2:
475 case EOpConstructMat3:
476 case EOpConstructMat4:
477 constructingMatrix = true;
484 // Note: It's okay to have too many components available, but not okay to have unused
485 // arguments. 'full' will go to true when enough args have been seen. If we loop
486 // again, there is an extra argument, so 'overfull' will become true.
490 bool constType = true;
492 bool overFull = false;
493 bool matrixInMatrix = false;
494 bool arrayArg = false;
495 for (size_t i = 0; i < function.getParamCount(); ++i) {
496 const TParameter& param = function.getParam(i);
497 size += param.type->getObjectSize();
499 if (constructingMatrix && param.type->isMatrix())
500 matrixInMatrix = true;
503 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
505 if (param.type->getQualifier() != EvqConst)
507 if (param.type->isArray())
512 type->setQualifier(EvqConst);
514 if (type->isArray() && static_cast<size_t>(type->getArraySize()) != function.getParamCount()) {
515 error(line, "array constructor needs one argument per array element", "constructor");
519 if (arrayArg && op != EOpConstructStruct) {
520 error(line, "constructing from a non-dereferenced array", "constructor");
524 if (matrixInMatrix && !type->isArray()) {
525 if (function.getParamCount() != 1) {
526 error(line, "constructing matrix from matrix can only take one argument", "constructor");
532 error(line, "too many arguments", "constructor");
536 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount()) {
537 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
541 if (!type->isMatrix() || !matrixInMatrix) {
542 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
543 (op == EOpConstructStruct && size < type->getObjectSize())) {
544 error(line, "not enough data provided for construction", "constructor");
549 TIntermTyped *typed = node ? node->getAsTyped() : 0;
551 error(line, "constructor argument does not have a type", "constructor");
554 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
555 error(line, "cannot convert a sampler", "constructor");
558 if (typed->getBasicType() == EbtVoid) {
559 error(line, "cannot convert a void", "constructor");
566 // This function checks to see if a void variable has been declared and raise an error message for such a case
568 // returns true in case of an error
570 bool TParseContext::voidErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& pubType)
572 if (pubType.type == EbtVoid) {
573 error(line, "illegal use of type 'void'", identifier.c_str());
580 // This function checks to see if the node (for the expression) contains a scalar boolean expression or not
582 // returns true in case of an error
584 bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TIntermTyped* type)
586 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
587 error(line, "boolean expression expected", "");
594 // This function checks to see if the node (for the expression) contains a scalar boolean expression or not
596 // returns true in case of an error
598 bool TParseContext::boolErrorCheck(const TSourceLoc& line, const TPublicType& pType)
600 if (pType.type != EbtBool || pType.isAggregate()) {
601 error(line, "boolean expression expected", "");
608 bool TParseContext::samplerErrorCheck(const TSourceLoc& line, const TPublicType& pType, const char* reason)
610 if (pType.type == EbtStruct) {
611 if (containsSampler(*pType.userDef)) {
612 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
618 } else if (IsSampler(pType.type)) {
619 error(line, reason, getBasicString(pType.type));
627 bool TParseContext::structQualifierErrorCheck(const TSourceLoc& line, const TPublicType& pType)
629 switch (pType.qualifier)
636 if (pType.type == EbtStruct)
638 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
645 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
651 bool TParseContext::locationDeclaratorListCheck(const TSourceLoc& line, const TPublicType &pType)
653 if (pType.layoutQualifier.location != -1)
655 error(line, "location must only be specified for a single input or output variable", "location");
662 bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc& line, TQualifier qualifier, const TType& type)
664 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
665 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
666 error(line, "samplers cannot be output parameters", type.getBasicString());
673 bool TParseContext::containsSampler(TType& type)
675 if (IsSampler(type.getBasicType()))
678 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock()) {
679 const TFieldList& fields = type.getStruct()->fields();
680 for (unsigned int i = 0; i < fields.size(); ++i) {
681 if (containsSampler(*fields[i]->type()))
690 // Do size checking for an array type's size.
692 // Returns true if there was an error.
694 bool TParseContext::arraySizeErrorCheck(const TSourceLoc& line, TIntermTyped* expr, int& size)
696 TIntermConstantUnion* constant = expr->getAsConstantUnion();
698 if (constant == 0 || !constant->isScalarInt())
700 error(line, "array size must be a constant integer expression", "");
704 unsigned int unsignedSize = 0;
706 if (constant->getBasicType() == EbtUInt)
708 unsignedSize = constant->getUConst(0);
709 size = static_cast<int>(unsignedSize);
713 size = constant->getIConst(0);
717 error(line, "array size must be non-negative", "");
722 unsignedSize = static_cast<unsigned int>(size);
727 error(line, "array size must be greater than zero", "");
732 // The size of arrays is restricted here to prevent issues further down the
733 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
734 // 4096 registers so this should be reasonable even for aggressively optimizable code.
735 const unsigned int sizeLimit = 65536;
737 if (unsignedSize > sizeLimit)
739 error(line, "array size too large", "");
748 // See if this qualifier can be an array.
750 // Returns true if there is an error.
752 bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc& line, TPublicType type)
754 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) || (type.qualifier == EvqConst)) {
755 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
763 // See if this type can be an array.
765 // Returns true if there is an error.
767 bool TParseContext::arrayTypeErrorCheck(const TSourceLoc& line, TPublicType type)
770 // Can the type be an array?
773 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
781 // Do all the semantic checking for declaring an array, with and
782 // without a size, and make the right changes to the symbol table.
784 // size == 0 means no specified size.
786 // Returns true if there was an error.
788 bool TParseContext::arrayErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType &type, TVariable*& variable)
791 // Don't check for reserved word use until after we know it's not in the symbol table,
792 // because reserved arrays can be redeclared.
795 bool builtIn = false;
796 bool sameScope = false;
797 TSymbol* symbol = symbolTable.find(identifier, 0, &builtIn, &sameScope);
798 if (symbol == 0 || !sameScope) {
799 if (reservedErrorCheck(line, identifier))
802 variable = new TVariable(&identifier, TType(type));
805 variable->getType().setArraySize(type.arraySize);
807 if (! symbolTable.declare(variable)) {
809 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
813 if (! symbol->isVariable()) {
814 error(line, "variable expected", identifier.c_str());
818 variable = static_cast<TVariable*>(symbol);
819 if (! variable->getType().isArray()) {
820 error(line, "redeclaring non-array as array", identifier.c_str());
823 if (variable->getType().getArraySize() > 0) {
824 error(line, "redeclaration of array with size", identifier.c_str());
828 if (! variable->getType().sameElementType(TType(type))) {
829 error(line, "redeclaration of array with a different type", identifier.c_str());
834 variable->getType().setArraySize(type.arraySize);
837 if (voidErrorCheck(line, identifier, type))
844 // Enforce non-initializer type/qualifier rules.
846 // Returns true if there was an error.
848 bool TParseContext::nonInitConstErrorCheck(const TSourceLoc& line, const TString& identifier, TPublicType& type, bool array)
850 if (type.qualifier == EvqConst)
852 // Make the qualifier make sense.
853 type.qualifier = EvqTemporary;
857 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
859 else if (type.isStructureContainingArrays())
861 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
865 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
875 // Do semantic checking for a variable declaration that has no initializer,
876 // and update the symbol table.
878 // Returns true if there was an error.
880 bool TParseContext::nonInitErrorCheck(const TSourceLoc& line, const TString& identifier, const TPublicType& type, TVariable*& variable)
882 if (reservedErrorCheck(line, identifier))
885 variable = new TVariable(&identifier, TType(type));
887 if (! symbolTable.declare(variable)) {
888 error(line, "redefinition", variable->getName().c_str());
894 if (voidErrorCheck(line, identifier, type))
900 bool TParseContext::paramErrorCheck(const TSourceLoc& line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
902 if (qualifier != EvqConst && qualifier != EvqTemporary) {
903 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
906 if (qualifier == EvqConst && paramQualifier != EvqIn) {
907 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
911 if (qualifier == EvqConst)
912 type->setQualifier(EvqConstReadOnly);
914 type->setQualifier(paramQualifier);
919 bool TParseContext::extensionErrorCheck(const TSourceLoc& line, const TString& extension)
921 const TExtensionBehavior& extBehavior = extensionBehavior();
922 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
923 if (iter == extBehavior.end()) {
924 error(line, "extension", extension.c_str(), "is not supported");
927 // In GLSL ES, an extension's default behavior is "disable".
928 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
929 error(line, "extension", extension.c_str(), "is disabled");
932 if (iter->second == EBhWarn) {
933 warning(line, "extension", extension.c_str(), "is being used");
940 bool TParseContext::singleDeclarationErrorCheck(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
942 if (structQualifierErrorCheck(identifierLocation, publicType))
945 // check for layout qualifier issues
946 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
948 if (layoutQualifier.matrixPacking != EmpUnspecified)
950 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking), "only valid for interface blocks");
954 if (layoutQualifier.blockStorage != EbsUnspecified)
956 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage), "only valid for interface blocks");
960 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut && layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
968 bool TParseContext::layoutLocationErrorCheck(const TSourceLoc& location, const TLayoutQualifier &layoutQualifier)
970 if (layoutQualifier.location != -1)
972 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
979 bool TParseContext::supportsExtension(const char* extension)
981 const TExtensionBehavior& extbehavior = extensionBehavior();
982 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
983 return (iter != extbehavior.end());
986 bool TParseContext::isExtensionEnabled(const char* extension) const
988 const TExtensionBehavior& extbehavior = extensionBehavior();
989 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
991 if (iter == extbehavior.end())
996 return (iter->second == EBhEnable || iter->second == EBhRequire);
999 void TParseContext::handleExtensionDirective(const TSourceLoc& loc, const char* extName, const char* behavior)
1001 pp::SourceLocation srcLoc;
1002 srcLoc.file = loc.first_file;
1003 srcLoc.line = loc.first_line;
1004 directiveHandler.handleExtension(srcLoc, extName, behavior);
1007 void TParseContext::handlePragmaDirective(const TSourceLoc& loc, const char* name, const char* value, bool stdgl)
1009 pp::SourceLocation srcLoc;
1010 srcLoc.file = loc.first_file;
1011 srcLoc.line = loc.first_line;
1012 directiveHandler.handlePragma(srcLoc, name, value, stdgl);
1015 /////////////////////////////////////////////////////////////////////////////////
1019 /////////////////////////////////////////////////////////////////////////////////
1021 const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1022 const TString *name,
1023 const TSymbol *symbol)
1025 const TVariable *variable = NULL;
1029 error(location, "undeclared identifier", name->c_str());
1032 else if (!symbol->isVariable())
1034 error(location, "variable expected", name->c_str());
1039 variable = static_cast<const TVariable*>(symbol);
1041 if (symbolTable.findBuiltIn(variable->getName(), shaderVersion) &&
1042 !variable->getExtension().empty() &&
1043 extensionErrorCheck(location, variable->getExtension()))
1051 TType type(EbtFloat, EbpUndefined);
1052 TVariable *fakeVariable = new TVariable(name, type);
1053 symbolTable.declare(fakeVariable);
1054 variable = fakeVariable;
1061 // Look up a function name in the symbol table, and make sure it is a function.
1063 // Return the function symbol if found, otherwise 0.
1065 const TFunction* TParseContext::findFunction(const TSourceLoc& line, TFunction* call, int shaderVersion, bool *builtIn)
1067 // First find by unmangled name to check whether the function name has been
1068 // hidden by a variable name or struct typename.
1069 // If a function is found, check for one with a matching argument list.
1070 const TSymbol* symbol = symbolTable.find(call->getName(), shaderVersion, builtIn);
1071 if (symbol == 0 || symbol->isFunction()) {
1072 symbol = symbolTable.find(call->getMangledName(), shaderVersion, builtIn);
1076 error(line, "no matching overloaded function found", call->getName().c_str());
1080 if (!symbol->isFunction()) {
1081 error(line, "function name expected", call->getName().c_str());
1085 return static_cast<const TFunction*>(symbol);
1089 // Initializers show up in several places in the grammar. Have one set of
1090 // code to handle them here.
1092 // Returns true on error, false if no error
1094 bool TParseContext::executeInitializer(const TSourceLoc& line, const TString& identifier, TPublicType& pType,
1095 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
1097 TType type = TType(pType);
1099 if (variable == 0) {
1100 if (reservedErrorCheck(line, identifier))
1103 if (voidErrorCheck(line, identifier, pType))
1107 // add variable to symbol table
1109 variable = new TVariable(&identifier, type);
1110 if (! symbolTable.declare(variable)) {
1111 error(line, "redefinition", variable->getName().c_str());
1113 // don't delete variable, it's used by error recovery, and the pool
1114 // pop will take care of the memory
1119 // identifier must be of type constant, a global, or a temporary
1121 TQualifier qualifier = variable->getType().getQualifier();
1122 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1123 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1127 // test for and propagate constant
1130 if (qualifier == EvqConst) {
1131 if (qualifier != initializer->getType().getQualifier()) {
1132 std::stringstream extraInfoStream;
1133 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1134 std::string extraInfo = extraInfoStream.str();
1135 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1136 variable->getType().setQualifier(EvqTemporary);
1139 if (type != initializer->getType()) {
1140 error(line, " non-matching types for const initializer ",
1141 variable->getType().getQualifierString());
1142 variable->getType().setQualifier(EvqTemporary);
1145 if (initializer->getAsConstantUnion()) {
1146 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1147 } else if (initializer->getAsSymbolNode()) {
1148 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1149 const TVariable* tVar = static_cast<const TVariable*>(symbol);
1151 ConstantUnion* constArray = tVar->getConstPointer();
1152 variable->shareConstPointer(constArray);
1154 std::stringstream extraInfoStream;
1155 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1156 std::string extraInfo = extraInfoStream.str();
1157 error(line, " cannot assign to", "=", extraInfo.c_str());
1158 variable->getType().setQualifier(EvqTemporary);
1163 if (qualifier != EvqConst) {
1164 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1165 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1166 if (intermNode == 0) {
1167 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1176 bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1178 ASSERT(aggrNode != NULL);
1179 if (!aggrNode->isConstructor())
1182 bool allConstant = true;
1184 // check if all the child nodes are constants so that they can be inserted into
1186 TIntermSequence *sequence = aggrNode->getSequence() ;
1187 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p) {
1188 if (!(*p)->getAsTyped()->getAsConstantUnion())
1195 TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, TLayoutQualifier layoutQualifier, const TPublicType& typeSpecifier)
1197 TPublicType returnType = typeSpecifier;
1198 returnType.qualifier = qualifier;
1199 returnType.layoutQualifier = layoutQualifier;
1201 if (typeSpecifier.array)
1203 error(typeSpecifier.line, "not supported", "first-class array");
1205 returnType.setArray(false);
1208 if (shaderVersion < 300)
1210 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1212 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1216 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1217 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1219 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1231 case EvqCentroidOut:
1233 if (typeSpecifier.type == EbtBool)
1235 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1238 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1240 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1246 case EvqFragmentOut:
1249 if (typeSpecifier.type == EbtBool)
1251 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1263 TIntermAggregate* TParseContext::parseSingleDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier)
1265 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1266 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1268 if (identifier != "")
1270 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
1273 // this error check can mutate the type
1274 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1277 TVariable* variable = 0;
1279 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1282 if (variable && symbol)
1284 symbol->setId(variable->getUniqueId());
1291 TIntermAggregate* TParseContext::parseSingleArrayDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& indexLocation, TIntermTyped *indexExpression)
1293 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
1296 // this error check can mutate the type
1297 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1300 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1305 TPublicType arrayType = publicType;
1308 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1314 arrayType.setArray(true, size);
1317 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(arrayType), identifierLocation);
1318 TIntermAggregate* aggregate = intermediate.makeAggregate(symbol, identifierLocation);
1319 TVariable* variable = 0;
1321 if (arrayErrorCheck(identifierLocation, identifier, arrayType, variable))
1324 if (variable && symbol)
1326 symbol->setId(variable->getUniqueId());
1332 TIntermAggregate* TParseContext::parseSingleInitDeclaration(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
1334 if (singleDeclarationErrorCheck(publicType, identifierLocation, identifier))
1337 TIntermNode* intermNode;
1338 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1341 // Build intermediate representation
1343 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : NULL;
1352 TIntermAggregate* TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
1353 const TSourceLoc &identifierLoc,
1354 const TString *identifier,
1355 const TSymbol *symbol)
1357 // invariant declaration
1358 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1365 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1371 const TString kGlFrontFacing("gl_FrontFacing");
1372 if (*identifier == kGlFrontFacing)
1374 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1378 symbolTable.addInvariantVarying(*identifier);
1379 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1381 const TType &type = variable->getType();
1382 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1383 *identifier, type, identifierLoc);
1385 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1386 aggregate->setOp(EOpInvariantDeclaration);
1391 TIntermAggregate* TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration, TSymbol *identifierSymbol, const TSourceLoc& identifierLocation, const TString &identifier)
1393 TIntermSymbol* symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1394 TIntermAggregate* intermAggregate = intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
1396 if (structQualifierErrorCheck(identifierLocation, publicType))
1399 if (locationDeclaratorListCheck(identifierLocation, publicType))
1402 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, false))
1405 TVariable* variable = 0;
1406 if (nonInitErrorCheck(identifierLocation, identifier, publicType, variable))
1408 if (symbol && variable)
1409 symbol->setId(variable->getUniqueId());
1411 return intermAggregate;
1414 TIntermAggregate* TParseContext::parseArrayDeclarator(TPublicType &publicType, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& arrayLocation, TIntermNode *declaratorList, TIntermTyped *indexExpression)
1416 if (structQualifierErrorCheck(identifierLocation, publicType))
1419 if (locationDeclaratorListCheck(identifierLocation, publicType))
1422 if (nonInitConstErrorCheck(identifierLocation, identifier, publicType, true))
1425 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1429 else if (indexExpression)
1432 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
1434 TPublicType arrayType(publicType);
1435 arrayType.setArray(true, size);
1436 TVariable* variable = NULL;
1437 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1439 TType type = TType(arrayType);
1440 type.setArraySize(size);
1442 return intermediate.growAggregate(declaratorList, intermediate.addSymbol(variable ? variable->getUniqueId() : 0, identifier, type, identifierLocation), identifierLocation);
1446 TPublicType arrayType(publicType);
1447 arrayType.setArray(true);
1448 TVariable* variable = NULL;
1449 if (arrayErrorCheck(arrayLocation, identifier, arrayType, variable))
1456 TIntermAggregate* TParseContext::parseInitDeclarator(TPublicType &publicType, TIntermAggregate *declaratorList, const TSourceLoc& identifierLocation, const TString &identifier, const TSourceLoc& initLocation, TIntermTyped *initializer)
1458 if (structQualifierErrorCheck(identifierLocation, publicType))
1461 if (locationDeclaratorListCheck(identifierLocation, publicType))
1464 TIntermNode* intermNode;
1465 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, intermNode))
1468 // build the intermediate representation
1472 return intermediate.growAggregate(declaratorList, intermNode, initLocation);
1476 return declaratorList;
1486 void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1488 if (typeQualifier.qualifier != EvqUniform)
1490 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
1495 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1496 ASSERT(!layoutQualifier.isEmpty());
1498 if (shaderVersion < 300)
1500 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1505 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1511 if (layoutQualifier.matrixPacking != EmpUnspecified)
1513 defaultMatrixPacking = layoutQualifier.matrixPacking;
1516 if (layoutQualifier.blockStorage != EbsUnspecified)
1518 defaultBlockStorage = layoutQualifier.blockStorage;
1522 TFunction *TParseContext::addConstructorFunc(TPublicType publicType)
1524 TOperator op = EOpNull;
1525 if (publicType.userDef)
1527 op = EOpConstructStruct;
1531 switch (publicType.type)
1534 if (publicType.isMatrix())
1536 // TODO: non-square matrices
1537 switch(publicType.getCols())
1539 case 2: op = EOpConstructMat2; break;
1540 case 3: op = EOpConstructMat3; break;
1541 case 4: op = EOpConstructMat4; break;
1546 switch(publicType.getNominalSize())
1548 case 1: op = EOpConstructFloat; break;
1549 case 2: op = EOpConstructVec2; break;
1550 case 3: op = EOpConstructVec3; break;
1551 case 4: op = EOpConstructVec4; break;
1557 switch(publicType.getNominalSize())
1559 case 1: op = EOpConstructInt; break;
1560 case 2: op = EOpConstructIVec2; break;
1561 case 3: op = EOpConstructIVec3; break;
1562 case 4: op = EOpConstructIVec4; break;
1567 switch(publicType.getNominalSize())
1569 case 1: op = EOpConstructUInt; break;
1570 case 2: op = EOpConstructUVec2; break;
1571 case 3: op = EOpConstructUVec3; break;
1572 case 4: op = EOpConstructUVec4; break;
1577 switch(publicType.getNominalSize())
1579 case 1: op = EOpConstructBool; break;
1580 case 2: op = EOpConstructBVec2; break;
1581 case 3: op = EOpConstructBVec3; break;
1582 case 4: op = EOpConstructBVec4; break;
1591 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1593 publicType.type = EbtFloat;
1594 op = EOpConstructFloat;
1599 TType type(publicType);
1600 return new TFunction(&tempString, type, op);
1603 // This function is used to test for the correctness of the parameters passed to various constructor functions
1604 // and also convert them to the right datatype if it is allowed and required.
1606 // Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1608 TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, const TType *type, TOperator op, TFunction *fnCall, const TSourceLoc &line)
1610 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
1612 if (!aggregateArguments)
1614 aggregateArguments = new TIntermAggregate;
1615 aggregateArguments->getSequence()->push_back(arguments);
1618 if (op == EOpConstructStruct)
1620 const TFieldList &fields = type->getStruct()->fields();
1621 TIntermSequence *args = aggregateArguments->getSequence();
1623 for (size_t i = 0; i < fields.size(); i++)
1625 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
1627 error(line, "Structure constructor arguments do not match structure fields", "Error");
1635 // Turn the argument list itself into a constructor
1636 TIntermTyped *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1637 TIntermTyped *constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1638 if (constConstructor)
1640 return constConstructor;
1646 TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1648 bool canBeFolded = areAllChildConst(aggrNode);
1649 aggrNode->setType(type);
1651 bool returnVal = false;
1652 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
1653 if (aggrNode->getSequence()->size() == 1) {
1654 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
1657 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
1662 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1669 // This function returns the tree representation for the vector field(s) being accessed from contant vector.
1670 // If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1671 // returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1672 // node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1673 // a constant matrix.
1675 TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, const TSourceLoc& line)
1677 TIntermTyped* typedNode;
1678 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1680 ConstantUnion *unionArray;
1681 if (tempConstantNode) {
1682 unionArray = tempConstantNode->getUnionArrayPointer();
1687 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1688 error(line, "Cannot offset into the vector", "Error");
1694 ConstantUnion* constArray = new ConstantUnion[fields.num];
1696 for (int i = 0; i < fields.num; i++) {
1697 if (fields.offsets[i] >= node->getType().getNominalSize()) {
1698 std::stringstream extraInfoStream;
1699 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1700 std::string extraInfo = extraInfoStream.str();
1701 error(line, "", "[", extraInfo.c_str());
1703 fields.offsets[i] = 0;
1706 constArray[i] = unionArray[fields.offsets[i]];
1709 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1714 // This function returns the column being accessed from a constant matrix. The values are retrieved from
1715 // the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1716 // to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1717 // constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1719 TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, const TSourceLoc& line)
1721 TIntermTyped* typedNode;
1722 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1724 if (index >= node->getType().getCols()) {
1725 std::stringstream extraInfoStream;
1726 extraInfoStream << "matrix field selection out of range '" << index << "'";
1727 std::string extraInfo = extraInfoStream.str();
1728 error(line, "", "[", extraInfo.c_str());
1733 if (tempConstantNode) {
1734 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1735 int size = tempConstantNode->getType().getCols();
1736 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1738 error(line, "Cannot offset into the matrix", "Error");
1749 // This function returns an element of an array accessed from a constant array. The values are retrieved from
1750 // the symbol table and parse-tree is built for the type of the element. The input
1751 // to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1752 // constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1754 TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, const TSourceLoc& line)
1756 TIntermTyped* typedNode;
1757 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1758 TType arrayElementType = node->getType();
1759 arrayElementType.clearArrayness();
1761 if (index >= node->getType().getArraySize()) {
1762 std::stringstream extraInfoStream;
1763 extraInfoStream << "array field selection out of range '" << index << "'";
1764 std::string extraInfo = extraInfoStream.str();
1765 error(line, "", "[", extraInfo.c_str());
1770 if (tempConstantNode) {
1771 size_t arrayElementSize = arrayElementType.getObjectSize();
1772 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1773 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1775 error(line, "Cannot offset into the array", "Error");
1786 // This function returns the value of a particular field inside a constant structure from the symbol table.
1787 // If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1788 // function and returns the parse-tree with the values of the embedded/nested struct.
1790 TIntermTyped* TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc& line)
1792 const TFieldList& fields = node->getType().getStruct()->fields();
1793 size_t instanceSize = 0;
1795 for (size_t index = 0; index < fields.size(); ++index) {
1796 if (fields[index]->name() == identifier) {
1799 instanceSize += fields[index]->type()->getObjectSize();
1803 TIntermTyped *typedNode;
1804 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
1805 if (tempConstantNode) {
1806 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
1808 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1810 error(line, "Cannot offset into the structure", "Error");
1820 // Interface/uniform blocks
1822 TIntermAggregate* TParseContext::addInterfaceBlock(const TPublicType& typeQualifier, const TSourceLoc& nameLine, const TString& blockName, TFieldList* fieldList,
1823 const TString* instanceName, const TSourceLoc& instanceLine, TIntermTyped* arrayIndex, const TSourceLoc& arrayIndexLine)
1825 if (reservedErrorCheck(nameLine, blockName))
1828 if (typeQualifier.qualifier != EvqUniform)
1830 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier), "interface blocks must be uniform");
1834 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
1835 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
1840 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
1842 blockLayoutQualifier.matrixPacking = defaultMatrixPacking;
1845 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
1847 blockLayoutQualifier.blockStorage = defaultBlockStorage;
1850 TSymbol* blockNameSymbol = new TInterfaceBlockName(&blockName);
1851 if (!symbolTable.declare(blockNameSymbol)) {
1852 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
1856 // check for sampler types and apply layout qualifiers
1857 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex) {
1858 TField* field = (*fieldList)[memberIndex];
1859 TType* fieldType = field->type();
1860 if (IsSampler(fieldType->getBasicType())) {
1861 error(field->line(), "unsupported type", fieldType->getBasicString(), "sampler types are not allowed in interface blocks");
1865 const TQualifier qualifier = fieldType->getQualifier();
1872 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
1877 // check layout qualifiers
1878 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
1879 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
1884 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
1886 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
1890 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
1892 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
1894 else if (!fieldType->isMatrix())
1896 error(field->line(), "invalid layout qualifier:", getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
1900 fieldType->setLayoutQualifier(fieldLayoutQualifier);
1905 if (arrayIndex != NULL)
1907 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
1911 TInterfaceBlock* interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
1912 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
1914 TString symbolName = "";
1919 // define symbols for the members of the interface block
1920 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
1922 TField* field = (*fieldList)[memberIndex];
1923 TType* fieldType = field->type();
1925 // set parent pointer of the field variable
1926 fieldType->setInterfaceBlock(interfaceBlock);
1928 TVariable* fieldVariable = new TVariable(&field->name(), *fieldType);
1929 fieldVariable->setQualifier(typeQualifier.qualifier);
1931 if (!symbolTable.declare(fieldVariable)) {
1932 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
1939 // add a symbol for this interface block
1940 TVariable* instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
1941 instanceTypeDef->setQualifier(typeQualifier.qualifier);
1943 if (!symbolTable.declare(instanceTypeDef)) {
1944 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
1948 symbolId = instanceTypeDef->getUniqueId();
1949 symbolName = instanceTypeDef->getName();
1952 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line), nameLine);
1953 aggregate->setOp(EOpDeclaration);
1955 exitStructDeclaration();
1959 bool TParseContext::enterStructDeclaration(const TSourceLoc& line, const TString& identifier)
1961 ++structNestingLevel;
1963 // Embedded structure definitions are not supported per GLSL ES spec.
1964 // They aren't allowed in GLSL either, but we need to detect this here
1965 // so we don't rely on the GLSL compiler to catch it.
1966 if (structNestingLevel > 1) {
1967 error(line, "", "Embedded struct definitions are not allowed");
1974 void TParseContext::exitStructDeclaration()
1976 --structNestingLevel;
1981 const int kWebGLMaxStructNesting = 4;
1985 bool TParseContext::structNestingErrorCheck(const TSourceLoc& line, const TField& field)
1987 if (!IsWebGLBasedSpec(shaderSpec)) {
1991 if (field.type()->getBasicType() != EbtStruct) {
1995 // We're already inside a structure definition at this point, so add
1996 // one to the field's struct nesting.
1997 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting) {
1998 std::stringstream reasonStream;
1999 reasonStream << "Reference of struct type "
2000 << field.type()->getStruct()->name().c_str()
2001 << " exceeds maximum allowed nesting level of "
2002 << kWebGLMaxStructNesting;
2003 std::string reason = reasonStream.str();
2004 error(line, reason.c_str(), field.name().c_str(), "");
2012 // Parse an array index expression
2014 TIntermTyped* TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc& location, TIntermTyped *indexExpression)
2016 TIntermTyped *indexedExpression = NULL;
2018 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2020 if (baseExpression->getAsSymbolNode())
2022 error(location, " left of '[' is not of type array, matrix, or vector ", baseExpression->getAsSymbolNode()->getSymbol().c_str());
2026 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2031 if (indexExpression->getQualifier() == EvqConst)
2033 int index = indexExpression->getAsConstantUnion()->getIConst(0);
2036 std::stringstream infoStream;
2037 infoStream << index;
2038 std::string info = infoStream.str();
2039 error(location, "negative index", info.c_str());
2043 if (baseExpression->getType().getQualifier() == EvqConst)
2045 if (baseExpression->isArray())
2047 // constant folding for arrays
2048 indexedExpression = addConstArrayNode(index, baseExpression, location);
2050 else if (baseExpression->isVector())
2052 // constant folding for vectors
2053 TVectorFields fields;
2055 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2056 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2058 else if (baseExpression->isMatrix())
2060 // constant folding for matrices
2061 indexedExpression = addConstMatrixNode(index, baseExpression, location);
2066 if (baseExpression->isArray())
2068 if (index >= baseExpression->getType().getArraySize())
2070 std::stringstream extraInfoStream;
2071 extraInfoStream << "array index out of range '" << index << "'";
2072 std::string extraInfo = extraInfoStream.str();
2073 error(location, "", "[", extraInfo.c_str());
2075 index = baseExpression->getType().getArraySize() - 1;
2077 else if (baseExpression->getQualifier() == EvqFragData && index > 0 && !isExtensionEnabled("GL_EXT_draw_buffers"))
2079 error(location, "", "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
2084 else if ((baseExpression->isVector() || baseExpression->isMatrix()) && baseExpression->getType().getNominalSize() <= index)
2086 std::stringstream extraInfoStream;
2087 extraInfoStream << "field selection out of range '" << index << "'";
2088 std::string extraInfo = extraInfoStream.str();
2089 error(location, "", "[", extraInfo.c_str());
2091 index = baseExpression->getType().getNominalSize() - 1;
2094 indexExpression->getAsConstantUnion()->getUnionArrayPointer()->setIConst(index);
2095 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
2100 if (baseExpression->isInterfaceBlock())
2102 error(location, "", "[", "array indexes for interface blocks arrays must be constant integral expressions");
2105 else if (baseExpression->getQualifier() == EvqFragmentOut)
2107 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
2111 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2114 if (indexedExpression == 0)
2116 ConstantUnion *unionArray = new ConstantUnion[1];
2117 unionArray->setFConst(0.0f);
2118 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2120 else if (baseExpression->isArray())
2122 const TType &baseType = baseExpression->getType();
2123 if (baseType.getStruct())
2125 TType copyOfType(baseType.getStruct());
2126 indexedExpression->setType(copyOfType);
2128 else if (baseType.isInterfaceBlock())
2130 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
2131 indexedExpression->setType(copyOfType);
2135 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, baseExpression->getNominalSize(), baseExpression->getSecondarySize()));
2138 if (baseExpression->getType().getQualifier() == EvqConst)
2140 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2143 else if (baseExpression->isMatrix())
2145 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2146 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier, baseExpression->getRows()));
2148 else if (baseExpression->isVector())
2150 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2151 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
2155 indexedExpression->setType(baseExpression->getType());
2158 return indexedExpression;
2161 TIntermTyped* TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc& dotLocation, const TString &fieldString, const TSourceLoc& fieldLocation)
2163 TIntermTyped *indexedExpression = NULL;
2165 if (baseExpression->isArray())
2167 error(fieldLocation, "cannot apply dot operator to an array", ".");
2171 if (baseExpression->isVector())
2173 TVectorFields fields;
2174 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2177 fields.offsets[0] = 0;
2181 if (baseExpression->getType().getQualifier() == EvqConst)
2183 // constant folding for vector fields
2184 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2185 if (indexedExpression == 0)
2188 indexedExpression = baseExpression;
2192 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqConst, (int) (fieldString).size()));
2197 TString vectorString = fieldString;
2198 TIntermTyped* index = intermediate.addSwizzle(fields, fieldLocation);
2199 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
2200 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary, (int) vectorString.size()));
2203 else if (baseExpression->isMatrix())
2205 TMatrixFields fields;
2206 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields, fieldLocation))
2208 fields.wholeRow = false;
2209 fields.wholeCol = false;
2215 if (fields.wholeRow || fields.wholeCol)
2217 error(dotLocation, " non-scalar fields not implemented yet", ".");
2219 ConstantUnion *unionArray = new ConstantUnion[1];
2220 unionArray->setIConst(0);
2221 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2222 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2223 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),EvqTemporary, baseExpression->getCols(), baseExpression->getRows()));
2227 ConstantUnion *unionArray = new ConstantUnion[1];
2228 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
2229 TIntermTyped* index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
2230 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2231 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2234 else if (baseExpression->getBasicType() == EbtStruct)
2236 bool fieldFound = false;
2237 const TFieldList& fields = baseExpression->getType().getStruct()->fields();
2240 error(dotLocation, "structure has no fields", "Internal Error");
2242 indexedExpression = baseExpression;
2247 for (i = 0; i < fields.size(); ++i)
2249 if (fields[i]->name() == fieldString)
2257 if (baseExpression->getType().getQualifier() == EvqConst)
2259 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2260 if (indexedExpression == 0)
2263 indexedExpression = baseExpression;
2267 indexedExpression->setType(*fields[i]->type());
2268 // change the qualifier of the return type, not of the structure field
2269 // as the structure definition is shared between various structures.
2270 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2275 ConstantUnion *unionArray = new ConstantUnion[1];
2276 unionArray->setIConst(i);
2277 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2278 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
2279 indexedExpression->setType(*fields[i]->type());
2284 error(dotLocation, " no such field in structure", fieldString.c_str());
2286 indexedExpression = baseExpression;
2290 else if (baseExpression->isInterfaceBlock())
2292 bool fieldFound = false;
2293 const TFieldList& fields = baseExpression->getType().getInterfaceBlock()->fields();
2296 error(dotLocation, "interface block has no fields", "Internal Error");
2298 indexedExpression = baseExpression;
2303 for (i = 0; i < fields.size(); ++i)
2305 if (fields[i]->name() == fieldString)
2313 ConstantUnion *unionArray = new ConstantUnion[1];
2314 unionArray->setIConst(i);
2315 TIntermTyped* index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2316 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index, dotLocation);
2317 indexedExpression->setType(*fields[i]->type());
2321 error(dotLocation, " no such field in interface block", fieldString.c_str());
2323 indexedExpression = baseExpression;
2329 if (shaderVersion < 300)
2331 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side", fieldString.c_str());
2335 error(dotLocation, " field selection requires structure, vector, matrix, or interface block on left hand side", fieldString.c_str());
2338 indexedExpression = baseExpression;
2341 return indexedExpression;
2344 TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine)
2346 TLayoutQualifier qualifier;
2348 qualifier.location = -1;
2349 qualifier.matrixPacking = EmpUnspecified;
2350 qualifier.blockStorage = EbsUnspecified;
2352 if (qualifierType == "shared")
2354 qualifier.blockStorage = EbsShared;
2356 else if (qualifierType == "packed")
2358 qualifier.blockStorage = EbsPacked;
2360 else if (qualifierType == "std140")
2362 qualifier.blockStorage = EbsStd140;
2364 else if (qualifierType == "row_major")
2366 qualifier.matrixPacking = EmpRowMajor;
2368 else if (qualifierType == "column_major")
2370 qualifier.matrixPacking = EmpColumnMajor;
2372 else if (qualifierType == "location")
2374 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2379 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2386 TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc& qualifierTypeLine, const TString &intValueString, int intValue, const TSourceLoc& intValueLine)
2388 TLayoutQualifier qualifier;
2390 qualifier.location = -1;
2391 qualifier.matrixPacking = EmpUnspecified;
2392 qualifier.blockStorage = EbsUnspecified;
2394 if (qualifierType != "location")
2396 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "only location may have arguments");
2401 // must check that location is non-negative
2404 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
2409 qualifier.location = intValue;
2416 TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
2418 TLayoutQualifier joinedQualifier = leftQualifier;
2420 if (rightQualifier.location != -1)
2422 joinedQualifier.location = rightQualifier.location;
2424 if (rightQualifier.matrixPacking != EmpUnspecified)
2426 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2428 if (rightQualifier.blockStorage != EbsUnspecified)
2430 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2433 return joinedQualifier;
2436 TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc, TQualifier interpolationQualifier,
2437 const TSourceLoc &storageLoc, TQualifier storageQualifier)
2439 TQualifier mergedQualifier = EvqSmoothIn;
2441 if (storageQualifier == EvqFragmentIn) {
2442 if (interpolationQualifier == EvqSmooth)
2443 mergedQualifier = EvqSmoothIn;
2444 else if (interpolationQualifier == EvqFlat)
2445 mergedQualifier = EvqFlatIn;
2448 else if (storageQualifier == EvqCentroidIn) {
2449 if (interpolationQualifier == EvqSmooth)
2450 mergedQualifier = EvqCentroidIn;
2451 else if (interpolationQualifier == EvqFlat)
2452 mergedQualifier = EvqFlatIn;
2455 else if (storageQualifier == EvqVertexOut) {
2456 if (interpolationQualifier == EvqSmooth)
2457 mergedQualifier = EvqSmoothOut;
2458 else if (interpolationQualifier == EvqFlat)
2459 mergedQualifier = EvqFlatOut;
2462 else if (storageQualifier == EvqCentroidOut) {
2463 if (interpolationQualifier == EvqSmooth)
2464 mergedQualifier = EvqCentroidOut;
2465 else if (interpolationQualifier == EvqFlat)
2466 mergedQualifier = EvqFlatOut;
2470 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString(interpolationQualifier));
2473 mergedQualifier = storageQualifier;
2477 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2481 TFieldList *TParseContext::addStructDeclaratorList(const TPublicType& typeSpecifier, TFieldList *fieldList)
2483 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier)) {
2487 for (unsigned int i = 0; i < fieldList->size(); ++i) {
2489 // Careful not to replace already known aspects of type, like array-ness
2491 TType* type = (*fieldList)[i]->type();
2492 type->setBasicType(typeSpecifier.type);
2493 type->setPrimarySize(typeSpecifier.primarySize);
2494 type->setSecondarySize(typeSpecifier.secondarySize);
2495 type->setPrecision(typeSpecifier.precision);
2496 type->setQualifier(typeSpecifier.qualifier);
2497 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
2499 // don't allow arrays of arrays
2500 if (type->isArray()) {
2501 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2504 if (typeSpecifier.array)
2505 type->setArraySize(typeSpecifier.arraySize);
2506 if (typeSpecifier.userDef) {
2507 type->setStruct(typeSpecifier.userDef->getStruct());
2510 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i])) {
2518 TPublicType TParseContext::addStructure(const TSourceLoc& structLine, const TSourceLoc& nameLine, const TString *structName, TFieldList* fieldList)
2520 TStructure* structure = new TStructure(structName, fieldList);
2521 TType* structureType = new TType(structure);
2523 structure->setUniqueId(TSymbolTable::nextUniqueId());
2525 if (!structName->empty())
2527 if (reservedErrorCheck(nameLine, *structName))
2531 TVariable* userTypeDef = new TVariable(structName, *structureType, true);
2532 if (!symbolTable.declare(userTypeDef)) {
2533 error(nameLine, "redefinition", structName->c_str(), "struct");
2538 // ensure we do not specify any storage qualifiers on the struct members
2539 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
2541 const TField &field = *(*fieldList)[typeListIndex];
2542 const TQualifier qualifier = field.type()->getQualifier();
2549 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
2555 TPublicType publicType;
2556 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
2557 publicType.userDef = structureType;
2558 exitStructDeclaration();
2564 // Parse an array of strings using yyparse.
2566 // Returns 0 for success.
2568 int PaParseStrings(size_t count, const char* const string[], const int length[],
2569 TParseContext* context) {
2570 if ((count == 0) || (string == NULL))
2573 if (glslang_initialize(context))
2576 int error = glslang_scan(count, string, length, context);
2578 error = glslang_parse(context);
2580 glslang_finalize(context);
2582 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;