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