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