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