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. */
1128 (state->AMD_conservative_depth_enable ||
1129 state->ARB_conservative_depth_enable)) {
1130 if (strcmp($1, "depth_any") == 0) {
1132 $$.flags.q.depth_any = 1;
1133 } else if (strcmp($1, "depth_greater") == 0) {
1135 $$.flags.q.depth_greater = 1;
1136 } else if (strcmp($1, "depth_less") == 0) {
1138 $$.flags.q.depth_less = 1;
1139 } else if (strcmp($1, "depth_unchanged") == 0) {
1141 $$.flags.q.depth_unchanged = 1;
1144 if (got_one && state->AMD_conservative_depth_warn) {
1145 _mesa_glsl_warning(& @1, state,
1146 "GL_AMD_conservative_depth "
1147 "layout qualifier `%s' is used\n", $1);
1149 if (got_one && state->ARB_conservative_depth_warn) {
1150 _mesa_glsl_warning(& @1, state,
1151 "GL_ARB_conservative_depth "
1152 "layout qualifier `%s' is used\n", $1);
1157 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1162 | any_identifier '=' INTCONSTANT
1164 bool got_one = false;
1166 memset(& $$, 0, sizeof($$));
1168 if (state->ARB_explicit_attrib_location_enable) {
1169 /* FINISHME: Handle 'index' once GL_ARB_blend_func_exteneded and
1170 * FINISHME: GLSL 1.30 (or later) are supported.
1172 if (strcmp("location", $1) == 0) {
1175 $$.flags.q.explicit_location = 1;
1180 _mesa_glsl_error(& @3, state,
1181 "invalid location %d specified\n", $3);
1187 /* If the identifier didn't match any known layout identifiers,
1191 _mesa_glsl_error(& @1, state, "unrecognized layout identifier "
1194 } else if (state->ARB_explicit_attrib_location_warn) {
1195 _mesa_glsl_warning(& @1, state,
1196 "GL_ARB_explicit_attrib_location layout "
1197 "identifier `%s' used\n", $1);
1202 interpolation_qualifier:
1205 memset(& $$, 0, sizeof($$));
1206 $$.flags.q.smooth = 1;
1210 memset(& $$, 0, sizeof($$));
1211 $$.flags.q.flat = 1;
1215 memset(& $$, 0, sizeof($$));
1216 $$.flags.q.noperspective = 1;
1220 parameter_type_qualifier:
1223 memset(& $$, 0, sizeof($$));
1224 $$.flags.q.constant = 1;
1231 | layout_qualifier storage_qualifier
1234 $$.flags.i |= $2.flags.i;
1236 | interpolation_qualifier
1237 | interpolation_qualifier storage_qualifier
1240 $$.flags.i |= $2.flags.i;
1242 | INVARIANT storage_qualifier
1245 $$.flags.q.invariant = 1;
1247 | INVARIANT interpolation_qualifier storage_qualifier
1250 $$.flags.i |= $3.flags.i;
1251 $$.flags.q.invariant = 1;
1255 memset(& $$, 0, sizeof($$));
1256 $$.flags.q.invariant = 1;
1263 memset(& $$, 0, sizeof($$));
1264 $$.flags.q.constant = 1;
1268 memset(& $$, 0, sizeof($$));
1269 $$.flags.q.attribute = 1;
1273 memset(& $$, 0, sizeof($$));
1274 $$.flags.q.varying = 1;
1278 memset(& $$, 0, sizeof($$));
1279 $$.flags.q.centroid = 1;
1280 $$.flags.q.varying = 1;
1284 memset(& $$, 0, sizeof($$));
1289 memset(& $$, 0, sizeof($$));
1294 memset(& $$, 0, sizeof($$));
1295 $$.flags.q.centroid = 1; $$.flags.q.in = 1;
1299 memset(& $$, 0, sizeof($$));
1300 $$.flags.q.centroid = 1; $$.flags.q.out = 1;
1304 memset(& $$, 0, sizeof($$));
1305 $$.flags.q.uniform = 1;
1310 type_specifier_no_prec
1314 | precision_qualifier type_specifier_no_prec
1321 type_specifier_no_prec:
1322 type_specifier_nonarray
1323 | type_specifier_nonarray '[' ']'
1326 $$->is_array = true;
1327 $$->array_size = NULL;
1329 | type_specifier_nonarray '[' constant_expression ']'
1332 $$->is_array = true;
1333 $$->array_size = $3;
1337 type_specifier_nonarray:
1338 basic_type_specifier_nonarray
1341 $$ = new(ctx) ast_type_specifier($1);
1342 $$->set_location(yylloc);
1347 $$ = new(ctx) ast_type_specifier($1);
1348 $$->set_location(yylloc);
1353 $$ = new(ctx) ast_type_specifier($1);
1354 $$->set_location(yylloc);
1358 basic_type_specifier_nonarray:
1359 VOID_TOK { $$ = ast_void; }
1360 | FLOAT_TOK { $$ = ast_float; }
1361 | INT_TOK { $$ = ast_int; }
1362 | UINT_TOK { $$ = ast_uint; }
1363 | BOOL_TOK { $$ = ast_bool; }
1364 | VEC2 { $$ = ast_vec2; }
1365 | VEC3 { $$ = ast_vec3; }
1366 | VEC4 { $$ = ast_vec4; }
1367 | BVEC2 { $$ = ast_bvec2; }
1368 | BVEC3 { $$ = ast_bvec3; }
1369 | BVEC4 { $$ = ast_bvec4; }
1370 | IVEC2 { $$ = ast_ivec2; }
1371 | IVEC3 { $$ = ast_ivec3; }
1372 | IVEC4 { $$ = ast_ivec4; }
1373 | UVEC2 { $$ = ast_uvec2; }
1374 | UVEC3 { $$ = ast_uvec3; }
1375 | UVEC4 { $$ = ast_uvec4; }
1376 | MAT2X2 { $$ = ast_mat2; }
1377 | MAT2X3 { $$ = ast_mat2x3; }
1378 | MAT2X4 { $$ = ast_mat2x4; }
1379 | MAT3X2 { $$ = ast_mat3x2; }
1380 | MAT3X3 { $$ = ast_mat3; }
1381 | MAT3X4 { $$ = ast_mat3x4; }
1382 | MAT4X2 { $$ = ast_mat4x2; }
1383 | MAT4X3 { $$ = ast_mat4x3; }
1384 | MAT4X4 { $$ = ast_mat4; }
1385 | SAMPLER1D { $$ = ast_sampler1d; }
1386 | SAMPLER2D { $$ = ast_sampler2d; }
1387 | SAMPLER2DRECT { $$ = ast_sampler2drect; }
1388 | SAMPLER3D { $$ = ast_sampler3d; }
1389 | SAMPLERCUBE { $$ = ast_samplercube; }
1390 | SAMPLEREXTERNALOES { $$ = ast_samplerexternaloes; }
1391 | SAMPLER1DSHADOW { $$ = ast_sampler1dshadow; }
1392 | SAMPLER2DSHADOW { $$ = ast_sampler2dshadow; }
1393 | SAMPLER2DRECTSHADOW { $$ = ast_sampler2drectshadow; }
1394 | SAMPLERCUBESHADOW { $$ = ast_samplercubeshadow; }
1395 | SAMPLER1DARRAY { $$ = ast_sampler1darray; }
1396 | SAMPLER2DARRAY { $$ = ast_sampler2darray; }
1397 | SAMPLER1DARRAYSHADOW { $$ = ast_sampler1darrayshadow; }
1398 | SAMPLER2DARRAYSHADOW { $$ = ast_sampler2darrayshadow; }
1399 | ISAMPLER1D { $$ = ast_isampler1d; }
1400 | ISAMPLER2D { $$ = ast_isampler2d; }
1401 | ISAMPLER3D { $$ = ast_isampler3d; }
1402 | ISAMPLERCUBE { $$ = ast_isamplercube; }
1403 | ISAMPLER1DARRAY { $$ = ast_isampler1darray; }
1404 | ISAMPLER2DARRAY { $$ = ast_isampler2darray; }
1405 | USAMPLER1D { $$ = ast_usampler1d; }
1406 | USAMPLER2D { $$ = ast_usampler2d; }
1407 | USAMPLER3D { $$ = ast_usampler3d; }
1408 | USAMPLERCUBE { $$ = ast_usamplercube; }
1409 | USAMPLER1DARRAY { $$ = ast_usampler1darray; }
1410 | USAMPLER2DARRAY { $$ = ast_usampler2darray; }
1413 precision_qualifier:
1415 if (!state->es_shader && state->language_version < 130)
1416 _mesa_glsl_error(& @1, state,
1417 "precision qualifier forbidden "
1418 "in %s (1.30 or later "
1420 state->version_string);
1422 $$ = ast_precision_high;
1425 if (!state->es_shader && state->language_version < 130)
1426 _mesa_glsl_error(& @1, state,
1427 "precision qualifier forbidden "
1428 "in %s (1.30 or later "
1430 state->version_string);
1432 $$ = ast_precision_medium;
1435 if (!state->es_shader && state->language_version < 130)
1436 _mesa_glsl_error(& @1, state,
1437 "precision qualifier forbidden "
1438 "in %s (1.30 or later "
1440 state->version_string);
1442 $$ = ast_precision_low;
1447 STRUCT any_identifier '{' struct_declaration_list '}'
1450 $$ = new(ctx) ast_struct_specifier($2, $4);
1451 $$->set_location(yylloc);
1452 state->symbols->add_type($2, glsl_type::void_type);
1454 | STRUCT '{' struct_declaration_list '}'
1457 $$ = new(ctx) ast_struct_specifier(NULL, $3);
1458 $$->set_location(yylloc);
1462 struct_declaration_list:
1465 $$ = (ast_node *) $1;
1466 $1->link.self_link();
1468 | struct_declaration_list struct_declaration
1470 $$ = (ast_node *) $1;
1471 $$->link.insert_before(& $2->link);
1476 type_specifier struct_declarator_list ';'
1479 ast_fully_specified_type *type = new(ctx) ast_fully_specified_type();
1480 type->set_location(yylloc);
1482 type->specifier = $1;
1483 $$ = new(ctx) ast_declarator_list(type);
1484 $$->set_location(yylloc);
1486 $$->declarations.push_degenerate_list_at_head(& $2->link);
1490 struct_declarator_list:
1494 $1->link.self_link();
1496 | struct_declarator_list ',' struct_declarator
1499 $$->link.insert_before(& $3->link);
1507 $$ = new(ctx) ast_declaration($1, false, NULL, NULL);
1508 $$->set_location(yylloc);
1509 state->symbols->add_variable(new(state) ir_variable(NULL, $1, ir_var_auto));
1511 | any_identifier '[' constant_expression ']'
1514 $$ = new(ctx) ast_declaration($1, true, $3, NULL);
1515 $$->set_location(yylloc);
1520 assignment_expression
1523 declaration_statement:
1527 // Grammar Note: labeled statements for SWITCH only; 'goto' is not
1530 compound_statement { $$ = (ast_node *) $1; }
1535 declaration_statement
1536 | expression_statement
1537 | selection_statement
1539 | iteration_statement
1547 $$ = new(ctx) ast_compound_statement(true, NULL);
1548 $$->set_location(yylloc);
1552 state->symbols->push_scope();
1557 $$ = new(ctx) ast_compound_statement(true, $3);
1558 $$->set_location(yylloc);
1559 state->symbols->pop_scope();
1563 statement_no_new_scope:
1564 compound_statement_no_new_scope { $$ = (ast_node *) $1; }
1568 compound_statement_no_new_scope:
1572 $$ = new(ctx) ast_compound_statement(false, NULL);
1573 $$->set_location(yylloc);
1575 | '{' statement_list '}'
1578 $$ = new(ctx) ast_compound_statement(false, $2);
1579 $$->set_location(yylloc);
1587 _mesa_glsl_error(& @1, state, "<nil> statement\n");
1592 $$->link.self_link();
1594 | statement_list statement
1597 _mesa_glsl_error(& @2, state, "<nil> statement\n");
1601 $$->link.insert_before(& $2->link);
1605 expression_statement:
1609 $$ = new(ctx) ast_expression_statement(NULL);
1610 $$->set_location(yylloc);
1615 $$ = new(ctx) ast_expression_statement($1);
1616 $$->set_location(yylloc);
1620 selection_statement:
1621 IF '(' expression ')' selection_rest_statement
1623 $$ = new(state) ast_selection_statement($3, $5.then_statement,
1625 $$->set_location(yylloc);
1629 selection_rest_statement:
1630 statement ELSE statement
1632 $$.then_statement = $1;
1633 $$.else_statement = $3;
1637 $$.then_statement = $1;
1638 $$.else_statement = NULL;
1645 $$ = (ast_node *) $1;
1647 | fully_specified_type any_identifier '=' initializer
1650 ast_declaration *decl = new(ctx) ast_declaration($2, false, NULL, $4);
1651 ast_declarator_list *declarator = new(ctx) ast_declarator_list($1);
1652 decl->set_location(yylloc);
1653 declarator->set_location(yylloc);
1655 declarator->declarations.push_tail(&decl->link);
1661 * siwtch_statement grammar is based on the syntax described in the body
1662 * of the GLSL spec, not in it's appendix!!!
1665 SWITCH '(' expression ')' switch_body
1667 $$ = new(state) ast_switch_statement($3, $5);
1674 $$ = new(state) ast_switch_body(NULL);
1675 $$->set_location(yylloc);
1677 | '{' case_statement_list '}'
1679 $$ = new(state) ast_switch_body($2);
1680 $$->set_location(yylloc);
1687 $$ = new(state) ast_case_label($2);
1691 $$ = new(state) ast_case_label(NULL);
1698 ast_case_label_list *labels = new(state) ast_case_label_list();
1700 labels->labels.push_tail(& $1->link);
1703 | case_label_list case_label
1706 $$->labels.push_tail(& $2->link);
1711 case_label_list statement
1713 ast_case_statement *stmts = new(state) ast_case_statement($1);
1715 stmts->stmts.push_tail(& $2->link);
1718 | case_statement statement
1721 $$->stmts.push_tail(& $2->link);
1725 case_statement_list:
1728 ast_case_statement_list *cases= new(state) ast_case_statement_list();
1730 cases->cases.push_tail(& $1->link);
1733 | case_statement_list case_statement
1736 $$->cases.push_tail(& $2->link);
1740 iteration_statement:
1741 WHILE '(' condition ')' statement_no_new_scope
1744 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_while,
1745 NULL, $3, NULL, $5);
1746 $$->set_location(yylloc);
1748 | DO statement WHILE '(' expression ')' ';'
1751 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_do_while,
1752 NULL, $5, NULL, $2);
1753 $$->set_location(yylloc);
1755 | FOR '(' for_init_statement for_rest_statement ')' statement_no_new_scope
1758 $$ = new(ctx) ast_iteration_statement(ast_iteration_statement::ast_for,
1759 $3, $4.cond, $4.rest, $6);
1760 $$->set_location(yylloc);
1765 expression_statement
1766 | declaration_statement
1783 | conditionopt ';' expression
1790 // Grammar Note: No 'goto'. Gotos are not supported.
1795 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_continue, NULL);
1796 $$->set_location(yylloc);
1801 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_break, NULL);
1802 $$->set_location(yylloc);
1807 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, NULL);
1808 $$->set_location(yylloc);
1810 | RETURN expression ';'
1813 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_return, $2);
1814 $$->set_location(yylloc);
1816 | DISCARD ';' // Fragment shader only.
1819 $$ = new(ctx) ast_jump_statement(ast_jump_statement::ast_discard, NULL);
1820 $$->set_location(yylloc);
1824 external_declaration:
1825 function_definition { $$ = $1; }
1826 | declaration { $$ = $1; }
1827 | pragma_statement { $$ = NULL; }
1830 function_definition:
1831 function_prototype compound_statement_no_new_scope
1834 $$ = new(ctx) ast_function_definition();
1835 $$->set_location(yylloc);
1839 state->symbols->pop_scope();