faaf4388c5491848f736378a079dafb512302253
[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(@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 // Vertex only.
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 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 ($3 >= 0) {
1323 $$.location = $3;
1324 } else {
1325 _mesa_glsl_error(& @3, state, "invalid location %d specified", $3);
1326 YYERROR;
1327 }
1328 }
1329
1330 if (match_layout_qualifier("index", $1, state) == 0) {
1331 $$.flags.q.explicit_index = 1;
1332
1333 if ($3 >= 0) {
1334 $$.index = $3;
1335 } else {
1336 _mesa_glsl_error(& @3, state, "invalid index %d specified", $3);
1337 YYERROR;
1338 }
1339 }
1340
1341 if ((state->ARB_shading_language_420pack_enable ||
1342 state->ARB_shader_atomic_counters_enable) &&
1343 match_layout_qualifier("binding", $1, state) == 0) {
1344 $$.flags.q.explicit_binding = 1;
1345 $$.binding = $3;
1346 }
1347
1348 if (state->ARB_shader_atomic_counters_enable &&
1349 match_layout_qualifier("offset", $1, state) == 0) {
1350 $$.flags.q.explicit_offset = 1;
1351 $$.offset = $3;
1352 }
1353
1354 if (match_layout_qualifier("max_vertices", $1, state) == 0) {
1355 $$.flags.q.max_vertices = 1;
1356
1357 if ($3 < 0) {
1358 _mesa_glsl_error(& @3, state,
1359 "invalid max_vertices %d specified", $3);
1360 YYERROR;
1361 } else {
1362 $$.max_vertices = $3;
1363 if (!state->is_version(150, 0)) {
1364 _mesa_glsl_error(& @3, state,
1365 "#version 150 max_vertices qualifier "
1366 "specified", $3);
1367 }
1368 }
1369 }
1370
1371 static const char *local_size_qualifiers[3] = {
1372 "local_size_x",
1373 "local_size_y",
1374 "local_size_z",
1375 };
1376 for (int i = 0; i < 3; i++) {
1377 if (match_layout_qualifier(local_size_qualifiers[i], $1,
1378 state) == 0) {
1379 if ($3 <= 0) {
1380 _mesa_glsl_error(& @3, state,
1381 "invalid %s of %d specified",
1382 local_size_qualifiers[i], $3);
1383 YYERROR;
1384 } else if (!state->is_version(430, 0) &&
1385 !state->ARB_compute_shader_enable) {
1386 _mesa_glsl_error(& @3, state,
1387 "%s qualifier requires GLSL 4.30 or "
1388 "ARB_compute_shader",
1389 local_size_qualifiers[i]);
1390 YYERROR;
1391 } else {
1392 $$.flags.q.local_size |= (1 << i);
1393 $$.local_size[i] = $3;
1394 }
1395 break;
1396 }
1397 }
1398
1399 if (match_layout_qualifier("invocations", $1, state) == 0) {
1400 $$.flags.q.invocations = 1;
1401
1402 if ($3 <= 0) {
1403 _mesa_glsl_error(& @3, state,
1404 "invalid invocations %d specified", $3);
1405 YYERROR;
1406 } else if ($3 > MAX_GEOMETRY_SHADER_INVOCATIONS) {
1407 _mesa_glsl_error(& @3, state,
1408 "invocations (%d) exceeds "
1409 "GL_MAX_GEOMETRY_SHADER_INVOCATIONS", $3);
1410 YYERROR;
1411 } else {
1412 $$.invocations = $3;
1413 if (!state->is_version(400, 0) &&
1414 !state->ARB_gpu_shader5_enable) {
1415 _mesa_glsl_error(& @3, state,
1416 "GL_ARB_gpu_shader5 invocations "
1417 "qualifier specified", $3);
1418 }
1419 }
1420 }
1421
1422 /* If the identifier didn't match any known layout identifiers,
1423 * emit an error.
1424 */
1425 if (!$$.flags.i) {
1426 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1427 "`%s'", $1);
1428 YYERROR;
1429 } else if (state->ARB_explicit_attrib_location_warn) {
1430 _mesa_glsl_warning(& @1, state,
1431 "GL_ARB_explicit_attrib_location layout "
1432 "identifier `%s' used", $1);
1433 }
1434 }
1435 | interface_block_layout_qualifier
1436 {
1437 $$ = $1;
1438 /* Layout qualifiers for ARB_uniform_buffer_object. */
1439 if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
1440 _mesa_glsl_error(& @1, state,
1441 "#version 140 / GL_ARB_uniform_buffer_object "
1442 "layout qualifier `%s' is used", $1);
1443 } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1444 _mesa_glsl_warning(& @1, state,
1445 "#version 140 / GL_ARB_uniform_buffer_object "
1446 "layout qualifier `%s' is used", $1);
1447 }
1448 }
1449 ;
1450
1451 /* This is a separate language rule because we parse these as tokens
1452 * (due to them being reserved keywords) instead of identifiers like
1453 * most qualifiers. See the any_identifier path of
1454 * layout_qualifier_id for the others.
1455 *
1456 * Note that since layout qualifiers are case-insensitive in desktop
1457 * GLSL, all of these qualifiers need to be handled as identifiers as
1458 * well (by the any_identifier path of layout_qualifier_id).
1459 */
1460 interface_block_layout_qualifier:
1461 ROW_MAJOR
1462 {
1463 memset(& $$, 0, sizeof($$));
1464 $$.flags.q.row_major = 1;
1465 }
1466 | PACKED_TOK
1467 {
1468 memset(& $$, 0, sizeof($$));
1469 $$.flags.q.packed = 1;
1470 }
1471 ;
1472
1473 interpolation_qualifier:
1474 SMOOTH
1475 {
1476 memset(& $$, 0, sizeof($$));
1477 $$.flags.q.smooth = 1;
1478 }
1479 | FLAT
1480 {
1481 memset(& $$, 0, sizeof($$));
1482 $$.flags.q.flat = 1;
1483 }
1484 | NOPERSPECTIVE
1485 {
1486 memset(& $$, 0, sizeof($$));
1487 $$.flags.q.noperspective = 1;
1488 }
1489 ;
1490
1491 type_qualifier:
1492 /* Single qualifiers */
1493 INVARIANT
1494 {
1495 memset(& $$, 0, sizeof($$));
1496 $$.flags.q.invariant = 1;
1497 }
1498 | auxiliary_storage_qualifier
1499 | storage_qualifier
1500 | interpolation_qualifier
1501 | layout_qualifier
1502 | precision_qualifier
1503 {
1504 memset(&$$, 0, sizeof($$));
1505 $$.precision = $1;
1506 }
1507
1508 /* Multiple qualifiers:
1509 * In GLSL 4.20, these can be specified in any order. In earlier versions,
1510 * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1511 *
1512 * invariant interpolation auxiliary storage precision ...or...
1513 * layout storage precision
1514 *
1515 * Each qualifier's rule ensures that the accumulated qualifiers on the right
1516 * side don't contain any that must appear on the left hand side.
1517 * For example, when processing a storage qualifier, we check that there are
1518 * no auxiliary, interpolation, layout, or invariant qualifiers to the right.
1519 */
1520 | INVARIANT type_qualifier
1521 {
1522 if ($2.flags.q.invariant)
1523 _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1524
1525 if ($2.has_layout()) {
1526 _mesa_glsl_error(&@1, state,
1527 "\"invariant\" cannot be used with layout(...)");
1528 }
1529
1530 $$ = $2;
1531 $$.flags.q.invariant = 1;
1532 }
1533 | interpolation_qualifier type_qualifier
1534 {
1535 /* Section 4.3 of the GLSL 1.40 specification states:
1536 * "...qualified with one of these interpolation qualifiers"
1537 *
1538 * GLSL 1.30 claims to allow "one or more", but insists that:
1539 * "These interpolation qualifiers may only precede the qualifiers in,
1540 * centroid in, out, or centroid out in a declaration."
1541 *
1542 * ...which means that e.g. smooth can't precede smooth, so there can be
1543 * only one after all, and the 1.40 text is a clarification, not a change.
1544 */
1545 if ($2.has_interpolation())
1546 _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1547
1548 if ($2.has_layout()) {
1549 _mesa_glsl_error(&@1, state, "interpolation qualifiers cannot be used "
1550 "with layout(...)");
1551 }
1552
1553 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.invariant) {
1554 _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1555 "after \"invariant\"");
1556 }
1557
1558 $$ = $1;
1559 $$.merge_qualifier(&@1, state, $2);
1560 }
1561 | layout_qualifier type_qualifier
1562 {
1563 /* The GLSL 1.50 grammar indicates that a layout(...) declaration can be
1564 * used standalone or immediately before a storage qualifier. It cannot
1565 * be used with interpolation qualifiers or invariant. There does not
1566 * appear to be any text indicating that it must come before the storage
1567 * qualifier, but always seems to in examples.
1568 */
1569 if (!state->ARB_shading_language_420pack_enable && $2.has_layout())
1570 _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
1571
1572 if ($2.flags.q.invariant)
1573 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1574 "the \"invariant\" qualifier");
1575
1576 if ($2.has_interpolation()) {
1577 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1578 "interpolation qualifiers");
1579 }
1580
1581 $$ = $1;
1582 $$.merge_qualifier(&@1, state, $2);
1583 }
1584 | auxiliary_storage_qualifier type_qualifier
1585 {
1586 if ($2.has_auxiliary_storage()) {
1587 _mesa_glsl_error(&@1, state,
1588 "duplicate auxiliary storage qualifier (centroid or sample)");
1589 }
1590
1591 if (!state->ARB_shading_language_420pack_enable &&
1592 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout())) {
1593 _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
1594 "just before storage qualifiers");
1595 }
1596 $$ = $1;
1597 $$.merge_qualifier(&@1, state, $2);
1598 }
1599 | storage_qualifier type_qualifier
1600 {
1601 /* Section 4.3 of the GLSL 1.20 specification states:
1602 * "Variable declarations may have a storage qualifier specified..."
1603 * 1.30 clarifies this to "may have one storage qualifier".
1604 */
1605 if ($2.has_storage())
1606 _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
1607
1608 if (!state->ARB_shading_language_420pack_enable &&
1609 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout() ||
1610 $2.has_auxiliary_storage())) {
1611 _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
1612 "invariant, interpolation, layout and auxiliary "
1613 "storage qualifiers");
1614 }
1615
1616 $$ = $1;
1617 $$.merge_qualifier(&@1, state, $2);
1618 }
1619 | precision_qualifier type_qualifier
1620 {
1621 if ($2.precision != ast_precision_none)
1622 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
1623
1624 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
1625 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
1626
1627 $$ = $2;
1628 $$.precision = $1;
1629 }
1630 ;
1631
1632 auxiliary_storage_qualifier:
1633 CENTROID
1634 {
1635 memset(& $$, 0, sizeof($$));
1636 $$.flags.q.centroid = 1;
1637 }
1638 | SAMPLE
1639 {
1640 memset(& $$, 0, sizeof($$));
1641 $$.flags.q.sample = 1;
1642 }
1643 /* TODO: "patch" also goes here someday. */
1644
1645 storage_qualifier:
1646 CONST_TOK
1647 {
1648 memset(& $$, 0, sizeof($$));
1649 $$.flags.q.constant = 1;
1650 }
1651 | ATTRIBUTE
1652 {
1653 memset(& $$, 0, sizeof($$));
1654 $$.flags.q.attribute = 1;
1655 }
1656 | VARYING
1657 {
1658 memset(& $$, 0, sizeof($$));
1659 $$.flags.q.varying = 1;
1660 }
1661 | IN_TOK
1662 {
1663 memset(& $$, 0, sizeof($$));
1664 $$.flags.q.in = 1;
1665 }
1666 | OUT_TOK
1667 {
1668 memset(& $$, 0, sizeof($$));
1669 $$.flags.q.out = 1;
1670 }
1671 | UNIFORM
1672 {
1673 memset(& $$, 0, sizeof($$));
1674 $$.flags.q.uniform = 1;
1675 }
1676 | COHERENT
1677 {
1678 memset(& $$, 0, sizeof($$));
1679 $$.flags.q.coherent = 1;
1680 }
1681 | VOLATILE
1682 {
1683 memset(& $$, 0, sizeof($$));
1684 $$.flags.q._volatile = 1;
1685 }
1686 | RESTRICT
1687 {
1688 STATIC_ASSERT(sizeof($$.flags.q) <= sizeof($$.flags.i));
1689 memset(& $$, 0, sizeof($$));
1690 $$.flags.q.restrict_flag = 1;
1691 }
1692 | READONLY
1693 {
1694 memset(& $$, 0, sizeof($$));
1695 $$.flags.q.read_only = 1;
1696 }
1697 | WRITEONLY
1698 {
1699 memset(& $$, 0, sizeof($$));
1700 $$.flags.q.write_only = 1;
1701 }
1702 ;
1703
1704 array_specifier:
1705 '[' ']'
1706 {
1707 void *ctx = state;
1708 $$ = new(ctx) ast_array_specifier(@1);
1709 $$->set_location_range(@1, @2);
1710 }
1711 | '[' constant_expression ']'
1712 {
1713 void *ctx = state;
1714 $$ = new(ctx) ast_array_specifier(@1, $2);
1715 $$->set_location_range(@1, @3);
1716 }
1717 | array_specifier '[' ']'
1718 {
1719 $$ = $1;
1720
1721 if (!state->ARB_arrays_of_arrays_enable) {
1722 _mesa_glsl_error(& @1, state,
1723 "GL_ARB_arrays_of_arrays "
1724 "required for defining arrays of arrays");
1725 } else {
1726 _mesa_glsl_error(& @1, state,
1727 "only the outermost array dimension can "
1728 "be unsized");
1729 }
1730 }
1731 | array_specifier '[' constant_expression ']'
1732 {
1733 $$ = $1;
1734
1735 if (!state->ARB_arrays_of_arrays_enable) {
1736 _mesa_glsl_error(& @1, state,
1737 "GL_ARB_arrays_of_arrays "
1738 "required for defining arrays of arrays");
1739 }
1740
1741 $$->add_dimension($3);
1742 }
1743 ;
1744
1745 type_specifier:
1746 type_specifier_nonarray
1747 | type_specifier_nonarray array_specifier
1748 {
1749 $$ = $1;
1750 $$->array_specifier = $2;
1751 }
1752 ;
1753
1754 type_specifier_nonarray:
1755 basic_type_specifier_nonarray
1756 {
1757 void *ctx = state;
1758 $$ = new(ctx) ast_type_specifier($1);
1759 $$->set_location(@1);
1760 }
1761 | struct_specifier
1762 {
1763 void *ctx = state;
1764 $$ = new(ctx) ast_type_specifier($1);
1765 $$->set_location(@1);
1766 }
1767 | TYPE_IDENTIFIER
1768 {
1769 void *ctx = state;
1770 $$ = new(ctx) ast_type_specifier($1);
1771 $$->set_location(@1);
1772 }
1773 ;
1774
1775 basic_type_specifier_nonarray:
1776 VOID_TOK { $$ = "void"; }
1777 | FLOAT_TOK { $$ = "float"; }
1778 | INT_TOK { $$ = "int"; }
1779 | UINT_TOK { $$ = "uint"; }
1780 | BOOL_TOK { $$ = "bool"; }
1781 | VEC2 { $$ = "vec2"; }
1782 | VEC3 { $$ = "vec3"; }
1783 | VEC4 { $$ = "vec4"; }
1784 | BVEC2 { $$ = "bvec2"; }
1785 | BVEC3 { $$ = "bvec3"; }
1786 | BVEC4 { $$ = "bvec4"; }
1787 | IVEC2 { $$ = "ivec2"; }
1788 | IVEC3 { $$ = "ivec3"; }
1789 | IVEC4 { $$ = "ivec4"; }
1790 | UVEC2 { $$ = "uvec2"; }
1791 | UVEC3 { $$ = "uvec3"; }
1792 | UVEC4 { $$ = "uvec4"; }
1793 | MAT2X2 { $$ = "mat2"; }
1794 | MAT2X3 { $$ = "mat2x3"; }
1795 | MAT2X4 { $$ = "mat2x4"; }
1796 | MAT3X2 { $$ = "mat3x2"; }
1797 | MAT3X3 { $$ = "mat3"; }
1798 | MAT3X4 { $$ = "mat3x4"; }
1799 | MAT4X2 { $$ = "mat4x2"; }
1800 | MAT4X3 { $$ = "mat4x3"; }
1801 | MAT4X4 { $$ = "mat4"; }
1802 | SAMPLER1D { $$ = "sampler1D"; }
1803 | SAMPLER2D { $$ = "sampler2D"; }
1804 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
1805 | SAMPLER3D { $$ = "sampler3D"; }
1806 | SAMPLERCUBE { $$ = "samplerCube"; }
1807 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
1808 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
1809 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
1810 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
1811 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
1812 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
1813 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
1814 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
1815 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
1816 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
1817 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
1818 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
1819 | ISAMPLER1D { $$ = "isampler1D"; }
1820 | ISAMPLER2D { $$ = "isampler2D"; }
1821 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
1822 | ISAMPLER3D { $$ = "isampler3D"; }
1823 | ISAMPLERCUBE { $$ = "isamplerCube"; }
1824 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
1825 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
1826 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
1827 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
1828 | USAMPLER1D { $$ = "usampler1D"; }
1829 | USAMPLER2D { $$ = "usampler2D"; }
1830 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
1831 | USAMPLER3D { $$ = "usampler3D"; }
1832 | USAMPLERCUBE { $$ = "usamplerCube"; }
1833 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
1834 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
1835 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
1836 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
1837 | SAMPLER2DMS { $$ = "sampler2DMS"; }
1838 | ISAMPLER2DMS { $$ = "isampler2DMS"; }
1839 | USAMPLER2DMS { $$ = "usampler2DMS"; }
1840 | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
1841 | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
1842 | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
1843 | IMAGE1D { $$ = "image1D"; }
1844 | IMAGE2D { $$ = "image2D"; }
1845 | IMAGE3D { $$ = "image3D"; }
1846 | IMAGE2DRECT { $$ = "image2DRect"; }
1847 | IMAGECUBE { $$ = "imageCube"; }
1848 | IMAGEBUFFER { $$ = "imageBuffer"; }
1849 | IMAGE1DARRAY { $$ = "image1DArray"; }
1850 | IMAGE2DARRAY { $$ = "image2DArray"; }
1851 | IMAGECUBEARRAY { $$ = "imageCubeArray"; }
1852 | IMAGE2DMS { $$ = "image2DMS"; }
1853 | IMAGE2DMSARRAY { $$ = "image2DMSArray"; }
1854 | IIMAGE1D { $$ = "iimage1D"; }
1855 | IIMAGE2D { $$ = "iimage2D"; }
1856 | IIMAGE3D { $$ = "iimage3D"; }
1857 | IIMAGE2DRECT { $$ = "iimage2DRect"; }
1858 | IIMAGECUBE { $$ = "iimageCube"; }
1859 | IIMAGEBUFFER { $$ = "iimageBuffer"; }
1860 | IIMAGE1DARRAY { $$ = "iimage1DArray"; }
1861 | IIMAGE2DARRAY { $$ = "iimage2DArray"; }
1862 | IIMAGECUBEARRAY { $$ = "iimageCubeArray"; }
1863 | IIMAGE2DMS { $$ = "iimage2DMS"; }
1864 | IIMAGE2DMSARRAY { $$ = "iimage2DMSArray"; }
1865 | UIMAGE1D { $$ = "uimage1D"; }
1866 | UIMAGE2D { $$ = "uimage2D"; }
1867 | UIMAGE3D { $$ = "uimage3D"; }
1868 | UIMAGE2DRECT { $$ = "uimage2DRect"; }
1869 | UIMAGECUBE { $$ = "uimageCube"; }
1870 | UIMAGEBUFFER { $$ = "uimageBuffer"; }
1871 | UIMAGE1DARRAY { $$ = "uimage1DArray"; }
1872 | UIMAGE2DARRAY { $$ = "uimage2DArray"; }
1873 | UIMAGECUBEARRAY { $$ = "uimageCubeArray"; }
1874 | UIMAGE2DMS { $$ = "uimage2DMS"; }
1875 | UIMAGE2DMSARRAY { $$ = "uimage2DMSArray"; }
1876 | ATOMIC_UINT { $$ = "atomic_uint"; }
1877 ;
1878
1879 precision_qualifier:
1880 HIGHP
1881 {
1882 state->check_precision_qualifiers_allowed(&@1);
1883 $$ = ast_precision_high;
1884 }
1885 | MEDIUMP
1886 {
1887 state->check_precision_qualifiers_allowed(&@1);
1888 $$ = ast_precision_medium;
1889 }
1890 | LOWP
1891 {
1892 state->check_precision_qualifiers_allowed(&@1);
1893 $$ = ast_precision_low;
1894 }
1895 ;
1896
1897 struct_specifier:
1898 STRUCT any_identifier '{' struct_declaration_list '}'
1899 {
1900 void *ctx = state;
1901 $$ = new(ctx) ast_struct_specifier($2, $4);
1902 $$->set_location_range(@2, @5);
1903 state->symbols->add_type($2, glsl_type::void_type);
1904 state->symbols->add_type_ast($2, new(ctx) ast_type_specifier($$));
1905 }
1906 | STRUCT '{' struct_declaration_list '}'
1907 {
1908 void *ctx = state;
1909 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1910 $$->set_location_range(@2, @4);
1911 }
1912 ;
1913
1914 struct_declaration_list:
1915 struct_declaration
1916 {
1917 $$ = $1;
1918 $1->link.self_link();
1919 }
1920 | struct_declaration_list struct_declaration
1921 {
1922 $$ = $1;
1923 $$->link.insert_before(& $2->link);
1924 }
1925 ;
1926
1927 struct_declaration:
1928 fully_specified_type struct_declarator_list ';'
1929 {
1930 void *ctx = state;
1931 ast_fully_specified_type *const type = $1;
1932 type->set_location(@1);
1933
1934 if (type->qualifier.flags.i != 0)
1935 _mesa_glsl_error(&@1, state,
1936 "only precision qualifiers may be applied to "
1937 "structure members");
1938
1939 $$ = new(ctx) ast_declarator_list(type);
1940 $$->set_location(@2);
1941
1942 $$->declarations.push_degenerate_list_at_head(& $2->link);
1943 }
1944 ;
1945
1946 struct_declarator_list:
1947 struct_declarator
1948 {
1949 $$ = $1;
1950 $1->link.self_link();
1951 }
1952 | struct_declarator_list ',' struct_declarator
1953 {
1954 $$ = $1;
1955 $$->link.insert_before(& $3->link);
1956 }
1957 ;
1958
1959 struct_declarator:
1960 any_identifier
1961 {
1962 void *ctx = state;
1963 $$ = new(ctx) ast_declaration($1, NULL, NULL);
1964 $$->set_location(@1);
1965 }
1966 | any_identifier array_specifier
1967 {
1968 void *ctx = state;
1969 $$ = new(ctx) ast_declaration($1, $2, NULL);
1970 $$->set_location_range(@1, @2);
1971 }
1972 ;
1973
1974 initializer:
1975 assignment_expression
1976 | '{' initializer_list '}'
1977 {
1978 $$ = $2;
1979 }
1980 | '{' initializer_list ',' '}'
1981 {
1982 $$ = $2;
1983 }
1984 ;
1985
1986 initializer_list:
1987 initializer
1988 {
1989 void *ctx = state;
1990 $$ = new(ctx) ast_aggregate_initializer();
1991 $$->set_location(@1);
1992 $$->expressions.push_tail(& $1->link);
1993 }
1994 | initializer_list ',' initializer
1995 {
1996 $1->expressions.push_tail(& $3->link);
1997 }
1998 ;
1999
2000 declaration_statement:
2001 declaration
2002 ;
2003
2004 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
2005 // supported.
2006 statement:
2007 compound_statement { $$ = (ast_node *) $1; }
2008 | simple_statement
2009 ;
2010
2011 simple_statement:
2012 declaration_statement
2013 | expression_statement
2014 | selection_statement
2015 | switch_statement
2016 | iteration_statement
2017 | jump_statement
2018 ;
2019
2020 compound_statement:
2021 '{' '}'
2022 {
2023 void *ctx = state;
2024 $$ = new(ctx) ast_compound_statement(true, NULL);
2025 $$->set_location_range(@1, @2);
2026 }
2027 | '{'
2028 {
2029 state->symbols->push_scope();
2030 }
2031 statement_list '}'
2032 {
2033 void *ctx = state;
2034 $$ = new(ctx) ast_compound_statement(true, $3);
2035 $$->set_location_range(@1, @4);
2036 state->symbols->pop_scope();
2037 }
2038 ;
2039
2040 statement_no_new_scope:
2041 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
2042 | simple_statement
2043 ;
2044
2045 compound_statement_no_new_scope:
2046 '{' '}'
2047 {
2048 void *ctx = state;
2049 $$ = new(ctx) ast_compound_statement(false, NULL);
2050 $$->set_location_range(@1, @2);
2051 }
2052 | '{' statement_list '}'
2053 {
2054 void *ctx = state;
2055 $$ = new(ctx) ast_compound_statement(false, $2);
2056 $$->set_location_range(@1, @3);
2057 }
2058 ;
2059
2060 statement_list:
2061 statement
2062 {
2063 if ($1 == NULL) {
2064 _mesa_glsl_error(& @1, state, "<nil> statement");
2065 assert($1 != NULL);
2066 }
2067
2068 $$ = $1;
2069 $$->link.self_link();
2070 }
2071 | statement_list statement
2072 {
2073 if ($2 == NULL) {
2074 _mesa_glsl_error(& @2, state, "<nil> statement");
2075 assert($2 != NULL);
2076 }
2077 $$ = $1;
2078 $$->link.insert_before(& $2->link);
2079 }
2080 ;
2081
2082 expression_statement:
2083 ';'
2084 {
2085 void *ctx = state;
2086 $$ = new(ctx) ast_expression_statement(NULL);
2087 $$->set_location(@1);
2088 }
2089 | expression ';'
2090 {
2091 void *ctx = state;
2092 $$ = new(ctx) ast_expression_statement($1);
2093 $$->set_location(@1);
2094 }
2095 ;
2096
2097 selection_statement:
2098 IF '(' expression ')' selection_rest_statement
2099 {
2100 $$ = new(state) ast_selection_statement($3, $5.then_statement,
2101 $5.else_statement);
2102 $$->set_location_range(@1, @5);
2103 }
2104 ;
2105
2106 selection_rest_statement:
2107 statement ELSE statement
2108 {
2109 $$.then_statement = $1;
2110 $$.else_statement = $3;
2111 }
2112 | statement %prec THEN
2113 {
2114 $$.then_statement = $1;
2115 $$.else_statement = NULL;
2116 }
2117 ;
2118
2119 condition:
2120 expression
2121 {
2122 $$ = (ast_node *) $1;
2123 }
2124 | fully_specified_type any_identifier '=' initializer
2125 {
2126 void *ctx = state;
2127 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
2128 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
2129 decl->set_location_range(@2, @4);
2130 declarator->set_location(@1);
2131
2132 declarator->declarations.push_tail(&decl->link);
2133 $$ = declarator;
2134 }
2135 ;
2136
2137 /*
2138 * siwtch_statement grammar is based on the syntax described in the body
2139 * of the GLSL spec, not in it's appendix!!!
2140 */
2141 switch_statement:
2142 SWITCH '(' expression ')' switch_body
2143 {
2144 $$ = new(state) ast_switch_statement($3, $5);
2145 $$->set_location_range(@1, @5);
2146 }
2147 ;
2148
2149 switch_body:
2150 '{' '}'
2151 {
2152 $$ = new(state) ast_switch_body(NULL);
2153 $$->set_location_range(@1, @2);
2154 }
2155 | '{' case_statement_list '}'
2156 {
2157 $$ = new(state) ast_switch_body($2);
2158 $$->set_location_range(@1, @3);
2159 }
2160 ;
2161
2162 case_label:
2163 CASE expression ':'
2164 {
2165 $$ = new(state) ast_case_label($2);
2166 $$->set_location(@2);
2167 }
2168 | DEFAULT ':'
2169 {
2170 $$ = new(state) ast_case_label(NULL);
2171 $$->set_location(@2);
2172 }
2173 ;
2174
2175 case_label_list:
2176 case_label
2177 {
2178 ast_case_label_list *labels = new(state) ast_case_label_list();
2179
2180 labels->labels.push_tail(& $1->link);
2181 $$ = labels;
2182 $$->set_location(@1);
2183 }
2184 | case_label_list case_label
2185 {
2186 $$ = $1;
2187 $$->labels.push_tail(& $2->link);
2188 }
2189 ;
2190
2191 case_statement:
2192 case_label_list statement
2193 {
2194 ast_case_statement *stmts = new(state) ast_case_statement($1);
2195 stmts->set_location(@2);
2196
2197 stmts->stmts.push_tail(& $2->link);
2198 $$ = stmts;
2199 }
2200 | case_statement statement
2201 {
2202 $$ = $1;
2203 $$->stmts.push_tail(& $2->link);
2204 }
2205 ;
2206
2207 case_statement_list:
2208 case_statement
2209 {
2210 ast_case_statement_list *cases= new(state) ast_case_statement_list();
2211 cases->set_location(@1);
2212
2213 cases->cases.push_tail(& $1->link);
2214 $$ = cases;
2215 }
2216 | case_statement_list case_statement
2217 {
2218 $$ = $1;
2219 $$->cases.push_tail(& $2->link);
2220 }
2221 ;
2222
2223 iteration_statement:
2224 WHILE '(' condition ')' statement_no_new_scope
2225 {
2226 void *ctx = state;
2227 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2228 NULL, $3, NULL, $5);
2229 $$->set_location_range(@1, @4);
2230 }
2231 | DO statement WHILE '(' expression ')' ';'
2232 {
2233 void *ctx = state;
2234 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2235 NULL, $5, NULL, $2);
2236 $$->set_location_range(@1, @6);
2237 }
2238 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2239 {
2240 void *ctx = state;
2241 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2242 $3, $4.cond, $4.rest, $6);
2243 $$->set_location_range(@1, @6);
2244 }
2245 ;
2246
2247 for_init_statement:
2248 expression_statement
2249 | declaration_statement
2250 ;
2251
2252 conditionopt:
2253 condition
2254 | /* empty */
2255 {
2256 $$ = NULL;
2257 }
2258 ;
2259
2260 for_rest_statement:
2261 conditionopt ';'
2262 {
2263 $$.cond = $1;
2264 $$.rest = NULL;
2265 }
2266 | conditionopt ';' expression
2267 {
2268 $$.cond = $1;
2269 $$.rest = $3;
2270 }
2271 ;
2272
2273 // Grammar Note: No 'goto'. Gotos are not supported.
2274 jump_statement:
2275 CONTINUE ';'
2276 {
2277 void *ctx = state;
2278 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2279 $$->set_location(@1);
2280 }
2281 | BREAK ';'
2282 {
2283 void *ctx = state;
2284 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2285 $$->set_location(@1);
2286 }
2287 | RETURN ';'
2288 {
2289 void *ctx = state;
2290 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2291 $$->set_location(@1);
2292 }
2293 | RETURN expression ';'
2294 {
2295 void *ctx = state;
2296 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2297 $$->set_location_range(@1, @2);
2298 }
2299 | DISCARD ';' // Fragment shader only.
2300 {
2301 void *ctx = state;
2302 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2303 $$->set_location(@1);
2304 }
2305 ;
2306
2307 external_declaration:
2308 function_definition { $$ = $1; }
2309 | declaration { $$ = $1; }
2310 | pragma_statement { $$ = NULL; }
2311 | layout_defaults { $$ = $1; }
2312 ;
2313
2314 function_definition:
2315 function_prototype compound_statement_no_new_scope
2316 {
2317 void *ctx = state;
2318 $$ = new(ctx) ast_function_definition();
2319 $$->set_location_range(@1, @2);
2320 $$->prototype = $1;
2321 $$->body = $2;
2322
2323 state->symbols->pop_scope();
2324 }
2325 ;
2326
2327 /* layout_qualifieropt is packed into this rule */
2328 interface_block:
2329 basic_interface_block
2330 {
2331 $$ = $1;
2332 }
2333 | layout_qualifier basic_interface_block
2334 {
2335 ast_interface_block *block = $2;
2336 if (!block->layout.merge_qualifier(& @1, state, $1)) {
2337 YYERROR;
2338 }
2339 $$ = block;
2340 }
2341 ;
2342
2343 basic_interface_block:
2344 interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2345 {
2346 ast_interface_block *const block = $6;
2347
2348 block->block_name = $2;
2349 block->declarations.push_degenerate_list_at_head(& $4->link);
2350
2351 if ($1.flags.q.uniform) {
2352 if (!state->has_uniform_buffer_objects()) {
2353 _mesa_glsl_error(& @1, state,
2354 "#version 140 / GL_ARB_uniform_buffer_object "
2355 "required for defining uniform blocks");
2356 } else if (state->ARB_uniform_buffer_object_warn) {
2357 _mesa_glsl_warning(& @1, state,
2358 "#version 140 / GL_ARB_uniform_buffer_object "
2359 "required for defining uniform blocks");
2360 }
2361 } else {
2362 if (state->es_shader || state->language_version < 150) {
2363 _mesa_glsl_error(& @1, state,
2364 "#version 150 required for using "
2365 "interface blocks");
2366 }
2367 }
2368
2369 /* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):
2370 * "It is illegal to have an input block in a vertex shader
2371 * or an output block in a fragment shader"
2372 */
2373 if ((state->stage == MESA_SHADER_VERTEX) && $1.flags.q.in) {
2374 _mesa_glsl_error(& @1, state,
2375 "`in' interface block is not allowed for "
2376 "a vertex shader");
2377 } else if ((state->stage == MESA_SHADER_FRAGMENT) && $1.flags.q.out) {
2378 _mesa_glsl_error(& @1, state,
2379 "`out' interface block is not allowed for "
2380 "a fragment shader");
2381 }
2382
2383 /* Since block arrays require names, and both features are added in
2384 * the same language versions, we don't have to explicitly
2385 * version-check both things.
2386 */
2387 if (block->instance_name != NULL) {
2388 state->check_version(150, 300, & @1, "interface blocks with "
2389 "an instance name are not allowed");
2390 }
2391
2392 uint64_t interface_type_mask;
2393 struct ast_type_qualifier temp_type_qualifier;
2394
2395 /* Get a bitmask containing only the in/out/uniform flags, allowing us
2396 * to ignore other irrelevant flags like interpolation qualifiers.
2397 */
2398 temp_type_qualifier.flags.i = 0;
2399 temp_type_qualifier.flags.q.uniform = true;
2400 temp_type_qualifier.flags.q.in = true;
2401 temp_type_qualifier.flags.q.out = true;
2402 interface_type_mask = temp_type_qualifier.flags.i;
2403
2404 /* Get the block's interface qualifier. The interface_qualifier
2405 * production rule guarantees that only one bit will be set (and
2406 * it will be in/out/uniform).
2407 */
2408 uint64_t block_interface_qualifier = $1.flags.i;
2409
2410 block->layout.flags.i |= block_interface_qualifier;
2411
2412 foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
2413 ast_type_qualifier& qualifier = member->type->qualifier;
2414 if ((qualifier.flags.i & interface_type_mask) == 0) {
2415 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2416 * "If no optional qualifier is used in a member declaration, the
2417 * qualifier of the variable is just in, out, or uniform as declared
2418 * by interface-qualifier."
2419 */
2420 qualifier.flags.i |= block_interface_qualifier;
2421 } else if ((qualifier.flags.i & interface_type_mask) !=
2422 block_interface_qualifier) {
2423 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2424 * "If optional qualifiers are used, they can include interpolation
2425 * and storage qualifiers and they must declare an input, output,
2426 * or uniform variable consistent with the interface qualifier of
2427 * the block."
2428 */
2429 _mesa_glsl_error(& @1, state,
2430 "uniform/in/out qualifier on "
2431 "interface block member does not match "
2432 "the interface block");
2433 }
2434 }
2435
2436 $$ = block;
2437 }
2438 ;
2439
2440 interface_qualifier:
2441 IN_TOK
2442 {
2443 memset(& $$, 0, sizeof($$));
2444 $$.flags.q.in = 1;
2445 }
2446 | OUT_TOK
2447 {
2448 memset(& $$, 0, sizeof($$));
2449 $$.flags.q.out = 1;
2450 }
2451 | UNIFORM
2452 {
2453 memset(& $$, 0, sizeof($$));
2454 $$.flags.q.uniform = 1;
2455 }
2456 ;
2457
2458 instance_name_opt:
2459 /* empty */
2460 {
2461 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2462 NULL, NULL);
2463 }
2464 | NEW_IDENTIFIER
2465 {
2466 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2467 $1, NULL);
2468 $$->set_location(@1);
2469 }
2470 | NEW_IDENTIFIER array_specifier
2471 {
2472 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2473 $1, $2);
2474 $$->set_location_range(@1, @2);
2475 }
2476 ;
2477
2478 member_list:
2479 member_declaration
2480 {
2481 $$ = $1;
2482 $1->link.self_link();
2483 }
2484 | member_declaration member_list
2485 {
2486 $$ = $1;
2487 $2->link.insert_before(& $$->link);
2488 }
2489 ;
2490
2491 member_declaration:
2492 fully_specified_type struct_declarator_list ';'
2493 {
2494 void *ctx = state;
2495 ast_fully_specified_type *type = $1;
2496 type->set_location(@1);
2497
2498 if (type->qualifier.flags.q.attribute) {
2499 _mesa_glsl_error(& @1, state,
2500 "keyword 'attribute' cannot be used with "
2501 "interface block member");
2502 } else if (type->qualifier.flags.q.varying) {
2503 _mesa_glsl_error(& @1, state,
2504 "keyword 'varying' cannot be used with "
2505 "interface block member");
2506 }
2507
2508 $$ = new(ctx) ast_declarator_list(type);
2509 $$->set_location(@2);
2510
2511 $$->declarations.push_degenerate_list_at_head(& $2->link);
2512 }
2513 ;
2514
2515 layout_defaults:
2516 layout_qualifier UNIFORM ';'
2517 {
2518 if (!state->default_uniform_qualifier->merge_qualifier(& @1, state, $1)) {
2519 YYERROR;
2520 }
2521 $$ = NULL;
2522 }
2523
2524 | layout_qualifier IN_TOK ';'
2525 {
2526 $$ = NULL;
2527 if (!state->in_qualifier->merge_in_qualifier(& @1, state, $1, $$)) {
2528 YYERROR;
2529 }
2530 }
2531
2532 | layout_qualifier OUT_TOK ';'
2533 {
2534 if (state->stage != MESA_SHADER_GEOMETRY) {
2535 _mesa_glsl_error(& @1, state,
2536 "out layout qualifiers only valid in "
2537 "geometry shaders");
2538 } else {
2539 if ($1.flags.q.prim_type) {
2540 /* Make sure this is a valid output primitive type. */
2541 switch ($1.prim_type) {
2542 case GL_POINTS:
2543 case GL_LINE_STRIP:
2544 case GL_TRIANGLE_STRIP:
2545 break;
2546 default:
2547 _mesa_glsl_error(&@1, state, "invalid geometry shader output "
2548 "primitive type");
2549 break;
2550 }
2551 }
2552 if (!state->out_qualifier->merge_qualifier(& @1, state, $1))
2553 YYERROR;
2554 }
2555 $$ = NULL;
2556 }