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