1 /* Source code parsing and tree node generation for the GNU compiler
2 for the Java(TM) language.
3 Copyright (C) 1997, 1998 Free Software Foundation, Inc.
4 Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.
23 Java and all Java-based marks are trademarks or registered trademarks
24 of Sun Microsystems, Inc. in the United States and other countries.
25 The Free Software Foundation is independent of Sun Microsystems, Inc. */
27 /* This file parses java source code and issues a tree node image
28 suitable for code generation (byte code and targeted CPU assembly
31 The grammar conforms to the Java grammar described in "The Java(TM)
32 Language Specification. J. Gosling, B. Joy, G. Steele. Addison Wesley
33 1996, ISBN 0-201-63451-1"
35 The following modifications were brought to the original grammar:
37 method_body: added the rule '| block SC_TK'
38 constructor_declaration: added two rules to accept SC_TK.
39 static_initializer: added the rule 'static block SC_TK'.
41 Note: All the extra rules described above should go away when the
42 empty_statement rule will work.
44 statement_nsi: 'nsi' should be read no_short_if.
46 Some rules have been modified to support JDK1.1 inner classes
47 definitions and other extensions. */
65 #include "java-tree.h"
71 /* Number of error found so far. */
73 /* Number of warning found so far. */
74 int java_warning_count;
76 /* The current parser context */
77 static struct parser_ctxt *ctxp;
79 /* binop_lookup maps token to tree_code. It is used where binary
80 operations are involved and required by the parser. RDIV_EXPR
81 covers both integral/floating point division. The code is changed
82 once the type of both operator is worked out. */
84 static enum tree_code binop_lookup[19] =
86 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
87 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
88 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
89 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
90 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
92 #define BINOP_LOOKUP(VALUE) \
93 binop_lookup [((VALUE) - PLUS_TK)% \
94 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
96 /* Fake WFL used to report error message. It is initialized once if
97 needed and reused with it's location information is overriden. */
98 static tree wfl_operator = NULL_TREE;
100 /* The "$L" identifier we use to create labels. */
101 static tree label_id;
116 /* Things defined here have to match the order of what's in the
117 binop_lookup table. */
119 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
120 %token LS_TK SRS_TK ZRS_TK
121 %token AND_TK XOR_TK OR_TK
122 %token BOOL_AND_TK BOOL_OR_TK
123 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
125 /* This maps to the same binop_lookup entry than the token above */
127 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
129 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
130 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
133 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
135 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
136 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
137 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
138 %token PAD_TK ABSTRACT_TK MODIFIER_TK
140 /* Keep those two in order, too */
141 %token DECR_TK INCR_TK
143 /* From now one, things can be in any order */
145 %token DEFAULT_TK IF_TK THROW_TK
146 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
147 %token THROWS_TK BREAK_TK IMPORT_TK
148 %token ELSE_TK INSTANCEOF_TK RETURN_TK
149 %token VOID_TK CATCH_TK INTERFACE_TK
150 %token CASE_TK EXTENDS_TK FINALLY_TK
151 %token SUPER_TK WHILE_TK CLASS_TK
152 %token SWITCH_TK CONST_TK TRY_TK
153 %token FOR_TK NEW_TK CONTINUE_TK
154 %token GOTO_TK PACKAGE_TK THIS_TK
156 %token BYTE_TK SHORT_TK INT_TK LONG_TK
157 %token CHAR_TK INTEGRAL_TK
159 %token FLOAT_TK DOUBLE_TK FP_TK
163 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
165 %token ASSIGN_ANY_TK ASSIGN_TK
166 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
168 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
169 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
171 %type <value> modifiers MODIFIER_TK
173 %type <node> super ID_TK identifier
174 %type <node> name simple_name qualified_name
175 %type <node> class_declaration type_declaration compilation_unit
176 field_declaration method_declaration extends_interfaces
177 interfaces interface_type_list
178 interface_declaration class_member_declaration
179 import_declarations package_declaration
180 type_declarations interface_body
181 interface_member_declaration constant_declaration
182 interface_member_declarations interface_type
183 abstract_method_declaration interface_type_list
184 %type <node> class_body_declaration class_member_declaration
185 static_initializer constructor_declaration block
186 %type <node> class_body_declarations
187 %type <node> class_or_interface_type class_type class_type_list
188 constructor_declarator explicit_constructor_invocation
189 %type <node> dim_expr dim_exprs this_or_super
191 %type <node> variable_declarator_id variable_declarator
192 variable_declarators variable_initializer
193 variable_initializers
195 %type <node> class_body
196 %type <node> block_statement local_variable_declaration_statement
197 block_statements local_variable_declaration
198 %type <node> statement statement_without_trailing_substatement
199 labeled_statement if_then_statement label_decl
200 if_then_else_statement while_statement for_statement
201 statement_nsi labeled_statement_nsi do_statement
202 if_then_else_statement_nsi while_statement_nsi
203 for_statement_nsi statement_expression_list for_init
204 for_update statement_expression expression_statement
205 primary_no_new_array expression primary
206 array_creation_expression array_type
207 class_instance_creation_expression field_access
208 method_invocation array_access something_dot_new
209 argument_list postfix_expression while_expression
210 post_increment_expression post_decrement_expression
211 unary_expression_not_plus_minus unary_expression
212 pre_increment_expression pre_decrement_expression
213 unary_expression_not_plus_minus cast_expression
214 multiplicative_expression additive_expression
215 shift_expression relational_expression
216 equality_expression and_expression
217 exclusive_or_expression inclusive_or_expression
218 conditional_and_expression conditional_or_expression
219 conditional_expression assignment_expression
220 left_hand_side assignment for_header for_begin
221 constant_expression do_statement_begin empty_statement
222 %type <node> return_statement break_statement continue_statement
224 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
225 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
226 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
227 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
228 %type <operator> ASSIGN_ANY_TK assignment_operator
229 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
230 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
231 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
232 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK
233 %token <operator> OP_TK OSB_TK DOT_TK
234 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
236 %type <node> method_body
238 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
239 STRING_LIT_TK NULL_TK VOID_TK
241 %type <node> IF_TK WHILE_TK FOR_TK
243 %type <node> formal_parameter_list formal_parameter
244 method_declarator method_header
246 %type <node> primitive_type reference_type type
247 BOOLEAN_TK INTEGRAL_TK FP_TK
250 /* 19.2 Production from 2.3: The Syntactic Grammar */
256 /* 19.3 Productions from 3: Lexical structure */
266 /* 19.4 Productions from 4: Types, Values and Variables */
279 class_or_interface_type
283 class_or_interface_type:
288 class_or_interface_type /* Default rule */
292 class_or_interface_type
296 primitive_type OSB_TK CSB_TK
298 $$ = build_java_array_type ($1, -1);
299 CLASS_LOADED_P ($$) = 1;
302 { $$ = build_unresolved_array_type ($1); }
303 | array_type OSB_TK CSB_TK
304 { $$ = build_unresolved_array_type ($1); }
305 | primitive_type OSB_TK error
306 {RULE ("']' expected"); RECOVER;}
307 | array_type OSB_TK error
308 {RULE ("']' expected"); RECOVER;}
311 /* 19.5 Productions from 6: Names */
313 simple_name /* Default rule */
314 | qualified_name /* Default rule */
318 identifier /* Default rule */
322 name DOT_TK identifier
323 { $$ = make_qualified_name ($1, $3, $2.location); }
330 /* 19.6: Production from 7: Packages */
333 | package_declaration
334 | import_declarations
336 | package_declaration import_declarations
337 | package_declaration type_declarations
338 | import_declarations type_declarations
339 | package_declaration import_declarations type_declarations
347 | import_declarations import_declaration
355 | type_declarations type_declaration
359 PACKAGE_TK name SC_TK
360 { ctxp->package = EXPR_WFL_NODE ($2); }
362 {yyerror ("Missing name"); RECOVER;}
363 | PACKAGE_TK name error
364 {yyerror ("';' expected"); RECOVER;}
368 single_type_import_declaration
369 | type_import_on_demand_declaration
372 single_type_import_declaration:
375 tree name = EXPR_WFL_NODE ($2), node, last_name;
376 int i = IDENTIFIER_LENGTH (name)-1;
377 char *last = &IDENTIFIER_POINTER (name)[i];
378 while (last != IDENTIFIER_POINTER (name))
384 last_name = get_identifier (++last);
385 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
387 tree err = find_name_in_single_imports (last_name);
388 if (err && err != name)
390 ($2, "Ambiguous class: `%s' and `%s'",
391 IDENTIFIER_POINTER (name),
392 IDENTIFIER_POINTER (err));
396 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name) = 1;
397 node = build_tree_list ($2, last_name);
398 TREE_CHAIN (node) = ctxp->import_list;
399 ctxp->import_list = node;
403 {yyerror ("Missing name"); RECOVER;}
404 | IMPORT_TK name error
405 {yyerror ("';' expected"); RECOVER;}
408 type_import_on_demand_declaration:
409 IMPORT_TK name DOT_TK MULT_TK SC_TK
411 tree name = EXPR_WFL_NODE ($2);
412 tree node = build_tree_list ($2, NULL_TREE);
413 if (!IS_AN_IMPORT_ON_DEMAND_P (name))
415 read_import_dir ($2);
416 IS_AN_IMPORT_ON_DEMAND_P (name) = 1;
418 TREE_CHAIN (node) = ctxp->import_demand_list;
419 ctxp->import_demand_list = node;
421 | IMPORT_TK name DOT_TK error
422 {yyerror ("'*' expected"); RECOVER;}
423 | IMPORT_TK name DOT_TK MULT_TK error
424 {yyerror ("';' expected"); RECOVER;}
430 maybe_generate_clinit ();
433 | interface_declaration
439 yyerror ("Class or interface declaration expected");
443 /* 19.7 Shortened from the original:
444 modifiers: modifier | modifiers modifier
445 modifier: any of public... */
451 | modifiers MODIFIER_TK
456 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
457 java_accstring_lookup (acc));
465 /* 19.8.1 Production from $8.1: Class Declaration */
467 modifiers CLASS_TK identifier super interfaces
468 { create_class ($1, $3, $4, $5); }
473 | CLASS_TK identifier super interfaces
474 { create_class (0, $2, $3, $4); }
479 | modifiers CLASS_TK error
480 {yyerror ("Missing class name"); RECOVER;}
482 {yyerror ("Missing class name"); RECOVER;}
483 | CLASS_TK identifier error
484 {if (!ctxp->class_err) yyerror ("'{' expected"); DRECOVER(class1);}
485 | modifiers CLASS_TK identifier error
486 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
491 | EXTENDS_TK class_type
493 | EXTENDS_TK class_type error
494 {yyerror ("'{' expected"); ctxp->class_err=1;}
496 {yyerror ("Missing super class name"); ctxp->class_err=1;}
501 | IMPLEMENTS_TK interface_type_list
503 | IMPLEMENTS_TK error
506 yyerror ("Missing interface name");
513 ctxp->interface_number = 1;
514 $$ = build_tree_list ($1, NULL_TREE);
516 | interface_type_list C_TK interface_type
518 ctxp->interface_number++;
519 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
521 | interface_type_list C_TK error
522 {yyerror ("Missing interface name"); RECOVER;}
527 { $$ = ctxp->current_parsed_class; }
528 | OCB_TK class_body_declarations CCB_TK
529 { $$ = ctxp->current_parsed_class; }
532 class_body_declarations:
533 class_body_declaration
534 | class_body_declarations class_body_declaration
537 class_body_declaration:
538 class_member_declaration
540 | constructor_declaration
541 | block /* Added, JDK1.1, instance initializer */
544 class_member_declaration:
547 | class_declaration /* Added, JDK1.1 inner classes */
548 | interface_declaration /* Added, JDK1.1 inner classes */
551 /* 19.8.2 Productions from 8.3: Field Declarations */
553 type variable_declarators SC_TK
554 { register_fields (0, $1, $2); }
555 | modifiers type variable_declarators SC_TK
560 ("Illegal modifier `%s' for field declaration",
561 $1, FIELD_MODIFIERS);
562 check_modifiers_consistency ($1);
563 register_fields ($1, $2, $3);
567 variable_declarators:
568 /* Should we use build_decl_list () instead ? FIXME */
569 variable_declarator /* Default rule */
570 | variable_declarators C_TK variable_declarator
571 { $$ = chainon ($1, $3); }
572 | variable_declarators C_TK error
573 {yyerror ("Missing term"); RECOVER;}
577 variable_declarator_id
578 { $$ = build_tree_list ($1, NULL_TREE); }
579 | variable_declarator_id ASSIGN_TK variable_initializer
581 if (java_error_count)
584 ($1, build_assignment ($2.token, $2.location, $1, $3));
586 | variable_declarator_id ASSIGN_TK error
588 yyerror ("Missing variable initializer");
589 $$ = build_tree_list ($1, NULL_TREE);
592 | variable_declarator_id ASSIGN_TK variable_initializer error
594 yyerror ("';' expected");
595 $$ = build_tree_list ($1, NULL_TREE);
600 variable_declarator_id:
602 | variable_declarator_id OSB_TK CSB_TK
604 $$ = NULL; /* FIXME */
607 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
608 | variable_declarator_id OSB_TK error
609 {yyerror ("']' expected"); DRECOVER(vdi);}
610 | variable_declarator_id CSB_TK error
611 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
614 variable_initializer:
620 /* 19.8.3 Productions from 8.4: Method Declarations */
624 current_function_decl = $1;
625 source_start_java_method (current_function_decl);
630 (DECL_FUNCTION_BODY (current_function_decl)) = $3;
631 maybe_absorb_scoping_blocks ();
632 exit_block (); /* Exit function's body. */
634 /* Merge last line of the function with first line,
635 directly in the function decl. It will be used to
636 emit correct debug info. */
637 DECL_SOURCE_LINE_MERGE (current_function_decl,
638 ctxp->last_ccb_indent1);
640 | method_header error
641 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
645 type method_declarator throws
646 { $$ = method_header (0, $1, $2, NULL); }
647 | VOID_TK method_declarator throws
648 { $$ = method_header (0, void_type_node, $2, NULL); }
649 | modifiers type method_declarator throws
650 { $$ = method_header ($1, $2, $3, NULL); }
651 | modifiers VOID_TK method_declarator throws
652 { $$ = method_header ($1, void_type_node, $3, NULL); }
655 | modifiers type error
658 {yyerror ("Identifier expected"); RECOVER;}
659 | modifiers VOID_TK error
660 {yyerror ("Identifier expected"); RECOVER;}
663 yyerror ("Invalid method declaration, return type required");
669 identifier OP_TK CP_TK
670 { $$ = method_declarator ($1, NULL_TREE); }
671 | identifier OP_TK formal_parameter_list CP_TK
672 { $$ = method_declarator ($1, $3); }
673 | method_declarator OSB_TK CSB_TK
675 /* Issue a warning here: obsolete declaration. FIXME */
676 $$ = NULL; /* FIXME */
678 | identifier OP_TK error
679 {yyerror ("')' expected"); DRECOVER(method_declarator);}
680 | method_declarator OSB_TK error
681 {yyerror ("']' expected"); RECOVER;}
684 formal_parameter_list:
687 ctxp->formal_parameter_number = 1;
689 | formal_parameter_list C_TK formal_parameter
691 ctxp->formal_parameter_number += 1;
692 $$ = chainon ($1, $3);
694 | formal_parameter_list C_TK error
695 {yyerror ("Missing formal parameter term"); RECOVER;}
699 type variable_declarator_id
701 $$ = build_tree_list ($2, $1);
703 | modifiers type variable_declarator_id /* Added, JDK1.1 final locals */
705 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
706 $$ = NULL; /* FIXME */
709 {yyerror ("Missing identifier"); RECOVER;}
710 | modifiers type error
712 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
713 yyerror ("Missing identifier"); RECOVER;
718 | THROWS_TK class_type_list
720 {yyerror ("Missing class type term"); RECOVER;}
725 | class_type_list C_TK class_type
726 | class_type_list C_TK error
727 {yyerror ("Missing class type term"); RECOVER;}
734 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
737 /* 19.8.4 Productions from 8.5: Static Initializers */
741 RULE ("STATIC_INITIALIZER");
743 | static block SC_TK /* Shouldn't be here. FIXME */
745 RULE ("STATIC_INITIALIZER");
749 static: /* Test lval.sub_token here */
752 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
756 /* 19.8.5 Productions from 8.6: Constructor Declarations */
757 /* NOTE FOR FURTHER WORK ON CONSTRUCTORS:
758 - If a forbidded modifier is found, the the error is either the use of
759 a forbidded modifier for a constructor OR bogus attempt to declare a
760 method without having specified the return type. FIXME */
761 constructor_declaration:
762 constructor_declarator throws constructor_body
764 RULE ("CONSTRUCTOR_DECLARATION");
766 | modifiers constructor_declarator throws constructor_body
768 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
769 RULE ("CONSTRUCTOR_DECLARATION (modifier)");
771 /* extra SC_TK, FIXME */
772 | constructor_declarator throws constructor_body SC_TK
774 RULE ("CONSTRUCTOR_DECLARATION");
776 /* extra SC_TK, FIXME */
777 | modifiers constructor_declarator throws constructor_body SC_TK
779 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
780 RULE ("CONSTRUCTOR_DECLARATION (modifier)");
782 /* I'm not happy with the SC_TK addition. It isn't in the grammer and should
783 probably be matched by and empty statement. But it doesn't work. FIXME */
786 constructor_declarator:
787 simple_name OP_TK CP_TK
788 | simple_name OP_TK formal_parameter_list CP_TK
793 | OCB_TK explicit_constructor_invocation CCB_TK
794 | OCB_TK block_statements CCB_TK
795 | OCB_TK explicit_constructor_invocation block_statements CCB_TK
798 /* Error recovery for that rule moved down expression_statement: rule. */
799 explicit_constructor_invocation:
800 this_or_super OP_TK CP_TK SC_TK
801 | this_or_super OP_TK argument_list CP_TK SC_TK
802 /* Added, JDK1.1 inner classes. Modified because the rule
803 'primary' couldn't work. */
804 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
805 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
806 {RULE ("explicit_constructor_invocation (X.super)");}
809 this_or_super: /* Added, simplifies error diagnostics */
812 tree wfl = build_wfl_node (this_identifier_node, input_filename, 0, 0);
813 EXPR_WFL_LINECOL (wfl) = $1.location;
818 tree wfl = build_wfl_node (super_identifier_node, input_filename, 0, 0);
819 EXPR_WFL_LINECOL (wfl) = $1.location;
824 /* 19.9 Productions from 9: Interfaces */
825 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
826 interface_declaration:
827 INTERFACE_TK identifier
828 { create_interface (0, $2, NULL_TREE); }
833 | modifiers INTERFACE_TK identifier
834 { create_interface ($1, $3, NULL_TREE); }
839 | INTERFACE_TK identifier extends_interfaces
840 { create_interface (0, $2, $3); }
845 | modifiers INTERFACE_TK identifier extends_interfaces
846 { create_interface ($1, $3, $4); }
851 | INTERFACE_TK identifier error
852 {yyerror ("(here)'{' expected"); RECOVER;}
853 | modifiers INTERFACE_TK identifier error
854 {yyerror ("(there)'{' expected"); RECOVER;}
858 EXTENDS_TK interface_type
860 ctxp->interface_number = 1;
861 $$ = build_tree_list ($2, NULL_TREE);
863 | extends_interfaces C_TK interface_type
865 ctxp->interface_number++;
866 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
869 {yyerror ("Invalid interface type"); RECOVER;}
870 | extends_interfaces C_TK error
871 {yyerror ("Missing term"); RECOVER;}
877 | OCB_TK interface_member_declarations CCB_TK
881 interface_member_declarations:
882 interface_member_declaration
883 | interface_member_declarations interface_member_declaration
886 interface_member_declaration:
888 | abstract_method_declaration
889 | class_declaration /* Added, JDK1.1 inner classes */
890 | interface_declaration /* Added, JDK1.1 inner classes */
893 constant_declaration:
897 abstract_method_declaration:
900 check_abstract_method_header ($1);
901 current_function_decl = NULL_TREE; /* FIXME ? */
903 | method_header error
904 {yyerror ("';' expected"); RECOVER;}
907 /* 19.10 Productions from 10: Arrays */
911 RULE ("ARRAY_INITIALIZER (empty)");
913 | OCB_TK variable_initializers CCB_TK
915 RULE ("ARRAY_INITIALIZER (variable)");
919 RULE ("ARRAY_INITIALIZER (,)");
921 | OCB_TK variable_initializers C_TK CCB_TK
923 RULE ("ARRAY_INITIALIZER (variable, ,)");
927 variable_initializers:
929 | variable_initializers C_TK variable_initializer
930 | variable_initializers C_TK error
931 {yyerror ("Missing term"); RECOVER;}
934 /* 19.11 Production from 14: Blocks and Statements */
937 { $$ = size_zero_node; }
943 maybe_absorb_scoping_blocks ();
950 | block_statements block_statement
954 local_variable_declaration_statement
956 { $$ = java_method_add_stmt (current_function_decl, $1); }
957 | class_declaration /* Added, JDK1.1 inner classes */
960 local_variable_declaration_statement:
961 local_variable_declaration SC_TK /* Can't catch missing ';' here */
964 local_variable_declaration:
965 type variable_declarators
966 { declare_local_variables (0, $1, $2); }
967 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
968 { declare_local_variables ($1, $2, $3); }
972 statement_without_trailing_substatement
974 { RULE ("STATEMENT (labeled)"); }
976 { RULE ("STATEMENT (if-then)"); }
977 | if_then_else_statement
978 { RULE ("STATEMENT (if-then-else)"); }
980 { RULE ("STATEMENT (while)"); }
983 /* If the for loop is unlabeled, we must return the
984 block it was defined it. It our last chance to
986 if (!LOOP_HAS_LABEL_P ($$))
992 statement_without_trailing_substatement
993 | labeled_statement_nsi
994 { RULE ("NSI STATEMENT (labeled)"); }
995 | if_then_else_statement_nsi
996 { RULE ("NSI STATEMENT (if-then-else)"); }
997 | while_statement_nsi
998 { RULE ("NSI STATEMENT (while)"); }
1000 { RULE ("NSI STATEMENT (for)"); }
1003 statement_without_trailing_substatement:
1005 { RULE ("STATEMENT (block)"); }
1007 { RULE ("STATEMENT (empty)"); }
1008 | expression_statement
1009 { RULE ("STATEMENT (expression)"); }
1011 { RULE ("STATEMENT (switch)"); }
1013 { RULE ("STATEMENT (do)"); }
1015 { RULE ("STATEMENT (break)"); }
1016 | continue_statement
1017 { RULE ("STATEMENT (continue)"); }
1019 | synchronized_statement
1020 { RULE ("STATEMENT (synchronized)"); }
1022 { RULE ("STATEMENT (throw)"); }
1024 { RULE ("STATEMENT (try)"); }
1029 { $$ = size_zero_node; }
1033 identifier REL_CL_TK
1035 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1036 EXPR_WFL_NODE ($1), $1);
1038 push_labeled_block ($$);
1039 PUSH_LABELED_BLOCK ($$);
1044 label_decl statement
1046 $$ = complete_labeled_statement ($1, $2);
1047 pop_labeled_block ();
1048 POP_LABELED_BLOCK ();
1051 {yyerror ("':' expected"); RECOVER;}
1054 labeled_statement_nsi:
1055 label_decl statement_nsi
1057 $$ = complete_labeled_statement ($1, $2);
1058 pop_labeled_block ();
1059 POP_LABELED_BLOCK ();
1063 /* We concentrate here a bunch of error handling rules that we couldn't write
1064 earlier, because expression_statement catches a missing ';'. */
1065 expression_statement:
1066 statement_expression SC_TK
1068 /* We have a statement. Generate a WFL around it so
1070 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1071 /* We know we have a statement, so set the debug
1072 info to be eventually generate here. */
1073 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1077 if (ctxp->prevent_ese != lineno)
1078 yyerror ("Invalid expression statement");
1079 DRECOVER (expr_stmt);
1083 if (ctxp->prevent_ese != lineno)
1084 yyerror ("Invalid expression statement");
1085 DRECOVER (expr_stmt);
1089 if (ctxp->prevent_ese != lineno)
1090 yyerror ("Invalid expression statement");
1091 DRECOVER (expr_stmt);
1093 | this_or_super OP_TK error
1094 {yyerror ("')' expected"); RECOVER;}
1095 | this_or_super OP_TK CP_TK error
1096 {yyerror ("';' expected"); RECOVER;}
1097 | this_or_super OP_TK argument_list error
1098 {yyerror ("')' expected"); RECOVER;}
1099 | this_or_super OP_TK argument_list CP_TK error
1100 {yyerror ("';' expected"); RECOVER;}
1101 | name DOT_TK SUPER_TK error
1102 {yyerror ("'(' expected"); RECOVER;}
1103 | name DOT_TK SUPER_TK OP_TK error
1104 {yyerror ("')' expected"); RECOVER;}
1105 | name DOT_TK SUPER_TK OP_TK argument_list error
1106 {yyerror ("')' expected"); RECOVER;}
1107 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1108 {yyerror ("';' expected"); RECOVER;}
1109 | name DOT_TK SUPER_TK OP_TK CP_TK error
1110 {yyerror ("';' expected"); RECOVER;}
1113 statement_expression:
1115 | pre_increment_expression
1117 RULE ("++INCREMENT");
1119 | pre_decrement_expression
1121 RULE ("--DECREMENT");
1123 | post_increment_expression
1125 RULE ("INCREMENT++");
1127 | post_decrement_expression
1129 RULE ("DECREMENT--");
1132 | class_instance_creation_expression
1134 RULE ("INSTANCE CREATION");
1139 IF_TK OP_TK expression CP_TK statement
1140 { $$ = build_if_else_statement ($2.location, $3, $5, NULL_TREE); }
1142 {yyerror ("'(' expected"); RECOVER;}
1144 {yyerror ("Missing term"); RECOVER;}
1145 | IF_TK OP_TK expression error
1146 {yyerror ("')' expected"); RECOVER;}
1149 if_then_else_statement:
1150 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1151 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1154 if_then_else_statement_nsi:
1155 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1156 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1160 SWITCH_TK OP_TK expression CP_TK switch_block
1162 {yyerror ("'(' expected"); RECOVER;}
1163 | SWITCH_TK OP_TK error
1164 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1165 | SWITCH_TK OP_TK expression CP_TK error
1166 {yyerror ("'{' expected"); RECOVER;}
1171 | OCB_TK switch_labels CCB_TK
1172 | OCB_TK switch_block_statement_groups CCB_TK
1173 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1176 switch_block_statement_groups:
1177 switch_block_statement_group
1178 | switch_block_statement_groups switch_block_statement_group
1181 switch_block_statement_group:
1182 switch_labels block_statements
1188 | switch_labels switch_label
1192 CASE_TK constant_expression REL_CL_TK
1193 | DEFAULT_TK REL_CL_TK
1195 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1196 | CASE_TK constant_expression error
1197 {yyerror ("':' expected"); RECOVER;}
1199 {yyerror ("':' expected"); RECOVER;}
1203 WHILE_TK OP_TK expression CP_TK
1205 tree body = build_loop_body ($2.location, $3, 0);
1206 $$ = build_new_loop (body);
1211 while_expression statement
1212 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1214 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1215 | WHILE_TK OP_TK error
1216 {yyerror ("Missing term and ')' expected"); RECOVER;}
1217 | WHILE_TK OP_TK expression error
1218 {yyerror ("')' expected"); RECOVER;}
1221 while_statement_nsi:
1222 while_expression statement_nsi
1223 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1229 tree body = build_loop_body (0, NULL_TREE, 1);
1230 $$ = build_new_loop (body);
1232 /* Need error handing here. FIXME */
1236 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1237 { $$ = complete_loop_body ($4.location, $5, $2, 1); }
1241 for_begin SC_TK expression SC_TK for_update CP_TK statement
1242 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1243 | for_begin SC_TK SC_TK for_update CP_TK statement
1245 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1246 /* We have not condition, so we get rid of the EXIT_EXPR */
1247 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1250 | for_begin SC_TK error
1251 {yyerror ("Invalid control expression"); RECOVER;}
1252 | for_begin SC_TK expression SC_TK error
1253 {yyerror ("Invalid update expression"); RECOVER;}
1254 | for_begin SC_TK SC_TK error
1255 {yyerror ("Invalid update expression"); RECOVER;}
1259 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1260 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1261 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1263 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1264 /* We have not condition, so we get rid of the EXIT_EXPR */
1265 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1273 /* This scope defined for local variable that may be
1274 defined within the scope of the for loop */
1278 {yyerror ("'(' expected"); DRECOVER(for_1);}
1279 | FOR_TK OP_TK error
1280 {yyerror ("Invalid init statement"); RECOVER;}
1286 /* We now declare the loop body. The loop is
1287 declared as a for loop. */
1288 tree body = build_loop_body (0, NULL_TREE, 0);
1289 $$ = build_new_loop (body);
1290 IS_FOR_LOOP_P ($$) = 1;
1291 /* The loop is added to the current block the for
1292 statement is defined within */
1293 java_method_add_stmt (current_function_decl, $$);
1296 for_init: /* Can be empty */
1297 { $$ = size_zero_node; }
1298 | statement_expression_list
1300 /* Init statement recorded within the previously
1301 defined block scope */
1302 $$ = java_method_add_stmt (current_function_decl, $1);
1304 | local_variable_declaration
1306 /* Local variable are recorded within the previously
1307 defined block scope */
1310 | statement_expression_list error
1311 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1314 for_update: /* Can be empty */
1315 {$$ = size_zero_node;}
1316 | statement_expression_list
1317 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1320 statement_expression_list:
1321 statement_expression
1322 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1323 | statement_expression_list C_TK statement_expression
1324 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1325 | statement_expression_list C_TK error
1326 {yyerror ("Missing term"); RECOVER;}
1331 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1332 | BREAK_TK identifier SC_TK
1333 { $$ = build_bc_statement ($1.location, 1, $2); }
1335 {yyerror ("Missing term"); RECOVER;}
1336 | BREAK_TK identifier error
1337 {yyerror ("';' expected"); RECOVER;}
1342 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1343 | CONTINUE_TK identifier SC_TK
1344 { $$ = build_bc_statement ($1.location, 0, $2); }
1346 {yyerror ("Missing term"); RECOVER;}
1347 | CONTINUE_TK identifier error
1348 {yyerror ("';' expected"); RECOVER;}
1353 { $$ = build_return ($1.location, NULL_TREE); }
1354 | RETURN_TK expression SC_TK
1355 { $$ = build_return ($1.location, $2); }
1357 {yyerror ("Missing term"); RECOVER;}
1358 | RETURN_TK expression error
1359 {yyerror ("';' expected"); RECOVER;}
1363 THROW_TK expression SC_TK
1365 {yyerror ("Missing term"); RECOVER;}
1366 | THROW_TK expression error
1367 {yyerror ("';' expected"); RECOVER;}
1370 synchronized_statement:
1371 synchronized OP_TK expression CP_TK block
1372 | synchronized OP_TK expression CP_TK error
1373 {yyerror ("'{' expected"); RECOVER;}
1374 | synchronized error
1375 {yyerror ("'(' expected"); RECOVER;}
1376 | synchronized OP_TK error CP_TK
1377 {yyerror ("Missing term"); RECOVER;}
1378 | synchronized OP_TK error
1379 {yyerror ("Missing term"); RECOVER;}
1382 synchronized: /* Test lval.sub_token here */
1385 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
1390 TRY_TK block catches
1391 | TRY_TK block finally
1392 | TRY_TK block catches finally
1394 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1399 | catches catch_clause
1403 CATCH_TK OP_TK formal_parameter CP_TK block
1405 {yyerror ("'(' expected"); RECOVER;}
1406 | CATCH_TK OP_TK error CP_TK /* That's for () */
1407 {yyerror ("Missing term"); DRECOVER (1);}
1408 | CATCH_TK OP_TK error
1409 {yyerror ("Missing term"); DRECOVER (2);}
1415 {yyerror ("'{' expected"); RECOVER; }
1418 /* 19.12 Production from 15: Expressions */
1420 primary_no_new_array
1421 | array_creation_expression
1424 primary_no_new_array:
1427 { $$ = build_this ($1.location); }
1428 | OP_TK expression CP_TK
1430 | class_instance_creation_expression
1434 /* type DOT_TK CLASS_TK doens't work. So we split the rule
1435 'type' into its components. Missing is something for array,
1436 which will complete the reference_type part. FIXME */
1437 | name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1438 | primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1439 | VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1440 /* Added, JDK1.1 inner classes. Documentation is wrong
1441 refering to a 'ClassName' (class_name) rule that doesn't
1442 exist. Used name instead. */
1443 | name DOT_TK THIS_TK
1444 | OP_TK expression error
1445 {yyerror ("')' expected"); RECOVER;}
1447 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1448 | primitive_type DOT_TK error
1449 {yyerror ("'class' expected" ); RECOVER;}
1450 | VOID_TK DOT_TK error
1451 {yyerror ("'class' expected" ); RECOVER;}
1454 class_instance_creation_expression:
1455 NEW_TK class_type OP_TK argument_list CP_TK
1457 $$ = build_method_invocation ($2, $4);
1458 TREE_SET_CODE ($$, JAVA_NEW_CLASS_EXPR);
1460 | NEW_TK class_type OP_TK CP_TK
1462 $$ = build_method_invocation ($2, NULL_TREE);
1463 TREE_SET_CODE ($$, JAVA_NEW_CLASS_EXPR);
1465 /* Added, JDK1.1 inner classes but modified to use
1466 'class_type' instead of 'TypeName' (type_name) mentionned
1467 in the documentation but doesn't exist. */
1468 | NEW_TK class_type OP_TK argument_list CP_TK class_body
1470 | NEW_TK class_type OP_TK CP_TK class_body
1472 /* Added, JDK1.1 inner classes, modified to use name or
1473 primary instead of primary solely which couldn't work in
1475 | something_dot_new identifier OP_TK CP_TK
1476 | something_dot_new identifier OP_TK CP_TK class_body
1477 | something_dot_new identifier OP_TK argument_list CP_TK
1478 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1479 | NEW_TK error SC_TK
1480 {yyerror ("'(' expected"); DRECOVER(new_1);}
1481 | NEW_TK class_type error
1482 {yyerror ("'(' expected"); RECOVER;}
1483 | NEW_TK class_type OP_TK error
1484 {yyerror ("')' or term expected"); RECOVER;}
1485 | NEW_TK class_type OP_TK argument_list error
1486 {yyerror ("')' expected"); RECOVER;}
1487 | something_dot_new error
1488 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1489 | something_dot_new identifier error
1490 {yyerror ("'(' expected"); RECOVER;}
1493 something_dot_new: /* Added, not part of the specs. */
1495 | primary DOT_TK NEW_TK
1501 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
1502 ctxp->formal_parameter_number = 1;
1504 | argument_list C_TK expression
1506 ctxp->formal_parameter_number += 1;
1507 $$ = tree_cons (NULL_TREE, $3, $1);
1509 | argument_list C_TK error
1510 {yyerror ("Missing term"); RECOVER;}
1513 array_creation_expression:
1514 NEW_TK primitive_type dim_exprs
1515 { $$ = build_newarray_node ($2, $3, 0); }
1516 | NEW_TK class_or_interface_type dim_exprs
1517 { $$ = build_newarray_node ($2, $3, 0); }
1518 | NEW_TK primitive_type dim_exprs dims
1519 { $$ = build_newarray_node ($2, $3, ctxp->osb_number); }
1520 | NEW_TK class_or_interface_type dim_exprs dims
1521 { $$ = build_newarray_node ($2, $3, ctxp->osb_number); }
1522 /* Added, JDK1.1 anonymous array. Initial documentation rule
1524 | NEW_TK class_or_interface_type dims array_initializer
1526 | NEW_TK primitive_type dims array_initializer
1528 | NEW_TK error CSB_TK
1529 {yyerror ("'[' expected"); DRECOVER ("]");}
1530 | NEW_TK error OSB_TK
1531 {yyerror ("']' expected"); RECOVER;}
1536 { $$ = build_tree_list (NULL_TREE, $1); }
1537 | dim_exprs dim_expr
1538 { $$ = tree_cons (NULL_TREE, $2, $$); }
1542 OSB_TK expression CSB_TK
1544 EXPR_WFL_LINECOL ($2) = $1.location;
1547 | OSB_TK expression error
1548 {yyerror ("']' expected"); RECOVER;}
1551 yyerror ("Missing term");
1552 yyerror ("']' expected");
1559 { ctxp->osb_number = 1; }
1560 | dims OSB_TK CSB_TK
1561 { ctxp->osb_number++; }
1563 { yyerror ("']' expected"); RECOVER;}
1567 primary DOT_TK identifier
1568 { $$ = make_qualified_primary ($1, $3, $2.location); }
1569 | SUPER_TK DOT_TK identifier
1572 build_wfl_node (super_identifier_node, input_filename, 0, 0);
1573 EXPR_WFL_LINECOL (super_wfl) = $1.location;
1574 $$ = make_qualified_name (super_wfl, $3, $2.location);
1577 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
1582 { $$ = build_method_invocation ($1, NULL_TREE); }
1583 | name OP_TK argument_list CP_TK
1584 { $$ = build_method_invocation ($1, $3); }
1585 | primary DOT_TK identifier OP_TK CP_TK
1587 tree invok = build_method_invocation ($3, NULL_TREE);
1588 $$ = make_qualified_primary ($1, invok, $2.location);
1590 | primary DOT_TK identifier OP_TK argument_list CP_TK
1592 tree invok = build_method_invocation ($3, $5);
1593 $$ = make_qualified_primary ($1, invok, $2.location);
1595 | SUPER_TK DOT_TK identifier OP_TK CP_TK
1598 tree wfl = build_wfl_node (super_identifier_node, input_filename, 0, 0);
1599 EXPR_WFL_LINECOL (wfl) = $1.location;
1600 invok = build_method_invocation ($3, NULL_TREE);
1601 $$ = make_qualified_primary (wfl, invok, $2.location);
1603 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
1606 tree wfl = build_wfl_node (super_identifier_node, input_filename, 0, 0);
1607 EXPR_WFL_LINECOL (wfl) = $1.location;
1608 invok = build_method_invocation ($3, $5);
1609 $$ = make_qualified_primary (wfl, invok, $2.location);
1611 /* Screws up thing. I let it here until I'm convinced it can
1613 | primary DOT_TK error
1614 {yyerror ("'(' expected"); DRECOVER(bad);} */
1615 | SUPER_TK DOT_TK error CP_TK
1616 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1617 | SUPER_TK DOT_TK error DOT_TK
1618 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1622 name OSB_TK expression CSB_TK
1623 { $$ = build_array_ref ($2.location, $1, $3); }
1624 | primary_no_new_array OSB_TK expression CSB_TK
1625 { $$ = build_array_ref ($2.location, $1, $3); }
1628 yyerror ("Missing term and ']' expected");
1629 DRECOVER(array_access);
1631 | name OSB_TK expression error
1633 yyerror ("']' expected");
1634 DRECOVER(array_access);
1636 | primary_no_new_array OSB_TK error
1638 yyerror ("Missing term and ']' expected");
1639 DRECOVER(array_access);
1641 | primary_no_new_array OSB_TK expression error
1643 yyerror ("']' expected");
1644 DRECOVER(array_access);
1651 | post_increment_expression
1652 | post_decrement_expression
1655 post_increment_expression:
1656 postfix_expression INCR_TK
1657 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1660 post_decrement_expression:
1661 postfix_expression DECR_TK
1662 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1666 pre_increment_expression
1667 | pre_decrement_expression
1668 | PLUS_TK unary_expression
1669 {$$ = build_unaryop ($1.token, $1.location, $2); }
1670 | MINUS_TK unary_expression
1671 {$$ = build_unaryop ($1.token, $1.location, $2); }
1672 | unary_expression_not_plus_minus
1674 {yyerror ("Missing term"); RECOVER}
1676 {yyerror ("Missing term"); RECOVER}
1679 pre_increment_expression:
1680 INCR_TK unary_expression
1681 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1683 {yyerror ("Missing term"); RECOVER}
1686 pre_decrement_expression:
1687 DECR_TK unary_expression
1688 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1690 {yyerror ("Missing term"); RECOVER}
1693 unary_expression_not_plus_minus:
1695 | NOT_TK unary_expression
1696 {$$ = build_unaryop ($1.token, $1.location, $2); }
1697 | NEG_TK unary_expression
1698 {$$ = build_unaryop ($1.token, $1.location, $2); }
1701 {yyerror ("Missing term"); RECOVER}
1703 {yyerror ("Missing term"); RECOVER}
1706 cast_expression: /* Error handling here is potentially weak */
1707 OP_TK primitive_type dims CP_TK unary_expression
1710 while (ctxp->osb_number--)
1711 type = build_java_array_type (type, -1);
1712 $$ = build_cast ($1.location, type, $5);
1714 | OP_TK primitive_type CP_TK unary_expression
1715 { $$ = build_cast ($1.location, $2, $4); }
1716 | OP_TK expression CP_TK unary_expression_not_plus_minus
1717 { $$ = build_cast ($1.location, $2, $4); }
1718 | OP_TK name dims CP_TK unary_expression_not_plus_minus
1721 while (ctxp->osb_number--)
1722 obstack_1grow (&temporary_obstack, '[');
1723 obstack_grow0 (&temporary_obstack,
1724 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
1725 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
1726 ptr = obstack_finish (&temporary_obstack);
1727 EXPR_WFL_NODE ($2) = get_identifier (ptr);
1728 $$ = build_cast ($1.location, $2, $5);
1730 | OP_TK primitive_type OSB_TK error
1731 {yyerror ("']' expected, invalid type expression");}
1734 if (ctxp->prevent_ese != lineno)
1735 yyerror ("Invalid type expression"); RECOVER;
1738 | OP_TK primitive_type dims CP_TK error
1739 {yyerror ("Missing term"); RECOVER;}
1740 | OP_TK primitive_type CP_TK error
1741 {yyerror ("Missing term"); RECOVER;}
1742 | OP_TK name dims CP_TK error
1743 {yyerror ("Missing term"); RECOVER;}
1746 multiplicative_expression:
1748 | multiplicative_expression MULT_TK unary_expression
1750 $$ = build_binop (BINOP_LOOKUP ($2.token),
1751 $2.location, $1, $3);
1753 | multiplicative_expression DIV_TK unary_expression
1755 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1758 | multiplicative_expression REM_TK unary_expression
1760 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1763 | multiplicative_expression MULT_TK error
1764 {yyerror ("Missing term"); RECOVER;}
1765 | multiplicative_expression DIV_TK error
1766 {yyerror ("Missing term"); RECOVER;}
1767 | multiplicative_expression REM_TK error
1768 {yyerror ("Missing term"); RECOVER;}
1771 additive_expression:
1772 multiplicative_expression
1773 | additive_expression PLUS_TK multiplicative_expression
1775 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1778 | additive_expression MINUS_TK multiplicative_expression
1780 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1783 | additive_expression PLUS_TK error
1784 {yyerror ("Missing term"); RECOVER;}
1785 | additive_expression MINUS_TK error
1786 {yyerror ("Missing term"); RECOVER;}
1791 | shift_expression LS_TK additive_expression
1793 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1796 | shift_expression SRS_TK additive_expression
1798 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1801 | shift_expression ZRS_TK additive_expression
1803 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1806 | shift_expression LS_TK error
1807 {yyerror ("Missing term"); RECOVER;}
1808 | shift_expression SRS_TK error
1809 {yyerror ("Missing term"); RECOVER;}
1810 | shift_expression ZRS_TK error
1811 {yyerror ("Missing term"); RECOVER;}
1814 relational_expression:
1816 | relational_expression LT_TK shift_expression
1818 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1821 | relational_expression GT_TK shift_expression
1823 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1826 | relational_expression LTE_TK shift_expression
1828 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1831 | relational_expression GTE_TK shift_expression
1833 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1836 | relational_expression INSTANCEOF_TK reference_type
1837 | relational_expression LT_TK error
1838 {yyerror ("Missing term"); RECOVER;}
1839 | relational_expression GT_TK error
1840 {yyerror ("Missing term"); RECOVER;}
1841 | relational_expression LTE_TK error
1842 {yyerror ("Missing term"); RECOVER;}
1843 | relational_expression GTE_TK error
1844 {yyerror ("Missing term"); RECOVER;}
1845 | relational_expression INSTANCEOF_TK error
1846 {yyerror ("Invalid reference type"); RECOVER;}
1849 equality_expression:
1850 relational_expression
1851 | equality_expression EQ_TK relational_expression
1853 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1856 | equality_expression NEQ_TK relational_expression
1858 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1861 | equality_expression EQ_TK error
1862 {yyerror ("Missing term"); RECOVER;}
1863 | equality_expression NEQ_TK error
1864 {yyerror ("Missing term"); RECOVER;}
1869 | and_expression AND_TK equality_expression
1871 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1874 | and_expression AND_TK error
1875 {yyerror ("Missing term"); RECOVER;}
1878 exclusive_or_expression:
1880 | exclusive_or_expression XOR_TK and_expression
1882 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1885 | exclusive_or_expression XOR_TK error
1886 {yyerror ("Missing term"); RECOVER;}
1889 inclusive_or_expression:
1890 exclusive_or_expression
1891 | inclusive_or_expression OR_TK exclusive_or_expression
1893 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1896 | inclusive_or_expression OR_TK error
1897 {yyerror ("Missing term"); RECOVER;}
1900 conditional_and_expression:
1901 inclusive_or_expression
1902 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
1904 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1907 | conditional_and_expression BOOL_AND_TK error
1908 {yyerror ("Missing term"); RECOVER;}
1911 conditional_or_expression:
1912 conditional_and_expression
1913 | conditional_or_expression BOOL_OR_TK conditional_and_expression
1915 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
1918 | conditional_or_expression BOOL_OR_TK error
1919 {yyerror ("Missing term"); RECOVER;}
1922 conditional_expression: /* Error handling here is weak */
1923 conditional_or_expression
1924 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
1925 | conditional_or_expression REL_QM_TK REL_CL_TK error
1928 yyerror ("Missing term");
1931 | conditional_or_expression REL_QM_TK error
1932 {yyerror ("Missing term"); DRECOVER (2);}
1933 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
1934 {yyerror ("Missing term"); DRECOVER (3);}
1937 assignment_expression:
1938 conditional_expression
1943 left_hand_side assignment_operator assignment_expression
1944 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
1945 | left_hand_side assignment_operator error
1947 if (ctxp->prevent_ese != lineno)
1948 yyerror ("Missing term");
1959 assignment_operator:
1965 assignment_expression
1968 constant_expression:
1977 /* Flag for the error report routine to issue the error the first time
1978 it's called (overriding the default behavior which is to drop the
1979 first invocation and honor the second one, taking advantage of a
1981 static int force_error = 0;
1983 /* Create a new parser context and make it the current one. */
1986 java_push_parser_context ()
1988 struct parser_ctxt *new =
1989 (struct parser_ctxt *)malloc(sizeof (struct parser_ctxt));
1991 bzero (new, sizeof (struct parser_ctxt));
1995 ctxp->incomplete_class = ctxp->next->incomplete_class;
1999 java_parser_context_save_global ()
2001 ctxp->finput = finput;
2002 ctxp->lineno = lineno;
2003 ctxp->current_class = current_class;
2004 ctxp->filename = input_filename;
2005 ctxp->current_function_decl = current_function_decl;
2009 java_parser_context_restore_global ()
2011 finput = ctxp->finput;
2012 lineno = ctxp->lineno;
2013 current_class = ctxp->current_class;
2014 input_filename = ctxp->filename;
2015 current_function_decl = ctxp->current_function_decl;
2019 java_pop_parser_context ()
2022 struct parser_ctxt *toFree = ctxp;
2023 struct parser_ctxt *next = ctxp->next;
2027 next->incomplete_class = ctxp->incomplete_class;
2028 lineno = ctxp->lineno;
2029 finput = ctxp->finput;
2030 current_class = ctxp->current_class;
2033 /* Set the single import class file flag to 0 for the current list
2034 of imported things */
2035 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2036 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2038 /* And restore those of the previous context */
2040 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2041 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2046 static int do_warning = 0;
2053 static int prev_lineno;
2054 static char *prev_msg;
2057 char *remainder, *code_from_source;
2058 extern struct obstack temporary_obstack;
2060 if (!force_error && prev_lineno == lineno)
2063 /* Save current error location but report latter, when the context is
2065 if (ctxp->java_error_flag == 0)
2067 ctxp->java_error_flag = 1;
2069 /* Do something to use the previous line if we're reaching the
2070 end of the file... */
2071 #ifdef VERBOSE_SKELETON
2072 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2077 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2078 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2081 ctxp->java_error_flag = 0;
2083 java_warning_count++;
2087 if (elc.col == 0 && msg[1] == ';')
2089 elc.col = ctxp->p_line->char_col-1;
2090 elc.line = ctxp->p_line->lineno;
2093 save_lineno = lineno;
2094 prev_lineno = lineno = elc.line;
2097 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2098 obstack_grow0 (&temporary_obstack,
2099 code_from_source, strlen (code_from_source));
2100 remainder = obstack_finish (&temporary_obstack);
2102 warning ("%s.\n%s", msg, remainder);
2104 error ("%s.\n%s", msg, remainder);
2106 /* This allow us to cheaply avoid an extra 'Invalid expression
2107 statement' error report when errors have been already reported on
2108 the same line. This occurs when we report an error but don't have
2109 a synchronization point other than ';', which
2110 expression_statement is the only one to take care of. */
2111 ctxp->prevent_ese = lineno = save_lineno;
2122 /* Issue an error message at a current source line CL */
2125 parse_error_context VPROTO ((tree cl, char *msg, ...))
2136 cl = va_arg (ap, tree);
2137 msg = va_arg (ap, char *);
2139 vsprintf (buffer, msg, ap);
2142 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2143 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 : EXPR_WFL_COLNO (cl));
2145 parse_error (buffer);
2149 /* Issue a warning at a current source line CL */
2152 parse_warning_context VPROTO ((tree cl, char *msg, ...))
2163 cl = va_arg (ap, tree);
2164 msg = va_arg (ap, char *);
2166 vsprintf (buffer, msg, ap);
2168 force_error = do_warning = 1;
2169 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2170 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 : EXPR_WFL_COLNO (cl));
2172 parse_error (buffer);
2173 do_warning = force_error = 0;
2177 java_report_errors ()
2179 if (java_error_count)
2180 fprintf (stderr, "%d error%s",
2181 java_error_count, (java_error_count == 1 ? "" : "s"));
2182 if (java_warning_count)
2183 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
2184 java_warning_count, (java_warning_count == 1 ? "" : "s"));
2185 if (java_error_count || java_warning_count)
2186 putc ('\n', stderr);
2190 java_accstring_lookup (flags)
2193 static char buffer [80];
2194 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
2196 /* Access modifier looked-up first for easier report on forbidden
2198 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
2199 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
2200 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
2201 if (flags & ACC_STATIC) COPY_RETURN ("static");
2202 if (flags & ACC_FINAL) COPY_RETURN ("final");
2203 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
2204 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
2205 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
2206 if (flags & ACC_NATIVE) COPY_RETURN ("native");
2207 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
2208 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
2216 redefinition_error (context, id, decl, cl)
2220 parse_error_context (cl, "%s `%s' already defined in %s:%d",
2221 context, IDENTIFIER_POINTER (id),
2222 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
2223 /* Here we should point out where its redefined. It's a unicode. FIXME */
2226 /* Build something that the type identifier resolver will identify as
2227 being an array to an unresolved type. TYPE_WFL is a WFL on a
2231 build_unresolved_array_type (type_or_wfl)
2236 /* TYPE_OR_WFL might be an array on a primitive type. In this case,
2237 just create a array type */
2238 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
2240 tree type = build_java_array_type (type_or_wfl, -1);
2241 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
2245 obstack_1grow (&temporary_obstack, '[');
2246 obstack_grow0 (&temporary_obstack,
2247 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
2248 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
2249 ptr = obstack_finish (&temporary_obstack);
2250 return build_expr_wfl (get_identifier (ptr),
2251 EXPR_WFL_FILENAME (type_or_wfl),
2252 EXPR_WFL_LINENO (type_or_wfl),
2253 EXPR_WFL_COLNO (type_or_wfl));
2256 /* Check modifiers. If one doesn't fit, retrieve it in its declaration line
2257 and point it out. */
2260 check_modifiers (message, value, mask)
2265 /* Should point out the one that don't fit. ASCII/unicode,
2266 going backward. FIXME */
2269 int i, remainder = value & ~mask;
2270 for (i = 0; i <= 10; i++)
2271 if ((1 << i) & remainder)
2272 parse_error_context (ctxp->modifier_ctx [i], message,
2273 java_accstring_lookup (1 << i));
2278 parser_add_interface (class_decl, interface_decl, wfl)
2279 tree class_decl, interface_decl, wfl;
2281 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
2282 parse_error_context (wfl, "Interface `%s' repeated",
2283 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
2286 /* Bulk of common class/interface checks. Return 1 if an error was
2287 encountered. TAG is 0 for a class, 1 for an interface. */
2290 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
2291 int is_interface, flags;
2292 tree raw_name, qualified_name, decl, cl;
2297 fprintf (stderr, " %s %s", (is_interface ? "interface" : "class"),
2298 IDENTIFIER_POINTER (qualified_name));
2300 /* Scope of an interface/class type name:
2301 - Can't be imported by a single type import
2302 - Can't already exists in the package */
2303 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
2304 && (node = find_name_in_single_imports (raw_name)))
2307 (cl, "%s name `%s' clashes with imported type `%s'",
2308 (is_interface ? "Interface" : "Class"),
2309 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
2312 if (decl && CLASS_COMPLETE_P (decl))
2314 redefinition_error ((is_interface ? "Interface" : "Class"),
2315 qualified_name, decl, cl);
2319 /* If public, file name should match class/interface name */
2320 if (flags & ACC_PUBLIC)
2324 /* Contains OS dependent assumption on path separator. FIXME */
2325 for (f = &input_filename [strlen (input_filename)];
2326 f != input_filename && f[0] != '/'; f--);
2329 if (strncmp (IDENTIFIER_POINTER (raw_name),
2330 f , IDENTIFIER_LENGTH (raw_name)) ||
2331 f [IDENTIFIER_LENGTH (raw_name)] != '.')
2332 parse_error_context (cl, "Public %s `%s' must be defined in a file "
2334 (is_interface ? "interface" : "class"),
2335 IDENTIFIER_POINTER (qualified_name),
2336 IDENTIFIER_POINTER (raw_name));
2339 check_modifiers ((is_interface ?
2340 "Illegal modifier `%s' for interface declaration" :
2341 "Illegal modifier `%s' for class declaration"), flags,
2342 (is_interface ? INTERFACE_MODIFIERS : CLASS_MODIFIERS));
2346 /* If DECL is NULL, create and push a new DECL, record the current
2347 line CL and do other maintenance things. */
2350 maybe_create_class_interface_decl (decl, qualified_name, cl)
2351 tree decl, qualified_name, cl;
2354 DECL_ARTIFICIAL (decl) = 1; /* FIXME */
2356 decl = push_class (make_class (), qualified_name);
2358 /* Take care of the file and line business */
2359 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
2360 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
2361 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
2363 ctxp->current_parsed_class = decl;
2365 /* Link the declaration to the already seen ones */
2366 TREE_CHAIN (decl) = ctxp->class_list;
2367 ctxp->class_list = decl;
2368 /* Install a new dependency list element */
2369 create_jdep_list (ctxp);
2371 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
2372 IDENTIFIER_POINTER (qualified_name)));
2377 add_superinterfaces (decl, interface_list)
2378 tree decl, interface_list;
2381 /* Superinterface(s): if present and defined, parser_check_super_interface ()
2382 takes care of ensuring that:
2383 - This is an accessible interface type,
2384 - Circularity detection.
2385 parser_add_interface is then called. If present but not defined,
2386 the check operation is delayed until the super interface gets
2388 for (node = interface_list; node; node = TREE_CHAIN (node))
2390 tree current = TREE_PURPOSE (node), interface_decl;
2391 if ((interface_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current))))
2393 if (!parser_check_super_interface (interface_decl, decl, current))
2394 parser_add_interface (decl, interface_decl, current);
2397 register_incomplete_type (JDEP_INTERFACE,
2398 current, decl, NULL_TREE);
2402 /* Create an interface in pass1 and return its decl. Return the
2403 interface's decl in pass 2. */
2406 create_interface (flags, id, super)
2411 tree raw_name = EXPR_WFL_NODE (id);
2412 tree q_name = parser_qualified_classname (id);
2413 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
2415 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
2417 /* Basic checks: scope, redefinition, modifiers */
2418 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
2421 /* Interface modifiers check
2422 - public/abstract allowed (already done at that point)
2423 - abstract is obsolete (comes first, it's a warning, or should be)
2424 - Can't use twice the same (checked in the modifier rule) */
2425 if (flags & ACC_ABSTRACT)
2426 parse_warning_context
2427 (MODIFIER_WFL (ABSTRACT_TK),
2428 "Obsolete use of `abstract' modifier. Interface `%s' is implicitely "
2429 "abstract", IDENTIFIER_POINTER (raw_name));
2430 if (flags & ACC_PUBLIC && flags & ACC_ABSTRACT)
2432 (MODIFIER_WFL (ABSTRACT_TK),
2433 "Can't specify both `public' and `abstract' modifiers in the "
2434 "definition of interface `%s'", IDENTIFIER_POINTER (raw_name));
2436 /* Create a new decl if DECL is NULL, otherwise fix it */
2437 decl = maybe_create_class_interface_decl (decl, q_name, id);
2439 /* Set super info and mark the class a complete */
2440 set_super_info (ACC_ABSTRACT | ACC_INTERFACE | flags, TREE_TYPE (decl),
2441 object_type_node, ctxp->interface_number);
2442 ctxp->interface_number = 0;
2443 CLASS_COMPLETE_P (decl) = 1;
2444 add_superinterfaces (decl, super);
2449 /* Create an class in pass1 and return its decl. Return class
2450 interface's decl in pass 2. */
2453 create_class (flags, id, super, interfaces)
2455 tree id, super, interfaces;
2458 tree raw_name = EXPR_WFL_NODE (id);
2459 tree class_id, decl;
2460 tree super_decl = NULL, super_decl_type;
2462 class_id = parser_qualified_classname (id);
2463 decl = IDENTIFIER_CLASS_VALUE (class_id);
2464 EXPR_WFL_NODE (id) = class_id;
2466 /* Basic check: scope, redefinition, modifiers */
2467 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
2470 /* Class modifier check:
2471 - Allowed modifier (already done at that point)
2472 - abstract AND final forbidden
2473 - Public classes defined in the correct file */
2474 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
2475 parse_error_context (id, "Class `%s' can't be declared both abstract "
2476 "and final", IDENTIFIER_POINTER (raw_name));
2478 /* Create a new decl if DECL is NULL, otherwise fix it */
2479 decl = maybe_create_class_interface_decl (decl, class_id, id);
2481 /* If SUPER exists, use it, otherwise use Object */
2484 /* Can't extend java.lang.Object */
2485 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
2487 parse_error_context (id, "Can't extend `java.lang.Object'");
2491 /* The class is known and exists if there is a decl. Otherwise,
2492 postpone the operation and do it later. */
2493 super_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (super));
2496 parser_check_super (super_decl, decl, id);
2497 super_decl_type = TREE_TYPE (super_decl);
2501 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
2503 else if (TREE_TYPE (decl) != object_type_node)
2504 super_decl_type = object_type_node;
2505 /* We're defining java.lang.Object */
2507 super_decl_type = NULL_TREE;
2509 /* Set super info and mark the class a complete */
2510 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
2511 ctxp->interface_number);
2512 ctxp->interface_number = 0;
2513 CLASS_COMPLETE_P (decl) = 1;
2514 add_superinterfaces (decl, interfaces);
2519 /* Can't use lookup_field () since we don't want to load the class and
2520 can't set the CLASS_LOADED_P flag */
2523 find_field (class, name)
2528 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
2530 if (DECL_NAME (decl) == name)
2536 /* Wrap around lookup_field that doesn't potentially upset the value
2540 lookup_field_wrapper (class, name)
2544 return lookup_field (&type, name);
2547 /* Find duplicate field within the same class declarations and report
2551 duplicate_declaration_error (class, new_field_name, new_type, cl)
2552 tree class, new_field_name, new_type, cl;
2554 /* This might be modified to work with method decl as well */
2555 tree decl = find_field (TREE_TYPE (ctxp->current_parsed_class),
2559 char *t1 = strdup ((char *)lang_printable_name (new_type, 1));
2561 strdup ((TREE_CODE (TREE_TYPE (decl)) == TREE_LIST ?
2562 IDENTIFIER_POINTER (TYPE_NAME
2563 (TREE_PURPOSE (TREE_TYPE (decl)))) :
2564 (char *)lang_printable_name (TREE_TYPE (decl), 1)));
2566 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
2567 t1, IDENTIFIER_POINTER (new_field_name),
2568 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
2569 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
2577 /* Field registration routine. If TYPE doesn't exist, field
2578 declarations are linked to the undefined TYPE dependency list, to
2579 be later resolved in java_complete_class () */
2582 register_fields (flags, type, variable_list)
2584 tree type, variable_list;
2586 tree current, type_decl, returned_type;
2587 tree class_type = TREE_TYPE (ctxp->current_parsed_class);
2588 int saved_lineno = lineno;
2590 tree wfl = NULL_TREE;
2592 /* If we're adding fields to interfaces, those fields are public,
2594 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
2596 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
2598 "%s", "interface field(s)");
2599 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
2601 "%s", "interface field(s)");
2602 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
2603 flags, ACC_FINAL, "%s", "interface field(s)");
2604 check_modifiers ("Illegal interface member modifier `%s'", flags,
2605 INTERFACE_FIELD_MODIFIERS);
2606 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
2609 if (unresolved_type_p (type, &returned_type))
2612 type = returned_type;
2616 type = obtain_incomplete_type (type);
2621 for (current = variable_list; current; current = TREE_CHAIN (current))
2623 tree cl = TREE_PURPOSE (current);
2624 tree init = TREE_VALUE (current);
2625 tree current_name = EXPR_WFL_NODE (cl);
2627 if (duplicate_declaration_error (class_type, current_name, type, cl))
2630 lineno = EXPR_WFL_LINENO (cl);
2631 field_decl = add_field (class_type, current_name, type, flags);
2633 /* Check if we must chain. */
2635 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
2637 /* Default value of a static field is 0 and it is considered
2639 if (flags & ACC_STATIC)
2640 INITIALIZED_P (field_decl) = 1;
2642 /* If we have an initialization value tied to the field */
2645 /* The field is declared static */
2646 if (flags & ACC_STATIC)
2649 if (flags & ACC_FINAL)
2651 /* Otherwise, the field should be initialized in
2652 <clinit>. This field is remembered so we can
2653 generate <clinit> later. */
2656 INITIALIZED_P (field_decl) = 1;
2657 TREE_CHAIN (init) = ctxp->static_initialized;
2658 ctxp->static_initialized = init;
2661 /* A non-static field declared with an immediate
2662 initialization is to be initialized in <init>, if
2663 any. This field is remembered to be processed at the
2664 time of the generation of <init>. */
2667 TREE_CHAIN (init) = ctxp->non_static_initialized;
2668 ctxp->non_static_initialized = init;
2673 lineno = saved_lineno;
2676 /* Check whether it is necessary to generate a <clinit> for the class
2680 maybe_generate_clinit ()
2683 tree meth, mdecl, c;
2684 tree cclass, class_wfl;
2686 if (!ctxp->static_initialized || java_error_count)
2689 cclass = TREE_TYPE (ctxp->current_parsed_class);
2690 class_wfl = build_expr_wfl (DECL_NAME (TYPE_NAME (cclass)),
2691 input_filename, 0, 0);
2693 saved_lineno = lineno;
2695 meth = make_node (FUNCTION_TYPE);
2696 TREE_TYPE (meth) = void_type_node;
2697 TYPE_ARG_TYPES (meth) = NULL_TREE;
2698 mdecl = add_method (cclass, ACC_STATIC, clinit_identifier_node,
2699 build_java_signature (meth));
2700 lineno = saved_lineno;
2702 DECL_SOURCE_LINE (mdecl) = 1;
2703 DECL_SOURCE_LINE_MERGE (mdecl, 1);
2704 source_start_java_method (mdecl);
2707 /* Keep initialization in order to enforce 8.5 */
2708 ctxp->static_initialized = nreverse (ctxp->static_initialized);
2710 /* We process the list of assignment we produced as the result of
2711 the declaration of initialized static field and add them as
2712 statement to the <clinit> method. */
2713 for (c = ctxp->static_initialized; c; c = TREE_CHAIN (c))
2715 /* We build the assignment expression that will initialize the
2716 field to its value. There are strict rules on static
2717 initializers (8.5). FIXME */
2718 java_method_add_stmt (mdecl, c);
2721 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
2723 ctxp->static_initialized = NULL_TREE;
2726 /* Shared accros method_declarator and method_header to remember the
2727 patch stage that was reached during the declaration of the method.
2728 A method DECL is built differently is there is no patch
2729 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
2730 pending on the currently defined method. */
2732 static int patch_stage;
2734 /* Check the method declaration and add the method to its current
2735 class. If the argument list is known to contain incomplete types,
2736 the method is partially added and the registration will be resume
2737 once the method arguments resolved */
2740 method_header (flags, type, mdecl, throws)
2742 tree type, mdecl, throws;
2744 tree meth = TREE_VALUE (mdecl);
2745 tree id = TREE_PURPOSE (mdecl);
2746 tree this_class = TREE_TYPE (ctxp->current_parsed_class);
2747 tree handle_class = CLASS_TO_HANDLE_TYPE (this_class);
2748 tree meth_name, returned_type;
2751 check_modifiers_consistency (flags);
2753 /* There are some forbidden modifiers for an abstract method and its
2754 class must be abstract as well. */
2755 if (flags & ACC_ABSTRACT)
2757 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
2758 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
2759 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
2760 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
2761 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
2762 if (!CLASS_ABSTRACT (TYPE_NAME (this_class)))
2764 (id, "Class `%s' must be declared abstract to define abstract "
2766 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
2767 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
2771 /* Method declared within the scope of an interface are implicitly
2772 abstract and public. Conflicts with other erroneously provided
2773 modifiers are check right after. */
2775 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
2777 /* If FLAGS isn't set because of a modifier, turn the
2778 corresponding modifier WFL to NULL so we issue a warning on
2779 the obsolete use of the modifier */
2780 if (!(flags & ACC_PUBLIC))
2781 MODIFIER_WFL (PUBLIC_TK) = NULL;
2782 if (!(flags & ACC_ABSTRACT))
2783 MODIFIER_WFL (ABSTRACT_TK) = NULL;
2784 flags |= ACC_PUBLIC;
2785 flags |= ACC_ABSTRACT;
2788 /* Modifiers context reset moved up, so abstract method declaration
2789 modifiers can be later checked. */
2791 meth_name = EXPR_WFL_NODE (id);
2793 if (unresolved_type_p (type, &returned_type))
2796 TREE_TYPE (meth) = returned_type;
2799 patch_stage = JDEP_METHOD_RETURN;
2801 register_incomplete_type (patch_stage, type, id, NULL_TREE);
2805 TREE_TYPE (meth) = type;
2807 saved_lineno = lineno;
2808 /* When defining an abstract or interface method, the curly
2809 bracket at level 1 doesn't exist because there is no function
2811 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
2812 EXPR_WFL_LINENO (id));
2814 if (patch_stage) /* includes ret type and/or all args */
2817 meth = add_method_1 (this_class, flags, meth_name, meth);
2818 /* Patch for the return type */
2819 if (patch_stage == JDEP_METHOD_RETURN)
2821 jdep = CLASSD_LAST (ctxp->classd_list);
2822 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
2824 /* This is the stop JDEP. METH allows the function's signature
2826 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
2830 tree signature = build_java_signature (meth);
2832 /* Save original argument list, including argument's names */
2833 orig_arg = TYPE_ARG_TYPES (meth);
2834 /* Add the method to its class */
2835 meth = add_method (this_class, flags, meth_name, signature);
2836 /* Fix the method argument list so we have the argument name
2838 arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
2839 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
2841 TREE_PURPOSE (arg) = this_identifier_node;
2842 arg = TREE_CHAIN (arg);
2846 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
2847 orig_arg = TREE_CHAIN (orig_arg);
2848 arg = TREE_CHAIN (arg);
2851 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
2852 lineno = saved_lineno;
2853 /* We set the DECL_NAME to ID so we can track the location where
2854 the function was declared. This allow us to report
2855 redefinition error accurately. When method are verified,
2856 DECL_NAME is reinstalled properly (using the content of the
2857 WFL node ID) (see check_method_redefinition). We don't do that
2858 when Object is being defined. */
2859 if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node)
2860 DECL_NAME (meth) = id;
2864 /* Check modifiers that can be declared but exclusively */
2867 check_modifiers_consistency (flags)
2871 tree cl = NULL_TREE;
2873 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, 0, acc_count, cl);
2874 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, 1, acc_count, cl);
2875 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, 2, acc_count, cl);
2878 (cl, "Inconsistent member declaration. At most one of `public', "
2879 "`private', or `protected' may be specified");
2882 /* Check the methode header METH for abstract specifics features */
2885 check_abstract_method_header (meth)
2888 int flags = get_access_flags_from_decl (meth);
2889 /* DECL_NAME might still be a WFL node */
2890 tree name = (TREE_CODE (DECL_NAME (meth)) == EXPR_WITH_FILE_LOCATION ?
2891 EXPR_WFL_NODE (DECL_NAME (meth)) : DECL_NAME (meth));
2893 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (ABSTRACT_TK), flags,
2894 ACC_ABSTRACT, "abstract method `%s'",
2895 IDENTIFIER_POINTER (name));
2896 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), flags,
2897 ACC_PUBLIC, "abstract method `%s'",
2898 IDENTIFIER_POINTER (name));
2900 check_modifiers ("Illegal modifier `%s' for interface method",
2901 flags, INTERFACE_METHOD_MODIFIERS);
2904 /* Create a FUNCTION_TYPE node and start augmenting it with the
2905 declared function arguments. Arguments type that can't be resolved
2906 are left as they are, but the returned node is marked as containing
2907 incomplete types. */
2910 method_declarator (id, list)
2913 tree arg_types = NULL_TREE, current, node;
2914 tree meth = make_node (FUNCTION_TYPE);
2918 patch_stage = JDEP_NO_PATCH;
2920 for (current = list; current; current = TREE_CHAIN (current))
2922 tree wfl_name = TREE_PURPOSE (current);
2923 tree type = TREE_VALUE (current);
2924 tree name = EXPR_WFL_NODE (wfl_name);
2925 tree patchable_type = NULL_TREE, already;
2926 tree arg_node, returned_type;
2928 /* Check redefinition */
2929 for (already = arg_types; already; already = TREE_CHAIN (already))
2930 if (TREE_PURPOSE (already) == name)
2933 (wfl_name, "Variable `%s' is used more than once in the "
2934 "argument list of method `%s'", IDENTIFIER_POINTER (name),
2935 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
2939 /* If we've an incomplete argument type, we know there is a location
2940 to patch when the type get resolved, later. */
2942 if (unresolved_type_p (type, &returned_type))
2945 type = returned_type;
2948 patch_stage = JDEP_METHOD;
2949 type = register_incomplete_type (patch_stage, type,
2950 wfl_name, NULL_TREE);
2951 jdep = CLASSD_LAST (ctxp->classd_list);
2952 JDEP_MISC (jdep) = id;
2955 /* The argument node: a name and a (possibly) incomplete type */
2956 arg_node = build_tree_list (name, type);
2958 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
2959 TREE_CHAIN (arg_node) = arg_types;
2960 arg_types = arg_node;
2962 TYPE_ARG_TYPES (meth) = nreverse (arg_types);
2963 node = build_tree_list (id, meth);
2968 unresolved_type_p (wfl, returned)
2973 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
2975 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
2977 *returned = (decl ? TREE_TYPE (decl) : NULL_TREE);
2985 /* From NAME, build a qualified identifier node using the
2986 qualification from the current package definition. */
2989 parser_qualified_classname (name)
2993 return merge_qualified_name (ctxp->package, EXPR_WFL_NODE (name));
2995 return EXPR_WFL_NODE (name);
2998 /* Called once the type a interface extends is resolved. Returns 0 if
2999 everything is OK. */
3002 parser_check_super_interface (super_decl, this_decl, this_wfl)
3003 tree super_decl, this_decl, this_wfl;
3005 tree super_type = TREE_TYPE (super_decl);
3007 /* Has to be an interface */
3008 if (!CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (super_decl))))
3011 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
3012 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
3013 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
3014 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
3015 "interface" : "class"),
3016 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
3020 /* Check scope: same package OK, other package: OK if public */
3021 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
3024 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
3025 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3026 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3030 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
3031 0 if everthing is OK. */
3034 parser_check_super (super_decl, this_decl, wfl)
3035 tree super_decl, this_decl, wfl;
3037 tree this_type = TREE_TYPE (this_decl);
3038 tree super_type = TREE_TYPE (super_decl);
3040 /* SUPER should be a CLASS (neither an array nor an interface) */
3041 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
3044 (wfl, "Class `%s' can't subclass %s `%s'",
3045 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3046 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
3047 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3051 if (CLASS_FINAL (TYPE_NAME (super_type)))
3053 parse_error_context (wfl, "Can't subclass final classes: %s",
3054 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3058 /* Check scope: same package OK, other package: OK if public */
3059 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
3062 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
3063 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3064 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3068 /* Create a new dependency list and link it (in a LIFO manner) to the
3069 CTXP list of type dependency list. */
3072 create_jdep_list (ctxp)
3073 struct parser_ctxt *ctxp;
3075 jdeplist *new = malloc (sizeof (jdeplist));
3078 fatal ("Can't alloc jdeplist - create_jdep_list");
3080 new->first = new->last = NULL;
3081 new->next = ctxp->classd_list;
3082 ctxp->classd_list = new;
3086 reverse_jdep_list (ctxp)
3087 struct parser_ctxt *ctxp;
3089 register jdeplist *prev = NULL, *current, *next;
3090 for (current = ctxp->classd_list; current; current = next)
3092 next = current->next;
3093 current->next = prev;
3099 /* Create a fake pointer based on the ID stored in the WFL */
3102 obtain_incomplete_type (wfl)
3106 tree name = EXPR_WFL_NODE (wfl);
3108 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
3109 if (TYPE_NAME (TREE_PURPOSE (ptr)) == name)
3115 push_obstacks (&permanent_obstack, &permanent_obstack);
3116 BUILD_PTR_FROM_NAME (core, name);
3117 ptr = build_tree_list (core, NULL_TREE);
3119 TREE_CHAIN (ptr) = ctxp->incomplete_class;
3120 ctxp->incomplete_class = ptr;
3126 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
3127 non NULL instead of computing a new fake type based on WFL. The new
3128 dependency is inserted in the current type dependency list, in FIFO
3132 register_incomplete_type (kind, wfl, decl, ptr)
3134 tree wfl, decl, ptr;
3136 jdep *new = malloc (sizeof (jdep));
3139 fatal ("Can't allocate new jdep - register_incomplete_type");
3140 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
3141 ptr = obtain_incomplete_type (wfl);
3143 JDEP_KIND (new) = kind;
3144 JDEP_DECL (new) = decl;
3145 JDEP_SOLV (new) = ptr;
3146 JDEP_WFL (new) = wfl;
3147 JDEP_CHAIN (new) = NULL;
3148 JDEP_MISC (new) = NULL_TREE;
3149 JDEP_GET_PATCH (new) = (tree *)NULL;
3151 JDEP_INSERT (ctxp->classd_list, new);
3157 java_check_circular_reference ()
3160 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
3162 tree type = TREE_TYPE (current);
3163 if (CLASS_INTERFACE (TYPE_NAME (type)))
3165 /* Check all interfaces this class extends */
3166 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
3171 n = TREE_VEC_LENGTH (basetype_vec);
3172 for (i = 0; i < n; i++)
3174 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
3175 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
3176 && interface_of_p (type, BINFO_TYPE (vec_elt)))
3177 parse_error_context (lookup_cl (current),
3178 "Cyclic interface inheritance");
3182 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
3183 parse_error_context (lookup_cl (current),
3184 "Cyclic class inheritance");
3189 safe_layout_class (class)
3192 tree save_current_class = current_class;
3193 char *save_input_filename = input_filename;
3194 int save_lineno = lineno;
3196 push_obstacks (&permanent_obstack, &permanent_obstack);
3197 layout_class (class);
3200 current_class = save_current_class;
3201 input_filename = save_input_filename;
3202 lineno = save_lineno;
3203 CLASS_LOADED_P (class) = 1;
3207 jdep_resolve_class (dep)
3212 if (!JDEP_RESOLVED_P (dep))
3215 resolve_class (JDEP_TO_RESOLVE (dep), JDEP_DECL (dep), JDEP_WFL (dep));
3216 JDEP_RESOLVED (dep, decl);
3219 decl = JDEP_RESOLVED_DECL (dep);
3223 complete_class_report_errors (dep);
3229 /* Complete unsatisfied class declaration and their dependencies */
3232 java_complete_class ()
3239 push_obstacks (&permanent_obstack, &permanent_obstack);
3241 /* Process imports and reverse the import on demand list */
3243 if (ctxp->import_demand_list)
3244 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
3246 /* Rever things so we have the right order */
3247 ctxp->class_list = nreverse (ctxp->class_list);
3248 ctxp->classd_list = reverse_jdep_list (ctxp);
3250 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
3252 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
3255 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
3259 if (!(decl = jdep_resolve_class (dep)))
3262 /* Now it's time to patch */
3263 switch (JDEP_KIND (dep))
3266 /* Simply patch super */
3267 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
3269 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
3270 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
3275 /* We do part of the job done in add_field */
3276 tree field_decl = JDEP_DECL (dep);
3277 tree field_type = TREE_TYPE (decl);
3278 push_obstacks (&permanent_obstack, &permanent_obstack);
3279 #if ! JAVA_PROMOTE_TO_INT
3280 if (TREE_CODE (field_type) == RECORD_TYPE)
3282 field_type = promote_type (field_type);
3284 TREE_TYPE (field_decl) = field_type;
3285 SOURCE_FRONTEND_DEBUG
3286 (("Completed field/var decl `%s' with `%s'",
3287 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
3288 IDENTIFIER_POINTER (DECL_NAME (decl))));
3291 case JDEP_METHOD: /* We start patching a method */
3292 case JDEP_METHOD_RETURN:
3298 tree type = promote_type (TREE_TYPE(decl));
3299 JDEP_APPLY_PATCH (dep, type);
3300 SOURCE_FRONTEND_DEBUG
3301 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
3302 "Completing fct `%s' with ret type `%s'":
3303 "Completing arg `%s' with type `%s'"),
3304 IDENTIFIER_POINTER (EXPR_WFL_NODE
3305 (JDEP_DECL_WFL (dep))),
3306 IDENTIFIER_POINTER (DECL_NAME (decl))));
3310 dep = JDEP_CHAIN (dep);
3311 if (JDEP_KIND (dep) == JDEP_METHOD_END)
3314 decl = jdep_resolve_class (dep);
3318 tree mdecl = JDEP_DECL (dep), signature;
3319 push_obstacks (&permanent_obstack, &permanent_obstack);
3320 /* Recompute and reset the signature */
3321 signature = build_java_signature (TREE_TYPE (mdecl));
3322 set_java_signature (TREE_TYPE (mdecl), signature);
3329 case JDEP_INTERFACE:
3330 if (parser_check_super_interface (decl, JDEP_DECL (dep),
3333 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
3337 JDEP_APPLY_PATCH (dep, promote_type (TREE_TYPE (decl)));
3338 SOURCE_FRONTEND_DEBUG
3339 (("Completing variable `%s' with type `%s'",
3340 (TREE_CODE (JDEP_DECL_WFL (dep)) == EXPR_WITH_FILE_LOCATION ?
3341 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))) :
3342 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL_WFL (dep)))),
3343 IDENTIFIER_POINTER (DECL_NAME (decl))));
3347 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
3348 SOURCE_FRONTEND_DEBUG
3349 (("Completing a random type dependency on a '%s' node",
3350 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
3354 JDEP_APPLY_PATCH (dep, promote_type (TREE_TYPE (decl)));
3355 SOURCE_FRONTEND_DEBUG
3356 (("Completing parameter `%s' with type `%s'",
3357 IDENTIFIER_POINTER (JDEP_MISC (dep)),
3358 IDENTIFIER_POINTER (DECL_NAME (decl))));
3362 fatal ("incomplete switch - java_complete_class");
3370 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
3374 resolve_class (class_type, decl, cl)
3375 tree class_type, decl, cl;
3377 char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
3379 tree resolved_type, resolved_type_decl;
3381 /* 1- Check to see if we have an array. If true, find what we really
3383 while (name[0] == '[')
3386 TYPE_NAME (class_type) = get_identifier (name);
3388 /* 2- Resolve the bare type */
3389 if (!(resolved_type_decl = do_resolve_class (class_type, decl, cl)))
3391 resolved_type = TREE_TYPE (resolved_type_decl);
3393 /* 3- If we have and array, reconstruct the array down to its nesting */
3396 while (base != name)
3398 if (TREE_CODE (resolved_type) == RECORD_TYPE)
3399 resolved_type = promote_type (resolved_type);
3400 resolved_type = build_java_array_type (resolved_type, -1);
3403 /* Build a fake decl for this, since this is what is expected to
3405 resolved_type_decl =
3406 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
3407 /* Figure how those two things are important for error report. FIXME */
3408 DECL_SOURCE_LINE (resolved_type_decl) = 0;
3409 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
3411 return resolved_type_decl;
3414 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
3415 are used to report error messages. */
3418 do_resolve_class (class_type, decl, cl)
3423 tree new_class_decl;
3424 tree original_name = NULL_TREE;
3426 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
3427 its is changed by find_in_imports{_on_demand} */
3429 /* 1- Check for the type in single imports */
3430 if (find_in_imports (class_type))
3433 /* 2- And check for the type in the current compilation unit. If it fails,
3434 try with a name qualified with the package name if appropriate. */
3436 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
3438 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
3439 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
3440 load_class (TYPE_NAME (class_type), 0);
3441 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
3444 original_name = TYPE_NAME (class_type);
3445 if (!QUALIFIED_P (TYPE_NAME (class_type)) && ctxp->package)
3446 TYPE_NAME (class_type) = merge_qualified_name (ctxp->package,
3447 TYPE_NAME (class_type));
3448 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
3450 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
3451 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
3452 load_class (TYPE_NAME (class_type), 0);
3453 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
3455 TYPE_NAME (class_type) = original_name;
3457 /* 3- Check an other compilation unit that bears the name of type */
3458 load_class (TYPE_NAME (class_type), 0);
3459 if (check_pkg_class_access (TYPE_NAME (class_type),
3460 (cl ? cl : lookup_cl (decl))))
3463 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
3464 return new_class_decl;
3466 /* 4- Check the import on demands. Don't allow bar.baz to be
3467 imported from foo.* */
3468 if (!QUALIFIED_P (TYPE_NAME (class_type)))
3469 if (find_in_imports_on_demand (class_type))
3472 /* 5- Last call for a resolution */
3473 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
3476 /* Resolve NAME and lay it out (if not done and if not the current
3477 parsed class). Return a decl node. */
3480 resolve_and_layout (name, cl)
3484 tree decl = resolve_no_layout (name, cl);
3485 if (decl && TREE_TYPE (decl) != current_class
3486 && !CLASS_LOADED_P (TREE_TYPE (decl)))
3487 safe_layout_class (TREE_TYPE (decl));
3491 /* Resolve a class, returns its decl but doesn't perform any
3492 layout. The current parsing context is saved and restored */
3495 resolve_no_layout (name, cl)
3499 BUILD_PTR_FROM_NAME (ptr, name);
3500 java_parser_context_save_global ();
3501 decl = resolve_class (ptr, NULL_TREE, cl);
3502 java_parser_context_restore_global ();
3507 /* Called to report errors. Skip leader '[' in a complex array type
3508 description that failed to be resolved. */
3511 purify_type_name (name)
3514 while (*name && *name == '[')
3519 /* The type CURRENT refers to can't be found. We print error messages. */
3522 complete_class_report_errors (dep)
3525 switch (JDEP_KIND (dep))
3529 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
3530 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
3531 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
3535 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
3536 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
3537 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
3539 case JDEP_METHOD: /* Covers arguments */
3541 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
3542 "argument `%s' of method `%s'",
3543 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
3544 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
3545 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
3547 case JDEP_METHOD_RETURN: /* Covers return type */
3549 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
3550 "return type of method `%s'",
3551 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
3552 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
3554 case JDEP_INTERFACE:
3556 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
3557 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
3558 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
3559 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
3563 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
3564 "local variable `%s'",
3565 purify_type_name (IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))),
3566 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
3571 /* Check uninitialized final. */
3579 check_method_redefinition (class, method)
3583 tree cl = DECL_NAME (method);
3584 tree sig = TYPE_LANG_SPECIFIC (TREE_TYPE (method))->signature;
3585 /* decl name of generated <clinit> doesn't need to be fixed and
3587 if (DECL_NAME (method) != clinit_identifier_node)
3589 /* NAME is just the plain name when Object is being defined */
3590 if (class != object_type_node)
3591 name = DECL_NAME (method) = EXPR_WFL_NODE (DECL_NAME (method));
3593 name = DECL_NAME (method);
3598 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
3600 struct lang_type *t = TYPE_LANG_SPECIFIC (TREE_TYPE (redef));
3602 if (! t || (redef == method))
3604 if (DECL_NAME (redef) == name && sig == t->signature)
3606 parse_error_context (cl, "Duplicate method declaration");
3613 /* Check all the methods of CLASS. Methods are first completed then
3614 checked according to regular method existance rules.
3615 If no constructor were encountered, then build its declaration. */
3618 java_check_regular_methods (class_decl)
3622 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
3623 tree super_class = CLASSTYPE_SUPER (class);
3624 int seen_constructor = 0;
3626 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
3628 /* Should take interfaces into account. FIXME */
3629 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
3632 tree method_wfl = DECL_NAME (method);
3635 if (DECL_CONSTRUCTOR_P (method))
3636 seen_constructor = 1;
3638 /* Check for redefinitions */
3639 if (check_method_redefinition (class, method))
3642 sig = build_java_argument_signature (TREE_TYPE (method));
3644 found = lookup_argument_method (super_class, DECL_NAME (method), sig);
3647 /* Can't override a method with the same name and different return
3649 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
3650 parse_warning_context
3652 "Method `%s' redefined with different return type in class `%s'",
3653 lang_printable_name (found),
3654 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
3656 /* Can't override final. Can't override static. */
3657 if (METHOD_FINAL (found) || METHOD_STATIC (found))
3659 /* Static *can* override static */
3660 if (METHOD_STATIC (found) && METHOD_STATIC (method))
3664 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
3665 (METHOD_FINAL (found) ? "Final" : "Static"),
3666 lang_printable_name (found),
3667 (METHOD_FINAL (found) ? "final" : "static"),
3669 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
3672 /* Static method can't override instance method. */
3673 if (METHOD_STATIC (method))
3677 "Instance methods can't be overriden by a static method. Method "
3678 "`%s' is an instance method in class `%s'",
3679 lang_printable_name (found),
3681 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
3684 /* Overriding/hiding public must be public or
3685 overriding/hiding protected must be protected or public */
3686 if ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method)) ||
3687 (METHOD_PROTECTED (found)
3688 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method))))
3692 "Methods can't be overridden to be more private. Method `%s' is "
3693 "%s in class `%s'", lang_printable_name (found),
3694 (METHOD_PUBLIC (found) ? "public" : "protected"),
3696 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
3700 /* If the method has default access in an other package, then
3701 issue a warning that the current method doesn't override the one
3702 that was found elsewhere */
3703 aflags = get_access_flags_from_decl (found);
3704 if ((!aflags || (aflags > ACC_PROTECTED))
3705 && !class_in_current_package (DECL_CONTEXT (found)))
3706 parse_warning_context
3707 (method_wfl, "Method `%s' in class `%s' does not "
3708 "override the corresponding method in class `%s', which is "
3709 "private to a different package",
3710 lang_printable_name (found),
3711 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
3712 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
3714 /* Check on (default) package access. FIXME. */
3715 /* Inheriting multiple methods with the same signature. FIXME */
3718 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
3720 if (!seen_constructor)
3722 /* No constructor seen, we craft one, at line 0 */
3723 int saved_lineno = lineno;
3726 meth = make_node (FUNCTION_TYPE);
3727 TREE_TYPE (meth) = void_type_node;
3728 TYPE_ARG_TYPES (meth) = NULL_TREE;
3729 decl = add_method (class, 0, init_identifier_node,
3730 build_java_signature (meth));
3731 DECL_CONSTRUCTOR_P (decl) = 1;
3732 lineno = saved_lineno;
3736 /* Check abstract method of interface INTERFACE */
3739 java_check_abstract_methods (interface)
3743 tree method, basetype_vec, found;
3745 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
3748 tree name = DECL_NAME (method);
3750 /* 2- Check for double definition inside the defining interface */
3751 if (check_method_redefinition (interface, method))
3754 /* 3- Overriding is OK as far as we preserve the return type and
3755 the thrown exceptions */
3756 found = lookup_java_interface_method2 (interface, method);
3760 (lookup_cl (method),
3761 "Method `%s' previously defined in interface `%s' is "
3762 "redefined with different return type in interface `%s'",
3763 lang_printable_name (found),
3764 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))),
3765 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))));
3770 /* 4- Inherited methods can't differ by their returned types */
3771 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
3773 n = TREE_VEC_LENGTH (basetype_vec);
3774 for (i = 0; i < n; i++)
3776 tree sub_interface_method, sub_interface;
3777 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
3780 sub_interface = BINFO_TYPE (vec_elt);
3781 for (sub_interface_method = TYPE_METHODS (sub_interface);
3782 sub_interface_method;
3783 sub_interface_method = TREE_CHAIN (sub_interface_method))
3785 found = lookup_java_interface_method2 (interface,
3786 sub_interface_method);
3787 if (found && (found != sub_interface_method))
3789 (lookup_cl (sub_interface_method),
3790 "Interface `%s' inherits method `%s' from interface `%s'. This "
3791 "method is redefined with a different return "
3792 "type in interface `%s'",
3793 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
3794 lang_printable_name (found),
3796 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (sub_interface_method)))),
3798 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
3803 /* Check the method on all the defined classes. Should be done to the
3804 classes declared in the compilation unit only. FIXME */
3807 java_check_methods ()
3811 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
3812 if (CLASS_FROM_SOURCE_P (TREE_TYPE (current)))
3814 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current));
3816 if (CLASS_INTERFACE (TYPE_NAME (class)))
3817 java_check_abstract_methods (class);
3819 java_check_regular_methods (current);
3823 /* Lookup methods in interfaces using their name and partial
3824 signature. Return a matching method only if their types differ. */
3827 lookup_java_interface_method2 (class, method_decl)
3828 tree class, method_decl;
3831 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
3836 n = TREE_VEC_LENGTH (basetype_vec);
3837 for (i = 0; i < n; i++)
3839 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
3840 if ((BINFO_TYPE (vec_elt) != object_type_node)
3842 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
3845 for (i = 0; i < n; i++)
3847 to_return = lookup_java_interface_method2
3848 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
3856 /* Lookup method using their name and partial signature. Return a
3857 matching method only if their types differ. */
3860 lookup_java_method2 (clas, method_decl, do_interface)
3861 tree clas, method_decl;
3864 tree method, method_signature, method_name, method_type;
3865 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
3866 method_name = DECL_NAME (method_decl);
3867 method_type = TREE_TYPE (TREE_TYPE (method_decl));
3869 while (clas != NULL_TREE)
3871 for (method = TYPE_METHODS (clas);
3872 method != NULL_TREE; method = TREE_CHAIN (method))
3874 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
3875 if (DECL_NAME (method) == method_name
3876 && method_sig == method_signature
3877 && TREE_TYPE (TREE_TYPE (method)) != method_type)
3882 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
3887 /* Return the line that matches DECL line number. Used during error
3894 static tree cl = NULL_TREE;
3899 if (cl == NULL_TREE)
3900 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
3902 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
3903 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
3908 /* Look for a simple name in the single-type import list */
3911 find_name_in_single_imports (name)
3916 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
3917 if (TREE_VALUE (node) == name)
3918 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
3923 /* Process all single-type import. */
3931 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
3933 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
3935 /* Don't load twice something already defined. */
3936 if (IDENTIFIER_CLASS_VALUE (to_be_found))
3938 QUALIFIED_P (to_be_found) = 1;
3939 load_class (to_be_found, 0);
3941 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
3942 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
3944 parse_error_context (TREE_PURPOSE (import),
3945 "Class or interface `%s' not found in import",
3946 IDENTIFIER_POINTER (to_be_found));
3955 /* Possibly find a class imported by a single-type import statement. Return
3956 1 if an error occured, 0 otherwise. */
3959 find_in_imports (class_type)
3964 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
3965 if (TREE_VALUE (import) == TYPE_NAME (class_type))
3967 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
3968 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
3969 return check_pkg_class_access (TYPE_NAME (class_type),
3970 TREE_PURPOSE (import));
3975 /* Process a import on demand statement (lazy) */
3978 read_import_entry (jcf, dirp, returned_name)
3981 char **returned_name;
3985 struct dirent *direntp = readdir (dirp);
3988 *returned_name = NULL;
3993 *returned_name = direntp->d_name;
3994 return (strlen (direntp->d_name));
3999 int current_dir_len = strlen (jcf->classname);
4000 char *current_entry;
4001 int current_entry_len;
4003 /* Here we read a zip directory as a file directory. The files
4004 we're selecting must have the same root than the directory
4007 ZipDirectory *zipd = (ZipDirectory *)jcf->zipd;
4011 current_entry = ZIPDIR_FILENAME (zipd);
4012 current_entry_len = zipd->filename_length;
4013 while (current_entry_len && current_entry [current_entry_len] != '/')
4014 current_entry_len--;
4015 /* If the path of the current file doesn't match the directory we're
4016 scanning, that the end of the search */
4017 current_entry_len++;
4018 if (strncmp (jcf->classname, current_entry, current_dir_len))
4020 *returned_name = NULL;
4023 /* Ok, we have at least the same path. The position of the last '/'
4024 of the current file we're examining should match the size of
4025 name of the directory we're browsing, otherwise that an entry
4026 belonging to a sub directory, we want to skip it. */
4027 if (current_entry_len != current_dir_len)
4028 zipd = ZIPDIR_NEXT (zipd);
4031 jcf->zipd = ZIPDIR_NEXT (zipd); /* Prepare next read */
4032 *returned_name = ¤t_entry [current_entry_len];
4033 return (zipd->filename_length - current_entry_len);
4039 /* Read a import directory, gathering potential match for further type
4040 references. Indifferently reads a filesystem or a ZIP archive
4044 read_import_dir (wfl)
4047 char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
4048 int name_len = IDENTIFIER_LENGTH (EXPR_WFL_NODE (wfl)), reclen;
4050 tree dirname = ident_subst (name, name_len, "", '.', '/', "");
4051 JCF jcfr, *jcf, *saved_jcf = current_jcf;
4052 char *founddirname, *d_name;
4053 struct ZipFileCache zip_cache;
4058 if (!(founddirname = find_class (name, name_len, jcf, 0)))
4059 fatal ("Can't import `%s'", name);
4060 if (jcf->outofsynch)
4061 jcf_out_of_synch (jcf);
4062 if (jcf->seen_in_zip)
4063 jcf->zipd = ZIPDIR_NEXT ((ZipDirectory *)jcf->zipd);
4065 else if (founddirname && (dirp = opendir (founddirname)))
4067 readdir (dirp); readdir (dirp);
4070 if (!founddirname && !dirp)
4072 static int first = 1;
4076 sprintf (buffer, "Can't find default package `%s'. Check "
4077 "the CLASSPATH environment variable and the access to the "
4084 parse_error_context (wfl, "Package `%s' not found in import", name);
4085 current_jcf = saved_jcf;
4089 /* Here we should have a unified way of retrieving an entry, to be
4091 while ((reclen = read_import_entry (jcf, dirp, &d_name)))
4093 int java_or_class = 0;
4096 && !strcmp (&d_name [reclen-5], ".java"))
4102 if (!java_or_class && (reclen > 6) &&
4103 !strcmp (&d_name [reclen-6], ".class"))
4114 obstack_grow (&temporary_obstack, name, name_len);
4115 obstack_1grow (&temporary_obstack, '/');
4116 obstack_grow0 (&temporary_obstack, d_name, len);
4117 id_name = obstack_finish (&temporary_obstack);
4119 node = get_identifier (id_name);
4120 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
4121 QUALIFIED_P (node) = 1; /* As soon as we turn / into . */
4127 current_jcf = saved_jcf;
4130 /* Possibly find a type in the import on demands specified
4131 types. Returns 1 if an error occured, 0 otherwise. Run throught the
4132 entire list, to detected potential double definitions. */
4135 find_in_imports_on_demand (class_type)
4138 tree node, import, node_to_use;
4142 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
4146 obstack_grow (&temporary_obstack,
4147 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
4148 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
4149 obstack_1grow (&temporary_obstack, '/');
4150 obstack_grow0 (&temporary_obstack,
4151 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
4152 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
4153 id_name = obstack_finish (&temporary_obstack);
4155 node = maybe_get_identifier (id_name);
4156 if (node && IS_A_CLASSFILE_NAME (node))
4160 cl = TREE_PURPOSE (import);
4168 (import, "Type `%s' also potentially defined in package `%s'",
4169 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
4170 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
4177 /* Setup lineno so that it refers to the line of the import (in
4178 case we parse a class file and encounter errors */
4180 int saved_lineno = lineno;
4181 lineno = EXPR_WFL_LINENO (cl);
4182 TYPE_NAME (class_type) = ident_subst (IDENTIFIER_POINTER (node_to_use),
4183 IDENTIFIER_LENGTH (node_to_use),
4185 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
4186 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4187 /* If there is no DECL set for the class or if the class isn't
4188 loaded and not seen in source yet, the load */
4189 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
4190 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
4191 load_class (node_to_use, 0);
4192 lineno = saved_lineno;
4193 return check_pkg_class_access (TYPE_NAME (class_type), cl);
4196 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
4199 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
4200 access violations were found, 1 otherwise. */
4203 check_pkg_class_access (class_name, cl)
4210 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
4213 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
4216 if (!CLASS_PUBLIC (TYPE_NAME (type)))
4219 (cl, "Can't access %s `%s'. Only public classes and interfaces in "
4220 "other packages can be accessed",
4221 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
4222 IDENTIFIER_POINTER (class_name));
4228 /* Local variable declaration. */
4231 declare_local_variables (modifier, type, vlist)
4236 tree decl, current, returned_type, type_wfl, init_stmt = NULL_TREE;
4239 /* Push a new block if statement were seen between the last time we
4240 pushed a block and now. Keep a cound of block to close */
4241 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)))
4243 tree body = DECL_FUNCTION_BODY (current_function_decl);
4244 tree b = enter_block ();
4245 BLOCK_EXPR_ORIGIN(b) = body;
4251 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
4253 (ctxp->modifier_ctx [i],
4254 (modifier == ACC_FINAL ?
4255 "Unsupported JDK1.1 `final' locals" :
4256 "Only `final' is allowed as a local variables modifier"));
4260 if (unresolved_type_p (type, &returned_type))
4263 type = returned_type;
4267 type = obtain_incomplete_type (type);
4272 for (current = vlist; current; current = TREE_CHAIN (current))
4274 tree wfl = TREE_PURPOSE (current);
4275 tree name = EXPR_WFL_NODE (wfl);
4276 tree init = TREE_VALUE (current);
4277 tree other = lookup_name_in_blocks (name);
4279 /* Don't try to use an INIT statement when an error was found */
4280 if (init && java_error_count)
4285 (wfl, "Variable `%s' is already defined in this method and was "
4286 "declared `%s %s' in line %d",
4287 IDENTIFIER_POINTER (name), lang_printable_name (TREE_TYPE (other)),
4288 IDENTIFIER_POINTER (name), DECL_SOURCE_LINE (other));
4291 if (!must_chain && TREE_CODE (type) == RECORD_TYPE)
4292 type = promote_type (type);
4293 /* Never layout this decl. This will be done when its scope
4295 decl = build_decl_no_layout (VAR_DECL, name, type);
4296 BLOCK_CHAIN_DECL (decl);
4298 /* Add the initialization function to the current function's code */
4302 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
4303 java_method_add_stmt
4304 (current_function_decl,
4305 build_debugable_stmt (EXPR_WFL_LINECOL (init), init));
4311 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
4312 dep = CLASSD_LAST (ctxp->classd_list);
4313 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
4317 SOURCE_FRONTEND_DEBUG (("Defined locals"));
4320 /* Called during parsing. Build decls from argument list. */
4323 source_start_java_method (fndecl)
4330 extern tree current_binding_level;
4331 current_function_decl = fndecl;
4333 /* New scope for the function */
4335 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
4336 tem != NULL_TREE; tem = TREE_CHAIN (tem), i++)
4338 tree type = TREE_VALUE (tem);
4339 tree name = TREE_PURPOSE (tem);
4341 /* If type is incomplete. Layout can't take place
4342 now. Create an incomplete decl and ask for the decl to be
4344 if (INCOMPLETE_TYPE_P (type))
4347 parm_decl = build_decl_no_layout (PARM_DECL, name, type);
4349 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
4350 jdep = CLASSD_LAST (ctxp->classd_list);
4351 JDEP_MISC (jdep) = name;
4352 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
4355 parm_decl = build_decl (PARM_DECL, name, type);
4357 BLOCK_CHAIN_DECL (parm_decl);
4359 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
4360 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
4362 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
4365 /* Called during expansion. Push decls formerly built from argument
4366 list so they're usable during expansion. */
4369 expand_start_java_method (fndecl)
4375 extern tree current_binding_level;
4376 current_function_decl = fndecl;
4378 announce_function (fndecl);
4379 pushlevel (1); /* Push parameters */
4380 ptr = &DECL_ARGUMENTS (fndecl);
4381 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
4384 tree next = TREE_CHAIN (tem);
4385 DECL_ARG_TYPE (tem) = TREE_TYPE (tem);
4386 layout_decl (tem, 0);
4388 INITIALIZED_P (tem) = 1; /* Parms are initialized */
4390 ptr = &TREE_CHAIN (tem);
4394 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
4395 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
4396 complete_start_java_method (fndecl);
4399 /* Terminate a function and expand its body. */
4402 source_end_java_method ()
4404 tree fndecl = current_function_decl;
4406 java_parser_context_save_global ();
4407 lineno = ctxp->last_ccb_indent1;
4409 /* Generate function's code */
4410 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
4411 && ! flag_emit_class_files)
4412 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
4414 /* pop out of its parameters */
4415 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
4417 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
4419 /* Generate rtl for function exit. */
4420 if (! flag_emit_class_files)
4422 lineno = DECL_SOURCE_LINE_LAST (fndecl);
4423 expand_function_end (input_filename, lineno, 0);
4425 /* Run the optimizers and output assembler code for this function. */
4426 rest_of_compilation (fndecl);
4429 current_function_decl = NULL_TREE;
4430 /* permanent_allocation (1); */
4431 java_parser_context_restore_global ();
4434 /* Record EXPR in the current function block. Complements compound
4435 expression second operand if necessary. */
4438 java_method_add_stmt (fndecl, expr)
4441 tree body = BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl));
4444 if (java_error_count)
4446 if ((node = add_stmt_to_compound (body, NULL_TREE, expr)) == body)
4449 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)) = node;
4450 TREE_SIDE_EFFECTS (node) = 1;
4454 /* Add STMT to EXISTING if possible, otherwise create a new
4455 COMPOUND_EXPR and add STMT to it. */
4458 add_stmt_to_compound (existing, type, stmt)
4459 tree existing, type, stmt;
4463 if (existing && (TREE_CODE (existing) == COMPOUND_EXPR)
4464 && TREE_OPERAND (existing, 1) == size_zero_node)
4466 TREE_OPERAND (existing, 1) = stmt;
4467 TREE_TYPE (existing) = type;
4471 node = build (COMPOUND_EXPR, type, existing, stmt);
4473 node = build (COMPOUND_EXPR, type, stmt, size_zero_node);
4478 /* Hold THIS for the scope of the current public method decl. */
4479 static tree current_this;
4481 /* Layout all class found during parsing */
4484 java_layout_classes ()
4487 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4489 current_class = TREE_TYPE (current);
4490 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
4491 if (!TYPE_SIZE (current_class))
4492 safe_layout_class (current_class);
4496 /* Expand all methods in all registered classes. */
4499 java_complete_expand_methods ()
4503 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
4505 extern tree current_constant_pool_data_ref;
4506 tree class_type = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current));
4510 current_class = TREE_TYPE (current);
4512 /* Initialize a new constant pool */
4513 init_outgoing_cpool ();
4515 /* Don't process function bodies in interfaces */
4516 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
4517 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
4519 current_function_decl = decl;
4520 /* Don't generate debug info on line zero when expanding a
4521 generated constructor. */
4522 if (DECL_CONSTRUCTOR_P (decl) && !DECL_FUNCTION_BODY (decl))
4524 /* If we found errors, it's too dangerous to try to generate
4525 and expand a constructor */
4526 if (!java_error_count)
4528 restore_line_number_status (1);
4529 java_complete_expand_method (decl);
4530 restore_line_number_status (0);
4534 java_complete_expand_method (decl);
4537 /* Make the class data, register it and run the rest of decl
4538 compilation on it */
4539 if (!java_error_count && ! flag_emit_class_files)
4541 make_class_data (current_class);
4543 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
4548 /* Complete and expand a method. */
4551 java_complete_expand_method (mdecl)
4556 int no_ac_found = 1;
4558 /* We generate some code for an empty constructor */
4559 if (DECL_CONSTRUCTOR_P (mdecl) && !DECL_FUNCTION_BODY (mdecl))
4561 tree arg_list, func, call;
4562 tree method_type = TREE_TYPE (mdecl);
4563 tree class_type = CLASS_TO_HANDLE_TYPE (current_class);
4564 tree self_type = (CLASSTYPE_SUPER (class_type) ?
4565 CLASSTYPE_SUPER (class_type) : class_type);
4566 tree method_signature =
4567 TYPE_LANG_SPECIFIC (method_type)->signature;
4569 lookup_java_constructor (CLASS_TO_HANDLE_TYPE (self_type),
4571 tree block, compound;
4573 /* Fixe the begining/ending lines of the method so that with
4574 no_line_numbers set to 1 it doesn't generate debug info at
4575 line 1 for this artificial constructor. */
4576 DECL_SOURCE_LINE (mdecl) = 1;
4577 DECL_SOURCE_LINE_MERGE (mdecl, 1);
4578 source_start_java_method (mdecl);
4579 arg_list = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl));
4581 func = build_known_method_ref (method, method_type, self_type,
4582 method_signature, arg_list);
4584 if (! flag_emit_class_files)
4585 func = build1 (NOP_EXPR, build_pointer_type (method_type), func);
4586 call = build (CALL_EXPR, TREE_TYPE (method_type), func,
4587 build_tree_list (NULL_TREE, arg_list), NULL_TREE);
4588 TREE_SIDE_EFFECTS (call) = 1;
4589 call = build_class_init (self_type, call);
4590 compound = java_method_add_stmt (mdecl, call);
4591 block = exit_block ();
4592 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = block;
4593 /* The function decl, its block and the compound statement
4594 within this block are all of void type. */
4595 TREE_TYPE (block) = TREE_TYPE (compound) =
4596 TREE_TYPE (DECL_FUNCTION_BODY (mdecl)) = void_type_node;
4601 if (DECL_FUNCTION_BODY (mdecl))
4603 expand_start_java_method (mdecl);
4606 = (!METHOD_STATIC (mdecl) ?
4607 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
4609 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) && no_ac_found)
4610 java_complete_tree (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)));
4611 /* Don't go any further if we've found error(s) during the
4613 if (!java_error_count)
4614 source_end_java_method ();
4618 /* Expand finals. */
4621 java_expand_finals ()
4625 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
4626 a tree list node containing RIGHT. Fore coming RIGHTs will be
4627 chained to this hook. LOCATION contains the location of the
4628 separating `.' operator. */
4631 make_qualified_primary (primary, right, location)
4632 tree primary, right;
4637 /* We want to process THIS . xxx symbolicaly, to keep it consistent
4638 with the way we're processing SUPER. A THIS from a primary as a
4639 different form than a SUPER. Turn THIS into something symbolic */
4640 if (TREE_CODE (primary) == JAVA_THIS_EXPR)
4642 wfl = build_wfl_node (this_identifier_node, input_filename, 0, 0);
4643 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
4644 wfl = make_qualified_name (wfl, right, location);
4645 PRIMARY_P (wfl) = 1;
4648 /* Other non WFL node are wrapped around a WFL */
4649 else if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
4651 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
4652 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
4653 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (primary, NULL_TREE);
4658 if (!EXPR_WFL_QUALIFICATION (primary))
4659 EXPR_WFL_QUALIFICATION (primary) =
4660 build_tree_list (primary, NULL_TREE);
4663 EXPR_WFL_LINECOL (right) = location;
4664 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
4665 PRIMARY_P (wfl) = 1;
4669 /* Simple merge of two name separated by a `.' */
4672 merge_qualified_name (left, right)
4676 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
4677 IDENTIFIER_LENGTH (left));
4678 obstack_1grow (&temporary_obstack, '.');
4679 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
4680 IDENTIFIER_LENGTH (right));
4681 node = get_identifier (obstack_base (&temporary_obstack));
4682 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
4683 QUALIFIED_P (node) = 1;
4687 /* Merge the two parts of a qualified name into LEFT. Set the
4688 location information of the resulting node to LOCATION, usually
4689 inherited from the location information of the `.' operator. */
4692 make_qualified_name (left, right, location)
4697 tree left_id = EXPR_WFL_NODE (left);
4698 tree right_id = EXPR_WFL_NODE (right);
4701 merge = merge_qualified_name (left_id, right_id);
4703 /* Left wasn't qualified and is now qualified */
4704 if (!QUALIFIED_P (left_id))
4706 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
4707 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
4708 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
4711 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
4712 EXPR_WFL_LINECOL (wfl) = location;
4713 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
4715 EXPR_WFL_NODE (left) = merge;
4719 /* Extract the last identifier component of the qualified in WFL. The
4720 last identifier is removed from the linked list */
4723 cut_identifier_in_qualified (wfl)
4727 tree previous = NULL_TREE;
4728 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
4729 if (!TREE_CHAIN (q))
4732 fatal ("Operating on a non qualified qualified WFL - "
4733 "cut_identifier_in_qualified");
4734 TREE_CHAIN (previous) = NULL_TREE;
4735 return TREE_PURPOSE (q);
4739 /* Resolve the expression name NAME. Return its decl. */
4742 resolve_expression_name (id)
4745 tree name = EXPR_WFL_NODE (id);
4748 /* 6.5.5.1: Simple expression names */
4749 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
4751 /* 15.13.1: NAME can appear within the scope of a local variable
4753 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
4756 /* 15.13.1: NAME can appear within a class declaration */
4759 decl = lookup_field_wrapper (current_class, name);
4762 int fs = FIELD_STATIC (decl);
4763 /* Instance variable (8.3.1.1) can't appear within
4764 static method, static initializer or initializer for
4765 a static variable. */
4766 if (!fs && METHOD_STATIC (current_function_decl))
4769 (id, "Can't make a static reference to nonstatic variable "
4770 "`%s' in class `%s'",
4771 IDENTIFIER_POINTER (name),
4772 IDENTIFIER_POINTER (DECL_NAME
4773 (TYPE_NAME (current_class))));
4774 return error_mark_node;
4776 decl = build_field_ref ((fs ? NULL_TREE : current_this),
4777 current_class, name);
4778 return (fs ? build_class_init (current_class, decl) : decl);
4780 /* Fall down to error report on undefined variable */
4783 /* 6.5.5.2 Qualified Expression Names */
4786 qualify_ambiguous_name (id);
4787 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
4788 /* 15.10.2: Accessing Superclass Members using super */
4789 return resolve_field_access (id, NULL, NULL);
4792 /* We've got an error here */
4793 parse_error_context (id, "Undefined variable `%s'",
4794 IDENTIFIER_POINTER (name));
4796 return error_mark_node;
4799 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
4800 We return something suitable to generate the field access. We also
4801 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
4802 recipient's address can be null. */
4805 resolve_field_access (qual_wfl, field_decl, field_type)
4807 tree *field_decl, *field_type;
4811 tree decl, where_found, type_found;
4813 if (resolve_qualified_expression_name (qual_wfl, &decl,
4814 &where_found, &type_found))
4815 return error_mark_node;
4817 /* Resolve the LENGTH field of an array here */
4818 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
4819 && ! flag_emit_class_files)
4821 tree length = build_java_array_length_access (where_found);
4823 build_java_arraynull_check (type_found, length, int_type_node);
4825 /* We might have been trying to resolve field.method(). In which
4826 case, the resolution is over and decl is the answer */
4827 else if (DECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
4829 else if (DECL_P (decl))
4831 is_static = DECL_P (decl) && FIELD_STATIC (decl);
4832 field_ref = build_field_ref ((is_static ? NULL_TREE : where_found),
4833 type_found, DECL_NAME (decl));
4834 if (field_ref == error_mark_node)
4835 return error_mark_node;
4838 field_ref = build_class_init (type_found, field_ref);
4839 /* If the static field was identified by an expression that
4840 needs to be generated, make the field access a compound
4841 expression whose first part of the evaluation of the
4842 field selector part. */
4843 if (where_found && TREE_CODE (where_found) != TYPE_DECL)
4845 tree type = QUAL_DECL_TYPE (field_ref);
4846 field_ref = build (COMPOUND_EXPR, type, where_found, field_ref);
4856 *field_type = QUAL_DECL_TYPE (decl);
4860 /* 6.5.5.2: Qualified Expression Names */
4863 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
4865 tree *found_decl, *type_found, *where_found;
4867 int from_type = 0; /* Field search initiated from a type */
4868 int from_super = 0, from_cast = 0;
4869 int previous_call_static = 0;
4871 tree decl = NULL_TREE, type = NULL_TREE, q;
4872 *where_found = NULL_TREE;
4874 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
4876 tree qual_wfl = QUAL_WFL (q);
4878 /* 15.10.1 Field Access Using a Primary */
4880 switch (TREE_CODE (qual_wfl))
4883 case JAVA_NEW_CLASS_EXPR:
4884 /* If the access to the function call is a non static field,
4885 build the code to access it. */
4886 if (DECL_P (decl) && !FIELD_STATIC (decl))
4888 decl = maybe_access_field (decl, *where_found, type);
4889 if (decl == error_mark_node)
4892 /* And code for the function call */
4893 if (complete_function_arguments (qual_wfl))
4896 patch_method_invocation_stmt (qual_wfl, decl, type, &is_static);
4897 if (*where_found == error_mark_node)
4899 *type_found = type = QUAL_DECL_TYPE (*where_found);
4901 /* If the previous call was static and this one is too,
4902 build a compound expression to hold the two (because in
4903 that case, previous function calls aren't transported as
4904 forcoming function's argument. */
4905 if (previous_call_static && is_static)
4907 decl = build (COMPOUND_EXPR, type, decl, *where_found);
4908 TREE_SIDE_EFFECTS (decl) = 1;
4912 previous_call_static = is_static;
4913 decl = *where_found;
4918 *where_found = decl = java_complete_tree (qual_wfl);
4919 if (decl == error_mark_node)
4921 *type_found = type = QUAL_DECL_TYPE (decl);
4926 /* If the access to the function call is a non static field,
4927 build the code to access it. */
4928 if (DECL_P (decl) && !FIELD_STATIC (decl))
4930 decl = maybe_access_field (decl, *where_found, type);
4931 if (decl == error_mark_node)
4934 /* And code for the array reference expression */
4935 decl = java_complete_tree (qual_wfl);
4936 if (decl == error_mark_node)
4938 type = QUAL_DECL_TYPE (decl);
4942 /* If we fall here, we weren't processing a (static) function call. */
4943 previous_call_static = 0;
4945 /* It can be the keyword THIS */
4946 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
4951 (wfl, "Keyword `this' used outside allowed context");
4954 /* We have to generate code for intermediate acess */
4955 *where_found = decl = current_this;
4956 type = QUAL_DECL_TYPE (decl);
4960 /* 15.10.2 Accessing Superclass Members using SUPER */
4961 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
4964 /* Check on the restricted use of SUPER */
4965 if (METHOD_STATIC (current_function_decl)
4966 || current_class == object_type_node)
4969 (wfl, "Keyword `super' used outside allowed context");
4972 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
4973 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
4974 CLASSTYPE_SUPER (current_class),
4975 build_this (EXPR_WFL_LINECOL (qual_wfl)));
4976 *where_found = decl = java_complete_tree (node);
4977 *type_found = type = QUAL_DECL_TYPE (decl);
4978 from_super = from_type = 1;
4982 /* 15.13.1: Can't search for field name in packages, so we
4983 assume a variable/class name was meant. */
4984 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
4986 if (from_super || from_cast)
4988 ((from_cast ? qual_wfl : wfl),
4989 "No variable `%s' defined in class `%s'",
4990 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
4991 lang_printable_name (type));
4994 (qual_wfl, "Undefined variable or class name: `%s'",
4995 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
4999 /* We have a type name. It's been already resolved when the
5000 expression was qualified. */
5001 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
5003 if (!(decl = QUAL_RESOLUTION (q)))
5004 return 1; /* Error reported already */
5006 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
5009 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
5010 java_accstring_lookup (get_access_flags_from_decl (decl)),
5011 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
5012 IDENTIFIER_POINTER (DECL_NAME (decl)),
5013 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
5017 type = TREE_TYPE (decl);
5020 /* We resolve and expression name */
5025 /* If there exists an early resolution, use it. That occurs
5026 only once and we know that there are more things to
5027 come. Don't do that when processing something after SUPER
5028 (we need more thing to be put in place below */
5029 if (!from_super && QUAL_RESOLUTION (q))
5030 decl = QUAL_RESOLUTION (q);
5032 /* We have to search for a field, knowing the type of its
5033 container. The flag FROM_TYPE indicates that we resolved
5034 the last member of the expression as a type name, which
5035 means that for the resolution of this field, will check
5036 on other errors than if the it was resolved as a member
5037 of an other field. */
5041 if (!from_type && !JREFERENCE_TYPE_P (type))
5044 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
5045 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
5046 lang_printable_name (type),
5047 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
5052 lookup_field_wrapper (type, EXPR_WFL_NODE (qual_wfl))))
5055 (qual_wfl, "No variable `%s' defined in class `%s'",
5056 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
5057 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
5061 /* Check on accessibility here */
5062 if (not_accessible_p (type, field_decl, from_super))
5066 "Can't access %s field `%s.%s' from `%s'",
5067 java_accstring_lookup
5068 (get_access_flags_from_decl (field_decl)),
5069 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
5070 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
5072 (DECL_NAME (TYPE_NAME (current_class))));
5076 /* There are things to check when fields are accessed
5077 from type. There are no restrictions on a static
5078 declaration of the field when it is accessed from an
5080 is_static = FIELD_STATIC (field_decl);
5081 if (!from_super && from_type
5082 && !TYPE_INTERFACE_P (type) && !is_static)
5085 (qual_wfl, "Can't make a static reference to nonstatic "
5086 "variable `%s' in class `%s'",
5087 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
5088 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
5091 from_cast = from_super = 0;
5093 /* If we need to generate something to get a proper handle
5094 on what this field is accessed from, do it now. */
5097 decl = maybe_access_field (decl, *where_found, type);
5098 if (decl == error_mark_node)
5102 /* We want to keep the location were found it, and the type
5104 *where_found = decl;
5107 /* This is the decl found and eventually the next one to
5113 type = QUAL_DECL_TYPE (decl);
5120 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
5121 can't be accessed from REFERENCE (a record type). */
5123 int not_accessible_p (reference, member, from_super)
5124 tree reference, member;
5127 int access_flag = get_access_flags_from_decl (member);
5129 /* Access always granted for members declared public */
5130 if (access_flag & ACC_PUBLIC)
5133 /* Check access on protected members */
5134 if (access_flag & ACC_PROTECTED)
5136 /* Access granted if it occurs from within the package
5137 containing the class in which the protected member is
5139 if (class_in_current_package (DECL_CONTEXT (member)))
5142 if (TREE_CODE (member) == FUNCTION_DECL && DECL_CONSTRUCTOR_P (member))
5144 /* Access from SUPER is granted */
5147 /* Otherwise, access isn't granted */
5152 /* If accessed with the form `super.member', then access is
5157 /* Otherwise, access is granted if occuring from the class where
5158 member is declared or a subclass of it */
5159 if (inherits_from_p (reference, current_class))
5165 /* Check access on private members. Access is granted only if it
5166 occurs from within the class in witch it is declared*/
5168 if (access_flag & ACC_PRIVATE)
5169 return (current_class == DECL_CONTEXT (member) ? 0 : 1);
5171 /* Default access are permitted only when occuring within the
5172 package in which the type (REFERENCE) is declared. In other words,
5173 REFERENCE is defined in the current package */
5175 return !class_in_current_package (reference);
5177 /* Otherwise, access is granted */
5181 /* Returns 1 if class was declared in the current package, 0 otherwise */
5184 class_in_current_package (class)
5187 static tree cache = NULL_TREE;
5194 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
5196 /* If the current package is empty and the name of CLASS is
5197 qualified, class isn't in the current package. If there is a
5198 current package and the name of the CLASS is not qualified, class
5199 isn't in the current package */
5200 if (!ctxp->package && qualified_flag || ctxp->package && !qualified_flag)
5203 /* If there is not package and the name of CLASS isn't qualified,
5204 they belong to the same unnamed package */
5205 if (!ctxp->package && !qualified_flag)
5208 /* Compare the left part of the name of CLASS with the package name */
5209 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
5210 if (ctxp->package == left)
5218 /* This function may generate code to access DECL from WHERE. This is
5219 done only if certain conditions meet. */
5222 maybe_access_field (decl, where, type)
5223 tree decl, where, type;
5225 if (DECL_P (decl) && decl != current_this
5226 && (!(TREE_CODE (decl) != PARM_DECL
5227 && FIELD_STATIC (decl)))
5228 && !IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)))
5229 decl = build_field_ref (where ? where : current_this,
5230 type, DECL_NAME (decl));
5234 /* Build a method invocation statement, by patching PATCH. If non NULL
5235 and according to the situation, PRIMARY and WHERE may be
5236 used. IS_STATIC is set to 1 if the invoked function is static. */
5239 patch_method_invocation_stmt (patch, primary, where, is_static)
5240 tree patch, primary, where;
5243 tree wfl = TREE_OPERAND (patch, 0);
5244 tree args = TREE_OPERAND (patch, 1);
5245 tree name = EXPR_WFL_NODE (wfl);
5246 tree list, class_type;
5248 /* Should be overriden if everything goes well. Otherwise, if
5249 something fails, it should keep this value. It stop the
5250 evaluation of a bogus assignment. See java_complete_tree,
5251 MODIFY_EXPR: for the reasons why we sometimes want to keep on
5252 evaluating an assignment */
5253 TREE_TYPE (patch) = error_mark_node;
5255 /* Since lookup functions are messing with line numbers, save the
5257 java_parser_context_save_global ();
5259 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
5261 /* Resolution of qualified name, excluding constructors */
5262 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
5264 tree class_decl, identifier, identifier_wfl;
5265 /* Extract the last IDENTIFIER of the qualified
5266 expression. This is a wfl and we will use it's location
5267 data during error report. */
5268 identifier_wfl = cut_identifier_in_qualified (wfl);
5269 identifier = EXPR_WFL_NODE (identifier_wfl);
5271 /* Given the context, IDENTIFIER is syntactically qualified
5272 as a MethodName. We need to qualify what's before */
5273 qualify_ambiguous_name (wfl);
5275 /* Package resolution are erroneous */
5276 if (RESOLVE_PACKAGE_NAME_P (wfl))
5279 breakdown_qualified (&remainder, NULL, EXPR_WFL_NODE (wfl));
5280 parse_error_context (wfl, "Can't search method `%s' in package "
5281 "`%s'",IDENTIFIER_POINTER (identifier),
5282 IDENTIFIER_POINTER (remainder));
5283 return error_mark_node;
5285 /* We're resolving a call from a type */
5286 else if (RESOLVE_TYPE_NAME_P (wfl))
5288 tree decl = QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl));
5289 tree name = DECL_NAME (decl);
5292 class_decl = resolve_and_layout (name, wfl);
5293 if (CLASS_INTERFACE (decl))
5296 (identifier_wfl, "Can't make static reference to method "
5297 "`%s' in interface `%s'", IDENTIFIER_POINTER (identifier),
5298 IDENTIFIER_POINTER (name));
5299 return error_mark_node;
5301 /* Look the method up in the type selector. The method ought
5303 type = TREE_TYPE (class_decl);
5304 list = lookup_method_invoke (0, wfl, type, identifier, args);
5305 if (list && !METHOD_STATIC (list))
5307 char *fct_name = strdup ((char *)lang_printable_name (list));
5310 "Can't make static reference to method `%s %s' in class `%s'",
5311 lang_printable_name (TREE_TYPE (TREE_TYPE (list))), fct_name,
5312 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
5314 return error_mark_node;
5317 /* We're resolving an expression name */
5322 /* 1- Find the field to which the call applies */
5323 field = resolve_field_access (wfl, NULL, &type);
5324 if (field == error_mark_node)
5325 return error_mark_node;
5327 /* 2- Do the layout of the class where the last field
5328 was found, so we can search it. */
5330 resolve_and_layout (DECL_NAME (TYPE_NAME (type)), NULL_TREE);
5332 /* 3- Retrieve a filtered list of method matches, Refine
5333 if necessary. In any cases, point out errors. */
5334 list = lookup_method_invoke (0, identifier_wfl, type,
5337 /* 4- Add the field as an argument */
5338 args = tree_cons (NULL_TREE, field, args);
5341 /* CLASS_TYPE is used during the call to not_accessible_p and
5342 IDENTIFIER_WFL will be used to report any problem further */
5343 class_type = TREE_TYPE (class_decl);
5344 wfl = identifier_wfl;
5346 /* Resolution of simple names, names generated after a primary: or
5350 tree class_to_search;
5351 int lc; /* Looking for Constructor */
5353 /* We search constructor in their target class */
5354 if (CALL_CONSTRUCTOR_P (patch))
5356 class_to_search = resolve_no_layout (EXPR_WFL_NODE (wfl), NULL_TREE);
5357 if (!class_to_search)
5360 (wfl, "Class `%s' not found in type declaration",
5361 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5362 return error_mark_node;
5365 /* Can't instantiate an abstract class */
5366 if (CLASS_ABSTRACT (class_to_search))
5369 (wfl, "Class `%s' is an abstract class. It can't be "
5370 "instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
5371 return error_mark_node;
5373 class_to_search = TREE_TYPE (class_to_search);
5376 /* This is a regular search in the local class, unless an
5377 alternate class is specified. */
5380 class_to_search = (where ? where : current_class);
5384 /* NAME is a simple identifier or comes from a primary. Search
5385 in the class whose declaration contain the method being
5387 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
5389 /* Don't continue if no method were found, as the next statement
5390 can't be executed then. */
5391 if (!list) return error_mark_node;
5393 /* Check for static reference if non static methods */
5394 if (check_for_static_method_reference (wfl, patch, list,
5395 class_to_search, primary))
5396 return error_mark_node;
5398 /* Non static/constructor methods are called with the current
5399 object extra argument. If method is resolved as a primary,
5400 use the primary otherwise use the current THIS. */
5401 if (!CALL_CONSTRUCTOR_P (patch) && !METHOD_STATIC (list))
5402 args = tree_cons (NULL_TREE, primary ? primary : current_this, args);
5404 class_type = class_to_search;
5407 /* Merge point of all resolution schemes. If we have nothing, this
5408 is an error, already signaled */
5409 if (!list) return error_mark_node;
5411 /* Check accessibility, position the is_static flag, build and
5413 if (not_accessible_p (class_type, list, 0))
5415 char *fct_name = strdup ((char *)lang_printable_name (list));
5417 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
5418 java_accstring_lookup (get_access_flags_from_decl (list)),
5419 lang_printable_name (TREE_TYPE (TREE_TYPE (list))),
5420 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class_type))), fct_name,
5421 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
5423 return error_mark_node;
5427 *is_static = METHOD_STATIC (list);
5428 java_parser_context_restore_global ();
5429 return patch_invoke (patch, list, args, wfl);
5432 /* Check that we're not trying to do a static reference to a method in
5433 non static method. Return 1 if it's the case, 0 otherwise. */
5436 check_for_static_method_reference (wfl, node, method, where, primary)
5437 tree wfl, node, method, where, primary;
5439 if (METHOD_STATIC (current_function_decl)
5440 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
5442 char *fct_name = strdup ((char *)lang_printable_name (method));
5444 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
5445 lang_printable_name (TREE_TYPE (TREE_TYPE (method))), fct_name,
5446 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
5453 /* Patch an invoke expression METHOD and ARGS, based on its invocation
5457 patch_invoke (patch, method, args, cl)
5458 tree patch, method, args;
5462 tree signature = build_java_signature (TREE_TYPE (method));
5465 switch (invocation_mode (method, 0))
5467 case INVOKE_VIRTUAL:
5468 dtable = invoke_build_dtable (0, args);
5469 func = build_invokevirtual (dtable, method);
5472 func = build_known_method_ref (method, TREE_TYPE (method),
5473 DECL_CONTEXT (method),
5475 args = nreverse (args);
5479 fatal ("Unknown invocation mode - build_invoke");
5484 /* Ensure self_type is initialized, (invokestatic). FIXME */
5485 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
5486 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
5487 TREE_OPERAND (patch, 0) = func;
5488 TREE_OPERAND (patch, 1) = args;
5489 original_call = patch;
5491 /* We're calling a constructor. New is called an its returned value
5492 is an argument to the constructor. We build a COMPOUND_EXPR and
5493 use saved expression so that the overall NEW expression value is
5494 a pointer to a newly created and initialized class. */
5495 if (CALL_CONSTRUCTOR_P (original_call))
5497 tree class = DECL_CONTEXT (method);
5498 tree c1, saved_new, size, new;
5499 if (!TYPE_SIZE (class))
5500 safe_layout_class (class);
5501 size = size_in_bytes (class);
5502 new = build (CALL_EXPR, promote_type (class),
5503 build_address_of (alloc_object_node),
5504 tree_cons (NULL_TREE, build_class_ref (class),
5505 build_tree_list (NULL_TREE,
5506 size_in_bytes (class))),
5508 saved_new = save_expr (new);
5509 c1 = build_tree_list (NULL_TREE, saved_new);
5510 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
5511 TREE_OPERAND (original_call, 1) = c1;
5512 TREE_SET_CODE (original_call, CALL_EXPR);
5513 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
5519 invocation_mode (method, super)
5523 int access = get_access_flags_from_decl (method);
5525 if (access & ACC_STATIC)
5526 return INVOKE_STATIC;
5528 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
5529 return INVOKE_STATIC;
5532 return INVOKE_SUPER;
5534 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
5535 return INVOKE_INTERFACE;
5537 if (DECL_CONSTRUCTOR_P (method))
5538 return INVOKE_STATIC;
5540 return INVOKE_VIRTUAL;
5543 /* Retrieve a refined list of matching methods. */
5546 lookup_method_invoke (lc, cl, class, name, arg_list)
5549 tree class, name, arg_list;
5551 tree method = make_node (FUNCTION_TYPE);
5552 tree arg_type_list = NULL_TREE;
5553 tree signature, list, node, scratch;
5555 for (node = arg_list; node; node = TREE_CHAIN (node))
5559 build_tree_list (NULL_TREE,
5560 promote_type (TREE_TYPE (TREE_VALUE (node))));
5561 arg_type_list = chainon (current_arg, arg_type_list);
5563 TYPE_ARG_TYPES (method) = arg_type_list;
5567 signature = build_java_argument_signature (method);
5568 list = match_java_method (class, name, signature);
5569 list = refine_accessible_methods_list (lc, list);
5573 TREE_TYPE (method) = void_type_node;
5574 signature = build_java_signature (method);
5575 list = lookup_java_constructor (class, signature);
5580 parse_error_context (cl, "Can't find method `%s(%s)' in class `%s'",
5581 IDENTIFIER_POINTER (name),
5582 IDENTIFIER_POINTER (signature),
5583 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))));
5590 if (TREE_CHAIN (list))
5592 tree most_specific_list = NULL_TREE;
5594 /* 15.11.2.2 Choose the Most Specific Method */
5595 for (current = list; current; current = TREE_CHAIN (current))
5598 tree method = TREE_VALUE (list);
5599 tree class_from = DECL_CONTEXT (method);
5600 for (rest = TREE_CHAIN (current); rest; rest = TREE_CHAIN (rest))
5602 tree other = TREE_VALUE (rest);
5604 /* METHOD can be declared more specific with regard to OTHER iif:
5606 - The class METHOD belongs can be converted to the
5607 class OTHER belongs by method invocation conversion
5608 (5.3). Since we're dealing with classes here, it is
5609 covered by the identity conversion or the windening
5610 primitive conversion.
5612 - The types of the arguments of METHOD can be
5613 converted to the types of the arguments of OTHER by
5614 method invocation conversion (5.3). */
5616 if (valid_ref_assignconv_cast_p (class_from,
5617 DECL_CONTEXT (other), 0)
5618 && 1) /* Test on args non implemented */
5619 most_specific_list = tree_cons (NULL_TREE, method,
5620 most_specific_list);
5623 list = most_specific_list;
5626 if (!list || TREE_CHAIN (list))
5628 parse_error_context (cl, "Can't find method `%s(%s)' in class `%s'",
5629 IDENTIFIER_POINTER (name),
5630 IDENTIFIER_POINTER (signature),
5631 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))));
5635 /* 15.11.3 Is the Chosen Method Appropriate ? */
5637 return TREE_VALUE (list);
5640 /* Refine accessible methods from the raw matching method list, as
5641 specified in 15.11.4.3. Return a (possibly empty) new method
5645 refine_accessible_methods_list (lc, list)
5646 int lc; /* Looking for Constructor */
5649 #define ADD_TO_LIST_AND_CONTINUE \
5651 refined_list = tree_cons (NULL_TREE, method, refined_list); \
5654 tree node, refined_list = NULL_TREE;
5655 tree current_class_name = DECL_NAME (TYPE_NAME (current_class));
5657 for (node = list; node; node = TREE_CHAIN (node))
5659 int access, identical;
5660 tree class_from, method, class_from_name;
5662 method = TREE_VALUE (node);
5664 /* Constructor not retained here, unless were specifically
5665 looking for them. */
5666 if (lc && DECL_CONSTRUCTOR_P (method))
5667 ADD_TO_LIST_AND_CONTINUE;
5669 access = get_access_flags_from_decl (method);
5670 class_from = DECL_CONTEXT (method);
5671 class_from_name = DECL_NAME (TYPE_NAME (class_from));
5673 identical = identical_subpath_p (current_class_name, class_from_name);
5675 /* Check accessibility of class_from from the current one: This
5676 test has been already carried out when qualify_ambiguous_name
5677 tried to resolve a type found in an other package. It is not
5678 necessary to retest things here, the error has been already
5681 /* Public method are always OK */
5682 if (access & ACC_PUBLIC)
5683 ADD_TO_LIST_AND_CONTINUE;
5685 /* Protected method access is OK if classes are from the
5686 same package or part of the same inheritance lineage */
5687 if ((access & ACC_PROTECTED)
5688 && (inherits_from_p (current_class, class_from) || identical))
5689 ADD_TO_LIST_AND_CONTINUE;
5691 /* Methods with default (package) access are OK if classes are
5692 from the same default package. */
5694 (!QUALIFIED_P (class_from_name) && !QUALIFIED_P (current_class_name)))
5695 ADD_TO_LIST_AND_CONTINUE;
5697 /* Private method accessible iff current class is the node where
5698 the method is defined */
5699 if ((access & ACC_PRIVATE) && (class_from == current_class))
5700 ADD_TO_LIST_AND_CONTINUE;
5702 #undef ADD_TO_LIST_AND_CONTINUE
5703 return refined_list;
5706 /* Qualification routines */
5709 qualify_ambiguous_name (id)
5712 tree qual, qual_wfl, name, decl, ptr_type, saved_current_class;
5713 int again, super_found = 0, this_found = 0;
5715 /* We first qualify the first element, then derive qualification of
5716 others based on the first one. If the first element is qualified
5717 by a resolution (field or type), this resolution is stored in the
5718 QUAL_RESOLUTION of the qual element being examined. We need to
5719 save the current_class since the use of SUPER might change the
5721 saved_current_class = current_class;
5722 qual = EXPR_WFL_QUALIFICATION (id);
5725 /* Simple qualified expression feature a qual_wfl that is a
5726 WFL. Expression derived from a primary feature more complicated
5727 things like a CALL_EXPR. Expression from primary need to be
5728 worked out to extract the part on which the qualification will
5730 qual_wfl = QUAL_WFL (qual);
5731 switch (TREE_CODE (qual_wfl))
5734 qual_wfl = TREE_OPERAND (qual_wfl, 0);
5735 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
5737 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
5738 qual_wfl = QUAL_WFL (qual);
5741 case JAVA_NEW_CLASS_EXPR:
5744 qual_wfl = TREE_OPERAND (qual_wfl, 0);
5747 name = EXPR_WFL_NODE (qual_wfl);
5748 ptr_type = current_class;
5750 /* If we have a THIS (from a primary), we set the context accordingly */
5751 if (name == this_identifier_node)
5753 qual = TREE_CHAIN (qual);
5754 qual_wfl = QUAL_WFL (qual);
5755 name = EXPR_WFL_NODE (qual_wfl);
5758 /* If we have a SUPER, we set the context accordingly */
5759 if (name == super_identifier_node)
5761 current_class = CLASSTYPE_SUPER (ptr_type);
5762 /* Check that there is such a thing as a super class. If not,
5763 return. The error will be caught later on, during the
5767 current_class = saved_current_class;
5770 qual = TREE_CHAIN (qual);
5771 /* Do one more interation to set things up */
5772 super_found = again = 1;
5776 /* If name appears within the scope of a location variable
5777 declaration or parameter declaration, then it is an expression
5778 name. We don't carry this test out if we're in the context of the
5779 use of SUPER or THIS */
5781 if (!this_found && !super_found && (decl = IDENTIFIER_LOCAL_VALUE (name)))
5783 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
5784 QUAL_RESOLUTION (qual) = decl;
5787 /* If within the class/interface NAME was found to be used there
5788 exists a (possibly inherited) field named NAME, then this is an
5790 else if ((decl = lookup_field_wrapper (ptr_type, name)))
5792 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
5793 QUAL_RESOLUTION (qual) = decl;
5796 /* We reclassify NAME as a type name if:
5797 - NAME is a class/interface declared within the compilation
5798 unit containing NAME,
5799 - NAME is imported via a single-type-import declaration,
5800 - NAME is declared in an another compilation unit of the package
5801 of the compilation unit containing NAME,
5802 - NAME is declared by exactly on type-import-on-demand declaration
5803 of the compilation unit containing NAME. */
5804 else if ((decl = resolve_and_layout (name, NULL_TREE)))
5806 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
5807 QUAL_RESOLUTION (qual) = decl;
5810 /* Method call are expression name */
5811 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR)
5812 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
5814 /* Check here that NAME isn't declared by more than one
5815 type-import-on-demand declaration of the compilation unit
5816 containing NAME. FIXME */
5818 /* Otherwise, NAME is reclassified as a package name */
5820 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
5822 /* Propagate the qualification accross other components of the
5824 for (qual = TREE_CHAIN (qual); qual;
5825 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
5827 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
5828 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
5830 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
5833 /* Store the global qualification for the ambiguous part of ID back
5835 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
5836 RESOLVE_EXPRESSION_NAME_P (id) = 1;
5837 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
5838 RESOLVE_TYPE_NAME_P (id) = 1;
5839 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
5840 RESOLVE_PACKAGE_NAME_P (id) = 1;
5842 /* Restore the current class */
5843 current_class = saved_current_class;
5847 breakdown_qualified (left, right, source)
5848 tree *left, *right, source;
5850 char *p = IDENTIFIER_POINTER (source), *base;
5851 int l = IDENTIFIER_LENGTH (source);
5853 /* Breakdown NAME into REMAINDER . IDENTIFIER */
5856 while (*p != '.' && p != base)
5859 /* We didn't find a '.'. Return an error */
5865 *right = get_identifier (p+1);
5866 *left = get_identifier (IDENTIFIER_POINTER (source));
5872 /* Return 1 if N1 and N2 have identical sub-path. */
5875 identical_subpath_p (n1, n2)
5880 if (!QUALIFIED_P (n1) || !QUALIFIED_P (n2))
5883 breakdown_qualified (&left1, NULL, n1);
5884 breakdown_qualified (&left2, NULL, n2);
5886 return left1 == left2;
5890 not_initialized_as_it_should_p (decl)
5895 if (TREE_CODE (decl) == FIELD_DECL
5896 && METHOD_STATIC (current_function_decl))
5898 return DECL_P (decl) && !INITIALIZED_P (decl);
5903 /* Patch tree nodes in a function body. When a BLOCK is found, push
5904 local variable decls if present. */
5907 java_complete_tree (node)
5910 tree nn, cn, wfl_op1, wfl_op2;
5913 /* CONVERT_EXPR always has its type set, even though it needs to be
5915 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
5918 /* The switch block implements cases processing container nodes
5919 first. Contained nodes are always written back. Leaves come
5920 next and return a value. */
5921 switch (TREE_CODE (node))
5925 /* 1- Block section.
5926 Set the local values on decl names so we can identify them
5927 faster when they're referenced. At that stage, identifiers
5928 are legal so we don't check for declaration errors. */
5929 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
5931 DECL_CONTEXT (cn) = current_function_decl;
5932 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
5933 INITIALIZED_P (cn) = 0;
5935 if (BLOCK_EXPR_BODY (node))
5937 BLOCK_EXPR_BODY (node) = java_complete_tree (BLOCK_EXPR_BODY (node));
5938 if (BLOCK_EXPR_BODY (node) == error_mark_node)
5939 return error_mark_node;
5941 /* Turn local bindings to null */
5942 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
5943 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
5945 TREE_TYPE (node) = void_type_node;
5948 /* 2- They are expressions but ultimately deal with statements */
5949 case LABELED_BLOCK_EXPR:
5950 PUSH_LABELED_BLOCK (node);
5951 if (LABELED_BLOCK_BODY (node))
5952 COMPLETE_CHECK_OP_1 (node);
5953 TREE_TYPE (node) = void_type_node;
5954 POP_LABELED_BLOCK ();
5957 case EXIT_BLOCK_EXPR:
5958 /* We don't complete operand 1, because it's the return value of
5959 the EXIT_BLOCK_EXPR which doesn't exist it Java */
5960 return patch_bc_statement (node);
5964 /* Check whether the loop was enclosed in a labeled
5965 statement. If not, create one, insert the loop in it and
5967 nn = patch_loop_statement (node);
5968 /* Anyways, walk the body of the loop */
5969 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
5970 if (TREE_OPERAND (node, 0) == error_mark_node)
5971 return error_mark_node;
5972 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
5973 /* If we returned something different, that's because we
5974 inserted a label. Pop the label too. */
5976 POP_LABELED_BLOCK ();
5981 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
5982 return patch_exit_expr (node);
5986 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
5987 if (TREE_OPERAND (node, 0) == error_mark_node)
5988 return error_mark_node;
5989 /* then-else branches */
5990 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
5991 if (TREE_OPERAND (node, 1) == error_mark_node)
5992 return error_mark_node;
5993 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
5994 if (TREE_OPERAND (node, 2) == error_mark_node)
5995 return error_mark_node;
5996 return patch_if_else_statement (node);
5999 /* 3- Expression section */
6001 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
6002 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
6003 if (TREE_OPERAND (node, 1) == error_mark_node)
6004 return error_mark_node;
6005 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
6009 return patch_return (node);
6011 case EXPR_WITH_FILE_LOCATION:
6012 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
6013 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
6014 return resolve_expression_name (node);
6017 EXPR_WFL_NODE (node) = java_complete_tree (EXPR_WFL_NODE (node));
6018 TREE_SIDE_EFFECTS (node) = 1;
6019 if (EXPR_WFL_NODE (node) == error_mark_node)
6021 /* Its important for the evaluation of assignment that
6022 this mark on the TREE_TYPE is propagated. */
6023 TREE_TYPE (node) = error_mark_node;
6024 return error_mark_node;
6027 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
6031 case JAVA_NEW_ARRAY_EXPR:
6032 /* Patch all the dimensions */
6034 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
6036 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
6037 tree dim = java_complete_tree (TREE_VALUE (cn));
6038 if (dim == error_mark_node)
6045 TREE_VALUE (cn) = save_expr (dim);
6046 /* Setup the location of the current dimension, for
6047 later error report. */
6049 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
6050 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
6053 /* They complete the array creation expression, if no errors
6055 return (flag ? error_mark_node : patch_newarray (node));
6057 case JAVA_NEW_CLASS_EXPR:
6059 /* Complete function's argument first */
6060 if (complete_function_arguments (node))
6061 return error_mark_node;
6063 return patch_method_invocation_stmt (node, NULL_TREE, NULL_TREE, NULL);
6066 /* Save potential wfls */
6067 wfl_op1 = TREE_OPERAND (node, 0);
6068 wfl_op2 = TREE_OPERAND (node, 1);
6069 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
6070 if (TREE_OPERAND (node, 0) == error_mark_node)
6071 return error_mark_node;
6073 if (COMPOUND_ASSIGN_P (wfl_op2))
6077 java_complete_tree (TREE_OPERAND (wfl_op2, 0));
6079 /* Hand stablize the lhs on both places */
6080 lvalue = stabilize_reference (other);
6081 TREE_OPERAND (node, 0) = lvalue;
6082 TREE_OPERAND (TREE_OPERAND (node, 1), 0) = lvalue;
6085 /* There are cases where the type of RHS is fixed. In those
6086 cases, if the evaluation of the RHS fails, we further the
6087 evaluation of the assignment to detect more errors. */
6088 nn = java_complete_tree (TREE_OPERAND (node, 1));
6089 if (nn == error_mark_node)
6091 /* It's hopeless, but we can further things on to discover
6092 an error during the assignment. In any cases, the
6093 assignment operation fails. */
6094 if (TREE_CODE (TREE_OPERAND (node, 1)) != EXPR_WITH_FILE_LOCATION
6095 && TREE_TYPE (TREE_OPERAND (node, 1)) != error_mark_node)
6096 patch_assignment (node, wfl_op1, wfl_op2);
6098 /* Now, we still mark the lhs as initialized */
6099 if (DECL_P (TREE_OPERAND (node, 0)))
6100 INITIALIZED_P (TREE_OPERAND (node, 0)) = 1;
6102 return error_mark_node;
6104 TREE_OPERAND (node, 1) = nn;
6105 return patch_assignment (node, wfl_op1, wfl_op2);
6116 case TRUNC_MOD_EXPR:
6118 case TRUTH_ANDIF_EXPR:
6119 case TRUTH_ORIF_EXPR:
6126 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
6127 knows how to handle those cases. */
6128 wfl_op1 = TREE_OPERAND (node, 0);
6129 wfl_op2 = TREE_OPERAND (node, 1);
6130 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
6131 if (TREE_OPERAND (node, 0) == error_mark_node)
6132 return error_mark_node;
6133 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
6134 if (TREE_OPERAND (node, 1) == error_mark_node)
6135 return error_mark_node;
6136 return patch_binop (node, wfl_op1, wfl_op2);
6138 case JAVA_UNARY_PLUS_EXPR:
6140 case TRUTH_NOT_EXPR:
6142 case PREDECREMENT_EXPR:
6143 case PREINCREMENT_EXPR:
6144 case POSTDECREMENT_EXPR:
6145 case POSTINCREMENT_EXPR:
6147 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
6148 how to handle those cases. */
6149 wfl_op1 = TREE_OPERAND (node, 0);
6150 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
6151 if (TREE_OPERAND (node, 0) == error_mark_node)
6152 return error_mark_node;
6153 return patch_unaryop (node, wfl_op1);
6156 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
6157 how to handle those cases. */
6158 wfl_op1 = TREE_OPERAND (node, 0);
6159 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
6160 if (TREE_OPERAND (node, 0) == error_mark_node)
6161 return error_mark_node;
6162 /* The same applies to wfl_op2 */
6163 wfl_op2 = TREE_OPERAND (node, 1);
6164 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
6165 if (TREE_OPERAND (node, 1) == error_mark_node)
6166 return error_mark_node;
6167 return patch_array_ref (node, wfl_op1, wfl_op2);
6169 case JAVA_THIS_EXPR:
6170 /* Can't use THIS in a static environment */
6173 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
6174 parse_error_context (wfl_operator, "Keyword `this' used outside "
6176 TREE_TYPE (node) = error_mark_node;
6177 return error_mark_node;
6179 return current_this;
6182 /* Build the internal string representation */
6183 push_obstacks (&permanent_obstack, &permanent_obstack);
6184 node = get_identifier (TREE_STRING_POINTER (node));
6185 location = alloc_name_constant (CONSTANT_String, node);
6186 node = build_ref_from_constant_pool (location);
6187 TREE_TYPE (node) = promote_type (string_type_node);
6191 fatal ("No case for tree code `%s' - java_complete_tree\n",
6192 tree_code_name [TREE_CODE (node)]);
6197 /* Complete function call's argument. Return a non zero value is an
6201 complete_function_arguments (node)
6207 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
6209 tree wfl = TREE_VALUE (cn), parm;
6210 parm = java_complete_tree (wfl);
6211 if (parm == error_mark_node)
6216 if (TREE_CODE (TREE_TYPE (parm)) == RECORD_TYPE)
6217 TREE_VALUE (cn) = convert (promote_type (TREE_TYPE (parm)), parm);
6219 TREE_VALUE (cn) = save_expr (parm);
6220 if (not_initialized_as_it_should_p (parm))
6222 ERROR_VARIABLE_NOT_INITIALIZED (wfl, EXPR_WFL_NODE (wfl));
6223 INITIALIZED_P (parm) = 1;
6229 /* Sometimes (for loops and variable initialized during their
6230 declaration), we want to wrap a statement around a WFL and turn it
6234 build_debugable_stmt (location, stmt)
6238 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
6240 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
6241 EXPR_WFL_LINECOL (stmt) = location;
6243 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
6248 build_expr_block (body, decls)
6251 tree node = make_node (BLOCK);
6252 BLOCK_EXPR_DECLS (node) = decls;
6253 BLOCK_EXPR_BODY (body);
6255 TREE_TYPE (node) = TREE_TYPE (body);
6256 TREE_SIDE_EFFECTS (node) = 1;
6260 /* Create a new function block and link its supercontext to the
6261 previous block. The current function DECL is used as supercontext
6262 when enter_block is called for the first time for a given
6263 function. The current function body (DECL_FUNCTION_BODY) is set to
6264 the newly created block. */
6266 static block_level = 0;
6271 tree b = build_expr_block (NULL_TREE, NULL_TREE);
6272 tree fndecl = current_function_decl;
6274 if (!DECL_FUNCTION_BODY (fndecl))
6276 BLOCK_SUPERCONTEXT (b) = fndecl;
6277 DECL_FUNCTION_BODY (fndecl) = b;
6281 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
6282 DECL_FUNCTION_BODY (fndecl) = b;
6287 /* Exit a block by changing the current function body
6288 (DECL_FUNCTION_BODY) to the current block super context, only if
6289 the block being exited isn't the method's top level one. */
6294 tree b = DECL_FUNCTION_BODY (current_function_decl);
6296 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
6297 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
6302 /* Lookup for NAME in the nested function's blocks, all the way up to
6303 the current toplevel one. It complies with Java's local variable
6307 lookup_name_in_blocks (name)
6310 tree b = DECL_FUNCTION_BODY (current_function_decl);
6312 while (b != current_function_decl)
6316 /* Paranoid sanity check. To be removed */
6317 if (TREE_CODE (b) != BLOCK)
6318 fatal ("non block expr function body - lookup_name_in_blocks");
6320 for (current = BLOCK_EXPR_DECLS (b); current;
6321 current = TREE_CHAIN (current))
6322 if (DECL_NAME (current) == name)
6324 b = BLOCK_SUPERCONTEXT (b);
6330 maybe_absorb_scoping_blocks ()
6332 while (BLOCK_EXPR_ORIGIN (DECL_FUNCTION_BODY (current_function_decl)))
6334 tree b = exit_block ();
6335 java_method_add_stmt (current_function_decl, b);
6336 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
6341 /* This section of the source is reserved to build_* functions that
6342 are building incomplete tree nodes and the patch_* functions that
6343 are completing them. */
6345 /* Build an incomplete CALL_EXPR node. Encapsulate it within a WFL */
6348 build_method_invocation (name, args)
6352 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
6353 TREE_SIDE_EFFECTS (call) = 1;
6354 /* Check on cases where NAME isn't a WFL. FIXME */
6355 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
6359 /* Build an incomplete assignment expression. */
6362 build_assignment (op, op_location, lhs, rhs)
6363 int op, op_location;
6367 /* Build the corresponding binop if we deal with a Compound
6368 Assignment operator. Mark the binop sub-tree as part of a
6369 Compound Assignment expression */
6370 if (op != ASSIGN_TK)
6372 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
6373 COMPOUND_ASSIGN_P (rhs) = 1;
6375 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
6376 TREE_SIDE_EFFECTS (assignment) = 1;
6377 EXPR_WFL_LINECOL (assignment) = op_location;
6381 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
6384 print_int_node (node)
6387 static char buffer [80];
6388 if (TREE_CONSTANT_OVERFLOW (node))
6389 sprintf (buffer, "<overflow>");
6391 if (TREE_INT_CST_HIGH (node) == 0)
6392 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
6393 TREE_INT_CST_LOW (node));
6394 else if (TREE_INT_CST_HIGH (node) == -1
6395 && TREE_INT_CST_LOW (node) != 0)
6398 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
6399 -TREE_INT_CST_LOW (node));
6402 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
6403 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
6408 /* 15.25 Assignment operators. */
6411 patch_assignment (node, wfl_op1, wfl_op2)
6416 tree rhs = TREE_OPERAND (node, 1);
6417 tree lvalue = TREE_OPERAND (node, 0);
6418 tree lhs_type, rhs_type, new_rhs = NULL_TREE;
6420 int error_found = 0;
6421 int lvalue_from_array = 0;
6423 /* Can't assign to a final. */
6424 if (DECL_P (lvalue) && FIELD_FINAL (lvalue))
6427 (wfl_op1, "Can't assign a value to the final variable `%s'",
6428 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl_op1)));
6432 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
6434 /* Lhs can be a named variable */
6435 if (DECL_P (lvalue))
6437 INITIALIZED_P (lvalue) = 1;
6438 lhs_type = TREE_TYPE (lvalue);
6440 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
6441 comment on reason why */
6442 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
6444 lhs_type = TREE_TYPE (lvalue);
6445 lvalue_from_array = 1;
6447 /* Or a field access */
6448 else if (TREE_CODE (lvalue) == COMPONENT_REF)
6449 lhs_type = TREE_TYPE (lvalue);
6450 /* Or a function return slot */
6451 else if (TREE_CODE (lvalue) == RESULT_DECL)
6452 lhs_type = TREE_TYPE (lvalue);
6453 /* Otherwise, this is an error */
6456 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
6460 rhs_type = TREE_TYPE (rhs);
6462 /* 5.2 Begin Assignment conversion */
6464 /* 5.1.1 Try Identity Conversion */
6465 if (lhs_type == rhs_type)
6468 /* 5.1.2 Try Widening Primitive Conversion */
6469 all_primitive = JPRIMITIVE_TYPE_P (lhs_type) && JPRIMITIVE_TYPE_P (rhs_type);
6470 if (all_primitive && JINTEGRAL_TYPE_P (rhs_type)
6471 && ((TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type))
6472 || (JFLOAT_TYPE_P (lhs_type) &&
6473 TYPE_PRECISION (rhs_type) == TYPE_PRECISION (lhs_type))))
6474 new_rhs = convert (lhs_type, rhs);
6475 else if (all_primitive && JFLOAT_TYPE_P (rhs_type)
6476 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
6477 new_rhs = convert (lhs_type, rhs);
6479 /* Try a narrowing primitive conversion:
6480 - expression is a constant expression of type int AND
6481 - variable is byte, short or char AND
6482 - The value of the expression is representable in the type of the
6484 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
6485 && (lhs_type == byte_type_node || lhs_type == char_type_node
6486 || lhs_type == short_type_node))
6488 if (int_fits_type_p (rhs, lhs_type))
6489 new_rhs = convert (lhs_type, rhs);
6491 parse_warning_context
6492 (wfl_op1, "Constant expression `%s' to wide for narrowing "
6493 "primitive conversion to `%s'",
6494 print_int_node (rhs), lang_printable_name (lhs_type));
6495 /* Reported a warning that will turn into an error further
6496 down, so we don't return */
6499 /* 5.2 Try a reference conversion */
6500 else if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
6502 /* `null' may be assigned to any reference type */
6503 if (rhs == null_pointer_node)
6504 new_rhs = null_pointer_node;
6505 /* Try the reference assignment conversion */
6506 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
6509 lhs_type = promote_type (rhs_type);
6512 /* 15.25.2 If we have a compound assignment, convert RHS into the
6514 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
6515 new_rhs = convert (lhs_type, rhs);
6517 /* Explicit cast required. This is an error */
6520 char *t1 = strdup ((char *)lang_printable_name (TREE_TYPE (rhs)));
6521 char *t2 = strdup ((char *)lang_printable_name (lhs_type));
6523 char operation [32]; /* Max size known */
6525 /* If the assignment is part of a declaration, we use the WFL of
6526 the declared variable to point out the error and call it a
6527 declaration problem. If the assignment is a genuine =
6528 operator, we call is a operator `=' problem, otherwise we
6529 call it an assignment problem. In both of these last cases,
6530 we use the WFL of the operator to indicate the error. */
6532 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
6535 strcpy (operation, "declaration");
6540 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
6541 strcpy (operation, "assignment");
6542 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
6543 strcpy (operation, "`return'");
6545 strcpy (operation, "`='");
6549 (wfl, (!can_cast_to_p (rhs_type, lhs_type) ?
6550 "Incompatible type for %s. Can't convert `%s' to `%s'" :
6551 "Incompatible type for %s. Explicit cast "
6552 "needed to convert `%s' to `%s'"), operation, t1, t2);
6553 free (t1); free (t2);
6557 /* Before reporting type incompatibility errors, check that the rhs
6558 is initialized, if a variable */
6559 if (not_initialized_as_it_should_p (rhs))
6561 ERROR_VARIABLE_NOT_INITIALIZED (wfl_op2, DECL_NAME (rhs));
6562 INITIALIZED_P (rhs) = 1;
6566 return error_mark_node;
6568 /* If we built a compound expression as the result of a reference
6569 assignment into an array element, return it here. */
6570 if (TREE_CODE (node) == COMPOUND_EXPR)
6573 TREE_OPERAND (node, 0) = lvalue;
6574 TREE_OPERAND (node, 1) = new_rhs;
6575 TREE_TYPE (node) = lhs_type;
6579 /* Check that SOURCE can be converted into DEST, at least with a
6580 cast. If the convertion can't occur at all, return 0 otherwise
6581 1. This function is used to produce accurate error messages on the
6582 reasons why an assignment failed. */
6585 can_cast_to_p (source, dest)
6589 if (TREE_CODE (source) == POINTER_TYPE)
6590 source = TREE_TYPE (source);
6591 if (TREE_CODE (dest) == POINTER_TYPE)
6592 dest = TREE_TYPE (dest);
6594 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
6595 return valid_ref_assignconv_cast_p (source, dest, 1);
6597 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
6603 /* Check that something of SOURCE type can be assigned or cast to
6604 something of DEST type at runtime. Return 1 if the operation is
6605 valid, 0 otherwise. If CAST is set to 1, we're treating the case
6606 were SOURCE is cast into DEST, which borrows a lot of the
6607 assignment check. */
6610 valid_ref_assignconv_cast_p (source, dest, cast)
6615 if (TREE_CODE (source) == POINTER_TYPE)
6616 source = TREE_TYPE (source);
6617 if (TREE_CODE (dest) == POINTER_TYPE)
6618 dest = TREE_TYPE (dest);
6619 /* Case where SOURCE is a class type */
6620 if (TYPE_CLASS_P (source))
6622 if (TYPE_CLASS_P (dest))
6623 return source == dest || inherits_from_p (source, dest)
6624 || cast && inherits_from_p (dest, source);
6625 if (TYPE_INTERFACE_P (dest))
6627 /* If doing a cast and SOURCE is final, the operation is
6628 always correct a compile time (because even if SOURCE
6629 does not implement DEST, a subclass of SOURCE might). */
6630 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
6632 /* Otherwise, SOURCE must implement DEST */
6633 return interface_of_p (dest, source);
6635 /* DEST is an array, cast permited if SOURCE is of Object type */
6636 return (cast && source == object_type_node ? 1 : 0);
6638 if (TYPE_INTERFACE_P (source))
6640 if (TYPE_CLASS_P (dest))
6642 /* If not casting, DEST must be the Object type */
6644 return dest == object_type_node;
6645 /* We're doing a cast. The cast is always valid is class
6646 DEST is not final, otherwise, DEST must implement SOURCE */
6647 else if (!CLASS_FINAL (TYPE_NAME (source)))
6650 return interface_of_p (source, dest);
6652 if (TYPE_INTERFACE_P (dest))
6654 /* If doing a cast, then if SOURCE and DEST contain method
6655 with the same signature but different return type, then
6656 this is a (compile time) error */
6659 tree method_source, method_dest;
6661 tree source_sig, dest_sig;
6663 for (method_source = TYPE_METHODS (source); method_source;
6664 method_source = TREE_CHAIN (method_source))
6667 build_java_argument_signature (TREE_TYPE (method_source));
6668 source_type = TREE_TYPE (TREE_TYPE (method_source));
6669 source_name = DECL_NAME (method_source);
6670 for (method_dest = TYPE_METHODS (dest);
6671 method_dest; method_dest = TREE_CHAIN (method_dest))
6673 build_java_argument_signature (TREE_TYPE (method_dest))
6674 && source_name == DECL_NAME (method_dest)
6675 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
6681 return source == dest || interface_of_p (dest, source);
6686 if (TYPE_ARRAY_P (source))
6688 if (TYPE_CLASS_P (dest))
6689 return dest == object_type_node;
6690 if (TYPE_INTERFACE_P (dest))
6691 return 0; /* Install test on Clonable. FIXME */
6694 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
6695 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
6697 if (source_element_type == dest_element_type)
6699 return valid_ref_assignconv_cast_p (source_element_type,
6700 dest_element_type, cast);
6707 /* Build an incomplete binop expression. */
6710 build_binop (op, op_location, op1, op2)
6717 /* URSHIFT_EXPR is not part of what GCC understands, we can't directly build
6720 build ((op == URSHIFT_EXPR ? RSHIFT_EXPR : op), NULL_TREE, op1, op2);
6721 if (op == URSHIFT_EXPR)
6722 TREE_SET_CODE (binop, op);
6724 TREE_SIDE_EFFECTS (binop) = 1;
6725 /* Store the location of the operator, for better error report. The
6726 string of the operator will be rebuild based on the OP value. */
6727 EXPR_WFL_LINECOL (binop) = op_location;
6731 /* Build the string of the operator retained by NODE. If NODE is part
6732 of a compound expression, add an '=' at the end of the string. This
6733 function is called when an error needs to be reported on an
6734 operator. The string is returned as a pointer to a static character
6738 operator_string (node)
6741 #define BUILD_OPERATOR_STRING(S) \
6743 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
6747 static char buffer [10];
6748 switch (TREE_CODE (node))
6750 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
6751 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
6752 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
6753 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
6754 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
6755 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
6756 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
6757 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
6758 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
6759 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
6760 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
6761 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
6762 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
6763 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
6764 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
6765 case GT_EXPR: BUILD_OPERATOR_STRING (">");
6766 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
6767 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
6768 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
6769 case JAVA_UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
6770 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
6771 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
6772 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
6773 case PREINCREMENT_EXPR: /* Fall through */
6774 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
6775 case PREDECREMENT_EXPR: /* Fall through */
6776 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
6778 fatal ("unregistered operator %s - operator_string",
6779 tree_code_name [TREE_CODE (node)]);
6782 #undef BUILD_OPERATOR_STRING
6785 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
6786 errors but we modify NODE so that it contains the type computed
6787 according to the expression, when it's fixed. Otherwise, we write
6788 error_mark_node as the type. It allows us to further the analysis
6789 of remaining nodes and detects more errors in certain cases. */
6792 patch_binop (node, wfl_op1, wfl_op2)
6797 tree op1 = TREE_OPERAND (node, 0);
6798 tree op2 = TREE_OPERAND (node, 1);
6799 tree op1_type = TREE_TYPE (op1);
6800 tree op2_type = TREE_TYPE (op2);
6802 int code = TREE_CODE (node);
6803 /* If 1, tell the routine that we have to return error_mark_node
6804 after checking for the initialization of the RHS */
6805 int error_found = 0;
6807 /* Figure what is going to be checked first for initialization prior
6808 its use. If NODE is part of a compound assignment, we check the
6809 second operand first, otherwise the first one first. We also
6810 initialize the matching WFL for the error report. `cfi' stands
6811 for Check For Initialization */
6812 tree cfi = (COMPOUND_ASSIGN_P (node) ? op2 : op1);
6813 tree cfi_wfl = (COMPOUND_ASSIGN_P (node) ? wfl_op2 : wfl_op1);
6815 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
6817 /* Check initialization of LHS first. We then silence further error
6818 message if the variable wasn't initialized */
6819 if (not_initialized_as_it_should_p (cfi))
6821 ERROR_VARIABLE_NOT_INITIALIZED (cfi_wfl, DECL_NAME (cfi));
6822 INITIALIZED_P (op1) = 1;
6827 /* 15.16 Multiplicative operators */
6828 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
6829 case RDIV_EXPR: /* 15.16.2 Division Operator / */
6830 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
6831 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
6833 if (!JPRIMITIVE_TYPE_P (op1_type))
6834 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
6835 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
6836 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
6837 TREE_TYPE (node) = error_mark_node;
6841 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
6842 /* Change the division operator if necessary */
6843 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
6844 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
6845 /* This one is more complicated. FLOATs are processed by a function
6846 call to soft_fmod. */
6847 if (code == TRUNC_MOD_EXPR)
6848 return build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
6851 /* 15.17 Additive Operators */
6852 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
6853 if (JSTRING_TYPE_P (op1_type) || JSTRING_TYPE_P (op2_type))
6854 fatal ("operator `+' non implemented on String - patch_binop");
6855 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
6857 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
6859 if (!JPRIMITIVE_TYPE_P (op1_type))
6860 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
6861 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
6862 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
6863 TREE_TYPE (node) = error_mark_node;
6867 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
6870 /* 15.18 Shift Operators */
6874 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
6876 if (!JINTEGRAL_TYPE_P (op1_type))
6877 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
6880 (wfl_operator, (JPRIMITIVE_TYPE_P (op2_type) ?
6881 "Incompatible type for `%s'. Explicit cast needed to convert "
6882 "shift distance from `%s' to integral" :
6883 "Incompatible type for `%s'. Can't convert shift distance from "
6884 "`%s' to integral"),
6885 operator_string (node), lang_printable_name (op2_type));
6886 TREE_TYPE (node) = error_mark_node;
6891 /* Unary numeric promotion (5.6.1) is performed on each operand
6893 op1 = convert (promote_type (op1_type), op1);
6894 op2 = convert (promote_type (op2_type), op2);
6896 /* The type of the shift expression is the type of the promoted
6897 type of the left-hand operand */
6898 prom_type = TREE_TYPE (op1);
6900 /* Shift int only up to 0x1f and long up to 0x3f */
6901 if (prom_type == int_type_node)
6902 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
6903 build_int_2 (0x1f, 0)));
6905 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
6906 build_int_2 (0x3f, 0)));
6908 /* The >>> operator is a >> operating on unsigned quantities */
6909 if (code == URSHIFT_EXPR)
6911 op1 = convert (unsigned_type (prom_type), op1);
6912 TREE_SET_CODE (node, RSHIFT_EXPR);
6917 /* 15.21 Bitwise and Logical Operators */
6921 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
6922 /* Binary numeric promotion is performed on both operand and the
6923 expression retain that type */
6924 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
6926 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
6927 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
6928 /* The type of the bitwise operator expression is BOOLEAN */
6929 prom_type = boolean_type_node;
6932 if (!JINTEGRAL_TYPE_P (op1_type))
6933 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
6934 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
6935 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
6936 TREE_TYPE (node) = error_mark_node;
6938 /* Insert a break here if adding thing before the switch's
6939 break for this case */
6943 /* 15.22 Conditional-And Operator */
6944 case TRUTH_ANDIF_EXPR:
6945 /* 15.23 Conditional-Or Operator */
6946 case TRUTH_ORIF_EXPR:
6947 /* Operands must be of BOOLEAN type */
6948 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
6949 TREE_CODE (op2_type) != BOOLEAN_TYPE)
6951 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
6952 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
6953 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
6954 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
6955 TREE_TYPE (node) = boolean_type_node;
6959 /* The type of the conditional operators is BOOLEAN */
6960 prom_type = boolean_type_node;
6963 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
6968 /* The type of each of the operands must be a primitive numeric
6970 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
6972 if (!JNUMERIC_TYPE_P (op1_type))
6973 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
6974 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
6975 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
6976 TREE_TYPE (node) = boolean_type_node;
6980 /* Binary numeric promotion is performed on the operands */
6981 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
6982 /* The type of the relation expression is always BOOLEAN */
6983 prom_type = boolean_type_node;
6986 /* 15.20 Equality Operator */
6989 /* 15.20.1 Numerical Equality Operators == and != */
6990 /* Binary numeric promotion is performed on the operands */
6991 if (JPRIMITIVE_TYPE_P (op1_type) && JPRIMITIVE_TYPE_P (op2_type))
6992 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
6994 /* 15.20.2 Boolean Equality Operators == and != */
6995 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
6996 TREE_CODE (op2_type) == BOOLEAN_TYPE)
6997 ; /* Nothing to do here */
6999 /* 15.20.3 Reference Equality Operators == and != */
7000 /* Types have to be either references or the null type */
7001 else if ((op1 == null_pointer_node || op2 == null_pointer_node
7002 || JREFERENCE_TYPE_P (op1_type)
7003 || JREFERENCE_TYPE_P (op2_type))
7004 && ((op1_type == op2_type)
7005 /* The should use a can_cast_to_p() */
7007 ; /* Nothing to do here */
7009 /* Else we have an error figure what can't be converted into
7010 what and report the error */
7014 t1 = strdup ((char *)lang_printable_name (op1_type));
7016 (wfl_operator, "Incompatible type for `%s'. Can't convert `%s' "
7017 "to `%s'", operator_string (node), t1,
7018 lang_printable_name (op2_type));
7020 TREE_TYPE (node) = boolean_type_node;
7024 prom_type = boolean_type_node;
7028 /* Then check the initialization of the RHS. We don't do that if
7029 we're dealing with a node that is part of a compound
7030 assignment. We then silence further error message if the variable
7031 wasn't initialized */
7032 if (not_initialized_as_it_should_p (op2) && !COMPOUND_ASSIGN_P (node))
7034 ERROR_VARIABLE_NOT_INITIALIZED (wfl_op2, DECL_NAME (op2));
7035 INITIALIZED_P (op2) = 1;
7039 return error_mark_node;
7041 TREE_OPERAND (node, 0) = op1;
7042 TREE_OPERAND (node, 1) = op2;
7043 TREE_TYPE (node) = prom_type;
7047 /* Build an incomplete unary operator expression. Unary `+' node is
7048 build as a CONV_EXPR, even though its tree code is overridden by a
7049 JAVA_UNARY_PLUS_EXPR that isn't a tree code, to differentiate it during
7053 build_unaryop (op_token, op_location, op1)
7054 int op_token, op_location;
7061 case PLUS_TK: op = CONVERT_EXPR; break;
7062 case MINUS_TK: op = NEGATE_EXPR; break;
7063 case NEG_TK: op = TRUTH_NOT_EXPR; break;
7064 case NOT_TK: op = BIT_NOT_EXPR; break;
7065 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
7069 unaryop = build1 (op, NULL_TREE, op1);
7070 if (op_token == PLUS_TK)
7071 TREE_SET_CODE (unaryop, JAVA_UNARY_PLUS_EXPR);
7073 TREE_SIDE_EFFECTS (unaryop) = 1;
7074 /* Store the location of the operator, for better error report. The
7075 string of the operator will be rebuild based on the OP value. */
7076 EXPR_WFL_LINECOL (unaryop) = op_location;
7080 /* Special case for the ++/-- operators, since they require an extra
7081 argument to build, which is set to NULL and patched
7082 later. IS_POST_P is 1 if the operator, 0 otherwise. */
7085 build_incdec (op_token, op_location, op1, is_post_p)
7086 int op_token, op_location;
7090 static enum tree_code lookup [2][2] =
7092 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
7093 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
7095 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
7096 NULL_TREE, op1, NULL_TREE);
7097 TREE_SIDE_EFFECTS (node) = 1;
7098 /* Store the location of the operator, for better error report. The
7099 string of the operator will be rebuild based on the OP value. */
7100 EXPR_WFL_LINECOL (node) = op_location;
7104 /* Build an incomplete cast operator, based on the use of the
7105 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
7106 set. java_complete_tree is trained to walk a CONVERT_EXPR even
7107 though its type is already set. */
7110 build_cast (location, type, exp)
7114 tree node = build1 (CONVERT_EXPR, type, exp);
7115 EXPR_WFL_LINECOL (node) = location;
7119 /* 15.14 Unary operators. We return error_mark_node in case of error,
7120 but preserve the type of NODE if the type is fixed. */
7123 patch_unaryop (node, wfl_op)
7127 tree op = TREE_OPERAND (node, 0);
7128 tree op_type = TREE_TYPE (op);
7129 tree prom_type, value;
7130 int code = TREE_CODE (node);
7131 int error_found = 0;
7133 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7137 /* 15.13.2 Postfix Increment Operator ++ */
7138 case POSTINCREMENT_EXPR:
7139 /* 15.13.3 Postfix Increment Operator -- */
7140 case POSTDECREMENT_EXPR:
7141 /* 15.14.1 Prefix Increment Operator ++ */
7142 case PREINCREMENT_EXPR:
7143 /* 15.14.2 Prefix Decrement Operator -- */
7144 case PREDECREMENT_EXPR:
7147 parse_error_context (wfl_operator, "Invalid argument to `%s'",
7148 operator_string (node));
7149 TREE_TYPE (node) = error_mark_node;
7152 else if (FIELD_FINAL (op))
7155 (wfl_op, "Can't assign a value to the final variable `%s'",
7156 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl_op)));
7157 TREE_TYPE (node) = error_mark_node;
7160 /* From now on, we know that op if a variable and that it has a
7161 valid wfl. We use wfl_op to locate errors related to the
7163 else if (!JNUMERIC_TYPE_P (op_type))
7166 (wfl_op, "Invalid argument type `%s' to `%s'",
7167 lang_printable_name (op_type), operator_string (node));
7168 TREE_TYPE (node) = error_mark_node;
7173 /* Before the addition, binary numeric promotion if performed on
7175 value = integer_one_node;
7176 prom_type = binary_numeric_promotion (op_type, TREE_TYPE (value),
7178 /* And write the promoted increment back */
7179 TREE_OPERAND (node, 1) = value;
7183 /* 15.14.3 Unary Plus Operator + */
7184 case JAVA_UNARY_PLUS_EXPR:
7185 /* 15.14.4 Unary Minus Operator - */
7187 if (!JNUMERIC_TYPE_P (op_type))
7189 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
7190 TREE_TYPE (node) = error_mark_node;
7193 /* Unary numeric promotion is performed on operand */
7196 prom_type = promote_type (op_type);
7197 op = convert (prom_type, op);
7198 if (code == JAVA_UNARY_PLUS_EXPR)
7203 /* 15.14.5 Bitwise Complement Operator ~ */
7205 if (!JINTEGRAL_TYPE_P (op_type))
7207 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
7208 TREE_TYPE (node) = error_mark_node;
7213 prom_type = promote_type (op_type);
7214 op = convert (prom_type, op);
7218 /* 15.14.6 Logical Complement Operator ! */
7219 case TRUTH_NOT_EXPR:
7220 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
7222 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
7223 TREE_TYPE (node) = boolean_type_node;
7227 prom_type = boolean_type_node;
7230 /* 15.15 Cast Expression */
7232 value = patch_cast (node, wfl_op, wfl_operator);
7233 if (value == error_mark_node)
7235 TREE_TYPE (node) = error_mark_node;
7243 /* Check variable initialization */
7244 if (not_initialized_as_it_should_p (op))
7246 ERROR_VARIABLE_NOT_INITIALIZED (wfl_op, DECL_NAME (op));
7247 INITIALIZED_P (op) = 1;
7251 return error_mark_node;
7252 /* In the case of JAVA_UNARY_PLUS_EXPR, we replaced NODE by a new one */
7253 else if (code != JAVA_UNARY_PLUS_EXPR && code != CONVERT_EXPR)
7255 TREE_OPERAND (node, 0) = op;
7256 TREE_TYPE (node) = prom_type;
7261 /* Generic type resolution that sometimes takes place during node
7262 patching. Returned the resolved type or generate an error
7263 message. Return the resolved type or NULL_TREE. */
7266 resolve_type_during_patch (type)
7269 if (unresolved_type_p (type, NULL))
7271 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
7274 parse_error_context (type,
7275 "Class `%s' not found in type declaration",
7276 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
7280 return TREE_TYPE (type_decl);
7284 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
7285 found. Otherwise NODE or something meant to replace it is returned. */
7288 patch_cast (node, wfl_op, wfl_operator)
7293 tree op = TREE_OPERAND (node, 0);
7294 tree op_type = TREE_TYPE (op);
7295 tree cast_type = TREE_TYPE (node);
7298 /* First resolve OP_TYPE if unresolved */
7299 if (!(cast_type = resolve_type_during_patch (cast_type)))
7300 return error_mark_node;
7302 /* Check on cast that are proven correct at compile time */
7303 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
7305 static tree convert_narrow ();
7307 if (cast_type == op_type)
7310 /* Try widening/narowwing convertion. Potentially, things need
7311 to be worked out in gcc so we implement the extreme cases
7312 correctly. fold_convert() needs to be fixed. */
7313 return convert (cast_type, op);
7316 /* The remaining legal casts involve conversion between reference
7317 types. Check for their compile time correctness. */
7318 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
7319 && valid_ref_assignconv_cast_p (op_type, cast_type, 1))
7321 TREE_TYPE (node) = promote_type (cast_type);
7322 /* Now, the case can be determined correct at compile time if
7323 OP_TYPE can be converted into CAST_TYPE by assignment
7326 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
7329 /* The cast requires a run-time check */
7330 return build (CALL_EXPR, promote_type (cast_type),
7331 build_address_of (soft_checkcast_node),
7332 tree_cons (NULL_TREE, build_class_ref (cast_type),
7333 build_tree_list (NULL_TREE, op)),
7337 /* Any other casts are proven incorrect at compile time */
7338 t1 = strdup ((char *)lang_printable_name (op_type));
7339 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
7340 t1, lang_printable_name (cast_type));
7342 return error_mark_node;
7345 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
7346 a list of indices. */
7348 build_array_ref (location, array, index)
7352 tree node = build (ARRAY_REF, NULL_TREE, array, index);
7353 EXPR_WFL_LINECOL (node) = location;
7357 /* 15.12 Array Access Expression */
7360 patch_array_ref (node, wfl_array, wfl_index)
7361 tree node, wfl_array, wfl_index;
7363 tree array = TREE_OPERAND (node, 0);
7364 tree array_type = TREE_TYPE (array);
7365 tree index = TREE_OPERAND (node, 1);
7366 tree index_type = TREE_TYPE (index);
7367 tree promoted_index_type;
7368 int error_found = 0;
7370 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7372 if (not_initialized_as_it_should_p (array))
7374 ERROR_VARIABLE_NOT_INITIALIZED (wfl_array, DECL_NAME (array));
7375 INITIALIZED_P (array) = 1;
7377 if (! flag_emit_class_files)
7378 array = save_expr (array);
7380 if (TREE_CODE (array_type) == POINTER_TYPE)
7381 array_type = TREE_TYPE (array_type);
7383 /* The array reference must be an array */
7384 if (!TYPE_ARRAY_P (array_type))
7387 (wfl_operator, "`[]' can only be applied to arrays. It can't be "
7388 "applied to `%s'", lang_printable_name (array_type));
7389 TREE_TYPE (node) = error_mark_node;
7393 /* The array index underdoes unary numeric promotion. The promoted
7395 promoted_index_type = promote_type (index_type);
7396 if (promoted_index_type != int_type_node)
7398 int could_cast = can_cast_to_p (index_type, int_type_node);
7401 (could_cast ? "Incompatible type for `[]'. Explicit cast needed to "
7402 "convert `%s' to `int'" : "Incompatible type for `[]'. "
7403 "Can't convert `%s' to `int'"),
7404 lang_printable_name (index_type));
7405 TREE_TYPE (node) = error_mark_node;
7409 /* Now if the index is a var/parm decl, check on its initialization */
7410 if (not_initialized_as_it_should_p (index))
7412 ERROR_VARIABLE_NOT_INITIALIZED (wfl_index, DECL_NAME (index));
7413 INITIALIZED_P (index) = 1;
7417 return error_mark_node;
7418 index = convert (promoted_index_type, index);
7420 if (TREE_CODE (array_type) == RECORD_TYPE)
7421 array_type = promote_type (TYPE_ARRAY_ELEMENT (array_type));
7422 if (flag_emit_class_files)
7424 TREE_OPERAND (node, 0)= array;
7425 TREE_OPERAND (node, 1)= index;
7428 node = build_java_arrayaccess (array, array_type, index);
7429 TREE_TYPE (node) = array_type;
7433 /* 15.9 Array Creation Expressions */
7436 build_newarray_node (type, dims, extra_dims)
7442 build (CALL_EXPR, NULL_TREE, type, nreverse (dims),
7443 build_int_2 (extra_dims, 0));
7444 TREE_SET_CODE (node, JAVA_NEW_ARRAY_EXPR);
7449 patch_newarray (node)
7452 tree type = TREE_OPERAND (node, 0);
7453 tree dims = TREE_OPERAND (node, 1);
7454 tree cdim, array_type;
7455 int error_found = 0;
7457 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
7460 /* Dimension types are verified. It's better for the types to be
7461 verified in order. */
7462 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
7465 tree dim = TREE_VALUE (cdim);
7467 /* Dim might have been saved during its evaluation */
7468 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
7470 /* The type of each specified dimension must be an integral type. */
7471 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
7474 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
7475 promoted type must be int. */
7478 dim = convert (promote_type (TREE_TYPE (dim)), dim);
7479 if (TREE_TYPE (dim) != int_type_node)
7483 /* Report errors on types here */
7487 (TREE_PURPOSE (cdim),
7488 "Incompatible type for dimension in array creation expression. "
7489 "%s convert `%s' to `int'",
7490 (can_cast_to_p (TREE_TYPE (dim), int_type_node) ?
7491 "Explicit cast needed to" : "Can't"),
7492 lang_printable_name (TREE_TYPE (dim)));
7496 /* Check for uninitialized variables */
7497 if (not_initialized_as_it_should_p (dim))
7499 ERROR_VARIABLE_NOT_INITIALIZED (TREE_PURPOSE (cdim),
7501 INITIALIZED_P (dim) = 1;
7505 TREE_PURPOSE (cdim) = NULL_TREE;
7508 /* Resolve array base type if unresolved */
7509 if (!(type = resolve_type_during_patch (type)))
7514 /* We don't want further evaluation of this bogus array creation
7516 TREE_TYPE (node) = error_mark_node;
7517 return error_mark_node;
7520 /* The node is transformed into a function call. Things are done
7521 differently according to the number of dimensions. If the number
7522 of dimension is equal to 1, then the nature of the base type
7523 (primitive or not) matters. */
7524 total_dims = xdims + ndims;
7525 if (total_dims == 1)
7527 if (JPRIMITIVE_TYPE_P (type))
7530 if (type == boolean_type_node)
7532 else if (type == char_type_node)
7534 else if (type == float_type_node)
7536 else if (type == double_type_node)
7538 else if (type == byte_type_node)
7540 else if (type == short_type_node)
7542 else if (type == int_type_node)
7544 else if (type == long_type_node)
7547 fatal ("Can't compute type code - patch_newarray");
7548 return build_newarray (type_code, TREE_VALUE (dims));
7551 return build_anewarray (type, TREE_VALUE (dims));
7554 /* Add extra dimensions as unknown dimensions */
7557 chainon (dims, build_tree_list (NULL_TREE, integer_negative_one_node));
7558 dims = chainon (dims, build_tree_list (NULL_TREE, integer_zero_node));
7560 /* Can't reuse what's already written in expr.c because it uses the
7561 JVM stack representation. Provide a build_multianewarray. FIXME */
7563 for (cdim = TREE_CHAIN (dims); cdim; cdim = TREE_CHAIN (cdim))
7564 array_type = build_java_array_type (promote_type (array_type),
7565 TREE_CODE (cdim) == INTEGER_CST ?
7566 TREE_INT_CST_LOW (cdim) : -1);
7567 return build (CALL_EXPR,
7568 promote_type (array_type),
7569 build_address_of (soft_multianewarray_node),
7570 tree_cons (NULL_TREE, build_class_ref (array_type),
7571 tree_cons (NULL_TREE,
7572 build_int_2 (total_dims, 0), dims )),
7577 build_this (location)
7580 tree node = build_wfl_node (this_identifier_node, input_filename, 0, 0);
7581 TREE_SET_CODE (node, JAVA_THIS_EXPR);
7582 EXPR_WFL_LINECOL (node) = location;
7586 /* 14.15 The return statement. It builds a modify expression that
7587 assigns the returned value to the RESULT_DECL that hold the value
7591 build_return (location, op)
7595 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
7596 EXPR_WFL_LINECOL (node) = location;
7604 tree return_exp = TREE_OPERAND (node, 0);
7605 tree meth = current_function_decl;
7606 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
7608 int error_found = 0;
7610 TREE_TYPE (node) = error_mark_node;
7611 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7613 /* It's invalid to have a return value within a function that is
7614 declared with the keyword void or that is a constructor */
7615 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
7618 /* It's invalid to have a no return value within a function that
7619 isn't declared with the keyword `void' */
7620 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
7625 char *t = strdup ((char *)lang_printable_name (mtype));
7626 parse_error_context (wfl_operator, "`return' with%s value from `%s %s'",
7627 (error_found == 1 ? "" : "out"), t,
7628 lang_printable_name (meth));
7630 return error_mark_node;
7633 /* If we have a return_exp, build a modify expression and expand it */
7636 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), return_exp);
7637 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
7638 modify = java_complete_tree (modify);
7639 if (modify != error_mark_node)
7641 TREE_SIDE_EFFECTS (modify) = 1;
7642 TREE_OPERAND (node, 0) = modify;
7645 return error_mark_node;
7647 TREE_TYPE (node) = void_type_node;
7648 TREE_SIDE_EFFECTS (node) = 1;
7652 /* 14.8 The if Statement */
7655 build_if_else_statement (location, expression, if_body, else_body)
7657 tree expression, if_body, else_body;
7660 /* FIXME: make else body be a void node, where this function is
7663 else_body = build (COMPOUND_EXPR, void_type_node, NULL_TREE, NULL_TREE);
7664 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
7665 EXPR_WFL_LINECOL (node) = location;
7670 patch_if_else_statement (node)
7673 tree expression = TREE_OPERAND (node, 0);
7675 TREE_TYPE (node) = error_mark_node;
7676 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7678 /* The type of expression must be boolean */
7679 if (TREE_TYPE (expression) != boolean_type_node)
7683 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
7684 lang_printable_name (TREE_TYPE (expression)));
7685 return error_mark_node;
7688 TREE_TYPE (node) = void_type_node;
7689 TREE_SIDE_EFFECTS (node) = 1;
7693 /* 14.6 Labeled Statements */
7695 /* Action taken when a lableled statement is parsed. a new
7696 LABELED_BLOCK_EXPR is created. No statement is attached to the
7700 build_labeled_block (location, label, wfl)
7704 tree label_name = merge_qualified_name (label_id, label);
7705 tree label_decl, node;
7707 /* Issue a warning if we try to reuse a label that was previously
7709 if (IDENTIFIER_LOCAL_VALUE (label_name))
7711 EXPR_WFL_LINECOL (wfl_operator) = location;
7712 parse_warning_context (wfl_operator, "Declaration of `%s' shadows "
7713 "a previous declaration",
7714 IDENTIFIER_POINTER (label));
7715 EXPR_WFL_LINECOL (wfl_operator) =
7716 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
7717 parse_warning_context (wfl_operator, "This is the location of the "
7718 "previous declaration of label `%s'",
7719 IDENTIFIER_POINTER (label));
7720 java_warning_count--;
7723 label_decl = create_label_decl (label_name);
7724 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
7725 EXPR_WFL_LINECOL (node) = location;
7726 TREE_SIDE_EFFECTS (node) = 1;
7730 /* Generate a label crafting a unique name for it. This is used to
7731 implicitely label loops that aren't the body part of labeled
7735 generate_labeled_block ()
7737 static int l_number = 0;
7741 sprintf (buf, "$a%d", l_number++);
7742 return build_labeled_block (0, get_identifier (buf), NULL_TREE);
7745 /* A labeled statement LBE is attached a statement. If the statement
7746 happens to be a loop, a link from the loop back to the label is
7750 complete_labeled_statement (lbe, statement)
7751 tree lbe; /* Labeled block expr */
7754 /* In anyways, tie the loop to its statement */
7755 LABELED_BLOCK_BODY (lbe) = statement;
7757 /* Ok, if statement is a for loop, we have to attach the labeled
7758 statement to the block the for loop belongs to and return the
7760 if (TREE_CODE (statement) == LOOP_EXPR && IS_FOR_LOOP_P (statement))
7762 java_method_add_stmt (current_function_decl, lbe);
7763 return exit_block ();
7769 /* 14.10, 14.11, 14.12 Loop Statements */
7771 /* Create an empty LOOP_EXPR and make it the last in the nested loop
7775 build_new_loop (loop_body)
7778 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
7779 TREE_SIDE_EFFECTS (loop) = 1;
7784 /* Create a loop body according to the following structure:
7786 COMPOUND_EXPR (loop main body)
7787 EXIT_EXPR (this order is for while/for loops.
7788 LABELED_BLOCK_EXPR the order is reversed for do loops)
7789 LABEL_DECL (continue occurding here branche at the
7790 BODY end of this labeled block)
7793 REVERSED, if non zero, tells that the loop condition expr comes
7794 after the body, like in the do-while loop. */
7797 build_loop_body (location, condition, reversed)
7802 tree first, second, label, body;
7804 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
7805 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
7806 condition = build_debugable_stmt (location, condition);
7807 TREE_SIDE_EFFECTS (condition) = 1;
7809 body = generate_labeled_block ();
7810 first = (reversed ? body : condition);
7811 second = (reversed ? condition : body);
7813 build (COMPOUND_EXPR, NULL_TREE,
7814 build (COMPOUND_EXPR, NULL_TREE, first, second), size_zero_node);
7817 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
7818 their order) on the current loop. Unlink the current loop from the
7822 complete_loop_body (location, condition, body, reversed)
7824 tree condition, body;
7827 tree to_return = ctxp->current_loop;
7828 tree loop_body = LOOP_EXPR_BODY (to_return);
7831 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
7832 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
7833 The real EXIT_EXPR is one operand further. */
7834 EXPR_WFL_LINECOL (cnode) = location;
7835 /* This one is for accurate error reports */
7836 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
7837 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
7839 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
7844 /* Tailored version of complete_loop_body for FOR loops, when FOR
7845 loops feature the condition part */
7848 complete_for_loop (location, condition, update, body)
7850 tree condition, update, body;
7852 /* Put the condition and the loop body in place */
7853 tree loop = complete_loop_body (location, condition, body, 0);
7854 /* LOOP is the current loop which has been now popped of the loop
7855 stack. Install the update block */
7856 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
7860 /* If the loop isn't surrounded by a labeled statement, create one and
7861 insert LOOP as it's body. */
7864 patch_loop_statement (loop)
7867 tree cbl, loop_label, to_return_as_loop;
7869 if (LOOP_HAS_LABEL_P (loop))
7871 loop_label = ctxp->current_labeled_block;
7872 to_return_as_loop = loop;
7876 loop_label = generate_labeled_block ();
7877 LABELED_BLOCK_BODY (loop_label) = loop;
7878 PUSH_LABELED_BLOCK (loop_label);
7879 to_return_as_loop = loop_label;
7881 TREE_TYPE (to_return_as_loop) = void_type_node;
7882 return to_return_as_loop;
7885 /* 14.13, 14.14: break and continue Statements */
7887 /* Build a break or a continue statement. a null NAME indicates an
7888 unlabeled break/continue statement. */
7891 build_bc_statement (location, is_break, name)
7892 int location, is_break;
7895 tree break_continue, label_block_expr = NULL_TREE;
7899 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
7900 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
7901 /* Null means that we don't have a target for this named
7902 break/continue. In this case, we make the target to be the
7903 label name, so that the error can be reported accuratly in
7904 patch_bc_statement. */
7905 label_block_expr = EXPR_WFL_NODE (name);
7907 /* Unlabeled break/continue will be handled during the
7908 break/continue patch operation */
7910 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
7912 IS_BREAK_STMT_P (break_continue) = is_break;
7913 TREE_SIDE_EFFECTS (break_continue) = 1;
7914 EXPR_WFL_LINECOL (break_continue) = location;
7915 return break_continue;
7918 /* Verification of a break/continue statement. */
7921 patch_bc_statement (node)
7924 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
7925 int is_unlabeled = 0;
7926 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7928 /* Not having a target means that the break/continue statement is
7929 unlabeled. We try to find a descent label for it */
7933 /* There should be a loop to branch to */
7934 if (ctxp->current_loop)
7936 /* At that stage, we're in the loop body, which is
7937 encapsulated around a LABELED_BLOCK_EXPR. So searching
7938 the current loop label requires us to consider the
7939 labeled block before the current one. */
7940 if (!LOOP_HAS_LABEL_SKIP_P (ctxp->current_loop))
7941 fatal ("unlabeled loop has not installed label -- "
7942 "patch_bc_statement");
7943 bc_label = TREE_CHAIN (ctxp->current_labeled_block);
7945 /* Not having a loop to break/continue to is an error */
7948 parse_error_context (wfl_operator, "`%s' must be in loop%s",
7949 (IS_BREAK_STMT_P (node) ? "break" : "continue"),
7950 (IS_BREAK_STMT_P (node) ? " or switch" : ""));
7951 return error_mark_node;
7954 /* Having an identifier here means that the target is unknown. */
7955 else if (TREE_CODE (bc_label) == IDENTIFIER_NODE)
7957 parse_error_context (wfl_operator, "No label definition found for `%s'",
7958 IDENTIFIER_POINTER (bc_label));
7959 return error_mark_node;
7962 /* Find the statement we're targeting. */
7963 target_stmt = LABELED_BLOCK_BODY (bc_label);
7965 /* 14.13 The break Statement */
7966 if (IS_BREAK_STMT_P (node))
7968 /* Named break are always fine, as far as they have a target
7969 (already verified). Anonymous break need to target
7970 while/do/for/switch */
7972 !(TREE_CODE (target_stmt) == LOOP_EXPR /* do/while/for */
7973 || 0)) /* switch FIXME */
7975 parse_error_context (wfl_operator,
7976 "`break' must be in loop or switch");
7977 return error_mark_node;
7979 /* If previously unlabeled, install the new found label */
7981 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
7983 /* 14.14 The continue Statement */
7984 /* The continue statement must always target a loop */
7987 if (TREE_CODE (target_stmt) != LOOP_EXPR) /* do/while/for */
7989 parse_error_context (wfl_operator, "`continue' must be in loop");
7990 return error_mark_node;
7992 /* Everything looks good. We can fix the `continue' jump to go
7993 at the place in the loop were the continue is. The continue
7994 is the current labeled block, by construction. */
7995 EXIT_BLOCK_LABELED_BLOCK (node) = ctxp->current_labeled_block;
7998 /* Our break/continue don't return values. */
7999 TREE_TYPE (node) = void_type_node;
8000 /* Encapsulate the break within a compound statement so that it's
8001 expanded all the times by expand_expr (and not clobered
8002 sometimes, like after a if statement) */
8003 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
8004 TREE_SIDE_EFFECTS (node) = 1;
8008 /* Process the exit expression belonging to a loop. Its type must be
8012 patch_exit_expr (node)
8015 tree expression = TREE_OPERAND (node, 0);
8016 TREE_TYPE (node) = error_mark_node;
8017 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8019 /* The type of expression must be boolean */
8020 if (TREE_TYPE (expression) != boolean_type_node)
8024 "Incompatible type for loop conditional. Can't convert `%s' to "
8026 lang_printable_name (TREE_TYPE (expression)));
8027 return error_mark_node;
8029 /* Now we know things are allright, invert the condition, fold and
8031 TREE_OPERAND (node, 0) =
8032 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
8033 TREE_TYPE (node) = void_type_node;