glsl: allow image qualifiers inside structures
[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 /* Layout qualifiers for ARB_bindless_texture. */
1593 if (!$$.flags.i) {
1594 if (match_layout_qualifier($1, "bindless_sampler", state) == 0)
1595 $$.flags.q.bindless_sampler = 1;
1596 if (match_layout_qualifier($1, "bound_sampler", state) == 0)
1597 $$.flags.q.bound_sampler = 1;
1598
1599 if (state->has_shader_image_load_store()) {
1600 if (match_layout_qualifier($1, "bindless_image", state) == 0)
1601 $$.flags.q.bindless_image = 1;
1602 if (match_layout_qualifier($1, "bound_image", state) == 0)
1603 $$.flags.q.bound_image = 1;
1604 }
1605
1606 if ($$.flags.i && !state->has_bindless()) {
1607 _mesa_glsl_error(& @1, state,
1608 "qualifier `%s` requires "
1609 "ARB_bindless_texture", $1);
1610 }
1611 }
1612
1613 if (!$$.flags.i) {
1614 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1615 "`%s'", $1);
1616 YYERROR;
1617 }
1618 }
1619 | any_identifier '=' constant_expression
1620 {
1621 memset(& $$, 0, sizeof($$));
1622 void *ctx = state->linalloc;
1623
1624 if ($3->oper != ast_int_constant &&
1625 $3->oper != ast_uint_constant &&
1626 !state->has_enhanced_layouts()) {
1627 _mesa_glsl_error(& @1, state,
1628 "compile-time constant expressions require "
1629 "GLSL 4.40 or ARB_enhanced_layouts");
1630 }
1631
1632 if (match_layout_qualifier("align", $1, state) == 0) {
1633 if (!state->has_enhanced_layouts()) {
1634 _mesa_glsl_error(& @1, state,
1635 "align qualifier requires "
1636 "GLSL 4.40 or ARB_enhanced_layouts");
1637 } else {
1638 $$.flags.q.explicit_align = 1;
1639 $$.align = $3;
1640 }
1641 }
1642
1643 if (match_layout_qualifier("location", $1, state) == 0) {
1644 $$.flags.q.explicit_location = 1;
1645
1646 if ($$.flags.q.attribute == 1 &&
1647 state->ARB_explicit_attrib_location_warn) {
1648 _mesa_glsl_warning(& @1, state,
1649 "GL_ARB_explicit_attrib_location layout "
1650 "identifier `%s' used", $1);
1651 }
1652 $$.location = $3;
1653 }
1654
1655 if (match_layout_qualifier("component", $1, state) == 0) {
1656 if (!state->has_enhanced_layouts()) {
1657 _mesa_glsl_error(& @1, state,
1658 "component qualifier requires "
1659 "GLSL 4.40 or ARB_enhanced_layouts");
1660 } else {
1661 $$.flags.q.explicit_component = 1;
1662 $$.component = $3;
1663 }
1664 }
1665
1666 if (match_layout_qualifier("index", $1, state) == 0) {
1667 if (state->es_shader && !state->EXT_blend_func_extended_enable) {
1668 _mesa_glsl_error(& @3, state, "index layout qualifier requires EXT_blend_func_extended");
1669 YYERROR;
1670 }
1671
1672 $$.flags.q.explicit_index = 1;
1673 $$.index = $3;
1674 }
1675
1676 if ((state->has_420pack_or_es31() ||
1677 state->has_atomic_counters() ||
1678 state->has_shader_storage_buffer_objects()) &&
1679 match_layout_qualifier("binding", $1, state) == 0) {
1680 $$.flags.q.explicit_binding = 1;
1681 $$.binding = $3;
1682 }
1683
1684 if ((state->has_atomic_counters() ||
1685 state->has_enhanced_layouts()) &&
1686 match_layout_qualifier("offset", $1, state) == 0) {
1687 $$.flags.q.explicit_offset = 1;
1688 $$.offset = $3;
1689 }
1690
1691 if (match_layout_qualifier("max_vertices", $1, state) == 0) {
1692 $$.flags.q.max_vertices = 1;
1693 $$.max_vertices = new(ctx) ast_layout_expression(@1, $3);
1694 if (!state->has_geometry_shader()) {
1695 _mesa_glsl_error(& @3, state,
1696 "#version 150 max_vertices qualifier "
1697 "specified", $3);
1698 }
1699 }
1700
1701 if (state->stage == MESA_SHADER_GEOMETRY) {
1702 if (match_layout_qualifier("stream", $1, state) == 0 &&
1703 state->check_explicit_attrib_stream_allowed(& @3)) {
1704 $$.flags.q.stream = 1;
1705 $$.flags.q.explicit_stream = 1;
1706 $$.stream = $3;
1707 }
1708 }
1709
1710 if (state->has_enhanced_layouts()) {
1711 if (match_layout_qualifier("xfb_buffer", $1, state) == 0) {
1712 $$.flags.q.xfb_buffer = 1;
1713 $$.flags.q.explicit_xfb_buffer = 1;
1714 $$.xfb_buffer = $3;
1715 }
1716
1717 if (match_layout_qualifier("xfb_offset", $1, state) == 0) {
1718 $$.flags.q.explicit_xfb_offset = 1;
1719 $$.offset = $3;
1720 }
1721
1722 if (match_layout_qualifier("xfb_stride", $1, state) == 0) {
1723 $$.flags.q.xfb_stride = 1;
1724 $$.flags.q.explicit_xfb_stride = 1;
1725 $$.xfb_stride = $3;
1726 }
1727 }
1728
1729 static const char * const local_size_qualifiers[3] = {
1730 "local_size_x",
1731 "local_size_y",
1732 "local_size_z",
1733 };
1734 for (int i = 0; i < 3; i++) {
1735 if (match_layout_qualifier(local_size_qualifiers[i], $1,
1736 state) == 0) {
1737 if (!state->has_compute_shader()) {
1738 _mesa_glsl_error(& @3, state,
1739 "%s qualifier requires GLSL 4.30 or "
1740 "GLSL ES 3.10 or ARB_compute_shader",
1741 local_size_qualifiers[i]);
1742 YYERROR;
1743 } else {
1744 $$.flags.q.local_size |= (1 << i);
1745 $$.local_size[i] = new(ctx) ast_layout_expression(@1, $3);
1746 }
1747 break;
1748 }
1749 }
1750
1751 if (match_layout_qualifier("invocations", $1, state) == 0) {
1752 $$.flags.q.invocations = 1;
1753 $$.invocations = new(ctx) ast_layout_expression(@1, $3);
1754 if (!state->is_version(400, 320) &&
1755 !state->ARB_gpu_shader5_enable &&
1756 !state->OES_geometry_shader_enable &&
1757 !state->EXT_geometry_shader_enable) {
1758 _mesa_glsl_error(& @3, state,
1759 "GL_ARB_gpu_shader5 invocations "
1760 "qualifier specified", $3);
1761 }
1762 }
1763
1764 /* Layout qualifiers for tessellation control shaders. */
1765 if (match_layout_qualifier("vertices", $1, state) == 0) {
1766 $$.flags.q.vertices = 1;
1767 $$.vertices = new(ctx) ast_layout_expression(@1, $3);
1768 if (!state->has_tessellation_shader()) {
1769 _mesa_glsl_error(& @1, state,
1770 "vertices qualifier requires GLSL 4.00 or "
1771 "ARB_tessellation_shader");
1772 }
1773 }
1774
1775 /* If the identifier didn't match any known layout identifiers,
1776 * emit an error.
1777 */
1778 if (!$$.flags.i) {
1779 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1780 "`%s'", $1);
1781 YYERROR;
1782 }
1783 }
1784 | interface_block_layout_qualifier
1785 {
1786 $$ = $1;
1787 /* Layout qualifiers for ARB_uniform_buffer_object. */
1788 if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
1789 _mesa_glsl_error(& @1, state,
1790 "#version 140 / GL_ARB_uniform_buffer_object "
1791 "layout qualifier `%s' is used", $1);
1792 } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1793 _mesa_glsl_warning(& @1, state,
1794 "#version 140 / GL_ARB_uniform_buffer_object "
1795 "layout qualifier `%s' is used", $1);
1796 }
1797 }
1798 ;
1799
1800 /* This is a separate language rule because we parse these as tokens
1801 * (due to them being reserved keywords) instead of identifiers like
1802 * most qualifiers. See the any_identifier path of
1803 * layout_qualifier_id for the others.
1804 *
1805 * Note that since layout qualifiers are case-insensitive in desktop
1806 * GLSL, all of these qualifiers need to be handled as identifiers as
1807 * well (by the any_identifier path of layout_qualifier_id).
1808 */
1809 interface_block_layout_qualifier:
1810 ROW_MAJOR
1811 {
1812 memset(& $$, 0, sizeof($$));
1813 $$.flags.q.row_major = 1;
1814 }
1815 | PACKED_TOK
1816 {
1817 memset(& $$, 0, sizeof($$));
1818 $$.flags.q.packed = 1;
1819 }
1820 | SHARED
1821 {
1822 memset(& $$, 0, sizeof($$));
1823 $$.flags.q.shared = 1;
1824 }
1825 ;
1826
1827 subroutine_qualifier:
1828 SUBROUTINE
1829 {
1830 memset(& $$, 0, sizeof($$));
1831 $$.flags.q.subroutine = 1;
1832 }
1833 | SUBROUTINE '(' subroutine_type_list ')'
1834 {
1835 memset(& $$, 0, sizeof($$));
1836 $$.flags.q.subroutine = 1;
1837 $$.subroutine_list = $3;
1838 }
1839 ;
1840
1841 subroutine_type_list:
1842 any_identifier
1843 {
1844 void *ctx = state->linalloc;
1845 ast_declaration *decl = new(ctx) ast_declaration($1, NULL, NULL);
1846 decl->set_location(@1);
1847
1848 $$ = new(ctx) ast_subroutine_list();
1849 $$->declarations.push_tail(&decl->link);
1850 }
1851 | subroutine_type_list ',' any_identifier
1852 {
1853 void *ctx = state->linalloc;
1854 ast_declaration *decl = new(ctx) ast_declaration($3, NULL, NULL);
1855 decl->set_location(@3);
1856
1857 $$ = $1;
1858 $$->declarations.push_tail(&decl->link);
1859 }
1860 ;
1861
1862 interpolation_qualifier:
1863 SMOOTH
1864 {
1865 memset(& $$, 0, sizeof($$));
1866 $$.flags.q.smooth = 1;
1867 }
1868 | FLAT
1869 {
1870 memset(& $$, 0, sizeof($$));
1871 $$.flags.q.flat = 1;
1872 }
1873 | NOPERSPECTIVE
1874 {
1875 memset(& $$, 0, sizeof($$));
1876 $$.flags.q.noperspective = 1;
1877 }
1878 ;
1879
1880 type_qualifier:
1881 /* Single qualifiers */
1882 INVARIANT
1883 {
1884 memset(& $$, 0, sizeof($$));
1885 $$.flags.q.invariant = 1;
1886 }
1887 | PRECISE
1888 {
1889 memset(& $$, 0, sizeof($$));
1890 $$.flags.q.precise = 1;
1891 }
1892 | auxiliary_storage_qualifier
1893 | storage_qualifier
1894 | interpolation_qualifier
1895 | layout_qualifier
1896 | memory_qualifier
1897 | subroutine_qualifier
1898 | precision_qualifier
1899 {
1900 memset(&$$, 0, sizeof($$));
1901 $$.precision = $1;
1902 }
1903
1904 /* Multiple qualifiers:
1905 * In GLSL 4.20, these can be specified in any order. In earlier versions,
1906 * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1907 *
1908 * invariant interpolation auxiliary storage precision ...or...
1909 * layout storage precision
1910 *
1911 * Each qualifier's rule ensures that the accumulated qualifiers on the right
1912 * side don't contain any that must appear on the left hand side.
1913 * For example, when processing a storage qualifier, we check that there are
1914 * no auxiliary, interpolation, layout, invariant, or precise qualifiers to the right.
1915 */
1916 | PRECISE type_qualifier
1917 {
1918 if ($2.flags.q.precise)
1919 _mesa_glsl_error(&@1, state, "duplicate \"precise\" qualifier");
1920
1921 $$ = $2;
1922 $$.flags.q.precise = 1;
1923 }
1924 | INVARIANT type_qualifier
1925 {
1926 if ($2.flags.q.invariant)
1927 _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1928
1929 if (!state->has_420pack_or_es31() && $2.flags.q.precise)
1930 _mesa_glsl_error(&@1, state,
1931 "\"invariant\" must come after \"precise\"");
1932
1933 $$ = $2;
1934 $$.flags.q.invariant = 1;
1935
1936 /* GLSL ES 3.00 spec, section 4.6.1 "The Invariant Qualifier":
1937 *
1938 * "Only variables output from a shader can be candidates for invariance.
1939 * This includes user-defined output variables and the built-in output
1940 * variables. As only outputs can be declared as invariant, an invariant
1941 * output from one shader stage will still match an input of a subsequent
1942 * stage without the input being declared as invariant."
1943 *
1944 * On the desktop side, this text first appears in GLSL 4.30.
1945 */
1946 if (state->is_version(430, 300) && $$.flags.q.in)
1947 _mesa_glsl_error(&@1, state, "invariant qualifiers cannot be used with shader inputs");
1948 }
1949 | interpolation_qualifier type_qualifier
1950 {
1951 /* Section 4.3 of the GLSL 1.40 specification states:
1952 * "...qualified with one of these interpolation qualifiers"
1953 *
1954 * GLSL 1.30 claims to allow "one or more", but insists that:
1955 * "These interpolation qualifiers may only precede the qualifiers in,
1956 * centroid in, out, or centroid out in a declaration."
1957 *
1958 * ...which means that e.g. smooth can't precede smooth, so there can be
1959 * only one after all, and the 1.40 text is a clarification, not a change.
1960 */
1961 if ($2.has_interpolation())
1962 _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1963
1964 if (!state->has_420pack_or_es31() &&
1965 ($2.flags.q.precise || $2.flags.q.invariant)) {
1966 _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1967 "after \"precise\" or \"invariant\"");
1968 }
1969
1970 $$ = $1;
1971 $$.merge_qualifier(&@1, state, $2, false);
1972 }
1973 | layout_qualifier type_qualifier
1974 {
1975 /* In the absence of ARB_shading_language_420pack, layout qualifiers may
1976 * appear no later than auxiliary storage qualifiers. There is no
1977 * particularly clear spec language mandating this, but in all examples
1978 * the layout qualifier precedes the storage qualifier.
1979 *
1980 * We allow combinations of layout with interpolation, invariant or
1981 * precise qualifiers since these are useful in ARB_separate_shader_objects.
1982 * There is no clear spec guidance on this either.
1983 */
1984 $$ = $1;
1985 $$.merge_qualifier(& @1, state, $2, false, $2.has_layout());
1986 }
1987 | subroutine_qualifier type_qualifier
1988 {
1989 $$ = $1;
1990 $$.merge_qualifier(&@1, state, $2, false);
1991 }
1992 | auxiliary_storage_qualifier type_qualifier
1993 {
1994 if ($2.has_auxiliary_storage()) {
1995 _mesa_glsl_error(&@1, state,
1996 "duplicate auxiliary storage qualifier (centroid or sample)");
1997 }
1998
1999 if (!state->has_420pack_or_es31() &&
2000 ($2.flags.q.precise || $2.flags.q.invariant ||
2001 $2.has_interpolation() || $2.has_layout())) {
2002 _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
2003 "just before storage qualifiers");
2004 }
2005 $$ = $1;
2006 $$.merge_qualifier(&@1, state, $2, false);
2007 }
2008 | storage_qualifier type_qualifier
2009 {
2010 /* Section 4.3 of the GLSL 1.20 specification states:
2011 * "Variable declarations may have a storage qualifier specified..."
2012 * 1.30 clarifies this to "may have one storage qualifier".
2013 */
2014 if ($2.has_storage())
2015 _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
2016
2017 if (!state->has_420pack_or_es31() &&
2018 ($2.flags.q.precise || $2.flags.q.invariant || $2.has_interpolation() ||
2019 $2.has_layout() || $2.has_auxiliary_storage())) {
2020 _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
2021 "precise, invariant, interpolation, layout and auxiliary "
2022 "storage qualifiers");
2023 }
2024
2025 $$ = $1;
2026 $$.merge_qualifier(&@1, state, $2, false);
2027 }
2028 | precision_qualifier type_qualifier
2029 {
2030 if ($2.precision != ast_precision_none)
2031 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
2032
2033 if (!(state->has_420pack_or_es31()) &&
2034 $2.flags.i != 0)
2035 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
2036
2037 $$ = $2;
2038 $$.precision = $1;
2039 }
2040 | memory_qualifier type_qualifier
2041 {
2042 $$ = $1;
2043 $$.merge_qualifier(&@1, state, $2, false);
2044 }
2045 ;
2046
2047 auxiliary_storage_qualifier:
2048 CENTROID
2049 {
2050 memset(& $$, 0, sizeof($$));
2051 $$.flags.q.centroid = 1;
2052 }
2053 | SAMPLE
2054 {
2055 memset(& $$, 0, sizeof($$));
2056 $$.flags.q.sample = 1;
2057 }
2058 | PATCH
2059 {
2060 memset(& $$, 0, sizeof($$));
2061 $$.flags.q.patch = 1;
2062 }
2063
2064 storage_qualifier:
2065 CONST_TOK
2066 {
2067 memset(& $$, 0, sizeof($$));
2068 $$.flags.q.constant = 1;
2069 }
2070 | ATTRIBUTE
2071 {
2072 memset(& $$, 0, sizeof($$));
2073 $$.flags.q.attribute = 1;
2074 }
2075 | VARYING
2076 {
2077 memset(& $$, 0, sizeof($$));
2078 $$.flags.q.varying = 1;
2079 }
2080 | IN_TOK
2081 {
2082 memset(& $$, 0, sizeof($$));
2083 $$.flags.q.in = 1;
2084 }
2085 | OUT_TOK
2086 {
2087 memset(& $$, 0, sizeof($$));
2088 $$.flags.q.out = 1;
2089
2090 if (state->stage == MESA_SHADER_GEOMETRY &&
2091 state->has_explicit_attrib_stream()) {
2092 /* Section 4.3.8.2 (Output Layout Qualifiers) of the GLSL 4.00
2093 * spec says:
2094 *
2095 * "If the block or variable is declared with the stream
2096 * identifier, it is associated with the specified stream;
2097 * otherwise, it is associated with the current default stream."
2098 */
2099 $$.flags.q.stream = 1;
2100 $$.flags.q.explicit_stream = 0;
2101 $$.stream = state->out_qualifier->stream;
2102 }
2103
2104 if (state->has_enhanced_layouts()) {
2105 $$.flags.q.xfb_buffer = 1;
2106 $$.flags.q.explicit_xfb_buffer = 0;
2107 $$.xfb_buffer = state->out_qualifier->xfb_buffer;
2108 }
2109 }
2110 | INOUT_TOK
2111 {
2112 memset(& $$, 0, sizeof($$));
2113 $$.flags.q.in = 1;
2114 $$.flags.q.out = 1;
2115
2116 if (!state->has_framebuffer_fetch() ||
2117 !state->is_version(130, 300) ||
2118 state->stage != MESA_SHADER_FRAGMENT)
2119 _mesa_glsl_error(&@1, state, "A single interface variable cannot be "
2120 "declared as both input and output");
2121 }
2122 | UNIFORM
2123 {
2124 memset(& $$, 0, sizeof($$));
2125 $$.flags.q.uniform = 1;
2126 }
2127 | BUFFER
2128 {
2129 memset(& $$, 0, sizeof($$));
2130 $$.flags.q.buffer = 1;
2131 }
2132 | SHARED
2133 {
2134 memset(& $$, 0, sizeof($$));
2135 $$.flags.q.shared_storage = 1;
2136 }
2137 ;
2138
2139 memory_qualifier:
2140 COHERENT
2141 {
2142 memset(& $$, 0, sizeof($$));
2143 $$.flags.q.coherent = 1;
2144 }
2145 | VOLATILE
2146 {
2147 memset(& $$, 0, sizeof($$));
2148 $$.flags.q._volatile = 1;
2149 }
2150 | RESTRICT
2151 {
2152 STATIC_ASSERT(sizeof($$.flags.q) <= sizeof($$.flags.i));
2153 memset(& $$, 0, sizeof($$));
2154 $$.flags.q.restrict_flag = 1;
2155 }
2156 | READONLY
2157 {
2158 memset(& $$, 0, sizeof($$));
2159 $$.flags.q.read_only = 1;
2160 }
2161 | WRITEONLY
2162 {
2163 memset(& $$, 0, sizeof($$));
2164 $$.flags.q.write_only = 1;
2165 }
2166 ;
2167
2168 array_specifier:
2169 '[' ']'
2170 {
2171 void *ctx = state->linalloc;
2172 $$ = new(ctx) ast_array_specifier(@1, new(ctx) ast_expression(
2173 ast_unsized_array_dim, NULL,
2174 NULL, NULL));
2175 $$->set_location_range(@1, @2);
2176 }
2177 | '[' constant_expression ']'
2178 {
2179 void *ctx = state->linalloc;
2180 $$ = new(ctx) ast_array_specifier(@1, $2);
2181 $$->set_location_range(@1, @3);
2182 }
2183 | array_specifier '[' ']'
2184 {
2185 void *ctx = state->linalloc;
2186 $$ = $1;
2187
2188 if (state->check_arrays_of_arrays_allowed(& @1)) {
2189 $$->add_dimension(new(ctx) ast_expression(ast_unsized_array_dim, NULL,
2190 NULL, NULL));
2191 }
2192 }
2193 | array_specifier '[' constant_expression ']'
2194 {
2195 $$ = $1;
2196
2197 if (state->check_arrays_of_arrays_allowed(& @1)) {
2198 $$->add_dimension($3);
2199 }
2200 }
2201 ;
2202
2203 type_specifier:
2204 type_specifier_nonarray
2205 | type_specifier_nonarray array_specifier
2206 {
2207 $$ = $1;
2208 $$->array_specifier = $2;
2209 }
2210 ;
2211
2212 type_specifier_nonarray:
2213 basic_type_specifier_nonarray
2214 {
2215 void *ctx = state->linalloc;
2216 $$ = new(ctx) ast_type_specifier($1);
2217 $$->set_location(@1);
2218 }
2219 | struct_specifier
2220 {
2221 void *ctx = state->linalloc;
2222 $$ = new(ctx) ast_type_specifier($1);
2223 $$->set_location(@1);
2224 }
2225 | TYPE_IDENTIFIER
2226 {
2227 void *ctx = state->linalloc;
2228 $$ = new(ctx) ast_type_specifier($1);
2229 $$->set_location(@1);
2230 }
2231 ;
2232
2233 basic_type_specifier_nonarray:
2234 VOID_TOK { $$ = "void"; }
2235 | FLOAT_TOK { $$ = "float"; }
2236 | DOUBLE_TOK { $$ = "double"; }
2237 | INT_TOK { $$ = "int"; }
2238 | UINT_TOK { $$ = "uint"; }
2239 | BOOL_TOK { $$ = "bool"; }
2240 | VEC2 { $$ = "vec2"; }
2241 | VEC3 { $$ = "vec3"; }
2242 | VEC4 { $$ = "vec4"; }
2243 | BVEC2 { $$ = "bvec2"; }
2244 | BVEC3 { $$ = "bvec3"; }
2245 | BVEC4 { $$ = "bvec4"; }
2246 | IVEC2 { $$ = "ivec2"; }
2247 | IVEC3 { $$ = "ivec3"; }
2248 | IVEC4 { $$ = "ivec4"; }
2249 | UVEC2 { $$ = "uvec2"; }
2250 | UVEC3 { $$ = "uvec3"; }
2251 | UVEC4 { $$ = "uvec4"; }
2252 | DVEC2 { $$ = "dvec2"; }
2253 | DVEC3 { $$ = "dvec3"; }
2254 | DVEC4 { $$ = "dvec4"; }
2255 | MAT2X2 { $$ = "mat2"; }
2256 | MAT2X3 { $$ = "mat2x3"; }
2257 | MAT2X4 { $$ = "mat2x4"; }
2258 | MAT3X2 { $$ = "mat3x2"; }
2259 | MAT3X3 { $$ = "mat3"; }
2260 | MAT3X4 { $$ = "mat3x4"; }
2261 | MAT4X2 { $$ = "mat4x2"; }
2262 | MAT4X3 { $$ = "mat4x3"; }
2263 | MAT4X4 { $$ = "mat4"; }
2264 | DMAT2X2 { $$ = "dmat2"; }
2265 | DMAT2X3 { $$ = "dmat2x3"; }
2266 | DMAT2X4 { $$ = "dmat2x4"; }
2267 | DMAT3X2 { $$ = "dmat3x2"; }
2268 | DMAT3X3 { $$ = "dmat3"; }
2269 | DMAT3X4 { $$ = "dmat3x4"; }
2270 | DMAT4X2 { $$ = "dmat4x2"; }
2271 | DMAT4X3 { $$ = "dmat4x3"; }
2272 | DMAT4X4 { $$ = "dmat4"; }
2273 | SAMPLER1D { $$ = "sampler1D"; }
2274 | SAMPLER2D { $$ = "sampler2D"; }
2275 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
2276 | SAMPLER3D { $$ = "sampler3D"; }
2277 | SAMPLERCUBE { $$ = "samplerCube"; }
2278 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
2279 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
2280 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
2281 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
2282 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
2283 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
2284 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
2285 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
2286 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
2287 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
2288 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
2289 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
2290 | ISAMPLER1D { $$ = "isampler1D"; }
2291 | ISAMPLER2D { $$ = "isampler2D"; }
2292 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
2293 | ISAMPLER3D { $$ = "isampler3D"; }
2294 | ISAMPLERCUBE { $$ = "isamplerCube"; }
2295 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
2296 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
2297 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
2298 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
2299 | USAMPLER1D { $$ = "usampler1D"; }
2300 | USAMPLER2D { $$ = "usampler2D"; }
2301 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
2302 | USAMPLER3D { $$ = "usampler3D"; }
2303 | USAMPLERCUBE { $$ = "usamplerCube"; }
2304 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
2305 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
2306 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
2307 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
2308 | SAMPLER2DMS { $$ = "sampler2DMS"; }
2309 | ISAMPLER2DMS { $$ = "isampler2DMS"; }
2310 | USAMPLER2DMS { $$ = "usampler2DMS"; }
2311 | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
2312 | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
2313 | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
2314 | IMAGE1D { $$ = "image1D"; }
2315 | IMAGE2D { $$ = "image2D"; }
2316 | IMAGE3D { $$ = "image3D"; }
2317 | IMAGE2DRECT { $$ = "image2DRect"; }
2318 | IMAGECUBE { $$ = "imageCube"; }
2319 | IMAGEBUFFER { $$ = "imageBuffer"; }
2320 | IMAGE1DARRAY { $$ = "image1DArray"; }
2321 | IMAGE2DARRAY { $$ = "image2DArray"; }
2322 | IMAGECUBEARRAY { $$ = "imageCubeArray"; }
2323 | IMAGE2DMS { $$ = "image2DMS"; }
2324 | IMAGE2DMSARRAY { $$ = "image2DMSArray"; }
2325 | IIMAGE1D { $$ = "iimage1D"; }
2326 | IIMAGE2D { $$ = "iimage2D"; }
2327 | IIMAGE3D { $$ = "iimage3D"; }
2328 | IIMAGE2DRECT { $$ = "iimage2DRect"; }
2329 | IIMAGECUBE { $$ = "iimageCube"; }
2330 | IIMAGEBUFFER { $$ = "iimageBuffer"; }
2331 | IIMAGE1DARRAY { $$ = "iimage1DArray"; }
2332 | IIMAGE2DARRAY { $$ = "iimage2DArray"; }
2333 | IIMAGECUBEARRAY { $$ = "iimageCubeArray"; }
2334 | IIMAGE2DMS { $$ = "iimage2DMS"; }
2335 | IIMAGE2DMSARRAY { $$ = "iimage2DMSArray"; }
2336 | UIMAGE1D { $$ = "uimage1D"; }
2337 | UIMAGE2D { $$ = "uimage2D"; }
2338 | UIMAGE3D { $$ = "uimage3D"; }
2339 | UIMAGE2DRECT { $$ = "uimage2DRect"; }
2340 | UIMAGECUBE { $$ = "uimageCube"; }
2341 | UIMAGEBUFFER { $$ = "uimageBuffer"; }
2342 | UIMAGE1DARRAY { $$ = "uimage1DArray"; }
2343 | UIMAGE2DARRAY { $$ = "uimage2DArray"; }
2344 | UIMAGECUBEARRAY { $$ = "uimageCubeArray"; }
2345 | UIMAGE2DMS { $$ = "uimage2DMS"; }
2346 | UIMAGE2DMSARRAY { $$ = "uimage2DMSArray"; }
2347 | ATOMIC_UINT { $$ = "atomic_uint"; }
2348 | INT64_TOK { $$ = "int64_t"; }
2349 | I64VEC2 { $$ = "i64vec2"; }
2350 | I64VEC3 { $$ = "i64vec3"; }
2351 | I64VEC4 { $$ = "i64vec4"; }
2352 | UINT64_TOK { $$ = "uint64_t"; }
2353 | U64VEC2 { $$ = "u64vec2"; }
2354 | U64VEC3 { $$ = "u64vec3"; }
2355 | U64VEC4 { $$ = "u64vec4"; }
2356 ;
2357
2358 precision_qualifier:
2359 HIGHP
2360 {
2361 state->check_precision_qualifiers_allowed(&@1);
2362 $$ = ast_precision_high;
2363 }
2364 | MEDIUMP
2365 {
2366 state->check_precision_qualifiers_allowed(&@1);
2367 $$ = ast_precision_medium;
2368 }
2369 | LOWP
2370 {
2371 state->check_precision_qualifiers_allowed(&@1);
2372 $$ = ast_precision_low;
2373 }
2374 ;
2375
2376 struct_specifier:
2377 STRUCT any_identifier '{' struct_declaration_list '}'
2378 {
2379 void *ctx = state->linalloc;
2380 $$ = new(ctx) ast_struct_specifier(ctx, $2, $4);
2381 $$->set_location_range(@2, @5);
2382 state->symbols->add_type($2, glsl_type::void_type);
2383 }
2384 | STRUCT '{' struct_declaration_list '}'
2385 {
2386 void *ctx = state->linalloc;
2387 $$ = new(ctx) ast_struct_specifier(ctx, NULL, $3);
2388 $$->set_location_range(@2, @4);
2389 }
2390 ;
2391
2392 struct_declaration_list:
2393 struct_declaration
2394 {
2395 $$ = $1;
2396 $1->link.self_link();
2397 }
2398 | struct_declaration_list struct_declaration
2399 {
2400 $$ = $1;
2401 $$->link.insert_before(& $2->link);
2402 }
2403 ;
2404
2405 struct_declaration:
2406 fully_specified_type struct_declarator_list ';'
2407 {
2408 void *ctx = state->linalloc;
2409 ast_fully_specified_type *const type = $1;
2410 type->set_location(@1);
2411
2412 if (state->has_bindless()) {
2413 ast_type_qualifier input_layout_mask;
2414
2415 /* Allow to declare qualifiers for images. */
2416 input_layout_mask.flags.i = 0;
2417 input_layout_mask.flags.q.coherent = 1;
2418 input_layout_mask.flags.q._volatile = 1;
2419 input_layout_mask.flags.q.restrict_flag = 1;
2420 input_layout_mask.flags.q.read_only = 1;
2421 input_layout_mask.flags.q.write_only = 1;
2422 input_layout_mask.flags.q.explicit_image_format = 1;
2423
2424 if ((type->qualifier.flags.i & ~input_layout_mask.flags.i) != 0) {
2425 _mesa_glsl_error(&@1, state,
2426 "only precision and image qualifiers may be "
2427 "applied to structure members");
2428 }
2429 } else {
2430 if (type->qualifier.flags.i != 0)
2431 _mesa_glsl_error(&@1, state,
2432 "only precision qualifiers may be applied to "
2433 "structure members");
2434 }
2435
2436 $$ = new(ctx) ast_declarator_list(type);
2437 $$->set_location(@2);
2438
2439 $$->declarations.push_degenerate_list_at_head(& $2->link);
2440 }
2441 ;
2442
2443 struct_declarator_list:
2444 struct_declarator
2445 {
2446 $$ = $1;
2447 $1->link.self_link();
2448 }
2449 | struct_declarator_list ',' struct_declarator
2450 {
2451 $$ = $1;
2452 $$->link.insert_before(& $3->link);
2453 }
2454 ;
2455
2456 struct_declarator:
2457 any_identifier
2458 {
2459 void *ctx = state->linalloc;
2460 $$ = new(ctx) ast_declaration($1, NULL, NULL);
2461 $$->set_location(@1);
2462 }
2463 | any_identifier array_specifier
2464 {
2465 void *ctx = state->linalloc;
2466 $$ = new(ctx) ast_declaration($1, $2, NULL);
2467 $$->set_location_range(@1, @2);
2468 }
2469 ;
2470
2471 initializer:
2472 assignment_expression
2473 | '{' initializer_list '}'
2474 {
2475 $$ = $2;
2476 }
2477 | '{' initializer_list ',' '}'
2478 {
2479 $$ = $2;
2480 }
2481 ;
2482
2483 initializer_list:
2484 initializer
2485 {
2486 void *ctx = state->linalloc;
2487 $$ = new(ctx) ast_aggregate_initializer();
2488 $$->set_location(@1);
2489 $$->expressions.push_tail(& $1->link);
2490 }
2491 | initializer_list ',' initializer
2492 {
2493 $1->expressions.push_tail(& $3->link);
2494 }
2495 ;
2496
2497 declaration_statement:
2498 declaration
2499 ;
2500
2501 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
2502 // supported.
2503 statement:
2504 compound_statement { $$ = (ast_node *) $1; }
2505 | simple_statement
2506 ;
2507
2508 simple_statement:
2509 declaration_statement
2510 | expression_statement
2511 | selection_statement
2512 | switch_statement
2513 | iteration_statement
2514 | jump_statement
2515 ;
2516
2517 compound_statement:
2518 '{' '}'
2519 {
2520 void *ctx = state->linalloc;
2521 $$ = new(ctx) ast_compound_statement(true, NULL);
2522 $$->set_location_range(@1, @2);
2523 }
2524 | '{'
2525 {
2526 state->symbols->push_scope();
2527 }
2528 statement_list '}'
2529 {
2530 void *ctx = state->linalloc;
2531 $$ = new(ctx) ast_compound_statement(true, $3);
2532 $$->set_location_range(@1, @4);
2533 state->symbols->pop_scope();
2534 }
2535 ;
2536
2537 statement_no_new_scope:
2538 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
2539 | simple_statement
2540 ;
2541
2542 compound_statement_no_new_scope:
2543 '{' '}'
2544 {
2545 void *ctx = state->linalloc;
2546 $$ = new(ctx) ast_compound_statement(false, NULL);
2547 $$->set_location_range(@1, @2);
2548 }
2549 | '{' statement_list '}'
2550 {
2551 void *ctx = state->linalloc;
2552 $$ = new(ctx) ast_compound_statement(false, $2);
2553 $$->set_location_range(@1, @3);
2554 }
2555 ;
2556
2557 statement_list:
2558 statement
2559 {
2560 if ($1 == NULL) {
2561 _mesa_glsl_error(& @1, state, "<nil> statement");
2562 assert($1 != NULL);
2563 }
2564
2565 $$ = $1;
2566 $$->link.self_link();
2567 }
2568 | statement_list statement
2569 {
2570 if ($2 == NULL) {
2571 _mesa_glsl_error(& @2, state, "<nil> statement");
2572 assert($2 != NULL);
2573 }
2574 $$ = $1;
2575 $$->link.insert_before(& $2->link);
2576 }
2577 ;
2578
2579 expression_statement:
2580 ';'
2581 {
2582 void *ctx = state->linalloc;
2583 $$ = new(ctx) ast_expression_statement(NULL);
2584 $$->set_location(@1);
2585 }
2586 | expression ';'
2587 {
2588 void *ctx = state->linalloc;
2589 $$ = new(ctx) ast_expression_statement($1);
2590 $$->set_location(@1);
2591 }
2592 ;
2593
2594 selection_statement:
2595 IF '(' expression ')' selection_rest_statement
2596 {
2597 $$ = new(state->linalloc) ast_selection_statement($3, $5.then_statement,
2598 $5.else_statement);
2599 $$->set_location_range(@1, @5);
2600 }
2601 ;
2602
2603 selection_rest_statement:
2604 statement ELSE statement
2605 {
2606 $$.then_statement = $1;
2607 $$.else_statement = $3;
2608 }
2609 | statement %prec THEN
2610 {
2611 $$.then_statement = $1;
2612 $$.else_statement = NULL;
2613 }
2614 ;
2615
2616 condition:
2617 expression
2618 {
2619 $$ = (ast_node *) $1;
2620 }
2621 | fully_specified_type any_identifier '=' initializer
2622 {
2623 void *ctx = state->linalloc;
2624 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
2625 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
2626 decl->set_location_range(@2, @4);
2627 declarator->set_location(@1);
2628
2629 declarator->declarations.push_tail(&decl->link);
2630 $$ = declarator;
2631 }
2632 ;
2633
2634 /*
2635 * switch_statement grammar is based on the syntax described in the body
2636 * of the GLSL spec, not in it's appendix!!!
2637 */
2638 switch_statement:
2639 SWITCH '(' expression ')' switch_body
2640 {
2641 $$ = new(state->linalloc) ast_switch_statement($3, $5);
2642 $$->set_location_range(@1, @5);
2643 }
2644 ;
2645
2646 switch_body:
2647 '{' '}'
2648 {
2649 $$ = new(state->linalloc) ast_switch_body(NULL);
2650 $$->set_location_range(@1, @2);
2651 }
2652 | '{' case_statement_list '}'
2653 {
2654 $$ = new(state->linalloc) ast_switch_body($2);
2655 $$->set_location_range(@1, @3);
2656 }
2657 ;
2658
2659 case_label:
2660 CASE expression ':'
2661 {
2662 $$ = new(state->linalloc) ast_case_label($2);
2663 $$->set_location(@2);
2664 }
2665 | DEFAULT ':'
2666 {
2667 $$ = new(state->linalloc) ast_case_label(NULL);
2668 $$->set_location(@2);
2669 }
2670 ;
2671
2672 case_label_list:
2673 case_label
2674 {
2675 ast_case_label_list *labels = new(state->linalloc) ast_case_label_list();
2676
2677 labels->labels.push_tail(& $1->link);
2678 $$ = labels;
2679 $$->set_location(@1);
2680 }
2681 | case_label_list case_label
2682 {
2683 $$ = $1;
2684 $$->labels.push_tail(& $2->link);
2685 }
2686 ;
2687
2688 case_statement:
2689 case_label_list statement
2690 {
2691 ast_case_statement *stmts = new(state->linalloc) ast_case_statement($1);
2692 stmts->set_location(@2);
2693
2694 stmts->stmts.push_tail(& $2->link);
2695 $$ = stmts;
2696 }
2697 | case_statement statement
2698 {
2699 $$ = $1;
2700 $$->stmts.push_tail(& $2->link);
2701 }
2702 ;
2703
2704 case_statement_list:
2705 case_statement
2706 {
2707 ast_case_statement_list *cases= new(state->linalloc) ast_case_statement_list();
2708 cases->set_location(@1);
2709
2710 cases->cases.push_tail(& $1->link);
2711 $$ = cases;
2712 }
2713 | case_statement_list case_statement
2714 {
2715 $$ = $1;
2716 $$->cases.push_tail(& $2->link);
2717 }
2718 ;
2719
2720 iteration_statement:
2721 WHILE '(' condition ')' statement_no_new_scope
2722 {
2723 void *ctx = state->linalloc;
2724 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2725 NULL, $3, NULL, $5);
2726 $$->set_location_range(@1, @4);
2727 }
2728 | DO statement WHILE '(' expression ')' ';'
2729 {
2730 void *ctx = state->linalloc;
2731 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2732 NULL, $5, NULL, $2);
2733 $$->set_location_range(@1, @6);
2734 }
2735 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2736 {
2737 void *ctx = state->linalloc;
2738 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2739 $3, $4.cond, $4.rest, $6);
2740 $$->set_location_range(@1, @6);
2741 }
2742 ;
2743
2744 for_init_statement:
2745 expression_statement
2746 | declaration_statement
2747 ;
2748
2749 conditionopt:
2750 condition
2751 | /* empty */
2752 {
2753 $$ = NULL;
2754 }
2755 ;
2756
2757 for_rest_statement:
2758 conditionopt ';'
2759 {
2760 $$.cond = $1;
2761 $$.rest = NULL;
2762 }
2763 | conditionopt ';' expression
2764 {
2765 $$.cond = $1;
2766 $$.rest = $3;
2767 }
2768 ;
2769
2770 // Grammar Note: No 'goto'. Gotos are not supported.
2771 jump_statement:
2772 CONTINUE ';'
2773 {
2774 void *ctx = state->linalloc;
2775 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2776 $$->set_location(@1);
2777 }
2778 | BREAK ';'
2779 {
2780 void *ctx = state->linalloc;
2781 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2782 $$->set_location(@1);
2783 }
2784 | RETURN ';'
2785 {
2786 void *ctx = state->linalloc;
2787 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2788 $$->set_location(@1);
2789 }
2790 | RETURN expression ';'
2791 {
2792 void *ctx = state->linalloc;
2793 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2794 $$->set_location_range(@1, @2);
2795 }
2796 | DISCARD ';' // Fragment shader only.
2797 {
2798 void *ctx = state->linalloc;
2799 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2800 $$->set_location(@1);
2801 }
2802 ;
2803
2804 external_declaration:
2805 function_definition { $$ = $1; }
2806 | declaration { $$ = $1; }
2807 | pragma_statement { $$ = NULL; }
2808 | layout_defaults { $$ = $1; }
2809 ;
2810
2811 function_definition:
2812 function_prototype compound_statement_no_new_scope
2813 {
2814 void *ctx = state->linalloc;
2815 $$ = new(ctx) ast_function_definition();
2816 $$->set_location_range(@1, @2);
2817 $$->prototype = $1;
2818 $$->body = $2;
2819
2820 state->symbols->pop_scope();
2821 }
2822 ;
2823
2824 /* layout_qualifieropt is packed into this rule */
2825 interface_block:
2826 basic_interface_block
2827 {
2828 $$ = $1;
2829 }
2830 | layout_qualifier interface_block
2831 {
2832 ast_interface_block *block = (ast_interface_block *) $2;
2833
2834 if (!$1.merge_qualifier(& @1, state, block->layout, false,
2835 block->layout.has_layout())) {
2836 YYERROR;
2837 }
2838
2839 block->layout = $1;
2840
2841 $$ = block;
2842 }
2843 | memory_qualifier interface_block
2844 {
2845 ast_interface_block *block = (ast_interface_block *)$2;
2846
2847 if (!block->default_layout.flags.q.buffer) {
2848 _mesa_glsl_error(& @1, state,
2849 "memory qualifiers can only be used in the "
2850 "declaration of shader storage blocks");
2851 }
2852 if (!$1.merge_qualifier(& @1, state, block->layout, false)) {
2853 YYERROR;
2854 }
2855 block->layout = $1;
2856 $$ = block;
2857 }
2858 ;
2859
2860 basic_interface_block:
2861 interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2862 {
2863 ast_interface_block *const block = $6;
2864
2865 if ($1.flags.q.uniform) {
2866 block->default_layout = *state->default_uniform_qualifier;
2867 } else if ($1.flags.q.buffer) {
2868 block->default_layout = *state->default_shader_storage_qualifier;
2869 }
2870 block->block_name = $2;
2871 block->declarations.push_degenerate_list_at_head(& $4->link);
2872
2873 _mesa_ast_process_interface_block(& @1, state, block, $1);
2874
2875 $$ = block;
2876 }
2877 ;
2878
2879 interface_qualifier:
2880 IN_TOK
2881 {
2882 memset(& $$, 0, sizeof($$));
2883 $$.flags.q.in = 1;
2884 }
2885 | OUT_TOK
2886 {
2887 memset(& $$, 0, sizeof($$));
2888 $$.flags.q.out = 1;
2889 }
2890 | UNIFORM
2891 {
2892 memset(& $$, 0, sizeof($$));
2893 $$.flags.q.uniform = 1;
2894 }
2895 | BUFFER
2896 {
2897 memset(& $$, 0, sizeof($$));
2898 $$.flags.q.buffer = 1;
2899 }
2900 | auxiliary_storage_qualifier interface_qualifier
2901 {
2902 if (!$1.flags.q.patch) {
2903 _mesa_glsl_error(&@1, state, "invalid interface qualifier");
2904 }
2905 if ($2.has_auxiliary_storage()) {
2906 _mesa_glsl_error(&@1, state, "duplicate patch qualifier");
2907 }
2908 $$ = $2;
2909 $$.flags.q.patch = 1;
2910 }
2911 ;
2912
2913 instance_name_opt:
2914 /* empty */
2915 {
2916 $$ = new(state->linalloc) ast_interface_block(NULL, NULL);
2917 }
2918 | NEW_IDENTIFIER
2919 {
2920 $$ = new(state->linalloc) ast_interface_block($1, NULL);
2921 $$->set_location(@1);
2922 }
2923 | NEW_IDENTIFIER array_specifier
2924 {
2925 $$ = new(state->linalloc) ast_interface_block($1, $2);
2926 $$->set_location_range(@1, @2);
2927 }
2928 ;
2929
2930 member_list:
2931 member_declaration
2932 {
2933 $$ = $1;
2934 $1->link.self_link();
2935 }
2936 | member_declaration member_list
2937 {
2938 $$ = $1;
2939 $2->link.insert_before(& $$->link);
2940 }
2941 ;
2942
2943 member_declaration:
2944 fully_specified_type struct_declarator_list ';'
2945 {
2946 void *ctx = state->linalloc;
2947 ast_fully_specified_type *type = $1;
2948 type->set_location(@1);
2949
2950 if (type->qualifier.flags.q.attribute) {
2951 _mesa_glsl_error(& @1, state,
2952 "keyword 'attribute' cannot be used with "
2953 "interface block member");
2954 } else if (type->qualifier.flags.q.varying) {
2955 _mesa_glsl_error(& @1, state,
2956 "keyword 'varying' cannot be used with "
2957 "interface block member");
2958 }
2959
2960 $$ = new(ctx) ast_declarator_list(type);
2961 $$->set_location(@2);
2962
2963 $$->declarations.push_degenerate_list_at_head(& $2->link);
2964 }
2965 ;
2966
2967 layout_uniform_defaults:
2968 layout_qualifier layout_uniform_defaults
2969 {
2970 $$ = $1;
2971 if (!$$.merge_qualifier(& @1, state, $2, false, true)) {
2972 YYERROR;
2973 }
2974 }
2975 | layout_qualifier UNIFORM ';'
2976 ;
2977
2978 layout_buffer_defaults:
2979 layout_qualifier layout_buffer_defaults
2980 {
2981 $$ = $1;
2982 if (!$$.merge_qualifier(& @1, state, $2, false, true)) {
2983 YYERROR;
2984 }
2985 }
2986 | layout_qualifier BUFFER ';'
2987 ;
2988
2989 layout_in_defaults:
2990 layout_qualifier layout_in_defaults
2991 {
2992 $$ = $1;
2993 if (!$$.merge_qualifier(& @1, state, $2, false, true)) {
2994 YYERROR;
2995 }
2996 if (!$$.validate_in_qualifier(& @1, state)) {
2997 YYERROR;
2998 }
2999 }
3000 | layout_qualifier IN_TOK ';'
3001 {
3002 if (!$1.validate_in_qualifier(& @1, state)) {
3003 YYERROR;
3004 }
3005 }
3006 ;
3007
3008 layout_out_defaults:
3009 layout_qualifier layout_out_defaults
3010 {
3011 $$ = $1;
3012 if (!$$.merge_qualifier(& @1, state, $2, false, true)) {
3013 YYERROR;
3014 }
3015 if (!$$.validate_out_qualifier(& @1, state)) {
3016 YYERROR;
3017 }
3018 }
3019 | layout_qualifier OUT_TOK ';'
3020 {
3021 if (!$1.validate_out_qualifier(& @1, state)) {
3022 YYERROR;
3023 }
3024 }
3025 ;
3026
3027 layout_defaults:
3028 layout_uniform_defaults
3029 {
3030 $$ = NULL;
3031 if (!state->default_uniform_qualifier->
3032 merge_qualifier(& @1, state, $1, false)) {
3033 YYERROR;
3034 }
3035 if (!state->default_uniform_qualifier->
3036 push_to_global(& @1, state)) {
3037 YYERROR;
3038 }
3039 }
3040 | layout_buffer_defaults
3041 {
3042 $$ = NULL;
3043 if (!state->default_shader_storage_qualifier->
3044 merge_qualifier(& @1, state, $1, false)) {
3045 YYERROR;
3046 }
3047 if (!state->default_shader_storage_qualifier->
3048 push_to_global(& @1, state)) {
3049 YYERROR;
3050 }
3051
3052 /* From the GLSL 4.50 spec, section 4.4.5:
3053 *
3054 * "It is a compile-time error to specify the binding identifier for
3055 * the global scope or for block member declarations."
3056 */
3057 if (state->default_shader_storage_qualifier->flags.q.explicit_binding) {
3058 _mesa_glsl_error(& @1, state,
3059 "binding qualifier cannot be set for default layout");
3060 }
3061 }
3062 | layout_in_defaults
3063 {
3064 $$ = NULL;
3065 if (!$1.merge_into_in_qualifier(& @1, state, $$)) {
3066 YYERROR;
3067 }
3068 if (!state->in_qualifier->push_to_global(& @1, state)) {
3069 YYERROR;
3070 }
3071 }
3072 | layout_out_defaults
3073 {
3074 $$ = NULL;
3075 if (!$1.merge_into_out_qualifier(& @1, state, $$)) {
3076 YYERROR;
3077 }
3078 if (!state->out_qualifier->push_to_global(& @1, state)) {
3079 YYERROR;
3080 }
3081 }
3082 ;