Descend down the tree in more locations in constant folding.
[mesa.git] / glsl_parser.ypp
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
33 #define YYLEX_PARAM state->scanner
34
35 %}
36
37 %pure-parser
38 %locations
39 %error-verbose
40
41 %lex-param {void *scanner}
42 %parse-param {struct _mesa_glsl_parse_state *state}
43 %name-prefix "_mesa_glsl_"
44
45 %union {
46 int n;
47 float real;
48 char *identifier;
49
50 union {
51 struct ast_type_qualifier q;
52 unsigned i;
53 } type_qualifier;
54
55 struct ast_node *node;
56 struct ast_type_specifier *type_specifier;
57 struct ast_fully_specified_type *fully_specified_type;
58 struct ast_function *function;
59 struct ast_parameter_declarator *parameter_declarator;
60 struct ast_function_definition *function_definition;
61 struct ast_compound_statement *compound_statement;
62 struct ast_expression *expression;
63 struct ast_declarator_list *declarator_list;
64 struct ast_struct_specifier *struct_specifier;
65 struct ast_declaration *declaration;
66
67 struct {
68 struct ast_node *cond;
69 struct ast_expression *rest;
70 } for_rest_statement;
71 }
72
73 %token ATTRIBUTE CONST BOOL FLOAT INT UINT
74 %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
75 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
76 %token MAT2 MAT3 MAT4 CENTROID IN OUT INOUT UNIFORM VARYING
77 %token NOPERSPECTIVE FLAT SMOOTH
78 %token MAT2X2 MAT2X3 MAT2X4
79 %token MAT3X2 MAT3X3 MAT3X4
80 %token MAT4X2 MAT4X3 MAT4X4
81 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
82 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
83 %token SAMPLER2DARRAYSHADOW ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
84 %token ISAMPLER1DARRAY ISAMPLER2DARRAY USAMPLER1D USAMPLER2D USAMPLER3D
85 %token USAMPLERCUBE USAMPLER1DARRAY USAMPLER2DARRAY
86 %token STRUCT VOID WHILE
87 %token <identifier> IDENTIFIER TYPE_NAME
88 %token <real> FLOATCONSTANT
89 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
90 %token <identifier> FIELD_SELECTION
91 %token LEFT_OP RIGHT_OP
92 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
93 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
94 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
95 %token SUB_ASSIGN
96 %token INVARIANT
97 %token HIGH_PRECISION MEDIUM_PRECISION LOW_PRECISION PRECISION
98
99 %token VERSION EXTENSION LINE PRAGMA COLON EOL INTERFACE OUTPUT
100
101 /* Reserved words that are not actually used in the grammar.
102 */
103 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED GOTO
104 %token INLINE NOINLINE VOLATILE PUBLIC STATIC EXTERN EXTERNAL
105 %token LONG SHORT DOUBLE HALF FIXED UNSIGNED INPUT OUPTUT
106 %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
107 %token SAMPLER2DRECT SAMPLER3DRECT SAMPLER2DRECTSHADOW
108 %token SIZEOF CAST NAMESPACE USING LOWP MEDIUMP HIGHP
109
110 %type <identifier> variable_identifier
111 %type <node> statement
112 %type <node> statement_list
113 %type <node> simple_statement
114 %type <node> statement_matched
115 %type <node> statement_unmatched
116 %type <n> precision_qualifier
117 %type <type_qualifier> type_qualifier
118 %type <type_qualifier> storage_qualifier
119 %type <type_qualifier> interpolation_qualifier
120 %type <type_specifier> type_specifier
121 %type <type_specifier> type_specifier_no_prec
122 %type <type_specifier> type_specifier_nonarray
123 %type <n> basic_type_specifier_nonarray
124 %type <fully_specified_type> fully_specified_type
125 %type <function> function_prototype
126 %type <function> function_header
127 %type <function> function_header_with_parameters
128 %type <function> function_declarator
129 %type <parameter_declarator> parameter_declarator
130 %type <parameter_declarator> parameter_declaration
131 %type <type_qualifier> parameter_qualifier
132 %type <type_qualifier> parameter_type_qualifier
133 %type <type_specifier> parameter_type_specifier
134 %type <function_definition> function_definition
135 %type <compound_statement> compound_statement_no_new_scope
136 %type <compound_statement> compound_statement
137 %type <node> statement_no_new_scope
138 %type <node> expression_statement
139 %type <expression> expression
140 %type <expression> primary_expression
141 %type <expression> assignment_expression
142 %type <expression> conditional_expression
143 %type <expression> logical_or_expression
144 %type <expression> logical_xor_expression
145 %type <expression> logical_and_expression
146 %type <expression> inclusive_or_expression
147 %type <expression> exclusive_or_expression
148 %type <expression> and_expression
149 %type <expression> equality_expression
150 %type <expression> relational_expression
151 %type <expression> shift_expression
152 %type <expression> additive_expression
153 %type <expression> multiplicative_expression
154 %type <expression> unary_expression
155 %type <expression> constant_expression
156 %type <expression> integer_expression
157 %type <expression> postfix_expression
158 %type <expression> function_call_header_with_parameters
159 %type <expression> function_call_header_no_parameters
160 %type <expression> function_call_header
161 %type <expression> function_call_generic
162 %type <expression> function_call_or_method
163 %type <expression> function_call
164 %type <n> assignment_operator
165 %type <n> unary_operator
166 %type <expression> function_identifier
167 %type <node> external_declaration
168 %type <declarator_list> init_declarator_list
169 %type <declarator_list> single_declaration
170 %type <expression> initializer
171 %type <node> declaration
172 %type <node> declaration_statement
173 %type <node> jump_statement
174 %type <struct_specifier> struct_specifier
175 %type <node> struct_declaration_list
176 %type <declarator_list> struct_declaration
177 %type <declaration> struct_declarator
178 %type <declaration> struct_declarator_list
179 %type <node> selection_statement_matched
180 %type <node> selection_statement_unmatched
181 %type <node> iteration_statement
182 %type <node> condition
183 %type <node> conditionopt
184 %type <node> for_init_statement
185 %type <for_rest_statement> for_rest_statement
186 %%
187
188 translation_unit:
189 version_statement
190 {
191 _mesa_glsl_initialize_types(state);
192 }
193 external_declaration_list
194 |
195 {
196 state->language_version = 110;
197 _mesa_glsl_initialize_types(state);
198 }
199 external_declaration_list
200 ;
201
202 version_statement:
203 VERSION INTCONSTANT EOL
204 {
205 switch ($2) {
206 case 110:
207 case 120:
208 case 130:
209 /* FINISHME: Check against implementation support versions. */
210 state->language_version = $2;
211 break;
212 default:
213 _mesa_glsl_error(& @2, state, "Shading language version"
214 "%u is not supported\n", $2);
215 break;
216 }
217 }
218 ;
219
220 external_declaration_list:
221 external_declaration
222 {
223 insert_at_tail(& state->translation_unit,
224 (struct simple_node *) $1);
225 }
226 | external_declaration_list external_declaration
227 {
228 insert_at_tail(& state->translation_unit,
229 (struct simple_node *) $2);
230 }
231 ;
232
233 variable_identifier:
234 IDENTIFIER
235 ;
236
237 primary_expression:
238 variable_identifier
239 {
240 $$ = new ast_expression(ast_identifier, NULL, NULL, NULL);
241 $$->set_location(yylloc);
242 $$->primary_expression.identifier = $1;
243 }
244 | INTCONSTANT
245 {
246 $$ = new ast_expression(ast_int_constant, NULL, NULL, NULL);
247 $$->set_location(yylloc);
248 $$->primary_expression.int_constant = $1;
249 }
250 | UINTCONSTANT
251 {
252 $$ = new ast_expression(ast_uint_constant, NULL, NULL, NULL);
253 $$->set_location(yylloc);
254 $$->primary_expression.uint_constant = $1;
255 }
256 | FLOATCONSTANT
257 {
258 $$ = new ast_expression(ast_float_constant, NULL, NULL, NULL);
259 $$->set_location(yylloc);
260 $$->primary_expression.float_constant = $1;
261 }
262 | BOOLCONSTANT
263 {
264 $$ = new ast_expression(ast_bool_constant, NULL, NULL, NULL);
265 $$->set_location(yylloc);
266 $$->primary_expression.bool_constant = $1;
267 }
268 | '(' expression ')'
269 {
270 $$ = $2;
271 }
272 ;
273
274 postfix_expression:
275 primary_expression
276 | postfix_expression '[' integer_expression ']'
277 {
278 $$ = new ast_expression(ast_array_index, $1, $3, NULL);
279 $$->set_location(yylloc);
280 }
281 | function_call
282 {
283 $$ = $1;
284 }
285 | postfix_expression '.' IDENTIFIER
286 {
287 $$ = new ast_expression(ast_field_selection, $1, NULL, NULL);
288 $$->set_location(yylloc);
289 $$->primary_expression.identifier = $3;
290 }
291 | postfix_expression INC_OP
292 {
293 $$ = new ast_expression(ast_post_inc, $1, NULL, NULL);
294 $$->set_location(yylloc);
295 }
296 | postfix_expression DEC_OP
297 {
298 $$ = new ast_expression(ast_post_dec, $1, NULL, NULL);
299 $$->set_location(yylloc);
300 }
301 ;
302
303 integer_expression:
304 expression
305 ;
306
307 function_call:
308 function_call_or_method
309 ;
310
311 function_call_or_method:
312 function_call_generic
313 | postfix_expression '.' function_call_generic
314 {
315 $$ = new ast_expression(ast_field_selection, $1, $3, NULL);
316 $$->set_location(yylloc);
317 }
318 ;
319
320 function_call_generic:
321 function_call_header_with_parameters ')'
322 | function_call_header_no_parameters ')'
323 ;
324
325 function_call_header_no_parameters:
326 function_call_header VOID
327 | function_call_header
328 ;
329
330 function_call_header_with_parameters:
331 function_call_header assignment_expression
332 {
333 $$ = $1;
334 $$->set_location(yylloc);
335 $$->subexpressions[1] = $2;
336 }
337 | function_call_header_with_parameters ',' assignment_expression
338 {
339 $$ = $1;
340 $$->set_location(yylloc);
341 insert_at_tail((struct simple_node *) $$->subexpressions[1],
342 (struct simple_node *) $3);
343 }
344 ;
345
346 // Grammar Note: Constructors look like functions, but lexical
347 // analysis recognized most of them as keywords. They are now
348 // recognized through "type_specifier".
349 function_call_header:
350 function_identifier '('
351 ;
352
353 function_identifier:
354 type_specifier
355 {
356 $$ = new ast_function_expression($1);
357 $$->set_location(yylloc);
358 }
359 | IDENTIFIER
360 {
361 ast_expression *callee = new ast_expression($1);
362 $$ = new ast_function_expression(callee);
363 $$->set_location(yylloc);
364 }
365 | FIELD_SELECTION
366 {
367 ast_expression *callee = new ast_expression($1);
368 $$ = new ast_function_expression(callee);
369 $$->set_location(yylloc);
370 }
371 ;
372
373 // Grammar Note: No traditional style type casts.
374 unary_expression:
375 postfix_expression
376 | INC_OP unary_expression
377 {
378 $$ = new ast_expression(ast_pre_inc, $2, NULL, NULL);
379 $$->set_location(yylloc);
380 }
381 | DEC_OP unary_expression
382 {
383 $$ = new ast_expression(ast_pre_dec, $2, NULL, NULL);
384 $$->set_location(yylloc);
385 }
386 | unary_operator unary_expression
387 {
388 $$ = new ast_expression($1, $2, NULL, NULL);
389 $$->set_location(yylloc);
390 }
391 ;
392
393 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
394 unary_operator:
395 '+' { $$ = ast_plus; }
396 | '-' { $$ = ast_neg; }
397 | '!' { $$ = ast_logic_not; }
398 | '~' { $$ = ast_bit_not; }
399 ;
400
401 multiplicative_expression:
402 unary_expression
403 | multiplicative_expression '*' unary_expression
404 {
405 $$ = new ast_expression_bin(ast_mul, $1, $3);
406 $$->set_location(yylloc);
407 }
408 | multiplicative_expression '/' unary_expression
409 {
410 $$ = new ast_expression_bin(ast_div, $1, $3);
411 $$->set_location(yylloc);
412 }
413 | multiplicative_expression '%' unary_expression
414 {
415 $$ = new ast_expression_bin(ast_mod, $1, $3);
416 $$->set_location(yylloc);
417 }
418 ;
419
420 additive_expression:
421 multiplicative_expression
422 | additive_expression '+' multiplicative_expression
423 {
424 $$ = new ast_expression_bin(ast_add, $1, $3);
425 $$->set_location(yylloc);
426 }
427 | additive_expression '-' multiplicative_expression
428 {
429 $$ = new ast_expression_bin(ast_sub, $1, $3);
430 $$->set_location(yylloc);
431 }
432 ;
433
434 shift_expression:
435 additive_expression
436 | shift_expression LEFT_OP additive_expression
437 {
438 $$ = new ast_expression_bin(ast_lshift, $1, $3);
439 $$->set_location(yylloc);
440 }
441 | shift_expression RIGHT_OP additive_expression
442 {
443 $$ = new ast_expression_bin(ast_rshift, $1, $3);
444 $$->set_location(yylloc);
445 }
446 ;
447
448 relational_expression:
449 shift_expression
450 | relational_expression '<' shift_expression
451 {
452 $$ = new ast_expression_bin(ast_less, $1, $3);
453 $$->set_location(yylloc);
454 }
455 | relational_expression '>' shift_expression
456 {
457 $$ = new ast_expression_bin(ast_greater, $1, $3);
458 $$->set_location(yylloc);
459 }
460 | relational_expression LE_OP shift_expression
461 {
462 $$ = new ast_expression_bin(ast_lequal, $1, $3);
463 $$->set_location(yylloc);
464 }
465 | relational_expression GE_OP shift_expression
466 {
467 $$ = new ast_expression_bin(ast_gequal, $1, $3);
468 $$->set_location(yylloc);
469 }
470 ;
471
472 equality_expression:
473 relational_expression
474 | equality_expression EQ_OP relational_expression
475 {
476 $$ = new ast_expression_bin(ast_equal, $1, $3);
477 $$->set_location(yylloc);
478 }
479 | equality_expression NE_OP relational_expression
480 {
481 $$ = new ast_expression_bin(ast_nequal, $1, $3);
482 $$->set_location(yylloc);
483 }
484 ;
485
486 and_expression:
487 equality_expression
488 | and_expression '&' equality_expression
489 {
490 $$ = new ast_expression_bin(ast_bit_or, $1, $3);
491 $$->set_location(yylloc);
492 }
493 ;
494
495 exclusive_or_expression:
496 and_expression
497 | exclusive_or_expression '^' and_expression
498 {
499 $$ = new ast_expression_bin(ast_bit_xor, $1, $3);
500 $$->set_location(yylloc);
501 }
502 ;
503
504 inclusive_or_expression:
505 exclusive_or_expression
506 | inclusive_or_expression '|' exclusive_or_expression
507 {
508 $$ = new ast_expression_bin(ast_bit_or, $1, $3);
509 $$->set_location(yylloc);
510 }
511 ;
512
513 logical_and_expression:
514 inclusive_or_expression
515 | logical_and_expression AND_OP inclusive_or_expression
516 {
517 $$ = new ast_expression_bin(ast_logic_and, $1, $3);
518 $$->set_location(yylloc);
519 }
520 ;
521
522 logical_xor_expression:
523 logical_and_expression
524 | logical_xor_expression XOR_OP logical_and_expression
525 {
526 $$ = new ast_expression_bin(ast_logic_xor, $1, $3);
527 $$->set_location(yylloc);
528 }
529 ;
530
531 logical_or_expression:
532 logical_xor_expression
533 | logical_or_expression OR_OP logical_xor_expression
534 {
535 $$ = new ast_expression_bin(ast_logic_or, $1, $3);
536 $$->set_location(yylloc);
537 }
538 ;
539
540 conditional_expression:
541 logical_or_expression
542 | logical_or_expression '?' expression ':' assignment_expression
543 {
544 $$ = new ast_expression(ast_conditional, $1, $3, $5);
545 $$->set_location(yylloc);
546 }
547 ;
548
549 assignment_expression:
550 conditional_expression
551 | unary_expression assignment_operator assignment_expression
552 {
553 $$ = new ast_expression($2, $1, $3, NULL);
554 $$->set_location(yylloc);
555 }
556 ;
557
558 assignment_operator:
559 '=' { $$ = ast_assign; }
560 | MUL_ASSIGN { $$ = ast_mul_assign; }
561 | DIV_ASSIGN { $$ = ast_div_assign; }
562 | MOD_ASSIGN { $$ = ast_mod_assign; }
563 | ADD_ASSIGN { $$ = ast_add_assign; }
564 | SUB_ASSIGN { $$ = ast_sub_assign; }
565 | LEFT_ASSIGN { $$ = ast_ls_assign; }
566 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
567 | AND_ASSIGN { $$ = ast_and_assign; }
568 | XOR_ASSIGN { $$ = ast_xor_assign; }
569 | OR_ASSIGN { $$ = ast_or_assign; }
570 ;
571
572 expression:
573 assignment_expression
574 {
575 $$ = $1;
576 }
577 | expression ',' assignment_expression
578 {
579 if ($1->oper != ast_sequence) {
580 $$ = new ast_expression(ast_sequence, NULL, NULL, NULL);
581 $$->set_location(yylloc);
582 insert_at_tail(& $$->expressions, $1);
583 } else {
584 $$ = $1;
585 }
586
587 insert_at_tail(& $$->expressions, $3);
588 }
589 ;
590
591 constant_expression:
592 conditional_expression
593 ;
594
595 declaration:
596 function_prototype ';'
597 {
598 $$ = $1;
599 }
600 | init_declarator_list ';'
601 {
602 $$ = $1;
603 }
604 | PRECISION precision_qualifier type_specifier_no_prec ';'
605 {
606 $$ = NULL; /* FINISHME */
607 }
608 ;
609
610 function_prototype:
611 function_declarator ')'
612 ;
613
614 function_declarator:
615 function_header
616 | function_header_with_parameters
617 ;
618
619 function_header_with_parameters:
620 function_header parameter_declaration
621 {
622 $$ = $1;
623 insert_at_tail(& $$->parameters,
624 (struct simple_node *) $2);
625 }
626 | function_header_with_parameters ',' parameter_declaration
627 {
628 $$ = $1;
629 insert_at_tail(& $$->parameters,
630 (struct simple_node *) $3);
631 }
632 ;
633
634 function_header:
635 fully_specified_type IDENTIFIER '('
636 {
637 $$ = new ast_function();
638 $$->set_location(yylloc);
639 $$->return_type = $1;
640 $$->identifier = $2;
641 }
642 ;
643
644 parameter_declarator:
645 type_specifier IDENTIFIER
646 {
647 $$ = new ast_parameter_declarator();
648 $$->set_location(yylloc);
649 $$->type = new ast_fully_specified_type();
650 $$->type->set_location(yylloc);
651 $$->type->specifier = $1;
652 $$->identifier = $2;
653 }
654 | type_specifier IDENTIFIER '[' constant_expression ']'
655 {
656 $$ = new ast_parameter_declarator();
657 $$->set_location(yylloc);
658 $$->type = new ast_fully_specified_type();
659 $$->type->set_location(yylloc);
660 $$->type->specifier = $1;
661 $$->identifier = $2;
662 $$->is_array = true;
663 $$->array_size = $4;
664 }
665 ;
666
667 parameter_declaration:
668 parameter_type_qualifier parameter_qualifier parameter_declarator
669 {
670 $1.i |= $2.i;
671
672 $$ = $3;
673 $$->type->qualifier = $1.q;
674 }
675 | parameter_qualifier parameter_declarator
676 {
677 $$ = $2;
678 $$->type->qualifier = $1.q;
679 }
680 | parameter_type_qualifier parameter_qualifier parameter_type_specifier
681 {
682 $1.i |= $2.i;
683
684 $$ = new ast_parameter_declarator();
685 $$->set_location(yylloc);
686 $$->type = new ast_fully_specified_type();
687 $$->type->qualifier = $1.q;
688 $$->type->specifier = $3;
689 }
690 | parameter_qualifier parameter_type_specifier
691 {
692 $$ = new ast_parameter_declarator();
693 $$->set_location(yylloc);
694 $$->type = new ast_fully_specified_type();
695 $$->type->qualifier = $1.q;
696 $$->type->specifier = $2;
697 }
698 ;
699
700 parameter_qualifier:
701 /* empty */ { $$.i = 0; }
702 | IN { $$.i = 0; $$.q.in = 1; }
703 | OUT { $$.i = 0; $$.q.out = 1; }
704 | INOUT { $$.i = 0; $$.q.in = 1; $$.q.out = 1; }
705 ;
706
707 parameter_type_specifier:
708 type_specifier
709 ;
710
711 init_declarator_list:
712 single_declaration
713 | init_declarator_list ',' IDENTIFIER
714 {
715 ast_declaration *decl = new ast_declaration($3, false, NULL, NULL);
716 decl->set_location(yylloc);
717
718 $$ = $1;
719 insert_at_tail(& $$->declarations,
720 (struct simple_node *) decl);
721 }
722 | init_declarator_list ',' IDENTIFIER '[' ']'
723 {
724 ast_declaration *decl = new ast_declaration($3, true, NULL, NULL);
725 decl->set_location(yylloc);
726
727 $$ = $1;
728 insert_at_tail(& $$->declarations,
729 (struct simple_node *) decl);
730 }
731 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']'
732 {
733 ast_declaration *decl = new ast_declaration($3, true, $5, NULL);
734 decl->set_location(yylloc);
735
736 $$ = $1;
737 insert_at_tail(& $$->declarations,
738 (struct simple_node *) decl);
739 }
740 | init_declarator_list ',' IDENTIFIER '[' ']' '=' initializer
741 {
742 ast_declaration *decl = new ast_declaration($3, true, NULL, $7);
743 decl->set_location(yylloc);
744
745 $$ = $1;
746 insert_at_tail(& $$->declarations,
747 (struct simple_node *) decl);
748 }
749 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']' '=' initializer
750 {
751 ast_declaration *decl = new ast_declaration($3, true, $5, $8);
752 decl->set_location(yylloc);
753
754 $$ = $1;
755 insert_at_tail(& $$->declarations,
756 (struct simple_node *) decl);
757 }
758 | init_declarator_list ',' IDENTIFIER '=' initializer
759 {
760 ast_declaration *decl = new ast_declaration($3, false, NULL, $5);
761 decl->set_location(yylloc);
762
763 $$ = $1;
764 insert_at_tail(& $$->declarations,
765 (struct simple_node *) decl);
766 }
767 ;
768
769 // Grammar Note: No 'enum', or 'typedef'.
770 single_declaration:
771 fully_specified_type
772 {
773 $$ = new ast_declarator_list($1);
774 $$->set_location(yylloc);
775 }
776 | fully_specified_type IDENTIFIER
777 {
778 ast_declaration *decl = new ast_declaration($2, false, NULL, NULL);
779
780 $$ = new ast_declarator_list($1);
781 $$->set_location(yylloc);
782 insert_at_tail(& $$->declarations,
783 (struct simple_node *) decl);
784 }
785 | fully_specified_type IDENTIFIER '[' ']'
786 {
787 ast_declaration *decl = new ast_declaration($2, true, NULL, NULL);
788
789 $$ = new ast_declarator_list($1);
790 $$->set_location(yylloc);
791 insert_at_tail(& $$->declarations,
792 (struct simple_node *) decl);
793 }
794 | fully_specified_type IDENTIFIER '[' constant_expression ']'
795 {
796 ast_declaration *decl = new ast_declaration($2, true, $4, NULL);
797
798 $$ = new ast_declarator_list($1);
799 $$->set_location(yylloc);
800 insert_at_tail(& $$->declarations,
801 (struct simple_node *) decl);
802 }
803 | fully_specified_type IDENTIFIER '[' ']' '=' initializer
804 {
805 ast_declaration *decl = new ast_declaration($2, true, NULL, $6);
806
807 $$ = new ast_declarator_list($1);
808 $$->set_location(yylloc);
809 insert_at_tail(& $$->declarations,
810 (struct simple_node *) decl);
811 }
812 | fully_specified_type IDENTIFIER '[' constant_expression ']' '=' initializer
813 {
814 ast_declaration *decl = new ast_declaration($2, true, $4, $7);
815
816 $$ = new ast_declarator_list($1);
817 $$->set_location(yylloc);
818 insert_at_tail(& $$->declarations,
819 (struct simple_node *) decl);
820 }
821 | fully_specified_type IDENTIFIER '=' initializer
822 {
823 ast_declaration *decl = new ast_declaration($2, false, NULL, $4);
824
825 $$ = new ast_declarator_list($1);
826 $$->set_location(yylloc);
827 insert_at_tail(& $$->declarations,
828 (struct simple_node *) decl);
829 }
830 | INVARIANT IDENTIFIER // Vertex only.
831 {
832 ast_declaration *decl = new ast_declaration($2, false, NULL, NULL);
833
834 $$ = new ast_declarator_list(NULL);
835 $$->set_location(yylloc);
836 $$->invariant = true;
837
838 insert_at_tail(& $$->declarations,
839 (struct simple_node *) decl);
840 }
841 ;
842
843 fully_specified_type:
844 type_specifier
845 {
846 $$ = new ast_fully_specified_type();
847 $$->set_location(yylloc);
848 $$->specifier = $1;
849 }
850 | type_qualifier type_specifier
851 {
852 $$ = new ast_fully_specified_type();
853 $$->set_location(yylloc);
854 $$->qualifier = $1.q;
855 $$->specifier = $2;
856 }
857 ;
858
859 interpolation_qualifier:
860 SMOOTH { $$.i = 0; $$.q.smooth = 1; }
861 | FLAT { $$.i = 0; $$.q.flat = 1; }
862 | NOPERSPECTIVE { $$.i = 0; $$.q.noperspective = 1; }
863 ;
864
865 parameter_type_qualifier:
866 CONST { $$.i = 0; $$.q.constant = 1; }
867 ;
868
869 type_qualifier:
870 storage_qualifier
871 | interpolation_qualifier type_qualifier
872 {
873 $$.i = $1.i | $2.i;
874 }
875 | INVARIANT type_qualifier
876 {
877 $$ = $2;
878 $$.q.invariant = 1;
879 }
880 ;
881
882 storage_qualifier:
883 CONST { $$.i = 0; $$.q.constant = 1; }
884 | ATTRIBUTE { $$.i = 0; $$.q.attribute = 1; }
885 | VARYING { $$.i = 0; $$.q.varying = 1; }
886 | CENTROID VARYING { $$.i = 0; $$.q.centroid = 1; $$.q.varying = 1; }
887 | IN { $$.i = 0; $$.q.in = 1; }
888 | OUT { $$.i = 0; $$.q.out = 1; }
889 | CENTROID IN { $$.i = 0; $$.q.centroid = 1; $$.q.in = 1; }
890 | CENTROID OUT { $$.i = 0; $$.q.centroid = 1; $$.q.out = 1; }
891 | UNIFORM { $$.i = 0; $$.q.uniform = 1; }
892 ;
893
894 type_specifier:
895 type_specifier_no_prec
896 | precision_qualifier type_specifier_no_prec
897 {
898 $$ = $2;
899 $$->precision = $1;
900 }
901 ;
902
903 type_specifier_no_prec:
904 type_specifier_nonarray
905 | type_specifier_nonarray '[' ']'
906 {
907 $$ = $1;
908 $$->is_array = true;
909 $$->array_size = NULL;
910 }
911 | type_specifier_nonarray '[' constant_expression ']'
912 {
913 $$ = $1;
914 $$->is_array = true;
915 $$->array_size = $3;
916 }
917 ;
918
919 type_specifier_nonarray:
920 basic_type_specifier_nonarray
921 {
922 $$ = new ast_type_specifier($1);
923 $$->set_location(yylloc);
924 }
925 | struct_specifier
926 {
927 $$ = new ast_type_specifier($1);
928 $$->set_location(yylloc);
929 }
930 | TYPE_NAME
931 {
932 $$ = new ast_type_specifier($1);
933 $$->set_location(yylloc);
934 }
935 ;
936
937 basic_type_specifier_nonarray:
938 VOID { $$ = ast_void; }
939 | FLOAT { $$ = ast_float; }
940 | INT { $$ = ast_int; }
941 | UINT { $$ = ast_uint; }
942 | BOOL { $$ = ast_bool; }
943 | VEC2 { $$ = ast_vec2; }
944 | VEC3 { $$ = ast_vec3; }
945 | VEC4 { $$ = ast_vec4; }
946 | BVEC2 { $$ = ast_bvec2; }
947 | BVEC3 { $$ = ast_bvec3; }
948 | BVEC4 { $$ = ast_bvec4; }
949 | IVEC2 { $$ = ast_ivec2; }
950 | IVEC3 { $$ = ast_ivec3; }
951 | IVEC4 { $$ = ast_ivec4; }
952 | UVEC2 { $$ = ast_uvec2; }
953 | UVEC3 { $$ = ast_uvec3; }
954 | UVEC4 { $$ = ast_uvec4; }
955 | MAT2 { $$ = ast_mat2; }
956 | MAT3 { $$ = ast_mat3; }
957 | MAT4 { $$ = ast_mat4; }
958 | MAT2X2 { $$ = ast_mat2; }
959 | MAT2X3 { $$ = ast_mat2x3; }
960 | MAT2X4 { $$ = ast_mat2x4; }
961 | MAT3X2 { $$ = ast_mat3x2; }
962 | MAT3X3 { $$ = ast_mat3; }
963 | MAT3X4 { $$ = ast_mat3x4; }
964 | MAT4X2 { $$ = ast_mat4x2; }
965 | MAT4X3 { $$ = ast_mat4x3; }
966 | MAT4X4 { $$ = ast_mat4; }
967 | SAMPLER1D { $$ = ast_sampler1d; }
968 | SAMPLER2D { $$ = ast_sampler2d; }
969 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
970 | SAMPLER3D { $$ = ast_sampler3d; }
971 | SAMPLERCUBE { $$ = ast_samplercube; }
972 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
973 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
974 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
975 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
976 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
977 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
978 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
979 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
980 | ISAMPLER1D { $$ = ast_isampler1d; }
981 | ISAMPLER2D { $$ = ast_isampler2d; }
982 | ISAMPLER3D { $$ = ast_isampler3d; }
983 | ISAMPLERCUBE { $$ = ast_isamplercube; }
984 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
985 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
986 | USAMPLER1D { $$ = ast_usampler1d; }
987 | USAMPLER2D { $$ = ast_usampler2d; }
988 | USAMPLER3D { $$ = ast_usampler3d; }
989 | USAMPLERCUBE { $$ = ast_usamplercube; }
990 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
991 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
992 ;
993
994 precision_qualifier:
995 HIGH_PRECISION { $$ = ast_precision_high; }
996 | MEDIUM_PRECISION { $$ = ast_precision_medium; }
997 | LOW_PRECISION { $$ = ast_precision_low; }
998 ;
999
1000 struct_specifier:
1001 STRUCT IDENTIFIER '{' struct_declaration_list '}'
1002 {
1003 $$ = new ast_struct_specifier($2, $4);
1004 $$->set_location(yylloc);
1005 }
1006 | STRUCT '{' struct_declaration_list '}'
1007 {
1008 $$ = new ast_struct_specifier(NULL, $3);
1009 $$->set_location(yylloc);
1010 }
1011 ;
1012
1013 struct_declaration_list:
1014 struct_declaration
1015 {
1016 $$ = (struct ast_node *) $1;
1017 }
1018 | struct_declaration_list struct_declaration
1019 {
1020 $$ = (struct ast_node *) $1;
1021 insert_at_tail((struct simple_node *) $$,
1022 (struct simple_node *) $2);
1023 }
1024 ;
1025
1026 struct_declaration:
1027 type_specifier struct_declarator_list ';'
1028 {
1029 ast_fully_specified_type *type = new ast_fully_specified_type();
1030 type->set_location(yylloc);
1031
1032 type->specifier = $1;
1033 $$ = new ast_declarator_list(type);
1034 $$->set_location(yylloc);
1035
1036 insert_at_tail((struct simple_node *) $2,
1037 & $$->declarations);
1038 }
1039 ;
1040
1041 struct_declarator_list:
1042 struct_declarator
1043 | struct_declarator_list ',' struct_declarator
1044 {
1045 $$ = $1;
1046 insert_at_tail((struct simple_node *) $$,
1047 (struct simple_node *) $3);
1048 }
1049 ;
1050
1051 struct_declarator:
1052 IDENTIFIER
1053 {
1054 $$ = new ast_declaration($1, false, NULL, NULL);
1055 $$->set_location(yylloc);
1056 }
1057 | IDENTIFIER '[' constant_expression ']'
1058 {
1059 $$ = new ast_declaration($1, true, $3, NULL);
1060 $$->set_location(yylloc);
1061 }
1062 ;
1063
1064 initializer:
1065 assignment_expression
1066 ;
1067
1068 declaration_statement:
1069 declaration
1070 ;
1071
1072 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1073 // supported.
1074 statement:
1075 statement_matched
1076 | statement_unmatched
1077 ;
1078
1079 statement_matched:
1080 compound_statement { $$ = (struct ast_node *) $1; }
1081 | simple_statement
1082 ;
1083
1084 statement_unmatched:
1085 selection_statement_unmatched
1086 ;
1087
1088 simple_statement:
1089 declaration_statement
1090 | expression_statement
1091 | selection_statement_matched
1092 | switch_statement { $$ = NULL; }
1093 | case_label { $$ = NULL; }
1094 | iteration_statement
1095 | jump_statement
1096 ;
1097
1098 compound_statement:
1099 '{' '}'
1100 {
1101 $$ = new ast_compound_statement(true, NULL);
1102 $$->set_location(yylloc);
1103 }
1104 | '{' statement_list '}'
1105 {
1106 $$ = new ast_compound_statement(true, $2);
1107 $$->set_location(yylloc);
1108 }
1109 ;
1110
1111 statement_no_new_scope:
1112 compound_statement_no_new_scope { $$ = (struct ast_node *) $1; }
1113 | simple_statement
1114 ;
1115
1116 compound_statement_no_new_scope:
1117 '{' '}'
1118 {
1119 $$ = new ast_compound_statement(false, NULL);
1120 $$->set_location(yylloc);
1121 }
1122 | '{' statement_list '}'
1123 {
1124 $$ = new ast_compound_statement(false, $2);
1125 $$->set_location(yylloc);
1126 }
1127 ;
1128
1129 statement_list:
1130 statement
1131 {
1132 if ($1 == NULL) {
1133 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1134 assert($1 != NULL);
1135 }
1136
1137 $$ = $1;
1138 make_empty_list((struct simple_node *) $$);
1139 }
1140 | statement_list statement
1141 {
1142 if ($2 == NULL) {
1143 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1144 assert($2 != NULL);
1145 }
1146 $$ = $1;
1147 insert_at_tail((struct simple_node *) $$,
1148 (struct simple_node *) $2);
1149 }
1150 ;
1151
1152 expression_statement:
1153 ';'
1154 {
1155 $$ = new ast_expression_statement(NULL);
1156 $$->set_location(yylloc);
1157 }
1158 | expression ';'
1159 {
1160 $$ = new ast_expression_statement($1);
1161 $$->set_location(yylloc);
1162 }
1163 ;
1164
1165 selection_statement_matched:
1166 IF '(' expression ')' statement_matched ELSE statement_matched
1167 {
1168 $$ = new ast_selection_statement($3, $5, $7);
1169 $$->set_location(yylloc);
1170 }
1171 ;
1172
1173 selection_statement_unmatched:
1174 IF '(' expression ')' statement_matched
1175 {
1176 $$ = new ast_selection_statement($3, $5, NULL);
1177 $$->set_location(yylloc);
1178 }
1179 | IF '(' expression ')' statement_unmatched
1180 {
1181 $$ = new ast_selection_statement($3, $5, NULL);
1182 $$->set_location(yylloc);
1183 }
1184 | IF '(' expression ')' statement_matched ELSE statement_unmatched
1185 {
1186 $$ = new ast_selection_statement($3, $5, $7);
1187 $$->set_location(yylloc);
1188 }
1189 ;
1190
1191 condition:
1192 expression
1193 {
1194 $$ = (struct ast_node *) $1;
1195 }
1196 | fully_specified_type IDENTIFIER '=' initializer
1197 {
1198 ast_declaration *decl = new ast_declaration($2, false, NULL, $4);
1199 ast_declarator_list *declarator = new ast_declarator_list($1);
1200 decl->set_location(yylloc);
1201 declarator->set_location(yylloc);
1202
1203 insert_at_tail(& declarator->declarations,
1204 (struct simple_node *) decl);
1205
1206 $$ = declarator;
1207 }
1208 ;
1209
1210 switch_statement:
1211 SWITCH '(' expression ')' compound_statement
1212 ;
1213
1214 case_label:
1215 CASE expression ':'
1216 | DEFAULT ':'
1217 ;
1218
1219 iteration_statement:
1220 WHILE '(' condition ')' statement_no_new_scope
1221 {
1222 $$ = new ast_iteration_statement(ast_iteration_statement::ast_while,
1223 NULL, $3, NULL, $5);
1224 $$->set_location(yylloc);
1225 }
1226 | DO statement WHILE '(' expression ')' ';'
1227 {
1228 $$ = new ast_iteration_statement(ast_iteration_statement::ast_do_while,
1229 NULL, $5, NULL, $2);
1230 $$->set_location(yylloc);
1231 }
1232 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1233 {
1234 $$ = new ast_iteration_statement(ast_iteration_statement::ast_for,
1235 $3, $4.cond, $4.rest, $6);
1236 $$->set_location(yylloc);
1237 }
1238 ;
1239
1240 for_init_statement:
1241 expression_statement
1242 | declaration_statement
1243 ;
1244
1245 conditionopt:
1246 condition
1247 | /* empty */
1248 {
1249 $$ = NULL;
1250 }
1251 ;
1252
1253 for_rest_statement:
1254 conditionopt ';'
1255 {
1256 $$.cond = $1;
1257 $$.rest = NULL;
1258 }
1259 | conditionopt ';' expression
1260 {
1261 $$.cond = $1;
1262 $$.rest = $3;
1263 }
1264 ;
1265
1266 // Grammar Note: No 'goto'. Gotos are not supported.
1267 jump_statement:
1268 CONTINUE ';'
1269 {
1270 $$ = new ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1271 $$->set_location(yylloc);
1272 }
1273 | BREAK ';'
1274 {
1275 $$ = new ast_jump_statement(ast_jump_statement::ast_break, NULL);
1276 $$->set_location(yylloc);
1277 }
1278 | RETURN ';'
1279 {
1280 $$ = new ast_jump_statement(ast_jump_statement::ast_return, NULL);
1281 $$->set_location(yylloc);
1282 }
1283 | RETURN expression ';'
1284 {
1285 $$ = new ast_jump_statement(ast_jump_statement::ast_return, $2);
1286 $$->set_location(yylloc);
1287 }
1288 | DISCARD ';' // Fragment shader only.
1289 {
1290 $$ = new ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1291 $$->set_location(yylloc);
1292 }
1293 ;
1294
1295 external_declaration:
1296 function_definition { $$ = $1; }
1297 | declaration { $$ = $1; }
1298 ;
1299
1300 function_definition:
1301 function_prototype compound_statement_no_new_scope
1302 {
1303 $$ = new ast_function_definition();
1304 $$->set_location(yylloc);
1305 $$->prototype = $1;
1306 $$->body = $2;
1307 }
1308 ;