glsl2: Use bison command line option to set prefix
[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 FLOAT INT UINT
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 MAT2 MAT3 MAT4 CENTROID IN OUT INOUT 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 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 GOTO
112 %token INLINE_TOK NOINLINE VOLATILE PUBLIC_TOK STATIC EXTERN EXTERNAL
113 %token LONG SHORT DOUBLE HALF FIXED UNSIGNED INPUT 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
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 { $$.i = 0; $$.q.in = 1; }
787 | OUT { $$.i = 0; $$.q.out = 1; }
788 | INOUT { $$.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 { $$.i = 0; $$.q.in = 1; }
1034 | OUT { $$.i = 0; $$.q.out = 1; }
1035 | CENTROID IN { $$.i = 0; $$.q.centroid = 1; $$.q.in = 1; }
1036 | CENTROID OUT { $$.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 { $$ = ast_void; }
1088 | FLOAT { $$ = ast_float; }
1089 | INT { $$ = ast_int; }
1090 | UINT { $$ = ast_uint; }
1091 | BOOL { $$ = 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 | MAT2 { $$ = ast_mat2; }
1105 | MAT3 { $$ = ast_mat3; }
1106 | MAT4 { $$ = ast_mat4; }
1107 | MAT2X2 { $$ = ast_mat2; }
1108 | MAT2X3 { $$ = ast_mat2x3; }
1109 | MAT2X4 { $$ = ast_mat2x4; }
1110 | MAT3X2 { $$ = ast_mat3x2; }
1111 | MAT3X3 { $$ = ast_mat3; }
1112 | MAT3X4 { $$ = ast_mat3x4; }
1113 | MAT4X2 { $$ = ast_mat4x2; }
1114 | MAT4X3 { $$ = ast_mat4x3; }
1115 | MAT4X4 { $$ = ast_mat4; }
1116 | SAMPLER1D { $$ = ast_sampler1d; }
1117 | SAMPLER2D { $$ = ast_sampler2d; }
1118 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
1119 | SAMPLER3D { $$ = ast_sampler3d; }
1120 | SAMPLERCUBE { $$ = ast_samplercube; }
1121 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
1122 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
1123 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
1124 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
1125 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
1126 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
1127 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
1128 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1129 | ISAMPLER1D { $$ = ast_isampler1d; }
1130 | ISAMPLER2D { $$ = ast_isampler2d; }
1131 | ISAMPLER3D { $$ = ast_isampler3d; }
1132 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1133 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1134 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1135 | USAMPLER1D { $$ = ast_usampler1d; }
1136 | USAMPLER2D { $$ = ast_usampler2d; }
1137 | USAMPLER3D { $$ = ast_usampler3d; }
1138 | USAMPLERCUBE { $$ = ast_usamplercube; }
1139 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1140 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1141 ;
1142
1143 precision_qualifier:
1144 HIGHP {
1145 if (state->language_version < 130)
1146 _mesa_glsl_error(& @1, state,
1147 "precision qualifier forbidden "
1148 "in GLSL %d.%d (1.30 or later "
1149 "required)\n",
1150 state->language_version / 100,
1151 state->language_version % 100);
1152
1153 $$ = ast_precision_high;
1154 }
1155 | MEDIUMP {
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_medium;
1165 }
1166 | LOWP {
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_low;
1176 }
1177 ;
1178
1179 struct_specifier:
1180 STRUCT IDENTIFIER '{' struct_declaration_list '}'
1181 {
1182 void *ctx = state;
1183 $$ = new(ctx) ast_struct_specifier($2, $4);
1184 $$->set_location(yylloc);
1185 }
1186 | STRUCT '{' struct_declaration_list '}'
1187 {
1188 void *ctx = state;
1189 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1190 $$->set_location(yylloc);
1191 }
1192 ;
1193
1194 struct_declaration_list:
1195 struct_declaration
1196 {
1197 $$ = (struct ast_node *) $1;
1198 $1->link.self_link();
1199 }
1200 | struct_declaration_list struct_declaration
1201 {
1202 $$ = (struct ast_node *) $1;
1203 $$->link.insert_before(& $2->link);
1204 }
1205 ;
1206
1207 struct_declaration:
1208 type_specifier struct_declarator_list ';'
1209 {
1210 void *ctx = state;
1211 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1212 type->set_location(yylloc);
1213
1214 type->specifier = $1;
1215 $$ = new(ctx) ast_declarator_list(type);
1216 $$->set_location(yylloc);
1217
1218 $$->declarations.push_degenerate_list_at_head(& $2->link);
1219 }
1220 ;
1221
1222 struct_declarator_list:
1223 struct_declarator
1224 {
1225 $$ = $1;
1226 $1->link.self_link();
1227 }
1228 | struct_declarator_list ',' struct_declarator
1229 {
1230 $$ = $1;
1231 $$->link.insert_before(& $3->link);
1232 }
1233 ;
1234
1235 struct_declarator:
1236 IDENTIFIER
1237 {
1238 void *ctx = state;
1239 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1240 $$->set_location(yylloc);
1241 }
1242 | IDENTIFIER '[' constant_expression ']'
1243 {
1244 void *ctx = state;
1245 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1246 $$->set_location(yylloc);
1247 }
1248 ;
1249
1250 initializer:
1251 assignment_expression
1252 ;
1253
1254 declaration_statement:
1255 declaration
1256 ;
1257
1258 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1259 // supported.
1260 statement:
1261 statement_matched
1262 | statement_unmatched
1263 ;
1264
1265 statement_matched:
1266 compound_statement { $$ = (struct ast_node *) $1; }
1267 | simple_statement
1268 ;
1269
1270 statement_unmatched:
1271 selection_statement_unmatched
1272 ;
1273
1274 simple_statement:
1275 declaration_statement
1276 | expression_statement
1277 | selection_statement_matched
1278 | switch_statement { $$ = NULL; }
1279 | case_label { $$ = NULL; }
1280 | iteration_statement
1281 | jump_statement
1282 ;
1283
1284 compound_statement:
1285 '{' '}'
1286 {
1287 void *ctx = state;
1288 $$ = new(ctx) ast_compound_statement(true, NULL);
1289 $$->set_location(yylloc);
1290 }
1291 | '{' statement_list '}'
1292 {
1293 void *ctx = state;
1294 $$ = new(ctx) ast_compound_statement(true, $2);
1295 $$->set_location(yylloc);
1296 }
1297 ;
1298
1299 statement_no_new_scope:
1300 compound_statement_no_new_scope { $$ = (struct ast_node *) $1; }
1301 | simple_statement
1302 ;
1303
1304 compound_statement_no_new_scope:
1305 '{' '}'
1306 {
1307 void *ctx = state;
1308 $$ = new(ctx) ast_compound_statement(false, NULL);
1309 $$->set_location(yylloc);
1310 }
1311 | '{' statement_list '}'
1312 {
1313 void *ctx = state;
1314 $$ = new(ctx) ast_compound_statement(false, $2);
1315 $$->set_location(yylloc);
1316 }
1317 ;
1318
1319 statement_list:
1320 statement
1321 {
1322 if ($1 == NULL) {
1323 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1324 assert($1 != NULL);
1325 }
1326
1327 $$ = $1;
1328 $$->link.self_link();
1329 }
1330 | statement_list statement
1331 {
1332 if ($2 == NULL) {
1333 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1334 assert($2 != NULL);
1335 }
1336 $$ = $1;
1337 $$->link.insert_before(& $2->link);
1338 }
1339 ;
1340
1341 expression_statement:
1342 ';'
1343 {
1344 void *ctx = state;
1345 $$ = new(ctx) ast_expression_statement(NULL);
1346 $$->set_location(yylloc);
1347 }
1348 | expression ';'
1349 {
1350 void *ctx = state;
1351 $$ = new(ctx) ast_expression_statement($1);
1352 $$->set_location(yylloc);
1353 }
1354 ;
1355
1356 selection_statement_matched:
1357 IF '(' expression ')' statement_matched ELSE statement_matched
1358 {
1359 void *ctx = state;
1360 $$ = new(ctx) ast_selection_statement($3, $5, $7);
1361 $$->set_location(yylloc);
1362 }
1363 ;
1364
1365 selection_statement_unmatched:
1366 IF '(' expression ')' statement_matched
1367 {
1368 void *ctx = state;
1369 $$ = new(ctx) ast_selection_statement($3, $5, NULL);
1370 $$->set_location(yylloc);
1371 }
1372 | IF '(' expression ')' statement_unmatched
1373 {
1374 void *ctx = state;
1375 $$ = new(ctx) ast_selection_statement($3, $5, NULL);
1376 $$->set_location(yylloc);
1377 }
1378 | IF '(' expression ')' statement_matched ELSE statement_unmatched
1379 {
1380 void *ctx = state;
1381 $$ = new(ctx) ast_selection_statement($3, $5, $7);
1382 $$->set_location(yylloc);
1383 }
1384 ;
1385
1386 condition:
1387 expression
1388 {
1389 $$ = (struct ast_node *) $1;
1390 }
1391 | fully_specified_type IDENTIFIER '=' initializer
1392 {
1393 void *ctx = state;
1394 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1395 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1396 decl->set_location(yylloc);
1397 declarator->set_location(yylloc);
1398
1399 declarator->declarations.push_tail(&decl->link);
1400 $$ = declarator;
1401 }
1402 ;
1403
1404 switch_statement:
1405 SWITCH '(' expression ')' compound_statement
1406 ;
1407
1408 case_label:
1409 CASE expression ':'
1410 | DEFAULT ':'
1411 ;
1412
1413 iteration_statement:
1414 WHILE '(' condition ')' statement_no_new_scope
1415 {
1416 void *ctx = state;
1417 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1418 NULL, $3, NULL, $5);
1419 $$->set_location(yylloc);
1420 }
1421 | DO statement WHILE '(' expression ')' ';'
1422 {
1423 void *ctx = state;
1424 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1425 NULL, $5, NULL, $2);
1426 $$->set_location(yylloc);
1427 }
1428 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1429 {
1430 void *ctx = state;
1431 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1432 $3, $4.cond, $4.rest, $6);
1433 $$->set_location(yylloc);
1434 }
1435 ;
1436
1437 for_init_statement:
1438 expression_statement
1439 | declaration_statement
1440 ;
1441
1442 conditionopt:
1443 condition
1444 | /* empty */
1445 {
1446 $$ = NULL;
1447 }
1448 ;
1449
1450 for_rest_statement:
1451 conditionopt ';'
1452 {
1453 $$.cond = $1;
1454 $$.rest = NULL;
1455 }
1456 | conditionopt ';' expression
1457 {
1458 $$.cond = $1;
1459 $$.rest = $3;
1460 }
1461 ;
1462
1463 // Grammar Note: No 'goto'. Gotos are not supported.
1464 jump_statement:
1465 CONTINUE ';'
1466 {
1467 void *ctx = state;
1468 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1469 $$->set_location(yylloc);
1470 }
1471 | BREAK ';'
1472 {
1473 void *ctx = state;
1474 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1475 $$->set_location(yylloc);
1476 }
1477 | RETURN ';'
1478 {
1479 void *ctx = state;
1480 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1481 $$->set_location(yylloc);
1482 }
1483 | RETURN expression ';'
1484 {
1485 void *ctx = state;
1486 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1487 $$->set_location(yylloc);
1488 }
1489 | DISCARD ';' // Fragment shader only.
1490 {
1491 void *ctx = state;
1492 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1493 $$->set_location(yylloc);
1494 }
1495 ;
1496
1497 external_declaration:
1498 function_definition { $$ = $1; }
1499 | declaration { $$ = $1; }
1500 ;
1501
1502 function_definition:
1503 function_prototype compound_statement_no_new_scope
1504 {
1505 void *ctx = state;
1506 $$ = new(ctx) ast_function_definition();
1507 $$->set_location(yylloc);
1508 $$->prototype = $1;
1509 $$->body = $2;
1510 }
1511 ;