OSDN Git Service

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