glsl: Add support for default precision statements
[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 required because pragmas are set to
280 * FINISHME: NULL. (See production rule for external_declaration.)
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 required because pragmas are set to
288 * FINISHME: NULL. (See production rule for external_declaration.)
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 $3->precision = $2;
712 $3->is_precision_statement = true;
713 $$ = $3;
714 }
715 ;
716
717 function_prototype:
718 function_declarator ')'
719 ;
720
721 function_declarator:
722 function_header
723 | function_header_with_parameters
724 ;
725
726 function_header_with_parameters:
727 function_header parameter_declaration
728 {
729 $$ = $1;
730 $$->parameters.push_tail(& $2->link);
731 }
732 | function_header_with_parameters ',' parameter_declaration
733 {
734 $$ = $1;
735 $$->parameters.push_tail(& $3->link);
736 }
737 ;
738
739 function_header:
740 fully_specified_type IDENTIFIER '('
741 {
742 void *ctx = state;
743 $$ = new(ctx) ast_function();
744 $$->set_location(yylloc);
745 $$->return_type = $1;
746 $$->identifier = $2;
747 }
748 ;
749
750 parameter_declarator:
751 type_specifier IDENTIFIER
752 {
753 void *ctx = state;
754 $$ = new(ctx) ast_parameter_declarator();
755 $$->set_location(yylloc);
756 $$->type = new(ctx) ast_fully_specified_type();
757 $$->type->set_location(yylloc);
758 $$->type->specifier = $1;
759 $$->identifier = $2;
760 }
761 | type_specifier IDENTIFIER '[' constant_expression ']'
762 {
763 void *ctx = state;
764 $$ = new(ctx) ast_parameter_declarator();
765 $$->set_location(yylloc);
766 $$->type = new(ctx) ast_fully_specified_type();
767 $$->type->set_location(yylloc);
768 $$->type->specifier = $1;
769 $$->identifier = $2;
770 $$->is_array = true;
771 $$->array_size = $4;
772 }
773 ;
774
775 parameter_declaration:
776 parameter_type_qualifier parameter_qualifier parameter_declarator
777 {
778 $1.flags.i |= $2.flags.i;
779
780 $$ = $3;
781 $$->type->qualifier = $1;
782 }
783 | parameter_qualifier parameter_declarator
784 {
785 $$ = $2;
786 $$->type->qualifier = $1;
787 }
788 | parameter_type_qualifier parameter_qualifier parameter_type_specifier
789 {
790 void *ctx = state;
791 $1.flags.i |= $2.flags.i;
792
793 $$ = new(ctx) ast_parameter_declarator();
794 $$->set_location(yylloc);
795 $$->type = new(ctx) ast_fully_specified_type();
796 $$->type->qualifier = $1;
797 $$->type->specifier = $3;
798 }
799 | parameter_qualifier parameter_type_specifier
800 {
801 void *ctx = state;
802 $$ = new(ctx) ast_parameter_declarator();
803 $$->set_location(yylloc);
804 $$->type = new(ctx) ast_fully_specified_type();
805 $$->type->qualifier = $1;
806 $$->type->specifier = $2;
807 }
808 ;
809
810 parameter_qualifier:
811 /* empty */
812 {
813 memset(& $$, 0, sizeof($$));
814 }
815 | IN_TOK
816 {
817 memset(& $$, 0, sizeof($$));
818 $$.flags.q.in = 1;
819 }
820 | OUT_TOK
821 {
822 memset(& $$, 0, sizeof($$));
823 $$.flags.q.out = 1;
824 }
825 | INOUT_TOK
826 {
827 memset(& $$, 0, sizeof($$));
828 $$.flags.q.in = 1;
829 $$.flags.q.out = 1;
830 }
831 ;
832
833 parameter_type_specifier:
834 type_specifier
835 ;
836
837 init_declarator_list:
838 single_declaration
839 | init_declarator_list ',' IDENTIFIER
840 {
841 void *ctx = state;
842 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
843 decl->set_location(yylloc);
844
845 $$ = $1;
846 $$->declarations.push_tail(&decl->link);
847 }
848 | init_declarator_list ',' IDENTIFIER '[' ']'
849 {
850 void *ctx = state;
851 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
852 decl->set_location(yylloc);
853
854 $$ = $1;
855 $$->declarations.push_tail(&decl->link);
856 }
857 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']'
858 {
859 void *ctx = state;
860 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
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, true, NULL, $7);
870 decl->set_location(yylloc);
871
872 $$ = $1;
873 $$->declarations.push_tail(&decl->link);
874 }
875 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']' '=' initializer
876 {
877 void *ctx = state;
878 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
879 decl->set_location(yylloc);
880
881 $$ = $1;
882 $$->declarations.push_tail(&decl->link);
883 }
884 | init_declarator_list ',' IDENTIFIER '=' initializer
885 {
886 void *ctx = state;
887 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
888 decl->set_location(yylloc);
889
890 $$ = $1;
891 $$->declarations.push_tail(&decl->link);
892 }
893 ;
894
895 // Grammar Note: No 'enum', or 'typedef'.
896 single_declaration:
897 fully_specified_type
898 {
899 void *ctx = state;
900 if ($1->specifier->type_specifier != ast_struct) {
901 _mesa_glsl_error(& @1, state, "empty declaration list\n");
902 YYERROR;
903 } else {
904 $$ = new(ctx) ast_declarator_list($1);
905 $$->set_location(yylloc);
906 }
907 }
908 | fully_specified_type IDENTIFIER
909 {
910 void *ctx = state;
911 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, 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 '[' ']'
918 {
919 void *ctx = state;
920 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
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 ']'
927 {
928 void *ctx = state;
929 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
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, true, NULL, $6);
939
940 $$ = new(ctx) ast_declarator_list($1);
941 $$->set_location(yylloc);
942 $$->declarations.push_tail(&decl->link);
943 }
944 | fully_specified_type IDENTIFIER '[' constant_expression ']' '=' initializer
945 {
946 void *ctx = state;
947 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
948
949 $$ = new(ctx) ast_declarator_list($1);
950 $$->set_location(yylloc);
951 $$->declarations.push_tail(&decl->link);
952 }
953 | fully_specified_type IDENTIFIER '=' initializer
954 {
955 void *ctx = state;
956 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
957
958 $$ = new(ctx) ast_declarator_list($1);
959 $$->set_location(yylloc);
960 $$->declarations.push_tail(&decl->link);
961 }
962 | INVARIANT IDENTIFIER // Vertex only.
963 {
964 void *ctx = state;
965 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
966
967 $$ = new(ctx) ast_declarator_list(NULL);
968 $$->set_location(yylloc);
969 $$->invariant = true;
970
971 $$->declarations.push_tail(&decl->link);
972 }
973 ;
974
975 fully_specified_type:
976 type_specifier
977 {
978 void *ctx = state;
979 $$ = new(ctx) ast_fully_specified_type();
980 $$->set_location(yylloc);
981 $$->specifier = $1;
982 }
983 | type_qualifier type_specifier
984 {
985 void *ctx = state;
986 $$ = new(ctx) ast_fully_specified_type();
987 $$->set_location(yylloc);
988 $$->qualifier = $1;
989 $$->specifier = $2;
990 }
991 ;
992
993 layout_qualifier:
994 LAYOUT_TOK '(' layout_qualifier_id_list ')'
995 {
996 $$ = $3;
997 }
998 ;
999
1000 layout_qualifier_id_list:
1001 layout_qualifier_id
1002 | layout_qualifier_id_list ',' layout_qualifier_id
1003 {
1004 if (($1.flags.i & $3.flags.i) != 0) {
1005 _mesa_glsl_error(& @3, state,
1006 "duplicate layout qualifiers used\n");
1007 YYERROR;
1008 }
1009
1010 $$.flags.i = $1.flags.i | $3.flags.i;
1011
1012 if ($1.flags.q.explicit_location)
1013 $$.location = $1.location;
1014
1015 if ($3.flags.q.explicit_location)
1016 $$.location = $3.location;
1017 }
1018 ;
1019
1020 layout_qualifier_id:
1021 IDENTIFIER
1022 {
1023 bool got_one = false;
1024
1025 memset(& $$, 0, sizeof($$));
1026
1027 if (state->ARB_fragment_coord_conventions_enable) {
1028 if (strcmp($1, "origin_upper_left") == 0) {
1029 got_one = true;
1030 $$.flags.q.origin_upper_left = 1;
1031 } else if (strcmp($1, "pixel_center_integer") == 0) {
1032 got_one = true;
1033 $$.flags.q.pixel_center_integer = 1;
1034 }
1035 }
1036
1037 /* If the identifier didn't match any known layout identifiers,
1038 * emit an error.
1039 */
1040 if (!got_one) {
1041 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1042 "`%s'\n", $1);
1043 YYERROR;
1044 } else if (state->ARB_fragment_coord_conventions_warn) {
1045 _mesa_glsl_warning(& @1, state,
1046 "GL_ARB_fragment_coord_conventions layout "
1047 "identifier `%s' used\n", $1);
1048 }
1049 }
1050 | IDENTIFIER '=' INTCONSTANT
1051 {
1052 bool got_one = false;
1053
1054 memset(& $$, 0, sizeof($$));
1055
1056 if (state->ARB_explicit_attrib_location_enable) {
1057 /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1058 * FINISHME: GLSL 1.30 (or later) are supported.
1059 */
1060 if (strcmp("location", $1) == 0) {
1061 got_one = true;
1062
1063 $$.flags.q.explicit_location = 1;
1064
1065 if ($3 >= 0) {
1066 $$.location = $3;
1067 } else {
1068 _mesa_glsl_error(& @3, state,
1069 "invalid location %d specified\n", $3);
1070 YYERROR;
1071 }
1072 }
1073 }
1074
1075 /* If the identifier didn't match any known layout identifiers,
1076 * emit an error.
1077 */
1078 if (!got_one) {
1079 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1080 "`%s'\n", $1);
1081 YYERROR;
1082 } else if (state->ARB_explicit_attrib_location_warn) {
1083 _mesa_glsl_warning(& @1, state,
1084 "GL_ARB_explicit_attrib_location layout "
1085 "identifier `%s' used\n", $1);
1086 }
1087 }
1088 ;
1089
1090 interpolation_qualifier:
1091 SMOOTH
1092 {
1093 memset(& $$, 0, sizeof($$));
1094 $$.flags.q.smooth = 1;
1095 }
1096 | FLAT
1097 {
1098 memset(& $$, 0, sizeof($$));
1099 $$.flags.q.flat = 1;
1100 }
1101 | NOPERSPECTIVE
1102 {
1103 memset(& $$, 0, sizeof($$));
1104 $$.flags.q.noperspective = 1;
1105 }
1106 ;
1107
1108 parameter_type_qualifier:
1109 CONST_TOK
1110 {
1111 memset(& $$, 0, sizeof($$));
1112 $$.flags.q.constant = 1;
1113 }
1114 ;
1115
1116 type_qualifier:
1117 storage_qualifier
1118 | layout_qualifier
1119 | layout_qualifier storage_qualifier
1120 {
1121 $$ = $1;
1122 $$.flags.i |= $2.flags.i;
1123 }
1124 | interpolation_qualifier
1125 | interpolation_qualifier storage_qualifier
1126 {
1127 $$ = $1;
1128 $$.flags.i |= $2.flags.i;
1129 }
1130 | INVARIANT storage_qualifier
1131 {
1132 $$ = $2;
1133 $$.flags.q.invariant = 1;
1134 }
1135 | INVARIANT interpolation_qualifier storage_qualifier
1136 {
1137 $$ = $2;
1138 $$.flags.i |= $3.flags.i;
1139 $$.flags.q.invariant = 1;
1140 }
1141 | INVARIANT
1142 {
1143 memset(& $$, 0, sizeof($$));
1144 $$.flags.q.invariant = 1;
1145 }
1146 ;
1147
1148 storage_qualifier:
1149 CONST_TOK
1150 {
1151 memset(& $$, 0, sizeof($$));
1152 $$.flags.q.constant = 1;
1153 }
1154 | ATTRIBUTE
1155 {
1156 memset(& $$, 0, sizeof($$));
1157 $$.flags.q.attribute = 1;
1158 }
1159 | VARYING
1160 {
1161 memset(& $$, 0, sizeof($$));
1162 $$.flags.q.varying = 1;
1163 }
1164 | CENTROID VARYING
1165 {
1166 memset(& $$, 0, sizeof($$));
1167 $$.flags.q.centroid = 1;
1168 $$.flags.q.varying = 1;
1169 }
1170 | IN_TOK
1171 {
1172 memset(& $$, 0, sizeof($$));
1173 $$.flags.q.in = 1;
1174 }
1175 | OUT_TOK
1176 {
1177 memset(& $$, 0, sizeof($$));
1178 $$.flags.q.out = 1;
1179 }
1180 | CENTROID IN_TOK
1181 {
1182 memset(& $$, 0, sizeof($$));
1183 $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1184 }
1185 | CENTROID OUT_TOK
1186 {
1187 memset(& $$, 0, sizeof($$));
1188 $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1189 }
1190 | UNIFORM
1191 {
1192 memset(& $$, 0, sizeof($$));
1193 $$.flags.q.uniform = 1;
1194 }
1195 ;
1196
1197 type_specifier:
1198 type_specifier_no_prec
1199 {
1200 $$ = $1;
1201 }
1202 | precision_qualifier type_specifier_no_prec
1203 {
1204 $$ = $2;
1205 $$->precision = $1;
1206 }
1207 ;
1208
1209 type_specifier_no_prec:
1210 type_specifier_nonarray
1211 | type_specifier_nonarray '[' ']'
1212 {
1213 $$ = $1;
1214 $$->is_array = true;
1215 $$->array_size = NULL;
1216 }
1217 | type_specifier_nonarray '[' constant_expression ']'
1218 {
1219 $$ = $1;
1220 $$->is_array = true;
1221 $$->array_size = $3;
1222 }
1223 ;
1224
1225 type_specifier_nonarray:
1226 basic_type_specifier_nonarray
1227 {
1228 void *ctx = state;
1229 $$ = new(ctx) ast_type_specifier($1);
1230 $$->set_location(yylloc);
1231 }
1232 | struct_specifier
1233 {
1234 void *ctx = state;
1235 $$ = new(ctx) ast_type_specifier($1);
1236 $$->set_location(yylloc);
1237 }
1238 | IDENTIFIER
1239 {
1240 void *ctx = state;
1241 $$ = new(ctx) ast_type_specifier($1);
1242 $$->set_location(yylloc);
1243 }
1244 ;
1245
1246 basic_type_specifier_nonarray:
1247 VOID_TOK { $$ = ast_void; }
1248 | FLOAT_TOK { $$ = ast_float; }
1249 | INT_TOK { $$ = ast_int; }
1250 | UINT_TOK { $$ = ast_uint; }
1251 | BOOL_TOK { $$ = ast_bool; }
1252 | VEC2 { $$ = ast_vec2; }
1253 | VEC3 { $$ = ast_vec3; }
1254 | VEC4 { $$ = ast_vec4; }
1255 | BVEC2 { $$ = ast_bvec2; }
1256 | BVEC3 { $$ = ast_bvec3; }
1257 | BVEC4 { $$ = ast_bvec4; }
1258 | IVEC2 { $$ = ast_ivec2; }
1259 | IVEC3 { $$ = ast_ivec3; }
1260 | IVEC4 { $$ = ast_ivec4; }
1261 | UVEC2 { $$ = ast_uvec2; }
1262 | UVEC3 { $$ = ast_uvec3; }
1263 | UVEC4 { $$ = ast_uvec4; }
1264 | MAT2X2 { $$ = ast_mat2; }
1265 | MAT2X3 { $$ = ast_mat2x3; }
1266 | MAT2X4 { $$ = ast_mat2x4; }
1267 | MAT3X2 { $$ = ast_mat3x2; }
1268 | MAT3X3 { $$ = ast_mat3; }
1269 | MAT3X4 { $$ = ast_mat3x4; }
1270 | MAT4X2 { $$ = ast_mat4x2; }
1271 | MAT4X3 { $$ = ast_mat4x3; }
1272 | MAT4X4 { $$ = ast_mat4; }
1273 | SAMPLER1D { $$ = ast_sampler1d; }
1274 | SAMPLER2D { $$ = ast_sampler2d; }
1275 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
1276 | SAMPLER3D { $$ = ast_sampler3d; }
1277 | SAMPLERCUBE { $$ = ast_samplercube; }
1278 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
1279 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
1280 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
1281 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
1282 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
1283 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
1284 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
1285 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1286 | ISAMPLER1D { $$ = ast_isampler1d; }
1287 | ISAMPLER2D { $$ = ast_isampler2d; }
1288 | ISAMPLER3D { $$ = ast_isampler3d; }
1289 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1290 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1291 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1292 | USAMPLER1D { $$ = ast_usampler1d; }
1293 | USAMPLER2D { $$ = ast_usampler2d; }
1294 | USAMPLER3D { $$ = ast_usampler3d; }
1295 | USAMPLERCUBE { $$ = ast_usamplercube; }
1296 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1297 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1298 ;
1299
1300 precision_qualifier:
1301 HIGHP {
1302 if (!state->es_shader && state->language_version < 130)
1303 _mesa_glsl_error(& @1, state,
1304 "precision qualifier forbidden "
1305 "in %s (1.30 or later "
1306 "required)\n",
1307 state->version_string);
1308
1309 $$ = ast_precision_high;
1310 }
1311 | MEDIUMP {
1312 if (!state->es_shader && state->language_version < 130)
1313 _mesa_glsl_error(& @1, state,
1314 "precision qualifier forbidden "
1315 "in %s (1.30 or later "
1316 "required)\n",
1317 state->version_string);
1318
1319 $$ = ast_precision_medium;
1320 }
1321 | LOWP {
1322 if (!state->es_shader && state->language_version < 130)
1323 _mesa_glsl_error(& @1, state,
1324 "precision qualifier forbidden "
1325 "in %s (1.30 or later "
1326 "required)\n",
1327 state->version_string);
1328
1329 $$ = ast_precision_low;
1330 }
1331 ;
1332
1333 struct_specifier:
1334 STRUCT IDENTIFIER '{' struct_declaration_list '}'
1335 {
1336 void *ctx = state;
1337 $$ = new(ctx) ast_struct_specifier($2, $4);
1338 $$->set_location(yylloc);
1339 }
1340 | STRUCT '{' struct_declaration_list '}'
1341 {
1342 void *ctx = state;
1343 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1344 $$->set_location(yylloc);
1345 }
1346 ;
1347
1348 struct_declaration_list:
1349 struct_declaration
1350 {
1351 $$ = (ast_node *) $1;
1352 $1->link.self_link();
1353 }
1354 | struct_declaration_list struct_declaration
1355 {
1356 $$ = (ast_node *) $1;
1357 $$->link.insert_before(& $2->link);
1358 }
1359 ;
1360
1361 struct_declaration:
1362 type_specifier struct_declarator_list ';'
1363 {
1364 void *ctx = state;
1365 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1366 type->set_location(yylloc);
1367
1368 type->specifier = $1;
1369 $$ = new(ctx) ast_declarator_list(type);
1370 $$->set_location(yylloc);
1371
1372 $$->declarations.push_degenerate_list_at_head(& $2->link);
1373 }
1374 ;
1375
1376 struct_declarator_list:
1377 struct_declarator
1378 {
1379 $$ = $1;
1380 $1->link.self_link();
1381 }
1382 | struct_declarator_list ',' struct_declarator
1383 {
1384 $$ = $1;
1385 $$->link.insert_before(& $3->link);
1386 }
1387 ;
1388
1389 struct_declarator:
1390 IDENTIFIER
1391 {
1392 void *ctx = state;
1393 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1394 $$->set_location(yylloc);
1395 }
1396 | IDENTIFIER '[' constant_expression ']'
1397 {
1398 void *ctx = state;
1399 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1400 $$->set_location(yylloc);
1401 }
1402 ;
1403
1404 initializer:
1405 assignment_expression
1406 ;
1407
1408 declaration_statement:
1409 declaration
1410 ;
1411
1412 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1413 // supported.
1414 statement:
1415 compound_statement { $$ = (ast_node *) $1; }
1416 | simple_statement
1417 ;
1418
1419 simple_statement:
1420 declaration_statement
1421 | expression_statement
1422 | selection_statement
1423 | switch_statement { $$ = NULL; }
1424 | case_label { $$ = NULL; }
1425 | iteration_statement
1426 | jump_statement
1427 ;
1428
1429 compound_statement:
1430 '{' '}'
1431 {
1432 void *ctx = state;
1433 $$ = new(ctx) ast_compound_statement(true, NULL);
1434 $$->set_location(yylloc);
1435 }
1436 | '{' statement_list '}'
1437 {
1438 void *ctx = state;
1439 $$ = new(ctx) ast_compound_statement(true, $2);
1440 $$->set_location(yylloc);
1441 }
1442 ;
1443
1444 statement_no_new_scope:
1445 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1446 | simple_statement
1447 ;
1448
1449 compound_statement_no_new_scope:
1450 '{' '}'
1451 {
1452 void *ctx = state;
1453 $$ = new(ctx) ast_compound_statement(false, NULL);
1454 $$->set_location(yylloc);
1455 }
1456 | '{' statement_list '}'
1457 {
1458 void *ctx = state;
1459 $$ = new(ctx) ast_compound_statement(false, $2);
1460 $$->set_location(yylloc);
1461 }
1462 ;
1463
1464 statement_list:
1465 statement
1466 {
1467 if ($1 == NULL) {
1468 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1469 assert($1 != NULL);
1470 }
1471
1472 $$ = $1;
1473 $$->link.self_link();
1474 }
1475 | statement_list statement
1476 {
1477 if ($2 == NULL) {
1478 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1479 assert($2 != NULL);
1480 }
1481 $$ = $1;
1482 $$->link.insert_before(& $2->link);
1483 }
1484 ;
1485
1486 expression_statement:
1487 ';'
1488 {
1489 void *ctx = state;
1490 $$ = new(ctx) ast_expression_statement(NULL);
1491 $$->set_location(yylloc);
1492 }
1493 | expression ';'
1494 {
1495 void *ctx = state;
1496 $$ = new(ctx) ast_expression_statement($1);
1497 $$->set_location(yylloc);
1498 }
1499 ;
1500
1501 selection_statement:
1502 IF '(' expression ')' selection_rest_statement
1503 {
1504 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1505 $5.else_statement);
1506 $$->set_location(yylloc);
1507 }
1508 ;
1509
1510 selection_rest_statement:
1511 statement ELSE statement
1512 {
1513 $$.then_statement = $1;
1514 $$.else_statement = $3;
1515 }
1516 | statement
1517 {
1518 $$.then_statement = $1;
1519 $$.else_statement = NULL;
1520 }
1521 ;
1522
1523 condition:
1524 expression
1525 {
1526 $$ = (ast_node *) $1;
1527 }
1528 | fully_specified_type IDENTIFIER '=' initializer
1529 {
1530 void *ctx = state;
1531 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1532 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1533 decl->set_location(yylloc);
1534 declarator->set_location(yylloc);
1535
1536 declarator->declarations.push_tail(&decl->link);
1537 $$ = declarator;
1538 }
1539 ;
1540
1541 switch_statement:
1542 SWITCH '(' expression ')' compound_statement
1543 ;
1544
1545 case_label:
1546 CASE expression ':'
1547 | DEFAULT ':'
1548 ;
1549
1550 iteration_statement:
1551 WHILE '(' condition ')' statement_no_new_scope
1552 {
1553 void *ctx = state;
1554 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1555 NULL, $3, NULL, $5);
1556 $$->set_location(yylloc);
1557 }
1558 | DO statement WHILE '(' expression ')' ';'
1559 {
1560 void *ctx = state;
1561 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1562 NULL, $5, NULL, $2);
1563 $$->set_location(yylloc);
1564 }
1565 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1566 {
1567 void *ctx = state;
1568 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1569 $3, $4.cond, $4.rest, $6);
1570 $$->set_location(yylloc);
1571 }
1572 ;
1573
1574 for_init_statement:
1575 expression_statement
1576 | declaration_statement
1577 ;
1578
1579 conditionopt:
1580 condition
1581 | /* empty */
1582 {
1583 $$ = NULL;
1584 }
1585 ;
1586
1587 for_rest_statement:
1588 conditionopt ';'
1589 {
1590 $$.cond = $1;
1591 $$.rest = NULL;
1592 }
1593 | conditionopt ';' expression
1594 {
1595 $$.cond = $1;
1596 $$.rest = $3;
1597 }
1598 ;
1599
1600 // Grammar Note: No 'goto'. Gotos are not supported.
1601 jump_statement:
1602 CONTINUE ';'
1603 {
1604 void *ctx = state;
1605 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1606 $$->set_location(yylloc);
1607 }
1608 | BREAK ';'
1609 {
1610 void *ctx = state;
1611 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1612 $$->set_location(yylloc);
1613 }
1614 | RETURN ';'
1615 {
1616 void *ctx = state;
1617 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1618 $$->set_location(yylloc);
1619 }
1620 | RETURN expression ';'
1621 {
1622 void *ctx = state;
1623 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1624 $$->set_location(yylloc);
1625 }
1626 | DISCARD ';' // Fragment shader only.
1627 {
1628 void *ctx = state;
1629 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1630 $$->set_location(yylloc);
1631 }
1632 ;
1633
1634 external_declaration:
1635 function_definition { $$ = $1; }
1636 | declaration { $$ = $1; }
1637 | pragma_statement { $$ = NULL; }
1638 ;
1639
1640 function_definition:
1641 function_prototype compound_statement_no_new_scope
1642 {
1643 void *ctx = state;
1644 $$ = new(ctx) ast_function_definition();
1645 $$->set_location(yylloc);
1646 $$->prototype = $1;
1647 $$->body = $2;
1648 }
1649 ;