Home | History | Annotate | Download | only in compiler
      1 /*
      2 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //    http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 
     16 This file contains the Yacc grammar for GLSL ES.
     17 Based on ANSI C Yacc grammar:
     18 http://www.lysator.liu.se/c/ANSI-C-grammar-y.html
     19 
     20 IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN generate_parser.sh,
     21 WHICH GENERATES THE GLSL ES PARSER (glslang_tab.cpp AND glslang_tab.h).
     22 */
     23 
     24 %{
     25 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
     26 //
     27 // Licensed under the Apache License, Version 2.0 (the "License");
     28 // you may not use this file except in compliance with the License.
     29 // You may obtain a copy of the License at
     30 //
     31 //    http://www.apache.org/licenses/LICENSE-2.0
     32 //
     33 // Unless required by applicable law or agreed to in writing, software
     34 // distributed under the License is distributed on an "AS IS" BASIS,
     35 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     36 // See the License for the specific language governing permissions and
     37 // limitations under the License.
     38 
     39 // This file is auto-generated by generate_parser.sh. DO NOT EDIT!
     40 
     41 // Ignore errors in auto-generated code.
     42 #if defined(__GNUC__)
     43 #pragma GCC diagnostic ignored "-Wunused-function"
     44 #pragma GCC diagnostic ignored "-Wunused-variable"
     45 #pragma GCC diagnostic ignored "-Wswitch-enum"
     46 #elif defined(_MSC_VER)
     47 #pragma warning(disable: 4065)
     48 #pragma warning(disable: 4189)
     49 #pragma warning(disable: 4505)
     50 #pragma warning(disable: 4701)
     51 #endif
     52 
     53 #include "SymbolTable.h"
     54 #include "ParseHelper.h"
     55 
     56 #define YYENABLE_NLS 0
     57 %}
     58 
     59 %expect 1 /* One shift reduce conflict because of if | else */
     60 %pure-parser
     61 %parse-param {TParseContext* context}
     62 %param {void* yyscanner}
     63 
     64 %code requires {
     65 #define YYLTYPE TSourceLoc
     66 #define YYLTYPE_IS_DECLARED 1
     67 }
     68 
     69 %union {
     70     struct {
     71         union {
     72             TString *string;
     73             float f;
     74             int i;
     75             unsigned int u;
     76             bool b;
     77         };
     78         TSymbol* symbol;
     79     } lex;
     80     struct {
     81         TOperator op;
     82         union {
     83             TIntermNode* intermNode;
     84             TIntermNodePair nodePair;
     85             TIntermTyped* intermTypedNode;
     86             TIntermAggregate* intermAggregate;
     87             TIntermSwitch* intermSwitch;
     88             TIntermCase* intermCase;
     89         };
     90         union {
     91             TPublicType type;
     92             TPrecision precision;
     93             TLayoutQualifier layoutQualifier;
     94             TQualifier qualifier;
     95             TFunction* function;
     96             TParameter param;
     97             TField* field;
     98             TFieldList* fieldList;
     99         };
    100     } interm;
    101 }
    102 
    103 %{
    104 extern int yylex(YYSTYPE* yylval, YYLTYPE* yylloc, void* yyscanner);
    105 extern void yyerror(YYLTYPE* lloc, TParseContext* context, void* scanner, const char* reason);
    106 
    107 #define YYLLOC_DEFAULT(Current, Rhs, N)                      \
    108   do {                                                       \
    109       if (N) {                                         \
    110         (Current).first_file = YYRHSLOC(Rhs, 1).first_file;  \
    111         (Current).first_line = YYRHSLOC(Rhs, 1).first_line;  \
    112         (Current).last_file = YYRHSLOC(Rhs, N).last_file;    \
    113         (Current).last_line = YYRHSLOC(Rhs, N).last_line;    \
    114       }                                                      \
    115       else {                                                 \
    116         (Current).first_file = YYRHSLOC(Rhs, 0).last_file;   \
    117         (Current).first_line = YYRHSLOC(Rhs, 0).last_line;   \
    118         (Current).last_file = YYRHSLOC(Rhs, 0).last_file;    \
    119         (Current).last_line = YYRHSLOC(Rhs, 0).last_line;    \
    120       }                                                      \
    121   } while (0)
    122 
    123 #define FRAG_VERT_ONLY(S, L) {  \
    124     if (context->getShaderType() != GL_FRAGMENT_SHADER &&  \
    125         context->getShaderType() != GL_VERTEX_SHADER) {  \
    126         context->error(L, " supported in vertex/fragment shaders only ", S);  \
    127         context->recover();  \
    128     }  \
    129 }
    130 
    131 #define VERTEX_ONLY(S, L) {  \
    132     if (context->getShaderType() != GL_VERTEX_SHADER) {  \
    133         context->error(L, " supported in vertex shaders only ", S);  \
    134         context->recover();  \
    135     }  \
    136 }
    137 
    138 #define FRAG_ONLY(S, L) {  \
    139     if (context->getShaderType() != GL_FRAGMENT_SHADER) {  \
    140         context->error(L, " supported in fragment shaders only ", S);  \
    141         context->recover();  \
    142     }  \
    143 }
    144 
    145 #define ES2_ONLY(S, L) {  \
    146     if (context->getShaderVersion() != 100) {  \
    147         context->error(L, " supported in GLSL ES 1.00 only ", S);  \
    148         context->recover();  \
    149     }  \
    150 }
    151 
    152 #define ES3_ONLY(TOKEN, LINE, REASON) {  \
    153     if (context->getShaderVersion() != 300) {  \
    154         context->error(LINE, REASON " supported in GLSL ES 3.00 only ", TOKEN);  \
    155         context->recover();  \
    156     }  \
    157 }
    158 %}
    159 
    160 %token <lex> INVARIANT HIGH_PRECISION MEDIUM_PRECISION LOW_PRECISION PRECISION
    161 %token <lex> ATTRIBUTE CONST_QUAL BOOL_TYPE FLOAT_TYPE INT_TYPE UINT_TYPE
    162 %token <lex> BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
    163 %token <lex> BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 VEC2 VEC3 VEC4 UVEC2 UVEC3 UVEC4
    164 %token <lex> MATRIX2 MATRIX3 MATRIX4 IN_QUAL OUT_QUAL INOUT_QUAL UNIFORM VARYING
    165 %token <lex> MATRIX2x3 MATRIX3x2 MATRIX2x4 MATRIX4x2 MATRIX3x4 MATRIX4x3
    166 %token <lex> CENTROID FLAT SMOOTH
    167 %token <lex> STRUCT VOID_TYPE WHILE
    168 %token <lex> SAMPLER2D SAMPLERCUBE SAMPLER_EXTERNAL_OES SAMPLER2DRECT SAMPLER2DARRAY
    169 %token <lex> ISAMPLER2D ISAMPLER3D ISAMPLERCUBE ISAMPLER2DARRAY
    170 %token <lex> USAMPLER2D USAMPLER3D USAMPLERCUBE USAMPLER2DARRAY
    171 %token <lex> SAMPLER3D SAMPLER3DRECT SAMPLER2DSHADOW SAMPLERCUBESHADOW SAMPLER2DARRAYSHADOW
    172 %token <lex> LAYOUT
    173 
    174 %token <lex> IDENTIFIER TYPE_NAME FLOATCONSTANT INTCONSTANT UINTCONSTANT BOOLCONSTANT
    175 %token <lex> FIELD_SELECTION
    176 %token <lex> LEFT_OP RIGHT_OP
    177 %token <lex> INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
    178 %token <lex> AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
    179 %token <lex> MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
    180 %token <lex> SUB_ASSIGN
    181 
    182 %token <lex> LEFT_PAREN RIGHT_PAREN LEFT_BRACKET RIGHT_BRACKET LEFT_BRACE RIGHT_BRACE DOT
    183 %token <lex> COMMA COLON EQUAL SEMICOLON BANG DASH TILDE PLUS STAR SLASH PERCENT
    184 %token <lex> LEFT_ANGLE RIGHT_ANGLE VERTICAL_BAR CARET AMPERSAND QUESTION
    185 
    186 %type <interm> assignment_operator unary_operator
    187 %type <interm.intermTypedNode> variable_identifier primary_expression postfix_expression
    188 %type <interm.intermTypedNode> expression integer_expression assignment_expression
    189 %type <interm.intermTypedNode> unary_expression multiplicative_expression additive_expression
    190 %type <interm.intermTypedNode> relational_expression equality_expression
    191 %type <interm.intermTypedNode> conditional_expression constant_expression
    192 %type <interm.intermTypedNode> logical_or_expression logical_xor_expression logical_and_expression
    193 %type <interm.intermTypedNode> shift_expression and_expression exclusive_or_expression inclusive_or_expression
    194 %type <interm.intermTypedNode> function_call initializer condition conditionopt
    195 
    196 %type <interm.intermNode> translation_unit function_definition
    197 %type <interm.intermNode> statement simple_statement
    198 %type <interm.intermAggregate>  statement_list compound_statement compound_statement_no_new_scope
    199 %type <interm.intermNode> declaration_statement selection_statement expression_statement
    200 %type <interm.intermNode> declaration external_declaration
    201 %type <interm.intermNode> for_init_statement
    202 %type <interm.nodePair> selection_rest_statement for_rest_statement
    203 %type <interm.intermSwitch> switch_statement
    204 %type <interm.intermCase> case_label
    205 %type <interm.intermNode> iteration_statement jump_statement statement_no_new_scope statement_with_scope
    206 %type <interm> single_declaration init_declarator_list
    207 
    208 %type <interm> parameter_declaration parameter_declarator parameter_type_specifier
    209 %type <interm.qualifier> parameter_qualifier parameter_type_qualifier
    210 %type <interm.layoutQualifier> layout_qualifier layout_qualifier_id_list layout_qualifier_id
    211 
    212 %type <interm.precision> precision_qualifier
    213 %type <interm.type> type_qualifier fully_specified_type type_specifier storage_qualifier interpolation_qualifier
    214 %type <interm.type> type_specifier_no_prec type_specifier_nonarray
    215 %type <interm.type> struct_specifier
    216 %type <interm.field> struct_declarator
    217 %type <interm.fieldList> struct_declarator_list struct_declaration struct_declaration_list
    218 %type <interm.function> function_header function_declarator function_identifier
    219 %type <interm.function> function_header_with_parameters function_call_header
    220 %type <interm> function_call_header_with_parameters function_call_header_no_parameters function_call_generic function_prototype
    221 %type <interm> function_call_or_method
    222 
    223 %type <lex> enter_struct
    224 
    225 %start translation_unit
    226 %%
    227 
    228 variable_identifier
    229     : IDENTIFIER {
    230         // The symbol table search was done in the lexical phase
    231         const TVariable *variable = context->getNamedVariable(@1, $1.string, $1.symbol);
    232 
    233         // don't delete $1.string, it's used by error recovery, and the pool
    234         // pop will reclaim the memory
    235 
    236         // Constants which aren't indexable arrays can be propagated by value.
    237         ConstantUnion *constArray = variable->getConstPointer();
    238         if (constArray && variable->getType().getArraySize() <= 1) {
    239             TType t(variable->getType());
    240             $$ = context->intermediate.addConstantUnion(constArray, t, @1);
    241         } else
    242             $$ = context->intermediate.addSymbol(variable->getUniqueId(),
    243                                                      variable->getName(),
    244                                                      variable->getType(), @1);
    245     }
    246     ;
    247 
    248 primary_expression
    249     : variable_identifier {
    250         $$ = $1;
    251     }
    252     | INTCONSTANT {
    253         ConstantUnion *unionArray = new ConstantUnion[1];
    254         unionArray->setIConst($1.i);
    255         $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConstExpr), @1);
    256     }
    257     | UINTCONSTANT {
    258         ConstantUnion *unionArray = new ConstantUnion[1];
    259         unionArray->setUConst($1.u);
    260         $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtUInt, EbpUndefined, EvqConstExpr), @1);
    261     }
    262     | FLOATCONSTANT {
    263         ConstantUnion *unionArray = new ConstantUnion[1];
    264         unionArray->setFConst($1.f);
    265         $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConstExpr), @1);
    266     }
    267     | BOOLCONSTANT {
    268         ConstantUnion *unionArray = new ConstantUnion[1];
    269         unionArray->setBConst($1.b);
    270         $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConstExpr), @1);
    271     }
    272     | LEFT_PAREN expression RIGHT_PAREN {
    273         $$ = $2;
    274     }
    275     ;
    276 
    277 postfix_expression
    278     : primary_expression {
    279         $$ = $1;
    280     }
    281     | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET {
    282         $$ = context->addIndexExpression($1, @2, $3);
    283     }
    284     | function_call {
    285         $$ = $1;
    286     }
    287     | postfix_expression DOT FIELD_SELECTION {
    288         $$ = context->addFieldSelectionExpression($1, @2, *$3.string, @3);
    289     }
    290     | postfix_expression INC_OP {
    291         $$ = context->addUnaryMathLValue(EOpPostIncrement, $1, @2);
    292     }
    293     | postfix_expression DEC_OP {
    294         $$ = context->addUnaryMathLValue(EOpPostDecrement, $1, @2);
    295     }
    296     ;
    297 
    298 integer_expression
    299     : expression {
    300         if (context->integerErrorCheck($1, "[]"))
    301             context->recover();
    302         $$ = $1;
    303     }
    304     ;
    305 
    306 function_call
    307     : function_call_or_method {
    308         bool fatalError = false;
    309         $$ = context->addFunctionCallOrMethod($1.function, $1.nodePair.node1, $1.nodePair.node2, @1, &fatalError);
    310         if (fatalError)
    311         {
    312             YYERROR;
    313         }
    314     }
    315     ;
    316 
    317 function_call_or_method
    318     : function_call_generic {
    319         $$ = $1;
    320         $$.nodePair.node2 = nullptr;
    321     }
    322     | postfix_expression DOT function_call_generic {
    323         ES3_ONLY("", @3, "methods");
    324         $$ = $3;
    325         $$.nodePair.node2 = $1;
    326     }
    327     ;
    328 
    329 function_call_generic
    330     : function_call_header_with_parameters RIGHT_PAREN {
    331         $$ = $1;
    332     }
    333     | function_call_header_no_parameters RIGHT_PAREN {
    334         $$ = $1;
    335     }
    336     ;
    337 
    338 function_call_header_no_parameters
    339     : function_call_header VOID_TYPE {
    340         $$.function = $1;
    341         $$.nodePair.node1 = nullptr;
    342     }
    343     | function_call_header {
    344         $$.function = $1;
    345         $$.nodePair.node1 = nullptr;
    346     }
    347     ;
    348 
    349 function_call_header_with_parameters
    350     : function_call_header assignment_expression {
    351         TParameter param = { 0, new TType($2->getType()) };
    352         $1->addParameter(param);
    353         $$.function = $1;
    354         $$.nodePair.node1 = $2;
    355     }
    356     | function_call_header_with_parameters COMMA assignment_expression {
    357         TParameter param = { 0, new TType($3->getType()) };
    358         $1.function->addParameter(param);
    359         $$.function = $1.function;
    360         $$.nodePair.node1 = context->intermediate.growAggregate($1.intermNode, $3, @2);
    361     }
    362     ;
    363 
    364 function_call_header
    365     : function_identifier LEFT_PAREN {
    366         $$ = $1;
    367     }
    368     ;
    369 
    370 // Grammar Note:  Constructors look like functions, but are recognized as types.
    371 
    372 function_identifier
    373     : type_specifier_no_prec {
    374         if ($1.array) {
    375             ES3_ONLY("[]", @1, "array constructor");
    376         }
    377         $$ = context->addConstructorFunc($1);
    378     }
    379     | IDENTIFIER {
    380         if (context->reservedErrorCheck(@1, *$1.string))
    381             context->recover();
    382         TType type(EbtVoid, EbpUndefined);
    383         TFunction *function = new TFunction($1.string, type);
    384         $$ = function;
    385     }
    386     | FIELD_SELECTION {
    387         if (context->reservedErrorCheck(@1, *$1.string))
    388             context->recover();
    389         TType type(EbtVoid, EbpUndefined);
    390         TFunction *function = new TFunction($1.string, type);
    391         $$ = function;
    392     }
    393     ;
    394 
    395 unary_expression
    396     : postfix_expression {
    397         $$ = $1;
    398     }
    399     | INC_OP unary_expression {
    400         $$ = context->addUnaryMathLValue(EOpPreIncrement, $2, @1);
    401     }
    402     | DEC_OP unary_expression {
    403         $$ = context->addUnaryMathLValue(EOpPreDecrement, $2, @1);
    404     }
    405     | unary_operator unary_expression {
    406         if ($1.op != EOpNull) {
    407             $$ = context->addUnaryMath($1.op, $2, @1);
    408         } else
    409             $$ = $2;
    410     }
    411     ;
    412 // Grammar Note:  No traditional style type casts.
    413 
    414 unary_operator
    415     : PLUS  { $$.op = EOpNull; }
    416     | DASH  { $$.op = EOpNegative; }
    417     | BANG  { $$.op = EOpLogicalNot; }
    418     | TILDE {
    419         ES3_ONLY("~", @1, "bit-wise operator");
    420         $$.op = EOpBitwiseNot;
    421     }
    422     ;
    423 // Grammar Note:  No '*' or '&' unary ops.  Pointers are not supported.
    424 
    425 multiplicative_expression
    426     : unary_expression { $$ = $1; }
    427     | multiplicative_expression STAR unary_expression {
    428         FRAG_VERT_ONLY("*", @2);
    429         $$ = context->addBinaryMath(EOpMul, $1, $3, @2);
    430     }
    431     | multiplicative_expression SLASH unary_expression {
    432         FRAG_VERT_ONLY("/", @2);
    433         $$ = context->addBinaryMath(EOpDiv, $1, $3, @2);
    434     }
    435     | multiplicative_expression PERCENT unary_expression {
    436         FRAG_VERT_ONLY("%", @2);
    437         ES3_ONLY("%", @2, "integer modulus operator");
    438         $$ = context->addBinaryMath(EOpIMod, $1, $3, @2);
    439     }
    440     ;
    441 
    442 additive_expression
    443     : multiplicative_expression { $$ = $1; }
    444     | additive_expression PLUS multiplicative_expression {
    445         $$ = context->addBinaryMath(EOpAdd, $1, $3, @2);
    446     }
    447     | additive_expression DASH multiplicative_expression {
    448         $$ = context->addBinaryMath(EOpSub, $1, $3, @2);
    449     }
    450     ;
    451 
    452 shift_expression
    453     : additive_expression { $$ = $1; }
    454     | shift_expression LEFT_OP additive_expression {
    455         ES3_ONLY("<<", @2, "bit-wise operator");
    456         $$ = context->addBinaryMath(EOpBitShiftLeft, $1, $3, @2);
    457     }
    458     | shift_expression RIGHT_OP additive_expression {
    459         ES3_ONLY(">>", @2, "bit-wise operator");
    460         $$ = context->addBinaryMath(EOpBitShiftRight, $1, $3, @2);
    461     }
    462     ;
    463 
    464 relational_expression
    465     : shift_expression { $$ = $1; }
    466     | relational_expression LEFT_ANGLE shift_expression {
    467         $$ = context->addBinaryMathBooleanResult(EOpLessThan, $1, $3, @2);
    468     }
    469     | relational_expression RIGHT_ANGLE shift_expression  {
    470         $$ = context->addBinaryMathBooleanResult(EOpGreaterThan, $1, $3, @2);
    471     }
    472     | relational_expression LE_OP shift_expression  {
    473         $$ = context->addBinaryMathBooleanResult(EOpLessThanEqual, $1, $3, @2);
    474     }
    475     | relational_expression GE_OP shift_expression  {
    476         $$ = context->addBinaryMathBooleanResult(EOpGreaterThanEqual, $1, $3, @2);
    477     }
    478     ;
    479 
    480 equality_expression
    481     : relational_expression { $$ = $1; }
    482     | equality_expression EQ_OP relational_expression  {
    483         $$ = context->addBinaryMathBooleanResult(EOpEqual, $1, $3, @2);
    484     }
    485     | equality_expression NE_OP relational_expression {
    486         $$ = context->addBinaryMathBooleanResult(EOpNotEqual, $1, $3, @2);
    487     }
    488     ;
    489 
    490 and_expression
    491     : equality_expression { $$ = $1; }
    492     | and_expression AMPERSAND equality_expression {
    493         ES3_ONLY("&", @2, "bit-wise operator");
    494         $$ = context->addBinaryMath(EOpBitwiseAnd, $1, $3, @2);
    495     }
    496     ;
    497 
    498 exclusive_or_expression
    499     : and_expression { $$ = $1; }
    500     | exclusive_or_expression CARET and_expression {
    501         ES3_ONLY("^", @2, "bit-wise operator");
    502         $$ = context->addBinaryMath(EOpBitwiseXor, $1, $3, @2);
    503     }
    504     ;
    505 
    506 inclusive_or_expression
    507     : exclusive_or_expression { $$ = $1; }
    508     | inclusive_or_expression VERTICAL_BAR exclusive_or_expression {
    509         ES3_ONLY("|", @2, "bit-wise operator");
    510         $$ = context->addBinaryMath(EOpBitwiseOr, $1, $3, @2);
    511     }
    512     ;
    513 
    514 logical_and_expression
    515     : inclusive_or_expression { $$ = $1; }
    516     | logical_and_expression AND_OP inclusive_or_expression {
    517         $$ = context->addBinaryMathBooleanResult(EOpLogicalAnd, $1, $3, @2);
    518     }
    519     ;
    520 
    521 logical_xor_expression
    522     : logical_and_expression { $$ = $1; }
    523     | logical_xor_expression XOR_OP logical_and_expression  {
    524         $$ = context->addBinaryMathBooleanResult(EOpLogicalXor, $1, $3, @2);
    525     }
    526     ;
    527 
    528 logical_or_expression
    529     : logical_xor_expression { $$ = $1; }
    530     | logical_or_expression OR_OP logical_xor_expression  {
    531         $$ = context->addBinaryMathBooleanResult(EOpLogicalOr, $1, $3, @2);
    532     }
    533     ;
    534 
    535 conditional_expression
    536     : logical_or_expression { $$ = $1; }
    537     | logical_or_expression QUESTION expression COLON assignment_expression {
    538         $$ = context->addTernarySelection($1, $3, $5, @2);
    539     }
    540     ;
    541 
    542 assignment_expression
    543     : conditional_expression { $$ = $1; }
    544     | unary_expression assignment_operator assignment_expression {
    545         if (context->lValueErrorCheck(@2, "assign", $1))
    546             context->recover();
    547         $$ = context->addAssign($2.op, $1, $3, @2);
    548     }
    549     ;
    550 
    551 assignment_operator
    552     : EQUAL        {                           $$.op = EOpAssign; }
    553     | MUL_ASSIGN   { FRAG_VERT_ONLY("*=", @1); $$.op = EOpMulAssign; }
    554     | DIV_ASSIGN   { FRAG_VERT_ONLY("/=", @1); $$.op = EOpDivAssign; }
    555     | MOD_ASSIGN   { ES3_ONLY("%=", @1, "integer modulus operator");
    556                      FRAG_VERT_ONLY("%=", @1); $$.op = EOpIModAssign; }
    557     | ADD_ASSIGN   {                           $$.op = EOpAddAssign; }
    558     | SUB_ASSIGN   {                           $$.op = EOpSubAssign; }
    559     | LEFT_ASSIGN  { ES3_ONLY("<<=", @1, "bit-wise operator");
    560                      FRAG_VERT_ONLY("<<=", @1);
    561                      $$.op = EOpBitShiftLeftAssign; }
    562     | RIGHT_ASSIGN { ES3_ONLY(">>=", @1, "bit-wise operator");
    563                      FRAG_VERT_ONLY(">>=", @1);
    564                      $$.op = EOpBitShiftRightAssign; }
    565     | AND_ASSIGN   { ES3_ONLY("&=", @1, "bit-wise operator");
    566                      FRAG_VERT_ONLY("&=", @1);
    567                      $$.op = EOpBitwiseAndAssign; }
    568     | XOR_ASSIGN   { ES3_ONLY("^=", @1, "bit-wise operator");
    569                      FRAG_VERT_ONLY("^=", @1);
    570                      $$.op = EOpBitwiseXorAssign; }
    571     | OR_ASSIGN    { ES3_ONLY("|=", @1, "bit-wise operator");
    572                      FRAG_VERT_ONLY("|=", @1);
    573                      $$.op = EOpBitwiseOrAssign; }
    574     ;
    575 
    576 expression
    577     : assignment_expression {
    578         $$ = $1;
    579     }
    580     | expression COMMA assignment_expression {
    581         $$ = context->intermediate.addComma($1, $3, @2);
    582         if ($$ == 0) {
    583             context->binaryOpError(@2, ",", $1->getCompleteString(), $3->getCompleteString());
    584             context->recover();
    585             $$ = $3;
    586         }
    587     }
    588     ;
    589 
    590 constant_expression
    591     : conditional_expression {
    592         if (context->constErrorCheck($1))
    593             context->recover();
    594         $$ = $1;
    595     }
    596     ;
    597 
    598 enter_struct
    599     : IDENTIFIER LEFT_BRACE {
    600         if (context->enterStructDeclaration(@1, *$1.string))
    601             context->recover();
    602         $$ = $1;
    603     }
    604     ;
    605 
    606 declaration
    607     : function_prototype SEMICOLON {
    608         $$ = context->addFunctionPrototypeDeclaration(*($1.function), @1);
    609     }
    610     | init_declarator_list SEMICOLON {
    611         TIntermAggregate *aggNode = $1.intermAggregate;
    612         if (aggNode && aggNode->getOp() == EOpNull)
    613             aggNode->setOp(EOpDeclaration);
    614         $$ = aggNode;
    615     }
    616     | PRECISION precision_qualifier type_specifier_no_prec SEMICOLON {
    617         if (!context->symbolTable.setDefaultPrecision( $3, $2 )) {
    618             context->error(@1, "illegal type argument for default precision qualifier", getBasicString($3.type));
    619             context->recover();
    620         }
    621         $$ = 0;
    622     }
    623     | type_qualifier enter_struct struct_declaration_list RIGHT_BRACE SEMICOLON {
    624         ES3_ONLY(getQualifierString($1.qualifier), @1, "interface blocks");
    625         $$ = context->addInterfaceBlock($1, @2, *$2.string, $3, NULL, @1, NULL, @1);
    626     }
    627     | type_qualifier enter_struct struct_declaration_list RIGHT_BRACE IDENTIFIER SEMICOLON {
    628         ES3_ONLY(getQualifierString($1.qualifier), @1, "interface blocks");
    629         $$ = context->addInterfaceBlock($1, @2, *$2.string, $3, $5.string, @5, NULL, @1);
    630     }
    631     | type_qualifier enter_struct struct_declaration_list RIGHT_BRACE IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET SEMICOLON {
    632         ES3_ONLY(getQualifierString($1.qualifier), @1, "interface blocks");
    633         $$ = context->addInterfaceBlock($1, @2, *$2.string, $3, $5.string, @5, $7, @6);
    634     }
    635     | type_qualifier SEMICOLON {
    636         context->parseGlobalLayoutQualifier($1);
    637         $$ = 0;
    638     }
    639     ;
    640 
    641 function_prototype
    642     : function_declarator RIGHT_PAREN  {
    643         $$.function = context->parseFunctionDeclarator(@2, $1);
    644     }
    645     ;
    646 
    647 function_declarator
    648     : function_header {
    649         $$ = $1;
    650     }
    651     | function_header_with_parameters {
    652         $$ = $1;
    653     }
    654     ;
    655 
    656 
    657 function_header_with_parameters
    658     : function_header parameter_declaration {
    659         // Add the parameter
    660         $$ = $1;
    661         if ($2.param.type->getBasicType() != EbtVoid)
    662             $1->addParameter($2.param);
    663         else
    664             delete $2.param.type;
    665     }
    666     | function_header_with_parameters COMMA parameter_declaration {
    667         //
    668         // Only first parameter of one-parameter functions can be void
    669         // The check for named parameters not being void is done in parameter_declarator
    670         //
    671         if ($3.param.type->getBasicType() == EbtVoid) {
    672             //
    673             // This parameter > first is void
    674             //
    675             context->error(@2, "cannot be an argument type except for '(void)'", "void");
    676             context->recover();
    677             delete $3.param.type;
    678         } else {
    679             // Add the parameter
    680             $$ = $1;
    681             $1->addParameter($3.param);
    682         }
    683     }
    684     ;
    685 
    686 function_header
    687     : fully_specified_type IDENTIFIER LEFT_PAREN {
    688         if ($1.qualifier != EvqGlobal && $1.qualifier != EvqTemporary) {
    689             context->error(@2, "no qualifiers allowed for function return", getQualifierString($1.qualifier));
    690             context->recover();
    691         }
    692         if (!$1.layoutQualifier.isEmpty())
    693         {
    694             context->error(@2, "no qualifiers allowed for function return", "layout");
    695             context->recover();
    696         }
    697         // make sure a sampler is not involved as well...
    698         if (context->samplerErrorCheck(@2, $1, "samplers can't be function return values"))
    699             context->recover();
    700 
    701         // Add the function as a prototype after parsing it (we do not support recursion)
    702         TFunction *function;
    703         TType type($1);
    704         function = new TFunction($2.string, type);
    705         $$ = function;
    706 
    707         context->symbolTable.push();
    708     }
    709     ;
    710 
    711 parameter_declarator
    712     // Type + name
    713     : type_specifier IDENTIFIER {
    714         if ($1.type == EbtVoid) {
    715             context->error(@2, "illegal use of type 'void'", $2.string->c_str());
    716             context->recover();
    717         }
    718         if (context->reservedErrorCheck(@2, *$2.string))
    719             context->recover();
    720         TParameter param = {$2.string, new TType($1)};
    721         $$.param = param;
    722     }
    723     | type_specifier IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
    724         // Check that we can make an array out of this type
    725         if (context->arrayTypeErrorCheck(@3, $1))
    726             context->recover();
    727 
    728         if (context->reservedErrorCheck(@2, *$2.string))
    729             context->recover();
    730 
    731         int size = 0;
    732         if (context->arraySizeErrorCheck(@3, $4, size))
    733             context->recover();
    734         $1.setArray(true, size);
    735 
    736         TType* type = new TType($1);
    737         TParameter param = { $2.string, type };
    738         $$.param = param;
    739     }
    740     ;
    741 
    742 parameter_declaration
    743     //
    744     // The only parameter qualifier a parameter can have are
    745     // IN_QUAL, OUT_QUAL, INOUT_QUAL, or CONST.
    746     //
    747 
    748     //
    749     // Type + name
    750     //
    751     : parameter_type_qualifier parameter_qualifier parameter_declarator {
    752         $$ = $3;
    753         if (context->paramErrorCheck(@3, $1, $2, $$.param.type))
    754             context->recover();
    755     }
    756     | parameter_qualifier parameter_declarator {
    757         $$ = $2;
    758         if (context->parameterSamplerErrorCheck(@2, $1, *$2.param.type))
    759             context->recover();
    760         if (context->paramErrorCheck(@2, EvqTemporary, $1, $$.param.type))
    761             context->recover();
    762     }
    763     //
    764     // Only type
    765     //
    766     | parameter_type_qualifier parameter_qualifier parameter_type_specifier {
    767         $$ = $3;
    768         if (context->paramErrorCheck(@3, $1, $2, $$.param.type))
    769             context->recover();
    770     }
    771     | parameter_qualifier parameter_type_specifier {
    772         $$ = $2;
    773         if (context->parameterSamplerErrorCheck(@2, $1, *$2.param.type))
    774             context->recover();
    775         if (context->paramErrorCheck(@2, EvqTemporary, $1, $$.param.type))
    776             context->recover();
    777     }
    778     ;
    779 
    780 parameter_qualifier
    781     : /* empty */ {
    782         $$ = EvqIn;
    783     }
    784     | IN_QUAL {
    785         $$ = EvqIn;
    786     }
    787     | OUT_QUAL {
    788         $$ = EvqOut;
    789     }
    790     | INOUT_QUAL {
    791         $$ = EvqInOut;
    792     }
    793     ;
    794 
    795 parameter_type_specifier
    796     : type_specifier {
    797         TParameter param = { 0, new TType($1) };
    798         $$.param = param;
    799     }
    800     ;
    801 
    802 init_declarator_list
    803     : single_declaration {
    804         $$ = $1;
    805     }
    806     | init_declarator_list COMMA IDENTIFIER {
    807         $$ = $1;
    808         $$.intermAggregate = context->parseDeclarator($$.type, $1.intermAggregate, @3, *$3.string);
    809     }
    810     | init_declarator_list COMMA IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
    811         $$ = $1;
    812         $$.intermAggregate = context->parseArrayDeclarator($$.type, $1.intermAggregate, @3, *$3.string, @4, $5);
    813     }
    814     | init_declarator_list COMMA IDENTIFIER LEFT_BRACKET RIGHT_BRACKET EQUAL initializer {
    815         ES3_ONLY("[]", @3, "implicitly sized array");
    816         $$ = $1;
    817         $$.intermAggregate = context->parseArrayInitDeclarator($$.type, $1.intermAggregate, @3, *$3.string, @4, nullptr, @6, $7);
    818     }
    819     | init_declarator_list COMMA IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET EQUAL initializer {
    820         ES3_ONLY("=", @7, "first-class arrays (array initializer)");
    821         $$ = $1;
    822         $$.intermAggregate = context->parseArrayInitDeclarator($$.type, $1.intermAggregate, @3, *$3.string, @4, $5, @7, $8);
    823     }
    824     | init_declarator_list COMMA IDENTIFIER EQUAL initializer {
    825         $$ = $1;
    826         $$.intermAggregate = context->parseInitDeclarator($$.type, $1.intermAggregate, @3, *$3.string, @4, $5);
    827     }
    828     ;
    829 
    830 single_declaration
    831     : fully_specified_type {
    832         $$.type = $1;
    833         $$.intermAggregate = context->parseSingleDeclaration($$.type, @1, "");
    834     }
    835     | fully_specified_type IDENTIFIER {
    836         $$.type = $1;
    837         $$.intermAggregate = context->parseSingleDeclaration($$.type, @2, *$2.string);
    838     }
    839     | fully_specified_type IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
    840         $$.type = $1;
    841         $$.intermAggregate = context->parseSingleArrayDeclaration($$.type, @2, *$2.string, @3, $4);
    842     }
    843     | fully_specified_type IDENTIFIER LEFT_BRACKET RIGHT_BRACKET EQUAL initializer {
    844         ES3_ONLY("[]", @3, "implicitly sized array");
    845         $$.type = $1;
    846         $$.intermAggregate = context->parseSingleArrayInitDeclaration($$.type, @2, *$2.string, @3, nullptr, @5, $6);
    847     }
    848     | fully_specified_type IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET EQUAL initializer {
    849         ES3_ONLY("=", @6, "first-class arrays (array initializer)");
    850         $$.type = $1;
    851         $$.intermAggregate = context->parseSingleArrayInitDeclaration($$.type, @2, *$2.string, @3, $4, @6, $7);
    852     }
    853     | fully_specified_type IDENTIFIER EQUAL initializer {
    854         $$.type = $1;
    855         $$.intermAggregate = context->parseSingleInitDeclaration($$.type, @2, *$2.string, @3, $4);
    856     }
    857     | INVARIANT IDENTIFIER {
    858         // $$.type is not used in invariant declarations.
    859         $$.intermAggregate = context->parseInvariantDeclaration(@1, @2, $2.string, $2.symbol);
    860     }
    861     ;
    862 
    863 fully_specified_type
    864     : type_specifier {
    865         $$ = $1;
    866 
    867         if ($1.array) {
    868             ES3_ONLY("[]", @1, "first-class-array");
    869             if (context->getShaderVersion() != 300) {
    870                 $1.clearArrayness();
    871             }
    872         }
    873     }
    874     | type_qualifier type_specifier  {
    875         $$ = context->addFullySpecifiedType($1.qualifier, $1.invariant, $1.layoutQualifier, $2);
    876     }
    877     ;
    878 
    879 interpolation_qualifier
    880     : SMOOTH {
    881         $$.qualifier = EvqSmooth;
    882     }
    883     | FLAT {
    884         $$.qualifier = EvqFlat;
    885     }
    886     ;
    887 
    888 parameter_type_qualifier
    889     : CONST_QUAL {
    890         $$ = EvqConstReadOnly;
    891     }
    892     ;
    893 
    894 type_qualifier
    895     : ATTRIBUTE {
    896         VERTEX_ONLY("attribute", @1);
    897         ES2_ONLY("attribute", @1);
    898         if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "attribute"))
    899             context->recover();
    900         $$.setBasic(EbtVoid, EvqAttribute, @1);
    901     }
    902     | VARYING {
    903         ES2_ONLY("varying", @1);
    904         if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "varying"))
    905             context->recover();
    906         if (context->getShaderType() == GL_VERTEX_SHADER)
    907             $$.setBasic(EbtVoid, EvqVaryingOut, @1);
    908         else
    909             $$.setBasic(EbtVoid, EvqVaryingIn, @1);
    910     }
    911     | INVARIANT VARYING {
    912         ES2_ONLY("varying", @1);
    913         if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "invariant varying"))
    914             context->recover();
    915         if (context->getShaderType() == GL_VERTEX_SHADER)
    916             $$.setBasic(EbtVoid, EvqInvariantVaryingOut, @1);
    917         else
    918             $$.setBasic(EbtVoid, EvqInvariantVaryingIn, @1);
    919     }
    920     | storage_qualifier {
    921         if ($1.qualifier != EvqConstExpr && !context->symbolTable.atGlobalLevel())
    922         {
    923             context->error(@1, "Local variables can only use the const storage qualifier.", getQualifierString($1.qualifier));
    924             context->recover();
    925         }
    926         $$.setBasic(EbtVoid, $1.qualifier, @1);
    927     }
    928 	| interpolation_qualifier storage_qualifier {
    929         $$ = context->joinInterpolationQualifiers(@1, $1.qualifier, @2, $2.qualifier);
    930     }
    931     | interpolation_qualifier {
    932         context->error(@1, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier", getQualifierString($1.qualifier));
    933         context->recover();
    934 
    935         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
    936         $$.setBasic(EbtVoid, qual, @1);
    937     }
    938 	| layout_qualifier {
    939         $$.qualifier = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
    940         $$.layoutQualifier = $1;
    941     }
    942     | layout_qualifier storage_qualifier {
    943         $$.setBasic(EbtVoid, $2.qualifier, @2);
    944         $$.layoutQualifier = $1;
    945     }
    946     | INVARIANT storage_qualifier {
    947         context->es3InvariantErrorCheck($2.qualifier, @1);
    948         $$.setBasic(EbtVoid, $2.qualifier, @2);
    949         $$.invariant = true;
    950     }
    951     | INVARIANT interpolation_qualifier storage_qualifier {
    952         context->es3InvariantErrorCheck($3.qualifier, @1);
    953         $$ = context->joinInterpolationQualifiers(@2, $2.qualifier, @3, $3.qualifier);
    954         $$.invariant = true;
    955     }
    956     ;
    957 
    958 storage_qualifier
    959     : CONST_QUAL {
    960         $$.qualifier = EvqConstExpr;
    961     }
    962     | IN_QUAL {
    963         ES3_ONLY("in", @1, "storage qualifier");
    964         $$.qualifier = (context->getShaderType() == GL_FRAGMENT_SHADER) ? EvqFragmentIn : EvqVertexIn;
    965     }
    966     | OUT_QUAL {
    967         ES3_ONLY("out", @1, "storage qualifier");
    968         $$.qualifier = (context->getShaderType() == GL_FRAGMENT_SHADER) ? EvqFragmentOut : EvqVertexOut;
    969     }
    970     | CENTROID IN_QUAL {
    971         ES3_ONLY("centroid in", @1, "storage qualifier");
    972         if (context->getShaderType() == GL_VERTEX_SHADER)
    973         {
    974             context->error(@1, "invalid storage qualifier", "it is an error to use 'centroid in' in the vertex shader");
    975             context->recover();
    976         }
    977         $$.qualifier = (context->getShaderType() == GL_FRAGMENT_SHADER) ? EvqCentroidIn : EvqVertexIn;
    978     }
    979     | CENTROID OUT_QUAL {
    980         ES3_ONLY("centroid out", @1, "storage qualifier");
    981         if (context->getShaderType() == GL_FRAGMENT_SHADER)
    982         {
    983             context->error(@1, "invalid storage qualifier", "it is an error to use 'centroid out' in the fragment shader");
    984             context->recover();
    985         }
    986         $$.qualifier = (context->getShaderType() == GL_FRAGMENT_SHADER) ? EvqFragmentOut : EvqCentroidOut;
    987     }
    988     | UNIFORM {
    989         if (context->globalErrorCheck(@1, context->symbolTable.atGlobalLevel(), "uniform"))
    990             context->recover();
    991         $$.qualifier = EvqUniform;
    992     }
    993     ;
    994 
    995 type_specifier
    996     : type_specifier_no_prec {
    997         $$ = $1;
    998 
    999         if ($$.precision == EbpUndefined) {
   1000             $$.precision = context->symbolTable.getDefaultPrecision($1.type);
   1001             if (context->precisionErrorCheck(@1, $$.precision, $1.type)) {
   1002                 context->recover();
   1003             }
   1004         }
   1005     }
   1006     | precision_qualifier type_specifier_no_prec {
   1007         $$ = $2;
   1008         $$.precision = $1;
   1009 
   1010         if (!SupportsPrecision($2.type)) {
   1011             context->error(@1, "illegal type for precision qualifier", getBasicString($2.type));
   1012             context->recover();
   1013         }
   1014     }
   1015     ;
   1016 
   1017 precision_qualifier
   1018     : HIGH_PRECISION {
   1019         $$ = EbpHigh;
   1020     }
   1021     | MEDIUM_PRECISION {
   1022         $$ = EbpMedium;
   1023     }
   1024     | LOW_PRECISION  {
   1025         $$ = EbpLow;
   1026     }
   1027     ;
   1028 
   1029 layout_qualifier
   1030     : LAYOUT LEFT_PAREN layout_qualifier_id_list RIGHT_PAREN {
   1031         ES3_ONLY("layout", @1, "qualifier");
   1032         $$ = $3;
   1033     }
   1034     ;
   1035 
   1036 layout_qualifier_id_list
   1037     : layout_qualifier_id {
   1038         $$ = $1;
   1039     }
   1040     | layout_qualifier_id_list COMMA layout_qualifier_id {
   1041         $$ = context->joinLayoutQualifiers($1, $3);
   1042     }
   1043     ;
   1044 
   1045 layout_qualifier_id
   1046     : IDENTIFIER {
   1047         $$ = context->parseLayoutQualifier(*$1.string, @1);
   1048     }
   1049     | IDENTIFIER EQUAL INTCONSTANT {
   1050         $$ = context->parseLayoutQualifier(*$1.string, @1, *$3.string, $3.i, @3);
   1051     }
   1052     | IDENTIFIER EQUAL UINTCONSTANT {
   1053         $$ = context->parseLayoutQualifier(*$1.string, @1, *$3.string, $3.i, @3);
   1054     }
   1055     ;
   1056 
   1057 type_specifier_no_prec
   1058     : type_specifier_nonarray {
   1059         $$ = $1;
   1060     }
   1061     | type_specifier_nonarray LEFT_BRACKET RIGHT_BRACKET {
   1062         ES3_ONLY("[]", @2, "implicitly sized array");
   1063         $$ = $1;
   1064         $$.setArray(true, 0);
   1065     }
   1066     | type_specifier_nonarray LEFT_BRACKET constant_expression RIGHT_BRACKET {
   1067         $$ = $1;
   1068 
   1069         if (context->arrayTypeErrorCheck(@2, $1))
   1070             context->recover();
   1071         else {
   1072             int size = 0;
   1073             if (context->arraySizeErrorCheck(@2, $3, size))
   1074                 context->recover();
   1075             $$.setArray(true, size);
   1076         }
   1077     }
   1078     ;
   1079 
   1080 type_specifier_nonarray
   1081     : VOID_TYPE {
   1082         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1083         $$.setBasic(EbtVoid, qual, @1);
   1084     }
   1085     | FLOAT_TYPE {
   1086         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1087         $$.setBasic(EbtFloat, qual, @1);
   1088     }
   1089     | INT_TYPE {
   1090         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1091         $$.setBasic(EbtInt, qual, @1);
   1092     }
   1093     | UINT_TYPE {
   1094         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1095         $$.setBasic(EbtUInt, qual, @1);
   1096     }
   1097     | BOOL_TYPE {
   1098         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1099         $$.setBasic(EbtBool, qual, @1);
   1100     }
   1101     | VEC2 {
   1102         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1103         $$.setBasic(EbtFloat, qual, @1);
   1104         $$.setAggregate(2);
   1105     }
   1106     | VEC3 {
   1107         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1108         $$.setBasic(EbtFloat, qual, @1);
   1109         $$.setAggregate(3);
   1110     }
   1111     | VEC4 {
   1112         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1113         $$.setBasic(EbtFloat, qual, @1);
   1114         $$.setAggregate(4);
   1115     }
   1116     | BVEC2 {
   1117         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1118         $$.setBasic(EbtBool, qual, @1);
   1119         $$.setAggregate(2);
   1120     }
   1121     | BVEC3 {
   1122         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1123         $$.setBasic(EbtBool, qual, @1);
   1124         $$.setAggregate(3);
   1125     }
   1126     | BVEC4 {
   1127         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1128         $$.setBasic(EbtBool, qual, @1);
   1129         $$.setAggregate(4);
   1130     }
   1131     | IVEC2 {
   1132         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1133         $$.setBasic(EbtInt, qual, @1);
   1134         $$.setAggregate(2);
   1135     }
   1136     | IVEC3 {
   1137         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1138         $$.setBasic(EbtInt, qual, @1);
   1139         $$.setAggregate(3);
   1140     }
   1141     | IVEC4 {
   1142         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1143         $$.setBasic(EbtInt, qual, @1);
   1144         $$.setAggregate(4);
   1145     }
   1146     | UVEC2 {
   1147         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1148         $$.setBasic(EbtUInt, qual, @1);
   1149         $$.setAggregate(2);
   1150     }
   1151     | UVEC3 {
   1152         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1153         $$.setBasic(EbtUInt, qual, @1);
   1154         $$.setAggregate(3);
   1155     }
   1156     | UVEC4 {
   1157         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1158         $$.setBasic(EbtUInt, qual, @1);
   1159         $$.setAggregate(4);
   1160     }
   1161     | MATRIX2 {
   1162         FRAG_VERT_ONLY("mat2", @1);
   1163         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1164         $$.setBasic(EbtFloat, qual, @1);
   1165         $$.setMatrix(2, 2);
   1166     }
   1167     | MATRIX3 {
   1168         FRAG_VERT_ONLY("mat3", @1);
   1169         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1170         $$.setBasic(EbtFloat, qual, @1);
   1171         $$.setMatrix(3, 3);
   1172     }
   1173     | MATRIX4 {
   1174         FRAG_VERT_ONLY("mat4", @1);
   1175         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1176         $$.setBasic(EbtFloat, qual, @1);
   1177         $$.setMatrix(4, 4);
   1178     }
   1179     | MATRIX2x3 {
   1180         FRAG_VERT_ONLY("mat2x3", @1);
   1181         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1182         $$.setBasic(EbtFloat, qual, @1);
   1183         $$.setMatrix(2, 3);
   1184     }
   1185     | MATRIX3x2 {
   1186         FRAG_VERT_ONLY("mat3x2", @1);
   1187         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1188         $$.setBasic(EbtFloat, qual, @1);
   1189         $$.setMatrix(3, 2);
   1190     }
   1191     | MATRIX2x4 {
   1192         FRAG_VERT_ONLY("mat2x4", @1);
   1193         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1194         $$.setBasic(EbtFloat, qual, @1);
   1195         $$.setMatrix(2, 4);
   1196     }
   1197     | MATRIX4x2 {
   1198         FRAG_VERT_ONLY("mat4x2", @1);
   1199         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1200         $$.setBasic(EbtFloat, qual, @1);
   1201         $$.setMatrix(4, 2);
   1202     }
   1203     | MATRIX3x4 {
   1204         FRAG_VERT_ONLY("mat3x4", @1);
   1205         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1206         $$.setBasic(EbtFloat, qual, @1);
   1207         $$.setMatrix(3, 4);
   1208     }
   1209     | MATRIX4x3 {
   1210         FRAG_VERT_ONLY("mat4x3", @1);
   1211         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1212         $$.setBasic(EbtFloat, qual, @1);
   1213         $$.setMatrix(4, 3);
   1214     }
   1215     | SAMPLER2D {
   1216         FRAG_VERT_ONLY("sampler2D", @1);
   1217         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1218         $$.setBasic(EbtSampler2D, qual, @1);
   1219     }
   1220     | SAMPLERCUBE {
   1221         FRAG_VERT_ONLY("samplerCube", @1);
   1222         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1223         $$.setBasic(EbtSamplerCube, qual, @1);
   1224     }
   1225     | SAMPLER_EXTERNAL_OES {
   1226         if (!context->supportsExtension("GL_OES_EGL_image_external")) {
   1227             context->error(@1, "unsupported type", "samplerExternalOES", "");
   1228             context->recover();
   1229         }
   1230         FRAG_VERT_ONLY("samplerExternalOES", @1);
   1231         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1232         $$.setBasic(EbtSamplerExternalOES, qual, @1);
   1233     }
   1234     | SAMPLER2DRECT {
   1235         if (!context->supportsExtension("GL_ARB_texture_rectangle")) {
   1236             context->error(@1, "unsupported type", "sampler2DRect", "");
   1237             context->recover();
   1238         }
   1239         FRAG_VERT_ONLY("sampler2DRect", @1);
   1240         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1241         $$.setBasic(EbtSampler2DRect, qual, @1);
   1242     }
   1243     | SAMPLER3D {
   1244         FRAG_VERT_ONLY("sampler3D", @1);
   1245         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1246         $$.setBasic(EbtSampler3D, qual, @1);
   1247     }
   1248     | SAMPLER2DARRAY {
   1249         FRAG_VERT_ONLY("sampler2DArray", @1);
   1250         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1251         $$.setBasic(EbtSampler2DArray, qual, @1);
   1252     }
   1253     | ISAMPLER2D {
   1254         FRAG_VERT_ONLY("isampler2D", @1);
   1255         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1256         $$.setBasic(EbtISampler2D, qual, @1);
   1257     }
   1258     | ISAMPLER3D {
   1259         FRAG_VERT_ONLY("isampler3D", @1);
   1260         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1261         $$.setBasic(EbtISampler3D, qual, @1);
   1262     }
   1263     | ISAMPLERCUBE {
   1264         FRAG_VERT_ONLY("isamplerCube", @1);
   1265         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1266         $$.setBasic(EbtISamplerCube, qual, @1);
   1267     }
   1268     | ISAMPLER2DARRAY {
   1269         FRAG_VERT_ONLY("isampler2DArray", @1);
   1270         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1271         $$.setBasic(EbtISampler2DArray, qual, @1);
   1272     }
   1273     | USAMPLER2D {
   1274         FRAG_VERT_ONLY("usampler2D", @1);
   1275         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1276         $$.setBasic(EbtUSampler2D, qual, @1);
   1277     }
   1278     | USAMPLER3D {
   1279         FRAG_VERT_ONLY("usampler3D", @1);
   1280         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1281         $$.setBasic(EbtUSampler3D, qual, @1);
   1282     }
   1283     | USAMPLERCUBE {
   1284         FRAG_VERT_ONLY("usamplerCube", @1);
   1285         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1286         $$.setBasic(EbtUSamplerCube, qual, @1);
   1287     }
   1288     | USAMPLER2DARRAY {
   1289         FRAG_VERT_ONLY("usampler2DArray", @1);
   1290         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1291         $$.setBasic(EbtUSampler2DArray, qual, @1);
   1292     }
   1293     | SAMPLER2DSHADOW {
   1294         FRAG_VERT_ONLY("sampler2DShadow", @1);
   1295         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1296         $$.setBasic(EbtSampler2DShadow, qual, @1);
   1297     }
   1298     | SAMPLERCUBESHADOW {
   1299         FRAG_VERT_ONLY("samplerCubeShadow", @1);
   1300         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1301         $$.setBasic(EbtSamplerCubeShadow, qual, @1);
   1302     }
   1303     | SAMPLER2DARRAYSHADOW {
   1304         FRAG_VERT_ONLY("sampler2DArrayShadow", @1);
   1305         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1306         $$.setBasic(EbtSampler2DArrayShadow, qual, @1);
   1307     }
   1308     | struct_specifier {
   1309         FRAG_VERT_ONLY("struct", @1);
   1310         $$ = $1;
   1311         $$.qualifier = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1312     }
   1313     | TYPE_NAME {
   1314         //
   1315         // This is for user defined type names.  The lexical phase looked up the
   1316         // type.
   1317         //
   1318         TType& structure = static_cast<TVariable*>($1.symbol)->getType();
   1319         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
   1320         $$.setBasic(EbtStruct, qual, @1);
   1321         $$.userDef = &structure;
   1322     }
   1323     ;
   1324 
   1325 struct_specifier
   1326     : STRUCT IDENTIFIER LEFT_BRACE { if (context->enterStructDeclaration(@2, *$2.string)) context->recover(); } struct_declaration_list RIGHT_BRACE {
   1327         $$ = context->addStructure(@1, @2, $2.string, $5);
   1328     }
   1329     | STRUCT LEFT_BRACE { if (context->enterStructDeclaration(@2, *$2.string)) context->recover(); } struct_declaration_list RIGHT_BRACE {
   1330         $$ = context->addStructure(@1, @1, NewPoolTString(""), $4);
   1331     }
   1332     ;
   1333 
   1334 struct_declaration_list
   1335     : struct_declaration {
   1336         $$ = $1;
   1337     }
   1338     | struct_declaration_list struct_declaration {
   1339         $$ = $1;
   1340         for (unsigned int i = 0; i < $2->size(); ++i) {
   1341             TField* field = (*$2)[i];
   1342             for (unsigned int j = 0; j < $$->size(); ++j) {
   1343                 if ((*$$)[j]->name() == field->name()) {
   1344                     context->error((*$2)[i]->line(), "duplicate field name in structure:", "struct", field->name().c_str());
   1345                     context->recover();
   1346                 }
   1347             }
   1348             $$->push_back((*$2)[i]);
   1349         }
   1350     }
   1351     ;
   1352 
   1353 struct_declaration
   1354     : type_specifier struct_declarator_list SEMICOLON {
   1355         $$ = context->addStructDeclaratorList($1, $2);
   1356     }
   1357     | type_qualifier type_specifier struct_declarator_list SEMICOLON {
   1358         // ES3 Only, but errors should be handled elsewhere
   1359         $2.qualifier = $1.qualifier;
   1360         $2.layoutQualifier = $1.layoutQualifier;
   1361         $$ = context->addStructDeclaratorList($2, $3);
   1362     }
   1363     ;
   1364 
   1365 struct_declarator_list
   1366     : struct_declarator {
   1367         $$ = NewPoolTFieldList();
   1368         $$->push_back($1);
   1369     }
   1370     | struct_declarator_list COMMA struct_declarator {
   1371         $$->push_back($3);
   1372     }
   1373     ;
   1374 
   1375 struct_declarator
   1376     : IDENTIFIER {
   1377         if (context->reservedErrorCheck(@1, *$1.string))
   1378             context->recover();
   1379 
   1380         TType* type = new TType(EbtVoid, EbpUndefined);
   1381         $$ = new TField(type, $1.string, @1);
   1382     }
   1383     | IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
   1384         if (context->reservedErrorCheck(@1, *$1.string))
   1385             context->recover();
   1386 
   1387         TType* type = new TType(EbtVoid, EbpUndefined);
   1388         int size = 0;
   1389         if (context->arraySizeErrorCheck($3->getLine(), $3, size))
   1390             context->recover();
   1391         type->setArraySize(size);
   1392 
   1393         $$ = new TField(type, $1.string, @1);
   1394     }
   1395     ;
   1396 
   1397 initializer
   1398     : assignment_expression { $$ = $1; }
   1399     ;
   1400 
   1401 declaration_statement
   1402     : declaration { $$ = $1; }
   1403     ;
   1404 
   1405 statement
   1406     : compound_statement  { $$ = $1; }
   1407     | simple_statement    { $$ = $1; }
   1408     ;
   1409 
   1410 // Grammar Note:  Labeled statements for SWITCH only; 'goto' is not supported.
   1411 
   1412 simple_statement
   1413     : declaration_statement { $$ = $1; }
   1414     | expression_statement  { $$ = $1; }
   1415     | selection_statement   { $$ = $1; }
   1416     | switch_statement      { $$ = $1; }
   1417     | case_label            { $$ = $1; }
   1418     | iteration_statement   { $$ = $1; }
   1419     | jump_statement        { $$ = $1; }
   1420     ;
   1421 
   1422 compound_statement
   1423     : LEFT_BRACE RIGHT_BRACE { $$ = 0; }
   1424     | LEFT_BRACE { context->symbolTable.push(); } statement_list { context->symbolTable.pop(); } RIGHT_BRACE {
   1425         if ($3 != 0) {
   1426             $3->setOp(EOpSequence);
   1427             $3->setEndLine(@5);
   1428         }
   1429         $$ = $3;
   1430     }
   1431     ;
   1432 
   1433 statement_no_new_scope
   1434     : compound_statement_no_new_scope { $$ = $1; }
   1435     | simple_statement                { $$ = $1; }
   1436     ;
   1437 
   1438 statement_with_scope
   1439     : { context->symbolTable.push(); } compound_statement_no_new_scope { context->symbolTable.pop(); $$ = $2; }
   1440     | { context->symbolTable.push(); } simple_statement                { context->symbolTable.pop(); $$ = $2; }
   1441     ;
   1442 
   1443 compound_statement_no_new_scope
   1444     // Statement that doesn't create a new scope, for selection_statement, iteration_statement
   1445     : LEFT_BRACE RIGHT_BRACE {
   1446         $$ = 0;
   1447     }
   1448     | LEFT_BRACE statement_list RIGHT_BRACE {
   1449         if ($2) {
   1450             $2->setOp(EOpSequence);
   1451             $2->setEndLine(@3);
   1452         }
   1453         $$ = $2;
   1454     }
   1455     ;
   1456 
   1457 statement_list
   1458     : statement {
   1459         $$ = context->intermediate.makeAggregate($1, @$);
   1460     }
   1461     | statement_list statement {
   1462         $$ = context->intermediate.growAggregate($1, $2, @$);
   1463     }
   1464     ;
   1465 
   1466 expression_statement
   1467     : SEMICOLON  { $$ = 0; }
   1468     | expression SEMICOLON  { $$ = static_cast<TIntermNode*>($1); }
   1469     ;
   1470 
   1471 selection_statement
   1472     : IF LEFT_PAREN expression RIGHT_PAREN selection_rest_statement {
   1473         if (context->boolErrorCheck(@1, $3))
   1474             context->recover();
   1475         $$ = context->intermediate.addSelection($3, $5, @1);
   1476     }
   1477     ;
   1478 
   1479 selection_rest_statement
   1480     : statement_with_scope ELSE statement_with_scope {
   1481         $$.node1 = $1;
   1482         $$.node2 = $3;
   1483     }
   1484     | statement_with_scope {
   1485         $$.node1 = $1;
   1486         $$.node2 = 0;
   1487     }
   1488     ;
   1489 
   1490 switch_statement
   1491     : SWITCH LEFT_PAREN expression RIGHT_PAREN { context->incrSwitchNestingLevel(); } compound_statement {
   1492         $$ = context->addSwitch($3, $6, @1);
   1493         context->decrSwitchNestingLevel();
   1494     }
   1495     ;
   1496 
   1497 case_label
   1498     : CASE constant_expression COLON {
   1499         $$ = context->addCase($2, @1);
   1500     }
   1501     | DEFAULT COLON {
   1502         $$ = context->addDefault(@1);
   1503     }
   1504     ;
   1505 
   1506 condition
   1507     // In 1996 c++ draft, conditions can include single declarations
   1508     : expression {
   1509         $$ = $1;
   1510         if (context->boolErrorCheck($1->getLine(), $1))
   1511             context->recover();
   1512     }
   1513     | fully_specified_type IDENTIFIER EQUAL initializer {
   1514         TIntermNode *intermNode;
   1515         if (context->boolErrorCheck(@2, $1))
   1516             context->recover();
   1517 
   1518         if (!context->executeInitializer(@2, *$2.string, $1, $4, &intermNode))
   1519             $$ = $4;
   1520         else {
   1521             context->recover();
   1522             $$ = 0;
   1523         }
   1524     }
   1525     ;
   1526 
   1527 iteration_statement
   1528     : WHILE LEFT_PAREN { context->symbolTable.push(); context->incrLoopNestingLevel(); } condition RIGHT_PAREN statement_no_new_scope {
   1529         context->symbolTable.pop();
   1530         $$ = context->intermediate.addLoop(ELoopWhile, 0, $4, 0, $6, @1);
   1531         context->decrLoopNestingLevel();
   1532     }
   1533     | DO { context->incrLoopNestingLevel(); } statement_with_scope WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON {
   1534         if (context->boolErrorCheck(@8, $6))
   1535             context->recover();
   1536 
   1537         $$ = context->intermediate.addLoop(ELoopDoWhile, 0, $6, 0, $3, @4);
   1538         context->decrLoopNestingLevel();
   1539     }
   1540     | FOR LEFT_PAREN { context->symbolTable.push(); context->incrLoopNestingLevel(); } for_init_statement for_rest_statement RIGHT_PAREN statement_no_new_scope {
   1541         context->symbolTable.pop();
   1542         $$ = context->intermediate.addLoop(ELoopFor, $4, reinterpret_cast<TIntermTyped*>($5.node1), reinterpret_cast<TIntermTyped*>($5.node2), $7, @1);
   1543         context->decrLoopNestingLevel();
   1544     }
   1545     ;
   1546 
   1547 for_init_statement
   1548     : expression_statement {
   1549         $$ = $1;
   1550     }
   1551     | declaration_statement {
   1552         $$ = $1;
   1553     }
   1554     ;
   1555 
   1556 conditionopt
   1557     : condition {
   1558         $$ = $1;
   1559     }
   1560     | /* May be null */ {
   1561         $$ = 0;
   1562     }
   1563     ;
   1564 
   1565 for_rest_statement
   1566     : conditionopt SEMICOLON {
   1567         $$.node1 = $1;
   1568         $$.node2 = 0;
   1569     }
   1570     | conditionopt SEMICOLON expression  {
   1571         $$.node1 = $1;
   1572         $$.node2 = $3;
   1573     }
   1574     ;
   1575 
   1576 jump_statement
   1577     : CONTINUE SEMICOLON {
   1578         $$ = context->addBranch(EOpContinue, @1);
   1579     }
   1580     | BREAK SEMICOLON {
   1581         $$ = context->addBranch(EOpBreak, @1);
   1582     }
   1583     | RETURN SEMICOLON {
   1584         $$ = context->addBranch(EOpReturn, @1);
   1585     }
   1586     | RETURN expression SEMICOLON {
   1587         $$ = context->addBranch(EOpReturn, $2, @1);
   1588     }
   1589     | DISCARD SEMICOLON {
   1590         FRAG_ONLY("discard", @1);
   1591         $$ = context->addBranch(EOpKill, @1);
   1592     }
   1593     ;
   1594 
   1595 // Grammar Note:  No 'goto'.  Gotos are not supported.
   1596 
   1597 translation_unit
   1598     : external_declaration {
   1599         $$ = $1;
   1600         context->setTreeRoot($$);
   1601     }
   1602     | translation_unit external_declaration {
   1603         $$ = context->intermediate.growAggregate($1, $2, @$);
   1604         context->setTreeRoot($$);
   1605     }
   1606     ;
   1607 
   1608 external_declaration
   1609     : function_definition {
   1610         $$ = $1;
   1611     }
   1612     | declaration {
   1613         $$ = $1;
   1614     }
   1615     ;
   1616 
   1617 function_definition
   1618     : function_prototype {
   1619         context->parseFunctionPrototype(@1, $1.function, &$1.intermAggregate);
   1620     }
   1621     compound_statement_no_new_scope {
   1622         $$ = context->addFunctionDefinition(*($1.function), $1.intermAggregate, $3, @1);
   1623     }
   1624     ;
   1625 
   1626 %%
   1627 
   1628 int glslang_parse(TParseContext* context) {
   1629     return yyparse(context, context->getScanner());
   1630 }
   1631