Add parameters to function declarations in the correct order
[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
342 function_identifier:
343 type_specifier
344 {
345 $$ = new ast_function_expression($1);
346 }
347 | IDENTIFIER
348 {
349 ast_expression *callee = new ast_expression($1);
350 $$ = new ast_function_expression(callee);
351 }
352 | FIELD_SELECTION
353 {
354 ast_expression *callee = new ast_expression($1);
355 $$ = new ast_function_expression(callee);
356 }
357 ;
358
359 // Grammar Note: No traditional style type casts.
360 unary_expression:
361 postfix_expression
362 | INC_OP unary_expression
363 {
364 $$ = new ast_expression(ast_pre_inc, $2, NULL, NULL);
365 }
366 | DEC_OP unary_expression
367 {
368 $$ = new ast_expression(ast_pre_dec, $2, NULL, NULL);
369 }
370 | unary_operator unary_expression
371 {
372 $$ = new ast_expression($1, $2, NULL, NULL);
373 }
374 ;
375
376 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
377 unary_operator:
378 '+' { $$ = ast_plus; }
379 | '-' { $$ = ast_neg; }
380 | '!' { $$ = ast_logic_not; }
381 | '~' { $$ = ast_bit_not; }
382 ;
383
384 multiplicative_expression:
385 unary_expression
386 | multiplicative_expression '*' unary_expression
387 {
388 $$ = new ast_expression_bin(ast_mul, $1, $3);
389 }
390 | multiplicative_expression '/' unary_expression
391 {
392 $$ = new ast_expression_bin(ast_div, $1, $3);
393 }
394 | multiplicative_expression '%' unary_expression
395 {
396 $$ = new ast_expression_bin(ast_mod, $1, $3);
397 }
398 ;
399
400 additive_expression:
401 multiplicative_expression
402 | additive_expression '+' multiplicative_expression
403 {
404 $$ = new ast_expression_bin(ast_add, $1, $3);
405 }
406 | additive_expression '-' multiplicative_expression
407 {
408 $$ = new ast_expression_bin(ast_sub, $1, $3);
409 }
410 ;
411
412 shift_expression:
413 additive_expression
414 | shift_expression LEFT_OP additive_expression
415 {
416 $$ = new ast_expression_bin(ast_lshift, $1, $3);
417 }
418 | shift_expression RIGHT_OP additive_expression
419 {
420 $$ = new ast_expression_bin(ast_rshift, $1, $3);
421 }
422 ;
423
424 relational_expression:
425 shift_expression
426 | relational_expression '<' shift_expression
427 {
428 $$ = new ast_expression_bin(ast_less, $1, $3);
429 }
430 | relational_expression '>' shift_expression
431 {
432 $$ = new ast_expression_bin(ast_greater, $1, $3);
433 }
434 | relational_expression LE_OP shift_expression
435 {
436 $$ = new ast_expression_bin(ast_lequal, $1, $3);
437 }
438 | relational_expression GE_OP shift_expression
439 {
440 $$ = new ast_expression_bin(ast_gequal, $1, $3);
441 }
442 ;
443
444 equality_expression:
445 relational_expression
446 | equality_expression EQ_OP relational_expression
447 {
448 $$ = new ast_expression_bin(ast_equal, $1, $3);
449 }
450 | equality_expression NE_OP relational_expression
451 {
452 $$ = new ast_expression_bin(ast_nequal, $1, $3);
453 }
454 ;
455
456 and_expression:
457 equality_expression
458 | and_expression '&' equality_expression
459 {
460 $$ = new ast_expression_bin(ast_bit_or, $1, $3);
461 }
462 ;
463
464 exclusive_or_expression:
465 and_expression
466 | exclusive_or_expression '^' and_expression
467 {
468 $$ = new ast_expression_bin(ast_bit_xor, $1, $3);
469 }
470 ;
471
472 inclusive_or_expression:
473 exclusive_or_expression
474 | inclusive_or_expression '|' exclusive_or_expression
475 {
476 $$ = new ast_expression_bin(ast_bit_or, $1, $3);
477 }
478 ;
479
480 logical_and_expression:
481 inclusive_or_expression
482 | logical_and_expression AND_OP inclusive_or_expression
483 {
484 $$ = new ast_expression_bin(ast_logic_and, $1, $3);
485 }
486 ;
487
488 logical_xor_expression:
489 logical_and_expression
490 | logical_xor_expression XOR_OP logical_and_expression
491 {
492 $$ = new ast_expression_bin(ast_logic_xor, $1, $3);
493 }
494 ;
495
496 logical_or_expression:
497 logical_xor_expression
498 | logical_or_expression OR_OP logical_xor_expression
499 {
500 $$ = new ast_expression_bin(ast_logic_or, $1, $3);
501 }
502 ;
503
504 conditional_expression:
505 logical_or_expression
506 | logical_or_expression '?' expression ':' assignment_expression
507 {
508 $$ = new ast_expression(ast_conditional, $1, $3, $5);
509 }
510 ;
511
512 assignment_expression:
513 conditional_expression
514 | unary_expression assignment_operator assignment_expression
515 {
516 $$ = new ast_expression($2, $1, $3, NULL);
517 }
518 ;
519
520 assignment_operator:
521 '=' { $$ = ast_assign; }
522 | MUL_ASSIGN { $$ = ast_mul_assign; }
523 | DIV_ASSIGN { $$ = ast_div_assign; }
524 | MOD_ASSIGN { $$ = ast_mod_assign; }
525 | ADD_ASSIGN { $$ = ast_add_assign; }
526 | SUB_ASSIGN { $$ = ast_sub_assign; }
527 | LEFT_ASSIGN { $$ = ast_ls_assign; }
528 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
529 | AND_ASSIGN { $$ = ast_and_assign; }
530 | XOR_ASSIGN { $$ = ast_xor_assign; }
531 | OR_ASSIGN { $$ = ast_or_assign; }
532 ;
533
534 expression:
535 assignment_expression
536 {
537 $$ = $1;
538 }
539 | expression ',' assignment_expression
540 {
541 if ($1->oper != ast_sequence) {
542 $$ = new ast_expression(ast_sequence, NULL, NULL, NULL);
543 insert_at_tail(& $$->expressions, $1);
544 } else {
545 $$ = $1;
546 }
547
548 insert_at_tail(& $$->expressions, $3);
549 }
550 ;
551
552 constant_expression:
553 conditional_expression
554 ;
555
556 declaration:
557 function_prototype ';'
558 {
559 $$ = $1;
560 }
561 | init_declarator_list ';'
562 {
563 $$ = $1;
564 }
565 | PRECISION precision_qualifier type_specifier_no_prec ';'
566 {
567 $$ = NULL; /* FINISHME */
568 }
569 ;
570
571 function_prototype:
572 function_declarator ')'
573 ;
574
575 function_declarator:
576 function_header
577 | function_header_with_parameters
578 ;
579
580 function_header_with_parameters:
581 function_header parameter_declaration
582 {
583 $$ = $1;
584 insert_at_tail(& $$->parameters,
585 (struct simple_node *) $2);
586 }
587 | function_header_with_parameters ',' parameter_declaration
588 {
589 $$ = $1;
590 insert_at_tail(& $$->parameters,
591 (struct simple_node *) $3);
592 }
593 ;
594
595 function_header:
596 fully_specified_type IDENTIFIER '('
597 {
598 $$ = new ast_function();
599 $$->return_type = $1;
600 $$->identifier = $2;
601 }
602 ;
603
604 parameter_declarator:
605 type_specifier IDENTIFIER
606 {
607 $$ = new ast_parameter_declarator();
608 $$->type = new ast_fully_specified_type();
609 $$->type->specifier = $1;
610 $$->identifier = $2;
611 }
612 | type_specifier IDENTIFIER '[' constant_expression ']'
613 {
614 $$ = new ast_parameter_declarator();
615 $$->type = new ast_fully_specified_type();
616 $$->type->specifier = $1;
617 $$->identifier = $2;
618 $$->is_array = true;
619 $$->array_size = $4;
620 }
621 ;
622
623 parameter_declaration:
624 parameter_type_qualifier parameter_qualifier parameter_declarator
625 {
626 $1.i |= $2.i;
627
628 $$ = $3;
629 $$->type->qualifier = $1.q;
630 }
631 | parameter_qualifier parameter_declarator
632 {
633 $$ = $2;
634 $$->type->qualifier = $1.q;
635 }
636 | parameter_type_qualifier parameter_qualifier parameter_type_specifier
637 {
638 $1.i |= $2.i;
639
640 $$ = new ast_parameter_declarator();
641 $$->type = new ast_fully_specified_type();
642 $$->type->qualifier = $1.q;
643 $$->type->specifier = $3;
644 }
645 | parameter_qualifier parameter_type_specifier
646 {
647 $$ = new ast_parameter_declarator();
648 $$->type = new ast_fully_specified_type();
649 $$->type->qualifier = $1.q;
650 $$->type->specifier = $2;
651 }
652 ;
653
654 parameter_qualifier:
655 /* empty */ { $$.i = 0; }
656 | IN { $$.i = 0; $$.q.in = 1; }
657 | OUT { $$.i = 0; $$.q.out = 1; }
658 | INOUT { $$.i = 0; $$.q.in = 1; $$.q.out = 1; }
659 ;
660
661 parameter_type_specifier:
662 type_specifier
663 ;
664
665 init_declarator_list:
666 single_declaration
667 | init_declarator_list ',' IDENTIFIER
668 {
669 ast_declaration *decl = new ast_declaration($3, false, NULL, NULL);
670
671 $$ = $1;
672 insert_at_tail(& $$->declarations,
673 (struct simple_node *) decl);
674 }
675 | init_declarator_list ',' IDENTIFIER '[' ']'
676 {
677 ast_declaration *decl = new ast_declaration($3, true, NULL, NULL);
678
679 $$ = $1;
680 insert_at_tail(& $$->declarations,
681 (struct simple_node *) decl);
682 }
683 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']'
684 {
685 ast_declaration *decl = new ast_declaration($3, true, $5, NULL);
686
687 $$ = $1;
688 insert_at_tail(& $$->declarations,
689 (struct simple_node *) decl);
690 }
691 | init_declarator_list ',' IDENTIFIER '[' ']' '=' initializer
692 {
693 ast_declaration *decl = new ast_declaration($3, true, NULL, $7);
694
695 $$ = $1;
696 insert_at_tail(& $$->declarations,
697 (struct simple_node *) decl);
698 }
699 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']' '=' initializer
700 {
701 ast_declaration *decl = new ast_declaration($3, true, $5, $8);
702
703 $$ = $1;
704 insert_at_tail(& $$->declarations,
705 (struct simple_node *) decl);
706 }
707 | init_declarator_list ',' IDENTIFIER '=' initializer
708 {
709 ast_declaration *decl = new ast_declaration($3, false, NULL, $5);
710
711 $$ = $1;
712 insert_at_tail(& $$->declarations,
713 (struct simple_node *) decl);
714 }
715 ;
716
717 // Grammar Note: No 'enum', or 'typedef'.
718 single_declaration:
719 fully_specified_type
720 {
721 $$ = new ast_declarator_list($1);
722 }
723 | fully_specified_type IDENTIFIER
724 {
725 ast_declaration *decl = new ast_declaration($2, false, NULL, NULL);
726
727 $$ = new ast_declarator_list($1);
728 insert_at_tail(& $$->declarations,
729 (struct simple_node *) decl);
730 }
731 | fully_specified_type IDENTIFIER '[' ']'
732 {
733 ast_declaration *decl = new ast_declaration($2, true, NULL, NULL);
734
735 $$ = new ast_declarator_list($1);
736 insert_at_tail(& $$->declarations,
737 (struct simple_node *) decl);
738 }
739 | fully_specified_type IDENTIFIER '[' constant_expression ']'
740 {
741 ast_declaration *decl = new ast_declaration($2, true, $4, NULL);
742
743 $$ = new ast_declarator_list($1);
744 insert_at_tail(& $$->declarations,
745 (struct simple_node *) decl);
746 }
747 | fully_specified_type IDENTIFIER '[' ']' '=' initializer
748 {
749 ast_declaration *decl = new ast_declaration($2, true, NULL, $6);
750
751 $$ = new ast_declarator_list($1);
752 insert_at_tail(& $$->declarations,
753 (struct simple_node *) decl);
754 }
755 | fully_specified_type IDENTIFIER '[' constant_expression ']' '=' initializer
756 {
757 ast_declaration *decl = new ast_declaration($2, true, $4, $7);
758
759 $$ = new ast_declarator_list($1);
760 insert_at_tail(& $$->declarations,
761 (struct simple_node *) decl);
762 }
763 | fully_specified_type IDENTIFIER '=' initializer
764 {
765 ast_declaration *decl = new ast_declaration($2, false, NULL, $4);
766
767 $$ = new ast_declarator_list($1);
768 insert_at_tail(& $$->declarations,
769 (struct simple_node *) decl);
770 }
771 | INVARIANT IDENTIFIER // Vertex only.
772 {
773 ast_declaration *decl = new ast_declaration($2, false, NULL, NULL);
774
775 $$ = new ast_declarator_list(NULL);
776 $$->invariant = true;
777
778 insert_at_tail(& $$->declarations,
779 (struct simple_node *) decl);
780 }
781 ;
782
783 fully_specified_type:
784 type_specifier
785 {
786 $$ = new ast_fully_specified_type();
787 $$->specifier = $1;
788 }
789 | type_qualifier type_specifier
790 {
791 $$ = new ast_fully_specified_type();
792 $$->qualifier = $1.q;
793 $$->specifier = $2;
794 }
795 ;
796
797 interpolation_qualifier:
798 SMOOTH { $$.i = 0; $$.q.smooth = 1; }
799 | FLAT { $$.i = 0; $$.q.flat = 1; }
800 | NOPERSPECTIVE { $$.i = 0; $$.q.noperspective = 1; }
801 ;
802
803 parameter_type_qualifier:
804 CONST { $$.i = 0; $$.q.constant = 1; }
805 ;
806
807 type_qualifier:
808 storage_qualifier
809 | interpolation_qualifier type_qualifier
810 {
811 $$.i = $1.i | $2.i;
812 }
813 | INVARIANT type_qualifier
814 {
815 $$ = $2;
816 $$.q.invariant = 1;
817 }
818 ;
819
820 storage_qualifier:
821 CONST { $$.i = 0; $$.q.constant = 1; }
822 | ATTRIBUTE { $$.i = 0; $$.q.attribute = 1; }
823 | VARYING { $$.i = 0; $$.q.varying = 1; }
824 | CENTROID VARYING { $$.i = 0; $$.q.centroid = 1; $$.q.varying = 1; }
825 | IN { $$.i = 0; $$.q.in = 1; }
826 | OUT { $$.i = 0; $$.q.out = 1; }
827 | CENTROID IN { $$.i = 0; $$.q.centroid = 1; $$.q.in = 1; }
828 | CENTROID OUT { $$.i = 0; $$.q.centroid = 1; $$.q.out = 1; }
829 | UNIFORM { $$.i = 0; $$.q.uniform = 1; }
830 ;
831
832 type_specifier:
833 type_specifier_no_prec
834 | precision_qualifier type_specifier_no_prec
835 {
836 $$ = $2;
837 $$->precision = $1;
838 }
839 ;
840
841 type_specifier_no_prec:
842 type_specifier_nonarray
843 | type_specifier_nonarray '[' ']'
844 {
845 $$ = $1;
846 $$->is_array = true;
847 $$->array_size = NULL;
848 }
849 | type_specifier_nonarray '[' constant_expression ']'
850 {
851 $$ = $1;
852 $$->is_array = true;
853 $$->array_size = $3;
854 }
855 ;
856
857 type_specifier_nonarray:
858 basic_type_specifier_nonarray
859 {
860 $$ = new ast_type_specifier($1);
861 }
862 | struct_specifier
863 {
864 $$ = new ast_type_specifier(ast_struct);
865 $$->structure = $1;
866 }
867 | TYPE_NAME
868 {
869 $$ = new ast_type_specifier(ast_type_name);
870 $$->type_name = $1;
871 }
872 ;
873
874 basic_type_specifier_nonarray:
875 VOID { $$ = ast_void; }
876 | FLOAT { $$ = ast_float; }
877 | INT { $$ = ast_int; }
878 | UINT { $$ = ast_uint; }
879 | BOOL { $$ = ast_bool; }
880 | VEC2 { $$ = ast_vec2; }
881 | VEC3 { $$ = ast_vec3; }
882 | VEC4 { $$ = ast_vec4; }
883 | BVEC2 { $$ = ast_bvec2; }
884 | BVEC3 { $$ = ast_bvec3; }
885 | BVEC4 { $$ = ast_bvec4; }
886 | IVEC2 { $$ = ast_ivec2; }
887 | IVEC3 { $$ = ast_ivec3; }
888 | IVEC4 { $$ = ast_ivec4; }
889 | UVEC2 { $$ = ast_uvec2; }
890 | UVEC3 { $$ = ast_uvec3; }
891 | UVEC4 { $$ = ast_uvec4; }
892 | MAT2 { $$ = ast_mat2; }
893 | MAT3 { $$ = ast_mat3; }
894 | MAT4 { $$ = ast_mat4; }
895 | MAT2X2 { $$ = ast_mat2; }
896 | MAT2X3 { $$ = ast_mat2x3; }
897 | MAT2X4 { $$ = ast_mat2x4; }
898 | MAT3X2 { $$ = ast_mat3x2; }
899 | MAT3X3 { $$ = ast_mat3; }
900 | MAT3X4 { $$ = ast_mat3x4; }
901 | MAT4X2 { $$ = ast_mat4x2; }
902 | MAT4X3 { $$ = ast_mat4x3; }
903 | MAT4X4 { $$ = ast_mat4; }
904 | SAMPLER1D { $$ = ast_sampler1d; }
905 | SAMPLER2D { $$ = ast_sampler2d; }
906 | SAMPLER3D { $$ = ast_sampler3d; }
907 | SAMPLERCUBE { $$ = ast_samplercube; }
908 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
909 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
910 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
911 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
912 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
913 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
914 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
915 | ISAMPLER1D { $$ = ast_isampler1d; }
916 | ISAMPLER2D { $$ = ast_isampler2d; }
917 | ISAMPLER3D { $$ = ast_isampler3d; }
918 | ISAMPLERCUBE { $$ = ast_isamplercube; }
919 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
920 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
921 | USAMPLER1D { $$ = ast_usampler1d; }
922 | USAMPLER2D { $$ = ast_usampler2d; }
923 | USAMPLER3D { $$ = ast_usampler3d; }
924 | USAMPLERCUBE { $$ = ast_usamplercube; }
925 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
926 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
927 ;
928
929 precision_qualifier:
930 HIGH_PRECISION { $$ = ast_precision_high; }
931 | MEDIUM_PRECISION { $$ = ast_precision_medium; }
932 | LOW_PRECISION { $$ = ast_precision_low; }
933 ;
934
935 struct_specifier:
936 STRUCT IDENTIFIER '{' struct_declaration_list '}'
937 {
938 $$ = new ast_struct_specifier($2, $4);
939
940 _mesa_symbol_table_add_symbol(state->symbols, 0, $2, $$);
941 }
942 | STRUCT '{' struct_declaration_list '}'
943 {
944 $$ = new ast_struct_specifier(NULL, $3);
945 }
946 ;
947
948 struct_declaration_list:
949 struct_declaration
950 {
951 $$ = (struct ast_node *) $1;
952 }
953 | struct_declaration_list struct_declaration
954 {
955 $$ = (struct ast_node *) $1;
956 insert_at_tail((struct simple_node *) $$,
957 (struct simple_node *) $2);
958 }
959 ;
960
961 struct_declaration:
962 type_specifier struct_declarator_list ';'
963 {
964 ast_fully_specified_type *type = new ast_fully_specified_type();
965
966 type->specifier = $1;
967 $$ = new ast_declarator_list(type);
968
969 insert_at_tail((struct simple_node *) $2,
970 & $$->declarations);
971 }
972 ;
973
974 struct_declarator_list:
975 struct_declarator
976 | struct_declarator_list ',' struct_declarator
977 {
978 $$ = $1;
979 insert_at_tail((struct simple_node *) $$,
980 (struct simple_node *) $3);
981 }
982 ;
983
984 struct_declarator:
985 IDENTIFIER
986 {
987 $$ = new ast_declaration($1, false, NULL, NULL);
988 }
989 | IDENTIFIER '[' constant_expression ']'
990 {
991 $$ = new ast_declaration($1, true, $3, NULL);
992 }
993 ;
994
995 initializer:
996 assignment_expression
997 ;
998
999 declaration_statement:
1000 declaration
1001 ;
1002
1003 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1004 // supported.
1005 statement:
1006 statement_matched
1007 | statement_unmatched
1008 ;
1009
1010 statement_matched:
1011 compound_statement { $$ = (struct ast_node *) $1; }
1012 | simple_statement
1013 ;
1014
1015 statement_unmatched:
1016 selection_statement_unmatched
1017 ;
1018
1019 simple_statement:
1020 declaration_statement
1021 | expression_statement
1022 | selection_statement_matched
1023 | switch_statement { $$ = NULL; }
1024 | case_label { $$ = NULL; }
1025 | iteration_statement
1026 | jump_statement
1027 ;
1028
1029 compound_statement:
1030 '{' '}'
1031 {
1032 $$ = new ast_compound_statement(true, NULL);
1033 }
1034 | '{' statement_list '}'
1035 {
1036 $$ = new ast_compound_statement(true, $2);
1037 }
1038 ;
1039
1040 statement_no_new_scope:
1041 compound_statement_no_new_scope { $$ = (struct ast_node *) $1; }
1042 | simple_statement
1043 ;
1044
1045 compound_statement_no_new_scope:
1046 '{' '}'
1047 {
1048 $$ = new ast_compound_statement(false, NULL);
1049 }
1050 | '{' statement_list '}'
1051 {
1052 $$ = new ast_compound_statement(false, $2);
1053 }
1054 ;
1055
1056 statement_list:
1057 statement
1058 {
1059 if ($1 == NULL) {
1060 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1061 assert($1 != NULL);
1062 }
1063
1064 $$ = $1;
1065 make_empty_list((struct simple_node *) $$);
1066 }
1067 | statement_list statement
1068 {
1069 if ($2 == NULL) {
1070 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1071 assert($2 != NULL);
1072 }
1073 $$ = $1;
1074 insert_at_tail((struct simple_node *) $$,
1075 (struct simple_node *) $2);
1076 }
1077 ;
1078
1079 expression_statement:
1080 ';'
1081 {
1082 $$ = new ast_expression_statement(NULL);
1083 }
1084 | expression ';'
1085 {
1086 $$ = new ast_expression_statement($1);
1087 }
1088 ;
1089
1090 selection_statement_matched:
1091 IF '(' expression ')' statement_matched ELSE statement_matched
1092 {
1093 $$ = new ast_selection_statement($3, $5, $7);
1094 }
1095 ;
1096
1097 selection_statement_unmatched:
1098 IF '(' expression ')' statement_matched
1099 {
1100 $$ = new ast_selection_statement($3, $5, NULL);
1101 }
1102 | IF '(' expression ')' statement_unmatched
1103 {
1104 $$ = new ast_selection_statement($3, $5, NULL);
1105 }
1106 | IF '(' expression ')' statement_matched ELSE statement_unmatched
1107 {
1108 $$ = new ast_selection_statement($3, $5, $7);
1109 }
1110 ;
1111
1112 condition:
1113 expression
1114 {
1115 $$ = (struct ast_node *) $1;
1116 }
1117 | fully_specified_type IDENTIFIER '=' initializer
1118 {
1119 ast_declaration *decl = new ast_declaration($2, false, NULL, $4);
1120 ast_declarator_list *declarator = new ast_declarator_list($1);
1121
1122 insert_at_tail(& declarator->declarations,
1123 (struct simple_node *) decl);
1124
1125 $$ = declarator;
1126 }
1127 ;
1128
1129 switch_statement:
1130 SWITCH '(' expression ')' compound_statement
1131 ;
1132
1133 case_label:
1134 CASE expression ':'
1135 | DEFAULT ':'
1136 ;
1137
1138 iteration_statement:
1139 WHILE '(' condition ')' statement_no_new_scope
1140 {
1141 $$ = new ast_iteration_statement(ast_iteration_statement::ast_while,
1142 NULL, $3, NULL, $5);
1143 }
1144 | DO statement WHILE '(' expression ')' ';'
1145 {
1146 $$ = new ast_iteration_statement(ast_iteration_statement::ast_do_while,
1147 NULL, $5, NULL, $2);
1148 }
1149 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1150 {
1151 $$ = new ast_iteration_statement(ast_iteration_statement::ast_for,
1152 $3, $4.cond, $4.rest, $6);
1153 }
1154 ;
1155
1156 for_init_statement:
1157 expression_statement
1158 | declaration_statement
1159 ;
1160
1161 conditionopt:
1162 condition
1163 | /* empty */
1164 {
1165 $$ = NULL;
1166 }
1167 ;
1168
1169 for_rest_statement:
1170 conditionopt ';'
1171 {
1172 $$.cond = $1;
1173 $$.rest = NULL;
1174 }
1175 | conditionopt ';' expression
1176 {
1177 $$.cond = $1;
1178 $$.rest = $3;
1179 }
1180 ;
1181
1182 // Grammar Note: No 'goto'. Gotos are not supported.
1183 jump_statement:
1184 CONTINUE ';'
1185 {
1186 $$ = new ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1187 }
1188 | BREAK ';'
1189 {
1190 $$ = new ast_jump_statement(ast_jump_statement::ast_break, NULL);
1191 }
1192 | RETURN ';'
1193 {
1194 $$ = new ast_jump_statement(ast_jump_statement::ast_return, NULL);
1195 }
1196 | RETURN expression ';'
1197 {
1198 $$ = new ast_jump_statement(ast_jump_statement::ast_return, $2);
1199 }
1200 | DISCARD ';' // Fragment shader only.
1201 {
1202 $$ = new ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1203 }
1204 ;
1205
1206 external_declaration:
1207 function_definition { $$ = $1; }
1208 | declaration { $$ = $1; }
1209 ;
1210
1211 function_definition:
1212 function_prototype compound_statement_no_new_scope
1213 {
1214 $$ = new ast_function_definition();
1215 $$->prototype = $1;
1216 $$->body = $2;
1217 }
1218 ;