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