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