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