* java/lang/natRuntime.cc (insertSystemProperties): Set
[official-gcc.git] / gcc / java / typeck.c
bloba15b1d7abb2144677207c23f7cefdfa386bca0cf
1 /* Handle types for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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 2, or (at your option)
10 any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "real.h"
34 #include "obstack.h"
35 #include "flags.h"
36 #include "java-tree.h"
37 #include "jcf.h"
38 #include "convert.h"
39 #include "toplev.h"
40 #include "ggc.h"
42 static tree convert_ieee_real_to_integer (tree, tree);
43 static tree parse_signature_type (const unsigned char **,
44 const unsigned char *);
45 static tree lookup_do (tree, int, tree, tree, tree (*)(tree));
46 static tree build_null_signature (tree);
48 tree * type_map;
50 /* Set the type of the local variable with index SLOT to TYPE. */
52 void
53 set_local_type (int slot, tree type)
55 int max_locals = DECL_MAX_LOCALS(current_function_decl);
56 int nslots = TYPE_IS_WIDE (type) ? 2 : 1;
58 if (slot < 0 || slot + nslots - 1 >= max_locals)
59 abort ();
61 type_map[slot] = type;
62 while (--nslots > 0)
63 type_map[++slot] = void_type_node;
66 /* Convert an IEEE real to an integer type. The result of such a
67 conversion when the source operand is a NaN isn't defined by
68 IEEE754, but by the Java language standard: it must be zero. Also,
69 overflows must be clipped to within range. This conversion
70 produces something like:
72 ((expr >= (float)MAX_INT)
73 ? MAX_INT
74 : ((expr <= (float)MIN_INT)
75 ? MIN_INT
76 : ((expr != expr)
77 ? 0
78 : (int)expr))) */
80 static tree
81 convert_ieee_real_to_integer (tree type, tree expr)
83 tree result;
84 expr = save_expr (expr);
86 result = fold (build3 (COND_EXPR, type,
87 fold (build2 (NE_EXPR, boolean_type_node, expr, expr)),
88 convert (type, integer_zero_node),
89 convert_to_integer (type, expr)));
91 result = fold (build3 (COND_EXPR, type,
92 fold (build2 (LE_EXPR, boolean_type_node, expr,
93 convert (TREE_TYPE (expr),
94 TYPE_MIN_VALUE (type)))),
95 TYPE_MIN_VALUE (type),
96 result));
98 result = fold (build3 (COND_EXPR, type,
99 fold (build2 (GE_EXPR, boolean_type_node, expr,
100 convert (TREE_TYPE (expr),
101 TYPE_MAX_VALUE (type)))),
102 TYPE_MAX_VALUE (type),
103 result));
105 return result;
108 /* Create an expression whose value is that of EXPR,
109 converted to type TYPE. The TREE_TYPE of the value
110 is always TYPE. This function implements all reasonable
111 conversions; callers should filter out those that are
112 not permitted by the language being compiled. */
114 tree
115 convert (tree type, tree expr)
117 enum tree_code code = TREE_CODE (type);
119 if (!expr)
120 return error_mark_node;
122 if (type == TREE_TYPE (expr)
123 || TREE_CODE (expr) == ERROR_MARK)
124 return expr;
125 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
126 return error_mark_node;
127 if (code == VOID_TYPE)
128 return build1 (CONVERT_EXPR, type, expr);
129 if (code == BOOLEAN_TYPE || code == CHAR_TYPE)
130 return fold_convert (type, expr);
131 if (code == INTEGER_TYPE)
133 if ((really_constant_p (expr)
134 || (! flag_unsafe_math_optimizations
135 && ! flag_emit_class_files))
136 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
137 && TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
138 return fold (convert_ieee_real_to_integer (type, expr));
139 else
141 /* fold very helpfully sets the overflow status if a type
142 overflows in a narrowing integer conversion, but Java
143 doesn't care. */
144 tree tmp = fold (convert_to_integer (type, expr));
145 TREE_OVERFLOW (tmp) = 0;
146 return tmp;
149 if (code == REAL_TYPE)
150 return fold (convert_to_real (type, expr));
151 if (code == POINTER_TYPE)
152 return fold (convert_to_pointer (type, expr));
153 error ("conversion to non-scalar type requested");
154 return error_mark_node;
158 /* Return a data type that has machine mode MODE.
159 If the mode is an integer,
160 then UNSIGNEDP selects between signed and unsigned types. */
162 tree
163 java_type_for_mode (enum machine_mode mode, int unsignedp)
165 if (mode == TYPE_MODE (int_type_node))
166 return unsignedp ? unsigned_int_type_node : int_type_node;
167 if (mode == TYPE_MODE (long_type_node))
168 return unsignedp ? unsigned_long_type_node : long_type_node;
169 if (mode == TYPE_MODE (short_type_node))
170 return unsignedp ? unsigned_short_type_node : short_type_node;
171 if (mode == TYPE_MODE (byte_type_node))
172 return unsignedp ? unsigned_byte_type_node : byte_type_node;
173 if (mode == TYPE_MODE (float_type_node))
174 return float_type_node;
175 if (mode == TYPE_MODE (double_type_node))
176 return double_type_node;
178 return 0;
181 /* Return an integer type with BITS bits of precision,
182 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
184 tree
185 java_type_for_size (unsigned bits, int unsignedp)
187 if (bits <= TYPE_PRECISION (byte_type_node))
188 return unsignedp ? unsigned_byte_type_node : byte_type_node;
189 if (bits <= TYPE_PRECISION (short_type_node))
190 return unsignedp ? unsigned_short_type_node : short_type_node;
191 if (bits <= TYPE_PRECISION (int_type_node))
192 return unsignedp ? unsigned_int_type_node : int_type_node;
193 if (bits <= TYPE_PRECISION (long_type_node))
194 return unsignedp ? unsigned_long_type_node : long_type_node;
195 return 0;
198 /* Return a type the same as TYPE except unsigned or
199 signed according to UNSIGNEDP. */
201 tree
202 java_signed_or_unsigned_type (int unsignedp, tree type)
204 if (! INTEGRAL_TYPE_P (type))
205 return type;
206 if (TYPE_PRECISION (type) == TYPE_PRECISION (int_type_node))
207 return unsignedp ? unsigned_int_type_node : int_type_node;
208 if (TYPE_PRECISION (type) == TYPE_PRECISION (byte_type_node))
209 return unsignedp ? unsigned_byte_type_node : byte_type_node;
210 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_type_node))
211 return unsignedp ? unsigned_short_type_node : short_type_node;
212 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_type_node))
213 return unsignedp ? unsigned_long_type_node : long_type_node;
214 return type;
217 /* Return a signed type the same as TYPE in other respects. */
219 tree
220 java_signed_type (tree type)
222 return java_signed_or_unsigned_type (0, type);
225 /* Return an unsigned type the same as TYPE in other respects. */
227 tree
228 java_unsigned_type (tree type)
230 return java_signed_or_unsigned_type (1, type);
233 /* Mark EXP saying that we need to be able to take the
234 address of it; it should not be allocated in a register.
235 Value is true if successful. */
237 bool
238 java_mark_addressable (tree exp)
240 tree x = exp;
241 while (1)
242 switch (TREE_CODE (x))
244 case ADDR_EXPR:
245 case COMPONENT_REF:
246 case ARRAY_REF:
247 case REALPART_EXPR:
248 case IMAGPART_EXPR:
249 x = TREE_OPERAND (x, 0);
250 break;
252 case TRUTH_ANDIF_EXPR:
253 case TRUTH_ORIF_EXPR:
254 case COMPOUND_EXPR:
255 x = TREE_OPERAND (x, 1);
256 break;
258 case COND_EXPR:
259 return java_mark_addressable (TREE_OPERAND (x, 1))
260 && java_mark_addressable (TREE_OPERAND (x, 2));
262 case CONSTRUCTOR:
263 TREE_ADDRESSABLE (x) = 1;
264 return true;
266 case INDIRECT_REF:
267 /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
268 incompatibility problems. Handle this case by marking FOO. */
269 if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
270 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
272 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
273 break;
275 if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
277 x = TREE_OPERAND (x, 0);
278 break;
280 return true;
282 case VAR_DECL:
283 case CONST_DECL:
284 case PARM_DECL:
285 case RESULT_DECL:
286 case FUNCTION_DECL:
287 TREE_ADDRESSABLE (x) = 1;
288 #if 0 /* poplevel deals with this now. */
289 if (DECL_CONTEXT (x) == 0)
290 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
291 #endif
292 /* drops through */
293 default:
294 return true;
298 /* Thorough checking of the arrayness of TYPE. */
301 is_array_type_p (tree type)
303 return TREE_CODE (type) == POINTER_TYPE
304 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
305 && TYPE_ARRAY_P (TREE_TYPE (type));
308 /* Return the length of a Java array type.
309 Return -1 if the length is unknown or non-constant. */
311 HOST_WIDE_INT
312 java_array_type_length (tree array_type)
314 tree arfld;
315 if (TREE_CODE (array_type) == POINTER_TYPE)
316 array_type = TREE_TYPE (array_type);
317 arfld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
318 if (arfld != NULL_TREE)
320 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
321 if (index_type != NULL_TREE)
323 tree high = TYPE_MAX_VALUE (index_type);
324 if (TREE_CODE (high) == INTEGER_CST)
325 return TREE_INT_CST_LOW (high) + 1;
328 return -1;
331 /* An array of unknown length will be ultimately given an length of
332 -2, so that we can still have `length' producing a negative value
333 even if found. This was part of an optimization aiming at removing
334 `length' from static arrays. We could restore it, FIXME. */
336 tree
337 build_prim_array_type (tree element_type, HOST_WIDE_INT length)
339 tree index = NULL;
341 if (length != -1)
343 tree max_index = build_int_cst (sizetype, length - 1);
344 index = build_index_type (max_index);
346 return build_array_type (element_type, index);
349 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
350 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
351 The LENGTH is -1 if the length is unknown. */
353 tree
354 build_java_array_type (tree element_type, HOST_WIDE_INT length)
356 tree sig, t, fld, atype, arfld;
357 char buf[12];
358 tree elsig = build_java_signature (element_type);
359 tree el_name = element_type;
360 buf[0] = '[';
361 if (length >= 0)
362 sprintf (buf+1, HOST_WIDE_INT_PRINT_DEC, length);
363 else
364 buf[1] = '\0';
365 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
366 buf, 0, 0, "");
367 t = IDENTIFIER_SIGNATURE_TYPE (sig);
368 if (t != NULL_TREE)
369 return TREE_TYPE (t);
370 t = make_class ();
371 IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
372 TYPE_ARRAY_P (t) = 1;
374 if (TREE_CODE (el_name) == POINTER_TYPE)
375 el_name = TREE_TYPE (el_name);
376 el_name = TYPE_NAME (el_name);
377 if (TREE_CODE (el_name) == TYPE_DECL)
378 el_name = DECL_NAME (el_name);
380 char suffix[12];
381 if (length >= 0)
382 sprintf (suffix, "[%d]", (int)length);
383 else
384 strcpy (suffix, "[]");
385 TYPE_NAME (t)
386 = build_decl (TYPE_DECL,
387 identifier_subst (el_name, "", '.', '.', suffix),
391 set_java_signature (t, sig);
392 set_super_info (0, t, object_type_node, 0);
393 if (TREE_CODE (element_type) == RECORD_TYPE)
394 element_type = promote_type (element_type);
395 TYPE_ARRAY_ELEMENT (t) = element_type;
397 /* Add length pseudo-field. */
398 fld = build_decl (FIELD_DECL, get_identifier ("length"), int_type_node);
399 TYPE_FIELDS (t) = fld;
400 DECL_CONTEXT (fld) = t;
401 FIELD_PUBLIC (fld) = 1;
402 FIELD_FINAL (fld) = 1;
403 TREE_READONLY (fld) = 1;
405 atype = build_prim_array_type (element_type, length);
406 arfld = build_decl (FIELD_DECL, get_identifier ("data"), atype);
407 DECL_CONTEXT (arfld) = t;
408 TREE_CHAIN (fld) = arfld;
409 DECL_ALIGN (arfld) = TYPE_ALIGN (element_type);
411 /* We could layout_class, but that loads java.lang.Object prematurely.
412 * This is called by the parser, and it is a bad idea to do load_class
413 * in the middle of parsing, because of possible circularity problems. */
414 push_super_field (t, object_type_node);
415 layout_type (t);
417 return t;
420 /* Promote TYPE to the type actually used for fields and parameters. */
422 tree
423 promote_type (tree type)
425 switch (TREE_CODE (type))
427 case RECORD_TYPE:
428 return build_pointer_type (type);
429 case BOOLEAN_TYPE:
430 if (type == boolean_type_node)
431 return promoted_boolean_type_node;
432 goto handle_int;
433 case CHAR_TYPE:
434 if (type == char_type_node)
435 return promoted_char_type_node;
436 goto handle_int;
437 case INTEGER_TYPE:
438 handle_int:
439 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
441 if (type == short_type_node)
442 return promoted_short_type_node;
443 if (type == byte_type_node)
444 return promoted_byte_type_node;
445 return int_type_node;
447 /* ... else fall through ... */
448 default:
449 return type;
453 /* Parse a signature string, starting at *PTR and ending at LIMIT.
454 Return the seen TREE_TYPE, updating *PTR. */
456 static tree
457 parse_signature_type (const unsigned char **ptr, const unsigned char *limit)
459 tree type;
461 if (*ptr >= limit)
462 abort ();
464 switch (**ptr)
466 case 'B': (*ptr)++; return byte_type_node;
467 case 'C': (*ptr)++; return char_type_node;
468 case 'D': (*ptr)++; return double_type_node;
469 case 'F': (*ptr)++; return float_type_node;
470 case 'S': (*ptr)++; return short_type_node;
471 case 'I': (*ptr)++; return int_type_node;
472 case 'J': (*ptr)++; return long_type_node;
473 case 'Z': (*ptr)++; return boolean_type_node;
474 case 'V': (*ptr)++; return void_type_node;
475 case '[':
476 for ((*ptr)++; (*ptr) < limit && ISDIGIT (**ptr); ) (*ptr)++;
477 type = parse_signature_type (ptr, limit);
478 type = build_java_array_type (type, -1);
479 break;
480 case 'L':
482 const unsigned char *start = ++(*ptr);
483 const unsigned char *str = start;
484 for ( ; ; str++)
486 if (str >= limit)
487 abort ();
488 if (*str == ';')
489 break;
491 *ptr = str+1;
492 type = lookup_class (unmangle_classname ((const char *) start, str - start));
493 break;
495 default:
496 abort ();
498 return promote_type (type);
501 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
502 and SIG_LENGTH bytes long.
503 Return a gcc type node. */
505 tree
506 parse_signature_string (const unsigned char *sig_string, int sig_length)
508 tree result_type;
509 const unsigned char *str = sig_string;
510 const unsigned char *limit = str + sig_length;
512 if (str < limit && str[0] == '(')
514 tree argtype_list = NULL_TREE;
515 str++;
516 while (str < limit && str[0] != ')')
518 tree argtype = parse_signature_type (&str, limit);
519 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
521 if (str++, str >= limit)
522 abort ();
523 result_type = parse_signature_type (&str, limit);
524 argtype_list = chainon (nreverse (argtype_list), end_params_node);
525 result_type = build_function_type (result_type, argtype_list);
527 else
528 result_type = parse_signature_type (&str, limit);
529 if (str != limit)
530 error ("junk at end of signature string");
531 return result_type;
534 /* Convert a signature to its type.
535 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
538 tree
539 get_type_from_signature (tree signature)
541 const unsigned char *sig = (const unsigned char *) IDENTIFIER_POINTER (signature);
542 int len = IDENTIFIER_LENGTH (signature);
543 tree type;
544 /* Primitive types aren't cached. */
545 if (len <= 1)
546 return parse_signature_string (sig, len);
547 type = IDENTIFIER_SIGNATURE_TYPE (signature);
548 if (type == NULL_TREE)
550 type = parse_signature_string (sig, len);
551 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
553 return type;
556 /* Ignore signature and always return null. Used by has_method. */
558 static tree
559 build_null_signature (tree type ATTRIBUTE_UNUSED)
561 return NULL_TREE;
564 /* Return the signature string for the arguments of method type TYPE. */
566 tree
567 build_java_argument_signature (tree type)
569 extern struct obstack temporary_obstack;
570 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
571 if (sig == NULL_TREE)
573 tree args = TYPE_ARG_TYPES (type);
574 if (TREE_CODE (type) == METHOD_TYPE)
575 args = TREE_CHAIN (args); /* Skip "this" argument. */
576 for (; args != end_params_node; args = TREE_CHAIN (args))
578 tree t = build_java_signature (TREE_VALUE (args));
579 obstack_grow (&temporary_obstack,
580 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
582 obstack_1grow (&temporary_obstack, '\0');
584 sig = get_identifier (obstack_base (&temporary_obstack));
585 TYPE_ARGUMENT_SIGNATURE (type) = sig;
586 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
588 return sig;
591 /* Return the signature of the given TYPE. */
593 tree
594 build_java_signature (tree type)
596 tree sig, t;
597 while (TREE_CODE (type) == POINTER_TYPE)
598 type = TREE_TYPE (type);
599 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
600 sig = TYPE_SIGNATURE (type);
601 if (sig == NULL_TREE)
603 char sg[2];
604 switch (TREE_CODE (type))
606 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
607 case CHAR_TYPE: sg[0] = 'C'; goto native;
608 case VOID_TYPE: sg[0] = 'V'; goto native;
609 case INTEGER_TYPE:
610 switch (TYPE_PRECISION (type))
612 case 8: sg[0] = 'B'; goto native;
613 case 16: sg[0] = 'S'; goto native;
614 case 32: sg[0] = 'I'; goto native;
615 case 64: sg[0] = 'J'; goto native;
616 default: goto bad_type;
618 case REAL_TYPE:
619 switch (TYPE_PRECISION (type))
621 case 32: sg[0] = 'F'; goto native;
622 case 64: sg[0] = 'D'; goto native;
623 default: goto bad_type;
625 native:
626 sg[1] = 0;
627 sig = get_identifier (sg);
628 break;
629 case RECORD_TYPE:
630 if (TYPE_ARRAY_P (type))
632 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
633 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
634 "[", 0, 0, "");
636 else
638 t = DECL_NAME (TYPE_NAME (type));
639 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
640 "L", '.', '/', ";");
642 break;
643 case METHOD_TYPE:
644 case FUNCTION_TYPE:
646 extern struct obstack temporary_obstack;
647 sig = build_java_argument_signature (type);
648 obstack_1grow (&temporary_obstack, '(');
649 obstack_grow (&temporary_obstack,
650 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
651 obstack_1grow (&temporary_obstack, ')');
653 t = build_java_signature (TREE_TYPE (type));
654 obstack_grow0 (&temporary_obstack,
655 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
657 sig = get_identifier (obstack_base (&temporary_obstack));
658 obstack_free (&temporary_obstack,
659 obstack_base (&temporary_obstack));
661 break;
662 bad_type:
663 default:
664 abort ();
666 TYPE_SIGNATURE (type) = sig;
668 return sig;
671 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
673 void
674 set_java_signature (tree type, tree sig)
676 tree old_sig;
677 while (TREE_CODE (type) == POINTER_TYPE)
678 type = TREE_TYPE (type);
679 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
680 old_sig = TYPE_SIGNATURE (type);
681 if (old_sig != NULL_TREE && old_sig != sig)
682 abort ();
683 TYPE_SIGNATURE (type) = sig;
684 #if 0 /* careful about METHOD_TYPE */
685 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
686 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
687 #endif
690 /* Search in SEARCHED_CLASS and its superclasses for a method matching
691 METHOD_NAME and signature METHOD_SIGNATURE. This function will
692 only search for methods declared in the class hierarchy; interfaces
693 will not be considered. Returns NULL_TREE if the method is not
694 found. */
695 tree
696 lookup_argument_method (tree searched_class, tree method_name,
697 tree method_signature)
699 return lookup_do (searched_class, 0,
700 method_name, method_signature,
701 build_java_argument_signature);
704 /* Like lookup_argument_method, but lets the caller set any flags
705 desired. */
706 tree
707 lookup_argument_method_generic (tree searched_class, tree method_name,
708 tree method_signature, int flags)
710 return lookup_do (searched_class, flags,
711 method_name, method_signature,
712 build_java_argument_signature);
716 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
717 matching METHOD_NAME and signature METHOD_SIGNATURE. Return a
718 FUNCTION_DECL on success, or NULL_TREE if none found. (Contrast
719 lookup_argument_method, which ignores return type.) If
720 SEARCHED_CLASS is an interface, search it too. */
721 tree
722 lookup_java_method (tree searched_class, tree method_name,
723 tree method_signature)
725 return lookup_do (searched_class, SEARCH_INTERFACE, method_name,
726 method_signature, build_java_signature);
729 /* Return true iff CLASS (or its ancestors) has a method METHOD_NAME.  */
731 has_method (tree class, tree method_name)
733 return lookup_do (class, SEARCH_INTERFACE,
734 method_name, NULL_TREE,
735 build_null_signature) != NULL_TREE;
738 /* Search in class SEARCHED_CLASS, but not its superclasses, for a
739 method matching METHOD_NAME and signature SIGNATURE. A private
740 helper for lookup_do. */
741 static tree
742 shallow_find_method (tree searched_class, int flags, tree method_name,
743 tree signature, tree (*signature_builder) (tree))
745 tree method;
746 for (method = TYPE_METHODS (searched_class);
747 method != NULL_TREE; method = TREE_CHAIN (method))
749 tree method_sig = (*signature_builder) (TREE_TYPE (method));
750 if (DECL_NAME (method) == method_name && method_sig == signature)
752 /* If the caller requires a visible method, then we
753 skip invisible methods here. */
754 if (! (flags & SEARCH_VISIBLE)
755 || ! METHOD_INVISIBLE (method))
756 return method;
759 return NULL_TREE;
762 /* Search in the superclasses of SEARCHED_CLASS for a method matching
763 METHOD_NAME and signature SIGNATURE. A private helper for
764 lookup_do. */
765 static tree
766 find_method_in_superclasses (tree searched_class, int flags,
767 tree method_name, tree signature,
768 tree (*signature_builder) (tree))
770 tree klass;
771 for (klass = CLASSTYPE_SUPER (searched_class); klass != NULL_TREE;
772 klass = CLASSTYPE_SUPER (klass))
774 tree method;
775 method = shallow_find_method (klass, flags, method_name,
776 signature, signature_builder);
777 if (method != NULL_TREE)
778 return method;
781 return NULL_TREE;
784 /* Search in the interfaces of SEARCHED_CLASS and its superinterfaces
785 for a method matching METHOD_NAME and signature SIGNATURE. A
786 private helper for lookup_do. */
787 static tree
788 find_method_in_interfaces (tree searched_class, int flags, tree method_name,
789 tree signature, tree (*signature_builder) (tree))
791 int i;
792 tree binfo, base_binfo;
794 for (binfo = TYPE_BINFO (searched_class), i = 1;
795 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
797 tree iclass = BINFO_TYPE (base_binfo);
798 tree method;
800 /* If the superinterface hasn't been loaded yet, do so now. */
801 if (CLASS_FROM_SOURCE_P (iclass))
802 safe_layout_class (iclass);
803 else if (!CLASS_LOADED_P (iclass))
804 load_class (iclass, 1);
806 /* First, we look in ICLASS. If that doesn't work we'll
807 recursively look through all its superinterfaces. */
808 method = shallow_find_method (iclass, flags, method_name,
809 signature, signature_builder);
810 if (method != NULL_TREE)
811 return method;
813 method = find_method_in_interfaces
814 (iclass, flags, method_name, signature, signature_builder);
815 if (method != NULL_TREE)
816 return method;
819 return NULL_TREE;
823 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
824 matching METHOD_NAME and signature SIGNATURE. FLAGS control some
825 parameters of the search.
827 SEARCH_INTERFACE means also search interfaces and superinterfaces
828 of SEARCHED_CLASS.
830 SEARCH_SUPER means skip SEARCHED_CLASS and start with its
831 superclass.
833 SEARCH_VISIBLE means skip methods for which METHOD_INVISIBLE is
834 set.
836 Return the matched method DECL or NULL_TREE. SIGNATURE_BUILDER is
837 used on method candidates to build their (sometimes partial)
838 signature. */
839 static tree
840 lookup_do (tree searched_class, int flags, tree method_name,
841 tree signature, tree (*signature_builder) (tree))
843 tree method;
845 if (searched_class == NULL_TREE)
846 return NULL_TREE;
848 if (flags & SEARCH_SUPER)
850 searched_class = CLASSTYPE_SUPER (searched_class);
851 if (searched_class == NULL_TREE)
852 return NULL_TREE;
855 /* First look in our own methods. */
856 method = shallow_find_method (searched_class, flags, method_name,
857 signature, signature_builder);
858 if (method)
859 return method;
861 /* Then look in our superclasses. */
862 if (! CLASS_INTERFACE (TYPE_NAME (searched_class)))
863 method = find_method_in_superclasses (searched_class, flags, method_name,
864 signature, signature_builder);
865 if (method)
866 return method;
868 /* If that doesn't work, look in our interfaces. */
869 if (flags & SEARCH_INTERFACE)
870 method = find_method_in_interfaces (searched_class, flags, method_name,
871 signature, signature_builder);
873 return method;
876 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
877 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
879 tree
880 lookup_java_constructor (tree clas, tree method_signature)
882 tree method = TYPE_METHODS (clas);
883 for ( ; method != NULL_TREE; method = TREE_CHAIN (method))
885 tree method_sig = build_java_signature (TREE_TYPE (method));
886 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
887 return method;
889 return NULL_TREE;
892 /* Return a type which is the Binary Numeric Promotion of the pair T1,
893 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
894 Promotion. It assumes that both T1 and T2 are eligible to BNP. */
896 tree
897 binary_numeric_promotion (tree t1, tree t2, tree *exp1, tree *exp2)
899 if (t1 == double_type_node || t2 == double_type_node)
901 if (t1 != double_type_node)
902 *exp1 = convert (double_type_node, *exp1);
903 if (t2 != double_type_node)
904 *exp2 = convert (double_type_node, *exp2);
905 return double_type_node;
907 if (t1 == float_type_node || t2 == float_type_node)
909 if (t1 != float_type_node)
910 *exp1 = convert (float_type_node, *exp1);
911 if (t2 != float_type_node)
912 *exp2 = convert (float_type_node, *exp2);
913 return float_type_node;
915 if (t1 == long_type_node || t2 == long_type_node)
917 if (t1 != long_type_node)
918 *exp1 = convert (long_type_node, *exp1);
919 if (t2 != long_type_node)
920 *exp2 = convert (long_type_node, *exp2);
921 return long_type_node;
924 if (t1 != int_type_node)
925 *exp1 = convert (int_type_node, *exp1);
926 if (t2 != int_type_node)
927 *exp2 = convert (int_type_node, *exp2);
928 return int_type_node;