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