3 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style license that can be
5 // found in the LICENSE file.
8 This file contains the Yacc grammar for GLSL ES.
9 Based on ANSI C Yacc grammar:
10 http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
12 IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN generate_parser.sh,
13 WHICH GENERATES THE GLSL ES PARSER (glslang_tab.cpp AND glslang_tab.h).
18 // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
19 // Use of this source code is governed by a BSD-style license that can be
20 // found in the LICENSE file.
23 // This file is auto-generated by generate_parser.sh. DO NOT EDIT!
25 // Ignore errors in auto-generated code.
27 #pragma GCC diagnostic ignored "-Wunused-function"
28 #pragma GCC diagnostic ignored "-Wunused-variable"
29 #pragma GCC diagnostic ignored "-Wswitch-enum"
30 #elif defined(_MSC_VER)
31 #pragma warning(disable: 4065)
32 #pragma warning(disable: 4189)
33 #pragma warning(disable: 4505)
34 #pragma warning(disable: 4701)
38 #include "compiler/translator/SymbolTable.h"
39 #include "compiler/translator/ParseContext.h"
40 #include "GLSLANG/ShaderLang.h"
42 #define YYENABLE_NLS 0
44 #define YYLEX_PARAM context->scanner
47 %expect 1 /* One shift reduce conflict because of if | else */
49 %parse-param {TParseContext* context}
53 #define YYLTYPE TSourceLoc
54 #define YYLTYPE_IS_DECLARED 1
71 TIntermNode* intermNode;
72 TIntermNodePair nodePair;
73 TIntermTyped* intermTypedNode;
74 TIntermAggregate* intermAggregate;
79 TLayoutQualifier layoutQualifier;
84 TFieldList* fieldList;
90 extern int yylex(YYSTYPE* yylval, YYLTYPE* yylloc, void* yyscanner);
91 extern void yyerror(YYLTYPE* yylloc, TParseContext* context, const char* reason);
93 #define YYLLOC_DEFAULT(Current, Rhs, N) \
96 (Current).first_file = YYRHSLOC(Rhs, 1).first_file; \
97 (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \
98 (Current).last_file = YYRHSLOC(Rhs, N).last_file; \
99 (Current).last_line = YYRHSLOC(Rhs, N).last_line; \
102 (Current).first_file = YYRHSLOC(Rhs, 0).last_file; \
103 (Current).first_line = YYRHSLOC(Rhs, 0).last_line; \
104 (Current).last_file = YYRHSLOC(Rhs, 0).last_file; \
105 (Current).last_line = YYRHSLOC(Rhs, 0).last_line; \
109 #define VERTEX_ONLY(S, L) { \
110 if (context->shaderType != GL_VERTEX_SHADER) { \
111 context->error(L, " supported in vertex shaders only ", S); \
112 context->recover(); \
116 #define FRAG_ONLY(S, L) { \
117 if (context->shaderType != GL_FRAGMENT_SHADER) { \
118 context->error(L, " supported in fragment shaders only ", S); \
119 context->recover(); \
123 #define ES2_ONLY(S, L) { \
124 if (context->shaderVersion != 100) { \
125 context->error(L, " supported in GLSL ES 1.00 only ", S); \
126 context->recover(); \
130 #define ES3_ONLY(TOKEN, LINE, REASON) { \
131 if (context->shaderVersion != 300) { \
132 context->error(LINE, REASON " supported in GLSL ES 3.00 only ", TOKEN); \
133 context->recover(); \
138 %token <lex> INVARIANT HIGH_PRECISION MEDIUM_PRECISION LOW_PRECISION PRECISION
139 %token <lex> ATTRIBUTE CONST_QUAL BOOL_TYPE FLOAT_TYPE INT_TYPE UINT_TYPE
140 %token <lex> BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
141 %token <lex> BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 VEC2 VEC3 VEC4 UVEC2 UVEC3 UVEC4
142 %token <lex> MATRIX2 MATRIX3 MATRIX4 IN_QUAL OUT_QUAL INOUT_QUAL UNIFORM VARYING
143 %token <lex> MATRIX2x3 MATRIX3x2 MATRIX2x4 MATRIX4x2 MATRIX3x4 MATRIX4x3
144 %token <lex> CENTROID FLAT SMOOTH
145 %token <lex> STRUCT VOID_TYPE WHILE
146 %token <lex> SAMPLER2D SAMPLERCUBE SAMPLER_EXTERNAL_OES SAMPLER2DRECT SAMPLER2DARRAY
147 %token <lex> ISAMPLER2D ISAMPLER3D ISAMPLERCUBE ISAMPLER2DARRAY
148 %token <lex> USAMPLER2D USAMPLER3D USAMPLERCUBE USAMPLER2DARRAY
149 %token <lex> SAMPLER3D SAMPLER3DRECT SAMPLER2DSHADOW SAMPLERCUBESHADOW SAMPLER2DARRAYSHADOW
152 %token <lex> IDENTIFIER TYPE_NAME FLOATCONSTANT INTCONSTANT UINTCONSTANT BOOLCONSTANT
153 %token <lex> FIELD_SELECTION
154 %token <lex> LEFT_OP RIGHT_OP
155 %token <lex> INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
156 %token <lex> AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
157 %token <lex> MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
158 %token <lex> SUB_ASSIGN
160 %token <lex> LEFT_PAREN RIGHT_PAREN LEFT_BRACKET RIGHT_BRACKET LEFT_BRACE RIGHT_BRACE DOT
161 %token <lex> COMMA COLON EQUAL SEMICOLON BANG DASH TILDE PLUS STAR SLASH PERCENT
162 %token <lex> LEFT_ANGLE RIGHT_ANGLE VERTICAL_BAR CARET AMPERSAND QUESTION
164 %type <lex> identifier
165 %type <interm> assignment_operator unary_operator
166 %type <interm.intermTypedNode> variable_identifier primary_expression postfix_expression
167 %type <interm.intermTypedNode> expression integer_expression assignment_expression
168 %type <interm.intermTypedNode> unary_expression multiplicative_expression additive_expression
169 %type <interm.intermTypedNode> relational_expression equality_expression
170 %type <interm.intermTypedNode> conditional_expression constant_expression
171 %type <interm.intermTypedNode> logical_or_expression logical_xor_expression logical_and_expression
172 %type <interm.intermTypedNode> shift_expression and_expression exclusive_or_expression inclusive_or_expression
173 %type <interm.intermTypedNode> function_call initializer condition conditionopt
175 %type <interm.intermNode> translation_unit function_definition
176 %type <interm.intermNode> statement simple_statement
177 %type <interm.intermAggregate> statement_list compound_statement
178 %type <interm.intermNode> declaration_statement selection_statement expression_statement
179 %type <interm.intermNode> declaration external_declaration
180 %type <interm.intermNode> for_init_statement compound_statement_no_new_scope
181 %type <interm.nodePair> selection_rest_statement for_rest_statement
182 %type <interm.intermNode> iteration_statement jump_statement statement_no_new_scope statement_with_scope
183 %type <interm> single_declaration init_declarator_list
185 %type <interm> parameter_declaration parameter_declarator parameter_type_specifier
186 %type <interm.qualifier> parameter_qualifier parameter_type_qualifier
187 %type <interm.layoutQualifier> layout_qualifier layout_qualifier_id_list layout_qualifier_id
189 %type <interm.precision> precision_qualifier
190 %type <interm.type> type_qualifier fully_specified_type type_specifier storage_qualifier interpolation_qualifier
191 %type <interm.type> type_specifier_no_prec type_specifier_nonarray
192 %type <interm.type> struct_specifier
193 %type <interm.field> struct_declarator
194 %type <interm.fieldList> struct_declarator_list struct_declaration struct_declaration_list
195 %type <interm.function> function_header function_declarator function_identifier
196 %type <interm.function> function_header_with_parameters function_call_header
197 %type <interm> function_call_header_with_parameters function_call_header_no_parameters function_call_generic function_prototype
198 %type <interm> function_call_or_method
200 %type <lex> enter_struct
202 %start translation_unit
211 // The symbol table search was done in the lexical phase
212 const TSymbol *symbol = $1.symbol;
213 const TVariable *variable = 0;
217 context->error(@1, "undeclared identifier", $1.string->c_str());
220 else if (!symbol->isVariable())
222 context->error(@1, "variable expected", $1.string->c_str());
227 variable = static_cast<const TVariable*>(symbol);
229 if (context->symbolTable.findBuiltIn(variable->getName(), context->shaderVersion) &&
230 !variable->getExtension().empty() &&
231 context->extensionErrorCheck(@1, variable->getExtension()))
239 TType type(EbtFloat, EbpUndefined);
240 TVariable *fakeVariable = new TVariable($1.string, type);
241 context->symbolTable.declare(fakeVariable);
242 variable = fakeVariable;
245 if (variable->getType().getQualifier() == EvqConst)
247 ConstantUnion* constArray = variable->getConstPointer();
248 TType t(variable->getType());
249 $$ = context->intermediate.addConstantUnion(constArray, t, @1);
253 $$ = context->intermediate.addSymbol(variable->getUniqueId(),
259 // don't delete $1.string, it's used by error recovery, and the pool
260 // pop will reclaim the memory
265 : variable_identifier {
269 ConstantUnion *unionArray = new ConstantUnion[1];
270 unionArray->setIConst($1.i);
271 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), @1);
274 ConstantUnion *unionArray = new ConstantUnion[1];
275 unionArray->setUConst($1.u);
276 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtUInt, EbpUndefined, EvqConst), @1);
279 ConstantUnion *unionArray = new ConstantUnion[1];
280 unionArray->setFConst($1.f);
281 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), @1);
284 ConstantUnion *unionArray = new ConstantUnion[1];
285 unionArray->setBConst($1.b);
286 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @1);
288 | LEFT_PAREN expression RIGHT_PAREN {
294 : primary_expression {
297 | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET {
298 $$ = context->addIndexExpression($1, @2, $3);
303 | postfix_expression DOT identifier {
304 $$ = context->addFieldSelectionExpression($1, @2, *$3.string, @3);
306 | postfix_expression INC_OP {
307 if (context->lValueErrorCheck(@2, "++", $1))
309 $$ = context->intermediate.addUnaryMath(EOpPostIncrement, $1, @2);
311 context->unaryOpError(@2, "++", $1->getCompleteString());
316 | postfix_expression DEC_OP {
317 if (context->lValueErrorCheck(@2, "--", $1))
319 $$ = context->intermediate.addUnaryMath(EOpPostDecrement, $1, @2);
321 context->unaryOpError(@2, "--", $1->getCompleteString());
330 if (context->integerErrorCheck($1, "[]"))
337 : function_call_or_method {
338 TFunction* fnCall = $1.function;
339 TOperator op = fnCall->getBuiltInOp();
344 // Then this should be a constructor.
345 // Don't go through the symbol table for constructors.
346 // Their parameters will be verified algorithmically.
348 TType type(EbtVoid, EbpUndefined); // use this to get the type back
349 if (context->constructorErrorCheck(@1, $1.intermNode, *fnCall, op, &type)) {
353 // It's a constructor, of type 'type'.
355 $$ = context->addConstructor($1.intermNode, &type, op, fnCall, @1);
360 $$ = context->intermediate.setAggregateOperator(0, op, @1);
365 // Not a constructor. Find it in the symbol table.
367 const TFunction* fnCandidate;
369 fnCandidate = context->findFunction(@1, fnCall, context->shaderVersion, &builtIn);
372 // A declared function.
374 if (builtIn && !fnCandidate->getExtension().empty() &&
375 context->extensionErrorCheck(@1, fnCandidate->getExtension())) {
378 op = fnCandidate->getBuiltInOp();
379 if (builtIn && op != EOpNull) {
381 // A function call mapped to a built-in operation.
383 if (fnCandidate->getParamCount() == 1) {
385 // Treat it like a built-in unary operator.
387 $$ = context->intermediate.addUnaryMath(op, $1.intermNode, @1);
389 std::stringstream extraInfoStream;
390 extraInfoStream << "built in unary operator function. Type: " << static_cast<TIntermTyped*>($1.intermNode)->getCompleteString();
391 std::string extraInfo = extraInfoStream.str();
392 context->error($1.intermNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
396 $$ = context->intermediate.setAggregateOperator($1.intermAggregate, op, @1);
399 // This is a real function call
401 $$ = context->intermediate.setAggregateOperator($1.intermAggregate, EOpFunctionCall, @1);
402 $$->setType(fnCandidate->getReturnType());
404 // this is how we know whether the given function is a builtIn function or a user defined function
405 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
406 // if builtIn == true, it's definitely a builtIn function with EOpNull
408 $$->getAsAggregate()->setUserDefined();
409 $$->getAsAggregate()->setName(fnCandidate->getMangledName());
412 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i) {
413 qual = fnCandidate->getParam(i).type->getQualifier();
414 if (qual == EvqOut || qual == EvqInOut) {
415 if (context->lValueErrorCheck($$->getLine(), "assign", (*($$->getAsAggregate()->getSequence()))[i]->getAsTyped())) {
416 context->error($1.intermNode->getLine(), "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
422 $$->setType(fnCandidate->getReturnType());
424 // error message was put out by PaFindFunction()
425 // Put on a dummy node for error recovery
426 ConstantUnion *unionArray = new ConstantUnion[1];
427 unionArray->setFConst(0.0f);
428 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), @1);
436 function_call_or_method
437 : function_call_generic {
440 | postfix_expression DOT function_call_generic {
441 context->error(@3, "methods are not supported", "");
447 function_call_generic
448 : function_call_header_with_parameters RIGHT_PAREN {
451 | function_call_header_no_parameters RIGHT_PAREN {
456 function_call_header_no_parameters
457 : function_call_header VOID_TYPE {
461 | function_call_header {
467 function_call_header_with_parameters
468 : function_call_header assignment_expression {
469 TParameter param = { 0, new TType($2->getType()) };
470 $1->addParameter(param);
474 | function_call_header_with_parameters COMMA assignment_expression {
475 TParameter param = { 0, new TType($3->getType()) };
476 $1.function->addParameter(param);
477 $$.function = $1.function;
478 $$.intermNode = context->intermediate.growAggregate($1.intermNode, $3, @2);
483 : function_identifier LEFT_PAREN {
488 // Grammar Note: Constructors look like functions, but are recognized as types.
491 : type_specifier_nonarray {
492 $$ = context->addConstructorFunc($1);
495 if (context->reservedErrorCheck(@1, *$1.string))
497 TType type(EbtVoid, EbpUndefined);
498 TFunction *function = new TFunction($1.string, type);
504 : postfix_expression {
507 | INC_OP unary_expression {
508 if (context->lValueErrorCheck(@1, "++", $2))
510 $$ = context->intermediate.addUnaryMath(EOpPreIncrement, $2, @1);
512 context->unaryOpError(@1, "++", $2->getCompleteString());
517 | DEC_OP unary_expression {
518 if (context->lValueErrorCheck(@1, "--", $2))
520 $$ = context->intermediate.addUnaryMath(EOpPreDecrement, $2, @1);
522 context->unaryOpError(@1, "--", $2->getCompleteString());
527 | unary_operator unary_expression {
528 if ($1.op != EOpNull) {
529 $$ = context->intermediate.addUnaryMath($1.op, $2, @1);
531 const char* errorOp = "";
533 case EOpNegative: errorOp = "-"; break;
534 case EOpLogicalNot: errorOp = "!"; break;
537 context->unaryOpError(@1, errorOp, $2->getCompleteString());
545 // Grammar Note: No traditional style type casts.
548 : PLUS { $$.op = EOpNull; }
549 | DASH { $$.op = EOpNegative; }
550 | BANG { $$.op = EOpLogicalNot; }
552 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
554 multiplicative_expression
555 : unary_expression { $$ = $1; }
556 | multiplicative_expression STAR unary_expression {
557 $$ = context->intermediate.addBinaryMath(EOpMul, $1, $3, @2);
559 context->binaryOpError(@2, "*", $1->getCompleteString(), $3->getCompleteString());
564 | multiplicative_expression SLASH unary_expression {
565 $$ = context->intermediate.addBinaryMath(EOpDiv, $1, $3, @2);
567 context->binaryOpError(@2, "/", $1->getCompleteString(), $3->getCompleteString());
575 : multiplicative_expression { $$ = $1; }
576 | additive_expression PLUS multiplicative_expression {
577 $$ = context->intermediate.addBinaryMath(EOpAdd, $1, $3, @2);
579 context->binaryOpError(@2, "+", $1->getCompleteString(), $3->getCompleteString());
584 | additive_expression DASH multiplicative_expression {
585 $$ = context->intermediate.addBinaryMath(EOpSub, $1, $3, @2);
587 context->binaryOpError(@2, "-", $1->getCompleteString(), $3->getCompleteString());
595 : additive_expression { $$ = $1; }
598 relational_expression
599 : shift_expression { $$ = $1; }
600 | relational_expression LEFT_ANGLE shift_expression {
601 $$ = context->intermediate.addBinaryMath(EOpLessThan, $1, $3, @2);
603 context->binaryOpError(@2, "<", $1->getCompleteString(), $3->getCompleteString());
605 ConstantUnion *unionArray = new ConstantUnion[1];
606 unionArray->setBConst(false);
607 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
610 | relational_expression RIGHT_ANGLE shift_expression {
611 $$ = context->intermediate.addBinaryMath(EOpGreaterThan, $1, $3, @2);
613 context->binaryOpError(@2, ">", $1->getCompleteString(), $3->getCompleteString());
615 ConstantUnion *unionArray = new ConstantUnion[1];
616 unionArray->setBConst(false);
617 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
620 | relational_expression LE_OP shift_expression {
621 $$ = context->intermediate.addBinaryMath(EOpLessThanEqual, $1, $3, @2);
623 context->binaryOpError(@2, "<=", $1->getCompleteString(), $3->getCompleteString());
625 ConstantUnion *unionArray = new ConstantUnion[1];
626 unionArray->setBConst(false);
627 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
630 | relational_expression GE_OP shift_expression {
631 $$ = context->intermediate.addBinaryMath(EOpGreaterThanEqual, $1, $3, @2);
633 context->binaryOpError(@2, ">=", $1->getCompleteString(), $3->getCompleteString());
635 ConstantUnion *unionArray = new ConstantUnion[1];
636 unionArray->setBConst(false);
637 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
643 : relational_expression { $$ = $1; }
644 | equality_expression EQ_OP relational_expression {
645 $$ = context->intermediate.addBinaryMath(EOpEqual, $1, $3, @2);
647 context->binaryOpError(@2, "==", $1->getCompleteString(), $3->getCompleteString());
649 ConstantUnion *unionArray = new ConstantUnion[1];
650 unionArray->setBConst(false);
651 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
654 | equality_expression NE_OP relational_expression {
655 $$ = context->intermediate.addBinaryMath(EOpNotEqual, $1, $3, @2);
657 context->binaryOpError(@2, "!=", $1->getCompleteString(), $3->getCompleteString());
659 ConstantUnion *unionArray = new ConstantUnion[1];
660 unionArray->setBConst(false);
661 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
667 : equality_expression { $$ = $1; }
670 exclusive_or_expression
671 : and_expression { $$ = $1; }
674 inclusive_or_expression
675 : exclusive_or_expression { $$ = $1; }
678 logical_and_expression
679 : inclusive_or_expression { $$ = $1; }
680 | logical_and_expression AND_OP inclusive_or_expression {
681 $$ = context->intermediate.addBinaryMath(EOpLogicalAnd, $1, $3, @2);
683 context->binaryOpError(@2, "&&", $1->getCompleteString(), $3->getCompleteString());
685 ConstantUnion *unionArray = new ConstantUnion[1];
686 unionArray->setBConst(false);
687 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
692 logical_xor_expression
693 : logical_and_expression { $$ = $1; }
694 | logical_xor_expression XOR_OP logical_and_expression {
695 $$ = context->intermediate.addBinaryMath(EOpLogicalXor, $1, $3, @2);
697 context->binaryOpError(@2, "^^", $1->getCompleteString(), $3->getCompleteString());
699 ConstantUnion *unionArray = new ConstantUnion[1];
700 unionArray->setBConst(false);
701 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
706 logical_or_expression
707 : logical_xor_expression { $$ = $1; }
708 | logical_or_expression OR_OP logical_xor_expression {
709 $$ = context->intermediate.addBinaryMath(EOpLogicalOr, $1, $3, @2);
711 context->binaryOpError(@2, "||", $1->getCompleteString(), $3->getCompleteString());
713 ConstantUnion *unionArray = new ConstantUnion[1];
714 unionArray->setBConst(false);
715 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
720 conditional_expression
721 : logical_or_expression { $$ = $1; }
722 | logical_or_expression QUESTION expression COLON assignment_expression {
723 if (context->boolErrorCheck(@2, $1))
726 $$ = context->intermediate.addSelection($1, $3, $5, @2);
727 if ($3->getType() != $5->getType())
731 context->binaryOpError(@2, ":", $3->getCompleteString(), $5->getCompleteString());
738 assignment_expression
739 : conditional_expression { $$ = $1; }
740 | unary_expression assignment_operator assignment_expression {
741 if (context->lValueErrorCheck(@2, "assign", $1))
743 $$ = context->intermediate.addAssign($2.op, $1, $3, @2);
745 context->assignError(@2, "assign", $1->getCompleteString(), $3->getCompleteString());
753 : EQUAL { $$.op = EOpAssign; }
754 | MUL_ASSIGN { $$.op = EOpMulAssign; }
755 | DIV_ASSIGN { $$.op = EOpDivAssign; }
756 | ADD_ASSIGN { $$.op = EOpAddAssign; }
757 | SUB_ASSIGN { $$.op = EOpSubAssign; }
761 : assignment_expression {
764 | expression COMMA assignment_expression {
765 $$ = context->intermediate.addComma($1, $3, @2);
767 context->binaryOpError(@2, ",", $1->getCompleteString(), $3->getCompleteString());
775 : conditional_expression {
776 if (context->constErrorCheck($1))
783 : IDENTIFIER LEFT_BRACE {
784 if (context->enterStructDeclaration(@1, *$1.string))
791 : function_prototype SEMICOLON {
792 TFunction &function = *($1.function);
794 TIntermAggregate *prototype = new TIntermAggregate;
795 prototype->setType(function.getReturnType());
796 prototype->setName(function.getName());
798 for (size_t i = 0; i < function.getParamCount(); i++)
800 const TParameter ¶m = function.getParam(i);
803 TVariable variable(param.name, *param.type);
805 prototype = context->intermediate.growAggregate(prototype, context->intermediate.addSymbol(variable.getUniqueId(), variable.getName(), variable.getType(), @1), @1);
809 prototype = context->intermediate.growAggregate(prototype, context->intermediate.addSymbol(0, "", *param.type, @1), @1);
813 prototype->setOp(EOpPrototype);
816 context->symbolTable.pop();
818 | init_declarator_list SEMICOLON {
819 if ($1.intermAggregate)
820 $1.intermAggregate->setOp(EOpDeclaration);
821 $$ = $1.intermAggregate;
823 | PRECISION precision_qualifier type_specifier_no_prec SEMICOLON {
824 if (($2 == EbpHigh) && (context->shaderType == GL_FRAGMENT_SHADER) && !context->fragmentPrecisionHigh) {
825 context->error(@1, "precision is not supported in fragment shader", "highp");
828 if (!context->symbolTable.setDefaultPrecision( $3, $2 )) {
829 context->error(@1, "illegal type argument for default precision qualifier", getBasicString($3.type));
834 | type_qualifier enter_struct struct_declaration_list RIGHT_BRACE SEMICOLON {
835 ES3_ONLY(getQualifierString($1.qualifier), @1, "interface blocks");
836 $$ = context->addInterfaceBlock($1, @2, *$2.string, $3, NULL, @$, NULL, @$);
838 | type_qualifier enter_struct struct_declaration_list RIGHT_BRACE IDENTIFIER SEMICOLON {
839 ES3_ONLY(getQualifierString($1.qualifier), @1, "interface blocks");
840 $$ = context->addInterfaceBlock($1, @2, *$2.string, $3, $5.string, @5, NULL, @$);
842 | type_qualifier enter_struct struct_declaration_list RIGHT_BRACE IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET SEMICOLON {
843 ES3_ONLY(getQualifierString($1.qualifier), @1, "interface blocks");
844 $$ = context->addInterfaceBlock($1, @2, *$2.string, $3, $5.string, @5, $7, @6);
846 | type_qualifier SEMICOLON {
847 context->parseGlobalLayoutQualifier($1);
853 : function_declarator RIGHT_PAREN {
855 // Multiple declarations of the same function are allowed.
857 // If this is a definition, the definition production code will check for redefinitions
858 // (we don't know at this point if it's a definition or not).
860 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
862 TFunction* prevDec = static_cast<TFunction*>(context->symbolTable.find($1->getMangledName(), context->shaderVersion));
864 if (prevDec->getReturnType() != $1->getReturnType()) {
865 context->error(@2, "overloaded functions must have the same return type", $1->getReturnType().getBasicString());
868 for (size_t i = 0; i < prevDec->getParamCount(); ++i) {
869 if (prevDec->getParam(i).type->getQualifier() != $1->getParam(i).type->getQualifier()) {
870 context->error(@2, "overloaded functions must have the same parameter qualifiers", $1->getParam(i).type->getQualifierString());
877 // Check for previously declared variables using the same name.
879 TSymbol *prevSym = context->symbolTable.find($1->getName(), context->shaderVersion);
882 if (!prevSym->isFunction())
884 context->error(@2, "redefinition", $1->getName().c_str(), "function");
890 // Insert the unmangled name to detect potential future redefinition as a variable.
891 TFunction *function = new TFunction(NewPoolTString($1->getName().c_str()), $1->getReturnType());
892 context->symbolTable.getOuterLevel()->insert(function);
896 // If this is a redeclaration, it could also be a definition,
897 // in which case, we want to use the variable names from this one, and not the one that's
898 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
902 // We're at the inner scope level of the function's arguments and body statement.
903 // Add the function prototype to the surrounding scope instead.
904 context->symbolTable.getOuterLevel()->insert($$.function);
912 | function_header_with_parameters {
918 function_header_with_parameters
919 : function_header parameter_declaration {
922 if ($2.param.type->getBasicType() != EbtVoid)
923 $1->addParameter($2.param);
925 delete $2.param.type;
927 | function_header_with_parameters COMMA parameter_declaration {
929 // Only first parameter of one-parameter functions can be void
930 // The check for named parameters not being void is done in parameter_declarator
932 if ($3.param.type->getBasicType() == EbtVoid) {
934 // This parameter > first is void
936 context->error(@2, "cannot be an argument type except for '(void)'", "void");
938 delete $3.param.type;
942 $1->addParameter($3.param);
948 : fully_specified_type IDENTIFIER LEFT_PAREN {
949 if ($1.qualifier != EvqGlobal && $1.qualifier != EvqTemporary) {
950 context->error(@2, "no qualifiers allowed for function return", getQualifierString($1.qualifier));
953 // make sure a sampler is not involved as well...
954 if (context->structQualifierErrorCheck(@2, $1))
957 // Add the function as a prototype after parsing it (we do not support recursion)
960 function = new TFunction($2.string, type);
963 context->symbolTable.push();
969 : type_specifier identifier {
970 if ($1.type == EbtVoid) {
971 context->error(@2, "illegal use of type 'void'", $2.string->c_str());
974 if (context->reservedErrorCheck(@2, *$2.string))
976 TParameter param = {$2.string, new TType($1)};
979 | type_specifier identifier LEFT_BRACKET constant_expression RIGHT_BRACKET {
980 // Check that we can make an array out of this type
981 if (context->arrayTypeErrorCheck(@3, $1))
984 if (context->reservedErrorCheck(@2, *$2.string))
988 if (context->arraySizeErrorCheck(@3, $4, size))
990 $1.setArray(true, size);
992 TType* type = new TType($1);
993 TParameter param = { $2.string, type };
998 parameter_declaration
1000 // The only parameter qualifier a parameter can have are
1001 // IN_QUAL, OUT_QUAL, INOUT_QUAL, or CONST.
1007 : parameter_type_qualifier parameter_qualifier parameter_declarator {
1009 if (context->paramErrorCheck(@3, $1, $2, $$.param.type))
1012 | parameter_qualifier parameter_declarator {
1014 if (context->parameterSamplerErrorCheck(@2, $1, *$2.param.type))
1016 if (context->paramErrorCheck(@2, EvqTemporary, $1, $$.param.type))
1022 | parameter_type_qualifier parameter_qualifier parameter_type_specifier {
1024 if (context->paramErrorCheck(@3, $1, $2, $$.param.type))
1027 | parameter_qualifier parameter_type_specifier {
1029 if (context->parameterSamplerErrorCheck(@2, $1, *$2.param.type))
1031 if (context->paramErrorCheck(@2, EvqTemporary, $1, $$.param.type))
1051 parameter_type_specifier
1053 TParameter param = { 0, new TType($1) };
1058 init_declarator_list
1059 : single_declaration {
1062 | init_declarator_list COMMA identifier {
1064 $$.intermAggregate = context->parseDeclarator($$.type, $1.intermAggregate, $3.symbol, @3, *$3.string);
1066 | init_declarator_list COMMA identifier LEFT_BRACKET RIGHT_BRACKET {
1068 context->parseArrayDeclarator($$.type, @3, *$3.string, @4, NULL, NULL);
1070 | init_declarator_list COMMA identifier LEFT_BRACKET constant_expression RIGHT_BRACKET {
1072 $$.intermAggregate = context->parseArrayDeclarator($$.type, @3, *$3.string, @4, $1.intermNode, $5);
1074 | init_declarator_list COMMA identifier EQUAL initializer {
1076 $$.intermAggregate = context->parseInitDeclarator($$.type, $1.intermAggregate, @3, *$3.string, @4, $5);
1081 : fully_specified_type {
1083 $$.intermAggregate = context->parseSingleDeclaration($$.type, @1, "");
1085 | fully_specified_type identifier {
1087 $$.intermAggregate = context->parseSingleDeclaration($$.type, @2, *$2.string);
1089 | fully_specified_type identifier LEFT_BRACKET RIGHT_BRACKET {
1090 context->error(@2, "unsized array declarations not supported", $2.string->c_str());
1094 $$.intermAggregate = context->parseSingleDeclaration($$.type, @2, *$2.string);
1096 | fully_specified_type identifier LEFT_BRACKET constant_expression RIGHT_BRACKET {
1098 $$.intermAggregate = context->parseSingleArrayDeclaration($$.type, @2, *$2.string, @3, $4);
1100 | fully_specified_type identifier EQUAL initializer {
1102 $$.intermAggregate = context->parseSingleInitDeclaration($$.type, @2, *$2.string, @3, $4);
1104 | INVARIANT IDENTIFIER {
1105 VERTEX_ONLY("invariant declaration", @1);
1106 if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "invariant varying"))
1108 $$.type.setBasic(EbtInvariant, EvqInvariantVaryingOut, @2);
1111 context->error(@2, "undeclared identifier declared as invariant", $2.string->c_str());
1114 $$.intermAggregate = 0;
1118 TIntermSymbol *symbol = context->intermediate.addSymbol(0, *$2.string, TType($$.type), @2);
1119 $$.intermAggregate = context->intermediate.makeAggregate(symbol, @2);
1124 fully_specified_type
1129 context->error(@1, "not supported", "first-class array");
1134 | type_qualifier type_specifier {
1135 $$ = context->addFullySpecifiedType($1.qualifier, $1.layoutQualifier, $2);
1139 interpolation_qualifier
1141 $$.qualifier = EvqSmooth;
1144 $$.qualifier = EvqFlat;
1148 parameter_type_qualifier
1156 VERTEX_ONLY("attribute", @1);
1157 ES2_ONLY("attribute", @1);
1158 if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "attribute"))
1160 $$.setBasic(EbtVoid, EvqAttribute, @1);
1163 ES2_ONLY("varying", @1);
1164 if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "varying"))
1166 if (context->shaderType == GL_VERTEX_SHADER)
1167 $$.setBasic(EbtVoid, EvqVaryingOut, @1);
1169 $$.setBasic(EbtVoid, EvqVaryingIn, @1);
1171 | INVARIANT VARYING {
1172 ES2_ONLY("varying", @1);
1173 if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "invariant varying"))
1175 if (context->shaderType == GL_VERTEX_SHADER)
1176 $$.setBasic(EbtVoid, EvqInvariantVaryingOut, @1);
1178 $$.setBasic(EbtVoid, EvqInvariantVaryingIn, @1);
1180 | storage_qualifier {
1181 if ($1.qualifier != EvqConst && !context->symbolTable.atGlobalLevel()) {
1182 context->error(@1, "Local variables can only use the const storage qualifier.", getQualifierString($1.qualifier));
1185 $$.setBasic(EbtVoid, $1.qualifier, @1);
1188 | interpolation_qualifier storage_qualifier {
1189 $$ = context->joinInterpolationQualifiers(@1, $1.qualifier, @2, $2.qualifier);
1191 | interpolation_qualifier {
1192 context->error(@1, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString($1.qualifier));
1195 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1196 $$.setBasic(EbtVoid, qual, @1);
1198 | layout_qualifier {
1199 $$.qualifier = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1200 $$.layoutQualifier = $1;
1202 | layout_qualifier storage_qualifier {
1203 $$.setBasic(EbtVoid, $2.qualifier, @2);
1204 $$.layoutQualifier = $1;
1210 $$.qualifier = EvqConst;
1213 ES3_ONLY("in", @1, "storage qualifier");
1214 $$.qualifier = (context->shaderType == GL_FRAGMENT_SHADER) ? EvqFragmentIn : EvqVertexIn;
1217 ES3_ONLY("out", @1, "storage qualifier");
1218 $$.qualifier = (context->shaderType == GL_FRAGMENT_SHADER) ? EvqFragmentOut : EvqVertexOut;
1220 | CENTROID IN_QUAL {
1221 ES3_ONLY("centroid in", @1, "storage qualifier");
1222 if (context->shaderType == GL_VERTEX_SHADER)
1224 context->error(@1, "invalid storage qualifier", "it is an error to use 'centroid in' in the vertex shader");
1227 $$.qualifier = (context->shaderType == GL_FRAGMENT_SHADER) ? EvqCentroidIn : EvqVertexIn;
1229 | CENTROID OUT_QUAL {
1230 ES3_ONLY("centroid out", @1, "storage qualifier");
1231 if (context->shaderType == GL_FRAGMENT_SHADER)
1233 context->error(@1, "invalid storage qualifier", "it is an error to use 'centroid out' in the fragment shader");
1236 $$.qualifier = (context->shaderType == GL_FRAGMENT_SHADER) ? EvqFragmentOut : EvqCentroidOut;
1239 if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "uniform"))
1241 $$.qualifier = EvqUniform;
1246 : type_specifier_no_prec {
1249 if ($$.precision == EbpUndefined) {
1250 $$.precision = context->symbolTable.getDefaultPrecision($1.type);
1251 if (context->precisionErrorCheck(@1, $$.precision, $1.type)) {
1256 | precision_qualifier type_specifier_no_prec {
1260 if (!SupportsPrecision($2.type)) {
1261 context->error(@1, "illegal type for precision qualifier", getBasicString($2.type));
1271 | MEDIUM_PRECISION {
1280 : LAYOUT LEFT_PAREN layout_qualifier_id_list RIGHT_PAREN {
1281 ES3_ONLY("layout", @1, "qualifier");
1286 layout_qualifier_id_list
1287 : layout_qualifier_id {
1290 | layout_qualifier_id_list COMMA layout_qualifier_id {
1291 $$ = context->joinLayoutQualifiers($1, $3);
1297 $$ = context->parseLayoutQualifier(*$1.string, @1);
1299 | IDENTIFIER EQUAL INTCONSTANT {
1300 $$ = context->parseLayoutQualifier(*$1.string, @1, *$3.string, $3.i, @3);
1302 | IDENTIFIER EQUAL UINTCONSTANT {
1303 $$ = context->parseLayoutQualifier(*$1.string, @1, *$3.string, $3.i, @3);
1307 type_specifier_no_prec
1308 : type_specifier_nonarray {
1311 | type_specifier_nonarray LEFT_BRACKET constant_expression RIGHT_BRACKET {
1314 if (context->arrayTypeErrorCheck(@2, $1))
1318 if (context->arraySizeErrorCheck(@2, $3, size))
1320 $$.setArray(true, size);
1325 type_specifier_nonarray
1327 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1328 $$.setBasic(EbtVoid, qual, @1);
1331 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1332 $$.setBasic(EbtFloat, qual, @1);
1335 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1336 $$.setBasic(EbtInt, qual, @1);
1339 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1340 $$.setBasic(EbtUInt, qual, @1);
1343 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1344 $$.setBasic(EbtBool, qual, @1);
1347 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1348 $$.setBasic(EbtFloat, qual, @1);
1352 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1353 $$.setBasic(EbtFloat, qual, @1);
1357 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1358 $$.setBasic(EbtFloat, qual, @1);
1362 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1363 $$.setBasic(EbtBool, qual, @1);
1367 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1368 $$.setBasic(EbtBool, qual, @1);
1372 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1373 $$.setBasic(EbtBool, qual, @1);
1377 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1378 $$.setBasic(EbtInt, qual, @1);
1382 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1383 $$.setBasic(EbtInt, qual, @1);
1387 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1388 $$.setBasic(EbtInt, qual, @1);
1392 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1393 $$.setBasic(EbtUInt, qual, @1);
1397 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1398 $$.setBasic(EbtUInt, qual, @1);
1402 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1403 $$.setBasic(EbtUInt, qual, @1);
1407 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1408 $$.setBasic(EbtFloat, qual, @1);
1412 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1413 $$.setBasic(EbtFloat, qual, @1);
1417 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1418 $$.setBasic(EbtFloat, qual, @1);
1422 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1423 $$.setBasic(EbtFloat, qual, @1);
1427 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1428 $$.setBasic(EbtFloat, qual, @1);
1432 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1433 $$.setBasic(EbtFloat, qual, @1);
1437 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1438 $$.setBasic(EbtFloat, qual, @1);
1442 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1443 $$.setBasic(EbtFloat, qual, @1);
1447 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1448 $$.setBasic(EbtFloat, qual, @1);
1452 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1453 $$.setBasic(EbtSampler2D, qual, @1);
1456 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1457 $$.setBasic(EbtSampler3D, qual, @1);
1460 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1461 $$.setBasic(EbtSamplerCube, qual, @1);
1464 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1465 $$.setBasic(EbtSampler2DArray, qual, @1);
1468 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1469 $$.setBasic(EbtISampler2D, qual, @1);
1472 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1473 $$.setBasic(EbtISampler3D, qual, @1);
1476 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1477 $$.setBasic(EbtISamplerCube, qual, @1);
1480 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1481 $$.setBasic(EbtISampler2DArray, qual, @1);
1484 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1485 $$.setBasic(EbtUSampler2D, qual, @1);
1488 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1489 $$.setBasic(EbtUSampler3D, qual, @1);
1492 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1493 $$.setBasic(EbtUSamplerCube, qual, @1);
1496 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1497 $$.setBasic(EbtUSampler2DArray, qual, @1);
1500 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1501 $$.setBasic(EbtSampler2DShadow, qual, @1);
1503 | SAMPLERCUBESHADOW {
1504 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1505 $$.setBasic(EbtSamplerCubeShadow, qual, @1);
1507 | SAMPLER2DARRAYSHADOW {
1508 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1509 $$.setBasic(EbtSampler2DArrayShadow, qual, @1);
1511 | SAMPLER_EXTERNAL_OES {
1512 if (!context->supportsExtension("GL_OES_EGL_image_external")) {
1513 context->error(@1, "unsupported type", "samplerExternalOES");
1516 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1517 $$.setBasic(EbtSamplerExternalOES, qual, @1);
1520 if (!context->supportsExtension("GL_ARB_texture_rectangle")) {
1521 context->error(@1, "unsupported type", "sampler2DRect");
1524 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1525 $$.setBasic(EbtSampler2DRect, qual, @1);
1527 | struct_specifier {
1529 $$.qualifier = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1533 // This is for user defined type names. The lexical phase looked up the
1536 TType& structure = static_cast<TVariable*>($1.symbol)->getType();
1537 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1538 $$.setBasic(EbtStruct, qual, @1);
1539 $$.userDef = &structure;
1544 : STRUCT identifier LEFT_BRACE { if (context->enterStructDeclaration(@2, *$2.string)) context->recover(); } struct_declaration_list RIGHT_BRACE {
1545 $$ = context->addStructure(@1, @2, $2.string, $5);
1547 | STRUCT LEFT_BRACE { if (context->enterStructDeclaration(@2, *$2.string)) context->recover(); } struct_declaration_list RIGHT_BRACE {
1548 $$ = context->addStructure(@1, @$, NewPoolTString(""), $4);
1552 struct_declaration_list
1553 : struct_declaration {
1556 | struct_declaration_list struct_declaration {
1558 for (size_t i = 0; i < $2->size(); ++i) {
1559 TField* field = (*$2)[i];
1560 for (size_t j = 0; j < $$->size(); ++j) {
1561 if ((*$$)[j]->name() == field->name()) {
1562 context->error(@2, "duplicate field name in structure:", "struct", field->name().c_str());
1566 $$->push_back(field);
1572 : type_specifier struct_declarator_list SEMICOLON {
1573 $$ = context->addStructDeclaratorList($1, $2);
1575 | type_qualifier type_specifier struct_declarator_list SEMICOLON {
1576 // ES3 Only, but errors should be handled elsewhere
1577 $2.qualifier = $1.qualifier;
1578 $2.layoutQualifier = $1.layoutQualifier;
1579 $$ = context->addStructDeclaratorList($2, $3);
1583 struct_declarator_list
1584 : struct_declarator {
1585 $$ = NewPoolTFieldList();
1588 | struct_declarator_list COMMA struct_declarator {
1595 if (context->reservedErrorCheck(@1, *$1.string))
1598 TType* type = new TType(EbtVoid, EbpUndefined);
1599 $$ = new TField(type, $1.string, @1);
1601 | identifier LEFT_BRACKET constant_expression RIGHT_BRACKET {
1602 if (context->reservedErrorCheck(@1, *$1.string))
1605 TType* type = new TType(EbtVoid, EbpUndefined);
1607 if (context->arraySizeErrorCheck(@3, $3, size))
1609 type->setArraySize(size);
1611 $$ = new TField(type, $1.string, @1);
1616 : assignment_expression { $$ = $1; }
1619 declaration_statement
1620 : declaration { $$ = $1; }
1624 : compound_statement { $$ = $1; }
1625 | simple_statement { $$ = $1; }
1628 // Grammar Note: No labeled statements; 'goto' is not supported.
1631 : declaration_statement { $$ = $1; }
1632 | expression_statement { $$ = $1; }
1633 | selection_statement { $$ = $1; }
1634 | iteration_statement { $$ = $1; }
1635 | jump_statement { $$ = $1; }
1639 : LEFT_BRACE RIGHT_BRACE { $$ = 0; }
1640 | LEFT_BRACE { context->symbolTable.push(); } statement_list { context->symbolTable.pop(); } RIGHT_BRACE {
1642 $3->setOp(EOpSequence);
1649 statement_no_new_scope
1650 : compound_statement_no_new_scope { $$ = $1; }
1651 | simple_statement { $$ = $1; }
1654 statement_with_scope
1655 : { context->symbolTable.push(); } compound_statement_no_new_scope { context->symbolTable.pop(); $$ = $2; }
1656 | { context->symbolTable.push(); } simple_statement { context->symbolTable.pop(); $$ = $2; }
1659 compound_statement_no_new_scope
1660 // Statement that doesn't create a new scope, for selection_statement, iteration_statement
1661 : LEFT_BRACE RIGHT_BRACE {
1664 | LEFT_BRACE statement_list RIGHT_BRACE {
1666 $2->setOp(EOpSequence);
1675 $$ = context->intermediate.makeAggregate($1, @$);
1677 | statement_list statement {
1678 $$ = context->intermediate.growAggregate($1, $2, @$);
1682 expression_statement
1683 : SEMICOLON { $$ = 0; }
1684 | expression SEMICOLON { $$ = static_cast<TIntermNode*>($1); }
1688 : IF LEFT_PAREN expression RIGHT_PAREN selection_rest_statement {
1689 if (context->boolErrorCheck(@1, $3))
1691 $$ = context->intermediate.addSelection($3, $5, @1);
1695 selection_rest_statement
1696 : statement_with_scope ELSE statement_with_scope {
1700 | statement_with_scope {
1706 // Grammar Note: No 'switch'. Switch statements not supported.
1709 // In 1996 c++ draft, conditions can include single declarations
1712 if (context->boolErrorCheck($1->getLine(), $1))
1715 | fully_specified_type identifier EQUAL initializer {
1716 TIntermNode* intermNode;
1717 if (context->structQualifierErrorCheck(@2, $1))
1719 if (context->boolErrorCheck(@2, $1))
1722 if (!context->executeInitializer(@2, *$2.string, $1, $4, intermNode))
1732 : WHILE LEFT_PAREN { context->symbolTable.push(); ++context->loopNestingLevel; } condition RIGHT_PAREN statement_no_new_scope {
1733 context->symbolTable.pop();
1734 $$ = context->intermediate.addLoop(ELoopWhile, 0, $4, 0, $6, @1);
1735 --context->loopNestingLevel;
1737 | DO { ++context->loopNestingLevel; } statement_with_scope WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON {
1738 if (context->boolErrorCheck(@8, $6))
1741 $$ = context->intermediate.addLoop(ELoopDoWhile, 0, $6, 0, $3, @4);
1742 --context->loopNestingLevel;
1744 | FOR LEFT_PAREN { context->symbolTable.push(); ++context->loopNestingLevel; } for_init_statement for_rest_statement RIGHT_PAREN statement_no_new_scope {
1745 context->symbolTable.pop();
1746 $$ = context->intermediate.addLoop(ELoopFor, $4, reinterpret_cast<TIntermTyped*>($5.node1), reinterpret_cast<TIntermTyped*>($5.node2), $7, @1);
1747 --context->loopNestingLevel;
1752 : expression_statement {
1755 | declaration_statement {
1764 | /* May be null */ {
1770 : conditionopt SEMICOLON {
1774 | conditionopt SEMICOLON expression {
1781 : CONTINUE SEMICOLON {
1782 if (context->loopNestingLevel <= 0) {
1783 context->error(@1, "continue statement only allowed in loops", "");
1786 $$ = context->intermediate.addBranch(EOpContinue, @1);
1789 if (context->loopNestingLevel <= 0) {
1790 context->error(@1, "break statement only allowed in loops", "");
1793 $$ = context->intermediate.addBranch(EOpBreak, @1);
1795 | RETURN SEMICOLON {
1796 $$ = context->intermediate.addBranch(EOpReturn, @1);
1797 if (context->currentFunctionType->getBasicType() != EbtVoid) {
1798 context->error(@1, "non-void function must return a value", "return");
1802 | RETURN expression SEMICOLON {
1803 $$ = context->intermediate.addBranch(EOpReturn, $2, @1);
1804 context->functionReturnsValue = true;
1805 if (context->currentFunctionType->getBasicType() == EbtVoid) {
1806 context->error(@1, "void function cannot return a value", "return");
1808 } else if (*(context->currentFunctionType) != $2->getType()) {
1809 context->error(@1, "function return is not matching type:", "return");
1813 | DISCARD SEMICOLON {
1814 FRAG_ONLY("discard", @1);
1815 $$ = context->intermediate.addBranch(EOpKill, @1);
1819 // Grammar Note: No 'goto'. Gotos are not supported.
1822 : external_declaration {
1824 context->treeRoot = $$;
1826 | translation_unit external_declaration {
1827 $$ = context->intermediate.growAggregate($1, $2, @$);
1828 context->treeRoot = $$;
1832 external_declaration
1833 : function_definition {
1842 : function_prototype {
1843 TFunction* function = $1.function;
1845 const TSymbol *builtIn = context->symbolTable.findBuiltIn(function->getMangledName(), context->shaderVersion);
1849 context->error(@1, "built-in functions cannot be redefined", function->getName().c_str());
1853 TFunction* prevDec = static_cast<TFunction*>(context->symbolTable.find(function->getMangledName(), context->shaderVersion));
1855 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1856 // as it would have just been put in the symbol table. Otherwise, we're looking up
1857 // an earlier occurance.
1859 if (prevDec->isDefined()) {
1861 // Then this function already has a body.
1863 context->error(@1, "function already has a body", function->getName().c_str());
1866 prevDec->setDefined();
1869 // Raise error message if main function takes any parameters or return anything other than void
1871 if (function->getName() == "main") {
1872 if (function->getParamCount() > 0) {
1873 context->error(@1, "function cannot take any parameter(s)", function->getName().c_str());
1876 if (function->getReturnType().getBasicType() != EbtVoid) {
1877 context->error(@1, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1883 // Remember the return type for later checking for RETURN statements.
1885 context->currentFunctionType = &(prevDec->getReturnType());
1886 context->functionReturnsValue = false;
1889 // Insert parameters into the symbol table.
1890 // If the parameter has no name, it's not an error, just don't insert it
1891 // (could be used for unused args).
1893 // Also, accumulate the list of parameters into the HIL, so lower level code
1894 // knows where to find parameters.
1896 TIntermAggregate* paramNodes = new TIntermAggregate;
1897 for (size_t i = 0; i < function->getParamCount(); i++) {
1898 const TParameter& param = function->getParam(i);
1899 if (param.name != 0) {
1900 TVariable *variable = new TVariable(param.name, *param.type);
1902 // Insert the parameters with name in the symbol table.
1904 if (! context->symbolTable.declare(variable)) {
1905 context->error(@1, "redefinition", variable->getName().c_str());
1911 // Add the parameter to the HIL
1913 paramNodes = context->intermediate.growAggregate(
1915 context->intermediate.addSymbol(variable->getUniqueId(),
1916 variable->getName(),
1917 variable->getType(), @1),
1920 paramNodes = context->intermediate.growAggregate(paramNodes, context->intermediate.addSymbol(0, "", *param.type, @1), @1);
1923 context->intermediate.setAggregateOperator(paramNodes, EOpParameters, @1);
1924 $1.intermAggregate = paramNodes;
1925 context->loopNestingLevel = 0;
1927 compound_statement_no_new_scope {
1928 //?? Check that all paths return a value if return type != void ?
1929 // May be best done as post process phase on intermediate code
1930 if (context->currentFunctionType->getBasicType() != EbtVoid && ! context->functionReturnsValue) {
1931 context->error(@1, "function does not return a value:", "", $1.function->getName().c_str());
1935 $$ = context->intermediate.growAggregate($1.intermAggregate, $3, @$);
1936 context->intermediate.setAggregateOperator($$, EOpFunction, @1);
1937 $$->getAsAggregate()->setName($1.function->getMangledName().c_str());
1938 $$->getAsAggregate()->setType($1.function->getReturnType());
1940 // store the pragma information for debug and optimize and other vendor specific
1941 // information. This information can be queried from the parse tree
1942 $$->getAsAggregate()->setOptimize(context->pragma().optimize);
1943 $$->getAsAggregate()->setDebug(context->pragma().debug);
1945 context->symbolTable.pop();
1951 int glslang_parse(TParseContext* context) {
1952 return yyparse(context);