* parse.y (DIR_SEPARATOR): New define.
[official-gcc.git] / gcc / java / parse.y
blob41d9f919b785c84025029317ce098dab53c0352e
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)
11 any later version.
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
29 language).
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 static_initializer: added the rule 'static block SC_TK'.
40 Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
43 statement_nsi: 'nsi' should be read no_short_if.
45 Some rules have been modified to support JDK1.1 inner classes
46 definitions and other extensions. */
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <dirent.h>
53 #ifdef __STDC__
54 #include <stdarg.h>
55 #else
56 #include <varargs.h>
57 #endif
59 #include "config.h"
60 #include "tree.h"
61 #include "rtl.h"
62 #include "obstack.h"
63 #include "toplev.h"
64 #include "flags.h"
65 #include "java-tree.h"
66 #include "jcf.h"
67 #include "lex.h"
68 #include "parse.h"
69 #include "zipfile.h"
70 #include "convert.h"
71 #include "buffer.h"
73 #ifndef DIR_SEPARATOR
74 #define DIR_SEPARATOR '/'
75 #endif
77 /* Local function prototypes */
78 static char *java_accstring_lookup PROTO ((int));
79 static void classitf_redefinition_error PROTO ((char *,tree, tree, tree));
80 static void variable_redefinition_error PROTO ((tree, tree, tree, int));
81 static void check_modifiers PROTO ((char *, int, int));
82 static tree create_class PROTO ((int, tree, tree, tree));
83 static tree create_interface PROTO ((int, tree, tree));
84 static tree find_field PROTO ((tree, tree));
85 static tree lookup_field_wrapper PROTO ((tree, tree));
86 static int duplicate_declaration_error_p PROTO ((tree, tree, tree));
87 static void register_fields PROTO ((int, tree, tree));
88 static tree parser_qualified_classname PROTO ((tree));
89 static int parser_check_super PROTO ((tree, tree, tree));
90 static int parser_check_super_interface PROTO ((tree, tree, tree));
91 static void check_modifiers_consistency PROTO ((int));
92 static tree lookup_cl PROTO ((tree));
93 static tree lookup_java_method2 PROTO ((tree, tree, int));
94 static tree method_header PROTO ((int, tree, tree, tree));
95 static void fix_method_argument_names PROTO ((tree ,tree));
96 static tree method_declarator PROTO ((tree, tree));
97 static void parse_warning_context VPROTO ((tree cl, char *msg, ...));
98 static void issue_warning_error_from_context PROTO ((tree, char *msg, va_list));
99 static tree parse_jdk1_1_error PROTO ((char *));
100 static void complete_class_report_errors PROTO ((jdep *));
101 static int process_imports PROTO ((void));
102 static void read_import_dir PROTO ((tree));
103 static int find_in_imports_on_demand PROTO ((tree));
104 static int find_in_imports PROTO ((tree));
105 static int check_pkg_class_access PROTO ((tree, tree));
106 static tree resolve_package PROTO ((tree, tree *));
107 static tree lookup_package_type PROTO ((char *, int));
108 static tree resolve_class PROTO ((tree, tree, tree));
109 static tree do_resolve_class PROTO ((tree, tree, tree));
110 static void declare_local_variables PROTO ((int, tree, tree));
111 static void source_start_java_method PROTO ((tree));
112 static void source_end_java_method PROTO ((void));
113 static void expand_start_java_method PROTO ((tree));
114 static tree find_name_in_single_imports PROTO ((tree));
115 static void check_abstract_method_header PROTO ((tree));
116 static tree lookup_java_interface_method2 PROTO ((tree, tree));
117 static tree resolve_expression_name PROTO ((tree, tree *));
118 static tree maybe_create_class_interface_decl PROTO ((tree, tree, tree));
119 static int check_class_interface_creation PROTO ((int, int, tree,
120 tree, tree, tree));
121 static tree patch_method_invocation PROTO ((tree, tree, tree,
122 int *, tree *));
123 static int breakdown_qualified PROTO ((tree *, tree *, tree));
124 static tree resolve_and_layout PROTO ((tree, tree));
125 static tree resolve_no_layout PROTO ((tree, tree));
126 static int invocation_mode PROTO ((tree, int));
127 static tree find_applicable_accessible_methods_list PROTO ((int, tree,
128 tree, tree));
129 static tree find_most_specific_methods_list PROTO ((tree));
130 static int argument_types_convertible PROTO ((tree, tree));
131 static tree patch_invoke PROTO ((tree, tree, tree));
132 static tree lookup_method_invoke PROTO ((int, tree, tree, tree, tree));
133 static tree register_incomplete_type PROTO ((int, tree, tree, tree));
134 static tree obtain_incomplete_type PROTO ((tree));
135 static tree java_complete_lhs PROTO ((tree));
136 static tree java_complete_tree PROTO ((tree));
137 static void java_complete_expand_method PROTO ((tree));
138 static int unresolved_type_p PROTO ((tree, tree *));
139 static void create_jdep_list PROTO ((struct parser_ctxt *));
140 static tree build_expr_block PROTO ((tree, tree));
141 static tree enter_block PROTO ((void));
142 static tree enter_a_block PROTO ((tree));
143 static tree exit_block PROTO ((void));
144 static tree lookup_name_in_blocks PROTO ((tree));
145 static void maybe_absorb_scoping_blocks PROTO ((void));
146 static tree build_method_invocation PROTO ((tree, tree));
147 static tree build_new_invocation PROTO ((tree, tree));
148 static tree build_assignment PROTO ((int, int, tree, tree));
149 static tree build_binop PROTO ((enum tree_code, int, tree, tree));
150 static int check_final_assignment PROTO ((tree ,tree));
151 static tree patch_assignment PROTO ((tree, tree, tree ));
152 static tree patch_binop PROTO ((tree, tree, tree));
153 static tree build_unaryop PROTO ((int, int, tree));
154 static tree build_incdec PROTO ((int, int, tree, int));
155 static tree patch_unaryop PROTO ((tree, tree));
156 static tree build_cast PROTO ((int, tree, tree));
157 static tree build_null_of_type PROTO ((tree));
158 static tree patch_cast PROTO ((tree, tree));
159 static int valid_ref_assignconv_cast_p PROTO ((tree, tree, int));
160 static int valid_builtin_assignconv_identity_widening_p PROTO ((tree, tree));
161 static int valid_cast_to_p PROTO ((tree, tree));
162 static int valid_method_invocation_conversion_p PROTO ((tree, tree));
163 static tree try_builtin_assignconv PROTO ((tree, tree, tree));
164 static tree try_reference_assignconv PROTO ((tree, tree));
165 static tree build_unresolved_array_type PROTO ((tree));
166 static tree build_array_from_name PROTO ((tree, tree, tree, tree *));
167 static tree build_array_ref PROTO ((int, tree, tree));
168 static tree patch_array_ref PROTO ((tree));
169 static tree make_qualified_name PROTO ((tree, tree, int));
170 static tree merge_qualified_name PROTO ((tree, tree));
171 static tree make_qualified_primary PROTO ((tree, tree, int));
172 static int resolve_qualified_expression_name PROTO ((tree, tree *,
173 tree *, tree *));
174 static void qualify_ambiguous_name PROTO ((tree));
175 static void maybe_generate_clinit PROTO ((void));
176 static tree resolve_field_access PROTO ((tree, tree *, tree *));
177 static tree build_newarray_node PROTO ((tree, tree, int));
178 static tree patch_newarray PROTO ((tree));
179 static tree resolve_type_during_patch PROTO ((tree));
180 static tree build_this PROTO ((int));
181 static tree build_return PROTO ((int, tree));
182 static tree patch_return PROTO ((tree));
183 static tree maybe_access_field PROTO ((tree, tree, tree));
184 static int complete_function_arguments PROTO ((tree));
185 static int check_for_static_method_reference PROTO ((tree, tree, tree, tree, tree));
186 static int not_accessible_p PROTO ((tree, tree, int));
187 static void check_deprecation PROTO ((tree, tree));
188 static int class_in_current_package PROTO ((tree));
189 static tree build_if_else_statement PROTO ((int, tree, tree, tree));
190 static tree patch_if_else_statement PROTO ((tree));
191 static tree add_stmt_to_compound PROTO ((tree, tree, tree));
192 static tree add_stmt_to_block PROTO ((tree, tree, tree));
193 static tree patch_exit_expr PROTO ((tree));
194 static tree build_labeled_block PROTO ((int, tree));
195 static tree generate_labeled_block PROTO (());
196 static tree complete_labeled_statement PROTO ((tree, tree));
197 static tree build_bc_statement PROTO ((int, int, tree));
198 static tree patch_bc_statement PROTO ((tree));
199 static tree patch_loop_statement PROTO ((tree));
200 static tree build_new_loop PROTO ((tree));
201 static tree build_loop_body PROTO ((int, tree, int));
202 static tree complete_loop_body PROTO ((int, tree, tree, int));
203 static tree build_debugable_stmt PROTO ((int, tree));
204 static tree complete_for_loop PROTO ((int, tree, tree, tree));
205 static tree patch_switch_statement PROTO ((tree));
206 static tree string_constant_concatenation PROTO ((tree, tree));
207 static tree build_string_concatenation PROTO ((tree, tree));
208 static tree patch_string_cst PROTO ((tree));
209 static tree patch_string PROTO ((tree));
210 static tree build_jump_to_finally PROTO ((tree, tree, tree, tree));
211 static tree build_try_statement PROTO ((int, tree, tree, tree));
212 static tree patch_try_statement PROTO ((tree));
213 static tree patch_synchronized_statement PROTO ((tree, tree));
214 static tree patch_throw_statement PROTO ((tree, tree));
215 static void check_thrown_exceptions PROTO ((int, tree));
216 static int check_thrown_exceptions_do PROTO ((tree));
217 static void purge_unchecked_exceptions PROTO ((tree));
218 static void check_throws_clauses PROTO ((tree, tree, tree));
219 static void complete_method_declaration PROTO ((tree));
220 static tree build_super_invocation PROTO (());
221 static int verify_constructor_circularity PROTO ((tree, tree));
222 static char *constructor_circularity_msg PROTO ((tree, tree));
223 static tree build_this_super_qualified_invocation PROTO ((int, tree, tree,
224 int, int));
225 static char *get_printable_method_name PROTO ((tree));
226 static tree patch_conditional_expr PROTO ((tree, tree, tree));
227 static void maybe_generate_finit PROTO (());
228 static void fix_constructors PROTO ((tree));
229 static int verify_constructor_super PROTO (());
230 static tree create_artificial_method PROTO ((tree, int, tree, tree, tree));
231 static void start_artificial_method_body PROTO ((tree));
232 static void end_artificial_method_body PROTO ((tree));
233 static int check_method_redefinition PROTO ((tree, tree));
234 static int reset_method_name PROTO ((tree));
235 static void java_check_regular_methods PROTO ((tree));
236 static void java_check_abstract_methods PROTO ((tree));
237 static tree maybe_build_primttype_type_ref PROTO ((tree, tree));
238 static void unreachable_stmt_error PROTO ((tree));
239 static tree find_expr_with_wfl PROTO ((tree));
240 static void missing_return_error PROTO ((tree));
241 static tree build_new_array_init PROTO ((int, tree));
242 static tree patch_new_array_init PROTO ((tree, tree));
243 static tree maybe_build_array_element_wfl PROTO ((tree));
244 static int array_constructor_check_entry PROTO ((tree, tree));
245 static char *purify_type_name PROTO ((char *));
246 static tree patch_initialized_static_field PROTO ((tree));
247 static tree fold_constant_for_init PROTO ((tree, tree));
249 /* Number of error found so far. */
250 int java_error_count;
251 /* Number of warning found so far. */
252 int java_warning_count;
254 /* The current parser context */
255 static struct parser_ctxt *ctxp;
257 /* List of things that were anlyzed for which code will be generated */
258 static struct parser_ctxt *ctxp_for_generation = NULL;
260 /* binop_lookup maps token to tree_code. It is used where binary
261 operations are involved and required by the parser. RDIV_EXPR
262 covers both integral/floating point division. The code is changed
263 once the type of both operator is worked out. */
265 static enum tree_code binop_lookup[19] =
267 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
268 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
269 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
270 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
271 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
273 #define BINOP_LOOKUP(VALUE) \
274 binop_lookup [((VALUE) - PLUS_TK)% \
275 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
277 /* Fake WFL used to report error message. It is initialized once if
278 needed and reused with it's location information is overriden. */
279 tree wfl_operator = NULL_TREE;
281 /* The "$L" identifier we use to create labels. */
282 static tree label_id = NULL_TREE;
284 /* The "StringBuffer" identifier used for the String `+' operator. */
285 static tree wfl_string_buffer = NULL_TREE;
287 /* The "append" identifier used for String `+' operator. */
288 static tree wfl_append = NULL_TREE;
290 /* The "toString" identifier used for String `+' operator. */
291 static tree wfl_to_string = NULL_TREE;
294 %union {
295 tree node;
296 int sub_token;
297 struct {
298 int token;
299 int location;
300 } operator;
301 int value;
304 %pure_parser
306 /* Things defined here have to match the order of what's in the
307 binop_lookup table. */
309 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
310 %token LS_TK SRS_TK ZRS_TK
311 %token AND_TK XOR_TK OR_TK
312 %token BOOL_AND_TK BOOL_OR_TK
313 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
315 /* This maps to the same binop_lookup entry than the token above */
317 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
318 %token REM_ASSIGN_TK
319 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
320 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
323 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
325 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
326 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
327 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
328 %token PAD_TK ABSTRACT_TK MODIFIER_TK
330 /* Keep those two in order, too */
331 %token DECR_TK INCR_TK
333 /* From now one, things can be in any order */
335 %token DEFAULT_TK IF_TK THROW_TK
336 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
337 %token THROWS_TK BREAK_TK IMPORT_TK
338 %token ELSE_TK INSTANCEOF_TK RETURN_TK
339 %token VOID_TK CATCH_TK INTERFACE_TK
340 %token CASE_TK EXTENDS_TK FINALLY_TK
341 %token SUPER_TK WHILE_TK CLASS_TK
342 %token SWITCH_TK CONST_TK TRY_TK
343 %token FOR_TK NEW_TK CONTINUE_TK
344 %token GOTO_TK PACKAGE_TK THIS_TK
346 %token BYTE_TK SHORT_TK INT_TK LONG_TK
347 %token CHAR_TK INTEGRAL_TK
349 %token FLOAT_TK DOUBLE_TK FP_TK
351 %token ID_TK
353 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
355 %token ASSIGN_ANY_TK ASSIGN_TK
356 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
358 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
359 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
361 %type <value> modifiers MODIFIER_TK
363 %type <node> super ID_TK identifier
364 %type <node> name simple_name qualified_name
365 %type <node> class_declaration type_declaration compilation_unit
366 field_declaration method_declaration extends_interfaces
367 interfaces interface_type_list
368 interface_declaration class_member_declaration
369 import_declarations package_declaration
370 type_declarations interface_body
371 interface_member_declaration constant_declaration
372 interface_member_declarations interface_type
373 abstract_method_declaration interface_type_list
374 %type <node> class_body_declaration class_member_declaration
375 static_initializer constructor_declaration block
376 %type <node> class_body_declarations constructor_header
377 %type <node> class_or_interface_type class_type class_type_list
378 constructor_declarator explicit_constructor_invocation
379 %type <node> dim_expr dim_exprs this_or_super throws
381 %type <node> variable_declarator_id variable_declarator
382 variable_declarators variable_initializer
383 variable_initializers constructor_body
384 array_initializer
386 %type <node> class_body block_end
387 %type <node> statement statement_without_trailing_substatement
388 labeled_statement if_then_statement label_decl
389 if_then_else_statement while_statement for_statement
390 statement_nsi labeled_statement_nsi do_statement
391 if_then_else_statement_nsi while_statement_nsi
392 for_statement_nsi statement_expression_list for_init
393 for_update statement_expression expression_statement
394 primary_no_new_array expression primary
395 array_creation_expression array_type
396 class_instance_creation_expression field_access
397 method_invocation array_access something_dot_new
398 argument_list postfix_expression while_expression
399 post_increment_expression post_decrement_expression
400 unary_expression_not_plus_minus unary_expression
401 pre_increment_expression pre_decrement_expression
402 unary_expression_not_plus_minus cast_expression
403 multiplicative_expression additive_expression
404 shift_expression relational_expression
405 equality_expression and_expression
406 exclusive_or_expression inclusive_or_expression
407 conditional_and_expression conditional_or_expression
408 conditional_expression assignment_expression
409 left_hand_side assignment for_header for_begin
410 constant_expression do_statement_begin empty_statement
411 switch_statement synchronized_statement throw_statement
412 try_statement switch_expression switch_block
413 catches catch_clause catch_clause_parameter finally
414 %type <node> return_statement break_statement continue_statement
416 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
417 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
418 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
419 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
420 %type <operator> ASSIGN_ANY_TK assignment_operator
421 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
422 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
423 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
424 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK
425 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
426 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
427 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
429 %type <node> method_body
431 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
432 STRING_LIT_TK NULL_TK VOID_TK
434 %type <node> IF_TK WHILE_TK FOR_TK
436 %type <node> formal_parameter_list formal_parameter
437 method_declarator method_header
439 %type <node> primitive_type reference_type type
440 BOOLEAN_TK INTEGRAL_TK FP_TK
443 /* 19.2 Production from 2.3: The Syntactic Grammar */
444 goal:
445 compilation_unit
449 /* 19.3 Productions from 3: Lexical structure */
450 literal:
451 INT_LIT_TK
452 | FP_LIT_TK
453 | BOOL_LIT_TK
454 | CHAR_LIT_TK
455 | STRING_LIT_TK
456 | NULL_TK
459 /* 19.4 Productions from 4: Types, Values and Variables */
460 type:
461 primitive_type
462 | reference_type
465 primitive_type:
466 INTEGRAL_TK
467 | FP_TK
468 | BOOLEAN_TK
471 reference_type:
472 class_or_interface_type
473 | array_type
476 class_or_interface_type:
477 name
480 class_type:
481 class_or_interface_type /* Default rule */
484 interface_type:
485 class_or_interface_type
488 array_type:
489 primitive_type OSB_TK CSB_TK
491 $$ = build_java_array_type ($1, -1);
492 CLASS_LOADED_P ($$) = 1;
494 | name OSB_TK CSB_TK
495 { $$ = build_unresolved_array_type ($1); }
496 | array_type OSB_TK CSB_TK
497 { $$ = build_unresolved_array_type ($1); }
498 | primitive_type OSB_TK error
499 {RULE ("']' expected"); RECOVER;}
500 | array_type OSB_TK error
501 {RULE ("']' expected"); RECOVER;}
504 /* 19.5 Productions from 6: Names */
505 name:
506 simple_name /* Default rule */
507 | qualified_name /* Default rule */
510 simple_name:
511 identifier /* Default rule */
514 qualified_name:
515 name DOT_TK identifier
516 { $$ = make_qualified_name ($1, $3, $2.location); }
519 identifier:
520 ID_TK
523 /* 19.6: Production from 7: Packages */
524 compilation_unit:
525 {$$ = NULL;}
526 | package_declaration
527 | import_declarations
528 | type_declarations
529 | package_declaration import_declarations
530 | package_declaration type_declarations
531 | import_declarations type_declarations
532 | package_declaration import_declarations type_declarations
535 import_declarations:
536 import_declaration
538 $$ = NULL;
540 | import_declarations import_declaration
542 $$ = NULL;
546 type_declarations:
547 type_declaration
548 | type_declarations type_declaration
551 package_declaration:
552 PACKAGE_TK name SC_TK
553 { ctxp->package = EXPR_WFL_NODE ($2); }
554 | PACKAGE_TK error
555 {yyerror ("Missing name"); RECOVER;}
556 | PACKAGE_TK name error
557 {yyerror ("';' expected"); RECOVER;}
560 import_declaration:
561 single_type_import_declaration
562 | type_import_on_demand_declaration
565 single_type_import_declaration:
566 IMPORT_TK name SC_TK
568 tree name = EXPR_WFL_NODE ($2), node, last_name;
569 int i = IDENTIFIER_LENGTH (name)-1;
570 char *last = &IDENTIFIER_POINTER (name)[i];
571 while (last != IDENTIFIER_POINTER (name))
573 if (last [0] == '.')
574 break;
575 last--;
577 last_name = get_identifier (++last);
578 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
580 tree err = find_name_in_single_imports (last_name);
581 if (err && err != name)
582 parse_error_context
583 ($2, "Ambiguous class: `%s' and `%s'",
584 IDENTIFIER_POINTER (name),
585 IDENTIFIER_POINTER (err));
586 else
587 REGISTER_IMPORT ($2, last_name)
589 else
590 REGISTER_IMPORT ($2, last_name);
592 | IMPORT_TK error
593 {yyerror ("Missing name"); RECOVER;}
594 | IMPORT_TK name error
595 {yyerror ("';' expected"); RECOVER;}
598 type_import_on_demand_declaration:
599 IMPORT_TK name DOT_TK MULT_TK SC_TK
601 tree name = EXPR_WFL_NODE ($2);
602 tree node = build_tree_list ($2, NULL_TREE);
603 read_import_dir ($2);
604 TREE_CHAIN (node) = ctxp->import_demand_list;
605 ctxp->import_demand_list = node;
607 | IMPORT_TK name DOT_TK error
608 {yyerror ("'*' expected"); RECOVER;}
609 | IMPORT_TK name DOT_TK MULT_TK error
610 {yyerror ("';' expected"); RECOVER;}
613 type_declaration:
614 class_declaration
616 maybe_generate_finit ();
617 maybe_generate_clinit ();
618 $$ = $1;
620 | interface_declaration
621 | SC_TK
622 { $$ = NULL; }
623 | error
625 YYERROR_NOW;
626 yyerror ("Class or interface declaration expected");
630 /* 19.7 Shortened from the original:
631 modifiers: modifier | modifiers modifier
632 modifier: any of public... */
633 modifiers:
634 MODIFIER_TK
636 $$ = (1 << $1);
638 | modifiers MODIFIER_TK
640 int acc = (1 << $2);
641 if ($$ & acc)
642 parse_error_context
643 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
644 java_accstring_lookup (acc));
645 else
647 $$ |= acc;
652 /* 19.8.1 Production from $8.1: Class Declaration */
653 class_declaration:
654 modifiers CLASS_TK identifier super interfaces
655 { create_class ($1, $3, $4, $5); }
656 class_body
658 $$ = $7;
660 | CLASS_TK identifier super interfaces
661 { create_class (0, $2, $3, $4); }
662 class_body
664 $$ = $6;
666 | modifiers CLASS_TK error
667 {yyerror ("Missing class name"); RECOVER;}
668 | CLASS_TK error
669 {yyerror ("Missing class name"); RECOVER;}
670 | CLASS_TK identifier error
671 {if (!ctxp->class_err) yyerror ("'{' expected"); DRECOVER(class1);}
672 | modifiers CLASS_TK identifier error
673 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
676 super:
677 { $$ = NULL; }
678 | EXTENDS_TK class_type
679 { $$ = $2; }
680 | EXTENDS_TK class_type error
681 {yyerror ("'{' expected"); ctxp->class_err=1;}
682 | EXTENDS_TK error
683 {yyerror ("Missing super class name"); ctxp->class_err=1;}
686 interfaces:
687 { $$ = NULL_TREE; }
688 | IMPLEMENTS_TK interface_type_list
689 { $$ = $2; }
690 | IMPLEMENTS_TK error
692 ctxp->class_err=1;
693 yyerror ("Missing interface name");
697 interface_type_list:
698 interface_type
700 ctxp->interface_number = 1;
701 $$ = build_tree_list ($1, NULL_TREE);
703 | interface_type_list C_TK interface_type
705 ctxp->interface_number++;
706 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
708 | interface_type_list C_TK error
709 {yyerror ("Missing interface name"); RECOVER;}
712 class_body:
713 OCB_TK CCB_TK
714 { $$ = ctxp->current_parsed_class; }
715 | OCB_TK class_body_declarations CCB_TK
716 { $$ = ctxp->current_parsed_class; }
719 class_body_declarations:
720 class_body_declaration
721 | class_body_declarations class_body_declaration
724 class_body_declaration:
725 class_member_declaration
726 | static_initializer
727 | constructor_declaration
728 | block /* Added, JDK1.1, instance initializer */
729 { $$ = parse_jdk1_1_error ("instance initializer"); }
732 class_member_declaration:
733 field_declaration
734 | method_declaration
735 | class_declaration /* Added, JDK1.1 inner classes */
736 { $$ = parse_jdk1_1_error ("inner classe declaration"); }
737 | interface_declaration /* Added, JDK1.1 inner classes */
738 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
741 /* 19.8.2 Productions from 8.3: Field Declarations */
742 field_declaration:
743 type variable_declarators SC_TK
744 { register_fields (0, $1, $2); }
745 | modifiers type variable_declarators SC_TK
747 check_modifiers
748 ("Illegal modifier `%s' for field declaration",
749 $1, FIELD_MODIFIERS);
750 check_modifiers_consistency ($1);
751 register_fields ($1, $2, $3);
755 variable_declarators:
756 /* Should we use build_decl_list () instead ? FIXME */
757 variable_declarator /* Default rule */
758 | variable_declarators C_TK variable_declarator
759 { $$ = chainon ($1, $3); }
760 | variable_declarators C_TK error
761 {yyerror ("Missing term"); RECOVER;}
764 variable_declarator:
765 variable_declarator_id
766 { $$ = build_tree_list ($1, NULL_TREE); }
767 | variable_declarator_id ASSIGN_TK variable_initializer
769 if (java_error_count)
770 $3 = NULL_TREE;
771 $$ = build_tree_list
772 ($1, build_assignment ($2.token, $2.location, $1, $3));
774 | variable_declarator_id ASSIGN_TK error
776 yyerror ("Missing variable initializer");
777 $$ = build_tree_list ($1, NULL_TREE);
778 RECOVER;
780 | variable_declarator_id ASSIGN_TK variable_initializer error
782 yyerror ("';' expected");
783 $$ = build_tree_list ($1, NULL_TREE);
784 RECOVER;
788 variable_declarator_id:
789 identifier
790 | variable_declarator_id OSB_TK CSB_TK
791 { $$ = build_unresolved_array_type ($1); }
792 | identifier error
793 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
794 | variable_declarator_id OSB_TK error
795 {yyerror ("']' expected"); DRECOVER(vdi);}
796 | variable_declarator_id CSB_TK error
797 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
800 variable_initializer:
801 expression
802 | array_initializer
805 /* 19.8.3 Productions from 8.4: Method Declarations */
806 method_declaration:
807 method_header
809 current_function_decl = $1;
810 source_start_java_method (current_function_decl);
812 method_body
813 { complete_method_declaration ($3); }
814 | method_header error
815 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
818 method_header:
819 type method_declarator throws
820 { $$ = method_header (0, $1, $2, $3); }
821 | VOID_TK method_declarator throws
822 { $$ = method_header (0, void_type_node, $2, $3); }
823 | modifiers type method_declarator throws
824 { $$ = method_header ($1, $2, $3, $4); }
825 | modifiers VOID_TK method_declarator throws
826 { $$ = method_header ($1, void_type_node, $3, $4); }
827 | type error
828 {RECOVER;}
829 | modifiers type error
830 {RECOVER;}
831 | VOID_TK error
832 {yyerror ("Identifier expected"); RECOVER;}
833 | modifiers VOID_TK error
834 {yyerror ("Identifier expected"); RECOVER;}
835 | modifiers error
837 yyerror ("Invalid method declaration, return type required");
838 RECOVER;
842 method_declarator:
843 identifier OP_TK CP_TK
844 { $$ = method_declarator ($1, NULL_TREE); }
845 | identifier OP_TK formal_parameter_list CP_TK
846 { $$ = method_declarator ($1, $3); }
847 | method_declarator OSB_TK CSB_TK
849 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
850 TREE_PURPOSE ($1) =
851 build_unresolved_array_type (TREE_PURPOSE ($1));
852 parse_warning_context
853 (wfl_operator,
854 "Discouraged form of returned type specification");
856 | identifier OP_TK error
857 {yyerror ("')' expected"); DRECOVER(method_declarator);}
858 | method_declarator OSB_TK error
859 {yyerror ("']' expected"); RECOVER;}
862 formal_parameter_list:
863 formal_parameter
865 ctxp->formal_parameter_number = 1;
867 | formal_parameter_list C_TK formal_parameter
869 ctxp->formal_parameter_number += 1;
870 $$ = chainon ($1, $3);
872 | formal_parameter_list C_TK error
873 {yyerror ("Missing formal parameter term"); RECOVER;}
876 formal_parameter:
877 type variable_declarator_id
879 $$ = build_tree_list ($2, $1);
881 | modifiers type variable_declarator_id /* Added, JDK1.1 final parms */
882 { $$ = parse_jdk1_1_error ("final parameters"); }
883 | type error
884 {yyerror ("Missing identifier"); RECOVER;}
885 | modifiers type error
887 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
888 yyerror ("Missing identifier"); RECOVER;
892 throws:
893 { $$ = NULL_TREE; }
894 | THROWS_TK class_type_list
895 { $$ = $2; }
896 | THROWS_TK error
897 {yyerror ("Missing class type term"); RECOVER;}
900 class_type_list:
901 class_type
902 { $$ = build_tree_list ($1, $1); }
903 | class_type_list C_TK class_type
904 { $$ = tree_cons ($3, $3, $1); }
905 | class_type_list C_TK error
906 {yyerror ("Missing class type term"); RECOVER;}
909 method_body:
910 block
911 | block SC_TK
912 | SC_TK
913 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
916 /* 19.8.4 Productions from 8.5: Static Initializers */
917 static_initializer:
918 static block
920 RULE ("STATIC_INITIALIZER");
922 | static block SC_TK /* Shouldn't be here. FIXME */
924 RULE ("STATIC_INITIALIZER");
928 static: /* Test lval.sub_token here */
929 MODIFIER_TK
931 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
935 /* 19.8.5 Productions from 8.6: Constructor Declarations */
936 constructor_declaration:
937 constructor_header
939 current_function_decl = $1;
940 source_start_java_method (current_function_decl);
942 constructor_body
943 { complete_method_declaration ($3); }
946 constructor_header:
947 constructor_declarator throws
948 { $$ = method_header (0, NULL_TREE, $1, $2); }
949 | modifiers constructor_declarator throws
950 { $$ = method_header ($1, NULL_TREE, $2, $3); }
953 constructor_declarator:
954 simple_name OP_TK CP_TK
955 { $$ = method_declarator ($1, NULL_TREE); }
956 | simple_name OP_TK formal_parameter_list CP_TK
957 { $$ = method_declarator ($1, $3); }
960 constructor_body:
961 /* Unlike regular method, we always need a complete (empty)
962 body so we can safely perform all the required code
963 addition (super invocation and field initialization) */
964 block_begin block_end
966 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
967 $$ = $2;
969 | block_begin explicit_constructor_invocation block_end
970 { $$ = $3; }
971 | block_begin block_statements block_end
972 { $$ = $3; }
973 | block_begin explicit_constructor_invocation block_statements block_end
974 { $$ = $4; }
977 /* Error recovery for that rule moved down expression_statement: rule. */
978 explicit_constructor_invocation:
979 this_or_super OP_TK CP_TK SC_TK
981 $$ = build_method_invocation ($1, NULL_TREE);
982 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
983 $$ = java_method_add_stmt (current_function_decl, $$);
985 | this_or_super OP_TK argument_list CP_TK SC_TK
987 $$ = build_method_invocation ($1, $3);
988 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
989 $$ = java_method_add_stmt (current_function_decl, $$);
991 /* Added, JDK1.1 inner classes. Modified because the rule
992 'primary' couldn't work. */
993 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
994 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
995 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
996 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
999 this_or_super: /* Added, simplifies error diagnostics */
1000 THIS_TK
1002 tree wfl = build_wfl_node (this_identifier_node,
1003 input_filename, 0, 0);
1004 EXPR_WFL_LINECOL (wfl) = $1.location;
1005 $$ = wfl;
1007 | SUPER_TK
1009 tree wfl = build_wfl_node (super_identifier_node,
1010 input_filename, 0, 0);
1011 EXPR_WFL_LINECOL (wfl) = $1.location;
1012 $$ = wfl;
1016 /* 19.9 Productions from 9: Interfaces */
1017 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1018 interface_declaration:
1019 INTERFACE_TK identifier
1020 { create_interface (0, $2, NULL_TREE); }
1021 interface_body
1023 $$ = $4;
1025 | modifiers INTERFACE_TK identifier
1026 { create_interface ($1, $3, NULL_TREE); }
1027 interface_body
1029 $$ = $5;
1031 | INTERFACE_TK identifier extends_interfaces
1032 { create_interface (0, $2, $3); }
1033 interface_body
1035 $$ = $5;
1037 | modifiers INTERFACE_TK identifier extends_interfaces
1038 { create_interface ($1, $3, $4); }
1039 interface_body
1041 $$ = $6;
1043 | INTERFACE_TK identifier error
1044 {yyerror ("(here)'{' expected"); RECOVER;}
1045 | modifiers INTERFACE_TK identifier error
1046 {yyerror ("(there)'{' expected"); RECOVER;}
1049 extends_interfaces:
1050 EXTENDS_TK interface_type
1052 ctxp->interface_number = 1;
1053 $$ = build_tree_list ($2, NULL_TREE);
1055 | extends_interfaces C_TK interface_type
1057 ctxp->interface_number++;
1058 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1060 | EXTENDS_TK error
1061 {yyerror ("Invalid interface type"); RECOVER;}
1062 | extends_interfaces C_TK error
1063 {yyerror ("Missing term"); RECOVER;}
1066 interface_body:
1067 OCB_TK CCB_TK
1068 { $$ = NULL_TREE; }
1069 | OCB_TK interface_member_declarations CCB_TK
1070 { $$ = NULL_TREE; }
1073 interface_member_declarations:
1074 interface_member_declaration
1075 | interface_member_declarations interface_member_declaration
1078 interface_member_declaration:
1079 constant_declaration
1080 | abstract_method_declaration
1081 | class_declaration /* Added, JDK1.1 inner classes */
1082 { $$ = parse_jdk1_1_error ("inner class declaration"); }
1083 | interface_declaration /* Added, JDK1.1 inner classes */
1084 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
1087 constant_declaration:
1088 field_declaration
1091 abstract_method_declaration:
1092 method_header SC_TK
1094 check_abstract_method_header ($1);
1095 current_function_decl = NULL_TREE; /* FIXME ? */
1097 | method_header error
1098 {yyerror ("';' expected"); RECOVER;}
1101 /* 19.10 Productions from 10: Arrays */
1102 array_initializer:
1103 OCB_TK CCB_TK
1104 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1105 | OCB_TK variable_initializers CCB_TK
1106 { $$ = build_new_array_init ($1.location, $2); }
1107 | OCB_TK variable_initializers C_TK CCB_TK
1108 { $$ = build_new_array_init ($1.location, $2); }
1111 variable_initializers:
1112 variable_initializer
1114 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1115 $1, NULL_TREE);
1117 | variable_initializers C_TK variable_initializer
1119 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1121 | variable_initializers C_TK error
1122 {yyerror ("Missing term"); RECOVER;}
1125 /* 19.11 Production from 14: Blocks and Statements */
1126 block:
1127 OCB_TK CCB_TK
1128 { $$ = empty_stmt_node; }
1129 | block_begin block_statements block_end
1130 { $$ = $3; }
1133 block_begin:
1134 OCB_TK
1135 { enter_block (); }
1138 block_end:
1139 CCB_TK
1141 maybe_absorb_scoping_blocks ();
1142 $$ = exit_block ();
1146 block_statements:
1147 block_statement
1148 | block_statements block_statement
1151 block_statement:
1152 local_variable_declaration_statement
1153 | statement
1154 { java_method_add_stmt (current_function_decl, $1); }
1155 | class_declaration /* Added, JDK1.1 inner classes */
1156 { parse_jdk1_1_error ("inner class declaration"); }
1159 local_variable_declaration_statement:
1160 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1163 local_variable_declaration:
1164 type variable_declarators
1165 { declare_local_variables (0, $1, $2); }
1166 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
1167 { declare_local_variables ($1, $2, $3); }
1170 statement:
1171 statement_without_trailing_substatement
1172 | labeled_statement
1173 | if_then_statement
1174 | if_then_else_statement
1175 | while_statement
1176 | for_statement
1178 /* If the for loop is unlabeled, we must return the
1179 block it was defined it. It our last chance to
1180 get a hold on it. */
1181 if (!LOOP_HAS_LABEL_P ($$))
1182 $$ = exit_block ();
1186 statement_nsi:
1187 statement_without_trailing_substatement
1188 | labeled_statement_nsi
1189 | if_then_else_statement_nsi
1190 | while_statement_nsi
1191 | for_statement_nsi
1194 statement_without_trailing_substatement:
1195 block
1196 | empty_statement
1197 | expression_statement
1198 | switch_statement
1199 | do_statement
1200 | break_statement
1201 | continue_statement
1202 | return_statement
1203 | synchronized_statement
1204 | throw_statement
1205 | try_statement
1208 empty_statement:
1209 SC_TK
1210 { $$ = empty_stmt_node; }
1213 label_decl:
1214 identifier REL_CL_TK
1216 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1217 EXPR_WFL_NODE ($1));
1218 pushlevel (2);
1219 push_labeled_block ($$);
1220 PUSH_LABELED_BLOCK ($$);
1224 labeled_statement:
1225 label_decl statement
1227 $$ = complete_labeled_statement ($1, $2);
1228 pop_labeled_block ();
1229 POP_LABELED_BLOCK ();
1231 | identifier error
1232 {yyerror ("':' expected"); RECOVER;}
1235 labeled_statement_nsi:
1236 label_decl statement_nsi
1238 $$ = complete_labeled_statement ($1, $2);
1239 pop_labeled_block ();
1240 POP_LABELED_BLOCK ();
1244 /* We concentrate here a bunch of error handling rules that we couldn't write
1245 earlier, because expression_statement catches a missing ';'. */
1246 expression_statement:
1247 statement_expression SC_TK
1249 /* We have a statement. Generate a WFL around it so
1250 we can debug it */
1251 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1252 /* We know we have a statement, so set the debug
1253 info to be eventually generate here. */
1254 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1256 | error SC_TK
1258 if (ctxp->prevent_ese != lineno)
1259 yyerror ("Invalid expression statement");
1260 DRECOVER (expr_stmt);
1262 | error OCB_TK
1264 if (ctxp->prevent_ese != lineno)
1265 yyerror ("Invalid expression statement");
1266 DRECOVER (expr_stmt);
1268 | error CCB_TK
1270 if (ctxp->prevent_ese != lineno)
1271 yyerror ("Invalid expression statement");
1272 DRECOVER (expr_stmt);
1274 | this_or_super OP_TK error
1275 {yyerror ("')' expected"); RECOVER;}
1276 | this_or_super OP_TK CP_TK error
1278 yyerror ("Constructor invocation must be first "
1279 "thing in a constructor");
1280 RECOVER;
1282 | this_or_super OP_TK argument_list error
1283 {yyerror ("')' expected"); RECOVER;}
1284 | this_or_super OP_TK argument_list CP_TK error
1286 yyerror ("Constructor invocation must be first "
1287 "thing in a constructor");
1288 RECOVER;
1290 | name DOT_TK SUPER_TK error
1291 {yyerror ("'(' expected"); RECOVER;}
1292 | name DOT_TK SUPER_TK OP_TK error
1293 {yyerror ("')' expected"); RECOVER;}
1294 | name DOT_TK SUPER_TK OP_TK argument_list error
1295 {yyerror ("')' expected"); RECOVER;}
1296 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1297 {yyerror ("';' expected"); RECOVER;}
1298 | name DOT_TK SUPER_TK OP_TK CP_TK error
1299 {yyerror ("';' expected"); RECOVER;}
1302 statement_expression:
1303 assignment
1304 | pre_increment_expression
1305 | pre_decrement_expression
1306 | post_increment_expression
1307 | post_decrement_expression
1308 | method_invocation
1309 | class_instance_creation_expression
1312 if_then_statement:
1313 IF_TK OP_TK expression CP_TK statement
1314 { $$ = build_if_else_statement ($2.location, $3, $5, NULL_TREE); }
1315 | IF_TK error
1316 {yyerror ("'(' expected"); RECOVER;}
1317 | IF_TK OP_TK error
1318 {yyerror ("Missing term"); RECOVER;}
1319 | IF_TK OP_TK expression error
1320 {yyerror ("')' expected"); RECOVER;}
1323 if_then_else_statement:
1324 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1325 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1328 if_then_else_statement_nsi:
1329 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1330 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1333 switch_statement:
1334 switch_expression
1336 enter_block ();
1338 switch_block
1340 /* Make into "proper list" of COMPOUND_EXPRs.
1341 I.e. make the last statment also have its own
1342 COMPOUND_EXPR. */
1343 maybe_absorb_scoping_blocks ();
1344 TREE_OPERAND ($1, 1) = exit_block ();
1345 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1349 switch_expression:
1350 SWITCH_TK OP_TK expression CP_TK
1352 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1353 EXPR_WFL_LINECOL ($$) = $2.location;
1355 | SWITCH_TK error
1356 {yyerror ("'(' expected"); RECOVER;}
1357 | SWITCH_TK OP_TK error
1358 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1359 | SWITCH_TK OP_TK expression CP_TK error
1360 {yyerror ("'{' expected"); RECOVER;}
1363 /* Default assignment is there to avoid type node on switch_block
1364 node. */
1366 switch_block:
1367 OCB_TK CCB_TK
1368 { $$ = NULL_TREE; }
1369 | OCB_TK switch_labels CCB_TK
1370 { $$ = NULL_TREE; }
1371 | OCB_TK switch_block_statement_groups CCB_TK
1372 { $$ = NULL_TREE; }
1373 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1374 { $$ = NULL_TREE; }
1377 switch_block_statement_groups:
1378 switch_block_statement_group
1379 | switch_block_statement_groups switch_block_statement_group
1382 switch_block_statement_group:
1383 switch_labels block_statements
1386 switch_labels:
1387 switch_label
1388 | switch_labels switch_label
1391 switch_label:
1392 CASE_TK constant_expression REL_CL_TK
1394 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1395 EXPR_WFL_LINECOL (lab) = $1.location;
1396 java_method_add_stmt (current_function_decl, lab);
1398 | DEFAULT_TK REL_CL_TK
1400 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1401 EXPR_WFL_LINECOL (lab) = $1.location;
1402 java_method_add_stmt (current_function_decl, lab);
1404 | CASE_TK error
1405 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1406 | CASE_TK constant_expression error
1407 {yyerror ("':' expected"); RECOVER;}
1408 | DEFAULT_TK error
1409 {yyerror ("':' expected"); RECOVER;}
1412 while_expression:
1413 WHILE_TK OP_TK expression CP_TK
1415 tree body = build_loop_body ($2.location, $3, 0);
1416 $$ = build_new_loop (body);
1420 while_statement:
1421 while_expression statement
1422 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1423 | WHILE_TK error
1424 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1425 | WHILE_TK OP_TK error
1426 {yyerror ("Missing term and ')' expected"); RECOVER;}
1427 | WHILE_TK OP_TK expression error
1428 {yyerror ("')' expected"); RECOVER;}
1431 while_statement_nsi:
1432 while_expression statement_nsi
1433 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1436 do_statement_begin:
1437 DO_TK
1439 tree body = build_loop_body (0, NULL_TREE, 1);
1440 $$ = build_new_loop (body);
1442 /* Need error handing here. FIXME */
1445 do_statement:
1446 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1447 { $$ = complete_loop_body ($4.location, $5, $2, 1); }
1450 for_statement:
1451 for_begin SC_TK expression SC_TK for_update CP_TK statement
1452 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1453 | for_begin SC_TK SC_TK for_update CP_TK statement
1455 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1456 /* We have not condition, so we get rid of the EXIT_EXPR */
1457 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1458 empty_stmt_node;
1460 | for_begin SC_TK error
1461 {yyerror ("Invalid control expression"); RECOVER;}
1462 | for_begin SC_TK expression SC_TK error
1463 {yyerror ("Invalid update expression"); RECOVER;}
1464 | for_begin SC_TK SC_TK error
1465 {yyerror ("Invalid update expression"); RECOVER;}
1468 for_statement_nsi:
1469 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1470 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1471 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1473 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1474 /* We have not condition, so we get rid of the EXIT_EXPR */
1475 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1476 empty_stmt_node;
1480 for_header:
1481 FOR_TK OP_TK
1483 /* This scope defined for local variable that may be
1484 defined within the scope of the for loop */
1485 enter_block ();
1487 | FOR_TK error
1488 {yyerror ("'(' expected"); DRECOVER(for_1);}
1489 | FOR_TK OP_TK error
1490 {yyerror ("Invalid init statement"); RECOVER;}
1493 for_begin:
1494 for_header for_init
1496 /* We now declare the loop body. The loop is
1497 declared as a for loop. */
1498 tree body = build_loop_body (0, NULL_TREE, 0);
1499 $$ = build_new_loop (body);
1500 IS_FOR_LOOP_P ($$) = 1;
1501 /* The loop is added to the current block the for
1502 statement is defined within */
1503 java_method_add_stmt (current_function_decl, $$);
1506 for_init: /* Can be empty */
1507 { $$ = empty_stmt_node; }
1508 | statement_expression_list
1510 /* Init statement recorded within the previously
1511 defined block scope */
1512 $$ = java_method_add_stmt (current_function_decl, $1);
1514 | local_variable_declaration
1516 /* Local variable are recorded within the previously
1517 defined block scope */
1518 $$ = NULL_TREE;
1520 | statement_expression_list error
1521 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1524 for_update: /* Can be empty */
1525 {$$ = empty_stmt_node;}
1526 | statement_expression_list
1527 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1530 statement_expression_list:
1531 statement_expression
1532 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1533 | statement_expression_list C_TK statement_expression
1534 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1535 | statement_expression_list C_TK error
1536 {yyerror ("Missing term"); RECOVER;}
1539 break_statement:
1540 BREAK_TK SC_TK
1541 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1542 | BREAK_TK identifier SC_TK
1543 { $$ = build_bc_statement ($1.location, 1, $2); }
1544 | BREAK_TK error
1545 {yyerror ("Missing term"); RECOVER;}
1546 | BREAK_TK identifier error
1547 {yyerror ("';' expected"); RECOVER;}
1550 continue_statement:
1551 CONTINUE_TK SC_TK
1552 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1553 | CONTINUE_TK identifier SC_TK
1554 { $$ = build_bc_statement ($1.location, 0, $2); }
1555 | CONTINUE_TK error
1556 {yyerror ("Missing term"); RECOVER;}
1557 | CONTINUE_TK identifier error
1558 {yyerror ("';' expected"); RECOVER;}
1561 return_statement:
1562 RETURN_TK SC_TK
1563 { $$ = build_return ($1.location, NULL_TREE); }
1564 | RETURN_TK expression SC_TK
1565 { $$ = build_return ($1.location, $2); }
1566 | RETURN_TK error
1567 {yyerror ("Missing term"); RECOVER;}
1568 | RETURN_TK expression error
1569 {yyerror ("';' expected"); RECOVER;}
1572 throw_statement:
1573 THROW_TK expression SC_TK
1575 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1576 EXPR_WFL_LINECOL ($$) = $1.location;
1578 | THROW_TK error
1579 {yyerror ("Missing term"); RECOVER;}
1580 | THROW_TK expression error
1581 {yyerror ("';' expected"); RECOVER;}
1584 synchronized_statement:
1585 synchronized OP_TK expression CP_TK block
1587 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1588 EXPR_WFL_LINECOL ($$) =
1589 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1591 | synchronized OP_TK expression CP_TK error
1592 {yyerror ("'{' expected"); RECOVER;}
1593 | synchronized error
1594 {yyerror ("'(' expected"); RECOVER;}
1595 | synchronized OP_TK error CP_TK
1596 {yyerror ("Missing term"); RECOVER;}
1597 | synchronized OP_TK error
1598 {yyerror ("Missing term"); RECOVER;}
1601 synchronized:
1602 MODIFIER_TK
1604 if ((1 << $1) != ACC_SYNCHRONIZED)
1605 fatal ("synchronized was '%d' - yyparse", (1 << $1));
1609 try_statement:
1610 TRY_TK block catches
1611 { $$ = build_try_statement ($1.location, $2, $3, NULL_TREE); }
1612 | TRY_TK block finally
1613 { $$ = build_try_statement ($1.location, $2, NULL_TREE, $3); }
1614 | TRY_TK block catches finally
1615 { $$ = build_try_statement ($1.location, $2, $3, $4); }
1616 | TRY_TK error
1617 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1620 catches:
1621 catch_clause
1622 | catches catch_clause
1624 TREE_CHAIN ($2) = $1;
1625 $$ = $2;
1629 catch_clause:
1630 catch_clause_parameter block
1632 java_method_add_stmt (current_function_decl, $2);
1633 exit_block ();
1634 $$ = $1;
1637 catch_clause_parameter:
1638 CATCH_TK OP_TK formal_parameter CP_TK
1640 /* We add a block to define a scope for
1641 formal_parameter (CCBP). The formal parameter is
1642 declared initialized by the appropriate function
1643 call */
1644 tree ccpb = enter_block ();
1645 tree init = build_assignment (ASSIGN_TK, $2.location,
1646 TREE_PURPOSE ($3),
1647 soft_exceptioninfo_call_node);
1648 declare_local_variables (0, TREE_VALUE ($3),
1649 build_tree_list (TREE_PURPOSE ($3),
1650 init));
1651 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1652 EXPR_WFL_LINECOL ($$) = $1.location;
1654 | CATCH_TK error
1655 {yyerror ("'(' expected"); RECOVER;}
1656 | CATCH_TK OP_TK error
1657 {yyerror ("Missing term or ')' expected"); DRECOVER (2);}
1658 | CATCH_TK OP_TK error CP_TK /* That's for () */
1659 {yyerror ("')' expected"); DRECOVER (1);}
1662 finally:
1663 FINALLY_TK block
1665 $$ = build (FINALLY_EXPR, NULL_TREE,
1666 create_label_decl (generate_name ()), $2);
1668 | FINALLY_TK error
1669 {yyerror ("'{' expected"); RECOVER; }
1672 /* 19.12 Production from 15: Expressions */
1673 primary:
1674 primary_no_new_array
1675 | array_creation_expression
1678 primary_no_new_array:
1679 literal
1680 | THIS_TK
1681 { $$ = build_this ($1.location); }
1682 | OP_TK expression CP_TK
1683 {$$ = $2;}
1684 | class_instance_creation_expression
1685 | field_access
1686 | method_invocation
1687 | array_access
1688 /* type DOT_TK CLASS_TK doens't work. So we split the rule
1689 'type' into its components. Missing is something for array,
1690 which will complete the reference_type part. FIXME */
1691 | name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1692 { $$ = parse_jdk1_1_error ("named class literals"); }
1693 | primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1694 { $$ = build_class_ref ($1); }
1695 | VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1696 { $$ = build_class_ref (void_type_node); }
1697 /* Added, JDK1.1 inner classes. Documentation is wrong
1698 refering to a 'ClassName' (class_name) rule that doesn't
1699 exist. Used name instead. */
1700 | name DOT_TK THIS_TK
1701 { $$ = parse_jdk1_1_error ("class literals"); }
1702 | OP_TK expression error
1703 {yyerror ("')' expected"); RECOVER;}
1704 | name DOT_TK error
1705 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1706 | primitive_type DOT_TK error
1707 {yyerror ("'class' expected" ); RECOVER;}
1708 | VOID_TK DOT_TK error
1709 {yyerror ("'class' expected" ); RECOVER;}
1712 class_instance_creation_expression:
1713 NEW_TK class_type OP_TK argument_list CP_TK
1714 { $$ = build_new_invocation ($2, $4); }
1715 | NEW_TK class_type OP_TK CP_TK
1716 { $$ = build_new_invocation ($2, NULL_TREE); }
1717 /* Added, JDK1.1 inner classes but modified to use
1718 'class_type' instead of 'TypeName' (type_name) mentionned
1719 in the documentation but doesn't exist. */
1720 | NEW_TK class_type OP_TK argument_list CP_TK class_body
1721 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1722 | NEW_TK class_type OP_TK CP_TK class_body
1723 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1724 /* Added, JDK1.1 inner classes, modified to use name or
1725 primary instead of primary solely which couldn't work in
1726 all situations. */
1727 | something_dot_new identifier OP_TK CP_TK
1728 | something_dot_new identifier OP_TK CP_TK class_body
1729 | something_dot_new identifier OP_TK argument_list CP_TK
1730 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1731 | NEW_TK error SC_TK
1732 {yyerror ("'(' expected"); DRECOVER(new_1);}
1733 | NEW_TK class_type error
1734 {yyerror ("'(' expected"); RECOVER;}
1735 | NEW_TK class_type OP_TK error
1736 {yyerror ("')' or term expected"); RECOVER;}
1737 | NEW_TK class_type OP_TK argument_list error
1738 {yyerror ("')' expected"); RECOVER;}
1739 | something_dot_new error
1740 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1741 | something_dot_new identifier error
1742 {yyerror ("'(' expected"); RECOVER;}
1745 something_dot_new: /* Added, not part of the specs. */
1746 name DOT_TK NEW_TK
1747 | primary DOT_TK NEW_TK
1750 argument_list:
1751 expression
1753 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
1754 ctxp->formal_parameter_number = 1;
1756 | argument_list C_TK expression
1758 ctxp->formal_parameter_number += 1;
1759 $$ = tree_cons (NULL_TREE, $3, $1);
1761 | argument_list C_TK error
1762 {yyerror ("Missing term"); RECOVER;}
1765 array_creation_expression:
1766 NEW_TK primitive_type dim_exprs
1767 { $$ = build_newarray_node ($2, $3, 0); }
1768 | NEW_TK class_or_interface_type dim_exprs
1769 { $$ = build_newarray_node ($2, $3, 0); }
1770 | NEW_TK primitive_type dim_exprs dims
1771 { $$ = build_newarray_node ($2, $3, ctxp->osb_number); }
1772 | NEW_TK class_or_interface_type dim_exprs dims
1773 { $$ = build_newarray_node ($2, $3, ctxp->osb_number); }
1774 /* Added, JDK1.1 anonymous array. Initial documentation rule
1775 modified */
1776 | NEW_TK class_or_interface_type dims array_initializer
1777 { $$ = parse_jdk1_1_error ("anonymous array"); }
1778 | NEW_TK primitive_type dims array_initializer
1779 { $$ = parse_jdk1_1_error ("anonymous array"); }
1780 | NEW_TK error CSB_TK
1781 {yyerror ("'[' expected"); DRECOVER ("]");}
1782 | NEW_TK error OSB_TK
1783 {yyerror ("']' expected"); RECOVER;}
1786 dim_exprs:
1787 dim_expr
1788 { $$ = build_tree_list (NULL_TREE, $1); }
1789 | dim_exprs dim_expr
1790 { $$ = tree_cons (NULL_TREE, $2, $$); }
1793 dim_expr:
1794 OSB_TK expression CSB_TK
1796 EXPR_WFL_LINECOL ($2) = $1.location;
1797 $$ = $2;
1799 | OSB_TK expression error
1800 {yyerror ("']' expected"); RECOVER;}
1801 | OSB_TK error
1803 yyerror ("Missing term");
1804 yyerror ("']' expected");
1805 RECOVER;
1809 dims:
1810 OSB_TK CSB_TK
1811 { ctxp->osb_number = 1; }
1812 | dims OSB_TK CSB_TK
1813 { ctxp->osb_number++; }
1814 | dims OSB_TK error
1815 { yyerror ("']' expected"); RECOVER;}
1818 field_access:
1819 primary DOT_TK identifier
1820 { $$ = make_qualified_primary ($1, $3, $2.location); }
1821 /* FIXME - REWRITE TO:
1822 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
1823 | SUPER_TK DOT_TK identifier
1825 tree super_wfl =
1826 build_wfl_node (super_identifier_node,
1827 input_filename, 0, 0);
1828 EXPR_WFL_LINECOL (super_wfl) = $1.location;
1829 $$ = make_qualified_name (super_wfl, $3, $2.location);
1831 | SUPER_TK error
1832 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
1835 method_invocation:
1836 name OP_TK CP_TK
1837 { $$ = build_method_invocation ($1, NULL_TREE); }
1838 | name OP_TK argument_list CP_TK
1839 { $$ = build_method_invocation ($1, $3); }
1840 | primary DOT_TK identifier OP_TK CP_TK
1842 if (TREE_CODE ($1) == THIS_EXPR)
1843 $$ = build_this_super_qualified_invocation
1844 (1, $3, NULL_TREE, 0, $2.location);
1845 else
1847 tree invok = build_method_invocation ($3, NULL_TREE);
1848 $$ = make_qualified_primary ($1, invok, $2.location);
1851 | primary DOT_TK identifier OP_TK argument_list CP_TK
1853 if (TREE_CODE ($1) == THIS_EXPR)
1854 $$ = build_this_super_qualified_invocation
1855 (1, $3, $5, 0, $2.location);
1856 else
1858 tree invok = build_method_invocation ($3, $5);
1859 $$ = make_qualified_primary ($1, invok, $2.location);
1862 | SUPER_TK DOT_TK identifier OP_TK CP_TK
1864 $$ = build_this_super_qualified_invocation
1865 (0, $3, NULL_TREE, $1.location, $2.location);
1867 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
1869 $$ = build_this_super_qualified_invocation
1870 (0, $3, $5, $1.location, $2.location);
1872 /* Screws up thing. I let it here until I'm convinced it can
1873 be removed. FIXME
1874 | primary DOT_TK error
1875 {yyerror ("'(' expected"); DRECOVER(bad);} */
1876 | SUPER_TK DOT_TK error CP_TK
1877 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1878 | SUPER_TK DOT_TK error DOT_TK
1879 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1882 array_access:
1883 name OSB_TK expression CSB_TK
1884 { $$ = build_array_ref ($2.location, $1, $3); }
1885 | primary_no_new_array OSB_TK expression CSB_TK
1886 { $$ = build_array_ref ($2.location, $1, $3); }
1887 | name OSB_TK error
1889 yyerror ("Missing term and ']' expected");
1890 DRECOVER(array_access);
1892 | name OSB_TK expression error
1894 yyerror ("']' expected");
1895 DRECOVER(array_access);
1897 | primary_no_new_array OSB_TK error
1899 yyerror ("Missing term and ']' expected");
1900 DRECOVER(array_access);
1902 | primary_no_new_array OSB_TK expression error
1904 yyerror ("']' expected");
1905 DRECOVER(array_access);
1909 postfix_expression:
1910 primary
1911 | name
1912 | post_increment_expression
1913 | post_decrement_expression
1916 post_increment_expression:
1917 postfix_expression INCR_TK
1918 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1921 post_decrement_expression:
1922 postfix_expression DECR_TK
1923 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1926 unary_expression:
1927 pre_increment_expression
1928 | pre_decrement_expression
1929 | PLUS_TK unary_expression
1930 {$$ = build_unaryop ($1.token, $1.location, $2); }
1931 | MINUS_TK unary_expression
1932 {$$ = build_unaryop ($1.token, $1.location, $2); }
1933 | unary_expression_not_plus_minus
1934 | PLUS_TK error
1935 {yyerror ("Missing term"); RECOVER}
1936 | MINUS_TK error
1937 {yyerror ("Missing term"); RECOVER}
1940 pre_increment_expression:
1941 INCR_TK unary_expression
1942 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1943 | INCR_TK error
1944 {yyerror ("Missing term"); RECOVER}
1947 pre_decrement_expression:
1948 DECR_TK unary_expression
1949 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1950 | DECR_TK error
1951 {yyerror ("Missing term"); RECOVER}
1954 unary_expression_not_plus_minus:
1955 postfix_expression
1956 | NOT_TK unary_expression
1957 {$$ = build_unaryop ($1.token, $1.location, $2); }
1958 | NEG_TK unary_expression
1959 {$$ = build_unaryop ($1.token, $1.location, $2); }
1960 | cast_expression
1961 | NOT_TK error
1962 {yyerror ("Missing term"); RECOVER}
1963 | NEG_TK error
1964 {yyerror ("Missing term"); RECOVER}
1967 cast_expression: /* Error handling here is potentially weak */
1968 OP_TK primitive_type dims CP_TK unary_expression
1970 tree type = $2;
1971 while (ctxp->osb_number--)
1972 type = build_java_array_type (type, -1);
1973 $$ = build_cast ($1.location, type, $5);
1975 | OP_TK primitive_type CP_TK unary_expression
1976 { $$ = build_cast ($1.location, $2, $4); }
1977 | OP_TK expression CP_TK unary_expression_not_plus_minus
1978 { $$ = build_cast ($1.location, $2, $4); }
1979 | OP_TK name dims CP_TK unary_expression_not_plus_minus
1981 char *ptr;
1982 while (ctxp->osb_number--)
1983 obstack_1grow (&temporary_obstack, '[');
1984 obstack_grow0 (&temporary_obstack,
1985 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
1986 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
1987 ptr = obstack_finish (&temporary_obstack);
1988 EXPR_WFL_NODE ($2) = get_identifier (ptr);
1989 $$ = build_cast ($1.location, $2, $5);
1991 | OP_TK primitive_type OSB_TK error
1992 {yyerror ("']' expected, invalid type expression");}
1993 | OP_TK error
1995 if (ctxp->prevent_ese != lineno)
1996 yyerror ("Invalid type expression"); RECOVER;
1997 RECOVER;
1999 | OP_TK primitive_type dims CP_TK error
2000 {yyerror ("Missing term"); RECOVER;}
2001 | OP_TK primitive_type CP_TK error
2002 {yyerror ("Missing term"); RECOVER;}
2003 | OP_TK name dims CP_TK error
2004 {yyerror ("Missing term"); RECOVER;}
2007 multiplicative_expression:
2008 unary_expression
2009 | multiplicative_expression MULT_TK unary_expression
2011 $$ = build_binop (BINOP_LOOKUP ($2.token),
2012 $2.location, $1, $3);
2014 | multiplicative_expression DIV_TK unary_expression
2016 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2017 $1, $3);
2019 | multiplicative_expression REM_TK unary_expression
2021 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2022 $1, $3);
2024 | multiplicative_expression MULT_TK error
2025 {yyerror ("Missing term"); RECOVER;}
2026 | multiplicative_expression DIV_TK error
2027 {yyerror ("Missing term"); RECOVER;}
2028 | multiplicative_expression REM_TK error
2029 {yyerror ("Missing term"); RECOVER;}
2032 additive_expression:
2033 multiplicative_expression
2034 | additive_expression PLUS_TK multiplicative_expression
2036 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2037 $1, $3);
2039 | additive_expression MINUS_TK multiplicative_expression
2041 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2042 $1, $3);
2044 | additive_expression PLUS_TK error
2045 {yyerror ("Missing term"); RECOVER;}
2046 | additive_expression MINUS_TK error
2047 {yyerror ("Missing term"); RECOVER;}
2050 shift_expression:
2051 additive_expression
2052 | shift_expression LS_TK additive_expression
2054 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2055 $1, $3);
2057 | shift_expression SRS_TK additive_expression
2059 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2060 $1, $3);
2062 | shift_expression ZRS_TK additive_expression
2064 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2065 $1, $3);
2067 | shift_expression LS_TK error
2068 {yyerror ("Missing term"); RECOVER;}
2069 | shift_expression SRS_TK error
2070 {yyerror ("Missing term"); RECOVER;}
2071 | shift_expression ZRS_TK error
2072 {yyerror ("Missing term"); RECOVER;}
2075 relational_expression:
2076 shift_expression
2077 | relational_expression LT_TK shift_expression
2079 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2080 $1, $3);
2082 | relational_expression GT_TK shift_expression
2084 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2085 $1, $3);
2087 | relational_expression LTE_TK shift_expression
2089 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2090 $1, $3);
2092 | relational_expression GTE_TK shift_expression
2094 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2095 $1, $3);
2097 | relational_expression INSTANCEOF_TK reference_type
2098 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2099 | relational_expression LT_TK error
2100 {yyerror ("Missing term"); RECOVER;}
2101 | relational_expression GT_TK error
2102 {yyerror ("Missing term"); RECOVER;}
2103 | relational_expression LTE_TK error
2104 {yyerror ("Missing term"); RECOVER;}
2105 | relational_expression GTE_TK error
2106 {yyerror ("Missing term"); RECOVER;}
2107 | relational_expression INSTANCEOF_TK error
2108 {yyerror ("Invalid reference type"); RECOVER;}
2111 equality_expression:
2112 relational_expression
2113 | equality_expression EQ_TK relational_expression
2115 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2116 $1, $3);
2118 | equality_expression NEQ_TK relational_expression
2120 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2121 $1, $3);
2123 | equality_expression EQ_TK error
2124 {yyerror ("Missing term"); RECOVER;}
2125 | equality_expression NEQ_TK error
2126 {yyerror ("Missing term"); RECOVER;}
2129 and_expression:
2130 equality_expression
2131 | and_expression AND_TK equality_expression
2133 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2134 $1, $3);
2136 | and_expression AND_TK error
2137 {yyerror ("Missing term"); RECOVER;}
2140 exclusive_or_expression:
2141 and_expression
2142 | exclusive_or_expression XOR_TK and_expression
2144 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2145 $1, $3);
2147 | exclusive_or_expression XOR_TK error
2148 {yyerror ("Missing term"); RECOVER;}
2151 inclusive_or_expression:
2152 exclusive_or_expression
2153 | inclusive_or_expression OR_TK exclusive_or_expression
2155 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2156 $1, $3);
2158 | inclusive_or_expression OR_TK error
2159 {yyerror ("Missing term"); RECOVER;}
2162 conditional_and_expression:
2163 inclusive_or_expression
2164 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2166 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2167 $1, $3);
2169 | conditional_and_expression BOOL_AND_TK error
2170 {yyerror ("Missing term"); RECOVER;}
2173 conditional_or_expression:
2174 conditional_and_expression
2175 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2177 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2178 $1, $3);
2180 | conditional_or_expression BOOL_OR_TK error
2181 {yyerror ("Missing term"); RECOVER;}
2184 conditional_expression: /* Error handling here is weak */
2185 conditional_or_expression
2186 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2188 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2189 EXPR_WFL_LINECOL ($$) = $2.location;
2191 | conditional_or_expression REL_QM_TK REL_CL_TK error
2193 YYERROR_NOW;
2194 yyerror ("Missing term");
2195 DRECOVER (1);
2197 | conditional_or_expression REL_QM_TK error
2198 {yyerror ("Missing term"); DRECOVER (2);}
2199 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2200 {yyerror ("Missing term"); DRECOVER (3);}
2203 assignment_expression:
2204 conditional_expression
2205 | assignment
2208 assignment:
2209 left_hand_side assignment_operator assignment_expression
2210 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2211 | left_hand_side assignment_operator error
2213 if (ctxp->prevent_ese != lineno)
2214 yyerror ("Missing term");
2215 DRECOVER (assign);
2219 left_hand_side:
2220 name
2221 | field_access
2222 | array_access
2225 assignment_operator:
2226 ASSIGN_ANY_TK
2227 | ASSIGN_TK
2230 expression:
2231 assignment_expression
2234 constant_expression:
2235 expression
2241 #include "lex.c"
2243 /* Flag for the error report routine to issue the error the first time
2244 it's called (overriding the default behavior which is to drop the
2245 first invocation and honor the second one, taking advantage of a
2246 richer context. */
2247 static int force_error = 0;
2249 /* Create a new parser context and make it the current one. */
2251 void
2252 java_push_parser_context ()
2254 struct parser_ctxt *new =
2255 (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2257 bzero (new, sizeof (struct parser_ctxt));
2258 new->next = ctxp;
2259 ctxp = new;
2260 if (ctxp->next)
2262 ctxp->incomplete_class = ctxp->next->incomplete_class;
2263 ctxp->gclass_list = ctxp->next->gclass_list;
2267 /* If the first file of a file list was a class file, no context
2268 exists for a source file to be parsed. This boolean remembers that
2269 java_parser_context_save_global might have created a dummy one, so
2270 that java_parser_context_restore_global can pop it. */
2271 static int extra_ctxp_pushed_p = 0;
2273 void
2274 java_parser_context_save_global ()
2276 if (!ctxp)
2278 java_push_parser_context ();
2279 extra_ctxp_pushed_p = 1;
2281 ctxp->finput = finput;
2282 ctxp->lineno = lineno;
2283 ctxp->current_class = current_class;
2284 ctxp->filename = input_filename;
2285 ctxp->current_function_decl = current_function_decl;
2288 void
2289 java_parser_context_restore_global ()
2291 finput = ctxp->finput;
2292 lineno = ctxp->lineno;
2293 current_class = ctxp->current_class;
2294 input_filename = ctxp->filename;
2295 current_function_decl = ctxp->current_function_decl;
2296 if (!ctxp->next && extra_ctxp_pushed_p)
2298 java_pop_parser_context (0);
2299 extra_ctxp_pushed_p = 0;
2303 void
2304 java_pop_parser_context (generate)
2305 int generate;
2307 tree current;
2308 struct parser_ctxt *toFree, *next;
2310 if (!ctxp)
2311 return;
2313 toFree = ctxp;
2314 next = ctxp->next;
2315 if (next)
2317 next->incomplete_class = ctxp->incomplete_class;
2318 next->gclass_list = ctxp->gclass_list;
2319 lineno = ctxp->lineno;
2320 finput = ctxp->finput;
2321 current_class = ctxp->current_class;
2324 /* Set the single import class file flag to 0 for the current list
2325 of imported things */
2326 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2327 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2329 /* And restore those of the previous context */
2330 if ((ctxp = next)) /* Assignment is really meant here */
2331 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2332 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2334 if (generate)
2336 toFree->next = ctxp_for_generation;
2337 ctxp_for_generation = toFree;
2339 else
2340 free (toFree);
2343 /* Reporting JDK1.1 features not implemented */
2345 static tree
2346 parse_jdk1_1_error (msg)
2347 char *msg;
2349 sorry (": `%s' JDK1.1(TM) feature", msg);
2350 java_error_count++;
2351 return empty_stmt_node;
2354 static int do_warning = 0;
2356 void
2357 yyerror (msg)
2358 char *msg;
2360 static java_lc elc;
2361 static int prev_lineno;
2362 static char *prev_msg;
2364 int save_lineno;
2365 char *remainder, *code_from_source;
2366 extern struct obstack temporary_obstack;
2368 if (!force_error && prev_lineno == lineno)
2369 return;
2371 /* Save current error location but report latter, when the context is
2372 richer. */
2373 if (ctxp->java_error_flag == 0)
2375 ctxp->java_error_flag = 1;
2376 elc = ctxp->elc;
2377 /* Do something to use the previous line if we're reaching the
2378 end of the file... */
2379 #ifdef VERBOSE_SKELETON
2380 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2381 #endif
2382 return;
2385 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2386 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2387 return;
2389 ctxp->java_error_flag = 0;
2390 if (do_warning)
2391 java_warning_count++;
2392 else
2393 java_error_count++;
2395 if (elc.col == 0 && msg[1] == ';')
2397 elc.col = ctxp->p_line->char_col-1;
2398 elc.line = ctxp->p_line->lineno;
2401 save_lineno = lineno;
2402 prev_lineno = lineno = elc.line;
2403 prev_msg = msg;
2405 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2406 obstack_grow0 (&temporary_obstack,
2407 code_from_source, strlen (code_from_source));
2408 remainder = obstack_finish (&temporary_obstack);
2409 if (do_warning)
2410 warning ("%s.\n%s", msg, remainder);
2411 else
2412 error ("%s.\n%s", msg, remainder);
2414 /* This allow us to cheaply avoid an extra 'Invalid expression
2415 statement' error report when errors have been already reported on
2416 the same line. This occurs when we report an error but don't have
2417 a synchronization point other than ';', which
2418 expression_statement is the only one to take care of. */
2419 ctxp->prevent_ese = lineno = save_lineno;
2422 static void
2423 issue_warning_error_from_context (cl, msg, ap)
2424 tree cl;
2425 char *msg;
2426 va_list ap;
2428 char *saved, *saved_input_filename;
2429 char buffer [4096];
2430 vsprintf (buffer, msg, ap);
2431 force_error = 1;
2433 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2434 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2435 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
2437 /* We have a CL, that's a good reason for using it if it contains data */
2438 saved = ctxp->filename;
2439 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2440 ctxp->filename = EXPR_WFL_FILENAME (cl);
2441 saved_input_filename = input_filename;
2442 input_filename = ctxp->filename;
2443 java_error (NULL);
2444 java_error (buffer);
2445 ctxp->filename = saved;
2446 input_filename = saved_input_filename;
2447 force_error = 0;
2450 /* Issue an error message at a current source line CL */
2452 void
2453 parse_error_context VPROTO ((tree cl, char *msg, ...))
2455 #ifndef __STDC__
2456 tree cl;
2457 char *msg;
2458 #endif
2459 va_list ap;
2461 VA_START (ap, msg);
2462 #ifndef __STDC__
2463 cl = va_arg (ap, tree);
2464 msg = va_arg (ap, char *);
2465 #endif
2466 issue_warning_error_from_context (cl, msg, ap);
2467 va_end (ap);
2470 /* Issue a warning at a current source line CL */
2472 static void
2473 parse_warning_context VPROTO ((tree cl, char *msg, ...))
2475 #ifndef __STDC__
2476 tree cl;
2477 char *msg;
2478 #endif
2479 va_list ap;
2481 VA_START (ap, msg);
2482 #ifndef __STDC__
2483 cl = va_arg (ap, tree);
2484 msg = va_arg (ap, char *);
2485 #endif
2487 force_error = do_warning = 1;
2488 issue_warning_error_from_context (cl, msg, ap);
2489 do_warning = force_error = 0;
2490 va_end (ap);
2493 static tree
2494 find_expr_with_wfl (node)
2495 tree node;
2497 while (node)
2499 char code;
2500 tree to_return;
2502 switch (TREE_CODE (node))
2504 case BLOCK:
2505 return find_expr_with_wfl (BLOCK_EXPR_BODY (node));
2507 case COMPOUND_EXPR:
2508 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
2509 if (to_return)
2510 return to_return;
2511 to_return = find_expr_with_wfl (TREE_OPERAND (node, 1));
2512 return to_return;
2514 case LOOP_EXPR:
2515 return find_expr_with_wfl (TREE_OPERAND (node, 0));
2517 case LABELED_BLOCK_EXPR:
2518 return find_expr_with_wfl (TREE_OPERAND (node, 1));
2519 default:
2520 code = TREE_CODE_CLASS (TREE_CODE (node));
2521 if (((code == '1') || (code == '2') || (code == 'e'))
2522 && EXPR_WFL_LINECOL (node))
2523 return node;
2526 return NULL_TREE;
2529 /* Issue a missing return statement error. Uses METHOD to figure the
2530 last line of the method the error occurs in. */
2532 static void
2533 missing_return_error (method)
2534 tree method;
2536 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
2537 parse_error_context (wfl_operator, "Missing return statement");
2540 /* Issue an unreachable statement error. From NODE, find the next
2541 statement to report appropriately. */
2542 static void
2543 unreachable_stmt_error (node)
2544 tree node;
2546 /* Browse node to find the next expression node that has a WFL. Use
2547 the location to report the error */
2548 if (TREE_CODE (node) == COMPOUND_EXPR)
2549 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
2550 else
2551 node = find_expr_with_wfl (node);
2553 if (node)
2555 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
2556 parse_error_context (wfl_operator, "Unreachable statement");
2558 else
2559 fatal ("Can't get valid statement - unreachable_stmt_error");
2563 java_report_errors ()
2565 if (java_error_count)
2566 fprintf (stderr, "%d error%s",
2567 java_error_count, (java_error_count == 1 ? "" : "s"));
2568 if (java_warning_count)
2569 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
2570 java_warning_count, (java_warning_count == 1 ? "" : "s"));
2571 if (java_error_count || java_warning_count)
2572 putc ('\n', stderr);
2573 return java_error_count;
2576 static char *
2577 java_accstring_lookup (flags)
2578 int flags;
2580 static char buffer [80];
2581 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
2583 /* Access modifier looked-up first for easier report on forbidden
2584 access. */
2585 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
2586 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
2587 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
2588 if (flags & ACC_STATIC) COPY_RETURN ("static");
2589 if (flags & ACC_FINAL) COPY_RETURN ("final");
2590 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
2591 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
2592 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
2593 if (flags & ACC_NATIVE) COPY_RETURN ("native");
2594 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
2595 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
2597 buffer [0] = '\0';
2598 return buffer;
2599 #undef COPY_RETURN
2602 /* Issuing error messages upon redefinition of classes, interfaces or
2603 variables. */
2605 static void
2606 classitf_redefinition_error (context, id, decl, cl)
2607 char *context;
2608 tree id, decl, cl;
2610 parse_error_context (cl, "%s `%s' already defined in %s:%d",
2611 context, IDENTIFIER_POINTER (id),
2612 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
2613 /* Here we should point out where its redefined. It's a unicode. FIXME */
2616 static void
2617 variable_redefinition_error (context, name, type, line)
2618 tree context, name, type;
2619 int line;
2621 char *type_name;
2623 /* Figure a proper name for type. We might haven't resolved it */
2624 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
2625 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
2626 else
2627 type_name = lang_printable_name (type, 0);
2629 parse_error_context (context,
2630 "Variable `%s' is already defined in this method and "
2631 "was declared `%s %s' at line %d",
2632 IDENTIFIER_POINTER (name),
2633 type_name, IDENTIFIER_POINTER (name), line);
2636 static tree
2637 build_array_from_name (type, type_wfl, name, ret_name)
2638 tree type, type_wfl, name, *ret_name;
2640 int more_dims = 0;
2641 char *string;
2643 /* Eventually get more dims */
2644 string = IDENTIFIER_POINTER (name);
2645 while (string [more_dims] == '[')
2646 more_dims++;
2648 /* If we have, then craft a new type for this variable */
2649 if (more_dims)
2651 name = get_identifier (&more_dims [string]);
2653 /* If type already is a reference on an array, get the base type */
2654 if ((TREE_CODE (type) == POINTER_TYPE) &&
2655 TYPE_ARRAY_P (TREE_TYPE (type)))
2656 type = TREE_TYPE (type);
2658 /* Building the first dimension of a primitive type uses this
2659 function */
2660 if (JPRIMITIVE_TYPE_P (type))
2662 type = build_java_array_type (type, -1);
2663 CLASS_LOADED_P (type) = 1;
2664 more_dims--;
2666 /* Otherwise, if we have a WFL for this type, use it (the type
2667 is already an array on an unresolved type, and we just keep
2668 on adding dimensions) */
2669 else if (type_wfl)
2670 type = type_wfl;
2672 /* Add all the dimensions */
2673 while (more_dims--)
2674 type = build_unresolved_array_type (type);
2676 /* The type may have been incomplete in the first place */
2677 if (type_wfl)
2678 type = obtain_incomplete_type (type);
2681 *ret_name = name;
2682 return type;
2685 /* Build something that the type identifier resolver will identify as
2686 being an array to an unresolved type. TYPE_WFL is a WFL on a
2687 identifier. */
2689 static tree
2690 build_unresolved_array_type (type_or_wfl)
2691 tree type_or_wfl;
2693 char *ptr;
2695 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
2696 just create a array type */
2697 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
2699 tree type = build_java_array_type (type_or_wfl, -1);
2700 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
2701 return type;
2704 obstack_1grow (&temporary_obstack, '[');
2705 obstack_grow0 (&temporary_obstack,
2706 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
2707 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
2708 ptr = obstack_finish (&temporary_obstack);
2709 return build_expr_wfl (get_identifier (ptr),
2710 EXPR_WFL_FILENAME (type_or_wfl),
2711 EXPR_WFL_LINENO (type_or_wfl),
2712 EXPR_WFL_COLNO (type_or_wfl));
2715 /* Check modifiers. If one doesn't fit, retrieve it in its declaration line
2716 and point it out. */
2718 static void
2719 check_modifiers (message, value, mask)
2720 char *message;
2721 int value;
2722 int mask;
2724 /* Should point out the one that don't fit. ASCII/unicode,
2725 going backward. FIXME */
2726 if (value & ~mask)
2728 int i, remainder = value & ~mask;
2729 for (i = 0; i <= 10; i++)
2730 if ((1 << i) & remainder)
2731 parse_error_context (ctxp->modifier_ctx [i], message,
2732 java_accstring_lookup (1 << i));
2736 static void
2737 parser_add_interface (class_decl, interface_decl, wfl)
2738 tree class_decl, interface_decl, wfl;
2740 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
2741 parse_error_context (wfl, "Interface `%s' repeated",
2742 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
2745 /* Bulk of common class/interface checks. Return 1 if an error was
2746 encountered. TAG is 0 for a class, 1 for an interface. */
2748 static int
2749 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
2750 int is_interface, flags;
2751 tree raw_name, qualified_name, decl, cl;
2753 tree node;
2755 if (!quiet_flag)
2756 fprintf (stderr, " %s %s", (is_interface ? "interface" : "class"),
2757 IDENTIFIER_POINTER (qualified_name));
2759 /* Scope of an interface/class type name:
2760 - Can't be imported by a single type import
2761 - Can't already exists in the package */
2762 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
2763 && (node = find_name_in_single_imports (raw_name)))
2765 parse_error_context
2766 (cl, "%s name `%s' clashes with imported type `%s'",
2767 (is_interface ? "Interface" : "Class"),
2768 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
2769 return 1;
2771 if (decl && CLASS_COMPLETE_P (decl))
2773 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
2774 qualified_name, decl, cl);
2775 return 1;
2778 /* If public, file name should match class/interface name */
2779 if (flags & ACC_PUBLIC)
2781 char *f;
2783 /* Contains OS dependent assumption on path separator. FIXME */
2784 for (f = &input_filename [strlen (input_filename)];
2785 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
2786 f--)
2788 if (f[0] == '/')
2789 f++;
2790 if (strncmp (IDENTIFIER_POINTER (raw_name),
2791 f , IDENTIFIER_LENGTH (raw_name)) ||
2792 f [IDENTIFIER_LENGTH (raw_name)] != '.')
2793 parse_error_context (cl, "Public %s `%s' must be defined in a file "
2794 "called `%s.java'",
2795 (is_interface ? "interface" : "class"),
2796 IDENTIFIER_POINTER (qualified_name),
2797 IDENTIFIER_POINTER (raw_name));
2800 check_modifiers ((is_interface ?
2801 "Illegal modifier `%s' for interface declaration" :
2802 "Illegal modifier `%s' for class declaration"), flags,
2803 (is_interface ? INTERFACE_MODIFIERS : CLASS_MODIFIERS));
2804 return 0;
2807 /* If DECL is NULL, create and push a new DECL, record the current
2808 line CL and do other maintenance things. */
2810 static tree
2811 maybe_create_class_interface_decl (decl, qualified_name, cl)
2812 tree decl, qualified_name, cl;
2814 if (!decl)
2815 decl = push_class (make_class (), qualified_name);
2817 /* Take care of the file and line business */
2818 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
2819 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
2820 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
2821 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
2822 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
2824 ctxp->current_parsed_class = decl;
2826 /* Link the declaration to the already seen ones */
2827 TREE_CHAIN (decl) = ctxp->class_list;
2828 ctxp->class_list = decl;
2830 /* Create a new nodes in the global lists */
2831 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
2832 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
2834 /* Install a new dependency list element */
2835 create_jdep_list (ctxp);
2837 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
2838 IDENTIFIER_POINTER (qualified_name)));
2839 return decl;
2842 static void
2843 add_superinterfaces (decl, interface_list)
2844 tree decl, interface_list;
2846 tree node;
2847 /* Superinterface(s): if present and defined, parser_check_super_interface ()
2848 takes care of ensuring that:
2849 - This is an accessible interface type,
2850 - Circularity detection.
2851 parser_add_interface is then called. If present but not defined,
2852 the check operation is delayed until the super interface gets
2853 defined. */
2854 for (node = interface_list; node; node = TREE_CHAIN (node))
2856 tree current = TREE_PURPOSE (node);
2857 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
2858 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
2860 if (!parser_check_super_interface (idecl, decl, current))
2861 parser_add_interface (decl, idecl, current);
2863 else
2864 register_incomplete_type (JDEP_INTERFACE,
2865 current, decl, NULL_TREE);
2869 /* Create an interface in pass1 and return its decl. Return the
2870 interface's decl in pass 2. */
2872 static tree
2873 create_interface (flags, id, super)
2874 int flags;
2875 tree id, super;
2877 tree raw_name = EXPR_WFL_NODE (id);
2878 tree q_name = parser_qualified_classname (id);
2879 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
2881 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
2883 /* Basic checks: scope, redefinition, modifiers */
2884 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
2885 return NULL_TREE;
2887 /* Interface modifiers check
2888 - public/abstract allowed (already done at that point)
2889 - abstract is obsolete (comes first, it's a warning, or should be)
2890 - Can't use twice the same (checked in the modifier rule) */
2891 if ((flags & ACC_ABSTRACT) && flag_redundant)
2892 parse_warning_context
2893 (MODIFIER_WFL (ABSTRACT_TK),
2894 "Redundant use of `abstract' modifier. Interface `%s' is implicitely "
2895 "abstract", IDENTIFIER_POINTER (raw_name));
2897 /* Create a new decl if DECL is NULL, otherwise fix it */
2898 decl = maybe_create_class_interface_decl (decl, q_name, id);
2900 /* Set super info and mark the class a complete */
2901 set_super_info (ACC_ABSTRACT | ACC_INTERFACE | flags, TREE_TYPE (decl),
2902 object_type_node, ctxp->interface_number);
2903 ctxp->interface_number = 0;
2904 CLASS_COMPLETE_P (decl) = 1;
2905 add_superinterfaces (decl, super);
2907 return decl;
2910 /* Create an class in pass1 and return its decl. Return class
2911 interface's decl in pass 2. */
2913 static tree
2914 create_class (flags, id, super, interfaces)
2915 int flags;
2916 tree id, super, interfaces;
2918 tree raw_name = EXPR_WFL_NODE (id);
2919 tree class_id, decl;
2920 tree super_decl = NULL, super_decl_type;
2922 class_id = parser_qualified_classname (id);
2923 decl = IDENTIFIER_CLASS_VALUE (class_id);
2924 ctxp->current_parsed_class_un = EXPR_WFL_NODE (id);
2925 EXPR_WFL_NODE (id) = class_id;
2927 /* Basic check: scope, redefinition, modifiers */
2928 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
2929 return NULL_TREE;
2931 /* Class modifier check:
2932 - Allowed modifier (already done at that point)
2933 - abstract AND final forbidden
2934 - Public classes defined in the correct file */
2935 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
2936 parse_error_context (id, "Class `%s' can't be declared both abstract "
2937 "and final", IDENTIFIER_POINTER (raw_name));
2939 /* Create a new decl if DECL is NULL, otherwise fix it */
2940 decl = maybe_create_class_interface_decl (decl, class_id, id);
2942 /* If SUPER exists, use it, otherwise use Object */
2943 if (super)
2945 /* Can't extend java.lang.Object */
2946 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
2948 parse_error_context (id, "Can't extend `java.lang.Object'");
2949 return NULL_TREE;
2952 /* The class is known and exists if there is a decl. Otherwise,
2953 postpone the operation and do it later. */
2954 super_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (super));
2955 if (super_decl)
2957 parser_check_super (super_decl, decl, id);
2958 super_decl_type = TREE_TYPE (super_decl);
2960 else
2961 super_decl_type =
2962 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
2964 else if (TREE_TYPE (decl) != object_type_node)
2965 super_decl_type = object_type_node;
2966 /* We're defining java.lang.Object */
2967 else
2968 super_decl_type = NULL_TREE;
2970 /* Set super info and mark the class a complete */
2971 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
2972 ctxp->interface_number);
2973 ctxp->interface_number = 0;
2974 CLASS_COMPLETE_P (decl) = 1;
2975 add_superinterfaces (decl, interfaces);
2977 /* Eventually sets the @deprecated tag flag */
2978 CHECK_DEPRECATED (decl);
2980 return decl;
2983 /* Can't use lookup_field () since we don't want to load the class and
2984 can't set the CLASS_LOADED_P flag */
2986 static tree
2987 find_field (class, name)
2988 tree class;
2989 tree name;
2991 tree decl;
2992 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
2994 if (DECL_NAME (decl) == name)
2995 return decl;
2997 return NULL_TREE;
3000 /* Wrap around lookup_field that doesn't potentially upset the value
3001 of CLASS */
3003 static tree
3004 lookup_field_wrapper (class, name)
3005 tree class, name;
3007 tree type = class;
3008 tree decl;
3009 java_parser_context_save_global ();
3010 decl = lookup_field (&type, name);
3011 java_parser_context_restore_global ();
3012 return decl;
3015 /* Find duplicate field within the same class declarations and report
3016 the error. Returns 1 if a duplicated field was found, 0
3017 otherwise. */
3019 static int
3020 duplicate_declaration_error_p (new_field_name, new_type, cl)
3021 tree new_field_name, new_type, cl;
3023 /* This might be modified to work with method decl as well */
3024 tree decl = find_field (TREE_TYPE (ctxp->current_parsed_class),
3025 new_field_name);
3026 if (decl)
3028 char *t1 = strdup (purify_type_name
3029 ((TREE_CODE (new_type) == POINTER_TYPE
3030 && TREE_TYPE (new_type) == NULL_TREE) ?
3031 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
3032 lang_printable_name (new_type, 1)));
3033 /* The type may not have been completed by the time we report
3034 the error */
3035 char *t2 = strdup (purify_type_name
3036 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
3037 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
3038 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
3039 lang_printable_name (TREE_TYPE (decl), 1)));
3040 parse_error_context
3041 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
3042 t1, IDENTIFIER_POINTER (new_field_name),
3043 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
3044 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3045 free (t1);
3046 free (t2);
3047 return 1;
3049 return 0;
3052 /* Field registration routine. If TYPE doesn't exist, field
3053 declarations are linked to the undefined TYPE dependency list, to
3054 be later resolved in java_complete_class () */
3056 static void
3057 register_fields (flags, type, variable_list)
3058 int flags;
3059 tree type, variable_list;
3061 tree current, saved_type;
3062 tree class_type = TREE_TYPE (ctxp->current_parsed_class);
3063 int saved_lineno = lineno;
3064 int must_chain = 0;
3065 tree wfl = NULL_TREE;
3067 /* If we're adding fields to interfaces, those fields are public,
3068 static, final */
3069 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
3071 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
3072 flags, ACC_PUBLIC,
3073 "%s", "interface field(s)");
3074 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
3075 flags, ACC_STATIC,
3076 "%s", "interface field(s)");
3077 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
3078 flags, ACC_FINAL, "%s", "interface field(s)");
3079 check_modifiers ("Illegal interface member modifier `%s'", flags,
3080 INTERFACE_FIELD_MODIFIERS);
3081 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
3084 /* Obtain a suitable type for resolution, if necessary */
3085 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
3087 /* If TYPE is fully resolved and we don't have a reference, make one */
3088 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3090 for (current = variable_list, saved_type = type; current;
3091 current = TREE_CHAIN (current), type = saved_type)
3093 tree real_type;
3094 tree field_decl;
3095 tree cl = TREE_PURPOSE (current);
3096 tree init = TREE_VALUE (current);
3097 tree current_name = EXPR_WFL_NODE (cl);
3099 /* Process NAME, as it may specify extra dimension(s) for it */
3100 type = build_array_from_name (type, wfl, current_name, &current_name);
3102 /* Type adjustment. We may have just readjusted TYPE because
3103 the variable specified more dimensions. Make sure we have
3104 a reference if we can and don't have one already. Also
3105 change the name if we have an init. */
3106 if (type != saved_type)
3108 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3109 if (init)
3110 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
3113 real_type = GET_REAL_TYPE (type);
3114 /* Check for redeclarations */
3115 if (duplicate_declaration_error_p (current_name, real_type, cl))
3116 continue;
3118 /* Set lineno to the line the field was found and create a
3119 declaration for it. Eventually sets the @deprecated tag flag. */
3120 lineno = EXPR_WFL_LINENO (cl);
3121 field_decl = add_field (class_type, current_name, real_type, flags);
3122 CHECK_DEPRECATED (field_decl);
3124 /* Check if we must chain. */
3125 if (must_chain)
3126 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
3128 /* Default value of a static field is 0 and it is considered
3129 initialized. */
3130 if (flags & ACC_STATIC)
3131 INITIALIZED_P (field_decl) = 1;
3133 /* If we have an initialization value tied to the field */
3134 if (init)
3136 /* The field is declared static */
3137 if (flags & ACC_STATIC)
3139 /* We include the field and its initialization part into
3140 a list used to generate <clinit>. After <clinit> is
3141 walked, fields initialization will be processed and
3142 fields initialized with know constants will be taken
3143 out of <clinit> and have ther DECL_INITIAL set
3144 appropriately. */
3145 TREE_CHAIN (init) = ctxp->static_initialized;
3146 ctxp->static_initialized = init;
3147 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
3149 /* A non-static field declared with an immediate initialization is
3150 to be initialized in <init>, if any. This field is remembered
3151 to be processed at the time of the generation of <init>. */
3152 else
3154 TREE_CHAIN (init) = ctxp->non_static_initialized;
3155 ctxp->non_static_initialized = init;
3157 INITIALIZED_P (field_decl) = 1;
3158 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
3161 lineno = saved_lineno;
3164 /* Generate the method $finit$ that initializes fields initialized
3165 upon declaration. */
3167 static void
3168 maybe_generate_finit ()
3170 tree mdecl, current;
3172 if (!ctxp->non_static_initialized || java_error_count)
3173 return;
3175 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3176 ACC_PRIVATE, void_type_node,
3177 finit_identifier_node, end_params_node);
3178 start_artificial_method_body (mdecl);
3180 ctxp->non_static_initialized = nreverse (ctxp->non_static_initialized);
3181 for (current = ctxp->non_static_initialized; current;
3182 current = TREE_CHAIN (current))
3183 java_method_add_stmt (mdecl,
3184 build_debugable_stmt (EXPR_WFL_LINECOL (current),
3185 current));
3187 end_artificial_method_body (mdecl);
3188 CLASS_HAS_FINIT_P (TREE_TYPE (ctxp->current_parsed_class)) = 1;
3189 ctxp->non_static_initialized = NULL_TREE;
3192 /* Check whether it is necessary to generate a <clinit> for the class
3193 we just parsed. */
3195 static void
3196 maybe_generate_clinit ()
3198 tree mdecl, c;
3200 if (!ctxp->static_initialized || java_error_count)
3201 return;
3203 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3204 ACC_STATIC, void_type_node,
3205 clinit_identifier_node, end_params_node);
3206 start_artificial_method_body (mdecl);
3208 /* Keep initialization in order to enforce 8.5 */
3209 ctxp->static_initialized = nreverse (ctxp->static_initialized);
3211 /* We process the list of assignment we produced as the result of
3212 the declaration of initialized static field and add them as
3213 statement to the <clinit> method. */
3214 for (c = ctxp->static_initialized; c; c = TREE_CHAIN (c))
3216 /* We build the assignment expression that will initialize the
3217 field to its value. There are strict rules on static
3218 initializers (8.5). FIXME */
3219 java_method_add_stmt (mdecl,
3220 build_debugable_stmt (EXPR_WFL_LINECOL (c), c));
3223 end_artificial_method_body (mdecl);
3224 ctxp->static_initialized = NULL_TREE;
3227 /* Shared accros method_declarator and method_header to remember the
3228 patch stage that was reached during the declaration of the method.
3229 A method DECL is built differently is there is no patch
3230 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
3231 pending on the currently defined method. */
3233 static int patch_stage;
3235 /* Check the method declaration and add the method to its current
3236 class. If the argument list is known to contain incomplete types,
3237 the method is partially added and the registration will be resume
3238 once the method arguments resolved. If TYPE is NULL, we're dealing
3239 with a constructor. */
3241 static tree
3242 method_header (flags, type, mdecl, throws)
3243 int flags;
3244 tree type, mdecl, throws;
3246 tree meth = TREE_VALUE (mdecl);
3247 tree id = TREE_PURPOSE (mdecl);
3248 tree this_class = TREE_TYPE (ctxp->current_parsed_class);
3249 tree type_wfl = NULL_TREE;
3250 tree meth_name = NULL_TREE, current, orig_arg;
3251 int saved_lineno;
3252 int constructor_ok = 0, must_chain;
3254 check_modifiers_consistency (flags);
3256 /* There are some forbidden modifiers for an abstract method and its
3257 class must be abstract as well. */
3258 if (type && (flags & ACC_ABSTRACT))
3260 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
3261 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
3262 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
3263 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
3264 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
3265 if (!CLASS_ABSTRACT (TYPE_NAME (this_class)))
3266 parse_error_context
3267 (id, "Class `%s' must be declared abstract to define abstract "
3268 "method `%s'",
3269 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
3270 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3272 /* Things to be checked when declaring a constructor */
3273 if (!type)
3275 int ec = java_error_count;
3276 /* 8.6: Constructor declarations: we might be trying to define a
3277 method without specifying a return type. */
3278 if (EXPR_WFL_NODE (id) != ctxp->current_parsed_class_un)
3279 parse_error_context
3280 (id, "Invalid method declaration, return type required");
3281 /* 8.6.3: Constructor modifiers */
3282 else
3284 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
3285 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
3286 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
3287 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
3288 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
3290 /* If we found error here, we don't consider it's OK to tread
3291 the method definition as a constructor, for the rest of this
3292 function */
3293 if (ec == java_error_count)
3294 constructor_ok = 1;
3297 /* Method declared within the scope of an interface are implicitly
3298 abstract and public. Conflicts with other erroneously provided
3299 modifiers are check right after. */
3301 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
3303 /* If FLAGS isn't set because of a modifier, turn the
3304 corresponding modifier WFL to NULL so we issue a warning on
3305 the obsolete use of the modifier */
3306 if (!(flags & ACC_PUBLIC))
3307 MODIFIER_WFL (PUBLIC_TK) = NULL;
3308 if (!(flags & ACC_ABSTRACT))
3309 MODIFIER_WFL (ABSTRACT_TK) = NULL;
3310 flags |= ACC_PUBLIC;
3311 flags |= ACC_ABSTRACT;
3314 /* Modifiers context reset moved up, so abstract method declaration
3315 modifiers can be later checked. */
3317 /* Set constructor returned type to void and method name to <init>,
3318 unless we found an error identifier the constructor (in which
3319 case we retain the original name) */
3320 if (!type)
3322 type = void_type_node;
3323 if (constructor_ok)
3324 meth_name = init_identifier_node;
3326 else
3327 meth_name = EXPR_WFL_NODE (id);
3329 /* Do the returned type resolution and registration if necessary */
3330 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3332 if (meth_name)
3333 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
3334 EXPR_WFL_NODE (id) = meth_name;
3335 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3337 if (must_chain)
3339 patch_stage = JDEP_METHOD_RETURN;
3340 register_incomplete_type (patch_stage, type_wfl, id, type);
3341 TREE_TYPE (meth) = GET_REAL_TYPE (type);
3343 else
3344 TREE_TYPE (meth) = type;
3346 saved_lineno = lineno;
3347 /* When defining an abstract or interface method, the curly
3348 bracket at level 1 doesn't exist because there is no function
3349 body */
3350 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
3351 EXPR_WFL_LINENO (id));
3353 /* Remember the original argument list */
3354 orig_arg = TYPE_ARG_TYPES (meth);
3356 if (patch_stage) /* includes ret type and/or all args */
3358 jdep *jdep;
3359 meth = add_method_1 (this_class, flags, meth_name, meth);
3360 /* Patch for the return type */
3361 if (patch_stage == JDEP_METHOD_RETURN)
3363 jdep = CLASSD_LAST (ctxp->classd_list);
3364 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
3366 /* This is the stop JDEP. METH allows the function's signature
3367 to be computed. */
3368 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
3370 else
3371 meth = add_method (this_class, flags, meth_name,
3372 build_java_signature (meth));
3374 /* Fix the method argument list so we have the argument name
3375 information */
3376 fix_method_argument_names (orig_arg, meth);
3378 /* Register the parameter number and re-install the current line
3379 number */
3380 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
3381 lineno = saved_lineno;
3383 /* Register exception specified by the `throws' keyword for
3384 resolution and set the method decl appropriate field to the list.
3385 Note: the grammar ensures that what we get here are class
3386 types. */
3387 if (throws)
3389 throws = nreverse (throws);
3390 for (current = throws; current; current = TREE_CHAIN (current))
3392 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
3393 NULL_TREE, NULL_TREE);
3394 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
3395 &TREE_VALUE (current);
3397 DECL_FUNCTION_THROWS (meth) = throws;
3400 /* We set the DECL_NAME to ID so we can track the location where
3401 the function was declared. This allow us to report
3402 redefinition error accurately. When method are verified,
3403 DECL_NAME is reinstalled properly (using the content of the
3404 WFL node ID) (see check_method_redefinition). We don't do that
3405 when Object is being defined. Constructor <init> names will be
3406 reinstalled the same way. */
3407 if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node)
3408 DECL_NAME (meth) = id;
3410 /* Set the flag if we correctly processed a constructor */
3411 if (constructor_ok)
3412 DECL_CONSTRUCTOR_P (meth) = 1;
3414 /* Eventually set the @deprecated tag flag */
3415 CHECK_DEPRECATED (meth);
3417 return meth;
3420 static void
3421 fix_method_argument_names (orig_arg, meth)
3422 tree orig_arg, meth;
3424 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
3425 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
3427 TREE_PURPOSE (arg) = this_identifier_node;
3428 arg = TREE_CHAIN (arg);
3430 while (orig_arg != end_params_node)
3432 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
3433 orig_arg = TREE_CHAIN (orig_arg);
3434 arg = TREE_CHAIN (arg);
3438 /* Complete the method declaration with METHOD_BODY. */
3440 static void
3441 complete_method_declaration (method_body)
3442 tree method_body;
3444 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
3445 maybe_absorb_scoping_blocks ();
3446 /* Exit function's body */
3447 exit_block ();
3448 /* Merge last line of the function with first line, directly in the
3449 function decl. It will be used to emit correct debug info. */
3450 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
3453 /* Build a an error message for constructor circularity errors. */
3455 static char *
3456 constructor_circularity_msg (from, to)
3457 tree from, to;
3459 static char string [4096];
3460 char *t = strdup (lang_printable_name (from, 0));
3461 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
3462 free (t);
3463 return string;
3466 /* Verify a circular call to METH. Return 1 if an error is found, 0
3467 otherwise. */
3469 static int
3470 verify_constructor_circularity (meth, current)
3471 tree meth, current;
3473 static tree list = NULL_TREE;
3474 tree c;
3475 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3477 if (TREE_VALUE (c) == meth)
3479 char *t;
3480 if (list)
3482 tree liste;
3483 list = nreverse (list);
3484 for (liste = list; liste; liste = TREE_CHAIN (liste))
3486 parse_error_context
3487 (TREE_PURPOSE (TREE_PURPOSE (liste)),
3488 constructor_circularity_msg
3489 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
3490 java_error_count--;
3493 t = strdup (lang_printable_name (meth, 0));
3494 parse_error_context (TREE_PURPOSE (c),
3495 "%s: recursive invocation of constructor `%s'",
3496 constructor_circularity_msg (current, meth), t);
3497 free (t);
3498 list = NULL_TREE;
3499 return 1;
3502 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3504 list = tree_cons (c, current, list);
3505 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
3506 return 1;
3507 list = TREE_CHAIN (list);
3509 return 0;
3512 /* Check modifiers that can be declared but exclusively */
3514 static void
3515 check_modifiers_consistency (flags)
3516 int flags;
3518 int acc_count = 0;
3519 tree cl = NULL_TREE;
3521 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, 0, acc_count, cl);
3522 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, 1, acc_count, cl);
3523 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, 2, acc_count, cl);
3524 if (acc_count > 1)
3525 parse_error_context
3526 (cl, "Inconsistent member declaration. At most one of `public', "
3527 "`private', or `protected' may be specified");
3530 /* Check the methode header METH for abstract specifics features */
3532 static void
3533 check_abstract_method_header (meth)
3534 tree meth;
3536 int flags = get_access_flags_from_decl (meth);
3537 /* DECL_NAME might still be a WFL node */
3538 tree name = GET_METHOD_NAME (meth);
3540 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (ABSTRACT_TK), flags,
3541 ACC_ABSTRACT, "abstract method `%s'",
3542 IDENTIFIER_POINTER (name));
3543 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), flags,
3544 ACC_PUBLIC, "abstract method `%s'",
3545 IDENTIFIER_POINTER (name));
3547 check_modifiers ("Illegal modifier `%s' for interface method",
3548 flags, INTERFACE_METHOD_MODIFIERS);
3551 /* Create a FUNCTION_TYPE node and start augmenting it with the
3552 declared function arguments. Arguments type that can't be resolved
3553 are left as they are, but the returned node is marked as containing
3554 incomplete types. */
3556 static tree
3557 method_declarator (id, list)
3558 tree id, list;
3560 tree arg_types = NULL_TREE, current, node;
3561 tree meth = make_node (FUNCTION_TYPE);
3562 jdep *jdep;
3564 patch_stage = JDEP_NO_PATCH;
3566 for (current = list; current; current = TREE_CHAIN (current))
3568 int must_chain = 0;
3569 tree wfl_name = TREE_PURPOSE (current);
3570 tree type = TREE_VALUE (current);
3571 tree name = EXPR_WFL_NODE (wfl_name);
3572 tree already, arg_node;
3573 tree type_wfl = NULL_TREE;
3574 tree real_type;
3576 /* Obtain a suitable type for resolution, if necessary */
3577 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3579 /* Process NAME, as it may specify extra dimension(s) for it */
3580 type = build_array_from_name (type, type_wfl, name, &name);
3581 EXPR_WFL_NODE (wfl_name) = name;
3583 real_type = GET_REAL_TYPE (type);
3584 if (TREE_CODE (real_type) == RECORD_TYPE)
3586 real_type = promote_type (real_type);
3587 if (TREE_CODE (type) == TREE_LIST)
3588 TREE_PURPOSE (type) = real_type;
3591 /* Check redefinition */
3592 for (already = arg_types; already; already = TREE_CHAIN (already))
3593 if (TREE_PURPOSE (already) == name)
3595 parse_error_context
3596 (wfl_name, "Variable `%s' is used more than once in the "
3597 "argument list of method `%s'", IDENTIFIER_POINTER (name),
3598 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3599 break;
3602 /* If we've an incomplete argument type, we know there is a location
3603 to patch when the type get resolved, later. */
3604 jdep = NULL;
3605 if (must_chain)
3607 patch_stage = JDEP_METHOD;
3608 type = register_incomplete_type (patch_stage,
3609 type_wfl, wfl_name, type);
3610 jdep = CLASSD_LAST (ctxp->classd_list);
3611 JDEP_MISC (jdep) = id;
3614 /* The argument node: a name and a (possibly) incomplete type */
3615 arg_node = build_tree_list (name, real_type);
3616 if (jdep)
3617 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
3618 TREE_CHAIN (arg_node) = arg_types;
3619 arg_types = arg_node;
3621 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
3622 node = build_tree_list (id, meth);
3623 return node;
3626 static int
3627 unresolved_type_p (wfl, returned)
3628 tree wfl;
3629 tree *returned;
3632 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
3634 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
3635 if (returned)
3636 *returned = (decl ? TREE_TYPE (decl) : NULL_TREE);
3637 return 1;
3639 if (returned)
3640 *returned = wfl;
3641 return 0;
3644 /* From NAME, build a qualified identifier node using the
3645 qualification from the current package definition. */
3647 static tree
3648 parser_qualified_classname (name)
3649 tree name;
3651 if (ctxp->package)
3652 return merge_qualified_name (ctxp->package, EXPR_WFL_NODE (name));
3653 else
3654 return EXPR_WFL_NODE (name);
3657 /* Called once the type a interface extends is resolved. Returns 0 if
3658 everything is OK. */
3660 static int
3661 parser_check_super_interface (super_decl, this_decl, this_wfl)
3662 tree super_decl, this_decl, this_wfl;
3664 tree super_type = TREE_TYPE (super_decl);
3666 /* Has to be an interface */
3667 if (!CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (super_decl))))
3669 parse_error_context
3670 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
3671 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
3672 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
3673 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
3674 "interface" : "class"),
3675 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
3676 return 1;
3679 /* Check scope: same package OK, other package: OK if public */
3680 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
3681 return 1;
3683 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
3684 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3685 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3686 return 0;
3689 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
3690 0 if everthing is OK. */
3692 static int
3693 parser_check_super (super_decl, this_decl, wfl)
3694 tree super_decl, this_decl, wfl;
3696 tree super_type = TREE_TYPE (super_decl);
3698 /* SUPER should be a CLASS (neither an array nor an interface) */
3699 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
3701 parse_error_context
3702 (wfl, "Class `%s' can't subclass %s `%s'",
3703 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3704 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
3705 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3706 return 1;
3709 if (CLASS_FINAL (TYPE_NAME (super_type)))
3711 parse_error_context (wfl, "Can't subclass final classes: %s",
3712 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3713 return 1;
3716 /* Check scope: same package OK, other package: OK if public */
3717 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
3718 return 1;
3720 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
3721 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3722 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3723 return 0;
3726 /* Create a new dependency list and link it (in a LIFO manner) to the
3727 CTXP list of type dependency list. */
3729 static void
3730 create_jdep_list (ctxp)
3731 struct parser_ctxt *ctxp;
3733 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
3734 new->first = new->last = NULL;
3735 new->next = ctxp->classd_list;
3736 ctxp->classd_list = new;
3739 static jdeplist *
3740 reverse_jdep_list (ctxp)
3741 struct parser_ctxt *ctxp;
3743 register jdeplist *prev = NULL, *current, *next;
3744 for (current = ctxp->classd_list; current; current = next)
3746 next = current->next;
3747 current->next = prev;
3748 prev = current;
3750 return prev;
3753 /* Create a fake pointer based on the ID stored in
3754 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
3755 registered again. */
3757 static tree
3758 obtain_incomplete_type (type_name)
3759 tree type_name;
3761 tree ptr, name;
3763 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
3764 name = EXPR_WFL_NODE (type_name);
3765 else if (INCOMPLETE_TYPE_P (type_name))
3766 name = TYPE_NAME (type_name);
3767 else
3768 fatal ("invalid type name - obtain_incomplete_type");
3770 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
3771 if (TYPE_NAME (TREE_PURPOSE (ptr)) == name)
3772 break;
3774 if (!ptr)
3776 tree core;
3777 push_obstacks (&permanent_obstack, &permanent_obstack);
3778 BUILD_PTR_FROM_NAME (core, name);
3779 layout_type (core);
3780 ptr = build_tree_list (core, NULL_TREE);
3781 pop_obstacks ();
3782 TREE_CHAIN (ptr) = ctxp->incomplete_class;
3783 ctxp->incomplete_class = ptr;
3786 return ptr;
3789 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
3790 non NULL instead of computing a new fake type based on WFL. The new
3791 dependency is inserted in the current type dependency list, in FIFO
3792 manner. */
3794 static tree
3795 register_incomplete_type (kind, wfl, decl, ptr)
3796 int kind;
3797 tree wfl, decl, ptr;
3799 jdep *new = (jdep *)xmalloc (sizeof (jdep));
3801 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
3802 ptr = obtain_incomplete_type (wfl);
3804 JDEP_KIND (new) = kind;
3805 JDEP_DECL (new) = decl;
3806 JDEP_SOLV (new) = ptr;
3807 JDEP_WFL (new) = wfl;
3808 JDEP_CHAIN (new) = NULL;
3809 JDEP_MISC (new) = NULL_TREE;
3810 JDEP_GET_PATCH (new) = (tree *)NULL;
3812 JDEP_INSERT (ctxp->classd_list, new);
3814 return ptr;
3817 void
3818 java_check_circular_reference ()
3820 tree current;
3821 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
3823 tree type = TREE_TYPE (current);
3824 if (CLASS_INTERFACE (TYPE_NAME (type)))
3826 /* Check all interfaces this class extends */
3827 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
3828 int n, i;
3830 if (!basetype_vec)
3831 return;
3832 n = TREE_VEC_LENGTH (basetype_vec);
3833 for (i = 0; i < n; i++)
3835 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
3836 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
3837 && interface_of_p (type, BINFO_TYPE (vec_elt)))
3838 parse_error_context (lookup_cl (current),
3839 "Cyclic interface inheritance");
3842 else
3843 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
3844 parse_error_context (lookup_cl (current),
3845 "Cyclic class inheritance");
3849 /* safe_layout_class just makes sure that we can load a class without
3850 disrupting the current_class, input_file, lineno, etc, information
3851 about the class processed currently. */
3853 void
3854 safe_layout_class (class)
3855 tree class;
3857 tree save_current_class = current_class;
3858 char *save_input_filename = input_filename;
3859 int save_lineno = lineno;
3861 push_obstacks (&permanent_obstack, &permanent_obstack);
3863 layout_class (class);
3864 pop_obstacks ();
3866 current_class = save_current_class;
3867 input_filename = save_input_filename;
3868 lineno = save_lineno;
3869 CLASS_LOADED_P (class) = 1;
3872 static tree
3873 jdep_resolve_class (dep)
3874 jdep *dep;
3876 tree decl;
3878 if (JDEP_RESOLVED_P (dep))
3879 decl = JDEP_RESOLVED_DECL (dep);
3880 else
3882 decl = resolve_class (JDEP_TO_RESOLVE (dep),
3883 JDEP_DECL (dep), JDEP_WFL (dep));
3884 JDEP_RESOLVED (dep, decl);
3887 if (!decl)
3888 complete_class_report_errors (dep);
3890 return decl;
3893 /* Complete unsatisfied class declaration and their dependencies */
3895 void
3896 java_complete_class ()
3898 tree cclass;
3899 jdeplist *cclassd;
3900 int error_found;
3901 tree type;
3903 push_obstacks (&permanent_obstack, &permanent_obstack);
3905 /* Process imports and reverse the import on demand list */
3906 process_imports ();
3907 if (ctxp->import_demand_list)
3908 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
3910 /* Rever things so we have the right order */
3911 ctxp->class_list = nreverse (ctxp->class_list);
3912 ctxp->classd_list = reverse_jdep_list (ctxp);
3914 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
3915 cclass && cclassd;
3916 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
3918 jdep *dep;
3919 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
3921 tree decl;
3922 if (!(decl = jdep_resolve_class (dep)))
3923 continue;
3925 /* Now it's time to patch */
3926 switch (JDEP_KIND (dep))
3928 case JDEP_SUPER:
3929 /* Simply patch super */
3930 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
3931 continue;
3932 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
3933 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
3934 break;
3936 case JDEP_FIELD:
3938 /* We do part of the job done in add_field */
3939 tree field_decl = JDEP_DECL (dep);
3940 tree field_type = TREE_TYPE (decl);
3941 push_obstacks (&permanent_obstack, &permanent_obstack);
3942 if (TREE_CODE (field_type) == RECORD_TYPE)
3943 field_type = promote_type (field_type);
3944 pop_obstacks ();
3945 TREE_TYPE (field_decl) = field_type;
3946 DECL_ALIGN (field_decl) = 0;
3947 layout_decl (field_decl, 0);
3948 SOURCE_FRONTEND_DEBUG
3949 (("Completed field/var decl `%s' with `%s'",
3950 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
3951 IDENTIFIER_POINTER (DECL_NAME (decl))));
3952 break;
3954 case JDEP_METHOD: /* We start patching a method */
3955 case JDEP_METHOD_RETURN:
3956 error_found = 0;
3957 while (1)
3959 if (decl)
3961 type = TREE_TYPE(decl);
3962 if (TREE_CODE (type) == RECORD_TYPE)
3963 type = promote_type (type);
3964 JDEP_APPLY_PATCH (dep, type);
3965 SOURCE_FRONTEND_DEBUG
3966 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
3967 "Completing fct `%s' with ret type `%s'":
3968 "Completing arg `%s' with type `%s'"),
3969 IDENTIFIER_POINTER (EXPR_WFL_NODE
3970 (JDEP_DECL_WFL (dep))),
3971 IDENTIFIER_POINTER (DECL_NAME (decl))));
3973 else
3974 error_found = 1;
3975 dep = JDEP_CHAIN (dep);
3976 if (JDEP_KIND (dep) == JDEP_METHOD_END)
3977 break;
3978 else
3979 decl = jdep_resolve_class (dep);
3981 if (!error_found)
3983 tree mdecl = JDEP_DECL (dep), signature;
3984 push_obstacks (&permanent_obstack, &permanent_obstack);
3985 /* Recompute and reset the signature */
3986 signature = build_java_signature (TREE_TYPE (mdecl));
3987 set_java_signature (TREE_TYPE (mdecl), signature);
3988 pop_obstacks ();
3990 else
3991 continue;
3992 break;
3994 case JDEP_INTERFACE:
3995 if (parser_check_super_interface (decl, JDEP_DECL (dep),
3996 JDEP_WFL (dep)))
3997 continue;
3998 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
3999 break;
4001 case JDEP_PARM:
4002 case JDEP_VARIABLE:
4003 type = TREE_TYPE(decl);
4004 if (TREE_CODE (type) == RECORD_TYPE)
4005 type = promote_type (type);
4006 JDEP_APPLY_PATCH (dep, type);
4007 break;
4009 case JDEP_TYPE:
4010 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4011 SOURCE_FRONTEND_DEBUG
4012 (("Completing a random type dependency on a '%s' node",
4013 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
4014 break;
4016 case JDEP_EXCEPTION:
4017 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4018 SOURCE_FRONTEND_DEBUG
4019 (("Completing `%s' `throws' argument node",
4020 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
4021 break;
4023 default:
4024 fatal ("Can't handle patch code %d - java_complete_class",
4025 JDEP_KIND (dep));
4029 pop_obstacks ();
4030 return;
4033 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
4034 array. */
4036 static tree
4037 resolve_class (class_type, decl, cl)
4038 tree class_type, decl, cl;
4040 char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
4041 char *base = name;
4042 tree resolved_type, resolved_type_decl;
4044 /* 1- Check to see if we have an array. If true, find what we really
4045 want to resolve */
4046 while (name[0] == '[')
4047 name++;
4048 if (base != name)
4049 TYPE_NAME (class_type) = get_identifier (name);
4051 /* 2- Resolve the bare type */
4052 if (!(resolved_type_decl = do_resolve_class (class_type, decl, cl)))
4053 return NULL_TREE;
4054 resolved_type = TREE_TYPE (resolved_type_decl);
4056 /* 3- If we have and array, reconstruct the array down to its nesting */
4057 if (base != name)
4059 while (base != name)
4061 if (TREE_CODE (resolved_type) == RECORD_TYPE)
4062 resolved_type = promote_type (resolved_type);
4063 resolved_type = build_java_array_type (resolved_type, -1);
4064 CLASS_LOADED_P (resolved_type) = 1;
4065 name--;
4067 /* Build a fake decl for this, since this is what is expected to
4068 be returned. */
4069 resolved_type_decl =
4070 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
4071 /* Figure how those two things are important for error report. FIXME */
4072 DECL_SOURCE_LINE (resolved_type_decl) = 0;
4073 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
4075 return resolved_type_decl;
4078 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
4079 are used to report error messages. */
4081 static tree
4082 do_resolve_class (class_type, decl, cl)
4083 tree class_type;
4084 tree decl;
4085 tree cl;
4087 tree new_class_decl;
4088 tree original_name = NULL_TREE;
4090 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
4091 its is changed by find_in_imports{_on_demand} */
4093 /* 1- Check for the type in single imports */
4094 if (find_in_imports (class_type))
4095 return NULL_TREE;
4097 /* 2- And check for the type in the current compilation unit. If it fails,
4098 try with a name qualified with the package name if appropriate. */
4099 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4101 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4102 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4103 load_class (TYPE_NAME (class_type), 0);
4104 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4107 original_name = TYPE_NAME (class_type);
4108 if (!QUALIFIED_P (TYPE_NAME (class_type)) && ctxp->package)
4109 TYPE_NAME (class_type) = merge_qualified_name (ctxp->package,
4110 TYPE_NAME (class_type));
4111 if (!(new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4112 load_class (TYPE_NAME (class_type), 0);
4113 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4115 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4116 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4117 load_class (TYPE_NAME (class_type), 0);
4118 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4120 TYPE_NAME (class_type) = original_name;
4122 /* 3- Check an other compilation unit that bears the name of type */
4123 load_class (TYPE_NAME (class_type), 0);
4124 if (check_pkg_class_access (TYPE_NAME (class_type),
4125 (cl ? cl : lookup_cl (decl))))
4126 return NULL_TREE;
4128 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4129 return new_class_decl;
4131 /* 4- Check the import on demands. Don't allow bar.baz to be
4132 imported from foo.* */
4133 if (!QUALIFIED_P (TYPE_NAME (class_type)))
4134 if (find_in_imports_on_demand (class_type))
4135 return NULL_TREE;
4137 /* 5- Last call for a resolution */
4138 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4141 /* Resolve NAME and lay it out (if not done and if not the current
4142 parsed class). Return a decl node. This function is meant to be
4143 called when type resolution is necessary during the walk pass. */
4145 static tree
4146 resolve_and_layout (something, cl)
4147 tree something;
4148 tree cl;
4150 tree decl;
4152 /* Don't do that on the current class */
4153 if (something == current_class)
4154 return TYPE_NAME (current_class);
4156 /* Don't do anything for void and other primitive types */
4157 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4158 return NULL_TREE;
4160 /* Pointer types can be reall pointer types or fake pointers. When
4161 finding a real pointer, recheck for primitive types */
4162 if (TREE_CODE (something) == POINTER_TYPE)
4164 if (TREE_TYPE (something))
4166 something = TREE_TYPE (something);
4167 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4168 return NULL_TREE;
4170 else
4171 something = TYPE_NAME (something);
4174 /* Don't do anything for arrays of primitive types */
4175 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
4176 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
4177 return NULL_TREE;
4179 /* If something is not and IDENTIFIER_NODE, it can be a a TYPE_DECL
4180 or a real TYPE */
4181 if (TREE_CODE (something) != IDENTIFIER_NODE)
4182 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
4183 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
4185 if (!(decl = resolve_no_layout (something, cl)))
4186 return NULL_TREE;
4188 /* Resolve and layout if necessary */
4189 layout_class_methods (TREE_TYPE (decl));
4190 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl)))
4191 CHECK_METHODS (decl);
4192 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
4193 safe_layout_class (TREE_TYPE (decl));
4195 return decl;
4198 /* Resolve a class, returns its decl but doesn't perform any
4199 layout. The current parsing context is saved and restored */
4201 static tree
4202 resolve_no_layout (name, cl)
4203 tree name, cl;
4205 tree ptr, decl;
4206 BUILD_PTR_FROM_NAME (ptr, name);
4207 java_parser_context_save_global ();
4208 decl = resolve_class (ptr, NULL_TREE, cl);
4209 java_parser_context_restore_global ();
4211 return decl;
4214 /* Called when reporting errors. Skip leader '[' in a complex array
4215 type description that failed to be resolved. */
4217 static char *
4218 purify_type_name (name)
4219 char *name;
4221 while (*name && *name == '[')
4222 name++;
4223 return name;
4226 /* The type CURRENT refers to can't be found. We print error messages. */
4228 static void
4229 complete_class_report_errors (dep)
4230 jdep *dep;
4232 char *name;
4234 if (!JDEP_WFL (dep))
4235 return;
4237 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
4238 switch (JDEP_KIND (dep))
4240 case JDEP_SUPER:
4241 parse_error_context
4242 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
4243 purify_type_name (name),
4244 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4245 break;
4246 case JDEP_FIELD:
4247 parse_error_context
4248 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
4249 purify_type_name (name),
4250 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4251 break;
4252 case JDEP_METHOD: /* Covers arguments */
4253 parse_error_context
4254 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4255 "argument `%s' of method `%s'",
4256 purify_type_name (name),
4257 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
4258 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
4259 break;
4260 case JDEP_METHOD_RETURN: /* Covers return type */
4261 parse_error_context
4262 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4263 "return type of method `%s'",
4264 purify_type_name (name),
4265 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
4266 break;
4267 case JDEP_INTERFACE:
4268 parse_error_context
4269 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
4270 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
4271 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
4272 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4273 break;
4274 case JDEP_VARIABLE:
4275 parse_error_context
4276 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4277 "local variable `%s'",
4278 purify_type_name (IDENTIFIER_POINTER
4279 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
4280 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4281 break;
4282 case JDEP_EXCEPTION: /* As specified by `throws' */
4283 parse_error_context
4284 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
4285 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
4286 break;
4287 default:
4288 /* Fix for -Wall. Just break doing nothing. The error will be
4289 caught later */
4290 break;
4294 /* Check uninitialized final. */
4296 void
4297 java_check_final ()
4301 /* Return a static string containing the DECL prototype string. If
4302 DECL is a constructor, use the class name instead of the form
4303 <init> */
4305 static char *
4306 get_printable_method_name (decl)
4307 tree decl;
4309 char *to_return;
4310 tree name;
4312 if (DECL_CONSTRUCTOR_P (decl))
4314 name = DECL_NAME (decl);
4315 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
4318 to_return = lang_printable_name (decl, 0);
4319 if (DECL_CONSTRUCTOR_P (decl))
4320 DECL_NAME (decl) = name;
4322 return to_return;
4325 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
4326 nevertheless needs to be verfied, 1 otherwise. */
4328 static int
4329 reset_method_name (method)
4330 tree method;
4332 if (DECL_NAME (method) != clinit_identifier_node
4333 && DECL_NAME (method) != finit_identifier_node)
4335 /* NAME is just the plain name when Object is being defined */
4336 if (DECL_CONTEXT (method) != object_type_node)
4337 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
4338 init_identifier_node : GET_METHOD_NAME (method));
4339 return 0;
4341 else
4342 return 1;
4345 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
4347 tree
4348 java_get_real_method_name (method_decl)
4349 tree method_decl;
4351 tree method_name = DECL_NAME (method_decl);
4352 if (DECL_CONSTRUCTOR_P (method_decl))
4353 return init_identifier_node;
4355 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
4356 and still can be a constructor. FIXME */
4358 /* Don't confuse method only bearing the name of their class as
4359 constructors */
4360 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
4361 && ctxp
4362 && ctxp->current_parsed_class_un == EXPR_WFL_NODE (method_name)
4363 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
4364 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
4365 return init_identifier_node;
4366 else
4367 return EXPR_WFL_NODE (method_name);
4370 /* Track method being redefined inside the same class. As a side
4371 effect, set DECL_NAME to an IDENTIFIER (prior entering this
4372 function it's a FWL, so we can track errors more accurately */
4374 static int
4375 check_method_redefinition (class, method)
4376 tree class, method;
4378 tree redef, name;
4379 tree cl = DECL_NAME (method);
4380 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
4381 /* decl name of artificial <clinit> and $finit$ doesn't need to be fixed and
4382 checked */
4384 /* Reset the method name before running the check. If it returns 1,
4385 the method doesn't need to be verified with respect to method
4386 redeclaration and we return 0 */
4387 if (reset_method_name (method))
4388 return 0;
4390 name = DECL_NAME (method);
4391 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
4393 if (redef == method)
4394 break;
4395 if (DECL_NAME (redef) == name
4396 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
4398 parse_error_context
4399 (cl, "Duplicate %s declaration `%s'",
4400 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
4401 get_printable_method_name (redef));
4402 return 1;
4405 return 0;
4408 /* Check all the methods of CLASS. Methods are first completed then
4409 checked according to regular method existance rules.
4410 If no constructor were encountered, then build its declaration. */
4412 static void
4413 java_check_regular_methods (class_decl)
4414 tree class_decl;
4416 int saw_constructor = 0;
4417 tree method;
4418 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
4419 tree super_class = CLASSTYPE_SUPER (class);
4420 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
4421 tree mthrows;
4423 /* It is not necessary to check methods defined in java.lang.Object */
4424 if (class == object_type_node)
4425 return;
4427 if (!TYPE_NVIRTUALS (class))
4428 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4430 /* Should take interfaces into account. FIXME */
4431 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
4433 tree sig;
4434 tree method_wfl = DECL_NAME (method);
4435 int aflags;
4437 /* If we previously found something and its name was saved,
4438 reinstall it now */
4439 if (found && saved_found_wfl)
4440 DECL_NAME (found) = saved_found_wfl;
4442 /* Check for redefinitions */
4443 if (check_method_redefinition (class, method))
4444 continue;
4446 /* If we see one constructor a mark so we don't generate the
4447 default one. Also skip other verifications: constructors
4448 can't be inherited hence hiden or overriden */
4449 if (DECL_CONSTRUCTOR_P (method))
4451 saw_constructor = 1;
4452 continue;
4455 /* We verify things thrown by the method. They must inherits from
4456 java.lang.Throwable */
4457 for (mthrows = DECL_FUNCTION_THROWS (method);
4458 mthrows; mthrows = TREE_CHAIN (mthrows))
4460 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
4461 parse_error_context
4462 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be "
4463 "a subclass of class `java.lang.Throwable'",
4464 IDENTIFIER_POINTER
4465 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
4468 sig = build_java_argument_signature (TREE_TYPE (method));
4469 found = lookup_argument_method (super_class, DECL_NAME (method), sig);
4471 /* Nothing overrides or it's a private method. */
4472 if (!found || (found && METHOD_PRIVATE (found)))
4473 continue;
4475 /* If found wasn't verified, it's DECL_NAME won't be set properly.
4476 We set it temporarily for the sake of the error report. */
4477 saved_found_wfl = DECL_NAME (found);
4478 reset_method_name (found);
4480 /* Can't override a method with the same name and different return
4481 types. */
4482 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
4484 char *t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)),
4485 0));
4486 parse_error_context
4487 (method_wfl,
4488 "Method `%s' was defined with return type `%s' in class `%s'",
4489 lang_printable_name (found, 0), t,
4490 IDENTIFIER_POINTER
4491 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4492 free (t);
4495 /* Can't override final. Can't override static. */
4496 if (METHOD_FINAL (found) || METHOD_STATIC (found))
4498 /* Static *can* override static */
4499 if (METHOD_STATIC (found) && METHOD_STATIC (method))
4500 continue;
4501 parse_error_context
4502 (method_wfl,
4503 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
4504 (METHOD_FINAL (found) ? "Final" : "Static"),
4505 lang_printable_name (found, 0),
4506 (METHOD_FINAL (found) ? "final" : "static"),
4507 IDENTIFIER_POINTER
4508 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4509 continue;
4511 /* Static method can't override instance method. */
4512 if (METHOD_STATIC (method))
4514 parse_error_context
4515 (method_wfl,
4516 "Instance methods can't be overriden by a static method. Method "
4517 "`%s' is an instance method in class `%s'",
4518 lang_printable_name (found, 0),
4519 IDENTIFIER_POINTER
4520 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4521 continue;
4524 aflags = get_access_flags_from_decl (found);
4525 /* - Overriding/hiding public must be public
4526 - Overriding/hiding protected must be protected or public
4527 - If the overriden or hidden method has default (package)
4528 access, then the overriding or hiding method must not be
4529 private; otherwise, a compile-time error occurs */
4530 if ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
4531 || (METHOD_PROTECTED (found)
4532 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
4533 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
4534 && METHOD_PRIVATE (method)))
4536 parse_error_context
4537 (method_wfl,
4538 "Methods can't be overridden to be more private. Method `%s' is "
4539 "not %s in class `%s'", lang_printable_name (method, 0),
4540 (METHOD_PUBLIC (method) ? "public" :
4541 (METHOD_PRIVATE (method) ? "private" : "protected")),
4542 IDENTIFIER_POINTER (DECL_NAME
4543 (TYPE_NAME (DECL_CONTEXT (found)))));
4544 continue;
4547 /* Overriding methods must have compatible `throws' clauses on checked
4548 exceptions, if any */
4549 check_throws_clauses (method, method_wfl, found);
4551 /* If the method has default access in an other package, then
4552 issue a warning that the current method doesn't override the
4553 one that was found elsewhere. Do not issue this warning when
4554 the match was found in java.lang.Object. */
4555 if (DECL_CONTEXT (found) != object_type_node
4556 && (!aflags || (aflags > ACC_PROTECTED))
4557 && !class_in_current_package (DECL_CONTEXT (found))
4558 && flag_not_overriding)
4559 parse_warning_context
4560 (method_wfl, "Method `%s' in class `%s' does not "
4561 "override the corresponding method in class `%s', which is "
4562 "private to a different package",
4563 lang_printable_name (found, 0),
4564 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
4565 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4567 /* Inheriting multiple methods with the same signature. FIXME */
4570 /* Don't forget eventual pending found and saved_found_wfl. Take
4571 into account that we might have exited because we saw an
4572 aritifical method as the last entry. */
4574 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
4575 DECL_NAME (found) = saved_found_wfl;
4577 if (!TYPE_NVIRTUALS (class))
4578 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4580 if (!saw_constructor)
4582 /* No constructor seen, we craft one, at line 0. Since this
4583 operation takes place after we laid methods out
4584 (layout_class_methods), we prepare the its DECL
4585 appropriately. */
4586 int flags;
4587 tree decl;
4589 /* If the class is declared PUBLIC, the default constructor is
4590 PUBLIC otherwise it has default access implied by no access
4591 modifiers. */
4592 flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
4593 ACC_PUBLIC : 0);
4594 decl = create_artificial_method (class, flags, void_type_node,
4595 init_identifier_node, end_params_node);
4596 DECL_CONSTRUCTOR_P (decl) = 1;
4597 layout_class_method (TREE_TYPE (class_decl), NULL_TREE, decl, NULL_TREE);
4601 /* Return a non zero value if the `throws' clause of METHOD (if any)
4602 is incompatible with the `throws' clause of FOUND (if any). */
4604 static void
4605 check_throws_clauses (method, method_wfl, found)
4606 tree method, method_wfl, found;
4608 tree mthrows, fthrows;
4610 /* Can't check these things with class loaded from bytecode. FIXME */
4611 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
4612 return;
4614 for (mthrows = DECL_FUNCTION_THROWS (method);
4615 mthrows; mthrows = TREE_CHAIN (mthrows))
4617 /* We don't verify unchecked expressions */
4618 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
4619 continue;
4620 /* Checked expression must be compatible */
4621 for (fthrows = DECL_FUNCTION_THROWS (found);
4622 fthrows; fthrows = TREE_CHAIN (fthrows))
4623 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
4624 break;
4625 if (!fthrows)
4627 parse_error_context
4628 (method_wfl, "Invalid checked exception class `%s' in "
4629 "`throws' clause. The exception must be a subclass of an "
4630 "exception thrown by `%s' from class `%s'",
4631 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
4632 lang_printable_name (found, 0),
4633 IDENTIFIER_POINTER
4634 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4639 /* Check abstract method of interface INTERFACE */
4641 static void
4642 java_check_abstract_methods (interface_decl)
4643 tree interface_decl;
4645 int i, n;
4646 tree method, basetype_vec, found;
4647 tree interface = TREE_TYPE (interface_decl);
4649 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
4651 tree method_wfl = DECL_NAME (method);
4653 /* 2- Check for double definition inside the defining interface */
4654 if (check_method_redefinition (interface, method))
4655 continue;
4657 /* 3- Overriding is OK as far as we preserve the return type and
4658 the thrown exceptions (FIXME) */
4659 found = lookup_java_interface_method2 (interface, method);
4660 if (found)
4662 char *t;
4663 tree saved_found_wfl = DECL_NAME (found);
4664 reset_method_name (found);
4665 t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
4666 parse_error_context
4667 (method_wfl,
4668 "Method `%s' was defined with return type `%s' in class `%s'",
4669 lang_printable_name (found, 0), t,
4670 IDENTIFIER_POINTER
4671 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4672 free (t);
4673 continue;
4675 DECL_NAME (found) = saved_found_wfl;
4679 /* 4- Inherited methods can't differ by their returned types */
4680 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
4681 return;
4682 n = TREE_VEC_LENGTH (basetype_vec);
4683 for (i = 0; i < n; i++)
4685 tree sub_interface_method, sub_interface;
4686 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4687 if (!vec_elt)
4688 continue;
4689 sub_interface = BINFO_TYPE (vec_elt);
4690 for (sub_interface_method = TYPE_METHODS (sub_interface);
4691 sub_interface_method;
4692 sub_interface_method = TREE_CHAIN (sub_interface_method))
4694 found = lookup_java_interface_method2 (interface,
4695 sub_interface_method);
4696 if (found && (found != sub_interface_method))
4698 tree saved_found_wfl = DECL_NAME (found);
4699 reset_method_name (found);
4700 parse_error_context
4701 (lookup_cl (sub_interface_method),
4702 "Interface `%s' inherits method `%s' from interface `%s'. "
4703 "This method is redefined with a different return type in "
4704 "interface `%s'",
4705 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
4706 lang_printable_name (found, 0),
4707 IDENTIFIER_POINTER
4708 (DECL_NAME (TYPE_NAME
4709 (DECL_CONTEXT (sub_interface_method)))),
4710 IDENTIFIER_POINTER
4711 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4712 DECL_NAME (found) = saved_found_wfl;
4718 /* Lookup methods in interfaces using their name and partial
4719 signature. Return a matching method only if their types differ. */
4721 static tree
4722 lookup_java_interface_method2 (class, method_decl)
4723 tree class, method_decl;
4725 int i, n;
4726 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
4728 if (!basetype_vec)
4729 return NULL_TREE;
4731 n = TREE_VEC_LENGTH (basetype_vec);
4732 for (i = 0; i < n; i++)
4734 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
4735 if ((BINFO_TYPE (vec_elt) != object_type_node)
4736 && (to_return =
4737 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
4738 return to_return;
4740 for (i = 0; i < n; i++)
4742 to_return = lookup_java_interface_method2
4743 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
4744 if (to_return)
4745 return to_return;
4748 return NULL_TREE;
4751 /* Lookup method using their name and partial signature. Return a
4752 matching method only if their types differ. */
4754 static tree
4755 lookup_java_method2 (clas, method_decl, do_interface)
4756 tree clas, method_decl;
4757 int do_interface;
4759 tree method, method_signature, method_name, method_type, name;
4761 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
4762 name = DECL_NAME (method_decl);
4763 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4764 EXPR_WFL_NODE (name) : name);
4765 method_type = TREE_TYPE (TREE_TYPE (method_decl));
4767 while (clas != NULL_TREE)
4769 for (method = TYPE_METHODS (clas);
4770 method != NULL_TREE; method = TREE_CHAIN (method))
4772 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
4773 tree name = DECL_NAME (method);
4774 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4775 EXPR_WFL_NODE (name) : name) == method_name
4776 && method_sig == method_signature
4777 && TREE_TYPE (TREE_TYPE (method)) != method_type)
4778 return method;
4780 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
4782 return NULL_TREE;
4785 /* Return the line that matches DECL line number. Used during error
4786 report */
4788 static tree
4789 lookup_cl (decl)
4790 tree decl;
4792 static tree cl = NULL_TREE;
4794 if (!decl)
4795 return NULL_TREE;
4797 if (cl == NULL_TREE)
4798 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
4800 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
4801 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
4803 return cl;
4806 /* Look for a simple name in the single-type import list */
4808 static tree
4809 find_name_in_single_imports (name)
4810 tree name;
4812 tree node;
4814 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
4815 if (TREE_VALUE (node) == name)
4816 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
4818 return NULL_TREE;
4821 /* Process all single-type import. */
4823 static int
4824 process_imports ()
4826 tree import;
4827 int error_found;
4829 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4831 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
4833 /* Don't load twice something already defined. */
4834 if (IDENTIFIER_CLASS_VALUE (to_be_found))
4835 continue;
4836 QUALIFIED_P (to_be_found) = 1;
4837 load_class (to_be_found, 0);
4838 error_found =
4839 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
4840 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
4842 parse_error_context (TREE_PURPOSE (import),
4843 "Class or interface `%s' not found in import",
4844 IDENTIFIER_POINTER (to_be_found));
4845 return 1;
4847 if (error_found)
4848 return 1;
4850 return 0;
4853 /* Possibly find a class imported by a single-type import statement. Return
4854 1 if an error occured, 0 otherwise. */
4856 static int
4857 find_in_imports (class_type)
4858 tree class_type;
4860 tree import;
4862 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4863 if (TREE_VALUE (import) == TYPE_NAME (class_type))
4865 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
4866 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
4868 return 0;
4871 static int
4872 note_possible_classname (name, len)
4873 char *name;
4874 int len;
4876 tree node;
4877 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
4878 len = len - 5;
4879 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
4880 len = len - 6;
4881 else
4882 return 0;
4883 node = ident_subst (name, len, "", '/', '.', "");
4884 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
4885 QUALIFIED_P (node) = 1; /* As soon as we turn / into . */
4886 return 1;
4889 /* Read a import directory, gathering potential match for further type
4890 references. Indifferently reads a filesystem or a ZIP archive
4891 directory. */
4893 static void
4894 read_import_dir (wfl)
4895 tree wfl;
4897 tree package_id = EXPR_WFL_NODE (wfl);
4898 char *package_name = IDENTIFIER_POINTER (package_id);
4899 int package_length = IDENTIFIER_LENGTH (package_id);
4900 DIR *dirp = NULL;
4901 JCF jcfr, *jcf, *saved_jcf = current_jcf;
4903 int found = 0;
4904 int k;
4905 void *entry;
4906 struct buffer filename[1];
4909 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
4910 return;
4911 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
4913 BUFFER_INIT (filename);
4914 buffer_grow (filename, package_length + 100);
4916 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
4918 char *entry_name = jcf_path_name (entry);
4919 int entry_length = strlen (entry_name);
4920 if (jcf_path_is_zipfile (entry))
4922 ZipFile *zipf;
4923 buffer_grow (filename, entry_length);
4924 memcpy (filename->data, entry_name, entry_length - 1);
4925 filename->data[entry_length-1] = '\0';
4926 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
4927 if (zipf == NULL)
4928 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
4929 else
4931 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
4932 BUFFER_RESET (filename);
4933 for (k = 0; k < package_length; k++)
4935 char ch = package_name[k];
4936 *filename->ptr++ = ch == '.' ? '/' : ch;
4938 *filename->ptr++ = '/';
4940 for (; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
4942 char *current_entry = ZIPDIR_FILENAME (zipd);
4943 int current_entry_len = zipd->filename_length;
4945 if (strncmp (filename->data, current_entry,
4946 BUFFER_LENGTH (filename)) != 0)
4947 continue;
4948 found += note_possible_classname (current_entry,
4949 current_entry_len);
4953 else
4955 BUFFER_RESET (filename);
4956 buffer_grow (filename, entry_length + package_length + 4);
4957 strcpy (filename->data, entry_name);
4958 filename->ptr = filename->data + entry_length;
4959 for (k = 0; k < package_length; k++)
4961 char ch = package_name[k];
4962 *filename->ptr++ = ch == '.' ? '/' : ch;
4964 *filename->ptr = '\0';
4966 dirp = opendir (filename->data);
4967 if (dirp == NULL)
4968 continue;
4969 *filename->ptr++ = '/';
4970 for (;;)
4972 int java_or_class = 0;
4973 int len;
4974 char *d_name;
4975 struct dirent *direntp = readdir (dirp);
4976 if (!direntp)
4977 break;
4978 d_name = direntp->d_name;
4979 len = strlen (direntp->d_name);
4980 buffer_grow (filename, len+1);
4981 strcpy (filename->ptr, d_name);
4982 found += note_possible_classname (filename->data + entry_length,
4983 package_length+len+1);
4985 if (dirp)
4986 closedir (dirp);
4990 free (filename->data);
4992 /* Here we should have a unified way of retrieving an entry, to be
4993 indexed. */
4994 if (!found)
4996 static int first = 1;
4997 if (first)
4999 char buffer [256];
5000 sprintf (buffer, "Can't find default package `%s'. Check "
5001 "the CLASSPATH environment variable and the access to the "
5002 "archives.", package_name);
5003 error (buffer);
5004 java_error_count++;
5005 first = 0;
5007 else
5008 parse_error_context (wfl, "Package `%s' not found in import",
5009 package_name);
5010 current_jcf = saved_jcf;
5011 return;
5013 current_jcf = saved_jcf;
5016 /* Possibly find a type in the import on demands specified
5017 types. Returns 1 if an error occured, 0 otherwise. Run throught the
5018 entire list, to detected potential double definitions. */
5020 static int
5021 find_in_imports_on_demand (class_type)
5022 tree class_type;
5024 tree node, import, node_to_use;
5025 int seen_once = -1;
5026 tree cl;
5028 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
5030 char *id_name;
5031 obstack_grow (&temporary_obstack,
5032 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
5033 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5034 obstack_1grow (&temporary_obstack, '.');
5035 obstack_grow0 (&temporary_obstack,
5036 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5037 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
5038 id_name = obstack_finish (&temporary_obstack);
5040 node = maybe_get_identifier (id_name);
5041 if (node && IS_A_CLASSFILE_NAME (node))
5043 if (seen_once < 0)
5045 cl = TREE_PURPOSE (import);
5046 seen_once = 1;
5047 node_to_use = node;
5049 else
5051 seen_once++;
5052 parse_error_context
5053 (import, "Type `%s' also potentially defined in package `%s'",
5054 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5055 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5060 if (seen_once == 1)
5062 /* Setup lineno so that it refers to the line of the import (in
5063 case we parse a class file and encounter errors */
5064 tree decl;
5065 int saved_lineno = lineno;
5066 lineno = EXPR_WFL_LINENO (cl);
5067 TYPE_NAME (class_type) = node_to_use;
5068 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
5069 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5070 /* If there is no DECL set for the class or if the class isn't
5071 loaded and not seen in source yet, the load */
5072 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
5073 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
5074 load_class (node_to_use, 0);
5075 lineno = saved_lineno;
5076 return check_pkg_class_access (TYPE_NAME (class_type), cl);
5078 else
5079 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
5082 static tree
5083 resolve_package (pkg, next)
5084 tree pkg, *next;
5086 tree type_name = NULL_TREE;
5087 char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5089 /* The trick is to determine when the package name stops and were
5090 the name of something contained in the package starts. Then we
5091 return a fully qualified name of what we want to get. */
5093 /* Do a quick search on well known package names */
5094 if (!strncmp (name, "java.lang.reflect", 17))
5096 *next =
5097 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
5098 type_name = lookup_package_type (name, 17);
5100 else if (!strncmp (name, "java.lang", 9))
5102 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
5103 type_name = lookup_package_type (name, 9);
5105 else
5106 return NULL_TREE; /* FIXME, search all imported packages. */
5108 return type_name;
5111 static tree
5112 lookup_package_type (name, from)
5113 char *name;
5114 int from;
5116 char subname [128];
5117 char *sub = &name[from+1];
5118 while (*sub != '.' && *sub)
5119 sub++;
5120 strncpy (subname, name, sub-name);
5121 subname [sub-name] = '\0';
5122 return get_identifier (subname);
5125 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
5126 access violations were found, 1 otherwise. */
5128 static int
5129 check_pkg_class_access (class_name, cl)
5130 tree class_name;
5131 tree cl;
5133 tree type;
5135 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
5136 return 0;
5138 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
5139 return 0;
5141 if (!CLASS_PUBLIC (TYPE_NAME (type)))
5143 parse_error_context
5144 (cl, "Can't access %s `%s'. Only public classes and interfaces in "
5145 "other packages can be accessed",
5146 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
5147 IDENTIFIER_POINTER (class_name));
5148 return 1;
5150 return 0;
5153 /* Local variable declaration. */
5155 static void
5156 declare_local_variables (modifier, type, vlist)
5157 int modifier;
5158 tree type;
5159 tree vlist;
5161 tree decl, current, saved_type;
5162 tree type_wfl = NULL_TREE;
5163 int must_chain = 0;
5165 /* Push a new block if statement were seen between the last time we
5166 pushed a block and now. Keep a cound of block to close */
5167 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)))
5169 tree body = DECL_FUNCTION_BODY (current_function_decl);
5170 tree b = enter_block ();
5171 BLOCK_EXPR_ORIGIN(b) = body;
5174 if (modifier)
5176 int i;
5177 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
5178 if (modifier == ACC_FINAL)
5180 if (flag_static_local_jdk1_1)
5181 parse_warning_context (ctxp->modifier_ctx [i],
5182 "Unsupported JDK1.1 `final' local variable "
5183 "(treated as non final)");
5185 else
5187 parse_error_context
5188 (ctxp->modifier_ctx [i],
5189 "Only `final' is allowed as a local variables modifier");
5190 return;
5194 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
5195 hold the TYPE value if a new incomplete has to be created (as
5196 opposed to being found already existing and reused). */
5197 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5199 /* If TYPE is fully resolved and we don't have a reference, make one */
5200 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5202 /* Go through all the declared variables */
5203 for (current = vlist, saved_type = type; current;
5204 current = TREE_CHAIN (current), type = saved_type)
5206 tree other, real_type;
5207 tree wfl = TREE_PURPOSE (current);
5208 tree name = EXPR_WFL_NODE (wfl);
5209 tree init = TREE_VALUE (current);
5211 /* Process NAME, as it may specify extra dimension(s) for it */
5212 type = build_array_from_name (type, type_wfl, name, &name);
5214 /* Variable redefinition check */
5215 if ((other = lookup_name_in_blocks (name)))
5217 variable_redefinition_error (wfl, name, TREE_TYPE (other),
5218 DECL_SOURCE_LINE (other));
5219 continue;
5222 /* Type adjustment. We may have just readjusted TYPE because
5223 the variable specified more dimensions. Make sure we have
5224 a reference if we can and don't have one already. */
5225 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5227 real_type = GET_REAL_TYPE (type);
5228 /* Never layout this decl. This will be done when its scope
5229 will be entered */
5230 decl = build_decl (VAR_DECL, name, real_type);
5231 BLOCK_CHAIN_DECL (decl);
5233 /* Don't try to use an INIT statement when an error was found */
5234 if (init && java_error_count)
5235 init = NULL_TREE;
5237 /* Add the initialization function to the current function's code */
5238 if (init)
5240 /* Name might have been readjusted */
5241 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
5242 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
5243 java_method_add_stmt (current_function_decl,
5244 build_debugable_stmt (EXPR_WFL_LINECOL (init),
5245 init));
5248 /* Setup dependency the type of the decl */
5249 if (must_chain)
5251 jdep *dep;
5252 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
5253 dep = CLASSD_LAST (ctxp->classd_list);
5254 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
5257 SOURCE_FRONTEND_DEBUG (("Defined locals"));
5260 /* Called during parsing. Build decls from argument list. */
5262 static void
5263 source_start_java_method (fndecl)
5264 tree fndecl;
5266 tree tem;
5267 tree parm_decl;
5268 int i;
5270 current_function_decl = fndecl;
5272 /* New scope for the function */
5273 enter_block ();
5274 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
5275 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
5277 tree type = TREE_VALUE (tem);
5278 tree name = TREE_PURPOSE (tem);
5280 /* If type is incomplete. Create an incomplete decl and ask for
5281 the decl to be patched later */
5282 if (INCOMPLETE_TYPE_P (type))
5284 jdep *jdep;
5285 tree real_type = GET_REAL_TYPE (type);
5286 parm_decl = build_decl (PARM_DECL, name, real_type);
5287 type = obtain_incomplete_type (type);
5288 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
5289 jdep = CLASSD_LAST (ctxp->classd_list);
5290 JDEP_MISC (jdep) = name;
5291 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
5293 else
5294 parm_decl = build_decl (PARM_DECL, name, type);
5296 BLOCK_CHAIN_DECL (parm_decl);
5298 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5299 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
5300 nreverse (tem);
5301 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
5304 /* Called during parsing. Creates an artificial method declaration. */
5306 static tree
5307 create_artificial_method (class, flags, type, name, args)
5308 tree class;
5309 int flags;
5310 tree type, name, args;
5312 int saved_lineno = lineno;
5313 tree mdecl;
5315 lineno = 0;
5316 mdecl = make_node (FUNCTION_TYPE);
5317 TREE_TYPE (mdecl) = type;
5318 TYPE_ARG_TYPES (mdecl) = args;
5319 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
5320 lineno = saved_lineno;
5321 DECL_ARTIFICIAL (mdecl) = 1;
5322 return mdecl;
5325 /* Starts the body if an artifical method. */
5327 static void
5328 start_artificial_method_body (mdecl)
5329 tree mdecl;
5331 DECL_SOURCE_LINE (mdecl) = 1;
5332 DECL_SOURCE_LINE_MERGE (mdecl, 1);
5333 source_start_java_method (mdecl);
5334 enter_block ();
5337 static void
5338 end_artificial_method_body (mdecl)
5339 tree mdecl;
5341 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
5342 exit_block ();
5345 /* Called during expansion. Push decls formerly built from argument
5346 list so they're usable during expansion. */
5348 static void
5349 expand_start_java_method (fndecl)
5350 tree fndecl;
5352 tree tem, *ptr;
5354 current_function_decl = fndecl;
5356 announce_function (fndecl);
5357 pushlevel (1); /* Push parameters */
5358 ptr = &DECL_ARGUMENTS (fndecl);
5359 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5360 while (tem)
5362 tree next = TREE_CHAIN (tem);
5363 tree type = TREE_TYPE (tem);
5364 #ifdef PROMOTE_PROTOTYPES
5365 if (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
5366 && INTEGRAL_TYPE_P (type))
5367 type = integer_type_node;
5368 #endif
5369 DECL_ARG_TYPE (tem) = type;
5370 layout_decl (tem, 0);
5371 pushdecl (tem);
5372 INITIALIZED_P (tem) = 1; /* Parms are initialized */
5373 *ptr = tem;
5374 ptr = &TREE_CHAIN (tem);
5375 tem = next;
5377 *ptr = NULL_TREE;
5378 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5379 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
5380 complete_start_java_method (fndecl);
5383 /* Terminate a function and expand its body. */
5385 static void
5386 source_end_java_method ()
5388 tree fndecl = current_function_decl;
5390 java_parser_context_save_global ();
5391 lineno = ctxp->last_ccb_indent1;
5393 /* Set EH language codes */
5394 java_set_exception_lang_code ();
5396 /* Generate function's code */
5397 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
5398 && ! flag_emit_class_files)
5399 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
5401 /* pop out of its parameters */
5402 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5403 poplevel (1, 0, 1);
5404 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
5406 /* Generate rtl for function exit. */
5407 if (! flag_emit_class_files)
5409 lineno = DECL_SOURCE_LINE_LAST (fndecl);
5410 /* Emit catch-finally clauses */
5411 emit_handlers ();
5412 expand_function_end (input_filename, lineno, 0);
5414 /* Run the optimizers and output assembler code for this function. */
5415 rest_of_compilation (fndecl);
5418 current_function_decl = NULL_TREE;
5419 /* permanent_allocation (1); */
5420 java_parser_context_restore_global ();
5423 /* Record EXPR in the current function block. Complements compound
5424 expression second operand if necessary. */
5426 tree
5427 java_method_add_stmt (fndecl, expr)
5428 tree fndecl, expr;
5430 return add_stmt_to_block (DECL_FUNCTION_BODY (fndecl), NULL_TREE, expr);
5433 static tree
5434 add_stmt_to_block (b, type, stmt)
5435 tree b, type, stmt;
5437 tree body = BLOCK_EXPR_BODY (b), c;
5439 if (java_error_count)
5440 return body;
5442 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
5443 return body;
5445 BLOCK_EXPR_BODY (b) = c;
5446 TREE_SIDE_EFFECTS (c) = 1;
5447 return c;
5450 /* Add STMT to EXISTING if possible, otherwise create a new
5451 COMPOUND_EXPR and add STMT to it. */
5453 static tree
5454 add_stmt_to_compound (existing, type, stmt)
5455 tree existing, type, stmt;
5457 if (existing)
5458 return build (COMPOUND_EXPR, type, existing, stmt);
5459 else
5460 return stmt;
5463 /* Hold THIS for the scope of the current public method decl. */
5464 static tree current_this;
5466 void java_layout_seen_class_methods ()
5468 tree previous_list = all_class_list;
5469 tree end = NULL_TREE;
5470 tree current;
5472 while (1)
5474 for (current = previous_list;
5475 current != end; current = TREE_CHAIN (current))
5476 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
5478 if (previous_list != all_class_list)
5480 end = previous_list;
5481 previous_list = all_class_list;
5483 else
5484 break;
5488 /* Layout the methods of all classes loaded in one way on an
5489 other. Check methods of source parsed classes. Then reorder the
5490 fields and layout the classes or the type of all source parsed
5491 classes */
5493 void
5494 java_layout_classes ()
5496 tree current;
5498 /* Layout the methods of all classes seen so far */
5499 java_layout_seen_class_methods ();
5500 java_parse_abort_on_error ();
5501 all_class_list = NULL_TREE;
5503 /* Then check the methods of all parsed classes */
5504 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5505 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
5506 CHECK_METHODS (TREE_VALUE (current));
5507 java_parse_abort_on_error ();
5509 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5511 current_class = TREE_TYPE (TREE_VALUE (current));
5513 /* Reverse the fields, but leave the dummy field in front.
5514 Fields are already ordered for Object and Class */
5515 if (TYPE_FIELDS (current_class) && current_class != object_type_node
5516 && current_class != class_type_node)
5518 /* If the dummy field is there, reverse the right fields and
5519 just layout the type for proper fields offset */
5520 if (!DECL_NAME (TYPE_FIELDS (current_class)))
5522 tree fields = TYPE_FIELDS (current_class);
5523 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
5524 TYPE_SIZE (current_class) = NULL_TREE;
5525 layout_type (current_class);
5527 /* We don't have a dummy field, we need to layout the class,
5528 after having reversed the fields */
5529 else
5531 TYPE_FIELDS (current_class) =
5532 nreverse (TYPE_FIELDS (current_class));
5533 TYPE_SIZE (current_class) = NULL_TREE;
5534 layout_class (current_class);
5537 else
5538 layout_class (current_class);
5540 /* From now on, the class is considered completely loaded */
5541 CLASS_LOADED_P (current_class) = 1;
5543 /* Error reported by the caller */
5544 if (java_error_count)
5545 return;
5548 /* We might have reloaded classes durign the process of laying out
5549 classes for code generation. We must layout the methods of those
5550 late additions, as constructor checks might use them */
5551 java_layout_seen_class_methods ();
5552 java_parse_abort_on_error ();
5555 /* Expand all methods in all registered classes. */
5557 void
5558 java_complete_expand_methods ()
5560 tree current;
5562 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5564 tree class_type = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current));
5565 tree decl;
5567 current_class = TREE_TYPE (current);
5569 /* Initialize a new constant pool */
5570 init_outgoing_cpool ();
5572 /* We want <clinit> (if any) to be processed first. */
5573 decl = tree_last (TYPE_METHODS (class_type));
5574 if (decl && DECL_NAME (decl) == clinit_identifier_node)
5576 tree list = nreverse (TYPE_METHODS (class_type));
5577 list = TREE_CHAIN (list);
5578 TREE_CHAIN (decl) = NULL_TREE;
5579 TYPE_METHODS (class_type) = chainon (decl, nreverse (list));
5582 /* Don't process function bodies in interfaces */
5583 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5584 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5586 current_function_decl = decl;
5587 /* Don't generate debug info on line zero when expanding a
5588 generated constructor. */
5589 if (DECL_CONSTRUCTOR_P (decl) && !DECL_FUNCTION_BODY (decl))
5591 /* If we found errors, it's too dangerous to try to generate
5592 and expand a constructor */
5593 if (!java_error_count)
5595 restore_line_number_status (1);
5596 java_complete_expand_method (decl);
5597 restore_line_number_status (0);
5600 else if (METHOD_ABSTRACT (decl) || METHOD_NATIVE (decl))
5601 continue;
5602 else
5603 java_complete_expand_method (decl);
5606 /* Now verify constructor circularity (stop after the first one
5607 we find) */
5608 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5609 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5610 if (DECL_CONSTRUCTOR_P (decl) &&
5611 verify_constructor_circularity (decl, decl))
5612 break;
5614 /* Make the class data, register it and run the rest of decl
5615 compilation on it */
5616 if (!java_error_count)
5618 if (flag_emit_class_files)
5619 write_classfile (current_class);
5620 else
5621 finish_class (current_class);
5626 /* Hold a list of catch clauses list. The first element of this list is
5627 the list of the catch clauses of the currently analysed try block. */
5628 static tree currently_caught_type_list;
5630 /* Complete and expand a method. */
5632 static void
5633 java_complete_expand_method (mdecl)
5634 tree mdecl;
5636 /* Fix constructors before expanding them */
5637 if (DECL_CONSTRUCTOR_P (mdecl))
5638 fix_constructors (mdecl);
5640 /* Expand functions that have a body */
5641 if (DECL_FUNCTION_BODY (mdecl))
5643 tree fbody = DECL_FUNCTION_BODY (mdecl);
5644 tree block_body = BLOCK_EXPR_BODY (fbody);
5645 expand_start_java_method (mdecl);
5647 current_this
5648 = (!METHOD_STATIC (mdecl) ?
5649 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
5651 /* Purge the `throws' list of unchecked exceptions */
5652 purge_unchecked_exceptions (mdecl);
5654 /* Install exceptions thrown with `throws' */
5655 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
5657 if (block_body != NULL_TREE)
5658 block_body = java_complete_tree (block_body);
5659 BLOCK_EXPR_BODY (fbody) = block_body;
5661 if ((block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
5662 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
5663 missing_return_error (current_function_decl);
5665 /* Don't go any further if we've found error(s) during the
5666 expansion */
5667 if (!java_error_count)
5668 source_end_java_method ();
5669 else
5671 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
5672 poplevel (1, 0, 1);
5675 /* Pop the exceptions and sanity check */
5676 POP_EXCEPTIONS();
5677 if (currently_caught_type_list)
5678 fatal ("Exception list non empty - java_complete_expand_method");
5682 /* Craft a body for default constructor. Patch existing constructor
5683 bodies with call to super() and field initialization statements if
5684 necessary. */
5686 static void
5687 fix_constructors (mdecl)
5688 tree mdecl;
5690 tree body = DECL_FUNCTION_BODY (mdecl);
5692 if (!body)
5694 /* The constructor body must be crafted by hand. It's the
5695 constructor we defined when we realize we didn't have the
5696 CLASSNAME() constructor */
5698 tree compound;
5700 /* It is an error for the compiler to generate a default
5701 constructor if the superclass doesn't have a constructor that
5702 takes no argument */
5703 if (verify_constructor_super ())
5705 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (current_class));
5706 char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
5707 parse_error_context (lookup_cl (TYPE_NAME (current_class)),
5708 "No constructor matching `%s()' found in "
5709 "class `%s'", n, n);
5712 start_artificial_method_body (mdecl);
5714 /* We don't generate a super constructor invocation if we're
5715 compiling java.lang.Object. build_super_invocation takes care
5716 of that. */
5717 compound = java_method_add_stmt (mdecl, build_super_invocation ());
5719 end_artificial_method_body (mdecl);
5721 /* Search for an explicit constructor invocation */
5722 else
5724 int found = 0;
5725 tree main_block = BLOCK_EXPR_BODY (body);
5726 tree compound = NULL_TREE;
5728 while (body)
5729 switch (TREE_CODE (body))
5731 case CALL_EXPR:
5732 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
5733 body = NULL_TREE;
5734 break;
5735 case COMPOUND_EXPR:
5736 case EXPR_WITH_FILE_LOCATION:
5737 body = TREE_OPERAND (body, 0);
5738 break;
5739 case BLOCK:
5740 body = BLOCK_EXPR_BODY (body);
5741 break;
5742 default:
5743 found = 0;
5744 body = NULL_TREE;
5746 /* The constructor is missing an invocation of super() */
5747 if (!found)
5748 compound = add_stmt_to_compound (compound, NULL_TREE,
5749 build_super_invocation ());
5751 /* Fix the constructor main block if we're adding extra stmts */
5752 if (compound)
5754 compound = add_stmt_to_compound (compound, NULL_TREE,
5755 BLOCK_EXPR_BODY (main_block));
5756 BLOCK_EXPR_BODY (main_block) = compound;
5761 /* Browse constructors in the super class, searching for a constructor
5762 that doesn't take any argument. Return 0 if one is found, 1
5763 otherwise. */
5765 static int
5766 verify_constructor_super ()
5768 tree class = CLASSTYPE_SUPER (current_class);
5769 if (!class)
5770 return 0;
5772 if (class)
5774 tree mdecl;
5775 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
5777 if (DECL_CONSTRUCTOR_P (mdecl)
5778 && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (mdecl))) == end_params_node)
5779 return 0;
5782 return 1;
5785 /* Expand finals. */
5787 void
5788 java_expand_finals ()
5792 /* Generate code for all context remembered for code generation. */
5794 void
5795 java_expand_classes ()
5797 java_parse_abort_on_error ();
5798 if (!(ctxp = ctxp_for_generation))
5799 return;
5800 java_layout_classes ();
5801 java_parse_abort_on_error ();
5803 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
5805 ctxp = ctxp_for_generation;
5806 lang_init_source (2); /* Error msgs have method prototypes */
5807 java_complete_expand_methods (); /* Complete and expand method bodies */
5808 java_parse_abort_on_error ();
5809 java_expand_finals (); /* Expand and check the finals */
5810 java_parse_abort_on_error ();
5811 java_check_final (); /* Check unitialized final */
5812 java_parse_abort_on_error ();
5816 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
5817 a tree list node containing RIGHT. Fore coming RIGHTs will be
5818 chained to this hook. LOCATION contains the location of the
5819 separating `.' operator. */
5821 static tree
5822 make_qualified_primary (primary, right, location)
5823 tree primary, right;
5824 int location;
5826 tree wfl;
5828 /* We want to process THIS . xxx symbolicaly, to keep it consistent
5829 with the way we're processing SUPER. A THIS from a primary as a
5830 different form than a SUPER. Turn THIS into something symbolic */
5831 if (TREE_CODE (primary) == THIS_EXPR)
5833 wfl = build_wfl_node (this_identifier_node, input_filename, 0, 0);
5834 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5835 wfl = make_qualified_name (wfl, right, location);
5836 PRIMARY_P (wfl) = 1;
5837 return wfl;
5839 /* Other non WFL node are wrapped around a WFL */
5840 else if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
5842 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
5843 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5844 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (primary, NULL_TREE);
5846 else
5848 wfl = primary;
5849 if (!EXPR_WFL_QUALIFICATION (primary))
5850 EXPR_WFL_QUALIFICATION (primary) =
5851 build_tree_list (primary, NULL_TREE);
5854 EXPR_WFL_LINECOL (right) = location;
5855 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
5856 PRIMARY_P (wfl) = 1;
5857 return wfl;
5860 /* Simple merge of two name separated by a `.' */
5862 static tree
5863 merge_qualified_name (left, right)
5864 tree left, right;
5866 tree node;
5867 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
5868 IDENTIFIER_LENGTH (left));
5869 obstack_1grow (&temporary_obstack, '.');
5870 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
5871 IDENTIFIER_LENGTH (right));
5872 node = get_identifier (obstack_base (&temporary_obstack));
5873 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
5874 QUALIFIED_P (node) = 1;
5875 return node;
5878 /* Merge the two parts of a qualified name into LEFT. Set the
5879 location information of the resulting node to LOCATION, usually
5880 inherited from the location information of the `.' operator. */
5882 static tree
5883 make_qualified_name (left, right, location)
5884 tree left, right;
5885 int location;
5887 tree left_id = EXPR_WFL_NODE (left);
5888 tree right_id = EXPR_WFL_NODE (right);
5889 tree wfl, merge;
5891 merge = merge_qualified_name (left_id, right_id);
5893 /* Left wasn't qualified and is now qualified */
5894 if (!QUALIFIED_P (left_id))
5896 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
5897 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
5898 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
5901 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
5902 EXPR_WFL_LINECOL (wfl) = location;
5903 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
5905 EXPR_WFL_NODE (left) = merge;
5906 return left;
5909 /* Extract the last identifier component of the qualified in WFL. The
5910 last identifier is removed from the linked list */
5912 static tree
5913 cut_identifier_in_qualified (wfl)
5914 tree wfl;
5916 tree q;
5917 tree previous = NULL_TREE;
5918 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
5919 if (!TREE_CHAIN (q))
5921 if (!previous)
5922 fatal ("Operating on a non qualified qualified WFL - "
5923 "cut_identifier_in_qualified");
5924 TREE_CHAIN (previous) = NULL_TREE;
5925 return TREE_PURPOSE (q);
5929 /* Resolve the expression name NAME. Return its decl. */
5931 static tree
5932 resolve_expression_name (id, orig)
5933 tree id;
5934 tree *orig;
5936 tree name = EXPR_WFL_NODE (id);
5937 tree decl;
5939 /* 6.5.5.1: Simple expression names */
5940 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
5942 /* 15.13.1: NAME can appear within the scope of a local variable
5943 declaration */
5944 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
5945 return decl;
5947 /* 15.13.1: NAME can appear within a class declaration */
5948 else
5950 decl = lookup_field_wrapper (current_class, name);
5951 if (decl)
5953 int fs = FIELD_STATIC (decl);
5954 /* Instance variable (8.3.1.1) can't appear within
5955 static method, static initializer or initializer for
5956 a static variable. */
5957 if (!fs && METHOD_STATIC (current_function_decl))
5959 parse_error_context
5960 (id, "Can't make a static reference to nonstatic variable "
5961 "`%s' in class `%s'",
5962 IDENTIFIER_POINTER (name),
5963 IDENTIFIER_POINTER (DECL_NAME
5964 (TYPE_NAME (current_class))));
5965 return error_mark_node;
5967 /* Instance variables can't appear as an argument of
5968 an explicit constructor invocation */
5969 if (!fs && ctxp->explicit_constructor_p)
5971 parse_error_context
5972 (id, "Can't reference `%s' before the superclass "
5973 "constructor has been called", IDENTIFIER_POINTER (name));
5974 return error_mark_node;
5977 /* Otherwise build what it takes to access the field */
5978 decl = build_field_ref ((fs ? NULL_TREE : current_this),
5979 current_class, name);
5980 if (fs && !flag_emit_class_files)
5981 decl = build_class_init (current_class, decl);
5982 /* We may be asked to save the real field access node */
5983 if (orig)
5984 *orig = decl;
5985 /* And we return what we got */
5986 return decl;
5988 /* Fall down to error report on undefined variable */
5991 /* 6.5.5.2 Qualified Expression Names */
5992 else
5994 if (orig)
5995 *orig = NULL_TREE;
5996 qualify_ambiguous_name (id);
5997 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
5998 /* 15.10.2: Accessing Superclass Members using super */
5999 return resolve_field_access (id, NULL, NULL);
6002 /* We've got an error here */
6003 parse_error_context (id, "Undefined variable `%s'",
6004 IDENTIFIER_POINTER (name));
6006 return error_mark_node;
6009 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
6010 We return something suitable to generate the field access. We also
6011 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
6012 recipient's address can be null. */
6014 static tree
6015 resolve_field_access (qual_wfl, field_decl, field_type)
6016 tree qual_wfl;
6017 tree *field_decl, *field_type;
6019 int is_static = 0;
6020 tree field_ref;
6021 tree decl, where_found, type_found;
6023 if (resolve_qualified_expression_name (qual_wfl, &decl,
6024 &where_found, &type_found))
6025 return error_mark_node;
6027 /* Resolve the LENGTH field of an array here */
6028 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
6029 && ! flag_emit_class_files)
6031 tree length = build_java_array_length_access (where_found);
6032 field_ref =
6033 build_java_arraynull_check (type_found, length, int_type_node);
6035 /* We might have been trying to resolve field.method(). In which
6036 case, the resolution is over and decl is the answer */
6037 else if (DECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
6038 field_ref = decl;
6039 else if (DECL_P (decl))
6041 int static_final_found = 0;
6042 if (!type_found)
6043 type_found = DECL_CONTEXT (decl);
6044 is_static = DECL_P (decl) && FIELD_STATIC (decl);
6045 if (FIELD_FINAL (decl)
6046 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
6047 && DECL_LANG_SPECIFIC (decl)
6048 && DECL_INITIAL (decl))
6050 field_ref = DECL_INITIAL (decl);
6051 static_final_found = 1;
6053 else
6054 field_ref = build_field_ref ((is_static ? NULL_TREE : where_found),
6055 type_found, DECL_NAME (decl));
6056 if (field_ref == error_mark_node)
6057 return error_mark_node;
6058 if (is_static && !static_final_found && !flag_emit_class_files)
6060 field_ref = build_class_init (type_found, field_ref);
6061 /* If the static field was identified by an expression that
6062 needs to be generated, make the field access a compound
6063 expression whose first part of the evaluation of the
6064 field selector part. */
6065 if (where_found && TREE_CODE (where_found) != TYPE_DECL
6066 && TREE_CODE (where_found) != RECORD_TYPE)
6068 tree type = QUAL_DECL_TYPE (field_ref);
6069 field_ref = build (COMPOUND_EXPR, type, where_found, field_ref);
6073 else
6074 field_ref = decl;
6076 if (field_decl)
6077 *field_decl = decl;
6078 if (field_type)
6079 *field_type = (QUAL_DECL_TYPE (decl) ?
6080 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
6081 return field_ref;
6084 /* 6.5.5.2: Qualified Expression Names */
6086 static int
6087 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
6088 tree wfl;
6089 tree *found_decl, *type_found, *where_found;
6091 int from_type = 0; /* Field search initiated from a type */
6092 int from_super = 0, from_cast = 0;
6093 int previous_call_static = 0;
6094 int is_static;
6095 tree decl = NULL_TREE, type = NULL_TREE, q;
6096 *type_found = *where_found = NULL_TREE;
6098 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
6100 tree qual_wfl = QUAL_WFL (q);
6102 /* 15.10.1 Field Access Using a Primary */
6103 switch (TREE_CODE (qual_wfl))
6105 case CALL_EXPR:
6106 case NEW_CLASS_EXPR:
6107 /* If the access to the function call is a non static field,
6108 build the code to access it. */
6109 if (DECL_P (decl) && !FIELD_STATIC (decl))
6111 decl = maybe_access_field (decl, *where_found,
6112 DECL_CONTEXT (decl));
6113 if (decl == error_mark_node)
6114 return 1;
6116 /* And code for the function call */
6117 if (complete_function_arguments (qual_wfl))
6118 return 1;
6119 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
6120 CALL_USING_SUPER (qual_wfl) = 1;
6121 *where_found =
6122 patch_method_invocation (qual_wfl, decl, type, &is_static, NULL);
6123 if (*where_found == error_mark_node)
6124 return 1;
6125 *type_found = type = QUAL_DECL_TYPE (*where_found);
6127 /* If the previous call was static and this one is too,
6128 build a compound expression to hold the two (because in
6129 that case, previous function calls aren't transported as
6130 forcoming function's argument. */
6131 if (previous_call_static && is_static)
6133 decl = build (COMPOUND_EXPR, type, decl, *where_found);
6134 TREE_SIDE_EFFECTS (decl) = 1;
6136 else
6138 previous_call_static = is_static;
6139 decl = *where_found;
6141 continue;
6143 case CONVERT_EXPR:
6144 *where_found = decl = java_complete_tree (qual_wfl);
6145 if (decl == error_mark_node)
6146 return 1;
6147 *type_found = type = QUAL_DECL_TYPE (decl);
6148 from_cast = 1;
6149 continue;
6151 case CONDITIONAL_EXPR:
6152 case STRING_CST:
6153 *where_found = decl = java_complete_tree (qual_wfl);
6154 if (decl == error_mark_node)
6155 return 1;
6156 *type_found = type = QUAL_DECL_TYPE (decl);
6157 continue;
6159 case ARRAY_REF:
6160 /* If the access to the function call is a non static field,
6161 build the code to access it. */
6162 if (DECL_P (decl) && !FIELD_STATIC (decl))
6164 decl = maybe_access_field (decl, *where_found, type);
6165 if (decl == error_mark_node)
6166 return 1;
6168 /* And code for the array reference expression */
6169 decl = java_complete_tree (qual_wfl);
6170 if (decl == error_mark_node)
6171 return 1;
6172 type = QUAL_DECL_TYPE (decl);
6173 continue;
6175 default:
6176 /* Fix for -Wall Just go to the next statement. Don't
6177 continue */
6180 /* If we fall here, we weren't processing a (static) function call. */
6181 previous_call_static = 0;
6183 /* It can be the keyword THIS */
6184 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
6186 if (!current_this)
6188 parse_error_context
6189 (wfl, "Keyword `this' used outside allowed context");
6190 return 1;
6192 /* We have to generate code for intermediate acess */
6193 *where_found = decl = current_this;
6194 *type_found = type = QUAL_DECL_TYPE (decl);
6195 continue;
6198 /* 15.10.2 Accessing Superclass Members using SUPER */
6199 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
6201 tree node;
6202 /* Check on the restricted use of SUPER */
6203 if (METHOD_STATIC (current_function_decl)
6204 || current_class == object_type_node)
6206 parse_error_context
6207 (wfl, "Keyword `super' used outside allowed context");
6208 return 1;
6210 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
6211 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
6212 CLASSTYPE_SUPER (current_class),
6213 build_this (EXPR_WFL_LINECOL (qual_wfl)));
6214 *where_found = decl = java_complete_tree (node);
6215 if (decl == error_mark_node)
6216 return 1;
6217 *type_found = type = QUAL_DECL_TYPE (decl);
6218 from_super = from_type = 1;
6219 continue;
6222 /* 15.13.1: Can't search for field name in packages, so we
6223 assume a variable/class name was meant. */
6224 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
6226 tree name = resolve_package (wfl, &q);
6227 if (name)
6229 *where_found = decl = resolve_no_layout (name, qual_wfl);
6230 /* We wan't to be absolutely that the class is laid
6231 out. We're going to search something inside it. */
6232 *type_found = type = TREE_TYPE (decl);
6233 layout_class (type);
6234 from_type = 1;
6235 /* Should be a list, really. FIXME */
6236 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 1;
6237 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 0;
6239 else
6241 if (from_super || from_cast)
6242 parse_error_context
6243 ((from_cast ? qual_wfl : wfl),
6244 "No variable `%s' defined in class `%s'",
6245 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6246 lang_printable_name (type, 0));
6247 else
6248 parse_error_context
6249 (qual_wfl, "Undefined variable or class name: `%s'",
6250 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
6251 return 1;
6255 /* We have a type name. It's been already resolved when the
6256 expression was qualified. */
6257 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
6259 if (!(decl = QUAL_RESOLUTION (q)))
6260 return 1; /* Error reported already */
6262 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
6264 parse_error_context
6265 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
6266 java_accstring_lookup (get_access_flags_from_decl (decl)),
6267 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6268 IDENTIFIER_POINTER (DECL_NAME (decl)),
6269 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6270 return 1;
6272 check_deprecation (qual_wfl, decl);
6274 type = TREE_TYPE (decl);
6275 from_type = 1;
6277 /* We resolve and expression name */
6278 else
6280 tree field_decl;
6282 /* If there exists an early resolution, use it. That occurs
6283 only once and we know that there are more things to
6284 come. Don't do that when processing something after SUPER
6285 (we need more thing to be put in place below */
6286 if (!from_super && QUAL_RESOLUTION (q))
6288 decl = QUAL_RESOLUTION (q);
6289 if (!type)
6291 if (!FIELD_STATIC (decl))
6292 *where_found = current_this;
6293 else
6295 *where_found = TREE_TYPE (decl);
6296 if (TREE_CODE (*where_found) == POINTER_TYPE)
6297 *where_found = TREE_TYPE (*where_found);
6302 /* We have to search for a field, knowing the type of its
6303 container. The flag FROM_TYPE indicates that we resolved
6304 the last member of the expression as a type name, which
6305 means that for the resolution of this field, we'll look
6306 for other errors than if it was resolved as a member of
6307 an other field. */
6308 else
6310 int is_static;
6311 tree field_decl_type; /* For layout */
6313 if (!from_type && !JREFERENCE_TYPE_P (type))
6315 parse_error_context
6316 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
6317 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6318 lang_printable_name (type, 0),
6319 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6320 return 1;
6323 if (!(field_decl =
6324 lookup_field_wrapper (type, EXPR_WFL_NODE (qual_wfl))))
6326 parse_error_context
6327 (qual_wfl, "No variable `%s' defined in class `%s'",
6328 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6329 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6330 return 1;
6333 /* Layout the type of field_decl, since we may need
6334 it. Don't do primitive types or loaded classes. The
6335 situation of non primitive arrays may not handled
6336 properly here. FIXME */
6337 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
6338 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
6339 else
6340 field_decl_type = TREE_TYPE (field_decl);
6341 if (!JPRIMITIVE_TYPE_P (field_decl_type)
6342 && !CLASS_LOADED_P (field_decl_type)
6343 && !TYPE_ARRAY_P (field_decl_type))
6344 resolve_and_layout (field_decl_type, NULL_TREE);
6345 if (TYPE_ARRAY_P (field_decl_type))
6346 CLASS_LOADED_P (field_decl_type) = 1;
6348 /* Check on accessibility here */
6349 if (not_accessible_p (type, field_decl, from_super))
6351 parse_error_context
6352 (qual_wfl,
6353 "Can't access %s field `%s.%s' from `%s'",
6354 java_accstring_lookup
6355 (get_access_flags_from_decl (field_decl)),
6356 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6357 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
6358 IDENTIFIER_POINTER
6359 (DECL_NAME (TYPE_NAME (current_class))));
6360 return 1;
6362 check_deprecation (qual_wfl, field_decl);
6364 /* There are things to check when fields are accessed
6365 from type. There are no restrictions on a static
6366 declaration of the field when it is accessed from an
6367 interface */
6368 is_static = FIELD_STATIC (field_decl);
6369 if (!from_super && from_type
6370 && !TYPE_INTERFACE_P (type) && !is_static)
6372 parse_error_context
6373 (qual_wfl, "Can't make a static reference to nonstatic "
6374 "variable `%s' in class `%s'",
6375 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6376 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6377 return 1;
6379 from_cast = from_super = 0;
6381 /* If we need to generate something to get a proper
6382 handle on what this field is accessed from, do it
6383 now. */
6384 if (!is_static)
6386 decl = maybe_access_field (decl, *where_found, *type_found);
6387 if (decl == error_mark_node)
6388 return 1;
6391 /* We want to keep the location were found it, and the type
6392 we found. */
6393 *where_found = decl;
6394 *type_found = type;
6396 /* This is the decl found and eventually the next one to
6397 search from */
6398 decl = field_decl;
6400 from_type = 0;
6401 type = QUAL_DECL_TYPE (decl);
6404 *found_decl = decl;
6405 return 0;
6408 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
6409 can't be accessed from REFERENCE (a record type). */
6411 int not_accessible_p (reference, member, from_super)
6412 tree reference, member;
6413 int from_super;
6415 int access_flag = get_access_flags_from_decl (member);
6417 /* Access always granted for members declared public */
6418 if (access_flag & ACC_PUBLIC)
6419 return 0;
6421 /* Check access on protected members */
6422 if (access_flag & ACC_PROTECTED)
6424 /* Access granted if it occurs from within the package
6425 containing the class in which the protected member is
6426 declared */
6427 if (class_in_current_package (DECL_CONTEXT (member)))
6428 return 0;
6430 /* If accessed with the form `super.member', then access is granted */
6431 if (from_super)
6432 return 0;
6434 /* Otherwise, access is granted if occuring from the class where
6435 member is declared or a subclass of it */
6436 if (inherits_from_p (reference, current_class))
6437 return 0;
6438 return 1;
6441 /* Check access on private members. Access is granted only if it
6442 occurs from within the class in witch it is declared */
6443 if (access_flag & ACC_PRIVATE)
6444 return (current_class == DECL_CONTEXT (member) ? 0 : 1);
6446 /* Default access are permitted only when occuring within the
6447 package in which the type (REFERENCE) is declared. In other words,
6448 REFERENCE is defined in the current package */
6449 if (ctxp->package)
6450 return !class_in_current_package (reference);
6452 /* Otherwise, access is granted */
6453 return 0;
6456 /* Test deprecated decl access. */
6457 static void
6458 check_deprecation (wfl, decl)
6459 tree wfl, decl;
6461 char *file = DECL_SOURCE_FILE (decl);
6462 /* Complain if the field is deprecated and the file it was defined
6463 in isn't compiled at the same time the file which contains its
6464 use is */
6465 if (DECL_DEPRECATED (decl)
6466 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
6468 char the [20];
6469 switch (TREE_CODE (decl))
6471 case FUNCTION_DECL:
6472 strcpy (the, "method");
6473 break;
6474 case FIELD_DECL:
6475 strcpy (the, "field");
6476 break;
6477 case TYPE_DECL:
6478 strcpy (the, "class");
6479 break;
6480 default:
6481 fatal ("unexpected DECL code - check_deprecation");
6483 parse_warning_context
6484 (wfl, "The %s `%s' in class `%s' has been deprecated",
6485 the, lang_printable_name (decl, 0),
6486 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
6490 /* Returns 1 if class was declared in the current package, 0 otherwise */
6492 static int
6493 class_in_current_package (class)
6494 tree class;
6496 static tree cache = NULL_TREE;
6497 int qualified_flag;
6498 tree left;
6500 if (cache == class)
6501 return 1;
6503 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
6505 /* If the current package is empty and the name of CLASS is
6506 qualified, class isn't in the current package. If there is a
6507 current package and the name of the CLASS is not qualified, class
6508 isn't in the current package */
6509 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
6510 return 0;
6512 /* If there is not package and the name of CLASS isn't qualified,
6513 they belong to the same unnamed package */
6514 if (!ctxp->package && !qualified_flag)
6515 return 1;
6517 /* Compare the left part of the name of CLASS with the package name */
6518 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
6519 if (ctxp->package == left)
6521 cache = class;
6522 return 1;
6524 return 0;
6527 /* This function may generate code to access DECL from WHERE. This is
6528 done only if certain conditions meet. */
6530 static tree
6531 maybe_access_field (decl, where, type)
6532 tree decl, where, type;
6534 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
6535 && !FIELD_STATIC (decl))
6536 decl = build_field_ref (where ? where : current_this,
6537 (type ? type : DECL_CONTEXT (decl)),
6538 DECL_NAME (decl));
6539 return decl;
6542 /* Build a method invocation, by patching PATCH. If non NULL
6543 and according to the situation, PRIMARY and WHERE may be
6544 used. IS_STATIC is set to 1 if the invoked function is static. */
6546 static tree
6547 patch_method_invocation (patch, primary, where, is_static, ret_decl)
6548 tree patch, primary, where;
6549 int *is_static;
6550 tree *ret_decl;
6552 tree wfl = TREE_OPERAND (patch, 0);
6553 tree args = TREE_OPERAND (patch, 1);
6554 tree name = EXPR_WFL_NODE (wfl);
6555 tree list;
6556 int is_static_flag = 0;
6557 int is_super_init = 0;
6559 /* Should be overriden if everything goes well. Otherwise, if
6560 something fails, it should keep this value. It stop the
6561 evaluation of a bogus assignment. See java_complete_tree,
6562 MODIFY_EXPR: for the reasons why we sometimes want to keep on
6563 evaluating an assignment */
6564 TREE_TYPE (patch) = error_mark_node;
6566 /* Since lookup functions are messing with line numbers, save the
6567 context now. */
6568 java_parser_context_save_global ();
6570 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
6572 /* Resolution of qualified name, excluding constructors */
6573 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
6575 tree class_decl, identifier, identifier_wfl;
6576 /* Extract the last IDENTIFIER of the qualified
6577 expression. This is a wfl and we will use it's location
6578 data during error report. */
6579 identifier_wfl = cut_identifier_in_qualified (wfl);
6580 identifier = EXPR_WFL_NODE (identifier_wfl);
6582 /* Given the context, IDENTIFIER is syntactically qualified
6583 as a MethodName. We need to qualify what's before */
6584 qualify_ambiguous_name (wfl);
6586 /* Package resolution are erroneous */
6587 if (RESOLVE_PACKAGE_NAME_P (wfl))
6589 tree remainder;
6590 breakdown_qualified (&remainder, NULL, EXPR_WFL_NODE (wfl));
6591 parse_error_context (wfl, "Can't search method `%s' in package "
6592 "`%s'",IDENTIFIER_POINTER (identifier),
6593 IDENTIFIER_POINTER (remainder));
6594 PATCH_METHOD_RETURN_ERROR ();
6596 /* We're resolving a call from a type */
6597 else if (RESOLVE_TYPE_NAME_P (wfl))
6599 tree decl = QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl));
6600 tree name = DECL_NAME (decl);
6601 tree type;
6603 class_decl = resolve_and_layout (name, wfl);
6604 if (CLASS_INTERFACE (decl))
6606 parse_error_context
6607 (identifier_wfl, "Can't make static reference to method "
6608 "`%s' in interface `%s'", IDENTIFIER_POINTER (identifier),
6609 IDENTIFIER_POINTER (name));
6610 PATCH_METHOD_RETURN_ERROR ();
6612 /* Look the method up in the type selector. The method ought
6613 to be static. */
6614 type = TREE_TYPE (class_decl);
6615 list = lookup_method_invoke (0, wfl, type, identifier, args);
6616 if (list && !METHOD_STATIC (list))
6618 char *fct_name = strdup (lang_printable_name (list, 0));
6619 parse_error_context
6620 (identifier_wfl,
6621 "Can't make static reference to method `%s %s' in class `%s'",
6622 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6623 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6624 free (fct_name);
6625 PATCH_METHOD_RETURN_ERROR ();
6627 args = nreverse (args);
6629 /* We're resolving an expression name */
6630 else
6632 tree field, type;
6634 /* 1- Find the field to which the call applies */
6635 field = resolve_field_access (wfl, NULL, &type);
6636 if (field == error_mark_node)
6637 PATCH_METHOD_RETURN_ERROR ();
6638 /* field is used in lieu of a primary. It alows us not to
6639 report errors on erroneous use of `this' in
6640 constructors. */
6641 primary = field;
6643 /* 2- Do the layout of the class where the last field
6644 was found, so we can search it. */
6645 class_decl = resolve_and_layout (type, NULL_TREE);
6646 if (class_decl != NULL_TREE)
6647 type = TREE_TYPE (class_decl);
6649 /* 3- Retrieve a filtered list of method matches, Refine
6650 if necessary. In any cases, point out errors. */
6651 list = lookup_method_invoke (0, identifier_wfl, type,
6652 identifier, args);
6654 /* 4- Add the field as an argument */
6655 args = tree_cons (NULL_TREE, field, nreverse (args));
6658 /* IDENTIFIER_WFL will be used to report any problem further */
6659 wfl = identifier_wfl;
6661 /* Resolution of simple names, names generated after a primary: or
6662 constructors */
6663 else
6665 tree class_to_search;
6666 int lc; /* Looking for Constructor */
6668 /* We search constructor in their target class */
6669 if (CALL_CONSTRUCTOR_P (patch))
6671 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6672 class_to_search = EXPR_WFL_NODE (wfl);
6673 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6674 this_identifier_node)
6675 class_to_search = NULL_TREE;
6676 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6677 super_identifier_node)
6679 is_super_init = 1;
6680 if (CLASSTYPE_SUPER (current_class))
6681 class_to_search =
6682 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
6683 else
6685 parse_error_context (wfl, "Can't invoke super constructor "
6686 "on java.lang.Object");
6687 PATCH_METHOD_RETURN_ERROR ();
6691 /* Class to search is NULL if we're searching the current one */
6692 if (class_to_search)
6694 class_to_search = resolve_and_layout (class_to_search,
6695 NULL_TREE);
6696 if (!class_to_search)
6698 parse_error_context
6699 (wfl, "Class `%s' not found in type declaration",
6700 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6701 PATCH_METHOD_RETURN_ERROR ();
6704 /* Can't instantiate an abstract class, but we can
6705 invoke it's constructor. It's use within the `new'
6706 context is denied here. */
6707 if (CLASS_ABSTRACT (class_to_search)
6708 && TREE_CODE (patch) == NEW_CLASS_EXPR)
6710 parse_error_context
6711 (wfl, "Class `%s' is an abstract class. It can't be "
6712 "instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6713 PATCH_METHOD_RETURN_ERROR ();
6715 class_to_search = TREE_TYPE (class_to_search);
6717 else
6718 class_to_search = current_class;
6719 lc = 1;
6721 /* This is a regular search in the local class, unless an
6722 alternate class is specified. */
6723 else
6725 class_to_search = (where ? where : current_class);
6726 lc = 0;
6729 /* NAME is a simple identifier or comes from a primary. Search
6730 in the class whose declaration contain the method being
6731 invoked. */
6732 resolve_and_layout (class_to_search, NULL_TREE);
6733 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
6735 /* Don't continue if no method were found, as the next statement
6736 can't be executed then. */
6737 if (!list)
6738 PATCH_METHOD_RETURN_ERROR ();
6740 /* Check for static reference if non static methods */
6741 if (check_for_static_method_reference (wfl, patch, list,
6742 class_to_search, primary))
6743 PATCH_METHOD_RETURN_ERROR ();
6745 /* Non static methods are called with the current object extra
6746 argument. If patch a `new TYPE()', the argument is the value
6747 returned by the object allocator. If method is resolved as a
6748 primary, use the primary otherwise use the current THIS. */
6749 args = nreverse (args);
6750 if (!METHOD_STATIC (list) && TREE_CODE (patch) != NEW_CLASS_EXPR)
6751 args = tree_cons (NULL_TREE, primary ? primary : current_this, args);
6754 /* Merge point of all resolution schemes. If we have nothing, this
6755 is an error, already signaled */
6756 if (!list)
6757 PATCH_METHOD_RETURN_ERROR ();
6759 /* Check accessibility, position the is_static flag, build and
6760 return the call */
6761 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
6763 char *fct_name = strdup (lang_printable_name (list, 0));
6764 parse_error_context
6765 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
6766 java_accstring_lookup (get_access_flags_from_decl (list)),
6767 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6768 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
6769 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6770 free (fct_name);
6771 PATCH_METHOD_RETURN_ERROR ();
6773 check_deprecation (wfl, list);
6775 is_static_flag = METHOD_STATIC (list);
6777 /* In the context of an explicit constructor invocation, we can't
6778 invoke any method relying on `this'. Exceptions are: we're
6779 invoking a static function, primary exists and is not the current
6780 this, we're creating a new object. */
6781 if (ctxp->explicit_constructor_p
6782 && !is_static_flag
6783 && (!primary || primary == current_this)
6784 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
6786 parse_error_context
6787 (wfl, "Can't reference `this' before the superclass constructor has "
6788 "been called");
6789 PATCH_METHOD_RETURN_ERROR ();
6791 java_parser_context_restore_global ();
6792 if (is_static)
6793 *is_static = is_static_flag;
6794 /* Sometimes, we want the decl of the selected method. Such as for
6795 EH checking */
6796 if (ret_decl)
6797 *ret_decl = list;
6798 patch = patch_invoke (patch, list, args);
6799 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
6801 /* Generate the code used to initialize fields declared with an
6802 initialization statement. For now, it returns a call the the
6803 artificial function $finit$, if required. */
6805 tree finit_call =
6806 build_method_invocation (build_expr_wfl (finit_identifier_node,
6807 input_filename, 0, 0),
6808 NULL_TREE);
6809 patch = build (COMPOUND_EXPR, void_type_node, patch,
6810 java_complete_tree (finit_call));
6811 CAN_COMPLETE_NORMALLY (patch) = 1;
6813 return patch;
6816 /* Check that we're not trying to do a static reference to a method in
6817 non static method. Return 1 if it's the case, 0 otherwise. */
6819 static int
6820 check_for_static_method_reference (wfl, node, method, where, primary)
6821 tree wfl, node, method, where, primary;
6823 if (METHOD_STATIC (current_function_decl)
6824 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
6826 char *fct_name = strdup (lang_printable_name (method, 0));
6827 parse_error_context
6828 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
6829 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
6830 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
6831 free (fct_name);
6832 return 1;
6834 return 0;
6837 /* Patch an invoke expression METHOD and ARGS, based on its invocation
6838 mode. */
6840 static tree
6841 patch_invoke (patch, method, args)
6842 tree patch, method, args;
6844 tree dtable, func;
6845 tree original_call, t, ta;
6847 /* Last step for args: convert build-in types. If we're dealing with
6848 a new TYPE() type call, the first argument to the constructor
6849 isn't found in the incomming argument list, but delivered by
6850 `new' */
6851 t = TYPE_ARG_TYPES (TREE_TYPE (method));
6852 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6853 t = TREE_CHAIN (t);
6854 for (ta = args; t != end_params_node && ta;
6855 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
6856 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
6857 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
6858 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
6860 if (flag_emit_class_files)
6861 func = method;
6862 else
6864 tree signature = build_java_signature (TREE_TYPE (method));
6865 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
6867 case INVOKE_VIRTUAL:
6868 dtable = invoke_build_dtable (0, args);
6869 func = build_invokevirtual (dtable, method);
6870 break;
6872 case INVOKE_SUPER:
6873 case INVOKE_STATIC:
6874 func = build_known_method_ref (method, TREE_TYPE (method),
6875 DECL_CONTEXT (method),
6876 signature, args);
6877 break;
6879 case INVOKE_INTERFACE:
6880 dtable = invoke_build_dtable (1, args);
6881 func = build_invokeinterface (dtable, DECL_NAME (method), signature);
6882 break;
6884 default:
6885 fatal ("internal error - unknown invocation_mode result");
6888 /* Ensure self_type is initialized, (invokestatic). FIXME */
6889 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
6892 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
6893 TREE_OPERAND (patch, 0) = func;
6894 TREE_OPERAND (patch, 1) = args;
6895 original_call = patch;
6897 /* We're processing a `new TYPE ()' form. New is called an its
6898 returned value is the first argument to the constructor. We build
6899 a COMPOUND_EXPR and use saved expression so that the overall NEW
6900 expression value is a pointer to a newly created and initialized
6901 class. */
6902 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
6904 tree class = DECL_CONTEXT (method);
6905 tree c1, saved_new, size, new;
6906 if (flag_emit_class_files)
6908 TREE_TYPE (patch) = build_pointer_type (class);
6909 return patch;
6911 if (!TYPE_SIZE (class))
6912 safe_layout_class (class);
6913 size = size_in_bytes (class);
6914 new = build (CALL_EXPR, promote_type (class),
6915 build_address_of (alloc_object_node),
6916 tree_cons (NULL_TREE, build_class_ref (class),
6917 build_tree_list (NULL_TREE,
6918 size_in_bytes (class))),
6919 NULL_TREE);
6920 saved_new = save_expr (new);
6921 c1 = build_tree_list (NULL_TREE, saved_new);
6922 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
6923 TREE_OPERAND (original_call, 1) = c1;
6924 TREE_SET_CODE (original_call, CALL_EXPR);
6925 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
6927 return patch;
6930 static int
6931 invocation_mode (method, super)
6932 tree method;
6933 int super;
6935 int access = get_access_flags_from_decl (method);
6937 if (super)
6938 return INVOKE_SUPER;
6940 if (access & ACC_STATIC || access & ACC_FINAL || access & ACC_PRIVATE)
6941 return INVOKE_STATIC;
6943 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
6944 return INVOKE_STATIC;
6946 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
6947 return INVOKE_INTERFACE;
6949 if (DECL_CONSTRUCTOR_P (method))
6950 return INVOKE_STATIC;
6952 return INVOKE_VIRTUAL;
6955 /* Retrieve a refined list of matching methods. It covers the step
6956 15.11.2 (Compile-Time Step 2) */
6958 static tree
6959 lookup_method_invoke (lc, cl, class, name, arg_list)
6960 int lc;
6961 tree cl;
6962 tree class, name, arg_list;
6964 tree atl = end_params_node; /* Arg Type List */
6965 tree method, signature, list, node;
6966 char *candidates; /* Used for error report */
6968 /* Fix the arguments */
6969 for (node = arg_list; node; node = TREE_CHAIN (node))
6971 tree current_arg = TREE_VALUE (node);
6972 /* Integer constant 0 passed as itself, not as a type */
6973 if (current_arg != integer_zero_node)
6974 current_arg = TREE_TYPE (TREE_VALUE (node));
6975 /* Non primitive type may have to be resolved */
6976 if (current_arg != integer_zero_node
6977 && !JPRIMITIVE_TYPE_P (current_arg))
6978 resolve_and_layout (current_arg, NULL_TREE);
6979 /* And promoted */
6980 if (TREE_CODE (current_arg) == RECORD_TYPE)
6981 current_arg = promote_type (current_arg);
6982 atl = tree_cons (NULL_TREE, current_arg, atl);
6985 /* Find all candidates and then refine the list, searching for the
6986 most specific method. */
6987 list = find_applicable_accessible_methods_list (lc, class, name, atl);
6988 list = find_most_specific_methods_list (list);
6989 if (list && !TREE_CHAIN (list))
6990 return TREE_VALUE (list);
6992 /* Issue an error. List candidates if any. Candidates are listed
6993 only if accessible (non accessible methods may end-up here for
6994 the sake of a better error report). */
6995 candidates = NULL;
6996 if (list)
6998 tree current;
6999 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
7000 for (current = list; current; current = TREE_CHAIN (current))
7002 tree cm = TREE_VALUE (current);
7003 char string [4096];
7004 if (!cm || not_accessible_p (class, cm, 0))
7005 continue;
7006 sprintf
7007 (string, " `%s' in `%s'%s",
7008 get_printable_method_name (cm),
7009 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
7010 (TREE_CHAIN (current) ? "\n" : ""));
7011 obstack_grow (&temporary_obstack, string, strlen (string));
7013 obstack_1grow (&temporary_obstack, '\0');
7014 candidates = obstack_finish (&temporary_obstack);
7016 /* Issue the error message */
7017 for (node = atl; node; node = TREE_CHAIN (node))
7018 if (TREE_VALUE (node) == integer_zero_node)
7019 TREE_VALUE (node) = long_type_node;
7020 method = make_node (FUNCTION_TYPE);
7021 TYPE_ARG_TYPES (method) = atl;
7022 signature = build_java_argument_signature (method);
7023 parse_error_context (cl, "Can't find %s `%s(%s)' in class `%s'%s",
7024 (lc ? "constructor" : "method"),
7025 (lc ?
7026 IDENTIFIER_POINTER(DECL_NAME (TYPE_NAME (class))) :
7027 IDENTIFIER_POINTER (name)),
7028 IDENTIFIER_POINTER (signature),
7029 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))),
7030 (candidates ? candidates : ""));
7031 return NULL_TREE;
7034 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
7035 when we're looking for a constructor. */
7037 static tree
7038 find_applicable_accessible_methods_list (lc, class, name, arglist)
7039 int lc;
7040 tree class, name, arglist;
7042 tree method;
7043 tree list = NULL_TREE, all_list = NULL_TREE;
7045 while (class != NULL_TREE)
7047 for (method = TYPE_METHODS (class);
7048 method != NULL_TREE; method = TREE_CHAIN (method))
7050 if (lc && !DECL_CONSTRUCTOR_P (method))
7051 continue;
7052 else if (!lc && (DECL_CONSTRUCTOR_P (method)
7053 || (GET_METHOD_NAME (method) != name)))
7054 continue;
7056 if (argument_types_convertible (method, arglist))
7058 /* Retain accessible methods only */
7059 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
7060 method, 0))
7061 list = tree_cons (NULL_TREE, method, list);
7062 else
7063 /* Also retain all selected method here */
7064 all_list = tree_cons (NULL_TREE, method, list);
7067 /* When dealing with constructor, stop here, otherwise search
7068 other classes */
7069 class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
7071 /* Either return the list obtained or all selected (but
7072 inaccessible) methods for better error report. */
7073 return (!list ? all_list : list);
7076 /* 15.11.2.2 Choose the Most Specific Method */
7078 static tree
7079 find_most_specific_methods_list (list)
7080 tree list;
7082 int max = 0;
7083 tree current, new_list = NULL_TREE;
7084 for (current = list; current; current = TREE_CHAIN (current))
7086 tree method;
7087 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
7089 for (method = list; method; method = TREE_CHAIN (method))
7091 /* Don't test a method against itself */
7092 if (method == current)
7093 continue;
7095 /* Compare arguments and location where method where declared */
7096 if (argument_types_convertible (TREE_VALUE (method),
7097 TREE_VALUE (current))
7098 && valid_method_invocation_conversion_p
7099 (DECL_CONTEXT (TREE_VALUE (method)),
7100 DECL_CONTEXT (TREE_VALUE (current))))
7102 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
7103 max = (v > max ? v : max);
7108 /* Review the list and select the maximally specific methods */
7109 for (current = list; current; current = TREE_CHAIN (current))
7110 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7111 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7113 /* If we can't find one, lower expectations and try to gather multiple
7114 maximally specific methods */
7115 while (!new_list)
7117 while (--max > 0)
7119 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7120 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7122 return new_list;
7125 return new_list;
7128 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
7129 converted by method invocation conversion (5.3) to the type of the
7130 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
7131 to change less often than M1. */
7133 static int
7134 argument_types_convertible (m1, m2_or_arglist)
7135 tree m1, m2_or_arglist;
7137 static tree m2_arg_value = NULL_TREE;
7138 static tree m2_arg_cache = NULL_TREE;
7140 register tree m1_arg, m2_arg;
7142 m1_arg = TYPE_ARG_TYPES (TREE_TYPE (m1));
7143 if (!METHOD_STATIC (m1))
7144 m1_arg = TREE_CHAIN (m1_arg);
7146 if (m2_arg_value == m2_or_arglist)
7147 m2_arg = m2_arg_cache;
7148 else
7150 /* M2_OR_ARGLIST can be a function DECL or a raw list of
7151 argument types */
7152 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
7154 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
7155 if (!METHOD_STATIC (m2_or_arglist))
7156 m2_arg = TREE_CHAIN (m2_arg);
7158 else
7159 m2_arg = m2_or_arglist;
7161 m2_arg_value = m2_or_arglist;
7162 m2_arg_cache = m2_arg;
7165 while (m1_arg != end_params_node && m2_arg != end_params_node)
7167 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
7168 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
7169 TREE_VALUE (m2_arg)))
7170 break;
7171 m1_arg = TREE_CHAIN (m1_arg);
7172 m2_arg = TREE_CHAIN (m2_arg);
7174 return m1_arg == end_params_node && m2_arg == end_params_node;
7177 /* Qualification routines */
7179 static void
7180 qualify_ambiguous_name (id)
7181 tree id;
7183 tree qual, qual_wfl, name, decl, ptr_type, saved_current_class;
7184 int again, super_found = 0, this_found = 0;
7186 /* We first qualify the first element, then derive qualification of
7187 others based on the first one. If the first element is qualified
7188 by a resolution (field or type), this resolution is stored in the
7189 QUAL_RESOLUTION of the qual element being examined. We need to
7190 save the current_class since the use of SUPER might change the
7191 its value. */
7192 saved_current_class = current_class;
7193 qual = EXPR_WFL_QUALIFICATION (id);
7194 do {
7196 /* Simple qualified expression feature a qual_wfl that is a
7197 WFL. Expression derived from a primary feature more complicated
7198 things like a CALL_EXPR. Expression from primary need to be
7199 worked out to extract the part on which the qualification will
7200 take place. */
7201 qual_wfl = QUAL_WFL (qual);
7202 switch (TREE_CODE (qual_wfl))
7204 case CALL_EXPR:
7205 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7206 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
7208 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
7209 qual_wfl = QUAL_WFL (qual);
7211 break;
7212 case NEW_CLASS_EXPR:
7213 case CONVERT_EXPR:
7214 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7215 break;
7216 case ARRAY_REF:
7217 while (TREE_CODE (qual_wfl) == ARRAY_REF)
7218 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7219 break;
7220 default:
7221 /* Fix for -Wall. Just break doing nothing */
7222 break;
7224 name = EXPR_WFL_NODE (qual_wfl);
7225 ptr_type = current_class;
7226 again = 0;
7227 /* If we have a THIS (from a primary), we set the context accordingly */
7228 if (name == this_identifier_node)
7230 qual = TREE_CHAIN (qual);
7231 qual_wfl = QUAL_WFL (qual);
7232 if (TREE_CODE (qual_wfl) == CALL_EXPR)
7233 again = 1;
7234 else
7235 name = EXPR_WFL_NODE (qual_wfl);
7236 this_found = 1;
7238 /* If we have a SUPER, we set the context accordingly */
7239 if (name == super_identifier_node)
7241 current_class = CLASSTYPE_SUPER (ptr_type);
7242 /* Check that there is such a thing as a super class. If not,
7243 return. The error will be caught later on, during the
7244 resolution */
7245 if (!current_class)
7247 current_class = saved_current_class;
7248 return;
7250 qual = TREE_CHAIN (qual);
7251 /* Do one more interation to set things up */
7252 super_found = again = 1;
7254 /* Loop one more time if we're dealing with ?: or a string constant */
7255 if (TREE_CODE (qual_wfl) == CONDITIONAL_EXPR
7256 || TREE_CODE (qual_wfl) == STRING_CST)
7258 qual = TREE_CHAIN (qual);
7259 qual_wfl = QUAL_WFL (qual);
7260 again = 1;
7262 } while (again);
7264 /* If name appears within the scope of a location variable
7265 declaration or parameter declaration, then it is an expression
7266 name. We don't carry this test out if we're in the context of the
7267 use of SUPER or THIS */
7269 if (!this_found && !super_found && (decl = IDENTIFIER_LOCAL_VALUE (name)))
7271 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7272 QUAL_RESOLUTION (qual) = decl;
7275 /* If within the class/interface NAME was found to be used there
7276 exists a (possibly inherited) field named NAME, then this is an
7277 expression name. */
7278 else if ((decl = lookup_field_wrapper (ptr_type, name)))
7280 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7281 QUAL_RESOLUTION (qual) = decl;
7284 /* We reclassify NAME as a type name if:
7285 - NAME is a class/interface declared within the compilation
7286 unit containing NAME,
7287 - NAME is imported via a single-type-import declaration,
7288 - NAME is declared in an another compilation unit of the package
7289 of the compilation unit containing NAME,
7290 - NAME is declared by exactly on type-import-on-demand declaration
7291 of the compilation unit containing NAME. */
7292 else if ((decl = resolve_and_layout (name, NULL_TREE)))
7294 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
7295 QUAL_RESOLUTION (qual) = decl;
7298 /* Method call are expression name */
7299 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
7300 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF)
7301 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7303 /* Check here that NAME isn't declared by more than one
7304 type-import-on-demand declaration of the compilation unit
7305 containing NAME. FIXME */
7307 /* Otherwise, NAME is reclassified as a package name */
7308 else
7309 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
7311 /* Propagate the qualification accross other components of the
7312 qualified name */
7313 for (qual = TREE_CHAIN (qual); qual;
7314 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
7316 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7317 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
7318 else
7319 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
7322 /* Store the global qualification for the ambiguous part of ID back
7323 into ID fields */
7324 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
7325 RESOLVE_EXPRESSION_NAME_P (id) = 1;
7326 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
7327 RESOLVE_TYPE_NAME_P (id) = 1;
7328 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7329 RESOLVE_PACKAGE_NAME_P (id) = 1;
7331 /* Restore the current class */
7332 current_class = saved_current_class;
7335 static int
7336 breakdown_qualified (left, right, source)
7337 tree *left, *right, source;
7339 char *p = IDENTIFIER_POINTER (source), *base;
7340 int l = IDENTIFIER_LENGTH (source);
7342 /* Breakdown NAME into REMAINDER . IDENTIFIER */
7343 base = p;
7344 p += (l-1);
7345 while (*p != '.' && p != base)
7346 p--;
7348 /* We didn't find a '.'. Return an error */
7349 if (p == base)
7350 return 1;
7352 *p = '\0';
7353 if (right)
7354 *right = get_identifier (p+1);
7355 *left = get_identifier (IDENTIFIER_POINTER (source));
7356 *p = '.';
7358 return 0;
7361 /* Patch tree nodes in a function body. When a BLOCK is found, push
7362 local variable decls if present.
7363 Same as java_complete_lhs, but does resolve static finals to values. */
7365 static tree
7366 java_complete_tree (node)
7367 tree node;
7369 node = java_complete_lhs (node);
7370 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7371 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE)
7373 tree value = DECL_INITIAL (node);
7374 DECL_INITIAL (node) = NULL_TREE;
7375 value = fold_constant_for_init (value, node);
7376 DECL_INITIAL (node) = value;
7377 if (value != NULL_TREE)
7378 return value;
7380 return node;
7383 /* Patch tree nodes in a function body. When a BLOCK is found, push
7384 local variable decls if present.
7385 Same as java_complete_tree, but does not resolve static finals to values. */
7387 static tree
7388 java_complete_lhs (node)
7389 tree node;
7391 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
7392 int flag;
7394 /* CONVERT_EXPR always has its type set, even though it needs to be
7395 worked out. */
7396 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
7397 return node;
7399 /* The switch block implements cases processing container nodes
7400 first. Contained nodes are always written back. Leaves come
7401 next and return a value. */
7402 switch (TREE_CODE (node))
7404 case BLOCK:
7406 /* 1- Block section.
7407 Set the local values on decl names so we can identify them
7408 faster when they're referenced. At that stage, identifiers
7409 are legal so we don't check for declaration errors. */
7410 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7412 DECL_CONTEXT (cn) = current_function_decl;
7413 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
7414 INITIALIZED_P (cn) = 0;
7416 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
7417 CAN_COMPLETE_NORMALLY (node) = 1;
7418 else
7420 tree stmt = BLOCK_EXPR_BODY (node);
7421 tree *ptr;
7422 int error_seen = 0;
7423 if (TREE_CODE (stmt) == COMPOUND_EXPR)
7425 /* Re-order from (((A; B); C); ...; Z) to
7426 (A; (B; (C ; (...; Z)))).
7427 This makes it easier to scan the statements left-to-right
7428 without using recursion (which might overflow the stack
7429 if the block has many statements. */
7430 for (;;)
7432 tree left = TREE_OPERAND (stmt, 0);
7433 if (TREE_CODE (left) != COMPOUND_EXPR)
7434 break;
7435 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
7436 TREE_OPERAND (left, 1) = stmt;
7437 stmt = left;
7439 BLOCK_EXPR_BODY (node) = stmt;
7442 /* Now do the actual complete, without deep recursion for
7443 long blocks. */
7444 ptr = &BLOCK_EXPR_BODY (node);
7445 while (TREE_CODE (*ptr) == COMPOUND_EXPR)
7447 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
7448 tree *next = &TREE_OPERAND (*ptr, 1);
7449 TREE_OPERAND (*ptr, 0) = cur;
7450 if (TREE_CODE (cur) == ERROR_MARK)
7451 error_seen++;
7452 else if (! CAN_COMPLETE_NORMALLY (cur))
7454 wfl_op2 = *next;
7455 for (;;)
7457 if (TREE_CODE (wfl_op2) == BLOCK)
7458 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
7459 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
7460 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
7461 else
7462 break;
7464 if (TREE_CODE (wfl_op2) != CASE_EXPR
7465 && TREE_CODE (wfl_op2) != DEFAULT_EXPR
7466 && wfl_op2 != empty_stmt_node)
7467 unreachable_stmt_error (*ptr);
7469 ptr = next;
7471 *ptr = java_complete_tree (*ptr);
7473 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
7474 return error_mark_node;
7475 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
7477 /* Turn local bindings to null */
7478 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7479 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
7481 TREE_TYPE (node) = void_type_node;
7482 break;
7484 /* 2- They are expressions but ultimately deal with statements */
7486 case THROW_EXPR:
7487 wfl_op1 = TREE_OPERAND (node, 0);
7488 COMPLETE_CHECK_OP_0 (node);
7489 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7490 return patch_throw_statement (node, wfl_op1);
7492 case SYNCHRONIZED_EXPR:
7493 wfl_op1 = TREE_OPERAND (node, 0);
7494 COMPLETE_CHECK_OP_0 (node);
7495 COMPLETE_CHECK_OP_1 (node);
7496 return patch_synchronized_statement (node, wfl_op1);
7498 case TRY_EXPR:
7499 return patch_try_statement (node);
7501 case LABELED_BLOCK_EXPR:
7502 PUSH_LABELED_BLOCK (node);
7503 if (LABELED_BLOCK_BODY (node))
7504 COMPLETE_CHECK_OP_1 (node);
7505 TREE_TYPE (node) = void_type_node;
7506 POP_LABELED_BLOCK ();
7507 if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
7508 CAN_COMPLETE_NORMALLY (node) = 1;
7509 return node;
7511 case EXIT_BLOCK_EXPR:
7512 /* We don't complete operand 1, because it's the return value of
7513 the EXIT_BLOCK_EXPR which doesn't exist it Java */
7514 return patch_bc_statement (node);
7516 case CASE_EXPR:
7517 cn = java_complete_tree (TREE_OPERAND (node, 0));
7518 if (cn == error_mark_node)
7519 return cn;
7521 /* First, the case expression must be constant */
7522 cn = fold (cn);
7524 if (!TREE_CONSTANT (cn))
7526 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7527 parse_error_context (node, "Constant expression required");
7528 return error_mark_node;
7531 nn = ctxp->current_loop;
7533 /* It must be assignable to the type of the switch expression. */
7534 if (!try_builtin_assignconv (NULL_TREE,
7535 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
7537 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7538 parse_error_context
7539 (wfl_operator,
7540 "Incompatible type for case. Can't convert `%s' to `int'",
7541 lang_printable_name (TREE_TYPE (cn), 0));
7542 return error_mark_node;
7545 cn = fold (convert (int_type_node, cn));
7547 /* Multiple instance of a case label bearing the same
7548 value is checked during code generation. The case
7549 expression is allright so far. */
7550 TREE_OPERAND (node, 0) = cn;
7551 TREE_TYPE (node) = void_type_node;
7552 CAN_COMPLETE_NORMALLY (node) = 1;
7553 TREE_SIDE_EFFECTS (node) = 1;
7554 break;
7556 case DEFAULT_EXPR:
7557 nn = ctxp->current_loop;
7558 /* Only one default label is allowed per switch statement */
7559 if (SWITCH_HAS_DEFAULT (nn))
7561 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7562 parse_error_context (wfl_operator,
7563 "Duplicate case label: `default'");
7564 return error_mark_node;
7566 else
7567 SWITCH_HAS_DEFAULT (nn) = 1;
7568 TREE_TYPE (node) = void_type_node;
7569 TREE_SIDE_EFFECTS (node) = 1;
7570 CAN_COMPLETE_NORMALLY (node) = 1;
7571 break;
7573 case SWITCH_EXPR:
7574 case LOOP_EXPR:
7575 PUSH_LOOP (node);
7576 /* Check whether the loop was enclosed in a labeled
7577 statement. If not, create one, insert the loop in it and
7578 return the node */
7579 nn = patch_loop_statement (node);
7581 /* Anyways, walk the body of the loop */
7582 if (TREE_CODE (node) == LOOP_EXPR)
7583 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7584 /* Switch statement: walk the switch expression and the cases */
7585 else
7586 node = patch_switch_statement (node);
7588 if (TREE_OPERAND (node, 0) == error_mark_node)
7589 return error_mark_node;
7590 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
7591 /* If we returned something different, that's because we
7592 inserted a label. Pop the label too. */
7593 if (nn != node)
7595 if (CAN_COMPLETE_NORMALLY (node))
7596 CAN_COMPLETE_NORMALLY (nn) = 1;
7597 POP_LABELED_BLOCK ();
7599 POP_LOOP ();
7600 return nn;
7602 case EXIT_EXPR:
7603 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7604 return patch_exit_expr (node);
7606 case COND_EXPR:
7607 /* Condition */
7608 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7609 if (TREE_OPERAND (node, 0) == error_mark_node)
7610 return error_mark_node;
7611 /* then-else branches */
7612 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7613 if (TREE_OPERAND (node, 1) == error_mark_node)
7614 return error_mark_node;
7615 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
7616 if (TREE_OPERAND (node, 2) == error_mark_node)
7617 return error_mark_node;
7618 return patch_if_else_statement (node);
7619 break;
7621 case CONDITIONAL_EXPR:
7622 /* Condition */
7623 wfl_op1 = TREE_OPERAND (node, 0);
7624 COMPLETE_CHECK_OP_0 (node);
7625 wfl_op2 = TREE_OPERAND (node, 1);
7626 COMPLETE_CHECK_OP_1 (node);
7627 wfl_op3 = TREE_OPERAND (node, 2);
7628 COMPLETE_CHECK_OP_2 (node);
7629 return patch_conditional_expr (node, wfl_op1, wfl_op2);
7631 /* 3- Expression section */
7632 case COMPOUND_EXPR:
7633 wfl_op2 = TREE_OPERAND (node, 1);
7634 TREE_OPERAND (node, 0) = nn =
7635 java_complete_tree (TREE_OPERAND (node, 0));
7636 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK
7637 && TREE_OPERAND (node, 1) != empty_stmt_node)
7639 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
7640 parse_error_context (wfl_operator, "Unreachable statement");
7642 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7643 if (TREE_OPERAND (node, 1) == error_mark_node)
7644 return error_mark_node;
7645 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
7646 CAN_COMPLETE_NORMALLY (node)
7647 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
7648 break;
7650 case RETURN_EXPR:
7651 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7652 return patch_return (node);
7654 case EXPR_WITH_FILE_LOCATION:
7655 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
7656 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
7658 node = resolve_expression_name (node, NULL);
7659 CAN_COMPLETE_NORMALLY (node) = 1;
7661 else
7663 tree body;
7664 int save_lineno = lineno;
7665 lineno = EXPR_WFL_LINENO (node);
7666 body = java_complete_tree (EXPR_WFL_NODE (node));
7667 lineno = save_lineno;
7668 EXPR_WFL_NODE (node) = body;
7669 TREE_SIDE_EFFECTS (node) = 1;
7670 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
7671 if (EXPR_WFL_NODE (node) == error_mark_node)
7673 /* Its important for the evaluation of assignment that
7674 this mark on the TREE_TYPE is propagated. */
7675 TREE_TYPE (node) = error_mark_node;
7676 return error_mark_node;
7678 else
7679 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
7682 break;
7684 case NEW_ARRAY_EXPR:
7685 /* Patch all the dimensions */
7686 flag = 0;
7687 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
7689 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
7690 tree dim = java_complete_tree (TREE_VALUE (cn));
7691 if (dim == error_mark_node)
7693 flag = 1;
7694 continue;
7696 else
7698 TREE_VALUE (cn) = dim;
7699 /* Setup the location of the current dimension, for
7700 later error report. */
7701 TREE_PURPOSE (cn) =
7702 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
7703 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
7706 /* They complete the array creation expression, if no errors
7707 were found. */
7708 CAN_COMPLETE_NORMALLY (node) = 1;
7709 return (flag ? error_mark_node : patch_newarray (node));
7711 case NEW_CLASS_EXPR:
7712 case CALL_EXPR:
7713 /* Complete function's argument(s) first */
7714 if (complete_function_arguments (node))
7715 return error_mark_node;
7716 else
7718 tree decl, wfl = TREE_OPERAND (node, 0);
7719 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
7721 node = patch_method_invocation (node, NULL_TREE,
7722 NULL_TREE, 0, &decl);
7723 if (node == error_mark_node)
7724 return error_mark_node;
7726 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
7727 /* If we call this(...), register signature and positions */
7728 if (in_this)
7729 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
7730 tree_cons (wfl, decl,
7731 DECL_CONSTRUCTOR_CALLS (current_function_decl));
7732 CAN_COMPLETE_NORMALLY (node) = 1;
7733 return node;
7736 case MODIFY_EXPR:
7737 /* Save potential wfls */
7738 wfl_op1 = TREE_OPERAND (node, 0);
7739 wfl_op2 = TREE_OPERAND (node, 1);
7740 TREE_OPERAND (node, 0) = java_complete_lhs (wfl_op1);
7741 if (TREE_OPERAND (node, 0) == error_mark_node)
7742 return error_mark_node;
7744 if (COMPOUND_ASSIGN_P (wfl_op2))
7746 tree lvalue;
7747 tree other =
7748 java_complete_tree (TREE_OPERAND (wfl_op2, 0));
7750 /* Hand stablize the lhs on both places */
7751 lvalue = stabilize_reference (other);
7752 TREE_OPERAND (node, 0) = lvalue;
7753 TREE_OPERAND (TREE_OPERAND (node, 1), 0) = lvalue;
7756 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
7757 function to complete this RHS */
7758 if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT)
7759 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
7760 TREE_OPERAND (node, 1));
7761 else
7762 nn = java_complete_tree (TREE_OPERAND (node, 1));
7764 /* There are cases where the type of RHS is fixed. In those
7765 cases, if the evaluation of the RHS fails, we further the
7766 evaluation of the assignment to detect more errors. */
7767 if (nn == error_mark_node)
7769 /* It's hopeless, but we can further things on to discover
7770 an error during the assignment. In any cases, the
7771 assignment operation fails. */
7772 if (TREE_CODE (TREE_OPERAND (node, 1)) != EXPR_WITH_FILE_LOCATION
7773 && TREE_CODE (TREE_OPERAND (node, 1)) != NEW_ARRAY_INIT
7774 && TREE_TYPE (TREE_OPERAND (node, 1)) != error_mark_node)
7775 patch_assignment (node, wfl_op1, wfl_op2);
7777 /* Now, we still mark the lhs as initialized */
7778 if (DECL_P (TREE_OPERAND (node, 0)))
7779 INITIALIZED_P (TREE_OPERAND (node, 0)) = 1;
7781 return error_mark_node;
7783 TREE_OPERAND (node, 1) = nn;
7785 /* In case we're handling = with a String as a RHS, we need to
7786 produce a String out of the RHS (it might still be a
7787 STRING_CST or a StringBuffer at this stage */
7788 if ((nn = patch_string (TREE_OPERAND (node, 1))))
7789 TREE_OPERAND (node, 1) = nn;
7790 node = patch_assignment (node, wfl_op1, wfl_op2);
7791 CAN_COMPLETE_NORMALLY (node) = 1;
7793 /* Before returning the node, in the context of a static field
7794 assignment in <clinit>, we may want to carray further
7795 optimizations. (VAR_DECL means it's a static field. See
7796 add_field. */
7797 if (DECL_NAME (current_function_decl) == clinit_identifier_node
7798 && MODIFY_EXPR_FROM_INITIALIZATION_P (node)
7799 && TREE_CODE (TREE_OPERAND (node, 0)) == VAR_DECL)
7800 node = patch_initialized_static_field (node);
7802 return node;
7804 case MULT_EXPR:
7805 case PLUS_EXPR:
7806 case MINUS_EXPR:
7807 case LSHIFT_EXPR:
7808 case RSHIFT_EXPR:
7809 case URSHIFT_EXPR:
7810 case BIT_AND_EXPR:
7811 case BIT_XOR_EXPR:
7812 case BIT_IOR_EXPR:
7813 case TRUNC_MOD_EXPR:
7814 case RDIV_EXPR:
7815 case TRUTH_ANDIF_EXPR:
7816 case TRUTH_ORIF_EXPR:
7817 case EQ_EXPR:
7818 case NE_EXPR:
7819 case GT_EXPR:
7820 case GE_EXPR:
7821 case LT_EXPR:
7822 case LE_EXPR:
7823 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
7824 knows how to handle those cases. */
7825 wfl_op1 = TREE_OPERAND (node, 0);
7826 wfl_op2 = TREE_OPERAND (node, 1);
7828 CAN_COMPLETE_NORMALLY (node) = 1;
7829 /* Don't complete string nodes if dealing with the PLUS operand. */
7830 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
7832 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7833 if (TREE_OPERAND (node, 0) == error_mark_node)
7834 return error_mark_node;
7836 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
7838 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
7839 if (TREE_OPERAND (node, 1) == error_mark_node)
7840 return error_mark_node;
7842 return patch_binop (node, wfl_op1, wfl_op2);
7844 case INSTANCEOF_EXPR:
7845 wfl_op1 = TREE_OPERAND (node, 0);
7846 COMPLETE_CHECK_OP_0 (node);
7847 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
7849 case UNARY_PLUS_EXPR:
7850 case NEGATE_EXPR:
7851 case TRUTH_NOT_EXPR:
7852 case BIT_NOT_EXPR:
7853 case PREDECREMENT_EXPR:
7854 case PREINCREMENT_EXPR:
7855 case POSTDECREMENT_EXPR:
7856 case POSTINCREMENT_EXPR:
7857 case CONVERT_EXPR:
7858 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
7859 how to handle those cases. */
7860 wfl_op1 = TREE_OPERAND (node, 0);
7861 CAN_COMPLETE_NORMALLY (node) = 1;
7862 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7863 if (TREE_OPERAND (node, 0) == error_mark_node)
7864 return error_mark_node;
7865 node = patch_unaryop (node, wfl_op1);
7866 CAN_COMPLETE_NORMALLY (node) = 1;
7867 break;
7869 case ARRAY_REF:
7870 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
7871 how to handle those cases. */
7872 wfl_op1 = TREE_OPERAND (node, 0);
7873 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7874 if (TREE_OPERAND (node, 0) == error_mark_node)
7875 return error_mark_node;
7876 if (!flag_emit_class_files)
7877 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
7878 /* The same applies to wfl_op2 */
7879 wfl_op2 = TREE_OPERAND (node, 1);
7880 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
7881 if (TREE_OPERAND (node, 1) == error_mark_node)
7882 return error_mark_node;
7883 if (!flag_emit_class_files)
7884 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
7885 return patch_array_ref (node);
7887 case RECORD_TYPE:
7888 return node;;
7890 case COMPONENT_REF:
7891 /* The first step in the re-write of qualified name handling. FIXME.
7892 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
7893 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7894 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
7896 tree name = TREE_OPERAND (node, 1);
7897 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
7898 if (field == NULL_TREE)
7900 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
7901 return error_mark_node;
7903 if (! FIELD_STATIC (field))
7905 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
7906 return error_mark_node;
7908 return field;
7910 else
7911 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
7912 break;
7914 case THIS_EXPR:
7915 /* Can't use THIS in a static environment */
7916 if (!current_this)
7918 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7919 parse_error_context (wfl_operator, "Keyword `this' used outside "
7920 "allowed context");
7921 TREE_TYPE (node) = error_mark_node;
7922 return error_mark_node;
7924 if (ctxp->explicit_constructor_p)
7926 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7927 parse_error_context
7928 (wfl_operator, "Can't reference `this' or `super' before the "
7929 "superclass constructor has been called");
7930 TREE_TYPE (node) = error_mark_node;
7931 return error_mark_node;
7933 return current_this;
7935 default:
7936 CAN_COMPLETE_NORMALLY (node) = 1;
7937 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
7938 and it's time to turn it into the appropriate String object
7940 if ((node = patch_string (node)))
7941 return node;
7942 fatal ("No case for tree code `%s' - java_complete_tree\n",
7943 tree_code_name [TREE_CODE (node)]);
7945 return node;
7948 /* Complete function call's argument. Return a non zero value is an
7949 error was found. */
7951 static int
7952 complete_function_arguments (node)
7953 tree node;
7955 int flag = 0;
7956 tree cn;
7958 ctxp->explicit_constructor_p += (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
7959 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
7961 tree wfl = TREE_VALUE (cn), parm, temp;
7962 parm = java_complete_tree (wfl);
7963 if (parm == error_mark_node)
7965 flag = 1;
7966 continue;
7968 /* If have a string literal that we haven't transformed yet or a
7969 crafted string buffer, as a result of use of the the String
7970 `+' operator. Build `parm.toString()' and expand it. */
7971 if ((temp = patch_string (parm)))
7972 parm = temp;
7973 /* Inline PRIMTYPE.TYPE read access */
7974 parm = maybe_build_primttype_type_ref (parm, wfl);
7976 TREE_VALUE (cn) = parm;
7978 ctxp->explicit_constructor_p -= (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
7979 return flag;
7982 /* Sometimes (for loops and variable initialized during their
7983 declaration), we want to wrap a statement around a WFL and turn it
7984 debugable. */
7986 static tree
7987 build_debugable_stmt (location, stmt)
7988 int location;
7989 tree stmt;
7991 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
7993 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
7994 EXPR_WFL_LINECOL (stmt) = location;
7996 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
7997 return stmt;
8000 static tree
8001 build_expr_block (body, decls)
8002 tree body, decls;
8004 tree node = make_node (BLOCK);
8005 BLOCK_EXPR_DECLS (node) = decls;
8006 BLOCK_EXPR_BODY (node) = body;
8007 if (body)
8008 TREE_TYPE (node) = TREE_TYPE (body);
8009 TREE_SIDE_EFFECTS (node) = 1;
8010 return node;
8013 /* Create a new function block and link it approriately to current
8014 function block chain */
8016 static tree
8017 enter_block ()
8019 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
8022 /* Link block B supercontext to the previous block. The current
8023 function DECL is used as supercontext when enter_a_block is called
8024 for the first time for a given function. The current function body
8025 (DECL_FUNCTION_BODY) is set to be block B. */
8027 static tree
8028 enter_a_block (b)
8029 tree b;
8031 tree fndecl = current_function_decl;
8033 if (!DECL_FUNCTION_BODY (fndecl))
8035 BLOCK_SUPERCONTEXT (b) = fndecl;
8036 DECL_FUNCTION_BODY (fndecl) = b;
8038 else
8040 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
8041 DECL_FUNCTION_BODY (fndecl) = b;
8043 return b;
8046 /* Exit a block by changing the current function body
8047 (DECL_FUNCTION_BODY) to the current block super context, only if
8048 the block being exited isn't the method's top level one. */
8050 static tree
8051 exit_block ()
8053 tree b = DECL_FUNCTION_BODY (current_function_decl);
8055 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
8056 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
8058 return b;
8061 /* Lookup for NAME in the nested function's blocks, all the way up to
8062 the current toplevel one. It complies with Java's local variable
8063 scoping rules. */
8065 static tree
8066 lookup_name_in_blocks (name)
8067 tree name;
8069 tree b = DECL_FUNCTION_BODY (current_function_decl);
8071 while (b != current_function_decl)
8073 tree current;
8075 /* Paranoid sanity check. To be removed */
8076 if (TREE_CODE (b) != BLOCK)
8077 fatal ("non block expr function body - lookup_name_in_blocks");
8079 for (current = BLOCK_EXPR_DECLS (b); current;
8080 current = TREE_CHAIN (current))
8081 if (DECL_NAME (current) == name)
8082 return current;
8083 b = BLOCK_SUPERCONTEXT (b);
8085 return NULL_TREE;
8088 static void
8089 maybe_absorb_scoping_blocks ()
8091 while (BLOCK_EXPR_ORIGIN (DECL_FUNCTION_BODY (current_function_decl)))
8093 tree b = exit_block ();
8094 java_method_add_stmt (current_function_decl, b);
8095 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
8100 /* This section of the source is reserved to build_* functions that
8101 are building incomplete tree nodes and the patch_* functions that
8102 are completing them. */
8104 /* Build a super() constructor invocation. Returns empty_stmt_node if
8105 we're currently dealing with the class java.lang.Object. */
8107 static tree
8108 build_super_invocation ()
8110 if (current_class == object_type_node)
8111 return empty_stmt_node;
8112 else
8114 tree super_wfl = build_wfl_node (super_identifier_node,
8115 input_filename, 0, 0);
8116 return build_method_invocation (super_wfl, NULL_TREE);
8120 /* Build a SUPER/THIS qualified method invocation. */
8122 static tree
8123 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
8124 int use_this;
8125 tree name, args;
8126 int lloc, rloc;
8129 tree invok;
8130 tree wfl =
8131 build_wfl_node ((use_this ? this_identifier_node : super_identifier_node),
8132 input_filename, 0, 0);
8133 EXPR_WFL_LINECOL (wfl) = lloc;
8134 invok = build_method_invocation (name, args);
8135 return make_qualified_primary (wfl, invok, rloc);
8138 /* Build an incomplete CALL_EXPR node. */
8140 static tree
8141 build_method_invocation (name, args)
8142 tree name;
8143 tree args;
8145 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
8146 TREE_SIDE_EFFECTS (call) = 1;
8147 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8148 return call;
8151 /* Build an incomplete new xxx(...) node. */
8153 static tree
8154 build_new_invocation (name, args)
8155 tree name, args;
8157 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
8158 TREE_SIDE_EFFECTS (call) = 1;
8159 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8160 return call;
8163 /* Build an incomplete assignment expression. */
8165 static tree
8166 build_assignment (op, op_location, lhs, rhs)
8167 int op, op_location;
8168 tree lhs, rhs;
8170 tree assignment;
8171 /* Build the corresponding binop if we deal with a Compound
8172 Assignment operator. Mark the binop sub-tree as part of a
8173 Compound Assignment expression */
8174 if (op != ASSIGN_TK)
8176 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
8177 COMPOUND_ASSIGN_P (rhs) = 1;
8179 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
8180 TREE_SIDE_EFFECTS (assignment) = 1;
8181 EXPR_WFL_LINECOL (assignment) = op_location;
8182 return assignment;
8185 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
8187 char *
8188 print_int_node (node)
8189 tree node;
8191 static char buffer [80];
8192 if (TREE_CONSTANT_OVERFLOW (node))
8193 sprintf (buffer, "<overflow>");
8195 if (TREE_INT_CST_HIGH (node) == 0)
8196 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
8197 TREE_INT_CST_LOW (node));
8198 else if (TREE_INT_CST_HIGH (node) == -1
8199 && TREE_INT_CST_LOW (node) != 0)
8201 buffer [0] = '-';
8202 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
8203 -TREE_INT_CST_LOW (node));
8205 else
8206 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
8207 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
8209 return buffer;
8212 /* Return 1 if you an assignment of a FINAL is attempted */
8214 static int
8215 check_final_assignment (lvalue, wfl)
8216 tree lvalue, wfl;
8218 if (DECL_P (lvalue) && FIELD_FINAL (lvalue) &&
8219 DECL_NAME (current_function_decl) != clinit_identifier_node)
8221 parse_error_context
8222 (wfl, "Can't assign a value to the final variable `%s'",
8223 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
8224 return 1;
8226 return 0;
8229 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
8230 read. This is needed to avoid circularities in the implementation
8231 of these fields in libjava. */
8233 static tree
8234 maybe_build_primttype_type_ref (rhs, wfl)
8235 tree rhs, wfl;
8237 tree to_return = NULL_TREE;
8238 tree rhs_type = TREE_TYPE (rhs);
8239 if (TREE_CODE (rhs) == COMPOUND_EXPR)
8241 tree n = TREE_OPERAND (rhs, 1);
8242 if (TREE_CODE (n) == VAR_DECL
8243 && DECL_NAME (n) == TYPE_identifier_node
8244 && rhs_type == class_ptr_type)
8246 char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
8247 if (!strncmp (self_name, "java.lang.", 10))
8248 to_return = build_primtype_type_ref (self_name);
8251 return (to_return ? to_return : rhs );
8254 /* 15.25 Assignment operators. */
8256 static tree
8257 patch_assignment (node, wfl_op1, wfl_op2)
8258 tree node;
8259 tree wfl_op1;
8260 tree wfl_op2;
8262 tree rhs = TREE_OPERAND (node, 1);
8263 tree lvalue = TREE_OPERAND (node, 0), llvalue;
8264 tree lhs_type, rhs_type, new_rhs = NULL_TREE;
8265 int error_found = 0;
8266 int lvalue_from_array = 0;
8268 /* Can't assign to a final. */
8269 if (check_final_assignment (lvalue, wfl_op1))
8270 error_found = 1;
8272 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8274 /* Lhs can be a named variable */
8275 if (DECL_P (lvalue))
8277 INITIALIZED_P (lvalue) = 1;
8278 lhs_type = TREE_TYPE (lvalue);
8280 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
8281 comment on reason why */
8282 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
8284 lhs_type = TREE_TYPE (lvalue);
8285 lvalue_from_array = 1;
8287 /* Or a field access */
8288 else if (TREE_CODE (lvalue) == COMPONENT_REF)
8289 lhs_type = TREE_TYPE (lvalue);
8290 /* Or a function return slot */
8291 else if (TREE_CODE (lvalue) == RESULT_DECL)
8292 lhs_type = TREE_TYPE (lvalue);
8293 /* Otherwise, we might want to try to write into an optimized static
8294 final, this is an of a different nature, reported further on. */
8295 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
8296 && resolve_expression_name (wfl_op1, &llvalue)
8297 && check_final_assignment (llvalue, wfl_op1))
8299 error_found = 1;
8300 /* What we should do instead is resetting the all the flags
8301 previously set, exchange lvalue for llvalue and continue. */
8302 return error_mark_node;
8304 else
8306 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
8307 error_found = 1;
8310 rhs_type = TREE_TYPE (rhs);
8311 /* 5.1 Try the assignment conversion for builtin type. */
8312 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
8314 /* 5.2 If it failed, try a reference conversion */
8315 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
8316 lhs_type = promote_type (rhs_type);
8318 /* 15.25.2 If we have a compound assignment, convert RHS into the
8319 type of the LHS */
8320 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8321 new_rhs = convert (lhs_type, rhs);
8323 /* Explicit cast required. This is an error */
8324 if (!new_rhs)
8326 char *t1 = strdup (lang_printable_name (TREE_TYPE (rhs), 0));
8327 char *t2 = strdup (lang_printable_name (lhs_type, 0));
8328 tree wfl;
8329 char operation [32]; /* Max size known */
8331 /* If the assignment is part of a declaration, we use the WFL of
8332 the declared variable to point out the error and call it a
8333 declaration problem. If the assignment is a genuine =
8334 operator, we call is a operator `=' problem, otherwise we
8335 call it an assignment problem. In both of these last cases,
8336 we use the WFL of the operator to indicate the error. */
8338 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
8340 wfl = wfl_op1;
8341 strcpy (operation, "declaration");
8343 else
8345 wfl = wfl_operator;
8346 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8347 strcpy (operation, "assignment");
8348 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
8349 strcpy (operation, "`return'");
8350 else
8351 strcpy (operation, "`='");
8354 parse_error_context
8355 (wfl, (!valid_cast_to_p (rhs_type, lhs_type) ?
8356 "Incompatible type for %s. Can't convert `%s' to `%s'" :
8357 "Incompatible type for %s. Explicit cast "
8358 "needed to convert `%s' to `%s'"), operation, t1, t2);
8359 free (t1); free (t2);
8360 error_found = 1;
8363 /* Inline read access to java.lang.PRIMTYPE.TYPE */
8364 if (new_rhs)
8365 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
8367 if (error_found)
8368 return error_mark_node;
8370 /* If we built a compound expression as the result of a reference
8371 assignment into an array element, return it here. */
8372 if (TREE_CODE (node) == COMPOUND_EXPR)
8373 return node;
8375 TREE_OPERAND (node, 0) = lvalue;
8376 TREE_OPERAND (node, 1) = new_rhs;
8377 TREE_TYPE (node) = lhs_type;
8378 return node;
8381 /* Optimize static (final) field initialized upon declaration.
8382 - If the field is static final and is assigned to a primitive
8383 constant type, then set its DECL_INITIAL to the value.
8384 - More to come. */
8386 static tree
8387 patch_initialized_static_field (node)
8388 tree node;
8390 tree field = TREE_OPERAND (node, 0);
8391 tree value = TREE_OPERAND (node, 1);
8393 if (DECL_INITIAL (field) != NULL_TREE)
8395 tree type = TREE_TYPE (value);
8396 if (FIELD_FINAL (field) && TREE_CONSTANT (value)
8397 && (JPRIMITIVE_TYPE_P (type)
8398 || (flag_emit_class_files
8399 && TREE_CODE (type) == POINTER_TYPE
8400 && TREE_TYPE (type) == string_type_node)))
8402 DECL_INITIAL (field) = value;
8403 return empty_stmt_node;
8405 DECL_INITIAL (field) = NULL_TREE;
8407 return node;
8410 /* Check that type SOURCE can be cast into type DEST. If the cast
8411 can't occur at all, return 0 otherwise 1. This function is used to
8412 produce accurate error messages on the reasons why an assignment
8413 failed. */
8415 static tree
8416 try_reference_assignconv (lhs_type, rhs)
8417 tree lhs_type, rhs;
8419 tree new_rhs = NULL_TREE;
8420 tree rhs_type = TREE_TYPE (rhs);
8422 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
8424 /* `null' may be assigned to any reference type */
8425 if (rhs == null_pointer_node)
8426 new_rhs = null_pointer_node;
8427 /* Try the reference assignment conversion */
8428 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
8429 new_rhs = rhs;
8430 /* This is a magic assignment that we process differently */
8431 else if (rhs == soft_exceptioninfo_call_node)
8432 new_rhs = rhs;
8434 return new_rhs;
8437 /* Check that RHS can be converted into LHS_TYPE by the assignment
8438 conversion (5.2), for the cases of RHS being a builtin type. Return
8439 NULL_TREE if the conversion fails or if because RHS isn't of a
8440 builtin type. Return a converted RHS if the conversion is possible. */
8442 static tree
8443 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
8444 tree wfl_op1, lhs_type, rhs;
8446 tree new_rhs = NULL_TREE;
8447 tree rhs_type = TREE_TYPE (rhs);
8449 /* Zero accepted everywhere */
8450 if (TREE_CODE (rhs) == INTEGER_CST
8451 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
8452 && JPRIMITIVE_TYPE_P (rhs_type))
8453 new_rhs = convert (lhs_type, rhs);
8455 /* 5.1.1 Try Identity Conversion,
8456 5.1.2 Try Widening Primitive Conversion */
8457 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
8458 new_rhs = convert (lhs_type, rhs);
8460 /* Try a narrowing primitive conversion (5.1.3):
8461 - expression is a constant expression of type int AND
8462 - variable is byte, short or char AND
8463 - The value of the expression is representable in the type of the
8464 variable */
8465 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
8466 && (lhs_type == byte_type_node || lhs_type == char_type_node
8467 || lhs_type == short_type_node))
8469 if (int_fits_type_p (rhs, lhs_type))
8470 new_rhs = convert (lhs_type, rhs);
8471 else if (wfl_op1) /* Might be called with a NULL */
8472 parse_warning_context
8473 (wfl_op1, "Constant expression `%s' to wide for narrowing "
8474 "primitive conversion to `%s'",
8475 print_int_node (rhs), lang_printable_name (lhs_type, 0));
8476 /* Reported a warning that will turn into an error further
8477 down, so we don't return */
8480 return new_rhs;
8483 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
8484 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
8485 0 is the conversion test fails. This implements parts the method
8486 invocation convertion (5.3). */
8488 static int
8489 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
8490 tree lhs_type, rhs_type;
8492 int all_primitive;
8494 if (lhs_type == rhs_type)
8495 return 1;
8497 /* Sometimes, instead of passing a type, we pass integer_zero_node
8498 so we know that an integral type can accomodate it */
8499 if (JINTEGRAL_TYPE_P (lhs_type) && (rhs_type == integer_zero_node))
8500 return 1;
8502 all_primitive =
8503 JPRIMITIVE_TYPE_P (lhs_type) && JPRIMITIVE_TYPE_P (rhs_type);
8505 if (!all_primitive)
8506 return 0;
8508 /* byte, even if it's smaller than a char can't be converted into a
8509 char. Short can't too, but the < test below takes care of that */
8510 if (lhs_type == char_type_node && rhs_type == byte_type_node)
8511 return 0;
8513 /* Accept all promoted type here. Note, we can't use <= in the test
8514 below, because we still need to bounce out assignments of short
8515 to char and the likes */
8516 if (lhs_type == int_type_node
8517 && (rhs_type == promoted_byte_type_node
8518 || rhs_type == promoted_short_type_node
8519 || rhs_type == promoted_char_type_node
8520 || rhs_type == promoted_boolean_type_node))
8521 return 1;
8523 if (JINTEGRAL_TYPE_P (rhs_type)
8524 && ((TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type))
8525 || (JFLOAT_TYPE_P (lhs_type) &&
8526 TYPE_PRECISION (rhs_type) == TYPE_PRECISION (lhs_type))))
8527 return 1;
8528 else if (JFLOAT_TYPE_P (rhs_type)
8529 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
8530 return 1;
8532 return 0;
8535 /* Check that something of SOURCE type can be assigned or cast to
8536 something of DEST type at runtime. Return 1 if the operation is
8537 valid, 0 otherwise. If CAST is set to 1, we're treating the case
8538 were SOURCE is cast into DEST, which borrows a lot of the
8539 assignment check. */
8541 static int
8542 valid_ref_assignconv_cast_p (source, dest, cast)
8543 tree source;
8544 tree dest;
8545 int cast;
8547 if (JNULLP_TYPE_P (source))
8548 return 1;
8549 if (TREE_CODE (source) == POINTER_TYPE)
8550 source = TREE_TYPE (source);
8551 if (TREE_CODE (dest) == POINTER_TYPE)
8552 dest = TREE_TYPE (dest);
8553 /* Case where SOURCE is a class type */
8554 if (TYPE_CLASS_P (source))
8556 if (TYPE_CLASS_P (dest))
8557 return source == dest || inherits_from_p (source, dest)
8558 || (cast && inherits_from_p (dest, source));
8559 if (TYPE_INTERFACE_P (dest))
8561 /* If doing a cast and SOURCE is final, the operation is
8562 always correct a compile time (because even if SOURCE
8563 does not implement DEST, a subclass of SOURCE might). */
8564 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
8565 return 1;
8566 /* Otherwise, SOURCE must implement DEST */
8567 return interface_of_p (dest, source);
8569 /* DEST is an array, cast permited if SOURCE is of Object type */
8570 return (cast && source == object_type_node ? 1 : 0);
8572 if (TYPE_INTERFACE_P (source))
8574 if (TYPE_CLASS_P (dest))
8576 /* If not casting, DEST must be the Object type */
8577 if (!cast)
8578 return dest == object_type_node;
8579 /* We're doing a cast. The cast is always valid is class
8580 DEST is not final, otherwise, DEST must implement SOURCE */
8581 else if (!CLASS_FINAL (TYPE_NAME (dest)))
8582 return 1;
8583 else
8584 return interface_of_p (source, dest);
8586 if (TYPE_INTERFACE_P (dest))
8588 /* If doing a cast, then if SOURCE and DEST contain method
8589 with the same signature but different return type, then
8590 this is a (compile time) error */
8591 if (cast)
8593 tree method_source, method_dest;
8594 tree source_type;
8595 tree source_sig;
8596 tree source_name;
8597 for (method_source = TYPE_METHODS (source); method_source;
8598 method_source = TREE_CHAIN (method_source))
8600 source_sig =
8601 build_java_argument_signature (TREE_TYPE (method_source));
8602 source_type = TREE_TYPE (TREE_TYPE (method_source));
8603 source_name = DECL_NAME (method_source);
8604 for (method_dest = TYPE_METHODS (dest);
8605 method_dest; method_dest = TREE_CHAIN (method_dest))
8606 if (source_sig ==
8607 build_java_argument_signature (TREE_TYPE (method_dest))
8608 && source_name == DECL_NAME (method_dest)
8609 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
8610 return 0;
8612 return 1;
8614 else
8615 return source == dest || interface_of_p (dest, source);
8617 else /* Array */
8618 return 0;
8620 if (TYPE_ARRAY_P (source))
8622 if (TYPE_CLASS_P (dest))
8623 return dest == object_type_node;
8624 if (TYPE_INTERFACE_P (dest))
8625 return 0; /* Install test on Clonable. FIXME */
8626 else /* Arrays */
8628 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
8629 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
8631 /* In case of severe errors, they turn out null */
8632 if (!dest_element_type || !source_element_type)
8633 return 0;
8634 if (source_element_type == dest_element_type)
8635 return 1;
8636 return valid_ref_assignconv_cast_p (source_element_type,
8637 dest_element_type, cast);
8639 return 0;
8641 return 0;
8644 static int
8645 valid_cast_to_p (source, dest)
8646 tree source;
8647 tree dest;
8649 if (TREE_CODE (source) == POINTER_TYPE)
8650 source = TREE_TYPE (source);
8651 if (TREE_CODE (dest) == POINTER_TYPE)
8652 dest = TREE_TYPE (dest);
8654 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
8655 return valid_ref_assignconv_cast_p (source, dest, 1);
8657 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
8658 return 1;
8660 return 0;
8663 /* Method invocation conversion test. Return 1 if type SOURCE can be
8664 converted to type DEST through the methond invocation conversion
8665 process (5.3) */
8667 static tree
8668 do_unary_numeric_promotion (arg)
8669 tree arg;
8671 tree type = TREE_TYPE (arg);
8672 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
8673 : TREE_CODE (type) == CHAR_TYPE)
8674 arg = convert (int_type_node, arg);
8675 return arg;
8678 static int
8679 valid_method_invocation_conversion_p (dest, source)
8680 tree dest, source;
8682 return (((JPRIMITIVE_TYPE_P (source) || (source == integer_zero_node))
8683 && JPRIMITIVE_TYPE_P (dest)
8684 && valid_builtin_assignconv_identity_widening_p (dest, source))
8685 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
8686 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
8687 && valid_ref_assignconv_cast_p (source, dest, 0)));
8690 /* Build an incomplete binop expression. */
8692 static tree
8693 build_binop (op, op_location, op1, op2)
8694 enum tree_code op;
8695 int op_location;
8696 tree op1, op2;
8698 tree binop = build (op, NULL_TREE, op1, op2);
8699 TREE_SIDE_EFFECTS (binop) = 1;
8700 /* Store the location of the operator, for better error report. The
8701 string of the operator will be rebuild based on the OP value. */
8702 EXPR_WFL_LINECOL (binop) = op_location;
8703 return binop;
8706 /* Build the string of the operator retained by NODE. If NODE is part
8707 of a compound expression, add an '=' at the end of the string. This
8708 function is called when an error needs to be reported on an
8709 operator. The string is returned as a pointer to a static character
8710 buffer. */
8712 static char *
8713 operator_string (node)
8714 tree node;
8716 #define BUILD_OPERATOR_STRING(S) \
8718 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
8719 return buffer; \
8722 static char buffer [10];
8723 switch (TREE_CODE (node))
8725 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
8726 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
8727 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
8728 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8729 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
8730 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
8731 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
8732 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
8733 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
8734 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
8735 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
8736 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
8737 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
8738 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
8739 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
8740 case GT_EXPR: BUILD_OPERATOR_STRING (">");
8741 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
8742 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
8743 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
8744 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8745 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
8746 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
8747 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
8748 case PREINCREMENT_EXPR: /* Fall through */
8749 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
8750 case PREDECREMENT_EXPR: /* Fall through */
8751 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
8752 default:
8753 fatal ("unregistered operator %s - operator_string",
8754 tree_code_name [TREE_CODE (node)]);
8756 return NULL;
8757 #undef BUILD_OPERATOR_STRING
8760 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
8761 errors but we modify NODE so that it contains the type computed
8762 according to the expression, when it's fixed. Otherwise, we write
8763 error_mark_node as the type. It allows us to further the analysis
8764 of remaining nodes and detects more errors in certain cases. */
8766 static tree
8767 patch_binop (node, wfl_op1, wfl_op2)
8768 tree node;
8769 tree wfl_op1;
8770 tree wfl_op2;
8772 tree op1 = TREE_OPERAND (node, 0);
8773 tree op2 = TREE_OPERAND (node, 1);
8774 tree op1_type = TREE_TYPE (op1);
8775 tree op2_type = TREE_TYPE (op2);
8776 tree prom_type;
8777 int code = TREE_CODE (node);
8779 /* If 1, tell the routine that we have to return error_mark_node
8780 after checking for the initialization of the RHS */
8781 int error_found = 0;
8783 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8785 switch (code)
8787 /* 15.16 Multiplicative operators */
8788 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
8789 case RDIV_EXPR: /* 15.16.2 Division Operator / */
8790 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
8791 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
8793 if (!JPRIMITIVE_TYPE_P (op1_type))
8794 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
8795 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
8796 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
8797 TREE_TYPE (node) = error_mark_node;
8798 error_found = 1;
8799 break;
8801 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8802 /* Change the division operator if necessary */
8803 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
8804 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
8805 /* This one is more complicated. FLOATs are processed by a function
8806 call to soft_fmod. */
8807 if (code == TRUNC_MOD_EXPR)
8808 return build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
8809 break;
8811 /* 15.17 Additive Operators */
8812 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
8814 /* Operation is valid if either one argument is a string
8815 constant, a String object or a StringBuffer crafted for the
8816 purpose of the a previous usage of the String concatenation
8817 operator */
8819 if (TREE_CODE (op1) == STRING_CST
8820 || TREE_CODE (op2) == STRING_CST
8821 || JSTRING_TYPE_P (op1_type)
8822 || JSTRING_TYPE_P (op2_type)
8823 || IS_CRAFTED_STRING_BUFFER_P (op1)
8824 || IS_CRAFTED_STRING_BUFFER_P (op2))
8825 return build_string_concatenation (op1, op2);
8827 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
8828 Numeric Types */
8829 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
8831 if (!JPRIMITIVE_TYPE_P (op1_type))
8832 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
8833 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
8834 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
8835 TREE_TYPE (node) = error_mark_node;
8836 error_found = 1;
8837 break;
8839 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8840 break;
8842 /* 15.18 Shift Operators */
8843 case LSHIFT_EXPR:
8844 case RSHIFT_EXPR:
8845 case URSHIFT_EXPR:
8846 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
8848 if (!JINTEGRAL_TYPE_P (op1_type))
8849 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
8850 else
8851 parse_error_context
8852 (wfl_operator, (JPRIMITIVE_TYPE_P (op2_type) ?
8853 "Incompatible type for `%s'. Explicit cast needed to convert "
8854 "shift distance from `%s' to integral" :
8855 "Incompatible type for `%s'. Can't convert shift distance from "
8856 "`%s' to integral"),
8857 operator_string (node), lang_printable_name (op2_type, 0));
8858 TREE_TYPE (node) = error_mark_node;
8859 error_found = 1;
8860 break;
8863 /* Unary numeric promotion (5.6.1) is performed on each operand
8864 separatly */
8865 op1 = do_unary_numeric_promotion (op1);
8866 op2 = do_unary_numeric_promotion (op2);
8868 /* The type of the shift expression is the type of the promoted
8869 type of the left-hand operand */
8870 prom_type = TREE_TYPE (op1);
8872 /* Shift int only up to 0x1f and long up to 0x3f */
8873 if (prom_type == int_type_node)
8874 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
8875 build_int_2 (0x1f, 0)));
8876 else
8877 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
8878 build_int_2 (0x3f, 0)));
8880 /* The >>> operator is a >> operating on unsigned quantities */
8881 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
8883 op1 = convert (unsigned_type (prom_type), op1);
8884 TREE_SET_CODE (node, RSHIFT_EXPR);
8886 break;
8888 /* 15.19.1 Type Comparison Operator instaceof */
8889 case INSTANCEOF_EXPR:
8891 TREE_TYPE (node) = boolean_type_node;
8893 if (!(op2_type = resolve_type_during_patch (op2)))
8894 return error_mark_node;
8896 /* The first operand must be a reference type or the null type */
8897 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
8898 error_found = 1; /* Error reported further below */
8900 /* The second operand must be a reference type */
8901 if (!JREFERENCE_TYPE_P (op2_type))
8903 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
8904 parse_error_context
8905 (wfl_operator, "Invalid argument `%s' for `instanceof'",
8906 lang_printable_name (op2_type, 0));
8907 error_found = 1;
8910 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
8912 /* If the first operand is null, the result is always false */
8913 if (op1 == null_pointer_node)
8914 return boolean_false_node;
8915 else if (flag_emit_class_files)
8917 TREE_OPERAND (node, 1) = op2_type;
8918 return node;
8920 /* Otherwise we have to invoke instance of to figure it out */
8921 else
8923 tree call =
8924 build (CALL_EXPR, boolean_type_node,
8925 build_address_of (soft_instanceof_node),
8926 tree_cons
8927 (NULL_TREE, op1,
8928 build_tree_list (NULL_TREE,
8929 build_class_ref (op2_type))),
8930 NULL_TREE);
8931 TREE_SIDE_EFFECTS (call) = 1;
8932 return call;
8935 /* There is no way the expression operand can be an instance of
8936 the type operand. This is a compile time error. */
8937 else
8939 char *t1 = strdup (lang_printable_name (op1_type, 0));
8940 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
8941 parse_error_context
8942 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
8943 t1, lang_printable_name (op2_type, 0));
8944 free (t1);
8945 error_found = 1;
8948 break;
8950 /* 15.21 Bitwise and Logical Operators */
8951 case BIT_AND_EXPR:
8952 case BIT_XOR_EXPR:
8953 case BIT_IOR_EXPR:
8954 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
8955 /* Binary numeric promotion is performed on both operand and the
8956 expression retain that type */
8957 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8959 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
8960 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
8961 /* The type of the bitwise operator expression is BOOLEAN */
8962 prom_type = boolean_type_node;
8963 else
8965 if (!JINTEGRAL_TYPE_P (op1_type))
8966 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
8967 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
8968 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
8969 TREE_TYPE (node) = error_mark_node;
8970 error_found = 1;
8971 /* Insert a break here if adding thing before the switch's
8972 break for this case */
8974 break;
8976 /* 15.22 Conditional-And Operator */
8977 case TRUTH_ANDIF_EXPR:
8978 /* 15.23 Conditional-Or Operator */
8979 case TRUTH_ORIF_EXPR:
8980 /* Operands must be of BOOLEAN type */
8981 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
8982 TREE_CODE (op2_type) != BOOLEAN_TYPE)
8984 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
8985 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
8986 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
8987 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
8988 TREE_TYPE (node) = boolean_type_node;
8989 error_found = 1;
8990 break;
8992 /* The type of the conditional operators is BOOLEAN */
8993 prom_type = boolean_type_node;
8994 break;
8996 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
8997 case LT_EXPR:
8998 case GT_EXPR:
8999 case LE_EXPR:
9000 case GE_EXPR:
9001 /* The type of each of the operands must be a primitive numeric
9002 type */
9003 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
9005 if (!JNUMERIC_TYPE_P (op1_type))
9006 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
9007 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
9008 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
9009 TREE_TYPE (node) = boolean_type_node;
9010 error_found = 1;
9011 break;
9013 /* Binary numeric promotion is performed on the operands */
9014 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9015 /* The type of the relation expression is always BOOLEAN */
9016 prom_type = boolean_type_node;
9017 break;
9019 /* 15.20 Equality Operator */
9020 case EQ_EXPR:
9021 case NE_EXPR:
9022 /* 15.20.1 Numerical Equality Operators == and != */
9023 /* Binary numeric promotion is performed on the operands */
9024 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
9025 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9027 /* 15.20.2 Boolean Equality Operators == and != */
9028 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
9029 TREE_CODE (op2_type) == BOOLEAN_TYPE)
9030 ; /* Nothing to do here */
9032 /* 15.20.3 Reference Equality Operators == and != */
9033 /* Types have to be either references or the null type. If
9034 they're references, it must be possible to convert either
9035 type to the other by casting conversion. */
9036 else if (op1 == null_pointer_node || op2 == null_pointer_node
9037 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
9038 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
9039 || valid_ref_assignconv_cast_p (op2_type,
9040 op1_type, 1))))
9041 ; /* Nothing to do here */
9043 /* Else we have an error figure what can't be converted into
9044 what and report the error */
9045 else
9047 char *t1;
9048 t1 = strdup (lang_printable_name (op1_type, 0));
9049 parse_error_context
9050 (wfl_operator, "Incompatible type for `%s'. Can't convert `%s' "
9051 "to `%s'", operator_string (node), t1,
9052 lang_printable_name (op2_type, 0));
9053 free (t1);
9054 TREE_TYPE (node) = boolean_type_node;
9055 error_found = 1;
9056 break;
9058 prom_type = boolean_type_node;
9059 break;
9062 if (error_found)
9063 return error_mark_node;
9065 TREE_OPERAND (node, 0) = op1;
9066 TREE_OPERAND (node, 1) = op2;
9067 TREE_TYPE (node) = prom_type;
9068 return fold (node);
9071 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
9072 zero value, the value of CSTE comes after the valude of STRING */
9074 static tree
9075 do_merge_string_cste (cste, string, string_len, after)
9076 tree cste;
9077 char *string;
9078 int string_len, after;
9080 int len = TREE_STRING_LENGTH (cste) + string_len;
9081 char *old = TREE_STRING_POINTER (cste);
9082 TREE_STRING_LENGTH (cste) = len;
9083 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
9084 if (after)
9086 strcpy (TREE_STRING_POINTER (cste), string);
9087 strcat (TREE_STRING_POINTER (cste), old);
9089 else
9091 strcpy (TREE_STRING_POINTER (cste), old);
9092 strcat (TREE_STRING_POINTER (cste), string);
9094 return cste;
9097 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
9098 new STRING_CST on success, NULL_TREE on failure */
9100 static tree
9101 merge_string_cste (op1, op2, after)
9102 tree op1, op2;
9103 int after;
9105 /* Handle two string constants right away */
9106 if (TREE_CODE (op2) == STRING_CST)
9107 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
9108 TREE_STRING_LENGTH (op2), after);
9110 /* Reasonable integer constant can be treated right away */
9111 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
9113 static char *boolean_true = "true";
9114 static char *boolean_false = "false";
9115 static char *null_pointer = "null";
9116 char ch[3];
9117 char *string;
9119 if (op2 == boolean_true_node)
9120 string = boolean_true;
9121 else if (op2 == boolean_false_node)
9122 string = boolean_false;
9123 else if (op2 == null_pointer_node)
9124 string = null_pointer;
9125 else if (TREE_TYPE (op2) == char_type_node)
9127 ch[0] = (char )TREE_INT_CST_LOW (op2);
9128 ch[1] = '\0';
9129 string = ch;
9131 else
9132 string = print_int_node (op2);
9134 return do_merge_string_cste (op1, string, strlen (string), after);
9136 return NULL_TREE;
9139 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
9140 has to be a STRING_CST and the other part must be a STRING_CST or a
9141 INTEGRAL constant. Return a new STRING_CST if the operation
9142 succeed, NULL_TREE otherwise.
9144 If the case we want to optimize for space, we might want to return
9145 NULL_TREE for each invocation of this routine. FIXME */
9147 static tree
9148 string_constant_concatenation (op1, op2)
9149 tree op1, op2;
9151 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
9153 tree string, rest;
9154 int invert;
9156 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
9157 rest = (string == op1 ? op2 : op1);
9158 invert = (string == op1 ? 0 : 1 );
9160 /* Walk REST, only if it looks reasonable */
9161 if (TREE_CODE (rest) != STRING_CST
9162 && !IS_CRAFTED_STRING_BUFFER_P (rest)
9163 && !JSTRING_TYPE_P (TREE_TYPE (rest))
9164 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
9166 rest = java_complete_tree (rest);
9167 if (rest == error_mark_node)
9168 return error_mark_node;
9169 rest = fold (rest);
9171 return merge_string_cste (string, rest, invert);
9173 return NULL_TREE;
9176 /* Implement the `+' operator. Does static optimization if possible,
9177 otherwise create (if necessary) and append elements to a
9178 StringBuffer. The StringBuffer will be carried around until it is
9179 used for a function call or an assignment. Then toString() will be
9180 called on it to turn it into a String object. */
9182 static tree
9183 build_string_concatenation (op1, op2)
9184 tree op1, op2;
9186 tree result;
9188 /* Try to do some static optimization */
9189 if ((result = string_constant_concatenation (op1, op2)))
9190 return result;
9192 /* If operands are string constant, turn then into object references */
9194 if (TREE_CODE (op1) == STRING_CST)
9195 op1 = patch_string_cst (op1);
9196 if (TREE_CODE (op2) == STRING_CST)
9197 op2 = patch_string_cst (op2);
9199 /* If OP1 isn't already a StringBuffer, create and
9200 initialize a new one */
9201 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
9203 /* Two solutions here:
9204 1) OP1 is a string reference, we call new StringBuffer(OP1)
9205 2) Op2 is something else, we call new StringBuffer().append(OP1). */
9206 if (JSTRING_TYPE_P (TREE_TYPE (op1)))
9207 op1 = BUILD_STRING_BUFFER (op1);
9208 else
9210 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
9211 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
9215 /* No longer the last node holding a crafted StringBuffer */
9216 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
9217 /* Create a node for `{new...,xxx}.append (op2)' */
9218 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
9219 /* Mark the last node holding a crafted StringBuffer */
9220 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
9222 return op1;
9225 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
9226 StringBuffer. If no string were found to be patched, return
9227 NULL. */
9229 static tree
9230 patch_string (node)
9231 tree node;
9233 if (node == error_mark_node)
9234 return error_mark_node;
9235 if (TREE_CODE (node) == STRING_CST)
9236 return patch_string_cst (node);
9237 else if (IS_CRAFTED_STRING_BUFFER_P (node))
9239 int saved = ctxp->explicit_constructor_p;
9240 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
9241 tree ret;
9242 /* Temporary disable forbid the use of `this'. */
9243 ctxp->explicit_constructor_p = 0;
9244 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
9245 /* Restore it at its previous value */
9246 ctxp->explicit_constructor_p = saved;
9247 return ret;
9249 return NULL_TREE;
9252 /* Build the internal representation of a string constant. */
9254 static tree
9255 patch_string_cst (node)
9256 tree node;
9258 int location;
9259 if (! flag_emit_class_files)
9261 push_obstacks (&permanent_obstack, &permanent_obstack);
9262 node = get_identifier (TREE_STRING_POINTER (node));
9263 location = alloc_name_constant (CONSTANT_String, node);
9264 node = build_ref_from_constant_pool (location);
9266 TREE_TYPE (node) = promote_type (string_type_node);
9267 TREE_CONSTANT (node) = 1;
9268 return node;
9271 /* Build an incomplete unary operator expression. */
9273 static tree
9274 build_unaryop (op_token, op_location, op1)
9275 int op_token, op_location;
9276 tree op1;
9278 enum tree_code op;
9279 tree unaryop;
9280 switch (op_token)
9282 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
9283 case MINUS_TK: op = NEGATE_EXPR; break;
9284 case NEG_TK: op = TRUTH_NOT_EXPR; break;
9285 case NOT_TK: op = BIT_NOT_EXPR; break;
9286 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
9287 op_token);
9290 unaryop = build1 (op, NULL_TREE, op1);
9291 TREE_SIDE_EFFECTS (unaryop) = 1;
9292 /* Store the location of the operator, for better error report. The
9293 string of the operator will be rebuild based on the OP value. */
9294 EXPR_WFL_LINECOL (unaryop) = op_location;
9295 return unaryop;
9298 /* Special case for the ++/-- operators, since they require an extra
9299 argument to build, which is set to NULL and patched
9300 later. IS_POST_P is 1 if the operator, 0 otherwise. */
9302 static tree
9303 build_incdec (op_token, op_location, op1, is_post_p)
9304 int op_token, op_location;
9305 tree op1;
9306 int is_post_p;
9308 static enum tree_code lookup [2][2] =
9310 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
9311 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
9313 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
9314 NULL_TREE, op1, NULL_TREE);
9315 TREE_SIDE_EFFECTS (node) = 1;
9316 /* Store the location of the operator, for better error report. The
9317 string of the operator will be rebuild based on the OP value. */
9318 EXPR_WFL_LINECOL (node) = op_location;
9319 return node;
9322 /* Build an incomplete cast operator, based on the use of the
9323 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
9324 set. java_complete_tree is trained to walk a CONVERT_EXPR even
9325 though its type is already set. */
9327 static tree
9328 build_cast (location, type, exp)
9329 int location;
9330 tree type, exp;
9332 tree node = build1 (CONVERT_EXPR, type, exp);
9333 EXPR_WFL_LINECOL (node) = location;
9334 return node;
9337 /* 15.14 Unary operators. We return error_mark_node in case of error,
9338 but preserve the type of NODE if the type is fixed. */
9340 static tree
9341 patch_unaryop (node, wfl_op)
9342 tree node;
9343 tree wfl_op;
9345 tree op = TREE_OPERAND (node, 0);
9346 tree op_type = TREE_TYPE (op);
9347 tree prom_type, value;
9348 int code = TREE_CODE (node);
9349 int error_found = 0;
9351 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9353 switch (code)
9355 /* 15.13.2 Postfix Increment Operator ++ */
9356 case POSTINCREMENT_EXPR:
9357 /* 15.13.3 Postfix Increment Operator -- */
9358 case POSTDECREMENT_EXPR:
9359 /* 15.14.1 Prefix Increment Operator ++ */
9360 case PREINCREMENT_EXPR:
9361 /* 15.14.2 Prefix Decrement Operator -- */
9362 case PREDECREMENT_EXPR:
9363 if (!DECL_P (op) && !((TREE_CODE (op) == INDIRECT_REF
9364 || TREE_CODE (op) == COMPONENT_REF)
9365 && JPRIMITIVE_TYPE_P (TREE_TYPE (op))))
9367 tree lvalue;
9368 /* Before screaming, check that we're not in fact trying to
9369 increment a optimized static final access, in which case
9370 we issue an different error message. */
9371 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
9372 && resolve_expression_name (wfl_op, &lvalue)
9373 && check_final_assignment (lvalue, wfl_op)))
9374 parse_error_context (wfl_operator, "Invalid argument to `%s'",
9375 operator_string (node));
9376 TREE_TYPE (node) = error_mark_node;
9377 error_found = 1;
9379 else if (check_final_assignment (op, wfl_op))
9380 error_found = 1;
9382 /* From now on, we know that op if a variable and that it has a
9383 valid wfl. We use wfl_op to locate errors related to the
9384 ++/-- operand. */
9385 else if (!JNUMERIC_TYPE_P (op_type))
9387 parse_error_context
9388 (wfl_op, "Invalid argument type `%s' to `%s'",
9389 lang_printable_name (op_type, 0), operator_string (node));
9390 TREE_TYPE (node) = error_mark_node;
9391 error_found = 1;
9393 else
9395 /* Before the addition, binary numeric promotion is performed on
9396 both operands */
9397 value = build_int_2 (1, 0);
9398 TREE_TYPE (node) =
9399 binary_numeric_promotion (op_type, TREE_TYPE (value), &op, &value);
9400 /* And write the promoted incremented and increment */
9401 TREE_OPERAND (node, 0) = op;
9402 TREE_OPERAND (node, 1) = value;
9403 /* Convert the overall back into its original type. */
9404 return fold (convert (op_type, node));
9406 break;
9408 /* 15.14.3 Unary Plus Operator + */
9409 case UNARY_PLUS_EXPR:
9410 /* 15.14.4 Unary Minus Operator - */
9411 case NEGATE_EXPR:
9412 if (!JNUMERIC_TYPE_P (op_type))
9414 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
9415 TREE_TYPE (node) = error_mark_node;
9416 error_found = 1;
9418 /* Unary numeric promotion is performed on operand */
9419 else
9421 op = do_unary_numeric_promotion (op);
9422 prom_type = TREE_TYPE (op);
9423 if (code == UNARY_PLUS_EXPR)
9424 return fold (op);
9426 break;
9428 /* 15.14.5 Bitwise Complement Operator ~ */
9429 case BIT_NOT_EXPR:
9430 if (!JINTEGRAL_TYPE_P (op_type))
9432 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
9433 TREE_TYPE (node) = error_mark_node;
9434 error_found = 1;
9436 else
9438 op = do_unary_numeric_promotion (op);
9439 prom_type = TREE_TYPE (op);
9441 break;
9443 /* 15.14.6 Logical Complement Operator ! */
9444 case TRUTH_NOT_EXPR:
9445 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
9447 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
9448 /* But the type is known. We will report an error if further
9449 attempt of a assignment is made with this rhs */
9450 TREE_TYPE (node) = boolean_type_node;
9451 error_found = 1;
9453 else
9454 prom_type = boolean_type_node;
9455 break;
9457 /* 15.15 Cast Expression */
9458 case CONVERT_EXPR:
9459 value = patch_cast (node, wfl_operator);
9460 if (value == error_mark_node)
9462 /* If this cast is part of an assignment, we tell the code
9463 that deals with it not to complain about a mismatch,
9464 because things have been cast, anyways */
9465 TREE_TYPE (node) = error_mark_node;
9466 error_found = 1;
9468 else
9469 return fold (value);
9470 break;
9473 if (error_found)
9474 return error_mark_node;
9476 /* There are cases where node has been replaced by something else
9477 and we don't end up returning here: UNARY_PLUS_EXPR,
9478 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
9479 TREE_OPERAND (node, 0) = fold (op);
9480 TREE_TYPE (node) = prom_type;
9481 return fold (node);
9484 /* Generic type resolution that sometimes takes place during node
9485 patching. Returned the resolved type or generate an error
9486 message. Return the resolved type or NULL_TREE. */
9488 static tree
9489 resolve_type_during_patch (type)
9490 tree type;
9492 if (unresolved_type_p (type, NULL))
9494 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
9495 if (!type_decl)
9497 parse_error_context (type,
9498 "Class `%s' not found in type declaration",
9499 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
9500 return NULL_TREE;
9502 else
9504 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
9505 return TREE_TYPE (type_decl);
9508 return type;
9510 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
9511 found. Otherwise NODE or something meant to replace it is returned. */
9513 static tree
9514 patch_cast (node, wfl_operator)
9515 tree node;
9516 tree wfl_operator;
9518 tree op = TREE_OPERAND (node, 0);
9519 tree op_type = TREE_TYPE (op);
9520 tree cast_type = TREE_TYPE (node);
9521 char *t1;
9523 /* First resolve OP_TYPE if unresolved */
9524 if (!(cast_type = resolve_type_during_patch (cast_type)))
9525 return error_mark_node;
9527 /* Check on cast that are proven correct at compile time */
9528 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
9530 static tree convert_narrow ();
9531 /* Same type */
9532 if (cast_type == op_type)
9533 return node;
9535 /* Try widening/narowwing convertion. Potentially, things need
9536 to be worked out in gcc so we implement the extreme cases
9537 correctly. fold_convert() needs to be fixed. */
9538 return convert (cast_type, op);
9541 /* null can be casted to references */
9542 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
9543 return build_null_of_type (cast_type);
9545 /* The remaining legal casts involve conversion between reference
9546 types. Check for their compile time correctness. */
9547 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
9548 && valid_ref_assignconv_cast_p (cast_type, op_type, 1))
9550 TREE_TYPE (node) = promote_type (cast_type);
9551 /* Now, the case can be determined correct at compile time if
9552 OP_TYPE can be converted into CAST_TYPE by assignment
9553 conversion (5.2) */
9555 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
9557 TREE_SET_CODE (node, NOP_EXPR);
9558 return node;
9561 if (flag_emit_class_files)
9563 TREE_SET_CODE (node, CONVERT_EXPR);
9564 return node;
9567 /* The cast requires a run-time check */
9568 return build (CALL_EXPR, promote_type (cast_type),
9569 build_address_of (soft_checkcast_node),
9570 tree_cons (NULL_TREE, build_class_ref (cast_type),
9571 build_tree_list (NULL_TREE, op)),
9572 NULL_TREE);
9575 /* Any other casts are proven incorrect at compile time */
9576 t1 = strdup (lang_printable_name (op_type, 0));
9577 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
9578 t1, lang_printable_name (cast_type, 0));
9579 free (t1);
9580 return error_mark_node;
9583 /* Build a null constant and give it the type TYPE. */
9585 static tree
9586 build_null_of_type (type)
9587 tree type;
9589 tree node = build_int_2 (0, 0);
9590 TREE_TYPE (node) = promote_type (type);
9591 return node;
9594 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
9595 a list of indices. */
9596 static tree
9597 build_array_ref (location, array, index)
9598 int location;
9599 tree array, index;
9601 tree node = build (ARRAY_REF, NULL_TREE, array, index);
9602 EXPR_WFL_LINECOL (node) = location;
9603 return node;
9606 /* 15.12 Array Access Expression */
9608 static tree
9609 patch_array_ref (node)
9610 tree node;
9612 tree array = TREE_OPERAND (node, 0);
9613 tree array_type = TREE_TYPE (array);
9614 tree index = TREE_OPERAND (node, 1);
9615 tree index_type = TREE_TYPE (index);
9616 int error_found = 0;
9618 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9620 if (TREE_CODE (array_type) == POINTER_TYPE)
9621 array_type = TREE_TYPE (array_type);
9623 /* The array reference must be an array */
9624 if (!TYPE_ARRAY_P (array_type))
9626 parse_error_context
9627 (wfl_operator, "`[]' can only be applied to arrays. It can't be "
9628 "applied to `%s'", lang_printable_name (array_type, 0));
9629 TREE_TYPE (node) = error_mark_node;
9630 error_found = 1;
9633 /* The array index underdoes unary numeric promotion. The promoted
9634 type must be int */
9635 index = do_unary_numeric_promotion (index);
9636 if (TREE_TYPE (index) != int_type_node)
9638 int could_cast = valid_cast_to_p (index_type, int_type_node);
9639 parse_error_context
9640 (wfl_operator,
9641 (could_cast ? "Incompatible type for `[]'. Explicit cast needed to "
9642 "convert `%s' to `int'" : "Incompatible type for `[]'. "
9643 "Can't convert `%s' to `int'"),
9644 lang_printable_name (index_type, 0));
9645 TREE_TYPE (node) = error_mark_node;
9646 error_found = 1;
9649 if (error_found)
9650 return error_mark_node;
9652 array_type = TYPE_ARRAY_ELEMENT (array_type);
9654 if (flag_emit_class_files)
9656 TREE_OPERAND (node, 0) = array;
9657 TREE_OPERAND (node, 1) = index;
9659 else
9660 node = build_java_arrayaccess (array, array_type, index);
9661 TREE_TYPE (node) = array_type;
9662 return node;
9665 /* 15.9 Array Creation Expressions */
9667 static tree
9668 build_newarray_node (type, dims, extra_dims)
9669 tree type;
9670 tree dims;
9671 int extra_dims;
9673 tree node =
9674 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
9675 build_int_2 (extra_dims, 0));
9676 return node;
9679 static tree
9680 patch_newarray (node)
9681 tree node;
9683 tree type = TREE_OPERAND (node, 0);
9684 tree dims = TREE_OPERAND (node, 1);
9685 tree cdim, array_type;
9686 int error_found = 0;
9687 int ndims = 0;
9688 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
9690 /* Dimension types are verified. It's better for the types to be
9691 verified in order. */
9692 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
9694 int dim_error = 0;
9695 tree dim = TREE_VALUE (cdim);
9697 /* Dim might have been saved during its evaluation */
9698 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
9700 /* The type of each specified dimension must be an integral type. */
9701 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
9702 dim_error = 1;
9704 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
9705 promoted type must be int. */
9706 else
9708 dim = do_unary_numeric_promotion (dim);
9709 if (TREE_TYPE (dim) != int_type_node)
9710 dim_error = 1;
9713 /* Report errors on types here */
9714 if (dim_error)
9716 parse_error_context
9717 (TREE_PURPOSE (cdim),
9718 "Incompatible type for dimension in array creation expression. "
9719 "%s convert `%s' to `int'",
9720 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
9721 "Explicit cast needed to" : "Can't"),
9722 lang_printable_name (TREE_TYPE (dim), 0));
9723 error_found = 1;
9726 TREE_PURPOSE (cdim) = NULL_TREE;
9729 /* Resolve array base type if unresolved */
9730 if (!(type = resolve_type_during_patch (type)))
9731 error_found = 1;
9733 if (error_found)
9735 /* We don't want further evaluation of this bogus array creation
9736 operation */
9737 TREE_TYPE (node) = error_mark_node;
9738 return error_mark_node;
9741 /* Set array_type to the actual (promoted) array type of the result. */
9742 if (TREE_CODE (type) == RECORD_TYPE)
9743 type = build_pointer_type (type);
9744 while (--xdims >= 0)
9746 type = promote_type (build_java_array_type (type, -1));
9748 dims = nreverse (dims);
9749 array_type = type;
9750 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
9752 type = array_type;
9753 array_type = build_java_array_type (type,
9754 TREE_CODE (cdim) == INTEGER_CST ?
9755 TREE_INT_CST_LOW (cdim) : -1);
9756 array_type = promote_type (array_type);
9758 dims = nreverse (dims);
9760 /* The node is transformed into a function call. Things are done
9761 differently according to the number of dimensions. If the number
9762 of dimension is equal to 1, then the nature of the base type
9763 (primitive or not) matters. */
9764 if (ndims == 1)
9765 return build_new_array (type, TREE_VALUE (dims));
9767 /* Can't reuse what's already written in expr.c because it uses the
9768 JVM stack representation. Provide a build_multianewarray. FIXME */
9769 return build (CALL_EXPR, array_type,
9770 build_address_of (soft_multianewarray_node),
9771 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
9772 tree_cons (NULL_TREE,
9773 build_int_2 (ndims, 0), dims )),
9774 NULL_TREE);
9777 /* 10.6 Array initializer. */
9779 /* Build a wfl for array element that don't have one, so we can
9780 pin-point errors. */
9782 static tree
9783 maybe_build_array_element_wfl (node)
9784 tree node;
9786 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
9787 return build_expr_wfl (NULL_TREE, ctxp->filename,
9788 ctxp->elc.line, ctxp->elc.prev_col);
9789 else
9790 return NULL_TREE;
9793 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
9794 identification of initialized arrays easier to detect during walk
9795 and expansion. */
9797 static tree
9798 build_new_array_init (location, values)
9799 int location;
9800 tree values;
9802 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
9803 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
9804 EXPR_WFL_LINECOL (to_return) = EXPR_WFL_LINECOL (constructor) = location;
9805 return to_return;
9808 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
9809 occurred. Otherwise return NODE after having set its type
9810 appropriately. */
9812 static tree
9813 patch_new_array_init (type, node)
9814 tree type, node;
9816 int error_seen = 0;
9817 tree current, element_type;
9818 HOST_WIDE_INT length;
9819 int all_constant = 1;
9820 tree init = TREE_OPERAND (node, 0);
9822 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
9824 parse_error_context (node,
9825 "Invalid array initializer for non-array type `%s'",
9826 lang_printable_name (type, 1));
9827 return error_mark_node;
9829 type = TREE_TYPE (type);
9830 element_type = TYPE_ARRAY_ELEMENT (type);
9832 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
9834 for (length = 0, current = CONSTRUCTOR_ELTS (init);
9835 current; length++, current = TREE_CHAIN (current))
9837 tree elt = TREE_VALUE (current);
9838 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
9840 error_seen |= array_constructor_check_entry (element_type, current);
9841 if (! TREE_CONSTANT (TREE_VALUE (current)))
9842 all_constant = 0;
9844 else
9846 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
9847 TREE_PURPOSE (current) = NULL_TREE;
9848 all_constant = 0;
9850 if (elt && TREE_VALUE (elt) == error_mark_node)
9851 error_seen = 1;
9854 if (error_seen)
9855 return error_mark_node;
9857 /* Create a new type. We can't reuse the one we have here by
9858 patching its dimension because it originally is of dimension -1
9859 hence reused by gcc. This would prevent triangular arrays. */
9860 type = build_java_array_type (element_type, length);
9861 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
9862 TREE_TYPE (node) = promote_type (type);
9863 TREE_CONSTANT (init) = all_constant;
9864 return node;
9867 /* Verify that one entry of the initializer element list can be
9868 assigned to the array base type. Report 1 if an error occurred, 0
9869 otherwise. */
9871 static int
9872 array_constructor_check_entry (type, entry)
9873 tree type, entry;
9875 char *array_type_string = NULL; /* For error reports */
9876 tree value, type_value, new_value, wfl_value, patched;
9877 int error_seen = 0;
9879 new_value = NULL_TREE;
9880 wfl_value = TREE_VALUE (entry);
9882 value = java_complete_tree (TREE_VALUE (entry));
9883 /* patch_string return error_mark_node if arg is error_mark_node */
9884 if ((patched = patch_string (value)))
9885 value = patched;
9886 if (value == error_mark_node)
9887 return 1;
9889 type_value = TREE_TYPE (value);
9891 /* At anytime, try_builtin_assignconv can report a warning on
9892 constant overflow during narrowing. */
9893 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
9894 new_value = try_builtin_assignconv (wfl_operator, type, value);
9895 if (!new_value && (new_value = try_reference_assignconv (type, value)))
9896 type_value = promote_type (type);
9898 /* Check and report errors */
9899 if (!new_value)
9901 char *msg = (!valid_cast_to_p (type_value, type) ?
9902 "Can't" : "Explicit cast needed to");
9903 if (!array_type_string)
9904 array_type_string = strdup (lang_printable_name (type, 1));
9905 parse_error_context
9906 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
9907 msg, lang_printable_name (type_value, 1), array_type_string);
9908 error_seen = 1;
9911 if (new_value)
9913 new_value = maybe_build_primttype_type_ref (new_value, wfl_operator);
9914 TREE_VALUE (entry) = new_value;
9917 if (array_type_string)
9918 free (array_type_string);
9920 TREE_PURPOSE (entry) = NULL_TREE;
9921 return error_seen;
9924 static tree
9925 build_this (location)
9926 int location;
9928 tree node = build_wfl_node (this_identifier_node, input_filename, 0, 0);
9929 TREE_SET_CODE (node, THIS_EXPR);
9930 EXPR_WFL_LINECOL (node) = location;
9931 return node;
9934 /* 14.15 The return statement. It builds a modify expression that
9935 assigns the returned value to the RESULT_DECL that hold the value
9936 to be returned. */
9938 static tree
9939 build_return (location, op)
9940 int location;
9941 tree op;
9943 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
9944 EXPR_WFL_LINECOL (node) = location;
9945 node = build_debugable_stmt (location, node);
9946 return node;
9949 static tree
9950 patch_return (node)
9951 tree node;
9953 tree return_exp = TREE_OPERAND (node, 0);
9954 tree meth = current_function_decl;
9955 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
9956 int error_found = 0;
9958 TREE_TYPE (node) = error_mark_node;
9959 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9961 /* It's invalid to have a return value within a function that is
9962 declared with the keyword void or that is a constructor */
9963 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
9964 error_found = 1;
9966 /* It's invalid to have a no return value within a function that
9967 isn't declared with the keyword `void' */
9968 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
9969 error_found = 2;
9971 if (error_found)
9973 if (!DECL_CONSTRUCTOR_P (meth))
9975 char *t = strdup (lang_printable_name (mtype, 0));
9976 parse_error_context (wfl_operator,
9977 "`return' with%s value from `%s %s'",
9978 (error_found == 1 ? "" : "out"),
9979 t, lang_printable_name (meth, 0));
9980 free (t);
9982 else
9983 parse_error_context (wfl_operator,
9984 "`return' with value from constructor `%s'",
9985 lang_printable_name (meth, 0));
9986 return error_mark_node;
9989 /* If we have a return_exp, build a modify expression and expand
9990 it. Note: at that point, the assignment is declared valid, but we
9991 may want to carry some more hacks */
9992 if (return_exp)
9994 tree exp = java_complete_tree (return_exp);
9995 tree modify, patched;
9997 /* If the function returned value and EXP are booleans, EXP has
9998 to be converted into the type of DECL_RESULT, which is integer
9999 (see complete_start_java_method) */
10000 if (TREE_TYPE (exp) == boolean_type_node &&
10001 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
10002 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
10004 /* `null' can be assigned to a function returning a reference */
10005 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
10006 exp == null_pointer_node)
10007 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
10009 if ((patched = patch_string (exp)))
10010 exp = patched;
10012 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
10013 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
10014 modify = java_complete_tree (modify);
10016 if (modify != error_mark_node)
10018 TREE_SIDE_EFFECTS (modify) = 1;
10019 TREE_OPERAND (node, 0) = modify;
10021 else
10022 return error_mark_node;
10024 TREE_TYPE (node) = void_type_node;
10025 TREE_SIDE_EFFECTS (node) = 1;
10026 return node;
10029 /* 14.8 The if Statement */
10031 static tree
10032 build_if_else_statement (location, expression, if_body, else_body)
10033 int location;
10034 tree expression, if_body, else_body;
10036 tree node;
10037 if (!else_body)
10038 else_body = empty_stmt_node;
10039 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
10040 EXPR_WFL_LINECOL (node) = location;
10041 node = build_debugable_stmt (location, node);
10042 return node;
10045 static tree
10046 patch_if_else_statement (node)
10047 tree node;
10049 tree expression = TREE_OPERAND (node, 0);
10051 TREE_TYPE (node) = error_mark_node;
10052 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10054 /* The type of expression must be boolean */
10055 if (TREE_TYPE (expression) != boolean_type_node
10056 && TREE_TYPE (expression) != promoted_boolean_type_node)
10058 parse_error_context
10059 (wfl_operator,
10060 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
10061 lang_printable_name (TREE_TYPE (expression), 0));
10062 return error_mark_node;
10065 TREE_TYPE (node) = void_type_node;
10066 TREE_SIDE_EFFECTS (node) = 1;
10067 CAN_COMPLETE_NORMALLY (node)
10068 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10069 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
10070 return node;
10073 /* 14.6 Labeled Statements */
10075 /* Action taken when a lableled statement is parsed. a new
10076 LABELED_BLOCK_EXPR is created. No statement is attached to the
10077 label, yet. */
10079 static tree
10080 build_labeled_block (location, label)
10081 int location;
10082 tree label;
10084 tree label_name = merge_qualified_name (label_id, label);
10085 tree label_decl, node;
10087 /* Issue an error if we try to reuse a label that was previously
10088 declared */
10089 if (IDENTIFIER_LOCAL_VALUE (label_name))
10091 EXPR_WFL_LINECOL (wfl_operator) = location;
10092 parse_error_context (wfl_operator, "Declaration of `%s' shadows "
10093 "a previous label declaration",
10094 IDENTIFIER_POINTER (label));
10095 EXPR_WFL_LINECOL (wfl_operator) =
10096 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
10097 parse_error_context (wfl_operator, "This is the location of the "
10098 "previous declaration of label `%s'",
10099 IDENTIFIER_POINTER (label));
10100 java_error_count--;
10103 label_decl = create_label_decl (label_name);
10104 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
10105 EXPR_WFL_LINECOL (node) = location;
10106 TREE_SIDE_EFFECTS (node) = 1;
10107 return node;
10110 /* Generate a label crafting a unique name for it. This is used to
10111 implicitely label loops that aren't the body part of labeled
10112 statement. */
10114 static tree
10115 generate_labeled_block ()
10117 return build_labeled_block (0, generate_name ());
10120 /* A labeled statement LBE is attached a statement. */
10122 static tree
10123 complete_labeled_statement (lbe, statement)
10124 tree lbe; /* Labeled block expr */
10125 tree statement;
10127 /* In anyways, tie the loop to its statement */
10128 LABELED_BLOCK_BODY (lbe) = statement;
10130 /* Ok, if statement is a for loop, we have to attach the labeled
10131 statement to the block the for loop belongs to and return the
10132 block instead */
10133 if (TREE_CODE (statement) == LOOP_EXPR && IS_FOR_LOOP_P (statement))
10135 java_method_add_stmt (current_function_decl, lbe);
10136 return exit_block ();
10139 return lbe;
10142 /* 14.10, 14.11, 14.12 Loop Statements */
10144 /* Create an empty LOOP_EXPR and make it the last in the nested loop
10145 list. */
10147 static tree
10148 build_new_loop (loop_body)
10149 tree loop_body;
10151 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
10152 TREE_SIDE_EFFECTS (loop) = 1;
10153 PUSH_LOOP (loop);
10154 return loop;
10157 /* Create a loop body according to the following structure:
10158 COMPOUND_EXPR
10159 COMPOUND_EXPR (loop main body)
10160 EXIT_EXPR (this order is for while/for loops.
10161 LABELED_BLOCK_EXPR the order is reversed for do loops)
10162 LABEL_DECL (continue occurding here branche at the
10163 BODY end of this labeled block)
10164 INCREMENT (if any)
10166 REVERSED, if non zero, tells that the loop condition expr comes
10167 after the body, like in the do-while loop.
10169 To obtain a loop, the loop body structure described above is
10170 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
10172 LABELED_BLOCK_EXPR
10173 LABEL_DECL (use this label to exit the loop)
10174 LOOP_EXPR
10175 <structure described above> */
10177 static tree
10178 build_loop_body (location, condition, reversed)
10179 int location;
10180 tree condition;
10181 int reversed;
10183 tree first, second, body;
10185 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
10186 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
10187 condition = build_debugable_stmt (location, condition);
10188 TREE_SIDE_EFFECTS (condition) = 1;
10190 body = generate_labeled_block ();
10191 first = (reversed ? body : condition);
10192 second = (reversed ? condition : body);
10193 return
10194 build (COMPOUND_EXPR, NULL_TREE,
10195 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
10198 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
10199 their order) on the current loop. Unlink the current loop from the
10200 loop list. */
10202 static tree
10203 complete_loop_body (location, condition, body, reversed)
10204 int location;
10205 tree condition, body;
10206 int reversed;
10208 tree to_return = ctxp->current_loop;
10209 tree loop_body = LOOP_EXPR_BODY (to_return);
10210 if (condition)
10212 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
10213 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
10214 The real EXIT_EXPR is one operand further. */
10215 EXPR_WFL_LINECOL (cnode) = location;
10216 /* This one is for accurate error reports */
10217 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
10218 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
10220 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
10221 POP_LOOP ();
10222 return to_return;
10225 /* Tailored version of complete_loop_body for FOR loops, when FOR
10226 loops feature the condition part */
10228 static tree
10229 complete_for_loop (location, condition, update, body)
10230 int location;
10231 tree condition, update, body;
10233 /* Put the condition and the loop body in place */
10234 tree loop = complete_loop_body (location, condition, body, 0);
10235 /* LOOP is the current loop which has been now popped of the loop
10236 stack. Install the update block */
10237 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
10238 return loop;
10241 /* If the loop isn't surrounded by a labeled statement, create one and
10242 insert LOOP as it's body. */
10244 static tree
10245 patch_loop_statement (loop)
10246 tree loop;
10248 tree loop_label, to_return_as_loop;
10250 if (LOOP_HAS_LABEL_P (loop))
10252 loop_label = ctxp->current_labeled_block;
10253 to_return_as_loop = loop;
10255 else
10257 loop_label = generate_labeled_block ();
10258 LABELED_BLOCK_BODY (loop_label) = loop;
10259 PUSH_LABELED_BLOCK (loop_label);
10260 to_return_as_loop = loop_label;
10262 TREE_TYPE (to_return_as_loop) = void_type_node;
10263 return to_return_as_loop;
10266 /* 14.13, 14.14: break and continue Statements */
10268 /* Build a break or a continue statement. a null NAME indicates an
10269 unlabeled break/continue statement. */
10271 static tree
10272 build_bc_statement (location, is_break, name)
10273 int location, is_break;
10274 tree name;
10276 tree break_continue, label_block_expr = NULL_TREE;
10278 if (name)
10280 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
10281 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
10282 /* Null means that we don't have a target for this named
10283 break/continue. In this case, we make the target to be the
10284 label name, so that the error can be reported accuratly in
10285 patch_bc_statement. */
10286 label_block_expr = EXPR_WFL_NODE (name);
10288 /* Unlabeled break/continue will be handled during the
10289 break/continue patch operation */
10290 break_continue
10291 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
10293 IS_BREAK_STMT_P (break_continue) = is_break;
10294 TREE_SIDE_EFFECTS (break_continue) = 1;
10295 EXPR_WFL_LINECOL (break_continue) = location;
10296 break_continue = build_debugable_stmt (location, break_continue);
10297 return break_continue;
10300 /* Verification of a break/continue statement. */
10302 static tree
10303 patch_bc_statement (node)
10304 tree node;
10306 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
10307 int is_unlabeled = 0;
10308 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10310 /* Not having a target means that the break/continue statement is
10311 unlabeled. We try to find a decent label for it */
10312 if (!bc_label)
10314 is_unlabeled = 1;
10315 /* There should be a loop/switch to branch to */
10316 if (ctxp->current_loop)
10318 if (TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10320 /* At that stage, we're in the loop body, which is
10321 encapsulated around a LABELED_BLOCK_EXPR. So searching
10322 the current loop label requires us to consider the
10323 labeled block before the current one. */
10324 if (!LOOP_HAS_LABEL_SKIP_P (ctxp->current_loop))
10325 fatal ("unlabeled loop has no installed label -- "
10326 "patch_bc_statement");
10327 bc_label = TREE_CHAIN (ctxp->current_labeled_block);
10329 /* For a SWITCH statement, this is the current one */
10330 else
10331 bc_label = ctxp->current_labeled_block;
10333 /* Not having a loop to break/continue to is an error */
10334 else
10336 parse_error_context (wfl_operator, "`%s' must be in loop%s",
10337 (IS_BREAK_STMT_P (node) ? "break" : "continue"),
10338 (IS_BREAK_STMT_P (node) ? " or switch" : ""));
10339 return error_mark_node;
10342 /* Having an identifier here means that the target is unknown. */
10343 else if (TREE_CODE (bc_label) == IDENTIFIER_NODE)
10345 parse_error_context (wfl_operator, "No label definition found for `%s'",
10346 IDENTIFIER_POINTER (bc_label));
10347 return error_mark_node;
10350 /* Find the statement we're targeting. */
10351 target_stmt = LABELED_BLOCK_BODY (bc_label);
10353 /* 14.13 The break Statement */
10354 if (IS_BREAK_STMT_P (node))
10356 /* Named break are always fine, as far as they have a target
10357 (already verified). Anonymous break need to target
10358 while/do/for/switch */
10359 if (is_unlabeled &&
10360 !(TREE_CODE (target_stmt) == LOOP_EXPR /* do/while/for */
10361 || TREE_CODE (target_stmt) == SWITCH_EXPR)) /* switch FIXME */
10363 parse_error_context (wfl_operator,
10364 "`break' must be in loop or switch");
10365 return error_mark_node;
10367 /* If previously unlabeled, install the new found label */
10368 if (is_unlabeled)
10369 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
10371 /* 14.14 The continue Statement */
10372 /* The continue statement must always target a loop */
10373 else
10375 if (TREE_CODE (target_stmt) != LOOP_EXPR) /* do/while/for */
10377 parse_error_context (wfl_operator, "`continue' must be in loop");
10378 return error_mark_node;
10380 /* Everything looks good. We can fix the `continue' jump to go
10381 at the place in the loop were the continue is. The continue
10382 is the current labeled block, by construction. */
10383 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label = ctxp->current_labeled_block;
10386 CAN_COMPLETE_NORMALLY (bc_label) = 1;
10388 /* Our break/continue don't return values. */
10389 TREE_TYPE (node) = void_type_node;
10390 /* Encapsulate the break within a compound statement so that it's
10391 expanded all the times by expand_expr (and not clobered
10392 sometimes, like after a if statement) */
10393 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
10394 TREE_SIDE_EFFECTS (node) = 1;
10395 return node;
10398 /* Process the exit expression belonging to a loop. Its type must be
10399 boolean. */
10401 static tree
10402 patch_exit_expr (node)
10403 tree node;
10405 tree expression = TREE_OPERAND (node, 0);
10406 TREE_TYPE (node) = error_mark_node;
10407 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10409 /* The type of expression must be boolean */
10410 if (TREE_TYPE (expression) != boolean_type_node)
10412 parse_error_context
10413 (wfl_operator,
10414 "Incompatible type for loop conditional. Can't convert `%s' to "
10415 "`boolean'",
10416 lang_printable_name (TREE_TYPE (expression), 0));
10417 return error_mark_node;
10419 /* Now we know things are allright, invert the condition, fold and
10420 return */
10421 TREE_OPERAND (node, 0) =
10422 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
10424 if (! integer_zerop (TREE_OPERAND (node, 0))
10425 && ctxp->current_loop != NULL_TREE
10426 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10427 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
10428 if (! integer_onep (TREE_OPERAND (node, 0)))
10429 CAN_COMPLETE_NORMALLY (node) = 1;
10432 TREE_TYPE (node) = void_type_node;
10433 return node;
10436 /* 14.9 Switch statement */
10438 static tree
10439 patch_switch_statement (node)
10440 tree node;
10442 tree se = TREE_OPERAND (node, 0), se_type;
10444 /* Complete the switch expression */
10445 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
10446 se_type = TREE_TYPE (se);
10447 /* The type of the switch expression must be char, byte, short or
10448 int */
10449 if (!JINTEGRAL_TYPE_P (se_type))
10451 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10452 parse_error_context (wfl_operator, "Incompatible type for `switch'. "
10453 "Can't convert `%s' to `int'",
10454 lang_printable_name (se_type, 0));
10455 /* This is what java_complete_tree will check */
10456 TREE_OPERAND (node, 0) = error_mark_node;
10457 return error_mark_node;
10460 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10462 /* Ready to return */
10463 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
10465 TREE_TYPE (node) = error_mark_node;
10466 return error_mark_node;
10468 TREE_TYPE (node) = void_type_node;
10469 TREE_SIDE_EFFECTS (node) = 1;
10470 CAN_COMPLETE_NORMALLY (node)
10471 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10472 || ! SWITCH_HAS_DEFAULT (node);
10473 return node;
10476 /* 14.18 The try statement */
10478 /* Wrap BLOCK around a LABELED_BLOCK, set DECL to the newly generated
10479 exit labeld and issue a jump to FINALLY_LABEL:
10481 LABELED_BLOCK
10482 BLOCK
10483 <orignal_statments>
10484 DECL = &LABEL_DECL
10485 GOTO_EXPR
10486 FINALLY_LABEL
10487 LABEL_DECL */
10489 static tree
10490 build_jump_to_finally (block, decl, finally_label, type)
10491 tree block, decl, finally_label, type;
10493 tree stmt;
10494 tree new_block = build (LABELED_BLOCK_EXPR, type,
10495 create_label_decl (generate_name ()), block);
10497 stmt = build (MODIFY_EXPR, void_type_node, decl,
10498 build_address_of (LABELED_BLOCK_LABEL (new_block)));
10499 TREE_SIDE_EFFECTS (stmt) = 1;
10500 CAN_COMPLETE_NORMALLY (stmt) = 1;
10501 add_stmt_to_block (block, type, stmt);
10502 stmt = build (GOTO_EXPR, void_type_node, finally_label);
10503 TREE_SIDE_EFFECTS (stmt) = 1;
10504 add_stmt_to_block (block, type, stmt);
10505 return new_block;
10508 static tree
10509 build_try_statement (location, try_block, catches, finally)
10510 int location;
10511 tree try_block, catches, finally;
10513 tree node, rff;
10515 if (finally && ! flag_emit_class_files)
10517 /* This block defines a scope for the entire try[-catch]-finally
10518 sequence. It hold a local variable used to return from the
10519 finally using a computed goto. We call it
10520 return_from_finally (RFF). */
10521 rff = build_decl (VAR_DECL, generate_name (), return_address_type_node);
10523 /* Modification of the try block. */
10524 try_block = build_jump_to_finally (try_block, rff,
10525 FINALLY_EXPR_LABEL (finally),
10526 NULL_TREE);
10528 /* To the finally block: add the computed goto */
10529 add_stmt_to_block (FINALLY_EXPR_BLOCK (finally), NULL_TREE,
10530 build (GOTO_EXPR, void_type_node, rff));
10532 /* Modification of each catch blocks, if any */
10533 if (catches)
10535 tree catch, catch_decl, catch_block, stmt;
10537 for (catch = catches; catch; catch = TREE_CHAIN (catch))
10538 TREE_OPERAND (catch, 0) =
10539 build_jump_to_finally (TREE_OPERAND (catch, 0), rff,
10540 FINALLY_EXPR_LABEL (finally),
10541 NULL_TREE);
10543 /* Plus, at the end of the list, we add the catch clause that
10544 will catch an uncaught exception, call finally and rethrow it:
10545 BLOCK
10546 void *exception_parameter; (catch_decl)
10547 LABELED_BLOCK
10548 BLOCK
10549 exception_parameter = _Jv_exception_info ();
10550 RFF = &LABEL_DECL;
10551 goto finally;
10552 LABEL_DECL;
10553 CALL_EXPR
10554 Jv_ReThrow
10555 exception_parameter */
10556 catch_decl = build_decl (VAR_DECL, generate_name (), ptr_type_node);
10557 BUILD_ASSIGN_EXCEPTION_INFO (stmt, catch_decl);
10558 catch_block = build_expr_block (stmt, NULL_TREE);
10559 catch_block = build_jump_to_finally (catch_block, rff,
10560 FINALLY_EXPR_LABEL (finally),
10561 void_type_node);
10562 BUILD_THROW (stmt, catch_decl);
10563 catch_block = build_expr_block (catch_block, catch_decl);
10564 add_stmt_to_block (catch_block, void_type_node, stmt);
10566 /* Link the new handler to the existing list as the first
10567 entry. It will be the last one to be generated. */
10568 catch = build1 (CATCH_EXPR, void_type_node, catch_block);
10569 TREE_CHAIN (catch) = catches;
10570 catches = catch;
10574 node = build (TRY_EXPR, NULL_TREE, try_block, catches, finally);
10575 EXPR_WFL_LINECOL (node) = location;
10577 /* If we have a finally, surround this whole thing by a block where
10578 the RFF local variable is defined. */
10580 return (finally && ! flag_emit_class_files ? build_expr_block (node, rff)
10581 : node);
10584 /* Get the catch clause block from an element of the catch clause
10585 list. If depends on whether a finally clause exists or node (in
10586 which case the original catch clause was surrounded by a
10587 LABELED_BLOCK_EXPR. */
10589 tree
10590 java_get_catch_block (node, finally_present_p)
10591 tree node;
10592 int finally_present_p;
10594 return (CATCH_EXPR_GET_EXPR (TREE_OPERAND (node, 0), finally_present_p));
10597 static tree
10598 patch_try_statement (node)
10599 tree node;
10601 int error_found = 0;
10602 tree try = TREE_OPERAND (node, 0);
10603 /* Exception handlers are considered in left to right order */
10604 tree catch = nreverse (TREE_OPERAND (node, 1));
10605 tree finally = TREE_OPERAND (node, 2);
10606 int finally_p = (finally ? 1 : 0);
10607 tree current, caught_type_list = NULL_TREE;
10609 /* Check catch clauses, if any. Every time we find an error, we try
10610 to process the next catch clause. We process the catch clause before
10611 the try block so that when processing the try block we can check thrown
10612 exceptions againts the caught type list. */
10613 for (current = catch; current; current = TREE_CHAIN (current))
10615 tree carg_decl, carg_type;
10616 tree sub_current, catch_block, catch_clause;
10617 int unreachable;
10619 /* Always detect the last catch clause if a finally is
10620 present. This is the catch-all handler and it just needs to
10621 be walked. */
10622 if (!TREE_CHAIN (current) && finally)
10624 TREE_OPERAND (current, 0) =
10625 java_complete_tree (TREE_OPERAND (current, 0));
10626 continue;
10629 /* At this point, the structure of the catch clause is
10630 LABELED_BLOCK_EXPR (if we have a finally)
10631 CATCH_EXPR (catch node)
10632 BLOCK (with the decl of the parameter)
10633 COMPOUND_EXPR
10634 MODIFY_EXPR (assignment of the catch parameter)
10635 BLOCK (catch clause block)
10636 LABEL_DECL (where to return after finally (if any))
10638 Since the structure of the catch clause depends on the
10639 presence of a finally, we use a function call to get to the
10640 cath clause */
10641 catch_clause = java_get_catch_block (current, finally_p);
10642 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
10643 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
10645 /* Catch clauses can't have more than one parameter declared,
10646 but it's already enforced by the grammar. Make sure that the
10647 only parameter of the clause statement in of class Throwable
10648 or a subclass of Throwable, but that was done earlier. The
10649 catch clause parameter type has also been resolved. */
10651 /* Just make sure that the catch clause parameter type inherits
10652 from java.lang.Throwable */
10653 if (!inherits_from_p (carg_type, throwable_type_node))
10655 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10656 parse_error_context (wfl_operator,
10657 "Can't catch class `%s'. Catch clause "
10658 "parameter type must be a subclass of "
10659 "class `java.lang.Throwable'",
10660 lang_printable_name (carg_type, 0));
10661 error_found = 1;
10662 continue;
10665 /* Partial check for unreachable catch statement: The catch
10666 clause is reachable iff is no earlier catch block A in
10667 the try statement such that the type of the catch
10668 clause's parameter is the same as or a subclass of the
10669 type of A's parameter */
10670 unreachable = 0;
10671 for (sub_current = catch;
10672 sub_current != current; sub_current = TREE_CHAIN (sub_current))
10674 tree sub_catch_clause, decl;
10675 sub_catch_clause = java_get_catch_block (sub_current, finally_p);
10676 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
10678 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
10680 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10681 parse_error_context
10682 (wfl_operator, "`catch' not reached because of the catch "
10683 "clause at line %d", EXPR_WFL_LINENO (sub_current));
10684 unreachable = error_found = 1;
10685 break;
10688 /* Complete the catch clause block */
10689 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
10690 if (catch_block == error_mark_node)
10692 error_found = 1;
10693 continue;
10695 if (CAN_COMPLETE_NORMALLY (catch_block))
10696 CAN_COMPLETE_NORMALLY (node) = 1;
10697 TREE_OPERAND (current, 0) = catch_block;
10699 if (unreachable)
10700 continue;
10702 /* Things to do here: the exception must be thrown */
10704 /* Link this type to the caught type list */
10705 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
10708 PUSH_EXCEPTIONS (caught_type_list);
10709 if ((try = java_complete_tree (try)) == error_mark_node)
10710 error_found = 1;
10711 if (CAN_COMPLETE_NORMALLY (try))
10712 CAN_COMPLETE_NORMALLY (node) = 1;
10713 POP_EXCEPTIONS ();
10715 /* Process finally */
10716 if (finally)
10718 current = java_complete_tree (FINALLY_EXPR_BLOCK (finally));
10719 FINALLY_EXPR_BLOCK (finally) = current;
10720 if (current == error_mark_node)
10721 error_found = 1;
10722 if (! CAN_COMPLETE_NORMALLY (current))
10723 CAN_COMPLETE_NORMALLY (node) = 0;
10726 /* Verification ends here */
10727 if (error_found)
10728 return error_mark_node;
10730 TREE_OPERAND (node, 0) = try;
10731 TREE_OPERAND (node, 1) = catch;
10732 TREE_OPERAND (node, 2) = finally;
10733 TREE_TYPE (node) = void_type_node;
10734 return node;
10737 /* 14.17 The synchronized Statement */
10739 static tree
10740 patch_synchronized_statement (node, wfl_op1)
10741 tree node, wfl_op1;
10743 tree expr = TREE_OPERAND (node, 0);
10744 tree block = TREE_OPERAND (node, 1);
10745 tree try_block, catch_all, stmt, compound, decl;
10747 /* The TYPE of expr must be a reference type */
10748 if (!JREFERENCE_TYPE_P (TREE_TYPE (TREE_OPERAND (node, 0))))
10750 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10751 parse_error_context (wfl_operator, "Incompatible type for `synchronized'"
10752 ". Can't convert `%s' to `java.lang.Object'",
10753 lang_printable_name (TREE_TYPE (expr), 0));
10754 return error_mark_node;
10757 /* Generate a try-finally for the synchronized statement, except
10758 that the handler that catches all throw exception calls
10759 _Jv_MonitorExit and then rethrow the exception.
10760 The synchronized statement is then implemented as:
10761 TRY
10763 _Jv_MonitorEnter (expression)
10764 synchronized_block
10765 _Jv_MonitorExit (expression)
10767 CATCH_ALL
10769 e = _Jv_exception_info ();
10770 _Jv_MonitorExit (expression)
10771 Throw (e);
10772 } */
10774 /* TRY block */
10775 BUILD_MONITOR_ENTER (stmt, expr);
10776 compound = add_stmt_to_compound (NULL_TREE, int_type_node, stmt);
10777 compound = add_stmt_to_compound (compound, void_type_node, block);
10778 if (CAN_COMPLETE_NORMALLY (block))
10780 BUILD_MONITOR_EXIT (stmt, expr);
10781 compound = add_stmt_to_compound (compound, int_type_node, stmt);
10783 try_block = build_expr_block (compound, NULL_TREE);
10784 CAN_COMPLETE_NORMALLY (try_block) = CAN_COMPLETE_NORMALLY (block);
10786 /* CATCH_ALL block */
10787 decl = build_decl (VAR_DECL, generate_name (), ptr_type_node);
10788 BUILD_ASSIGN_EXCEPTION_INFO (stmt, decl);
10789 compound = add_stmt_to_compound (NULL_TREE, void_type_node, stmt);
10790 BUILD_MONITOR_EXIT (stmt, expr);
10791 compound = add_stmt_to_compound (compound, int_type_node, stmt);
10792 BUILD_THROW (stmt, decl);
10793 compound = add_stmt_to_compound (compound, void_type_node, stmt);
10794 catch_all = build_expr_block (compound, decl);
10795 catch_all = build_expr_block (catch_all, NULL_TREE);
10796 catch_all = build1 (CATCH_EXPR, void_type_node, catch_all);
10798 /* TRY-CATCH statement */
10799 compound = build (TRY_EXPR, void_type_node, try_block, catch_all, NULL_TREE);
10800 CAN_COMPLETE_NORMALLY (compound) = CAN_COMPLETE_NORMALLY (try_block);
10801 return compound;
10804 /* 14.16 The throw Statement */
10806 static tree
10807 patch_throw_statement (node, wfl_op1)
10808 tree node, wfl_op1;
10810 tree expr = TREE_OPERAND (node, 0);
10811 tree type = TREE_TYPE (expr);
10812 int unchecked_ok = 0, tryblock_throws_ok = 0;
10814 /* Thrown expression must be assignable to java.lang.Throwable */
10815 if (!try_reference_assignconv (throwable_type_node, expr))
10817 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10818 parse_error_context (wfl_operator, "Can't throw `%s'; it must be a "
10819 "subclass of class `java.lang.Throwable'",
10820 lang_printable_name (type, 0));
10821 /* If the thrown expression was a reference, we further the
10822 compile-time check. */
10823 if (!JREFERENCE_TYPE_P (type))
10824 return error_mark_node;
10827 /* At least one of the following must be true */
10829 /* The type of the throw expression is a not checked exception,
10830 i.e. is a unchecked expression. */
10831 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
10833 /* Throw is contained in a try statement and at least one catch
10834 clause can receive the thrown expression or the current method is
10835 declared to throw such an exception. Or, the throw statement is
10836 contained in a method or constructor declaration and the type of
10837 the Expression is assignable to at least one type listed in the
10838 throws clause the declaration. */
10839 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10840 if (!unchecked_ok)
10841 tryblock_throws_ok =
10842 check_thrown_exceptions_do (TREE_TYPE (expr));
10843 if (!(unchecked_ok || tryblock_throws_ok))
10845 /* If there is a surrounding try block that has no matching
10846 clatch clause, report it first. A surrounding try block exits
10847 only if there is something after the list of checked
10848 exception thrown by the current function (if any). */
10849 if (IN_TRY_BLOCK_P ())
10850 parse_error_context (wfl_operator, "Checked exception `%s' can't be "
10851 "caught by any of the catch clause(s) "
10852 "of the surrounding `try' block",
10853 lang_printable_name (type, 0));
10854 /* If we have no surrounding try statement and the method doesn't have
10855 any throws, report it now. FIXME */
10856 else if (!EXCEPTIONS_P (currently_caught_type_list)
10857 && !tryblock_throws_ok)
10858 parse_error_context (wfl_operator, "Checked exception `%s' isn't "
10859 "thrown from a `try' block",
10860 lang_printable_name (type, 0));
10861 /* Otherwise, the current method doesn't have the appropriate
10862 throws declaration */
10863 else
10864 parse_error_context (wfl_operator, "Checked exception `%s' doesn't "
10865 "match any of current method's `throws' "
10866 "declaration(s)",
10867 lang_printable_name (type, 0));
10868 return error_mark_node;
10871 /* If a throw statement is contained in a static initializer, then a
10872 compile-time check ensures that either its value is always an
10873 unchecked exception or its value is always caught by some try
10874 statement that contains it. FIXME, static initializer. */
10876 if (! flag_emit_class_files)
10877 BUILD_THROW (node, expr);
10878 return node;
10881 /* Check that exception said to be thrown by method DECL can be
10882 effectively caught from where DECL is invoked. */
10884 static void
10885 check_thrown_exceptions (location, decl)
10886 int location;
10887 tree decl;
10889 tree throws;
10890 /* For all the unchecked exceptions thrown by DECL */
10891 for (throws = DECL_FUNCTION_THROWS (decl); throws;
10892 throws = TREE_CHAIN (throws))
10893 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
10895 #if 1
10896 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
10897 if (DECL_NAME (decl) == get_identifier ("clone"))
10898 continue;
10899 #endif
10900 EXPR_WFL_LINECOL (wfl_operator) = location;
10901 parse_error_context
10902 (wfl_operator, "Exception `%s' must be caught, or it must be "
10903 "declared in the `throws' clause of `%s'",
10904 lang_printable_name (TREE_VALUE (throws), 0),
10905 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
10909 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
10910 try-catch blocks, OR is listed in the `throws' clause of the
10911 current method. */
10913 static int
10914 check_thrown_exceptions_do (exception)
10915 tree exception;
10917 tree list = currently_caught_type_list;
10918 resolve_and_layout (exception, NULL_TREE);
10919 /* First, all the nested try-catch-finally at that stage. The
10920 last element contains `throws' clause exceptions, if any. */
10921 if (IS_UNCHECKED_EXCEPTION_P (exception))
10922 return 1;
10923 while (list)
10925 tree caught;
10926 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
10927 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
10928 return 1;
10929 list = TREE_CHAIN (list);
10931 return 0;
10934 static void
10935 purge_unchecked_exceptions (mdecl)
10936 tree mdecl;
10938 tree throws = DECL_FUNCTION_THROWS (mdecl);
10939 tree new = NULL_TREE;
10941 while (throws)
10943 tree next = TREE_CHAIN (throws);
10944 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
10946 TREE_CHAIN (throws) = new;
10947 new = throws;
10949 throws = next;
10951 /* List is inverted here, but it doesn't matter */
10952 DECL_FUNCTION_THROWS (mdecl) = new;
10955 /* 15.24 Conditional Operator ?: */
10957 static tree
10958 patch_conditional_expr (node, wfl_cond, wfl_op1)
10959 tree node, wfl_cond, wfl_op1;
10961 tree cond = TREE_OPERAND (node, 0);
10962 tree op1 = TREE_OPERAND (node, 1);
10963 tree op2 = TREE_OPERAND (node, 2);
10964 tree resulting_type = NULL_TREE;
10965 tree t1, t2, patched;
10966 int error_found = 0;
10968 /* Operands of ?: might be StringBuffers crafted as a result of a
10969 string concatenation. Obtain a descent operand here. */
10970 if ((patched = patch_string (op1)))
10971 TREE_OPERAND (node, 1) = op1 = patched;
10972 if ((patched = patch_string (op2)))
10973 TREE_OPERAND (node, 2) = op2 = patched;
10975 t1 = TREE_TYPE (op1);
10976 t2 = TREE_TYPE (op2);
10978 /* The first expression must be a boolean */
10979 if (TREE_TYPE (cond) != boolean_type_node)
10981 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
10982 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
10983 "convert `%s' to `boolean'",
10984 lang_printable_name (TREE_TYPE (cond), 0));
10985 error_found = 1;
10988 /* Second and third can be numeric, boolean (i.e. primitive),
10989 references or null. Anything else results in an error */
10990 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
10991 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
10992 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
10993 || (t1 == boolean_type_node && t2 == boolean_type_node)))
10994 error_found = 1;
10996 /* Determine the type of the conditional expression. Same types are
10997 easy to deal with */
10998 else if (t1 == t2)
10999 resulting_type = t1;
11001 /* There are different rules for numeric types */
11002 else if (JNUMERIC_TYPE_P (t1))
11004 /* if byte/short found, the resulting type is short */
11005 if ((t1 == byte_type_node && t2 == short_type_node)
11006 || (t1 == short_type_node && t2 == byte_type_node))
11007 resulting_type = short_type_node;
11009 /* If t1 is a constant int and t2 is of type byte, short or char
11010 and t1's value fits in t2, then the resulting type is t2 */
11011 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
11012 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
11013 resulting_type = t2;
11015 /* If t2 is a constant int and t1 is of type byte, short or char
11016 and t2's value fits in t1, then the resulting type is t1 */
11017 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
11018 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
11019 resulting_type = t1;
11021 /* Otherwise, binary numeric promotion is applied and the
11022 resulting type is the promoted type of operand 1 and 2 */
11023 else
11024 resulting_type = binary_numeric_promotion (t2, t2,
11025 &TREE_OPERAND (node, 1),
11026 &TREE_OPERAND (node, 2));
11029 /* Cases of a reference and a null type */
11030 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
11031 resulting_type = t1;
11033 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
11034 resulting_type = t2;
11036 /* Last case: different reference types. If a type can be converted
11037 into the other one by assignment conversion, the latter
11038 determines the type of the expression */
11039 else if ((resulting_type = try_reference_assignconv (t1, op2)))
11040 resulting_type = promote_type (t1);
11042 else if ((resulting_type = try_reference_assignconv (t2, op1)))
11043 resulting_type = promote_type (t2);
11045 /* If we don't have any resulting type, we're in trouble */
11046 if (!resulting_type)
11048 char *t = strdup (lang_printable_name (t1, 0));
11049 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
11050 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11051 "convert `%s' to `%s'", t,
11052 lang_printable_name (t2, 0));
11053 free (t);
11054 error_found = 1;
11057 if (error_found)
11059 TREE_TYPE (node) = error_mark_node;
11060 return error_mark_node;
11063 TREE_TYPE (node) = resulting_type;
11064 TREE_SET_CODE (node, COND_EXPR);
11065 CAN_COMPLETE_NORMALLY (node) = 1;
11066 return node;
11069 /* Try to constant fold NODE.
11070 If NODE is not a constant expression, return NULL_EXPR.
11071 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
11073 static tree
11074 fold_constant_for_init (node, context)
11075 tree node;
11076 tree context;
11078 tree op0, op1, val;
11079 enum tree_code code = TREE_CODE (node);
11081 if (code == INTEGER_CST || code == REAL_CST || code == STRING_CST)
11082 return node;
11083 if (TREE_TYPE (node) != NULL_TREE)
11084 return NULL_TREE;
11086 switch (code)
11088 case MULT_EXPR:
11089 case PLUS_EXPR:
11090 case MINUS_EXPR:
11091 case LSHIFT_EXPR:
11092 case RSHIFT_EXPR:
11093 case URSHIFT_EXPR:
11094 case BIT_AND_EXPR:
11095 case BIT_XOR_EXPR:
11096 case BIT_IOR_EXPR:
11097 case TRUNC_MOD_EXPR:
11098 case RDIV_EXPR:
11099 case TRUTH_ANDIF_EXPR:
11100 case TRUTH_ORIF_EXPR:
11101 case EQ_EXPR:
11102 case NE_EXPR:
11103 case GT_EXPR:
11104 case GE_EXPR:
11105 case LT_EXPR:
11106 case LE_EXPR:
11107 op0 = TREE_OPERAND (node, 0);
11108 op1 = TREE_OPERAND (node, 1);
11109 val = fold_constant_for_init (op0, context);
11110 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11111 return NULL_TREE;
11112 TREE_OPERAND (node, 0) = val;
11113 val = fold_constant_for_init (op1, context);
11114 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11115 return NULL_TREE;
11116 TREE_OPERAND (node, 1) = val;
11117 return patch_binop (node, op0, op1);
11119 case UNARY_PLUS_EXPR:
11120 case NEGATE_EXPR:
11121 case TRUTH_NOT_EXPR:
11122 case BIT_NOT_EXPR:
11123 case CONVERT_EXPR:
11124 op0 = TREE_OPERAND (node, 0);
11125 val = fold_constant_for_init (op0, context);
11126 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11127 return NULL_TREE;
11128 TREE_OPERAND (node, 0) = val;
11129 node = patch_unaryop (node, op0);
11130 break;
11132 case COND_EXPR:
11133 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
11134 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11135 return NULL_TREE;
11136 TREE_OPERAND (node, 0) = val;
11137 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
11138 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11139 return NULL_TREE;
11140 TREE_OPERAND (node, 1) = val;
11141 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
11142 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11143 return NULL_TREE;
11144 TREE_OPERAND (node, 2) = val;
11145 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
11146 : TREE_OPERAND (node, 2);
11148 case VAR_DECL:
11149 if (! FIELD_STATIC (node) || ! FIELD_FINAL (node)
11150 || DECL_INITIAL (node) == NULL_TREE)
11151 return NULL_TREE;
11152 val = DECL_INITIAL (node);
11153 /* Guard against infinite recursion. */
11154 DECL_INITIAL (node) = NULL_TREE;
11155 val = fold_constant_for_init (val, DECL_CONTEXT (node));
11156 DECL_INITIAL (node) = val;
11157 return val;
11159 case EXPR_WITH_FILE_LOCATION:
11160 /* Compare java_complete_tree and resolve_expression_name. */
11161 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11162 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11164 tree name = EXPR_WFL_NODE (node);
11165 tree decl;
11166 if (PRIMARY_P (node))
11167 return NULL_TREE;
11168 else if (! QUALIFIED_P (name))
11170 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
11171 if (! FIELD_STATIC (decl))
11172 return NULL_TREE;
11173 return fold_constant_for_init (decl, decl);
11175 else
11177 #if 0
11178 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
11179 qualify_ambiguous_name (node);
11180 if (resolve_field_access (node, &decl, NULL)
11181 && decl != NULL_TREE)
11182 return fold_constant_for_init (decl, decl);
11183 #endif
11184 return NULL_TREE;
11187 else
11189 op0 = TREE_OPERAND (node, 0);
11190 val = fold_constant_for_init (op0, context);
11191 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11192 return NULL_TREE;
11193 TREE_OPERAND (node, 0) = val;
11194 return val;
11197 default:
11198 return NULL_TREE;