* optimize.c (initialize_inlined_parameters): Take FN to which the
[official-gcc.git] / gcc / java / typeck.c
blob331de1c9fb61be282dac7b9898c69f87bb7a90ed
1 /* Handle types for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 97-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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.
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. */
25 /* Written by Per Bothner <bothner@cygnus.com> */
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"
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 *));
41 tree * type_map;
42 extern struct obstack permanent_obstack;
44 /* Set the type of the local variable with index SLOT to TYPE. */
46 void
47 set_local_type (slot, type)
48 int slot;
49 tree type;
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;
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:
66 ((expr >= (float)MAX_INT)
67 ? MAX_INT
68 : ((expr <= (float)MIN_INT)
69 ? MIN_INT
70 : ((expr != expr)
71 ? 0
72 : (int)expr))) */
74 static tree
75 convert_ieee_real_to_integer (type, expr)
76 tree type, expr;
78 tree result;
79 expr = save_expr (expr);
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));
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);
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);
98 return result;
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. */
107 tree
108 convert (type, expr)
109 tree type, expr;
111 register enum tree_code code = TREE_CODE (type);
113 if (do_not_fold)
114 return build1 (NOP_EXPR, type, expr);
116 if (type == TREE_TYPE (expr)
117 || TREE_CODE (expr) == ERROR_MARK)
118 return expr;
119 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
120 return error_mark_node;
121 if (code == VOID_TYPE)
122 return build1 (CONVERT_EXPR, type, expr);
123 if (code == BOOLEAN_TYPE)
124 return fold (convert_to_boolean (type, expr));
125 if (code == INTEGER_TYPE)
127 if (! flag_fast_math
128 && ! flag_emit_class_files
129 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
130 && TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
131 return fold (convert_ieee_real_to_integer (type, expr));
132 else
133 return fold (convert_to_integer (type, expr));
135 if (code == REAL_TYPE)
136 return fold (convert_to_real (type, expr));
137 if (code == CHAR_TYPE)
138 return fold (convert_to_char (type, expr));
139 if (code == POINTER_TYPE)
140 return fold (convert_to_pointer (type, expr));
141 error ("conversion to non-scalar type requested");
142 return error_mark_node;
146 tree
147 convert_to_char (type, expr)
148 tree type, expr;
150 return build1 (NOP_EXPR, type, expr);
153 tree
154 convert_to_boolean (type, expr)
155 tree type, expr;
157 return build1 (NOP_EXPR, type, expr);
160 /* Print an error message for invalid use of an incomplete type.
161 VALUE is the expression that was used (or 0 if that isn't known)
162 and TYPE is the type that was invalid. */
164 void
165 incomplete_type_error (value, type)
166 tree value ATTRIBUTE_UNUSED;
167 tree type ATTRIBUTE_UNUSED;
169 error ("internal error - use of undefined type");
172 /* Return a data type that has machine mode MODE.
173 If the mode is an integer,
174 then UNSIGNEDP selects between signed and unsigned types. */
176 tree
177 type_for_mode (mode, unsignedp)
178 enum machine_mode mode;
179 int unsignedp;
181 if (mode == TYPE_MODE (int_type_node))
182 return unsignedp ? unsigned_int_type_node : int_type_node;
183 if (mode == TYPE_MODE (long_type_node))
184 return unsignedp ? unsigned_long_type_node : long_type_node;
185 if (mode == TYPE_MODE (short_type_node))
186 return unsignedp ? unsigned_short_type_node : short_type_node;
187 if (mode == TYPE_MODE (byte_type_node))
188 return unsignedp ? unsigned_byte_type_node : byte_type_node;
189 if (mode == TYPE_MODE (float_type_node))
190 return float_type_node;
191 if (mode == TYPE_MODE (double_type_node))
192 return double_type_node;
194 return 0;
197 /* Return an integer type with BITS bits of precision,
198 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
200 tree
201 type_for_size (bits, unsignedp)
202 unsigned bits;
203 int unsignedp;
205 if (bits <= TYPE_PRECISION (byte_type_node))
206 return unsignedp ? unsigned_byte_type_node : byte_type_node;
207 if (bits <= TYPE_PRECISION (short_type_node))
208 return unsignedp ? unsigned_short_type_node : short_type_node;
209 if (bits <= TYPE_PRECISION (int_type_node))
210 return unsignedp ? unsigned_int_type_node : int_type_node;
211 if (bits <= TYPE_PRECISION (long_type_node))
212 return unsignedp ? unsigned_long_type_node : long_type_node;
213 return 0;
216 /* Return a type the same as TYPE except unsigned or
217 signed according to UNSIGNEDP. */
219 tree
220 signed_or_unsigned_type (unsignedp, type)
221 int unsignedp;
222 tree type;
224 if (! INTEGRAL_TYPE_P (type))
225 return type;
226 if (TYPE_PRECISION (type) == TYPE_PRECISION (int_type_node))
227 return unsignedp ? unsigned_int_type_node : int_type_node;
228 if (TYPE_PRECISION (type) == TYPE_PRECISION (byte_type_node))
229 return unsignedp ? unsigned_byte_type_node : byte_type_node;
230 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_type_node))
231 return unsignedp ? unsigned_short_type_node : short_type_node;
232 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_type_node))
233 return unsignedp ? unsigned_long_type_node : long_type_node;
234 return type;
237 /* Return a signed type the same as TYPE in other respects. */
239 tree
240 signed_type (type)
241 tree type;
243 return signed_or_unsigned_type (0, type);
246 /* Return an unsigned type the same as TYPE in other respects. */
248 tree
249 unsigned_type (type)
250 tree type;
252 return signed_or_unsigned_type (1, type);
256 /* Mark EXP saying that we need to be able to take the
257 address of it; it should not be allocated in a register.
258 Value is 1 if successful. */
261 mark_addressable (exp)
262 tree exp;
264 register tree x = exp;
265 while (1)
266 switch (TREE_CODE (x))
268 case ADDR_EXPR:
269 case COMPONENT_REF:
270 case ARRAY_REF:
271 case REALPART_EXPR:
272 case IMAGPART_EXPR:
273 x = TREE_OPERAND (x, 0);
274 break;
276 case TRUTH_ANDIF_EXPR:
277 case TRUTH_ORIF_EXPR:
278 case COMPOUND_EXPR:
279 x = TREE_OPERAND (x, 1);
280 break;
282 case COND_EXPR:
283 return mark_addressable (TREE_OPERAND (x, 1))
284 & mark_addressable (TREE_OPERAND (x, 2));
286 case CONSTRUCTOR:
287 TREE_ADDRESSABLE (x) = 1;
288 return 1;
290 case INDIRECT_REF:
291 /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
292 incompatibility problems. Handle this case by marking FOO. */
293 if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
294 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
296 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
297 break;
299 if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
301 x = TREE_OPERAND (x, 0);
302 break;
304 return 1;
306 case VAR_DECL:
307 case CONST_DECL:
308 case PARM_DECL:
309 case RESULT_DECL:
310 case FUNCTION_DECL:
311 TREE_ADDRESSABLE (x) = 1;
312 #if 0 /* poplevel deals with this now. */
313 if (DECL_CONTEXT (x) == 0)
314 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
315 #endif
316 /* drops through */
317 default:
318 return 1;
322 /* Thorough checking of the arrayness of TYPE. */
325 is_array_type_p (type)
326 tree type;
328 return TREE_CODE (type) == POINTER_TYPE
329 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
330 && TYPE_ARRAY_P (TREE_TYPE (type));
333 /* Return the length of a Java array type.
334 Return -1 if the length is unknown or non-constant. */
336 HOST_WIDE_INT
337 java_array_type_length (array_type)
338 tree array_type;
340 tree arfld;
341 if (TREE_CODE (array_type) == POINTER_TYPE)
342 array_type = TREE_TYPE (array_type);
343 arfld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
344 if (arfld != NULL_TREE)
346 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
347 tree high = TYPE_MAX_VALUE (index_type);
348 if (TREE_CODE (high) == INTEGER_CST)
349 return TREE_INT_CST_LOW (high) + 1;
351 return -1;
354 tree
355 build_prim_array_type (element_type, length)
356 tree element_type;
357 HOST_WIDE_INT length;
359 tree max_index = build_int_2 (length - 1, 0);
360 TREE_TYPE (max_index) = sizetype;
361 return build_array_type (element_type, build_index_type (max_index));
364 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
365 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
366 The LENGTH is -1 if the length is unknown. */
368 tree
369 build_java_array_type (element_type, length)
370 tree element_type;
371 HOST_WIDE_INT length;
373 tree sig, t, fld;
374 char buf[12];
375 tree elsig = build_java_signature (element_type);
376 tree el_name = element_type;
377 buf[0] = '[';
378 if (length >= 0)
379 sprintf (buf+1, HOST_WIDE_INT_PRINT_DEC, length);
380 else
381 buf[1] = '\0';
382 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
383 buf, 0, 0, "");
384 t = IDENTIFIER_SIGNATURE_TYPE (sig);
385 if (t != NULL_TREE)
386 return TREE_TYPE (t);
387 t = make_class ();
388 IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
389 TYPE_ARRAY_P (t) = 1;
391 if (TREE_CODE (el_name) == POINTER_TYPE)
392 el_name = TREE_TYPE (el_name);
393 el_name = TYPE_NAME (el_name);
394 if (TREE_CODE (el_name) == TYPE_DECL)
395 el_name = DECL_NAME (el_name);
396 TYPE_NAME (t) = identifier_subst (el_name, "", '.', '.', "[]");
398 set_java_signature (t, sig);
399 set_super_info (0, t, object_type_node, 0);
400 if (TREE_CODE (element_type) == RECORD_TYPE)
401 element_type = promote_type (element_type);
402 TYPE_ARRAY_ELEMENT (t) = element_type;
404 /* Add length pseudo-field. */
405 push_obstacks (&permanent_obstack, &permanent_obstack);
406 fld = build_decl (FIELD_DECL, get_identifier ("length"), int_type_node);
407 TYPE_FIELDS (t) = fld;
408 DECL_CONTEXT (fld) = t;
409 FIELD_PUBLIC (fld) = 1;
410 FIELD_FINAL (fld) = 1;
412 if (length >= 0)
414 tree atype = build_prim_array_type (element_type, length);
415 tree arfld = build_decl (FIELD_DECL, get_identifier ("data"), atype);
416 DECL_CONTEXT (arfld) = t;
417 TREE_CHAIN (fld) = arfld;
419 else
420 TYPE_ALIGN (t) = TYPE_ALIGN (element_type);
421 pop_obstacks ();
423 /* We could layout_class, but that loads java.lang.Object prematurely.
424 * This is called by the parser, and it is a bad idea to do load_class
425 * in the middle of parsing, because of possible circularity problems. */
426 push_super_field (t, object_type_node);
427 layout_type (t);
429 return t;
432 /* Promote TYPE to the type actually used for fields and parameters. */
434 tree
435 promote_type (type)
436 tree type;
438 switch (TREE_CODE (type))
440 case RECORD_TYPE:
441 return build_pointer_type (CLASS_TO_HANDLE_TYPE (type));
442 case BOOLEAN_TYPE:
443 if (type == boolean_type_node)
444 return promoted_boolean_type_node;
445 goto handle_int;
446 case CHAR_TYPE:
447 if (type == char_type_node)
448 return promoted_char_type_node;
449 goto handle_int;
450 case INTEGER_TYPE:
451 handle_int:
452 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
454 if (type == short_type_node)
455 return promoted_short_type_node;
456 if (type == byte_type_node)
457 return promoted_byte_type_node;
458 return int_type_node;
460 /* ... else fall through ... */
461 default:
462 return type;
466 /* Parse a signature string, starting at *PTR and ending at LIMIT.
467 Return the seen TREE_TYPE, updating *PTR. */
469 static tree
470 parse_signature_type (ptr, limit)
471 const unsigned char **ptr, *limit;
473 tree type;
474 if ((*ptr) >= limit)
475 fatal ("bad signature string");
476 switch (*(*ptr))
478 case 'B': (*ptr)++; return byte_type_node;
479 case 'C': (*ptr)++; return char_type_node;
480 case 'D': (*ptr)++; return double_type_node;
481 case 'F': (*ptr)++; return float_type_node;
482 case 'S': (*ptr)++; return short_type_node;
483 case 'I': (*ptr)++; return int_type_node;
484 case 'J': (*ptr)++; return long_type_node;
485 case 'Z': (*ptr)++; return boolean_type_node;
486 case 'V': (*ptr)++; return void_type_node;
487 case '[':
488 for ((*ptr)++; (*ptr) < limit && ISDIGIT (**ptr); ) (*ptr)++;
489 type = parse_signature_type (ptr, limit);
490 type = build_java_array_type (type, -1);
491 break;
492 case 'L':
494 const unsigned char *start = ++(*ptr);
495 register const unsigned char *str = start;
496 for ( ; ; str++)
498 if (str >= limit)
499 fatal ("bad signature string");
500 if (*str == ';')
501 break;
503 *ptr = str+1;
504 type = lookup_class (unmangle_classname (start, str - start));
505 break;
507 default:
508 fatal ("unrecognized signature string");
510 return promote_type (type);
513 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
514 and SIG_LENGTH bytes long.
515 Return a gcc type node. */
517 tree
518 parse_signature_string (sig_string, sig_length)
519 const unsigned char *sig_string;
520 int sig_length;
522 tree result_type;
523 const unsigned char *str = sig_string;
524 const unsigned char *limit = str + sig_length;
526 push_obstacks (&permanent_obstack, &permanent_obstack);
527 if (str < limit && str[0] == '(')
529 tree argtype_list = NULL_TREE;
530 str++;
531 while (str < limit && str[0] != ')')
533 tree argtype = parse_signature_type (&str, limit);
534 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
536 if (str++, str >= limit)
537 fatal ("bad signature string");
538 result_type = parse_signature_type (&str, limit);
539 argtype_list = chainon (nreverse (argtype_list), end_params_node);
540 result_type = build_function_type (result_type, argtype_list);
542 else
543 result_type = parse_signature_type (&str, limit);
544 if (str != limit)
545 error ("junk at end of signature string");
546 pop_obstacks ();
547 return result_type;
550 /* Convert a signature to its type.
551 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
554 tree
555 get_type_from_signature (tree signature)
557 const unsigned char *sig = (const unsigned char *) IDENTIFIER_POINTER (signature);
558 int len = IDENTIFIER_LENGTH (signature);
559 tree type;
560 /* Primitive types aren't cached. */
561 if (len <= 1)
562 return parse_signature_string (sig, len);
563 type = IDENTIFIER_SIGNATURE_TYPE (signature);
564 if (type == NULL_TREE)
566 type = parse_signature_string (sig, len);
567 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
569 return type;
572 /* Return the signature string for the arguments of method type TYPE. */
574 tree
575 build_java_argument_signature (type)
576 tree type;
578 extern struct obstack temporary_obstack;
579 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
580 if (sig == NULL_TREE)
582 tree args = TYPE_ARG_TYPES (type);
583 if (TREE_CODE (type) == METHOD_TYPE)
584 args = TREE_CHAIN (args); /* Skip "this" argument. */
585 for (; args != end_params_node; args = TREE_CHAIN (args))
587 tree t = build_java_signature (TREE_VALUE (args));
588 obstack_grow (&temporary_obstack,
589 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
591 obstack_1grow (&temporary_obstack, '\0');
593 sig = get_identifier (obstack_base (&temporary_obstack));
594 TYPE_ARGUMENT_SIGNATURE (type) = sig;
595 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
597 return sig;
600 /* Return the signature of the given TYPE. */
602 tree
603 build_java_signature (type)
604 tree type;
606 tree sig, t;
607 push_obstacks (&permanent_obstack, &permanent_obstack);
608 while (TREE_CODE (type) == POINTER_TYPE)
609 type = TREE_TYPE (type);
610 if (TYPE_LANG_SPECIFIC (type) == NULL)
612 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
613 perm_calloc (1, sizeof (struct lang_type));
615 sig = TYPE_LANG_SPECIFIC (type)->signature;
616 if (sig == NULL_TREE)
618 char sg[2];
619 switch (TREE_CODE (type))
621 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
622 case CHAR_TYPE: sg[0] = 'C'; goto native;
623 case VOID_TYPE: sg[0] = 'V'; goto native;
624 case INTEGER_TYPE:
625 switch (TYPE_PRECISION (type))
627 case 8: sg[0] = 'B'; goto native;
628 case 16: sg[0] = 'S'; goto native;
629 case 32: sg[0] = 'I'; goto native;
630 case 64: sg[0] = 'J'; goto native;
631 default: goto bad_type;
633 case REAL_TYPE:
634 switch (TYPE_PRECISION (type))
636 case 32: sg[0] = 'F'; goto native;
637 case 64: sg[0] = 'D'; goto native;
638 default: goto bad_type;
640 native:
641 sg[1] = 0;
642 sig = get_identifier (sg);
643 break;
644 case RECORD_TYPE:
645 if (TYPE_ARRAY_P (type))
647 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
648 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
649 "[", 0, 0, "");
651 else
653 t = DECL_NAME (TYPE_NAME (type));
654 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
655 "L", '.', '/', ";");
657 break;
658 case METHOD_TYPE:
659 case FUNCTION_TYPE:
661 extern struct obstack temporary_obstack;
662 sig = build_java_argument_signature (type);
663 obstack_1grow (&temporary_obstack, '(');
664 obstack_grow (&temporary_obstack,
665 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
666 obstack_1grow (&temporary_obstack, ')');
668 t = build_java_signature (TREE_TYPE (type));
669 obstack_grow0 (&temporary_obstack,
670 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
672 sig = get_identifier (obstack_base (&temporary_obstack));
673 obstack_free (&temporary_obstack,
674 obstack_base (&temporary_obstack));
676 break;
677 bad_type:
678 default:
679 fatal ("internal error - build_java_signature passed invalid type");
681 TYPE_LANG_SPECIFIC (type)->signature = sig;
683 pop_obstacks ();
684 return sig;
687 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
689 void
690 set_java_signature (type, sig)
691 tree type;
692 tree sig;
694 tree old_sig;
695 while (TREE_CODE (type) == POINTER_TYPE)
696 type = TREE_TYPE (type);
697 if (TYPE_LANG_SPECIFIC (type) == NULL)
699 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
700 perm_calloc (1, sizeof (struct lang_type));
703 old_sig = TYPE_LANG_SPECIFIC (type)->signature;
704 if (old_sig != NULL_TREE && old_sig != sig)
705 fatal ("internal error - set_java_signature");
706 TYPE_LANG_SPECIFIC (type)->signature = sig;
707 #if 0 /* careful about METHOD_TYPE */
708 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
709 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
710 #endif
713 /* Search in class CLAS (and its superclasses) for a method
714 matching METHOD_NAME and argument signature METHOD_SIGNATURE.
715 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
716 (Contrast lookup_java_method, which takes into account return type.) */
718 tree
719 lookup_argument_method (clas, method_name, method_signature)
720 tree clas, method_name, method_signature;
722 tree method;
723 while (clas != NULL_TREE)
725 for (method = TYPE_METHODS (clas);
726 method != NULL_TREE; method = TREE_CHAIN (method))
728 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
729 tree name = DECL_NAME (method);
730 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
731 EXPR_WFL_NODE (name) : name) == method_name
732 && method_sig == method_signature)
733 return method;
735 clas = CLASSTYPE_SUPER (clas);
737 return NULL_TREE;
740 /* Search in class CLAS (and its superclasses) for a method
741 matching METHOD_NAME and signature METHOD_SIGNATURE.
742 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
743 (Contrast lookup_argument_method, which ignores return type.) */
745 tree
746 lookup_java_method (searched_class, method_name, method_signature)
747 tree searched_class, method_name, method_signature;
749 tree method;
750 tree currently_searched = searched_class;
752 while (currently_searched != NULL_TREE)
754 for (method = TYPE_METHODS (currently_searched);
755 method != NULL_TREE; method = TREE_CHAIN (method))
757 tree method_sig = build_java_signature (TREE_TYPE (method));
758 if (DECL_NAME (method) == method_name
759 && method_sig == method_signature)
760 return method;
762 currently_searched = CLASSTYPE_SUPER (currently_searched);
765 /* If this class is an interface class, search its superinterfaces as
766 * well. A superinterface is not an interface's superclass: a
767 * super interface is implemented by the interface.
770 currently_searched = searched_class;
771 if (CLASS_INTERFACE (TYPE_NAME (currently_searched)))
773 int i;
774 int interface_len =
775 TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (currently_searched)) - 1;
777 for (i = interface_len; i > 0; i--)
779 tree child =
780 TREE_VEC_ELT (TYPE_BINFO_BASETYPES (currently_searched), i);
781 tree iclass = BINFO_TYPE (child);
783 /* If the superinterface hasn't been loaded yet, do so now. */
784 if (! CLASS_LOADED_P (iclass))
785 load_class (iclass, 1);
787 for (method = TYPE_METHODS (iclass);
788 method != NULL_TREE; method = TREE_CHAIN (method))
790 tree method_sig = build_java_signature (TREE_TYPE (method));
792 if (DECL_NAME (method) == method_name
793 && method_sig == method_signature)
794 return method;
797 /* it could be defined in a supersuperinterface */
798 if (CLASS_INTERFACE (TYPE_NAME (iclass)))
800 method = lookup_java_method (iclass,
801 method_name,
802 method_signature);
803 if (method != NULL_TREE)
804 return method;
808 return NULL_TREE;
811 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
812 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
814 tree
815 lookup_java_constructor (clas, method_signature)
816 tree clas, method_signature;
818 tree method = TYPE_METHODS (clas);
819 for ( ; method != NULL_TREE; method = TREE_CHAIN (method))
821 tree method_sig = build_java_signature (TREE_TYPE (method));
822 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
823 return method;
825 return NULL_TREE;
828 /* Return a type which is the Binary Numeric Promotion of the pair T1,
829 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
830 Promotion. It assumes that both T1 and T2 are elligible to BNP. */
832 tree
833 binary_numeric_promotion (t1, t2, exp1, exp2)
834 tree t1;
835 tree t2;
836 tree *exp1;
837 tree *exp2;
839 if (t1 == double_type_node || t2 == double_type_node)
841 if (t1 != double_type_node)
842 *exp1 = convert (double_type_node, *exp1);
843 if (t2 != double_type_node)
844 *exp2 = convert (double_type_node, *exp2);
845 return double_type_node;
847 if (t1 == float_type_node || t2 == float_type_node)
849 if (t1 != float_type_node)
850 *exp1 = convert (float_type_node, *exp1);
851 if (t2 != float_type_node)
852 *exp2 = convert (float_type_node, *exp2);
853 return float_type_node;
855 if (t1 == long_type_node || t2 == long_type_node)
857 if (t1 != long_type_node)
858 *exp1 = convert (long_type_node, *exp1);
859 if (t2 != long_type_node)
860 *exp2 = convert (long_type_node, *exp2);
861 return long_type_node;
864 if (t1 != int_type_node)
865 *exp1 = convert (int_type_node, *exp1);
866 if (t2 != int_type_node)
867 *exp2 = convert (int_type_node, *exp2);
868 return int_type_node;