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