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