920213c3035c404faf657ce0f241ecc844984646
[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 ($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 { $$ = "void"; }
1369 | FLOAT_TOK { $$ = "float"; }
1370 | INT_TOK { $$ = "int"; }
1371 | UINT_TOK { $$ = "uint"; }
1372 | BOOL_TOK { $$ = "bool"; }
1373 | VEC2 { $$ = "vec2"; }
1374 | VEC3 { $$ = "vec3"; }
1375 | VEC4 { $$ = "vec4"; }
1376 | BVEC2 { $$ = "bvec2"; }
1377 | BVEC3 { $$ = "bvec3"; }
1378 | BVEC4 { $$ = "bvec4"; }
1379 | IVEC2 { $$ = "ivec2"; }
1380 | IVEC3 { $$ = "ivec3"; }
1381 | IVEC4 { $$ = "ivec4"; }
1382 | UVEC2 { $$ = "uvec2"; }
1383 | UVEC3 { $$ = "uvec3"; }
1384 | UVEC4 { $$ = "uvec4"; }
1385 | MAT2X2 { $$ = "mat2"; }
1386 | MAT2X3 { $$ = "mat2x3"; }
1387 | MAT2X4 { $$ = "mat2x4"; }
1388 | MAT3X2 { $$ = "mat3x2"; }
1389 | MAT3X3 { $$ = "mat3"; }
1390 | MAT3X4 { $$ = "mat3x4"; }
1391 | MAT4X2 { $$ = "mat4x2"; }
1392 | MAT4X3 { $$ = "mat4x3"; }
1393 | MAT4X4 { $$ = "mat4"; }
1394 | SAMPLER1D { $$ = "sampler1D"; }
1395 | SAMPLER2D { $$ = "sampler2D"; }
1396 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
1397 | SAMPLER3D { $$ = "sampler3D"; }
1398 | SAMPLERCUBE { $$ = "samplerCube"; }
1399 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
1400 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
1401 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
1402 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
1403 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
1404 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
1405 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
1406 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
1407 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
1408 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
1409 | ISAMPLER1D { $$ = "isampler1D"; }
1410 | ISAMPLER2D { $$ = "isampler2D"; }
1411 | ISAMPLER3D { $$ = "isampler3D"; }
1412 | ISAMPLERCUBE { $$ = "isamplerCube"; }
1413 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
1414 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
1415 | USAMPLER1D { $$ = "usampler1D"; }
1416 | USAMPLER2D { $$ = "usampler2D"; }
1417 | USAMPLER3D { $$ = "usampler3D"; }
1418 | USAMPLERCUBE { $$ = "usamplerCube"; }
1419 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
1420 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
1421 ;
1422
1423 precision_qualifier:
1424 HIGHP {
1425 if (!state->es_shader && state->language_version < 130)
1426 _mesa_glsl_error(& @1, state,
1427 "precision qualifier forbidden "
1428 "in %s (1.30 or later "
1429 "required)\n",
1430 state->version_string);
1431
1432 $$ = ast_precision_high;
1433 }
1434 | MEDIUMP {
1435 if (!state->es_shader && state->language_version < 130)
1436 _mesa_glsl_error(& @1, state,
1437 "precision qualifier forbidden "
1438 "in %s (1.30 or later "
1439 "required)\n",
1440 state->version_string);
1441
1442 $$ = ast_precision_medium;
1443 }
1444 | LOWP {
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_low;
1453 }
1454 ;
1455
1456 struct_specifier:
1457 STRUCT any_identifier '{' struct_declaration_list '}'
1458 {
1459 void *ctx = state;
1460 $$ = new(ctx) ast_struct_specifier($2, $4);
1461 $$->set_location(yylloc);
1462 state->symbols->add_type($2, glsl_type::void_type);
1463 }
1464 | STRUCT '{' struct_declaration_list '}'
1465 {
1466 void *ctx = state;
1467 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1468 $$->set_location(yylloc);
1469 }
1470 ;
1471
1472 struct_declaration_list:
1473 struct_declaration
1474 {
1475 $$ = (ast_node *) $1;
1476 $1->link.self_link();
1477 }
1478 | struct_declaration_list struct_declaration
1479 {
1480 $$ = (ast_node *) $1;
1481 $$->link.insert_before(& $2->link);
1482 }
1483 ;
1484
1485 struct_declaration:
1486 type_specifier struct_declarator_list ';'
1487 {
1488 void *ctx = state;
1489 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1490 type->set_location(yylloc);
1491
1492 type->specifier = $1;
1493 $$ = new(ctx) ast_declarator_list(type);
1494 $$->set_location(yylloc);
1495
1496 $$->declarations.push_degenerate_list_at_head(& $2->link);
1497 }
1498 ;
1499
1500 struct_declarator_list:
1501 struct_declarator
1502 {
1503 $$ = $1;
1504 $1->link.self_link();
1505 }
1506 | struct_declarator_list ',' struct_declarator
1507 {
1508 $$ = $1;
1509 $$->link.insert_before(& $3->link);
1510 }
1511 ;
1512
1513 struct_declarator:
1514 any_identifier
1515 {
1516 void *ctx = state;
1517 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1518 $$->set_location(yylloc);
1519 state->symbols->add_variable(new(state) ir_variable(NULL, $1, ir_var_auto));
1520 }
1521 | any_identifier '[' constant_expression ']'
1522 {
1523 void *ctx = state;
1524 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1525 $$->set_location(yylloc);
1526 }
1527 ;
1528
1529 initializer:
1530 assignment_expression
1531 ;
1532
1533 declaration_statement:
1534 declaration
1535 ;
1536
1537 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1538 // supported.
1539 statement:
1540 compound_statement { $$ = (ast_node *) $1; }
1541 | simple_statement
1542 ;
1543
1544 simple_statement:
1545 declaration_statement
1546 | expression_statement
1547 | selection_statement
1548 | switch_statement
1549 | iteration_statement
1550 | jump_statement
1551 ;
1552
1553 compound_statement:
1554 '{' '}'
1555 {
1556 void *ctx = state;
1557 $$ = new(ctx) ast_compound_statement(true, NULL);
1558 $$->set_location(yylloc);
1559 }
1560 | '{'
1561 {
1562 state->symbols->push_scope();
1563 }
1564 statement_list '}'
1565 {
1566 void *ctx = state;
1567 $$ = new(ctx) ast_compound_statement(true, $3);
1568 $$->set_location(yylloc);
1569 state->symbols->pop_scope();
1570 }
1571 ;
1572
1573 statement_no_new_scope:
1574 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1575 | simple_statement
1576 ;
1577
1578 compound_statement_no_new_scope:
1579 '{' '}'
1580 {
1581 void *ctx = state;
1582 $$ = new(ctx) ast_compound_statement(false, NULL);
1583 $$->set_location(yylloc);
1584 }
1585 | '{' statement_list '}'
1586 {
1587 void *ctx = state;
1588 $$ = new(ctx) ast_compound_statement(false, $2);
1589 $$->set_location(yylloc);
1590 }
1591 ;
1592
1593 statement_list:
1594 statement
1595 {
1596 if ($1 == NULL) {
1597 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1598 assert($1 != NULL);
1599 }
1600
1601 $$ = $1;
1602 $$->link.self_link();
1603 }
1604 | statement_list statement
1605 {
1606 if ($2 == NULL) {
1607 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1608 assert($2 != NULL);
1609 }
1610 $$ = $1;
1611 $$->link.insert_before(& $2->link);
1612 }
1613 ;
1614
1615 expression_statement:
1616 ';'
1617 {
1618 void *ctx = state;
1619 $$ = new(ctx) ast_expression_statement(NULL);
1620 $$->set_location(yylloc);
1621 }
1622 | expression ';'
1623 {
1624 void *ctx = state;
1625 $$ = new(ctx) ast_expression_statement($1);
1626 $$->set_location(yylloc);
1627 }
1628 ;
1629
1630 selection_statement:
1631 IF '(' expression ')' selection_rest_statement
1632 {
1633 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1634 $5.else_statement);
1635 $$->set_location(yylloc);
1636 }
1637 ;
1638
1639 selection_rest_statement:
1640 statement ELSE statement
1641 {
1642 $$.then_statement = $1;
1643 $$.else_statement = $3;
1644 }
1645 | statement
1646 {
1647 $$.then_statement = $1;
1648 $$.else_statement = NULL;
1649 }
1650 ;
1651
1652 condition:
1653 expression
1654 {
1655 $$ = (ast_node *) $1;
1656 }
1657 | fully_specified_type any_identifier '=' initializer
1658 {
1659 void *ctx = state;
1660 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1661 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1662 decl->set_location(yylloc);
1663 declarator->set_location(yylloc);
1664
1665 declarator->declarations.push_tail(&decl->link);
1666 $$ = declarator;
1667 }
1668 ;
1669
1670 /*
1671 * siwtch_statement grammar is based on the syntax described in the body
1672 * of the GLSL spec, not in it's appendix!!!
1673 */
1674 switch_statement:
1675 SWITCH '(' expression ')' switch_body
1676 {
1677 $$ = new(state) ast_switch_statement($3, $5);
1678 $$->set_location(yylloc);
1679 }
1680 ;
1681
1682 switch_body:
1683 '{' '}'
1684 {
1685 $$ = new(state) ast_switch_body(NULL);
1686 $$->set_location(yylloc);
1687 }
1688 | '{' case_statement_list '}'
1689 {
1690 $$ = new(state) ast_switch_body($2);
1691 $$->set_location(yylloc);
1692 }
1693 ;
1694
1695 case_label:
1696 CASE expression ':'
1697 {
1698 $$ = new(state) ast_case_label($2);
1699 $$->set_location(yylloc);
1700 }
1701 | DEFAULT ':'
1702 {
1703 $$ = new(state) ast_case_label(NULL);
1704 $$->set_location(yylloc);
1705 }
1706 ;
1707
1708 case_label_list:
1709 case_label
1710 {
1711 ast_case_label_list *labels = new(state) ast_case_label_list();
1712
1713 labels->labels.push_tail(& $1->link);
1714 $$ = labels;
1715 $$->set_location(yylloc);
1716 }
1717 | case_label_list case_label
1718 {
1719 $$ = $1;
1720 $$->labels.push_tail(& $2->link);
1721 }
1722 ;
1723
1724 case_statement:
1725 case_label_list statement
1726 {
1727 ast_case_statement *stmts = new(state) ast_case_statement($1);
1728 stmts->set_location(yylloc);
1729
1730 stmts->stmts.push_tail(& $2->link);
1731 $$ = stmts;
1732 }
1733 | case_statement statement
1734 {
1735 $$ = $1;
1736 $$->stmts.push_tail(& $2->link);
1737 }
1738 ;
1739
1740 case_statement_list:
1741 case_statement
1742 {
1743 ast_case_statement_list *cases= new(state) ast_case_statement_list();
1744 cases->set_location(yylloc);
1745
1746 cases->cases.push_tail(& $1->link);
1747 $$ = cases;
1748 }
1749 | case_statement_list case_statement
1750 {
1751 $$ = $1;
1752 $$->cases.push_tail(& $2->link);
1753 }
1754 ;
1755
1756 iteration_statement:
1757 WHILE '(' condition ')' statement_no_new_scope
1758 {
1759 void *ctx = state;
1760 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1761 NULL, $3, NULL, $5);
1762 $$->set_location(yylloc);
1763 }
1764 | DO statement WHILE '(' expression ')' ';'
1765 {
1766 void *ctx = state;
1767 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1768 NULL, $5, NULL, $2);
1769 $$->set_location(yylloc);
1770 }
1771 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1772 {
1773 void *ctx = state;
1774 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1775 $3, $4.cond, $4.rest, $6);
1776 $$->set_location(yylloc);
1777 }
1778 ;
1779
1780 for_init_statement:
1781 expression_statement
1782 | declaration_statement
1783 ;
1784
1785 conditionopt:
1786 condition
1787 | /* empty */
1788 {
1789 $$ = NULL;
1790 }
1791 ;
1792
1793 for_rest_statement:
1794 conditionopt ';'
1795 {
1796 $$.cond = $1;
1797 $$.rest = NULL;
1798 }
1799 | conditionopt ';' expression
1800 {
1801 $$.cond = $1;
1802 $$.rest = $3;
1803 }
1804 ;
1805
1806 // Grammar Note: No 'goto'. Gotos are not supported.
1807 jump_statement:
1808 CONTINUE ';'
1809 {
1810 void *ctx = state;
1811 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1812 $$->set_location(yylloc);
1813 }
1814 | BREAK ';'
1815 {
1816 void *ctx = state;
1817 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1818 $$->set_location(yylloc);
1819 }
1820 | RETURN ';'
1821 {
1822 void *ctx = state;
1823 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1824 $$->set_location(yylloc);
1825 }
1826 | RETURN expression ';'
1827 {
1828 void *ctx = state;
1829 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1830 $$->set_location(yylloc);
1831 }
1832 | DISCARD ';' // Fragment shader only.
1833 {
1834 void *ctx = state;
1835 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1836 $$->set_location(yylloc);
1837 }
1838 ;
1839
1840 external_declaration:
1841 function_definition { $$ = $1; }
1842 | declaration { $$ = $1; }
1843 | pragma_statement { $$ = NULL; }
1844 ;
1845
1846 function_definition:
1847 function_prototype compound_statement_no_new_scope
1848 {
1849 void *ctx = state;
1850 $$ = new(ctx) ast_function_definition();
1851 $$->set_location(yylloc);
1852 $$->prototype = $1;
1853 $$->body = $2;
1854
1855 state->symbols->pop_scope();
1856 }
1857 ;