Merge branch 'llvm-cliptest-viewport'
[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_and, $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 if (($1.flags.i & $3.flags.i) != 0) {
987 _mesa_glsl_error(& @3, state,
988 "duplicate layout qualifiers used\n");
989 YYERROR;
990 }
991
992 $$.flags.i = $1.flags.i | $3.flags.i;
993
994 if ($1.flags.q.explicit_location)
995 $$.location = $1.location;
996
997 if ($3.flags.q.explicit_location)
998 $$.location = $3.location;
999 }
1000 ;
1001
1002 layout_qualifier_id:
1003 IDENTIFIER
1004 {
1005 bool got_one = false;
1006
1007 memset(& $$, 0, sizeof($$));
1008
1009 if (state->ARB_fragment_coord_conventions_enable) {
1010 if (strcmp($1, "origin_upper_left") == 0) {
1011 got_one = true;
1012 $$.flags.q.origin_upper_left = 1;
1013 } else if (strcmp($1, "pixel_center_integer") == 0) {
1014 got_one = true;
1015 $$.flags.q.pixel_center_integer = 1;
1016 }
1017 }
1018
1019 /* If the identifier didn't match any known layout identifiers,
1020 * emit an error.
1021 */
1022 if (!got_one) {
1023 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1024 "`%s'\n", $1);
1025 YYERROR;
1026 } else if (state->ARB_fragment_coord_conventions_warn) {
1027 _mesa_glsl_warning(& @1, state,
1028 "GL_ARB_fragment_coord_conventions layout "
1029 "identifier `%s' used\n", $1);
1030 }
1031 }
1032 | IDENTIFIER '=' INTCONSTANT
1033 {
1034 bool got_one = false;
1035
1036 memset(& $$, 0, sizeof($$));
1037
1038 if (state->ARB_explicit_attrib_location_enable) {
1039 /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1040 * FINISHME: GLSL 1.30 (or later) are supported.
1041 */
1042 if (strcmp("location", $1) == 0) {
1043 got_one = true;
1044
1045 $$.flags.q.explicit_location = 1;
1046
1047 if ($3 >= 0) {
1048 $$.location = $3;
1049 } else {
1050 _mesa_glsl_error(& @3, state,
1051 "invalid location %d specified\n", $3);
1052 YYERROR;
1053 }
1054 }
1055 }
1056
1057 /* If the identifier didn't match any known layout identifiers,
1058 * emit an error.
1059 */
1060 if (!got_one) {
1061 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1062 "`%s'\n", $1);
1063 YYERROR;
1064 } else if (state->ARB_explicit_attrib_location_warn) {
1065 _mesa_glsl_warning(& @1, state,
1066 "GL_ARB_explicit_attrib_location layout "
1067 "identifier `%s' used\n", $1);
1068 }
1069 }
1070 ;
1071
1072 interpolation_qualifier:
1073 SMOOTH
1074 {
1075 memset(& $$, 0, sizeof($$));
1076 $$.flags.q.smooth = 1;
1077 }
1078 | FLAT
1079 {
1080 memset(& $$, 0, sizeof($$));
1081 $$.flags.q.flat = 1;
1082 }
1083 | NOPERSPECTIVE
1084 {
1085 memset(& $$, 0, sizeof($$));
1086 $$.flags.q.noperspective = 1;
1087 }
1088 ;
1089
1090 parameter_type_qualifier:
1091 CONST_TOK
1092 {
1093 memset(& $$, 0, sizeof($$));
1094 $$.flags.q.constant = 1;
1095 }
1096 ;
1097
1098 type_qualifier:
1099 storage_qualifier
1100 | layout_qualifier
1101 | layout_qualifier storage_qualifier
1102 {
1103 $$ = $1;
1104 $$.flags.i |= $2.flags.i;
1105 }
1106 | interpolation_qualifier
1107 | interpolation_qualifier storage_qualifier
1108 {
1109 $$ = $1;
1110 $$.flags.i |= $2.flags.i;
1111 }
1112 | INVARIANT storage_qualifier
1113 {
1114 $$ = $2;
1115 $$.flags.q.invariant = 1;
1116 }
1117 | INVARIANT interpolation_qualifier storage_qualifier
1118 {
1119 $$ = $2;
1120 $$.flags.i |= $3.flags.i;
1121 $$.flags.q.invariant = 1;
1122 }
1123 | INVARIANT
1124 {
1125 memset(& $$, 0, sizeof($$));
1126 $$.flags.q.invariant = 1;
1127 }
1128 ;
1129
1130 storage_qualifier:
1131 CONST_TOK
1132 {
1133 memset(& $$, 0, sizeof($$));
1134 $$.flags.q.constant = 1;
1135 }
1136 | ATTRIBUTE
1137 {
1138 memset(& $$, 0, sizeof($$));
1139 $$.flags.q.attribute = 1;
1140 }
1141 | VARYING
1142 {
1143 memset(& $$, 0, sizeof($$));
1144 $$.flags.q.varying = 1;
1145 }
1146 | CENTROID VARYING
1147 {
1148 memset(& $$, 0, sizeof($$));
1149 $$.flags.q.centroid = 1;
1150 $$.flags.q.varying = 1;
1151 }
1152 | IN_TOK
1153 {
1154 memset(& $$, 0, sizeof($$));
1155 $$.flags.q.in = 1;
1156 }
1157 | OUT_TOK
1158 {
1159 memset(& $$, 0, sizeof($$));
1160 $$.flags.q.out = 1;
1161 }
1162 | CENTROID IN_TOK
1163 {
1164 memset(& $$, 0, sizeof($$));
1165 $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1166 }
1167 | CENTROID OUT_TOK
1168 {
1169 memset(& $$, 0, sizeof($$));
1170 $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1171 }
1172 | UNIFORM
1173 {
1174 memset(& $$, 0, sizeof($$));
1175 $$.flags.q.uniform = 1;
1176 }
1177 ;
1178
1179 type_specifier:
1180 type_specifier_no_prec
1181 | precision_qualifier type_specifier_no_prec
1182 {
1183 $$ = $2;
1184 $$->precision = $1;
1185 }
1186 ;
1187
1188 type_specifier_no_prec:
1189 type_specifier_nonarray
1190 | type_specifier_nonarray '[' ']'
1191 {
1192 $$ = $1;
1193 $$->is_array = true;
1194 $$->array_size = NULL;
1195 }
1196 | type_specifier_nonarray '[' constant_expression ']'
1197 {
1198 $$ = $1;
1199 $$->is_array = true;
1200 $$->array_size = $3;
1201 }
1202 ;
1203
1204 type_specifier_nonarray:
1205 basic_type_specifier_nonarray
1206 {
1207 void *ctx = state;
1208 $$ = new(ctx) ast_type_specifier($1);
1209 $$->set_location(yylloc);
1210 }
1211 | struct_specifier
1212 {
1213 void *ctx = state;
1214 $$ = new(ctx) ast_type_specifier($1);
1215 $$->set_location(yylloc);
1216 }
1217 | IDENTIFIER
1218 {
1219 void *ctx = state;
1220 $$ = new(ctx) ast_type_specifier($1);
1221 $$->set_location(yylloc);
1222 }
1223 ;
1224
1225 basic_type_specifier_nonarray:
1226 VOID_TOK { $$ = ast_void; }
1227 | FLOAT_TOK { $$ = ast_float; }
1228 | INT_TOK { $$ = ast_int; }
1229 | UINT_TOK { $$ = ast_uint; }
1230 | BOOL_TOK { $$ = ast_bool; }
1231 | VEC2 { $$ = ast_vec2; }
1232 | VEC3 { $$ = ast_vec3; }
1233 | VEC4 { $$ = ast_vec4; }
1234 | BVEC2 { $$ = ast_bvec2; }
1235 | BVEC3 { $$ = ast_bvec3; }
1236 | BVEC4 { $$ = ast_bvec4; }
1237 | IVEC2 { $$ = ast_ivec2; }
1238 | IVEC3 { $$ = ast_ivec3; }
1239 | IVEC4 { $$ = ast_ivec4; }
1240 | UVEC2 { $$ = ast_uvec2; }
1241 | UVEC3 { $$ = ast_uvec3; }
1242 | UVEC4 { $$ = ast_uvec4; }
1243 | MAT2X2 { $$ = ast_mat2; }
1244 | MAT2X3 { $$ = ast_mat2x3; }
1245 | MAT2X4 { $$ = ast_mat2x4; }
1246 | MAT3X2 { $$ = ast_mat3x2; }
1247 | MAT3X3 { $$ = ast_mat3; }
1248 | MAT3X4 { $$ = ast_mat3x4; }
1249 | MAT4X2 { $$ = ast_mat4x2; }
1250 | MAT4X3 { $$ = ast_mat4x3; }
1251 | MAT4X4 { $$ = ast_mat4; }
1252 | SAMPLER1D { $$ = ast_sampler1d; }
1253 | SAMPLER2D { $$ = ast_sampler2d; }
1254 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
1255 | SAMPLER3D { $$ = ast_sampler3d; }
1256 | SAMPLERCUBE { $$ = ast_samplercube; }
1257 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
1258 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
1259 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
1260 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
1261 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
1262 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
1263 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
1264 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1265 | ISAMPLER1D { $$ = ast_isampler1d; }
1266 | ISAMPLER2D { $$ = ast_isampler2d; }
1267 | ISAMPLER3D { $$ = ast_isampler3d; }
1268 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1269 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1270 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1271 | USAMPLER1D { $$ = ast_usampler1d; }
1272 | USAMPLER2D { $$ = ast_usampler2d; }
1273 | USAMPLER3D { $$ = ast_usampler3d; }
1274 | USAMPLERCUBE { $$ = ast_usamplercube; }
1275 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1276 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1277 ;
1278
1279 precision_qualifier:
1280 HIGHP {
1281 if (!state->es_shader && state->language_version < 130)
1282 _mesa_glsl_error(& @1, state,
1283 "precision qualifier forbidden "
1284 "in GLSL %d.%d (1.30 or later "
1285 "required)\n",
1286 state->language_version / 100,
1287 state->language_version % 100);
1288
1289 $$ = ast_precision_high;
1290 }
1291 | MEDIUMP {
1292 if (!state->es_shader && state->language_version < 130)
1293 _mesa_glsl_error(& @1, state,
1294 "precision qualifier forbidden "
1295 "in GLSL %d.%d (1.30 or later "
1296 "required)\n",
1297 state->language_version / 100,
1298 state->language_version % 100);
1299
1300 $$ = ast_precision_medium;
1301 }
1302 | LOWP {
1303 if (!state->es_shader && state->language_version < 130)
1304 _mesa_glsl_error(& @1, state,
1305 "precision qualifier forbidden "
1306 "in GLSL %d.%d (1.30 or later "
1307 "required)\n",
1308 state->language_version / 100,
1309 state->language_version % 100);
1310
1311 $$ = ast_precision_low;
1312 }
1313 ;
1314
1315 struct_specifier:
1316 STRUCT IDENTIFIER '{' struct_declaration_list '}'
1317 {
1318 void *ctx = state;
1319 $$ = new(ctx) ast_struct_specifier($2, $4);
1320 $$->set_location(yylloc);
1321 }
1322 | STRUCT '{' struct_declaration_list '}'
1323 {
1324 void *ctx = state;
1325 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1326 $$->set_location(yylloc);
1327 }
1328 ;
1329
1330 struct_declaration_list:
1331 struct_declaration
1332 {
1333 $$ = (ast_node *) $1;
1334 $1->link.self_link();
1335 }
1336 | struct_declaration_list struct_declaration
1337 {
1338 $$ = (ast_node *) $1;
1339 $$->link.insert_before(& $2->link);
1340 }
1341 ;
1342
1343 struct_declaration:
1344 type_specifier struct_declarator_list ';'
1345 {
1346 void *ctx = state;
1347 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1348 type->set_location(yylloc);
1349
1350 type->specifier = $1;
1351 $$ = new(ctx) ast_declarator_list(type);
1352 $$->set_location(yylloc);
1353
1354 $$->declarations.push_degenerate_list_at_head(& $2->link);
1355 }
1356 ;
1357
1358 struct_declarator_list:
1359 struct_declarator
1360 {
1361 $$ = $1;
1362 $1->link.self_link();
1363 }
1364 | struct_declarator_list ',' struct_declarator
1365 {
1366 $$ = $1;
1367 $$->link.insert_before(& $3->link);
1368 }
1369 ;
1370
1371 struct_declarator:
1372 IDENTIFIER
1373 {
1374 void *ctx = state;
1375 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1376 $$->set_location(yylloc);
1377 }
1378 | IDENTIFIER '[' constant_expression ']'
1379 {
1380 void *ctx = state;
1381 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1382 $$->set_location(yylloc);
1383 }
1384 ;
1385
1386 initializer:
1387 assignment_expression
1388 ;
1389
1390 declaration_statement:
1391 declaration
1392 ;
1393
1394 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1395 // supported.
1396 statement:
1397 compound_statement { $$ = (ast_node *) $1; }
1398 | simple_statement
1399 ;
1400
1401 simple_statement:
1402 declaration_statement
1403 | expression_statement
1404 | selection_statement
1405 | switch_statement { $$ = NULL; }
1406 | case_label { $$ = NULL; }
1407 | iteration_statement
1408 | jump_statement
1409 ;
1410
1411 compound_statement:
1412 '{' '}'
1413 {
1414 void *ctx = state;
1415 $$ = new(ctx) ast_compound_statement(true, NULL);
1416 $$->set_location(yylloc);
1417 }
1418 | '{' statement_list '}'
1419 {
1420 void *ctx = state;
1421 $$ = new(ctx) ast_compound_statement(true, $2);
1422 $$->set_location(yylloc);
1423 }
1424 ;
1425
1426 statement_no_new_scope:
1427 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1428 | simple_statement
1429 ;
1430
1431 compound_statement_no_new_scope:
1432 '{' '}'
1433 {
1434 void *ctx = state;
1435 $$ = new(ctx) ast_compound_statement(false, NULL);
1436 $$->set_location(yylloc);
1437 }
1438 | '{' statement_list '}'
1439 {
1440 void *ctx = state;
1441 $$ = new(ctx) ast_compound_statement(false, $2);
1442 $$->set_location(yylloc);
1443 }
1444 ;
1445
1446 statement_list:
1447 statement
1448 {
1449 if ($1 == NULL) {
1450 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1451 assert($1 != NULL);
1452 }
1453
1454 $$ = $1;
1455 $$->link.self_link();
1456 }
1457 | statement_list statement
1458 {
1459 if ($2 == NULL) {
1460 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1461 assert($2 != NULL);
1462 }
1463 $$ = $1;
1464 $$->link.insert_before(& $2->link);
1465 }
1466 ;
1467
1468 expression_statement:
1469 ';'
1470 {
1471 void *ctx = state;
1472 $$ = new(ctx) ast_expression_statement(NULL);
1473 $$->set_location(yylloc);
1474 }
1475 | expression ';'
1476 {
1477 void *ctx = state;
1478 $$ = new(ctx) ast_expression_statement($1);
1479 $$->set_location(yylloc);
1480 }
1481 ;
1482
1483 selection_statement:
1484 IF '(' expression ')' selection_rest_statement
1485 {
1486 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1487 $5.else_statement);
1488 $$->set_location(yylloc);
1489 }
1490 ;
1491
1492 selection_rest_statement:
1493 statement ELSE statement
1494 {
1495 $$.then_statement = $1;
1496 $$.else_statement = $3;
1497 }
1498 | statement
1499 {
1500 $$.then_statement = $1;
1501 $$.else_statement = NULL;
1502 }
1503 ;
1504
1505 condition:
1506 expression
1507 {
1508 $$ = (ast_node *) $1;
1509 }
1510 | fully_specified_type IDENTIFIER '=' initializer
1511 {
1512 void *ctx = state;
1513 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1514 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1515 decl->set_location(yylloc);
1516 declarator->set_location(yylloc);
1517
1518 declarator->declarations.push_tail(&decl->link);
1519 $$ = declarator;
1520 }
1521 ;
1522
1523 switch_statement:
1524 SWITCH '(' expression ')' compound_statement
1525 ;
1526
1527 case_label:
1528 CASE expression ':'
1529 | DEFAULT ':'
1530 ;
1531
1532 iteration_statement:
1533 WHILE '(' condition ')' statement_no_new_scope
1534 {
1535 void *ctx = state;
1536 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1537 NULL, $3, NULL, $5);
1538 $$->set_location(yylloc);
1539 }
1540 | DO statement WHILE '(' expression ')' ';'
1541 {
1542 void *ctx = state;
1543 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1544 NULL, $5, NULL, $2);
1545 $$->set_location(yylloc);
1546 }
1547 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1548 {
1549 void *ctx = state;
1550 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1551 $3, $4.cond, $4.rest, $6);
1552 $$->set_location(yylloc);
1553 }
1554 ;
1555
1556 for_init_statement:
1557 expression_statement
1558 | declaration_statement
1559 ;
1560
1561 conditionopt:
1562 condition
1563 | /* empty */
1564 {
1565 $$ = NULL;
1566 }
1567 ;
1568
1569 for_rest_statement:
1570 conditionopt ';'
1571 {
1572 $$.cond = $1;
1573 $$.rest = NULL;
1574 }
1575 | conditionopt ';' expression
1576 {
1577 $$.cond = $1;
1578 $$.rest = $3;
1579 }
1580 ;
1581
1582 // Grammar Note: No 'goto'. Gotos are not supported.
1583 jump_statement:
1584 CONTINUE ';'
1585 {
1586 void *ctx = state;
1587 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1588 $$->set_location(yylloc);
1589 }
1590 | BREAK ';'
1591 {
1592 void *ctx = state;
1593 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1594 $$->set_location(yylloc);
1595 }
1596 | RETURN ';'
1597 {
1598 void *ctx = state;
1599 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1600 $$->set_location(yylloc);
1601 }
1602 | RETURN expression ';'
1603 {
1604 void *ctx = state;
1605 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1606 $$->set_location(yylloc);
1607 }
1608 | DISCARD ';' // Fragment shader only.
1609 {
1610 void *ctx = state;
1611 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1612 $$->set_location(yylloc);
1613 }
1614 ;
1615
1616 external_declaration:
1617 function_definition { $$ = $1; }
1618 | declaration { $$ = $1; }
1619 | pragma_statement { $$ = NULL; }
1620 ;
1621
1622 function_definition:
1623 function_prototype compound_statement_no_new_scope
1624 {
1625 void *ctx = state;
1626 $$ = new(ctx) ast_function_definition();
1627 $$->set_location(yylloc);
1628 $$->prototype = $1;
1629 $$->body = $2;
1630 }
1631 ;