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