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