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