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