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