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