OSDN Git Service

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