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