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