Use glsl_type::get_instance instead of _mesa_glsl_get_vector_type
[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 | SAMPLER3D { $$ = ast_sampler3d; }
904 | SAMPLERCUBE { $$ = ast_samplercube; }
905 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
906 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
907 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
908 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
909 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
910 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
911 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
912 | ISAMPLER1D { $$ = ast_isampler1d; }
913 | ISAMPLER2D { $$ = ast_isampler2d; }
914 | ISAMPLER3D { $$ = ast_isampler3d; }
915 | ISAMPLERCUBE { $$ = ast_isamplercube; }
916 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
917 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
918 | USAMPLER1D { $$ = ast_usampler1d; }
919 | USAMPLER2D { $$ = ast_usampler2d; }
920 | USAMPLER3D { $$ = ast_usampler3d; }
921 | USAMPLERCUBE { $$ = ast_usamplercube; }
922 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
923 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
924 ;
925
926 precision_qualifier:
927 HIGH_PRECISION { $$ = ast_precision_high; }
928 | MEDIUM_PRECISION { $$ = ast_precision_medium; }
929 | LOW_PRECISION { $$ = ast_precision_low; }
930 ;
931
932 struct_specifier:
933 STRUCT IDENTIFIER '{' struct_declaration_list '}'
934 {
935 $$ = new ast_struct_specifier($2, $4);
936 }
937 | STRUCT '{' struct_declaration_list '}'
938 {
939 $$ = new ast_struct_specifier(NULL, $3);
940 }
941 ;
942
943 struct_declaration_list:
944 struct_declaration
945 {
946 $$ = (struct ast_node *) $1;
947 }
948 | struct_declaration_list struct_declaration
949 {
950 $$ = (struct ast_node *) $1;
951 insert_at_tail((struct simple_node *) $$,
952 (struct simple_node *) $2);
953 }
954 ;
955
956 struct_declaration:
957 type_specifier struct_declarator_list ';'
958 {
959 ast_fully_specified_type *type = new ast_fully_specified_type();
960
961 type->specifier = $1;
962 $$ = new ast_declarator_list(type);
963
964 insert_at_tail((struct simple_node *) $2,
965 & $$->declarations);
966 }
967 ;
968
969 struct_declarator_list:
970 struct_declarator
971 | struct_declarator_list ',' struct_declarator
972 {
973 $$ = $1;
974 insert_at_tail((struct simple_node *) $$,
975 (struct simple_node *) $3);
976 }
977 ;
978
979 struct_declarator:
980 IDENTIFIER
981 {
982 $$ = new ast_declaration($1, false, NULL, NULL);
983 }
984 | IDENTIFIER '[' constant_expression ']'
985 {
986 $$ = new ast_declaration($1, true, $3, NULL);
987 }
988 ;
989
990 initializer:
991 assignment_expression
992 ;
993
994 declaration_statement:
995 declaration
996 ;
997
998 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
999 // supported.
1000 statement:
1001 statement_matched
1002 | statement_unmatched
1003 ;
1004
1005 statement_matched:
1006 compound_statement { $$ = (struct ast_node *) $1; }
1007 | simple_statement
1008 ;
1009
1010 statement_unmatched:
1011 selection_statement_unmatched
1012 ;
1013
1014 simple_statement:
1015 declaration_statement
1016 | expression_statement
1017 | selection_statement_matched
1018 | switch_statement { $$ = NULL; }
1019 | case_label { $$ = NULL; }
1020 | iteration_statement
1021 | jump_statement
1022 ;
1023
1024 compound_statement:
1025 '{' '}'
1026 {
1027 $$ = new ast_compound_statement(true, NULL);
1028 }
1029 | '{' statement_list '}'
1030 {
1031 $$ = new ast_compound_statement(true, $2);
1032 }
1033 ;
1034
1035 statement_no_new_scope:
1036 compound_statement_no_new_scope { $$ = (struct ast_node *) $1; }
1037 | simple_statement
1038 ;
1039
1040 compound_statement_no_new_scope:
1041 '{' '}'
1042 {
1043 $$ = new ast_compound_statement(false, NULL);
1044 }
1045 | '{' statement_list '}'
1046 {
1047 $$ = new ast_compound_statement(false, $2);
1048 }
1049 ;
1050
1051 statement_list:
1052 statement
1053 {
1054 if ($1 == NULL) {
1055 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1056 assert($1 != NULL);
1057 }
1058
1059 $$ = $1;
1060 make_empty_list((struct simple_node *) $$);
1061 }
1062 | statement_list statement
1063 {
1064 if ($2 == NULL) {
1065 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1066 assert($2 != NULL);
1067 }
1068 $$ = $1;
1069 insert_at_tail((struct simple_node *) $$,
1070 (struct simple_node *) $2);
1071 }
1072 ;
1073
1074 expression_statement:
1075 ';'
1076 {
1077 $$ = new ast_expression_statement(NULL);
1078 }
1079 | expression ';'
1080 {
1081 $$ = new ast_expression_statement($1);
1082 }
1083 ;
1084
1085 selection_statement_matched:
1086 IF '(' expression ')' statement_matched ELSE statement_matched
1087 {
1088 $$ = new ast_selection_statement($3, $5, $7);
1089 }
1090 ;
1091
1092 selection_statement_unmatched:
1093 IF '(' expression ')' statement_matched
1094 {
1095 $$ = new ast_selection_statement($3, $5, NULL);
1096 }
1097 | IF '(' expression ')' statement_unmatched
1098 {
1099 $$ = new ast_selection_statement($3, $5, NULL);
1100 }
1101 | IF '(' expression ')' statement_matched ELSE statement_unmatched
1102 {
1103 $$ = new ast_selection_statement($3, $5, $7);
1104 }
1105 ;
1106
1107 condition:
1108 expression
1109 {
1110 $$ = (struct ast_node *) $1;
1111 }
1112 | fully_specified_type IDENTIFIER '=' initializer
1113 {
1114 ast_declaration *decl = new ast_declaration($2, false, NULL, $4);
1115 ast_declarator_list *declarator = new ast_declarator_list($1);
1116
1117 insert_at_tail(& declarator->declarations,
1118 (struct simple_node *) decl);
1119
1120 $$ = declarator;
1121 }
1122 ;
1123
1124 switch_statement:
1125 SWITCH '(' expression ')' compound_statement
1126 ;
1127
1128 case_label:
1129 CASE expression ':'
1130 | DEFAULT ':'
1131 ;
1132
1133 iteration_statement:
1134 WHILE '(' condition ')' statement_no_new_scope
1135 {
1136 $$ = new ast_iteration_statement(ast_iteration_statement::ast_while,
1137 NULL, $3, NULL, $5);
1138 }
1139 | DO statement WHILE '(' expression ')' ';'
1140 {
1141 $$ = new ast_iteration_statement(ast_iteration_statement::ast_do_while,
1142 NULL, $5, NULL, $2);
1143 }
1144 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1145 {
1146 $$ = new ast_iteration_statement(ast_iteration_statement::ast_for,
1147 $3, $4.cond, $4.rest, $6);
1148 }
1149 ;
1150
1151 for_init_statement:
1152 expression_statement
1153 | declaration_statement
1154 ;
1155
1156 conditionopt:
1157 condition
1158 | /* empty */
1159 {
1160 $$ = NULL;
1161 }
1162 ;
1163
1164 for_rest_statement:
1165 conditionopt ';'
1166 {
1167 $$.cond = $1;
1168 $$.rest = NULL;
1169 }
1170 | conditionopt ';' expression
1171 {
1172 $$.cond = $1;
1173 $$.rest = $3;
1174 }
1175 ;
1176
1177 // Grammar Note: No 'goto'. Gotos are not supported.
1178 jump_statement:
1179 CONTINUE ';'
1180 {
1181 $$ = new ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1182 }
1183 | BREAK ';'
1184 {
1185 $$ = new ast_jump_statement(ast_jump_statement::ast_break, NULL);
1186 }
1187 | RETURN ';'
1188 {
1189 $$ = new ast_jump_statement(ast_jump_statement::ast_return, NULL);
1190 }
1191 | RETURN expression ';'
1192 {
1193 $$ = new ast_jump_statement(ast_jump_statement::ast_return, $2);
1194 }
1195 | DISCARD ';' // Fragment shader only.
1196 {
1197 $$ = new ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1198 }
1199 ;
1200
1201 external_declaration:
1202 function_definition { $$ = $1; }
1203 | declaration { $$ = $1; }
1204 ;
1205
1206 function_definition:
1207 function_prototype compound_statement_no_new_scope
1208 {
1209 $$ = new ast_function_definition();
1210 $$->prototype = $1;
1211 $$->body = $2;
1212 }
1213 ;