2011-06-20 Zhenyao Mo <zmo@google.com>
[WebKit-https.git] / Source / ThirdParty / ANGLE / src / compiler / glslang.y
1 /*
2 //
3 // Copyright (c) 2002-2010 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.
6 //
7
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
11
12 IF YOU MODIFY THIS FILE YOU ALSO NEED TO RUN generate_glslang_parser.sh,
13 WHICH GENERATES THE GLSL ES PARSER (glslang_tab.cpp AND glslang_tab.h).
14 */
15
16 %{
17 //
18 // Copyright (c) 2002-2010 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.
21 //
22
23 // This file is auto-generated by generate_glslang_parser.sh. DO NOT EDIT!
24
25 #include "compiler/SymbolTable.h"
26 #include "compiler/ParseHelper.h"
27 #include "GLSLANG/ShaderLang.h"
28
29 #define YYLEX_PARAM context->scanner
30 %}
31
32 %expect 1 /* One shift reduce conflict because of if | else */
33 %pure-parser
34 %parse-param {TParseContext* context}
35
36 %union {
37     struct {
38         TSourceLoc line;
39         union {
40             TString *string;
41             float f;
42             int i;
43             bool b;
44         };
45         TSymbol* symbol;
46     } lex;
47     struct {
48         TSourceLoc line;
49         TOperator op;
50         union {
51             TIntermNode* intermNode;
52             TIntermNodePair nodePair;
53             TIntermTyped* intermTypedNode;
54             TIntermAggregate* intermAggregate;
55         };
56         union {
57             TPublicType type;
58             TPrecision precision;
59             TQualifier qualifier;
60             TFunction* function;
61             TParameter param;
62             TTypeLine typeLine;
63             TTypeList* typeList;
64         };
65     } interm;
66 }
67
68 %{
69 extern int yylex(YYSTYPE* yylval_param, void* yyscanner);
70 extern void yyerror(TParseContext* context, const char* reason);
71
72 #define FRAG_VERT_ONLY(S, L) {  \
73     if (context->shaderType != SH_FRAGMENT_SHADER &&  \
74         context->shaderType != SH_VERTEX_SHADER) {  \
75         context->error(L, " supported in vertex/fragment shaders only ", S, "", "");  \
76         context->recover();  \
77     }  \
78 }
79
80 #define VERTEX_ONLY(S, L) {  \
81     if (context->shaderType != SH_VERTEX_SHADER) {  \
82         context->error(L, " supported in vertex shaders only ", S, "", "");  \
83         context->recover();  \
84     }  \
85 }
86
87 #define FRAG_ONLY(S, L) {  \
88     if (context->shaderType != SH_FRAGMENT_SHADER) {  \
89         context->error(L, " supported in fragment shaders only ", S, "", "");  \
90         context->recover();  \
91     }  \
92 }
93 %}
94
95 %token <lex> INVARIANT HIGH_PRECISION MEDIUM_PRECISION LOW_PRECISION PRECISION
96 %token <lex> ATTRIBUTE CONST_QUAL BOOL_TYPE FLOAT_TYPE INT_TYPE
97 %token <lex> BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN
98 %token <lex> BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 VEC2 VEC3 VEC4
99 %token <lex> MATRIX2 MATRIX3 MATRIX4 IN_QUAL OUT_QUAL INOUT_QUAL UNIFORM VARYING
100 %token <lex> STRUCT VOID_TYPE WHILE
101 %token <lex> SAMPLER2D SAMPLERCUBE
102
103 %token <lex> IDENTIFIER TYPE_NAME FLOATCONSTANT INTCONSTANT BOOLCONSTANT
104 %token <lex> FIELD_SELECTION
105 %token <lex> LEFT_OP RIGHT_OP
106 %token <lex> INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
107 %token <lex> AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
108 %token <lex> MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
109 %token <lex> SUB_ASSIGN
110
111 %token <lex> LEFT_PAREN RIGHT_PAREN LEFT_BRACKET RIGHT_BRACKET LEFT_BRACE RIGHT_BRACE DOT
112 %token <lex> COMMA COLON EQUAL SEMICOLON BANG DASH TILDE PLUS STAR SLASH PERCENT
113 %token <lex> LEFT_ANGLE RIGHT_ANGLE VERTICAL_BAR CARET AMPERSAND QUESTION
114
115 %type <interm> assignment_operator unary_operator
116 %type <interm.intermTypedNode> variable_identifier primary_expression postfix_expression
117 %type <interm.intermTypedNode> expression integer_expression assignment_expression
118 %type <interm.intermTypedNode> unary_expression multiplicative_expression additive_expression
119 %type <interm.intermTypedNode> relational_expression equality_expression
120 %type <interm.intermTypedNode> conditional_expression constant_expression
121 %type <interm.intermTypedNode> logical_or_expression logical_xor_expression logical_and_expression
122 %type <interm.intermTypedNode> shift_expression and_expression exclusive_or_expression inclusive_or_expression
123 %type <interm.intermTypedNode> function_call initializer condition conditionopt
124
125 %type <interm.intermNode> translation_unit function_definition
126 %type <interm.intermNode> statement simple_statement
127 %type <interm.intermAggregate>  statement_list compound_statement
128 %type <interm.intermNode> declaration_statement selection_statement expression_statement
129 %type <interm.intermNode> declaration external_declaration
130 %type <interm.intermNode> for_init_statement compound_statement_no_new_scope
131 %type <interm.nodePair> selection_rest_statement for_rest_statement
132 %type <interm.intermNode> iteration_statement jump_statement statement_no_new_scope
133 %type <interm> single_declaration init_declarator_list
134
135 %type <interm> parameter_declaration parameter_declarator parameter_type_specifier
136 %type <interm.qualifier> parameter_qualifier
137
138 %type <interm.precision> precision_qualifier
139 %type <interm.type> type_qualifier fully_specified_type type_specifier
140 %type <interm.type> type_specifier_no_prec type_specifier_nonarray
141 %type <interm.type> struct_specifier
142 %type <interm.typeLine> struct_declarator
143 %type <interm.typeList> struct_declarator_list struct_declaration struct_declaration_list
144 %type <interm.function> function_header function_declarator function_identifier
145 %type <interm.function> function_header_with_parameters function_call_header
146 %type <interm> function_call_header_with_parameters function_call_header_no_parameters function_call_generic function_prototype
147 %type <interm> function_call_or_method
148
149 %start translation_unit
150 %%
151
152 variable_identifier
153     : IDENTIFIER {
154         // The symbol table search was done in the lexical phase
155         const TSymbol* symbol = $1.symbol;
156         const TVariable* variable;
157         if (symbol == 0) {
158             context->error($1.line, "undeclared identifier", $1.string->c_str(), "");
159             context->recover();
160             TType type(EbtFloat, EbpUndefined);
161             TVariable* fakeVariable = new TVariable($1.string, type);
162             context->symbolTable.insert(*fakeVariable);
163             variable = fakeVariable;
164         } else {
165             // This identifier can only be a variable type symbol
166             if (! symbol->isVariable()) {
167                 context->error($1.line, "variable expected", $1.string->c_str(), "");
168                 context->recover();
169             }
170             variable = static_cast<const TVariable*>(symbol);
171         }
172
173         // don't delete $1.string, it's used by error recovery, and the pool
174         // pop will reclaim the memory
175
176         if (variable->getType().getQualifier() == EvqConst ) {
177             ConstantUnion* constArray = variable->getConstPointer();
178             TType t(variable->getType());
179             $$ = context->intermediate.addConstantUnion(constArray, t, $1.line);
180         } else
181             $$ = context->intermediate.addSymbol(variable->getUniqueId(),
182                                                      variable->getName(),
183                                                      variable->getType(), $1.line);
184     }
185     ;
186
187 primary_expression
188     : variable_identifier {
189         $$ = $1;
190     }
191     | INTCONSTANT {
192         //
193         // INT_TYPE is only 16-bit plus sign bit for vertex/fragment shaders,
194         // check for overflow for constants
195         //
196         if (abs($1.i) >= (1 << 16)) {
197             context->error($1.line, " integer constant overflow", "", "");
198             context->recover();
199         }
200         ConstantUnion *unionArray = new ConstantUnion[1];
201         unionArray->setIConst($1.i);
202         $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), $1.line);
203     }
204     | FLOATCONSTANT {
205         ConstantUnion *unionArray = new ConstantUnion[1];
206         unionArray->setFConst($1.f);
207         $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), $1.line);
208     }
209     | BOOLCONSTANT {
210         ConstantUnion *unionArray = new ConstantUnion[1];
211         unionArray->setBConst($1.b);
212         $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $1.line);
213     }
214     | LEFT_PAREN expression RIGHT_PAREN {
215         $$ = $2;
216     }
217     ;
218
219 postfix_expression
220     : primary_expression {
221         $$ = $1;
222     }
223     | postfix_expression LEFT_BRACKET integer_expression RIGHT_BRACKET {
224         if (!$1->isArray() && !$1->isMatrix() && !$1->isVector()) {
225             if ($1->getAsSymbolNode())
226                 context->error($2.line, " left of '[' is not of type array, matrix, or vector ", $1->getAsSymbolNode()->getSymbol().c_str(), "");
227             else
228                 context->error($2.line, " left of '[' is not of type array, matrix, or vector ", "expression", "");
229             context->recover();
230         }
231         if ($1->getType().getQualifier() == EvqConst && $3->getQualifier() == EvqConst) {
232             if ($1->isArray()) { // constant folding for arrays
233                 $$ = context->addConstArrayNode($3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(), $1, $2.line);
234             } else if ($1->isVector()) {  // constant folding for vectors
235                 TVectorFields fields;
236                 fields.num = 1;
237                 fields.offsets[0] = $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(); // need to do it this way because v.xy sends fields integer array
238                 $$ = context->addConstVectorNode(fields, $1, $2.line);
239             } else if ($1->isMatrix()) { // constant folding for matrices
240                 $$ = context->addConstMatrixNode($3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(), $1, $2.line);
241             }
242         } else {
243             if ($3->getQualifier() == EvqConst) {
244                 if (($1->isVector() || $1->isMatrix()) && $1->getType().getNominalSize() <= $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst() && !$1->isArray() ) {
245                     context->error($2.line, "", "[", "field selection out of range '%d'", $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst());
246                     context->recover();
247                 } else {
248                     if ($1->isArray()) {
249                         if ($1->getType().getArraySize() == 0) {
250                             if ($1->getType().getMaxArraySize() <= $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst()) {
251                                 if (context->arraySetMaxSize($1->getAsSymbolNode(), $1->getTypePointer(), $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst(), true, $2.line))
252                                     context->recover();
253                             } else {
254                                 if (context->arraySetMaxSize($1->getAsSymbolNode(), $1->getTypePointer(), 0, false, $2.line))
255                                     context->recover();
256                             }
257                         } else if ( $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst() >= $1->getType().getArraySize()) {
258                             context->error($2.line, "", "[", "array index out of range '%d'", $3->getAsConstantUnion()->getUnionArrayPointer()->getIConst());
259                             context->recover();
260                         }
261                     }
262                     $$ = context->intermediate.addIndex(EOpIndexDirect, $1, $3, $2.line);
263                 }
264             } else {
265                 if ($1->isArray() && $1->getType().getArraySize() == 0) {
266                     context->error($2.line, "", "[", "array must be redeclared with a size before being indexed with a variable");
267                     context->recover();
268                 }
269
270                 $$ = context->intermediate.addIndex(EOpIndexIndirect, $1, $3, $2.line);
271             }
272         }
273         if ($$ == 0) {
274             ConstantUnion *unionArray = new ConstantUnion[1];
275             unionArray->setFConst(0.0f);
276             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), $2.line);
277         } else if ($1->isArray()) {
278             if ($1->getType().getStruct())
279                 $$->setType(TType($1->getType().getStruct(), $1->getType().getTypeName()));
280             else
281                 $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqTemporary, $1->getNominalSize(), $1->isMatrix()));
282
283             if ($1->getType().getQualifier() == EvqConst)
284                 $$->getTypePointer()->setQualifier(EvqConst);
285         } else if ($1->isMatrix() && $1->getType().getQualifier() == EvqConst)
286             $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqConst, $1->getNominalSize()));
287         else if ($1->isMatrix())
288             $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqTemporary, $1->getNominalSize()));
289         else if ($1->isVector() && $1->getType().getQualifier() == EvqConst)
290             $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqConst));
291         else if ($1->isVector())
292             $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqTemporary));
293         else
294             $$->setType($1->getType());
295     }
296     | function_call {
297         $$ = $1;
298     }
299     | postfix_expression DOT FIELD_SELECTION {
300         if ($1->isArray()) {
301             context->error($3.line, "cannot apply dot operator to an array", ".", "");
302             context->recover();
303         }
304
305         if ($1->isVector()) {
306             TVectorFields fields;
307             if (! context->parseVectorFields(*$3.string, $1->getNominalSize(), fields, $3.line)) {
308                 fields.num = 1;
309                 fields.offsets[0] = 0;
310                 context->recover();
311             }
312
313             if ($1->getType().getQualifier() == EvqConst) { // constant folding for vector fields
314                 $$ = context->addConstVectorNode(fields, $1, $3.line);
315                 if ($$ == 0) {
316                     context->recover();
317                     $$ = $1;
318                 }
319                 else
320                     $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqConst, (int) (*$3.string).size()));
321             } else {
322                 if (fields.num == 1) {
323                     ConstantUnion *unionArray = new ConstantUnion[1];
324                     unionArray->setIConst(fields.offsets[0]);
325                     TIntermTyped* index = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), $3.line);
326                     $$ = context->intermediate.addIndex(EOpIndexDirect, $1, index, $2.line);
327                     $$->setType(TType($1->getBasicType(), $1->getPrecision()));
328                 } else {
329                     TString vectorString = *$3.string;
330                     TIntermTyped* index = context->intermediate.addSwizzle(fields, $3.line);
331                     $$ = context->intermediate.addIndex(EOpVectorSwizzle, $1, index, $2.line);
332                     $$->setType(TType($1->getBasicType(), $1->getPrecision(), EvqTemporary, (int) vectorString.size()));
333                 }
334             }
335         } else if ($1->isMatrix()) {
336             TMatrixFields fields;
337             if (! context->parseMatrixFields(*$3.string, $1->getNominalSize(), fields, $3.line)) {
338                 fields.wholeRow = false;
339                 fields.wholeCol = false;
340                 fields.row = 0;
341                 fields.col = 0;
342                 context->recover();
343             }
344
345             if (fields.wholeRow || fields.wholeCol) {
346                 context->error($2.line, " non-scalar fields not implemented yet", ".", "");
347                 context->recover();
348                 ConstantUnion *unionArray = new ConstantUnion[1];
349                 unionArray->setIConst(0);
350                 TIntermTyped* index = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), $3.line);
351                 $$ = context->intermediate.addIndex(EOpIndexDirect, $1, index, $2.line);
352                 $$->setType(TType($1->getBasicType(), $1->getPrecision(),EvqTemporary, $1->getNominalSize()));
353             } else {
354                 ConstantUnion *unionArray = new ConstantUnion[1];
355                 unionArray->setIConst(fields.col * $1->getNominalSize() + fields.row);
356                 TIntermTyped* index = context->intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), $3.line);
357                 $$ = context->intermediate.addIndex(EOpIndexDirect, $1, index, $2.line);
358                 $$->setType(TType($1->getBasicType(), $1->getPrecision()));
359             }
360         } else if ($1->getBasicType() == EbtStruct) {
361             bool fieldFound = false;
362             const TTypeList* fields = $1->getType().getStruct();
363             if (fields == 0) {
364                 context->error($2.line, "structure has no fields", "Internal Error", "");
365                 context->recover();
366                 $$ = $1;
367             } else {
368                 unsigned int i;
369                 for (i = 0; i < fields->size(); ++i) {
370                     if ((*fields)[i].type->getFieldName() == *$3.string) {
371                         fieldFound = true;
372                         break;
373                     }
374                 }
375                 if (fieldFound) {
376                     if ($1->getType().getQualifier() == EvqConst) {
377                         $$ = context->addConstStruct(*$3.string, $1, $2.line);
378                         if ($$ == 0) {
379                             context->recover();
380                             $$ = $1;
381                         }
382                         else {
383                             $$->setType(*(*fields)[i].type);
384                             // change the qualifier of the return type, not of the structure field
385                             // as the structure definition is shared between various structures.
386                             $$->getTypePointer()->setQualifier(EvqConst);
387                         }
388                     } else {
389                         ConstantUnion *unionArray = new ConstantUnion[1];
390                         unionArray->setIConst(i);
391                         TIntermTyped* index = context->intermediate.addConstantUnion(unionArray, *(*fields)[i].type, $3.line);
392                         $$ = context->intermediate.addIndex(EOpIndexDirectStruct, $1, index, $2.line);
393                         $$->setType(*(*fields)[i].type);
394                     }
395                 } else {
396                     context->error($2.line, " no such field in structure", $3.string->c_str(), "");
397                     context->recover();
398                     $$ = $1;
399                 }
400             }
401         } else {
402             context->error($2.line, " field selection requires structure, vector, or matrix on left hand side", $3.string->c_str(), "");
403             context->recover();
404             $$ = $1;
405         }
406         // don't delete $3.string, it's from the pool
407     }
408     | postfix_expression INC_OP {
409         if (context->lValueErrorCheck($2.line, "++", $1))
410             context->recover();
411         $$ = context->intermediate.addUnaryMath(EOpPostIncrement, $1, $2.line, context->symbolTable);
412         if ($$ == 0) {
413             context->unaryOpError($2.line, "++", $1->getCompleteString());
414             context->recover();
415             $$ = $1;
416         }
417     }
418     | postfix_expression DEC_OP {
419         if (context->lValueErrorCheck($2.line, "--", $1))
420             context->recover();
421         $$ = context->intermediate.addUnaryMath(EOpPostDecrement, $1, $2.line, context->symbolTable);
422         if ($$ == 0) {
423             context->unaryOpError($2.line, "--", $1->getCompleteString());
424             context->recover();
425             $$ = $1;
426         }
427     }
428     ;
429
430 integer_expression
431     : expression {
432         if (context->integerErrorCheck($1, "[]"))
433             context->recover();
434         $$ = $1;
435     }
436     ;
437
438 function_call
439     : function_call_or_method {
440         TFunction* fnCall = $1.function;
441         TOperator op = fnCall->getBuiltInOp();
442
443         if (op != EOpNull)
444         {
445             //
446             // Then this should be a constructor.
447             // Don't go through the symbol table for constructors.
448             // Their parameters will be verified algorithmically.
449             //
450             TType type(EbtVoid, EbpUndefined);  // use this to get the type back
451             if (context->constructorErrorCheck($1.line, $1.intermNode, *fnCall, op, &type)) {
452                 $$ = 0;
453             } else {
454                 //
455                 // It's a constructor, of type 'type'.
456                 //
457                 $$ = context->addConstructor($1.intermNode, &type, op, fnCall, $1.line);
458             }
459
460             if ($$ == 0) {
461                 context->recover();
462                 $$ = context->intermediate.setAggregateOperator(0, op, $1.line);
463             }
464             $$->setType(type);
465         } else {
466             //
467             // Not a constructor.  Find it in the symbol table.
468             //
469             const TFunction* fnCandidate;
470             bool builtIn;
471             fnCandidate = context->findFunction($1.line, fnCall, &builtIn);
472             if (fnCandidate) {
473                 //
474                 // A declared function.
475                 //
476                 if (builtIn && !fnCandidate->getExtension().empty() &&
477                     context->extensionErrorCheck($1.line, fnCandidate->getExtension())) {
478                     context->recover();
479                 }
480                 op = fnCandidate->getBuiltInOp();
481                 if (builtIn && op != EOpNull) {
482                     //
483                     // A function call mapped to a built-in operation.
484                     //
485                     if (fnCandidate->getParamCount() == 1) {
486                         //
487                         // Treat it like a built-in unary operator.
488                         //
489                         $$ = context->intermediate.addUnaryMath(op, $1.intermNode, 0, context->symbolTable);
490                         if ($$ == 0)  {
491                             context->error($1.intermNode->getLine(), " wrong operand type", "Internal Error",
492                                 "built in unary operator function.  Type: %s",
493                                 static_cast<TIntermTyped*>($1.intermNode)->getCompleteString().c_str());
494                             YYERROR;
495                         }
496                     } else {
497                         $$ = context->intermediate.setAggregateOperator($1.intermAggregate, op, $1.line);
498                     }
499                 } else {
500                     // This is a real function call
501
502                     $$ = context->intermediate.setAggregateOperator($1.intermAggregate, EOpFunctionCall, $1.line);
503                     $$->setType(fnCandidate->getReturnType());
504
505                     // this is how we know whether the given function is a builtIn function or a user defined function
506                     // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
507                     // if builtIn == true, it's definitely a builtIn function with EOpNull
508                     if (!builtIn)
509                         $$->getAsAggregate()->setUserDefined();
510                     $$->getAsAggregate()->setName(fnCandidate->getMangledName());
511
512                     TQualifier qual;
513                     for (int i = 0; i < fnCandidate->getParamCount(); ++i) {
514                         qual = fnCandidate->getParam(i).type->getQualifier();
515                         if (qual == EvqOut || qual == EvqInOut) {
516                             if (context->lValueErrorCheck($$->getLine(), "assign", $$->getAsAggregate()->getSequence()[i]->getAsTyped())) {
517                                 context->error($1.intermNode->getLine(), "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error", "");
518                                 context->recover();
519                             }
520                         }
521                     }
522                 }
523                 $$->setType(fnCandidate->getReturnType());
524             } else {
525                 // error message was put out by PaFindFunction()
526                 // Put on a dummy node for error recovery
527                 ConstantUnion *unionArray = new ConstantUnion[1];
528                 unionArray->setFConst(0.0f);
529                 $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), $1.line);
530                 context->recover();
531             }
532         }
533         delete fnCall;
534     }
535     ;
536
537 function_call_or_method
538     : function_call_generic {
539         $$ = $1;
540     }
541     | postfix_expression DOT function_call_generic {
542         context->error($3.line, "methods are not supported", "", "");
543         context->recover();
544         $$ = $3;
545     }
546     ;
547
548 function_call_generic
549     : function_call_header_with_parameters RIGHT_PAREN {
550         $$ = $1;
551         $$.line = $2.line;
552     }
553     | function_call_header_no_parameters RIGHT_PAREN {
554         $$ = $1;
555         $$.line = $2.line;
556     }
557     ;
558
559 function_call_header_no_parameters
560     : function_call_header VOID_TYPE {
561         $$.function = $1;
562         $$.intermNode = 0;
563     }
564     | function_call_header {
565         $$.function = $1;
566         $$.intermNode = 0;
567     }
568     ;
569
570 function_call_header_with_parameters
571     : function_call_header assignment_expression {
572         TParameter param = { 0, new TType($2->getType()) };
573         $1->addParameter(param);
574         $$.function = $1;
575         $$.intermNode = $2;
576     }
577     | function_call_header_with_parameters COMMA assignment_expression {
578         TParameter param = { 0, new TType($3->getType()) };
579         $1.function->addParameter(param);
580         $$.function = $1.function;
581         $$.intermNode = context->intermediate.growAggregate($1.intermNode, $3, $2.line);
582     }
583     ;
584
585 function_call_header
586     : function_identifier LEFT_PAREN {
587         $$ = $1;
588     }
589     ;
590
591 // Grammar Note:  Constructors look like functions, but are recognized as types.
592
593 function_identifier
594     : type_specifier_nonarray {
595         //
596         // Constructor
597         //
598         TOperator op = EOpNull;
599         if ($1.userDef) {
600             op = EOpConstructStruct;
601         } else {
602             switch ($1.type) {
603             case EbtFloat:
604                 if ($1.matrix) {
605                     switch($1.size) {
606                     case 2:                                     op = EOpConstructMat2;  break;
607                     case 3:                                     op = EOpConstructMat3;  break;
608                     case 4:                                     op = EOpConstructMat4;  break;
609                     }
610                 } else {
611                     switch($1.size) {
612                     case 1:                                     op = EOpConstructFloat; break;
613                     case 2:                                     op = EOpConstructVec2;  break;
614                     case 3:                                     op = EOpConstructVec3;  break;
615                     case 4:                                     op = EOpConstructVec4;  break;
616                     }
617                 }
618                 break;
619             case EbtInt:
620                 switch($1.size) {
621                 case 1:                                         op = EOpConstructInt;   break;
622                 case 2:       FRAG_VERT_ONLY("ivec2", $1.line); op = EOpConstructIVec2; break;
623                 case 3:       FRAG_VERT_ONLY("ivec3", $1.line); op = EOpConstructIVec3; break;
624                 case 4:       FRAG_VERT_ONLY("ivec4", $1.line); op = EOpConstructIVec4; break;
625                 }
626                 break;
627             case EbtBool:
628                 switch($1.size) {
629                 case 1:                                         op = EOpConstructBool;  break;
630                 case 2:       FRAG_VERT_ONLY("bvec2", $1.line); op = EOpConstructBVec2; break;
631                 case 3:       FRAG_VERT_ONLY("bvec3", $1.line); op = EOpConstructBVec3; break;
632                 case 4:       FRAG_VERT_ONLY("bvec4", $1.line); op = EOpConstructBVec4; break;
633                 }
634                 break;
635             default: break;
636             }
637             if (op == EOpNull) {
638                 context->error($1.line, "cannot construct this type", getBasicString($1.type), "");
639                 context->recover();
640                 $1.type = EbtFloat;
641                 op = EOpConstructFloat;
642             }
643         }
644         TString tempString;
645         TType type($1);
646         TFunction *function = new TFunction(&tempString, type, op);
647         $$ = function;
648     }
649     | IDENTIFIER {
650         if (context->reservedErrorCheck($1.line, *$1.string))
651             context->recover();
652         TType type(EbtVoid, EbpUndefined);
653         TFunction *function = new TFunction($1.string, type);
654         $$ = function;
655     }
656     | FIELD_SELECTION {
657         if (context->reservedErrorCheck($1.line, *$1.string))
658             context->recover();
659         TType type(EbtVoid, EbpUndefined);
660         TFunction *function = new TFunction($1.string, type);
661         $$ = function;
662     }
663     ;
664
665 unary_expression
666     : postfix_expression {
667         $$ = $1;
668     }
669     | INC_OP unary_expression {
670         if (context->lValueErrorCheck($1.line, "++", $2))
671             context->recover();
672         $$ = context->intermediate.addUnaryMath(EOpPreIncrement, $2, $1.line, context->symbolTable);
673         if ($$ == 0) {
674             context->unaryOpError($1.line, "++", $2->getCompleteString());
675             context->recover();
676             $$ = $2;
677         }
678     }
679     | DEC_OP unary_expression {
680         if (context->lValueErrorCheck($1.line, "--", $2))
681             context->recover();
682         $$ = context->intermediate.addUnaryMath(EOpPreDecrement, $2, $1.line, context->symbolTable);
683         if ($$ == 0) {
684             context->unaryOpError($1.line, "--", $2->getCompleteString());
685             context->recover();
686             $$ = $2;
687         }
688     }
689     | unary_operator unary_expression {
690         if ($1.op != EOpNull) {
691             $$ = context->intermediate.addUnaryMath($1.op, $2, $1.line, context->symbolTable);
692             if ($$ == 0) {
693                 const char* errorOp = "";
694                 switch($1.op) {
695                 case EOpNegative:   errorOp = "-"; break;
696                 case EOpLogicalNot: errorOp = "!"; break;
697                 default: break;
698                 }
699                 context->unaryOpError($1.line, errorOp, $2->getCompleteString());
700                 context->recover();
701                 $$ = $2;
702             }
703         } else
704             $$ = $2;
705     }
706     ;
707 // Grammar Note:  No traditional style type casts.
708
709 unary_operator
710     : PLUS  { $$.line = $1.line; $$.op = EOpNull; }
711     | DASH  { $$.line = $1.line; $$.op = EOpNegative; }
712     | BANG  { $$.line = $1.line; $$.op = EOpLogicalNot; }
713     ;
714 // Grammar Note:  No '*' or '&' unary ops.  Pointers are not supported.
715
716 multiplicative_expression
717     : unary_expression { $$ = $1; }
718     | multiplicative_expression STAR unary_expression {
719         FRAG_VERT_ONLY("*", $2.line);
720         $$ = context->intermediate.addBinaryMath(EOpMul, $1, $3, $2.line, context->symbolTable);
721         if ($$ == 0) {
722             context->binaryOpError($2.line, "*", $1->getCompleteString(), $3->getCompleteString());
723             context->recover();
724             $$ = $1;
725         }
726     }
727     | multiplicative_expression SLASH unary_expression {
728         FRAG_VERT_ONLY("/", $2.line);
729         $$ = context->intermediate.addBinaryMath(EOpDiv, $1, $3, $2.line, context->symbolTable);
730         if ($$ == 0) {
731             context->binaryOpError($2.line, "/", $1->getCompleteString(), $3->getCompleteString());
732             context->recover();
733             $$ = $1;
734         }
735     }
736     ;
737
738 additive_expression
739     : multiplicative_expression { $$ = $1; }
740     | additive_expression PLUS multiplicative_expression {
741         $$ = context->intermediate.addBinaryMath(EOpAdd, $1, $3, $2.line, context->symbolTable);
742         if ($$ == 0) {
743             context->binaryOpError($2.line, "+", $1->getCompleteString(), $3->getCompleteString());
744             context->recover();
745             $$ = $1;
746         }
747     }
748     | additive_expression DASH multiplicative_expression {
749         $$ = context->intermediate.addBinaryMath(EOpSub, $1, $3, $2.line, context->symbolTable);
750         if ($$ == 0) {
751             context->binaryOpError($2.line, "-", $1->getCompleteString(), $3->getCompleteString());
752             context->recover();
753             $$ = $1;
754         }
755     }
756     ;
757
758 shift_expression
759     : additive_expression { $$ = $1; }
760     ;
761
762 relational_expression
763     : shift_expression { $$ = $1; }
764     | relational_expression LEFT_ANGLE shift_expression {
765         $$ = context->intermediate.addBinaryMath(EOpLessThan, $1, $3, $2.line, context->symbolTable);
766         if ($$ == 0) {
767             context->binaryOpError($2.line, "<", $1->getCompleteString(), $3->getCompleteString());
768             context->recover();
769             ConstantUnion *unionArray = new ConstantUnion[1];
770             unionArray->setBConst(false);
771             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
772         }
773     }
774     | relational_expression RIGHT_ANGLE shift_expression  {
775         $$ = context->intermediate.addBinaryMath(EOpGreaterThan, $1, $3, $2.line, context->symbolTable);
776         if ($$ == 0) {
777             context->binaryOpError($2.line, ">", $1->getCompleteString(), $3->getCompleteString());
778             context->recover();
779             ConstantUnion *unionArray = new ConstantUnion[1];
780             unionArray->setBConst(false);
781             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
782         }
783     }
784     | relational_expression LE_OP shift_expression  {
785         $$ = context->intermediate.addBinaryMath(EOpLessThanEqual, $1, $3, $2.line, context->symbolTable);
786         if ($$ == 0) {
787             context->binaryOpError($2.line, "<=", $1->getCompleteString(), $3->getCompleteString());
788             context->recover();
789             ConstantUnion *unionArray = new ConstantUnion[1];
790             unionArray->setBConst(false);
791             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
792         }
793     }
794     | relational_expression GE_OP shift_expression  {
795         $$ = context->intermediate.addBinaryMath(EOpGreaterThanEqual, $1, $3, $2.line, context->symbolTable);
796         if ($$ == 0) {
797             context->binaryOpError($2.line, ">=", $1->getCompleteString(), $3->getCompleteString());
798             context->recover();
799             ConstantUnion *unionArray = new ConstantUnion[1];
800             unionArray->setBConst(false);
801             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
802         }
803     }
804     ;
805
806 equality_expression
807     : relational_expression { $$ = $1; }
808     | equality_expression EQ_OP relational_expression  {
809         $$ = context->intermediate.addBinaryMath(EOpEqual, $1, $3, $2.line, context->symbolTable);
810         if ($$ == 0) {
811             context->binaryOpError($2.line, "==", $1->getCompleteString(), $3->getCompleteString());
812             context->recover();
813             ConstantUnion *unionArray = new ConstantUnion[1];
814             unionArray->setBConst(false);
815             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
816         }
817     }
818     | equality_expression NE_OP relational_expression {
819         $$ = context->intermediate.addBinaryMath(EOpNotEqual, $1, $3, $2.line, context->symbolTable);
820         if ($$ == 0) {
821             context->binaryOpError($2.line, "!=", $1->getCompleteString(), $3->getCompleteString());
822             context->recover();
823             ConstantUnion *unionArray = new ConstantUnion[1];
824             unionArray->setBConst(false);
825             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
826         }
827     }
828     ;
829
830 and_expression
831     : equality_expression { $$ = $1; }
832     ;
833
834 exclusive_or_expression
835     : and_expression { $$ = $1; }
836     ;
837
838 inclusive_or_expression
839     : exclusive_or_expression { $$ = $1; }
840     ;
841
842 logical_and_expression
843     : inclusive_or_expression { $$ = $1; }
844     | logical_and_expression AND_OP inclusive_or_expression {
845         $$ = context->intermediate.addBinaryMath(EOpLogicalAnd, $1, $3, $2.line, context->symbolTable);
846         if ($$ == 0) {
847             context->binaryOpError($2.line, "&&", $1->getCompleteString(), $3->getCompleteString());
848             context->recover();
849             ConstantUnion *unionArray = new ConstantUnion[1];
850             unionArray->setBConst(false);
851             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
852         }
853     }
854     ;
855
856 logical_xor_expression
857     : logical_and_expression { $$ = $1; }
858     | logical_xor_expression XOR_OP logical_and_expression  {
859         $$ = context->intermediate.addBinaryMath(EOpLogicalXor, $1, $3, $2.line, context->symbolTable);
860         if ($$ == 0) {
861             context->binaryOpError($2.line, "^^", $1->getCompleteString(), $3->getCompleteString());
862             context->recover();
863             ConstantUnion *unionArray = new ConstantUnion[1];
864             unionArray->setBConst(false);
865             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
866         }
867     }
868     ;
869
870 logical_or_expression
871     : logical_xor_expression { $$ = $1; }
872     | logical_or_expression OR_OP logical_xor_expression  {
873         $$ = context->intermediate.addBinaryMath(EOpLogicalOr, $1, $3, $2.line, context->symbolTable);
874         if ($$ == 0) {
875             context->binaryOpError($2.line, "||", $1->getCompleteString(), $3->getCompleteString());
876             context->recover();
877             ConstantUnion *unionArray = new ConstantUnion[1];
878             unionArray->setBConst(false);
879             $$ = context->intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), $2.line);
880         }
881     }
882     ;
883
884 conditional_expression
885     : logical_or_expression { $$ = $1; }
886     | logical_or_expression QUESTION expression COLON assignment_expression {
887        if (context->boolErrorCheck($2.line, $1))
888             context->recover();
889
890         $$ = context->intermediate.addSelection($1, $3, $5, $2.line);
891         if ($3->getType() != $5->getType())
892             $$ = 0;
893
894         if ($$ == 0) {
895             context->binaryOpError($2.line, ":", $3->getCompleteString(), $5->getCompleteString());
896             context->recover();
897             $$ = $5;
898         }
899     }
900     ;
901
902 assignment_expression
903     : conditional_expression { $$ = $1; }
904     | unary_expression assignment_operator assignment_expression {
905         if (context->lValueErrorCheck($2.line, "assign", $1))
906             context->recover();
907         $$ = context->intermediate.addAssign($2.op, $1, $3, $2.line);
908         if ($$ == 0) {
909             context->assignError($2.line, "assign", $1->getCompleteString(), $3->getCompleteString());
910             context->recover();
911             $$ = $1;
912         }
913     }
914     ;
915
916 assignment_operator
917     : EQUAL        {                                    $$.line = $1.line; $$.op = EOpAssign; }
918     | MUL_ASSIGN   { FRAG_VERT_ONLY("*=", $1.line);     $$.line = $1.line; $$.op = EOpMulAssign; }
919     | DIV_ASSIGN   { FRAG_VERT_ONLY("/=", $1.line);     $$.line = $1.line; $$.op = EOpDivAssign; }
920     | ADD_ASSIGN   {                                    $$.line = $1.line; $$.op = EOpAddAssign; }
921     | SUB_ASSIGN   {                                    $$.line = $1.line; $$.op = EOpSubAssign; }
922     ;
923
924 expression
925     : assignment_expression {
926         $$ = $1;
927     }
928     | expression COMMA assignment_expression {
929         $$ = context->intermediate.addComma($1, $3, $2.line);
930         if ($$ == 0) {
931             context->binaryOpError($2.line, ",", $1->getCompleteString(), $3->getCompleteString());
932             context->recover();
933             $$ = $3;
934         }
935     }
936     ;
937
938 constant_expression
939     : conditional_expression {
940         if (context->constErrorCheck($1))
941             context->recover();
942         $$ = $1;
943     }
944     ;
945
946 declaration
947     : function_prototype SEMICOLON   {
948         TFunction &function = *($1.function);
949         
950         TIntermAggregate *prototype = new TIntermAggregate;
951         prototype->setType(function.getReturnType());
952         prototype->setName(function.getName());
953         
954         for (int i = 0; i < function.getParamCount(); i++)
955         {
956             const TParameter &param = function.getParam(i);
957             if (param.name != 0)
958             {
959                 TVariable *variable = new TVariable(param.name, *param.type);
960                 
961                 prototype = context->intermediate.growAggregate(prototype, context->intermediate.addSymbol(variable->getUniqueId(), variable->getName(), variable->getType(), $1.line), $1.line);
962             }
963             else
964             {
965                 prototype = context->intermediate.growAggregate(prototype, context->intermediate.addSymbol(0, "", *param.type, $1.line), $1.line);
966             }
967         }
968         
969         prototype->setOp(EOpPrototype);
970         $$ = prototype;
971     }
972     | init_declarator_list SEMICOLON {
973         if ($1.intermAggregate)
974             $1.intermAggregate->setOp(EOpDeclaration);
975         $$ = $1.intermAggregate;
976     }
977     | PRECISION precision_qualifier type_specifier_no_prec SEMICOLON {
978         context->symbolTable.setDefaultPrecision( $3.type, $2 );
979         $$ = 0;
980     }
981     ;
982
983 function_prototype
984     : function_declarator RIGHT_PAREN  {
985         //
986         // Multiple declarations of the same function are allowed.
987         //
988         // If this is a definition, the definition production code will check for redefinitions
989         // (we don't know at this point if it's a definition or not).
990         //
991         // Redeclarations are allowed.  But, return types and parameter qualifiers must match.
992         //
993         TFunction* prevDec = static_cast<TFunction*>(context->symbolTable.find($1->getMangledName()));
994         if (prevDec) {
995             if (prevDec->getReturnType() != $1->getReturnType()) {
996                 context->error($2.line, "overloaded functions must have the same return type", $1->getReturnType().getBasicString(), "");
997                 context->recover();
998             }
999             for (int i = 0; i < prevDec->getParamCount(); ++i) {
1000                 if (prevDec->getParam(i).type->getQualifier() != $1->getParam(i).type->getQualifier()) {
1001                     context->error($2.line, "overloaded functions must have the same parameter qualifiers", $1->getParam(i).type->getQualifierString(), "");
1002                     context->recover();
1003                 }
1004             }
1005         }
1006
1007         //
1008         // If this is a redeclaration, it could also be a definition,
1009         // in which case, we want to use the variable names from this one, and not the one that's
1010         // being redeclared.  So, pass back up this declaration, not the one in the symbol table.
1011         //
1012         $$.function = $1;
1013         $$.line = $2.line;
1014
1015         context->symbolTable.insert(*$$.function);
1016     }
1017     ;
1018
1019 function_declarator
1020     : function_header {
1021         $$ = $1;
1022     }
1023     | function_header_with_parameters {
1024         $$ = $1;
1025     }
1026     ;
1027
1028
1029 function_header_with_parameters
1030     : function_header parameter_declaration {
1031         // Add the parameter
1032         $$ = $1;
1033         if ($2.param.type->getBasicType() != EbtVoid)
1034             $1->addParameter($2.param);
1035         else
1036             delete $2.param.type;
1037     }
1038     | function_header_with_parameters COMMA parameter_declaration {
1039         //
1040         // Only first parameter of one-parameter functions can be void
1041         // The check for named parameters not being void is done in parameter_declarator
1042         //
1043         if ($3.param.type->getBasicType() == EbtVoid) {
1044             //
1045             // This parameter > first is void
1046             //
1047             context->error($2.line, "cannot be an argument type except for '(void)'", "void", "");
1048             context->recover();
1049             delete $3.param.type;
1050         } else {
1051             // Add the parameter
1052             $$ = $1;
1053             $1->addParameter($3.param);
1054         }
1055     }
1056     ;
1057
1058 function_header
1059     : fully_specified_type IDENTIFIER LEFT_PAREN {
1060         if ($1.qualifier != EvqGlobal && $1.qualifier != EvqTemporary) {
1061             context->error($2.line, "no qualifiers allowed for function return", getQualifierString($1.qualifier), "");
1062             context->recover();
1063         }
1064         // make sure a sampler is not involved as well...
1065         if (context->structQualifierErrorCheck($2.line, $1))
1066             context->recover();
1067
1068         // Add the function as a prototype after parsing it (we do not support recursion)
1069         TFunction *function;
1070         TType type($1);
1071         function = new TFunction($2.string, type);
1072         $$ = function;
1073     }
1074     ;
1075
1076 parameter_declarator
1077     // Type + name
1078     : type_specifier IDENTIFIER {
1079         if ($1.type == EbtVoid) {
1080             context->error($2.line, "illegal use of type 'void'", $2.string->c_str(), "");
1081             context->recover();
1082         }
1083         if (context->reservedErrorCheck($2.line, *$2.string))
1084             context->recover();
1085         TParameter param = {$2.string, new TType($1)};
1086         $$.line = $2.line;
1087         $$.param = param;
1088     }
1089     | type_specifier IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
1090         // Check that we can make an array out of this type
1091         if (context->arrayTypeErrorCheck($3.line, $1))
1092             context->recover();
1093
1094         if (context->reservedErrorCheck($2.line, *$2.string))
1095             context->recover();
1096
1097         int size;
1098         if (context->arraySizeErrorCheck($3.line, $4, size))
1099             context->recover();
1100         $1.setArray(true, size);
1101
1102         TType* type = new TType($1);
1103         TParameter param = { $2.string, type };
1104         $$.line = $2.line;
1105         $$.param = param;
1106     }
1107     ;
1108
1109 parameter_declaration
1110     //
1111     // The only parameter qualifier a parameter can have are
1112     // IN_QUAL, OUT_QUAL, INOUT_QUAL, or CONST.
1113     //
1114
1115     //
1116     // Type + name
1117     //
1118     : type_qualifier parameter_qualifier parameter_declarator {
1119         $$ = $3;
1120         if (context->paramErrorCheck($3.line, $1.qualifier, $2, $$.param.type))
1121             context->recover();
1122     }
1123     | parameter_qualifier parameter_declarator {
1124         $$ = $2;
1125         if (context->parameterSamplerErrorCheck($2.line, $1, *$2.param.type))
1126             context->recover();
1127         if (context->paramErrorCheck($2.line, EvqTemporary, $1, $$.param.type))
1128             context->recover();
1129     }
1130     //
1131     // Only type
1132     //
1133     | type_qualifier parameter_qualifier parameter_type_specifier {
1134         $$ = $3;
1135         if (context->paramErrorCheck($3.line, $1.qualifier, $2, $$.param.type))
1136             context->recover();
1137     }
1138     | parameter_qualifier parameter_type_specifier {
1139         $$ = $2;
1140         if (context->parameterSamplerErrorCheck($2.line, $1, *$2.param.type))
1141             context->recover();
1142         if (context->paramErrorCheck($2.line, EvqTemporary, $1, $$.param.type))
1143             context->recover();
1144     }
1145     ;
1146
1147 parameter_qualifier
1148     : /* empty */ {
1149         $$ = EvqIn;
1150     }
1151     | IN_QUAL {
1152         $$ = EvqIn;
1153     }
1154     | OUT_QUAL {
1155         $$ = EvqOut;
1156     }
1157     | INOUT_QUAL {
1158         $$ = EvqInOut;
1159     }
1160     ;
1161
1162 parameter_type_specifier
1163     : type_specifier {
1164         TParameter param = { 0, new TType($1) };
1165         $$.param = param;
1166     }
1167     ;
1168
1169 init_declarator_list
1170     : single_declaration {
1171         $$ = $1;
1172     }
1173     | init_declarator_list COMMA IDENTIFIER {
1174         TIntermSymbol* symbol = context->intermediate.addSymbol(0, *$3.string, TType($1.type), $3.line);
1175         $$.intermAggregate = context->intermediate.growAggregate($1.intermNode, symbol, $3.line);
1176         
1177         if (context->structQualifierErrorCheck($3.line, $$.type))
1178             context->recover();
1179
1180         if (context->nonInitConstErrorCheck($3.line, *$3.string, $$.type))
1181             context->recover();
1182
1183         TVariable* variable = 0;
1184         if (context->nonInitErrorCheck($3.line, *$3.string, $$.type, variable))
1185             context->recover();
1186         if (symbol && variable)
1187             symbol->setId(variable->getUniqueId());
1188     }
1189     | init_declarator_list COMMA IDENTIFIER LEFT_BRACKET RIGHT_BRACKET {
1190         if (context->structQualifierErrorCheck($3.line, $1.type))
1191             context->recover();
1192
1193         if (context->nonInitConstErrorCheck($3.line, *$3.string, $1.type))
1194             context->recover();
1195
1196         $$ = $1;
1197
1198         if (context->arrayTypeErrorCheck($4.line, $1.type) || context->arrayQualifierErrorCheck($4.line, $1.type))
1199             context->recover();
1200         else {
1201             $1.type.setArray(true);
1202             TVariable* variable;
1203             if (context->arrayErrorCheck($4.line, *$3.string, $1.type, variable))
1204                 context->recover();
1205         }
1206     }
1207     | init_declarator_list COMMA IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
1208         if (context->structQualifierErrorCheck($3.line, $1.type))
1209             context->recover();
1210
1211         if (context->nonInitConstErrorCheck($3.line, *$3.string, $1.type))
1212             context->recover();
1213
1214         $$ = $1;
1215
1216         if (context->arrayTypeErrorCheck($4.line, $1.type) || context->arrayQualifierErrorCheck($4.line, $1.type))
1217             context->recover();
1218         else {
1219             int size;
1220             if (context->arraySizeErrorCheck($4.line, $5, size))
1221                 context->recover();
1222             $1.type.setArray(true, size);
1223             TVariable* variable = 0;
1224             if (context->arrayErrorCheck($4.line, *$3.string, $1.type, variable))
1225                 context->recover();
1226             TType type = TType($1.type);
1227             type.setArraySize(size);
1228             $$.intermAggregate = context->intermediate.growAggregate($1.intermNode, context->intermediate.addSymbol(variable ? variable->getUniqueId() : 0, *$3.string, type, $3.line), $3.line);
1229         }
1230     }
1231     | init_declarator_list COMMA IDENTIFIER EQUAL initializer {
1232         if (context->structQualifierErrorCheck($3.line, $1.type))
1233             context->recover();
1234
1235         $$ = $1;
1236
1237         TIntermNode* intermNode;
1238         if (!context->executeInitializer($3.line, *$3.string, $1.type, $5, intermNode)) {
1239             //
1240             // build the intermediate representation
1241             //
1242             if (intermNode)
1243         $$.intermAggregate = context->intermediate.growAggregate($1.intermNode, intermNode, $4.line);
1244             else
1245                 $$.intermAggregate = $1.intermAggregate;
1246         } else {
1247             context->recover();
1248             $$.intermAggregate = 0;
1249         }
1250     }
1251     ;
1252
1253 single_declaration
1254     : fully_specified_type {
1255         $$.type = $1;
1256         $$.intermAggregate = context->intermediate.makeAggregate(context->intermediate.addSymbol(0, "", TType($1), $1.line), $1.line);
1257     }
1258     | fully_specified_type IDENTIFIER {
1259         TIntermSymbol* symbol = context->intermediate.addSymbol(0, *$2.string, TType($1), $2.line);
1260         $$.intermAggregate = context->intermediate.makeAggregate(symbol, $2.line);
1261         
1262         if (context->structQualifierErrorCheck($2.line, $$.type))
1263             context->recover();
1264
1265         if (context->nonInitConstErrorCheck($2.line, *$2.string, $$.type))
1266             context->recover();
1267             
1268             $$.type = $1;
1269
1270         TVariable* variable = 0;
1271         if (context->nonInitErrorCheck($2.line, *$2.string, $$.type, variable))
1272             context->recover();
1273         if (variable && symbol)
1274             symbol->setId(variable->getUniqueId());
1275     }
1276     | fully_specified_type IDENTIFIER LEFT_BRACKET RIGHT_BRACKET {
1277         context->error($2.line, "unsized array declarations not supported", $2.string->c_str(), "");
1278         context->recover();
1279
1280         TIntermSymbol* symbol = context->intermediate.addSymbol(0, *$2.string, TType($1), $2.line);
1281         $$.intermAggregate = context->intermediate.makeAggregate(symbol, $2.line);
1282         $$.type = $1;
1283     }
1284     | fully_specified_type IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
1285         TType type = TType($1);
1286         int size;
1287         if (context->arraySizeErrorCheck($2.line, $4, size))
1288             context->recover();
1289         type.setArraySize(size);
1290         TIntermSymbol* symbol = context->intermediate.addSymbol(0, *$2.string, type, $2.line);
1291         $$.intermAggregate = context->intermediate.makeAggregate(symbol, $2.line);
1292         
1293         if (context->structQualifierErrorCheck($2.line, $1))
1294             context->recover();
1295
1296         if (context->nonInitConstErrorCheck($2.line, *$2.string, $1))
1297             context->recover();
1298
1299         $$.type = $1;
1300
1301         if (context->arrayTypeErrorCheck($3.line, $1) || context->arrayQualifierErrorCheck($3.line, $1))
1302             context->recover();
1303         else {
1304             int size;
1305             if (context->arraySizeErrorCheck($3.line, $4, size))
1306                 context->recover();
1307
1308             $1.setArray(true, size);
1309             TVariable* variable = 0;
1310             if (context->arrayErrorCheck($3.line, *$2.string, $1, variable))
1311                 context->recover();
1312             if (variable && symbol)
1313                 symbol->setId(variable->getUniqueId());
1314         }
1315     }
1316     | fully_specified_type IDENTIFIER EQUAL initializer {
1317         if (context->structQualifierErrorCheck($2.line, $1))
1318             context->recover();
1319
1320         $$.type = $1;
1321
1322         TIntermNode* intermNode;
1323         if (!context->executeInitializer($2.line, *$2.string, $1, $4, intermNode)) {
1324         //
1325         // Build intermediate representation
1326         //
1327             if(intermNode)
1328                 $$.intermAggregate = context->intermediate.makeAggregate(intermNode, $3.line);
1329             else
1330                 $$.intermAggregate = 0;
1331         } else {
1332             context->recover();
1333             $$.intermAggregate = 0;
1334         }
1335     }
1336     | INVARIANT IDENTIFIER {
1337         VERTEX_ONLY("invariant declaration", $1.line);
1338         $$.qualifier = EvqInvariantVaryingOut;
1339         $$.intermAggregate = 0;
1340     }
1341
1342 //
1343 // Place holder for the pack/unpack languages.
1344 //
1345 //    | buffer_specifier {
1346 //        $$.intermAggregate = 0;
1347 //    }
1348     ;
1349
1350 // Grammar Note:  No 'enum', or 'typedef'.
1351
1352 //
1353 // Place holder for the pack/unpack languages.
1354 //
1355 //%type <interm> buffer_declaration
1356 //%type <interm.type> buffer_specifier input_or_output buffer_declaration_list
1357 //buffer_specifier
1358 //    : input_or_output LEFT_BRACE buffer_declaration_list RIGHT_BRACE {
1359 //    }
1360 //    ;
1361 //
1362 //input_or_output
1363 //    : INPUT {
1364 //        if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "input"))
1365 //            context->recover();
1366 //        UNPACK_ONLY("input", $1.line);
1367 //        $$.qualifier = EvqInput;
1368 //    }
1369 //    | OUTPUT {
1370 //        if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "output"))
1371 //            context->recover();
1372 //        PACK_ONLY("output", $1.line);
1373 //        $$.qualifier = EvqOutput;
1374 //    }
1375 //    ;
1376
1377 //
1378 // Place holder for the pack/unpack languages.
1379 //
1380 //buffer_declaration_list
1381 //    : buffer_declaration {
1382 //    }
1383 //    | buffer_declaration_list buffer_declaration {
1384 //    }
1385 //    ;
1386
1387 //
1388 // Input/output semantics:
1389 //   float must be 16 or 32 bits
1390 //   float alignment restrictions?
1391 //   check for only one input and only one output
1392 //   sum of bitfields has to be multiple of 32
1393 //
1394
1395 //
1396 // Place holder for the pack/unpack languages.
1397 //
1398 //buffer_declaration
1399 //    : type_specifier IDENTIFIER COLON constant_expression SEMICOLON {
1400 //        if (context->reservedErrorCheck($2.line, *$2.string, context))
1401 //            context->recover();
1402 //        $$.variable = new TVariable($2.string, $1);
1403 //        if (! context->symbolTable.insert(*$$.variable)) {
1404 //            context->error($2.line, "redefinition", $$.variable->getName().c_str(), "");
1405 //            context->recover();
1406 //            // don't have to delete $$.variable, the pool pop will take care of it
1407 //        }
1408 //    }
1409 //    ;
1410
1411 fully_specified_type
1412     : type_specifier {
1413         $$ = $1;
1414
1415         if ($1.array) {
1416             context->error($1.line, "not supported", "first-class array", "");
1417             context->recover();
1418             $1.setArray(false);
1419         }
1420     }
1421     | type_qualifier type_specifier  {
1422         if ($2.array) {
1423             context->error($2.line, "not supported", "first-class array", "");
1424             context->recover();
1425             $2.setArray(false);
1426         }
1427
1428         if ($1.qualifier == EvqAttribute &&
1429             ($2.type == EbtBool || $2.type == EbtInt)) {
1430             context->error($2.line, "cannot be bool or int", getQualifierString($1.qualifier), "");
1431             context->recover();
1432         }
1433         if (($1.qualifier == EvqVaryingIn || $1.qualifier == EvqVaryingOut) &&
1434             ($2.type == EbtBool || $2.type == EbtInt)) {
1435             context->error($2.line, "cannot be bool or int", getQualifierString($1.qualifier), "");
1436             context->recover();
1437         }
1438         $$ = $2;
1439         $$.qualifier = $1.qualifier;
1440     }
1441     ;
1442
1443 type_qualifier
1444     : CONST_QUAL {
1445         $$.setBasic(EbtVoid, EvqConst, $1.line);
1446     }
1447     | ATTRIBUTE {
1448         VERTEX_ONLY("attribute", $1.line);
1449         if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "attribute"))
1450             context->recover();
1451         $$.setBasic(EbtVoid, EvqAttribute, $1.line);
1452     }
1453     | VARYING {
1454         if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "varying"))
1455             context->recover();
1456         if (context->shaderType == SH_VERTEX_SHADER)
1457             $$.setBasic(EbtVoid, EvqVaryingOut, $1.line);
1458         else
1459             $$.setBasic(EbtVoid, EvqVaryingIn, $1.line);
1460     }
1461     | INVARIANT VARYING {
1462         if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "invariant varying"))
1463             context->recover();
1464         if (context->shaderType == SH_VERTEX_SHADER)
1465             $$.setBasic(EbtVoid, EvqInvariantVaryingOut, $1.line);
1466         else
1467             $$.setBasic(EbtVoid, EvqInvariantVaryingIn, $1.line);
1468     }
1469     | UNIFORM {
1470         if (context->globalErrorCheck($1.line, context->symbolTable.atGlobalLevel(), "uniform"))
1471             context->recover();
1472         $$.setBasic(EbtVoid, EvqUniform, $1.line);
1473     }
1474     ;
1475
1476 type_specifier
1477     : type_specifier_no_prec {
1478         $$ = $1;
1479
1480         if ($$.precision == EbpUndefined) {
1481             $$.precision = context->symbolTable.getDefaultPrecision($1.type);
1482             if (context->precisionErrorCheck($1.line, $$.precision, $1.type)) {
1483                 context->recover();
1484             }
1485         }
1486     }
1487     | precision_qualifier type_specifier_no_prec {
1488         $$ = $2;
1489         $$.precision = $1;
1490     }
1491     ;
1492
1493 precision_qualifier
1494     : HIGH_PRECISION {
1495         $$ = EbpHigh;
1496     }
1497     | MEDIUM_PRECISION {
1498         $$ = EbpMedium;
1499     }
1500     | LOW_PRECISION  {
1501         $$ = EbpLow;
1502     }
1503     ;
1504
1505 type_specifier_no_prec
1506     : type_specifier_nonarray {
1507         $$ = $1;
1508     }
1509     | type_specifier_nonarray LEFT_BRACKET constant_expression RIGHT_BRACKET {
1510         $$ = $1;
1511
1512         if (context->arrayTypeErrorCheck($2.line, $1))
1513             context->recover();
1514         else {
1515             int size;
1516             if (context->arraySizeErrorCheck($2.line, $3, size))
1517                 context->recover();
1518             $$.setArray(true, size);
1519         }
1520     }
1521     ;
1522
1523 type_specifier_nonarray
1524     : VOID_TYPE {
1525         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1526         $$.setBasic(EbtVoid, qual, $1.line);
1527     }
1528     | FLOAT_TYPE {
1529         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1530         $$.setBasic(EbtFloat, qual, $1.line);
1531     }
1532     | INT_TYPE {
1533         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1534         $$.setBasic(EbtInt, qual, $1.line);
1535     }
1536     | BOOL_TYPE {
1537         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1538         $$.setBasic(EbtBool, qual, $1.line);
1539     }
1540 //    | UNSIGNED INT_TYPE {
1541 //        PACK_UNPACK_ONLY("unsigned", $1.line);
1542 //        TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1543 //        $$.setBasic(EbtInt, qual, $1.line);
1544 //    }
1545     | VEC2 {
1546         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1547         $$.setBasic(EbtFloat, qual, $1.line);
1548         $$.setAggregate(2);
1549     }
1550     | VEC3 {
1551         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1552         $$.setBasic(EbtFloat, qual, $1.line);
1553         $$.setAggregate(3);
1554     }
1555     | VEC4 {
1556         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1557         $$.setBasic(EbtFloat, qual, $1.line);
1558         $$.setAggregate(4);
1559     }
1560     | BVEC2 {
1561         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1562         $$.setBasic(EbtBool, qual, $1.line);
1563         $$.setAggregate(2);
1564     }
1565     | BVEC3 {
1566         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1567         $$.setBasic(EbtBool, qual, $1.line);
1568         $$.setAggregate(3);
1569     }
1570     | BVEC4 {
1571         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1572         $$.setBasic(EbtBool, qual, $1.line);
1573         $$.setAggregate(4);
1574     }
1575     | IVEC2 {
1576         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1577         $$.setBasic(EbtInt, qual, $1.line);
1578         $$.setAggregate(2);
1579     }
1580     | IVEC3 {
1581         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1582         $$.setBasic(EbtInt, qual, $1.line);
1583         $$.setAggregate(3);
1584     }
1585     | IVEC4 {
1586         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1587         $$.setBasic(EbtInt, qual, $1.line);
1588         $$.setAggregate(4);
1589     }
1590     | MATRIX2 {
1591         FRAG_VERT_ONLY("mat2", $1.line);
1592         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1593         $$.setBasic(EbtFloat, qual, $1.line);
1594         $$.setAggregate(2, true);
1595     }
1596     | MATRIX3 {
1597         FRAG_VERT_ONLY("mat3", $1.line);
1598         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1599         $$.setBasic(EbtFloat, qual, $1.line);
1600         $$.setAggregate(3, true);
1601     }
1602     | MATRIX4 {
1603         FRAG_VERT_ONLY("mat4", $1.line);
1604         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1605         $$.setBasic(EbtFloat, qual, $1.line);
1606         $$.setAggregate(4, true);
1607     }
1608     | SAMPLER2D {
1609         FRAG_VERT_ONLY("sampler2D", $1.line);
1610         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1611         $$.setBasic(EbtSampler2D, qual, $1.line);
1612     }
1613     | SAMPLERCUBE {
1614         FRAG_VERT_ONLY("samplerCube", $1.line);
1615         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1616         $$.setBasic(EbtSamplerCube, qual, $1.line);
1617     }
1618     | struct_specifier {
1619         FRAG_VERT_ONLY("struct", $1.line);
1620         $$ = $1;
1621         $$.qualifier = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1622     }
1623     | TYPE_NAME {
1624         //
1625         // This is for user defined type names.  The lexical phase looked up the
1626         // type.
1627         //
1628         TType& structure = static_cast<TVariable*>($1.symbol)->getType();
1629         TQualifier qual = context->symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary;
1630         $$.setBasic(EbtStruct, qual, $1.line);
1631         $$.userDef = &structure;
1632     }
1633     ;
1634
1635 struct_specifier
1636     : STRUCT IDENTIFIER LEFT_BRACE struct_declaration_list RIGHT_BRACE {
1637         if (context->reservedErrorCheck($2.line, *$2.string))
1638             context->recover();
1639
1640         TType* structure = new TType($4, *$2.string);
1641         TVariable* userTypeDef = new TVariable($2.string, *structure, true);
1642         if (! context->symbolTable.insert(*userTypeDef)) {
1643             context->error($2.line, "redefinition", $2.string->c_str(), "struct");
1644             context->recover();
1645         }
1646         $$.setBasic(EbtStruct, EvqTemporary, $1.line);
1647         $$.userDef = structure;
1648     }
1649     | STRUCT LEFT_BRACE struct_declaration_list RIGHT_BRACE {
1650         TType* structure = new TType($3, TString(""));
1651         $$.setBasic(EbtStruct, EvqTemporary, $1.line);
1652         $$.userDef = structure;
1653     }
1654     ;
1655
1656 struct_declaration_list
1657     : struct_declaration {
1658         $$ = $1;
1659     }
1660     | struct_declaration_list struct_declaration {
1661         $$ = $1;
1662         for (unsigned int i = 0; i < $2->size(); ++i) {
1663             for (unsigned int j = 0; j < $$->size(); ++j) {
1664                 if ((*$$)[j].type->getFieldName() == (*$2)[i].type->getFieldName()) {
1665                     context->error((*$2)[i].line, "duplicate field name in structure:", "struct", (*$2)[i].type->getFieldName().c_str());
1666                     context->recover();
1667                 }
1668             }
1669             $$->push_back((*$2)[i]);
1670         }
1671     }
1672     ;
1673
1674 struct_declaration
1675     : type_specifier struct_declarator_list SEMICOLON {
1676         $$ = $2;
1677
1678         if (context->voidErrorCheck($1.line, (*$2)[0].type->getFieldName(), $1)) {
1679             context->recover();
1680         }
1681         for (unsigned int i = 0; i < $$->size(); ++i) {
1682             //
1683             // Careful not to replace already known aspects of type, like array-ness
1684             //
1685             TType* type = (*$$)[i].type;
1686             type->setBasicType($1.type);
1687             type->setNominalSize($1.size);
1688             type->setMatrix($1.matrix);
1689             type->setPrecision($1.precision);
1690
1691             // don't allow arrays of arrays
1692             if (type->isArray()) {
1693                 if (context->arrayTypeErrorCheck($1.line, $1))
1694                     context->recover();
1695             }
1696             if ($1.array)
1697                 type->setArraySize($1.arraySize);
1698             if ($1.userDef) {
1699                 type->setStruct($1.userDef->getStruct());
1700                 type->setTypeName($1.userDef->getTypeName());
1701             }
1702         }
1703     }
1704     ;
1705
1706 struct_declarator_list
1707     : struct_declarator {
1708         $$ = NewPoolTTypeList();
1709         $$->push_back($1);
1710     }
1711     | struct_declarator_list COMMA struct_declarator {
1712         $$->push_back($3);
1713     }
1714     ;
1715
1716 struct_declarator
1717     : IDENTIFIER {
1718         if (context->reservedErrorCheck($1.line, *$1.string))
1719             context->recover();
1720
1721         $$.type = new TType(EbtVoid, EbpUndefined);
1722         $$.line = $1.line;
1723         $$.type->setFieldName(*$1.string);
1724     }
1725     | IDENTIFIER LEFT_BRACKET constant_expression RIGHT_BRACKET {
1726         if (context->reservedErrorCheck($1.line, *$1.string))
1727             context->recover();
1728
1729         $$.type = new TType(EbtVoid, EbpUndefined);
1730         $$.line = $1.line;
1731         $$.type->setFieldName(*$1.string);
1732
1733         int size;
1734         if (context->arraySizeErrorCheck($2.line, $3, size))
1735             context->recover();
1736         $$.type->setArraySize(size);
1737     }
1738     ;
1739
1740 initializer
1741     : assignment_expression { $$ = $1; }
1742     ;
1743
1744 declaration_statement
1745     : declaration { $$ = $1; }
1746     ;
1747
1748 statement
1749     : compound_statement  { $$ = $1; }
1750     | simple_statement    { $$ = $1; }
1751     ;
1752
1753 // Grammar Note:  No labeled statements; 'goto' is not supported.
1754
1755 simple_statement
1756     : declaration_statement { $$ = $1; }
1757     | expression_statement  { $$ = $1; }
1758     | selection_statement   { $$ = $1; }
1759     | iteration_statement   { $$ = $1; }
1760     | jump_statement        { $$ = $1; }
1761     ;
1762
1763 compound_statement
1764     : LEFT_BRACE RIGHT_BRACE { $$ = 0; }
1765     | LEFT_BRACE { context->symbolTable.push(); } statement_list { context->symbolTable.pop(); } RIGHT_BRACE {
1766         if ($3 != 0) {
1767             $3->setOp(EOpSequence);
1768             $3->setEndLine($5.line);
1769         }
1770         $$ = $3;
1771     }
1772     ;
1773
1774 statement_no_new_scope
1775     : compound_statement_no_new_scope { $$ = $1; }
1776     | simple_statement                { $$ = $1; }
1777     ;
1778
1779 compound_statement_no_new_scope
1780     // Statement that doesn't create a new scope, for selection_statement, iteration_statement
1781     : LEFT_BRACE RIGHT_BRACE {
1782         $$ = 0;
1783     }
1784     | LEFT_BRACE statement_list RIGHT_BRACE {
1785         if ($2) {
1786             $2->setOp(EOpSequence);
1787             $2->setEndLine($3.line);
1788         }
1789         $$ = $2;
1790     }
1791     ;
1792
1793 statement_list
1794     : statement {
1795         $$ = context->intermediate.makeAggregate($1, 0);
1796     }
1797     | statement_list statement {
1798         $$ = context->intermediate.growAggregate($1, $2, 0);
1799     }
1800     ;
1801
1802 expression_statement
1803     : SEMICOLON  { $$ = 0; }
1804     | expression SEMICOLON  { $$ = static_cast<TIntermNode*>($1); }
1805     ;
1806
1807 selection_statement
1808     : IF LEFT_PAREN expression RIGHT_PAREN selection_rest_statement {
1809         if (context->boolErrorCheck($1.line, $3))
1810             context->recover();
1811         $$ = context->intermediate.addSelection($3, $5, $1.line);
1812     }
1813     ;
1814
1815 selection_rest_statement
1816     : statement ELSE statement {
1817         $$.node1 = $1;
1818         $$.node2 = $3;
1819     }
1820     | statement {
1821         $$.node1 = $1;
1822         $$.node2 = 0;
1823     }
1824     ;
1825
1826 // Grammar Note:  No 'switch'.  Switch statements not supported.
1827
1828 condition
1829     // In 1996 c++ draft, conditions can include single declarations
1830     : expression {
1831         $$ = $1;
1832         if (context->boolErrorCheck($1->getLine(), $1))
1833             context->recover();
1834     }
1835     | fully_specified_type IDENTIFIER EQUAL initializer {
1836         TIntermNode* intermNode;
1837         if (context->structQualifierErrorCheck($2.line, $1))
1838             context->recover();
1839         if (context->boolErrorCheck($2.line, $1))
1840             context->recover();
1841
1842         if (!context->executeInitializer($2.line, *$2.string, $1, $4, intermNode))
1843             $$ = $4;
1844         else {
1845             context->recover();
1846             $$ = 0;
1847         }
1848     }
1849     ;
1850
1851 iteration_statement
1852     : WHILE LEFT_PAREN { context->symbolTable.push(); ++context->loopNestingLevel; } condition RIGHT_PAREN statement_no_new_scope {
1853         context->symbolTable.pop();
1854         $$ = context->intermediate.addLoop(ELoopWhile, 0, $4, 0, $6, $1.line);
1855         --context->loopNestingLevel;
1856     }
1857     | DO { ++context->loopNestingLevel; } statement WHILE LEFT_PAREN expression RIGHT_PAREN SEMICOLON {
1858         if (context->boolErrorCheck($8.line, $6))
1859             context->recover();
1860
1861         $$ = context->intermediate.addLoop(ELoopDoWhile, 0, $6, 0, $3, $4.line);
1862         --context->loopNestingLevel;
1863     }
1864     | FOR LEFT_PAREN { context->symbolTable.push(); ++context->loopNestingLevel; } for_init_statement for_rest_statement RIGHT_PAREN statement_no_new_scope {
1865         context->symbolTable.pop();
1866         $$ = context->intermediate.addLoop(ELoopFor, $4, reinterpret_cast<TIntermTyped*>($5.node1), reinterpret_cast<TIntermTyped*>($5.node2), $7, $1.line);
1867         --context->loopNestingLevel;
1868     }
1869     ;
1870
1871 for_init_statement
1872     : expression_statement {
1873         $$ = $1;
1874     }
1875     | declaration_statement {
1876         $$ = $1;
1877     }
1878     ;
1879
1880 conditionopt
1881     : condition {
1882         $$ = $1;
1883     }
1884     | /* May be null */ {
1885         $$ = 0;
1886     }
1887     ;
1888
1889 for_rest_statement
1890     : conditionopt SEMICOLON {
1891         $$.node1 = $1;
1892         $$.node2 = 0;
1893     }
1894     | conditionopt SEMICOLON expression  {
1895         $$.node1 = $1;
1896         $$.node2 = $3;
1897     }
1898     ;
1899
1900 jump_statement
1901     : CONTINUE SEMICOLON {
1902         if (context->loopNestingLevel <= 0) {
1903             context->error($1.line, "continue statement only allowed in loops", "", "");
1904             context->recover();
1905         }
1906         $$ = context->intermediate.addBranch(EOpContinue, $1.line);
1907     }
1908     | BREAK SEMICOLON {
1909         if (context->loopNestingLevel <= 0) {
1910             context->error($1.line, "break statement only allowed in loops", "", "");
1911             context->recover();
1912         }
1913         $$ = context->intermediate.addBranch(EOpBreak, $1.line);
1914     }
1915     | RETURN SEMICOLON {
1916         $$ = context->intermediate.addBranch(EOpReturn, $1.line);
1917         if (context->currentFunctionType->getBasicType() != EbtVoid) {
1918             context->error($1.line, "non-void function must return a value", "return", "");
1919             context->recover();
1920         }
1921     }
1922     | RETURN expression SEMICOLON {
1923         $$ = context->intermediate.addBranch(EOpReturn, $2, $1.line);
1924         context->functionReturnsValue = true;
1925         if (context->currentFunctionType->getBasicType() == EbtVoid) {
1926             context->error($1.line, "void function cannot return a value", "return", "");
1927             context->recover();
1928         } else if (*(context->currentFunctionType) != $2->getType()) {
1929             context->error($1.line, "function return is not matching type:", "return", "");
1930             context->recover();
1931         }
1932     }
1933     | DISCARD SEMICOLON {
1934         FRAG_ONLY("discard", $1.line);
1935         $$ = context->intermediate.addBranch(EOpKill, $1.line);
1936     }
1937     ;
1938
1939 // Grammar Note:  No 'goto'.  Gotos are not supported.
1940
1941 translation_unit
1942     : external_declaration {
1943         $$ = $1;
1944         context->treeRoot = $$;
1945     }
1946     | translation_unit external_declaration {
1947         $$ = context->intermediate.growAggregate($1, $2, 0);
1948         context->treeRoot = $$;
1949     }
1950     ;
1951
1952 external_declaration
1953     : function_definition {
1954         $$ = $1;
1955     }
1956     | declaration {
1957         $$ = $1;
1958     }
1959     ;
1960
1961 function_definition
1962     : function_prototype {
1963         TFunction* function = $1.function;
1964         TFunction* prevDec = static_cast<TFunction*>(context->symbolTable.find(function->getMangledName()));
1965         //
1966         // Note:  'prevDec' could be 'function' if this is the first time we've seen function
1967         // as it would have just been put in the symbol table.  Otherwise, we're looking up
1968         // an earlier occurance.
1969         //
1970         if (prevDec->isDefined()) {
1971             //
1972             // Then this function already has a body.
1973             //
1974             context->error($1.line, "function already has a body", function->getName().c_str(), "");
1975             context->recover();
1976         }
1977         prevDec->setDefined();
1978
1979         //
1980         // Raise error message if main function takes any parameters or return anything other than void
1981         //
1982         if (function->getName() == "main") {
1983             if (function->getParamCount() > 0) {
1984                 context->error($1.line, "function cannot take any parameter(s)", function->getName().c_str(), "");
1985                 context->recover();
1986             }
1987             if (function->getReturnType().getBasicType() != EbtVoid) {
1988                 context->error($1.line, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1989                 context->recover();
1990             }
1991         }
1992
1993         //
1994         // New symbol table scope for body of function plus its arguments
1995         //
1996         context->symbolTable.push();
1997
1998         //
1999         // Remember the return type for later checking for RETURN statements.
2000         //
2001         context->currentFunctionType = &(prevDec->getReturnType());
2002         context->functionReturnsValue = false;
2003
2004         //
2005         // Insert parameters into the symbol table.
2006         // If the parameter has no name, it's not an error, just don't insert it
2007         // (could be used for unused args).
2008         //
2009         // Also, accumulate the list of parameters into the HIL, so lower level code
2010         // knows where to find parameters.
2011         //
2012         TIntermAggregate* paramNodes = new TIntermAggregate;
2013         for (int i = 0; i < function->getParamCount(); i++) {
2014             const TParameter& param = function->getParam(i);
2015             if (param.name != 0) {
2016                 TVariable *variable = new TVariable(param.name, *param.type);
2017                 //
2018                 // Insert the parameters with name in the symbol table.
2019                 //
2020                 if (! context->symbolTable.insert(*variable)) {
2021                     context->error($1.line, "redefinition", variable->getName().c_str(), "");
2022                     context->recover();
2023                     delete variable;
2024                 }
2025
2026                 //
2027                 // Add the parameter to the HIL
2028                 //
2029                 paramNodes = context->intermediate.growAggregate(
2030                                                paramNodes,
2031                                                context->intermediate.addSymbol(variable->getUniqueId(),
2032                                                                        variable->getName(),
2033                                                                        variable->getType(), $1.line),
2034                                                $1.line);
2035             } else {
2036                 paramNodes = context->intermediate.growAggregate(paramNodes, context->intermediate.addSymbol(0, "", *param.type, $1.line), $1.line);
2037             }
2038         }
2039         context->intermediate.setAggregateOperator(paramNodes, EOpParameters, $1.line);
2040         $1.intermAggregate = paramNodes;
2041         context->loopNestingLevel = 0;
2042     }
2043     compound_statement_no_new_scope {
2044         //?? Check that all paths return a value if return type != void ?
2045         //   May be best done as post process phase on intermediate code
2046         if (context->currentFunctionType->getBasicType() != EbtVoid && ! context->functionReturnsValue) {
2047             context->error($1.line, "function does not return a value:", "", $1.function->getName().c_str());
2048             context->recover();
2049         }
2050         context->symbolTable.pop();
2051         $$ = context->intermediate.growAggregate($1.intermAggregate, $3, 0);
2052         context->intermediate.setAggregateOperator($$, EOpFunction, $1.line);
2053         $$->getAsAggregate()->setName($1.function->getMangledName().c_str());
2054         $$->getAsAggregate()->setType($1.function->getReturnType());
2055
2056         // store the pragma information for debug and optimize and other vendor specific
2057         // information. This information can be queried from the parse tree
2058         $$->getAsAggregate()->setOptimize(context->contextPragma.optimize);
2059         $$->getAsAggregate()->setDebug(context->contextPragma.debug);
2060         $$->getAsAggregate()->addToPragmaTable(context->contextPragma.pragmaTable);
2061
2062         if ($3 && $3->getAsAggregate())
2063             $$->getAsAggregate()->setEndLine($3->getAsAggregate()->getEndLine());
2064     }
2065     ;
2066
2067 %%
2068
2069 int glslang_parse(TParseContext* context) {
2070     return yyparse(context);
2071 }
2072