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