re PR tree-optimization/38835 (field-insensitive PTA causes libstdc++ miscompiles)
[gcc.git] / gcc / java / typeck.c
1 /* Handle types for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2007, 2008
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
20
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24
25 /* Written by Per Bothner <bothner@cygnus.com> */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "real.h"
33 #include "obstack.h"
34 #include "flags.h"
35 #include "java-tree.h"
36 #include "jcf.h"
37 #include "convert.h"
38 #include "toplev.h"
39 #include "ggc.h"
40
41 static tree convert_ieee_real_to_integer (tree, tree);
42 static tree parse_signature_type (const unsigned char **,
43 const unsigned char *);
44 static tree lookup_do (tree, int, tree, tree, tree (*)(tree));
45 static tree build_null_signature (tree);
46
47 tree * type_map;
48
49 /* Set the type of the local variable with index SLOT to TYPE. */
50
51 void
52 set_local_type (int slot, tree type)
53 {
54 int max_locals = DECL_MAX_LOCALS(current_function_decl);
55 int nslots = TYPE_IS_WIDE (type) ? 2 : 1;
56
57 gcc_assert (slot >= 0 && (slot + nslots - 1 < max_locals));
58
59 type_map[slot] = type;
60 while (--nslots > 0)
61 type_map[++slot] = void_type_node;
62 }
63
64 /* Convert an IEEE real to an integer type. The result of such a
65 conversion when the source operand is a NaN isn't defined by
66 IEEE754, but by the Java language standard: it must be zero. Also,
67 overflows must be clipped to within range. This conversion
68 produces something like:
69
70 ((expr >= (float)MAX_INT)
71 ? MAX_INT
72 : ((expr <= (float)MIN_INT)
73 ? MIN_INT
74 : ((expr != expr)
75 ? 0
76 : (int)expr))) */
77
78 static tree
79 convert_ieee_real_to_integer (tree type, tree expr)
80 {
81 tree result;
82 expr = save_expr (expr);
83
84 result = fold_build3 (COND_EXPR, type,
85 fold_build2 (NE_EXPR, boolean_type_node, expr, expr),
86 convert (type, integer_zero_node),
87 convert_to_integer (type, expr));
88
89 result = fold_build3 (COND_EXPR, type,
90 fold_build2 (LE_EXPR, boolean_type_node, expr,
91 convert (TREE_TYPE (expr),
92 TYPE_MIN_VALUE (type))),
93 TYPE_MIN_VALUE (type),
94 result);
95
96 result = fold_build3 (COND_EXPR, type,
97 fold_build2 (GE_EXPR, boolean_type_node, expr,
98 convert (TREE_TYPE (expr),
99 TYPE_MAX_VALUE (type))),
100 TYPE_MAX_VALUE (type),
101 result);
102
103 return result;
104 }
105
106 /* Create an expression whose value is that of EXPR,
107 converted to type TYPE. The TREE_TYPE of the value
108 is always TYPE. This function implements all reasonable
109 conversions; callers should filter out those that are
110 not permitted by the language being compiled. */
111
112 tree
113 convert (tree type, tree expr)
114 {
115 enum tree_code code = TREE_CODE (type);
116
117 if (!expr)
118 return error_mark_node;
119
120 if (type == TREE_TYPE (expr)
121 || TREE_CODE (expr) == ERROR_MARK)
122 return expr;
123 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
124 return error_mark_node;
125 if (code == VOID_TYPE)
126 return build1 (CONVERT_EXPR, type, expr);
127 if (code == BOOLEAN_TYPE)
128 return fold_convert (type, expr);
129 if (code == INTEGER_TYPE)
130 {
131 if (type == char_type_node || type == promoted_char_type_node)
132 return fold_convert (type, expr);
133 if ((really_constant_p (expr) || ! flag_unsafe_math_optimizations)
134 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
135 return convert_ieee_real_to_integer (type, expr);
136 else
137 {
138 /* fold very helpfully sets the overflow status if a type
139 overflows in a narrowing integer conversion, but Java
140 doesn't care. */
141 tree tmp = fold (convert_to_integer (type, expr));
142 if (TREE_CODE (tmp) == INTEGER_CST)
143 TREE_OVERFLOW (tmp) = 0;
144 return tmp;
145 }
146 }
147 if (code == REAL_TYPE)
148 return fold (convert_to_real (type, expr));
149 if (code == POINTER_TYPE)
150 return fold (convert_to_pointer (type, expr));
151 error ("conversion to non-scalar type requested");
152 return error_mark_node;
153 }
154
155
156 /* Return a data type that has machine mode MODE.
157 If the mode is an integer,
158 then UNSIGNEDP selects between signed and unsigned types. */
159
160 tree
161 java_type_for_mode (enum machine_mode mode, int unsignedp)
162 {
163 if (mode == TYPE_MODE (int_type_node))
164 return unsignedp ? unsigned_int_type_node : int_type_node;
165 if (mode == TYPE_MODE (long_type_node))
166 return unsignedp ? unsigned_long_type_node : long_type_node;
167 if (mode == TYPE_MODE (short_type_node))
168 return unsignedp ? unsigned_short_type_node : short_type_node;
169 if (mode == TYPE_MODE (byte_type_node))
170 return unsignedp ? unsigned_byte_type_node : byte_type_node;
171 if (mode == TYPE_MODE (float_type_node))
172 return float_type_node;
173 if (mode == TYPE_MODE (double_type_node))
174 return double_type_node;
175
176 return 0;
177 }
178
179 /* Return an integer type with BITS bits of precision,
180 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
181
182 tree
183 java_type_for_size (unsigned bits, int unsignedp)
184 {
185 if (bits <= TYPE_PRECISION (byte_type_node))
186 return unsignedp ? unsigned_byte_type_node : byte_type_node;
187 if (bits <= TYPE_PRECISION (short_type_node))
188 return unsignedp ? unsigned_short_type_node : short_type_node;
189 if (bits <= TYPE_PRECISION (int_type_node))
190 return unsignedp ? unsigned_int_type_node : int_type_node;
191 if (bits <= TYPE_PRECISION (long_type_node))
192 return unsignedp ? unsigned_long_type_node : long_type_node;
193 return 0;
194 }
195
196 /* Mark EXP saying that we need to be able to take the
197 address of it; it should not be allocated in a register.
198 Value is true if successful. */
199
200 bool
201 java_mark_addressable (tree exp)
202 {
203 tree x = exp;
204 while (1)
205 switch (TREE_CODE (x))
206 {
207 case ADDR_EXPR:
208 case COMPONENT_REF:
209 case ARRAY_REF:
210 case REALPART_EXPR:
211 case IMAGPART_EXPR:
212 x = TREE_OPERAND (x, 0);
213 break;
214
215 case TRUTH_ANDIF_EXPR:
216 case TRUTH_ORIF_EXPR:
217 case COMPOUND_EXPR:
218 x = TREE_OPERAND (x, 1);
219 break;
220
221 case COND_EXPR:
222 return java_mark_addressable (TREE_OPERAND (x, 1))
223 && java_mark_addressable (TREE_OPERAND (x, 2));
224
225 case CONSTRUCTOR:
226 TREE_ADDRESSABLE (x) = 1;
227 return true;
228
229 case INDIRECT_REF:
230 /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
231 incompatibility problems. Handle this case by marking FOO. */
232 if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
233 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
234 {
235 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
236 break;
237 }
238 if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
239 {
240 x = TREE_OPERAND (x, 0);
241 break;
242 }
243 return true;
244
245 case VAR_DECL:
246 case CONST_DECL:
247 case PARM_DECL:
248 case RESULT_DECL:
249 case FUNCTION_DECL:
250 TREE_ADDRESSABLE (x) = 1;
251 #if 0 /* poplevel deals with this now. */
252 if (DECL_CONTEXT (x) == 0)
253 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
254 #endif
255 /* drops through */
256 default:
257 return true;
258 }
259 }
260
261 /* Thorough checking of the arrayness of TYPE. */
262
263 int
264 is_array_type_p (tree type)
265 {
266 return TREE_CODE (type) == POINTER_TYPE
267 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
268 && TYPE_ARRAY_P (TREE_TYPE (type));
269 }
270
271 /* Return the length of a Java array type.
272 Return -1 if the length is unknown or non-constant. */
273
274 HOST_WIDE_INT
275 java_array_type_length (tree array_type)
276 {
277 tree arfld;
278 if (TREE_CODE (array_type) == POINTER_TYPE)
279 array_type = TREE_TYPE (array_type);
280 arfld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
281 if (arfld != NULL_TREE)
282 {
283 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
284 if (index_type != NULL_TREE)
285 {
286 tree high = TYPE_MAX_VALUE (index_type);
287 if (TREE_CODE (high) == INTEGER_CST)
288 return TREE_INT_CST_LOW (high) + 1;
289 }
290 }
291 return -1;
292 }
293
294 /* An array of unknown length will be ultimately given a length of
295 -2, so that we can still have `length' producing a negative value
296 even if found. This was part of an optimization aiming at removing
297 `length' from static arrays. We could restore it, FIXME. */
298
299 tree
300 build_prim_array_type (tree element_type, HOST_WIDE_INT length)
301 {
302 tree index = NULL;
303
304 if (length != -1)
305 {
306 tree max_index = build_int_cst (sizetype, length - 1);
307 index = build_index_type (max_index);
308 }
309 return build_array_type (element_type, index);
310 }
311
312 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
313 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
314 The LENGTH is -1 if the length is unknown. */
315
316 tree
317 build_java_array_type (tree element_type, HOST_WIDE_INT length)
318 {
319 tree sig, t, fld, atype, arfld;
320 char buf[23];
321 tree elsig = build_java_signature (element_type);
322 tree el_name = element_type;
323 buf[0] = '[';
324 if (length >= 0)
325 sprintf (buf+1, HOST_WIDE_INT_PRINT_DEC, length);
326 else
327 buf[1] = '\0';
328 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
329 buf, 0, 0, "");
330 t = IDENTIFIER_SIGNATURE_TYPE (sig);
331 if (t != NULL_TREE)
332 return TREE_TYPE (t);
333 t = make_class ();
334 IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
335 TYPE_ARRAY_P (t) = 1;
336
337 if (TREE_CODE (el_name) == POINTER_TYPE)
338 el_name = TREE_TYPE (el_name);
339 el_name = TYPE_NAME (el_name);
340 if (TREE_CODE (el_name) == TYPE_DECL)
341 el_name = DECL_NAME (el_name);
342 {
343 char suffix[23];
344 if (length >= 0)
345 sprintf (suffix, "[%d]", (int)length);
346 else
347 strcpy (suffix, "[]");
348 TYPE_NAME (t)
349 = TYPE_STUB_DECL (t)
350 = build_decl (TYPE_DECL,
351 identifier_subst (el_name, "", '.', '.', suffix),
352 t);
353 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (t)) = true;
354 }
355
356 set_java_signature (t, sig);
357 set_super_info (0, t, object_type_node, 0);
358 if (TREE_CODE (element_type) == RECORD_TYPE)
359 element_type = promote_type (element_type);
360 TYPE_ARRAY_ELEMENT (t) = element_type;
361
362 /* Add length pseudo-field. */
363 fld = build_decl (FIELD_DECL, get_identifier ("length"), int_type_node);
364 TYPE_FIELDS (t) = fld;
365 DECL_CONTEXT (fld) = t;
366 FIELD_PUBLIC (fld) = 1;
367 FIELD_FINAL (fld) = 1;
368 TREE_READONLY (fld) = 1;
369
370 atype = build_prim_array_type (element_type, length);
371 arfld = build_decl (FIELD_DECL, get_identifier ("data"), atype);
372 DECL_CONTEXT (arfld) = t;
373 TREE_CHAIN (fld) = arfld;
374 DECL_ALIGN (arfld) = TYPE_ALIGN (element_type);
375
376 /* We could layout_class, but that loads java.lang.Object prematurely.
377 * This is called by the parser, and it is a bad idea to do load_class
378 * in the middle of parsing, because of possible circularity problems. */
379 push_super_field (t, object_type_node);
380 layout_type (t);
381
382 return t;
383 }
384
385 /* Promote TYPE to the type actually used for fields and parameters. */
386
387 tree
388 promote_type (tree type)
389 {
390 switch (TREE_CODE (type))
391 {
392 case RECORD_TYPE:
393 return build_pointer_type (type);
394 case BOOLEAN_TYPE:
395 if (type == boolean_type_node)
396 return promoted_boolean_type_node;
397 goto handle_int;
398 case INTEGER_TYPE:
399 if (type == char_type_node)
400 return promoted_char_type_node;
401 handle_int:
402 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
403 {
404 if (type == short_type_node)
405 return promoted_short_type_node;
406 if (type == byte_type_node)
407 return promoted_byte_type_node;
408 return int_type_node;
409 }
410 /* ... else fall through ... */
411 default:
412 return type;
413 }
414 }
415
416 /* Parse a signature string, starting at *PTR and ending at LIMIT.
417 Return the seen TREE_TYPE, updating *PTR. */
418
419 static tree
420 parse_signature_type (const unsigned char **ptr, const unsigned char *limit)
421 {
422 tree type;
423 gcc_assert (*ptr < limit);
424
425 switch (**ptr)
426 {
427 case 'B': (*ptr)++; return byte_type_node;
428 case 'C': (*ptr)++; return char_type_node;
429 case 'D': (*ptr)++; return double_type_node;
430 case 'F': (*ptr)++; return float_type_node;
431 case 'S': (*ptr)++; return short_type_node;
432 case 'I': (*ptr)++; return int_type_node;
433 case 'J': (*ptr)++; return long_type_node;
434 case 'Z': (*ptr)++; return boolean_type_node;
435 case 'V': (*ptr)++; return void_type_node;
436 case '[':
437 for ((*ptr)++; (*ptr) < limit && ISDIGIT (**ptr); ) (*ptr)++;
438 type = parse_signature_type (ptr, limit);
439 type = build_java_array_type (type, -1);
440 break;
441 case 'L':
442 {
443 const unsigned char *start = ++(*ptr);
444 const unsigned char *str = start;
445 for ( ; ; str++)
446 {
447 gcc_assert (str < limit);
448 if (*str == ';')
449 break;
450 }
451 *ptr = str+1;
452 type = lookup_class (unmangle_classname ((const char *) start, str - start));
453 break;
454 }
455 default:
456 gcc_unreachable ();
457 }
458 return promote_type (type);
459 }
460
461 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
462 and SIG_LENGTH bytes long.
463 Return a gcc type node. */
464
465 tree
466 parse_signature_string (const unsigned char *sig_string, int sig_length)
467 {
468 tree result_type;
469 const unsigned char *str = sig_string;
470 const unsigned char *limit = str + sig_length;
471
472 if (str < limit && str[0] == '(')
473 {
474 tree argtype_list = NULL_TREE;
475 str++;
476 while (str < limit && str[0] != ')')
477 {
478 tree argtype = parse_signature_type (&str, limit);
479 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
480 }
481 if (str++, str >= limit)
482 abort ();
483 result_type = parse_signature_type (&str, limit);
484 argtype_list = chainon (nreverse (argtype_list), end_params_node);
485 result_type = build_function_type (result_type, argtype_list);
486 }
487 else
488 result_type = parse_signature_type (&str, limit);
489 if (str != limit)
490 error ("junk at end of signature string");
491 return result_type;
492 }
493
494 /* Convert a signature to its type.
495 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
496 */
497
498 tree
499 get_type_from_signature (tree signature)
500 {
501 const unsigned char *sig = (const unsigned char *) IDENTIFIER_POINTER (signature);
502 int len = IDENTIFIER_LENGTH (signature);
503 tree type;
504 /* Primitive types aren't cached. */
505 if (len <= 1)
506 return parse_signature_string (sig, len);
507 type = IDENTIFIER_SIGNATURE_TYPE (signature);
508 if (type == NULL_TREE)
509 {
510 type = parse_signature_string (sig, len);
511 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
512 }
513 return type;
514 }
515
516 /* Ignore signature and always return null. Used by has_method. */
517
518 static tree
519 build_null_signature (tree type ATTRIBUTE_UNUSED)
520 {
521 return NULL_TREE;
522 }
523
524 /* Return the signature string for the arguments of method type TYPE. */
525
526 tree
527 build_java_argument_signature (tree type)
528 {
529 extern struct obstack temporary_obstack;
530 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
531 if (sig == NULL_TREE)
532 {
533 tree args = TYPE_ARG_TYPES (type);
534 if (TREE_CODE (type) == METHOD_TYPE)
535 args = TREE_CHAIN (args); /* Skip "this" argument. */
536 for (; args != end_params_node; args = TREE_CHAIN (args))
537 {
538 tree t = build_java_signature (TREE_VALUE (args));
539 obstack_grow (&temporary_obstack,
540 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
541 }
542 obstack_1grow (&temporary_obstack, '\0');
543
544 sig = get_identifier (obstack_base (&temporary_obstack));
545 TYPE_ARGUMENT_SIGNATURE (type) = sig;
546 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
547 }
548 return sig;
549 }
550
551 /* Return the signature of the given TYPE. */
552
553 tree
554 build_java_signature (tree type)
555 {
556 tree sig, t;
557 while (TREE_CODE (type) == POINTER_TYPE)
558 type = TREE_TYPE (type);
559 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
560 sig = TYPE_SIGNATURE (type);
561 if (sig == NULL_TREE)
562 {
563 char sg[2];
564 switch (TREE_CODE (type))
565 {
566 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
567 case VOID_TYPE: sg[0] = 'V'; goto native;
568 case INTEGER_TYPE:
569 if (type == char_type_node || type == promoted_char_type_node)
570 {
571 sg[0] = 'C';
572 goto native;
573 }
574 switch (TYPE_PRECISION (type))
575 {
576 case 8: sg[0] = 'B'; goto native;
577 case 16: sg[0] = 'S'; goto native;
578 case 32: sg[0] = 'I'; goto native;
579 case 64: sg[0] = 'J'; goto native;
580 default: goto bad_type;
581 }
582 case REAL_TYPE:
583 switch (TYPE_PRECISION (type))
584 {
585 case 32: sg[0] = 'F'; goto native;
586 case 64: sg[0] = 'D'; goto native;
587 default: goto bad_type;
588 }
589 native:
590 sg[1] = 0;
591 sig = get_identifier (sg);
592 break;
593 case RECORD_TYPE:
594 if (TYPE_ARRAY_P (type))
595 {
596 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
597 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
598 "[", 0, 0, "");
599 }
600 else
601 {
602 t = DECL_NAME (TYPE_NAME (type));
603 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
604 "L", '.', '/', ";");
605 }
606 break;
607 case METHOD_TYPE:
608 case FUNCTION_TYPE:
609 {
610 extern struct obstack temporary_obstack;
611 sig = build_java_argument_signature (type);
612 obstack_1grow (&temporary_obstack, '(');
613 obstack_grow (&temporary_obstack,
614 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
615 obstack_1grow (&temporary_obstack, ')');
616
617 t = build_java_signature (TREE_TYPE (type));
618 obstack_grow0 (&temporary_obstack,
619 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
620
621 sig = get_identifier (obstack_base (&temporary_obstack));
622 obstack_free (&temporary_obstack,
623 obstack_base (&temporary_obstack));
624 }
625 break;
626 bad_type:
627 default:
628 gcc_unreachable ();
629 }
630 TYPE_SIGNATURE (type) = sig;
631 }
632 return sig;
633 }
634
635 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
636
637 void
638 set_java_signature (tree type, tree sig)
639 {
640 tree old_sig;
641 while (TREE_CODE (type) == POINTER_TYPE)
642 type = TREE_TYPE (type);
643 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
644 old_sig = TYPE_SIGNATURE (type);
645 if (old_sig != NULL_TREE && old_sig != sig)
646 abort ();
647 TYPE_SIGNATURE (type) = sig;
648 #if 0 /* careful about METHOD_TYPE */
649 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
650 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
651 #endif
652 }
653
654 /* Search in SEARCHED_CLASS and its superclasses for a method matching
655 METHOD_NAME and signature METHOD_SIGNATURE. This function will
656 only search for methods declared in the class hierarchy; interfaces
657 will not be considered. Returns NULL_TREE if the method is not
658 found. */
659 tree
660 lookup_argument_method (tree searched_class, tree method_name,
661 tree method_signature)
662 {
663 return lookup_do (searched_class, 0,
664 method_name, method_signature,
665 build_java_argument_signature);
666 }
667
668 /* Like lookup_argument_method, but lets the caller set any flags
669 desired. */
670 tree
671 lookup_argument_method_generic (tree searched_class, tree method_name,
672 tree method_signature, int flags)
673 {
674 return lookup_do (searched_class, flags,
675 method_name, method_signature,
676 build_java_argument_signature);
677 }
678
679
680 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
681 matching METHOD_NAME and signature METHOD_SIGNATURE. Return a
682 FUNCTION_DECL on success, or NULL_TREE if none found. (Contrast
683 lookup_argument_method, which ignores return type.) If
684 SEARCHED_CLASS is an interface, search it too. */
685 tree
686 lookup_java_method (tree searched_class, tree method_name,
687 tree method_signature)
688 {
689 return lookup_do (searched_class, SEARCH_INTERFACE, method_name,
690 method_signature, build_java_signature);
691 }
692
693 /* Return true iff KLASS (or its ancestors) has a method METHOD_NAME.  */
694 int
695 has_method (tree klass, tree method_name)
696 {
697 return lookup_do (klass, SEARCH_INTERFACE,
698 method_name, NULL_TREE,
699 build_null_signature) != NULL_TREE;
700 }
701
702 /* Search in class SEARCHED_CLASS, but not its superclasses, for a
703 method matching METHOD_NAME and signature SIGNATURE. A private
704 helper for lookup_do. */
705 static tree
706 shallow_find_method (tree searched_class, int flags, tree method_name,
707 tree signature, tree (*signature_builder) (tree))
708 {
709 tree method;
710 for (method = TYPE_METHODS (searched_class);
711 method != NULL_TREE; method = TREE_CHAIN (method))
712 {
713 tree method_sig = (*signature_builder) (TREE_TYPE (method));
714 if (DECL_NAME (method) == method_name && method_sig == signature)
715 {
716 /* If the caller requires a visible method, then we
717 skip invisible methods here. */
718 if (! (flags & SEARCH_VISIBLE)
719 || ! METHOD_INVISIBLE (method))
720 return method;
721 }
722 }
723 return NULL_TREE;
724 }
725
726 /* Search in the superclasses of SEARCHED_CLASS for a method matching
727 METHOD_NAME and signature SIGNATURE. A private helper for
728 lookup_do. */
729 static tree
730 find_method_in_superclasses (tree searched_class, int flags,
731 tree method_name, tree signature,
732 tree (*signature_builder) (tree))
733 {
734 tree klass;
735 for (klass = CLASSTYPE_SUPER (searched_class); klass != NULL_TREE;
736 klass = CLASSTYPE_SUPER (klass))
737 {
738 tree method;
739 method = shallow_find_method (klass, flags, method_name,
740 signature, signature_builder);
741 if (method != NULL_TREE)
742 return method;
743 }
744
745 return NULL_TREE;
746 }
747
748 /* Search in the interfaces of SEARCHED_CLASS and its superinterfaces
749 for a method matching METHOD_NAME and signature SIGNATURE. A
750 private helper for lookup_do. */
751 static tree
752 find_method_in_interfaces (tree searched_class, int flags, tree method_name,
753 tree signature, tree (*signature_builder) (tree))
754 {
755 int i;
756 tree binfo, base_binfo;
757
758 for (binfo = TYPE_BINFO (searched_class), i = 1;
759 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
760 {
761 tree iclass = BINFO_TYPE (base_binfo);
762 tree method;
763
764 /* If the superinterface hasn't been loaded yet, do so now. */
765 if (!CLASS_LOADED_P (iclass))
766 load_class (iclass, 1);
767
768 /* First, we look in ICLASS. If that doesn't work we'll
769 recursively look through all its superinterfaces. */
770 method = shallow_find_method (iclass, flags, method_name,
771 signature, signature_builder);
772 if (method != NULL_TREE)
773 return method;
774
775 method = find_method_in_interfaces
776 (iclass, flags, method_name, signature, signature_builder);
777 if (method != NULL_TREE)
778 return method;
779 }
780
781 return NULL_TREE;
782 }
783
784
785 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
786 matching METHOD_NAME and signature SIGNATURE. FLAGS control some
787 parameters of the search.
788
789 SEARCH_INTERFACE means also search interfaces and superinterfaces
790 of SEARCHED_CLASS.
791
792 SEARCH_SUPER means skip SEARCHED_CLASS and start with its
793 superclass.
794
795 SEARCH_VISIBLE means skip methods for which METHOD_INVISIBLE is
796 set.
797
798 Return the matched method DECL or NULL_TREE. SIGNATURE_BUILDER is
799 used on method candidates to build their (sometimes partial)
800 signature. */
801 static tree
802 lookup_do (tree searched_class, int flags, tree method_name,
803 tree signature, tree (*signature_builder) (tree))
804 {
805 tree method;
806 tree orig_class = searched_class;
807
808 if (searched_class == NULL_TREE)
809 return NULL_TREE;
810
811 if (flags & SEARCH_SUPER)
812 {
813 searched_class = CLASSTYPE_SUPER (searched_class);
814 if (searched_class == NULL_TREE)
815 return NULL_TREE;
816 }
817
818 /* First look in our own methods. */
819 method = shallow_find_method (searched_class, flags, method_name,
820 signature, signature_builder);
821 if (method)
822 return method;
823
824 /* Then look in our superclasses. */
825 if (! CLASS_INTERFACE (TYPE_NAME (searched_class)))
826 method = find_method_in_superclasses (searched_class, flags, method_name,
827 signature, signature_builder);
828 if (method)
829 return method;
830
831 /* If that doesn't work, look in our interfaces. */
832 if (flags & SEARCH_INTERFACE)
833 method = find_method_in_interfaces (orig_class, flags, method_name,
834 signature, signature_builder);
835
836 return method;
837 }
838
839 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
840 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
841
842 tree
843 lookup_java_constructor (tree clas, tree method_signature)
844 {
845 tree method = TYPE_METHODS (clas);
846 for ( ; method != NULL_TREE; method = TREE_CHAIN (method))
847 {
848 tree method_sig = build_java_signature (TREE_TYPE (method));
849 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
850 return method;
851 }
852 return NULL_TREE;
853 }
854
855 /* Return a type which is the Binary Numeric Promotion of the pair T1,
856 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
857 Promotion. It assumes that both T1 and T2 are eligible to BNP. */
858
859 tree
860 binary_numeric_promotion (tree t1, tree t2, tree *exp1, tree *exp2)
861 {
862 if (t1 == double_type_node || t2 == double_type_node)
863 {
864 if (t1 != double_type_node)
865 *exp1 = convert (double_type_node, *exp1);
866 if (t2 != double_type_node)
867 *exp2 = convert (double_type_node, *exp2);
868 return double_type_node;
869 }
870 if (t1 == float_type_node || t2 == float_type_node)
871 {
872 if (t1 != float_type_node)
873 *exp1 = convert (float_type_node, *exp1);
874 if (t2 != float_type_node)
875 *exp2 = convert (float_type_node, *exp2);
876 return float_type_node;
877 }
878 if (t1 == long_type_node || t2 == long_type_node)
879 {
880 if (t1 != long_type_node)
881 *exp1 = convert (long_type_node, *exp1);
882 if (t2 != long_type_node)
883 *exp2 = convert (long_type_node, *exp2);
884 return long_type_node;
885 }
886
887 if (t1 != int_type_node)
888 *exp1 = convert (int_type_node, *exp1);
889 if (t2 != int_type_node)
890 *exp2 = convert (int_type_node, *exp2);
891 return int_type_node;
892 }