3 * Copyright © 2008, 2009 Intel Corporation
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:
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
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.
30 #include "glsl_parser_extras.h"
31 #include "glsl_types.h"
32 #include "main/context.h"
36 static void yyerror(YYLTYPE *loc, _mesa_glsl_parse_state *st, const char *msg)
38 _mesa_glsl_error(loc, st, "%s", msg);
42 _mesa_glsl_lex(YYSTYPE *val, YYLTYPE *loc, _mesa_glsl_parse_state *state)
44 return _mesa_glsl_lexer_lex(val, loc, state->scanner);
47 static bool match_layout_qualifier(const char *s1, const char *s2,
48 _mesa_glsl_parse_state *state)
50 /* From the GLSL 1.50 spec, section 4.3.8 (Layout Qualifiers):
52 * "The tokens in any layout-qualifier-id-list ... are not case
53 * sensitive, unless explicitly noted otherwise."
55 * The text "unless explicitly noted otherwise" appears to be
56 * vacuous--no desktop GLSL spec (up through GLSL 4.40) notes
59 * However, the GLSL ES 3.00 spec says, in section 4.3.8 (Layout
62 * "As for other identifiers, they are case sensitive."
64 * So we need to do a case-sensitive or a case-insensitive match,
65 * depending on whether we are compiling for GLSL ES.
68 return strcmp(s1, s2);
70 return strcasecmp(s1, s2);
88 %lex-param {struct _mesa_glsl_parse_state *state}
89 %parse-param {struct _mesa_glsl_parse_state *state}
94 const char *identifier;
96 struct ast_type_qualifier type_qualifier;
99 ast_type_specifier *type_specifier;
100 ast_array_specifier *array_specifier;
101 ast_fully_specified_type *fully_specified_type;
102 ast_function *function;
103 ast_parameter_declarator *parameter_declarator;
104 ast_function_definition *function_definition;
105 ast_compound_statement *compound_statement;
106 ast_expression *expression;
107 ast_declarator_list *declarator_list;
108 ast_struct_specifier *struct_specifier;
109 ast_declaration *declaration;
110 ast_switch_body *switch_body;
111 ast_case_label *case_label;
112 ast_case_label_list *case_label_list;
113 ast_case_statement *case_statement;
114 ast_case_statement_list *case_statement_list;
115 ast_interface_block *interface_block;
119 ast_expression *rest;
120 } for_rest_statement;
123 ast_node *then_statement;
124 ast_node *else_statement;
125 } selection_rest_statement;
128 %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK
129 %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
130 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
131 %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING
132 %token NOPERSPECTIVE FLAT SMOOTH
133 %token MAT2X2 MAT2X3 MAT2X4
134 %token MAT3X2 MAT3X3 MAT3X4
135 %token MAT4X2 MAT4X3 MAT4X4
136 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
137 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
138 %token SAMPLER2DARRAYSHADOW SAMPLERCUBEARRAY SAMPLERCUBEARRAYSHADOW
139 %token ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
140 %token ISAMPLER1DARRAY ISAMPLER2DARRAY ISAMPLERCUBEARRAY
141 %token USAMPLER1D USAMPLER2D USAMPLER3D USAMPLERCUBE USAMPLER1DARRAY
142 %token USAMPLER2DARRAY USAMPLERCUBEARRAY
143 %token SAMPLER2DRECT ISAMPLER2DRECT USAMPLER2DRECT SAMPLER2DRECTSHADOW
144 %token SAMPLERBUFFER ISAMPLERBUFFER USAMPLERBUFFER
145 %token SAMPLER2DMS ISAMPLER2DMS USAMPLER2DMS
146 %token SAMPLER2DMSARRAY ISAMPLER2DMSARRAY USAMPLER2DMSARRAY
147 %token SAMPLEREXTERNALOES
148 %token IMAGE1D IMAGE2D IMAGE3D IMAGE2DRECT IMAGECUBE IMAGEBUFFER
149 %token IMAGE1DARRAY IMAGE2DARRAY IMAGECUBEARRAY IMAGE2DMS IMAGE2DMSARRAY
150 %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGE2DRECT IIMAGECUBE IIMAGEBUFFER
151 %token IIMAGE1DARRAY IIMAGE2DARRAY IIMAGECUBEARRAY IIMAGE2DMS IIMAGE2DMSARRAY
152 %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGE2DRECT UIMAGECUBE UIMAGEBUFFER
153 %token UIMAGE1DARRAY UIMAGE2DARRAY UIMAGECUBEARRAY UIMAGE2DMS UIMAGE2DMSARRAY
154 %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW
155 %token COHERENT VOLATILE RESTRICT READONLY WRITEONLY
157 %token STRUCT VOID_TOK WHILE
158 %token <identifier> IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
159 %type <identifier> any_identifier
160 %type <interface_block> instance_name_opt
161 %token <real> FLOATCONSTANT
162 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
163 %token <identifier> FIELD_SELECTION
164 %token LEFT_OP RIGHT_OP
165 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
166 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
167 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
170 %token LOWP MEDIUMP HIGHP SUPERP PRECISION
172 %token VERSION_TOK EXTENSION LINE COLON EOL INTERFACE OUTPUT
173 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
174 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
175 %token PRAGMA_INVARIANT_ALL
178 /* Reserved words that are not actually used in the grammar.
180 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
181 %token INLINE_TOK NOINLINE PUBLIC_TOK STATIC EXTERN EXTERNAL
182 %token LONG_TOK SHORT_TOK DOUBLE_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK OUPTUT
183 %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
185 %token SIZEOF CAST NAMESPACE USING
186 %token RESOURCE PATCH SAMPLE
191 %token COMMON PARTITION ACTIVE FILTER ROW_MAJOR
193 %type <identifier> variable_identifier
194 %type <node> statement
195 %type <node> statement_list
196 %type <node> simple_statement
197 %type <n> precision_qualifier
198 %type <type_qualifier> type_qualifier
199 %type <type_qualifier> auxiliary_storage_qualifier
200 %type <type_qualifier> storage_qualifier
201 %type <type_qualifier> interpolation_qualifier
202 %type <type_qualifier> layout_qualifier
203 %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
204 %type <type_qualifier> interface_block_layout_qualifier
205 %type <type_qualifier> interface_qualifier
206 %type <type_specifier> type_specifier
207 %type <type_specifier> type_specifier_nonarray
208 %type <array_specifier> array_specifier
209 %type <identifier> basic_type_specifier_nonarray
210 %type <fully_specified_type> fully_specified_type
211 %type <function> function_prototype
212 %type <function> function_header
213 %type <function> function_header_with_parameters
214 %type <function> function_declarator
215 %type <parameter_declarator> parameter_declarator
216 %type <parameter_declarator> parameter_declaration
217 %type <type_qualifier> parameter_qualifier
218 %type <type_qualifier> parameter_direction_qualifier
219 %type <type_specifier> parameter_type_specifier
220 %type <function_definition> function_definition
221 %type <compound_statement> compound_statement_no_new_scope
222 %type <compound_statement> compound_statement
223 %type <node> statement_no_new_scope
224 %type <node> expression_statement
225 %type <expression> expression
226 %type <expression> primary_expression
227 %type <expression> assignment_expression
228 %type <expression> conditional_expression
229 %type <expression> logical_or_expression
230 %type <expression> logical_xor_expression
231 %type <expression> logical_and_expression
232 %type <expression> inclusive_or_expression
233 %type <expression> exclusive_or_expression
234 %type <expression> and_expression
235 %type <expression> equality_expression
236 %type <expression> relational_expression
237 %type <expression> shift_expression
238 %type <expression> additive_expression
239 %type <expression> multiplicative_expression
240 %type <expression> unary_expression
241 %type <expression> constant_expression
242 %type <expression> integer_expression
243 %type <expression> postfix_expression
244 %type <expression> function_call_header_with_parameters
245 %type <expression> function_call_header_no_parameters
246 %type <expression> function_call_header
247 %type <expression> function_call_generic
248 %type <expression> function_call_or_method
249 %type <expression> function_call
250 %type <expression> method_call_generic
251 %type <expression> method_call_header_with_parameters
252 %type <expression> method_call_header_no_parameters
253 %type <expression> method_call_header
254 %type <n> assignment_operator
255 %type <n> unary_operator
256 %type <expression> function_identifier
257 %type <node> external_declaration
258 %type <declarator_list> init_declarator_list
259 %type <declarator_list> single_declaration
260 %type <expression> initializer
261 %type <expression> initializer_list
262 %type <node> declaration
263 %type <node> declaration_statement
264 %type <node> jump_statement
265 %type <node> interface_block
266 %type <interface_block> basic_interface_block
267 %type <struct_specifier> struct_specifier
268 %type <declarator_list> struct_declaration_list
269 %type <declarator_list> struct_declaration
270 %type <declaration> struct_declarator
271 %type <declaration> struct_declarator_list
272 %type <declarator_list> member_list
273 %type <declarator_list> member_declaration
274 %type <node> selection_statement
275 %type <selection_rest_statement> selection_rest_statement
276 %type <node> switch_statement
277 %type <switch_body> switch_body
278 %type <case_label_list> case_label_list
279 %type <case_label> case_label
280 %type <case_statement> case_statement
281 %type <case_statement_list> case_statement_list
282 %type <node> iteration_statement
283 %type <node> condition
284 %type <node> conditionopt
285 %type <node> for_init_statement
286 %type <for_rest_statement> for_rest_statement
287 %type <n> integer_constant
288 %type <node> layout_defaults
294 version_statement extension_statement_list
296 _mesa_glsl_initialize_types(state);
298 external_declaration_list
300 delete state->symbols;
301 state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
302 _mesa_glsl_initialize_types(state);
307 /* blank - no #version specified: defaults are already set */
308 | VERSION_TOK INTCONSTANT EOL
310 state->process_version_directive(&@2, $2, NULL);
315 | VERSION_TOK INTCONSTANT any_identifier EOL
317 state->process_version_directive(&@2, $2, $3);
326 | PRAGMA_DEBUG_OFF EOL
327 | PRAGMA_OPTIMIZE_ON EOL
328 | PRAGMA_OPTIMIZE_OFF EOL
329 | PRAGMA_INVARIANT_ALL EOL
331 if (!state->is_version(120, 100)) {
332 _mesa_glsl_warning(& @1, state,
333 "pragma `invariant(all)' not supported in %s "
334 "(GLSL ES 1.00 or GLSL 1.20 required)",
335 state->get_version_string());
337 state->all_invariant = true;
342 extension_statement_list:
344 | extension_statement_list extension_statement
354 EXTENSION any_identifier COLON any_identifier EOL
356 if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
362 external_declaration_list:
365 /* FINISHME: The NULL test is required because pragmas are set to
366 * FINISHME: NULL. (See production rule for external_declaration.)
369 state->translation_unit.push_tail(& $1->link);
371 | external_declaration_list external_declaration
373 /* FINISHME: The NULL test is required because pragmas are set to
374 * FINISHME: NULL. (See production rule for external_declaration.)
377 state->translation_unit.push_tail(& $2->link);
390 $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
391 $$->set_location(yylloc);
392 $$->primary_expression.identifier = $1;
397 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
398 $$->set_location(yylloc);
399 $$->primary_expression.int_constant = $1;
404 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
405 $$->set_location(yylloc);
406 $$->primary_expression.uint_constant = $1;
411 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
412 $$->set_location(yylloc);
413 $$->primary_expression.float_constant = $1;
418 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
419 $$->set_location(yylloc);
420 $$->primary_expression.bool_constant = $1;
430 | postfix_expression '[' integer_expression ']'
433 $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
434 $$->set_location(yylloc);
440 | postfix_expression '.' any_identifier
443 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
444 $$->set_location(yylloc);
445 $$->primary_expression.identifier = $3;
447 | postfix_expression INC_OP
450 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
451 $$->set_location(yylloc);
453 | postfix_expression DEC_OP
456 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
457 $$->set_location(yylloc);
466 function_call_or_method
469 function_call_or_method:
470 function_call_generic
471 | postfix_expression '.' method_call_generic
474 $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
475 $$->set_location(yylloc);
479 function_call_generic:
480 function_call_header_with_parameters ')'
481 | function_call_header_no_parameters ')'
484 function_call_header_no_parameters:
485 function_call_header VOID_TOK
486 | function_call_header
489 function_call_header_with_parameters:
490 function_call_header assignment_expression
493 $$->set_location(yylloc);
494 $$->expressions.push_tail(& $2->link);
496 | function_call_header_with_parameters ',' assignment_expression
499 $$->set_location(yylloc);
500 $$->expressions.push_tail(& $3->link);
504 // Grammar Note: Constructors look like functions, but lexical
505 // analysis recognized most of them as keywords. They are now
506 // recognized through "type_specifier".
507 function_call_header:
508 function_identifier '('
515 $$ = new(ctx) ast_function_expression($1);
516 $$->set_location(yylloc);
518 | variable_identifier
521 ast_expression *callee = new(ctx) ast_expression($1);
522 $$ = new(ctx) ast_function_expression(callee);
523 $$->set_location(yylloc);
528 ast_expression *callee = new(ctx) ast_expression($1);
529 $$ = new(ctx) ast_function_expression(callee);
530 $$->set_location(yylloc);
535 method_call_header_with_parameters ')'
536 | method_call_header_no_parameters ')'
539 method_call_header_no_parameters:
540 method_call_header VOID_TOK
544 method_call_header_with_parameters:
545 method_call_header assignment_expression
548 $$->set_location(yylloc);
549 $$->expressions.push_tail(& $2->link);
551 | method_call_header_with_parameters ',' assignment_expression
554 $$->set_location(yylloc);
555 $$->expressions.push_tail(& $3->link);
559 // Grammar Note: Constructors look like methods, but lexical
560 // analysis recognized most of them as keywords. They are now
561 // recognized through "type_specifier".
563 variable_identifier '('
566 ast_expression *callee = new(ctx) ast_expression($1);
567 $$ = new(ctx) ast_function_expression(callee);
568 $$->set_location(yylloc);
572 // Grammar Note: No traditional style type casts.
575 | INC_OP unary_expression
578 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
579 $$->set_location(yylloc);
581 | DEC_OP unary_expression
584 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
585 $$->set_location(yylloc);
587 | unary_operator unary_expression
590 $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
591 $$->set_location(yylloc);
595 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
597 '+' { $$ = ast_plus; }
598 | '-' { $$ = ast_neg; }
599 | '!' { $$ = ast_logic_not; }
600 | '~' { $$ = ast_bit_not; }
603 multiplicative_expression:
605 | multiplicative_expression '*' unary_expression
608 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
609 $$->set_location(yylloc);
611 | multiplicative_expression '/' unary_expression
614 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
615 $$->set_location(yylloc);
617 | multiplicative_expression '%' unary_expression
620 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
621 $$->set_location(yylloc);
626 multiplicative_expression
627 | additive_expression '+' multiplicative_expression
630 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
631 $$->set_location(yylloc);
633 | additive_expression '-' multiplicative_expression
636 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
637 $$->set_location(yylloc);
643 | shift_expression LEFT_OP additive_expression
646 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
647 $$->set_location(yylloc);
649 | shift_expression RIGHT_OP additive_expression
652 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
653 $$->set_location(yylloc);
657 relational_expression:
659 | relational_expression '<' shift_expression
662 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
663 $$->set_location(yylloc);
665 | relational_expression '>' shift_expression
668 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
669 $$->set_location(yylloc);
671 | relational_expression LE_OP shift_expression
674 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
675 $$->set_location(yylloc);
677 | relational_expression GE_OP shift_expression
680 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
681 $$->set_location(yylloc);
686 relational_expression
687 | equality_expression EQ_OP relational_expression
690 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
691 $$->set_location(yylloc);
693 | equality_expression NE_OP relational_expression
696 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
697 $$->set_location(yylloc);
703 | and_expression '&' equality_expression
706 $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
707 $$->set_location(yylloc);
711 exclusive_or_expression:
713 | exclusive_or_expression '^' and_expression
716 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
717 $$->set_location(yylloc);
721 inclusive_or_expression:
722 exclusive_or_expression
723 | inclusive_or_expression '|' exclusive_or_expression
726 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
727 $$->set_location(yylloc);
731 logical_and_expression:
732 inclusive_or_expression
733 | logical_and_expression AND_OP inclusive_or_expression
736 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
737 $$->set_location(yylloc);
741 logical_xor_expression:
742 logical_and_expression
743 | logical_xor_expression XOR_OP logical_and_expression
746 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
747 $$->set_location(yylloc);
751 logical_or_expression:
752 logical_xor_expression
753 | logical_or_expression OR_OP logical_xor_expression
756 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
757 $$->set_location(yylloc);
761 conditional_expression:
762 logical_or_expression
763 | logical_or_expression '?' expression ':' assignment_expression
766 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
767 $$->set_location(yylloc);
771 assignment_expression:
772 conditional_expression
773 | unary_expression assignment_operator assignment_expression
776 $$ = new(ctx) ast_expression($2, $1, $3, NULL);
777 $$->set_location(yylloc);
782 '=' { $$ = ast_assign; }
783 | MUL_ASSIGN { $$ = ast_mul_assign; }
784 | DIV_ASSIGN { $$ = ast_div_assign; }
785 | MOD_ASSIGN { $$ = ast_mod_assign; }
786 | ADD_ASSIGN { $$ = ast_add_assign; }
787 | SUB_ASSIGN { $$ = ast_sub_assign; }
788 | LEFT_ASSIGN { $$ = ast_ls_assign; }
789 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
790 | AND_ASSIGN { $$ = ast_and_assign; }
791 | XOR_ASSIGN { $$ = ast_xor_assign; }
792 | OR_ASSIGN { $$ = ast_or_assign; }
796 assignment_expression
800 | expression ',' assignment_expression
803 if ($1->oper != ast_sequence) {
804 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
805 $$->set_location(yylloc);
806 $$->expressions.push_tail(& $1->link);
811 $$->expressions.push_tail(& $3->link);
816 conditional_expression
820 function_prototype ';'
822 state->symbols->pop_scope();
825 | init_declarator_list ';'
829 | PRECISION precision_qualifier type_specifier ';'
831 $3->default_precision = $2;
841 function_declarator ')'
846 | function_header_with_parameters
849 function_header_with_parameters:
850 function_header parameter_declaration
853 $$->parameters.push_tail(& $2->link);
855 | function_header_with_parameters ',' parameter_declaration
858 $$->parameters.push_tail(& $3->link);
863 fully_specified_type variable_identifier '('
866 $$ = new(ctx) ast_function();
867 $$->set_location(yylloc);
868 $$->return_type = $1;
871 state->symbols->add_function(new(state) ir_function($2));
872 state->symbols->push_scope();
876 parameter_declarator:
877 type_specifier any_identifier
880 $$ = new(ctx) ast_parameter_declarator();
881 $$->set_location(yylloc);
882 $$->type = new(ctx) ast_fully_specified_type();
883 $$->type->set_location(yylloc);
884 $$->type->specifier = $1;
887 | type_specifier any_identifier array_specifier
890 $$ = new(ctx) ast_parameter_declarator();
891 $$->set_location(yylloc);
892 $$->type = new(ctx) ast_fully_specified_type();
893 $$->type->set_location(yylloc);
894 $$->type->specifier = $1;
896 $$->array_specifier = $3;
900 parameter_declaration:
901 parameter_qualifier parameter_declarator
904 $$->type->qualifier = $1;
906 | parameter_qualifier parameter_type_specifier
909 $$ = new(ctx) ast_parameter_declarator();
910 $$->set_location(yylloc);
911 $$->type = new(ctx) ast_fully_specified_type();
912 $$->type->qualifier = $1;
913 $$->type->specifier = $2;
920 memset(& $$, 0, sizeof($$));
922 | CONST_TOK parameter_qualifier
924 if ($2.flags.q.constant)
925 _mesa_glsl_error(&@1, state, "duplicate const qualifier");
928 $$.flags.q.constant = 1;
930 | parameter_direction_qualifier parameter_qualifier
932 if (($1.flags.q.in || $1.flags.q.out) && ($2.flags.q.in || $2.flags.q.out))
933 _mesa_glsl_error(&@1, state, "duplicate in/out/inout qualifier");
935 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.constant)
936 _mesa_glsl_error(&@1, state, "const must be specified before "
940 $$.merge_qualifier(&@1, state, $2);
942 | precision_qualifier parameter_qualifier
944 if ($2.precision != ast_precision_none)
945 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
947 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
948 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
954 parameter_direction_qualifier:
957 memset(& $$, 0, sizeof($$));
962 memset(& $$, 0, sizeof($$));
967 memset(& $$, 0, sizeof($$));
973 parameter_type_specifier:
977 init_declarator_list:
979 | init_declarator_list ',' any_identifier
982 ast_declaration *decl = new(ctx) ast_declaration($3, NULL, NULL);
983 decl->set_location(yylloc);
986 $$->declarations.push_tail(&decl->link);
987 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
989 | init_declarator_list ',' any_identifier array_specifier
992 ast_declaration *decl = new(ctx) ast_declaration($3, $4, NULL);
993 decl->set_location(yylloc);
996 $$->declarations.push_tail(&decl->link);
997 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
999 | init_declarator_list ',' any_identifier array_specifier '=' initializer
1002 ast_declaration *decl = new(ctx) ast_declaration($3, $4, $6);
1003 decl->set_location(yylloc);
1006 $$->declarations.push_tail(&decl->link);
1007 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1009 | init_declarator_list ',' any_identifier '=' initializer
1012 ast_declaration *decl = new(ctx) ast_declaration($3, NULL, $5);
1013 decl->set_location(yylloc);
1016 $$->declarations.push_tail(&decl->link);
1017 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1021 // Grammar Note: No 'enum', or 'typedef'.
1023 fully_specified_type
1026 /* Empty declaration list is valid. */
1027 $$ = new(ctx) ast_declarator_list($1);
1028 $$->set_location(yylloc);
1030 | fully_specified_type any_identifier
1033 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
1035 $$ = new(ctx) ast_declarator_list($1);
1036 $$->set_location(yylloc);
1037 $$->declarations.push_tail(&decl->link);
1039 | fully_specified_type any_identifier array_specifier
1042 ast_declaration *decl = new(ctx) ast_declaration($2, $3, NULL);
1044 $$ = new(ctx) ast_declarator_list($1);
1045 $$->set_location(yylloc);
1046 $$->declarations.push_tail(&decl->link);
1048 | fully_specified_type any_identifier array_specifier '=' initializer
1051 ast_declaration *decl = new(ctx) ast_declaration($2, $3, $5);
1053 $$ = new(ctx) ast_declarator_list($1);
1054 $$->set_location(yylloc);
1055 $$->declarations.push_tail(&decl->link);
1057 | fully_specified_type any_identifier '=' initializer
1060 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
1062 $$ = new(ctx) ast_declarator_list($1);
1063 $$->set_location(yylloc);
1064 $$->declarations.push_tail(&decl->link);
1066 | INVARIANT variable_identifier // Vertex only.
1069 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
1071 $$ = new(ctx) ast_declarator_list(NULL);
1072 $$->set_location(yylloc);
1073 $$->invariant = true;
1075 $$->declarations.push_tail(&decl->link);
1079 fully_specified_type:
1083 $$ = new(ctx) ast_fully_specified_type();
1084 $$->set_location(yylloc);
1087 | type_qualifier type_specifier
1090 $$ = new(ctx) ast_fully_specified_type();
1091 $$->set_location(yylloc);
1098 LAYOUT_TOK '(' layout_qualifier_id_list ')'
1104 layout_qualifier_id_list:
1106 | layout_qualifier_id_list ',' layout_qualifier_id
1109 if (!$$.merge_qualifier(& @3, state, $3)) {
1116 INTCONSTANT { $$ = $1; }
1117 | UINTCONSTANT { $$ = $1; }
1120 layout_qualifier_id:
1123 memset(& $$, 0, sizeof($$));
1125 /* Layout qualifiers for ARB_fragment_coord_conventions. */
1126 if (!$$.flags.i && (state->ARB_fragment_coord_conventions_enable ||
1127 state->is_version(150, 0))) {
1128 if (match_layout_qualifier($1, "origin_upper_left", state) == 0) {
1129 $$.flags.q.origin_upper_left = 1;
1130 } else if (match_layout_qualifier($1, "pixel_center_integer",
1132 $$.flags.q.pixel_center_integer = 1;
1135 if ($$.flags.i && state->ARB_fragment_coord_conventions_warn) {
1136 _mesa_glsl_warning(& @1, state,
1137 "GL_ARB_fragment_coord_conventions layout "
1138 "identifier `%s' used", $1);
1142 /* Layout qualifiers for AMD/ARB_conservative_depth. */
1144 (state->AMD_conservative_depth_enable ||
1145 state->ARB_conservative_depth_enable)) {
1146 if (match_layout_qualifier($1, "depth_any", state) == 0) {
1147 $$.flags.q.depth_any = 1;
1148 } else if (match_layout_qualifier($1, "depth_greater", state) == 0) {
1149 $$.flags.q.depth_greater = 1;
1150 } else if (match_layout_qualifier($1, "depth_less", state) == 0) {
1151 $$.flags.q.depth_less = 1;
1152 } else if (match_layout_qualifier($1, "depth_unchanged",
1154 $$.flags.q.depth_unchanged = 1;
1157 if ($$.flags.i && state->AMD_conservative_depth_warn) {
1158 _mesa_glsl_warning(& @1, state,
1159 "GL_AMD_conservative_depth "
1160 "layout qualifier `%s' is used", $1);
1162 if ($$.flags.i && state->ARB_conservative_depth_warn) {
1163 _mesa_glsl_warning(& @1, state,
1164 "GL_ARB_conservative_depth "
1165 "layout qualifier `%s' is used", $1);
1169 /* See also interface_block_layout_qualifier. */
1170 if (!$$.flags.i && state->has_uniform_buffer_objects()) {
1171 if (match_layout_qualifier($1, "std140", state) == 0) {
1172 $$.flags.q.std140 = 1;
1173 } else if (match_layout_qualifier($1, "shared", state) == 0) {
1174 $$.flags.q.shared = 1;
1175 } else if (match_layout_qualifier($1, "column_major", state) == 0) {
1176 $$.flags.q.column_major = 1;
1177 /* "row_major" is a reserved word in GLSL 1.30+. Its token is parsed
1178 * below in the interface_block_layout_qualifier rule.
1180 * It is not a reserved word in GLSL ES 3.00, so it's handled here as
1183 * Also, this takes care of alternate capitalizations of
1184 * "row_major" (which is necessary because layout qualifiers
1185 * are case-insensitive in desktop GLSL).
1187 } else if (match_layout_qualifier($1, "row_major", state) == 0) {
1188 $$.flags.q.row_major = 1;
1189 /* "packed" is a reserved word in GLSL, and its token is
1190 * parsed below in the interface_block_layout_qualifier rule.
1191 * However, we must take care of alternate capitalizations of
1192 * "packed", because layout qualifiers are case-insensitive
1195 } else if (match_layout_qualifier($1, "packed", state) == 0) {
1196 $$.flags.q.packed = 1;
1199 if ($$.flags.i && state->ARB_uniform_buffer_object_warn) {
1200 _mesa_glsl_warning(& @1, state,
1201 "#version 140 / GL_ARB_uniform_buffer_object "
1202 "layout qualifier `%s' is used", $1);
1206 /* Layout qualifiers for GLSL 1.50 geometry shaders. */
1212 { "points", GL_POINTS },
1213 { "lines", GL_LINES },
1214 { "lines_adjacency", GL_LINES_ADJACENCY },
1215 { "line_strip", GL_LINE_STRIP },
1216 { "triangles", GL_TRIANGLES },
1217 { "triangles_adjacency", GL_TRIANGLES_ADJACENCY },
1218 { "triangle_strip", GL_TRIANGLE_STRIP },
1220 for (unsigned i = 0; i < Elements(map); i++) {
1221 if (match_layout_qualifier($1, map[i].s, state) == 0) {
1222 $$.flags.q.prim_type = 1;
1223 $$.prim_type = map[i].e;
1228 if ($$.flags.i && !state->is_version(150, 0)) {
1229 _mesa_glsl_error(& @1, state, "#version 150 layout "
1230 "qualifier `%s' used", $1);
1234 /* Layout qualifiers for ARB_shader_image_load_store. */
1235 if (state->ARB_shader_image_load_store_enable ||
1236 state->is_version(420, 0)) {
1238 static const struct {
1241 glsl_base_type base_type;
1243 { "rgba32f", GL_RGBA32F, GLSL_TYPE_FLOAT },
1244 { "rgba16f", GL_RGBA16F, GLSL_TYPE_FLOAT },
1245 { "rg32f", GL_RG32F, GLSL_TYPE_FLOAT },
1246 { "rg16f", GL_RG16F, GLSL_TYPE_FLOAT },
1247 { "r11f_g11f_b10f", GL_R11F_G11F_B10F, GLSL_TYPE_FLOAT },
1248 { "r32f", GL_R32F, GLSL_TYPE_FLOAT },
1249 { "r16f", GL_R16F, GLSL_TYPE_FLOAT },
1250 { "rgba32ui", GL_RGBA32UI, GLSL_TYPE_UINT },
1251 { "rgba16ui", GL_RGBA16UI, GLSL_TYPE_UINT },
1252 { "rgb10_a2ui", GL_RGB10_A2UI, GLSL_TYPE_UINT },
1253 { "rgba8ui", GL_RGBA8UI, GLSL_TYPE_UINT },
1254 { "rg32ui", GL_RG32UI, GLSL_TYPE_UINT },
1255 { "rg16ui", GL_RG16UI, GLSL_TYPE_UINT },
1256 { "rg8ui", GL_RG8UI, GLSL_TYPE_UINT },
1257 { "r32ui", GL_R32UI, GLSL_TYPE_UINT },
1258 { "r16ui", GL_R16UI, GLSL_TYPE_UINT },
1259 { "r8ui", GL_R8UI, GLSL_TYPE_UINT },
1260 { "rgba32i", GL_RGBA32I, GLSL_TYPE_INT },
1261 { "rgba16i", GL_RGBA16I, GLSL_TYPE_INT },
1262 { "rgba8i", GL_RGBA8I, GLSL_TYPE_INT },
1263 { "rg32i", GL_RG32I, GLSL_TYPE_INT },
1264 { "rg16i", GL_RG16I, GLSL_TYPE_INT },
1265 { "rg8i", GL_RG8I, GLSL_TYPE_INT },
1266 { "r32i", GL_R32I, GLSL_TYPE_INT },
1267 { "r16i", GL_R16I, GLSL_TYPE_INT },
1268 { "r8i", GL_R8I, GLSL_TYPE_INT },
1269 { "rgba16", GL_RGBA16, GLSL_TYPE_FLOAT },
1270 { "rgb10_a2", GL_RGB10_A2, GLSL_TYPE_FLOAT },
1271 { "rgba8", GL_RGBA8, GLSL_TYPE_FLOAT },
1272 { "rg16", GL_RG16, GLSL_TYPE_FLOAT },
1273 { "rg8", GL_RG8, GLSL_TYPE_FLOAT },
1274 { "r16", GL_R16, GLSL_TYPE_FLOAT },
1275 { "r8", GL_R8, GLSL_TYPE_FLOAT },
1276 { "rgba16_snorm", GL_RGBA16_SNORM, GLSL_TYPE_FLOAT },
1277 { "rgba8_snorm", GL_RGBA8_SNORM, GLSL_TYPE_FLOAT },
1278 { "rg16_snorm", GL_RG16_SNORM, GLSL_TYPE_FLOAT },
1279 { "rg8_snorm", GL_RG8_SNORM, GLSL_TYPE_FLOAT },
1280 { "r16_snorm", GL_R16_SNORM, GLSL_TYPE_FLOAT },
1281 { "r8_snorm", GL_R8_SNORM, GLSL_TYPE_FLOAT }
1284 for (unsigned i = 0; i < Elements(map); i++) {
1285 if (match_layout_qualifier($1, map[i].name, state) == 0) {
1286 $$.flags.q.explicit_image_format = 1;
1287 $$.image_format = map[i].format;
1288 $$.image_base_type = map[i].base_type;
1295 match_layout_qualifier($1, "early_fragment_tests", state) == 0) {
1296 $$.flags.q.early_fragment_tests = 1;
1301 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1306 | any_identifier '=' integer_constant
1308 memset(& $$, 0, sizeof($$));
1310 if (match_layout_qualifier("location", $1, state) == 0) {
1311 $$.flags.q.explicit_location = 1;
1316 _mesa_glsl_error(& @3, state, "invalid location %d specified", $3);
1321 if (match_layout_qualifier("index", $1, state) == 0) {
1322 $$.flags.q.explicit_index = 1;
1327 _mesa_glsl_error(& @3, state, "invalid index %d specified", $3);
1332 if ((state->ARB_shading_language_420pack_enable ||
1333 state->ARB_shader_atomic_counters_enable) &&
1334 match_layout_qualifier("binding", $1, state) == 0) {
1335 $$.flags.q.explicit_binding = 1;
1339 if (state->ARB_shader_atomic_counters_enable &&
1340 match_layout_qualifier("offset", $1, state) == 0) {
1341 $$.flags.q.explicit_offset = 1;
1345 if (match_layout_qualifier("max_vertices", $1, state) == 0) {
1346 $$.flags.q.max_vertices = 1;
1349 _mesa_glsl_error(& @3, state,
1350 "invalid max_vertices %d specified", $3);
1353 $$.max_vertices = $3;
1354 if (!state->is_version(150, 0)) {
1355 _mesa_glsl_error(& @3, state,
1356 "#version 150 max_vertices qualifier "
1362 static const char *local_size_qualifiers[3] = {
1367 for (int i = 0; i < 3; i++) {
1368 if (match_layout_qualifier(local_size_qualifiers[i], $1,
1371 _mesa_glsl_error(& @3, state,
1372 "invalid %s of %d specified",
1373 local_size_qualifiers[i], $3);
1375 } else if (!state->is_version(430, 0) &&
1376 !state->ARB_compute_shader_enable) {
1377 _mesa_glsl_error(& @3, state,
1378 "%s qualifier requires GLSL 4.30 or "
1379 "ARB_compute_shader",
1380 local_size_qualifiers[i]);
1383 $$.flags.q.local_size |= (1 << i);
1384 $$.local_size[i] = $3;
1390 /* If the identifier didn't match any known layout identifiers,
1394 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1397 } else if (state->ARB_explicit_attrib_location_warn) {
1398 _mesa_glsl_warning(& @1, state,
1399 "GL_ARB_explicit_attrib_location layout "
1400 "identifier `%s' used", $1);
1403 | interface_block_layout_qualifier
1406 /* Layout qualifiers for ARB_uniform_buffer_object. */
1407 if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
1408 _mesa_glsl_error(& @1, state,
1409 "#version 140 / GL_ARB_uniform_buffer_object "
1410 "layout qualifier `%s' is used", $1);
1411 } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1412 _mesa_glsl_warning(& @1, state,
1413 "#version 140 / GL_ARB_uniform_buffer_object "
1414 "layout qualifier `%s' is used", $1);
1419 /* This is a separate language rule because we parse these as tokens
1420 * (due to them being reserved keywords) instead of identifiers like
1421 * most qualifiers. See the any_identifier path of
1422 * layout_qualifier_id for the others.
1424 * Note that since layout qualifiers are case-insensitive in desktop
1425 * GLSL, all of these qualifiers need to be handled as identifiers as
1426 * well (by the any_identifier path of layout_qualifier_id).
1428 interface_block_layout_qualifier:
1431 memset(& $$, 0, sizeof($$));
1432 $$.flags.q.row_major = 1;
1436 memset(& $$, 0, sizeof($$));
1437 $$.flags.q.packed = 1;
1441 interpolation_qualifier:
1444 memset(& $$, 0, sizeof($$));
1445 $$.flags.q.smooth = 1;
1449 memset(& $$, 0, sizeof($$));
1450 $$.flags.q.flat = 1;
1454 memset(& $$, 0, sizeof($$));
1455 $$.flags.q.noperspective = 1;
1460 /* Single qualifiers */
1463 memset(& $$, 0, sizeof($$));
1464 $$.flags.q.invariant = 1;
1466 | auxiliary_storage_qualifier
1468 | interpolation_qualifier
1470 | precision_qualifier
1472 memset(&$$, 0, sizeof($$));
1476 /* Multiple qualifiers:
1477 * In GLSL 4.20, these can be specified in any order. In earlier versions,
1478 * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1480 * invariant interpolation auxiliary storage precision ...or...
1481 * layout storage precision
1483 * Each qualifier's rule ensures that the accumulated qualifiers on the right
1484 * side don't contain any that must appear on the left hand side.
1485 * For example, when processing a storage qualifier, we check that there are
1486 * no auxiliary, interpolation, layout, or invariant qualifiers to the right.
1488 | INVARIANT type_qualifier
1490 if ($2.flags.q.invariant)
1491 _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1493 if ($2.has_layout()) {
1494 _mesa_glsl_error(&@1, state,
1495 "\"invariant\" cannot be used with layout(...)");
1499 $$.flags.q.invariant = 1;
1501 | interpolation_qualifier type_qualifier
1503 /* Section 4.3 of the GLSL 1.40 specification states:
1504 * "...qualified with one of these interpolation qualifiers"
1506 * GLSL 1.30 claims to allow "one or more", but insists that:
1507 * "These interpolation qualifiers may only precede the qualifiers in,
1508 * centroid in, out, or centroid out in a declaration."
1510 * ...which means that e.g. smooth can't precede smooth, so there can be
1511 * only one after all, and the 1.40 text is a clarification, not a change.
1513 if ($2.has_interpolation())
1514 _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1516 if ($2.has_layout()) {
1517 _mesa_glsl_error(&@1, state, "interpolation qualifiers cannot be used "
1518 "with layout(...)");
1521 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.invariant) {
1522 _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1523 "after \"invariant\"");
1527 $$.merge_qualifier(&@1, state, $2);
1529 | layout_qualifier type_qualifier
1531 /* The GLSL 1.50 grammar indicates that a layout(...) declaration can be
1532 * used standalone or immediately before a storage qualifier. It cannot
1533 * be used with interpolation qualifiers or invariant. There does not
1534 * appear to be any text indicating that it must come before the storage
1535 * qualifier, but always seems to in examples.
1537 if (!state->ARB_shading_language_420pack_enable && $2.has_layout())
1538 _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
1540 if ($2.flags.q.invariant)
1541 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1542 "the \"invariant\" qualifier");
1544 if ($2.has_interpolation()) {
1545 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1546 "interpolation qualifiers");
1550 $$.merge_qualifier(&@1, state, $2);
1552 | auxiliary_storage_qualifier type_qualifier
1554 if ($2.has_auxiliary_storage()) {
1555 _mesa_glsl_error(&@1, state,
1556 "duplicate auxiliary storage qualifier (centroid or sample)");
1559 if (!state->ARB_shading_language_420pack_enable &&
1560 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout())) {
1561 _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
1562 "just before storage qualifiers");
1565 $$.merge_qualifier(&@1, state, $2);
1567 | storage_qualifier type_qualifier
1569 /* Section 4.3 of the GLSL 1.20 specification states:
1570 * "Variable declarations may have a storage qualifier specified..."
1571 * 1.30 clarifies this to "may have one storage qualifier".
1573 if ($2.has_storage())
1574 _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
1576 if (!state->ARB_shading_language_420pack_enable &&
1577 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout() ||
1578 $2.has_auxiliary_storage())) {
1579 _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
1580 "invariant, interpolation, layout and auxiliary "
1581 "storage qualifiers");
1585 $$.merge_qualifier(&@1, state, $2);
1587 | precision_qualifier type_qualifier
1589 if ($2.precision != ast_precision_none)
1590 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
1592 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
1593 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
1600 auxiliary_storage_qualifier:
1603 memset(& $$, 0, sizeof($$));
1604 $$.flags.q.centroid = 1;
1608 memset(& $$, 0, sizeof($$));
1609 $$.flags.q.sample = 1;
1611 /* TODO: "patch" also goes here someday. */
1616 memset(& $$, 0, sizeof($$));
1617 $$.flags.q.constant = 1;
1621 memset(& $$, 0, sizeof($$));
1622 $$.flags.q.attribute = 1;
1626 memset(& $$, 0, sizeof($$));
1627 $$.flags.q.varying = 1;
1631 memset(& $$, 0, sizeof($$));
1636 memset(& $$, 0, sizeof($$));
1641 memset(& $$, 0, sizeof($$));
1642 $$.flags.q.uniform = 1;
1646 memset(& $$, 0, sizeof($$));
1647 $$.flags.q.coherent = 1;
1651 memset(& $$, 0, sizeof($$));
1652 $$.flags.q._volatile = 1;
1656 STATIC_ASSERT(sizeof($$.flags.q) <= sizeof($$.flags.i));
1657 memset(& $$, 0, sizeof($$));
1658 $$.flags.q._restrict = 1;
1662 memset(& $$, 0, sizeof($$));
1663 $$.flags.q.read_only = 1;
1667 memset(& $$, 0, sizeof($$));
1668 $$.flags.q.write_only = 1;
1676 $$ = new(ctx) ast_array_specifier(yylloc);
1678 | '[' constant_expression ']'
1681 $$ = new(ctx) ast_array_specifier(yylloc, $2);
1683 | array_specifier '[' ']'
1687 if (!state->ARB_arrays_of_arrays_enable) {
1688 _mesa_glsl_error(& @1, state,
1689 "GL_ARB_arrays_of_arrays "
1690 "required for defining arrays of arrays");
1692 _mesa_glsl_error(& @1, state,
1693 "only the outermost array dimension can "
1697 | array_specifier '[' constant_expression ']'
1701 if (!state->ARB_arrays_of_arrays_enable) {
1702 _mesa_glsl_error(& @1, state,
1703 "GL_ARB_arrays_of_arrays "
1704 "required for defining arrays of arrays");
1707 $$->add_dimension($3);
1712 type_specifier_nonarray
1713 | type_specifier_nonarray array_specifier
1716 $$->array_specifier = $2;
1720 type_specifier_nonarray:
1721 basic_type_specifier_nonarray
1724 $$ = new(ctx) ast_type_specifier($1);
1725 $$->set_location(yylloc);
1730 $$ = new(ctx) ast_type_specifier($1);
1731 $$->set_location(yylloc);
1736 $$ = new(ctx) ast_type_specifier($1);
1737 $$->set_location(yylloc);
1741 basic_type_specifier_nonarray:
1742 VOID_TOK { $$ = "void"; }
1743 | FLOAT_TOK { $$ = "float"; }
1744 | INT_TOK { $$ = "int"; }
1745 | UINT_TOK { $$ = "uint"; }
1746 | BOOL_TOK { $$ = "bool"; }
1747 | VEC2 { $$ = "vec2"; }
1748 | VEC3 { $$ = "vec3"; }
1749 | VEC4 { $$ = "vec4"; }
1750 | BVEC2 { $$ = "bvec2"; }
1751 | BVEC3 { $$ = "bvec3"; }
1752 | BVEC4 { $$ = "bvec4"; }
1753 | IVEC2 { $$ = "ivec2"; }
1754 | IVEC3 { $$ = "ivec3"; }
1755 | IVEC4 { $$ = "ivec4"; }
1756 | UVEC2 { $$ = "uvec2"; }
1757 | UVEC3 { $$ = "uvec3"; }
1758 | UVEC4 { $$ = "uvec4"; }
1759 | MAT2X2 { $$ = "mat2"; }
1760 | MAT2X3 { $$ = "mat2x3"; }
1761 | MAT2X4 { $$ = "mat2x4"; }
1762 | MAT3X2 { $$ = "mat3x2"; }
1763 | MAT3X3 { $$ = "mat3"; }
1764 | MAT3X4 { $$ = "mat3x4"; }
1765 | MAT4X2 { $$ = "mat4x2"; }
1766 | MAT4X3 { $$ = "mat4x3"; }
1767 | MAT4X4 { $$ = "mat4"; }
1768 | SAMPLER1D { $$ = "sampler1D"; }
1769 | SAMPLER2D { $$ = "sampler2D"; }
1770 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
1771 | SAMPLER3D { $$ = "sampler3D"; }
1772 | SAMPLERCUBE { $$ = "samplerCube"; }
1773 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
1774 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
1775 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
1776 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
1777 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
1778 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
1779 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
1780 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
1781 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
1782 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
1783 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
1784 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
1785 | ISAMPLER1D { $$ = "isampler1D"; }
1786 | ISAMPLER2D { $$ = "isampler2D"; }
1787 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
1788 | ISAMPLER3D { $$ = "isampler3D"; }
1789 | ISAMPLERCUBE { $$ = "isamplerCube"; }
1790 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
1791 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
1792 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
1793 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
1794 | USAMPLER1D { $$ = "usampler1D"; }
1795 | USAMPLER2D { $$ = "usampler2D"; }
1796 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
1797 | USAMPLER3D { $$ = "usampler3D"; }
1798 | USAMPLERCUBE { $$ = "usamplerCube"; }
1799 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
1800 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
1801 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
1802 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
1803 | SAMPLER2DMS { $$ = "sampler2DMS"; }
1804 | ISAMPLER2DMS { $$ = "isampler2DMS"; }
1805 | USAMPLER2DMS { $$ = "usampler2DMS"; }
1806 | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
1807 | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
1808 | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
1809 | ATOMIC_UINT { $$ = "atomic_uint"; }
1812 precision_qualifier:
1815 state->check_precision_qualifiers_allowed(&@1);
1816 $$ = ast_precision_high;
1820 state->check_precision_qualifiers_allowed(&@1);
1821 $$ = ast_precision_medium;
1825 state->check_precision_qualifiers_allowed(&@1);
1826 $$ = ast_precision_low;
1831 STRUCT any_identifier '{' struct_declaration_list '}'
1834 $$ = new(ctx) ast_struct_specifier($2, $4);
1835 $$->set_location(yylloc);
1836 state->symbols->add_type($2, glsl_type::void_type);
1837 state->symbols->add_type_ast($2, new(ctx) ast_type_specifier($$));
1839 | STRUCT '{' struct_declaration_list '}'
1842 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1843 $$->set_location(yylloc);
1847 struct_declaration_list:
1851 $1->link.self_link();
1853 | struct_declaration_list struct_declaration
1856 $$->link.insert_before(& $2->link);
1861 fully_specified_type struct_declarator_list ';'
1864 ast_fully_specified_type *const type = $1;
1865 type->set_location(yylloc);
1867 if (type->qualifier.flags.i != 0)
1868 _mesa_glsl_error(&@1, state,
1869 "only precision qualifiers may be applied to "
1870 "structure members");
1872 $$ = new(ctx) ast_declarator_list(type);
1873 $$->set_location(yylloc);
1875 $$->declarations.push_degenerate_list_at_head(& $2->link);
1879 struct_declarator_list:
1883 $1->link.self_link();
1885 | struct_declarator_list ',' struct_declarator
1888 $$->link.insert_before(& $3->link);
1896 $$ = new(ctx) ast_declaration($1, NULL, NULL);
1897 $$->set_location(yylloc);
1899 | any_identifier array_specifier
1902 $$ = new(ctx) ast_declaration($1, $2, NULL);
1903 $$->set_location(yylloc);
1908 assignment_expression
1909 | '{' initializer_list '}'
1913 | '{' initializer_list ',' '}'
1923 $$ = new(ctx) ast_aggregate_initializer();
1924 $$->set_location(yylloc);
1925 $$->expressions.push_tail(& $1->link);
1927 | initializer_list ',' initializer
1929 $1->expressions.push_tail(& $3->link);
1933 declaration_statement:
1937 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1940 compound_statement { $$ = (ast_node *) $1; }
1945 declaration_statement
1946 | expression_statement
1947 | selection_statement
1949 | iteration_statement
1957 $$ = new(ctx) ast_compound_statement(true, NULL);
1958 $$->set_location(yylloc);
1962 state->symbols->push_scope();
1967 $$ = new(ctx) ast_compound_statement(true, $3);
1968 $$->set_location(yylloc);
1969 state->symbols->pop_scope();
1973 statement_no_new_scope:
1974 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1978 compound_statement_no_new_scope:
1982 $$ = new(ctx) ast_compound_statement(false, NULL);
1983 $$->set_location(yylloc);
1985 | '{' statement_list '}'
1988 $$ = new(ctx) ast_compound_statement(false, $2);
1989 $$->set_location(yylloc);
1997 _mesa_glsl_error(& @1, state, "<nil> statement");
2002 $$->link.self_link();
2004 | statement_list statement
2007 _mesa_glsl_error(& @2, state, "<nil> statement");
2011 $$->link.insert_before(& $2->link);
2015 expression_statement:
2019 $$ = new(ctx) ast_expression_statement(NULL);
2020 $$->set_location(yylloc);
2025 $$ = new(ctx) ast_expression_statement($1);
2026 $$->set_location(yylloc);
2030 selection_statement:
2031 IF '(' expression ')' selection_rest_statement
2033 $$ = new(state) ast_selection_statement($3, $5.then_statement,
2035 $$->set_location(yylloc);
2039 selection_rest_statement:
2040 statement ELSE statement
2042 $$.then_statement = $1;
2043 $$.else_statement = $3;
2045 | statement %prec THEN
2047 $$.then_statement = $1;
2048 $$.else_statement = NULL;
2055 $$ = (ast_node *) $1;
2057 | fully_specified_type any_identifier '=' initializer
2060 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
2061 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
2062 decl->set_location(yylloc);
2063 declarator->set_location(yylloc);
2065 declarator->declarations.push_tail(&decl->link);
2071 * siwtch_statement grammar is based on the syntax described in the body
2072 * of the GLSL spec, not in it's appendix!!!
2075 SWITCH '(' expression ')' switch_body
2077 $$ = new(state) ast_switch_statement($3, $5);
2078 $$->set_location(yylloc);
2085 $$ = new(state) ast_switch_body(NULL);
2086 $$->set_location(yylloc);
2088 | '{' case_statement_list '}'
2090 $$ = new(state) ast_switch_body($2);
2091 $$->set_location(yylloc);
2098 $$ = new(state) ast_case_label($2);
2099 $$->set_location(yylloc);
2103 $$ = new(state) ast_case_label(NULL);
2104 $$->set_location(yylloc);
2111 ast_case_label_list *labels = new(state) ast_case_label_list();
2113 labels->labels.push_tail(& $1->link);
2115 $$->set_location(yylloc);
2117 | case_label_list case_label
2120 $$->labels.push_tail(& $2->link);
2125 case_label_list statement
2127 ast_case_statement *stmts = new(state) ast_case_statement($1);
2128 stmts->set_location(yylloc);
2130 stmts->stmts.push_tail(& $2->link);
2133 | case_statement statement
2136 $$->stmts.push_tail(& $2->link);
2140 case_statement_list:
2143 ast_case_statement_list *cases= new(state) ast_case_statement_list();
2144 cases->set_location(yylloc);
2146 cases->cases.push_tail(& $1->link);
2149 | case_statement_list case_statement
2152 $$->cases.push_tail(& $2->link);
2156 iteration_statement:
2157 WHILE '(' condition ')' statement_no_new_scope
2160 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2161 NULL, $3, NULL, $5);
2162 $$->set_location(yylloc);
2164 | DO statement WHILE '(' expression ')' ';'
2167 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2168 NULL, $5, NULL, $2);
2169 $$->set_location(yylloc);
2171 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2174 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2175 $3, $4.cond, $4.rest, $6);
2176 $$->set_location(yylloc);
2181 expression_statement
2182 | declaration_statement
2199 | conditionopt ';' expression
2206 // Grammar Note: No 'goto'. Gotos are not supported.
2211 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2212 $$->set_location(yylloc);
2217 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2218 $$->set_location(yylloc);
2223 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2224 $$->set_location(yylloc);
2226 | RETURN expression ';'
2229 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2230 $$->set_location(yylloc);
2232 | DISCARD ';' // Fragment shader only.
2235 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2236 $$->set_location(yylloc);
2240 external_declaration:
2241 function_definition { $$ = $1; }
2242 | declaration { $$ = $1; }
2243 | pragma_statement { $$ = NULL; }
2244 | layout_defaults { $$ = $1; }
2247 function_definition:
2248 function_prototype compound_statement_no_new_scope
2251 $$ = new(ctx) ast_function_definition();
2252 $$->set_location(yylloc);
2256 state->symbols->pop_scope();
2260 /* layout_qualifieropt is packed into this rule */
2262 basic_interface_block
2266 | layout_qualifier basic_interface_block
2268 ast_interface_block *block = $2;
2269 if (!block->layout.merge_qualifier(& @1, state, $1)) {
2276 basic_interface_block:
2277 interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2279 ast_interface_block *const block = $6;
2281 block->block_name = $2;
2282 block->declarations.push_degenerate_list_at_head(& $4->link);
2284 if ($1.flags.q.uniform) {
2285 if (!state->has_uniform_buffer_objects()) {
2286 _mesa_glsl_error(& @1, state,
2287 "#version 140 / GL_ARB_uniform_buffer_object "
2288 "required for defining uniform blocks");
2289 } else if (state->ARB_uniform_buffer_object_warn) {
2290 _mesa_glsl_warning(& @1, state,
2291 "#version 140 / GL_ARB_uniform_buffer_object "
2292 "required for defining uniform blocks");
2295 if (state->es_shader || state->language_version < 150) {
2296 _mesa_glsl_error(& @1, state,
2297 "#version 150 required for using "
2298 "interface blocks");
2302 /* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):
2303 * "It is illegal to have an input block in a vertex shader
2304 * or an output block in a fragment shader"
2306 if ((state->stage == MESA_SHADER_VERTEX) && $1.flags.q.in) {
2307 _mesa_glsl_error(& @1, state,
2308 "`in' interface block is not allowed for "
2310 } else if ((state->stage == MESA_SHADER_FRAGMENT) && $1.flags.q.out) {
2311 _mesa_glsl_error(& @1, state,
2312 "`out' interface block is not allowed for "
2313 "a fragment shader");
2316 /* Since block arrays require names, and both features are added in
2317 * the same language versions, we don't have to explicitly
2318 * version-check both things.
2320 if (block->instance_name != NULL) {
2321 state->check_version(150, 300, & @1, "interface blocks with "
2322 "an instance name are not allowed");
2325 uint64_t interface_type_mask;
2326 struct ast_type_qualifier temp_type_qualifier;
2328 /* Get a bitmask containing only the in/out/uniform flags, allowing us
2329 * to ignore other irrelevant flags like interpolation qualifiers.
2331 temp_type_qualifier.flags.i = 0;
2332 temp_type_qualifier.flags.q.uniform = true;
2333 temp_type_qualifier.flags.q.in = true;
2334 temp_type_qualifier.flags.q.out = true;
2335 interface_type_mask = temp_type_qualifier.flags.i;
2337 /* Get the block's interface qualifier. The interface_qualifier
2338 * production rule guarantees that only one bit will be set (and
2339 * it will be in/out/uniform).
2341 uint64_t block_interface_qualifier = $1.flags.i;
2343 block->layout.flags.i |= block_interface_qualifier;
2345 foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
2346 ast_type_qualifier& qualifier = member->type->qualifier;
2347 if ((qualifier.flags.i & interface_type_mask) == 0) {
2348 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2349 * "If no optional qualifier is used in a member declaration, the
2350 * qualifier of the variable is just in, out, or uniform as declared
2351 * by interface-qualifier."
2353 qualifier.flags.i |= block_interface_qualifier;
2354 } else if ((qualifier.flags.i & interface_type_mask) !=
2355 block_interface_qualifier) {
2356 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2357 * "If optional qualifiers are used, they can include interpolation
2358 * and storage qualifiers and they must declare an input, output,
2359 * or uniform variable consistent with the interface qualifier of
2362 _mesa_glsl_error(& @1, state,
2363 "uniform/in/out qualifier on "
2364 "interface block member does not match "
2365 "the interface block");
2373 interface_qualifier:
2376 memset(& $$, 0, sizeof($$));
2381 memset(& $$, 0, sizeof($$));
2386 memset(& $$, 0, sizeof($$));
2387 $$.flags.q.uniform = 1;
2394 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2399 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2402 | NEW_IDENTIFIER array_specifier
2404 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2413 $1->link.self_link();
2415 | member_declaration member_list
2418 $2->link.insert_before(& $$->link);
2423 fully_specified_type struct_declarator_list ';'
2426 ast_fully_specified_type *type = $1;
2427 type->set_location(yylloc);
2429 if (type->qualifier.flags.q.attribute) {
2430 _mesa_glsl_error(& @1, state,
2431 "keyword 'attribute' cannot be used with "
2432 "interface block member");
2433 } else if (type->qualifier.flags.q.varying) {
2434 _mesa_glsl_error(& @1, state,
2435 "keyword 'varying' cannot be used with "
2436 "interface block member");
2439 $$ = new(ctx) ast_declarator_list(type);
2440 $$->set_location(yylloc);
2442 $$->declarations.push_degenerate_list_at_head(& $2->link);
2447 layout_qualifier UNIFORM ';'
2449 if (!state->default_uniform_qualifier->merge_qualifier(& @1, state, $1)) {
2455 | layout_qualifier IN_TOK ';'
2459 switch (state->stage) {
2460 case MESA_SHADER_GEOMETRY: {
2461 if (!$1.flags.q.prim_type) {
2462 _mesa_glsl_error(& @1, state,
2463 "input layout qualifiers must specify a primitive"
2466 /* Make sure this is a valid input primitive type. */
2467 switch ($1.prim_type) {
2470 case GL_LINES_ADJACENCY:
2472 case GL_TRIANGLES_ADJACENCY:
2473 $$ = new(ctx) ast_gs_input_layout(@1, $1.prim_type);
2476 _mesa_glsl_error(&@1, state,
2477 "invalid geometry shader input primitive type");
2483 case MESA_SHADER_FRAGMENT:
2484 if ($1.flags.q.early_fragment_tests) {
2485 state->early_fragment_tests = true;
2487 _mesa_glsl_error(& @1, state, "invalid input layout qualifier");
2490 case MESA_SHADER_COMPUTE: {
2491 if ($1.flags.q.local_size == 0) {
2492 _mesa_glsl_error(& @1, state,
2493 "input layout qualifiers must specify a local "
2496 /* Infer a local_size of 1 for every unspecified dimension */
2497 unsigned local_size[3];
2498 for (int i = 0; i < 3; i++) {
2499 if ($1.flags.q.local_size & (1 << i))
2500 local_size[i] = $1.local_size[i];
2504 $$ = new(ctx) ast_cs_input_layout(@1, local_size);
2509 _mesa_glsl_error(& @1, state,
2510 "input layout qualifiers only valid in "
2511 "geometry, fragment and compute shaders");
2516 | layout_qualifier OUT_TOK ';'
2518 if (state->stage != MESA_SHADER_GEOMETRY) {
2519 _mesa_glsl_error(& @1, state,
2520 "out layout qualifiers only valid in "
2521 "geometry shaders");
2523 if ($1.flags.q.prim_type) {
2524 /* Make sure this is a valid output primitive type. */
2525 switch ($1.prim_type) {
2528 case GL_TRIANGLE_STRIP:
2531 _mesa_glsl_error(&@1, state, "invalid geometry shader output "
2536 if (!state->out_qualifier->merge_qualifier(& @1, state, $1))