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