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