glsl: Clear type_qualifier using memset
[mesa.git] / src / glsl / 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 %error-verbose
39
40 %locations
41 %initial-action {
42 @$.first_line = 1;
43 @$.first_column = 1;
44 @$.last_line = 1;
45 @$.last_column = 1;
46 @$.source = 0;
47 }
48
49 %lex-param {void *scanner}
50 %parse-param {struct _mesa_glsl_parse_state *state}
51
52 %union {
53 int n;
54 float real;
55 char *identifier;
56
57 union {
58 struct ast_type_qualifier q;
59 unsigned i;
60 } type_qualifier;
61
62 ast_node *node;
63 ast_type_specifier *type_specifier;
64 ast_fully_specified_type *fully_specified_type;
65 ast_function *function;
66 ast_parameter_declarator *parameter_declarator;
67 ast_function_definition *function_definition;
68 ast_compound_statement *compound_statement;
69 ast_expression *expression;
70 ast_declarator_list *declarator_list;
71 ast_struct_specifier *struct_specifier;
72 ast_declaration *declaration;
73
74 struct {
75 ast_node *cond;
76 ast_expression *rest;
77 } for_rest_statement;
78
79 struct {
80 ast_node *then_statement;
81 ast_node *else_statement;
82 } selection_rest_statement;
83 }
84
85 %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK
86 %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
87 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
88 %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING
89 %token NOPERSPECTIVE FLAT SMOOTH
90 %token MAT2X2 MAT2X3 MAT2X4
91 %token MAT3X2 MAT3X3 MAT3X4
92 %token MAT4X2 MAT4X3 MAT4X4
93 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
94 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
95 %token SAMPLER2DARRAYSHADOW ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
96 %token ISAMPLER1DARRAY ISAMPLER2DARRAY USAMPLER1D USAMPLER2D USAMPLER3D
97 %token USAMPLERCUBE USAMPLER1DARRAY USAMPLER2DARRAY
98 %token STRUCT VOID_TOK WHILE
99 %token <identifier> IDENTIFIER
100 %token <real> FLOATCONSTANT
101 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
102 %token <identifier> FIELD_SELECTION
103 %token LEFT_OP RIGHT_OP
104 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
105 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
106 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
107 %token SUB_ASSIGN
108 %token INVARIANT
109 %token LOWP MEDIUMP HIGHP SUPERP PRECISION
110
111 %token VERSION EXTENSION LINE COLON EOL INTERFACE OUTPUT
112 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
113 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
114 %token LAYOUT_TOK
115
116 /* Reserved words that are not actually used in the grammar.
117 */
118 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
119 %token INLINE_TOK NOINLINE VOLATILE PUBLIC_TOK STATIC EXTERN EXTERNAL
120 %token LONG_TOK SHORT_TOK DOUBLE_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK OUPTUT
121 %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
122 %token SAMPLER2DRECT SAMPLER3DRECT SAMPLER2DRECTSHADOW
123 %token SIZEOF CAST NAMESPACE USING
124
125 %token ERROR_TOK
126
127 %token COMMON PARTITION ACTIVE SAMPLERBUFFER FILTER
128 %token IMAGE1D IMAGE2D IMAGE3D IMAGECUBE IMAGE1DARRAY IMAGE2DARRAY
129 %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGECUBE IIMAGE1DARRAY IIMAGE2DARRAY
130 %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGECUBE UIMAGE1DARRAY UIMAGE2DARRAY
131 %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGEBUFFER IIMAGEBUFFER UIMAGEBUFFER
132 %token ROW_MAJOR
133
134 %type <identifier> variable_identifier
135 %type <node> statement
136 %type <node> statement_list
137 %type <node> simple_statement
138 %type <n> precision_qualifier
139 %type <type_qualifier> type_qualifier
140 %type <type_qualifier> storage_qualifier
141 %type <type_qualifier> interpolation_qualifier
142 %type <type_qualifier> layout_qualifier
143 %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
144 %type <type_specifier> type_specifier
145 %type <type_specifier> type_specifier_no_prec
146 %type <type_specifier> type_specifier_nonarray
147 %type <n> basic_type_specifier_nonarray
148 %type <fully_specified_type> fully_specified_type
149 %type <function> function_prototype
150 %type <function> function_header
151 %type <function> function_header_with_parameters
152 %type <function> function_declarator
153 %type <parameter_declarator> parameter_declarator
154 %type <parameter_declarator> parameter_declaration
155 %type <type_qualifier> parameter_qualifier
156 %type <type_qualifier> parameter_type_qualifier
157 %type <type_specifier> parameter_type_specifier
158 %type <function_definition> function_definition
159 %type <compound_statement> compound_statement_no_new_scope
160 %type <compound_statement> compound_statement
161 %type <node> statement_no_new_scope
162 %type <node> expression_statement
163 %type <expression> expression
164 %type <expression> primary_expression
165 %type <expression> assignment_expression
166 %type <expression> conditional_expression
167 %type <expression> logical_or_expression
168 %type <expression> logical_xor_expression
169 %type <expression> logical_and_expression
170 %type <expression> inclusive_or_expression
171 %type <expression> exclusive_or_expression
172 %type <expression> and_expression
173 %type <expression> equality_expression
174 %type <expression> relational_expression
175 %type <expression> shift_expression
176 %type <expression> additive_expression
177 %type <expression> multiplicative_expression
178 %type <expression> unary_expression
179 %type <expression> constant_expression
180 %type <expression> integer_expression
181 %type <expression> postfix_expression
182 %type <expression> function_call_header_with_parameters
183 %type <expression> function_call_header_no_parameters
184 %type <expression> function_call_header
185 %type <expression> function_call_generic
186 %type <expression> function_call_or_method
187 %type <expression> function_call
188 %type <n> assignment_operator
189 %type <n> unary_operator
190 %type <expression> function_identifier
191 %type <node> external_declaration
192 %type <declarator_list> init_declarator_list
193 %type <declarator_list> single_declaration
194 %type <expression> initializer
195 %type <node> declaration
196 %type <node> declaration_statement
197 %type <node> jump_statement
198 %type <struct_specifier> struct_specifier
199 %type <node> struct_declaration_list
200 %type <declarator_list> struct_declaration
201 %type <declaration> struct_declarator
202 %type <declaration> struct_declarator_list
203 %type <node> selection_statement
204 %type <selection_rest_statement> selection_rest_statement
205 %type <node> iteration_statement
206 %type <node> condition
207 %type <node> conditionopt
208 %type <node> for_init_statement
209 %type <for_rest_statement> for_rest_statement
210 %%
211
212 translation_unit:
213 version_statement extension_statement_list
214 {
215 _mesa_glsl_initialize_types(state);
216 }
217 external_declaration_list
218 ;
219
220 version_statement:
221 /* blank - no #version specified: defaults are already set */
222 | VERSION INTCONSTANT EOL
223 {
224 switch ($2) {
225 case 100:
226 state->es_shader = true;
227 case 110:
228 case 120:
229 case 130:
230 /* FINISHME: Check against implementation support versions. */
231 state->language_version = $2;
232 break;
233 default:
234 _mesa_glsl_error(& @2, state, "Shading language version"
235 "%u is not supported\n", $2);
236 break;
237 }
238 }
239 ;
240
241 pragma_statement:
242 PRAGMA_DEBUG_ON EOL
243 | PRAGMA_DEBUG_OFF EOL
244 | PRAGMA_OPTIMIZE_ON EOL
245 | PRAGMA_OPTIMIZE_OFF EOL
246 ;
247
248 extension_statement_list:
249
250 | extension_statement_list extension_statement
251 ;
252
253 extension_statement:
254 EXTENSION IDENTIFIER COLON IDENTIFIER EOL
255 {
256 if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
257 YYERROR;
258 }
259 }
260 ;
261
262 external_declaration_list:
263 external_declaration
264 {
265 /* FINISHME: The NULL test is only required because 'precision'
266 * FINISHME: statements are not yet supported.
267 */
268 if ($1 != NULL)
269 state->translation_unit.push_tail(& $1->link);
270 }
271 | external_declaration_list external_declaration
272 {
273 /* FINISHME: The NULL test is only required because 'precision'
274 * FINISHME: statements are not yet supported.
275 */
276 if ($2 != NULL)
277 state->translation_unit.push_tail(& $2->link);
278 }
279 ;
280
281 variable_identifier:
282 IDENTIFIER
283 ;
284
285 primary_expression:
286 variable_identifier
287 {
288 void *ctx = state;
289 $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
290 $$->set_location(yylloc);
291 $$->primary_expression.identifier = $1;
292 }
293 | INTCONSTANT
294 {
295 void *ctx = state;
296 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
297 $$->set_location(yylloc);
298 $$->primary_expression.int_constant = $1;
299 }
300 | UINTCONSTANT
301 {
302 void *ctx = state;
303 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
304 $$->set_location(yylloc);
305 $$->primary_expression.uint_constant = $1;
306 }
307 | FLOATCONSTANT
308 {
309 void *ctx = state;
310 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
311 $$->set_location(yylloc);
312 $$->primary_expression.float_constant = $1;
313 }
314 | BOOLCONSTANT
315 {
316 void *ctx = state;
317 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
318 $$->set_location(yylloc);
319 $$->primary_expression.bool_constant = $1;
320 }
321 | '(' expression ')'
322 {
323 $$ = $2;
324 }
325 ;
326
327 postfix_expression:
328 primary_expression
329 | postfix_expression '[' integer_expression ']'
330 {
331 void *ctx = state;
332 $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
333 $$->set_location(yylloc);
334 }
335 | function_call
336 {
337 $$ = $1;
338 }
339 | postfix_expression '.' IDENTIFIER
340 {
341 void *ctx = state;
342 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
343 $$->set_location(yylloc);
344 $$->primary_expression.identifier = $3;
345 }
346 | postfix_expression INC_OP
347 {
348 void *ctx = state;
349 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
350 $$->set_location(yylloc);
351 }
352 | postfix_expression DEC_OP
353 {
354 void *ctx = state;
355 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
356 $$->set_location(yylloc);
357 }
358 ;
359
360 integer_expression:
361 expression
362 ;
363
364 function_call:
365 function_call_or_method
366 ;
367
368 function_call_or_method:
369 function_call_generic
370 | postfix_expression '.' function_call_generic
371 {
372 void *ctx = state;
373 $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
374 $$->set_location(yylloc);
375 }
376 ;
377
378 function_call_generic:
379 function_call_header_with_parameters ')'
380 | function_call_header_no_parameters ')'
381 ;
382
383 function_call_header_no_parameters:
384 function_call_header VOID_TOK
385 | function_call_header
386 ;
387
388 function_call_header_with_parameters:
389 function_call_header assignment_expression
390 {
391 $$ = $1;
392 $$->set_location(yylloc);
393 $$->expressions.push_tail(& $2->link);
394 }
395 | function_call_header_with_parameters ',' assignment_expression
396 {
397 $$ = $1;
398 $$->set_location(yylloc);
399 $$->expressions.push_tail(& $3->link);
400 }
401 ;
402
403 // Grammar Note: Constructors look like functions, but lexical
404 // analysis recognized most of them as keywords. They are now
405 // recognized through "type_specifier".
406 function_call_header:
407 function_identifier '('
408 ;
409
410 function_identifier:
411 type_specifier
412 {
413 void *ctx = state;
414 $$ = new(ctx) ast_function_expression($1);
415 $$->set_location(yylloc);
416 }
417 | IDENTIFIER
418 {
419 void *ctx = state;
420 ast_expression *callee = new(ctx) ast_expression($1);
421 $$ = new(ctx) ast_function_expression(callee);
422 $$->set_location(yylloc);
423 }
424 | FIELD_SELECTION
425 {
426 void *ctx = state;
427 ast_expression *callee = new(ctx) ast_expression($1);
428 $$ = new(ctx) ast_function_expression(callee);
429 $$->set_location(yylloc);
430 }
431 ;
432
433 // Grammar Note: No traditional style type casts.
434 unary_expression:
435 postfix_expression
436 | INC_OP unary_expression
437 {
438 void *ctx = state;
439 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
440 $$->set_location(yylloc);
441 }
442 | DEC_OP unary_expression
443 {
444 void *ctx = state;
445 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
446 $$->set_location(yylloc);
447 }
448 | unary_operator unary_expression
449 {
450 void *ctx = state;
451 $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
452 $$->set_location(yylloc);
453 }
454 ;
455
456 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
457 unary_operator:
458 '+' { $$ = ast_plus; }
459 | '-' { $$ = ast_neg; }
460 | '!' { $$ = ast_logic_not; }
461 | '~' { $$ = ast_bit_not; }
462 ;
463
464 multiplicative_expression:
465 unary_expression
466 | multiplicative_expression '*' unary_expression
467 {
468 void *ctx = state;
469 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
470 $$->set_location(yylloc);
471 }
472 | multiplicative_expression '/' unary_expression
473 {
474 void *ctx = state;
475 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
476 $$->set_location(yylloc);
477 }
478 | multiplicative_expression '%' unary_expression
479 {
480 void *ctx = state;
481 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
482 $$->set_location(yylloc);
483 }
484 ;
485
486 additive_expression:
487 multiplicative_expression
488 | additive_expression '+' multiplicative_expression
489 {
490 void *ctx = state;
491 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
492 $$->set_location(yylloc);
493 }
494 | additive_expression '-' multiplicative_expression
495 {
496 void *ctx = state;
497 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
498 $$->set_location(yylloc);
499 }
500 ;
501
502 shift_expression:
503 additive_expression
504 | shift_expression LEFT_OP additive_expression
505 {
506 void *ctx = state;
507 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
508 $$->set_location(yylloc);
509 }
510 | shift_expression RIGHT_OP additive_expression
511 {
512 void *ctx = state;
513 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
514 $$->set_location(yylloc);
515 }
516 ;
517
518 relational_expression:
519 shift_expression
520 | relational_expression '<' shift_expression
521 {
522 void *ctx = state;
523 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
524 $$->set_location(yylloc);
525 }
526 | relational_expression '>' shift_expression
527 {
528 void *ctx = state;
529 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
530 $$->set_location(yylloc);
531 }
532 | relational_expression LE_OP shift_expression
533 {
534 void *ctx = state;
535 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
536 $$->set_location(yylloc);
537 }
538 | relational_expression GE_OP shift_expression
539 {
540 void *ctx = state;
541 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
542 $$->set_location(yylloc);
543 }
544 ;
545
546 equality_expression:
547 relational_expression
548 | equality_expression EQ_OP relational_expression
549 {
550 void *ctx = state;
551 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
552 $$->set_location(yylloc);
553 }
554 | equality_expression NE_OP relational_expression
555 {
556 void *ctx = state;
557 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
558 $$->set_location(yylloc);
559 }
560 ;
561
562 and_expression:
563 equality_expression
564 | and_expression '&' equality_expression
565 {
566 void *ctx = state;
567 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
568 $$->set_location(yylloc);
569 }
570 ;
571
572 exclusive_or_expression:
573 and_expression
574 | exclusive_or_expression '^' and_expression
575 {
576 void *ctx = state;
577 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
578 $$->set_location(yylloc);
579 }
580 ;
581
582 inclusive_or_expression:
583 exclusive_or_expression
584 | inclusive_or_expression '|' exclusive_or_expression
585 {
586 void *ctx = state;
587 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
588 $$->set_location(yylloc);
589 }
590 ;
591
592 logical_and_expression:
593 inclusive_or_expression
594 | logical_and_expression AND_OP inclusive_or_expression
595 {
596 void *ctx = state;
597 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
598 $$->set_location(yylloc);
599 }
600 ;
601
602 logical_xor_expression:
603 logical_and_expression
604 | logical_xor_expression XOR_OP logical_and_expression
605 {
606 void *ctx = state;
607 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
608 $$->set_location(yylloc);
609 }
610 ;
611
612 logical_or_expression:
613 logical_xor_expression
614 | logical_or_expression OR_OP logical_xor_expression
615 {
616 void *ctx = state;
617 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
618 $$->set_location(yylloc);
619 }
620 ;
621
622 conditional_expression:
623 logical_or_expression
624 | logical_or_expression '?' expression ':' assignment_expression
625 {
626 void *ctx = state;
627 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
628 $$->set_location(yylloc);
629 }
630 ;
631
632 assignment_expression:
633 conditional_expression
634 | unary_expression assignment_operator assignment_expression
635 {
636 void *ctx = state;
637 $$ = new(ctx) ast_expression($2, $1, $3, NULL);
638 $$->set_location(yylloc);
639 }
640 ;
641
642 assignment_operator:
643 '=' { $$ = ast_assign; }
644 | MUL_ASSIGN { $$ = ast_mul_assign; }
645 | DIV_ASSIGN { $$ = ast_div_assign; }
646 | MOD_ASSIGN { $$ = ast_mod_assign; }
647 | ADD_ASSIGN { $$ = ast_add_assign; }
648 | SUB_ASSIGN { $$ = ast_sub_assign; }
649 | LEFT_ASSIGN { $$ = ast_ls_assign; }
650 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
651 | AND_ASSIGN { $$ = ast_and_assign; }
652 | XOR_ASSIGN { $$ = ast_xor_assign; }
653 | OR_ASSIGN { $$ = ast_or_assign; }
654 ;
655
656 expression:
657 assignment_expression
658 {
659 $$ = $1;
660 }
661 | expression ',' assignment_expression
662 {
663 void *ctx = state;
664 if ($1->oper != ast_sequence) {
665 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
666 $$->set_location(yylloc);
667 $$->expressions.push_tail(& $1->link);
668 } else {
669 $$ = $1;
670 }
671
672 $$->expressions.push_tail(& $3->link);
673 }
674 ;
675
676 constant_expression:
677 conditional_expression
678 ;
679
680 declaration:
681 function_prototype ';'
682 {
683 $$ = $1;
684 }
685 | init_declarator_list ';'
686 {
687 $$ = $1;
688 }
689 | PRECISION precision_qualifier type_specifier_no_prec ';'
690 {
691 if (($3->type_specifier != ast_float)
692 && ($3->type_specifier != ast_int)) {
693 _mesa_glsl_error(& @3, state, "global precision qualifier can "
694 "only be applied to `int' or `float'\n");
695 YYERROR;
696 }
697
698 $$ = NULL; /* FINISHME */
699 }
700 ;
701
702 function_prototype:
703 function_declarator ')'
704 ;
705
706 function_declarator:
707 function_header
708 | function_header_with_parameters
709 ;
710
711 function_header_with_parameters:
712 function_header parameter_declaration
713 {
714 $$ = $1;
715 $$->parameters.push_tail(& $2->link);
716 }
717 | function_header_with_parameters ',' parameter_declaration
718 {
719 $$ = $1;
720 $$->parameters.push_tail(& $3->link);
721 }
722 ;
723
724 function_header:
725 fully_specified_type IDENTIFIER '('
726 {
727 void *ctx = state;
728 $$ = new(ctx) ast_function();
729 $$->set_location(yylloc);
730 $$->return_type = $1;
731 $$->identifier = $2;
732 }
733 ;
734
735 parameter_declarator:
736 type_specifier IDENTIFIER
737 {
738 void *ctx = state;
739 $$ = new(ctx) ast_parameter_declarator();
740 $$->set_location(yylloc);
741 $$->type = new(ctx) ast_fully_specified_type();
742 $$->type->set_location(yylloc);
743 $$->type->specifier = $1;
744 $$->identifier = $2;
745 }
746 | type_specifier IDENTIFIER '[' constant_expression ']'
747 {
748 void *ctx = state;
749 $$ = new(ctx) ast_parameter_declarator();
750 $$->set_location(yylloc);
751 $$->type = new(ctx) ast_fully_specified_type();
752 $$->type->set_location(yylloc);
753 $$->type->specifier = $1;
754 $$->identifier = $2;
755 $$->is_array = true;
756 $$->array_size = $4;
757 }
758 ;
759
760 parameter_declaration:
761 parameter_type_qualifier parameter_qualifier parameter_declarator
762 {
763 $1.i |= $2.i;
764
765 $$ = $3;
766 $$->type->qualifier = $1.q;
767 }
768 | parameter_qualifier parameter_declarator
769 {
770 $$ = $2;
771 $$->type->qualifier = $1.q;
772 }
773 | parameter_type_qualifier parameter_qualifier parameter_type_specifier
774 {
775 void *ctx = state;
776 $1.i |= $2.i;
777
778 $$ = new(ctx) ast_parameter_declarator();
779 $$->set_location(yylloc);
780 $$->type = new(ctx) ast_fully_specified_type();
781 $$->type->qualifier = $1.q;
782 $$->type->specifier = $3;
783 }
784 | parameter_qualifier parameter_type_specifier
785 {
786 void *ctx = state;
787 $$ = new(ctx) ast_parameter_declarator();
788 $$->set_location(yylloc);
789 $$->type = new(ctx) ast_fully_specified_type();
790 $$->type->qualifier = $1.q;
791 $$->type->specifier = $2;
792 }
793 ;
794
795 parameter_qualifier:
796 /* empty */
797 {
798 memset(& $$, 0, sizeof($$));
799 }
800 | IN_TOK
801 {
802 memset(& $$, 0, sizeof($$));
803 $$.q.in = 1;
804 }
805 | OUT_TOK
806 {
807 memset(& $$, 0, sizeof($$));
808 $$.q.out = 1;
809 }
810 | INOUT_TOK
811 {
812 memset(& $$, 0, sizeof($$));
813 $$.q.in = 1; $$.q.out = 1;
814 }
815 ;
816
817 parameter_type_specifier:
818 type_specifier
819 ;
820
821 init_declarator_list:
822 single_declaration
823 | init_declarator_list ',' IDENTIFIER
824 {
825 void *ctx = state;
826 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
827 decl->set_location(yylloc);
828
829 $$ = $1;
830 $$->declarations.push_tail(&decl->link);
831 }
832 | init_declarator_list ',' IDENTIFIER '[' ']'
833 {
834 void *ctx = state;
835 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
836 decl->set_location(yylloc);
837
838 $$ = $1;
839 $$->declarations.push_tail(&decl->link);
840 }
841 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']'
842 {
843 void *ctx = state;
844 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
845 decl->set_location(yylloc);
846
847 $$ = $1;
848 $$->declarations.push_tail(&decl->link);
849 }
850 | init_declarator_list ',' IDENTIFIER '[' ']' '=' initializer
851 {
852 void *ctx = state;
853 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7);
854 decl->set_location(yylloc);
855
856 $$ = $1;
857 $$->declarations.push_tail(&decl->link);
858 }
859 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']' '=' initializer
860 {
861 void *ctx = state;
862 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
863 decl->set_location(yylloc);
864
865 $$ = $1;
866 $$->declarations.push_tail(&decl->link);
867 }
868 | init_declarator_list ',' IDENTIFIER '=' initializer
869 {
870 void *ctx = state;
871 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
872 decl->set_location(yylloc);
873
874 $$ = $1;
875 $$->declarations.push_tail(&decl->link);
876 }
877 ;
878
879 // Grammar Note: No 'enum', or 'typedef'.
880 single_declaration:
881 fully_specified_type
882 {
883 void *ctx = state;
884 if ($1->specifier->type_specifier != ast_struct) {
885 _mesa_glsl_error(& @1, state, "empty declaration list\n");
886 YYERROR;
887 } else {
888 $$ = new(ctx) ast_declarator_list($1);
889 $$->set_location(yylloc);
890 }
891 }
892 | fully_specified_type IDENTIFIER
893 {
894 void *ctx = state;
895 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
896
897 $$ = new(ctx) ast_declarator_list($1);
898 $$->set_location(yylloc);
899 $$->declarations.push_tail(&decl->link);
900 }
901 | fully_specified_type IDENTIFIER '[' ']'
902 {
903 void *ctx = state;
904 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
905
906 $$ = new(ctx) ast_declarator_list($1);
907 $$->set_location(yylloc);
908 $$->declarations.push_tail(&decl->link);
909 }
910 | fully_specified_type IDENTIFIER '[' constant_expression ']'
911 {
912 void *ctx = state;
913 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
914
915 $$ = new(ctx) ast_declarator_list($1);
916 $$->set_location(yylloc);
917 $$->declarations.push_tail(&decl->link);
918 }
919 | fully_specified_type IDENTIFIER '[' ']' '=' initializer
920 {
921 void *ctx = state;
922 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6);
923
924 $$ = new(ctx) ast_declarator_list($1);
925 $$->set_location(yylloc);
926 $$->declarations.push_tail(&decl->link);
927 }
928 | fully_specified_type IDENTIFIER '[' constant_expression ']' '=' initializer
929 {
930 void *ctx = state;
931 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
932
933 $$ = new(ctx) ast_declarator_list($1);
934 $$->set_location(yylloc);
935 $$->declarations.push_tail(&decl->link);
936 }
937 | fully_specified_type IDENTIFIER '=' initializer
938 {
939 void *ctx = state;
940 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
941
942 $$ = new(ctx) ast_declarator_list($1);
943 $$->set_location(yylloc);
944 $$->declarations.push_tail(&decl->link);
945 }
946 | INVARIANT IDENTIFIER // Vertex only.
947 {
948 void *ctx = state;
949 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
950
951 $$ = new(ctx) ast_declarator_list(NULL);
952 $$->set_location(yylloc);
953 $$->invariant = true;
954
955 $$->declarations.push_tail(&decl->link);
956 }
957 ;
958
959 fully_specified_type:
960 type_specifier
961 {
962 void *ctx = state;
963 $$ = new(ctx) ast_fully_specified_type();
964 $$->set_location(yylloc);
965 $$->specifier = $1;
966 }
967 | type_qualifier type_specifier
968 {
969 void *ctx = state;
970 $$ = new(ctx) ast_fully_specified_type();
971 $$->set_location(yylloc);
972 $$->qualifier = $1.q;
973 $$->specifier = $2;
974 }
975 ;
976
977 layout_qualifier:
978 LAYOUT_TOK '(' layout_qualifier_id_list ')'
979 {
980 $$ = $3;
981 }
982 ;
983
984 layout_qualifier_id_list:
985 layout_qualifier_id
986 | layout_qualifier_id_list ',' layout_qualifier_id
987 {
988 $$.i = $1.i | $3.i;
989 }
990 ;
991
992 layout_qualifier_id:
993 IDENTIFIER
994 {
995 bool got_one = false;
996
997 memset(& $$, 0, sizeof($$));
998
999 if (state->ARB_fragment_coord_conventions_enable) {
1000 if (strcmp($1, "origin_upper_left") == 0) {
1001 got_one = true;
1002 $$.q.origin_upper_left = 1;
1003 } else if (strcmp($1, "pixel_center_integer") == 0) {
1004 got_one = true;
1005 $$.q.pixel_center_integer = 1;
1006 }
1007 }
1008
1009 /* If the identifier didn't match any known layout identifiers,
1010 * emit an error.
1011 */
1012 if (!got_one) {
1013 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1014 "`%s'\n", $1);
1015 YYERROR;
1016 } else if (state->ARB_fragment_coord_conventions_warn) {
1017 _mesa_glsl_warning(& @1, state,
1018 "GL_ARB_fragment_coord_conventions layout "
1019 "identifier `%s' used\n", $1);
1020 }
1021 }
1022 ;
1023
1024 interpolation_qualifier:
1025 SMOOTH
1026 {
1027 memset(& $$, 0, sizeof($$));
1028 $$.q.smooth = 1;
1029 }
1030 | FLAT
1031 {
1032 memset(& $$, 0, sizeof($$));
1033 $$.q.flat = 1;
1034 }
1035 | NOPERSPECTIVE
1036 {
1037 memset(& $$, 0, sizeof($$));
1038 $$.q.noperspective = 1;
1039 }
1040 ;
1041
1042 parameter_type_qualifier:
1043 CONST_TOK
1044 {
1045 memset(& $$, 0, sizeof($$));
1046 $$.q.constant = 1;
1047 }
1048 ;
1049
1050 type_qualifier:
1051 storage_qualifier
1052 | layout_qualifier
1053 | layout_qualifier storage_qualifier
1054 {
1055 $$.i = $1.i | $2.i;
1056 }
1057 | interpolation_qualifier
1058 | interpolation_qualifier storage_qualifier
1059 {
1060 $$.i = $1.i | $2.i;
1061 }
1062 | INVARIANT storage_qualifier
1063 {
1064 $$ = $2;
1065 $$.q.invariant = 1;
1066 }
1067 | INVARIANT interpolation_qualifier storage_qualifier
1068 {
1069 $$.i = $2.i | $3.i;
1070 $$.q.invariant = 1;
1071 }
1072 | INVARIANT
1073 {
1074 memset(& $$, 0, sizeof($$));
1075 $$.q.invariant = 1;
1076 }
1077 ;
1078
1079 storage_qualifier:
1080 CONST_TOK
1081 {
1082 memset(& $$, 0, sizeof($$));
1083 $$.q.constant = 1;
1084 }
1085 | ATTRIBUTE
1086 {
1087 memset(& $$, 0, sizeof($$));
1088 $$.q.attribute = 1;
1089 }
1090 | VARYING
1091 {
1092 memset(& $$, 0, sizeof($$));
1093 $$.q.varying = 1;
1094 }
1095 | CENTROID VARYING
1096 {
1097 memset(& $$, 0, sizeof($$));
1098 $$.q.centroid = 1; $$.q.varying = 1;
1099 }
1100 | IN_TOK
1101 {
1102 memset(& $$, 0, sizeof($$));
1103 $$.q.in = 1;
1104 }
1105 | OUT_TOK
1106 {
1107 memset(& $$, 0, sizeof($$));
1108 $$.q.out = 1;
1109 }
1110 | CENTROID IN_TOK
1111 {
1112 memset(& $$, 0, sizeof($$));
1113 $$.q.centroid = 1; $$.q.in = 1;
1114 }
1115 | CENTROID OUT_TOK
1116 {
1117 memset(& $$, 0, sizeof($$));
1118 $$.q.centroid = 1; $$.q.out = 1;
1119 }
1120 | UNIFORM
1121 {
1122 memset(& $$, 0, sizeof($$));
1123 $$.q.uniform = 1;
1124 }
1125 ;
1126
1127 type_specifier:
1128 type_specifier_no_prec
1129 | precision_qualifier type_specifier_no_prec
1130 {
1131 $$ = $2;
1132 $$->precision = $1;
1133 }
1134 ;
1135
1136 type_specifier_no_prec:
1137 type_specifier_nonarray
1138 | type_specifier_nonarray '[' ']'
1139 {
1140 $$ = $1;
1141 $$->is_array = true;
1142 $$->array_size = NULL;
1143 }
1144 | type_specifier_nonarray '[' constant_expression ']'
1145 {
1146 $$ = $1;
1147 $$->is_array = true;
1148 $$->array_size = $3;
1149 }
1150 ;
1151
1152 type_specifier_nonarray:
1153 basic_type_specifier_nonarray
1154 {
1155 void *ctx = state;
1156 $$ = new(ctx) ast_type_specifier($1);
1157 $$->set_location(yylloc);
1158 }
1159 | struct_specifier
1160 {
1161 void *ctx = state;
1162 $$ = new(ctx) ast_type_specifier($1);
1163 $$->set_location(yylloc);
1164 }
1165 | IDENTIFIER
1166 {
1167 void *ctx = state;
1168 $$ = new(ctx) ast_type_specifier($1);
1169 $$->set_location(yylloc);
1170 }
1171 ;
1172
1173 basic_type_specifier_nonarray:
1174 VOID_TOK { $$ = ast_void; }
1175 | FLOAT_TOK { $$ = ast_float; }
1176 | INT_TOK { $$ = ast_int; }
1177 | UINT_TOK { $$ = ast_uint; }
1178 | BOOL_TOK { $$ = ast_bool; }
1179 | VEC2 { $$ = ast_vec2; }
1180 | VEC3 { $$ = ast_vec3; }
1181 | VEC4 { $$ = ast_vec4; }
1182 | BVEC2 { $$ = ast_bvec2; }
1183 | BVEC3 { $$ = ast_bvec3; }
1184 | BVEC4 { $$ = ast_bvec4; }
1185 | IVEC2 { $$ = ast_ivec2; }
1186 | IVEC3 { $$ = ast_ivec3; }
1187 | IVEC4 { $$ = ast_ivec4; }
1188 | UVEC2 { $$ = ast_uvec2; }
1189 | UVEC3 { $$ = ast_uvec3; }
1190 | UVEC4 { $$ = ast_uvec4; }
1191 | MAT2X2 { $$ = ast_mat2; }
1192 | MAT2X3 { $$ = ast_mat2x3; }
1193 | MAT2X4 { $$ = ast_mat2x4; }
1194 | MAT3X2 { $$ = ast_mat3x2; }
1195 | MAT3X3 { $$ = ast_mat3; }
1196 | MAT3X4 { $$ = ast_mat3x4; }
1197 | MAT4X2 { $$ = ast_mat4x2; }
1198 | MAT4X3 { $$ = ast_mat4x3; }
1199 | MAT4X4 { $$ = ast_mat4; }
1200 | SAMPLER1D { $$ = ast_sampler1d; }
1201 | SAMPLER2D { $$ = ast_sampler2d; }
1202 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
1203 | SAMPLER3D { $$ = ast_sampler3d; }
1204 | SAMPLERCUBE { $$ = ast_samplercube; }
1205 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
1206 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
1207 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
1208 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
1209 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
1210 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
1211 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
1212 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1213 | ISAMPLER1D { $$ = ast_isampler1d; }
1214 | ISAMPLER2D { $$ = ast_isampler2d; }
1215 | ISAMPLER3D { $$ = ast_isampler3d; }
1216 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1217 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1218 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1219 | USAMPLER1D { $$ = ast_usampler1d; }
1220 | USAMPLER2D { $$ = ast_usampler2d; }
1221 | USAMPLER3D { $$ = ast_usampler3d; }
1222 | USAMPLERCUBE { $$ = ast_usamplercube; }
1223 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1224 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1225 ;
1226
1227 precision_qualifier:
1228 HIGHP {
1229 if (!state->es_shader && state->language_version < 130)
1230 _mesa_glsl_error(& @1, state,
1231 "precision qualifier forbidden "
1232 "in GLSL %d.%d (1.30 or later "
1233 "required)\n",
1234 state->language_version / 100,
1235 state->language_version % 100);
1236
1237 $$ = ast_precision_high;
1238 }
1239 | MEDIUMP {
1240 if (!state->es_shader && state->language_version < 130)
1241 _mesa_glsl_error(& @1, state,
1242 "precision qualifier forbidden "
1243 "in GLSL %d.%d (1.30 or later "
1244 "required)\n",
1245 state->language_version / 100,
1246 state->language_version % 100);
1247
1248 $$ = ast_precision_medium;
1249 }
1250 | LOWP {
1251 if (!state->es_shader && state->language_version < 130)
1252 _mesa_glsl_error(& @1, state,
1253 "precision qualifier forbidden "
1254 "in GLSL %d.%d (1.30 or later "
1255 "required)\n",
1256 state->language_version / 100,
1257 state->language_version % 100);
1258
1259 $$ = ast_precision_low;
1260 }
1261 ;
1262
1263 struct_specifier:
1264 STRUCT IDENTIFIER '{' struct_declaration_list '}'
1265 {
1266 void *ctx = state;
1267 $$ = new(ctx) ast_struct_specifier($2, $4);
1268 $$->set_location(yylloc);
1269 }
1270 | STRUCT '{' struct_declaration_list '}'
1271 {
1272 void *ctx = state;
1273 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1274 $$->set_location(yylloc);
1275 }
1276 ;
1277
1278 struct_declaration_list:
1279 struct_declaration
1280 {
1281 $$ = (ast_node *) $1;
1282 $1->link.self_link();
1283 }
1284 | struct_declaration_list struct_declaration
1285 {
1286 $$ = (ast_node *) $1;
1287 $$->link.insert_before(& $2->link);
1288 }
1289 ;
1290
1291 struct_declaration:
1292 type_specifier struct_declarator_list ';'
1293 {
1294 void *ctx = state;
1295 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1296 type->set_location(yylloc);
1297
1298 type->specifier = $1;
1299 $$ = new(ctx) ast_declarator_list(type);
1300 $$->set_location(yylloc);
1301
1302 $$->declarations.push_degenerate_list_at_head(& $2->link);
1303 }
1304 ;
1305
1306 struct_declarator_list:
1307 struct_declarator
1308 {
1309 $$ = $1;
1310 $1->link.self_link();
1311 }
1312 | struct_declarator_list ',' struct_declarator
1313 {
1314 $$ = $1;
1315 $$->link.insert_before(& $3->link);
1316 }
1317 ;
1318
1319 struct_declarator:
1320 IDENTIFIER
1321 {
1322 void *ctx = state;
1323 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1324 $$->set_location(yylloc);
1325 }
1326 | IDENTIFIER '[' constant_expression ']'
1327 {
1328 void *ctx = state;
1329 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1330 $$->set_location(yylloc);
1331 }
1332 ;
1333
1334 initializer:
1335 assignment_expression
1336 ;
1337
1338 declaration_statement:
1339 declaration
1340 ;
1341
1342 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1343 // supported.
1344 statement:
1345 compound_statement { $$ = (ast_node *) $1; }
1346 | simple_statement
1347 ;
1348
1349 simple_statement:
1350 declaration_statement
1351 | expression_statement
1352 | selection_statement
1353 | switch_statement { $$ = NULL; }
1354 | case_label { $$ = NULL; }
1355 | iteration_statement
1356 | jump_statement
1357 ;
1358
1359 compound_statement:
1360 '{' '}'
1361 {
1362 void *ctx = state;
1363 $$ = new(ctx) ast_compound_statement(true, NULL);
1364 $$->set_location(yylloc);
1365 }
1366 | '{' statement_list '}'
1367 {
1368 void *ctx = state;
1369 $$ = new(ctx) ast_compound_statement(true, $2);
1370 $$->set_location(yylloc);
1371 }
1372 ;
1373
1374 statement_no_new_scope:
1375 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1376 | simple_statement
1377 ;
1378
1379 compound_statement_no_new_scope:
1380 '{' '}'
1381 {
1382 void *ctx = state;
1383 $$ = new(ctx) ast_compound_statement(false, NULL);
1384 $$->set_location(yylloc);
1385 }
1386 | '{' statement_list '}'
1387 {
1388 void *ctx = state;
1389 $$ = new(ctx) ast_compound_statement(false, $2);
1390 $$->set_location(yylloc);
1391 }
1392 ;
1393
1394 statement_list:
1395 statement
1396 {
1397 if ($1 == NULL) {
1398 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1399 assert($1 != NULL);
1400 }
1401
1402 $$ = $1;
1403 $$->link.self_link();
1404 }
1405 | statement_list statement
1406 {
1407 if ($2 == NULL) {
1408 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1409 assert($2 != NULL);
1410 }
1411 $$ = $1;
1412 $$->link.insert_before(& $2->link);
1413 }
1414 ;
1415
1416 expression_statement:
1417 ';'
1418 {
1419 void *ctx = state;
1420 $$ = new(ctx) ast_expression_statement(NULL);
1421 $$->set_location(yylloc);
1422 }
1423 | expression ';'
1424 {
1425 void *ctx = state;
1426 $$ = new(ctx) ast_expression_statement($1);
1427 $$->set_location(yylloc);
1428 }
1429 ;
1430
1431 selection_statement:
1432 IF '(' expression ')' selection_rest_statement
1433 {
1434 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1435 $5.else_statement);
1436 $$->set_location(yylloc);
1437 }
1438 ;
1439
1440 selection_rest_statement:
1441 statement ELSE statement
1442 {
1443 $$.then_statement = $1;
1444 $$.else_statement = $3;
1445 }
1446 | statement
1447 {
1448 $$.then_statement = $1;
1449 $$.else_statement = NULL;
1450 }
1451 ;
1452
1453 condition:
1454 expression
1455 {
1456 $$ = (ast_node *) $1;
1457 }
1458 | fully_specified_type IDENTIFIER '=' initializer
1459 {
1460 void *ctx = state;
1461 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1462 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1463 decl->set_location(yylloc);
1464 declarator->set_location(yylloc);
1465
1466 declarator->declarations.push_tail(&decl->link);
1467 $$ = declarator;
1468 }
1469 ;
1470
1471 switch_statement:
1472 SWITCH '(' expression ')' compound_statement
1473 ;
1474
1475 case_label:
1476 CASE expression ':'
1477 | DEFAULT ':'
1478 ;
1479
1480 iteration_statement:
1481 WHILE '(' condition ')' statement_no_new_scope
1482 {
1483 void *ctx = state;
1484 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1485 NULL, $3, NULL, $5);
1486 $$->set_location(yylloc);
1487 }
1488 | DO statement WHILE '(' expression ')' ';'
1489 {
1490 void *ctx = state;
1491 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1492 NULL, $5, NULL, $2);
1493 $$->set_location(yylloc);
1494 }
1495 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1496 {
1497 void *ctx = state;
1498 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1499 $3, $4.cond, $4.rest, $6);
1500 $$->set_location(yylloc);
1501 }
1502 ;
1503
1504 for_init_statement:
1505 expression_statement
1506 | declaration_statement
1507 ;
1508
1509 conditionopt:
1510 condition
1511 | /* empty */
1512 {
1513 $$ = NULL;
1514 }
1515 ;
1516
1517 for_rest_statement:
1518 conditionopt ';'
1519 {
1520 $$.cond = $1;
1521 $$.rest = NULL;
1522 }
1523 | conditionopt ';' expression
1524 {
1525 $$.cond = $1;
1526 $$.rest = $3;
1527 }
1528 ;
1529
1530 // Grammar Note: No 'goto'. Gotos are not supported.
1531 jump_statement:
1532 CONTINUE ';'
1533 {
1534 void *ctx = state;
1535 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1536 $$->set_location(yylloc);
1537 }
1538 | BREAK ';'
1539 {
1540 void *ctx = state;
1541 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1542 $$->set_location(yylloc);
1543 }
1544 | RETURN ';'
1545 {
1546 void *ctx = state;
1547 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1548 $$->set_location(yylloc);
1549 }
1550 | RETURN expression ';'
1551 {
1552 void *ctx = state;
1553 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1554 $$->set_location(yylloc);
1555 }
1556 | DISCARD ';' // Fragment shader only.
1557 {
1558 void *ctx = state;
1559 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1560 $$->set_location(yylloc);
1561 }
1562 ;
1563
1564 external_declaration:
1565 function_definition { $$ = $1; }
1566 | declaration { $$ = $1; }
1567 | pragma_statement { $$ = NULL; }
1568 ;
1569
1570 function_definition:
1571 function_prototype compound_statement_no_new_scope
1572 {
1573 void *ctx = state;
1574 $$ = new(ctx) ast_function_definition();
1575 $$->set_location(yylloc);
1576 $$->prototype = $1;
1577 $$->body = $2;
1578 }
1579 ;