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