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