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. */
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;
1325 _mesa_glsl_error(& @3, state, "invalid location %d specified", $3);
1330 if (match_layout_qualifier("index", $1, state) == 0) {
1331 $$.flags.q.explicit_index = 1;
1336 _mesa_glsl_error(& @3, state, "invalid index %d specified", $3);
1341 if ((state->ARB_shading_language_420pack_enable ||
1342 state->ARB_shader_atomic_counters_enable) &&
1343 match_layout_qualifier("binding", $1, state) == 0) {
1344 $$.flags.q.explicit_binding = 1;
1348 if (state->ARB_shader_atomic_counters_enable &&
1349 match_layout_qualifier("offset", $1, state) == 0) {
1350 $$.flags.q.explicit_offset = 1;
1354 if (match_layout_qualifier("max_vertices", $1, state) == 0) {
1355 $$.flags.q.max_vertices = 1;
1358 _mesa_glsl_error(& @3, state,
1359 "invalid max_vertices %d specified", $3);
1362 $$.max_vertices = $3;
1363 if (!state->is_version(150, 0)) {
1364 _mesa_glsl_error(& @3, state,
1365 "#version 150 max_vertices qualifier "
1371 static const char *local_size_qualifiers[3] = {
1376 for (int i = 0; i < 3; i++) {
1377 if (match_layout_qualifier(local_size_qualifiers[i], $1,
1380 _mesa_glsl_error(& @3, state,
1381 "invalid %s of %d specified",
1382 local_size_qualifiers[i], $3);
1384 } else if (!state->is_version(430, 0) &&
1385 !state->ARB_compute_shader_enable) {
1386 _mesa_glsl_error(& @3, state,
1387 "%s qualifier requires GLSL 4.30 or "
1388 "ARB_compute_shader",
1389 local_size_qualifiers[i]);
1392 $$.flags.q.local_size |= (1 << i);
1393 $$.local_size[i] = $3;
1399 if (match_layout_qualifier("invocations", $1, state) == 0) {
1400 $$.flags.q.invocations = 1;
1403 _mesa_glsl_error(& @3, state,
1404 "invalid invocations %d specified", $3);
1406 } else if ($3 > MAX_GEOMETRY_SHADER_INVOCATIONS) {
1407 _mesa_glsl_error(& @3, state,
1408 "invocations (%d) exceeds "
1409 "GL_MAX_GEOMETRY_SHADER_INVOCATIONS", $3);
1412 $$.invocations = $3;
1413 if (!state->is_version(400, 0) &&
1414 !state->ARB_gpu_shader5_enable) {
1415 _mesa_glsl_error(& @3, state,
1416 "GL_ARB_gpu_shader5 invocations "
1417 "qualifier specified", $3);
1422 /* If the identifier didn't match any known layout identifiers,
1426 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1429 } else if (state->ARB_explicit_attrib_location_warn) {
1430 _mesa_glsl_warning(& @1, state,
1431 "GL_ARB_explicit_attrib_location layout "
1432 "identifier `%s' used", $1);
1435 | interface_block_layout_qualifier
1438 /* Layout qualifiers for ARB_uniform_buffer_object. */
1439 if ($$.flags.q.uniform && !state->has_uniform_buffer_objects()) {
1440 _mesa_glsl_error(& @1, state,
1441 "#version 140 / GL_ARB_uniform_buffer_object "
1442 "layout qualifier `%s' is used", $1);
1443 } else if ($$.flags.q.uniform && state->ARB_uniform_buffer_object_warn) {
1444 _mesa_glsl_warning(& @1, state,
1445 "#version 140 / GL_ARB_uniform_buffer_object "
1446 "layout qualifier `%s' is used", $1);
1451 /* This is a separate language rule because we parse these as tokens
1452 * (due to them being reserved keywords) instead of identifiers like
1453 * most qualifiers. See the any_identifier path of
1454 * layout_qualifier_id for the others.
1456 * Note that since layout qualifiers are case-insensitive in desktop
1457 * GLSL, all of these qualifiers need to be handled as identifiers as
1458 * well (by the any_identifier path of layout_qualifier_id).
1460 interface_block_layout_qualifier:
1463 memset(& $$, 0, sizeof($$));
1464 $$.flags.q.row_major = 1;
1468 memset(& $$, 0, sizeof($$));
1469 $$.flags.q.packed = 1;
1473 interpolation_qualifier:
1476 memset(& $$, 0, sizeof($$));
1477 $$.flags.q.smooth = 1;
1481 memset(& $$, 0, sizeof($$));
1482 $$.flags.q.flat = 1;
1486 memset(& $$, 0, sizeof($$));
1487 $$.flags.q.noperspective = 1;
1492 /* Single qualifiers */
1495 memset(& $$, 0, sizeof($$));
1496 $$.flags.q.invariant = 1;
1498 | auxiliary_storage_qualifier
1500 | interpolation_qualifier
1502 | precision_qualifier
1504 memset(&$$, 0, sizeof($$));
1508 /* Multiple qualifiers:
1509 * In GLSL 4.20, these can be specified in any order. In earlier versions,
1510 * they appear in this order (see GLSL 1.50 section 4.7 & comments below):
1512 * invariant interpolation auxiliary storage precision ...or...
1513 * layout storage precision
1515 * Each qualifier's rule ensures that the accumulated qualifiers on the right
1516 * side don't contain any that must appear on the left hand side.
1517 * For example, when processing a storage qualifier, we check that there are
1518 * no auxiliary, interpolation, layout, or invariant qualifiers to the right.
1520 | INVARIANT type_qualifier
1522 if ($2.flags.q.invariant)
1523 _mesa_glsl_error(&@1, state, "duplicate \"invariant\" qualifier");
1525 if ($2.has_layout()) {
1526 _mesa_glsl_error(&@1, state,
1527 "\"invariant\" cannot be used with layout(...)");
1531 $$.flags.q.invariant = 1;
1533 | interpolation_qualifier type_qualifier
1535 /* Section 4.3 of the GLSL 1.40 specification states:
1536 * "...qualified with one of these interpolation qualifiers"
1538 * GLSL 1.30 claims to allow "one or more", but insists that:
1539 * "These interpolation qualifiers may only precede the qualifiers in,
1540 * centroid in, out, or centroid out in a declaration."
1542 * ...which means that e.g. smooth can't precede smooth, so there can be
1543 * only one after all, and the 1.40 text is a clarification, not a change.
1545 if ($2.has_interpolation())
1546 _mesa_glsl_error(&@1, state, "duplicate interpolation qualifier");
1548 if ($2.has_layout()) {
1549 _mesa_glsl_error(&@1, state, "interpolation qualifiers cannot be used "
1550 "with layout(...)");
1553 if (!state->ARB_shading_language_420pack_enable && $2.flags.q.invariant) {
1554 _mesa_glsl_error(&@1, state, "interpolation qualifiers must come "
1555 "after \"invariant\"");
1559 $$.merge_qualifier(&@1, state, $2);
1561 | layout_qualifier type_qualifier
1563 /* The GLSL 1.50 grammar indicates that a layout(...) declaration can be
1564 * used standalone or immediately before a storage qualifier. It cannot
1565 * be used with interpolation qualifiers or invariant. There does not
1566 * appear to be any text indicating that it must come before the storage
1567 * qualifier, but always seems to in examples.
1569 if (!state->ARB_shading_language_420pack_enable && $2.has_layout())
1570 _mesa_glsl_error(&@1, state, "duplicate layout(...) qualifiers");
1572 if ($2.flags.q.invariant)
1573 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1574 "the \"invariant\" qualifier");
1576 if ($2.has_interpolation()) {
1577 _mesa_glsl_error(&@1, state, "layout(...) cannot be used with "
1578 "interpolation qualifiers");
1582 $$.merge_qualifier(&@1, state, $2);
1584 | auxiliary_storage_qualifier type_qualifier
1586 if ($2.has_auxiliary_storage()) {
1587 _mesa_glsl_error(&@1, state,
1588 "duplicate auxiliary storage qualifier (centroid or sample)");
1591 if (!state->ARB_shading_language_420pack_enable &&
1592 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout())) {
1593 _mesa_glsl_error(&@1, state, "auxiliary storage qualifiers must come "
1594 "just before storage qualifiers");
1597 $$.merge_qualifier(&@1, state, $2);
1599 | storage_qualifier type_qualifier
1601 /* Section 4.3 of the GLSL 1.20 specification states:
1602 * "Variable declarations may have a storage qualifier specified..."
1603 * 1.30 clarifies this to "may have one storage qualifier".
1605 if ($2.has_storage())
1606 _mesa_glsl_error(&@1, state, "duplicate storage qualifier");
1608 if (!state->ARB_shading_language_420pack_enable &&
1609 ($2.flags.q.invariant || $2.has_interpolation() || $2.has_layout() ||
1610 $2.has_auxiliary_storage())) {
1611 _mesa_glsl_error(&@1, state, "storage qualifiers must come after "
1612 "invariant, interpolation, layout and auxiliary "
1613 "storage qualifiers");
1617 $$.merge_qualifier(&@1, state, $2);
1619 | precision_qualifier type_qualifier
1621 if ($2.precision != ast_precision_none)
1622 _mesa_glsl_error(&@1, state, "duplicate precision qualifier");
1624 if (!state->ARB_shading_language_420pack_enable && $2.flags.i != 0)
1625 _mesa_glsl_error(&@1, state, "precision qualifiers must come last");
1632 auxiliary_storage_qualifier:
1635 memset(& $$, 0, sizeof($$));
1636 $$.flags.q.centroid = 1;
1640 memset(& $$, 0, sizeof($$));
1641 $$.flags.q.sample = 1;
1643 /* TODO: "patch" also goes here someday. */
1648 memset(& $$, 0, sizeof($$));
1649 $$.flags.q.constant = 1;
1653 memset(& $$, 0, sizeof($$));
1654 $$.flags.q.attribute = 1;
1658 memset(& $$, 0, sizeof($$));
1659 $$.flags.q.varying = 1;
1663 memset(& $$, 0, sizeof($$));
1668 memset(& $$, 0, sizeof($$));
1673 memset(& $$, 0, sizeof($$));
1674 $$.flags.q.uniform = 1;
1678 memset(& $$, 0, sizeof($$));
1679 $$.flags.q.coherent = 1;
1683 memset(& $$, 0, sizeof($$));
1684 $$.flags.q._volatile = 1;
1688 STATIC_ASSERT(sizeof($$.flags.q) <= sizeof($$.flags.i));
1689 memset(& $$, 0, sizeof($$));
1690 $$.flags.q.restrict_flag = 1;
1694 memset(& $$, 0, sizeof($$));
1695 $$.flags.q.read_only = 1;
1699 memset(& $$, 0, sizeof($$));
1700 $$.flags.q.write_only = 1;
1708 $$ = new(ctx) ast_array_specifier(@1);
1709 $$->set_location_range(@1, @2);
1711 | '[' constant_expression ']'
1714 $$ = new(ctx) ast_array_specifier(@1, $2);
1715 $$->set_location_range(@1, @3);
1717 | array_specifier '[' ']'
1721 if (!state->ARB_arrays_of_arrays_enable) {
1722 _mesa_glsl_error(& @1, state,
1723 "GL_ARB_arrays_of_arrays "
1724 "required for defining arrays of arrays");
1726 _mesa_glsl_error(& @1, state,
1727 "only the outermost array dimension can "
1731 | array_specifier '[' constant_expression ']'
1735 if (!state->ARB_arrays_of_arrays_enable) {
1736 _mesa_glsl_error(& @1, state,
1737 "GL_ARB_arrays_of_arrays "
1738 "required for defining arrays of arrays");
1741 $$->add_dimension($3);
1746 type_specifier_nonarray
1747 | type_specifier_nonarray array_specifier
1750 $$->array_specifier = $2;
1754 type_specifier_nonarray:
1755 basic_type_specifier_nonarray
1758 $$ = new(ctx) ast_type_specifier($1);
1759 $$->set_location(@1);
1764 $$ = new(ctx) ast_type_specifier($1);
1765 $$->set_location(@1);
1770 $$ = new(ctx) ast_type_specifier($1);
1771 $$->set_location(@1);
1775 basic_type_specifier_nonarray:
1776 VOID_TOK { $$ = "void"; }
1777 | FLOAT_TOK { $$ = "float"; }
1778 | INT_TOK { $$ = "int"; }
1779 | UINT_TOK { $$ = "uint"; }
1780 | BOOL_TOK { $$ = "bool"; }
1781 | VEC2 { $$ = "vec2"; }
1782 | VEC3 { $$ = "vec3"; }
1783 | VEC4 { $$ = "vec4"; }
1784 | BVEC2 { $$ = "bvec2"; }
1785 | BVEC3 { $$ = "bvec3"; }
1786 | BVEC4 { $$ = "bvec4"; }
1787 | IVEC2 { $$ = "ivec2"; }
1788 | IVEC3 { $$ = "ivec3"; }
1789 | IVEC4 { $$ = "ivec4"; }
1790 | UVEC2 { $$ = "uvec2"; }
1791 | UVEC3 { $$ = "uvec3"; }
1792 | UVEC4 { $$ = "uvec4"; }
1793 | MAT2X2 { $$ = "mat2"; }
1794 | MAT2X3 { $$ = "mat2x3"; }
1795 | MAT2X4 { $$ = "mat2x4"; }
1796 | MAT3X2 { $$ = "mat3x2"; }
1797 | MAT3X3 { $$ = "mat3"; }
1798 | MAT3X4 { $$ = "mat3x4"; }
1799 | MAT4X2 { $$ = "mat4x2"; }
1800 | MAT4X3 { $$ = "mat4x3"; }
1801 | MAT4X4 { $$ = "mat4"; }
1802 | SAMPLER1D { $$ = "sampler1D"; }
1803 | SAMPLER2D { $$ = "sampler2D"; }
1804 | SAMPLER2DRECT { $$ = "sampler2DRect"; }
1805 | SAMPLER3D { $$ = "sampler3D"; }
1806 | SAMPLERCUBE { $$ = "samplerCube"; }
1807 | SAMPLEREXTERNALOES { $$ = "samplerExternalOES"; }
1808 | SAMPLER1DSHADOW { $$ = "sampler1DShadow"; }
1809 | SAMPLER2DSHADOW { $$ = "sampler2DShadow"; }
1810 | SAMPLER2DRECTSHADOW { $$ = "sampler2DRectShadow"; }
1811 | SAMPLERCUBESHADOW { $$ = "samplerCubeShadow"; }
1812 | SAMPLER1DARRAY { $$ = "sampler1DArray"; }
1813 | SAMPLER2DARRAY { $$ = "sampler2DArray"; }
1814 | SAMPLER1DARRAYSHADOW { $$ = "sampler1DArrayShadow"; }
1815 | SAMPLER2DARRAYSHADOW { $$ = "sampler2DArrayShadow"; }
1816 | SAMPLERBUFFER { $$ = "samplerBuffer"; }
1817 | SAMPLERCUBEARRAY { $$ = "samplerCubeArray"; }
1818 | SAMPLERCUBEARRAYSHADOW { $$ = "samplerCubeArrayShadow"; }
1819 | ISAMPLER1D { $$ = "isampler1D"; }
1820 | ISAMPLER2D { $$ = "isampler2D"; }
1821 | ISAMPLER2DRECT { $$ = "isampler2DRect"; }
1822 | ISAMPLER3D { $$ = "isampler3D"; }
1823 | ISAMPLERCUBE { $$ = "isamplerCube"; }
1824 | ISAMPLER1DARRAY { $$ = "isampler1DArray"; }
1825 | ISAMPLER2DARRAY { $$ = "isampler2DArray"; }
1826 | ISAMPLERBUFFER { $$ = "isamplerBuffer"; }
1827 | ISAMPLERCUBEARRAY { $$ = "isamplerCubeArray"; }
1828 | USAMPLER1D { $$ = "usampler1D"; }
1829 | USAMPLER2D { $$ = "usampler2D"; }
1830 | USAMPLER2DRECT { $$ = "usampler2DRect"; }
1831 | USAMPLER3D { $$ = "usampler3D"; }
1832 | USAMPLERCUBE { $$ = "usamplerCube"; }
1833 | USAMPLER1DARRAY { $$ = "usampler1DArray"; }
1834 | USAMPLER2DARRAY { $$ = "usampler2DArray"; }
1835 | USAMPLERBUFFER { $$ = "usamplerBuffer"; }
1836 | USAMPLERCUBEARRAY { $$ = "usamplerCubeArray"; }
1837 | SAMPLER2DMS { $$ = "sampler2DMS"; }
1838 | ISAMPLER2DMS { $$ = "isampler2DMS"; }
1839 | USAMPLER2DMS { $$ = "usampler2DMS"; }
1840 | SAMPLER2DMSARRAY { $$ = "sampler2DMSArray"; }
1841 | ISAMPLER2DMSARRAY { $$ = "isampler2DMSArray"; }
1842 | USAMPLER2DMSARRAY { $$ = "usampler2DMSArray"; }
1843 | IMAGE1D { $$ = "image1D"; }
1844 | IMAGE2D { $$ = "image2D"; }
1845 | IMAGE3D { $$ = "image3D"; }
1846 | IMAGE2DRECT { $$ = "image2DRect"; }
1847 | IMAGECUBE { $$ = "imageCube"; }
1848 | IMAGEBUFFER { $$ = "imageBuffer"; }
1849 | IMAGE1DARRAY { $$ = "image1DArray"; }
1850 | IMAGE2DARRAY { $$ = "image2DArray"; }
1851 | IMAGECUBEARRAY { $$ = "imageCubeArray"; }
1852 | IMAGE2DMS { $$ = "image2DMS"; }
1853 | IMAGE2DMSARRAY { $$ = "image2DMSArray"; }
1854 | IIMAGE1D { $$ = "iimage1D"; }
1855 | IIMAGE2D { $$ = "iimage2D"; }
1856 | IIMAGE3D { $$ = "iimage3D"; }
1857 | IIMAGE2DRECT { $$ = "iimage2DRect"; }
1858 | IIMAGECUBE { $$ = "iimageCube"; }
1859 | IIMAGEBUFFER { $$ = "iimageBuffer"; }
1860 | IIMAGE1DARRAY { $$ = "iimage1DArray"; }
1861 | IIMAGE2DARRAY { $$ = "iimage2DArray"; }
1862 | IIMAGECUBEARRAY { $$ = "iimageCubeArray"; }
1863 | IIMAGE2DMS { $$ = "iimage2DMS"; }
1864 | IIMAGE2DMSARRAY { $$ = "iimage2DMSArray"; }
1865 | UIMAGE1D { $$ = "uimage1D"; }
1866 | UIMAGE2D { $$ = "uimage2D"; }
1867 | UIMAGE3D { $$ = "uimage3D"; }
1868 | UIMAGE2DRECT { $$ = "uimage2DRect"; }
1869 | UIMAGECUBE { $$ = "uimageCube"; }
1870 | UIMAGEBUFFER { $$ = "uimageBuffer"; }
1871 | UIMAGE1DARRAY { $$ = "uimage1DArray"; }
1872 | UIMAGE2DARRAY { $$ = "uimage2DArray"; }
1873 | UIMAGECUBEARRAY { $$ = "uimageCubeArray"; }
1874 | UIMAGE2DMS { $$ = "uimage2DMS"; }
1875 | UIMAGE2DMSARRAY { $$ = "uimage2DMSArray"; }
1876 | ATOMIC_UINT { $$ = "atomic_uint"; }
1879 precision_qualifier:
1882 state->check_precision_qualifiers_allowed(&@1);
1883 $$ = ast_precision_high;
1887 state->check_precision_qualifiers_allowed(&@1);
1888 $$ = ast_precision_medium;
1892 state->check_precision_qualifiers_allowed(&@1);
1893 $$ = ast_precision_low;
1898 STRUCT any_identifier '{' struct_declaration_list '}'
1901 $$ = new(ctx) ast_struct_specifier($2, $4);
1902 $$->set_location_range(@2, @5);
1903 state->symbols->add_type($2, glsl_type::void_type);
1904 state->symbols->add_type_ast($2, new(ctx) ast_type_specifier($$));
1906 | STRUCT '{' struct_declaration_list '}'
1909 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1910 $$->set_location_range(@2, @4);
1914 struct_declaration_list:
1918 $1->link.self_link();
1920 | struct_declaration_list struct_declaration
1923 $$->link.insert_before(& $2->link);
1928 fully_specified_type struct_declarator_list ';'
1931 ast_fully_specified_type *const type = $1;
1932 type->set_location(@1);
1934 if (type->qualifier.flags.i != 0)
1935 _mesa_glsl_error(&@1, state,
1936 "only precision qualifiers may be applied to "
1937 "structure members");
1939 $$ = new(ctx) ast_declarator_list(type);
1940 $$->set_location(@2);
1942 $$->declarations.push_degenerate_list_at_head(& $2->link);
1946 struct_declarator_list:
1950 $1->link.self_link();
1952 | struct_declarator_list ',' struct_declarator
1955 $$->link.insert_before(& $3->link);
1963 $$ = new(ctx) ast_declaration($1, NULL, NULL);
1964 $$->set_location(@1);
1966 | any_identifier array_specifier
1969 $$ = new(ctx) ast_declaration($1, $2, NULL);
1970 $$->set_location_range(@1, @2);
1975 assignment_expression
1976 | '{' initializer_list '}'
1980 | '{' initializer_list ',' '}'
1990 $$ = new(ctx) ast_aggregate_initializer();
1991 $$->set_location(@1);
1992 $$->expressions.push_tail(& $1->link);
1994 | initializer_list ',' initializer
1996 $1->expressions.push_tail(& $3->link);
2000 declaration_statement:
2004 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
2007 compound_statement { $$ = (ast_node *) $1; }
2012 declaration_statement
2013 | expression_statement
2014 | selection_statement
2016 | iteration_statement
2024 $$ = new(ctx) ast_compound_statement(true, NULL);
2025 $$->set_location_range(@1, @2);
2029 state->symbols->push_scope();
2034 $$ = new(ctx) ast_compound_statement(true, $3);
2035 $$->set_location_range(@1, @4);
2036 state->symbols->pop_scope();
2040 statement_no_new_scope:
2041 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
2045 compound_statement_no_new_scope:
2049 $$ = new(ctx) ast_compound_statement(false, NULL);
2050 $$->set_location_range(@1, @2);
2052 | '{' statement_list '}'
2055 $$ = new(ctx) ast_compound_statement(false, $2);
2056 $$->set_location_range(@1, @3);
2064 _mesa_glsl_error(& @1, state, "<nil> statement");
2069 $$->link.self_link();
2071 | statement_list statement
2074 _mesa_glsl_error(& @2, state, "<nil> statement");
2078 $$->link.insert_before(& $2->link);
2082 expression_statement:
2086 $$ = new(ctx) ast_expression_statement(NULL);
2087 $$->set_location(@1);
2092 $$ = new(ctx) ast_expression_statement($1);
2093 $$->set_location(@1);
2097 selection_statement:
2098 IF '(' expression ')' selection_rest_statement
2100 $$ = new(state) ast_selection_statement($3, $5.then_statement,
2102 $$->set_location_range(@1, @5);
2106 selection_rest_statement:
2107 statement ELSE statement
2109 $$.then_statement = $1;
2110 $$.else_statement = $3;
2112 | statement %prec THEN
2114 $$.then_statement = $1;
2115 $$.else_statement = NULL;
2122 $$ = (ast_node *) $1;
2124 | fully_specified_type any_identifier '=' initializer
2127 ast_declaration *decl = new(ctx) ast_declaration($2, NULL, $4);
2128 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
2129 decl->set_location_range(@2, @4);
2130 declarator->set_location(@1);
2132 declarator->declarations.push_tail(&decl->link);
2138 * siwtch_statement grammar is based on the syntax described in the body
2139 * of the GLSL spec, not in it's appendix!!!
2142 SWITCH '(' expression ')' switch_body
2144 $$ = new(state) ast_switch_statement($3, $5);
2145 $$->set_location_range(@1, @5);
2152 $$ = new(state) ast_switch_body(NULL);
2153 $$->set_location_range(@1, @2);
2155 | '{' case_statement_list '}'
2157 $$ = new(state) ast_switch_body($2);
2158 $$->set_location_range(@1, @3);
2165 $$ = new(state) ast_case_label($2);
2166 $$->set_location(@2);
2170 $$ = new(state) ast_case_label(NULL);
2171 $$->set_location(@2);
2178 ast_case_label_list *labels = new(state) ast_case_label_list();
2180 labels->labels.push_tail(& $1->link);
2182 $$->set_location(@1);
2184 | case_label_list case_label
2187 $$->labels.push_tail(& $2->link);
2192 case_label_list statement
2194 ast_case_statement *stmts = new(state) ast_case_statement($1);
2195 stmts->set_location(@2);
2197 stmts->stmts.push_tail(& $2->link);
2200 | case_statement statement
2203 $$->stmts.push_tail(& $2->link);
2207 case_statement_list:
2210 ast_case_statement_list *cases= new(state) ast_case_statement_list();
2211 cases->set_location(@1);
2213 cases->cases.push_tail(& $1->link);
2216 | case_statement_list case_statement
2219 $$->cases.push_tail(& $2->link);
2223 iteration_statement:
2224 WHILE '(' condition ')' statement_no_new_scope
2227 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
2228 NULL, $3, NULL, $5);
2229 $$->set_location_range(@1, @4);
2231 | DO statement WHILE '(' expression ')' ';'
2234 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
2235 NULL, $5, NULL, $2);
2236 $$->set_location_range(@1, @6);
2238 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
2241 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
2242 $3, $4.cond, $4.rest, $6);
2243 $$->set_location_range(@1, @6);
2248 expression_statement
2249 | declaration_statement
2266 | conditionopt ';' expression
2273 // Grammar Note: No 'goto'. Gotos are not supported.
2278 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
2279 $$->set_location(@1);
2284 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
2285 $$->set_location(@1);
2290 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
2291 $$->set_location(@1);
2293 | RETURN expression ';'
2296 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
2297 $$->set_location_range(@1, @2);
2299 | DISCARD ';' // Fragment shader only.
2302 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
2303 $$->set_location(@1);
2307 external_declaration:
2308 function_definition { $$ = $1; }
2309 | declaration { $$ = $1; }
2310 | pragma_statement { $$ = NULL; }
2311 | layout_defaults { $$ = $1; }
2314 function_definition:
2315 function_prototype compound_statement_no_new_scope
2318 $$ = new(ctx) ast_function_definition();
2319 $$->set_location_range(@1, @2);
2323 state->symbols->pop_scope();
2327 /* layout_qualifieropt is packed into this rule */
2329 basic_interface_block
2333 | layout_qualifier basic_interface_block
2335 ast_interface_block *block = $2;
2336 if (!block->layout.merge_qualifier(& @1, state, $1)) {
2343 basic_interface_block:
2344 interface_qualifier NEW_IDENTIFIER '{' member_list '}' instance_name_opt ';'
2346 ast_interface_block *const block = $6;
2348 block->block_name = $2;
2349 block->declarations.push_degenerate_list_at_head(& $4->link);
2351 if ($1.flags.q.uniform) {
2352 if (!state->has_uniform_buffer_objects()) {
2353 _mesa_glsl_error(& @1, state,
2354 "#version 140 / GL_ARB_uniform_buffer_object "
2355 "required for defining uniform blocks");
2356 } else if (state->ARB_uniform_buffer_object_warn) {
2357 _mesa_glsl_warning(& @1, state,
2358 "#version 140 / GL_ARB_uniform_buffer_object "
2359 "required for defining uniform blocks");
2362 if (state->es_shader || state->language_version < 150) {
2363 _mesa_glsl_error(& @1, state,
2364 "#version 150 required for using "
2365 "interface blocks");
2369 /* From the GLSL 1.50.11 spec, section 4.3.7 ("Interface Blocks"):
2370 * "It is illegal to have an input block in a vertex shader
2371 * or an output block in a fragment shader"
2373 if ((state->stage == MESA_SHADER_VERTEX) && $1.flags.q.in) {
2374 _mesa_glsl_error(& @1, state,
2375 "`in' interface block is not allowed for "
2377 } else if ((state->stage == MESA_SHADER_FRAGMENT) && $1.flags.q.out) {
2378 _mesa_glsl_error(& @1, state,
2379 "`out' interface block is not allowed for "
2380 "a fragment shader");
2383 /* Since block arrays require names, and both features are added in
2384 * the same language versions, we don't have to explicitly
2385 * version-check both things.
2387 if (block->instance_name != NULL) {
2388 state->check_version(150, 300, & @1, "interface blocks with "
2389 "an instance name are not allowed");
2392 uint64_t interface_type_mask;
2393 struct ast_type_qualifier temp_type_qualifier;
2395 /* Get a bitmask containing only the in/out/uniform flags, allowing us
2396 * to ignore other irrelevant flags like interpolation qualifiers.
2398 temp_type_qualifier.flags.i = 0;
2399 temp_type_qualifier.flags.q.uniform = true;
2400 temp_type_qualifier.flags.q.in = true;
2401 temp_type_qualifier.flags.q.out = true;
2402 interface_type_mask = temp_type_qualifier.flags.i;
2404 /* Get the block's interface qualifier. The interface_qualifier
2405 * production rule guarantees that only one bit will be set (and
2406 * it will be in/out/uniform).
2408 uint64_t block_interface_qualifier = $1.flags.i;
2410 block->layout.flags.i |= block_interface_qualifier;
2412 foreach_list_typed (ast_declarator_list, member, link, &block->declarations) {
2413 ast_type_qualifier& qualifier = member->type->qualifier;
2414 if ((qualifier.flags.i & interface_type_mask) == 0) {
2415 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2416 * "If no optional qualifier is used in a member declaration, the
2417 * qualifier of the variable is just in, out, or uniform as declared
2418 * by interface-qualifier."
2420 qualifier.flags.i |= block_interface_qualifier;
2421 } else if ((qualifier.flags.i & interface_type_mask) !=
2422 block_interface_qualifier) {
2423 /* GLSLangSpec.1.50.11, 4.3.7 (Interface Blocks):
2424 * "If optional qualifiers are used, they can include interpolation
2425 * and storage qualifiers and they must declare an input, output,
2426 * or uniform variable consistent with the interface qualifier of
2429 _mesa_glsl_error(& @1, state,
2430 "uniform/in/out qualifier on "
2431 "interface block member does not match "
2432 "the interface block");
2440 interface_qualifier:
2443 memset(& $$, 0, sizeof($$));
2448 memset(& $$, 0, sizeof($$));
2453 memset(& $$, 0, sizeof($$));
2454 $$.flags.q.uniform = 1;
2461 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2466 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2468 $$->set_location(@1);
2470 | NEW_IDENTIFIER array_specifier
2472 $$ = new(state) ast_interface_block(*state->default_uniform_qualifier,
2474 $$->set_location_range(@1, @2);
2482 $1->link.self_link();
2484 | member_declaration member_list
2487 $2->link.insert_before(& $$->link);
2492 fully_specified_type struct_declarator_list ';'
2495 ast_fully_specified_type *type = $1;
2496 type->set_location(@1);
2498 if (type->qualifier.flags.q.attribute) {
2499 _mesa_glsl_error(& @1, state,
2500 "keyword 'attribute' cannot be used with "
2501 "interface block member");
2502 } else if (type->qualifier.flags.q.varying) {
2503 _mesa_glsl_error(& @1, state,
2504 "keyword 'varying' cannot be used with "
2505 "interface block member");
2508 $$ = new(ctx) ast_declarator_list(type);
2509 $$->set_location(@2);
2511 $$->declarations.push_degenerate_list_at_head(& $2->link);
2516 layout_qualifier UNIFORM ';'
2518 if (!state->default_uniform_qualifier->merge_qualifier(& @1, state, $1)) {
2524 | layout_qualifier IN_TOK ';'
2527 if (!state->in_qualifier->merge_in_qualifier(& @1, state, $1, $$)) {
2532 | layout_qualifier OUT_TOK ';'
2534 if (state->stage != MESA_SHADER_GEOMETRY) {
2535 _mesa_glsl_error(& @1, state,
2536 "out layout qualifiers only valid in "
2537 "geometry shaders");
2539 if ($1.flags.q.prim_type) {
2540 /* Make sure this is a valid output primitive type. */
2541 switch ($1.prim_type) {
2544 case GL_TRIANGLE_STRIP:
2547 _mesa_glsl_error(&@1, state, "invalid geometry shader output "
2552 if (!state->out_qualifier->merge_qualifier(& @1, state, $1))