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