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