OSDN Git Service

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