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