* lang.opt (-freduced-reflection): New option.
[official-gcc.git] / gcc / java / class.c
blobbce16779b757610c5e417574cd6b528c677ca91c
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 = XNEW (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 = XNEW (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 /* Unfortunately we must create the binfo here, so that class
323 loading works. */
324 TYPE_BINFO (type) = make_tree_binfo (0);
325 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
327 return type;
330 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
331 and where each of the constituents is separated by '/',
332 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
334 tree
335 unmangle_classname (const char *name, int name_length)
337 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
338 /* It's not sufficient to compare to_return and get_identifier
339 (name) to determine whether to_return is qualified. There are
340 cases in signature analysis where name will be stripped of a
341 trailing ';'. */
342 name = IDENTIFIER_POINTER (to_return);
343 while (*name)
344 if (*name++ == '.')
346 QUALIFIED_P (to_return) = 1;
347 break;
350 return to_return;
353 #define GEN_TABLE(TABLE, NAME, TABLE_TYPE, TYPE) \
354 do \
356 const char *typename = IDENTIFIER_POINTER (mangled_classname ("", TYPE)); \
357 char *buf = alloca (strlen (typename) + strlen (#NAME "_syms_") + 1); \
358 tree decl; \
360 sprintf (buf, #NAME "_%s", typename); \
361 TYPE_## TABLE ##_DECL (type) = decl = \
362 build_decl (VAR_DECL, get_identifier (buf), TABLE_TYPE); \
363 DECL_EXTERNAL (decl) = 1; \
364 TREE_STATIC (decl) = 1; \
365 TREE_READONLY (decl) = 1; \
366 TREE_CONSTANT (decl) = 1; \
367 DECL_IGNORED_P (decl) = 1; \
368 /* Mark the table as belonging to this class. */ \
369 pushdecl (decl); \
370 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl); \
371 DECL_OWNER (decl) = TYPE; \
372 sprintf (buf, #NAME "_syms_%s", typename); \
373 TYPE_## TABLE ##_SYMS_DECL (TYPE) = \
374 build_decl (VAR_DECL, get_identifier (buf), symbols_array_type); \
375 TREE_STATIC (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
376 TREE_CONSTANT (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
377 DECL_IGNORED_P (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
379 while (0)
381 /* Given a class, create the DECLs for all its associated indirect
382 dispatch tables. */
383 void
384 gen_indirect_dispatch_tables (tree type)
386 const char *typename = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
388 tree field = NULL;
389 char *buf = alloca (strlen (typename) + strlen ("_catch_classes_") + 1);
390 tree catch_class_type = make_node (RECORD_TYPE);
392 sprintf (buf, "_catch_classes_%s", typename);
393 PUSH_FIELD (catch_class_type, field, "address", utf8const_ptr_type);
394 PUSH_FIELD (catch_class_type, field, "classname", ptr_type_node);
395 FINISH_RECORD (catch_class_type);
397 TYPE_CTABLE_DECL (type)
398 = build_decl (VAR_DECL, get_identifier (buf),
399 build_array_type (catch_class_type, 0));
400 DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
401 TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
402 TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
403 TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
404 DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
405 pushdecl (TYPE_CTABLE_DECL (type));
408 if (flag_indirect_dispatch)
410 GEN_TABLE (ATABLE, _atable, atable_type, type);
411 GEN_TABLE (OTABLE, _otable, otable_type, type);
412 GEN_TABLE (ITABLE, _itable, itable_type, type);
416 #undef GEN_TABLE
418 tree
419 push_class (tree class_type, tree class_name)
421 tree decl, signature;
422 location_t saved_loc = input_location;
423 #ifndef USE_MAPPED_LOCATION
424 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
425 input_filename = IDENTIFIER_POINTER (source_name);
426 input_line = 0;
427 #endif
428 CLASS_P (class_type) = 1;
429 decl = build_decl (TYPE_DECL, class_name, class_type);
430 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
432 /* dbxout needs a DECL_SIZE if in gstabs mode */
433 DECL_SIZE (decl) = integer_zero_node;
435 input_location = saved_loc;
436 signature = identifier_subst (class_name, "L", '.', '/', ";");
437 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
439 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
440 both a typedef and in the struct name-space. We may want to re-visit
441 this later, but for now it reduces the changes needed for gdb. */
442 DECL_ARTIFICIAL (decl) = 1;
444 pushdecl_top_level (decl);
446 return decl;
449 /* Finds the (global) class named NAME. Creates the class if not found.
450 Also creates associated TYPE_DECL.
451 Does not check if the class actually exists, load the class,
452 fill in field or methods, or do layout_type. */
454 tree
455 lookup_class (tree name)
457 tree decl = IDENTIFIER_CLASS_VALUE (name);
458 if (decl == NULL_TREE)
459 decl = push_class (make_class (), name);
460 return TREE_TYPE (decl);
463 void
464 set_super_info (int access_flags, tree this_class,
465 tree super_class, int interfaces_count)
467 int total_supers = interfaces_count;
468 tree class_decl = TYPE_NAME (this_class);
470 if (super_class)
471 total_supers++;
473 if (total_supers)
474 TYPE_BINFO (this_class) = make_tree_binfo (total_supers);
475 TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
476 if (super_class)
478 tree super_binfo = make_tree_binfo (0);
479 BINFO_TYPE (super_binfo) = super_class;
480 BINFO_OFFSET (super_binfo) = integer_zero_node;
481 BINFO_BASE_APPEND (TYPE_BINFO (this_class), super_binfo);
482 CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
485 set_class_decl_access_flags (access_flags, class_decl);
488 void
489 set_class_decl_access_flags (int access_flags, tree class_decl)
491 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
492 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
493 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
494 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
495 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
496 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
497 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
498 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
499 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
502 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
503 direct sub-classes of Object are 1, and so on. */
506 class_depth (tree clas)
508 int depth = 0;
509 if (! CLASS_LOADED_P (clas))
510 load_class (clas, 1);
511 if (TYPE_SIZE (clas) == error_mark_node)
512 return -1;
513 while (clas != object_type_node)
515 depth++;
516 clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
518 return depth;
521 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
524 interface_of_p (tree type1, tree type2)
526 int i;
527 tree binfo, base_binfo;
529 if (! TYPE_BINFO (type2))
530 return 0;
532 for (binfo = TYPE_BINFO (type2), i = 0;
533 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
534 if (BINFO_TYPE (base_binfo) == type1)
535 return 1;
537 for (binfo = TYPE_BINFO (type2), i = 0;
538 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) /* */
539 if (BINFO_TYPE (base_binfo)
540 && interface_of_p (type1, BINFO_TYPE (base_binfo)))
541 return 1;
543 return 0;
546 /* Return true iff TYPE1 inherits from TYPE2. */
549 inherits_from_p (tree type1, tree type2)
551 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
553 if (type1 == type2)
554 return 1;
556 if (! CLASS_LOADED_P (type1))
557 load_class (type1, 1);
559 type1 = maybe_layout_super_class (CLASSTYPE_SUPER (type1), type1);
561 return 0;
564 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
567 enclosing_context_p (tree type1, tree type2)
569 if (!INNER_CLASS_TYPE_P (type2))
570 return 0;
572 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
573 type2;
574 type2 = (INNER_CLASS_TYPE_P (type2) ?
575 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
577 if (type2 == type1)
578 return 1;
581 return 0;
585 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
586 nesting level. */
589 common_enclosing_context_p (tree type1, tree type2)
591 while (type1)
593 tree current;
594 for (current = type2; current;
595 current = (INNER_CLASS_TYPE_P (current) ?
596 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
597 NULL_TREE))
598 if (type1 == current)
599 return 1;
601 if (INNER_CLASS_TYPE_P (type1))
602 type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
603 else
604 break;
606 return 0;
609 /* Return 1 iff there exists a common enclosing "this" between TYPE1
610 and TYPE2, without crossing any static context. */
613 common_enclosing_instance_p (tree type1, tree type2)
615 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
616 return 0;
618 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
619 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
620 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
622 tree current;
623 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
624 current = (PURE_INNER_CLASS_TYPE_P (current) ?
625 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
626 NULL_TREE))
627 if (type1 == current)
628 return 1;
630 return 0;
633 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
634 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
635 if attempt is made to add it twice. */
637 tree
638 maybe_add_interface (tree this_class, tree interface_class)
640 tree binfo, base_binfo;
641 int i;
643 for (binfo = TYPE_BINFO (this_class), i = 0;
644 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
645 if (BINFO_TYPE (base_binfo) == interface_class)
646 return interface_class;
647 add_interface (this_class, interface_class);
648 return NULL_TREE;
651 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
653 void
654 add_interface (tree this_class, tree interface_class)
656 tree interface_binfo = make_tree_binfo (0);
658 BINFO_TYPE (interface_binfo) = interface_class;
659 BINFO_OFFSET (interface_binfo) = integer_zero_node;
660 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
661 BINFO_VIRTUAL_P (interface_binfo) = 1;
663 BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
666 #if 0
667 /* Return the address of a pointer to the first FUNCTION_DECL
668 in the list (*LIST) whose DECL_NAME is NAME. */
670 static tree *
671 find_named_method (tree *list, tree name)
673 while (*list && DECL_NAME (*list) != name)
674 list = &TREE_CHAIN (*list);
675 return list;
677 #endif
679 static tree
680 build_java_method_type (tree fntype, tree this_class, int access_flags)
682 if (access_flags & ACC_STATIC)
683 return fntype;
684 return build_method_type (this_class, fntype);
687 tree
688 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
690 tree method_type, fndecl;
692 method_type = build_java_method_type (function_type,
693 this_class, access_flags);
695 fndecl = build_decl (FUNCTION_DECL, name, method_type);
696 DECL_CONTEXT (fndecl) = this_class;
698 DECL_LANG_SPECIFIC (fndecl)
699 = ggc_alloc_cleared (sizeof (struct lang_decl));
700 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
702 /* Initialize the static initializer test table. */
704 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
705 java_treetreehash_create (10, 1);
707 /* Initialize the initialized (static) class table. */
708 if (access_flags & ACC_STATIC)
709 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
710 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
712 /* Initialize the static method invocation compound list */
713 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
715 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
716 TYPE_METHODS (this_class) = fndecl;
718 /* Notice that this is a finalizer and update the class type
719 accordingly. This is used to optimize instance allocation. */
720 if (name == finalize_identifier_node
721 && TREE_TYPE (function_type) == void_type_node
722 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
723 HAS_FINALIZER_P (this_class) = 1;
725 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
726 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
727 if (access_flags & ACC_PRIVATE)
728 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
729 if (access_flags & ACC_NATIVE)
731 METHOD_NATIVE (fndecl) = 1;
732 DECL_EXTERNAL (fndecl) = 1;
734 if (access_flags & ACC_STATIC)
735 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
736 if (access_flags & ACC_FINAL)
737 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
738 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
739 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
740 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
741 return fndecl;
744 /* Add a method to THIS_CLASS.
745 The method's name is NAME.
746 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
748 tree
749 add_method (tree this_class, int access_flags, tree name, tree method_sig)
751 tree function_type, fndecl;
752 const unsigned char *sig
753 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
755 if (sig[0] != '(')
756 fatal_error ("bad method signature");
758 function_type = get_type_from_signature (method_sig);
759 fndecl = add_method_1 (this_class, access_flags, name, function_type);
760 set_java_signature (TREE_TYPE (fndecl), method_sig);
761 return fndecl;
764 tree
765 add_field (tree class, tree name, tree field_type, int flags)
767 int is_static = (flags & ACC_STATIC) != 0;
768 tree field;
769 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
770 TREE_CHAIN (field) = TYPE_FIELDS (class);
771 TYPE_FIELDS (class) = field;
772 DECL_CONTEXT (field) = class;
774 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
775 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
776 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
777 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
778 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
779 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
780 if (is_static)
782 FIELD_STATIC (field) = 1;
783 /* Always make field externally visible. This is required so
784 that native methods can always access the field. */
785 TREE_PUBLIC (field) = 1;
786 /* Considered external until we know what classes are being
787 compiled into this object file. */
788 DECL_EXTERNAL (field) = 1;
791 return field;
794 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
796 void
797 set_constant_value (tree field, tree constant)
799 if (field == NULL_TREE)
800 warning (OPT_Wattributes,
801 "misplaced ConstantValue attribute (not in any field)");
802 else if (DECL_INITIAL (field) != NULL_TREE)
803 warning (OPT_Wattributes,
804 "duplicate ConstantValue attribute for field '%s'",
805 IDENTIFIER_POINTER (DECL_NAME (field)));
806 else
808 DECL_INITIAL (field) = constant;
809 if (TREE_TYPE (constant) != TREE_TYPE (field)
810 && ! (TREE_TYPE (constant) == int_type_node
811 && INTEGRAL_TYPE_P (TREE_TYPE (field))
812 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
813 && ! (TREE_TYPE (constant) == utf8const_ptr_type
814 && TREE_TYPE (field) == string_ptr_type_node))
815 error ("ConstantValue attribute of field '%s' has wrong type",
816 IDENTIFIER_POINTER (DECL_NAME (field)));
817 if (FIELD_FINAL (field))
818 DECL_FIELD_FINAL_IUD (field) = 1;
822 /* Count the number of Unicode chars encoded in a given Ut8 string. */
824 #if 0
826 strLengthUtf8 (char *str, int len)
828 register unsigned char* ptr = (unsigned char*) str;
829 register unsigned char *limit = ptr + len;
830 int str_length = 0;
831 for (; ptr < limit; str_length++) {
832 if (UTF8_GET (ptr, limit) < 0)
833 return -1;
835 return str_length;
837 #endif
840 /* Calculate a hash value for a string encoded in Utf8 format.
841 * This returns the same hash value as specified for java.lang.String.hashCode.
844 static int32
845 hashUtf8String (const char *str, int len)
847 const unsigned char* ptr = (const unsigned char*) str;
848 const unsigned char *limit = ptr + len;
849 int32 hash = 0;
850 for (; ptr < limit;)
852 int ch = UTF8_GET (ptr, limit);
853 /* Updated specification from
854 http://www.javasoft.com/docs/books/jls/clarify.html. */
855 hash = (31 * hash) + ch;
857 return hash;
860 static GTY(()) tree utf8_decl_list = NULL_TREE;
862 tree
863 build_utf8_ref (tree name)
865 const char * name_ptr = IDENTIFIER_POINTER(name);
866 int name_len = IDENTIFIER_LENGTH(name);
867 char buf[60];
868 tree ctype, field = NULL_TREE, str_type, cinit, string;
869 static int utf8_count = 0;
870 int name_hash;
871 tree ref = IDENTIFIER_UTF8_REF (name);
872 tree decl;
873 if (ref != NULL_TREE)
874 return ref;
876 ctype = make_node (RECORD_TYPE);
877 str_type = build_prim_array_type (unsigned_byte_type_node,
878 name_len + 1); /* Allow for final '\0'. */
879 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
880 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
881 PUSH_FIELD (ctype, field, "data", str_type);
882 FINISH_RECORD (ctype);
883 START_RECORD_CONSTRUCTOR (cinit, ctype);
884 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
885 PUSH_FIELD_VALUE (cinit, "hash", build_int_cst (NULL_TREE, name_hash));
886 PUSH_FIELD_VALUE (cinit, "length", build_int_cst (NULL_TREE, name_len));
887 string = build_string (name_len, name_ptr);
888 TREE_TYPE (string) = str_type;
889 PUSH_FIELD_VALUE (cinit, "data", string);
890 FINISH_RECORD_CONSTRUCTOR (cinit);
891 TREE_CONSTANT (cinit) = 1;
892 TREE_INVARIANT (cinit) = 1;
894 /* Generate a unique-enough identifier. */
895 sprintf(buf, "_Utf%d", ++utf8_count);
897 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
898 TREE_STATIC (decl) = 1;
899 DECL_ARTIFICIAL (decl) = 1;
900 DECL_IGNORED_P (decl) = 1;
901 TREE_READONLY (decl) = 1;
902 TREE_THIS_VOLATILE (decl) = 0;
903 DECL_INITIAL (decl) = cinit;
905 if (HAVE_GAS_SHF_MERGE)
907 int decl_size;
908 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
909 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
910 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
911 if (flag_merge_constants && decl_size < 256)
913 char buf[32];
914 int flags = (SECTION_OVERRIDE
915 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
916 sprintf (buf, ".rodata.jutf8.%d", decl_size);
917 switch_to_section (get_section (buf, flags, NULL));
918 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
922 TREE_CHAIN (decl) = utf8_decl_list;
923 layout_decl (decl, 0);
924 pushdecl (decl);
925 rest_of_decl_compilation (decl, global_bindings_p (), 0);
926 cgraph_varpool_mark_needed_node (cgraph_varpool_node (decl));
927 utf8_decl_list = decl;
928 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
929 IDENTIFIER_UTF8_REF (name) = ref;
930 return ref;
933 /* Like build_class_ref, but instead of a direct reference generate a
934 pointer into the constant pool. */
936 static tree
937 build_indirect_class_ref (tree type)
939 int index;
940 tree cl;
941 index = alloc_class_constant (type);
942 cl = build_ref_from_constant_pool (index);
943 return convert (promote_type (class_ptr_type), cl);
946 /* Build a reference to the class TYPE.
947 Also handles primitive types and array types. */
949 tree
950 build_class_ref (tree type)
952 int is_compiled = is_compiled_class (type);
953 if (is_compiled)
955 tree ref, decl_name, decl;
956 if (TREE_CODE (type) == POINTER_TYPE)
957 type = TREE_TYPE (type);
959 if (flag_indirect_dispatch
960 && type != output_class
961 && TREE_CODE (type) == RECORD_TYPE)
962 return build_indirect_class_ref (type);
964 if (TREE_CODE (type) == RECORD_TYPE)
966 if (TYPE_SIZE (type) == error_mark_node)
967 return null_pointer_node;
968 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
969 "", '/', '/', ".class");
970 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
971 if (decl == NULL_TREE)
973 decl = build_decl (VAR_DECL, decl_name, class_type_node);
974 TREE_STATIC (decl) = 1;
975 TREE_PUBLIC (decl) = 1;
976 DECL_IGNORED_P (decl) = 1;
977 DECL_ARTIFICIAL (decl) = 1;
978 if (is_compiled == 1)
979 DECL_EXTERNAL (decl) = 1;
980 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
981 DECL_CLASS_FIELD_P (decl) = 1;
982 DECL_CONTEXT (decl) = type;
984 /* ??? We want to preserve the DECL_CONTEXT we set just above,
985 that that means not calling pushdecl_top_level. */
986 IDENTIFIER_GLOBAL_VALUE (decl_name) = decl;
989 else
991 const char *name;
992 char buffer[25];
993 if (flag_emit_class_files)
995 const char *prim_class_name;
996 tree prim_class;
997 if (type == char_type_node)
998 prim_class_name = "java.lang.Character";
999 else if (type == boolean_type_node)
1000 prim_class_name = "java.lang.Boolean";
1001 else if (type == byte_type_node)
1002 prim_class_name = "java.lang.Byte";
1003 else if (type == short_type_node)
1004 prim_class_name = "java.lang.Short";
1005 else if (type == int_type_node)
1006 prim_class_name = "java.lang.Integer";
1007 else if (type == long_type_node)
1008 prim_class_name = "java.lang.Long";
1009 else if (type == float_type_node)
1010 prim_class_name = "java.lang.Float";
1011 else if (type == double_type_node)
1012 prim_class_name = "java.lang.Double";
1013 else if (type == void_type_node)
1014 prim_class_name = "java.lang.Void";
1015 else
1016 gcc_unreachable ();
1018 prim_class = lookup_class (get_identifier (prim_class_name));
1019 /* We wrap the class in a NOP_EXPR, because it is a
1020 type. We can't hold it in the COMPONENT_REF itself,
1021 as that type must remain NULL. */
1022 prim_class = build1 (NOP_EXPR, prim_class, NULL_TREE);
1024 return build3 (COMPONENT_REF, NULL_TREE,
1025 prim_class, TYPE_identifier_node, NULL_TREE);
1027 decl_name = TYPE_NAME (type);
1028 if (TREE_CODE (decl_name) == TYPE_DECL)
1029 decl_name = DECL_NAME (decl_name);
1030 name = IDENTIFIER_POINTER (decl_name);
1031 if (strncmp (name, "promoted_", 9) == 0)
1032 name += 9;
1033 sprintf (buffer, "_Jv_%sClass", name);
1034 decl_name = get_identifier (buffer);
1035 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1036 if (decl == NULL_TREE)
1038 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1039 TREE_STATIC (decl) = 1;
1040 TREE_PUBLIC (decl) = 1;
1041 DECL_EXTERNAL (decl) = 1;
1042 DECL_ARTIFICIAL (decl) = 1;
1043 pushdecl_top_level (decl);
1047 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1048 return ref;
1050 else
1051 return build_indirect_class_ref (type);
1054 /* Create a local statically allocated variable that will hold a
1055 pointer to a static field. */
1057 static tree
1058 build_fieldref_cache_entry (int index, tree fdecl ATTRIBUTE_UNUSED)
1060 tree decl, decl_name;
1061 const char *name = IDENTIFIER_POINTER (mangled_classname ("_cpool_", output_class));
1062 char *buf = alloca (strlen (name) + 20);
1063 sprintf (buf, "%s_%d_ref", name, index);
1064 decl_name = get_identifier (buf);
1065 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1066 if (decl == NULL_TREE)
1068 decl = build_decl (VAR_DECL, decl_name, ptr_type_node);
1069 TREE_STATIC (decl) = 1;
1070 TREE_PUBLIC (decl) = 0;
1071 DECL_EXTERNAL (decl) = 0;
1072 DECL_ARTIFICIAL (decl) = 1;
1073 pushdecl_top_level (decl);
1075 return decl;
1078 tree
1079 build_static_field_ref (tree fdecl)
1081 tree fclass = DECL_CONTEXT (fdecl);
1082 int is_compiled = is_compiled_class (fclass);
1083 int from_class = ! CLASS_FROM_SOURCE_P (current_class);
1085 /* Allow static final fields to fold to a constant. When using
1086 -findirect-dispatch, we simply never do this folding if compiling
1087 from .class; in the .class file constants will be referred to via
1088 the constant pool. */
1089 if ((!flag_indirect_dispatch || !from_class)
1090 && (is_compiled
1091 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1092 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1093 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1094 && TREE_CONSTANT (DECL_INITIAL (fdecl)))))
1096 if (is_compiled == 1)
1097 DECL_EXTERNAL (fdecl) = 1;
1099 else
1101 /* Generate a CONSTANT_FieldRef for FDECL in the constant pool
1102 and a class local static variable CACHE_ENTRY, then
1104 *(fdecl **)((__builtin_expect (cache_entry == null, false))
1105 ? cache_entry = _Jv_ResolvePoolEntry (output_class, cpool_index)
1106 : cache_entry)
1108 This can mostly be optimized away, so that the usual path is a
1109 load followed by a test and branch. _Jv_ResolvePoolEntry is
1110 only called once for each constant pool entry.
1112 There is an optimization that we don't do: at the start of a
1113 method, create a local copy of CACHE_ENTRY and use that instead.
1117 int cpool_index = alloc_constant_fieldref (output_class, fdecl);
1118 tree cache_entry = build_fieldref_cache_entry (cpool_index, fdecl);
1119 tree test
1120 = build3 (CALL_EXPR, boolean_type_node,
1121 build_address_of (built_in_decls[BUILT_IN_EXPECT]),
1122 tree_cons (NULL_TREE, build2 (EQ_EXPR, boolean_type_node,
1123 cache_entry, null_pointer_node),
1124 build_tree_list (NULL_TREE, boolean_false_node)),
1125 NULL_TREE);
1126 tree cpool_index_cst = build_int_cst (NULL_TREE, cpool_index);
1127 tree init
1128 = build3 (CALL_EXPR, ptr_type_node,
1129 build_address_of (soft_resolvepoolentry_node),
1130 tree_cons (NULL_TREE, build_class_ref (output_class),
1131 build_tree_list (NULL_TREE, cpool_index_cst)),
1132 NULL_TREE);
1133 init = build2 (MODIFY_EXPR, ptr_type_node, cache_entry, init);
1134 init = build3 (COND_EXPR, ptr_type_node, test, init, cache_entry);
1135 init = fold_convert (build_pointer_type (TREE_TYPE (fdecl)), init);
1136 fdecl = build1 (INDIRECT_REF, TREE_TYPE (fdecl), init);
1138 return fdecl;
1142 get_access_flags_from_decl (tree decl)
1144 int access_flags = 0;
1145 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1147 if (FIELD_STATIC (decl))
1148 access_flags |= ACC_STATIC;
1149 if (FIELD_PUBLIC (decl))
1150 access_flags |= ACC_PUBLIC;
1151 if (FIELD_PROTECTED (decl))
1152 access_flags |= ACC_PROTECTED;
1153 if (FIELD_PRIVATE (decl))
1154 access_flags |= ACC_PRIVATE;
1155 if (FIELD_FINAL (decl))
1156 access_flags |= ACC_FINAL;
1157 if (FIELD_VOLATILE (decl))
1158 access_flags |= ACC_VOLATILE;
1159 if (FIELD_TRANSIENT (decl))
1160 access_flags |= ACC_TRANSIENT;
1161 return access_flags;
1163 if (TREE_CODE (decl) == TYPE_DECL)
1165 if (CLASS_PUBLIC (decl))
1166 access_flags |= ACC_PUBLIC;
1167 if (CLASS_FINAL (decl))
1168 access_flags |= ACC_FINAL;
1169 if (CLASS_SUPER (decl))
1170 access_flags |= ACC_SUPER;
1171 if (CLASS_INTERFACE (decl))
1172 access_flags |= ACC_INTERFACE;
1173 if (CLASS_ABSTRACT (decl))
1174 access_flags |= ACC_ABSTRACT;
1175 if (CLASS_STATIC (decl))
1176 access_flags |= ACC_STATIC;
1177 if (CLASS_PRIVATE (decl))
1178 access_flags |= ACC_PRIVATE;
1179 if (CLASS_PROTECTED (decl))
1180 access_flags |= ACC_PROTECTED;
1181 if (CLASS_STRICTFP (decl))
1182 access_flags |= ACC_STRICT;
1183 return access_flags;
1185 if (TREE_CODE (decl) == FUNCTION_DECL)
1187 if (METHOD_PUBLIC (decl))
1188 access_flags |= ACC_PUBLIC;
1189 if (METHOD_PRIVATE (decl))
1190 access_flags |= ACC_PRIVATE;
1191 if (METHOD_PROTECTED (decl))
1192 access_flags |= ACC_PROTECTED;
1193 if (METHOD_STATIC (decl))
1194 access_flags |= ACC_STATIC;
1195 if (METHOD_FINAL (decl))
1196 access_flags |= ACC_FINAL;
1197 if (METHOD_SYNCHRONIZED (decl))
1198 access_flags |= ACC_SYNCHRONIZED;
1199 if (METHOD_NATIVE (decl))
1200 access_flags |= ACC_NATIVE;
1201 if (METHOD_ABSTRACT (decl))
1202 access_flags |= ACC_ABSTRACT;
1203 if (METHOD_STRICTFP (decl))
1204 access_flags |= ACC_STRICT;
1205 if (METHOD_INVISIBLE (decl))
1206 access_flags |= ACC_INVISIBLE;
1207 return access_flags;
1209 gcc_unreachable ();
1212 static GTY (()) int alias_labelno = 0;
1214 /* Create a private alias for METHOD. Using this alias instead of the method
1215 decl ensures that ncode entries in the method table point to the real function
1216 at runtime, not a PLT entry. */
1218 static tree
1219 make_local_function_alias (tree method)
1221 #ifdef ASM_OUTPUT_DEF
1222 tree alias;
1224 const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1225 char *name = alloca (strlen (method_name) + 2);
1226 char *buf = alloca (strlen (method_name) + 128);
1228 /* Only create aliases for local functions. */
1229 if (DECL_EXTERNAL (method))
1230 return method;
1232 /* Prefix method_name with 'L' for the alias label. */
1233 *name = 'L';
1234 strcpy (name + 1, method_name);
1236 ASM_GENERATE_INTERNAL_LABEL (buf, name, alias_labelno++);
1237 alias = build_decl (FUNCTION_DECL, get_identifier (buf),
1238 TREE_TYPE (method));
1239 DECL_CONTEXT (alias) = NULL;
1240 TREE_READONLY (alias) = TREE_READONLY (method);
1241 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1242 TREE_PUBLIC (alias) = 0;
1243 DECL_EXTERNAL (alias) = 0;
1244 DECL_ARTIFICIAL (alias) = 1;
1245 DECL_INLINE (alias) = 0;
1246 DECL_INITIAL (alias) = error_mark_node;
1247 TREE_ADDRESSABLE (alias) = 1;
1248 TREE_USED (alias) = 1;
1249 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
1250 if (!flag_syntax_only)
1251 assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1252 return alias;
1253 #else
1254 return method;
1255 #endif
1258 /** Make reflection data (_Jv_Field) for field FDECL. */
1260 static tree
1261 make_field_value (tree fdecl)
1263 tree finit;
1264 int flags;
1265 tree type = TREE_TYPE (fdecl);
1266 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1268 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1269 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1270 if (resolved)
1271 type = build_class_ref (type);
1272 else
1274 tree signature = build_java_signature (type);
1276 type = build_utf8_ref (unmangle_classname
1277 (IDENTIFIER_POINTER (signature),
1278 IDENTIFIER_LENGTH (signature)));
1280 PUSH_FIELD_VALUE (finit, "type", type);
1282 flags = get_access_flags_from_decl (fdecl);
1283 if (! resolved)
1284 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1286 PUSH_FIELD_VALUE (finit, "accflags", build_int_cst (NULL_TREE, flags));
1287 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1289 PUSH_FIELD_VALUE
1290 (finit, "info",
1291 build_constructor_from_list (field_info_union_node,
1292 build_tree_list
1293 ((FIELD_STATIC (fdecl)
1294 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1295 : TYPE_FIELDS (field_info_union_node)),
1296 (FIELD_STATIC (fdecl)
1297 ? build_address_of (fdecl)
1298 : byte_position (fdecl)))));
1300 FINISH_RECORD_CONSTRUCTOR (finit);
1301 return finit;
1304 /** Make reflection data (_Jv_Method) for method MDECL. */
1306 static tree
1307 make_method_value (tree mdecl)
1309 static int method_name_count = 0;
1310 tree minit;
1311 tree index;
1312 tree code;
1313 tree class_decl;
1314 #define ACC_TRANSLATED 0x4000
1315 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1317 class_decl = DECL_CONTEXT (mdecl);
1318 /* For interfaces, the index field contains the dispatch index. */
1319 if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1320 index = build_int_cst (NULL_TREE,
1321 get_interface_method_index (mdecl, class_decl));
1322 else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1323 index = get_method_index (mdecl);
1324 else
1325 index = integer_minus_one_node;
1327 code = null_pointer_node;
1328 if (METHOD_ABSTRACT (mdecl))
1329 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1330 soft_abstractmethod_node);
1331 else
1332 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1333 make_local_function_alias (mdecl));
1334 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1335 PUSH_FIELD_VALUE (minit, "name",
1336 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1337 init_identifier_node
1338 : DECL_NAME (mdecl)));
1340 tree signature = build_java_signature (TREE_TYPE (mdecl));
1341 PUSH_FIELD_VALUE (minit, "signature",
1342 (build_utf8_ref
1343 (unmangle_classname
1344 (IDENTIFIER_POINTER(signature),
1345 IDENTIFIER_LENGTH(signature)))));
1347 PUSH_FIELD_VALUE (minit, "accflags", build_int_cst (NULL_TREE, accflags));
1348 PUSH_FIELD_VALUE (minit, "index", index);
1349 PUSH_FIELD_VALUE (minit, "ncode", code);
1352 /* Compute the `throws' information for the method. */
1353 tree table = null_pointer_node;
1354 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1356 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1357 tree iter, type, array;
1358 char buf[60];
1360 table = tree_cons (NULL_TREE, table, NULL_TREE);
1361 for (iter = DECL_FUNCTION_THROWS (mdecl);
1362 iter != NULL_TREE;
1363 iter = TREE_CHAIN (iter))
1365 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1366 tree utf8
1367 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1368 IDENTIFIER_LENGTH (sig)));
1369 table = tree_cons (NULL_TREE, utf8, table);
1371 type = build_prim_array_type (ptr_type_node, length);
1372 table = build_constructor_from_list (type, table);
1373 /* Compute something unique enough. */
1374 sprintf (buf, "_methods%d", method_name_count++);
1375 array = build_decl (VAR_DECL, get_identifier (buf), type);
1376 DECL_INITIAL (array) = table;
1377 TREE_STATIC (array) = 1;
1378 DECL_ARTIFICIAL (array) = 1;
1379 DECL_IGNORED_P (array) = 1;
1380 rest_of_decl_compilation (array, 1, 0);
1382 table = build1 (ADDR_EXPR, ptr_type_node, array);
1385 PUSH_FIELD_VALUE (minit, "throws", table);
1388 FINISH_RECORD_CONSTRUCTOR (minit);
1389 return minit;
1392 static tree
1393 get_dispatch_vector (tree type)
1395 tree vtable = TYPE_VTABLE (type);
1397 if (vtable == NULL_TREE)
1399 HOST_WIDE_INT i;
1400 tree method;
1401 tree super = CLASSTYPE_SUPER (type);
1402 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1403 vtable = make_tree_vec (nvirtuals);
1404 TYPE_VTABLE (type) = vtable;
1405 if (super != NULL_TREE)
1407 tree super_vtable = get_dispatch_vector (super);
1409 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1410 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1413 for (method = TYPE_METHODS (type); method != NULL_TREE;
1414 method = TREE_CHAIN (method))
1416 tree method_index = get_method_index (method);
1417 if (method_index != NULL_TREE
1418 && host_integerp (method_index, 0))
1419 TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1423 return vtable;
1426 static tree
1427 get_dispatch_table (tree type, tree this_class_addr)
1429 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1430 tree vtable = get_dispatch_vector (type);
1431 int i, j;
1432 tree list = NULL_TREE;
1433 int nvirtuals = TREE_VEC_LENGTH (vtable);
1434 int arraysize;
1435 tree gc_descr;
1437 for (i = nvirtuals; --i >= 0; )
1439 tree method = TREE_VEC_ELT (vtable, i);
1440 if (METHOD_ABSTRACT (method))
1442 if (! abstract_p)
1443 warning (0, "%Jabstract method in non-abstract class", method);
1445 if (TARGET_VTABLE_USES_DESCRIPTORS)
1446 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1447 list = tree_cons (NULL_TREE, null_pointer_node, list);
1448 else
1449 list = tree_cons (NULL_TREE, null_pointer_node, list);
1451 else
1453 if (TARGET_VTABLE_USES_DESCRIPTORS)
1454 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1456 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1457 method, build_int_cst (NULL_TREE, j));
1458 TREE_CONSTANT (fdesc) = 1;
1459 TREE_INVARIANT (fdesc) = 1;
1460 list = tree_cons (NULL_TREE, fdesc, list);
1462 else
1463 list = tree_cons (NULL_TREE,
1464 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1465 method),
1466 list);
1470 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1471 using the Boehm GC we sometimes stash a GC type descriptor
1472 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1473 the emitted byte count during the output to the assembly file. */
1474 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1475 fake "function descriptor". It's first word is the is the class
1476 pointer, and subsequent words (usually one) contain the GC descriptor.
1477 In all other cases, we reserve two extra vtable slots. */
1478 gc_descr = get_boehm_type_descriptor (type);
1479 list = tree_cons (NULL_TREE, gc_descr, list);
1480 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1481 list = tree_cons (NULL_TREE, gc_descr, list);
1482 list = tree_cons (NULL_TREE, this_class_addr, list);
1484 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1485 list = tree_cons (NULL_TREE, null_pointer_node, list);
1486 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1487 list = tree_cons (integer_zero_node, null_pointer_node, list);
1489 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1490 if (TARGET_VTABLE_USES_DESCRIPTORS)
1491 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1492 arraysize += 2;
1493 return build_constructor_from_list
1494 (build_prim_array_type (nativecode_ptr_type_node,
1495 arraysize), list);
1499 /* Set the method_index for a method decl. */
1500 void
1501 set_method_index (tree decl, tree method_index)
1503 if (method_index != NULL_TREE)
1505 /* method_index is null if we're using indirect dispatch. */
1506 method_index = fold (convert (sizetype, method_index));
1508 if (TARGET_VTABLE_USES_DESCRIPTORS)
1509 /* Add one to skip bogus descriptor for class and GC descriptor. */
1510 method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1511 else
1512 /* Add 1 to skip "class" field of dtable, and 1 to skip GC
1513 descriptor. */
1514 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1517 DECL_VINDEX (decl) = method_index;
1520 /* Get the method_index for a method decl. */
1521 tree
1522 get_method_index (tree decl)
1524 tree method_index = DECL_VINDEX (decl);
1526 if (! method_index)
1527 return NULL;
1529 if (TARGET_VTABLE_USES_DESCRIPTORS)
1530 /* Sub one to skip bogus descriptor for class and GC descriptor. */
1531 method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1532 else
1533 /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1534 method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1536 return method_index;
1539 static int
1540 supers_all_compiled (tree type)
1542 while (type != NULL_TREE)
1544 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1545 return 0;
1546 type = CLASSTYPE_SUPER (type);
1548 return 1;
1551 /* The forth (index of 3) element in the vtable is the GC descriptor.
1552 A value of 2 indicates that the class uses _Jv_MarkObj. */
1553 static int
1554 uses_jv_markobj_p(tree dtable)
1556 tree v;
1557 v = VEC_index (constructor_elt, CONSTRUCTOR_ELTS (dtable), 3)->value;
1558 return (2 == TREE_INT_CST_LOW (v));
1561 void
1562 make_class_data (tree type)
1564 tree decl, cons, temp;
1565 tree field, fields_decl;
1566 tree static_fields = NULL_TREE;
1567 tree instance_fields = NULL_TREE;
1568 HOST_WIDE_INT static_field_count = 0;
1569 HOST_WIDE_INT instance_field_count = 0;
1570 HOST_WIDE_INT field_count;
1571 tree field_array_type;
1572 tree method;
1573 tree methods = NULL_TREE;
1574 tree dtable_decl = NULL_TREE;
1575 HOST_WIDE_INT method_count = 0;
1576 tree method_array_type;
1577 tree methods_decl;
1578 tree super;
1579 tree this_class_addr;
1580 tree constant_pool_constructor;
1581 tree interfaces = null_pointer_node;
1582 int interface_len = 0;
1583 int uses_jv_markobj = 0;
1584 tree type_decl = TYPE_NAME (type);
1585 tree id_main = get_identifier("main");
1586 tree id_class = get_identifier("java.lang.Class");
1587 /** Offset from start of virtual function table declaration
1588 to where objects actually point at, following new g++ ABI. */
1589 tree dtable_start_offset = build_int_cst (NULL_TREE,
1590 2 * POINTER_SIZE / BITS_PER_UNIT);
1592 this_class_addr = build_class_ref (type);
1593 decl = TREE_OPERAND (this_class_addr, 0);
1595 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1596 && !flag_indirect_dispatch)
1598 tree dtable = get_dispatch_table (type, this_class_addr);
1599 uses_jv_markobj = uses_jv_markobj_p(dtable);
1600 dtable_decl = build_dtable_decl (type);
1601 DECL_INITIAL (dtable_decl) = dtable;
1602 TREE_STATIC (dtable_decl) = 1;
1603 DECL_ARTIFICIAL (dtable_decl) = 1;
1604 DECL_IGNORED_P (dtable_decl) = 1;
1605 TREE_PUBLIC (dtable_decl) = 1;
1606 rest_of_decl_compilation (dtable_decl, 1, 0);
1607 if (type == class_type_node)
1608 class_dtable_decl = dtable_decl;
1611 /* Build Field array. */
1612 field = TYPE_FIELDS (type);
1613 while (field && DECL_ARTIFICIAL (field))
1614 field = TREE_CHAIN (field); /* Skip dummy fields. */
1615 if (field && DECL_NAME (field) == NULL_TREE)
1616 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1617 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1619 if (! DECL_ARTIFICIAL (field))
1621 if (FIELD_STATIC (field))
1623 /* We must always create reflection data for static fields
1624 as it is used in the creation of the field itself. */
1625 tree init = make_field_value (field);
1626 tree initial = DECL_INITIAL (field);
1627 static_field_count++;
1628 static_fields = tree_cons (NULL_TREE, init, static_fields);
1629 /* If the initial value is a string constant,
1630 prevent output_constant from trying to assemble the value. */
1631 if (initial != NULL_TREE
1632 && TREE_TYPE (initial) == string_ptr_type_node)
1633 DECL_INITIAL (field) = NULL_TREE;
1634 rest_of_decl_compilation (field, 1, 1);
1635 DECL_INITIAL (field) = initial;
1637 else if (uses_jv_markobj || !flag_reduced_reflection)
1639 tree init = make_field_value (field);
1640 instance_field_count++;
1641 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1645 field_count = static_field_count + instance_field_count;
1646 if (field_count > 0)
1648 static_fields = nreverse (static_fields);
1649 instance_fields = nreverse (instance_fields);
1650 static_fields = chainon (static_fields, instance_fields);
1651 field_array_type = build_prim_array_type (field_type_node, field_count);
1652 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1653 field_array_type);
1654 DECL_INITIAL (fields_decl) = build_constructor_from_list
1655 (field_array_type, static_fields);
1656 TREE_STATIC (fields_decl) = 1;
1657 DECL_ARTIFICIAL (fields_decl) = 1;
1658 DECL_IGNORED_P (fields_decl) = 1;
1659 rest_of_decl_compilation (fields_decl, 1, 0);
1661 else
1662 fields_decl = NULL_TREE;
1664 /* Build Method array. */
1665 for (method = TYPE_METHODS (type);
1666 method != NULL_TREE; method = TREE_CHAIN (method))
1668 tree init;
1669 if (METHOD_PRIVATE (method)
1670 && ! flag_keep_inline_functions
1671 && optimize)
1672 continue;
1673 /* Even if we have a decl, we don't necessarily have the code.
1674 This can happen if we inherit a method from a superclass for
1675 which we don't have a .class file. */
1676 if (METHOD_DUMMY (method))
1677 continue;
1679 /* Generate method reflection data if:
1681 - !flag_reduced_reflection.
1683 - <clinit> -- The runtime uses reflection to initialize the
1684 class.
1686 - Any method in class java.lang.Class -- Class.forName() and
1687 perhaps other things require it.
1689 - class$ -- It does not work if reflection data missing.
1691 - main -- Reflection is used to find main(String[]) methods.
1693 - public not static -- It is potentially part of an
1694 interface. The runtime uses reflection data to build
1695 interface dispatch tables. */
1696 if (!flag_reduced_reflection
1697 || DECL_CLINIT_P (method)
1698 || DECL_NAME (type_decl) == id_class
1699 || DECL_NAME (method) == id_main
1700 || (METHOD_PUBLIC (method) && !METHOD_STATIC (method))
1701 || TYPE_DOT_CLASS (type) == method)
1703 init = make_method_value (method);
1704 method_count++;
1705 methods = tree_cons (NULL_TREE, init, methods);
1708 method_array_type = build_prim_array_type (method_type_node, method_count);
1709 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1710 method_array_type);
1711 DECL_INITIAL (methods_decl) = build_constructor_from_list
1712 (method_array_type, nreverse (methods));
1713 TREE_STATIC (methods_decl) = 1;
1714 DECL_ARTIFICIAL (methods_decl) = 1;
1715 DECL_IGNORED_P (methods_decl) = 1;
1716 rest_of_decl_compilation (methods_decl, 1, 0);
1718 if (class_dtable_decl == NULL_TREE)
1720 class_dtable_decl = build_dtable_decl (class_type_node);
1721 TREE_STATIC (class_dtable_decl) = 1;
1722 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1723 DECL_IGNORED_P (class_dtable_decl) = 1;
1724 if (is_compiled_class (class_type_node) != 2)
1725 DECL_EXTERNAL (class_dtable_decl) = 1;
1726 rest_of_decl_compilation (class_dtable_decl, 1, 0);
1729 super = CLASSTYPE_SUPER (type);
1730 if (super == NULL_TREE)
1731 super = null_pointer_node;
1732 else if (! flag_indirect_dispatch
1733 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1734 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1735 super = build_class_ref (super);
1736 else
1738 int super_index = alloc_class_constant (super);
1739 super = build_int_cst (ptr_type_node, super_index);
1742 /* Build and emit the array of implemented interfaces. */
1743 if (type != object_type_node)
1744 interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
1746 if (interface_len > 0)
1748 tree init = NULL_TREE;
1749 int i;
1750 tree interface_array_type, idecl;
1751 interface_array_type
1752 = build_prim_array_type (class_ptr_type, interface_len);
1753 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1754 interface_array_type);
1756 for (i = interface_len; i > 0; i--)
1758 tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1759 tree iclass = BINFO_TYPE (child);
1760 tree index;
1761 if (! flag_indirect_dispatch
1762 && (assume_compiled
1763 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1764 index = build_class_ref (iclass);
1765 else
1767 int int_index = alloc_class_constant (iclass);
1768 index = build_int_cst (ptr_type_node, int_index);
1770 init = tree_cons (NULL_TREE, index, init);
1772 DECL_INITIAL (idecl) = build_constructor_from_list (interface_array_type,
1773 init);
1774 TREE_STATIC (idecl) = 1;
1775 DECL_ARTIFICIAL (idecl) = 1;
1776 DECL_IGNORED_P (idecl) = 1;
1777 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1778 rest_of_decl_compilation (idecl, 1, 0);
1781 constant_pool_constructor = build_constants_constructor ();
1783 if (flag_indirect_dispatch)
1785 TYPE_OTABLE_DECL (type)
1786 = emit_symbol_table
1787 (DECL_NAME (TYPE_OTABLE_DECL (type)),
1788 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
1789 TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
1791 TYPE_ATABLE_DECL (type)
1792 = emit_symbol_table
1793 (DECL_NAME (TYPE_ATABLE_DECL (type)),
1794 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
1795 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
1797 TYPE_ITABLE_DECL (type)
1798 = emit_symbol_table
1799 (DECL_NAME (TYPE_ITABLE_DECL (type)),
1800 TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
1801 TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
1804 TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1806 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1807 PUSH_FIELD_VALUE (temp, "vtable",
1808 build2 (PLUS_EXPR, dtable_ptr_type,
1809 build1 (ADDR_EXPR, dtable_ptr_type,
1810 class_dtable_decl),
1811 dtable_start_offset));
1812 if (! flag_hash_synchronization)
1813 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1814 FINISH_RECORD_CONSTRUCTOR (temp);
1815 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1816 PUSH_SUPER_VALUE (cons, temp);
1817 PUSH_FIELD_VALUE (cons, "next_or_version", gcj_abi_version);
1818 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1819 PUSH_FIELD_VALUE (cons, "accflags",
1820 build_int_cst (NULL_TREE,
1821 get_access_flags_from_decl (type_decl)));
1823 PUSH_FIELD_VALUE (cons, "superclass",
1824 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1825 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1826 PUSH_FIELD_VALUE (cons, "methods",
1827 methods_decl == NULL_TREE ? null_pointer_node
1828 : build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1829 PUSH_FIELD_VALUE (cons, "method_count",
1830 build_int_cst (NULL_TREE, method_count));
1832 if (flag_indirect_dispatch)
1833 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1834 else
1835 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1837 PUSH_FIELD_VALUE (cons, "fields",
1838 fields_decl == NULL_TREE ? null_pointer_node
1839 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1840 /* If we're using the binary compatibility ABI we don't know the
1841 size until load time. */
1842 PUSH_FIELD_VALUE (cons, "size_in_bytes",
1843 (flag_indirect_dispatch
1844 ? integer_minus_one_node
1845 : size_in_bytes (type)));
1846 PUSH_FIELD_VALUE (cons, "field_count",
1847 build_int_cst (NULL_TREE, field_count));
1848 PUSH_FIELD_VALUE (cons, "static_field_count",
1849 build_int_cst (NULL_TREE, static_field_count));
1851 if (flag_indirect_dispatch)
1852 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1853 else
1854 PUSH_FIELD_VALUE (cons, "vtable",
1855 dtable_decl == NULL_TREE ? null_pointer_node
1856 : build2 (PLUS_EXPR, dtable_ptr_type,
1857 build1 (ADDR_EXPR, dtable_ptr_type,
1858 dtable_decl),
1859 dtable_start_offset));
1860 if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1862 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1863 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1865 else
1867 pushdecl_top_level (TYPE_OTABLE_SYMS_DECL (type));
1868 PUSH_FIELD_VALUE (cons, "otable",
1869 build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1870 PUSH_FIELD_VALUE (cons, "otable_syms",
1871 build1 (ADDR_EXPR, symbols_array_ptr_type,
1872 TYPE_OTABLE_SYMS_DECL (type)));
1873 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1874 TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1876 if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1878 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1879 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1881 else
1883 pushdecl_top_level (TYPE_ATABLE_SYMS_DECL (type));
1884 PUSH_FIELD_VALUE (cons, "atable",
1885 build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1886 PUSH_FIELD_VALUE (cons, "atable_syms",
1887 build1 (ADDR_EXPR, symbols_array_ptr_type,
1888 TYPE_ATABLE_SYMS_DECL (type)));
1889 TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1890 TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1892 if (TYPE_ITABLE_METHODS(type) == NULL_TREE)
1894 PUSH_FIELD_VALUE (cons, "itable", null_pointer_node);
1895 PUSH_FIELD_VALUE (cons, "itable_syms", null_pointer_node);
1897 else
1899 pushdecl_top_level (TYPE_ITABLE_SYMS_DECL (type));
1900 PUSH_FIELD_VALUE (cons, "itable",
1901 build1 (ADDR_EXPR, itable_ptr_type, TYPE_ITABLE_DECL (type)));
1902 PUSH_FIELD_VALUE (cons, "itable_syms",
1903 build1 (ADDR_EXPR, symbols_array_ptr_type,
1904 TYPE_ITABLE_SYMS_DECL (type)));
1905 TREE_CONSTANT (TYPE_ITABLE_DECL (type)) = 1;
1906 TREE_INVARIANT (TYPE_ITABLE_DECL (type)) = 1;
1909 PUSH_FIELD_VALUE (cons, "catch_classes",
1910 build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
1911 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1912 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1913 PUSH_FIELD_VALUE (cons, "interface_count",
1914 build_int_cst (NULL_TREE, interface_len));
1915 PUSH_FIELD_VALUE (cons, "state",
1916 convert (byte_type_node,
1917 build_int_cst (NULL_TREE, JV_STATE_PRELOADING)));
1919 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1920 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1921 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1922 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1923 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1924 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1927 tree assertion_table_ref;
1928 if (TYPE_ASSERTIONS (type) == NULL)
1929 assertion_table_ref = null_pointer_node;
1930 else
1931 assertion_table_ref = build1 (ADDR_EXPR,
1932 build_pointer_type (assertion_table_type),
1933 emit_assertion_table (type));
1935 PUSH_FIELD_VALUE (cons, "assertion_table", assertion_table_ref);
1938 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1939 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1940 PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1941 PUSH_FIELD_VALUE (cons, "engine", null_pointer_node);
1943 FINISH_RECORD_CONSTRUCTOR (cons);
1945 DECL_INITIAL (decl) = cons;
1947 /* Hash synchronization requires at least 64-bit alignment. */
1948 if (flag_hash_synchronization && POINTER_SIZE < 64)
1949 DECL_ALIGN (decl) = 64;
1951 rest_of_decl_compilation (decl, 1, 0);
1953 TYPE_OTABLE_DECL (type) = NULL_TREE;
1954 TYPE_ATABLE_DECL (type) = NULL_TREE;
1955 TYPE_CTABLE_DECL (type) = NULL_TREE;
1958 void
1959 finish_class (void)
1961 if (TYPE_VERIFY_METHOD (output_class))
1963 tree verify_method = TYPE_VERIFY_METHOD (output_class);
1964 DECL_SAVED_TREE (verify_method)
1965 = add_stmt_to_compound (DECL_SAVED_TREE (verify_method), void_type_node,
1966 build1 (RETURN_EXPR, void_type_node, NULL));
1967 java_genericize (verify_method);
1968 cgraph_finalize_function (verify_method, false);
1969 TYPE_ASSERTIONS (current_class) = NULL;
1972 java_expand_catch_classes (current_class);
1974 current_function_decl = NULL_TREE;
1975 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (current_class)) = 0;
1976 make_class_data (current_class);
1977 register_class ();
1978 rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
1981 /* Return 2 if CLASS is compiled by this compilation job;
1982 return 1 if CLASS can otherwise be assumed to be compiled;
1983 return 0 if we cannot assume that CLASS is compiled.
1984 Returns 1 for primitive and 0 for array types. */
1986 is_compiled_class (tree class)
1988 int seen_in_zip;
1989 if (TREE_CODE (class) == POINTER_TYPE)
1990 class = TREE_TYPE (class);
1991 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1992 return 1;
1993 if (TYPE_ARRAY_P (class))
1994 return 0;
1995 if (class == current_class)
1996 return 2;
1998 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1999 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
2001 /* The class was seen in the current ZIP file and will be
2002 available as a compiled class in the future but may not have
2003 been loaded already. Load it if necessary. This prevent
2004 build_class_ref () from crashing. */
2006 if (seen_in_zip && !CLASS_LOADED_P (class))
2007 load_class (class, 1);
2009 /* We return 2 for class seen in ZIP and class from files
2010 belonging to the same compilation unit */
2011 return 2;
2014 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
2016 if (!CLASS_LOADED_P (class))
2018 if (CLASS_FROM_SOURCE_P (class))
2019 safe_layout_class (class);
2020 else
2021 load_class (class, 1);
2023 return 1;
2026 return 0;
2029 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
2031 tree
2032 build_dtable_decl (tree type)
2034 tree dtype, decl;
2036 /* We need to build a new dtable type so that its size is uniquely
2037 computed when we're dealing with the class for real and not just
2038 faking it (like java.lang.Class during the initialization of the
2039 compiler.) We know we're not faking a class when CURRENT_CLASS is
2040 TYPE. */
2041 if (current_class == type)
2043 tree dummy = NULL_TREE;
2044 int n;
2046 dtype = make_node (RECORD_TYPE);
2048 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
2049 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
2051 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
2052 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2054 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2055 TREE_CHAIN (dummy) = tmp_field;
2056 DECL_CONTEXT (tmp_field) = dtype;
2057 DECL_ARTIFICIAL (tmp_field) = 1;
2058 dummy = tmp_field;
2061 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
2062 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2064 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2065 TREE_CHAIN (dummy) = tmp_field;
2066 DECL_CONTEXT (tmp_field) = dtype;
2067 DECL_ARTIFICIAL (tmp_field) = 1;
2068 dummy = tmp_field;
2071 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
2072 if (TARGET_VTABLE_USES_DESCRIPTORS)
2073 n *= TARGET_VTABLE_USES_DESCRIPTORS;
2075 PUSH_FIELD (dtype, dummy, "methods",
2076 build_prim_array_type (nativecode_ptr_type_node, n));
2077 layout_type (dtype);
2079 else
2080 dtype = dtable_type;
2082 decl = build_decl (VAR_DECL, get_identifier ("vt$"), dtype);
2083 DECL_CONTEXT (decl) = type;
2084 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl);
2085 DECL_VTABLE_P (decl) = 1;
2087 return decl;
2090 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2091 fields inherited from SUPER_CLASS. */
2093 void
2094 push_super_field (tree this_class, tree super_class)
2096 tree base_decl;
2097 /* Don't insert the field if we're just re-laying the class out. */
2098 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2099 return;
2100 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
2101 DECL_IGNORED_P (base_decl) = 1;
2102 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2103 TYPE_FIELDS (this_class) = base_decl;
2104 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2105 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2108 /* Handle the different manners we may have to lay out a super class. */
2110 static tree
2111 maybe_layout_super_class (tree super_class, tree this_class)
2113 if (!super_class)
2114 return NULL_TREE;
2115 else if (TREE_CODE (super_class) == RECORD_TYPE)
2117 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
2118 safe_layout_class (super_class);
2119 if (!CLASS_LOADED_P (super_class))
2120 load_class (super_class, 1);
2122 /* We might have to layout the class before its dependency on
2123 the super class gets resolved by java_complete_class */
2124 else if (TREE_CODE (super_class) == POINTER_TYPE)
2126 if (TREE_TYPE (super_class) != NULL_TREE)
2127 super_class = TREE_TYPE (super_class);
2128 else
2130 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
2131 we give it one. */
2132 tree this_wrap = NULL_TREE;
2134 /* Set the correct context for class resolution. */
2135 current_class = this_class;
2137 if (this_class)
2139 tree this_decl = TYPE_NAME (this_class);
2140 #ifdef USE_MAPPED_LOCATION
2141 this_wrap = build_expr_wfl (this_class,
2142 DECL_SOURCE_LOCATION (this_decl));
2143 #else
2144 this_wrap = build_expr_wfl (this_class,
2145 DECL_SOURCE_FILE (this_decl),
2146 DECL_SOURCE_LINE (this_decl), 0);
2147 #endif
2149 super_class
2150 = do_resolve_class (DECL_CONTEXT (TYPE_NAME (this_class)),
2151 this_class, super_class, NULL_TREE, this_wrap);
2152 if (!super_class)
2153 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
2154 super_class = TREE_TYPE (super_class);
2157 if (!TYPE_SIZE (super_class))
2158 safe_layout_class (super_class);
2160 return super_class;
2163 void
2164 layout_class (tree this_class)
2166 tree super_class = CLASSTYPE_SUPER (this_class);
2168 class_list = tree_cons (this_class, NULL_TREE, class_list);
2169 if (CLASS_BEING_LAIDOUT (this_class))
2171 char buffer [1024];
2172 char *report;
2173 tree current;
2175 sprintf (buffer, " with '%s'",
2176 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2177 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2179 for (current = TREE_CHAIN (class_list); current;
2180 current = TREE_CHAIN (current))
2182 tree decl = TYPE_NAME (TREE_PURPOSE (current));
2183 sprintf (buffer, "\n which inherits from '%s' (%s:%d)",
2184 IDENTIFIER_POINTER (DECL_NAME (decl)),
2185 DECL_SOURCE_FILE (decl),
2186 DECL_SOURCE_LINE (decl));
2187 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2189 obstack_1grow (&temporary_obstack, '\0');
2190 report = obstack_finish (&temporary_obstack);
2191 cyclic_inheritance_report = ggc_strdup (report);
2192 obstack_free (&temporary_obstack, report);
2193 TYPE_SIZE (this_class) = error_mark_node;
2194 return;
2196 CLASS_BEING_LAIDOUT (this_class) = 1;
2198 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2200 tree maybe_super_class
2201 = maybe_layout_super_class (super_class, this_class);
2202 if (maybe_super_class == NULL
2203 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2205 TYPE_SIZE (this_class) = error_mark_node;
2206 CLASS_BEING_LAIDOUT (this_class) = 0;
2207 class_list = TREE_CHAIN (class_list);
2208 return;
2210 if (TYPE_SIZE (this_class) == NULL_TREE)
2211 push_super_field (this_class, maybe_super_class);
2214 layout_type (this_class);
2216 /* Also recursively load/layout any superinterfaces, but only if
2217 class was loaded from bytecode. The source parser will take care
2218 of this itself. */
2219 if (!CLASS_FROM_SOURCE_P (this_class))
2221 int i;
2222 if (TYPE_BINFO (this_class))
2224 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2226 tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2227 tree super_interface = BINFO_TYPE (binfo);
2228 tree maybe_super_interface
2229 = maybe_layout_super_class (super_interface, NULL_TREE);
2230 if (maybe_super_interface == NULL
2231 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2233 TYPE_SIZE (this_class) = error_mark_node;
2234 CLASS_BEING_LAIDOUT (this_class) = 0;
2235 class_list = TREE_CHAIN (class_list);
2236 return;
2242 /* Convert the size back to an SI integer value. */
2243 TYPE_SIZE_UNIT (this_class) =
2244 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2246 CLASS_BEING_LAIDOUT (this_class) = 0;
2247 class_list = TREE_CHAIN (class_list);
2250 static void
2251 add_miranda_methods (tree base_class, tree search_class)
2253 int i;
2254 tree binfo, base_binfo;
2256 if (!CLASS_PARSED_P (search_class))
2257 load_class (search_class, 1);
2259 for (binfo = TYPE_BINFO (search_class), i = 1;
2260 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2262 tree method_decl;
2263 tree elt = BINFO_TYPE (base_binfo);
2265 /* FIXME: This is totally bogus. We should not be handling
2266 Miranda methods at all if we're using the BC ABI. */
2267 if (TYPE_DUMMY (elt))
2268 continue;
2270 /* Ensure that interface methods are seen in declared order. */
2271 if (!CLASS_LOADED_P (elt))
2272 load_class (elt, 1);
2273 layout_class_methods (elt);
2275 /* All base classes will have been laid out at this point, so the order
2276 will be correct. This code must match similar layout code in the
2277 runtime. */
2278 for (method_decl = TYPE_METHODS (elt);
2279 method_decl; method_decl = TREE_CHAIN (method_decl))
2281 tree sig, override;
2283 /* An interface can have <clinit>. */
2284 if (ID_CLINIT_P (DECL_NAME (method_decl)))
2285 continue;
2287 sig = build_java_argument_signature (TREE_TYPE (method_decl));
2288 override = lookup_argument_method (base_class,
2289 DECL_NAME (method_decl), sig);
2290 if (override == NULL_TREE)
2292 /* Found a Miranda method. Add it. */
2293 tree new_method;
2294 sig = build_java_signature (TREE_TYPE (method_decl));
2295 new_method
2296 = add_method (base_class,
2297 get_access_flags_from_decl (method_decl),
2298 DECL_NAME (method_decl), sig);
2299 METHOD_INVISIBLE (new_method) = 1;
2303 /* Try superinterfaces. */
2304 add_miranda_methods (base_class, elt);
2308 void
2309 layout_class_methods (tree this_class)
2311 tree method_decl, dtable_count;
2312 tree super_class, type_name;
2314 if (TYPE_NVIRTUALS (this_class))
2315 return;
2317 super_class = CLASSTYPE_SUPER (this_class);
2319 if (super_class)
2321 super_class = maybe_layout_super_class (super_class, this_class);
2322 if (!TYPE_NVIRTUALS (super_class))
2323 layout_class_methods (super_class);
2324 dtable_count = TYPE_NVIRTUALS (super_class);
2326 else
2327 dtable_count = integer_zero_node;
2329 type_name = TYPE_NAME (this_class);
2330 if (!flag_indirect_dispatch
2331 && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2333 /* An abstract class can have methods which are declared only in
2334 an implemented interface. These are called "Miranda
2335 methods". We make a dummy method entry for such methods
2336 here. */
2337 add_miranda_methods (this_class, this_class);
2340 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2342 for (method_decl = TYPE_METHODS (this_class);
2343 method_decl; method_decl = TREE_CHAIN (method_decl))
2344 dtable_count = layout_class_method (this_class, super_class,
2345 method_decl, dtable_count);
2347 TYPE_NVIRTUALS (this_class) = dtable_count;
2350 /* Return the index of METHOD in INTERFACE. This index begins at 1
2351 and is used as an argument for _Jv_LookupInterfaceMethodIdx(). */
2353 get_interface_method_index (tree method, tree interface)
2355 tree meth;
2356 int i = 1;
2358 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth))
2360 if (meth == method)
2361 return i;
2362 /* We don't want to put <clinit> into the interface table. */
2363 if (! ID_CLINIT_P (DECL_NAME (meth)))
2364 ++i;
2365 gcc_assert (meth != NULL_TREE);
2369 /* Lay METHOD_DECL out, returning a possibly new value of
2370 DTABLE_COUNT. Also mangle the method's name. */
2372 tree
2373 layout_class_method (tree this_class, tree super_class,
2374 tree method_decl, tree dtable_count)
2376 tree method_name = DECL_NAME (method_decl);
2378 TREE_PUBLIC (method_decl) = 1;
2379 /* Considered external until we know what classes are being
2380 compiled into this object file. */
2381 DECL_EXTERNAL (method_decl) = 1;
2383 if (ID_INIT_P (method_name))
2385 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2386 const char *ptr;
2387 for (ptr = p; *ptr; )
2389 if (*ptr++ == '.')
2390 p = ptr;
2392 DECL_CONSTRUCTOR_P (method_decl) = 1;
2393 build_java_argument_signature (TREE_TYPE (method_decl));
2395 else if (! METHOD_STATIC (method_decl))
2397 tree method_sig =
2398 build_java_argument_signature (TREE_TYPE (method_decl));
2399 bool method_override = false;
2400 tree super_method = lookup_argument_method (super_class, method_name,
2401 method_sig);
2402 if (super_method != NULL_TREE
2403 && ! METHOD_DUMMY (super_method))
2405 method_override = true;
2406 if (! METHOD_PUBLIC (super_method) &&
2407 ! METHOD_PROTECTED (super_method))
2409 /* Don't override private method, or default-access method in
2410 another package. */
2411 if (METHOD_PRIVATE (super_method) ||
2412 ! in_same_package (TYPE_NAME (this_class),
2413 TYPE_NAME (super_class)))
2414 method_override = false;
2417 if (method_override)
2419 tree method_index = get_method_index (super_method);
2420 set_method_index (method_decl, method_index);
2421 if (method_index == NULL_TREE
2422 && ! flag_indirect_dispatch
2423 && !CLASS_FROM_SOURCE_P (this_class)
2424 && ! DECL_ARTIFICIAL (super_method))
2425 error ("non-static method %q+D overrides static method",
2426 method_decl);
2428 else if (this_class == object_type_node
2429 && (METHOD_FINAL (method_decl)
2430 || METHOD_PRIVATE (method_decl)))
2432 /* We don't generate vtable entries for final Object
2433 methods. This is simply to save space, since every
2434 object would otherwise have to define them. */
2436 else if (! METHOD_PRIVATE (method_decl)
2437 && dtable_count)
2439 /* We generate vtable entries for final methods because they
2440 may one day be changed to non-final. */
2441 set_method_index (method_decl, dtable_count);
2442 dtable_count = fold_build2 (PLUS_EXPR, integer_type_node,
2443 dtable_count, integer_one_node);
2447 return dtable_count;
2450 static void
2451 register_class (void)
2453 tree node;
2455 if (!registered_class)
2456 registered_class = VEC_alloc (tree, gc, 8);
2458 node = TREE_OPERAND (build_class_ref (current_class), 0);
2459 VEC_safe_push (tree, gc, registered_class, node);
2462 /* Emit something to register classes at start-up time.
2464 The preferred mechanism is through the .jcr section, which contain
2465 a list of pointers to classes which get registered during constructor
2466 invocation time.
2468 The fallback mechanism is to add statements to *LIST_P to call
2469 _Jv_RegisterClass for each class in this file. These statements will
2470 be added to a static constructor function for this translation unit. */
2472 void
2473 emit_register_classes (tree *list_p)
2475 if (registered_class == NULL)
2476 return;
2478 /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2479 TARGET_ASM_NAMED_SECTION, else 0. Some targets meet those conditions
2480 but lack suitable crtbegin/end objects or linker support. These
2481 targets can overide the default in tm.h to use the fallback mechanism. */
2482 if (TARGET_USE_JCR_SECTION)
2484 tree klass, t;
2485 int i;
2487 #ifdef JCR_SECTION_NAME
2488 switch_to_section (get_section (JCR_SECTION_NAME, SECTION_WRITE, NULL));
2489 #else
2490 /* A target has defined TARGET_USE_JCR_SECTION,
2491 but doesn't have a JCR_SECTION_NAME. */
2492 gcc_unreachable ();
2493 #endif
2494 assemble_align (POINTER_SIZE);
2496 for (i = 0; VEC_iterate (tree, registered_class, i, klass); ++i)
2498 t = build_fold_addr_expr (klass);
2499 output_constant (t, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE);
2502 else
2504 tree klass, t, register_class_fn;
2505 int i;
2507 t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2508 t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2509 TREE_PUBLIC (t) = 1;
2510 DECL_EXTERNAL (t) = 1;
2511 register_class_fn = t;
2513 for (i = 0; VEC_iterate (tree, registered_class, i, klass); ++i)
2515 t = build_fold_addr_expr (klass);
2516 t = tree_cons (NULL, t, NULL);
2517 t = build_function_call_expr (register_class_fn, t);
2518 append_to_statement_list (t, list_p);
2523 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2525 static tree
2526 build_symbol_entry (tree decl)
2528 tree clname, name, signature, sym;
2529 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2530 /* ??? Constructors are given the name foo.foo all the way through
2531 the compiler, but in the method table they're all renamed
2532 foo.<init>. So, we have to do the same here unless we want an
2533 unresolved reference at runtime. */
2534 name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
2535 && DECL_CONSTRUCTOR_P (decl))
2536 ? init_identifier_node
2537 : DECL_NAME (decl));
2538 signature = build_java_signature (TREE_TYPE (decl));
2539 signature = build_utf8_ref (unmangle_classname
2540 (IDENTIFIER_POINTER (signature),
2541 IDENTIFIER_LENGTH (signature)));
2543 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2544 PUSH_FIELD_VALUE (sym, "clname", clname);
2545 PUSH_FIELD_VALUE (sym, "name", name);
2546 PUSH_FIELD_VALUE (sym, "signature", signature);
2547 FINISH_RECORD_CONSTRUCTOR (sym);
2548 TREE_CONSTANT (sym) = 1;
2549 TREE_INVARIANT (sym) = 1;
2551 return sym;
2554 /* Emit a symbol table: used by -findirect-dispatch. */
2556 tree
2557 emit_symbol_table (tree name, tree the_table, tree decl_list,
2558 tree the_syms_decl, tree the_array_element_type,
2559 int element_size)
2561 tree method_list, method, table, list, null_symbol;
2562 tree table_size, the_array_type;
2563 int index;
2565 /* Only emit a table if this translation unit actually made any
2566 references via it. */
2567 if (decl_list == NULL_TREE)
2568 return the_table;
2570 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2571 index = 0;
2572 method_list = decl_list;
2573 list = NULL_TREE;
2574 while (method_list != NULL_TREE)
2576 method = TREE_VALUE (method_list);
2577 list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2578 method_list = TREE_CHAIN (method_list);
2579 index++;
2582 /* Terminate the list with a "null" entry. */
2583 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2584 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2585 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2586 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2587 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2588 TREE_CONSTANT (null_symbol) = 1;
2589 TREE_INVARIANT (null_symbol) = 1;
2590 list = tree_cons (NULL_TREE, null_symbol, list);
2592 /* Put the list in the right order and make it a constructor. */
2593 list = nreverse (list);
2594 table = build_constructor_from_list (symbols_array_type, list);
2596 /* Make it the initial value for otable_syms and emit the decl. */
2597 DECL_INITIAL (the_syms_decl) = table;
2598 DECL_ARTIFICIAL (the_syms_decl) = 1;
2599 DECL_IGNORED_P (the_syms_decl) = 1;
2600 rest_of_decl_compilation (the_syms_decl, 1, 0);
2602 /* Now that its size is known, redefine the table as an
2603 uninitialized static array of INDEX + 1 elements. The extra entry
2604 is used by the runtime to track whether the table has been
2605 initialized. */
2606 table_size
2607 = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2608 the_array_type = build_array_type (the_array_element_type, table_size);
2609 the_table = build_decl (VAR_DECL, name, the_array_type);
2610 TREE_STATIC (the_table) = 1;
2611 TREE_READONLY (the_table) = 1;
2612 rest_of_decl_compilation (the_table, 1, 0);
2614 return the_table;
2617 /* Make an entry for the catch_classes list. */
2618 tree
2619 make_catch_class_record (tree catch_class, tree classname)
2621 tree entry;
2622 tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2623 START_RECORD_CONSTRUCTOR (entry, type);
2624 PUSH_FIELD_VALUE (entry, "address", catch_class);
2625 PUSH_FIELD_VALUE (entry, "classname", classname);
2626 FINISH_RECORD_CONSTRUCTOR (entry);
2627 return entry;
2631 /* Generate the list of Throwable classes that are caught by exception
2632 handlers in this class. */
2633 tree
2634 emit_catch_table (tree this_class)
2636 tree table, table_size, array_type;
2637 TYPE_CATCH_CLASSES (this_class) =
2638 tree_cons (NULL,
2639 make_catch_class_record (null_pointer_node, null_pointer_node),
2640 TYPE_CATCH_CLASSES (this_class));
2641 TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2642 TYPE_CATCH_CLASSES (this_class) =
2643 tree_cons (NULL,
2644 make_catch_class_record (null_pointer_node, null_pointer_node),
2645 TYPE_CATCH_CLASSES (this_class));
2646 table_size = build_index_type
2647 (build_int_cst (NULL_TREE,
2648 list_length (TYPE_CATCH_CLASSES (this_class))));
2649 array_type
2650 = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2651 table_size);
2652 table =
2653 build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2654 DECL_INITIAL (table) =
2655 build_constructor_from_list (array_type, TYPE_CATCH_CLASSES (this_class));
2656 TREE_STATIC (table) = 1;
2657 TREE_READONLY (table) = 1;
2658 DECL_IGNORED_P (table) = 1;
2659 rest_of_decl_compilation (table, 1, 0);
2660 return table;
2663 /* Given a type, return the signature used by
2664 _Jv_FindClassFromSignature() in libgcj. This isn't exactly the
2665 same as build_java_signature() because we want the canonical array
2666 type. */
2668 static tree
2669 build_signature_for_libgcj (tree type)
2671 tree sig, ref;
2673 sig = build_java_signature (type);
2674 ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
2675 IDENTIFIER_LENGTH (sig)));
2676 return ref;
2679 /* Add an entry to the type assertion table. Callback used during hashtable
2680 traversal. */
2682 static int
2683 add_assertion_table_entry (void **htab_entry, void *ptr)
2685 tree entry;
2686 tree code_val, op1_utf8, op2_utf8;
2687 tree *list = (tree *) ptr;
2688 type_assertion *as = (type_assertion *) *htab_entry;
2690 code_val = build_int_cst (NULL_TREE, as->assertion_code);
2692 if (as->op1 == NULL_TREE)
2693 op1_utf8 = null_pointer_node;
2694 else
2695 op1_utf8 = build_signature_for_libgcj (as->op1);
2697 if (as->op2 == NULL_TREE)
2698 op2_utf8 = null_pointer_node;
2699 else
2700 op2_utf8 = build_signature_for_libgcj (as->op2);
2702 START_RECORD_CONSTRUCTOR (entry, assertion_entry_type);
2703 PUSH_FIELD_VALUE (entry, "assertion_code", code_val);
2704 PUSH_FIELD_VALUE (entry, "op1", op1_utf8);
2705 PUSH_FIELD_VALUE (entry, "op2", op2_utf8);
2706 FINISH_RECORD_CONSTRUCTOR (entry);
2708 *list = tree_cons (NULL_TREE, entry, *list);
2709 return true;
2712 /* Generate the type assertion table for CLASS, and return its DECL. */
2714 static tree
2715 emit_assertion_table (tree class)
2717 tree null_entry, ctor, table_decl;
2718 tree list = NULL_TREE;
2719 htab_t assertions_htab = TYPE_ASSERTIONS (class);
2721 /* Iterate through the hash table. */
2722 htab_traverse (assertions_htab, add_assertion_table_entry, &list);
2724 /* Finish with a null entry. */
2725 START_RECORD_CONSTRUCTOR (null_entry, assertion_entry_type);
2726 PUSH_FIELD_VALUE (null_entry, "assertion_code", integer_zero_node);
2727 PUSH_FIELD_VALUE (null_entry, "op1", null_pointer_node);
2728 PUSH_FIELD_VALUE (null_entry, "op2", null_pointer_node);
2729 FINISH_RECORD_CONSTRUCTOR (null_entry);
2731 list = tree_cons (NULL_TREE, null_entry, list);
2733 /* Put the list in the right order and make it a constructor. */
2734 list = nreverse (list);
2735 ctor = build_constructor_from_list (assertion_table_type, list);
2737 table_decl = build_decl (VAR_DECL, mangled_classname ("_type_assert_", class),
2738 assertion_table_type);
2740 TREE_STATIC (table_decl) = 1;
2741 TREE_READONLY (table_decl) = 1;
2742 TREE_CONSTANT (table_decl) = 1;
2743 DECL_IGNORED_P (table_decl) = 1;
2745 DECL_INITIAL (table_decl) = ctor;
2746 DECL_ARTIFICIAL (table_decl) = 1;
2747 rest_of_decl_compilation (table_decl, 1, 0);
2749 return table_decl;
2752 void
2753 init_class_processing (void)
2755 fields_ident = get_identifier ("fields");
2756 info_ident = get_identifier ("info");
2758 gcc_obstack_init (&temporary_obstack);
2761 static hashval_t java_treetreehash_hash (const void *);
2762 static int java_treetreehash_compare (const void *, const void *);
2764 /* A hash table mapping trees to trees. Used generally. */
2766 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2768 static hashval_t
2769 java_treetreehash_hash (const void *k_p)
2771 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2772 return JAVA_TREEHASHHASH_H (k->key);
2775 static int
2776 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2778 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2779 tree k2 = (tree) k2_p;
2780 return (k1->key == k2);
2783 tree
2784 java_treetreehash_find (htab_t ht, tree t)
2786 struct treetreehash_entry *e;
2787 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2788 e = htab_find_with_hash (ht, t, hv);
2789 if (e == NULL)
2790 return NULL;
2791 else
2792 return e->value;
2795 tree *
2796 java_treetreehash_new (htab_t ht, tree t)
2798 void **e;
2799 struct treetreehash_entry *tthe;
2800 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2802 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2803 if (*e == NULL)
2805 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2806 tthe->key = t;
2807 *e = tthe;
2809 else
2810 tthe = (struct treetreehash_entry *) *e;
2811 return &tthe->value;
2814 htab_t
2815 java_treetreehash_create (size_t size, int gc)
2817 if (gc)
2818 return htab_create_ggc (size, java_treetreehash_hash,
2819 java_treetreehash_compare, NULL);
2820 else
2821 return htab_create_alloc (size, java_treetreehash_hash,
2822 java_treetreehash_compare, free, xcalloc, free);
2825 /* Break down qualified IDENTIFIER into package and class-name components.
2826 For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
2827 "pkg.foo", and RIGHT to "Bar". */
2830 split_qualified_name (tree *left, tree *right, tree source)
2832 char *p, *base;
2833 int l = IDENTIFIER_LENGTH (source);
2835 base = alloca (l + 1);
2836 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
2838 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
2839 p = base + l - 1;
2840 while (*p != '.' && p != base)
2841 p--;
2843 /* We didn't find a '.'. Return an error. */
2844 if (p == base)
2845 return 1;
2847 *p = '\0';
2848 if (right)
2849 *right = get_identifier (p+1);
2850 *left = get_identifier (base);
2852 return 0;
2855 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
2856 if the classes are from the same package. */
2859 in_same_package (tree name1, tree name2)
2861 tree tmp;
2862 tree pkg1;
2863 tree pkg2;
2865 if (TREE_CODE (name1) == TYPE_DECL)
2866 name1 = DECL_NAME (name1);
2867 if (TREE_CODE (name2) == TYPE_DECL)
2868 name2 = DECL_NAME (name2);
2870 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
2871 /* One in empty package. */
2872 return 0;
2874 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
2875 /* Both in empty package. */
2876 return 1;
2878 split_qualified_name (&pkg1, &tmp, name1);
2879 split_qualified_name (&pkg2, &tmp, name2);
2881 return (pkg1 == pkg2);
2884 #include "gt-java-class.h"