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