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> */
31 #include "java-tree.h"
37 extern struct obstack permanent_obstack;
39 /* Set the type of the local variable with index SLOT to TYPE. */
42 set_local_type (slot, type)
46 int max_locals = DECL_MAX_LOCALS(current_function_decl);
47 int nslots = TYPE_IS_WIDE (type) ? 2 : 1;
48 if (slot < 0 || slot + nslots - 1 >= max_locals)
49 fatal ("invalid local variable index");
50 type_map[slot] = type;
52 type_map[++slot] = void_type_node;
55 extern tree convert_to_integer (tree type, tree expr);
56 extern tree convert_to_real (tree type, tree expr);
57 extern tree convert_to_pointer (tree type, tree expr);
59 /* Create an expression whose value is that of EXPR,
60 converted to type TYPE. The TREE_TYPE of the value
61 is always TYPE. This function implements all reasonable
62 conversions; callers should filter out those that are
63 not permitted by the language being compiled. */
69 register enum tree_code code = TREE_CODE (type);
71 if (type == TREE_TYPE (expr)
72 || TREE_CODE (expr) == ERROR_MARK)
74 if (TREE_CODE (TREE_TYPE (expr)) == ERROR_MARK)
75 return error_mark_node;
76 if (code == BOOLEAN_TYPE)
77 return fold (convert_to_boolean (type, expr));
78 if (code == INTEGER_TYPE)
79 return fold (convert_to_integer (type, expr));
80 if (code == REAL_TYPE)
81 return fold (convert_to_real (type, expr));
82 if (code == CHAR_TYPE)
83 return fold (convert_to_char (type, expr));
84 if (code == POINTER_TYPE)
85 return fold (convert_to_pointer (type, expr));
86 error ("conversion to non-scalar type requested");
87 return error_mark_node;
91 convert_to_char (type, expr)
94 return build1 (NOP_EXPR, type, expr);
98 convert_to_boolean (type, expr)
101 return build1 (NOP_EXPR, type, expr);
104 /* Print an error message for invalid use of an incomplete type.
105 VALUE is the expression that was used (or 0 if that isn't known)
106 and TYPE is the type that was invalid. */
109 incomplete_type_error (value, type)
113 error ("internal error - use of undefined type");
116 /* Return a data type that has machine mode MODE.
117 If the mode is an integer,
118 then UNSIGNEDP selects between signed and unsigned types. */
121 type_for_mode (mode, unsignedp)
122 enum machine_mode mode;
125 if (mode == TYPE_MODE (int_type_node))
126 return unsignedp ? unsigned_int_type_node : int_type_node;
127 if (mode == TYPE_MODE (long_type_node))
128 return unsignedp ? unsigned_long_type_node : long_type_node;
129 if (mode == TYPE_MODE (short_type_node))
130 return unsignedp ? unsigned_short_type_node : short_type_node;
131 if (mode == TYPE_MODE (byte_type_node))
132 return unsignedp ? unsigned_byte_type_node : byte_type_node;
133 if (mode == TYPE_MODE (float_type_node))
134 return float_type_node;
135 if (mode == TYPE_MODE (double_type_node))
136 return double_type_node;
141 /* Return an integer type with BITS bits of precision,
142 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
145 type_for_size (bits, unsignedp)
149 if (bits <= TYPE_PRECISION (byte_type_node))
150 return unsignedp ? unsigned_byte_type_node : byte_type_node;
151 if (bits <= TYPE_PRECISION (short_type_node))
152 return unsignedp ? unsigned_short_type_node : short_type_node;
153 if (bits <= TYPE_PRECISION (int_type_node))
154 return unsignedp ? unsigned_int_type_node : int_type_node;
155 if (bits <= TYPE_PRECISION (long_type_node))
156 return unsignedp ? unsigned_long_type_node : long_type_node;
160 /* Return a type the same as TYPE except unsigned or
161 signed according to UNSIGNEDP. */
164 signed_or_unsigned_type (unsignedp, type)
168 if (! INTEGRAL_TYPE_P (type))
170 if (TYPE_PRECISION (type) == TYPE_PRECISION (int_type_node))
171 return unsignedp ? unsigned_int_type_node : int_type_node;
172 if (TYPE_PRECISION (type) == TYPE_PRECISION (byte_type_node))
173 return unsignedp ? unsigned_byte_type_node : byte_type_node;
174 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_type_node))
175 return unsignedp ? unsigned_short_type_node : short_type_node;
176 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_type_node))
177 return unsignedp ? unsigned_long_type_node : long_type_node;
181 /* Return a signed type the same as TYPE in other respects. */
187 return signed_or_unsigned_type (0, type);
190 /* Return an unsigned type the same as TYPE in other respects. */
196 return signed_or_unsigned_type (1, type);
200 /* Mark EXP saying that we need to be able to take the
201 address of it; it should not be allocated in a register.
202 Value is 1 if successful. */
205 mark_addressable (exp)
208 register tree x = exp;
210 switch (TREE_CODE (x))
217 x = TREE_OPERAND (x, 0);
220 case TRUTH_ANDIF_EXPR:
221 case TRUTH_ORIF_EXPR:
223 x = TREE_OPERAND (x, 1);
227 return mark_addressable (TREE_OPERAND (x, 1))
228 & mark_addressable (TREE_OPERAND (x, 2));
231 TREE_ADDRESSABLE (x) = 1;
235 /* We sometimes add a cast *(TYPE*)&FOO to handle type and mode
236 incompatibility problems. Handle this case by marking FOO. */
237 if (TREE_CODE (TREE_OPERAND (x, 0)) == NOP_EXPR
238 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (x, 0), 0)) == ADDR_EXPR)
240 x = TREE_OPERAND (TREE_OPERAND (x, 0), 0);
243 if (TREE_CODE (TREE_OPERAND (x, 0)) == ADDR_EXPR)
245 x = TREE_OPERAND (x, 0);
255 TREE_ADDRESSABLE (x) = 1;
256 #if 0 /* poplevel deals with this now. */
257 if (DECL_CONTEXT (x) == 0)
258 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (x)) = 1;
266 /* Thorough checking of the arrayness of TYPE. */
269 is_array_type_p (type)
272 return TREE_CODE (type) == POINTER_TYPE
273 && TREE_CODE (TREE_TYPE (type)) == RECORD_TYPE
274 && TYPE_ARRAY_P (TREE_TYPE (type));
277 /* Return the length of a Java array type.
278 Return -1 if the length is unknown or non-constant. */
281 java_array_type_length (array_type)
285 if (TREE_CODE (array_type) == POINTER_TYPE)
286 array_type = TREE_TYPE (array_type);
287 arfld = TREE_CHAIN (TREE_CHAIN (TYPE_FIELDS (array_type)));
288 if (arfld != NULL_TREE)
290 tree index_type = TYPE_DOMAIN (TREE_TYPE (arfld));
291 tree high = TYPE_MAX_VALUE (index_type);
292 if (TREE_CODE (high) == INTEGER_CST)
293 return TREE_INT_CST_LOW (high) + 1;
299 build_prim_array_type (element_type, length)
301 HOST_WIDE_INT length;
303 tree max_index = build_int_2 (length - 1, 0);
304 TREE_TYPE (max_index) = sizetype;
305 return build_array_type (element_type, build_index_type (max_index));
308 /* Return a Java array type with a given ELEMENT_TYPE and LENGTH.
309 These are hashed (shared) using IDENTIFIER_SIGNATURE_TYPE.
310 The LENGTH is -1 if the length is unknown. */
313 build_java_array_type (element_type, length)
315 HOST_WIDE_INT length;
319 tree elsig = build_java_signature (element_type);
320 tree el_name = element_type;
321 sprintf (buf, length >= 0 ? "[%d" : "[", length);
322 sig = ident_subst (IDENTIFIER_POINTER (elsig), IDENTIFIER_LENGTH (elsig),
324 t = IDENTIFIER_SIGNATURE_TYPE (sig);
326 return TREE_TYPE (t);
328 IDENTIFIER_SIGNATURE_TYPE (sig) = build_pointer_type (t);
329 TYPE_ARRAY_P (t) = 1;
331 if (TREE_CODE (el_name) == POINTER_TYPE)
332 el_name = TREE_TYPE (el_name);
333 el_name = TYPE_NAME (el_name);
334 if (TREE_CODE (el_name) == TYPE_DECL)
335 el_name = DECL_NAME (el_name);
336 TYPE_NAME (t) = identifier_subst (el_name, "", '.', '.', "[]");
338 set_java_signature (t, sig);
339 set_super_info (0, t, object_type_node, 0);
340 if (TREE_CODE (element_type) == RECORD_TYPE)
341 element_type = promote_type (element_type);
342 TYPE_ARRAY_ELEMENT (t) = element_type;
344 /* Add length pseudo-field. */
345 push_obstacks (&permanent_obstack, &permanent_obstack);
346 fld = build_decl (FIELD_DECL, get_identifier ("length"), int_type_node);
347 TYPE_FIELDS (t) = fld;
348 DECL_CONTEXT (fld) = t;
349 FIELD_PUBLIC (fld) = 1;
350 FIELD_FINAL (fld) = 1;
354 tree atype = build_prim_array_type (element_type, length);
355 tree arfld = build_decl (FIELD_DECL, get_identifier ("data"), atype);
356 DECL_CONTEXT (arfld) = t;
357 TREE_CHAIN (fld) = arfld;
360 TYPE_ALIGN (t) = TYPE_ALIGN (element_type);
367 /* Promote TYPE to the type actually used for fields and parameters. */
373 switch (TREE_CODE (type))
376 return build_pointer_type (CLASS_TO_HANDLE_TYPE (type));
378 if (type == boolean_type_node)
379 return promoted_boolean_type_node;
382 if (type == char_type_node)
383 return promoted_char_type_node;
387 if (TYPE_PRECISION (type) < TYPE_PRECISION (int_type_node))
389 if (type == short_type_node)
390 return promoted_short_type_node;
391 if (type == byte_type_node)
392 return promoted_byte_type_node;
393 return int_type_node;
395 /* ... else fall through ... */
401 /* Parse a signature string, starting at *PTR and ending at LIMIT.
402 Return the seen TREE_TYPE, updating *PTR. */
405 parse_signature_type (ptr, limit)
406 const unsigned char **ptr, *limit;
410 fatal ("bad signature string");
413 case 'B': (*ptr)++; return byte_type_node;
414 case 'C': (*ptr)++; return char_type_node;
415 case 'D': (*ptr)++; return double_type_node;
416 case 'F': (*ptr)++; return float_type_node;
417 case 'S': (*ptr)++; return short_type_node;
418 case 'I': (*ptr)++; return int_type_node;
419 case 'J': (*ptr)++; return long_type_node;
420 case 'Z': (*ptr)++; return boolean_type_node;
421 case 'V': (*ptr)++; return void_type_node;
423 for ((*ptr)++; (*ptr) < limit && isdigit (**ptr); ) (*ptr)++;
424 type = parse_signature_type (ptr, limit);
425 type = build_java_array_type (type, -1);
429 const unsigned char *start = ++(*ptr);
430 register const unsigned char *str = start;
434 fatal ("bad signature string");
439 type = lookup_class (unmangle_classname (start, str - start));
443 fatal ("unrecognized signature string");
445 return promote_type (type);
448 /* Parse a Java "mangled" signature string, starting at SIG_STRING,
449 and SIG_LENGTH bytes long.
450 Return a gcc type node. */
453 parse_signature_string (sig_string, sig_length)
454 const unsigned char *sig_string;
458 const unsigned char *str = sig_string;
459 const unsigned char *limit = str + sig_length;
461 push_obstacks (&permanent_obstack, &permanent_obstack);
462 if (str < limit && str[0] == '(')
464 tree argtype_list = NULL_TREE;
466 while (str < limit && str[0] != ')')
468 tree argtype = parse_signature_type (&str, limit);
469 argtype_list = tree_cons (NULL_TREE, argtype, argtype_list);
471 if (str++, str >= limit)
472 fatal ("bad signature string");
473 result_type = parse_signature_type (&str, limit);
474 result_type = build_function_type (result_type,
475 nreverse (argtype_list));
478 result_type = parse_signature_type (&str, limit);
480 error ("junk at end of signature string");
485 /* Convert a signature to its type.
486 * Uses IDENTIFIER_SIGNATURE_TYPE as a cache (except for primitive types).
490 get_type_from_signature (tree signature)
492 unsigned char *sig = (unsigned char *) IDENTIFIER_POINTER (signature);
493 int len = IDENTIFIER_LENGTH (signature);
495 /* Primitive types aren't cached. */
497 return parse_signature_string (sig, len);
498 type = IDENTIFIER_SIGNATURE_TYPE (signature);
499 if (type == NULL_TREE)
501 type = parse_signature_string (sig, len);
502 IDENTIFIER_SIGNATURE_TYPE (signature) = type;
507 /* Return the signature string for the arguments of method type TYPE. */
510 build_java_argument_signature (type)
513 extern struct obstack temporary_obstack;
514 tree sig = TYPE_ARGUMENT_SIGNATURE (type);
515 if (sig == NULL_TREE)
517 tree args = TYPE_ARG_TYPES (type);
518 if (TREE_CODE (type) == METHOD_TYPE)
519 args = TREE_CHAIN (args); /* Skip "this" argument. */
520 for (; args != NULL_TREE; args = TREE_CHAIN (args))
522 tree t = build_java_signature (TREE_VALUE (args));
523 obstack_grow (&temporary_obstack,
524 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
526 obstack_1grow (&temporary_obstack, '\0');
528 sig = get_identifier (obstack_base (&temporary_obstack));
529 TYPE_ARGUMENT_SIGNATURE (type) = sig;
530 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
535 /* Return the signature of the given TYPE. */
538 build_java_signature (type)
542 push_obstacks (&permanent_obstack, &permanent_obstack);
543 while (TREE_CODE (type) == POINTER_TYPE)
544 type = TREE_TYPE (type);
545 if (TYPE_LANG_SPECIFIC (type) == NULL)
547 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
548 perm_calloc (1, sizeof (struct lang_type));
550 sig = TYPE_LANG_SPECIFIC (type)->signature;
551 if (sig == NULL_TREE)
554 switch (TREE_CODE (type))
556 case BOOLEAN_TYPE: sg[0] = 'Z'; goto native;
557 case CHAR_TYPE: sg[0] = 'C'; goto native;
558 case VOID_TYPE: sg[0] = 'V'; goto native;
560 switch (TYPE_PRECISION (type))
562 case 8: sg[0] = 'B'; goto native;
563 case 16: sg[0] = 'S'; goto native;
564 case 32: sg[0] = 'I'; goto native;
565 case 64: sg[0] = 'J'; goto native;
566 default: goto bad_type;
569 switch (TYPE_PRECISION (type))
571 case 32: sg[0] = 'F'; goto native;
572 case 64: sg[0] = 'D'; goto native;
573 default: goto bad_type;
577 sig = get_identifier (sg);
580 if (TYPE_ARRAY_P (type))
582 t = build_java_signature (TYPE_ARRAY_ELEMENT (type));
583 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
588 t = DECL_NAME (TYPE_NAME (type));
589 sig = ident_subst (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t),
596 extern struct obstack temporary_obstack;
597 sig = build_java_argument_signature (type);
598 obstack_1grow (&temporary_obstack, '(');
599 obstack_grow (&temporary_obstack,
600 IDENTIFIER_POINTER (sig), IDENTIFIER_LENGTH (sig));
601 obstack_1grow (&temporary_obstack, ')');
603 t = build_java_signature (TREE_TYPE (type));
604 obstack_grow0 (&temporary_obstack,
605 IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
607 sig = get_identifier (obstack_base (&temporary_obstack));
608 obstack_free (&temporary_obstack,
609 obstack_base (&temporary_obstack));
614 fatal ("internal error - build_java_signature passed invalid type");
616 TYPE_LANG_SPECIFIC (type)->signature = sig;
622 /* Save signature string SIG (an IDENTIFIER_NODE) in TYPE for future use. */
625 set_java_signature (type, sig)
630 while (TREE_CODE (type) == POINTER_TYPE)
631 type = TREE_TYPE (type);
632 if (TYPE_LANG_SPECIFIC (type) == NULL)
634 TYPE_LANG_SPECIFIC (type) = (struct lang_type *)
635 perm_calloc (1, sizeof (struct lang_type));
638 old_sig = TYPE_LANG_SPECIFIC (type)->signature;
639 if (old_sig != NULL_TREE && old_sig != sig)
640 fatal ("internal error - set_java_signature");
641 TYPE_LANG_SPECIFIC (type)->signature = sig;
642 #if 0 /* careful about METHOD_TYPE */
643 if (IDENTIFIER_SIGNATURE_TYPE (sig) == NULL_TREE && TREE_PERMANENT (type))
644 IDENTIFIER_SIGNATURE_TYPE (sig) = type;
648 /* Search in class CLAS (and its superclasses) for a method
649 matching METHOD_NAME and argument signature METHOD_SIGNATURE.
650 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
651 (Contrast lookup_java_method, which takes into account return type.) */
654 lookup_argument_method (clas, method_name, method_signature)
655 tree clas, method_name, method_signature;
658 while (clas != NULL_TREE)
660 for (method = TYPE_METHODS (clas);
661 method != NULL_TREE; method = TREE_CHAIN (method))
663 tree method_sig = build_java_argument_signature (TREE_TYPE (method));
664 tree name = DECL_NAME (method);
665 if ((TREE_CODE (name) == EXPR_WITH_FILE_LOCATION ?
666 EXPR_WFL_NODE (DECL_NAME (method)) : name) == method_name
667 && method_sig == method_signature)
670 clas = CLASSTYPE_SUPER (clas);
675 /* Search in class CLAS (and its superclasses) for a method
676 matching METHOD_NAME and signature METHOD_SIGNATURE.
677 Return a FUNCTION_DECL on success, or NULL_TREE if none found.
678 (Contrast lookup_argument_method, which ignores return type.) */
681 lookup_java_method (clas, method_name, method_signature)
682 tree clas, method_name, method_signature;
685 while (clas != NULL_TREE)
687 for (method = TYPE_METHODS (clas);
688 method != NULL_TREE; method = TREE_CHAIN (method))
690 tree method_sig = build_java_signature (TREE_TYPE (method));
691 if (DECL_NAME (method) == method_name
692 && method_sig == method_signature)
695 clas = CLASSTYPE_SUPER (clas);
700 /* Search in class CLAS for a constructor matching METHOD_SIGNATURE.
701 Return a FUNCTION_DECL on success, or NULL_TREE if none found. */
704 lookup_java_constructor (clas, method_signature)
705 tree clas, method_signature;
707 tree method = TYPE_METHODS (clas);
708 for ( ; method != NULL_TREE; method = TREE_CHAIN (method))
710 tree method_sig = build_java_signature (TREE_TYPE (method));
711 if (DECL_CONSTRUCTOR_P (method) && method_sig == method_signature)
717 /* Return a type which is the Binary Numeric Promotion of the pair T1,
718 T2 and convert EXP1 and/or EXP2. See 5.6.2 Binary Numeric
719 Promotion. It assumes that both T1 and T2 are elligible to BNP. */
722 binary_numeric_promotion (t1, t2, exp1, exp2)
728 if (t1 == double_type_node || t2 == double_type_node)
730 if (t1 != double_type_node)
731 *exp1 = convert (double_type_node, *exp1);
732 if (t2 != double_type_node)
733 *exp2 = convert (double_type_node, *exp2);
734 return double_type_node;
736 if (t1 == float_type_node || t2 == float_type_node)
738 if (t1 != float_type_node)
739 *exp1 = convert (float_type_node, *exp1);
740 if (t2 != float_type_node)
741 *exp2 = convert (float_type_node, *exp2);
742 return float_type_node;
744 if (t1 == long_type_node || t2 == long_type_node)
746 if (t1 != long_type_node)
747 *exp1 = convert (long_type_node, *exp1);
748 if (t2 != long_type_node)
749 *exp2 = convert (long_type_node, *exp2);
750 return long_type_node;
753 if (t1 != int_type_node)
754 *exp1 = convert (int_type_node, *exp1);
755 if (t2 != int_type_node)
756 *exp2 = convert (int_type_node, *exp2);
757 return int_type_node;