1 /* Handle types for the GNU compiler for the Java(TM) language.
2 Copyright (C) 1996, 97-98, 1999 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. Also,
59 overflows must be clipped to within range. This conversion
60 produces something like:
62 ((expr >= (float)MAX_INT)
64 : ((expr <= (float)MIN_INT)
71 convert_ieee_real_to_integer (type, expr)
75 expr = save_expr (expr);
77 result = build (COND_EXPR, type,
78 build (NE_EXPR, boolean_type_node, expr, expr),
79 convert (type, integer_zero_node),
80 convert_to_integer (type, expr));
82 result = build (COND_EXPR, type,
83 build (LE_EXPR, boolean_type_node, expr,
84 convert (TREE_TYPE (expr), TYPE_MIN_VALUE (type))),
85 TYPE_MIN_VALUE (type),
88 result = build (COND_EXPR, type,
89 build (GE_EXPR, boolean_type_node, expr,
90 convert (TREE_TYPE (expr), TYPE_MAX_VALUE (type))),
91 TYPE_MAX_VALUE (type),
97 /* Create an expression whose value is that of EXPR,
98 converted to type TYPE. The TREE_TYPE of the value
99 is always TYPE. This function implements all reasonable
100 conversions; callers should filter out those that are
101 not permitted by the language being compiled. */
107 register enum tree_code code = TREE_CODE (type);
110 return build1 (NOP_EXPR, type, expr);
112 if (type == TREE_TYPE (expr)
113 || TREE_CODE (expr) == ERROR_MARK)
115 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
116 return error_mark_node;
117 if (code == VOID_TYPE)
118 return build1 (CONVERT_EXPR, type, expr);
119 if (code == BOOLEAN_TYPE)
120 return fold (convert_to_boolean (type, expr));
121 if (code == INTEGER_TYPE)
124 && ! flag_emit_class_files
125 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
126 && TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
127 return fold (convert_ieee_real_to_integer (type, expr));
129 return fold (convert_to_integer (type, expr));
131 if (code == REAL_TYPE)
132 return fold (convert_to_real (type, expr));
133 if (code == CHAR_TYPE)
134 return fold (convert_to_char (type, expr));
135 if (code == POINTER_TYPE)
136 return fold (convert_to_pointer (type, expr));
137 error ("conversion to non-scalar type requested");
138 return error_mark_node;
143 convert_to_char (type, expr)
146 return build1 (NOP_EXPR, type, expr);
150 convert_to_boolean (type, expr)
153 return build1 (NOP_EXPR, type, expr);
156 /* Print an error message for invalid use of an incomplete type.
157 VALUE is the expression that was used (or 0 if that isn't known)
158 and TYPE is the type that was invalid. */
161 incomplete_type_error (value, type)
162 tree value ATTRIBUTE_UNUSED;
163 tree type ATTRIBUTE_UNUSED;
165 error ("internal error - use of undefined type");
168 /* Return a data type that has machine mode MODE.
169 If the mode is an integer,
170 then UNSIGNEDP selects between signed and unsigned types. */
173 type_for_mode (mode, unsignedp)
174 enum machine_mode mode;
177 if (mode == TYPE_MODE (int_type_node))
178 return unsignedp ? unsigned_int_type_node : int_type_node;
179 if (mode == TYPE_MODE (long_type_node))
180 return unsignedp ? unsigned_long_type_node : long_type_node;
181 if (mode == TYPE_MODE (short_type_node))
182 return unsignedp ? unsigned_short_type_node : short_type_node;
183 if (mode == TYPE_MODE (byte_type_node))
184 return unsignedp ? unsigned_byte_type_node : byte_type_node;
185 if (mode == TYPE_MODE (float_type_node))
186 return float_type_node;
187 if (mode == TYPE_MODE (double_type_node))
188 return double_type_node;
193 /* Return an integer type with BITS bits of precision,
194 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
197 type_for_size (bits, unsignedp)
201 if (bits <= TYPE_PRECISION (byte_type_node))
202 return unsignedp ? unsigned_byte_type_node : byte_type_node;
203 if (bits <= TYPE_PRECISION (short_type_node))
204 return unsignedp ? unsigned_short_type_node : short_type_node;
205 if (bits <= TYPE_PRECISION (int_type_node))
206 return unsignedp ? unsigned_int_type_node : int_type_node;
207 if (bits <= TYPE_PRECISION (long_type_node))
208 return unsignedp ? unsigned_long_type_node : long_type_node;
212 /* Return a type the same as TYPE except unsigned or
213 signed according to UNSIGNEDP. */
216 signed_or_unsigned_type (unsignedp, type)
220 if (! INTEGRAL_TYPE_P (type))
222 if (TYPE_PRECISION (type) == TYPE_PRECISION (int_type_node))
223 return unsignedp ? unsigned_int_type_node : int_type_node;
224 if (TYPE_PRECISION (type) == TYPE_PRECISION (byte_type_node))
225 return unsignedp ? unsigned_byte_type_node : byte_type_node;
226 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_type_node))
227 return unsignedp ? unsigned_short_type_node : short_type_node;
228 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_type_node))
229 return unsignedp ? unsigned_long_type_node : long_type_node;
233 /* Return a signed type the same as TYPE in other respects. */
239 return signed_or_unsigned_type (0, type);
242 /* Return an unsigned type the same as TYPE in other respects. */
248 return signed_or_unsigned_type (1, type);
252 /* Mark EXP saying that we need to be able to take the
253 address of it; it should not be allocated in a register.
254 Value is 1 if successful. */
257 mark_addressable (exp)
260 register tree x = exp;
262 switch (TREE_CODE (x))
269 x = TREE_OPERAND (x, 0);
272 case TRUTH_ANDIF_EXPR:
273 case TRUTH_ORIF_EXPR:
275 x = TREE_OPERAND (x, 1);
279 return mark_addressable (TREE_OPERAND (x, 1))
280 & mark_addressable (TREE_OPERAND (x, 2));
283 TREE_ADDRESSABLE (x) = 1;
287 /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
288 incompatibility problems. Handle this case by marking FOO. */
289 if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
290 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
292 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
295 if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
297 x = TREE_OPERAND (x, 0);
307 TREE_ADDRESSABLE (x) = 1;
308 #if 0 /* poplevel deals with this now. */
309 if (DECL_CONTEXT (x) == 0)
310 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
318 /* Thorough checking of the arrayness of TYPE. */
321 is_array_type_p (type)
324 return TREE_CODE (type) == POINTER_TYPE
325 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
326 && TYPE_ARRAY_P (TREE_TYPE (type));
329 /* Return the length of a Java array type.
330 Return -1 if the length is unknown or non-constant. */
333 java_array_type_length (array_type)
337 if (TREE_CODE (array_type) == POINTER_TYPE)
338 array_type = TREE_TYPE (array_type);
339 arfld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
340 if (arfld != NULL_TREE)
342 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
343 tree high = TYPE_MAX_VALUE (index_type);
344 if (TREE_CODE (high) == INTEGER_CST)
345 return TREE_INT_CST_LOW (high) + 1;
351 build_prim_array_type (element_type, length)
353 HOST_WIDE_INT length;
355 tree max_index = build_int_2 (length - 1, 0);
356 TREE_TYPE (max_index) = sizetype;
357 return build_array_type (element_type, build_index_type (max_index));
360 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
361 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
362 The LENGTH is -1 if the length is unknown. */
365 build_java_array_type (element_type, length)
367 HOST_WIDE_INT length;
371 tree elsig = build_java_signature (element_type);
372 tree el_name = element_type;
373 sprintf (buf, length >= 0 ? "[%d" : "[", length);
374 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
376 t = IDENTIFIER_SIGNATURE_TYPE (sig);
378 return TREE_TYPE (t);
380 IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
381 TYPE_ARRAY_P (t) = 1;
383 if (TREE_CODE (el_name) == POINTER_TYPE)
384 el_name = TREE_TYPE (el_name);
385 el_name = TYPE_NAME (el_name);
386 if (TREE_CODE (el_name) == TYPE_DECL)
387 el_name = DECL_NAME (el_name);
388 TYPE_NAME (t) = identifier_subst (el_name, "", '.', '.', "[]");
390 set_java_signature (t, sig);
391 set_super_info (0, t, object_type_node, 0);
392 if (TREE_CODE (element_type) == RECORD_TYPE)
393 element_type = promote_type (element_type);
394 TYPE_ARRAY_ELEMENT (t) = element_type;
396 /* Add length pseudo-field. */
397 push_obstacks (&permanent_obstack, &permanent_obstack);
398 fld = build_decl (FIELD_DECL, get_identifier ("length"), int_type_node);
399 TYPE_FIELDS (t) = fld;
400 DECL_CONTEXT (fld) = t;
401 FIELD_PUBLIC (fld) = 1;
402 FIELD_FINAL (fld) = 1;
406 tree atype = build_prim_array_type (element_type, length);
407 tree arfld = build_decl (FIELD_DECL, get_identifier ("data"), atype);
408 DECL_CONTEXT (arfld) = t;
409 TREE_CHAIN (fld) = arfld;
412 TYPE_ALIGN (t) = TYPE_ALIGN (element_type);
415 /* We could layout_class, but that loads java.lang.Object prematurely.
416 * This is called by the parser, and it is a bad idea to do load_class
417 * in the middle of parsing, because of possible circularity problems. */
418 push_super_field (t, object_type_node);
424 /* Promote TYPE to the type actually used for fields and parameters. */
430 switch (TREE_CODE (type))
433 return build_pointer_type (CLASS_TO_HANDLE_TYPE (type));
435 if (type == boolean_type_node)
436 return promoted_boolean_type_node;
439 if (type == char_type_node)
440 return promoted_char_type_node;
444 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
446 if (type == short_type_node)
447 return promoted_short_type_node;
448 if (type == byte_type_node)
449 return promoted_byte_type_node;
450 return int_type_node;
452 /* ... else fall through ... */
458 /* Parse a signature string, starting at *PTR and ending at LIMIT.
459 Return the seen TREE_TYPE, updating *PTR. */
462 parse_signature_type (ptr, limit)
463 const unsigned char **ptr, *limit;
467 fatal ("bad signature string");
470 case 'B': (*ptr)++; return byte_type_node;
471 case 'C': (*ptr)++; return char_type_node;
472 case 'D': (*ptr)++; return double_type_node;
473 case 'F': (*ptr)++; return float_type_node;
474 case 'S': (*ptr)++; return short_type_node;
475 case 'I': (*ptr)++; return int_type_node;
476 case 'J': (*ptr)++; return long_type_node;
477 case 'Z': (*ptr)++; return boolean_type_node;
478 case 'V': (*ptr)++; return void_type_node;
480 for ((*ptr)++; (*ptr) < limit && ISDIGIT (**ptr); ) (*ptr)++;
481 type = parse_signature_type (ptr, limit);
482 type = build_java_array_type (type, -1);
486 const unsigned char *start = ++(*ptr);
487 register const unsigned char *str = start;
491 fatal ("bad signature string");
496 type = lookup_class (unmangle_classname (start, str - start));
500 fatal ("unrecognized signature string");
502 return promote_type (type);
505 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
506 and SIG_LENGTH bytes long.
507 Return a gcc type node. */
510 parse_signature_string (sig_string, sig_length)
511 const unsigned char *sig_string;
515 const unsigned char *str = sig_string;
516 const unsigned char *limit = str + sig_length;
518 push_obstacks (&permanent_obstack, &permanent_obstack);
519 if (str < limit && str[0] == '(')
521 tree argtype_list = NULL_TREE;
523 while (str < limit && str[0] != ')')
525 tree argtype = parse_signature_type (&str, limit);
526 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
528 if (str++, str >= limit)
529 fatal ("bad signature string");
530 result_type = parse_signature_type (&str, limit);
531 argtype_list = chainon (nreverse (argtype_list), end_params_node);
532 result_type = build_function_type (result_type, argtype_list);
535 result_type = parse_signature_type (&str, limit);
537 error ("junk at end of signature string");
542 /* Convert a signature to its type.
543 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
547 get_type_from_signature (tree signature)
549 unsigned char *sig = (unsigned char *) IDENTIFIER_POINTER (signature);
550 int len = IDENTIFIER_LENGTH (signature);
552 /* Primitive types aren't cached. */
554 return parse_signature_string (sig, len);
555 type = IDENTIFIER_SIGNATURE_TYPE (signature);
556 if (type == NULL_TREE)
558 type = parse_signature_string (sig, len);
559 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
564 /* Return the signature string for the arguments of method type TYPE. */
567 build_java_argument_signature (type)
570 extern struct obstack temporary_obstack;
571 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
572 if (sig == NULL_TREE)
574 tree args = TYPE_ARG_TYPES (type);
575 if (TREE_CODE (type) == METHOD_TYPE)
576 args = TREE_CHAIN (args); /* Skip "this" argument. */
577 for (; args != end_params_node; args = TREE_CHAIN (args))
579 tree t = build_java_signature (TREE_VALUE (args));
580 obstack_grow (&temporary_obstack,
581 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
583 obstack_1grow (&temporary_obstack, '\0');
585 sig = get_identifier (obstack_base (&temporary_obstack));
586 TYPE_ARGUMENT_SIGNATURE (type) = sig;
587 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
592 /* Return the signature of the given TYPE. */
595 build_java_signature (type)
599 push_obstacks (&permanent_obstack, &permanent_obstack);
600 while (TREE_CODE (type) == POINTER_TYPE)
601 type = TREE_TYPE (type);
602 if (TYPE_LANG_SPECIFIC (type) == NULL)
604 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
605 perm_calloc (1, sizeof (struct lang_type));
607 sig = TYPE_LANG_SPECIFIC (type)->signature;
608 if (sig == NULL_TREE)
611 switch (TREE_CODE (type))
613 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
614 case CHAR_TYPE: sg[0] = 'C'; goto native;
615 case VOID_TYPE: sg[0] = 'V'; goto native;
617 switch (TYPE_PRECISION (type))
619 case 8: sg[0] = 'B'; goto native;
620 case 16: sg[0] = 'S'; goto native;
621 case 32: sg[0] = 'I'; goto native;
622 case 64: sg[0] = 'J'; goto native;
623 default: goto bad_type;
626 switch (TYPE_PRECISION (type))
628 case 32: sg[0] = 'F'; goto native;
629 case 64: sg[0] = 'D'; goto native;
630 default: goto bad_type;
634 sig = get_identifier (sg);
637 if (TYPE_ARRAY_P (type))
639 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
640 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
645 t = DECL_NAME (TYPE_NAME (type));
646 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
653 extern struct obstack temporary_obstack;
654 sig = build_java_argument_signature (type);
655 obstack_1grow (&temporary_obstack, '(');
656 obstack_grow (&temporary_obstack,
657 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
658 obstack_1grow (&temporary_obstack, ')');
660 t = build_java_signature (TREE_TYPE (type));
661 obstack_grow0 (&temporary_obstack,
662 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
664 sig = get_identifier (obstack_base (&temporary_obstack));
665 obstack_free (&temporary_obstack,
666 obstack_base (&temporary_obstack));
671 fatal ("internal error - build_java_signature passed invalid type");
673 TYPE_LANG_SPECIFIC (type)->signature = sig;
679 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
682 set_java_signature (type, sig)
687 while (TREE_CODE (type) == POINTER_TYPE)
688 type = TREE_TYPE (type);
689 if (TYPE_LANG_SPECIFIC (type) == NULL)
691 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
692 perm_calloc (1, sizeof (struct lang_type));
695 old_sig = TYPE_LANG_SPECIFIC (type)->signature;
696 if (old_sig != NULL_TREE && old_sig != sig)
697 fatal ("internal error - set_java_signature");
698 TYPE_LANG_SPECIFIC (type)->signature = sig;
699 #if 0 /* careful about METHOD_TYPE */
700 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
701 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
705 /* Search in class CLAS (and its superclasses) for a method
706 matching METHOD_NAME and argument signature METHOD_SIGNATURE.
707 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
708 (Contrast lookup_java_method, which takes into account return type.) */
711 lookup_argument_method (clas, method_name, method_signature)
712 tree clas, method_name, method_signature;
715 while (clas != NULL_TREE)
717 for (method = TYPE_METHODS (clas);
718 method != NULL_TREE; method = TREE_CHAIN (method))
720 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
721 tree name = DECL_NAME (method);
722 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
723 EXPR_WFL_NODE (name) : name) == method_name
724 && method_sig == method_signature)
727 clas = CLASSTYPE_SUPER (clas);
732 /* Search in class CLAS (and its superclasses) for a method
733 matching METHOD_NAME and signature METHOD_SIGNATURE.
734 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
735 (Contrast lookup_argument_method, which ignores return type.) */
738 lookup_java_method (clas, method_name, method_signature)
739 tree clas, method_name, method_signature;
742 while (clas != NULL_TREE)
744 for (method = TYPE_METHODS (clas);
745 method != NULL_TREE; method = TREE_CHAIN (method))
747 tree method_sig = build_java_signature (TREE_TYPE (method));
748 if (DECL_NAME (method) == method_name
749 && method_sig == method_signature)
752 clas = CLASSTYPE_SUPER (clas);
757 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
758 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
761 lookup_java_constructor (clas, method_signature)
762 tree clas, method_signature;
764 tree method = TYPE_METHODS (clas);
765 for ( ; method != NULL_TREE; method = TREE_CHAIN (method))
767 tree method_sig = build_java_signature (TREE_TYPE (method));
768 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
774 /* Return a type which is the Binary Numeric Promotion of the pair T1,
775 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
776 Promotion. It assumes that both T1 and T2 are elligible to BNP. */
779 binary_numeric_promotion (t1, t2, exp1, exp2)
785 if (t1 == double_type_node || t2 == double_type_node)
787 if (t1 != double_type_node)
788 *exp1 = convert (double_type_node, *exp1);
789 if (t2 != double_type_node)
790 *exp2 = convert (double_type_node, *exp2);
791 return double_type_node;
793 if (t1 == float_type_node || t2 == float_type_node)
795 if (t1 != float_type_node)
796 *exp1 = convert (float_type_node, *exp1);
797 if (t2 != float_type_node)
798 *exp2 = convert (float_type_node, *exp2);
799 return float_type_node;
801 if (t1 == long_type_node || t2 == long_type_node)
803 if (t1 != long_type_node)
804 *exp1 = convert (long_type_node, *exp1);
805 if (t2 != long_type_node)
806 *exp2 = convert (long_type_node, *exp2);
807 return long_type_node;
810 if (t1 != int_type_node)
811 *exp1 = convert (int_type_node, *exp1);
812 if (t2 != int_type_node)
813 *exp2 = convert (int_type_node, *exp2);
814 return int_type_node;