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