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