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