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 TVariable *variable = context->getNamedVariable(@1, $1.string, $1.symbol);
214 if (variable->getType().getQualifier() == EvqConst)
216 ConstantUnion* constArray = variable->getConstPointer();
217 TType t(variable->getType());
218 $$ = context->intermediate.addConstantUnion(constArray, t, @1);
222 $$ = context->intermediate.addSymbol(variable->getUniqueId(),
228 // don't delete $1.string, it's used by error recovery, and the pool
229 // pop will reclaim the memory
234 : variable_identifier {
238 ConstantUnion *unionArray = new ConstantUnion[1];
239 unionArray->setIConst($1.i);
240 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), @1);
243 ConstantUnion *unionArray = new ConstantUnion[1];
244 unionArray->setUConst($1.u);
245 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtUInt, EbpUndefined, EvqConst), @1);
248 ConstantUnion *unionArray = new ConstantUnion[1];
249 unionArray->setFConst($1.f);
250 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), @1);
253 ConstantUnion *unionArray = new ConstantUnion[1];
254 unionArray->setBConst($1.b);
255 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @1);
257 | LEFT_PAREN expression RIGHT_PAREN {
263 : primary_expression {
266 | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET {
267 $$ = context->addIndexExpression($1, @2, $3);
272 | postfix_expression DOT identifier {
273 $$ = context->addFieldSelectionExpression($1, @2, *$3.string, @3);
275 | postfix_expression INC_OP {
276 if (context->lValueErrorCheck(@2, "++", $1))
278 $$ = context->intermediate.addUnaryMath(EOpPostIncrement, $1, @2);
280 context->unaryOpError(@2, "++", $1->getCompleteString());
285 | postfix_expression DEC_OP {
286 if (context->lValueErrorCheck(@2, "--", $1))
288 $$ = context->intermediate.addUnaryMath(EOpPostDecrement, $1, @2);
290 context->unaryOpError(@2, "--", $1->getCompleteString());
299 if (context->integerErrorCheck($1, "[]"))
306 : function_call_or_method {
307 TFunction* fnCall = $1.function;
308 TOperator op = fnCall->getBuiltInOp();
313 // Then this should be a constructor.
314 // Don't go through the symbol table for constructors.
315 // Their parameters will be verified algorithmically.
317 TType type(EbtVoid, EbpUndefined); // use this to get the type back
318 if (context->constructorErrorCheck(@1, $1.intermNode, *fnCall, op, &type)) {
322 // It's a constructor, of type 'type'.
324 $$ = context->addConstructor($1.intermNode, &type, op, fnCall, @1);
329 $$ = context->intermediate.setAggregateOperator(0, op, @1);
334 // Not a constructor. Find it in the symbol table.
336 const TFunction* fnCandidate;
338 fnCandidate = context->findFunction(@1, fnCall, context->shaderVersion, &builtIn);
341 // A declared function.
343 if (builtIn && !fnCandidate->getExtension().empty() &&
344 context->extensionErrorCheck(@1, fnCandidate->getExtension())) {
347 op = fnCandidate->getBuiltInOp();
348 if (builtIn && op != EOpNull) {
350 // A function call mapped to a built-in operation.
352 if (fnCandidate->getParamCount() == 1) {
354 // Treat it like a built-in unary operator.
356 $$ = context->intermediate.addUnaryMath(op, $1.intermNode, @1);
358 std::stringstream extraInfoStream;
359 extraInfoStream << "built in unary operator function. Type: " << static_cast<TIntermTyped*>($1.intermNode)->getCompleteString();
360 std::string extraInfo = extraInfoStream.str();
361 context->error($1.intermNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
365 $$ = context->intermediate.setAggregateOperator($1.intermAggregate, op, @1);
368 // This is a real function call
370 $$ = context->intermediate.setAggregateOperator($1.intermAggregate, EOpFunctionCall, @1);
371 $$->setType(fnCandidate->getReturnType());
373 // this is how we know whether the given function is a builtIn function or a user defined function
374 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
375 // if builtIn == true, it's definitely a builtIn function with EOpNull
377 $$->getAsAggregate()->setUserDefined();
378 $$->getAsAggregate()->setName(fnCandidate->getMangledName());
381 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i) {
382 qual = fnCandidate->getParam(i).type->getQualifier();
383 if (qual == EvqOut || qual == EvqInOut) {
384 if (context->lValueErrorCheck($$->getLine(), "assign", (*($$->getAsAggregate()->getSequence()))[i]->getAsTyped())) {
385 context->error($1.intermNode->getLine(), "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
391 $$->setType(fnCandidate->getReturnType());
393 // error message was put out by PaFindFunction()
394 // Put on a dummy node for error recovery
395 ConstantUnion *unionArray = new ConstantUnion[1];
396 unionArray->setFConst(0.0f);
397 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), @1);
405 function_call_or_method
406 : function_call_generic {
409 | postfix_expression DOT function_call_generic {
410 context->error(@3, "methods are not supported", "");
416 function_call_generic
417 : function_call_header_with_parameters RIGHT_PAREN {
420 | function_call_header_no_parameters RIGHT_PAREN {
425 function_call_header_no_parameters
426 : function_call_header VOID_TYPE {
430 | function_call_header {
436 function_call_header_with_parameters
437 : function_call_header assignment_expression {
438 TParameter param = { 0, new TType($2->getType()) };
439 $1->addParameter(param);
443 | function_call_header_with_parameters COMMA assignment_expression {
444 TParameter param = { 0, new TType($3->getType()) };
445 $1.function->addParameter(param);
446 $$.function = $1.function;
447 $$.intermNode = context->intermediate.growAggregate($1.intermNode, $3, @2);
452 : function_identifier LEFT_PAREN {
457 // Grammar Note: Constructors look like functions, but are recognized as types.
460 : type_specifier_nonarray {
461 $$ = context->addConstructorFunc($1);
464 if (context->reservedErrorCheck(@1, *$1.string))
466 TType type(EbtVoid, EbpUndefined);
467 TFunction *function = new TFunction($1.string, type);
473 : postfix_expression {
476 | INC_OP unary_expression {
477 if (context->lValueErrorCheck(@1, "++", $2))
479 $$ = context->intermediate.addUnaryMath(EOpPreIncrement, $2, @1);
481 context->unaryOpError(@1, "++", $2->getCompleteString());
486 | DEC_OP unary_expression {
487 if (context->lValueErrorCheck(@1, "--", $2))
489 $$ = context->intermediate.addUnaryMath(EOpPreDecrement, $2, @1);
491 context->unaryOpError(@1, "--", $2->getCompleteString());
496 | unary_operator unary_expression {
497 if ($1.op != EOpNull) {
498 $$ = context->intermediate.addUnaryMath($1.op, $2, @1);
500 const char* errorOp = "";
502 case EOpNegative: errorOp = "-"; break;
503 case EOpLogicalNot: errorOp = "!"; break;
506 context->unaryOpError(@1, errorOp, $2->getCompleteString());
514 // Grammar Note: No traditional style type casts.
517 : PLUS { $$.op = EOpNull; }
518 | DASH { $$.op = EOpNegative; }
519 | BANG { $$.op = EOpLogicalNot; }
521 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
523 multiplicative_expression
524 : unary_expression { $$ = $1; }
525 | multiplicative_expression STAR unary_expression {
526 $$ = context->intermediate.addBinaryMath(EOpMul, $1, $3, @2);
528 context->binaryOpError(@2, "*", $1->getCompleteString(), $3->getCompleteString());
533 | multiplicative_expression SLASH unary_expression {
534 $$ = context->intermediate.addBinaryMath(EOpDiv, $1, $3, @2);
536 context->binaryOpError(@2, "/", $1->getCompleteString(), $3->getCompleteString());
544 : multiplicative_expression { $$ = $1; }
545 | additive_expression PLUS multiplicative_expression {
546 $$ = context->intermediate.addBinaryMath(EOpAdd, $1, $3, @2);
548 context->binaryOpError(@2, "+", $1->getCompleteString(), $3->getCompleteString());
553 | additive_expression DASH multiplicative_expression {
554 $$ = context->intermediate.addBinaryMath(EOpSub, $1, $3, @2);
556 context->binaryOpError(@2, "-", $1->getCompleteString(), $3->getCompleteString());
564 : additive_expression { $$ = $1; }
567 relational_expression
568 : shift_expression { $$ = $1; }
569 | relational_expression LEFT_ANGLE shift_expression {
570 $$ = context->intermediate.addBinaryMath(EOpLessThan, $1, $3, @2);
572 context->binaryOpError(@2, "<", $1->getCompleteString(), $3->getCompleteString());
574 ConstantUnion *unionArray = new ConstantUnion[1];
575 unionArray->setBConst(false);
576 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
579 | relational_expression RIGHT_ANGLE shift_expression {
580 $$ = context->intermediate.addBinaryMath(EOpGreaterThan, $1, $3, @2);
582 context->binaryOpError(@2, ">", $1->getCompleteString(), $3->getCompleteString());
584 ConstantUnion *unionArray = new ConstantUnion[1];
585 unionArray->setBConst(false);
586 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
589 | relational_expression LE_OP shift_expression {
590 $$ = context->intermediate.addBinaryMath(EOpLessThanEqual, $1, $3, @2);
592 context->binaryOpError(@2, "<=", $1->getCompleteString(), $3->getCompleteString());
594 ConstantUnion *unionArray = new ConstantUnion[1];
595 unionArray->setBConst(false);
596 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
599 | relational_expression GE_OP shift_expression {
600 $$ = context->intermediate.addBinaryMath(EOpGreaterThanEqual, $1, $3, @2);
602 context->binaryOpError(@2, ">=", $1->getCompleteString(), $3->getCompleteString());
604 ConstantUnion *unionArray = new ConstantUnion[1];
605 unionArray->setBConst(false);
606 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
612 : relational_expression { $$ = $1; }
613 | equality_expression EQ_OP relational_expression {
614 $$ = context->intermediate.addBinaryMath(EOpEqual, $1, $3, @2);
616 context->binaryOpError(@2, "==", $1->getCompleteString(), $3->getCompleteString());
618 ConstantUnion *unionArray = new ConstantUnion[1];
619 unionArray->setBConst(false);
620 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
623 | equality_expression NE_OP relational_expression {
624 $$ = context->intermediate.addBinaryMath(EOpNotEqual, $1, $3, @2);
626 context->binaryOpError(@2, "!=", $1->getCompleteString(), $3->getCompleteString());
628 ConstantUnion *unionArray = new ConstantUnion[1];
629 unionArray->setBConst(false);
630 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
636 : equality_expression { $$ = $1; }
639 exclusive_or_expression
640 : and_expression { $$ = $1; }
643 inclusive_or_expression
644 : exclusive_or_expression { $$ = $1; }
647 logical_and_expression
648 : inclusive_or_expression { $$ = $1; }
649 | logical_and_expression AND_OP inclusive_or_expression {
650 $$ = context->intermediate.addBinaryMath(EOpLogicalAnd, $1, $3, @2);
652 context->binaryOpError(@2, "&&", $1->getCompleteString(), $3->getCompleteString());
654 ConstantUnion *unionArray = new ConstantUnion[1];
655 unionArray->setBConst(false);
656 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
661 logical_xor_expression
662 : logical_and_expression { $$ = $1; }
663 | logical_xor_expression XOR_OP logical_and_expression {
664 $$ = context->intermediate.addBinaryMath(EOpLogicalXor, $1, $3, @2);
666 context->binaryOpError(@2, "^^", $1->getCompleteString(), $3->getCompleteString());
668 ConstantUnion *unionArray = new ConstantUnion[1];
669 unionArray->setBConst(false);
670 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
675 logical_or_expression
676 : logical_xor_expression { $$ = $1; }
677 | logical_or_expression OR_OP logical_xor_expression {
678 $$ = context->intermediate.addBinaryMath(EOpLogicalOr, $1, $3, @2);
680 context->binaryOpError(@2, "||", $1->getCompleteString(), $3->getCompleteString());
682 ConstantUnion *unionArray = new ConstantUnion[1];
683 unionArray->setBConst(false);
684 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), @2);
689 conditional_expression
690 : logical_or_expression { $$ = $1; }
691 | logical_or_expression QUESTION expression COLON assignment_expression {
692 if (context->boolErrorCheck(@2, $1))
695 $$ = context->intermediate.addSelection($1, $3, $5, @2);
696 if ($3->getType() != $5->getType())
700 context->binaryOpError(@2, ":", $3->getCompleteString(), $5->getCompleteString());
707 assignment_expression
708 : conditional_expression { $$ = $1; }
709 | unary_expression assignment_operator assignment_expression {
710 if (context->lValueErrorCheck(@2, "assign", $1))
712 $$ = context->intermediate.addAssign($2.op, $1, $3, @2);
714 context->assignError(@2, "assign", $1->getCompleteString(), $3->getCompleteString());
722 : EQUAL { $$.op = EOpAssign; }
723 | MUL_ASSIGN { $$.op = EOpMulAssign; }
724 | DIV_ASSIGN { $$.op = EOpDivAssign; }
725 | ADD_ASSIGN { $$.op = EOpAddAssign; }
726 | SUB_ASSIGN { $$.op = EOpSubAssign; }
730 : assignment_expression {
733 | expression COMMA assignment_expression {
734 $$ = context->intermediate.addComma($1, $3, @2);
736 context->binaryOpError(@2, ",", $1->getCompleteString(), $3->getCompleteString());
744 : conditional_expression {
745 if (context->constErrorCheck($1))
752 : IDENTIFIER LEFT_BRACE {
753 if (context->enterStructDeclaration(@1, *$1.string))
760 : function_prototype SEMICOLON {
761 TFunction &function = *($1.function);
763 TIntermAggregate *prototype = new TIntermAggregate;
764 prototype->setType(function.getReturnType());
765 prototype->setName(function.getName());
767 for (size_t i = 0; i < function.getParamCount(); i++)
769 const TParameter ¶m = function.getParam(i);
772 TVariable variable(param.name, *param.type);
774 prototype = context->intermediate.growAggregate(prototype, context->intermediate.addSymbol(variable.getUniqueId(), variable.getName(), variable.getType(), @1), @1);
778 prototype = context->intermediate.growAggregate(prototype, context->intermediate.addSymbol(0, "", *param.type, @1), @1);
782 prototype->setOp(EOpPrototype);
785 context->symbolTable.pop();
787 | init_declarator_list SEMICOLON {
788 TIntermAggregate *aggNode = $1.intermAggregate;
789 if (aggNode && aggNode->getOp() == EOpNull)
790 aggNode->setOp(EOpDeclaration);
793 | PRECISION precision_qualifier type_specifier_no_prec SEMICOLON {
794 if (($2 == EbpHigh) && (context->shaderType == GL_FRAGMENT_SHADER) && !context->fragmentPrecisionHigh) {
795 context->error(@1, "precision is not supported in fragment shader", "highp");
798 if (!context->symbolTable.setDefaultPrecision( $3, $2 )) {
799 context->error(@1, "illegal type argument for default precision qualifier", getBasicString($3.type));
804 | type_qualifier enter_struct struct_declaration_list RIGHT_BRACE SEMICOLON {
805 ES3_ONLY(getQualifierString($1.qualifier), @1, "interface blocks");
806 $$ = context->addInterfaceBlock($1, @2, *$2.string, $3, NULL, @$, NULL, @$);
808 | type_qualifier enter_struct struct_declaration_list RIGHT_BRACE IDENTIFIER SEMICOLON {
809 ES3_ONLY(getQualifierString($1.qualifier), @1, "interface blocks");
810 $$ = context->addInterfaceBlock($1, @2, *$2.string, $3, $5.string, @5, NULL, @$);
812 | type_qualifier enter_struct struct_declaration_list RIGHT_BRACE IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET SEMICOLON {
813 ES3_ONLY(getQualifierString($1.qualifier), @1, "interface blocks");
814 $$ = context->addInterfaceBlock($1, @2, *$2.string, $3, $5.string, @5, $7, @6);
816 | type_qualifier SEMICOLON {
817 context->parseGlobalLayoutQualifier($1);
823 : function_declarator RIGHT_PAREN {
825 // Multiple declarations of the same function are allowed.
827 // If this is a definition, the definition production code will check for redefinitions
828 // (we don't know at this point if it's a definition or not).
830 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
832 TFunction* prevDec = static_cast<TFunction*>(context->symbolTable.find($1->getMangledName(), context->shaderVersion));
834 if (prevDec->getReturnType() != $1->getReturnType()) {
835 context->error(@2, "overloaded functions must have the same return type", $1->getReturnType().getBasicString());
838 for (size_t i = 0; i < prevDec->getParamCount(); ++i) {
839 if (prevDec->getParam(i).type->getQualifier() != $1->getParam(i).type->getQualifier()) {
840 context->error(@2, "overloaded functions must have the same parameter qualifiers", $1->getParam(i).type->getQualifierString());
847 // Check for previously declared variables using the same name.
849 TSymbol *prevSym = context->symbolTable.find($1->getName(), context->shaderVersion);
852 if (!prevSym->isFunction())
854 context->error(@2, "redefinition", $1->getName().c_str(), "function");
860 // Insert the unmangled name to detect potential future redefinition as a variable.
861 TFunction *function = new TFunction(NewPoolTString($1->getName().c_str()), $1->getReturnType());
862 context->symbolTable.getOuterLevel()->insert(function);
866 // If this is a redeclaration, it could also be a definition,
867 // in which case, we want to use the variable names from this one, and not the one that's
868 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
872 // We're at the inner scope level of the function's arguments and body statement.
873 // Add the function prototype to the surrounding scope instead.
874 context->symbolTable.getOuterLevel()->insert($$.function);
882 | function_header_with_parameters {
888 function_header_with_parameters
889 : function_header parameter_declaration {
892 if ($2.param.type->getBasicType() != EbtVoid)
893 $1->addParameter($2.param);
895 delete $2.param.type;
897 | function_header_with_parameters COMMA parameter_declaration {
899 // Only first parameter of one-parameter functions can be void
900 // The check for named parameters not being void is done in parameter_declarator
902 if ($3.param.type->getBasicType() == EbtVoid) {
904 // This parameter > first is void
906 context->error(@2, "cannot be an argument type except for '(void)'", "void");
908 delete $3.param.type;
912 $1->addParameter($3.param);
918 : fully_specified_type IDENTIFIER LEFT_PAREN {
919 if ($1.qualifier != EvqGlobal && $1.qualifier != EvqTemporary) {
920 context->error(@2, "no qualifiers allowed for function return", getQualifierString($1.qualifier));
923 // make sure a sampler is not involved as well...
924 if (context->structQualifierErrorCheck(@2, $1))
927 // Add the function as a prototype after parsing it (we do not support recursion)
930 function = new TFunction($2.string, type);
933 context->symbolTable.push();
939 : type_specifier identifier {
940 if ($1.type == EbtVoid) {
941 context->error(@2, "illegal use of type 'void'", $2.string->c_str());
944 if (context->reservedErrorCheck(@2, *$2.string))
946 TParameter param = {$2.string, new TType($1)};
949 | type_specifier identifier LEFT_BRACKET constant_expression RIGHT_BRACKET {
950 // Check that we can make an array out of this type
951 if (context->arrayTypeErrorCheck(@3, $1))
954 if (context->reservedErrorCheck(@2, *$2.string))
958 if (context->arraySizeErrorCheck(@3, $4, size))
960 $1.setArray(true, size);
962 TType* type = new TType($1);
963 TParameter param = { $2.string, type };
968 parameter_declaration
970 // The only parameter qualifier a parameter can have are
971 // IN_QUAL, OUT_QUAL, INOUT_QUAL, or CONST.
977 : parameter_type_qualifier parameter_qualifier parameter_declarator {
979 if (context->paramErrorCheck(@3, $1, $2, $$.param.type))
982 | parameter_qualifier parameter_declarator {
984 if (context->parameterSamplerErrorCheck(@2, $1, *$2.param.type))
986 if (context->paramErrorCheck(@2, EvqTemporary, $1, $$.param.type))
992 | parameter_type_qualifier parameter_qualifier parameter_type_specifier {
994 if (context->paramErrorCheck(@3, $1, $2, $$.param.type))
997 | parameter_qualifier parameter_type_specifier {
999 if (context->parameterSamplerErrorCheck(@2, $1, *$2.param.type))
1001 if (context->paramErrorCheck(@2, EvqTemporary, $1, $$.param.type))
1021 parameter_type_specifier
1023 TParameter param = { 0, new TType($1) };
1028 init_declarator_list
1029 : single_declaration {
1032 | init_declarator_list COMMA identifier {
1034 $$.intermAggregate = context->parseDeclarator($$.type, $1.intermAggregate, $3.symbol, @3, *$3.string);
1036 | init_declarator_list COMMA identifier LEFT_BRACKET RIGHT_BRACKET {
1038 context->parseArrayDeclarator($$.type, @3, *$3.string, @4, NULL, NULL);
1040 | init_declarator_list COMMA identifier LEFT_BRACKET constant_expression RIGHT_BRACKET {
1042 $$.intermAggregate = context->parseArrayDeclarator($$.type, @3, *$3.string, @4, $1.intermNode, $5);
1044 | init_declarator_list COMMA identifier EQUAL initializer {
1046 $$.intermAggregate = context->parseInitDeclarator($$.type, $1.intermAggregate, @3, *$3.string, @4, $5);
1051 : fully_specified_type {
1053 $$.intermAggregate = context->parseSingleDeclaration($$.type, @1, "");
1055 | fully_specified_type identifier {
1057 $$.intermAggregate = context->parseSingleDeclaration($$.type, @2, *$2.string);
1059 | fully_specified_type identifier LEFT_BRACKET RIGHT_BRACKET {
1060 context->error(@2, "unsized array declarations not supported", $2.string->c_str());
1064 $$.intermAggregate = context->parseSingleDeclaration($$.type, @2, *$2.string);
1066 | fully_specified_type identifier LEFT_BRACKET constant_expression RIGHT_BRACKET {
1068 $$.intermAggregate = context->parseSingleArrayDeclaration($$.type, @2, *$2.string, @3, $4);
1070 | fully_specified_type identifier EQUAL initializer {
1072 $$.intermAggregate = context->parseSingleInitDeclaration($$.type, @2, *$2.string, @3, $4);
1074 | INVARIANT IDENTIFIER {
1075 // $$.type is not used in invariant declarations.
1076 $$.intermAggregate = context->parseInvariantDeclaration(@1, @2, $2.string, $2.symbol);
1080 fully_specified_type
1085 context->error(@1, "not supported", "first-class array");
1090 | type_qualifier type_specifier {
1091 $$ = context->addFullySpecifiedType($1.qualifier, $1.layoutQualifier, $2);
1095 interpolation_qualifier
1097 $$.qualifier = EvqSmooth;
1100 $$.qualifier = EvqFlat;
1104 parameter_type_qualifier
1112 VERTEX_ONLY("attribute", @1);
1113 ES2_ONLY("attribute", @1);
1114 if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "attribute"))
1116 $$.setBasic(EbtVoid, EvqAttribute, @1);
1119 ES2_ONLY("varying", @1);
1120 if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "varying"))
1122 if (context->shaderType == GL_VERTEX_SHADER)
1123 $$.setBasic(EbtVoid, EvqVaryingOut, @1);
1125 $$.setBasic(EbtVoid, EvqVaryingIn, @1);
1127 | INVARIANT VARYING {
1128 ES2_ONLY("varying", @1);
1129 if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "invariant varying"))
1131 if (context->shaderType == GL_VERTEX_SHADER)
1132 $$.setBasic(EbtVoid, EvqInvariantVaryingOut, @1);
1134 $$.setBasic(EbtVoid, EvqInvariantVaryingIn, @1);
1136 | storage_qualifier {
1137 if ($1.qualifier != EvqConst && !context->symbolTable.atGlobalLevel()) {
1138 context->error(@1, "Local variables can only use the const storage qualifier.", getQualifierString($1.qualifier));
1141 $$.setBasic(EbtVoid, $1.qualifier, @1);
1144 | interpolation_qualifier storage_qualifier {
1145 $$ = context->joinInterpolationQualifiers(@1, $1.qualifier, @2, $2.qualifier);
1147 | interpolation_qualifier {
1148 context->error(@1, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getInterpolationString($1.qualifier));
1151 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1152 $$.setBasic(EbtVoid, qual, @1);
1154 | layout_qualifier {
1155 $$.qualifier = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1156 $$.layoutQualifier = $1;
1158 | layout_qualifier storage_qualifier {
1159 $$.setBasic(EbtVoid, $2.qualifier, @2);
1160 $$.layoutQualifier = $1;
1166 $$.qualifier = EvqConst;
1169 ES3_ONLY("in", @1, "storage qualifier");
1170 $$.qualifier = (context->shaderType == GL_FRAGMENT_SHADER) ? EvqFragmentIn : EvqVertexIn;
1173 ES3_ONLY("out", @1, "storage qualifier");
1174 $$.qualifier = (context->shaderType == GL_FRAGMENT_SHADER) ? EvqFragmentOut : EvqVertexOut;
1176 | CENTROID IN_QUAL {
1177 ES3_ONLY("centroid in", @1, "storage qualifier");
1178 if (context->shaderType == GL_VERTEX_SHADER)
1180 context->error(@1, "invalid storage qualifier", "it is an error to use 'centroid in' in the vertex shader");
1183 $$.qualifier = (context->shaderType == GL_FRAGMENT_SHADER) ? EvqCentroidIn : EvqVertexIn;
1185 | CENTROID OUT_QUAL {
1186 ES3_ONLY("centroid out", @1, "storage qualifier");
1187 if (context->shaderType == GL_FRAGMENT_SHADER)
1189 context->error(@1, "invalid storage qualifier", "it is an error to use 'centroid out' in the fragment shader");
1192 $$.qualifier = (context->shaderType == GL_FRAGMENT_SHADER) ? EvqFragmentOut : EvqCentroidOut;
1195 if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "uniform"))
1197 $$.qualifier = EvqUniform;
1202 : type_specifier_no_prec {
1205 if ($$.precision == EbpUndefined) {
1206 $$.precision = context->symbolTable.getDefaultPrecision($1.type);
1207 if (context->precisionErrorCheck(@1, $$.precision, $1.type)) {
1212 | precision_qualifier type_specifier_no_prec {
1216 if (!SupportsPrecision($2.type)) {
1217 context->error(@1, "illegal type for precision qualifier", getBasicString($2.type));
1227 | MEDIUM_PRECISION {
1236 : LAYOUT LEFT_PAREN layout_qualifier_id_list RIGHT_PAREN {
1237 ES3_ONLY("layout", @1, "qualifier");
1242 layout_qualifier_id_list
1243 : layout_qualifier_id {
1246 | layout_qualifier_id_list COMMA layout_qualifier_id {
1247 $$ = context->joinLayoutQualifiers($1, $3);
1253 $$ = context->parseLayoutQualifier(*$1.string, @1);
1255 | IDENTIFIER EQUAL INTCONSTANT {
1256 $$ = context->parseLayoutQualifier(*$1.string, @1, *$3.string, $3.i, @3);
1258 | IDENTIFIER EQUAL UINTCONSTANT {
1259 $$ = context->parseLayoutQualifier(*$1.string, @1, *$3.string, $3.i, @3);
1263 type_specifier_no_prec
1264 : type_specifier_nonarray {
1267 | type_specifier_nonarray LEFT_BRACKET constant_expression RIGHT_BRACKET {
1270 if (context->arrayTypeErrorCheck(@2, $1))
1274 if (context->arraySizeErrorCheck(@2, $3, size))
1276 $$.setArray(true, size);
1281 type_specifier_nonarray
1283 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1284 $$.setBasic(EbtVoid, qual, @1);
1287 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1288 $$.setBasic(EbtFloat, qual, @1);
1291 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1292 $$.setBasic(EbtInt, qual, @1);
1295 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1296 $$.setBasic(EbtUInt, qual, @1);
1299 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1300 $$.setBasic(EbtBool, qual, @1);
1303 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1304 $$.setBasic(EbtFloat, qual, @1);
1308 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1309 $$.setBasic(EbtFloat, qual, @1);
1313 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1314 $$.setBasic(EbtFloat, qual, @1);
1318 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1319 $$.setBasic(EbtBool, qual, @1);
1323 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1324 $$.setBasic(EbtBool, qual, @1);
1328 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1329 $$.setBasic(EbtBool, qual, @1);
1333 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1334 $$.setBasic(EbtInt, qual, @1);
1338 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1339 $$.setBasic(EbtInt, qual, @1);
1343 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1344 $$.setBasic(EbtInt, qual, @1);
1348 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1349 $$.setBasic(EbtUInt, qual, @1);
1353 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1354 $$.setBasic(EbtUInt, qual, @1);
1358 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1359 $$.setBasic(EbtUInt, qual, @1);
1363 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1364 $$.setBasic(EbtFloat, qual, @1);
1368 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1369 $$.setBasic(EbtFloat, qual, @1);
1373 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1374 $$.setBasic(EbtFloat, qual, @1);
1378 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1379 $$.setBasic(EbtFloat, qual, @1);
1383 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1384 $$.setBasic(EbtFloat, qual, @1);
1388 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1389 $$.setBasic(EbtFloat, qual, @1);
1393 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1394 $$.setBasic(EbtFloat, qual, @1);
1398 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1399 $$.setBasic(EbtFloat, qual, @1);
1403 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1404 $$.setBasic(EbtFloat, qual, @1);
1408 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1409 $$.setBasic(EbtSampler2D, qual, @1);
1412 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1413 $$.setBasic(EbtSampler3D, qual, @1);
1416 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1417 $$.setBasic(EbtSamplerCube, qual, @1);
1420 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1421 $$.setBasic(EbtSampler2DArray, qual, @1);
1424 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1425 $$.setBasic(EbtISampler2D, qual, @1);
1428 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1429 $$.setBasic(EbtISampler3D, qual, @1);
1432 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1433 $$.setBasic(EbtISamplerCube, qual, @1);
1436 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1437 $$.setBasic(EbtISampler2DArray, qual, @1);
1440 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1441 $$.setBasic(EbtUSampler2D, qual, @1);
1444 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1445 $$.setBasic(EbtUSampler3D, qual, @1);
1448 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1449 $$.setBasic(EbtUSamplerCube, qual, @1);
1452 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1453 $$.setBasic(EbtUSampler2DArray, qual, @1);
1456 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1457 $$.setBasic(EbtSampler2DShadow, qual, @1);
1459 | SAMPLERCUBESHADOW {
1460 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1461 $$.setBasic(EbtSamplerCubeShadow, qual, @1);
1463 | SAMPLER2DARRAYSHADOW {
1464 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1465 $$.setBasic(EbtSampler2DArrayShadow, qual, @1);
1467 | SAMPLER_EXTERNAL_OES {
1468 if (!context->supportsExtension("GL_OES_EGL_image_external")) {
1469 context->error(@1, "unsupported type", "samplerExternalOES");
1472 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1473 $$.setBasic(EbtSamplerExternalOES, qual, @1);
1476 if (!context->supportsExtension("GL_ARB_texture_rectangle")) {
1477 context->error(@1, "unsupported type", "sampler2DRect");
1480 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1481 $$.setBasic(EbtSampler2DRect, qual, @1);
1483 | struct_specifier {
1485 $$.qualifier = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1489 // This is for user defined type names. The lexical phase looked up the
1492 TType& structure = static_cast<TVariable*>($1.symbol)->getType();
1493 TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1494 $$.setBasic(EbtStruct, qual, @1);
1495 $$.userDef = &structure;
1500 : STRUCT identifier LEFT_BRACE { if (context->enterStructDeclaration(@2, *$2.string)) context->recover(); } struct_declaration_list RIGHT_BRACE {
1501 $$ = context->addStructure(@1, @2, $2.string, $5);
1503 | STRUCT LEFT_BRACE { if (context->enterStructDeclaration(@2, *$2.string)) context->recover(); } struct_declaration_list RIGHT_BRACE {
1504 $$ = context->addStructure(@1, @$, NewPoolTString(""), $4);
1508 struct_declaration_list
1509 : struct_declaration {
1512 | struct_declaration_list struct_declaration {
1514 for (size_t i = 0; i < $2->size(); ++i) {
1515 TField* field = (*$2)[i];
1516 for (size_t j = 0; j < $$->size(); ++j) {
1517 if ((*$$)[j]->name() == field->name()) {
1518 context->error(@2, "duplicate field name in structure:", "struct", field->name().c_str());
1522 $$->push_back(field);
1528 : type_specifier struct_declarator_list SEMICOLON {
1529 $$ = context->addStructDeclaratorList($1, $2);
1531 | type_qualifier type_specifier struct_declarator_list SEMICOLON {
1532 // ES3 Only, but errors should be handled elsewhere
1533 $2.qualifier = $1.qualifier;
1534 $2.layoutQualifier = $1.layoutQualifier;
1535 $$ = context->addStructDeclaratorList($2, $3);
1539 struct_declarator_list
1540 : struct_declarator {
1541 $$ = NewPoolTFieldList();
1544 | struct_declarator_list COMMA struct_declarator {
1551 if (context->reservedErrorCheck(@1, *$1.string))
1554 TType* type = new TType(EbtVoid, EbpUndefined);
1555 $$ = new TField(type, $1.string, @1);
1557 | identifier LEFT_BRACKET constant_expression RIGHT_BRACKET {
1558 if (context->reservedErrorCheck(@1, *$1.string))
1561 TType* type = new TType(EbtVoid, EbpUndefined);
1563 if (context->arraySizeErrorCheck(@3, $3, size))
1565 type->setArraySize(size);
1567 $$ = new TField(type, $1.string, @1);
1572 : assignment_expression { $$ = $1; }
1575 declaration_statement
1576 : declaration { $$ = $1; }
1580 : compound_statement { $$ = $1; }
1581 | simple_statement { $$ = $1; }
1584 // Grammar Note: No labeled statements; 'goto' is not supported.
1587 : declaration_statement { $$ = $1; }
1588 | expression_statement { $$ = $1; }
1589 | selection_statement { $$ = $1; }
1590 | iteration_statement { $$ = $1; }
1591 | jump_statement { $$ = $1; }
1595 : LEFT_BRACE RIGHT_BRACE { $$ = 0; }
1596 | LEFT_BRACE { context->symbolTable.push(); } statement_list { context->symbolTable.pop(); } RIGHT_BRACE {
1598 $3->setOp(EOpSequence);
1605 statement_no_new_scope
1606 : compound_statement_no_new_scope { $$ = $1; }
1607 | simple_statement { $$ = $1; }
1610 statement_with_scope
1611 : { context->symbolTable.push(); } compound_statement_no_new_scope { context->symbolTable.pop(); $$ = $2; }
1612 | { context->symbolTable.push(); } simple_statement { context->symbolTable.pop(); $$ = $2; }
1615 compound_statement_no_new_scope
1616 // Statement that doesn't create a new scope, for selection_statement, iteration_statement
1617 : LEFT_BRACE RIGHT_BRACE {
1620 | LEFT_BRACE statement_list RIGHT_BRACE {
1622 $2->setOp(EOpSequence);
1631 $$ = context->intermediate.makeAggregate($1, @$);
1633 | statement_list statement {
1634 $$ = context->intermediate.growAggregate($1, $2, @$);
1638 expression_statement
1639 : SEMICOLON { $$ = 0; }
1640 | expression SEMICOLON { $$ = static_cast<TIntermNode*>($1); }
1644 : IF LEFT_PAREN expression RIGHT_PAREN selection_rest_statement {
1645 if (context->boolErrorCheck(@1, $3))
1647 $$ = context->intermediate.addSelection($3, $5, @1);
1651 selection_rest_statement
1652 : statement_with_scope ELSE statement_with_scope {
1656 | statement_with_scope {
1662 // Grammar Note: No 'switch'. Switch statements not supported.
1665 // In 1996 c++ draft, conditions can include single declarations
1668 if (context->boolErrorCheck($1->getLine(), $1))
1671 | fully_specified_type identifier EQUAL initializer {
1672 TIntermNode* intermNode;
1673 if (context->structQualifierErrorCheck(@2, $1))
1675 if (context->boolErrorCheck(@2, $1))
1678 if (!context->executeInitializer(@2, *$2.string, $1, $4, intermNode))
1688 : WHILE LEFT_PAREN { context->symbolTable.push(); ++context->loopNestingLevel; } condition RIGHT_PAREN statement_no_new_scope {
1689 context->symbolTable.pop();
1690 $$ = context->intermediate.addLoop(ELoopWhile, 0, $4, 0, $6, @1);
1691 --context->loopNestingLevel;
1693 | DO { ++context->loopNestingLevel; } statement_with_scope WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON {
1694 if (context->boolErrorCheck(@8, $6))
1697 $$ = context->intermediate.addLoop(ELoopDoWhile, 0, $6, 0, $3, @4);
1698 --context->loopNestingLevel;
1700 | FOR LEFT_PAREN { context->symbolTable.push(); ++context->loopNestingLevel; } for_init_statement for_rest_statement RIGHT_PAREN statement_no_new_scope {
1701 context->symbolTable.pop();
1702 $$ = context->intermediate.addLoop(ELoopFor, $4, reinterpret_cast<TIntermTyped*>($5.node1), reinterpret_cast<TIntermTyped*>($5.node2), $7, @1);
1703 --context->loopNestingLevel;
1708 : expression_statement {
1711 | declaration_statement {
1720 | /* May be null */ {
1726 : conditionopt SEMICOLON {
1730 | conditionopt SEMICOLON expression {
1737 : CONTINUE SEMICOLON {
1738 if (context->loopNestingLevel <= 0) {
1739 context->error(@1, "continue statement only allowed in loops", "");
1742 $$ = context->intermediate.addBranch(EOpContinue, @1);
1745 if (context->loopNestingLevel <= 0) {
1746 context->error(@1, "break statement only allowed in loops", "");
1749 $$ = context->intermediate.addBranch(EOpBreak, @1);
1751 | RETURN SEMICOLON {
1752 $$ = context->intermediate.addBranch(EOpReturn, @1);
1753 if (context->currentFunctionType->getBasicType() != EbtVoid) {
1754 context->error(@1, "non-void function must return a value", "return");
1758 | RETURN expression SEMICOLON {
1759 $$ = context->intermediate.addBranch(EOpReturn, $2, @1);
1760 context->functionReturnsValue = true;
1761 if (context->currentFunctionType->getBasicType() == EbtVoid) {
1762 context->error(@1, "void function cannot return a value", "return");
1764 } else if (*(context->currentFunctionType) != $2->getType()) {
1765 context->error(@1, "function return is not matching type:", "return");
1769 | DISCARD SEMICOLON {
1770 FRAG_ONLY("discard", @1);
1771 $$ = context->intermediate.addBranch(EOpKill, @1);
1775 // Grammar Note: No 'goto'. Gotos are not supported.
1778 : external_declaration {
1780 context->treeRoot = $$;
1782 | translation_unit external_declaration {
1783 $$ = context->intermediate.growAggregate($1, $2, @$);
1784 context->treeRoot = $$;
1788 external_declaration
1789 : function_definition {
1798 : function_prototype {
1799 TFunction* function = $1.function;
1801 const TSymbol *builtIn = context->symbolTable.findBuiltIn(function->getMangledName(), context->shaderVersion);
1805 context->error(@1, "built-in functions cannot be redefined", function->getName().c_str());
1809 TFunction* prevDec = static_cast<TFunction*>(context->symbolTable.find(function->getMangledName(), context->shaderVersion));
1811 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1812 // as it would have just been put in the symbol table. Otherwise, we're looking up
1813 // an earlier occurance.
1815 if (prevDec->isDefined()) {
1817 // Then this function already has a body.
1819 context->error(@1, "function already has a body", function->getName().c_str());
1822 prevDec->setDefined();
1825 // Raise error message if main function takes any parameters or return anything other than void
1827 if (function->getName() == "main") {
1828 if (function->getParamCount() > 0) {
1829 context->error(@1, "function cannot take any parameter(s)", function->getName().c_str());
1832 if (function->getReturnType().getBasicType() != EbtVoid) {
1833 context->error(@1, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1839 // Remember the return type for later checking for RETURN statements.
1841 context->currentFunctionType = &(prevDec->getReturnType());
1842 context->functionReturnsValue = false;
1845 // Insert parameters into the symbol table.
1846 // If the parameter has no name, it's not an error, just don't insert it
1847 // (could be used for unused args).
1849 // Also, accumulate the list of parameters into the HIL, so lower level code
1850 // knows where to find parameters.
1852 TIntermAggregate* paramNodes = new TIntermAggregate;
1853 for (size_t i = 0; i < function->getParamCount(); i++) {
1854 const TParameter& param = function->getParam(i);
1855 if (param.name != 0) {
1856 TVariable *variable = new TVariable(param.name, *param.type);
1858 // Insert the parameters with name in the symbol table.
1860 if (! context->symbolTable.declare(variable)) {
1861 context->error(@1, "redefinition", variable->getName().c_str());
1867 // Add the parameter to the HIL
1869 paramNodes = context->intermediate.growAggregate(
1871 context->intermediate.addSymbol(variable->getUniqueId(),
1872 variable->getName(),
1873 variable->getType(), @1),
1876 paramNodes = context->intermediate.growAggregate(paramNodes, context->intermediate.addSymbol(0, "", *param.type, @1), @1);
1879 context->intermediate.setAggregateOperator(paramNodes, EOpParameters, @1);
1880 $1.intermAggregate = paramNodes;
1881 context->loopNestingLevel = 0;
1883 compound_statement_no_new_scope {
1884 //?? Check that all paths return a value if return type != void ?
1885 // May be best done as post process phase on intermediate code
1886 if (context->currentFunctionType->getBasicType() != EbtVoid && ! context->functionReturnsValue) {
1887 context->error(@1, "function does not return a value:", "", $1.function->getName().c_str());
1891 $$ = context->intermediate.growAggregate($1.intermAggregate, $3, @$);
1892 context->intermediate.setAggregateOperator($$, EOpFunction, @1);
1893 $$->getAsAggregate()->setName($1.function->getMangledName().c_str());
1894 $$->getAsAggregate()->setType($1.function->getReturnType());
1896 // store the pragma information for debug and optimize and other vendor specific
1897 // information. This information can be queried from the parse tree
1898 $$->getAsAggregate()->setOptimize(context->pragma().optimize);
1899 $$->getAsAggregate()->setDebug(context->pragma().debug);
1901 context->symbolTable.pop();
1907 int glslang_parse(TParseContext* context) {
1908 return yyparse(context);