glcpp/tests: Add test to check #line followed by code
[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 %%
242
243 translation_unit:
244 version_statement extension_statement_list
245 {
246 _mesa_glsl_initialize_types(state);
247 }
248 external_declaration_list
249 {
250 delete state->symbols;
251 state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
252 _mesa_glsl_initialize_types(state);
253 }
254 ;
255
256 version_statement:
257 /* blank - no #version specified: defaults are already set */
258 | VERSION_TOK INTCONSTANT EOL
259 {
260 bool supported = false;
261
262 switch ($2) {
263 case 100:
264 state->es_shader = true;
265 supported = state->ctx->API == API_OPENGLES2 ||
266 state->ctx->Extensions.ARB_ES2_compatibility;
267 break;
268 case 110:
269 case 120:
270 /* FINISHME: Once the OpenGL 3.0 'forward compatible' context or
271 * the OpenGL 3.2 Core context is supported, this logic will need
272 * change. Older versions of GLSL are no longer supported
273 * outside the compatibility contexts of 3.x.
274 */
275 case 130:
276 case 140:
277 case 150:
278 case 330:
279 case 400:
280 case 410:
281 case 420:
282 supported = _mesa_is_desktop_gl(state->ctx) &&
283 ((unsigned) $2) <= state->ctx->Const.GLSLVersion;
284 break;
285 default:
286 supported = false;
287 break;
288 }
289
290 state->language_version = $2;
291 state->version_string =
292 ralloc_asprintf(state, "GLSL%s %d.%02d",
293 state->es_shader ? " ES" : "",
294 state->language_version / 100,
295 state->language_version % 100);
296
297 if (!supported) {
298 _mesa_glsl_error(& @2, state, "%s is not supported. "
299 "Supported versions are: %s\n",
300 state->version_string,
301 state->supported_version_string);
302 }
303
304 if (state->language_version >= 140) {
305 state->ARB_uniform_buffer_object_enable = true;
306 }
307 }
308 ;
309
310 pragma_statement:
311 PRAGMA_DEBUG_ON EOL
312 | PRAGMA_DEBUG_OFF EOL
313 | PRAGMA_OPTIMIZE_ON EOL
314 | PRAGMA_OPTIMIZE_OFF EOL
315 | PRAGMA_INVARIANT_ALL EOL
316 {
317 if (state->language_version == 110) {
318 _mesa_glsl_warning(& @1, state,
319 "pragma `invariant(all)' not supported in %s",
320 state->version_string);
321 } else {
322 state->all_invariant = true;
323 }
324 }
325 ;
326
327 extension_statement_list:
328
329 | extension_statement_list extension_statement
330 ;
331
332 any_identifier:
333 IDENTIFIER
334 | TYPE_IDENTIFIER
335 | NEW_IDENTIFIER
336 ;
337
338 extension_statement:
339 EXTENSION any_identifier COLON any_identifier EOL
340 {
341 if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
342 YYERROR;
343 }
344 }
345 ;
346
347 external_declaration_list:
348 external_declaration
349 {
350 /* FINISHME: The NULL test is required because pragmas are set to
351 * FINISHME: NULL. (See production rule for external_declaration.)
352 */
353 if ($1 != NULL)
354 state->translation_unit.push_tail(& $1->link);
355 }
356 | external_declaration_list external_declaration
357 {
358 /* FINISHME: The NULL test is required because pragmas are set to
359 * FINISHME: NULL. (See production rule for external_declaration.)
360 */
361 if ($2 != NULL)
362 state->translation_unit.push_tail(& $2->link);
363 }
364 ;
365
366 variable_identifier:
367 IDENTIFIER
368 | NEW_IDENTIFIER
369 ;
370
371 primary_expression:
372 variable_identifier
373 {
374 void *ctx = state;
375 $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
376 $$->set_location(yylloc);
377 $$->primary_expression.identifier = $1;
378 }
379 | INTCONSTANT
380 {
381 void *ctx = state;
382 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
383 $$->set_location(yylloc);
384 $$->primary_expression.int_constant = $1;
385 }
386 | UINTCONSTANT
387 {
388 void *ctx = state;
389 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
390 $$->set_location(yylloc);
391 $$->primary_expression.uint_constant = $1;
392 }
393 | FLOATCONSTANT
394 {
395 void *ctx = state;
396 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
397 $$->set_location(yylloc);
398 $$->primary_expression.float_constant = $1;
399 }
400 | BOOLCONSTANT
401 {
402 void *ctx = state;
403 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
404 $$->set_location(yylloc);
405 $$->primary_expression.bool_constant = $1;
406 }
407 | '(' expression ')'
408 {
409 $$ = $2;
410 }
411 ;
412
413 postfix_expression:
414 primary_expression
415 | postfix_expression '[' integer_expression ']'
416 {
417 void *ctx = state;
418 $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
419 $$->set_location(yylloc);
420 }
421 | function_call
422 {
423 $$ = $1;
424 }
425 | postfix_expression '.' any_identifier
426 {
427 void *ctx = state;
428 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
429 $$->set_location(yylloc);
430 $$->primary_expression.identifier = $3;
431 }
432 | postfix_expression INC_OP
433 {
434 void *ctx = state;
435 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
436 $$->set_location(yylloc);
437 }
438 | postfix_expression DEC_OP
439 {
440 void *ctx = state;
441 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
442 $$->set_location(yylloc);
443 }
444 ;
445
446 integer_expression:
447 expression
448 ;
449
450 function_call:
451 function_call_or_method
452 ;
453
454 function_call_or_method:
455 function_call_generic
456 | postfix_expression '.' method_call_generic
457 {
458 void *ctx = state;
459 $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
460 $$->set_location(yylloc);
461 }
462 ;
463
464 function_call_generic:
465 function_call_header_with_parameters ')'
466 | function_call_header_no_parameters ')'
467 ;
468
469 function_call_header_no_parameters:
470 function_call_header VOID_TOK
471 | function_call_header
472 ;
473
474 function_call_header_with_parameters:
475 function_call_header assignment_expression
476 {
477 $$ = $1;
478 $$->set_location(yylloc);
479 $$->expressions.push_tail(& $2->link);
480 }
481 | function_call_header_with_parameters ',' assignment_expression
482 {
483 $$ = $1;
484 $$->set_location(yylloc);
485 $$->expressions.push_tail(& $3->link);
486 }
487 ;
488
489 // Grammar Note: Constructors look like functions, but lexical
490 // analysis recognized most of them as keywords. They are now
491 // recognized through "type_specifier".
492 function_call_header:
493 function_identifier '('
494 ;
495
496 function_identifier:
497 type_specifier
498 {
499 void *ctx = state;
500 $$ = new(ctx) ast_function_expression($1);
501 $$->set_location(yylloc);
502 }
503 | variable_identifier
504 {
505 void *ctx = state;
506 ast_expression *callee = new(ctx) ast_expression($1);
507 $$ = new(ctx) ast_function_expression(callee);
508 $$->set_location(yylloc);
509 }
510 | FIELD_SELECTION
511 {
512 void *ctx = state;
513 ast_expression *callee = new(ctx) ast_expression($1);
514 $$ = new(ctx) ast_function_expression(callee);
515 $$->set_location(yylloc);
516 }
517 ;
518
519 method_call_generic:
520 method_call_header_with_parameters ')'
521 | method_call_header_no_parameters ')'
522 ;
523
524 method_call_header_no_parameters:
525 method_call_header VOID_TOK
526 | method_call_header
527 ;
528
529 method_call_header_with_parameters:
530 method_call_header assignment_expression
531 {
532 $$ = $1;
533 $$->set_location(yylloc);
534 $$->expressions.push_tail(& $2->link);
535 }
536 | method_call_header_with_parameters ',' assignment_expression
537 {
538 $$ = $1;
539 $$->set_location(yylloc);
540 $$->expressions.push_tail(& $3->link);
541 }
542 ;
543
544 // Grammar Note: Constructors look like methods, but lexical
545 // analysis recognized most of them as keywords. They are now
546 // recognized through "type_specifier".
547 method_call_header:
548 variable_identifier '('
549 {
550 void *ctx = state;
551 ast_expression *callee = new(ctx) ast_expression($1);
552 $$ = new(ctx) ast_function_expression(callee);
553 $$->set_location(yylloc);
554 }
555 ;
556
557 // Grammar Note: No traditional style type casts.
558 unary_expression:
559 postfix_expression
560 | INC_OP unary_expression
561 {
562 void *ctx = state;
563 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
564 $$->set_location(yylloc);
565 }
566 | DEC_OP unary_expression
567 {
568 void *ctx = state;
569 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
570 $$->set_location(yylloc);
571 }
572 | unary_operator unary_expression
573 {
574 void *ctx = state;
575 $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
576 $$->set_location(yylloc);
577 }
578 ;
579
580 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
581 unary_operator:
582 '+' { $$ = ast_plus; }
583 | '-' { $$ = ast_neg; }
584 | '!' { $$ = ast_logic_not; }
585 | '~' { $$ = ast_bit_not; }
586 ;
587
588 multiplicative_expression:
589 unary_expression
590 | multiplicative_expression '*' unary_expression
591 {
592 void *ctx = state;
593 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
594 $$->set_location(yylloc);
595 }
596 | multiplicative_expression '/' unary_expression
597 {
598 void *ctx = state;
599 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
600 $$->set_location(yylloc);
601 }
602 | multiplicative_expression '%' unary_expression
603 {
604 void *ctx = state;
605 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
606 $$->set_location(yylloc);
607 }
608 ;
609
610 additive_expression:
611 multiplicative_expression
612 | additive_expression '+' multiplicative_expression
613 {
614 void *ctx = state;
615 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
616 $$->set_location(yylloc);
617 }
618 | additive_expression '-' multiplicative_expression
619 {
620 void *ctx = state;
621 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
622 $$->set_location(yylloc);
623 }
624 ;
625
626 shift_expression:
627 additive_expression
628 | shift_expression LEFT_OP additive_expression
629 {
630 void *ctx = state;
631 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
632 $$->set_location(yylloc);
633 }
634 | shift_expression RIGHT_OP additive_expression
635 {
636 void *ctx = state;
637 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
638 $$->set_location(yylloc);
639 }
640 ;
641
642 relational_expression:
643 shift_expression
644 | relational_expression '<' shift_expression
645 {
646 void *ctx = state;
647 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
648 $$->set_location(yylloc);
649 }
650 | relational_expression '>' shift_expression
651 {
652 void *ctx = state;
653 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
654 $$->set_location(yylloc);
655 }
656 | relational_expression LE_OP shift_expression
657 {
658 void *ctx = state;
659 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
660 $$->set_location(yylloc);
661 }
662 | relational_expression GE_OP shift_expression
663 {
664 void *ctx = state;
665 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
666 $$->set_location(yylloc);
667 }
668 ;
669
670 equality_expression:
671 relational_expression
672 | equality_expression EQ_OP relational_expression
673 {
674 void *ctx = state;
675 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
676 $$->set_location(yylloc);
677 }
678 | equality_expression NE_OP relational_expression
679 {
680 void *ctx = state;
681 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
682 $$->set_location(yylloc);
683 }
684 ;
685
686 and_expression:
687 equality_expression
688 | and_expression '&' equality_expression
689 {
690 void *ctx = state;
691 $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
692 $$->set_location(yylloc);
693 }
694 ;
695
696 exclusive_or_expression:
697 and_expression
698 | exclusive_or_expression '^' and_expression
699 {
700 void *ctx = state;
701 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
702 $$->set_location(yylloc);
703 }
704 ;
705
706 inclusive_or_expression:
707 exclusive_or_expression
708 | inclusive_or_expression '|' exclusive_or_expression
709 {
710 void *ctx = state;
711 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
712 $$->set_location(yylloc);
713 }
714 ;
715
716 logical_and_expression:
717 inclusive_or_expression
718 | logical_and_expression AND_OP inclusive_or_expression
719 {
720 void *ctx = state;
721 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
722 $$->set_location(yylloc);
723 }
724 ;
725
726 logical_xor_expression:
727 logical_and_expression
728 | logical_xor_expression XOR_OP logical_and_expression
729 {
730 void *ctx = state;
731 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
732 $$->set_location(yylloc);
733 }
734 ;
735
736 logical_or_expression:
737 logical_xor_expression
738 | logical_or_expression OR_OP logical_xor_expression
739 {
740 void *ctx = state;
741 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
742 $$->set_location(yylloc);
743 }
744 ;
745
746 conditional_expression:
747 logical_or_expression
748 | logical_or_expression '?' expression ':' assignment_expression
749 {
750 void *ctx = state;
751 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
752 $$->set_location(yylloc);
753 }
754 ;
755
756 assignment_expression:
757 conditional_expression
758 | unary_expression assignment_operator assignment_expression
759 {
760 void *ctx = state;
761 $$ = new(ctx) ast_expression($2, $1, $3, NULL);
762 $$->set_location(yylloc);
763 }
764 ;
765
766 assignment_operator:
767 '=' { $$ = ast_assign; }
768 | MUL_ASSIGN { $$ = ast_mul_assign; }
769 | DIV_ASSIGN { $$ = ast_div_assign; }
770 | MOD_ASSIGN { $$ = ast_mod_assign; }
771 | ADD_ASSIGN { $$ = ast_add_assign; }
772 | SUB_ASSIGN { $$ = ast_sub_assign; }
773 | LEFT_ASSIGN { $$ = ast_ls_assign; }
774 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
775 | AND_ASSIGN { $$ = ast_and_assign; }
776 | XOR_ASSIGN { $$ = ast_xor_assign; }
777 | OR_ASSIGN { $$ = ast_or_assign; }
778 ;
779
780 expression:
781 assignment_expression
782 {
783 $$ = $1;
784 }
785 | expression ',' assignment_expression
786 {
787 void *ctx = state;
788 if ($1->oper != ast_sequence) {
789 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
790 $$->set_location(yylloc);
791 $$->expressions.push_tail(& $1->link);
792 } else {
793 $$ = $1;
794 }
795
796 $$->expressions.push_tail(& $3->link);
797 }
798 ;
799
800 constant_expression:
801 conditional_expression
802 ;
803
804 declaration:
805 function_prototype ';'
806 {
807 state->symbols->pop_scope();
808 $$ = $1;
809 }
810 | init_declarator_list ';'
811 {
812 $$ = $1;
813 }
814 | PRECISION precision_qualifier type_specifier_no_prec ';'
815 {
816 $3->precision = $2;
817 $3->is_precision_statement = true;
818 $$ = $3;
819 }
820 | uniform_block
821 {
822 $$ = $1;
823 }
824 ;
825
826 function_prototype:
827 function_declarator ')'
828 ;
829
830 function_declarator:
831 function_header
832 | function_header_with_parameters
833 ;
834
835 function_header_with_parameters:
836 function_header parameter_declaration
837 {
838 $$ = $1;
839 $$->parameters.push_tail(& $2->link);
840 }
841 | function_header_with_parameters ',' parameter_declaration
842 {
843 $$ = $1;
844 $$->parameters.push_tail(& $3->link);
845 }
846 ;
847
848 function_header:
849 fully_specified_type variable_identifier '('
850 {
851 void *ctx = state;
852 $$ = new(ctx) ast_function();
853 $$->set_location(yylloc);
854 $$->return_type = $1;
855 $$->identifier = $2;
856
857 state->symbols->add_function(new(state) ir_function($2));
858 state->symbols->push_scope();
859 }
860 ;
861
862 parameter_declarator:
863 type_specifier any_identifier
864 {
865 void *ctx = state;
866 $$ = new(ctx) ast_parameter_declarator();
867 $$->set_location(yylloc);
868 $$->type = new(ctx) ast_fully_specified_type();
869 $$->type->set_location(yylloc);
870 $$->type->specifier = $1;
871 $$->identifier = $2;
872 }
873 | type_specifier any_identifier '[' constant_expression ']'
874 {
875 void *ctx = state;
876 $$ = new(ctx) ast_parameter_declarator();
877 $$->set_location(yylloc);
878 $$->type = new(ctx) ast_fully_specified_type();
879 $$->type->set_location(yylloc);
880 $$->type->specifier = $1;
881 $$->identifier = $2;
882 $$->is_array = true;
883 $$->array_size = $4;
884 }
885 ;
886
887 parameter_declaration:
888 parameter_type_qualifier parameter_qualifier parameter_declarator
889 {
890 $1.flags.i |= $2.flags.i;
891
892 $$ = $3;
893 $$->type->qualifier = $1;
894 }
895 | parameter_qualifier parameter_declarator
896 {
897 $$ = $2;
898 $$->type->qualifier = $1;
899 }
900 | parameter_type_qualifier parameter_qualifier parameter_type_specifier
901 {
902 void *ctx = state;
903 $1.flags.i |= $2.flags.i;
904
905 $$ = new(ctx) ast_parameter_declarator();
906 $$->set_location(yylloc);
907 $$->type = new(ctx) ast_fully_specified_type();
908 $$->type->qualifier = $1;
909 $$->type->specifier = $3;
910 }
911 | parameter_qualifier parameter_type_specifier
912 {
913 void *ctx = state;
914 $$ = new(ctx) ast_parameter_declarator();
915 $$->set_location(yylloc);
916 $$->type = new(ctx) ast_fully_specified_type();
917 $$->type->qualifier = $1;
918 $$->type->specifier = $2;
919 }
920 ;
921
922 parameter_qualifier:
923 /* empty */
924 {
925 memset(& $$, 0, sizeof($$));
926 }
927 | IN_TOK
928 {
929 memset(& $$, 0, sizeof($$));
930 $$.flags.q.in = 1;
931 }
932 | OUT_TOK
933 {
934 memset(& $$, 0, sizeof($$));
935 $$.flags.q.out = 1;
936 }
937 | INOUT_TOK
938 {
939 memset(& $$, 0, sizeof($$));
940 $$.flags.q.in = 1;
941 $$.flags.q.out = 1;
942 }
943 ;
944
945 parameter_type_specifier:
946 type_specifier
947 ;
948
949 init_declarator_list:
950 single_declaration
951 | init_declarator_list ',' any_identifier
952 {
953 void *ctx = state;
954 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
955 decl->set_location(yylloc);
956
957 $$ = $1;
958 $$->declarations.push_tail(&decl->link);
959 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
960 }
961 | init_declarator_list ',' any_identifier '[' ']'
962 {
963 void *ctx = state;
964 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
965 decl->set_location(yylloc);
966
967 $$ = $1;
968 $$->declarations.push_tail(&decl->link);
969 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
970 }
971 | init_declarator_list ',' any_identifier '[' constant_expression ']'
972 {
973 void *ctx = state;
974 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
975 decl->set_location(yylloc);
976
977 $$ = $1;
978 $$->declarations.push_tail(&decl->link);
979 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
980 }
981 | init_declarator_list ',' any_identifier '[' ']' '=' initializer
982 {
983 void *ctx = state;
984 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7);
985 decl->set_location(yylloc);
986
987 $$ = $1;
988 $$->declarations.push_tail(&decl->link);
989 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
990 }
991 | init_declarator_list ',' any_identifier '[' constant_expression ']' '=' initializer
992 {
993 void *ctx = state;
994 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
995 decl->set_location(yylloc);
996
997 $$ = $1;
998 $$->declarations.push_tail(&decl->link);
999 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1000 }
1001 | init_declarator_list ',' any_identifier '=' initializer
1002 {
1003 void *ctx = state;
1004 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
1005 decl->set_location(yylloc);
1006
1007 $$ = $1;
1008 $$->declarations.push_tail(&decl->link);
1009 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1010 }
1011 ;
1012
1013 // Grammar Note: No 'enum', or 'typedef'.
1014 single_declaration:
1015 fully_specified_type
1016 {
1017 void *ctx = state;
1018 /* Empty declaration list is valid. */
1019 $$ = new(ctx) ast_declarator_list($1);
1020 $$->set_location(yylloc);
1021 }
1022 | fully_specified_type any_identifier
1023 {
1024 void *ctx = state;
1025 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1026
1027 $$ = new(ctx) ast_declarator_list($1);
1028 $$->set_location(yylloc);
1029 $$->declarations.push_tail(&decl->link);
1030 }
1031 | fully_specified_type any_identifier '[' ']'
1032 {
1033 void *ctx = state;
1034 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
1035
1036 $$ = new(ctx) ast_declarator_list($1);
1037 $$->set_location(yylloc);
1038 $$->declarations.push_tail(&decl->link);
1039 }
1040 | fully_specified_type any_identifier '[' constant_expression ']'
1041 {
1042 void *ctx = state;
1043 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
1044
1045 $$ = new(ctx) ast_declarator_list($1);
1046 $$->set_location(yylloc);
1047 $$->declarations.push_tail(&decl->link);
1048 }
1049 | fully_specified_type any_identifier '[' ']' '=' initializer
1050 {
1051 void *ctx = state;
1052 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6);
1053
1054 $$ = new(ctx) ast_declarator_list($1);
1055 $$->set_location(yylloc);
1056 $$->declarations.push_tail(&decl->link);
1057 }
1058 | fully_specified_type any_identifier '[' constant_expression ']' '=' initializer
1059 {
1060 void *ctx = state;
1061 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
1062
1063 $$ = new(ctx) ast_declarator_list($1);
1064 $$->set_location(yylloc);
1065 $$->declarations.push_tail(&decl->link);
1066 }
1067 | fully_specified_type any_identifier '=' initializer
1068 {
1069 void *ctx = state;
1070 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1071
1072 $$ = new(ctx) ast_declarator_list($1);
1073 $$->set_location(yylloc);
1074 $$->declarations.push_tail(&decl->link);
1075 }
1076 | INVARIANT variable_identifier // Vertex only.
1077 {
1078 void *ctx = state;
1079 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1080
1081 $$ = new(ctx) ast_declarator_list(NULL);
1082 $$->set_location(yylloc);
1083 $$->invariant = true;
1084
1085 $$->declarations.push_tail(&decl->link);
1086 }
1087 ;
1088
1089 fully_specified_type:
1090 type_specifier
1091 {
1092 void *ctx = state;
1093 $$ = new(ctx) ast_fully_specified_type();
1094 $$->set_location(yylloc);
1095 $$->specifier = $1;
1096 }
1097 | type_qualifier type_specifier
1098 {
1099 void *ctx = state;
1100 $$ = new(ctx) ast_fully_specified_type();
1101 $$->set_location(yylloc);
1102 $$->qualifier = $1;
1103 $$->specifier = $2;
1104 }
1105 ;
1106
1107 layout_qualifier:
1108 LAYOUT_TOK '(' layout_qualifier_id_list ')'
1109 {
1110 $$ = $3;
1111 }
1112 ;
1113
1114 layout_qualifier_id_list:
1115 layout_qualifier_id
1116 | layout_qualifier_id_list ',' layout_qualifier_id
1117 {
1118 $$ = $1;
1119 if (!$$.merge_qualifier(& @3, state, $3)) {
1120 YYERROR;
1121 }
1122 }
1123 ;
1124
1125 layout_qualifier_id:
1126 any_identifier
1127 {
1128 memset(& $$, 0, sizeof($$));
1129
1130 /* Layout qualifiers for ARB_fragment_coord_conventions. */
1131 if (!$$.flags.i && state->ARB_fragment_coord_conventions_enable) {
1132 if (strcmp($1, "origin_upper_left") == 0) {
1133 $$.flags.q.origin_upper_left = 1;
1134 } else if (strcmp($1, "pixel_center_integer") == 0) {
1135 $$.flags.q.pixel_center_integer = 1;
1136 }
1137
1138 if ($$.flags.i && state->ARB_fragment_coord_conventions_warn) {
1139 _mesa_glsl_warning(& @1, state,
1140 "GL_ARB_fragment_coord_conventions layout "
1141 "identifier `%s' used\n", $1);
1142 }
1143 }
1144
1145 /* Layout qualifiers for AMD/ARB_conservative_depth. */
1146 if (!$$.flags.i &&
1147 (state->AMD_conservative_depth_enable ||
1148 state->ARB_conservative_depth_enable)) {
1149 if (strcmp($1, "depth_any") == 0) {
1150 $$.flags.q.depth_any = 1;
1151 } else if (strcmp($1, "depth_greater") == 0) {
1152 $$.flags.q.depth_greater = 1;
1153 } else if (strcmp($1, "depth_less") == 0) {
1154 $$.flags.q.depth_less = 1;
1155 } else if (strcmp($1, "depth_unchanged") == 0) {
1156 $$.flags.q.depth_unchanged = 1;
1157 }
1158
1159 if ($$.flags.i && state->AMD_conservative_depth_warn) {
1160 _mesa_glsl_warning(& @1, state,
1161 "GL_AMD_conservative_depth "
1162 "layout qualifier `%s' is used\n", $1);
1163 }
1164 if ($$.flags.i && state->ARB_conservative_depth_warn) {
1165 _mesa_glsl_warning(& @1, state,
1166 "GL_ARB_conservative_depth "
1167 "layout qualifier `%s' is used\n", $1);
1168 }
1169 }
1170
1171 /* See also uniform_block_layout_qualifier. */
1172 if (!$$.flags.i && state->ARB_uniform_buffer_object_enable) {
1173 if (strcmp($1, "std140") == 0) {
1174 $$.flags.q.std140 = 1;
1175 } else if (strcmp($1, "shared") == 0) {
1176 $$.flags.q.shared = 1;
1177 } else if (strcmp($1, "column_major") == 0) {
1178 $$.flags.q.column_major = 1;
1179 }
1180
1181 if ($$.flags.i && state->ARB_uniform_buffer_object_warn) {
1182 _mesa_glsl_warning(& @1, state,
1183 "#version 140 / GL_ARB_uniform_buffer_object "
1184 "layout qualifier `%s' is used\n", $1);
1185 }
1186 }
1187
1188 if (!$$.flags.i) {
1189 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1190 "`%s'\n", $1);
1191 YYERROR;
1192 }
1193 }
1194 | any_identifier '=' INTCONSTANT
1195 {
1196 memset(& $$, 0, sizeof($$));
1197
1198 if (state->ARB_explicit_attrib_location_enable) {
1199 /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1200 * FINISHME: GLSL 1.30 (or later) are supported.
1201 */
1202 if (strcmp("location", $1) == 0) {
1203 $$.flags.q.explicit_location = 1;
1204
1205 if ($3 >= 0) {
1206 $$.location = $3;
1207 } else {
1208 _mesa_glsl_error(& @3, state,
1209 "invalid location %d specified\n", $3);
1210 YYERROR;
1211 }
1212 }
1213
1214 if (strcmp("index", $1) == 0) {
1215 $$.flags.q.explicit_index = 1;
1216
1217 if ($3 >= 0) {
1218 $$.index = $3;
1219 } else {
1220 _mesa_glsl_error(& @3, state,
1221 "invalid index %d specified\n", $3);
1222 YYERROR;
1223 }
1224 }
1225 }
1226
1227 /* If the identifier didn't match any known layout identifiers,
1228 * emit an error.
1229 */
1230 if (!$$.flags.i) {
1231 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1232 "`%s'\n", $1);
1233 YYERROR;
1234 } else if (state->ARB_explicit_attrib_location_warn) {
1235 _mesa_glsl_warning(& @1, state,
1236 "GL_ARB_explicit_attrib_location layout "
1237 "identifier `%s' used\n", $1);
1238 }
1239 }
1240 | uniform_block_layout_qualifier
1241 {
1242 $$ = $1;
1243 /* Layout qualifiers for ARB_uniform_buffer_object. */
1244 if (!state->ARB_uniform_buffer_object_enable) {
1245 _mesa_glsl_error(& @1, state,
1246 "#version 140 / GL_ARB_uniform_buffer_object "
1247 "layout qualifier `%s' is used\n", $1);
1248 } else if (state->ARB_uniform_buffer_object_warn) {
1249 _mesa_glsl_warning(& @1, state,
1250 "#version 140 / GL_ARB_uniform_buffer_object "
1251 "layout qualifier `%s' is used\n", $1);
1252 }
1253 }
1254 ;
1255
1256 /* This is a separate language rule because we parse these as tokens
1257 * (due to them being reserved keywords) instead of identifiers like
1258 * most qualifiers. See the any_identifier path of
1259 * layout_qualifier_id for the others.
1260 */
1261 uniform_block_layout_qualifier:
1262 ROW_MAJOR
1263 {
1264 memset(& $$, 0, sizeof($$));
1265 $$.flags.q.row_major = 1;
1266 }
1267 | PACKED_TOK
1268 {
1269 memset(& $$, 0, sizeof($$));
1270 $$.flags.q.packed = 1;
1271 }
1272 ;
1273
1274 interpolation_qualifier:
1275 SMOOTH
1276 {
1277 memset(& $$, 0, sizeof($$));
1278 $$.flags.q.smooth = 1;
1279 }
1280 | FLAT
1281 {
1282 memset(& $$, 0, sizeof($$));
1283 $$.flags.q.flat = 1;
1284 }
1285 | NOPERSPECTIVE
1286 {
1287 memset(& $$, 0, sizeof($$));
1288 $$.flags.q.noperspective = 1;
1289 }
1290 ;
1291
1292 parameter_type_qualifier:
1293 CONST_TOK
1294 {
1295 memset(& $$, 0, sizeof($$));
1296 $$.flags.q.constant = 1;
1297 }
1298 ;
1299
1300 type_qualifier:
1301 storage_qualifier
1302 | layout_qualifier
1303 | layout_qualifier storage_qualifier
1304 {
1305 $$ = $1;
1306 $$.flags.i |= $2.flags.i;
1307 }
1308 | interpolation_qualifier
1309 | interpolation_qualifier storage_qualifier
1310 {
1311 $$ = $1;
1312 $$.flags.i |= $2.flags.i;
1313 }
1314 | INVARIANT storage_qualifier
1315 {
1316 $$ = $2;
1317 $$.flags.q.invariant = 1;
1318 }
1319 | INVARIANT interpolation_qualifier storage_qualifier
1320 {
1321 $$ = $2;
1322 $$.flags.i |= $3.flags.i;
1323 $$.flags.q.invariant = 1;
1324 }
1325 | INVARIANT
1326 {
1327 memset(& $$, 0, sizeof($$));
1328 $$.flags.q.invariant = 1;
1329 }
1330 ;
1331
1332 storage_qualifier:
1333 CONST_TOK
1334 {
1335 memset(& $$, 0, sizeof($$));
1336 $$.flags.q.constant = 1;
1337 }
1338 | ATTRIBUTE
1339 {
1340 memset(& $$, 0, sizeof($$));
1341 $$.flags.q.attribute = 1;
1342 }
1343 | VARYING
1344 {
1345 memset(& $$, 0, sizeof($$));
1346 $$.flags.q.varying = 1;
1347 }
1348 | CENTROID VARYING
1349 {
1350 memset(& $$, 0, sizeof($$));
1351 $$.flags.q.centroid = 1;
1352 $$.flags.q.varying = 1;
1353 }
1354 | IN_TOK
1355 {
1356 memset(& $$, 0, sizeof($$));
1357 $$.flags.q.in = 1;
1358 }
1359 | OUT_TOK
1360 {
1361 memset(& $$, 0, sizeof($$));
1362 $$.flags.q.out = 1;
1363 }
1364 | CENTROID IN_TOK
1365 {
1366 memset(& $$, 0, sizeof($$));
1367 $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1368 }
1369 | CENTROID OUT_TOK
1370 {
1371 memset(& $$, 0, sizeof($$));
1372 $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1373 }
1374 | UNIFORM
1375 {
1376 memset(& $$, 0, sizeof($$));
1377 $$.flags.q.uniform = 1;
1378 }
1379 ;
1380
1381 type_specifier:
1382 type_specifier_no_prec
1383 {
1384 $$ = $1;
1385 }
1386 | precision_qualifier type_specifier_no_prec
1387 {
1388 $$ = $2;
1389 $$->precision = $1;
1390 }
1391 ;
1392
1393 type_specifier_no_prec:
1394 type_specifier_nonarray
1395 | type_specifier_nonarray '[' ']'
1396 {
1397 $$ = $1;
1398 $$->is_array = true;
1399 $$->array_size = NULL;
1400 }
1401 | type_specifier_nonarray '[' constant_expression ']'
1402 {
1403 $$ = $1;
1404 $$->is_array = true;
1405 $$->array_size = $3;
1406 }
1407 ;
1408
1409 type_specifier_nonarray:
1410 basic_type_specifier_nonarray
1411 {
1412 void *ctx = state;
1413 $$ = new(ctx) ast_type_specifier($1);
1414 $$->set_location(yylloc);
1415 }
1416 | struct_specifier
1417 {
1418 void *ctx = state;
1419 $$ = new(ctx) ast_type_specifier($1);
1420 $$->set_location(yylloc);
1421 }
1422 | TYPE_IDENTIFIER
1423 {
1424 void *ctx = state;
1425 $$ = new(ctx) ast_type_specifier($1);
1426 $$->set_location(yylloc);
1427 }
1428 ;
1429
1430 basic_type_specifier_nonarray:
1431 VOID_TOK { $$ = "void"; }
1432 | FLOAT_TOK { $$ = "float"; }
1433 | INT_TOK { $$ = "int"; }
1434 | UINT_TOK { $$ = "uint"; }
1435 | BOOL_TOK { $$ = "bool"; }
1436 | VEC2 { $$ = "vec2"; }
1437 | VEC3 { $$ = "vec3"; }
1438 | VEC4 { $$ = "vec4"; }
1439 | BVEC2 { $$ = "bvec2"; }
1440 | BVEC3 { $$ = "bvec3"; }
1441 | BVEC4 { $$ = "bvec4"; }
1442 | IVEC2 { $$ = "ivec2"; }
1443 | IVEC3 { $$ = "ivec3"; }
1444 | IVEC4 { $$ = "ivec4"; }
1445 | UVEC2 { $$ = "uvec2"; }
1446 | UVEC3 { $$ = "uvec3"; }
1447 | UVEC4 { $$ = "uvec4"; }
1448 | MAT2X2 { $$ = "mat2"; }
1449 | MAT2X3 { $$ = "mat2x3"; }
1450 | MAT2X4 { $$ = "mat2x4"; }
1451 | MAT3X2 { $$ = "mat3x2"; }
1452 | MAT3X3 { $$ = "mat3"; }
1453 | MAT3X4 { $$ = "mat3x4"; }
1454 | MAT4X2 { $$ = "mat4x2"; }
1455 | MAT4X3 { $$ = "mat4x3"; }
1456 | MAT4X4 { $$ = "mat4"; }
1457 | SAMPLER1D { $$ = "sampler1D"; }
1458 | SAMPLER2D { $$ = "sampler2D"; }
1459 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
1460 | SAMPLER3D { $$ = "sampler3D"; }
1461 | SAMPLERCUBE { $$ = "samplerCube"; }
1462 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
1463 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
1464 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
1465 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
1466 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
1467 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
1468 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
1469 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
1470 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
1471 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
1472 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
1473 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
1474 | ISAMPLER1D { $$ = "isampler1D"; }
1475 | ISAMPLER2D { $$ = "isampler2D"; }
1476 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
1477 | ISAMPLER3D { $$ = "isampler3D"; }
1478 | ISAMPLERCUBE { $$ = "isamplerCube"; }
1479 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
1480 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
1481 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
1482 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
1483 | USAMPLER1D { $$ = "usampler1D"; }
1484 | USAMPLER2D { $$ = "usampler2D"; }
1485 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
1486 | USAMPLER3D { $$ = "usampler3D"; }
1487 | USAMPLERCUBE { $$ = "usamplerCube"; }
1488 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
1489 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
1490 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
1491 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
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 | layout_defaults { $$ = NULL; }
1916 ;
1917
1918 function_definition:
1919 function_prototype compound_statement_no_new_scope
1920 {
1921 void *ctx = state;
1922 $$ = new(ctx) ast_function_definition();
1923 $$->set_location(yylloc);
1924 $$->prototype = $1;
1925 $$->body = $2;
1926
1927 state->symbols->pop_scope();
1928 }
1929 ;
1930
1931 /* layout_qualifieropt is packed into this rule */
1932 uniform_block:
1933 UNIFORM NEW_IDENTIFIER '{' member_list '}' ';'
1934 {
1935 void *ctx = state;
1936 $$ = new(ctx) ast_uniform_block(*state->default_uniform_qualifier,
1937 $2, $4);
1938
1939 if (!state->ARB_uniform_buffer_object_enable) {
1940 _mesa_glsl_error(& @1, state,
1941 "#version 140 / GL_ARB_uniform_buffer_object "
1942 "required for defining uniform blocks\n");
1943 } else if (state->ARB_uniform_buffer_object_warn) {
1944 _mesa_glsl_warning(& @1, state,
1945 "#version 140 / GL_ARB_uniform_buffer_object "
1946 "required for defining uniform blocks\n");
1947 }
1948 }
1949 | layout_qualifier UNIFORM NEW_IDENTIFIER '{' member_list '}' ';'
1950 {
1951 void *ctx = state;
1952
1953 ast_type_qualifier qual = *state->default_uniform_qualifier;
1954 if (!qual.merge_qualifier(& @1, state, $1)) {
1955 YYERROR;
1956 }
1957 $$ = new(ctx) ast_uniform_block(qual, $3, $5);
1958
1959 if (!state->ARB_uniform_buffer_object_enable) {
1960 _mesa_glsl_error(& @1, state,
1961 "#version 140 / GL_ARB_uniform_buffer_object "
1962 "required for defining uniform blocks\n");
1963 } else if (state->ARB_uniform_buffer_object_warn) {
1964 _mesa_glsl_warning(& @1, state,
1965 "#version 140 / GL_ARB_uniform_buffer_object "
1966 "required for defining uniform blocks\n");
1967 }
1968 }
1969 ;
1970
1971 member_list:
1972 member_declaration
1973 {
1974 $$ = $1;
1975 $1->link.self_link();
1976 }
1977 | member_declaration member_list
1978 {
1979 $$ = $1;
1980 $2->link.insert_before(& $$->link);
1981 }
1982 ;
1983
1984 /* Specifying "uniform" inside of a uniform block is redundant. */
1985 uniformopt:
1986 /* nothing */
1987 | UNIFORM
1988 ;
1989
1990 member_declaration:
1991 layout_qualifier uniformopt type_specifier struct_declarator_list ';'
1992 {
1993 void *ctx = state;
1994 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1995 type->set_location(yylloc);
1996
1997 type->qualifier = $1;
1998 type->qualifier.flags.q.uniform = true;
1999 type->specifier = $3;
2000 $$ = new(ctx) ast_declarator_list(type);
2001 $$->set_location(yylloc);
2002 $$->ubo_qualifiers_valid = true;
2003
2004 $$->declarations.push_degenerate_list_at_head(& $4->link);
2005 }
2006 | uniformopt type_specifier struct_declarator_list ';'
2007 {
2008 void *ctx = state;
2009 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
2010 type->set_location(yylloc);
2011
2012 type->qualifier.flags.q.uniform = true;
2013 type->specifier = $2;
2014 $$ = new(ctx) ast_declarator_list(type);
2015 $$->set_location(yylloc);
2016 $$->ubo_qualifiers_valid = true;
2017
2018 $$->declarations.push_degenerate_list_at_head(& $3->link);
2019 }
2020 ;
2021
2022 layout_defaults:
2023 layout_qualifier UNIFORM ';'
2024 {
2025 if (!state->default_uniform_qualifier->merge_qualifier(& @1, state,
2026 $1)) {
2027 YYERROR;
2028 }
2029 }