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(@1);
392 $$->primary_expression.identifier = $1;
397 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
398 $$->set_location(@1);
399 $$->primary_expression.int_constant = $1;
404 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
405 $$->set_location(@1);
406 $$->primary_expression.uint_constant = $1;
411 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
412 $$->set_location(@1);
413 $$->primary_expression.float_constant = $1;
418 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
419 $$->set_location(@1);
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_range(@1, @4);
440 | postfix_expression '.' any_identifier
443 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
444 $$->set_location_range(@1, @3);
445 $$->primary_expression.identifier = $3;
447 | postfix_expression INC_OP
450 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
451 $$->set_location_range(@1, @2);
453 | postfix_expression DEC_OP
456 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
457 $$->set_location_range(@1, @2);
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_range(@1, @3);
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(@1);
494 $$->expressions.push_tail(& $2->link);
496 | function_call_header_with_parameters ',' assignment_expression
499 $$->set_location(@1);
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(@1);
518 | variable_identifier
521 ast_expression *callee = new(ctx) ast_expression($1);
522 callee->set_location(@1);
523 $$ = new(ctx) ast_function_expression(callee);
524 $$->set_location(@1);
529 ast_expression *callee = new(ctx) ast_expression($1);
530 callee->set_location(@1);
531 $$ = new(ctx) ast_function_expression(callee);
532 $$->set_location(@1);
537 method_call_header_with_parameters ')'
538 | method_call_header_no_parameters ')'
541 method_call_header_no_parameters:
542 method_call_header VOID_TOK
546 method_call_header_with_parameters:
547 method_call_header assignment_expression
550 $$->set_location(@1);
551 $$->expressions.push_tail(& $2->link);
553 | method_call_header_with_parameters ',' assignment_expression
556 $$->set_location(@1);
557 $$->expressions.push_tail(& $3->link);
561 // Grammar Note: Constructors look like methods, but lexical
562 // analysis recognized most of them as keywords. They are now
563 // recognized through "type_specifier".
565 variable_identifier '('
568 ast_expression *callee = new(ctx) ast_expression($1);
569 callee->set_location(@1);
570 $$ = new(ctx) ast_function_expression(callee);
571 $$->set_location(@1);
575 // Grammar Note: No traditional style type casts.
578 | INC_OP unary_expression
581 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
582 $$->set_location(@1);
584 | DEC_OP unary_expression
587 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
588 $$->set_location(@1);
590 | unary_operator unary_expression
593 $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
594 $$->set_location_range(@1, @2);
598 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
600 '+' { $$ = ast_plus; }
601 | '-' { $$ = ast_neg; }
602 | '!' { $$ = ast_logic_not; }
603 | '~' { $$ = ast_bit_not; }
606 multiplicative_expression:
608 | multiplicative_expression '*' unary_expression
611 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
612 $$->set_location_range(@1, @3);
614 | multiplicative_expression '/' unary_expression
617 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
618 $$->set_location_range(@1, @3);
620 | multiplicative_expression '%' unary_expression
623 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
624 $$->set_location_range(@1, @3);
629 multiplicative_expression
630 | additive_expression '+' multiplicative_expression
633 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
634 $$->set_location_range(@1, @3);
636 | additive_expression '-' multiplicative_expression
639 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
640 $$->set_location_range(@1, @3);
646 | shift_expression LEFT_OP additive_expression
649 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
650 $$->set_location_range(@1, @3);
652 | shift_expression RIGHT_OP additive_expression
655 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
656 $$->set_location_range(@1, @3);
660 relational_expression:
662 | relational_expression '<' shift_expression
665 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
666 $$->set_location_range(@1, @3);
668 | relational_expression '>' shift_expression
671 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
672 $$->set_location_range(@1, @3);
674 | relational_expression LE_OP shift_expression
677 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
678 $$->set_location_range(@1, @3);
680 | relational_expression GE_OP shift_expression
683 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
684 $$->set_location_range(@1, @3);
689 relational_expression
690 | equality_expression EQ_OP relational_expression
693 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
694 $$->set_location_range(@1, @3);
696 | equality_expression NE_OP relational_expression
699 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
700 $$->set_location_range(@1, @3);
706 | and_expression '&' equality_expression
709 $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
710 $$->set_location_range(@1, @3);
714 exclusive_or_expression:
716 | exclusive_or_expression '^' and_expression
719 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
720 $$->set_location_range(@1, @3);
724 inclusive_or_expression:
725 exclusive_or_expression
726 | inclusive_or_expression '|' exclusive_or_expression
729 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
730 $$->set_location_range(@1, @3);
734 logical_and_expression:
735 inclusive_or_expression
736 | logical_and_expression AND_OP inclusive_or_expression
739 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
740 $$->set_location_range(@1, @3);
744 logical_xor_expression:
745 logical_and_expression
746 | logical_xor_expression XOR_OP logical_and_expression
749 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
750 $$->set_location_range(@1, @3);
754 logical_or_expression:
755 logical_xor_expression
756 | logical_or_expression OR_OP logical_xor_expression
759 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
760 $$->set_location_range(@1, @3);
764 conditional_expression:
765 logical_or_expression
766 | logical_or_expression '?' expression ':' assignment_expression
769 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
770 $$->set_location_range(@1, @5);
774 assignment_expression:
775 conditional_expression
776 | unary_expression assignment_operator assignment_expression
779 $$ = new(ctx) ast_expression($2, $1, $3, NULL);
780 $$->set_location_range(@1, @3);
785 '=' { $$ = ast_assign; }
786 | MUL_ASSIGN { $$ = ast_mul_assign; }
787 | DIV_ASSIGN { $$ = ast_div_assign; }
788 | MOD_ASSIGN { $$ = ast_mod_assign; }
789 | ADD_ASSIGN { $$ = ast_add_assign; }
790 | SUB_ASSIGN { $$ = ast_sub_assign; }
791 | LEFT_ASSIGN { $$ = ast_ls_assign; }
792 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
793 | AND_ASSIGN { $$ = ast_and_assign; }
794 | XOR_ASSIGN { $$ = ast_xor_assign; }
795 | OR_ASSIGN { $$ = ast_or_assign; }
799 assignment_expression
803 | expression ',' assignment_expression
806 if ($1->oper != ast_sequence) {
807 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
808 $$->set_location_range(@1, @3);
809 $$->expressions.push_tail(& $1->link);
814 $$->expressions.push_tail(& $3->link);
819 conditional_expression
823 function_prototype ';'
825 state->symbols->pop_scope();
828 | init_declarator_list ';'
832 | PRECISION precision_qualifier type_specifier ';'
834 $3->default_precision = $2;
844 function_declarator ')'
849 | function_header_with_parameters
852 function_header_with_parameters:
853 function_header parameter_declaration
856 $$->parameters.push_tail(& $2->link);
858 | function_header_with_parameters ',' parameter_declaration
861 $$->parameters.push_tail(& $3->link);
866 fully_specified_type variable_identifier '('
869 $$ = new(ctx) ast_function();
870 $$->set_location(@2);
871 $$->return_type = $1;
874 state->symbols->add_function(new(state) ir_function($2));
875 state->symbols->push_scope();
879 parameter_declarator:
880 type_specifier any_identifier
883 $$ = new(ctx) ast_parameter_declarator();
884 $$->set_location_range(@1, @2);
885 $$->type = new(ctx) ast_fully_specified_type();
886 $$->type->set_location(@1);
887 $$->type->specifier = $1;
890 | type_specifier any_identifier array_specifier
893 $$ = new(ctx) ast_parameter_declarator();
894 $$->set_location_range(@1, @3);
895 $$->type = new(ctx) ast_fully_specified_type();
896 $$->type->set_location(@1);
897 $$->type->specifier = $1;
899 $$->array_specifier = $3;
903 parameter_declaration:
904 parameter_qualifier parameter_declarator
907 $$->type->qualifier = $1;
909 | parameter_qualifier parameter_type_specifier
912 $$ = new(ctx) ast_parameter_declarator();
913 $$->set_location(@2);
914 $$->type = new(ctx) ast_fully_specified_type();
915 $$->type->set_location_range(@1, @2);
916 $$->type->qualifier = $1;
917 $$->type->specifier = $2;
924 memset(& $$, 0, sizeof($$));
926 | CONST_TOK parameter_qualifier
928 if ($2.flags.q.constant)
929 _mesa_glsl_error(&@1, state, "duplicate const qualifier");
932 $$.flags.q.constant = 1;
934 | parameter_direction_qualifier parameter_qualifier
936 if (($1.flags.q.in || $1.flags.q.out) && ($2.flags.q.in || $2.flags.q.out))
937 _mesa_glsl_error(&@1, state, "duplicate in/out/inout qualifier");
939 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.constant)
940 _mesa_glsl_error(&@1, state, "const must be specified before "
944 $$.merge_qualifier(&@1, state, $2);
946 | precision_qualifier parameter_qualifier
948 if ($2.precision != ast_precision_none)
949 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
951 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
952 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
958 parameter_direction_qualifier:
961 memset(& $$, 0, sizeof($$));
966 memset(& $$, 0, sizeof($$));
971 memset(& $$, 0, sizeof($$));
977 parameter_type_specifier:
981 init_declarator_list:
983 | init_declarator_list ',' any_identifier
986 ast_declaration *decl = new(ctx) ast_declaration($3, NULL, NULL);
987 decl->set_location(@3);
990 $$->declarations.push_tail(&decl->link);
991 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
993 | init_declarator_list ',' any_identifier array_specifier
996 ast_declaration *decl = new(ctx) ast_declaration($3, $4, NULL);
997 decl->set_location_range(@3, @4);
1000 $$->declarations.push_tail(&decl->link);
1001 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1003 | init_declarator_list ',' any_identifier array_specifier '=' initializer
1006 ast_declaration *decl = new(ctx) ast_declaration($3, $4, $6);
1007 decl->set_location_range(@3, @4);
1010 $$->declarations.push_tail(&decl->link);
1011 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1013 | init_declarator_list ',' any_identifier '=' initializer
1016 ast_declaration *decl = new(ctx) ast_declaration($3, NULL, $5);
1017 decl->set_location(@3);
1020 $$->declarations.push_tail(&decl->link);
1021 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
1025 // Grammar Note: No 'enum', or 'typedef'.
1027 fully_specified_type
1030 /* Empty declaration list is valid. */
1031 $$ = new(ctx) ast_declarator_list($1);
1032 $$->set_location(@1);
1034 | fully_specified_type any_identifier
1037 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
1038 decl->set_location(@2);
1040 $$ = new(ctx) ast_declarator_list($1);
1041 $$->set_location_range(@1, @2);
1042 $$->declarations.push_tail(&decl->link);
1044 | fully_specified_type any_identifier array_specifier
1047 ast_declaration *decl = new(ctx) ast_declaration($2, $3, NULL);
1048 decl->set_location_range(@2, @3);
1050 $$ = new(ctx) ast_declarator_list($1);
1051 $$->set_location_range(@1, @3);
1052 $$->declarations.push_tail(&decl->link);
1054 | fully_specified_type any_identifier array_specifier '=' initializer
1057 ast_declaration *decl = new(ctx) ast_declaration($2, $3, $5);
1058 decl->set_location_range(@2, @3);
1060 $$ = new(ctx) ast_declarator_list($1);
1061 $$->set_location_range(@1, @3);
1062 $$->declarations.push_tail(&decl->link);
1064 | fully_specified_type any_identifier '=' initializer
1067 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
1068 decl->set_location(@2);
1070 $$ = new(ctx) ast_declarator_list($1);
1071 $$->set_location_range(@1, @2);
1072 $$->declarations.push_tail(&decl->link);
1074 | INVARIANT variable_identifier // Vertex only.
1077 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, NULL);
1078 decl->set_location(@2);
1080 $$ = new(ctx) ast_declarator_list(NULL);
1081 $$->set_location_range(@1, @2);
1082 $$->invariant = true;
1084 $$->declarations.push_tail(&decl->link);
1088 fully_specified_type:
1092 $$ = new(ctx) ast_fully_specified_type();
1093 $$->set_location(@1);
1096 | type_qualifier type_specifier
1099 $$ = new(ctx) ast_fully_specified_type();
1100 $$->set_location_range(@1, @2);
1107 LAYOUT_TOK '(' layout_qualifier_id_list ')'
1113 layout_qualifier_id_list:
1115 | layout_qualifier_id_list ',' layout_qualifier_id
1118 if (!$$.merge_qualifier(& @3, state, $3)) {
1125 INTCONSTANT { $$ = $1; }
1126 | UINTCONSTANT { $$ = $1; }
1129 layout_qualifier_id:
1132 memset(& $$, 0, sizeof($$));
1134 /* Layout qualifiers for ARB_fragment_coord_conventions. */
1135 if (!$$.flags.i && (state->ARB_fragment_coord_conventions_enable ||
1136 state->is_version(150, 0))) {
1137 if (match_layout_qualifier($1, "origin_upper_left", state) == 0) {
1138 $$.flags.q.origin_upper_left = 1;
1139 } else if (match_layout_qualifier($1, "pixel_center_integer",
1141 $$.flags.q.pixel_center_integer = 1;
1144 if ($$.flags.i && state->ARB_fragment_coord_conventions_warn) {
1145 _mesa_glsl_warning(& @1, state,
1146 "GL_ARB_fragment_coord_conventions layout "
1147 "identifier `%s' used", $1);
1151 /* Layout qualifiers for AMD/ARB_conservative_depth. */
1153 (state->AMD_conservative_depth_enable ||
1154 state->ARB_conservative_depth_enable)) {
1155 if (match_layout_qualifier($1, "depth_any", state) == 0) {
1156 $$.flags.q.depth_any = 1;
1157 } else if (match_layout_qualifier($1, "depth_greater", state) == 0) {
1158 $$.flags.q.depth_greater = 1;
1159 } else if (match_layout_qualifier($1, "depth_less", state) == 0) {
1160 $$.flags.q.depth_less = 1;
1161 } else if (match_layout_qualifier($1, "depth_unchanged",
1163 $$.flags.q.depth_unchanged = 1;
1166 if ($$.flags.i && state->AMD_conservative_depth_warn) {
1167 _mesa_glsl_warning(& @1, state,
1168 "GL_AMD_conservative_depth "
1169 "layout qualifier `%s' is used", $1);
1171 if ($$.flags.i && state->ARB_conservative_depth_warn) {
1172 _mesa_glsl_warning(& @1, state,
1173 "GL_ARB_conservative_depth "
1174 "layout qualifier `%s' is used", $1);
1178 /* See also interface_block_layout_qualifier. */
1179 if (!$$.flags.i && state->has_uniform_buffer_objects()) {
1180 if (match_layout_qualifier($1, "std140", state) == 0) {
1181 $$.flags.q.std140 = 1;
1182 } else if (match_layout_qualifier($1, "shared", state) == 0) {
1183 $$.flags.q.shared = 1;
1184 } else if (match_layout_qualifier($1, "column_major", state) == 0) {
1185 $$.flags.q.column_major = 1;
1186 /* "row_major" is a reserved word in GLSL 1.30+. Its token is parsed
1187 * below in the interface_block_layout_qualifier rule.
1189 * It is not a reserved word in GLSL ES 3.00, so it's handled here as
1192 * Also, this takes care of alternate capitalizations of
1193 * "row_major" (which is necessary because layout qualifiers
1194 * are case-insensitive in desktop GLSL).
1196 } else if (match_layout_qualifier($1, "row_major", state) == 0) {
1197 $$.flags.q.row_major = 1;
1198 /* "packed" is a reserved word in GLSL, and its token is
1199 * parsed below in the interface_block_layout_qualifier rule.
1200 * However, we must take care of alternate capitalizations of
1201 * "packed", because layout qualifiers are case-insensitive
1204 } else if (match_layout_qualifier($1, "packed", state) == 0) {
1205 $$.flags.q.packed = 1;
1208 if ($$.flags.i && state->ARB_uniform_buffer_object_warn) {
1209 _mesa_glsl_warning(& @1, state,
1210 "#version 140 / GL_ARB_uniform_buffer_object "
1211 "layout qualifier `%s' is used", $1);
1215 /* Layout qualifiers for GLSL 1.50 geometry shaders. */
1217 static const struct {
1221 { "points", GL_POINTS },
1222 { "lines", GL_LINES },
1223 { "lines_adjacency", GL_LINES_ADJACENCY },
1224 { "line_strip", GL_LINE_STRIP },
1225 { "triangles", GL_TRIANGLES },
1226 { "triangles_adjacency", GL_TRIANGLES_ADJACENCY },
1227 { "triangle_strip", GL_TRIANGLE_STRIP },
1229 for (unsigned i = 0; i < Elements(map); i++) {
1230 if (match_layout_qualifier($1, map[i].s, state) == 0) {
1231 $$.flags.q.prim_type = 1;
1232 $$.prim_type = map[i].e;
1237 if ($$.flags.i && !state->is_version(150, 0)) {
1238 _mesa_glsl_error(& @1, state, "#version 150 layout "
1239 "qualifier `%s' used", $1);
1243 /* Layout qualifiers for ARB_shader_image_load_store. */
1244 if (state->ARB_shader_image_load_store_enable ||
1245 state->is_version(420, 0)) {
1247 static const struct {
1250 glsl_base_type base_type;
1252 { "rgba32f", GL_RGBA32F, GLSL_TYPE_FLOAT },
1253 { "rgba16f", GL_RGBA16F, GLSL_TYPE_FLOAT },
1254 { "rg32f", GL_RG32F, GLSL_TYPE_FLOAT },
1255 { "rg16f", GL_RG16F, GLSL_TYPE_FLOAT },
1256 { "r11f_g11f_b10f", GL_R11F_G11F_B10F, GLSL_TYPE_FLOAT },
1257 { "r32f", GL_R32F, GLSL_TYPE_FLOAT },
1258 { "r16f", GL_R16F, GLSL_TYPE_FLOAT },
1259 { "rgba32ui", GL_RGBA32UI, GLSL_TYPE_UINT },
1260 { "rgba16ui", GL_RGBA16UI, GLSL_TYPE_UINT },
1261 { "rgb10_a2ui", GL_RGB10_A2UI, GLSL_TYPE_UINT },
1262 { "rgba8ui", GL_RGBA8UI, GLSL_TYPE_UINT },
1263 { "rg32ui", GL_RG32UI, GLSL_TYPE_UINT },
1264 { "rg16ui", GL_RG16UI, GLSL_TYPE_UINT },
1265 { "rg8ui", GL_RG8UI, GLSL_TYPE_UINT },
1266 { "r32ui", GL_R32UI, GLSL_TYPE_UINT },
1267 { "r16ui", GL_R16UI, GLSL_TYPE_UINT },
1268 { "r8ui", GL_R8UI, GLSL_TYPE_UINT },
1269 { "rgba32i", GL_RGBA32I, GLSL_TYPE_INT },
1270 { "rgba16i", GL_RGBA16I, GLSL_TYPE_INT },
1271 { "rgba8i", GL_RGBA8I, GLSL_TYPE_INT },
1272 { "rg32i", GL_RG32I, GLSL_TYPE_INT },
1273 { "rg16i", GL_RG16I, GLSL_TYPE_INT },
1274 { "rg8i", GL_RG8I, GLSL_TYPE_INT },
1275 { "r32i", GL_R32I, GLSL_TYPE_INT },
1276 { "r16i", GL_R16I, GLSL_TYPE_INT },
1277 { "r8i", GL_R8I, GLSL_TYPE_INT },
1278 { "rgba16", GL_RGBA16, GLSL_TYPE_FLOAT },
1279 { "rgb10_a2", GL_RGB10_A2, GLSL_TYPE_FLOAT },
1280 { "rgba8", GL_RGBA8, GLSL_TYPE_FLOAT },
1281 { "rg16", GL_RG16, GLSL_TYPE_FLOAT },
1282 { "rg8", GL_RG8, GLSL_TYPE_FLOAT },
1283 { "r16", GL_R16, GLSL_TYPE_FLOAT },
1284 { "r8", GL_R8, GLSL_TYPE_FLOAT },
1285 { "rgba16_snorm", GL_RGBA16_SNORM, GLSL_TYPE_FLOAT },
1286 { "rgba8_snorm", GL_RGBA8_SNORM, GLSL_TYPE_FLOAT },
1287 { "rg16_snorm", GL_RG16_SNORM, GLSL_TYPE_FLOAT },
1288 { "rg8_snorm", GL_RG8_SNORM, GLSL_TYPE_FLOAT },
1289 { "r16_snorm", GL_R16_SNORM, GLSL_TYPE_FLOAT },
1290 { "r8_snorm", GL_R8_SNORM, GLSL_TYPE_FLOAT }
1293 for (unsigned i = 0; i < Elements(map); i++) {
1294 if (match_layout_qualifier($1, map[i].name, state) == 0) {
1295 $$.flags.q.explicit_image_format = 1;
1296 $$.image_format = map[i].format;
1297 $$.image_base_type = map[i].base_type;
1304 match_layout_qualifier($1, "early_fragment_tests", state) == 0) {
1305 $$.flags.q.early_fragment_tests = 1;
1310 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1315 | any_identifier '=' integer_constant
1317 memset(& $$, 0, sizeof($$));
1319 if (match_layout_qualifier("location", $1, state) == 0) {
1320 $$.flags.q.explicit_location = 1;
1322 if ($$.flags.q.attribute == 1 &&
1323 state->ARB_explicit_attrib_location_warn) {
1324 _mesa_glsl_warning(& @1, state,
1325 "GL_ARB_explicit_attrib_location layout "
1326 "identifier `%s' used", $1);
1332 _mesa_glsl_error(& @3, state, "invalid location %d specified", $3);
1337 if (match_layout_qualifier("index", $1, state) == 0) {
1338 $$.flags.q.explicit_index = 1;
1343 _mesa_glsl_error(& @3, state, "invalid index %d specified", $3);
1348 if ((state->ARB_shading_language_420pack_enable ||
1349 state->ARB_shader_atomic_counters_enable) &&
1350 match_layout_qualifier("binding", $1, state) == 0) {
1351 $$.flags.q.explicit_binding = 1;
1355 if (state->ARB_shader_atomic_counters_enable &&
1356 match_layout_qualifier("offset", $1, state) == 0) {
1357 $$.flags.q.explicit_offset = 1;
1361 if (match_layout_qualifier("max_vertices", $1, state) == 0) {
1362 $$.flags.q.max_vertices = 1;
1365 _mesa_glsl_error(& @3, state,
1366 "invalid max_vertices %d specified", $3);
1369 $$.max_vertices = $3;
1370 if (!state->is_version(150, 0)) {
1371 _mesa_glsl_error(& @3, state,
1372 "#version 150 max_vertices qualifier "
1378 static const char * const local_size_qualifiers[3] = {
1383 for (int i = 0; i < 3; i++) {
1384 if (match_layout_qualifier(local_size_qualifiers[i], $1,
1387 _mesa_glsl_error(& @3, state,
1388 "invalid %s of %d specified",
1389 local_size_qualifiers[i], $3);
1391 } else if (!state->is_version(430, 0) &&
1392 !state->ARB_compute_shader_enable) {
1393 _mesa_glsl_error(& @3, state,
1394 "%s qualifier requires GLSL 4.30 or "
1395 "ARB_compute_shader",
1396 local_size_qualifiers[i]);
1399 $$.flags.q.local_size |= (1 << i);
1400 $$.local_size[i] = $3;
1406 if (match_layout_qualifier("invocations", $1, state) == 0) {
1407 $$.flags.q.invocations = 1;
1410 _mesa_glsl_error(& @3, state,
1411 "invalid invocations %d specified", $3);
1413 } else if ($3 > MAX_GEOMETRY_SHADER_INVOCATIONS) {
1414 _mesa_glsl_error(& @3, state,
1415 "invocations (%d) exceeds "
1416 "GL_MAX_GEOMETRY_SHADER_INVOCATIONS", $3);
1419 $$.invocations = $3;
1420 if (!state->is_version(400, 0) &&
1421 !state->ARB_gpu_shader5_enable) {
1422 _mesa_glsl_error(& @3, state,
1423 "GL_ARB_gpu_shader5 invocations "
1424 "qualifier specified", $3);
1429 /* If the identifier didn't match any known layout identifiers,
1433 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1438 | interface_block_layout_qualifier
1441 /* Layout qualifiers for ARB_uniform_buffer_object. */
1442 if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
1443 _mesa_glsl_error(& @1, state,
1444 "#version 140 / GL_ARB_uniform_buffer_object "
1445 "layout qualifier `%s' is used", $1);
1446 } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1447 _mesa_glsl_warning(& @1, state,
1448 "#version 140 / GL_ARB_uniform_buffer_object "
1449 "layout qualifier `%s' is used", $1);
1454 /* This is a separate language rule because we parse these as tokens
1455 * (due to them being reserved keywords) instead of identifiers like
1456 * most qualifiers. See the any_identifier path of
1457 * layout_qualifier_id for the others.
1459 * Note that since layout qualifiers are case-insensitive in desktop
1460 * GLSL, all of these qualifiers need to be handled as identifiers as
1461 * well (by the any_identifier path of layout_qualifier_id).
1463 interface_block_layout_qualifier:
1466 memset(& $$, 0, sizeof($$));
1467 $$.flags.q.row_major = 1;
1471 memset(& $$, 0, sizeof($$));
1472 $$.flags.q.packed = 1;
1476 interpolation_qualifier:
1479 memset(& $$, 0, sizeof($$));
1480 $$.flags.q.smooth = 1;
1484 memset(& $$, 0, sizeof($$));
1485 $$.flags.q.flat = 1;
1489 memset(& $$, 0, sizeof($$));
1490 $$.flags.q.noperspective = 1;
1495 /* Single qualifiers */
1498 memset(& $$, 0, sizeof($$));
1499 $$.flags.q.invariant = 1;
1501 | auxiliary_storage_qualifier
1503 | interpolation_qualifier
1505 | precision_qualifier
1507 memset(&$$, 0, sizeof($$));
1511 /* Multiple qualifiers:
1512 * In GLSL 4.20, these can be specified in any order. In earlier versions,
1513 * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1515 * invariant interpolation auxiliary storage precision ...or...
1516 * layout storage precision
1518 * Each qualifier's rule ensures that the accumulated qualifiers on the right
1519 * side don't contain any that must appear on the left hand side.
1520 * For example, when processing a storage qualifier, we check that there are
1521 * no auxiliary, interpolation, layout, or invariant qualifiers to the right.
1523 | INVARIANT type_qualifier
1525 if ($2.flags.q.invariant)
1526 _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1528 if ($2.has_layout()) {
1529 _mesa_glsl_error(&@1, state,
1530 "\"invariant\" cannot be used with layout(...)");
1534 $$.flags.q.invariant = 1;
1536 | interpolation_qualifier type_qualifier
1538 /* Section 4.3 of the GLSL 1.40 specification states:
1539 * "...qualified with one of these interpolation qualifiers"
1541 * GLSL 1.30 claims to allow "one or more", but insists that:
1542 * "These interpolation qualifiers may only precede the qualifiers in,
1543 * centroid in, out, or centroid out in a declaration."
1545 * ...which means that e.g. smooth can't precede smooth, so there can be
1546 * only one after all, and the 1.40 text is a clarification, not a change.
1548 if ($2.has_interpolation())
1549 _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1551 if ($2.has_layout()) {
1552 _mesa_glsl_error(&@1, state, "interpolation qualifiers cannot be used "
1553 "with layout(...)");
1556 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.invariant) {
1557 _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1558 "after \"invariant\"");
1562 $$.merge_qualifier(&@1, state, $2);
1564 | layout_qualifier type_qualifier
1566 /* The GLSL 1.50 grammar indicates that a layout(...) declaration can be
1567 * used standalone or immediately before a storage qualifier. It cannot
1568 * be used with interpolation qualifiers or invariant. There does not
1569 * appear to be any text indicating that it must come before the storage
1570 * qualifier, but always seems to in examples.
1572 if (!state->ARB_shading_language_420pack_enable && $2.has_layout())
1573 _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
1575 if ($2.flags.q.invariant)
1576 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1577 "the \"invariant\" qualifier");
1579 if ($2.has_interpolation()) {
1580 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1581 "interpolation qualifiers");
1585 $$.merge_qualifier(&@1, state, $2);
1587 | auxiliary_storage_qualifier type_qualifier
1589 if ($2.has_auxiliary_storage()) {
1590 _mesa_glsl_error(&@1, state,
1591 "duplicate auxiliary storage qualifier (centroid or sample)");
1594 if (!state->ARB_shading_language_420pack_enable &&
1595 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout())) {
1596 _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
1597 "just before storage qualifiers");
1600 $$.merge_qualifier(&@1, state, $2);
1602 | storage_qualifier type_qualifier
1604 /* Section 4.3 of the GLSL 1.20 specification states:
1605 * "Variable declarations may have a storage qualifier specified..."
1606 * 1.30 clarifies this to "may have one storage qualifier".
1608 if ($2.has_storage())
1609 _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
1611 if (!state->ARB_shading_language_420pack_enable &&
1612 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout() ||
1613 $2.has_auxiliary_storage())) {
1614 _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
1615 "invariant, interpolation, layout and auxiliary "
1616 "storage qualifiers");
1620 $$.merge_qualifier(&@1, state, $2);
1622 | precision_qualifier type_qualifier
1624 if ($2.precision != ast_precision_none)
1625 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
1627 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
1628 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
1635 auxiliary_storage_qualifier:
1638 memset(& $$, 0, sizeof($$));
1639 $$.flags.q.centroid = 1;
1643 memset(& $$, 0, sizeof($$));
1644 $$.flags.q.sample = 1;
1646 /* TODO: "patch" also goes here someday. */
1651 memset(& $$, 0, sizeof($$));
1652 $$.flags.q.constant = 1;
1656 memset(& $$, 0, sizeof($$));
1657 $$.flags.q.attribute = 1;
1661 memset(& $$, 0, sizeof($$));
1662 $$.flags.q.varying = 1;
1666 memset(& $$, 0, sizeof($$));
1671 memset(& $$, 0, sizeof($$));
1676 memset(& $$, 0, sizeof($$));
1677 $$.flags.q.uniform = 1;
1681 memset(& $$, 0, sizeof($$));
1682 $$.flags.q.coherent = 1;
1686 memset(& $$, 0, sizeof($$));
1687 $$.flags.q._volatile = 1;
1691 STATIC_ASSERT(sizeof($$.flags.q) <= sizeof($$.flags.i));
1692 memset(& $$, 0, sizeof($$));
1693 $$.flags.q.restrict_flag = 1;
1697 memset(& $$, 0, sizeof($$));
1698 $$.flags.q.read_only = 1;
1702 memset(& $$, 0, sizeof($$));
1703 $$.flags.q.write_only = 1;
1711 $$ = new(ctx) ast_array_specifier(@1);
1712 $$->set_location_range(@1, @2);
1714 | '[' constant_expression ']'
1717 $$ = new(ctx) ast_array_specifier(@1, $2);
1718 $$->set_location_range(@1, @3);
1720 | array_specifier '[' ']'
1724 if (!state->ARB_arrays_of_arrays_enable) {
1725 _mesa_glsl_error(& @1, state,
1726 "GL_ARB_arrays_of_arrays "
1727 "required for defining arrays of arrays");
1729 _mesa_glsl_error(& @1, state,
1730 "only the outermost array dimension can "
1734 | array_specifier '[' constant_expression ']'
1738 if (!state->ARB_arrays_of_arrays_enable) {
1739 _mesa_glsl_error(& @1, state,
1740 "GL_ARB_arrays_of_arrays "
1741 "required for defining arrays of arrays");
1744 $$->add_dimension($3);
1749 type_specifier_nonarray
1750 | type_specifier_nonarray array_specifier
1753 $$->array_specifier = $2;
1757 type_specifier_nonarray:
1758 basic_type_specifier_nonarray
1761 $$ = new(ctx) ast_type_specifier($1);
1762 $$->set_location(@1);
1767 $$ = new(ctx) ast_type_specifier($1);
1768 $$->set_location(@1);
1773 $$ = new(ctx) ast_type_specifier($1);
1774 $$->set_location(@1);
1778 basic_type_specifier_nonarray:
1779 VOID_TOK { $$ = "void"; }
1780 | FLOAT_TOK { $$ = "float"; }
1781 | INT_TOK { $$ = "int"; }
1782 | UINT_TOK { $$ = "uint"; }
1783 | BOOL_TOK { $$ = "bool"; }
1784 | VEC2 { $$ = "vec2"; }
1785 | VEC3 { $$ = "vec3"; }
1786 | VEC4 { $$ = "vec4"; }
1787 | BVEC2 { $$ = "bvec2"; }
1788 | BVEC3 { $$ = "bvec3"; }
1789 | BVEC4 { $$ = "bvec4"; }
1790 | IVEC2 { $$ = "ivec2"; }
1791 | IVEC3 { $$ = "ivec3"; }
1792 | IVEC4 { $$ = "ivec4"; }
1793 | UVEC2 { $$ = "uvec2"; }
1794 | UVEC3 { $$ = "uvec3"; }
1795 | UVEC4 { $$ = "uvec4"; }
1796 | MAT2X2 { $$ = "mat2"; }
1797 | MAT2X3 { $$ = "mat2x3"; }
1798 | MAT2X4 { $$ = "mat2x4"; }
1799 | MAT3X2 { $$ = "mat3x2"; }
1800 | MAT3X3 { $$ = "mat3"; }
1801 | MAT3X4 { $$ = "mat3x4"; }
1802 | MAT4X2 { $$ = "mat4x2"; }
1803 | MAT4X3 { $$ = "mat4x3"; }
1804 | MAT4X4 { $$ = "mat4"; }
1805 | SAMPLER1D { $$ = "sampler1D"; }
1806 | SAMPLER2D { $$ = "sampler2D"; }
1807 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
1808 | SAMPLER3D { $$ = "sampler3D"; }
1809 | SAMPLERCUBE { $$ = "samplerCube"; }
1810 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
1811 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
1812 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
1813 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
1814 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
1815 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
1816 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
1817 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
1818 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
1819 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
1820 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
1821 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
1822 | ISAMPLER1D { $$ = "isampler1D"; }
1823 | ISAMPLER2D { $$ = "isampler2D"; }
1824 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
1825 | ISAMPLER3D { $$ = "isampler3D"; }
1826 | ISAMPLERCUBE { $$ = "isamplerCube"; }
1827 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
1828 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
1829 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
1830 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
1831 | USAMPLER1D { $$ = "usampler1D"; }
1832 | USAMPLER2D { $$ = "usampler2D"; }
1833 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
1834 | USAMPLER3D { $$ = "usampler3D"; }
1835 | USAMPLERCUBE { $$ = "usamplerCube"; }
1836 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
1837 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
1838 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
1839 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
1840 | SAMPLER2DMS { $$ = "sampler2DMS"; }
1841 | ISAMPLER2DMS { $$ = "isampler2DMS"; }
1842 | USAMPLER2DMS { $$ = "usampler2DMS"; }
1843 | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
1844 | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
1845 | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
1846 | IMAGE1D { $$ = "image1D"; }
1847 | IMAGE2D { $$ = "image2D"; }
1848 | IMAGE3D { $$ = "image3D"; }
1849 | IMAGE2DRECT { $$ = "image2DRect"; }
1850 | IMAGECUBE { $$ = "imageCube"; }
1851 | IMAGEBUFFER { $$ = "imageBuffer"; }
1852 | IMAGE1DARRAY { $$ = "image1DArray"; }
1853 | IMAGE2DARRAY { $$ = "image2DArray"; }
1854 | IMAGECUBEARRAY { $$ = "imageCubeArray"; }
1855 | IMAGE2DMS { $$ = "image2DMS"; }
1856 | IMAGE2DMSARRAY { $$ = "image2DMSArray"; }
1857 | IIMAGE1D { $$ = "iimage1D"; }
1858 | IIMAGE2D { $$ = "iimage2D"; }
1859 | IIMAGE3D { $$ = "iimage3D"; }
1860 | IIMAGE2DRECT { $$ = "iimage2DRect"; }
1861 | IIMAGECUBE { $$ = "iimageCube"; }
1862 | IIMAGEBUFFER { $$ = "iimageBuffer"; }
1863 | IIMAGE1DARRAY { $$ = "iimage1DArray"; }
1864 | IIMAGE2DARRAY { $$ = "iimage2DArray"; }
1865 | IIMAGECUBEARRAY { $$ = "iimageCubeArray"; }
1866 | IIMAGE2DMS { $$ = "iimage2DMS"; }
1867 | IIMAGE2DMSARRAY { $$ = "iimage2DMSArray"; }
1868 | UIMAGE1D { $$ = "uimage1D"; }
1869 | UIMAGE2D { $$ = "uimage2D"; }
1870 | UIMAGE3D { $$ = "uimage3D"; }
1871 | UIMAGE2DRECT { $$ = "uimage2DRect"; }
1872 | UIMAGECUBE { $$ = "uimageCube"; }
1873 | UIMAGEBUFFER { $$ = "uimageBuffer"; }
1874 | UIMAGE1DARRAY { $$ = "uimage1DArray"; }
1875 | UIMAGE2DARRAY { $$ = "uimage2DArray"; }
1876 | UIMAGECUBEARRAY { $$ = "uimageCubeArray"; }
1877 | UIMAGE2DMS { $$ = "uimage2DMS"; }
1878 | UIMAGE2DMSARRAY { $$ = "uimage2DMSArray"; }
1879 | ATOMIC_UINT { $$ = "atomic_uint"; }
1882 precision_qualifier:
1885 state->check_precision_qualifiers_allowed(&@1);
1886 $$ = ast_precision_high;
1890 state->check_precision_qualifiers_allowed(&@1);
1891 $$ = ast_precision_medium;
1895 state->check_precision_qualifiers_allowed(&@1);
1896 $$ = ast_precision_low;
1901 STRUCT any_identifier '{' struct_declaration_list '}'
1904 $$ = new(ctx) ast_struct_specifier($2, $4);
1905 $$->set_location_range(@2, @5);
1906 state->symbols->add_type($2, glsl_type::void_type);
1908 | STRUCT '{' struct_declaration_list '}'
1911 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1912 $$->set_location_range(@2, @4);
1916 struct_declaration_list:
1920 $1->link.self_link();
1922 | struct_declaration_list struct_declaration
1925 $$->link.insert_before(& $2->link);
1930 fully_specified_type struct_declarator_list ';'
1933 ast_fully_specified_type *const type = $1;
1934 type->set_location(@1);
1936 if (type->qualifier.flags.i != 0)
1937 _mesa_glsl_error(&@1, state,
1938 "only precision qualifiers may be applied to "
1939 "structure members");
1941 $$ = new(ctx) ast_declarator_list(type);
1942 $$->set_location(@2);
1944 $$->declarations.push_degenerate_list_at_head(& $2->link);
1948 struct_declarator_list:
1952 $1->link.self_link();
1954 | struct_declarator_list ',' struct_declarator
1957 $$->link.insert_before(& $3->link);
1965 $$ = new(ctx) ast_declaration($1, NULL, NULL);
1966 $$->set_location(@1);
1968 | any_identifier array_specifier
1971 $$ = new(ctx) ast_declaration($1, $2, NULL);
1972 $$->set_location_range(@1, @2);
1977 assignment_expression
1978 | '{' initializer_list '}'
1982 | '{' initializer_list ',' '}'
1992 $$ = new(ctx) ast_aggregate_initializer();
1993 $$->set_location(@1);
1994 $$->expressions.push_tail(& $1->link);
1996 | initializer_list ',' initializer
1998 $1->expressions.push_tail(& $3->link);
2002 declaration_statement:
2006 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
2009 compound_statement { $$ = (ast_node *) $1; }
2014 declaration_statement
2015 | expression_statement
2016 | selection_statement
2018 | iteration_statement
2026 $$ = new(ctx) ast_compound_statement(true, NULL);
2027 $$->set_location_range(@1, @2);
2031 state->symbols->push_scope();
2036 $$ = new(ctx) ast_compound_statement(true, $3);
2037 $$->set_location_range(@1, @4);
2038 state->symbols->pop_scope();
2042 statement_no_new_scope:
2043 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
2047 compound_statement_no_new_scope:
2051 $$ = new(ctx) ast_compound_statement(false, NULL);
2052 $$->set_location_range(@1, @2);
2054 | '{' statement_list '}'
2057 $$ = new(ctx) ast_compound_statement(false, $2);
2058 $$->set_location_range(@1, @3);
2066 _mesa_glsl_error(& @1, state, "<nil> statement");
2071 $$->link.self_link();
2073 | statement_list statement
2076 _mesa_glsl_error(& @2, state, "<nil> statement");
2080 $$->link.insert_before(& $2->link);
2084 expression_statement:
2088 $$ = new(ctx) ast_expression_statement(NULL);
2089 $$->set_location(@1);
2094 $$ = new(ctx) ast_expression_statement($1);
2095 $$->set_location(@1);
2099 selection_statement:
2100 IF '(' expression ')' selection_rest_statement
2102 $$ = new(state) ast_selection_statement($3, $5.then_statement,
2104 $$->set_location_range(@1, @5);
2108 selection_rest_statement:
2109 statement ELSE statement
2111 $$.then_statement = $1;
2112 $$.else_statement = $3;
2114 | statement %prec THEN
2116 $$.then_statement = $1;
2117 $$.else_statement = NULL;
2124 $$ = (ast_node *) $1;
2126 | fully_specified_type any_identifier '=' initializer
2129 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
2130 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
2131 decl->set_location_range(@2, @4);
2132 declarator->set_location(@1);
2134 declarator->declarations.push_tail(&decl->link);
2140 * siwtch_statement grammar is based on the syntax described in the body
2141 * of the GLSL spec, not in it's appendix!!!
2144 SWITCH '(' expression ')' switch_body
2146 $$ = new(state) ast_switch_statement($3, $5);
2147 $$->set_location_range(@1, @5);
2154 $$ = new(state) ast_switch_body(NULL);
2155 $$->set_location_range(@1, @2);
2157 | '{' case_statement_list '}'
2159 $$ = new(state) ast_switch_body($2);
2160 $$->set_location_range(@1, @3);
2167 $$ = new(state) ast_case_label($2);
2168 $$->set_location(@2);
2172 $$ = new(state) ast_case_label(NULL);
2173 $$->set_location(@2);
2180 ast_case_label_list *labels = new(state) ast_case_label_list();
2182 labels->labels.push_tail(& $1->link);
2184 $$->set_location(@1);
2186 | case_label_list case_label
2189 $$->labels.push_tail(& $2->link);
2194 case_label_list statement
2196 ast_case_statement *stmts = new(state) ast_case_statement($1);
2197 stmts->set_location(@2);
2199 stmts->stmts.push_tail(& $2->link);
2202 | case_statement statement
2205 $$->stmts.push_tail(& $2->link);
2209 case_statement_list:
2212 ast_case_statement_list *cases= new(state) ast_case_statement_list();
2213 cases->set_location(@1);
2215 cases->cases.push_tail(& $1->link);
2218 | case_statement_list case_statement
2221 $$->cases.push_tail(& $2->link);
2225 iteration_statement:
2226 WHILE '(' condition ')' statement_no_new_scope
2229 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2230 NULL, $3, NULL, $5);
2231 $$->set_location_range(@1, @4);
2233 | DO statement WHILE '(' expression ')' ';'
2236 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2237 NULL, $5, NULL, $2);
2238 $$->set_location_range(@1, @6);
2240 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2243 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2244 $3, $4.cond, $4.rest, $6);
2245 $$->set_location_range(@1, @6);
2250 expression_statement
2251 | declaration_statement
2268 | conditionopt ';' expression
2275 // Grammar Note: No 'goto'. Gotos are not supported.
2280 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2281 $$->set_location(@1);
2286 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2287 $$->set_location(@1);
2292 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2293 $$->set_location(@1);
2295 | RETURN expression ';'
2298 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2299 $$->set_location_range(@1, @2);
2301 | DISCARD ';' // Fragment shader only.
2304 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2305 $$->set_location(@1);
2309 external_declaration:
2310 function_definition { $$ = $1; }
2311 | declaration { $$ = $1; }
2312 | pragma_statement { $$ = NULL; }
2313 | layout_defaults { $$ = $1; }
2316 function_definition:
2317 function_prototype compound_statement_no_new_scope
2320 $$ = new(ctx) ast_function_definition();
2321 $$->set_location_range(@1, @2);
2325 state->symbols->pop_scope();
2329 /* layout_qualifieropt is packed into this rule */
2331 basic_interface_block
2335 | layout_qualifier basic_interface_block
2337 ast_interface_block *block = $2;
2338 if (!block->layout.merge_qualifier(& @1, state, $1)) {
2345 basic_interface_block:
2346 interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2348 ast_interface_block *const block = $6;
2350 block->block_name = $2;
2351 block->declarations.push_degenerate_list_at_head(& $4->link);
2353 if ($1.flags.q.uniform) {
2354 if (!state->has_uniform_buffer_objects()) {
2355 _mesa_glsl_error(& @1, state,
2356 "#version 140 / GL_ARB_uniform_buffer_object "
2357 "required for defining uniform blocks");
2358 } else if (state->ARB_uniform_buffer_object_warn) {
2359 _mesa_glsl_warning(& @1, state,
2360 "#version 140 / GL_ARB_uniform_buffer_object "
2361 "required for defining uniform blocks");
2364 if (state->es_shader || state->language_version < 150) {
2365 _mesa_glsl_error(& @1, state,
2366 "#version 150 required for using "
2367 "interface blocks");
2371 /* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):
2372 * "It is illegal to have an input block in a vertex shader
2373 * or an output block in a fragment shader"
2375 if ((state->stage == MESA_SHADER_VERTEX) && $1.flags.q.in) {
2376 _mesa_glsl_error(& @1, state,
2377 "`in' interface block is not allowed for "
2379 } else if ((state->stage == MESA_SHADER_FRAGMENT) && $1.flags.q.out) {
2380 _mesa_glsl_error(& @1, state,
2381 "`out' interface block is not allowed for "
2382 "a fragment shader");
2385 /* Since block arrays require names, and both features are added in
2386 * the same language versions, we don't have to explicitly
2387 * version-check both things.
2389 if (block->instance_name != NULL) {
2390 state->check_version(150, 300, & @1, "interface blocks with "
2391 "an instance name are not allowed");
2394 uint64_t interface_type_mask;
2395 struct ast_type_qualifier temp_type_qualifier;
2397 /* Get a bitmask containing only the in/out/uniform flags, allowing us
2398 * to ignore other irrelevant flags like interpolation qualifiers.
2400 temp_type_qualifier.flags.i = 0;
2401 temp_type_qualifier.flags.q.uniform = true;
2402 temp_type_qualifier.flags.q.in = true;
2403 temp_type_qualifier.flags.q.out = true;
2404 interface_type_mask = temp_type_qualifier.flags.i;
2406 /* Get the block's interface qualifier. The interface_qualifier
2407 * production rule guarantees that only one bit will be set (and
2408 * it will be in/out/uniform).
2410 uint64_t block_interface_qualifier = $1.flags.i;
2412 block->layout.flags.i |= block_interface_qualifier;
2414 foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
2415 ast_type_qualifier& qualifier = member->type->qualifier;
2416 if ((qualifier.flags.i & interface_type_mask) == 0) {
2417 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2418 * "If no optional qualifier is used in a member declaration, the
2419 * qualifier of the variable is just in, out, or uniform as declared
2420 * by interface-qualifier."
2422 qualifier.flags.i |= block_interface_qualifier;
2423 } else if ((qualifier.flags.i & interface_type_mask) !=
2424 block_interface_qualifier) {
2425 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2426 * "If optional qualifiers are used, they can include interpolation
2427 * and storage qualifiers and they must declare an input, output,
2428 * or uniform variable consistent with the interface qualifier of
2431 _mesa_glsl_error(& @1, state,
2432 "uniform/in/out qualifier on "
2433 "interface block member does not match "
2434 "the interface block");
2442 interface_qualifier:
2445 memset(& $$, 0, sizeof($$));
2450 memset(& $$, 0, sizeof($$));
2455 memset(& $$, 0, sizeof($$));
2456 $$.flags.q.uniform = 1;
2463 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2468 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2470 $$->set_location(@1);
2472 | NEW_IDENTIFIER array_specifier
2474 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2476 $$->set_location_range(@1, @2);
2484 $1->link.self_link();
2486 | member_declaration member_list
2489 $2->link.insert_before(& $$->link);
2494 fully_specified_type struct_declarator_list ';'
2497 ast_fully_specified_type *type = $1;
2498 type->set_location(@1);
2500 if (type->qualifier.flags.q.attribute) {
2501 _mesa_glsl_error(& @1, state,
2502 "keyword 'attribute' cannot be used with "
2503 "interface block member");
2504 } else if (type->qualifier.flags.q.varying) {
2505 _mesa_glsl_error(& @1, state,
2506 "keyword 'varying' cannot be used with "
2507 "interface block member");
2510 $$ = new(ctx) ast_declarator_list(type);
2511 $$->set_location(@2);
2513 $$->declarations.push_degenerate_list_at_head(& $2->link);
2518 layout_qualifier UNIFORM ';'
2520 if (!state->default_uniform_qualifier->merge_qualifier(& @1, state, $1)) {
2526 | layout_qualifier IN_TOK ';'
2529 if (!state->in_qualifier->merge_in_qualifier(& @1, state, $1, $$)) {
2534 | layout_qualifier OUT_TOK ';'
2536 if (state->stage != MESA_SHADER_GEOMETRY) {
2537 _mesa_glsl_error(& @1, state,
2538 "out layout qualifiers only valid in "
2539 "geometry shaders");
2541 if ($1.flags.q.prim_type) {
2542 /* Make sure this is a valid output primitive type. */
2543 switch ($1.prim_type) {
2546 case GL_TRIANGLE_STRIP:
2549 _mesa_glsl_error(&@1, state, "invalid geometry shader output "
2554 if (!state->out_qualifier->merge_qualifier(& @1, state, $1))