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