2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / java / class.c
blob6c772e0a8081b5cfab60963f609497a49a64cdb3
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "java-tree.h"
36 #include "jcf.h"
37 #include "obstack.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "parse.h"
41 #include "function.h"
42 #include "ggc.h"
43 #include "stdio.h"
44 #include "target.h"
46 /* DOS brain-damage */
47 #ifndef O_BINARY
48 #define O_BINARY 0 /* MS-DOS brain-damage */
49 #endif
51 static tree make_method_value (tree);
52 static tree build_java_method_type (tree, tree, int);
53 static int32 hashUtf8String (const char *, int);
54 static tree make_field_value (tree);
55 static tree get_dispatch_vector (tree);
56 static tree get_dispatch_table (tree, tree);
57 static int supers_all_compiled (tree type);
58 static void add_interface_do (tree, tree, int);
59 static tree maybe_layout_super_class (tree, tree);
60 static void add_miranda_methods (tree, tree);
61 static int assume_compiled (const char *);
62 static tree build_symbol_entry (tree);
64 static GTY(()) rtx registerClass_libfunc;
66 struct obstack temporary_obstack;
68 /* The compiler generates different code depending on whether or not
69 it can assume certain classes have been compiled down to native
70 code or not. The compiler options -fassume-compiled= and
71 -fno-assume-compiled= are used to create a tree of
72 assume_compiled_node objects. This tree is queried to determine if
73 a class is assume to be compiled or not. Each node in the tree
74 represents either a package or a specific class. */
76 typedef struct assume_compiled_node_struct
78 /* The class or package name. */
79 const char *ident;
81 /* Nonzero if this represents an exclusion. */
82 int excludep;
84 /* Pointers to other nodes in the tree. */
85 struct assume_compiled_node_struct *parent;
86 struct assume_compiled_node_struct *sibling;
87 struct assume_compiled_node_struct *child;
88 } assume_compiled_node;
90 static assume_compiled_node *find_assume_compiled_node (assume_compiled_node *,
91 const char *);
93 /* This is the root of the include/exclude tree. */
95 static assume_compiled_node *assume_compiled_tree;
97 static GTY(()) tree class_roots[5];
98 #define registered_class class_roots[0]
99 #define fields_ident class_roots[1] /* get_identifier ("fields") */
100 #define info_ident class_roots[2] /* get_identifier ("info") */
101 #define class_list class_roots[3]
102 #define class_dtable_decl class_roots[4]
104 /* Return the node that most closely represents the class whose name
105 is IDENT. Start the search from NODE. Return NULL if an
106 appropriate node does not exist. */
108 static assume_compiled_node *
109 find_assume_compiled_node (assume_compiled_node *node, const char *ident)
111 while (node)
113 size_t node_ident_length = strlen (node->ident);
115 /* node_ident_length is zero at the root of the tree. If the
116 identifiers are the same length, then we have matching
117 classes. Otherwise check if we've matched an enclosing
118 package name. */
120 if (node_ident_length == 0
121 || (strncmp (ident, node->ident, node_ident_length) == 0
122 && (strlen (ident) == node_ident_length
123 || ident[node_ident_length] == '.')))
125 /* We've found a match, however, there might be a more
126 specific match. */
128 assume_compiled_node *found = find_assume_compiled_node (node->child,
129 ident);
130 if (found)
131 return found;
132 else
133 return node;
136 /* No match yet. Continue through the sibling list. */
137 node = node->sibling;
140 /* No match at all in this tree. */
141 return NULL;
144 /* Add a new IDENT to the include/exclude tree. It's an exclusion
145 if EXCLUDEP is nonzero. */
147 void
148 add_assume_compiled (const char *ident, int excludep)
150 int len;
151 assume_compiled_node *parent;
152 assume_compiled_node *node = xmalloc (sizeof (assume_compiled_node));
154 node->ident = xstrdup (ident);
155 node->excludep = excludep;
156 node->child = NULL;
158 /* Create the root of the tree if it doesn't exist yet. */
160 if (NULL == assume_compiled_tree)
162 assume_compiled_tree = xmalloc (sizeof (assume_compiled_node));
163 assume_compiled_tree->ident = "";
164 assume_compiled_tree->excludep = 0;
165 assume_compiled_tree->sibling = NULL;
166 assume_compiled_tree->child = NULL;
167 assume_compiled_tree->parent = NULL;
170 /* Calling the function with the empty string means we're setting
171 excludep for the root of the hierarchy. */
173 if (0 == ident[0])
175 assume_compiled_tree->excludep = excludep;
176 return;
179 /* Find the parent node for this new node. PARENT will either be a
180 class or a package name. Adjust PARENT accordingly. */
182 parent = find_assume_compiled_node (assume_compiled_tree, ident);
183 len = strlen (parent->ident);
184 if (parent->ident[len] && parent->ident[len] != '.')
185 parent = parent->parent;
187 /* Insert NODE into the tree. */
189 node->parent = parent;
190 node->sibling = parent->child;
191 parent->child = node;
194 /* Returns nonzero if IDENT is the name of a class that the compiler
195 should assume has been compiled to object code. */
197 static int
198 assume_compiled (const char *ident)
200 assume_compiled_node *i;
201 int result;
203 if (NULL == assume_compiled_tree)
204 return 1;
206 i = find_assume_compiled_node (assume_compiled_tree,
207 ident);
209 result = ! i->excludep;
211 return (result);
214 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
215 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
216 Also, PREFIX is prepended, and SUFFIX is appended. */
218 tree
219 ident_subst (const char* old_name,
220 int old_length,
221 const char *prefix,
222 int old_char,
223 int new_char,
224 const char *suffix)
226 int prefix_len = strlen (prefix);
227 int suffix_len = strlen (suffix);
228 int i = prefix_len + old_length + suffix_len + 1;
229 #ifdef __GNUC__
230 char buffer[i];
231 #else
232 char *buffer = alloca (i);
233 #endif
234 strcpy (buffer, prefix);
235 for (i = 0; i < old_length; i++)
237 char ch = old_name[i];
238 if (ch == old_char)
239 ch = new_char;
240 buffer[prefix_len + i] = ch;
242 strcpy (buffer + prefix_len + old_length, suffix);
243 return get_identifier (buffer);
246 /* Return an IDENTIFIER_NODE the same as OLD_ID,
247 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
248 Also, PREFIX is prepended, and SUFFIX is appended. */
250 tree
251 identifier_subst (const tree old_id,
252 const char *prefix,
253 int old_char,
254 int new_char,
255 const char *suffix)
257 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
258 prefix, old_char, new_char, suffix);
261 /* Generate a valid C identifier from the name of the class TYPE,
262 prefixed by PREFIX. */
264 tree
265 mangled_classname (const char *prefix, tree type)
267 tree ident = TYPE_NAME (type);
268 if (TREE_CODE (ident) != IDENTIFIER_NODE)
269 ident = DECL_NAME (ident);
270 return identifier_subst (ident, prefix, '.', '_', "");
273 tree
274 make_class (void)
276 tree type;
277 type = make_node (RECORD_TYPE);
278 TYPE_BINFO (type) = make_tree_vec (BINFO_ELTS);
279 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
281 return type;
284 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
285 and where each of the constituents is separated by '/',
286 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
288 tree
289 unmangle_classname (const char *name, int name_length)
291 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
292 /* It's not sufficient to compare to_return and get_identifier
293 (name) to determine whether to_return is qualified. There are
294 cases in signature analysis where name will be stripped of a
295 trailing ';'. */
296 name = IDENTIFIER_POINTER (to_return);
297 while (*name)
298 if (*name++ == '.')
300 QUALIFIED_P (to_return) = 1;
301 break;
304 return to_return;
307 tree
308 push_class (tree class_type, tree class_name)
310 tree decl, signature;
311 location_t saved_loc = input_location;
312 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
313 CLASS_P (class_type) = 1;
314 input_filename = IDENTIFIER_POINTER (source_name);
315 input_line = 0;
316 decl = build_decl (TYPE_DECL, class_name, class_type);
318 /* dbxout needs a DECL_SIZE if in gstabs mode */
319 DECL_SIZE (decl) = integer_zero_node;
321 input_location = saved_loc;
322 signature = identifier_subst (class_name, "L", '.', '/', ";");
323 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
325 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
326 both a typedef and in the struct name-space. We may want to re-visit
327 this later, but for now it reduces the changes needed for gdb. */
328 DECL_ARTIFICIAL (decl) = 1;
330 pushdecl_top_level (decl);
332 return decl;
335 /* Finds the (global) class named NAME. Creates the class if not found.
336 Also creates associated TYPE_DECL.
337 Does not check if the class actually exists, load the class,
338 fill in field or methods, or do layout_type. */
340 tree
341 lookup_class (tree name)
343 tree decl = IDENTIFIER_CLASS_VALUE (name);
344 if (decl == NULL_TREE)
345 decl = push_class (make_class (), name);
346 return TREE_TYPE (decl);
349 void
350 set_super_info (int access_flags, tree this_class,
351 tree super_class, int interfaces_count)
353 int total_supers = interfaces_count;
354 tree class_decl = TYPE_NAME (this_class);
355 if (super_class)
356 total_supers++;
358 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
359 if (super_class)
361 tree super_binfo = make_tree_vec (BINFO_ELTS);
362 BINFO_TYPE (super_binfo) = super_class;
363 BINFO_OFFSET (super_binfo) = integer_zero_node;
364 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
365 = super_binfo;
366 CLASS_HAS_SUPER (this_class) = 1;
369 set_class_decl_access_flags (access_flags, class_decl);
372 void
373 set_class_decl_access_flags (int access_flags, tree class_decl)
375 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
376 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
377 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
378 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
379 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
380 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
381 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
382 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
383 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
386 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
387 direct sub-classes of Object are 1, and so on. */
390 class_depth (tree clas)
392 int depth = 0;
393 if (! CLASS_LOADED_P (clas))
394 load_class (clas, 1);
395 if (TYPE_SIZE (clas) == error_mark_node)
396 return -1;
397 while (clas != object_type_node)
399 depth++;
400 clas = TYPE_BINFO_BASETYPE (clas, 0);
402 return depth;
405 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
408 interface_of_p (tree type1, tree type2)
410 int n, i;
411 tree basetype_vec;
413 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
414 return 0;
415 n = TREE_VEC_LENGTH (basetype_vec);
416 for (i = 0; i < n; i++)
418 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
419 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
420 return 1;
422 for (i = 0; i < n; i++)
424 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
425 if (vec_elt && BINFO_TYPE (vec_elt)
426 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
427 return 1;
429 return 0;
432 /* Return true iff TYPE1 inherits from TYPE2. */
435 inherits_from_p (tree type1, tree type2)
437 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
439 if (type1 == type2)
440 return 1;
441 type1 = CLASSTYPE_SUPER (type1);
443 return 0;
446 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
449 enclosing_context_p (tree type1, tree type2)
451 if (!INNER_CLASS_TYPE_P (type2))
452 return 0;
454 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
455 type2;
456 type2 = (INNER_CLASS_TYPE_P (type2) ?
457 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
459 if (type2 == type1)
460 return 1;
463 return 0;
466 /* Return 1 iff there exists a common enclosing context between TYPE1
467 and TYPE2. */
469 int common_enclosing_context_p (tree type1, tree type2)
471 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
472 return 0;
474 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
475 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
476 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
478 tree current;
479 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
480 current = (PURE_INNER_CLASS_TYPE_P (current) ?
481 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
482 NULL_TREE))
483 if (type1 == current)
484 return 1;
486 return 0;
489 static void
490 add_interface_do (tree basetype_vec, tree interface_class, int i)
492 tree interface_binfo = make_tree_vec (BINFO_ELTS);
493 BINFO_TYPE (interface_binfo) = interface_class;
494 BINFO_OFFSET (interface_binfo) = integer_zero_node;
495 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
496 TREE_VIA_VIRTUAL (interface_binfo) = 1;
497 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
500 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
501 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
502 if attempt is made to add it twice. */
504 tree
505 maybe_add_interface (tree this_class, tree interface_class)
507 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
508 int i;
509 int n = TREE_VEC_LENGTH (basetype_vec);
510 for (i = 0; ; i++)
512 if (i >= n)
514 error ("internal error - too many interface type");
515 return NULL_TREE;
517 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
518 break;
519 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
520 return interface_class;
522 add_interface_do (basetype_vec, interface_class, i);
523 return NULL_TREE;
526 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
528 void
529 add_interface (tree this_class, tree interface_class)
531 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
532 int i;
533 int n = TREE_VEC_LENGTH (basetype_vec);
534 for (i = 0; ; i++)
536 if (i >= n)
538 error ("internal error - too many interface type");
539 return;
541 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
542 break;
544 add_interface_do (basetype_vec, interface_class, i);
547 #if 0
548 /* Return the address of a pointer to the first FUNCTION_DECL
549 in the list (*LIST) whose DECL_NAME is NAME. */
551 static tree *
552 find_named_method (tree *list, tree name)
554 while (*list && DECL_NAME (*list) != name)
555 list = &TREE_CHAIN (*list);
556 return list;
558 #endif
560 static tree
561 build_java_method_type (tree fntype, tree this_class, int access_flags)
563 if (access_flags & ACC_STATIC)
564 return fntype;
565 return build_method_type (this_class, fntype);
568 tree
569 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
571 tree method_type, fndecl;
573 method_type = build_java_method_type (function_type,
574 this_class, access_flags);
576 fndecl = build_decl (FUNCTION_DECL, name, method_type);
577 DECL_CONTEXT (fndecl) = this_class;
579 DECL_LANG_SPECIFIC (fndecl)
580 = ggc_alloc_cleared (sizeof (struct lang_decl));
581 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
583 /* Initialize the static initializer test table. */
585 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
586 java_treetreehash_create (10, 1);
588 /* Initialize the initialized (static) class table. */
589 if (access_flags & ACC_STATIC)
590 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
591 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
593 /* Initialize the static method invocation compound list */
594 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
596 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
597 TYPE_METHODS (this_class) = fndecl;
599 /* Notice that this is a finalizer and update the class type
600 accordingly. This is used to optimize instance allocation. */
601 if (name == finalize_identifier_node
602 && TREE_TYPE (function_type) == void_type_node
603 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
604 HAS_FINALIZER_P (this_class) = 1;
606 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
607 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
608 if (access_flags & ACC_PRIVATE)
609 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
610 if (access_flags & ACC_NATIVE)
612 METHOD_NATIVE (fndecl) = 1;
613 DECL_EXTERNAL (fndecl) = 1;
615 if (access_flags & ACC_STATIC)
616 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
617 if (access_flags & ACC_FINAL)
618 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
619 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
620 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
621 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
622 return fndecl;
625 /* Add a method to THIS_CLASS.
626 The method's name is NAME.
627 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
629 tree
630 add_method (tree this_class, int access_flags, tree name, tree method_sig)
632 tree function_type, fndecl;
633 const unsigned char *sig
634 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
636 if (sig[0] != '(')
637 fatal_error ("bad method signature");
639 function_type = get_type_from_signature (method_sig);
640 fndecl = add_method_1 (this_class, access_flags, name, function_type);
641 set_java_signature (TREE_TYPE (fndecl), method_sig);
642 return fndecl;
645 tree
646 add_field (tree class, tree name, tree field_type, int flags)
648 int is_static = (flags & ACC_STATIC) != 0;
649 tree field;
650 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
651 TREE_CHAIN (field) = TYPE_FIELDS (class);
652 TYPE_FIELDS (class) = field;
653 DECL_CONTEXT (field) = class;
655 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
656 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
657 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
658 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
659 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
660 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
661 if (is_static)
663 FIELD_STATIC (field) = 1;
664 /* Always make field externally visible. This is required so
665 that native methods can always access the field. */
666 TREE_PUBLIC (field) = 1;
667 /* Considered external until we know what classes are being
668 compiled into this object file. */
669 DECL_EXTERNAL (field) = 1;
672 return field;
675 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
677 void
678 set_constant_value (tree field, tree constant)
680 if (field == NULL_TREE)
681 warning ("misplaced ConstantValue attribute (not in any field)");
682 else if (DECL_INITIAL (field) != NULL_TREE)
683 warning ("duplicate ConstantValue attribute for field '%s'",
684 IDENTIFIER_POINTER (DECL_NAME (field)));
685 else
687 DECL_INITIAL (field) = constant;
688 if (TREE_TYPE (constant) != TREE_TYPE (field)
689 && ! (TREE_TYPE (constant) == int_type_node
690 && INTEGRAL_TYPE_P (TREE_TYPE (field))
691 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
692 && ! (TREE_TYPE (constant) == utf8const_ptr_type
693 && TREE_TYPE (field) == string_ptr_type_node))
694 error ("ConstantValue attribute of field '%s' has wrong type",
695 IDENTIFIER_POINTER (DECL_NAME (field)));
696 if (FIELD_FINAL (field))
697 DECL_FIELD_FINAL_IUD (field) = 1;
701 /* Count the number of Unicode chars encoded in a given Ut8 string. */
703 #if 0
705 strLengthUtf8 (char *str, int len)
707 register unsigned char* ptr = (unsigned char*) str;
708 register unsigned char *limit = ptr + len;
709 int str_length = 0;
710 for (; ptr < limit; str_length++) {
711 if (UTF8_GET (ptr, limit) < 0)
712 return -1;
714 return str_length;
716 #endif
719 /* Calculate a hash value for a string encoded in Utf8 format.
720 * This returns the same hash value as specified for java.lang.String.hashCode.
723 static int32
724 hashUtf8String (const char *str, int len)
726 const unsigned char* ptr = (const unsigned char*) str;
727 const unsigned char *limit = ptr + len;
728 int32 hash = 0;
729 for (; ptr < limit;)
731 int ch = UTF8_GET (ptr, limit);
732 /* Updated specification from
733 http://www.javasoft.com/docs/books/jls/clarify.html. */
734 hash = (31 * hash) + ch;
736 return hash;
739 static GTY(()) tree utf8_decl_list = NULL_TREE;
741 tree
742 build_utf8_ref (tree name)
744 const char * name_ptr = IDENTIFIER_POINTER(name);
745 int name_len = IDENTIFIER_LENGTH(name);
746 char buf[60];
747 tree ctype, field = NULL_TREE, str_type, cinit, string;
748 static int utf8_count = 0;
749 int name_hash;
750 tree ref = IDENTIFIER_UTF8_REF (name);
751 tree decl;
752 if (ref != NULL_TREE)
753 return ref;
755 ctype = make_node (RECORD_TYPE);
756 str_type = build_prim_array_type (unsigned_byte_type_node,
757 name_len + 1); /* Allow for final '\0'. */
758 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
759 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
760 PUSH_FIELD (ctype, field, "data", str_type);
761 FINISH_RECORD (ctype);
762 START_RECORD_CONSTRUCTOR (cinit, ctype);
763 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
764 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
765 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
766 string = build_string (name_len, name_ptr);
767 TREE_TYPE (string) = str_type;
768 PUSH_FIELD_VALUE (cinit, "data", string);
769 FINISH_RECORD_CONSTRUCTOR (cinit);
770 TREE_CONSTANT (cinit) = 1;
772 /* Generate a unique-enough identifier. */
773 sprintf(buf, "_Utf%d", ++utf8_count);
775 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
776 TREE_STATIC (decl) = 1;
777 DECL_ARTIFICIAL (decl) = 1;
778 DECL_IGNORED_P (decl) = 1;
779 TREE_READONLY (decl) = 1;
780 TREE_THIS_VOLATILE (decl) = 0;
781 DECL_INITIAL (decl) = cinit;
783 if (HAVE_GAS_SHF_MERGE)
785 int decl_size;
786 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
787 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
788 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
789 if (flag_merge_constants && decl_size < 256)
791 char buf[32];
792 int flags = (SECTION_OVERRIDE
793 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
794 sprintf (buf, ".rodata.jutf8.%d", decl_size);
795 named_section_flags (buf, flags);
796 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
800 TREE_CHAIN (decl) = utf8_decl_list;
801 layout_decl (decl, 0);
802 pushdecl (decl);
803 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
804 utf8_decl_list = decl;
805 make_decl_rtl (decl, (char*) 0);
806 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
807 IDENTIFIER_UTF8_REF (name) = ref;
808 return ref;
811 /* Like build_class_ref, but instead of a direct reference generate a
812 pointer into the constant pool. */
814 static tree
815 build_indirect_class_ref (tree type)
817 int index;
818 tree cl;
819 index = alloc_class_constant (type);
820 cl = build_ref_from_constant_pool (index);
821 TREE_TYPE (cl) = promote_type (class_ptr_type);
822 return cl;
825 /* Build a reference to the class TYPE.
826 Also handles primitive types and array types. */
828 tree
829 build_class_ref (tree type)
831 int is_compiled = is_compiled_class (type);
832 if (is_compiled)
834 tree ref, decl_name, decl;
835 if (TREE_CODE (type) == POINTER_TYPE)
836 type = TREE_TYPE (type);
838 if (flag_indirect_dispatch
839 && type != current_class
840 && TREE_CODE (type) == RECORD_TYPE)
841 return build_indirect_class_ref (type);
843 if (TREE_CODE (type) == RECORD_TYPE)
845 if (TYPE_SIZE (type) == error_mark_node)
846 return null_pointer_node;
847 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
848 "", '/', '/', ".class");
849 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
850 if (decl == NULL_TREE)
852 decl = build_decl (VAR_DECL, decl_name, class_type_node);
853 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
854 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
855 TREE_STATIC (decl) = 1;
856 TREE_PUBLIC (decl) = 1;
857 DECL_IGNORED_P (decl) = 1;
858 DECL_ARTIFICIAL (decl) = 1;
859 if (is_compiled == 1)
860 DECL_EXTERNAL (decl) = 1;
861 SET_DECL_ASSEMBLER_NAME (decl,
862 java_mangle_class_field
863 (&temporary_obstack, type));
864 make_decl_rtl (decl, NULL);
865 pushdecl_top_level (decl);
868 else
870 const char *name;
871 char buffer[25];
872 if (flag_emit_class_files)
874 const char *prim_class_name;
875 tree prim_class;
876 if (type == char_type_node)
877 prim_class_name = "java.lang.Character";
878 else if (type == boolean_type_node)
879 prim_class_name = "java.lang.Boolean";
880 else if (type == byte_type_node)
881 prim_class_name = "java.lang.Byte";
882 else if (type == short_type_node)
883 prim_class_name = "java.lang.Short";
884 else if (type == int_type_node)
885 prim_class_name = "java.lang.Integer";
886 else if (type == long_type_node)
887 prim_class_name = "java.lang.Long";
888 else if (type == float_type_node)
889 prim_class_name = "java.lang.Float";
890 else if (type == double_type_node)
891 prim_class_name = "java.lang.Double";
892 else if (type == void_type_node)
893 prim_class_name = "java.lang.Void";
894 else
895 abort ();
897 prim_class = lookup_class (get_identifier (prim_class_name));
898 return build (COMPONENT_REF, NULL_TREE,
899 prim_class, TYPE_identifier_node);
901 decl_name = TYPE_NAME (type);
902 if (TREE_CODE (decl_name) == TYPE_DECL)
903 decl_name = DECL_NAME (decl_name);
904 name = IDENTIFIER_POINTER (decl_name);
905 if (strncmp (name, "promoted_", 9) == 0)
906 name += 9;
907 sprintf (buffer, "_Jv_%sClass", name);
908 decl_name = get_identifier (buffer);
909 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
910 if (decl == NULL_TREE)
912 decl = build_decl (VAR_DECL, decl_name, class_type_node);
913 TREE_STATIC (decl) = 1;
914 TREE_PUBLIC (decl) = 1;
915 DECL_EXTERNAL (decl) = 1;
916 make_decl_rtl (decl, NULL);
917 pushdecl_top_level (decl);
921 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
922 return ref;
924 else
925 return build_indirect_class_ref (type);
928 tree
929 build_static_field_ref (tree fdecl)
931 tree fclass = DECL_CONTEXT (fdecl);
932 int is_compiled = is_compiled_class (fclass);
934 /* Allow static final fields to fold to a constant. When using
935 -fno-assume-compiled, gcj will sometimes try to fold a field from
936 an uncompiled class. This is required when the field in question
937 meets the appropriate criteria for a compile-time constant.
938 However, currently sometimes gcj is too eager and will end up
939 returning the field itself, leading to an incorrect external
940 reference being generated. */
941 if ((is_compiled
942 && (! flag_indirect_dispatch || current_class == fclass))
943 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
944 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
945 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
946 && TREE_CONSTANT (DECL_INITIAL (fdecl))))
948 if (!DECL_RTL_SET_P (fdecl))
950 if (is_compiled == 1)
951 DECL_EXTERNAL (fdecl) = 1;
952 make_decl_rtl (fdecl, NULL);
954 return fdecl;
957 if (flag_indirect_dispatch)
959 tree table_index
960 = build_int_2 (get_symbol_table_index (fdecl, &atable_methods), 0);
961 tree field_address
962 = build (ARRAY_REF, build_pointer_type (TREE_TYPE (fdecl)),
963 atable_decl, table_index);
964 return fold (build1 (INDIRECT_REF, TREE_TYPE (fdecl),
965 field_address));
967 else
969 /* Compile as:
970 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
971 tree ref = build_class_ref (fclass);
972 tree fld;
973 int field_index = 0;
974 ref = build1 (INDIRECT_REF, class_type_node, ref);
975 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
976 lookup_field (&class_type_node, fields_ident));
978 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
980 if (fld == fdecl)
981 break;
982 if (fld == NULL_TREE)
983 fatal_error ("field '%s' not found in class",
984 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
985 if (FIELD_STATIC (fld))
986 field_index++;
988 field_index *= int_size_in_bytes (field_type_node);
989 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
990 ref, build_int_2 (field_index, 0)));
991 ref = build1 (INDIRECT_REF, field_type_node, ref);
992 ref = build (COMPONENT_REF, field_info_union_node,
993 ref, lookup_field (&field_type_node, info_ident));
994 ref = build (COMPONENT_REF, ptr_type_node,
995 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
996 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1001 get_access_flags_from_decl (tree decl)
1003 int access_flags = 0;
1004 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1006 if (FIELD_STATIC (decl))
1007 access_flags |= ACC_STATIC;
1008 if (FIELD_PUBLIC (decl))
1009 access_flags |= ACC_PUBLIC;
1010 if (FIELD_PROTECTED (decl))
1011 access_flags |= ACC_PROTECTED;
1012 if (FIELD_PRIVATE (decl))
1013 access_flags |= ACC_PRIVATE;
1014 if (FIELD_FINAL (decl))
1015 access_flags |= ACC_FINAL;
1016 if (FIELD_VOLATILE (decl))
1017 access_flags |= ACC_VOLATILE;
1018 if (FIELD_TRANSIENT (decl))
1019 access_flags |= ACC_TRANSIENT;
1020 return access_flags;
1022 if (TREE_CODE (decl) == TYPE_DECL)
1024 if (CLASS_PUBLIC (decl))
1025 access_flags |= ACC_PUBLIC;
1026 if (CLASS_FINAL (decl))
1027 access_flags |= ACC_FINAL;
1028 if (CLASS_SUPER (decl))
1029 access_flags |= ACC_SUPER;
1030 if (CLASS_INTERFACE (decl))
1031 access_flags |= ACC_INTERFACE;
1032 if (CLASS_ABSTRACT (decl))
1033 access_flags |= ACC_ABSTRACT;
1034 if (CLASS_STATIC (decl))
1035 access_flags |= ACC_STATIC;
1036 if (CLASS_PRIVATE (decl))
1037 access_flags |= ACC_PRIVATE;
1038 if (CLASS_PROTECTED (decl))
1039 access_flags |= ACC_PROTECTED;
1040 if (CLASS_STRICTFP (decl))
1041 access_flags |= ACC_STRICT;
1042 return access_flags;
1044 if (TREE_CODE (decl) == FUNCTION_DECL)
1046 if (METHOD_PUBLIC (decl))
1047 access_flags |= ACC_PUBLIC;
1048 if (METHOD_PRIVATE (decl))
1049 access_flags |= ACC_PRIVATE;
1050 if (METHOD_PROTECTED (decl))
1051 access_flags |= ACC_PROTECTED;
1052 if (METHOD_STATIC (decl))
1053 access_flags |= ACC_STATIC;
1054 if (METHOD_FINAL (decl))
1055 access_flags |= ACC_FINAL;
1056 if (METHOD_SYNCHRONIZED (decl))
1057 access_flags |= ACC_SYNCHRONIZED;
1058 if (METHOD_NATIVE (decl))
1059 access_flags |= ACC_NATIVE;
1060 if (METHOD_ABSTRACT (decl))
1061 access_flags |= ACC_ABSTRACT;
1062 if (METHOD_STRICTFP (decl))
1063 access_flags |= ACC_STRICT;
1064 if (METHOD_INVISIBLE (decl))
1065 access_flags |= ACC_INVISIBLE;
1066 return access_flags;
1068 abort ();
1071 static tree
1072 make_field_value (tree fdecl)
1074 tree finit;
1075 int flags;
1076 tree type = TREE_TYPE (fdecl);
1077 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1079 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1080 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1081 if (resolved)
1082 type = build_class_ref (type);
1083 else
1085 tree signature = build_java_signature (type);
1087 type = build_utf8_ref (unmangle_classname
1088 (IDENTIFIER_POINTER (signature),
1089 IDENTIFIER_LENGTH (signature)));
1091 PUSH_FIELD_VALUE (finit, "type", type);
1093 flags = get_access_flags_from_decl (fdecl);
1094 if (! resolved)
1095 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1097 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1098 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1100 PUSH_FIELD_VALUE
1101 (finit, "info",
1102 build_constructor (field_info_union_node,
1103 build_tree_list
1104 ((FIELD_STATIC (fdecl)
1105 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1106 : TYPE_FIELDS (field_info_union_node)),
1107 (FIELD_STATIC (fdecl)
1108 ? build_address_of (build_static_field_ref (fdecl))
1109 : byte_position (fdecl)))));
1111 FINISH_RECORD_CONSTRUCTOR (finit);
1112 return finit;
1115 static tree
1116 make_method_value (tree mdecl)
1118 static int method_name_count = 0;
1119 tree minit;
1120 tree index;
1121 tree code;
1122 #define ACC_TRANSLATED 0x4000
1123 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1125 if (!flag_indirect_dispatch && DECL_VINDEX (mdecl) != NULL_TREE)
1126 index = DECL_VINDEX (mdecl);
1127 else
1128 index = integer_minus_one_node;
1130 code = null_pointer_node;
1131 if (DECL_RTL_SET_P (mdecl))
1132 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1133 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1134 PUSH_FIELD_VALUE (minit, "name",
1135 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1136 init_identifier_node
1137 : DECL_NAME (mdecl)));
1139 tree signature = build_java_signature (TREE_TYPE (mdecl));
1140 PUSH_FIELD_VALUE (minit, "signature",
1141 (build_utf8_ref
1142 (unmangle_classname
1143 (IDENTIFIER_POINTER(signature),
1144 IDENTIFIER_LENGTH(signature)))));
1146 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1147 PUSH_FIELD_VALUE (minit, "index", index);
1148 PUSH_FIELD_VALUE (minit, "ncode", code);
1151 /* Compute the `throws' information for the method. */
1152 tree table = null_pointer_node;
1153 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1155 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1156 tree iter, type, array;
1157 char buf[60];
1159 table = tree_cons (NULL_TREE, table, NULL_TREE);
1160 for (iter = DECL_FUNCTION_THROWS (mdecl);
1161 iter != NULL_TREE;
1162 iter = TREE_CHAIN (iter))
1164 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1165 tree utf8
1166 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1167 IDENTIFIER_LENGTH (sig)));
1168 table = tree_cons (NULL_TREE, utf8, table);
1170 type = build_prim_array_type (ptr_type_node, length);
1171 table = build_constructor (type, table);
1172 /* Compute something unique enough. */
1173 sprintf (buf, "_methods%d", method_name_count++);
1174 array = build_decl (VAR_DECL, get_identifier (buf), type);
1175 DECL_INITIAL (array) = table;
1176 TREE_STATIC (array) = 1;
1177 DECL_ARTIFICIAL (array) = 1;
1178 DECL_IGNORED_P (array) = 1;
1179 rest_of_decl_compilation (array, (char*) 0, 1, 0);
1181 table = build1 (ADDR_EXPR, ptr_type_node, array);
1184 PUSH_FIELD_VALUE (minit, "throws", table);
1187 FINISH_RECORD_CONSTRUCTOR (minit);
1188 return minit;
1191 static tree
1192 get_dispatch_vector (tree type)
1194 tree vtable = TYPE_VTABLE (type);
1196 if (vtable == NULL_TREE)
1198 HOST_WIDE_INT i;
1199 tree method;
1200 tree super = CLASSTYPE_SUPER (type);
1201 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1202 vtable = make_tree_vec (nvirtuals);
1203 TYPE_VTABLE (type) = vtable;
1204 if (super != NULL_TREE)
1206 tree super_vtable = get_dispatch_vector (super);
1208 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1209 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1212 for (method = TYPE_METHODS (type); method != NULL_TREE;
1213 method = TREE_CHAIN (method))
1214 if (DECL_VINDEX (method) != NULL_TREE
1215 && host_integerp (DECL_VINDEX (method), 0))
1216 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1217 = method;
1220 return vtable;
1223 static tree
1224 get_dispatch_table (tree type, tree this_class_addr)
1226 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1227 tree vtable = get_dispatch_vector (type);
1228 int i, j;
1229 tree list = NULL_TREE;
1230 int nvirtuals = TREE_VEC_LENGTH (vtable);
1231 int arraysize;
1232 tree gc_descr;
1234 for (i = nvirtuals; --i >= 0; )
1236 tree method = TREE_VEC_ELT (vtable, i);
1237 if (METHOD_ABSTRACT (method))
1239 if (! abstract_p)
1240 warning ("%Jabstract method in non-abstract class", method);
1242 if (TARGET_VTABLE_USES_DESCRIPTORS)
1243 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1244 list = tree_cons (NULL_TREE, null_pointer_node, list);
1245 else
1246 list = tree_cons (NULL_TREE, null_pointer_node, list);
1248 else
1250 if (!DECL_RTL_SET_P (method))
1251 make_decl_rtl (method, NULL);
1253 if (TARGET_VTABLE_USES_DESCRIPTORS)
1254 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1256 tree fdesc = build (FDESC_EXPR, nativecode_ptr_type_node,
1257 method, build_int_2 (j, 0));
1258 TREE_CONSTANT (fdesc) = 1;
1259 list = tree_cons (NULL_TREE, fdesc, list);
1261 else
1262 list = tree_cons (NULL_TREE,
1263 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1264 method),
1265 list);
1269 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1270 using the Boehm GC we sometimes stash a GC type descriptor
1271 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1272 the emitted byte count during the output to the assembly file. */
1273 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1274 fake "function descriptor". It's first word is the is the class
1275 pointer, and subsequent words (usually one) contain the GC descriptor.
1276 In all other cases, we reserve two extra vtable slots. */
1277 gc_descr = get_boehm_type_descriptor (type);
1278 list = tree_cons (NULL_TREE, gc_descr, list);
1279 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1280 list = tree_cons (NULL_TREE, gc_descr, list);
1281 list = tree_cons (NULL_TREE, this_class_addr, list);
1283 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1284 list = tree_cons (NULL_TREE, null_pointer_node, list);
1285 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1286 list = tree_cons (integer_zero_node, null_pointer_node, list);
1288 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1289 if (TARGET_VTABLE_USES_DESCRIPTORS)
1290 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1291 arraysize += 2;
1292 return build_constructor (build_prim_array_type (nativecode_ptr_type_node,
1293 arraysize), list);
1296 static int
1297 supers_all_compiled (tree type)
1299 while (type != NULL_TREE)
1301 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1302 return 0;
1303 type = CLASSTYPE_SUPER (type);
1305 return 1;
1308 void
1309 make_class_data (tree type)
1311 tree decl, cons, temp;
1312 tree field, fields_decl;
1313 tree static_fields = NULL_TREE;
1314 tree instance_fields = NULL_TREE;
1315 HOST_WIDE_INT static_field_count = 0;
1316 HOST_WIDE_INT instance_field_count = 0;
1317 HOST_WIDE_INT field_count;
1318 tree field_array_type;
1319 tree method;
1320 tree methods = NULL_TREE;
1321 tree dtable_decl = NULL_TREE;
1322 HOST_WIDE_INT method_count = 0;
1323 tree method_array_type;
1324 tree methods_decl;
1325 tree super;
1326 tree this_class_addr;
1327 tree constant_pool_constructor;
1328 tree interfaces = null_pointer_node;
1329 int interface_len = 0;
1330 tree type_decl = TYPE_NAME (type);
1331 /** Offset from start of virtual function table declaration
1332 to where objects actually point at, following new g++ ABI. */
1333 tree dtable_start_offset = build_int_2 (2 * POINTER_SIZE / BITS_PER_UNIT, 0);
1335 this_class_addr = build_class_ref (type);
1336 decl = TREE_OPERAND (this_class_addr, 0);
1338 /* Build Field array. */
1339 field = TYPE_FIELDS (type);
1340 if (DECL_NAME (field) == NULL_TREE)
1341 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1342 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1344 if (! DECL_ARTIFICIAL (field))
1346 tree init = make_field_value (field);
1347 if (FIELD_STATIC (field))
1349 tree initial = DECL_INITIAL (field);
1350 static_field_count++;
1351 static_fields = tree_cons (NULL_TREE, init, static_fields);
1352 /* If the initial value is a string constant,
1353 prevent output_constant from trying to assemble the value. */
1354 if (initial != NULL_TREE
1355 && TREE_TYPE (initial) == string_ptr_type_node)
1356 DECL_INITIAL (field) = NULL_TREE;
1357 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1358 DECL_INITIAL (field) = initial;
1360 else
1362 instance_field_count++;
1363 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1367 field_count = static_field_count + instance_field_count;
1368 if (field_count > 0)
1370 static_fields = nreverse (static_fields);
1371 instance_fields = nreverse (instance_fields);
1372 static_fields = chainon (static_fields, instance_fields);
1373 field_array_type = build_prim_array_type (field_type_node, field_count);
1374 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1375 field_array_type);
1376 DECL_INITIAL (fields_decl) = build_constructor (field_array_type,
1377 static_fields);
1378 TREE_STATIC (fields_decl) = 1;
1379 DECL_ARTIFICIAL (fields_decl) = 1;
1380 DECL_IGNORED_P (fields_decl) = 1;
1381 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1383 else
1384 fields_decl = NULL_TREE;
1386 /* Build Method array. */
1387 for (method = TYPE_METHODS (type);
1388 method != NULL_TREE; method = TREE_CHAIN (method))
1390 tree init;
1391 if (METHOD_PRIVATE (method)
1392 && ! flag_keep_inline_functions
1393 && (flag_inline_functions || optimize))
1394 continue;
1395 init = make_method_value (method);
1396 method_count++;
1397 methods = tree_cons (NULL_TREE, init, methods);
1399 method_array_type = build_prim_array_type (method_type_node, method_count);
1400 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1401 method_array_type);
1402 DECL_INITIAL (methods_decl) = build_constructor (method_array_type,
1403 nreverse (methods));
1404 TREE_STATIC (methods_decl) = 1;
1405 DECL_ARTIFICIAL (methods_decl) = 1;
1406 DECL_IGNORED_P (methods_decl) = 1;
1407 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1409 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1410 && !flag_indirect_dispatch)
1412 tree dtable = get_dispatch_table (type, this_class_addr);
1413 dtable_decl = build_dtable_decl (type);
1414 DECL_INITIAL (dtable_decl) = dtable;
1415 TREE_STATIC (dtable_decl) = 1;
1416 DECL_ARTIFICIAL (dtable_decl) = 1;
1417 DECL_IGNORED_P (dtable_decl) = 1;
1418 TREE_PUBLIC (dtable_decl) = 1;
1419 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1420 if (type == class_type_node)
1421 class_dtable_decl = dtable_decl;
1424 if (class_dtable_decl == NULL_TREE)
1426 class_dtable_decl = build_dtable_decl (class_type_node);
1427 TREE_STATIC (class_dtable_decl) = 1;
1428 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1429 DECL_IGNORED_P (class_dtable_decl) = 1;
1430 if (is_compiled_class (class_type_node) != 2)
1431 DECL_EXTERNAL (class_dtable_decl) = 1;
1432 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1435 super = CLASSTYPE_SUPER (type);
1436 if (super == NULL_TREE)
1437 super = null_pointer_node;
1438 else if (! flag_indirect_dispatch
1439 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1440 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1441 super = build_class_ref (super);
1442 else
1444 int super_index = alloc_class_constant (super);
1445 super = build_int_2 (super_index, 0);
1446 TREE_TYPE (super) = ptr_type_node;
1449 /* Build and emit the array of implemented interfaces. */
1450 if (type != object_type_node)
1451 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1452 if (interface_len > 0)
1454 tree init = NULL_TREE;
1455 int i;
1456 tree interface_array_type, idecl;
1457 interface_array_type
1458 = build_prim_array_type (class_ptr_type, interface_len);
1459 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1460 interface_array_type);
1461 for (i = interface_len; i > 0; i--)
1463 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1464 tree iclass = BINFO_TYPE (child);
1465 tree index;
1466 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1467 index = build_class_ref (iclass);
1468 else
1470 int int_index = alloc_class_constant (iclass);
1471 index = build_int_2 (int_index, 0);
1472 TREE_TYPE (index) = ptr_type_node;
1474 init = tree_cons (NULL_TREE, index, init);
1476 DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
1477 TREE_STATIC (idecl) = 1;
1478 DECL_ARTIFICIAL (idecl) = 1;
1479 DECL_IGNORED_P (idecl) = 1;
1480 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1481 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1484 constant_pool_constructor = build_constants_constructor ();
1486 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1487 PUSH_FIELD_VALUE (temp, "vtable",
1488 build (PLUS_EXPR, dtable_ptr_type,
1489 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl),
1490 dtable_start_offset));
1491 if (! flag_hash_synchronization)
1492 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1493 FINISH_RECORD_CONSTRUCTOR (temp);
1494 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1495 PUSH_SUPER_VALUE (cons, temp);
1496 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1497 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1498 PUSH_FIELD_VALUE (cons, "accflags",
1499 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1501 PUSH_FIELD_VALUE (cons, "superclass",
1502 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1503 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1504 PUSH_FIELD_VALUE (cons, "methods",
1505 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1506 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1508 if (flag_indirect_dispatch)
1509 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1510 else
1511 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1513 PUSH_FIELD_VALUE (cons, "fields",
1514 fields_decl == NULL_TREE ? null_pointer_node
1515 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1516 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1517 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1518 PUSH_FIELD_VALUE (cons, "static_field_count",
1519 build_int_2 (static_field_count, 0));
1521 if (flag_indirect_dispatch)
1522 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1523 else
1524 PUSH_FIELD_VALUE (cons, "vtable",
1525 dtable_decl == NULL_TREE ? null_pointer_node
1526 : build (PLUS_EXPR, dtable_ptr_type,
1527 build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl),
1528 dtable_start_offset));
1529 if (otable_methods == NULL_TREE)
1531 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1532 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1534 else
1536 PUSH_FIELD_VALUE (cons, "otable",
1537 build1 (ADDR_EXPR, otable_ptr_type, otable_decl));
1538 PUSH_FIELD_VALUE (cons, "otable_syms",
1539 build1 (ADDR_EXPR, symbols_array_ptr_type,
1540 otable_syms_decl));
1541 TREE_CONSTANT (otable_decl) = 1;
1543 if (atable_methods == NULL_TREE)
1545 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1546 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1548 else
1550 PUSH_FIELD_VALUE (cons, "atable",
1551 build1 (ADDR_EXPR, atable_ptr_type, atable_decl));
1552 PUSH_FIELD_VALUE (cons, "atable_syms",
1553 build1 (ADDR_EXPR, symbols_array_ptr_type,
1554 atable_syms_decl));
1555 TREE_CONSTANT (atable_decl) = 1;
1558 PUSH_FIELD_VALUE (cons, "catch_classes",
1559 build1 (ADDR_EXPR, ptr_type_node, ctable_decl));
1560 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1561 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1562 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1563 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1565 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1566 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1567 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1568 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1569 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1570 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1571 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1572 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1574 FINISH_RECORD_CONSTRUCTOR (cons);
1576 DECL_INITIAL (decl) = cons;
1578 /* Hash synchronization requires at least 64-bit alignment. */
1579 if (flag_hash_synchronization && POINTER_SIZE < 64)
1580 DECL_ALIGN (decl) = 64;
1582 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1585 void
1586 finish_class (void)
1588 tree method;
1589 tree type_methods = TYPE_METHODS (current_class);
1590 int saw_native_method = 0;
1592 /* Find out if we have any native methods. We use this information
1593 later. */
1594 for (method = type_methods;
1595 method != NULL_TREE;
1596 method = TREE_CHAIN (method))
1598 if (METHOD_NATIVE (method))
1600 saw_native_method = 1;
1601 break;
1605 /* Emit deferred inline methods. */
1606 for (method = type_methods; method != NULL_TREE; )
1608 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1610 output_inline_function (method);
1611 /* Scan the list again to see if there are any earlier
1612 methods to emit. */
1613 method = type_methods;
1614 continue;
1616 method = TREE_CHAIN (method);
1619 current_function_decl = NULL_TREE;
1620 make_class_data (current_class);
1621 register_class ();
1622 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1625 /* Return 2 if CLASS is compiled by this compilation job;
1626 return 1 if CLASS can otherwise be assumed to be compiled;
1627 return 0 if we cannot assume that CLASS is compiled.
1628 Returns 1 for primitive and 0 for array types. */
1630 is_compiled_class (tree class)
1632 int seen_in_zip;
1633 if (TREE_CODE (class) == POINTER_TYPE)
1634 class = TREE_TYPE (class);
1635 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1636 return 1;
1637 if (TYPE_ARRAY_P (class))
1638 return 0;
1639 if (class == current_class)
1640 return 2;
1642 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1643 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1645 /* The class was seen in the current ZIP file and will be
1646 available as a compiled class in the future but may not have
1647 been loaded already. Load it if necessary. This prevent
1648 build_class_ref () from crashing. */
1650 if (seen_in_zip && !CLASS_LOADED_P (class))
1651 load_class (class, 1);
1653 /* We return 2 for class seen in ZIP and class from files
1654 belonging to the same compilation unit */
1655 return 2;
1658 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1660 if (!CLASS_LOADED_P (class))
1662 if (CLASS_FROM_SOURCE_P (class))
1663 safe_layout_class (class);
1664 else
1665 load_class (class, 1);
1667 return 1;
1670 return 0;
1673 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1675 tree
1676 build_dtable_decl (tree type)
1678 tree dtype;
1680 /* We need to build a new dtable type so that its size is uniquely
1681 computed when we're dealing with the class for real and not just
1682 faking it (like java.lang.Class during the initialization of the
1683 compiler.) We know we're not faking a class when CURRENT_CLASS is
1684 TYPE. */
1685 if (current_class == type)
1687 tree dummy = NULL_TREE;
1688 int n;
1690 dtype = make_node (RECORD_TYPE);
1692 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1693 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1695 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1696 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1698 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1699 TREE_CHAIN (dummy) = tmp_field;
1700 DECL_CONTEXT (tmp_field) = dtype;
1701 DECL_ARTIFICIAL (tmp_field) = 1;
1702 dummy = tmp_field;
1705 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1706 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1708 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1709 TREE_CHAIN (dummy) = tmp_field;
1710 DECL_CONTEXT (tmp_field) = dtype;
1711 DECL_ARTIFICIAL (tmp_field) = 1;
1712 dummy = tmp_field;
1715 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
1716 if (TARGET_VTABLE_USES_DESCRIPTORS)
1717 n *= TARGET_VTABLE_USES_DESCRIPTORS;
1719 PUSH_FIELD (dtype, dummy, "methods",
1720 build_prim_array_type (nativecode_ptr_type_node, n));
1721 layout_type (dtype);
1723 else
1724 dtype = dtable_type;
1726 return build_decl (VAR_DECL,
1727 java_mangle_vtable (&temporary_obstack, type), dtype);
1730 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1731 fields inherited from SUPER_CLASS. */
1733 void
1734 push_super_field (tree this_class, tree super_class)
1736 tree base_decl;
1737 /* Don't insert the field if we're just re-laying the class out. */
1738 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1739 return;
1740 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1741 DECL_IGNORED_P (base_decl) = 1;
1742 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1743 TYPE_FIELDS (this_class) = base_decl;
1744 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1745 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1748 /* Handle the different manners we may have to lay out a super class. */
1750 static tree
1751 maybe_layout_super_class (tree super_class, tree this_class)
1753 if (TREE_CODE (super_class) == RECORD_TYPE)
1755 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1756 safe_layout_class (super_class);
1757 if (!CLASS_LOADED_P (super_class))
1758 load_class (super_class, 1);
1760 /* We might have to layout the class before its dependency on
1761 the super class gets resolved by java_complete_class */
1762 else if (TREE_CODE (super_class) == POINTER_TYPE)
1764 if (TREE_TYPE (super_class) != NULL_TREE)
1765 super_class = TREE_TYPE (super_class);
1766 else
1768 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
1769 we give it one. */
1770 tree this_wrap = NULL_TREE;
1772 if (this_class)
1774 tree this_decl = TYPE_NAME (this_class);
1775 this_wrap = build_expr_wfl (this_class,
1776 DECL_SOURCE_FILE (this_decl),
1777 DECL_SOURCE_LINE (this_decl), 0);
1779 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1780 super_class, NULL_TREE, this_wrap);
1781 if (!super_class)
1782 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1783 super_class = TREE_TYPE (super_class);
1786 if (!TYPE_SIZE (super_class))
1787 safe_layout_class (super_class);
1789 return super_class;
1792 void
1793 layout_class (tree this_class)
1795 tree super_class = CLASSTYPE_SUPER (this_class);
1796 tree field;
1798 class_list = tree_cons (this_class, NULL_TREE, class_list);
1799 if (CLASS_BEING_LAIDOUT (this_class))
1801 char buffer [1024];
1802 char *report;
1803 tree current;
1805 sprintf (buffer, " with `%s'",
1806 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1807 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1809 for (current = TREE_CHAIN (class_list); current;
1810 current = TREE_CHAIN (current))
1812 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1813 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1814 IDENTIFIER_POINTER (DECL_NAME (decl)),
1815 DECL_SOURCE_FILE (decl),
1816 DECL_SOURCE_LINE (decl));
1817 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1819 obstack_1grow (&temporary_obstack, '\0');
1820 report = obstack_finish (&temporary_obstack);
1821 cyclic_inheritance_report = ggc_strdup (report);
1822 obstack_free (&temporary_obstack, report);
1823 TYPE_SIZE (this_class) = error_mark_node;
1824 return;
1826 CLASS_BEING_LAIDOUT (this_class) = 1;
1828 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1830 tree maybe_super_class
1831 = maybe_layout_super_class (super_class, this_class);
1832 if (maybe_super_class == NULL
1833 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1835 TYPE_SIZE (this_class) = error_mark_node;
1836 CLASS_BEING_LAIDOUT (this_class) = 0;
1837 class_list = TREE_CHAIN (class_list);
1838 return;
1840 if (TYPE_SIZE (this_class) == NULL_TREE)
1841 push_super_field (this_class, maybe_super_class);
1844 for (field = TYPE_FIELDS (this_class);
1845 field != NULL_TREE; field = TREE_CHAIN (field))
1847 if (FIELD_STATIC (field))
1849 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1850 SET_DECL_ASSEMBLER_NAME (field,
1851 java_mangle_decl
1852 (&temporary_obstack, field));
1856 layout_type (this_class);
1858 /* Also recursively load/layout any superinterfaces, but only if
1859 class was loaded from bytecode. The source parser will take care
1860 of this itself. */
1861 if (!CLASS_FROM_SOURCE_P (this_class))
1863 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1865 if (basetype_vec)
1867 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1868 int i;
1869 for (i = n; i > 0; i--)
1871 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1872 tree super_interface = BINFO_TYPE (vec_elt);
1874 tree maybe_super_interface
1875 = maybe_layout_super_class (super_interface, NULL_TREE);
1876 if (maybe_super_interface == NULL
1877 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1879 TYPE_SIZE (this_class) = error_mark_node;
1880 CLASS_BEING_LAIDOUT (this_class) = 0;
1881 class_list = TREE_CHAIN (class_list);
1882 return;
1888 /* Convert the size back to an SI integer value. */
1889 TYPE_SIZE_UNIT (this_class) =
1890 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1892 CLASS_BEING_LAIDOUT (this_class) = 0;
1893 class_list = TREE_CHAIN (class_list);
1896 static void
1897 add_miranda_methods (tree base_class, tree search_class)
1899 tree basetype_vec = TYPE_BINFO_BASETYPES (search_class);
1900 int i, n = TREE_VEC_LENGTH (basetype_vec);
1901 for (i = 1; i < n; ++i)
1903 tree method_decl;
1904 tree elt = TREE_VEC_ELT (basetype_vec, i);
1905 if (elt == NULL_TREE)
1906 break;
1907 elt = BINFO_TYPE (elt);
1909 /* Ensure that interface methods are seen in declared order. */
1910 layout_class_methods (elt);
1912 /* All base classes will have been laid out at this point, so the order
1913 will be correct. This code must match similar layout code in the
1914 runtime. */
1915 for (method_decl = TYPE_METHODS (elt);
1916 method_decl; method_decl = TREE_CHAIN (method_decl))
1918 tree sig, override;
1920 /* An interface can have <clinit>. */
1921 if (ID_CLINIT_P (DECL_NAME (method_decl)))
1922 continue;
1924 sig = build_java_argument_signature (TREE_TYPE (method_decl));
1925 override = lookup_argument_method (base_class,
1926 DECL_NAME (method_decl), sig);
1927 if (override == NULL_TREE)
1929 /* Found a Miranda method. Add it. */
1930 tree new_method;
1931 sig = build_java_signature (TREE_TYPE (method_decl));
1932 new_method
1933 = add_method (base_class,
1934 get_access_flags_from_decl (method_decl),
1935 DECL_NAME (method_decl), sig);
1936 METHOD_INVISIBLE (new_method) = 1;
1940 /* Try superinterfaces. */
1941 add_miranda_methods (base_class, elt);
1945 void
1946 layout_class_methods (tree this_class)
1948 tree method_decl, dtable_count;
1949 tree super_class;
1951 if (TYPE_NVIRTUALS (this_class))
1952 return;
1954 super_class = CLASSTYPE_SUPER (this_class);
1956 if (super_class)
1958 super_class = maybe_layout_super_class (super_class, this_class);
1959 if (!TYPE_NVIRTUALS (super_class))
1960 layout_class_methods (super_class);
1961 dtable_count = TYPE_NVIRTUALS (super_class);
1963 else
1964 dtable_count = integer_zero_node;
1966 if (CLASS_ABSTRACT (TYPE_NAME (this_class)))
1968 /* An abstract class can have methods which are declared only in
1969 an implemented interface. These are called "Miranda
1970 methods". We make a dummy method entry for such methods
1971 here. */
1972 add_miranda_methods (this_class, this_class);
1975 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
1977 for (method_decl = TYPE_METHODS (this_class);
1978 method_decl; method_decl = TREE_CHAIN (method_decl))
1979 dtable_count = layout_class_method (this_class, super_class,
1980 method_decl, dtable_count);
1982 TYPE_NVIRTUALS (this_class) = dtable_count;
1985 /* Lay METHOD_DECL out, returning a possibly new value of
1986 DTABLE_COUNT. Also mangle the method's name. */
1988 tree
1989 layout_class_method (tree this_class, tree super_class,
1990 tree method_decl, tree dtable_count)
1992 tree method_name = DECL_NAME (method_decl);
1994 TREE_PUBLIC (method_decl) = 1;
1995 /* Considered external until we know what classes are being
1996 compiled into this object file. */
1997 DECL_EXTERNAL (method_decl) = 1;
1999 /* This is a good occasion to mangle the method's name */
2000 SET_DECL_ASSEMBLER_NAME (method_decl,
2001 java_mangle_decl (&temporary_obstack,
2002 method_decl));
2003 /* We don't generate a RTL for the method if it's abstract, or if
2004 it's an interface method that isn't clinit. */
2005 if (! METHOD_ABSTRACT (method_decl)
2006 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2007 && (DECL_CLINIT_P (method_decl))))
2008 make_decl_rtl (method_decl, NULL);
2010 if (ID_INIT_P (method_name))
2012 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2013 const char *ptr;
2014 for (ptr = p; *ptr; )
2016 if (*ptr++ == '.')
2017 p = ptr;
2019 DECL_CONSTRUCTOR_P (method_decl) = 1;
2020 build_java_argument_signature (TREE_TYPE (method_decl));
2022 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
2024 tree method_sig =
2025 build_java_argument_signature (TREE_TYPE (method_decl));
2026 tree super_method = lookup_argument_method (super_class, method_name,
2027 method_sig);
2028 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
2030 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
2031 if (DECL_VINDEX (method_decl) == NULL_TREE
2032 && !CLASS_FROM_SOURCE_P (this_class))
2033 error ("%Jnon-static method '%D' overrides static method",
2034 method_decl, method_decl);
2036 else if (! METHOD_FINAL (method_decl)
2037 && ! METHOD_PRIVATE (method_decl)
2038 && ! CLASS_FINAL (TYPE_NAME (this_class))
2039 && dtable_count)
2041 DECL_VINDEX (method_decl) = dtable_count;
2042 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
2043 dtable_count, integer_one_node));
2047 return dtable_count;
2050 void
2051 register_class (void)
2053 /* END does not need to be registered with the garbage collector
2054 because it always points into the list given by REGISTERED_CLASS,
2055 and that variable is registered with the collector. */
2056 static tree end;
2057 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2058 tree current = copy_node (node);
2060 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2061 if (!registered_class)
2062 registered_class = current;
2063 else
2064 TREE_CHAIN (end) = current;
2066 end = current;
2069 /* Emit something to register classes at start-up time.
2071 The preferred mechanism is through the .jcr section, which contain
2072 a list of pointers to classes which get registered during
2073 constructor invocation time. The fallback mechanism is to generate
2074 a `constructor' function which calls _Jv_RegisterClass for each
2075 class in this file. */
2077 void
2078 emit_register_classes (void)
2080 /* ??? This isn't quite the correct test. We also have to know
2081 that the target is using gcc's crtbegin/crtend objects rather
2082 than the ones that come with the operating system. */
2083 if (SUPPORTS_WEAK && targetm.have_named_sections)
2085 #ifdef JCR_SECTION_NAME
2086 tree t;
2087 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2088 assemble_align (POINTER_SIZE);
2089 for (t = registered_class; t; t = TREE_CHAIN (t))
2090 assemble_integer (XEXP (DECL_RTL (t), 0),
2091 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2092 #else
2093 abort ();
2094 #endif
2096 else
2098 extern tree get_file_function_name (int);
2099 tree init_name = get_file_function_name ('I');
2100 tree init_type = build_function_type (void_type_node, end_params_node);
2101 tree init_decl;
2102 tree t;
2103 location_t saved_loc = input_location;
2105 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
2106 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
2107 DECL_SOURCE_LINE (init_decl) = 0;
2108 TREE_STATIC (init_decl) = 1;
2109 current_function_decl = init_decl;
2110 DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
2111 void_type_node);
2113 /* It can be a static function as long as collect2 does not have
2114 to scan the object file to find its ctor/dtor routine. */
2115 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
2117 /* Suppress spurious warnings. */
2118 TREE_USED (init_decl) = 1;
2120 pushlevel (0);
2121 make_decl_rtl (init_decl, NULL);
2122 init_function_start (init_decl);
2123 expand_function_start (init_decl, 0);
2125 /* Do not allow the function to be deferred. */
2126 current_function_cannot_inline
2127 = "static constructors and destructors cannot be inlined";
2129 for ( t = registered_class; t; t = TREE_CHAIN (t))
2130 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
2131 XEXP (DECL_RTL (t), 0), Pmode);
2132 input_location = DECL_SOURCE_LOCATION (init_decl);
2133 expand_function_end ();
2134 poplevel (1, 0, 1);
2135 rest_of_compilation (init_decl);
2136 current_function_decl = NULL_TREE;
2138 if (targetm.have_ctors_dtors)
2139 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
2140 DEFAULT_INIT_PRIORITY);
2141 input_location = saved_loc;
2145 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2147 static tree
2148 build_symbol_entry (tree decl)
2150 tree clname, name, signature, sym;
2152 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2153 name = build_utf8_ref (DECL_NAME (decl));
2154 signature = build_java_signature (TREE_TYPE (decl));
2155 signature = build_utf8_ref (unmangle_classname
2156 (IDENTIFIER_POINTER (signature),
2157 IDENTIFIER_LENGTH (signature)));
2159 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2160 PUSH_FIELD_VALUE (sym, "clname", clname);
2161 PUSH_FIELD_VALUE (sym, "name", name);
2162 PUSH_FIELD_VALUE (sym, "signature", signature);
2163 FINISH_RECORD_CONSTRUCTOR (sym);
2164 TREE_CONSTANT (sym) = 1;
2166 return sym;
2169 /* Emit a symbol table: used by -findirect-dispatch. */
2171 tree
2172 emit_symbol_table (tree name, tree the_table, tree decl_list, tree the_syms_decl,
2173 tree the_array_element_type)
2175 tree method_list, method, table, list, null_symbol;
2176 tree table_size, the_array_type;
2177 int index;
2179 /* Only emit a table if this translation unit actually made any
2180 references via it. */
2181 if (decl_list == NULL_TREE)
2182 return the_table;
2184 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2185 index = 0;
2186 method_list = decl_list;
2187 list = NULL_TREE;
2188 while (method_list != NULL_TREE)
2190 method = TREE_VALUE (method_list);
2191 list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2192 method_list = TREE_CHAIN (method_list);
2193 index++;
2196 /* Terminate the list with a "null" entry. */
2197 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2198 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2199 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2200 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2201 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2202 TREE_CONSTANT (null_symbol) = 1;
2203 list = tree_cons (NULL_TREE, null_symbol, list);
2205 /* Put the list in the right order and make it a constructor. */
2206 list = nreverse (list);
2207 table = build_constructor (symbols_array_type, list);
2209 /* Make it the initial value for otable_syms and emit the decl. */
2210 DECL_INITIAL (the_syms_decl) = table;
2211 DECL_ARTIFICIAL (the_syms_decl) = 1;
2212 DECL_IGNORED_P (the_syms_decl) = 1;
2213 rest_of_decl_compilation (the_syms_decl, NULL, 1, 0);
2215 /* Now that its size is known, redefine the table as an
2216 uninitialized static array of INDEX + 1 elements. The extra entry
2217 is used by the runtime to track whether the table has been
2218 initialized. */
2219 table_size = build_index_type (build_int_2 (index, 0));
2220 the_array_type = build_array_type (the_array_element_type, table_size);
2221 the_table = build_decl (VAR_DECL, name, the_array_type);
2222 TREE_STATIC (the_table) = 1;
2223 TREE_READONLY (the_table) = 1;
2224 rest_of_decl_compilation (the_table, NULL, 1, 0);
2226 return the_table;
2229 /* make an entry for the catch_classes list. */
2230 tree
2231 make_catch_class_record (tree catch_class, tree classname)
2233 tree entry;
2234 tree type = TREE_TYPE (TREE_TYPE (ctable_decl));
2235 START_RECORD_CONSTRUCTOR (entry, type);
2236 PUSH_FIELD_VALUE (entry, "address", catch_class);
2237 PUSH_FIELD_VALUE (entry, "classname", classname);
2238 FINISH_RECORD_CONSTRUCTOR (entry);
2239 return entry;
2243 /* Generate the list of Throwable classes that are caught by exception
2244 handlers in this compilation. */
2245 void
2246 emit_catch_table (void)
2248 tree table, table_size, array_type;
2249 catch_classes
2250 = tree_cons (NULL,
2251 make_catch_class_record (null_pointer_node, null_pointer_node),
2252 catch_classes);
2253 catch_classes = nreverse (catch_classes);
2254 catch_classes
2255 = tree_cons (NULL,
2256 make_catch_class_record (null_pointer_node, null_pointer_node),
2257 catch_classes);
2258 table_size = build_index_type (build_int_2 (list_length (catch_classes), 0));
2259 array_type
2260 = build_array_type (TREE_TYPE (TREE_TYPE (ctable_decl)), table_size);
2261 table = build_decl (VAR_DECL, DECL_NAME (ctable_decl), array_type);
2262 DECL_INITIAL (table) = build_constructor (array_type, catch_classes);
2263 TREE_STATIC (table) = 1;
2264 TREE_READONLY (table) = 1;
2265 rest_of_decl_compilation (table, NULL, 1, 0);
2266 ctable_decl = table;
2270 void
2271 init_class_processing (void)
2273 registerClass_libfunc = gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterClass");
2274 fields_ident = get_identifier ("fields");
2275 info_ident = get_identifier ("info");
2276 gcc_obstack_init (&temporary_obstack);
2279 static hashval_t java_treetreehash_hash (const void *);
2280 static int java_treetreehash_compare (const void *, const void *);
2282 /* A hash table mapping trees to trees. Used generally. */
2284 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2286 static hashval_t
2287 java_treetreehash_hash (const void *k_p)
2289 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2290 return JAVA_TREEHASHHASH_H (k->key);
2293 static int
2294 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2296 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2297 tree k2 = (tree) k2_p;
2298 return (k1->key == k2);
2301 tree
2302 java_treetreehash_find (htab_t ht, tree t)
2304 struct treetreehash_entry *e;
2305 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2306 e = htab_find_with_hash (ht, t, hv);
2307 if (e == NULL)
2308 return NULL;
2309 else
2310 return e->value;
2313 tree *
2314 java_treetreehash_new (htab_t ht, tree t)
2316 void **e;
2317 struct treetreehash_entry *tthe;
2318 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2320 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2321 if (*e == NULL)
2323 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2324 tthe->key = t;
2325 *e = tthe;
2327 else
2328 tthe = (struct treetreehash_entry *) *e;
2329 return &tthe->value;
2332 htab_t
2333 java_treetreehash_create (size_t size, int gc)
2335 if (gc)
2336 return htab_create_ggc (size, java_treetreehash_hash,
2337 java_treetreehash_compare, NULL);
2338 else
2339 return htab_create_alloc (size, java_treetreehash_hash,
2340 java_treetreehash_compare, free, xcalloc, free);
2343 #include "gt-java-class.h"