2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / java / typeck.c
blobdf06a4bf8f6b3219d4fa4418da2cd060387bfcf7
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 "alias.h"
30 #include "symtab.h"
31 #include "options.h"
32 #include "tree.h"
33 #include "fold-const.h"
34 #include "stor-layout.h"
35 #include "stringpool.h"
36 #include "obstack.h"
37 #include "flags.h"
38 #include "java-tree.h"
39 #include "jcf.h"
40 #include "convert.h"
41 #include "diagnostic-core.h"
43 static tree convert_ieee_real_to_integer (tree, tree);
44 static tree parse_signature_type (const unsigned char **,
45 const unsigned char *);
46 static tree lookup_do (tree, int, tree, tree, tree (*)(tree));
47 static tree build_null_signature (tree);
49 tree * type_map;
51 /* Set the type of the local variable with index SLOT to TYPE. */
53 void
54 set_local_type (int slot, tree type)
56 int max_locals = DECL_MAX_LOCALS(current_function_decl);
57 int nslots = TYPE_IS_WIDE (type) ? 2 : 1;
59 gcc_assert (slot >= 0 && (slot + nslots - 1 < max_locals));
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)
130 return fold_convert (type, expr);
131 if (code == INTEGER_TYPE)
133 if (type == char_type_node || type == promoted_char_type_node)
134 return fold_convert (type, expr);
135 if ((really_constant_p (expr) || ! flag_unsafe_math_optimizations)
136 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
137 return convert_ieee_real_to_integer (type, expr);
138 else
140 /* fold very helpfully sets the overflow status if a type
141 overflows in a narrowing integer conversion, but Java
142 doesn't care. */
143 tree tmp = fold (convert_to_integer (type, expr));
144 if (TREE_CODE (tmp) == INTEGER_CST)
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 (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 /* Thorough checking of the arrayness of TYPE. */
201 is_array_type_p (tree type)
203 return TREE_CODE (type) == POINTER_TYPE
204 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
205 && TYPE_ARRAY_P (TREE_TYPE (type));
208 /* Return the length of a Java array type.
209 Return -1 if the length is unknown or non-constant. */
211 HOST_WIDE_INT
212 java_array_type_length (tree array_type)
214 tree arfld;
215 if (TREE_CODE (array_type) == POINTER_TYPE)
216 array_type = TREE_TYPE (array_type);
217 arfld = DECL_CHAIN (DECL_CHAIN (TYPE_FIELDS (array_type)));
218 if (arfld != NULL_TREE)
220 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
221 if (index_type != NULL_TREE)
223 tree high = TYPE_MAX_VALUE (index_type);
224 if (TREE_CODE (high) == INTEGER_CST)
225 return TREE_INT_CST_LOW (high) + 1;
228 return -1;
231 /* An array of unknown length will be ultimately given a length of
232 -2, so that we can still have `length' producing a negative value
233 even if found. This was part of an optimization aiming at removing
234 `length' from static arrays. We could restore it, FIXME. */
236 tree
237 build_prim_array_type (tree element_type, HOST_WIDE_INT length)
239 tree index = NULL;
241 if (length != -1)
243 tree max_index = build_int_cst (sizetype, length - 1);
244 index = build_index_type (max_index);
246 return build_array_type (element_type, index);
249 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
250 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
251 The LENGTH is -1 if the length is unknown. */
253 tree
254 build_java_array_type (tree element_type, HOST_WIDE_INT length)
256 tree sig, t, fld, atype, arfld;
257 char buf[23];
258 tree elsig = build_java_signature (element_type);
259 tree el_name = element_type;
260 buf[0] = '[';
261 if (length >= 0)
262 sprintf (buf+1, HOST_WIDE_INT_PRINT_DEC, length);
263 else
264 buf[1] = '\0';
265 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
266 buf, 0, 0, "");
267 t = IDENTIFIER_SIGNATURE_TYPE (sig);
268 if (t != NULL_TREE)
269 return TREE_TYPE (t);
270 t = make_class ();
271 IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
272 TYPE_ARRAY_P (t) = 1;
274 if (TREE_CODE (el_name) == POINTER_TYPE)
275 el_name = TREE_TYPE (el_name);
276 el_name = TYPE_NAME (el_name);
277 if (TREE_CODE (el_name) == TYPE_DECL)
278 el_name = DECL_NAME (el_name);
280 char suffix[23];
281 if (length >= 0)
282 sprintf (suffix, "[%d]", (int)length);
283 else
284 strcpy (suffix, "[]");
285 TYPE_NAME (t)
286 = TYPE_STUB_DECL (t)
287 = build_decl (input_location, TYPE_DECL,
288 identifier_subst (el_name, "", '.', '.', suffix),
290 TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (t)) = true;
293 set_java_signature (t, sig);
294 set_super_info (0, t, object_type_node, 0);
295 if (TREE_CODE (element_type) == RECORD_TYPE)
296 element_type = promote_type (element_type);
297 TYPE_ARRAY_ELEMENT (t) = element_type;
299 /* Add length pseudo-field. */
300 fld = build_decl (input_location,
301 FIELD_DECL, get_identifier ("length"), int_type_node);
302 TYPE_FIELDS (t) = fld;
303 DECL_CONTEXT (fld) = t;
304 FIELD_PUBLIC (fld) = 1;
305 FIELD_FINAL (fld) = 1;
306 TREE_READONLY (fld) = 1;
308 atype = build_prim_array_type (element_type, length);
309 arfld = build_decl (input_location,
310 FIELD_DECL, get_identifier ("data"), atype);
311 DECL_CONTEXT (arfld) = t;
312 DECL_CHAIN (fld) = arfld;
313 DECL_ALIGN (arfld) = TYPE_ALIGN (element_type);
315 /* We could layout_class, but that loads java.lang.Object prematurely.
316 * This is called by the parser, and it is a bad idea to do load_class
317 * in the middle of parsing, because of possible circularity problems. */
318 push_super_field (t, object_type_node);
319 layout_type (t);
321 return t;
324 /* Promote TYPE to the type actually used for fields and parameters. */
326 tree
327 promote_type (tree type)
329 switch (TREE_CODE (type))
331 case RECORD_TYPE:
332 return build_pointer_type (type);
333 case BOOLEAN_TYPE:
334 if (type == boolean_type_node)
335 return promoted_boolean_type_node;
336 goto handle_int;
337 case INTEGER_TYPE:
338 if (type == char_type_node)
339 return promoted_char_type_node;
340 handle_int:
341 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
343 if (type == short_type_node)
344 return promoted_short_type_node;
345 if (type == byte_type_node)
346 return promoted_byte_type_node;
347 return int_type_node;
349 /* ... else fall through ... */
350 default:
351 return type;
355 /* Parse a signature string, starting at *PTR and ending at LIMIT.
356 Return the seen TREE_TYPE, updating *PTR. */
358 static tree
359 parse_signature_type (const unsigned char **ptr, const unsigned char *limit)
361 tree type;
362 gcc_assert (*ptr < limit);
364 switch (**ptr)
366 case 'B': (*ptr)++; return byte_type_node;
367 case 'C': (*ptr)++; return char_type_node;
368 case 'D': (*ptr)++; return double_type_node;
369 case 'F': (*ptr)++; return float_type_node;
370 case 'S': (*ptr)++; return short_type_node;
371 case 'I': (*ptr)++; return int_type_node;
372 case 'J': (*ptr)++; return long_type_node;
373 case 'Z': (*ptr)++; return boolean_type_node;
374 case 'V': (*ptr)++; return void_type_node;
375 case '[':
376 for ((*ptr)++; (*ptr) < limit && ISDIGIT (**ptr); ) (*ptr)++;
377 type = parse_signature_type (ptr, limit);
378 type = build_java_array_type (type, -1);
379 break;
380 case 'L':
382 const unsigned char *start = ++(*ptr);
383 const unsigned char *str = start;
384 for ( ; ; str++)
386 gcc_assert (str < limit);
387 if (*str == ';')
388 break;
390 *ptr = str+1;
391 type = lookup_class (unmangle_classname ((const char *) start, str - start));
392 break;
394 default:
395 gcc_unreachable ();
397 return promote_type (type);
400 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
401 and SIG_LENGTH bytes long.
402 Return a gcc type node. */
404 tree
405 parse_signature_string (const unsigned char *sig_string, int sig_length)
407 tree result_type;
408 const unsigned char *str = sig_string;
409 const unsigned char *limit = str + sig_length;
411 if (str < limit && str[0] == '(')
413 tree argtype_list = NULL_TREE;
414 str++;
415 while (str < limit && str[0] != ')')
417 tree argtype = parse_signature_type (&str, limit);
418 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
420 if (str++, str >= limit)
421 abort ();
422 result_type = parse_signature_type (&str, limit);
423 argtype_list = chainon (nreverse (argtype_list), end_params_node);
424 result_type = build_function_type (result_type, argtype_list);
426 else
427 result_type = parse_signature_type (&str, limit);
428 if (str != limit)
429 error ("junk at end of signature string");
430 return result_type;
433 /* Convert a signature to its type.
434 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
437 tree
438 get_type_from_signature (tree signature)
440 const unsigned char *sig = (const unsigned char *) IDENTIFIER_POINTER (signature);
441 int len = IDENTIFIER_LENGTH (signature);
442 tree type;
443 /* Primitive types aren't cached. */
444 if (len <= 1)
445 return parse_signature_string (sig, len);
446 type = IDENTIFIER_SIGNATURE_TYPE (signature);
447 if (type == NULL_TREE)
449 type = parse_signature_string (sig, len);
450 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
452 return type;
455 /* Ignore signature and always return null. Used by has_method. */
457 static tree
458 build_null_signature (tree type ATTRIBUTE_UNUSED)
460 return NULL_TREE;
463 /* Return the signature string for the arguments of method type TYPE. */
465 tree
466 build_java_argument_signature (tree type)
468 extern struct obstack temporary_obstack;
469 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
470 if (sig == NULL_TREE)
472 tree args = TYPE_ARG_TYPES (type);
473 if (TREE_CODE (type) == METHOD_TYPE)
474 args = TREE_CHAIN (args); /* Skip "this" argument. */
475 for (; args != end_params_node; args = TREE_CHAIN (args))
477 tree t = build_java_signature (TREE_VALUE (args));
478 obstack_grow (&temporary_obstack,
479 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
481 obstack_1grow (&temporary_obstack, '\0');
483 sig = get_identifier ((char *) obstack_base (&temporary_obstack));
484 TYPE_ARGUMENT_SIGNATURE (type) = sig;
485 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
487 return sig;
490 /* Return the signature of the given TYPE. */
492 tree
493 build_java_signature (tree type)
495 tree sig, t;
496 while (TREE_CODE (type) == POINTER_TYPE)
497 type = TREE_TYPE (type);
498 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
499 sig = TYPE_SIGNATURE (type);
500 if (sig == NULL_TREE)
502 char sg[2];
503 switch (TREE_CODE (type))
505 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
506 case VOID_TYPE: sg[0] = 'V'; goto native;
507 case INTEGER_TYPE:
508 if (type == char_type_node || type == promoted_char_type_node)
510 sg[0] = 'C';
511 goto native;
513 switch (TYPE_PRECISION (type))
515 case 8: sg[0] = 'B'; goto native;
516 case 16: sg[0] = 'S'; goto native;
517 case 32: sg[0] = 'I'; goto native;
518 case 64: sg[0] = 'J'; goto native;
519 default: goto bad_type;
521 case REAL_TYPE:
522 switch (TYPE_PRECISION (type))
524 case 32: sg[0] = 'F'; goto native;
525 case 64: sg[0] = 'D'; goto native;
526 default: goto bad_type;
528 native:
529 sg[1] = 0;
530 sig = get_identifier (sg);
531 break;
532 case RECORD_TYPE:
533 if (TYPE_ARRAY_P (type))
535 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
536 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
537 "[", 0, 0, "");
539 else
541 t = DECL_NAME (TYPE_NAME (type));
542 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
543 "L", '.', '/', ";");
545 break;
546 case METHOD_TYPE:
547 case FUNCTION_TYPE:
549 extern struct obstack temporary_obstack;
550 sig = build_java_argument_signature (type);
551 obstack_1grow (&temporary_obstack, '(');
552 obstack_grow (&temporary_obstack,
553 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
554 obstack_1grow (&temporary_obstack, ')');
556 t = build_java_signature (TREE_TYPE (type));
557 obstack_grow0 (&temporary_obstack,
558 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
560 sig = get_identifier ((char *) obstack_base (&temporary_obstack));
561 obstack_free (&temporary_obstack,
562 obstack_base (&temporary_obstack));
564 break;
565 bad_type:
566 default:
567 gcc_unreachable ();
569 TYPE_SIGNATURE (type) = sig;
571 return sig;
574 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
576 void
577 set_java_signature (tree type, tree sig)
579 tree old_sig;
580 while (TREE_CODE (type) == POINTER_TYPE)
581 type = TREE_TYPE (type);
582 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
583 old_sig = TYPE_SIGNATURE (type);
584 if (old_sig != NULL_TREE && old_sig != sig)
585 abort ();
586 TYPE_SIGNATURE (type) = sig;
587 #if 0 /* careful about METHOD_TYPE */
588 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
589 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
590 #endif
593 /* Search in SEARCHED_CLASS and its superclasses for a method matching
594 METHOD_NAME and signature METHOD_SIGNATURE. This function will
595 only search for methods declared in the class hierarchy; interfaces
596 will not be considered. Returns NULL_TREE if the method is not
597 found. */
598 tree
599 lookup_argument_method (tree searched_class, tree method_name,
600 tree method_signature)
602 return lookup_do (searched_class, 0,
603 method_name, method_signature,
604 build_java_argument_signature);
607 /* Like lookup_argument_method, but lets the caller set any flags
608 desired. */
609 tree
610 lookup_argument_method_generic (tree searched_class, tree method_name,
611 tree method_signature, int flags)
613 return lookup_do (searched_class, flags,
614 method_name, method_signature,
615 build_java_argument_signature);
619 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
620 matching METHOD_NAME and signature METHOD_SIGNATURE. Return a
621 FUNCTION_DECL on success, or NULL_TREE if none found. (Contrast
622 lookup_argument_method, which ignores return type.) If
623 SEARCHED_CLASS is an interface, search it too. */
624 tree
625 lookup_java_method (tree searched_class, tree method_name,
626 tree method_signature)
628 return lookup_do (searched_class, SEARCH_INTERFACE, method_name,
629 method_signature, build_java_signature);
632 /* Return true iff KLASS (or its ancestors) has a method METHOD_NAME.  */
634 has_method (tree klass, tree method_name)
636 return lookup_do (klass, SEARCH_INTERFACE,
637 method_name, NULL_TREE,
638 build_null_signature) != NULL_TREE;
641 /* Search in class SEARCHED_CLASS, but not its superclasses, for a
642 method matching METHOD_NAME and signature SIGNATURE. A private
643 helper for lookup_do. */
644 static tree
645 shallow_find_method (tree searched_class, int flags, tree method_name,
646 tree signature, tree (*signature_builder) (tree))
648 tree method;
649 for (method = TYPE_METHODS (searched_class);
650 method != NULL_TREE; method = DECL_CHAIN (method))
652 tree method_sig = (*signature_builder) (TREE_TYPE (method));
653 if (DECL_NAME (method) == method_name && method_sig == signature)
655 /* If the caller requires a visible method, then we
656 skip invisible methods here. */
657 if (! (flags & SEARCH_VISIBLE)
658 || ! METHOD_INVISIBLE (method))
659 return method;
662 return NULL_TREE;
665 /* Search in the superclasses of SEARCHED_CLASS for a method matching
666 METHOD_NAME and signature SIGNATURE. A private helper for
667 lookup_do. */
668 static tree
669 find_method_in_superclasses (tree searched_class, int flags,
670 tree method_name, tree signature,
671 tree (*signature_builder) (tree))
673 tree klass;
674 for (klass = CLASSTYPE_SUPER (searched_class); klass != NULL_TREE;
675 klass = CLASSTYPE_SUPER (klass))
677 tree method;
678 method = shallow_find_method (klass, flags, method_name,
679 signature, signature_builder);
680 if (method != NULL_TREE)
681 return method;
684 return NULL_TREE;
687 /* Search in the interfaces of SEARCHED_CLASS and its superinterfaces
688 for a method matching METHOD_NAME and signature SIGNATURE. A
689 private helper for lookup_do. */
690 static tree
691 find_method_in_interfaces (tree searched_class, int flags, tree method_name,
692 tree signature, tree (*signature_builder) (tree))
694 int i;
695 tree binfo, base_binfo;
697 for (binfo = TYPE_BINFO (searched_class), i = 1;
698 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
700 tree iclass = BINFO_TYPE (base_binfo);
701 tree method;
703 /* If the superinterface hasn't been loaded yet, do so now. */
704 if (!CLASS_LOADED_P (iclass))
705 load_class (iclass, 1);
707 /* First, we look in ICLASS. If that doesn't work we'll
708 recursively look through all its superinterfaces. */
709 method = shallow_find_method (iclass, flags, method_name,
710 signature, signature_builder);
711 if (method != NULL_TREE)
712 return method;
714 method = find_method_in_interfaces
715 (iclass, flags, method_name, signature, signature_builder);
716 if (method != NULL_TREE)
717 return method;
720 return NULL_TREE;
724 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
725 matching METHOD_NAME and signature SIGNATURE. FLAGS control some
726 parameters of the search.
728 SEARCH_INTERFACE means also search interfaces and superinterfaces
729 of SEARCHED_CLASS.
731 SEARCH_SUPER means skip SEARCHED_CLASS and start with its
732 superclass.
734 SEARCH_VISIBLE means skip methods for which METHOD_INVISIBLE is
735 set.
737 Return the matched method DECL or NULL_TREE. SIGNATURE_BUILDER is
738 used on method candidates to build their (sometimes partial)
739 signature. */
740 static tree
741 lookup_do (tree searched_class, int flags, tree method_name,
742 tree signature, tree (*signature_builder) (tree))
744 tree method;
745 tree orig_class = searched_class;
747 if (searched_class == NULL_TREE)
748 return NULL_TREE;
750 if (flags & SEARCH_SUPER)
752 searched_class = CLASSTYPE_SUPER (searched_class);
753 if (searched_class == NULL_TREE)
754 return NULL_TREE;
757 /* First look in our own methods. */
758 method = shallow_find_method (searched_class, flags, method_name,
759 signature, signature_builder);
760 if (method)
761 return method;
763 /* Then look in our superclasses. */
764 if (! CLASS_INTERFACE (TYPE_NAME (searched_class)))
765 method = find_method_in_superclasses (searched_class, flags, method_name,
766 signature, signature_builder);
767 if (method)
768 return method;
770 /* If that doesn't work, look in our interfaces. */
771 if (flags & SEARCH_INTERFACE)
772 method = find_method_in_interfaces (orig_class, flags, method_name,
773 signature, signature_builder);
775 return method;
778 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
779 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
781 tree
782 lookup_java_constructor (tree clas, tree method_signature)
784 tree method = TYPE_METHODS (clas);
785 for ( ; method != NULL_TREE; method = DECL_CHAIN (method))
787 tree method_sig = build_java_signature (TREE_TYPE (method));
788 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
789 return method;
791 return NULL_TREE;
794 /* Return a type which is the Binary Numeric Promotion of the pair T1,
795 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
796 Promotion. It assumes that both T1 and T2 are eligible to BNP. */
798 tree
799 binary_numeric_promotion (tree t1, tree t2, tree *exp1, tree *exp2)
801 if (t1 == double_type_node || t2 == double_type_node)
803 if (t1 != double_type_node)
804 *exp1 = convert (double_type_node, *exp1);
805 if (t2 != double_type_node)
806 *exp2 = convert (double_type_node, *exp2);
807 return double_type_node;
809 if (t1 == float_type_node || t2 == float_type_node)
811 if (t1 != float_type_node)
812 *exp1 = convert (float_type_node, *exp1);
813 if (t2 != float_type_node)
814 *exp2 = convert (float_type_node, *exp2);
815 return float_type_node;
817 if (t1 == long_type_node || t2 == long_type_node)
819 if (t1 != long_type_node)
820 *exp1 = convert (long_type_node, *exp1);
821 if (t2 != long_type_node)
822 *exp2 = convert (long_type_node, *exp2);
823 return long_type_node;
826 if (t1 != int_type_node)
827 *exp1 = convert (int_type_node, *exp1);
828 if (t2 != int_type_node)
829 *exp2 = convert (int_type_node, *exp2);
830 return int_type_node;