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