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