OSDN Git Service

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