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;
268 supported = state->Const.GLSL_140;
275 state->language_version = $2;
276 state->version_string =
277 ralloc_asprintf(state, "GLSL%s %d.%02d",
278 state->es_shader ? " ES" : "",
279 state->language_version / 100,
280 state->language_version % 100);
283 _mesa_glsl_error(& @2, state, "%s is not supported. "
284 "Supported versions are: %s\n",
285 state->version_string,
286 state->supported_version_string);
293 | PRAGMA_DEBUG_OFF EOL
294 | PRAGMA_OPTIMIZE_ON EOL
295 | PRAGMA_OPTIMIZE_OFF EOL
296 | PRAGMA_INVARIANT_ALL EOL
298 if (state->language_version < 120) {
299 _mesa_glsl_warning(& @1, state,
300 "pragma `invariant(all)' not supported in %s",
301 state->version_string);
303 state->all_invariant = true;
308 extension_statement_list:
310 | extension_statement_list extension_statement
320 EXTENSION any_identifier COLON any_identifier EOL
322 if (!_mesa_glsl_process_extension($2, & @2, $4, & @4, state)) {
328 external_declaration_list:
331 /* FINISHME: The NULL test is required because pragmas are set to
332 * FINISHME: NULL. (See production rule for external_declaration.)
335 state->translation_unit.push_tail(& $1->link);
337 | external_declaration_list external_declaration
339 /* FINISHME: The NULL test is required because pragmas are set to
340 * FINISHME: NULL. (See production rule for external_declaration.)
343 state->translation_unit.push_tail(& $2->link);
356 $$ = new(ctx) ast_expression(ast_identifier, NULL, NULL, NULL);
357 $$->set_location(yylloc);
358 $$->primary_expression.identifier = $1;
363 $$ = new(ctx) ast_expression(ast_int_constant, NULL, NULL, NULL);
364 $$->set_location(yylloc);
365 $$->primary_expression.int_constant = $1;
370 $$ = new(ctx) ast_expression(ast_uint_constant, NULL, NULL, NULL);
371 $$->set_location(yylloc);
372 $$->primary_expression.uint_constant = $1;
377 $$ = new(ctx) ast_expression(ast_float_constant, NULL, NULL, NULL);
378 $$->set_location(yylloc);
379 $$->primary_expression.float_constant = $1;
384 $$ = new(ctx) ast_expression(ast_bool_constant, NULL, NULL, NULL);
385 $$->set_location(yylloc);
386 $$->primary_expression.bool_constant = $1;
396 | postfix_expression '[' integer_expression ']'
399 $$ = new(ctx) ast_expression(ast_array_index, $1, $3, NULL);
400 $$->set_location(yylloc);
406 | postfix_expression '.' any_identifier
409 $$ = new(ctx) ast_expression(ast_field_selection, $1, NULL, NULL);
410 $$->set_location(yylloc);
411 $$->primary_expression.identifier = $3;
413 | postfix_expression INC_OP
416 $$ = new(ctx) ast_expression(ast_post_inc, $1, NULL, NULL);
417 $$->set_location(yylloc);
419 | postfix_expression DEC_OP
422 $$ = new(ctx) ast_expression(ast_post_dec, $1, NULL, NULL);
423 $$->set_location(yylloc);
432 function_call_or_method
435 function_call_or_method:
436 function_call_generic
437 | postfix_expression '.' method_call_generic
440 $$ = new(ctx) ast_expression(ast_field_selection, $1, $3, NULL);
441 $$->set_location(yylloc);
445 function_call_generic:
446 function_call_header_with_parameters ')'
447 | function_call_header_no_parameters ')'
450 function_call_header_no_parameters:
451 function_call_header VOID_TOK
452 | function_call_header
455 function_call_header_with_parameters:
456 function_call_header assignment_expression
459 $$->set_location(yylloc);
460 $$->expressions.push_tail(& $2->link);
462 | function_call_header_with_parameters ',' assignment_expression
465 $$->set_location(yylloc);
466 $$->expressions.push_tail(& $3->link);
470 // Grammar Note: Constructors look like functions, but lexical
471 // analysis recognized most of them as keywords. They are now
472 // recognized through "type_specifier".
473 function_call_header:
474 function_identifier '('
481 $$ = new(ctx) ast_function_expression($1);
482 $$->set_location(yylloc);
484 | variable_identifier
487 ast_expression *callee = new(ctx) ast_expression($1);
488 $$ = new(ctx) ast_function_expression(callee);
489 $$->set_location(yylloc);
494 ast_expression *callee = new(ctx) ast_expression($1);
495 $$ = new(ctx) ast_function_expression(callee);
496 $$->set_location(yylloc);
501 method_call_header_with_parameters ')'
502 | method_call_header_no_parameters ')'
505 method_call_header_no_parameters:
506 method_call_header VOID_TOK
510 method_call_header_with_parameters:
511 method_call_header assignment_expression
514 $$->set_location(yylloc);
515 $$->expressions.push_tail(& $2->link);
517 | method_call_header_with_parameters ',' assignment_expression
520 $$->set_location(yylloc);
521 $$->expressions.push_tail(& $3->link);
525 // Grammar Note: Constructors look like methods, but lexical
526 // analysis recognized most of them as keywords. They are now
527 // recognized through "type_specifier".
529 variable_identifier '('
532 ast_expression *callee = new(ctx) ast_expression($1);
533 $$ = new(ctx) ast_function_expression(callee);
534 $$->set_location(yylloc);
538 // Grammar Note: No traditional style type casts.
541 | INC_OP unary_expression
544 $$ = new(ctx) ast_expression(ast_pre_inc, $2, NULL, NULL);
545 $$->set_location(yylloc);
547 | DEC_OP unary_expression
550 $$ = new(ctx) ast_expression(ast_pre_dec, $2, NULL, NULL);
551 $$->set_location(yylloc);
553 | unary_operator unary_expression
556 $$ = new(ctx) ast_expression($1, $2, NULL, NULL);
557 $$->set_location(yylloc);
561 // Grammar Note: No '*' or '&' unary ops. Pointers are not supported.
563 '+' { $$ = ast_plus; }
564 | '-' { $$ = ast_neg; }
565 | '!' { $$ = ast_logic_not; }
566 | '~' { $$ = ast_bit_not; }
569 multiplicative_expression:
571 | multiplicative_expression '*' unary_expression
574 $$ = new(ctx) ast_expression_bin(ast_mul, $1, $3);
575 $$->set_location(yylloc);
577 | multiplicative_expression '/' unary_expression
580 $$ = new(ctx) ast_expression_bin(ast_div, $1, $3);
581 $$->set_location(yylloc);
583 | multiplicative_expression '%' unary_expression
586 $$ = new(ctx) ast_expression_bin(ast_mod, $1, $3);
587 $$->set_location(yylloc);
592 multiplicative_expression
593 | additive_expression '+' multiplicative_expression
596 $$ = new(ctx) ast_expression_bin(ast_add, $1, $3);
597 $$->set_location(yylloc);
599 | additive_expression '-' multiplicative_expression
602 $$ = new(ctx) ast_expression_bin(ast_sub, $1, $3);
603 $$->set_location(yylloc);
609 | shift_expression LEFT_OP additive_expression
612 $$ = new(ctx) ast_expression_bin(ast_lshift, $1, $3);
613 $$->set_location(yylloc);
615 | shift_expression RIGHT_OP additive_expression
618 $$ = new(ctx) ast_expression_bin(ast_rshift, $1, $3);
619 $$->set_location(yylloc);
623 relational_expression:
625 | relational_expression '<' shift_expression
628 $$ = new(ctx) ast_expression_bin(ast_less, $1, $3);
629 $$->set_location(yylloc);
631 | relational_expression '>' shift_expression
634 $$ = new(ctx) ast_expression_bin(ast_greater, $1, $3);
635 $$->set_location(yylloc);
637 | relational_expression LE_OP shift_expression
640 $$ = new(ctx) ast_expression_bin(ast_lequal, $1, $3);
641 $$->set_location(yylloc);
643 | relational_expression GE_OP shift_expression
646 $$ = new(ctx) ast_expression_bin(ast_gequal, $1, $3);
647 $$->set_location(yylloc);
652 relational_expression
653 | equality_expression EQ_OP relational_expression
656 $$ = new(ctx) ast_expression_bin(ast_equal, $1, $3);
657 $$->set_location(yylloc);
659 | equality_expression NE_OP relational_expression
662 $$ = new(ctx) ast_expression_bin(ast_nequal, $1, $3);
663 $$->set_location(yylloc);
669 | and_expression '&' equality_expression
672 $$ = new(ctx) ast_expression_bin(ast_bit_and, $1, $3);
673 $$->set_location(yylloc);
677 exclusive_or_expression:
679 | exclusive_or_expression '^' and_expression
682 $$ = new(ctx) ast_expression_bin(ast_bit_xor, $1, $3);
683 $$->set_location(yylloc);
687 inclusive_or_expression:
688 exclusive_or_expression
689 | inclusive_or_expression '|' exclusive_or_expression
692 $$ = new(ctx) ast_expression_bin(ast_bit_or, $1, $3);
693 $$->set_location(yylloc);
697 logical_and_expression:
698 inclusive_or_expression
699 | logical_and_expression AND_OP inclusive_or_expression
702 $$ = new(ctx) ast_expression_bin(ast_logic_and, $1, $3);
703 $$->set_location(yylloc);
707 logical_xor_expression:
708 logical_and_expression
709 | logical_xor_expression XOR_OP logical_and_expression
712 $$ = new(ctx) ast_expression_bin(ast_logic_xor, $1, $3);
713 $$->set_location(yylloc);
717 logical_or_expression:
718 logical_xor_expression
719 | logical_or_expression OR_OP logical_xor_expression
722 $$ = new(ctx) ast_expression_bin(ast_logic_or, $1, $3);
723 $$->set_location(yylloc);
727 conditional_expression:
728 logical_or_expression
729 | logical_or_expression '?' expression ':' assignment_expression
732 $$ = new(ctx) ast_expression(ast_conditional, $1, $3, $5);
733 $$->set_location(yylloc);
737 assignment_expression:
738 conditional_expression
739 | unary_expression assignment_operator assignment_expression
742 $$ = new(ctx) ast_expression($2, $1, $3, NULL);
743 $$->set_location(yylloc);
748 '=' { $$ = ast_assign; }
749 | MUL_ASSIGN { $$ = ast_mul_assign; }
750 | DIV_ASSIGN { $$ = ast_div_assign; }
751 | MOD_ASSIGN { $$ = ast_mod_assign; }
752 | ADD_ASSIGN { $$ = ast_add_assign; }
753 | SUB_ASSIGN { $$ = ast_sub_assign; }
754 | LEFT_ASSIGN { $$ = ast_ls_assign; }
755 | RIGHT_ASSIGN { $$ = ast_rs_assign; }
756 | AND_ASSIGN { $$ = ast_and_assign; }
757 | XOR_ASSIGN { $$ = ast_xor_assign; }
758 | OR_ASSIGN { $$ = ast_or_assign; }
762 assignment_expression
766 | expression ',' assignment_expression
769 if ($1->oper != ast_sequence) {
770 $$ = new(ctx) ast_expression(ast_sequence, NULL, NULL, NULL);
771 $$->set_location(yylloc);
772 $$->expressions.push_tail(& $1->link);
777 $$->expressions.push_tail(& $3->link);
782 conditional_expression
786 function_prototype ';'
788 state->symbols->pop_scope();
791 | init_declarator_list ';'
795 | PRECISION precision_qualifier type_specifier_no_prec ';'
798 $3->is_precision_statement = true;
804 function_declarator ')'
809 | function_header_with_parameters
812 function_header_with_parameters:
813 function_header parameter_declaration
816 $$->parameters.push_tail(& $2->link);
818 | function_header_with_parameters ',' parameter_declaration
821 $$->parameters.push_tail(& $3->link);
826 fully_specified_type variable_identifier '('
829 $$ = new(ctx) ast_function();
830 $$->set_location(yylloc);
831 $$->return_type = $1;
834 state->symbols->add_function(new(state) ir_function($2));
835 state->symbols->push_scope();
839 parameter_declarator:
840 type_specifier any_identifier
843 $$ = new(ctx) ast_parameter_declarator();
844 $$->set_location(yylloc);
845 $$->type = new(ctx) ast_fully_specified_type();
846 $$->type->set_location(yylloc);
847 $$->type->specifier = $1;
850 | type_specifier any_identifier '[' constant_expression ']'
853 $$ = new(ctx) ast_parameter_declarator();
854 $$->set_location(yylloc);
855 $$->type = new(ctx) ast_fully_specified_type();
856 $$->type->set_location(yylloc);
857 $$->type->specifier = $1;
864 parameter_declaration:
865 parameter_type_qualifier parameter_qualifier parameter_declarator
867 $1.flags.i |= $2.flags.i;
870 $$->type->qualifier = $1;
872 | parameter_qualifier parameter_declarator
875 $$->type->qualifier = $1;
877 | parameter_type_qualifier parameter_qualifier parameter_type_specifier
880 $1.flags.i |= $2.flags.i;
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 = $3;
888 | parameter_qualifier parameter_type_specifier
891 $$ = new(ctx) ast_parameter_declarator();
892 $$->set_location(yylloc);
893 $$->type = new(ctx) ast_fully_specified_type();
894 $$->type->qualifier = $1;
895 $$->type->specifier = $2;
902 memset(& $$, 0, sizeof($$));
906 memset(& $$, 0, sizeof($$));
911 memset(& $$, 0, sizeof($$));
916 memset(& $$, 0, sizeof($$));
922 parameter_type_specifier:
926 init_declarator_list:
928 | init_declarator_list ',' any_identifier
931 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, NULL);
932 decl->set_location(yylloc);
935 $$->declarations.push_tail(&decl->link);
936 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
938 | init_declarator_list ',' any_identifier '[' ']'
941 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, NULL);
942 decl->set_location(yylloc);
945 $$->declarations.push_tail(&decl->link);
946 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
948 | init_declarator_list ',' any_identifier '[' constant_expression ']'
951 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, NULL);
952 decl->set_location(yylloc);
955 $$->declarations.push_tail(&decl->link);
956 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
958 | init_declarator_list ',' any_identifier '[' ']' '=' initializer
961 ast_declaration *decl = new(ctx) ast_declaration($3, true, NULL, $7);
962 decl->set_location(yylloc);
965 $$->declarations.push_tail(&decl->link);
966 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
968 | init_declarator_list ',' any_identifier '[' constant_expression ']' '=' initializer
971 ast_declaration *decl = new(ctx) ast_declaration($3, true, $5, $8);
972 decl->set_location(yylloc);
975 $$->declarations.push_tail(&decl->link);
976 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
978 | init_declarator_list ',' any_identifier '=' initializer
981 ast_declaration *decl = new(ctx) ast_declaration($3, false, NULL, $5);
982 decl->set_location(yylloc);
985 $$->declarations.push_tail(&decl->link);
986 state->symbols->add_variable(new(state) ir_variable(NULL, $3, ir_var_auto));
990 // Grammar Note: No 'enum', or 'typedef'.
995 /* Empty declaration list is valid. */
996 $$ = new(ctx) ast_declarator_list($1);
997 $$->set_location(yylloc);
999 | fully_specified_type any_identifier
1002 ast_declaration *decl = new(ctx) ast_declaration($2, false, 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 '[' ']'
1011 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, NULL);
1013 $$ = new(ctx) ast_declarator_list($1);
1014 $$->set_location(yylloc);
1015 $$->declarations.push_tail(&decl->link);
1017 | fully_specified_type any_identifier '[' constant_expression ']'
1020 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, NULL);
1022 $$ = new(ctx) ast_declarator_list($1);
1023 $$->set_location(yylloc);
1024 $$->declarations.push_tail(&decl->link);
1026 | fully_specified_type any_identifier '[' ']' '=' initializer
1029 ast_declaration *decl = new(ctx) ast_declaration($2, true, NULL, $6);
1031 $$ = new(ctx) ast_declarator_list($1);
1032 $$->set_location(yylloc);
1033 $$->declarations.push_tail(&decl->link);
1035 | fully_specified_type any_identifier '[' constant_expression ']' '=' initializer
1038 ast_declaration *decl = new(ctx) ast_declaration($2, true, $4, $7);
1040 $$ = new(ctx) ast_declarator_list($1);
1041 $$->set_location(yylloc);
1042 $$->declarations.push_tail(&decl->link);
1044 | fully_specified_type any_identifier '=' initializer
1047 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1049 $$ = new(ctx) ast_declarator_list($1);
1050 $$->set_location(yylloc);
1051 $$->declarations.push_tail(&decl->link);
1053 | INVARIANT variable_identifier // Vertex only.
1056 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, NULL);
1058 $$ = new(ctx) ast_declarator_list(NULL);
1059 $$->set_location(yylloc);
1060 $$->invariant = true;
1062 $$->declarations.push_tail(&decl->link);
1066 fully_specified_type:
1070 $$ = new(ctx) ast_fully_specified_type();
1071 $$->set_location(yylloc);
1074 | type_qualifier type_specifier
1077 $$ = new(ctx) ast_fully_specified_type();
1078 $$->set_location(yylloc);
1085 LAYOUT_TOK '(' layout_qualifier_id_list ')'
1091 layout_qualifier_id_list:
1093 | layout_qualifier_id_list ',' layout_qualifier_id
1095 if (($1.flags.i & $3.flags.i) != 0) {
1096 _mesa_glsl_error(& @3, state,
1097 "duplicate layout qualifiers used\n");
1101 $$.flags.i = $1.flags.i | $3.flags.i;
1103 if ($1.flags.q.explicit_location)
1104 $$.location = $1.location;
1106 if ($3.flags.q.explicit_location)
1107 $$.location = $3.location;
1111 layout_qualifier_id:
1114 bool got_one = false;
1116 memset(& $$, 0, sizeof($$));
1118 /* Layout qualifiers for ARB_fragment_coord_conventions. */
1119 if (!got_one && state->ARB_fragment_coord_conventions_enable) {
1120 if (strcmp($1, "origin_upper_left") == 0) {
1122 $$.flags.q.origin_upper_left = 1;
1123 } else if (strcmp($1, "pixel_center_integer") == 0) {
1125 $$.flags.q.pixel_center_integer = 1;
1128 if (got_one && state->ARB_fragment_coord_conventions_warn) {
1129 _mesa_glsl_warning(& @1, state,
1130 "GL_ARB_fragment_coord_conventions layout "
1131 "identifier `%s' used\n", $1);
1135 /* Layout qualifiers for AMD/ARB_conservative_depth. */
1137 (state->AMD_conservative_depth_enable ||
1138 state->ARB_conservative_depth_enable)) {
1139 if (strcmp($1, "depth_any") == 0) {
1141 $$.flags.q.depth_any = 1;
1142 } else if (strcmp($1, "depth_greater") == 0) {
1144 $$.flags.q.depth_greater = 1;
1145 } else if (strcmp($1, "depth_less") == 0) {
1147 $$.flags.q.depth_less = 1;
1148 } else if (strcmp($1, "depth_unchanged") == 0) {
1150 $$.flags.q.depth_unchanged = 1;
1153 if (got_one && state->AMD_conservative_depth_warn) {
1154 _mesa_glsl_warning(& @1, state,
1155 "GL_AMD_conservative_depth "
1156 "layout qualifier `%s' is used\n", $1);
1158 if (got_one && state->ARB_conservative_depth_warn) {
1159 _mesa_glsl_warning(& @1, state,
1160 "GL_ARB_conservative_depth "
1161 "layout qualifier `%s' is used\n", $1);
1166 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1171 | any_identifier '=' INTCONSTANT
1173 bool got_one = false;
1175 memset(& $$, 0, sizeof($$));
1177 if (state->ARB_explicit_attrib_location_enable) {
1178 /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1179 * FINISHME: GLSL 1.30 (or later) are supported.
1181 if (strcmp("location", $1) == 0) {
1184 $$.flags.q.explicit_location = 1;
1189 _mesa_glsl_error(& @3, state,
1190 "invalid location %d specified\n", $3);
1196 /* If the identifier didn't match any known layout identifiers,
1200 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1203 } else if (state->ARB_explicit_attrib_location_warn) {
1204 _mesa_glsl_warning(& @1, state,
1205 "GL_ARB_explicit_attrib_location layout "
1206 "identifier `%s' used\n", $1);
1211 interpolation_qualifier:
1214 memset(& $$, 0, sizeof($$));
1215 $$.flags.q.smooth = 1;
1219 memset(& $$, 0, sizeof($$));
1220 $$.flags.q.flat = 1;
1224 memset(& $$, 0, sizeof($$));
1225 $$.flags.q.noperspective = 1;
1229 parameter_type_qualifier:
1232 memset(& $$, 0, sizeof($$));
1233 $$.flags.q.constant = 1;
1240 | layout_qualifier storage_qualifier
1243 $$.flags.i |= $2.flags.i;
1245 | interpolation_qualifier
1246 | interpolation_qualifier storage_qualifier
1249 $$.flags.i |= $2.flags.i;
1251 | INVARIANT storage_qualifier
1254 $$.flags.q.invariant = 1;
1256 | INVARIANT interpolation_qualifier storage_qualifier
1259 $$.flags.i |= $3.flags.i;
1260 $$.flags.q.invariant = 1;
1264 memset(& $$, 0, sizeof($$));
1265 $$.flags.q.invariant = 1;
1272 memset(& $$, 0, sizeof($$));
1273 $$.flags.q.constant = 1;
1277 memset(& $$, 0, sizeof($$));
1278 $$.flags.q.attribute = 1;
1282 memset(& $$, 0, sizeof($$));
1283 $$.flags.q.varying = 1;
1287 memset(& $$, 0, sizeof($$));
1288 $$.flags.q.centroid = 1;
1289 $$.flags.q.varying = 1;
1293 memset(& $$, 0, sizeof($$));
1298 memset(& $$, 0, sizeof($$));
1303 memset(& $$, 0, sizeof($$));
1304 $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1308 memset(& $$, 0, sizeof($$));
1309 $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1313 memset(& $$, 0, sizeof($$));
1314 $$.flags.q.uniform = 1;
1319 type_specifier_no_prec
1323 | precision_qualifier type_specifier_no_prec
1330 type_specifier_no_prec:
1331 type_specifier_nonarray
1332 | type_specifier_nonarray '[' ']'
1335 $$->is_array = true;
1336 $$->array_size = NULL;
1338 | type_specifier_nonarray '[' constant_expression ']'
1341 $$->is_array = true;
1342 $$->array_size = $3;
1346 type_specifier_nonarray:
1347 basic_type_specifier_nonarray
1350 $$ = new(ctx) ast_type_specifier($1);
1351 $$->set_location(yylloc);
1356 $$ = new(ctx) ast_type_specifier($1);
1357 $$->set_location(yylloc);
1362 $$ = new(ctx) ast_type_specifier($1);
1363 $$->set_location(yylloc);
1367 basic_type_specifier_nonarray:
1368 VOID_TOK { $$ = ast_void; }
1369 | FLOAT_TOK { $$ = ast_float; }
1370 | INT_TOK { $$ = ast_int; }
1371 | UINT_TOK { $$ = ast_uint; }
1372 | BOOL_TOK { $$ = ast_bool; }
1373 | VEC2 { $$ = ast_vec2; }
1374 | VEC3 { $$ = ast_vec3; }
1375 | VEC4 { $$ = ast_vec4; }
1376 | BVEC2 { $$ = ast_bvec2; }
1377 | BVEC3 { $$ = ast_bvec3; }
1378 | BVEC4 { $$ = ast_bvec4; }
1379 | IVEC2 { $$ = ast_ivec2; }
1380 | IVEC3 { $$ = ast_ivec3; }
1381 | IVEC4 { $$ = ast_ivec4; }
1382 | UVEC2 { $$ = ast_uvec2; }
1383 | UVEC3 { $$ = ast_uvec3; }
1384 | UVEC4 { $$ = ast_uvec4; }
1385 | MAT2X2 { $$ = ast_mat2; }
1386 | MAT2X3 { $$ = ast_mat2x3; }
1387 | MAT2X4 { $$ = ast_mat2x4; }
1388 | MAT3X2 { $$ = ast_mat3x2; }
1389 | MAT3X3 { $$ = ast_mat3; }
1390 | MAT3X4 { $$ = ast_mat3x4; }
1391 | MAT4X2 { $$ = ast_mat4x2; }
1392 | MAT4X3 { $$ = ast_mat4x3; }
1393 | MAT4X4 { $$ = ast_mat4; }
1394 | SAMPLER1D { $$ = ast_sampler1d; }
1395 | SAMPLER2D { $$ = ast_sampler2d; }
1396 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
1397 | SAMPLER3D { $$ = ast_sampler3d; }
1398 | SAMPLERCUBE { $$ = ast_samplercube; }
1399 | SAMPLEREXTERNALOES { $$ = ast_samplerexternaloes; }
1400 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
1401 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
1402 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
1403 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
1404 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
1405 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
1406 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
1407 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1408 | ISAMPLER1D { $$ = ast_isampler1d; }
1409 | ISAMPLER2D { $$ = ast_isampler2d; }
1410 | ISAMPLER3D { $$ = ast_isampler3d; }
1411 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1412 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1413 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1414 | USAMPLER1D { $$ = ast_usampler1d; }
1415 | USAMPLER2D { $$ = ast_usampler2d; }
1416 | USAMPLER3D { $$ = ast_usampler3d; }
1417 | USAMPLERCUBE { $$ = ast_usamplercube; }
1418 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1419 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1422 precision_qualifier:
1424 if (!state->es_shader && state->language_version < 130)
1425 _mesa_glsl_error(& @1, state,
1426 "precision qualifier forbidden "
1427 "in %s (1.30 or later "
1429 state->version_string);
1431 $$ = ast_precision_high;
1434 if (!state->es_shader && state->language_version < 130)
1435 _mesa_glsl_error(& @1, state,
1436 "precision qualifier forbidden "
1437 "in %s (1.30 or later "
1439 state->version_string);
1441 $$ = ast_precision_medium;
1444 if (!state->es_shader && state->language_version < 130)
1445 _mesa_glsl_error(& @1, state,
1446 "precision qualifier forbidden "
1447 "in %s (1.30 or later "
1449 state->version_string);
1451 $$ = ast_precision_low;
1456 STRUCT any_identifier '{' struct_declaration_list '}'
1459 $$ = new(ctx) ast_struct_specifier($2, $4);
1460 $$->set_location(yylloc);
1461 state->symbols->add_type($2, glsl_type::void_type);
1463 | STRUCT '{' struct_declaration_list '}'
1466 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1467 $$->set_location(yylloc);
1471 struct_declaration_list:
1474 $$ = (ast_node *) $1;
1475 $1->link.self_link();
1477 | struct_declaration_list struct_declaration
1479 $$ = (ast_node *) $1;
1480 $$->link.insert_before(& $2->link);
1485 type_specifier struct_declarator_list ';'
1488 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1489 type->set_location(yylloc);
1491 type->specifier = $1;
1492 $$ = new(ctx) ast_declarator_list(type);
1493 $$->set_location(yylloc);
1495 $$->declarations.push_degenerate_list_at_head(& $2->link);
1499 struct_declarator_list:
1503 $1->link.self_link();
1505 | struct_declarator_list ',' struct_declarator
1508 $$->link.insert_before(& $3->link);
1516 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1517 $$->set_location(yylloc);
1518 state->symbols->add_variable(new(state) ir_variable(NULL, $1, ir_var_auto));
1520 | any_identifier '[' constant_expression ']'
1523 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1524 $$->set_location(yylloc);
1529 assignment_expression
1532 declaration_statement:
1536 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1539 compound_statement { $$ = (ast_node *) $1; }
1544 declaration_statement
1545 | expression_statement
1546 | selection_statement
1548 | iteration_statement
1556 $$ = new(ctx) ast_compound_statement(true, NULL);
1557 $$->set_location(yylloc);
1561 state->symbols->push_scope();
1566 $$ = new(ctx) ast_compound_statement(true, $3);
1567 $$->set_location(yylloc);
1568 state->symbols->pop_scope();
1572 statement_no_new_scope:
1573 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1577 compound_statement_no_new_scope:
1581 $$ = new(ctx) ast_compound_statement(false, NULL);
1582 $$->set_location(yylloc);
1584 | '{' statement_list '}'
1587 $$ = new(ctx) ast_compound_statement(false, $2);
1588 $$->set_location(yylloc);
1596 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1601 $$->link.self_link();
1603 | statement_list statement
1606 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1610 $$->link.insert_before(& $2->link);
1614 expression_statement:
1618 $$ = new(ctx) ast_expression_statement(NULL);
1619 $$->set_location(yylloc);
1624 $$ = new(ctx) ast_expression_statement($1);
1625 $$->set_location(yylloc);
1629 selection_statement:
1630 IF '(' expression ')' selection_rest_statement
1632 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1634 $$->set_location(yylloc);
1638 selection_rest_statement:
1639 statement ELSE statement
1641 $$.then_statement = $1;
1642 $$.else_statement = $3;
1646 $$.then_statement = $1;
1647 $$.else_statement = NULL;
1654 $$ = (ast_node *) $1;
1656 | fully_specified_type any_identifier '=' initializer
1659 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1660 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1661 decl->set_location(yylloc);
1662 declarator->set_location(yylloc);
1664 declarator->declarations.push_tail(&decl->link);
1670 * siwtch_statement grammar is based on the syntax described in the body
1671 * of the GLSL spec, not in it's appendix!!!
1674 SWITCH '(' expression ')' switch_body
1676 $$ = new(state) ast_switch_statement($3, $5);
1677 $$->set_location(yylloc);
1684 $$ = new(state) ast_switch_body(NULL);
1685 $$->set_location(yylloc);
1687 | '{' case_statement_list '}'
1689 $$ = new(state) ast_switch_body($2);
1690 $$->set_location(yylloc);
1697 $$ = new(state) ast_case_label($2);
1698 $$->set_location(yylloc);
1702 $$ = new(state) ast_case_label(NULL);
1703 $$->set_location(yylloc);
1710 ast_case_label_list *labels = new(state) ast_case_label_list();
1712 labels->labels.push_tail(& $1->link);
1714 $$->set_location(yylloc);
1716 | case_label_list case_label
1719 $$->labels.push_tail(& $2->link);
1724 case_label_list statement
1726 ast_case_statement *stmts = new(state) ast_case_statement($1);
1727 stmts->set_location(yylloc);
1729 stmts->stmts.push_tail(& $2->link);
1732 | case_statement statement
1735 $$->stmts.push_tail(& $2->link);
1739 case_statement_list:
1742 ast_case_statement_list *cases= new(state) ast_case_statement_list();
1743 cases->set_location(yylloc);
1745 cases->cases.push_tail(& $1->link);
1748 | case_statement_list case_statement
1751 $$->cases.push_tail(& $2->link);
1755 iteration_statement:
1756 WHILE '(' condition ')' statement_no_new_scope
1759 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1760 NULL, $3, NULL, $5);
1761 $$->set_location(yylloc);
1763 | DO statement WHILE '(' expression ')' ';'
1766 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1767 NULL, $5, NULL, $2);
1768 $$->set_location(yylloc);
1770 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1773 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1774 $3, $4.cond, $4.rest, $6);
1775 $$->set_location(yylloc);
1780 expression_statement
1781 | declaration_statement
1798 | conditionopt ';' expression
1805 // Grammar Note: No 'goto'. Gotos are not supported.
1810 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1811 $$->set_location(yylloc);
1816 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1817 $$->set_location(yylloc);
1822 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1823 $$->set_location(yylloc);
1825 | RETURN expression ';'
1828 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1829 $$->set_location(yylloc);
1831 | DISCARD ';' // Fragment shader only.
1834 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1835 $$->set_location(yylloc);
1839 external_declaration:
1840 function_definition { $$ = $1; }
1841 | declaration { $$ = $1; }
1842 | pragma_statement { $$ = NULL; }
1845 function_definition:
1846 function_prototype compound_statement_no_new_scope
1849 $$ = new(ctx) ast_function_definition();
1850 $$->set_location(yylloc);
1854 state->symbols->pop_scope();