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