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