71ab039d620dc9b98e2c35625fc284195f0a6eb1
[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 &&
1128 (state->AMD_conservative_depth_enable ||
1129 state->ARB_conservative_depth_enable)) {
1130 if (strcmp($1, "depth_any") == 0) {
1131 got_one = true;
1132 $$.flags.q.depth_any = 1;
1133 } else if (strcmp($1, "depth_greater") == 0) {
1134 got_one = true;
1135 $$.flags.q.depth_greater = 1;
1136 } else if (strcmp($1, "depth_less") == 0) {
1137 got_one = true;
1138 $$.flags.q.depth_less = 1;
1139 } else if (strcmp($1, "depth_unchanged") == 0) {
1140 got_one = true;
1141 $$.flags.q.depth_unchanged = 1;
1142 }
1143
1144 if (got_one && state->AMD_conservative_depth_warn) {
1145 _mesa_glsl_warning(& @1, state,
1146 "GL_AMD_conservative_depth "
1147 "layout qualifier `%s' is used\n", $1);
1148 }
1149 if (got_one && state->ARB_conservative_depth_warn) {
1150 _mesa_glsl_warning(& @1, state,
1151 "GL_ARB_conservative_depth "
1152 "layout qualifier `%s' is used\n", $1);
1153 }
1154 }
1155
1156 if (!got_one) {
1157 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1158 "`%s'\n", $1);
1159 YYERROR;
1160 }
1161 }
1162 | any_identifier '=' INTCONSTANT
1163 {
1164 bool got_one = false;
1165
1166 memset(& $$, 0, sizeof($$));
1167
1168 if (state->ARB_explicit_attrib_location_enable) {
1169 /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1170 * FINISHME: GLSL 1.30 (or later) are supported.
1171 */
1172 if (strcmp("location", $1) == 0) {
1173 got_one = true;
1174
1175 $$.flags.q.explicit_location = 1;
1176
1177 if ($3 >= 0) {
1178 $$.location = $3;
1179 } else {
1180 _mesa_glsl_error(& @3, state,
1181 "invalid location %d specified\n", $3);
1182 YYERROR;
1183 }
1184 }
1185 }
1186
1187 /* If the identifier didn't match any known layout identifiers,
1188 * emit an error.
1189 */
1190 if (!got_one) {
1191 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1192 "`%s'\n", $1);
1193 YYERROR;
1194 } else if (state->ARB_explicit_attrib_location_warn) {
1195 _mesa_glsl_warning(& @1, state,
1196 "GL_ARB_explicit_attrib_location layout "
1197 "identifier `%s' used\n", $1);
1198 }
1199 }
1200 ;
1201
1202 interpolation_qualifier:
1203 SMOOTH
1204 {
1205 memset(& $$, 0, sizeof($$));
1206 $$.flags.q.smooth = 1;
1207 }
1208 | FLAT
1209 {
1210 memset(& $$, 0, sizeof($$));
1211 $$.flags.q.flat = 1;
1212 }
1213 | NOPERSPECTIVE
1214 {
1215 memset(& $$, 0, sizeof($$));
1216 $$.flags.q.noperspective = 1;
1217 }
1218 ;
1219
1220 parameter_type_qualifier:
1221 CONST_TOK
1222 {
1223 memset(& $$, 0, sizeof($$));
1224 $$.flags.q.constant = 1;
1225 }
1226 ;
1227
1228 type_qualifier:
1229 storage_qualifier
1230 | layout_qualifier
1231 | layout_qualifier storage_qualifier
1232 {
1233 $$ = $1;
1234 $$.flags.i |= $2.flags.i;
1235 }
1236 | interpolation_qualifier
1237 | interpolation_qualifier storage_qualifier
1238 {
1239 $$ = $1;
1240 $$.flags.i |= $2.flags.i;
1241 }
1242 | INVARIANT storage_qualifier
1243 {
1244 $$ = $2;
1245 $$.flags.q.invariant = 1;
1246 }
1247 | INVARIANT interpolation_qualifier storage_qualifier
1248 {
1249 $$ = $2;
1250 $$.flags.i |= $3.flags.i;
1251 $$.flags.q.invariant = 1;
1252 }
1253 | INVARIANT
1254 {
1255 memset(& $$, 0, sizeof($$));
1256 $$.flags.q.invariant = 1;
1257 }
1258 ;
1259
1260 storage_qualifier:
1261 CONST_TOK
1262 {
1263 memset(& $$, 0, sizeof($$));
1264 $$.flags.q.constant = 1;
1265 }
1266 | ATTRIBUTE
1267 {
1268 memset(& $$, 0, sizeof($$));
1269 $$.flags.q.attribute = 1;
1270 }
1271 | VARYING
1272 {
1273 memset(& $$, 0, sizeof($$));
1274 $$.flags.q.varying = 1;
1275 }
1276 | CENTROID VARYING
1277 {
1278 memset(& $$, 0, sizeof($$));
1279 $$.flags.q.centroid = 1;
1280 $$.flags.q.varying = 1;
1281 }
1282 | IN_TOK
1283 {
1284 memset(& $$, 0, sizeof($$));
1285 $$.flags.q.in = 1;
1286 }
1287 | OUT_TOK
1288 {
1289 memset(& $$, 0, sizeof($$));
1290 $$.flags.q.out = 1;
1291 }
1292 | CENTROID IN_TOK
1293 {
1294 memset(& $$, 0, sizeof($$));
1295 $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1296 }
1297 | CENTROID OUT_TOK
1298 {
1299 memset(& $$, 0, sizeof($$));
1300 $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1301 }
1302 | UNIFORM
1303 {
1304 memset(& $$, 0, sizeof($$));
1305 $$.flags.q.uniform = 1;
1306 }
1307 ;
1308
1309 type_specifier:
1310 type_specifier_no_prec
1311 {
1312 $$ = $1;
1313 }
1314 | precision_qualifier type_specifier_no_prec
1315 {
1316 $$ = $2;
1317 $$->precision = $1;
1318 }
1319 ;
1320
1321 type_specifier_no_prec:
1322 type_specifier_nonarray
1323 | type_specifier_nonarray '[' ']'
1324 {
1325 $$ = $1;
1326 $$->is_array = true;
1327 $$->array_size = NULL;
1328 }
1329 | type_specifier_nonarray '[' constant_expression ']'
1330 {
1331 $$ = $1;
1332 $$->is_array = true;
1333 $$->array_size = $3;
1334 }
1335 ;
1336
1337 type_specifier_nonarray:
1338 basic_type_specifier_nonarray
1339 {
1340 void *ctx = state;
1341 $$ = new(ctx) ast_type_specifier($1);
1342 $$->set_location(yylloc);
1343 }
1344 | struct_specifier
1345 {
1346 void *ctx = state;
1347 $$ = new(ctx) ast_type_specifier($1);
1348 $$->set_location(yylloc);
1349 }
1350 | TYPE_IDENTIFIER
1351 {
1352 void *ctx = state;
1353 $$ = new(ctx) ast_type_specifier($1);
1354 $$->set_location(yylloc);
1355 }
1356 ;
1357
1358 basic_type_specifier_nonarray:
1359 VOID_TOK { $$ = ast_void; }
1360 | FLOAT_TOK { $$ = ast_float; }
1361 | INT_TOK { $$ = ast_int; }
1362 | UINT_TOK { $$ = ast_uint; }
1363 | BOOL_TOK { $$ = ast_bool; }
1364 | VEC2 { $$ = ast_vec2; }
1365 | VEC3 { $$ = ast_vec3; }
1366 | VEC4 { $$ = ast_vec4; }
1367 | BVEC2 { $$ = ast_bvec2; }
1368 | BVEC3 { $$ = ast_bvec3; }
1369 | BVEC4 { $$ = ast_bvec4; }
1370 | IVEC2 { $$ = ast_ivec2; }
1371 | IVEC3 { $$ = ast_ivec3; }
1372 | IVEC4 { $$ = ast_ivec4; }
1373 | UVEC2 { $$ = ast_uvec2; }
1374 | UVEC3 { $$ = ast_uvec3; }
1375 | UVEC4 { $$ = ast_uvec4; }
1376 | MAT2X2 { $$ = ast_mat2; }
1377 | MAT2X3 { $$ = ast_mat2x3; }
1378 | MAT2X4 { $$ = ast_mat2x4; }
1379 | MAT3X2 { $$ = ast_mat3x2; }
1380 | MAT3X3 { $$ = ast_mat3; }
1381 | MAT3X4 { $$ = ast_mat3x4; }
1382 | MAT4X2 { $$ = ast_mat4x2; }
1383 | MAT4X3 { $$ = ast_mat4x3; }
1384 | MAT4X4 { $$ = ast_mat4; }
1385 | SAMPLER1D { $$ = ast_sampler1d; }
1386 | SAMPLER2D { $$ = ast_sampler2d; }
1387 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
1388 | SAMPLER3D { $$ = ast_sampler3d; }
1389 | SAMPLERCUBE { $$ = ast_samplercube; }
1390 | SAMPLEREXTERNALOES { $$ = ast_samplerexternaloes; }
1391 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
1392 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
1393 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
1394 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
1395 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
1396 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
1397 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
1398 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1399 | ISAMPLER1D { $$ = ast_isampler1d; }
1400 | ISAMPLER2D { $$ = ast_isampler2d; }
1401 | ISAMPLER3D { $$ = ast_isampler3d; }
1402 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1403 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1404 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1405 | USAMPLER1D { $$ = ast_usampler1d; }
1406 | USAMPLER2D { $$ = ast_usampler2d; }
1407 | USAMPLER3D { $$ = ast_usampler3d; }
1408 | USAMPLERCUBE { $$ = ast_usamplercube; }
1409 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1410 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1411 ;
1412
1413 precision_qualifier:
1414 HIGHP {
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_high;
1423 }
1424 | MEDIUMP {
1425 if (!state->es_shader && state->language_version < 130)
1426 _mesa_glsl_error(& @1, state,
1427 "precision qualifier forbidden "
1428 "in %s (1.30 or later "
1429 "required)\n",
1430 state->version_string);
1431
1432 $$ = ast_precision_medium;
1433 }
1434 | LOWP {
1435 if (!state->es_shader && state->language_version < 130)
1436 _mesa_glsl_error(& @1, state,
1437 "precision qualifier forbidden "
1438 "in %s (1.30 or later "
1439 "required)\n",
1440 state->version_string);
1441
1442 $$ = ast_precision_low;
1443 }
1444 ;
1445
1446 struct_specifier:
1447 STRUCT any_identifier '{' struct_declaration_list '}'
1448 {
1449 void *ctx = state;
1450 $$ = new(ctx) ast_struct_specifier($2, $4);
1451 $$->set_location(yylloc);
1452 state->symbols->add_type($2, glsl_type::void_type);
1453 }
1454 | STRUCT '{' struct_declaration_list '}'
1455 {
1456 void *ctx = state;
1457 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1458 $$->set_location(yylloc);
1459 }
1460 ;
1461
1462 struct_declaration_list:
1463 struct_declaration
1464 {
1465 $$ = (ast_node *) $1;
1466 $1->link.self_link();
1467 }
1468 | struct_declaration_list struct_declaration
1469 {
1470 $$ = (ast_node *) $1;
1471 $$->link.insert_before(& $2->link);
1472 }
1473 ;
1474
1475 struct_declaration:
1476 type_specifier struct_declarator_list ';'
1477 {
1478 void *ctx = state;
1479 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1480 type->set_location(yylloc);
1481
1482 type->specifier = $1;
1483 $$ = new(ctx) ast_declarator_list(type);
1484 $$->set_location(yylloc);
1485
1486 $$->declarations.push_degenerate_list_at_head(& $2->link);
1487 }
1488 ;
1489
1490 struct_declarator_list:
1491 struct_declarator
1492 {
1493 $$ = $1;
1494 $1->link.self_link();
1495 }
1496 | struct_declarator_list ',' struct_declarator
1497 {
1498 $$ = $1;
1499 $$->link.insert_before(& $3->link);
1500 }
1501 ;
1502
1503 struct_declarator:
1504 any_identifier
1505 {
1506 void *ctx = state;
1507 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1508 $$->set_location(yylloc);
1509 state->symbols->add_variable(new(state) ir_variable(NULL, $1, ir_var_auto));
1510 }
1511 | any_identifier '[' constant_expression ']'
1512 {
1513 void *ctx = state;
1514 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1515 $$->set_location(yylloc);
1516 }
1517 ;
1518
1519 initializer:
1520 assignment_expression
1521 ;
1522
1523 declaration_statement:
1524 declaration
1525 ;
1526
1527 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1528 // supported.
1529 statement:
1530 compound_statement { $$ = (ast_node *) $1; }
1531 | simple_statement
1532 ;
1533
1534 simple_statement:
1535 declaration_statement
1536 | expression_statement
1537 | selection_statement
1538 | switch_statement
1539 | iteration_statement
1540 | jump_statement
1541 ;
1542
1543 compound_statement:
1544 '{' '}'
1545 {
1546 void *ctx = state;
1547 $$ = new(ctx) ast_compound_statement(true, NULL);
1548 $$->set_location(yylloc);
1549 }
1550 | '{'
1551 {
1552 state->symbols->push_scope();
1553 }
1554 statement_list '}'
1555 {
1556 void *ctx = state;
1557 $$ = new(ctx) ast_compound_statement(true, $3);
1558 $$->set_location(yylloc);
1559 state->symbols->pop_scope();
1560 }
1561 ;
1562
1563 statement_no_new_scope:
1564 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1565 | simple_statement
1566 ;
1567
1568 compound_statement_no_new_scope:
1569 '{' '}'
1570 {
1571 void *ctx = state;
1572 $$ = new(ctx) ast_compound_statement(false, NULL);
1573 $$->set_location(yylloc);
1574 }
1575 | '{' statement_list '}'
1576 {
1577 void *ctx = state;
1578 $$ = new(ctx) ast_compound_statement(false, $2);
1579 $$->set_location(yylloc);
1580 }
1581 ;
1582
1583 statement_list:
1584 statement
1585 {
1586 if ($1 == NULL) {
1587 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1588 assert($1 != NULL);
1589 }
1590
1591 $$ = $1;
1592 $$->link.self_link();
1593 }
1594 | statement_list statement
1595 {
1596 if ($2 == NULL) {
1597 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1598 assert($2 != NULL);
1599 }
1600 $$ = $1;
1601 $$->link.insert_before(& $2->link);
1602 }
1603 ;
1604
1605 expression_statement:
1606 ';'
1607 {
1608 void *ctx = state;
1609 $$ = new(ctx) ast_expression_statement(NULL);
1610 $$->set_location(yylloc);
1611 }
1612 | expression ';'
1613 {
1614 void *ctx = state;
1615 $$ = new(ctx) ast_expression_statement($1);
1616 $$->set_location(yylloc);
1617 }
1618 ;
1619
1620 selection_statement:
1621 IF '(' expression ')' selection_rest_statement
1622 {
1623 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1624 $5.else_statement);
1625 $$->set_location(yylloc);
1626 }
1627 ;
1628
1629 selection_rest_statement:
1630 statement ELSE statement
1631 {
1632 $$.then_statement = $1;
1633 $$.else_statement = $3;
1634 }
1635 | statement
1636 {
1637 $$.then_statement = $1;
1638 $$.else_statement = NULL;
1639 }
1640 ;
1641
1642 condition:
1643 expression
1644 {
1645 $$ = (ast_node *) $1;
1646 }
1647 | fully_specified_type any_identifier '=' initializer
1648 {
1649 void *ctx = state;
1650 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1651 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1652 decl->set_location(yylloc);
1653 declarator->set_location(yylloc);
1654
1655 declarator->declarations.push_tail(&decl->link);
1656 $$ = declarator;
1657 }
1658 ;
1659
1660 /*
1661 * siwtch_statement grammar is based on the syntax described in the body
1662 * of the GLSL spec, not in it's appendix!!!
1663 */
1664 switch_statement:
1665 SWITCH '(' expression ')' switch_body
1666 {
1667 $$ = new(state) ast_switch_statement($3, $5);
1668 }
1669 ;
1670
1671 switch_body:
1672 '{' '}'
1673 {
1674 $$ = new(state) ast_switch_body(NULL);
1675 $$->set_location(yylloc);
1676 }
1677 | '{' case_statement_list '}'
1678 {
1679 $$ = new(state) ast_switch_body($2);
1680 $$->set_location(yylloc);
1681 }
1682 ;
1683
1684 case_label:
1685 CASE expression ':'
1686 {
1687 $$ = new(state) ast_case_label($2);
1688 }
1689 | DEFAULT ':'
1690 {
1691 $$ = new(state) ast_case_label(NULL);
1692 }
1693 ;
1694
1695 case_label_list:
1696 case_label
1697 {
1698 ast_case_label_list *labels = new(state) ast_case_label_list();
1699
1700 labels->labels.push_tail(& $1->link);
1701 $$ = labels;
1702 }
1703 | case_label_list case_label
1704 {
1705 $$ = $1;
1706 $$->labels.push_tail(& $2->link);
1707 }
1708 ;
1709
1710 case_statement:
1711 case_label_list statement
1712 {
1713 ast_case_statement *stmts = new(state) ast_case_statement($1);
1714
1715 stmts->stmts.push_tail(& $2->link);
1716 $$ = stmts;
1717 }
1718 | case_statement statement
1719 {
1720 $$ = $1;
1721 $$->stmts.push_tail(& $2->link);
1722 }
1723 ;
1724
1725 case_statement_list:
1726 case_statement
1727 {
1728 ast_case_statement_list *cases= new(state) ast_case_statement_list();
1729
1730 cases->cases.push_tail(& $1->link);
1731 $$ = cases;
1732 }
1733 | case_statement_list case_statement
1734 {
1735 $$ = $1;
1736 $$->cases.push_tail(& $2->link);
1737 }
1738 ;
1739
1740 iteration_statement:
1741 WHILE '(' condition ')' statement_no_new_scope
1742 {
1743 void *ctx = state;
1744 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1745 NULL, $3, NULL, $5);
1746 $$->set_location(yylloc);
1747 }
1748 | DO statement WHILE '(' expression ')' ';'
1749 {
1750 void *ctx = state;
1751 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1752 NULL, $5, NULL, $2);
1753 $$->set_location(yylloc);
1754 }
1755 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1756 {
1757 void *ctx = state;
1758 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1759 $3, $4.cond, $4.rest, $6);
1760 $$->set_location(yylloc);
1761 }
1762 ;
1763
1764 for_init_statement:
1765 expression_statement
1766 | declaration_statement
1767 ;
1768
1769 conditionopt:
1770 condition
1771 | /* empty */
1772 {
1773 $$ = NULL;
1774 }
1775 ;
1776
1777 for_rest_statement:
1778 conditionopt ';'
1779 {
1780 $$.cond = $1;
1781 $$.rest = NULL;
1782 }
1783 | conditionopt ';' expression
1784 {
1785 $$.cond = $1;
1786 $$.rest = $3;
1787 }
1788 ;
1789
1790 // Grammar Note: No 'goto'. Gotos are not supported.
1791 jump_statement:
1792 CONTINUE ';'
1793 {
1794 void *ctx = state;
1795 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1796 $$->set_location(yylloc);
1797 }
1798 | BREAK ';'
1799 {
1800 void *ctx = state;
1801 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1802 $$->set_location(yylloc);
1803 }
1804 | RETURN ';'
1805 {
1806 void *ctx = state;
1807 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1808 $$->set_location(yylloc);
1809 }
1810 | RETURN expression ';'
1811 {
1812 void *ctx = state;
1813 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1814 $$->set_location(yylloc);
1815 }
1816 | DISCARD ';' // Fragment shader only.
1817 {
1818 void *ctx = state;
1819 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1820 $$->set_location(yylloc);
1821 }
1822 ;
1823
1824 external_declaration:
1825 function_definition { $$ = $1; }
1826 | declaration { $$ = $1; }
1827 | pragma_statement { $$ = NULL; }
1828 ;
1829
1830 function_definition:
1831 function_prototype compound_statement_no_new_scope
1832 {
1833 void *ctx = state;
1834 $$ = new(ctx) ast_function_definition();
1835 $$->set_location(yylloc);
1836 $$->prototype = $1;
1837 $$->body = $2;
1838
1839 state->symbols->pop_scope();
1840 }
1841 ;