2 // Copyright (c) 2002-2013 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 "ParseHelper.h"
13 #include "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, int 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;
78 fields.offsets[i] = 3;
82 fields.offsets[i] = 3;
86 fields.offsets[i] = 3;
90 error(line, "illegal vector field selection", compString.c_str());
95 for (int i = 0; i < fields.num; ++i) {
96 if (fields.offsets[i] >= vecSize) {
97 error(line, "vector field selection out of range", compString.c_str());
102 if (fieldSet[i] != fieldSet[i-1]) {
103 error(line, "illegal - vector component fields not from the same set", compString.c_str());
114 // Look at a '.' field selector string and change it into offsets
117 bool TParseContext::parseMatrixFields(const TString& compString, int matSize, TMatrixFields& fields, int line)
119 fields.wholeRow = false;
120 fields.wholeCol = false;
124 if (compString.size() != 2) {
125 error(line, "illegal length of matrix field selection", compString.c_str());
129 if (compString[0] == '_') {
130 if (compString[1] < '0' || compString[1] > '3') {
131 error(line, "illegal matrix field selection", compString.c_str());
134 fields.wholeCol = true;
135 fields.col = compString[1] - '0';
136 } else if (compString[1] == '_') {
137 if (compString[0] < '0' || compString[0] > '3') {
138 error(line, "illegal matrix field selection", compString.c_str());
141 fields.wholeRow = true;
142 fields.row = compString[0] - '0';
144 if (compString[0] < '0' || compString[0] > '3' ||
145 compString[1] < '0' || compString[1] > '3') {
146 error(line, "illegal matrix field selection", compString.c_str());
149 fields.row = compString[0] - '0';
150 fields.col = compString[1] - '0';
153 if (fields.row >= matSize || fields.col >= matSize) {
154 error(line, "matrix field selection out of range", compString.c_str());
161 ///////////////////////////////////////////////////////////////////////
165 ////////////////////////////////////////////////////////////////////////
168 // Track whether errors have occurred.
170 void TParseContext::recover()
175 // Used by flex/bison to output all syntax and parsing errors.
177 void TParseContext::error(TSourceLoc loc,
178 const char* reason, const char* token,
179 const char* extraInfo)
181 pp::SourceLocation srcLoc;
182 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
183 diagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
184 srcLoc, reason, token, extraInfo);
188 void TParseContext::warning(TSourceLoc loc,
189 const char* reason, const char* token,
190 const char* extraInfo) {
191 pp::SourceLocation srcLoc;
192 DecodeSourceLoc(loc, &srcLoc.file, &srcLoc.line);
193 diagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
194 srcLoc, reason, token, extraInfo);
197 void TParseContext::trace(const char* str)
199 diagnostics.writeDebug(str);
203 // Same error message for all places assignments don't work.
205 void TParseContext::assignError(int line, const char* op, TString left, TString right)
207 std::stringstream extraInfoStream;
208 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
209 std::string extraInfo = extraInfoStream.str();
210 error(line, "", op, extraInfo.c_str());
214 // Same error message for all places unary operations don't work.
216 void TParseContext::unaryOpError(int line, const char* op, TString operand)
218 std::stringstream extraInfoStream;
219 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
220 << " (or there is no acceptable conversion)";
221 std::string extraInfo = extraInfoStream.str();
222 error(line, " wrong operand type", op, extraInfo.c_str());
226 // Same error message for all binary operations don't work.
228 void TParseContext::binaryOpError(int line, const char* op, TString left, TString right)
230 std::stringstream extraInfoStream;
231 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
232 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
233 std::string extraInfo = extraInfoStream.str();
234 error(line, " wrong operand types ", op, extraInfo.c_str());
237 bool TParseContext::precisionErrorCheck(int line, TPrecision precision, TBasicType type){
238 if (!checksPrecisionErrors)
242 if( precision == EbpUndefined ){
243 error( line, "No precision specified for (float)", "" );
248 if( precision == EbpUndefined ){
249 error( line, "No precision specified (int)", "" );
260 // Both test and if necessary, spit out an error, to see if the node is really
261 // an l-value that can be operated on this way.
263 // Returns true if the was an error.
265 bool TParseContext::lValueErrorCheck(int line, const char* op, TIntermTyped* node)
267 TIntermSymbol* symNode = node->getAsSymbolNode();
268 TIntermBinary* binaryNode = node->getAsBinaryNode();
273 switch(binaryNode->getOp()) {
275 case EOpIndexIndirect:
276 case EOpIndexDirectStruct:
277 return lValueErrorCheck(line, op, binaryNode->getLeft());
278 case EOpVectorSwizzle:
279 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
281 int offset[4] = {0,0,0,0};
283 TIntermTyped* rightNode = binaryNode->getRight();
284 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
286 for (TIntermSequence::iterator p = aggrNode->getSequence().begin();
287 p != aggrNode->getSequence().end(); p++) {
288 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
290 if (offset[value] > 1) {
291 error(line, " l-value of swizzle cannot have duplicate components", op);
302 error(line, " l-value required", op);
308 const char* symbol = 0;
310 symbol = symNode->getSymbol().c_str();
312 const char* message = 0;
313 switch (node->getQualifier()) {
314 case EvqConst: message = "can't modify a const"; break;
315 case EvqConstReadOnly: message = "can't modify a const"; break;
316 case EvqAttribute: message = "can't modify an attribute"; break;
317 case EvqUniform: message = "can't modify a uniform"; break;
318 case EvqVaryingIn: message = "can't modify a varying"; break;
319 case EvqInput: message = "can't modify an input"; break;
320 case EvqFragCoord: message = "can't modify gl_FragCoord"; break;
321 case EvqFrontFacing: message = "can't modify gl_FrontFacing"; break;
322 case EvqPointCoord: message = "can't modify gl_PointCoord"; break;
326 // Type that can't be written to?
328 if(IsSampler(node->getBasicType()))
330 message = "can't modify a sampler";
332 else if(node->getBasicType() == EbtVoid)
334 message = "can't modify void";
338 if (message == 0 && binaryNode == 0 && symNode == 0) {
339 error(line, " l-value required", op);
346 // Everything else is okay, no error.
352 // If we get here, we have an error and a message.
355 std::stringstream extraInfoStream;
356 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
357 std::string extraInfo = extraInfoStream.str();
358 error(line, " l-value required", op, extraInfo.c_str());
361 std::stringstream extraInfoStream;
362 extraInfoStream << "(" << message << ")";
363 std::string extraInfo = extraInfoStream.str();
364 error(line, " l-value required", op, extraInfo.c_str());
371 // Both test, and if necessary spit out an error, to see if the node is really
374 // Returns true if the was an error.
376 bool TParseContext::constErrorCheck(TIntermTyped* node)
378 if (node->getQualifier() == EvqConst)
381 error(node->getLine(), "constant expression required", "");
387 // Both test, and if necessary spit out an error, to see if the node is really
390 // Returns true if the was an error.
392 bool TParseContext::integerErrorCheck(TIntermTyped* node, const char* token)
394 if (node->getBasicType() == EbtInt && node->getNominalSize() == 1)
397 error(node->getLine(), "integer expression required", token);
403 // Both test, and if necessary spit out an error, to see if we are currently
406 // Returns true if the was an error.
408 bool TParseContext::globalErrorCheck(int line, bool global, const char* token)
413 error(line, "only allowed at global scope", token);
419 // For now, keep it simple: if it starts "gl_", it's reserved, independent
420 // of scope. Except, if the symbol table is at the built-in push-level,
421 // which is when we are parsing built-ins.
422 // Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
425 // Returns true if there was an error.
427 bool TParseContext::reservedErrorCheck(int line, const TString& identifier)
429 static const char* reservedErrMsg = "reserved built-in name";
430 if (!symbolTable.atBuiltInLevel()) {
431 if (identifier.compare(0, 3, "gl_") == 0) {
432 error(line, reservedErrMsg, "gl_");
435 if (identifier.find("__") != TString::npos) {
436 error(line, "identifiers containing two consecutive underscores (__) are reserved as possible future keywords", identifier.c_str());
445 // Make sure there is enough data provided to the constructor to build
446 // something of the type of the constructor. Also returns the type of
449 // Returns true if there was an error in construction.
451 bool TParseContext::constructorErrorCheck(int line, TIntermNode* node, TFunction& function, TOperator op, TType* type)
453 *type = function.getReturnType();
455 bool constructingMatrix = false;
457 case EOpConstructMat2:
458 case EOpConstructMat3:
459 case EOpConstructMat4:
460 constructingMatrix = true;
467 // Note: It's okay to have too many components available, but not okay to have unused
468 // arguments. 'full' will go to true when enough args have been seen. If we loop
469 // again, there is an extra argument, so 'overfull' will become true.
473 bool constType = true;
475 bool overFull = false;
476 bool matrixInMatrix = false;
477 bool arrayArg = false;
478 for (int i = 0; i < function.getParamCount(); ++i) {
479 const TParameter& param = function.getParam(i);
480 size += param.type->getObjectSize();
482 if (constructingMatrix && param.type->isMatrix())
483 matrixInMatrix = true;
486 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
488 if (param.type->getQualifier() != EvqConst)
490 if (param.type->isArray())
495 type->setQualifier(EvqConst);
497 if (type->isArray() && type->getArraySize() != function.getParamCount()) {
498 error(line, "array constructor needs one argument per array element", "constructor");
502 if (arrayArg && op != EOpConstructStruct) {
503 error(line, "constructing from a non-dereferenced array", "constructor");
507 if (matrixInMatrix && !type->isArray()) {
508 if (function.getParamCount() != 1) {
509 error(line, "constructing matrix from matrix can only take one argument", "constructor");
515 error(line, "too many arguments", "constructor");
519 if (op == EOpConstructStruct && !type->isArray() && int(type->getStruct()->size()) != function.getParamCount()) {
520 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
524 if (!type->isMatrix() || !matrixInMatrix) {
525 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
526 (op == EOpConstructStruct && size < type->getObjectSize())) {
527 error(line, "not enough data provided for construction", "constructor");
532 TIntermTyped *typed = node ? node->getAsTyped() : 0;
534 error(line, "constructor argument does not have a type", "constructor");
537 if (op != EOpConstructStruct && IsSampler(typed->getBasicType())) {
538 error(line, "cannot convert a sampler", "constructor");
541 if (typed->getBasicType() == EbtVoid) {
542 error(line, "cannot convert a void", "constructor");
549 // This function checks to see if a void variable has been declared and raise an error message for such a case
551 // returns true in case of an error
553 bool TParseContext::voidErrorCheck(int line, const TString& identifier, const TPublicType& pubType)
555 if (pubType.type == EbtVoid) {
556 error(line, "illegal use of type 'void'", identifier.c_str());
563 // This function checks to see if the node (for the expression) contains a scalar boolean expression or not
565 // returns true in case of an error
567 bool TParseContext::boolErrorCheck(int line, const TIntermTyped* type)
569 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector()) {
570 error(line, "boolean expression expected", "");
577 // This function checks to see if the node (for the expression) contains a scalar boolean expression or not
579 // returns true in case of an error
581 bool TParseContext::boolErrorCheck(int line, const TPublicType& pType)
583 if (pType.type != EbtBool || pType.array || pType.matrix || (pType.size > 1)) {
584 error(line, "boolean expression expected", "");
591 bool TParseContext::samplerErrorCheck(int line, const TPublicType& pType, const char* reason)
593 if (pType.type == EbtStruct) {
594 if (containsSampler(*pType.userDef)) {
595 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
601 } else if (IsSampler(pType.type)) {
602 error(line, reason, getBasicString(pType.type));
610 bool TParseContext::structQualifierErrorCheck(int line, const TPublicType& pType)
612 if ((pType.qualifier == EvqVaryingIn || pType.qualifier == EvqVaryingOut || pType.qualifier == EvqAttribute) &&
613 pType.type == EbtStruct) {
614 error(line, "cannot be used with a structure", getQualifierString(pType.qualifier));
619 if (pType.qualifier != EvqUniform && samplerErrorCheck(line, pType, "samplers must be uniform"))
625 bool TParseContext::parameterSamplerErrorCheck(int line, TQualifier qualifier, const TType& type)
627 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
628 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType())) {
629 error(line, "samplers cannot be output parameters", type.getBasicString());
636 bool TParseContext::containsSampler(TType& type)
638 if (IsSampler(type.getBasicType()))
641 if (type.getBasicType() == EbtStruct) {
642 TTypeList& structure = *type.getStruct();
643 for (unsigned int i = 0; i < structure.size(); ++i) {
644 if (containsSampler(*structure[i].type))
653 // Do size checking for an array type's size.
655 // Returns true if there was an error.
657 bool TParseContext::arraySizeErrorCheck(int line, TIntermTyped* expr, int& size)
659 TIntermConstantUnion* constant = expr->getAsConstantUnion();
660 if (constant == 0 || constant->getBasicType() != EbtInt) {
661 error(line, "array size must be a constant integer expression", "");
665 size = constant->getIConst(0);
668 error(line, "array size must be a positive integer", "");
677 // See if this qualifier can be an array.
679 // Returns true if there is an error.
681 bool TParseContext::arrayQualifierErrorCheck(int line, TPublicType type)
683 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqConst)) {
684 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
692 // See if this type can be an array.
694 // Returns true if there is an error.
696 bool TParseContext::arrayTypeErrorCheck(int line, TPublicType type)
699 // Can the type be an array?
702 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
710 // Do all the semantic checking for declaring an array, with and
711 // without a size, and make the right changes to the symbol table.
713 // size == 0 means no specified size.
715 // Returns true if there was an error.
717 bool TParseContext::arrayErrorCheck(int line, TString& identifier, TPublicType type, TVariable*& variable)
720 // Don't check for reserved word use until after we know it's not in the symbol table,
721 // because reserved arrays can be redeclared.
724 bool builtIn = false;
725 bool sameScope = false;
726 TSymbol* symbol = symbolTable.find(identifier, &builtIn, &sameScope);
727 if (symbol == 0 || !sameScope) {
728 if (reservedErrorCheck(line, identifier))
731 variable = new TVariable(&identifier, TType(type));
734 variable->getType().setArraySize(type.arraySize);
736 if (! symbolTable.insert(*variable)) {
738 error(line, "INTERNAL ERROR inserting new symbol", identifier.c_str());
742 if (! symbol->isVariable()) {
743 error(line, "variable expected", identifier.c_str());
747 variable = static_cast<TVariable*>(symbol);
748 if (! variable->getType().isArray()) {
749 error(line, "redeclaring non-array as array", identifier.c_str());
752 if (variable->getType().getArraySize() > 0) {
753 error(line, "redeclaration of array with size", identifier.c_str());
757 if (! variable->getType().sameElementType(TType(type))) {
758 error(line, "redeclaration of array with a different type", identifier.c_str());
762 TType* t = variable->getArrayInformationType();
764 if (t->getMaxArraySize() > type.arraySize) {
765 error(line, "higher index value already used for the array", identifier.c_str());
768 t->setArraySize(type.arraySize);
769 t = t->getArrayInformationType();
773 variable->getType().setArraySize(type.arraySize);
776 if (voidErrorCheck(line, identifier, type))
782 bool TParseContext::arraySetMaxSize(TIntermSymbol *node, TType* type, int size, bool updateFlag, TSourceLoc line)
784 bool builtIn = false;
785 TSymbol* symbol = symbolTable.find(node->getSymbol(), &builtIn);
787 error(line, " undeclared identifier", node->getSymbol().c_str());
790 TVariable* variable = static_cast<TVariable*>(symbol);
792 type->setArrayInformationType(variable->getArrayInformationType());
793 variable->updateArrayInformationType(type);
795 // special casing to test index value of gl_FragData. If the accessed index is >= gl_MaxDrawBuffers
797 if (node->getSymbol() == "gl_FragData") {
798 TSymbol* fragData = symbolTable.find("gl_MaxDrawBuffers", &builtIn);
801 int fragDataValue = static_cast<TVariable*>(fragData)->getConstPointer()[0].getIConst();
802 if (fragDataValue <= size) {
803 error(line, "", "[", "gl_FragData can only have a max array size of up to gl_MaxDrawBuffers");
808 // we dont want to update the maxArraySize when this flag is not set, we just want to include this
809 // node type in the chain of node types so that its updated when a higher maxArraySize comes in.
814 variable->getType().setMaxArraySize(size);
815 type->setMaxArraySize(size);
818 while(tt->getArrayInformationType() != 0) {
819 tt = tt->getArrayInformationType();
820 tt->setMaxArraySize(size);
827 // Enforce non-initializer type/qualifier rules.
829 // Returns true if there was an error.
831 bool TParseContext::nonInitConstErrorCheck(int line, TString& identifier, TPublicType& type, bool array)
833 if (type.qualifier == EvqConst)
835 // Make the qualifier make sense.
836 type.qualifier = EvqTemporary;
840 error(line, "arrays may not be declared constant since they cannot be initialized", identifier.c_str());
842 else if (type.isStructureContainingArrays())
844 error(line, "structures containing arrays may not be declared constant since they cannot be initialized", identifier.c_str());
848 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
858 // Do semantic checking for a variable declaration that has no initializer,
859 // and update the symbol table.
861 // Returns true if there was an error.
863 bool TParseContext::nonInitErrorCheck(int line, TString& identifier, TPublicType& type, TVariable*& variable)
865 if (reservedErrorCheck(line, identifier))
868 variable = new TVariable(&identifier, TType(type));
870 if (! symbolTable.insert(*variable)) {
871 error(line, "redefinition", variable->getName().c_str());
877 if (voidErrorCheck(line, identifier, type))
883 bool TParseContext::paramErrorCheck(int line, TQualifier qualifier, TQualifier paramQualifier, TType* type)
885 if (qualifier != EvqConst && qualifier != EvqTemporary) {
886 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
889 if (qualifier == EvqConst && paramQualifier != EvqIn) {
890 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
894 if (qualifier == EvqConst)
895 type->setQualifier(EvqConstReadOnly);
897 type->setQualifier(paramQualifier);
902 bool TParseContext::extensionErrorCheck(int line, const TString& extension)
904 const TExtensionBehavior& extBehavior = extensionBehavior();
905 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
906 if (iter == extBehavior.end()) {
907 error(line, "extension", extension.c_str(), "is not supported");
910 // In GLSL ES, an extension's default behavior is "disable".
911 if (iter->second == EBhDisable || iter->second == EBhUndefined) {
912 error(line, "extension", extension.c_str(), "is disabled");
915 if (iter->second == EBhWarn) {
916 warning(line, "extension", extension.c_str(), "is being used");
923 bool TParseContext::supportsExtension(const char* extension)
925 const TExtensionBehavior& extbehavior = extensionBehavior();
926 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
927 return (iter != extbehavior.end());
930 void TParseContext::handleExtensionDirective(int line, const char* extName, const char* behavior)
932 pp::SourceLocation loc;
933 DecodeSourceLoc(line, &loc.file, &loc.line);
934 directiveHandler.handleExtension(loc, extName, behavior);
937 void TParseContext::handlePragmaDirective(int line, const char* name, const char* value)
939 pp::SourceLocation loc;
940 DecodeSourceLoc(line, &loc.file, &loc.line);
941 directiveHandler.handlePragma(loc, name, value);
944 /////////////////////////////////////////////////////////////////////////////////
948 /////////////////////////////////////////////////////////////////////////////////
951 // Look up a function name in the symbol table, and make sure it is a function.
953 // Return the function symbol if found, otherwise 0.
955 const TFunction* TParseContext::findFunction(int line, TFunction* call, bool *builtIn)
957 // First find by unmangled name to check whether the function name has been
958 // hidden by a variable name or struct typename.
959 const TSymbol* symbol = symbolTable.find(call->getName(), builtIn);
961 symbol = symbolTable.find(call->getMangledName(), builtIn);
965 error(line, "no matching overloaded function found", call->getName().c_str());
969 if (!symbol->isFunction()) {
970 error(line, "function name expected", call->getName().c_str());
974 return static_cast<const TFunction*>(symbol);
978 // Initializers show up in several places in the grammar. Have one set of
979 // code to handle them here.
981 bool TParseContext::executeInitializer(TSourceLoc line, TString& identifier, TPublicType& pType,
982 TIntermTyped* initializer, TIntermNode*& intermNode, TVariable* variable)
984 TType type = TType(pType);
987 if (reservedErrorCheck(line, identifier))
990 if (voidErrorCheck(line, identifier, pType))
994 // add variable to symbol table
996 variable = new TVariable(&identifier, type);
997 if (! symbolTable.insert(*variable)) {
998 error(line, "redefinition", variable->getName().c_str());
1000 // don't delete variable, it's used by error recovery, and the pool
1001 // pop will take care of the memory
1006 // identifier must be of type constant, a global, or a temporary
1008 TQualifier qualifier = variable->getType().getQualifier();
1009 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst)) {
1010 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
1014 // test for and propagate constant
1017 if (qualifier == EvqConst) {
1018 if (qualifier != initializer->getType().getQualifier()) {
1019 std::stringstream extraInfoStream;
1020 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1021 std::string extraInfo = extraInfoStream.str();
1022 error(line, " assigning non-constant to", "=", extraInfo.c_str());
1023 variable->getType().setQualifier(EvqTemporary);
1026 if (type != initializer->getType()) {
1027 error(line, " non-matching types for const initializer ",
1028 variable->getType().getQualifierString());
1029 variable->getType().setQualifier(EvqTemporary);
1032 if (initializer->getAsConstantUnion()) {
1033 ConstantUnion* unionArray = variable->getConstPointer();
1035 if (type.getObjectSize() == 1 && type.getBasicType() != EbtStruct) {
1036 *unionArray = (initializer->getAsConstantUnion()->getUnionArrayPointer())[0];
1038 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
1040 } else if (initializer->getAsSymbolNode()) {
1041 const TSymbol* symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol());
1042 const TVariable* tVar = static_cast<const TVariable*>(symbol);
1044 ConstantUnion* constArray = tVar->getConstPointer();
1045 variable->shareConstPointer(constArray);
1047 std::stringstream extraInfoStream;
1048 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1049 std::string extraInfo = extraInfoStream.str();
1050 error(line, " cannot assign to", "=", extraInfo.c_str());
1051 variable->getType().setQualifier(EvqTemporary);
1056 if (qualifier != EvqConst) {
1057 TIntermSymbol* intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), line);
1058 intermNode = intermediate.addAssign(EOpInitialize, intermSymbol, initializer, line);
1059 if (intermNode == 0) {
1060 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1069 bool TParseContext::areAllChildConst(TIntermAggregate* aggrNode)
1071 ASSERT(aggrNode != NULL);
1072 if (!aggrNode->isConstructor())
1075 bool allConstant = true;
1077 // check if all the child nodes are constants so that they can be inserted into
1079 TIntermSequence &sequence = aggrNode->getSequence() ;
1080 for (TIntermSequence::iterator p = sequence.begin(); p != sequence.end(); ++p) {
1081 if (!(*p)->getAsTyped()->getAsConstantUnion())
1088 // This function is used to test for the correctness of the parameters passed to various constructor functions
1089 // and also convert them to the right datatype if it is allowed and required.
1091 // Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1093 TIntermTyped* TParseContext::addConstructor(TIntermNode* arguments, const TType* type, TOperator op, TFunction* fnCall, TSourceLoc line)
1095 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
1097 if(!aggregateArguments)
1099 aggregateArguments = new TIntermAggregate;
1100 aggregateArguments->getSequence().push_back(arguments);
1103 if(op == EOpConstructStruct)
1105 TTypeList &fields = *type->getStruct();
1106 TIntermSequence &args = aggregateArguments->getSequence();
1108 for(size_t i = 0; i < fields.size(); i++)
1110 if(args[i]->getAsTyped()->getType() != *fields[i].type)
1112 error(line, "Structure constructor arguments do not match structure fields", "Error");
1120 // Turn the argument list itself into a constructor
1121 TIntermTyped *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1122 TIntermTyped *constConstructor = foldConstConstructor(constructor->getAsAggregate(), *type);
1123 if(constConstructor)
1125 return constConstructor;
1131 TIntermTyped* TParseContext::foldConstConstructor(TIntermAggregate* aggrNode, const TType& type)
1133 bool canBeFolded = areAllChildConst(aggrNode);
1134 aggrNode->setType(type);
1136 bool returnVal = false;
1137 ConstantUnion* unionArray = new ConstantUnion[type.getObjectSize()];
1138 if (aggrNode->getSequence().size() == 1) {
1139 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type, true);
1142 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type);
1147 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1154 // This function returns the tree representation for the vector field(s) being accessed from contant vector.
1155 // If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1156 // returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
1157 // node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
1158 // a constant matrix.
1160 TIntermTyped* TParseContext::addConstVectorNode(TVectorFields& fields, TIntermTyped* node, TSourceLoc line)
1162 TIntermTyped* typedNode;
1163 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1165 ConstantUnion *unionArray;
1166 if (tempConstantNode) {
1167 unionArray = tempConstantNode->getUnionArrayPointer();
1172 } else { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
1173 error(line, "Cannot offset into the vector", "Error");
1179 ConstantUnion* constArray = new ConstantUnion[fields.num];
1181 for (int i = 0; i < fields.num; i++) {
1182 if (fields.offsets[i] >= node->getType().getObjectSize()) {
1183 std::stringstream extraInfoStream;
1184 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
1185 std::string extraInfo = extraInfoStream.str();
1186 error(line, "", "[", extraInfo.c_str());
1188 fields.offsets[i] = 0;
1191 constArray[i] = unionArray[fields.offsets[i]];
1194 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
1199 // This function returns the column being accessed from a constant matrix. The values are retrieved from
1200 // the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
1201 // to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
1202 // constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
1204 TIntermTyped* TParseContext::addConstMatrixNode(int index, TIntermTyped* node, TSourceLoc line)
1206 TIntermTyped* typedNode;
1207 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1209 if (index >= node->getType().getNominalSize()) {
1210 std::stringstream extraInfoStream;
1211 extraInfoStream << "matrix field selection out of range '" << index << "'";
1212 std::string extraInfo = extraInfoStream.str();
1213 error(line, "", "[", extraInfo.c_str());
1218 if (tempConstantNode) {
1219 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1220 int size = tempConstantNode->getType().getNominalSize();
1221 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
1223 error(line, "Cannot offset into the matrix", "Error");
1234 // This function returns an element of an array accessed from a constant array. The values are retrieved from
1235 // the symbol table and parse-tree is built for the type of the element. The input
1236 // to the function could either be a symbol node (a[0] where a is a constant array)that represents a
1237 // constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
1239 TIntermTyped* TParseContext::addConstArrayNode(int index, TIntermTyped* node, TSourceLoc line)
1241 TIntermTyped* typedNode;
1242 TIntermConstantUnion* tempConstantNode = node->getAsConstantUnion();
1243 TType arrayElementType = node->getType();
1244 arrayElementType.clearArrayness();
1246 if (index >= node->getType().getArraySize()) {
1247 std::stringstream extraInfoStream;
1248 extraInfoStream << "array field selection out of range '" << index << "'";
1249 std::string extraInfo = extraInfoStream.str();
1250 error(line, "", "[", extraInfo.c_str());
1255 int arrayElementSize = arrayElementType.getObjectSize();
1257 if (tempConstantNode) {
1258 ConstantUnion* unionArray = tempConstantNode->getUnionArrayPointer();
1259 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(), line);
1261 error(line, "Cannot offset into the array", "Error");
1272 // This function returns the value of a particular field inside a constant structure from the symbol table.
1273 // If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
1274 // function and returns the parse-tree with the values of the embedded/nested struct.
1276 TIntermTyped* TParseContext::addConstStruct(TString& identifier, TIntermTyped* node, TSourceLoc line)
1278 const TTypeList* fields = node->getType().getStruct();
1279 TIntermTyped *typedNode;
1280 int instanceSize = 0;
1281 unsigned int index = 0;
1282 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
1284 for ( index = 0; index < fields->size(); ++index) {
1285 if ((*fields)[index].type->getFieldName() == identifier) {
1288 instanceSize += (*fields)[index].type->getObjectSize();
1292 if (tempConstantNode) {
1293 ConstantUnion* constArray = tempConstantNode->getUnionArrayPointer();
1295 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line); // type will be changed in the calling function
1297 error(line, "Cannot offset into the structure", "Error");
1306 bool TParseContext::enterStructDeclaration(int line, const TString& identifier)
1308 ++structNestingLevel;
1310 // Embedded structure definitions are not supported per GLSL ES spec.
1311 // They aren't allowed in GLSL either, but we need to detect this here
1312 // so we don't rely on the GLSL compiler to catch it.
1313 if (structNestingLevel > 1) {
1314 error(line, "", "Embedded struct definitions are not allowed");
1321 void TParseContext::exitStructDeclaration()
1323 --structNestingLevel;
1327 // Parse an array of strings using yyparse.
1329 // Returns 0 for success.
1331 int PaParseStrings(int count, const char* const string[], const int length[],
1332 TParseContext* context) {
1333 if ((count == 0) || (string == NULL))
1336 if (glslang_initialize(context))
1339 int error = glslang_scan(count, string, length, context);
1341 error = glslang_parse(context);
1343 glslang_finalize(context);
1345 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;