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