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