glsl: fix missing breaks in equals(ir_texture,..)
[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 #undef yyerror
35
36 static void yyerror(YYLTYPE *loc, _mesa_glsl_parse_state *st, const char *msg)
37 {
38 _mesa_glsl_error(loc, st, "%s", msg);
39 }
40
41 static int
42 _mesa_glsl_lex(YYSTYPE *val, YYLTYPE *loc, _mesa_glsl_parse_state *state)
43 {
44 return _mesa_glsl_lexer_lex(val, loc, state->scanner);
45 }
46
47 static bool match_layout_qualifier(const char *s1, const char *s2,
48 _mesa_glsl_parse_state *state)
49 {
50 /* From the GLSL 1.50 spec, section 4.3.8 (Layout Qualifiers):
51 *
52 * "The tokens in any layout-qualifier-id-list ... are not case
53 * sensitive, unless explicitly noted otherwise."
54 *
55 * The text "unless explicitly noted otherwise" appears to be
56 * vacuous--no desktop GLSL spec (up through GLSL 4.40) notes
57 * otherwise.
58 *
59 * However, the GLSL ES 3.00 spec says, in section 4.3.8 (Layout
60 * Qualifiers):
61 *
62 * "As for other identifiers, they are case sensitive."
63 *
64 * So we need to do a case-sensitive or a case-insensitive match,
65 * depending on whether we are compiling for GLSL ES.
66 */
67 if (state->es_shader)
68 return strcmp(s1, s2);
69 else
70 return strcasecmp(s1, s2);
71 }
72 %}
73
74 %expect 0
75
76 %pure-parser
77 %error-verbose
78
79 %locations
80 %initial-action {
81 @$.first_line = 1;
82 @$.first_column = 1;
83 @$.last_line = 1;
84 @$.last_column = 1;
85 @$.source = 0;
86 }
87
88 %lex-param {struct _mesa_glsl_parse_state *state}
89 %parse-param {struct _mesa_glsl_parse_state *state}
90
91 %union {
92 int n;
93 float real;
94 const char *identifier;
95
96 struct ast_type_qualifier type_qualifier;
97
98 ast_node *node;
99 ast_type_specifier *type_specifier;
100 ast_fully_specified_type *fully_specified_type;
101 ast_function *function;
102 ast_parameter_declarator *parameter_declarator;
103 ast_function_definition *function_definition;
104 ast_compound_statement *compound_statement;
105 ast_expression *expression;
106 ast_declarator_list *declarator_list;
107 ast_struct_specifier *struct_specifier;
108 ast_declaration *declaration;
109 ast_switch_body *switch_body;
110 ast_case_label *case_label;
111 ast_case_label_list *case_label_list;
112 ast_case_statement *case_statement;
113 ast_case_statement_list *case_statement_list;
114 ast_interface_block *interface_block;
115
116 struct {
117 ast_node *cond;
118 ast_expression *rest;
119 } for_rest_statement;
120
121 struct {
122 ast_node *then_statement;
123 ast_node *else_statement;
124 } selection_rest_statement;
125 }
126
127 %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK
128 %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
129 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
130 %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING
131 %token NOPERSPECTIVE FLAT SMOOTH
132 %token MAT2X2 MAT2X3 MAT2X4
133 %token MAT3X2 MAT3X3 MAT3X4
134 %token MAT4X2 MAT4X3 MAT4X4
135 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
136 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
137 %token SAMPLER2DARRAYSHADOW SAMPLERCUBEARRAY SAMPLERCUBEARRAYSHADOW
138 %token ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
139 %token ISAMPLER1DARRAY ISAMPLER2DARRAY ISAMPLERCUBEARRAY
140 %token USAMPLER1D USAMPLER2D USAMPLER3D USAMPLERCUBE USAMPLER1DARRAY
141 %token USAMPLER2DARRAY USAMPLERCUBEARRAY
142 %token SAMPLER2DRECT ISAMPLER2DRECT USAMPLER2DRECT SAMPLER2DRECTSHADOW
143 %token SAMPLERBUFFER ISAMPLERBUFFER USAMPLERBUFFER
144 %token SAMPLER2DMS ISAMPLER2DMS USAMPLER2DMS
145 %token SAMPLER2DMSARRAY ISAMPLER2DMSARRAY USAMPLER2DMSARRAY
146 %token SAMPLEREXTERNALOES
147 %token ATOMIC_UINT
148 %token STRUCT VOID_TOK WHILE
149 %token <identifier> IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
150 %type <identifier> any_identifier
151 %type <interface_block> instance_name_opt
152 %token <real> FLOATCONSTANT
153 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
154 %token <identifier> FIELD_SELECTION
155 %token LEFT_OP RIGHT_OP
156 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
157 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
158 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
159 %token SUB_ASSIGN
160 %token INVARIANT
161 %token LOWP MEDIUMP HIGHP SUPERP PRECISION
162
163 %token VERSION_TOK EXTENSION LINE COLON EOL INTERFACE OUTPUT
164 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
165 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
166 %token PRAGMA_INVARIANT_ALL
167 %token LAYOUT_TOK
168
169 /* Reserved words that are not actually used in the grammar.
170 */
171 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
172 %token INLINE_TOK NOINLINE VOLATILE PUBLIC_TOK STATIC EXTERN EXTERNAL
173 %token LONG_TOK SHORT_TOK DOUBLE_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK OUPTUT
174 %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
175 %token SAMPLER3DRECT
176 %token SIZEOF CAST NAMESPACE USING
177 %token COHERENT RESTRICT READONLY WRITEONLY RESOURCE PATCH SAMPLE
178 %token SUBROUTINE
179
180 %token ERROR_TOK
181
182 %token COMMON PARTITION ACTIVE FILTER
183 %token IMAGE1D IMAGE2D IMAGE3D IMAGECUBE IMAGE1DARRAY IMAGE2DARRAY
184 %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGECUBE IIMAGE1DARRAY IIMAGE2DARRAY
185 %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGECUBE UIMAGE1DARRAY UIMAGE2DARRAY
186 %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGEBUFFER IIMAGEBUFFER UIMAGEBUFFER
187 %token IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW
188 %token ROW_MAJOR
189
190 %type <identifier> variable_identifier
191 %type <node> statement
192 %type <node> statement_list
193 %type <node> simple_statement
194 %type <n> precision_qualifier
195 %type <type_qualifier> type_qualifier
196 %type <type_qualifier> auxiliary_storage_qualifier
197 %type <type_qualifier> storage_qualifier
198 %type <type_qualifier> interpolation_qualifier
199 %type <type_qualifier> layout_qualifier
200 %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
201 %type <type_qualifier> interface_block_layout_qualifier
202 %type <type_qualifier> interface_qualifier
203 %type <type_specifier> type_specifier
204 %type <type_specifier> type_specifier_nonarray
205 %type <identifier> basic_type_specifier_nonarray
206 %type <fully_specified_type> fully_specified_type
207 %type <function> function_prototype
208 %type <function> function_header
209 %type <function> function_header_with_parameters
210 %type <function> function_declarator
211 %type <parameter_declarator> parameter_declarator
212 %type <parameter_declarator> parameter_declaration
213 %type <type_qualifier> parameter_qualifier
214 %type <type_qualifier> parameter_direction_qualifier
215 %type <type_specifier> parameter_type_specifier
216 %type <function_definition> function_definition
217 %type <compound_statement> compound_statement_no_new_scope
218 %type <compound_statement> compound_statement
219 %type <node> statement_no_new_scope
220 %type <node> expression_statement
221 %type <expression> expression
222 %type <expression> primary_expression
223 %type <expression> assignment_expression
224 %type <expression> conditional_expression
225 %type <expression> logical_or_expression
226 %type <expression> logical_xor_expression
227 %type <expression> logical_and_expression
228 %type <expression> inclusive_or_expression
229 %type <expression> exclusive_or_expression
230 %type <expression> and_expression
231 %type <expression> equality_expression
232 %type <expression> relational_expression
233 %type <expression> shift_expression
234 %type <expression> additive_expression
235 %type <expression> multiplicative_expression
236 %type <expression> unary_expression
237 %type <expression> constant_expression
238 %type <expression> integer_expression
239 %type <expression> postfix_expression
240 %type <expression> function_call_header_with_parameters
241 %type <expression> function_call_header_no_parameters
242 %type <expression> function_call_header
243 %type <expression> function_call_generic
244 %type <expression> function_call_or_method
245 %type <expression> function_call
246 %type <expression> method_call_generic
247 %type <expression> method_call_header_with_parameters
248 %type <expression> method_call_header_no_parameters
249 %type <expression> method_call_header
250 %type <n> assignment_operator
251 %type <n> unary_operator
252 %type <expression> function_identifier
253 %type <node> external_declaration
254 %type <declarator_list> init_declarator_list
255 %type <declarator_list> single_declaration
256 %type <expression> initializer
257 %type <expression> initializer_list
258 %type <node> declaration
259 %type <node> declaration_statement
260 %type <node> jump_statement
261 %type <node> interface_block
262 %type <interface_block> basic_interface_block
263 %type <struct_specifier> struct_specifier
264 %type <declarator_list> struct_declaration_list
265 %type <declarator_list> struct_declaration
266 %type <declaration> struct_declarator
267 %type <declaration> struct_declarator_list
268 %type <declarator_list> member_list
269 %type <declarator_list> member_declaration
270 %type <node> selection_statement
271 %type <selection_rest_statement> selection_rest_statement
272 %type <node> switch_statement
273 %type <switch_body> switch_body
274 %type <case_label_list> case_label_list
275 %type <case_label> case_label
276 %type <case_statement> case_statement
277 %type <case_statement_list> case_statement_list
278 %type <node> iteration_statement
279 %type <node> condition
280 %type <node> conditionopt
281 %type <node> for_init_statement
282 %type <for_rest_statement> for_rest_statement
283 %type <n> integer_constant
284 %type <node> layout_defaults
285
286 %right THEN ELSE
287 %%
288
289 translation_unit:
290 version_statement extension_statement_list
291 {
292 _mesa_glsl_initialize_types(state);
293 }
294 external_declaration_list
295 {
296 delete state->symbols;
297 state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
298 _mesa_glsl_initialize_types(state);
299 }
300 ;
301
302 version_statement:
303 /* blank - no #version specified: defaults are already set */
304 | VERSION_TOK INTCONSTANT EOL
305 {
306 state->process_version_directive(&@2, $2, NULL);
307 if (state->error) {
308 YYERROR;
309 }
310 }
311 | VERSION_TOK INTCONSTANT any_identifier EOL
312 {
313 state->process_version_directive(&@2, $2, $3);
314 if (state->error) {
315 YYERROR;
316 }
317 }
318 ;
319
320 pragma_statement:
321 PRAGMA_DEBUG_ON EOL
322 | PRAGMA_DEBUG_OFF EOL
323 | PRAGMA_OPTIMIZE_ON EOL
324 | PRAGMA_OPTIMIZE_OFF EOL
325 | PRAGMA_INVARIANT_ALL EOL
326 {
327 if (!state->is_version(120, 100)) {
328 _mesa_glsl_warning(& @1, state,
329 "pragma `invariant(all)' not supported in %s "
330 "(GLSL ES 1.00 or GLSL 1.20 required)",
331 state->get_version_string());
332 } else {
333 state->all_invariant = true;
334 }
335 }
336 ;
337
338 extension_statement_list:
339
340 | extension_statement_list extension_statement
341 ;
342
343 any_identifier:
344 IDENTIFIER
345 | TYPE_IDENTIFIER
346 | NEW_IDENTIFIER
347 ;
348
349 extension_statement:
350 EXTENSION any_identifier COLON any_identifier EOL
351 {
352 if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
353 YYERROR;
354 }
355 }
356 ;
357
358 external_declaration_list:
359 external_declaration
360 {
361 /* FINISHME: The NULL test is required because pragmas are set to
362 * FINISHME: NULL. (See production rule for external_declaration.)
363 */
364 if ($1 != NULL)
365 state->translation_unit.push_tail(& $1->link);
366 }
367 | external_declaration_list external_declaration
368 {
369 /* FINISHME: The NULL test is required because pragmas are set to
370 * FINISHME: NULL. (See production rule for external_declaration.)
371 */
372 if ($2 != NULL)
373 state->translation_unit.push_tail(& $2->link);
374 }
375 ;
376
377 variable_identifier:
378 IDENTIFIER
379 | NEW_IDENTIFIER
380 ;
381
382 primary_expression:
383 variable_identifier
384 {
385 void *ctx = state;
386 $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
387 $$->set_location(yylloc);
388 $$->primary_expression.identifier = $1;
389 }
390 | INTCONSTANT
391 {
392 void *ctx = state;
393 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
394 $$->set_location(yylloc);
395 $$->primary_expression.int_constant = $1;
396 }
397 | UINTCONSTANT
398 {
399 void *ctx = state;
400 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
401 $$->set_location(yylloc);
402 $$->primary_expression.uint_constant = $1;
403 }
404 | FLOATCONSTANT
405 {
406 void *ctx = state;
407 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
408 $$->set_location(yylloc);
409 $$->primary_expression.float_constant = $1;
410 }
411 | BOOLCONSTANT
412 {
413 void *ctx = state;
414 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
415 $$->set_location(yylloc);
416 $$->primary_expression.bool_constant = $1;
417 }
418 | '(' expression ')'
419 {
420 $$ = $2;
421 }
422 ;
423
424 postfix_expression:
425 primary_expression
426 | postfix_expression '[' integer_expression ']'
427 {
428 void *ctx = state;
429 $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
430 $$->set_location(yylloc);
431 }
432 | function_call
433 {
434 $$ = $1;
435 }
436 | postfix_expression '.' any_identifier
437 {
438 void *ctx = state;
439 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
440 $$->set_location(yylloc);
441 $$->primary_expression.identifier = $3;
442 }
443 | postfix_expression INC_OP
444 {
445 void *ctx = state;
446 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
447 $$->set_location(yylloc);
448 }
449 | postfix_expression DEC_OP
450 {
451 void *ctx = state;
452 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
453 $$->set_location(yylloc);
454 }
455 ;
456
457 integer_expression:
458 expression
459 ;
460
461 function_call:
462 function_call_or_method
463 ;
464
465 function_call_or_method:
466 function_call_generic
467 | postfix_expression '.' method_call_generic
468 {
469 void *ctx = state;
470 $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
471 $$->set_location(yylloc);
472 }
473 ;
474
475 function_call_generic:
476 function_call_header_with_parameters ')'
477 | function_call_header_no_parameters ')'
478 ;
479
480 function_call_header_no_parameters:
481 function_call_header VOID_TOK
482 | function_call_header
483 ;
484
485 function_call_header_with_parameters:
486 function_call_header assignment_expression
487 {
488 $$ = $1;
489 $$->set_location(yylloc);
490 $$->expressions.push_tail(& $2->link);
491 }
492 | function_call_header_with_parameters ',' assignment_expression
493 {
494 $$ = $1;
495 $$->set_location(yylloc);
496 $$->expressions.push_tail(& $3->link);
497 }
498 ;
499
500 // Grammar Note: Constructors look like functions, but lexical
501 // analysis recognized most of them as keywords. They are now
502 // recognized through "type_specifier".
503 function_call_header:
504 function_identifier '('
505 ;
506
507 function_identifier:
508 type_specifier
509 {
510 void *ctx = state;
511 $$ = new(ctx) ast_function_expression($1);
512 $$->set_location(yylloc);
513 }
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 | FIELD_SELECTION
522 {
523 void *ctx = state;
524 ast_expression *callee = new(ctx) ast_expression($1);
525 $$ = new(ctx) ast_function_expression(callee);
526 $$->set_location(yylloc);
527 }
528 ;
529
530 method_call_generic:
531 method_call_header_with_parameters ')'
532 | method_call_header_no_parameters ')'
533 ;
534
535 method_call_header_no_parameters:
536 method_call_header VOID_TOK
537 | method_call_header
538 ;
539
540 method_call_header_with_parameters:
541 method_call_header assignment_expression
542 {
543 $$ = $1;
544 $$->set_location(yylloc);
545 $$->expressions.push_tail(& $2->link);
546 }
547 | method_call_header_with_parameters ',' assignment_expression
548 {
549 $$ = $1;
550 $$->set_location(yylloc);
551 $$->expressions.push_tail(& $3->link);
552 }
553 ;
554
555 // Grammar Note: Constructors look like methods, but lexical
556 // analysis recognized most of them as keywords. They are now
557 // recognized through "type_specifier".
558 method_call_header:
559 variable_identifier '('
560 {
561 void *ctx = state;
562 ast_expression *callee = new(ctx) ast_expression($1);
563 $$ = new(ctx) ast_function_expression(callee);
564 $$->set_location(yylloc);
565 }
566 ;
567
568 // Grammar Note: No traditional style type casts.
569 unary_expression:
570 postfix_expression
571 | INC_OP unary_expression
572 {
573 void *ctx = state;
574 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
575 $$->set_location(yylloc);
576 }
577 | DEC_OP unary_expression
578 {
579 void *ctx = state;
580 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
581 $$->set_location(yylloc);
582 }
583 | unary_operator unary_expression
584 {
585 void *ctx = state;
586 $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
587 $$->set_location(yylloc);
588 }
589 ;
590
591 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
592 unary_operator:
593 '+' { $$ = ast_plus; }
594 | '-' { $$ = ast_neg; }
595 | '!' { $$ = ast_logic_not; }
596 | '~' { $$ = ast_bit_not; }
597 ;
598
599 multiplicative_expression:
600 unary_expression
601 | multiplicative_expression '*' unary_expression
602 {
603 void *ctx = state;
604 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
605 $$->set_location(yylloc);
606 }
607 | multiplicative_expression '/' unary_expression
608 {
609 void *ctx = state;
610 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
611 $$->set_location(yylloc);
612 }
613 | multiplicative_expression '%' unary_expression
614 {
615 void *ctx = state;
616 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
617 $$->set_location(yylloc);
618 }
619 ;
620
621 additive_expression:
622 multiplicative_expression
623 | additive_expression '+' multiplicative_expression
624 {
625 void *ctx = state;
626 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
627 $$->set_location(yylloc);
628 }
629 | additive_expression '-' multiplicative_expression
630 {
631 void *ctx = state;
632 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
633 $$->set_location(yylloc);
634 }
635 ;
636
637 shift_expression:
638 additive_expression
639 | shift_expression LEFT_OP additive_expression
640 {
641 void *ctx = state;
642 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
643 $$->set_location(yylloc);
644 }
645 | shift_expression RIGHT_OP additive_expression
646 {
647 void *ctx = state;
648 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
649 $$->set_location(yylloc);
650 }
651 ;
652
653 relational_expression:
654 shift_expression
655 | relational_expression '<' shift_expression
656 {
657 void *ctx = state;
658 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
659 $$->set_location(yylloc);
660 }
661 | relational_expression '>' shift_expression
662 {
663 void *ctx = state;
664 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
665 $$->set_location(yylloc);
666 }
667 | relational_expression LE_OP shift_expression
668 {
669 void *ctx = state;
670 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
671 $$->set_location(yylloc);
672 }
673 | relational_expression GE_OP shift_expression
674 {
675 void *ctx = state;
676 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
677 $$->set_location(yylloc);
678 }
679 ;
680
681 equality_expression:
682 relational_expression
683 | equality_expression EQ_OP relational_expression
684 {
685 void *ctx = state;
686 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
687 $$->set_location(yylloc);
688 }
689 | equality_expression NE_OP relational_expression
690 {
691 void *ctx = state;
692 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
693 $$->set_location(yylloc);
694 }
695 ;
696
697 and_expression:
698 equality_expression
699 | and_expression '&' equality_expression
700 {
701 void *ctx = state;
702 $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
703 $$->set_location(yylloc);
704 }
705 ;
706
707 exclusive_or_expression:
708 and_expression
709 | exclusive_or_expression '^' and_expression
710 {
711 void *ctx = state;
712 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
713 $$->set_location(yylloc);
714 }
715 ;
716
717 inclusive_or_expression:
718 exclusive_or_expression
719 | inclusive_or_expression '|' exclusive_or_expression
720 {
721 void *ctx = state;
722 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
723 $$->set_location(yylloc);
724 }
725 ;
726
727 logical_and_expression:
728 inclusive_or_expression
729 | logical_and_expression AND_OP inclusive_or_expression
730 {
731 void *ctx = state;
732 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
733 $$->set_location(yylloc);
734 }
735 ;
736
737 logical_xor_expression:
738 logical_and_expression
739 | logical_xor_expression XOR_OP logical_and_expression
740 {
741 void *ctx = state;
742 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
743 $$->set_location(yylloc);
744 }
745 ;
746
747 logical_or_expression:
748 logical_xor_expression
749 | logical_or_expression OR_OP logical_xor_expression
750 {
751 void *ctx = state;
752 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
753 $$->set_location(yylloc);
754 }
755 ;
756
757 conditional_expression:
758 logical_or_expression
759 | logical_or_expression '?' expression ':' assignment_expression
760 {
761 void *ctx = state;
762 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
763 $$->set_location(yylloc);
764 }
765 ;
766
767 assignment_expression:
768 conditional_expression
769 | unary_expression assignment_operator assignment_expression
770 {
771 void *ctx = state;
772 $$ = new(ctx) ast_expression($2, $1, $3, NULL);
773 $$->set_location(yylloc);
774 }
775 ;
776
777 assignment_operator:
778 '=' { $$ = ast_assign; }
779 | MUL_ASSIGN { $$ = ast_mul_assign; }
780 | DIV_ASSIGN { $$ = ast_div_assign; }
781 | MOD_ASSIGN { $$ = ast_mod_assign; }
782 | ADD_ASSIGN { $$ = ast_add_assign; }
783 | SUB_ASSIGN { $$ = ast_sub_assign; }
784 | LEFT_ASSIGN { $$ = ast_ls_assign; }
785 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
786 | AND_ASSIGN { $$ = ast_and_assign; }
787 | XOR_ASSIGN { $$ = ast_xor_assign; }
788 | OR_ASSIGN { $$ = ast_or_assign; }
789 ;
790
791 expression:
792 assignment_expression
793 {
794 $$ = $1;
795 }
796 | expression ',' assignment_expression
797 {
798 void *ctx = state;
799 if ($1->oper != ast_sequence) {
800 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
801 $$->set_location(yylloc);
802 $$->expressions.push_tail(& $1->link);
803 } else {
804 $$ = $1;
805 }
806
807 $$->expressions.push_tail(& $3->link);
808 }
809 ;
810
811 constant_expression:
812 conditional_expression
813 ;
814
815 declaration:
816 function_prototype ';'
817 {
818 state->symbols->pop_scope();
819 $$ = $1;
820 }
821 | init_declarator_list ';'
822 {
823 $$ = $1;
824 }
825 | PRECISION precision_qualifier type_specifier ';'
826 {
827 $3->default_precision = $2;
828 $$ = $3;
829 }
830 | interface_block
831 {
832 $$ = $1;
833 }
834 ;
835
836 function_prototype:
837 function_declarator ')'
838 ;
839
840 function_declarator:
841 function_header
842 | function_header_with_parameters
843 ;
844
845 function_header_with_parameters:
846 function_header parameter_declaration
847 {
848 $$ = $1;
849 $$->parameters.push_tail(& $2->link);
850 }
851 | function_header_with_parameters ',' parameter_declaration
852 {
853 $$ = $1;
854 $$->parameters.push_tail(& $3->link);
855 }
856 ;
857
858 function_header:
859 fully_specified_type variable_identifier '('
860 {
861 void *ctx = state;
862 $$ = new(ctx) ast_function();
863 $$->set_location(yylloc);
864 $$->return_type = $1;
865 $$->identifier = $2;
866
867 state->symbols->add_function(new(state) ir_function($2));
868 state->symbols->push_scope();
869 }
870 ;
871
872 parameter_declarator:
873 type_specifier any_identifier
874 {
875 void *ctx = state;
876 $$ = new(ctx) ast_parameter_declarator();
877 $$->set_location(yylloc);
878 $$->type = new(ctx) ast_fully_specified_type();
879 $$->type->set_location(yylloc);
880 $$->type->specifier = $1;
881 $$->identifier = $2;
882 }
883 | type_specifier any_identifier '[' constant_expression ']'
884 {
885 void *ctx = state;
886 $$ = new(ctx) ast_parameter_declarator();
887 $$->set_location(yylloc);
888 $$->type = new(ctx) ast_fully_specified_type();
889 $$->type->set_location(yylloc);
890 $$->type->specifier = $1;
891 $$->identifier = $2;
892 $$->is_array = true;
893 $$->array_size = $4;
894 }
895 ;
896
897 parameter_declaration:
898 parameter_qualifier parameter_declarator
899 {
900 $$ = $2;
901 $$->type->qualifier = $1;
902 }
903 | parameter_qualifier parameter_type_specifier
904 {
905 void *ctx = state;
906 $$ = new(ctx) ast_parameter_declarator();
907 $$->set_location(yylloc);
908 $$->type = new(ctx) ast_fully_specified_type();
909 $$->type->qualifier = $1;
910 $$->type->specifier = $2;
911 }
912 ;
913
914 parameter_qualifier:
915 /* empty */
916 {
917 memset(& $$, 0, sizeof($$));
918 }
919 | CONST_TOK parameter_qualifier
920 {
921 if ($2.flags.q.constant)
922 _mesa_glsl_error(&@1, state, "duplicate const qualifier");
923
924 $$ = $2;
925 $$.flags.q.constant = 1;
926 }
927 | parameter_direction_qualifier parameter_qualifier
928 {
929 if (($1.flags.q.in || $1.flags.q.out) && ($2.flags.q.in || $2.flags.q.out))
930 _mesa_glsl_error(&@1, state, "duplicate in/out/inout qualifier");
931
932 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.constant)
933 _mesa_glsl_error(&@1, state, "const must be specified before "
934 "in/out/inout");
935
936 $$ = $1;
937 $$.merge_qualifier(&@1, state, $2);
938 }
939 | precision_qualifier parameter_qualifier
940 {
941 if ($2.precision != ast_precision_none)
942 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
943
944 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
945 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
946
947 $$ = $2;
948 $$.precision = $1;
949 }
950
951 parameter_direction_qualifier:
952 IN_TOK
953 {
954 memset(& $$, 0, sizeof($$));
955 $$.flags.q.in = 1;
956 }
957 | OUT_TOK
958 {
959 memset(& $$, 0, sizeof($$));
960 $$.flags.q.out = 1;
961 }
962 | INOUT_TOK
963 {
964 memset(& $$, 0, sizeof($$));
965 $$.flags.q.in = 1;
966 $$.flags.q.out = 1;
967 }
968 ;
969
970 parameter_type_specifier:
971 type_specifier
972 ;
973
974 init_declarator_list:
975 single_declaration
976 | init_declarator_list ',' any_identifier
977 {
978 void *ctx = state;
979 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
980 decl->set_location(yylloc);
981
982 $$ = $1;
983 $$->declarations.push_tail(&decl->link);
984 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
985 }
986 | init_declarator_list ',' any_identifier '[' ']'
987 {
988 void *ctx = state;
989 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
990 decl->set_location(yylloc);
991
992 $$ = $1;
993 $$->declarations.push_tail(&decl->link);
994 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
995 }
996 | init_declarator_list ',' any_identifier '[' constant_expression ']'
997 {
998 void *ctx = state;
999 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
1000 decl->set_location(yylloc);
1001
1002 $$ = $1;
1003 $$->declarations.push_tail(&decl->link);
1004 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1005 }
1006 | init_declarator_list ',' any_identifier '[' ']' '=' initializer
1007 {
1008 void *ctx = state;
1009 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7);
1010 decl->set_location(yylloc);
1011
1012 $$ = $1;
1013 $$->declarations.push_tail(&decl->link);
1014 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1015 if ($7->oper == ast_aggregate) {
1016 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$7;
1017 ast_type_specifier *type = new(ctx) ast_type_specifier($1->type->specifier, true, NULL);
1018 _mesa_ast_set_aggregate_type(type, ai, state);
1019 }
1020 }
1021 | init_declarator_list ',' any_identifier '[' constant_expression ']' '=' initializer
1022 {
1023 void *ctx = state;
1024 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
1025 decl->set_location(yylloc);
1026
1027 $$ = $1;
1028 $$->declarations.push_tail(&decl->link);
1029 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1030 if ($8->oper == ast_aggregate) {
1031 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$8;
1032 ast_type_specifier *type = new(ctx) ast_type_specifier($1->type->specifier, true, $5);
1033 _mesa_ast_set_aggregate_type(type, ai, state);
1034 }
1035 }
1036 | init_declarator_list ',' any_identifier '=' initializer
1037 {
1038 void *ctx = state;
1039 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
1040 decl->set_location(yylloc);
1041
1042 $$ = $1;
1043 $$->declarations.push_tail(&decl->link);
1044 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1045 if ($5->oper == ast_aggregate) {
1046 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$5;
1047 _mesa_ast_set_aggregate_type($1->type->specifier, ai, state);
1048 }
1049 }
1050 ;
1051
1052 // Grammar Note: No 'enum', or 'typedef'.
1053 single_declaration:
1054 fully_specified_type
1055 {
1056 void *ctx = state;
1057 /* Empty declaration list is valid. */
1058 $$ = new(ctx) ast_declarator_list($1);
1059 $$->set_location(yylloc);
1060 }
1061 | fully_specified_type any_identifier
1062 {
1063 void *ctx = state;
1064 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1065
1066 $$ = new(ctx) ast_declarator_list($1);
1067 $$->set_location(yylloc);
1068 $$->declarations.push_tail(&decl->link);
1069 }
1070 | fully_specified_type any_identifier '[' ']'
1071 {
1072 void *ctx = state;
1073 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
1074
1075 $$ = new(ctx) ast_declarator_list($1);
1076 $$->set_location(yylloc);
1077 $$->declarations.push_tail(&decl->link);
1078 }
1079 | fully_specified_type any_identifier '[' constant_expression ']'
1080 {
1081 void *ctx = state;
1082 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
1083
1084 $$ = new(ctx) ast_declarator_list($1);
1085 $$->set_location(yylloc);
1086 $$->declarations.push_tail(&decl->link);
1087 }
1088 | fully_specified_type any_identifier '[' ']' '=' initializer
1089 {
1090 void *ctx = state;
1091 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6);
1092
1093 $$ = new(ctx) ast_declarator_list($1);
1094 $$->set_location(yylloc);
1095 $$->declarations.push_tail(&decl->link);
1096 if ($6->oper == ast_aggregate) {
1097 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$6;
1098 ast_type_specifier *type = new(ctx) ast_type_specifier($1->specifier, true, NULL);
1099 _mesa_ast_set_aggregate_type(type, ai, state);
1100 }
1101 }
1102 | fully_specified_type any_identifier '[' constant_expression ']' '=' initializer
1103 {
1104 void *ctx = state;
1105 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
1106
1107 $$ = new(ctx) ast_declarator_list($1);
1108 $$->set_location(yylloc);
1109 $$->declarations.push_tail(&decl->link);
1110 if ($7->oper == ast_aggregate) {
1111 ast_aggregate_initializer *ai = (ast_aggregate_initializer *)$7;
1112 ast_type_specifier *type = new(ctx) ast_type_specifier($1->specifier, true, $4);
1113 _mesa_ast_set_aggregate_type(type, ai, state);
1114 }
1115 }
1116 | fully_specified_type any_identifier '=' initializer
1117 {
1118 void *ctx = state;
1119 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1120
1121 $$ = new(ctx) ast_declarator_list($1);
1122 $$->set_location(yylloc);
1123 $$->declarations.push_tail(&decl->link);
1124 if ($4->oper == ast_aggregate) {
1125 _mesa_ast_set_aggregate_type($1->specifier, $4, state);
1126 }
1127 }
1128 | INVARIANT variable_identifier // Vertex only.
1129 {
1130 void *ctx = state;
1131 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1132
1133 $$ = new(ctx) ast_declarator_list(NULL);
1134 $$->set_location(yylloc);
1135 $$->invariant = true;
1136
1137 $$->declarations.push_tail(&decl->link);
1138 }
1139 ;
1140
1141 fully_specified_type:
1142 type_specifier
1143 {
1144 void *ctx = state;
1145 $$ = new(ctx) ast_fully_specified_type();
1146 $$->set_location(yylloc);
1147 $$->specifier = $1;
1148 }
1149 | type_qualifier type_specifier
1150 {
1151 void *ctx = state;
1152 $$ = new(ctx) ast_fully_specified_type();
1153 $$->set_location(yylloc);
1154 $$->qualifier = $1;
1155 $$->specifier = $2;
1156 }
1157 ;
1158
1159 layout_qualifier:
1160 LAYOUT_TOK '(' layout_qualifier_id_list ')'
1161 {
1162 $$ = $3;
1163 }
1164 ;
1165
1166 layout_qualifier_id_list:
1167 layout_qualifier_id
1168 | layout_qualifier_id_list ',' layout_qualifier_id
1169 {
1170 $$ = $1;
1171 if (!$$.merge_qualifier(& @3, state, $3)) {
1172 YYERROR;
1173 }
1174 }
1175 ;
1176
1177 integer_constant:
1178 INTCONSTANT { $$ = $1; }
1179 | UINTCONSTANT { $$ = $1; }
1180 ;
1181
1182 layout_qualifier_id:
1183 any_identifier
1184 {
1185 memset(& $$, 0, sizeof($$));
1186
1187 /* Layout qualifiers for ARB_fragment_coord_conventions. */
1188 if (!$$.flags.i && (state->ARB_fragment_coord_conventions_enable ||
1189 state->is_version(150, 0))) {
1190 if (match_layout_qualifier($1, "origin_upper_left", state) == 0) {
1191 $$.flags.q.origin_upper_left = 1;
1192 } else if (match_layout_qualifier($1, "pixel_center_integer",
1193 state) == 0) {
1194 $$.flags.q.pixel_center_integer = 1;
1195 }
1196
1197 if ($$.flags.i && state->ARB_fragment_coord_conventions_warn) {
1198 _mesa_glsl_warning(& @1, state,
1199 "GL_ARB_fragment_coord_conventions layout "
1200 "identifier `%s' used", $1);
1201 }
1202 }
1203
1204 /* Layout qualifiers for AMD/ARB_conservative_depth. */
1205 if (!$$.flags.i &&
1206 (state->AMD_conservative_depth_enable ||
1207 state->ARB_conservative_depth_enable)) {
1208 if (match_layout_qualifier($1, "depth_any", state) == 0) {
1209 $$.flags.q.depth_any = 1;
1210 } else if (match_layout_qualifier($1, "depth_greater", state) == 0) {
1211 $$.flags.q.depth_greater = 1;
1212 } else if (match_layout_qualifier($1, "depth_less", state) == 0) {
1213 $$.flags.q.depth_less = 1;
1214 } else if (match_layout_qualifier($1, "depth_unchanged",
1215 state) == 0) {
1216 $$.flags.q.depth_unchanged = 1;
1217 }
1218
1219 if ($$.flags.i && state->AMD_conservative_depth_warn) {
1220 _mesa_glsl_warning(& @1, state,
1221 "GL_AMD_conservative_depth "
1222 "layout qualifier `%s' is used", $1);
1223 }
1224 if ($$.flags.i && state->ARB_conservative_depth_warn) {
1225 _mesa_glsl_warning(& @1, state,
1226 "GL_ARB_conservative_depth "
1227 "layout qualifier `%s' is used", $1);
1228 }
1229 }
1230
1231 /* See also interface_block_layout_qualifier. */
1232 if (!$$.flags.i && state->has_uniform_buffer_objects()) {
1233 if (match_layout_qualifier($1, "std140", state) == 0) {
1234 $$.flags.q.std140 = 1;
1235 } else if (match_layout_qualifier($1, "shared", state) == 0) {
1236 $$.flags.q.shared = 1;
1237 } else if (match_layout_qualifier($1, "column_major", state) == 0) {
1238 $$.flags.q.column_major = 1;
1239 /* "row_major" is a reserved word in GLSL 1.30+. Its token is parsed
1240 * below in the interface_block_layout_qualifier rule.
1241 *
1242 * It is not a reserved word in GLSL ES 3.00, so it's handled here as
1243 * an identifier.
1244 *
1245 * Also, this takes care of alternate capitalizations of
1246 * "row_major" (which is necessary because layout qualifiers
1247 * are case-insensitive in desktop GLSL).
1248 */
1249 } else if (match_layout_qualifier($1, "row_major", state) == 0) {
1250 $$.flags.q.row_major = 1;
1251 /* "packed" is a reserved word in GLSL, and its token is
1252 * parsed below in the interface_block_layout_qualifier rule.
1253 * However, we must take care of alternate capitalizations of
1254 * "packed", because layout qualifiers are case-insensitive
1255 * in desktop GLSL.
1256 */
1257 } else if (match_layout_qualifier($1, "packed", state) == 0) {
1258 $$.flags.q.packed = 1;
1259 }
1260
1261 if ($$.flags.i && state->ARB_uniform_buffer_object_warn) {
1262 _mesa_glsl_warning(& @1, state,
1263 "#version 140 / GL_ARB_uniform_buffer_object "
1264 "layout qualifier `%s' is used", $1);
1265 }
1266 }
1267
1268 /* Layout qualifiers for GLSL 1.50 geometry shaders. */
1269 if (!$$.flags.i) {
1270 struct {
1271 const char *s;
1272 GLenum e;
1273 } map[] = {
1274 { "points", GL_POINTS },
1275 { "lines", GL_LINES },
1276 { "lines_adjacency", GL_LINES_ADJACENCY },
1277 { "line_strip", GL_LINE_STRIP },
1278 { "triangles", GL_TRIANGLES },
1279 { "triangles_adjacency", GL_TRIANGLES_ADJACENCY },
1280 { "triangle_strip", GL_TRIANGLE_STRIP },
1281 };
1282 for (unsigned i = 0; i < Elements(map); i++) {
1283 if (match_layout_qualifier($1, map[i].s, state) == 0) {
1284 $$.flags.q.prim_type = 1;
1285 $$.prim_type = map[i].e;
1286 break;
1287 }
1288 }
1289
1290 if ($$.flags.i && !state->is_version(150, 0)) {
1291 _mesa_glsl_error(& @1, state, "#version 150 layout "
1292 "qualifier `%s' used", $1);
1293 }
1294 }
1295
1296 if (!$$.flags.i) {
1297 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1298 "`%s'", $1);
1299 YYERROR;
1300 }
1301 }
1302 | any_identifier '=' integer_constant
1303 {
1304 memset(& $$, 0, sizeof($$));
1305
1306 if (match_layout_qualifier("location", $1, state) == 0) {
1307 $$.flags.q.explicit_location = 1;
1308
1309 if ($3 >= 0) {
1310 $$.location = $3;
1311 } else {
1312 _mesa_glsl_error(& @3, state, "invalid location %d specified", $3);
1313 YYERROR;
1314 }
1315 }
1316
1317 if (match_layout_qualifier("index", $1, state) == 0) {
1318 $$.flags.q.explicit_index = 1;
1319
1320 if ($3 >= 0) {
1321 $$.index = $3;
1322 } else {
1323 _mesa_glsl_error(& @3, state, "invalid index %d specified", $3);
1324 YYERROR;
1325 }
1326 }
1327
1328 if ((state->ARB_shading_language_420pack_enable ||
1329 state->ARB_shader_atomic_counters_enable) &&
1330 match_layout_qualifier("binding", $1, state) == 0) {
1331 $$.flags.q.explicit_binding = 1;
1332 $$.binding = $3;
1333 }
1334
1335 if (state->ARB_shader_atomic_counters_enable &&
1336 match_layout_qualifier("offset", $1, state) == 0) {
1337 $$.flags.q.explicit_offset = 1;
1338 $$.offset = $3;
1339 }
1340
1341 if (match_layout_qualifier("max_vertices", $1, state) == 0) {
1342 $$.flags.q.max_vertices = 1;
1343
1344 if ($3 < 0) {
1345 _mesa_glsl_error(& @3, state,
1346 "invalid max_vertices %d specified", $3);
1347 YYERROR;
1348 } else {
1349 $$.max_vertices = $3;
1350 if (!state->is_version(150, 0)) {
1351 _mesa_glsl_error(& @3, state,
1352 "#version 150 max_vertices qualifier "
1353 "specified", $3);
1354 }
1355 }
1356 }
1357
1358 /* If the identifier didn't match any known layout identifiers,
1359 * emit an error.
1360 */
1361 if (!$$.flags.i) {
1362 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1363 "`%s'", $1);
1364 YYERROR;
1365 } else if (state->ARB_explicit_attrib_location_warn) {
1366 _mesa_glsl_warning(& @1, state,
1367 "GL_ARB_explicit_attrib_location layout "
1368 "identifier `%s' used", $1);
1369 }
1370 }
1371 | interface_block_layout_qualifier
1372 {
1373 $$ = $1;
1374 /* Layout qualifiers for ARB_uniform_buffer_object. */
1375 if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
1376 _mesa_glsl_error(& @1, state,
1377 "#version 140 / GL_ARB_uniform_buffer_object "
1378 "layout qualifier `%s' is used", $1);
1379 } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1380 _mesa_glsl_warning(& @1, state,
1381 "#version 140 / GL_ARB_uniform_buffer_object "
1382 "layout qualifier `%s' is used", $1);
1383 }
1384 }
1385 ;
1386
1387 /* This is a separate language rule because we parse these as tokens
1388 * (due to them being reserved keywords) instead of identifiers like
1389 * most qualifiers. See the any_identifier path of
1390 * layout_qualifier_id for the others.
1391 *
1392 * Note that since layout qualifiers are case-insensitive in desktop
1393 * GLSL, all of these qualifiers need to be handled as identifiers as
1394 * well (by the any_identifier path of layout_qualifier_id).
1395 */
1396 interface_block_layout_qualifier:
1397 ROW_MAJOR
1398 {
1399 memset(& $$, 0, sizeof($$));
1400 $$.flags.q.row_major = 1;
1401 }
1402 | PACKED_TOK
1403 {
1404 memset(& $$, 0, sizeof($$));
1405 $$.flags.q.packed = 1;
1406 }
1407 ;
1408
1409 interpolation_qualifier:
1410 SMOOTH
1411 {
1412 memset(& $$, 0, sizeof($$));
1413 $$.flags.q.smooth = 1;
1414 }
1415 | FLAT
1416 {
1417 memset(& $$, 0, sizeof($$));
1418 $$.flags.q.flat = 1;
1419 }
1420 | NOPERSPECTIVE
1421 {
1422 memset(& $$, 0, sizeof($$));
1423 $$.flags.q.noperspective = 1;
1424 }
1425 ;
1426
1427 type_qualifier:
1428 /* Single qualifiers */
1429 INVARIANT
1430 {
1431 memset(& $$, 0, sizeof($$));
1432 $$.flags.q.invariant = 1;
1433 }
1434 | auxiliary_storage_qualifier
1435 | storage_qualifier
1436 | interpolation_qualifier
1437 | layout_qualifier
1438 | precision_qualifier
1439 {
1440 memset(&$$, 0, sizeof($$));
1441 $$.precision = $1;
1442 }
1443
1444 /* Multiple qualifiers:
1445 * In GLSL 4.20, these can be specified in any order. In earlier versions,
1446 * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1447 *
1448 * invariant interpolation auxiliary storage precision ...or...
1449 * layout storage precision
1450 *
1451 * Each qualifier's rule ensures that the accumulated qualifiers on the right
1452 * side don't contain any that must appear on the left hand side.
1453 * For example, when processing a storage qualifier, we check that there are
1454 * no auxiliary, interpolation, layout, or invariant qualifiers to the right.
1455 */
1456 | INVARIANT type_qualifier
1457 {
1458 if ($2.flags.q.invariant)
1459 _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1460
1461 if ($2.has_layout()) {
1462 _mesa_glsl_error(&@1, state,
1463 "\"invariant\" cannot be used with layout(...)");
1464 }
1465
1466 $$ = $2;
1467 $$.flags.q.invariant = 1;
1468 }
1469 | interpolation_qualifier type_qualifier
1470 {
1471 /* Section 4.3 of the GLSL 1.40 specification states:
1472 * "...qualified with one of these interpolation qualifiers"
1473 *
1474 * GLSL 1.30 claims to allow "one or more", but insists that:
1475 * "These interpolation qualifiers may only precede the qualifiers in,
1476 * centroid in, out, or centroid out in a declaration."
1477 *
1478 * ...which means that e.g. smooth can't precede smooth, so there can be
1479 * only one after all, and the 1.40 text is a clarification, not a change.
1480 */
1481 if ($2.has_interpolation())
1482 _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1483
1484 if ($2.has_layout()) {
1485 _mesa_glsl_error(&@1, state, "interpolation qualifiers cannot be used "
1486 "with layout(...)");
1487 }
1488
1489 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.invariant) {
1490 _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1491 "after \"invariant\"");
1492 }
1493
1494 $$ = $1;
1495 $$.merge_qualifier(&@1, state, $2);
1496 }
1497 | layout_qualifier type_qualifier
1498 {
1499 /* The GLSL 1.50 grammar indicates that a layout(...) declaration can be
1500 * used standalone or immediately before a storage qualifier. It cannot
1501 * be used with interpolation qualifiers or invariant. There does not
1502 * appear to be any text indicating that it must come before the storage
1503 * qualifier, but always seems to in examples.
1504 */
1505 if (!state->ARB_shading_language_420pack_enable && $2.has_layout())
1506 _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
1507
1508 if ($2.flags.q.invariant)
1509 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1510 "the \"invariant\" qualifier");
1511
1512 if ($2.has_interpolation()) {
1513 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1514 "interpolation qualifiers");
1515 }
1516
1517 $$ = $1;
1518 $$.merge_qualifier(&@1, state, $2);
1519 }
1520 | auxiliary_storage_qualifier type_qualifier
1521 {
1522 if ($2.has_auxiliary_storage()) {
1523 _mesa_glsl_error(&@1, state,
1524 "duplicate auxiliary storage qualifier (centroid)");
1525 }
1526
1527 if (!state->ARB_shading_language_420pack_enable &&
1528 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout())) {
1529 _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
1530 "just before storage qualifiers");
1531 }
1532 $$ = $1;
1533 $$.flags.i |= $2.flags.i;
1534 }
1535 | storage_qualifier type_qualifier
1536 {
1537 /* Section 4.3 of the GLSL 1.20 specification states:
1538 * "Variable declarations may have a storage qualifier specified..."
1539 * 1.30 clarifies this to "may have one storage qualifier".
1540 */
1541 if ($2.has_storage())
1542 _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
1543
1544 if (!state->ARB_shading_language_420pack_enable &&
1545 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout() ||
1546 $2.has_auxiliary_storage())) {
1547 _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
1548 "invariant, interpolation, layout and auxiliary "
1549 "storage qualifiers");
1550 }
1551
1552 $$ = $1;
1553 $$.merge_qualifier(&@1, state, $2);
1554 }
1555 | precision_qualifier type_qualifier
1556 {
1557 if ($2.precision != ast_precision_none)
1558 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
1559
1560 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
1561 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
1562
1563 $$ = $2;
1564 $$.precision = $1;
1565 }
1566 ;
1567
1568 auxiliary_storage_qualifier:
1569 CENTROID
1570 {
1571 memset(& $$, 0, sizeof($$));
1572 $$.flags.q.centroid = 1;
1573 }
1574 /* TODO: "sample" and "patch" also go here someday. */
1575
1576 storage_qualifier:
1577 CONST_TOK
1578 {
1579 memset(& $$, 0, sizeof($$));
1580 $$.flags.q.constant = 1;
1581 }
1582 | ATTRIBUTE
1583 {
1584 memset(& $$, 0, sizeof($$));
1585 $$.flags.q.attribute = 1;
1586 }
1587 | VARYING
1588 {
1589 memset(& $$, 0, sizeof($$));
1590 $$.flags.q.varying = 1;
1591 }
1592 | IN_TOK
1593 {
1594 memset(& $$, 0, sizeof($$));
1595 $$.flags.q.in = 1;
1596 }
1597 | OUT_TOK
1598 {
1599 memset(& $$, 0, sizeof($$));
1600 $$.flags.q.out = 1;
1601 }
1602 | UNIFORM
1603 {
1604 memset(& $$, 0, sizeof($$));
1605 $$.flags.q.uniform = 1;
1606 }
1607 ;
1608
1609 type_specifier:
1610 type_specifier_nonarray
1611 | type_specifier_nonarray '[' ']'
1612 {
1613 $$ = $1;
1614 $$->is_array = true;
1615 $$->array_size = NULL;
1616 }
1617 | type_specifier_nonarray '[' constant_expression ']'
1618 {
1619 $$ = $1;
1620 $$->is_array = true;
1621 $$->array_size = $3;
1622 }
1623 ;
1624
1625 type_specifier_nonarray:
1626 basic_type_specifier_nonarray
1627 {
1628 void *ctx = state;
1629 $$ = new(ctx) ast_type_specifier($1);
1630 $$->set_location(yylloc);
1631 }
1632 | struct_specifier
1633 {
1634 void *ctx = state;
1635 $$ = new(ctx) ast_type_specifier($1);
1636 $$->set_location(yylloc);
1637 }
1638 | TYPE_IDENTIFIER
1639 {
1640 void *ctx = state;
1641 $$ = new(ctx) ast_type_specifier($1);
1642 $$->set_location(yylloc);
1643 }
1644 ;
1645
1646 basic_type_specifier_nonarray:
1647 VOID_TOK { $$ = "void"; }
1648 | FLOAT_TOK { $$ = "float"; }
1649 | INT_TOK { $$ = "int"; }
1650 | UINT_TOK { $$ = "uint"; }
1651 | BOOL_TOK { $$ = "bool"; }
1652 | VEC2 { $$ = "vec2"; }
1653 | VEC3 { $$ = "vec3"; }
1654 | VEC4 { $$ = "vec4"; }
1655 | BVEC2 { $$ = "bvec2"; }
1656 | BVEC3 { $$ = "bvec3"; }
1657 | BVEC4 { $$ = "bvec4"; }
1658 | IVEC2 { $$ = "ivec2"; }
1659 | IVEC3 { $$ = "ivec3"; }
1660 | IVEC4 { $$ = "ivec4"; }
1661 | UVEC2 { $$ = "uvec2"; }
1662 | UVEC3 { $$ = "uvec3"; }
1663 | UVEC4 { $$ = "uvec4"; }
1664 | MAT2X2 { $$ = "mat2"; }
1665 | MAT2X3 { $$ = "mat2x3"; }
1666 | MAT2X4 { $$ = "mat2x4"; }
1667 | MAT3X2 { $$ = "mat3x2"; }
1668 | MAT3X3 { $$ = "mat3"; }
1669 | MAT3X4 { $$ = "mat3x4"; }
1670 | MAT4X2 { $$ = "mat4x2"; }
1671 | MAT4X3 { $$ = "mat4x3"; }
1672 | MAT4X4 { $$ = "mat4"; }
1673 | SAMPLER1D { $$ = "sampler1D"; }
1674 | SAMPLER2D { $$ = "sampler2D"; }
1675 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
1676 | SAMPLER3D { $$ = "sampler3D"; }
1677 | SAMPLERCUBE { $$ = "samplerCube"; }
1678 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
1679 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
1680 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
1681 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
1682 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
1683 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
1684 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
1685 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
1686 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
1687 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
1688 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
1689 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
1690 | ISAMPLER1D { $$ = "isampler1D"; }
1691 | ISAMPLER2D { $$ = "isampler2D"; }
1692 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
1693 | ISAMPLER3D { $$ = "isampler3D"; }
1694 | ISAMPLERCUBE { $$ = "isamplerCube"; }
1695 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
1696 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
1697 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
1698 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
1699 | USAMPLER1D { $$ = "usampler1D"; }
1700 | USAMPLER2D { $$ = "usampler2D"; }
1701 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
1702 | USAMPLER3D { $$ = "usampler3D"; }
1703 | USAMPLERCUBE { $$ = "usamplerCube"; }
1704 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
1705 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
1706 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
1707 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
1708 | SAMPLER2DMS { $$ = "sampler2DMS"; }
1709 | ISAMPLER2DMS { $$ = "isampler2DMS"; }
1710 | USAMPLER2DMS { $$ = "usampler2DMS"; }
1711 | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
1712 | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
1713 | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
1714 | ATOMIC_UINT { $$ = "atomic_uint"; }
1715 ;
1716
1717 precision_qualifier:
1718 HIGHP
1719 {
1720 state->check_precision_qualifiers_allowed(&@1);
1721 $$ = ast_precision_high;
1722 }
1723 | MEDIUMP
1724 {
1725 state->check_precision_qualifiers_allowed(&@1);
1726 $$ = ast_precision_medium;
1727 }
1728 | LOWP
1729 {
1730 state->check_precision_qualifiers_allowed(&@1);
1731 $$ = ast_precision_low;
1732 }
1733 ;
1734
1735 struct_specifier:
1736 STRUCT any_identifier '{' struct_declaration_list '}'
1737 {
1738 void *ctx = state;
1739 $$ = new(ctx) ast_struct_specifier($2, $4);
1740 $$->set_location(yylloc);
1741 state->symbols->add_type($2, glsl_type::void_type);
1742 state->symbols->add_type_ast($2, new(ctx) ast_type_specifier($$));
1743 }
1744 | STRUCT '{' struct_declaration_list '}'
1745 {
1746 void *ctx = state;
1747 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1748 $$->set_location(yylloc);
1749 }
1750 ;
1751
1752 struct_declaration_list:
1753 struct_declaration
1754 {
1755 $$ = $1;
1756 $1->link.self_link();
1757 }
1758 | struct_declaration_list struct_declaration
1759 {
1760 $$ = $1;
1761 $$->link.insert_before(& $2->link);
1762 }
1763 ;
1764
1765 struct_declaration:
1766 fully_specified_type struct_declarator_list ';'
1767 {
1768 void *ctx = state;
1769 ast_fully_specified_type *const type = $1;
1770 type->set_location(yylloc);
1771
1772 if (type->qualifier.flags.i != 0)
1773 _mesa_glsl_error(&@1, state,
1774 "only precision qualifiers may be applied to "
1775 "structure members");
1776
1777 $$ = new(ctx) ast_declarator_list(type);
1778 $$->set_location(yylloc);
1779
1780 $$->declarations.push_degenerate_list_at_head(& $2->link);
1781 }
1782 ;
1783
1784 struct_declarator_list:
1785 struct_declarator
1786 {
1787 $$ = $1;
1788 $1->link.self_link();
1789 }
1790 | struct_declarator_list ',' struct_declarator
1791 {
1792 $$ = $1;
1793 $$->link.insert_before(& $3->link);
1794 }
1795 ;
1796
1797 struct_declarator:
1798 any_identifier
1799 {
1800 void *ctx = state;
1801 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1802 $$->set_location(yylloc);
1803 }
1804 | any_identifier '[' ']'
1805 {
1806 void *ctx = state;
1807 $$ = new(ctx) ast_declaration($1, true, NULL, NULL);
1808 $$->set_location(yylloc);
1809 }
1810 | any_identifier '[' constant_expression ']'
1811 {
1812 void *ctx = state;
1813 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1814 $$->set_location(yylloc);
1815 }
1816 ;
1817
1818 initializer:
1819 assignment_expression
1820 | '{' initializer_list '}'
1821 {
1822 $$ = $2;
1823 }
1824 | '{' initializer_list ',' '}'
1825 {
1826 $$ = $2;
1827 }
1828 ;
1829
1830 initializer_list:
1831 initializer
1832 {
1833 void *ctx = state;
1834 $$ = new(ctx) ast_aggregate_initializer();
1835 $$->set_location(yylloc);
1836 $$->expressions.push_tail(& $1->link);
1837 }
1838 | initializer_list ',' initializer
1839 {
1840 $1->expressions.push_tail(& $3->link);
1841 }
1842 ;
1843
1844 declaration_statement:
1845 declaration
1846 ;
1847
1848 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1849 // supported.
1850 statement:
1851 compound_statement { $$ = (ast_node *) $1; }
1852 | simple_statement
1853 ;
1854
1855 simple_statement:
1856 declaration_statement
1857 | expression_statement
1858 | selection_statement
1859 | switch_statement
1860 | iteration_statement
1861 | jump_statement
1862 ;
1863
1864 compound_statement:
1865 '{' '}'
1866 {
1867 void *ctx = state;
1868 $$ = new(ctx) ast_compound_statement(true, NULL);
1869 $$->set_location(yylloc);
1870 }
1871 | '{'
1872 {
1873 state->symbols->push_scope();
1874 }
1875 statement_list '}'
1876 {
1877 void *ctx = state;
1878 $$ = new(ctx) ast_compound_statement(true, $3);
1879 $$->set_location(yylloc);
1880 state->symbols->pop_scope();
1881 }
1882 ;
1883
1884 statement_no_new_scope:
1885 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1886 | simple_statement
1887 ;
1888
1889 compound_statement_no_new_scope:
1890 '{' '}'
1891 {
1892 void *ctx = state;
1893 $$ = new(ctx) ast_compound_statement(false, NULL);
1894 $$->set_location(yylloc);
1895 }
1896 | '{' statement_list '}'
1897 {
1898 void *ctx = state;
1899 $$ = new(ctx) ast_compound_statement(false, $2);
1900 $$->set_location(yylloc);
1901 }
1902 ;
1903
1904 statement_list:
1905 statement
1906 {
1907 if ($1 == NULL) {
1908 _mesa_glsl_error(& @1, state, "<nil> statement");
1909 assert($1 != NULL);
1910 }
1911
1912 $$ = $1;
1913 $$->link.self_link();
1914 }
1915 | statement_list statement
1916 {
1917 if ($2 == NULL) {
1918 _mesa_glsl_error(& @2, state, "<nil> statement");
1919 assert($2 != NULL);
1920 }
1921 $$ = $1;
1922 $$->link.insert_before(& $2->link);
1923 }
1924 ;
1925
1926 expression_statement:
1927 ';'
1928 {
1929 void *ctx = state;
1930 $$ = new(ctx) ast_expression_statement(NULL);
1931 $$->set_location(yylloc);
1932 }
1933 | expression ';'
1934 {
1935 void *ctx = state;
1936 $$ = new(ctx) ast_expression_statement($1);
1937 $$->set_location(yylloc);
1938 }
1939 ;
1940
1941 selection_statement:
1942 IF '(' expression ')' selection_rest_statement
1943 {
1944 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1945 $5.else_statement);
1946 $$->set_location(yylloc);
1947 }
1948 ;
1949
1950 selection_rest_statement:
1951 statement ELSE statement
1952 {
1953 $$.then_statement = $1;
1954 $$.else_statement = $3;
1955 }
1956 | statement %prec THEN
1957 {
1958 $$.then_statement = $1;
1959 $$.else_statement = NULL;
1960 }
1961 ;
1962
1963 condition:
1964 expression
1965 {
1966 $$ = (ast_node *) $1;
1967 }
1968 | fully_specified_type any_identifier '=' initializer
1969 {
1970 void *ctx = state;
1971 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1972 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1973 decl->set_location(yylloc);
1974 declarator->set_location(yylloc);
1975
1976 declarator->declarations.push_tail(&decl->link);
1977 $$ = declarator;
1978 }
1979 ;
1980
1981 /*
1982 * siwtch_statement grammar is based on the syntax described in the body
1983 * of the GLSL spec, not in it's appendix!!!
1984 */
1985 switch_statement:
1986 SWITCH '(' expression ')' switch_body
1987 {
1988 $$ = new(state) ast_switch_statement($3, $5);
1989 $$->set_location(yylloc);
1990 }
1991 ;
1992
1993 switch_body:
1994 '{' '}'
1995 {
1996 $$ = new(state) ast_switch_body(NULL);
1997 $$->set_location(yylloc);
1998 }
1999 | '{' case_statement_list '}'
2000 {
2001 $$ = new(state) ast_switch_body($2);
2002 $$->set_location(yylloc);
2003 }
2004 ;
2005
2006 case_label:
2007 CASE expression ':'
2008 {
2009 $$ = new(state) ast_case_label($2);
2010 $$->set_location(yylloc);
2011 }
2012 | DEFAULT ':'
2013 {
2014 $$ = new(state) ast_case_label(NULL);
2015 $$->set_location(yylloc);
2016 }
2017 ;
2018
2019 case_label_list:
2020 case_label
2021 {
2022 ast_case_label_list *labels = new(state) ast_case_label_list();
2023
2024 labels->labels.push_tail(& $1->link);
2025 $$ = labels;
2026 $$->set_location(yylloc);
2027 }
2028 | case_label_list case_label
2029 {
2030 $$ = $1;
2031 $$->labels.push_tail(& $2->link);
2032 }
2033 ;
2034
2035 case_statement:
2036 case_label_list statement
2037 {
2038 ast_case_statement *stmts = new(state) ast_case_statement($1);
2039 stmts->set_location(yylloc);
2040
2041 stmts->stmts.push_tail(& $2->link);
2042 $$ = stmts;
2043 }
2044 | case_statement statement
2045 {
2046 $$ = $1;
2047 $$->stmts.push_tail(& $2->link);
2048 }
2049 ;
2050
2051 case_statement_list:
2052 case_statement
2053 {
2054 ast_case_statement_list *cases= new(state) ast_case_statement_list();
2055 cases->set_location(yylloc);
2056
2057 cases->cases.push_tail(& $1->link);
2058 $$ = cases;
2059 }
2060 | case_statement_list case_statement
2061 {
2062 $$ = $1;
2063 $$->cases.push_tail(& $2->link);
2064 }
2065 ;
2066
2067 iteration_statement:
2068 WHILE '(' condition ')' statement_no_new_scope
2069 {
2070 void *ctx = state;
2071 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2072 NULL, $3, NULL, $5);
2073 $$->set_location(yylloc);
2074 }
2075 | DO statement WHILE '(' expression ')' ';'
2076 {
2077 void *ctx = state;
2078 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2079 NULL, $5, NULL, $2);
2080 $$->set_location(yylloc);
2081 }
2082 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2083 {
2084 void *ctx = state;
2085 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2086 $3, $4.cond, $4.rest, $6);
2087 $$->set_location(yylloc);
2088 }
2089 ;
2090
2091 for_init_statement:
2092 expression_statement
2093 | declaration_statement
2094 ;
2095
2096 conditionopt:
2097 condition
2098 | /* empty */
2099 {
2100 $$ = NULL;
2101 }
2102 ;
2103
2104 for_rest_statement:
2105 conditionopt ';'
2106 {
2107 $$.cond = $1;
2108 $$.rest = NULL;
2109 }
2110 | conditionopt ';' expression
2111 {
2112 $$.cond = $1;
2113 $$.rest = $3;
2114 }
2115 ;
2116
2117 // Grammar Note: No 'goto'. Gotos are not supported.
2118 jump_statement:
2119 CONTINUE ';'
2120 {
2121 void *ctx = state;
2122 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2123 $$->set_location(yylloc);
2124 }
2125 | BREAK ';'
2126 {
2127 void *ctx = state;
2128 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2129 $$->set_location(yylloc);
2130 }
2131 | RETURN ';'
2132 {
2133 void *ctx = state;
2134 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2135 $$->set_location(yylloc);
2136 }
2137 | RETURN expression ';'
2138 {
2139 void *ctx = state;
2140 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2141 $$->set_location(yylloc);
2142 }
2143 | DISCARD ';' // Fragment shader only.
2144 {
2145 void *ctx = state;
2146 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2147 $$->set_location(yylloc);
2148 }
2149 ;
2150
2151 external_declaration:
2152 function_definition { $$ = $1; }
2153 | declaration { $$ = $1; }
2154 | pragma_statement { $$ = NULL; }
2155 | layout_defaults { $$ = $1; }
2156 ;
2157
2158 function_definition:
2159 function_prototype compound_statement_no_new_scope
2160 {
2161 void *ctx = state;
2162 $$ = new(ctx) ast_function_definition();
2163 $$->set_location(yylloc);
2164 $$->prototype = $1;
2165 $$->body = $2;
2166
2167 state->symbols->pop_scope();
2168 }
2169 ;
2170
2171 /* layout_qualifieropt is packed into this rule */
2172 interface_block:
2173 basic_interface_block
2174 {
2175 $$ = $1;
2176 }
2177 | layout_qualifier basic_interface_block
2178 {
2179 ast_interface_block *block = $2;
2180 if (!block->layout.merge_qualifier(& @1, state, $1)) {
2181 YYERROR;
2182 }
2183 $$ = block;
2184 }
2185 ;
2186
2187 basic_interface_block:
2188 interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2189 {
2190 ast_interface_block *const block = $6;
2191
2192 block->block_name = $2;
2193 block->declarations.push_degenerate_list_at_head(& $4->link);
2194
2195 if ($1.flags.q.uniform) {
2196 if (!state->has_uniform_buffer_objects()) {
2197 _mesa_glsl_error(& @1, state,
2198 "#version 140 / GL_ARB_uniform_buffer_object "
2199 "required for defining uniform blocks");
2200 } else if (state->ARB_uniform_buffer_object_warn) {
2201 _mesa_glsl_warning(& @1, state,
2202 "#version 140 / GL_ARB_uniform_buffer_object "
2203 "required for defining uniform blocks");
2204 }
2205 } else {
2206 if (state->es_shader || state->language_version < 150) {
2207 _mesa_glsl_error(& @1, state,
2208 "#version 150 required for using "
2209 "interface blocks");
2210 }
2211 }
2212
2213 /* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):
2214 * "It is illegal to have an input block in a vertex shader
2215 * or an output block in a fragment shader"
2216 */
2217 if ((state->target == vertex_shader) && $1.flags.q.in) {
2218 _mesa_glsl_error(& @1, state,
2219 "`in' interface block is not allowed for "
2220 "a vertex shader");
2221 } else if ((state->target == fragment_shader) && $1.flags.q.out) {
2222 _mesa_glsl_error(& @1, state,
2223 "`out' interface block is not allowed for "
2224 "a fragment shader");
2225 }
2226
2227 /* Since block arrays require names, and both features are added in
2228 * the same language versions, we don't have to explicitly
2229 * version-check both things.
2230 */
2231 if (block->instance_name != NULL) {
2232 state->check_version(150, 300, & @1, "interface blocks with "
2233 "an instance name are not allowed");
2234 }
2235
2236 unsigned interface_type_mask;
2237 struct ast_type_qualifier temp_type_qualifier;
2238
2239 /* Get a bitmask containing only the in/out/uniform flags, allowing us
2240 * to ignore other irrelevant flags like interpolation qualifiers.
2241 */
2242 temp_type_qualifier.flags.i = 0;
2243 temp_type_qualifier.flags.q.uniform = true;
2244 temp_type_qualifier.flags.q.in = true;
2245 temp_type_qualifier.flags.q.out = true;
2246 interface_type_mask = temp_type_qualifier.flags.i;
2247
2248 /* Get the block's interface qualifier. The interface_qualifier
2249 * production rule guarantees that only one bit will be set (and
2250 * it will be in/out/uniform).
2251 */
2252 unsigned block_interface_qualifier = $1.flags.i;
2253
2254 block->layout.flags.i |= block_interface_qualifier;
2255
2256 foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
2257 ast_type_qualifier& qualifier = member->type->qualifier;
2258 if ((qualifier.flags.i & interface_type_mask) == 0) {
2259 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2260 * "If no optional qualifier is used in a member declaration, the
2261 * qualifier of the variable is just in, out, or uniform as declared
2262 * by interface-qualifier."
2263 */
2264 qualifier.flags.i |= block_interface_qualifier;
2265 } else if ((qualifier.flags.i & interface_type_mask) !=
2266 block_interface_qualifier) {
2267 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2268 * "If optional qualifiers are used, they can include interpolation
2269 * and storage qualifiers and they must declare an input, output,
2270 * or uniform variable consistent with the interface qualifier of
2271 * the block."
2272 */
2273 _mesa_glsl_error(& @1, state,
2274 "uniform/in/out qualifier on "
2275 "interface block member does not match "
2276 "the interface block");
2277 }
2278 }
2279
2280 $$ = block;
2281 }
2282 ;
2283
2284 interface_qualifier:
2285 IN_TOK
2286 {
2287 memset(& $$, 0, sizeof($$));
2288 $$.flags.q.in = 1;
2289 }
2290 | OUT_TOK
2291 {
2292 memset(& $$, 0, sizeof($$));
2293 $$.flags.q.out = 1;
2294 }
2295 | UNIFORM
2296 {
2297 memset(& $$, 0, sizeof($$));
2298 $$.flags.q.uniform = 1;
2299 }
2300 ;
2301
2302 instance_name_opt:
2303 /* empty */
2304 {
2305 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2306 NULL, false, NULL);
2307 }
2308 | NEW_IDENTIFIER
2309 {
2310 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2311 $1, false, NULL);
2312 }
2313 | NEW_IDENTIFIER '[' constant_expression ']'
2314 {
2315 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2316 $1, true, $3);
2317 }
2318 | NEW_IDENTIFIER '[' ']'
2319 {
2320 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2321 $1, true, NULL);
2322 }
2323 ;
2324
2325 member_list:
2326 member_declaration
2327 {
2328 $$ = $1;
2329 $1->link.self_link();
2330 }
2331 | member_declaration member_list
2332 {
2333 $$ = $1;
2334 $2->link.insert_before(& $$->link);
2335 }
2336 ;
2337
2338 member_declaration:
2339 fully_specified_type struct_declarator_list ';'
2340 {
2341 void *ctx = state;
2342 ast_fully_specified_type *type = $1;
2343 type->set_location(yylloc);
2344
2345 if (type->qualifier.flags.q.attribute) {
2346 _mesa_glsl_error(& @1, state,
2347 "keyword 'attribute' cannot be used with "
2348 "interface block member");
2349 } else if (type->qualifier.flags.q.varying) {
2350 _mesa_glsl_error(& @1, state,
2351 "keyword 'varying' cannot be used with "
2352 "interface block member");
2353 }
2354
2355 $$ = new(ctx) ast_declarator_list(type);
2356 $$->set_location(yylloc);
2357
2358 $$->declarations.push_degenerate_list_at_head(& $2->link);
2359 }
2360 ;
2361
2362 layout_defaults:
2363 layout_qualifier UNIFORM ';'
2364 {
2365 if (!state->default_uniform_qualifier->merge_qualifier(& @1, state, $1)) {
2366 YYERROR;
2367 }
2368 $$ = NULL;
2369 }
2370
2371 | layout_qualifier IN_TOK ';'
2372 {
2373 void *ctx = state;
2374 $$ = NULL;
2375 if (state->target != geometry_shader) {
2376 _mesa_glsl_error(& @1, state,
2377 "input layout qualifiers only valid in "
2378 "geometry shaders");
2379 } else if (!$1.flags.q.prim_type) {
2380 _mesa_glsl_error(& @1, state,
2381 "input layout qualifiers must specify a primitive"
2382 " type");
2383 } else {
2384 /* Make sure this is a valid input primitive type. */
2385 switch ($1.prim_type) {
2386 case GL_POINTS:
2387 case GL_LINES:
2388 case GL_LINES_ADJACENCY:
2389 case GL_TRIANGLES:
2390 case GL_TRIANGLES_ADJACENCY:
2391 $$ = new(ctx) ast_gs_input_layout(@1, $1.prim_type);
2392 break;
2393 default:
2394 _mesa_glsl_error(&@1, state,
2395 "invalid geometry shader input primitive type");
2396 break;
2397 }
2398 }
2399 }
2400
2401 | layout_qualifier OUT_TOK ';'
2402 {
2403 if (state->target != geometry_shader) {
2404 _mesa_glsl_error(& @1, state,
2405 "out layout qualifiers only valid in "
2406 "geometry shaders");
2407 } else {
2408 if ($1.flags.q.prim_type) {
2409 /* Make sure this is a valid output primitive type. */
2410 switch ($1.prim_type) {
2411 case GL_POINTS:
2412 case GL_LINE_STRIP:
2413 case GL_TRIANGLE_STRIP:
2414 break;
2415 default:
2416 _mesa_glsl_error(&@1, state, "invalid geometry shader output "
2417 "primitive type");
2418 break;
2419 }
2420 }
2421 if (!state->out_qualifier->merge_qualifier(& @1, state, $1))
2422 YYERROR;
2423 }
2424 $$ = NULL;
2425 }