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