OSDN Git Service

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