OSDN Git Service

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