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