glsl: Put `sample`-qualified varyings in their own packing classes
[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 #include <assert.h>
28
29 #include "ast.h"
30 #include "glsl_parser_extras.h"
31 #include "glsl_types.h"
32 #include "main/context.h"
33
34 #undef yyerror
35
36 static void yyerror(YYLTYPE *loc, _mesa_glsl_parse_state *st, const char *msg)
37 {
38 _mesa_glsl_error(loc, st, "%s", msg);
39 }
40
41 static int
42 _mesa_glsl_lex(YYSTYPE *val, YYLTYPE *loc, _mesa_glsl_parse_state *state)
43 {
44 return _mesa_glsl_lexer_lex(val, loc, state->scanner);
45 }
46
47 static bool match_layout_qualifier(const char *s1, const char *s2,
48 _mesa_glsl_parse_state *state)
49 {
50 /* From the GLSL 1.50 spec, section 4.3.8 (Layout Qualifiers):
51 *
52 * "The tokens in any layout-qualifier-id-list ... are not case
53 * sensitive, unless explicitly noted otherwise."
54 *
55 * The text "unless explicitly noted otherwise" appears to be
56 * vacuous--no desktop GLSL spec (up through GLSL 4.40) notes
57 * otherwise.
58 *
59 * However, the GLSL ES 3.00 spec says, in section 4.3.8 (Layout
60 * Qualifiers):
61 *
62 * "As for other identifiers, they are case sensitive."
63 *
64 * So we need to do a case-sensitive or a case-insensitive match,
65 * depending on whether we are compiling for GLSL ES.
66 */
67 if (state->es_shader)
68 return strcmp(s1, s2);
69 else
70 return strcasecmp(s1, s2);
71 }
72 %}
73
74 %expect 0
75
76 %pure-parser
77 %error-verbose
78
79 %locations
80 %initial-action {
81 @$.first_line = 1;
82 @$.first_column = 1;
83 @$.last_line = 1;
84 @$.last_column = 1;
85 @$.source = 0;
86 }
87
88 %lex-param {struct _mesa_glsl_parse_state *state}
89 %parse-param {struct _mesa_glsl_parse_state *state}
90
91 %union {
92 int n;
93 float real;
94 const char *identifier;
95
96 struct ast_type_qualifier type_qualifier;
97
98 ast_node *node;
99 ast_type_specifier *type_specifier;
100 ast_fully_specified_type *fully_specified_type;
101 ast_function *function;
102 ast_parameter_declarator *parameter_declarator;
103 ast_function_definition *function_definition;
104 ast_compound_statement *compound_statement;
105 ast_expression *expression;
106 ast_declarator_list *declarator_list;
107 ast_struct_specifier *struct_specifier;
108 ast_declaration *declaration;
109 ast_switch_body *switch_body;
110 ast_case_label *case_label;
111 ast_case_label_list *case_label_list;
112 ast_case_statement *case_statement;
113 ast_case_statement_list *case_statement_list;
114 ast_interface_block *interface_block;
115
116 struct {
117 ast_node *cond;
118 ast_expression *rest;
119 } for_rest_statement;
120
121 struct {
122 ast_node *then_statement;
123 ast_node *else_statement;
124 } selection_rest_statement;
125 }
126
127 %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK
128 %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
129 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
130 %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING
131 %token NOPERSPECTIVE FLAT SMOOTH
132 %token MAT2X2 MAT2X3 MAT2X4
133 %token MAT3X2 MAT3X3 MAT3X4
134 %token MAT4X2 MAT4X3 MAT4X4
135 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
136 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
137 %token SAMPLER2DARRAYSHADOW SAMPLERCUBEARRAY SAMPLERCUBEARRAYSHADOW
138 %token ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
139 %token ISAMPLER1DARRAY ISAMPLER2DARRAY ISAMPLERCUBEARRAY
140 %token USAMPLER1D USAMPLER2D USAMPLER3D USAMPLERCUBE USAMPLER1DARRAY
141 %token USAMPLER2DARRAY USAMPLERCUBEARRAY
142 %token SAMPLER2DRECT ISAMPLER2DRECT USAMPLER2DRECT SAMPLER2DRECTSHADOW
143 %token SAMPLERBUFFER ISAMPLERBUFFER USAMPLERBUFFER
144 %token SAMPLER2DMS ISAMPLER2DMS USAMPLER2DMS
145 %token SAMPLER2DMSARRAY ISAMPLER2DMSARRAY USAMPLER2DMSARRAY
146 %token SAMPLEREXTERNALOES
147 %token ATOMIC_UINT
148 %token STRUCT VOID_TOK WHILE
149 %token <identifier> IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
150 %type <identifier> any_identifier
151 %type <interface_block> instance_name_opt
152 %token <real> FLOATCONSTANT
153 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
154 %token <identifier> FIELD_SELECTION
155 %token LEFT_OP RIGHT_OP
156 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
157 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
158 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
159 %token SUB_ASSIGN
160 %token INVARIANT
161 %token LOWP MEDIUMP HIGHP SUPERP PRECISION
162
163 %token VERSION_TOK EXTENSION LINE COLON EOL INTERFACE OUTPUT
164 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
165 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
166 %token PRAGMA_INVARIANT_ALL
167 %token LAYOUT_TOK
168
169 /* Reserved words that are not actually used in the grammar.
170 */
171 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
172 %token INLINE_TOK NOINLINE VOLATILE PUBLIC_TOK STATIC EXTERN EXTERNAL
173 %token LONG_TOK SHORT_TOK DOUBLE_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK OUPTUT
174 %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
175 %token SAMPLER3DRECT
176 %token SIZEOF CAST NAMESPACE USING
177 %token COHERENT RESTRICT READONLY WRITEONLY RESOURCE PATCH SAMPLE
178 %token SUBROUTINE
179
180 %token ERROR_TOK
181
182 %token COMMON PARTITION ACTIVE FILTER
183 %token IMAGE1D IMAGE2D IMAGE3D IMAGECUBE IMAGE1DARRAY IMAGE2DARRAY
184 %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGECUBE IIMAGE1DARRAY IIMAGE2DARRAY
185 %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGECUBE UIMAGE1DARRAY UIMAGE2DARRAY
186 %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGEBUFFER IIMAGEBUFFER UIMAGEBUFFER
187 %token IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW
188 %token ROW_MAJOR
189
190 %type <identifier> variable_identifier
191 %type <node> statement
192 %type <node> statement_list
193 %type <node> simple_statement
194 %type <n> precision_qualifier
195 %type <type_qualifier> type_qualifier
196 %type <type_qualifier> auxiliary_storage_qualifier
197 %type <type_qualifier> storage_qualifier
198 %type <type_qualifier> interpolation_qualifier
199 %type <type_qualifier> layout_qualifier
200 %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
201 %type <type_qualifier> interface_block_layout_qualifier
202 %type <type_qualifier> interface_qualifier
203 %type <type_specifier> type_specifier
204 %type <type_specifier> type_specifier_nonarray
205 %type <identifier> basic_type_specifier_nonarray
206 %type <fully_specified_type> fully_specified_type
207 %type <function> function_prototype
208 %type <function> function_header
209 %type <function> function_header_with_parameters
210 %type <function> function_declarator
211 %type <parameter_declarator> parameter_declarator
212 %type <parameter_declarator> parameter_declaration
213 %type <type_qualifier> parameter_qualifier
214 %type <type_qualifier> parameter_direction_qualifier
215 %type <type_specifier> parameter_type_specifier
216 %type <function_definition> function_definition
217 %type <compound_statement> compound_statement_no_new_scope
218 %type <compound_statement> compound_statement
219 %type <node> statement_no_new_scope
220 %type <node> expression_statement
221 %type <expression> expression
222 %type <expression> primary_expression
223 %type <expression> assignment_expression
224 %type <expression> conditional_expression
225 %type <expression> logical_or_expression
226 %type <expression> logical_xor_expression
227 %type <expression> logical_and_expression
228 %type <expression> inclusive_or_expression
229 %type <expression> exclusive_or_expression
230 %type <expression> and_expression
231 %type <expression> equality_expression
232 %type <expression> relational_expression
233 %type <expression> shift_expression
234 %type <expression> additive_expression
235 %type <expression> multiplicative_expression
236 %type <expression> unary_expression
237 %type <expression> constant_expression
238 %type <expression> integer_expression
239 %type <expression> postfix_expression
240 %type <expression> function_call_header_with_parameters
241 %type <expression> function_call_header_no_parameters
242 %type <expression> function_call_header
243 %type <expression> function_call_generic
244 %type <expression> function_call_or_method
245 %type <expression> function_call
246 %type <expression> method_call_generic
247 %type <expression> method_call_header_with_parameters
248 %type <expression> method_call_header_no_parameters
249 %type <expression> method_call_header
250 %type <n> assignment_operator
251 %type <n> unary_operator
252 %type <expression> function_identifier
253 %type <node> external_declaration
254 %type <declarator_list> init_declarator_list
255 %type <declarator_list> single_declaration
256 %type <expression> initializer
257 %type <expression> initializer_list
258 %type <node> declaration
259 %type <node> declaration_statement
260 %type <node> jump_statement
261 %type <node> interface_block
262 %type <interface_block> basic_interface_block
263 %type <struct_specifier> struct_specifier
264 %type <declarator_list> struct_declaration_list
265 %type <declarator_list> struct_declaration
266 %type <declaration> struct_declarator
267 %type <declaration> struct_declarator_list
268 %type <declarator_list> member_list
269 %type <declarator_list> member_declaration
270 %type <node> selection_statement
271 %type <selection_rest_statement> selection_rest_statement
272 %type <node> switch_statement
273 %type <switch_body> switch_body
274 %type <case_label_list> case_label_list
275 %type <case_label> case_label
276 %type <case_statement> case_statement
277 %type <case_statement_list> case_statement_list
278 %type <node> iteration_statement
279 %type <node> condition
280 %type <node> conditionopt
281 %type <node> for_init_statement
282 %type <for_rest_statement> for_rest_statement
283 %type <n> integer_constant
284 %type <node> layout_defaults
285
286 %right THEN ELSE
287 %%
288
289 translation_unit:
290 version_statement extension_statement_list
291 {
292 _mesa_glsl_initialize_types(state);
293 }
294 external_declaration_list
295 {
296 delete state->symbols;
297 state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
298 _mesa_glsl_initialize_types(state);
299 }
300 ;
301
302 version_statement:
303 /* blank - no #version specified: defaults are already set */
304 | VERSION_TOK INTCONSTANT EOL
305 {
306 state->process_version_directive(&@2, $2, NULL);
307 if (state->error) {
308 YYERROR;
309 }
310 }
311 | VERSION_TOK INTCONSTANT any_identifier EOL
312 {
313 state->process_version_directive(&@2, $2, $3);
314 if (state->error) {
315 YYERROR;
316 }
317 }
318 ;
319
320 pragma_statement:
321 PRAGMA_DEBUG_ON EOL
322 | PRAGMA_DEBUG_OFF EOL
323 | PRAGMA_OPTIMIZE_ON EOL
324 | PRAGMA_OPTIMIZE_OFF EOL
325 | PRAGMA_INVARIANT_ALL EOL
326 {
327 if (!state->is_version(120, 100)) {
328 _mesa_glsl_warning(& @1, state,
329 "pragma `invariant(all)' not supported in %s "
330 "(GLSL ES 1.00 or GLSL 1.20 required)",
331 state->get_version_string());
332 } else {
333 state->all_invariant = true;
334 }
335 }
336 ;
337
338 extension_statement_list:
339
340 | extension_statement_list extension_statement
341 ;
342
343 any_identifier:
344 IDENTIFIER
345 | TYPE_IDENTIFIER
346 | NEW_IDENTIFIER
347 ;
348
349 extension_statement:
350 EXTENSION any_identifier COLON any_identifier EOL
351 {
352 if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
353 YYERROR;
354 }
355 }
356 ;
357
358 external_declaration_list:
359 external_declaration
360 {
361 /* FINISHME: The NULL test is required because pragmas are set to
362 * FINISHME: NULL. (See production rule for external_declaration.)
363 */
364 if ($1 != NULL)
365 state->translation_unit.push_tail(& $1->link);
366 }
367 | external_declaration_list external_declaration
368 {
369 /* FINISHME: The NULL test is required because pragmas are set to
370 * FINISHME: NULL. (See production rule for external_declaration.)
371 */
372 if ($2 != NULL)
373 state->translation_unit.push_tail(& $2->link);
374 }
375 ;
376
377 variable_identifier:
378 IDENTIFIER
379 | NEW_IDENTIFIER
380 ;
381
382 primary_expression:
383 variable_identifier
384 {
385 void *ctx = state;
386 $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
387 $$->set_location(yylloc);
388 $$->primary_expression.identifier = $1;
389 }
390 | INTCONSTANT
391 {
392 void *ctx = state;
393 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
394 $$->set_location(yylloc);
395 $$->primary_expression.int_constant = $1;
396 }
397 | UINTCONSTANT
398 {
399 void *ctx = state;
400 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
401 $$->set_location(yylloc);
402 $$->primary_expression.uint_constant = $1;
403 }
404 | FLOATCONSTANT
405 {
406 void *ctx = state;
407 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
408 $$->set_location(yylloc);
409 $$->primary_expression.float_constant = $1;
410 }
411 | BOOLCONSTANT
412 {
413 void *ctx = state;
414 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
415 $$->set_location(yylloc);
416 $$->primary_expression.bool_constant = $1;
417 }
418 | '(' expression ')'
419 {
420 $$ = $2;
421 }
422 ;
423
424 postfix_expression:
425 primary_expression
426 | postfix_expression '[' integer_expression ']'
427 {
428 void *ctx = state;
429 $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
430 $$->set_location(yylloc);
431 }
432 | function_call
433 {
434 $$ = $1;
435 }
436 | postfix_expression '.' any_identifier
437 {
438 void *ctx = state;
439 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
440 $$->set_location(yylloc);
441 $$->primary_expression.identifier = $3;
442 }
443 | postfix_expression INC_OP
444 {
445 void *ctx = state;
446 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
447 $$->set_location(yylloc);
448 }
449 | postfix_expression DEC_OP
450 {
451 void *ctx = state;
452 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
453 $$->set_location(yylloc);
454 }
455 ;
456
457 integer_expression:
458 expression
459 ;
460
461 function_call:
462 function_call_or_method
463 ;
464
465 function_call_or_method:
466 function_call_generic
467 | postfix_expression '.' method_call_generic
468 {
469 void *ctx = state;
470 $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
471 $$->set_location(yylloc);
472 }
473 ;
474
475 function_call_generic:
476 function_call_header_with_parameters ')'
477 | function_call_header_no_parameters ')'
478 ;
479
480 function_call_header_no_parameters:
481 function_call_header VOID_TOK
482 | function_call_header
483 ;
484
485 function_call_header_with_parameters:
486 function_call_header assignment_expression
487 {
488 $$ = $1;
489 $$->set_location(yylloc);
490 $$->expressions.push_tail(& $2->link);
491 }
492 | function_call_header_with_parameters ',' assignment_expression
493 {
494 $$ = $1;
495 $$->set_location(yylloc);
496 $$->expressions.push_tail(& $3->link);
497 }
498 ;
499
500 // Grammar Note: Constructors look like functions, but lexical
501 // analysis recognized most of them as keywords. They are now
502 // recognized through "type_specifier".
503 function_call_header:
504 function_identifier '('
505 ;
506
507 function_identifier:
508 type_specifier
509 {
510 void *ctx = state;
511 $$ = new(ctx) ast_function_expression($1);
512 $$->set_location(yylloc);
513 }
514 | variable_identifier
515 {
516 void *ctx = state;
517 ast_expression *callee = new(ctx) ast_expression($1);
518 $$ = new(ctx) ast_function_expression(callee);
519 $$->set_location(yylloc);
520 }
521 | FIELD_SELECTION
522 {
523 void *ctx = state;
524 ast_expression *callee = new(ctx) ast_expression($1);
525 $$ = new(ctx) ast_function_expression(callee);
526 $$->set_location(yylloc);
527 }
528 ;
529
530 method_call_generic:
531 method_call_header_with_parameters ')'
532 | method_call_header_no_parameters ')'
533 ;
534
535 method_call_header_no_parameters:
536 method_call_header VOID_TOK
537 | method_call_header
538 ;
539
540 method_call_header_with_parameters:
541 method_call_header assignment_expression
542 {
543 $$ = $1;
544 $$->set_location(yylloc);
545 $$->expressions.push_tail(& $2->link);
546 }
547 | method_call_header_with_parameters ',' assignment_expression
548 {
549 $$ = $1;
550 $$->set_location(yylloc);
551 $$->expressions.push_tail(& $3->link);
552 }
553 ;
554
555 // Grammar Note: Constructors look like methods, but lexical
556 // analysis recognized most of them as keywords. They are now
557 // recognized through "type_specifier".
558 method_call_header:
559 variable_identifier '('
560 {
561 void *ctx = state;
562 ast_expression *callee = new(ctx) ast_expression($1);
563 $$ = new(ctx) ast_function_expression(callee);
564 $$->set_location(yylloc);
565 }
566 ;
567
568 // Grammar Note: No traditional style type casts.
569 unary_expression:
570 postfix_expression
571 | INC_OP unary_expression
572 {
573 void *ctx = state;
574 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
575 $$->set_location(yylloc);
576 }
577 | DEC_OP unary_expression
578 {
579 void *ctx = state;
580 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
581 $$->set_location(yylloc);
582 }
583 | unary_operator unary_expression
584 {
585 void *ctx = state;
586 $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
587 $$->set_location(yylloc);
588 }
589 ;
590
591 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
592 unary_operator:
593 '+' { $$ = ast_plus; }
594 | '-' { $$ = ast_neg; }
595 | '!' { $$ = ast_logic_not; }
596 | '~' { $$ = ast_bit_not; }
597 ;
598
599 multiplicative_expression:
600 unary_expression
601 | multiplicative_expression '*' unary_expression
602 {
603 void *ctx = state;
604 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
605 $$->set_location(yylloc);
606 }
607 | multiplicative_expression '/' unary_expression
608 {
609 void *ctx = state;
610 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
611 $$->set_location(yylloc);
612 }
613 | multiplicative_expression '%' unary_expression
614 {
615 void *ctx = state;
616 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
617 $$->set_location(yylloc);
618 }
619 ;
620
621 additive_expression:
622 multiplicative_expression
623 | additive_expression '+' multiplicative_expression
624 {
625 void *ctx = state;
626 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
627 $$->set_location(yylloc);
628 }
629 | additive_expression '-' multiplicative_expression
630 {
631 void *ctx = state;
632 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
633 $$->set_location(yylloc);
634 }
635 ;
636
637 shift_expression:
638 additive_expression
639 | shift_expression LEFT_OP additive_expression
640 {
641 void *ctx = state;
642 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
643 $$->set_location(yylloc);
644 }
645 | shift_expression RIGHT_OP additive_expression
646 {
647 void *ctx = state;
648 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
649 $$->set_location(yylloc);
650 }
651 ;
652
653 relational_expression:
654 shift_expression
655 | relational_expression '<' shift_expression
656 {
657 void *ctx = state;
658 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
659 $$->set_location(yylloc);
660 }
661 | relational_expression '>' shift_expression
662 {
663 void *ctx = state;
664 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
665 $$->set_location(yylloc);
666 }
667 | relational_expression LE_OP shift_expression
668 {
669 void *ctx = state;
670 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
671 $$->set_location(yylloc);
672 }
673 | relational_expression GE_OP shift_expression
674 {
675 void *ctx = state;
676 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
677 $$->set_location(yylloc);
678 }
679 ;
680
681 equality_expression:
682 relational_expression
683 | equality_expression EQ_OP relational_expression
684 {
685 void *ctx = state;
686 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
687 $$->set_location(yylloc);
688 }
689 | equality_expression NE_OP relational_expression
690 {
691 void *ctx = state;
692 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
693 $$->set_location(yylloc);
694 }
695 ;
696
697 and_expression:
698 equality_expression
699 | and_expression '&' equality_expression
700 {
701 void *ctx = state;
702 $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
703 $$->set_location(yylloc);
704 }
705 ;
706
707 exclusive_or_expression:
708 and_expression
709 | exclusive_or_expression '^' and_expression
710 {
711 void *ctx = state;
712 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
713 $$->set_location(yylloc);
714 }
715 ;
716
717 inclusive_or_expression:
718 exclusive_or_expression
719 | inclusive_or_expression '|' exclusive_or_expression
720 {
721 void *ctx = state;
722 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
723 $$->set_location(yylloc);
724 }
725 ;
726
727 logical_and_expression:
728 inclusive_or_expression
729 | logical_and_expression AND_OP inclusive_or_expression
730 {
731 void *ctx = state;
732 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
733 $$->set_location(yylloc);
734 }
735 ;
736
737 logical_xor_expression:
738 logical_and_expression
739 | logical_xor_expression XOR_OP logical_and_expression
740 {
741 void *ctx = state;
742 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
743 $$->set_location(yylloc);
744 }
745 ;
746
747 logical_or_expression:
748 logical_xor_expression
749 | logical_or_expression OR_OP logical_xor_expression
750 {
751 void *ctx = state;
752 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
753 $$->set_location(yylloc);
754 }
755 ;
756
757 conditional_expression:
758 logical_or_expression
759 | logical_or_expression '?' expression ':' assignment_expression
760 {
761 void *ctx = state;
762 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
763 $$->set_location(yylloc);
764 }
765 ;
766
767 assignment_expression:
768 conditional_expression
769 | unary_expression assignment_operator assignment_expression
770 {
771 void *ctx = state;
772 $$ = new(ctx) ast_expression($2, $1, $3, NULL);
773 $$->set_location(yylloc);
774 }
775 ;
776
777 assignment_operator:
778 '=' { $$ = ast_assign; }
779 | MUL_ASSIGN { $$ = ast_mul_assign; }
780 | DIV_ASSIGN { $$ = ast_div_assign; }
781 | MOD_ASSIGN { $$ = ast_mod_assign; }
782 | ADD_ASSIGN { $$ = ast_add_assign; }
783 | SUB_ASSIGN { $$ = ast_sub_assign; }
784 | LEFT_ASSIGN { $$ = ast_ls_assign; }
785 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
786 | AND_ASSIGN { $$ = ast_and_assign; }
787 | XOR_ASSIGN { $$ = ast_xor_assign; }
788 | OR_ASSIGN { $$ = ast_or_assign; }
789 ;
790
791 expression:
792 assignment_expression
793 {
794 $$ = $1;
795 }
796 | expression ',' assignment_expression
797 {
798 void *ctx = state;
799 if ($1->oper != ast_sequence) {
800 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
801 $$->set_location(yylloc);
802 $$->expressions.push_tail(& $1->link);
803 } else {
804 $$ = $1;
805 }
806
807 $$->expressions.push_tail(& $3->link);
808 }
809 ;
810
811 constant_expression:
812 conditional_expression
813 ;
814
815 declaration:
816 function_prototype ';'
817 {
818 state->symbols->pop_scope();
819 $$ = $1;
820 }
821 | init_declarator_list ';'
822 {
823 $$ = $1;
824 }
825 | PRECISION precision_qualifier type_specifier ';'
826 {
827 $3->default_precision = $2;
828 $$ = $3;
829 }
830 | interface_block
831 {
832 $$ = $1;
833 }
834 ;
835
836 function_prototype:
837 function_declarator ')'
838 ;
839
840 function_declarator:
841 function_header
842 | function_header_with_parameters
843 ;
844
845 function_header_with_parameters:
846 function_header parameter_declaration
847 {
848 $$ = $1;
849 $$->parameters.push_tail(& $2->link);
850 }
851 | function_header_with_parameters ',' parameter_declaration
852 {
853 $$ = $1;
854 $$->parameters.push_tail(& $3->link);
855 }
856 ;
857
858 function_header:
859 fully_specified_type variable_identifier '('
860 {
861 void *ctx = state;
862 $$ = new(ctx) ast_function();
863 $$->set_location(yylloc);
864 $$->return_type = $1;
865 $$->identifier = $2;
866
867 state->symbols->add_function(new(state) ir_function($2));
868 state->symbols->push_scope();
869 }
870 ;
871
872 parameter_declarator:
873 type_specifier any_identifier
874 {
875 void *ctx = state;
876 $$ = new(ctx) ast_parameter_declarator();
877 $$->set_location(yylloc);
878 $$->type = new(ctx) ast_fully_specified_type();
879 $$->type->set_location(yylloc);
880 $$->type->specifier = $1;
881 $$->identifier = $2;
882 }
883 | type_specifier any_identifier '[' constant_expression ']'
884 {
885 void *ctx = state;
886 $$ = new(ctx) ast_parameter_declarator();
887 $$->set_location(yylloc);
888 $$->type = new(ctx) ast_fully_specified_type();
889 $$->type->set_location(yylloc);
890 $$->type->specifier = $1;
891 $$->identifier = $2;
892 $$->is_array = true;
893 $$->array_size = $4;
894 }
895 ;
896
897 parameter_declaration:
898 parameter_qualifier parameter_declarator
899 {
900 $$ = $2;
901 $$->type->qualifier = $1;
902 }
903 | parameter_qualifier parameter_type_specifier
904 {
905 void *ctx = state;
906 $$ = new(ctx) ast_parameter_declarator();
907 $$->set_location(yylloc);
908 $$->type = new(ctx) ast_fully_specified_type();
909 $$->type->qualifier = $1;
910 $$->type->specifier = $2;
911 }
912 ;
913
914 parameter_qualifier:
915 /* empty */
916 {
917 memset(& $$, 0, sizeof($$));
918 }
919 | CONST_TOK parameter_qualifier
920 {
921 if ($2.flags.q.constant)
922 _mesa_glsl_error(&@1, state, "duplicate const qualifier");
923
924 $$ = $2;
925 $$.flags.q.constant = 1;
926 }
927 | parameter_direction_qualifier parameter_qualifier
928 {
929 if (($1.flags.q.in || $1.flags.q.out) && ($2.flags.q.in || $2.flags.q.out))
930 _mesa_glsl_error(&@1, state, "duplicate in/out/inout qualifier");
931
932 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.constant)
933 _mesa_glsl_error(&@1, state, "const must be specified before "
934 "in/out/inout");
935
936 $$ = $1;
937 $$.merge_qualifier(&@1, state, $2);
938 }
939 | precision_qualifier parameter_qualifier
940 {
941 if ($2.precision != ast_precision_none)
942 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
943
944 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
945 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
946
947 $$ = $2;
948 $$.precision = $1;
949 }
950
951 parameter_direction_qualifier:
952 IN_TOK
953 {
954 memset(& $$, 0, sizeof($$));
955 $$.flags.q.in = 1;
956 }
957 | OUT_TOK
958 {
959 memset(& $$, 0, sizeof($$));
960 $$.flags.q.out = 1;
961 }
962 | INOUT_TOK
963 {
964 memset(& $$, 0, sizeof($$));
965 $$.flags.q.in = 1;
966 $$.flags.q.out = 1;
967 }
968 ;
969
970 parameter_type_specifier:
971 type_specifier
972 ;
973
974 init_declarator_list:
975 single_declaration
976 | init_declarator_list ',' any_identifier
977 {
978 void *ctx = state;
979 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
980 decl->set_location(yylloc);
981
982 $$ = $1;
983 $$->declarations.push_tail(&decl->link);
984 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
985 }
986 | init_declarator_list ',' any_identifier '[' ']'
987 {
988 void *ctx = state;
989 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
990 decl->set_location(yylloc);
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 '[' constant_expression ']'
997 {
998 void *ctx = state;
999 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
1000 decl->set_location(yylloc);
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 '[' ']' '=' initializer
1007 {
1008 void *ctx = state;
1009 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7);
1010 decl->set_location(yylloc);
1011
1012 $$ = $1;
1013 $$->declarations.push_tail(&decl->link);
1014 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1015 if ($7->oper == ast_aggregate) {
1016 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$7;
1017 ast_type_specifier *type = new(ctx) ast_type_specifier($1->type->specifier, true, NULL);
1018 _mesa_ast_set_aggregate_type(type, ai, state);
1019 }
1020 }
1021 | init_declarator_list ',' any_identifier '[' constant_expression ']' '=' initializer
1022 {
1023 void *ctx = state;
1024 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
1025 decl->set_location(yylloc);
1026
1027 $$ = $1;
1028 $$->declarations.push_tail(&decl->link);
1029 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1030 if ($8->oper == ast_aggregate) {
1031 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$8;
1032 ast_type_specifier *type = new(ctx) ast_type_specifier($1->type->specifier, true, $5);
1033 _mesa_ast_set_aggregate_type(type, ai, state);
1034 }
1035 }
1036 | init_declarator_list ',' any_identifier '=' initializer
1037 {
1038 void *ctx = state;
1039 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
1040 decl->set_location(yylloc);
1041
1042 $$ = $1;
1043 $$->declarations.push_tail(&decl->link);
1044 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1045 if ($5->oper == ast_aggregate) {
1046 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$5;
1047 _mesa_ast_set_aggregate_type($1->type->specifier, ai, state);
1048 }
1049 }
1050 ;
1051
1052 // Grammar Note: No 'enum', or 'typedef'.
1053 single_declaration:
1054 fully_specified_type
1055 {
1056 void *ctx = state;
1057 /* Empty declaration list is valid. */
1058 $$ = new(ctx) ast_declarator_list($1);
1059 $$->set_location(yylloc);
1060 }
1061 | fully_specified_type any_identifier
1062 {
1063 void *ctx = state;
1064 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1065
1066 $$ = new(ctx) ast_declarator_list($1);
1067 $$->set_location(yylloc);
1068 $$->declarations.push_tail(&decl->link);
1069 }
1070 | fully_specified_type any_identifier '[' ']'
1071 {
1072 void *ctx = state;
1073 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
1074
1075 $$ = new(ctx) ast_declarator_list($1);
1076 $$->set_location(yylloc);
1077 $$->declarations.push_tail(&decl->link);
1078 }
1079 | fully_specified_type any_identifier '[' constant_expression ']'
1080 {
1081 void *ctx = state;
1082 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
1083
1084 $$ = new(ctx) ast_declarator_list($1);
1085 $$->set_location(yylloc);
1086 $$->declarations.push_tail(&decl->link);
1087 }
1088 | fully_specified_type any_identifier '[' ']' '=' initializer
1089 {
1090 void *ctx = state;
1091 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6);
1092
1093 $$ = new(ctx) ast_declarator_list($1);
1094 $$->set_location(yylloc);
1095 $$->declarations.push_tail(&decl->link);
1096 if ($6->oper == ast_aggregate) {
1097 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$6;
1098 ast_type_specifier *type = new(ctx) ast_type_specifier($1->specifier, true, NULL);
1099 _mesa_ast_set_aggregate_type(type, ai, state);
1100 }
1101 }
1102 | fully_specified_type any_identifier '[' constant_expression ']' '=' initializer
1103 {
1104 void *ctx = state;
1105 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
1106
1107 $$ = new(ctx) ast_declarator_list($1);
1108 $$->set_location(yylloc);
1109 $$->declarations.push_tail(&decl->link);
1110 if ($7->oper == ast_aggregate) {
1111 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$7;
1112 ast_type_specifier *type = new(ctx) ast_type_specifier($1->specifier, true, $4);
1113 _mesa_ast_set_aggregate_type(type, ai, state);
1114 }
1115 }
1116 | fully_specified_type any_identifier '=' initializer
1117 {
1118 void *ctx = state;
1119 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1120
1121 $$ = new(ctx) ast_declarator_list($1);
1122 $$->set_location(yylloc);
1123 $$->declarations.push_tail(&decl->link);
1124 if ($4->oper == ast_aggregate) {
1125 _mesa_ast_set_aggregate_type($1->specifier, $4, state);
1126 }
1127 }
1128 | INVARIANT variable_identifier // Vertex only.
1129 {
1130 void *ctx = state;
1131 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1132
1133 $$ = new(ctx) ast_declarator_list(NULL);
1134 $$->set_location(yylloc);
1135 $$->invariant = true;
1136
1137 $$->declarations.push_tail(&decl->link);
1138 }
1139 ;
1140
1141 fully_specified_type:
1142 type_specifier
1143 {
1144 void *ctx = state;
1145 $$ = new(ctx) ast_fully_specified_type();
1146 $$->set_location(yylloc);
1147 $$->specifier = $1;
1148 }
1149 | type_qualifier type_specifier
1150 {
1151 void *ctx = state;
1152 $$ = new(ctx) ast_fully_specified_type();
1153 $$->set_location(yylloc);
1154 $$->qualifier = $1;
1155 $$->specifier = $2;
1156 }
1157 ;
1158
1159 layout_qualifier:
1160 LAYOUT_TOK '(' layout_qualifier_id_list ')'
1161 {
1162 $$ = $3;
1163 }
1164 ;
1165
1166 layout_qualifier_id_list:
1167 layout_qualifier_id
1168 | layout_qualifier_id_list ',' layout_qualifier_id
1169 {
1170 $$ = $1;
1171 if (!$$.merge_qualifier(& @3, state, $3)) {
1172 YYERROR;
1173 }
1174 }
1175 ;
1176
1177 integer_constant:
1178 INTCONSTANT { $$ = $1; }
1179 | UINTCONSTANT { $$ = $1; }
1180 ;
1181
1182 layout_qualifier_id:
1183 any_identifier
1184 {
1185 memset(& $$, 0, sizeof($$));
1186
1187 /* Layout qualifiers for ARB_fragment_coord_conventions. */
1188 if (!$$.flags.i && (state->ARB_fragment_coord_conventions_enable ||
1189 state->is_version(150, 0))) {
1190 if (match_layout_qualifier($1, "origin_upper_left", state) == 0) {
1191 $$.flags.q.origin_upper_left = 1;
1192 } else if (match_layout_qualifier($1, "pixel_center_integer",
1193 state) == 0) {
1194 $$.flags.q.pixel_center_integer = 1;
1195 }
1196
1197 if ($$.flags.i && state->ARB_fragment_coord_conventions_warn) {
1198 _mesa_glsl_warning(& @1, state,
1199 "GL_ARB_fragment_coord_conventions layout "
1200 "identifier `%s' used", $1);
1201 }
1202 }
1203
1204 /* Layout qualifiers for AMD/ARB_conservative_depth. */
1205 if (!$$.flags.i &&
1206 (state->AMD_conservative_depth_enable ||
1207 state->ARB_conservative_depth_enable)) {
1208 if (match_layout_qualifier($1, "depth_any", state) == 0) {
1209 $$.flags.q.depth_any = 1;
1210 } else if (match_layout_qualifier($1, "depth_greater", state) == 0) {
1211 $$.flags.q.depth_greater = 1;
1212 } else if (match_layout_qualifier($1, "depth_less", state) == 0) {
1213 $$.flags.q.depth_less = 1;
1214 } else if (match_layout_qualifier($1, "depth_unchanged",
1215 state) == 0) {
1216 $$.flags.q.depth_unchanged = 1;
1217 }
1218
1219 if ($$.flags.i && state->AMD_conservative_depth_warn) {
1220 _mesa_glsl_warning(& @1, state,
1221 "GL_AMD_conservative_depth "
1222 "layout qualifier `%s' is used", $1);
1223 }
1224 if ($$.flags.i && state->ARB_conservative_depth_warn) {
1225 _mesa_glsl_warning(& @1, state,
1226 "GL_ARB_conservative_depth "
1227 "layout qualifier `%s' is used", $1);
1228 }
1229 }
1230
1231 /* See also interface_block_layout_qualifier. */
1232 if (!$$.flags.i && state->has_uniform_buffer_objects()) {
1233 if (match_layout_qualifier($1, "std140", state) == 0) {
1234 $$.flags.q.std140 = 1;
1235 } else if (match_layout_qualifier($1, "shared", state) == 0) {
1236 $$.flags.q.shared = 1;
1237 } else if (match_layout_qualifier($1, "column_major", state) == 0) {
1238 $$.flags.q.column_major = 1;
1239 /* "row_major" is a reserved word in GLSL 1.30+. Its token is parsed
1240 * below in the interface_block_layout_qualifier rule.
1241 *
1242 * It is not a reserved word in GLSL ES 3.00, so it's handled here as
1243 * an identifier.
1244 *
1245 * Also, this takes care of alternate capitalizations of
1246 * "row_major" (which is necessary because layout qualifiers
1247 * are case-insensitive in desktop GLSL).
1248 */
1249 } else if (match_layout_qualifier($1, "row_major", state) == 0) {
1250 $$.flags.q.row_major = 1;
1251 /* "packed" is a reserved word in GLSL, and its token is
1252 * parsed below in the interface_block_layout_qualifier rule.
1253 * However, we must take care of alternate capitalizations of
1254 * "packed", because layout qualifiers are case-insensitive
1255 * in desktop GLSL.
1256 */
1257 } else if (match_layout_qualifier($1, "packed", state) == 0) {
1258 $$.flags.q.packed = 1;
1259 }
1260
1261 if ($$.flags.i && state->ARB_uniform_buffer_object_warn) {
1262 _mesa_glsl_warning(& @1, state,
1263 "#version 140 / GL_ARB_uniform_buffer_object "
1264 "layout qualifier `%s' is used", $1);
1265 }
1266 }
1267
1268 /* Layout qualifiers for GLSL 1.50 geometry shaders. */
1269 if (!$$.flags.i) {
1270 struct {
1271 const char *s;
1272 GLenum e;
1273 } map[] = {
1274 { "points", GL_POINTS },
1275 { "lines", GL_LINES },
1276 { "lines_adjacency", GL_LINES_ADJACENCY },
1277 { "line_strip", GL_LINE_STRIP },
1278 { "triangles", GL_TRIANGLES },
1279 { "triangles_adjacency", GL_TRIANGLES_ADJACENCY },
1280 { "triangle_strip", GL_TRIANGLE_STRIP },
1281 };
1282 for (unsigned i = 0; i < Elements(map); i++) {
1283 if (match_layout_qualifier($1, map[i].s, state) == 0) {
1284 $$.flags.q.prim_type = 1;
1285 $$.prim_type = map[i].e;
1286 break;
1287 }
1288 }
1289
1290 if ($$.flags.i && !state->is_version(150, 0)) {
1291 _mesa_glsl_error(& @1, state, "#version 150 layout "
1292 "qualifier `%s' used", $1);
1293 }
1294 }
1295
1296 if (!$$.flags.i) {
1297 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1298 "`%s'", $1);
1299 YYERROR;
1300 }
1301 }
1302 | any_identifier '=' integer_constant
1303 {
1304 memset(& $$, 0, sizeof($$));
1305
1306 if (match_layout_qualifier("location", $1, state) == 0) {
1307 $$.flags.q.explicit_location = 1;
1308
1309 if ($3 >= 0) {
1310 $$.location = $3;
1311 } else {
1312 _mesa_glsl_error(& @3, state, "invalid location %d specified", $3);
1313 YYERROR;
1314 }
1315 }
1316
1317 if (match_layout_qualifier("index", $1, state) == 0) {
1318 $$.flags.q.explicit_index = 1;
1319
1320 if ($3 >= 0) {
1321 $$.index = $3;
1322 } else {
1323 _mesa_glsl_error(& @3, state, "invalid index %d specified", $3);
1324 YYERROR;
1325 }
1326 }
1327
1328 if ((state->ARB_shading_language_420pack_enable ||
1329 state->ARB_shader_atomic_counters_enable) &&
1330 match_layout_qualifier("binding", $1, state) == 0) {
1331 $$.flags.q.explicit_binding = 1;
1332 $$.binding = $3;
1333 }
1334
1335 if (state->ARB_shader_atomic_counters_enable &&
1336 match_layout_qualifier("offset", $1, state) == 0) {
1337 $$.flags.q.explicit_offset = 1;
1338 $$.offset = $3;
1339 }
1340
1341 if (match_layout_qualifier("max_vertices", $1, state) == 0) {
1342 $$.flags.q.max_vertices = 1;
1343
1344 if ($3 < 0) {
1345 _mesa_glsl_error(& @3, state,
1346 "invalid max_vertices %d specified", $3);
1347 YYERROR;
1348 } else {
1349 $$.max_vertices = $3;
1350 if (!state->is_version(150, 0)) {
1351 _mesa_glsl_error(& @3, state,
1352 "#version 150 max_vertices qualifier "
1353 "specified", $3);
1354 }
1355 }
1356 }
1357
1358 /* If the identifier didn't match any known layout identifiers,
1359 * emit an error.
1360 */
1361 if (!$$.flags.i) {
1362 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1363 "`%s'", $1);
1364 YYERROR;
1365 } else if (state->ARB_explicit_attrib_location_warn) {
1366 _mesa_glsl_warning(& @1, state,
1367 "GL_ARB_explicit_attrib_location layout "
1368 "identifier `%s' used", $1);
1369 }
1370 }
1371 | interface_block_layout_qualifier
1372 {
1373 $$ = $1;
1374 /* Layout qualifiers for ARB_uniform_buffer_object. */
1375 if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
1376 _mesa_glsl_error(& @1, state,
1377 "#version 140 / GL_ARB_uniform_buffer_object "
1378 "layout qualifier `%s' is used", $1);
1379 } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1380 _mesa_glsl_warning(& @1, state,
1381 "#version 140 / GL_ARB_uniform_buffer_object "
1382 "layout qualifier `%s' is used", $1);
1383 }
1384 }
1385 ;
1386
1387 /* This is a separate language rule because we parse these as tokens
1388 * (due to them being reserved keywords) instead of identifiers like
1389 * most qualifiers. See the any_identifier path of
1390 * layout_qualifier_id for the others.
1391 *
1392 * Note that since layout qualifiers are case-insensitive in desktop
1393 * GLSL, all of these qualifiers need to be handled as identifiers as
1394 * well (by the any_identifier path of layout_qualifier_id).
1395 */
1396 interface_block_layout_qualifier:
1397 ROW_MAJOR
1398 {
1399 memset(& $$, 0, sizeof($$));
1400 $$.flags.q.row_major = 1;
1401 }
1402 | PACKED_TOK
1403 {
1404 memset(& $$, 0, sizeof($$));
1405 $$.flags.q.packed = 1;
1406 }
1407 ;
1408
1409 interpolation_qualifier:
1410 SMOOTH
1411 {
1412 memset(& $$, 0, sizeof($$));
1413 $$.flags.q.smooth = 1;
1414 }
1415 | FLAT
1416 {
1417 memset(& $$, 0, sizeof($$));
1418 $$.flags.q.flat = 1;
1419 }
1420 | NOPERSPECTIVE
1421 {
1422 memset(& $$, 0, sizeof($$));
1423 $$.flags.q.noperspective = 1;
1424 }
1425 ;
1426
1427 type_qualifier:
1428 /* Single qualifiers */
1429 INVARIANT
1430 {
1431 memset(& $$, 0, sizeof($$));
1432 $$.flags.q.invariant = 1;
1433 }
1434 | auxiliary_storage_qualifier
1435 | storage_qualifier
1436 | interpolation_qualifier
1437 | layout_qualifier
1438 | precision_qualifier
1439 {
1440 memset(&$$, 0, sizeof($$));
1441 $$.precision = $1;
1442 }
1443
1444 /* Multiple qualifiers:
1445 * In GLSL 4.20, these can be specified in any order. In earlier versions,
1446 * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1447 *
1448 * invariant interpolation auxiliary storage precision ...or...
1449 * layout storage precision
1450 *
1451 * Each qualifier's rule ensures that the accumulated qualifiers on the right
1452 * side don't contain any that must appear on the left hand side.
1453 * For example, when processing a storage qualifier, we check that there are
1454 * no auxiliary, interpolation, layout, or invariant qualifiers to the right.
1455 */
1456 | INVARIANT type_qualifier
1457 {
1458 if ($2.flags.q.invariant)
1459 _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1460
1461 if ($2.has_layout()) {
1462 _mesa_glsl_error(&@1, state,
1463 "\"invariant\" cannot be used with layout(...)");
1464 }
1465
1466 $$ = $2;
1467 $$.flags.q.invariant = 1;
1468 }
1469 | interpolation_qualifier type_qualifier
1470 {
1471 /* Section 4.3 of the GLSL 1.40 specification states:
1472 * "...qualified with one of these interpolation qualifiers"
1473 *
1474 * GLSL 1.30 claims to allow "one or more", but insists that:
1475 * "These interpolation qualifiers may only precede the qualifiers in,
1476 * centroid in, out, or centroid out in a declaration."
1477 *
1478 * ...which means that e.g. smooth can't precede smooth, so there can be
1479 * only one after all, and the 1.40 text is a clarification, not a change.
1480 */
1481 if ($2.has_interpolation())
1482 _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1483
1484 if ($2.has_layout()) {
1485 _mesa_glsl_error(&@1, state, "interpolation qualifiers cannot be used "
1486 "with layout(...)");
1487 }
1488
1489 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.invariant) {
1490 _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1491 "after \"invariant\"");
1492 }
1493
1494 $$ = $1;
1495 $$.merge_qualifier(&@1, state, $2);
1496 }
1497 | layout_qualifier type_qualifier
1498 {
1499 /* The GLSL 1.50 grammar indicates that a layout(...) declaration can be
1500 * used standalone or immediately before a storage qualifier. It cannot
1501 * be used with interpolation qualifiers or invariant. There does not
1502 * appear to be any text indicating that it must come before the storage
1503 * qualifier, but always seems to in examples.
1504 */
1505 if (!state->ARB_shading_language_420pack_enable && $2.has_layout())
1506 _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
1507
1508 if ($2.flags.q.invariant)
1509 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1510 "the \"invariant\" qualifier");
1511
1512 if ($2.has_interpolation()) {
1513 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1514 "interpolation qualifiers");
1515 }
1516
1517 $$ = $1;
1518 $$.merge_qualifier(&@1, state, $2);
1519 }
1520 | auxiliary_storage_qualifier type_qualifier
1521 {
1522 if ($2.has_auxiliary_storage()) {
1523 _mesa_glsl_error(&@1, state,
1524 "duplicate auxiliary storage qualifier (centroid or sample)");
1525 }
1526
1527 if (!state->ARB_shading_language_420pack_enable &&
1528 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout())) {
1529 _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
1530 "just before storage qualifiers");
1531 }
1532 $$ = $1;
1533 $$.flags.i |= $2.flags.i;
1534 }
1535 | storage_qualifier type_qualifier
1536 {
1537 /* Section 4.3 of the GLSL 1.20 specification states:
1538 * "Variable declarations may have a storage qualifier specified..."
1539 * 1.30 clarifies this to "may have one storage qualifier".
1540 */
1541 if ($2.has_storage())
1542 _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
1543
1544 if (!state->ARB_shading_language_420pack_enable &&
1545 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout() ||
1546 $2.has_auxiliary_storage())) {
1547 _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
1548 "invariant, interpolation, layout and auxiliary "
1549 "storage qualifiers");
1550 }
1551
1552 $$ = $1;
1553 $$.merge_qualifier(&@1, state, $2);
1554 }
1555 | precision_qualifier type_qualifier
1556 {
1557 if ($2.precision != ast_precision_none)
1558 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
1559
1560 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
1561 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
1562
1563 $$ = $2;
1564 $$.precision = $1;
1565 }
1566 ;
1567
1568 auxiliary_storage_qualifier:
1569 CENTROID
1570 {
1571 memset(& $$, 0, sizeof($$));
1572 $$.flags.q.centroid = 1;
1573 }
1574 | SAMPLE
1575 {
1576 memset(& $$, 0, sizeof($$));
1577 $$.flags.q.sample = 1;
1578 }
1579 /* TODO: "patch" also goes here someday. */
1580
1581 storage_qualifier:
1582 CONST_TOK
1583 {
1584 memset(& $$, 0, sizeof($$));
1585 $$.flags.q.constant = 1;
1586 }
1587 | ATTRIBUTE
1588 {
1589 memset(& $$, 0, sizeof($$));
1590 $$.flags.q.attribute = 1;
1591 }
1592 | VARYING
1593 {
1594 memset(& $$, 0, sizeof($$));
1595 $$.flags.q.varying = 1;
1596 }
1597 | IN_TOK
1598 {
1599 memset(& $$, 0, sizeof($$));
1600 $$.flags.q.in = 1;
1601 }
1602 | OUT_TOK
1603 {
1604 memset(& $$, 0, sizeof($$));
1605 $$.flags.q.out = 1;
1606 }
1607 | UNIFORM
1608 {
1609 memset(& $$, 0, sizeof($$));
1610 $$.flags.q.uniform = 1;
1611 }
1612 ;
1613
1614 type_specifier:
1615 type_specifier_nonarray
1616 | type_specifier_nonarray '[' ']'
1617 {
1618 $$ = $1;
1619 $$->is_array = true;
1620 $$->array_size = NULL;
1621 }
1622 | type_specifier_nonarray '[' constant_expression ']'
1623 {
1624 $$ = $1;
1625 $$->is_array = true;
1626 $$->array_size = $3;
1627 }
1628 ;
1629
1630 type_specifier_nonarray:
1631 basic_type_specifier_nonarray
1632 {
1633 void *ctx = state;
1634 $$ = new(ctx) ast_type_specifier($1);
1635 $$->set_location(yylloc);
1636 }
1637 | struct_specifier
1638 {
1639 void *ctx = state;
1640 $$ = new(ctx) ast_type_specifier($1);
1641 $$->set_location(yylloc);
1642 }
1643 | TYPE_IDENTIFIER
1644 {
1645 void *ctx = state;
1646 $$ = new(ctx) ast_type_specifier($1);
1647 $$->set_location(yylloc);
1648 }
1649 ;
1650
1651 basic_type_specifier_nonarray:
1652 VOID_TOK { $$ = "void"; }
1653 | FLOAT_TOK { $$ = "float"; }
1654 | INT_TOK { $$ = "int"; }
1655 | UINT_TOK { $$ = "uint"; }
1656 | BOOL_TOK { $$ = "bool"; }
1657 | VEC2 { $$ = "vec2"; }
1658 | VEC3 { $$ = "vec3"; }
1659 | VEC4 { $$ = "vec4"; }
1660 | BVEC2 { $$ = "bvec2"; }
1661 | BVEC3 { $$ = "bvec3"; }
1662 | BVEC4 { $$ = "bvec4"; }
1663 | IVEC2 { $$ = "ivec2"; }
1664 | IVEC3 { $$ = "ivec3"; }
1665 | IVEC4 { $$ = "ivec4"; }
1666 | UVEC2 { $$ = "uvec2"; }
1667 | UVEC3 { $$ = "uvec3"; }
1668 | UVEC4 { $$ = "uvec4"; }
1669 | MAT2X2 { $$ = "mat2"; }
1670 | MAT2X3 { $$ = "mat2x3"; }
1671 | MAT2X4 { $$ = "mat2x4"; }
1672 | MAT3X2 { $$ = "mat3x2"; }
1673 | MAT3X3 { $$ = "mat3"; }
1674 | MAT3X4 { $$ = "mat3x4"; }
1675 | MAT4X2 { $$ = "mat4x2"; }
1676 | MAT4X3 { $$ = "mat4x3"; }
1677 | MAT4X4 { $$ = "mat4"; }
1678 | SAMPLER1D { $$ = "sampler1D"; }
1679 | SAMPLER2D { $$ = "sampler2D"; }
1680 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
1681 | SAMPLER3D { $$ = "sampler3D"; }
1682 | SAMPLERCUBE { $$ = "samplerCube"; }
1683 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
1684 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
1685 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
1686 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
1687 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
1688 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
1689 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
1690 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
1691 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
1692 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
1693 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
1694 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
1695 | ISAMPLER1D { $$ = "isampler1D"; }
1696 | ISAMPLER2D { $$ = "isampler2D"; }
1697 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
1698 | ISAMPLER3D { $$ = "isampler3D"; }
1699 | ISAMPLERCUBE { $$ = "isamplerCube"; }
1700 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
1701 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
1702 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
1703 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
1704 | USAMPLER1D { $$ = "usampler1D"; }
1705 | USAMPLER2D { $$ = "usampler2D"; }
1706 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
1707 | USAMPLER3D { $$ = "usampler3D"; }
1708 | USAMPLERCUBE { $$ = "usamplerCube"; }
1709 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
1710 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
1711 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
1712 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
1713 | SAMPLER2DMS { $$ = "sampler2DMS"; }
1714 | ISAMPLER2DMS { $$ = "isampler2DMS"; }
1715 | USAMPLER2DMS { $$ = "usampler2DMS"; }
1716 | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
1717 | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
1718 | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
1719 | ATOMIC_UINT { $$ = "atomic_uint"; }
1720 ;
1721
1722 precision_qualifier:
1723 HIGHP
1724 {
1725 state->check_precision_qualifiers_allowed(&@1);
1726 $$ = ast_precision_high;
1727 }
1728 | MEDIUMP
1729 {
1730 state->check_precision_qualifiers_allowed(&@1);
1731 $$ = ast_precision_medium;
1732 }
1733 | LOWP
1734 {
1735 state->check_precision_qualifiers_allowed(&@1);
1736 $$ = ast_precision_low;
1737 }
1738 ;
1739
1740 struct_specifier:
1741 STRUCT any_identifier '{' struct_declaration_list '}'
1742 {
1743 void *ctx = state;
1744 $$ = new(ctx) ast_struct_specifier($2, $4);
1745 $$->set_location(yylloc);
1746 state->symbols->add_type($2, glsl_type::void_type);
1747 state->symbols->add_type_ast($2, new(ctx) ast_type_specifier($$));
1748 }
1749 | STRUCT '{' struct_declaration_list '}'
1750 {
1751 void *ctx = state;
1752 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1753 $$->set_location(yylloc);
1754 }
1755 ;
1756
1757 struct_declaration_list:
1758 struct_declaration
1759 {
1760 $$ = $1;
1761 $1->link.self_link();
1762 }
1763 | struct_declaration_list struct_declaration
1764 {
1765 $$ = $1;
1766 $$->link.insert_before(& $2->link);
1767 }
1768 ;
1769
1770 struct_declaration:
1771 fully_specified_type struct_declarator_list ';'
1772 {
1773 void *ctx = state;
1774 ast_fully_specified_type *const type = $1;
1775 type->set_location(yylloc);
1776
1777 if (type->qualifier.flags.i != 0)
1778 _mesa_glsl_error(&@1, state,
1779 "only precision qualifiers may be applied to "
1780 "structure members");
1781
1782 $$ = new(ctx) ast_declarator_list(type);
1783 $$->set_location(yylloc);
1784
1785 $$->declarations.push_degenerate_list_at_head(& $2->link);
1786 }
1787 ;
1788
1789 struct_declarator_list:
1790 struct_declarator
1791 {
1792 $$ = $1;
1793 $1->link.self_link();
1794 }
1795 | struct_declarator_list ',' struct_declarator
1796 {
1797 $$ = $1;
1798 $$->link.insert_before(& $3->link);
1799 }
1800 ;
1801
1802 struct_declarator:
1803 any_identifier
1804 {
1805 void *ctx = state;
1806 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1807 $$->set_location(yylloc);
1808 }
1809 | any_identifier '[' ']'
1810 {
1811 void *ctx = state;
1812 $$ = new(ctx) ast_declaration($1, true, NULL, NULL);
1813 $$->set_location(yylloc);
1814 }
1815 | any_identifier '[' constant_expression ']'
1816 {
1817 void *ctx = state;
1818 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1819 $$->set_location(yylloc);
1820 }
1821 ;
1822
1823 initializer:
1824 assignment_expression
1825 | '{' initializer_list '}'
1826 {
1827 $$ = $2;
1828 }
1829 | '{' initializer_list ',' '}'
1830 {
1831 $$ = $2;
1832 }
1833 ;
1834
1835 initializer_list:
1836 initializer
1837 {
1838 void *ctx = state;
1839 $$ = new(ctx) ast_aggregate_initializer();
1840 $$->set_location(yylloc);
1841 $$->expressions.push_tail(& $1->link);
1842 }
1843 | initializer_list ',' initializer
1844 {
1845 $1->expressions.push_tail(& $3->link);
1846 }
1847 ;
1848
1849 declaration_statement:
1850 declaration
1851 ;
1852
1853 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1854 // supported.
1855 statement:
1856 compound_statement { $$ = (ast_node *) $1; }
1857 | simple_statement
1858 ;
1859
1860 simple_statement:
1861 declaration_statement
1862 | expression_statement
1863 | selection_statement
1864 | switch_statement
1865 | iteration_statement
1866 | jump_statement
1867 ;
1868
1869 compound_statement:
1870 '{' '}'
1871 {
1872 void *ctx = state;
1873 $$ = new(ctx) ast_compound_statement(true, NULL);
1874 $$->set_location(yylloc);
1875 }
1876 | '{'
1877 {
1878 state->symbols->push_scope();
1879 }
1880 statement_list '}'
1881 {
1882 void *ctx = state;
1883 $$ = new(ctx) ast_compound_statement(true, $3);
1884 $$->set_location(yylloc);
1885 state->symbols->pop_scope();
1886 }
1887 ;
1888
1889 statement_no_new_scope:
1890 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1891 | simple_statement
1892 ;
1893
1894 compound_statement_no_new_scope:
1895 '{' '}'
1896 {
1897 void *ctx = state;
1898 $$ = new(ctx) ast_compound_statement(false, NULL);
1899 $$->set_location(yylloc);
1900 }
1901 | '{' statement_list '}'
1902 {
1903 void *ctx = state;
1904 $$ = new(ctx) ast_compound_statement(false, $2);
1905 $$->set_location(yylloc);
1906 }
1907 ;
1908
1909 statement_list:
1910 statement
1911 {
1912 if ($1 == NULL) {
1913 _mesa_glsl_error(& @1, state, "<nil> statement");
1914 assert($1 != NULL);
1915 }
1916
1917 $$ = $1;
1918 $$->link.self_link();
1919 }
1920 | statement_list statement
1921 {
1922 if ($2 == NULL) {
1923 _mesa_glsl_error(& @2, state, "<nil> statement");
1924 assert($2 != NULL);
1925 }
1926 $$ = $1;
1927 $$->link.insert_before(& $2->link);
1928 }
1929 ;
1930
1931 expression_statement:
1932 ';'
1933 {
1934 void *ctx = state;
1935 $$ = new(ctx) ast_expression_statement(NULL);
1936 $$->set_location(yylloc);
1937 }
1938 | expression ';'
1939 {
1940 void *ctx = state;
1941 $$ = new(ctx) ast_expression_statement($1);
1942 $$->set_location(yylloc);
1943 }
1944 ;
1945
1946 selection_statement:
1947 IF '(' expression ')' selection_rest_statement
1948 {
1949 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1950 $5.else_statement);
1951 $$->set_location(yylloc);
1952 }
1953 ;
1954
1955 selection_rest_statement:
1956 statement ELSE statement
1957 {
1958 $$.then_statement = $1;
1959 $$.else_statement = $3;
1960 }
1961 | statement %prec THEN
1962 {
1963 $$.then_statement = $1;
1964 $$.else_statement = NULL;
1965 }
1966 ;
1967
1968 condition:
1969 expression
1970 {
1971 $$ = (ast_node *) $1;
1972 }
1973 | fully_specified_type any_identifier '=' initializer
1974 {
1975 void *ctx = state;
1976 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1977 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1978 decl->set_location(yylloc);
1979 declarator->set_location(yylloc);
1980
1981 declarator->declarations.push_tail(&decl->link);
1982 $$ = declarator;
1983 }
1984 ;
1985
1986 /*
1987 * siwtch_statement grammar is based on the syntax described in the body
1988 * of the GLSL spec, not in it's appendix!!!
1989 */
1990 switch_statement:
1991 SWITCH '(' expression ')' switch_body
1992 {
1993 $$ = new(state) ast_switch_statement($3, $5);
1994 $$->set_location(yylloc);
1995 }
1996 ;
1997
1998 switch_body:
1999 '{' '}'
2000 {
2001 $$ = new(state) ast_switch_body(NULL);
2002 $$->set_location(yylloc);
2003 }
2004 | '{' case_statement_list '}'
2005 {
2006 $$ = new(state) ast_switch_body($2);
2007 $$->set_location(yylloc);
2008 }
2009 ;
2010
2011 case_label:
2012 CASE expression ':'
2013 {
2014 $$ = new(state) ast_case_label($2);
2015 $$->set_location(yylloc);
2016 }
2017 | DEFAULT ':'
2018 {
2019 $$ = new(state) ast_case_label(NULL);
2020 $$->set_location(yylloc);
2021 }
2022 ;
2023
2024 case_label_list:
2025 case_label
2026 {
2027 ast_case_label_list *labels = new(state) ast_case_label_list();
2028
2029 labels->labels.push_tail(& $1->link);
2030 $$ = labels;
2031 $$->set_location(yylloc);
2032 }
2033 | case_label_list case_label
2034 {
2035 $$ = $1;
2036 $$->labels.push_tail(& $2->link);
2037 }
2038 ;
2039
2040 case_statement:
2041 case_label_list statement
2042 {
2043 ast_case_statement *stmts = new(state) ast_case_statement($1);
2044 stmts->set_location(yylloc);
2045
2046 stmts->stmts.push_tail(& $2->link);
2047 $$ = stmts;
2048 }
2049 | case_statement statement
2050 {
2051 $$ = $1;
2052 $$->stmts.push_tail(& $2->link);
2053 }
2054 ;
2055
2056 case_statement_list:
2057 case_statement
2058 {
2059 ast_case_statement_list *cases= new(state) ast_case_statement_list();
2060 cases->set_location(yylloc);
2061
2062 cases->cases.push_tail(& $1->link);
2063 $$ = cases;
2064 }
2065 | case_statement_list case_statement
2066 {
2067 $$ = $1;
2068 $$->cases.push_tail(& $2->link);
2069 }
2070 ;
2071
2072 iteration_statement:
2073 WHILE '(' condition ')' statement_no_new_scope
2074 {
2075 void *ctx = state;
2076 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2077 NULL, $3, NULL, $5);
2078 $$->set_location(yylloc);
2079 }
2080 | DO statement WHILE '(' expression ')' ';'
2081 {
2082 void *ctx = state;
2083 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2084 NULL, $5, NULL, $2);
2085 $$->set_location(yylloc);
2086 }
2087 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2088 {
2089 void *ctx = state;
2090 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2091 $3, $4.cond, $4.rest, $6);
2092 $$->set_location(yylloc);
2093 }
2094 ;
2095
2096 for_init_statement:
2097 expression_statement
2098 | declaration_statement
2099 ;
2100
2101 conditionopt:
2102 condition
2103 | /* empty */
2104 {
2105 $$ = NULL;
2106 }
2107 ;
2108
2109 for_rest_statement:
2110 conditionopt ';'
2111 {
2112 $$.cond = $1;
2113 $$.rest = NULL;
2114 }
2115 | conditionopt ';' expression
2116 {
2117 $$.cond = $1;
2118 $$.rest = $3;
2119 }
2120 ;
2121
2122 // Grammar Note: No 'goto'. Gotos are not supported.
2123 jump_statement:
2124 CONTINUE ';'
2125 {
2126 void *ctx = state;
2127 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2128 $$->set_location(yylloc);
2129 }
2130 | BREAK ';'
2131 {
2132 void *ctx = state;
2133 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2134 $$->set_location(yylloc);
2135 }
2136 | RETURN ';'
2137 {
2138 void *ctx = state;
2139 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2140 $$->set_location(yylloc);
2141 }
2142 | RETURN expression ';'
2143 {
2144 void *ctx = state;
2145 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2146 $$->set_location(yylloc);
2147 }
2148 | DISCARD ';' // Fragment shader only.
2149 {
2150 void *ctx = state;
2151 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2152 $$->set_location(yylloc);
2153 }
2154 ;
2155
2156 external_declaration:
2157 function_definition { $$ = $1; }
2158 | declaration { $$ = $1; }
2159 | pragma_statement { $$ = NULL; }
2160 | layout_defaults { $$ = $1; }
2161 ;
2162
2163 function_definition:
2164 function_prototype compound_statement_no_new_scope
2165 {
2166 void *ctx = state;
2167 $$ = new(ctx) ast_function_definition();
2168 $$->set_location(yylloc);
2169 $$->prototype = $1;
2170 $$->body = $2;
2171
2172 state->symbols->pop_scope();
2173 }
2174 ;
2175
2176 /* layout_qualifieropt is packed into this rule */
2177 interface_block:
2178 basic_interface_block
2179 {
2180 $$ = $1;
2181 }
2182 | layout_qualifier basic_interface_block
2183 {
2184 ast_interface_block *block = $2;
2185 if (!block->layout.merge_qualifier(& @1, state, $1)) {
2186 YYERROR;
2187 }
2188 $$ = block;
2189 }
2190 ;
2191
2192 basic_interface_block:
2193 interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2194 {
2195 ast_interface_block *const block = $6;
2196
2197 block->block_name = $2;
2198 block->declarations.push_degenerate_list_at_head(& $4->link);
2199
2200 if ($1.flags.q.uniform) {
2201 if (!state->has_uniform_buffer_objects()) {
2202 _mesa_glsl_error(& @1, state,
2203 "#version 140 / GL_ARB_uniform_buffer_object "
2204 "required for defining uniform blocks");
2205 } else if (state->ARB_uniform_buffer_object_warn) {
2206 _mesa_glsl_warning(& @1, state,
2207 "#version 140 / GL_ARB_uniform_buffer_object "
2208 "required for defining uniform blocks");
2209 }
2210 } else {
2211 if (state->es_shader || state->language_version < 150) {
2212 _mesa_glsl_error(& @1, state,
2213 "#version 150 required for using "
2214 "interface blocks");
2215 }
2216 }
2217
2218 /* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):
2219 * "It is illegal to have an input block in a vertex shader
2220 * or an output block in a fragment shader"
2221 */
2222 if ((state->target == vertex_shader) && $1.flags.q.in) {
2223 _mesa_glsl_error(& @1, state,
2224 "`in' interface block is not allowed for "
2225 "a vertex shader");
2226 } else if ((state->target == fragment_shader) && $1.flags.q.out) {
2227 _mesa_glsl_error(& @1, state,
2228 "`out' interface block is not allowed for "
2229 "a fragment shader");
2230 }
2231
2232 /* Since block arrays require names, and both features are added in
2233 * the same language versions, we don't have to explicitly
2234 * version-check both things.
2235 */
2236 if (block->instance_name != NULL) {
2237 state->check_version(150, 300, & @1, "interface blocks with "
2238 "an instance name are not allowed");
2239 }
2240
2241 unsigned interface_type_mask;
2242 struct ast_type_qualifier temp_type_qualifier;
2243
2244 /* Get a bitmask containing only the in/out/uniform flags, allowing us
2245 * to ignore other irrelevant flags like interpolation qualifiers.
2246 */
2247 temp_type_qualifier.flags.i = 0;
2248 temp_type_qualifier.flags.q.uniform = true;
2249 temp_type_qualifier.flags.q.in = true;
2250 temp_type_qualifier.flags.q.out = true;
2251 interface_type_mask = temp_type_qualifier.flags.i;
2252
2253 /* Get the block's interface qualifier. The interface_qualifier
2254 * production rule guarantees that only one bit will be set (and
2255 * it will be in/out/uniform).
2256 */
2257 unsigned block_interface_qualifier = $1.flags.i;
2258
2259 block->layout.flags.i |= block_interface_qualifier;
2260
2261 foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
2262 ast_type_qualifier& qualifier = member->type->qualifier;
2263 if ((qualifier.flags.i & interface_type_mask) == 0) {
2264 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2265 * "If no optional qualifier is used in a member declaration, the
2266 * qualifier of the variable is just in, out, or uniform as declared
2267 * by interface-qualifier."
2268 */
2269 qualifier.flags.i |= block_interface_qualifier;
2270 } else if ((qualifier.flags.i & interface_type_mask) !=
2271 block_interface_qualifier) {
2272 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2273 * "If optional qualifiers are used, they can include interpolation
2274 * and storage qualifiers and they must declare an input, output,
2275 * or uniform variable consistent with the interface qualifier of
2276 * the block."
2277 */
2278 _mesa_glsl_error(& @1, state,
2279 "uniform/in/out qualifier on "
2280 "interface block member does not match "
2281 "the interface block");
2282 }
2283 }
2284
2285 $$ = block;
2286 }
2287 ;
2288
2289 interface_qualifier:
2290 IN_TOK
2291 {
2292 memset(& $$, 0, sizeof($$));
2293 $$.flags.q.in = 1;
2294 }
2295 | OUT_TOK
2296 {
2297 memset(& $$, 0, sizeof($$));
2298 $$.flags.q.out = 1;
2299 }
2300 | UNIFORM
2301 {
2302 memset(& $$, 0, sizeof($$));
2303 $$.flags.q.uniform = 1;
2304 }
2305 ;
2306
2307 instance_name_opt:
2308 /* empty */
2309 {
2310 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2311 NULL, false, NULL);
2312 }
2313 | NEW_IDENTIFIER
2314 {
2315 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2316 $1, false, NULL);
2317 }
2318 | NEW_IDENTIFIER '[' constant_expression ']'
2319 {
2320 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2321 $1, true, $3);
2322 }
2323 | NEW_IDENTIFIER '[' ']'
2324 {
2325 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2326 $1, true, NULL);
2327 }
2328 ;
2329
2330 member_list:
2331 member_declaration
2332 {
2333 $$ = $1;
2334 $1->link.self_link();
2335 }
2336 | member_declaration member_list
2337 {
2338 $$ = $1;
2339 $2->link.insert_before(& $$->link);
2340 }
2341 ;
2342
2343 member_declaration:
2344 fully_specified_type struct_declarator_list ';'
2345 {
2346 void *ctx = state;
2347 ast_fully_specified_type *type = $1;
2348 type->set_location(yylloc);
2349
2350 if (type->qualifier.flags.q.attribute) {
2351 _mesa_glsl_error(& @1, state,
2352 "keyword 'attribute' cannot be used with "
2353 "interface block member");
2354 } else if (type->qualifier.flags.q.varying) {
2355 _mesa_glsl_error(& @1, state,
2356 "keyword 'varying' cannot be used with "
2357 "interface block member");
2358 }
2359
2360 $$ = new(ctx) ast_declarator_list(type);
2361 $$->set_location(yylloc);
2362
2363 $$->declarations.push_degenerate_list_at_head(& $2->link);
2364 }
2365 ;
2366
2367 layout_defaults:
2368 layout_qualifier UNIFORM ';'
2369 {
2370 if (!state->default_uniform_qualifier->merge_qualifier(& @1, state, $1)) {
2371 YYERROR;
2372 }
2373 $$ = NULL;
2374 }
2375
2376 | layout_qualifier IN_TOK ';'
2377 {
2378 void *ctx = state;
2379 $$ = NULL;
2380 if (state->target != geometry_shader) {
2381 _mesa_glsl_error(& @1, state,
2382 "input layout qualifiers only valid in "
2383 "geometry shaders");
2384 } else if (!$1.flags.q.prim_type) {
2385 _mesa_glsl_error(& @1, state,
2386 "input layout qualifiers must specify a primitive"
2387 " type");
2388 } else {
2389 /* Make sure this is a valid input primitive type. */
2390 switch ($1.prim_type) {
2391 case GL_POINTS:
2392 case GL_LINES:
2393 case GL_LINES_ADJACENCY:
2394 case GL_TRIANGLES:
2395 case GL_TRIANGLES_ADJACENCY:
2396 $$ = new(ctx) ast_gs_input_layout(@1, $1.prim_type);
2397 break;
2398 default:
2399 _mesa_glsl_error(&@1, state,
2400 "invalid geometry shader input primitive type");
2401 break;
2402 }
2403 }
2404 }
2405
2406 | layout_qualifier OUT_TOK ';'
2407 {
2408 if (state->target != geometry_shader) {
2409 _mesa_glsl_error(& @1, state,
2410 "out layout qualifiers only valid in "
2411 "geometry shaders");
2412 } else {
2413 if ($1.flags.q.prim_type) {
2414 /* Make sure this is a valid output primitive type. */
2415 switch ($1.prim_type) {
2416 case GL_POINTS:
2417 case GL_LINE_STRIP:
2418 case GL_TRIANGLE_STRIP:
2419 break;
2420 default:
2421 _mesa_glsl_error(&@1, state, "invalid geometry shader output "
2422 "primitive type");
2423 break;
2424 }
2425 }
2426 if (!state->out_qualifier->merge_qualifier(& @1, state, $1))
2427 YYERROR;
2428 }
2429 $$ = NULL;
2430 }