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