1 /* Handle types for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996 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)
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> */
30 #include "java-tree.h"
35 extern struct obstack permanent_obstack;
37 /* Set the type of the local variable with index SLOT to TYPE. */
40 set_local_type (slot, type)
44 int max_locals = DECL_MAX_LOCALS(current_function_decl);
45 int nslots = TYPE_IS_WIDE (type) ? 2 : 1;
46 if (slot < 0 || slot + nslots - 1 >= max_locals)
47 fatal ("invalid local variable index");
48 type_map[slot] = type;
50 type_map[++slot] = void_type_node;
53 extern tree convert_to_integer (tree type, tree expr);
54 extern tree convert_to_real (tree type, tree expr);
55 extern tree convert_to_pointer (tree type, tree expr);
57 /* Create an expression whose value is that of EXPR,
58 converted to type TYPE. The TREE_TYPE of the value
59 is always TYPE. This function implements all reasonable
60 conversions; callers should filter out those that are
61 not permitted by the language being compiled. */
67 register enum tree_code code = TREE_CODE (type);
69 if (type == TREE_TYPE (expr)
70 || TREE_CODE (expr) == ERROR_MARK)
72 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
73 return error_mark_node;
74 if (code == BOOLEAN_TYPE)
75 return fold (convert_to_boolean (type, expr));
76 if (code == INTEGER_TYPE)
77 return fold (convert_to_integer (type, expr));
78 if (code == REAL_TYPE)
79 return fold (convert_to_real (type, expr));
80 if (code == CHAR_TYPE)
81 return fold (convert_to_char (type, expr));
82 if (code == POINTER_TYPE)
83 return fold (convert_to_pointer (type, expr));
84 error ("conversion to non-scalar type requested");
85 return error_mark_node;
89 convert_to_char (type, expr)
92 return build1 (NOP_EXPR, type, expr);
96 convert_to_boolean (type, expr)
99 return build1 (NOP_EXPR, type, expr);
102 /* Print an error message for invalid use of an incomplete type.
103 VALUE is the expression that was used (or 0 if that isn't known)
104 and TYPE is the type that was invalid. */
107 incomplete_type_error (value, type)
111 error ("internal error - use of undefined type");
114 /* Return a data type that has machine mode MODE.
115 If the mode is an integer,
116 then UNSIGNEDP selects between signed and unsigned types. */
119 type_for_mode (mode, unsignedp)
120 enum machine_mode mode;
123 if (mode == TYPE_MODE (int_type_node))
124 return unsignedp ? unsigned_int_type_node : int_type_node;
125 if (mode == TYPE_MODE (long_type_node))
126 return unsignedp ? unsigned_long_type_node : long_type_node;
127 if (mode == TYPE_MODE (short_type_node))
128 return unsignedp ? unsigned_short_type_node : short_type_node;
129 if (mode == TYPE_MODE (byte_type_node))
130 return unsignedp ? unsigned_byte_type_node : byte_type_node;
131 if (mode == TYPE_MODE (float_type_node))
132 return float_type_node;
133 if (mode == TYPE_MODE (double_type_node))
134 return double_type_node;
137 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
138 return build_pointer_type (char_type_node);
140 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
141 return build_pointer_type (integer_type_node);
147 /* Return an integer type with BITS bits of precision,
148 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
151 type_for_size (bits, unsignedp)
155 if (bits <= TYPE_PRECISION (byte_type_node))
156 return unsignedp ? unsigned_byte_type_node : byte_type_node;
157 if (bits <= TYPE_PRECISION (short_type_node))
158 return unsignedp ? unsigned_short_type_node : short_type_node;
159 if (bits <= TYPE_PRECISION (int_type_node))
160 return unsignedp ? unsigned_int_type_node : int_type_node;
161 if (bits <= TYPE_PRECISION (long_type_node))
162 return unsignedp ? unsigned_long_type_node : long_type_node;
166 /* Return a type the same as TYPE except unsigned or
167 signed according to UNSIGNEDP. */
170 signed_or_unsigned_type (unsignedp, type)
174 if (! INTEGRAL_TYPE_P (type))
176 if (TYPE_PRECISION (type) == TYPE_PRECISION (int_type_node))
177 return unsignedp ? unsigned_int_type_node : int_type_node;
178 if (TYPE_PRECISION (type) == TYPE_PRECISION (byte_type_node))
179 return unsignedp ? unsigned_byte_type_node : byte_type_node;
180 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_type_node))
181 return unsignedp ? unsigned_short_type_node : short_type_node;
182 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_type_node))
183 return unsignedp ? unsigned_long_type_node : long_type_node;
187 /* Return a signed type the same as TYPE in other respects. */
193 return signed_or_unsigned_type (0, type);
196 /* Return an unsigned type the same as TYPE in other respects. */
202 return signed_or_unsigned_type (1, type);
206 /* Mark EXP saying that we need to be able to take the
207 address of it; it should not be allocated in a register.
208 Value is 1 if successful. */
211 mark_addressable (exp)
214 register tree x = exp;
216 switch (TREE_CODE (x))
223 x = TREE_OPERAND (x, 0);
226 case TRUTH_ANDIF_EXPR:
227 case TRUTH_ORIF_EXPR:
229 x = TREE_OPERAND (x, 1);
233 return mark_addressable (TREE_OPERAND (x, 1))
234 & mark_addressable (TREE_OPERAND (x, 2));
237 TREE_ADDRESSABLE (x) = 1;
241 /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
242 incompatibility problems. Handle this case by marking FOO. */
243 if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
244 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
246 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
249 if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
251 x = TREE_OPERAND (x, 0);
261 TREE_ADDRESSABLE (x) = 1;
262 #if 0 /* poplevel deals with this now. */
263 if (DECL_CONTEXT (x) == 0)
264 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
272 /* Thorough checking of the arrayness of TYPE. */
275 is_array_type_p (type)
278 return TREE_CODE (type) == POINTER_TYPE
279 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
280 && TYPE_ARRAY_P (TREE_TYPE (type));
283 /* Return the length of a Java array type.
284 Return -1 if the length is unknown or non-constant. */
287 java_array_type_length (array_type)
291 if (TREE_CODE (array_type) == POINTER_TYPE)
292 array_type = TREE_TYPE (array_type);
293 arfld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
294 if (arfld != NULL_TREE)
296 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
297 tree high = TYPE_MAX_VALUE (index_type);
298 if (TREE_CODE (high) == INTEGER_CST)
299 return TREE_INT_CST_LOW (high) + 1;
305 build_prim_array_type (element_type, length)
307 HOST_WIDE_INT length;
309 tree max_index = build_int_2 (length - 1, 0);
310 TREE_TYPE (max_index) = sizetype;
311 return build_array_type (element_type, build_index_type (max_index));
314 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
315 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
316 The LENGTH is -1 if the length is unknown. */
319 build_java_array_type (element_type, length)
321 HOST_WIDE_INT length;
325 tree elsig = build_java_signature (element_type);
326 tree el_name = element_type;
327 sprintf (buf, length >= 0 ? "[%d" : "[", length);
328 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
330 t = IDENTIFIER_SIGNATURE_TYPE (sig);
334 IDENTIFIER_SIGNATURE_TYPE (sig) = t;
335 TYPE_ARRAY_P (t) = 1;
337 if (TREE_CODE (el_name) == POINTER_TYPE)
338 el_name = TREE_TYPE (el_name);
339 el_name = TYPE_NAME (el_name);
340 if (TREE_CODE (el_name) == TYPE_DECL)
341 el_name = DECL_NAME (el_name);
342 TYPE_NAME (t) = identifier_subst (el_name, "", '.', '.', "[]");
344 set_java_signature (t, sig);
345 set_super_info (0, t, object_type_node, 0);
346 TYPE_ARRAY_ELEMENT (t) = element_type;
348 /* Add length pseudo-field. */
349 push_obstacks (&permanent_obstack, &permanent_obstack);
350 fld = build_decl (FIELD_DECL, get_identifier ("length"), int_type_node);
351 TYPE_FIELDS (t) = fld;
352 DECL_CONTEXT (fld) = t;
353 FIELD_PUBLIC (fld) = 1;
354 FIELD_FINAL (fld) = 1;
358 tree atype = build_prim_array_type (element_type, length);
359 tree arfld = build_decl (FIELD_DECL, get_identifier ("data"), atype);
360 DECL_CONTEXT (arfld) = t;
361 TREE_CHAIN (fld) = arfld;
364 TYPE_ALIGN (t) = TYPE_ALIGN (element_type);
371 /* Promote TYPE to the type actually used for fields and parameters. */
377 switch (TREE_CODE (type))
380 return build_pointer_type (CLASS_TO_HANDLE_TYPE (type));
382 if (type == boolean_type_node)
383 return promoted_boolean_type_node;
386 if (type == char_type_node)
387 return promoted_char_type_node;
391 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
393 if (type == short_type_node)
394 return promoted_short_type_node;
395 if (type == byte_type_node)
396 return promoted_byte_type_node;
397 return int_type_node;
399 /* ... else fall through ... */
405 /* Parse a signature string, starting at *PTR and ending at LIMIT.
406 Return the seen TREE_TYPE, updating *PTR. */
409 parse_signature_type (ptr, limit)
410 const unsigned char **ptr, *limit;
413 fatal ("bad signature string");
416 case 'B': (*ptr)++; return byte_type_node;
417 case 'C': (*ptr)++; return char_type_node;
418 case 'D': (*ptr)++; return double_type_node;
419 case 'F': (*ptr)++; return float_type_node;
420 case 'S': (*ptr)++; return short_type_node;
421 case 'I': (*ptr)++; return int_type_node;
422 case 'J': (*ptr)++; return long_type_node;
423 case 'Z': (*ptr)++; return boolean_type_node;
424 case 'V': (*ptr)++; return void_type_node;
426 for ((*ptr)++; (*ptr) < limit && isdigit (**ptr); ) (*ptr)++;
428 tree element_type = parse_signature_type (ptr, limit);
429 if (TREE_CODE (element_type) == RECORD_TYPE)
430 element_type = promote_type (element_type);
431 return build_java_array_type (element_type, -1);
435 const unsigned char *start = ++(*ptr);
436 register const unsigned char *str = start;
440 fatal ("bad signature string");
445 return lookup_class (unmangle_classname (start, str - start));
448 fatal ("unrecognized signature string");
452 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
453 and SIG_LENGTH bytes long.
454 Return a gcc type node. */
457 parse_signature_string (sig_string, sig_length)
458 const unsigned char *sig_string;
462 const unsigned char *str = sig_string;
463 const unsigned char *limit = str + sig_length;
465 push_obstacks (&permanent_obstack, &permanent_obstack);
466 if (str < limit && str[0] == '(')
468 tree argtype_list = NULL_TREE;
470 while (str < limit && str[0] != ')')
472 tree argtype = promote_type (parse_signature_type (&str, limit));
473 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
475 if (str++, str >= limit)
476 fatal ("bad signature string");
477 result_type = promote_type (parse_signature_type (&str, limit));
478 result_type = build_function_type (result_type,
479 nreverse (argtype_list));
482 result_type = parse_signature_type (&str, limit);
484 error ("junk at end of signature string");
489 /* Convert a signature to its type.
490 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
494 get_type_from_signature (tree signature)
496 unsigned char *sig = (unsigned char *) IDENTIFIER_POINTER (signature);
497 int len = IDENTIFIER_LENGTH (signature);
499 /* Primitive types aren't cached. */
501 return parse_signature_string (sig, len);
502 type = IDENTIFIER_SIGNATURE_TYPE (signature);
503 if (type == NULL_TREE)
505 type = parse_signature_string (sig, len);
506 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
511 /* Return the signature string for the arguments of method type TYPE. */
514 build_java_argument_signature (type)
517 extern struct obstack temporary_obstack;
518 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
519 if (sig == NULL_TREE)
521 tree args = TYPE_ARG_TYPES (type);
522 if (TREE_CODE (type) == METHOD_TYPE)
523 args = TREE_CHAIN (args); /* Skip "this" argument. */
524 for (; args != NULL_TREE; args = TREE_CHAIN (args))
526 tree t = build_java_signature (TREE_VALUE (args));
527 obstack_grow (&temporary_obstack,
528 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
530 obstack_1grow (&temporary_obstack, '\0');
532 sig = get_identifier (obstack_base (&temporary_obstack));
533 TYPE_ARGUMENT_SIGNATURE (type) = sig;
534 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
539 /* Return the signature of the given TYPE. */
542 build_java_signature (type)
546 push_obstacks (&permanent_obstack, &permanent_obstack);
547 while (TREE_CODE (type) == POINTER_TYPE)
548 type = TREE_TYPE (type);
549 if (TYPE_LANG_SPECIFIC (type) == NULL)
551 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
552 perm_calloc (1, sizeof (struct lang_type));
555 sig = TYPE_LANG_SPECIFIC (type)->signature;
556 if (sig == NULL_TREE)
559 switch (TREE_CODE (type))
561 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
562 case CHAR_TYPE: sg[0] = 'C'; goto native;
563 case VOID_TYPE: sg[0] = 'V'; goto native;
565 switch (TYPE_PRECISION (type))
567 case 8: sg[0] = 'B'; goto native;
568 case 16: sg[0] = 'S'; goto native;
569 case 32: sg[0] = 'I'; goto native;
570 case 64: sg[0] = 'J'; goto native;
571 default: goto bad_type;
574 switch (TYPE_PRECISION (type))
576 case 32: sg[0] = 'F'; goto native;
577 case 64: sg[0] = 'D'; goto native;
578 default: goto bad_type;
582 sig = get_identifier (sg);
585 if (TYPE_ARRAY_P (type))
587 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
588 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
593 t = DECL_NAME (TYPE_NAME (type));
594 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
601 extern struct obstack temporary_obstack;
602 sig = build_java_argument_signature (type);
603 obstack_1grow (&temporary_obstack, '(');
604 obstack_grow (&temporary_obstack,
605 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
606 obstack_1grow (&temporary_obstack, ')');
608 t = build_java_signature (TREE_TYPE (type));
609 obstack_grow0 (&temporary_obstack,
610 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
612 sig = get_identifier (obstack_base (&temporary_obstack));
613 obstack_free (&temporary_obstack,
614 obstack_base (&temporary_obstack));
619 fatal ("internal error - build_java_signature passed invalid type");
621 TYPE_LANG_SPECIFIC (type)->signature = sig;
627 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
630 set_java_signature (type, sig)
635 while (TREE_CODE (type) == POINTER_TYPE)
636 type = TREE_TYPE (type);
637 if (TYPE_LANG_SPECIFIC (type) == NULL)
639 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
640 perm_calloc (1, sizeof (struct lang_type));
643 old_sig = TYPE_LANG_SPECIFIC (type)->signature;
644 if (old_sig != NULL_TREE && old_sig != sig)
645 fatal ("internal error - set_java_signature");
646 TYPE_LANG_SPECIFIC (type)->signature = sig;
647 #if 0 /* careful about METHOD_TYPE */
648 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
649 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
653 /* Search in class CLAS (and its superclasses) for a method
654 matching METHOD_NAME and argument signature METHOD_SIGNATURE.
655 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
656 (Contrast lookup_java_method, which takes into account return type.) */
659 lookup_argument_method (clas, method_name, method_signature)
660 tree clas, method_name, method_signature;
663 while (clas != NULL_TREE)
665 for (method = TYPE_METHODS (clas);
666 method != NULL_TREE; method = TREE_CHAIN (method))
668 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
669 if (DECL_NAME (method) == method_name && method_sig == method_signature)
672 clas = CLASSTYPE_SUPER (clas);
677 /* Search in class CLAS (and its superclasses) for a method
678 matching METHOD_NAME and signature METHOD_SIGNATURE.
679 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
680 (Contrast lookup_argument_method, which ignores return type.) */
683 lookup_java_method (clas, method_name, method_signature)
684 tree clas, method_name, method_signature;
687 while (clas != NULL_TREE)
689 for (method = TYPE_METHODS (clas);
690 method != NULL_TREE; method = TREE_CHAIN (method))
692 tree method_sig = build_java_signature (TREE_TYPE (method));
693 if (DECL_NAME (method) == method_name && method_sig == method_signature)
696 clas = CLASSTYPE_SUPER (clas);
701 /* Search in class CLAS (and its superclasses) for methods matching
702 METHOD_NAME and METHOD_SIGNATURE. Return a list of FUNCTION_DECLs.
703 When called from here, build_java_signature doesn't take the
704 returned type into account. */
707 match_java_method (clas, method_name, method_signature)
708 tree clas, method_name, method_signature;
711 tree list = NULL_TREE;
712 while (clas != NULL_TREE)
714 for (method = TYPE_METHODS (clas);
715 method != NULL_TREE; method = TREE_CHAIN (method))
717 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
718 if (DECL_NAME (method) == method_name
719 && method_sig == method_signature)
720 list = tree_cons (NULL_TREE, method, list);
722 clas = CLASSTYPE_SUPER (clas);
727 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
728 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
731 lookup_java_constructor (clas, method_signature)
732 tree clas, method_signature;
734 tree method = TYPE_METHODS (clas);
735 for ( ; method != NULL_TREE; method = TREE_CHAIN (method))
737 tree method_sig = build_java_signature (TREE_TYPE (method));
738 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
744 /* Return a type which is the Binary Numeric Promotion of the pair T1,
745 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
746 Promotion. It assumes that both T1 and T2 are elligible to BNP. */
749 binary_numeric_promotion (t1, t2, exp1, exp2)
755 if (t1 == double_type_node || t2 == double_type_node)
757 if (t1 != double_type_node)
758 *exp1 = convert (double_type_node, *exp1);
759 if (t2 != double_type_node)
760 *exp2 = convert (double_type_node, *exp2);
761 return double_type_node;
763 if (t1 == float_type_node || t2 == float_type_node)
765 if (t1 != float_type_node)
766 *exp1 = convert (float_type_node, *exp1);
767 if (t2 != float_type_node)
768 *exp2 = convert (float_type_node, *exp2);
769 return float_type_node;
771 if (t1 == long_type_node || t2 == long_type_node)
773 if (t1 != long_type_node)
774 *exp1 = convert (long_type_node, *exp1);
775 if (t2 != long_type_node)
776 *exp2 = convert (long_type_node, *exp2);
777 return long_type_node;
780 if (t1 != int_type_node)
781 *exp1 = convert (int_type_node, *exp1);
782 if (t2 != int_type_node)
783 *exp2 = convert (int_type_node, *exp2);
784 return int_type_node;