OSDN Git Service

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