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