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