OSDN Git Service

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