Mark ChangeLog
[official-gcc.git] / gcc / java / class.c
blob5615eaba714129b6deb70ecbee57bd53175afdce
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"
49 /* DOS brain-damage */
50 #ifndef O_BINARY
51 #define O_BINARY 0 /* MS-DOS brain-damage */
52 #endif
54 static tree make_method_value (tree);
55 static tree build_java_method_type (tree, tree, int);
56 static int32 hashUtf8String (const char *, int);
57 static tree make_field_value (tree);
58 static tree get_dispatch_vector (tree);
59 static tree get_dispatch_table (tree, tree);
60 static int supers_all_compiled (tree type);
61 static tree maybe_layout_super_class (tree, tree);
62 static void add_miranda_methods (tree, tree);
63 static int assume_compiled (const char *);
64 static tree build_symbol_entry (tree);
65 static tree emit_assertion_table (tree);
67 struct obstack temporary_obstack;
69 /* The compiler generates different code depending on whether or not
70 it can assume certain classes have been compiled down to native
71 code or not. The compiler options -fassume-compiled= and
72 -fno-assume-compiled= are used to create a tree of
73 class_flag_node objects. This tree is queried to determine if
74 a class is assume to be compiled or not. Each node in the tree
75 represents either a package or a specific class. */
77 typedef struct class_flag_node_struct
79 /* The class or package name. */
80 const char *ident;
82 /* Nonzero if this represents an exclusion. */
83 int value;
85 /* Pointers to other nodes in the tree. */
86 struct class_flag_node_struct *parent;
87 struct class_flag_node_struct *sibling;
88 struct class_flag_node_struct *child;
89 } class_flag_node;
91 static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
92 static void add_class_flag (class_flag_node **, const char *, int);
94 /* This is the root of the include/exclude tree. */
96 static class_flag_node *assume_compiled_tree;
98 static class_flag_node *enable_assert_tree;
100 static GTY(()) tree class_roots[5];
101 #define registered_class class_roots[0]
102 #define fields_ident class_roots[1] /* get_identifier ("fields") */
103 #define info_ident class_roots[2] /* get_identifier ("info") */
104 #define class_list class_roots[3]
105 #define class_dtable_decl class_roots[4]
107 /* Return the node that most closely represents the class whose name
108 is IDENT. Start the search from NODE (followed by its siblings).
109 Return NULL if an appropriate node does not exist. */
111 static class_flag_node *
112 find_class_flag_node (class_flag_node *node, const char *ident)
114 while (node)
116 size_t node_ident_length = strlen (node->ident);
118 /* node_ident_length is zero at the root of the tree. If the
119 identifiers are the same length, then we have matching
120 classes. Otherwise check if we've matched an enclosing
121 package name. */
123 if (node_ident_length == 0
124 || (strncmp (ident, node->ident, node_ident_length) == 0
125 && (ident[node_ident_length] == '\0'
126 || ident[node_ident_length] == '.')))
128 /* We've found a match, however, there might be a more
129 specific match. */
131 class_flag_node *found = find_class_flag_node (node->child, ident);
132 if (found)
133 return found;
134 else
135 return node;
138 /* No match yet. Continue through the sibling list. */
139 node = node->sibling;
142 /* No match at all in this tree. */
143 return NULL;
146 void
147 add_class_flag (class_flag_node **rootp, const char *ident, int value)
149 class_flag_node *root = *rootp;
150 class_flag_node *parent, *node;
152 /* Create the root of the tree if it doesn't exist yet. */
154 if (NULL == root)
156 root = xmalloc (sizeof (class_flag_node));
157 root->ident = "";
158 root->value = 0;
159 root->sibling = NULL;
160 root->child = NULL;
161 root->parent = NULL;
162 *rootp = root;
165 /* Calling the function with the empty string means we're setting
166 value for the root of the hierarchy. */
168 if (0 == ident[0])
170 root->value = value;
171 return;
174 /* Find the parent node for this new node. PARENT will either be a
175 class or a package name. Adjust PARENT accordingly. */
177 parent = find_class_flag_node (root, ident);
178 if (strcmp (ident, parent->ident) == 0)
179 parent->value = value;
180 else
182 /* Insert new node into the tree. */
183 node = xmalloc (sizeof (class_flag_node));
185 node->ident = xstrdup (ident);
186 node->value = value;
187 node->child = NULL;
189 node->parent = parent;
190 node->sibling = parent->child;
191 parent->child = node;
195 /* Add a new IDENT to the include/exclude tree. It's an exclusion
196 if EXCLUDEP is nonzero. */
198 void
199 add_assume_compiled (const char *ident, int excludep)
201 add_class_flag (&assume_compiled_tree, ident, excludep);
204 /* The default value returned by enable_assertions. */
206 #define DEFAULT_ENABLE_ASSERT (flag_emit_class_files || optimize == 0)
208 /* Enter IDENT (a class or package name) into the enable-assertions table.
209 VALUE is true to enable and false to disable. */
211 void
212 add_enable_assert (const char *ident, int value)
214 if (enable_assert_tree == NULL)
215 add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
216 add_class_flag (&enable_assert_tree, ident, value);
219 /* Returns nonzero if IDENT is the name of a class that the compiler
220 should assume has been compiled to object code. */
222 static int
223 assume_compiled (const char *ident)
225 class_flag_node *i;
226 int result;
228 if (NULL == assume_compiled_tree)
229 return 1;
231 i = find_class_flag_node (assume_compiled_tree, ident);
233 result = ! i->value;
235 return (result);
238 /* Return true if we should generate code to check assertions within KLASS. */
240 bool
241 enable_assertions (tree klass)
243 /* Check if command-line specifies whether we should check assertions. */
245 if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
247 const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
248 class_flag_node *node
249 = find_class_flag_node (enable_assert_tree, ident);
250 return node->value;
253 /* The default is to enable assertions if generating class files,
254 or not optimizing. */
255 return DEFAULT_ENABLE_ASSERT;
258 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
259 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
260 Also, PREFIX is prepended, and SUFFIX is appended. */
262 tree
263 ident_subst (const char* old_name,
264 int old_length,
265 const char *prefix,
266 int old_char,
267 int new_char,
268 const char *suffix)
270 int prefix_len = strlen (prefix);
271 int suffix_len = strlen (suffix);
272 int i = prefix_len + old_length + suffix_len + 1;
273 char *buffer = alloca (i);
275 strcpy (buffer, prefix);
276 for (i = 0; i < old_length; i++)
278 char ch = old_name[i];
279 if (ch == old_char)
280 ch = new_char;
281 buffer[prefix_len + i] = ch;
283 strcpy (buffer + prefix_len + old_length, suffix);
284 return get_identifier (buffer);
287 /* Return an IDENTIFIER_NODE the same as OLD_ID,
288 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
289 Also, PREFIX is prepended, and SUFFIX is appended. */
291 tree
292 identifier_subst (const tree old_id,
293 const char *prefix,
294 int old_char,
295 int new_char,
296 const char *suffix)
298 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
299 prefix, old_char, new_char, suffix);
302 /* Generate a valid C identifier from the name of the class TYPE,
303 prefixed by PREFIX. */
305 tree
306 mangled_classname (const char *prefix, tree type)
308 tree ident = TYPE_NAME (type);
309 if (TREE_CODE (ident) != IDENTIFIER_NODE)
310 ident = DECL_NAME (ident);
311 return identifier_subst (ident, prefix, '.', '_', "");
314 tree
315 make_class (void)
317 tree type;
318 type = make_node (RECORD_TYPE);
319 /* Unfortunately we must create the binfo here, so that class
320 loading works. */
321 TYPE_BINFO (type) = make_tree_binfo (0);
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);
429 /* dbxout needs a DECL_SIZE if in gstabs mode */
430 DECL_SIZE (decl) = integer_zero_node;
432 input_location = saved_loc;
433 signature = identifier_subst (class_name, "L", '.', '/', ";");
434 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
436 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
437 both a typedef and in the struct name-space. We may want to re-visit
438 this later, but for now it reduces the changes needed for gdb. */
439 DECL_ARTIFICIAL (decl) = 1;
441 pushdecl_top_level (decl);
443 return decl;
446 /* Finds the (global) class named NAME. Creates the class if not found.
447 Also creates associated TYPE_DECL.
448 Does not check if the class actually exists, load the class,
449 fill in field or methods, or do layout_type. */
451 tree
452 lookup_class (tree name)
454 tree decl = IDENTIFIER_CLASS_VALUE (name);
455 if (decl == NULL_TREE)
456 decl = push_class (make_class (), name);
457 return TREE_TYPE (decl);
460 void
461 set_super_info (int access_flags, tree this_class,
462 tree super_class, int interfaces_count)
464 int total_supers = interfaces_count;
465 tree class_decl = TYPE_NAME (this_class);
467 if (super_class)
468 total_supers++;
470 if (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;
553 if (! CLASS_LOADED_P (type1))
554 load_class (type1, 1);
556 type1 = maybe_layout_super_class (CLASSTYPE_SUPER (type1), type1);
558 return 0;
561 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
564 enclosing_context_p (tree type1, tree type2)
566 if (!INNER_CLASS_TYPE_P (type2))
567 return 0;
569 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
570 type2;
571 type2 = (INNER_CLASS_TYPE_P (type2) ?
572 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
574 if (type2 == type1)
575 return 1;
578 return 0;
582 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
583 nesting level. */
586 common_enclosing_context_p (tree type1, tree type2)
588 while (type1)
590 tree current;
591 for (current = type2; current;
592 current = (INNER_CLASS_TYPE_P (current) ?
593 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
594 NULL_TREE))
595 if (type1 == current)
596 return 1;
598 if (INNER_CLASS_TYPE_P (type1))
599 type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
600 else
601 break;
603 return 0;
606 /* Return 1 iff there exists a common enclosing "this" between TYPE1
607 and TYPE2, without crossing any static context. */
610 common_enclosing_instance_p (tree type1, tree type2)
612 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
613 return 0;
615 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
616 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
617 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
619 tree current;
620 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
621 current = (PURE_INNER_CLASS_TYPE_P (current) ?
622 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
623 NULL_TREE))
624 if (type1 == current)
625 return 1;
627 return 0;
630 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
631 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
632 if attempt is made to add it twice. */
634 tree
635 maybe_add_interface (tree this_class, tree interface_class)
637 tree binfo, base_binfo;
638 int i;
640 for (binfo = TYPE_BINFO (this_class), i = 0;
641 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
642 if (BINFO_TYPE (base_binfo) == interface_class)
643 return interface_class;
644 add_interface (this_class, interface_class);
645 return NULL_TREE;
648 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
650 void
651 add_interface (tree this_class, tree interface_class)
653 tree interface_binfo = make_tree_binfo (0);
655 BINFO_TYPE (interface_binfo) = interface_class;
656 BINFO_OFFSET (interface_binfo) = integer_zero_node;
657 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
658 BINFO_VIRTUAL_P (interface_binfo) = 1;
660 BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
663 #if 0
664 /* Return the address of a pointer to the first FUNCTION_DECL
665 in the list (*LIST) whose DECL_NAME is NAME. */
667 static tree *
668 find_named_method (tree *list, tree name)
670 while (*list && DECL_NAME (*list) != name)
671 list = &TREE_CHAIN (*list);
672 return list;
674 #endif
676 static tree
677 build_java_method_type (tree fntype, tree this_class, int access_flags)
679 if (access_flags & ACC_STATIC)
680 return fntype;
681 return build_method_type (this_class, fntype);
684 tree
685 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
687 tree method_type, fndecl;
689 method_type = build_java_method_type (function_type,
690 this_class, access_flags);
692 fndecl = build_decl (FUNCTION_DECL, name, method_type);
693 DECL_CONTEXT (fndecl) = this_class;
695 DECL_LANG_SPECIFIC (fndecl)
696 = ggc_alloc_cleared (sizeof (struct lang_decl));
697 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
699 /* Initialize the static initializer test table. */
701 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
702 java_treetreehash_create (10, 1);
704 /* Initialize the initialized (static) class table. */
705 if (access_flags & ACC_STATIC)
706 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
707 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
709 /* Initialize the static method invocation compound list */
710 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
712 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
713 TYPE_METHODS (this_class) = fndecl;
715 /* Notice that this is a finalizer and update the class type
716 accordingly. This is used to optimize instance allocation. */
717 if (name == finalize_identifier_node
718 && TREE_TYPE (function_type) == void_type_node
719 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
720 HAS_FINALIZER_P (this_class) = 1;
722 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
723 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
724 if (access_flags & ACC_PRIVATE)
725 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
726 if (access_flags & ACC_NATIVE)
728 METHOD_NATIVE (fndecl) = 1;
729 DECL_EXTERNAL (fndecl) = 1;
731 if (access_flags & ACC_STATIC)
732 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
733 if (access_flags & ACC_FINAL)
734 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
735 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
736 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
737 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
738 return fndecl;
741 /* Add a method to THIS_CLASS.
742 The method's name is NAME.
743 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
745 tree
746 add_method (tree this_class, int access_flags, tree name, tree method_sig)
748 tree function_type, fndecl;
749 const unsigned char *sig
750 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
752 if (sig[0] != '(')
753 fatal_error ("bad method signature");
755 function_type = get_type_from_signature (method_sig);
756 fndecl = add_method_1 (this_class, access_flags, name, function_type);
757 set_java_signature (TREE_TYPE (fndecl), method_sig);
758 return fndecl;
761 tree
762 add_field (tree class, tree name, tree field_type, int flags)
764 int is_static = (flags & ACC_STATIC) != 0;
765 tree field;
766 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
767 TREE_CHAIN (field) = TYPE_FIELDS (class);
768 TYPE_FIELDS (class) = field;
769 DECL_CONTEXT (field) = class;
771 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
772 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
773 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
774 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
775 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
776 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
777 if (is_static)
779 FIELD_STATIC (field) = 1;
780 /* Always make field externally visible. This is required so
781 that native methods can always access the field. */
782 TREE_PUBLIC (field) = 1;
783 /* Considered external until we know what classes are being
784 compiled into this object file. */
785 DECL_EXTERNAL (field) = 1;
788 return field;
791 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
793 void
794 set_constant_value (tree field, tree constant)
796 if (field == NULL_TREE)
797 warning ("misplaced ConstantValue attribute (not in any field)");
798 else if (DECL_INITIAL (field) != NULL_TREE)
799 warning ("duplicate ConstantValue attribute for field '%s'",
800 IDENTIFIER_POINTER (DECL_NAME (field)));
801 else
803 DECL_INITIAL (field) = constant;
804 if (TREE_TYPE (constant) != TREE_TYPE (field)
805 && ! (TREE_TYPE (constant) == int_type_node
806 && INTEGRAL_TYPE_P (TREE_TYPE (field))
807 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
808 && ! (TREE_TYPE (constant) == utf8const_ptr_type
809 && TREE_TYPE (field) == string_ptr_type_node))
810 error ("ConstantValue attribute of field '%s' has wrong type",
811 IDENTIFIER_POINTER (DECL_NAME (field)));
812 if (FIELD_FINAL (field))
813 DECL_FIELD_FINAL_IUD (field) = 1;
817 /* Count the number of Unicode chars encoded in a given Ut8 string. */
819 #if 0
821 strLengthUtf8 (char *str, int len)
823 register unsigned char* ptr = (unsigned char*) str;
824 register unsigned char *limit = ptr + len;
825 int str_length = 0;
826 for (; ptr < limit; str_length++) {
827 if (UTF8_GET (ptr, limit) < 0)
828 return -1;
830 return str_length;
832 #endif
835 /* Calculate a hash value for a string encoded in Utf8 format.
836 * This returns the same hash value as specified for java.lang.String.hashCode.
839 static int32
840 hashUtf8String (const char *str, int len)
842 const unsigned char* ptr = (const unsigned char*) str;
843 const unsigned char *limit = ptr + len;
844 int32 hash = 0;
845 for (; ptr < limit;)
847 int ch = UTF8_GET (ptr, limit);
848 /* Updated specification from
849 http://www.javasoft.com/docs/books/jls/clarify.html. */
850 hash = (31 * hash) + ch;
852 return hash;
855 static GTY(()) tree utf8_decl_list = NULL_TREE;
857 tree
858 build_utf8_ref (tree name)
860 const char * name_ptr = IDENTIFIER_POINTER(name);
861 int name_len = IDENTIFIER_LENGTH(name);
862 char buf[60];
863 tree ctype, field = NULL_TREE, str_type, cinit, string;
864 static int utf8_count = 0;
865 int name_hash;
866 tree ref = IDENTIFIER_UTF8_REF (name);
867 tree decl;
868 if (ref != NULL_TREE)
869 return ref;
871 ctype = make_node (RECORD_TYPE);
872 str_type = build_prim_array_type (unsigned_byte_type_node,
873 name_len + 1); /* Allow for final '\0'. */
874 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
875 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
876 PUSH_FIELD (ctype, field, "data", str_type);
877 FINISH_RECORD (ctype);
878 START_RECORD_CONSTRUCTOR (cinit, ctype);
879 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
880 PUSH_FIELD_VALUE (cinit, "hash", build_int_cst (NULL_TREE, name_hash));
881 PUSH_FIELD_VALUE (cinit, "length", build_int_cst (NULL_TREE, name_len));
882 string = build_string (name_len, name_ptr);
883 TREE_TYPE (string) = str_type;
884 PUSH_FIELD_VALUE (cinit, "data", string);
885 FINISH_RECORD_CONSTRUCTOR (cinit);
886 TREE_CONSTANT (cinit) = 1;
887 TREE_INVARIANT (cinit) = 1;
889 /* Generate a unique-enough identifier. */
890 sprintf(buf, "_Utf%d", ++utf8_count);
892 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
893 TREE_STATIC (decl) = 1;
894 DECL_ARTIFICIAL (decl) = 1;
895 DECL_IGNORED_P (decl) = 1;
896 TREE_READONLY (decl) = 1;
897 TREE_THIS_VOLATILE (decl) = 0;
898 DECL_INITIAL (decl) = cinit;
900 if (HAVE_GAS_SHF_MERGE)
902 int decl_size;
903 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
904 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
905 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
906 if (flag_merge_constants && decl_size < 256)
908 char buf[32];
909 int flags = (SECTION_OVERRIDE
910 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
911 sprintf (buf, ".rodata.jutf8.%d", decl_size);
912 named_section_flags (buf, flags);
913 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
917 TREE_CHAIN (decl) = utf8_decl_list;
918 layout_decl (decl, 0);
919 pushdecl (decl);
920 rest_of_decl_compilation (decl, global_bindings_p (), 0);
921 utf8_decl_list = decl;
922 make_decl_rtl (decl);
923 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
924 IDENTIFIER_UTF8_REF (name) = ref;
925 return ref;
928 /* Like build_class_ref, but instead of a direct reference generate a
929 pointer into the constant pool. */
931 static tree
932 build_indirect_class_ref (tree type)
934 int index;
935 tree cl;
936 index = alloc_class_constant (type);
937 cl = build_ref_from_constant_pool (index);
938 return convert (promote_type (class_ptr_type), cl);
941 /* Build a reference to the class TYPE.
942 Also handles primitive types and array types. */
944 tree
945 build_class_ref (tree type)
947 int is_compiled = is_compiled_class (type);
948 if (is_compiled)
950 tree ref, decl_name, decl;
951 if (TREE_CODE (type) == POINTER_TYPE)
952 type = TREE_TYPE (type);
954 if (flag_indirect_dispatch
955 && type != output_class
956 && TREE_CODE (type) == RECORD_TYPE)
957 return build_indirect_class_ref (type);
959 if (TREE_CODE (type) == RECORD_TYPE)
961 if (TYPE_SIZE (type) == error_mark_node)
962 return null_pointer_node;
963 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
964 "", '/', '/', ".class");
965 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
966 if (decl == NULL_TREE)
968 decl = build_decl (VAR_DECL, decl_name, class_type_node);
969 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
970 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
971 TREE_STATIC (decl) = 1;
972 TREE_PUBLIC (decl) = 1;
973 DECL_IGNORED_P (decl) = 1;
974 DECL_ARTIFICIAL (decl) = 1;
975 if (is_compiled == 1)
976 DECL_EXTERNAL (decl) = 1;
977 SET_DECL_ASSEMBLER_NAME (decl,
978 java_mangle_class_field
979 (&temporary_obstack, type));
980 make_decl_rtl (decl);
981 pushdecl_top_level (decl);
984 else
986 const char *name;
987 char buffer[25];
988 if (flag_emit_class_files)
990 const char *prim_class_name;
991 tree prim_class;
992 if (type == char_type_node)
993 prim_class_name = "java.lang.Character";
994 else if (type == boolean_type_node)
995 prim_class_name = "java.lang.Boolean";
996 else if (type == byte_type_node)
997 prim_class_name = "java.lang.Byte";
998 else if (type == short_type_node)
999 prim_class_name = "java.lang.Short";
1000 else if (type == int_type_node)
1001 prim_class_name = "java.lang.Integer";
1002 else if (type == long_type_node)
1003 prim_class_name = "java.lang.Long";
1004 else if (type == float_type_node)
1005 prim_class_name = "java.lang.Float";
1006 else if (type == double_type_node)
1007 prim_class_name = "java.lang.Double";
1008 else if (type == void_type_node)
1009 prim_class_name = "java.lang.Void";
1010 else
1011 abort ();
1013 prim_class = lookup_class (get_identifier (prim_class_name));
1014 return build3 (COMPONENT_REF, NULL_TREE,
1015 prim_class, TYPE_identifier_node, NULL_TREE);
1017 decl_name = TYPE_NAME (type);
1018 if (TREE_CODE (decl_name) == TYPE_DECL)
1019 decl_name = DECL_NAME (decl_name);
1020 name = IDENTIFIER_POINTER (decl_name);
1021 if (strncmp (name, "promoted_", 9) == 0)
1022 name += 9;
1023 sprintf (buffer, "_Jv_%sClass", name);
1024 decl_name = get_identifier (buffer);
1025 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1026 if (decl == NULL_TREE)
1028 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1029 TREE_STATIC (decl) = 1;
1030 TREE_PUBLIC (decl) = 1;
1031 DECL_EXTERNAL (decl) = 1;
1032 DECL_ARTIFICIAL (decl) = 1;
1033 make_decl_rtl (decl);
1034 pushdecl_top_level (decl);
1038 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1039 return ref;
1041 else
1042 return build_indirect_class_ref (type);
1045 /* Create a local statically allocated variable that will hold a
1046 pointer to a static field. */
1048 static tree
1049 build_fieldref_cache_entry (int index, tree fdecl ATTRIBUTE_UNUSED)
1051 tree decl, decl_name;
1052 const char *name = IDENTIFIER_POINTER (mangled_classname ("_cpool_", output_class));
1053 char *buf = alloca (strlen (name) + 20);
1054 sprintf (buf, "%s_%d_ref", name, index);
1055 decl_name = get_identifier (buf);
1056 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1057 if (decl == NULL_TREE)
1059 decl = build_decl (VAR_DECL, decl_name, ptr_type_node);
1060 TREE_STATIC (decl) = 1;
1061 TREE_PUBLIC (decl) = 0;
1062 DECL_EXTERNAL (decl) = 0;
1063 DECL_ARTIFICIAL (decl) = 1;
1064 make_decl_rtl (decl);
1065 pushdecl_top_level (decl);
1067 return decl;
1070 tree
1071 build_static_field_ref (tree fdecl)
1073 tree fclass = DECL_CONTEXT (fdecl);
1074 int is_compiled = is_compiled_class (fclass);
1075 int from_class = ! CLASS_FROM_SOURCE_P (current_class);
1077 /* Allow static final fields to fold to a constant. When using
1078 -findirect-dispatch, we simply never do this folding if compiling
1079 from .class; in the .class file constants will be referred to via
1080 the constant pool. */
1081 if ((!flag_indirect_dispatch || !from_class)
1082 && (is_compiled
1083 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1084 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1085 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1086 && TREE_CONSTANT (DECL_INITIAL (fdecl)))))
1088 if (!DECL_RTL_SET_P (fdecl))
1090 if (is_compiled == 1)
1091 DECL_EXTERNAL (fdecl) = 1;
1092 make_decl_rtl (fdecl);
1095 else
1097 /* Generate a CONSTANT_FieldRef for FDECL in the constant pool
1098 and a class local static variable CACHE_ENTRY, then
1100 *(fdecl **)((__builtin_expect (cache_entry == null, false))
1101 ? cache_entry = _Jv_ResolvePoolEntry (output_class, cpool_index)
1102 : cache_entry)
1104 This can mostly be optimized away, so that the usual path is a
1105 load followed by a test and branch. _Jv_ResolvePoolEntry is
1106 only called once for each constant pool entry.
1108 There is an optimization that we don't do: at the start of a
1109 method, create a local copy of CACHE_ENTRY and use that instead.
1113 int cpool_index = alloc_constant_fieldref (output_class, fdecl);
1114 tree cache_entry = build_fieldref_cache_entry (cpool_index, fdecl);
1115 tree test
1116 = build3 (CALL_EXPR, boolean_type_node,
1117 build_address_of (built_in_decls[BUILT_IN_EXPECT]),
1118 tree_cons (NULL_TREE, build2 (EQ_EXPR, boolean_type_node,
1119 cache_entry, null_pointer_node),
1120 build_tree_list (NULL_TREE, boolean_false_node)),
1121 NULL_TREE);
1122 tree cpool_index_cst = build_int_cst (NULL_TREE, cpool_index);
1123 tree init
1124 = build3 (CALL_EXPR, ptr_type_node,
1125 build_address_of (soft_resolvepoolentry_node),
1126 tree_cons (NULL_TREE, build_class_ref (output_class),
1127 build_tree_list (NULL_TREE, cpool_index_cst)),
1128 NULL_TREE);
1129 init = build2 (MODIFY_EXPR, ptr_type_node, cache_entry, init);
1130 init = build3 (COND_EXPR, ptr_type_node, test, init, cache_entry);
1131 init = fold_convert (build_pointer_type (TREE_TYPE (fdecl)), init);
1132 fdecl = build1 (INDIRECT_REF, TREE_TYPE (fdecl), init);
1134 return fdecl;
1138 get_access_flags_from_decl (tree decl)
1140 int access_flags = 0;
1141 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1143 if (FIELD_STATIC (decl))
1144 access_flags |= ACC_STATIC;
1145 if (FIELD_PUBLIC (decl))
1146 access_flags |= ACC_PUBLIC;
1147 if (FIELD_PROTECTED (decl))
1148 access_flags |= ACC_PROTECTED;
1149 if (FIELD_PRIVATE (decl))
1150 access_flags |= ACC_PRIVATE;
1151 if (FIELD_FINAL (decl))
1152 access_flags |= ACC_FINAL;
1153 if (FIELD_VOLATILE (decl))
1154 access_flags |= ACC_VOLATILE;
1155 if (FIELD_TRANSIENT (decl))
1156 access_flags |= ACC_TRANSIENT;
1157 return access_flags;
1159 if (TREE_CODE (decl) == TYPE_DECL)
1161 if (CLASS_PUBLIC (decl))
1162 access_flags |= ACC_PUBLIC;
1163 if (CLASS_FINAL (decl))
1164 access_flags |= ACC_FINAL;
1165 if (CLASS_SUPER (decl))
1166 access_flags |= ACC_SUPER;
1167 if (CLASS_INTERFACE (decl))
1168 access_flags |= ACC_INTERFACE;
1169 if (CLASS_ABSTRACT (decl))
1170 access_flags |= ACC_ABSTRACT;
1171 if (CLASS_STATIC (decl))
1172 access_flags |= ACC_STATIC;
1173 if (CLASS_PRIVATE (decl))
1174 access_flags |= ACC_PRIVATE;
1175 if (CLASS_PROTECTED (decl))
1176 access_flags |= ACC_PROTECTED;
1177 if (CLASS_STRICTFP (decl))
1178 access_flags |= ACC_STRICT;
1179 return access_flags;
1181 if (TREE_CODE (decl) == FUNCTION_DECL)
1183 if (METHOD_PUBLIC (decl))
1184 access_flags |= ACC_PUBLIC;
1185 if (METHOD_PRIVATE (decl))
1186 access_flags |= ACC_PRIVATE;
1187 if (METHOD_PROTECTED (decl))
1188 access_flags |= ACC_PROTECTED;
1189 if (METHOD_STATIC (decl))
1190 access_flags |= ACC_STATIC;
1191 if (METHOD_FINAL (decl))
1192 access_flags |= ACC_FINAL;
1193 if (METHOD_SYNCHRONIZED (decl))
1194 access_flags |= ACC_SYNCHRONIZED;
1195 if (METHOD_NATIVE (decl))
1196 access_flags |= ACC_NATIVE;
1197 if (METHOD_ABSTRACT (decl))
1198 access_flags |= ACC_ABSTRACT;
1199 if (METHOD_STRICTFP (decl))
1200 access_flags |= ACC_STRICT;
1201 if (METHOD_INVISIBLE (decl))
1202 access_flags |= ACC_INVISIBLE;
1203 return access_flags;
1205 abort ();
1208 static GTY (()) int alias_labelno = 0;
1210 /* Create a private alias for METHOD. Using this alias instead of the method
1211 decl ensures that ncode entries in the method table point to the real function
1212 at runtime, not a PLT entry. */
1214 static tree
1215 make_local_function_alias (tree method)
1217 #ifdef ASM_OUTPUT_DEF
1218 tree alias;
1220 const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1221 char *name = alloca (strlen (method_name) + 2);
1222 char *buf = alloca (strlen (method_name) + 128);
1224 /* Only create aliases for local functions. */
1225 if (DECL_EXTERNAL (method))
1226 return method;
1228 /* Prefix method_name with 'L' for the alias label. */
1229 *name = 'L';
1230 strcpy (name + 1, method_name);
1232 ASM_GENERATE_INTERNAL_LABEL (buf, name, alias_labelno++);
1233 alias = build_decl (FUNCTION_DECL, get_identifier (buf),
1234 TREE_TYPE (method));
1235 DECL_CONTEXT (alias) = NULL;
1236 TREE_READONLY (alias) = TREE_READONLY (method);
1237 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1238 TREE_PUBLIC (alias) = 0;
1239 DECL_EXTERNAL (alias) = 0;
1240 DECL_ARTIFICIAL (alias) = 1;
1241 DECL_INLINE (alias) = 0;
1242 DECL_INITIAL (alias) = error_mark_node;
1243 TREE_ADDRESSABLE (alias) = 1;
1244 TREE_USED (alias) = 1;
1245 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
1246 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
1247 if (!flag_syntax_only)
1248 assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1249 return alias;
1250 #else
1251 return method;
1252 #endif
1255 /** Make reflection data (_Jv_Field) for field FDECL. */
1257 static tree
1258 make_field_value (tree fdecl)
1260 tree finit;
1261 int flags;
1262 tree type = TREE_TYPE (fdecl);
1263 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1265 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1266 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1267 if (resolved)
1268 type = build_class_ref (type);
1269 else
1271 tree signature = build_java_signature (type);
1273 type = build_utf8_ref (unmangle_classname
1274 (IDENTIFIER_POINTER (signature),
1275 IDENTIFIER_LENGTH (signature)));
1277 PUSH_FIELD_VALUE (finit, "type", type);
1279 flags = get_access_flags_from_decl (fdecl);
1280 if (! resolved)
1281 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1283 PUSH_FIELD_VALUE (finit, "accflags", build_int_cst (NULL_TREE, flags));
1284 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1286 PUSH_FIELD_VALUE
1287 (finit, "info",
1288 build_constructor (field_info_union_node,
1289 build_tree_list
1290 ((FIELD_STATIC (fdecl)
1291 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1292 : TYPE_FIELDS (field_info_union_node)),
1293 (FIELD_STATIC (fdecl)
1294 ? build_address_of (fdecl)
1295 : byte_position (fdecl)))));
1297 FINISH_RECORD_CONSTRUCTOR (finit);
1298 return finit;
1301 /** Make reflection data (_Jv_Method) for method MDECL. */
1303 static tree
1304 make_method_value (tree mdecl)
1306 static int method_name_count = 0;
1307 tree minit;
1308 tree index;
1309 tree code;
1310 tree class_decl;
1311 #define ACC_TRANSLATED 0x4000
1312 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1314 class_decl = DECL_CONTEXT (mdecl);
1315 /* For interfaces, the index field contains the dispatch index. */
1316 if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1317 index = build_int_cst (NULL_TREE,
1318 get_interface_method_index (mdecl, class_decl));
1319 else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1320 index = get_method_index (mdecl);
1321 else
1322 index = integer_minus_one_node;
1324 code = null_pointer_node;
1325 if (DECL_RTL_SET_P (mdecl))
1326 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1327 make_local_function_alias (mdecl));
1328 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1329 PUSH_FIELD_VALUE (minit, "name",
1330 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1331 init_identifier_node
1332 : DECL_NAME (mdecl)));
1334 tree signature = build_java_signature (TREE_TYPE (mdecl));
1335 PUSH_FIELD_VALUE (minit, "signature",
1336 (build_utf8_ref
1337 (unmangle_classname
1338 (IDENTIFIER_POINTER(signature),
1339 IDENTIFIER_LENGTH(signature)))));
1341 PUSH_FIELD_VALUE (minit, "accflags", build_int_cst (NULL_TREE, accflags));
1342 PUSH_FIELD_VALUE (minit, "index", index);
1343 PUSH_FIELD_VALUE (minit, "ncode", code);
1346 /* Compute the `throws' information for the method. */
1347 tree table = null_pointer_node;
1348 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1350 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1351 tree iter, type, array;
1352 char buf[60];
1354 table = tree_cons (NULL_TREE, table, NULL_TREE);
1355 for (iter = DECL_FUNCTION_THROWS (mdecl);
1356 iter != NULL_TREE;
1357 iter = TREE_CHAIN (iter))
1359 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1360 tree utf8
1361 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1362 IDENTIFIER_LENGTH (sig)));
1363 table = tree_cons (NULL_TREE, utf8, table);
1365 type = build_prim_array_type (ptr_type_node, length);
1366 table = build_constructor (type, table);
1367 /* Compute something unique enough. */
1368 sprintf (buf, "_methods%d", method_name_count++);
1369 array = build_decl (VAR_DECL, get_identifier (buf), type);
1370 DECL_INITIAL (array) = table;
1371 TREE_STATIC (array) = 1;
1372 DECL_ARTIFICIAL (array) = 1;
1373 DECL_IGNORED_P (array) = 1;
1374 rest_of_decl_compilation (array, 1, 0);
1376 table = build1 (ADDR_EXPR, ptr_type_node, array);
1379 PUSH_FIELD_VALUE (minit, "throws", table);
1382 FINISH_RECORD_CONSTRUCTOR (minit);
1383 return minit;
1386 static tree
1387 get_dispatch_vector (tree type)
1389 tree vtable = TYPE_VTABLE (type);
1391 if (vtable == NULL_TREE)
1393 HOST_WIDE_INT i;
1394 tree method;
1395 tree super = CLASSTYPE_SUPER (type);
1396 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1397 vtable = make_tree_vec (nvirtuals);
1398 TYPE_VTABLE (type) = vtable;
1399 if (super != NULL_TREE)
1401 tree super_vtable = get_dispatch_vector (super);
1403 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1404 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1407 for (method = TYPE_METHODS (type); method != NULL_TREE;
1408 method = TREE_CHAIN (method))
1410 tree method_index = get_method_index (method);
1411 if (method_index != NULL_TREE
1412 && host_integerp (method_index, 0))
1413 TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1417 return vtable;
1420 static tree
1421 get_dispatch_table (tree type, tree this_class_addr)
1423 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1424 tree vtable = get_dispatch_vector (type);
1425 int i, j;
1426 tree list = NULL_TREE;
1427 int nvirtuals = TREE_VEC_LENGTH (vtable);
1428 int arraysize;
1429 tree gc_descr;
1431 for (i = nvirtuals; --i >= 0; )
1433 tree method = TREE_VEC_ELT (vtable, i);
1434 if (METHOD_ABSTRACT (method))
1436 if (! abstract_p)
1437 warning ("%Jabstract method in non-abstract class", method);
1439 if (TARGET_VTABLE_USES_DESCRIPTORS)
1440 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1441 list = tree_cons (NULL_TREE, null_pointer_node, list);
1442 else
1443 list = tree_cons (NULL_TREE, null_pointer_node, list);
1445 else
1447 if (!DECL_RTL_SET_P (method))
1448 make_decl_rtl (method);
1450 if (TARGET_VTABLE_USES_DESCRIPTORS)
1451 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1453 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1454 method, build_int_cst (NULL_TREE, j));
1455 TREE_CONSTANT (fdesc) = 1;
1456 TREE_INVARIANT (fdesc) = 1;
1457 list = tree_cons (NULL_TREE, fdesc, list);
1459 else
1460 list = tree_cons (NULL_TREE,
1461 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1462 method),
1463 list);
1467 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1468 using the Boehm GC we sometimes stash a GC type descriptor
1469 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1470 the emitted byte count during the output to the assembly file. */
1471 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1472 fake "function descriptor". It's first word is the is the class
1473 pointer, and subsequent words (usually one) contain the GC descriptor.
1474 In all other cases, we reserve two extra vtable slots. */
1475 gc_descr = get_boehm_type_descriptor (type);
1476 list = tree_cons (NULL_TREE, gc_descr, list);
1477 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1478 list = tree_cons (NULL_TREE, gc_descr, list);
1479 list = tree_cons (NULL_TREE, this_class_addr, list);
1481 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1482 list = tree_cons (NULL_TREE, null_pointer_node, list);
1483 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1484 list = tree_cons (integer_zero_node, null_pointer_node, list);
1486 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1487 if (TARGET_VTABLE_USES_DESCRIPTORS)
1488 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1489 arraysize += 2;
1490 return build_constructor (build_prim_array_type (nativecode_ptr_type_node,
1491 arraysize), list);
1495 /* Set the method_index for a method decl. */
1496 void
1497 set_method_index (tree decl, tree method_index)
1499 if (method_index != NULL_TREE)
1501 /* method_index is null if we're using indirect dispatch. */
1502 method_index = fold (convert (sizetype, method_index));
1504 if (TARGET_VTABLE_USES_DESCRIPTORS)
1505 /* Add one to skip bogus descriptor for class and GC descriptor. */
1506 method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1507 else
1508 /* Add 1 to skip "class" field of dtable, and 1 to skip GC
1509 descriptor. */
1510 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1513 DECL_VINDEX (decl) = method_index;
1516 /* Get the method_index for a method decl. */
1517 tree
1518 get_method_index (tree decl)
1520 tree method_index = DECL_VINDEX (decl);
1522 if (! method_index)
1523 return NULL;
1525 if (TARGET_VTABLE_USES_DESCRIPTORS)
1526 /* Sub one to skip bogus descriptor for class and GC descriptor. */
1527 method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1528 else
1529 /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1530 method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1532 return method_index;
1535 static int
1536 supers_all_compiled (tree type)
1538 while (type != NULL_TREE)
1540 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1541 return 0;
1542 type = CLASSTYPE_SUPER (type);
1544 return 1;
1547 void
1548 make_class_data (tree type)
1550 tree decl, cons, temp;
1551 tree field, fields_decl;
1552 tree static_fields = NULL_TREE;
1553 tree instance_fields = NULL_TREE;
1554 HOST_WIDE_INT static_field_count = 0;
1555 HOST_WIDE_INT instance_field_count = 0;
1556 HOST_WIDE_INT field_count;
1557 tree field_array_type;
1558 tree method;
1559 tree methods = NULL_TREE;
1560 tree dtable_decl = NULL_TREE;
1561 HOST_WIDE_INT method_count = 0;
1562 tree method_array_type;
1563 tree methods_decl;
1564 tree super;
1565 tree this_class_addr;
1566 tree constant_pool_constructor;
1567 tree interfaces = null_pointer_node;
1568 int interface_len = 0;
1569 tree type_decl = TYPE_NAME (type);
1570 /** Offset from start of virtual function table declaration
1571 to where objects actually point at, following new g++ ABI. */
1572 tree dtable_start_offset = build_int_cst (NULL_TREE,
1573 2 * POINTER_SIZE / BITS_PER_UNIT);
1575 this_class_addr = build_class_ref (type);
1576 decl = TREE_OPERAND (this_class_addr, 0);
1578 /* Build Field array. */
1579 field = TYPE_FIELDS (type);
1580 while (field && DECL_ARTIFICIAL (field))
1581 field = TREE_CHAIN (field); /* Skip dummy fields. */
1582 if (field && DECL_NAME (field) == NULL_TREE)
1583 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1584 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1586 if (! DECL_ARTIFICIAL (field))
1588 tree init = make_field_value (field);
1589 if (FIELD_STATIC (field))
1591 tree initial = DECL_INITIAL (field);
1592 static_field_count++;
1593 static_fields = tree_cons (NULL_TREE, init, static_fields);
1594 /* If the initial value is a string constant,
1595 prevent output_constant from trying to assemble the value. */
1596 if (initial != NULL_TREE
1597 && TREE_TYPE (initial) == string_ptr_type_node)
1598 DECL_INITIAL (field) = NULL_TREE;
1599 rest_of_decl_compilation (field, 1, 1);
1600 DECL_INITIAL (field) = initial;
1602 else
1604 instance_field_count++;
1605 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1609 field_count = static_field_count + instance_field_count;
1610 if (field_count > 0)
1612 static_fields = nreverse (static_fields);
1613 instance_fields = nreverse (instance_fields);
1614 static_fields = chainon (static_fields, instance_fields);
1615 field_array_type = build_prim_array_type (field_type_node, field_count);
1616 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1617 field_array_type);
1618 DECL_INITIAL (fields_decl) = build_constructor (field_array_type,
1619 static_fields);
1620 TREE_STATIC (fields_decl) = 1;
1621 DECL_ARTIFICIAL (fields_decl) = 1;
1622 DECL_IGNORED_P (fields_decl) = 1;
1623 rest_of_decl_compilation (fields_decl, 1, 0);
1625 else
1626 fields_decl = NULL_TREE;
1628 /* Build Method array. */
1629 for (method = TYPE_METHODS (type);
1630 method != NULL_TREE; method = TREE_CHAIN (method))
1632 tree init;
1633 if (METHOD_PRIVATE (method)
1634 && ! flag_keep_inline_functions
1635 && optimize)
1636 continue;
1637 /* Even if we have a decl, we don't necessarily have the code.
1638 This can happen if we inherit a method from a superclass for
1639 which we don't have a .class file. */
1640 if (METHOD_DUMMY (method))
1641 continue;
1642 init = make_method_value (method);
1643 method_count++;
1644 methods = tree_cons (NULL_TREE, init, methods);
1646 method_array_type = build_prim_array_type (method_type_node, method_count);
1647 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1648 method_array_type);
1649 DECL_INITIAL (methods_decl) = build_constructor (method_array_type,
1650 nreverse (methods));
1651 TREE_STATIC (methods_decl) = 1;
1652 DECL_ARTIFICIAL (methods_decl) = 1;
1653 DECL_IGNORED_P (methods_decl) = 1;
1654 rest_of_decl_compilation (methods_decl, 1, 0);
1656 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1657 && !flag_indirect_dispatch)
1659 tree dtable = get_dispatch_table (type, this_class_addr);
1660 dtable_decl = build_dtable_decl (type);
1661 DECL_INITIAL (dtable_decl) = dtable;
1662 TREE_STATIC (dtable_decl) = 1;
1663 DECL_ARTIFICIAL (dtable_decl) = 1;
1664 DECL_IGNORED_P (dtable_decl) = 1;
1665 TREE_PUBLIC (dtable_decl) = 1;
1666 rest_of_decl_compilation (dtable_decl, 1, 0);
1667 if (type == class_type_node)
1668 class_dtable_decl = dtable_decl;
1671 if (class_dtable_decl == NULL_TREE)
1673 class_dtable_decl = build_dtable_decl (class_type_node);
1674 TREE_STATIC (class_dtable_decl) = 1;
1675 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1676 DECL_IGNORED_P (class_dtable_decl) = 1;
1677 if (is_compiled_class (class_type_node) != 2)
1678 DECL_EXTERNAL (class_dtable_decl) = 1;
1679 rest_of_decl_compilation (class_dtable_decl, 1, 0);
1682 super = CLASSTYPE_SUPER (type);
1683 if (super == NULL_TREE)
1684 super = null_pointer_node;
1685 else if (! flag_indirect_dispatch
1686 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1687 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1688 super = build_class_ref (super);
1689 else
1691 int super_index = alloc_class_constant (super);
1692 super = build_int_cst (ptr_type_node, super_index);
1695 /* Build and emit the array of implemented interfaces. */
1696 if (type != object_type_node)
1697 interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
1699 if (interface_len > 0)
1701 tree init = NULL_TREE;
1702 int i;
1703 tree interface_array_type, idecl;
1704 interface_array_type
1705 = build_prim_array_type (class_ptr_type, interface_len);
1706 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1707 interface_array_type);
1709 for (i = interface_len; i > 0; i--)
1711 tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1712 tree iclass = BINFO_TYPE (child);
1713 tree index;
1714 if (! flag_indirect_dispatch
1715 && (assume_compiled
1716 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1717 index = build_class_ref (iclass);
1718 else
1720 int int_index = alloc_class_constant (iclass);
1721 index = build_int_cst (ptr_type_node, int_index);
1723 init = tree_cons (NULL_TREE, index, init);
1725 DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
1726 TREE_STATIC (idecl) = 1;
1727 DECL_ARTIFICIAL (idecl) = 1;
1728 DECL_IGNORED_P (idecl) = 1;
1729 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1730 rest_of_decl_compilation (idecl, 1, 0);
1733 constant_pool_constructor = build_constants_constructor ();
1735 if (flag_indirect_dispatch)
1737 TYPE_OTABLE_DECL (type)
1738 = emit_symbol_table
1739 (DECL_NAME (TYPE_OTABLE_DECL (type)),
1740 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
1741 TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
1743 TYPE_ATABLE_DECL (type)
1744 = emit_symbol_table
1745 (DECL_NAME (TYPE_ATABLE_DECL (type)),
1746 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
1747 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
1749 TYPE_ITABLE_DECL (type)
1750 = emit_symbol_table
1751 (DECL_NAME (TYPE_ITABLE_DECL (type)),
1752 TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
1753 TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
1756 TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1758 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1759 PUSH_FIELD_VALUE (temp, "vtable",
1760 build2 (PLUS_EXPR, dtable_ptr_type,
1761 build1 (ADDR_EXPR, dtable_ptr_type,
1762 class_dtable_decl),
1763 dtable_start_offset));
1764 if (! flag_hash_synchronization)
1765 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1766 FINISH_RECORD_CONSTRUCTOR (temp);
1767 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1768 PUSH_SUPER_VALUE (cons, temp);
1769 PUSH_FIELD_VALUE (cons, "next_or_version", gcj_abi_version);
1770 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1771 PUSH_FIELD_VALUE (cons, "accflags",
1772 build_int_cst (NULL_TREE,
1773 get_access_flags_from_decl (type_decl)));
1775 PUSH_FIELD_VALUE (cons, "superclass",
1776 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1777 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1778 PUSH_FIELD_VALUE (cons, "methods",
1779 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1780 PUSH_FIELD_VALUE (cons, "method_count",
1781 build_int_cst (NULL_TREE, method_count));
1783 if (flag_indirect_dispatch)
1784 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1785 else
1786 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1788 PUSH_FIELD_VALUE (cons, "fields",
1789 fields_decl == NULL_TREE ? null_pointer_node
1790 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1791 /* If we're using the binary compatibility ABI we don't know the
1792 size until load time. */
1793 PUSH_FIELD_VALUE (cons, "size_in_bytes",
1794 (flag_indirect_dispatch
1795 ? integer_minus_one_node
1796 : size_in_bytes (type)));
1797 PUSH_FIELD_VALUE (cons, "field_count",
1798 build_int_cst (NULL_TREE, field_count));
1799 PUSH_FIELD_VALUE (cons, "static_field_count",
1800 build_int_cst (NULL_TREE, static_field_count));
1802 if (flag_indirect_dispatch)
1803 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1804 else
1805 PUSH_FIELD_VALUE (cons, "vtable",
1806 dtable_decl == NULL_TREE ? null_pointer_node
1807 : build2 (PLUS_EXPR, dtable_ptr_type,
1808 build1 (ADDR_EXPR, dtable_ptr_type,
1809 dtable_decl),
1810 dtable_start_offset));
1811 if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1813 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1814 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1816 else
1818 PUSH_FIELD_VALUE (cons, "otable",
1819 build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1820 PUSH_FIELD_VALUE (cons, "otable_syms",
1821 build1 (ADDR_EXPR, symbols_array_ptr_type,
1822 TYPE_OTABLE_SYMS_DECL (type)));
1823 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1824 TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1826 if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1828 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1829 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1831 else
1833 PUSH_FIELD_VALUE (cons, "atable",
1834 build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1835 PUSH_FIELD_VALUE (cons, "atable_syms",
1836 build1 (ADDR_EXPR, symbols_array_ptr_type,
1837 TYPE_ATABLE_SYMS_DECL (type)));
1838 TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1839 TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1841 if (TYPE_ITABLE_METHODS(type) == NULL_TREE)
1843 PUSH_FIELD_VALUE (cons, "itable", null_pointer_node);
1844 PUSH_FIELD_VALUE (cons, "itable_syms", null_pointer_node);
1846 else
1848 PUSH_FIELD_VALUE (cons, "itable",
1849 build1 (ADDR_EXPR, itable_ptr_type, TYPE_ITABLE_DECL (type)));
1850 PUSH_FIELD_VALUE (cons, "itable_syms",
1851 build1 (ADDR_EXPR, symbols_array_ptr_type,
1852 TYPE_ITABLE_SYMS_DECL (type)));
1853 TREE_CONSTANT (TYPE_ITABLE_DECL (type)) = 1;
1854 TREE_INVARIANT (TYPE_ITABLE_DECL (type)) = 1;
1857 PUSH_FIELD_VALUE (cons, "catch_classes",
1858 build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
1859 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1860 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1861 PUSH_FIELD_VALUE (cons, "interface_count",
1862 build_int_cst (NULL_TREE, interface_len));
1863 PUSH_FIELD_VALUE (cons, "state",
1864 convert (byte_type_node,
1865 build_int_cst (NULL_TREE, JV_STATE_PRELOADING)));
1867 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1868 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1869 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1870 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1871 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1872 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1875 tree assertion_table_ref;
1876 if (TYPE_ASSERTIONS (type) == NULL)
1877 assertion_table_ref = null_pointer_node;
1878 else
1879 assertion_table_ref = build1 (ADDR_EXPR,
1880 build_pointer_type (assertion_table_type),
1881 emit_assertion_table (type));
1883 PUSH_FIELD_VALUE (cons, "assertion_table", assertion_table_ref);
1886 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1887 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1888 PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1889 PUSH_FIELD_VALUE (cons, "engine", null_pointer_node);
1891 FINISH_RECORD_CONSTRUCTOR (cons);
1893 DECL_INITIAL (decl) = cons;
1895 /* Hash synchronization requires at least 64-bit alignment. */
1896 if (flag_hash_synchronization && POINTER_SIZE < 64)
1897 DECL_ALIGN (decl) = 64;
1899 rest_of_decl_compilation (decl, 1, 0);
1901 TYPE_OTABLE_DECL (type) = NULL_TREE;
1902 TYPE_ATABLE_DECL (type) = NULL_TREE;
1903 TYPE_CTABLE_DECL (type) = NULL_TREE;
1906 void
1907 finish_class (void)
1909 if (TYPE_VERIFY_METHOD (output_class))
1911 tree verify_method = TYPE_VERIFY_METHOD (output_class);
1912 DECL_SAVED_TREE (verify_method)
1913 = add_stmt_to_compound (DECL_SAVED_TREE (verify_method), void_type_node,
1914 build (RETURN_EXPR, void_type_node, NULL));
1915 java_genericize (verify_method);
1916 cgraph_finalize_function (verify_method, false);
1917 TYPE_ASSERTIONS (current_class) = NULL;
1920 java_expand_catch_classes (current_class);
1922 current_function_decl = NULL_TREE;
1923 make_class_data (current_class);
1924 register_class ();
1925 rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
1928 /* Return 2 if CLASS is compiled by this compilation job;
1929 return 1 if CLASS can otherwise be assumed to be compiled;
1930 return 0 if we cannot assume that CLASS is compiled.
1931 Returns 1 for primitive and 0 for array types. */
1933 is_compiled_class (tree class)
1935 int seen_in_zip;
1936 if (TREE_CODE (class) == POINTER_TYPE)
1937 class = TREE_TYPE (class);
1938 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1939 return 1;
1940 if (TYPE_ARRAY_P (class))
1941 return 0;
1942 if (class == current_class)
1943 return 2;
1945 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1946 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1948 /* The class was seen in the current ZIP file and will be
1949 available as a compiled class in the future but may not have
1950 been loaded already. Load it if necessary. This prevent
1951 build_class_ref () from crashing. */
1953 if (seen_in_zip && !CLASS_LOADED_P (class))
1954 load_class (class, 1);
1956 /* We return 2 for class seen in ZIP and class from files
1957 belonging to the same compilation unit */
1958 return 2;
1961 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1963 if (!CLASS_LOADED_P (class))
1965 if (CLASS_FROM_SOURCE_P (class))
1966 safe_layout_class (class);
1967 else
1968 load_class (class, 1);
1970 return 1;
1973 return 0;
1976 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1978 tree
1979 build_dtable_decl (tree type)
1981 tree dtype;
1983 /* We need to build a new dtable type so that its size is uniquely
1984 computed when we're dealing with the class for real and not just
1985 faking it (like java.lang.Class during the initialization of the
1986 compiler.) We know we're not faking a class when CURRENT_CLASS is
1987 TYPE. */
1988 if (current_class == type)
1990 tree dummy = NULL_TREE;
1991 int n;
1993 dtype = make_node (RECORD_TYPE);
1995 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1996 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1998 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1999 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2001 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2002 TREE_CHAIN (dummy) = tmp_field;
2003 DECL_CONTEXT (tmp_field) = dtype;
2004 DECL_ARTIFICIAL (tmp_field) = 1;
2005 dummy = tmp_field;
2008 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
2009 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2011 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2012 TREE_CHAIN (dummy) = tmp_field;
2013 DECL_CONTEXT (tmp_field) = dtype;
2014 DECL_ARTIFICIAL (tmp_field) = 1;
2015 dummy = tmp_field;
2018 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
2019 if (TARGET_VTABLE_USES_DESCRIPTORS)
2020 n *= TARGET_VTABLE_USES_DESCRIPTORS;
2022 PUSH_FIELD (dtype, dummy, "methods",
2023 build_prim_array_type (nativecode_ptr_type_node, n));
2024 layout_type (dtype);
2026 else
2027 dtype = dtable_type;
2029 return build_decl (VAR_DECL,
2030 java_mangle_vtable (&temporary_obstack, type), dtype);
2033 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2034 fields inherited from SUPER_CLASS. */
2036 void
2037 push_super_field (tree this_class, tree super_class)
2039 tree base_decl;
2040 /* Don't insert the field if we're just re-laying the class out. */
2041 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2042 return;
2043 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
2044 DECL_IGNORED_P (base_decl) = 1;
2045 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2046 TYPE_FIELDS (this_class) = base_decl;
2047 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2048 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2051 /* Handle the different manners we may have to lay out a super class. */
2053 static tree
2054 maybe_layout_super_class (tree super_class, tree this_class)
2056 if (!super_class)
2057 return NULL_TREE;
2058 else if (TREE_CODE (super_class) == RECORD_TYPE)
2060 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
2061 safe_layout_class (super_class);
2062 if (!CLASS_LOADED_P (super_class))
2063 load_class (super_class, 1);
2065 /* We might have to layout the class before its dependency on
2066 the super class gets resolved by java_complete_class */
2067 else if (TREE_CODE (super_class) == POINTER_TYPE)
2069 if (TREE_TYPE (super_class) != NULL_TREE)
2070 super_class = TREE_TYPE (super_class);
2071 else
2073 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
2074 we give it one. */
2075 tree this_wrap = NULL_TREE;
2077 /* Set the correct context for class resolution. */
2078 current_class = this_class;
2080 if (this_class)
2082 tree this_decl = TYPE_NAME (this_class);
2083 #ifdef USE_MAPPED_LOCATION
2084 this_wrap = build_expr_wfl (this_class,
2085 DECL_SOURCE_LOCATION (this_decl));
2086 #else
2087 this_wrap = build_expr_wfl (this_class,
2088 DECL_SOURCE_FILE (this_decl),
2089 DECL_SOURCE_LINE (this_decl), 0);
2090 #endif
2092 super_class = do_resolve_class (NULL_TREE, this_class,
2093 super_class, NULL_TREE, this_wrap);
2094 if (!super_class)
2095 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
2096 super_class = TREE_TYPE (super_class);
2099 if (!TYPE_SIZE (super_class))
2100 safe_layout_class (super_class);
2102 return super_class;
2105 void
2106 layout_class (tree this_class)
2108 tree super_class = CLASSTYPE_SUPER (this_class);
2109 tree field;
2111 class_list = tree_cons (this_class, NULL_TREE, class_list);
2112 if (CLASS_BEING_LAIDOUT (this_class))
2114 char buffer [1024];
2115 char *report;
2116 tree current;
2118 sprintf (buffer, " with '%s'",
2119 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2120 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2122 for (current = TREE_CHAIN (class_list); current;
2123 current = TREE_CHAIN (current))
2125 tree decl = TYPE_NAME (TREE_PURPOSE (current));
2126 sprintf (buffer, "\n which inherits from '%s' (%s:%d)",
2127 IDENTIFIER_POINTER (DECL_NAME (decl)),
2128 DECL_SOURCE_FILE (decl),
2129 DECL_SOURCE_LINE (decl));
2130 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2132 obstack_1grow (&temporary_obstack, '\0');
2133 report = obstack_finish (&temporary_obstack);
2134 cyclic_inheritance_report = ggc_strdup (report);
2135 obstack_free (&temporary_obstack, report);
2136 TYPE_SIZE (this_class) = error_mark_node;
2137 return;
2139 CLASS_BEING_LAIDOUT (this_class) = 1;
2141 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2143 tree maybe_super_class
2144 = maybe_layout_super_class (super_class, this_class);
2145 if (maybe_super_class == NULL
2146 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2148 TYPE_SIZE (this_class) = error_mark_node;
2149 CLASS_BEING_LAIDOUT (this_class) = 0;
2150 class_list = TREE_CHAIN (class_list);
2151 return;
2153 if (TYPE_SIZE (this_class) == NULL_TREE)
2154 push_super_field (this_class, maybe_super_class);
2157 for (field = TYPE_FIELDS (this_class);
2158 field != NULL_TREE; field = TREE_CHAIN (field))
2160 if (FIELD_STATIC (field))
2162 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
2163 SET_DECL_ASSEMBLER_NAME (field,
2164 java_mangle_decl
2165 (&temporary_obstack, field));
2169 layout_type (this_class);
2171 /* Also recursively load/layout any superinterfaces, but only if
2172 class was loaded from bytecode. The source parser will take care
2173 of this itself. */
2174 if (!CLASS_FROM_SOURCE_P (this_class))
2176 int i;
2177 if (TYPE_BINFO (this_class))
2179 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2181 tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2182 tree super_interface = BINFO_TYPE (binfo);
2183 tree maybe_super_interface
2184 = maybe_layout_super_class (super_interface, NULL_TREE);
2185 if (maybe_super_interface == NULL
2186 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2188 TYPE_SIZE (this_class) = error_mark_node;
2189 CLASS_BEING_LAIDOUT (this_class) = 0;
2190 class_list = TREE_CHAIN (class_list);
2191 return;
2197 /* Convert the size back to an SI integer value. */
2198 TYPE_SIZE_UNIT (this_class) =
2199 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2201 CLASS_BEING_LAIDOUT (this_class) = 0;
2202 class_list = TREE_CHAIN (class_list);
2205 static void
2206 add_miranda_methods (tree base_class, tree search_class)
2208 int i;
2209 tree binfo, base_binfo;
2211 if (!CLASS_PARSED_P (search_class))
2212 load_class (search_class, 1);
2214 for (binfo = TYPE_BINFO (search_class), i = 1;
2215 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2217 tree method_decl;
2218 tree elt = BINFO_TYPE (base_binfo);
2220 /* FIXME: This is totally bogus. We should not be handling
2221 Miranda methods at all if we're using the BC ABI. */
2222 if (TYPE_DUMMY (elt))
2223 continue;
2225 /* Ensure that interface methods are seen in declared order. */
2226 if (!CLASS_LOADED_P (elt))
2227 load_class (elt, 1);
2228 layout_class_methods (elt);
2230 /* All base classes will have been laid out at this point, so the order
2231 will be correct. This code must match similar layout code in the
2232 runtime. */
2233 for (method_decl = TYPE_METHODS (elt);
2234 method_decl; method_decl = TREE_CHAIN (method_decl))
2236 tree sig, override;
2238 /* An interface can have <clinit>. */
2239 if (ID_CLINIT_P (DECL_NAME (method_decl)))
2240 continue;
2242 sig = build_java_argument_signature (TREE_TYPE (method_decl));
2243 override = lookup_argument_method (base_class,
2244 DECL_NAME (method_decl), sig);
2245 if (override == NULL_TREE)
2247 /* Found a Miranda method. Add it. */
2248 tree new_method;
2249 sig = build_java_signature (TREE_TYPE (method_decl));
2250 new_method
2251 = add_method (base_class,
2252 get_access_flags_from_decl (method_decl),
2253 DECL_NAME (method_decl), sig);
2254 METHOD_INVISIBLE (new_method) = 1;
2258 /* Try superinterfaces. */
2259 add_miranda_methods (base_class, elt);
2263 void
2264 layout_class_methods (tree this_class)
2266 tree method_decl, dtable_count;
2267 tree super_class, type_name;
2269 if (TYPE_NVIRTUALS (this_class))
2270 return;
2272 super_class = CLASSTYPE_SUPER (this_class);
2274 if (super_class)
2276 super_class = maybe_layout_super_class (super_class, this_class);
2277 if (!TYPE_NVIRTUALS (super_class))
2278 layout_class_methods (super_class);
2279 dtable_count = TYPE_NVIRTUALS (super_class);
2281 else
2282 dtable_count = integer_zero_node;
2284 type_name = TYPE_NAME (this_class);
2285 if (!flag_indirect_dispatch
2286 && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2288 /* An abstract class can have methods which are declared only in
2289 an implemented interface. These are called "Miranda
2290 methods". We make a dummy method entry for such methods
2291 here. */
2292 add_miranda_methods (this_class, this_class);
2295 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2297 for (method_decl = TYPE_METHODS (this_class);
2298 method_decl; method_decl = TREE_CHAIN (method_decl))
2299 dtable_count = layout_class_method (this_class, super_class,
2300 method_decl, dtable_count);
2302 TYPE_NVIRTUALS (this_class) = dtable_count;
2305 /* Return the index of METHOD in INTERFACE. This index begins at 1 and is used as an
2306 argument for _Jv_LookupInterfaceMethodIdx(). */
2308 get_interface_method_index (tree method, tree interface)
2310 tree meth;
2311 int i = 1;
2313 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
2315 if (meth == method)
2316 return i;
2317 if (meth == NULL_TREE)
2318 abort ();
2322 /* Lay METHOD_DECL out, returning a possibly new value of
2323 DTABLE_COUNT. Also mangle the method's name. */
2325 tree
2326 layout_class_method (tree this_class, tree super_class,
2327 tree method_decl, tree dtable_count)
2329 tree method_name = DECL_NAME (method_decl);
2331 TREE_PUBLIC (method_decl) = 1;
2332 /* Considered external until we know what classes are being
2333 compiled into this object file. */
2334 DECL_EXTERNAL (method_decl) = 1;
2336 /* This is a good occasion to mangle the method's name */
2337 SET_DECL_ASSEMBLER_NAME (method_decl,
2338 java_mangle_decl (&temporary_obstack,
2339 method_decl));
2340 /* We don't generate a RTL for the method if it's abstract, or if
2341 it's an interface method that isn't clinit. */
2342 if (! METHOD_ABSTRACT (method_decl)
2343 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2344 && (DECL_CLINIT_P (method_decl))))
2345 make_decl_rtl (method_decl);
2347 if (ID_INIT_P (method_name))
2349 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2350 const char *ptr;
2351 for (ptr = p; *ptr; )
2353 if (*ptr++ == '.')
2354 p = ptr;
2356 DECL_CONSTRUCTOR_P (method_decl) = 1;
2357 build_java_argument_signature (TREE_TYPE (method_decl));
2359 else if (! METHOD_STATIC (method_decl))
2361 tree method_sig =
2362 build_java_argument_signature (TREE_TYPE (method_decl));
2363 bool method_override = false;
2364 tree super_method = lookup_argument_method (super_class, method_name,
2365 method_sig);
2366 if (super_method != NULL_TREE
2367 && ! METHOD_DUMMY (super_method))
2369 method_override = true;
2370 if (! METHOD_PUBLIC (super_method) &&
2371 ! METHOD_PROTECTED (super_method))
2373 /* Don't override private method, or default-access method in
2374 another package. */
2375 if (METHOD_PRIVATE (super_method) ||
2376 ! in_same_package (TYPE_NAME (this_class),
2377 TYPE_NAME (super_class)))
2378 method_override = false;
2381 if (method_override)
2383 tree method_index = get_method_index (super_method);
2384 set_method_index (method_decl, method_index);
2385 if (method_index == NULL_TREE
2386 && ! flag_indirect_dispatch
2387 && !CLASS_FROM_SOURCE_P (this_class)
2388 && ! DECL_ARTIFICIAL (super_method))
2389 error ("%Jnon-static method '%D' overrides static method",
2390 method_decl, method_decl);
2392 else if (this_class == object_type_node
2393 && (METHOD_FINAL (method_decl)
2394 || METHOD_PRIVATE (method_decl)))
2396 /* We don't generate vtable entries for final Object
2397 methods. This is simply to save space, since every
2398 object would otherwise have to define them. */
2400 else if (! METHOD_PRIVATE (method_decl)
2401 && dtable_count)
2403 /* We generate vtable entries for final methods because they
2404 may one day be changed to non-final. */
2405 set_method_index (method_decl, dtable_count);
2406 dtable_count = fold (build2 (PLUS_EXPR, integer_type_node,
2407 dtable_count, integer_one_node));
2411 return dtable_count;
2414 void
2415 register_class (void)
2417 /* END does not need to be registered with the garbage collector
2418 because it always points into the list given by REGISTERED_CLASS,
2419 and that variable is registered with the collector. */
2420 static tree end;
2421 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2422 tree current = copy_node (node);
2424 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2425 if (!registered_class)
2426 registered_class = current;
2427 else
2428 TREE_CHAIN (end) = current;
2430 end = current;
2433 /* Emit something to register classes at start-up time.
2435 The preferred mechanism is through the .jcr section, which contain
2436 a list of pointers to classes which get registered during constructor
2437 invocation time.
2439 The fallback mechanism is to add statements to *LIST_P to call
2440 _Jv_RegisterClass for each class in this file. These statements will
2441 be added to a static constructor function for this translation unit. */
2443 void
2444 emit_register_classes (tree *list_p)
2446 if (registered_class == NULL)
2447 return;
2449 /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2450 TARGET_ASM_NAMED_SECTION, else 0. Some targets meet those conditions
2451 but lack suitable crtbegin/end objects or linker support. These
2452 targets can overide the default in tm.h to use the fallback mechanism. */
2453 if (TARGET_USE_JCR_SECTION)
2455 #ifdef JCR_SECTION_NAME
2456 tree t;
2457 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2458 assemble_align (POINTER_SIZE);
2459 for (t = registered_class; t; t = TREE_CHAIN (t))
2460 assemble_integer (XEXP (DECL_RTL (t), 0),
2461 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2462 #else
2463 /* A target has defined TARGET_USE_JCR_SECTION, but doesn't have a
2464 JCR_SECTION_NAME. */
2465 abort ();
2466 #endif
2468 else
2470 tree klass, t, register_class_fn;
2472 t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2473 t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2474 TREE_PUBLIC (t) = 1;
2475 DECL_EXTERNAL (t) = 1;
2476 register_class_fn = t;
2478 for (klass = registered_class; klass; klass = TREE_CHAIN (klass))
2480 t = build_fold_addr_expr (klass);
2481 t = tree_cons (NULL, t, NULL);
2482 t = build_function_call_expr (register_class_fn, t);
2483 append_to_statement_list (t, list_p);
2488 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2490 static tree
2491 build_symbol_entry (tree decl)
2493 tree clname, name, signature, sym;
2494 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2495 /* ??? Constructors are given the name foo.foo all the way through
2496 the compiler, but in the method table they're all renamed
2497 foo.<init>. So, we have to do the same here unless we want an
2498 unresolved reference at runtime. */
2499 name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
2500 && DECL_CONSTRUCTOR_P (decl))
2501 ? init_identifier_node
2502 : DECL_NAME (decl));
2503 signature = build_java_signature (TREE_TYPE (decl));
2504 signature = build_utf8_ref (unmangle_classname
2505 (IDENTIFIER_POINTER (signature),
2506 IDENTIFIER_LENGTH (signature)));
2508 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2509 PUSH_FIELD_VALUE (sym, "clname", clname);
2510 PUSH_FIELD_VALUE (sym, "name", name);
2511 PUSH_FIELD_VALUE (sym, "signature", signature);
2512 FINISH_RECORD_CONSTRUCTOR (sym);
2513 TREE_CONSTANT (sym) = 1;
2514 TREE_INVARIANT (sym) = 1;
2516 return sym;
2519 /* Emit a symbol table: used by -findirect-dispatch. */
2521 tree
2522 emit_symbol_table (tree name, tree the_table, tree decl_list,
2523 tree the_syms_decl, tree the_array_element_type,
2524 int element_size)
2526 tree method_list, method, table, list, null_symbol;
2527 tree table_size, the_array_type;
2528 int index;
2530 /* Only emit a table if this translation unit actually made any
2531 references via it. */
2532 if (decl_list == NULL_TREE)
2533 return the_table;
2535 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2536 index = 0;
2537 method_list = decl_list;
2538 list = NULL_TREE;
2539 while (method_list != NULL_TREE)
2541 method = TREE_VALUE (method_list);
2542 list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2543 method_list = TREE_CHAIN (method_list);
2544 index++;
2547 /* Terminate the list with a "null" entry. */
2548 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2549 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2550 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2551 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2552 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2553 TREE_CONSTANT (null_symbol) = 1;
2554 TREE_INVARIANT (null_symbol) = 1;
2555 list = tree_cons (NULL_TREE, null_symbol, list);
2557 /* Put the list in the right order and make it a constructor. */
2558 list = nreverse (list);
2559 table = build_constructor (symbols_array_type, list);
2561 /* Make it the initial value for otable_syms and emit the decl. */
2562 DECL_INITIAL (the_syms_decl) = table;
2563 DECL_ARTIFICIAL (the_syms_decl) = 1;
2564 DECL_IGNORED_P (the_syms_decl) = 1;
2565 rest_of_decl_compilation (the_syms_decl, 1, 0);
2567 /* Now that its size is known, redefine the table as an
2568 uninitialized static array of INDEX + 1 elements. The extra entry
2569 is used by the runtime to track whether the table has been
2570 initialized. */
2571 table_size
2572 = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2573 the_array_type = build_array_type (the_array_element_type, table_size);
2574 the_table = build_decl (VAR_DECL, name, the_array_type);
2575 TREE_STATIC (the_table) = 1;
2576 TREE_READONLY (the_table) = 1;
2577 rest_of_decl_compilation (the_table, 1, 0);
2579 return the_table;
2582 /* Make an entry for the catch_classes list. */
2583 tree
2584 make_catch_class_record (tree catch_class, tree classname)
2586 tree entry;
2587 tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2588 START_RECORD_CONSTRUCTOR (entry, type);
2589 PUSH_FIELD_VALUE (entry, "address", catch_class);
2590 PUSH_FIELD_VALUE (entry, "classname", classname);
2591 FINISH_RECORD_CONSTRUCTOR (entry);
2592 return entry;
2596 /* Generate the list of Throwable classes that are caught by exception
2597 handlers in this class. */
2598 tree
2599 emit_catch_table (tree this_class)
2601 tree table, table_size, array_type;
2602 TYPE_CATCH_CLASSES (this_class) =
2603 tree_cons (NULL,
2604 make_catch_class_record (null_pointer_node, null_pointer_node),
2605 TYPE_CATCH_CLASSES (this_class));
2606 TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2607 TYPE_CATCH_CLASSES (this_class) =
2608 tree_cons (NULL,
2609 make_catch_class_record (null_pointer_node, null_pointer_node),
2610 TYPE_CATCH_CLASSES (this_class));
2611 table_size = build_index_type
2612 (build_int_cst (NULL_TREE,
2613 list_length (TYPE_CATCH_CLASSES (this_class))));
2614 array_type
2615 = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2616 table_size);
2617 table =
2618 build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2619 DECL_INITIAL (table) =
2620 build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
2621 TREE_STATIC (table) = 1;
2622 TREE_READONLY (table) = 1;
2623 DECL_IGNORED_P (table) = 1;
2624 rest_of_decl_compilation (table, 1, 0);
2625 return table;
2628 /* Given a type, return the signature used by
2629 _Jv_FindClassFromSignature() in libgcj. This isn't exactly the
2630 same as build_java_signature() because we want the canonical array
2631 type. */
2633 static tree
2634 build_signature_for_libgcj (tree type)
2636 tree sig, ref;
2638 sig = build_java_signature (type);
2639 ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
2640 IDENTIFIER_LENGTH (sig)));
2641 return ref;
2644 /* Add an entry to the type assertion table. Callback used during hashtable
2645 traversal. */
2647 static int
2648 add_assertion_table_entry (void **htab_entry, void *ptr)
2650 tree entry;
2651 tree code_val, op1_utf8, op2_utf8;
2652 tree *list = (tree *) ptr;
2653 type_assertion *as = (type_assertion *) *htab_entry;
2655 code_val = build_int_cst (NULL_TREE, as->assertion_code);
2657 if (as->op1 == NULL_TREE)
2658 op1_utf8 = null_pointer_node;
2659 else
2660 op1_utf8 = build_signature_for_libgcj (as->op1);
2662 if (as->op2 == NULL_TREE)
2663 op2_utf8 = null_pointer_node;
2664 else
2665 op2_utf8 = build_signature_for_libgcj (as->op2);
2667 START_RECORD_CONSTRUCTOR (entry, assertion_entry_type);
2668 PUSH_FIELD_VALUE (entry, "assertion_code", code_val);
2669 PUSH_FIELD_VALUE (entry, "op1", op1_utf8);
2670 PUSH_FIELD_VALUE (entry, "op2", op2_utf8);
2671 FINISH_RECORD_CONSTRUCTOR (entry);
2673 *list = tree_cons (NULL_TREE, entry, *list);
2674 return true;
2677 /* Generate the type assertion table for CLASS, and return its DECL. */
2679 static tree
2680 emit_assertion_table (tree class)
2682 tree null_entry, ctor, table_decl;
2683 tree list = NULL_TREE;
2684 htab_t assertions_htab = TYPE_ASSERTIONS (class);
2686 /* Iterate through the hash table. */
2687 htab_traverse (assertions_htab, add_assertion_table_entry, &list);
2689 /* Finish with a null entry. */
2690 START_RECORD_CONSTRUCTOR (null_entry, assertion_entry_type);
2691 PUSH_FIELD_VALUE (null_entry, "assertion_code", integer_zero_node);
2692 PUSH_FIELD_VALUE (null_entry, "op1", null_pointer_node);
2693 PUSH_FIELD_VALUE (null_entry, "op2", null_pointer_node);
2694 FINISH_RECORD_CONSTRUCTOR (null_entry);
2696 list = tree_cons (NULL_TREE, null_entry, list);
2698 /* Put the list in the right order and make it a constructor. */
2699 list = nreverse (list);
2700 ctor = build_constructor (assertion_table_type, list);
2702 table_decl = build_decl (VAR_DECL, mangled_classname ("_type_assert_", class),
2703 assertion_table_type);
2705 TREE_STATIC (table_decl) = 1;
2706 TREE_READONLY (table_decl) = 1;
2707 TREE_CONSTANT (table_decl) = 1;
2708 DECL_IGNORED_P (table_decl) = 1;
2710 DECL_INITIAL (table_decl) = ctor;
2711 DECL_ARTIFICIAL (table_decl) = 1;
2712 rest_of_decl_compilation (table_decl, 1, 0);
2714 return table_decl;
2717 void
2718 init_class_processing (void)
2720 fields_ident = get_identifier ("fields");
2721 info_ident = get_identifier ("info");
2723 gcc_obstack_init (&temporary_obstack);
2726 static hashval_t java_treetreehash_hash (const void *);
2727 static int java_treetreehash_compare (const void *, const void *);
2729 /* A hash table mapping trees to trees. Used generally. */
2731 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2733 static hashval_t
2734 java_treetreehash_hash (const void *k_p)
2736 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2737 return JAVA_TREEHASHHASH_H (k->key);
2740 static int
2741 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2743 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2744 tree k2 = (tree) k2_p;
2745 return (k1->key == k2);
2748 tree
2749 java_treetreehash_find (htab_t ht, tree t)
2751 struct treetreehash_entry *e;
2752 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2753 e = htab_find_with_hash (ht, t, hv);
2754 if (e == NULL)
2755 return NULL;
2756 else
2757 return e->value;
2760 tree *
2761 java_treetreehash_new (htab_t ht, tree t)
2763 void **e;
2764 struct treetreehash_entry *tthe;
2765 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2767 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2768 if (*e == NULL)
2770 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2771 tthe->key = t;
2772 *e = tthe;
2774 else
2775 tthe = (struct treetreehash_entry *) *e;
2776 return &tthe->value;
2779 htab_t
2780 java_treetreehash_create (size_t size, int gc)
2782 if (gc)
2783 return htab_create_ggc (size, java_treetreehash_hash,
2784 java_treetreehash_compare, NULL);
2785 else
2786 return htab_create_alloc (size, java_treetreehash_hash,
2787 java_treetreehash_compare, free, xcalloc, free);
2790 /* Break down qualified IDENTIFIER into package and class-name components.
2791 For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
2792 "pkg.foo", and RIGHT to "Bar". */
2795 split_qualified_name (tree *left, tree *right, tree source)
2797 char *p, *base;
2798 int l = IDENTIFIER_LENGTH (source);
2800 base = alloca (l + 1);
2801 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
2803 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
2804 p = base + l - 1;
2805 while (*p != '.' && p != base)
2806 p--;
2808 /* We didn't find a '.'. Return an error. */
2809 if (p == base)
2810 return 1;
2812 *p = '\0';
2813 if (right)
2814 *right = get_identifier (p+1);
2815 *left = get_identifier (base);
2817 return 0;
2820 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
2821 if the classes are from the same package. */
2824 in_same_package (tree name1, tree name2)
2826 tree tmp;
2827 tree pkg1;
2828 tree pkg2;
2830 if (TREE_CODE (name1) == TYPE_DECL)
2831 name1 = DECL_NAME (name1);
2832 if (TREE_CODE (name2) == TYPE_DECL)
2833 name2 = DECL_NAME (name2);
2835 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
2836 /* One in empty package. */
2837 return 0;
2839 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
2840 /* Both in empty package. */
2841 return 1;
2843 split_qualified_name (&pkg1, &tmp, name1);
2844 split_qualified_name (&pkg2, &tmp, name2);
2846 return (pkg1 == pkg2);
2849 #include "gt-java-class.h"