2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / gcc / java / typeck.c
blob0da6380c4c24c893e652f29f057e11d3be2df556
1 /* Handle types for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Per Bothner <bothner@cygnus.com> */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "hash-set.h"
30 #include "machmode.h"
31 #include "vec.h"
32 #include "double-int.h"
33 #include "input.h"
34 #include "alias.h"
35 #include "symtab.h"
36 #include "options.h"
37 #include "wide-int.h"
38 #include "inchash.h"
39 #include "tree.h"
40 #include "fold-const.h"
41 #include "stor-layout.h"
42 #include "stringpool.h"
43 #include "obstack.h"
44 #include "flags.h"
45 #include "java-tree.h"
46 #include "jcf.h"
47 #include "convert.h"
48 #include "diagnostic-core.h"
49 #include "ggc.h"
51 static tree convert_ieee_real_to_integer (tree, tree);
52 static tree parse_signature_type (const unsigned char **,
53 const unsigned char *);
54 static tree lookup_do (tree, int, tree, tree, tree (*)(tree));
55 static tree build_null_signature (tree);
57 tree * type_map;
59 /* Set the type of the local variable with index SLOT to TYPE. */
61 void
62 set_local_type (int slot, tree type)
64 int max_locals = DECL_MAX_LOCALS(current_function_decl);
65 int nslots = TYPE_IS_WIDE (type) ? 2 : 1;
67 gcc_assert (slot >= 0 && (slot + nslots - 1 < max_locals));
69 type_map[slot] = type;
70 while (--nslots > 0)
71 type_map[++slot] = void_type_node;
74 /* Convert an IEEE real to an integer type. The result of such a
75 conversion when the source operand is a NaN isn't defined by
76 IEEE754, but by the Java language standard: it must be zero. Also,
77 overflows must be clipped to within range. This conversion
78 produces something like:
80 ((expr >= (float)MAX_INT)
81 ? MAX_INT
82 : ((expr <= (float)MIN_INT)
83 ? MIN_INT
84 : ((expr != expr)
85 ? 0
86 : (int)expr))) */
88 static tree
89 convert_ieee_real_to_integer (tree type, tree expr)
91 tree result;
92 expr = save_expr (expr);
94 result = fold_build3 (COND_EXPR, type,
95 fold_build2 (NE_EXPR, boolean_type_node, expr, expr),
96 convert (type, integer_zero_node),
97 convert_to_integer (type, expr));
99 result = fold_build3 (COND_EXPR, type,
100 fold_build2 (LE_EXPR, boolean_type_node, expr,
101 convert (TREE_TYPE (expr),
102 TYPE_MIN_VALUE (type))),
103 TYPE_MIN_VALUE (type),
104 result);
106 result = fold_build3 (COND_EXPR, type,
107 fold_build2 (GE_EXPR, boolean_type_node, expr,
108 convert (TREE_TYPE (expr),
109 TYPE_MAX_VALUE (type))),
110 TYPE_MAX_VALUE (type),
111 result);
113 return result;
116 /* Create an expression whose value is that of EXPR,
117 converted to type TYPE. The TREE_TYPE of the value
118 is always TYPE. This function implements all reasonable
119 conversions; callers should filter out those that are
120 not permitted by the language being compiled. */
122 tree
123 convert (tree type, tree expr)
125 enum tree_code code = TREE_CODE (type);
127 if (!expr)
128 return error_mark_node;
130 if (type == TREE_TYPE (expr)
131 || TREE_CODE (expr) == ERROR_MARK)
132 return expr;
133 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
134 return error_mark_node;
135 if (code == VOID_TYPE)
136 return build1 (CONVERT_EXPR, type, expr);
137 if (code == BOOLEAN_TYPE)
138 return fold_convert (type, expr);
139 if (code == INTEGER_TYPE)
141 if (type == char_type_node || type == promoted_char_type_node)
142 return fold_convert (type, expr);
143 if ((really_constant_p (expr) || ! flag_unsafe_math_optimizations)
144 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
145 return convert_ieee_real_to_integer (type, expr);
146 else
148 /* fold very helpfully sets the overflow status if a type
149 overflows in a narrowing integer conversion, but Java
150 doesn't care. */
151 tree tmp = fold (convert_to_integer (type, expr));
152 if (TREE_CODE (tmp) == INTEGER_CST)
153 TREE_OVERFLOW (tmp) = 0;
154 return tmp;
157 if (code == REAL_TYPE)
158 return fold (convert_to_real (type, expr));
159 if (code == POINTER_TYPE)
160 return fold (convert_to_pointer (type, expr));
161 error ("conversion to non-scalar type requested");
162 return error_mark_node;
166 /* Return a data type that has machine mode MODE.
167 If the mode is an integer,
168 then UNSIGNEDP selects between signed and unsigned types. */
170 tree
171 java_type_for_mode (machine_mode mode, int unsignedp)
173 if (mode == TYPE_MODE (int_type_node))
174 return unsignedp ? unsigned_int_type_node : int_type_node;
175 if (mode == TYPE_MODE (long_type_node))
176 return unsignedp ? unsigned_long_type_node : long_type_node;
177 if (mode == TYPE_MODE (short_type_node))
178 return unsignedp ? unsigned_short_type_node : short_type_node;
179 if (mode == TYPE_MODE (byte_type_node))
180 return unsignedp ? unsigned_byte_type_node : byte_type_node;
181 if (mode == TYPE_MODE (float_type_node))
182 return float_type_node;
183 if (mode == TYPE_MODE (double_type_node))
184 return double_type_node;
186 return 0;
189 /* Return an integer type with BITS bits of precision,
190 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
192 tree
193 java_type_for_size (unsigned bits, int unsignedp)
195 if (bits <= TYPE_PRECISION (byte_type_node))
196 return unsignedp ? unsigned_byte_type_node : byte_type_node;
197 if (bits <= TYPE_PRECISION (short_type_node))
198 return unsignedp ? unsigned_short_type_node : short_type_node;
199 if (bits <= TYPE_PRECISION (int_type_node))
200 return unsignedp ? unsigned_int_type_node : int_type_node;
201 if (bits <= TYPE_PRECISION (long_type_node))
202 return unsignedp ? unsigned_long_type_node : long_type_node;
203 return 0;
206 /* Thorough checking of the arrayness of TYPE. */
209 is_array_type_p (tree type)
211 return TREE_CODE (type) == POINTER_TYPE
212 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
213 && TYPE_ARRAY_P (TREE_TYPE (type));
216 /* Return the length of a Java array type.
217 Return -1 if the length is unknown or non-constant. */
219 HOST_WIDE_INT
220 java_array_type_length (tree array_type)
222 tree arfld;
223 if (TREE_CODE (array_type) == POINTER_TYPE)
224 array_type = TREE_TYPE (array_type);
225 arfld = DECL_CHAIN (DECL_CHAIN (TYPE_FIELDS (array_type)));
226 if (arfld != NULL_TREE)
228 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
229 if (index_type != NULL_TREE)
231 tree high = TYPE_MAX_VALUE (index_type);
232 if (TREE_CODE (high) == INTEGER_CST)
233 return TREE_INT_CST_LOW (high) + 1;
236 return -1;
239 /* An array of unknown length will be ultimately given a length of
240 -2, so that we can still have `length' producing a negative value
241 even if found. This was part of an optimization aiming at removing
242 `length' from static arrays. We could restore it, FIXME. */
244 tree
245 build_prim_array_type (tree element_type, HOST_WIDE_INT length)
247 tree index = NULL;
249 if (length != -1)
251 tree max_index = build_int_cst (sizetype, length - 1);
252 index = build_index_type (max_index);
254 return build_array_type (element_type, index);
257 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
258 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
259 The LENGTH is -1 if the length is unknown. */
261 tree
262 build_java_array_type (tree element_type, HOST_WIDE_INT length)
264 tree sig, t, fld, atype, arfld;
265 char buf[23];
266 tree elsig = build_java_signature (element_type);
267 tree el_name = element_type;
268 buf[0] = '[';
269 if (length >= 0)
270 sprintf (buf+1, HOST_WIDE_INT_PRINT_DEC, length);
271 else
272 buf[1] = '\0';
273 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
274 buf, 0, 0, "");
275 t = IDENTIFIER_SIGNATURE_TYPE (sig);
276 if (t != NULL_TREE)
277 return TREE_TYPE (t);
278 t = make_class ();
279 IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
280 TYPE_ARRAY_P (t) = 1;
282 if (TREE_CODE (el_name) == POINTER_TYPE)
283 el_name = TREE_TYPE (el_name);
284 el_name = TYPE_NAME (el_name);
285 if (TREE_CODE (el_name) == TYPE_DECL)
286 el_name = DECL_NAME (el_name);
288 char suffix[23];
289 if (length >= 0)
290 sprintf (suffix, "[%d]", (int)length);
291 else
292 strcpy (suffix, "[]");
293 TYPE_NAME (t)
294 = TYPE_STUB_DECL (t)
295 = build_decl (input_location, TYPE_DECL,
296 identifier_subst (el_name, "", '.', '.', suffix),
298 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (t)) = true;
301 set_java_signature (t, sig);
302 set_super_info (0, t, object_type_node, 0);
303 if (TREE_CODE (element_type) == RECORD_TYPE)
304 element_type = promote_type (element_type);
305 TYPE_ARRAY_ELEMENT (t) = element_type;
307 /* Add length pseudo-field. */
308 fld = build_decl (input_location,
309 FIELD_DECL, get_identifier ("length"), int_type_node);
310 TYPE_FIELDS (t) = fld;
311 DECL_CONTEXT (fld) = t;
312 FIELD_PUBLIC (fld) = 1;
313 FIELD_FINAL (fld) = 1;
314 TREE_READONLY (fld) = 1;
316 atype = build_prim_array_type (element_type, length);
317 arfld = build_decl (input_location,
318 FIELD_DECL, get_identifier ("data"), atype);
319 DECL_CONTEXT (arfld) = t;
320 DECL_CHAIN (fld) = arfld;
321 DECL_ALIGN (arfld) = TYPE_ALIGN (element_type);
323 /* We could layout_class, but that loads java.lang.Object prematurely.
324 * This is called by the parser, and it is a bad idea to do load_class
325 * in the middle of parsing, because of possible circularity problems. */
326 push_super_field (t, object_type_node);
327 layout_type (t);
329 return t;
332 /* Promote TYPE to the type actually used for fields and parameters. */
334 tree
335 promote_type (tree type)
337 switch (TREE_CODE (type))
339 case RECORD_TYPE:
340 return build_pointer_type (type);
341 case BOOLEAN_TYPE:
342 if (type == boolean_type_node)
343 return promoted_boolean_type_node;
344 goto handle_int;
345 case INTEGER_TYPE:
346 if (type == char_type_node)
347 return promoted_char_type_node;
348 handle_int:
349 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
351 if (type == short_type_node)
352 return promoted_short_type_node;
353 if (type == byte_type_node)
354 return promoted_byte_type_node;
355 return int_type_node;
357 /* ... else fall through ... */
358 default:
359 return type;
363 /* Parse a signature string, starting at *PTR and ending at LIMIT.
364 Return the seen TREE_TYPE, updating *PTR. */
366 static tree
367 parse_signature_type (const unsigned char **ptr, const unsigned char *limit)
369 tree type;
370 gcc_assert (*ptr < limit);
372 switch (**ptr)
374 case 'B': (*ptr)++; return byte_type_node;
375 case 'C': (*ptr)++; return char_type_node;
376 case 'D': (*ptr)++; return double_type_node;
377 case 'F': (*ptr)++; return float_type_node;
378 case 'S': (*ptr)++; return short_type_node;
379 case 'I': (*ptr)++; return int_type_node;
380 case 'J': (*ptr)++; return long_type_node;
381 case 'Z': (*ptr)++; return boolean_type_node;
382 case 'V': (*ptr)++; return void_type_node;
383 case '[':
384 for ((*ptr)++; (*ptr) < limit && ISDIGIT (**ptr); ) (*ptr)++;
385 type = parse_signature_type (ptr, limit);
386 type = build_java_array_type (type, -1);
387 break;
388 case 'L':
390 const unsigned char *start = ++(*ptr);
391 const unsigned char *str = start;
392 for ( ; ; str++)
394 gcc_assert (str < limit);
395 if (*str == ';')
396 break;
398 *ptr = str+1;
399 type = lookup_class (unmangle_classname ((const char *) start, str - start));
400 break;
402 default:
403 gcc_unreachable ();
405 return promote_type (type);
408 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
409 and SIG_LENGTH bytes long.
410 Return a gcc type node. */
412 tree
413 parse_signature_string (const unsigned char *sig_string, int sig_length)
415 tree result_type;
416 const unsigned char *str = sig_string;
417 const unsigned char *limit = str + sig_length;
419 if (str < limit && str[0] == '(')
421 tree argtype_list = NULL_TREE;
422 str++;
423 while (str < limit && str[0] != ')')
425 tree argtype = parse_signature_type (&str, limit);
426 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
428 if (str++, str >= limit)
429 abort ();
430 result_type = parse_signature_type (&str, limit);
431 argtype_list = chainon (nreverse (argtype_list), end_params_node);
432 result_type = build_function_type (result_type, argtype_list);
434 else
435 result_type = parse_signature_type (&str, limit);
436 if (str != limit)
437 error ("junk at end of signature string");
438 return result_type;
441 /* Convert a signature to its type.
442 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
445 tree
446 get_type_from_signature (tree signature)
448 const unsigned char *sig = (const unsigned char *) IDENTIFIER_POINTER (signature);
449 int len = IDENTIFIER_LENGTH (signature);
450 tree type;
451 /* Primitive types aren't cached. */
452 if (len <= 1)
453 return parse_signature_string (sig, len);
454 type = IDENTIFIER_SIGNATURE_TYPE (signature);
455 if (type == NULL_TREE)
457 type = parse_signature_string (sig, len);
458 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
460 return type;
463 /* Ignore signature and always return null. Used by has_method. */
465 static tree
466 build_null_signature (tree type ATTRIBUTE_UNUSED)
468 return NULL_TREE;
471 /* Return the signature string for the arguments of method type TYPE. */
473 tree
474 build_java_argument_signature (tree type)
476 extern struct obstack temporary_obstack;
477 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
478 if (sig == NULL_TREE)
480 tree args = TYPE_ARG_TYPES (type);
481 if (TREE_CODE (type) == METHOD_TYPE)
482 args = TREE_CHAIN (args); /* Skip "this" argument. */
483 for (; args != end_params_node; args = TREE_CHAIN (args))
485 tree t = build_java_signature (TREE_VALUE (args));
486 obstack_grow (&temporary_obstack,
487 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
489 obstack_1grow (&temporary_obstack, '\0');
491 sig = get_identifier ((char *) obstack_base (&temporary_obstack));
492 TYPE_ARGUMENT_SIGNATURE (type) = sig;
493 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
495 return sig;
498 /* Return the signature of the given TYPE. */
500 tree
501 build_java_signature (tree type)
503 tree sig, t;
504 while (TREE_CODE (type) == POINTER_TYPE)
505 type = TREE_TYPE (type);
506 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
507 sig = TYPE_SIGNATURE (type);
508 if (sig == NULL_TREE)
510 char sg[2];
511 switch (TREE_CODE (type))
513 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
514 case VOID_TYPE: sg[0] = 'V'; goto native;
515 case INTEGER_TYPE:
516 if (type == char_type_node || type == promoted_char_type_node)
518 sg[0] = 'C';
519 goto native;
521 switch (TYPE_PRECISION (type))
523 case 8: sg[0] = 'B'; goto native;
524 case 16: sg[0] = 'S'; goto native;
525 case 32: sg[0] = 'I'; goto native;
526 case 64: sg[0] = 'J'; goto native;
527 default: goto bad_type;
529 case REAL_TYPE:
530 switch (TYPE_PRECISION (type))
532 case 32: sg[0] = 'F'; goto native;
533 case 64: sg[0] = 'D'; goto native;
534 default: goto bad_type;
536 native:
537 sg[1] = 0;
538 sig = get_identifier (sg);
539 break;
540 case RECORD_TYPE:
541 if (TYPE_ARRAY_P (type))
543 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
544 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
545 "[", 0, 0, "");
547 else
549 t = DECL_NAME (TYPE_NAME (type));
550 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
551 "L", '.', '/', ";");
553 break;
554 case METHOD_TYPE:
555 case FUNCTION_TYPE:
557 extern struct obstack temporary_obstack;
558 sig = build_java_argument_signature (type);
559 obstack_1grow (&temporary_obstack, '(');
560 obstack_grow (&temporary_obstack,
561 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
562 obstack_1grow (&temporary_obstack, ')');
564 t = build_java_signature (TREE_TYPE (type));
565 obstack_grow0 (&temporary_obstack,
566 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
568 sig = get_identifier ((char *) obstack_base (&temporary_obstack));
569 obstack_free (&temporary_obstack,
570 obstack_base (&temporary_obstack));
572 break;
573 bad_type:
574 default:
575 gcc_unreachable ();
577 TYPE_SIGNATURE (type) = sig;
579 return sig;
582 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
584 void
585 set_java_signature (tree type, tree sig)
587 tree old_sig;
588 while (TREE_CODE (type) == POINTER_TYPE)
589 type = TREE_TYPE (type);
590 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
591 old_sig = TYPE_SIGNATURE (type);
592 if (old_sig != NULL_TREE && old_sig != sig)
593 abort ();
594 TYPE_SIGNATURE (type) = sig;
595 #if 0 /* careful about METHOD_TYPE */
596 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
597 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
598 #endif
601 /* Search in SEARCHED_CLASS and its superclasses for a method matching
602 METHOD_NAME and signature METHOD_SIGNATURE. This function will
603 only search for methods declared in the class hierarchy; interfaces
604 will not be considered. Returns NULL_TREE if the method is not
605 found. */
606 tree
607 lookup_argument_method (tree searched_class, tree method_name,
608 tree method_signature)
610 return lookup_do (searched_class, 0,
611 method_name, method_signature,
612 build_java_argument_signature);
615 /* Like lookup_argument_method, but lets the caller set any flags
616 desired. */
617 tree
618 lookup_argument_method_generic (tree searched_class, tree method_name,
619 tree method_signature, int flags)
621 return lookup_do (searched_class, flags,
622 method_name, method_signature,
623 build_java_argument_signature);
627 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
628 matching METHOD_NAME and signature METHOD_SIGNATURE. Return a
629 FUNCTION_DECL on success, or NULL_TREE if none found. (Contrast
630 lookup_argument_method, which ignores return type.) If
631 SEARCHED_CLASS is an interface, search it too. */
632 tree
633 lookup_java_method (tree searched_class, tree method_name,
634 tree method_signature)
636 return lookup_do (searched_class, SEARCH_INTERFACE, method_name,
637 method_signature, build_java_signature);
640 /* Return true iff KLASS (or its ancestors) has a method METHOD_NAME.  */
642 has_method (tree klass, tree method_name)
644 return lookup_do (klass, SEARCH_INTERFACE,
645 method_name, NULL_TREE,
646 build_null_signature) != NULL_TREE;
649 /* Search in class SEARCHED_CLASS, but not its superclasses, for a
650 method matching METHOD_NAME and signature SIGNATURE. A private
651 helper for lookup_do. */
652 static tree
653 shallow_find_method (tree searched_class, int flags, tree method_name,
654 tree signature, tree (*signature_builder) (tree))
656 tree method;
657 for (method = TYPE_METHODS (searched_class);
658 method != NULL_TREE; method = DECL_CHAIN (method))
660 tree method_sig = (*signature_builder) (TREE_TYPE (method));
661 if (DECL_NAME (method) == method_name && method_sig == signature)
663 /* If the caller requires a visible method, then we
664 skip invisible methods here. */
665 if (! (flags & SEARCH_VISIBLE)
666 || ! METHOD_INVISIBLE (method))
667 return method;
670 return NULL_TREE;
673 /* Search in the superclasses of SEARCHED_CLASS for a method matching
674 METHOD_NAME and signature SIGNATURE. A private helper for
675 lookup_do. */
676 static tree
677 find_method_in_superclasses (tree searched_class, int flags,
678 tree method_name, tree signature,
679 tree (*signature_builder) (tree))
681 tree klass;
682 for (klass = CLASSTYPE_SUPER (searched_class); klass != NULL_TREE;
683 klass = CLASSTYPE_SUPER (klass))
685 tree method;
686 method = shallow_find_method (klass, flags, method_name,
687 signature, signature_builder);
688 if (method != NULL_TREE)
689 return method;
692 return NULL_TREE;
695 /* Search in the interfaces of SEARCHED_CLASS and its superinterfaces
696 for a method matching METHOD_NAME and signature SIGNATURE. A
697 private helper for lookup_do. */
698 static tree
699 find_method_in_interfaces (tree searched_class, int flags, tree method_name,
700 tree signature, tree (*signature_builder) (tree))
702 int i;
703 tree binfo, base_binfo;
705 for (binfo = TYPE_BINFO (searched_class), i = 1;
706 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
708 tree iclass = BINFO_TYPE (base_binfo);
709 tree method;
711 /* If the superinterface hasn't been loaded yet, do so now. */
712 if (!CLASS_LOADED_P (iclass))
713 load_class (iclass, 1);
715 /* First, we look in ICLASS. If that doesn't work we'll
716 recursively look through all its superinterfaces. */
717 method = shallow_find_method (iclass, flags, method_name,
718 signature, signature_builder);
719 if (method != NULL_TREE)
720 return method;
722 method = find_method_in_interfaces
723 (iclass, flags, method_name, signature, signature_builder);
724 if (method != NULL_TREE)
725 return method;
728 return NULL_TREE;
732 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
733 matching METHOD_NAME and signature SIGNATURE. FLAGS control some
734 parameters of the search.
736 SEARCH_INTERFACE means also search interfaces and superinterfaces
737 of SEARCHED_CLASS.
739 SEARCH_SUPER means skip SEARCHED_CLASS and start with its
740 superclass.
742 SEARCH_VISIBLE means skip methods for which METHOD_INVISIBLE is
743 set.
745 Return the matched method DECL or NULL_TREE. SIGNATURE_BUILDER is
746 used on method candidates to build their (sometimes partial)
747 signature. */
748 static tree
749 lookup_do (tree searched_class, int flags, tree method_name,
750 tree signature, tree (*signature_builder) (tree))
752 tree method;
753 tree orig_class = searched_class;
755 if (searched_class == NULL_TREE)
756 return NULL_TREE;
758 if (flags & SEARCH_SUPER)
760 searched_class = CLASSTYPE_SUPER (searched_class);
761 if (searched_class == NULL_TREE)
762 return NULL_TREE;
765 /* First look in our own methods. */
766 method = shallow_find_method (searched_class, flags, method_name,
767 signature, signature_builder);
768 if (method)
769 return method;
771 /* Then look in our superclasses. */
772 if (! CLASS_INTERFACE (TYPE_NAME (searched_class)))
773 method = find_method_in_superclasses (searched_class, flags, method_name,
774 signature, signature_builder);
775 if (method)
776 return method;
778 /* If that doesn't work, look in our interfaces. */
779 if (flags & SEARCH_INTERFACE)
780 method = find_method_in_interfaces (orig_class, flags, method_name,
781 signature, signature_builder);
783 return method;
786 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
787 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
789 tree
790 lookup_java_constructor (tree clas, tree method_signature)
792 tree method = TYPE_METHODS (clas);
793 for ( ; method != NULL_TREE; method = DECL_CHAIN (method))
795 tree method_sig = build_java_signature (TREE_TYPE (method));
796 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
797 return method;
799 return NULL_TREE;
802 /* Return a type which is the Binary Numeric Promotion of the pair T1,
803 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
804 Promotion. It assumes that both T1 and T2 are eligible to BNP. */
806 tree
807 binary_numeric_promotion (tree t1, tree t2, tree *exp1, tree *exp2)
809 if (t1 == double_type_node || t2 == double_type_node)
811 if (t1 != double_type_node)
812 *exp1 = convert (double_type_node, *exp1);
813 if (t2 != double_type_node)
814 *exp2 = convert (double_type_node, *exp2);
815 return double_type_node;
817 if (t1 == float_type_node || t2 == float_type_node)
819 if (t1 != float_type_node)
820 *exp1 = convert (float_type_node, *exp1);
821 if (t2 != float_type_node)
822 *exp2 = convert (float_type_node, *exp2);
823 return float_type_node;
825 if (t1 == long_type_node || t2 == long_type_node)
827 if (t1 != long_type_node)
828 *exp1 = convert (long_type_node, *exp1);
829 if (t2 != long_type_node)
830 *exp2 = convert (long_type_node, *exp2);
831 return long_type_node;
834 if (t1 != int_type_node)
835 *exp1 = convert (int_type_node, *exp1);
836 if (t2 != int_type_node)
837 *exp2 = convert (int_type_node, *exp2);
838 return int_type_node;