Use yy_scan_string and stop caring about shader->SourceLen.
[mesa.git] / 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 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 NOINLINE VOLATILE PUBLIC 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 $$ = new ast_expression(ast_identifier, NULL, NULL, NULL);
259 $$->set_location(yylloc);
260 $$->primary_expression.identifier = $1;
261 }
262 | INTCONSTANT
263 {
264 $$ = new ast_expression(ast_int_constant, NULL, NULL, NULL);
265 $$->set_location(yylloc);
266 $$->primary_expression.int_constant = $1;
267 }
268 | UINTCONSTANT
269 {
270 $$ = new ast_expression(ast_uint_constant, NULL, NULL, NULL);
271 $$->set_location(yylloc);
272 $$->primary_expression.uint_constant = $1;
273 }
274 | FLOATCONSTANT
275 {
276 $$ = new ast_expression(ast_float_constant, NULL, NULL, NULL);
277 $$->set_location(yylloc);
278 $$->primary_expression.float_constant = $1;
279 }
280 | BOOLCONSTANT
281 {
282 $$ = new ast_expression(ast_bool_constant, NULL, NULL, NULL);
283 $$->set_location(yylloc);
284 $$->primary_expression.bool_constant = $1;
285 }
286 | '(' expression ')'
287 {
288 $$ = $2;
289 }
290 ;
291
292 postfix_expression:
293 primary_expression
294 | postfix_expression '[' integer_expression ']'
295 {
296 $$ = new ast_expression(ast_array_index, $1, $3, NULL);
297 $$->set_location(yylloc);
298 }
299 | function_call
300 {
301 /* Function call parameters used to be stored as a circular list in
302 * subexpressions[1]. They are now stored as a regular list in
303 * expressions. This assertion validates that the old code was
304 * correctly converted. It can eventually be removed.
305 */
306 assert($1->subexpressions[1] == NULL);
307 $$ = $1;
308 }
309 | postfix_expression '.' IDENTIFIER
310 {
311 $$ = new ast_expression(ast_field_selection, $1, NULL, NULL);
312 $$->set_location(yylloc);
313 $$->primary_expression.identifier = $3;
314 }
315 | postfix_expression INC_OP
316 {
317 $$ = new ast_expression(ast_post_inc, $1, NULL, NULL);
318 $$->set_location(yylloc);
319 }
320 | postfix_expression DEC_OP
321 {
322 $$ = new ast_expression(ast_post_dec, $1, NULL, NULL);
323 $$->set_location(yylloc);
324 }
325 ;
326
327 integer_expression:
328 expression
329 ;
330
331 function_call:
332 function_call_or_method
333 ;
334
335 function_call_or_method:
336 function_call_generic
337 | postfix_expression '.' function_call_generic
338 {
339 $$ = new ast_expression(ast_field_selection, $1, $3, NULL);
340 $$->set_location(yylloc);
341 }
342 ;
343
344 function_call_generic:
345 function_call_header_with_parameters ')'
346 | function_call_header_no_parameters ')'
347 ;
348
349 function_call_header_no_parameters:
350 function_call_header VOID
351 | function_call_header
352 ;
353
354 function_call_header_with_parameters:
355 function_call_header assignment_expression
356 {
357 $$ = $1;
358 $$->set_location(yylloc);
359 $$->expressions.push_tail(& $2->link);
360 }
361 | function_call_header_with_parameters ',' assignment_expression
362 {
363 $$ = $1;
364 $$->set_location(yylloc);
365 $$->expressions.push_tail(& $3->link);
366 }
367 ;
368
369 // Grammar Note: Constructors look like functions, but lexical
370 // analysis recognized most of them as keywords. They are now
371 // recognized through "type_specifier".
372 function_call_header:
373 function_identifier '('
374 ;
375
376 function_identifier:
377 type_specifier
378 {
379 $$ = new ast_function_expression($1);
380 $$->set_location(yylloc);
381 }
382 | IDENTIFIER
383 {
384 ast_expression *callee = new ast_expression($1);
385 $$ = new ast_function_expression(callee);
386 $$->set_location(yylloc);
387 }
388 | FIELD_SELECTION
389 {
390 ast_expression *callee = new ast_expression($1);
391 $$ = new ast_function_expression(callee);
392 $$->set_location(yylloc);
393 }
394 ;
395
396 // Grammar Note: No traditional style type casts.
397 unary_expression:
398 postfix_expression
399 | INC_OP unary_expression
400 {
401 $$ = new ast_expression(ast_pre_inc, $2, NULL, NULL);
402 $$->set_location(yylloc);
403 }
404 | DEC_OP unary_expression
405 {
406 $$ = new ast_expression(ast_pre_dec, $2, NULL, NULL);
407 $$->set_location(yylloc);
408 }
409 | unary_operator unary_expression
410 {
411 $$ = new ast_expression($1, $2, NULL, NULL);
412 $$->set_location(yylloc);
413 }
414 ;
415
416 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
417 unary_operator:
418 '+' { $$ = ast_plus; }
419 | '-' { $$ = ast_neg; }
420 | '!' { $$ = ast_logic_not; }
421 | '~' { $$ = ast_bit_not; }
422 ;
423
424 multiplicative_expression:
425 unary_expression
426 | multiplicative_expression '*' unary_expression
427 {
428 $$ = new ast_expression_bin(ast_mul, $1, $3);
429 $$->set_location(yylloc);
430 }
431 | multiplicative_expression '/' unary_expression
432 {
433 $$ = new ast_expression_bin(ast_div, $1, $3);
434 $$->set_location(yylloc);
435 }
436 | multiplicative_expression '%' unary_expression
437 {
438 $$ = new ast_expression_bin(ast_mod, $1, $3);
439 $$->set_location(yylloc);
440 }
441 ;
442
443 additive_expression:
444 multiplicative_expression
445 | additive_expression '+' multiplicative_expression
446 {
447 $$ = new ast_expression_bin(ast_add, $1, $3);
448 $$->set_location(yylloc);
449 }
450 | additive_expression '-' multiplicative_expression
451 {
452 $$ = new ast_expression_bin(ast_sub, $1, $3);
453 $$->set_location(yylloc);
454 }
455 ;
456
457 shift_expression:
458 additive_expression
459 | shift_expression LEFT_OP additive_expression
460 {
461 $$ = new ast_expression_bin(ast_lshift, $1, $3);
462 $$->set_location(yylloc);
463 }
464 | shift_expression RIGHT_OP additive_expression
465 {
466 $$ = new ast_expression_bin(ast_rshift, $1, $3);
467 $$->set_location(yylloc);
468 }
469 ;
470
471 relational_expression:
472 shift_expression
473 | relational_expression '<' shift_expression
474 {
475 $$ = new ast_expression_bin(ast_less, $1, $3);
476 $$->set_location(yylloc);
477 }
478 | relational_expression '>' shift_expression
479 {
480 $$ = new ast_expression_bin(ast_greater, $1, $3);
481 $$->set_location(yylloc);
482 }
483 | relational_expression LE_OP shift_expression
484 {
485 $$ = new ast_expression_bin(ast_lequal, $1, $3);
486 $$->set_location(yylloc);
487 }
488 | relational_expression GE_OP shift_expression
489 {
490 $$ = new ast_expression_bin(ast_gequal, $1, $3);
491 $$->set_location(yylloc);
492 }
493 ;
494
495 equality_expression:
496 relational_expression
497 | equality_expression EQ_OP relational_expression
498 {
499 $$ = new ast_expression_bin(ast_equal, $1, $3);
500 $$->set_location(yylloc);
501 }
502 | equality_expression NE_OP relational_expression
503 {
504 $$ = new ast_expression_bin(ast_nequal, $1, $3);
505 $$->set_location(yylloc);
506 }
507 ;
508
509 and_expression:
510 equality_expression
511 | and_expression '&' equality_expression
512 {
513 $$ = new ast_expression_bin(ast_bit_or, $1, $3);
514 $$->set_location(yylloc);
515 }
516 ;
517
518 exclusive_or_expression:
519 and_expression
520 | exclusive_or_expression '^' and_expression
521 {
522 $$ = new ast_expression_bin(ast_bit_xor, $1, $3);
523 $$->set_location(yylloc);
524 }
525 ;
526
527 inclusive_or_expression:
528 exclusive_or_expression
529 | inclusive_or_expression '|' exclusive_or_expression
530 {
531 $$ = new ast_expression_bin(ast_bit_or, $1, $3);
532 $$->set_location(yylloc);
533 }
534 ;
535
536 logical_and_expression:
537 inclusive_or_expression
538 | logical_and_expression AND_OP inclusive_or_expression
539 {
540 $$ = new ast_expression_bin(ast_logic_and, $1, $3);
541 $$->set_location(yylloc);
542 }
543 ;
544
545 logical_xor_expression:
546 logical_and_expression
547 | logical_xor_expression XOR_OP logical_and_expression
548 {
549 $$ = new ast_expression_bin(ast_logic_xor, $1, $3);
550 $$->set_location(yylloc);
551 }
552 ;
553
554 logical_or_expression:
555 logical_xor_expression
556 | logical_or_expression OR_OP logical_xor_expression
557 {
558 $$ = new ast_expression_bin(ast_logic_or, $1, $3);
559 $$->set_location(yylloc);
560 }
561 ;
562
563 conditional_expression:
564 logical_or_expression
565 | logical_or_expression '?' expression ':' assignment_expression
566 {
567 $$ = new ast_expression(ast_conditional, $1, $3, $5);
568 $$->set_location(yylloc);
569 }
570 ;
571
572 assignment_expression:
573 conditional_expression
574 | unary_expression assignment_operator assignment_expression
575 {
576 $$ = new ast_expression($2, $1, $3, NULL);
577 $$->set_location(yylloc);
578 }
579 ;
580
581 assignment_operator:
582 '=' { $$ = ast_assign; }
583 | MUL_ASSIGN { $$ = ast_mul_assign; }
584 | DIV_ASSIGN { $$ = ast_div_assign; }
585 | MOD_ASSIGN { $$ = ast_mod_assign; }
586 | ADD_ASSIGN { $$ = ast_add_assign; }
587 | SUB_ASSIGN { $$ = ast_sub_assign; }
588 | LEFT_ASSIGN { $$ = ast_ls_assign; }
589 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
590 | AND_ASSIGN { $$ = ast_and_assign; }
591 | XOR_ASSIGN { $$ = ast_xor_assign; }
592 | OR_ASSIGN { $$ = ast_or_assign; }
593 ;
594
595 expression:
596 assignment_expression
597 {
598 $$ = $1;
599 }
600 | expression ',' assignment_expression
601 {
602 if ($1->oper != ast_sequence) {
603 $$ = new ast_expression(ast_sequence, NULL, NULL, NULL);
604 $$->set_location(yylloc);
605 $$->expressions.push_tail(& $1->link);
606 } else {
607 $$ = $1;
608 }
609
610 $$->expressions.push_tail(& $3->link);
611 }
612 ;
613
614 constant_expression:
615 conditional_expression
616 ;
617
618 declaration:
619 function_prototype ';'
620 {
621 $$ = $1;
622 }
623 | init_declarator_list ';'
624 {
625 $$ = $1;
626 }
627 | PRECISION precision_qualifier type_specifier_no_prec ';'
628 {
629 if (($3->type_specifier != ast_float)
630 && ($3->type_specifier != ast_int)) {
631 _mesa_glsl_error(& @3, state, "global precision qualifier can "
632 "only be applied to `int' or `float'\n");
633 YYERROR;
634 }
635
636 $$ = NULL; /* FINISHME */
637 }
638 ;
639
640 function_prototype:
641 function_declarator ')'
642 ;
643
644 function_declarator:
645 function_header
646 | function_header_with_parameters
647 ;
648
649 function_header_with_parameters:
650 function_header parameter_declaration
651 {
652 $$ = $1;
653 $$->parameters.push_tail(& $2->link);
654 }
655 | function_header_with_parameters ',' parameter_declaration
656 {
657 $$ = $1;
658 $$->parameters.push_tail(& $3->link);
659 }
660 ;
661
662 function_header:
663 fully_specified_type IDENTIFIER '('
664 {
665 $$ = new ast_function();
666 $$->set_location(yylloc);
667 $$->return_type = $1;
668 $$->identifier = $2;
669 }
670 ;
671
672 parameter_declarator:
673 type_specifier IDENTIFIER
674 {
675 $$ = new ast_parameter_declarator();
676 $$->set_location(yylloc);
677 $$->type = new ast_fully_specified_type();
678 $$->type->set_location(yylloc);
679 $$->type->specifier = $1;
680 $$->identifier = $2;
681 }
682 | type_specifier IDENTIFIER '[' constant_expression ']'
683 {
684 $$ = new ast_parameter_declarator();
685 $$->set_location(yylloc);
686 $$->type = new ast_fully_specified_type();
687 $$->type->set_location(yylloc);
688 $$->type->specifier = $1;
689 $$->identifier = $2;
690 $$->is_array = true;
691 $$->array_size = $4;
692 }
693 ;
694
695 parameter_declaration:
696 parameter_type_qualifier parameter_qualifier parameter_declarator
697 {
698 $1.i |= $2.i;
699
700 $$ = $3;
701 $$->type->qualifier = $1.q;
702 }
703 | parameter_qualifier parameter_declarator
704 {
705 $$ = $2;
706 $$->type->qualifier = $1.q;
707 }
708 | parameter_type_qualifier parameter_qualifier parameter_type_specifier
709 {
710 $1.i |= $2.i;
711
712 $$ = new ast_parameter_declarator();
713 $$->set_location(yylloc);
714 $$->type = new ast_fully_specified_type();
715 $$->type->qualifier = $1.q;
716 $$->type->specifier = $3;
717 }
718 | parameter_qualifier parameter_type_specifier
719 {
720 $$ = new ast_parameter_declarator();
721 $$->set_location(yylloc);
722 $$->type = new ast_fully_specified_type();
723 $$->type->qualifier = $1.q;
724 $$->type->specifier = $2;
725 }
726 ;
727
728 parameter_qualifier:
729 /* empty */ { $$.i = 0; }
730 | IN { $$.i = 0; $$.q.in = 1; }
731 | OUT { $$.i = 0; $$.q.out = 1; }
732 | INOUT { $$.i = 0; $$.q.in = 1; $$.q.out = 1; }
733 ;
734
735 parameter_type_specifier:
736 type_specifier
737 ;
738
739 init_declarator_list:
740 single_declaration
741 | init_declarator_list ',' IDENTIFIER
742 {
743 ast_declaration *decl = new ast_declaration($3, false, NULL, NULL);
744 decl->set_location(yylloc);
745
746 $$ = $1;
747 $$->declarations.push_tail(&decl->link);
748 }
749 | init_declarator_list ',' IDENTIFIER '[' ']'
750 {
751 ast_declaration *decl = new ast_declaration($3, true, NULL, NULL);
752 decl->set_location(yylloc);
753
754 $$ = $1;
755 $$->declarations.push_tail(&decl->link);
756 }
757 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']'
758 {
759 ast_declaration *decl = new ast_declaration($3, true, $5, NULL);
760 decl->set_location(yylloc);
761
762 $$ = $1;
763 $$->declarations.push_tail(&decl->link);
764 }
765 | init_declarator_list ',' IDENTIFIER '[' ']' '=' initializer
766 {
767 ast_declaration *decl = new ast_declaration($3, true, NULL, $7);
768 decl->set_location(yylloc);
769
770 $$ = $1;
771 $$->declarations.push_tail(&decl->link);
772 }
773 | init_declarator_list ',' IDENTIFIER '[' constant_expression ']' '=' initializer
774 {
775 ast_declaration *decl = new ast_declaration($3, true, $5, $8);
776 decl->set_location(yylloc);
777
778 $$ = $1;
779 $$->declarations.push_tail(&decl->link);
780 }
781 | init_declarator_list ',' IDENTIFIER '=' initializer
782 {
783 ast_declaration *decl = new ast_declaration($3, false, NULL, $5);
784 decl->set_location(yylloc);
785
786 $$ = $1;
787 $$->declarations.push_tail(&decl->link);
788 }
789 ;
790
791 // Grammar Note: No 'enum', or 'typedef'.
792 single_declaration:
793 fully_specified_type
794 {
795 if ($1->specifier->type_specifier != ast_struct) {
796 _mesa_glsl_error(& @1, state, "empty declaration list\n");
797 YYERROR;
798 } else {
799 $$ = new ast_declarator_list($1);
800 $$->set_location(yylloc);
801 }
802 }
803 | fully_specified_type IDENTIFIER
804 {
805 ast_declaration *decl = new ast_declaration($2, false, NULL, NULL);
806
807 $$ = new ast_declarator_list($1);
808 $$->set_location(yylloc);
809 $$->declarations.push_tail(&decl->link);
810 }
811 | fully_specified_type IDENTIFIER '[' ']'
812 {
813 ast_declaration *decl = new ast_declaration($2, true, NULL, NULL);
814
815 $$ = new ast_declarator_list($1);
816 $$->set_location(yylloc);
817 $$->declarations.push_tail(&decl->link);
818 }
819 | fully_specified_type IDENTIFIER '[' constant_expression ']'
820 {
821 ast_declaration *decl = new ast_declaration($2, true, $4, NULL);
822
823 $$ = new ast_declarator_list($1);
824 $$->set_location(yylloc);
825 $$->declarations.push_tail(&decl->link);
826 }
827 | fully_specified_type IDENTIFIER '[' ']' '=' initializer
828 {
829 ast_declaration *decl = new ast_declaration($2, true, NULL, $6);
830
831 $$ = new ast_declarator_list($1);
832 $$->set_location(yylloc);
833 $$->declarations.push_tail(&decl->link);
834 }
835 | fully_specified_type IDENTIFIER '[' constant_expression ']' '=' initializer
836 {
837 ast_declaration *decl = new ast_declaration($2, true, $4, $7);
838
839 $$ = new ast_declarator_list($1);
840 $$->set_location(yylloc);
841 $$->declarations.push_tail(&decl->link);
842 }
843 | fully_specified_type IDENTIFIER '=' initializer
844 {
845 ast_declaration *decl = new ast_declaration($2, false, NULL, $4);
846
847 $$ = new ast_declarator_list($1);
848 $$->set_location(yylloc);
849 $$->declarations.push_tail(&decl->link);
850 }
851 | INVARIANT IDENTIFIER // Vertex only.
852 {
853 ast_declaration *decl = new ast_declaration($2, false, NULL, NULL);
854
855 $$ = new ast_declarator_list(NULL);
856 $$->set_location(yylloc);
857 $$->invariant = true;
858
859 $$->declarations.push_tail(&decl->link);
860 }
861 ;
862
863 fully_specified_type:
864 type_specifier
865 {
866 $$ = new ast_fully_specified_type();
867 $$->set_location(yylloc);
868 $$->specifier = $1;
869 }
870 | type_qualifier type_specifier
871 {
872 $$ = new ast_fully_specified_type();
873 $$->set_location(yylloc);
874 $$->qualifier = $1.q;
875 $$->specifier = $2;
876 }
877 ;
878
879 interpolation_qualifier:
880 SMOOTH { $$.i = 0; $$.q.smooth = 1; }
881 | FLAT { $$.i = 0; $$.q.flat = 1; }
882 | NOPERSPECTIVE { $$.i = 0; $$.q.noperspective = 1; }
883 ;
884
885 parameter_type_qualifier:
886 CONST { $$.i = 0; $$.q.constant = 1; }
887 ;
888
889 type_qualifier:
890 storage_qualifier
891 | interpolation_qualifier type_qualifier
892 {
893 $$.i = $1.i | $2.i;
894 }
895 | INVARIANT type_qualifier
896 {
897 $$ = $2;
898 $$.q.invariant = 1;
899 }
900 ;
901
902 storage_qualifier:
903 CONST { $$.i = 0; $$.q.constant = 1; }
904 | ATTRIBUTE { $$.i = 0; $$.q.attribute = 1; }
905 | VARYING { $$.i = 0; $$.q.varying = 1; }
906 | CENTROID VARYING { $$.i = 0; $$.q.centroid = 1; $$.q.varying = 1; }
907 | IN { $$.i = 0; $$.q.in = 1; }
908 | OUT { $$.i = 0; $$.q.out = 1; }
909 | CENTROID IN { $$.i = 0; $$.q.centroid = 1; $$.q.in = 1; }
910 | CENTROID OUT { $$.i = 0; $$.q.centroid = 1; $$.q.out = 1; }
911 | UNIFORM { $$.i = 0; $$.q.uniform = 1; }
912 ;
913
914 type_specifier:
915 type_specifier_no_prec
916 | precision_qualifier type_specifier_no_prec
917 {
918 $$ = $2;
919 $$->precision = $1;
920 }
921 ;
922
923 type_specifier_no_prec:
924 type_specifier_nonarray
925 | type_specifier_nonarray '[' ']'
926 {
927 $$ = $1;
928 $$->is_array = true;
929 $$->array_size = NULL;
930 }
931 | type_specifier_nonarray '[' constant_expression ']'
932 {
933 $$ = $1;
934 $$->is_array = true;
935 $$->array_size = $3;
936 }
937 ;
938
939 type_specifier_nonarray:
940 basic_type_specifier_nonarray
941 {
942 $$ = new ast_type_specifier($1);
943 $$->set_location(yylloc);
944 }
945 | struct_specifier
946 {
947 $$ = new ast_type_specifier($1);
948 $$->set_location(yylloc);
949 }
950 | IDENTIFIER
951 {
952 $$ = new ast_type_specifier($1);
953 $$->set_location(yylloc);
954 }
955 ;
956
957 basic_type_specifier_nonarray:
958 VOID { $$ = ast_void; }
959 | FLOAT { $$ = ast_float; }
960 | INT { $$ = ast_int; }
961 | UINT { $$ = ast_uint; }
962 | BOOL { $$ = ast_bool; }
963 | VEC2 { $$ = ast_vec2; }
964 | VEC3 { $$ = ast_vec3; }
965 | VEC4 { $$ = ast_vec4; }
966 | BVEC2 { $$ = ast_bvec2; }
967 | BVEC3 { $$ = ast_bvec3; }
968 | BVEC4 { $$ = ast_bvec4; }
969 | IVEC2 { $$ = ast_ivec2; }
970 | IVEC3 { $$ = ast_ivec3; }
971 | IVEC4 { $$ = ast_ivec4; }
972 | UVEC2 { $$ = ast_uvec2; }
973 | UVEC3 { $$ = ast_uvec3; }
974 | UVEC4 { $$ = ast_uvec4; }
975 | MAT2 { $$ = ast_mat2; }
976 | MAT3 { $$ = ast_mat3; }
977 | MAT4 { $$ = ast_mat4; }
978 | MAT2X2 { $$ = ast_mat2; }
979 | MAT2X3 { $$ = ast_mat2x3; }
980 | MAT2X4 { $$ = ast_mat2x4; }
981 | MAT3X2 { $$ = ast_mat3x2; }
982 | MAT3X3 { $$ = ast_mat3; }
983 | MAT3X4 { $$ = ast_mat3x4; }
984 | MAT4X2 { $$ = ast_mat4x2; }
985 | MAT4X3 { $$ = ast_mat4x3; }
986 | MAT4X4 { $$ = ast_mat4; }
987 | SAMPLER1D { $$ = ast_sampler1d; }
988 | SAMPLER2D { $$ = ast_sampler2d; }
989 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
990 | SAMPLER3D { $$ = ast_sampler3d; }
991 | SAMPLERCUBE { $$ = ast_samplercube; }
992 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
993 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
994 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
995 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
996 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
997 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
998 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
999 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1000 | ISAMPLER1D { $$ = ast_isampler1d; }
1001 | ISAMPLER2D { $$ = ast_isampler2d; }
1002 | ISAMPLER3D { $$ = ast_isampler3d; }
1003 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1004 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1005 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1006 | USAMPLER1D { $$ = ast_usampler1d; }
1007 | USAMPLER2D { $$ = ast_usampler2d; }
1008 | USAMPLER3D { $$ = ast_usampler3d; }
1009 | USAMPLERCUBE { $$ = ast_usamplercube; }
1010 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1011 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1012 ;
1013
1014 precision_qualifier:
1015 HIGHP {
1016 if (state->language_version < 130)
1017 _mesa_glsl_error(& @1, state,
1018 "precission qualifier forbidden "
1019 "in GLSL %d.%d (1.30 or later "
1020 "required)\n",
1021 state->language_version / 100,
1022 state->language_version % 100);
1023
1024 $$ = ast_precision_high;
1025 }
1026 | MEDIUMP {
1027 if (state->language_version < 130)
1028 _mesa_glsl_error(& @1, state,
1029 "precission qualifier forbidden "
1030 "in GLSL %d.%d (1.30 or later "
1031 "required)\n",
1032 state->language_version / 100,
1033 state->language_version % 100);
1034
1035 $$ = ast_precision_medium;
1036 }
1037 | LOWP {
1038 if (state->language_version < 130)
1039 _mesa_glsl_error(& @1, state,
1040 "precission qualifier forbidden "
1041 "in GLSL %d.%d (1.30 or later "
1042 "required)\n",
1043 state->language_version / 100,
1044 state->language_version % 100);
1045
1046 $$ = ast_precision_low;
1047 }
1048 ;
1049
1050 struct_specifier:
1051 STRUCT IDENTIFIER '{' struct_declaration_list '}'
1052 {
1053 $$ = new ast_struct_specifier($2, $4);
1054 $$->set_location(yylloc);
1055 }
1056 | STRUCT '{' struct_declaration_list '}'
1057 {
1058 $$ = new ast_struct_specifier(NULL, $3);
1059 $$->set_location(yylloc);
1060 }
1061 ;
1062
1063 struct_declaration_list:
1064 struct_declaration
1065 {
1066 $$ = (struct ast_node *) $1;
1067 $1->link.self_link();
1068 }
1069 | struct_declaration_list struct_declaration
1070 {
1071 $$ = (struct ast_node *) $1;
1072 $$->link.insert_before(& $2->link);
1073 }
1074 ;
1075
1076 struct_declaration:
1077 type_specifier struct_declarator_list ';'
1078 {
1079 ast_fully_specified_type *type = new ast_fully_specified_type();
1080 type->set_location(yylloc);
1081
1082 type->specifier = $1;
1083 $$ = new ast_declarator_list(type);
1084 $$->set_location(yylloc);
1085
1086 $$->declarations.push_degenerate_list_at_head(& $2->link);
1087 }
1088 ;
1089
1090 struct_declarator_list:
1091 struct_declarator
1092 {
1093 $$ = $1;
1094 $1->link.self_link();
1095 }
1096 | struct_declarator_list ',' struct_declarator
1097 {
1098 $$ = $1;
1099 $$->link.insert_before(& $3->link);
1100 }
1101 ;
1102
1103 struct_declarator:
1104 IDENTIFIER
1105 {
1106 $$ = new ast_declaration($1, false, NULL, NULL);
1107 $$->set_location(yylloc);
1108 }
1109 | IDENTIFIER '[' constant_expression ']'
1110 {
1111 $$ = new ast_declaration($1, true, $3, NULL);
1112 $$->set_location(yylloc);
1113 }
1114 ;
1115
1116 initializer:
1117 assignment_expression
1118 ;
1119
1120 declaration_statement:
1121 declaration
1122 ;
1123
1124 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1125 // supported.
1126 statement:
1127 statement_matched
1128 | statement_unmatched
1129 ;
1130
1131 statement_matched:
1132 compound_statement { $$ = (struct ast_node *) $1; }
1133 | simple_statement
1134 ;
1135
1136 statement_unmatched:
1137 selection_statement_unmatched
1138 ;
1139
1140 simple_statement:
1141 declaration_statement
1142 | expression_statement
1143 | selection_statement_matched
1144 | switch_statement { $$ = NULL; }
1145 | case_label { $$ = NULL; }
1146 | iteration_statement
1147 | jump_statement
1148 ;
1149
1150 compound_statement:
1151 '{' '}'
1152 {
1153 $$ = new ast_compound_statement(true, NULL);
1154 $$->set_location(yylloc);
1155 }
1156 | '{' statement_list '}'
1157 {
1158 $$ = new ast_compound_statement(true, $2);
1159 $$->set_location(yylloc);
1160 }
1161 ;
1162
1163 statement_no_new_scope:
1164 compound_statement_no_new_scope { $$ = (struct ast_node *) $1; }
1165 | simple_statement
1166 ;
1167
1168 compound_statement_no_new_scope:
1169 '{' '}'
1170 {
1171 $$ = new ast_compound_statement(false, NULL);
1172 $$->set_location(yylloc);
1173 }
1174 | '{' statement_list '}'
1175 {
1176 $$ = new ast_compound_statement(false, $2);
1177 $$->set_location(yylloc);
1178 }
1179 ;
1180
1181 statement_list:
1182 statement
1183 {
1184 if ($1 == NULL) {
1185 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1186 assert($1 != NULL);
1187 }
1188
1189 $$ = $1;
1190 $$->link.self_link();
1191 }
1192 | statement_list statement
1193 {
1194 if ($2 == NULL) {
1195 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1196 assert($2 != NULL);
1197 }
1198 $$ = $1;
1199 $$->link.insert_before(& $2->link);
1200 }
1201 ;
1202
1203 expression_statement:
1204 ';'
1205 {
1206 $$ = new ast_expression_statement(NULL);
1207 $$->set_location(yylloc);
1208 }
1209 | expression ';'
1210 {
1211 $$ = new ast_expression_statement($1);
1212 $$->set_location(yylloc);
1213 }
1214 ;
1215
1216 selection_statement_matched:
1217 IF '(' expression ')' statement_matched ELSE statement_matched
1218 {
1219 $$ = new ast_selection_statement($3, $5, $7);
1220 $$->set_location(yylloc);
1221 }
1222 ;
1223
1224 selection_statement_unmatched:
1225 IF '(' expression ')' statement_matched
1226 {
1227 $$ = new ast_selection_statement($3, $5, NULL);
1228 $$->set_location(yylloc);
1229 }
1230 | IF '(' expression ')' statement_unmatched
1231 {
1232 $$ = new ast_selection_statement($3, $5, NULL);
1233 $$->set_location(yylloc);
1234 }
1235 | IF '(' expression ')' statement_matched ELSE statement_unmatched
1236 {
1237 $$ = new ast_selection_statement($3, $5, $7);
1238 $$->set_location(yylloc);
1239 }
1240 ;
1241
1242 condition:
1243 expression
1244 {
1245 $$ = (struct ast_node *) $1;
1246 }
1247 | fully_specified_type IDENTIFIER '=' initializer
1248 {
1249 ast_declaration *decl = new ast_declaration($2, false, NULL, $4);
1250 ast_declarator_list *declarator = new ast_declarator_list($1);
1251 decl->set_location(yylloc);
1252 declarator->set_location(yylloc);
1253
1254 declarator->declarations.push_tail(&decl->link);
1255 $$ = declarator;
1256 }
1257 ;
1258
1259 switch_statement:
1260 SWITCH '(' expression ')' compound_statement
1261 ;
1262
1263 case_label:
1264 CASE expression ':'
1265 | DEFAULT ':'
1266 ;
1267
1268 iteration_statement:
1269 WHILE '(' condition ')' statement_no_new_scope
1270 {
1271 $$ = new ast_iteration_statement(ast_iteration_statement::ast_while,
1272 NULL, $3, NULL, $5);
1273 $$->set_location(yylloc);
1274 }
1275 | DO statement WHILE '(' expression ')' ';'
1276 {
1277 $$ = new ast_iteration_statement(ast_iteration_statement::ast_do_while,
1278 NULL, $5, NULL, $2);
1279 $$->set_location(yylloc);
1280 }
1281 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1282 {
1283 $$ = new ast_iteration_statement(ast_iteration_statement::ast_for,
1284 $3, $4.cond, $4.rest, $6);
1285 $$->set_location(yylloc);
1286 }
1287 ;
1288
1289 for_init_statement:
1290 expression_statement
1291 | declaration_statement
1292 ;
1293
1294 conditionopt:
1295 condition
1296 | /* empty */
1297 {
1298 $$ = NULL;
1299 }
1300 ;
1301
1302 for_rest_statement:
1303 conditionopt ';'
1304 {
1305 $$.cond = $1;
1306 $$.rest = NULL;
1307 }
1308 | conditionopt ';' expression
1309 {
1310 $$.cond = $1;
1311 $$.rest = $3;
1312 }
1313 ;
1314
1315 // Grammar Note: No 'goto'. Gotos are not supported.
1316 jump_statement:
1317 CONTINUE ';'
1318 {
1319 $$ = new ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1320 $$->set_location(yylloc);
1321 }
1322 | BREAK ';'
1323 {
1324 $$ = new ast_jump_statement(ast_jump_statement::ast_break, NULL);
1325 $$->set_location(yylloc);
1326 }
1327 | RETURN ';'
1328 {
1329 $$ = new ast_jump_statement(ast_jump_statement::ast_return, NULL);
1330 $$->set_location(yylloc);
1331 }
1332 | RETURN expression ';'
1333 {
1334 $$ = new ast_jump_statement(ast_jump_statement::ast_return, $2);
1335 $$->set_location(yylloc);
1336 }
1337 | DISCARD ';' // Fragment shader only.
1338 {
1339 $$ = new ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1340 $$->set_location(yylloc);
1341 }
1342 ;
1343
1344 external_declaration:
1345 function_definition { $$ = $1; }
1346 | declaration { $$ = $1; }
1347 ;
1348
1349 function_definition:
1350 function_prototype compound_statement_no_new_scope
1351 {
1352 $$ = new ast_function_definition();
1353 $$->set_location(yylloc);
1354 $$->prototype = $1;
1355 $$->body = $2;
1356 }
1357 ;