2c339add62af43e75252020e9f44a5721f49f15c
[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 if (strcmp($1, "origin_upper_left") == 0) {
1163 $$.flags.q.origin_upper_left = 1;
1164 } else if (strcmp($1, "pixel_center_integer") == 0) {
1165 $$.flags.q.pixel_center_integer = 1;
1166 }
1167
1168 if ($$.flags.i && state->ARB_fragment_coord_conventions_warn) {
1169 _mesa_glsl_warning(& @1, state,
1170 "GL_ARB_fragment_coord_conventions layout "
1171 "identifier `%s' used", $1);
1172 }
1173 }
1174
1175 /* Layout qualifiers for AMD/ARB_conservative_depth. */
1176 if (!$$.flags.i &&
1177 (state->AMD_conservative_depth_enable ||
1178 state->ARB_conservative_depth_enable)) {
1179 if (strcmp($1, "depth_any") == 0) {
1180 $$.flags.q.depth_any = 1;
1181 } else if (strcmp($1, "depth_greater") == 0) {
1182 $$.flags.q.depth_greater = 1;
1183 } else if (strcmp($1, "depth_less") == 0) {
1184 $$.flags.q.depth_less = 1;
1185 } else if (strcmp($1, "depth_unchanged") == 0) {
1186 $$.flags.q.depth_unchanged = 1;
1187 }
1188
1189 if ($$.flags.i && state->AMD_conservative_depth_warn) {
1190 _mesa_glsl_warning(& @1, state,
1191 "GL_AMD_conservative_depth "
1192 "layout qualifier `%s' is used", $1);
1193 }
1194 if ($$.flags.i && state->ARB_conservative_depth_warn) {
1195 _mesa_glsl_warning(& @1, state,
1196 "GL_ARB_conservative_depth "
1197 "layout qualifier `%s' is used", $1);
1198 }
1199 }
1200
1201 /* See also interface_block_layout_qualifier. */
1202 if (!$$.flags.i && state->ARB_uniform_buffer_object_enable) {
1203 if (strcmp($1, "std140") == 0) {
1204 $$.flags.q.std140 = 1;
1205 } else if (strcmp($1, "shared") == 0) {
1206 $$.flags.q.shared = 1;
1207 } else if (strcmp($1, "column_major") == 0) {
1208 $$.flags.q.column_major = 1;
1209 /* "row_major" is a reserved word in GLSL 1.30+. Its token is parsed
1210 * below in the interface_block_layout_qualifier rule.
1211 *
1212 * It is not a reserved word in GLSL ES 3.00, so it's handled here as
1213 * an identifier.
1214 */
1215 } else if (strcmp($1, "row_major") == 0) {
1216 $$.flags.q.row_major = 1;
1217 }
1218
1219 if ($$.flags.i && state->ARB_uniform_buffer_object_warn) {
1220 _mesa_glsl_warning(& @1, state,
1221 "#version 140 / GL_ARB_uniform_buffer_object "
1222 "layout qualifier `%s' is used", $1);
1223 }
1224 }
1225
1226 /* Layout qualifiers for GLSL 1.50 geometry shaders. */
1227 if (!$$.flags.i) {
1228 struct {
1229 const char *s;
1230 GLenum e;
1231 } map[] = {
1232 { "points", GL_POINTS },
1233 { "lines", GL_LINES },
1234 { "lines_adjacency", GL_LINES_ADJACENCY },
1235 { "line_strip", GL_LINE_STRIP },
1236 { "triangles", GL_TRIANGLES },
1237 { "triangles_adjacency", GL_TRIANGLES_ADJACENCY },
1238 { "triangle_strip", GL_TRIANGLE_STRIP },
1239 };
1240 for (unsigned i = 0; i < Elements(map); i++) {
1241 if (strcmp($1, map[i].s) == 0) {
1242 $$.flags.q.prim_type = 1;
1243 $$.prim_type = map[i].e;
1244 break;
1245 }
1246 }
1247
1248 if ($$.flags.i && !state->is_version(150, 0)) {
1249 _mesa_glsl_error(& @1, state, "#version 150 layout "
1250 "qualifier `%s' used", $1);
1251 }
1252 }
1253
1254 if (!$$.flags.i) {
1255 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1256 "`%s'", $1);
1257 YYERROR;
1258 }
1259 }
1260 | any_identifier '=' integer_constant
1261 {
1262 memset(& $$, 0, sizeof($$));
1263
1264 if (state->ARB_explicit_attrib_location_enable) {
1265 if (strcmp("location", $1) == 0) {
1266 $$.flags.q.explicit_location = 1;
1267
1268 if ($3 >= 0) {
1269 $$.location = $3;
1270 } else {
1271 _mesa_glsl_error(& @3, state,
1272 "invalid location %d specified", $3);
1273 YYERROR;
1274 }
1275 }
1276
1277 if (strcmp("index", $1) == 0) {
1278 $$.flags.q.explicit_index = 1;
1279
1280 if ($3 >= 0) {
1281 $$.index = $3;
1282 } else {
1283 _mesa_glsl_error(& @3, state,
1284 "invalid index %d specified", $3);
1285 YYERROR;
1286 }
1287 }
1288 }
1289
1290 if (state->ARB_shading_language_420pack_enable &&
1291 strcmp("binding", $1) == 0) {
1292 $$.flags.q.explicit_binding = 1;
1293 $$.binding = $3;
1294 }
1295
1296 if (strcmp("max_vertices", $1) == 0) {
1297 $$.flags.q.max_vertices = 1;
1298
1299 if ($3 < 0) {
1300 _mesa_glsl_error(& @3, state,
1301 "invalid max_vertices %d specified", $3);
1302 YYERROR;
1303 } else {
1304 $$.max_vertices = $3;
1305 if (!state->is_version(150, 0)) {
1306 _mesa_glsl_error(& @3, state,
1307 "#version 150 max_vertices qualifier "
1308 "specified", $3);
1309 }
1310 }
1311 }
1312
1313 /* If the identifier didn't match any known layout identifiers,
1314 * emit an error.
1315 */
1316 if (!$$.flags.i) {
1317 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1318 "`%s'", $1);
1319 YYERROR;
1320 } else if (state->ARB_explicit_attrib_location_warn) {
1321 _mesa_glsl_warning(& @1, state,
1322 "GL_ARB_explicit_attrib_location layout "
1323 "identifier `%s' used", $1);
1324 }
1325 }
1326 | interface_block_layout_qualifier
1327 {
1328 $$ = $1;
1329 /* Layout qualifiers for ARB_uniform_buffer_object. */
1330 if ($$.flags.q.uniform && !state->ARB_uniform_buffer_object_enable) {
1331 _mesa_glsl_error(& @1, state,
1332 "#version 140 / GL_ARB_uniform_buffer_object "
1333 "layout qualifier `%s' is used", $1);
1334 } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1335 _mesa_glsl_warning(& @1, state,
1336 "#version 140 / GL_ARB_uniform_buffer_object "
1337 "layout qualifier `%s' is used", $1);
1338 }
1339 }
1340 ;
1341
1342 /* This is a separate language rule because we parse these as tokens
1343 * (due to them being reserved keywords) instead of identifiers like
1344 * most qualifiers. See the any_identifier path of
1345 * layout_qualifier_id for the others.
1346 */
1347 interface_block_layout_qualifier:
1348 ROW_MAJOR
1349 {
1350 memset(& $$, 0, sizeof($$));
1351 $$.flags.q.row_major = 1;
1352 }
1353 | PACKED_TOK
1354 {
1355 memset(& $$, 0, sizeof($$));
1356 $$.flags.q.packed = 1;
1357 }
1358 ;
1359
1360 interpolation_qualifier:
1361 SMOOTH
1362 {
1363 memset(& $$, 0, sizeof($$));
1364 $$.flags.q.smooth = 1;
1365 }
1366 | FLAT
1367 {
1368 memset(& $$, 0, sizeof($$));
1369 $$.flags.q.flat = 1;
1370 }
1371 | NOPERSPECTIVE
1372 {
1373 memset(& $$, 0, sizeof($$));
1374 $$.flags.q.noperspective = 1;
1375 }
1376 ;
1377
1378 type_qualifier:
1379 /* Single qualifiers */
1380 INVARIANT
1381 {
1382 memset(& $$, 0, sizeof($$));
1383 $$.flags.q.invariant = 1;
1384 }
1385 | auxiliary_storage_qualifier
1386 | storage_qualifier
1387 | interpolation_qualifier
1388 | layout_qualifier
1389 | precision_qualifier
1390 {
1391 memset(&$$, 0, sizeof($$));
1392 $$.precision = $1;
1393 }
1394
1395 /* Multiple qualifiers:
1396 * In GLSL 4.20, these can be specified in any order. In earlier versions,
1397 * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1398 *
1399 * invariant interpolation auxiliary storage precision ...or...
1400 * layout storage precision
1401 *
1402 * Each qualifier's rule ensures that the accumulated qualifiers on the right
1403 * side don't contain any that must appear on the left hand side.
1404 * For example, when processing a storage qualifier, we check that there are
1405 * no auxiliary, interpolation, layout, or invariant qualifiers to the right.
1406 */
1407 | INVARIANT type_qualifier
1408 {
1409 if ($2.flags.q.invariant)
1410 _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1411
1412 if ($2.has_layout()) {
1413 _mesa_glsl_error(&@1, state,
1414 "\"invariant\" cannot be used with layout(...)");
1415 }
1416
1417 $$ = $2;
1418 $$.flags.q.invariant = 1;
1419 }
1420 | interpolation_qualifier type_qualifier
1421 {
1422 /* Section 4.3 of the GLSL 1.40 specification states:
1423 * "...qualified with one of these interpolation qualifiers"
1424 *
1425 * GLSL 1.30 claims to allow "one or more", but insists that:
1426 * "These interpolation qualifiers may only precede the qualifiers in,
1427 * centroid in, out, or centroid out in a declaration."
1428 *
1429 * ...which means that e.g. smooth can't precede smooth, so there can be
1430 * only one after all, and the 1.40 text is a clarification, not a change.
1431 */
1432 if ($2.has_interpolation())
1433 _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1434
1435 if ($2.has_layout()) {
1436 _mesa_glsl_error(&@1, state, "interpolation qualifiers cannot be used "
1437 "with layout(...)");
1438 }
1439
1440 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.invariant) {
1441 _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1442 "after \"invariant\"");
1443 }
1444
1445 $$ = $1;
1446 $$.merge_qualifier(&@1, state, $2);
1447 }
1448 | layout_qualifier type_qualifier
1449 {
1450 /* The GLSL 1.50 grammar indicates that a layout(...) declaration can be
1451 * used standalone or immediately before a storage qualifier. It cannot
1452 * be used with interpolation qualifiers or invariant. There does not
1453 * appear to be any text indicating that it must come before the storage
1454 * qualifier, but always seems to in examples.
1455 */
1456 if (!state->ARB_shading_language_420pack_enable && $2.has_layout())
1457 _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
1458
1459 if ($2.flags.q.invariant)
1460 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1461 "the \"invariant\" qualifier");
1462
1463 if ($2.has_interpolation()) {
1464 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1465 "interpolation qualifiers");
1466 }
1467
1468 $$ = $1;
1469 $$.merge_qualifier(&@1, state, $2);
1470 }
1471 | auxiliary_storage_qualifier type_qualifier
1472 {
1473 if ($2.has_auxiliary_storage()) {
1474 _mesa_glsl_error(&@1, state,
1475 "duplicate auxiliary storage qualifier (centroid)");
1476 }
1477
1478 if (!state->ARB_shading_language_420pack_enable &&
1479 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout())) {
1480 _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
1481 "just before storage qualifiers");
1482 }
1483 $$ = $1;
1484 $$.flags.i |= $2.flags.i;
1485 }
1486 | storage_qualifier type_qualifier
1487 {
1488 /* Section 4.3 of the GLSL 1.20 specification states:
1489 * "Variable declarations may have a storage qualifier specified..."
1490 * 1.30 clarifies this to "may have one storage qualifier".
1491 */
1492 if ($2.has_storage())
1493 _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
1494
1495 if (!state->ARB_shading_language_420pack_enable &&
1496 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout() ||
1497 $2.has_auxiliary_storage())) {
1498 _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
1499 "invariant, interpolation, layout and auxiliary "
1500 "storage qualifiers");
1501 }
1502
1503 $$ = $1;
1504 $$.merge_qualifier(&@1, state, $2);
1505 }
1506 | precision_qualifier type_qualifier
1507 {
1508 if ($2.precision != ast_precision_none)
1509 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
1510
1511 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
1512 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
1513
1514 $$ = $2;
1515 $$.precision = $1;
1516 }
1517 ;
1518
1519 auxiliary_storage_qualifier:
1520 CENTROID
1521 {
1522 memset(& $$, 0, sizeof($$));
1523 $$.flags.q.centroid = 1;
1524 }
1525 /* TODO: "sample" and "patch" also go here someday. */
1526
1527 storage_qualifier:
1528 CONST_TOK
1529 {
1530 memset(& $$, 0, sizeof($$));
1531 $$.flags.q.constant = 1;
1532 }
1533 | ATTRIBUTE
1534 {
1535 memset(& $$, 0, sizeof($$));
1536 $$.flags.q.attribute = 1;
1537 }
1538 | VARYING
1539 {
1540 memset(& $$, 0, sizeof($$));
1541 $$.flags.q.varying = 1;
1542 }
1543 | IN_TOK
1544 {
1545 memset(& $$, 0, sizeof($$));
1546 $$.flags.q.in = 1;
1547 }
1548 | OUT_TOK
1549 {
1550 memset(& $$, 0, sizeof($$));
1551 $$.flags.q.out = 1;
1552 }
1553 | UNIFORM
1554 {
1555 memset(& $$, 0, sizeof($$));
1556 $$.flags.q.uniform = 1;
1557 }
1558 ;
1559
1560 type_specifier:
1561 type_specifier_nonarray
1562 | type_specifier_nonarray '[' ']'
1563 {
1564 $$ = $1;
1565 $$->is_array = true;
1566 $$->array_size = NULL;
1567 }
1568 | type_specifier_nonarray '[' constant_expression ']'
1569 {
1570 $$ = $1;
1571 $$->is_array = true;
1572 $$->array_size = $3;
1573 }
1574 ;
1575
1576 type_specifier_nonarray:
1577 basic_type_specifier_nonarray
1578 {
1579 void *ctx = state;
1580 $$ = new(ctx) ast_type_specifier($1);
1581 $$->set_location(yylloc);
1582 }
1583 | struct_specifier
1584 {
1585 void *ctx = state;
1586 $$ = new(ctx) ast_type_specifier($1);
1587 $$->set_location(yylloc);
1588 }
1589 | TYPE_IDENTIFIER
1590 {
1591 void *ctx = state;
1592 $$ = new(ctx) ast_type_specifier($1);
1593 $$->set_location(yylloc);
1594 }
1595 ;
1596
1597 basic_type_specifier_nonarray:
1598 VOID_TOK { $$ = "void"; }
1599 | FLOAT_TOK { $$ = "float"; }
1600 | INT_TOK { $$ = "int"; }
1601 | UINT_TOK { $$ = "uint"; }
1602 | BOOL_TOK { $$ = "bool"; }
1603 | VEC2 { $$ = "vec2"; }
1604 | VEC3 { $$ = "vec3"; }
1605 | VEC4 { $$ = "vec4"; }
1606 | BVEC2 { $$ = "bvec2"; }
1607 | BVEC3 { $$ = "bvec3"; }
1608 | BVEC4 { $$ = "bvec4"; }
1609 | IVEC2 { $$ = "ivec2"; }
1610 | IVEC3 { $$ = "ivec3"; }
1611 | IVEC4 { $$ = "ivec4"; }
1612 | UVEC2 { $$ = "uvec2"; }
1613 | UVEC3 { $$ = "uvec3"; }
1614 | UVEC4 { $$ = "uvec4"; }
1615 | MAT2X2 { $$ = "mat2"; }
1616 | MAT2X3 { $$ = "mat2x3"; }
1617 | MAT2X4 { $$ = "mat2x4"; }
1618 | MAT3X2 { $$ = "mat3x2"; }
1619 | MAT3X3 { $$ = "mat3"; }
1620 | MAT3X4 { $$ = "mat3x4"; }
1621 | MAT4X2 { $$ = "mat4x2"; }
1622 | MAT4X3 { $$ = "mat4x3"; }
1623 | MAT4X4 { $$ = "mat4"; }
1624 | SAMPLER1D { $$ = "sampler1D"; }
1625 | SAMPLER2D { $$ = "sampler2D"; }
1626 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
1627 | SAMPLER3D { $$ = "sampler3D"; }
1628 | SAMPLERCUBE { $$ = "samplerCube"; }
1629 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
1630 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
1631 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
1632 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
1633 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
1634 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
1635 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
1636 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
1637 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
1638 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
1639 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
1640 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
1641 | ISAMPLER1D { $$ = "isampler1D"; }
1642 | ISAMPLER2D { $$ = "isampler2D"; }
1643 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
1644 | ISAMPLER3D { $$ = "isampler3D"; }
1645 | ISAMPLERCUBE { $$ = "isamplerCube"; }
1646 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
1647 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
1648 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
1649 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
1650 | USAMPLER1D { $$ = "usampler1D"; }
1651 | USAMPLER2D { $$ = "usampler2D"; }
1652 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
1653 | USAMPLER3D { $$ = "usampler3D"; }
1654 | USAMPLERCUBE { $$ = "usamplerCube"; }
1655 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
1656 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
1657 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
1658 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
1659 | SAMPLER2DMS { $$ = "sampler2DMS"; }
1660 | ISAMPLER2DMS { $$ = "isampler2DMS"; }
1661 | USAMPLER2DMS { $$ = "usampler2DMS"; }
1662 | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
1663 | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
1664 | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
1665 ;
1666
1667 precision_qualifier:
1668 HIGHP
1669 {
1670 state->check_precision_qualifiers_allowed(&@1);
1671 $$ = ast_precision_high;
1672 }
1673 | MEDIUMP
1674 {
1675 state->check_precision_qualifiers_allowed(&@1);
1676 $$ = ast_precision_medium;
1677 }
1678 | LOWP
1679 {
1680 state->check_precision_qualifiers_allowed(&@1);
1681 $$ = ast_precision_low;
1682 }
1683 ;
1684
1685 struct_specifier:
1686 STRUCT any_identifier '{' struct_declaration_list '}'
1687 {
1688 void *ctx = state;
1689 $$ = new(ctx) ast_struct_specifier($2, $4);
1690 $$->set_location(yylloc);
1691 state->symbols->add_type($2, glsl_type::void_type);
1692 state->symbols->add_type_ast($2, new(ctx) ast_type_specifier($$));
1693 }
1694 | STRUCT '{' struct_declaration_list '}'
1695 {
1696 void *ctx = state;
1697 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1698 $$->set_location(yylloc);
1699 }
1700 ;
1701
1702 struct_declaration_list:
1703 struct_declaration
1704 {
1705 $$ = $1;
1706 $1->link.self_link();
1707 }
1708 | struct_declaration_list struct_declaration
1709 {
1710 $$ = $1;
1711 $$->link.insert_before(& $2->link);
1712 }
1713 ;
1714
1715 struct_declaration:
1716 type_specifier struct_declarator_list ';'
1717 {
1718 void *ctx = state;
1719 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1720 type->set_location(yylloc);
1721
1722 type->specifier = $1;
1723 $$ = new(ctx) ast_declarator_list(type);
1724 $$->set_location(yylloc);
1725
1726 $$->declarations.push_degenerate_list_at_head(& $2->link);
1727 }
1728 ;
1729
1730 struct_declarator_list:
1731 struct_declarator
1732 {
1733 $$ = $1;
1734 $1->link.self_link();
1735 }
1736 | struct_declarator_list ',' struct_declarator
1737 {
1738 $$ = $1;
1739 $$->link.insert_before(& $3->link);
1740 }
1741 ;
1742
1743 struct_declarator:
1744 any_identifier
1745 {
1746 void *ctx = state;
1747 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1748 $$->set_location(yylloc);
1749 }
1750 | any_identifier '[' constant_expression ']'
1751 {
1752 void *ctx = state;
1753 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1754 $$->set_location(yylloc);
1755 }
1756 ;
1757
1758 initializer:
1759 assignment_expression
1760 | '{' initializer_list '}'
1761 {
1762 $$ = $2;
1763 }
1764 | '{' initializer_list ',' '}'
1765 {
1766 $$ = $2;
1767 }
1768 ;
1769
1770 initializer_list:
1771 initializer
1772 {
1773 void *ctx = state;
1774 $$ = new(ctx) ast_aggregate_initializer();
1775 $$->set_location(yylloc);
1776 $$->expressions.push_tail(& $1->link);
1777 }
1778 | initializer_list ',' initializer
1779 {
1780 $1->expressions.push_tail(& $3->link);
1781 }
1782 ;
1783
1784 declaration_statement:
1785 declaration
1786 ;
1787
1788 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1789 // supported.
1790 statement:
1791 compound_statement { $$ = (ast_node *) $1; }
1792 | simple_statement
1793 ;
1794
1795 simple_statement:
1796 declaration_statement
1797 | expression_statement
1798 | selection_statement
1799 | switch_statement
1800 | iteration_statement
1801 | jump_statement
1802 ;
1803
1804 compound_statement:
1805 '{' '}'
1806 {
1807 void *ctx = state;
1808 $$ = new(ctx) ast_compound_statement(true, NULL);
1809 $$->set_location(yylloc);
1810 }
1811 | '{'
1812 {
1813 state->symbols->push_scope();
1814 }
1815 statement_list '}'
1816 {
1817 void *ctx = state;
1818 $$ = new(ctx) ast_compound_statement(true, $3);
1819 $$->set_location(yylloc);
1820 state->symbols->pop_scope();
1821 }
1822 ;
1823
1824 statement_no_new_scope:
1825 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1826 | simple_statement
1827 ;
1828
1829 compound_statement_no_new_scope:
1830 '{' '}'
1831 {
1832 void *ctx = state;
1833 $$ = new(ctx) ast_compound_statement(false, NULL);
1834 $$->set_location(yylloc);
1835 }
1836 | '{' statement_list '}'
1837 {
1838 void *ctx = state;
1839 $$ = new(ctx) ast_compound_statement(false, $2);
1840 $$->set_location(yylloc);
1841 }
1842 ;
1843
1844 statement_list:
1845 statement
1846 {
1847 if ($1 == NULL) {
1848 _mesa_glsl_error(& @1, state, "<nil> statement");
1849 assert($1 != NULL);
1850 }
1851
1852 $$ = $1;
1853 $$->link.self_link();
1854 }
1855 | statement_list statement
1856 {
1857 if ($2 == NULL) {
1858 _mesa_glsl_error(& @2, state, "<nil> statement");
1859 assert($2 != NULL);
1860 }
1861 $$ = $1;
1862 $$->link.insert_before(& $2->link);
1863 }
1864 ;
1865
1866 expression_statement:
1867 ';'
1868 {
1869 void *ctx = state;
1870 $$ = new(ctx) ast_expression_statement(NULL);
1871 $$->set_location(yylloc);
1872 }
1873 | expression ';'
1874 {
1875 void *ctx = state;
1876 $$ = new(ctx) ast_expression_statement($1);
1877 $$->set_location(yylloc);
1878 }
1879 ;
1880
1881 selection_statement:
1882 IF '(' expression ')' selection_rest_statement
1883 {
1884 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1885 $5.else_statement);
1886 $$->set_location(yylloc);
1887 }
1888 ;
1889
1890 selection_rest_statement:
1891 statement ELSE statement
1892 {
1893 $$.then_statement = $1;
1894 $$.else_statement = $3;
1895 }
1896 | statement %prec THEN
1897 {
1898 $$.then_statement = $1;
1899 $$.else_statement = NULL;
1900 }
1901 ;
1902
1903 condition:
1904 expression
1905 {
1906 $$ = (ast_node *) $1;
1907 }
1908 | fully_specified_type any_identifier '=' initializer
1909 {
1910 void *ctx = state;
1911 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1912 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1913 decl->set_location(yylloc);
1914 declarator->set_location(yylloc);
1915
1916 declarator->declarations.push_tail(&decl->link);
1917 $$ = declarator;
1918 }
1919 ;
1920
1921 /*
1922 * siwtch_statement grammar is based on the syntax described in the body
1923 * of the GLSL spec, not in it's appendix!!!
1924 */
1925 switch_statement:
1926 SWITCH '(' expression ')' switch_body
1927 {
1928 $$ = new(state) ast_switch_statement($3, $5);
1929 $$->set_location(yylloc);
1930 }
1931 ;
1932
1933 switch_body:
1934 '{' '}'
1935 {
1936 $$ = new(state) ast_switch_body(NULL);
1937 $$->set_location(yylloc);
1938 }
1939 | '{' case_statement_list '}'
1940 {
1941 $$ = new(state) ast_switch_body($2);
1942 $$->set_location(yylloc);
1943 }
1944 ;
1945
1946 case_label:
1947 CASE expression ':'
1948 {
1949 $$ = new(state) ast_case_label($2);
1950 $$->set_location(yylloc);
1951 }
1952 | DEFAULT ':'
1953 {
1954 $$ = new(state) ast_case_label(NULL);
1955 $$->set_location(yylloc);
1956 }
1957 ;
1958
1959 case_label_list:
1960 case_label
1961 {
1962 ast_case_label_list *labels = new(state) ast_case_label_list();
1963
1964 labels->labels.push_tail(& $1->link);
1965 $$ = labels;
1966 $$->set_location(yylloc);
1967 }
1968 | case_label_list case_label
1969 {
1970 $$ = $1;
1971 $$->labels.push_tail(& $2->link);
1972 }
1973 ;
1974
1975 case_statement:
1976 case_label_list statement
1977 {
1978 ast_case_statement *stmts = new(state) ast_case_statement($1);
1979 stmts->set_location(yylloc);
1980
1981 stmts->stmts.push_tail(& $2->link);
1982 $$ = stmts;
1983 }
1984 | case_statement statement
1985 {
1986 $$ = $1;
1987 $$->stmts.push_tail(& $2->link);
1988 }
1989 ;
1990
1991 case_statement_list:
1992 case_statement
1993 {
1994 ast_case_statement_list *cases= new(state) ast_case_statement_list();
1995 cases->set_location(yylloc);
1996
1997 cases->cases.push_tail(& $1->link);
1998 $$ = cases;
1999 }
2000 | case_statement_list case_statement
2001 {
2002 $$ = $1;
2003 $$->cases.push_tail(& $2->link);
2004 }
2005 ;
2006
2007 iteration_statement:
2008 WHILE '(' condition ')' statement_no_new_scope
2009 {
2010 void *ctx = state;
2011 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2012 NULL, $3, NULL, $5);
2013 $$->set_location(yylloc);
2014 }
2015 | DO statement WHILE '(' expression ')' ';'
2016 {
2017 void *ctx = state;
2018 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2019 NULL, $5, NULL, $2);
2020 $$->set_location(yylloc);
2021 }
2022 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2023 {
2024 void *ctx = state;
2025 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2026 $3, $4.cond, $4.rest, $6);
2027 $$->set_location(yylloc);
2028 }
2029 ;
2030
2031 for_init_statement:
2032 expression_statement
2033 | declaration_statement
2034 ;
2035
2036 conditionopt:
2037 condition
2038 | /* empty */
2039 {
2040 $$ = NULL;
2041 }
2042 ;
2043
2044 for_rest_statement:
2045 conditionopt ';'
2046 {
2047 $$.cond = $1;
2048 $$.rest = NULL;
2049 }
2050 | conditionopt ';' expression
2051 {
2052 $$.cond = $1;
2053 $$.rest = $3;
2054 }
2055 ;
2056
2057 // Grammar Note: No 'goto'. Gotos are not supported.
2058 jump_statement:
2059 CONTINUE ';'
2060 {
2061 void *ctx = state;
2062 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2063 $$->set_location(yylloc);
2064 }
2065 | BREAK ';'
2066 {
2067 void *ctx = state;
2068 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2069 $$->set_location(yylloc);
2070 }
2071 | RETURN ';'
2072 {
2073 void *ctx = state;
2074 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2075 $$->set_location(yylloc);
2076 }
2077 | RETURN expression ';'
2078 {
2079 void *ctx = state;
2080 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2081 $$->set_location(yylloc);
2082 }
2083 | DISCARD ';' // Fragment shader only.
2084 {
2085 void *ctx = state;
2086 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2087 $$->set_location(yylloc);
2088 }
2089 ;
2090
2091 external_declaration:
2092 function_definition { $$ = $1; }
2093 | declaration { $$ = $1; }
2094 | pragma_statement { $$ = NULL; }
2095 | layout_defaults { $$ = $1; }
2096 ;
2097
2098 function_definition:
2099 function_prototype compound_statement_no_new_scope
2100 {
2101 void *ctx = state;
2102 $$ = new(ctx) ast_function_definition();
2103 $$->set_location(yylloc);
2104 $$->prototype = $1;
2105 $$->body = $2;
2106
2107 state->symbols->pop_scope();
2108 }
2109 ;
2110
2111 /* layout_qualifieropt is packed into this rule */
2112 interface_block:
2113 basic_interface_block
2114 {
2115 $$ = $1;
2116 }
2117 | layout_qualifier basic_interface_block
2118 {
2119 ast_interface_block *block = $2;
2120 if (!block->layout.merge_qualifier(& @1, state, $1)) {
2121 YYERROR;
2122 }
2123 $$ = block;
2124 }
2125 ;
2126
2127 basic_interface_block:
2128 interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2129 {
2130 ast_interface_block *const block = $6;
2131
2132 block->block_name = $2;
2133 block->declarations.push_degenerate_list_at_head(& $4->link);
2134
2135 if ($1.flags.q.uniform) {
2136 if (!state->ARB_uniform_buffer_object_enable) {
2137 _mesa_glsl_error(& @1, state,
2138 "#version 140 / GL_ARB_uniform_buffer_object "
2139 "required for defining uniform blocks");
2140 } else if (state->ARB_uniform_buffer_object_warn) {
2141 _mesa_glsl_warning(& @1, state,
2142 "#version 140 / GL_ARB_uniform_buffer_object "
2143 "required for defining uniform blocks");
2144 }
2145 } else {
2146 if (state->es_shader || state->language_version < 150) {
2147 _mesa_glsl_error(& @1, state,
2148 "#version 150 required for using "
2149 "interface blocks");
2150 }
2151 }
2152
2153 /* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):
2154 * "It is illegal to have an input block in a vertex shader
2155 * or an output block in a fragment shader"
2156 */
2157 if ((state->target == vertex_shader) && $1.flags.q.in) {
2158 _mesa_glsl_error(& @1, state,
2159 "`in' interface block is not allowed for "
2160 "a vertex shader");
2161 } else if ((state->target == fragment_shader) && $1.flags.q.out) {
2162 _mesa_glsl_error(& @1, state,
2163 "`out' interface block is not allowed for "
2164 "a fragment shader");
2165 }
2166
2167 /* Since block arrays require names, and both features are added in
2168 * the same language versions, we don't have to explicitly
2169 * version-check both things.
2170 */
2171 if (block->instance_name != NULL) {
2172 state->check_version(150, 300, & @1, "interface blocks with "
2173 "an instance name are not allowed");
2174 }
2175
2176 unsigned interface_type_mask;
2177 struct ast_type_qualifier temp_type_qualifier;
2178
2179 /* Get a bitmask containing only the in/out/uniform flags, allowing us
2180 * to ignore other irrelevant flags like interpolation qualifiers.
2181 */
2182 temp_type_qualifier.flags.i = 0;
2183 temp_type_qualifier.flags.q.uniform = true;
2184 temp_type_qualifier.flags.q.in = true;
2185 temp_type_qualifier.flags.q.out = true;
2186 interface_type_mask = temp_type_qualifier.flags.i;
2187
2188 /* Get the block's interface qualifier. The interface_qualifier
2189 * production rule guarantees that only one bit will be set (and
2190 * it will be in/out/uniform).
2191 */
2192 unsigned block_interface_qualifier = $1.flags.i;
2193
2194 block->layout.flags.i |= block_interface_qualifier;
2195
2196 foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
2197 ast_type_qualifier& qualifier = member->type->qualifier;
2198 if ((qualifier.flags.i & interface_type_mask) == 0) {
2199 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2200 * "If no optional qualifier is used in a member declaration, the
2201 * qualifier of the variable is just in, out, or uniform as declared
2202 * by interface-qualifier."
2203 */
2204 qualifier.flags.i |= block_interface_qualifier;
2205 } else if ((qualifier.flags.i & interface_type_mask) !=
2206 block_interface_qualifier) {
2207 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2208 * "If optional qualifiers are used, they can include interpolation
2209 * and storage qualifiers and they must declare an input, output,
2210 * or uniform variable consistent with the interface qualifier of
2211 * the block."
2212 */
2213 _mesa_glsl_error(& @1, state,
2214 "uniform/in/out qualifier on "
2215 "interface block member does not match "
2216 "the interface block");
2217 }
2218 }
2219
2220 $$ = block;
2221 }
2222 ;
2223
2224 interface_qualifier:
2225 IN_TOK
2226 {
2227 memset(& $$, 0, sizeof($$));
2228 $$.flags.q.in = 1;
2229 }
2230 | OUT_TOK
2231 {
2232 memset(& $$, 0, sizeof($$));
2233 $$.flags.q.out = 1;
2234 }
2235 | UNIFORM
2236 {
2237 memset(& $$, 0, sizeof($$));
2238 $$.flags.q.uniform = 1;
2239 }
2240 ;
2241
2242 instance_name_opt:
2243 /* empty */
2244 {
2245 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2246 NULL, NULL);
2247 }
2248 | NEW_IDENTIFIER
2249 {
2250 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2251 $1, NULL);
2252 }
2253 | NEW_IDENTIFIER '[' constant_expression ']'
2254 {
2255 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2256 $1, $3);
2257 }
2258 | NEW_IDENTIFIER '[' ']'
2259 {
2260 _mesa_glsl_error(& @1, state,
2261 "instance block arrays must be explicitly sized");
2262
2263 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2264 $1, NULL);
2265 }
2266 ;
2267
2268 member_list:
2269 member_declaration
2270 {
2271 $$ = $1;
2272 $1->link.self_link();
2273 }
2274 | member_declaration member_list
2275 {
2276 $$ = $1;
2277 $2->link.insert_before(& $$->link);
2278 }
2279 ;
2280
2281 member_declaration:
2282 fully_specified_type struct_declarator_list ';'
2283 {
2284 void *ctx = state;
2285 ast_fully_specified_type *type = $1;
2286 type->set_location(yylloc);
2287
2288 if (type->qualifier.flags.q.attribute) {
2289 _mesa_glsl_error(& @1, state,
2290 "keyword 'attribute' cannot be used with "
2291 "interface block member");
2292 } else if (type->qualifier.flags.q.varying) {
2293 _mesa_glsl_error(& @1, state,
2294 "keyword 'varying' cannot be used with "
2295 "interface block member");
2296 }
2297
2298 $$ = new(ctx) ast_declarator_list(type);
2299 $$->set_location(yylloc);
2300 $$->ubo_qualifiers_valid = true;
2301
2302 $$->declarations.push_degenerate_list_at_head(& $2->link);
2303 }
2304 ;
2305
2306 layout_defaults:
2307 layout_qualifier UNIFORM ';'
2308 {
2309 if (!state->default_uniform_qualifier->merge_qualifier(& @1, state, $1)) {
2310 YYERROR;
2311 }
2312 $$ = NULL;
2313 }
2314
2315 | layout_qualifier IN_TOK ';'
2316 {
2317 void *ctx = state;
2318 if (state->target != geometry_shader) {
2319 _mesa_glsl_error(& @1, state,
2320 "input layout qualifiers only valid in "
2321 "geometry shaders");
2322 } else if (!$1.flags.q.prim_type) {
2323 _mesa_glsl_error(& @1, state,
2324 "input layout qualifiers must specify a primitive"
2325 " type");
2326 }
2327 $$ = new(ctx) ast_gs_input_layout(@1, $1.prim_type);
2328 }
2329
2330 | layout_qualifier OUT_TOK ';'
2331 {
2332 if (state->target != geometry_shader) {
2333 _mesa_glsl_error(& @1, state,
2334 "out layout qualifiers only valid in "
2335 "geometry shaders");
2336 } else if (!state->out_qualifier->merge_qualifier(& @1, state, $1)) {
2337 YYERROR;
2338 }
2339 $$ = NULL;
2340 }