OSDN Git Service

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