db29165518070eace280ccd9d21f28966a9285ee
[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.flags.q.subroutine) {
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_any = 1;
1231 } else if (match_layout_qualifier($1, "depth_greater", state) == 0) {
1232 $$.flags.q.depth_greater = 1;
1233 } else if (match_layout_qualifier($1, "depth_less", state) == 0) {
1234 $$.flags.q.depth_less = 1;
1235 } else if (match_layout_qualifier($1, "depth_unchanged",
1236 state) == 0) {
1237 $$.flags.q.depth_unchanged = 1;
1238 }
1239
1240 if ($$.flags.i && state->AMD_conservative_depth_warn) {
1241 _mesa_glsl_warning(& @1, state,
1242 "GL_AMD_conservative_depth "
1243 "layout qualifier `%s' is used", $1);
1244 }
1245 if ($$.flags.i && state->ARB_conservative_depth_warn) {
1246 _mesa_glsl_warning(& @1, state,
1247 "GL_ARB_conservative_depth "
1248 "layout qualifier `%s' is used", $1);
1249 }
1250 }
1251
1252 /* See also interface_block_layout_qualifier. */
1253 if (!$$.flags.i && state->has_uniform_buffer_objects()) {
1254 if (match_layout_qualifier($1, "std140", state) == 0) {
1255 $$.flags.q.std140 = 1;
1256 } else if (match_layout_qualifier($1, "shared", state) == 0) {
1257 $$.flags.q.shared = 1;
1258 } else if (match_layout_qualifier($1, "std430", state) == 0) {
1259 $$.flags.q.std430 = 1;
1260 } else if (match_layout_qualifier($1, "column_major", state) == 0) {
1261 $$.flags.q.column_major = 1;
1262 /* "row_major" is a reserved word in GLSL 1.30+. Its token is parsed
1263 * below in the interface_block_layout_qualifier rule.
1264 *
1265 * It is not a reserved word in GLSL ES 3.00, so it's handled here as
1266 * an identifier.
1267 *
1268 * Also, this takes care of alternate capitalizations of
1269 * "row_major" (which is necessary because layout qualifiers
1270 * are case-insensitive in desktop GLSL).
1271 */
1272 } else if (match_layout_qualifier($1, "row_major", state) == 0) {
1273 $$.flags.q.row_major = 1;
1274 /* "packed" is a reserved word in GLSL, and its token is
1275 * parsed below in the interface_block_layout_qualifier rule.
1276 * However, we must take care of alternate capitalizations of
1277 * "packed", because layout qualifiers are case-insensitive
1278 * in desktop GLSL.
1279 */
1280 } else if (match_layout_qualifier($1, "packed", state) == 0) {
1281 $$.flags.q.packed = 1;
1282 }
1283
1284 if ($$.flags.i && state->ARB_uniform_buffer_object_warn) {
1285 _mesa_glsl_warning(& @1, state,
1286 "#version 140 / GL_ARB_uniform_buffer_object "
1287 "layout qualifier `%s' is used", $1);
1288 }
1289 }
1290
1291 /* Layout qualifiers for GLSL 1.50 geometry shaders. */
1292 if (!$$.flags.i) {
1293 static const struct {
1294 const char *s;
1295 GLenum e;
1296 } map[] = {
1297 { "points", GL_POINTS },
1298 { "lines", GL_LINES },
1299 { "lines_adjacency", GL_LINES_ADJACENCY },
1300 { "line_strip", GL_LINE_STRIP },
1301 { "triangles", GL_TRIANGLES },
1302 { "triangles_adjacency", GL_TRIANGLES_ADJACENCY },
1303 { "triangle_strip", GL_TRIANGLE_STRIP },
1304 };
1305 for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
1306 if (match_layout_qualifier($1, map[i].s, state) == 0) {
1307 $$.flags.q.prim_type = 1;
1308 $$.prim_type = map[i].e;
1309 break;
1310 }
1311 }
1312
1313 if ($$.flags.i && !state->has_geometry_shader() &&
1314 !state->has_tessellation_shader()) {
1315 _mesa_glsl_error(& @1, state, "#version 150 layout "
1316 "qualifier `%s' used", $1);
1317 }
1318 }
1319
1320 /* Layout qualifiers for ARB_shader_image_load_store. */
1321 if (state->ARB_shader_image_load_store_enable ||
1322 state->is_version(420, 310)) {
1323 if (!$$.flags.i) {
1324 static const struct {
1325 const char *name;
1326 GLenum format;
1327 glsl_base_type base_type;
1328 /** Minimum desktop GLSL version required for the image
1329 * format. Use 130 if already present in the original
1330 * ARB extension.
1331 */
1332 unsigned required_glsl;
1333 /** Minimum GLSL ES version required for the image format. */
1334 unsigned required_essl;
1335 /* NV_image_formats */
1336 bool nv_image_formats;
1337 } map[] = {
1338 { "rgba32f", GL_RGBA32F, GLSL_TYPE_FLOAT, 130, 310, false },
1339 { "rgba16f", GL_RGBA16F, GLSL_TYPE_FLOAT, 130, 310, false },
1340 { "rg32f", GL_RG32F, GLSL_TYPE_FLOAT, 130, 0, true },
1341 { "rg16f", GL_RG16F, GLSL_TYPE_FLOAT, 130, 0, true },
1342 { "r11f_g11f_b10f", GL_R11F_G11F_B10F, GLSL_TYPE_FLOAT, 130, 0, true },
1343 { "r32f", GL_R32F, GLSL_TYPE_FLOAT, 130, 310, false },
1344 { "r16f", GL_R16F, GLSL_TYPE_FLOAT, 130, 0, true },
1345 { "rgba32ui", GL_RGBA32UI, GLSL_TYPE_UINT, 130, 310, false },
1346 { "rgba16ui", GL_RGBA16UI, GLSL_TYPE_UINT, 130, 310, false },
1347 { "rgb10_a2ui", GL_RGB10_A2UI, GLSL_TYPE_UINT, 130, 0, true },
1348 { "rgba8ui", GL_RGBA8UI, GLSL_TYPE_UINT, 130, 310, false },
1349 { "rg32ui", GL_RG32UI, GLSL_TYPE_UINT, 130, 0, true },
1350 { "rg16ui", GL_RG16UI, GLSL_TYPE_UINT, 130, 0, true },
1351 { "rg8ui", GL_RG8UI, GLSL_TYPE_UINT, 130, 0, true },
1352 { "r32ui", GL_R32UI, GLSL_TYPE_UINT, 130, 310, false },
1353 { "r16ui", GL_R16UI, GLSL_TYPE_UINT, 130, 0, true },
1354 { "r8ui", GL_R8UI, GLSL_TYPE_UINT, 130, 0, true },
1355 { "rgba32i", GL_RGBA32I, GLSL_TYPE_INT, 130, 310, false },
1356 { "rgba16i", GL_RGBA16I, GLSL_TYPE_INT, 130, 310, false },
1357 { "rgba8i", GL_RGBA8I, GLSL_TYPE_INT, 130, 310, false },
1358 { "rg32i", GL_RG32I, GLSL_TYPE_INT, 130, 0, true },
1359 { "rg16i", GL_RG16I, GLSL_TYPE_INT, 130, 0, true },
1360 { "rg8i", GL_RG8I, GLSL_TYPE_INT, 130, 0, true },
1361 { "r32i", GL_R32I, GLSL_TYPE_INT, 130, 310, false },
1362 { "r16i", GL_R16I, GLSL_TYPE_INT, 130, 0, true },
1363 { "r8i", GL_R8I, GLSL_TYPE_INT, 130, 0, true },
1364 { "rgba16", GL_RGBA16, GLSL_TYPE_FLOAT, 130, 0, false },
1365 { "rgb10_a2", GL_RGB10_A2, GLSL_TYPE_FLOAT, 130, 0, true },
1366 { "rgba8", GL_RGBA8, GLSL_TYPE_FLOAT, 130, 310, false },
1367 { "rg16", GL_RG16, GLSL_TYPE_FLOAT, 130, 0, false },
1368 { "rg8", GL_RG8, GLSL_TYPE_FLOAT, 130, 0, true },
1369 { "r16", GL_R16, GLSL_TYPE_FLOAT, 130, 0, false },
1370 { "r8", GL_R8, GLSL_TYPE_FLOAT, 130, 0, true },
1371 { "rgba16_snorm", GL_RGBA16_SNORM, GLSL_TYPE_FLOAT, 130, 0, false },
1372 { "rgba8_snorm", GL_RGBA8_SNORM, GLSL_TYPE_FLOAT, 130, 310, false },
1373 { "rg16_snorm", GL_RG16_SNORM, GLSL_TYPE_FLOAT, 130, 0, false },
1374 { "rg8_snorm", GL_RG8_SNORM, GLSL_TYPE_FLOAT, 130, 0, true },
1375 { "r16_snorm", GL_R16_SNORM, GLSL_TYPE_FLOAT, 130, 0, false },
1376 { "r8_snorm", GL_R8_SNORM, GLSL_TYPE_FLOAT, 130, 0, true }
1377 };
1378
1379 for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
1380 if ((state->is_version(map[i].required_glsl,
1381 map[i].required_essl) ||
1382 (state->NV_image_formats_enable &&
1383 map[i].nv_image_formats)) &&
1384 match_layout_qualifier($1, map[i].name, state) == 0) {
1385 $$.flags.q.explicit_image_format = 1;
1386 $$.image_format = map[i].format;
1387 $$.image_base_type = map[i].base_type;
1388 break;
1389 }
1390 }
1391 }
1392 }
1393
1394 if (!$$.flags.i) {
1395 if (match_layout_qualifier($1, "early_fragment_tests", state) == 0) {
1396 /* From section 4.4.1.3 of the GLSL 4.50 specification
1397 * (Fragment Shader Inputs):
1398 *
1399 * "Fragment shaders also allow the following layout
1400 * qualifier on in only (not with variable declarations)
1401 * layout-qualifier-id
1402 * early_fragment_tests
1403 * [...]"
1404 */
1405 if (state->stage != MESA_SHADER_FRAGMENT) {
1406 _mesa_glsl_error(& @1, state,
1407 "early_fragment_tests layout qualifier only "
1408 "valid in fragment shaders");
1409 }
1410
1411 $$.flags.q.early_fragment_tests = 1;
1412 }
1413
1414 if (match_layout_qualifier($1, "inner_coverage", state) == 0) {
1415 if (state->stage != MESA_SHADER_FRAGMENT) {
1416 _mesa_glsl_error(& @1, state,
1417 "inner_coverage layout qualifier only "
1418 "valid in fragment shaders");
1419 }
1420
1421 if (state->INTEL_conservative_rasterization_enable) {
1422 $$.flags.q.inner_coverage = 1;
1423 } else {
1424 _mesa_glsl_error(& @1, state,
1425 "inner_coverage layout qualifier present, "
1426 "but the INTEL_conservative_rasterization extension "
1427 "is not enabled.");
1428 }
1429 }
1430
1431 if (match_layout_qualifier($1, "post_depth_coverage", state) == 0) {
1432 if (state->stage != MESA_SHADER_FRAGMENT) {
1433 _mesa_glsl_error(& @1, state,
1434 "post_depth_coverage layout qualifier only "
1435 "valid in fragment shaders");
1436 }
1437
1438 if (state->ARB_post_depth_coverage_enable ||
1439 state->INTEL_conservative_rasterization_enable) {
1440 $$.flags.q.post_depth_coverage = 1;
1441 } else {
1442 _mesa_glsl_error(& @1, state,
1443 "post_depth_coverage layout qualifier present, "
1444 "but the GL_ARB_post_depth_coverage extension "
1445 "is not enabled.");
1446 }
1447 }
1448
1449 if ($$.flags.q.post_depth_coverage && $$.flags.q.inner_coverage) {
1450 _mesa_glsl_error(& @1, state,
1451 "post_depth_coverage & inner_coverage layout qualifiers "
1452 "are mutually exclusive");
1453 }
1454 }
1455
1456 /* Layout qualifiers for tessellation evaluation shaders. */
1457 if (!$$.flags.i) {
1458 static const struct {
1459 const char *s;
1460 GLenum e;
1461 } map[] = {
1462 /* triangles already parsed by gs-specific code */
1463 { "quads", GL_QUADS },
1464 { "isolines", GL_ISOLINES },
1465 };
1466 for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
1467 if (match_layout_qualifier($1, map[i].s, state) == 0) {
1468 $$.flags.q.prim_type = 1;
1469 $$.prim_type = map[i].e;
1470 break;
1471 }
1472 }
1473
1474 if ($$.flags.i && !state->has_tessellation_shader()) {
1475 _mesa_glsl_error(& @1, state,
1476 "primitive mode qualifier `%s' requires "
1477 "GLSL 4.00 or ARB_tessellation_shader", $1);
1478 }
1479 }
1480 if (!$$.flags.i) {
1481 static const struct {
1482 const char *s;
1483 enum gl_tess_spacing e;
1484 } map[] = {
1485 { "equal_spacing", TESS_SPACING_EQUAL },
1486 { "fractional_odd_spacing", TESS_SPACING_FRACTIONAL_ODD },
1487 { "fractional_even_spacing", TESS_SPACING_FRACTIONAL_EVEN },
1488 };
1489 for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
1490 if (match_layout_qualifier($1, map[i].s, state) == 0) {
1491 $$.flags.q.vertex_spacing = 1;
1492 $$.vertex_spacing = map[i].e;
1493 break;
1494 }
1495 }
1496
1497 if ($$.flags.i && !state->has_tessellation_shader()) {
1498 _mesa_glsl_error(& @1, state,
1499 "vertex spacing qualifier `%s' requires "
1500 "GLSL 4.00 or ARB_tessellation_shader", $1);
1501 }
1502 }
1503 if (!$$.flags.i) {
1504 if (match_layout_qualifier($1, "cw", state) == 0) {
1505 $$.flags.q.ordering = 1;
1506 $$.ordering = GL_CW;
1507 } else if (match_layout_qualifier($1, "ccw", state) == 0) {
1508 $$.flags.q.ordering = 1;
1509 $$.ordering = GL_CCW;
1510 }
1511
1512 if ($$.flags.i && !state->has_tessellation_shader()) {
1513 _mesa_glsl_error(& @1, state,
1514 "ordering qualifier `%s' requires "
1515 "GLSL 4.00 or ARB_tessellation_shader", $1);
1516 }
1517 }
1518 if (!$$.flags.i) {
1519 if (match_layout_qualifier($1, "point_mode", state) == 0) {
1520 $$.flags.q.point_mode = 1;
1521 $$.point_mode = true;
1522 }
1523
1524 if ($$.flags.i && !state->has_tessellation_shader()) {
1525 _mesa_glsl_error(& @1, state,
1526 "qualifier `point_mode' requires "
1527 "GLSL 4.00 or ARB_tessellation_shader");
1528 }
1529 }
1530
1531 if (!$$.flags.i) {
1532 static const struct {
1533 const char *s;
1534 uint32_t mask;
1535 } map[] = {
1536 { "blend_support_multiply", BLEND_MULTIPLY },
1537 { "blend_support_screen", BLEND_SCREEN },
1538 { "blend_support_overlay", BLEND_OVERLAY },
1539 { "blend_support_darken", BLEND_DARKEN },
1540 { "blend_support_lighten", BLEND_LIGHTEN },
1541 { "blend_support_colordodge", BLEND_COLORDODGE },
1542 { "blend_support_colorburn", BLEND_COLORBURN },
1543 { "blend_support_hardlight", BLEND_HARDLIGHT },
1544 { "blend_support_softlight", BLEND_SOFTLIGHT },
1545 { "blend_support_difference", BLEND_DIFFERENCE },
1546 { "blend_support_exclusion", BLEND_EXCLUSION },
1547 { "blend_support_hsl_hue", BLEND_HSL_HUE },
1548 { "blend_support_hsl_saturation", BLEND_HSL_SATURATION },
1549 { "blend_support_hsl_color", BLEND_HSL_COLOR },
1550 { "blend_support_hsl_luminosity", BLEND_HSL_LUMINOSITY },
1551 { "blend_support_all_equations", BLEND_ALL },
1552 };
1553 for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
1554 if (match_layout_qualifier($1, map[i].s, state) == 0) {
1555 $$.flags.q.blend_support = 1;
1556 state->fs_blend_support |= map[i].mask;
1557 break;
1558 }
1559 }
1560
1561 if ($$.flags.i &&
1562 !state->KHR_blend_equation_advanced_enable &&
1563 !state->is_version(0, 320)) {
1564 _mesa_glsl_error(& @1, state,
1565 "advanced blending layout qualifiers require "
1566 "ESSL 3.20 or KHR_blend_equation_advanced");
1567 }
1568
1569 if ($$.flags.i && state->stage != MESA_SHADER_FRAGMENT) {
1570 _mesa_glsl_error(& @1, state,
1571 "advanced blending layout qualifiers only "
1572 "valid in fragment shaders");
1573 }
1574 }
1575
1576 /* Layout qualifiers for ARB_compute_variable_group_size. */
1577 if (!$$.flags.i) {
1578 if (match_layout_qualifier($1, "local_size_variable", state) == 0) {
1579 $$.flags.q.local_size_variable = 1;
1580 }
1581
1582 if ($$.flags.i && !state->ARB_compute_variable_group_size_enable) {
1583 _mesa_glsl_error(& @1, state,
1584 "qualifier `local_size_variable` requires "
1585 "ARB_compute_variable_group_size");
1586 }
1587 }
1588
1589 if (!$$.flags.i) {
1590 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1591 "`%s'", $1);
1592 YYERROR;
1593 }
1594 }
1595 | any_identifier '=' constant_expression
1596 {
1597 memset(& $$, 0, sizeof($$));
1598 void *ctx = state->linalloc;
1599
1600 if ($3->oper != ast_int_constant &&
1601 $3->oper != ast_uint_constant &&
1602 !state->has_enhanced_layouts()) {
1603 _mesa_glsl_error(& @1, state,
1604 "compile-time constant expressions require "
1605 "GLSL 4.40 or ARB_enhanced_layouts");
1606 }
1607
1608 if (match_layout_qualifier("align", $1, state) == 0) {
1609 if (!state->has_enhanced_layouts()) {
1610 _mesa_glsl_error(& @1, state,
1611 "align qualifier requires "
1612 "GLSL 4.40 or ARB_enhanced_layouts");
1613 } else {
1614 $$.flags.q.explicit_align = 1;
1615 $$.align = $3;
1616 }
1617 }
1618
1619 if (match_layout_qualifier("location", $1, state) == 0) {
1620 $$.flags.q.explicit_location = 1;
1621
1622 if ($$.flags.q.attribute == 1 &&
1623 state->ARB_explicit_attrib_location_warn) {
1624 _mesa_glsl_warning(& @1, state,
1625 "GL_ARB_explicit_attrib_location layout "
1626 "identifier `%s' used", $1);
1627 }
1628 $$.location = $3;
1629 }
1630
1631 if (match_layout_qualifier("component", $1, state) == 0) {
1632 if (!state->has_enhanced_layouts()) {
1633 _mesa_glsl_error(& @1, state,
1634 "component qualifier requires "
1635 "GLSL 4.40 or ARB_enhanced_layouts");
1636 } else {
1637 $$.flags.q.explicit_component = 1;
1638 $$.component = $3;
1639 }
1640 }
1641
1642 if (match_layout_qualifier("index", $1, state) == 0) {
1643 if (state->es_shader && !state->EXT_blend_func_extended_enable) {
1644 _mesa_glsl_error(& @3, state, "index layout qualifier requires EXT_blend_func_extended");
1645 YYERROR;
1646 }
1647
1648 $$.flags.q.explicit_index = 1;
1649 $$.index = $3;
1650 }
1651
1652 if ((state->has_420pack_or_es31() ||
1653 state->has_atomic_counters() ||
1654 state->has_shader_storage_buffer_objects()) &&
1655 match_layout_qualifier("binding", $1, state) == 0) {
1656 $$.flags.q.explicit_binding = 1;
1657 $$.binding = $3;
1658 }
1659
1660 if ((state->has_atomic_counters() ||
1661 state->has_enhanced_layouts()) &&
1662 match_layout_qualifier("offset", $1, state) == 0) {
1663 $$.flags.q.explicit_offset = 1;
1664 $$.offset = $3;
1665 }
1666
1667 if (match_layout_qualifier("max_vertices", $1, state) == 0) {
1668 $$.flags.q.max_vertices = 1;
1669 $$.max_vertices = new(ctx) ast_layout_expression(@1, $3);
1670 if (!state->has_geometry_shader()) {
1671 _mesa_glsl_error(& @3, state,
1672 "#version 150 max_vertices qualifier "
1673 "specified", $3);
1674 }
1675 }
1676
1677 if (state->stage == MESA_SHADER_GEOMETRY) {
1678 if (match_layout_qualifier("stream", $1, state) == 0 &&
1679 state->check_explicit_attrib_stream_allowed(& @3)) {
1680 $$.flags.q.stream = 1;
1681 $$.flags.q.explicit_stream = 1;
1682 $$.stream = $3;
1683 }
1684 }
1685
1686 if (state->has_enhanced_layouts()) {
1687 if (match_layout_qualifier("xfb_buffer", $1, state) == 0) {
1688 $$.flags.q.xfb_buffer = 1;
1689 $$.flags.q.explicit_xfb_buffer = 1;
1690 $$.xfb_buffer = $3;
1691 }
1692
1693 if (match_layout_qualifier("xfb_offset", $1, state) == 0) {
1694 $$.flags.q.explicit_xfb_offset = 1;
1695 $$.offset = $3;
1696 }
1697
1698 if (match_layout_qualifier("xfb_stride", $1, state) == 0) {
1699 $$.flags.q.xfb_stride = 1;
1700 $$.flags.q.explicit_xfb_stride = 1;
1701 $$.xfb_stride = $3;
1702 }
1703 }
1704
1705 static const char * const local_size_qualifiers[3] = {
1706 "local_size_x",
1707 "local_size_y",
1708 "local_size_z",
1709 };
1710 for (int i = 0; i < 3; i++) {
1711 if (match_layout_qualifier(local_size_qualifiers[i], $1,
1712 state) == 0) {
1713 if (!state->has_compute_shader()) {
1714 _mesa_glsl_error(& @3, state,
1715 "%s qualifier requires GLSL 4.30 or "
1716 "GLSL ES 3.10 or ARB_compute_shader",
1717 local_size_qualifiers[i]);
1718 YYERROR;
1719 } else {
1720 $$.flags.q.local_size |= (1 << i);
1721 $$.local_size[i] = new(ctx) ast_layout_expression(@1, $3);
1722 }
1723 break;
1724 }
1725 }
1726
1727 if (match_layout_qualifier("invocations", $1, state) == 0) {
1728 $$.flags.q.invocations = 1;
1729 $$.invocations = new(ctx) ast_layout_expression(@1, $3);
1730 if (!state->is_version(400, 320) &&
1731 !state->ARB_gpu_shader5_enable &&
1732 !state->OES_geometry_shader_enable &&
1733 !state->EXT_geometry_shader_enable) {
1734 _mesa_glsl_error(& @3, state,
1735 "GL_ARB_gpu_shader5 invocations "
1736 "qualifier specified", $3);
1737 }
1738 }
1739
1740 /* Layout qualifiers for tessellation control shaders. */
1741 if (match_layout_qualifier("vertices", $1, state) == 0) {
1742 $$.flags.q.vertices = 1;
1743 $$.vertices = new(ctx) ast_layout_expression(@1, $3);
1744 if (!state->has_tessellation_shader()) {
1745 _mesa_glsl_error(& @1, state,
1746 "vertices qualifier requires GLSL 4.00 or "
1747 "ARB_tessellation_shader");
1748 }
1749 }
1750
1751 /* If the identifier didn't match any known layout identifiers,
1752 * emit an error.
1753 */
1754 if (!$$.flags.i) {
1755 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1756 "`%s'", $1);
1757 YYERROR;
1758 }
1759 }
1760 | interface_block_layout_qualifier
1761 {
1762 $$ = $1;
1763 /* Layout qualifiers for ARB_uniform_buffer_object. */
1764 if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
1765 _mesa_glsl_error(& @1, state,
1766 "#version 140 / GL_ARB_uniform_buffer_object "
1767 "layout qualifier `%s' is used", $1);
1768 } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1769 _mesa_glsl_warning(& @1, state,
1770 "#version 140 / GL_ARB_uniform_buffer_object "
1771 "layout qualifier `%s' is used", $1);
1772 }
1773 }
1774 ;
1775
1776 /* This is a separate language rule because we parse these as tokens
1777 * (due to them being reserved keywords) instead of identifiers like
1778 * most qualifiers. See the any_identifier path of
1779 * layout_qualifier_id for the others.
1780 *
1781 * Note that since layout qualifiers are case-insensitive in desktop
1782 * GLSL, all of these qualifiers need to be handled as identifiers as
1783 * well (by the any_identifier path of layout_qualifier_id).
1784 */
1785 interface_block_layout_qualifier:
1786 ROW_MAJOR
1787 {
1788 memset(& $$, 0, sizeof($$));
1789 $$.flags.q.row_major = 1;
1790 }
1791 | PACKED_TOK
1792 {
1793 memset(& $$, 0, sizeof($$));
1794 $$.flags.q.packed = 1;
1795 }
1796 | SHARED
1797 {
1798 memset(& $$, 0, sizeof($$));
1799 $$.flags.q.shared = 1;
1800 }
1801 ;
1802
1803 subroutine_qualifier:
1804 SUBROUTINE
1805 {
1806 memset(& $$, 0, sizeof($$));
1807 $$.flags.q.subroutine = 1;
1808 }
1809 | SUBROUTINE '(' subroutine_type_list ')'
1810 {
1811 memset(& $$, 0, sizeof($$));
1812 $$.flags.q.subroutine_def = 1;
1813 $$.subroutine_list = $3;
1814 }
1815 ;
1816
1817 subroutine_type_list:
1818 any_identifier
1819 {
1820 void *ctx = state->linalloc;
1821 ast_declaration *decl = new(ctx) ast_declaration($1, NULL, NULL);
1822 decl->set_location(@1);
1823
1824 $$ = new(ctx) ast_subroutine_list();
1825 $$->declarations.push_tail(&decl->link);
1826 }
1827 | subroutine_type_list ',' any_identifier
1828 {
1829 void *ctx = state->linalloc;
1830 ast_declaration *decl = new(ctx) ast_declaration($3, NULL, NULL);
1831 decl->set_location(@3);
1832
1833 $$ = $1;
1834 $$->declarations.push_tail(&decl->link);
1835 }
1836 ;
1837
1838 interpolation_qualifier:
1839 SMOOTH
1840 {
1841 memset(& $$, 0, sizeof($$));
1842 $$.flags.q.smooth = 1;
1843 }
1844 | FLAT
1845 {
1846 memset(& $$, 0, sizeof($$));
1847 $$.flags.q.flat = 1;
1848 }
1849 | NOPERSPECTIVE
1850 {
1851 memset(& $$, 0, sizeof($$));
1852 $$.flags.q.noperspective = 1;
1853 }
1854 ;
1855
1856 type_qualifier:
1857 /* Single qualifiers */
1858 INVARIANT
1859 {
1860 memset(& $$, 0, sizeof($$));
1861 $$.flags.q.invariant = 1;
1862 }
1863 | PRECISE
1864 {
1865 memset(& $$, 0, sizeof($$));
1866 $$.flags.q.precise = 1;
1867 }
1868 | auxiliary_storage_qualifier
1869 | storage_qualifier
1870 | interpolation_qualifier
1871 | layout_qualifier
1872 | memory_qualifier
1873 | subroutine_qualifier
1874 | precision_qualifier
1875 {
1876 memset(&$$, 0, sizeof($$));
1877 $$.precision = $1;
1878 }
1879
1880 /* Multiple qualifiers:
1881 * In GLSL 4.20, these can be specified in any order. In earlier versions,
1882 * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1883 *
1884 * invariant interpolation auxiliary storage precision ...or...
1885 * layout storage precision
1886 *
1887 * Each qualifier's rule ensures that the accumulated qualifiers on the right
1888 * side don't contain any that must appear on the left hand side.
1889 * For example, when processing a storage qualifier, we check that there are
1890 * no auxiliary, interpolation, layout, invariant, or precise qualifiers to the right.
1891 */
1892 | PRECISE type_qualifier
1893 {
1894 if ($2.flags.q.precise)
1895 _mesa_glsl_error(&@1, state, "duplicate \"precise\" qualifier");
1896
1897 $$ = $2;
1898 $$.flags.q.precise = 1;
1899 }
1900 | INVARIANT type_qualifier
1901 {
1902 if ($2.flags.q.invariant)
1903 _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1904
1905 if (!state->has_420pack_or_es31() && $2.flags.q.precise)
1906 _mesa_glsl_error(&@1, state,
1907 "\"invariant\" must come after \"precise\"");
1908
1909 $$ = $2;
1910 $$.flags.q.invariant = 1;
1911
1912 /* GLSL ES 3.00 spec, section 4.6.1 "The Invariant Qualifier":
1913 *
1914 * "Only variables output from a shader can be candidates for invariance.
1915 * This includes user-defined output variables and the built-in output
1916 * variables. As only outputs can be declared as invariant, an invariant
1917 * output from one shader stage will still match an input of a subsequent
1918 * stage without the input being declared as invariant."
1919 *
1920 * On the desktop side, this text first appears in GLSL 4.30.
1921 */
1922 if (state->is_version(430, 300) && $$.flags.q.in)
1923 _mesa_glsl_error(&@1, state, "invariant qualifiers cannot be used with shader inputs");
1924 }
1925 | interpolation_qualifier type_qualifier
1926 {
1927 /* Section 4.3 of the GLSL 1.40 specification states:
1928 * "...qualified with one of these interpolation qualifiers"
1929 *
1930 * GLSL 1.30 claims to allow "one or more", but insists that:
1931 * "These interpolation qualifiers may only precede the qualifiers in,
1932 * centroid in, out, or centroid out in a declaration."
1933 *
1934 * ...which means that e.g. smooth can't precede smooth, so there can be
1935 * only one after all, and the 1.40 text is a clarification, not a change.
1936 */
1937 if ($2.has_interpolation())
1938 _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1939
1940 if (!state->has_420pack_or_es31() &&
1941 ($2.flags.q.precise || $2.flags.q.invariant)) {
1942 _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1943 "after \"precise\" or \"invariant\"");
1944 }
1945
1946 $$ = $1;
1947 $$.merge_qualifier(&@1, state, $2, false);
1948 }
1949 | layout_qualifier type_qualifier
1950 {
1951 /* In the absence of ARB_shading_language_420pack, layout qualifiers may
1952 * appear no later than auxiliary storage qualifiers. There is no
1953 * particularly clear spec language mandating this, but in all examples
1954 * the layout qualifier precedes the storage qualifier.
1955 *
1956 * We allow combinations of layout with interpolation, invariant or
1957 * precise qualifiers since these are useful in ARB_separate_shader_objects.
1958 * There is no clear spec guidance on this either.
1959 */
1960 $$ = $1;
1961 $$.merge_qualifier(& @1, state, $2, false, $2.has_layout());
1962 }
1963 | subroutine_qualifier type_qualifier
1964 {
1965 $$ = $1;
1966 $$.merge_qualifier(&@1, state, $2, false);
1967 }
1968 | auxiliary_storage_qualifier type_qualifier
1969 {
1970 if ($2.has_auxiliary_storage()) {
1971 _mesa_glsl_error(&@1, state,
1972 "duplicate auxiliary storage qualifier (centroid or sample)");
1973 }
1974
1975 if (!state->has_420pack_or_es31() &&
1976 ($2.flags.q.precise || $2.flags.q.invariant ||
1977 $2.has_interpolation() || $2.has_layout())) {
1978 _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
1979 "just before storage qualifiers");
1980 }
1981 $$ = $1;
1982 $$.merge_qualifier(&@1, state, $2, false);
1983 }
1984 | storage_qualifier type_qualifier
1985 {
1986 /* Section 4.3 of the GLSL 1.20 specification states:
1987 * "Variable declarations may have a storage qualifier specified..."
1988 * 1.30 clarifies this to "may have one storage qualifier".
1989 */
1990 if ($2.has_storage())
1991 _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
1992
1993 if (!state->has_420pack_or_es31() &&
1994 ($2.flags.q.precise || $2.flags.q.invariant || $2.has_interpolation() ||
1995 $2.has_layout() || $2.has_auxiliary_storage())) {
1996 _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
1997 "precise, invariant, interpolation, layout and auxiliary "
1998 "storage qualifiers");
1999 }
2000
2001 $$ = $1;
2002 $$.merge_qualifier(&@1, state, $2, false);
2003 }
2004 | precision_qualifier type_qualifier
2005 {
2006 if ($2.precision != ast_precision_none)
2007 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
2008
2009 if (!(state->has_420pack_or_es31()) &&
2010 $2.flags.i != 0)
2011 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
2012
2013 $$ = $2;
2014 $$.precision = $1;
2015 }
2016 | memory_qualifier type_qualifier
2017 {
2018 $$ = $1;
2019 $$.merge_qualifier(&@1, state, $2, false);
2020 }
2021 ;
2022
2023 auxiliary_storage_qualifier:
2024 CENTROID
2025 {
2026 memset(& $$, 0, sizeof($$));
2027 $$.flags.q.centroid = 1;
2028 }
2029 | SAMPLE
2030 {
2031 memset(& $$, 0, sizeof($$));
2032 $$.flags.q.sample = 1;
2033 }
2034 | PATCH
2035 {
2036 memset(& $$, 0, sizeof($$));
2037 $$.flags.q.patch = 1;
2038 }
2039
2040 storage_qualifier:
2041 CONST_TOK
2042 {
2043 memset(& $$, 0, sizeof($$));
2044 $$.flags.q.constant = 1;
2045 }
2046 | ATTRIBUTE
2047 {
2048 memset(& $$, 0, sizeof($$));
2049 $$.flags.q.attribute = 1;
2050 }
2051 | VARYING
2052 {
2053 memset(& $$, 0, sizeof($$));
2054 $$.flags.q.varying = 1;
2055 }
2056 | IN_TOK
2057 {
2058 memset(& $$, 0, sizeof($$));
2059 $$.flags.q.in = 1;
2060 }
2061 | OUT_TOK
2062 {
2063 memset(& $$, 0, sizeof($$));
2064 $$.flags.q.out = 1;
2065
2066 if (state->stage == MESA_SHADER_GEOMETRY &&
2067 state->has_explicit_attrib_stream()) {
2068 /* Section 4.3.8.2 (Output Layout Qualifiers) of the GLSL 4.00
2069 * spec says:
2070 *
2071 * "If the block or variable is declared with the stream
2072 * identifier, it is associated with the specified stream;
2073 * otherwise, it is associated with the current default stream."
2074 */
2075 $$.flags.q.stream = 1;
2076 $$.flags.q.explicit_stream = 0;
2077 $$.stream = state->out_qualifier->stream;
2078 }
2079
2080 if (state->has_enhanced_layouts()) {
2081 $$.flags.q.xfb_buffer = 1;
2082 $$.flags.q.explicit_xfb_buffer = 0;
2083 $$.xfb_buffer = state->out_qualifier->xfb_buffer;
2084 }
2085 }
2086 | INOUT_TOK
2087 {
2088 memset(& $$, 0, sizeof($$));
2089 $$.flags.q.in = 1;
2090 $$.flags.q.out = 1;
2091
2092 if (!state->has_framebuffer_fetch() ||
2093 !state->is_version(130, 300) ||
2094 state->stage != MESA_SHADER_FRAGMENT)
2095 _mesa_glsl_error(&@1, state, "A single interface variable cannot be "
2096 "declared as both input and output");
2097 }
2098 | UNIFORM
2099 {
2100 memset(& $$, 0, sizeof($$));
2101 $$.flags.q.uniform = 1;
2102 }
2103 | BUFFER
2104 {
2105 memset(& $$, 0, sizeof($$));
2106 $$.flags.q.buffer = 1;
2107 }
2108 | SHARED
2109 {
2110 memset(& $$, 0, sizeof($$));
2111 $$.flags.q.shared_storage = 1;
2112 }
2113 ;
2114
2115 memory_qualifier:
2116 COHERENT
2117 {
2118 memset(& $$, 0, sizeof($$));
2119 $$.flags.q.coherent = 1;
2120 }
2121 | VOLATILE
2122 {
2123 memset(& $$, 0, sizeof($$));
2124 $$.flags.q._volatile = 1;
2125 }
2126 | RESTRICT
2127 {
2128 STATIC_ASSERT(sizeof($$.flags.q) <= sizeof($$.flags.i));
2129 memset(& $$, 0, sizeof($$));
2130 $$.flags.q.restrict_flag = 1;
2131 }
2132 | READONLY
2133 {
2134 memset(& $$, 0, sizeof($$));
2135 $$.flags.q.read_only = 1;
2136 }
2137 | WRITEONLY
2138 {
2139 memset(& $$, 0, sizeof($$));
2140 $$.flags.q.write_only = 1;
2141 }
2142 ;
2143
2144 array_specifier:
2145 '[' ']'
2146 {
2147 void *ctx = state->linalloc;
2148 $$ = new(ctx) ast_array_specifier(@1, new(ctx) ast_expression(
2149 ast_unsized_array_dim, NULL,
2150 NULL, NULL));
2151 $$->set_location_range(@1, @2);
2152 }
2153 | '[' constant_expression ']'
2154 {
2155 void *ctx = state->linalloc;
2156 $$ = new(ctx) ast_array_specifier(@1, $2);
2157 $$->set_location_range(@1, @3);
2158 }
2159 | array_specifier '[' ']'
2160 {
2161 void *ctx = state->linalloc;
2162 $$ = $1;
2163
2164 if (state->check_arrays_of_arrays_allowed(& @1)) {
2165 $$->add_dimension(new(ctx) ast_expression(ast_unsized_array_dim, NULL,
2166 NULL, NULL));
2167 }
2168 }
2169 | array_specifier '[' constant_expression ']'
2170 {
2171 $$ = $1;
2172
2173 if (state->check_arrays_of_arrays_allowed(& @1)) {
2174 $$->add_dimension($3);
2175 }
2176 }
2177 ;
2178
2179 type_specifier:
2180 type_specifier_nonarray
2181 | type_specifier_nonarray array_specifier
2182 {
2183 $$ = $1;
2184 $$->array_specifier = $2;
2185 }
2186 ;
2187
2188 type_specifier_nonarray:
2189 basic_type_specifier_nonarray
2190 {
2191 void *ctx = state->linalloc;
2192 $$ = new(ctx) ast_type_specifier($1);
2193 $$->set_location(@1);
2194 }
2195 | struct_specifier
2196 {
2197 void *ctx = state->linalloc;
2198 $$ = new(ctx) ast_type_specifier($1);
2199 $$->set_location(@1);
2200 }
2201 | TYPE_IDENTIFIER
2202 {
2203 void *ctx = state->linalloc;
2204 $$ = new(ctx) ast_type_specifier($1);
2205 $$->set_location(@1);
2206 }
2207 ;
2208
2209 basic_type_specifier_nonarray:
2210 VOID_TOK { $$ = "void"; }
2211 | FLOAT_TOK { $$ = "float"; }
2212 | DOUBLE_TOK { $$ = "double"; }
2213 | INT_TOK { $$ = "int"; }
2214 | UINT_TOK { $$ = "uint"; }
2215 | BOOL_TOK { $$ = "bool"; }
2216 | VEC2 { $$ = "vec2"; }
2217 | VEC3 { $$ = "vec3"; }
2218 | VEC4 { $$ = "vec4"; }
2219 | BVEC2 { $$ = "bvec2"; }
2220 | BVEC3 { $$ = "bvec3"; }
2221 | BVEC4 { $$ = "bvec4"; }
2222 | IVEC2 { $$ = "ivec2"; }
2223 | IVEC3 { $$ = "ivec3"; }
2224 | IVEC4 { $$ = "ivec4"; }
2225 | UVEC2 { $$ = "uvec2"; }
2226 | UVEC3 { $$ = "uvec3"; }
2227 | UVEC4 { $$ = "uvec4"; }
2228 | DVEC2 { $$ = "dvec2"; }
2229 | DVEC3 { $$ = "dvec3"; }
2230 | DVEC4 { $$ = "dvec4"; }
2231 | MAT2X2 { $$ = "mat2"; }
2232 | MAT2X3 { $$ = "mat2x3"; }
2233 | MAT2X4 { $$ = "mat2x4"; }
2234 | MAT3X2 { $$ = "mat3x2"; }
2235 | MAT3X3 { $$ = "mat3"; }
2236 | MAT3X4 { $$ = "mat3x4"; }
2237 | MAT4X2 { $$ = "mat4x2"; }
2238 | MAT4X3 { $$ = "mat4x3"; }
2239 | MAT4X4 { $$ = "mat4"; }
2240 | DMAT2X2 { $$ = "dmat2"; }
2241 | DMAT2X3 { $$ = "dmat2x3"; }
2242 | DMAT2X4 { $$ = "dmat2x4"; }
2243 | DMAT3X2 { $$ = "dmat3x2"; }
2244 | DMAT3X3 { $$ = "dmat3"; }
2245 | DMAT3X4 { $$ = "dmat3x4"; }
2246 | DMAT4X2 { $$ = "dmat4x2"; }
2247 | DMAT4X3 { $$ = "dmat4x3"; }
2248 | DMAT4X4 { $$ = "dmat4"; }
2249 | SAMPLER1D { $$ = "sampler1D"; }
2250 | SAMPLER2D { $$ = "sampler2D"; }
2251 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
2252 | SAMPLER3D { $$ = "sampler3D"; }
2253 | SAMPLERCUBE { $$ = "samplerCube"; }
2254 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
2255 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
2256 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
2257 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
2258 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
2259 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
2260 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
2261 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
2262 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
2263 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
2264 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
2265 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
2266 | ISAMPLER1D { $$ = "isampler1D"; }
2267 | ISAMPLER2D { $$ = "isampler2D"; }
2268 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
2269 | ISAMPLER3D { $$ = "isampler3D"; }
2270 | ISAMPLERCUBE { $$ = "isamplerCube"; }
2271 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
2272 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
2273 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
2274 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
2275 | USAMPLER1D { $$ = "usampler1D"; }
2276 | USAMPLER2D { $$ = "usampler2D"; }
2277 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
2278 | USAMPLER3D { $$ = "usampler3D"; }
2279 | USAMPLERCUBE { $$ = "usamplerCube"; }
2280 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
2281 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
2282 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
2283 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
2284 | SAMPLER2DMS { $$ = "sampler2DMS"; }
2285 | ISAMPLER2DMS { $$ = "isampler2DMS"; }
2286 | USAMPLER2DMS { $$ = "usampler2DMS"; }
2287 | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
2288 | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
2289 | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
2290 | IMAGE1D { $$ = "image1D"; }
2291 | IMAGE2D { $$ = "image2D"; }
2292 | IMAGE3D { $$ = "image3D"; }
2293 | IMAGE2DRECT { $$ = "image2DRect"; }
2294 | IMAGECUBE { $$ = "imageCube"; }
2295 | IMAGEBUFFER { $$ = "imageBuffer"; }
2296 | IMAGE1DARRAY { $$ = "image1DArray"; }
2297 | IMAGE2DARRAY { $$ = "image2DArray"; }
2298 | IMAGECUBEARRAY { $$ = "imageCubeArray"; }
2299 | IMAGE2DMS { $$ = "image2DMS"; }
2300 | IMAGE2DMSARRAY { $$ = "image2DMSArray"; }
2301 | IIMAGE1D { $$ = "iimage1D"; }
2302 | IIMAGE2D { $$ = "iimage2D"; }
2303 | IIMAGE3D { $$ = "iimage3D"; }
2304 | IIMAGE2DRECT { $$ = "iimage2DRect"; }
2305 | IIMAGECUBE { $$ = "iimageCube"; }
2306 | IIMAGEBUFFER { $$ = "iimageBuffer"; }
2307 | IIMAGE1DARRAY { $$ = "iimage1DArray"; }
2308 | IIMAGE2DARRAY { $$ = "iimage2DArray"; }
2309 | IIMAGECUBEARRAY { $$ = "iimageCubeArray"; }
2310 | IIMAGE2DMS { $$ = "iimage2DMS"; }
2311 | IIMAGE2DMSARRAY { $$ = "iimage2DMSArray"; }
2312 | UIMAGE1D { $$ = "uimage1D"; }
2313 | UIMAGE2D { $$ = "uimage2D"; }
2314 | UIMAGE3D { $$ = "uimage3D"; }
2315 | UIMAGE2DRECT { $$ = "uimage2DRect"; }
2316 | UIMAGECUBE { $$ = "uimageCube"; }
2317 | UIMAGEBUFFER { $$ = "uimageBuffer"; }
2318 | UIMAGE1DARRAY { $$ = "uimage1DArray"; }
2319 | UIMAGE2DARRAY { $$ = "uimage2DArray"; }
2320 | UIMAGECUBEARRAY { $$ = "uimageCubeArray"; }
2321 | UIMAGE2DMS { $$ = "uimage2DMS"; }
2322 | UIMAGE2DMSARRAY { $$ = "uimage2DMSArray"; }
2323 | ATOMIC_UINT { $$ = "atomic_uint"; }
2324 | INT64_TOK { $$ = "int64_t"; }
2325 | I64VEC2 { $$ = "i64vec2"; }
2326 | I64VEC3 { $$ = "i64vec3"; }
2327 | I64VEC4 { $$ = "i64vec4"; }
2328 | UINT64_TOK { $$ = "uint64_t"; }
2329 | U64VEC2 { $$ = "u64vec2"; }
2330 | U64VEC3 { $$ = "u64vec3"; }
2331 | U64VEC4 { $$ = "u64vec4"; }
2332 ;
2333
2334 precision_qualifier:
2335 HIGHP
2336 {
2337 state->check_precision_qualifiers_allowed(&@1);
2338 $$ = ast_precision_high;
2339 }
2340 | MEDIUMP
2341 {
2342 state->check_precision_qualifiers_allowed(&@1);
2343 $$ = ast_precision_medium;
2344 }
2345 | LOWP
2346 {
2347 state->check_precision_qualifiers_allowed(&@1);
2348 $$ = ast_precision_low;
2349 }
2350 ;
2351
2352 struct_specifier:
2353 STRUCT any_identifier '{' struct_declaration_list '}'
2354 {
2355 void *ctx = state->linalloc;
2356 $$ = new(ctx) ast_struct_specifier(ctx, $2, $4);
2357 $$->set_location_range(@2, @5);
2358 state->symbols->add_type($2, glsl_type::void_type);
2359 }
2360 | STRUCT '{' struct_declaration_list '}'
2361 {
2362 void *ctx = state->linalloc;
2363 $$ = new(ctx) ast_struct_specifier(ctx, NULL, $3);
2364 $$->set_location_range(@2, @4);
2365 }
2366 ;
2367
2368 struct_declaration_list:
2369 struct_declaration
2370 {
2371 $$ = $1;
2372 $1->link.self_link();
2373 }
2374 | struct_declaration_list struct_declaration
2375 {
2376 $$ = $1;
2377 $$->link.insert_before(& $2->link);
2378 }
2379 ;
2380
2381 struct_declaration:
2382 fully_specified_type struct_declarator_list ';'
2383 {
2384 void *ctx = state->linalloc;
2385 ast_fully_specified_type *const type = $1;
2386 type->set_location(@1);
2387
2388 if (type->qualifier.flags.i != 0)
2389 _mesa_glsl_error(&@1, state,
2390 "only precision qualifiers may be applied to "
2391 "structure members");
2392
2393 $$ = new(ctx) ast_declarator_list(type);
2394 $$->set_location(@2);
2395
2396 $$->declarations.push_degenerate_list_at_head(& $2->link);
2397 }
2398 ;
2399
2400 struct_declarator_list:
2401 struct_declarator
2402 {
2403 $$ = $1;
2404 $1->link.self_link();
2405 }
2406 | struct_declarator_list ',' struct_declarator
2407 {
2408 $$ = $1;
2409 $$->link.insert_before(& $3->link);
2410 }
2411 ;
2412
2413 struct_declarator:
2414 any_identifier
2415 {
2416 void *ctx = state->linalloc;
2417 $$ = new(ctx) ast_declaration($1, NULL, NULL);
2418 $$->set_location(@1);
2419 }
2420 | any_identifier array_specifier
2421 {
2422 void *ctx = state->linalloc;
2423 $$ = new(ctx) ast_declaration($1, $2, NULL);
2424 $$->set_location_range(@1, @2);
2425 }
2426 ;
2427
2428 initializer:
2429 assignment_expression
2430 | '{' initializer_list '}'
2431 {
2432 $$ = $2;
2433 }
2434 | '{' initializer_list ',' '}'
2435 {
2436 $$ = $2;
2437 }
2438 ;
2439
2440 initializer_list:
2441 initializer
2442 {
2443 void *ctx = state->linalloc;
2444 $$ = new(ctx) ast_aggregate_initializer();
2445 $$->set_location(@1);
2446 $$->expressions.push_tail(& $1->link);
2447 }
2448 | initializer_list ',' initializer
2449 {
2450 $1->expressions.push_tail(& $3->link);
2451 }
2452 ;
2453
2454 declaration_statement:
2455 declaration
2456 ;
2457
2458 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
2459 // supported.
2460 statement:
2461 compound_statement { $$ = (ast_node *) $1; }
2462 | simple_statement
2463 ;
2464
2465 simple_statement:
2466 declaration_statement
2467 | expression_statement
2468 | selection_statement
2469 | switch_statement
2470 | iteration_statement
2471 | jump_statement
2472 ;
2473
2474 compound_statement:
2475 '{' '}'
2476 {
2477 void *ctx = state->linalloc;
2478 $$ = new(ctx) ast_compound_statement(true, NULL);
2479 $$->set_location_range(@1, @2);
2480 }
2481 | '{'
2482 {
2483 state->symbols->push_scope();
2484 }
2485 statement_list '}'
2486 {
2487 void *ctx = state->linalloc;
2488 $$ = new(ctx) ast_compound_statement(true, $3);
2489 $$->set_location_range(@1, @4);
2490 state->symbols->pop_scope();
2491 }
2492 ;
2493
2494 statement_no_new_scope:
2495 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
2496 | simple_statement
2497 ;
2498
2499 compound_statement_no_new_scope:
2500 '{' '}'
2501 {
2502 void *ctx = state->linalloc;
2503 $$ = new(ctx) ast_compound_statement(false, NULL);
2504 $$->set_location_range(@1, @2);
2505 }
2506 | '{' statement_list '}'
2507 {
2508 void *ctx = state->linalloc;
2509 $$ = new(ctx) ast_compound_statement(false, $2);
2510 $$->set_location_range(@1, @3);
2511 }
2512 ;
2513
2514 statement_list:
2515 statement
2516 {
2517 if ($1 == NULL) {
2518 _mesa_glsl_error(& @1, state, "<nil> statement");
2519 assert($1 != NULL);
2520 }
2521
2522 $$ = $1;
2523 $$->link.self_link();
2524 }
2525 | statement_list statement
2526 {
2527 if ($2 == NULL) {
2528 _mesa_glsl_error(& @2, state, "<nil> statement");
2529 assert($2 != NULL);
2530 }
2531 $$ = $1;
2532 $$->link.insert_before(& $2->link);
2533 }
2534 ;
2535
2536 expression_statement:
2537 ';'
2538 {
2539 void *ctx = state->linalloc;
2540 $$ = new(ctx) ast_expression_statement(NULL);
2541 $$->set_location(@1);
2542 }
2543 | expression ';'
2544 {
2545 void *ctx = state->linalloc;
2546 $$ = new(ctx) ast_expression_statement($1);
2547 $$->set_location(@1);
2548 }
2549 ;
2550
2551 selection_statement:
2552 IF '(' expression ')' selection_rest_statement
2553 {
2554 $$ = new(state->linalloc) ast_selection_statement($3, $5.then_statement,
2555 $5.else_statement);
2556 $$->set_location_range(@1, @5);
2557 }
2558 ;
2559
2560 selection_rest_statement:
2561 statement ELSE statement
2562 {
2563 $$.then_statement = $1;
2564 $$.else_statement = $3;
2565 }
2566 | statement %prec THEN
2567 {
2568 $$.then_statement = $1;
2569 $$.else_statement = NULL;
2570 }
2571 ;
2572
2573 condition:
2574 expression
2575 {
2576 $$ = (ast_node *) $1;
2577 }
2578 | fully_specified_type any_identifier '=' initializer
2579 {
2580 void *ctx = state->linalloc;
2581 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
2582 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
2583 decl->set_location_range(@2, @4);
2584 declarator->set_location(@1);
2585
2586 declarator->declarations.push_tail(&decl->link);
2587 $$ = declarator;
2588 }
2589 ;
2590
2591 /*
2592 * switch_statement grammar is based on the syntax described in the body
2593 * of the GLSL spec, not in it's appendix!!!
2594 */
2595 switch_statement:
2596 SWITCH '(' expression ')' switch_body
2597 {
2598 $$ = new(state->linalloc) ast_switch_statement($3, $5);
2599 $$->set_location_range(@1, @5);
2600 }
2601 ;
2602
2603 switch_body:
2604 '{' '}'
2605 {
2606 $$ = new(state->linalloc) ast_switch_body(NULL);
2607 $$->set_location_range(@1, @2);
2608 }
2609 | '{' case_statement_list '}'
2610 {
2611 $$ = new(state->linalloc) ast_switch_body($2);
2612 $$->set_location_range(@1, @3);
2613 }
2614 ;
2615
2616 case_label:
2617 CASE expression ':'
2618 {
2619 $$ = new(state->linalloc) ast_case_label($2);
2620 $$->set_location(@2);
2621 }
2622 | DEFAULT ':'
2623 {
2624 $$ = new(state->linalloc) ast_case_label(NULL);
2625 $$->set_location(@2);
2626 }
2627 ;
2628
2629 case_label_list:
2630 case_label
2631 {
2632 ast_case_label_list *labels = new(state->linalloc) ast_case_label_list();
2633
2634 labels->labels.push_tail(& $1->link);
2635 $$ = labels;
2636 $$->set_location(@1);
2637 }
2638 | case_label_list case_label
2639 {
2640 $$ = $1;
2641 $$->labels.push_tail(& $2->link);
2642 }
2643 ;
2644
2645 case_statement:
2646 case_label_list statement
2647 {
2648 ast_case_statement *stmts = new(state->linalloc) ast_case_statement($1);
2649 stmts->set_location(@2);
2650
2651 stmts->stmts.push_tail(& $2->link);
2652 $$ = stmts;
2653 }
2654 | case_statement statement
2655 {
2656 $$ = $1;
2657 $$->stmts.push_tail(& $2->link);
2658 }
2659 ;
2660
2661 case_statement_list:
2662 case_statement
2663 {
2664 ast_case_statement_list *cases= new(state->linalloc) ast_case_statement_list();
2665 cases->set_location(@1);
2666
2667 cases->cases.push_tail(& $1->link);
2668 $$ = cases;
2669 }
2670 | case_statement_list case_statement
2671 {
2672 $$ = $1;
2673 $$->cases.push_tail(& $2->link);
2674 }
2675 ;
2676
2677 iteration_statement:
2678 WHILE '(' condition ')' statement_no_new_scope
2679 {
2680 void *ctx = state->linalloc;
2681 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2682 NULL, $3, NULL, $5);
2683 $$->set_location_range(@1, @4);
2684 }
2685 | DO statement WHILE '(' expression ')' ';'
2686 {
2687 void *ctx = state->linalloc;
2688 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2689 NULL, $5, NULL, $2);
2690 $$->set_location_range(@1, @6);
2691 }
2692 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2693 {
2694 void *ctx = state->linalloc;
2695 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2696 $3, $4.cond, $4.rest, $6);
2697 $$->set_location_range(@1, @6);
2698 }
2699 ;
2700
2701 for_init_statement:
2702 expression_statement
2703 | declaration_statement
2704 ;
2705
2706 conditionopt:
2707 condition
2708 | /* empty */
2709 {
2710 $$ = NULL;
2711 }
2712 ;
2713
2714 for_rest_statement:
2715 conditionopt ';'
2716 {
2717 $$.cond = $1;
2718 $$.rest = NULL;
2719 }
2720 | conditionopt ';' expression
2721 {
2722 $$.cond = $1;
2723 $$.rest = $3;
2724 }
2725 ;
2726
2727 // Grammar Note: No 'goto'. Gotos are not supported.
2728 jump_statement:
2729 CONTINUE ';'
2730 {
2731 void *ctx = state->linalloc;
2732 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2733 $$->set_location(@1);
2734 }
2735 | BREAK ';'
2736 {
2737 void *ctx = state->linalloc;
2738 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2739 $$->set_location(@1);
2740 }
2741 | RETURN ';'
2742 {
2743 void *ctx = state->linalloc;
2744 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2745 $$->set_location(@1);
2746 }
2747 | RETURN expression ';'
2748 {
2749 void *ctx = state->linalloc;
2750 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2751 $$->set_location_range(@1, @2);
2752 }
2753 | DISCARD ';' // Fragment shader only.
2754 {
2755 void *ctx = state->linalloc;
2756 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2757 $$->set_location(@1);
2758 }
2759 ;
2760
2761 external_declaration:
2762 function_definition { $$ = $1; }
2763 | declaration { $$ = $1; }
2764 | pragma_statement { $$ = NULL; }
2765 | layout_defaults { $$ = $1; }
2766 ;
2767
2768 function_definition:
2769 function_prototype compound_statement_no_new_scope
2770 {
2771 void *ctx = state->linalloc;
2772 $$ = new(ctx) ast_function_definition();
2773 $$->set_location_range(@1, @2);
2774 $$->prototype = $1;
2775 $$->body = $2;
2776
2777 state->symbols->pop_scope();
2778 }
2779 ;
2780
2781 /* layout_qualifieropt is packed into this rule */
2782 interface_block:
2783 basic_interface_block
2784 {
2785 $$ = $1;
2786 }
2787 | layout_qualifier interface_block
2788 {
2789 ast_interface_block *block = (ast_interface_block *) $2;
2790
2791 if (!$1.merge_qualifier(& @1, state, block->layout, false,
2792 block->layout.has_layout())) {
2793 YYERROR;
2794 }
2795
2796 block->layout = $1;
2797
2798 $$ = block;
2799 }
2800 | memory_qualifier interface_block
2801 {
2802 ast_interface_block *block = (ast_interface_block *)$2;
2803
2804 if (!block->default_layout.flags.q.buffer) {
2805 _mesa_glsl_error(& @1, state,
2806 "memory qualifiers can only be used in the "
2807 "declaration of shader storage blocks");
2808 }
2809 if (!$1.merge_qualifier(& @1, state, block->layout, false)) {
2810 YYERROR;
2811 }
2812 block->layout = $1;
2813 $$ = block;
2814 }
2815 ;
2816
2817 basic_interface_block:
2818 interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2819 {
2820 ast_interface_block *const block = $6;
2821
2822 if ($1.flags.q.uniform) {
2823 block->default_layout = *state->default_uniform_qualifier;
2824 } else if ($1.flags.q.buffer) {
2825 block->default_layout = *state->default_shader_storage_qualifier;
2826 }
2827 block->block_name = $2;
2828 block->declarations.push_degenerate_list_at_head(& $4->link);
2829
2830 _mesa_ast_process_interface_block(& @1, state, block, $1);
2831
2832 $$ = block;
2833 }
2834 ;
2835
2836 interface_qualifier:
2837 IN_TOK
2838 {
2839 memset(& $$, 0, sizeof($$));
2840 $$.flags.q.in = 1;
2841 }
2842 | OUT_TOK
2843 {
2844 memset(& $$, 0, sizeof($$));
2845 $$.flags.q.out = 1;
2846 }
2847 | UNIFORM
2848 {
2849 memset(& $$, 0, sizeof($$));
2850 $$.flags.q.uniform = 1;
2851 }
2852 | BUFFER
2853 {
2854 memset(& $$, 0, sizeof($$));
2855 $$.flags.q.buffer = 1;
2856 }
2857 | auxiliary_storage_qualifier interface_qualifier
2858 {
2859 if (!$1.flags.q.patch) {
2860 _mesa_glsl_error(&@1, state, "invalid interface qualifier");
2861 }
2862 if ($2.has_auxiliary_storage()) {
2863 _mesa_glsl_error(&@1, state, "duplicate patch qualifier");
2864 }
2865 $$ = $2;
2866 $$.flags.q.patch = 1;
2867 }
2868 ;
2869
2870 instance_name_opt:
2871 /* empty */
2872 {
2873 $$ = new(state->linalloc) ast_interface_block(NULL, NULL);
2874 }
2875 | NEW_IDENTIFIER
2876 {
2877 $$ = new(state->linalloc) ast_interface_block($1, NULL);
2878 $$->set_location(@1);
2879 }
2880 | NEW_IDENTIFIER array_specifier
2881 {
2882 $$ = new(state->linalloc) ast_interface_block($1, $2);
2883 $$->set_location_range(@1, @2);
2884 }
2885 ;
2886
2887 member_list:
2888 member_declaration
2889 {
2890 $$ = $1;
2891 $1->link.self_link();
2892 }
2893 | member_declaration member_list
2894 {
2895 $$ = $1;
2896 $2->link.insert_before(& $$->link);
2897 }
2898 ;
2899
2900 member_declaration:
2901 fully_specified_type struct_declarator_list ';'
2902 {
2903 void *ctx = state->linalloc;
2904 ast_fully_specified_type *type = $1;
2905 type->set_location(@1);
2906
2907 if (type->qualifier.flags.q.attribute) {
2908 _mesa_glsl_error(& @1, state,
2909 "keyword 'attribute' cannot be used with "
2910 "interface block member");
2911 } else if (type->qualifier.flags.q.varying) {
2912 _mesa_glsl_error(& @1, state,
2913 "keyword 'varying' cannot be used with "
2914 "interface block member");
2915 }
2916
2917 $$ = new(ctx) ast_declarator_list(type);
2918 $$->set_location(@2);
2919
2920 $$->declarations.push_degenerate_list_at_head(& $2->link);
2921 }
2922 ;
2923
2924 layout_uniform_defaults:
2925 layout_qualifier layout_uniform_defaults
2926 {
2927 $$ = $1;
2928 if (!$$.merge_qualifier(& @1, state, $2, false, true)) {
2929 YYERROR;
2930 }
2931 }
2932 | layout_qualifier UNIFORM ';'
2933 ;
2934
2935 layout_buffer_defaults:
2936 layout_qualifier layout_buffer_defaults
2937 {
2938 $$ = $1;
2939 if (!$$.merge_qualifier(& @1, state, $2, false, true)) {
2940 YYERROR;
2941 }
2942 }
2943 | layout_qualifier BUFFER ';'
2944 ;
2945
2946 layout_in_defaults:
2947 layout_qualifier layout_in_defaults
2948 {
2949 $$ = $1;
2950 if (!$$.merge_qualifier(& @1, state, $2, false, true)) {
2951 YYERROR;
2952 }
2953 if (!$$.validate_in_qualifier(& @1, state)) {
2954 YYERROR;
2955 }
2956 }
2957 | layout_qualifier IN_TOK ';'
2958 {
2959 if (!$1.validate_in_qualifier(& @1, state)) {
2960 YYERROR;
2961 }
2962 }
2963 ;
2964
2965 layout_out_defaults:
2966 layout_qualifier layout_out_defaults
2967 {
2968 $$ = $1;
2969 if (!$$.merge_qualifier(& @1, state, $2, false, true)) {
2970 YYERROR;
2971 }
2972 if (!$$.validate_out_qualifier(& @1, state)) {
2973 YYERROR;
2974 }
2975 }
2976 | layout_qualifier OUT_TOK ';'
2977 {
2978 if (!$1.validate_out_qualifier(& @1, state)) {
2979 YYERROR;
2980 }
2981 }
2982 ;
2983
2984 layout_defaults:
2985 layout_uniform_defaults
2986 {
2987 $$ = NULL;
2988 if (!state->default_uniform_qualifier->
2989 merge_qualifier(& @1, state, $1, false)) {
2990 YYERROR;
2991 }
2992 if (!state->default_uniform_qualifier->
2993 push_to_global(& @1, state)) {
2994 YYERROR;
2995 }
2996 }
2997 | layout_buffer_defaults
2998 {
2999 $$ = NULL;
3000 if (!state->default_shader_storage_qualifier->
3001 merge_qualifier(& @1, state, $1, false)) {
3002 YYERROR;
3003 }
3004 if (!state->default_shader_storage_qualifier->
3005 push_to_global(& @1, state)) {
3006 YYERROR;
3007 }
3008
3009 /* From the GLSL 4.50 spec, section 4.4.5:
3010 *
3011 * "It is a compile-time error to specify the binding identifier for
3012 * the global scope or for block member declarations."
3013 */
3014 if (state->default_shader_storage_qualifier->flags.q.explicit_binding) {
3015 _mesa_glsl_error(& @1, state,
3016 "binding qualifier cannot be set for default layout");
3017 }
3018 }
3019 | layout_in_defaults
3020 {
3021 $$ = NULL;
3022 if (!$1.merge_into_in_qualifier(& @1, state, $$)) {
3023 YYERROR;
3024 }
3025 if (!state->in_qualifier->push_to_global(& @1, state)) {
3026 YYERROR;
3027 }
3028 }
3029 | layout_out_defaults
3030 {
3031 $$ = NULL;
3032 if (!$1.merge_into_out_qualifier(& @1, state, $$)) {
3033 YYERROR;
3034 }
3035 if (!state->out_qualifier->push_to_global(& @1, state)) {
3036 YYERROR;
3037 }
3038 }
3039 ;