FSF GCC merge 02/23/03
[official-gcc.git] / gcc / java / class.c
bloba58f604da35bc614721f5f160bbfb19cd58ca88b
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 int assume_compiled (const char *);
61 static tree build_method_symbols_entry (tree);
63 static GTY(()) rtx registerClass_libfunc;
65 struct obstack temporary_obstack;
67 /* The compiler generates different code depending on whether or not
68 it can assume certain classes have been compiled down to native
69 code or not. The compiler options -fassume-compiled= and
70 -fno-assume-compiled= are used to create a tree of
71 assume_compiled_node objects. This tree is queried to determine if
72 a class is assume to be compiled or not. Each node in the tree
73 represents either a package or a specific class. */
75 typedef struct assume_compiled_node_struct
77 /* The class or package name. */
78 const char *ident;
80 /* Nonzero if this represents an exclusion. */
81 int excludep;
83 /* Pointers to other nodes in the tree. */
84 struct assume_compiled_node_struct *parent;
85 struct assume_compiled_node_struct *sibling;
86 struct assume_compiled_node_struct *child;
87 } assume_compiled_node;
89 static assume_compiled_node *find_assume_compiled_node (assume_compiled_node *,
90 const char *);
92 /* This is the root of the include/exclude tree. */
94 static assume_compiled_node *assume_compiled_tree;
96 static GTY(()) tree class_roots[5];
97 #define registered_class class_roots[0]
98 #define fields_ident class_roots[1] /* get_identifier ("fields") */
99 #define info_ident class_roots[2] /* get_identifier ("info") */
100 #define class_list class_roots[3]
101 #define class_dtable_decl class_roots[4]
103 /* Return the node that most closely represents the class whose name
104 is IDENT. Start the search from NODE. Return NULL if an
105 appropriate node does not exist. */
107 static assume_compiled_node *
108 find_assume_compiled_node (assume_compiled_node *node, const char *ident)
110 while (node)
112 size_t node_ident_length = strlen (node->ident);
114 /* node_ident_length is zero at the root of the tree. If the
115 identifiers are the same length, then we have matching
116 classes. Otherwise check if we've matched an enclosing
117 package name. */
119 if (node_ident_length == 0
120 || (strncmp (ident, node->ident, node_ident_length) == 0
121 && (strlen (ident) == node_ident_length
122 || ident[node_ident_length] == '.')))
124 /* We've found a match, however, there might be a more
125 specific match. */
127 assume_compiled_node *found = find_assume_compiled_node (node->child,
128 ident);
129 if (found)
130 return found;
131 else
132 return node;
135 /* No match yet. Continue through the sibling list. */
136 node = node->sibling;
139 /* No match at all in this tree. */
140 return NULL;
143 /* Add a new IDENT to the include/exclude tree. It's an exclusion
144 if EXCLUDEP is nonzero. */
146 void
147 add_assume_compiled (const char *ident, int excludep)
149 int len;
150 assume_compiled_node *parent;
151 assume_compiled_node *node =
152 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 const char *save_input_filename = input_filename;
312 int save_lineno = lineno;
313 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
314 CLASS_P (class_type) = 1;
315 input_filename = IDENTIFIER_POINTER (source_name);
316 lineno = 0;
317 decl = build_decl (TYPE_DECL, class_name, class_type);
319 /* dbxout needs a DECL_SIZE if in gstabs mode */
320 DECL_SIZE (decl) = integer_zero_node;
322 input_filename = save_input_filename;
323 lineno = save_lineno;
324 signature = identifier_subst (class_name, "L", '.', '/', ";");
325 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
327 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
328 both a typedef and in the struct name-space. We may want to re-visit
329 this later, but for now it reduces the changes needed for gdb. */
330 DECL_ARTIFICIAL (decl) = 1;
332 pushdecl_top_level (decl);
334 return decl;
337 /* Finds the (global) class named NAME. Creates the class if not found.
338 Also creates associated TYPE_DECL.
339 Does not check if the class actually exists, load the class,
340 fill in field or methods, or do layout_type. */
342 tree
343 lookup_class (tree name)
345 tree decl = IDENTIFIER_CLASS_VALUE (name);
346 if (decl == NULL_TREE)
347 decl = push_class (make_class (), name);
348 return TREE_TYPE (decl);
351 void
352 set_super_info (int access_flags, tree this_class,
353 tree super_class, int interfaces_count)
355 int total_supers = interfaces_count;
356 tree class_decl = TYPE_NAME (this_class);
357 if (super_class)
358 total_supers++;
360 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
361 if (super_class)
363 tree super_binfo = make_tree_vec (BINFO_ELTS);
364 BINFO_TYPE (super_binfo) = super_class;
365 BINFO_OFFSET (super_binfo) = integer_zero_node;
366 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
367 = super_binfo;
368 CLASS_HAS_SUPER (this_class) = 1;
371 set_class_decl_access_flags (access_flags, class_decl);
374 void
375 set_class_decl_access_flags (int access_flags, tree class_decl)
377 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
378 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
379 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
380 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
381 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
382 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
383 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
384 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
385 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
388 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
389 direct sub-classes of Object are 1, and so on. */
392 class_depth (tree clas)
394 int depth = 0;
395 if (! CLASS_LOADED_P (clas))
396 load_class (clas, 1);
397 if (TYPE_SIZE (clas) == error_mark_node)
398 return -1;
399 while (clas != object_type_node)
401 depth++;
402 clas = TYPE_BINFO_BASETYPE (clas, 0);
404 return depth;
407 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
410 interface_of_p (tree type1, tree type2)
412 int n, i;
413 tree basetype_vec;
415 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
416 return 0;
417 n = TREE_VEC_LENGTH (basetype_vec);
418 for (i = 0; i < n; i++)
420 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
421 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
422 return 1;
424 for (i = 0; i < n; i++)
426 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
427 if (vec_elt && BINFO_TYPE (vec_elt)
428 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
429 return 1;
431 return 0;
434 /* Return true iff TYPE1 inherits from TYPE2. */
437 inherits_from_p (tree type1, tree type2)
439 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
441 if (type1 == type2)
442 return 1;
443 type1 = CLASSTYPE_SUPER (type1);
445 return 0;
448 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
451 enclosing_context_p (tree type1, tree type2)
453 if (!INNER_CLASS_TYPE_P (type2))
454 return 0;
456 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
457 type2;
458 type2 = (INNER_CLASS_TYPE_P (type2) ?
459 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
461 if (type2 == type1)
462 return 1;
465 return 0;
468 /* Return 1 iff there exists a common enclosing context between TYPE1
469 and TYPE2. */
471 int common_enclosing_context_p (tree type1, tree type2)
473 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
474 return 0;
476 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
477 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
478 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
480 tree current;
481 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
482 current = (PURE_INNER_CLASS_TYPE_P (current) ?
483 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
484 NULL_TREE))
485 if (type1 == current)
486 return 1;
488 return 0;
491 static void
492 add_interface_do (tree basetype_vec, tree interface_class, int i)
494 tree interface_binfo = make_tree_vec (BINFO_ELTS);
495 BINFO_TYPE (interface_binfo) = interface_class;
496 BINFO_OFFSET (interface_binfo) = integer_zero_node;
497 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
498 TREE_VIA_VIRTUAL (interface_binfo) = 1;
499 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
502 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
503 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
504 if attempt is made to add it twice. */
506 tree
507 maybe_add_interface (tree this_class, tree interface_class)
509 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
510 int i;
511 int n = TREE_VEC_LENGTH (basetype_vec);
512 for (i = 0; ; i++)
514 if (i >= n)
516 error ("internal error - too many interface type");
517 return NULL_TREE;
519 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
520 break;
521 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
522 return interface_class;
524 add_interface_do (basetype_vec, interface_class, i);
525 return NULL_TREE;
528 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
530 void
531 add_interface (tree this_class, tree interface_class)
533 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
534 int i;
535 int n = TREE_VEC_LENGTH (basetype_vec);
536 for (i = 0; ; i++)
538 if (i >= n)
540 error ("internal error - too many interface type");
541 return;
543 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
544 break;
546 add_interface_do (basetype_vec, interface_class, i);
549 #if 0
550 /* Return the address of a pointer to the first FUNCTION_DECL
551 in the list (*LIST) whose DECL_NAME is NAME. */
553 static tree *
554 find_named_method (tree *list, tree name)
556 while (*list && DECL_NAME (*list) != name)
557 list = &TREE_CHAIN (*list);
558 return list;
560 #endif
562 static tree
563 build_java_method_type (tree fntype, tree this_class, int access_flags)
565 if (access_flags & ACC_STATIC)
566 return fntype;
567 return build_method_type (this_class, fntype);
570 tree
571 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
573 tree method_type, fndecl;
575 method_type = build_java_method_type (function_type,
576 this_class, access_flags);
578 fndecl = build_decl (FUNCTION_DECL, name, method_type);
579 DECL_CONTEXT (fndecl) = this_class;
581 DECL_LANG_SPECIFIC (fndecl)
582 = ggc_alloc_cleared (sizeof (struct lang_decl));
583 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
585 /* Initialize the static initializer test table. */
587 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
588 java_treetreehash_create (10, 1);
590 /* Initialize the initialized (static) class table. */
591 if (access_flags & ACC_STATIC)
592 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
593 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
595 /* Initialize the static method invocation compound list */
596 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
598 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
599 TYPE_METHODS (this_class) = fndecl;
601 /* Notice that this is a finalizer and update the class type
602 accordingly. This is used to optimize instance allocation. */
603 if (name == finalize_identifier_node
604 && TREE_TYPE (function_type) == void_type_node
605 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
606 HAS_FINALIZER_P (this_class) = 1;
608 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
609 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
610 if (access_flags & ACC_PRIVATE)
611 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
612 if (access_flags & ACC_NATIVE)
614 METHOD_NATIVE (fndecl) = 1;
615 DECL_EXTERNAL (fndecl) = 1;
617 if (access_flags & ACC_STATIC)
618 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
619 if (access_flags & ACC_FINAL)
620 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
621 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
622 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
623 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
624 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
625 return fndecl;
628 /* Add a method to THIS_CLASS.
629 The method's name is NAME.
630 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
632 tree
633 add_method (tree this_class, int access_flags, tree name, tree method_sig)
635 tree function_type, fndecl;
636 const unsigned char *sig
637 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
639 if (sig[0] != '(')
640 fatal_error ("bad method signature");
642 function_type = get_type_from_signature (method_sig);
643 fndecl = add_method_1 (this_class, access_flags, name, function_type);
644 set_java_signature (TREE_TYPE (fndecl), method_sig);
645 return fndecl;
648 tree
649 add_field (tree class, tree name, tree field_type, int flags)
651 int is_static = (flags & ACC_STATIC) != 0;
652 tree field;
653 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
654 TREE_CHAIN (field) = TYPE_FIELDS (class);
655 TYPE_FIELDS (class) = field;
656 DECL_CONTEXT (field) = class;
658 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
659 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
660 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
661 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
662 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
663 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
664 if (is_static)
666 FIELD_STATIC (field) = 1;
667 /* Always make field externally visible. This is required so
668 that native methods can always access the field. */
669 TREE_PUBLIC (field) = 1;
670 /* Considered external until we know what classes are being
671 compiled into this object file. */
672 DECL_EXTERNAL (field) = 1;
675 return field;
678 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
680 void
681 set_constant_value (tree field, tree constant)
683 if (field == NULL_TREE)
684 warning ("misplaced ConstantValue attribute (not in any field)");
685 else if (DECL_INITIAL (field) != NULL_TREE)
686 warning ("duplicate ConstantValue attribute for field '%s'",
687 IDENTIFIER_POINTER (DECL_NAME (field)));
688 else
690 DECL_INITIAL (field) = constant;
691 if (TREE_TYPE (constant) != TREE_TYPE (field)
692 && ! (TREE_TYPE (constant) == int_type_node
693 && INTEGRAL_TYPE_P (TREE_TYPE (field))
694 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
695 && ! (TREE_TYPE (constant) == utf8const_ptr_type
696 && TREE_TYPE (field) == string_ptr_type_node))
697 error ("ConstantValue attribute of field '%s' has wrong type",
698 IDENTIFIER_POINTER (DECL_NAME (field)));
699 if (FIELD_FINAL (field))
700 DECL_FIELD_FINAL_IUD (field) = 1;
704 /* Count the number of Unicode chars encoded in a given Ut8 string. */
706 #if 0
708 strLengthUtf8 (char *str, int len)
710 register unsigned char* ptr = (unsigned char*) str;
711 register unsigned char *limit = ptr + len;
712 int str_length = 0;
713 for (; ptr < limit; str_length++) {
714 if (UTF8_GET (ptr, limit) < 0)
715 return -1;
717 return str_length;
719 #endif
722 /* Calculate a hash value for a string encoded in Utf8 format.
723 * This returns the same hash value as specified for java.lang.String.hashCode.
726 static int32
727 hashUtf8String (const char *str, int len)
729 register const unsigned char* ptr = (const unsigned char*) str;
730 register const unsigned char *limit = ptr + len;
731 int32 hash = 0;
732 for (; ptr < limit;)
734 int ch = UTF8_GET (ptr, limit);
735 /* Updated specification from
736 http://www.javasoft.com/docs/books/jls/clarify.html. */
737 hash = (31 * hash) + ch;
739 return hash;
742 static GTY(()) tree utf8_decl_list = NULL_TREE;
744 tree
745 build_utf8_ref (tree name)
747 const char * name_ptr = IDENTIFIER_POINTER(name);
748 int name_len = IDENTIFIER_LENGTH(name);
749 char buf[60];
750 tree ctype, field = NULL_TREE, str_type, cinit, string;
751 static int utf8_count = 0;
752 int name_hash;
753 tree ref = IDENTIFIER_UTF8_REF (name);
754 tree decl;
755 if (ref != NULL_TREE)
756 return ref;
758 ctype = make_node (RECORD_TYPE);
759 str_type = build_prim_array_type (unsigned_byte_type_node,
760 name_len + 1); /* Allow for final '\0'. */
761 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
762 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
763 PUSH_FIELD (ctype, field, "data", str_type);
764 FINISH_RECORD (ctype);
765 START_RECORD_CONSTRUCTOR (cinit, ctype);
766 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
767 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
768 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
769 string = build_string (name_len, name_ptr);
770 TREE_TYPE (string) = str_type;
771 PUSH_FIELD_VALUE (cinit, "data", string);
772 FINISH_RECORD_CONSTRUCTOR (cinit);
773 TREE_CONSTANT (cinit) = 1;
775 /* Generate a unique-enough identifier. */
776 sprintf(buf, "_Utf%d", ++utf8_count);
778 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
779 TREE_STATIC (decl) = 1;
780 DECL_ARTIFICIAL (decl) = 1;
781 DECL_IGNORED_P (decl) = 1;
782 TREE_READONLY (decl) = 1;
783 TREE_THIS_VOLATILE (decl) = 0;
784 DECL_INITIAL (decl) = cinit;
785 #ifdef HAVE_GAS_SHF_MERGE
787 int decl_size;
788 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
789 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
790 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
791 if (flag_merge_constants && decl_size < 256)
793 char buf[32];
794 int flags = (SECTION_OVERRIDE
795 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
796 sprintf (buf, ".rodata.jutf8.%d", decl_size);
797 named_section_flags (buf, flags);
798 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
801 #endif
802 TREE_CHAIN (decl) = utf8_decl_list;
803 layout_decl (decl, 0);
804 pushdecl (decl);
805 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
806 utf8_decl_list = decl;
807 make_decl_rtl (decl, (char*) 0);
808 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
809 IDENTIFIER_UTF8_REF (name) = ref;
810 return ref;
813 /* Build a reference to the class TYPE.
814 Also handles primitive types and array types. */
816 tree
817 build_class_ref (tree type)
819 int is_compiled = is_compiled_class (type);
820 if (is_compiled)
822 tree ref, decl_name, decl;
823 if (TREE_CODE (type) == POINTER_TYPE)
824 type = TREE_TYPE (type);
825 if (TREE_CODE (type) == RECORD_TYPE)
827 if (TYPE_SIZE (type) == error_mark_node)
828 return null_pointer_node;
829 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
830 "", '/', '/', ".class");
831 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
832 if (decl == NULL_TREE)
834 decl = build_decl (VAR_DECL, decl_name, class_type_node);
835 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
836 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
837 TREE_STATIC (decl) = 1;
838 TREE_PUBLIC (decl) = 1;
839 DECL_IGNORED_P (decl) = 1;
840 DECL_ARTIFICIAL (decl) = 1;
841 if (is_compiled == 1)
842 DECL_EXTERNAL (decl) = 1;
843 SET_DECL_ASSEMBLER_NAME (decl,
844 java_mangle_class_field
845 (&temporary_obstack, type));
846 make_decl_rtl (decl, NULL);
847 pushdecl_top_level (decl);
850 else
852 const char *name;
853 char buffer[25];
854 if (flag_emit_class_files)
856 const char *prim_class_name;
857 tree prim_class;
858 if (type == char_type_node)
859 prim_class_name = "java.lang.Character";
860 else if (type == boolean_type_node)
861 prim_class_name = "java.lang.Boolean";
862 else if (type == byte_type_node)
863 prim_class_name = "java.lang.Byte";
864 else if (type == short_type_node)
865 prim_class_name = "java.lang.Short";
866 else if (type == int_type_node)
867 prim_class_name = "java.lang.Integer";
868 else if (type == long_type_node)
869 prim_class_name = "java.lang.Long";
870 else if (type == float_type_node)
871 prim_class_name = "java.lang.Float";
872 else if (type == double_type_node)
873 prim_class_name = "java.lang.Double";
874 else if (type == void_type_node)
875 prim_class_name = "java.lang.Void";
876 else
877 abort ();
879 prim_class = lookup_class (get_identifier (prim_class_name));
880 return build (COMPONENT_REF, NULL_TREE,
881 prim_class, TYPE_identifier_node);
883 decl_name = TYPE_NAME (type);
884 if (TREE_CODE (decl_name) == TYPE_DECL)
885 decl_name = DECL_NAME (decl_name);
886 name = IDENTIFIER_POINTER (decl_name);
887 if (strncmp (name, "promoted_", 9) == 0)
888 name += 9;
889 sprintf (buffer, "_Jv_%sClass", name);
890 decl_name = get_identifier (buffer);
891 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
892 if (decl == NULL_TREE)
894 decl = build_decl (VAR_DECL, decl_name, class_type_node);
895 TREE_STATIC (decl) = 1;
896 TREE_PUBLIC (decl) = 1;
897 DECL_EXTERNAL (decl) = 1;
898 make_decl_rtl (decl, NULL);
899 pushdecl_top_level (decl);
903 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
904 return ref;
906 else
908 int index;
909 tree cl;
910 index = alloc_class_constant (type);
911 cl = build_ref_from_constant_pool (index);
912 TREE_TYPE (cl) = promote_type (class_ptr_type);
913 return cl;
917 tree
918 build_static_field_ref (tree fdecl)
920 tree fclass = DECL_CONTEXT (fdecl);
921 int is_compiled = is_compiled_class (fclass);
923 /* Allow static final fields to fold to a constant. When using
924 -fno-assume-compiled, gcj will sometimes try to fold a field from
925 an uncompiled class. This is required when the field in question
926 meets the appropriate criteria for a compile-time constant.
927 However, currently sometimes gcj is too eager and will end up
928 returning the field itself, leading to an incorrect external
929 reference being generated. */
930 if (is_compiled
931 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
932 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
933 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
934 && TREE_CONSTANT (DECL_INITIAL (fdecl))))
936 if (!DECL_RTL_SET_P (fdecl))
938 if (is_compiled == 1)
939 DECL_EXTERNAL (fdecl) = 1;
940 make_decl_rtl (fdecl, NULL);
942 return fdecl;
944 else
946 /* Compile as:
947 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
948 tree ref = build_class_ref (fclass);
949 tree fld;
950 int field_index = 0;
951 ref = build1 (INDIRECT_REF, class_type_node, ref);
952 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
953 lookup_field (&class_type_node, fields_ident));
955 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
957 if (fld == fdecl)
958 break;
959 if (fld == NULL_TREE)
960 fatal_error ("field '%s' not found in class",
961 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
962 if (FIELD_STATIC (fld))
963 field_index++;
965 field_index *= int_size_in_bytes (field_type_node);
966 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
967 ref, build_int_2 (field_index, 0)));
968 ref = build1 (INDIRECT_REF, field_type_node, ref);
969 ref = build (COMPONENT_REF, field_info_union_node,
970 ref, lookup_field (&field_type_node, info_ident));
971 ref = build (COMPONENT_REF, ptr_type_node,
972 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
973 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
978 get_access_flags_from_decl (tree decl)
980 int access_flags = 0;
981 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
983 if (FIELD_STATIC (decl))
984 access_flags |= ACC_STATIC;
985 if (FIELD_PUBLIC (decl))
986 access_flags |= ACC_PUBLIC;
987 if (FIELD_PROTECTED (decl))
988 access_flags |= ACC_PROTECTED;
989 if (FIELD_PRIVATE (decl))
990 access_flags |= ACC_PRIVATE;
991 if (FIELD_FINAL (decl))
992 access_flags |= ACC_FINAL;
993 if (FIELD_VOLATILE (decl))
994 access_flags |= ACC_VOLATILE;
995 if (FIELD_TRANSIENT (decl))
996 access_flags |= ACC_TRANSIENT;
997 return access_flags;
999 if (TREE_CODE (decl) == TYPE_DECL)
1001 if (CLASS_PUBLIC (decl))
1002 access_flags |= ACC_PUBLIC;
1003 if (CLASS_FINAL (decl))
1004 access_flags |= ACC_FINAL;
1005 if (CLASS_SUPER (decl))
1006 access_flags |= ACC_SUPER;
1007 if (CLASS_INTERFACE (decl))
1008 access_flags |= ACC_INTERFACE;
1009 if (CLASS_ABSTRACT (decl))
1010 access_flags |= ACC_ABSTRACT;
1011 if (CLASS_STATIC (decl))
1012 access_flags |= ACC_STATIC;
1013 if (CLASS_PRIVATE (decl))
1014 access_flags |= ACC_PRIVATE;
1015 if (CLASS_PROTECTED (decl))
1016 access_flags |= ACC_PROTECTED;
1017 if (CLASS_STRICTFP (decl))
1018 access_flags |= ACC_STRICT;
1019 return access_flags;
1021 if (TREE_CODE (decl) == FUNCTION_DECL)
1023 if (METHOD_PUBLIC (decl))
1024 access_flags |= ACC_PUBLIC;
1025 if (METHOD_PRIVATE (decl))
1026 access_flags |= ACC_PRIVATE;
1027 if (METHOD_PROTECTED (decl))
1028 access_flags |= ACC_PROTECTED;
1029 if (METHOD_STATIC (decl))
1030 access_flags |= ACC_STATIC;
1031 if (METHOD_FINAL (decl))
1032 access_flags |= ACC_FINAL;
1033 if (METHOD_SYNCHRONIZED (decl))
1034 access_flags |= ACC_SYNCHRONIZED;
1035 if (METHOD_NATIVE (decl))
1036 access_flags |= ACC_NATIVE;
1037 if (METHOD_ABSTRACT (decl))
1038 access_flags |= ACC_ABSTRACT;
1039 if (METHOD_TRANSIENT (decl))
1040 access_flags |= ACC_TRANSIENT;
1041 if (METHOD_STRICTFP (decl))
1042 access_flags |= ACC_STRICT;
1043 return access_flags;
1045 abort ();
1048 static tree
1049 make_field_value (tree fdecl)
1051 tree finit;
1052 int flags;
1053 tree type = TREE_TYPE (fdecl);
1054 int resolved = is_compiled_class (type);
1056 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1057 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1058 if (resolved)
1059 type = build_class_ref (type);
1060 else
1062 tree signature = build_java_signature (type);
1064 type = build_utf8_ref (unmangle_classname
1065 (IDENTIFIER_POINTER (signature),
1066 IDENTIFIER_LENGTH (signature)));
1068 PUSH_FIELD_VALUE (finit, "type", type);
1070 flags = get_access_flags_from_decl (fdecl);
1071 if (! resolved)
1072 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1074 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1075 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1077 PUSH_FIELD_VALUE
1078 (finit, "info",
1079 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1080 build_tree_list
1081 ((FIELD_STATIC (fdecl)
1082 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1083 : TYPE_FIELDS (field_info_union_node)),
1084 (FIELD_STATIC (fdecl)
1085 ? build_address_of (build_static_field_ref (fdecl))
1086 : byte_position (fdecl)))));
1088 FINISH_RECORD_CONSTRUCTOR (finit);
1089 return finit;
1092 static tree
1093 make_method_value (tree mdecl)
1095 static int method_name_count = 0;
1096 tree minit;
1097 tree index;
1098 tree code;
1099 #define ACC_TRANSLATED 0x4000
1100 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1102 if (!flag_indirect_dispatch && DECL_VINDEX (mdecl) != NULL_TREE)
1103 index = DECL_VINDEX (mdecl);
1104 else
1105 index = integer_minus_one_node;
1107 code = null_pointer_node;
1108 if (DECL_RTL_SET_P (mdecl))
1109 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1110 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1111 PUSH_FIELD_VALUE (minit, "name",
1112 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1113 init_identifier_node
1114 : DECL_NAME (mdecl)));
1116 tree signature = build_java_signature (TREE_TYPE (mdecl));
1117 PUSH_FIELD_VALUE (minit, "signature",
1118 (build_utf8_ref
1119 (unmangle_classname
1120 (IDENTIFIER_POINTER(signature),
1121 IDENTIFIER_LENGTH(signature)))));
1123 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1124 PUSH_FIELD_VALUE (minit, "index", index);
1125 PUSH_FIELD_VALUE (minit, "ncode", code);
1128 /* Compute the `throws' information for the method. */
1129 tree table = null_pointer_node;
1130 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1132 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1133 tree iter, type, array;
1134 char buf[60];
1136 table = tree_cons (NULL_TREE, table, NULL_TREE);
1137 for (iter = DECL_FUNCTION_THROWS (mdecl);
1138 iter != NULL_TREE;
1139 iter = TREE_CHAIN (iter))
1141 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1142 tree utf8
1143 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1144 IDENTIFIER_LENGTH (sig)));
1145 table = tree_cons (NULL_TREE, utf8, table);
1147 type = build_prim_array_type (ptr_type_node, length);
1148 table = build (CONSTRUCTOR, type, NULL_TREE, table);
1149 /* Compute something unique enough. */
1150 sprintf (buf, "_methods%d", method_name_count++);
1151 array = build_decl (VAR_DECL, get_identifier (buf), type);
1152 DECL_INITIAL (array) = table;
1153 TREE_STATIC (array) = 1;
1154 DECL_ARTIFICIAL (array) = 1;
1155 DECL_IGNORED_P (array) = 1;
1156 rest_of_decl_compilation (array, (char*) 0, 1, 0);
1158 table = build1 (ADDR_EXPR, ptr_type_node, array);
1161 PUSH_FIELD_VALUE (minit, "throws", table);
1164 FINISH_RECORD_CONSTRUCTOR (minit);
1165 return minit;
1168 static tree
1169 get_dispatch_vector (tree type)
1171 tree vtable = TYPE_VTABLE (type);
1173 if (vtable == NULL_TREE)
1175 HOST_WIDE_INT i;
1176 tree method;
1177 tree super = CLASSTYPE_SUPER (type);
1178 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1179 vtable = make_tree_vec (nvirtuals);
1180 TYPE_VTABLE (type) = vtable;
1181 if (super != NULL_TREE)
1183 tree super_vtable = get_dispatch_vector (super);
1185 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1186 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1189 for (method = TYPE_METHODS (type); method != NULL_TREE;
1190 method = TREE_CHAIN (method))
1191 if (DECL_VINDEX (method) != NULL_TREE
1192 && host_integerp (DECL_VINDEX (method), 0))
1193 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1194 = method;
1197 return vtable;
1200 static tree
1201 get_dispatch_table (tree type, tree this_class_addr)
1203 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1204 tree vtable = get_dispatch_vector (type);
1205 int i, j;
1206 tree list = NULL_TREE;
1207 int nvirtuals = TREE_VEC_LENGTH (vtable);
1208 int arraysize;
1209 tree gc_descr;
1211 for (i = nvirtuals; --i >= 0; )
1213 tree method = TREE_VEC_ELT (vtable, i);
1214 if (METHOD_ABSTRACT (method))
1216 if (! abstract_p)
1217 warning_with_decl (method,
1218 "abstract method in non-abstract class");
1220 if (TARGET_VTABLE_USES_DESCRIPTORS)
1221 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1222 list = tree_cons (NULL_TREE, null_pointer_node, list);
1223 else
1224 list = tree_cons (NULL_TREE, null_pointer_node, list);
1226 else
1228 if (!DECL_RTL_SET_P (method))
1229 make_decl_rtl (method, NULL);
1231 if (TARGET_VTABLE_USES_DESCRIPTORS)
1232 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1234 tree fdesc = build (FDESC_EXPR, nativecode_ptr_type_node,
1235 method, build_int_2 (j, 0));
1236 TREE_CONSTANT (fdesc) = 1;
1237 list = tree_cons (NULL_TREE, fdesc, list);
1239 else
1240 list = tree_cons (NULL_TREE,
1241 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1242 method),
1243 list);
1247 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1248 using the Boehm GC we sometimes stash a GC type descriptor
1249 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1250 the emitted byte count during the output to the assembly file. */
1251 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1252 fake "function descriptor". It's first word is the is the class
1253 pointer, and subsequent words (usually one) contain the GC descriptor.
1254 In all other cases, we reserve two extra vtable slots. */
1255 gc_descr = get_boehm_type_descriptor (type);
1256 list = tree_cons (NULL_TREE, gc_descr, list);
1257 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1258 list = tree_cons (NULL_TREE, gc_descr, list);
1259 list = tree_cons (NULL_TREE, this_class_addr, list);
1261 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1262 list = tree_cons (NULL_TREE, null_pointer_node, list);
1263 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1264 list = tree_cons (integer_zero_node, null_pointer_node, list);
1266 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1267 if (TARGET_VTABLE_USES_DESCRIPTORS)
1268 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1269 arraysize += 2;
1270 return build (CONSTRUCTOR,
1271 build_prim_array_type (nativecode_ptr_type_node, arraysize),
1272 NULL_TREE, list);
1275 static int
1276 supers_all_compiled (tree type)
1278 while (type != NULL_TREE)
1280 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1281 return 0;
1282 type = CLASSTYPE_SUPER (type);
1284 return 1;
1287 void
1288 make_class_data (tree type)
1290 tree decl, cons, temp;
1291 tree field, fields_decl;
1292 tree static_fields = NULL_TREE;
1293 tree instance_fields = NULL_TREE;
1294 HOST_WIDE_INT static_field_count = 0;
1295 HOST_WIDE_INT instance_field_count = 0;
1296 HOST_WIDE_INT field_count;
1297 tree field_array_type;
1298 tree method;
1299 tree methods = NULL_TREE;
1300 tree dtable_decl = NULL_TREE;
1301 HOST_WIDE_INT method_count = 0;
1302 tree method_array_type;
1303 tree methods_decl;
1304 tree super;
1305 tree this_class_addr;
1306 tree constant_pool_constructor;
1307 tree interfaces = null_pointer_node;
1308 int interface_len = 0;
1309 tree type_decl = TYPE_NAME (type);
1310 /** Offset from start of virtual function table declaration
1311 to where objects actually point at, following new g++ ABI. */
1312 tree dtable_start_offset = build_int_2 (2 * POINTER_SIZE / BITS_PER_UNIT, 0);
1314 this_class_addr = build_class_ref (type);
1315 decl = TREE_OPERAND (this_class_addr, 0);
1317 /* Build Field array. */
1318 field = TYPE_FIELDS (type);
1319 if (DECL_NAME (field) == NULL_TREE)
1320 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1321 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1323 if (! DECL_ARTIFICIAL (field))
1325 tree init = make_field_value (field);
1326 if (FIELD_STATIC (field))
1328 tree initial = DECL_INITIAL (field);
1329 static_field_count++;
1330 static_fields = tree_cons (NULL_TREE, init, static_fields);
1331 /* If the initial value is a string constant,
1332 prevent output_constant from trying to assemble the value. */
1333 if (initial != NULL_TREE
1334 && TREE_TYPE (initial) == string_ptr_type_node)
1335 DECL_INITIAL (field) = NULL_TREE;
1336 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1337 DECL_INITIAL (field) = initial;
1339 else
1341 instance_field_count++;
1342 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1346 field_count = static_field_count + instance_field_count;
1347 if (field_count > 0)
1349 static_fields = nreverse (static_fields);
1350 instance_fields = nreverse (instance_fields);
1351 static_fields = chainon (static_fields, instance_fields);
1352 field_array_type = build_prim_array_type (field_type_node, field_count);
1353 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1354 field_array_type);
1355 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1356 NULL_TREE, static_fields);
1357 TREE_STATIC (fields_decl) = 1;
1358 DECL_ARTIFICIAL (fields_decl) = 1;
1359 DECL_IGNORED_P (fields_decl) = 1;
1360 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1362 else
1363 fields_decl = NULL_TREE;
1365 /* Build Method array. */
1366 for (method = TYPE_METHODS (type);
1367 method != NULL_TREE; method = TREE_CHAIN (method))
1369 tree init;
1370 if (METHOD_PRIVATE (method)
1371 && ! flag_keep_inline_functions
1372 && (flag_inline_functions || optimize))
1373 continue;
1374 init = make_method_value (method);
1375 method_count++;
1376 methods = tree_cons (NULL_TREE, init, methods);
1378 method_array_type = build_prim_array_type (method_type_node, method_count);
1379 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1380 method_array_type);
1381 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1382 NULL_TREE, nreverse (methods));
1383 TREE_STATIC (methods_decl) = 1;
1384 DECL_ARTIFICIAL (methods_decl) = 1;
1385 DECL_IGNORED_P (methods_decl) = 1;
1386 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1388 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1389 && !flag_indirect_dispatch)
1391 tree dtable = get_dispatch_table (type, this_class_addr);
1392 dtable_decl = build_dtable_decl (type);
1393 DECL_INITIAL (dtable_decl) = dtable;
1394 TREE_STATIC (dtable_decl) = 1;
1395 DECL_ARTIFICIAL (dtable_decl) = 1;
1396 DECL_IGNORED_P (dtable_decl) = 1;
1397 TREE_PUBLIC (dtable_decl) = 1;
1398 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1399 if (type == class_type_node)
1400 class_dtable_decl = dtable_decl;
1403 if (class_dtable_decl == NULL_TREE)
1405 class_dtable_decl = build_dtable_decl (class_type_node);
1406 TREE_STATIC (class_dtable_decl) = 1;
1407 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1408 DECL_IGNORED_P (class_dtable_decl) = 1;
1409 if (is_compiled_class (class_type_node) != 2)
1410 DECL_EXTERNAL (class_dtable_decl) = 1;
1411 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1414 super = CLASSTYPE_SUPER (type);
1415 if (super == NULL_TREE)
1416 super = null_pointer_node;
1417 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1418 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1419 super = build_class_ref (super);
1420 else
1422 int super_index = alloc_class_constant (super);
1423 super = build_int_2 (super_index, 0);
1424 TREE_TYPE (super) = ptr_type_node;
1427 /* Build and emit the array of implemented interfaces. */
1428 if (type != object_type_node)
1429 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1430 if (interface_len > 0)
1432 tree init = NULL_TREE;
1433 int i;
1434 tree interface_array_type, idecl;
1435 interface_array_type
1436 = build_prim_array_type (class_ptr_type, interface_len);
1437 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1438 interface_array_type);
1439 for (i = interface_len; i > 0; i--)
1441 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1442 tree iclass = BINFO_TYPE (child);
1443 tree index;
1444 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1445 index = build_class_ref (iclass);
1446 else
1448 int int_index = alloc_class_constant (iclass);
1449 index = build_int_2 (int_index, 0);
1450 TREE_TYPE (index) = ptr_type_node;
1452 init = tree_cons (NULL_TREE, index, init);
1454 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1455 NULL_TREE, init);
1456 TREE_STATIC (idecl) = 1;
1457 DECL_ARTIFICIAL (idecl) = 1;
1458 DECL_IGNORED_P (idecl) = 1;
1459 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1460 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1463 constant_pool_constructor = build_constants_constructor ();
1465 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1466 PUSH_FIELD_VALUE (temp, "vtable",
1467 build (PLUS_EXPR, dtable_ptr_type,
1468 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl),
1469 dtable_start_offset));
1470 if (! flag_hash_synchronization)
1471 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1472 FINISH_RECORD_CONSTRUCTOR (temp);
1473 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1474 PUSH_SUPER_VALUE (cons, temp);
1475 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1476 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1477 PUSH_FIELD_VALUE (cons, "accflags",
1478 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1480 PUSH_FIELD_VALUE (cons, "superclass",
1481 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1482 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1483 PUSH_FIELD_VALUE (cons, "methods",
1484 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1485 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1487 if (flag_indirect_dispatch)
1488 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node)
1489 else
1490 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1492 PUSH_FIELD_VALUE (cons, "fields",
1493 fields_decl == NULL_TREE ? null_pointer_node
1494 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1495 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1496 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1497 PUSH_FIELD_VALUE (cons, "static_field_count",
1498 build_int_2 (static_field_count, 0));
1500 if (flag_indirect_dispatch)
1501 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node)
1502 else
1503 PUSH_FIELD_VALUE (cons, "vtable",
1504 dtable_decl == NULL_TREE ? null_pointer_node
1505 : build (PLUS_EXPR, dtable_ptr_type,
1506 build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl),
1507 dtable_start_offset));
1509 if (otable_methods == NULL_TREE)
1511 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1512 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1514 else
1516 PUSH_FIELD_VALUE (cons, "otable",
1517 build1 (ADDR_EXPR, otable_ptr_type, otable_decl));
1518 PUSH_FIELD_VALUE (cons, "otable_syms",
1519 build1 (ADDR_EXPR, method_symbols_array_ptr_type,
1520 otable_syms_decl));
1522 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1523 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1524 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1525 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1527 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1528 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1529 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1530 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1531 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1532 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1533 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1535 FINISH_RECORD_CONSTRUCTOR (cons);
1537 DECL_INITIAL (decl) = cons;
1539 /* Hash synchronization requires at least 64-bit alignment. */
1540 if (flag_hash_synchronization && POINTER_SIZE < 64)
1541 DECL_ALIGN (decl) = 64;
1543 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1546 void
1547 finish_class (void)
1549 tree method;
1550 tree type_methods = TYPE_METHODS (current_class);
1551 int saw_native_method = 0;
1553 /* Find out if we have any native methods. We use this information
1554 later. */
1555 for (method = type_methods;
1556 method != NULL_TREE;
1557 method = TREE_CHAIN (method))
1559 if (METHOD_NATIVE (method))
1561 saw_native_method = 1;
1562 break;
1566 /* Emit deferred inline methods. */
1567 for (method = type_methods; method != NULL_TREE; )
1569 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1571 output_inline_function (method);
1572 /* Scan the list again to see if there are any earlier
1573 methods to emit. */
1574 method = type_methods;
1575 continue;
1577 method = TREE_CHAIN (method);
1580 current_function_decl = NULL_TREE;
1581 make_class_data (current_class);
1582 register_class ();
1583 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1586 /* Return 2 if CLASS is compiled by this compilation job;
1587 return 1 if CLASS can otherwise be assumed to be compiled;
1588 return 0 if we cannot assume that CLASS is compiled.
1589 Returns 1 for primitive and 0 for array types. */
1591 is_compiled_class (tree class)
1593 int seen_in_zip;
1594 if (TREE_CODE (class) == POINTER_TYPE)
1595 class = TREE_TYPE (class);
1596 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1597 return 1;
1598 if (TYPE_ARRAY_P (class))
1599 return 0;
1600 if (class == current_class)
1601 return 2;
1603 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1604 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1606 /* The class was seen in the current ZIP file and will be
1607 available as a compiled class in the future but may not have
1608 been loaded already. Load it if necessary. This prevent
1609 build_class_ref () from crashing. */
1611 if (seen_in_zip && !CLASS_LOADED_P (class))
1612 load_class (class, 1);
1614 /* We return 2 for class seen in ZIP and class from files
1615 belonging to the same compilation unit */
1616 return 2;
1619 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1621 if (!CLASS_LOADED_P (class))
1623 if (CLASS_FROM_SOURCE_P (class))
1624 safe_layout_class (class);
1625 else
1626 load_class (class, 1);
1628 return 1;
1631 return 0;
1634 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1636 tree
1637 build_dtable_decl (tree type)
1639 tree dtype;
1641 /* We need to build a new dtable type so that its size is uniquely
1642 computed when we're dealing with the class for real and not just
1643 faking it (like java.lang.Class during the initialization of the
1644 compiler.) We know we're not faking a class when CURRENT_CLASS is
1645 TYPE. */
1646 if (current_class == type)
1648 tree dummy = NULL_TREE;
1649 int n;
1651 dtype = make_node (RECORD_TYPE);
1653 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1654 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1656 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1657 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1659 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1660 TREE_CHAIN (dummy) = tmp_field;
1661 DECL_CONTEXT (tmp_field) = dtype;
1662 DECL_ARTIFICIAL (tmp_field) = 1;
1663 dummy = tmp_field;
1666 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1667 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1669 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1670 TREE_CHAIN (dummy) = tmp_field;
1671 DECL_CONTEXT (tmp_field) = dtype;
1672 DECL_ARTIFICIAL (tmp_field) = 1;
1673 dummy = tmp_field;
1676 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
1677 if (TARGET_VTABLE_USES_DESCRIPTORS)
1678 n *= TARGET_VTABLE_USES_DESCRIPTORS;
1680 PUSH_FIELD (dtype, dummy, "methods",
1681 build_prim_array_type (nativecode_ptr_type_node, n));
1682 layout_type (dtype);
1684 else
1685 dtype = dtable_type;
1687 return build_decl (VAR_DECL,
1688 java_mangle_vtable (&temporary_obstack, type), dtype);
1691 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1692 fields inherited from SUPER_CLASS. */
1694 void
1695 push_super_field (tree this_class, tree super_class)
1697 tree base_decl;
1698 /* Don't insert the field if we're just re-laying the class out. */
1699 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1700 return;
1701 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1702 DECL_IGNORED_P (base_decl) = 1;
1703 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1704 TYPE_FIELDS (this_class) = base_decl;
1705 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1706 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1709 /* Handle the different manners we may have to lay out a super class. */
1711 static tree
1712 maybe_layout_super_class (tree super_class, tree this_class)
1714 if (TREE_CODE (super_class) == RECORD_TYPE)
1716 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1717 safe_layout_class (super_class);
1718 if (!CLASS_LOADED_P (super_class))
1719 load_class (super_class, 1);
1721 /* We might have to layout the class before its dependency on
1722 the super class gets resolved by java_complete_class */
1723 else if (TREE_CODE (super_class) == POINTER_TYPE)
1725 if (TREE_TYPE (super_class) != NULL_TREE)
1726 super_class = TREE_TYPE (super_class);
1727 else
1729 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1730 super_class, NULL_TREE, this_class);
1731 if (!super_class)
1732 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1733 super_class = TREE_TYPE (super_class);
1736 if (!TYPE_SIZE (super_class))
1737 safe_layout_class (super_class);
1739 return super_class;
1742 void
1743 layout_class (tree this_class)
1745 tree super_class = CLASSTYPE_SUPER (this_class);
1746 tree field;
1748 class_list = tree_cons (this_class, NULL_TREE, class_list);
1749 if (CLASS_BEING_LAIDOUT (this_class))
1751 char buffer [1024];
1752 char *report;
1753 tree current;
1755 sprintf (buffer, " with `%s'",
1756 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1757 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1759 for (current = TREE_CHAIN (class_list); current;
1760 current = TREE_CHAIN (current))
1762 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1763 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1764 IDENTIFIER_POINTER (DECL_NAME (decl)),
1765 DECL_SOURCE_FILE (decl),
1766 DECL_SOURCE_LINE (decl));
1767 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1769 obstack_1grow (&temporary_obstack, '\0');
1770 report = obstack_finish (&temporary_obstack);
1771 cyclic_inheritance_report = ggc_strdup (report);
1772 obstack_free (&temporary_obstack, report);
1773 TYPE_SIZE (this_class) = error_mark_node;
1774 return;
1776 CLASS_BEING_LAIDOUT (this_class) = 1;
1778 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1780 tree maybe_super_class
1781 = maybe_layout_super_class (super_class, this_class);
1782 if (maybe_super_class == NULL
1783 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1785 TYPE_SIZE (this_class) = error_mark_node;
1786 CLASS_BEING_LAIDOUT (this_class) = 0;
1787 class_list = TREE_CHAIN (class_list);
1788 return;
1790 if (TYPE_SIZE (this_class) == NULL_TREE)
1791 push_super_field (this_class, maybe_super_class);
1794 for (field = TYPE_FIELDS (this_class);
1795 field != NULL_TREE; field = TREE_CHAIN (field))
1797 if (FIELD_STATIC (field))
1799 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1800 SET_DECL_ASSEMBLER_NAME (field,
1801 java_mangle_decl
1802 (&temporary_obstack, field));
1806 layout_type (this_class);
1808 /* Also recursively load/layout any superinterfaces, but only if class was
1809 loaded from bytecode. The source parser will take care of this itself. */
1810 if (!CLASS_FROM_SOURCE_P (this_class))
1812 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1814 if (basetype_vec)
1816 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1817 int i;
1818 for (i = n; i > 0; i--)
1820 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1821 tree super_interface = BINFO_TYPE (vec_elt);
1823 tree maybe_super_interface
1824 = maybe_layout_super_class (super_interface, NULL_TREE);
1825 if (maybe_super_interface == NULL
1826 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1828 TYPE_SIZE (this_class) = error_mark_node;
1829 CLASS_BEING_LAIDOUT (this_class) = 0;
1830 class_list = TREE_CHAIN (class_list);
1831 return;
1837 /* Convert the size back to an SI integer value */
1838 TYPE_SIZE_UNIT (this_class) =
1839 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1841 CLASS_BEING_LAIDOUT (this_class) = 0;
1842 class_list = TREE_CHAIN (class_list);
1845 void
1846 layout_class_methods (tree this_class)
1848 tree method_decl, dtable_count;
1849 tree super_class;
1851 if (TYPE_NVIRTUALS (this_class))
1852 return;
1854 super_class = CLASSTYPE_SUPER (this_class);
1856 if (super_class)
1858 super_class = maybe_layout_super_class (super_class, this_class);
1859 if (!TYPE_NVIRTUALS (super_class))
1860 layout_class_methods (super_class);
1861 dtable_count = TYPE_NVIRTUALS (super_class);
1863 else
1864 dtable_count = integer_zero_node;
1866 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
1868 for (method_decl = TYPE_METHODS (this_class);
1869 method_decl; method_decl = TREE_CHAIN (method_decl))
1870 dtable_count = layout_class_method (this_class, super_class,
1871 method_decl, dtable_count);
1873 TYPE_NVIRTUALS (this_class) = dtable_count;
1876 /* Lay METHOD_DECL out, returning a possibly new value of
1877 DTABLE_COUNT. Also mangle the method's name. */
1879 tree
1880 layout_class_method (tree this_class, tree super_class,
1881 tree method_decl, tree dtable_count)
1883 tree method_name = DECL_NAME (method_decl);
1885 TREE_PUBLIC (method_decl) = 1;
1887 /* This is a good occasion to mangle the method's name */
1888 SET_DECL_ASSEMBLER_NAME (method_decl,
1889 java_mangle_decl (&temporary_obstack,
1890 method_decl));
1891 /* We don't generate a RTL for the method if it's abstract, or if
1892 it's an interface method that isn't clinit. */
1893 if (! METHOD_ABSTRACT (method_decl)
1894 || (CLASS_INTERFACE (TYPE_NAME (this_class))
1895 && (DECL_CLINIT_P (method_decl))))
1896 make_decl_rtl (method_decl, NULL);
1898 if (ID_INIT_P (method_name))
1900 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1901 const char *ptr;
1902 for (ptr = p; *ptr; )
1904 if (*ptr++ == '.')
1905 p = ptr;
1907 DECL_CONSTRUCTOR_P (method_decl) = 1;
1908 build_java_argument_signature (TREE_TYPE (method_decl));
1910 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
1912 tree method_sig =
1913 build_java_argument_signature (TREE_TYPE (method_decl));
1914 tree super_method = lookup_argument_method (super_class, method_name,
1915 method_sig);
1916 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
1918 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
1919 if (DECL_VINDEX (method_decl) == NULL_TREE
1920 && !CLASS_FROM_SOURCE_P (this_class))
1921 error_with_decl (method_decl,
1922 "non-static method '%s' overrides static method");
1924 else if (! METHOD_FINAL (method_decl)
1925 && ! METHOD_PRIVATE (method_decl)
1926 && ! CLASS_FINAL (TYPE_NAME (this_class))
1927 && dtable_count)
1929 DECL_VINDEX (method_decl) = dtable_count;
1930 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
1931 dtable_count, integer_one_node));
1935 return dtable_count;
1938 void
1939 register_class (void)
1941 /* END does not need to be registered with the garbage collector
1942 because it always points into the list given by REGISTERED_CLASS,
1943 and that variable is registered with the collector. */
1944 static tree end;
1945 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
1946 tree current = copy_node (node);
1948 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
1949 if (!registered_class)
1950 registered_class = current;
1951 else
1952 TREE_CHAIN (end) = current;
1954 end = current;
1957 /* Emit something to register classes at start-up time.
1959 The preferred mechanism is through the .jcr section, which contain
1960 a list of pointers to classes which get registered during
1961 constructor invocation time. The fallback mechanism is to generate
1962 a `constructor' function which calls _Jv_RegisterClass for each
1963 class in this file. */
1965 void
1966 emit_register_classes (void)
1968 /* ??? This isn't quite the correct test. We also have to know
1969 that the target is using gcc's crtbegin/crtend objects rather
1970 than the ones that come with the operating system. */
1971 if (SUPPORTS_WEAK && targetm.have_named_sections)
1973 #ifdef JCR_SECTION_NAME
1974 tree t;
1975 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
1976 assemble_align (POINTER_SIZE);
1977 for (t = registered_class; t; t = TREE_CHAIN (t))
1978 assemble_integer (XEXP (DECL_RTL (t), 0),
1979 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1980 #else
1981 abort ();
1982 #endif
1984 else
1986 extern tree get_file_function_name (int);
1987 tree init_name = get_file_function_name ('I');
1988 tree init_type = build_function_type (void_type_node, end_params_node);
1989 tree init_decl;
1990 tree t;
1992 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
1993 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
1994 TREE_STATIC (init_decl) = 1;
1995 current_function_decl = init_decl;
1996 DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
1997 void_type_node);
1999 /* It can be a static function as long as collect2 does not have
2000 to scan the object file to find its ctor/dtor routine. */
2001 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
2003 /* Suppress spurious warnings. */
2004 TREE_USED (init_decl) = 1;
2006 pushlevel (0);
2007 make_decl_rtl (init_decl, NULL);
2008 init_function_start (init_decl, input_filename, 0);
2009 expand_function_start (init_decl, 0);
2011 /* Do not allow the function to be deferred. */
2012 current_function_cannot_inline
2013 = "static constructors and destructors cannot be inlined";
2015 for ( t = registered_class; t; t = TREE_CHAIN (t))
2016 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
2017 XEXP (DECL_RTL (t), 0), Pmode);
2019 expand_function_end (input_filename, 0, 0);
2020 poplevel (1, 0, 1);
2021 rest_of_compilation (init_decl);
2022 current_function_decl = NULL_TREE;
2024 if (targetm.have_ctors_dtors)
2025 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
2026 DEFAULT_INIT_PRIORITY);
2030 /* Make a method_symbol_type (_Jv_MethodSymbol) node for METHOD. */
2032 tree
2033 build_method_symbols_entry (tree method)
2035 tree clname, name, signature, method_symbol;
2037 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (method))));
2038 name = build_utf8_ref (DECL_NAME (method));
2039 signature = build_java_signature (TREE_TYPE (method));
2040 signature = build_utf8_ref (unmangle_classname
2041 (IDENTIFIER_POINTER (signature),
2042 IDENTIFIER_LENGTH (signature)));
2044 START_RECORD_CONSTRUCTOR (method_symbol, method_symbol_type);
2045 PUSH_FIELD_VALUE (method_symbol, "clname", clname);
2046 PUSH_FIELD_VALUE (method_symbol, "name", name);
2047 PUSH_FIELD_VALUE (method_symbol, "signature", signature);
2048 FINISH_RECORD_CONSTRUCTOR (method_symbol);
2049 TREE_CONSTANT (method_symbol) = 1;
2051 return method_symbol;
2054 /* Emit the offset symbols table for indirect virtual dispatch. */
2056 void
2057 emit_offset_symbol_table (void)
2059 tree method_list, method, table, list, null_symbol;
2060 tree otable_bound, otable_array_type;
2061 int index;
2063 /* Only emit an offset table if this translation unit actually made virtual
2064 calls. */
2065 if (otable_methods == NULL_TREE)
2066 return;
2068 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2069 index = 0;
2070 method_list = otable_methods;
2071 list = NULL_TREE;
2072 while (method_list != NULL_TREE)
2074 method = TREE_VALUE (method_list);
2075 list = tree_cons (NULL_TREE, build_method_symbols_entry (method), list);
2076 method_list = TREE_CHAIN (method_list);
2077 index++;
2080 /* Terminate the list with a "null" entry. */
2081 START_RECORD_CONSTRUCTOR (null_symbol, method_symbol_type);
2082 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2083 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2084 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2085 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2086 TREE_CONSTANT (null_symbol) = 1;
2087 list = tree_cons (NULL_TREE, null_symbol, list);
2089 /* Put the list in the right order and make it a constructor. */
2090 list = nreverse (list);
2091 table = build (CONSTRUCTOR, method_symbols_array_type, NULL_TREE, list);
2093 /* Make it the initial value for otable_syms and emit the decl. */
2094 DECL_INITIAL (otable_syms_decl) = table;
2095 DECL_ARTIFICIAL (otable_syms_decl) = 1;
2096 DECL_IGNORED_P (otable_syms_decl) = 1;
2097 rest_of_decl_compilation (otable_syms_decl, NULL, 1, 0);
2099 /* Now that its size is known, redefine otable as an uninitialized static
2100 array of INDEX + 1 integers. The extra entry is used by the runtime
2101 to track whether the otable has been initialized. */
2102 otable_bound = build_index_type (build_int_2 (index, 0));
2103 otable_array_type = build_array_type (integer_type_node, otable_bound);
2104 otable_decl = build_decl (VAR_DECL, get_identifier ("otable"),
2105 otable_array_type);
2106 TREE_STATIC (otable_decl) = 1;
2107 TREE_READONLY (otable_decl) = 1;
2108 rest_of_decl_compilation (otable_decl, NULL, 1, 0);
2111 void
2112 init_class_processing (void)
2114 registerClass_libfunc = gen_rtx_SYMBOL_REF (Pmode, "_Jv_RegisterClass");
2115 fields_ident = get_identifier ("fields");
2116 info_ident = get_identifier ("info");
2117 gcc_obstack_init (&temporary_obstack);
2120 static hashval_t java_treetreehash_hash (const void *);
2121 static int java_treetreehash_compare (const void *, const void *);
2123 /* A hash table mapping trees to trees. Used generally. */
2125 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2127 static hashval_t
2128 java_treetreehash_hash (const void *k_p)
2130 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2131 return JAVA_TREEHASHHASH_H (k->key);
2134 static int
2135 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2137 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2138 tree k2 = (tree) k2_p;
2139 return (k1->key == k2);
2142 tree
2143 java_treetreehash_find (htab_t ht, tree t)
2145 struct treetreehash_entry *e;
2146 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2147 e = (struct treetreehash_entry *) htab_find_with_hash (ht, t, hv);
2148 if (e == NULL)
2149 return NULL;
2150 else
2151 return e->value;
2154 tree *
2155 java_treetreehash_new (htab_t ht, tree t)
2157 void **e;
2158 struct treetreehash_entry *tthe;
2159 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2161 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2162 if (*e == NULL)
2164 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2165 tthe->key = t;
2166 *e = tthe;
2168 else
2169 tthe = (struct treetreehash_entry *) *e;
2170 return &tthe->value;
2173 htab_t
2174 java_treetreehash_create (size_t size, int gc)
2176 if (gc)
2177 return htab_create_ggc (size, java_treetreehash_hash,
2178 java_treetreehash_compare, NULL);
2179 else
2180 return htab_create_alloc (size, java_treetreehash_hash,
2181 java_treetreehash_compare, free, xcalloc, free);
2184 #include "gt-java-class.h"