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