* common.opt (-Wattributes): New. Default true.
[official-gcc.git] / gcc / java / class.c
blob3e8ae2693d65b607d6ada27c5dbdf563ef017883
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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"
45 #include "except.h"
46 #include "cgraph.h"
47 #include "tree-iterator.h"
48 #include "cgraph.h"
50 /* DOS brain-damage */
51 #ifndef O_BINARY
52 #define O_BINARY 0 /* MS-DOS brain-damage */
53 #endif
55 static tree make_method_value (tree);
56 static tree build_java_method_type (tree, tree, int);
57 static int32 hashUtf8String (const char *, int);
58 static tree make_field_value (tree);
59 static tree get_dispatch_vector (tree);
60 static tree get_dispatch_table (tree, tree);
61 static int supers_all_compiled (tree type);
62 static tree maybe_layout_super_class (tree, tree);
63 static void add_miranda_methods (tree, tree);
64 static int assume_compiled (const char *);
65 static tree build_symbol_entry (tree);
66 static tree emit_assertion_table (tree);
67 static void register_class (void);
69 struct obstack temporary_obstack;
71 /* The compiler generates different code depending on whether or not
72 it can assume certain classes have been compiled down to native
73 code or not. The compiler options -fassume-compiled= and
74 -fno-assume-compiled= are used to create a tree of
75 class_flag_node objects. This tree is queried to determine if
76 a class is assume to be compiled or not. Each node in the tree
77 represents either a package or a specific class. */
79 typedef struct class_flag_node_struct
81 /* The class or package name. */
82 const char *ident;
84 /* Nonzero if this represents an exclusion. */
85 int value;
87 /* Pointers to other nodes in the tree. */
88 struct class_flag_node_struct *parent;
89 struct class_flag_node_struct *sibling;
90 struct class_flag_node_struct *child;
91 } class_flag_node;
93 static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
94 static void add_class_flag (class_flag_node **, const char *, int);
96 /* This is the root of the include/exclude tree. */
98 static class_flag_node *assume_compiled_tree;
100 static class_flag_node *enable_assert_tree;
102 static GTY(()) tree class_roots[4];
103 #define fields_ident class_roots[0] /* get_identifier ("fields") */
104 #define info_ident class_roots[1] /* get_identifier ("info") */
105 #define class_list class_roots[2]
106 #define class_dtable_decl class_roots[3]
108 static GTY(()) VEC(tree,gc) *registered_class;
110 /* Return the node that most closely represents the class whose name
111 is IDENT. Start the search from NODE (followed by its siblings).
112 Return NULL if an appropriate node does not exist. */
114 static class_flag_node *
115 find_class_flag_node (class_flag_node *node, const char *ident)
117 while (node)
119 size_t node_ident_length = strlen (node->ident);
121 /* node_ident_length is zero at the root of the tree. If the
122 identifiers are the same length, then we have matching
123 classes. Otherwise check if we've matched an enclosing
124 package name. */
126 if (node_ident_length == 0
127 || (strncmp (ident, node->ident, node_ident_length) == 0
128 && (ident[node_ident_length] == '\0'
129 || ident[node_ident_length] == '.')))
131 /* We've found a match, however, there might be a more
132 specific match. */
134 class_flag_node *found = find_class_flag_node (node->child, ident);
135 if (found)
136 return found;
137 else
138 return node;
141 /* No match yet. Continue through the sibling list. */
142 node = node->sibling;
145 /* No match at all in this tree. */
146 return NULL;
149 void
150 add_class_flag (class_flag_node **rootp, const char *ident, int value)
152 class_flag_node *root = *rootp;
153 class_flag_node *parent, *node;
155 /* Create the root of the tree if it doesn't exist yet. */
157 if (NULL == root)
159 root = xmalloc (sizeof (class_flag_node));
160 root->ident = "";
161 root->value = 0;
162 root->sibling = NULL;
163 root->child = NULL;
164 root->parent = NULL;
165 *rootp = root;
168 /* Calling the function with the empty string means we're setting
169 value for the root of the hierarchy. */
171 if (0 == ident[0])
173 root->value = value;
174 return;
177 /* Find the parent node for this new node. PARENT will either be a
178 class or a package name. Adjust PARENT accordingly. */
180 parent = find_class_flag_node (root, ident);
181 if (strcmp (ident, parent->ident) == 0)
182 parent->value = value;
183 else
185 /* Insert new node into the tree. */
186 node = xmalloc (sizeof (class_flag_node));
188 node->ident = xstrdup (ident);
189 node->value = value;
190 node->child = NULL;
192 node->parent = parent;
193 node->sibling = parent->child;
194 parent->child = node;
198 /* Add a new IDENT to the include/exclude tree. It's an exclusion
199 if EXCLUDEP is nonzero. */
201 void
202 add_assume_compiled (const char *ident, int excludep)
204 add_class_flag (&assume_compiled_tree, ident, excludep);
207 /* The default value returned by enable_assertions. */
209 #define DEFAULT_ENABLE_ASSERT (flag_emit_class_files || optimize == 0)
211 /* Enter IDENT (a class or package name) into the enable-assertions table.
212 VALUE is true to enable and false to disable. */
214 void
215 add_enable_assert (const char *ident, int value)
217 if (enable_assert_tree == NULL)
218 add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
219 add_class_flag (&enable_assert_tree, ident, value);
222 /* Returns nonzero if IDENT is the name of a class that the compiler
223 should assume has been compiled to object code. */
225 static int
226 assume_compiled (const char *ident)
228 class_flag_node *i;
229 int result;
231 if (NULL == assume_compiled_tree)
232 return 1;
234 i = find_class_flag_node (assume_compiled_tree, ident);
236 result = ! i->value;
238 return (result);
241 /* Return true if we should generate code to check assertions within KLASS. */
243 bool
244 enable_assertions (tree klass)
246 /* Check if command-line specifies whether we should check assertions. */
248 if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
250 const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
251 class_flag_node *node
252 = find_class_flag_node (enable_assert_tree, ident);
253 return node->value;
256 /* The default is to enable assertions if generating class files,
257 or not optimizing. */
258 return DEFAULT_ENABLE_ASSERT;
261 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
262 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
263 Also, PREFIX is prepended, and SUFFIX is appended. */
265 tree
266 ident_subst (const char* old_name,
267 int old_length,
268 const char *prefix,
269 int old_char,
270 int new_char,
271 const char *suffix)
273 int prefix_len = strlen (prefix);
274 int suffix_len = strlen (suffix);
275 int i = prefix_len + old_length + suffix_len + 1;
276 char *buffer = alloca (i);
278 strcpy (buffer, prefix);
279 for (i = 0; i < old_length; i++)
281 char ch = old_name[i];
282 if (ch == old_char)
283 ch = new_char;
284 buffer[prefix_len + i] = ch;
286 strcpy (buffer + prefix_len + old_length, suffix);
287 return get_identifier (buffer);
290 /* Return an IDENTIFIER_NODE the same as OLD_ID,
291 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
292 Also, PREFIX is prepended, and SUFFIX is appended. */
294 tree
295 identifier_subst (const tree old_id,
296 const char *prefix,
297 int old_char,
298 int new_char,
299 const char *suffix)
301 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
302 prefix, old_char, new_char, suffix);
305 /* Generate a valid C identifier from the name of the class TYPE,
306 prefixed by PREFIX. */
308 tree
309 mangled_classname (const char *prefix, tree type)
311 tree ident = TYPE_NAME (type);
312 if (TREE_CODE (ident) != IDENTIFIER_NODE)
313 ident = DECL_NAME (ident);
314 return identifier_subst (ident, prefix, '.', '_', "");
317 tree
318 make_class (void)
320 tree type;
321 type = make_node (RECORD_TYPE);
322 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
324 return type;
327 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
328 and where each of the constituents is separated by '/',
329 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
331 tree
332 unmangle_classname (const char *name, int name_length)
334 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
335 /* It's not sufficient to compare to_return and get_identifier
336 (name) to determine whether to_return is qualified. There are
337 cases in signature analysis where name will be stripped of a
338 trailing ';'. */
339 name = IDENTIFIER_POINTER (to_return);
340 while (*name)
341 if (*name++ == '.')
343 QUALIFIED_P (to_return) = 1;
344 break;
347 return to_return;
350 #define GEN_TABLE(TABLE, NAME, TABLE_TYPE, TYPE) \
351 do \
353 const char *typename = IDENTIFIER_POINTER (mangled_classname ("", TYPE)); \
354 char *buf = alloca (strlen (typename) + strlen (#NAME "_syms_") + 1); \
355 tree decl; \
357 sprintf (buf, #NAME "_%s", typename); \
358 TYPE_## TABLE ##_DECL (type) = decl = \
359 build_decl (VAR_DECL, get_identifier (buf), TABLE_TYPE); \
360 DECL_EXTERNAL (decl) = 1; \
361 TREE_STATIC (decl) = 1; \
362 TREE_READONLY (decl) = 1; \
363 TREE_CONSTANT (decl) = 1; \
364 DECL_IGNORED_P (decl) = 1; \
365 /* Mark the table as belonging to this class. */ \
366 pushdecl (decl); \
367 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl); \
368 DECL_OWNER (decl) = TYPE; \
369 sprintf (buf, #NAME "_syms_%s", typename); \
370 TYPE_## TABLE ##_SYMS_DECL (TYPE) = \
371 build_decl (VAR_DECL, get_identifier (buf), symbols_array_type); \
372 TREE_STATIC (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
373 TREE_CONSTANT (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
374 DECL_IGNORED_P (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
375 pushdecl (TYPE_## TABLE ##_SYMS_DECL (TYPE)); \
377 while (0)
379 /* Given a class, create the DECLs for all its associated indirect
380 dispatch tables. */
381 void
382 gen_indirect_dispatch_tables (tree type)
384 const char *typename = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
386 tree field = NULL;
387 char *buf = alloca (strlen (typename) + strlen ("_catch_classes_") + 1);
388 tree catch_class_type = make_node (RECORD_TYPE);
390 sprintf (buf, "_catch_classes_%s", typename);
391 PUSH_FIELD (catch_class_type, field, "address", utf8const_ptr_type);
392 PUSH_FIELD (catch_class_type, field, "classname", ptr_type_node);
393 FINISH_RECORD (catch_class_type);
395 TYPE_CTABLE_DECL (type)
396 = build_decl (VAR_DECL, get_identifier (buf),
397 build_array_type (catch_class_type, 0));
398 DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
399 TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
400 TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
401 TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
402 DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
403 pushdecl (TYPE_CTABLE_DECL (type));
406 if (flag_indirect_dispatch)
408 GEN_TABLE (ATABLE, _atable, atable_type, type);
409 GEN_TABLE (OTABLE, _otable, otable_type, type);
410 GEN_TABLE (ITABLE, _itable, itable_type, type);
414 #undef GEN_TABLE
416 tree
417 push_class (tree class_type, tree class_name)
419 tree decl, signature;
420 location_t saved_loc = input_location;
421 #ifndef USE_MAPPED_LOCATION
422 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
423 input_filename = IDENTIFIER_POINTER (source_name);
424 input_line = 0;
425 #endif
426 CLASS_P (class_type) = 1;
427 decl = build_decl (TYPE_DECL, class_name, class_type);
428 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
430 /* dbxout needs a DECL_SIZE if in gstabs mode */
431 DECL_SIZE (decl) = integer_zero_node;
433 input_location = saved_loc;
434 signature = identifier_subst (class_name, "L", '.', '/', ";");
435 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
437 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
438 both a typedef and in the struct name-space. We may want to re-visit
439 this later, but for now it reduces the changes needed for gdb. */
440 DECL_ARTIFICIAL (decl) = 1;
442 pushdecl_top_level (decl);
444 return decl;
447 /* Finds the (global) class named NAME. Creates the class if not found.
448 Also creates associated TYPE_DECL.
449 Does not check if the class actually exists, load the class,
450 fill in field or methods, or do layout_type. */
452 tree
453 lookup_class (tree name)
455 tree decl = IDENTIFIER_CLASS_VALUE (name);
456 if (decl == NULL_TREE)
457 decl = push_class (make_class (), name);
458 return TREE_TYPE (decl);
461 void
462 set_super_info (int access_flags, tree this_class,
463 tree super_class, int interfaces_count)
465 int total_supers = interfaces_count;
466 tree class_decl = TYPE_NAME (this_class);
468 if (super_class)
469 total_supers++;
471 TYPE_BINFO (this_class) = make_tree_binfo (total_supers);
472 TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
473 if (super_class)
475 tree super_binfo = make_tree_binfo (0);
476 BINFO_TYPE (super_binfo) = super_class;
477 BINFO_OFFSET (super_binfo) = integer_zero_node;
478 BINFO_BASE_APPEND (TYPE_BINFO (this_class), super_binfo);
479 CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
482 set_class_decl_access_flags (access_flags, class_decl);
485 void
486 set_class_decl_access_flags (int access_flags, tree class_decl)
488 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
489 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
490 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
491 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
492 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
493 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
494 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
495 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
496 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
499 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
500 direct sub-classes of Object are 1, and so on. */
503 class_depth (tree clas)
505 int depth = 0;
506 if (! CLASS_LOADED_P (clas))
507 load_class (clas, 1);
508 if (TYPE_SIZE (clas) == error_mark_node)
509 return -1;
510 while (clas != object_type_node)
512 depth++;
513 clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
515 return depth;
518 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
521 interface_of_p (tree type1, tree type2)
523 int i;
524 tree binfo, base_binfo;
526 if (! TYPE_BINFO (type2))
527 return 0;
529 for (binfo = TYPE_BINFO (type2), i = 0;
530 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
531 if (BINFO_TYPE (base_binfo) == type1)
532 return 1;
534 for (binfo = TYPE_BINFO (type2), i = 0;
535 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) /* */
536 if (BINFO_TYPE (base_binfo)
537 && interface_of_p (type1, BINFO_TYPE (base_binfo)))
538 return 1;
540 return 0;
543 /* Return true iff TYPE1 inherits from TYPE2. */
546 inherits_from_p (tree type1, tree type2)
548 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
550 if (type1 == type2)
551 return 1;
552 type1 = CLASSTYPE_SUPER (type1);
554 return 0;
557 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
560 enclosing_context_p (tree type1, tree type2)
562 if (!INNER_CLASS_TYPE_P (type2))
563 return 0;
565 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
566 type2;
567 type2 = (INNER_CLASS_TYPE_P (type2) ?
568 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
570 if (type2 == type1)
571 return 1;
574 return 0;
578 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
579 nesting level. */
582 common_enclosing_context_p (tree type1, tree type2)
584 while (type1)
586 tree current;
587 for (current = type2; current;
588 current = (INNER_CLASS_TYPE_P (current) ?
589 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
590 NULL_TREE))
591 if (type1 == current)
592 return 1;
594 if (INNER_CLASS_TYPE_P (type1))
595 type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
596 else
597 break;
599 return 0;
602 /* Return 1 iff there exists a common enclosing "this" between TYPE1
603 and TYPE2, without crossing any static context. */
606 common_enclosing_instance_p (tree type1, tree type2)
608 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
609 return 0;
611 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
612 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
613 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
615 tree current;
616 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
617 current = (PURE_INNER_CLASS_TYPE_P (current) ?
618 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
619 NULL_TREE))
620 if (type1 == current)
621 return 1;
623 return 0;
626 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
627 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
628 if attempt is made to add it twice. */
630 tree
631 maybe_add_interface (tree this_class, tree interface_class)
633 tree binfo, base_binfo;
634 int i;
636 for (binfo = TYPE_BINFO (this_class), i = 0;
637 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
638 if (BINFO_TYPE (base_binfo) == interface_class)
639 return interface_class;
640 add_interface (this_class, interface_class);
641 return NULL_TREE;
644 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
646 void
647 add_interface (tree this_class, tree interface_class)
649 tree interface_binfo = make_tree_binfo (0);
651 BINFO_TYPE (interface_binfo) = interface_class;
652 BINFO_OFFSET (interface_binfo) = integer_zero_node;
653 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
654 BINFO_VIRTUAL_P (interface_binfo) = 1;
656 BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
659 #if 0
660 /* Return the address of a pointer to the first FUNCTION_DECL
661 in the list (*LIST) whose DECL_NAME is NAME. */
663 static tree *
664 find_named_method (tree *list, tree name)
666 while (*list && DECL_NAME (*list) != name)
667 list = &TREE_CHAIN (*list);
668 return list;
670 #endif
672 static tree
673 build_java_method_type (tree fntype, tree this_class, int access_flags)
675 if (access_flags & ACC_STATIC)
676 return fntype;
677 return build_method_type (this_class, fntype);
680 tree
681 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
683 tree method_type, fndecl;
685 method_type = build_java_method_type (function_type,
686 this_class, access_flags);
688 fndecl = build_decl (FUNCTION_DECL, name, method_type);
689 DECL_CONTEXT (fndecl) = this_class;
691 DECL_LANG_SPECIFIC (fndecl)
692 = ggc_alloc_cleared (sizeof (struct lang_decl));
693 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
695 /* Initialize the static initializer test table. */
697 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
698 java_treetreehash_create (10, 1);
700 /* Initialize the initialized (static) class table. */
701 if (access_flags & ACC_STATIC)
702 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
703 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
705 /* Initialize the static method invocation compound list */
706 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
708 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
709 TYPE_METHODS (this_class) = fndecl;
711 /* Notice that this is a finalizer and update the class type
712 accordingly. This is used to optimize instance allocation. */
713 if (name == finalize_identifier_node
714 && TREE_TYPE (function_type) == void_type_node
715 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
716 HAS_FINALIZER_P (this_class) = 1;
718 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
719 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
720 if (access_flags & ACC_PRIVATE)
721 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
722 if (access_flags & ACC_NATIVE)
724 METHOD_NATIVE (fndecl) = 1;
725 DECL_EXTERNAL (fndecl) = 1;
727 if (access_flags & ACC_STATIC)
728 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
729 if (access_flags & ACC_FINAL)
730 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
731 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
732 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
733 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
734 return fndecl;
737 /* Add a method to THIS_CLASS.
738 The method's name is NAME.
739 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
741 tree
742 add_method (tree this_class, int access_flags, tree name, tree method_sig)
744 tree function_type, fndecl;
745 const unsigned char *sig
746 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
748 if (sig[0] != '(')
749 fatal_error ("bad method signature");
751 function_type = get_type_from_signature (method_sig);
752 fndecl = add_method_1 (this_class, access_flags, name, function_type);
753 set_java_signature (TREE_TYPE (fndecl), method_sig);
754 return fndecl;
757 tree
758 add_field (tree class, tree name, tree field_type, int flags)
760 int is_static = (flags & ACC_STATIC) != 0;
761 tree field;
762 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
763 TREE_CHAIN (field) = TYPE_FIELDS (class);
764 TYPE_FIELDS (class) = field;
765 DECL_CONTEXT (field) = class;
767 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
768 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
769 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
770 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
771 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
772 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
773 if (is_static)
775 FIELD_STATIC (field) = 1;
776 /* Always make field externally visible. This is required so
777 that native methods can always access the field. */
778 TREE_PUBLIC (field) = 1;
779 /* Considered external until we know what classes are being
780 compiled into this object file. */
781 DECL_EXTERNAL (field) = 1;
784 return field;
787 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
789 void
790 set_constant_value (tree field, tree constant)
792 if (field == NULL_TREE)
793 warning (OPT_Wattributes,
794 "misplaced ConstantValue attribute (not in any field)");
795 else if (DECL_INITIAL (field) != NULL_TREE)
796 warning (OPT_Wattributes,
797 "duplicate ConstantValue attribute for field '%s'",
798 IDENTIFIER_POINTER (DECL_NAME (field)));
799 else
801 DECL_INITIAL (field) = constant;
802 if (TREE_TYPE (constant) != TREE_TYPE (field)
803 && ! (TREE_TYPE (constant) == int_type_node
804 && INTEGRAL_TYPE_P (TREE_TYPE (field))
805 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
806 && ! (TREE_TYPE (constant) == utf8const_ptr_type
807 && TREE_TYPE (field) == string_ptr_type_node))
808 error ("ConstantValue attribute of field '%s' has wrong type",
809 IDENTIFIER_POINTER (DECL_NAME (field)));
810 if (FIELD_FINAL (field))
811 DECL_FIELD_FINAL_IUD (field) = 1;
815 /* Count the number of Unicode chars encoded in a given Ut8 string. */
817 #if 0
819 strLengthUtf8 (char *str, int len)
821 register unsigned char* ptr = (unsigned char*) str;
822 register unsigned char *limit = ptr + len;
823 int str_length = 0;
824 for (; ptr < limit; str_length++) {
825 if (UTF8_GET (ptr, limit) < 0)
826 return -1;
828 return str_length;
830 #endif
833 /* Calculate a hash value for a string encoded in Utf8 format.
834 * This returns the same hash value as specified for java.lang.String.hashCode.
837 static int32
838 hashUtf8String (const char *str, int len)
840 const unsigned char* ptr = (const unsigned char*) str;
841 const unsigned char *limit = ptr + len;
842 int32 hash = 0;
843 for (; ptr < limit;)
845 int ch = UTF8_GET (ptr, limit);
846 /* Updated specification from
847 http://www.javasoft.com/docs/books/jls/clarify.html. */
848 hash = (31 * hash) + ch;
850 return hash;
853 static GTY(()) tree utf8_decl_list = NULL_TREE;
855 tree
856 build_utf8_ref (tree name)
858 const char * name_ptr = IDENTIFIER_POINTER(name);
859 int name_len = IDENTIFIER_LENGTH(name);
860 char buf[60];
861 tree ctype, field = NULL_TREE, str_type, cinit, string;
862 static int utf8_count = 0;
863 int name_hash;
864 tree ref = IDENTIFIER_UTF8_REF (name);
865 tree decl;
866 if (ref != NULL_TREE)
867 return ref;
869 ctype = make_node (RECORD_TYPE);
870 str_type = build_prim_array_type (unsigned_byte_type_node,
871 name_len + 1); /* Allow for final '\0'. */
872 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
873 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
874 PUSH_FIELD (ctype, field, "data", str_type);
875 FINISH_RECORD (ctype);
876 START_RECORD_CONSTRUCTOR (cinit, ctype);
877 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
878 PUSH_FIELD_VALUE (cinit, "hash", build_int_cst (NULL_TREE, name_hash));
879 PUSH_FIELD_VALUE (cinit, "length", build_int_cst (NULL_TREE, name_len));
880 string = build_string (name_len, name_ptr);
881 TREE_TYPE (string) = str_type;
882 PUSH_FIELD_VALUE (cinit, "data", string);
883 FINISH_RECORD_CONSTRUCTOR (cinit);
884 TREE_CONSTANT (cinit) = 1;
885 TREE_INVARIANT (cinit) = 1;
887 /* Generate a unique-enough identifier. */
888 sprintf(buf, "_Utf%d", ++utf8_count);
890 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
891 TREE_STATIC (decl) = 1;
892 DECL_ARTIFICIAL (decl) = 1;
893 DECL_IGNORED_P (decl) = 1;
894 TREE_READONLY (decl) = 1;
895 TREE_THIS_VOLATILE (decl) = 0;
896 DECL_INITIAL (decl) = cinit;
898 if (HAVE_GAS_SHF_MERGE)
900 int decl_size;
901 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
902 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
903 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
904 if (flag_merge_constants && decl_size < 256)
906 char buf[32];
907 int flags = (SECTION_OVERRIDE
908 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
909 sprintf (buf, ".rodata.jutf8.%d", decl_size);
910 named_section_flags (buf, flags);
911 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
915 TREE_CHAIN (decl) = utf8_decl_list;
916 layout_decl (decl, 0);
917 pushdecl (decl);
918 rest_of_decl_compilation (decl, global_bindings_p (), 0);
919 cgraph_varpool_mark_needed_node (cgraph_varpool_node (decl));
920 utf8_decl_list = decl;
921 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
922 IDENTIFIER_UTF8_REF (name) = ref;
923 return ref;
926 /* Like build_class_ref, but instead of a direct reference generate a
927 pointer into the constant pool. */
929 static tree
930 build_indirect_class_ref (tree type)
932 int index;
933 tree cl;
934 index = alloc_class_constant (type);
935 cl = build_ref_from_constant_pool (index);
936 return convert (promote_type (class_ptr_type), cl);
939 /* Build a reference to the class TYPE.
940 Also handles primitive types and array types. */
942 tree
943 build_class_ref (tree type)
945 int is_compiled = is_compiled_class (type);
946 if (is_compiled)
948 tree ref, decl_name, decl;
949 if (TREE_CODE (type) == POINTER_TYPE)
950 type = TREE_TYPE (type);
952 if (flag_indirect_dispatch
953 && type != output_class
954 && TREE_CODE (type) == RECORD_TYPE)
955 return build_indirect_class_ref (type);
957 if (TREE_CODE (type) == RECORD_TYPE)
959 if (TYPE_SIZE (type) == error_mark_node)
960 return null_pointer_node;
961 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
962 "", '/', '/', ".class");
963 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
964 if (decl == NULL_TREE)
966 decl = build_decl (VAR_DECL, decl_name, class_type_node);
967 TREE_STATIC (decl) = 1;
968 TREE_PUBLIC (decl) = 1;
969 DECL_IGNORED_P (decl) = 1;
970 DECL_ARTIFICIAL (decl) = 1;
971 if (is_compiled == 1)
972 DECL_EXTERNAL (decl) = 1;
973 SET_DECL_ASSEMBLER_NAME (decl,
974 java_mangle_class_field
975 (&temporary_obstack, type));
976 pushdecl_top_level (decl);
979 else
981 const char *name;
982 char buffer[25];
983 if (flag_emit_class_files)
985 const char *prim_class_name;
986 tree prim_class;
987 if (type == char_type_node)
988 prim_class_name = "java.lang.Character";
989 else if (type == boolean_type_node)
990 prim_class_name = "java.lang.Boolean";
991 else if (type == byte_type_node)
992 prim_class_name = "java.lang.Byte";
993 else if (type == short_type_node)
994 prim_class_name = "java.lang.Short";
995 else if (type == int_type_node)
996 prim_class_name = "java.lang.Integer";
997 else if (type == long_type_node)
998 prim_class_name = "java.lang.Long";
999 else if (type == float_type_node)
1000 prim_class_name = "java.lang.Float";
1001 else if (type == double_type_node)
1002 prim_class_name = "java.lang.Double";
1003 else if (type == void_type_node)
1004 prim_class_name = "java.lang.Void";
1005 else
1006 abort ();
1008 prim_class = lookup_class (get_identifier (prim_class_name));
1009 return build3 (COMPONENT_REF, NULL_TREE,
1010 prim_class, TYPE_identifier_node, NULL_TREE);
1012 decl_name = TYPE_NAME (type);
1013 if (TREE_CODE (decl_name) == TYPE_DECL)
1014 decl_name = DECL_NAME (decl_name);
1015 name = IDENTIFIER_POINTER (decl_name);
1016 if (strncmp (name, "promoted_", 9) == 0)
1017 name += 9;
1018 sprintf (buffer, "_Jv_%sClass", name);
1019 decl_name = get_identifier (buffer);
1020 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1021 if (decl == NULL_TREE)
1023 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1024 TREE_STATIC (decl) = 1;
1025 TREE_PUBLIC (decl) = 1;
1026 DECL_EXTERNAL (decl) = 1;
1027 DECL_ARTIFICIAL (decl) = 1;
1028 pushdecl_top_level (decl);
1032 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1033 return ref;
1035 else
1036 return build_indirect_class_ref (type);
1039 /* Create a local statically allocated variable that will hold a
1040 pointer to a static field. */
1042 static tree
1043 build_fieldref_cache_entry (int index, tree fdecl ATTRIBUTE_UNUSED)
1045 tree decl, decl_name;
1046 const char *name = IDENTIFIER_POINTER (mangled_classname ("_cpool_", output_class));
1047 char *buf = alloca (strlen (name) + 20);
1048 sprintf (buf, "%s_%d_ref", name, index);
1049 decl_name = get_identifier (buf);
1050 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1051 if (decl == NULL_TREE)
1053 decl = build_decl (VAR_DECL, decl_name, ptr_type_node);
1054 TREE_STATIC (decl) = 1;
1055 TREE_PUBLIC (decl) = 0;
1056 DECL_EXTERNAL (decl) = 0;
1057 DECL_ARTIFICIAL (decl) = 1;
1058 pushdecl_top_level (decl);
1060 return decl;
1063 tree
1064 build_static_field_ref (tree fdecl)
1066 tree fclass = DECL_CONTEXT (fdecl);
1067 int is_compiled = is_compiled_class (fclass);
1069 /* Allow static final fields to fold to a constant. When using
1070 -fno-assume-compiled, gcj will sometimes try to fold a field from
1071 an uncompiled class. This is required when the field in question
1072 meets the appropriate criteria for a compile-time constant.
1073 However, currently sometimes gcj is too eager and will end up
1074 returning the field itself, leading to an incorrect external
1075 reference being generated. */
1076 if ((is_compiled && !flag_indirect_dispatch)
1077 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1078 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1079 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1080 && TREE_CONSTANT (DECL_INITIAL (fdecl))))
1082 if (is_compiled == 1)
1083 DECL_EXTERNAL (fdecl) = 1;
1085 else
1087 /* Generate a CONSTANT_FieldRef for FDECL in the constant pool
1088 and a class local static variable CACHE_ENTRY, then
1090 *(fdecl **)((__builtin_expect (cache_entry == null, false))
1091 ? cache_entry = _Jv_ResolvePoolEntry (output_class, cpool_index)
1092 : cache_entry)
1094 This can mostly be optimized away, so that the usual path is a
1095 load followed by a test and branch. _Jv_ResolvePoolEntry is
1096 only called once for each constant pool entry.
1098 There is an optimization that we don't do: at the start of a
1099 method, create a local copy of CACHE_ENTRY and use that instead.
1103 int cpool_index = alloc_constant_fieldref (output_class, fdecl);
1104 tree cache_entry = build_fieldref_cache_entry (cpool_index, fdecl);
1105 tree test
1106 = build3 (CALL_EXPR, boolean_type_node,
1107 build_address_of (built_in_decls[BUILT_IN_EXPECT]),
1108 tree_cons (NULL_TREE, build2 (EQ_EXPR, boolean_type_node,
1109 cache_entry, null_pointer_node),
1110 build_tree_list (NULL_TREE, boolean_false_node)),
1111 NULL_TREE);
1112 tree cpool_index_cst = build_int_cst (NULL_TREE, cpool_index);
1113 tree init
1114 = build3 (CALL_EXPR, ptr_type_node,
1115 build_address_of (soft_resolvepoolentry_node),
1116 tree_cons (NULL_TREE, build_class_ref (output_class),
1117 build_tree_list (NULL_TREE, cpool_index_cst)),
1118 NULL_TREE);
1119 init = build2 (MODIFY_EXPR, ptr_type_node, cache_entry, init);
1120 init = build3 (COND_EXPR, ptr_type_node, test, init, cache_entry);
1121 init = fold_convert (build_pointer_type (TREE_TYPE (fdecl)), init);
1122 fdecl = build1 (INDIRECT_REF, TREE_TYPE (fdecl), init);
1124 return fdecl;
1128 get_access_flags_from_decl (tree decl)
1130 int access_flags = 0;
1131 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1133 if (FIELD_STATIC (decl))
1134 access_flags |= ACC_STATIC;
1135 if (FIELD_PUBLIC (decl))
1136 access_flags |= ACC_PUBLIC;
1137 if (FIELD_PROTECTED (decl))
1138 access_flags |= ACC_PROTECTED;
1139 if (FIELD_PRIVATE (decl))
1140 access_flags |= ACC_PRIVATE;
1141 if (FIELD_FINAL (decl))
1142 access_flags |= ACC_FINAL;
1143 if (FIELD_VOLATILE (decl))
1144 access_flags |= ACC_VOLATILE;
1145 if (FIELD_TRANSIENT (decl))
1146 access_flags |= ACC_TRANSIENT;
1147 return access_flags;
1149 if (TREE_CODE (decl) == TYPE_DECL)
1151 if (CLASS_PUBLIC (decl))
1152 access_flags |= ACC_PUBLIC;
1153 if (CLASS_FINAL (decl))
1154 access_flags |= ACC_FINAL;
1155 if (CLASS_SUPER (decl))
1156 access_flags |= ACC_SUPER;
1157 if (CLASS_INTERFACE (decl))
1158 access_flags |= ACC_INTERFACE;
1159 if (CLASS_ABSTRACT (decl))
1160 access_flags |= ACC_ABSTRACT;
1161 if (CLASS_STATIC (decl))
1162 access_flags |= ACC_STATIC;
1163 if (CLASS_PRIVATE (decl))
1164 access_flags |= ACC_PRIVATE;
1165 if (CLASS_PROTECTED (decl))
1166 access_flags |= ACC_PROTECTED;
1167 if (CLASS_STRICTFP (decl))
1168 access_flags |= ACC_STRICT;
1169 return access_flags;
1171 if (TREE_CODE (decl) == FUNCTION_DECL)
1173 if (METHOD_PUBLIC (decl))
1174 access_flags |= ACC_PUBLIC;
1175 if (METHOD_PRIVATE (decl))
1176 access_flags |= ACC_PRIVATE;
1177 if (METHOD_PROTECTED (decl))
1178 access_flags |= ACC_PROTECTED;
1179 if (METHOD_STATIC (decl))
1180 access_flags |= ACC_STATIC;
1181 if (METHOD_FINAL (decl))
1182 access_flags |= ACC_FINAL;
1183 if (METHOD_SYNCHRONIZED (decl))
1184 access_flags |= ACC_SYNCHRONIZED;
1185 if (METHOD_NATIVE (decl))
1186 access_flags |= ACC_NATIVE;
1187 if (METHOD_ABSTRACT (decl))
1188 access_flags |= ACC_ABSTRACT;
1189 if (METHOD_STRICTFP (decl))
1190 access_flags |= ACC_STRICT;
1191 if (METHOD_INVISIBLE (decl))
1192 access_flags |= ACC_INVISIBLE;
1193 return access_flags;
1195 abort ();
1198 static GTY (()) int alias_labelno = 0;
1200 /* Create a private alias for METHOD. Using this alias instead of the method
1201 decl ensures that ncode entries in the method table point to the real function
1202 at runtime, not a PLT entry. */
1204 static tree
1205 make_local_function_alias (tree method)
1207 #ifdef ASM_OUTPUT_DEF
1208 tree alias;
1210 const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1211 char *name = alloca (strlen (method_name) + 2);
1212 char *buf = alloca (strlen (method_name) + 128);
1214 /* Only create aliases for local functions. */
1215 if (DECL_EXTERNAL (method))
1216 return method;
1218 /* Prefix method_name with 'L' for the alias label. */
1219 *name = 'L';
1220 strcpy (name + 1, method_name);
1222 ASM_GENERATE_INTERNAL_LABEL (buf, name, alias_labelno++);
1223 alias = build_decl (FUNCTION_DECL, get_identifier (buf),
1224 TREE_TYPE (method));
1225 DECL_CONTEXT (alias) = NULL;
1226 TREE_READONLY (alias) = TREE_READONLY (method);
1227 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1228 TREE_PUBLIC (alias) = 0;
1229 DECL_EXTERNAL (alias) = 0;
1230 DECL_ARTIFICIAL (alias) = 1;
1231 DECL_INLINE (alias) = 0;
1232 DECL_INITIAL (alias) = error_mark_node;
1233 TREE_ADDRESSABLE (alias) = 1;
1234 TREE_USED (alias) = 1;
1235 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
1236 if (!flag_syntax_only)
1237 assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1238 return alias;
1239 #else
1240 return method;
1241 #endif
1244 /** Make reflection data (_Jv_Field) for field FDECL. */
1246 static tree
1247 make_field_value (tree fdecl)
1249 tree finit;
1250 int flags;
1251 tree type = TREE_TYPE (fdecl);
1252 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1254 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1255 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1256 if (resolved)
1257 type = build_class_ref (type);
1258 else
1260 tree signature = build_java_signature (type);
1262 type = build_utf8_ref (unmangle_classname
1263 (IDENTIFIER_POINTER (signature),
1264 IDENTIFIER_LENGTH (signature)));
1266 PUSH_FIELD_VALUE (finit, "type", type);
1268 flags = get_access_flags_from_decl (fdecl);
1269 if (! resolved)
1270 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1272 PUSH_FIELD_VALUE (finit, "accflags", build_int_cst (NULL_TREE, flags));
1273 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1275 PUSH_FIELD_VALUE
1276 (finit, "info",
1277 build_constructor (field_info_union_node,
1278 build_tree_list
1279 ((FIELD_STATIC (fdecl)
1280 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1281 : TYPE_FIELDS (field_info_union_node)),
1282 (FIELD_STATIC (fdecl)
1283 ? build_address_of (fdecl)
1284 : byte_position (fdecl)))));
1286 FINISH_RECORD_CONSTRUCTOR (finit);
1287 return finit;
1290 /** Make reflection data (_Jv_Method) for method MDECL. */
1292 static tree
1293 make_method_value (tree mdecl)
1295 static int method_name_count = 0;
1296 tree minit;
1297 tree index;
1298 tree code;
1299 tree class_decl;
1300 #define ACC_TRANSLATED 0x4000
1301 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1303 class_decl = DECL_CONTEXT (mdecl);
1304 /* For interfaces, the index field contains the dispatch index. */
1305 if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1306 index = build_int_cst (NULL_TREE,
1307 get_interface_method_index (mdecl, class_decl));
1308 else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1309 index = get_method_index (mdecl);
1310 else
1311 index = integer_minus_one_node;
1313 code = null_pointer_node;
1314 if (!METHOD_ABSTRACT (mdecl))
1315 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1316 make_local_function_alias (mdecl));
1317 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1318 PUSH_FIELD_VALUE (minit, "name",
1319 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1320 init_identifier_node
1321 : DECL_NAME (mdecl)));
1323 tree signature = build_java_signature (TREE_TYPE (mdecl));
1324 PUSH_FIELD_VALUE (minit, "signature",
1325 (build_utf8_ref
1326 (unmangle_classname
1327 (IDENTIFIER_POINTER(signature),
1328 IDENTIFIER_LENGTH(signature)))));
1330 PUSH_FIELD_VALUE (minit, "accflags", build_int_cst (NULL_TREE, accflags));
1331 PUSH_FIELD_VALUE (minit, "index", index);
1332 PUSH_FIELD_VALUE (minit, "ncode", code);
1335 /* Compute the `throws' information for the method. */
1336 tree table = null_pointer_node;
1337 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1339 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1340 tree iter, type, array;
1341 char buf[60];
1343 table = tree_cons (NULL_TREE, table, NULL_TREE);
1344 for (iter = DECL_FUNCTION_THROWS (mdecl);
1345 iter != NULL_TREE;
1346 iter = TREE_CHAIN (iter))
1348 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1349 tree utf8
1350 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1351 IDENTIFIER_LENGTH (sig)));
1352 table = tree_cons (NULL_TREE, utf8, table);
1354 type = build_prim_array_type (ptr_type_node, length);
1355 table = build_constructor (type, table);
1356 /* Compute something unique enough. */
1357 sprintf (buf, "_methods%d", method_name_count++);
1358 array = build_decl (VAR_DECL, get_identifier (buf), type);
1359 DECL_INITIAL (array) = table;
1360 TREE_STATIC (array) = 1;
1361 DECL_ARTIFICIAL (array) = 1;
1362 DECL_IGNORED_P (array) = 1;
1363 rest_of_decl_compilation (array, 1, 0);
1365 table = build1 (ADDR_EXPR, ptr_type_node, array);
1368 PUSH_FIELD_VALUE (minit, "throws", table);
1371 FINISH_RECORD_CONSTRUCTOR (minit);
1372 return minit;
1375 static tree
1376 get_dispatch_vector (tree type)
1378 tree vtable = TYPE_VTABLE (type);
1380 if (vtable == NULL_TREE)
1382 HOST_WIDE_INT i;
1383 tree method;
1384 tree super = CLASSTYPE_SUPER (type);
1385 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1386 vtable = make_tree_vec (nvirtuals);
1387 TYPE_VTABLE (type) = vtable;
1388 if (super != NULL_TREE)
1390 tree super_vtable = get_dispatch_vector (super);
1392 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1393 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1396 for (method = TYPE_METHODS (type); method != NULL_TREE;
1397 method = TREE_CHAIN (method))
1399 tree method_index = get_method_index (method);
1400 if (method_index != NULL_TREE
1401 && host_integerp (method_index, 0))
1402 TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1406 return vtable;
1409 static tree
1410 get_dispatch_table (tree type, tree this_class_addr)
1412 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1413 tree vtable = get_dispatch_vector (type);
1414 int i, j;
1415 tree list = NULL_TREE;
1416 int nvirtuals = TREE_VEC_LENGTH (vtable);
1417 int arraysize;
1418 tree gc_descr;
1420 for (i = nvirtuals; --i >= 0; )
1422 tree method = TREE_VEC_ELT (vtable, i);
1423 if (METHOD_ABSTRACT (method))
1425 if (! abstract_p)
1426 warning (0, "%Jabstract method in non-abstract class", method);
1428 if (TARGET_VTABLE_USES_DESCRIPTORS)
1429 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1430 list = tree_cons (NULL_TREE, null_pointer_node, list);
1431 else
1432 list = tree_cons (NULL_TREE, null_pointer_node, list);
1434 else
1436 if (TARGET_VTABLE_USES_DESCRIPTORS)
1437 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1439 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1440 method, build_int_cst (NULL_TREE, j));
1441 TREE_CONSTANT (fdesc) = 1;
1442 TREE_INVARIANT (fdesc) = 1;
1443 list = tree_cons (NULL_TREE, fdesc, list);
1445 else
1446 list = tree_cons (NULL_TREE,
1447 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1448 method),
1449 list);
1453 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1454 using the Boehm GC we sometimes stash a GC type descriptor
1455 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1456 the emitted byte count during the output to the assembly file. */
1457 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1458 fake "function descriptor". It's first word is the is the class
1459 pointer, and subsequent words (usually one) contain the GC descriptor.
1460 In all other cases, we reserve two extra vtable slots. */
1461 gc_descr = get_boehm_type_descriptor (type);
1462 list = tree_cons (NULL_TREE, gc_descr, list);
1463 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1464 list = tree_cons (NULL_TREE, gc_descr, list);
1465 list = tree_cons (NULL_TREE, this_class_addr, list);
1467 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1468 list = tree_cons (NULL_TREE, null_pointer_node, list);
1469 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1470 list = tree_cons (integer_zero_node, null_pointer_node, list);
1472 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1473 if (TARGET_VTABLE_USES_DESCRIPTORS)
1474 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1475 arraysize += 2;
1476 return build_constructor (build_prim_array_type (nativecode_ptr_type_node,
1477 arraysize), list);
1481 /* Set the method_index for a method decl. */
1482 void
1483 set_method_index (tree decl, tree method_index)
1485 if (method_index != NULL_TREE)
1487 /* method_index is null if we're using indirect dispatch. */
1488 method_index = fold (convert (sizetype, method_index));
1490 if (TARGET_VTABLE_USES_DESCRIPTORS)
1491 /* Add one to skip bogus descriptor for class and GC descriptor. */
1492 method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1493 else
1494 /* Add 1 to skip "class" field of dtable, and 1 to skip GC
1495 descriptor. */
1496 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1499 DECL_VINDEX (decl) = method_index;
1502 /* Get the method_index for a method decl. */
1503 tree
1504 get_method_index (tree decl)
1506 tree method_index = DECL_VINDEX (decl);
1508 if (! method_index)
1509 return NULL;
1511 if (TARGET_VTABLE_USES_DESCRIPTORS)
1512 /* Sub one to skip bogus descriptor for class and GC descriptor. */
1513 method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1514 else
1515 /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1516 method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1518 return method_index;
1521 static int
1522 supers_all_compiled (tree type)
1524 while (type != NULL_TREE)
1526 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1527 return 0;
1528 type = CLASSTYPE_SUPER (type);
1530 return 1;
1533 void
1534 make_class_data (tree type)
1536 tree decl, cons, temp;
1537 tree field, fields_decl;
1538 tree static_fields = NULL_TREE;
1539 tree instance_fields = NULL_TREE;
1540 HOST_WIDE_INT static_field_count = 0;
1541 HOST_WIDE_INT instance_field_count = 0;
1542 HOST_WIDE_INT field_count;
1543 tree field_array_type;
1544 tree method;
1545 tree methods = NULL_TREE;
1546 tree dtable_decl = NULL_TREE;
1547 HOST_WIDE_INT method_count = 0;
1548 tree method_array_type;
1549 tree methods_decl;
1550 tree super;
1551 tree this_class_addr;
1552 tree constant_pool_constructor;
1553 tree interfaces = null_pointer_node;
1554 int interface_len = 0;
1555 tree type_decl = TYPE_NAME (type);
1556 /** Offset from start of virtual function table declaration
1557 to where objects actually point at, following new g++ ABI. */
1558 tree dtable_start_offset = build_int_cst (NULL_TREE,
1559 2 * POINTER_SIZE / BITS_PER_UNIT);
1561 this_class_addr = build_class_ref (type);
1562 decl = TREE_OPERAND (this_class_addr, 0);
1564 /* Build Field array. */
1565 field = TYPE_FIELDS (type);
1566 while (field && DECL_ARTIFICIAL (field))
1567 field = TREE_CHAIN (field); /* Skip dummy fields. */
1568 if (field && DECL_NAME (field) == NULL_TREE)
1569 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1570 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1572 if (! DECL_ARTIFICIAL (field))
1574 tree init = make_field_value (field);
1575 if (FIELD_STATIC (field))
1577 tree initial = DECL_INITIAL (field);
1578 static_field_count++;
1579 static_fields = tree_cons (NULL_TREE, init, static_fields);
1580 /* If the initial value is a string constant,
1581 prevent output_constant from trying to assemble the value. */
1582 if (initial != NULL_TREE
1583 && TREE_TYPE (initial) == string_ptr_type_node)
1584 DECL_INITIAL (field) = NULL_TREE;
1585 rest_of_decl_compilation (field, 1, 1);
1586 DECL_INITIAL (field) = initial;
1588 else
1590 instance_field_count++;
1591 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1595 field_count = static_field_count + instance_field_count;
1596 if (field_count > 0)
1598 static_fields = nreverse (static_fields);
1599 instance_fields = nreverse (instance_fields);
1600 static_fields = chainon (static_fields, instance_fields);
1601 field_array_type = build_prim_array_type (field_type_node, field_count);
1602 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1603 field_array_type);
1604 DECL_INITIAL (fields_decl) = build_constructor (field_array_type,
1605 static_fields);
1606 TREE_STATIC (fields_decl) = 1;
1607 DECL_ARTIFICIAL (fields_decl) = 1;
1608 DECL_IGNORED_P (fields_decl) = 1;
1609 rest_of_decl_compilation (fields_decl, 1, 0);
1611 else
1612 fields_decl = NULL_TREE;
1614 /* Build Method array. */
1615 for (method = TYPE_METHODS (type);
1616 method != NULL_TREE; method = TREE_CHAIN (method))
1618 tree init;
1619 if (METHOD_PRIVATE (method)
1620 && ! flag_keep_inline_functions
1621 && optimize)
1622 continue;
1623 /* Even if we have a decl, we don't necessarily have the code.
1624 This can happen if we inherit a method from a superclass for
1625 which we don't have a .class file. */
1626 if (METHOD_DUMMY (method))
1627 continue;
1628 init = make_method_value (method);
1629 method_count++;
1630 methods = tree_cons (NULL_TREE, init, methods);
1632 method_array_type = build_prim_array_type (method_type_node, method_count);
1633 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1634 method_array_type);
1635 DECL_INITIAL (methods_decl) = build_constructor (method_array_type,
1636 nreverse (methods));
1637 TREE_STATIC (methods_decl) = 1;
1638 DECL_ARTIFICIAL (methods_decl) = 1;
1639 DECL_IGNORED_P (methods_decl) = 1;
1640 rest_of_decl_compilation (methods_decl, 1, 0);
1642 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1643 && !flag_indirect_dispatch)
1645 tree dtable = get_dispatch_table (type, this_class_addr);
1646 dtable_decl = build_dtable_decl (type);
1647 DECL_INITIAL (dtable_decl) = dtable;
1648 TREE_STATIC (dtable_decl) = 1;
1649 DECL_ARTIFICIAL (dtable_decl) = 1;
1650 DECL_IGNORED_P (dtable_decl) = 1;
1651 TREE_PUBLIC (dtable_decl) = 1;
1652 rest_of_decl_compilation (dtable_decl, 1, 0);
1653 if (type == class_type_node)
1654 class_dtable_decl = dtable_decl;
1657 if (class_dtable_decl == NULL_TREE)
1659 class_dtable_decl = build_dtable_decl (class_type_node);
1660 TREE_STATIC (class_dtable_decl) = 1;
1661 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1662 DECL_IGNORED_P (class_dtable_decl) = 1;
1663 if (is_compiled_class (class_type_node) != 2)
1664 DECL_EXTERNAL (class_dtable_decl) = 1;
1665 rest_of_decl_compilation (class_dtable_decl, 1, 0);
1668 super = CLASSTYPE_SUPER (type);
1669 if (super == NULL_TREE)
1670 super = null_pointer_node;
1671 else if (! flag_indirect_dispatch
1672 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1673 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1674 super = build_class_ref (super);
1675 else
1677 int super_index = alloc_class_constant (super);
1678 super = build_int_cst (ptr_type_node, super_index);
1681 /* Build and emit the array of implemented interfaces. */
1682 if (type != object_type_node)
1683 interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
1685 if (interface_len > 0)
1687 tree init = NULL_TREE;
1688 int i;
1689 tree interface_array_type, idecl;
1690 interface_array_type
1691 = build_prim_array_type (class_ptr_type, interface_len);
1692 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1693 interface_array_type);
1695 for (i = interface_len; i > 0; i--)
1697 tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1698 tree iclass = BINFO_TYPE (child);
1699 tree index;
1700 if (! flag_indirect_dispatch
1701 && (assume_compiled
1702 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1703 index = build_class_ref (iclass);
1704 else
1706 int int_index = alloc_class_constant (iclass);
1707 index = build_int_cst (ptr_type_node, int_index);
1709 init = tree_cons (NULL_TREE, index, init);
1711 DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
1712 TREE_STATIC (idecl) = 1;
1713 DECL_ARTIFICIAL (idecl) = 1;
1714 DECL_IGNORED_P (idecl) = 1;
1715 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1716 rest_of_decl_compilation (idecl, 1, 0);
1719 constant_pool_constructor = build_constants_constructor ();
1721 if (flag_indirect_dispatch)
1723 TYPE_OTABLE_DECL (type)
1724 = emit_symbol_table
1725 (DECL_NAME (TYPE_OTABLE_DECL (type)),
1726 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
1727 TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
1729 TYPE_ATABLE_DECL (type)
1730 = emit_symbol_table
1731 (DECL_NAME (TYPE_ATABLE_DECL (type)),
1732 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
1733 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
1735 TYPE_ITABLE_DECL (type)
1736 = emit_symbol_table
1737 (DECL_NAME (TYPE_ITABLE_DECL (type)),
1738 TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
1739 TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
1742 TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1744 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1745 PUSH_FIELD_VALUE (temp, "vtable",
1746 build2 (PLUS_EXPR, dtable_ptr_type,
1747 build1 (ADDR_EXPR, dtable_ptr_type,
1748 class_dtable_decl),
1749 dtable_start_offset));
1750 if (! flag_hash_synchronization)
1751 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1752 FINISH_RECORD_CONSTRUCTOR (temp);
1753 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1754 PUSH_SUPER_VALUE (cons, temp);
1755 PUSH_FIELD_VALUE (cons, "next_or_version", gcj_abi_version);
1756 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1757 PUSH_FIELD_VALUE (cons, "accflags",
1758 build_int_cst (NULL_TREE,
1759 get_access_flags_from_decl (type_decl)));
1761 PUSH_FIELD_VALUE (cons, "superclass",
1762 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1763 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1764 PUSH_FIELD_VALUE (cons, "methods",
1765 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1766 PUSH_FIELD_VALUE (cons, "method_count",
1767 build_int_cst (NULL_TREE, method_count));
1769 if (flag_indirect_dispatch)
1770 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1771 else
1772 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1774 PUSH_FIELD_VALUE (cons, "fields",
1775 fields_decl == NULL_TREE ? null_pointer_node
1776 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1777 /* If we're using the binary compatibility ABI we don't know the
1778 size until load time. */
1779 PUSH_FIELD_VALUE (cons, "size_in_bytes",
1780 (flag_indirect_dispatch
1781 ? integer_minus_one_node
1782 : size_in_bytes (type)));
1783 PUSH_FIELD_VALUE (cons, "field_count",
1784 build_int_cst (NULL_TREE, field_count));
1785 PUSH_FIELD_VALUE (cons, "static_field_count",
1786 build_int_cst (NULL_TREE, static_field_count));
1788 if (flag_indirect_dispatch)
1789 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1790 else
1791 PUSH_FIELD_VALUE (cons, "vtable",
1792 dtable_decl == NULL_TREE ? null_pointer_node
1793 : build2 (PLUS_EXPR, dtable_ptr_type,
1794 build1 (ADDR_EXPR, dtable_ptr_type,
1795 dtable_decl),
1796 dtable_start_offset));
1797 if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1799 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1800 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1802 else
1804 PUSH_FIELD_VALUE (cons, "otable",
1805 build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1806 PUSH_FIELD_VALUE (cons, "otable_syms",
1807 build1 (ADDR_EXPR, symbols_array_ptr_type,
1808 TYPE_OTABLE_SYMS_DECL (type)));
1809 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1810 TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1812 if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1814 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1815 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1817 else
1819 PUSH_FIELD_VALUE (cons, "atable",
1820 build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1821 PUSH_FIELD_VALUE (cons, "atable_syms",
1822 build1 (ADDR_EXPR, symbols_array_ptr_type,
1823 TYPE_ATABLE_SYMS_DECL (type)));
1824 TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1825 TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1827 if (TYPE_ITABLE_METHODS(type) == NULL_TREE)
1829 PUSH_FIELD_VALUE (cons, "itable", null_pointer_node);
1830 PUSH_FIELD_VALUE (cons, "itable_syms", null_pointer_node);
1832 else
1834 PUSH_FIELD_VALUE (cons, "itable",
1835 build1 (ADDR_EXPR, itable_ptr_type, TYPE_ITABLE_DECL (type)));
1836 PUSH_FIELD_VALUE (cons, "itable_syms",
1837 build1 (ADDR_EXPR, symbols_array_ptr_type,
1838 TYPE_ITABLE_SYMS_DECL (type)));
1839 TREE_CONSTANT (TYPE_ITABLE_DECL (type)) = 1;
1840 TREE_INVARIANT (TYPE_ITABLE_DECL (type)) = 1;
1843 PUSH_FIELD_VALUE (cons, "catch_classes",
1844 build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
1845 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1846 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1847 PUSH_FIELD_VALUE (cons, "interface_count",
1848 build_int_cst (NULL_TREE, interface_len));
1849 PUSH_FIELD_VALUE
1850 (cons, "state",
1851 convert (byte_type_node,
1852 build_int_cst (NULL_TREE,
1853 flag_indirect_dispatch
1854 ? JV_STATE_PRELOADING
1855 : JV_STATE_COMPILED)));
1857 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1858 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1859 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1860 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1861 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1862 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1865 tree assertion_table_ref;
1866 if (TYPE_ASSERTIONS (type) == NULL)
1867 assertion_table_ref = null_pointer_node;
1868 else
1869 assertion_table_ref = build1 (ADDR_EXPR,
1870 build_pointer_type (assertion_table_type),
1871 emit_assertion_table (type));
1873 PUSH_FIELD_VALUE (cons, "assertion_table", assertion_table_ref);
1876 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1877 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1878 PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1879 PUSH_FIELD_VALUE (cons, "engine", null_pointer_node);
1881 FINISH_RECORD_CONSTRUCTOR (cons);
1883 DECL_INITIAL (decl) = cons;
1885 /* Hash synchronization requires at least 64-bit alignment. */
1886 if (flag_hash_synchronization && POINTER_SIZE < 64)
1887 DECL_ALIGN (decl) = 64;
1889 rest_of_decl_compilation (decl, 1, 0);
1891 TYPE_OTABLE_DECL (type) = NULL_TREE;
1892 TYPE_ATABLE_DECL (type) = NULL_TREE;
1893 TYPE_CTABLE_DECL (type) = NULL_TREE;
1896 void
1897 finish_class (void)
1899 if (TYPE_VERIFY_METHOD (output_class))
1901 tree verify_method = TYPE_VERIFY_METHOD (output_class);
1902 DECL_SAVED_TREE (verify_method)
1903 = add_stmt_to_compound (DECL_SAVED_TREE (verify_method), void_type_node,
1904 build (RETURN_EXPR, void_type_node, NULL));
1905 java_genericize (verify_method);
1906 cgraph_finalize_function (verify_method, false);
1907 TYPE_ASSERTIONS (current_class) = NULL;
1910 java_expand_catch_classes (current_class);
1912 current_function_decl = NULL_TREE;
1913 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (current_class)) = 0;
1914 make_class_data (current_class);
1915 register_class ();
1916 rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
1919 /* Return 2 if CLASS is compiled by this compilation job;
1920 return 1 if CLASS can otherwise be assumed to be compiled;
1921 return 0 if we cannot assume that CLASS is compiled.
1922 Returns 1 for primitive and 0 for array types. */
1924 is_compiled_class (tree class)
1926 int seen_in_zip;
1927 if (TREE_CODE (class) == POINTER_TYPE)
1928 class = TREE_TYPE (class);
1929 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1930 return 1;
1931 if (TYPE_ARRAY_P (class))
1932 return 0;
1933 if (class == current_class)
1934 return 2;
1936 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1937 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1939 /* The class was seen in the current ZIP file and will be
1940 available as a compiled class in the future but may not have
1941 been loaded already. Load it if necessary. This prevent
1942 build_class_ref () from crashing. */
1944 if (seen_in_zip && !CLASS_LOADED_P (class))
1945 load_class (class, 1);
1947 /* We return 2 for class seen in ZIP and class from files
1948 belonging to the same compilation unit */
1949 return 2;
1952 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1954 if (!CLASS_LOADED_P (class))
1956 if (CLASS_FROM_SOURCE_P (class))
1957 safe_layout_class (class);
1958 else
1959 load_class (class, 1);
1961 return 1;
1964 return 0;
1967 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1969 tree
1970 build_dtable_decl (tree type)
1972 tree dtype;
1974 /* We need to build a new dtable type so that its size is uniquely
1975 computed when we're dealing with the class for real and not just
1976 faking it (like java.lang.Class during the initialization of the
1977 compiler.) We know we're not faking a class when CURRENT_CLASS is
1978 TYPE. */
1979 if (current_class == type)
1981 tree dummy = NULL_TREE;
1982 int n;
1984 dtype = make_node (RECORD_TYPE);
1986 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1987 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1989 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1990 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1992 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1993 TREE_CHAIN (dummy) = tmp_field;
1994 DECL_CONTEXT (tmp_field) = dtype;
1995 DECL_ARTIFICIAL (tmp_field) = 1;
1996 dummy = tmp_field;
1999 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
2000 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2002 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2003 TREE_CHAIN (dummy) = tmp_field;
2004 DECL_CONTEXT (tmp_field) = dtype;
2005 DECL_ARTIFICIAL (tmp_field) = 1;
2006 dummy = tmp_field;
2009 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
2010 if (TARGET_VTABLE_USES_DESCRIPTORS)
2011 n *= TARGET_VTABLE_USES_DESCRIPTORS;
2013 PUSH_FIELD (dtype, dummy, "methods",
2014 build_prim_array_type (nativecode_ptr_type_node, n));
2015 layout_type (dtype);
2017 else
2018 dtype = dtable_type;
2020 return build_decl (VAR_DECL,
2021 java_mangle_vtable (&temporary_obstack, type), dtype);
2024 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2025 fields inherited from SUPER_CLASS. */
2027 void
2028 push_super_field (tree this_class, tree super_class)
2030 tree base_decl;
2031 /* Don't insert the field if we're just re-laying the class out. */
2032 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2033 return;
2034 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
2035 DECL_IGNORED_P (base_decl) = 1;
2036 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2037 TYPE_FIELDS (this_class) = base_decl;
2038 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2039 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2042 /* Handle the different manners we may have to lay out a super class. */
2044 static tree
2045 maybe_layout_super_class (tree super_class, tree this_class)
2047 if (TREE_CODE (super_class) == RECORD_TYPE)
2049 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
2050 safe_layout_class (super_class);
2051 if (!CLASS_LOADED_P (super_class))
2052 load_class (super_class, 1);
2054 /* We might have to layout the class before its dependency on
2055 the super class gets resolved by java_complete_class */
2056 else if (TREE_CODE (super_class) == POINTER_TYPE)
2058 if (TREE_TYPE (super_class) != NULL_TREE)
2059 super_class = TREE_TYPE (super_class);
2060 else
2062 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
2063 we give it one. */
2064 tree this_wrap = NULL_TREE;
2066 if (this_class)
2068 tree this_decl = TYPE_NAME (this_class);
2069 #ifdef USE_MAPPED_LOCATION
2070 this_wrap = build_expr_wfl (this_class,
2071 DECL_SOURCE_LOCATION (this_decl));
2072 #else
2073 this_wrap = build_expr_wfl (this_class,
2074 DECL_SOURCE_FILE (this_decl),
2075 DECL_SOURCE_LINE (this_decl), 0);
2076 #endif
2078 super_class = do_resolve_class (NULL_TREE, this_class,
2079 super_class, NULL_TREE, this_wrap);
2080 if (!super_class)
2081 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
2082 super_class = TREE_TYPE (super_class);
2085 if (!TYPE_SIZE (super_class))
2086 safe_layout_class (super_class);
2088 return super_class;
2091 void
2092 layout_class (tree this_class)
2094 tree super_class = CLASSTYPE_SUPER (this_class);
2095 tree field;
2097 class_list = tree_cons (this_class, NULL_TREE, class_list);
2098 if (CLASS_BEING_LAIDOUT (this_class))
2100 char buffer [1024];
2101 char *report;
2102 tree current;
2104 sprintf (buffer, " with '%s'",
2105 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2106 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2108 for (current = TREE_CHAIN (class_list); current;
2109 current = TREE_CHAIN (current))
2111 tree decl = TYPE_NAME (TREE_PURPOSE (current));
2112 sprintf (buffer, "\n which inherits from '%s' (%s:%d)",
2113 IDENTIFIER_POINTER (DECL_NAME (decl)),
2114 DECL_SOURCE_FILE (decl),
2115 DECL_SOURCE_LINE (decl));
2116 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2118 obstack_1grow (&temporary_obstack, '\0');
2119 report = obstack_finish (&temporary_obstack);
2120 cyclic_inheritance_report = ggc_strdup (report);
2121 obstack_free (&temporary_obstack, report);
2122 TYPE_SIZE (this_class) = error_mark_node;
2123 return;
2125 CLASS_BEING_LAIDOUT (this_class) = 1;
2127 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2129 tree maybe_super_class
2130 = maybe_layout_super_class (super_class, this_class);
2131 if (maybe_super_class == NULL
2132 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2134 TYPE_SIZE (this_class) = error_mark_node;
2135 CLASS_BEING_LAIDOUT (this_class) = 0;
2136 class_list = TREE_CHAIN (class_list);
2137 return;
2139 if (TYPE_SIZE (this_class) == NULL_TREE)
2140 push_super_field (this_class, maybe_super_class);
2143 for (field = TYPE_FIELDS (this_class);
2144 field != NULL_TREE; field = TREE_CHAIN (field))
2146 if (FIELD_STATIC (field))
2148 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
2149 SET_DECL_ASSEMBLER_NAME (field,
2150 java_mangle_decl
2151 (&temporary_obstack, field));
2155 layout_type (this_class);
2157 /* Also recursively load/layout any superinterfaces, but only if
2158 class was loaded from bytecode. The source parser will take care
2159 of this itself. */
2160 if (!CLASS_FROM_SOURCE_P (this_class))
2162 int i;
2163 if (TYPE_BINFO (this_class))
2165 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2167 tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2168 tree super_interface = BINFO_TYPE (binfo);
2169 tree maybe_super_interface
2170 = maybe_layout_super_class (super_interface, NULL_TREE);
2171 if (maybe_super_interface == NULL
2172 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2174 TYPE_SIZE (this_class) = error_mark_node;
2175 CLASS_BEING_LAIDOUT (this_class) = 0;
2176 class_list = TREE_CHAIN (class_list);
2177 return;
2183 /* Convert the size back to an SI integer value. */
2184 TYPE_SIZE_UNIT (this_class) =
2185 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2187 CLASS_BEING_LAIDOUT (this_class) = 0;
2188 class_list = TREE_CHAIN (class_list);
2191 static void
2192 add_miranda_methods (tree base_class, tree search_class)
2194 int i;
2195 tree binfo, base_binfo;
2197 if (!CLASS_PARSED_P (search_class))
2198 load_class (search_class, 1);
2200 for (binfo = TYPE_BINFO (search_class), i = 1;
2201 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2203 tree method_decl;
2204 tree elt = BINFO_TYPE (base_binfo);
2206 /* FIXME: This is totally bogus. We should not be handling
2207 Miranda methods at all if we're using the BC ABI. */
2208 if (TYPE_DUMMY (elt))
2209 continue;
2211 /* Ensure that interface methods are seen in declared order. */
2212 if (!CLASS_LOADED_P (elt))
2213 load_class (elt, 1);
2214 layout_class_methods (elt);
2216 /* All base classes will have been laid out at this point, so the order
2217 will be correct. This code must match similar layout code in the
2218 runtime. */
2219 for (method_decl = TYPE_METHODS (elt);
2220 method_decl; method_decl = TREE_CHAIN (method_decl))
2222 tree sig, override;
2224 /* An interface can have <clinit>. */
2225 if (ID_CLINIT_P (DECL_NAME (method_decl)))
2226 continue;
2228 sig = build_java_argument_signature (TREE_TYPE (method_decl));
2229 override = lookup_argument_method (base_class,
2230 DECL_NAME (method_decl), sig);
2231 if (override == NULL_TREE)
2233 /* Found a Miranda method. Add it. */
2234 tree new_method;
2235 sig = build_java_signature (TREE_TYPE (method_decl));
2236 new_method
2237 = add_method (base_class,
2238 get_access_flags_from_decl (method_decl),
2239 DECL_NAME (method_decl), sig);
2240 METHOD_INVISIBLE (new_method) = 1;
2244 /* Try superinterfaces. */
2245 add_miranda_methods (base_class, elt);
2249 void
2250 layout_class_methods (tree this_class)
2252 tree method_decl, dtable_count;
2253 tree super_class, type_name;
2255 if (TYPE_NVIRTUALS (this_class))
2256 return;
2258 super_class = CLASSTYPE_SUPER (this_class);
2260 if (super_class)
2262 super_class = maybe_layout_super_class (super_class, this_class);
2263 if (!TYPE_NVIRTUALS (super_class))
2264 layout_class_methods (super_class);
2265 dtable_count = TYPE_NVIRTUALS (super_class);
2267 else
2268 dtable_count = integer_zero_node;
2270 type_name = TYPE_NAME (this_class);
2271 if (!flag_indirect_dispatch
2272 && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2274 /* An abstract class can have methods which are declared only in
2275 an implemented interface. These are called "Miranda
2276 methods". We make a dummy method entry for such methods
2277 here. */
2278 add_miranda_methods (this_class, this_class);
2281 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2283 for (method_decl = TYPE_METHODS (this_class);
2284 method_decl; method_decl = TREE_CHAIN (method_decl))
2285 dtable_count = layout_class_method (this_class, super_class,
2286 method_decl, dtable_count);
2288 TYPE_NVIRTUALS (this_class) = dtable_count;
2291 /* Return the index of METHOD in INTERFACE. This index begins at 1 and is used as an
2292 argument for _Jv_LookupInterfaceMethodIdx(). */
2294 get_interface_method_index (tree method, tree interface)
2296 tree meth;
2297 int i = 1;
2299 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
2301 if (meth == method)
2302 return i;
2303 if (meth == NULL_TREE)
2304 abort ();
2308 /* Lay METHOD_DECL out, returning a possibly new value of
2309 DTABLE_COUNT. Also mangle the method's name. */
2311 tree
2312 layout_class_method (tree this_class, tree super_class,
2313 tree method_decl, tree dtable_count)
2315 tree method_name = DECL_NAME (method_decl);
2317 TREE_PUBLIC (method_decl) = 1;
2318 /* Considered external until we know what classes are being
2319 compiled into this object file. */
2320 DECL_EXTERNAL (method_decl) = 1;
2322 /* This is a good occasion to mangle the method's name */
2323 SET_DECL_ASSEMBLER_NAME (method_decl,
2324 java_mangle_decl (&temporary_obstack,
2325 method_decl));
2327 if (ID_INIT_P (method_name))
2329 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2330 const char *ptr;
2331 for (ptr = p; *ptr; )
2333 if (*ptr++ == '.')
2334 p = ptr;
2336 DECL_CONSTRUCTOR_P (method_decl) = 1;
2337 build_java_argument_signature (TREE_TYPE (method_decl));
2339 else if (! METHOD_STATIC (method_decl))
2341 tree method_sig =
2342 build_java_argument_signature (TREE_TYPE (method_decl));
2343 bool method_override = false;
2344 tree super_method = lookup_argument_method (super_class, method_name,
2345 method_sig);
2346 if (super_method != NULL_TREE
2347 && ! METHOD_DUMMY (super_method))
2349 method_override = true;
2350 if (! METHOD_PUBLIC (super_method) &&
2351 ! METHOD_PROTECTED (super_method))
2353 /* Don't override private method, or default-access method in
2354 another package. */
2355 if (METHOD_PRIVATE (super_method) ||
2356 ! in_same_package (TYPE_NAME (this_class),
2357 TYPE_NAME (super_class)))
2358 method_override = false;
2361 if (method_override)
2363 tree method_index = get_method_index (super_method);
2364 set_method_index (method_decl, method_index);
2365 if (method_index == NULL_TREE
2366 && ! flag_indirect_dispatch
2367 && !CLASS_FROM_SOURCE_P (this_class)
2368 && ! DECL_ARTIFICIAL (super_method))
2369 error ("%Jnon-static method '%D' overrides static method",
2370 method_decl, method_decl);
2372 else if (this_class == object_type_node
2373 && (METHOD_FINAL (method_decl)
2374 || METHOD_PRIVATE (method_decl)))
2376 /* We don't generate vtable entries for final Object
2377 methods. This is simply to save space, since every
2378 object would otherwise have to define them. */
2380 else if (! METHOD_PRIVATE (method_decl)
2381 && dtable_count)
2383 /* We generate vtable entries for final methods because they
2384 may one day be changed to non-final. */
2385 set_method_index (method_decl, dtable_count);
2386 dtable_count = fold (build2 (PLUS_EXPR, integer_type_node,
2387 dtable_count, integer_one_node));
2391 return dtable_count;
2394 static void
2395 register_class (void)
2397 tree node;
2399 if (!registered_class)
2400 registered_class = VEC_alloc (tree, gc, 8);
2402 node = TREE_OPERAND (build_class_ref (current_class), 0);
2403 VEC_safe_push (tree, gc, registered_class, node);
2406 /* Emit something to register classes at start-up time.
2408 The preferred mechanism is through the .jcr section, which contain
2409 a list of pointers to classes which get registered during constructor
2410 invocation time.
2412 The fallback mechanism is to add statements to *LIST_P to call
2413 _Jv_RegisterClass for each class in this file. These statements will
2414 be added to a static constructor function for this translation unit. */
2416 void
2417 emit_register_classes (tree *list_p)
2419 if (registered_class == NULL)
2420 return;
2422 /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2423 TARGET_ASM_NAMED_SECTION, else 0. Some targets meet those conditions
2424 but lack suitable crtbegin/end objects or linker support. These
2425 targets can overide the default in tm.h to use the fallback mechanism. */
2426 if (TARGET_USE_JCR_SECTION)
2428 tree klass, t;
2429 int i;
2431 #ifdef JCR_SECTION_NAME
2432 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2433 #else
2434 /* A target has defined TARGET_USE_JCR_SECTION,
2435 but doesn't have a JCR_SECTION_NAME. */
2436 gcc_unreachable ();
2437 #endif
2438 assemble_align (POINTER_SIZE);
2440 for (i = 0; VEC_iterate (tree, registered_class, i, klass); ++i)
2442 t = build_fold_addr_expr (klass);
2443 output_constant (t, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE);
2446 else
2448 tree klass, t, register_class_fn;
2449 int i;
2451 t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2452 t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2453 TREE_PUBLIC (t) = 1;
2454 DECL_EXTERNAL (t) = 1;
2455 register_class_fn = t;
2457 for (i = 0; VEC_iterate (tree, registered_class, i, klass); ++i)
2459 t = build_fold_addr_expr (klass);
2460 t = tree_cons (NULL, t, NULL);
2461 t = build_function_call_expr (register_class_fn, t);
2462 append_to_statement_list (t, list_p);
2467 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2469 static tree
2470 build_symbol_entry (tree decl)
2472 tree clname, name, signature, sym;
2473 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2474 /* ??? Constructors are given the name foo.foo all the way through
2475 the compiler, but in the method table they're all renamed
2476 foo.<init>. So, we have to do the same here unless we want an
2477 unresolved reference at runtime. */
2478 name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
2479 && DECL_CONSTRUCTOR_P (decl))
2480 ? init_identifier_node
2481 : DECL_NAME (decl));
2482 signature = build_java_signature (TREE_TYPE (decl));
2483 signature = build_utf8_ref (unmangle_classname
2484 (IDENTIFIER_POINTER (signature),
2485 IDENTIFIER_LENGTH (signature)));
2487 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2488 PUSH_FIELD_VALUE (sym, "clname", clname);
2489 PUSH_FIELD_VALUE (sym, "name", name);
2490 PUSH_FIELD_VALUE (sym, "signature", signature);
2491 FINISH_RECORD_CONSTRUCTOR (sym);
2492 TREE_CONSTANT (sym) = 1;
2493 TREE_INVARIANT (sym) = 1;
2495 return sym;
2498 /* Emit a symbol table: used by -findirect-dispatch. */
2500 tree
2501 emit_symbol_table (tree name, tree the_table, tree decl_list,
2502 tree the_syms_decl, tree the_array_element_type,
2503 int element_size)
2505 tree method_list, method, table, list, null_symbol;
2506 tree table_size, the_array_type;
2507 int index;
2509 /* Only emit a table if this translation unit actually made any
2510 references via it. */
2511 if (decl_list == NULL_TREE)
2512 return the_table;
2514 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2515 index = 0;
2516 method_list = decl_list;
2517 list = NULL_TREE;
2518 while (method_list != NULL_TREE)
2520 method = TREE_VALUE (method_list);
2521 list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2522 method_list = TREE_CHAIN (method_list);
2523 index++;
2526 /* Terminate the list with a "null" entry. */
2527 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2528 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2529 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2530 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2531 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2532 TREE_CONSTANT (null_symbol) = 1;
2533 TREE_INVARIANT (null_symbol) = 1;
2534 list = tree_cons (NULL_TREE, null_symbol, list);
2536 /* Put the list in the right order and make it a constructor. */
2537 list = nreverse (list);
2538 table = build_constructor (symbols_array_type, list);
2540 /* Make it the initial value for otable_syms and emit the decl. */
2541 DECL_INITIAL (the_syms_decl) = table;
2542 DECL_ARTIFICIAL (the_syms_decl) = 1;
2543 DECL_IGNORED_P (the_syms_decl) = 1;
2544 rest_of_decl_compilation (the_syms_decl, 1, 0);
2546 /* Now that its size is known, redefine the table as an
2547 uninitialized static array of INDEX + 1 elements. The extra entry
2548 is used by the runtime to track whether the table has been
2549 initialized. */
2550 table_size
2551 = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2552 the_array_type = build_array_type (the_array_element_type, table_size);
2553 the_table = build_decl (VAR_DECL, name, the_array_type);
2554 TREE_STATIC (the_table) = 1;
2555 TREE_READONLY (the_table) = 1;
2556 rest_of_decl_compilation (the_table, 1, 0);
2558 return the_table;
2561 /* Make an entry for the catch_classes list. */
2562 tree
2563 make_catch_class_record (tree catch_class, tree classname)
2565 tree entry;
2566 tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2567 START_RECORD_CONSTRUCTOR (entry, type);
2568 PUSH_FIELD_VALUE (entry, "address", catch_class);
2569 PUSH_FIELD_VALUE (entry, "classname", classname);
2570 FINISH_RECORD_CONSTRUCTOR (entry);
2571 return entry;
2575 /* Generate the list of Throwable classes that are caught by exception
2576 handlers in this class. */
2577 tree
2578 emit_catch_table (tree this_class)
2580 tree table, table_size, array_type;
2581 TYPE_CATCH_CLASSES (this_class) =
2582 tree_cons (NULL,
2583 make_catch_class_record (null_pointer_node, null_pointer_node),
2584 TYPE_CATCH_CLASSES (this_class));
2585 TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2586 TYPE_CATCH_CLASSES (this_class) =
2587 tree_cons (NULL,
2588 make_catch_class_record (null_pointer_node, null_pointer_node),
2589 TYPE_CATCH_CLASSES (this_class));
2590 table_size = build_index_type
2591 (build_int_cst (NULL_TREE,
2592 list_length (TYPE_CATCH_CLASSES (this_class))));
2593 array_type
2594 = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2595 table_size);
2596 table =
2597 build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2598 DECL_INITIAL (table) =
2599 build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
2600 TREE_STATIC (table) = 1;
2601 TREE_READONLY (table) = 1;
2602 DECL_IGNORED_P (table) = 1;
2603 rest_of_decl_compilation (table, 1, 0);
2604 return table;
2607 /* Given a type, return the signature used by
2608 _Jv_FindClassFromSignature() in libgcj. This isn't exactly the
2609 same as build_java_signature() because we want the canonical array
2610 type. */
2612 static tree
2613 build_signature_for_libgcj (tree type)
2615 tree sig, ref;
2617 sig = build_java_signature (type);
2618 ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
2619 IDENTIFIER_LENGTH (sig)));
2620 return ref;
2623 /* Add an entry to the type assertion table. Callback used during hashtable
2624 traversal. */
2626 static int
2627 add_assertion_table_entry (void **htab_entry, void *ptr)
2629 tree entry;
2630 tree code_val, op1_utf8, op2_utf8;
2631 tree *list = (tree *) ptr;
2632 type_assertion *as = (type_assertion *) *htab_entry;
2634 code_val = build_int_cst (NULL_TREE, as->assertion_code);
2636 if (as->op1 == NULL_TREE)
2637 op1_utf8 = null_pointer_node;
2638 else
2639 op1_utf8 = build_signature_for_libgcj (as->op1);
2641 if (as->op2 == NULL_TREE)
2642 op2_utf8 = null_pointer_node;
2643 else
2644 op2_utf8 = build_signature_for_libgcj (as->op2);
2646 START_RECORD_CONSTRUCTOR (entry, assertion_entry_type);
2647 PUSH_FIELD_VALUE (entry, "assertion_code", code_val);
2648 PUSH_FIELD_VALUE (entry, "op1", op1_utf8);
2649 PUSH_FIELD_VALUE (entry, "op2", op2_utf8);
2650 FINISH_RECORD_CONSTRUCTOR (entry);
2652 *list = tree_cons (NULL_TREE, entry, *list);
2653 return true;
2656 /* Generate the type assertion table for CLASS, and return its DECL. */
2658 static tree
2659 emit_assertion_table (tree class)
2661 tree null_entry, ctor, table_decl;
2662 tree list = NULL_TREE;
2663 htab_t assertions_htab = TYPE_ASSERTIONS (class);
2665 /* Iterate through the hash table. */
2666 htab_traverse (assertions_htab, add_assertion_table_entry, &list);
2668 /* Finish with a null entry. */
2669 START_RECORD_CONSTRUCTOR (null_entry, assertion_entry_type);
2670 PUSH_FIELD_VALUE (null_entry, "assertion_code", integer_zero_node);
2671 PUSH_FIELD_VALUE (null_entry, "op1", null_pointer_node);
2672 PUSH_FIELD_VALUE (null_entry, "op2", null_pointer_node);
2673 FINISH_RECORD_CONSTRUCTOR (null_entry);
2675 list = tree_cons (NULL_TREE, null_entry, list);
2677 /* Put the list in the right order and make it a constructor. */
2678 list = nreverse (list);
2679 ctor = build_constructor (assertion_table_type, list);
2681 table_decl = build_decl (VAR_DECL, mangled_classname ("_type_assert_", class),
2682 assertion_table_type);
2684 TREE_STATIC (table_decl) = 1;
2685 TREE_READONLY (table_decl) = 1;
2686 TREE_CONSTANT (table_decl) = 1;
2687 DECL_IGNORED_P (table_decl) = 1;
2689 DECL_INITIAL (table_decl) = ctor;
2690 DECL_ARTIFICIAL (table_decl) = 1;
2691 rest_of_decl_compilation (table_decl, 1, 0);
2693 return table_decl;
2696 void
2697 init_class_processing (void)
2699 fields_ident = get_identifier ("fields");
2700 info_ident = get_identifier ("info");
2702 gcc_obstack_init (&temporary_obstack);
2705 static hashval_t java_treetreehash_hash (const void *);
2706 static int java_treetreehash_compare (const void *, const void *);
2708 /* A hash table mapping trees to trees. Used generally. */
2710 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2712 static hashval_t
2713 java_treetreehash_hash (const void *k_p)
2715 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2716 return JAVA_TREEHASHHASH_H (k->key);
2719 static int
2720 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2722 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2723 tree k2 = (tree) k2_p;
2724 return (k1->key == k2);
2727 tree
2728 java_treetreehash_find (htab_t ht, tree t)
2730 struct treetreehash_entry *e;
2731 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2732 e = htab_find_with_hash (ht, t, hv);
2733 if (e == NULL)
2734 return NULL;
2735 else
2736 return e->value;
2739 tree *
2740 java_treetreehash_new (htab_t ht, tree t)
2742 void **e;
2743 struct treetreehash_entry *tthe;
2744 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2746 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2747 if (*e == NULL)
2749 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2750 tthe->key = t;
2751 *e = tthe;
2753 else
2754 tthe = (struct treetreehash_entry *) *e;
2755 return &tthe->value;
2758 htab_t
2759 java_treetreehash_create (size_t size, int gc)
2761 if (gc)
2762 return htab_create_ggc (size, java_treetreehash_hash,
2763 java_treetreehash_compare, NULL);
2764 else
2765 return htab_create_alloc (size, java_treetreehash_hash,
2766 java_treetreehash_compare, free, xcalloc, free);
2769 /* Break down qualified IDENTIFIER into package and class-name components.
2770 For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
2771 "pkg.foo", and RIGHT to "Bar". */
2774 split_qualified_name (tree *left, tree *right, tree source)
2776 char *p, *base;
2777 int l = IDENTIFIER_LENGTH (source);
2779 base = alloca (l + 1);
2780 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
2782 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
2783 p = base + l - 1;
2784 while (*p != '.' && p != base)
2785 p--;
2787 /* We didn't find a '.'. Return an error. */
2788 if (p == base)
2789 return 1;
2791 *p = '\0';
2792 if (right)
2793 *right = get_identifier (p+1);
2794 *left = get_identifier (base);
2796 return 0;
2799 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
2800 if the classes are from the same package. */
2803 in_same_package (tree name1, tree name2)
2805 tree tmp;
2806 tree pkg1;
2807 tree pkg2;
2809 if (TREE_CODE (name1) == TYPE_DECL)
2810 name1 = DECL_NAME (name1);
2811 if (TREE_CODE (name2) == TYPE_DECL)
2812 name2 = DECL_NAME (name2);
2814 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
2815 /* One in empty package. */
2816 return 0;
2818 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
2819 /* Both in empty package. */
2820 return 1;
2822 split_qualified_name (&pkg1, &tmp, name1);
2823 split_qualified_name (&pkg2, &tmp, name2);
2825 return (pkg1 == pkg2);
2828 #include "gt-java-class.h"