scons: Fix scons build.
[profile/ivi/mesa.git] / src / glsl / glsl_parser.yy
1 %{
2 /*
3  * Copyright © 2008, 2009 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28     
29 #include "ast.h"
30 #include "glsl_parser_extras.h"
31 #include "glsl_types.h"
32
33 #define YYLEX_PARAM state->scanner
34
35 #undef yyerror
36
37 static void yyerror(YYLTYPE *loc, _mesa_glsl_parse_state *st, const char *msg)
38 {
39    _mesa_glsl_error(loc, st, "%s", msg);
40 }
41 %}
42
43 %pure-parser
44 %error-verbose
45
46 %locations
47 %initial-action {
48    @$.first_line = 1;
49    @$.first_column = 1;
50    @$.last_line = 1;
51    @$.last_column = 1;
52    @$.source = 0;
53 }
54
55 %lex-param   {void *scanner}
56 %parse-param {struct _mesa_glsl_parse_state *state}
57
58 %union {
59    int n;
60    float real;
61    const char *identifier;
62
63    struct ast_type_qualifier type_qualifier;
64
65    ast_node *node;
66    ast_type_specifier *type_specifier;
67    ast_fully_specified_type *fully_specified_type;
68    ast_function *function;
69    ast_parameter_declarator *parameter_declarator;
70    ast_function_definition *function_definition;
71    ast_compound_statement *compound_statement;
72    ast_expression *expression;
73    ast_declarator_list *declarator_list;
74    ast_struct_specifier *struct_specifier;
75    ast_declaration *declaration;
76    ast_switch_body *switch_body;
77    ast_case_label *case_label;
78    ast_case_label_list *case_label_list;
79    ast_case_statement *case_statement;
80    ast_case_statement_list *case_statement_list;
81
82    struct {
83       ast_node *cond;
84       ast_expression *rest;
85    } for_rest_statement;
86
87    struct {
88       ast_node *then_statement;
89       ast_node *else_statement;
90    } selection_rest_statement;
91 }
92
93 %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK
94 %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
95 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
96 %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING
97 %token NOPERSPECTIVE FLAT SMOOTH
98 %token MAT2X2 MAT2X3 MAT2X4
99 %token MAT3X2 MAT3X3 MAT3X4
100 %token MAT4X2 MAT4X3 MAT4X4
101 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
102 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
103 %token SAMPLER2DARRAYSHADOW ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
104 %token ISAMPLER1DARRAY ISAMPLER2DARRAY USAMPLER1D USAMPLER2D USAMPLER3D
105 %token USAMPLERCUBE USAMPLER1DARRAY USAMPLER2DARRAY
106 %token SAMPLER2DRECT ISAMPLER2DRECT USAMPLER2DRECT SAMPLER2DRECTSHADOW
107 %token SAMPLERBUFFER ISAMPLERBUFFER USAMPLERBUFFER
108 %token SAMPLEREXTERNALOES
109 %token STRUCT VOID_TOK WHILE
110 %token <identifier> IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
111 %type <identifier> any_identifier
112 %token <real> FLOATCONSTANT
113 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
114 %token <identifier> FIELD_SELECTION
115 %token LEFT_OP RIGHT_OP
116 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
117 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
118 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
119 %token SUB_ASSIGN
120 %token INVARIANT
121 %token LOWP MEDIUMP HIGHP SUPERP PRECISION
122
123 %token VERSION_TOK EXTENSION LINE COLON EOL INTERFACE OUTPUT
124 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
125 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
126 %token PRAGMA_INVARIANT_ALL
127 %token LAYOUT_TOK
128
129    /* Reserved words that are not actually used in the grammar.
130     */
131 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
132 %token INLINE_TOK NOINLINE VOLATILE PUBLIC_TOK STATIC EXTERN EXTERNAL
133 %token LONG_TOK SHORT_TOK DOUBLE_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK OUPTUT
134 %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
135 %token SAMPLER3DRECT
136 %token SIZEOF CAST NAMESPACE USING
137
138 %token ERROR_TOK
139
140 %token COMMON PARTITION ACTIVE FILTER
141 %token  IMAGE1D  IMAGE2D  IMAGE3D  IMAGECUBE  IMAGE1DARRAY  IMAGE2DARRAY
142 %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGECUBE IIMAGE1DARRAY IIMAGE2DARRAY
143 %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGECUBE UIMAGE1DARRAY UIMAGE2DARRAY
144 %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGEBUFFER IIMAGEBUFFER UIMAGEBUFFER
145 %token IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW
146 %token ROW_MAJOR
147
148 %type <identifier> variable_identifier
149 %type <node> statement
150 %type <node> statement_list
151 %type <node> simple_statement
152 %type <n> precision_qualifier
153 %type <type_qualifier> type_qualifier
154 %type <type_qualifier> storage_qualifier
155 %type <type_qualifier> interpolation_qualifier
156 %type <type_qualifier> layout_qualifier
157 %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
158 %type <type_specifier> type_specifier
159 %type <type_specifier> type_specifier_no_prec
160 %type <type_specifier> type_specifier_nonarray
161 %type <identifier> basic_type_specifier_nonarray
162 %type <fully_specified_type> fully_specified_type
163 %type <function> function_prototype
164 %type <function> function_header
165 %type <function> function_header_with_parameters
166 %type <function> function_declarator
167 %type <parameter_declarator> parameter_declarator
168 %type <parameter_declarator> parameter_declaration
169 %type <type_qualifier> parameter_qualifier
170 %type <type_qualifier> parameter_type_qualifier
171 %type <type_specifier> parameter_type_specifier
172 %type <function_definition> function_definition
173 %type <compound_statement> compound_statement_no_new_scope
174 %type <compound_statement> compound_statement
175 %type <node> statement_no_new_scope
176 %type <node> expression_statement
177 %type <expression> expression
178 %type <expression> primary_expression
179 %type <expression> assignment_expression
180 %type <expression> conditional_expression
181 %type <expression> logical_or_expression
182 %type <expression> logical_xor_expression
183 %type <expression> logical_and_expression
184 %type <expression> inclusive_or_expression
185 %type <expression> exclusive_or_expression
186 %type <expression> and_expression
187 %type <expression> equality_expression
188 %type <expression> relational_expression
189 %type <expression> shift_expression
190 %type <expression> additive_expression
191 %type <expression> multiplicative_expression
192 %type <expression> unary_expression
193 %type <expression> constant_expression
194 %type <expression> integer_expression
195 %type <expression> postfix_expression
196 %type <expression> function_call_header_with_parameters
197 %type <expression> function_call_header_no_parameters
198 %type <expression> function_call_header
199 %type <expression> function_call_generic
200 %type <expression> function_call_or_method
201 %type <expression> function_call
202 %type <expression> method_call_generic
203 %type <expression> method_call_header_with_parameters
204 %type <expression> method_call_header_no_parameters
205 %type <expression> method_call_header
206 %type <n> assignment_operator
207 %type <n> unary_operator
208 %type <expression> function_identifier
209 %type <node> external_declaration
210 %type <declarator_list> init_declarator_list
211 %type <declarator_list> single_declaration
212 %type <expression> initializer
213 %type <node> declaration
214 %type <node> declaration_statement
215 %type <node> jump_statement
216 %type <struct_specifier> struct_specifier
217 %type <node> struct_declaration_list
218 %type <declarator_list> struct_declaration
219 %type <declaration> struct_declarator
220 %type <declaration> struct_declarator_list
221 %type <node> selection_statement
222 %type <selection_rest_statement> selection_rest_statement
223 %type <node> switch_statement
224 %type <switch_body> switch_body
225 %type <case_label_list> case_label_list
226 %type <case_label> case_label
227 %type <case_statement> case_statement
228 %type <case_statement_list> case_statement_list
229 %type <node> iteration_statement
230 %type <node> condition
231 %type <node> conditionopt
232 %type <node> for_init_statement
233 %type <for_rest_statement> for_rest_statement
234 %%
235
236 translation_unit: 
237         version_statement extension_statement_list
238         {
239            _mesa_glsl_initialize_types(state);
240         }
241         external_declaration_list
242         {
243            delete state->symbols;
244            state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
245            _mesa_glsl_initialize_types(state);
246         }
247         ;
248
249 version_statement:
250         /* blank - no #version specified: defaults are already set */
251         | VERSION_TOK INTCONSTANT EOL
252         {
253            bool supported = false;
254
255            switch ($2) {
256            case 100:
257               state->es_shader = true;
258               supported = state->Const.GLSL_100ES;
259               break;
260            case 110:
261               supported = state->Const.GLSL_110;
262               break;
263            case 120:
264               supported = state->Const.GLSL_120;
265               break;
266            case 130:
267               supported = state->Const.GLSL_130;
268               break;
269            case 140:
270               supported = state->Const.GLSL_140;
271               break;
272            default:
273               supported = false;
274               break;
275            }
276
277            state->language_version = $2;
278            state->version_string =
279               ralloc_asprintf(state, "GLSL%s %d.%02d",
280                               state->es_shader ? " ES" : "",
281                               state->language_version / 100,
282                               state->language_version % 100);
283
284            if (!supported) {
285               _mesa_glsl_error(& @2, state, "%s is not supported. "
286                                "Supported versions are: %s\n",
287                                state->version_string,
288                                state->supported_version_string);
289            }
290         }
291         ;
292
293 pragma_statement:
294         PRAGMA_DEBUG_ON EOL
295         | PRAGMA_DEBUG_OFF EOL
296         | PRAGMA_OPTIMIZE_ON EOL
297         | PRAGMA_OPTIMIZE_OFF EOL
298         | PRAGMA_INVARIANT_ALL EOL
299         {
300            if (state->language_version < 120) {
301               _mesa_glsl_warning(& @1, state,
302                                  "pragma `invariant(all)' not supported in %s",
303                                  state->version_string);
304            } else {
305               state->all_invariant = true;
306            }
307         }
308         ;
309
310 extension_statement_list:
311
312         | extension_statement_list extension_statement
313         ;
314
315 any_identifier:
316         IDENTIFIER
317         | TYPE_IDENTIFIER
318         | NEW_IDENTIFIER
319         ;
320
321 extension_statement:
322         EXTENSION any_identifier COLON any_identifier EOL
323         {
324            if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
325               YYERROR;
326            }
327         }
328         ;
329
330 external_declaration_list:
331         external_declaration
332         {
333            /* FINISHME: The NULL test is required because pragmas are set to
334             * FINISHME: NULL. (See production rule for external_declaration.)
335             */
336            if ($1 != NULL)
337               state->translation_unit.push_tail(& $1->link);
338         }
339         | external_declaration_list external_declaration
340         {
341            /* FINISHME: The NULL test is required because pragmas are set to
342             * FINISHME: NULL. (See production rule for external_declaration.)
343             */
344            if ($2 != NULL)
345               state->translation_unit.push_tail(& $2->link);
346         }
347         ;
348
349 variable_identifier:
350         IDENTIFIER
351         | NEW_IDENTIFIER
352         ;
353
354 primary_expression:
355         variable_identifier
356         {
357            void *ctx = state;
358            $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
359            $$->set_location(yylloc);
360            $$->primary_expression.identifier = $1;
361         }
362         | INTCONSTANT
363         {
364            void *ctx = state;
365            $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
366            $$->set_location(yylloc);
367            $$->primary_expression.int_constant = $1;
368         }
369         | UINTCONSTANT
370         {
371            void *ctx = state;
372            $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
373            $$->set_location(yylloc);
374            $$->primary_expression.uint_constant = $1;
375         }
376         | FLOATCONSTANT
377         {
378            void *ctx = state;
379            $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
380            $$->set_location(yylloc);
381            $$->primary_expression.float_constant = $1;
382         }
383         | BOOLCONSTANT
384         {
385            void *ctx = state;
386            $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
387            $$->set_location(yylloc);
388            $$->primary_expression.bool_constant = $1;
389         }
390         | '(' expression ')'
391         {
392            $$ = $2;
393         }
394         ;
395
396 postfix_expression:
397         primary_expression
398         | postfix_expression '[' integer_expression ']'
399         {
400            void *ctx = state;
401            $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
402            $$->set_location(yylloc);
403         }
404         | function_call
405         {
406            $$ = $1;
407         }
408         | postfix_expression '.' any_identifier
409         {
410            void *ctx = state;
411            $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
412            $$->set_location(yylloc);
413            $$->primary_expression.identifier = $3;
414         }
415         | postfix_expression INC_OP
416         {
417            void *ctx = state;
418            $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
419            $$->set_location(yylloc);
420         }
421         | postfix_expression DEC_OP
422         {
423            void *ctx = state;
424            $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
425            $$->set_location(yylloc);
426         }
427         ;
428
429 integer_expression:
430         expression
431         ;
432
433 function_call:
434         function_call_or_method
435         ;
436
437 function_call_or_method:
438         function_call_generic
439         | postfix_expression '.' method_call_generic
440         {
441            void *ctx = state;
442            $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
443            $$->set_location(yylloc);
444         }
445         ;
446
447 function_call_generic:
448         function_call_header_with_parameters ')'
449         | function_call_header_no_parameters ')'
450         ;
451
452 function_call_header_no_parameters:
453         function_call_header VOID_TOK
454         | function_call_header
455         ;
456
457 function_call_header_with_parameters:
458         function_call_header assignment_expression
459         {
460            $$ = $1;
461            $$->set_location(yylloc);
462            $$->expressions.push_tail(& $2->link);
463         }
464         | function_call_header_with_parameters ',' assignment_expression
465         {
466            $$ = $1;
467            $$->set_location(yylloc);
468            $$->expressions.push_tail(& $3->link);
469         }
470         ;
471
472         // Grammar Note: Constructors look like functions, but lexical 
473         // analysis recognized most of them as keywords. They are now
474         // recognized through "type_specifier".
475 function_call_header:
476         function_identifier '('
477         ;
478
479 function_identifier:
480         type_specifier
481         {
482            void *ctx = state;
483            $$ = new(ctx) ast_function_expression($1);
484            $$->set_location(yylloc);
485         }
486         | variable_identifier
487         {
488            void *ctx = state;
489            ast_expression *callee = new(ctx) ast_expression($1);
490            $$ = new(ctx) ast_function_expression(callee);
491            $$->set_location(yylloc);
492         }
493         | FIELD_SELECTION
494         {
495            void *ctx = state;
496            ast_expression *callee = new(ctx) ast_expression($1);
497            $$ = new(ctx) ast_function_expression(callee);
498            $$->set_location(yylloc);
499         }
500         ;
501
502 method_call_generic:
503         method_call_header_with_parameters ')'
504         | method_call_header_no_parameters ')'
505         ;
506
507 method_call_header_no_parameters:
508         method_call_header VOID_TOK
509         | method_call_header
510         ;
511
512 method_call_header_with_parameters:
513         method_call_header assignment_expression
514         {
515            $$ = $1;
516            $$->set_location(yylloc);
517            $$->expressions.push_tail(& $2->link);
518         }
519         | method_call_header_with_parameters ',' assignment_expression
520         {
521            $$ = $1;
522            $$->set_location(yylloc);
523            $$->expressions.push_tail(& $3->link);
524         }
525         ;
526
527         // Grammar Note: Constructors look like methods, but lexical 
528         // analysis recognized most of them as keywords. They are now
529         // recognized through "type_specifier".
530 method_call_header:
531         variable_identifier '('
532         {
533            void *ctx = state;
534            ast_expression *callee = new(ctx) ast_expression($1);
535            $$ = new(ctx) ast_function_expression(callee);
536            $$->set_location(yylloc);
537         }
538         ;
539
540         // Grammar Note: No traditional style type casts.
541 unary_expression:
542         postfix_expression
543         | INC_OP unary_expression
544         {
545            void *ctx = state;
546            $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
547            $$->set_location(yylloc);
548         }
549         | DEC_OP unary_expression
550         {
551            void *ctx = state;
552            $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
553            $$->set_location(yylloc);
554         }
555         | unary_operator unary_expression
556         {
557            void *ctx = state;
558            $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
559            $$->set_location(yylloc);
560         }
561         ;
562
563         // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
564 unary_operator:
565         '+'     { $$ = ast_plus; }
566         | '-'   { $$ = ast_neg; }
567         | '!'   { $$ = ast_logic_not; }
568         | '~'   { $$ = ast_bit_not; }
569         ;
570
571 multiplicative_expression:
572         unary_expression
573         | multiplicative_expression '*' unary_expression
574         {
575            void *ctx = state;
576            $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
577            $$->set_location(yylloc);
578         }
579         | multiplicative_expression '/' unary_expression
580         {
581            void *ctx = state;
582            $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
583            $$->set_location(yylloc);
584         }
585         | multiplicative_expression '%' unary_expression
586         {
587            void *ctx = state;
588            $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
589            $$->set_location(yylloc);
590         }
591         ;
592
593 additive_expression:
594         multiplicative_expression
595         | additive_expression '+' multiplicative_expression
596         {
597            void *ctx = state;
598            $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
599            $$->set_location(yylloc);
600         }
601         | additive_expression '-' multiplicative_expression
602         {
603            void *ctx = state;
604            $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
605            $$->set_location(yylloc);
606         }
607         ;
608
609 shift_expression:
610         additive_expression
611         | shift_expression LEFT_OP additive_expression
612         {
613            void *ctx = state;
614            $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
615            $$->set_location(yylloc);
616         }
617         | shift_expression RIGHT_OP additive_expression
618         {
619            void *ctx = state;
620            $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
621            $$->set_location(yylloc);
622         }
623         ;
624
625 relational_expression:
626         shift_expression
627         | relational_expression '<' shift_expression
628         {
629            void *ctx = state;
630            $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
631            $$->set_location(yylloc);
632         }
633         | relational_expression '>' shift_expression
634         {
635            void *ctx = state;
636            $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
637            $$->set_location(yylloc);
638         }
639         | relational_expression LE_OP shift_expression
640         {
641            void *ctx = state;
642            $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
643            $$->set_location(yylloc);
644         }
645         | relational_expression GE_OP shift_expression
646         {
647            void *ctx = state;
648            $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
649            $$->set_location(yylloc);
650         }
651         ;
652
653 equality_expression:
654         relational_expression
655         | equality_expression EQ_OP relational_expression
656         {
657            void *ctx = state;
658            $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
659            $$->set_location(yylloc);
660         }
661         | equality_expression NE_OP relational_expression
662         {
663            void *ctx = state;
664            $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
665            $$->set_location(yylloc);
666         }
667         ;
668
669 and_expression:
670         equality_expression
671         | and_expression '&' equality_expression
672         {
673            void *ctx = state;
674            $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
675            $$->set_location(yylloc);
676         }
677         ;
678
679 exclusive_or_expression:
680         and_expression
681         | exclusive_or_expression '^' and_expression
682         {
683            void *ctx = state;
684            $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
685            $$->set_location(yylloc);
686         }
687         ;
688
689 inclusive_or_expression:
690         exclusive_or_expression
691         | inclusive_or_expression '|' exclusive_or_expression
692         {
693            void *ctx = state;
694            $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
695            $$->set_location(yylloc);
696         }
697         ;
698
699 logical_and_expression:
700         inclusive_or_expression
701         | logical_and_expression AND_OP inclusive_or_expression
702         {
703            void *ctx = state;
704            $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
705            $$->set_location(yylloc);
706         }
707         ;
708
709 logical_xor_expression:
710         logical_and_expression
711         | logical_xor_expression XOR_OP logical_and_expression
712         {
713            void *ctx = state;
714            $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
715            $$->set_location(yylloc);
716         }
717         ;
718
719 logical_or_expression:
720         logical_xor_expression
721         | logical_or_expression OR_OP logical_xor_expression
722         {
723            void *ctx = state;
724            $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
725            $$->set_location(yylloc);
726         }
727         ;
728
729 conditional_expression:
730         logical_or_expression
731         | logical_or_expression '?' expression ':' assignment_expression
732         {
733            void *ctx = state;
734            $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
735            $$->set_location(yylloc);
736         }
737         ;
738
739 assignment_expression:
740         conditional_expression
741         | unary_expression assignment_operator assignment_expression
742         {
743            void *ctx = state;
744            $$ = new(ctx) ast_expression($2, $1, $3, NULL);
745            $$->set_location(yylloc);
746         }
747         ;
748
749 assignment_operator:
750         '='             { $$ = ast_assign; }
751         | MUL_ASSIGN    { $$ = ast_mul_assign; }
752         | DIV_ASSIGN    { $$ = ast_div_assign; }
753         | MOD_ASSIGN    { $$ = ast_mod_assign; }
754         | ADD_ASSIGN    { $$ = ast_add_assign; }
755         | SUB_ASSIGN    { $$ = ast_sub_assign; }
756         | LEFT_ASSIGN   { $$ = ast_ls_assign; }
757         | RIGHT_ASSIGN  { $$ = ast_rs_assign; }
758         | AND_ASSIGN    { $$ = ast_and_assign; }
759         | XOR_ASSIGN    { $$ = ast_xor_assign; }
760         | OR_ASSIGN     { $$ = ast_or_assign; }
761         ;
762
763 expression:
764         assignment_expression
765         {
766            $$ = $1;
767         }
768         | expression ',' assignment_expression
769         {
770            void *ctx = state;
771            if ($1->oper != ast_sequence) {
772               $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
773               $$->set_location(yylloc);
774               $$->expressions.push_tail(& $1->link);
775            } else {
776               $$ = $1;
777            }
778
779            $$->expressions.push_tail(& $3->link);
780         }
781         ;
782
783 constant_expression:
784         conditional_expression
785         ;
786
787 declaration:
788         function_prototype ';'
789         {
790            state->symbols->pop_scope();
791            $$ = $1;
792         }
793         | init_declarator_list ';'
794         {
795            $$ = $1;
796         }
797         | PRECISION precision_qualifier type_specifier_no_prec ';'
798         {
799            $3->precision = $2;
800            $3->is_precision_statement = true;
801            $$ = $3;
802         }
803         ;
804
805 function_prototype:
806         function_declarator ')'
807         ;
808
809 function_declarator:
810         function_header
811         | function_header_with_parameters
812         ;
813
814 function_header_with_parameters:
815         function_header parameter_declaration
816         {
817            $$ = $1;
818            $$->parameters.push_tail(& $2->link);
819         }
820         | function_header_with_parameters ',' parameter_declaration
821         {
822            $$ = $1;
823            $$->parameters.push_tail(& $3->link);
824         }
825         ;
826
827 function_header:
828         fully_specified_type variable_identifier '('
829         {
830            void *ctx = state;
831            $$ = new(ctx) ast_function();
832            $$->set_location(yylloc);
833            $$->return_type = $1;
834            $$->identifier = $2;
835
836            state->symbols->add_function(new(state) ir_function($2));
837            state->symbols->push_scope();
838         }
839         ;
840
841 parameter_declarator:
842         type_specifier any_identifier
843         {
844            void *ctx = state;
845            $$ = new(ctx) ast_parameter_declarator();
846            $$->set_location(yylloc);
847            $$->type = new(ctx) ast_fully_specified_type();
848            $$->type->set_location(yylloc);
849            $$->type->specifier = $1;
850            $$->identifier = $2;
851         }
852         | type_specifier any_identifier '[' constant_expression ']'
853         {
854            void *ctx = state;
855            $$ = new(ctx) ast_parameter_declarator();
856            $$->set_location(yylloc);
857            $$->type = new(ctx) ast_fully_specified_type();
858            $$->type->set_location(yylloc);
859            $$->type->specifier = $1;
860            $$->identifier = $2;
861            $$->is_array = true;
862            $$->array_size = $4;
863         }
864         ;
865
866 parameter_declaration:
867         parameter_type_qualifier parameter_qualifier parameter_declarator
868         {
869            $1.flags.i |= $2.flags.i;
870
871            $$ = $3;
872            $$->type->qualifier = $1;
873         }
874         | parameter_qualifier parameter_declarator
875         {
876            $$ = $2;
877            $$->type->qualifier = $1;
878         }
879         | parameter_type_qualifier parameter_qualifier parameter_type_specifier
880         {
881            void *ctx = state;
882            $1.flags.i |= $2.flags.i;
883
884            $$ = new(ctx) ast_parameter_declarator();
885            $$->set_location(yylloc);
886            $$->type = new(ctx) ast_fully_specified_type();
887            $$->type->qualifier = $1;
888            $$->type->specifier = $3;
889         }
890         | parameter_qualifier parameter_type_specifier
891         {
892            void *ctx = state;
893            $$ = new(ctx) ast_parameter_declarator();
894            $$->set_location(yylloc);
895            $$->type = new(ctx) ast_fully_specified_type();
896            $$->type->qualifier = $1;
897            $$->type->specifier = $2;
898         }
899         ;
900
901 parameter_qualifier:
902         /* empty */
903         {
904            memset(& $$, 0, sizeof($$));
905         }
906         | IN_TOK
907         {
908            memset(& $$, 0, sizeof($$));
909            $$.flags.q.in = 1;
910         }
911         | OUT_TOK
912         {
913            memset(& $$, 0, sizeof($$));
914            $$.flags.q.out = 1;
915         }
916         | INOUT_TOK
917         {
918            memset(& $$, 0, sizeof($$));
919            $$.flags.q.in = 1;
920            $$.flags.q.out = 1;
921         }
922         ;
923
924 parameter_type_specifier:
925         type_specifier
926         ;
927
928 init_declarator_list:
929         single_declaration
930         | init_declarator_list ',' any_identifier
931         {
932            void *ctx = state;
933            ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
934            decl->set_location(yylloc);
935
936            $$ = $1;
937            $$->declarations.push_tail(&decl->link);
938            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
939         }
940         | init_declarator_list ',' any_identifier '[' ']'
941         {
942            void *ctx = state;
943            ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
944            decl->set_location(yylloc);
945
946            $$ = $1;
947            $$->declarations.push_tail(&decl->link);
948            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
949         }
950         | init_declarator_list ',' any_identifier '[' constant_expression ']'
951         {
952            void *ctx = state;
953            ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
954            decl->set_location(yylloc);
955
956            $$ = $1;
957            $$->declarations.push_tail(&decl->link);
958            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
959         }
960         | init_declarator_list ',' any_identifier '[' ']' '=' initializer
961         {
962            void *ctx = state;
963            ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7);
964            decl->set_location(yylloc);
965
966            $$ = $1;
967            $$->declarations.push_tail(&decl->link);
968            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
969         }
970         | init_declarator_list ',' any_identifier '[' constant_expression ']' '=' initializer
971         {
972            void *ctx = state;
973            ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
974            decl->set_location(yylloc);
975
976            $$ = $1;
977            $$->declarations.push_tail(&decl->link);
978            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
979         }
980         | init_declarator_list ',' any_identifier '=' initializer
981         {
982            void *ctx = state;
983            ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
984            decl->set_location(yylloc);
985
986            $$ = $1;
987            $$->declarations.push_tail(&decl->link);
988            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
989         }
990         ;
991
992         // Grammar Note: No 'enum', or 'typedef'.
993 single_declaration:
994         fully_specified_type
995         {
996            void *ctx = state;
997            /* Empty declaration list is valid. */
998            $$ = new(ctx) ast_declarator_list($1);
999            $$->set_location(yylloc);
1000         }
1001         | fully_specified_type any_identifier
1002         {
1003            void *ctx = state;
1004            ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1005
1006            $$ = new(ctx) ast_declarator_list($1);
1007            $$->set_location(yylloc);
1008            $$->declarations.push_tail(&decl->link);
1009         }
1010         | fully_specified_type any_identifier '[' ']'
1011         {
1012            void *ctx = state;
1013            ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
1014
1015            $$ = new(ctx) ast_declarator_list($1);
1016            $$->set_location(yylloc);
1017            $$->declarations.push_tail(&decl->link);
1018         }
1019         | fully_specified_type any_identifier '[' constant_expression ']'
1020         {
1021            void *ctx = state;
1022            ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
1023
1024            $$ = new(ctx) ast_declarator_list($1);
1025            $$->set_location(yylloc);
1026            $$->declarations.push_tail(&decl->link);
1027         }
1028         | fully_specified_type any_identifier '[' ']' '=' initializer
1029         {
1030            void *ctx = state;
1031            ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6);
1032
1033            $$ = new(ctx) ast_declarator_list($1);
1034            $$->set_location(yylloc);
1035            $$->declarations.push_tail(&decl->link);
1036         }
1037         | fully_specified_type any_identifier '[' constant_expression ']' '=' initializer
1038         {
1039            void *ctx = state;
1040            ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
1041
1042            $$ = new(ctx) ast_declarator_list($1);
1043            $$->set_location(yylloc);
1044            $$->declarations.push_tail(&decl->link);
1045         }
1046         | fully_specified_type any_identifier '=' initializer
1047         {
1048            void *ctx = state;
1049            ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1050
1051            $$ = new(ctx) ast_declarator_list($1);
1052            $$->set_location(yylloc);
1053            $$->declarations.push_tail(&decl->link);
1054         }
1055         | INVARIANT variable_identifier // Vertex only.
1056         {
1057            void *ctx = state;
1058            ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1059
1060            $$ = new(ctx) ast_declarator_list(NULL);
1061            $$->set_location(yylloc);
1062            $$->invariant = true;
1063
1064            $$->declarations.push_tail(&decl->link);
1065         }
1066         ;
1067
1068 fully_specified_type:
1069         type_specifier
1070         {
1071            void *ctx = state;
1072            $$ = new(ctx) ast_fully_specified_type();
1073            $$->set_location(yylloc);
1074            $$->specifier = $1;
1075         }
1076         | type_qualifier type_specifier
1077         {
1078            void *ctx = state;
1079            $$ = new(ctx) ast_fully_specified_type();
1080            $$->set_location(yylloc);
1081            $$->qualifier = $1;
1082            $$->specifier = $2;
1083         }
1084         ;
1085
1086 layout_qualifier:
1087         LAYOUT_TOK '(' layout_qualifier_id_list ')'
1088         {
1089           $$ = $3;
1090         }
1091         ;
1092
1093 layout_qualifier_id_list:
1094         layout_qualifier_id
1095         | layout_qualifier_id_list ',' layout_qualifier_id
1096         {
1097            if (($1.flags.i & $3.flags.i) != 0) {
1098               _mesa_glsl_error(& @3, state,
1099                                "duplicate layout qualifiers used\n");
1100               YYERROR;
1101            }
1102
1103            $$.flags.i = $1.flags.i | $3.flags.i;
1104
1105            if ($1.flags.q.explicit_location)
1106               $$.location = $1.location;
1107
1108            if ($1.flags.q.explicit_index)
1109               $$.index = $1.index;
1110
1111            if ($3.flags.q.explicit_location)
1112               $$.location = $3.location;
1113
1114            if ($3.flags.q.explicit_index)
1115               $$.index = $3.index;
1116         }
1117         ;
1118
1119 layout_qualifier_id:
1120         any_identifier
1121         {
1122            bool got_one = false;
1123
1124            memset(& $$, 0, sizeof($$));
1125
1126            /* Layout qualifiers for ARB_fragment_coord_conventions. */
1127            if (!got_one && state->ARB_fragment_coord_conventions_enable) {
1128               if (strcmp($1, "origin_upper_left") == 0) {
1129                  got_one = true;
1130                  $$.flags.q.origin_upper_left = 1;
1131               } else if (strcmp($1, "pixel_center_integer") == 0) {
1132                  got_one = true;
1133                  $$.flags.q.pixel_center_integer = 1;
1134               }
1135
1136               if (got_one && state->ARB_fragment_coord_conventions_warn) {
1137                  _mesa_glsl_warning(& @1, state,
1138                                     "GL_ARB_fragment_coord_conventions layout "
1139                                     "identifier `%s' used\n", $1);
1140               }
1141            }
1142
1143            /* Layout qualifiers for AMD/ARB_conservative_depth. */
1144            if (!got_one &&
1145                (state->AMD_conservative_depth_enable ||
1146                 state->ARB_conservative_depth_enable)) {
1147               if (strcmp($1, "depth_any") == 0) {
1148                  got_one = true;
1149                  $$.flags.q.depth_any = 1;
1150               } else if (strcmp($1, "depth_greater") == 0) {
1151                  got_one = true;
1152                  $$.flags.q.depth_greater = 1;
1153               } else if (strcmp($1, "depth_less") == 0) {
1154                  got_one = true;
1155                  $$.flags.q.depth_less = 1;
1156               } else if (strcmp($1, "depth_unchanged") == 0) {
1157                  got_one = true;
1158                  $$.flags.q.depth_unchanged = 1;
1159               }
1160         
1161               if (got_one && state->AMD_conservative_depth_warn) {
1162                  _mesa_glsl_warning(& @1, state,
1163                                     "GL_AMD_conservative_depth "
1164                                     "layout qualifier `%s' is used\n", $1);
1165               }
1166               if (got_one && state->ARB_conservative_depth_warn) {
1167                  _mesa_glsl_warning(& @1, state,
1168                                     "GL_ARB_conservative_depth "
1169                                     "layout qualifier `%s' is used\n", $1);
1170               }
1171            }
1172
1173            if (!got_one) {
1174               _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1175                                "`%s'\n", $1);
1176               YYERROR;
1177            }
1178         }
1179         | any_identifier '=' INTCONSTANT
1180         {
1181            bool got_one = false;
1182
1183            memset(& $$, 0, sizeof($$));
1184
1185            if (state->ARB_explicit_attrib_location_enable) {
1186               /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1187                * FINISHME: GLSL 1.30 (or later) are supported.
1188                */
1189               if (strcmp("location", $1) == 0) {
1190                  got_one = true;
1191
1192                  $$.flags.q.explicit_location = 1;
1193
1194                  if ($3 >= 0) {
1195                     $$.location = $3;
1196                  } else {
1197                     _mesa_glsl_error(& @3, state,
1198                                      "invalid location %d specified\n", $3);
1199                     YYERROR;
1200                  }
1201               }
1202
1203               if (strcmp("index", $1) == 0) {
1204                  got_one = true;
1205
1206                  $$.flags.q.explicit_index = 1;
1207
1208                  if ($3 >= 0) {
1209                     $$.index = $3;
1210                  } else {
1211                     _mesa_glsl_error(& @3, state,
1212                                      "invalid index %d specified\n", $3);
1213                     YYERROR;
1214                  }
1215               }
1216            }
1217
1218            /* If the identifier didn't match any known layout identifiers,
1219             * emit an error.
1220             */
1221            if (!got_one) {
1222               _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1223                                "`%s'\n", $1);
1224               YYERROR;
1225            } else if (state->ARB_explicit_attrib_location_warn) {
1226               _mesa_glsl_warning(& @1, state,
1227                                  "GL_ARB_explicit_attrib_location layout "
1228                                  "identifier `%s' used\n", $1);
1229            }
1230         }
1231         ;
1232
1233 interpolation_qualifier:
1234         SMOOTH
1235         {
1236            memset(& $$, 0, sizeof($$));
1237            $$.flags.q.smooth = 1;
1238         }
1239         | FLAT
1240         {
1241            memset(& $$, 0, sizeof($$));
1242            $$.flags.q.flat = 1;
1243         }
1244         | NOPERSPECTIVE
1245         {
1246            memset(& $$, 0, sizeof($$));
1247            $$.flags.q.noperspective = 1;
1248         }
1249         ;
1250
1251 parameter_type_qualifier:
1252         CONST_TOK
1253         {
1254            memset(& $$, 0, sizeof($$));
1255            $$.flags.q.constant = 1;
1256         }
1257         ;
1258
1259 type_qualifier:
1260         storage_qualifier
1261         | layout_qualifier
1262         | layout_qualifier storage_qualifier
1263         {
1264            $$ = $1;
1265            $$.flags.i |= $2.flags.i;
1266         }
1267         | interpolation_qualifier
1268         | interpolation_qualifier storage_qualifier
1269         {
1270            $$ = $1;
1271            $$.flags.i |= $2.flags.i;
1272         }
1273         | INVARIANT storage_qualifier
1274         {
1275            $$ = $2;
1276            $$.flags.q.invariant = 1;
1277         }
1278         | INVARIANT interpolation_qualifier storage_qualifier
1279         {
1280            $$ = $2;
1281            $$.flags.i |= $3.flags.i;
1282            $$.flags.q.invariant = 1;
1283         }
1284         | INVARIANT
1285         {
1286            memset(& $$, 0, sizeof($$));
1287            $$.flags.q.invariant = 1;
1288         }
1289         ;
1290
1291 storage_qualifier:
1292         CONST_TOK
1293         {
1294            memset(& $$, 0, sizeof($$));
1295            $$.flags.q.constant = 1;
1296         }
1297         | ATTRIBUTE
1298         {
1299            memset(& $$, 0, sizeof($$));
1300            $$.flags.q.attribute = 1;
1301         }
1302         | VARYING
1303         {
1304            memset(& $$, 0, sizeof($$));
1305            $$.flags.q.varying = 1;
1306         }
1307         | CENTROID VARYING
1308         {
1309            memset(& $$, 0, sizeof($$));
1310            $$.flags.q.centroid = 1;
1311            $$.flags.q.varying = 1;
1312         }
1313         | IN_TOK
1314         {
1315            memset(& $$, 0, sizeof($$));
1316            $$.flags.q.in = 1;
1317         }
1318         | OUT_TOK
1319         {
1320            memset(& $$, 0, sizeof($$));
1321            $$.flags.q.out = 1;
1322         }
1323         | CENTROID IN_TOK
1324         {
1325            memset(& $$, 0, sizeof($$));
1326            $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1327         }
1328         | CENTROID OUT_TOK
1329         {
1330            memset(& $$, 0, sizeof($$));
1331            $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1332         }
1333         | UNIFORM
1334         {
1335            memset(& $$, 0, sizeof($$));
1336            $$.flags.q.uniform = 1;
1337         }
1338         ;
1339
1340 type_specifier:
1341         type_specifier_no_prec
1342         {
1343            $$ = $1;
1344         }
1345         | precision_qualifier type_specifier_no_prec
1346         {
1347            $$ = $2;
1348            $$->precision = $1;
1349         }
1350         ;
1351
1352 type_specifier_no_prec:
1353         type_specifier_nonarray
1354         | type_specifier_nonarray '[' ']'
1355         {
1356            $$ = $1;
1357            $$->is_array = true;
1358            $$->array_size = NULL;
1359         }
1360         | type_specifier_nonarray '[' constant_expression ']'
1361         {
1362            $$ = $1;
1363            $$->is_array = true;
1364            $$->array_size = $3;
1365         }
1366         ;
1367
1368 type_specifier_nonarray:
1369         basic_type_specifier_nonarray
1370         {
1371            void *ctx = state;
1372            $$ = new(ctx) ast_type_specifier($1);
1373            $$->set_location(yylloc);
1374         }
1375         | struct_specifier
1376         {
1377            void *ctx = state;
1378            $$ = new(ctx) ast_type_specifier($1);
1379            $$->set_location(yylloc);
1380         }
1381         | TYPE_IDENTIFIER
1382         {
1383            void *ctx = state;
1384            $$ = new(ctx) ast_type_specifier($1);
1385            $$->set_location(yylloc);
1386         }
1387         ;
1388
1389 basic_type_specifier_nonarray:
1390         VOID_TOK                { $$ = "void"; }
1391         | FLOAT_TOK             { $$ = "float"; }
1392         | INT_TOK               { $$ = "int"; }
1393         | UINT_TOK              { $$ = "uint"; }
1394         | BOOL_TOK              { $$ = "bool"; }
1395         | VEC2                  { $$ = "vec2"; }
1396         | VEC3                  { $$ = "vec3"; }
1397         | VEC4                  { $$ = "vec4"; }
1398         | BVEC2                 { $$ = "bvec2"; }
1399         | BVEC3                 { $$ = "bvec3"; }
1400         | BVEC4                 { $$ = "bvec4"; }
1401         | IVEC2                 { $$ = "ivec2"; }
1402         | IVEC3                 { $$ = "ivec3"; }
1403         | IVEC4                 { $$ = "ivec4"; }
1404         | UVEC2                 { $$ = "uvec2"; }
1405         | UVEC3                 { $$ = "uvec3"; }
1406         | UVEC4                 { $$ = "uvec4"; }
1407         | MAT2X2                { $$ = "mat2"; }
1408         | MAT2X3                { $$ = "mat2x3"; }
1409         | MAT2X4                { $$ = "mat2x4"; }
1410         | MAT3X2                { $$ = "mat3x2"; }
1411         | MAT3X3                { $$ = "mat3"; }
1412         | MAT3X4                { $$ = "mat3x4"; }
1413         | MAT4X2                { $$ = "mat4x2"; }
1414         | MAT4X3                { $$ = "mat4x3"; }
1415         | MAT4X4                { $$ = "mat4"; }
1416         | SAMPLER1D             { $$ = "sampler1D"; }
1417         | SAMPLER2D             { $$ = "sampler2D"; }
1418         | SAMPLER2DRECT         { $$ = "sampler2DRect"; }
1419         | SAMPLER3D             { $$ = "sampler3D"; }
1420         | SAMPLERCUBE           { $$ = "samplerCube"; }
1421         | SAMPLEREXTERNALOES    { $$ = "samplerExternalOES"; }
1422         | SAMPLER1DSHADOW       { $$ = "sampler1DShadow"; }
1423         | SAMPLER2DSHADOW       { $$ = "sampler2DShadow"; }
1424         | SAMPLER2DRECTSHADOW   { $$ = "sampler2DRectShadow"; }
1425         | SAMPLERCUBESHADOW     { $$ = "samplerCubeShadow"; }
1426         | SAMPLER1DARRAY        { $$ = "sampler1DArray"; }
1427         | SAMPLER2DARRAY        { $$ = "sampler2DArray"; }
1428         | SAMPLER1DARRAYSHADOW  { $$ = "sampler1DArrayShadow"; }
1429         | SAMPLER2DARRAYSHADOW  { $$ = "sampler2DArrayShadow"; }
1430         | SAMPLERBUFFER         { $$ = "samplerBuffer"; }
1431         | ISAMPLER1D            { $$ = "isampler1D"; }
1432         | ISAMPLER2D            { $$ = "isampler2D"; }
1433         | ISAMPLER2DRECT        { $$ = "isampler2DRect"; }
1434         | ISAMPLER3D            { $$ = "isampler3D"; }
1435         | ISAMPLERCUBE          { $$ = "isamplerCube"; }
1436         | ISAMPLER1DARRAY       { $$ = "isampler1DArray"; }
1437         | ISAMPLER2DARRAY       { $$ = "isampler2DArray"; }
1438         | ISAMPLERBUFFER        { $$ = "isamplerBuffer"; }
1439         | USAMPLER1D            { $$ = "usampler1D"; }
1440         | USAMPLER2D            { $$ = "usampler2D"; }
1441         | USAMPLER2DRECT        { $$ = "usampler2DRect"; }
1442         | USAMPLER3D            { $$ = "usampler3D"; }
1443         | USAMPLERCUBE          { $$ = "usamplerCube"; }
1444         | USAMPLER1DARRAY       { $$ = "usampler1DArray"; }
1445         | USAMPLER2DARRAY       { $$ = "usampler2DArray"; }
1446         | USAMPLERBUFFER        { $$ = "usamplerBuffer"; }
1447         ;
1448
1449 precision_qualifier:
1450         HIGHP     {
1451                      if (!state->es_shader && state->language_version < 130)
1452                         _mesa_glsl_error(& @1, state,
1453                                          "precision qualifier forbidden "
1454                                          "in %s (1.30 or later "
1455                                          "required)\n",
1456                                          state->version_string);
1457
1458                      $$ = ast_precision_high;
1459                   }
1460         | MEDIUMP {
1461                      if (!state->es_shader && state->language_version < 130)
1462                         _mesa_glsl_error(& @1, state,
1463                                          "precision qualifier forbidden "
1464                                          "in %s (1.30 or later "
1465                                          "required)\n",
1466                                          state->version_string);
1467
1468                      $$ = ast_precision_medium;
1469                   }
1470         | LOWP    {
1471                      if (!state->es_shader && state->language_version < 130)
1472                         _mesa_glsl_error(& @1, state,
1473                                          "precision qualifier forbidden "
1474                                          "in %s (1.30 or later "
1475                                          "required)\n",
1476                                          state->version_string);
1477
1478                      $$ = ast_precision_low;
1479                   }
1480         ;
1481
1482 struct_specifier:
1483         STRUCT any_identifier '{' struct_declaration_list '}'
1484         {
1485            void *ctx = state;
1486            $$ = new(ctx) ast_struct_specifier($2, $4);
1487            $$->set_location(yylloc);
1488            state->symbols->add_type($2, glsl_type::void_type);
1489         }
1490         | STRUCT '{' struct_declaration_list '}'
1491         {
1492            void *ctx = state;
1493            $$ = new(ctx) ast_struct_specifier(NULL, $3);
1494            $$->set_location(yylloc);
1495         }
1496         ;
1497
1498 struct_declaration_list:
1499         struct_declaration
1500         {
1501            $$ = (ast_node *) $1;
1502            $1->link.self_link();
1503         }
1504         | struct_declaration_list struct_declaration
1505         {
1506            $$ = (ast_node *) $1;
1507            $$->link.insert_before(& $2->link);
1508         }
1509         ;
1510
1511 struct_declaration:
1512         type_specifier struct_declarator_list ';'
1513         {
1514            void *ctx = state;
1515            ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1516            type->set_location(yylloc);
1517
1518            type->specifier = $1;
1519            $$ = new(ctx) ast_declarator_list(type);
1520            $$->set_location(yylloc);
1521
1522            $$->declarations.push_degenerate_list_at_head(& $2->link);
1523         }
1524         ;
1525
1526 struct_declarator_list:
1527         struct_declarator
1528         {
1529            $$ = $1;
1530            $1->link.self_link();
1531         }
1532         | struct_declarator_list ',' struct_declarator
1533         {
1534            $$ = $1;
1535            $$->link.insert_before(& $3->link);
1536         }
1537         ;
1538
1539 struct_declarator:
1540         any_identifier
1541         {
1542            void *ctx = state;
1543            $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1544            $$->set_location(yylloc);
1545            state->symbols->add_variable(new(state) ir_variable(NULL, $1, ir_var_auto));
1546         }
1547         | any_identifier '[' constant_expression ']'
1548         {
1549            void *ctx = state;
1550            $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1551            $$->set_location(yylloc);
1552         }
1553         ;
1554
1555 initializer:
1556         assignment_expression
1557         ;
1558
1559 declaration_statement:
1560         declaration
1561         ;
1562
1563         // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1564         // supported.
1565 statement:
1566         compound_statement      { $$ = (ast_node *) $1; }
1567         | simple_statement
1568         ;
1569
1570 simple_statement:
1571         declaration_statement
1572         | expression_statement
1573         | selection_statement
1574         | switch_statement
1575         | iteration_statement
1576         | jump_statement
1577         ;
1578
1579 compound_statement:
1580         '{' '}'
1581         {
1582            void *ctx = state;
1583            $$ = new(ctx) ast_compound_statement(true, NULL);
1584            $$->set_location(yylloc);
1585         }
1586         | '{'
1587         {
1588            state->symbols->push_scope();
1589         }
1590         statement_list '}'
1591         {
1592            void *ctx = state;
1593            $$ = new(ctx) ast_compound_statement(true, $3);
1594            $$->set_location(yylloc);
1595            state->symbols->pop_scope();
1596         }
1597         ;
1598
1599 statement_no_new_scope:
1600         compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1601         | simple_statement
1602         ;
1603
1604 compound_statement_no_new_scope:
1605         '{' '}'
1606         {
1607            void *ctx = state;
1608            $$ = new(ctx) ast_compound_statement(false, NULL);
1609            $$->set_location(yylloc);
1610         }
1611         | '{' statement_list '}'
1612         {
1613            void *ctx = state;
1614            $$ = new(ctx) ast_compound_statement(false, $2);
1615            $$->set_location(yylloc);
1616         }
1617         ;
1618
1619 statement_list:
1620         statement
1621         {
1622            if ($1 == NULL) {
1623               _mesa_glsl_error(& @1, state, "<nil> statement\n");
1624               assert($1 != NULL);
1625            }
1626
1627            $$ = $1;
1628            $$->link.self_link();
1629         }
1630         | statement_list statement
1631         {
1632            if ($2 == NULL) {
1633               _mesa_glsl_error(& @2, state, "<nil> statement\n");
1634               assert($2 != NULL);
1635            }
1636            $$ = $1;
1637            $$->link.insert_before(& $2->link);
1638         }
1639         ;
1640
1641 expression_statement:
1642         ';'
1643         {
1644            void *ctx = state;
1645            $$ = new(ctx) ast_expression_statement(NULL);
1646            $$->set_location(yylloc);
1647         }
1648         | expression ';'
1649         {
1650            void *ctx = state;
1651            $$ = new(ctx) ast_expression_statement($1);
1652            $$->set_location(yylloc);
1653         }
1654         ;
1655
1656 selection_statement:
1657         IF '(' expression ')' selection_rest_statement
1658         {
1659            $$ = new(state) ast_selection_statement($3, $5.then_statement,
1660                                                    $5.else_statement);
1661            $$->set_location(yylloc);
1662         }
1663         ;
1664
1665 selection_rest_statement:
1666         statement ELSE statement
1667         {
1668            $$.then_statement = $1;
1669            $$.else_statement = $3;
1670         }
1671         | statement
1672         {
1673            $$.then_statement = $1;
1674            $$.else_statement = NULL;
1675         }
1676         ;
1677
1678 condition:
1679         expression
1680         {
1681            $$ = (ast_node *) $1;
1682         }
1683         | fully_specified_type any_identifier '=' initializer
1684         {
1685            void *ctx = state;
1686            ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1687            ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1688            decl->set_location(yylloc);
1689            declarator->set_location(yylloc);
1690
1691            declarator->declarations.push_tail(&decl->link);
1692            $$ = declarator;
1693         }
1694         ;
1695
1696 /*
1697  * siwtch_statement grammar is based on the syntax described in the body
1698  * of the GLSL spec, not in it's appendix!!!
1699  */
1700 switch_statement:
1701         SWITCH '(' expression ')' switch_body
1702         {
1703            $$ = new(state) ast_switch_statement($3, $5);
1704            $$->set_location(yylloc);
1705         }
1706         ;
1707
1708 switch_body:
1709         '{' '}'
1710         {
1711            $$ = new(state) ast_switch_body(NULL);
1712            $$->set_location(yylloc);
1713         }
1714         | '{' case_statement_list '}'
1715         {
1716            $$ = new(state) ast_switch_body($2);
1717            $$->set_location(yylloc);
1718         }
1719         ;
1720
1721 case_label:
1722         CASE expression ':'
1723         {
1724            $$ = new(state) ast_case_label($2);
1725            $$->set_location(yylloc);
1726         }
1727         | DEFAULT ':'
1728         {
1729            $$ = new(state) ast_case_label(NULL);
1730            $$->set_location(yylloc);
1731         }
1732         ;
1733
1734 case_label_list:
1735         case_label
1736         {
1737            ast_case_label_list *labels = new(state) ast_case_label_list();
1738
1739            labels->labels.push_tail(& $1->link);
1740            $$ = labels;
1741            $$->set_location(yylloc);
1742         }
1743         | case_label_list case_label
1744         {
1745            $$ = $1;
1746            $$->labels.push_tail(& $2->link);
1747         }
1748         ;
1749
1750 case_statement:
1751         case_label_list statement
1752         {
1753            ast_case_statement *stmts = new(state) ast_case_statement($1);
1754            stmts->set_location(yylloc);
1755
1756            stmts->stmts.push_tail(& $2->link);
1757            $$ = stmts;
1758         }
1759         | case_statement statement
1760         {
1761            $$ = $1;
1762            $$->stmts.push_tail(& $2->link);
1763         }
1764         ;
1765
1766 case_statement_list:
1767         case_statement
1768         {
1769            ast_case_statement_list *cases= new(state) ast_case_statement_list();
1770            cases->set_location(yylloc);
1771
1772            cases->cases.push_tail(& $1->link);
1773            $$ = cases;
1774         }
1775         | case_statement_list case_statement
1776         {
1777            $$ = $1;
1778            $$->cases.push_tail(& $2->link);
1779         }
1780         ;
1781
1782 iteration_statement:
1783         WHILE '(' condition ')' statement_no_new_scope
1784         {
1785            void *ctx = state;
1786            $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1787                                                     NULL, $3, NULL, $5);
1788            $$->set_location(yylloc);
1789         }
1790         | DO statement WHILE '(' expression ')' ';'
1791         {
1792            void *ctx = state;
1793            $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1794                                                     NULL, $5, NULL, $2);
1795            $$->set_location(yylloc);
1796         }
1797         | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1798         {
1799            void *ctx = state;
1800            $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1801                                                     $3, $4.cond, $4.rest, $6);
1802            $$->set_location(yylloc);
1803         }
1804         ;
1805
1806 for_init_statement:
1807         expression_statement
1808         | declaration_statement
1809         ;
1810
1811 conditionopt:
1812         condition
1813         | /* empty */
1814         {
1815            $$ = NULL;
1816         }
1817         ;
1818
1819 for_rest_statement:
1820         conditionopt ';'
1821         {
1822            $$.cond = $1;
1823            $$.rest = NULL;
1824         }
1825         | conditionopt ';' expression
1826         {
1827            $$.cond = $1;
1828            $$.rest = $3;
1829         }
1830         ;
1831
1832         // Grammar Note: No 'goto'. Gotos are not supported.
1833 jump_statement:
1834         CONTINUE ';' 
1835         {
1836            void *ctx = state;
1837            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1838            $$->set_location(yylloc);
1839         }
1840         | BREAK ';'
1841         {
1842            void *ctx = state;
1843            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1844            $$->set_location(yylloc);
1845         }
1846         | RETURN ';'
1847         {
1848            void *ctx = state;
1849            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1850            $$->set_location(yylloc);
1851         }
1852         | RETURN expression ';'
1853         {
1854            void *ctx = state;
1855            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1856            $$->set_location(yylloc);
1857         }
1858         | DISCARD ';' // Fragment shader only.
1859         {
1860            void *ctx = state;
1861            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1862            $$->set_location(yylloc);
1863         }
1864         ;
1865
1866 external_declaration:
1867         function_definition     { $$ = $1; }
1868         | declaration           { $$ = $1; }
1869         | pragma_statement      { $$ = NULL; }
1870         ;
1871
1872 function_definition:
1873         function_prototype compound_statement_no_new_scope
1874         {
1875            void *ctx = state;
1876            $$ = new(ctx) ast_function_definition();
1877            $$->set_location(yylloc);
1878            $$->prototype = $1;
1879            $$->body = $2;
1880
1881            state->symbols->pop_scope();
1882         }
1883         ;