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