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