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