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