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