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