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