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. 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 assignment, expr_decl;
70 expr = save_expr (expr);
72 return build (COND_EXPR, type,
73 build (NE_EXPR, boolean_type_node, expr, expr),
74 convert (type, integer_zero_node),
75 convert_to_integer (type, expr));
78 /* Create an expression whose value is that of EXPR,
79 converted to type TYPE. The TREE_TYPE of the value
80 is always TYPE. This function implements all reasonable
81 conversions; callers should filter out those that are
82 not permitted by the language being compiled. */
88 register enum tree_code code = TREE_CODE (type);
90 if (type == TREE_TYPE (expr)
91 || TREE_CODE (expr) == ERROR_MARK)
93 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
94 return error_mark_node;
95 if (code == VOID_TYPE)
96 return build1 (CONVERT_EXPR, type, expr);
97 if (code == BOOLEAN_TYPE)
98 return fold (convert_to_boolean (type, expr));
99 if (code == INTEGER_TYPE)
101 if (TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE
102 #ifdef TARGET_SOFT_FLOAT
103 && !TARGET_SOFT_FLOAT
105 && !flag_emit_class_files
107 && TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
108 return fold (convert_ieee_real_to_integer (type, expr));
110 return fold (convert_to_integer (type, expr));
112 if (code == REAL_TYPE)
113 return fold (convert_to_real (type, expr));
114 if (code == CHAR_TYPE)
115 return fold (convert_to_char (type, expr));
116 if (code == POINTER_TYPE)
117 return fold (convert_to_pointer (type, expr));
118 error ("conversion to non-scalar type requested");
119 return error_mark_node;
124 convert_to_char (type, expr)
127 return build1 (NOP_EXPR, type, expr);
131 convert_to_boolean (type, expr)
134 return build1 (NOP_EXPR, type, expr);
137 /* Print an error message for invalid use of an incomplete type.
138 VALUE is the expression that was used (or 0 if that isn't known)
139 and TYPE is the type that was invalid. */
142 incomplete_type_error (value, type)
143 tree value ATTRIBUTE_UNUSED;
144 tree type ATTRIBUTE_UNUSED;
146 error ("internal error - use of undefined type");
149 /* Return a data type that has machine mode MODE.
150 If the mode is an integer,
151 then UNSIGNEDP selects between signed and unsigned types. */
154 type_for_mode (mode, unsignedp)
155 enum machine_mode mode;
158 if (mode == TYPE_MODE (int_type_node))
159 return unsignedp ? unsigned_int_type_node : int_type_node;
160 if (mode == TYPE_MODE (long_type_node))
161 return unsignedp ? unsigned_long_type_node : long_type_node;
162 if (mode == TYPE_MODE (short_type_node))
163 return unsignedp ? unsigned_short_type_node : short_type_node;
164 if (mode == TYPE_MODE (byte_type_node))
165 return unsignedp ? unsigned_byte_type_node : byte_type_node;
166 if (mode == TYPE_MODE (float_type_node))
167 return float_type_node;
168 if (mode == TYPE_MODE (double_type_node))
169 return double_type_node;
174 /* Return an integer type with BITS bits of precision,
175 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
178 type_for_size (bits, unsignedp)
182 if (bits <= TYPE_PRECISION (byte_type_node))
183 return unsignedp ? unsigned_byte_type_node : byte_type_node;
184 if (bits <= TYPE_PRECISION (short_type_node))
185 return unsignedp ? unsigned_short_type_node : short_type_node;
186 if (bits <= TYPE_PRECISION (int_type_node))
187 return unsignedp ? unsigned_int_type_node : int_type_node;
188 if (bits <= TYPE_PRECISION (long_type_node))
189 return unsignedp ? unsigned_long_type_node : long_type_node;
193 /* Return a type the same as TYPE except unsigned or
194 signed according to UNSIGNEDP. */
197 signed_or_unsigned_type (unsignedp, type)
201 if (! INTEGRAL_TYPE_P (type))
203 if (TYPE_PRECISION (type) == TYPE_PRECISION (int_type_node))
204 return unsignedp ? unsigned_int_type_node : int_type_node;
205 if (TYPE_PRECISION (type) == TYPE_PRECISION (byte_type_node))
206 return unsignedp ? unsigned_byte_type_node : byte_type_node;
207 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_type_node))
208 return unsignedp ? unsigned_short_type_node : short_type_node;
209 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_type_node))
210 return unsignedp ? unsigned_long_type_node : long_type_node;
214 /* Return a signed type the same as TYPE in other respects. */
220 return signed_or_unsigned_type (0, type);
223 /* Return an unsigned type the same as TYPE in other respects. */
229 return signed_or_unsigned_type (1, type);
233 /* Mark EXP saying that we need to be able to take the
234 address of it; it should not be allocated in a register.
235 Value is 1 if successful. */
238 mark_addressable (exp)
241 register tree x = exp;
243 switch (TREE_CODE (x))
250 x = TREE_OPERAND (x, 0);
253 case TRUTH_ANDIF_EXPR:
254 case TRUTH_ORIF_EXPR:
256 x = TREE_OPERAND (x, 1);
260 return mark_addressable (TREE_OPERAND (x, 1))
261 & mark_addressable (TREE_OPERAND (x, 2));
264 TREE_ADDRESSABLE (x) = 1;
268 /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
269 incompatibility problems. Handle this case by marking FOO. */
270 if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
271 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
273 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
276 if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
278 x = TREE_OPERAND (x, 0);
288 TREE_ADDRESSABLE (x) = 1;
289 #if 0 /* poplevel deals with this now. */
290 if (DECL_CONTEXT (x) == 0)
291 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
299 /* Thorough checking of the arrayness of TYPE. */
302 is_array_type_p (type)
305 return TREE_CODE (type) == POINTER_TYPE
306 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
307 && TYPE_ARRAY_P (TREE_TYPE (type));
310 /* Return the length of a Java array type.
311 Return -1 if the length is unknown or non-constant. */
314 java_array_type_length (array_type)
318 if (TREE_CODE (array_type) == POINTER_TYPE)
319 array_type = TREE_TYPE (array_type);
320 arfld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
321 if (arfld != NULL_TREE)
323 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
324 tree high = TYPE_MAX_VALUE (index_type);
325 if (TREE_CODE (high) == INTEGER_CST)
326 return TREE_INT_CST_LOW (high) + 1;
332 build_prim_array_type (element_type, length)
334 HOST_WIDE_INT length;
336 tree max_index = build_int_2 (length - 1, 0);
337 TREE_TYPE (max_index) = sizetype;
338 return build_array_type (element_type, build_index_type (max_index));
341 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
342 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
343 The LENGTH is -1 if the length is unknown. */
346 build_java_array_type (element_type, length)
348 HOST_WIDE_INT length;
352 tree elsig = build_java_signature (element_type);
353 tree el_name = element_type;
354 sprintf (buf, length >= 0 ? "[%d" : "[", length);
355 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
357 t = IDENTIFIER_SIGNATURE_TYPE (sig);
359 return TREE_TYPE (t);
361 IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
362 TYPE_ARRAY_P (t) = 1;
364 if (TREE_CODE (el_name) == POINTER_TYPE)
365 el_name = TREE_TYPE (el_name);
366 el_name = TYPE_NAME (el_name);
367 if (TREE_CODE (el_name) == TYPE_DECL)
368 el_name = DECL_NAME (el_name);
369 TYPE_NAME (t) = identifier_subst (el_name, "", '.', '.', "[]");
371 set_java_signature (t, sig);
372 set_super_info (0, t, object_type_node, 0);
373 if (TREE_CODE (element_type) == RECORD_TYPE)
374 element_type = promote_type (element_type);
375 TYPE_ARRAY_ELEMENT (t) = element_type;
377 /* Add length pseudo-field. */
378 push_obstacks (&permanent_obstack, &permanent_obstack);
379 fld = build_decl (FIELD_DECL, get_identifier ("length"), int_type_node);
380 TYPE_FIELDS (t) = fld;
381 DECL_CONTEXT (fld) = t;
382 FIELD_PUBLIC (fld) = 1;
383 FIELD_FINAL (fld) = 1;
387 tree atype = build_prim_array_type (element_type, length);
388 tree arfld = build_decl (FIELD_DECL, get_identifier ("data"), atype);
389 DECL_CONTEXT (arfld) = t;
390 TREE_CHAIN (fld) = arfld;
393 TYPE_ALIGN (t) = TYPE_ALIGN (element_type);
396 /* We could layout_class, but that loads java.lang.Object prematurely.
397 * This is called by the parser, and it is a bad idea to do load_class
398 * in the middle of parsing, because of possible circularity problems. */
399 push_super_field (t, object_type_node);
405 /* Promote TYPE to the type actually used for fields and parameters. */
411 switch (TREE_CODE (type))
414 return build_pointer_type (CLASS_TO_HANDLE_TYPE (type));
416 if (type == boolean_type_node)
417 return promoted_boolean_type_node;
420 if (type == char_type_node)
421 return promoted_char_type_node;
425 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
427 if (type == short_type_node)
428 return promoted_short_type_node;
429 if (type == byte_type_node)
430 return promoted_byte_type_node;
431 return int_type_node;
433 /* ... else fall through ... */
439 /* Parse a signature string, starting at *PTR and ending at LIMIT.
440 Return the seen TREE_TYPE, updating *PTR. */
443 parse_signature_type (ptr, limit)
444 const unsigned char **ptr, *limit;
448 fatal ("bad signature string");
451 case 'B': (*ptr)++; return byte_type_node;
452 case 'C': (*ptr)++; return char_type_node;
453 case 'D': (*ptr)++; return double_type_node;
454 case 'F': (*ptr)++; return float_type_node;
455 case 'S': (*ptr)++; return short_type_node;
456 case 'I': (*ptr)++; return int_type_node;
457 case 'J': (*ptr)++; return long_type_node;
458 case 'Z': (*ptr)++; return boolean_type_node;
459 case 'V': (*ptr)++; return void_type_node;
461 for ((*ptr)++; (*ptr) < limit && ISDIGIT (**ptr); ) (*ptr)++;
462 type = parse_signature_type (ptr, limit);
463 type = build_java_array_type (type, -1);
467 const unsigned char *start = ++(*ptr);
468 register const unsigned char *str = start;
472 fatal ("bad signature string");
477 type = lookup_class (unmangle_classname (start, str - start));
481 fatal ("unrecognized signature string");
483 return promote_type (type);
486 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
487 and SIG_LENGTH bytes long.
488 Return a gcc type node. */
491 parse_signature_string (sig_string, sig_length)
492 const unsigned char *sig_string;
496 const unsigned char *str = sig_string;
497 const unsigned char *limit = str + sig_length;
499 push_obstacks (&permanent_obstack, &permanent_obstack);
500 if (str < limit && str[0] == '(')
502 tree argtype_list = NULL_TREE;
504 while (str < limit && str[0] != ')')
506 tree argtype = parse_signature_type (&str, limit);
507 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
509 if (str++, str >= limit)
510 fatal ("bad signature string");
511 result_type = parse_signature_type (&str, limit);
512 argtype_list = chainon (nreverse (argtype_list), end_params_node);
513 result_type = build_function_type (result_type, argtype_list);
516 result_type = parse_signature_type (&str, limit);
518 error ("junk at end of signature string");
523 /* Convert a signature to its type.
524 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
528 get_type_from_signature (tree signature)
530 unsigned char *sig = (unsigned char *) IDENTIFIER_POINTER (signature);
531 int len = IDENTIFIER_LENGTH (signature);
533 /* Primitive types aren't cached. */
535 return parse_signature_string (sig, len);
536 type = IDENTIFIER_SIGNATURE_TYPE (signature);
537 if (type == NULL_TREE)
539 type = parse_signature_string (sig, len);
540 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
545 /* Return the signature string for the arguments of method type TYPE. */
548 build_java_argument_signature (type)
551 extern struct obstack temporary_obstack;
552 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
553 if (sig == NULL_TREE)
555 tree args = TYPE_ARG_TYPES (type);
556 if (TREE_CODE (type) == METHOD_TYPE)
557 args = TREE_CHAIN (args); /* Skip "this" argument. */
558 for (; args != end_params_node; args = TREE_CHAIN (args))
560 tree t = build_java_signature (TREE_VALUE (args));
561 obstack_grow (&temporary_obstack,
562 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
564 obstack_1grow (&temporary_obstack, '\0');
566 sig = get_identifier (obstack_base (&temporary_obstack));
567 TYPE_ARGUMENT_SIGNATURE (type) = sig;
568 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
573 /* Return the signature of the given TYPE. */
576 build_java_signature (type)
580 push_obstacks (&permanent_obstack, &permanent_obstack);
581 while (TREE_CODE (type) == POINTER_TYPE)
582 type = TREE_TYPE (type);
583 if (TYPE_LANG_SPECIFIC (type) == NULL)
585 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
586 perm_calloc (1, sizeof (struct lang_type));
588 sig = TYPE_LANG_SPECIFIC (type)->signature;
589 if (sig == NULL_TREE)
592 switch (TREE_CODE (type))
594 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
595 case CHAR_TYPE: sg[0] = 'C'; goto native;
596 case VOID_TYPE: sg[0] = 'V'; goto native;
598 switch (TYPE_PRECISION (type))
600 case 8: sg[0] = 'B'; goto native;
601 case 16: sg[0] = 'S'; goto native;
602 case 32: sg[0] = 'I'; goto native;
603 case 64: sg[0] = 'J'; goto native;
604 default: goto bad_type;
607 switch (TYPE_PRECISION (type))
609 case 32: sg[0] = 'F'; goto native;
610 case 64: sg[0] = 'D'; goto native;
611 default: goto bad_type;
615 sig = get_identifier (sg);
618 if (TYPE_ARRAY_P (type))
620 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
621 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
626 t = DECL_NAME (TYPE_NAME (type));
627 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
634 extern struct obstack temporary_obstack;
635 sig = build_java_argument_signature (type);
636 obstack_1grow (&temporary_obstack, '(');
637 obstack_grow (&temporary_obstack,
638 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
639 obstack_1grow (&temporary_obstack, ')');
641 t = build_java_signature (TREE_TYPE (type));
642 obstack_grow0 (&temporary_obstack,
643 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
645 sig = get_identifier (obstack_base (&temporary_obstack));
646 obstack_free (&temporary_obstack,
647 obstack_base (&temporary_obstack));
652 fatal ("internal error - build_java_signature passed invalid type");
654 TYPE_LANG_SPECIFIC (type)->signature = sig;
660 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
663 set_java_signature (type, sig)
668 while (TREE_CODE (type) == POINTER_TYPE)
669 type = TREE_TYPE (type);
670 if (TYPE_LANG_SPECIFIC (type) == NULL)
672 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
673 perm_calloc (1, sizeof (struct lang_type));
676 old_sig = TYPE_LANG_SPECIFIC (type)->signature;
677 if (old_sig != NULL_TREE && old_sig != sig)
678 fatal ("internal error - set_java_signature");
679 TYPE_LANG_SPECIFIC (type)->signature = sig;
680 #if 0 /* careful about METHOD_TYPE */
681 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
682 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
686 /* Search in class CLAS (and its superclasses) for a method
687 matching METHOD_NAME and argument signature METHOD_SIGNATURE.
688 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
689 (Contrast lookup_java_method, which takes into account return type.) */
692 lookup_argument_method (clas, method_name, method_signature)
693 tree clas, method_name, method_signature;
696 while (clas != NULL_TREE)
698 for (method = TYPE_METHODS (clas);
699 method != NULL_TREE; method = TREE_CHAIN (method))
701 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
702 tree name = DECL_NAME (method);
703 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
704 EXPR_WFL_NODE (name) : name) == method_name
705 && method_sig == method_signature)
708 clas = CLASSTYPE_SUPER (clas);
713 /* Search in class CLAS (and its superclasses) for a method
714 matching METHOD_NAME and signature METHOD_SIGNATURE.
715 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
716 (Contrast lookup_argument_method, which ignores return type.) */
719 lookup_java_method (clas, method_name, method_signature)
720 tree clas, method_name, method_signature;
723 while (clas != NULL_TREE)
725 for (method = TYPE_METHODS (clas);
726 method != NULL_TREE; method = TREE_CHAIN (method))
728 tree method_sig = build_java_signature (TREE_TYPE (method));
729 if (DECL_NAME (method) == method_name
730 && method_sig == method_signature)
733 clas = CLASSTYPE_SUPER (clas);
738 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
739 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
742 lookup_java_constructor (clas, method_signature)
743 tree clas, method_signature;
745 tree method = TYPE_METHODS (clas);
746 for ( ; method != NULL_TREE; method = TREE_CHAIN (method))
748 tree method_sig = build_java_signature (TREE_TYPE (method));
749 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
755 /* Return a type which is the Binary Numeric Promotion of the pair T1,
756 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
757 Promotion. It assumes that both T1 and T2 are elligible to BNP. */
760 binary_numeric_promotion (t1, t2, exp1, exp2)
766 if (t1 == double_type_node || t2 == double_type_node)
768 if (t1 != double_type_node)
769 *exp1 = convert (double_type_node, *exp1);
770 if (t2 != double_type_node)
771 *exp2 = convert (double_type_node, *exp2);
772 return double_type_node;
774 if (t1 == float_type_node || t2 == float_type_node)
776 if (t1 != float_type_node)
777 *exp1 = convert (float_type_node, *exp1);
778 if (t2 != float_type_node)
779 *exp2 = convert (float_type_node, *exp2);
780 return float_type_node;
782 if (t1 == long_type_node || t2 == long_type_node)
784 if (t1 != long_type_node)
785 *exp1 = convert (long_type_node, *exp1);
786 if (t2 != long_type_node)
787 *exp2 = convert (long_type_node, *exp2);
788 return long_type_node;
791 if (t1 != int_type_node)
792 *exp1 = convert (int_type_node, *exp1);
793 if (t2 != int_type_node)
794 *exp2 = convert (int_type_node, *exp2);
795 return int_type_node;