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
37 static void yyerror(YYLTYPE *loc, _mesa_glsl_parse_state *st, const char *msg)
39 _mesa_glsl_error(loc, st, "%s", msg);
55 %lex-param {void *scanner}
56 %parse-param {struct _mesa_glsl_parse_state *state}
63 struct ast_type_qualifier type_qualifier;
66 ast_type_specifier *type_specifier;
67 ast_fully_specified_type *fully_specified_type;
68 ast_function *function;
69 ast_parameter_declarator *parameter_declarator;
70 ast_function_definition *function_definition;
71 ast_compound_statement *compound_statement;
72 ast_expression *expression;
73 ast_declarator_list *declarator_list;
74 ast_struct_specifier *struct_specifier;
75 ast_declaration *declaration;
76 ast_switch_body *switch_body;
77 ast_case_label *case_label;
78 ast_case_label_list *case_label_list;
79 ast_case_statement *case_statement;
80 ast_case_statement_list *case_statement_list;
88 ast_node *then_statement;
89 ast_node *else_statement;
90 } selection_rest_statement;
93 %token ATTRIBUTE CONST_TOK BOOL_TOK FLOAT_TOK INT_TOK UINT_TOK
94 %token BREAK CONTINUE DO ELSE FOR IF DISCARD RETURN SWITCH CASE DEFAULT
95 %token BVEC2 BVEC3 BVEC4 IVEC2 IVEC3 IVEC4 UVEC2 UVEC3 UVEC4 VEC2 VEC3 VEC4
96 %token CENTROID IN_TOK OUT_TOK INOUT_TOK UNIFORM VARYING
97 %token NOPERSPECTIVE FLAT SMOOTH
98 %token MAT2X2 MAT2X3 MAT2X4
99 %token MAT3X2 MAT3X3 MAT3X4
100 %token MAT4X2 MAT4X3 MAT4X4
101 %token SAMPLER1D SAMPLER2D SAMPLER3D SAMPLERCUBE SAMPLER1DSHADOW SAMPLER2DSHADOW
102 %token SAMPLERCUBESHADOW SAMPLER1DARRAY SAMPLER2DARRAY SAMPLER1DARRAYSHADOW
103 %token SAMPLER2DARRAYSHADOW ISAMPLER1D ISAMPLER2D ISAMPLER3D ISAMPLERCUBE
104 %token ISAMPLER1DARRAY ISAMPLER2DARRAY USAMPLER1D USAMPLER2D USAMPLER3D
105 %token USAMPLERCUBE USAMPLER1DARRAY USAMPLER2DARRAY
106 %token SAMPLEREXTERNALOES
107 %token STRUCT VOID_TOK WHILE
108 %token <identifier> IDENTIFIER TYPE_IDENTIFIER NEW_IDENTIFIER
109 %type <identifier> any_identifier
110 %token <real> FLOATCONSTANT
111 %token <n> INTCONSTANT UINTCONSTANT BOOLCONSTANT
112 %token <identifier> FIELD_SELECTION
113 %token LEFT_OP RIGHT_OP
114 %token INC_OP DEC_OP LE_OP GE_OP EQ_OP NE_OP
115 %token AND_OP OR_OP XOR_OP MUL_ASSIGN DIV_ASSIGN ADD_ASSIGN
116 %token MOD_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN XOR_ASSIGN OR_ASSIGN
119 %token LOWP MEDIUMP HIGHP SUPERP PRECISION
121 %token VERSION_TOK EXTENSION LINE COLON EOL INTERFACE OUTPUT
122 %token PRAGMA_DEBUG_ON PRAGMA_DEBUG_OFF
123 %token PRAGMA_OPTIMIZE_ON PRAGMA_OPTIMIZE_OFF
124 %token PRAGMA_INVARIANT_ALL
127 /* Reserved words that are not actually used in the grammar.
129 %token ASM CLASS UNION ENUM TYPEDEF TEMPLATE THIS PACKED_TOK GOTO
130 %token INLINE_TOK NOINLINE VOLATILE PUBLIC_TOK STATIC EXTERN EXTERNAL
131 %token LONG_TOK SHORT_TOK DOUBLE_TOK HALF FIXED_TOK UNSIGNED INPUT_TOK OUPTUT
132 %token HVEC2 HVEC3 HVEC4 DVEC2 DVEC3 DVEC4 FVEC2 FVEC3 FVEC4
133 %token SAMPLER2DRECT SAMPLER3DRECT SAMPLER2DRECTSHADOW
134 %token SIZEOF CAST NAMESPACE USING
138 %token COMMON PARTITION ACTIVE SAMPLERBUFFER FILTER
139 %token IMAGE1D IMAGE2D IMAGE3D IMAGECUBE IMAGE1DARRAY IMAGE2DARRAY
140 %token IIMAGE1D IIMAGE2D IIMAGE3D IIMAGECUBE IIMAGE1DARRAY IIMAGE2DARRAY
141 %token UIMAGE1D UIMAGE2D UIMAGE3D UIMAGECUBE UIMAGE1DARRAY UIMAGE2DARRAY
142 %token IMAGE1DSHADOW IMAGE2DSHADOW IMAGEBUFFER IIMAGEBUFFER UIMAGEBUFFER
143 %token IMAGE1DARRAYSHADOW IMAGE2DARRAYSHADOW
146 %type <identifier> variable_identifier
147 %type <node> statement
148 %type <node> statement_list
149 %type <node> simple_statement
150 %type <n> precision_qualifier
151 %type <type_qualifier> type_qualifier
152 %type <type_qualifier> storage_qualifier
153 %type <type_qualifier> interpolation_qualifier
154 %type <type_qualifier> layout_qualifier
155 %type <type_qualifier> layout_qualifier_id_list layout_qualifier_id
156 %type <type_specifier> type_specifier
157 %type <type_specifier> type_specifier_no_prec
158 %type <type_specifier> type_specifier_nonarray
159 %type <n> basic_type_specifier_nonarray
160 %type <fully_specified_type> fully_specified_type
161 %type <function> function_prototype
162 %type <function> function_header
163 %type <function> function_header_with_parameters
164 %type <function> function_declarator
165 %type <parameter_declarator> parameter_declarator
166 %type <parameter_declarator> parameter_declaration
167 %type <type_qualifier> parameter_qualifier
168 %type <type_qualifier> parameter_type_qualifier
169 %type <type_specifier> parameter_type_specifier
170 %type <function_definition> function_definition
171 %type <compound_statement> compound_statement_no_new_scope
172 %type <compound_statement> compound_statement
173 %type <node> statement_no_new_scope
174 %type <node> expression_statement
175 %type <expression> expression
176 %type <expression> primary_expression
177 %type <expression> assignment_expression
178 %type <expression> conditional_expression
179 %type <expression> logical_or_expression
180 %type <expression> logical_xor_expression
181 %type <expression> logical_and_expression
182 %type <expression> inclusive_or_expression
183 %type <expression> exclusive_or_expression
184 %type <expression> and_expression
185 %type <expression> equality_expression
186 %type <expression> relational_expression
187 %type <expression> shift_expression
188 %type <expression> additive_expression
189 %type <expression> multiplicative_expression
190 %type <expression> unary_expression
191 %type <expression> constant_expression
192 %type <expression> integer_expression
193 %type <expression> postfix_expression
194 %type <expression> function_call_header_with_parameters
195 %type <expression> function_call_header_no_parameters
196 %type <expression> function_call_header
197 %type <expression> function_call_generic
198 %type <expression> function_call_or_method
199 %type <expression> function_call
200 %type <expression> method_call_generic
201 %type <expression> method_call_header_with_parameters
202 %type <expression> method_call_header_no_parameters
203 %type <expression> method_call_header
204 %type <n> assignment_operator
205 %type <n> unary_operator
206 %type <expression> function_identifier
207 %type <node> external_declaration
208 %type <declarator_list> init_declarator_list
209 %type <declarator_list> single_declaration
210 %type <expression> initializer
211 %type <node> declaration
212 %type <node> declaration_statement
213 %type <node> jump_statement
214 %type <struct_specifier> struct_specifier
215 %type <node> struct_declaration_list
216 %type <declarator_list> struct_declaration
217 %type <declaration> struct_declarator
218 %type <declaration> struct_declarator_list
219 %type <node> selection_statement
220 %type <selection_rest_statement> selection_rest_statement
221 %type <node> switch_statement
222 %type <switch_body> switch_body
223 %type <case_label_list> case_label_list
224 %type <case_label> case_label
225 %type <case_statement> case_statement
226 %type <case_statement_list> case_statement_list
227 %type <node> iteration_statement
228 %type <node> condition
229 %type <node> conditionopt
230 %type <node> for_init_statement
231 %type <for_rest_statement> for_rest_statement
235 version_statement extension_statement_list
237 _mesa_glsl_initialize_types(state);
239 external_declaration_list
241 delete state->symbols;
242 state->symbols = new(ralloc_parent(state)) glsl_symbol_table;
243 _mesa_glsl_initialize_types(state);
248 /* blank - no #version specified: defaults are already set */
249 | VERSION_TOK INTCONSTANT EOL
251 bool supported = false;
255 state->es_shader = true;
256 supported = state->Const.GLSL_100ES;
259 supported = state->Const.GLSL_110;
262 supported = state->Const.GLSL_120;
265 supported = state->Const.GLSL_130;
272 state->language_version = $2;
273 state->version_string =
274 ralloc_asprintf(state, "GLSL%s %d.%02d",
275 state->es_shader ? " ES" : "",
276 state->language_version / 100,
277 state->language_version % 100);
280 _mesa_glsl_error(& @2, state, "%s is not supported. "
281 "Supported versions are: %s\n",
282 state->version_string,
283 state->supported_version_string);
290 | PRAGMA_DEBUG_OFF EOL
291 | PRAGMA_OPTIMIZE_ON EOL
292 | PRAGMA_OPTIMIZE_OFF EOL
293 | PRAGMA_INVARIANT_ALL EOL
295 if (state->language_version < 120) {
296 _mesa_glsl_warning(& @1, state,
297 "pragma `invariant(all)' not supported in %s",
298 state->version_string);
300 state->all_invariant = true;
305 extension_statement_list:
307 | extension_statement_list extension_statement
317 EXTENSION any_identifier COLON any_identifier EOL
319 if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
325 external_declaration_list:
328 /* FINISHME: The NULL test is required because pragmas are set to
329 * FINISHME: NULL. (See production rule for external_declaration.)
332 state->translation_unit.push_tail(& $1->link);
334 | external_declaration_list external_declaration
336 /* FINISHME: The NULL test is required because pragmas are set to
337 * FINISHME: NULL. (See production rule for external_declaration.)
340 state->translation_unit.push_tail(& $2->link);
353 $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
354 $$->set_location(yylloc);
355 $$->primary_expression.identifier = $1;
360 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
361 $$->set_location(yylloc);
362 $$->primary_expression.int_constant = $1;
367 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
368 $$->set_location(yylloc);
369 $$->primary_expression.uint_constant = $1;
374 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
375 $$->set_location(yylloc);
376 $$->primary_expression.float_constant = $1;
381 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
382 $$->set_location(yylloc);
383 $$->primary_expression.bool_constant = $1;
393 | postfix_expression '[' integer_expression ']'
396 $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
397 $$->set_location(yylloc);
403 | postfix_expression '.' any_identifier
406 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
407 $$->set_location(yylloc);
408 $$->primary_expression.identifier = $3;
410 | postfix_expression INC_OP
413 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
414 $$->set_location(yylloc);
416 | postfix_expression DEC_OP
419 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
420 $$->set_location(yylloc);
429 function_call_or_method
432 function_call_or_method:
433 function_call_generic
434 | postfix_expression '.' method_call_generic
437 $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
438 $$->set_location(yylloc);
442 function_call_generic:
443 function_call_header_with_parameters ')'
444 | function_call_header_no_parameters ')'
447 function_call_header_no_parameters:
448 function_call_header VOID_TOK
449 | function_call_header
452 function_call_header_with_parameters:
453 function_call_header assignment_expression
456 $$->set_location(yylloc);
457 $$->expressions.push_tail(& $2->link);
459 | function_call_header_with_parameters ',' assignment_expression
462 $$->set_location(yylloc);
463 $$->expressions.push_tail(& $3->link);
467 // Grammar Note: Constructors look like functions, but lexical
468 // analysis recognized most of them as keywords. They are now
469 // recognized through "type_specifier".
470 function_call_header:
471 function_identifier '('
478 $$ = new(ctx) ast_function_expression($1);
479 $$->set_location(yylloc);
481 | variable_identifier
484 ast_expression *callee = new(ctx) ast_expression($1);
485 $$ = new(ctx) ast_function_expression(callee);
486 $$->set_location(yylloc);
491 ast_expression *callee = new(ctx) ast_expression($1);
492 $$ = new(ctx) ast_function_expression(callee);
493 $$->set_location(yylloc);
498 method_call_header_with_parameters ')'
499 | method_call_header_no_parameters ')'
502 method_call_header_no_parameters:
503 method_call_header VOID_TOK
507 method_call_header_with_parameters:
508 method_call_header assignment_expression
511 $$->set_location(yylloc);
512 $$->expressions.push_tail(& $2->link);
514 | method_call_header_with_parameters ',' assignment_expression
517 $$->set_location(yylloc);
518 $$->expressions.push_tail(& $3->link);
522 // Grammar Note: Constructors look like methods, but lexical
523 // analysis recognized most of them as keywords. They are now
524 // recognized through "type_specifier".
526 variable_identifier '('
529 ast_expression *callee = new(ctx) ast_expression($1);
530 $$ = new(ctx) ast_function_expression(callee);
531 $$->set_location(yylloc);
535 // Grammar Note: No traditional style type casts.
538 | INC_OP unary_expression
541 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
542 $$->set_location(yylloc);
544 | DEC_OP unary_expression
547 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
548 $$->set_location(yylloc);
550 | unary_operator unary_expression
553 $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
554 $$->set_location(yylloc);
558 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
560 '+' { $$ = ast_plus; }
561 | '-' { $$ = ast_neg; }
562 | '!' { $$ = ast_logic_not; }
563 | '~' { $$ = ast_bit_not; }
566 multiplicative_expression:
568 | multiplicative_expression '*' unary_expression
571 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
572 $$->set_location(yylloc);
574 | multiplicative_expression '/' unary_expression
577 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
578 $$->set_location(yylloc);
580 | multiplicative_expression '%' unary_expression
583 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
584 $$->set_location(yylloc);
589 multiplicative_expression
590 | additive_expression '+' multiplicative_expression
593 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
594 $$->set_location(yylloc);
596 | additive_expression '-' multiplicative_expression
599 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
600 $$->set_location(yylloc);
606 | shift_expression LEFT_OP additive_expression
609 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
610 $$->set_location(yylloc);
612 | shift_expression RIGHT_OP additive_expression
615 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
616 $$->set_location(yylloc);
620 relational_expression:
622 | relational_expression '<' shift_expression
625 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
626 $$->set_location(yylloc);
628 | relational_expression '>' shift_expression
631 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
632 $$->set_location(yylloc);
634 | relational_expression LE_OP shift_expression
637 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
638 $$->set_location(yylloc);
640 | relational_expression GE_OP shift_expression
643 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
644 $$->set_location(yylloc);
649 relational_expression
650 | equality_expression EQ_OP relational_expression
653 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
654 $$->set_location(yylloc);
656 | equality_expression NE_OP relational_expression
659 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
660 $$->set_location(yylloc);
666 | and_expression '&' equality_expression
669 $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
670 $$->set_location(yylloc);
674 exclusive_or_expression:
676 | exclusive_or_expression '^' and_expression
679 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
680 $$->set_location(yylloc);
684 inclusive_or_expression:
685 exclusive_or_expression
686 | inclusive_or_expression '|' exclusive_or_expression
689 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
690 $$->set_location(yylloc);
694 logical_and_expression:
695 inclusive_or_expression
696 | logical_and_expression AND_OP inclusive_or_expression
699 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
700 $$->set_location(yylloc);
704 logical_xor_expression:
705 logical_and_expression
706 | logical_xor_expression XOR_OP logical_and_expression
709 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
710 $$->set_location(yylloc);
714 logical_or_expression:
715 logical_xor_expression
716 | logical_or_expression OR_OP logical_xor_expression
719 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
720 $$->set_location(yylloc);
724 conditional_expression:
725 logical_or_expression
726 | logical_or_expression '?' expression ':' assignment_expression
729 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
730 $$->set_location(yylloc);
734 assignment_expression:
735 conditional_expression
736 | unary_expression assignment_operator assignment_expression
739 $$ = new(ctx) ast_expression($2, $1, $3, NULL);
740 $$->set_location(yylloc);
745 '=' { $$ = ast_assign; }
746 | MUL_ASSIGN { $$ = ast_mul_assign; }
747 | DIV_ASSIGN { $$ = ast_div_assign; }
748 | MOD_ASSIGN { $$ = ast_mod_assign; }
749 | ADD_ASSIGN { $$ = ast_add_assign; }
750 | SUB_ASSIGN { $$ = ast_sub_assign; }
751 | LEFT_ASSIGN { $$ = ast_ls_assign; }
752 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
753 | AND_ASSIGN { $$ = ast_and_assign; }
754 | XOR_ASSIGN { $$ = ast_xor_assign; }
755 | OR_ASSIGN { $$ = ast_or_assign; }
759 assignment_expression
763 | expression ',' assignment_expression
766 if ($1->oper != ast_sequence) {
767 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
768 $$->set_location(yylloc);
769 $$->expressions.push_tail(& $1->link);
774 $$->expressions.push_tail(& $3->link);
779 conditional_expression
783 function_prototype ';'
785 state->symbols->pop_scope();
788 | init_declarator_list ';'
792 | PRECISION precision_qualifier type_specifier_no_prec ';'
795 $3->is_precision_statement = true;
801 function_declarator ')'
806 | function_header_with_parameters
809 function_header_with_parameters:
810 function_header parameter_declaration
813 $$->parameters.push_tail(& $2->link);
815 | function_header_with_parameters ',' parameter_declaration
818 $$->parameters.push_tail(& $3->link);
823 fully_specified_type variable_identifier '('
826 $$ = new(ctx) ast_function();
827 $$->set_location(yylloc);
828 $$->return_type = $1;
831 state->symbols->add_function(new(state) ir_function($2));
832 state->symbols->push_scope();
836 parameter_declarator:
837 type_specifier any_identifier
840 $$ = new(ctx) ast_parameter_declarator();
841 $$->set_location(yylloc);
842 $$->type = new(ctx) ast_fully_specified_type();
843 $$->type->set_location(yylloc);
844 $$->type->specifier = $1;
847 | type_specifier any_identifier '[' constant_expression ']'
850 $$ = new(ctx) ast_parameter_declarator();
851 $$->set_location(yylloc);
852 $$->type = new(ctx) ast_fully_specified_type();
853 $$->type->set_location(yylloc);
854 $$->type->specifier = $1;
861 parameter_declaration:
862 parameter_type_qualifier parameter_qualifier parameter_declarator
864 $1.flags.i |= $2.flags.i;
867 $$->type->qualifier = $1;
869 | parameter_qualifier parameter_declarator
872 $$->type->qualifier = $1;
874 | parameter_type_qualifier parameter_qualifier parameter_type_specifier
877 $1.flags.i |= $2.flags.i;
879 $$ = new(ctx) ast_parameter_declarator();
880 $$->set_location(yylloc);
881 $$->type = new(ctx) ast_fully_specified_type();
882 $$->type->qualifier = $1;
883 $$->type->specifier = $3;
885 | parameter_qualifier parameter_type_specifier
888 $$ = new(ctx) ast_parameter_declarator();
889 $$->set_location(yylloc);
890 $$->type = new(ctx) ast_fully_specified_type();
891 $$->type->qualifier = $1;
892 $$->type->specifier = $2;
899 memset(& $$, 0, sizeof($$));
903 memset(& $$, 0, sizeof($$));
908 memset(& $$, 0, sizeof($$));
913 memset(& $$, 0, sizeof($$));
919 parameter_type_specifier:
923 init_declarator_list:
925 | init_declarator_list ',' any_identifier
928 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
929 decl->set_location(yylloc);
932 $$->declarations.push_tail(&decl->link);
933 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
935 | init_declarator_list ',' any_identifier '[' ']'
938 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
939 decl->set_location(yylloc);
942 $$->declarations.push_tail(&decl->link);
943 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
945 | init_declarator_list ',' any_identifier '[' constant_expression ']'
948 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
949 decl->set_location(yylloc);
952 $$->declarations.push_tail(&decl->link);
953 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
955 | init_declarator_list ',' any_identifier '[' ']' '=' initializer
958 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7);
959 decl->set_location(yylloc);
962 $$->declarations.push_tail(&decl->link);
963 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
965 | init_declarator_list ',' any_identifier '[' constant_expression ']' '=' initializer
968 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
969 decl->set_location(yylloc);
972 $$->declarations.push_tail(&decl->link);
973 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
975 | init_declarator_list ',' any_identifier '=' initializer
978 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
979 decl->set_location(yylloc);
982 $$->declarations.push_tail(&decl->link);
983 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
987 // Grammar Note: No 'enum', or 'typedef'.
992 /* Empty declaration list is valid. */
993 $$ = new(ctx) ast_declarator_list($1);
994 $$->set_location(yylloc);
996 | fully_specified_type any_identifier
999 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1001 $$ = new(ctx) ast_declarator_list($1);
1002 $$->set_location(yylloc);
1003 $$->declarations.push_tail(&decl->link);
1005 | fully_specified_type any_identifier '[' ']'
1008 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
1010 $$ = new(ctx) ast_declarator_list($1);
1011 $$->set_location(yylloc);
1012 $$->declarations.push_tail(&decl->link);
1014 | fully_specified_type any_identifier '[' constant_expression ']'
1017 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
1019 $$ = new(ctx) ast_declarator_list($1);
1020 $$->set_location(yylloc);
1021 $$->declarations.push_tail(&decl->link);
1023 | fully_specified_type any_identifier '[' ']' '=' initializer
1026 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6);
1028 $$ = new(ctx) ast_declarator_list($1);
1029 $$->set_location(yylloc);
1030 $$->declarations.push_tail(&decl->link);
1032 | fully_specified_type any_identifier '[' constant_expression ']' '=' initializer
1035 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
1037 $$ = new(ctx) ast_declarator_list($1);
1038 $$->set_location(yylloc);
1039 $$->declarations.push_tail(&decl->link);
1041 | fully_specified_type any_identifier '=' initializer
1044 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1046 $$ = new(ctx) ast_declarator_list($1);
1047 $$->set_location(yylloc);
1048 $$->declarations.push_tail(&decl->link);
1050 | INVARIANT variable_identifier // Vertex only.
1053 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1055 $$ = new(ctx) ast_declarator_list(NULL);
1056 $$->set_location(yylloc);
1057 $$->invariant = true;
1059 $$->declarations.push_tail(&decl->link);
1063 fully_specified_type:
1067 $$ = new(ctx) ast_fully_specified_type();
1068 $$->set_location(yylloc);
1071 | type_qualifier type_specifier
1074 $$ = new(ctx) ast_fully_specified_type();
1075 $$->set_location(yylloc);
1082 LAYOUT_TOK '(' layout_qualifier_id_list ')'
1088 layout_qualifier_id_list:
1090 | layout_qualifier_id_list ',' layout_qualifier_id
1092 if (($1.flags.i & $3.flags.i) != 0) {
1093 _mesa_glsl_error(& @3, state,
1094 "duplicate layout qualifiers used\n");
1098 $$.flags.i = $1.flags.i | $3.flags.i;
1100 if ($1.flags.q.explicit_location)
1101 $$.location = $1.location;
1103 if ($3.flags.q.explicit_location)
1104 $$.location = $3.location;
1108 layout_qualifier_id:
1111 bool got_one = false;
1113 memset(& $$, 0, sizeof($$));
1115 /* Layout qualifiers for ARB_fragment_coord_conventions. */
1116 if (!got_one && state->ARB_fragment_coord_conventions_enable) {
1117 if (strcmp($1, "origin_upper_left") == 0) {
1119 $$.flags.q.origin_upper_left = 1;
1120 } else if (strcmp($1, "pixel_center_integer") == 0) {
1122 $$.flags.q.pixel_center_integer = 1;
1125 if (got_one && state->ARB_fragment_coord_conventions_warn) {
1126 _mesa_glsl_warning(& @1, state,
1127 "GL_ARB_fragment_coord_conventions layout "
1128 "identifier `%s' used\n", $1);
1132 /* Layout qualifiers for AMD/ARB_conservative_depth. */
1134 (state->AMD_conservative_depth_enable ||
1135 state->ARB_conservative_depth_enable)) {
1136 if (strcmp($1, "depth_any") == 0) {
1138 $$.flags.q.depth_any = 1;
1139 } else if (strcmp($1, "depth_greater") == 0) {
1141 $$.flags.q.depth_greater = 1;
1142 } else if (strcmp($1, "depth_less") == 0) {
1144 $$.flags.q.depth_less = 1;
1145 } else if (strcmp($1, "depth_unchanged") == 0) {
1147 $$.flags.q.depth_unchanged = 1;
1150 if (got_one && state->AMD_conservative_depth_warn) {
1151 _mesa_glsl_warning(& @1, state,
1152 "GL_AMD_conservative_depth "
1153 "layout qualifier `%s' is used\n", $1);
1155 if (got_one && state->ARB_conservative_depth_warn) {
1156 _mesa_glsl_warning(& @1, state,
1157 "GL_ARB_conservative_depth "
1158 "layout qualifier `%s' is used\n", $1);
1163 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1168 | any_identifier '=' INTCONSTANT
1170 bool got_one = false;
1172 memset(& $$, 0, sizeof($$));
1174 if (state->ARB_explicit_attrib_location_enable) {
1175 /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1176 * FINISHME: GLSL 1.30 (or later) are supported.
1178 if (strcmp("location", $1) == 0) {
1181 $$.flags.q.explicit_location = 1;
1186 _mesa_glsl_error(& @3, state,
1187 "invalid location %d specified\n", $3);
1193 /* If the identifier didn't match any known layout identifiers,
1197 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1200 } else if (state->ARB_explicit_attrib_location_warn) {
1201 _mesa_glsl_warning(& @1, state,
1202 "GL_ARB_explicit_attrib_location layout "
1203 "identifier `%s' used\n", $1);
1208 interpolation_qualifier:
1211 memset(& $$, 0, sizeof($$));
1212 $$.flags.q.smooth = 1;
1216 memset(& $$, 0, sizeof($$));
1217 $$.flags.q.flat = 1;
1221 memset(& $$, 0, sizeof($$));
1222 $$.flags.q.noperspective = 1;
1226 parameter_type_qualifier:
1229 memset(& $$, 0, sizeof($$));
1230 $$.flags.q.constant = 1;
1237 | layout_qualifier storage_qualifier
1240 $$.flags.i |= $2.flags.i;
1242 | interpolation_qualifier
1243 | interpolation_qualifier storage_qualifier
1246 $$.flags.i |= $2.flags.i;
1248 | INVARIANT storage_qualifier
1251 $$.flags.q.invariant = 1;
1253 | INVARIANT interpolation_qualifier storage_qualifier
1256 $$.flags.i |= $3.flags.i;
1257 $$.flags.q.invariant = 1;
1261 memset(& $$, 0, sizeof($$));
1262 $$.flags.q.invariant = 1;
1269 memset(& $$, 0, sizeof($$));
1270 $$.flags.q.constant = 1;
1274 memset(& $$, 0, sizeof($$));
1275 $$.flags.q.attribute = 1;
1279 memset(& $$, 0, sizeof($$));
1280 $$.flags.q.varying = 1;
1284 memset(& $$, 0, sizeof($$));
1285 $$.flags.q.centroid = 1;
1286 $$.flags.q.varying = 1;
1290 memset(& $$, 0, sizeof($$));
1295 memset(& $$, 0, sizeof($$));
1300 memset(& $$, 0, sizeof($$));
1301 $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1305 memset(& $$, 0, sizeof($$));
1306 $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1310 memset(& $$, 0, sizeof($$));
1311 $$.flags.q.uniform = 1;
1316 type_specifier_no_prec
1320 | precision_qualifier type_specifier_no_prec
1327 type_specifier_no_prec:
1328 type_specifier_nonarray
1329 | type_specifier_nonarray '[' ']'
1332 $$->is_array = true;
1333 $$->array_size = NULL;
1335 | type_specifier_nonarray '[' constant_expression ']'
1338 $$->is_array = true;
1339 $$->array_size = $3;
1343 type_specifier_nonarray:
1344 basic_type_specifier_nonarray
1347 $$ = new(ctx) ast_type_specifier($1);
1348 $$->set_location(yylloc);
1353 $$ = new(ctx) ast_type_specifier($1);
1354 $$->set_location(yylloc);
1359 $$ = new(ctx) ast_type_specifier($1);
1360 $$->set_location(yylloc);
1364 basic_type_specifier_nonarray:
1365 VOID_TOK { $$ = ast_void; }
1366 | FLOAT_TOK { $$ = ast_float; }
1367 | INT_TOK { $$ = ast_int; }
1368 | UINT_TOK { $$ = ast_uint; }
1369 | BOOL_TOK { $$ = ast_bool; }
1370 | VEC2 { $$ = ast_vec2; }
1371 | VEC3 { $$ = ast_vec3; }
1372 | VEC4 { $$ = ast_vec4; }
1373 | BVEC2 { $$ = ast_bvec2; }
1374 | BVEC3 { $$ = ast_bvec3; }
1375 | BVEC4 { $$ = ast_bvec4; }
1376 | IVEC2 { $$ = ast_ivec2; }
1377 | IVEC3 { $$ = ast_ivec3; }
1378 | IVEC4 { $$ = ast_ivec4; }
1379 | UVEC2 { $$ = ast_uvec2; }
1380 | UVEC3 { $$ = ast_uvec3; }
1381 | UVEC4 { $$ = ast_uvec4; }
1382 | MAT2X2 { $$ = ast_mat2; }
1383 | MAT2X3 { $$ = ast_mat2x3; }
1384 | MAT2X4 { $$ = ast_mat2x4; }
1385 | MAT3X2 { $$ = ast_mat3x2; }
1386 | MAT3X3 { $$ = ast_mat3; }
1387 | MAT3X4 { $$ = ast_mat3x4; }
1388 | MAT4X2 { $$ = ast_mat4x2; }
1389 | MAT4X3 { $$ = ast_mat4x3; }
1390 | MAT4X4 { $$ = ast_mat4; }
1391 | SAMPLER1D { $$ = ast_sampler1d; }
1392 | SAMPLER2D { $$ = ast_sampler2d; }
1393 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
1394 | SAMPLER3D { $$ = ast_sampler3d; }
1395 | SAMPLERCUBE { $$ = ast_samplercube; }
1396 | SAMPLEREXTERNALOES { $$ = ast_samplerexternaloes; }
1397 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
1398 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
1399 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
1400 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
1401 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
1402 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
1403 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
1404 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1405 | ISAMPLER1D { $$ = ast_isampler1d; }
1406 | ISAMPLER2D { $$ = ast_isampler2d; }
1407 | ISAMPLER3D { $$ = ast_isampler3d; }
1408 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1409 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1410 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1411 | USAMPLER1D { $$ = ast_usampler1d; }
1412 | USAMPLER2D { $$ = ast_usampler2d; }
1413 | USAMPLER3D { $$ = ast_usampler3d; }
1414 | USAMPLERCUBE { $$ = ast_usamplercube; }
1415 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1416 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1419 precision_qualifier:
1421 if (!state->es_shader && state->language_version < 130)
1422 _mesa_glsl_error(& @1, state,
1423 "precision qualifier forbidden "
1424 "in %s (1.30 or later "
1426 state->version_string);
1428 $$ = ast_precision_high;
1431 if (!state->es_shader && state->language_version < 130)
1432 _mesa_glsl_error(& @1, state,
1433 "precision qualifier forbidden "
1434 "in %s (1.30 or later "
1436 state->version_string);
1438 $$ = ast_precision_medium;
1441 if (!state->es_shader && state->language_version < 130)
1442 _mesa_glsl_error(& @1, state,
1443 "precision qualifier forbidden "
1444 "in %s (1.30 or later "
1446 state->version_string);
1448 $$ = ast_precision_low;
1453 STRUCT any_identifier '{' struct_declaration_list '}'
1456 $$ = new(ctx) ast_struct_specifier($2, $4);
1457 $$->set_location(yylloc);
1458 state->symbols->add_type($2, glsl_type::void_type);
1460 | STRUCT '{' struct_declaration_list '}'
1463 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1464 $$->set_location(yylloc);
1468 struct_declaration_list:
1471 $$ = (ast_node *) $1;
1472 $1->link.self_link();
1474 | struct_declaration_list struct_declaration
1476 $$ = (ast_node *) $1;
1477 $$->link.insert_before(& $2->link);
1482 type_specifier struct_declarator_list ';'
1485 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1486 type->set_location(yylloc);
1488 type->specifier = $1;
1489 $$ = new(ctx) ast_declarator_list(type);
1490 $$->set_location(yylloc);
1492 $$->declarations.push_degenerate_list_at_head(& $2->link);
1496 struct_declarator_list:
1500 $1->link.self_link();
1502 | struct_declarator_list ',' struct_declarator
1505 $$->link.insert_before(& $3->link);
1513 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1514 $$->set_location(yylloc);
1515 state->symbols->add_variable(new(state) ir_variable(NULL, $1, ir_var_auto));
1517 | any_identifier '[' constant_expression ']'
1520 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1521 $$->set_location(yylloc);
1526 assignment_expression
1529 declaration_statement:
1533 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1536 compound_statement { $$ = (ast_node *) $1; }
1541 declaration_statement
1542 | expression_statement
1543 | selection_statement
1545 | iteration_statement
1553 $$ = new(ctx) ast_compound_statement(true, NULL);
1554 $$->set_location(yylloc);
1558 state->symbols->push_scope();
1563 $$ = new(ctx) ast_compound_statement(true, $3);
1564 $$->set_location(yylloc);
1565 state->symbols->pop_scope();
1569 statement_no_new_scope:
1570 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1574 compound_statement_no_new_scope:
1578 $$ = new(ctx) ast_compound_statement(false, NULL);
1579 $$->set_location(yylloc);
1581 | '{' statement_list '}'
1584 $$ = new(ctx) ast_compound_statement(false, $2);
1585 $$->set_location(yylloc);
1593 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1598 $$->link.self_link();
1600 | statement_list statement
1603 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1607 $$->link.insert_before(& $2->link);
1611 expression_statement:
1615 $$ = new(ctx) ast_expression_statement(NULL);
1616 $$->set_location(yylloc);
1621 $$ = new(ctx) ast_expression_statement($1);
1622 $$->set_location(yylloc);
1626 selection_statement:
1627 IF '(' expression ')' selection_rest_statement
1629 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1631 $$->set_location(yylloc);
1635 selection_rest_statement:
1636 statement ELSE statement
1638 $$.then_statement = $1;
1639 $$.else_statement = $3;
1643 $$.then_statement = $1;
1644 $$.else_statement = NULL;
1651 $$ = (ast_node *) $1;
1653 | fully_specified_type any_identifier '=' initializer
1656 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1657 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1658 decl->set_location(yylloc);
1659 declarator->set_location(yylloc);
1661 declarator->declarations.push_tail(&decl->link);
1667 * siwtch_statement grammar is based on the syntax described in the body
1668 * of the GLSL spec, not in it's appendix!!!
1671 SWITCH '(' expression ')' switch_body
1673 $$ = new(state) ast_switch_statement($3, $5);
1674 $$->set_location(yylloc);
1681 $$ = new(state) ast_switch_body(NULL);
1682 $$->set_location(yylloc);
1684 | '{' case_statement_list '}'
1686 $$ = new(state) ast_switch_body($2);
1687 $$->set_location(yylloc);
1694 $$ = new(state) ast_case_label($2);
1695 $$->set_location(yylloc);
1699 $$ = new(state) ast_case_label(NULL);
1700 $$->set_location(yylloc);
1707 ast_case_label_list *labels = new(state) ast_case_label_list();
1709 labels->labels.push_tail(& $1->link);
1711 $$->set_location(yylloc);
1713 | case_label_list case_label
1716 $$->labels.push_tail(& $2->link);
1721 case_label_list statement
1723 ast_case_statement *stmts = new(state) ast_case_statement($1);
1724 stmts->set_location(yylloc);
1726 stmts->stmts.push_tail(& $2->link);
1729 | case_statement statement
1732 $$->stmts.push_tail(& $2->link);
1736 case_statement_list:
1739 ast_case_statement_list *cases= new(state) ast_case_statement_list();
1740 cases->set_location(yylloc);
1742 cases->cases.push_tail(& $1->link);
1745 | case_statement_list case_statement
1748 $$->cases.push_tail(& $2->link);
1752 iteration_statement:
1753 WHILE '(' condition ')' statement_no_new_scope
1756 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1757 NULL, $3, NULL, $5);
1758 $$->set_location(yylloc);
1760 | DO statement WHILE '(' expression ')' ';'
1763 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1764 NULL, $5, NULL, $2);
1765 $$->set_location(yylloc);
1767 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1770 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1771 $3, $4.cond, $4.rest, $6);
1772 $$->set_location(yylloc);
1777 expression_statement
1778 | declaration_statement
1795 | conditionopt ';' expression
1802 // Grammar Note: No 'goto'. Gotos are not supported.
1807 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1808 $$->set_location(yylloc);
1813 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1814 $$->set_location(yylloc);
1819 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1820 $$->set_location(yylloc);
1822 | RETURN expression ';'
1825 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1826 $$->set_location(yylloc);
1828 | DISCARD ';' // Fragment shader only.
1831 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1832 $$->set_location(yylloc);
1836 external_declaration:
1837 function_definition { $$ = $1; }
1838 | declaration { $$ = $1; }
1839 | pragma_statement { $$ = NULL; }
1842 function_definition:
1843 function_prototype compound_statement_no_new_scope
1846 $$ = new(ctx) ast_function_definition();
1847 $$->set_location(yylloc);
1851 state->symbols->pop_scope();