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