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"
33 #define YYLEX_PARAM state->scanner
49 %lex-param {void *scanner}
50 %parse-param {struct _mesa_glsl_parse_state *state}
57 struct ast_type_qualifier type_qualifier;
60 ast_type_specifier *type_specifier;
61 ast_fully_specified_type *fully_specified_type;
62 ast_function *function;
63 ast_parameter_declarator *parameter_declarator;
64 ast_function_definition *function_definition;
65 ast_compound_statement *compound_statement;
66 ast_expression *expression;
67 ast_declarator_list *declarator_list;
68 ast_struct_specifier *struct_specifier;
69 ast_declaration *declaration;
70 ast_switch_body *switch_body;
71 ast_case_label *case_label;
72 ast_case_label_list *case_label_list;
73 ast_case_statement *case_statement;
74 ast_case_statement_list *case_statement_list;
82 ast_node *then_statement;
83 ast_node *else_statement;
84 } selection_rest_statement;
87 %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK
88 %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
89 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
90 %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING
91 %token NOPERSPECTIVE FLAT SMOOTH
92 %token MAT2X2 MAT2X3 MAT2X4
93 %token MAT3X2 MAT3X3 MAT3X4
94 %token MAT4X2 MAT4X3 MAT4X4
95 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
96 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
97 %token SAMPLER2DARRAYSHADOW ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
98 %token ISAMPLER1DARRAY ISAMPLER2DARRAY USAMPLER1D USAMPLER2D USAMPLER3D
99 %token USAMPLERCUBE USAMPLER1DARRAY USAMPLER2DARRAY
100 %token SAMPLEREXTERNALOES
101 %token STRUCT VOID_TOK WHILE
102 %token <identifier> IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
103 %type <identifier> any_identifier
104 %token <real> FLOATCONSTANT
105 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
106 %token <identifier> FIELD_SELECTION
107 %token LEFT_OP RIGHT_OP
108 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
109 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
110 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
113 %token LOWP MEDIUMP HIGHP SUPERP PRECISION
115 %token VERSION EXTENSION LINE COLON EOL INTERFACE OUTPUT
116 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
117 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
118 %token PRAGMA_INVARIANT_ALL
121 /* Reserved words that are not actually used in the grammar.
123 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
124 %token INLINE_TOK NOINLINE VOLATILE PUBLIC_TOK STATIC EXTERN EXTERNAL
125 %token LONG_TOK SHORT_TOK DOUBLE_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK OUPTUT
126 %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
127 %token SAMPLER2DRECT SAMPLER3DRECT SAMPLER2DRECTSHADOW
128 %token SIZEOF CAST NAMESPACE USING
132 %token COMMON PARTITION ACTIVE SAMPLERBUFFER FILTER
133 %token IMAGE1D IMAGE2D IMAGE3D IMAGECUBE IMAGE1DARRAY IMAGE2DARRAY
134 %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGECUBE IIMAGE1DARRAY IIMAGE2DARRAY
135 %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGECUBE UIMAGE1DARRAY UIMAGE2DARRAY
136 %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGEBUFFER IIMAGEBUFFER UIMAGEBUFFER
137 %token IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW
140 %type <identifier> variable_identifier
141 %type <node> statement
142 %type <node> statement_list
143 %type <node> simple_statement
144 %type <n> precision_qualifier
145 %type <type_qualifier> type_qualifier
146 %type <type_qualifier> storage_qualifier
147 %type <type_qualifier> interpolation_qualifier
148 %type <type_qualifier> layout_qualifier
149 %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
150 %type <type_specifier> type_specifier
151 %type <type_specifier> type_specifier_no_prec
152 %type <type_specifier> type_specifier_nonarray
153 %type <n> basic_type_specifier_nonarray
154 %type <fully_specified_type> fully_specified_type
155 %type <function> function_prototype
156 %type <function> function_header
157 %type <function> function_header_with_parameters
158 %type <function> function_declarator
159 %type <parameter_declarator> parameter_declarator
160 %type <parameter_declarator> parameter_declaration
161 %type <type_qualifier> parameter_qualifier
162 %type <type_qualifier> parameter_type_qualifier
163 %type <type_specifier> parameter_type_specifier
164 %type <function_definition> function_definition
165 %type <compound_statement> compound_statement_no_new_scope
166 %type <compound_statement> compound_statement
167 %type <node> statement_no_new_scope
168 %type <node> expression_statement
169 %type <expression> expression
170 %type <expression> primary_expression
171 %type <expression> assignment_expression
172 %type <expression> conditional_expression
173 %type <expression> logical_or_expression
174 %type <expression> logical_xor_expression
175 %type <expression> logical_and_expression
176 %type <expression> inclusive_or_expression
177 %type <expression> exclusive_or_expression
178 %type <expression> and_expression
179 %type <expression> equality_expression
180 %type <expression> relational_expression
181 %type <expression> shift_expression
182 %type <expression> additive_expression
183 %type <expression> multiplicative_expression
184 %type <expression> unary_expression
185 %type <expression> constant_expression
186 %type <expression> integer_expression
187 %type <expression> postfix_expression
188 %type <expression> function_call_header_with_parameters
189 %type <expression> function_call_header_no_parameters
190 %type <expression> function_call_header
191 %type <expression> function_call_generic
192 %type <expression> function_call_or_method
193 %type <expression> function_call
194 %type <expression> method_call_generic
195 %type <expression> method_call_header_with_parameters
196 %type <expression> method_call_header_no_parameters
197 %type <expression> method_call_header
198 %type <n> assignment_operator
199 %type <n> unary_operator
200 %type <expression> function_identifier
201 %type <node> external_declaration
202 %type <declarator_list> init_declarator_list
203 %type <declarator_list> single_declaration
204 %type <expression> initializer
205 %type <node> declaration
206 %type <node> declaration_statement
207 %type <node> jump_statement
208 %type <struct_specifier> struct_specifier
209 %type <node> struct_declaration_list
210 %type <declarator_list> struct_declaration
211 %type <declaration> struct_declarator
212 %type <declaration> struct_declarator_list
213 %type <node> selection_statement
214 %type <selection_rest_statement> selection_rest_statement
215 %type <node> switch_statement
216 %type <switch_body> switch_body
217 %type <case_label_list> case_label_list
218 %type <case_label> case_label
219 %type <case_statement> case_statement
220 %type <case_statement_list> case_statement_list
221 %type <node> iteration_statement
222 %type <node> condition
223 %type <node> conditionopt
224 %type <node> for_init_statement
225 %type <for_rest_statement> for_rest_statement
229 version_statement extension_statement_list
231 _mesa_glsl_initialize_types(state);
233 external_declaration_list
235 delete state->symbols;
236 state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
237 _mesa_glsl_initialize_types(state);
242 /* blank - no #version specified: defaults are already set */
243 | VERSION INTCONSTANT EOL
245 bool supported = false;
249 state->es_shader = true;
250 supported = state->Const.GLSL_100ES;
253 supported = state->Const.GLSL_110;
256 supported = state->Const.GLSL_120;
259 supported = state->Const.GLSL_130;
266 state->language_version = $2;
267 state->version_string =
268 ralloc_asprintf(state, "GLSL%s %d.%02d",
269 state->es_shader ? " ES" : "",
270 state->language_version / 100,
271 state->language_version % 100);
274 _mesa_glsl_error(& @2, state, "%s is not supported. "
275 "Supported versions are: %s\n",
276 state->version_string,
277 state->supported_version_string);
284 | PRAGMA_DEBUG_OFF EOL
285 | PRAGMA_OPTIMIZE_ON EOL
286 | PRAGMA_OPTIMIZE_OFF EOL
287 | PRAGMA_INVARIANT_ALL EOL
289 if (state->language_version < 120) {
290 _mesa_glsl_warning(& @1, state,
291 "pragma `invariant(all)' not supported in %s",
292 state->version_string);
294 state->all_invariant = true;
299 extension_statement_list:
301 | extension_statement_list extension_statement
311 EXTENSION any_identifier COLON any_identifier EOL
313 if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
319 external_declaration_list:
322 /* FINISHME: The NULL test is required because pragmas are set to
323 * FINISHME: NULL. (See production rule for external_declaration.)
326 state->translation_unit.push_tail(& $1->link);
328 | external_declaration_list external_declaration
330 /* FINISHME: The NULL test is required because pragmas are set to
331 * FINISHME: NULL. (See production rule for external_declaration.)
334 state->translation_unit.push_tail(& $2->link);
347 $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
348 $$->set_location(yylloc);
349 $$->primary_expression.identifier = $1;
354 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
355 $$->set_location(yylloc);
356 $$->primary_expression.int_constant = $1;
361 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
362 $$->set_location(yylloc);
363 $$->primary_expression.uint_constant = $1;
368 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
369 $$->set_location(yylloc);
370 $$->primary_expression.float_constant = $1;
375 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
376 $$->set_location(yylloc);
377 $$->primary_expression.bool_constant = $1;
387 | postfix_expression '[' integer_expression ']'
390 $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
391 $$->set_location(yylloc);
397 | postfix_expression '.' any_identifier
400 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
401 $$->set_location(yylloc);
402 $$->primary_expression.identifier = $3;
404 | postfix_expression INC_OP
407 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
408 $$->set_location(yylloc);
410 | postfix_expression DEC_OP
413 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
414 $$->set_location(yylloc);
423 function_call_or_method
426 function_call_or_method:
427 function_call_generic
428 | postfix_expression '.' method_call_generic
431 $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
432 $$->set_location(yylloc);
436 function_call_generic:
437 function_call_header_with_parameters ')'
438 | function_call_header_no_parameters ')'
441 function_call_header_no_parameters:
442 function_call_header VOID_TOK
443 | function_call_header
446 function_call_header_with_parameters:
447 function_call_header assignment_expression
450 $$->set_location(yylloc);
451 $$->expressions.push_tail(& $2->link);
453 | function_call_header_with_parameters ',' assignment_expression
456 $$->set_location(yylloc);
457 $$->expressions.push_tail(& $3->link);
461 // Grammar Note: Constructors look like functions, but lexical
462 // analysis recognized most of them as keywords. They are now
463 // recognized through "type_specifier".
464 function_call_header:
465 function_identifier '('
472 $$ = new(ctx) ast_function_expression($1);
473 $$->set_location(yylloc);
475 | variable_identifier
478 ast_expression *callee = new(ctx) ast_expression($1);
479 $$ = new(ctx) ast_function_expression(callee);
480 $$->set_location(yylloc);
485 ast_expression *callee = new(ctx) ast_expression($1);
486 $$ = new(ctx) ast_function_expression(callee);
487 $$->set_location(yylloc);
492 method_call_header_with_parameters ')'
493 | method_call_header_no_parameters ')'
496 method_call_header_no_parameters:
497 method_call_header VOID_TOK
501 method_call_header_with_parameters:
502 method_call_header assignment_expression
505 $$->set_location(yylloc);
506 $$->expressions.push_tail(& $2->link);
508 | method_call_header_with_parameters ',' assignment_expression
511 $$->set_location(yylloc);
512 $$->expressions.push_tail(& $3->link);
516 // Grammar Note: Constructors look like methods, but lexical
517 // analysis recognized most of them as keywords. They are now
518 // recognized through "type_specifier".
520 variable_identifier '('
523 ast_expression *callee = new(ctx) ast_expression($1);
524 $$ = new(ctx) ast_function_expression(callee);
525 $$->set_location(yylloc);
529 // Grammar Note: No traditional style type casts.
532 | INC_OP unary_expression
535 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
536 $$->set_location(yylloc);
538 | DEC_OP unary_expression
541 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
542 $$->set_location(yylloc);
544 | unary_operator unary_expression
547 $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
548 $$->set_location(yylloc);
552 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
554 '+' { $$ = ast_plus; }
555 | '-' { $$ = ast_neg; }
556 | '!' { $$ = ast_logic_not; }
557 | '~' { $$ = ast_bit_not; }
560 multiplicative_expression:
562 | multiplicative_expression '*' unary_expression
565 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
566 $$->set_location(yylloc);
568 | multiplicative_expression '/' unary_expression
571 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
572 $$->set_location(yylloc);
574 | multiplicative_expression '%' unary_expression
577 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
578 $$->set_location(yylloc);
583 multiplicative_expression
584 | additive_expression '+' multiplicative_expression
587 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
588 $$->set_location(yylloc);
590 | additive_expression '-' multiplicative_expression
593 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
594 $$->set_location(yylloc);
600 | shift_expression LEFT_OP additive_expression
603 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
604 $$->set_location(yylloc);
606 | shift_expression RIGHT_OP additive_expression
609 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
610 $$->set_location(yylloc);
614 relational_expression:
616 | relational_expression '<' shift_expression
619 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
620 $$->set_location(yylloc);
622 | relational_expression '>' shift_expression
625 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
626 $$->set_location(yylloc);
628 | relational_expression LE_OP shift_expression
631 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
632 $$->set_location(yylloc);
634 | relational_expression GE_OP shift_expression
637 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
638 $$->set_location(yylloc);
643 relational_expression
644 | equality_expression EQ_OP relational_expression
647 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
648 $$->set_location(yylloc);
650 | equality_expression NE_OP relational_expression
653 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
654 $$->set_location(yylloc);
660 | and_expression '&' equality_expression
663 $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
664 $$->set_location(yylloc);
668 exclusive_or_expression:
670 | exclusive_or_expression '^' and_expression
673 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
674 $$->set_location(yylloc);
678 inclusive_or_expression:
679 exclusive_or_expression
680 | inclusive_or_expression '|' exclusive_or_expression
683 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
684 $$->set_location(yylloc);
688 logical_and_expression:
689 inclusive_or_expression
690 | logical_and_expression AND_OP inclusive_or_expression
693 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
694 $$->set_location(yylloc);
698 logical_xor_expression:
699 logical_and_expression
700 | logical_xor_expression XOR_OP logical_and_expression
703 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
704 $$->set_location(yylloc);
708 logical_or_expression:
709 logical_xor_expression
710 | logical_or_expression OR_OP logical_xor_expression
713 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
714 $$->set_location(yylloc);
718 conditional_expression:
719 logical_or_expression
720 | logical_or_expression '?' expression ':' assignment_expression
723 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
724 $$->set_location(yylloc);
728 assignment_expression:
729 conditional_expression
730 | unary_expression assignment_operator assignment_expression
733 $$ = new(ctx) ast_expression($2, $1, $3, NULL);
734 $$->set_location(yylloc);
739 '=' { $$ = ast_assign; }
740 | MUL_ASSIGN { $$ = ast_mul_assign; }
741 | DIV_ASSIGN { $$ = ast_div_assign; }
742 | MOD_ASSIGN { $$ = ast_mod_assign; }
743 | ADD_ASSIGN { $$ = ast_add_assign; }
744 | SUB_ASSIGN { $$ = ast_sub_assign; }
745 | LEFT_ASSIGN { $$ = ast_ls_assign; }
746 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
747 | AND_ASSIGN { $$ = ast_and_assign; }
748 | XOR_ASSIGN { $$ = ast_xor_assign; }
749 | OR_ASSIGN { $$ = ast_or_assign; }
753 assignment_expression
757 | expression ',' assignment_expression
760 if ($1->oper != ast_sequence) {
761 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
762 $$->set_location(yylloc);
763 $$->expressions.push_tail(& $1->link);
768 $$->expressions.push_tail(& $3->link);
773 conditional_expression
777 function_prototype ';'
779 state->symbols->pop_scope();
782 | init_declarator_list ';'
786 | PRECISION precision_qualifier type_specifier_no_prec ';'
789 $3->is_precision_statement = true;
795 function_declarator ')'
800 | function_header_with_parameters
803 function_header_with_parameters:
804 function_header parameter_declaration
807 $$->parameters.push_tail(& $2->link);
809 | function_header_with_parameters ',' parameter_declaration
812 $$->parameters.push_tail(& $3->link);
817 fully_specified_type variable_identifier '('
820 $$ = new(ctx) ast_function();
821 $$->set_location(yylloc);
822 $$->return_type = $1;
825 state->symbols->add_function(new(state) ir_function($2));
826 state->symbols->push_scope();
830 parameter_declarator:
831 type_specifier any_identifier
834 $$ = new(ctx) ast_parameter_declarator();
835 $$->set_location(yylloc);
836 $$->type = new(ctx) ast_fully_specified_type();
837 $$->type->set_location(yylloc);
838 $$->type->specifier = $1;
841 | type_specifier any_identifier '[' constant_expression ']'
844 $$ = new(ctx) ast_parameter_declarator();
845 $$->set_location(yylloc);
846 $$->type = new(ctx) ast_fully_specified_type();
847 $$->type->set_location(yylloc);
848 $$->type->specifier = $1;
855 parameter_declaration:
856 parameter_type_qualifier parameter_qualifier parameter_declarator
858 $1.flags.i |= $2.flags.i;
861 $$->type->qualifier = $1;
863 | parameter_qualifier parameter_declarator
866 $$->type->qualifier = $1;
868 | parameter_type_qualifier parameter_qualifier parameter_type_specifier
871 $1.flags.i |= $2.flags.i;
873 $$ = new(ctx) ast_parameter_declarator();
874 $$->set_location(yylloc);
875 $$->type = new(ctx) ast_fully_specified_type();
876 $$->type->qualifier = $1;
877 $$->type->specifier = $3;
879 | parameter_qualifier parameter_type_specifier
882 $$ = new(ctx) ast_parameter_declarator();
883 $$->set_location(yylloc);
884 $$->type = new(ctx) ast_fully_specified_type();
885 $$->type->qualifier = $1;
886 $$->type->specifier = $2;
893 memset(& $$, 0, sizeof($$));
897 memset(& $$, 0, sizeof($$));
902 memset(& $$, 0, sizeof($$));
907 memset(& $$, 0, sizeof($$));
913 parameter_type_specifier:
917 init_declarator_list:
919 | init_declarator_list ',' any_identifier
922 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
923 decl->set_location(yylloc);
926 $$->declarations.push_tail(&decl->link);
927 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
929 | init_declarator_list ',' any_identifier '[' ']'
932 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
933 decl->set_location(yylloc);
936 $$->declarations.push_tail(&decl->link);
937 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
939 | init_declarator_list ',' any_identifier '[' constant_expression ']'
942 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
943 decl->set_location(yylloc);
946 $$->declarations.push_tail(&decl->link);
947 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
949 | init_declarator_list ',' any_identifier '[' ']' '=' initializer
952 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7);
953 decl->set_location(yylloc);
956 $$->declarations.push_tail(&decl->link);
957 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
959 | init_declarator_list ',' any_identifier '[' constant_expression ']' '=' initializer
962 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
963 decl->set_location(yylloc);
966 $$->declarations.push_tail(&decl->link);
967 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
969 | init_declarator_list ',' any_identifier '=' initializer
972 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
973 decl->set_location(yylloc);
976 $$->declarations.push_tail(&decl->link);
977 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
981 // Grammar Note: No 'enum', or 'typedef'.
986 /* Empty declaration list is valid. */
987 $$ = new(ctx) ast_declarator_list($1);
988 $$->set_location(yylloc);
990 | fully_specified_type any_identifier
993 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
995 $$ = new(ctx) ast_declarator_list($1);
996 $$->set_location(yylloc);
997 $$->declarations.push_tail(&decl->link);
999 | fully_specified_type any_identifier '[' ']'
1002 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
1004 $$ = new(ctx) ast_declarator_list($1);
1005 $$->set_location(yylloc);
1006 $$->declarations.push_tail(&decl->link);
1008 | fully_specified_type any_identifier '[' constant_expression ']'
1011 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
1013 $$ = new(ctx) ast_declarator_list($1);
1014 $$->set_location(yylloc);
1015 $$->declarations.push_tail(&decl->link);
1017 | fully_specified_type any_identifier '[' ']' '=' initializer
1020 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6);
1022 $$ = new(ctx) ast_declarator_list($1);
1023 $$->set_location(yylloc);
1024 $$->declarations.push_tail(&decl->link);
1026 | fully_specified_type any_identifier '[' constant_expression ']' '=' initializer
1029 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
1031 $$ = new(ctx) ast_declarator_list($1);
1032 $$->set_location(yylloc);
1033 $$->declarations.push_tail(&decl->link);
1035 | fully_specified_type any_identifier '=' initializer
1038 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1040 $$ = new(ctx) ast_declarator_list($1);
1041 $$->set_location(yylloc);
1042 $$->declarations.push_tail(&decl->link);
1044 | INVARIANT variable_identifier // Vertex only.
1047 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1049 $$ = new(ctx) ast_declarator_list(NULL);
1050 $$->set_location(yylloc);
1051 $$->invariant = true;
1053 $$->declarations.push_tail(&decl->link);
1057 fully_specified_type:
1061 $$ = new(ctx) ast_fully_specified_type();
1062 $$->set_location(yylloc);
1065 | type_qualifier type_specifier
1068 $$ = new(ctx) ast_fully_specified_type();
1069 $$->set_location(yylloc);
1076 LAYOUT_TOK '(' layout_qualifier_id_list ')'
1082 layout_qualifier_id_list:
1084 | layout_qualifier_id_list ',' layout_qualifier_id
1086 if (($1.flags.i & $3.flags.i) != 0) {
1087 _mesa_glsl_error(& @3, state,
1088 "duplicate layout qualifiers used\n");
1092 $$.flags.i = $1.flags.i | $3.flags.i;
1094 if ($1.flags.q.explicit_location)
1095 $$.location = $1.location;
1097 if ($3.flags.q.explicit_location)
1098 $$.location = $3.location;
1102 layout_qualifier_id:
1105 bool got_one = false;
1107 memset(& $$, 0, sizeof($$));
1109 /* Layout qualifiers for ARB_fragment_coord_conventions. */
1110 if (!got_one && state->ARB_fragment_coord_conventions_enable) {
1111 if (strcmp($1, "origin_upper_left") == 0) {
1113 $$.flags.q.origin_upper_left = 1;
1114 } else if (strcmp($1, "pixel_center_integer") == 0) {
1116 $$.flags.q.pixel_center_integer = 1;
1119 if (got_one && state->ARB_fragment_coord_conventions_warn) {
1120 _mesa_glsl_warning(& @1, state,
1121 "GL_ARB_fragment_coord_conventions layout "
1122 "identifier `%s' used\n", $1);
1126 /* Layout qualifiers for AMD/ARB_conservative_depth. */
1127 if (!got_one && state->AMD_conservative_depth_enable) {
1128 if (strcmp($1, "depth_any") == 0) {
1130 $$.flags.q.depth_any = 1;
1131 } else if (strcmp($1, "depth_greater") == 0) {
1133 $$.flags.q.depth_greater = 1;
1134 } else if (strcmp($1, "depth_less") == 0) {
1136 $$.flags.q.depth_less = 1;
1137 } else if (strcmp($1, "depth_unchanged") == 0) {
1139 $$.flags.q.depth_unchanged = 1;
1142 if (got_one && state->AMD_conservative_depth_warn) {
1143 _mesa_glsl_warning(& @1, state,
1144 "GL_ARB_conservative_depth "
1145 "layout qualifier `%s' is used\n", $1);
1150 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1155 | any_identifier '=' INTCONSTANT
1157 bool got_one = false;
1159 memset(& $$, 0, sizeof($$));
1161 if (state->ARB_explicit_attrib_location_enable) {
1162 /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1163 * FINISHME: GLSL 1.30 (or later) are supported.
1165 if (strcmp("location", $1) == 0) {
1168 $$.flags.q.explicit_location = 1;
1173 _mesa_glsl_error(& @3, state,
1174 "invalid location %d specified\n", $3);
1180 /* If the identifier didn't match any known layout identifiers,
1184 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1187 } else if (state->ARB_explicit_attrib_location_warn) {
1188 _mesa_glsl_warning(& @1, state,
1189 "GL_ARB_explicit_attrib_location layout "
1190 "identifier `%s' used\n", $1);
1195 interpolation_qualifier:
1198 memset(& $$, 0, sizeof($$));
1199 $$.flags.q.smooth = 1;
1203 memset(& $$, 0, sizeof($$));
1204 $$.flags.q.flat = 1;
1208 memset(& $$, 0, sizeof($$));
1209 $$.flags.q.noperspective = 1;
1213 parameter_type_qualifier:
1216 memset(& $$, 0, sizeof($$));
1217 $$.flags.q.constant = 1;
1224 | layout_qualifier storage_qualifier
1227 $$.flags.i |= $2.flags.i;
1229 | interpolation_qualifier
1230 | interpolation_qualifier storage_qualifier
1233 $$.flags.i |= $2.flags.i;
1235 | INVARIANT storage_qualifier
1238 $$.flags.q.invariant = 1;
1240 | INVARIANT interpolation_qualifier storage_qualifier
1243 $$.flags.i |= $3.flags.i;
1244 $$.flags.q.invariant = 1;
1248 memset(& $$, 0, sizeof($$));
1249 $$.flags.q.invariant = 1;
1256 memset(& $$, 0, sizeof($$));
1257 $$.flags.q.constant = 1;
1261 memset(& $$, 0, sizeof($$));
1262 $$.flags.q.attribute = 1;
1266 memset(& $$, 0, sizeof($$));
1267 $$.flags.q.varying = 1;
1271 memset(& $$, 0, sizeof($$));
1272 $$.flags.q.centroid = 1;
1273 $$.flags.q.varying = 1;
1277 memset(& $$, 0, sizeof($$));
1282 memset(& $$, 0, sizeof($$));
1287 memset(& $$, 0, sizeof($$));
1288 $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1292 memset(& $$, 0, sizeof($$));
1293 $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1297 memset(& $$, 0, sizeof($$));
1298 $$.flags.q.uniform = 1;
1303 type_specifier_no_prec
1307 | precision_qualifier type_specifier_no_prec
1314 type_specifier_no_prec:
1315 type_specifier_nonarray
1316 | type_specifier_nonarray '[' ']'
1319 $$->is_array = true;
1320 $$->array_size = NULL;
1322 | type_specifier_nonarray '[' constant_expression ']'
1325 $$->is_array = true;
1326 $$->array_size = $3;
1330 type_specifier_nonarray:
1331 basic_type_specifier_nonarray
1334 $$ = new(ctx) ast_type_specifier($1);
1335 $$->set_location(yylloc);
1340 $$ = new(ctx) ast_type_specifier($1);
1341 $$->set_location(yylloc);
1346 $$ = new(ctx) ast_type_specifier($1);
1347 $$->set_location(yylloc);
1351 basic_type_specifier_nonarray:
1352 VOID_TOK { $$ = ast_void; }
1353 | FLOAT_TOK { $$ = ast_float; }
1354 | INT_TOK { $$ = ast_int; }
1355 | UINT_TOK { $$ = ast_uint; }
1356 | BOOL_TOK { $$ = ast_bool; }
1357 | VEC2 { $$ = ast_vec2; }
1358 | VEC3 { $$ = ast_vec3; }
1359 | VEC4 { $$ = ast_vec4; }
1360 | BVEC2 { $$ = ast_bvec2; }
1361 | BVEC3 { $$ = ast_bvec3; }
1362 | BVEC4 { $$ = ast_bvec4; }
1363 | IVEC2 { $$ = ast_ivec2; }
1364 | IVEC3 { $$ = ast_ivec3; }
1365 | IVEC4 { $$ = ast_ivec4; }
1366 | UVEC2 { $$ = ast_uvec2; }
1367 | UVEC3 { $$ = ast_uvec3; }
1368 | UVEC4 { $$ = ast_uvec4; }
1369 | MAT2X2 { $$ = ast_mat2; }
1370 | MAT2X3 { $$ = ast_mat2x3; }
1371 | MAT2X4 { $$ = ast_mat2x4; }
1372 | MAT3X2 { $$ = ast_mat3x2; }
1373 | MAT3X3 { $$ = ast_mat3; }
1374 | MAT3X4 { $$ = ast_mat3x4; }
1375 | MAT4X2 { $$ = ast_mat4x2; }
1376 | MAT4X3 { $$ = ast_mat4x3; }
1377 | MAT4X4 { $$ = ast_mat4; }
1378 | SAMPLER1D { $$ = ast_sampler1d; }
1379 | SAMPLER2D { $$ = ast_sampler2d; }
1380 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
1381 | SAMPLER3D { $$ = ast_sampler3d; }
1382 | SAMPLERCUBE { $$ = ast_samplercube; }
1383 | SAMPLEREXTERNALOES { $$ = ast_samplerexternaloes; }
1384 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
1385 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
1386 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
1387 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
1388 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
1389 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
1390 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
1391 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1392 | ISAMPLER1D { $$ = ast_isampler1d; }
1393 | ISAMPLER2D { $$ = ast_isampler2d; }
1394 | ISAMPLER3D { $$ = ast_isampler3d; }
1395 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1396 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1397 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1398 | USAMPLER1D { $$ = ast_usampler1d; }
1399 | USAMPLER2D { $$ = ast_usampler2d; }
1400 | USAMPLER3D { $$ = ast_usampler3d; }
1401 | USAMPLERCUBE { $$ = ast_usamplercube; }
1402 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1403 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1406 precision_qualifier:
1408 if (!state->es_shader && state->language_version < 130)
1409 _mesa_glsl_error(& @1, state,
1410 "precision qualifier forbidden "
1411 "in %s (1.30 or later "
1413 state->version_string);
1415 $$ = ast_precision_high;
1418 if (!state->es_shader && state->language_version < 130)
1419 _mesa_glsl_error(& @1, state,
1420 "precision qualifier forbidden "
1421 "in %s (1.30 or later "
1423 state->version_string);
1425 $$ = ast_precision_medium;
1428 if (!state->es_shader && state->language_version < 130)
1429 _mesa_glsl_error(& @1, state,
1430 "precision qualifier forbidden "
1431 "in %s (1.30 or later "
1433 state->version_string);
1435 $$ = ast_precision_low;
1440 STRUCT any_identifier '{' struct_declaration_list '}'
1443 $$ = new(ctx) ast_struct_specifier($2, $4);
1444 $$->set_location(yylloc);
1445 state->symbols->add_type($2, glsl_type::void_type);
1447 | STRUCT '{' struct_declaration_list '}'
1450 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1451 $$->set_location(yylloc);
1455 struct_declaration_list:
1458 $$ = (ast_node *) $1;
1459 $1->link.self_link();
1461 | struct_declaration_list struct_declaration
1463 $$ = (ast_node *) $1;
1464 $$->link.insert_before(& $2->link);
1469 type_specifier struct_declarator_list ';'
1472 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1473 type->set_location(yylloc);
1475 type->specifier = $1;
1476 $$ = new(ctx) ast_declarator_list(type);
1477 $$->set_location(yylloc);
1479 $$->declarations.push_degenerate_list_at_head(& $2->link);
1483 struct_declarator_list:
1487 $1->link.self_link();
1489 | struct_declarator_list ',' struct_declarator
1492 $$->link.insert_before(& $3->link);
1500 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1501 $$->set_location(yylloc);
1502 state->symbols->add_variable(new(state) ir_variable(NULL, $1, ir_var_auto));
1504 | any_identifier '[' constant_expression ']'
1507 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1508 $$->set_location(yylloc);
1513 assignment_expression
1516 declaration_statement:
1520 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1523 compound_statement { $$ = (ast_node *) $1; }
1528 declaration_statement
1529 | expression_statement
1530 | selection_statement
1532 | iteration_statement
1540 $$ = new(ctx) ast_compound_statement(true, NULL);
1541 $$->set_location(yylloc);
1545 state->symbols->push_scope();
1550 $$ = new(ctx) ast_compound_statement(true, $3);
1551 $$->set_location(yylloc);
1552 state->symbols->pop_scope();
1556 statement_no_new_scope:
1557 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1561 compound_statement_no_new_scope:
1565 $$ = new(ctx) ast_compound_statement(false, NULL);
1566 $$->set_location(yylloc);
1568 | '{' statement_list '}'
1571 $$ = new(ctx) ast_compound_statement(false, $2);
1572 $$->set_location(yylloc);
1580 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1585 $$->link.self_link();
1587 | statement_list statement
1590 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1594 $$->link.insert_before(& $2->link);
1598 expression_statement:
1602 $$ = new(ctx) ast_expression_statement(NULL);
1603 $$->set_location(yylloc);
1608 $$ = new(ctx) ast_expression_statement($1);
1609 $$->set_location(yylloc);
1613 selection_statement:
1614 IF '(' expression ')' selection_rest_statement
1616 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1618 $$->set_location(yylloc);
1622 selection_rest_statement:
1623 statement ELSE statement
1625 $$.then_statement = $1;
1626 $$.else_statement = $3;
1630 $$.then_statement = $1;
1631 $$.else_statement = NULL;
1638 $$ = (ast_node *) $1;
1640 | fully_specified_type any_identifier '=' initializer
1643 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1644 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1645 decl->set_location(yylloc);
1646 declarator->set_location(yylloc);
1648 declarator->declarations.push_tail(&decl->link);
1654 * siwtch_statement grammar is based on the syntax described in the body
1655 * of the GLSL spec, not in it's appendix!!!
1658 SWITCH '(' expression ')' switch_body
1660 $$ = new(state) ast_switch_statement($3, $5);
1667 $$ = new(state) ast_switch_body(NULL);
1668 $$->set_location(yylloc);
1670 | '{' case_statement_list '}'
1672 $$ = new(state) ast_switch_body($2);
1673 $$->set_location(yylloc);
1680 $$ = new(state) ast_case_label($2);
1684 $$ = new(state) ast_case_label(NULL);
1691 ast_case_label_list *labels = new(state) ast_case_label_list();
1693 labels->labels.push_tail(& $1->link);
1696 | case_label_list case_label
1699 $$->labels.push_tail(& $2->link);
1704 case_label_list statement
1706 ast_case_statement *stmts = new(state) ast_case_statement($1);
1708 stmts->stmts.push_tail(& $2->link);
1711 | case_statement statement
1714 $$->stmts.push_tail(& $2->link);
1718 case_statement_list:
1721 ast_case_statement_list *cases= new(state) ast_case_statement_list();
1723 cases->cases.push_tail(& $1->link);
1726 | case_statement_list case_statement
1729 $$->cases.push_tail(& $2->link);
1733 iteration_statement:
1734 WHILE '(' condition ')' statement_no_new_scope
1737 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1738 NULL, $3, NULL, $5);
1739 $$->set_location(yylloc);
1741 | DO statement WHILE '(' expression ')' ';'
1744 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1745 NULL, $5, NULL, $2);
1746 $$->set_location(yylloc);
1748 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1751 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1752 $3, $4.cond, $4.rest, $6);
1753 $$->set_location(yylloc);
1758 expression_statement
1759 | declaration_statement
1776 | conditionopt ';' expression
1783 // Grammar Note: No 'goto'. Gotos are not supported.
1788 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1789 $$->set_location(yylloc);
1794 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1795 $$->set_location(yylloc);
1800 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1801 $$->set_location(yylloc);
1803 | RETURN expression ';'
1806 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1807 $$->set_location(yylloc);
1809 | DISCARD ';' // Fragment shader only.
1812 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1813 $$->set_location(yylloc);
1817 external_declaration:
1818 function_definition { $$ = $1; }
1819 | declaration { $$ = $1; }
1820 | pragma_statement { $$ = NULL; }
1823 function_definition:
1824 function_prototype compound_statement_no_new_scope
1827 $$ = new(ctx) ast_function_definition();
1828 $$->set_location(yylloc);
1832 state->symbols->pop_scope();