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