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