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