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> */
32 #include "java-tree.h"
38 extern struct obstack permanent_obstack;
40 /* Set the type of the local variable with index SLOT to TYPE. */
43 set_local_type (slot, type)
47 int max_locals = DECL_MAX_LOCALS(current_function_decl);
48 int nslots = TYPE_IS_WIDE (type) ? 2 : 1;
49 if (slot < 0 || slot + nslots - 1 >= max_locals)
50 fatal ("invalid local variable index");
51 type_map[slot] = type;
53 type_map[++slot] = void_type_node;
56 /* Convert an IEEE real to an integer type. The result of such a
57 conversion when the source operand is a NaN isn't defined by
58 IEEE754, but by the Java language standard: it must be zero. This
59 conversion produces something like:
61 ({ double tmp = expr; (tmp != tmp) ? 0 : (int)tmp; })
66 convert_ieee_real_to_integer (type, expr)
69 tree node, assignment, expr_decl;
71 expr_decl = build_decl (VAR_DECL, generate_name (), TREE_TYPE (expr));
72 layout_decl (expr_decl, 0);
73 expand_decl (pushdecl (expr_decl));
74 assignment = build (MODIFY_EXPR, NULL_TREE, expr_decl, expr);
75 TREE_SIDE_EFFECTS (assignment) = 1;
76 TREE_TYPE (assignment) = type;
78 expr = build (COMPOUND_EXPR, NULL_TREE,
80 build (COND_EXPR, type,
81 build (NE_EXPR, boolean_type_node, expr_decl, expr_decl),
83 convert_to_integer (type, expr_decl)));
85 TREE_TYPE (expr) = type;
89 /* Create an expression whose value is that of EXPR,
90 converted to type TYPE. The TREE_TYPE of the value
91 is always TYPE. This function implements all reasonable
92 conversions; callers should filter out those that are
93 not permitted by the language being compiled. */
99 register enum tree_code code = TREE_CODE (type);
101 if (type == TREE_TYPE (expr)
102 || TREE_CODE (expr) == ERROR_MARK)
104 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
105 return error_mark_node;
106 if (code == VOID_TYPE)
107 return build1 (CONVERT_EXPR, type, expr);
108 if (code == BOOLEAN_TYPE)
109 return fold (convert_to_boolean (type, expr));
110 if (code == INTEGER_TYPE)
112 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
113 #ifdef TARGET_SOFT_FLOAT
114 && !TARGET_SOFT_FLOAT
116 && !flag_emit_class_files
118 && TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
119 return fold (convert_ieee_real_to_integer (type, expr));
121 return fold (convert_to_integer (type, expr));
123 if (code == REAL_TYPE)
124 return fold (convert_to_real (type, expr));
125 if (code == CHAR_TYPE)
126 return fold (convert_to_char (type, expr));
127 if (code == POINTER_TYPE)
128 return fold (convert_to_pointer (type, expr));
129 error ("conversion to non-scalar type requested");
130 return error_mark_node;
135 convert_to_char (type, expr)
138 return build1 (NOP_EXPR, type, expr);
142 convert_to_boolean (type, expr)
145 return build1 (NOP_EXPR, type, expr);
148 /* Print an error message for invalid use of an incomplete type.
149 VALUE is the expression that was used (or 0 if that isn't known)
150 and TYPE is the type that was invalid. */
153 incomplete_type_error (value, type)
157 error ("internal error - use of undefined type");
160 /* Return a data type that has machine mode MODE.
161 If the mode is an integer,
162 then UNSIGNEDP selects between signed and unsigned types. */
165 type_for_mode (mode, unsignedp)
166 enum machine_mode mode;
169 if (mode == TYPE_MODE (int_type_node))
170 return unsignedp ? unsigned_int_type_node : int_type_node;
171 if (mode == TYPE_MODE (long_type_node))
172 return unsignedp ? unsigned_long_type_node : long_type_node;
173 if (mode == TYPE_MODE (short_type_node))
174 return unsignedp ? unsigned_short_type_node : short_type_node;
175 if (mode == TYPE_MODE (byte_type_node))
176 return unsignedp ? unsigned_byte_type_node : byte_type_node;
177 if (mode == TYPE_MODE (float_type_node))
178 return float_type_node;
179 if (mode == TYPE_MODE (double_type_node))
180 return double_type_node;
185 /* Return an integer type with BITS bits of precision,
186 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
189 type_for_size (bits, unsignedp)
193 if (bits <= TYPE_PRECISION (byte_type_node))
194 return unsignedp ? unsigned_byte_type_node : byte_type_node;
195 if (bits <= TYPE_PRECISION (short_type_node))
196 return unsignedp ? unsigned_short_type_node : short_type_node;
197 if (bits <= TYPE_PRECISION (int_type_node))
198 return unsignedp ? unsigned_int_type_node : int_type_node;
199 if (bits <= TYPE_PRECISION (long_type_node))
200 return unsignedp ? unsigned_long_type_node : long_type_node;
204 /* Return a type the same as TYPE except unsigned or
205 signed according to UNSIGNEDP. */
208 signed_or_unsigned_type (unsignedp, type)
212 if (! INTEGRAL_TYPE_P (type))
214 if (TYPE_PRECISION (type) == TYPE_PRECISION (int_type_node))
215 return unsignedp ? unsigned_int_type_node : int_type_node;
216 if (TYPE_PRECISION (type) == TYPE_PRECISION (byte_type_node))
217 return unsignedp ? unsigned_byte_type_node : byte_type_node;
218 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_type_node))
219 return unsignedp ? unsigned_short_type_node : short_type_node;
220 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_type_node))
221 return unsignedp ? unsigned_long_type_node : long_type_node;
225 /* Return a signed type the same as TYPE in other respects. */
231 return signed_or_unsigned_type (0, type);
234 /* Return an unsigned type the same as TYPE in other respects. */
240 return signed_or_unsigned_type (1, type);
244 /* Mark EXP saying that we need to be able to take the
245 address of it; it should not be allocated in a register.
246 Value is 1 if successful. */
249 mark_addressable (exp)
252 register tree x = exp;
254 switch (TREE_CODE (x))
261 x = TREE_OPERAND (x, 0);
264 case TRUTH_ANDIF_EXPR:
265 case TRUTH_ORIF_EXPR:
267 x = TREE_OPERAND (x, 1);
271 return mark_addressable (TREE_OPERAND (x, 1))
272 & mark_addressable (TREE_OPERAND (x, 2));
275 TREE_ADDRESSABLE (x) = 1;
279 /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
280 incompatibility problems. Handle this case by marking FOO. */
281 if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
282 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
284 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
287 if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
289 x = TREE_OPERAND (x, 0);
299 TREE_ADDRESSABLE (x) = 1;
300 #if 0 /* poplevel deals with this now. */
301 if (DECL_CONTEXT (x) == 0)
302 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
310 /* Thorough checking of the arrayness of TYPE. */
313 is_array_type_p (type)
316 return TREE_CODE (type) == POINTER_TYPE
317 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
318 && TYPE_ARRAY_P (TREE_TYPE (type));
321 /* Return the length of a Java array type.
322 Return -1 if the length is unknown or non-constant. */
325 java_array_type_length (array_type)
329 if (TREE_CODE (array_type) == POINTER_TYPE)
330 array_type = TREE_TYPE (array_type);
331 arfld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
332 if (arfld != NULL_TREE)
334 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
335 tree high = TYPE_MAX_VALUE (index_type);
336 if (TREE_CODE (high) == INTEGER_CST)
337 return TREE_INT_CST_LOW (high) + 1;
343 build_prim_array_type (element_type, length)
345 HOST_WIDE_INT length;
347 tree max_index = build_int_2 (length - 1, 0);
348 TREE_TYPE (max_index) = sizetype;
349 return build_array_type (element_type, build_index_type (max_index));
352 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
353 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
354 The LENGTH is -1 if the length is unknown. */
357 build_java_array_type (element_type, length)
359 HOST_WIDE_INT length;
363 tree elsig = build_java_signature (element_type);
364 tree el_name = element_type;
365 sprintf (buf, length >= 0 ? "[%d" : "[", length);
366 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
368 t = IDENTIFIER_SIGNATURE_TYPE (sig);
370 return TREE_TYPE (t);
372 IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
373 TYPE_ARRAY_P (t) = 1;
375 if (TREE_CODE (el_name) == POINTER_TYPE)
376 el_name = TREE_TYPE (el_name);
377 el_name = TYPE_NAME (el_name);
378 if (TREE_CODE (el_name) == TYPE_DECL)
379 el_name = DECL_NAME (el_name);
380 TYPE_NAME (t) = identifier_subst (el_name, "", '.', '.', "[]");
382 set_java_signature (t, sig);
383 set_super_info (0, t, object_type_node, 0);
384 if (TREE_CODE (element_type) == RECORD_TYPE)
385 element_type = promote_type (element_type);
386 TYPE_ARRAY_ELEMENT (t) = element_type;
388 /* Add length pseudo-field. */
389 push_obstacks (&permanent_obstack, &permanent_obstack);
390 fld = build_decl (FIELD_DECL, get_identifier ("length"), int_type_node);
391 TYPE_FIELDS (t) = fld;
392 DECL_CONTEXT (fld) = t;
393 FIELD_PUBLIC (fld) = 1;
394 FIELD_FINAL (fld) = 1;
398 tree atype = build_prim_array_type (element_type, length);
399 tree arfld = build_decl (FIELD_DECL, get_identifier ("data"), atype);
400 DECL_CONTEXT (arfld) = t;
401 TREE_CHAIN (fld) = arfld;
404 TYPE_ALIGN (t) = TYPE_ALIGN (element_type);
411 /* Promote TYPE to the type actually used for fields and parameters. */
417 switch (TREE_CODE (type))
420 return build_pointer_type (CLASS_TO_HANDLE_TYPE (type));
422 if (type == boolean_type_node)
423 return promoted_boolean_type_node;
426 if (type == char_type_node)
427 return promoted_char_type_node;
431 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
433 if (type == short_type_node)
434 return promoted_short_type_node;
435 if (type == byte_type_node)
436 return promoted_byte_type_node;
437 return int_type_node;
439 /* ... else fall through ... */
445 /* Parse a signature string, starting at *PTR and ending at LIMIT.
446 Return the seen TREE_TYPE, updating *PTR. */
449 parse_signature_type (ptr, limit)
450 const unsigned char **ptr, *limit;
454 fatal ("bad signature string");
457 case 'B': (*ptr)++; return byte_type_node;
458 case 'C': (*ptr)++; return char_type_node;
459 case 'D': (*ptr)++; return double_type_node;
460 case 'F': (*ptr)++; return float_type_node;
461 case 'S': (*ptr)++; return short_type_node;
462 case 'I': (*ptr)++; return int_type_node;
463 case 'J': (*ptr)++; return long_type_node;
464 case 'Z': (*ptr)++; return boolean_type_node;
465 case 'V': (*ptr)++; return void_type_node;
467 for ((*ptr)++; (*ptr) < limit && isdigit (**ptr); ) (*ptr)++;
468 type = parse_signature_type (ptr, limit);
469 type = build_java_array_type (type, -1);
473 const unsigned char *start = ++(*ptr);
474 register const unsigned char *str = start;
478 fatal ("bad signature string");
483 type = lookup_class (unmangle_classname (start, str - start));
487 fatal ("unrecognized signature string");
489 return promote_type (type);
492 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
493 and SIG_LENGTH bytes long.
494 Return a gcc type node. */
497 parse_signature_string (sig_string, sig_length)
498 const unsigned char *sig_string;
502 const unsigned char *str = sig_string;
503 const unsigned char *limit = str + sig_length;
505 push_obstacks (&permanent_obstack, &permanent_obstack);
506 if (str < limit && str[0] == '(')
508 tree argtype_list = NULL_TREE;
510 while (str < limit && str[0] != ')')
512 tree argtype = parse_signature_type (&str, limit);
513 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
515 if (str++, str >= limit)
516 fatal ("bad signature string");
517 result_type = parse_signature_type (&str, limit);
518 argtype_list = chainon (nreverse (argtype_list), end_params_node);
519 result_type = build_function_type (result_type, argtype_list);
522 result_type = parse_signature_type (&str, limit);
524 error ("junk at end of signature string");
529 /* Convert a signature to its type.
530 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
534 get_type_from_signature (tree signature)
536 unsigned char *sig = (unsigned char *) IDENTIFIER_POINTER (signature);
537 int len = IDENTIFIER_LENGTH (signature);
539 /* Primitive types aren't cached. */
541 return parse_signature_string (sig, len);
542 type = IDENTIFIER_SIGNATURE_TYPE (signature);
543 if (type == NULL_TREE)
545 type = parse_signature_string (sig, len);
546 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
551 /* Return the signature string for the arguments of method type TYPE. */
554 build_java_argument_signature (type)
557 extern struct obstack temporary_obstack;
558 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
559 if (sig == NULL_TREE)
561 tree args = TYPE_ARG_TYPES (type);
562 if (TREE_CODE (type) == METHOD_TYPE)
563 args = TREE_CHAIN (args); /* Skip "this" argument. */
564 for (; args != end_params_node; args = TREE_CHAIN (args))
566 tree t = build_java_signature (TREE_VALUE (args));
567 obstack_grow (&temporary_obstack,
568 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
570 obstack_1grow (&temporary_obstack, '\0');
572 sig = get_identifier (obstack_base (&temporary_obstack));
573 TYPE_ARGUMENT_SIGNATURE (type) = sig;
574 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
579 /* Return the signature of the given TYPE. */
582 build_java_signature (type)
586 push_obstacks (&permanent_obstack, &permanent_obstack);
587 while (TREE_CODE (type) == POINTER_TYPE)
588 type = TREE_TYPE (type);
589 if (TYPE_LANG_SPECIFIC (type) == NULL)
591 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
592 perm_calloc (1, sizeof (struct lang_type));
594 sig = TYPE_LANG_SPECIFIC (type)->signature;
595 if (sig == NULL_TREE)
598 switch (TREE_CODE (type))
600 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
601 case CHAR_TYPE: sg[0] = 'C'; goto native;
602 case VOID_TYPE: sg[0] = 'V'; goto native;
604 switch (TYPE_PRECISION (type))
606 case 8: sg[0] = 'B'; goto native;
607 case 16: sg[0] = 'S'; goto native;
608 case 32: sg[0] = 'I'; goto native;
609 case 64: sg[0] = 'J'; goto native;
610 default: goto bad_type;
613 switch (TYPE_PRECISION (type))
615 case 32: sg[0] = 'F'; goto native;
616 case 64: sg[0] = 'D'; goto native;
617 default: goto bad_type;
621 sig = get_identifier (sg);
624 if (TYPE_ARRAY_P (type))
626 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
627 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
632 t = DECL_NAME (TYPE_NAME (type));
633 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
640 extern struct obstack temporary_obstack;
641 sig = build_java_argument_signature (type);
642 obstack_1grow (&temporary_obstack, '(');
643 obstack_grow (&temporary_obstack,
644 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
645 obstack_1grow (&temporary_obstack, ')');
647 t = build_java_signature (TREE_TYPE (type));
648 obstack_grow0 (&temporary_obstack,
649 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
651 sig = get_identifier (obstack_base (&temporary_obstack));
652 obstack_free (&temporary_obstack,
653 obstack_base (&temporary_obstack));
658 fatal ("internal error - build_java_signature passed invalid type");
660 TYPE_LANG_SPECIFIC (type)->signature = sig;
666 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
669 set_java_signature (type, sig)
674 while (TREE_CODE (type) == POINTER_TYPE)
675 type = TREE_TYPE (type);
676 if (TYPE_LANG_SPECIFIC (type) == NULL)
678 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
679 perm_calloc (1, sizeof (struct lang_type));
682 old_sig = TYPE_LANG_SPECIFIC (type)->signature;
683 if (old_sig != NULL_TREE && old_sig != sig)
684 fatal ("internal error - set_java_signature");
685 TYPE_LANG_SPECIFIC (type)->signature = sig;
686 #if 0 /* careful about METHOD_TYPE */
687 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
688 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
692 /* Search in class CLAS (and its superclasses) for a method
693 matching METHOD_NAME and argument signature METHOD_SIGNATURE.
694 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
695 (Contrast lookup_java_method, which takes into account return type.) */
698 lookup_argument_method (clas, method_name, method_signature)
699 tree clas, method_name, method_signature;
702 while (clas != NULL_TREE)
704 for (method = TYPE_METHODS (clas);
705 method != NULL_TREE; method = TREE_CHAIN (method))
707 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
708 tree name = DECL_NAME (method);
709 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
710 EXPR_WFL_NODE (name) : name) == method_name
711 && method_sig == method_signature)
714 clas = CLASSTYPE_SUPER (clas);
719 /* Search in class CLAS (and its superclasses) for a method
720 matching METHOD_NAME and signature METHOD_SIGNATURE.
721 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
722 (Contrast lookup_argument_method, which ignores return type.) */
725 lookup_java_method (clas, method_name, method_signature)
726 tree clas, method_name, method_signature;
729 while (clas != NULL_TREE)
731 for (method = TYPE_METHODS (clas);
732 method != NULL_TREE; method = TREE_CHAIN (method))
734 tree method_sig = build_java_signature (TREE_TYPE (method));
735 if (DECL_NAME (method) == method_name
736 && method_sig == method_signature)
739 clas = CLASSTYPE_SUPER (clas);
744 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
745 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
748 lookup_java_constructor (clas, method_signature)
749 tree clas, method_signature;
751 tree method = TYPE_METHODS (clas);
752 for ( ; method != NULL_TREE; method = TREE_CHAIN (method))
754 tree method_sig = build_java_signature (TREE_TYPE (method));
755 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
761 /* Return a type which is the Binary Numeric Promotion of the pair T1,
762 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
763 Promotion. It assumes that both T1 and T2 are elligible to BNP. */
766 binary_numeric_promotion (t1, t2, exp1, exp2)
772 if (t1 == double_type_node || t2 == double_type_node)
774 if (t1 != double_type_node)
775 *exp1 = convert (double_type_node, *exp1);
776 if (t2 != double_type_node)
777 *exp2 = convert (double_type_node, *exp2);
778 return double_type_node;
780 if (t1 == float_type_node || t2 == float_type_node)
782 if (t1 != float_type_node)
783 *exp1 = convert (float_type_node, *exp1);
784 if (t2 != float_type_node)
785 *exp2 = convert (float_type_node, *exp2);
786 return float_type_node;
788 if (t1 == long_type_node || t2 == long_type_node)
790 if (t1 != long_type_node)
791 *exp1 = convert (long_type_node, *exp1);
792 if (t2 != long_type_node)
793 *exp2 = convert (long_type_node, *exp2);
794 return long_type_node;
797 if (t1 != int_type_node)
798 *exp1 = convert (int_type_node, *exp1);
799 if (t2 != int_type_node)
800 *exp2 = convert (int_type_node, *exp2);
801 return int_type_node;