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