]> gcc.gnu.org Git - gcc.git/blob - gcc/java/parse.y
parse.y: include system.h instead of including standard headers directly with the...
[gcc.git] / gcc / java / parse.y
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)
5
6 This file is part of GNU CC.
7
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.
12
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.
17
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.
22
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. */
26
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).
30
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"
34
35 The following modifications were brought to the original grammar:
36
37 method_body: added the rule '| block SC_TK'
38 static_initializer: added the rule 'static block SC_TK'.
39
40 Note: All the extra rules described above should go away when the
41 empty_statement rule will work.
42
43 statement_nsi: 'nsi' should be read no_short_if.
44
45 Some rules have been modified to support JDK1.1 inner classes
46 definitions and other extensions. */
47
48 %{
49 #include "config.h"
50 #include "system.h"
51 #include <dirent.h>
52 #include "tree.h"
53 #include "rtl.h"
54 #include "obstack.h"
55 #include "toplev.h"
56 #include "flags.h"
57 #include "java-tree.h"
58 #include "jcf.h"
59 #include "lex.h"
60 #include "parse.h"
61 #include "zipfile.h"
62 #include "convert.h"
63 #include "buffer.h"
64
65 #ifndef DIR_SEPARATOR
66 #define DIR_SEPARATOR '/'
67 #endif
68
69 /* Local function prototypes */
70 static char *java_accstring_lookup PROTO ((int));
71 static void classitf_redefinition_error PROTO ((char *,tree, tree, tree));
72 static void variable_redefinition_error PROTO ((tree, tree, tree, int));
73 static void check_modifiers PROTO ((char *, int, int));
74 static tree create_class PROTO ((int, tree, tree, tree));
75 static tree create_interface PROTO ((int, tree, tree));
76 static tree find_field PROTO ((tree, tree));
77 static tree lookup_field_wrapper PROTO ((tree, tree));
78 static int duplicate_declaration_error_p PROTO ((tree, tree, tree));
79 static void register_fields PROTO ((int, tree, tree));
80 static tree parser_qualified_classname PROTO ((tree));
81 static int parser_check_super PROTO ((tree, tree, tree));
82 static int parser_check_super_interface PROTO ((tree, tree, tree));
83 static void check_modifiers_consistency PROTO ((int));
84 static tree lookup_cl PROTO ((tree));
85 static tree lookup_java_method2 PROTO ((tree, tree, int));
86 static tree method_header PROTO ((int, tree, tree, tree));
87 static void fix_method_argument_names PROTO ((tree ,tree));
88 static tree method_declarator PROTO ((tree, tree));
89 static void parse_warning_context VPROTO ((tree cl, char *msg, ...));
90 static void issue_warning_error_from_context PROTO ((tree, char *msg, va_list));
91 static tree parse_jdk1_1_error PROTO ((char *));
92 static void complete_class_report_errors PROTO ((jdep *));
93 static int process_imports PROTO ((void));
94 static void read_import_dir PROTO ((tree));
95 static int find_in_imports_on_demand PROTO ((tree));
96 static int find_in_imports PROTO ((tree));
97 static int check_pkg_class_access PROTO ((tree, tree));
98 static tree resolve_package PROTO ((tree, tree *));
99 static tree lookup_package_type PROTO ((char *, int));
100 static tree resolve_class PROTO ((tree, tree, tree));
101 static tree do_resolve_class PROTO ((tree, tree, tree));
102 static void declare_local_variables PROTO ((int, tree, tree));
103 static void source_start_java_method PROTO ((tree));
104 static void source_end_java_method PROTO ((void));
105 static void expand_start_java_method PROTO ((tree));
106 static tree find_name_in_single_imports PROTO ((tree));
107 static void check_abstract_method_header PROTO ((tree));
108 static tree lookup_java_interface_method2 PROTO ((tree, tree));
109 static tree resolve_expression_name PROTO ((tree, tree *));
110 static tree maybe_create_class_interface_decl PROTO ((tree, tree, tree));
111 static int check_class_interface_creation PROTO ((int, int, tree,
112 tree, tree, tree));
113 static tree patch_method_invocation PROTO ((tree, tree, tree,
114 int *, tree *));
115 static int breakdown_qualified PROTO ((tree *, tree *, tree));
116 static tree resolve_and_layout PROTO ((tree, tree));
117 static tree resolve_no_layout PROTO ((tree, tree));
118 static int invocation_mode PROTO ((tree, int));
119 static tree find_applicable_accessible_methods_list PROTO ((int, tree,
120 tree, tree));
121 static tree find_most_specific_methods_list PROTO ((tree));
122 static int argument_types_convertible PROTO ((tree, tree));
123 static tree patch_invoke PROTO ((tree, tree, tree));
124 static tree lookup_method_invoke PROTO ((int, tree, tree, tree, tree));
125 static tree register_incomplete_type PROTO ((int, tree, tree, tree));
126 static tree obtain_incomplete_type PROTO ((tree));
127 static tree java_complete_lhs PROTO ((tree));
128 static tree java_complete_tree PROTO ((tree));
129 static void java_complete_expand_method PROTO ((tree));
130 static int unresolved_type_p PROTO ((tree, tree *));
131 static void create_jdep_list PROTO ((struct parser_ctxt *));
132 static tree build_expr_block PROTO ((tree, tree));
133 static tree enter_block PROTO ((void));
134 static tree enter_a_block PROTO ((tree));
135 static tree exit_block PROTO ((void));
136 static tree lookup_name_in_blocks PROTO ((tree));
137 static void maybe_absorb_scoping_blocks PROTO ((void));
138 static tree build_method_invocation PROTO ((tree, tree));
139 static tree build_new_invocation PROTO ((tree, tree));
140 static tree build_assignment PROTO ((int, int, tree, tree));
141 static tree build_binop PROTO ((enum tree_code, int, tree, tree));
142 static int check_final_assignment PROTO ((tree ,tree));
143 static tree patch_assignment PROTO ((tree, tree, tree ));
144 static tree patch_binop PROTO ((tree, tree, tree));
145 static tree build_unaryop PROTO ((int, int, tree));
146 static tree build_incdec PROTO ((int, int, tree, int));
147 static tree patch_unaryop PROTO ((tree, tree));
148 static tree build_cast PROTO ((int, tree, tree));
149 static tree build_null_of_type PROTO ((tree));
150 static tree patch_cast PROTO ((tree, tree));
151 static int valid_ref_assignconv_cast_p PROTO ((tree, tree, int));
152 static int valid_builtin_assignconv_identity_widening_p PROTO ((tree, tree));
153 static int valid_cast_to_p PROTO ((tree, tree));
154 static int valid_method_invocation_conversion_p PROTO ((tree, tree));
155 static tree try_builtin_assignconv PROTO ((tree, tree, tree));
156 static tree try_reference_assignconv PROTO ((tree, tree));
157 static tree build_unresolved_array_type PROTO ((tree));
158 static tree build_array_from_name PROTO ((tree, tree, tree, tree *));
159 static tree build_array_ref PROTO ((int, tree, tree));
160 static tree patch_array_ref PROTO ((tree));
161 static tree make_qualified_name PROTO ((tree, tree, int));
162 static tree merge_qualified_name PROTO ((tree, tree));
163 static tree make_qualified_primary PROTO ((tree, tree, int));
164 static int resolve_qualified_expression_name PROTO ((tree, tree *,
165 tree *, tree *));
166 static void qualify_ambiguous_name PROTO ((tree));
167 static void maybe_generate_clinit PROTO ((void));
168 static tree resolve_field_access PROTO ((tree, tree *, tree *));
169 static tree build_newarray_node PROTO ((tree, tree, int));
170 static tree patch_newarray PROTO ((tree));
171 static tree resolve_type_during_patch PROTO ((tree));
172 static tree build_this PROTO ((int));
173 static tree build_return PROTO ((int, tree));
174 static tree patch_return PROTO ((tree));
175 static tree maybe_access_field PROTO ((tree, tree, tree));
176 static int complete_function_arguments PROTO ((tree));
177 static int check_for_static_method_reference PROTO ((tree, tree, tree, tree, tree));
178 static int not_accessible_p PROTO ((tree, tree, int));
179 static void check_deprecation PROTO ((tree, tree));
180 static int class_in_current_package PROTO ((tree));
181 static tree build_if_else_statement PROTO ((int, tree, tree, tree));
182 static tree patch_if_else_statement PROTO ((tree));
183 static tree add_stmt_to_compound PROTO ((tree, tree, tree));
184 static tree add_stmt_to_block PROTO ((tree, tree, tree));
185 static tree patch_exit_expr PROTO ((tree));
186 static tree build_labeled_block PROTO ((int, tree));
187 static tree generate_labeled_block PROTO (());
188 static tree complete_labeled_statement PROTO ((tree, tree));
189 static tree build_bc_statement PROTO ((int, int, tree));
190 static tree patch_bc_statement PROTO ((tree));
191 static tree patch_loop_statement PROTO ((tree));
192 static tree build_new_loop PROTO ((tree));
193 static tree build_loop_body PROTO ((int, tree, int));
194 static tree complete_loop_body PROTO ((int, tree, tree, int));
195 static tree build_debugable_stmt PROTO ((int, tree));
196 static tree complete_for_loop PROTO ((int, tree, tree, tree));
197 static tree patch_switch_statement PROTO ((tree));
198 static tree string_constant_concatenation PROTO ((tree, tree));
199 static tree build_string_concatenation PROTO ((tree, tree));
200 static tree patch_string_cst PROTO ((tree));
201 static tree patch_string PROTO ((tree));
202 static tree build_jump_to_finally PROTO ((tree, tree, tree, tree));
203 static tree build_try_statement PROTO ((int, tree, tree, tree));
204 static tree patch_try_statement PROTO ((tree));
205 static tree patch_synchronized_statement PROTO ((tree, tree));
206 static tree patch_throw_statement PROTO ((tree, tree));
207 static void check_thrown_exceptions PROTO ((int, tree));
208 static int check_thrown_exceptions_do PROTO ((tree));
209 static void purge_unchecked_exceptions PROTO ((tree));
210 static void check_throws_clauses PROTO ((tree, tree, tree));
211 static void complete_method_declaration PROTO ((tree));
212 static tree build_super_invocation PROTO (());
213 static int verify_constructor_circularity PROTO ((tree, tree));
214 static char *constructor_circularity_msg PROTO ((tree, tree));
215 static tree build_this_super_qualified_invocation PROTO ((int, tree, tree,
216 int, int));
217 static char *get_printable_method_name PROTO ((tree));
218 static tree patch_conditional_expr PROTO ((tree, tree, tree));
219 static void maybe_generate_finit PROTO (());
220 static void fix_constructors PROTO ((tree));
221 static int verify_constructor_super PROTO (());
222 static tree create_artificial_method PROTO ((tree, int, tree, tree, tree));
223 static void start_artificial_method_body PROTO ((tree));
224 static void end_artificial_method_body PROTO ((tree));
225 static int check_method_redefinition PROTO ((tree, tree));
226 static int reset_method_name PROTO ((tree));
227 static void java_check_regular_methods PROTO ((tree));
228 static void java_check_abstract_methods PROTO ((tree));
229 static tree maybe_build_primttype_type_ref PROTO ((tree, tree));
230 static void unreachable_stmt_error PROTO ((tree));
231 static tree find_expr_with_wfl PROTO ((tree));
232 static void missing_return_error PROTO ((tree));
233 static tree build_new_array_init PROTO ((int, tree));
234 static tree patch_new_array_init PROTO ((tree, tree));
235 static tree maybe_build_array_element_wfl PROTO ((tree));
236 static int array_constructor_check_entry PROTO ((tree, tree));
237 static char *purify_type_name PROTO ((char *));
238 static tree patch_initialized_static_field PROTO ((tree));
239 static tree fold_constant_for_init PROTO ((tree, tree));
240
241 /* Number of error found so far. */
242 int java_error_count;
243 /* Number of warning found so far. */
244 int java_warning_count;
245
246 /* The current parser context */
247 static struct parser_ctxt *ctxp;
248
249 /* List of things that were anlyzed for which code will be generated */
250 static struct parser_ctxt *ctxp_for_generation = NULL;
251
252 /* binop_lookup maps token to tree_code. It is used where binary
253 operations are involved and required by the parser. RDIV_EXPR
254 covers both integral/floating point division. The code is changed
255 once the type of both operator is worked out. */
256
257 static enum tree_code binop_lookup[19] =
258 {
259 PLUS_EXPR, MINUS_EXPR, MULT_EXPR, RDIV_EXPR, TRUNC_MOD_EXPR,
260 LSHIFT_EXPR, RSHIFT_EXPR, URSHIFT_EXPR,
261 BIT_AND_EXPR, BIT_XOR_EXPR, BIT_IOR_EXPR,
262 TRUTH_ANDIF_EXPR, TRUTH_ORIF_EXPR,
263 EQ_EXPR, NE_EXPR, GT_EXPR, GE_EXPR, LT_EXPR, LE_EXPR,
264 };
265 #define BINOP_LOOKUP(VALUE) \
266 binop_lookup [((VALUE) - PLUS_TK)% \
267 (sizeof (binop_lookup) / sizeof (binop_lookup[0]))]
268
269 /* Fake WFL used to report error message. It is initialized once if
270 needed and reused with it's location information is overriden. */
271 tree wfl_operator = NULL_TREE;
272
273 /* The "$L" identifier we use to create labels. */
274 static tree label_id = NULL_TREE;
275
276 /* The "StringBuffer" identifier used for the String `+' operator. */
277 static tree wfl_string_buffer = NULL_TREE;
278
279 /* The "append" identifier used for String `+' operator. */
280 static tree wfl_append = NULL_TREE;
281
282 /* The "toString" identifier used for String `+' operator. */
283 static tree wfl_to_string = NULL_TREE;
284 %}
285
286 %union {
287 tree node;
288 int sub_token;
289 struct {
290 int token;
291 int location;
292 } operator;
293 int value;
294 }
295
296 %pure_parser
297
298 /* Things defined here have to match the order of what's in the
299 binop_lookup table. */
300
301 %token PLUS_TK MINUS_TK MULT_TK DIV_TK REM_TK
302 %token LS_TK SRS_TK ZRS_TK
303 %token AND_TK XOR_TK OR_TK
304 %token BOOL_AND_TK BOOL_OR_TK
305 %token EQ_TK NEQ_TK GT_TK GTE_TK LT_TK LTE_TK
306
307 /* This maps to the same binop_lookup entry than the token above */
308
309 %token PLUS_ASSIGN_TK MINUS_ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
310 %token REM_ASSIGN_TK
311 %token LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
312 %token AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
313
314
315 /* Modifier TOKEN have to be kept in this order. Don't scramble it */
316
317 %token PUBLIC_TK PRIVATE_TK PROTECTED_TK
318 %token STATIC_TK FINAL_TK SYNCHRONIZED_TK
319 %token VOLATILE_TK TRANSIENT_TK NATIVE_TK
320 %token PAD_TK ABSTRACT_TK MODIFIER_TK
321
322 /* Keep those two in order, too */
323 %token DECR_TK INCR_TK
324
325 /* From now one, things can be in any order */
326
327 %token DEFAULT_TK IF_TK THROW_TK
328 %token BOOLEAN_TK DO_TK IMPLEMENTS_TK
329 %token THROWS_TK BREAK_TK IMPORT_TK
330 %token ELSE_TK INSTANCEOF_TK RETURN_TK
331 %token VOID_TK CATCH_TK INTERFACE_TK
332 %token CASE_TK EXTENDS_TK FINALLY_TK
333 %token SUPER_TK WHILE_TK CLASS_TK
334 %token SWITCH_TK CONST_TK TRY_TK
335 %token FOR_TK NEW_TK CONTINUE_TK
336 %token GOTO_TK PACKAGE_TK THIS_TK
337
338 %token BYTE_TK SHORT_TK INT_TK LONG_TK
339 %token CHAR_TK INTEGRAL_TK
340
341 %token FLOAT_TK DOUBLE_TK FP_TK
342
343 %token ID_TK
344
345 %token REL_QM_TK REL_CL_TK NOT_TK NEG_TK
346
347 %token ASSIGN_ANY_TK ASSIGN_TK
348 %token OP_TK CP_TK OCB_TK CCB_TK OSB_TK CSB_TK SC_TK C_TK DOT_TK
349
350 %token STRING_LIT_TK CHAR_LIT_TK INT_LIT_TK FP_LIT_TK
351 %token TRUE_TK FALSE_TK BOOL_LIT_TK NULL_TK
352
353 %type <value> modifiers MODIFIER_TK
354
355 %type <node> super ID_TK identifier
356 %type <node> name simple_name qualified_name
357 %type <node> class_declaration type_declaration compilation_unit
358 field_declaration method_declaration extends_interfaces
359 interfaces interface_type_list
360 interface_declaration class_member_declaration
361 import_declarations package_declaration
362 type_declarations interface_body
363 interface_member_declaration constant_declaration
364 interface_member_declarations interface_type
365 abstract_method_declaration interface_type_list
366 %type <node> class_body_declaration class_member_declaration
367 static_initializer constructor_declaration block
368 %type <node> class_body_declarations constructor_header
369 %type <node> class_or_interface_type class_type class_type_list
370 constructor_declarator explicit_constructor_invocation
371 %type <node> dim_expr dim_exprs this_or_super throws
372
373 %type <node> variable_declarator_id variable_declarator
374 variable_declarators variable_initializer
375 variable_initializers constructor_body
376 array_initializer
377
378 %type <node> class_body block_end
379 %type <node> statement statement_without_trailing_substatement
380 labeled_statement if_then_statement label_decl
381 if_then_else_statement while_statement for_statement
382 statement_nsi labeled_statement_nsi do_statement
383 if_then_else_statement_nsi while_statement_nsi
384 for_statement_nsi statement_expression_list for_init
385 for_update statement_expression expression_statement
386 primary_no_new_array expression primary
387 array_creation_expression array_type
388 class_instance_creation_expression field_access
389 method_invocation array_access something_dot_new
390 argument_list postfix_expression while_expression
391 post_increment_expression post_decrement_expression
392 unary_expression_not_plus_minus unary_expression
393 pre_increment_expression pre_decrement_expression
394 unary_expression_not_plus_minus cast_expression
395 multiplicative_expression additive_expression
396 shift_expression relational_expression
397 equality_expression and_expression
398 exclusive_or_expression inclusive_or_expression
399 conditional_and_expression conditional_or_expression
400 conditional_expression assignment_expression
401 left_hand_side assignment for_header for_begin
402 constant_expression do_statement_begin empty_statement
403 switch_statement synchronized_statement throw_statement
404 try_statement switch_expression switch_block
405 catches catch_clause catch_clause_parameter finally
406 %type <node> return_statement break_statement continue_statement
407
408 %type <operator> ASSIGN_TK MULT_ASSIGN_TK DIV_ASSIGN_TK
409 %type <operator> REM_ASSIGN_TK PLUS_ASSIGN_TK MINUS_ASSIGN_TK
410 %type <operator> LS_ASSIGN_TK SRS_ASSIGN_TK ZRS_ASSIGN_TK
411 %type <operator> AND_ASSIGN_TK XOR_ASSIGN_TK OR_ASSIGN_TK
412 %type <operator> ASSIGN_ANY_TK assignment_operator
413 %token <operator> EQ_TK GTE_TK ZRS_TK SRS_TK GT_TK LTE_TK LS_TK
414 %token <operator> BOOL_AND_TK AND_TK BOOL_OR_TK OR_TK INCR_TK PLUS_TK
415 %token <operator> DECR_TK MINUS_TK MULT_TK DIV_TK XOR_TK REM_TK NEQ_TK
416 %token <operator> NEG_TK REL_QM_TK REL_CL_TK NOT_TK LT_TK OCB_TK
417 %token <operator> OP_TK OSB_TK DOT_TK THROW_TK INSTANCEOF_TK
418 %type <operator> THIS_TK SUPER_TK RETURN_TK BREAK_TK CONTINUE_TK
419 %type <operator> CASE_TK DEFAULT_TK TRY_TK CATCH_TK SYNCHRONIZED_TK
420
421 %type <node> method_body
422
423 %type <node> literal INT_LIT_TK FP_LIT_TK BOOL_LIT_TK CHAR_LIT_TK
424 STRING_LIT_TK NULL_TK VOID_TK
425
426 %type <node> IF_TK WHILE_TK FOR_TK
427
428 %type <node> formal_parameter_list formal_parameter
429 method_declarator method_header
430
431 %type <node> primitive_type reference_type type
432 BOOLEAN_TK INTEGRAL_TK FP_TK
433
434 %%
435 /* 19.2 Production from 2.3: The Syntactic Grammar */
436 goal:
437 compilation_unit
438 {}
439 ;
440
441 /* 19.3 Productions from 3: Lexical structure */
442 literal:
443 INT_LIT_TK
444 | FP_LIT_TK
445 | BOOL_LIT_TK
446 | CHAR_LIT_TK
447 | STRING_LIT_TK
448 | NULL_TK
449 ;
450
451 /* 19.4 Productions from 4: Types, Values and Variables */
452 type:
453 primitive_type
454 | reference_type
455 ;
456
457 primitive_type:
458 INTEGRAL_TK
459 | FP_TK
460 | BOOLEAN_TK
461 ;
462
463 reference_type:
464 class_or_interface_type
465 | array_type
466 ;
467
468 class_or_interface_type:
469 name
470 ;
471
472 class_type:
473 class_or_interface_type /* Default rule */
474 ;
475
476 interface_type:
477 class_or_interface_type
478 ;
479
480 array_type:
481 primitive_type OSB_TK CSB_TK
482 {
483 $$ = build_java_array_type ($1, -1);
484 CLASS_LOADED_P ($$) = 1;
485 }
486 | name OSB_TK CSB_TK
487 { $$ = build_unresolved_array_type ($1); }
488 | array_type OSB_TK CSB_TK
489 { $$ = build_unresolved_array_type ($1); }
490 | primitive_type OSB_TK error
491 {RULE ("']' expected"); RECOVER;}
492 | array_type OSB_TK error
493 {RULE ("']' expected"); RECOVER;}
494 ;
495
496 /* 19.5 Productions from 6: Names */
497 name:
498 simple_name /* Default rule */
499 | qualified_name /* Default rule */
500 ;
501
502 simple_name:
503 identifier /* Default rule */
504 ;
505
506 qualified_name:
507 name DOT_TK identifier
508 { $$ = make_qualified_name ($1, $3, $2.location); }
509 ;
510
511 identifier:
512 ID_TK
513 ;
514
515 /* 19.6: Production from 7: Packages */
516 compilation_unit:
517 {$$ = NULL;}
518 | package_declaration
519 | import_declarations
520 | type_declarations
521 | package_declaration import_declarations
522 | package_declaration type_declarations
523 | import_declarations type_declarations
524 | package_declaration import_declarations type_declarations
525 ;
526
527 import_declarations:
528 import_declaration
529 {
530 $$ = NULL;
531 }
532 | import_declarations import_declaration
533 {
534 $$ = NULL;
535 }
536 ;
537
538 type_declarations:
539 type_declaration
540 | type_declarations type_declaration
541 ;
542
543 package_declaration:
544 PACKAGE_TK name SC_TK
545 { ctxp->package = EXPR_WFL_NODE ($2); }
546 | PACKAGE_TK error
547 {yyerror ("Missing name"); RECOVER;}
548 | PACKAGE_TK name error
549 {yyerror ("';' expected"); RECOVER;}
550 ;
551
552 import_declaration:
553 single_type_import_declaration
554 | type_import_on_demand_declaration
555 ;
556
557 single_type_import_declaration:
558 IMPORT_TK name SC_TK
559 {
560 tree name = EXPR_WFL_NODE ($2), node, last_name;
561 int i = IDENTIFIER_LENGTH (name)-1;
562 char *last = &IDENTIFIER_POINTER (name)[i];
563 while (last != IDENTIFIER_POINTER (name))
564 {
565 if (last [0] == '.')
566 break;
567 last--;
568 }
569 last_name = get_identifier (++last);
570 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (last_name))
571 {
572 tree err = find_name_in_single_imports (last_name);
573 if (err && err != name)
574 parse_error_context
575 ($2, "Ambiguous class: `%s' and `%s'",
576 IDENTIFIER_POINTER (name),
577 IDENTIFIER_POINTER (err));
578 else
579 REGISTER_IMPORT ($2, last_name)
580 }
581 else
582 REGISTER_IMPORT ($2, last_name);
583 }
584 | IMPORT_TK error
585 {yyerror ("Missing name"); RECOVER;}
586 | IMPORT_TK name error
587 {yyerror ("';' expected"); RECOVER;}
588 ;
589
590 type_import_on_demand_declaration:
591 IMPORT_TK name DOT_TK MULT_TK SC_TK
592 {
593 tree name = EXPR_WFL_NODE ($2);
594 tree node = build_tree_list ($2, NULL_TREE);
595 read_import_dir ($2);
596 TREE_CHAIN (node) = ctxp->import_demand_list;
597 ctxp->import_demand_list = node;
598 }
599 | IMPORT_TK name DOT_TK error
600 {yyerror ("'*' expected"); RECOVER;}
601 | IMPORT_TK name DOT_TK MULT_TK error
602 {yyerror ("';' expected"); RECOVER;}
603 ;
604
605 type_declaration:
606 class_declaration
607 {
608 maybe_generate_finit ();
609 maybe_generate_clinit ();
610 $$ = $1;
611 }
612 | interface_declaration
613 | SC_TK
614 { $$ = NULL; }
615 | error
616 {
617 YYERROR_NOW;
618 yyerror ("Class or interface declaration expected");
619 }
620 ;
621
622 /* 19.7 Shortened from the original:
623 modifiers: modifier | modifiers modifier
624 modifier: any of public... */
625 modifiers:
626 MODIFIER_TK
627 {
628 $$ = (1 << $1);
629 }
630 | modifiers MODIFIER_TK
631 {
632 int acc = (1 << $2);
633 if ($$ & acc)
634 parse_error_context
635 (ctxp->modifier_ctx [$2], "Modifier `%s' declared twice",
636 java_accstring_lookup (acc));
637 else
638 {
639 $$ |= acc;
640 }
641 }
642 ;
643
644 /* 19.8.1 Production from $8.1: Class Declaration */
645 class_declaration:
646 modifiers CLASS_TK identifier super interfaces
647 { create_class ($1, $3, $4, $5); }
648 class_body
649 {
650 $$ = $7;
651 }
652 | CLASS_TK identifier super interfaces
653 { create_class (0, $2, $3, $4); }
654 class_body
655 {
656 $$ = $6;
657 }
658 | modifiers CLASS_TK error
659 {yyerror ("Missing class name"); RECOVER;}
660 | CLASS_TK error
661 {yyerror ("Missing class name"); RECOVER;}
662 | CLASS_TK identifier error
663 {if (!ctxp->class_err) yyerror ("'{' expected"); DRECOVER(class1);}
664 | modifiers CLASS_TK identifier error
665 {if (!ctxp->class_err) yyerror ("'{' expected"); RECOVER;}
666 ;
667
668 super:
669 { $$ = NULL; }
670 | EXTENDS_TK class_type
671 { $$ = $2; }
672 | EXTENDS_TK class_type error
673 {yyerror ("'{' expected"); ctxp->class_err=1;}
674 | EXTENDS_TK error
675 {yyerror ("Missing super class name"); ctxp->class_err=1;}
676 ;
677
678 interfaces:
679 { $$ = NULL_TREE; }
680 | IMPLEMENTS_TK interface_type_list
681 { $$ = $2; }
682 | IMPLEMENTS_TK error
683 {
684 ctxp->class_err=1;
685 yyerror ("Missing interface name");
686 }
687 ;
688
689 interface_type_list:
690 interface_type
691 {
692 ctxp->interface_number = 1;
693 $$ = build_tree_list ($1, NULL_TREE);
694 }
695 | interface_type_list C_TK interface_type
696 {
697 ctxp->interface_number++;
698 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
699 }
700 | interface_type_list C_TK error
701 {yyerror ("Missing interface name"); RECOVER;}
702 ;
703
704 class_body:
705 OCB_TK CCB_TK
706 { $$ = ctxp->current_parsed_class; }
707 | OCB_TK class_body_declarations CCB_TK
708 { $$ = ctxp->current_parsed_class; }
709 ;
710
711 class_body_declarations:
712 class_body_declaration
713 | class_body_declarations class_body_declaration
714 ;
715
716 class_body_declaration:
717 class_member_declaration
718 | static_initializer
719 | constructor_declaration
720 | block /* Added, JDK1.1, instance initializer */
721 { $$ = parse_jdk1_1_error ("instance initializer"); }
722 ;
723
724 class_member_declaration:
725 field_declaration
726 | method_declaration
727 | class_declaration /* Added, JDK1.1 inner classes */
728 { $$ = parse_jdk1_1_error ("inner classe declaration"); }
729 | interface_declaration /* Added, JDK1.1 inner classes */
730 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
731 ;
732
733 /* 19.8.2 Productions from 8.3: Field Declarations */
734 field_declaration:
735 type variable_declarators SC_TK
736 { register_fields (0, $1, $2); }
737 | modifiers type variable_declarators SC_TK
738 {
739 check_modifiers
740 ("Illegal modifier `%s' for field declaration",
741 $1, FIELD_MODIFIERS);
742 check_modifiers_consistency ($1);
743 register_fields ($1, $2, $3);
744 }
745 ;
746
747 variable_declarators:
748 /* Should we use build_decl_list () instead ? FIXME */
749 variable_declarator /* Default rule */
750 | variable_declarators C_TK variable_declarator
751 { $$ = chainon ($1, $3); }
752 | variable_declarators C_TK error
753 {yyerror ("Missing term"); RECOVER;}
754 ;
755
756 variable_declarator:
757 variable_declarator_id
758 { $$ = build_tree_list ($1, NULL_TREE); }
759 | variable_declarator_id ASSIGN_TK variable_initializer
760 {
761 if (java_error_count)
762 $3 = NULL_TREE;
763 $$ = build_tree_list
764 ($1, build_assignment ($2.token, $2.location, $1, $3));
765 }
766 | variable_declarator_id ASSIGN_TK error
767 {
768 yyerror ("Missing variable initializer");
769 $$ = build_tree_list ($1, NULL_TREE);
770 RECOVER;
771 }
772 | variable_declarator_id ASSIGN_TK variable_initializer error
773 {
774 yyerror ("';' expected");
775 $$ = build_tree_list ($1, NULL_TREE);
776 RECOVER;
777 }
778 ;
779
780 variable_declarator_id:
781 identifier
782 | variable_declarator_id OSB_TK CSB_TK
783 { $$ = build_unresolved_array_type ($1); }
784 | identifier error
785 {yyerror ("Invalid declaration"); DRECOVER(vdi);}
786 | variable_declarator_id OSB_TK error
787 {yyerror ("']' expected"); DRECOVER(vdi);}
788 | variable_declarator_id CSB_TK error
789 {yyerror ("Unbalanced ']'"); DRECOVER(vdi);}
790 ;
791
792 variable_initializer:
793 expression
794 | array_initializer
795 ;
796
797 /* 19.8.3 Productions from 8.4: Method Declarations */
798 method_declaration:
799 method_header
800 {
801 current_function_decl = $1;
802 source_start_java_method (current_function_decl);
803 }
804 method_body
805 { complete_method_declaration ($3); }
806 | method_header error
807 {YYNOT_TWICE yyerror ("'{' expected"); RECOVER;}
808 ;
809
810 method_header:
811 type method_declarator throws
812 { $$ = method_header (0, $1, $2, $3); }
813 | VOID_TK method_declarator throws
814 { $$ = method_header (0, void_type_node, $2, $3); }
815 | modifiers type method_declarator throws
816 { $$ = method_header ($1, $2, $3, $4); }
817 | modifiers VOID_TK method_declarator throws
818 { $$ = method_header ($1, void_type_node, $3, $4); }
819 | type error
820 {RECOVER;}
821 | modifiers type error
822 {RECOVER;}
823 | VOID_TK error
824 {yyerror ("Identifier expected"); RECOVER;}
825 | modifiers VOID_TK error
826 {yyerror ("Identifier expected"); RECOVER;}
827 | modifiers error
828 {
829 yyerror ("Invalid method declaration, return type required");
830 RECOVER;
831 }
832 ;
833
834 method_declarator:
835 identifier OP_TK CP_TK
836 { $$ = method_declarator ($1, NULL_TREE); }
837 | identifier OP_TK formal_parameter_list CP_TK
838 { $$ = method_declarator ($1, $3); }
839 | method_declarator OSB_TK CSB_TK
840 {
841 EXPR_WFL_LINECOL (wfl_operator) = $2.location;
842 TREE_PURPOSE ($1) =
843 build_unresolved_array_type (TREE_PURPOSE ($1));
844 parse_warning_context
845 (wfl_operator,
846 "Discouraged form of returned type specification");
847 }
848 | identifier OP_TK error
849 {yyerror ("')' expected"); DRECOVER(method_declarator);}
850 | method_declarator OSB_TK error
851 {yyerror ("']' expected"); RECOVER;}
852 ;
853
854 formal_parameter_list:
855 formal_parameter
856 {
857 ctxp->formal_parameter_number = 1;
858 }
859 | formal_parameter_list C_TK formal_parameter
860 {
861 ctxp->formal_parameter_number += 1;
862 $$ = chainon ($1, $3);
863 }
864 | formal_parameter_list C_TK error
865 {yyerror ("Missing formal parameter term"); RECOVER;}
866 ;
867
868 formal_parameter:
869 type variable_declarator_id
870 {
871 $$ = build_tree_list ($2, $1);
872 }
873 | modifiers type variable_declarator_id /* Added, JDK1.1 final parms */
874 { $$ = parse_jdk1_1_error ("final parameters"); }
875 | type error
876 {yyerror ("Missing identifier"); RECOVER;}
877 | modifiers type error
878 {
879 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
880 yyerror ("Missing identifier"); RECOVER;
881 }
882 ;
883
884 throws:
885 { $$ = NULL_TREE; }
886 | THROWS_TK class_type_list
887 { $$ = $2; }
888 | THROWS_TK error
889 {yyerror ("Missing class type term"); RECOVER;}
890 ;
891
892 class_type_list:
893 class_type
894 { $$ = build_tree_list ($1, $1); }
895 | class_type_list C_TK class_type
896 { $$ = tree_cons ($3, $3, $1); }
897 | class_type_list C_TK error
898 {yyerror ("Missing class type term"); RECOVER;}
899 ;
900
901 method_body:
902 block
903 | block SC_TK
904 | SC_TK
905 { $$ = NULL_TREE; } /* Probably not the right thing to do. */
906 ;
907
908 /* 19.8.4 Productions from 8.5: Static Initializers */
909 static_initializer:
910 static block
911 {
912 RULE ("STATIC_INITIALIZER");
913 }
914 | static block SC_TK /* Shouldn't be here. FIXME */
915 {
916 RULE ("STATIC_INITIALIZER");
917 }
918 ;
919
920 static: /* Test lval.sub_token here */
921 MODIFIER_TK
922 {
923 SOURCE_FRONTEND_DEBUG (("Modifiers: %d", $1));
924 }
925 ;
926
927 /* 19.8.5 Productions from 8.6: Constructor Declarations */
928 constructor_declaration:
929 constructor_header
930 {
931 current_function_decl = $1;
932 source_start_java_method (current_function_decl);
933 }
934 constructor_body
935 { complete_method_declaration ($3); }
936 ;
937
938 constructor_header:
939 constructor_declarator throws
940 { $$ = method_header (0, NULL_TREE, $1, $2); }
941 | modifiers constructor_declarator throws
942 { $$ = method_header ($1, NULL_TREE, $2, $3); }
943 ;
944
945 constructor_declarator:
946 simple_name OP_TK CP_TK
947 { $$ = method_declarator ($1, NULL_TREE); }
948 | simple_name OP_TK formal_parameter_list CP_TK
949 { $$ = method_declarator ($1, $3); }
950 ;
951
952 constructor_body:
953 /* Unlike regular method, we always need a complete (empty)
954 body so we can safely perform all the required code
955 addition (super invocation and field initialization) */
956 block_begin block_end
957 {
958 BLOCK_EXPR_BODY ($2) = empty_stmt_node;
959 $$ = $2;
960 }
961 | block_begin explicit_constructor_invocation block_end
962 { $$ = $3; }
963 | block_begin block_statements block_end
964 { $$ = $3; }
965 | block_begin explicit_constructor_invocation block_statements block_end
966 { $$ = $4; }
967 ;
968
969 /* Error recovery for that rule moved down expression_statement: rule. */
970 explicit_constructor_invocation:
971 this_or_super OP_TK CP_TK SC_TK
972 {
973 $$ = build_method_invocation ($1, NULL_TREE);
974 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
975 $$ = java_method_add_stmt (current_function_decl, $$);
976 }
977 | this_or_super OP_TK argument_list CP_TK SC_TK
978 {
979 $$ = build_method_invocation ($1, $3);
980 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $$);
981 $$ = java_method_add_stmt (current_function_decl, $$);
982 }
983 /* Added, JDK1.1 inner classes. Modified because the rule
984 'primary' couldn't work. */
985 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK SC_TK
986 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
987 | name DOT_TK SUPER_TK OP_TK CP_TK SC_TK
988 {$$ = parse_jdk1_1_error ("explicit constructor invocation"); }
989 ;
990
991 this_or_super: /* Added, simplifies error diagnostics */
992 THIS_TK
993 {
994 tree wfl = build_wfl_node (this_identifier_node,
995 input_filename, 0, 0);
996 EXPR_WFL_LINECOL (wfl) = $1.location;
997 $$ = wfl;
998 }
999 | SUPER_TK
1000 {
1001 tree wfl = build_wfl_node (super_identifier_node,
1002 input_filename, 0, 0);
1003 EXPR_WFL_LINECOL (wfl) = $1.location;
1004 $$ = wfl;
1005 }
1006 ;
1007
1008 /* 19.9 Productions from 9: Interfaces */
1009 /* 19.9.1 Productions from 9.1: Interfaces Declarations */
1010 interface_declaration:
1011 INTERFACE_TK identifier
1012 { create_interface (0, $2, NULL_TREE); }
1013 interface_body
1014 {
1015 $$ = $4;
1016 }
1017 | modifiers INTERFACE_TK identifier
1018 { create_interface ($1, $3, NULL_TREE); }
1019 interface_body
1020 {
1021 $$ = $5;
1022 }
1023 | INTERFACE_TK identifier extends_interfaces
1024 { create_interface (0, $2, $3); }
1025 interface_body
1026 {
1027 $$ = $5;
1028 }
1029 | modifiers INTERFACE_TK identifier extends_interfaces
1030 { create_interface ($1, $3, $4); }
1031 interface_body
1032 {
1033 $$ = $6;
1034 }
1035 | INTERFACE_TK identifier error
1036 {yyerror ("(here)'{' expected"); RECOVER;}
1037 | modifiers INTERFACE_TK identifier error
1038 {yyerror ("(there)'{' expected"); RECOVER;}
1039 ;
1040
1041 extends_interfaces:
1042 EXTENDS_TK interface_type
1043 {
1044 ctxp->interface_number = 1;
1045 $$ = build_tree_list ($2, NULL_TREE);
1046 }
1047 | extends_interfaces C_TK interface_type
1048 {
1049 ctxp->interface_number++;
1050 $$ = chainon ($1, build_tree_list ($3, NULL_TREE));
1051 }
1052 | EXTENDS_TK error
1053 {yyerror ("Invalid interface type"); RECOVER;}
1054 | extends_interfaces C_TK error
1055 {yyerror ("Missing term"); RECOVER;}
1056 ;
1057
1058 interface_body:
1059 OCB_TK CCB_TK
1060 { $$ = NULL_TREE; }
1061 | OCB_TK interface_member_declarations CCB_TK
1062 { $$ = NULL_TREE; }
1063 ;
1064
1065 interface_member_declarations:
1066 interface_member_declaration
1067 | interface_member_declarations interface_member_declaration
1068 ;
1069
1070 interface_member_declaration:
1071 constant_declaration
1072 | abstract_method_declaration
1073 | class_declaration /* Added, JDK1.1 inner classes */
1074 { $$ = parse_jdk1_1_error ("inner class declaration"); }
1075 | interface_declaration /* Added, JDK1.1 inner classes */
1076 { $$ = parse_jdk1_1_error ("inner interface declaration"); }
1077 ;
1078
1079 constant_declaration:
1080 field_declaration
1081 ;
1082
1083 abstract_method_declaration:
1084 method_header SC_TK
1085 {
1086 check_abstract_method_header ($1);
1087 current_function_decl = NULL_TREE; /* FIXME ? */
1088 }
1089 | method_header error
1090 {yyerror ("';' expected"); RECOVER;}
1091 ;
1092
1093 /* 19.10 Productions from 10: Arrays */
1094 array_initializer:
1095 OCB_TK CCB_TK
1096 { $$ = build_new_array_init ($1.location, NULL_TREE); }
1097 | OCB_TK variable_initializers CCB_TK
1098 { $$ = build_new_array_init ($1.location, $2); }
1099 | OCB_TK variable_initializers C_TK CCB_TK
1100 { $$ = build_new_array_init ($1.location, $2); }
1101 ;
1102
1103 variable_initializers:
1104 variable_initializer
1105 {
1106 $$ = tree_cons (maybe_build_array_element_wfl ($1),
1107 $1, NULL_TREE);
1108 }
1109 | variable_initializers C_TK variable_initializer
1110 {
1111 $$ = tree_cons (maybe_build_array_element_wfl ($3), $3, $1);
1112 }
1113 | variable_initializers C_TK error
1114 {yyerror ("Missing term"); RECOVER;}
1115 ;
1116
1117 /* 19.11 Production from 14: Blocks and Statements */
1118 block:
1119 OCB_TK CCB_TK
1120 { $$ = empty_stmt_node; }
1121 | block_begin block_statements block_end
1122 { $$ = $3; }
1123 ;
1124
1125 block_begin:
1126 OCB_TK
1127 { enter_block (); }
1128 ;
1129
1130 block_end:
1131 CCB_TK
1132 {
1133 maybe_absorb_scoping_blocks ();
1134 $$ = exit_block ();
1135 }
1136 ;
1137
1138 block_statements:
1139 block_statement
1140 | block_statements block_statement
1141 ;
1142
1143 block_statement:
1144 local_variable_declaration_statement
1145 | statement
1146 { java_method_add_stmt (current_function_decl, $1); }
1147 | class_declaration /* Added, JDK1.1 inner classes */
1148 { parse_jdk1_1_error ("inner class declaration"); }
1149 ;
1150
1151 local_variable_declaration_statement:
1152 local_variable_declaration SC_TK /* Can't catch missing ';' here */
1153 ;
1154
1155 local_variable_declaration:
1156 type variable_declarators
1157 { declare_local_variables (0, $1, $2); }
1158 | modifiers type variable_declarators /* Added, JDK1.1 final locals */
1159 { declare_local_variables ($1, $2, $3); }
1160 ;
1161
1162 statement:
1163 statement_without_trailing_substatement
1164 | labeled_statement
1165 | if_then_statement
1166 | if_then_else_statement
1167 | while_statement
1168 | for_statement
1169 {
1170 /* If the for loop is unlabeled, we must return the
1171 block it was defined it. It our last chance to
1172 get a hold on it. */
1173 if (!LOOP_HAS_LABEL_P ($$))
1174 $$ = exit_block ();
1175 }
1176 ;
1177
1178 statement_nsi:
1179 statement_without_trailing_substatement
1180 | labeled_statement_nsi
1181 | if_then_else_statement_nsi
1182 | while_statement_nsi
1183 | for_statement_nsi
1184 ;
1185
1186 statement_without_trailing_substatement:
1187 block
1188 | empty_statement
1189 | expression_statement
1190 | switch_statement
1191 | do_statement
1192 | break_statement
1193 | continue_statement
1194 | return_statement
1195 | synchronized_statement
1196 | throw_statement
1197 | try_statement
1198 ;
1199
1200 empty_statement:
1201 SC_TK
1202 { $$ = empty_stmt_node; }
1203 ;
1204
1205 label_decl:
1206 identifier REL_CL_TK
1207 {
1208 $$ = build_labeled_block (EXPR_WFL_LINECOL ($1),
1209 EXPR_WFL_NODE ($1));
1210 pushlevel (2);
1211 push_labeled_block ($$);
1212 PUSH_LABELED_BLOCK ($$);
1213 }
1214 ;
1215
1216 labeled_statement:
1217 label_decl statement
1218 {
1219 $$ = complete_labeled_statement ($1, $2);
1220 pop_labeled_block ();
1221 POP_LABELED_BLOCK ();
1222 }
1223 | identifier error
1224 {yyerror ("':' expected"); RECOVER;}
1225 ;
1226
1227 labeled_statement_nsi:
1228 label_decl statement_nsi
1229 {
1230 $$ = complete_labeled_statement ($1, $2);
1231 pop_labeled_block ();
1232 POP_LABELED_BLOCK ();
1233 }
1234 ;
1235
1236 /* We concentrate here a bunch of error handling rules that we couldn't write
1237 earlier, because expression_statement catches a missing ';'. */
1238 expression_statement:
1239 statement_expression SC_TK
1240 {
1241 /* We have a statement. Generate a WFL around it so
1242 we can debug it */
1243 $$ = build_expr_wfl ($1, input_filename, lineno, 0);
1244 /* We know we have a statement, so set the debug
1245 info to be eventually generate here. */
1246 $$ = JAVA_MAYBE_GENERATE_DEBUG_INFO ($$);
1247 }
1248 | error SC_TK
1249 {
1250 if (ctxp->prevent_ese != lineno)
1251 yyerror ("Invalid expression statement");
1252 DRECOVER (expr_stmt);
1253 }
1254 | error OCB_TK
1255 {
1256 if (ctxp->prevent_ese != lineno)
1257 yyerror ("Invalid expression statement");
1258 DRECOVER (expr_stmt);
1259 }
1260 | error CCB_TK
1261 {
1262 if (ctxp->prevent_ese != lineno)
1263 yyerror ("Invalid expression statement");
1264 DRECOVER (expr_stmt);
1265 }
1266 | this_or_super OP_TK error
1267 {yyerror ("')' expected"); RECOVER;}
1268 | this_or_super OP_TK CP_TK error
1269 {
1270 yyerror ("Constructor invocation must be first "
1271 "thing in a constructor");
1272 RECOVER;
1273 }
1274 | this_or_super OP_TK argument_list error
1275 {yyerror ("')' expected"); RECOVER;}
1276 | this_or_super OP_TK argument_list CP_TK error
1277 {
1278 yyerror ("Constructor invocation must be first "
1279 "thing in a constructor");
1280 RECOVER;
1281 }
1282 | name DOT_TK SUPER_TK error
1283 {yyerror ("'(' expected"); RECOVER;}
1284 | name DOT_TK SUPER_TK OP_TK error
1285 {yyerror ("')' expected"); RECOVER;}
1286 | name DOT_TK SUPER_TK OP_TK argument_list error
1287 {yyerror ("')' expected"); RECOVER;}
1288 | name DOT_TK SUPER_TK OP_TK argument_list CP_TK error
1289 {yyerror ("';' expected"); RECOVER;}
1290 | name DOT_TK SUPER_TK OP_TK CP_TK error
1291 {yyerror ("';' expected"); RECOVER;}
1292 ;
1293
1294 statement_expression:
1295 assignment
1296 | pre_increment_expression
1297 | pre_decrement_expression
1298 | post_increment_expression
1299 | post_decrement_expression
1300 | method_invocation
1301 | class_instance_creation_expression
1302 ;
1303
1304 if_then_statement:
1305 IF_TK OP_TK expression CP_TK statement
1306 { $$ = build_if_else_statement ($2.location, $3, $5, NULL_TREE); }
1307 | IF_TK error
1308 {yyerror ("'(' expected"); RECOVER;}
1309 | IF_TK OP_TK error
1310 {yyerror ("Missing term"); RECOVER;}
1311 | IF_TK OP_TK expression error
1312 {yyerror ("')' expected"); RECOVER;}
1313 ;
1314
1315 if_then_else_statement:
1316 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement
1317 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1318 ;
1319
1320 if_then_else_statement_nsi:
1321 IF_TK OP_TK expression CP_TK statement_nsi ELSE_TK statement_nsi
1322 { $$ = build_if_else_statement ($2.location, $3, $5, $7); }
1323 ;
1324
1325 switch_statement:
1326 switch_expression
1327 {
1328 enter_block ();
1329 }
1330 switch_block
1331 {
1332 /* Make into "proper list" of COMPOUND_EXPRs.
1333 I.e. make the last statment also have its own
1334 COMPOUND_EXPR. */
1335 maybe_absorb_scoping_blocks ();
1336 TREE_OPERAND ($1, 1) = exit_block ();
1337 $$ = build_debugable_stmt (EXPR_WFL_LINECOL ($1), $1);
1338 }
1339 ;
1340
1341 switch_expression:
1342 SWITCH_TK OP_TK expression CP_TK
1343 {
1344 $$ = build (SWITCH_EXPR, NULL_TREE, $3, NULL_TREE);
1345 EXPR_WFL_LINECOL ($$) = $2.location;
1346 }
1347 | SWITCH_TK error
1348 {yyerror ("'(' expected"); RECOVER;}
1349 | SWITCH_TK OP_TK error
1350 {yyerror ("Missing term or ')'"); DRECOVER(switch_statement);}
1351 | SWITCH_TK OP_TK expression CP_TK error
1352 {yyerror ("'{' expected"); RECOVER;}
1353 ;
1354
1355 /* Default assignment is there to avoid type node on switch_block
1356 node. */
1357
1358 switch_block:
1359 OCB_TK CCB_TK
1360 { $$ = NULL_TREE; }
1361 | OCB_TK switch_labels CCB_TK
1362 { $$ = NULL_TREE; }
1363 | OCB_TK switch_block_statement_groups CCB_TK
1364 { $$ = NULL_TREE; }
1365 | OCB_TK switch_block_statement_groups switch_labels CCB_TK
1366 { $$ = NULL_TREE; }
1367 ;
1368
1369 switch_block_statement_groups:
1370 switch_block_statement_group
1371 | switch_block_statement_groups switch_block_statement_group
1372 ;
1373
1374 switch_block_statement_group:
1375 switch_labels block_statements
1376 ;
1377
1378 switch_labels:
1379 switch_label
1380 | switch_labels switch_label
1381 ;
1382
1383 switch_label:
1384 CASE_TK constant_expression REL_CL_TK
1385 {
1386 tree lab = build1 (CASE_EXPR, NULL_TREE, $2);
1387 EXPR_WFL_LINECOL (lab) = $1.location;
1388 java_method_add_stmt (current_function_decl, lab);
1389 }
1390 | DEFAULT_TK REL_CL_TK
1391 {
1392 tree lab = build1 (DEFAULT_EXPR, NULL_TREE, NULL_TREE);
1393 EXPR_WFL_LINECOL (lab) = $1.location;
1394 java_method_add_stmt (current_function_decl, lab);
1395 }
1396 | CASE_TK error
1397 {yyerror ("Missing or invalid constant expression"); RECOVER;}
1398 | CASE_TK constant_expression error
1399 {yyerror ("':' expected"); RECOVER;}
1400 | DEFAULT_TK error
1401 {yyerror ("':' expected"); RECOVER;}
1402 ;
1403
1404 while_expression:
1405 WHILE_TK OP_TK expression CP_TK
1406 {
1407 tree body = build_loop_body ($2.location, $3, 0);
1408 $$ = build_new_loop (body);
1409 }
1410 ;
1411
1412 while_statement:
1413 while_expression statement
1414 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1415 | WHILE_TK error
1416 {YYERROR_NOW; yyerror ("'(' expected"); RECOVER;}
1417 | WHILE_TK OP_TK error
1418 {yyerror ("Missing term and ')' expected"); RECOVER;}
1419 | WHILE_TK OP_TK expression error
1420 {yyerror ("')' expected"); RECOVER;}
1421 ;
1422
1423 while_statement_nsi:
1424 while_expression statement_nsi
1425 { $$ = complete_loop_body (0, NULL_TREE, $2, 0); }
1426 ;
1427
1428 do_statement_begin:
1429 DO_TK
1430 {
1431 tree body = build_loop_body (0, NULL_TREE, 1);
1432 $$ = build_new_loop (body);
1433 }
1434 /* Need error handing here. FIXME */
1435 ;
1436
1437 do_statement:
1438 do_statement_begin statement WHILE_TK OP_TK expression CP_TK SC_TK
1439 { $$ = complete_loop_body ($4.location, $5, $2, 1); }
1440 ;
1441
1442 for_statement:
1443 for_begin SC_TK expression SC_TK for_update CP_TK statement
1444 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1445 | for_begin SC_TK SC_TK for_update CP_TK statement
1446 {
1447 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1448 /* We have not condition, so we get rid of the EXIT_EXPR */
1449 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1450 empty_stmt_node;
1451 }
1452 | for_begin SC_TK error
1453 {yyerror ("Invalid control expression"); RECOVER;}
1454 | for_begin SC_TK expression SC_TK error
1455 {yyerror ("Invalid update expression"); RECOVER;}
1456 | for_begin SC_TK SC_TK error
1457 {yyerror ("Invalid update expression"); RECOVER;}
1458 ;
1459
1460 for_statement_nsi:
1461 for_begin SC_TK expression SC_TK for_update CP_TK statement_nsi
1462 { $$ = complete_for_loop (EXPR_WFL_LINECOL ($3), $3, $5, $7);}
1463 | for_begin SC_TK SC_TK for_update CP_TK statement_nsi
1464 {
1465 $$ = complete_for_loop (0, NULL_TREE, $4, $6);
1466 /* We have not condition, so we get rid of the EXIT_EXPR */
1467 LOOP_EXPR_BODY_CONDITION_EXPR (LOOP_EXPR_BODY ($$), 0) =
1468 empty_stmt_node;
1469 }
1470 ;
1471
1472 for_header:
1473 FOR_TK OP_TK
1474 {
1475 /* This scope defined for local variable that may be
1476 defined within the scope of the for loop */
1477 enter_block ();
1478 }
1479 | FOR_TK error
1480 {yyerror ("'(' expected"); DRECOVER(for_1);}
1481 | FOR_TK OP_TK error
1482 {yyerror ("Invalid init statement"); RECOVER;}
1483 ;
1484
1485 for_begin:
1486 for_header for_init
1487 {
1488 /* We now declare the loop body. The loop is
1489 declared as a for loop. */
1490 tree body = build_loop_body (0, NULL_TREE, 0);
1491 $$ = build_new_loop (body);
1492 IS_FOR_LOOP_P ($$) = 1;
1493 /* The loop is added to the current block the for
1494 statement is defined within */
1495 java_method_add_stmt (current_function_decl, $$);
1496 }
1497 ;
1498 for_init: /* Can be empty */
1499 { $$ = empty_stmt_node; }
1500 | statement_expression_list
1501 {
1502 /* Init statement recorded within the previously
1503 defined block scope */
1504 $$ = java_method_add_stmt (current_function_decl, $1);
1505 }
1506 | local_variable_declaration
1507 {
1508 /* Local variable are recorded within the previously
1509 defined block scope */
1510 $$ = NULL_TREE;
1511 }
1512 | statement_expression_list error
1513 {yyerror ("';' expected"); DRECOVER(for_init_1);}
1514 ;
1515
1516 for_update: /* Can be empty */
1517 {$$ = empty_stmt_node;}
1518 | statement_expression_list
1519 { $$ = build_debugable_stmt (BUILD_LOCATION (), $1); }
1520 ;
1521
1522 statement_expression_list:
1523 statement_expression
1524 { $$ = add_stmt_to_compound (NULL_TREE, NULL_TREE, $1); }
1525 | statement_expression_list C_TK statement_expression
1526 { $$ = add_stmt_to_compound ($1, NULL_TREE, $3); }
1527 | statement_expression_list C_TK error
1528 {yyerror ("Missing term"); RECOVER;}
1529 ;
1530
1531 break_statement:
1532 BREAK_TK SC_TK
1533 { $$ = build_bc_statement ($1.location, 1, NULL_TREE); }
1534 | BREAK_TK identifier SC_TK
1535 { $$ = build_bc_statement ($1.location, 1, $2); }
1536 | BREAK_TK error
1537 {yyerror ("Missing term"); RECOVER;}
1538 | BREAK_TK identifier error
1539 {yyerror ("';' expected"); RECOVER;}
1540 ;
1541
1542 continue_statement:
1543 CONTINUE_TK SC_TK
1544 { $$ = build_bc_statement ($1.location, 0, NULL_TREE); }
1545 | CONTINUE_TK identifier SC_TK
1546 { $$ = build_bc_statement ($1.location, 0, $2); }
1547 | CONTINUE_TK error
1548 {yyerror ("Missing term"); RECOVER;}
1549 | CONTINUE_TK identifier error
1550 {yyerror ("';' expected"); RECOVER;}
1551 ;
1552
1553 return_statement:
1554 RETURN_TK SC_TK
1555 { $$ = build_return ($1.location, NULL_TREE); }
1556 | RETURN_TK expression SC_TK
1557 { $$ = build_return ($1.location, $2); }
1558 | RETURN_TK error
1559 {yyerror ("Missing term"); RECOVER;}
1560 | RETURN_TK expression error
1561 {yyerror ("';' expected"); RECOVER;}
1562 ;
1563
1564 throw_statement:
1565 THROW_TK expression SC_TK
1566 {
1567 $$ = build1 (THROW_EXPR, NULL_TREE, $2);
1568 EXPR_WFL_LINECOL ($$) = $1.location;
1569 }
1570 | THROW_TK error
1571 {yyerror ("Missing term"); RECOVER;}
1572 | THROW_TK expression error
1573 {yyerror ("';' expected"); RECOVER;}
1574 ;
1575
1576 synchronized_statement:
1577 synchronized OP_TK expression CP_TK block
1578 {
1579 $$ = build (SYNCHRONIZED_EXPR, NULL_TREE, $3, $5);
1580 EXPR_WFL_LINECOL ($$) =
1581 EXPR_WFL_LINECOL (MODIFIER_WFL (SYNCHRONIZED_TK));
1582 }
1583 | synchronized OP_TK expression CP_TK error
1584 {yyerror ("'{' expected"); RECOVER;}
1585 | synchronized error
1586 {yyerror ("'(' expected"); RECOVER;}
1587 | synchronized OP_TK error CP_TK
1588 {yyerror ("Missing term"); RECOVER;}
1589 | synchronized OP_TK error
1590 {yyerror ("Missing term"); RECOVER;}
1591 ;
1592
1593 synchronized:
1594 MODIFIER_TK
1595 {
1596 if ((1 << $1) != ACC_SYNCHRONIZED)
1597 fatal ("synchronized was '%d' - yyparse", (1 << $1));
1598 }
1599 ;
1600
1601 try_statement:
1602 TRY_TK block catches
1603 { $$ = build_try_statement ($1.location, $2, $3, NULL_TREE); }
1604 | TRY_TK block finally
1605 { $$ = build_try_statement ($1.location, $2, NULL_TREE, $3); }
1606 | TRY_TK block catches finally
1607 { $$ = build_try_statement ($1.location, $2, $3, $4); }
1608 | TRY_TK error
1609 {yyerror ("'{' expected"); DRECOVER (try_statement);}
1610 ;
1611
1612 catches:
1613 catch_clause
1614 | catches catch_clause
1615 {
1616 TREE_CHAIN ($2) = $1;
1617 $$ = $2;
1618 }
1619 ;
1620
1621 catch_clause:
1622 catch_clause_parameter block
1623 {
1624 java_method_add_stmt (current_function_decl, $2);
1625 exit_block ();
1626 $$ = $1;
1627 }
1628
1629 catch_clause_parameter:
1630 CATCH_TK OP_TK formal_parameter CP_TK
1631 {
1632 /* We add a block to define a scope for
1633 formal_parameter (CCBP). The formal parameter is
1634 declared initialized by the appropriate function
1635 call */
1636 tree ccpb = enter_block ();
1637 tree init = build_assignment (ASSIGN_TK, $2.location,
1638 TREE_PURPOSE ($3),
1639 soft_exceptioninfo_call_node);
1640 declare_local_variables (0, TREE_VALUE ($3),
1641 build_tree_list (TREE_PURPOSE ($3),
1642 init));
1643 $$ = build1 (CATCH_EXPR, NULL_TREE, ccpb);
1644 EXPR_WFL_LINECOL ($$) = $1.location;
1645 }
1646 | CATCH_TK error
1647 {yyerror ("'(' expected"); RECOVER;}
1648 | CATCH_TK OP_TK error
1649 {yyerror ("Missing term or ')' expected"); DRECOVER (2);}
1650 | CATCH_TK OP_TK error CP_TK /* That's for () */
1651 {yyerror ("')' expected"); DRECOVER (1);}
1652 ;
1653
1654 finally:
1655 FINALLY_TK block
1656 {
1657 $$ = build (FINALLY_EXPR, NULL_TREE,
1658 create_label_decl (generate_name ()), $2);
1659 }
1660 | FINALLY_TK error
1661 {yyerror ("'{' expected"); RECOVER; }
1662 ;
1663
1664 /* 19.12 Production from 15: Expressions */
1665 primary:
1666 primary_no_new_array
1667 | array_creation_expression
1668 ;
1669
1670 primary_no_new_array:
1671 literal
1672 | THIS_TK
1673 { $$ = build_this ($1.location); }
1674 | OP_TK expression CP_TK
1675 {$$ = $2;}
1676 | class_instance_creation_expression
1677 | field_access
1678 | method_invocation
1679 | array_access
1680 /* type DOT_TK CLASS_TK doens't work. So we split the rule
1681 'type' into its components. Missing is something for array,
1682 which will complete the reference_type part. FIXME */
1683 | name DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1684 { $$ = parse_jdk1_1_error ("named class literals"); }
1685 | primitive_type DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1686 { $$ = build_class_ref ($1); }
1687 | VOID_TK DOT_TK CLASS_TK /* Added, JDK1.1 class literals */
1688 { $$ = build_class_ref (void_type_node); }
1689 /* Added, JDK1.1 inner classes. Documentation is wrong
1690 refering to a 'ClassName' (class_name) rule that doesn't
1691 exist. Used name instead. */
1692 | name DOT_TK THIS_TK
1693 { $$ = parse_jdk1_1_error ("class literals"); }
1694 | OP_TK expression error
1695 {yyerror ("')' expected"); RECOVER;}
1696 | name DOT_TK error
1697 {yyerror ("'class' or 'this' expected" ); RECOVER;}
1698 | primitive_type DOT_TK error
1699 {yyerror ("'class' expected" ); RECOVER;}
1700 | VOID_TK DOT_TK error
1701 {yyerror ("'class' expected" ); RECOVER;}
1702 ;
1703
1704 class_instance_creation_expression:
1705 NEW_TK class_type OP_TK argument_list CP_TK
1706 { $$ = build_new_invocation ($2, $4); }
1707 | NEW_TK class_type OP_TK CP_TK
1708 { $$ = build_new_invocation ($2, NULL_TREE); }
1709 /* Added, JDK1.1 inner classes but modified to use
1710 'class_type' instead of 'TypeName' (type_name) mentionned
1711 in the documentation but doesn't exist. */
1712 | NEW_TK class_type OP_TK argument_list CP_TK class_body
1713 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1714 | NEW_TK class_type OP_TK CP_TK class_body
1715 { $$ = parse_jdk1_1_error ("inner class instance creation"); }
1716 /* Added, JDK1.1 inner classes, modified to use name or
1717 primary instead of primary solely which couldn't work in
1718 all situations. */
1719 | something_dot_new identifier OP_TK CP_TK
1720 | something_dot_new identifier OP_TK CP_TK class_body
1721 | something_dot_new identifier OP_TK argument_list CP_TK
1722 | something_dot_new identifier OP_TK argument_list CP_TK class_body
1723 | NEW_TK error SC_TK
1724 {yyerror ("'(' expected"); DRECOVER(new_1);}
1725 | NEW_TK class_type error
1726 {yyerror ("'(' expected"); RECOVER;}
1727 | NEW_TK class_type OP_TK error
1728 {yyerror ("')' or term expected"); RECOVER;}
1729 | NEW_TK class_type OP_TK argument_list error
1730 {yyerror ("')' expected"); RECOVER;}
1731 | something_dot_new error
1732 {YYERROR_NOW; yyerror ("Identifier expected"); RECOVER;}
1733 | something_dot_new identifier error
1734 {yyerror ("'(' expected"); RECOVER;}
1735 ;
1736
1737 something_dot_new: /* Added, not part of the specs. */
1738 name DOT_TK NEW_TK
1739 | primary DOT_TK NEW_TK
1740 ;
1741
1742 argument_list:
1743 expression
1744 {
1745 $$ = tree_cons (NULL_TREE, $1, NULL_TREE);
1746 ctxp->formal_parameter_number = 1;
1747 }
1748 | argument_list C_TK expression
1749 {
1750 ctxp->formal_parameter_number += 1;
1751 $$ = tree_cons (NULL_TREE, $3, $1);
1752 }
1753 | argument_list C_TK error
1754 {yyerror ("Missing term"); RECOVER;}
1755 ;
1756
1757 array_creation_expression:
1758 NEW_TK primitive_type dim_exprs
1759 { $$ = build_newarray_node ($2, $3, 0); }
1760 | NEW_TK class_or_interface_type dim_exprs
1761 { $$ = build_newarray_node ($2, $3, 0); }
1762 | NEW_TK primitive_type dim_exprs dims
1763 { $$ = build_newarray_node ($2, $3, ctxp->osb_number); }
1764 | NEW_TK class_or_interface_type dim_exprs dims
1765 { $$ = build_newarray_node ($2, $3, ctxp->osb_number); }
1766 /* Added, JDK1.1 anonymous array. Initial documentation rule
1767 modified */
1768 | NEW_TK class_or_interface_type dims array_initializer
1769 { $$ = parse_jdk1_1_error ("anonymous array"); }
1770 | NEW_TK primitive_type dims array_initializer
1771 { $$ = parse_jdk1_1_error ("anonymous array"); }
1772 | NEW_TK error CSB_TK
1773 {yyerror ("'[' expected"); DRECOVER ("]");}
1774 | NEW_TK error OSB_TK
1775 {yyerror ("']' expected"); RECOVER;}
1776 ;
1777
1778 dim_exprs:
1779 dim_expr
1780 { $$ = build_tree_list (NULL_TREE, $1); }
1781 | dim_exprs dim_expr
1782 { $$ = tree_cons (NULL_TREE, $2, $$); }
1783 ;
1784
1785 dim_expr:
1786 OSB_TK expression CSB_TK
1787 {
1788 EXPR_WFL_LINECOL ($2) = $1.location;
1789 $$ = $2;
1790 }
1791 | OSB_TK expression error
1792 {yyerror ("']' expected"); RECOVER;}
1793 | OSB_TK error
1794 {
1795 yyerror ("Missing term");
1796 yyerror ("']' expected");
1797 RECOVER;
1798 }
1799 ;
1800
1801 dims:
1802 OSB_TK CSB_TK
1803 { ctxp->osb_number = 1; }
1804 | dims OSB_TK CSB_TK
1805 { ctxp->osb_number++; }
1806 | dims OSB_TK error
1807 { yyerror ("']' expected"); RECOVER;}
1808 ;
1809
1810 field_access:
1811 primary DOT_TK identifier
1812 { $$ = make_qualified_primary ($1, $3, $2.location); }
1813 /* FIXME - REWRITE TO:
1814 { $$ = build_binop (COMPONENT_REF, $2.location, $1, $3); } */
1815 | SUPER_TK DOT_TK identifier
1816 {
1817 tree super_wfl =
1818 build_wfl_node (super_identifier_node,
1819 input_filename, 0, 0);
1820 EXPR_WFL_LINECOL (super_wfl) = $1.location;
1821 $$ = make_qualified_name (super_wfl, $3, $2.location);
1822 }
1823 | SUPER_TK error
1824 {yyerror ("Field expected"); DRECOVER (super_field_acces);}
1825 ;
1826
1827 method_invocation:
1828 name OP_TK CP_TK
1829 { $$ = build_method_invocation ($1, NULL_TREE); }
1830 | name OP_TK argument_list CP_TK
1831 { $$ = build_method_invocation ($1, $3); }
1832 | primary DOT_TK identifier OP_TK CP_TK
1833 {
1834 if (TREE_CODE ($1) == THIS_EXPR)
1835 $$ = build_this_super_qualified_invocation
1836 (1, $3, NULL_TREE, 0, $2.location);
1837 else
1838 {
1839 tree invok = build_method_invocation ($3, NULL_TREE);
1840 $$ = make_qualified_primary ($1, invok, $2.location);
1841 }
1842 }
1843 | primary DOT_TK identifier OP_TK argument_list CP_TK
1844 {
1845 if (TREE_CODE ($1) == THIS_EXPR)
1846 $$ = build_this_super_qualified_invocation
1847 (1, $3, $5, 0, $2.location);
1848 else
1849 {
1850 tree invok = build_method_invocation ($3, $5);
1851 $$ = make_qualified_primary ($1, invok, $2.location);
1852 }
1853 }
1854 | SUPER_TK DOT_TK identifier OP_TK CP_TK
1855 {
1856 $$ = build_this_super_qualified_invocation
1857 (0, $3, NULL_TREE, $1.location, $2.location);
1858 }
1859 | SUPER_TK DOT_TK identifier OP_TK argument_list CP_TK
1860 {
1861 $$ = build_this_super_qualified_invocation
1862 (0, $3, $5, $1.location, $2.location);
1863 }
1864 /* Screws up thing. I let it here until I'm convinced it can
1865 be removed. FIXME
1866 | primary DOT_TK error
1867 {yyerror ("'(' expected"); DRECOVER(bad);} */
1868 | SUPER_TK DOT_TK error CP_TK
1869 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1870 | SUPER_TK DOT_TK error DOT_TK
1871 { yyerror ("'(' expected"); DRECOVER (method_invocation); }
1872 ;
1873
1874 array_access:
1875 name OSB_TK expression CSB_TK
1876 { $$ = build_array_ref ($2.location, $1, $3); }
1877 | primary_no_new_array OSB_TK expression CSB_TK
1878 { $$ = build_array_ref ($2.location, $1, $3); }
1879 | name OSB_TK error
1880 {
1881 yyerror ("Missing term and ']' expected");
1882 DRECOVER(array_access);
1883 }
1884 | name OSB_TK expression error
1885 {
1886 yyerror ("']' expected");
1887 DRECOVER(array_access);
1888 }
1889 | primary_no_new_array OSB_TK error
1890 {
1891 yyerror ("Missing term and ']' expected");
1892 DRECOVER(array_access);
1893 }
1894 | primary_no_new_array OSB_TK expression error
1895 {
1896 yyerror ("']' expected");
1897 DRECOVER(array_access);
1898 }
1899 ;
1900
1901 postfix_expression:
1902 primary
1903 | name
1904 | post_increment_expression
1905 | post_decrement_expression
1906 ;
1907
1908 post_increment_expression:
1909 postfix_expression INCR_TK
1910 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1911 ;
1912
1913 post_decrement_expression:
1914 postfix_expression DECR_TK
1915 { $$ = build_incdec ($2.token, $2.location, $1, 1); }
1916 ;
1917
1918 unary_expression:
1919 pre_increment_expression
1920 | pre_decrement_expression
1921 | PLUS_TK unary_expression
1922 {$$ = build_unaryop ($1.token, $1.location, $2); }
1923 | MINUS_TK unary_expression
1924 {$$ = build_unaryop ($1.token, $1.location, $2); }
1925 | unary_expression_not_plus_minus
1926 | PLUS_TK error
1927 {yyerror ("Missing term"); RECOVER}
1928 | MINUS_TK error
1929 {yyerror ("Missing term"); RECOVER}
1930 ;
1931
1932 pre_increment_expression:
1933 INCR_TK unary_expression
1934 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1935 | INCR_TK error
1936 {yyerror ("Missing term"); RECOVER}
1937 ;
1938
1939 pre_decrement_expression:
1940 DECR_TK unary_expression
1941 {$$ = build_incdec ($1.token, $1.location, $2, 0); }
1942 | DECR_TK error
1943 {yyerror ("Missing term"); RECOVER}
1944 ;
1945
1946 unary_expression_not_plus_minus:
1947 postfix_expression
1948 | NOT_TK unary_expression
1949 {$$ = build_unaryop ($1.token, $1.location, $2); }
1950 | NEG_TK unary_expression
1951 {$$ = build_unaryop ($1.token, $1.location, $2); }
1952 | cast_expression
1953 | NOT_TK error
1954 {yyerror ("Missing term"); RECOVER}
1955 | NEG_TK error
1956 {yyerror ("Missing term"); RECOVER}
1957 ;
1958
1959 cast_expression: /* Error handling here is potentially weak */
1960 OP_TK primitive_type dims CP_TK unary_expression
1961 {
1962 tree type = $2;
1963 while (ctxp->osb_number--)
1964 type = build_java_array_type (type, -1);
1965 $$ = build_cast ($1.location, type, $5);
1966 }
1967 | OP_TK primitive_type CP_TK unary_expression
1968 { $$ = build_cast ($1.location, $2, $4); }
1969 | OP_TK expression CP_TK unary_expression_not_plus_minus
1970 { $$ = build_cast ($1.location, $2, $4); }
1971 | OP_TK name dims CP_TK unary_expression_not_plus_minus
1972 {
1973 char *ptr;
1974 while (ctxp->osb_number--)
1975 obstack_1grow (&temporary_obstack, '[');
1976 obstack_grow0 (&temporary_obstack,
1977 IDENTIFIER_POINTER (EXPR_WFL_NODE ($2)),
1978 IDENTIFIER_LENGTH (EXPR_WFL_NODE ($2)));
1979 ptr = obstack_finish (&temporary_obstack);
1980 EXPR_WFL_NODE ($2) = get_identifier (ptr);
1981 $$ = build_cast ($1.location, $2, $5);
1982 }
1983 | OP_TK primitive_type OSB_TK error
1984 {yyerror ("']' expected, invalid type expression");}
1985 | OP_TK error
1986 {
1987 if (ctxp->prevent_ese != lineno)
1988 yyerror ("Invalid type expression"); RECOVER;
1989 RECOVER;
1990 }
1991 | OP_TK primitive_type dims CP_TK error
1992 {yyerror ("Missing term"); RECOVER;}
1993 | OP_TK primitive_type CP_TK error
1994 {yyerror ("Missing term"); RECOVER;}
1995 | OP_TK name dims CP_TK error
1996 {yyerror ("Missing term"); RECOVER;}
1997 ;
1998
1999 multiplicative_expression:
2000 unary_expression
2001 | multiplicative_expression MULT_TK unary_expression
2002 {
2003 $$ = build_binop (BINOP_LOOKUP ($2.token),
2004 $2.location, $1, $3);
2005 }
2006 | multiplicative_expression DIV_TK unary_expression
2007 {
2008 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2009 $1, $3);
2010 }
2011 | multiplicative_expression REM_TK unary_expression
2012 {
2013 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2014 $1, $3);
2015 }
2016 | multiplicative_expression MULT_TK error
2017 {yyerror ("Missing term"); RECOVER;}
2018 | multiplicative_expression DIV_TK error
2019 {yyerror ("Missing term"); RECOVER;}
2020 | multiplicative_expression REM_TK error
2021 {yyerror ("Missing term"); RECOVER;}
2022 ;
2023
2024 additive_expression:
2025 multiplicative_expression
2026 | additive_expression PLUS_TK multiplicative_expression
2027 {
2028 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2029 $1, $3);
2030 }
2031 | additive_expression MINUS_TK multiplicative_expression
2032 {
2033 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2034 $1, $3);
2035 }
2036 | additive_expression PLUS_TK error
2037 {yyerror ("Missing term"); RECOVER;}
2038 | additive_expression MINUS_TK error
2039 {yyerror ("Missing term"); RECOVER;}
2040 ;
2041
2042 shift_expression:
2043 additive_expression
2044 | shift_expression LS_TK additive_expression
2045 {
2046 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2047 $1, $3);
2048 }
2049 | shift_expression SRS_TK additive_expression
2050 {
2051 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2052 $1, $3);
2053 }
2054 | shift_expression ZRS_TK additive_expression
2055 {
2056 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2057 $1, $3);
2058 }
2059 | shift_expression LS_TK error
2060 {yyerror ("Missing term"); RECOVER;}
2061 | shift_expression SRS_TK error
2062 {yyerror ("Missing term"); RECOVER;}
2063 | shift_expression ZRS_TK error
2064 {yyerror ("Missing term"); RECOVER;}
2065 ;
2066
2067 relational_expression:
2068 shift_expression
2069 | relational_expression LT_TK shift_expression
2070 {
2071 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2072 $1, $3);
2073 }
2074 | relational_expression GT_TK shift_expression
2075 {
2076 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2077 $1, $3);
2078 }
2079 | relational_expression LTE_TK shift_expression
2080 {
2081 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2082 $1, $3);
2083 }
2084 | relational_expression GTE_TK shift_expression
2085 {
2086 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2087 $1, $3);
2088 }
2089 | relational_expression INSTANCEOF_TK reference_type
2090 { $$ = build_binop (INSTANCEOF_EXPR, $2.location, $1, $3); }
2091 | relational_expression LT_TK error
2092 {yyerror ("Missing term"); RECOVER;}
2093 | relational_expression GT_TK error
2094 {yyerror ("Missing term"); RECOVER;}
2095 | relational_expression LTE_TK error
2096 {yyerror ("Missing term"); RECOVER;}
2097 | relational_expression GTE_TK error
2098 {yyerror ("Missing term"); RECOVER;}
2099 | relational_expression INSTANCEOF_TK error
2100 {yyerror ("Invalid reference type"); RECOVER;}
2101 ;
2102
2103 equality_expression:
2104 relational_expression
2105 | equality_expression EQ_TK relational_expression
2106 {
2107 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2108 $1, $3);
2109 }
2110 | equality_expression NEQ_TK relational_expression
2111 {
2112 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2113 $1, $3);
2114 }
2115 | equality_expression EQ_TK error
2116 {yyerror ("Missing term"); RECOVER;}
2117 | equality_expression NEQ_TK error
2118 {yyerror ("Missing term"); RECOVER;}
2119 ;
2120
2121 and_expression:
2122 equality_expression
2123 | and_expression AND_TK equality_expression
2124 {
2125 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2126 $1, $3);
2127 }
2128 | and_expression AND_TK error
2129 {yyerror ("Missing term"); RECOVER;}
2130 ;
2131
2132 exclusive_or_expression:
2133 and_expression
2134 | exclusive_or_expression XOR_TK and_expression
2135 {
2136 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2137 $1, $3);
2138 }
2139 | exclusive_or_expression XOR_TK error
2140 {yyerror ("Missing term"); RECOVER;}
2141 ;
2142
2143 inclusive_or_expression:
2144 exclusive_or_expression
2145 | inclusive_or_expression OR_TK exclusive_or_expression
2146 {
2147 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2148 $1, $3);
2149 }
2150 | inclusive_or_expression OR_TK error
2151 {yyerror ("Missing term"); RECOVER;}
2152 ;
2153
2154 conditional_and_expression:
2155 inclusive_or_expression
2156 | conditional_and_expression BOOL_AND_TK inclusive_or_expression
2157 {
2158 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2159 $1, $3);
2160 }
2161 | conditional_and_expression BOOL_AND_TK error
2162 {yyerror ("Missing term"); RECOVER;}
2163 ;
2164
2165 conditional_or_expression:
2166 conditional_and_expression
2167 | conditional_or_expression BOOL_OR_TK conditional_and_expression
2168 {
2169 $$ = build_binop (BINOP_LOOKUP ($2.token), $2.location,
2170 $1, $3);
2171 }
2172 | conditional_or_expression BOOL_OR_TK error
2173 {yyerror ("Missing term"); RECOVER;}
2174 ;
2175
2176 conditional_expression: /* Error handling here is weak */
2177 conditional_or_expression
2178 | conditional_or_expression REL_QM_TK expression REL_CL_TK conditional_expression
2179 {
2180 $$ = build (CONDITIONAL_EXPR, NULL_TREE, $1, $3, $5);
2181 EXPR_WFL_LINECOL ($$) = $2.location;
2182 }
2183 | conditional_or_expression REL_QM_TK REL_CL_TK error
2184 {
2185 YYERROR_NOW;
2186 yyerror ("Missing term");
2187 DRECOVER (1);
2188 }
2189 | conditional_or_expression REL_QM_TK error
2190 {yyerror ("Missing term"); DRECOVER (2);}
2191 | conditional_or_expression REL_QM_TK expression REL_CL_TK error
2192 {yyerror ("Missing term"); DRECOVER (3);}
2193 ;
2194
2195 assignment_expression:
2196 conditional_expression
2197 | assignment
2198 ;
2199
2200 assignment:
2201 left_hand_side assignment_operator assignment_expression
2202 { $$ = build_assignment ($2.token, $2.location, $1, $3); }
2203 | left_hand_side assignment_operator error
2204 {
2205 if (ctxp->prevent_ese != lineno)
2206 yyerror ("Missing term");
2207 DRECOVER (assign);
2208 }
2209 ;
2210
2211 left_hand_side:
2212 name
2213 | field_access
2214 | array_access
2215 ;
2216
2217 assignment_operator:
2218 ASSIGN_ANY_TK
2219 | ASSIGN_TK
2220 ;
2221
2222 expression:
2223 assignment_expression
2224 ;
2225
2226 constant_expression:
2227 expression
2228 ;
2229
2230 %%
2231 \f
2232
2233 #include "lex.c"
2234
2235 /* Flag for the error report routine to issue the error the first time
2236 it's called (overriding the default behavior which is to drop the
2237 first invocation and honor the second one, taking advantage of a
2238 richer context. */
2239 static int force_error = 0;
2240
2241 /* Create a new parser context and make it the current one. */
2242
2243 void
2244 java_push_parser_context ()
2245 {
2246 struct parser_ctxt *new =
2247 (struct parser_ctxt *)xmalloc(sizeof (struct parser_ctxt));
2248
2249 bzero (new, sizeof (struct parser_ctxt));
2250 new->next = ctxp;
2251 ctxp = new;
2252 if (ctxp->next)
2253 {
2254 ctxp->incomplete_class = ctxp->next->incomplete_class;
2255 ctxp->gclass_list = ctxp->next->gclass_list;
2256 }
2257 }
2258
2259 /* If the first file of a file list was a class file, no context
2260 exists for a source file to be parsed. This boolean remembers that
2261 java_parser_context_save_global might have created a dummy one, so
2262 that java_parser_context_restore_global can pop it. */
2263 static int extra_ctxp_pushed_p = 0;
2264
2265 void
2266 java_parser_context_save_global ()
2267 {
2268 if (!ctxp)
2269 {
2270 java_push_parser_context ();
2271 extra_ctxp_pushed_p = 1;
2272 }
2273 ctxp->finput = finput;
2274 ctxp->lineno = lineno;
2275 ctxp->current_class = current_class;
2276 ctxp->filename = input_filename;
2277 ctxp->current_function_decl = current_function_decl;
2278 }
2279
2280 void
2281 java_parser_context_restore_global ()
2282 {
2283 finput = ctxp->finput;
2284 lineno = ctxp->lineno;
2285 current_class = ctxp->current_class;
2286 input_filename = ctxp->filename;
2287 current_function_decl = ctxp->current_function_decl;
2288 if (!ctxp->next && extra_ctxp_pushed_p)
2289 {
2290 java_pop_parser_context (0);
2291 extra_ctxp_pushed_p = 0;
2292 }
2293 }
2294
2295 void
2296 java_pop_parser_context (generate)
2297 int generate;
2298 {
2299 tree current;
2300 struct parser_ctxt *toFree, *next;
2301
2302 if (!ctxp)
2303 return;
2304
2305 toFree = ctxp;
2306 next = ctxp->next;
2307 if (next)
2308 {
2309 next->incomplete_class = ctxp->incomplete_class;
2310 next->gclass_list = ctxp->gclass_list;
2311 lineno = ctxp->lineno;
2312 finput = ctxp->finput;
2313 current_class = ctxp->current_class;
2314 }
2315
2316 /* Set the single import class file flag to 0 for the current list
2317 of imported things */
2318 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2319 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 0;
2320
2321 /* And restore those of the previous context */
2322 if ((ctxp = next)) /* Assignment is really meant here */
2323 for (current = ctxp->import_list; current; current = TREE_CHAIN (current))
2324 IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (TREE_PURPOSE (current)) = 1;
2325
2326 if (generate)
2327 {
2328 toFree->next = ctxp_for_generation;
2329 ctxp_for_generation = toFree;
2330 }
2331 else
2332 free (toFree);
2333 }
2334
2335 /* Reporting JDK1.1 features not implemented */
2336
2337 static tree
2338 parse_jdk1_1_error (msg)
2339 char *msg;
2340 {
2341 sorry (": `%s' JDK1.1(TM) feature", msg);
2342 java_error_count++;
2343 return empty_stmt_node;
2344 }
2345
2346 static int do_warning = 0;
2347
2348 void
2349 yyerror (msg)
2350 char *msg;
2351 {
2352 static java_lc elc;
2353 static int prev_lineno;
2354 static char *prev_msg;
2355
2356 int save_lineno;
2357 char *remainder, *code_from_source;
2358 extern struct obstack temporary_obstack;
2359
2360 if (!force_error && prev_lineno == lineno)
2361 return;
2362
2363 /* Save current error location but report latter, when the context is
2364 richer. */
2365 if (ctxp->java_error_flag == 0)
2366 {
2367 ctxp->java_error_flag = 1;
2368 elc = ctxp->elc;
2369 /* Do something to use the previous line if we're reaching the
2370 end of the file... */
2371 #ifdef VERBOSE_SKELETON
2372 printf ("* Error detected (%s)\n", (msg ? msg : "(null)"));
2373 #endif
2374 return;
2375 }
2376
2377 /* Ignore duplicate message on the same line. BTW, this is dubious. FIXME */
2378 if (!force_error && msg == prev_msg && prev_lineno == elc.line)
2379 return;
2380
2381 ctxp->java_error_flag = 0;
2382 if (do_warning)
2383 java_warning_count++;
2384 else
2385 java_error_count++;
2386
2387 if (elc.col == 0 && msg[1] == ';')
2388 {
2389 elc.col = ctxp->p_line->char_col-1;
2390 elc.line = ctxp->p_line->lineno;
2391 }
2392
2393 save_lineno = lineno;
2394 prev_lineno = lineno = elc.line;
2395 prev_msg = msg;
2396
2397 code_from_source = java_get_line_col (ctxp->filename, elc.line, elc.col);
2398 obstack_grow0 (&temporary_obstack,
2399 code_from_source, strlen (code_from_source));
2400 remainder = obstack_finish (&temporary_obstack);
2401 if (do_warning)
2402 warning ("%s.\n%s", msg, remainder);
2403 else
2404 error ("%s.\n%s", msg, remainder);
2405
2406 /* This allow us to cheaply avoid an extra 'Invalid expression
2407 statement' error report when errors have been already reported on
2408 the same line. This occurs when we report an error but don't have
2409 a synchronization point other than ';', which
2410 expression_statement is the only one to take care of. */
2411 ctxp->prevent_ese = lineno = save_lineno;
2412 }
2413
2414 static void
2415 issue_warning_error_from_context (cl, msg, ap)
2416 tree cl;
2417 char *msg;
2418 va_list ap;
2419 {
2420 char *saved, *saved_input_filename;
2421 char buffer [4096];
2422 vsprintf (buffer, msg, ap);
2423 force_error = 1;
2424
2425 ctxp->elc.line = EXPR_WFL_LINENO (cl);
2426 ctxp->elc.col = (EXPR_WFL_COLNO (cl) == 0xfff ? -1 :
2427 (EXPR_WFL_COLNO (cl) == 0xffe ? -2 : EXPR_WFL_COLNO (cl)));
2428
2429 /* We have a CL, that's a good reason for using it if it contains data */
2430 saved = ctxp->filename;
2431 if (TREE_CODE (cl) == EXPR_WITH_FILE_LOCATION && EXPR_WFL_FILENAME_NODE (cl))
2432 ctxp->filename = EXPR_WFL_FILENAME (cl);
2433 saved_input_filename = input_filename;
2434 input_filename = ctxp->filename;
2435 java_error (NULL);
2436 java_error (buffer);
2437 ctxp->filename = saved;
2438 input_filename = saved_input_filename;
2439 force_error = 0;
2440 }
2441
2442 /* Issue an error message at a current source line CL */
2443
2444 void
2445 parse_error_context VPROTO ((tree cl, char *msg, ...))
2446 {
2447 #ifndef __STDC__
2448 tree cl;
2449 char *msg;
2450 #endif
2451 va_list ap;
2452
2453 VA_START (ap, msg);
2454 #ifndef __STDC__
2455 cl = va_arg (ap, tree);
2456 msg = va_arg (ap, char *);
2457 #endif
2458 issue_warning_error_from_context (cl, msg, ap);
2459 va_end (ap);
2460 }
2461
2462 /* Issue a warning at a current source line CL */
2463
2464 static void
2465 parse_warning_context VPROTO ((tree cl, char *msg, ...))
2466 {
2467 #ifndef __STDC__
2468 tree cl;
2469 char *msg;
2470 #endif
2471 va_list ap;
2472
2473 VA_START (ap, msg);
2474 #ifndef __STDC__
2475 cl = va_arg (ap, tree);
2476 msg = va_arg (ap, char *);
2477 #endif
2478
2479 force_error = do_warning = 1;
2480 issue_warning_error_from_context (cl, msg, ap);
2481 do_warning = force_error = 0;
2482 va_end (ap);
2483 }
2484
2485 static tree
2486 find_expr_with_wfl (node)
2487 tree node;
2488 {
2489 while (node)
2490 {
2491 char code;
2492 tree to_return;
2493
2494 switch (TREE_CODE (node))
2495 {
2496 case BLOCK:
2497 return find_expr_with_wfl (BLOCK_EXPR_BODY (node));
2498
2499 case COMPOUND_EXPR:
2500 to_return = find_expr_with_wfl (TREE_OPERAND (node, 0));
2501 if (to_return)
2502 return to_return;
2503 to_return = find_expr_with_wfl (TREE_OPERAND (node, 1));
2504 return to_return;
2505
2506 case LOOP_EXPR:
2507 return find_expr_with_wfl (TREE_OPERAND (node, 0));
2508
2509 case LABELED_BLOCK_EXPR:
2510 return find_expr_with_wfl (TREE_OPERAND (node, 1));
2511 default:
2512 code = TREE_CODE_CLASS (TREE_CODE (node));
2513 if (((code == '1') || (code == '2') || (code == 'e'))
2514 && EXPR_WFL_LINECOL (node))
2515 return node;
2516 }
2517 }
2518 return NULL_TREE;
2519 }
2520
2521 /* Issue a missing return statement error. Uses METHOD to figure the
2522 last line of the method the error occurs in. */
2523
2524 static void
2525 missing_return_error (method)
2526 tree method;
2527 {
2528 EXPR_WFL_SET_LINECOL (wfl_operator, DECL_SOURCE_LINE_LAST (method), -2);
2529 parse_error_context (wfl_operator, "Missing return statement");
2530 }
2531
2532 /* Issue an unreachable statement error. From NODE, find the next
2533 statement to report appropriately. */
2534 static void
2535 unreachable_stmt_error (node)
2536 tree node;
2537 {
2538 /* Browse node to find the next expression node that has a WFL. Use
2539 the location to report the error */
2540 if (TREE_CODE (node) == COMPOUND_EXPR)
2541 node = find_expr_with_wfl (TREE_OPERAND (node, 1));
2542 else
2543 node = find_expr_with_wfl (node);
2544
2545 if (node)
2546 {
2547 EXPR_WFL_SET_LINECOL (wfl_operator, EXPR_WFL_LINENO (node), -2);
2548 parse_error_context (wfl_operator, "Unreachable statement");
2549 }
2550 else
2551 fatal ("Can't get valid statement - unreachable_stmt_error");
2552 }
2553
2554 int
2555 java_report_errors ()
2556 {
2557 if (java_error_count)
2558 fprintf (stderr, "%d error%s",
2559 java_error_count, (java_error_count == 1 ? "" : "s"));
2560 if (java_warning_count)
2561 fprintf (stderr, "%s%d warning%s", (java_error_count ? ", " : ""),
2562 java_warning_count, (java_warning_count == 1 ? "" : "s"));
2563 if (java_error_count || java_warning_count)
2564 putc ('\n', stderr);
2565 return java_error_count;
2566 }
2567
2568 static char *
2569 java_accstring_lookup (flags)
2570 int flags;
2571 {
2572 static char buffer [80];
2573 #define COPY_RETURN(S) {strcpy (buffer, S); return buffer;}
2574
2575 /* Access modifier looked-up first for easier report on forbidden
2576 access. */
2577 if (flags & ACC_PUBLIC) COPY_RETURN ("public");
2578 if (flags & ACC_PRIVATE) COPY_RETURN ("private");
2579 if (flags & ACC_PROTECTED) COPY_RETURN ("protected");
2580 if (flags & ACC_STATIC) COPY_RETURN ("static");
2581 if (flags & ACC_FINAL) COPY_RETURN ("final");
2582 if (flags & ACC_SYNCHRONIZED) COPY_RETURN ("synchronized");
2583 if (flags & ACC_VOLATILE) COPY_RETURN ("volatile");
2584 if (flags & ACC_TRANSIENT) COPY_RETURN ("transient");
2585 if (flags & ACC_NATIVE) COPY_RETURN ("native");
2586 if (flags & ACC_INTERFACE) COPY_RETURN ("interface");
2587 if (flags & ACC_ABSTRACT) COPY_RETURN ("abstract");
2588
2589 buffer [0] = '\0';
2590 return buffer;
2591 #undef COPY_RETURN
2592 }
2593
2594 /* Issuing error messages upon redefinition of classes, interfaces or
2595 variables. */
2596
2597 static void
2598 classitf_redefinition_error (context, id, decl, cl)
2599 char *context;
2600 tree id, decl, cl;
2601 {
2602 parse_error_context (cl, "%s `%s' already defined in %s:%d",
2603 context, IDENTIFIER_POINTER (id),
2604 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
2605 /* Here we should point out where its redefined. It's a unicode. FIXME */
2606 }
2607
2608 static void
2609 variable_redefinition_error (context, name, type, line)
2610 tree context, name, type;
2611 int line;
2612 {
2613 char *type_name;
2614
2615 /* Figure a proper name for type. We might haven't resolved it */
2616 if (TREE_CODE (type) == POINTER_TYPE && !TREE_TYPE (type))
2617 type_name = IDENTIFIER_POINTER (TYPE_NAME (type));
2618 else
2619 type_name = lang_printable_name (type, 0);
2620
2621 parse_error_context (context,
2622 "Variable `%s' is already defined in this method and "
2623 "was declared `%s %s' at line %d",
2624 IDENTIFIER_POINTER (name),
2625 type_name, IDENTIFIER_POINTER (name), line);
2626 }
2627
2628 static tree
2629 build_array_from_name (type, type_wfl, name, ret_name)
2630 tree type, type_wfl, name, *ret_name;
2631 {
2632 int more_dims = 0;
2633 char *string;
2634
2635 /* Eventually get more dims */
2636 string = IDENTIFIER_POINTER (name);
2637 while (string [more_dims] == '[')
2638 more_dims++;
2639
2640 /* If we have, then craft a new type for this variable */
2641 if (more_dims)
2642 {
2643 name = get_identifier (&more_dims [string]);
2644
2645 /* If type already is a reference on an array, get the base type */
2646 if ((TREE_CODE (type) == POINTER_TYPE) &&
2647 TYPE_ARRAY_P (TREE_TYPE (type)))
2648 type = TREE_TYPE (type);
2649
2650 /* Building the first dimension of a primitive type uses this
2651 function */
2652 if (JPRIMITIVE_TYPE_P (type))
2653 {
2654 type = build_java_array_type (type, -1);
2655 CLASS_LOADED_P (type) = 1;
2656 more_dims--;
2657 }
2658 /* Otherwise, if we have a WFL for this type, use it (the type
2659 is already an array on an unresolved type, and we just keep
2660 on adding dimensions) */
2661 else if (type_wfl)
2662 type = type_wfl;
2663
2664 /* Add all the dimensions */
2665 while (more_dims--)
2666 type = build_unresolved_array_type (type);
2667
2668 /* The type may have been incomplete in the first place */
2669 if (type_wfl)
2670 type = obtain_incomplete_type (type);
2671 }
2672
2673 *ret_name = name;
2674 return type;
2675 }
2676
2677 /* Build something that the type identifier resolver will identify as
2678 being an array to an unresolved type. TYPE_WFL is a WFL on a
2679 identifier. */
2680
2681 static tree
2682 build_unresolved_array_type (type_or_wfl)
2683 tree type_or_wfl;
2684 {
2685 char *ptr;
2686
2687 /* TYPE_OR_WFL might be an array on a resolved type. In this case,
2688 just create a array type */
2689 if (TREE_CODE (type_or_wfl) == RECORD_TYPE)
2690 {
2691 tree type = build_java_array_type (type_or_wfl, -1);
2692 CLASS_LOADED_P (type) = CLASS_LOADED_P (type_or_wfl);
2693 return type;
2694 }
2695
2696 obstack_1grow (&temporary_obstack, '[');
2697 obstack_grow0 (&temporary_obstack,
2698 IDENTIFIER_POINTER (EXPR_WFL_NODE (type_or_wfl)),
2699 IDENTIFIER_LENGTH (EXPR_WFL_NODE (type_or_wfl)));
2700 ptr = obstack_finish (&temporary_obstack);
2701 return build_expr_wfl (get_identifier (ptr),
2702 EXPR_WFL_FILENAME (type_or_wfl),
2703 EXPR_WFL_LINENO (type_or_wfl),
2704 EXPR_WFL_COLNO (type_or_wfl));
2705 }
2706
2707 /* Check modifiers. If one doesn't fit, retrieve it in its declaration line
2708 and point it out. */
2709
2710 static void
2711 check_modifiers (message, value, mask)
2712 char *message;
2713 int value;
2714 int mask;
2715 {
2716 /* Should point out the one that don't fit. ASCII/unicode,
2717 going backward. FIXME */
2718 if (value & ~mask)
2719 {
2720 int i, remainder = value & ~mask;
2721 for (i = 0; i <= 10; i++)
2722 if ((1 << i) & remainder)
2723 parse_error_context (ctxp->modifier_ctx [i], message,
2724 java_accstring_lookup (1 << i));
2725 }
2726 }
2727
2728 static void
2729 parser_add_interface (class_decl, interface_decl, wfl)
2730 tree class_decl, interface_decl, wfl;
2731 {
2732 if (maybe_add_interface (TREE_TYPE (class_decl), TREE_TYPE (interface_decl)))
2733 parse_error_context (wfl, "Interface `%s' repeated",
2734 IDENTIFIER_POINTER (DECL_NAME (interface_decl)));
2735 }
2736
2737 /* Bulk of common class/interface checks. Return 1 if an error was
2738 encountered. TAG is 0 for a class, 1 for an interface. */
2739
2740 static int
2741 check_class_interface_creation (is_interface, flags, raw_name, qualified_name, decl, cl)
2742 int is_interface, flags;
2743 tree raw_name, qualified_name, decl, cl;
2744 {
2745 tree node;
2746
2747 if (!quiet_flag)
2748 fprintf (stderr, " %s %s", (is_interface ? "interface" : "class"),
2749 IDENTIFIER_POINTER (qualified_name));
2750
2751 /* Scope of an interface/class type name:
2752 - Can't be imported by a single type import
2753 - Can't already exists in the package */
2754 if (IS_A_SINGLE_IMPORT_CLASSFILE_NAME_P (raw_name)
2755 && (node = find_name_in_single_imports (raw_name)))
2756 {
2757 parse_error_context
2758 (cl, "%s name `%s' clashes with imported type `%s'",
2759 (is_interface ? "Interface" : "Class"),
2760 IDENTIFIER_POINTER (raw_name), IDENTIFIER_POINTER (node));
2761 return 1;
2762 }
2763 if (decl && CLASS_COMPLETE_P (decl))
2764 {
2765 classitf_redefinition_error ((is_interface ? "Interface" : "Class"),
2766 qualified_name, decl, cl);
2767 return 1;
2768 }
2769
2770 /* If public, file name should match class/interface name */
2771 if (flags & ACC_PUBLIC)
2772 {
2773 char *f;
2774
2775 /* Contains OS dependent assumption on path separator. FIXME */
2776 for (f = &input_filename [strlen (input_filename)];
2777 f != input_filename && f[0] != '/' && f[0] != DIR_SEPARATOR;
2778 f--)
2779 ;
2780 if (f[0] == '/' || f[0] == DIR_SEPARATOR)
2781 f++;
2782 if (strncmp (IDENTIFIER_POINTER (raw_name),
2783 f , IDENTIFIER_LENGTH (raw_name)) ||
2784 f [IDENTIFIER_LENGTH (raw_name)] != '.')
2785 parse_error_context (cl, "Public %s `%s' must be defined in a file "
2786 "called `%s.java'",
2787 (is_interface ? "interface" : "class"),
2788 IDENTIFIER_POINTER (qualified_name),
2789 IDENTIFIER_POINTER (raw_name));
2790 }
2791
2792 check_modifiers ((is_interface ?
2793 "Illegal modifier `%s' for interface declaration" :
2794 "Illegal modifier `%s' for class declaration"), flags,
2795 (is_interface ? INTERFACE_MODIFIERS : CLASS_MODIFIERS));
2796 return 0;
2797 }
2798
2799 /* If DECL is NULL, create and push a new DECL, record the current
2800 line CL and do other maintenance things. */
2801
2802 static tree
2803 maybe_create_class_interface_decl (decl, qualified_name, cl)
2804 tree decl, qualified_name, cl;
2805 {
2806 if (!decl)
2807 decl = push_class (make_class (), qualified_name);
2808
2809 /* Take care of the file and line business */
2810 DECL_SOURCE_FILE (decl) = EXPR_WFL_FILENAME (cl);
2811 DECL_SOURCE_LINE (decl) = EXPR_WFL_LINENO (cl);
2812 CLASS_FROM_SOURCE_P (TREE_TYPE (decl)) = 1;
2813 CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (TREE_TYPE (decl)) =
2814 IS_A_COMMAND_LINE_FILENAME_P (EXPR_WFL_FILENAME_NODE (cl));
2815
2816 ctxp->current_parsed_class = decl;
2817
2818 /* Link the declaration to the already seen ones */
2819 TREE_CHAIN (decl) = ctxp->class_list;
2820 ctxp->class_list = decl;
2821
2822 /* Create a new nodes in the global lists */
2823 ctxp->gclass_list = tree_cons (NULL_TREE, decl, ctxp->gclass_list);
2824 all_class_list = tree_cons (NULL_TREE, decl, all_class_list);
2825
2826 /* Install a new dependency list element */
2827 create_jdep_list (ctxp);
2828
2829 SOURCE_FRONTEND_DEBUG (("Defining class/interface %s",
2830 IDENTIFIER_POINTER (qualified_name)));
2831 return decl;
2832 }
2833
2834 static void
2835 add_superinterfaces (decl, interface_list)
2836 tree decl, interface_list;
2837 {
2838 tree node;
2839 /* Superinterface(s): if present and defined, parser_check_super_interface ()
2840 takes care of ensuring that:
2841 - This is an accessible interface type,
2842 - Circularity detection.
2843 parser_add_interface is then called. If present but not defined,
2844 the check operation is delayed until the super interface gets
2845 defined. */
2846 for (node = interface_list; node; node = TREE_CHAIN (node))
2847 {
2848 tree current = TREE_PURPOSE (node);
2849 tree idecl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (current));
2850 if (idecl && CLASS_LOADED_P (TREE_TYPE (idecl)))
2851 {
2852 if (!parser_check_super_interface (idecl, decl, current))
2853 parser_add_interface (decl, idecl, current);
2854 }
2855 else
2856 register_incomplete_type (JDEP_INTERFACE,
2857 current, decl, NULL_TREE);
2858 }
2859 }
2860
2861 /* Create an interface in pass1 and return its decl. Return the
2862 interface's decl in pass 2. */
2863
2864 static tree
2865 create_interface (flags, id, super)
2866 int flags;
2867 tree id, super;
2868 {
2869 tree raw_name = EXPR_WFL_NODE (id);
2870 tree q_name = parser_qualified_classname (id);
2871 tree decl = IDENTIFIER_CLASS_VALUE (q_name);
2872
2873 EXPR_WFL_NODE (id) = q_name; /* Keep source location, even if refined. */
2874
2875 /* Basic checks: scope, redefinition, modifiers */
2876 if (check_class_interface_creation (1, flags, raw_name, q_name, decl, id))
2877 return NULL_TREE;
2878
2879 /* Interface modifiers check
2880 - public/abstract allowed (already done at that point)
2881 - abstract is obsolete (comes first, it's a warning, or should be)
2882 - Can't use twice the same (checked in the modifier rule) */
2883 if ((flags & ACC_ABSTRACT) && flag_redundant)
2884 parse_warning_context
2885 (MODIFIER_WFL (ABSTRACT_TK),
2886 "Redundant use of `abstract' modifier. Interface `%s' is implicitely "
2887 "abstract", IDENTIFIER_POINTER (raw_name));
2888
2889 /* Create a new decl if DECL is NULL, otherwise fix it */
2890 decl = maybe_create_class_interface_decl (decl, q_name, id);
2891
2892 /* Set super info and mark the class a complete */
2893 set_super_info (ACC_ABSTRACT | ACC_INTERFACE | flags, TREE_TYPE (decl),
2894 object_type_node, ctxp->interface_number);
2895 ctxp->interface_number = 0;
2896 CLASS_COMPLETE_P (decl) = 1;
2897 add_superinterfaces (decl, super);
2898
2899 return decl;
2900 }
2901
2902 /* Create an class in pass1 and return its decl. Return class
2903 interface's decl in pass 2. */
2904
2905 static tree
2906 create_class (flags, id, super, interfaces)
2907 int flags;
2908 tree id, super, interfaces;
2909 {
2910 tree raw_name = EXPR_WFL_NODE (id);
2911 tree class_id, decl;
2912 tree super_decl = NULL, super_decl_type;
2913
2914 class_id = parser_qualified_classname (id);
2915 decl = IDENTIFIER_CLASS_VALUE (class_id);
2916 ctxp->current_parsed_class_un = EXPR_WFL_NODE (id);
2917 EXPR_WFL_NODE (id) = class_id;
2918
2919 /* Basic check: scope, redefinition, modifiers */
2920 if (check_class_interface_creation (0, flags, raw_name, class_id, decl, id))
2921 return NULL_TREE;
2922
2923 /* Class modifier check:
2924 - Allowed modifier (already done at that point)
2925 - abstract AND final forbidden
2926 - Public classes defined in the correct file */
2927 if ((flags & ACC_ABSTRACT) && (flags & ACC_FINAL))
2928 parse_error_context (id, "Class `%s' can't be declared both abstract "
2929 "and final", IDENTIFIER_POINTER (raw_name));
2930
2931 /* Create a new decl if DECL is NULL, otherwise fix it */
2932 decl = maybe_create_class_interface_decl (decl, class_id, id);
2933
2934 /* If SUPER exists, use it, otherwise use Object */
2935 if (super)
2936 {
2937 /* Can't extend java.lang.Object */
2938 if (TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_id)) == object_type_node)
2939 {
2940 parse_error_context (id, "Can't extend `java.lang.Object'");
2941 return NULL_TREE;
2942 }
2943
2944 /* The class is known and exists if there is a decl. Otherwise,
2945 postpone the operation and do it later. */
2946 super_decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (super));
2947 if (super_decl)
2948 {
2949 parser_check_super (super_decl, decl, id);
2950 super_decl_type = TREE_TYPE (super_decl);
2951 }
2952 else
2953 super_decl_type =
2954 register_incomplete_type (JDEP_SUPER, super, decl, NULL_TREE);
2955 }
2956 else if (TREE_TYPE (decl) != object_type_node)
2957 super_decl_type = object_type_node;
2958 /* We're defining java.lang.Object */
2959 else
2960 super_decl_type = NULL_TREE;
2961
2962 /* Set super info and mark the class a complete */
2963 set_super_info (flags, TREE_TYPE (decl), super_decl_type,
2964 ctxp->interface_number);
2965 ctxp->interface_number = 0;
2966 CLASS_COMPLETE_P (decl) = 1;
2967 add_superinterfaces (decl, interfaces);
2968
2969 /* Eventually sets the @deprecated tag flag */
2970 CHECK_DEPRECATED (decl);
2971
2972 return decl;
2973 }
2974
2975 /* Can't use lookup_field () since we don't want to load the class and
2976 can't set the CLASS_LOADED_P flag */
2977
2978 static tree
2979 find_field (class, name)
2980 tree class;
2981 tree name;
2982 {
2983 tree decl;
2984 for (decl = TYPE_FIELDS (class); decl; decl = TREE_CHAIN (decl))
2985 {
2986 if (DECL_NAME (decl) == name)
2987 return decl;
2988 }
2989 return NULL_TREE;
2990 }
2991
2992 /* Wrap around lookup_field that doesn't potentially upset the value
2993 of CLASS */
2994
2995 static tree
2996 lookup_field_wrapper (class, name)
2997 tree class, name;
2998 {
2999 tree type = class;
3000 tree decl;
3001 java_parser_context_save_global ();
3002 decl = lookup_field (&type, name);
3003 java_parser_context_restore_global ();
3004 return decl;
3005 }
3006
3007 /* Find duplicate field within the same class declarations and report
3008 the error. Returns 1 if a duplicated field was found, 0
3009 otherwise. */
3010
3011 static int
3012 duplicate_declaration_error_p (new_field_name, new_type, cl)
3013 tree new_field_name, new_type, cl;
3014 {
3015 /* This might be modified to work with method decl as well */
3016 tree decl = find_field (TREE_TYPE (ctxp->current_parsed_class),
3017 new_field_name);
3018 if (decl)
3019 {
3020 char *t1 = strdup (purify_type_name
3021 ((TREE_CODE (new_type) == POINTER_TYPE
3022 && TREE_TYPE (new_type) == NULL_TREE) ?
3023 IDENTIFIER_POINTER (TYPE_NAME (new_type)) :
3024 lang_printable_name (new_type, 1)));
3025 /* The type may not have been completed by the time we report
3026 the error */
3027 char *t2 = strdup (purify_type_name
3028 ((TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE
3029 && TREE_TYPE (TREE_TYPE (decl)) == NULL_TREE) ?
3030 IDENTIFIER_POINTER (TYPE_NAME (TREE_TYPE (decl))) :
3031 lang_printable_name (TREE_TYPE (decl), 1)));
3032 parse_error_context
3033 (cl , "Duplicate variable declaration: `%s %s' was `%s %s' (%s:%d)",
3034 t1, IDENTIFIER_POINTER (new_field_name),
3035 t2, IDENTIFIER_POINTER (DECL_NAME (decl)),
3036 DECL_SOURCE_FILE (decl), DECL_SOURCE_LINE (decl));
3037 free (t1);
3038 free (t2);
3039 return 1;
3040 }
3041 return 0;
3042 }
3043
3044 /* Field registration routine. If TYPE doesn't exist, field
3045 declarations are linked to the undefined TYPE dependency list, to
3046 be later resolved in java_complete_class () */
3047
3048 static void
3049 register_fields (flags, type, variable_list)
3050 int flags;
3051 tree type, variable_list;
3052 {
3053 tree current, saved_type;
3054 tree class_type = TREE_TYPE (ctxp->current_parsed_class);
3055 int saved_lineno = lineno;
3056 int must_chain = 0;
3057 tree wfl = NULL_TREE;
3058
3059 /* If we're adding fields to interfaces, those fields are public,
3060 static, final */
3061 if (CLASS_INTERFACE (TYPE_NAME (class_type)))
3062 {
3063 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK),
3064 flags, ACC_PUBLIC,
3065 "%s", "interface field(s)");
3066 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (STATIC_TK),
3067 flags, ACC_STATIC,
3068 "%s", "interface field(s)");
3069 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (FINAL_TK),
3070 flags, ACC_FINAL, "%s", "interface field(s)");
3071 check_modifiers ("Illegal interface member modifier `%s'", flags,
3072 INTERFACE_FIELD_MODIFIERS);
3073 flags |= (ACC_PUBLIC | ACC_STATIC | ACC_FINAL);
3074 }
3075
3076 /* Obtain a suitable type for resolution, if necessary */
3077 SET_TYPE_FOR_RESOLUTION (type, wfl, must_chain);
3078
3079 /* If TYPE is fully resolved and we don't have a reference, make one */
3080 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3081
3082 for (current = variable_list, saved_type = type; current;
3083 current = TREE_CHAIN (current), type = saved_type)
3084 {
3085 tree real_type;
3086 tree field_decl;
3087 tree cl = TREE_PURPOSE (current);
3088 tree init = TREE_VALUE (current);
3089 tree current_name = EXPR_WFL_NODE (cl);
3090
3091 /* Process NAME, as it may specify extra dimension(s) for it */
3092 type = build_array_from_name (type, wfl, current_name, &current_name);
3093
3094 /* Type adjustment. We may have just readjusted TYPE because
3095 the variable specified more dimensions. Make sure we have
3096 a reference if we can and don't have one already. Also
3097 change the name if we have an init. */
3098 if (type != saved_type)
3099 {
3100 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3101 if (init)
3102 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = current_name;
3103 }
3104
3105 real_type = GET_REAL_TYPE (type);
3106 /* Check for redeclarations */
3107 if (duplicate_declaration_error_p (current_name, real_type, cl))
3108 continue;
3109
3110 /* Set lineno to the line the field was found and create a
3111 declaration for it. Eventually sets the @deprecated tag flag. */
3112 lineno = EXPR_WFL_LINENO (cl);
3113 field_decl = add_field (class_type, current_name, real_type, flags);
3114 CHECK_DEPRECATED (field_decl);
3115
3116 /* Check if we must chain. */
3117 if (must_chain)
3118 register_incomplete_type (JDEP_FIELD, wfl, field_decl, type);
3119
3120 /* Default value of a static field is 0 and it is considered
3121 initialized. */
3122 if (flags & ACC_STATIC)
3123 INITIALIZED_P (field_decl) = 1;
3124
3125 /* If we have an initialization value tied to the field */
3126 if (init)
3127 {
3128 /* The field is declared static */
3129 if (flags & ACC_STATIC)
3130 {
3131 /* We include the field and its initialization part into
3132 a list used to generate <clinit>. After <clinit> is
3133 walked, fields initialization will be processed and
3134 fields initialized with know constants will be taken
3135 out of <clinit> and have ther DECL_INITIAL set
3136 appropriately. */
3137 TREE_CHAIN (init) = ctxp->static_initialized;
3138 ctxp->static_initialized = init;
3139 DECL_INITIAL (field_decl) = TREE_OPERAND (init, 1);
3140 }
3141 /* A non-static field declared with an immediate initialization is
3142 to be initialized in <init>, if any. This field is remembered
3143 to be processed at the time of the generation of <init>. */
3144 else
3145 {
3146 TREE_CHAIN (init) = ctxp->non_static_initialized;
3147 ctxp->non_static_initialized = init;
3148 }
3149 INITIALIZED_P (field_decl) = 1;
3150 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
3151 }
3152 }
3153 lineno = saved_lineno;
3154 }
3155
3156 /* Generate the method $finit$ that initializes fields initialized
3157 upon declaration. */
3158
3159 static void
3160 maybe_generate_finit ()
3161 {
3162 tree mdecl, current;
3163
3164 if (!ctxp->non_static_initialized || java_error_count)
3165 return;
3166
3167 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3168 ACC_PRIVATE, void_type_node,
3169 finit_identifier_node, end_params_node);
3170 start_artificial_method_body (mdecl);
3171
3172 ctxp->non_static_initialized = nreverse (ctxp->non_static_initialized);
3173 for (current = ctxp->non_static_initialized; current;
3174 current = TREE_CHAIN (current))
3175 java_method_add_stmt (mdecl,
3176 build_debugable_stmt (EXPR_WFL_LINECOL (current),
3177 current));
3178
3179 end_artificial_method_body (mdecl);
3180 CLASS_HAS_FINIT_P (TREE_TYPE (ctxp->current_parsed_class)) = 1;
3181 ctxp->non_static_initialized = NULL_TREE;
3182 }
3183
3184 /* Check whether it is necessary to generate a <clinit> for the class
3185 we just parsed. */
3186
3187 static void
3188 maybe_generate_clinit ()
3189 {
3190 tree mdecl, c;
3191
3192 if (!ctxp->static_initialized || java_error_count)
3193 return;
3194
3195 mdecl = create_artificial_method (TREE_TYPE (ctxp->current_parsed_class),
3196 ACC_STATIC, void_type_node,
3197 clinit_identifier_node, end_params_node);
3198 start_artificial_method_body (mdecl);
3199
3200 /* Keep initialization in order to enforce 8.5 */
3201 ctxp->static_initialized = nreverse (ctxp->static_initialized);
3202
3203 /* We process the list of assignment we produced as the result of
3204 the declaration of initialized static field and add them as
3205 statement to the <clinit> method. */
3206 for (c = ctxp->static_initialized; c; c = TREE_CHAIN (c))
3207 {
3208 /* We build the assignment expression that will initialize the
3209 field to its value. There are strict rules on static
3210 initializers (8.5). FIXME */
3211 java_method_add_stmt (mdecl,
3212 build_debugable_stmt (EXPR_WFL_LINECOL (c), c));
3213 }
3214
3215 end_artificial_method_body (mdecl);
3216 ctxp->static_initialized = NULL_TREE;
3217 }
3218
3219 /* Shared accros method_declarator and method_header to remember the
3220 patch stage that was reached during the declaration of the method.
3221 A method DECL is built differently is there is no patch
3222 (JDEP_NO_PATCH) or a patch (JDEP_METHOD or JDEP_METHOD_RETURN)
3223 pending on the currently defined method. */
3224
3225 static int patch_stage;
3226
3227 /* Check the method declaration and add the method to its current
3228 class. If the argument list is known to contain incomplete types,
3229 the method is partially added and the registration will be resume
3230 once the method arguments resolved. If TYPE is NULL, we're dealing
3231 with a constructor. */
3232
3233 static tree
3234 method_header (flags, type, mdecl, throws)
3235 int flags;
3236 tree type, mdecl, throws;
3237 {
3238 tree meth = TREE_VALUE (mdecl);
3239 tree id = TREE_PURPOSE (mdecl);
3240 tree this_class = TREE_TYPE (ctxp->current_parsed_class);
3241 tree type_wfl = NULL_TREE;
3242 tree meth_name = NULL_TREE, current, orig_arg;
3243 int saved_lineno;
3244 int constructor_ok = 0, must_chain;
3245
3246 check_modifiers_consistency (flags);
3247
3248 /* There are some forbidden modifiers for an abstract method and its
3249 class must be abstract as well. */
3250 if (type && (flags & ACC_ABSTRACT))
3251 {
3252 ABSTRACT_CHECK (flags, ACC_PRIVATE, id, "Private");
3253 ABSTRACT_CHECK (flags, ACC_STATIC, id, "Static");
3254 ABSTRACT_CHECK (flags, ACC_FINAL, id, "Final");
3255 ABSTRACT_CHECK (flags, ACC_NATIVE, id, "Native");
3256 ABSTRACT_CHECK (flags, ACC_SYNCHRONIZED,id, "Synchronized");
3257 if (!CLASS_ABSTRACT (TYPE_NAME (this_class)))
3258 parse_error_context
3259 (id, "Class `%s' must be declared abstract to define abstract "
3260 "method `%s'",
3261 IDENTIFIER_POINTER (DECL_NAME (ctxp->current_parsed_class)),
3262 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3263 }
3264 /* Things to be checked when declaring a constructor */
3265 if (!type)
3266 {
3267 int ec = java_error_count;
3268 /* 8.6: Constructor declarations: we might be trying to define a
3269 method without specifying a return type. */
3270 if (EXPR_WFL_NODE (id) != ctxp->current_parsed_class_un)
3271 parse_error_context
3272 (id, "Invalid method declaration, return type required");
3273 /* 8.6.3: Constructor modifiers */
3274 else
3275 {
3276 JCONSTRUCTOR_CHECK (flags, ACC_ABSTRACT, id, "abstract");
3277 JCONSTRUCTOR_CHECK (flags, ACC_STATIC, id, "static");
3278 JCONSTRUCTOR_CHECK (flags, ACC_FINAL, id, "final");
3279 JCONSTRUCTOR_CHECK (flags, ACC_NATIVE, id, "native");
3280 JCONSTRUCTOR_CHECK (flags, ACC_SYNCHRONIZED, id, "synchronized");
3281 }
3282 /* If we found error here, we don't consider it's OK to tread
3283 the method definition as a constructor, for the rest of this
3284 function */
3285 if (ec == java_error_count)
3286 constructor_ok = 1;
3287 }
3288
3289 /* Method declared within the scope of an interface are implicitly
3290 abstract and public. Conflicts with other erroneously provided
3291 modifiers are check right after. */
3292
3293 if (CLASS_INTERFACE (TYPE_NAME (this_class)))
3294 {
3295 /* If FLAGS isn't set because of a modifier, turn the
3296 corresponding modifier WFL to NULL so we issue a warning on
3297 the obsolete use of the modifier */
3298 if (!(flags & ACC_PUBLIC))
3299 MODIFIER_WFL (PUBLIC_TK) = NULL;
3300 if (!(flags & ACC_ABSTRACT))
3301 MODIFIER_WFL (ABSTRACT_TK) = NULL;
3302 flags |= ACC_PUBLIC;
3303 flags |= ACC_ABSTRACT;
3304 }
3305
3306 /* Modifiers context reset moved up, so abstract method declaration
3307 modifiers can be later checked. */
3308
3309 /* Set constructor returned type to void and method name to <init>,
3310 unless we found an error identifier the constructor (in which
3311 case we retain the original name) */
3312 if (!type)
3313 {
3314 type = void_type_node;
3315 if (constructor_ok)
3316 meth_name = init_identifier_node;
3317 }
3318 else
3319 meth_name = EXPR_WFL_NODE (id);
3320
3321 /* Do the returned type resolution and registration if necessary */
3322 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3323
3324 if (meth_name)
3325 type = build_array_from_name (type, type_wfl, meth_name, &meth_name);
3326 EXPR_WFL_NODE (id) = meth_name;
3327 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
3328
3329 if (must_chain)
3330 {
3331 patch_stage = JDEP_METHOD_RETURN;
3332 register_incomplete_type (patch_stage, type_wfl, id, type);
3333 TREE_TYPE (meth) = GET_REAL_TYPE (type);
3334 }
3335 else
3336 TREE_TYPE (meth) = type;
3337
3338 saved_lineno = lineno;
3339 /* When defining an abstract or interface method, the curly
3340 bracket at level 1 doesn't exist because there is no function
3341 body */
3342 lineno = (ctxp->first_ccb_indent1 ? ctxp->first_ccb_indent1 :
3343 EXPR_WFL_LINENO (id));
3344
3345 /* Remember the original argument list */
3346 orig_arg = TYPE_ARG_TYPES (meth);
3347
3348 if (patch_stage) /* includes ret type and/or all args */
3349 {
3350 jdep *jdep;
3351 meth = add_method_1 (this_class, flags, meth_name, meth);
3352 /* Patch for the return type */
3353 if (patch_stage == JDEP_METHOD_RETURN)
3354 {
3355 jdep = CLASSD_LAST (ctxp->classd_list);
3356 JDEP_GET_PATCH (jdep) = &TREE_TYPE (TREE_TYPE (meth));
3357 }
3358 /* This is the stop JDEP. METH allows the function's signature
3359 to be computed. */
3360 register_incomplete_type (JDEP_METHOD_END, NULL_TREE, meth, NULL_TREE);
3361 }
3362 else
3363 meth = add_method (this_class, flags, meth_name,
3364 build_java_signature (meth));
3365
3366 /* Fix the method argument list so we have the argument name
3367 information */
3368 fix_method_argument_names (orig_arg, meth);
3369
3370 /* Register the parameter number and re-install the current line
3371 number */
3372 DECL_MAX_LOCALS (meth) = ctxp->formal_parameter_number+1;
3373 lineno = saved_lineno;
3374
3375 /* Register exception specified by the `throws' keyword for
3376 resolution and set the method decl appropriate field to the list.
3377 Note: the grammar ensures that what we get here are class
3378 types. */
3379 if (throws)
3380 {
3381 throws = nreverse (throws);
3382 for (current = throws; current; current = TREE_CHAIN (current))
3383 {
3384 register_incomplete_type (JDEP_EXCEPTION, TREE_VALUE (current),
3385 NULL_TREE, NULL_TREE);
3386 JDEP_GET_PATCH (CLASSD_LAST (ctxp->classd_list)) =
3387 &TREE_VALUE (current);
3388 }
3389 DECL_FUNCTION_THROWS (meth) = throws;
3390 }
3391
3392 /* We set the DECL_NAME to ID so we can track the location where
3393 the function was declared. This allow us to report
3394 redefinition error accurately. When method are verified,
3395 DECL_NAME is reinstalled properly (using the content of the
3396 WFL node ID) (see check_method_redefinition). We don't do that
3397 when Object is being defined. Constructor <init> names will be
3398 reinstalled the same way. */
3399 if (TREE_TYPE (ctxp->current_parsed_class) != object_type_node)
3400 DECL_NAME (meth) = id;
3401
3402 /* Set the flag if we correctly processed a constructor */
3403 if (constructor_ok)
3404 DECL_CONSTRUCTOR_P (meth) = 1;
3405
3406 /* Eventually set the @deprecated tag flag */
3407 CHECK_DEPRECATED (meth);
3408
3409 return meth;
3410 }
3411
3412 static void
3413 fix_method_argument_names (orig_arg, meth)
3414 tree orig_arg, meth;
3415 {
3416 tree arg = TYPE_ARG_TYPES (TREE_TYPE (meth));
3417 if (TREE_CODE (TREE_TYPE (meth)) == METHOD_TYPE)
3418 {
3419 TREE_PURPOSE (arg) = this_identifier_node;
3420 arg = TREE_CHAIN (arg);
3421 }
3422 while (orig_arg != end_params_node)
3423 {
3424 TREE_PURPOSE (arg) = TREE_PURPOSE (orig_arg);
3425 orig_arg = TREE_CHAIN (orig_arg);
3426 arg = TREE_CHAIN (arg);
3427 }
3428 }
3429
3430 /* Complete the method declaration with METHOD_BODY. */
3431
3432 static void
3433 complete_method_declaration (method_body)
3434 tree method_body;
3435 {
3436 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)) = method_body;
3437 maybe_absorb_scoping_blocks ();
3438 /* Exit function's body */
3439 exit_block ();
3440 /* Merge last line of the function with first line, directly in the
3441 function decl. It will be used to emit correct debug info. */
3442 DECL_SOURCE_LINE_MERGE (current_function_decl, ctxp->last_ccb_indent1);
3443 }
3444
3445 /* Build a an error message for constructor circularity errors. */
3446
3447 static char *
3448 constructor_circularity_msg (from, to)
3449 tree from, to;
3450 {
3451 static char string [4096];
3452 char *t = strdup (lang_printable_name (from, 0));
3453 sprintf (string, "`%s' invokes `%s'", t, lang_printable_name (to, 0));
3454 free (t);
3455 return string;
3456 }
3457
3458 /* Verify a circular call to METH. Return 1 if an error is found, 0
3459 otherwise. */
3460
3461 static int
3462 verify_constructor_circularity (meth, current)
3463 tree meth, current;
3464 {
3465 static tree list = NULL_TREE;
3466 tree c;
3467 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3468 {
3469 if (TREE_VALUE (c) == meth)
3470 {
3471 char *t;
3472 if (list)
3473 {
3474 tree liste;
3475 list = nreverse (list);
3476 for (liste = list; liste; liste = TREE_CHAIN (liste))
3477 {
3478 parse_error_context
3479 (TREE_PURPOSE (TREE_PURPOSE (liste)),
3480 constructor_circularity_msg
3481 (TREE_VALUE (liste), TREE_VALUE (TREE_PURPOSE (liste))));
3482 java_error_count--;
3483 }
3484 }
3485 t = strdup (lang_printable_name (meth, 0));
3486 parse_error_context (TREE_PURPOSE (c),
3487 "%s: recursive invocation of constructor `%s'",
3488 constructor_circularity_msg (current, meth), t);
3489 free (t);
3490 list = NULL_TREE;
3491 return 1;
3492 }
3493 }
3494 for (c = DECL_CONSTRUCTOR_CALLS (current); c; c = TREE_CHAIN (c))
3495 {
3496 list = tree_cons (c, current, list);
3497 if (verify_constructor_circularity (meth, TREE_VALUE (c)))
3498 return 1;
3499 list = TREE_CHAIN (list);
3500 }
3501 return 0;
3502 }
3503
3504 /* Check modifiers that can be declared but exclusively */
3505
3506 static void
3507 check_modifiers_consistency (flags)
3508 int flags;
3509 {
3510 int acc_count = 0;
3511 tree cl = NULL_TREE;
3512
3513 THIS_MODIFIER_ONLY (flags, ACC_PUBLIC, 0, acc_count, cl);
3514 THIS_MODIFIER_ONLY (flags, ACC_PRIVATE, 1, acc_count, cl);
3515 THIS_MODIFIER_ONLY (flags, ACC_PROTECTED, 2, acc_count, cl);
3516 if (acc_count > 1)
3517 parse_error_context
3518 (cl, "Inconsistent member declaration. At most one of `public', "
3519 "`private', or `protected' may be specified");
3520 }
3521
3522 /* Check the methode header METH for abstract specifics features */
3523
3524 static void
3525 check_abstract_method_header (meth)
3526 tree meth;
3527 {
3528 int flags = get_access_flags_from_decl (meth);
3529 /* DECL_NAME might still be a WFL node */
3530 tree name = GET_METHOD_NAME (meth);
3531
3532 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (ABSTRACT_TK), flags,
3533 ACC_ABSTRACT, "abstract method `%s'",
3534 IDENTIFIER_POINTER (name));
3535 OBSOLETE_MODIFIER_WARNING (MODIFIER_WFL (PUBLIC_TK), flags,
3536 ACC_PUBLIC, "abstract method `%s'",
3537 IDENTIFIER_POINTER (name));
3538
3539 check_modifiers ("Illegal modifier `%s' for interface method",
3540 flags, INTERFACE_METHOD_MODIFIERS);
3541 }
3542
3543 /* Create a FUNCTION_TYPE node and start augmenting it with the
3544 declared function arguments. Arguments type that can't be resolved
3545 are left as they are, but the returned node is marked as containing
3546 incomplete types. */
3547
3548 static tree
3549 method_declarator (id, list)
3550 tree id, list;
3551 {
3552 tree arg_types = NULL_TREE, current, node;
3553 tree meth = make_node (FUNCTION_TYPE);
3554 jdep *jdep;
3555
3556 patch_stage = JDEP_NO_PATCH;
3557
3558 for (current = list; current; current = TREE_CHAIN (current))
3559 {
3560 int must_chain = 0;
3561 tree wfl_name = TREE_PURPOSE (current);
3562 tree type = TREE_VALUE (current);
3563 tree name = EXPR_WFL_NODE (wfl_name);
3564 tree already, arg_node;
3565 tree type_wfl = NULL_TREE;
3566 tree real_type;
3567
3568 /* Obtain a suitable type for resolution, if necessary */
3569 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
3570
3571 /* Process NAME, as it may specify extra dimension(s) for it */
3572 type = build_array_from_name (type, type_wfl, name, &name);
3573 EXPR_WFL_NODE (wfl_name) = name;
3574
3575 real_type = GET_REAL_TYPE (type);
3576 if (TREE_CODE (real_type) == RECORD_TYPE)
3577 {
3578 real_type = promote_type (real_type);
3579 if (TREE_CODE (type) == TREE_LIST)
3580 TREE_PURPOSE (type) = real_type;
3581 }
3582
3583 /* Check redefinition */
3584 for (already = arg_types; already; already = TREE_CHAIN (already))
3585 if (TREE_PURPOSE (already) == name)
3586 {
3587 parse_error_context
3588 (wfl_name, "Variable `%s' is used more than once in the "
3589 "argument list of method `%s'", IDENTIFIER_POINTER (name),
3590 IDENTIFIER_POINTER (EXPR_WFL_NODE (id)));
3591 break;
3592 }
3593
3594 /* If we've an incomplete argument type, we know there is a location
3595 to patch when the type get resolved, later. */
3596 jdep = NULL;
3597 if (must_chain)
3598 {
3599 patch_stage = JDEP_METHOD;
3600 type = register_incomplete_type (patch_stage,
3601 type_wfl, wfl_name, type);
3602 jdep = CLASSD_LAST (ctxp->classd_list);
3603 JDEP_MISC (jdep) = id;
3604 }
3605
3606 /* The argument node: a name and a (possibly) incomplete type */
3607 arg_node = build_tree_list (name, real_type);
3608 if (jdep)
3609 JDEP_GET_PATCH (jdep) = &TREE_VALUE (arg_node);
3610 TREE_CHAIN (arg_node) = arg_types;
3611 arg_types = arg_node;
3612 }
3613 TYPE_ARG_TYPES (meth) = chainon (nreverse (arg_types), end_params_node);
3614 node = build_tree_list (id, meth);
3615 return node;
3616 }
3617
3618 static int
3619 unresolved_type_p (wfl, returned)
3620 tree wfl;
3621 tree *returned;
3622
3623 {
3624 if (TREE_CODE (wfl) == EXPR_WITH_FILE_LOCATION)
3625 {
3626 tree decl = IDENTIFIER_CLASS_VALUE (EXPR_WFL_NODE (wfl));
3627 if (returned)
3628 *returned = (decl ? TREE_TYPE (decl) : NULL_TREE);
3629 return 1;
3630 }
3631 if (returned)
3632 *returned = wfl;
3633 return 0;
3634 }
3635
3636 /* From NAME, build a qualified identifier node using the
3637 qualification from the current package definition. */
3638
3639 static tree
3640 parser_qualified_classname (name)
3641 tree name;
3642 {
3643 if (ctxp->package)
3644 return merge_qualified_name (ctxp->package, EXPR_WFL_NODE (name));
3645 else
3646 return EXPR_WFL_NODE (name);
3647 }
3648
3649 /* Called once the type a interface extends is resolved. Returns 0 if
3650 everything is OK. */
3651
3652 static int
3653 parser_check_super_interface (super_decl, this_decl, this_wfl)
3654 tree super_decl, this_decl, this_wfl;
3655 {
3656 tree super_type = TREE_TYPE (super_decl);
3657
3658 /* Has to be an interface */
3659 if (!CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (super_decl))))
3660 {
3661 parse_error_context
3662 (this_wfl, "Can't use %s `%s' to implement/extend %s `%s'",
3663 (TYPE_ARRAY_P (super_type) ? "array" : "class"),
3664 IDENTIFIER_POINTER (DECL_NAME (super_decl)),
3665 (CLASS_INTERFACE (TYPE_NAME (TREE_TYPE (this_decl))) ?
3666 "interface" : "class"),
3667 IDENTIFIER_POINTER (DECL_NAME (this_decl)));
3668 return 1;
3669 }
3670
3671 /* Check scope: same package OK, other package: OK if public */
3672 if (check_pkg_class_access (DECL_NAME (super_decl), lookup_cl (this_decl)))
3673 return 1;
3674
3675 SOURCE_FRONTEND_DEBUG (("Completing interface %s with %s",
3676 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3677 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3678 return 0;
3679 }
3680
3681 /* Makes sure that SUPER_DECL is suitable to extend THIS_DECL. Returns
3682 0 if everthing is OK. */
3683
3684 static int
3685 parser_check_super (super_decl, this_decl, wfl)
3686 tree super_decl, this_decl, wfl;
3687 {
3688 tree super_type = TREE_TYPE (super_decl);
3689
3690 /* SUPER should be a CLASS (neither an array nor an interface) */
3691 if (TYPE_ARRAY_P (super_type) || CLASS_INTERFACE (TYPE_NAME (super_type)))
3692 {
3693 parse_error_context
3694 (wfl, "Class `%s' can't subclass %s `%s'",
3695 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3696 (CLASS_INTERFACE (TYPE_NAME (super_type)) ? "interface" : "array"),
3697 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3698 return 1;
3699 }
3700
3701 if (CLASS_FINAL (TYPE_NAME (super_type)))
3702 {
3703 parse_error_context (wfl, "Can't subclass final classes: %s",
3704 IDENTIFIER_POINTER (DECL_NAME (super_decl)));
3705 return 1;
3706 }
3707
3708 /* Check scope: same package OK, other package: OK if public */
3709 if (check_pkg_class_access (DECL_NAME (super_decl), wfl))
3710 return 1;
3711
3712 SOURCE_FRONTEND_DEBUG (("Completing class %s with %s",
3713 IDENTIFIER_POINTER (DECL_NAME (this_decl)),
3714 IDENTIFIER_POINTER (DECL_NAME (super_decl))));
3715 return 0;
3716 }
3717
3718 /* Create a new dependency list and link it (in a LIFO manner) to the
3719 CTXP list of type dependency list. */
3720
3721 static void
3722 create_jdep_list (ctxp)
3723 struct parser_ctxt *ctxp;
3724 {
3725 jdeplist *new = (jdeplist *)xmalloc (sizeof (jdeplist));
3726 new->first = new->last = NULL;
3727 new->next = ctxp->classd_list;
3728 ctxp->classd_list = new;
3729 }
3730
3731 static jdeplist *
3732 reverse_jdep_list (ctxp)
3733 struct parser_ctxt *ctxp;
3734 {
3735 register jdeplist *prev = NULL, *current, *next;
3736 for (current = ctxp->classd_list; current; current = next)
3737 {
3738 next = current->next;
3739 current->next = prev;
3740 prev = current;
3741 }
3742 return prev;
3743 }
3744
3745 /* Create a fake pointer based on the ID stored in
3746 TYPE_NAME. TYPE_NAME can be a WFL or a incomplete type asking to be
3747 registered again. */
3748
3749 static tree
3750 obtain_incomplete_type (type_name)
3751 tree type_name;
3752 {
3753 tree ptr, name;
3754
3755 if (TREE_CODE (type_name) == EXPR_WITH_FILE_LOCATION)
3756 name = EXPR_WFL_NODE (type_name);
3757 else if (INCOMPLETE_TYPE_P (type_name))
3758 name = TYPE_NAME (type_name);
3759 else
3760 fatal ("invalid type name - obtain_incomplete_type");
3761
3762 for (ptr = ctxp->incomplete_class; ptr; ptr = TREE_CHAIN (ptr))
3763 if (TYPE_NAME (TREE_PURPOSE (ptr)) == name)
3764 break;
3765
3766 if (!ptr)
3767 {
3768 tree core;
3769 push_obstacks (&permanent_obstack, &permanent_obstack);
3770 BUILD_PTR_FROM_NAME (core, name);
3771 layout_type (core);
3772 ptr = build_tree_list (core, NULL_TREE);
3773 pop_obstacks ();
3774 TREE_CHAIN (ptr) = ctxp->incomplete_class;
3775 ctxp->incomplete_class = ptr;
3776 }
3777
3778 return ptr;
3779 }
3780
3781 /* Register a incomplete type whose name is WFL. Reuse PTR if PTR is
3782 non NULL instead of computing a new fake type based on WFL. The new
3783 dependency is inserted in the current type dependency list, in FIFO
3784 manner. */
3785
3786 static tree
3787 register_incomplete_type (kind, wfl, decl, ptr)
3788 int kind;
3789 tree wfl, decl, ptr;
3790 {
3791 jdep *new = (jdep *)xmalloc (sizeof (jdep));
3792
3793 if (!ptr && kind != JDEP_METHOD_END) /* JDEP_METHOD_END is a mere marker */
3794 ptr = obtain_incomplete_type (wfl);
3795
3796 JDEP_KIND (new) = kind;
3797 JDEP_DECL (new) = decl;
3798 JDEP_SOLV (new) = ptr;
3799 JDEP_WFL (new) = wfl;
3800 JDEP_CHAIN (new) = NULL;
3801 JDEP_MISC (new) = NULL_TREE;
3802 JDEP_GET_PATCH (new) = (tree *)NULL;
3803
3804 JDEP_INSERT (ctxp->classd_list, new);
3805
3806 return ptr;
3807 }
3808
3809 void
3810 java_check_circular_reference ()
3811 {
3812 tree current;
3813 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
3814 {
3815 tree type = TREE_TYPE (current);
3816 if (CLASS_INTERFACE (TYPE_NAME (type)))
3817 {
3818 /* Check all interfaces this class extends */
3819 tree basetype_vec = TYPE_BINFO_BASETYPES (type);
3820 int n, i;
3821
3822 if (!basetype_vec)
3823 return;
3824 n = TREE_VEC_LENGTH (basetype_vec);
3825 for (i = 0; i < n; i++)
3826 {
3827 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
3828 if (vec_elt && BINFO_TYPE (vec_elt) != object_type_node
3829 && interface_of_p (type, BINFO_TYPE (vec_elt)))
3830 parse_error_context (lookup_cl (current),
3831 "Cyclic interface inheritance");
3832 }
3833 }
3834 else
3835 if (inherits_from_p (CLASSTYPE_SUPER (type), type))
3836 parse_error_context (lookup_cl (current),
3837 "Cyclic class inheritance");
3838 }
3839 }
3840
3841 /* safe_layout_class just makes sure that we can load a class without
3842 disrupting the current_class, input_file, lineno, etc, information
3843 about the class processed currently. */
3844
3845 void
3846 safe_layout_class (class)
3847 tree class;
3848 {
3849 tree save_current_class = current_class;
3850 char *save_input_filename = input_filename;
3851 int save_lineno = lineno;
3852
3853 push_obstacks (&permanent_obstack, &permanent_obstack);
3854
3855 layout_class (class);
3856 pop_obstacks ();
3857
3858 current_class = save_current_class;
3859 input_filename = save_input_filename;
3860 lineno = save_lineno;
3861 CLASS_LOADED_P (class) = 1;
3862 }
3863
3864 static tree
3865 jdep_resolve_class (dep)
3866 jdep *dep;
3867 {
3868 tree decl;
3869
3870 if (JDEP_RESOLVED_P (dep))
3871 decl = JDEP_RESOLVED_DECL (dep);
3872 else
3873 {
3874 decl = resolve_class (JDEP_TO_RESOLVE (dep),
3875 JDEP_DECL (dep), JDEP_WFL (dep));
3876 JDEP_RESOLVED (dep, decl);
3877 }
3878
3879 if (!decl)
3880 complete_class_report_errors (dep);
3881
3882 return decl;
3883 }
3884
3885 /* Complete unsatisfied class declaration and their dependencies */
3886
3887 void
3888 java_complete_class ()
3889 {
3890 tree cclass;
3891 jdeplist *cclassd;
3892 int error_found;
3893 tree type;
3894
3895 push_obstacks (&permanent_obstack, &permanent_obstack);
3896
3897 /* Process imports and reverse the import on demand list */
3898 process_imports ();
3899 if (ctxp->import_demand_list)
3900 ctxp->import_demand_list = nreverse (ctxp->import_demand_list);
3901
3902 /* Rever things so we have the right order */
3903 ctxp->class_list = nreverse (ctxp->class_list);
3904 ctxp->classd_list = reverse_jdep_list (ctxp);
3905
3906 for (cclassd = ctxp->classd_list, cclass = ctxp->class_list;
3907 cclass && cclassd;
3908 cclass = TREE_CHAIN (cclass), cclassd = CLASSD_CHAIN (cclassd))
3909 {
3910 jdep *dep;
3911 for (dep = CLASSD_FIRST (cclassd); dep; dep = JDEP_CHAIN (dep))
3912 {
3913 tree decl;
3914 if (!(decl = jdep_resolve_class (dep)))
3915 continue;
3916
3917 /* Now it's time to patch */
3918 switch (JDEP_KIND (dep))
3919 {
3920 case JDEP_SUPER:
3921 /* Simply patch super */
3922 if (parser_check_super (decl, JDEP_DECL (dep), JDEP_WFL (dep)))
3923 continue;
3924 BINFO_TYPE (TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO
3925 (TREE_TYPE (JDEP_DECL (dep)))), 0)) = TREE_TYPE (decl);
3926 break;
3927
3928 case JDEP_FIELD:
3929 {
3930 /* We do part of the job done in add_field */
3931 tree field_decl = JDEP_DECL (dep);
3932 tree field_type = TREE_TYPE (decl);
3933 push_obstacks (&permanent_obstack, &permanent_obstack);
3934 if (TREE_CODE (field_type) == RECORD_TYPE)
3935 field_type = promote_type (field_type);
3936 pop_obstacks ();
3937 TREE_TYPE (field_decl) = field_type;
3938 DECL_ALIGN (field_decl) = 0;
3939 layout_decl (field_decl, 0);
3940 SOURCE_FRONTEND_DEBUG
3941 (("Completed field/var decl `%s' with `%s'",
3942 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
3943 IDENTIFIER_POINTER (DECL_NAME (decl))));
3944 break;
3945 }
3946 case JDEP_METHOD: /* We start patching a method */
3947 case JDEP_METHOD_RETURN:
3948 error_found = 0;
3949 while (1)
3950 {
3951 if (decl)
3952 {
3953 type = TREE_TYPE(decl);
3954 if (TREE_CODE (type) == RECORD_TYPE)
3955 type = promote_type (type);
3956 JDEP_APPLY_PATCH (dep, type);
3957 SOURCE_FRONTEND_DEBUG
3958 (((JDEP_KIND (dep) == JDEP_METHOD_RETURN ?
3959 "Completing fct `%s' with ret type `%s'":
3960 "Completing arg `%s' with type `%s'"),
3961 IDENTIFIER_POINTER (EXPR_WFL_NODE
3962 (JDEP_DECL_WFL (dep))),
3963 IDENTIFIER_POINTER (DECL_NAME (decl))));
3964 }
3965 else
3966 error_found = 1;
3967 dep = JDEP_CHAIN (dep);
3968 if (JDEP_KIND (dep) == JDEP_METHOD_END)
3969 break;
3970 else
3971 decl = jdep_resolve_class (dep);
3972 }
3973 if (!error_found)
3974 {
3975 tree mdecl = JDEP_DECL (dep), signature;
3976 push_obstacks (&permanent_obstack, &permanent_obstack);
3977 /* Recompute and reset the signature */
3978 signature = build_java_signature (TREE_TYPE (mdecl));
3979 set_java_signature (TREE_TYPE (mdecl), signature);
3980 pop_obstacks ();
3981 }
3982 else
3983 continue;
3984 break;
3985
3986 case JDEP_INTERFACE:
3987 if (parser_check_super_interface (decl, JDEP_DECL (dep),
3988 JDEP_WFL (dep)))
3989 continue;
3990 parser_add_interface (JDEP_DECL (dep), decl, JDEP_WFL (dep));
3991 break;
3992
3993 case JDEP_PARM:
3994 case JDEP_VARIABLE:
3995 type = TREE_TYPE(decl);
3996 if (TREE_CODE (type) == RECORD_TYPE)
3997 type = promote_type (type);
3998 JDEP_APPLY_PATCH (dep, type);
3999 break;
4000
4001 case JDEP_TYPE:
4002 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4003 SOURCE_FRONTEND_DEBUG
4004 (("Completing a random type dependency on a '%s' node",
4005 tree_code_name [TREE_CODE (JDEP_DECL (dep))]));
4006 break;
4007
4008 case JDEP_EXCEPTION:
4009 JDEP_APPLY_PATCH (dep, TREE_TYPE (decl));
4010 SOURCE_FRONTEND_DEBUG
4011 (("Completing `%s' `throws' argument node",
4012 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)))));
4013 break;
4014
4015 default:
4016 fatal ("Can't handle patch code %d - java_complete_class",
4017 JDEP_KIND (dep));
4018 }
4019 }
4020 }
4021 pop_obstacks ();
4022 return;
4023 }
4024
4025 /* Resolve class CLASS_TYPE. Handle the case of trying to resolve an
4026 array. */
4027
4028 static tree
4029 resolve_class (class_type, decl, cl)
4030 tree class_type, decl, cl;
4031 {
4032 char *name = IDENTIFIER_POINTER (TYPE_NAME (class_type));
4033 char *base = name;
4034 tree resolved_type, resolved_type_decl;
4035
4036 /* 1- Check to see if we have an array. If true, find what we really
4037 want to resolve */
4038 while (name[0] == '[')
4039 name++;
4040 if (base != name)
4041 TYPE_NAME (class_type) = get_identifier (name);
4042
4043 /* 2- Resolve the bare type */
4044 if (!(resolved_type_decl = do_resolve_class (class_type, decl, cl)))
4045 return NULL_TREE;
4046 resolved_type = TREE_TYPE (resolved_type_decl);
4047
4048 /* 3- If we have and array, reconstruct the array down to its nesting */
4049 if (base != name)
4050 {
4051 while (base != name)
4052 {
4053 if (TREE_CODE (resolved_type) == RECORD_TYPE)
4054 resolved_type = promote_type (resolved_type);
4055 resolved_type = build_java_array_type (resolved_type, -1);
4056 CLASS_LOADED_P (resolved_type) = 1;
4057 name--;
4058 }
4059 /* Build a fake decl for this, since this is what is expected to
4060 be returned. */
4061 resolved_type_decl =
4062 build_decl (TYPE_DECL, TYPE_NAME (resolved_type), resolved_type);
4063 /* Figure how those two things are important for error report. FIXME */
4064 DECL_SOURCE_LINE (resolved_type_decl) = 0;
4065 DECL_SOURCE_FILE (resolved_type_decl) = input_filename;
4066 }
4067 return resolved_type_decl;
4068 }
4069
4070 /* Effectively perform the resolution of class CLASS_TYPE. DECL or CL
4071 are used to report error messages. */
4072
4073 static tree
4074 do_resolve_class (class_type, decl, cl)
4075 tree class_type;
4076 tree decl;
4077 tree cl;
4078 {
4079 tree new_class_decl;
4080 tree original_name = NULL_TREE;
4081
4082 /* Do not try to replace TYPE_NAME (class_type) by a variable, since
4083 its is changed by find_in_imports{_on_demand} */
4084
4085 /* 1- Check for the type in single imports */
4086 if (find_in_imports (class_type))
4087 return NULL_TREE;
4088
4089 /* 2- And check for the type in the current compilation unit. If it fails,
4090 try with a name qualified with the package name if appropriate. */
4091 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4092 {
4093 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4094 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4095 load_class (TYPE_NAME (class_type), 0);
4096 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4097 }
4098
4099 original_name = TYPE_NAME (class_type);
4100 if (!QUALIFIED_P (TYPE_NAME (class_type)) && ctxp->package)
4101 TYPE_NAME (class_type) = merge_qualified_name (ctxp->package,
4102 TYPE_NAME (class_type));
4103 if (!(new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4104 load_class (TYPE_NAME (class_type), 0);
4105 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4106 {
4107 if (!CLASS_LOADED_P (TREE_TYPE (new_class_decl)) &&
4108 !CLASS_FROM_SOURCE_P (TREE_TYPE (new_class_decl)))
4109 load_class (TYPE_NAME (class_type), 0);
4110 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4111 }
4112 TYPE_NAME (class_type) = original_name;
4113
4114 /* 3- Check an other compilation unit that bears the name of type */
4115 load_class (TYPE_NAME (class_type), 0);
4116 if (check_pkg_class_access (TYPE_NAME (class_type),
4117 (cl ? cl : lookup_cl (decl))))
4118 return NULL_TREE;
4119
4120 if ((new_class_decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type))))
4121 return new_class_decl;
4122
4123 /* 4- Check the import on demands. Don't allow bar.baz to be
4124 imported from foo.* */
4125 if (!QUALIFIED_P (TYPE_NAME (class_type)))
4126 if (find_in_imports_on_demand (class_type))
4127 return NULL_TREE;
4128
4129 /* 5- Last call for a resolution */
4130 return IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
4131 }
4132
4133 /* Resolve NAME and lay it out (if not done and if not the current
4134 parsed class). Return a decl node. This function is meant to be
4135 called when type resolution is necessary during the walk pass. */
4136
4137 static tree
4138 resolve_and_layout (something, cl)
4139 tree something;
4140 tree cl;
4141 {
4142 tree decl;
4143
4144 /* Don't do that on the current class */
4145 if (something == current_class)
4146 return TYPE_NAME (current_class);
4147
4148 /* Don't do anything for void and other primitive types */
4149 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4150 return NULL_TREE;
4151
4152 /* Pointer types can be reall pointer types or fake pointers. When
4153 finding a real pointer, recheck for primitive types */
4154 if (TREE_CODE (something) == POINTER_TYPE)
4155 {
4156 if (TREE_TYPE (something))
4157 {
4158 something = TREE_TYPE (something);
4159 if (JPRIMITIVE_TYPE_P (something) || something == void_type_node)
4160 return NULL_TREE;
4161 }
4162 else
4163 something = TYPE_NAME (something);
4164 }
4165
4166 /* Don't do anything for arrays of primitive types */
4167 if (TREE_CODE (something) == RECORD_TYPE && TYPE_ARRAY_P (something)
4168 && JPRIMITIVE_TYPE_P (TYPE_ARRAY_ELEMENT (something)))
4169 return NULL_TREE;
4170
4171 /* If something is not and IDENTIFIER_NODE, it can be a a TYPE_DECL
4172 or a real TYPE */
4173 if (TREE_CODE (something) != IDENTIFIER_NODE)
4174 something = (TREE_CODE (TYPE_NAME (something)) == TYPE_DECL ?
4175 DECL_NAME (TYPE_NAME (something)) : TYPE_NAME (something));
4176
4177 if (!(decl = resolve_no_layout (something, cl)))
4178 return NULL_TREE;
4179
4180 /* Resolve and layout if necessary */
4181 layout_class_methods (TREE_TYPE (decl));
4182 if (CLASS_FROM_SOURCE_P (TREE_TYPE (decl)))
4183 CHECK_METHODS (decl);
4184 if (TREE_TYPE (decl) != current_class && !CLASS_LOADED_P (TREE_TYPE (decl)))
4185 safe_layout_class (TREE_TYPE (decl));
4186
4187 return decl;
4188 }
4189
4190 /* Resolve a class, returns its decl but doesn't perform any
4191 layout. The current parsing context is saved and restored */
4192
4193 static tree
4194 resolve_no_layout (name, cl)
4195 tree name, cl;
4196 {
4197 tree ptr, decl;
4198 BUILD_PTR_FROM_NAME (ptr, name);
4199 java_parser_context_save_global ();
4200 decl = resolve_class (ptr, NULL_TREE, cl);
4201 java_parser_context_restore_global ();
4202
4203 return decl;
4204 }
4205
4206 /* Called when reporting errors. Skip leader '[' in a complex array
4207 type description that failed to be resolved. */
4208
4209 static char *
4210 purify_type_name (name)
4211 char *name;
4212 {
4213 while (*name && *name == '[')
4214 name++;
4215 return name;
4216 }
4217
4218 /* The type CURRENT refers to can't be found. We print error messages. */
4219
4220 static void
4221 complete_class_report_errors (dep)
4222 jdep *dep;
4223 {
4224 char *name;
4225
4226 if (!JDEP_WFL (dep))
4227 return;
4228
4229 name = IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep)));
4230 switch (JDEP_KIND (dep))
4231 {
4232 case JDEP_SUPER:
4233 parse_error_context
4234 (JDEP_WFL (dep), "Superclass `%s' of class `%s' not found",
4235 purify_type_name (name),
4236 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4237 break;
4238 case JDEP_FIELD:
4239 parse_error_context
4240 (JDEP_WFL (dep), "Type `%s' not found in declaration of field `%s'",
4241 purify_type_name (name),
4242 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4243 break;
4244 case JDEP_METHOD: /* Covers arguments */
4245 parse_error_context
4246 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4247 "argument `%s' of method `%s'",
4248 purify_type_name (name),
4249 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))),
4250 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_MISC (dep))));
4251 break;
4252 case JDEP_METHOD_RETURN: /* Covers return type */
4253 parse_error_context
4254 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4255 "return type of method `%s'",
4256 purify_type_name (name),
4257 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_DECL_WFL (dep))));
4258 break;
4259 case JDEP_INTERFACE:
4260 parse_error_context
4261 (JDEP_WFL (dep), "Superinterface `%s' of %s `%s' not found",
4262 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))),
4263 (CLASS_OR_INTERFACE (JDEP_DECL (dep), "class", "interface")),
4264 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4265 break;
4266 case JDEP_VARIABLE:
4267 parse_error_context
4268 (JDEP_WFL (dep), "Type `%s' not found in the declaration of the "
4269 "local variable `%s'",
4270 purify_type_name (IDENTIFIER_POINTER
4271 (EXPR_WFL_NODE (JDEP_WFL (dep)))),
4272 IDENTIFIER_POINTER (DECL_NAME (JDEP_DECL (dep))));
4273 break;
4274 case JDEP_EXCEPTION: /* As specified by `throws' */
4275 parse_error_context
4276 (JDEP_WFL (dep), "Class `%s' not found in `throws'",
4277 IDENTIFIER_POINTER (EXPR_WFL_NODE (JDEP_WFL (dep))));
4278 break;
4279 default:
4280 /* Fix for -Wall. Just break doing nothing. The error will be
4281 caught later */
4282 break;
4283 }
4284 }
4285
4286 /* Check uninitialized final. */
4287
4288 void
4289 java_check_final ()
4290 {
4291 }
4292
4293 /* Return a static string containing the DECL prototype string. If
4294 DECL is a constructor, use the class name instead of the form
4295 <init> */
4296
4297 static char *
4298 get_printable_method_name (decl)
4299 tree decl;
4300 {
4301 char *to_return;
4302 tree name;
4303
4304 if (DECL_CONSTRUCTOR_P (decl))
4305 {
4306 name = DECL_NAME (decl);
4307 DECL_NAME (decl) = DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)));
4308 }
4309
4310 to_return = lang_printable_name (decl, 0);
4311 if (DECL_CONSTRUCTOR_P (decl))
4312 DECL_NAME (decl) = name;
4313
4314 return to_return;
4315 }
4316
4317 /* Reinstall the proper DECL_NAME on METHOD. Return 0 if the method
4318 nevertheless needs to be verfied, 1 otherwise. */
4319
4320 static int
4321 reset_method_name (method)
4322 tree method;
4323 {
4324 if (DECL_NAME (method) != clinit_identifier_node
4325 && DECL_NAME (method) != finit_identifier_node)
4326 {
4327 /* NAME is just the plain name when Object is being defined */
4328 if (DECL_CONTEXT (method) != object_type_node)
4329 DECL_NAME (method) = (DECL_CONSTRUCTOR_P (method) ?
4330 init_identifier_node : GET_METHOD_NAME (method));
4331 return 0;
4332 }
4333 else
4334 return 1;
4335 }
4336
4337 /* Return the name of METHOD_DECL, when DECL_NAME is a WFL */
4338
4339 tree
4340 java_get_real_method_name (method_decl)
4341 tree method_decl;
4342 {
4343 tree method_name = DECL_NAME (method_decl);
4344 if (DECL_CONSTRUCTOR_P (method_decl))
4345 return init_identifier_node;
4346
4347 /* Explain here why METHOD_DECL doesn't have the DECL_CONSTRUCTUR_P
4348 and still can be a constructor. FIXME */
4349
4350 /* Don't confuse method only bearing the name of their class as
4351 constructors */
4352 else if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (method_decl))
4353 && ctxp
4354 && ctxp->current_parsed_class_un == EXPR_WFL_NODE (method_name)
4355 && get_access_flags_from_decl (method_decl) <= ACC_PROTECTED
4356 && TREE_TYPE (TREE_TYPE (method_decl)) == void_type_node)
4357 return init_identifier_node;
4358 else
4359 return EXPR_WFL_NODE (method_name);
4360 }
4361
4362 /* Track method being redefined inside the same class. As a side
4363 effect, set DECL_NAME to an IDENTIFIER (prior entering this
4364 function it's a FWL, so we can track errors more accurately */
4365
4366 static int
4367 check_method_redefinition (class, method)
4368 tree class, method;
4369 {
4370 tree redef, name;
4371 tree cl = DECL_NAME (method);
4372 tree sig = TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (method));
4373 /* decl name of artificial <clinit> and $finit$ doesn't need to be fixed and
4374 checked */
4375
4376 /* Reset the method name before running the check. If it returns 1,
4377 the method doesn't need to be verified with respect to method
4378 redeclaration and we return 0 */
4379 if (reset_method_name (method))
4380 return 0;
4381
4382 name = DECL_NAME (method);
4383 for (redef = TYPE_METHODS (class); redef; redef = TREE_CHAIN (redef))
4384 {
4385 if (redef == method)
4386 break;
4387 if (DECL_NAME (redef) == name
4388 && sig == TYPE_ARGUMENT_SIGNATURE (TREE_TYPE (redef)))
4389 {
4390 parse_error_context
4391 (cl, "Duplicate %s declaration `%s'",
4392 (DECL_CONSTRUCTOR_P (redef) ? "constructor" : "method"),
4393 get_printable_method_name (redef));
4394 return 1;
4395 }
4396 }
4397 return 0;
4398 }
4399
4400 /* Check all the methods of CLASS. Methods are first completed then
4401 checked according to regular method existance rules.
4402 If no constructor were encountered, then build its declaration. */
4403
4404 static void
4405 java_check_regular_methods (class_decl)
4406 tree class_decl;
4407 {
4408 int saw_constructor = 0;
4409 tree method;
4410 tree class = CLASS_TO_HANDLE_TYPE (TREE_TYPE (class_decl));
4411 tree super_class = CLASSTYPE_SUPER (class);
4412 tree saved_found_wfl = NULL_TREE, found = NULL_TREE;
4413 tree mthrows;
4414
4415 /* It is not necessary to check methods defined in java.lang.Object */
4416 if (class == object_type_node)
4417 return;
4418
4419 if (!TYPE_NVIRTUALS (class))
4420 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4421
4422 /* Should take interfaces into account. FIXME */
4423 for (method = TYPE_METHODS (class); method; method = TREE_CHAIN (method))
4424 {
4425 tree sig;
4426 tree method_wfl = DECL_NAME (method);
4427 int aflags;
4428
4429 /* If we previously found something and its name was saved,
4430 reinstall it now */
4431 if (found && saved_found_wfl)
4432 DECL_NAME (found) = saved_found_wfl;
4433
4434 /* Check for redefinitions */
4435 if (check_method_redefinition (class, method))
4436 continue;
4437
4438 /* If we see one constructor a mark so we don't generate the
4439 default one. Also skip other verifications: constructors
4440 can't be inherited hence hiden or overriden */
4441 if (DECL_CONSTRUCTOR_P (method))
4442 {
4443 saw_constructor = 1;
4444 continue;
4445 }
4446
4447 /* We verify things thrown by the method. They must inherits from
4448 java.lang.Throwable */
4449 for (mthrows = DECL_FUNCTION_THROWS (method);
4450 mthrows; mthrows = TREE_CHAIN (mthrows))
4451 {
4452 if (!inherits_from_p (TREE_VALUE (mthrows), throwable_type_node))
4453 parse_error_context
4454 (TREE_PURPOSE (mthrows), "Class `%s' in `throws' clause must be "
4455 "a subclass of class `java.lang.Throwable'",
4456 IDENTIFIER_POINTER
4457 (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))));
4458 }
4459
4460 sig = build_java_argument_signature (TREE_TYPE (method));
4461 found = lookup_argument_method (super_class, DECL_NAME (method), sig);
4462
4463 /* Nothing overrides or it's a private method. */
4464 if (!found || (found && METHOD_PRIVATE (found)))
4465 continue;
4466
4467 /* If found wasn't verified, it's DECL_NAME won't be set properly.
4468 We set it temporarily for the sake of the error report. */
4469 saved_found_wfl = DECL_NAME (found);
4470 reset_method_name (found);
4471
4472 /* Can't override a method with the same name and different return
4473 types. */
4474 if (TREE_TYPE (TREE_TYPE (found)) != TREE_TYPE (TREE_TYPE (method)))
4475 {
4476 char *t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)),
4477 0));
4478 parse_error_context
4479 (method_wfl,
4480 "Method `%s' was defined with return type `%s' in class `%s'",
4481 lang_printable_name (found, 0), t,
4482 IDENTIFIER_POINTER
4483 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4484 free (t);
4485 }
4486
4487 /* Can't override final. Can't override static. */
4488 if (METHOD_FINAL (found) || METHOD_STATIC (found))
4489 {
4490 /* Static *can* override static */
4491 if (METHOD_STATIC (found) && METHOD_STATIC (method))
4492 continue;
4493 parse_error_context
4494 (method_wfl,
4495 "%s methods can't be overriden. Method `%s' is %s in class `%s'",
4496 (METHOD_FINAL (found) ? "Final" : "Static"),
4497 lang_printable_name (found, 0),
4498 (METHOD_FINAL (found) ? "final" : "static"),
4499 IDENTIFIER_POINTER
4500 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4501 continue;
4502 }
4503 /* Static method can't override instance method. */
4504 if (METHOD_STATIC (method))
4505 {
4506 parse_error_context
4507 (method_wfl,
4508 "Instance methods can't be overriden by a static method. Method "
4509 "`%s' is an instance method in class `%s'",
4510 lang_printable_name (found, 0),
4511 IDENTIFIER_POINTER
4512 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4513 continue;
4514 }
4515
4516 aflags = get_access_flags_from_decl (found);
4517 /* - Overriding/hiding public must be public
4518 - Overriding/hiding protected must be protected or public
4519 - If the overriden or hidden method has default (package)
4520 access, then the overriding or hiding method must not be
4521 private; otherwise, a compile-time error occurs */
4522 if ((METHOD_PUBLIC (found) && !METHOD_PUBLIC (method))
4523 || (METHOD_PROTECTED (found)
4524 && !(METHOD_PUBLIC (method) || METHOD_PROTECTED (method)))
4525 || (!(aflags & (ACC_PUBLIC | ACC_PRIVATE | ACC_STATIC))
4526 && METHOD_PRIVATE (method)))
4527 {
4528 parse_error_context
4529 (method_wfl,
4530 "Methods can't be overridden to be more private. Method `%s' is "
4531 "not %s in class `%s'", lang_printable_name (method, 0),
4532 (METHOD_PUBLIC (method) ? "public" :
4533 (METHOD_PRIVATE (method) ? "private" : "protected")),
4534 IDENTIFIER_POINTER (DECL_NAME
4535 (TYPE_NAME (DECL_CONTEXT (found)))));
4536 continue;
4537 }
4538
4539 /* Overriding methods must have compatible `throws' clauses on checked
4540 exceptions, if any */
4541 check_throws_clauses (method, method_wfl, found);
4542
4543 /* If the method has default access in an other package, then
4544 issue a warning that the current method doesn't override the
4545 one that was found elsewhere. Do not issue this warning when
4546 the match was found in java.lang.Object. */
4547 if (DECL_CONTEXT (found) != object_type_node
4548 && (!aflags || (aflags > ACC_PROTECTED))
4549 && !class_in_current_package (DECL_CONTEXT (found))
4550 && flag_not_overriding)
4551 parse_warning_context
4552 (method_wfl, "Method `%s' in class `%s' does not "
4553 "override the corresponding method in class `%s', which is "
4554 "private to a different package",
4555 lang_printable_name (found, 0),
4556 IDENTIFIER_POINTER (DECL_NAME (class_decl)),
4557 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4558
4559 /* Inheriting multiple methods with the same signature. FIXME */
4560 }
4561
4562 /* Don't forget eventual pending found and saved_found_wfl. Take
4563 into account that we might have exited because we saw an
4564 aritifical method as the last entry. */
4565
4566 if (found && !DECL_ARTIFICIAL (found) && saved_found_wfl)
4567 DECL_NAME (found) = saved_found_wfl;
4568
4569 if (!TYPE_NVIRTUALS (class))
4570 TYPE_METHODS (class) = nreverse (TYPE_METHODS (class));
4571
4572 if (!saw_constructor)
4573 {
4574 /* No constructor seen, we craft one, at line 0. Since this
4575 operation takes place after we laid methods out
4576 (layout_class_methods), we prepare the its DECL
4577 appropriately. */
4578 int flags;
4579 tree decl;
4580
4581 /* If the class is declared PUBLIC, the default constructor is
4582 PUBLIC otherwise it has default access implied by no access
4583 modifiers. */
4584 flags = (get_access_flags_from_decl (class_decl) & ACC_PUBLIC ?
4585 ACC_PUBLIC : 0);
4586 decl = create_artificial_method (class, flags, void_type_node,
4587 init_identifier_node, end_params_node);
4588 DECL_CONSTRUCTOR_P (decl) = 1;
4589 layout_class_method (TREE_TYPE (class_decl), NULL_TREE, decl, NULL_TREE);
4590 }
4591 }
4592
4593 /* Return a non zero value if the `throws' clause of METHOD (if any)
4594 is incompatible with the `throws' clause of FOUND (if any). */
4595
4596 static void
4597 check_throws_clauses (method, method_wfl, found)
4598 tree method, method_wfl, found;
4599 {
4600 tree mthrows, fthrows;
4601
4602 /* Can't check these things with class loaded from bytecode. FIXME */
4603 if (!CLASS_FROM_SOURCE_P (DECL_CONTEXT (found)))
4604 return;
4605
4606 for (mthrows = DECL_FUNCTION_THROWS (method);
4607 mthrows; mthrows = TREE_CHAIN (mthrows))
4608 {
4609 /* We don't verify unchecked expressions */
4610 if (IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (mthrows)))
4611 continue;
4612 /* Checked expression must be compatible */
4613 for (fthrows = DECL_FUNCTION_THROWS (found);
4614 fthrows; fthrows = TREE_CHAIN (fthrows))
4615 if (inherits_from_p (TREE_VALUE (mthrows), TREE_VALUE (fthrows)))
4616 break;
4617 if (!fthrows)
4618 {
4619 parse_error_context
4620 (method_wfl, "Invalid checked exception class `%s' in "
4621 "`throws' clause. The exception must be a subclass of an "
4622 "exception thrown by `%s' from class `%s'",
4623 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (TREE_VALUE (mthrows)))),
4624 lang_printable_name (found, 0),
4625 IDENTIFIER_POINTER
4626 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4627 }
4628 }
4629 }
4630
4631 /* Check abstract method of interface INTERFACE */
4632
4633 static void
4634 java_check_abstract_methods (interface_decl)
4635 tree interface_decl;
4636 {
4637 int i, n;
4638 tree method, basetype_vec, found;
4639 tree interface = TREE_TYPE (interface_decl);
4640
4641 for (method = TYPE_METHODS (interface); method; method = TREE_CHAIN (method))
4642 {
4643 tree method_wfl = DECL_NAME (method);
4644
4645 /* 2- Check for double definition inside the defining interface */
4646 if (check_method_redefinition (interface, method))
4647 continue;
4648
4649 /* 3- Overriding is OK as far as we preserve the return type and
4650 the thrown exceptions (FIXME) */
4651 found = lookup_java_interface_method2 (interface, method);
4652 if (found)
4653 {
4654 char *t;
4655 tree saved_found_wfl = DECL_NAME (found);
4656 reset_method_name (found);
4657 t = strdup (lang_printable_name (TREE_TYPE (TREE_TYPE (found)), 0));
4658 parse_error_context
4659 (method_wfl,
4660 "Method `%s' was defined with return type `%s' in class `%s'",
4661 lang_printable_name (found, 0), t,
4662 IDENTIFIER_POINTER
4663 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4664 free (t);
4665 continue;
4666
4667 DECL_NAME (found) = saved_found_wfl;
4668 }
4669 }
4670
4671 /* 4- Inherited methods can't differ by their returned types */
4672 if (!(basetype_vec = TYPE_BINFO_BASETYPES (interface)))
4673 return;
4674 n = TREE_VEC_LENGTH (basetype_vec);
4675 for (i = 0; i < n; i++)
4676 {
4677 tree sub_interface_method, sub_interface;
4678 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
4679 if (!vec_elt)
4680 continue;
4681 sub_interface = BINFO_TYPE (vec_elt);
4682 for (sub_interface_method = TYPE_METHODS (sub_interface);
4683 sub_interface_method;
4684 sub_interface_method = TREE_CHAIN (sub_interface_method))
4685 {
4686 found = lookup_java_interface_method2 (interface,
4687 sub_interface_method);
4688 if (found && (found != sub_interface_method))
4689 {
4690 tree saved_found_wfl = DECL_NAME (found);
4691 reset_method_name (found);
4692 parse_error_context
4693 (lookup_cl (sub_interface_method),
4694 "Interface `%s' inherits method `%s' from interface `%s'. "
4695 "This method is redefined with a different return type in "
4696 "interface `%s'",
4697 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (interface))),
4698 lang_printable_name (found, 0),
4699 IDENTIFIER_POINTER
4700 (DECL_NAME (TYPE_NAME
4701 (DECL_CONTEXT (sub_interface_method)))),
4702 IDENTIFIER_POINTER
4703 (DECL_NAME (TYPE_NAME (DECL_CONTEXT (found)))));
4704 DECL_NAME (found) = saved_found_wfl;
4705 }
4706 }
4707 }
4708 }
4709
4710 /* Lookup methods in interfaces using their name and partial
4711 signature. Return a matching method only if their types differ. */
4712
4713 static tree
4714 lookup_java_interface_method2 (class, method_decl)
4715 tree class, method_decl;
4716 {
4717 int i, n;
4718 tree basetype_vec = TYPE_BINFO_BASETYPES (class), to_return;
4719
4720 if (!basetype_vec)
4721 return NULL_TREE;
4722
4723 n = TREE_VEC_LENGTH (basetype_vec);
4724 for (i = 0; i < n; i++)
4725 {
4726 tree vec_elt = TREE_VEC_ELT (basetype_vec, i), to_return;
4727 if ((BINFO_TYPE (vec_elt) != object_type_node)
4728 && (to_return =
4729 lookup_java_method2 (BINFO_TYPE (vec_elt), method_decl, 1)))
4730 return to_return;
4731 }
4732 for (i = 0; i < n; i++)
4733 {
4734 to_return = lookup_java_interface_method2
4735 (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)), method_decl);
4736 if (to_return)
4737 return to_return;
4738 }
4739
4740 return NULL_TREE;
4741 }
4742
4743 /* Lookup method using their name and partial signature. Return a
4744 matching method only if their types differ. */
4745
4746 static tree
4747 lookup_java_method2 (clas, method_decl, do_interface)
4748 tree clas, method_decl;
4749 int do_interface;
4750 {
4751 tree method, method_signature, method_name, method_type, name;
4752
4753 method_signature = build_java_argument_signature (TREE_TYPE (method_decl));
4754 name = DECL_NAME (method_decl);
4755 method_name = (TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4756 EXPR_WFL_NODE (name) : name);
4757 method_type = TREE_TYPE (TREE_TYPE (method_decl));
4758
4759 while (clas != NULL_TREE)
4760 {
4761 for (method = TYPE_METHODS (clas);
4762 method != NULL_TREE; method = TREE_CHAIN (method))
4763 {
4764 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
4765 tree name = DECL_NAME (method);
4766 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
4767 EXPR_WFL_NODE (name) : name) == method_name
4768 && method_sig == method_signature
4769 && TREE_TYPE (TREE_TYPE (method)) != method_type)
4770 return method;
4771 }
4772 clas = (do_interface ? NULL_TREE : CLASSTYPE_SUPER (clas));
4773 }
4774 return NULL_TREE;
4775 }
4776
4777 /* Return the line that matches DECL line number. Used during error
4778 report */
4779
4780 static tree
4781 lookup_cl (decl)
4782 tree decl;
4783 {
4784 static tree cl = NULL_TREE;
4785
4786 if (!decl)
4787 return NULL_TREE;
4788
4789 if (cl == NULL_TREE)
4790 cl = build_expr_wfl (NULL_TREE, NULL, 0, 0);
4791
4792 EXPR_WFL_FILENAME_NODE (cl) = get_identifier (DECL_SOURCE_FILE (decl));
4793 EXPR_WFL_SET_LINECOL (cl, DECL_SOURCE_LINE_FIRST (decl), -1);
4794
4795 return cl;
4796 }
4797
4798 /* Look for a simple name in the single-type import list */
4799
4800 static tree
4801 find_name_in_single_imports (name)
4802 tree name;
4803 {
4804 tree node;
4805
4806 for (node = ctxp->import_list; node; node = TREE_CHAIN (node))
4807 if (TREE_VALUE (node) == name)
4808 return (EXPR_WFL_NODE (TREE_PURPOSE (node)));
4809
4810 return NULL_TREE;
4811 }
4812
4813 /* Process all single-type import. */
4814
4815 static int
4816 process_imports ()
4817 {
4818 tree import;
4819 int error_found;
4820
4821 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4822 {
4823 tree to_be_found = EXPR_WFL_NODE (TREE_PURPOSE (import));
4824
4825 /* Don't load twice something already defined. */
4826 if (IDENTIFIER_CLASS_VALUE (to_be_found))
4827 continue;
4828 QUALIFIED_P (to_be_found) = 1;
4829 load_class (to_be_found, 0);
4830 error_found =
4831 check_pkg_class_access (to_be_found, TREE_PURPOSE (import));
4832 if (!IDENTIFIER_CLASS_VALUE (to_be_found))
4833 {
4834 parse_error_context (TREE_PURPOSE (import),
4835 "Class or interface `%s' not found in import",
4836 IDENTIFIER_POINTER (to_be_found));
4837 return 1;
4838 }
4839 if (error_found)
4840 return 1;
4841 }
4842 return 0;
4843 }
4844
4845 /* Possibly find a class imported by a single-type import statement. Return
4846 1 if an error occured, 0 otherwise. */
4847
4848 static int
4849 find_in_imports (class_type)
4850 tree class_type;
4851 {
4852 tree import;
4853
4854 for (import = ctxp->import_list; import; import = TREE_CHAIN (import))
4855 if (TREE_VALUE (import) == TYPE_NAME (class_type))
4856 {
4857 TYPE_NAME (class_type) = EXPR_WFL_NODE (TREE_PURPOSE (import));
4858 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
4859 }
4860 return 0;
4861 }
4862
4863 static int
4864 note_possible_classname (name, len)
4865 char *name;
4866 int len;
4867 {
4868 tree node;
4869 if (len > 5 && strncmp (&name [len-5], ".java", 5) == 0)
4870 len = len - 5;
4871 else if (len > 6 && strncmp (&name [len-6], ".class", 6) == 0)
4872 len = len - 6;
4873 else
4874 return 0;
4875 node = ident_subst (name, len, "", '/', '.', "");
4876 IS_A_CLASSFILE_NAME (node) = 1; /* Or soon to be */
4877 QUALIFIED_P (node) = 1; /* As soon as we turn / into . */
4878 return 1;
4879 }
4880
4881 /* Read a import directory, gathering potential match for further type
4882 references. Indifferently reads a filesystem or a ZIP archive
4883 directory. */
4884
4885 static void
4886 read_import_dir (wfl)
4887 tree wfl;
4888 {
4889 tree package_id = EXPR_WFL_NODE (wfl);
4890 char *package_name = IDENTIFIER_POINTER (package_id);
4891 int package_length = IDENTIFIER_LENGTH (package_id);
4892 DIR *dirp = NULL;
4893 JCF jcfr, *jcf, *saved_jcf = current_jcf;
4894
4895 int found = 0;
4896 int k;
4897 void *entry;
4898 struct buffer filename[1];
4899
4900
4901 if (IS_AN_IMPORT_ON_DEMAND_P (package_id))
4902 return;
4903 IS_AN_IMPORT_ON_DEMAND_P (package_id) = 1;
4904
4905 BUFFER_INIT (filename);
4906 buffer_grow (filename, package_length + 100);
4907
4908 for (entry = jcf_path_start (); entry != NULL; entry = jcf_path_next (entry))
4909 {
4910 char *entry_name = jcf_path_name (entry);
4911 int entry_length = strlen (entry_name);
4912 if (jcf_path_is_zipfile (entry))
4913 {
4914 ZipFile *zipf;
4915 buffer_grow (filename, entry_length);
4916 memcpy (filename->data, entry_name, entry_length - 1);
4917 filename->data[entry_length-1] = '\0';
4918 zipf = opendir_in_zip (filename->data, jcf_path_is_system (entry));
4919 if (zipf == NULL)
4920 error ("malformed .zip archive in CLASSPATH: %s", entry_name);
4921 else
4922 {
4923 ZipDirectory *zipd = (ZipDirectory *) zipf->central_directory;
4924 BUFFER_RESET (filename);
4925 for (k = 0; k < package_length; k++)
4926 {
4927 char ch = package_name[k];
4928 *filename->ptr++ = ch == '.' ? '/' : ch;
4929 }
4930 *filename->ptr++ = '/';
4931
4932 for (; k < zipf->count; k++, zipd = ZIPDIR_NEXT (zipd))
4933 {
4934 char *current_entry = ZIPDIR_FILENAME (zipd);
4935 int current_entry_len = zipd->filename_length;
4936
4937 if (strncmp (filename->data, current_entry,
4938 BUFFER_LENGTH (filename)) != 0)
4939 continue;
4940 found += note_possible_classname (current_entry,
4941 current_entry_len);
4942 }
4943 }
4944 }
4945 else
4946 {
4947 BUFFER_RESET (filename);
4948 buffer_grow (filename, entry_length + package_length + 4);
4949 strcpy (filename->data, entry_name);
4950 filename->ptr = filename->data + entry_length;
4951 for (k = 0; k < package_length; k++)
4952 {
4953 char ch = package_name[k];
4954 *filename->ptr++ = ch == '.' ? '/' : ch;
4955 }
4956 *filename->ptr = '\0';
4957
4958 dirp = opendir (filename->data);
4959 if (dirp == NULL)
4960 continue;
4961 *filename->ptr++ = '/';
4962 for (;;)
4963 {
4964 int java_or_class = 0;
4965 int len;
4966 char *d_name;
4967 struct dirent *direntp = readdir (dirp);
4968 if (!direntp)
4969 break;
4970 d_name = direntp->d_name;
4971 len = strlen (direntp->d_name);
4972 buffer_grow (filename, len+1);
4973 strcpy (filename->ptr, d_name);
4974 found += note_possible_classname (filename->data + entry_length,
4975 package_length+len+1);
4976 }
4977 if (dirp)
4978 closedir (dirp);
4979 }
4980 }
4981
4982 free (filename->data);
4983
4984 /* Here we should have a unified way of retrieving an entry, to be
4985 indexed. */
4986 if (!found)
4987 {
4988 static int first = 1;
4989 if (first)
4990 {
4991 char buffer [256];
4992 sprintf (buffer, "Can't find default package `%s'. Check "
4993 "the CLASSPATH environment variable and the access to the "
4994 "archives.", package_name);
4995 error (buffer);
4996 java_error_count++;
4997 first = 0;
4998 }
4999 else
5000 parse_error_context (wfl, "Package `%s' not found in import",
5001 package_name);
5002 current_jcf = saved_jcf;
5003 return;
5004 }
5005 current_jcf = saved_jcf;
5006 }
5007
5008 /* Possibly find a type in the import on demands specified
5009 types. Returns 1 if an error occured, 0 otherwise. Run throught the
5010 entire list, to detected potential double definitions. */
5011
5012 static int
5013 find_in_imports_on_demand (class_type)
5014 tree class_type;
5015 {
5016 tree node, import, node_to_use;
5017 int seen_once = -1;
5018 tree cl;
5019
5020 for (import = ctxp->import_demand_list; import; import = TREE_CHAIN (import))
5021 {
5022 char *id_name;
5023 obstack_grow (&temporary_obstack,
5024 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))),
5025 IDENTIFIER_LENGTH (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5026 obstack_1grow (&temporary_obstack, '.');
5027 obstack_grow0 (&temporary_obstack,
5028 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5029 IDENTIFIER_LENGTH (TYPE_NAME (class_type)));
5030 id_name = obstack_finish (&temporary_obstack);
5031
5032 node = maybe_get_identifier (id_name);
5033 if (node && IS_A_CLASSFILE_NAME (node))
5034 {
5035 if (seen_once < 0)
5036 {
5037 cl = TREE_PURPOSE (import);
5038 seen_once = 1;
5039 node_to_use = node;
5040 }
5041 else
5042 {
5043 seen_once++;
5044 parse_error_context
5045 (import, "Type `%s' also potentially defined in package `%s'",
5046 IDENTIFIER_POINTER (TYPE_NAME (class_type)),
5047 IDENTIFIER_POINTER (EXPR_WFL_NODE (TREE_PURPOSE (import))));
5048 }
5049 }
5050 }
5051
5052 if (seen_once == 1)
5053 {
5054 /* Setup lineno so that it refers to the line of the import (in
5055 case we parse a class file and encounter errors */
5056 tree decl;
5057 int saved_lineno = lineno;
5058 lineno = EXPR_WFL_LINENO (cl);
5059 TYPE_NAME (class_type) = node_to_use;
5060 QUALIFIED_P (TYPE_NAME (class_type)) = 1;
5061 decl = IDENTIFIER_CLASS_VALUE (TYPE_NAME (class_type));
5062 /* If there is no DECL set for the class or if the class isn't
5063 loaded and not seen in source yet, the load */
5064 if (!decl || (!CLASS_LOADED_P (TREE_TYPE (decl))
5065 && !CLASS_FROM_SOURCE_P (TREE_TYPE (decl))))
5066 load_class (node_to_use, 0);
5067 lineno = saved_lineno;
5068 return check_pkg_class_access (TYPE_NAME (class_type), cl);
5069 }
5070 else
5071 return (seen_once < 0 ? 0 : seen_once); /* It's ok not to have found */
5072 }
5073
5074 static tree
5075 resolve_package (pkg, next)
5076 tree pkg, *next;
5077 {
5078 tree type_name = NULL_TREE;
5079 char *name = IDENTIFIER_POINTER (EXPR_WFL_NODE (pkg));
5080
5081 /* The trick is to determine when the package name stops and were
5082 the name of something contained in the package starts. Then we
5083 return a fully qualified name of what we want to get. */
5084
5085 /* Do a quick search on well known package names */
5086 if (!strncmp (name, "java.lang.reflect", 17))
5087 {
5088 *next =
5089 TREE_CHAIN (TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg))));
5090 type_name = lookup_package_type (name, 17);
5091 }
5092 else if (!strncmp (name, "java.lang", 9))
5093 {
5094 *next = TREE_CHAIN (TREE_CHAIN (EXPR_WFL_QUALIFICATION (pkg)));
5095 type_name = lookup_package_type (name, 9);
5096 }
5097 else
5098 return NULL_TREE; /* FIXME, search all imported packages. */
5099
5100 return type_name;
5101 }
5102
5103 static tree
5104 lookup_package_type (name, from)
5105 char *name;
5106 int from;
5107 {
5108 char subname [128];
5109 char *sub = &name[from+1];
5110 while (*sub != '.' && *sub)
5111 sub++;
5112 strncpy (subname, name, sub-name);
5113 subname [sub-name] = '\0';
5114 return get_identifier (subname);
5115 }
5116
5117 /* Check that CLASS_NAME refers to a PUBLIC class. Return 0 if no
5118 access violations were found, 1 otherwise. */
5119
5120 static int
5121 check_pkg_class_access (class_name, cl)
5122 tree class_name;
5123 tree cl;
5124 {
5125 tree type;
5126
5127 if (!QUALIFIED_P (class_name) || !IDENTIFIER_CLASS_VALUE (class_name))
5128 return 0;
5129
5130 if (!(type = TREE_TYPE (IDENTIFIER_CLASS_VALUE (class_name))))
5131 return 0;
5132
5133 if (!CLASS_PUBLIC (TYPE_NAME (type)))
5134 {
5135 parse_error_context
5136 (cl, "Can't access %s `%s'. Only public classes and interfaces in "
5137 "other packages can be accessed",
5138 (CLASS_INTERFACE (TYPE_NAME (type)) ? "interface" : "class"),
5139 IDENTIFIER_POINTER (class_name));
5140 return 1;
5141 }
5142 return 0;
5143 }
5144
5145 /* Local variable declaration. */
5146
5147 static void
5148 declare_local_variables (modifier, type, vlist)
5149 int modifier;
5150 tree type;
5151 tree vlist;
5152 {
5153 tree decl, current, saved_type;
5154 tree type_wfl = NULL_TREE;
5155 int must_chain = 0;
5156
5157 /* Push a new block if statement were seen between the last time we
5158 pushed a block and now. Keep a cound of block to close */
5159 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (current_function_decl)))
5160 {
5161 tree body = DECL_FUNCTION_BODY (current_function_decl);
5162 tree b = enter_block ();
5163 BLOCK_EXPR_ORIGIN(b) = body;
5164 }
5165
5166 if (modifier)
5167 {
5168 int i;
5169 for (i = 0; i <= 10; i++) if (1 << i & modifier) break;
5170 if (modifier == ACC_FINAL)
5171 {
5172 if (flag_static_local_jdk1_1)
5173 parse_warning_context (ctxp->modifier_ctx [i],
5174 "Unsupported JDK1.1 `final' local variable "
5175 "(treated as non final)");
5176 }
5177 else
5178 {
5179 parse_error_context
5180 (ctxp->modifier_ctx [i],
5181 "Only `final' is allowed as a local variables modifier");
5182 return;
5183 }
5184 }
5185
5186 /* Obtain an incomplete type if TYPE is not complete. TYPE_WFL will
5187 hold the TYPE value if a new incomplete has to be created (as
5188 opposed to being found already existing and reused). */
5189 SET_TYPE_FOR_RESOLUTION (type, type_wfl, must_chain);
5190
5191 /* If TYPE is fully resolved and we don't have a reference, make one */
5192 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5193
5194 /* Go through all the declared variables */
5195 for (current = vlist, saved_type = type; current;
5196 current = TREE_CHAIN (current), type = saved_type)
5197 {
5198 tree other, real_type;
5199 tree wfl = TREE_PURPOSE (current);
5200 tree name = EXPR_WFL_NODE (wfl);
5201 tree init = TREE_VALUE (current);
5202
5203 /* Process NAME, as it may specify extra dimension(s) for it */
5204 type = build_array_from_name (type, type_wfl, name, &name);
5205
5206 /* Variable redefinition check */
5207 if ((other = lookup_name_in_blocks (name)))
5208 {
5209 variable_redefinition_error (wfl, name, TREE_TYPE (other),
5210 DECL_SOURCE_LINE (other));
5211 continue;
5212 }
5213
5214 /* Type adjustment. We may have just readjusted TYPE because
5215 the variable specified more dimensions. Make sure we have
5216 a reference if we can and don't have one already. */
5217 PROMOTE_RECORD_IF_COMPLETE (type, must_chain);
5218
5219 real_type = GET_REAL_TYPE (type);
5220 /* Never layout this decl. This will be done when its scope
5221 will be entered */
5222 decl = build_decl (VAR_DECL, name, real_type);
5223 BLOCK_CHAIN_DECL (decl);
5224
5225 /* Don't try to use an INIT statement when an error was found */
5226 if (init && java_error_count)
5227 init = NULL_TREE;
5228
5229 /* Add the initialization function to the current function's code */
5230 if (init)
5231 {
5232 /* Name might have been readjusted */
5233 EXPR_WFL_NODE (TREE_OPERAND (init, 0)) = name;
5234 MODIFY_EXPR_FROM_INITIALIZATION_P (init) = 1;
5235 java_method_add_stmt (current_function_decl,
5236 build_debugable_stmt (EXPR_WFL_LINECOL (init),
5237 init));
5238 }
5239
5240 /* Setup dependency the type of the decl */
5241 if (must_chain)
5242 {
5243 jdep *dep;
5244 register_incomplete_type (JDEP_VARIABLE, type_wfl, decl, type);
5245 dep = CLASSD_LAST (ctxp->classd_list);
5246 JDEP_GET_PATCH (dep) = &TREE_TYPE (decl);
5247 }
5248 }
5249 SOURCE_FRONTEND_DEBUG (("Defined locals"));
5250 }
5251
5252 /* Called during parsing. Build decls from argument list. */
5253
5254 static void
5255 source_start_java_method (fndecl)
5256 tree fndecl;
5257 {
5258 tree tem;
5259 tree parm_decl;
5260 int i;
5261
5262 current_function_decl = fndecl;
5263
5264 /* New scope for the function */
5265 enter_block ();
5266 for (tem = TYPE_ARG_TYPES (TREE_TYPE (fndecl)), i = 0;
5267 tem != end_params_node; tem = TREE_CHAIN (tem), i++)
5268 {
5269 tree type = TREE_VALUE (tem);
5270 tree name = TREE_PURPOSE (tem);
5271
5272 /* If type is incomplete. Create an incomplete decl and ask for
5273 the decl to be patched later */
5274 if (INCOMPLETE_TYPE_P (type))
5275 {
5276 jdep *jdep;
5277 tree real_type = GET_REAL_TYPE (type);
5278 parm_decl = build_decl (PARM_DECL, name, real_type);
5279 type = obtain_incomplete_type (type);
5280 register_incomplete_type (JDEP_PARM, NULL_TREE, NULL_TREE, type);
5281 jdep = CLASSD_LAST (ctxp->classd_list);
5282 JDEP_MISC (jdep) = name;
5283 JDEP_GET_PATCH (jdep) = &TREE_TYPE (parm_decl);
5284 }
5285 else
5286 parm_decl = build_decl (PARM_DECL, name, type);
5287
5288 BLOCK_CHAIN_DECL (parm_decl);
5289 }
5290 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5291 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl)) =
5292 nreverse (tem);
5293 DECL_ARG_SLOT_COUNT (current_function_decl) = i;
5294 }
5295
5296 /* Called during parsing. Creates an artificial method declaration. */
5297
5298 static tree
5299 create_artificial_method (class, flags, type, name, args)
5300 tree class;
5301 int flags;
5302 tree type, name, args;
5303 {
5304 int saved_lineno = lineno;
5305 tree mdecl;
5306
5307 lineno = 0;
5308 mdecl = make_node (FUNCTION_TYPE);
5309 TREE_TYPE (mdecl) = type;
5310 TYPE_ARG_TYPES (mdecl) = args;
5311 mdecl = add_method (class, flags, name, build_java_signature (mdecl));
5312 lineno = saved_lineno;
5313 DECL_ARTIFICIAL (mdecl) = 1;
5314 return mdecl;
5315 }
5316
5317 /* Starts the body if an artifical method. */
5318
5319 static void
5320 start_artificial_method_body (mdecl)
5321 tree mdecl;
5322 {
5323 DECL_SOURCE_LINE (mdecl) = 1;
5324 DECL_SOURCE_LINE_MERGE (mdecl, 1);
5325 source_start_java_method (mdecl);
5326 enter_block ();
5327 }
5328
5329 static void
5330 end_artificial_method_body (mdecl)
5331 tree mdecl;
5332 {
5333 BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (mdecl)) = exit_block ();
5334 exit_block ();
5335 }
5336
5337 /* Called during expansion. Push decls formerly built from argument
5338 list so they're usable during expansion. */
5339
5340 static void
5341 expand_start_java_method (fndecl)
5342 tree fndecl;
5343 {
5344 tree tem, *ptr;
5345
5346 current_function_decl = fndecl;
5347
5348 announce_function (fndecl);
5349 pushlevel (1); /* Push parameters */
5350 ptr = &DECL_ARGUMENTS (fndecl);
5351 tem = BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (current_function_decl));
5352 while (tem)
5353 {
5354 tree next = TREE_CHAIN (tem);
5355 tree type = TREE_TYPE (tem);
5356 #ifdef PROMOTE_PROTOTYPES
5357 if (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)
5358 && INTEGRAL_TYPE_P (type))
5359 type = integer_type_node;
5360 #endif
5361 DECL_ARG_TYPE (tem) = type;
5362 layout_decl (tem, 0);
5363 pushdecl (tem);
5364 INITIALIZED_P (tem) = 1; /* Parms are initialized */
5365 *ptr = tem;
5366 ptr = &TREE_CHAIN (tem);
5367 tem = next;
5368 }
5369 *ptr = NULL_TREE;
5370 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5371 lineno = DECL_SOURCE_LINE_FIRST (fndecl);
5372 complete_start_java_method (fndecl);
5373 }
5374
5375 /* Terminate a function and expand its body. */
5376
5377 static void
5378 source_end_java_method ()
5379 {
5380 tree fndecl = current_function_decl;
5381
5382 java_parser_context_save_global ();
5383 lineno = ctxp->last_ccb_indent1;
5384
5385 /* Set EH language codes */
5386 java_set_exception_lang_code ();
5387
5388 /* Generate function's code */
5389 if (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl))
5390 && ! flag_emit_class_files)
5391 expand_expr_stmt (BLOCK_EXPR_BODY (DECL_FUNCTION_BODY (fndecl)));
5392
5393 /* pop out of its parameters */
5394 pushdecl_force_head (DECL_ARGUMENTS (fndecl));
5395 poplevel (1, 0, 1);
5396 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
5397
5398 /* Generate rtl for function exit. */
5399 if (! flag_emit_class_files)
5400 {
5401 lineno = DECL_SOURCE_LINE_LAST (fndecl);
5402 /* Emit catch-finally clauses */
5403 emit_handlers ();
5404 expand_function_end (input_filename, lineno, 0);
5405
5406 /* Run the optimizers and output assembler code for this function. */
5407 rest_of_compilation (fndecl);
5408 }
5409
5410 current_function_decl = NULL_TREE;
5411 /* permanent_allocation (1); */
5412 java_parser_context_restore_global ();
5413 }
5414
5415 /* Record EXPR in the current function block. Complements compound
5416 expression second operand if necessary. */
5417
5418 tree
5419 java_method_add_stmt (fndecl, expr)
5420 tree fndecl, expr;
5421 {
5422 return add_stmt_to_block (DECL_FUNCTION_BODY (fndecl), NULL_TREE, expr);
5423 }
5424
5425 static tree
5426 add_stmt_to_block (b, type, stmt)
5427 tree b, type, stmt;
5428 {
5429 tree body = BLOCK_EXPR_BODY (b), c;
5430
5431 if (java_error_count)
5432 return body;
5433
5434 if ((c = add_stmt_to_compound (body, type, stmt)) == body)
5435 return body;
5436
5437 BLOCK_EXPR_BODY (b) = c;
5438 TREE_SIDE_EFFECTS (c) = 1;
5439 return c;
5440 }
5441
5442 /* Add STMT to EXISTING if possible, otherwise create a new
5443 COMPOUND_EXPR and add STMT to it. */
5444
5445 static tree
5446 add_stmt_to_compound (existing, type, stmt)
5447 tree existing, type, stmt;
5448 {
5449 if (existing)
5450 return build (COMPOUND_EXPR, type, existing, stmt);
5451 else
5452 return stmt;
5453 }
5454
5455 /* Hold THIS for the scope of the current public method decl. */
5456 static tree current_this;
5457
5458 void java_layout_seen_class_methods ()
5459 {
5460 tree previous_list = all_class_list;
5461 tree end = NULL_TREE;
5462 tree current;
5463
5464 while (1)
5465 {
5466 for (current = previous_list;
5467 current != end; current = TREE_CHAIN (current))
5468 layout_class_methods (TREE_TYPE (TREE_VALUE (current)));
5469
5470 if (previous_list != all_class_list)
5471 {
5472 end = previous_list;
5473 previous_list = all_class_list;
5474 }
5475 else
5476 break;
5477 }
5478 }
5479
5480 /* Layout the methods of all classes loaded in one way on an
5481 other. Check methods of source parsed classes. Then reorder the
5482 fields and layout the classes or the type of all source parsed
5483 classes */
5484
5485 void
5486 java_layout_classes ()
5487 {
5488 tree current;
5489
5490 /* Layout the methods of all classes seen so far */
5491 java_layout_seen_class_methods ();
5492 java_parse_abort_on_error ();
5493 all_class_list = NULL_TREE;
5494
5495 /* Then check the methods of all parsed classes */
5496 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5497 if (CLASS_FROM_SOURCE_P (TREE_TYPE (TREE_VALUE (current))))
5498 CHECK_METHODS (TREE_VALUE (current));
5499 java_parse_abort_on_error ();
5500
5501 for (current = ctxp->gclass_list; current; current = TREE_CHAIN (current))
5502 {
5503 current_class = TREE_TYPE (TREE_VALUE (current));
5504
5505 /* Reverse the fields, but leave the dummy field in front.
5506 Fields are already ordered for Object and Class */
5507 if (TYPE_FIELDS (current_class) && current_class != object_type_node
5508 && current_class != class_type_node)
5509 {
5510 /* If the dummy field is there, reverse the right fields and
5511 just layout the type for proper fields offset */
5512 if (!DECL_NAME (TYPE_FIELDS (current_class)))
5513 {
5514 tree fields = TYPE_FIELDS (current_class);
5515 TREE_CHAIN (fields) = nreverse (TREE_CHAIN (fields));
5516 TYPE_SIZE (current_class) = NULL_TREE;
5517 layout_type (current_class);
5518 }
5519 /* We don't have a dummy field, we need to layout the class,
5520 after having reversed the fields */
5521 else
5522 {
5523 TYPE_FIELDS (current_class) =
5524 nreverse (TYPE_FIELDS (current_class));
5525 TYPE_SIZE (current_class) = NULL_TREE;
5526 layout_class (current_class);
5527 }
5528 }
5529 else
5530 layout_class (current_class);
5531
5532 /* From now on, the class is considered completely loaded */
5533 CLASS_LOADED_P (current_class) = 1;
5534
5535 /* Error reported by the caller */
5536 if (java_error_count)
5537 return;
5538 }
5539
5540 /* We might have reloaded classes durign the process of laying out
5541 classes for code generation. We must layout the methods of those
5542 late additions, as constructor checks might use them */
5543 java_layout_seen_class_methods ();
5544 java_parse_abort_on_error ();
5545 }
5546
5547 /* Expand all methods in all registered classes. */
5548
5549 void
5550 java_complete_expand_methods ()
5551 {
5552 tree current;
5553
5554 for (current = ctxp->class_list; current; current = TREE_CHAIN (current))
5555 {
5556 tree class_type = CLASS_TO_HANDLE_TYPE (TREE_TYPE (current));
5557 tree decl;
5558
5559 current_class = TREE_TYPE (current);
5560
5561 /* Initialize a new constant pool */
5562 init_outgoing_cpool ();
5563
5564 /* We want <clinit> (if any) to be processed first. */
5565 decl = tree_last (TYPE_METHODS (class_type));
5566 if (decl && DECL_NAME (decl) == clinit_identifier_node)
5567 {
5568 tree list = nreverse (TYPE_METHODS (class_type));
5569 list = TREE_CHAIN (list);
5570 TREE_CHAIN (decl) = NULL_TREE;
5571 TYPE_METHODS (class_type) = chainon (decl, nreverse (list));
5572 }
5573
5574 /* Don't process function bodies in interfaces */
5575 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5576 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5577 {
5578 current_function_decl = decl;
5579 /* Don't generate debug info on line zero when expanding a
5580 generated constructor. */
5581 if (DECL_CONSTRUCTOR_P (decl) && !DECL_FUNCTION_BODY (decl))
5582 {
5583 /* If we found errors, it's too dangerous to try to generate
5584 and expand a constructor */
5585 if (!java_error_count)
5586 {
5587 restore_line_number_status (1);
5588 java_complete_expand_method (decl);
5589 restore_line_number_status (0);
5590 }
5591 }
5592 else if (METHOD_ABSTRACT (decl) || METHOD_NATIVE (decl))
5593 continue;
5594 else
5595 java_complete_expand_method (decl);
5596 }
5597
5598 /* Now verify constructor circularity (stop after the first one
5599 we find) */
5600 if (!CLASS_INTERFACE (TYPE_NAME (current_class)))
5601 for (decl = TYPE_METHODS (class_type); decl; decl = TREE_CHAIN (decl))
5602 if (DECL_CONSTRUCTOR_P (decl) &&
5603 verify_constructor_circularity (decl, decl))
5604 break;
5605
5606 /* Make the class data, register it and run the rest of decl
5607 compilation on it */
5608 if (!java_error_count)
5609 {
5610 if (flag_emit_class_files)
5611 write_classfile (current_class);
5612 else
5613 finish_class (current_class);
5614 }
5615 }
5616 }
5617
5618 /* Hold a list of catch clauses list. The first element of this list is
5619 the list of the catch clauses of the currently analysed try block. */
5620 static tree currently_caught_type_list;
5621
5622 /* Complete and expand a method. */
5623
5624 static void
5625 java_complete_expand_method (mdecl)
5626 tree mdecl;
5627 {
5628 /* Fix constructors before expanding them */
5629 if (DECL_CONSTRUCTOR_P (mdecl))
5630 fix_constructors (mdecl);
5631
5632 /* Expand functions that have a body */
5633 if (DECL_FUNCTION_BODY (mdecl))
5634 {
5635 tree fbody = DECL_FUNCTION_BODY (mdecl);
5636 tree block_body = BLOCK_EXPR_BODY (fbody);
5637 expand_start_java_method (mdecl);
5638
5639 current_this
5640 = (!METHOD_STATIC (mdecl) ?
5641 BLOCK_EXPR_DECLS (DECL_FUNCTION_BODY (mdecl)) : NULL_TREE);
5642
5643 /* Purge the `throws' list of unchecked exceptions */
5644 purge_unchecked_exceptions (mdecl);
5645
5646 /* Install exceptions thrown with `throws' */
5647 PUSH_EXCEPTIONS (DECL_FUNCTION_THROWS (mdecl));
5648
5649 if (block_body != NULL_TREE)
5650 block_body = java_complete_tree (block_body);
5651 BLOCK_EXPR_BODY (fbody) = block_body;
5652
5653 if ((block_body == NULL_TREE || CAN_COMPLETE_NORMALLY (block_body))
5654 && TREE_CODE (TREE_TYPE (TREE_TYPE (mdecl))) != VOID_TYPE)
5655 missing_return_error (current_function_decl);
5656
5657 /* Don't go any further if we've found error(s) during the
5658 expansion */
5659 if (!java_error_count)
5660 source_end_java_method ();
5661 else
5662 {
5663 pushdecl_force_head (DECL_ARGUMENTS (mdecl));
5664 poplevel (1, 0, 1);
5665 }
5666
5667 /* Pop the exceptions and sanity check */
5668 POP_EXCEPTIONS();
5669 if (currently_caught_type_list)
5670 fatal ("Exception list non empty - java_complete_expand_method");
5671 }
5672 }
5673
5674 /* Craft a body for default constructor. Patch existing constructor
5675 bodies with call to super() and field initialization statements if
5676 necessary. */
5677
5678 static void
5679 fix_constructors (mdecl)
5680 tree mdecl;
5681 {
5682 tree body = DECL_FUNCTION_BODY (mdecl);
5683
5684 if (!body)
5685 {
5686 /* The constructor body must be crafted by hand. It's the
5687 constructor we defined when we realize we didn't have the
5688 CLASSNAME() constructor */
5689
5690 tree compound;
5691
5692 /* It is an error for the compiler to generate a default
5693 constructor if the superclass doesn't have a constructor that
5694 takes no argument */
5695 if (verify_constructor_super ())
5696 {
5697 tree sclass_decl = TYPE_NAME (CLASSTYPE_SUPER (current_class));
5698 char *n = IDENTIFIER_POINTER (DECL_NAME (sclass_decl));
5699 parse_error_context (lookup_cl (TYPE_NAME (current_class)),
5700 "No constructor matching `%s()' found in "
5701 "class `%s'", n, n);
5702 }
5703
5704 start_artificial_method_body (mdecl);
5705
5706 /* We don't generate a super constructor invocation if we're
5707 compiling java.lang.Object. build_super_invocation takes care
5708 of that. */
5709 compound = java_method_add_stmt (mdecl, build_super_invocation ());
5710
5711 end_artificial_method_body (mdecl);
5712 }
5713 /* Search for an explicit constructor invocation */
5714 else
5715 {
5716 int found = 0;
5717 tree main_block = BLOCK_EXPR_BODY (body);
5718 tree compound = NULL_TREE;
5719
5720 while (body)
5721 switch (TREE_CODE (body))
5722 {
5723 case CALL_EXPR:
5724 found = CALL_EXPLICIT_CONSTRUCTOR_P (body);
5725 body = NULL_TREE;
5726 break;
5727 case COMPOUND_EXPR:
5728 case EXPR_WITH_FILE_LOCATION:
5729 body = TREE_OPERAND (body, 0);
5730 break;
5731 case BLOCK:
5732 body = BLOCK_EXPR_BODY (body);
5733 break;
5734 default:
5735 found = 0;
5736 body = NULL_TREE;
5737 }
5738 /* The constructor is missing an invocation of super() */
5739 if (!found)
5740 compound = add_stmt_to_compound (compound, NULL_TREE,
5741 build_super_invocation ());
5742
5743 /* Fix the constructor main block if we're adding extra stmts */
5744 if (compound)
5745 {
5746 compound = add_stmt_to_compound (compound, NULL_TREE,
5747 BLOCK_EXPR_BODY (main_block));
5748 BLOCK_EXPR_BODY (main_block) = compound;
5749 }
5750 }
5751 }
5752
5753 /* Browse constructors in the super class, searching for a constructor
5754 that doesn't take any argument. Return 0 if one is found, 1
5755 otherwise. */
5756
5757 static int
5758 verify_constructor_super ()
5759 {
5760 tree class = CLASSTYPE_SUPER (current_class);
5761 if (!class)
5762 return 0;
5763
5764 if (class)
5765 {
5766 tree mdecl;
5767 for (mdecl = TYPE_METHODS (class); mdecl; mdecl = TREE_CHAIN (mdecl))
5768 {
5769 if (DECL_CONSTRUCTOR_P (mdecl)
5770 && TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (mdecl))) == end_params_node)
5771 return 0;
5772 }
5773 }
5774 return 1;
5775 }
5776
5777 /* Expand finals. */
5778
5779 void
5780 java_expand_finals ()
5781 {
5782 }
5783
5784 /* Generate code for all context remembered for code generation. */
5785
5786 void
5787 java_expand_classes ()
5788 {
5789 java_parse_abort_on_error ();
5790 if (!(ctxp = ctxp_for_generation))
5791 return;
5792 java_layout_classes ();
5793 java_parse_abort_on_error ();
5794
5795 for (; ctxp_for_generation; ctxp_for_generation = ctxp_for_generation->next)
5796 {
5797 ctxp = ctxp_for_generation;
5798 lang_init_source (2); /* Error msgs have method prototypes */
5799 java_complete_expand_methods (); /* Complete and expand method bodies */
5800 java_parse_abort_on_error ();
5801 java_expand_finals (); /* Expand and check the finals */
5802 java_parse_abort_on_error ();
5803 java_check_final (); /* Check unitialized final */
5804 java_parse_abort_on_error ();
5805 }
5806 }
5807
5808 /* Wrap non WFL PRIMARY around a WFL and set EXPR_WFL_QUALIFICATION to
5809 a tree list node containing RIGHT. Fore coming RIGHTs will be
5810 chained to this hook. LOCATION contains the location of the
5811 separating `.' operator. */
5812
5813 static tree
5814 make_qualified_primary (primary, right, location)
5815 tree primary, right;
5816 int location;
5817 {
5818 tree wfl;
5819
5820 /* We want to process THIS . xxx symbolicaly, to keep it consistent
5821 with the way we're processing SUPER. A THIS from a primary as a
5822 different form than a SUPER. Turn THIS into something symbolic */
5823 if (TREE_CODE (primary) == THIS_EXPR)
5824 {
5825 wfl = build_wfl_node (this_identifier_node, input_filename, 0, 0);
5826 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5827 wfl = make_qualified_name (wfl, right, location);
5828 PRIMARY_P (wfl) = 1;
5829 return wfl;
5830 }
5831 /* Other non WFL node are wrapped around a WFL */
5832 else if (TREE_CODE (primary) != EXPR_WITH_FILE_LOCATION)
5833 {
5834 wfl = build_expr_wfl (NULL_TREE, ctxp->filename, 0, 0);
5835 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (primary);
5836 EXPR_WFL_QUALIFICATION (wfl) = build_tree_list (primary, NULL_TREE);
5837 }
5838 else
5839 {
5840 wfl = primary;
5841 if (!EXPR_WFL_QUALIFICATION (primary))
5842 EXPR_WFL_QUALIFICATION (primary) =
5843 build_tree_list (primary, NULL_TREE);
5844 }
5845
5846 EXPR_WFL_LINECOL (right) = location;
5847 chainon (EXPR_WFL_QUALIFICATION (wfl), build_tree_list (right, NULL_TREE));
5848 PRIMARY_P (wfl) = 1;
5849 return wfl;
5850 }
5851
5852 /* Simple merge of two name separated by a `.' */
5853
5854 static tree
5855 merge_qualified_name (left, right)
5856 tree left, right;
5857 {
5858 tree node;
5859 obstack_grow (&temporary_obstack, IDENTIFIER_POINTER (left),
5860 IDENTIFIER_LENGTH (left));
5861 obstack_1grow (&temporary_obstack, '.');
5862 obstack_grow0 (&temporary_obstack, IDENTIFIER_POINTER (right),
5863 IDENTIFIER_LENGTH (right));
5864 node = get_identifier (obstack_base (&temporary_obstack));
5865 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
5866 QUALIFIED_P (node) = 1;
5867 return node;
5868 }
5869
5870 /* Merge the two parts of a qualified name into LEFT. Set the
5871 location information of the resulting node to LOCATION, usually
5872 inherited from the location information of the `.' operator. */
5873
5874 static tree
5875 make_qualified_name (left, right, location)
5876 tree left, right;
5877 int location;
5878 {
5879 tree left_id = EXPR_WFL_NODE (left);
5880 tree right_id = EXPR_WFL_NODE (right);
5881 tree wfl, merge;
5882
5883 merge = merge_qualified_name (left_id, right_id);
5884
5885 /* Left wasn't qualified and is now qualified */
5886 if (!QUALIFIED_P (left_id))
5887 {
5888 tree wfl = build_expr_wfl (left_id, ctxp->filename, 0, 0);
5889 EXPR_WFL_LINECOL (wfl) = EXPR_WFL_LINECOL (left);
5890 EXPR_WFL_QUALIFICATION (left) = build_tree_list (wfl, NULL_TREE);
5891 }
5892
5893 wfl = build_expr_wfl (right_id, ctxp->filename, 0, 0);
5894 EXPR_WFL_LINECOL (wfl) = location;
5895 chainon (EXPR_WFL_QUALIFICATION (left), build_tree_list (wfl, NULL_TREE));
5896
5897 EXPR_WFL_NODE (left) = merge;
5898 return left;
5899 }
5900
5901 /* Extract the last identifier component of the qualified in WFL. The
5902 last identifier is removed from the linked list */
5903
5904 static tree
5905 cut_identifier_in_qualified (wfl)
5906 tree wfl;
5907 {
5908 tree q;
5909 tree previous = NULL_TREE;
5910 for (q = EXPR_WFL_QUALIFICATION (wfl); ; previous = q, q = TREE_CHAIN (q))
5911 if (!TREE_CHAIN (q))
5912 {
5913 if (!previous)
5914 fatal ("Operating on a non qualified qualified WFL - "
5915 "cut_identifier_in_qualified");
5916 TREE_CHAIN (previous) = NULL_TREE;
5917 return TREE_PURPOSE (q);
5918 }
5919 }
5920
5921 /* Resolve the expression name NAME. Return its decl. */
5922
5923 static tree
5924 resolve_expression_name (id, orig)
5925 tree id;
5926 tree *orig;
5927 {
5928 tree name = EXPR_WFL_NODE (id);
5929 tree decl;
5930
5931 /* 6.5.5.1: Simple expression names */
5932 if (!PRIMARY_P (id) && !QUALIFIED_P (name))
5933 {
5934 /* 15.13.1: NAME can appear within the scope of a local variable
5935 declaration */
5936 if ((decl = IDENTIFIER_LOCAL_VALUE (name)))
5937 return decl;
5938
5939 /* 15.13.1: NAME can appear within a class declaration */
5940 else
5941 {
5942 decl = lookup_field_wrapper (current_class, name);
5943 if (decl)
5944 {
5945 int fs = FIELD_STATIC (decl);
5946 /* Instance variable (8.3.1.1) can't appear within
5947 static method, static initializer or initializer for
5948 a static variable. */
5949 if (!fs && METHOD_STATIC (current_function_decl))
5950 {
5951 parse_error_context
5952 (id, "Can't make a static reference to nonstatic variable "
5953 "`%s' in class `%s'",
5954 IDENTIFIER_POINTER (name),
5955 IDENTIFIER_POINTER (DECL_NAME
5956 (TYPE_NAME (current_class))));
5957 return error_mark_node;
5958 }
5959 /* Instance variables can't appear as an argument of
5960 an explicit constructor invocation */
5961 if (!fs && ctxp->explicit_constructor_p)
5962 {
5963 parse_error_context
5964 (id, "Can't reference `%s' before the superclass "
5965 "constructor has been called", IDENTIFIER_POINTER (name));
5966 return error_mark_node;
5967 }
5968
5969 /* Otherwise build what it takes to access the field */
5970 decl = build_field_ref ((fs ? NULL_TREE : current_this),
5971 current_class, name);
5972 if (fs && !flag_emit_class_files)
5973 decl = build_class_init (current_class, decl);
5974 /* We may be asked to save the real field access node */
5975 if (orig)
5976 *orig = decl;
5977 /* And we return what we got */
5978 return decl;
5979 }
5980 /* Fall down to error report on undefined variable */
5981 }
5982 }
5983 /* 6.5.5.2 Qualified Expression Names */
5984 else
5985 {
5986 if (orig)
5987 *orig = NULL_TREE;
5988 qualify_ambiguous_name (id);
5989 /* 15.10.1 Field Access Using a Primary and/or Expression Name */
5990 /* 15.10.2: Accessing Superclass Members using super */
5991 return resolve_field_access (id, NULL, NULL);
5992 }
5993
5994 /* We've got an error here */
5995 parse_error_context (id, "Undefined variable `%s'",
5996 IDENTIFIER_POINTER (name));
5997
5998 return error_mark_node;
5999 }
6000
6001 /* 15.10.1 Field Acess Using a Primary and/or Expression Name.
6002 We return something suitable to generate the field access. We also
6003 return the field decl in FIELD_DECL and its type in FIELD_TYPE. If
6004 recipient's address can be null. */
6005
6006 static tree
6007 resolve_field_access (qual_wfl, field_decl, field_type)
6008 tree qual_wfl;
6009 tree *field_decl, *field_type;
6010 {
6011 int is_static = 0;
6012 tree field_ref;
6013 tree decl, where_found, type_found;
6014
6015 if (resolve_qualified_expression_name (qual_wfl, &decl,
6016 &where_found, &type_found))
6017 return error_mark_node;
6018
6019 /* Resolve the LENGTH field of an array here */
6020 if (DECL_NAME (decl) == length_identifier_node && TYPE_ARRAY_P (type_found)
6021 && ! flag_emit_class_files)
6022 {
6023 tree length = build_java_array_length_access (where_found);
6024 field_ref =
6025 build_java_arraynull_check (type_found, length, int_type_node);
6026 }
6027 /* We might have been trying to resolve field.method(). In which
6028 case, the resolution is over and decl is the answer */
6029 else if (DECL_P (decl) && IDENTIFIER_LOCAL_VALUE (DECL_NAME (decl)) == decl)
6030 field_ref = decl;
6031 else if (DECL_P (decl))
6032 {
6033 int static_final_found = 0;
6034 if (!type_found)
6035 type_found = DECL_CONTEXT (decl);
6036 is_static = DECL_P (decl) && FIELD_STATIC (decl);
6037 if (FIELD_FINAL (decl)
6038 && JPRIMITIVE_TYPE_P (TREE_TYPE (decl))
6039 && DECL_LANG_SPECIFIC (decl)
6040 && DECL_INITIAL (decl))
6041 {
6042 field_ref = DECL_INITIAL (decl);
6043 static_final_found = 1;
6044 }
6045 else
6046 field_ref = build_field_ref ((is_static ? NULL_TREE : where_found),
6047 type_found, DECL_NAME (decl));
6048 if (field_ref == error_mark_node)
6049 return error_mark_node;
6050 if (is_static && !static_final_found && !flag_emit_class_files)
6051 {
6052 field_ref = build_class_init (type_found, field_ref);
6053 /* If the static field was identified by an expression that
6054 needs to be generated, make the field access a compound
6055 expression whose first part of the evaluation of the
6056 field selector part. */
6057 if (where_found && TREE_CODE (where_found) != TYPE_DECL
6058 && TREE_CODE (where_found) != RECORD_TYPE)
6059 {
6060 tree type = QUAL_DECL_TYPE (field_ref);
6061 field_ref = build (COMPOUND_EXPR, type, where_found, field_ref);
6062 }
6063 }
6064 }
6065 else
6066 field_ref = decl;
6067
6068 if (field_decl)
6069 *field_decl = decl;
6070 if (field_type)
6071 *field_type = (QUAL_DECL_TYPE (decl) ?
6072 QUAL_DECL_TYPE (decl) : TREE_TYPE (decl));
6073 return field_ref;
6074 }
6075
6076 /* 6.5.5.2: Qualified Expression Names */
6077
6078 static int
6079 resolve_qualified_expression_name (wfl, found_decl, where_found, type_found)
6080 tree wfl;
6081 tree *found_decl, *type_found, *where_found;
6082 {
6083 int from_type = 0; /* Field search initiated from a type */
6084 int from_super = 0, from_cast = 0;
6085 int previous_call_static = 0;
6086 int is_static;
6087 tree decl = NULL_TREE, type = NULL_TREE, q;
6088 *type_found = *where_found = NULL_TREE;
6089
6090 for (q = EXPR_WFL_QUALIFICATION (wfl); q; q = TREE_CHAIN (q))
6091 {
6092 tree qual_wfl = QUAL_WFL (q);
6093
6094 /* 15.10.1 Field Access Using a Primary */
6095 switch (TREE_CODE (qual_wfl))
6096 {
6097 case CALL_EXPR:
6098 case NEW_CLASS_EXPR:
6099 /* If the access to the function call is a non static field,
6100 build the code to access it. */
6101 if (DECL_P (decl) && !FIELD_STATIC (decl))
6102 {
6103 decl = maybe_access_field (decl, *where_found,
6104 DECL_CONTEXT (decl));
6105 if (decl == error_mark_node)
6106 return 1;
6107 }
6108 /* And code for the function call */
6109 if (complete_function_arguments (qual_wfl))
6110 return 1;
6111 if (from_super && TREE_CODE (qual_wfl) == CALL_EXPR)
6112 CALL_USING_SUPER (qual_wfl) = 1;
6113 *where_found =
6114 patch_method_invocation (qual_wfl, decl, type, &is_static, NULL);
6115 if (*where_found == error_mark_node)
6116 return 1;
6117 *type_found = type = QUAL_DECL_TYPE (*where_found);
6118
6119 /* If the previous call was static and this one is too,
6120 build a compound expression to hold the two (because in
6121 that case, previous function calls aren't transported as
6122 forcoming function's argument. */
6123 if (previous_call_static && is_static)
6124 {
6125 decl = build (COMPOUND_EXPR, type, decl, *where_found);
6126 TREE_SIDE_EFFECTS (decl) = 1;
6127 }
6128 else
6129 {
6130 previous_call_static = is_static;
6131 decl = *where_found;
6132 }
6133 continue;
6134
6135 case CONVERT_EXPR:
6136 *where_found = decl = java_complete_tree (qual_wfl);
6137 if (decl == error_mark_node)
6138 return 1;
6139 *type_found = type = QUAL_DECL_TYPE (decl);
6140 from_cast = 1;
6141 continue;
6142
6143 case CONDITIONAL_EXPR:
6144 case STRING_CST:
6145 *where_found = decl = java_complete_tree (qual_wfl);
6146 if (decl == error_mark_node)
6147 return 1;
6148 *type_found = type = QUAL_DECL_TYPE (decl);
6149 continue;
6150
6151 case ARRAY_REF:
6152 /* If the access to the function call is a non static field,
6153 build the code to access it. */
6154 if (DECL_P (decl) && !FIELD_STATIC (decl))
6155 {
6156 decl = maybe_access_field (decl, *where_found, type);
6157 if (decl == error_mark_node)
6158 return 1;
6159 }
6160 /* And code for the array reference expression */
6161 decl = java_complete_tree (qual_wfl);
6162 if (decl == error_mark_node)
6163 return 1;
6164 type = QUAL_DECL_TYPE (decl);
6165 continue;
6166
6167 default:
6168 /* Fix for -Wall Just go to the next statement. Don't
6169 continue */
6170 }
6171
6172 /* If we fall here, we weren't processing a (static) function call. */
6173 previous_call_static = 0;
6174
6175 /* It can be the keyword THIS */
6176 if (EXPR_WFL_NODE (qual_wfl) == this_identifier_node)
6177 {
6178 if (!current_this)
6179 {
6180 parse_error_context
6181 (wfl, "Keyword `this' used outside allowed context");
6182 return 1;
6183 }
6184 /* We have to generate code for intermediate acess */
6185 *where_found = decl = current_this;
6186 *type_found = type = QUAL_DECL_TYPE (decl);
6187 continue;
6188 }
6189
6190 /* 15.10.2 Accessing Superclass Members using SUPER */
6191 if (EXPR_WFL_NODE (qual_wfl) == super_identifier_node)
6192 {
6193 tree node;
6194 /* Check on the restricted use of SUPER */
6195 if (METHOD_STATIC (current_function_decl)
6196 || current_class == object_type_node)
6197 {
6198 parse_error_context
6199 (wfl, "Keyword `super' used outside allowed context");
6200 return 1;
6201 }
6202 /* Otherwise, treat SUPER as (SUPER_CLASS)THIS */
6203 node = build_cast (EXPR_WFL_LINECOL (qual_wfl),
6204 CLASSTYPE_SUPER (current_class),
6205 build_this (EXPR_WFL_LINECOL (qual_wfl)));
6206 *where_found = decl = java_complete_tree (node);
6207 if (decl == error_mark_node)
6208 return 1;
6209 *type_found = type = QUAL_DECL_TYPE (decl);
6210 from_super = from_type = 1;
6211 continue;
6212 }
6213
6214 /* 15.13.1: Can't search for field name in packages, so we
6215 assume a variable/class name was meant. */
6216 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
6217 {
6218 tree name = resolve_package (wfl, &q);
6219 if (name)
6220 {
6221 *where_found = decl = resolve_no_layout (name, qual_wfl);
6222 /* We wan't to be absolutely that the class is laid
6223 out. We're going to search something inside it. */
6224 *type_found = type = TREE_TYPE (decl);
6225 layout_class (type);
6226 from_type = 1;
6227 /* Should be a list, really. FIXME */
6228 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 1;
6229 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (TREE_CHAIN (q))) = 0;
6230 }
6231 else
6232 {
6233 if (from_super || from_cast)
6234 parse_error_context
6235 ((from_cast ? qual_wfl : wfl),
6236 "No variable `%s' defined in class `%s'",
6237 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6238 lang_printable_name (type, 0));
6239 else
6240 parse_error_context
6241 (qual_wfl, "Undefined variable or class name: `%s'",
6242 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)));
6243 return 1;
6244 }
6245 }
6246
6247 /* We have a type name. It's been already resolved when the
6248 expression was qualified. */
6249 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
6250 {
6251 if (!(decl = QUAL_RESOLUTION (q)))
6252 return 1; /* Error reported already */
6253
6254 if (not_accessible_p (TREE_TYPE (decl), decl, 0))
6255 {
6256 parse_error_context
6257 (qual_wfl, "Can't access %s field `%s.%s' from `%s'",
6258 java_accstring_lookup (get_access_flags_from_decl (decl)),
6259 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6260 IDENTIFIER_POINTER (DECL_NAME (decl)),
6261 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6262 return 1;
6263 }
6264 check_deprecation (qual_wfl, decl);
6265
6266 type = TREE_TYPE (decl);
6267 from_type = 1;
6268 }
6269 /* We resolve and expression name */
6270 else
6271 {
6272 tree field_decl;
6273
6274 /* If there exists an early resolution, use it. That occurs
6275 only once and we know that there are more things to
6276 come. Don't do that when processing something after SUPER
6277 (we need more thing to be put in place below */
6278 if (!from_super && QUAL_RESOLUTION (q))
6279 {
6280 decl = QUAL_RESOLUTION (q);
6281 if (!type)
6282 {
6283 if (!FIELD_STATIC (decl))
6284 *where_found = current_this;
6285 else
6286 {
6287 *where_found = TREE_TYPE (decl);
6288 if (TREE_CODE (*where_found) == POINTER_TYPE)
6289 *where_found = TREE_TYPE (*where_found);
6290 }
6291 }
6292 }
6293
6294 /* We have to search for a field, knowing the type of its
6295 container. The flag FROM_TYPE indicates that we resolved
6296 the last member of the expression as a type name, which
6297 means that for the resolution of this field, we'll look
6298 for other errors than if it was resolved as a member of
6299 an other field. */
6300 else
6301 {
6302 int is_static;
6303 tree field_decl_type; /* For layout */
6304
6305 if (!from_type && !JREFERENCE_TYPE_P (type))
6306 {
6307 parse_error_context
6308 (qual_wfl, "Attempt to reference field `%s' in `%s %s'",
6309 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6310 lang_printable_name (type, 0),
6311 IDENTIFIER_POINTER (DECL_NAME (field_decl)));
6312 return 1;
6313 }
6314
6315 if (!(field_decl =
6316 lookup_field_wrapper (type, EXPR_WFL_NODE (qual_wfl))))
6317 {
6318 parse_error_context
6319 (qual_wfl, "No variable `%s' defined in class `%s'",
6320 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6321 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6322 return 1;
6323 }
6324
6325 /* Layout the type of field_decl, since we may need
6326 it. Don't do primitive types or loaded classes. The
6327 situation of non primitive arrays may not handled
6328 properly here. FIXME */
6329 if (TREE_CODE (TREE_TYPE (field_decl)) == POINTER_TYPE)
6330 field_decl_type = TREE_TYPE (TREE_TYPE (field_decl));
6331 else
6332 field_decl_type = TREE_TYPE (field_decl);
6333 if (!JPRIMITIVE_TYPE_P (field_decl_type)
6334 && !CLASS_LOADED_P (field_decl_type)
6335 && !TYPE_ARRAY_P (field_decl_type))
6336 resolve_and_layout (field_decl_type, NULL_TREE);
6337 if (TYPE_ARRAY_P (field_decl_type))
6338 CLASS_LOADED_P (field_decl_type) = 1;
6339
6340 /* Check on accessibility here */
6341 if (not_accessible_p (type, field_decl, from_super))
6342 {
6343 parse_error_context
6344 (qual_wfl,
6345 "Can't access %s field `%s.%s' from `%s'",
6346 java_accstring_lookup
6347 (get_access_flags_from_decl (field_decl)),
6348 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))),
6349 IDENTIFIER_POINTER (DECL_NAME (field_decl)),
6350 IDENTIFIER_POINTER
6351 (DECL_NAME (TYPE_NAME (current_class))));
6352 return 1;
6353 }
6354 check_deprecation (qual_wfl, field_decl);
6355
6356 /* There are things to check when fields are accessed
6357 from type. There are no restrictions on a static
6358 declaration of the field when it is accessed from an
6359 interface */
6360 is_static = FIELD_STATIC (field_decl);
6361 if (!from_super && from_type
6362 && !TYPE_INTERFACE_P (type) && !is_static)
6363 {
6364 parse_error_context
6365 (qual_wfl, "Can't make a static reference to nonstatic "
6366 "variable `%s' in class `%s'",
6367 IDENTIFIER_POINTER (EXPR_WFL_NODE (qual_wfl)),
6368 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6369 return 1;
6370 }
6371 from_cast = from_super = 0;
6372
6373 /* If we need to generate something to get a proper
6374 handle on what this field is accessed from, do it
6375 now. */
6376 if (!is_static)
6377 {
6378 decl = maybe_access_field (decl, *where_found, *type_found);
6379 if (decl == error_mark_node)
6380 return 1;
6381 }
6382
6383 /* We want to keep the location were found it, and the type
6384 we found. */
6385 *where_found = decl;
6386 *type_found = type;
6387
6388 /* This is the decl found and eventually the next one to
6389 search from */
6390 decl = field_decl;
6391 }
6392 from_type = 0;
6393 type = QUAL_DECL_TYPE (decl);
6394 }
6395 }
6396 *found_decl = decl;
6397 return 0;
6398 }
6399
6400 /* 6.6 Qualified name and access control. Returns 1 if MEMBER (a decl)
6401 can't be accessed from REFERENCE (a record type). */
6402
6403 int not_accessible_p (reference, member, from_super)
6404 tree reference, member;
6405 int from_super;
6406 {
6407 int access_flag = get_access_flags_from_decl (member);
6408
6409 /* Access always granted for members declared public */
6410 if (access_flag & ACC_PUBLIC)
6411 return 0;
6412
6413 /* Check access on protected members */
6414 if (access_flag & ACC_PROTECTED)
6415 {
6416 /* Access granted if it occurs from within the package
6417 containing the class in which the protected member is
6418 declared */
6419 if (class_in_current_package (DECL_CONTEXT (member)))
6420 return 0;
6421
6422 /* If accessed with the form `super.member', then access is granted */
6423 if (from_super)
6424 return 0;
6425
6426 /* Otherwise, access is granted if occuring from the class where
6427 member is declared or a subclass of it */
6428 if (inherits_from_p (reference, current_class))
6429 return 0;
6430 return 1;
6431 }
6432
6433 /* Check access on private members. Access is granted only if it
6434 occurs from within the class in witch it is declared */
6435 if (access_flag & ACC_PRIVATE)
6436 return (current_class == DECL_CONTEXT (member) ? 0 : 1);
6437
6438 /* Default access are permitted only when occuring within the
6439 package in which the type (REFERENCE) is declared. In other words,
6440 REFERENCE is defined in the current package */
6441 if (ctxp->package)
6442 return !class_in_current_package (reference);
6443
6444 /* Otherwise, access is granted */
6445 return 0;
6446 }
6447
6448 /* Test deprecated decl access. */
6449 static void
6450 check_deprecation (wfl, decl)
6451 tree wfl, decl;
6452 {
6453 char *file = DECL_SOURCE_FILE (decl);
6454 /* Complain if the field is deprecated and the file it was defined
6455 in isn't compiled at the same time the file which contains its
6456 use is */
6457 if (DECL_DEPRECATED (decl)
6458 && !IS_A_COMMAND_LINE_FILENAME_P (get_identifier (file)))
6459 {
6460 char the [20];
6461 switch (TREE_CODE (decl))
6462 {
6463 case FUNCTION_DECL:
6464 strcpy (the, "method");
6465 break;
6466 case FIELD_DECL:
6467 strcpy (the, "field");
6468 break;
6469 case TYPE_DECL:
6470 strcpy (the, "class");
6471 break;
6472 default:
6473 fatal ("unexpected DECL code - check_deprecation");
6474 }
6475 parse_warning_context
6476 (wfl, "The %s `%s' in class `%s' has been deprecated",
6477 the, lang_printable_name (decl, 0),
6478 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl)))));
6479 }
6480 }
6481
6482 /* Returns 1 if class was declared in the current package, 0 otherwise */
6483
6484 static int
6485 class_in_current_package (class)
6486 tree class;
6487 {
6488 static tree cache = NULL_TREE;
6489 int qualified_flag;
6490 tree left;
6491
6492 if (cache == class)
6493 return 1;
6494
6495 qualified_flag = QUALIFIED_P (DECL_NAME (TYPE_NAME (class)));
6496
6497 /* If the current package is empty and the name of CLASS is
6498 qualified, class isn't in the current package. If there is a
6499 current package and the name of the CLASS is not qualified, class
6500 isn't in the current package */
6501 if ((!ctxp->package && qualified_flag) || (ctxp->package && !qualified_flag))
6502 return 0;
6503
6504 /* If there is not package and the name of CLASS isn't qualified,
6505 they belong to the same unnamed package */
6506 if (!ctxp->package && !qualified_flag)
6507 return 1;
6508
6509 /* Compare the left part of the name of CLASS with the package name */
6510 breakdown_qualified (&left, NULL, DECL_NAME (TYPE_NAME (class)));
6511 if (ctxp->package == left)
6512 {
6513 cache = class;
6514 return 1;
6515 }
6516 return 0;
6517 }
6518
6519 /* This function may generate code to access DECL from WHERE. This is
6520 done only if certain conditions meet. */
6521
6522 static tree
6523 maybe_access_field (decl, where, type)
6524 tree decl, where, type;
6525 {
6526 if (TREE_CODE (decl) == FIELD_DECL && decl != current_this
6527 && !FIELD_STATIC (decl))
6528 decl = build_field_ref (where ? where : current_this,
6529 (type ? type : DECL_CONTEXT (decl)),
6530 DECL_NAME (decl));
6531 return decl;
6532 }
6533
6534 /* Build a method invocation, by patching PATCH. If non NULL
6535 and according to the situation, PRIMARY and WHERE may be
6536 used. IS_STATIC is set to 1 if the invoked function is static. */
6537
6538 static tree
6539 patch_method_invocation (patch, primary, where, is_static, ret_decl)
6540 tree patch, primary, where;
6541 int *is_static;
6542 tree *ret_decl;
6543 {
6544 tree wfl = TREE_OPERAND (patch, 0);
6545 tree args = TREE_OPERAND (patch, 1);
6546 tree name = EXPR_WFL_NODE (wfl);
6547 tree list;
6548 int is_static_flag = 0;
6549 int is_super_init = 0;
6550
6551 /* Should be overriden if everything goes well. Otherwise, if
6552 something fails, it should keep this value. It stop the
6553 evaluation of a bogus assignment. See java_complete_tree,
6554 MODIFY_EXPR: for the reasons why we sometimes want to keep on
6555 evaluating an assignment */
6556 TREE_TYPE (patch) = error_mark_node;
6557
6558 /* Since lookup functions are messing with line numbers, save the
6559 context now. */
6560 java_parser_context_save_global ();
6561
6562 /* 15.11.1: Compile-Time Step 1: Determine Class or Interface to Search */
6563
6564 /* Resolution of qualified name, excluding constructors */
6565 if (QUALIFIED_P (name) && !CALL_CONSTRUCTOR_P (patch))
6566 {
6567 tree class_decl, identifier, identifier_wfl;
6568 /* Extract the last IDENTIFIER of the qualified
6569 expression. This is a wfl and we will use it's location
6570 data during error report. */
6571 identifier_wfl = cut_identifier_in_qualified (wfl);
6572 identifier = EXPR_WFL_NODE (identifier_wfl);
6573
6574 /* Given the context, IDENTIFIER is syntactically qualified
6575 as a MethodName. We need to qualify what's before */
6576 qualify_ambiguous_name (wfl);
6577
6578 /* Package resolution are erroneous */
6579 if (RESOLVE_PACKAGE_NAME_P (wfl))
6580 {
6581 tree remainder;
6582 breakdown_qualified (&remainder, NULL, EXPR_WFL_NODE (wfl));
6583 parse_error_context (wfl, "Can't search method `%s' in package "
6584 "`%s'",IDENTIFIER_POINTER (identifier),
6585 IDENTIFIER_POINTER (remainder));
6586 PATCH_METHOD_RETURN_ERROR ();
6587 }
6588 /* We're resolving a call from a type */
6589 else if (RESOLVE_TYPE_NAME_P (wfl))
6590 {
6591 tree decl = QUAL_RESOLUTION (EXPR_WFL_QUALIFICATION (wfl));
6592 tree name = DECL_NAME (decl);
6593 tree type;
6594
6595 class_decl = resolve_and_layout (name, wfl);
6596 if (CLASS_INTERFACE (decl))
6597 {
6598 parse_error_context
6599 (identifier_wfl, "Can't make static reference to method "
6600 "`%s' in interface `%s'", IDENTIFIER_POINTER (identifier),
6601 IDENTIFIER_POINTER (name));
6602 PATCH_METHOD_RETURN_ERROR ();
6603 }
6604 /* Look the method up in the type selector. The method ought
6605 to be static. */
6606 type = TREE_TYPE (class_decl);
6607 list = lookup_method_invoke (0, wfl, type, identifier, args);
6608 if (list && !METHOD_STATIC (list))
6609 {
6610 char *fct_name = strdup (lang_printable_name (list, 0));
6611 parse_error_context
6612 (identifier_wfl,
6613 "Can't make static reference to method `%s %s' in class `%s'",
6614 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6615 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
6616 free (fct_name);
6617 PATCH_METHOD_RETURN_ERROR ();
6618 }
6619 args = nreverse (args);
6620 }
6621 /* We're resolving an expression name */
6622 else
6623 {
6624 tree field, type;
6625
6626 /* 1- Find the field to which the call applies */
6627 field = resolve_field_access (wfl, NULL, &type);
6628 if (field == error_mark_node)
6629 PATCH_METHOD_RETURN_ERROR ();
6630 /* field is used in lieu of a primary. It alows us not to
6631 report errors on erroneous use of `this' in
6632 constructors. */
6633 primary = field;
6634
6635 /* 2- Do the layout of the class where the last field
6636 was found, so we can search it. */
6637 class_decl = resolve_and_layout (type, NULL_TREE);
6638 if (class_decl != NULL_TREE)
6639 type = TREE_TYPE (class_decl);
6640
6641 /* 3- Retrieve a filtered list of method matches, Refine
6642 if necessary. In any cases, point out errors. */
6643 list = lookup_method_invoke (0, identifier_wfl, type,
6644 identifier, args);
6645
6646 /* 4- Add the field as an argument */
6647 args = tree_cons (NULL_TREE, field, nreverse (args));
6648 }
6649
6650 /* IDENTIFIER_WFL will be used to report any problem further */
6651 wfl = identifier_wfl;
6652 }
6653 /* Resolution of simple names, names generated after a primary: or
6654 constructors */
6655 else
6656 {
6657 tree class_to_search;
6658 int lc; /* Looking for Constructor */
6659
6660 /* We search constructor in their target class */
6661 if (CALL_CONSTRUCTOR_P (patch))
6662 {
6663 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6664 class_to_search = EXPR_WFL_NODE (wfl);
6665 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6666 this_identifier_node)
6667 class_to_search = NULL_TREE;
6668 else if (EXPR_WFL_NODE (TREE_OPERAND (patch, 0)) ==
6669 super_identifier_node)
6670 {
6671 is_super_init = 1;
6672 if (CLASSTYPE_SUPER (current_class))
6673 class_to_search =
6674 DECL_NAME (TYPE_NAME (CLASSTYPE_SUPER (current_class)));
6675 else
6676 {
6677 parse_error_context (wfl, "Can't invoke super constructor "
6678 "on java.lang.Object");
6679 PATCH_METHOD_RETURN_ERROR ();
6680 }
6681 }
6682
6683 /* Class to search is NULL if we're searching the current one */
6684 if (class_to_search)
6685 {
6686 class_to_search = resolve_and_layout (class_to_search,
6687 NULL_TREE);
6688 if (!class_to_search)
6689 {
6690 parse_error_context
6691 (wfl, "Class `%s' not found in type declaration",
6692 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6693 PATCH_METHOD_RETURN_ERROR ();
6694 }
6695
6696 /* Can't instantiate an abstract class, but we can
6697 invoke it's constructor. It's use within the `new'
6698 context is denied here. */
6699 if (CLASS_ABSTRACT (class_to_search)
6700 && TREE_CODE (patch) == NEW_CLASS_EXPR)
6701 {
6702 parse_error_context
6703 (wfl, "Class `%s' is an abstract class. It can't be "
6704 "instantiated", IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
6705 PATCH_METHOD_RETURN_ERROR ();
6706 }
6707 class_to_search = TREE_TYPE (class_to_search);
6708 }
6709 else
6710 class_to_search = current_class;
6711 lc = 1;
6712 }
6713 /* This is a regular search in the local class, unless an
6714 alternate class is specified. */
6715 else
6716 {
6717 class_to_search = (where ? where : current_class);
6718 lc = 0;
6719 }
6720
6721 /* NAME is a simple identifier or comes from a primary. Search
6722 in the class whose declaration contain the method being
6723 invoked. */
6724 resolve_and_layout (class_to_search, NULL_TREE);
6725 list = lookup_method_invoke (lc, wfl, class_to_search, name, args);
6726
6727 /* Don't continue if no method were found, as the next statement
6728 can't be executed then. */
6729 if (!list)
6730 PATCH_METHOD_RETURN_ERROR ();
6731
6732 /* Check for static reference if non static methods */
6733 if (check_for_static_method_reference (wfl, patch, list,
6734 class_to_search, primary))
6735 PATCH_METHOD_RETURN_ERROR ();
6736
6737 /* Non static methods are called with the current object extra
6738 argument. If patch a `new TYPE()', the argument is the value
6739 returned by the object allocator. If method is resolved as a
6740 primary, use the primary otherwise use the current THIS. */
6741 args = nreverse (args);
6742 if (!METHOD_STATIC (list) && TREE_CODE (patch) != NEW_CLASS_EXPR)
6743 args = tree_cons (NULL_TREE, primary ? primary : current_this, args);
6744 }
6745
6746 /* Merge point of all resolution schemes. If we have nothing, this
6747 is an error, already signaled */
6748 if (!list)
6749 PATCH_METHOD_RETURN_ERROR ();
6750
6751 /* Check accessibility, position the is_static flag, build and
6752 return the call */
6753 if (not_accessible_p (DECL_CONTEXT (current_function_decl), list, 0))
6754 {
6755 char *fct_name = strdup (lang_printable_name (list, 0));
6756 parse_error_context
6757 (wfl, "Can't access %s method `%s %s.%s' from `%s'",
6758 java_accstring_lookup (get_access_flags_from_decl (list)),
6759 lang_printable_name (TREE_TYPE (TREE_TYPE (list)), 0),
6760 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (list)))),
6761 fct_name, IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
6762 free (fct_name);
6763 PATCH_METHOD_RETURN_ERROR ();
6764 }
6765 check_deprecation (wfl, list);
6766
6767 is_static_flag = METHOD_STATIC (list);
6768
6769 /* In the context of an explicit constructor invocation, we can't
6770 invoke any method relying on `this'. Exceptions are: we're
6771 invoking a static function, primary exists and is not the current
6772 this, we're creating a new object. */
6773 if (ctxp->explicit_constructor_p
6774 && !is_static_flag
6775 && (!primary || primary == current_this)
6776 && (TREE_CODE (patch) != NEW_CLASS_EXPR))
6777 {
6778 parse_error_context
6779 (wfl, "Can't reference `this' before the superclass constructor has "
6780 "been called");
6781 PATCH_METHOD_RETURN_ERROR ();
6782 }
6783 java_parser_context_restore_global ();
6784 if (is_static)
6785 *is_static = is_static_flag;
6786 /* Sometimes, we want the decl of the selected method. Such as for
6787 EH checking */
6788 if (ret_decl)
6789 *ret_decl = list;
6790 patch = patch_invoke (patch, list, args);
6791 if (is_super_init && CLASS_HAS_FINIT_P (current_class))
6792 {
6793 /* Generate the code used to initialize fields declared with an
6794 initialization statement. For now, it returns a call the the
6795 artificial function $finit$, if required. */
6796
6797 tree finit_call =
6798 build_method_invocation (build_expr_wfl (finit_identifier_node,
6799 input_filename, 0, 0),
6800 NULL_TREE);
6801 patch = build (COMPOUND_EXPR, void_type_node, patch,
6802 java_complete_tree (finit_call));
6803 CAN_COMPLETE_NORMALLY (patch) = 1;
6804 }
6805 return patch;
6806 }
6807
6808 /* Check that we're not trying to do a static reference to a method in
6809 non static method. Return 1 if it's the case, 0 otherwise. */
6810
6811 static int
6812 check_for_static_method_reference (wfl, node, method, where, primary)
6813 tree wfl, node, method, where, primary;
6814 {
6815 if (METHOD_STATIC (current_function_decl)
6816 && !METHOD_STATIC (method) && !primary && !CALL_CONSTRUCTOR_P (node))
6817 {
6818 char *fct_name = strdup (lang_printable_name (method, 0));
6819 parse_error_context
6820 (wfl, "Can't make static reference to method `%s %s' in class `%s'",
6821 lang_printable_name (TREE_TYPE (TREE_TYPE (method)), 0), fct_name,
6822 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (where))));
6823 free (fct_name);
6824 return 1;
6825 }
6826 return 0;
6827 }
6828
6829 /* Patch an invoke expression METHOD and ARGS, based on its invocation
6830 mode. */
6831
6832 static tree
6833 patch_invoke (patch, method, args)
6834 tree patch, method, args;
6835 {
6836 tree dtable, func;
6837 tree original_call, t, ta;
6838
6839 /* Last step for args: convert build-in types. If we're dealing with
6840 a new TYPE() type call, the first argument to the constructor
6841 isn't found in the incomming argument list, but delivered by
6842 `new' */
6843 t = TYPE_ARG_TYPES (TREE_TYPE (method));
6844 if (TREE_CODE (patch) == NEW_CLASS_EXPR)
6845 t = TREE_CHAIN (t);
6846 for (ta = args; t != end_params_node && ta;
6847 t = TREE_CHAIN (t), ta = TREE_CHAIN (ta))
6848 if (JPRIMITIVE_TYPE_P (TREE_TYPE (TREE_VALUE (ta))) &&
6849 TREE_TYPE (TREE_VALUE (ta)) != TREE_VALUE (t))
6850 TREE_VALUE (ta) = convert (TREE_VALUE (t), TREE_VALUE (ta));
6851
6852 if (flag_emit_class_files)
6853 func = method;
6854 else
6855 {
6856 tree signature = build_java_signature (TREE_TYPE (method));
6857 switch (invocation_mode (method, CALL_USING_SUPER (patch)))
6858 {
6859 case INVOKE_VIRTUAL:
6860 dtable = invoke_build_dtable (0, args);
6861 func = build_invokevirtual (dtable, method);
6862 break;
6863
6864 case INVOKE_SUPER:
6865 case INVOKE_STATIC:
6866 func = build_known_method_ref (method, TREE_TYPE (method),
6867 DECL_CONTEXT (method),
6868 signature, args);
6869 break;
6870
6871 case INVOKE_INTERFACE:
6872 dtable = invoke_build_dtable (1, args);
6873 func = build_invokeinterface (dtable, DECL_NAME (method), signature);
6874 break;
6875
6876 default:
6877 fatal ("internal error - unknown invocation_mode result");
6878 }
6879
6880 /* Ensure self_type is initialized, (invokestatic). FIXME */
6881 func = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (method)), func);
6882 }
6883
6884 TREE_TYPE (patch) = TREE_TYPE (TREE_TYPE (method));
6885 TREE_OPERAND (patch, 0) = func;
6886 TREE_OPERAND (patch, 1) = args;
6887 original_call = patch;
6888
6889 /* We're processing a `new TYPE ()' form. New is called an its
6890 returned value is the first argument to the constructor. We build
6891 a COMPOUND_EXPR and use saved expression so that the overall NEW
6892 expression value is a pointer to a newly created and initialized
6893 class. */
6894 if (TREE_CODE (original_call) == NEW_CLASS_EXPR)
6895 {
6896 tree class = DECL_CONTEXT (method);
6897 tree c1, saved_new, size, new;
6898 if (flag_emit_class_files)
6899 {
6900 TREE_TYPE (patch) = build_pointer_type (class);
6901 return patch;
6902 }
6903 if (!TYPE_SIZE (class))
6904 safe_layout_class (class);
6905 size = size_in_bytes (class);
6906 new = build (CALL_EXPR, promote_type (class),
6907 build_address_of (alloc_object_node),
6908 tree_cons (NULL_TREE, build_class_ref (class),
6909 build_tree_list (NULL_TREE,
6910 size_in_bytes (class))),
6911 NULL_TREE);
6912 saved_new = save_expr (new);
6913 c1 = build_tree_list (NULL_TREE, saved_new);
6914 TREE_CHAIN (c1) = TREE_OPERAND (original_call, 1);
6915 TREE_OPERAND (original_call, 1) = c1;
6916 TREE_SET_CODE (original_call, CALL_EXPR);
6917 patch = build (COMPOUND_EXPR, TREE_TYPE (new), patch, saved_new);
6918 }
6919 return patch;
6920 }
6921
6922 static int
6923 invocation_mode (method, super)
6924 tree method;
6925 int super;
6926 {
6927 int access = get_access_flags_from_decl (method);
6928
6929 if (super)
6930 return INVOKE_SUPER;
6931
6932 if (access & ACC_STATIC || access & ACC_FINAL || access & ACC_PRIVATE)
6933 return INVOKE_STATIC;
6934
6935 if (CLASS_FINAL (TYPE_NAME (DECL_CONTEXT (method))))
6936 return INVOKE_STATIC;
6937
6938 if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (method))))
6939 return INVOKE_INTERFACE;
6940
6941 if (DECL_CONSTRUCTOR_P (method))
6942 return INVOKE_STATIC;
6943
6944 return INVOKE_VIRTUAL;
6945 }
6946
6947 /* Retrieve a refined list of matching methods. It covers the step
6948 15.11.2 (Compile-Time Step 2) */
6949
6950 static tree
6951 lookup_method_invoke (lc, cl, class, name, arg_list)
6952 int lc;
6953 tree cl;
6954 tree class, name, arg_list;
6955 {
6956 tree atl = end_params_node; /* Arg Type List */
6957 tree method, signature, list, node;
6958 char *candidates; /* Used for error report */
6959
6960 /* Fix the arguments */
6961 for (node = arg_list; node; node = TREE_CHAIN (node))
6962 {
6963 tree current_arg = TREE_VALUE (node);
6964 /* Integer constant 0 passed as itself, not as a type */
6965 if (current_arg != integer_zero_node)
6966 current_arg = TREE_TYPE (TREE_VALUE (node));
6967 /* Non primitive type may have to be resolved */
6968 if (current_arg != integer_zero_node
6969 && !JPRIMITIVE_TYPE_P (current_arg))
6970 resolve_and_layout (current_arg, NULL_TREE);
6971 /* And promoted */
6972 if (TREE_CODE (current_arg) == RECORD_TYPE)
6973 current_arg = promote_type (current_arg);
6974 atl = tree_cons (NULL_TREE, current_arg, atl);
6975 }
6976
6977 /* Find all candidates and then refine the list, searching for the
6978 most specific method. */
6979 list = find_applicable_accessible_methods_list (lc, class, name, atl);
6980 list = find_most_specific_methods_list (list);
6981 if (list && !TREE_CHAIN (list))
6982 return TREE_VALUE (list);
6983
6984 /* Issue an error. List candidates if any. Candidates are listed
6985 only if accessible (non accessible methods may end-up here for
6986 the sake of a better error report). */
6987 candidates = NULL;
6988 if (list)
6989 {
6990 tree current;
6991 obstack_grow (&temporary_obstack, ". Candidates are:\n", 18);
6992 for (current = list; current; current = TREE_CHAIN (current))
6993 {
6994 tree cm = TREE_VALUE (current);
6995 char string [4096];
6996 if (!cm || not_accessible_p (class, cm, 0))
6997 continue;
6998 sprintf
6999 (string, " `%s' in `%s'%s",
7000 get_printable_method_name (cm),
7001 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (DECL_CONTEXT (cm)))),
7002 (TREE_CHAIN (current) ? "\n" : ""));
7003 obstack_grow (&temporary_obstack, string, strlen (string));
7004 }
7005 obstack_1grow (&temporary_obstack, '\0');
7006 candidates = obstack_finish (&temporary_obstack);
7007 }
7008 /* Issue the error message */
7009 for (node = atl; node; node = TREE_CHAIN (node))
7010 if (TREE_VALUE (node) == integer_zero_node)
7011 TREE_VALUE (node) = long_type_node;
7012 method = make_node (FUNCTION_TYPE);
7013 TYPE_ARG_TYPES (method) = atl;
7014 signature = build_java_argument_signature (method);
7015 parse_error_context (cl, "Can't find %s `%s(%s)' in class `%s'%s",
7016 (lc ? "constructor" : "method"),
7017 (lc ?
7018 IDENTIFIER_POINTER(DECL_NAME (TYPE_NAME (class))) :
7019 IDENTIFIER_POINTER (name)),
7020 IDENTIFIER_POINTER (signature),
7021 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class))),
7022 (candidates ? candidates : ""));
7023 return NULL_TREE;
7024 }
7025
7026 /* 15.11.2.1: Find Methods that are Applicable and Accessible. LC is 1
7027 when we're looking for a constructor. */
7028
7029 static tree
7030 find_applicable_accessible_methods_list (lc, class, name, arglist)
7031 int lc;
7032 tree class, name, arglist;
7033 {
7034 tree method;
7035 tree list = NULL_TREE, all_list = NULL_TREE;
7036
7037 while (class != NULL_TREE)
7038 {
7039 for (method = TYPE_METHODS (class);
7040 method != NULL_TREE; method = TREE_CHAIN (method))
7041 {
7042 if (lc && !DECL_CONSTRUCTOR_P (method))
7043 continue;
7044 else if (!lc && (DECL_CONSTRUCTOR_P (method)
7045 || (GET_METHOD_NAME (method) != name)))
7046 continue;
7047
7048 if (argument_types_convertible (method, arglist))
7049 {
7050 /* Retain accessible methods only */
7051 if (!not_accessible_p (DECL_CONTEXT (current_function_decl),
7052 method, 0))
7053 list = tree_cons (NULL_TREE, method, list);
7054 else
7055 /* Also retain all selected method here */
7056 all_list = tree_cons (NULL_TREE, method, list);
7057 }
7058 }
7059 /* When dealing with constructor, stop here, otherwise search
7060 other classes */
7061 class = (lc ? NULL_TREE : CLASSTYPE_SUPER (class));
7062 }
7063 /* Either return the list obtained or all selected (but
7064 inaccessible) methods for better error report. */
7065 return (!list ? all_list : list);
7066 }
7067
7068 /* 15.11.2.2 Choose the Most Specific Method */
7069
7070 static tree
7071 find_most_specific_methods_list (list)
7072 tree list;
7073 {
7074 int max = 0;
7075 tree current, new_list = NULL_TREE;
7076 for (current = list; current; current = TREE_CHAIN (current))
7077 {
7078 tree method;
7079 DECL_SPECIFIC_COUNT (TREE_VALUE (current)) = 0;
7080
7081 for (method = list; method; method = TREE_CHAIN (method))
7082 {
7083 /* Don't test a method against itself */
7084 if (method == current)
7085 continue;
7086
7087 /* Compare arguments and location where method where declared */
7088 if (argument_types_convertible (TREE_VALUE (method),
7089 TREE_VALUE (current))
7090 && valid_method_invocation_conversion_p
7091 (DECL_CONTEXT (TREE_VALUE (method)),
7092 DECL_CONTEXT (TREE_VALUE (current))))
7093 {
7094 int v = ++DECL_SPECIFIC_COUNT (TREE_VALUE (current));
7095 max = (v > max ? v : max);
7096 }
7097 }
7098 }
7099
7100 /* Review the list and select the maximally specific methods */
7101 for (current = list; current; current = TREE_CHAIN (current))
7102 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7103 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7104
7105 /* If we can't find one, lower expectations and try to gather multiple
7106 maximally specific methods */
7107 while (!new_list)
7108 {
7109 while (--max > 0)
7110 {
7111 if (DECL_SPECIFIC_COUNT (TREE_VALUE (current)) == max)
7112 new_list = tree_cons (NULL_TREE, TREE_VALUE (current), new_list);
7113 }
7114 return new_list;
7115 }
7116
7117 return new_list;
7118 }
7119
7120 /* Make sure that the type of each M2_OR_ARGLIST arguments can be
7121 converted by method invocation conversion (5.3) to the type of the
7122 corresponding parameter of M1. Implementation expects M2_OR_ARGLIST
7123 to change less often than M1. */
7124
7125 static int
7126 argument_types_convertible (m1, m2_or_arglist)
7127 tree m1, m2_or_arglist;
7128 {
7129 static tree m2_arg_value = NULL_TREE;
7130 static tree m2_arg_cache = NULL_TREE;
7131
7132 register tree m1_arg, m2_arg;
7133
7134 m1_arg = TYPE_ARG_TYPES (TREE_TYPE (m1));
7135 if (!METHOD_STATIC (m1))
7136 m1_arg = TREE_CHAIN (m1_arg);
7137
7138 if (m2_arg_value == m2_or_arglist)
7139 m2_arg = m2_arg_cache;
7140 else
7141 {
7142 /* M2_OR_ARGLIST can be a function DECL or a raw list of
7143 argument types */
7144 if (m2_or_arglist && TREE_CODE (m2_or_arglist) == FUNCTION_DECL)
7145 {
7146 m2_arg = TYPE_ARG_TYPES (TREE_TYPE (m2_or_arglist));
7147 if (!METHOD_STATIC (m2_or_arglist))
7148 m2_arg = TREE_CHAIN (m2_arg);
7149 }
7150 else
7151 m2_arg = m2_or_arglist;
7152
7153 m2_arg_value = m2_or_arglist;
7154 m2_arg_cache = m2_arg;
7155 }
7156
7157 while (m1_arg != end_params_node && m2_arg != end_params_node)
7158 {
7159 resolve_and_layout (TREE_VALUE (m1_arg), NULL_TREE);
7160 if (!valid_method_invocation_conversion_p (TREE_VALUE (m1_arg),
7161 TREE_VALUE (m2_arg)))
7162 break;
7163 m1_arg = TREE_CHAIN (m1_arg);
7164 m2_arg = TREE_CHAIN (m2_arg);
7165 }
7166 return m1_arg == end_params_node && m2_arg == end_params_node;
7167 }
7168
7169 /* Qualification routines */
7170
7171 static void
7172 qualify_ambiguous_name (id)
7173 tree id;
7174 {
7175 tree qual, qual_wfl, name, decl, ptr_type, saved_current_class;
7176 int again, super_found = 0, this_found = 0;
7177
7178 /* We first qualify the first element, then derive qualification of
7179 others based on the first one. If the first element is qualified
7180 by a resolution (field or type), this resolution is stored in the
7181 QUAL_RESOLUTION of the qual element being examined. We need to
7182 save the current_class since the use of SUPER might change the
7183 its value. */
7184 saved_current_class = current_class;
7185 qual = EXPR_WFL_QUALIFICATION (id);
7186 do {
7187
7188 /* Simple qualified expression feature a qual_wfl that is a
7189 WFL. Expression derived from a primary feature more complicated
7190 things like a CALL_EXPR. Expression from primary need to be
7191 worked out to extract the part on which the qualification will
7192 take place. */
7193 qual_wfl = QUAL_WFL (qual);
7194 switch (TREE_CODE (qual_wfl))
7195 {
7196 case CALL_EXPR:
7197 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7198 if (TREE_CODE (qual_wfl) != EXPR_WITH_FILE_LOCATION)
7199 {
7200 qual = EXPR_WFL_QUALIFICATION (qual_wfl);
7201 qual_wfl = QUAL_WFL (qual);
7202 }
7203 break;
7204 case NEW_CLASS_EXPR:
7205 case CONVERT_EXPR:
7206 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7207 break;
7208 case ARRAY_REF:
7209 while (TREE_CODE (qual_wfl) == ARRAY_REF)
7210 qual_wfl = TREE_OPERAND (qual_wfl, 0);
7211 break;
7212 default:
7213 /* Fix for -Wall. Just break doing nothing */
7214 break;
7215 }
7216 name = EXPR_WFL_NODE (qual_wfl);
7217 ptr_type = current_class;
7218 again = 0;
7219 /* If we have a THIS (from a primary), we set the context accordingly */
7220 if (name == this_identifier_node)
7221 {
7222 qual = TREE_CHAIN (qual);
7223 qual_wfl = QUAL_WFL (qual);
7224 if (TREE_CODE (qual_wfl) == CALL_EXPR)
7225 again = 1;
7226 else
7227 name = EXPR_WFL_NODE (qual_wfl);
7228 this_found = 1;
7229 }
7230 /* If we have a SUPER, we set the context accordingly */
7231 if (name == super_identifier_node)
7232 {
7233 current_class = CLASSTYPE_SUPER (ptr_type);
7234 /* Check that there is such a thing as a super class. If not,
7235 return. The error will be caught later on, during the
7236 resolution */
7237 if (!current_class)
7238 {
7239 current_class = saved_current_class;
7240 return;
7241 }
7242 qual = TREE_CHAIN (qual);
7243 /* Do one more interation to set things up */
7244 super_found = again = 1;
7245 }
7246 /* Loop one more time if we're dealing with ?: or a string constant */
7247 if (TREE_CODE (qual_wfl) == CONDITIONAL_EXPR
7248 || TREE_CODE (qual_wfl) == STRING_CST)
7249 {
7250 qual = TREE_CHAIN (qual);
7251 qual_wfl = QUAL_WFL (qual);
7252 again = 1;
7253 }
7254 } while (again);
7255
7256 /* If name appears within the scope of a location variable
7257 declaration or parameter declaration, then it is an expression
7258 name. We don't carry this test out if we're in the context of the
7259 use of SUPER or THIS */
7260
7261 if (!this_found && !super_found && (decl = IDENTIFIER_LOCAL_VALUE (name)))
7262 {
7263 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7264 QUAL_RESOLUTION (qual) = decl;
7265 }
7266
7267 /* If within the class/interface NAME was found to be used there
7268 exists a (possibly inherited) field named NAME, then this is an
7269 expression name. */
7270 else if ((decl = lookup_field_wrapper (ptr_type, name)))
7271 {
7272 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7273 QUAL_RESOLUTION (qual) = decl;
7274 }
7275
7276 /* We reclassify NAME as a type name if:
7277 - NAME is a class/interface declared within the compilation
7278 unit containing NAME,
7279 - NAME is imported via a single-type-import declaration,
7280 - NAME is declared in an another compilation unit of the package
7281 of the compilation unit containing NAME,
7282 - NAME is declared by exactly on type-import-on-demand declaration
7283 of the compilation unit containing NAME. */
7284 else if ((decl = resolve_and_layout (name, NULL_TREE)))
7285 {
7286 RESOLVE_TYPE_NAME_P (qual_wfl) = 1;
7287 QUAL_RESOLUTION (qual) = decl;
7288 }
7289
7290 /* Method call are expression name */
7291 else if (TREE_CODE (QUAL_WFL (qual)) == CALL_EXPR
7292 || TREE_CODE (QUAL_WFL (qual)) == ARRAY_REF)
7293 RESOLVE_EXPRESSION_NAME_P (qual_wfl) = 1;
7294
7295 /* Check here that NAME isn't declared by more than one
7296 type-import-on-demand declaration of the compilation unit
7297 containing NAME. FIXME */
7298
7299 /* Otherwise, NAME is reclassified as a package name */
7300 else
7301 RESOLVE_PACKAGE_NAME_P (qual_wfl) = 1;
7302
7303 /* Propagate the qualification accross other components of the
7304 qualified name */
7305 for (qual = TREE_CHAIN (qual); qual;
7306 qual_wfl = QUAL_WFL (qual), qual = TREE_CHAIN (qual))
7307 {
7308 if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7309 RESOLVE_PACKAGE_NAME_P (QUAL_WFL (qual)) = 1;
7310 else
7311 RESOLVE_EXPRESSION_NAME_P (QUAL_WFL (qual)) = 1;
7312 }
7313
7314 /* Store the global qualification for the ambiguous part of ID back
7315 into ID fields */
7316 if (RESOLVE_EXPRESSION_NAME_P (qual_wfl))
7317 RESOLVE_EXPRESSION_NAME_P (id) = 1;
7318 else if (RESOLVE_TYPE_NAME_P (qual_wfl))
7319 RESOLVE_TYPE_NAME_P (id) = 1;
7320 else if (RESOLVE_PACKAGE_NAME_P (qual_wfl))
7321 RESOLVE_PACKAGE_NAME_P (id) = 1;
7322
7323 /* Restore the current class */
7324 current_class = saved_current_class;
7325 }
7326
7327 static int
7328 breakdown_qualified (left, right, source)
7329 tree *left, *right, source;
7330 {
7331 char *p = IDENTIFIER_POINTER (source), *base;
7332 int l = IDENTIFIER_LENGTH (source);
7333
7334 /* Breakdown NAME into REMAINDER . IDENTIFIER */
7335 base = p;
7336 p += (l-1);
7337 while (*p != '.' && p != base)
7338 p--;
7339
7340 /* We didn't find a '.'. Return an error */
7341 if (p == base)
7342 return 1;
7343
7344 *p = '\0';
7345 if (right)
7346 *right = get_identifier (p+1);
7347 *left = get_identifier (IDENTIFIER_POINTER (source));
7348 *p = '.';
7349
7350 return 0;
7351 }
7352
7353 /* Patch tree nodes in a function body. When a BLOCK is found, push
7354 local variable decls if present.
7355 Same as java_complete_lhs, but does resolve static finals to values. */
7356
7357 static tree
7358 java_complete_tree (node)
7359 tree node;
7360 {
7361 node = java_complete_lhs (node);
7362 if (TREE_CODE (node) == VAR_DECL && FIELD_STATIC (node)
7363 && FIELD_FINAL (node) && DECL_INITIAL (node) != NULL_TREE)
7364 {
7365 tree value = DECL_INITIAL (node);
7366 DECL_INITIAL (node) = NULL_TREE;
7367 value = fold_constant_for_init (value, node);
7368 DECL_INITIAL (node) = value;
7369 if (value != NULL_TREE)
7370 return value;
7371 }
7372 return node;
7373 }
7374
7375 /* Patch tree nodes in a function body. When a BLOCK is found, push
7376 local variable decls if present.
7377 Same as java_complete_tree, but does not resolve static finals to values. */
7378
7379 static tree
7380 java_complete_lhs (node)
7381 tree node;
7382 {
7383 tree nn, cn, wfl_op1, wfl_op2, wfl_op3;
7384 int flag;
7385
7386 /* CONVERT_EXPR always has its type set, even though it needs to be
7387 worked out. */
7388 if (TREE_TYPE (node) && TREE_CODE (node) != CONVERT_EXPR)
7389 return node;
7390
7391 /* The switch block implements cases processing container nodes
7392 first. Contained nodes are always written back. Leaves come
7393 next and return a value. */
7394 switch (TREE_CODE (node))
7395 {
7396 case BLOCK:
7397
7398 /* 1- Block section.
7399 Set the local values on decl names so we can identify them
7400 faster when they're referenced. At that stage, identifiers
7401 are legal so we don't check for declaration errors. */
7402 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7403 {
7404 DECL_CONTEXT (cn) = current_function_decl;
7405 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = cn;
7406 INITIALIZED_P (cn) = 0;
7407 }
7408 if (BLOCK_EXPR_BODY (node) == NULL_TREE)
7409 CAN_COMPLETE_NORMALLY (node) = 1;
7410 else
7411 {
7412 tree stmt = BLOCK_EXPR_BODY (node);
7413 tree *ptr;
7414 int error_seen = 0;
7415 if (TREE_CODE (stmt) == COMPOUND_EXPR)
7416 {
7417 /* Re-order from (((A; B); C); ...; Z) to
7418 (A; (B; (C ; (...; Z)))).
7419 This makes it easier to scan the statements left-to-right
7420 without using recursion (which might overflow the stack
7421 if the block has many statements. */
7422 for (;;)
7423 {
7424 tree left = TREE_OPERAND (stmt, 0);
7425 if (TREE_CODE (left) != COMPOUND_EXPR)
7426 break;
7427 TREE_OPERAND (stmt, 0) = TREE_OPERAND (left, 1);
7428 TREE_OPERAND (left, 1) = stmt;
7429 stmt = left;
7430 }
7431 BLOCK_EXPR_BODY (node) = stmt;
7432 }
7433
7434 /* Now do the actual complete, without deep recursion for
7435 long blocks. */
7436 ptr = &BLOCK_EXPR_BODY (node);
7437 while (TREE_CODE (*ptr) == COMPOUND_EXPR)
7438 {
7439 tree cur = java_complete_tree (TREE_OPERAND (*ptr, 0));
7440 tree *next = &TREE_OPERAND (*ptr, 1);
7441 TREE_OPERAND (*ptr, 0) = cur;
7442 if (TREE_CODE (cur) == ERROR_MARK)
7443 error_seen++;
7444 else if (! CAN_COMPLETE_NORMALLY (cur))
7445 {
7446 wfl_op2 = *next;
7447 for (;;)
7448 {
7449 if (TREE_CODE (wfl_op2) == BLOCK)
7450 wfl_op2 = BLOCK_EXPR_BODY (wfl_op2);
7451 else if (TREE_CODE (wfl_op2) == COMPOUND_EXPR)
7452 wfl_op2 = TREE_OPERAND (wfl_op2, 0);
7453 else
7454 break;
7455 }
7456 if (TREE_CODE (wfl_op2) != CASE_EXPR
7457 && TREE_CODE (wfl_op2) != DEFAULT_EXPR
7458 && wfl_op2 != empty_stmt_node)
7459 unreachable_stmt_error (*ptr);
7460 }
7461 ptr = next;
7462 }
7463 *ptr = java_complete_tree (*ptr);
7464
7465 if (TREE_CODE (*ptr) == ERROR_MARK || error_seen > 0)
7466 return error_mark_node;
7467 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (*ptr);
7468 }
7469 /* Turn local bindings to null */
7470 for (cn = BLOCK_EXPR_DECLS (node); cn; cn = TREE_CHAIN (cn))
7471 IDENTIFIER_LOCAL_VALUE (DECL_NAME (cn)) = NULL_TREE;
7472
7473 TREE_TYPE (node) = void_type_node;
7474 break;
7475
7476 /* 2- They are expressions but ultimately deal with statements */
7477
7478 case THROW_EXPR:
7479 wfl_op1 = TREE_OPERAND (node, 0);
7480 COMPLETE_CHECK_OP_0 (node);
7481 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7482 return patch_throw_statement (node, wfl_op1);
7483
7484 case SYNCHRONIZED_EXPR:
7485 wfl_op1 = TREE_OPERAND (node, 0);
7486 return patch_synchronized_statement (node, wfl_op1);
7487
7488 case TRY_EXPR:
7489 return patch_try_statement (node);
7490
7491 case CLEANUP_POINT_EXPR:
7492 COMPLETE_CHECK_OP_0 (node);
7493 TREE_TYPE (node) = void_type_node;
7494 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
7495 return node;
7496
7497 case WITH_CLEANUP_EXPR:
7498 COMPLETE_CHECK_OP_0 (node);
7499 COMPLETE_CHECK_OP_2 (node);
7500 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 0));
7501 TREE_TYPE (node) = void_type_node;
7502 return node;
7503
7504 case LABELED_BLOCK_EXPR:
7505 PUSH_LABELED_BLOCK (node);
7506 if (LABELED_BLOCK_BODY (node))
7507 COMPLETE_CHECK_OP_1 (node);
7508 TREE_TYPE (node) = void_type_node;
7509 POP_LABELED_BLOCK ();
7510 if (CAN_COMPLETE_NORMALLY (LABELED_BLOCK_BODY (node)))
7511 CAN_COMPLETE_NORMALLY (node) = 1;
7512 return node;
7513
7514 case EXIT_BLOCK_EXPR:
7515 /* We don't complete operand 1, because it's the return value of
7516 the EXIT_BLOCK_EXPR which doesn't exist it Java */
7517 return patch_bc_statement (node);
7518
7519 case CASE_EXPR:
7520 cn = java_complete_tree (TREE_OPERAND (node, 0));
7521 if (cn == error_mark_node)
7522 return cn;
7523
7524 /* First, the case expression must be constant */
7525 cn = fold (cn);
7526
7527 if (!TREE_CONSTANT (cn))
7528 {
7529 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7530 parse_error_context (node, "Constant expression required");
7531 return error_mark_node;
7532 }
7533
7534 nn = ctxp->current_loop;
7535
7536 /* It must be assignable to the type of the switch expression. */
7537 if (!try_builtin_assignconv (NULL_TREE,
7538 TREE_TYPE (TREE_OPERAND (nn, 0)), cn))
7539 {
7540 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7541 parse_error_context
7542 (wfl_operator,
7543 "Incompatible type for case. Can't convert `%s' to `int'",
7544 lang_printable_name (TREE_TYPE (cn), 0));
7545 return error_mark_node;
7546 }
7547
7548 cn = fold (convert (int_type_node, cn));
7549
7550 /* Multiple instance of a case label bearing the same
7551 value is checked during code generation. The case
7552 expression is allright so far. */
7553 TREE_OPERAND (node, 0) = cn;
7554 TREE_TYPE (node) = void_type_node;
7555 CAN_COMPLETE_NORMALLY (node) = 1;
7556 TREE_SIDE_EFFECTS (node) = 1;
7557 break;
7558
7559 case DEFAULT_EXPR:
7560 nn = ctxp->current_loop;
7561 /* Only one default label is allowed per switch statement */
7562 if (SWITCH_HAS_DEFAULT (nn))
7563 {
7564 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7565 parse_error_context (wfl_operator,
7566 "Duplicate case label: `default'");
7567 return error_mark_node;
7568 }
7569 else
7570 SWITCH_HAS_DEFAULT (nn) = 1;
7571 TREE_TYPE (node) = void_type_node;
7572 TREE_SIDE_EFFECTS (node) = 1;
7573 CAN_COMPLETE_NORMALLY (node) = 1;
7574 break;
7575
7576 case SWITCH_EXPR:
7577 case LOOP_EXPR:
7578 PUSH_LOOP (node);
7579 /* Check whether the loop was enclosed in a labeled
7580 statement. If not, create one, insert the loop in it and
7581 return the node */
7582 nn = patch_loop_statement (node);
7583
7584 /* Anyways, walk the body of the loop */
7585 if (TREE_CODE (node) == LOOP_EXPR)
7586 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7587 /* Switch statement: walk the switch expression and the cases */
7588 else
7589 node = patch_switch_statement (node);
7590
7591 if (TREE_OPERAND (node, 0) == error_mark_node)
7592 return error_mark_node;
7593 TREE_TYPE (nn) = TREE_TYPE (node) = void_type_node;
7594 /* If we returned something different, that's because we
7595 inserted a label. Pop the label too. */
7596 if (nn != node)
7597 {
7598 if (CAN_COMPLETE_NORMALLY (node))
7599 CAN_COMPLETE_NORMALLY (nn) = 1;
7600 POP_LABELED_BLOCK ();
7601 }
7602 POP_LOOP ();
7603 return nn;
7604
7605 case EXIT_EXPR:
7606 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7607 return patch_exit_expr (node);
7608
7609 case COND_EXPR:
7610 /* Condition */
7611 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7612 if (TREE_OPERAND (node, 0) == error_mark_node)
7613 return error_mark_node;
7614 /* then-else branches */
7615 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7616 if (TREE_OPERAND (node, 1) == error_mark_node)
7617 return error_mark_node;
7618 TREE_OPERAND (node, 2) = java_complete_tree (TREE_OPERAND (node, 2));
7619 if (TREE_OPERAND (node, 2) == error_mark_node)
7620 return error_mark_node;
7621 return patch_if_else_statement (node);
7622 break;
7623
7624 case CONDITIONAL_EXPR:
7625 /* Condition */
7626 wfl_op1 = TREE_OPERAND (node, 0);
7627 COMPLETE_CHECK_OP_0 (node);
7628 wfl_op2 = TREE_OPERAND (node, 1);
7629 COMPLETE_CHECK_OP_1 (node);
7630 wfl_op3 = TREE_OPERAND (node, 2);
7631 COMPLETE_CHECK_OP_2 (node);
7632 return patch_conditional_expr (node, wfl_op1, wfl_op2);
7633
7634 /* 3- Expression section */
7635 case COMPOUND_EXPR:
7636 wfl_op2 = TREE_OPERAND (node, 1);
7637 TREE_OPERAND (node, 0) = nn =
7638 java_complete_tree (TREE_OPERAND (node, 0));
7639 if (! CAN_COMPLETE_NORMALLY (nn) && TREE_CODE (nn) != ERROR_MARK
7640 && TREE_OPERAND (node, 1) != empty_stmt_node)
7641 {
7642 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
7643 parse_error_context (wfl_operator, "Unreachable statement");
7644 }
7645 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
7646 if (TREE_OPERAND (node, 1) == error_mark_node)
7647 return error_mark_node;
7648 TREE_TYPE (node) = TREE_TYPE (TREE_OPERAND (node, 1));
7649 CAN_COMPLETE_NORMALLY (node)
7650 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1));
7651 break;
7652
7653 case RETURN_EXPR:
7654 /* CAN_COMPLETE_NORMALLY (node) = 0; */
7655 return patch_return (node);
7656
7657 case EXPR_WITH_FILE_LOCATION:
7658 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
7659 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
7660 {
7661 node = resolve_expression_name (node, NULL);
7662 CAN_COMPLETE_NORMALLY (node) = 1;
7663 }
7664 else
7665 {
7666 tree body;
7667 int save_lineno = lineno;
7668 lineno = EXPR_WFL_LINENO (node);
7669 body = java_complete_tree (EXPR_WFL_NODE (node));
7670 lineno = save_lineno;
7671 EXPR_WFL_NODE (node) = body;
7672 TREE_SIDE_EFFECTS (node) = 1;
7673 CAN_COMPLETE_NORMALLY (node) = CAN_COMPLETE_NORMALLY (body);
7674 if (EXPR_WFL_NODE (node) == error_mark_node)
7675 {
7676 /* Its important for the evaluation of assignment that
7677 this mark on the TREE_TYPE is propagated. */
7678 TREE_TYPE (node) = error_mark_node;
7679 return error_mark_node;
7680 }
7681 else
7682 TREE_TYPE (node) = TREE_TYPE (EXPR_WFL_NODE (node));
7683
7684 }
7685 break;
7686
7687 case NEW_ARRAY_EXPR:
7688 /* Patch all the dimensions */
7689 flag = 0;
7690 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
7691 {
7692 int location = EXPR_WFL_LINECOL (TREE_VALUE (cn));
7693 tree dim = java_complete_tree (TREE_VALUE (cn));
7694 if (dim == error_mark_node)
7695 {
7696 flag = 1;
7697 continue;
7698 }
7699 else
7700 {
7701 TREE_VALUE (cn) = dim;
7702 /* Setup the location of the current dimension, for
7703 later error report. */
7704 TREE_PURPOSE (cn) =
7705 build_expr_wfl (NULL_TREE, input_filename, 0, 0);
7706 EXPR_WFL_LINECOL (TREE_PURPOSE (cn)) = location;
7707 }
7708 }
7709 /* They complete the array creation expression, if no errors
7710 were found. */
7711 CAN_COMPLETE_NORMALLY (node) = 1;
7712 return (flag ? error_mark_node : patch_newarray (node));
7713
7714 case NEW_CLASS_EXPR:
7715 case CALL_EXPR:
7716 /* Complete function's argument(s) first */
7717 if (complete_function_arguments (node))
7718 return error_mark_node;
7719 else
7720 {
7721 tree decl, wfl = TREE_OPERAND (node, 0);
7722 int in_this = CALL_THIS_CONSTRUCTOR_P (node);
7723
7724 node = patch_method_invocation (node, NULL_TREE,
7725 NULL_TREE, 0, &decl);
7726 if (node == error_mark_node)
7727 return error_mark_node;
7728
7729 check_thrown_exceptions (EXPR_WFL_LINECOL (node), decl);
7730 /* If we call this(...), register signature and positions */
7731 if (in_this)
7732 DECL_CONSTRUCTOR_CALLS (current_function_decl) =
7733 tree_cons (wfl, decl,
7734 DECL_CONSTRUCTOR_CALLS (current_function_decl));
7735 CAN_COMPLETE_NORMALLY (node) = 1;
7736 return node;
7737 }
7738
7739 case MODIFY_EXPR:
7740 /* Save potential wfls */
7741 wfl_op1 = TREE_OPERAND (node, 0);
7742 wfl_op2 = TREE_OPERAND (node, 1);
7743 TREE_OPERAND (node, 0) = java_complete_lhs (wfl_op1);
7744 if (TREE_OPERAND (node, 0) == error_mark_node)
7745 return error_mark_node;
7746
7747 if (COMPOUND_ASSIGN_P (wfl_op2))
7748 {
7749 tree lvalue;
7750 tree other =
7751 java_complete_tree (TREE_OPERAND (wfl_op2, 0));
7752
7753 /* Hand stablize the lhs on both places */
7754 lvalue = stabilize_reference (other);
7755 TREE_OPERAND (node, 0) = lvalue;
7756 TREE_OPERAND (TREE_OPERAND (node, 1), 0) = lvalue;
7757 }
7758
7759 /* If we're about to patch a NEW_ARRAY_INIT, we call a special
7760 function to complete this RHS */
7761 if (TREE_CODE (wfl_op2) == NEW_ARRAY_INIT)
7762 nn = patch_new_array_init (TREE_TYPE (TREE_OPERAND (node, 0)),
7763 TREE_OPERAND (node, 1));
7764 else
7765 nn = java_complete_tree (TREE_OPERAND (node, 1));
7766
7767 /* There are cases where the type of RHS is fixed. In those
7768 cases, if the evaluation of the RHS fails, we further the
7769 evaluation of the assignment to detect more errors. */
7770 if (nn == error_mark_node)
7771 {
7772 /* It's hopeless, but we can further things on to discover
7773 an error during the assignment. In any cases, the
7774 assignment operation fails. */
7775 if (TREE_CODE (TREE_OPERAND (node, 1)) != EXPR_WITH_FILE_LOCATION
7776 && TREE_CODE (TREE_OPERAND (node, 1)) != NEW_ARRAY_INIT
7777 && TREE_TYPE (TREE_OPERAND (node, 1)) != error_mark_node)
7778 patch_assignment (node, wfl_op1, wfl_op2);
7779
7780 /* Now, we still mark the lhs as initialized */
7781 if (DECL_P (TREE_OPERAND (node, 0)))
7782 INITIALIZED_P (TREE_OPERAND (node, 0)) = 1;
7783
7784 return error_mark_node;
7785 }
7786 TREE_OPERAND (node, 1) = nn;
7787
7788 /* In case we're handling = with a String as a RHS, we need to
7789 produce a String out of the RHS (it might still be a
7790 STRING_CST or a StringBuffer at this stage */
7791 if ((nn = patch_string (TREE_OPERAND (node, 1))))
7792 TREE_OPERAND (node, 1) = nn;
7793 node = patch_assignment (node, wfl_op1, wfl_op2);
7794 CAN_COMPLETE_NORMALLY (node) = 1;
7795
7796 /* Before returning the node, in the context of a static field
7797 assignment in <clinit>, we may want to carray further
7798 optimizations. (VAR_DECL means it's a static field. See
7799 add_field. */
7800 if (DECL_NAME (current_function_decl) == clinit_identifier_node
7801 && MODIFY_EXPR_FROM_INITIALIZATION_P (node)
7802 && TREE_CODE (TREE_OPERAND (node, 0)) == VAR_DECL)
7803 node = patch_initialized_static_field (node);
7804
7805 return node;
7806
7807 case MULT_EXPR:
7808 case PLUS_EXPR:
7809 case MINUS_EXPR:
7810 case LSHIFT_EXPR:
7811 case RSHIFT_EXPR:
7812 case URSHIFT_EXPR:
7813 case BIT_AND_EXPR:
7814 case BIT_XOR_EXPR:
7815 case BIT_IOR_EXPR:
7816 case TRUNC_MOD_EXPR:
7817 case RDIV_EXPR:
7818 case TRUTH_ANDIF_EXPR:
7819 case TRUTH_ORIF_EXPR:
7820 case EQ_EXPR:
7821 case NE_EXPR:
7822 case GT_EXPR:
7823 case GE_EXPR:
7824 case LT_EXPR:
7825 case LE_EXPR:
7826 /* Operands 0 and 1 are WFL in certain cases only. patch_binop
7827 knows how to handle those cases. */
7828 wfl_op1 = TREE_OPERAND (node, 0);
7829 wfl_op2 = TREE_OPERAND (node, 1);
7830
7831 CAN_COMPLETE_NORMALLY (node) = 1;
7832 /* Don't complete string nodes if dealing with the PLUS operand. */
7833 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op1))
7834 {
7835 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7836 if (TREE_OPERAND (node, 0) == error_mark_node)
7837 return error_mark_node;
7838 }
7839 if (TREE_CODE (node) != PLUS_EXPR || !JSTRING_P (wfl_op2))
7840 {
7841 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
7842 if (TREE_OPERAND (node, 1) == error_mark_node)
7843 return error_mark_node;
7844 }
7845 return patch_binop (node, wfl_op1, wfl_op2);
7846
7847 case INSTANCEOF_EXPR:
7848 wfl_op1 = TREE_OPERAND (node, 0);
7849 COMPLETE_CHECK_OP_0 (node);
7850 return patch_binop (node, wfl_op1, TREE_OPERAND (node, 1));
7851
7852 case UNARY_PLUS_EXPR:
7853 case NEGATE_EXPR:
7854 case TRUTH_NOT_EXPR:
7855 case BIT_NOT_EXPR:
7856 case PREDECREMENT_EXPR:
7857 case PREINCREMENT_EXPR:
7858 case POSTDECREMENT_EXPR:
7859 case POSTINCREMENT_EXPR:
7860 case CONVERT_EXPR:
7861 /* There are cases were wfl_op1 is a WFL. patch_unaryop knows
7862 how to handle those cases. */
7863 wfl_op1 = TREE_OPERAND (node, 0);
7864 CAN_COMPLETE_NORMALLY (node) = 1;
7865 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7866 if (TREE_OPERAND (node, 0) == error_mark_node)
7867 return error_mark_node;
7868 node = patch_unaryop (node, wfl_op1);
7869 CAN_COMPLETE_NORMALLY (node) = 1;
7870 break;
7871
7872 case ARRAY_REF:
7873 /* There are cases were wfl_op1 is a WFL. patch_array_ref knows
7874 how to handle those cases. */
7875 wfl_op1 = TREE_OPERAND (node, 0);
7876 TREE_OPERAND (node, 0) = java_complete_tree (wfl_op1);
7877 if (TREE_OPERAND (node, 0) == error_mark_node)
7878 return error_mark_node;
7879 if (!flag_emit_class_files)
7880 TREE_OPERAND (node, 0) = save_expr (TREE_OPERAND (node, 0));
7881 /* The same applies to wfl_op2 */
7882 wfl_op2 = TREE_OPERAND (node, 1);
7883 TREE_OPERAND (node, 1) = java_complete_tree (wfl_op2);
7884 if (TREE_OPERAND (node, 1) == error_mark_node)
7885 return error_mark_node;
7886 if (!flag_emit_class_files)
7887 TREE_OPERAND (node, 1) = save_expr (TREE_OPERAND (node, 1));
7888 return patch_array_ref (node);
7889
7890 case RECORD_TYPE:
7891 return node;;
7892
7893 case COMPONENT_REF:
7894 /* The first step in the re-write of qualified name handling. FIXME.
7895 So far, this is only to support PRIMTYPE.class -> PRIMCLASS.TYPE. */
7896 TREE_OPERAND (node, 0) = java_complete_tree (TREE_OPERAND (node, 0));
7897 if (TREE_CODE (TREE_OPERAND (node, 0)) == RECORD_TYPE)
7898 {
7899 tree name = TREE_OPERAND (node, 1);
7900 tree field = lookup_field_wrapper (TREE_OPERAND (node, 0), name);
7901 if (field == NULL_TREE)
7902 {
7903 error ("missing static field `%s'", IDENTIFIER_POINTER (name));
7904 return error_mark_node;
7905 }
7906 if (! FIELD_STATIC (field))
7907 {
7908 error ("not a static field `%s'", IDENTIFIER_POINTER (name));
7909 return error_mark_node;
7910 }
7911 return field;
7912 }
7913 else
7914 fatal ("unimplemented java_complete_tree for COMPONENT_REF");
7915 break;
7916
7917 case THIS_EXPR:
7918 /* Can't use THIS in a static environment */
7919 if (!current_this)
7920 {
7921 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7922 parse_error_context (wfl_operator, "Keyword `this' used outside "
7923 "allowed context");
7924 TREE_TYPE (node) = error_mark_node;
7925 return error_mark_node;
7926 }
7927 if (ctxp->explicit_constructor_p)
7928 {
7929 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
7930 parse_error_context
7931 (wfl_operator, "Can't reference `this' or `super' before the "
7932 "superclass constructor has been called");
7933 TREE_TYPE (node) = error_mark_node;
7934 return error_mark_node;
7935 }
7936 return current_this;
7937
7938 default:
7939 CAN_COMPLETE_NORMALLY (node) = 1;
7940 /* Ok: may be we have a STRING_CST or a crafted `StringBuffer'
7941 and it's time to turn it into the appropriate String object
7942 */
7943 if ((node = patch_string (node)))
7944 return node;
7945 fatal ("No case for tree code `%s' - java_complete_tree\n",
7946 tree_code_name [TREE_CODE (node)]);
7947 }
7948 return node;
7949 }
7950
7951 /* Complete function call's argument. Return a non zero value is an
7952 error was found. */
7953
7954 static int
7955 complete_function_arguments (node)
7956 tree node;
7957 {
7958 int flag = 0;
7959 tree cn;
7960
7961 ctxp->explicit_constructor_p += (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
7962 for (cn = TREE_OPERAND (node, 1); cn; cn = TREE_CHAIN (cn))
7963 {
7964 tree wfl = TREE_VALUE (cn), parm, temp;
7965 parm = java_complete_tree (wfl);
7966 if (parm == error_mark_node)
7967 {
7968 flag = 1;
7969 continue;
7970 }
7971 /* If have a string literal that we haven't transformed yet or a
7972 crafted string buffer, as a result of use of the the String
7973 `+' operator. Build `parm.toString()' and expand it. */
7974 if ((temp = patch_string (parm)))
7975 parm = temp;
7976 /* Inline PRIMTYPE.TYPE read access */
7977 parm = maybe_build_primttype_type_ref (parm, wfl);
7978
7979 TREE_VALUE (cn) = parm;
7980 }
7981 ctxp->explicit_constructor_p -= (CALL_THIS_CONSTRUCTOR_P (node) ? 1 : 0);
7982 return flag;
7983 }
7984
7985 /* Sometimes (for loops and variable initialized during their
7986 declaration), we want to wrap a statement around a WFL and turn it
7987 debugable. */
7988
7989 static tree
7990 build_debugable_stmt (location, stmt)
7991 int location;
7992 tree stmt;
7993 {
7994 if (TREE_CODE (stmt) != EXPR_WITH_FILE_LOCATION)
7995 {
7996 stmt = build_expr_wfl (stmt, input_filename, 0, 0);
7997 EXPR_WFL_LINECOL (stmt) = location;
7998 }
7999 JAVA_MAYBE_GENERATE_DEBUG_INFO (stmt);
8000 return stmt;
8001 }
8002
8003 static tree
8004 build_expr_block (body, decls)
8005 tree body, decls;
8006 {
8007 tree node = make_node (BLOCK);
8008 BLOCK_EXPR_DECLS (node) = decls;
8009 BLOCK_EXPR_BODY (node) = body;
8010 if (body)
8011 TREE_TYPE (node) = TREE_TYPE (body);
8012 TREE_SIDE_EFFECTS (node) = 1;
8013 return node;
8014 }
8015
8016 /* Create a new function block and link it approriately to current
8017 function block chain */
8018
8019 static tree
8020 enter_block ()
8021 {
8022 return (enter_a_block (build_expr_block (NULL_TREE, NULL_TREE)));
8023 }
8024
8025 /* Link block B supercontext to the previous block. The current
8026 function DECL is used as supercontext when enter_a_block is called
8027 for the first time for a given function. The current function body
8028 (DECL_FUNCTION_BODY) is set to be block B. */
8029
8030 static tree
8031 enter_a_block (b)
8032 tree b;
8033 {
8034 tree fndecl = current_function_decl;
8035
8036 if (!DECL_FUNCTION_BODY (fndecl))
8037 {
8038 BLOCK_SUPERCONTEXT (b) = fndecl;
8039 DECL_FUNCTION_BODY (fndecl) = b;
8040 }
8041 else
8042 {
8043 BLOCK_SUPERCONTEXT (b) = DECL_FUNCTION_BODY (fndecl);
8044 DECL_FUNCTION_BODY (fndecl) = b;
8045 }
8046 return b;
8047 }
8048
8049 /* Exit a block by changing the current function body
8050 (DECL_FUNCTION_BODY) to the current block super context, only if
8051 the block being exited isn't the method's top level one. */
8052
8053 static tree
8054 exit_block ()
8055 {
8056 tree b = DECL_FUNCTION_BODY (current_function_decl);
8057
8058 if (BLOCK_SUPERCONTEXT (b) != current_function_decl)
8059 DECL_FUNCTION_BODY (current_function_decl) = BLOCK_SUPERCONTEXT (b);
8060
8061 return b;
8062 }
8063
8064 /* Lookup for NAME in the nested function's blocks, all the way up to
8065 the current toplevel one. It complies with Java's local variable
8066 scoping rules. */
8067
8068 static tree
8069 lookup_name_in_blocks (name)
8070 tree name;
8071 {
8072 tree b = DECL_FUNCTION_BODY (current_function_decl);
8073
8074 while (b != current_function_decl)
8075 {
8076 tree current;
8077
8078 /* Paranoid sanity check. To be removed */
8079 if (TREE_CODE (b) != BLOCK)
8080 fatal ("non block expr function body - lookup_name_in_blocks");
8081
8082 for (current = BLOCK_EXPR_DECLS (b); current;
8083 current = TREE_CHAIN (current))
8084 if (DECL_NAME (current) == name)
8085 return current;
8086 b = BLOCK_SUPERCONTEXT (b);
8087 }
8088 return NULL_TREE;
8089 }
8090
8091 static void
8092 maybe_absorb_scoping_blocks ()
8093 {
8094 while (BLOCK_EXPR_ORIGIN (DECL_FUNCTION_BODY (current_function_decl)))
8095 {
8096 tree b = exit_block ();
8097 java_method_add_stmt (current_function_decl, b);
8098 SOURCE_FRONTEND_DEBUG (("Absorbing scoping block at line %d", lineno));
8099 }
8100 }
8101
8102 \f
8103 /* This section of the source is reserved to build_* functions that
8104 are building incomplete tree nodes and the patch_* functions that
8105 are completing them. */
8106
8107 /* Build a super() constructor invocation. Returns empty_stmt_node if
8108 we're currently dealing with the class java.lang.Object. */
8109
8110 static tree
8111 build_super_invocation ()
8112 {
8113 if (current_class == object_type_node)
8114 return empty_stmt_node;
8115 else
8116 {
8117 tree super_wfl = build_wfl_node (super_identifier_node,
8118 input_filename, 0, 0);
8119 return build_method_invocation (super_wfl, NULL_TREE);
8120 }
8121 }
8122
8123 /* Build a SUPER/THIS qualified method invocation. */
8124
8125 static tree
8126 build_this_super_qualified_invocation (use_this, name, args, lloc, rloc)
8127 int use_this;
8128 tree name, args;
8129 int lloc, rloc;
8130
8131 {
8132 tree invok;
8133 tree wfl =
8134 build_wfl_node ((use_this ? this_identifier_node : super_identifier_node),
8135 input_filename, 0, 0);
8136 EXPR_WFL_LINECOL (wfl) = lloc;
8137 invok = build_method_invocation (name, args);
8138 return make_qualified_primary (wfl, invok, rloc);
8139 }
8140
8141 /* Build an incomplete CALL_EXPR node. */
8142
8143 static tree
8144 build_method_invocation (name, args)
8145 tree name;
8146 tree args;
8147 {
8148 tree call = build (CALL_EXPR, NULL_TREE, name, args, NULL_TREE);
8149 TREE_SIDE_EFFECTS (call) = 1;
8150 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8151 return call;
8152 }
8153
8154 /* Build an incomplete new xxx(...) node. */
8155
8156 static tree
8157 build_new_invocation (name, args)
8158 tree name, args;
8159 {
8160 tree call = build (NEW_CLASS_EXPR, NULL_TREE, name, args, NULL_TREE);
8161 TREE_SIDE_EFFECTS (call) = 1;
8162 EXPR_WFL_LINECOL (call) = EXPR_WFL_LINECOL (name);
8163 return call;
8164 }
8165
8166 /* Build an incomplete assignment expression. */
8167
8168 static tree
8169 build_assignment (op, op_location, lhs, rhs)
8170 int op, op_location;
8171 tree lhs, rhs;
8172 {
8173 tree assignment;
8174 /* Build the corresponding binop if we deal with a Compound
8175 Assignment operator. Mark the binop sub-tree as part of a
8176 Compound Assignment expression */
8177 if (op != ASSIGN_TK)
8178 {
8179 rhs = build_binop (BINOP_LOOKUP (op), op_location, lhs, rhs);
8180 COMPOUND_ASSIGN_P (rhs) = 1;
8181 }
8182 assignment = build (MODIFY_EXPR, NULL_TREE, lhs, rhs);
8183 TREE_SIDE_EFFECTS (assignment) = 1;
8184 EXPR_WFL_LINECOL (assignment) = op_location;
8185 return assignment;
8186 }
8187
8188 /* Print an INTEGER_CST node in a static buffer, and return the buffer. */
8189
8190 char *
8191 print_int_node (node)
8192 tree node;
8193 {
8194 static char buffer [80];
8195 if (TREE_CONSTANT_OVERFLOW (node))
8196 sprintf (buffer, "<overflow>");
8197
8198 if (TREE_INT_CST_HIGH (node) == 0)
8199 sprintf (buffer, HOST_WIDE_INT_PRINT_UNSIGNED,
8200 TREE_INT_CST_LOW (node));
8201 else if (TREE_INT_CST_HIGH (node) == -1
8202 && TREE_INT_CST_LOW (node) != 0)
8203 {
8204 buffer [0] = '-';
8205 sprintf (&buffer [1], HOST_WIDE_INT_PRINT_UNSIGNED,
8206 -TREE_INT_CST_LOW (node));
8207 }
8208 else
8209 sprintf (buffer, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
8210 TREE_INT_CST_HIGH (node), TREE_INT_CST_LOW (node));
8211
8212 return buffer;
8213 }
8214
8215 /* Return 1 if you an assignment of a FINAL is attempted */
8216
8217 static int
8218 check_final_assignment (lvalue, wfl)
8219 tree lvalue, wfl;
8220 {
8221 if (DECL_P (lvalue) && FIELD_FINAL (lvalue) &&
8222 DECL_NAME (current_function_decl) != clinit_identifier_node)
8223 {
8224 parse_error_context
8225 (wfl, "Can't assign a value to the final variable `%s'",
8226 IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl)));
8227 return 1;
8228 }
8229 return 0;
8230 }
8231
8232 /* Inline references to java.lang.PRIMTYPE.TYPE when accessed in
8233 read. This is needed to avoid circularities in the implementation
8234 of these fields in libjava. */
8235
8236 static tree
8237 maybe_build_primttype_type_ref (rhs, wfl)
8238 tree rhs, wfl;
8239 {
8240 tree to_return = NULL_TREE;
8241 tree rhs_type = TREE_TYPE (rhs);
8242 if (TREE_CODE (rhs) == COMPOUND_EXPR)
8243 {
8244 tree n = TREE_OPERAND (rhs, 1);
8245 if (TREE_CODE (n) == VAR_DECL
8246 && DECL_NAME (n) == TYPE_identifier_node
8247 && rhs_type == class_ptr_type)
8248 {
8249 char *self_name = IDENTIFIER_POINTER (EXPR_WFL_NODE (wfl));
8250 if (!strncmp (self_name, "java.lang.", 10))
8251 to_return = build_primtype_type_ref (self_name);
8252 }
8253 }
8254 return (to_return ? to_return : rhs );
8255 }
8256
8257 /* 15.25 Assignment operators. */
8258
8259 static tree
8260 patch_assignment (node, wfl_op1, wfl_op2)
8261 tree node;
8262 tree wfl_op1;
8263 tree wfl_op2;
8264 {
8265 tree rhs = TREE_OPERAND (node, 1);
8266 tree lvalue = TREE_OPERAND (node, 0), llvalue;
8267 tree lhs_type, rhs_type, new_rhs = NULL_TREE;
8268 int error_found = 0;
8269 int lvalue_from_array = 0;
8270
8271 /* Can't assign to a final. */
8272 if (check_final_assignment (lvalue, wfl_op1))
8273 error_found = 1;
8274
8275 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8276
8277 /* Lhs can be a named variable */
8278 if (DECL_P (lvalue))
8279 {
8280 INITIALIZED_P (lvalue) = 1;
8281 lhs_type = TREE_TYPE (lvalue);
8282 }
8283 /* Or Lhs can be a array acccess. Should that be lvalue ? FIXME +
8284 comment on reason why */
8285 else if (TREE_CODE (wfl_op1) == ARRAY_REF)
8286 {
8287 lhs_type = TREE_TYPE (lvalue);
8288 lvalue_from_array = 1;
8289 }
8290 /* Or a field access */
8291 else if (TREE_CODE (lvalue) == COMPONENT_REF)
8292 lhs_type = TREE_TYPE (lvalue);
8293 /* Or a function return slot */
8294 else if (TREE_CODE (lvalue) == RESULT_DECL)
8295 lhs_type = TREE_TYPE (lvalue);
8296 /* Otherwise, we might want to try to write into an optimized static
8297 final, this is an of a different nature, reported further on. */
8298 else if (TREE_CODE (wfl_op1) == EXPR_WITH_FILE_LOCATION
8299 && resolve_expression_name (wfl_op1, &llvalue)
8300 && check_final_assignment (llvalue, wfl_op1))
8301 {
8302 error_found = 1;
8303 /* What we should do instead is resetting the all the flags
8304 previously set, exchange lvalue for llvalue and continue. */
8305 return error_mark_node;
8306 }
8307 else
8308 {
8309 parse_error_context (wfl_op1, "Invalid left hand side of assignment");
8310 error_found = 1;
8311 }
8312
8313 rhs_type = TREE_TYPE (rhs);
8314 /* 5.1 Try the assignment conversion for builtin type. */
8315 new_rhs = try_builtin_assignconv (wfl_op1, lhs_type, rhs);
8316
8317 /* 5.2 If it failed, try a reference conversion */
8318 if (!new_rhs && (new_rhs = try_reference_assignconv (lhs_type, rhs)))
8319 lhs_type = promote_type (rhs_type);
8320
8321 /* 15.25.2 If we have a compound assignment, convert RHS into the
8322 type of the LHS */
8323 else if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8324 new_rhs = convert (lhs_type, rhs);
8325
8326 /* Explicit cast required. This is an error */
8327 if (!new_rhs)
8328 {
8329 char *t1 = strdup (lang_printable_name (TREE_TYPE (rhs), 0));
8330 char *t2 = strdup (lang_printable_name (lhs_type, 0));
8331 tree wfl;
8332 char operation [32]; /* Max size known */
8333
8334 /* If the assignment is part of a declaration, we use the WFL of
8335 the declared variable to point out the error and call it a
8336 declaration problem. If the assignment is a genuine =
8337 operator, we call is a operator `=' problem, otherwise we
8338 call it an assignment problem. In both of these last cases,
8339 we use the WFL of the operator to indicate the error. */
8340
8341 if (MODIFY_EXPR_FROM_INITIALIZATION_P (node))
8342 {
8343 wfl = wfl_op1;
8344 strcpy (operation, "declaration");
8345 }
8346 else
8347 {
8348 wfl = wfl_operator;
8349 if (COMPOUND_ASSIGN_P (TREE_OPERAND (node, 1)))
8350 strcpy (operation, "assignment");
8351 else if (TREE_CODE (TREE_OPERAND (node, 0)) == RESULT_DECL)
8352 strcpy (operation, "`return'");
8353 else
8354 strcpy (operation, "`='");
8355 }
8356
8357 parse_error_context
8358 (wfl, (!valid_cast_to_p (rhs_type, lhs_type) ?
8359 "Incompatible type for %s. Can't convert `%s' to `%s'" :
8360 "Incompatible type for %s. Explicit cast "
8361 "needed to convert `%s' to `%s'"), operation, t1, t2);
8362 free (t1); free (t2);
8363 error_found = 1;
8364 }
8365
8366 /* Inline read access to java.lang.PRIMTYPE.TYPE */
8367 if (new_rhs)
8368 new_rhs = maybe_build_primttype_type_ref (new_rhs, wfl_op2);
8369
8370 if (error_found)
8371 return error_mark_node;
8372
8373 /* If we built a compound expression as the result of a reference
8374 assignment into an array element, return it here. */
8375 if (TREE_CODE (node) == COMPOUND_EXPR)
8376 return node;
8377
8378 TREE_OPERAND (node, 0) = lvalue;
8379 TREE_OPERAND (node, 1) = new_rhs;
8380 TREE_TYPE (node) = lhs_type;
8381 return node;
8382 }
8383
8384 /* Optimize static (final) field initialized upon declaration.
8385 - If the field is static final and is assigned to a primitive
8386 constant type, then set its DECL_INITIAL to the value.
8387 - More to come. */
8388
8389 static tree
8390 patch_initialized_static_field (node)
8391 tree node;
8392 {
8393 tree field = TREE_OPERAND (node, 0);
8394 tree value = TREE_OPERAND (node, 1);
8395
8396 if (DECL_INITIAL (field) != NULL_TREE)
8397 {
8398 tree type = TREE_TYPE (value);
8399 if (FIELD_FINAL (field) && TREE_CONSTANT (value)
8400 && (JPRIMITIVE_TYPE_P (type)
8401 || (flag_emit_class_files
8402 && TREE_CODE (type) == POINTER_TYPE
8403 && TREE_TYPE (type) == string_type_node)))
8404 {
8405 DECL_INITIAL (field) = value;
8406 return empty_stmt_node;
8407 }
8408 DECL_INITIAL (field) = NULL_TREE;
8409 }
8410 return node;
8411 }
8412
8413 /* Check that type SOURCE can be cast into type DEST. If the cast
8414 can't occur at all, return 0 otherwise 1. This function is used to
8415 produce accurate error messages on the reasons why an assignment
8416 failed. */
8417
8418 static tree
8419 try_reference_assignconv (lhs_type, rhs)
8420 tree lhs_type, rhs;
8421 {
8422 tree new_rhs = NULL_TREE;
8423 tree rhs_type = TREE_TYPE (rhs);
8424
8425 if (!JPRIMITIVE_TYPE_P (rhs_type) && JREFERENCE_TYPE_P (lhs_type))
8426 {
8427 /* `null' may be assigned to any reference type */
8428 if (rhs == null_pointer_node)
8429 new_rhs = null_pointer_node;
8430 /* Try the reference assignment conversion */
8431 else if (valid_ref_assignconv_cast_p (rhs_type, lhs_type, 0))
8432 new_rhs = rhs;
8433 /* This is a magic assignment that we process differently */
8434 else if (rhs == soft_exceptioninfo_call_node)
8435 new_rhs = rhs;
8436 }
8437 return new_rhs;
8438 }
8439
8440 /* Check that RHS can be converted into LHS_TYPE by the assignment
8441 conversion (5.2), for the cases of RHS being a builtin type. Return
8442 NULL_TREE if the conversion fails or if because RHS isn't of a
8443 builtin type. Return a converted RHS if the conversion is possible. */
8444
8445 static tree
8446 try_builtin_assignconv (wfl_op1, lhs_type, rhs)
8447 tree wfl_op1, lhs_type, rhs;
8448 {
8449 tree new_rhs = NULL_TREE;
8450 tree rhs_type = TREE_TYPE (rhs);
8451
8452 /* Zero accepted everywhere */
8453 if (TREE_CODE (rhs) == INTEGER_CST
8454 && TREE_INT_CST_HIGH (rhs) == 0 && TREE_INT_CST_LOW (rhs) == 0
8455 && JPRIMITIVE_TYPE_P (rhs_type))
8456 new_rhs = convert (lhs_type, rhs);
8457
8458 /* 5.1.1 Try Identity Conversion,
8459 5.1.2 Try Widening Primitive Conversion */
8460 else if (valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type))
8461 new_rhs = convert (lhs_type, rhs);
8462
8463 /* Try a narrowing primitive conversion (5.1.3):
8464 - expression is a constant expression of type int AND
8465 - variable is byte, short or char AND
8466 - The value of the expression is representable in the type of the
8467 variable */
8468 else if (rhs_type == int_type_node && TREE_CONSTANT (rhs)
8469 && (lhs_type == byte_type_node || lhs_type == char_type_node
8470 || lhs_type == short_type_node))
8471 {
8472 if (int_fits_type_p (rhs, lhs_type))
8473 new_rhs = convert (lhs_type, rhs);
8474 else if (wfl_op1) /* Might be called with a NULL */
8475 parse_warning_context
8476 (wfl_op1, "Constant expression `%s' to wide for narrowing "
8477 "primitive conversion to `%s'",
8478 print_int_node (rhs), lang_printable_name (lhs_type, 0));
8479 /* Reported a warning that will turn into an error further
8480 down, so we don't return */
8481 }
8482
8483 return new_rhs;
8484 }
8485
8486 /* Return 1 if RHS_TYPE can be converted to LHS_TYPE by identity
8487 conversion (5.1.1) or widening primitve conversion (5.1.2). Return
8488 0 is the conversion test fails. This implements parts the method
8489 invocation convertion (5.3). */
8490
8491 static int
8492 valid_builtin_assignconv_identity_widening_p (lhs_type, rhs_type)
8493 tree lhs_type, rhs_type;
8494 {
8495 int all_primitive;
8496
8497 if (lhs_type == rhs_type)
8498 return 1;
8499
8500 /* Sometimes, instead of passing a type, we pass integer_zero_node
8501 so we know that an integral type can accomodate it */
8502 if (JINTEGRAL_TYPE_P (lhs_type) && (rhs_type == integer_zero_node))
8503 return 1;
8504
8505 all_primitive =
8506 JPRIMITIVE_TYPE_P (lhs_type) && JPRIMITIVE_TYPE_P (rhs_type);
8507
8508 if (!all_primitive)
8509 return 0;
8510
8511 /* byte, even if it's smaller than a char can't be converted into a
8512 char. Short can't too, but the < test below takes care of that */
8513 if (lhs_type == char_type_node && rhs_type == byte_type_node)
8514 return 0;
8515
8516 /* Accept all promoted type here. Note, we can't use <= in the test
8517 below, because we still need to bounce out assignments of short
8518 to char and the likes */
8519 if (lhs_type == int_type_node
8520 && (rhs_type == promoted_byte_type_node
8521 || rhs_type == promoted_short_type_node
8522 || rhs_type == promoted_char_type_node
8523 || rhs_type == promoted_boolean_type_node))
8524 return 1;
8525
8526 if (JINTEGRAL_TYPE_P (rhs_type)
8527 && ((TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type))
8528 || (JFLOAT_TYPE_P (lhs_type) &&
8529 TYPE_PRECISION (rhs_type) == TYPE_PRECISION (lhs_type))))
8530 return 1;
8531 else if (JFLOAT_TYPE_P (rhs_type)
8532 && (TYPE_PRECISION (rhs_type) < TYPE_PRECISION (lhs_type)))
8533 return 1;
8534
8535 return 0;
8536 }
8537
8538 /* Check that something of SOURCE type can be assigned or cast to
8539 something of DEST type at runtime. Return 1 if the operation is
8540 valid, 0 otherwise. If CAST is set to 1, we're treating the case
8541 were SOURCE is cast into DEST, which borrows a lot of the
8542 assignment check. */
8543
8544 static int
8545 valid_ref_assignconv_cast_p (source, dest, cast)
8546 tree source;
8547 tree dest;
8548 int cast;
8549 {
8550 if (JNULLP_TYPE_P (source))
8551 return 1;
8552 if (TREE_CODE (source) == POINTER_TYPE)
8553 source = TREE_TYPE (source);
8554 if (TREE_CODE (dest) == POINTER_TYPE)
8555 dest = TREE_TYPE (dest);
8556 /* Case where SOURCE is a class type */
8557 if (TYPE_CLASS_P (source))
8558 {
8559 if (TYPE_CLASS_P (dest))
8560 return source == dest || inherits_from_p (source, dest)
8561 || (cast && inherits_from_p (dest, source));
8562 if (TYPE_INTERFACE_P (dest))
8563 {
8564 /* If doing a cast and SOURCE is final, the operation is
8565 always correct a compile time (because even if SOURCE
8566 does not implement DEST, a subclass of SOURCE might). */
8567 if (cast && !CLASS_FINAL (TYPE_NAME (source)))
8568 return 1;
8569 /* Otherwise, SOURCE must implement DEST */
8570 return interface_of_p (dest, source);
8571 }
8572 /* DEST is an array, cast permited if SOURCE is of Object type */
8573 return (cast && source == object_type_node ? 1 : 0);
8574 }
8575 if (TYPE_INTERFACE_P (source))
8576 {
8577 if (TYPE_CLASS_P (dest))
8578 {
8579 /* If not casting, DEST must be the Object type */
8580 if (!cast)
8581 return dest == object_type_node;
8582 /* We're doing a cast. The cast is always valid is class
8583 DEST is not final, otherwise, DEST must implement SOURCE */
8584 else if (!CLASS_FINAL (TYPE_NAME (dest)))
8585 return 1;
8586 else
8587 return interface_of_p (source, dest);
8588 }
8589 if (TYPE_INTERFACE_P (dest))
8590 {
8591 /* If doing a cast, then if SOURCE and DEST contain method
8592 with the same signature but different return type, then
8593 this is a (compile time) error */
8594 if (cast)
8595 {
8596 tree method_source, method_dest;
8597 tree source_type;
8598 tree source_sig;
8599 tree source_name;
8600 for (method_source = TYPE_METHODS (source); method_source;
8601 method_source = TREE_CHAIN (method_source))
8602 {
8603 source_sig =
8604 build_java_argument_signature (TREE_TYPE (method_source));
8605 source_type = TREE_TYPE (TREE_TYPE (method_source));
8606 source_name = DECL_NAME (method_source);
8607 for (method_dest = TYPE_METHODS (dest);
8608 method_dest; method_dest = TREE_CHAIN (method_dest))
8609 if (source_sig ==
8610 build_java_argument_signature (TREE_TYPE (method_dest))
8611 && source_name == DECL_NAME (method_dest)
8612 && source_type != TREE_TYPE (TREE_TYPE (method_dest)))
8613 return 0;
8614 }
8615 return 1;
8616 }
8617 else
8618 return source == dest || interface_of_p (dest, source);
8619 }
8620 else /* Array */
8621 return 0;
8622 }
8623 if (TYPE_ARRAY_P (source))
8624 {
8625 if (TYPE_CLASS_P (dest))
8626 return dest == object_type_node;
8627 if (TYPE_INTERFACE_P (dest))
8628 return 0; /* Install test on Clonable. FIXME */
8629 else /* Arrays */
8630 {
8631 tree source_element_type = TYPE_ARRAY_ELEMENT (source);
8632 tree dest_element_type = TYPE_ARRAY_ELEMENT (dest);
8633
8634 /* In case of severe errors, they turn out null */
8635 if (!dest_element_type || !source_element_type)
8636 return 0;
8637 if (source_element_type == dest_element_type)
8638 return 1;
8639 return valid_ref_assignconv_cast_p (source_element_type,
8640 dest_element_type, cast);
8641 }
8642 return 0;
8643 }
8644 return 0;
8645 }
8646
8647 static int
8648 valid_cast_to_p (source, dest)
8649 tree source;
8650 tree dest;
8651 {
8652 if (TREE_CODE (source) == POINTER_TYPE)
8653 source = TREE_TYPE (source);
8654 if (TREE_CODE (dest) == POINTER_TYPE)
8655 dest = TREE_TYPE (dest);
8656
8657 if (TREE_CODE (source) == RECORD_TYPE && TREE_CODE (dest) == RECORD_TYPE)
8658 return valid_ref_assignconv_cast_p (source, dest, 1);
8659
8660 else if (JNUMERIC_TYPE_P (source) && JNUMERIC_TYPE_P (dest))
8661 return 1;
8662
8663 return 0;
8664 }
8665
8666 /* Method invocation conversion test. Return 1 if type SOURCE can be
8667 converted to type DEST through the methond invocation conversion
8668 process (5.3) */
8669
8670 static tree
8671 do_unary_numeric_promotion (arg)
8672 tree arg;
8673 {
8674 tree type = TREE_TYPE (arg);
8675 if (TREE_CODE (type) == INTEGER_TYPE ? TYPE_PRECISION (type) < 32
8676 : TREE_CODE (type) == CHAR_TYPE)
8677 arg = convert (int_type_node, arg);
8678 return arg;
8679 }
8680
8681 static int
8682 valid_method_invocation_conversion_p (dest, source)
8683 tree dest, source;
8684 {
8685 return (((JPRIMITIVE_TYPE_P (source) || (source == integer_zero_node))
8686 && JPRIMITIVE_TYPE_P (dest)
8687 && valid_builtin_assignconv_identity_widening_p (dest, source))
8688 || ((JREFERENCE_TYPE_P (source) || JNULLP_TYPE_P (source))
8689 && (JREFERENCE_TYPE_P (dest) || JNULLP_TYPE_P (dest))
8690 && valid_ref_assignconv_cast_p (source, dest, 0)));
8691 }
8692
8693 /* Build an incomplete binop expression. */
8694
8695 static tree
8696 build_binop (op, op_location, op1, op2)
8697 enum tree_code op;
8698 int op_location;
8699 tree op1, op2;
8700 {
8701 tree binop = build (op, NULL_TREE, op1, op2);
8702 TREE_SIDE_EFFECTS (binop) = 1;
8703 /* Store the location of the operator, for better error report. The
8704 string of the operator will be rebuild based on the OP value. */
8705 EXPR_WFL_LINECOL (binop) = op_location;
8706 return binop;
8707 }
8708
8709 /* Build the string of the operator retained by NODE. If NODE is part
8710 of a compound expression, add an '=' at the end of the string. This
8711 function is called when an error needs to be reported on an
8712 operator. The string is returned as a pointer to a static character
8713 buffer. */
8714
8715 static char *
8716 operator_string (node)
8717 tree node;
8718 {
8719 #define BUILD_OPERATOR_STRING(S) \
8720 { \
8721 sprintf (buffer, "%s%s", S, (COMPOUND_ASSIGN_P (node) ? "=" : "")); \
8722 return buffer; \
8723 }
8724
8725 static char buffer [10];
8726 switch (TREE_CODE (node))
8727 {
8728 case MULT_EXPR: BUILD_OPERATOR_STRING ("*");
8729 case RDIV_EXPR: BUILD_OPERATOR_STRING ("/");
8730 case TRUNC_MOD_EXPR: BUILD_OPERATOR_STRING ("%");
8731 case PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8732 case MINUS_EXPR: BUILD_OPERATOR_STRING ("-");
8733 case LSHIFT_EXPR: BUILD_OPERATOR_STRING ("<<");
8734 case RSHIFT_EXPR: BUILD_OPERATOR_STRING (">>");
8735 case URSHIFT_EXPR: BUILD_OPERATOR_STRING (">>>");
8736 case BIT_AND_EXPR: BUILD_OPERATOR_STRING ("&");
8737 case BIT_XOR_EXPR: BUILD_OPERATOR_STRING ("^");
8738 case BIT_IOR_EXPR: BUILD_OPERATOR_STRING ("|");
8739 case TRUTH_ANDIF_EXPR: BUILD_OPERATOR_STRING ("&&");
8740 case TRUTH_ORIF_EXPR: BUILD_OPERATOR_STRING ("||");
8741 case EQ_EXPR: BUILD_OPERATOR_STRING ("==");
8742 case NE_EXPR: BUILD_OPERATOR_STRING ("!=");
8743 case GT_EXPR: BUILD_OPERATOR_STRING (">");
8744 case GE_EXPR: BUILD_OPERATOR_STRING (">=");
8745 case LT_EXPR: BUILD_OPERATOR_STRING ("<");
8746 case LE_EXPR: BUILD_OPERATOR_STRING ("<=");
8747 case UNARY_PLUS_EXPR: BUILD_OPERATOR_STRING ("+");
8748 case NEGATE_EXPR: BUILD_OPERATOR_STRING ("-");
8749 case TRUTH_NOT_EXPR: BUILD_OPERATOR_STRING ("!");
8750 case BIT_NOT_EXPR: BUILD_OPERATOR_STRING ("~");
8751 case PREINCREMENT_EXPR: /* Fall through */
8752 case POSTINCREMENT_EXPR: BUILD_OPERATOR_STRING ("++");
8753 case PREDECREMENT_EXPR: /* Fall through */
8754 case POSTDECREMENT_EXPR: BUILD_OPERATOR_STRING ("--");
8755 default:
8756 fatal ("unregistered operator %s - operator_string",
8757 tree_code_name [TREE_CODE (node)]);
8758 }
8759 return NULL;
8760 #undef BUILD_OPERATOR_STRING
8761 }
8762
8763 /* Binary operators (15.16 up to 15.18). We return error_mark_node on
8764 errors but we modify NODE so that it contains the type computed
8765 according to the expression, when it's fixed. Otherwise, we write
8766 error_mark_node as the type. It allows us to further the analysis
8767 of remaining nodes and detects more errors in certain cases. */
8768
8769 static tree
8770 patch_binop (node, wfl_op1, wfl_op2)
8771 tree node;
8772 tree wfl_op1;
8773 tree wfl_op2;
8774 {
8775 tree op1 = TREE_OPERAND (node, 0);
8776 tree op2 = TREE_OPERAND (node, 1);
8777 tree op1_type = TREE_TYPE (op1);
8778 tree op2_type = TREE_TYPE (op2);
8779 tree prom_type;
8780 int code = TREE_CODE (node);
8781
8782 /* If 1, tell the routine that we have to return error_mark_node
8783 after checking for the initialization of the RHS */
8784 int error_found = 0;
8785
8786 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
8787
8788 switch (code)
8789 {
8790 /* 15.16 Multiplicative operators */
8791 case MULT_EXPR: /* 15.16.1 Multiplication Operator * */
8792 case RDIV_EXPR: /* 15.16.2 Division Operator / */
8793 case TRUNC_MOD_EXPR: /* 15.16.3 Remainder operator % */
8794 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
8795 {
8796 if (!JPRIMITIVE_TYPE_P (op1_type))
8797 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
8798 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
8799 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
8800 TREE_TYPE (node) = error_mark_node;
8801 error_found = 1;
8802 break;
8803 }
8804 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8805 /* Change the division operator if necessary */
8806 if (code == RDIV_EXPR && TREE_CODE (prom_type) == INTEGER_TYPE)
8807 TREE_SET_CODE (node, TRUNC_DIV_EXPR);
8808 /* This one is more complicated. FLOATs are processed by a function
8809 call to soft_fmod. */
8810 if (code == TRUNC_MOD_EXPR)
8811 return build_java_binop (TRUNC_MOD_EXPR, prom_type, op1, op2);
8812 break;
8813
8814 /* 15.17 Additive Operators */
8815 case PLUS_EXPR: /* 15.17.1 String Concatenation Operator + */
8816
8817 /* Operation is valid if either one argument is a string
8818 constant, a String object or a StringBuffer crafted for the
8819 purpose of the a previous usage of the String concatenation
8820 operator */
8821
8822 if (TREE_CODE (op1) == STRING_CST
8823 || TREE_CODE (op2) == STRING_CST
8824 || JSTRING_TYPE_P (op1_type)
8825 || JSTRING_TYPE_P (op2_type)
8826 || IS_CRAFTED_STRING_BUFFER_P (op1)
8827 || IS_CRAFTED_STRING_BUFFER_P (op2))
8828 return build_string_concatenation (op1, op2);
8829
8830 case MINUS_EXPR: /* 15.17.2 Additive Operators (+ and -) for
8831 Numeric Types */
8832 if (!JPRIMITIVE_TYPE_P (op1_type) || !JPRIMITIVE_TYPE_P (op2_type))
8833 {
8834 if (!JPRIMITIVE_TYPE_P (op1_type))
8835 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
8836 if (!JPRIMITIVE_TYPE_P (op2_type) && (op1_type != op2_type))
8837 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
8838 TREE_TYPE (node) = error_mark_node;
8839 error_found = 1;
8840 break;
8841 }
8842 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8843 break;
8844
8845 /* 15.18 Shift Operators */
8846 case LSHIFT_EXPR:
8847 case RSHIFT_EXPR:
8848 case URSHIFT_EXPR:
8849 if (!JINTEGRAL_TYPE_P (op1_type) || !JINTEGRAL_TYPE_P (op2_type))
8850 {
8851 if (!JINTEGRAL_TYPE_P (op1_type))
8852 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
8853 else
8854 parse_error_context
8855 (wfl_operator, (JPRIMITIVE_TYPE_P (op2_type) ?
8856 "Incompatible type for `%s'. Explicit cast needed to convert "
8857 "shift distance from `%s' to integral" :
8858 "Incompatible type for `%s'. Can't convert shift distance from "
8859 "`%s' to integral"),
8860 operator_string (node), lang_printable_name (op2_type, 0));
8861 TREE_TYPE (node) = error_mark_node;
8862 error_found = 1;
8863 break;
8864 }
8865
8866 /* Unary numeric promotion (5.6.1) is performed on each operand
8867 separatly */
8868 op1 = do_unary_numeric_promotion (op1);
8869 op2 = do_unary_numeric_promotion (op2);
8870
8871 /* The type of the shift expression is the type of the promoted
8872 type of the left-hand operand */
8873 prom_type = TREE_TYPE (op1);
8874
8875 /* Shift int only up to 0x1f and long up to 0x3f */
8876 if (prom_type == int_type_node)
8877 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
8878 build_int_2 (0x1f, 0)));
8879 else
8880 op2 = fold (build (BIT_AND_EXPR, int_type_node, op2,
8881 build_int_2 (0x3f, 0)));
8882
8883 /* The >>> operator is a >> operating on unsigned quantities */
8884 if (code == URSHIFT_EXPR && ! flag_emit_class_files)
8885 {
8886 op1 = convert (unsigned_type (prom_type), op1);
8887 TREE_SET_CODE (node, RSHIFT_EXPR);
8888 }
8889 break;
8890
8891 /* 15.19.1 Type Comparison Operator instaceof */
8892 case INSTANCEOF_EXPR:
8893
8894 TREE_TYPE (node) = boolean_type_node;
8895
8896 if (!(op2_type = resolve_type_during_patch (op2)))
8897 return error_mark_node;
8898
8899 /* The first operand must be a reference type or the null type */
8900 if (!JREFERENCE_TYPE_P (op1_type) && op1 != null_pointer_node)
8901 error_found = 1; /* Error reported further below */
8902
8903 /* The second operand must be a reference type */
8904 if (!JREFERENCE_TYPE_P (op2_type))
8905 {
8906 SET_WFL_OPERATOR (wfl_operator, node, wfl_op2);
8907 parse_error_context
8908 (wfl_operator, "Invalid argument `%s' for `instanceof'",
8909 lang_printable_name (op2_type, 0));
8910 error_found = 1;
8911 }
8912
8913 if (!error_found && valid_ref_assignconv_cast_p (op1_type, op2_type, 1))
8914 {
8915 /* If the first operand is null, the result is always false */
8916 if (op1 == null_pointer_node)
8917 return boolean_false_node;
8918 else if (flag_emit_class_files)
8919 {
8920 TREE_OPERAND (node, 1) = op2_type;
8921 return node;
8922 }
8923 /* Otherwise we have to invoke instance of to figure it out */
8924 else
8925 {
8926 tree call =
8927 build (CALL_EXPR, boolean_type_node,
8928 build_address_of (soft_instanceof_node),
8929 tree_cons
8930 (NULL_TREE, op1,
8931 build_tree_list (NULL_TREE,
8932 build_class_ref (op2_type))),
8933 NULL_TREE);
8934 TREE_SIDE_EFFECTS (call) = 1;
8935 return call;
8936 }
8937 }
8938 /* There is no way the expression operand can be an instance of
8939 the type operand. This is a compile time error. */
8940 else
8941 {
8942 char *t1 = strdup (lang_printable_name (op1_type, 0));
8943 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
8944 parse_error_context
8945 (wfl_operator, "Impossible for `%s' to be instance of `%s'",
8946 t1, lang_printable_name (op2_type, 0));
8947 free (t1);
8948 error_found = 1;
8949 }
8950
8951 break;
8952
8953 /* 15.21 Bitwise and Logical Operators */
8954 case BIT_AND_EXPR:
8955 case BIT_XOR_EXPR:
8956 case BIT_IOR_EXPR:
8957 if (JINTEGRAL_TYPE_P (op1_type) && JINTEGRAL_TYPE_P (op2_type))
8958 /* Binary numeric promotion is performed on both operand and the
8959 expression retain that type */
8960 prom_type = binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
8961
8962 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE
8963 && TREE_CODE (op1_type) == BOOLEAN_TYPE)
8964 /* The type of the bitwise operator expression is BOOLEAN */
8965 prom_type = boolean_type_node;
8966 else
8967 {
8968 if (!JINTEGRAL_TYPE_P (op1_type))
8969 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op1_type);
8970 if (!JINTEGRAL_TYPE_P (op2_type) && (op1_type != op2_type))
8971 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op2_type);
8972 TREE_TYPE (node) = error_mark_node;
8973 error_found = 1;
8974 /* Insert a break here if adding thing before the switch's
8975 break for this case */
8976 }
8977 break;
8978
8979 /* 15.22 Conditional-And Operator */
8980 case TRUTH_ANDIF_EXPR:
8981 /* 15.23 Conditional-Or Operator */
8982 case TRUTH_ORIF_EXPR:
8983 /* Operands must be of BOOLEAN type */
8984 if (TREE_CODE (op1_type) != BOOLEAN_TYPE ||
8985 TREE_CODE (op2_type) != BOOLEAN_TYPE)
8986 {
8987 if (TREE_CODE (op1_type) != BOOLEAN_TYPE)
8988 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op1_type);
8989 if (TREE_CODE (op2_type) != BOOLEAN_TYPE && (op1_type != op2_type))
8990 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op2_type);
8991 TREE_TYPE (node) = boolean_type_node;
8992 error_found = 1;
8993 break;
8994 }
8995 /* The type of the conditional operators is BOOLEAN */
8996 prom_type = boolean_type_node;
8997 break;
8998
8999 /* 15.19.1 Numerical Comparison Operators <, <=, >, >= */
9000 case LT_EXPR:
9001 case GT_EXPR:
9002 case LE_EXPR:
9003 case GE_EXPR:
9004 /* The type of each of the operands must be a primitive numeric
9005 type */
9006 if (!JNUMERIC_TYPE_P (op1_type) || ! JNUMERIC_TYPE_P (op2_type))
9007 {
9008 if (!JNUMERIC_TYPE_P (op1_type))
9009 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op1_type);
9010 if (!JNUMERIC_TYPE_P (op2_type) && (op1_type != op2_type))
9011 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op2_type);
9012 TREE_TYPE (node) = boolean_type_node;
9013 error_found = 1;
9014 break;
9015 }
9016 /* Binary numeric promotion is performed on the operands */
9017 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9018 /* The type of the relation expression is always BOOLEAN */
9019 prom_type = boolean_type_node;
9020 break;
9021
9022 /* 15.20 Equality Operator */
9023 case EQ_EXPR:
9024 case NE_EXPR:
9025 /* 15.20.1 Numerical Equality Operators == and != */
9026 /* Binary numeric promotion is performed on the operands */
9027 if (JNUMERIC_TYPE_P (op1_type) && JNUMERIC_TYPE_P (op2_type))
9028 binary_numeric_promotion (op1_type, op2_type, &op1, &op2);
9029
9030 /* 15.20.2 Boolean Equality Operators == and != */
9031 else if (TREE_CODE (op1_type) == BOOLEAN_TYPE &&
9032 TREE_CODE (op2_type) == BOOLEAN_TYPE)
9033 ; /* Nothing to do here */
9034
9035 /* 15.20.3 Reference Equality Operators == and != */
9036 /* Types have to be either references or the null type. If
9037 they're references, it must be possible to convert either
9038 type to the other by casting conversion. */
9039 else if (op1 == null_pointer_node || op2 == null_pointer_node
9040 || (JREFERENCE_TYPE_P (op1_type) && JREFERENCE_TYPE_P (op2_type)
9041 && (valid_ref_assignconv_cast_p (op1_type, op2_type, 1)
9042 || valid_ref_assignconv_cast_p (op2_type,
9043 op1_type, 1))))
9044 ; /* Nothing to do here */
9045
9046 /* Else we have an error figure what can't be converted into
9047 what and report the error */
9048 else
9049 {
9050 char *t1;
9051 t1 = strdup (lang_printable_name (op1_type, 0));
9052 parse_error_context
9053 (wfl_operator, "Incompatible type for `%s'. Can't convert `%s' "
9054 "to `%s'", operator_string (node), t1,
9055 lang_printable_name (op2_type, 0));
9056 free (t1);
9057 TREE_TYPE (node) = boolean_type_node;
9058 error_found = 1;
9059 break;
9060 }
9061 prom_type = boolean_type_node;
9062 break;
9063 }
9064
9065 if (error_found)
9066 return error_mark_node;
9067
9068 TREE_OPERAND (node, 0) = op1;
9069 TREE_OPERAND (node, 1) = op2;
9070 TREE_TYPE (node) = prom_type;
9071 return fold (node);
9072 }
9073
9074 /* Concatenate the STRING_CST CSTE and STRING. When AFTER is a non
9075 zero value, the value of CSTE comes after the valude of STRING */
9076
9077 static tree
9078 do_merge_string_cste (cste, string, string_len, after)
9079 tree cste;
9080 char *string;
9081 int string_len, after;
9082 {
9083 int len = TREE_STRING_LENGTH (cste) + string_len;
9084 char *old = TREE_STRING_POINTER (cste);
9085 TREE_STRING_LENGTH (cste) = len;
9086 TREE_STRING_POINTER (cste) = obstack_alloc (expression_obstack, len+1);
9087 if (after)
9088 {
9089 strcpy (TREE_STRING_POINTER (cste), string);
9090 strcat (TREE_STRING_POINTER (cste), old);
9091 }
9092 else
9093 {
9094 strcpy (TREE_STRING_POINTER (cste), old);
9095 strcat (TREE_STRING_POINTER (cste), string);
9096 }
9097 return cste;
9098 }
9099
9100 /* Tries to merge OP1 (a STRING_CST) and OP2 (if suitable). Return a
9101 new STRING_CST on success, NULL_TREE on failure */
9102
9103 static tree
9104 merge_string_cste (op1, op2, after)
9105 tree op1, op2;
9106 int after;
9107 {
9108 /* Handle two string constants right away */
9109 if (TREE_CODE (op2) == STRING_CST)
9110 return do_merge_string_cste (op1, TREE_STRING_POINTER (op2),
9111 TREE_STRING_LENGTH (op2), after);
9112
9113 /* Reasonable integer constant can be treated right away */
9114 if (TREE_CODE (op2) == INTEGER_CST && !TREE_CONSTANT_OVERFLOW (op2))
9115 {
9116 static char *boolean_true = "true";
9117 static char *boolean_false = "false";
9118 static char *null_pointer = "null";
9119 char ch[3];
9120 char *string;
9121
9122 if (op2 == boolean_true_node)
9123 string = boolean_true;
9124 else if (op2 == boolean_false_node)
9125 string = boolean_false;
9126 else if (op2 == null_pointer_node)
9127 string = null_pointer;
9128 else if (TREE_TYPE (op2) == char_type_node)
9129 {
9130 ch[0] = (char )TREE_INT_CST_LOW (op2);
9131 ch[1] = '\0';
9132 string = ch;
9133 }
9134 else
9135 string = print_int_node (op2);
9136
9137 return do_merge_string_cste (op1, string, strlen (string), after);
9138 }
9139 return NULL_TREE;
9140 }
9141
9142 /* Tries to statically concatenate OP1 and OP2 if possible. Either one
9143 has to be a STRING_CST and the other part must be a STRING_CST or a
9144 INTEGRAL constant. Return a new STRING_CST if the operation
9145 succeed, NULL_TREE otherwise.
9146
9147 If the case we want to optimize for space, we might want to return
9148 NULL_TREE for each invocation of this routine. FIXME */
9149
9150 static tree
9151 string_constant_concatenation (op1, op2)
9152 tree op1, op2;
9153 {
9154 if (TREE_CODE (op1) == STRING_CST || (TREE_CODE (op2) == STRING_CST))
9155 {
9156 tree string, rest;
9157 int invert;
9158
9159 string = (TREE_CODE (op1) == STRING_CST ? op1 : op2);
9160 rest = (string == op1 ? op2 : op1);
9161 invert = (string == op1 ? 0 : 1 );
9162
9163 /* Walk REST, only if it looks reasonable */
9164 if (TREE_CODE (rest) != STRING_CST
9165 && !IS_CRAFTED_STRING_BUFFER_P (rest)
9166 && !JSTRING_TYPE_P (TREE_TYPE (rest))
9167 && TREE_CODE (rest) == EXPR_WITH_FILE_LOCATION)
9168 {
9169 rest = java_complete_tree (rest);
9170 if (rest == error_mark_node)
9171 return error_mark_node;
9172 rest = fold (rest);
9173 }
9174 return merge_string_cste (string, rest, invert);
9175 }
9176 return NULL_TREE;
9177 }
9178
9179 /* Implement the `+' operator. Does static optimization if possible,
9180 otherwise create (if necessary) and append elements to a
9181 StringBuffer. The StringBuffer will be carried around until it is
9182 used for a function call or an assignment. Then toString() will be
9183 called on it to turn it into a String object. */
9184
9185 static tree
9186 build_string_concatenation (op1, op2)
9187 tree op1, op2;
9188 {
9189 tree result;
9190
9191 /* Try to do some static optimization */
9192 if ((result = string_constant_concatenation (op1, op2)))
9193 return result;
9194
9195 /* If operands are string constant, turn then into object references */
9196
9197 if (TREE_CODE (op1) == STRING_CST)
9198 op1 = patch_string_cst (op1);
9199 if (TREE_CODE (op2) == STRING_CST)
9200 op2 = patch_string_cst (op2);
9201
9202 /* If OP1 isn't already a StringBuffer, create and
9203 initialize a new one */
9204 if (!IS_CRAFTED_STRING_BUFFER_P (op1))
9205 {
9206 /* Two solutions here:
9207 1) OP1 is a string reference, we call new StringBuffer(OP1)
9208 2) Op2 is something else, we call new StringBuffer().append(OP1). */
9209 if (JSTRING_TYPE_P (TREE_TYPE (op1)))
9210 op1 = BUILD_STRING_BUFFER (op1);
9211 else
9212 {
9213 tree aNew = BUILD_STRING_BUFFER (NULL_TREE);
9214 op1 = make_qualified_primary (aNew, BUILD_APPEND (op1), 0);
9215 }
9216 }
9217
9218 /* No longer the last node holding a crafted StringBuffer */
9219 IS_CRAFTED_STRING_BUFFER_P (op1) = 0;
9220 /* Create a node for `{new...,xxx}.append (op2)' */
9221 op1 = make_qualified_primary (op1, BUILD_APPEND (op2), 0);
9222 /* Mark the last node holding a crafted StringBuffer */
9223 IS_CRAFTED_STRING_BUFFER_P (op1) = 1;
9224
9225 return op1;
9226 }
9227
9228 /* Patch the string node NODE. NODE can be a STRING_CST of a crafted
9229 StringBuffer. If no string were found to be patched, return
9230 NULL. */
9231
9232 static tree
9233 patch_string (node)
9234 tree node;
9235 {
9236 if (node == error_mark_node)
9237 return error_mark_node;
9238 if (TREE_CODE (node) == STRING_CST)
9239 return patch_string_cst (node);
9240 else if (IS_CRAFTED_STRING_BUFFER_P (node))
9241 {
9242 int saved = ctxp->explicit_constructor_p;
9243 tree invoke = build_method_invocation (wfl_to_string, NULL_TREE);
9244 tree ret;
9245 /* Temporary disable forbid the use of `this'. */
9246 ctxp->explicit_constructor_p = 0;
9247 ret = java_complete_tree (make_qualified_primary (node, invoke, 0));
9248 /* Restore it at its previous value */
9249 ctxp->explicit_constructor_p = saved;
9250 return ret;
9251 }
9252 return NULL_TREE;
9253 }
9254
9255 /* Build the internal representation of a string constant. */
9256
9257 static tree
9258 patch_string_cst (node)
9259 tree node;
9260 {
9261 int location;
9262 if (! flag_emit_class_files)
9263 {
9264 push_obstacks (&permanent_obstack, &permanent_obstack);
9265 node = get_identifier (TREE_STRING_POINTER (node));
9266 location = alloc_name_constant (CONSTANT_String, node);
9267 node = build_ref_from_constant_pool (location);
9268 }
9269 TREE_TYPE (node) = promote_type (string_type_node);
9270 TREE_CONSTANT (node) = 1;
9271 return node;
9272 }
9273
9274 /* Build an incomplete unary operator expression. */
9275
9276 static tree
9277 build_unaryop (op_token, op_location, op1)
9278 int op_token, op_location;
9279 tree op1;
9280 {
9281 enum tree_code op;
9282 tree unaryop;
9283 switch (op_token)
9284 {
9285 case PLUS_TK: op = UNARY_PLUS_EXPR; break;
9286 case MINUS_TK: op = NEGATE_EXPR; break;
9287 case NEG_TK: op = TRUTH_NOT_EXPR; break;
9288 case NOT_TK: op = BIT_NOT_EXPR; break;
9289 default: fatal ("Unknown token `%d' for unary operator - build_unaryop",
9290 op_token);
9291 }
9292
9293 unaryop = build1 (op, NULL_TREE, op1);
9294 TREE_SIDE_EFFECTS (unaryop) = 1;
9295 /* Store the location of the operator, for better error report. The
9296 string of the operator will be rebuild based on the OP value. */
9297 EXPR_WFL_LINECOL (unaryop) = op_location;
9298 return unaryop;
9299 }
9300
9301 /* Special case for the ++/-- operators, since they require an extra
9302 argument to build, which is set to NULL and patched
9303 later. IS_POST_P is 1 if the operator, 0 otherwise. */
9304
9305 static tree
9306 build_incdec (op_token, op_location, op1, is_post_p)
9307 int op_token, op_location;
9308 tree op1;
9309 int is_post_p;
9310 {
9311 static enum tree_code lookup [2][2] =
9312 {
9313 { PREDECREMENT_EXPR, PREINCREMENT_EXPR, },
9314 { POSTDECREMENT_EXPR, POSTINCREMENT_EXPR, },
9315 };
9316 tree node = build (lookup [is_post_p][(op_token - DECR_TK)],
9317 NULL_TREE, op1, NULL_TREE);
9318 TREE_SIDE_EFFECTS (node) = 1;
9319 /* Store the location of the operator, for better error report. The
9320 string of the operator will be rebuild based on the OP value. */
9321 EXPR_WFL_LINECOL (node) = op_location;
9322 return node;
9323 }
9324
9325 /* Build an incomplete cast operator, based on the use of the
9326 CONVERT_EXPR. Note that TREE_TYPE of the constructed node is
9327 set. java_complete_tree is trained to walk a CONVERT_EXPR even
9328 though its type is already set. */
9329
9330 static tree
9331 build_cast (location, type, exp)
9332 int location;
9333 tree type, exp;
9334 {
9335 tree node = build1 (CONVERT_EXPR, type, exp);
9336 EXPR_WFL_LINECOL (node) = location;
9337 return node;
9338 }
9339
9340 /* 15.14 Unary operators. We return error_mark_node in case of error,
9341 but preserve the type of NODE if the type is fixed. */
9342
9343 static tree
9344 patch_unaryop (node, wfl_op)
9345 tree node;
9346 tree wfl_op;
9347 {
9348 tree op = TREE_OPERAND (node, 0);
9349 tree op_type = TREE_TYPE (op);
9350 tree prom_type, value;
9351 int code = TREE_CODE (node);
9352 int error_found = 0;
9353
9354 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9355
9356 switch (code)
9357 {
9358 /* 15.13.2 Postfix Increment Operator ++ */
9359 case POSTINCREMENT_EXPR:
9360 /* 15.13.3 Postfix Increment Operator -- */
9361 case POSTDECREMENT_EXPR:
9362 /* 15.14.1 Prefix Increment Operator ++ */
9363 case PREINCREMENT_EXPR:
9364 /* 15.14.2 Prefix Decrement Operator -- */
9365 case PREDECREMENT_EXPR:
9366 if (!DECL_P (op) && !((TREE_CODE (op) == INDIRECT_REF
9367 || TREE_CODE (op) == COMPONENT_REF)
9368 && JPRIMITIVE_TYPE_P (TREE_TYPE (op))))
9369 {
9370 tree lvalue;
9371 /* Before screaming, check that we're not in fact trying to
9372 increment a optimized static final access, in which case
9373 we issue an different error message. */
9374 if (!(TREE_CODE (wfl_op) == EXPR_WITH_FILE_LOCATION
9375 && resolve_expression_name (wfl_op, &lvalue)
9376 && check_final_assignment (lvalue, wfl_op)))
9377 parse_error_context (wfl_operator, "Invalid argument to `%s'",
9378 operator_string (node));
9379 TREE_TYPE (node) = error_mark_node;
9380 error_found = 1;
9381 }
9382 else if (check_final_assignment (op, wfl_op))
9383 error_found = 1;
9384
9385 /* From now on, we know that op if a variable and that it has a
9386 valid wfl. We use wfl_op to locate errors related to the
9387 ++/-- operand. */
9388 else if (!JNUMERIC_TYPE_P (op_type))
9389 {
9390 parse_error_context
9391 (wfl_op, "Invalid argument type `%s' to `%s'",
9392 lang_printable_name (op_type, 0), operator_string (node));
9393 TREE_TYPE (node) = error_mark_node;
9394 error_found = 1;
9395 }
9396 else
9397 {
9398 /* Before the addition, binary numeric promotion is performed on
9399 both operands */
9400 value = build_int_2 (1, 0);
9401 TREE_TYPE (node) =
9402 binary_numeric_promotion (op_type, TREE_TYPE (value), &op, &value);
9403 /* And write the promoted incremented and increment */
9404 TREE_OPERAND (node, 0) = op;
9405 TREE_OPERAND (node, 1) = value;
9406 /* Convert the overall back into its original type. */
9407 return fold (convert (op_type, node));
9408 }
9409 break;
9410
9411 /* 15.14.3 Unary Plus Operator + */
9412 case UNARY_PLUS_EXPR:
9413 /* 15.14.4 Unary Minus Operator - */
9414 case NEGATE_EXPR:
9415 if (!JNUMERIC_TYPE_P (op_type))
9416 {
9417 ERROR_CANT_CONVERT_TO_NUMERIC (wfl_operator, node, op_type);
9418 TREE_TYPE (node) = error_mark_node;
9419 error_found = 1;
9420 }
9421 /* Unary numeric promotion is performed on operand */
9422 else
9423 {
9424 op = do_unary_numeric_promotion (op);
9425 prom_type = TREE_TYPE (op);
9426 if (code == UNARY_PLUS_EXPR)
9427 return fold (op);
9428 }
9429 break;
9430
9431 /* 15.14.5 Bitwise Complement Operator ~ */
9432 case BIT_NOT_EXPR:
9433 if (!JINTEGRAL_TYPE_P (op_type))
9434 {
9435 ERROR_CAST_NEEDED_TO_INTEGRAL (wfl_operator, node, op_type);
9436 TREE_TYPE (node) = error_mark_node;
9437 error_found = 1;
9438 }
9439 else
9440 {
9441 op = do_unary_numeric_promotion (op);
9442 prom_type = TREE_TYPE (op);
9443 }
9444 break;
9445
9446 /* 15.14.6 Logical Complement Operator ! */
9447 case TRUTH_NOT_EXPR:
9448 if (TREE_CODE (op_type) != BOOLEAN_TYPE)
9449 {
9450 ERROR_CANT_CONVERT_TO_BOOLEAN (wfl_operator, node, op_type);
9451 /* But the type is known. We will report an error if further
9452 attempt of a assignment is made with this rhs */
9453 TREE_TYPE (node) = boolean_type_node;
9454 error_found = 1;
9455 }
9456 else
9457 prom_type = boolean_type_node;
9458 break;
9459
9460 /* 15.15 Cast Expression */
9461 case CONVERT_EXPR:
9462 value = patch_cast (node, wfl_operator);
9463 if (value == error_mark_node)
9464 {
9465 /* If this cast is part of an assignment, we tell the code
9466 that deals with it not to complain about a mismatch,
9467 because things have been cast, anyways */
9468 TREE_TYPE (node) = error_mark_node;
9469 error_found = 1;
9470 }
9471 else
9472 return fold (value);
9473 break;
9474 }
9475
9476 if (error_found)
9477 return error_mark_node;
9478
9479 /* There are cases where node has been replaced by something else
9480 and we don't end up returning here: UNARY_PLUS_EXPR,
9481 CONVERT_EXPR, {POST,PRE}{INCR,DECR}EMENT_EXPR. */
9482 TREE_OPERAND (node, 0) = fold (op);
9483 TREE_TYPE (node) = prom_type;
9484 return fold (node);
9485 }
9486
9487 /* Generic type resolution that sometimes takes place during node
9488 patching. Returned the resolved type or generate an error
9489 message. Return the resolved type or NULL_TREE. */
9490
9491 static tree
9492 resolve_type_during_patch (type)
9493 tree type;
9494 {
9495 if (unresolved_type_p (type, NULL))
9496 {
9497 tree type_decl = resolve_no_layout (EXPR_WFL_NODE (type), NULL_TREE);
9498 if (!type_decl)
9499 {
9500 parse_error_context (type,
9501 "Class `%s' not found in type declaration",
9502 IDENTIFIER_POINTER (EXPR_WFL_NODE (type)));
9503 return NULL_TREE;
9504 }
9505 else
9506 {
9507 CLASS_LOADED_P (TREE_TYPE (type_decl)) = 1;
9508 return TREE_TYPE (type_decl);
9509 }
9510 }
9511 return type;
9512 }
9513 /* 5.5 Casting Conversion. error_mark_node is returned if an error is
9514 found. Otherwise NODE or something meant to replace it is returned. */
9515
9516 static tree
9517 patch_cast (node, wfl_operator)
9518 tree node;
9519 tree wfl_operator;
9520 {
9521 tree op = TREE_OPERAND (node, 0);
9522 tree op_type = TREE_TYPE (op);
9523 tree cast_type = TREE_TYPE (node);
9524 char *t1;
9525
9526 /* First resolve OP_TYPE if unresolved */
9527 if (!(cast_type = resolve_type_during_patch (cast_type)))
9528 return error_mark_node;
9529
9530 /* Check on cast that are proven correct at compile time */
9531 if (JNUMERIC_TYPE_P (cast_type) && JNUMERIC_TYPE_P (op_type))
9532 {
9533 static tree convert_narrow ();
9534 /* Same type */
9535 if (cast_type == op_type)
9536 return node;
9537
9538 /* Try widening/narowwing convertion. Potentially, things need
9539 to be worked out in gcc so we implement the extreme cases
9540 correctly. fold_convert() needs to be fixed. */
9541 return convert (cast_type, op);
9542 }
9543
9544 /* null can be casted to references */
9545 if (op == null_pointer_node && JREFERENCE_TYPE_P (cast_type))
9546 return build_null_of_type (cast_type);
9547
9548 /* The remaining legal casts involve conversion between reference
9549 types. Check for their compile time correctness. */
9550 if (JREFERENCE_TYPE_P (op_type) && JREFERENCE_TYPE_P (cast_type)
9551 && valid_ref_assignconv_cast_p (cast_type, op_type, 1))
9552 {
9553 TREE_TYPE (node) = promote_type (cast_type);
9554 /* Now, the case can be determined correct at compile time if
9555 OP_TYPE can be converted into CAST_TYPE by assignment
9556 conversion (5.2) */
9557
9558 if (valid_ref_assignconv_cast_p (op_type, cast_type, 0))
9559 {
9560 TREE_SET_CODE (node, NOP_EXPR);
9561 return node;
9562 }
9563
9564 if (flag_emit_class_files)
9565 {
9566 TREE_SET_CODE (node, CONVERT_EXPR);
9567 return node;
9568 }
9569
9570 /* The cast requires a run-time check */
9571 return build (CALL_EXPR, promote_type (cast_type),
9572 build_address_of (soft_checkcast_node),
9573 tree_cons (NULL_TREE, build_class_ref (cast_type),
9574 build_tree_list (NULL_TREE, op)),
9575 NULL_TREE);
9576 }
9577
9578 /* Any other casts are proven incorrect at compile time */
9579 t1 = strdup (lang_printable_name (op_type, 0));
9580 parse_error_context (wfl_operator, "Invalid cast from `%s' to `%s'",
9581 t1, lang_printable_name (cast_type, 0));
9582 free (t1);
9583 return error_mark_node;
9584 }
9585
9586 /* Build a null constant and give it the type TYPE. */
9587
9588 static tree
9589 build_null_of_type (type)
9590 tree type;
9591 {
9592 tree node = build_int_2 (0, 0);
9593 TREE_TYPE (node) = promote_type (type);
9594 return node;
9595 }
9596
9597 /* Build an ARRAY_REF incomplete tree node. Note that operand 1 isn't
9598 a list of indices. */
9599 static tree
9600 build_array_ref (location, array, index)
9601 int location;
9602 tree array, index;
9603 {
9604 tree node = build (ARRAY_REF, NULL_TREE, array, index);
9605 EXPR_WFL_LINECOL (node) = location;
9606 return node;
9607 }
9608
9609 /* 15.12 Array Access Expression */
9610
9611 static tree
9612 patch_array_ref (node)
9613 tree node;
9614 {
9615 tree array = TREE_OPERAND (node, 0);
9616 tree array_type = TREE_TYPE (array);
9617 tree index = TREE_OPERAND (node, 1);
9618 tree index_type = TREE_TYPE (index);
9619 int error_found = 0;
9620
9621 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9622
9623 if (TREE_CODE (array_type) == POINTER_TYPE)
9624 array_type = TREE_TYPE (array_type);
9625
9626 /* The array reference must be an array */
9627 if (!TYPE_ARRAY_P (array_type))
9628 {
9629 parse_error_context
9630 (wfl_operator, "`[]' can only be applied to arrays. It can't be "
9631 "applied to `%s'", lang_printable_name (array_type, 0));
9632 TREE_TYPE (node) = error_mark_node;
9633 error_found = 1;
9634 }
9635
9636 /* The array index underdoes unary numeric promotion. The promoted
9637 type must be int */
9638 index = do_unary_numeric_promotion (index);
9639 if (TREE_TYPE (index) != int_type_node)
9640 {
9641 int could_cast = valid_cast_to_p (index_type, int_type_node);
9642 parse_error_context
9643 (wfl_operator,
9644 (could_cast ? "Incompatible type for `[]'. Explicit cast needed to "
9645 "convert `%s' to `int'" : "Incompatible type for `[]'. "
9646 "Can't convert `%s' to `int'"),
9647 lang_printable_name (index_type, 0));
9648 TREE_TYPE (node) = error_mark_node;
9649 error_found = 1;
9650 }
9651
9652 if (error_found)
9653 return error_mark_node;
9654
9655 array_type = TYPE_ARRAY_ELEMENT (array_type);
9656
9657 if (flag_emit_class_files)
9658 {
9659 TREE_OPERAND (node, 0) = array;
9660 TREE_OPERAND (node, 1) = index;
9661 }
9662 else
9663 node = build_java_arrayaccess (array, array_type, index);
9664 TREE_TYPE (node) = array_type;
9665 return node;
9666 }
9667
9668 /* 15.9 Array Creation Expressions */
9669
9670 static tree
9671 build_newarray_node (type, dims, extra_dims)
9672 tree type;
9673 tree dims;
9674 int extra_dims;
9675 {
9676 tree node =
9677 build (NEW_ARRAY_EXPR, NULL_TREE, type, nreverse (dims),
9678 build_int_2 (extra_dims, 0));
9679 return node;
9680 }
9681
9682 static tree
9683 patch_newarray (node)
9684 tree node;
9685 {
9686 tree type = TREE_OPERAND (node, 0);
9687 tree dims = TREE_OPERAND (node, 1);
9688 tree cdim, array_type;
9689 int error_found = 0;
9690 int ndims = 0;
9691 int xdims = TREE_INT_CST_LOW (TREE_OPERAND (node, 2));
9692
9693 /* Dimension types are verified. It's better for the types to be
9694 verified in order. */
9695 for (cdim = dims, ndims = 0; cdim; cdim = TREE_CHAIN (cdim), ndims++ )
9696 {
9697 int dim_error = 0;
9698 tree dim = TREE_VALUE (cdim);
9699
9700 /* Dim might have been saved during its evaluation */
9701 dim = (TREE_CODE (dim) == SAVE_EXPR ? dim = TREE_OPERAND (dim, 0) : dim);
9702
9703 /* The type of each specified dimension must be an integral type. */
9704 if (!JINTEGRAL_TYPE_P (TREE_TYPE (dim)))
9705 dim_error = 1;
9706
9707 /* Each expression undergoes an unary numeric promotion (5.6.1) and the
9708 promoted type must be int. */
9709 else
9710 {
9711 dim = do_unary_numeric_promotion (dim);
9712 if (TREE_TYPE (dim) != int_type_node)
9713 dim_error = 1;
9714 }
9715
9716 /* Report errors on types here */
9717 if (dim_error)
9718 {
9719 parse_error_context
9720 (TREE_PURPOSE (cdim),
9721 "Incompatible type for dimension in array creation expression. "
9722 "%s convert `%s' to `int'",
9723 (valid_cast_to_p (TREE_TYPE (dim), int_type_node) ?
9724 "Explicit cast needed to" : "Can't"),
9725 lang_printable_name (TREE_TYPE (dim), 0));
9726 error_found = 1;
9727 }
9728
9729 TREE_PURPOSE (cdim) = NULL_TREE;
9730 }
9731
9732 /* Resolve array base type if unresolved */
9733 if (!(type = resolve_type_during_patch (type)))
9734 error_found = 1;
9735
9736 if (error_found)
9737 {
9738 /* We don't want further evaluation of this bogus array creation
9739 operation */
9740 TREE_TYPE (node) = error_mark_node;
9741 return error_mark_node;
9742 }
9743
9744 /* Set array_type to the actual (promoted) array type of the result. */
9745 if (TREE_CODE (type) == RECORD_TYPE)
9746 type = build_pointer_type (type);
9747 while (--xdims >= 0)
9748 {
9749 type = promote_type (build_java_array_type (type, -1));
9750 }
9751 dims = nreverse (dims);
9752 array_type = type;
9753 for (cdim = dims; cdim; cdim = TREE_CHAIN (cdim))
9754 {
9755 type = array_type;
9756 array_type = build_java_array_type (type,
9757 TREE_CODE (cdim) == INTEGER_CST ?
9758 TREE_INT_CST_LOW (cdim) : -1);
9759 array_type = promote_type (array_type);
9760 }
9761 dims = nreverse (dims);
9762
9763 /* The node is transformed into a function call. Things are done
9764 differently according to the number of dimensions. If the number
9765 of dimension is equal to 1, then the nature of the base type
9766 (primitive or not) matters. */
9767 if (ndims == 1)
9768 return build_new_array (type, TREE_VALUE (dims));
9769
9770 /* Can't reuse what's already written in expr.c because it uses the
9771 JVM stack representation. Provide a build_multianewarray. FIXME */
9772 return build (CALL_EXPR, array_type,
9773 build_address_of (soft_multianewarray_node),
9774 tree_cons (NULL_TREE, build_class_ref (TREE_TYPE (array_type)),
9775 tree_cons (NULL_TREE,
9776 build_int_2 (ndims, 0), dims )),
9777 NULL_TREE);
9778 }
9779
9780 /* 10.6 Array initializer. */
9781
9782 /* Build a wfl for array element that don't have one, so we can
9783 pin-point errors. */
9784
9785 static tree
9786 maybe_build_array_element_wfl (node)
9787 tree node;
9788 {
9789 if (TREE_CODE (node) != EXPR_WITH_FILE_LOCATION)
9790 return build_expr_wfl (NULL_TREE, ctxp->filename,
9791 ctxp->elc.line, ctxp->elc.prev_col);
9792 else
9793 return NULL_TREE;
9794 }
9795
9796 /* Build a NEW_ARRAY_INIT that features a CONSTRUCTOR node. This makes
9797 identification of initialized arrays easier to detect during walk
9798 and expansion. */
9799
9800 static tree
9801 build_new_array_init (location, values)
9802 int location;
9803 tree values;
9804 {
9805 tree constructor = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, values);
9806 tree to_return = build1 (NEW_ARRAY_INIT, NULL_TREE, constructor);
9807 EXPR_WFL_LINECOL (to_return) = EXPR_WFL_LINECOL (constructor) = location;
9808 return to_return;
9809 }
9810
9811 /* Expand a NEW_ARRAY_INIT node. Return error_mark_node if an error
9812 occurred. Otherwise return NODE after having set its type
9813 appropriately. */
9814
9815 static tree
9816 patch_new_array_init (type, node)
9817 tree type, node;
9818 {
9819 int error_seen = 0;
9820 tree current, element_type;
9821 HOST_WIDE_INT length;
9822 int all_constant = 1;
9823 tree init = TREE_OPERAND (node, 0);
9824
9825 if (TREE_CODE (type) != POINTER_TYPE || ! TYPE_ARRAY_P (TREE_TYPE (type)))
9826 {
9827 parse_error_context (node,
9828 "Invalid array initializer for non-array type `%s'",
9829 lang_printable_name (type, 1));
9830 return error_mark_node;
9831 }
9832 type = TREE_TYPE (type);
9833 element_type = TYPE_ARRAY_ELEMENT (type);
9834
9835 CONSTRUCTOR_ELTS (init) = nreverse (CONSTRUCTOR_ELTS (init));
9836
9837 for (length = 0, current = CONSTRUCTOR_ELTS (init);
9838 current; length++, current = TREE_CHAIN (current))
9839 {
9840 tree elt = TREE_VALUE (current);
9841 if (elt == NULL_TREE || TREE_CODE (elt) != NEW_ARRAY_INIT)
9842 {
9843 error_seen |= array_constructor_check_entry (element_type, current);
9844 if (! TREE_CONSTANT (TREE_VALUE (current)))
9845 all_constant = 0;
9846 }
9847 else
9848 {
9849 TREE_VALUE (current) = patch_new_array_init (element_type, elt);
9850 TREE_PURPOSE (current) = NULL_TREE;
9851 all_constant = 0;
9852 }
9853 if (elt && TREE_VALUE (elt) == error_mark_node)
9854 error_seen = 1;
9855 }
9856
9857 if (error_seen)
9858 return error_mark_node;
9859
9860 /* Create a new type. We can't reuse the one we have here by
9861 patching its dimension because it originally is of dimension -1
9862 hence reused by gcc. This would prevent triangular arrays. */
9863 type = build_java_array_type (element_type, length);
9864 TREE_TYPE (init) = TREE_TYPE (TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (type))));
9865 TREE_TYPE (node) = promote_type (type);
9866 TREE_CONSTANT (init) = all_constant;
9867 return node;
9868 }
9869
9870 /* Verify that one entry of the initializer element list can be
9871 assigned to the array base type. Report 1 if an error occurred, 0
9872 otherwise. */
9873
9874 static int
9875 array_constructor_check_entry (type, entry)
9876 tree type, entry;
9877 {
9878 char *array_type_string = NULL; /* For error reports */
9879 tree value, type_value, new_value, wfl_value, patched;
9880 int error_seen = 0;
9881
9882 new_value = NULL_TREE;
9883 wfl_value = TREE_VALUE (entry);
9884
9885 value = java_complete_tree (TREE_VALUE (entry));
9886 /* patch_string return error_mark_node if arg is error_mark_node */
9887 if ((patched = patch_string (value)))
9888 value = patched;
9889 if (value == error_mark_node)
9890 return 1;
9891
9892 type_value = TREE_TYPE (value);
9893
9894 /* At anytime, try_builtin_assignconv can report a warning on
9895 constant overflow during narrowing. */
9896 SET_WFL_OPERATOR (wfl_operator, TREE_PURPOSE (entry), wfl_value);
9897 new_value = try_builtin_assignconv (wfl_operator, type, value);
9898 if (!new_value && (new_value = try_reference_assignconv (type, value)))
9899 type_value = promote_type (type);
9900
9901 /* Check and report errors */
9902 if (!new_value)
9903 {
9904 char *msg = (!valid_cast_to_p (type_value, type) ?
9905 "Can't" : "Explicit cast needed to");
9906 if (!array_type_string)
9907 array_type_string = strdup (lang_printable_name (type, 1));
9908 parse_error_context
9909 (wfl_operator, "Incompatible type for array. %s convert `%s' to `%s'",
9910 msg, lang_printable_name (type_value, 1), array_type_string);
9911 error_seen = 1;
9912 }
9913
9914 if (new_value)
9915 {
9916 new_value = maybe_build_primttype_type_ref (new_value, wfl_operator);
9917 TREE_VALUE (entry) = new_value;
9918 }
9919
9920 if (array_type_string)
9921 free (array_type_string);
9922
9923 TREE_PURPOSE (entry) = NULL_TREE;
9924 return error_seen;
9925 }
9926
9927 static tree
9928 build_this (location)
9929 int location;
9930 {
9931 tree node = build_wfl_node (this_identifier_node, input_filename, 0, 0);
9932 TREE_SET_CODE (node, THIS_EXPR);
9933 EXPR_WFL_LINECOL (node) = location;
9934 return node;
9935 }
9936
9937 /* 14.15 The return statement. It builds a modify expression that
9938 assigns the returned value to the RESULT_DECL that hold the value
9939 to be returned. */
9940
9941 static tree
9942 build_return (location, op)
9943 int location;
9944 tree op;
9945 {
9946 tree node = build1 (RETURN_EXPR, NULL_TREE, op);
9947 EXPR_WFL_LINECOL (node) = location;
9948 node = build_debugable_stmt (location, node);
9949 return node;
9950 }
9951
9952 static tree
9953 patch_return (node)
9954 tree node;
9955 {
9956 tree return_exp = TREE_OPERAND (node, 0);
9957 tree meth = current_function_decl;
9958 tree mtype = TREE_TYPE (TREE_TYPE (current_function_decl));
9959 int error_found = 0;
9960
9961 TREE_TYPE (node) = error_mark_node;
9962 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
9963
9964 /* It's invalid to have a return value within a function that is
9965 declared with the keyword void or that is a constructor */
9966 if (return_exp && (mtype == void_type_node || DECL_CONSTRUCTOR_P (meth)))
9967 error_found = 1;
9968
9969 /* It's invalid to have a no return value within a function that
9970 isn't declared with the keyword `void' */
9971 if (!return_exp && (mtype != void_type_node && !DECL_CONSTRUCTOR_P (meth)))
9972 error_found = 2;
9973
9974 if (error_found)
9975 {
9976 if (!DECL_CONSTRUCTOR_P (meth))
9977 {
9978 char *t = strdup (lang_printable_name (mtype, 0));
9979 parse_error_context (wfl_operator,
9980 "`return' with%s value from `%s %s'",
9981 (error_found == 1 ? "" : "out"),
9982 t, lang_printable_name (meth, 0));
9983 free (t);
9984 }
9985 else
9986 parse_error_context (wfl_operator,
9987 "`return' with value from constructor `%s'",
9988 lang_printable_name (meth, 0));
9989 return error_mark_node;
9990 }
9991
9992 /* If we have a return_exp, build a modify expression and expand
9993 it. Note: at that point, the assignment is declared valid, but we
9994 may want to carry some more hacks */
9995 if (return_exp)
9996 {
9997 tree exp = java_complete_tree (return_exp);
9998 tree modify, patched;
9999
10000 /* If the function returned value and EXP are booleans, EXP has
10001 to be converted into the type of DECL_RESULT, which is integer
10002 (see complete_start_java_method) */
10003 if (TREE_TYPE (exp) == boolean_type_node &&
10004 TREE_TYPE (TREE_TYPE (meth)) == boolean_type_node)
10005 exp = convert_to_integer (TREE_TYPE (DECL_RESULT (meth)), exp);
10006
10007 /* `null' can be assigned to a function returning a reference */
10008 if (JREFERENCE_TYPE_P (TREE_TYPE (TREE_TYPE (meth))) &&
10009 exp == null_pointer_node)
10010 exp = build_null_of_type (TREE_TYPE (TREE_TYPE (meth)));
10011
10012 if ((patched = patch_string (exp)))
10013 exp = patched;
10014
10015 modify = build (MODIFY_EXPR, NULL_TREE, DECL_RESULT (meth), exp);
10016 EXPR_WFL_LINECOL (modify) = EXPR_WFL_LINECOL (node);
10017 modify = java_complete_tree (modify);
10018
10019 if (modify != error_mark_node)
10020 {
10021 TREE_SIDE_EFFECTS (modify) = 1;
10022 TREE_OPERAND (node, 0) = modify;
10023 }
10024 else
10025 return error_mark_node;
10026 }
10027 TREE_TYPE (node) = void_type_node;
10028 TREE_SIDE_EFFECTS (node) = 1;
10029 return node;
10030 }
10031
10032 /* 14.8 The if Statement */
10033
10034 static tree
10035 build_if_else_statement (location, expression, if_body, else_body)
10036 int location;
10037 tree expression, if_body, else_body;
10038 {
10039 tree node;
10040 if (!else_body)
10041 else_body = empty_stmt_node;
10042 node = build (COND_EXPR, NULL_TREE, expression, if_body, else_body);
10043 EXPR_WFL_LINECOL (node) = location;
10044 node = build_debugable_stmt (location, node);
10045 return node;
10046 }
10047
10048 static tree
10049 patch_if_else_statement (node)
10050 tree node;
10051 {
10052 tree expression = TREE_OPERAND (node, 0);
10053
10054 TREE_TYPE (node) = error_mark_node;
10055 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10056
10057 /* The type of expression must be boolean */
10058 if (TREE_TYPE (expression) != boolean_type_node
10059 && TREE_TYPE (expression) != promoted_boolean_type_node)
10060 {
10061 parse_error_context
10062 (wfl_operator,
10063 "Incompatible type for `if'. Can't convert `%s' to `boolean'",
10064 lang_printable_name (TREE_TYPE (expression), 0));
10065 return error_mark_node;
10066 }
10067
10068 TREE_TYPE (node) = void_type_node;
10069 TREE_SIDE_EFFECTS (node) = 1;
10070 CAN_COMPLETE_NORMALLY (node)
10071 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10072 | CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 2));
10073 return node;
10074 }
10075
10076 /* 14.6 Labeled Statements */
10077
10078 /* Action taken when a lableled statement is parsed. a new
10079 LABELED_BLOCK_EXPR is created. No statement is attached to the
10080 label, yet. */
10081
10082 static tree
10083 build_labeled_block (location, label)
10084 int location;
10085 tree label;
10086 {
10087 tree label_name = merge_qualified_name (label_id, label);
10088 tree label_decl, node;
10089
10090 /* Issue an error if we try to reuse a label that was previously
10091 declared */
10092 if (IDENTIFIER_LOCAL_VALUE (label_name))
10093 {
10094 EXPR_WFL_LINECOL (wfl_operator) = location;
10095 parse_error_context (wfl_operator, "Declaration of `%s' shadows "
10096 "a previous label declaration",
10097 IDENTIFIER_POINTER (label));
10098 EXPR_WFL_LINECOL (wfl_operator) =
10099 EXPR_WFL_LINECOL (IDENTIFIER_LOCAL_VALUE (label_name));
10100 parse_error_context (wfl_operator, "This is the location of the "
10101 "previous declaration of label `%s'",
10102 IDENTIFIER_POINTER (label));
10103 java_error_count--;
10104 }
10105
10106 label_decl = create_label_decl (label_name);
10107 node = build (LABELED_BLOCK_EXPR, NULL_TREE, label_decl, NULL_TREE);
10108 EXPR_WFL_LINECOL (node) = location;
10109 TREE_SIDE_EFFECTS (node) = 1;
10110 return node;
10111 }
10112
10113 /* Generate a label crafting a unique name for it. This is used to
10114 implicitely label loops that aren't the body part of labeled
10115 statement. */
10116
10117 static tree
10118 generate_labeled_block ()
10119 {
10120 return build_labeled_block (0, generate_name ());
10121 }
10122
10123 /* A labeled statement LBE is attached a statement. */
10124
10125 static tree
10126 complete_labeled_statement (lbe, statement)
10127 tree lbe; /* Labeled block expr */
10128 tree statement;
10129 {
10130 /* In anyways, tie the loop to its statement */
10131 LABELED_BLOCK_BODY (lbe) = statement;
10132
10133 /* Ok, if statement is a for loop, we have to attach the labeled
10134 statement to the block the for loop belongs to and return the
10135 block instead */
10136 if (TREE_CODE (statement) == LOOP_EXPR && IS_FOR_LOOP_P (statement))
10137 {
10138 java_method_add_stmt (current_function_decl, lbe);
10139 return exit_block ();
10140 }
10141
10142 return lbe;
10143 }
10144
10145 /* 14.10, 14.11, 14.12 Loop Statements */
10146
10147 /* Create an empty LOOP_EXPR and make it the last in the nested loop
10148 list. */
10149
10150 static tree
10151 build_new_loop (loop_body)
10152 tree loop_body;
10153 {
10154 tree loop = build (LOOP_EXPR, NULL_TREE, loop_body);
10155 TREE_SIDE_EFFECTS (loop) = 1;
10156 PUSH_LOOP (loop);
10157 return loop;
10158 }
10159
10160 /* Create a loop body according to the following structure:
10161 COMPOUND_EXPR
10162 COMPOUND_EXPR (loop main body)
10163 EXIT_EXPR (this order is for while/for loops.
10164 LABELED_BLOCK_EXPR the order is reversed for do loops)
10165 LABEL_DECL (continue occurding here branche at the
10166 BODY end of this labeled block)
10167 INCREMENT (if any)
10168
10169 REVERSED, if non zero, tells that the loop condition expr comes
10170 after the body, like in the do-while loop.
10171
10172 To obtain a loop, the loop body structure described above is
10173 encapsulated within a LOOP_EXPR surrounded by a LABELED_BLOCK_EXPR:
10174
10175 LABELED_BLOCK_EXPR
10176 LABEL_DECL (use this label to exit the loop)
10177 LOOP_EXPR
10178 <structure described above> */
10179
10180 static tree
10181 build_loop_body (location, condition, reversed)
10182 int location;
10183 tree condition;
10184 int reversed;
10185 {
10186 tree first, second, body;
10187
10188 condition = build (EXIT_EXPR, NULL_TREE, condition); /* Force walk */
10189 EXPR_WFL_LINECOL (condition) = location; /* For accurate error report */
10190 condition = build_debugable_stmt (location, condition);
10191 TREE_SIDE_EFFECTS (condition) = 1;
10192
10193 body = generate_labeled_block ();
10194 first = (reversed ? body : condition);
10195 second = (reversed ? condition : body);
10196 return
10197 build (COMPOUND_EXPR, NULL_TREE,
10198 build (COMPOUND_EXPR, NULL_TREE, first, second), empty_stmt_node);
10199 }
10200
10201 /* Install CONDITION (if any) and loop BODY (using REVERSED to tell
10202 their order) on the current loop. Unlink the current loop from the
10203 loop list. */
10204
10205 static tree
10206 complete_loop_body (location, condition, body, reversed)
10207 int location;
10208 tree condition, body;
10209 int reversed;
10210 {
10211 tree to_return = ctxp->current_loop;
10212 tree loop_body = LOOP_EXPR_BODY (to_return);
10213 if (condition)
10214 {
10215 tree cnode = LOOP_EXPR_BODY_CONDITION_EXPR (loop_body, reversed);
10216 /* We wrapped the EXIT_EXPR around a WFL so we can debug it.
10217 The real EXIT_EXPR is one operand further. */
10218 EXPR_WFL_LINECOL (cnode) = location;
10219 /* This one is for accurate error reports */
10220 EXPR_WFL_LINECOL (TREE_OPERAND (cnode, 0)) = location;
10221 TREE_OPERAND (TREE_OPERAND (cnode, 0), 0) = condition;
10222 }
10223 LOOP_EXPR_BODY_BODY_EXPR (loop_body, reversed) = body;
10224 POP_LOOP ();
10225 return to_return;
10226 }
10227
10228 /* Tailored version of complete_loop_body for FOR loops, when FOR
10229 loops feature the condition part */
10230
10231 static tree
10232 complete_for_loop (location, condition, update, body)
10233 int location;
10234 tree condition, update, body;
10235 {
10236 /* Put the condition and the loop body in place */
10237 tree loop = complete_loop_body (location, condition, body, 0);
10238 /* LOOP is the current loop which has been now popped of the loop
10239 stack. Install the update block */
10240 LOOP_EXPR_BODY_UPDATE_BLOCK (LOOP_EXPR_BODY (loop)) = update;
10241 return loop;
10242 }
10243
10244 /* If the loop isn't surrounded by a labeled statement, create one and
10245 insert LOOP as it's body. */
10246
10247 static tree
10248 patch_loop_statement (loop)
10249 tree loop;
10250 {
10251 tree loop_label, to_return_as_loop;
10252
10253 if (LOOP_HAS_LABEL_P (loop))
10254 {
10255 loop_label = ctxp->current_labeled_block;
10256 to_return_as_loop = loop;
10257 }
10258 else
10259 {
10260 loop_label = generate_labeled_block ();
10261 LABELED_BLOCK_BODY (loop_label) = loop;
10262 PUSH_LABELED_BLOCK (loop_label);
10263 to_return_as_loop = loop_label;
10264 }
10265 TREE_TYPE (to_return_as_loop) = void_type_node;
10266 return to_return_as_loop;
10267 }
10268
10269 /* 14.13, 14.14: break and continue Statements */
10270
10271 /* Build a break or a continue statement. a null NAME indicates an
10272 unlabeled break/continue statement. */
10273
10274 static tree
10275 build_bc_statement (location, is_break, name)
10276 int location, is_break;
10277 tree name;
10278 {
10279 tree break_continue, label_block_expr = NULL_TREE;
10280
10281 if (name)
10282 {
10283 if (!(label_block_expr = IDENTIFIER_LOCAL_VALUE
10284 (merge_qualified_name (label_id, EXPR_WFL_NODE (name)))))
10285 /* Null means that we don't have a target for this named
10286 break/continue. In this case, we make the target to be the
10287 label name, so that the error can be reported accuratly in
10288 patch_bc_statement. */
10289 label_block_expr = EXPR_WFL_NODE (name);
10290 }
10291 /* Unlabeled break/continue will be handled during the
10292 break/continue patch operation */
10293 break_continue
10294 = build (EXIT_BLOCK_EXPR, NULL_TREE, label_block_expr, NULL_TREE);
10295
10296 IS_BREAK_STMT_P (break_continue) = is_break;
10297 TREE_SIDE_EFFECTS (break_continue) = 1;
10298 EXPR_WFL_LINECOL (break_continue) = location;
10299 break_continue = build_debugable_stmt (location, break_continue);
10300 return break_continue;
10301 }
10302
10303 /* Verification of a break/continue statement. */
10304
10305 static tree
10306 patch_bc_statement (node)
10307 tree node;
10308 {
10309 tree bc_label = EXIT_BLOCK_LABELED_BLOCK (node), target_stmt;
10310 int is_unlabeled = 0;
10311 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10312
10313 /* Not having a target means that the break/continue statement is
10314 unlabeled. We try to find a decent label for it */
10315 if (!bc_label)
10316 {
10317 is_unlabeled = 1;
10318 /* There should be a loop/switch to branch to */
10319 if (ctxp->current_loop)
10320 {
10321 if (TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10322 {
10323 /* At that stage, we're in the loop body, which is
10324 encapsulated around a LABELED_BLOCK_EXPR. So searching
10325 the current loop label requires us to consider the
10326 labeled block before the current one. */
10327 if (!LOOP_HAS_LABEL_SKIP_P (ctxp->current_loop))
10328 fatal ("unlabeled loop has no installed label -- "
10329 "patch_bc_statement");
10330 bc_label = TREE_CHAIN (ctxp->current_labeled_block);
10331 }
10332 /* For a SWITCH statement, this is the current one */
10333 else
10334 bc_label = ctxp->current_labeled_block;
10335 }
10336 /* Not having a loop to break/continue to is an error */
10337 else
10338 {
10339 parse_error_context (wfl_operator, "`%s' must be in loop%s",
10340 (IS_BREAK_STMT_P (node) ? "break" : "continue"),
10341 (IS_BREAK_STMT_P (node) ? " or switch" : ""));
10342 return error_mark_node;
10343 }
10344 }
10345 /* Having an identifier here means that the target is unknown. */
10346 else if (TREE_CODE (bc_label) == IDENTIFIER_NODE)
10347 {
10348 parse_error_context (wfl_operator, "No label definition found for `%s'",
10349 IDENTIFIER_POINTER (bc_label));
10350 return error_mark_node;
10351 }
10352
10353 /* Find the statement we're targeting. */
10354 target_stmt = LABELED_BLOCK_BODY (bc_label);
10355
10356 /* 14.13 The break Statement */
10357 if (IS_BREAK_STMT_P (node))
10358 {
10359 /* Named break are always fine, as far as they have a target
10360 (already verified). Anonymous break need to target
10361 while/do/for/switch */
10362 if (is_unlabeled &&
10363 !(TREE_CODE (target_stmt) == LOOP_EXPR /* do/while/for */
10364 || TREE_CODE (target_stmt) == SWITCH_EXPR)) /* switch FIXME */
10365 {
10366 parse_error_context (wfl_operator,
10367 "`break' must be in loop or switch");
10368 return error_mark_node;
10369 }
10370 /* If previously unlabeled, install the new found label */
10371 if (is_unlabeled)
10372 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label;
10373 }
10374 /* 14.14 The continue Statement */
10375 /* The continue statement must always target a loop */
10376 else
10377 {
10378 if (TREE_CODE (target_stmt) != LOOP_EXPR) /* do/while/for */
10379 {
10380 parse_error_context (wfl_operator, "`continue' must be in loop");
10381 return error_mark_node;
10382 }
10383 /* Everything looks good. We can fix the `continue' jump to go
10384 at the place in the loop were the continue is. The continue
10385 is the current labeled block, by construction. */
10386 EXIT_BLOCK_LABELED_BLOCK (node) = bc_label = ctxp->current_labeled_block;
10387 }
10388
10389 CAN_COMPLETE_NORMALLY (bc_label) = 1;
10390
10391 /* Our break/continue don't return values. */
10392 TREE_TYPE (node) = void_type_node;
10393 /* Encapsulate the break within a compound statement so that it's
10394 expanded all the times by expand_expr (and not clobered
10395 sometimes, like after a if statement) */
10396 node = add_stmt_to_compound (NULL_TREE, void_type_node, node);
10397 TREE_SIDE_EFFECTS (node) = 1;
10398 return node;
10399 }
10400
10401 /* Process the exit expression belonging to a loop. Its type must be
10402 boolean. */
10403
10404 static tree
10405 patch_exit_expr (node)
10406 tree node;
10407 {
10408 tree expression = TREE_OPERAND (node, 0);
10409 TREE_TYPE (node) = error_mark_node;
10410 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10411
10412 /* The type of expression must be boolean */
10413 if (TREE_TYPE (expression) != boolean_type_node)
10414 {
10415 parse_error_context
10416 (wfl_operator,
10417 "Incompatible type for loop conditional. Can't convert `%s' to "
10418 "`boolean'",
10419 lang_printable_name (TREE_TYPE (expression), 0));
10420 return error_mark_node;
10421 }
10422 /* Now we know things are allright, invert the condition, fold and
10423 return */
10424 TREE_OPERAND (node, 0) =
10425 fold (build1 (TRUTH_NOT_EXPR, boolean_type_node, expression));
10426
10427 if (! integer_zerop (TREE_OPERAND (node, 0))
10428 && ctxp->current_loop != NULL_TREE
10429 && TREE_CODE (ctxp->current_loop) == LOOP_EXPR)
10430 CAN_COMPLETE_NORMALLY (ctxp->current_loop) = 1;
10431 if (! integer_onep (TREE_OPERAND (node, 0)))
10432 CAN_COMPLETE_NORMALLY (node) = 1;
10433
10434
10435 TREE_TYPE (node) = void_type_node;
10436 return node;
10437 }
10438
10439 /* 14.9 Switch statement */
10440
10441 static tree
10442 patch_switch_statement (node)
10443 tree node;
10444 {
10445 tree se = TREE_OPERAND (node, 0), se_type;
10446
10447 /* Complete the switch expression */
10448 se = TREE_OPERAND (node, 0) = java_complete_tree (se);
10449 se_type = TREE_TYPE (se);
10450 /* The type of the switch expression must be char, byte, short or
10451 int */
10452 if (!JINTEGRAL_TYPE_P (se_type))
10453 {
10454 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (node);
10455 parse_error_context (wfl_operator, "Incompatible type for `switch'. "
10456 "Can't convert `%s' to `int'",
10457 lang_printable_name (se_type, 0));
10458 /* This is what java_complete_tree will check */
10459 TREE_OPERAND (node, 0) = error_mark_node;
10460 return error_mark_node;
10461 }
10462
10463 TREE_OPERAND (node, 1) = java_complete_tree (TREE_OPERAND (node, 1));
10464
10465 /* Ready to return */
10466 if (TREE_CODE (TREE_OPERAND (node, 1)) == ERROR_MARK)
10467 {
10468 TREE_TYPE (node) = error_mark_node;
10469 return error_mark_node;
10470 }
10471 TREE_TYPE (node) = void_type_node;
10472 TREE_SIDE_EFFECTS (node) = 1;
10473 CAN_COMPLETE_NORMALLY (node)
10474 = CAN_COMPLETE_NORMALLY (TREE_OPERAND (node, 1))
10475 || ! SWITCH_HAS_DEFAULT (node);
10476 return node;
10477 }
10478
10479 /* 14.18 The try statement */
10480
10481 /* Wrap BLOCK around a LABELED_BLOCK, set DECL to the newly generated
10482 exit labeld and issue a jump to FINALLY_LABEL:
10483
10484 LABELED_BLOCK
10485 BLOCK
10486 <orignal_statments>
10487 DECL = &LABEL_DECL
10488 GOTO_EXPR
10489 FINALLY_LABEL
10490 LABEL_DECL */
10491
10492 static tree
10493 build_jump_to_finally (block, decl, finally_label, type)
10494 tree block, decl, finally_label, type;
10495 {
10496 tree stmt;
10497 tree new_block = build (LABELED_BLOCK_EXPR, type,
10498 create_label_decl (generate_name ()), block);
10499
10500 stmt = build (MODIFY_EXPR, void_type_node, decl,
10501 build_address_of (LABELED_BLOCK_LABEL (new_block)));
10502 TREE_SIDE_EFFECTS (stmt) = 1;
10503 CAN_COMPLETE_NORMALLY (stmt) = 1;
10504 add_stmt_to_block (block, type, stmt);
10505 stmt = build (GOTO_EXPR, void_type_node, finally_label);
10506 TREE_SIDE_EFFECTS (stmt) = 1;
10507 add_stmt_to_block (block, type, stmt);
10508 return new_block;
10509 }
10510
10511 static tree
10512 build_try_statement (location, try_block, catches, finally)
10513 int location;
10514 tree try_block, catches, finally;
10515 {
10516 tree node, rff;
10517
10518 if (finally && ! flag_emit_class_files)
10519 {
10520 /* This block defines a scope for the entire try[-catch]-finally
10521 sequence. It hold a local variable used to return from the
10522 finally using a computed goto. We call it
10523 return_from_finally (RFF). */
10524 rff = build_decl (VAR_DECL, generate_name (), return_address_type_node);
10525
10526 /* Modification of the try block. */
10527 try_block = build_jump_to_finally (try_block, rff,
10528 FINALLY_EXPR_LABEL (finally),
10529 NULL_TREE);
10530
10531 /* To the finally block: add the computed goto */
10532 add_stmt_to_block (FINALLY_EXPR_BLOCK (finally), NULL_TREE,
10533 build (GOTO_EXPR, void_type_node, rff));
10534
10535 /* Modification of each catch blocks, if any */
10536 if (catches)
10537 {
10538 tree catch, catch_decl, catch_block, stmt;
10539
10540 for (catch = catches; catch; catch = TREE_CHAIN (catch))
10541 TREE_OPERAND (catch, 0) =
10542 build_jump_to_finally (TREE_OPERAND (catch, 0), rff,
10543 FINALLY_EXPR_LABEL (finally),
10544 NULL_TREE);
10545
10546 /* Plus, at the end of the list, we add the catch clause that
10547 will catch an uncaught exception, call finally and rethrow it:
10548 BLOCK
10549 void *exception_parameter; (catch_decl)
10550 LABELED_BLOCK
10551 BLOCK
10552 exception_parameter = _Jv_exception_info ();
10553 RFF = &LABEL_DECL;
10554 goto finally;
10555 LABEL_DECL;
10556 CALL_EXPR
10557 Jv_ReThrow
10558 exception_parameter */
10559 catch_decl = build_decl (VAR_DECL, generate_name (), ptr_type_node);
10560 BUILD_ASSIGN_EXCEPTION_INFO (stmt, catch_decl);
10561 catch_block = build_expr_block (stmt, NULL_TREE);
10562 catch_block = build_jump_to_finally (catch_block, rff,
10563 FINALLY_EXPR_LABEL (finally),
10564 void_type_node);
10565 BUILD_THROW (stmt, catch_decl);
10566 catch_block = build_expr_block (catch_block, catch_decl);
10567 add_stmt_to_block (catch_block, void_type_node, stmt);
10568
10569 /* Link the new handler to the existing list as the first
10570 entry. It will be the last one to be generated. */
10571 catch = build1 (CATCH_EXPR, void_type_node, catch_block);
10572 TREE_CHAIN (catch) = catches;
10573 catches = catch;
10574 }
10575 }
10576
10577 node = build (TRY_EXPR, NULL_TREE, try_block, catches, finally);
10578 EXPR_WFL_LINECOL (node) = location;
10579
10580 /* If we have a finally, surround this whole thing by a block where
10581 the RFF local variable is defined. */
10582
10583 return (finally && ! flag_emit_class_files ? build_expr_block (node, rff)
10584 : node);
10585 }
10586
10587 /* Get the catch clause block from an element of the catch clause
10588 list. If depends on whether a finally clause exists or node (in
10589 which case the original catch clause was surrounded by a
10590 LABELED_BLOCK_EXPR. */
10591
10592 tree
10593 java_get_catch_block (node, finally_present_p)
10594 tree node;
10595 int finally_present_p;
10596 {
10597 return (CATCH_EXPR_GET_EXPR (TREE_OPERAND (node, 0), finally_present_p));
10598 }
10599
10600 static tree
10601 patch_try_statement (node)
10602 tree node;
10603 {
10604 int error_found = 0;
10605 tree try = TREE_OPERAND (node, 0);
10606 /* Exception handlers are considered in left to right order */
10607 tree catch = nreverse (TREE_OPERAND (node, 1));
10608 tree finally = TREE_OPERAND (node, 2);
10609 int finally_p = (finally ? 1 : 0);
10610 tree current, caught_type_list = NULL_TREE;
10611
10612 /* Check catch clauses, if any. Every time we find an error, we try
10613 to process the next catch clause. We process the catch clause before
10614 the try block so that when processing the try block we can check thrown
10615 exceptions againts the caught type list. */
10616 for (current = catch; current; current = TREE_CHAIN (current))
10617 {
10618 tree carg_decl, carg_type;
10619 tree sub_current, catch_block, catch_clause;
10620 int unreachable;
10621
10622 /* Always detect the last catch clause if a finally is
10623 present. This is the catch-all handler and it just needs to
10624 be walked. */
10625 if (!TREE_CHAIN (current) && finally)
10626 {
10627 TREE_OPERAND (current, 0) =
10628 java_complete_tree (TREE_OPERAND (current, 0));
10629 continue;
10630 }
10631
10632 /* At this point, the structure of the catch clause is
10633 LABELED_BLOCK_EXPR (if we have a finally)
10634 CATCH_EXPR (catch node)
10635 BLOCK (with the decl of the parameter)
10636 COMPOUND_EXPR
10637 MODIFY_EXPR (assignment of the catch parameter)
10638 BLOCK (catch clause block)
10639 LABEL_DECL (where to return after finally (if any))
10640
10641 Since the structure of the catch clause depends on the
10642 presence of a finally, we use a function call to get to the
10643 cath clause */
10644 catch_clause = java_get_catch_block (current, finally_p);
10645 carg_decl = BLOCK_EXPR_DECLS (catch_clause);
10646 carg_type = TREE_TYPE (TREE_TYPE (carg_decl));
10647
10648 /* Catch clauses can't have more than one parameter declared,
10649 but it's already enforced by the grammar. Make sure that the
10650 only parameter of the clause statement in of class Throwable
10651 or a subclass of Throwable, but that was done earlier. The
10652 catch clause parameter type has also been resolved. */
10653
10654 /* Just make sure that the catch clause parameter type inherits
10655 from java.lang.Throwable */
10656 if (!inherits_from_p (carg_type, throwable_type_node))
10657 {
10658 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10659 parse_error_context (wfl_operator,
10660 "Can't catch class `%s'. Catch clause "
10661 "parameter type must be a subclass of "
10662 "class `java.lang.Throwable'",
10663 lang_printable_name (carg_type, 0));
10664 error_found = 1;
10665 continue;
10666 }
10667
10668 /* Partial check for unreachable catch statement: The catch
10669 clause is reachable iff is no earlier catch block A in
10670 the try statement such that the type of the catch
10671 clause's parameter is the same as or a subclass of the
10672 type of A's parameter */
10673 unreachable = 0;
10674 for (sub_current = catch;
10675 sub_current != current; sub_current = TREE_CHAIN (sub_current))
10676 {
10677 tree sub_catch_clause, decl;
10678 sub_catch_clause = java_get_catch_block (sub_current, finally_p);
10679 decl = BLOCK_EXPR_DECLS (sub_catch_clause);
10680
10681 if (inherits_from_p (carg_type, TREE_TYPE (TREE_TYPE (decl))))
10682 {
10683 EXPR_WFL_LINECOL (wfl_operator) = EXPR_WFL_LINECOL (current);
10684 parse_error_context
10685 (wfl_operator, "`catch' not reached because of the catch "
10686 "clause at line %d", EXPR_WFL_LINENO (sub_current));
10687 unreachable = error_found = 1;
10688 break;
10689 }
10690 }
10691 /* Complete the catch clause block */
10692 catch_block = java_complete_tree (TREE_OPERAND (current, 0));
10693 if (catch_block == error_mark_node)
10694 {
10695 error_found = 1;
10696 continue;
10697 }
10698 if (CAN_COMPLETE_NORMALLY (catch_block))
10699 CAN_COMPLETE_NORMALLY (node) = 1;
10700 TREE_OPERAND (current, 0) = catch_block;
10701
10702 if (unreachable)
10703 continue;
10704
10705 /* Things to do here: the exception must be thrown */
10706
10707 /* Link this type to the caught type list */
10708 caught_type_list = tree_cons (NULL_TREE, carg_type, caught_type_list);
10709 }
10710
10711 PUSH_EXCEPTIONS (caught_type_list);
10712 if ((try = java_complete_tree (try)) == error_mark_node)
10713 error_found = 1;
10714 if (CAN_COMPLETE_NORMALLY (try))
10715 CAN_COMPLETE_NORMALLY (node) = 1;
10716 POP_EXCEPTIONS ();
10717
10718 /* Process finally */
10719 if (finally)
10720 {
10721 current = java_complete_tree (FINALLY_EXPR_BLOCK (finally));
10722 FINALLY_EXPR_BLOCK (finally) = current;
10723 if (current == error_mark_node)
10724 error_found = 1;
10725 if (! CAN_COMPLETE_NORMALLY (current))
10726 CAN_COMPLETE_NORMALLY (node) = 0;
10727 }
10728
10729 /* Verification ends here */
10730 if (error_found)
10731 return error_mark_node;
10732
10733 TREE_OPERAND (node, 0) = try;
10734 TREE_OPERAND (node, 1) = catch;
10735 TREE_OPERAND (node, 2) = finally;
10736 TREE_TYPE (node) = void_type_node;
10737 return node;
10738 }
10739
10740 /* 14.17 The synchronized Statement */
10741
10742 static tree
10743 patch_synchronized_statement (node, wfl_op1)
10744 tree node, wfl_op1;
10745 {
10746 tree expr = java_complete_tree (TREE_OPERAND (node, 0));
10747 tree block = TREE_OPERAND (node, 1);
10748
10749 tree enter, exit, finally, expr_decl;
10750
10751 if (expr == error_mark_node)
10752 {
10753 block = java_complete_tree (block);
10754 return expr;
10755 }
10756
10757 /* The TYPE of expr must be a reference type */
10758 if (!JREFERENCE_TYPE_P (TREE_TYPE (expr)))
10759 {
10760 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10761 parse_error_context (wfl_operator, "Incompatible type for `synchronized'"
10762 ". Can't convert `%s' to `java.lang.Object'",
10763 lang_printable_name (TREE_TYPE (expr), 0));
10764 return error_mark_node;
10765 }
10766
10767 /* Generate a try-finally for the synchronized statement, except
10768 that the handler that catches all throw exception calls
10769 _Jv_MonitorExit and then rethrow the exception.
10770 The synchronized statement is then implemented as:
10771 TRY
10772 {
10773 _Jv_MonitorEnter (expression)
10774 synchronized_block
10775 _Jv_MonitorExit (expression)
10776 }
10777 CATCH_ALL
10778 {
10779 e = _Jv_exception_info ();
10780 _Jv_MonitorExit (expression)
10781 Throw (e);
10782 } */
10783
10784 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
10785 BUILD_MONITOR_ENTER (enter, expr_decl);
10786 BUILD_MONITOR_EXIT (exit, expr_decl);
10787 CAN_COMPLETE_NORMALLY (enter) = 1;
10788 CAN_COMPLETE_NORMALLY (exit) = 1;
10789 node = build1 (CLEANUP_POINT_EXPR, NULL_TREE,
10790 build (COMPOUND_EXPR, NULL_TREE,
10791 build (WITH_CLEANUP_EXPR, NULL_TREE,
10792 build (COMPOUND_EXPR, NULL_TREE,
10793 build (MODIFY_EXPR, NULL_TREE,
10794 expr_decl, expr),
10795 enter),
10796 NULL_TREE, exit),
10797 block));
10798 node = build_expr_block (node, expr_decl);
10799
10800 return java_complete_tree (node);
10801 }
10802
10803 /* 14.16 The throw Statement */
10804
10805 static tree
10806 patch_throw_statement (node, wfl_op1)
10807 tree node, wfl_op1;
10808 {
10809 tree expr = TREE_OPERAND (node, 0);
10810 tree type = TREE_TYPE (expr);
10811 int unchecked_ok = 0, tryblock_throws_ok = 0;
10812
10813 /* Thrown expression must be assignable to java.lang.Throwable */
10814 if (!try_reference_assignconv (throwable_type_node, expr))
10815 {
10816 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10817 parse_error_context (wfl_operator, "Can't throw `%s'; it must be a "
10818 "subclass of class `java.lang.Throwable'",
10819 lang_printable_name (type, 0));
10820 /* If the thrown expression was a reference, we further the
10821 compile-time check. */
10822 if (!JREFERENCE_TYPE_P (type))
10823 return error_mark_node;
10824 }
10825
10826 /* At least one of the following must be true */
10827
10828 /* The type of the throw expression is a not checked exception,
10829 i.e. is a unchecked expression. */
10830 unchecked_ok = IS_UNCHECKED_EXCEPTION_P (TREE_TYPE (type));
10831
10832 /* Throw is contained in a try statement and at least one catch
10833 clause can receive the thrown expression or the current method is
10834 declared to throw such an exception. Or, the throw statement is
10835 contained in a method or constructor declaration and the type of
10836 the Expression is assignable to at least one type listed in the
10837 throws clause the declaration. */
10838 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
10839 if (!unchecked_ok)
10840 tryblock_throws_ok =
10841 check_thrown_exceptions_do (TREE_TYPE (expr));
10842 if (!(unchecked_ok || tryblock_throws_ok))
10843 {
10844 /* If there is a surrounding try block that has no matching
10845 clatch clause, report it first. A surrounding try block exits
10846 only if there is something after the list of checked
10847 exception thrown by the current function (if any). */
10848 if (IN_TRY_BLOCK_P ())
10849 parse_error_context (wfl_operator, "Checked exception `%s' can't be "
10850 "caught by any of the catch clause(s) "
10851 "of the surrounding `try' block",
10852 lang_printable_name (type, 0));
10853 /* If we have no surrounding try statement and the method doesn't have
10854 any throws, report it now. FIXME */
10855 else if (!EXCEPTIONS_P (currently_caught_type_list)
10856 && !tryblock_throws_ok)
10857 parse_error_context (wfl_operator, "Checked exception `%s' isn't "
10858 "thrown from a `try' block",
10859 lang_printable_name (type, 0));
10860 /* Otherwise, the current method doesn't have the appropriate
10861 throws declaration */
10862 else
10863 parse_error_context (wfl_operator, "Checked exception `%s' doesn't "
10864 "match any of current method's `throws' "
10865 "declaration(s)",
10866 lang_printable_name (type, 0));
10867 return error_mark_node;
10868 }
10869
10870 /* If a throw statement is contained in a static initializer, then a
10871 compile-time check ensures that either its value is always an
10872 unchecked exception or its value is always caught by some try
10873 statement that contains it. FIXME, static initializer. */
10874
10875 if (! flag_emit_class_files)
10876 BUILD_THROW (node, expr);
10877 return node;
10878 }
10879
10880 /* Check that exception said to be thrown by method DECL can be
10881 effectively caught from where DECL is invoked. */
10882
10883 static void
10884 check_thrown_exceptions (location, decl)
10885 int location;
10886 tree decl;
10887 {
10888 tree throws;
10889 /* For all the unchecked exceptions thrown by DECL */
10890 for (throws = DECL_FUNCTION_THROWS (decl); throws;
10891 throws = TREE_CHAIN (throws))
10892 if (!check_thrown_exceptions_do (TREE_VALUE (throws)))
10893 {
10894 #if 1
10895 /* Temporary hack to suppresses errors about cloning arrays. FIXME */
10896 if (DECL_NAME (decl) == get_identifier ("clone"))
10897 continue;
10898 #endif
10899 EXPR_WFL_LINECOL (wfl_operator) = location;
10900 parse_error_context
10901 (wfl_operator, "Exception `%s' must be caught, or it must be "
10902 "declared in the `throws' clause of `%s'",
10903 lang_printable_name (TREE_VALUE (throws), 0),
10904 IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
10905 }
10906 }
10907
10908 /* Return 1 if checked EXCEPTION is caught at the current nesting level of
10909 try-catch blocks, OR is listed in the `throws' clause of the
10910 current method. */
10911
10912 static int
10913 check_thrown_exceptions_do (exception)
10914 tree exception;
10915 {
10916 tree list = currently_caught_type_list;
10917 resolve_and_layout (exception, NULL_TREE);
10918 /* First, all the nested try-catch-finally at that stage. The
10919 last element contains `throws' clause exceptions, if any. */
10920 if (IS_UNCHECKED_EXCEPTION_P (exception))
10921 return 1;
10922 while (list)
10923 {
10924 tree caught;
10925 for (caught = TREE_VALUE (list); caught; caught = TREE_CHAIN (caught))
10926 if (valid_ref_assignconv_cast_p (exception, TREE_VALUE (caught), 0))
10927 return 1;
10928 list = TREE_CHAIN (list);
10929 }
10930 return 0;
10931 }
10932
10933 static void
10934 purge_unchecked_exceptions (mdecl)
10935 tree mdecl;
10936 {
10937 tree throws = DECL_FUNCTION_THROWS (mdecl);
10938 tree new = NULL_TREE;
10939
10940 while (throws)
10941 {
10942 tree next = TREE_CHAIN (throws);
10943 if (!IS_UNCHECKED_EXCEPTION_P (TREE_VALUE (throws)))
10944 {
10945 TREE_CHAIN (throws) = new;
10946 new = throws;
10947 }
10948 throws = next;
10949 }
10950 /* List is inverted here, but it doesn't matter */
10951 DECL_FUNCTION_THROWS (mdecl) = new;
10952 }
10953
10954 /* 15.24 Conditional Operator ?: */
10955
10956 static tree
10957 patch_conditional_expr (node, wfl_cond, wfl_op1)
10958 tree node, wfl_cond, wfl_op1;
10959 {
10960 tree cond = TREE_OPERAND (node, 0);
10961 tree op1 = TREE_OPERAND (node, 1);
10962 tree op2 = TREE_OPERAND (node, 2);
10963 tree resulting_type = NULL_TREE;
10964 tree t1, t2, patched;
10965 int error_found = 0;
10966
10967 /* Operands of ?: might be StringBuffers crafted as a result of a
10968 string concatenation. Obtain a descent operand here. */
10969 if ((patched = patch_string (op1)))
10970 TREE_OPERAND (node, 1) = op1 = patched;
10971 if ((patched = patch_string (op2)))
10972 TREE_OPERAND (node, 2) = op2 = patched;
10973
10974 t1 = TREE_TYPE (op1);
10975 t2 = TREE_TYPE (op2);
10976
10977 /* The first expression must be a boolean */
10978 if (TREE_TYPE (cond) != boolean_type_node)
10979 {
10980 SET_WFL_OPERATOR (wfl_operator, node, wfl_cond);
10981 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
10982 "convert `%s' to `boolean'",
10983 lang_printable_name (TREE_TYPE (cond), 0));
10984 error_found = 1;
10985 }
10986
10987 /* Second and third can be numeric, boolean (i.e. primitive),
10988 references or null. Anything else results in an error */
10989 if (!((JNUMERIC_TYPE_P (t1) && JNUMERIC_TYPE_P (t2))
10990 || ((JREFERENCE_TYPE_P (t1) || op1 == null_pointer_node)
10991 && (JREFERENCE_TYPE_P (t2) || op2 == null_pointer_node))
10992 || (t1 == boolean_type_node && t2 == boolean_type_node)))
10993 error_found = 1;
10994
10995 /* Determine the type of the conditional expression. Same types are
10996 easy to deal with */
10997 else if (t1 == t2)
10998 resulting_type = t1;
10999
11000 /* There are different rules for numeric types */
11001 else if (JNUMERIC_TYPE_P (t1))
11002 {
11003 /* if byte/short found, the resulting type is short */
11004 if ((t1 == byte_type_node && t2 == short_type_node)
11005 || (t1 == short_type_node && t2 == byte_type_node))
11006 resulting_type = short_type_node;
11007
11008 /* If t1 is a constant int and t2 is of type byte, short or char
11009 and t1's value fits in t2, then the resulting type is t2 */
11010 else if ((t1 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 1)))
11011 && JBSC_TYPE_P (t2) && int_fits_type_p (TREE_OPERAND (node, 1), t2))
11012 resulting_type = t2;
11013
11014 /* If t2 is a constant int and t1 is of type byte, short or char
11015 and t2's value fits in t1, then the resulting type is t1 */
11016 else if ((t2 == int_type_node && TREE_CONSTANT (TREE_OPERAND (node, 2)))
11017 && JBSC_TYPE_P (t1) && int_fits_type_p (TREE_OPERAND (node, 2), t1))
11018 resulting_type = t1;
11019
11020 /* Otherwise, binary numeric promotion is applied and the
11021 resulting type is the promoted type of operand 1 and 2 */
11022 else
11023 resulting_type = binary_numeric_promotion (t2, t2,
11024 &TREE_OPERAND (node, 1),
11025 &TREE_OPERAND (node, 2));
11026 }
11027
11028 /* Cases of a reference and a null type */
11029 else if (JREFERENCE_TYPE_P (t1) && op2 == null_pointer_node)
11030 resulting_type = t1;
11031
11032 else if (JREFERENCE_TYPE_P (t2) && op1 == null_pointer_node)
11033 resulting_type = t2;
11034
11035 /* Last case: different reference types. If a type can be converted
11036 into the other one by assignment conversion, the latter
11037 determines the type of the expression */
11038 else if ((resulting_type = try_reference_assignconv (t1, op2)))
11039 resulting_type = promote_type (t1);
11040
11041 else if ((resulting_type = try_reference_assignconv (t2, op1)))
11042 resulting_type = promote_type (t2);
11043
11044 /* If we don't have any resulting type, we're in trouble */
11045 if (!resulting_type)
11046 {
11047 char *t = strdup (lang_printable_name (t1, 0));
11048 SET_WFL_OPERATOR (wfl_operator, node, wfl_op1);
11049 parse_error_context (wfl_operator, "Incompatible type for `?:'. Can't "
11050 "convert `%s' to `%s'", t,
11051 lang_printable_name (t2, 0));
11052 free (t);
11053 error_found = 1;
11054 }
11055
11056 if (error_found)
11057 {
11058 TREE_TYPE (node) = error_mark_node;
11059 return error_mark_node;
11060 }
11061
11062 TREE_TYPE (node) = resulting_type;
11063 TREE_SET_CODE (node, COND_EXPR);
11064 CAN_COMPLETE_NORMALLY (node) = 1;
11065 return node;
11066 }
11067
11068 /* Try to constant fold NODE.
11069 If NODE is not a constant expression, return NULL_EXPR.
11070 CONTEXT is a static final VAR_DECL whose initializer we are folding. */
11071
11072 static tree
11073 fold_constant_for_init (node, context)
11074 tree node;
11075 tree context;
11076 {
11077 tree op0, op1, val;
11078 enum tree_code code = TREE_CODE (node);
11079
11080 if (code == INTEGER_CST || code == REAL_CST || code == STRING_CST)
11081 return node;
11082 if (TREE_TYPE (node) != NULL_TREE)
11083 return NULL_TREE;
11084
11085 switch (code)
11086 {
11087 case MULT_EXPR:
11088 case PLUS_EXPR:
11089 case MINUS_EXPR:
11090 case LSHIFT_EXPR:
11091 case RSHIFT_EXPR:
11092 case URSHIFT_EXPR:
11093 case BIT_AND_EXPR:
11094 case BIT_XOR_EXPR:
11095 case BIT_IOR_EXPR:
11096 case TRUNC_MOD_EXPR:
11097 case RDIV_EXPR:
11098 case TRUTH_ANDIF_EXPR:
11099 case TRUTH_ORIF_EXPR:
11100 case EQ_EXPR:
11101 case NE_EXPR:
11102 case GT_EXPR:
11103 case GE_EXPR:
11104 case LT_EXPR:
11105 case LE_EXPR:
11106 op0 = TREE_OPERAND (node, 0);
11107 op1 = TREE_OPERAND (node, 1);
11108 val = fold_constant_for_init (op0, context);
11109 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11110 return NULL_TREE;
11111 TREE_OPERAND (node, 0) = val;
11112 val = fold_constant_for_init (op1, context);
11113 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11114 return NULL_TREE;
11115 TREE_OPERAND (node, 1) = val;
11116 return patch_binop (node, op0, op1);
11117
11118 case UNARY_PLUS_EXPR:
11119 case NEGATE_EXPR:
11120 case TRUTH_NOT_EXPR:
11121 case BIT_NOT_EXPR:
11122 case CONVERT_EXPR:
11123 op0 = TREE_OPERAND (node, 0);
11124 val = fold_constant_for_init (op0, context);
11125 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11126 return NULL_TREE;
11127 TREE_OPERAND (node, 0) = val;
11128 return patch_unaryop (node, op0);
11129 break;
11130
11131 case COND_EXPR:
11132 val = fold_constant_for_init (TREE_OPERAND (node, 0), context);
11133 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11134 return NULL_TREE;
11135 TREE_OPERAND (node, 0) = val;
11136 val = fold_constant_for_init (TREE_OPERAND (node, 1), context);
11137 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11138 return NULL_TREE;
11139 TREE_OPERAND (node, 1) = val;
11140 val = fold_constant_for_init (TREE_OPERAND (node, 2), context);
11141 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11142 return NULL_TREE;
11143 TREE_OPERAND (node, 2) = val;
11144 return integer_zerop (TREE_OPERAND (node, 0)) ? TREE_OPERAND (node, 1)
11145 : TREE_OPERAND (node, 2);
11146
11147 case VAR_DECL:
11148 if (! FIELD_STATIC (node) || ! FIELD_FINAL (node)
11149 || DECL_INITIAL (node) == NULL_TREE)
11150 return NULL_TREE;
11151 val = DECL_INITIAL (node);
11152 /* Guard against infinite recursion. */
11153 DECL_INITIAL (node) = NULL_TREE;
11154 val = fold_constant_for_init (val, DECL_CONTEXT (node));
11155 DECL_INITIAL (node) = val;
11156 return val;
11157
11158 case EXPR_WITH_FILE_LOCATION:
11159 /* Compare java_complete_tree and resolve_expression_name. */
11160 if (!EXPR_WFL_NODE (node) /* Or a PRIMARY flag ? */
11161 || TREE_CODE (EXPR_WFL_NODE (node)) == IDENTIFIER_NODE)
11162 {
11163 tree name = EXPR_WFL_NODE (node);
11164 tree decl;
11165 if (PRIMARY_P (node))
11166 return NULL_TREE;
11167 else if (! QUALIFIED_P (name))
11168 {
11169 decl = lookup_field_wrapper (DECL_CONTEXT (context), name);
11170 if (! FIELD_STATIC (decl))
11171 return NULL_TREE;
11172 return fold_constant_for_init (decl, decl);
11173 }
11174 else
11175 {
11176 #if 0
11177 /* Wait until the USE_COMPONENT_REF re-write. FIXME. */
11178 qualify_ambiguous_name (node);
11179 if (resolve_field_access (node, &decl, NULL)
11180 && decl != NULL_TREE)
11181 return fold_constant_for_init (decl, decl);
11182 #endif
11183 return NULL_TREE;
11184 }
11185 }
11186 else
11187 {
11188 op0 = TREE_OPERAND (node, 0);
11189 val = fold_constant_for_init (op0, context);
11190 if (val == NULL_TREE || ! TREE_CONSTANT (val))
11191 return NULL_TREE;
11192 TREE_OPERAND (node, 0) = val;
11193 return val;
11194 }
11195
11196 default:
11197 return NULL_TREE;
11198 }
11199 }
This page took 0.484257 seconds and 6 git commands to generate.