OSDN Git Service

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