OSDN Git Service

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