OSDN Git Service

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