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