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