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