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