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