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