revert 213 commits (to 56092) from the last month. 10 still need work to resolve...
[AROS.git] / workbench / libs / mesa / src / glsl / glsl_parser.yy
blob2c0498ece7a946a357ffc01d6e40a3b90d2a3faf
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"
33 #define YYLEX_PARAM state->scanner
37 %pure-parser
38 %error-verbose
40 %locations
41 %initial-action {
42    @$.first_line = 1;
43    @$.first_column = 1;
44    @$.last_line = 1;
45    @$.last_column = 1;
46    @$.source = 0;
49 %lex-param   {void *scanner}
50 %parse-param {struct _mesa_glsl_parse_state *state}
52 %union {
53    int n;
54    float real;
55    char *identifier;
57    struct ast_type_qualifier type_qualifier;
59    ast_node *node;
60    ast_type_specifier *type_specifier;
61    ast_fully_specified_type *fully_specified_type;
62    ast_function *function;
63    ast_parameter_declarator *parameter_declarator;
64    ast_function_definition *function_definition;
65    ast_compound_statement *compound_statement;
66    ast_expression *expression;
67    ast_declarator_list *declarator_list;
68    ast_struct_specifier *struct_specifier;
69    ast_declaration *declaration;
71    struct {
72       ast_node *cond;
73       ast_expression *rest;
74    } for_rest_statement;
76    struct {
77       ast_node *then_statement;
78       ast_node *else_statement;
79    } selection_rest_statement;
82 %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK
83 %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
84 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
85 %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING
86 %token NOPERSPECTIVE FLAT SMOOTH
87 %token MAT2X2 MAT2X3 MAT2X4
88 %token MAT3X2 MAT3X3 MAT3X4
89 %token MAT4X2 MAT4X3 MAT4X4
90 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
91 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
92 %token SAMPLER2DARRAYSHADOW ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
93 %token ISAMPLER1DARRAY ISAMPLER2DARRAY USAMPLER1D USAMPLER2D USAMPLER3D
94 %token USAMPLERCUBE USAMPLER1DARRAY USAMPLER2DARRAY
95 %token STRUCT VOID_TOK WHILE
96 %token <identifier> IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
97 %type <identifier> any_identifier
98 %token <real> FLOATCONSTANT
99 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
100 %token <identifier> FIELD_SELECTION
101 %token LEFT_OP RIGHT_OP
102 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
103 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
104 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
105 %token SUB_ASSIGN
106 %token INVARIANT
107 %token LOWP MEDIUMP HIGHP SUPERP PRECISION
109 %token VERSION EXTENSION LINE COLON EOL INTERFACE OUTPUT
110 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
111 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
112 %token PRAGMA_INVARIANT_ALL
113 %token LAYOUT_TOK
115    /* Reserved words that are not actually used in the grammar.
116     */
117 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
118 %token INLINE_TOK NOINLINE VOLATILE PUBLIC_TOK STATIC EXTERN EXTERNAL
119 %token LONG_TOK SHORT_TOK DOUBLE_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK OUPTUT
120 %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
121 %token SAMPLER2DRECT SAMPLER3DRECT SAMPLER2DRECTSHADOW
122 %token SIZEOF CAST NAMESPACE USING
124 %token ERROR_TOK
126 %token COMMON PARTITION ACTIVE SAMPLERBUFFER FILTER
127 %token  IMAGE1D  IMAGE2D  IMAGE3D  IMAGECUBE  IMAGE1DARRAY  IMAGE2DARRAY
128 %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGECUBE IIMAGE1DARRAY IIMAGE2DARRAY
129 %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGECUBE UIMAGE1DARRAY UIMAGE2DARRAY
130 %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGEBUFFER IIMAGEBUFFER UIMAGEBUFFER
131 %token IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW
132 %token ROW_MAJOR
134 %type <identifier> variable_identifier
135 %type <node> statement
136 %type <node> statement_list
137 %type <node> simple_statement
138 %type <n> precision_qualifier
139 %type <type_qualifier> type_qualifier
140 %type <type_qualifier> storage_qualifier
141 %type <type_qualifier> interpolation_qualifier
142 %type <type_qualifier> layout_qualifier
143 %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
144 %type <type_specifier> type_specifier
145 %type <type_specifier> type_specifier_no_prec
146 %type <type_specifier> type_specifier_nonarray
147 %type <n> basic_type_specifier_nonarray
148 %type <fully_specified_type> fully_specified_type
149 %type <function> function_prototype
150 %type <function> function_header
151 %type <function> function_header_with_parameters
152 %type <function> function_declarator
153 %type <parameter_declarator> parameter_declarator
154 %type <parameter_declarator> parameter_declaration
155 %type <type_qualifier> parameter_qualifier
156 %type <type_qualifier> parameter_type_qualifier
157 %type <type_specifier> parameter_type_specifier
158 %type <function_definition> function_definition
159 %type <compound_statement> compound_statement_no_new_scope
160 %type <compound_statement> compound_statement
161 %type <node> statement_no_new_scope
162 %type <node> expression_statement
163 %type <expression> expression
164 %type <expression> primary_expression
165 %type <expression> assignment_expression
166 %type <expression> conditional_expression
167 %type <expression> logical_or_expression
168 %type <expression> logical_xor_expression
169 %type <expression> logical_and_expression
170 %type <expression> inclusive_or_expression
171 %type <expression> exclusive_or_expression
172 %type <expression> and_expression
173 %type <expression> equality_expression
174 %type <expression> relational_expression
175 %type <expression> shift_expression
176 %type <expression> additive_expression
177 %type <expression> multiplicative_expression
178 %type <expression> unary_expression
179 %type <expression> constant_expression
180 %type <expression> integer_expression
181 %type <expression> postfix_expression
182 %type <expression> function_call_header_with_parameters
183 %type <expression> function_call_header_no_parameters
184 %type <expression> function_call_header
185 %type <expression> function_call_generic
186 %type <expression> function_call_or_method
187 %type <expression> function_call
188 %type <expression> method_call_generic
189 %type <expression> method_call_header_with_parameters
190 %type <expression> method_call_header_no_parameters
191 %type <expression> method_call_header
192 %type <n> assignment_operator
193 %type <n> unary_operator
194 %type <expression> function_identifier
195 %type <node> external_declaration
196 %type <declarator_list> init_declarator_list
197 %type <declarator_list> single_declaration
198 %type <expression> initializer
199 %type <node> declaration
200 %type <node> declaration_statement
201 %type <node> jump_statement
202 %type <struct_specifier> struct_specifier
203 %type <node> struct_declaration_list
204 %type <declarator_list> struct_declaration
205 %type <declaration> struct_declarator
206 %type <declaration> struct_declarator_list
207 %type <node> selection_statement
208 %type <selection_rest_statement> selection_rest_statement
209 %type <node> iteration_statement
210 %type <node> condition
211 %type <node> conditionopt
212 %type <node> for_init_statement
213 %type <for_rest_statement> for_rest_statement
216 translation_unit: 
217         version_statement extension_statement_list
218         {
219            _mesa_glsl_initialize_types(state);
220         }
221         external_declaration_list
222         {
223            delete state->symbols;
224            state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
225            _mesa_glsl_initialize_types(state);
226         }
227         ;
229 version_statement:
230         /* blank - no #version specified: defaults are already set */
231         | VERSION INTCONSTANT EOL
232         {
233            bool supported = false;
235            switch ($2) {
236            case 100:
237               state->es_shader = true;
238               supported = state->Const.GLSL_100ES;
239               break;
240            case 110:
241               supported = state->Const.GLSL_110;
242               break;
243            case 120:
244               supported = state->Const.GLSL_120;
245               break;
246            case 130:
247               supported = state->Const.GLSL_130;
248               break;
249            default:
250               supported = false;
251               break;
252            }
254            state->language_version = $2;
255            state->version_string =
256               ralloc_asprintf(state, "GLSL%s %d.%02d",
257                               state->es_shader ? " ES" : "",
258                               state->language_version / 100,
259                               state->language_version % 100);
261            if (!supported) {
262               _mesa_glsl_error(& @2, state, "%s is not supported. "
263                                "Supported versions are: %s\n",
264                                state->version_string,
265                                state->supported_version_string);
266            }
267         }
268         ;
270 pragma_statement:
271         PRAGMA_DEBUG_ON EOL
272         | PRAGMA_DEBUG_OFF EOL
273         | PRAGMA_OPTIMIZE_ON EOL
274         | PRAGMA_OPTIMIZE_OFF EOL
275         | PRAGMA_INVARIANT_ALL EOL
276         {
277            if (state->language_version < 120) {
278               _mesa_glsl_warning(& @1, state,
279                                  "pragma `invariant(all)' not supported in %s",
280                                  state->version_string);
281            } else {
282               state->all_invariant = true;
283            }
284         }
285         ;
287 extension_statement_list:
289         | extension_statement_list extension_statement
290         ;
292 any_identifier:
293         IDENTIFIER
294         | TYPE_IDENTIFIER
295         | NEW_IDENTIFIER
296         ;
298 extension_statement:
299         EXTENSION any_identifier COLON any_identifier EOL
300         {
301            if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
302               YYERROR;
303            }
304         }
305         ;
307 external_declaration_list:
308         external_declaration
309         {
310            /* FINISHME: The NULL test is required because pragmas are set to
311             * FINISHME: NULL. (See production rule for external_declaration.)
312             */
313            if ($1 != NULL)
314               state->translation_unit.push_tail(& $1->link);
315         }
316         | external_declaration_list external_declaration
317         {
318            /* FINISHME: The NULL test is required because pragmas are set to
319             * FINISHME: NULL. (See production rule for external_declaration.)
320             */
321            if ($2 != NULL)
322               state->translation_unit.push_tail(& $2->link);
323         }
324         ;
326 variable_identifier:
327         IDENTIFIER
328         | NEW_IDENTIFIER
329         ;
331 primary_expression:
332         variable_identifier
333         {
334            void *ctx = state;
335            $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
336            $$->set_location(yylloc);
337            $$->primary_expression.identifier = $1;
338         }
339         | INTCONSTANT
340         {
341            void *ctx = state;
342            $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
343            $$->set_location(yylloc);
344            $$->primary_expression.int_constant = $1;
345         }
346         | UINTCONSTANT
347         {
348            void *ctx = state;
349            $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
350            $$->set_location(yylloc);
351            $$->primary_expression.uint_constant = $1;
352         }
353         | FLOATCONSTANT
354         {
355            void *ctx = state;
356            $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
357            $$->set_location(yylloc);
358            $$->primary_expression.float_constant = $1;
359         }
360         | BOOLCONSTANT
361         {
362            void *ctx = state;
363            $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
364            $$->set_location(yylloc);
365            $$->primary_expression.bool_constant = $1;
366         }
367         | '(' expression ')'
368         {
369            $$ = $2;
370         }
371         ;
373 postfix_expression:
374         primary_expression
375         | postfix_expression '[' integer_expression ']'
376         {
377            void *ctx = state;
378            $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
379            $$->set_location(yylloc);
380         }
381         | function_call
382         {
383            $$ = $1;
384         }
385         | postfix_expression '.' any_identifier
386         {
387            void *ctx = state;
388            $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
389            $$->set_location(yylloc);
390            $$->primary_expression.identifier = $3;
391         }
392         | postfix_expression INC_OP
393         {
394            void *ctx = state;
395            $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
396            $$->set_location(yylloc);
397         }
398         | postfix_expression DEC_OP
399         {
400            void *ctx = state;
401            $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
402            $$->set_location(yylloc);
403         }
404         ;
406 integer_expression:
407         expression
408         ;
410 function_call:
411         function_call_or_method
412         ;
414 function_call_or_method:
415         function_call_generic
416         | postfix_expression '.' method_call_generic
417         {
418            void *ctx = state;
419            $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
420            $$->set_location(yylloc);
421         }
422         ;
424 function_call_generic:
425         function_call_header_with_parameters ')'
426         | function_call_header_no_parameters ')'
427         ;
429 function_call_header_no_parameters:
430         function_call_header VOID_TOK
431         | function_call_header
432         ;
434 function_call_header_with_parameters:
435         function_call_header assignment_expression
436         {
437            $$ = $1;
438            $$->set_location(yylloc);
439            $$->expressions.push_tail(& $2->link);
440         }
441         | function_call_header_with_parameters ',' assignment_expression
442         {
443            $$ = $1;
444            $$->set_location(yylloc);
445            $$->expressions.push_tail(& $3->link);
446         }
447         ;
449         // Grammar Note: Constructors look like functions, but lexical 
450         // analysis recognized most of them as keywords. They are now
451         // recognized through "type_specifier".
452 function_call_header:
453         function_identifier '('
454         ;
456 function_identifier:
457         type_specifier
458         {
459            void *ctx = state;
460            $$ = new(ctx) ast_function_expression($1);
461            $$->set_location(yylloc);
462         }
463         | variable_identifier
464         {
465            void *ctx = state;
466            ast_expression *callee = new(ctx) ast_expression($1);
467            $$ = new(ctx) ast_function_expression(callee);
468            $$->set_location(yylloc);
469         }
470         | FIELD_SELECTION
471         {
472            void *ctx = state;
473            ast_expression *callee = new(ctx) ast_expression($1);
474            $$ = new(ctx) ast_function_expression(callee);
475            $$->set_location(yylloc);
476         }
477         ;
479 method_call_generic:
480         method_call_header_with_parameters ')'
481         | method_call_header_no_parameters ')'
482         ;
484 method_call_header_no_parameters:
485         method_call_header VOID_TOK
486         | method_call_header
487         ;
489 method_call_header_with_parameters:
490         method_call_header assignment_expression
491         {
492            $$ = $1;
493            $$->set_location(yylloc);
494            $$->expressions.push_tail(& $2->link);
495         }
496         | method_call_header_with_parameters ',' assignment_expression
497         {
498            $$ = $1;
499            $$->set_location(yylloc);
500            $$->expressions.push_tail(& $3->link);
501         }
502         ;
504         // Grammar Note: Constructors look like methods, but lexical 
505         // analysis recognized most of them as keywords. They are now
506         // recognized through "type_specifier".
507 method_call_header:
508         variable_identifier '('
509         {
510            void *ctx = state;
511            ast_expression *callee = new(ctx) ast_expression($1);
512            $$ = new(ctx) ast_function_expression(callee);
513            $$->set_location(yylloc);
514         }
515         ;
517         // Grammar Note: No traditional style type casts.
518 unary_expression:
519         postfix_expression
520         | INC_OP unary_expression
521         {
522            void *ctx = state;
523            $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
524            $$->set_location(yylloc);
525         }
526         | DEC_OP unary_expression
527         {
528            void *ctx = state;
529            $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
530            $$->set_location(yylloc);
531         }
532         | unary_operator unary_expression
533         {
534            void *ctx = state;
535            $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
536            $$->set_location(yylloc);
537         }
538         ;
540         // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
541 unary_operator:
542         '+'     { $$ = ast_plus; }
543         | '-'   { $$ = ast_neg; }
544         | '!'   { $$ = ast_logic_not; }
545         | '~'   { $$ = ast_bit_not; }
546         ;
548 multiplicative_expression:
549         unary_expression
550         | multiplicative_expression '*' unary_expression
551         {
552            void *ctx = state;
553            $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
554            $$->set_location(yylloc);
555         }
556         | multiplicative_expression '/' unary_expression
557         {
558            void *ctx = state;
559            $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
560            $$->set_location(yylloc);
561         }
562         | multiplicative_expression '%' unary_expression
563         {
564            void *ctx = state;
565            $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
566            $$->set_location(yylloc);
567         }
568         ;
570 additive_expression:
571         multiplicative_expression
572         | additive_expression '+' multiplicative_expression
573         {
574            void *ctx = state;
575            $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
576            $$->set_location(yylloc);
577         }
578         | additive_expression '-' multiplicative_expression
579         {
580            void *ctx = state;
581            $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
582            $$->set_location(yylloc);
583         }
584         ;
586 shift_expression:
587         additive_expression
588         | shift_expression LEFT_OP additive_expression
589         {
590            void *ctx = state;
591            $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
592            $$->set_location(yylloc);
593         }
594         | shift_expression RIGHT_OP additive_expression
595         {
596            void *ctx = state;
597            $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
598            $$->set_location(yylloc);
599         }
600         ;
602 relational_expression:
603         shift_expression
604         | relational_expression '<' shift_expression
605         {
606            void *ctx = state;
607            $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
608            $$->set_location(yylloc);
609         }
610         | relational_expression '>' shift_expression
611         {
612            void *ctx = state;
613            $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
614            $$->set_location(yylloc);
615         }
616         | relational_expression LE_OP shift_expression
617         {
618            void *ctx = state;
619            $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
620            $$->set_location(yylloc);
621         }
622         | relational_expression GE_OP shift_expression
623         {
624            void *ctx = state;
625            $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
626            $$->set_location(yylloc);
627         }
628         ;
630 equality_expression:
631         relational_expression
632         | equality_expression EQ_OP relational_expression
633         {
634            void *ctx = state;
635            $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
636            $$->set_location(yylloc);
637         }
638         | equality_expression NE_OP relational_expression
639         {
640            void *ctx = state;
641            $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
642            $$->set_location(yylloc);
643         }
644         ;
646 and_expression:
647         equality_expression
648         | and_expression '&' equality_expression
649         {
650            void *ctx = state;
651            $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
652            $$->set_location(yylloc);
653         }
654         ;
656 exclusive_or_expression:
657         and_expression
658         | exclusive_or_expression '^' and_expression
659         {
660            void *ctx = state;
661            $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
662            $$->set_location(yylloc);
663         }
664         ;
666 inclusive_or_expression:
667         exclusive_or_expression
668         | inclusive_or_expression '|' exclusive_or_expression
669         {
670            void *ctx = state;
671            $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
672            $$->set_location(yylloc);
673         }
674         ;
676 logical_and_expression:
677         inclusive_or_expression
678         | logical_and_expression AND_OP inclusive_or_expression
679         {
680            void *ctx = state;
681            $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
682            $$->set_location(yylloc);
683         }
684         ;
686 logical_xor_expression:
687         logical_and_expression
688         | logical_xor_expression XOR_OP logical_and_expression
689         {
690            void *ctx = state;
691            $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
692            $$->set_location(yylloc);
693         }
694         ;
696 logical_or_expression:
697         logical_xor_expression
698         | logical_or_expression OR_OP logical_xor_expression
699         {
700            void *ctx = state;
701            $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
702            $$->set_location(yylloc);
703         }
704         ;
706 conditional_expression:
707         logical_or_expression
708         | logical_or_expression '?' expression ':' assignment_expression
709         {
710            void *ctx = state;
711            $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
712            $$->set_location(yylloc);
713         }
714         ;
716 assignment_expression:
717         conditional_expression
718         | unary_expression assignment_operator assignment_expression
719         {
720            void *ctx = state;
721            $$ = new(ctx) ast_expression($2, $1, $3, NULL);
722            $$->set_location(yylloc);
723         }
724         ;
726 assignment_operator:
727         '='             { $$ = ast_assign; }
728         | MUL_ASSIGN    { $$ = ast_mul_assign; }
729         | DIV_ASSIGN    { $$ = ast_div_assign; }
730         | MOD_ASSIGN    { $$ = ast_mod_assign; }
731         | ADD_ASSIGN    { $$ = ast_add_assign; }
732         | SUB_ASSIGN    { $$ = ast_sub_assign; }
733         | LEFT_ASSIGN   { $$ = ast_ls_assign; }
734         | RIGHT_ASSIGN  { $$ = ast_rs_assign; }
735         | AND_ASSIGN    { $$ = ast_and_assign; }
736         | XOR_ASSIGN    { $$ = ast_xor_assign; }
737         | OR_ASSIGN     { $$ = ast_or_assign; }
738         ;
740 expression:
741         assignment_expression
742         {
743            $$ = $1;
744         }
745         | expression ',' assignment_expression
746         {
747            void *ctx = state;
748            if ($1->oper != ast_sequence) {
749               $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
750               $$->set_location(yylloc);
751               $$->expressions.push_tail(& $1->link);
752            } else {
753               $$ = $1;
754            }
756            $$->expressions.push_tail(& $3->link);
757         }
758         ;
760 constant_expression:
761         conditional_expression
762         ;
764 declaration:
765         function_prototype ';'
766         {
767            state->symbols->pop_scope();
768            $$ = $1;
769         }
770         | init_declarator_list ';'
771         {
772            $$ = $1;
773         }
774         | PRECISION precision_qualifier type_specifier_no_prec ';'
775         {
776            $3->precision = $2;
777            $3->is_precision_statement = true;
778            $$ = $3;
779         }
780         ;
782 function_prototype:
783         function_declarator ')'
784         ;
786 function_declarator:
787         function_header
788         | function_header_with_parameters
789         ;
791 function_header_with_parameters:
792         function_header parameter_declaration
793         {
794            $$ = $1;
795            $$->parameters.push_tail(& $2->link);
796         }
797         | function_header_with_parameters ',' parameter_declaration
798         {
799            $$ = $1;
800            $$->parameters.push_tail(& $3->link);
801         }
802         ;
804 function_header:
805         fully_specified_type variable_identifier '('
806         {
807            void *ctx = state;
808            $$ = new(ctx) ast_function();
809            $$->set_location(yylloc);
810            $$->return_type = $1;
811            $$->identifier = $2;
813            state->symbols->add_function(new(state) ir_function($2));
814            state->symbols->push_scope();
815         }
816         ;
818 parameter_declarator:
819         type_specifier any_identifier
820         {
821            void *ctx = state;
822            $$ = new(ctx) ast_parameter_declarator();
823            $$->set_location(yylloc);
824            $$->type = new(ctx) ast_fully_specified_type();
825            $$->type->set_location(yylloc);
826            $$->type->specifier = $1;
827            $$->identifier = $2;
828         }
829         | type_specifier any_identifier '[' constant_expression ']'
830         {
831            void *ctx = state;
832            $$ = new(ctx) ast_parameter_declarator();
833            $$->set_location(yylloc);
834            $$->type = new(ctx) ast_fully_specified_type();
835            $$->type->set_location(yylloc);
836            $$->type->specifier = $1;
837            $$->identifier = $2;
838            $$->is_array = true;
839            $$->array_size = $4;
840         }
841         ;
843 parameter_declaration:
844         parameter_type_qualifier parameter_qualifier parameter_declarator
845         {
846            $1.flags.i |= $2.flags.i;
848            $$ = $3;
849            $$->type->qualifier = $1;
850         }
851         | parameter_qualifier parameter_declarator
852         {
853            $$ = $2;
854            $$->type->qualifier = $1;
855         }
856         | parameter_type_qualifier parameter_qualifier parameter_type_specifier
857         {
858            void *ctx = state;
859            $1.flags.i |= $2.flags.i;
861            $$ = new(ctx) ast_parameter_declarator();
862            $$->set_location(yylloc);
863            $$->type = new(ctx) ast_fully_specified_type();
864            $$->type->qualifier = $1;
865            $$->type->specifier = $3;
866         }
867         | parameter_qualifier parameter_type_specifier
868         {
869            void *ctx = state;
870            $$ = new(ctx) ast_parameter_declarator();
871            $$->set_location(yylloc);
872            $$->type = new(ctx) ast_fully_specified_type();
873            $$->type->qualifier = $1;
874            $$->type->specifier = $2;
875         }
876         ;
878 parameter_qualifier:
879         /* empty */
880         {
881            memset(& $$, 0, sizeof($$));
882         }
883         | IN_TOK
884         {
885            memset(& $$, 0, sizeof($$));
886            $$.flags.q.in = 1;
887         }
888         | OUT_TOK
889         {
890            memset(& $$, 0, sizeof($$));
891            $$.flags.q.out = 1;
892         }
893         | INOUT_TOK
894         {
895            memset(& $$, 0, sizeof($$));
896            $$.flags.q.in = 1;
897            $$.flags.q.out = 1;
898         }
899         ;
901 parameter_type_specifier:
902         type_specifier
903         ;
905 init_declarator_list:
906         single_declaration
907         | init_declarator_list ',' any_identifier
908         {
909            void *ctx = state;
910            ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
911            decl->set_location(yylloc);
913            $$ = $1;
914            $$->declarations.push_tail(&decl->link);
915            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
916         }
917         | init_declarator_list ',' any_identifier '[' ']'
918         {
919            void *ctx = state;
920            ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
921            decl->set_location(yylloc);
923            $$ = $1;
924            $$->declarations.push_tail(&decl->link);
925            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
926         }
927         | init_declarator_list ',' any_identifier '[' constant_expression ']'
928         {
929            void *ctx = state;
930            ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
931            decl->set_location(yylloc);
933            $$ = $1;
934            $$->declarations.push_tail(&decl->link);
935            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
936         }
937         | init_declarator_list ',' any_identifier '[' ']' '=' initializer
938         {
939            void *ctx = state;
940            ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7);
941            decl->set_location(yylloc);
943            $$ = $1;
944            $$->declarations.push_tail(&decl->link);
945            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
946         }
947         | init_declarator_list ',' any_identifier '[' constant_expression ']' '=' initializer
948         {
949            void *ctx = state;
950            ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
951            decl->set_location(yylloc);
953            $$ = $1;
954            $$->declarations.push_tail(&decl->link);
955            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
956         }
957         | init_declarator_list ',' any_identifier '=' initializer
958         {
959            void *ctx = state;
960            ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
961            decl->set_location(yylloc);
963            $$ = $1;
964            $$->declarations.push_tail(&decl->link);
965            state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
966         }
967         ;
969         // Grammar Note: No 'enum', or 'typedef'.
970 single_declaration:
971         fully_specified_type
972         {
973            void *ctx = state;
974            if ($1->specifier->type_specifier != ast_struct) {
975               _mesa_glsl_error(& @1, state, "empty declaration list\n");
976               YYERROR;
977            } else {
978               $$ = new(ctx) ast_declarator_list($1);
979               $$->set_location(yylloc);
980            }
981         }
982         | fully_specified_type any_identifier
983         {
984            void *ctx = state;
985            ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
987            $$ = new(ctx) ast_declarator_list($1);
988            $$->set_location(yylloc);
989            $$->declarations.push_tail(&decl->link);
990         }
991         | fully_specified_type any_identifier '[' ']'
992         {
993            void *ctx = state;
994            ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
996            $$ = new(ctx) ast_declarator_list($1);
997            $$->set_location(yylloc);
998            $$->declarations.push_tail(&decl->link);
999         }
1000         | fully_specified_type any_identifier '[' constant_expression ']'
1001         {
1002            void *ctx = state;
1003            ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
1005            $$ = new(ctx) ast_declarator_list($1);
1006            $$->set_location(yylloc);
1007            $$->declarations.push_tail(&decl->link);
1008         }
1009         | fully_specified_type any_identifier '[' ']' '=' initializer
1010         {
1011            void *ctx = state;
1012            ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6);
1014            $$ = new(ctx) ast_declarator_list($1);
1015            $$->set_location(yylloc);
1016            $$->declarations.push_tail(&decl->link);
1017         }
1018         | fully_specified_type any_identifier '[' constant_expression ']' '=' initializer
1019         {
1020            void *ctx = state;
1021            ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
1023            $$ = new(ctx) ast_declarator_list($1);
1024            $$->set_location(yylloc);
1025            $$->declarations.push_tail(&decl->link);
1026         }
1027         | fully_specified_type any_identifier '=' initializer
1028         {
1029            void *ctx = state;
1030            ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1032            $$ = new(ctx) ast_declarator_list($1);
1033            $$->set_location(yylloc);
1034            $$->declarations.push_tail(&decl->link);
1035         }
1036         | INVARIANT variable_identifier // Vertex only.
1037         {
1038            void *ctx = state;
1039            ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1041            $$ = new(ctx) ast_declarator_list(NULL);
1042            $$->set_location(yylloc);
1043            $$->invariant = true;
1045            $$->declarations.push_tail(&decl->link);
1046         }
1047         ;
1049 fully_specified_type:
1050         type_specifier
1051         {
1052            void *ctx = state;
1053            $$ = new(ctx) ast_fully_specified_type();
1054            $$->set_location(yylloc);
1055            $$->specifier = $1;
1056         }
1057         | type_qualifier type_specifier
1058         {
1059            void *ctx = state;
1060            $$ = new(ctx) ast_fully_specified_type();
1061            $$->set_location(yylloc);
1062            $$->qualifier = $1;
1063            $$->specifier = $2;
1064         }
1065         ;
1067 layout_qualifier:
1068         LAYOUT_TOK '(' layout_qualifier_id_list ')'
1069         {
1070           $$ = $3;
1071         }
1072         ;
1074 layout_qualifier_id_list:
1075         layout_qualifier_id
1076         | layout_qualifier_id_list ',' layout_qualifier_id
1077         {
1078            if (($1.flags.i & $3.flags.i) != 0) {
1079               _mesa_glsl_error(& @3, state,
1080                                "duplicate layout qualifiers used\n");
1081               YYERROR;
1082            }
1084            $$.flags.i = $1.flags.i | $3.flags.i;
1086            if ($1.flags.q.explicit_location)
1087               $$.location = $1.location;
1089            if ($3.flags.q.explicit_location)
1090               $$.location = $3.location;
1091         }
1092         ;
1094 layout_qualifier_id:
1095         any_identifier
1096         {
1097            bool got_one = false;
1099            memset(& $$, 0, sizeof($$));
1101            /* Layout qualifiers for ARB_fragment_coord_conventions. */
1102            if (!got_one && state->ARB_fragment_coord_conventions_enable) {
1103               if (strcmp($1, "origin_upper_left") == 0) {
1104                  got_one = true;
1105                  $$.flags.q.origin_upper_left = 1;
1106               } else if (strcmp($1, "pixel_center_integer") == 0) {
1107                  got_one = true;
1108                  $$.flags.q.pixel_center_integer = 1;
1109               }
1111               if (got_one && state->ARB_fragment_coord_conventions_warn) {
1112                  _mesa_glsl_warning(& @1, state,
1113                                     "GL_ARB_fragment_coord_conventions layout "
1114                                     "identifier `%s' used\n", $1);
1115               }
1116            }
1118            /* Layout qualifiers for AMD_conservative_depth. */
1119            if (!got_one && state->AMD_conservative_depth_enable) {
1120               if (strcmp($1, "depth_any") == 0) {
1121                  got_one = true;
1122                  $$.flags.q.depth_any = 1;
1123               } else if (strcmp($1, "depth_greater") == 0) {
1124                  got_one = true;
1125                  $$.flags.q.depth_greater = 1;
1126               } else if (strcmp($1, "depth_less") == 0) {
1127                  got_one = true;
1128                  $$.flags.q.depth_less = 1;
1129               } else if (strcmp($1, "depth_unchanged") == 0) {
1130                  got_one = true;
1131                  $$.flags.q.depth_unchanged = 1;
1132               }
1133         
1134               if (got_one && state->AMD_conservative_depth_warn) {
1135                  _mesa_glsl_warning(& @1, state,
1136                                     "GL_AMD_conservative_depth "
1137                                     "layout qualifier `%s' is used\n", $1);
1138               }
1139            }
1141            if (!got_one) {
1142               _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1143                                "`%s'\n", $1);
1144               YYERROR;
1145            }
1146         }
1147         | any_identifier '=' INTCONSTANT
1148         {
1149            bool got_one = false;
1151            memset(& $$, 0, sizeof($$));
1153            if (state->ARB_explicit_attrib_location_enable) {
1154               /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1155                * FINISHME: GLSL 1.30 (or later) are supported.
1156                */
1157               if (strcmp("location", $1) == 0) {
1158                  got_one = true;
1160                  $$.flags.q.explicit_location = 1;
1162                  if ($3 >= 0) {
1163                     $$.location = $3;
1164                  } else {
1165                     _mesa_glsl_error(& @3, state,
1166                                      "invalid location %d specified\n", $3);
1167                     YYERROR;
1168                  }
1169               }
1170            }
1172            /* If the identifier didn't match any known layout identifiers,
1173             * emit an error.
1174             */
1175            if (!got_one) {
1176               _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1177                                "`%s'\n", $1);
1178               YYERROR;
1179            } else if (state->ARB_explicit_attrib_location_warn) {
1180               _mesa_glsl_warning(& @1, state,
1181                                  "GL_ARB_explicit_attrib_location layout "
1182                                  "identifier `%s' used\n", $1);
1183            }
1184         }
1185         ;
1187 interpolation_qualifier:
1188         SMOOTH
1189         {
1190            memset(& $$, 0, sizeof($$));
1191            $$.flags.q.smooth = 1;
1192         }
1193         | FLAT
1194         {
1195            memset(& $$, 0, sizeof($$));
1196            $$.flags.q.flat = 1;
1197         }
1198         | NOPERSPECTIVE
1199         {
1200            memset(& $$, 0, sizeof($$));
1201            $$.flags.q.noperspective = 1;
1202         }
1203         ;
1205 parameter_type_qualifier:
1206         CONST_TOK
1207         {
1208            memset(& $$, 0, sizeof($$));
1209            $$.flags.q.constant = 1;
1210         }
1211         ;
1213 type_qualifier:
1214         storage_qualifier
1215         | layout_qualifier
1216         | layout_qualifier storage_qualifier
1217         {
1218            $$ = $1;
1219            $$.flags.i |= $2.flags.i;
1220         }
1221         | interpolation_qualifier
1222         | interpolation_qualifier storage_qualifier
1223         {
1224            $$ = $1;
1225            $$.flags.i |= $2.flags.i;
1226         }
1227         | INVARIANT storage_qualifier
1228         {
1229            $$ = $2;
1230            $$.flags.q.invariant = 1;
1231         }
1232         | INVARIANT interpolation_qualifier storage_qualifier
1233         {
1234            $$ = $2;
1235            $$.flags.i |= $3.flags.i;
1236            $$.flags.q.invariant = 1;
1237         }
1238         | INVARIANT
1239         {
1240            memset(& $$, 0, sizeof($$));
1241            $$.flags.q.invariant = 1;
1242         }
1243         ;
1245 storage_qualifier:
1246         CONST_TOK
1247         {
1248            memset(& $$, 0, sizeof($$));
1249            $$.flags.q.constant = 1;
1250         }
1251         | ATTRIBUTE
1252         {
1253            memset(& $$, 0, sizeof($$));
1254            $$.flags.q.attribute = 1;
1255         }
1256         | VARYING
1257         {
1258            memset(& $$, 0, sizeof($$));
1259            $$.flags.q.varying = 1;
1260         }
1261         | CENTROID VARYING
1262         {
1263            memset(& $$, 0, sizeof($$));
1264            $$.flags.q.centroid = 1;
1265            $$.flags.q.varying = 1;
1266         }
1267         | IN_TOK
1268         {
1269            memset(& $$, 0, sizeof($$));
1270            $$.flags.q.in = 1;
1271         }
1272         | OUT_TOK
1273         {
1274            memset(& $$, 0, sizeof($$));
1275            $$.flags.q.out = 1;
1276         }
1277         | CENTROID IN_TOK
1278         {
1279            memset(& $$, 0, sizeof($$));
1280            $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1281         }
1282         | CENTROID OUT_TOK
1283         {
1284            memset(& $$, 0, sizeof($$));
1285            $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1286         }
1287         | UNIFORM
1288         {
1289            memset(& $$, 0, sizeof($$));
1290            $$.flags.q.uniform = 1;
1291         }
1292         ;
1294 type_specifier:
1295         type_specifier_no_prec
1296         {
1297            $$ = $1;
1298         }
1299         | precision_qualifier type_specifier_no_prec
1300         {
1301            $$ = $2;
1302            $$->precision = $1;
1303         }
1304         ;
1306 type_specifier_no_prec:
1307         type_specifier_nonarray
1308         | type_specifier_nonarray '[' ']'
1309         {
1310            $$ = $1;
1311            $$->is_array = true;
1312            $$->array_size = NULL;
1313         }
1314         | type_specifier_nonarray '[' constant_expression ']'
1315         {
1316            $$ = $1;
1317            $$->is_array = true;
1318            $$->array_size = $3;
1319         }
1320         ;
1322 type_specifier_nonarray:
1323         basic_type_specifier_nonarray
1324         {
1325            void *ctx = state;
1326            $$ = new(ctx) ast_type_specifier($1);
1327            $$->set_location(yylloc);
1328         }
1329         | struct_specifier
1330         {
1331            void *ctx = state;
1332            $$ = new(ctx) ast_type_specifier($1);
1333            $$->set_location(yylloc);
1334         }
1335         | TYPE_IDENTIFIER
1336         {
1337            void *ctx = state;
1338            $$ = new(ctx) ast_type_specifier($1);
1339            $$->set_location(yylloc);
1340         }
1341         ;
1343 basic_type_specifier_nonarray:
1344         VOID_TOK                { $$ = ast_void; }
1345         | FLOAT_TOK             { $$ = ast_float; }
1346         | INT_TOK               { $$ = ast_int; }
1347         | UINT_TOK              { $$ = ast_uint; }
1348         | BOOL_TOK              { $$ = ast_bool; }
1349         | VEC2                  { $$ = ast_vec2; }
1350         | VEC3                  { $$ = ast_vec3; }
1351         | VEC4                  { $$ = ast_vec4; }
1352         | BVEC2                 { $$ = ast_bvec2; }
1353         | BVEC3                 { $$ = ast_bvec3; }
1354         | BVEC4                 { $$ = ast_bvec4; }
1355         | IVEC2                 { $$ = ast_ivec2; }
1356         | IVEC3                 { $$ = ast_ivec3; }
1357         | IVEC4                 { $$ = ast_ivec4; }
1358         | UVEC2                 { $$ = ast_uvec2; }
1359         | UVEC3                 { $$ = ast_uvec3; }
1360         | UVEC4                 { $$ = ast_uvec4; }
1361         | MAT2X2                { $$ = ast_mat2; }
1362         | MAT2X3                { $$ = ast_mat2x3; }
1363         | MAT2X4                { $$ = ast_mat2x4; }
1364         | MAT3X2                { $$ = ast_mat3x2; }
1365         | MAT3X3                { $$ = ast_mat3; }
1366         | MAT3X4                { $$ = ast_mat3x4; }
1367         | MAT4X2                { $$ = ast_mat4x2; }
1368         | MAT4X3                { $$ = ast_mat4x3; }
1369         | MAT4X4                { $$ = ast_mat4; }
1370         | SAMPLER1D             { $$ = ast_sampler1d; }
1371         | SAMPLER2D             { $$ = ast_sampler2d; }
1372         | SAMPLER2DRECT         { $$ = ast_sampler2drect; }
1373         | SAMPLER3D             { $$ = ast_sampler3d; }
1374         | SAMPLERCUBE           { $$ = ast_samplercube; }
1375         | SAMPLER1DSHADOW       { $$ = ast_sampler1dshadow; }
1376         | SAMPLER2DSHADOW       { $$ = ast_sampler2dshadow; }
1377         | SAMPLER2DRECTSHADOW   { $$ = ast_sampler2drectshadow; }
1378         | SAMPLERCUBESHADOW     { $$ = ast_samplercubeshadow; }
1379         | SAMPLER1DARRAY        { $$ = ast_sampler1darray; }
1380         | SAMPLER2DARRAY        { $$ = ast_sampler2darray; }
1381         | SAMPLER1DARRAYSHADOW  { $$ = ast_sampler1darrayshadow; }
1382         | SAMPLER2DARRAYSHADOW  { $$ = ast_sampler2darrayshadow; }
1383         | ISAMPLER1D            { $$ = ast_isampler1d; }
1384         | ISAMPLER2D            { $$ = ast_isampler2d; }
1385         | ISAMPLER3D            { $$ = ast_isampler3d; }
1386         | ISAMPLERCUBE          { $$ = ast_isamplercube; }
1387         | ISAMPLER1DARRAY       { $$ = ast_isampler1darray; }
1388         | ISAMPLER2DARRAY       { $$ = ast_isampler2darray; }
1389         | USAMPLER1D            { $$ = ast_usampler1d; }
1390         | USAMPLER2D            { $$ = ast_usampler2d; }
1391         | USAMPLER3D            { $$ = ast_usampler3d; }
1392         | USAMPLERCUBE          { $$ = ast_usamplercube; }
1393         | USAMPLER1DARRAY       { $$ = ast_usampler1darray; }
1394         | USAMPLER2DARRAY       { $$ = ast_usampler2darray; }
1395         ;
1397 precision_qualifier:
1398         HIGHP     {
1399                      if (!state->es_shader && state->language_version < 130)
1400                         _mesa_glsl_error(& @1, state,
1401                                          "precision qualifier forbidden "
1402                                          "in %s (1.30 or later "
1403                                          "required)\n",
1404                                          state->version_string);
1406                      $$ = ast_precision_high;
1407                   }
1408         | MEDIUMP {
1409                      if (!state->es_shader && state->language_version < 130)
1410                         _mesa_glsl_error(& @1, state,
1411                                          "precision qualifier forbidden "
1412                                          "in %s (1.30 or later "
1413                                          "required)\n",
1414                                          state->version_string);
1416                      $$ = ast_precision_medium;
1417                   }
1418         | LOWP    {
1419                      if (!state->es_shader && state->language_version < 130)
1420                         _mesa_glsl_error(& @1, state,
1421                                          "precision qualifier forbidden "
1422                                          "in %s (1.30 or later "
1423                                          "required)\n",
1424                                          state->version_string);
1426                      $$ = ast_precision_low;
1427                   }
1428         ;
1430 struct_specifier:
1431         STRUCT any_identifier '{' struct_declaration_list '}'
1432         {
1433            void *ctx = state;
1434            $$ = new(ctx) ast_struct_specifier($2, $4);
1435            $$->set_location(yylloc);
1436            state->symbols->add_type($2, glsl_type::void_type);
1437         }
1438         | STRUCT '{' struct_declaration_list '}'
1439         {
1440            void *ctx = state;
1441            $$ = new(ctx) ast_struct_specifier(NULL, $3);
1442            $$->set_location(yylloc);
1443         }
1444         ;
1446 struct_declaration_list:
1447         struct_declaration
1448         {
1449            $$ = (ast_node *) $1;
1450            $1->link.self_link();
1451         }
1452         | struct_declaration_list struct_declaration
1453         {
1454            $$ = (ast_node *) $1;
1455            $$->link.insert_before(& $2->link);
1456         }
1457         ;
1459 struct_declaration:
1460         type_specifier struct_declarator_list ';'
1461         {
1462            void *ctx = state;
1463            ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1464            type->set_location(yylloc);
1466            type->specifier = $1;
1467            $$ = new(ctx) ast_declarator_list(type);
1468            $$->set_location(yylloc);
1470            $$->declarations.push_degenerate_list_at_head(& $2->link);
1471         }
1472         ;
1474 struct_declarator_list:
1475         struct_declarator
1476         {
1477            $$ = $1;
1478            $1->link.self_link();
1479         }
1480         | struct_declarator_list ',' struct_declarator
1481         {
1482            $$ = $1;
1483            $$->link.insert_before(& $3->link);
1484         }
1485         ;
1487 struct_declarator:
1488         any_identifier
1489         {
1490            void *ctx = state;
1491            $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1492            $$->set_location(yylloc);
1493            state->symbols->add_variable(new(state) ir_variable(NULL, $1, ir_var_auto));
1494         }
1495         | any_identifier '[' constant_expression ']'
1496         {
1497            void *ctx = state;
1498            $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1499            $$->set_location(yylloc);
1500         }
1501         ;
1503 initializer:
1504         assignment_expression
1505         ;
1507 declaration_statement:
1508         declaration
1509         ;
1511         // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1512         // supported.
1513 statement:
1514         compound_statement      { $$ = (ast_node *) $1; }
1515         | simple_statement
1516         ;
1518 simple_statement:
1519         declaration_statement
1520         | expression_statement
1521         | selection_statement
1522         | switch_statement              { $$ = NULL; }
1523         | case_label                    { $$ = NULL; }
1524         | iteration_statement
1525         | jump_statement
1526         ;
1528 compound_statement:
1529         '{' '}'
1530         {
1531            void *ctx = state;
1532            $$ = new(ctx) ast_compound_statement(true, NULL);
1533            $$->set_location(yylloc);
1534         }
1535         | '{'
1536         {
1537            state->symbols->push_scope();
1538         }
1539         statement_list '}'
1540         {
1541            void *ctx = state;
1542            $$ = new(ctx) ast_compound_statement(true, $3);
1543            $$->set_location(yylloc);
1544            state->symbols->pop_scope();
1545         }
1546         ;
1548 statement_no_new_scope:
1549         compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1550         | simple_statement
1551         ;
1553 compound_statement_no_new_scope:
1554         '{' '}'
1555         {
1556            void *ctx = state;
1557            $$ = new(ctx) ast_compound_statement(false, NULL);
1558            $$->set_location(yylloc);
1559         }
1560         | '{' statement_list '}'
1561         {
1562            void *ctx = state;
1563            $$ = new(ctx) ast_compound_statement(false, $2);
1564            $$->set_location(yylloc);
1565         }
1566         ;
1568 statement_list:
1569         statement
1570         {
1571            if ($1 == NULL) {
1572               _mesa_glsl_error(& @1, state, "<nil> statement\n");
1573               assert($1 != NULL);
1574            }
1576            $$ = $1;
1577            $$->link.self_link();
1578         }
1579         | statement_list statement
1580         {
1581            if ($2 == NULL) {
1582               _mesa_glsl_error(& @2, state, "<nil> statement\n");
1583               assert($2 != NULL);
1584            }
1585            $$ = $1;
1586            $$->link.insert_before(& $2->link);
1587         }
1588         ;
1590 expression_statement:
1591         ';'
1592         {
1593            void *ctx = state;
1594            $$ = new(ctx) ast_expression_statement(NULL);
1595            $$->set_location(yylloc);
1596         }
1597         | expression ';'
1598         {
1599            void *ctx = state;
1600            $$ = new(ctx) ast_expression_statement($1);
1601            $$->set_location(yylloc);
1602         }
1603         ;
1605 selection_statement:
1606         IF '(' expression ')' selection_rest_statement
1607         {
1608            $$ = new(state) ast_selection_statement($3, $5.then_statement,
1609                                                    $5.else_statement);
1610            $$->set_location(yylloc);
1611         }
1612         ;
1614 selection_rest_statement:
1615         statement ELSE statement
1616         {
1617            $$.then_statement = $1;
1618            $$.else_statement = $3;
1619         }
1620         | statement
1621         {
1622            $$.then_statement = $1;
1623            $$.else_statement = NULL;
1624         }
1625         ;
1627 condition:
1628         expression
1629         {
1630            $$ = (ast_node *) $1;
1631         }
1632         | fully_specified_type any_identifier '=' initializer
1633         {
1634            void *ctx = state;
1635            ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1636            ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1637            decl->set_location(yylloc);
1638            declarator->set_location(yylloc);
1640            declarator->declarations.push_tail(&decl->link);
1641            $$ = declarator;
1642         }
1643         ;
1645 switch_statement:
1646         SWITCH '(' expression ')' compound_statement
1647         ;
1649 case_label:
1650         CASE expression ':'
1651         | DEFAULT ':'
1652         ;
1654 iteration_statement:
1655         WHILE '(' condition ')' statement_no_new_scope
1656         {
1657            void *ctx = state;
1658            $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1659                                                     NULL, $3, NULL, $5);
1660            $$->set_location(yylloc);
1661         }
1662         | DO statement WHILE '(' expression ')' ';'
1663         {
1664            void *ctx = state;
1665            $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1666                                                     NULL, $5, NULL, $2);
1667            $$->set_location(yylloc);
1668         }
1669         | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1670         {
1671            void *ctx = state;
1672            $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1673                                                     $3, $4.cond, $4.rest, $6);
1674            $$->set_location(yylloc);
1675         }
1676         ;
1678 for_init_statement:
1679         expression_statement
1680         | declaration_statement
1681         ;
1683 conditionopt:
1684         condition
1685         | /* empty */
1686         {
1687            $$ = NULL;
1688         }
1689         ;
1691 for_rest_statement:
1692         conditionopt ';'
1693         {
1694            $$.cond = $1;
1695            $$.rest = NULL;
1696         }
1697         | conditionopt ';' expression
1698         {
1699            $$.cond = $1;
1700            $$.rest = $3;
1701         }
1702         ;
1704         // Grammar Note: No 'goto'. Gotos are not supported.
1705 jump_statement:
1706         CONTINUE ';' 
1707         {
1708            void *ctx = state;
1709            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1710            $$->set_location(yylloc);
1711         }
1712         | BREAK ';'
1713         {
1714            void *ctx = state;
1715            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1716            $$->set_location(yylloc);
1717         }
1718         | RETURN ';'
1719         {
1720            void *ctx = state;
1721            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1722            $$->set_location(yylloc);
1723         }
1724         | RETURN expression ';'
1725         {
1726            void *ctx = state;
1727            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1728            $$->set_location(yylloc);
1729         }
1730         | DISCARD ';' // Fragment shader only.
1731         {
1732            void *ctx = state;
1733            $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1734            $$->set_location(yylloc);
1735         }
1736         ;
1738 external_declaration:
1739         function_definition     { $$ = $1; }
1740         | declaration           { $$ = $1; }
1741         | pragma_statement      { $$ = NULL; }
1742         ;
1744 function_definition:
1745         function_prototype compound_statement_no_new_scope
1746         {
1747            void *ctx = state;
1748            $$ = new(ctx) ast_function_definition();
1749            $$->set_location(yylloc);
1750            $$->prototype = $1;
1751            $$->body = $2;
1753            state->symbols->pop_scope();
1754         }
1755         ;