OSDN Git Service

Merge remote-tracking branch 'mesa/12.0' into marshmallow-x86
[android-x86/external-mesa.git] / src / compiler / 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 #ifndef _MSC_VER
28 #include <strings.h>
29 #endif
30 #include <assert.h>
31
32 #include "ast.h"
33 #include "glsl_parser_extras.h"
34 #include "compiler/glsl_types.h"
35 #include "main/context.h"
36
37 #ifdef _MSC_VER
38 #pragma warning( disable : 4065 ) // switch statement contains 'default' but no 'case' labels
39 #endif
40
41 #undef yyerror
42
43 static void yyerror(YYLTYPE *loc, _mesa_glsl_parse_state *st, const char *msg)
44 {
45    _mesa_glsl_error(loc, st, "%s", msg);
46 }
47
48 static int
49 _mesa_glsl_lex(YYSTYPE *val, YYLTYPE *loc, _mesa_glsl_parse_state *state)
50 {
51    return _mesa_glsl_lexer_lex(val, loc, state->scanner);
52 }
53
54 static bool match_layout_qualifier(const char *s1, const char *s2,
55                                    _mesa_glsl_parse_state *state)
56 {
57    /* From the GLSL 1.50 spec, section 4.3.8 (Layout Qualifiers):
58     *
59     *     "The tokens in any layout-qualifier-id-list ... are not case
60     *     sensitive, unless explicitly noted otherwise."
61     *
62     * The text "unless explicitly noted otherwise" appears to be
63     * vacuous--no desktop GLSL spec (up through GLSL 4.40) notes
64     * otherwise.
65     *
66     * However, the GLSL ES 3.00 spec says, in section 4.3.8 (Layout
67     * Qualifiers):
68     *
69     *     "As for other identifiers, they are case sensitive."
70     *
71     * So we need to do a case-sensitive or a case-insensitive match,
72     * depending on whether we are compiling for GLSL ES.
73     */
74    if (state->es_shader)
75       return strcmp(s1, s2);
76    else
77       return strcasecmp(s1, s2);
78 }
79 %}
80
81 %expect 0
82
83 %pure-parser
84 %error-verbose
85
86 %locations
87 %initial-action {
88    @$.first_line = 1;
89    @$.first_column = 1;
90    @$.last_line = 1;
91    @$.last_column = 1;
92    @$.source = 0;
93 }
94
95 %lex-param   {struct _mesa_glsl_parse_state *state}
96 %parse-param {struct _mesa_glsl_parse_state *state}
97
98 %union {
99    int n;
100    float real;
101    double dreal;
102    const char *identifier;
103
104    struct ast_type_qualifier type_qualifier;
105
106    ast_node *node;
107    ast_type_specifier *type_specifier;
108    ast_array_specifier *array_specifier;
109    ast_fully_specified_type *fully_specified_type;
110    ast_function *function;
111    ast_parameter_declarator *parameter_declarator;
112    ast_function_definition *function_definition;
113    ast_compound_statement *compound_statement;
114    ast_expression *expression;
115    ast_declarator_list *declarator_list;
116    ast_struct_specifier *struct_specifier;
117    ast_declaration *declaration;
118    ast_switch_body *switch_body;
119    ast_case_label *case_label;
120    ast_case_label_list *case_label_list;
121    ast_case_statement *case_statement;
122    ast_case_statement_list *case_statement_list;
123    ast_interface_block *interface_block;
124    ast_subroutine_list *subroutine_list;
125    struct {
126       ast_node *cond;
127       ast_expression *rest;
128    } for_rest_statement;
129
130    struct {
131       ast_node *then_statement;
132       ast_node *else_statement;
133    } selection_rest_statement;
134 }
135
136 %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK DOUBLE_TOK
137 %token BREAK BUFFER CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
138 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4 DVEC2 DVEC3 DVEC4
139 %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING SAMPLE
140 %token NOPERSPECTIVE FLAT SMOOTH
141 %token MAT2X2 MAT2X3 MAT2X4
142 %token MAT3X2 MAT3X3 MAT3X4
143 %token MAT4X2 MAT4X3 MAT4X4
144 %token DMAT2X2 DMAT2X3 DMAT2X4
145 %token DMAT3X2 DMAT3X3 DMAT3X4
146 %token DMAT4X2 DMAT4X3 DMAT4X4
147 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
148 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
149 %token SAMPLER2DARRAYSHADOW SAMPLERCUBEARRAY SAMPLERCUBEARRAYSHADOW
150 %token ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
151 %token ISAMPLER1DARRAY ISAMPLER2DARRAY ISAMPLERCUBEARRAY
152 %token USAMPLER1D USAMPLER2D USAMPLER3D USAMPLERCUBE USAMPLER1DARRAY
153 %token USAMPLER2DARRAY USAMPLERCUBEARRAY
154 %token SAMPLER2DRECT ISAMPLER2DRECT USAMPLER2DRECT SAMPLER2DRECTSHADOW
155 %token SAMPLERBUFFER ISAMPLERBUFFER USAMPLERBUFFER
156 %token SAMPLER2DMS ISAMPLER2DMS USAMPLER2DMS
157 %token SAMPLER2DMSARRAY ISAMPLER2DMSARRAY USAMPLER2DMSARRAY
158 %token SAMPLEREXTERNALOES
159 %token IMAGE1D IMAGE2D IMAGE3D IMAGE2DRECT IMAGECUBE IMAGEBUFFER
160 %token IMAGE1DARRAY IMAGE2DARRAY IMAGECUBEARRAY IMAGE2DMS IMAGE2DMSARRAY
161 %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGE2DRECT IIMAGECUBE IIMAGEBUFFER
162 %token IIMAGE1DARRAY IIMAGE2DARRAY IIMAGECUBEARRAY IIMAGE2DMS IIMAGE2DMSARRAY
163 %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGE2DRECT UIMAGECUBE UIMAGEBUFFER
164 %token UIMAGE1DARRAY UIMAGE2DARRAY UIMAGECUBEARRAY UIMAGE2DMS UIMAGE2DMSARRAY
165 %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW
166 %token COHERENT VOLATILE RESTRICT READONLY WRITEONLY
167 %token ATOMIC_UINT
168 %token SHARED
169 %token STRUCT VOID_TOK WHILE
170 %token <identifier> IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
171 %type <identifier> any_identifier
172 %type <interface_block> instance_name_opt
173 %token <real> FLOATCONSTANT
174 %token <dreal> DOUBLECONSTANT
175 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
176 %token <identifier> FIELD_SELECTION
177 %token LEFT_OP RIGHT_OP
178 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
179 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
180 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
181 %token SUB_ASSIGN
182 %token INVARIANT PRECISE
183 %token LOWP MEDIUMP HIGHP SUPERP PRECISION
184
185 %token VERSION_TOK EXTENSION LINE COLON EOL INTERFACE OUTPUT
186 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
187 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
188 %token PRAGMA_INVARIANT_ALL
189 %token LAYOUT_TOK
190 %token DOT_TOK
191    /* Reserved words that are not actually used in the grammar.
192     */
193 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
194 %token INLINE_TOK NOINLINE PUBLIC_TOK STATIC EXTERN EXTERNAL
195 %token LONG_TOK SHORT_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK
196 %token HVEC2 HVEC3 HVEC4 FVEC2 FVEC3 FVEC4
197 %token SAMPLER3DRECT
198 %token SIZEOF CAST NAMESPACE USING
199 %token RESOURCE PATCH
200 %token SUBROUTINE
201
202 %token ERROR_TOK
203
204 %token COMMON PARTITION ACTIVE FILTER ROW_MAJOR
205
206 %type <identifier> variable_identifier
207 %type <node> statement
208 %type <node> statement_list
209 %type <node> simple_statement
210 %type <n> precision_qualifier
211 %type <type_qualifier> type_qualifier
212 %type <type_qualifier> auxiliary_storage_qualifier
213 %type <type_qualifier> storage_qualifier
214 %type <type_qualifier> interpolation_qualifier
215 %type <type_qualifier> layout_qualifier
216 %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
217 %type <type_qualifier> interface_block_layout_qualifier
218 %type <type_qualifier> memory_qualifier
219 %type <type_qualifier> subroutine_qualifier
220 %type <subroutine_list> subroutine_type_list
221 %type <type_qualifier> interface_qualifier
222 %type <type_qualifier> uniform_interface_qualifier
223 %type <type_qualifier> buffer_interface_qualifier
224 %type <type_specifier> type_specifier
225 %type <type_specifier> type_specifier_nonarray
226 %type <array_specifier> array_specifier
227 %type <identifier> basic_type_specifier_nonarray
228 %type <fully_specified_type> fully_specified_type
229 %type <function> function_prototype
230 %type <function> function_header
231 %type <function> function_header_with_parameters
232 %type <function> function_declarator
233 %type <parameter_declarator> parameter_declarator
234 %type <parameter_declarator> parameter_declaration
235 %type <type_qualifier> parameter_qualifier
236 %type <type_qualifier> parameter_direction_qualifier
237 %type <type_specifier> parameter_type_specifier
238 %type <function_definition> function_definition
239 %type <compound_statement> compound_statement_no_new_scope
240 %type <compound_statement> compound_statement
241 %type <node> statement_no_new_scope
242 %type <node> expression_statement
243 %type <expression> expression
244 %type <expression> primary_expression
245 %type <expression> assignment_expression
246 %type <expression> conditional_expression
247 %type <expression> logical_or_expression
248 %type <expression> logical_xor_expression
249 %type <expression> logical_and_expression
250 %type <expression> inclusive_or_expression
251 %type <expression> exclusive_or_expression
252 %type <expression> and_expression
253 %type <expression> equality_expression
254 %type <expression> relational_expression
255 %type <expression> shift_expression
256 %type <expression> additive_expression
257 %type <expression> multiplicative_expression
258 %type <expression> unary_expression
259 %type <expression> constant_expression
260 %type <expression> integer_expression
261 %type <expression> postfix_expression
262 %type <expression> function_call_header_with_parameters
263 %type <expression> function_call_header_no_parameters
264 %type <expression> function_call_header
265 %type <expression> function_call_generic
266 %type <expression> function_call_or_method
267 %type <expression> function_call
268 %type <n> assignment_operator
269 %type <n> unary_operator
270 %type <expression> function_identifier
271 %type <node> external_declaration
272 %type <declarator_list> init_declarator_list
273 %type <declarator_list> single_declaration
274 %type <expression> initializer
275 %type <expression> initializer_list
276 %type <node> declaration
277 %type <node> declaration_statement
278 %type <node> jump_statement
279 %type <node> interface_block
280 %type <interface_block> basic_interface_block
281 %type <struct_specifier> struct_specifier
282 %type <declarator_list> struct_declaration_list
283 %type <declarator_list> struct_declaration
284 %type <declaration> struct_declarator
285 %type <declaration> struct_declarator_list
286 %type <declarator_list> member_list
287 %type <declarator_list> member_declaration
288 %type <node> selection_statement
289 %type <selection_rest_statement> selection_rest_statement
290 %type <node> switch_statement
291 %type <switch_body> switch_body
292 %type <case_label_list> case_label_list
293 %type <case_label> case_label
294 %type <case_statement> case_statement
295 %type <case_statement_list> case_statement_list
296 %type <node> iteration_statement
297 %type <node> condition
298 %type <node> conditionopt
299 %type <node> for_init_statement
300 %type <for_rest_statement> for_rest_statement
301 %type <node> layout_defaults
302 %type <node> layout_uniform_defaults
303 %type <node> layout_buffer_defaults
304 %type <node> layout_in_defaults
305 %type <node> layout_out_defaults
306
307 %right THEN ELSE
308 %%
309
310 translation_unit:
311    version_statement extension_statement_list
312    {
313       _mesa_glsl_initialize_types(state);
314    }
315    external_declaration_list
316    {
317       delete state->symbols;
318       state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
319       if (state->es_shader) {
320          if (state->stage == MESA_SHADER_FRAGMENT) {
321             state->symbols->add_default_precision_qualifier("int", ast_precision_medium);
322          } else {
323             state->symbols->add_default_precision_qualifier("float", ast_precision_high);
324             state->symbols->add_default_precision_qualifier("int", ast_precision_high);
325          }
326          state->symbols->add_default_precision_qualifier("sampler2D", ast_precision_low);
327          state->symbols->add_default_precision_qualifier("samplerExternalOES", ast_precision_low);
328          state->symbols->add_default_precision_qualifier("samplerCube", ast_precision_low);
329          state->symbols->add_default_precision_qualifier("atomic_uint", ast_precision_high);
330       }
331       _mesa_glsl_initialize_types(state);
332    }
333    ;
334
335 version_statement:
336    /* blank - no #version specified: defaults are already set */
337    | VERSION_TOK INTCONSTANT EOL
338    {
339       state->process_version_directive(&@2, $2, NULL);
340       if (state->error) {
341          YYERROR;
342       }
343    }
344    | VERSION_TOK INTCONSTANT any_identifier EOL
345    {
346       state->process_version_directive(&@2, $2, $3);
347       if (state->error) {
348          YYERROR;
349       }
350    }
351    ;
352
353 pragma_statement:
354    PRAGMA_DEBUG_ON EOL
355    | PRAGMA_DEBUG_OFF EOL
356    | PRAGMA_OPTIMIZE_ON EOL
357    | PRAGMA_OPTIMIZE_OFF EOL
358    | PRAGMA_INVARIANT_ALL EOL
359    {
360       /* Pragma invariant(all) cannot be used in a fragment shader.
361        *
362        * Page 27 of the GLSL 1.20 spec, Page 53 of the GLSL ES 3.00 spec:
363        *
364        *     "It is an error to use this pragma in a fragment shader."
365        */
366       if (state->is_version(120, 300) &&
367           state->stage == MESA_SHADER_FRAGMENT) {
368          _mesa_glsl_error(& @1, state,
369                           "pragma `invariant(all)' cannot be used "
370                           "in a fragment shader.");
371       } else if (!state->is_version(120, 100)) {
372          _mesa_glsl_warning(& @1, state,
373                             "pragma `invariant(all)' not supported in %s "
374                             "(GLSL ES 1.00 or GLSL 1.20 required)",
375                             state->get_version_string());
376       } else {
377          state->all_invariant = true;
378       }
379    }
380    ;
381
382 extension_statement_list:
383
384    | extension_statement_list extension_statement
385    ;
386
387 any_identifier:
388    IDENTIFIER
389    | TYPE_IDENTIFIER
390    | NEW_IDENTIFIER
391    ;
392
393 extension_statement:
394    EXTENSION any_identifier COLON any_identifier EOL
395    {
396       if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
397          YYERROR;
398       }
399    }
400    ;
401
402 external_declaration_list:
403    external_declaration
404    {
405       /* FINISHME: The NULL test is required because pragmas are set to
406        * FINISHME: NULL. (See production rule for external_declaration.)
407        */
408       if ($1 != NULL)
409          state->translation_unit.push_tail(& $1->link);
410    }
411    | external_declaration_list external_declaration
412    {
413       /* FINISHME: The NULL test is required because pragmas are set to
414        * FINISHME: NULL. (See production rule for external_declaration.)
415        */
416       if ($2 != NULL)
417          state->translation_unit.push_tail(& $2->link);
418    }
419    | external_declaration_list extension_statement {
420       if (!state->allow_extension_directive_midshader) {
421          _mesa_glsl_error(& @2, state,
422                           "#extension directive is not allowed "
423                           "in the middle of a shader");
424          YYERROR;
425       }
426    }
427    ;
428
429 variable_identifier:
430    IDENTIFIER
431    | NEW_IDENTIFIER
432    ;
433
434 primary_expression:
435    variable_identifier
436    {
437       void *ctx = state;
438       $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
439       $$->set_location(@1);
440       $$->primary_expression.identifier = $1;
441    }
442    | INTCONSTANT
443    {
444       void *ctx = state;
445       $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
446       $$->set_location(@1);
447       $$->primary_expression.int_constant = $1;
448    }
449    | UINTCONSTANT
450    {
451       void *ctx = state;
452       $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
453       $$->set_location(@1);
454       $$->primary_expression.uint_constant = $1;
455    }
456    | FLOATCONSTANT
457    {
458       void *ctx = state;
459       $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
460       $$->set_location(@1);
461       $$->primary_expression.float_constant = $1;
462    }
463    | DOUBLECONSTANT
464    {
465       void *ctx = state;
466       $$ = new(ctx) ast_expression(ast_double_constant, NULL, NULL, NULL);
467       $$->set_location(@1);
468       $$->primary_expression.double_constant = $1;
469    }
470    | BOOLCONSTANT
471    {
472       void *ctx = state;
473       $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
474       $$->set_location(@1);
475       $$->primary_expression.bool_constant = $1;
476    }
477    | '(' expression ')'
478    {
479       $$ = $2;
480    }
481    ;
482
483 postfix_expression:
484    primary_expression
485    | postfix_expression '[' integer_expression ']'
486    {
487       void *ctx = state;
488       $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
489       $$->set_location_range(@1, @4);
490    }
491    | function_call
492    {
493       $$ = $1;
494    }
495    | postfix_expression DOT_TOK FIELD_SELECTION
496    {
497       void *ctx = state;
498       $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
499       $$->set_location_range(@1, @3);
500       $$->primary_expression.identifier = $3;
501    }
502    | postfix_expression INC_OP
503    {
504       void *ctx = state;
505       $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
506       $$->set_location_range(@1, @2);
507    }
508    | postfix_expression DEC_OP
509    {
510       void *ctx = state;
511       $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
512       $$->set_location_range(@1, @2);
513    }
514    ;
515
516 integer_expression:
517    expression
518    ;
519
520 function_call:
521    function_call_or_method
522    ;
523
524 function_call_or_method:
525    function_call_generic
526    ;
527
528 function_call_generic:
529    function_call_header_with_parameters ')'
530    | function_call_header_no_parameters ')'
531    ;
532
533 function_call_header_no_parameters:
534    function_call_header VOID_TOK
535    | function_call_header
536    ;
537
538 function_call_header_with_parameters:
539    function_call_header assignment_expression
540    {
541       $$ = $1;
542       $$->set_location(@1);
543       $$->expressions.push_tail(& $2->link);
544    }
545    | function_call_header_with_parameters ',' assignment_expression
546    {
547       $$ = $1;
548       $$->set_location(@1);
549       $$->expressions.push_tail(& $3->link);
550    }
551    ;
552
553    // Grammar Note: Constructors look like functions, but lexical
554    // analysis recognized most of them as keywords. They are now
555    // recognized through "type_specifier".
556 function_call_header:
557    function_identifier '('
558    ;
559
560 function_identifier:
561    type_specifier
562    {
563       void *ctx = state;
564       $$ = new(ctx) ast_function_expression($1);
565       $$->set_location(@1);
566       }
567    | postfix_expression
568    {
569       void *ctx = state;
570       $$ = new(ctx) ast_function_expression($1);
571       $$->set_location(@1);
572       }
573    ;
574
575    // Grammar Note: Constructors look like methods, but lexical
576    // analysis recognized most of them as keywords. They are now
577    // recognized through "type_specifier".
578
579    // Grammar Note: No traditional style type casts.
580 unary_expression:
581    postfix_expression
582    | INC_OP unary_expression
583    {
584       void *ctx = state;
585       $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
586       $$->set_location(@1);
587    }
588    | DEC_OP unary_expression
589    {
590       void *ctx = state;
591       $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
592       $$->set_location(@1);
593    }
594    | unary_operator unary_expression
595    {
596       void *ctx = state;
597       $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
598       $$->set_location_range(@1, @2);
599    }
600    ;
601
602    // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
603 unary_operator:
604    '+'   { $$ = ast_plus; }
605    | '-' { $$ = ast_neg; }
606    | '!' { $$ = ast_logic_not; }
607    | '~' { $$ = ast_bit_not; }
608    ;
609
610 multiplicative_expression:
611    unary_expression
612    | multiplicative_expression '*' unary_expression
613    {
614       void *ctx = state;
615       $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
616       $$->set_location_range(@1, @3);
617    }
618    | multiplicative_expression '/' unary_expression
619    {
620       void *ctx = state;
621       $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
622       $$->set_location_range(@1, @3);
623    }
624    | multiplicative_expression '%' unary_expression
625    {
626       void *ctx = state;
627       $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
628       $$->set_location_range(@1, @3);
629    }
630    ;
631
632 additive_expression:
633    multiplicative_expression
634    | additive_expression '+' multiplicative_expression
635    {
636       void *ctx = state;
637       $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
638       $$->set_location_range(@1, @3);
639    }
640    | additive_expression '-' multiplicative_expression
641    {
642       void *ctx = state;
643       $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
644       $$->set_location_range(@1, @3);
645    }
646    ;
647
648 shift_expression:
649    additive_expression
650    | shift_expression LEFT_OP additive_expression
651    {
652       void *ctx = state;
653       $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
654       $$->set_location_range(@1, @3);
655    }
656    | shift_expression RIGHT_OP additive_expression
657    {
658       void *ctx = state;
659       $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
660       $$->set_location_range(@1, @3);
661    }
662    ;
663
664 relational_expression:
665    shift_expression
666    | relational_expression '<' shift_expression
667    {
668       void *ctx = state;
669       $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
670       $$->set_location_range(@1, @3);
671    }
672    | relational_expression '>' shift_expression
673    {
674       void *ctx = state;
675       $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
676       $$->set_location_range(@1, @3);
677    }
678    | relational_expression LE_OP shift_expression
679    {
680       void *ctx = state;
681       $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
682       $$->set_location_range(@1, @3);
683    }
684    | relational_expression GE_OP shift_expression
685    {
686       void *ctx = state;
687       $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
688       $$->set_location_range(@1, @3);
689    }
690    ;
691
692 equality_expression:
693    relational_expression
694    | equality_expression EQ_OP relational_expression
695    {
696       void *ctx = state;
697       $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
698       $$->set_location_range(@1, @3);
699    }
700    | equality_expression NE_OP relational_expression
701    {
702       void *ctx = state;
703       $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
704       $$->set_location_range(@1, @3);
705    }
706    ;
707
708 and_expression:
709    equality_expression
710    | and_expression '&' equality_expression
711    {
712       void *ctx = state;
713       $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
714       $$->set_location_range(@1, @3);
715    }
716    ;
717
718 exclusive_or_expression:
719    and_expression
720    | exclusive_or_expression '^' and_expression
721    {
722       void *ctx = state;
723       $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
724       $$->set_location_range(@1, @3);
725    }
726    ;
727
728 inclusive_or_expression:
729    exclusive_or_expression
730    | inclusive_or_expression '|' exclusive_or_expression
731    {
732       void *ctx = state;
733       $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
734       $$->set_location_range(@1, @3);
735    }
736    ;
737
738 logical_and_expression:
739    inclusive_or_expression
740    | logical_and_expression AND_OP inclusive_or_expression
741    {
742       void *ctx = state;
743       $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
744       $$->set_location_range(@1, @3);
745    }
746    ;
747
748 logical_xor_expression:
749    logical_and_expression
750    | logical_xor_expression XOR_OP logical_and_expression
751    {
752       void *ctx = state;
753       $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
754       $$->set_location_range(@1, @3);
755    }
756    ;
757
758 logical_or_expression:
759    logical_xor_expression
760    | logical_or_expression OR_OP logical_xor_expression
761    {
762       void *ctx = state;
763       $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
764       $$->set_location_range(@1, @3);
765    }
766    ;
767
768 conditional_expression:
769    logical_or_expression
770    | logical_or_expression '?' expression ':' assignment_expression
771    {
772       void *ctx = state;
773       $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
774       $$->set_location_range(@1, @5);
775    }
776    ;
777
778 assignment_expression:
779    conditional_expression
780    | unary_expression assignment_operator assignment_expression
781    {
782       void *ctx = state;
783       $$ = new(ctx) ast_expression($2, $1, $3, NULL);
784       $$->set_location_range(@1, @3);
785    }
786    ;
787
788 assignment_operator:
789    '='                { $$ = ast_assign; }
790    | MUL_ASSIGN       { $$ = ast_mul_assign; }
791    | DIV_ASSIGN       { $$ = ast_div_assign; }
792    | MOD_ASSIGN       { $$ = ast_mod_assign; }
793    | ADD_ASSIGN       { $$ = ast_add_assign; }
794    | SUB_ASSIGN       { $$ = ast_sub_assign; }
795    | LEFT_ASSIGN      { $$ = ast_ls_assign; }
796    | RIGHT_ASSIGN     { $$ = ast_rs_assign; }
797    | AND_ASSIGN       { $$ = ast_and_assign; }
798    | XOR_ASSIGN       { $$ = ast_xor_assign; }
799    | OR_ASSIGN        { $$ = ast_or_assign; }
800    ;
801
802 expression:
803    assignment_expression
804    {
805       $$ = $1;
806    }
807    | expression ',' assignment_expression
808    {
809       void *ctx = state;
810       if ($1->oper != ast_sequence) {
811          $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
812          $$->set_location_range(@1, @3);
813          $$->expressions.push_tail(& $1->link);
814       } else {
815          $$ = $1;
816       }
817
818       $$->expressions.push_tail(& $3->link);
819    }
820    ;
821
822 constant_expression:
823    conditional_expression
824    ;
825
826 declaration:
827    function_prototype ';'
828    {
829       state->symbols->pop_scope();
830       $$ = $1;
831    }
832    | init_declarator_list ';'
833    {
834       $$ = $1;
835    }
836    | PRECISION precision_qualifier type_specifier ';'
837    {
838       $3->default_precision = $2;
839       $$ = $3;
840    }
841    | interface_block
842    {
843       $$ = $1;
844    }
845    ;
846
847 function_prototype:
848    function_declarator ')'
849    ;
850
851 function_declarator:
852    function_header
853    | function_header_with_parameters
854    ;
855
856 function_header_with_parameters:
857    function_header parameter_declaration
858    {
859       $$ = $1;
860       $$->parameters.push_tail(& $2->link);
861    }
862    | function_header_with_parameters ',' parameter_declaration
863    {
864       $$ = $1;
865       $$->parameters.push_tail(& $3->link);
866    }
867    ;
868
869 function_header:
870    fully_specified_type variable_identifier '('
871    {
872       void *ctx = state;
873       $$ = new(ctx) ast_function();
874       $$->set_location(@2);
875       $$->return_type = $1;
876       $$->identifier = $2;
877
878       if ($1->qualifier.flags.q.subroutine) {
879          /* add type for IDENTIFIER search */
880          state->symbols->add_type($2, glsl_type::get_subroutine_instance($2));
881       } else
882          state->symbols->add_function(new(state) ir_function($2));
883       state->symbols->push_scope();
884    }
885    ;
886
887 parameter_declarator:
888    type_specifier any_identifier
889    {
890       void *ctx = state;
891       $$ = new(ctx) ast_parameter_declarator();
892       $$->set_location_range(@1, @2);
893       $$->type = new(ctx) ast_fully_specified_type();
894       $$->type->set_location(@1);
895       $$->type->specifier = $1;
896       $$->identifier = $2;
897       state->symbols->add_variable(new(state) ir_variable(NULL, $2, ir_var_auto));
898    }
899    | type_specifier any_identifier array_specifier
900    {
901       void *ctx = state;
902       $$ = new(ctx) ast_parameter_declarator();
903       $$->set_location_range(@1, @3);
904       $$->type = new(ctx) ast_fully_specified_type();
905       $$->type->set_location(@1);
906       $$->type->specifier = $1;
907       $$->identifier = $2;
908       $$->array_specifier = $3;
909       state->symbols->add_variable(new(state) ir_variable(NULL, $2, ir_var_auto));
910    }
911    ;
912
913 parameter_declaration:
914    parameter_qualifier parameter_declarator
915    {
916       $$ = $2;
917       $$->type->qualifier = $1;
918    }
919    | parameter_qualifier parameter_type_specifier
920    {
921       void *ctx = state;
922       $$ = new(ctx) ast_parameter_declarator();
923       $$->set_location(@2);
924       $$->type = new(ctx) ast_fully_specified_type();
925       $$->type->set_location_range(@1, @2);
926       $$->type->qualifier = $1;
927       $$->type->specifier = $2;
928    }
929    ;
930
931 parameter_qualifier:
932    /* empty */
933    {
934       memset(& $$, 0, sizeof($$));
935    }
936    | CONST_TOK parameter_qualifier
937    {
938       if ($2.flags.q.constant)
939          _mesa_glsl_error(&@1, state, "duplicate const qualifier");
940
941       $$ = $2;
942       $$.flags.q.constant = 1;
943    }
944    | PRECISE parameter_qualifier
945    {
946       if ($2.flags.q.precise)
947          _mesa_glsl_error(&@1, state, "duplicate precise qualifier");
948
949       $$ = $2;
950       $$.flags.q.precise = 1;
951    }
952    | parameter_direction_qualifier parameter_qualifier
953    {
954       if (($1.flags.q.in || $1.flags.q.out) && ($2.flags.q.in || $2.flags.q.out))
955          _mesa_glsl_error(&@1, state, "duplicate in/out/inout qualifier");
956
957       if (!state->has_420pack_or_es31() && $2.flags.q.constant)
958          _mesa_glsl_error(&@1, state, "in/out/inout must come after const "
959                                       "or precise");
960
961       $$ = $1;
962       $$.merge_qualifier(&@1, state, $2, false);
963    }
964    | precision_qualifier parameter_qualifier
965    {
966       if ($2.precision != ast_precision_none)
967          _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
968
969       if (!state->has_420pack_or_es31() &&
970           $2.flags.i != 0)
971          _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
972
973       $$ = $2;
974       $$.precision = $1;
975    }
976    | memory_qualifier parameter_qualifier
977    {
978       $$ = $1;
979       $$.merge_qualifier(&@1, state, $2, false);
980    }
981
982 parameter_direction_qualifier:
983    IN_TOK
984    {
985       memset(& $$, 0, sizeof($$));
986       $$.flags.q.in = 1;
987    }
988    | OUT_TOK
989    {
990       memset(& $$, 0, sizeof($$));
991       $$.flags.q.out = 1;
992    }
993    | INOUT_TOK
994    {
995       memset(& $$, 0, sizeof($$));
996       $$.flags.q.in = 1;
997       $$.flags.q.out = 1;
998    }
999    ;
1000
1001 parameter_type_specifier:
1002    type_specifier
1003    ;
1004
1005 init_declarator_list:
1006    single_declaration
1007    | init_declarator_list ',' any_identifier
1008    {
1009       void *ctx = state;
1010       ast_declaration *decl = new(ctx) ast_declaration($3, NULL, NULL);
1011       decl->set_location(@3);
1012
1013       $$ = $1;
1014       $$->declarations.push_tail(&decl->link);
1015       state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1016    }
1017    | init_declarator_list ',' any_identifier array_specifier
1018    {
1019       void *ctx = state;
1020       ast_declaration *decl = new(ctx) ast_declaration($3, $4, NULL);
1021       decl->set_location_range(@3, @4);
1022
1023       $$ = $1;
1024       $$->declarations.push_tail(&decl->link);
1025       state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1026    }
1027    | init_declarator_list ',' any_identifier array_specifier '=' initializer
1028    {
1029       void *ctx = state;
1030       ast_declaration *decl = new(ctx) ast_declaration($3, $4, $6);
1031       decl->set_location_range(@3, @4);
1032
1033       $$ = $1;
1034       $$->declarations.push_tail(&decl->link);
1035       state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1036    }
1037    | init_declarator_list ',' any_identifier '=' initializer
1038    {
1039       void *ctx = state;
1040       ast_declaration *decl = new(ctx) ast_declaration($3, NULL, $5);
1041       decl->set_location(@3);
1042
1043       $$ = $1;
1044       $$->declarations.push_tail(&decl->link);
1045       state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1046    }
1047    ;
1048
1049    // Grammar Note: No 'enum', or 'typedef'.
1050 single_declaration:
1051    fully_specified_type
1052    {
1053       void *ctx = state;
1054       /* Empty declaration list is valid. */
1055       $$ = new(ctx) ast_declarator_list($1);
1056       $$->set_location(@1);
1057    }
1058    | fully_specified_type any_identifier
1059    {
1060       void *ctx = state;
1061       ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
1062       decl->set_location(@2);
1063
1064       $$ = new(ctx) ast_declarator_list($1);
1065       $$->set_location_range(@1, @2);
1066       $$->declarations.push_tail(&decl->link);
1067       state->symbols->add_variable(new(state) ir_variable(NULL, $2, ir_var_auto));
1068    }
1069    | fully_specified_type any_identifier array_specifier
1070    {
1071       void *ctx = state;
1072       ast_declaration *decl = new(ctx) ast_declaration($2, $3, NULL);
1073       decl->set_location_range(@2, @3);
1074
1075       $$ = new(ctx) ast_declarator_list($1);
1076       $$->set_location_range(@1, @3);
1077       $$->declarations.push_tail(&decl->link);
1078       state->symbols->add_variable(new(state) ir_variable(NULL, $2, ir_var_auto));
1079    }
1080    | fully_specified_type any_identifier array_specifier '=' initializer
1081    {
1082       void *ctx = state;
1083       ast_declaration *decl = new(ctx) ast_declaration($2, $3, $5);
1084       decl->set_location_range(@2, @3);
1085
1086       $$ = new(ctx) ast_declarator_list($1);
1087       $$->set_location_range(@1, @3);
1088       $$->declarations.push_tail(&decl->link);
1089       state->symbols->add_variable(new(state) ir_variable(NULL, $2, ir_var_auto));
1090    }
1091    | fully_specified_type any_identifier '=' initializer
1092    {
1093       void *ctx = state;
1094       ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
1095       decl->set_location(@2);
1096
1097       $$ = new(ctx) ast_declarator_list($1);
1098       $$->set_location_range(@1, @2);
1099       $$->declarations.push_tail(&decl->link);
1100       state->symbols->add_variable(new(state) ir_variable(NULL, $2, ir_var_auto));
1101    }
1102    | INVARIANT variable_identifier
1103    {
1104       void *ctx = state;
1105       ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
1106       decl->set_location(@2);
1107
1108       $$ = new(ctx) ast_declarator_list(NULL);
1109       $$->set_location_range(@1, @2);
1110       $$->invariant = true;
1111
1112       $$->declarations.push_tail(&decl->link);
1113    }
1114    | PRECISE variable_identifier
1115    {
1116       void *ctx = state;
1117       ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
1118       decl->set_location(@2);
1119
1120       $$ = new(ctx) ast_declarator_list(NULL);
1121       $$->set_location_range(@1, @2);
1122       $$->precise = true;
1123
1124       $$->declarations.push_tail(&decl->link);
1125    }
1126    ;
1127
1128 fully_specified_type:
1129    type_specifier
1130    {
1131       void *ctx = state;
1132       $$ = new(ctx) ast_fully_specified_type();
1133       $$->set_location(@1);
1134       $$->specifier = $1;
1135    }
1136    | type_qualifier type_specifier
1137    {
1138       void *ctx = state;
1139       $$ = new(ctx) ast_fully_specified_type();
1140       $$->set_location_range(@1, @2);
1141       $$->qualifier = $1;
1142       $$->specifier = $2;
1143       if ($$->specifier->structure != NULL &&
1144           $$->specifier->structure->is_declaration) {
1145             $$->specifier->structure->layout = &$$->qualifier;
1146       }
1147    }
1148    ;
1149
1150 layout_qualifier:
1151    LAYOUT_TOK '(' layout_qualifier_id_list ')'
1152    {
1153       $$ = $3;
1154    }
1155    ;
1156
1157 layout_qualifier_id_list:
1158    layout_qualifier_id
1159    | layout_qualifier_id_list ',' layout_qualifier_id
1160    {
1161       $$ = $1;
1162       if (!$$.merge_qualifier(& @3, state, $3, true)) {
1163          YYERROR;
1164       }
1165    }
1166    ;
1167
1168 layout_qualifier_id:
1169    any_identifier
1170    {
1171       memset(& $$, 0, sizeof($$));
1172
1173       /* Layout qualifiers for ARB_fragment_coord_conventions. */
1174       if (!$$.flags.i && (state->ARB_fragment_coord_conventions_enable ||
1175                           state->is_version(150, 0))) {
1176          if (match_layout_qualifier($1, "origin_upper_left", state) == 0) {
1177             $$.flags.q.origin_upper_left = 1;
1178          } else if (match_layout_qualifier($1, "pixel_center_integer",
1179                                            state) == 0) {
1180             $$.flags.q.pixel_center_integer = 1;
1181          }
1182
1183          if ($$.flags.i && state->ARB_fragment_coord_conventions_warn) {
1184             _mesa_glsl_warning(& @1, state,
1185                                "GL_ARB_fragment_coord_conventions layout "
1186                                "identifier `%s' used", $1);
1187          }
1188       }
1189
1190       /* Layout qualifiers for AMD/ARB_conservative_depth. */
1191       if (!$$.flags.i &&
1192           (state->AMD_conservative_depth_enable ||
1193            state->ARB_conservative_depth_enable ||
1194            state->is_version(420, 0))) {
1195          if (match_layout_qualifier($1, "depth_any", state) == 0) {
1196             $$.flags.q.depth_any = 1;
1197          } else if (match_layout_qualifier($1, "depth_greater", state) == 0) {
1198             $$.flags.q.depth_greater = 1;
1199          } else if (match_layout_qualifier($1, "depth_less", state) == 0) {
1200             $$.flags.q.depth_less = 1;
1201          } else if (match_layout_qualifier($1, "depth_unchanged",
1202                                            state) == 0) {
1203             $$.flags.q.depth_unchanged = 1;
1204          }
1205
1206          if ($$.flags.i && state->AMD_conservative_depth_warn) {
1207             _mesa_glsl_warning(& @1, state,
1208                                "GL_AMD_conservative_depth "
1209                                "layout qualifier `%s' is used", $1);
1210          }
1211          if ($$.flags.i && state->ARB_conservative_depth_warn) {
1212             _mesa_glsl_warning(& @1, state,
1213                                "GL_ARB_conservative_depth "
1214                                "layout qualifier `%s' is used", $1);
1215          }
1216       }
1217
1218       /* See also interface_block_layout_qualifier. */
1219       if (!$$.flags.i && state->has_uniform_buffer_objects()) {
1220          if (match_layout_qualifier($1, "std140", state) == 0) {
1221             $$.flags.q.std140 = 1;
1222          } else if (match_layout_qualifier($1, "shared", state) == 0) {
1223             $$.flags.q.shared = 1;
1224          } else if (match_layout_qualifier($1, "std430", state) == 0) {
1225             $$.flags.q.std430 = 1;
1226          } else if (match_layout_qualifier($1, "column_major", state) == 0) {
1227             $$.flags.q.column_major = 1;
1228          /* "row_major" is a reserved word in GLSL 1.30+. Its token is parsed
1229           * below in the interface_block_layout_qualifier rule.
1230           *
1231           * It is not a reserved word in GLSL ES 3.00, so it's handled here as
1232           * an identifier.
1233           *
1234           * Also, this takes care of alternate capitalizations of
1235           * "row_major" (which is necessary because layout qualifiers
1236           * are case-insensitive in desktop GLSL).
1237           */
1238          } else if (match_layout_qualifier($1, "row_major", state) == 0) {
1239             $$.flags.q.row_major = 1;
1240          /* "packed" is a reserved word in GLSL, and its token is
1241           * parsed below in the interface_block_layout_qualifier rule.
1242           * However, we must take care of alternate capitalizations of
1243           * "packed", because layout qualifiers are case-insensitive
1244           * in desktop GLSL.
1245           */
1246          } else if (match_layout_qualifier($1, "packed", state) == 0) {
1247            $$.flags.q.packed = 1;
1248          }
1249
1250          if ($$.flags.i && state->ARB_uniform_buffer_object_warn) {
1251             _mesa_glsl_warning(& @1, state,
1252                                "#version 140 / GL_ARB_uniform_buffer_object "
1253                                "layout qualifier `%s' is used", $1);
1254          }
1255       }
1256
1257       /* Layout qualifiers for GLSL 1.50 geometry shaders. */
1258       if (!$$.flags.i) {
1259          static const struct {
1260             const char *s;
1261             GLenum e;
1262          } map[] = {
1263                  { "points", GL_POINTS },
1264                  { "lines", GL_LINES },
1265                  { "lines_adjacency", GL_LINES_ADJACENCY },
1266                  { "line_strip", GL_LINE_STRIP },
1267                  { "triangles", GL_TRIANGLES },
1268                  { "triangles_adjacency", GL_TRIANGLES_ADJACENCY },
1269                  { "triangle_strip", GL_TRIANGLE_STRIP },
1270          };
1271          for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
1272             if (match_layout_qualifier($1, map[i].s, state) == 0) {
1273                $$.flags.q.prim_type = 1;
1274                $$.prim_type = map[i].e;
1275                break;
1276             }
1277          }
1278
1279          if ($$.flags.i && !state->has_geometry_shader()) {
1280             _mesa_glsl_error(& @1, state, "#version 150 layout "
1281                              "qualifier `%s' used", $1);
1282          }
1283       }
1284
1285       /* Layout qualifiers for ARB_shader_image_load_store. */
1286       if (state->ARB_shader_image_load_store_enable ||
1287           state->is_version(420, 310)) {
1288          if (!$$.flags.i) {
1289             static const struct {
1290                const char *name;
1291                GLenum format;
1292                glsl_base_type base_type;
1293                /** Minimum desktop GLSL version required for the image
1294                 * format.  Use 130 if already present in the original
1295                 * ARB extension.
1296                 */
1297                unsigned required_glsl;
1298                /** Minimum GLSL ES version required for the image format. */
1299                unsigned required_essl;
1300             } map[] = {
1301                { "rgba32f", GL_RGBA32F, GLSL_TYPE_FLOAT, 130, 310 },
1302                { "rgba16f", GL_RGBA16F, GLSL_TYPE_FLOAT, 130, 310 },
1303                { "rg32f", GL_RG32F, GLSL_TYPE_FLOAT, 130, 0 },
1304                { "rg16f", GL_RG16F, GLSL_TYPE_FLOAT, 130, 0 },
1305                { "r11f_g11f_b10f", GL_R11F_G11F_B10F, GLSL_TYPE_FLOAT, 130, 0 },
1306                { "r32f", GL_R32F, GLSL_TYPE_FLOAT, 130, 310 },
1307                { "r16f", GL_R16F, GLSL_TYPE_FLOAT, 130, 0 },
1308                { "rgba32ui", GL_RGBA32UI, GLSL_TYPE_UINT, 130, 310 },
1309                { "rgba16ui", GL_RGBA16UI, GLSL_TYPE_UINT, 130, 310 },
1310                { "rgb10_a2ui", GL_RGB10_A2UI, GLSL_TYPE_UINT, 130, 0 },
1311                { "rgba8ui", GL_RGBA8UI, GLSL_TYPE_UINT, 130, 310 },
1312                { "rg32ui", GL_RG32UI, GLSL_TYPE_UINT, 130, 0 },
1313                { "rg16ui", GL_RG16UI, GLSL_TYPE_UINT, 130, 0 },
1314                { "rg8ui", GL_RG8UI, GLSL_TYPE_UINT, 130, 0 },
1315                { "r32ui", GL_R32UI, GLSL_TYPE_UINT, 130, 310 },
1316                { "r16ui", GL_R16UI, GLSL_TYPE_UINT, 130, 0 },
1317                { "r8ui", GL_R8UI, GLSL_TYPE_UINT, 130, 0 },
1318                { "rgba32i", GL_RGBA32I, GLSL_TYPE_INT, 130, 310 },
1319                { "rgba16i", GL_RGBA16I, GLSL_TYPE_INT, 130, 310 },
1320                { "rgba8i", GL_RGBA8I, GLSL_TYPE_INT, 130, 310 },
1321                { "rg32i", GL_RG32I, GLSL_TYPE_INT, 130, 0 },
1322                { "rg16i", GL_RG16I, GLSL_TYPE_INT, 130, 0 },
1323                { "rg8i", GL_RG8I, GLSL_TYPE_INT, 130, 0 },
1324                { "r32i", GL_R32I, GLSL_TYPE_INT, 130, 310 },
1325                { "r16i", GL_R16I, GLSL_TYPE_INT, 130, 0 },
1326                { "r8i", GL_R8I, GLSL_TYPE_INT, 130, 0 },
1327                { "rgba16", GL_RGBA16, GLSL_TYPE_FLOAT, 130, 0 },
1328                { "rgb10_a2", GL_RGB10_A2, GLSL_TYPE_FLOAT, 130, 0 },
1329                { "rgba8", GL_RGBA8, GLSL_TYPE_FLOAT, 130, 310 },
1330                { "rg16", GL_RG16, GLSL_TYPE_FLOAT, 130, 0 },
1331                { "rg8", GL_RG8, GLSL_TYPE_FLOAT, 130, 0 },
1332                { "r16", GL_R16, GLSL_TYPE_FLOAT, 130, 0 },
1333                { "r8", GL_R8, GLSL_TYPE_FLOAT, 130, 0 },
1334                { "rgba16_snorm", GL_RGBA16_SNORM, GLSL_TYPE_FLOAT, 130, 0 },
1335                { "rgba8_snorm", GL_RGBA8_SNORM, GLSL_TYPE_FLOAT, 130, 310 },
1336                { "rg16_snorm", GL_RG16_SNORM, GLSL_TYPE_FLOAT, 130, 0 },
1337                { "rg8_snorm", GL_RG8_SNORM, GLSL_TYPE_FLOAT, 130, 0 },
1338                { "r16_snorm", GL_R16_SNORM, GLSL_TYPE_FLOAT, 130, 0 },
1339                { "r8_snorm", GL_R8_SNORM, GLSL_TYPE_FLOAT, 130, 0 }
1340             };
1341
1342             for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
1343                if (state->is_version(map[i].required_glsl,
1344                                      map[i].required_essl) &&
1345                    match_layout_qualifier($1, map[i].name, state) == 0) {
1346                   $$.flags.q.explicit_image_format = 1;
1347                   $$.image_format = map[i].format;
1348                   $$.image_base_type = map[i].base_type;
1349                   break;
1350                }
1351             }
1352          }
1353
1354          if (!$$.flags.i &&
1355              match_layout_qualifier($1, "early_fragment_tests", state) == 0) {
1356             /* From section 4.4.1.3 of the GLSL 4.50 specification
1357              * (Fragment Shader Inputs):
1358              *
1359              *  "Fragment shaders also allow the following layout
1360              *   qualifier on in only (not with variable declarations)
1361              *     layout-qualifier-id
1362              *        early_fragment_tests
1363              *   [...]"
1364              */
1365             if (state->stage != MESA_SHADER_FRAGMENT) {
1366                _mesa_glsl_error(& @1, state,
1367                                 "early_fragment_tests layout qualifier only "
1368                                 "valid in fragment shaders");
1369             }
1370
1371             $$.flags.q.early_fragment_tests = 1;
1372          }
1373       }
1374
1375       /* Layout qualifiers for tessellation evaluation shaders. */
1376       if (!$$.flags.i) {
1377          struct {
1378             const char *s;
1379             GLenum e;
1380          } map[] = {
1381                  /* triangles already parsed by gs-specific code */
1382                  { "quads", GL_QUADS },
1383                  { "isolines", GL_ISOLINES },
1384          };
1385          for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
1386             if (match_layout_qualifier($1, map[i].s, state) == 0) {
1387                $$.flags.q.prim_type = 1;
1388                $$.prim_type = map[i].e;
1389                break;
1390             }
1391          }
1392
1393          if ($$.flags.i &&
1394              !state->ARB_tessellation_shader_enable &&
1395              !state->is_version(400, 0)) {
1396             _mesa_glsl_error(& @1, state,
1397                              "primitive mode qualifier `%s' requires "
1398                              "GLSL 4.00 or ARB_tessellation_shader", $1);
1399          }
1400       }
1401       if (!$$.flags.i) {
1402          struct {
1403             const char *s;
1404             GLenum e;
1405          } map[] = {
1406                  { "equal_spacing", GL_EQUAL },
1407                  { "fractional_odd_spacing", GL_FRACTIONAL_ODD },
1408                  { "fractional_even_spacing", GL_FRACTIONAL_EVEN },
1409          };
1410          for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
1411             if (match_layout_qualifier($1, map[i].s, state) == 0) {
1412                $$.flags.q.vertex_spacing = 1;
1413                $$.vertex_spacing = map[i].e;
1414                break;
1415             }
1416          }
1417
1418          if ($$.flags.i &&
1419              !state->ARB_tessellation_shader_enable &&
1420              !state->is_version(400, 0)) {
1421             _mesa_glsl_error(& @1, state,
1422                              "vertex spacing qualifier `%s' requires "
1423                              "GLSL 4.00 or ARB_tessellation_shader", $1);
1424          }
1425       }
1426       if (!$$.flags.i) {
1427          if (match_layout_qualifier($1, "cw", state) == 0) {
1428             $$.flags.q.ordering = 1;
1429             $$.ordering = GL_CW;
1430          } else if (match_layout_qualifier($1, "ccw", state) == 0) {
1431             $$.flags.q.ordering = 1;
1432             $$.ordering = GL_CCW;
1433          }
1434
1435          if ($$.flags.i &&
1436              !state->ARB_tessellation_shader_enable &&
1437              !state->is_version(400, 0)) {
1438             _mesa_glsl_error(& @1, state,
1439                              "ordering qualifier `%s' requires "
1440                              "GLSL 4.00 or ARB_tessellation_shader", $1);
1441          }
1442       }
1443       if (!$$.flags.i) {
1444          if (match_layout_qualifier($1, "point_mode", state) == 0) {
1445             $$.flags.q.point_mode = 1;
1446             $$.point_mode = true;
1447          }
1448
1449          if ($$.flags.i &&
1450              !state->ARB_tessellation_shader_enable &&
1451              !state->is_version(400, 0)) {
1452             _mesa_glsl_error(& @1, state,
1453                              "qualifier `point_mode' requires "
1454                              "GLSL 4.00 or ARB_tessellation_shader");
1455          }
1456       }
1457
1458       if (!$$.flags.i) {
1459          _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1460                           "`%s'", $1);
1461          YYERROR;
1462       }
1463    }
1464    | any_identifier '=' constant_expression
1465    {
1466       memset(& $$, 0, sizeof($$));
1467       void *ctx = state;
1468
1469       if ($3->oper != ast_int_constant &&
1470           $3->oper != ast_uint_constant &&
1471           !state->has_enhanced_layouts()) {
1472          _mesa_glsl_error(& @1, state,
1473                           "compile-time constant expressions require "
1474                           "GLSL 4.40 or ARB_enhanced_layouts");
1475       }
1476
1477       if (match_layout_qualifier("align", $1, state) == 0) {
1478          if (!state->has_enhanced_layouts()) {
1479             _mesa_glsl_error(& @1, state,
1480                              "align qualifier requires "
1481                              "GLSL 4.40 or ARB_enhanced_layouts");
1482          } else {
1483             $$.flags.q.explicit_align = 1;
1484             $$.align = $3;
1485          }
1486       }
1487
1488       if (match_layout_qualifier("location", $1, state) == 0) {
1489          $$.flags.q.explicit_location = 1;
1490
1491          if ($$.flags.q.attribute == 1 &&
1492              state->ARB_explicit_attrib_location_warn) {
1493             _mesa_glsl_warning(& @1, state,
1494                                "GL_ARB_explicit_attrib_location layout "
1495                                "identifier `%s' used", $1);
1496          }
1497          $$.location = $3;
1498       }
1499
1500       if (match_layout_qualifier("component", $1, state) == 0) {
1501          if (!state->has_enhanced_layouts()) {
1502             _mesa_glsl_error(& @1, state,
1503                              "component qualifier requires "
1504                              "GLSL 4.40 or ARB_enhanced_layouts");
1505          } else {
1506             $$.flags.q.explicit_component = 1;
1507             $$.component = $3;
1508          }
1509       }
1510
1511       if (match_layout_qualifier("index", $1, state) == 0) {
1512          if (state->es_shader && !state->EXT_blend_func_extended_enable) {
1513             _mesa_glsl_error(& @3, state, "index layout qualifier requires EXT_blend_func_extended");
1514             YYERROR;
1515          }
1516
1517          $$.flags.q.explicit_index = 1;
1518          $$.index = $3;
1519       }
1520
1521       if ((state->has_420pack_or_es31() ||
1522            state->has_atomic_counters() ||
1523            state->has_shader_storage_buffer_objects()) &&
1524           match_layout_qualifier("binding", $1, state) == 0) {
1525          $$.flags.q.explicit_binding = 1;
1526          $$.binding = $3;
1527       }
1528
1529       if ((state->has_atomic_counters() ||
1530            state->has_enhanced_layouts()) &&
1531           match_layout_qualifier("offset", $1, state) == 0) {
1532          $$.flags.q.explicit_offset = 1;
1533          $$.offset = $3;
1534       }
1535
1536       if (match_layout_qualifier("max_vertices", $1, state) == 0) {
1537          $$.flags.q.max_vertices = 1;
1538          $$.max_vertices = new(ctx) ast_layout_expression(@1, $3);
1539          if (!state->has_geometry_shader()) {
1540             _mesa_glsl_error(& @3, state,
1541                              "#version 150 max_vertices qualifier "
1542                              "specified", $3);
1543          }
1544       }
1545
1546       if (state->stage == MESA_SHADER_GEOMETRY) {
1547          if (match_layout_qualifier("stream", $1, state) == 0 &&
1548              state->check_explicit_attrib_stream_allowed(& @3)) {
1549             $$.flags.q.stream = 1;
1550             $$.flags.q.explicit_stream = 1;
1551             $$.stream = $3;
1552          }
1553       }
1554
1555       if (state->has_enhanced_layouts()) {
1556          if (match_layout_qualifier("xfb_buffer", $1, state) == 0) {
1557             $$.flags.q.xfb_buffer = 1;
1558             $$.flags.q.explicit_xfb_buffer = 1;
1559             $$.xfb_buffer = $3;
1560          }
1561
1562          if (match_layout_qualifier("xfb_offset", $1, state) == 0) {
1563             $$.flags.q.explicit_xfb_offset = 1;
1564             $$.offset = $3;
1565          }
1566
1567          if (match_layout_qualifier("xfb_stride", $1, state) == 0) {
1568             $$.flags.q.xfb_stride = 1;
1569             $$.flags.q.explicit_xfb_stride = 1;
1570             $$.xfb_stride = $3;
1571          }
1572       }
1573
1574       static const char * const local_size_qualifiers[3] = {
1575          "local_size_x",
1576          "local_size_y",
1577          "local_size_z",
1578       };
1579       for (int i = 0; i < 3; i++) {
1580          if (match_layout_qualifier(local_size_qualifiers[i], $1,
1581                                     state) == 0) {
1582             if (!state->has_compute_shader()) {
1583                _mesa_glsl_error(& @3, state,
1584                                 "%s qualifier requires GLSL 4.30 or "
1585                                 "GLSL ES 3.10 or ARB_compute_shader",
1586                                 local_size_qualifiers[i]);
1587                YYERROR;
1588             } else {
1589                $$.flags.q.local_size |= (1 << i);
1590                $$.local_size[i] = new(ctx) ast_layout_expression(@1, $3);
1591             }
1592             break;
1593          }
1594       }
1595
1596       if (match_layout_qualifier("invocations", $1, state) == 0) {
1597          $$.flags.q.invocations = 1;
1598          $$.invocations = new(ctx) ast_layout_expression(@1, $3);
1599          if (!state->is_version(400, 0) &&
1600              !state->ARB_gpu_shader5_enable) {
1601             _mesa_glsl_error(& @3, state,
1602                              "GL_ARB_gpu_shader5 invocations "
1603                              "qualifier specified", $3);
1604          }
1605       }
1606
1607       /* Layout qualifiers for tessellation control shaders. */
1608       if (match_layout_qualifier("vertices", $1, state) == 0) {
1609          $$.flags.q.vertices = 1;
1610          $$.vertices = new(ctx) ast_layout_expression(@1, $3);
1611          if (!state->ARB_tessellation_shader_enable &&
1612              !state->is_version(400, 0)) {
1613             _mesa_glsl_error(& @1, state,
1614                              "vertices qualifier requires GLSL 4.00 or "
1615                              "ARB_tessellation_shader");
1616          }
1617       }
1618
1619       /* If the identifier didn't match any known layout identifiers,
1620        * emit an error.
1621        */
1622       if (!$$.flags.i) {
1623          _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1624                           "`%s'", $1);
1625          YYERROR;
1626       }
1627    }
1628    | interface_block_layout_qualifier
1629    {
1630       $$ = $1;
1631       /* Layout qualifiers for ARB_uniform_buffer_object. */
1632       if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
1633          _mesa_glsl_error(& @1, state,
1634                           "#version 140 / GL_ARB_uniform_buffer_object "
1635                           "layout qualifier `%s' is used", $1);
1636       } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1637          _mesa_glsl_warning(& @1, state,
1638                             "#version 140 / GL_ARB_uniform_buffer_object "
1639                             "layout qualifier `%s' is used", $1);
1640       }
1641    }
1642    ;
1643
1644 /* This is a separate language rule because we parse these as tokens
1645  * (due to them being reserved keywords) instead of identifiers like
1646  * most qualifiers.  See the any_identifier path of
1647  * layout_qualifier_id for the others.
1648  *
1649  * Note that since layout qualifiers are case-insensitive in desktop
1650  * GLSL, all of these qualifiers need to be handled as identifiers as
1651  * well (by the any_identifier path of layout_qualifier_id).
1652  */
1653 interface_block_layout_qualifier:
1654    ROW_MAJOR
1655    {
1656       memset(& $$, 0, sizeof($$));
1657       $$.flags.q.row_major = 1;
1658    }
1659    | PACKED_TOK
1660    {
1661       memset(& $$, 0, sizeof($$));
1662       $$.flags.q.packed = 1;
1663    }
1664    | SHARED
1665    {
1666       memset(& $$, 0, sizeof($$));
1667       $$.flags.q.shared = 1;
1668    }
1669    ;
1670
1671 subroutine_qualifier:
1672    SUBROUTINE
1673    {
1674       memset(& $$, 0, sizeof($$));
1675       $$.flags.q.subroutine = 1;
1676    }
1677    | SUBROUTINE '(' subroutine_type_list ')'
1678    {
1679       memset(& $$, 0, sizeof($$));
1680       $$.flags.q.subroutine_def = 1;
1681       $$.subroutine_list = $3;
1682    }
1683    ;
1684
1685 subroutine_type_list:
1686    any_identifier
1687    {
1688         void *ctx = state;
1689         ast_declaration *decl = new(ctx)  ast_declaration($1, NULL, NULL);
1690         decl->set_location(@1);
1691
1692         $$ = new(ctx) ast_subroutine_list();
1693         $$->declarations.push_tail(&decl->link);
1694    }
1695    | subroutine_type_list ',' any_identifier
1696    {
1697         void *ctx = state;
1698         ast_declaration *decl = new(ctx)  ast_declaration($3, NULL, NULL);
1699         decl->set_location(@3);
1700
1701         $$ = $1;
1702         $$->declarations.push_tail(&decl->link);
1703    }
1704    ;
1705
1706 interpolation_qualifier:
1707    SMOOTH
1708    {
1709       memset(& $$, 0, sizeof($$));
1710       $$.flags.q.smooth = 1;
1711    }
1712    | FLAT
1713    {
1714       memset(& $$, 0, sizeof($$));
1715       $$.flags.q.flat = 1;
1716    }
1717    | NOPERSPECTIVE
1718    {
1719       memset(& $$, 0, sizeof($$));
1720       $$.flags.q.noperspective = 1;
1721    }
1722    ;
1723
1724 type_qualifier:
1725    /* Single qualifiers */
1726    INVARIANT
1727    {
1728       memset(& $$, 0, sizeof($$));
1729       $$.flags.q.invariant = 1;
1730    }
1731    | PRECISE
1732    {
1733       memset(& $$, 0, sizeof($$));
1734       $$.flags.q.precise = 1;
1735    }
1736    | auxiliary_storage_qualifier
1737    | storage_qualifier
1738    | interpolation_qualifier
1739    | layout_qualifier
1740    | memory_qualifier
1741    | subroutine_qualifier
1742    | precision_qualifier
1743    {
1744       memset(&$$, 0, sizeof($$));
1745       $$.precision = $1;
1746    }
1747
1748    /* Multiple qualifiers:
1749     * In GLSL 4.20, these can be specified in any order.  In earlier versions,
1750     * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1751     *
1752     *    invariant interpolation auxiliary storage precision  ...or...
1753     *    layout storage precision
1754     *
1755     * Each qualifier's rule ensures that the accumulated qualifiers on the right
1756     * side don't contain any that must appear on the left hand side.
1757     * For example, when processing a storage qualifier, we check that there are
1758     * no auxiliary, interpolation, layout, invariant, or precise qualifiers to the right.
1759     */
1760    | PRECISE type_qualifier
1761    {
1762       if ($2.flags.q.precise)
1763          _mesa_glsl_error(&@1, state, "duplicate \"precise\" qualifier");
1764
1765       $$ = $2;
1766       $$.flags.q.precise = 1;
1767    }
1768    | INVARIANT type_qualifier
1769    {
1770       if ($2.flags.q.invariant)
1771          _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1772
1773       if (!state->has_420pack_or_es31() && $2.flags.q.precise)
1774          _mesa_glsl_error(&@1, state,
1775                           "\"invariant\" must come after \"precise\"");
1776
1777       $$ = $2;
1778       $$.flags.q.invariant = 1;
1779
1780       /* GLSL ES 3.00 spec, section 4.6.1 "The Invariant Qualifier":
1781        *
1782        * "Only variables output from a shader can be candidates for invariance.
1783        * This includes user-defined output variables and the built-in output
1784        * variables. As only outputs can be declared as invariant, an invariant
1785        * output from one shader stage will still match an input of a subsequent
1786        * stage without the input being declared as invariant."
1787        *
1788        * On the desktop side, this text first appears in GLSL 4.30.
1789        */
1790       if (state->is_version(430, 300) && $$.flags.q.in)
1791          _mesa_glsl_error(&@1, state, "invariant qualifiers cannot be used with shader inputs");
1792    }
1793    | interpolation_qualifier type_qualifier
1794    {
1795       /* Section 4.3 of the GLSL 1.40 specification states:
1796        * "...qualified with one of these interpolation qualifiers"
1797        *
1798        * GLSL 1.30 claims to allow "one or more", but insists that:
1799        * "These interpolation qualifiers may only precede the qualifiers in,
1800        *  centroid in, out, or centroid out in a declaration."
1801        *
1802        * ...which means that e.g. smooth can't precede smooth, so there can be
1803        * only one after all, and the 1.40 text is a clarification, not a change.
1804        */
1805       if ($2.has_interpolation())
1806          _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1807
1808       if (!state->has_420pack_or_es31() &&
1809           ($2.flags.q.precise || $2.flags.q.invariant)) {
1810          _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1811                           "after \"precise\" or \"invariant\"");
1812       }
1813
1814       $$ = $1;
1815       $$.merge_qualifier(&@1, state, $2, false);
1816    }
1817    | layout_qualifier type_qualifier
1818    {
1819       /* In the absence of ARB_shading_language_420pack, layout qualifiers may
1820        * appear no later than auxiliary storage qualifiers. There is no
1821        * particularly clear spec language mandating this, but in all examples
1822        * the layout qualifier precedes the storage qualifier.
1823        *
1824        * We allow combinations of layout with interpolation, invariant or
1825        * precise qualifiers since these are useful in ARB_separate_shader_objects.
1826        * There is no clear spec guidance on this either.
1827        */
1828       if (!state->has_420pack_or_es31() && $2.has_layout())
1829          _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
1830
1831       $$ = $1;
1832       $$.merge_qualifier(&@1, state, $2, false);
1833    }
1834    | subroutine_qualifier type_qualifier
1835    {
1836       $$ = $1;
1837       $$.merge_qualifier(&@1, state, $2, false);
1838    }
1839    | auxiliary_storage_qualifier type_qualifier
1840    {
1841       if ($2.has_auxiliary_storage()) {
1842          _mesa_glsl_error(&@1, state,
1843                           "duplicate auxiliary storage qualifier (centroid or sample)");
1844       }
1845
1846       if (!state->has_420pack_or_es31() &&
1847           ($2.flags.q.precise || $2.flags.q.invariant ||
1848            $2.has_interpolation() || $2.has_layout())) {
1849          _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
1850                           "just before storage qualifiers");
1851       }
1852       $$ = $1;
1853       $$.merge_qualifier(&@1, state, $2, false);
1854    }
1855    | storage_qualifier type_qualifier
1856    {
1857       /* Section 4.3 of the GLSL 1.20 specification states:
1858        * "Variable declarations may have a storage qualifier specified..."
1859        *  1.30 clarifies this to "may have one storage qualifier".
1860        */
1861       if ($2.has_storage())
1862          _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
1863
1864       if (!state->has_420pack_or_es31() &&
1865           ($2.flags.q.precise || $2.flags.q.invariant || $2.has_interpolation() ||
1866            $2.has_layout() || $2.has_auxiliary_storage())) {
1867          _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
1868                           "precise, invariant, interpolation, layout and auxiliary "
1869                           "storage qualifiers");
1870       }
1871
1872       $$ = $1;
1873       $$.merge_qualifier(&@1, state, $2, false);
1874    }
1875    | precision_qualifier type_qualifier
1876    {
1877       if ($2.precision != ast_precision_none)
1878          _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
1879
1880       if (!(state->has_420pack_or_es31()) &&
1881           $2.flags.i != 0)
1882          _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
1883
1884       $$ = $2;
1885       $$.precision = $1;
1886    }
1887    | memory_qualifier type_qualifier
1888    {
1889       $$ = $1;
1890       $$.merge_qualifier(&@1, state, $2, false);
1891    }
1892    ;
1893
1894 auxiliary_storage_qualifier:
1895    CENTROID
1896    {
1897       memset(& $$, 0, sizeof($$));
1898       $$.flags.q.centroid = 1;
1899    }
1900    | SAMPLE
1901    {
1902       memset(& $$, 0, sizeof($$));
1903       $$.flags.q.sample = 1;
1904    }
1905    | PATCH
1906    {
1907       memset(& $$, 0, sizeof($$));
1908       $$.flags.q.patch = 1;
1909    }
1910
1911 storage_qualifier:
1912    CONST_TOK
1913    {
1914       memset(& $$, 0, sizeof($$));
1915       $$.flags.q.constant = 1;
1916    }
1917    | ATTRIBUTE
1918    {
1919       memset(& $$, 0, sizeof($$));
1920       $$.flags.q.attribute = 1;
1921    }
1922    | VARYING
1923    {
1924       memset(& $$, 0, sizeof($$));
1925       $$.flags.q.varying = 1;
1926    }
1927    | IN_TOK
1928    {
1929       memset(& $$, 0, sizeof($$));
1930       $$.flags.q.in = 1;
1931    }
1932    | OUT_TOK
1933    {
1934       memset(& $$, 0, sizeof($$));
1935       $$.flags.q.out = 1;
1936
1937       if (state->stage == MESA_SHADER_GEOMETRY &&
1938           state->has_explicit_attrib_stream()) {
1939          /* Section 4.3.8.2 (Output Layout Qualifiers) of the GLSL 4.00
1940           * spec says:
1941           *
1942           *     "If the block or variable is declared with the stream
1943           *     identifier, it is associated with the specified stream;
1944           *     otherwise, it is associated with the current default stream."
1945           */
1946           $$.flags.q.stream = 1;
1947           $$.flags.q.explicit_stream = 0;
1948           $$.stream = state->out_qualifier->stream;
1949       }
1950
1951       if (state->has_enhanced_layouts()) {
1952           $$.flags.q.xfb_buffer = 1;
1953           $$.flags.q.explicit_xfb_buffer = 0;
1954           $$.xfb_buffer = state->out_qualifier->xfb_buffer;
1955       }
1956    }
1957    | UNIFORM
1958    {
1959       memset(& $$, 0, sizeof($$));
1960       $$.flags.q.uniform = 1;
1961    }
1962    | BUFFER
1963    {
1964       memset(& $$, 0, sizeof($$));
1965       $$.flags.q.buffer = 1;
1966    }
1967    | SHARED
1968    {
1969       memset(& $$, 0, sizeof($$));
1970       $$.flags.q.shared_storage = 1;
1971    }
1972    ;
1973
1974 memory_qualifier:
1975    COHERENT
1976    {
1977       memset(& $$, 0, sizeof($$));
1978       $$.flags.q.coherent = 1;
1979    }
1980    | VOLATILE
1981    {
1982       memset(& $$, 0, sizeof($$));
1983       $$.flags.q._volatile = 1;
1984    }
1985    | RESTRICT
1986    {
1987       STATIC_ASSERT(sizeof($$.flags.q) <= sizeof($$.flags.i));
1988       memset(& $$, 0, sizeof($$));
1989       $$.flags.q.restrict_flag = 1;
1990    }
1991    | READONLY
1992    {
1993       memset(& $$, 0, sizeof($$));
1994       $$.flags.q.read_only = 1;
1995    }
1996    | WRITEONLY
1997    {
1998       memset(& $$, 0, sizeof($$));
1999       $$.flags.q.write_only = 1;
2000    }
2001    ;
2002
2003 array_specifier:
2004    '[' ']'
2005    {
2006       void *ctx = state;
2007       $$ = new(ctx) ast_array_specifier(@1, new(ctx) ast_expression(
2008                                                   ast_unsized_array_dim, NULL,
2009                                                   NULL, NULL));
2010       $$->set_location_range(@1, @2);
2011    }
2012    | '[' constant_expression ']'
2013    {
2014       void *ctx = state;
2015       $$ = new(ctx) ast_array_specifier(@1, $2);
2016       $$->set_location_range(@1, @3);
2017    }
2018    | array_specifier '[' ']'
2019    {
2020       void *ctx = state;
2021       $$ = $1;
2022
2023       if (state->check_arrays_of_arrays_allowed(& @1)) {
2024          $$->add_dimension(new(ctx) ast_expression(ast_unsized_array_dim, NULL,
2025                                                    NULL, NULL));
2026       }
2027    }
2028    | array_specifier '[' constant_expression ']'
2029    {
2030       $$ = $1;
2031
2032       if (state->check_arrays_of_arrays_allowed(& @1)) {
2033          $$->add_dimension($3);
2034       }
2035    }
2036    ;
2037
2038 type_specifier:
2039    type_specifier_nonarray
2040    | type_specifier_nonarray array_specifier
2041    {
2042       $$ = $1;
2043       $$->array_specifier = $2;
2044    }
2045    ;
2046
2047 type_specifier_nonarray:
2048    basic_type_specifier_nonarray
2049    {
2050       void *ctx = state;
2051       $$ = new(ctx) ast_type_specifier($1);
2052       $$->set_location(@1);
2053    }
2054    | struct_specifier
2055    {
2056       void *ctx = state;
2057       $$ = new(ctx) ast_type_specifier($1);
2058       $$->set_location(@1);
2059    }
2060    | TYPE_IDENTIFIER
2061    {
2062       void *ctx = state;
2063       $$ = new(ctx) ast_type_specifier($1);
2064       $$->set_location(@1);
2065    }
2066    ;
2067
2068 basic_type_specifier_nonarray:
2069    VOID_TOK                 { $$ = "void"; }
2070    | FLOAT_TOK              { $$ = "float"; }
2071    | DOUBLE_TOK             { $$ = "double"; }
2072    | INT_TOK                { $$ = "int"; }
2073    | UINT_TOK               { $$ = "uint"; }
2074    | BOOL_TOK               { $$ = "bool"; }
2075    | VEC2                   { $$ = "vec2"; }
2076    | VEC3                   { $$ = "vec3"; }
2077    | VEC4                   { $$ = "vec4"; }
2078    | BVEC2                  { $$ = "bvec2"; }
2079    | BVEC3                  { $$ = "bvec3"; }
2080    | BVEC4                  { $$ = "bvec4"; }
2081    | IVEC2                  { $$ = "ivec2"; }
2082    | IVEC3                  { $$ = "ivec3"; }
2083    | IVEC4                  { $$ = "ivec4"; }
2084    | UVEC2                  { $$ = "uvec2"; }
2085    | UVEC3                  { $$ = "uvec3"; }
2086    | UVEC4                  { $$ = "uvec4"; }
2087    | DVEC2                  { $$ = "dvec2"; }
2088    | DVEC3                  { $$ = "dvec3"; }
2089    | DVEC4                  { $$ = "dvec4"; }
2090    | MAT2X2                 { $$ = "mat2"; }
2091    | MAT2X3                 { $$ = "mat2x3"; }
2092    | MAT2X4                 { $$ = "mat2x4"; }
2093    | MAT3X2                 { $$ = "mat3x2"; }
2094    | MAT3X3                 { $$ = "mat3"; }
2095    | MAT3X4                 { $$ = "mat3x4"; }
2096    | MAT4X2                 { $$ = "mat4x2"; }
2097    | MAT4X3                 { $$ = "mat4x3"; }
2098    | MAT4X4                 { $$ = "mat4"; }
2099    | DMAT2X2                { $$ = "dmat2"; }
2100    | DMAT2X3                { $$ = "dmat2x3"; }
2101    | DMAT2X4                { $$ = "dmat2x4"; }
2102    | DMAT3X2                { $$ = "dmat3x2"; }
2103    | DMAT3X3                { $$ = "dmat3"; }
2104    | DMAT3X4                { $$ = "dmat3x4"; }
2105    | DMAT4X2                { $$ = "dmat4x2"; }
2106    | DMAT4X3                { $$ = "dmat4x3"; }
2107    | DMAT4X4                { $$ = "dmat4"; }
2108    | SAMPLER1D              { $$ = "sampler1D"; }
2109    | SAMPLER2D              { $$ = "sampler2D"; }
2110    | SAMPLER2DRECT          { $$ = "sampler2DRect"; }
2111    | SAMPLER3D              { $$ = "sampler3D"; }
2112    | SAMPLERCUBE            { $$ = "samplerCube"; }
2113    | SAMPLEREXTERNALOES     { $$ = "samplerExternalOES"; }
2114    | SAMPLER1DSHADOW        { $$ = "sampler1DShadow"; }
2115    | SAMPLER2DSHADOW        { $$ = "sampler2DShadow"; }
2116    | SAMPLER2DRECTSHADOW    { $$ = "sampler2DRectShadow"; }
2117    | SAMPLERCUBESHADOW      { $$ = "samplerCubeShadow"; }
2118    | SAMPLER1DARRAY         { $$ = "sampler1DArray"; }
2119    | SAMPLER2DARRAY         { $$ = "sampler2DArray"; }
2120    | SAMPLER1DARRAYSHADOW   { $$ = "sampler1DArrayShadow"; }
2121    | SAMPLER2DARRAYSHADOW   { $$ = "sampler2DArrayShadow"; }
2122    | SAMPLERBUFFER          { $$ = "samplerBuffer"; }
2123    | SAMPLERCUBEARRAY       { $$ = "samplerCubeArray"; }
2124    | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
2125    | ISAMPLER1D             { $$ = "isampler1D"; }
2126    | ISAMPLER2D             { $$ = "isampler2D"; }
2127    | ISAMPLER2DRECT         { $$ = "isampler2DRect"; }
2128    | ISAMPLER3D             { $$ = "isampler3D"; }
2129    | ISAMPLERCUBE           { $$ = "isamplerCube"; }
2130    | ISAMPLER1DARRAY        { $$ = "isampler1DArray"; }
2131    | ISAMPLER2DARRAY        { $$ = "isampler2DArray"; }
2132    | ISAMPLERBUFFER         { $$ = "isamplerBuffer"; }
2133    | ISAMPLERCUBEARRAY      { $$ = "isamplerCubeArray"; }
2134    | USAMPLER1D             { $$ = "usampler1D"; }
2135    | USAMPLER2D             { $$ = "usampler2D"; }
2136    | USAMPLER2DRECT         { $$ = "usampler2DRect"; }
2137    | USAMPLER3D             { $$ = "usampler3D"; }
2138    | USAMPLERCUBE           { $$ = "usamplerCube"; }
2139    | USAMPLER1DARRAY        { $$ = "usampler1DArray"; }
2140    | USAMPLER2DARRAY        { $$ = "usampler2DArray"; }
2141    | USAMPLERBUFFER         { $$ = "usamplerBuffer"; }
2142    | USAMPLERCUBEARRAY      { $$ = "usamplerCubeArray"; }
2143    | SAMPLER2DMS            { $$ = "sampler2DMS"; }
2144    | ISAMPLER2DMS           { $$ = "isampler2DMS"; }
2145    | USAMPLER2DMS           { $$ = "usampler2DMS"; }
2146    | SAMPLER2DMSARRAY       { $$ = "sampler2DMSArray"; }
2147    | ISAMPLER2DMSARRAY      { $$ = "isampler2DMSArray"; }
2148    | USAMPLER2DMSARRAY      { $$ = "usampler2DMSArray"; }
2149    | IMAGE1D                { $$ = "image1D"; }
2150    | IMAGE2D                { $$ = "image2D"; }
2151    | IMAGE3D                { $$ = "image3D"; }
2152    | IMAGE2DRECT            { $$ = "image2DRect"; }
2153    | IMAGECUBE              { $$ = "imageCube"; }
2154    | IMAGEBUFFER            { $$ = "imageBuffer"; }
2155    | IMAGE1DARRAY           { $$ = "image1DArray"; }
2156    | IMAGE2DARRAY           { $$ = "image2DArray"; }
2157    | IMAGECUBEARRAY         { $$ = "imageCubeArray"; }
2158    | IMAGE2DMS              { $$ = "image2DMS"; }
2159    | IMAGE2DMSARRAY         { $$ = "image2DMSArray"; }
2160    | IIMAGE1D               { $$ = "iimage1D"; }
2161    | IIMAGE2D               { $$ = "iimage2D"; }
2162    | IIMAGE3D               { $$ = "iimage3D"; }
2163    | IIMAGE2DRECT           { $$ = "iimage2DRect"; }
2164    | IIMAGECUBE             { $$ = "iimageCube"; }
2165    | IIMAGEBUFFER           { $$ = "iimageBuffer"; }
2166    | IIMAGE1DARRAY          { $$ = "iimage1DArray"; }
2167    | IIMAGE2DARRAY          { $$ = "iimage2DArray"; }
2168    | IIMAGECUBEARRAY        { $$ = "iimageCubeArray"; }
2169    | IIMAGE2DMS             { $$ = "iimage2DMS"; }
2170    | IIMAGE2DMSARRAY        { $$ = "iimage2DMSArray"; }
2171    | UIMAGE1D               { $$ = "uimage1D"; }
2172    | UIMAGE2D               { $$ = "uimage2D"; }
2173    | UIMAGE3D               { $$ = "uimage3D"; }
2174    | UIMAGE2DRECT           { $$ = "uimage2DRect"; }
2175    | UIMAGECUBE             { $$ = "uimageCube"; }
2176    | UIMAGEBUFFER           { $$ = "uimageBuffer"; }
2177    | UIMAGE1DARRAY          { $$ = "uimage1DArray"; }
2178    | UIMAGE2DARRAY          { $$ = "uimage2DArray"; }
2179    | UIMAGECUBEARRAY        { $$ = "uimageCubeArray"; }
2180    | UIMAGE2DMS             { $$ = "uimage2DMS"; }
2181    | UIMAGE2DMSARRAY        { $$ = "uimage2DMSArray"; }
2182    | ATOMIC_UINT            { $$ = "atomic_uint"; }
2183    ;
2184
2185 precision_qualifier:
2186    HIGHP
2187    {
2188       state->check_precision_qualifiers_allowed(&@1);
2189       $$ = ast_precision_high;
2190    }
2191    | MEDIUMP
2192    {
2193       state->check_precision_qualifiers_allowed(&@1);
2194       $$ = ast_precision_medium;
2195    }
2196    | LOWP
2197    {
2198       state->check_precision_qualifiers_allowed(&@1);
2199       $$ = ast_precision_low;
2200    }
2201    ;
2202
2203 struct_specifier:
2204    STRUCT any_identifier '{' struct_declaration_list '}'
2205    {
2206       void *ctx = state;
2207       $$ = new(ctx) ast_struct_specifier($2, $4);
2208       $$->set_location_range(@2, @5);
2209       state->symbols->add_type($2, glsl_type::void_type);
2210    }
2211    | STRUCT '{' struct_declaration_list '}'
2212    {
2213       void *ctx = state;
2214       $$ = new(ctx) ast_struct_specifier(NULL, $3);
2215       $$->set_location_range(@2, @4);
2216    }
2217    ;
2218
2219 struct_declaration_list:
2220    struct_declaration
2221    {
2222       $$ = $1;
2223       $1->link.self_link();
2224    }
2225    | struct_declaration_list struct_declaration
2226    {
2227       $$ = $1;
2228       $$->link.insert_before(& $2->link);
2229    }
2230    ;
2231
2232 struct_declaration:
2233    fully_specified_type struct_declarator_list ';'
2234    {
2235       void *ctx = state;
2236       ast_fully_specified_type *const type = $1;
2237       type->set_location(@1);
2238
2239       if (type->qualifier.flags.i != 0)
2240          _mesa_glsl_error(&@1, state,
2241                           "only precision qualifiers may be applied to "
2242                           "structure members");
2243
2244       $$ = new(ctx) ast_declarator_list(type);
2245       $$->set_location(@2);
2246
2247       $$->declarations.push_degenerate_list_at_head(& $2->link);
2248    }
2249    ;
2250
2251 struct_declarator_list:
2252    struct_declarator
2253    {
2254       $$ = $1;
2255       $1->link.self_link();
2256    }
2257    | struct_declarator_list ',' struct_declarator
2258    {
2259       $$ = $1;
2260       $$->link.insert_before(& $3->link);
2261    }
2262    ;
2263
2264 struct_declarator:
2265    any_identifier
2266    {
2267       void *ctx = state;
2268       $$ = new(ctx) ast_declaration($1, NULL, NULL);
2269       $$->set_location(@1);
2270    }
2271    | any_identifier array_specifier
2272    {
2273       void *ctx = state;
2274       $$ = new(ctx) ast_declaration($1, $2, NULL);
2275       $$->set_location_range(@1, @2);
2276    }
2277    ;
2278
2279 initializer:
2280    assignment_expression
2281    | '{' initializer_list '}'
2282    {
2283       $$ = $2;
2284    }
2285    | '{' initializer_list ',' '}'
2286    {
2287       $$ = $2;
2288    }
2289    ;
2290
2291 initializer_list:
2292    initializer
2293    {
2294       void *ctx = state;
2295       $$ = new(ctx) ast_aggregate_initializer();
2296       $$->set_location(@1);
2297       $$->expressions.push_tail(& $1->link);
2298    }
2299    | initializer_list ',' initializer
2300    {
2301       $1->expressions.push_tail(& $3->link);
2302    }
2303    ;
2304
2305 declaration_statement:
2306    declaration
2307    ;
2308
2309    // Grammar Note: labeled statements for SWITCH only; 'goto' is not
2310    // supported.
2311 statement:
2312    compound_statement        { $$ = (ast_node *) $1; }
2313    | simple_statement
2314    ;
2315
2316 simple_statement:
2317    declaration_statement
2318    | expression_statement
2319    | selection_statement
2320    | switch_statement
2321    | iteration_statement
2322    | jump_statement
2323    ;
2324
2325 compound_statement:
2326    '{' '}'
2327    {
2328       void *ctx = state;
2329       $$ = new(ctx) ast_compound_statement(true, NULL);
2330       $$->set_location_range(@1, @2);
2331    }
2332    | '{'
2333    {
2334       state->symbols->push_scope();
2335    }
2336    statement_list '}'
2337    {
2338       void *ctx = state;
2339       $$ = new(ctx) ast_compound_statement(true, $3);
2340       $$->set_location_range(@1, @4);
2341       state->symbols->pop_scope();
2342    }
2343    ;
2344
2345 statement_no_new_scope:
2346    compound_statement_no_new_scope { $$ = (ast_node *) $1; }
2347    | simple_statement
2348    ;
2349
2350 compound_statement_no_new_scope:
2351    '{' '}'
2352    {
2353       void *ctx = state;
2354       $$ = new(ctx) ast_compound_statement(false, NULL);
2355       $$->set_location_range(@1, @2);
2356    }
2357    | '{' statement_list '}'
2358    {
2359       void *ctx = state;
2360       $$ = new(ctx) ast_compound_statement(false, $2);
2361       $$->set_location_range(@1, @3);
2362    }
2363    ;
2364
2365 statement_list:
2366    statement
2367    {
2368       if ($1 == NULL) {
2369          _mesa_glsl_error(& @1, state, "<nil> statement");
2370          assert($1 != NULL);
2371       }
2372
2373       $$ = $1;
2374       $$->link.self_link();
2375    }
2376    | statement_list statement
2377    {
2378       if ($2 == NULL) {
2379          _mesa_glsl_error(& @2, state, "<nil> statement");
2380          assert($2 != NULL);
2381       }
2382       $$ = $1;
2383       $$->link.insert_before(& $2->link);
2384    }
2385    ;
2386
2387 expression_statement:
2388    ';'
2389    {
2390       void *ctx = state;
2391       $$ = new(ctx) ast_expression_statement(NULL);
2392       $$->set_location(@1);
2393    }
2394    | expression ';'
2395    {
2396       void *ctx = state;
2397       $$ = new(ctx) ast_expression_statement($1);
2398       $$->set_location(@1);
2399    }
2400    ;
2401
2402 selection_statement:
2403    IF '(' expression ')' selection_rest_statement
2404    {
2405       $$ = new(state) ast_selection_statement($3, $5.then_statement,
2406                                               $5.else_statement);
2407       $$->set_location_range(@1, @5);
2408    }
2409    ;
2410
2411 selection_rest_statement:
2412    statement ELSE statement
2413    {
2414       $$.then_statement = $1;
2415       $$.else_statement = $3;
2416    }
2417    | statement %prec THEN
2418    {
2419       $$.then_statement = $1;
2420       $$.else_statement = NULL;
2421    }
2422    ;
2423
2424 condition:
2425    expression
2426    {
2427       $$ = (ast_node *) $1;
2428    }
2429    | fully_specified_type any_identifier '=' initializer
2430    {
2431       void *ctx = state;
2432       ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
2433       ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
2434       decl->set_location_range(@2, @4);
2435       declarator->set_location(@1);
2436
2437       declarator->declarations.push_tail(&decl->link);
2438       $$ = declarator;
2439    }
2440    ;
2441
2442 /*
2443  * switch_statement grammar is based on the syntax described in the body
2444  * of the GLSL spec, not in it's appendix!!!
2445  */
2446 switch_statement:
2447    SWITCH '(' expression ')' switch_body
2448    {
2449       $$ = new(state) ast_switch_statement($3, $5);
2450       $$->set_location_range(@1, @5);
2451    }
2452    ;
2453
2454 switch_body:
2455    '{' '}'
2456    {
2457       $$ = new(state) ast_switch_body(NULL);
2458       $$->set_location_range(@1, @2);
2459    }
2460    | '{' case_statement_list '}'
2461    {
2462       $$ = new(state) ast_switch_body($2);
2463       $$->set_location_range(@1, @3);
2464    }
2465    ;
2466
2467 case_label:
2468    CASE expression ':'
2469    {
2470       $$ = new(state) ast_case_label($2);
2471       $$->set_location(@2);
2472    }
2473    | DEFAULT ':'
2474    {
2475       $$ = new(state) ast_case_label(NULL);
2476       $$->set_location(@2);
2477    }
2478    ;
2479
2480 case_label_list:
2481    case_label
2482    {
2483       ast_case_label_list *labels = new(state) ast_case_label_list();
2484
2485       labels->labels.push_tail(& $1->link);
2486       $$ = labels;
2487       $$->set_location(@1);
2488    }
2489    | case_label_list case_label
2490    {
2491       $$ = $1;
2492       $$->labels.push_tail(& $2->link);
2493    }
2494    ;
2495
2496 case_statement:
2497    case_label_list statement
2498    {
2499       ast_case_statement *stmts = new(state) ast_case_statement($1);
2500       stmts->set_location(@2);
2501
2502       stmts->stmts.push_tail(& $2->link);
2503       $$ = stmts;
2504    }
2505    | case_statement statement
2506    {
2507       $$ = $1;
2508       $$->stmts.push_tail(& $2->link);
2509    }
2510    ;
2511
2512 case_statement_list:
2513    case_statement
2514    {
2515       ast_case_statement_list *cases= new(state) ast_case_statement_list();
2516       cases->set_location(@1);
2517
2518       cases->cases.push_tail(& $1->link);
2519       $$ = cases;
2520    }
2521    | case_statement_list case_statement
2522    {
2523       $$ = $1;
2524       $$->cases.push_tail(& $2->link);
2525    }
2526    ;
2527
2528 iteration_statement:
2529    WHILE '(' condition ')' statement_no_new_scope
2530    {
2531       void *ctx = state;
2532       $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2533                                             NULL, $3, NULL, $5);
2534       $$->set_location_range(@1, @4);
2535    }
2536    | DO statement WHILE '(' expression ')' ';'
2537    {
2538       void *ctx = state;
2539       $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2540                                             NULL, $5, NULL, $2);
2541       $$->set_location_range(@1, @6);
2542    }
2543    | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2544    {
2545       void *ctx = state;
2546       $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2547                                             $3, $4.cond, $4.rest, $6);
2548       $$->set_location_range(@1, @6);
2549    }
2550    ;
2551
2552 for_init_statement:
2553    expression_statement
2554    | declaration_statement
2555    ;
2556
2557 conditionopt:
2558    condition
2559    | /* empty */
2560    {
2561       $$ = NULL;
2562    }
2563    ;
2564
2565 for_rest_statement:
2566    conditionopt ';'
2567    {
2568       $$.cond = $1;
2569       $$.rest = NULL;
2570    }
2571    | conditionopt ';' expression
2572    {
2573       $$.cond = $1;
2574       $$.rest = $3;
2575    }
2576    ;
2577
2578    // Grammar Note: No 'goto'. Gotos are not supported.
2579 jump_statement:
2580    CONTINUE ';'
2581    {
2582       void *ctx = state;
2583       $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2584       $$->set_location(@1);
2585    }
2586    | BREAK ';'
2587    {
2588       void *ctx = state;
2589       $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2590       $$->set_location(@1);
2591    }
2592    | RETURN ';'
2593    {
2594       void *ctx = state;
2595       $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2596       $$->set_location(@1);
2597    }
2598    | RETURN expression ';'
2599    {
2600       void *ctx = state;
2601       $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2602       $$->set_location_range(@1, @2);
2603    }
2604    | DISCARD ';' // Fragment shader only.
2605    {
2606       void *ctx = state;
2607       $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2608       $$->set_location(@1);
2609    }
2610    ;
2611
2612 external_declaration:
2613    function_definition      { $$ = $1; }
2614    | declaration            { $$ = $1; }
2615    | pragma_statement       { $$ = NULL; }
2616    | layout_defaults        { $$ = $1; }
2617    ;
2618
2619 function_definition:
2620    function_prototype compound_statement_no_new_scope
2621    {
2622       void *ctx = state;
2623       $$ = new(ctx) ast_function_definition();
2624       $$->set_location_range(@1, @2);
2625       $$->prototype = $1;
2626       $$->body = $2;
2627
2628       state->symbols->pop_scope();
2629    }
2630    ;
2631
2632 /* layout_qualifieropt is packed into this rule */
2633 interface_block:
2634    basic_interface_block
2635    {
2636       $$ = $1;
2637    }
2638    | layout_qualifier interface_block
2639    {
2640       ast_interface_block *block = (ast_interface_block *) $2;
2641
2642       if (!state->has_420pack_or_es31() && block->layout.has_layout() &&
2643           !block->layout.is_default_qualifier) {
2644          _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
2645          YYERROR;
2646       }
2647
2648       if (!block->layout.merge_qualifier(& @1, state, $1, false)) {
2649          YYERROR;
2650       }
2651
2652       block->layout.is_default_qualifier = false;
2653
2654       $$ = block;
2655    }
2656    | memory_qualifier interface_block
2657    {
2658       ast_interface_block *block = (ast_interface_block *)$2;
2659
2660       if (!block->layout.flags.q.buffer) {
2661             _mesa_glsl_error(& @1, state,
2662                              "memory qualifiers can only be used in the "
2663                              "declaration of shader storage blocks");
2664       }
2665       if (!block->layout.merge_qualifier(& @1, state, $1, false)) {
2666          YYERROR;
2667       }
2668       $$ = block;
2669    }
2670    ;
2671
2672 basic_interface_block:
2673    interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2674    {
2675       ast_interface_block *const block = $6;
2676
2677       block->block_name = $2;
2678       block->declarations.push_degenerate_list_at_head(& $4->link);
2679
2680       _mesa_ast_process_interface_block(& @1, state, block, $1);
2681
2682       $$ = block;
2683    }
2684    | uniform_interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2685    {
2686       ast_interface_block *const block = $6;
2687
2688       block->layout = *state->default_uniform_qualifier;
2689       block->block_name = $2;
2690       block->declarations.push_degenerate_list_at_head(& $4->link);
2691
2692       _mesa_ast_process_interface_block(& @1, state, block, $1);
2693
2694       $$ = block;
2695    }
2696    | buffer_interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2697    {
2698       ast_interface_block *const block = $6;
2699
2700       block->layout = *state->default_shader_storage_qualifier;
2701       block->block_name = $2;
2702       block->declarations.push_degenerate_list_at_head(& $4->link);
2703
2704       _mesa_ast_process_interface_block(& @1, state, block, $1);
2705
2706       $$ = block;
2707    }
2708    ;
2709
2710 interface_qualifier:
2711    IN_TOK
2712    {
2713       memset(& $$, 0, sizeof($$));
2714       $$.flags.q.in = 1;
2715    }
2716    | OUT_TOK
2717    {
2718       memset(& $$, 0, sizeof($$));
2719       $$.flags.q.out = 1;
2720    }
2721    ;
2722
2723 uniform_interface_qualifier:
2724    UNIFORM
2725    {
2726       memset(& $$, 0, sizeof($$));
2727       $$.flags.q.uniform = 1;
2728    }
2729    ;
2730
2731 buffer_interface_qualifier:
2732    BUFFER
2733    {
2734       memset(& $$, 0, sizeof($$));
2735       $$.flags.q.buffer = 1;
2736    }
2737    ;
2738
2739 instance_name_opt:
2740    /* empty */
2741    {
2742       $$ = new(state) ast_interface_block(NULL, NULL);
2743    }
2744    | NEW_IDENTIFIER
2745    {
2746       $$ = new(state) ast_interface_block($1, NULL);
2747       $$->set_location(@1);
2748    }
2749    | NEW_IDENTIFIER array_specifier
2750    {
2751       $$ = new(state) ast_interface_block($1, $2);
2752       $$->set_location_range(@1, @2);
2753    }
2754    ;
2755
2756 member_list:
2757    member_declaration
2758    {
2759       $$ = $1;
2760       $1->link.self_link();
2761    }
2762    | member_declaration member_list
2763    {
2764       $$ = $1;
2765       $2->link.insert_before(& $$->link);
2766    }
2767    ;
2768
2769 member_declaration:
2770    fully_specified_type struct_declarator_list ';'
2771    {
2772       void *ctx = state;
2773       ast_fully_specified_type *type = $1;
2774       type->set_location(@1);
2775
2776       if (type->qualifier.flags.q.attribute) {
2777          _mesa_glsl_error(& @1, state,
2778                           "keyword 'attribute' cannot be used with "
2779                           "interface block member");
2780       } else if (type->qualifier.flags.q.varying) {
2781          _mesa_glsl_error(& @1, state,
2782                           "keyword 'varying' cannot be used with "
2783                           "interface block member");
2784       }
2785
2786       $$ = new(ctx) ast_declarator_list(type);
2787       $$->set_location(@2);
2788
2789       $$->declarations.push_degenerate_list_at_head(& $2->link);
2790    }
2791    ;
2792
2793 layout_uniform_defaults:
2794    layout_qualifier layout_uniform_defaults
2795    {
2796       $$ = NULL;
2797       if (!state->has_420pack_or_es31()) {
2798          _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
2799          YYERROR;
2800       } else {
2801          if (!state->default_uniform_qualifier->
2802                 merge_qualifier(& @1, state, $1, false)) {
2803             YYERROR;
2804          }
2805       }
2806    }
2807    | layout_qualifier UNIFORM ';'
2808    {
2809       if (!state->default_uniform_qualifier->
2810              merge_qualifier(& @1, state, $1, false)) {
2811          YYERROR;
2812       }
2813       $$ = NULL;
2814    }
2815    ;
2816
2817 layout_buffer_defaults:
2818    layout_qualifier layout_buffer_defaults
2819    {
2820       $$ = NULL;
2821       if (!state->has_420pack_or_es31()) {
2822          _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
2823          YYERROR;
2824       } else {
2825          if (!state->default_shader_storage_qualifier->
2826                 merge_qualifier(& @1, state, $1, false)) {
2827             YYERROR;
2828          }
2829       }
2830    }
2831    | layout_qualifier BUFFER ';'
2832    {
2833       if (!state->default_shader_storage_qualifier->
2834              merge_qualifier(& @1, state, $1, false)) {
2835          YYERROR;
2836       }
2837
2838       /* From the GLSL 4.50 spec, section 4.4.5:
2839        *
2840        *     "It is a compile-time error to specify the binding identifier for
2841        *     the global scope or for block member declarations."
2842        */
2843       if (state->default_shader_storage_qualifier->flags.q.explicit_binding) {
2844          _mesa_glsl_error(& @1, state,
2845                           "binding qualifier cannot be set for default layout");
2846       }
2847
2848       $$ = NULL;
2849    }
2850    ;
2851
2852 layout_in_defaults:
2853    layout_qualifier layout_in_defaults
2854    {
2855       $$ = NULL;
2856       if (!state->has_420pack_or_es31()) {
2857          _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
2858          YYERROR;
2859       } else {
2860          if (!state->in_qualifier->
2861                 merge_in_qualifier(& @1, state, $1, $$, false)) {
2862             YYERROR;
2863          }
2864          $$ = $2;
2865       }
2866    }
2867    | layout_qualifier IN_TOK ';'
2868    {
2869       $$ = NULL;
2870       if (!state->in_qualifier->
2871              merge_in_qualifier(& @1, state, $1, $$, true)) {
2872          YYERROR;
2873       }
2874    }
2875    ;
2876
2877 layout_out_defaults:
2878    layout_qualifier layout_out_defaults
2879    {
2880       $$ = NULL;
2881       if (!state->has_420pack_or_es31()) {
2882          _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
2883          YYERROR;
2884       } else {
2885          if (!state->out_qualifier->
2886                 merge_out_qualifier(& @1, state, $1, $$, false)) {
2887             YYERROR;
2888          }
2889          $$ = $2;
2890       }
2891    }
2892    | layout_qualifier OUT_TOK ';'
2893    {
2894       $$ = NULL;
2895       if (!state->out_qualifier->
2896              merge_out_qualifier(& @1, state, $1, $$, true))
2897          YYERROR;
2898    }
2899    ;
2900
2901 layout_defaults:
2902    layout_uniform_defaults
2903    | layout_buffer_defaults
2904    | layout_in_defaults
2905    | layout_out_defaults
2906    ;