OSDN Git Service

2000-08-16 Andrew Haley <aph@cygnus.com>
[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 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  
20
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
24
25 /* Written by Per Bothner <bothner@cygnus.com> */
26
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "obstack.h"
31 #include "flags.h"
32 #include "java-tree.h"
33 #include "jcf.h"
34 #include "convert.h"
35 #include "toplev.h"
36
37 static tree convert_ieee_real_to_integer PARAMS ((tree, tree));
38 static tree parse_signature_type PARAMS ((const unsigned char **,
39                                          const unsigned char *));
40 static tree lookup_do PARAMS ((tree, tree, tree, tree, tree (*)(tree)));
41
42 tree * type_map;
43 extern struct obstack permanent_obstack;
44
45 /* Set the type of the local variable with index SLOT to TYPE. */
46
47 void
48 set_local_type (slot, type)
49      int slot;
50      tree type;
51 {
52   int max_locals = DECL_MAX_LOCALS(current_function_decl);
53   int nslots = TYPE_IS_WIDE (type) ? 2 : 1;
54   if (slot < 0 || slot + nslots - 1 >= max_locals)
55     fatal ("invalid local variable index");
56   type_map[slot] = type;
57   while (--nslots > 0)
58     type_map[++slot] = void_type_node;
59 }
60
61 /* Convert an IEEE real to an integer type.  The result of such a
62    conversion when the source operand is a NaN isn't defined by
63    IEEE754, but by the Java language standard: it must be zero.  Also,
64    overflows must be clipped to within range.  This conversion
65    produces something like:
66
67       ((expr >= (float)MAX_INT)
68        ? MAX_INT 
69        : ((expr <= (float)MIN_INT)
70           ? MIN_INT
71           : ((expr != expr)
72              ? 0 
73              : (int)expr))) */
74
75 static tree
76 convert_ieee_real_to_integer (type, expr)
77      tree type, expr;
78 {
79   tree result;
80   expr = save_expr (expr);
81
82   result = build (COND_EXPR, type,
83                   build (NE_EXPR, boolean_type_node, expr, expr),
84                   convert (type, integer_zero_node),
85                   convert_to_integer (type, expr));
86                   
87   result = build (COND_EXPR, type, 
88                   build (LE_EXPR, boolean_type_node, expr, 
89                          convert (TREE_TYPE (expr), TYPE_MIN_VALUE (type))),
90                   TYPE_MIN_VALUE (type),
91                   result);
92
93   result = build (COND_EXPR, type,
94                   build (GE_EXPR, boolean_type_node, expr, 
95                          convert (TREE_TYPE (expr), TYPE_MAX_VALUE (type))),    
96                   TYPE_MAX_VALUE (type),
97                   result);
98
99   return result;
100 }  
101
102 /* Create an expression whose value is that of EXPR,
103    converted to type TYPE.  The TREE_TYPE of the value
104    is always TYPE.  This function implements all reasonable
105    conversions; callers should filter out those that are
106    not permitted by the language being compiled.  */
107
108 tree
109 convert (type, expr)
110      tree type, expr;
111 {
112   register enum tree_code code = TREE_CODE (type);
113
114   if (!expr)
115    return error_mark_node;
116
117   if (do_not_fold)
118     return build1 (NOP_EXPR, type, expr);
119
120   if (type == TREE_TYPE (expr)
121       || TREE_CODE (expr) == ERROR_MARK)
122     return expr;
123   if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
124     return error_mark_node;
125   if (code == VOID_TYPE)
126     return build1 (CONVERT_EXPR, type, expr);
127   if (code == BOOLEAN_TYPE)
128     return fold (convert_to_boolean (type, expr));
129   if (code == INTEGER_TYPE)
130     {
131       if (! flag_fast_math
132           && ! flag_emit_class_files
133           && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
134           && TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
135         return fold (convert_ieee_real_to_integer (type, expr));
136       else
137         return fold (convert_to_integer (type, expr));
138     }     
139   if (code == REAL_TYPE)
140     return fold (convert_to_real (type, expr));
141   if (code == CHAR_TYPE)
142     return fold (convert_to_char (type, expr));
143   if (code == POINTER_TYPE)
144     return fold (convert_to_pointer (type, expr));
145   error ("conversion to non-scalar type requested");
146   return error_mark_node;
147 }
148
149
150 tree
151 convert_to_char (type, expr)
152     tree type, expr;
153 {
154   return build1 (NOP_EXPR, type, expr);
155 }
156
157 tree
158 convert_to_boolean (type, expr)
159      tree type, expr;
160 {
161   return build1 (NOP_EXPR, type, expr);
162 }
163
164 /* Print an error message for invalid use of an incomplete type.
165    VALUE is the expression that was used (or 0 if that isn't known)
166    and TYPE is the type that was invalid.  */
167
168 void
169 incomplete_type_error (value, type)
170   tree value ATTRIBUTE_UNUSED;
171   tree type ATTRIBUTE_UNUSED;
172 {
173   error ("internal error - use of undefined type");
174 }
175
176 /* Return a data type that has machine mode MODE.
177    If the mode is an integer,
178    then UNSIGNEDP selects between signed and unsigned types.  */
179
180 tree
181 type_for_mode (mode, unsignedp)
182      enum machine_mode mode;
183      int unsignedp;
184 {
185   if (mode == TYPE_MODE (int_type_node))
186     return unsignedp ? unsigned_int_type_node : int_type_node;
187   if (mode == TYPE_MODE (long_type_node))
188     return unsignedp ? unsigned_long_type_node : long_type_node;
189   if (mode == TYPE_MODE (short_type_node))
190     return unsignedp ? unsigned_short_type_node : short_type_node;
191   if (mode == TYPE_MODE (byte_type_node))
192     return unsignedp ? unsigned_byte_type_node : byte_type_node;
193   if (mode == TYPE_MODE (float_type_node))
194     return float_type_node;
195   if (mode == TYPE_MODE (double_type_node))
196     return double_type_node;
197
198   return 0;
199 }
200
201 /* Return an integer type with BITS bits of precision,
202    that is unsigned if UNSIGNEDP is nonzero, otherwise signed.  */
203
204 tree
205 type_for_size (bits, unsignedp)
206      unsigned bits;
207      int unsignedp;
208 {
209   if (bits <= TYPE_PRECISION (byte_type_node))
210     return unsignedp ? unsigned_byte_type_node : byte_type_node;
211   if (bits <= TYPE_PRECISION (short_type_node))
212     return unsignedp ? unsigned_short_type_node : short_type_node;
213   if (bits <= TYPE_PRECISION (int_type_node))
214     return unsignedp ? unsigned_int_type_node : int_type_node;
215   if (bits <= TYPE_PRECISION (long_type_node))
216     return unsignedp ? unsigned_long_type_node : long_type_node;
217   return 0;
218 }
219
220 /* Return a type the same as TYPE except unsigned or
221    signed according to UNSIGNEDP.  */
222
223 tree
224 signed_or_unsigned_type (unsignedp, type)
225      int unsignedp;
226      tree type;
227 {
228   if (! INTEGRAL_TYPE_P (type))
229     return type;
230   if (TYPE_PRECISION (type) == TYPE_PRECISION (int_type_node))
231     return unsignedp ? unsigned_int_type_node : int_type_node;
232   if (TYPE_PRECISION (type) == TYPE_PRECISION (byte_type_node))
233     return unsignedp ? unsigned_byte_type_node : byte_type_node;
234   if (TYPE_PRECISION (type) == TYPE_PRECISION (short_type_node))
235     return unsignedp ? unsigned_short_type_node : short_type_node;
236   if (TYPE_PRECISION (type) == TYPE_PRECISION (long_type_node))
237     return unsignedp ? unsigned_long_type_node : long_type_node;
238   return type;
239 }
240
241 /* Return a signed type the same as TYPE in other respects.  */
242
243 tree
244 signed_type (type)
245      tree type;
246 {
247   return signed_or_unsigned_type (0, type);
248 }
249
250 /* Return an unsigned type the same as TYPE in other respects.  */
251
252 tree
253 unsigned_type (type)
254      tree type;
255 {
256   return signed_or_unsigned_type (1, type);
257
258 }
259
260 /* Mark EXP saying that we need to be able to take the
261    address of it; it should not be allocated in a register.
262    Value is 1 if successful.  */
263
264 int
265 mark_addressable (exp)
266      tree exp;
267 {
268   register tree x = exp;
269   while (1)
270     switch (TREE_CODE (x))
271       {
272       case ADDR_EXPR:
273       case COMPONENT_REF:
274       case ARRAY_REF:
275       case REALPART_EXPR:
276       case IMAGPART_EXPR:
277         x = TREE_OPERAND (x, 0);
278         break;
279
280       case TRUTH_ANDIF_EXPR:
281       case TRUTH_ORIF_EXPR:
282       case COMPOUND_EXPR:
283         x = TREE_OPERAND (x, 1);
284         break;
285
286       case COND_EXPR:
287         return mark_addressable (TREE_OPERAND (x, 1))
288           & mark_addressable (TREE_OPERAND (x, 2));
289
290       case CONSTRUCTOR:
291         TREE_ADDRESSABLE (x) = 1;
292         return 1;
293
294       case INDIRECT_REF:
295         /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
296            incompatibility problems.  Handle this case by marking FOO.  */
297         if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
298             && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
299           {
300             x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
301             break;
302           }
303         if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
304           {
305             x = TREE_OPERAND (x, 0);
306             break;
307           }
308         return 1;
309
310       case VAR_DECL:
311       case CONST_DECL:
312       case PARM_DECL:
313       case RESULT_DECL:
314       case FUNCTION_DECL:
315         TREE_ADDRESSABLE (x) = 1;
316 #if 0  /* poplevel deals with this now.  */
317         if (DECL_CONTEXT (x) == 0)
318           TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
319 #endif
320         /* drops through */
321       default:
322         return 1;
323     }
324 }
325
326 /* Thorough checking of the arrayness of TYPE.  */
327
328 int
329 is_array_type_p (type)
330      tree type;
331 {
332   return TREE_CODE (type) == POINTER_TYPE
333     && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
334     && TYPE_ARRAY_P (TREE_TYPE (type));
335 }
336
337 /* Return the length of a Java array type.
338    Return -1 if the length is unknown or non-constant. */
339
340 HOST_WIDE_INT
341 java_array_type_length (array_type)
342      tree array_type;
343 {
344   tree arfld;
345   if (TREE_CODE (array_type) == POINTER_TYPE)
346     array_type = TREE_TYPE (array_type);
347   arfld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
348   if (arfld != NULL_TREE)
349     {
350       tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
351       tree high = TYPE_MAX_VALUE (index_type);
352       if (TREE_CODE (high) == INTEGER_CST)
353         return TREE_INT_CST_LOW (high) + 1;
354     }
355   return -1;
356 }
357
358 tree
359 build_prim_array_type (element_type, length)
360      tree element_type;
361      HOST_WIDE_INT length;
362 {
363   tree max_index = build_int_2 (length - 1, (0 == length ? -1 : 0));
364   TREE_TYPE (max_index) = sizetype;
365   return build_array_type (element_type, build_index_type (max_index));
366 }
367
368 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
369    These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
370    The LENGTH is -1 if the length is unknown. */
371
372 tree
373 build_java_array_type (element_type, length)
374      tree element_type;
375      HOST_WIDE_INT length;
376 {
377   tree sig, t, fld;
378   char buf[12];
379   tree elsig = build_java_signature (element_type);
380   tree el_name = element_type;
381   buf[0] = '[';
382   if (length >= 0)
383     sprintf (buf+1, HOST_WIDE_INT_PRINT_DEC, length);
384   else
385     buf[1] = '\0';
386   sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
387                      buf, 0, 0, "");
388   t = IDENTIFIER_SIGNATURE_TYPE (sig);
389   if (t != NULL_TREE)
390     return TREE_TYPE (t);
391   t = make_class ();
392   IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
393   TYPE_ARRAY_P (t) = 1;
394
395   if (TREE_CODE (el_name) == POINTER_TYPE)
396     el_name = TREE_TYPE (el_name);
397   el_name = TYPE_NAME (el_name);
398   if (TREE_CODE (el_name) == TYPE_DECL)
399     el_name = DECL_NAME (el_name);
400   TYPE_NAME (t) = identifier_subst (el_name, "", '.', '.', "[]");
401
402   set_java_signature (t, sig);
403   set_super_info (0, t, object_type_node, 0);
404   if (TREE_CODE (element_type) == RECORD_TYPE)
405     element_type = promote_type (element_type);
406   TYPE_ARRAY_ELEMENT (t) = element_type;
407
408   /* Add length pseudo-field. */
409   push_obstacks (&permanent_obstack, &permanent_obstack);
410   fld = build_decl (FIELD_DECL, get_identifier ("length"), int_type_node);
411   TYPE_FIELDS (t) = fld;
412   DECL_CONTEXT (fld) = t;
413   FIELD_PUBLIC (fld) = 1;
414   FIELD_FINAL (fld) = 1;
415
416   if (length >= 0)
417     {
418       tree atype = build_prim_array_type (element_type, length);
419       tree arfld = build_decl (FIELD_DECL, get_identifier ("data"), atype);
420       
421       DECL_CONTEXT (arfld) = t;
422       TREE_CHAIN (fld) = arfld;
423
424       /* We need to force the data field to begin at an alignment at
425        least equal to the biggest alignment in an object type node
426        in order to be compatible with the way that JArray is defined
427        in CNI.  However, we can't exceed BIGGEST_FIELD_ALIGNMENT. */
428       {
429       unsigned desired_align = TYPE_ALIGN (object_type_node);
430       desired_align = MAX (desired_align, TYPE_ALIGN (element_type));
431 #ifdef BIGGEST_FIELD_ALIGNMENT
432       desired_align = MIN (desired_align, 
433                            (unsigned) BIGGEST_FIELD_ALIGNMENT);
434 #endif
435 #ifdef ADJUST_FIELD_ALIGN
436       desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
437 #endif
438       DECL_ALIGN (arfld) = desired_align;
439       }
440     }
441   else
442     {
443       unsigned desired_align = TYPE_ALIGN (element_type);
444 #ifdef BIGGEST_FIELD_ALIGNMENT
445       desired_align = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
446 #endif
447       TYPE_ALIGN (t) = desired_align;
448     }
449   pop_obstacks ();
450
451   /* We could layout_class, but that loads java.lang.Object prematurely.
452    * This is called by the parser, and it is a bad idea to do load_class
453    * in the middle of parsing, because of possible circularity problems. */
454   push_super_field (t, object_type_node);
455   layout_type (t);
456
457   return t;
458 }
459
460 /* Promote TYPE to the type actually used for fields and parameters. */
461
462 tree
463 promote_type (type)
464      tree type;
465 {
466   switch (TREE_CODE (type))
467     {
468     case RECORD_TYPE:
469       return build_pointer_type (CLASS_TO_HANDLE_TYPE (type));
470     case BOOLEAN_TYPE:
471       if (type == boolean_type_node)
472         return promoted_boolean_type_node;
473       goto handle_int;
474     case CHAR_TYPE:
475       if (type == char_type_node)
476         return promoted_char_type_node;
477       goto handle_int;
478     case INTEGER_TYPE:
479     handle_int:
480       if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
481         {
482           if (type == short_type_node)
483             return promoted_short_type_node;
484           if (type == byte_type_node)
485             return promoted_byte_type_node;
486           return int_type_node;
487         }
488       /* ... else fall through ... */
489     default:
490       return type;
491     }
492 }
493
494 /* Parse a signature string, starting at *PTR and ending at LIMIT.
495    Return the seen TREE_TYPE, updating *PTR. */
496
497 static tree
498 parse_signature_type (ptr, limit)
499      const unsigned char **ptr, *limit;
500 {
501   tree type;
502   if ((*ptr) >= limit)
503     fatal ("bad signature string");
504   switch (*(*ptr))
505     {
506     case 'B':  (*ptr)++;  return byte_type_node;
507     case 'C':  (*ptr)++;  return char_type_node;
508     case 'D':  (*ptr)++;  return double_type_node;
509     case 'F':  (*ptr)++;  return float_type_node;
510     case 'S':  (*ptr)++;  return short_type_node;
511     case 'I':  (*ptr)++;  return int_type_node;
512     case 'J':  (*ptr)++;  return long_type_node;
513     case 'Z':  (*ptr)++;  return boolean_type_node;
514     case 'V':  (*ptr)++;  return void_type_node;
515     case '[':
516       for ((*ptr)++; (*ptr) < limit && ISDIGIT (**ptr); ) (*ptr)++;
517       type = parse_signature_type (ptr, limit);
518       type = build_java_array_type (type, -1); 
519       break;
520     case 'L':
521       {
522         const unsigned char *start = ++(*ptr);
523         register const unsigned char *str = start;
524         for ( ; ; str++)
525           {
526             if (str >= limit)
527               fatal ("bad signature string");
528             if (*str == ';')
529               break;
530           }
531         *ptr = str+1;
532         type = lookup_class (unmangle_classname (start, str - start));
533         break;
534       }
535     default:
536       fatal ("unrecognized signature string");
537     }
538   return promote_type (type);
539 }
540
541 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
542    and SIG_LENGTH bytes long.
543    Return a gcc type node. */
544
545 tree
546 parse_signature_string (sig_string, sig_length)
547      const unsigned char *sig_string;
548      int sig_length;
549 {
550   tree result_type;
551   const unsigned char *str = sig_string;
552   const unsigned char *limit = str + sig_length;
553
554   push_obstacks (&permanent_obstack, &permanent_obstack);
555   if (str < limit && str[0] == '(')
556     {
557       tree argtype_list = NULL_TREE;
558       str++;
559       while (str < limit && str[0] != ')')
560         {
561           tree argtype = parse_signature_type (&str, limit);
562           argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
563         }
564       if (str++, str >= limit)
565         fatal ("bad signature string");
566       result_type = parse_signature_type (&str, limit);
567       argtype_list = chainon (nreverse (argtype_list), end_params_node);
568       result_type = build_function_type (result_type, argtype_list);
569     }
570   else
571     result_type = parse_signature_type (&str, limit);
572   if (str != limit)
573     error ("junk at end of signature string");
574   pop_obstacks ();
575   return result_type;
576 }
577
578 /* Convert a signature to its type.
579  * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
580  */
581
582 tree
583 get_type_from_signature (tree signature)
584 {
585   const unsigned char *sig = (const unsigned char *) IDENTIFIER_POINTER (signature);
586   int len = IDENTIFIER_LENGTH (signature);
587   tree type;
588   /* Primitive types aren't cached. */
589   if (len <= 1)
590     return parse_signature_string (sig, len);
591   type = IDENTIFIER_SIGNATURE_TYPE (signature);
592   if (type == NULL_TREE)
593     {
594       type = parse_signature_string (sig, len);
595       IDENTIFIER_SIGNATURE_TYPE (signature) = type;
596     }
597   return type;
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   push_obstacks (&permanent_obstack, &permanent_obstack);
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           fatal ("internal error - build_java_signature passed invalid type");
704         }
705       TYPE_SIGNATURE (type) = sig;
706     }
707   pop_obstacks ();
708   return sig;
709 }
710
711 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
712
713 void
714 set_java_signature (type, sig)
715      tree type;
716      tree sig;
717 {
718   tree old_sig;
719   while (TREE_CODE (type) == POINTER_TYPE)
720     type = TREE_TYPE (type);
721   MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
722   old_sig = TYPE_SIGNATURE (type);
723   if (old_sig != NULL_TREE && old_sig != sig)
724     fatal ("internal error - set_java_signature");
725   TYPE_SIGNATURE (type) = sig;
726 #if 0 /* careful about METHOD_TYPE */
727   if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
728     IDENTIFIER_SIGNATURE_TYPE (sig) = type;
729 #endif
730 }
731
732 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
733    matching METHOD_NAME and signature SIGNATURE.  If SEARCHED_INTERFACE is
734    not NULL_TREE then first search its superinterfaces for a similar match.
735    Return the matched method DECL or NULL_TREE.  SIGNATURE_BUILDER is
736    used on method candidates to build their (sometimes partial)
737    signature.  */
738
739 tree
740 lookup_argument_method (searched_class, method_name, method_signature)
741      tree searched_class, method_name, method_signature;
742 {
743   return lookup_do (searched_class, NULL_TREE, method_name, method_signature, 
744                     build_java_argument_signature);
745 }
746
747 /* Search in class SEARCHED_CLASS (and its superclasses and
748    implemented interfaces) for a method matching METHOD_NAME and
749    argument signature METHOD_SIGNATURE.  Return a FUNCTION_DECL on
750    success, or NULL_TREE if none found.  (Contrast lookup_java_method,
751    which takes into account return type.) */
752
753 tree
754 lookup_argument_method2 (searched_class, method_name, method_signature)
755      tree searched_class, method_name, method_signature;
756 {
757   return lookup_do (CLASSTYPE_SUPER (searched_class), searched_class,
758                     method_name, method_signature, 
759                     build_java_argument_signature);
760 }
761
762 /* Search in class SEARCHED_CLASS (and its superclasses) for a method
763    matching METHOD_NAME and signature METHOD_SIGNATURE.  Return a
764    FUNCTION_DECL on success, or NULL_TREE if none found.  (Contrast
765    lookup_argument_method, which ignores return type.) If
766    SEARCHED_CLASS is an interface, search it too. */
767
768 tree
769 lookup_java_method (searched_class, method_name, method_signature)
770      tree searched_class, method_name, method_signature;
771 {
772   tree searched_interface;
773   
774   /* If this class is an interface class, search its superinterfaces
775    * first. A superinterface is not an interface's superclass: a super
776    * interface is implemented by the interface.  */
777
778   searched_interface = (CLASS_INTERFACE (TYPE_NAME (searched_class)) ?
779                         searched_class : NULL_TREE);
780   return lookup_do (searched_class, searched_interface, method_name, 
781                     method_signature, build_java_signature);
782 }
783
784 /* Search in class SEARCHED_CLASS (an its superclasses) for a method
785    matching METHOD_NAME and signature SIGNATURE.  Also search in
786    SEARCHED_INTERFACE (an its superinterfaces) for a similar match.
787    Return the matched method DECL or NULL_TREE.  SIGNATURE_BUILDER is
788    used on method candidates to build their (sometimes partial)
789    signature.  */
790
791 static tree
792 lookup_do (searched_class, searched_interface, method_name, signature, signature_builder)
793      tree searched_class, searched_interface, method_name, signature;
794      tree (*signature_builder) PARAMS ((tree));
795 {
796   tree method;
797   
798   if (searched_interface)
799     {
800       int i;
801       int interface_len = 
802         TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (searched_interface)) - 1;
803
804       for (i = interface_len; i > 0; i--)
805        {
806          tree child = 
807            TREE_VEC_ELT (TYPE_BINFO_BASETYPES (searched_interface), i);
808          tree iclass = BINFO_TYPE (child);
809
810          /* If the superinterface hasn't been loaded yet, do so now.  */
811          if (CLASS_FROM_SOURCE_P (iclass))
812            safe_layout_class (iclass);
813          else if (!CLASS_LOADED_P (iclass))
814            load_class (iclass, 1);
815
816          for (method = TYPE_METHODS (iclass);
817               method != NULL_TREE;  method = TREE_CHAIN (method))
818            {
819              tree method_sig = (*signature_builder) (TREE_TYPE (method));
820              tree name = DECL_NAME (method);
821
822              if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
823                   EXPR_WFL_NODE (name) : name) == method_name
824                  && method_sig == signature)
825                return method;
826            }
827
828          /* it could be defined in a supersuperinterface */
829          if (CLASS_INTERFACE (TYPE_NAME (iclass)))
830            {
831              method = lookup_do (iclass, iclass, method_name, 
832                                  signature, signature_builder);
833              if (method != NULL_TREE) 
834                return method;
835            }
836        }
837     }
838
839   while (searched_class != NULL_TREE)
840     {
841       for (method = TYPE_METHODS (searched_class);
842            method != NULL_TREE;  method = TREE_CHAIN (method))
843         {
844           tree method_sig = (*signature_builder) (TREE_TYPE (method));
845           tree name = DECL_NAME (method);
846
847           if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
848                EXPR_WFL_NODE (name) : name) == method_name
849               && method_sig == signature)
850             return method;
851         }
852       searched_class = CLASSTYPE_SUPER (searched_class);
853     }
854
855   return NULL_TREE;
856 }
857
858 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
859    Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
860
861 tree
862 lookup_java_constructor (clas, method_signature)
863      tree clas, method_signature;
864 {
865   tree method = TYPE_METHODS (clas);
866   for ( ; method != NULL_TREE;  method = TREE_CHAIN (method))
867     {
868       tree method_sig = build_java_signature (TREE_TYPE (method));
869       if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
870         return method;
871     }
872   return NULL_TREE;
873 }
874
875 /* Return a type which is the Binary Numeric Promotion of the pair T1,
876    T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
877    Promotion. It assumes that both T1 and T2 are elligible to BNP. */
878
879 tree
880 binary_numeric_promotion (t1, t2, exp1, exp2)
881      tree t1;
882      tree t2;
883      tree *exp1;
884      tree *exp2;
885 {
886   if (t1 == double_type_node || t2 == double_type_node)
887     {
888       if (t1 != double_type_node)
889         *exp1 = convert (double_type_node, *exp1);
890       if (t2 != double_type_node)
891         *exp2 = convert (double_type_node, *exp2);
892       return double_type_node;
893     }
894   if (t1 == float_type_node || t2 == float_type_node)
895     {
896       if (t1 != float_type_node)
897         *exp1 = convert (float_type_node, *exp1);
898       if (t2 != float_type_node)
899         *exp2 = convert (float_type_node, *exp2);
900       return float_type_node;
901     }
902   if (t1 == long_type_node || t2 == long_type_node)
903     {
904       if (t1 != long_type_node)
905         *exp1 = convert (long_type_node, *exp1);
906       if (t2 != long_type_node)
907         *exp2 = convert (long_type_node, *exp2);
908       return long_type_node;
909     }
910
911   if (t1 != int_type_node)
912     *exp1 = convert (int_type_node, *exp1);
913   if (t2 != int_type_node)
914     *exp2 = convert (int_type_node, *exp2);
915   return int_type_node;
916 }