OSDN Git Service

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