OSDN Git Service

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