* combine.c (expand_compound_operation) <ZERO_EXTRACT>: Add
[official-gcc.git] / gcc / java / class.c
blob94138200d0b1965983c5d0b6d829d6e7add5cd40
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
321 return type;
324 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
325 and where each of the constituents is separated by '/',
326 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
328 tree
329 unmangle_classname (const char *name, int name_length)
331 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
332 /* It's not sufficient to compare to_return and get_identifier
333 (name) to determine whether to_return is qualified. There are
334 cases in signature analysis where name will be stripped of a
335 trailing ';'. */
336 name = IDENTIFIER_POINTER (to_return);
337 while (*name)
338 if (*name++ == '.')
340 QUALIFIED_P (to_return) = 1;
341 break;
344 return to_return;
347 #define GEN_TABLE(TABLE, NAME, TABLE_TYPE, TYPE) \
348 do \
350 const char *typename = IDENTIFIER_POINTER (mangled_classname ("", TYPE)); \
351 char *buf = alloca (strlen (typename) + strlen (#NAME "_syms_") + 1); \
352 tree decl; \
354 sprintf (buf, #NAME "_%s", typename); \
355 TYPE_## TABLE ##_DECL (type) = decl = \
356 build_decl (VAR_DECL, get_identifier (buf), TABLE_TYPE); \
357 DECL_EXTERNAL (decl) = 1; \
358 TREE_STATIC (decl) = 1; \
359 TREE_READONLY (decl) = 1; \
360 TREE_CONSTANT (decl) = 1; \
361 DECL_IGNORED_P (decl) = 1; \
362 /* Mark the table as belonging to this class. */ \
363 pushdecl (decl); \
364 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl); \
365 DECL_OWNER (decl) = TYPE; \
366 sprintf (buf, #NAME "_syms_%s", typename); \
367 TYPE_## TABLE ##_SYMS_DECL (TYPE) = \
368 build_decl (VAR_DECL, get_identifier (buf), symbols_array_type); \
369 TREE_STATIC (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
370 TREE_CONSTANT (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
371 DECL_IGNORED_P (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
372 pushdecl (TYPE_## TABLE ##_SYMS_DECL (TYPE)); \
374 while (0)
376 /* Given a class, create the DECLs for all its associated indirect
377 dispatch tables. */
378 void
379 gen_indirect_dispatch_tables (tree type)
381 const char *typename = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
383 tree field = NULL;
384 char *buf = alloca (strlen (typename) + strlen ("_catch_classes_") + 1);
385 tree catch_class_type = make_node (RECORD_TYPE);
387 sprintf (buf, "_catch_classes_%s", typename);
388 PUSH_FIELD (catch_class_type, field, "address", utf8const_ptr_type);
389 PUSH_FIELD (catch_class_type, field, "classname", ptr_type_node);
390 FINISH_RECORD (catch_class_type);
392 TYPE_CTABLE_DECL (type)
393 = build_decl (VAR_DECL, get_identifier (buf),
394 build_array_type (catch_class_type, 0));
395 DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
396 TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
397 TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
398 TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
399 DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
400 pushdecl (TYPE_CTABLE_DECL (type));
403 if (flag_indirect_dispatch)
405 GEN_TABLE (ATABLE, _atable, atable_type, type);
406 GEN_TABLE (OTABLE, _otable, otable_type, type);
407 GEN_TABLE (ITABLE, _itable, itable_type, type);
411 #undef GEN_TABLE
413 tree
414 push_class (tree class_type, tree class_name)
416 tree decl, signature;
417 location_t saved_loc = input_location;
418 #ifndef USE_MAPPED_LOCATION
419 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
420 input_filename = IDENTIFIER_POINTER (source_name);
421 input_line = 0;
422 #endif
423 CLASS_P (class_type) = 1;
424 decl = build_decl (TYPE_DECL, class_name, class_type);
426 /* dbxout needs a DECL_SIZE if in gstabs mode */
427 DECL_SIZE (decl) = integer_zero_node;
429 input_location = saved_loc;
430 signature = identifier_subst (class_name, "L", '.', '/', ";");
431 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
433 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
434 both a typedef and in the struct name-space. We may want to re-visit
435 this later, but for now it reduces the changes needed for gdb. */
436 DECL_ARTIFICIAL (decl) = 1;
438 pushdecl_top_level (decl);
440 return decl;
443 /* Finds the (global) class named NAME. Creates the class if not found.
444 Also creates associated TYPE_DECL.
445 Does not check if the class actually exists, load the class,
446 fill in field or methods, or do layout_type. */
448 tree
449 lookup_class (tree name)
451 tree decl = IDENTIFIER_CLASS_VALUE (name);
452 if (decl == NULL_TREE)
453 decl = push_class (make_class (), name);
454 return TREE_TYPE (decl);
457 void
458 set_super_info (int access_flags, tree this_class,
459 tree super_class, int interfaces_count)
461 int total_supers = interfaces_count;
462 tree class_decl = TYPE_NAME (this_class);
464 if (super_class)
465 total_supers++;
467 TYPE_BINFO (this_class) = make_tree_binfo (total_supers);
468 TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
469 if (super_class)
471 tree super_binfo = make_tree_binfo (0);
472 BINFO_TYPE (super_binfo) = super_class;
473 BINFO_OFFSET (super_binfo) = integer_zero_node;
474 BINFO_BASE_APPEND (TYPE_BINFO (this_class), super_binfo);
475 CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
478 set_class_decl_access_flags (access_flags, class_decl);
481 void
482 set_class_decl_access_flags (int access_flags, tree class_decl)
484 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
485 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
486 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
487 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
488 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
489 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
490 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
491 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
492 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
495 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
496 direct sub-classes of Object are 1, and so on. */
499 class_depth (tree clas)
501 int depth = 0;
502 if (! CLASS_LOADED_P (clas))
503 load_class (clas, 1);
504 if (TYPE_SIZE (clas) == error_mark_node)
505 return -1;
506 while (clas != object_type_node)
508 depth++;
509 clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
511 return depth;
514 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
517 interface_of_p (tree type1, tree type2)
519 int i;
520 tree binfo, base_binfo;
522 if (! TYPE_BINFO (type2))
523 return 0;
525 for (binfo = TYPE_BINFO (type2), i = 0;
526 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
527 if (BINFO_TYPE (base_binfo) == type1)
528 return 1;
530 for (binfo = TYPE_BINFO (type2), i = 0;
531 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) /* */
532 if (BINFO_TYPE (base_binfo)
533 && interface_of_p (type1, BINFO_TYPE (base_binfo)))
534 return 1;
536 return 0;
539 /* Return true iff TYPE1 inherits from TYPE2. */
542 inherits_from_p (tree type1, tree type2)
544 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
546 if (type1 == type2)
547 return 1;
548 type1 = CLASSTYPE_SUPER (type1);
550 return 0;
553 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
556 enclosing_context_p (tree type1, tree type2)
558 if (!INNER_CLASS_TYPE_P (type2))
559 return 0;
561 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
562 type2;
563 type2 = (INNER_CLASS_TYPE_P (type2) ?
564 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
566 if (type2 == type1)
567 return 1;
570 return 0;
574 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
575 nesting level. */
578 common_enclosing_context_p (tree type1, tree type2)
580 while (type1)
582 tree current;
583 for (current = type2; current;
584 current = (INNER_CLASS_TYPE_P (current) ?
585 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
586 NULL_TREE))
587 if (type1 == current)
588 return 1;
590 if (INNER_CLASS_TYPE_P (type1))
591 type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
592 else
593 break;
595 return 0;
598 /* Return 1 iff there exists a common enclosing "this" between TYPE1
599 and TYPE2, without crossing any static context. */
602 common_enclosing_instance_p (tree type1, tree type2)
604 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
605 return 0;
607 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
608 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
609 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
611 tree current;
612 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
613 current = (PURE_INNER_CLASS_TYPE_P (current) ?
614 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
615 NULL_TREE))
616 if (type1 == current)
617 return 1;
619 return 0;
622 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
623 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
624 if attempt is made to add it twice. */
626 tree
627 maybe_add_interface (tree this_class, tree interface_class)
629 tree binfo, base_binfo;
630 int i;
632 for (binfo = TYPE_BINFO (this_class), i = 0;
633 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
634 if (BINFO_TYPE (base_binfo) == interface_class)
635 return interface_class;
636 add_interface (this_class, interface_class);
637 return NULL_TREE;
640 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
642 void
643 add_interface (tree this_class, tree interface_class)
645 tree interface_binfo = make_tree_binfo (0);
647 BINFO_TYPE (interface_binfo) = interface_class;
648 BINFO_OFFSET (interface_binfo) = integer_zero_node;
649 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
650 BINFO_VIRTUAL_P (interface_binfo) = 1;
652 BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
655 #if 0
656 /* Return the address of a pointer to the first FUNCTION_DECL
657 in the list (*LIST) whose DECL_NAME is NAME. */
659 static tree *
660 find_named_method (tree *list, tree name)
662 while (*list && DECL_NAME (*list) != name)
663 list = &TREE_CHAIN (*list);
664 return list;
666 #endif
668 static tree
669 build_java_method_type (tree fntype, tree this_class, int access_flags)
671 if (access_flags & ACC_STATIC)
672 return fntype;
673 return build_method_type (this_class, fntype);
676 tree
677 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
679 tree method_type, fndecl;
681 method_type = build_java_method_type (function_type,
682 this_class, access_flags);
684 fndecl = build_decl (FUNCTION_DECL, name, method_type);
685 DECL_CONTEXT (fndecl) = this_class;
687 DECL_LANG_SPECIFIC (fndecl)
688 = ggc_alloc_cleared (sizeof (struct lang_decl));
689 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
691 /* Initialize the static initializer test table. */
693 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
694 java_treetreehash_create (10, 1);
696 /* Initialize the initialized (static) class table. */
697 if (access_flags & ACC_STATIC)
698 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
699 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
701 /* Initialize the static method invocation compound list */
702 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
704 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
705 TYPE_METHODS (this_class) = fndecl;
707 /* Notice that this is a finalizer and update the class type
708 accordingly. This is used to optimize instance allocation. */
709 if (name == finalize_identifier_node
710 && TREE_TYPE (function_type) == void_type_node
711 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
712 HAS_FINALIZER_P (this_class) = 1;
714 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
715 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
716 if (access_flags & ACC_PRIVATE)
717 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
718 if (access_flags & ACC_NATIVE)
720 METHOD_NATIVE (fndecl) = 1;
721 DECL_EXTERNAL (fndecl) = 1;
723 if (access_flags & ACC_STATIC)
724 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
725 if (access_flags & ACC_FINAL)
726 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
727 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
728 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
729 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
730 return fndecl;
733 /* Add a method to THIS_CLASS.
734 The method's name is NAME.
735 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
737 tree
738 add_method (tree this_class, int access_flags, tree name, tree method_sig)
740 tree function_type, fndecl;
741 const unsigned char *sig
742 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
744 if (sig[0] != '(')
745 fatal_error ("bad method signature");
747 function_type = get_type_from_signature (method_sig);
748 fndecl = add_method_1 (this_class, access_flags, name, function_type);
749 set_java_signature (TREE_TYPE (fndecl), method_sig);
750 return fndecl;
753 tree
754 add_field (tree class, tree name, tree field_type, int flags)
756 int is_static = (flags & ACC_STATIC) != 0;
757 tree field;
758 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
759 TREE_CHAIN (field) = TYPE_FIELDS (class);
760 TYPE_FIELDS (class) = field;
761 DECL_CONTEXT (field) = class;
763 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
764 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
765 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
766 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
767 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
768 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
769 if (is_static)
771 FIELD_STATIC (field) = 1;
772 /* Always make field externally visible. This is required so
773 that native methods can always access the field. */
774 TREE_PUBLIC (field) = 1;
775 /* Considered external until we know what classes are being
776 compiled into this object file. */
777 DECL_EXTERNAL (field) = 1;
780 return field;
783 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
785 void
786 set_constant_value (tree field, tree constant)
788 if (field == NULL_TREE)
789 warning ("misplaced ConstantValue attribute (not in any field)");
790 else if (DECL_INITIAL (field) != NULL_TREE)
791 warning ("duplicate ConstantValue attribute for field '%s'",
792 IDENTIFIER_POINTER (DECL_NAME (field)));
793 else
795 DECL_INITIAL (field) = constant;
796 if (TREE_TYPE (constant) != TREE_TYPE (field)
797 && ! (TREE_TYPE (constant) == int_type_node
798 && INTEGRAL_TYPE_P (TREE_TYPE (field))
799 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
800 && ! (TREE_TYPE (constant) == utf8const_ptr_type
801 && TREE_TYPE (field) == string_ptr_type_node))
802 error ("ConstantValue attribute of field '%s' has wrong type",
803 IDENTIFIER_POINTER (DECL_NAME (field)));
804 if (FIELD_FINAL (field))
805 DECL_FIELD_FINAL_IUD (field) = 1;
809 /* Count the number of Unicode chars encoded in a given Ut8 string. */
811 #if 0
813 strLengthUtf8 (char *str, int len)
815 register unsigned char* ptr = (unsigned char*) str;
816 register unsigned char *limit = ptr + len;
817 int str_length = 0;
818 for (; ptr < limit; str_length++) {
819 if (UTF8_GET (ptr, limit) < 0)
820 return -1;
822 return str_length;
824 #endif
827 /* Calculate a hash value for a string encoded in Utf8 format.
828 * This returns the same hash value as specified for java.lang.String.hashCode.
831 static int32
832 hashUtf8String (const char *str, int len)
834 const unsigned char* ptr = (const unsigned char*) str;
835 const unsigned char *limit = ptr + len;
836 int32 hash = 0;
837 for (; ptr < limit;)
839 int ch = UTF8_GET (ptr, limit);
840 /* Updated specification from
841 http://www.javasoft.com/docs/books/jls/clarify.html. */
842 hash = (31 * hash) + ch;
844 return hash;
847 static GTY(()) tree utf8_decl_list = NULL_TREE;
849 tree
850 build_utf8_ref (tree name)
852 const char * name_ptr = IDENTIFIER_POINTER(name);
853 int name_len = IDENTIFIER_LENGTH(name);
854 char buf[60];
855 tree ctype, field = NULL_TREE, str_type, cinit, string;
856 static int utf8_count = 0;
857 int name_hash;
858 tree ref = IDENTIFIER_UTF8_REF (name);
859 tree decl;
860 if (ref != NULL_TREE)
861 return ref;
863 ctype = make_node (RECORD_TYPE);
864 str_type = build_prim_array_type (unsigned_byte_type_node,
865 name_len + 1); /* Allow for final '\0'. */
866 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
867 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
868 PUSH_FIELD (ctype, field, "data", str_type);
869 FINISH_RECORD (ctype);
870 START_RECORD_CONSTRUCTOR (cinit, ctype);
871 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
872 PUSH_FIELD_VALUE (cinit, "hash", build_int_cst (NULL_TREE, name_hash));
873 PUSH_FIELD_VALUE (cinit, "length", build_int_cst (NULL_TREE, name_len));
874 string = build_string (name_len, name_ptr);
875 TREE_TYPE (string) = str_type;
876 PUSH_FIELD_VALUE (cinit, "data", string);
877 FINISH_RECORD_CONSTRUCTOR (cinit);
878 TREE_CONSTANT (cinit) = 1;
879 TREE_INVARIANT (cinit) = 1;
881 /* Generate a unique-enough identifier. */
882 sprintf(buf, "_Utf%d", ++utf8_count);
884 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
885 TREE_STATIC (decl) = 1;
886 DECL_ARTIFICIAL (decl) = 1;
887 DECL_IGNORED_P (decl) = 1;
888 TREE_READONLY (decl) = 1;
889 TREE_THIS_VOLATILE (decl) = 0;
890 DECL_INITIAL (decl) = cinit;
892 if (HAVE_GAS_SHF_MERGE)
894 int decl_size;
895 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
896 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
897 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
898 if (flag_merge_constants && decl_size < 256)
900 char buf[32];
901 int flags = (SECTION_OVERRIDE
902 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
903 sprintf (buf, ".rodata.jutf8.%d", decl_size);
904 named_section_flags (buf, flags);
905 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
909 TREE_CHAIN (decl) = utf8_decl_list;
910 layout_decl (decl, 0);
911 pushdecl (decl);
912 rest_of_decl_compilation (decl, global_bindings_p (), 0);
913 utf8_decl_list = decl;
914 make_decl_rtl (decl);
915 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
916 IDENTIFIER_UTF8_REF (name) = ref;
917 return ref;
920 /* Like build_class_ref, but instead of a direct reference generate a
921 pointer into the constant pool. */
923 static tree
924 build_indirect_class_ref (tree type)
926 int index;
927 tree cl;
928 index = alloc_class_constant (type);
929 cl = build_ref_from_constant_pool (index);
930 return convert (promote_type (class_ptr_type), cl);
933 /* Build a reference to the class TYPE.
934 Also handles primitive types and array types. */
936 tree
937 build_class_ref (tree type)
939 int is_compiled = is_compiled_class (type);
940 if (is_compiled)
942 tree ref, decl_name, decl;
943 if (TREE_CODE (type) == POINTER_TYPE)
944 type = TREE_TYPE (type);
946 if (flag_indirect_dispatch
947 && type != output_class
948 && TREE_CODE (type) == RECORD_TYPE)
949 return build_indirect_class_ref (type);
951 if (TREE_CODE (type) == RECORD_TYPE)
953 if (TYPE_SIZE (type) == error_mark_node)
954 return null_pointer_node;
955 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
956 "", '/', '/', ".class");
957 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
958 if (decl == NULL_TREE)
960 decl = build_decl (VAR_DECL, decl_name, class_type_node);
961 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
962 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
963 TREE_STATIC (decl) = 1;
964 TREE_PUBLIC (decl) = 1;
965 DECL_IGNORED_P (decl) = 1;
966 DECL_ARTIFICIAL (decl) = 1;
967 if (is_compiled == 1)
968 DECL_EXTERNAL (decl) = 1;
969 SET_DECL_ASSEMBLER_NAME (decl,
970 java_mangle_class_field
971 (&temporary_obstack, type));
972 make_decl_rtl (decl);
973 pushdecl_top_level (decl);
976 else
978 const char *name;
979 char buffer[25];
980 if (flag_emit_class_files)
982 const char *prim_class_name;
983 tree prim_class;
984 if (type == char_type_node)
985 prim_class_name = "java.lang.Character";
986 else if (type == boolean_type_node)
987 prim_class_name = "java.lang.Boolean";
988 else if (type == byte_type_node)
989 prim_class_name = "java.lang.Byte";
990 else if (type == short_type_node)
991 prim_class_name = "java.lang.Short";
992 else if (type == int_type_node)
993 prim_class_name = "java.lang.Integer";
994 else if (type == long_type_node)
995 prim_class_name = "java.lang.Long";
996 else if (type == float_type_node)
997 prim_class_name = "java.lang.Float";
998 else if (type == double_type_node)
999 prim_class_name = "java.lang.Double";
1000 else if (type == void_type_node)
1001 prim_class_name = "java.lang.Void";
1002 else
1003 abort ();
1005 prim_class = lookup_class (get_identifier (prim_class_name));
1006 return build3 (COMPONENT_REF, NULL_TREE,
1007 prim_class, TYPE_identifier_node, NULL_TREE);
1009 decl_name = TYPE_NAME (type);
1010 if (TREE_CODE (decl_name) == TYPE_DECL)
1011 decl_name = DECL_NAME (decl_name);
1012 name = IDENTIFIER_POINTER (decl_name);
1013 if (strncmp (name, "promoted_", 9) == 0)
1014 name += 9;
1015 sprintf (buffer, "_Jv_%sClass", name);
1016 decl_name = get_identifier (buffer);
1017 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1018 if (decl == NULL_TREE)
1020 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1021 TREE_STATIC (decl) = 1;
1022 TREE_PUBLIC (decl) = 1;
1023 DECL_EXTERNAL (decl) = 1;
1024 DECL_ARTIFICIAL (decl) = 1;
1025 make_decl_rtl (decl);
1026 pushdecl_top_level (decl);
1030 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1031 return ref;
1033 else
1034 return build_indirect_class_ref (type);
1037 tree
1038 build_static_field_ref (tree fdecl)
1040 tree fclass = DECL_CONTEXT (fdecl);
1041 int is_compiled = is_compiled_class (fclass);
1043 /* Allow static final fields to fold to a constant. When using
1044 -fno-assume-compiled, gcj will sometimes try to fold a field from
1045 an uncompiled class. This is required when the field in question
1046 meets the appropriate criteria for a compile-time constant.
1047 However, currently sometimes gcj is too eager and will end up
1048 returning the field itself, leading to an incorrect external
1049 reference being generated. */
1050 if ((is_compiled && !flag_indirect_dispatch)
1051 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1052 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1053 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1054 && TREE_CONSTANT (DECL_INITIAL (fdecl))))
1056 if (!DECL_RTL_SET_P (fdecl))
1058 if (is_compiled == 1)
1059 DECL_EXTERNAL (fdecl) = 1;
1060 make_decl_rtl (fdecl);
1062 return fdecl;
1065 if (flag_indirect_dispatch)
1067 tree table_index
1068 = build_int_cst (NULL_TREE, get_symbol_table_index
1069 (fdecl, &TYPE_ATABLE_METHODS (output_class)));
1070 tree field_address
1071 = build4 (ARRAY_REF,
1072 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
1073 TYPE_ATABLE_DECL (output_class), table_index,
1074 NULL_TREE, NULL_TREE);
1075 field_address = convert (build_pointer_type (TREE_TYPE (fdecl)),
1076 field_address);
1077 return fold (build1 (INDIRECT_REF, TREE_TYPE (fdecl),
1078 field_address));
1080 else
1082 /* Compile as:
1083 *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1084 tree ref = build_class_ref (fclass);
1085 tree fld;
1086 int field_index = 0;
1087 ref = build1 (INDIRECT_REF, class_type_node, ref);
1088 ref = build3 (COMPONENT_REF, field_ptr_type_node, ref,
1089 lookup_field (&class_type_node, fields_ident),
1090 NULL_TREE);
1092 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1094 if (fld == fdecl)
1095 break;
1096 if (fld == NULL_TREE)
1097 fatal_error ("field '%s' not found in class",
1098 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1099 if (FIELD_STATIC (fld))
1100 field_index++;
1102 field_index *= int_size_in_bytes (field_type_node);
1103 ref = fold (build2 (PLUS_EXPR, field_ptr_type_node,
1104 ref, build_int_cst (NULL_TREE, field_index)));
1105 ref = build1 (INDIRECT_REF, field_type_node, ref);
1106 ref = build3 (COMPONENT_REF, field_info_union_node,
1107 ref, lookup_field (&field_type_node, info_ident),
1108 NULL_TREE);
1109 ref = build3 (COMPONENT_REF, ptr_type_node,
1110 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)),
1111 NULL_TREE);
1112 ref = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (fdecl)), ref);
1113 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1118 get_access_flags_from_decl (tree decl)
1120 int access_flags = 0;
1121 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1123 if (FIELD_STATIC (decl))
1124 access_flags |= ACC_STATIC;
1125 if (FIELD_PUBLIC (decl))
1126 access_flags |= ACC_PUBLIC;
1127 if (FIELD_PROTECTED (decl))
1128 access_flags |= ACC_PROTECTED;
1129 if (FIELD_PRIVATE (decl))
1130 access_flags |= ACC_PRIVATE;
1131 if (FIELD_FINAL (decl))
1132 access_flags |= ACC_FINAL;
1133 if (FIELD_VOLATILE (decl))
1134 access_flags |= ACC_VOLATILE;
1135 if (FIELD_TRANSIENT (decl))
1136 access_flags |= ACC_TRANSIENT;
1137 return access_flags;
1139 if (TREE_CODE (decl) == TYPE_DECL)
1141 if (CLASS_PUBLIC (decl))
1142 access_flags |= ACC_PUBLIC;
1143 if (CLASS_FINAL (decl))
1144 access_flags |= ACC_FINAL;
1145 if (CLASS_SUPER (decl))
1146 access_flags |= ACC_SUPER;
1147 if (CLASS_INTERFACE (decl))
1148 access_flags |= ACC_INTERFACE;
1149 if (CLASS_ABSTRACT (decl))
1150 access_flags |= ACC_ABSTRACT;
1151 if (CLASS_STATIC (decl))
1152 access_flags |= ACC_STATIC;
1153 if (CLASS_PRIVATE (decl))
1154 access_flags |= ACC_PRIVATE;
1155 if (CLASS_PROTECTED (decl))
1156 access_flags |= ACC_PROTECTED;
1157 if (CLASS_STRICTFP (decl))
1158 access_flags |= ACC_STRICT;
1159 return access_flags;
1161 if (TREE_CODE (decl) == FUNCTION_DECL)
1163 if (METHOD_PUBLIC (decl))
1164 access_flags |= ACC_PUBLIC;
1165 if (METHOD_PRIVATE (decl))
1166 access_flags |= ACC_PRIVATE;
1167 if (METHOD_PROTECTED (decl))
1168 access_flags |= ACC_PROTECTED;
1169 if (METHOD_STATIC (decl))
1170 access_flags |= ACC_STATIC;
1171 if (METHOD_FINAL (decl))
1172 access_flags |= ACC_FINAL;
1173 if (METHOD_SYNCHRONIZED (decl))
1174 access_flags |= ACC_SYNCHRONIZED;
1175 if (METHOD_NATIVE (decl))
1176 access_flags |= ACC_NATIVE;
1177 if (METHOD_ABSTRACT (decl))
1178 access_flags |= ACC_ABSTRACT;
1179 if (METHOD_STRICTFP (decl))
1180 access_flags |= ACC_STRICT;
1181 if (METHOD_INVISIBLE (decl))
1182 access_flags |= ACC_INVISIBLE;
1183 return access_flags;
1185 abort ();
1188 static GTY (()) int alias_labelno = 0;
1190 /* Create a private alias for METHOD. Using this alias instead of the method
1191 decl ensures that ncode entries in the method table point to the real function
1192 at runtime, not a PLT entry. */
1194 static tree
1195 make_local_function_alias (tree method)
1197 #ifdef ASM_OUTPUT_DEF
1198 tree alias;
1200 const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1201 char *name = alloca (strlen (method_name) + 2);
1202 char *buf = alloca (strlen (method_name) + 128);
1204 /* Only create aliases for local functions. */
1205 if (DECL_EXTERNAL (method))
1206 return method;
1208 /* Prefix method_name with 'L' for the alias label. */
1209 *name = 'L';
1210 strcpy (name + 1, method_name);
1212 ASM_GENERATE_INTERNAL_LABEL (buf, name, alias_labelno++);
1213 alias = build_decl (FUNCTION_DECL, get_identifier (buf),
1214 TREE_TYPE (method));
1215 DECL_CONTEXT (alias) = NULL;
1216 TREE_READONLY (alias) = TREE_READONLY (method);
1217 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1218 TREE_PUBLIC (alias) = 0;
1219 DECL_EXTERNAL (alias) = 0;
1220 DECL_ARTIFICIAL (alias) = 1;
1221 DECL_INLINE (alias) = 0;
1222 DECL_INITIAL (alias) = error_mark_node;
1223 TREE_ADDRESSABLE (alias) = 1;
1224 TREE_USED (alias) = 1;
1225 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
1226 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
1227 if (!flag_syntax_only)
1228 assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1229 return alias;
1230 #else
1231 return method;
1232 #endif
1235 /** Make reflection data (_Jv_Field) for field FDECL. */
1237 static tree
1238 make_field_value (tree fdecl)
1240 tree finit;
1241 int flags;
1242 tree type = TREE_TYPE (fdecl);
1243 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1245 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1246 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1247 if (resolved)
1248 type = build_class_ref (type);
1249 else
1251 tree signature = build_java_signature (type);
1253 type = build_utf8_ref (unmangle_classname
1254 (IDENTIFIER_POINTER (signature),
1255 IDENTIFIER_LENGTH (signature)));
1257 PUSH_FIELD_VALUE (finit, "type", type);
1259 flags = get_access_flags_from_decl (fdecl);
1260 if (! resolved)
1261 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1263 PUSH_FIELD_VALUE (finit, "accflags", build_int_cst (NULL_TREE, flags));
1264 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1266 PUSH_FIELD_VALUE
1267 (finit, "info",
1268 build_constructor (field_info_union_node,
1269 build_tree_list
1270 ((FIELD_STATIC (fdecl)
1271 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1272 : TYPE_FIELDS (field_info_union_node)),
1273 (FIELD_STATIC (fdecl)
1274 ? build_address_of (fdecl)
1275 : byte_position (fdecl)))));
1277 FINISH_RECORD_CONSTRUCTOR (finit);
1278 return finit;
1281 /** Make reflection data (_Jv_Method) for method MDECL. */
1283 static tree
1284 make_method_value (tree mdecl)
1286 static int method_name_count = 0;
1287 tree minit;
1288 tree index;
1289 tree code;
1290 tree class_decl;
1291 #define ACC_TRANSLATED 0x4000
1292 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1294 class_decl = DECL_CONTEXT (mdecl);
1295 /* For interfaces, the index field contains the dispatch index. */
1296 if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1297 index = build_int_cst (NULL_TREE,
1298 get_interface_method_index (mdecl, class_decl));
1299 else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1300 index = get_method_index (mdecl);
1301 else
1302 index = integer_minus_one_node;
1304 code = null_pointer_node;
1305 if (DECL_RTL_SET_P (mdecl))
1306 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1307 make_local_function_alias (mdecl));
1308 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1309 PUSH_FIELD_VALUE (minit, "name",
1310 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1311 init_identifier_node
1312 : DECL_NAME (mdecl)));
1314 tree signature = build_java_signature (TREE_TYPE (mdecl));
1315 PUSH_FIELD_VALUE (minit, "signature",
1316 (build_utf8_ref
1317 (unmangle_classname
1318 (IDENTIFIER_POINTER(signature),
1319 IDENTIFIER_LENGTH(signature)))));
1321 PUSH_FIELD_VALUE (minit, "accflags", build_int_cst (NULL_TREE, accflags));
1322 PUSH_FIELD_VALUE (minit, "index", index);
1323 PUSH_FIELD_VALUE (minit, "ncode", code);
1326 /* Compute the `throws' information for the method. */
1327 tree table = null_pointer_node;
1328 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1330 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1331 tree iter, type, array;
1332 char buf[60];
1334 table = tree_cons (NULL_TREE, table, NULL_TREE);
1335 for (iter = DECL_FUNCTION_THROWS (mdecl);
1336 iter != NULL_TREE;
1337 iter = TREE_CHAIN (iter))
1339 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1340 tree utf8
1341 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1342 IDENTIFIER_LENGTH (sig)));
1343 table = tree_cons (NULL_TREE, utf8, table);
1345 type = build_prim_array_type (ptr_type_node, length);
1346 table = build_constructor (type, table);
1347 /* Compute something unique enough. */
1348 sprintf (buf, "_methods%d", method_name_count++);
1349 array = build_decl (VAR_DECL, get_identifier (buf), type);
1350 DECL_INITIAL (array) = table;
1351 TREE_STATIC (array) = 1;
1352 DECL_ARTIFICIAL (array) = 1;
1353 DECL_IGNORED_P (array) = 1;
1354 rest_of_decl_compilation (array, 1, 0);
1356 table = build1 (ADDR_EXPR, ptr_type_node, array);
1359 PUSH_FIELD_VALUE (minit, "throws", table);
1362 FINISH_RECORD_CONSTRUCTOR (minit);
1363 return minit;
1366 static tree
1367 get_dispatch_vector (tree type)
1369 tree vtable = TYPE_VTABLE (type);
1371 if (vtable == NULL_TREE)
1373 HOST_WIDE_INT i;
1374 tree method;
1375 tree super = CLASSTYPE_SUPER (type);
1376 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1377 vtable = make_tree_vec (nvirtuals);
1378 TYPE_VTABLE (type) = vtable;
1379 if (super != NULL_TREE)
1381 tree super_vtable = get_dispatch_vector (super);
1383 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1384 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1387 for (method = TYPE_METHODS (type); method != NULL_TREE;
1388 method = TREE_CHAIN (method))
1390 tree method_index = get_method_index (method);
1391 if (method_index != NULL_TREE
1392 && host_integerp (method_index, 0))
1393 TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1397 return vtable;
1400 static tree
1401 get_dispatch_table (tree type, tree this_class_addr)
1403 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1404 tree vtable = get_dispatch_vector (type);
1405 int i, j;
1406 tree list = NULL_TREE;
1407 int nvirtuals = TREE_VEC_LENGTH (vtable);
1408 int arraysize;
1409 tree gc_descr;
1411 for (i = nvirtuals; --i >= 0; )
1413 tree method = TREE_VEC_ELT (vtable, i);
1414 if (METHOD_ABSTRACT (method))
1416 if (! abstract_p)
1417 warning ("%Jabstract method in non-abstract class", method);
1419 if (TARGET_VTABLE_USES_DESCRIPTORS)
1420 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1421 list = tree_cons (NULL_TREE, null_pointer_node, list);
1422 else
1423 list = tree_cons (NULL_TREE, null_pointer_node, list);
1425 else
1427 if (!DECL_RTL_SET_P (method))
1428 make_decl_rtl (method);
1430 if (TARGET_VTABLE_USES_DESCRIPTORS)
1431 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1433 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1434 method, build_int_cst (NULL_TREE, j));
1435 TREE_CONSTANT (fdesc) = 1;
1436 TREE_INVARIANT (fdesc) = 1;
1437 list = tree_cons (NULL_TREE, fdesc, list);
1439 else
1440 list = tree_cons (NULL_TREE,
1441 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1442 method),
1443 list);
1447 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1448 using the Boehm GC we sometimes stash a GC type descriptor
1449 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1450 the emitted byte count during the output to the assembly file. */
1451 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1452 fake "function descriptor". It's first word is the is the class
1453 pointer, and subsequent words (usually one) contain the GC descriptor.
1454 In all other cases, we reserve two extra vtable slots. */
1455 gc_descr = get_boehm_type_descriptor (type);
1456 list = tree_cons (NULL_TREE, gc_descr, list);
1457 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1458 list = tree_cons (NULL_TREE, gc_descr, list);
1459 list = tree_cons (NULL_TREE, this_class_addr, list);
1461 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1462 list = tree_cons (NULL_TREE, null_pointer_node, list);
1463 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1464 list = tree_cons (integer_zero_node, null_pointer_node, list);
1466 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1467 if (TARGET_VTABLE_USES_DESCRIPTORS)
1468 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1469 arraysize += 2;
1470 return build_constructor (build_prim_array_type (nativecode_ptr_type_node,
1471 arraysize), list);
1475 /* Set the method_index for a method decl. */
1476 void
1477 set_method_index (tree decl, tree method_index)
1479 method_index = fold (convert (sizetype, method_index));
1481 if (TARGET_VTABLE_USES_DESCRIPTORS)
1482 /* Add one to skip bogus descriptor for class and GC descriptor. */
1483 method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1484 else
1485 /* Add 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1486 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1488 DECL_VINDEX (decl) = method_index;
1491 /* Get the method_index for a method decl. */
1492 tree
1493 get_method_index (tree decl)
1495 tree method_index = DECL_VINDEX (decl);
1497 if (! method_index)
1498 return NULL;
1500 if (TARGET_VTABLE_USES_DESCRIPTORS)
1501 /* Sub one to skip bogus descriptor for class and GC descriptor. */
1502 method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1503 else
1504 /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1505 method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1507 return method_index;
1510 static int
1511 supers_all_compiled (tree type)
1513 while (type != NULL_TREE)
1515 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1516 return 0;
1517 type = CLASSTYPE_SUPER (type);
1519 return 1;
1522 void
1523 make_class_data (tree type)
1525 tree decl, cons, temp;
1526 tree field, fields_decl;
1527 tree static_fields = NULL_TREE;
1528 tree instance_fields = NULL_TREE;
1529 HOST_WIDE_INT static_field_count = 0;
1530 HOST_WIDE_INT instance_field_count = 0;
1531 HOST_WIDE_INT field_count;
1532 tree field_array_type;
1533 tree method;
1534 tree methods = NULL_TREE;
1535 tree dtable_decl = NULL_TREE;
1536 HOST_WIDE_INT method_count = 0;
1537 tree method_array_type;
1538 tree methods_decl;
1539 tree super;
1540 tree this_class_addr;
1541 tree constant_pool_constructor;
1542 tree interfaces = null_pointer_node;
1543 int interface_len = 0;
1544 tree type_decl = TYPE_NAME (type);
1545 /** Offset from start of virtual function table declaration
1546 to where objects actually point at, following new g++ ABI. */
1547 tree dtable_start_offset = build_int_cst (NULL_TREE,
1548 2 * POINTER_SIZE / BITS_PER_UNIT);
1550 this_class_addr = build_class_ref (type);
1551 decl = TREE_OPERAND (this_class_addr, 0);
1553 /* Build Field array. */
1554 field = TYPE_FIELDS (type);
1555 while (field && DECL_ARTIFICIAL (field))
1556 field = TREE_CHAIN (field); /* Skip dummy fields. */
1557 if (field && DECL_NAME (field) == NULL_TREE)
1558 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1559 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1561 if (! DECL_ARTIFICIAL (field))
1563 tree init = make_field_value (field);
1564 if (FIELD_STATIC (field))
1566 tree initial = DECL_INITIAL (field);
1567 static_field_count++;
1568 static_fields = tree_cons (NULL_TREE, init, static_fields);
1569 /* If the initial value is a string constant,
1570 prevent output_constant from trying to assemble the value. */
1571 if (initial != NULL_TREE
1572 && TREE_TYPE (initial) == string_ptr_type_node)
1573 DECL_INITIAL (field) = NULL_TREE;
1574 rest_of_decl_compilation (field, 1, 1);
1575 DECL_INITIAL (field) = initial;
1577 else
1579 instance_field_count++;
1580 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1584 field_count = static_field_count + instance_field_count;
1585 if (field_count > 0)
1587 static_fields = nreverse (static_fields);
1588 instance_fields = nreverse (instance_fields);
1589 static_fields = chainon (static_fields, instance_fields);
1590 field_array_type = build_prim_array_type (field_type_node, field_count);
1591 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1592 field_array_type);
1593 DECL_INITIAL (fields_decl) = build_constructor (field_array_type,
1594 static_fields);
1595 TREE_STATIC (fields_decl) = 1;
1596 DECL_ARTIFICIAL (fields_decl) = 1;
1597 DECL_IGNORED_P (fields_decl) = 1;
1598 rest_of_decl_compilation (fields_decl, 1, 0);
1600 else
1601 fields_decl = NULL_TREE;
1603 /* Build Method array. */
1604 for (method = TYPE_METHODS (type);
1605 method != NULL_TREE; method = TREE_CHAIN (method))
1607 tree init;
1608 if (METHOD_PRIVATE (method)
1609 && ! flag_keep_inline_functions
1610 && optimize)
1611 continue;
1612 /* Even if we have a decl, we don't necessarily have the code.
1613 This can happen if we inherit a method from a superclass for
1614 which we don't have a .class file. */
1615 if (METHOD_DUMMY (method))
1616 continue;
1617 init = make_method_value (method);
1618 method_count++;
1619 methods = tree_cons (NULL_TREE, init, methods);
1621 method_array_type = build_prim_array_type (method_type_node, method_count);
1622 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1623 method_array_type);
1624 DECL_INITIAL (methods_decl) = build_constructor (method_array_type,
1625 nreverse (methods));
1626 TREE_STATIC (methods_decl) = 1;
1627 DECL_ARTIFICIAL (methods_decl) = 1;
1628 DECL_IGNORED_P (methods_decl) = 1;
1629 rest_of_decl_compilation (methods_decl, 1, 0);
1631 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1632 && !flag_indirect_dispatch)
1634 tree dtable = get_dispatch_table (type, this_class_addr);
1635 dtable_decl = build_dtable_decl (type);
1636 DECL_INITIAL (dtable_decl) = dtable;
1637 TREE_STATIC (dtable_decl) = 1;
1638 DECL_ARTIFICIAL (dtable_decl) = 1;
1639 DECL_IGNORED_P (dtable_decl) = 1;
1640 TREE_PUBLIC (dtable_decl) = 1;
1641 rest_of_decl_compilation (dtable_decl, 1, 0);
1642 if (type == class_type_node)
1643 class_dtable_decl = dtable_decl;
1646 if (class_dtable_decl == NULL_TREE)
1648 class_dtable_decl = build_dtable_decl (class_type_node);
1649 TREE_STATIC (class_dtable_decl) = 1;
1650 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1651 DECL_IGNORED_P (class_dtable_decl) = 1;
1652 if (is_compiled_class (class_type_node) != 2)
1653 DECL_EXTERNAL (class_dtable_decl) = 1;
1654 rest_of_decl_compilation (class_dtable_decl, 1, 0);
1657 super = CLASSTYPE_SUPER (type);
1658 if (super == NULL_TREE)
1659 super = null_pointer_node;
1660 else if (! flag_indirect_dispatch
1661 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1662 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1663 super = build_class_ref (super);
1664 else
1666 int super_index = alloc_class_constant (super);
1667 super = build_int_cst (ptr_type_node, super_index);
1670 /* Build and emit the array of implemented interfaces. */
1671 if (type != object_type_node)
1672 interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
1674 if (interface_len > 0)
1676 tree init = NULL_TREE;
1677 int i;
1678 tree interface_array_type, idecl;
1679 interface_array_type
1680 = build_prim_array_type (class_ptr_type, interface_len);
1681 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1682 interface_array_type);
1684 for (i = interface_len; i > 0; i--)
1686 tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1687 tree iclass = BINFO_TYPE (child);
1688 tree index;
1689 if (! flag_indirect_dispatch
1690 && (assume_compiled
1691 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1692 index = build_class_ref (iclass);
1693 else
1695 int int_index = alloc_class_constant (iclass);
1696 index = build_int_cst (ptr_type_node, int_index);
1698 init = tree_cons (NULL_TREE, index, init);
1700 DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
1701 TREE_STATIC (idecl) = 1;
1702 DECL_ARTIFICIAL (idecl) = 1;
1703 DECL_IGNORED_P (idecl) = 1;
1704 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1705 rest_of_decl_compilation (idecl, 1, 0);
1708 constant_pool_constructor = build_constants_constructor ();
1710 if (flag_indirect_dispatch)
1712 TYPE_OTABLE_DECL (type)
1713 = emit_symbol_table
1714 (DECL_NAME (TYPE_OTABLE_DECL (type)),
1715 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
1716 TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
1718 TYPE_ATABLE_DECL (type)
1719 = emit_symbol_table
1720 (DECL_NAME (TYPE_ATABLE_DECL (type)),
1721 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
1722 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
1724 TYPE_ITABLE_DECL (type)
1725 = emit_symbol_table
1726 (DECL_NAME (TYPE_ITABLE_DECL (type)),
1727 TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
1728 TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
1731 TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1733 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1734 PUSH_FIELD_VALUE (temp, "vtable",
1735 build2 (PLUS_EXPR, dtable_ptr_type,
1736 build1 (ADDR_EXPR, dtable_ptr_type,
1737 class_dtable_decl),
1738 dtable_start_offset));
1739 if (! flag_hash_synchronization)
1740 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1741 FINISH_RECORD_CONSTRUCTOR (temp);
1742 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1743 PUSH_SUPER_VALUE (cons, temp);
1744 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1745 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1746 PUSH_FIELD_VALUE (cons, "accflags",
1747 build_int_cst (NULL_TREE,
1748 get_access_flags_from_decl (type_decl)));
1750 PUSH_FIELD_VALUE (cons, "superclass",
1751 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1752 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1753 PUSH_FIELD_VALUE (cons, "methods",
1754 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1755 PUSH_FIELD_VALUE (cons, "method_count",
1756 build_int_cst (NULL_TREE, method_count));
1758 if (flag_indirect_dispatch)
1759 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1760 else
1761 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1763 PUSH_FIELD_VALUE (cons, "fields",
1764 fields_decl == NULL_TREE ? null_pointer_node
1765 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1766 /* If we're using the binary compatibility ABI we don't know the
1767 size until load time. */
1768 PUSH_FIELD_VALUE (cons, "size_in_bytes",
1769 (flag_indirect_dispatch
1770 ? integer_minus_one_node
1771 : size_in_bytes (type)));
1772 PUSH_FIELD_VALUE (cons, "field_count",
1773 build_int_cst (NULL_TREE, field_count));
1774 PUSH_FIELD_VALUE (cons, "static_field_count",
1775 build_int_cst (NULL_TREE, static_field_count));
1777 if (flag_indirect_dispatch)
1778 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1779 else
1780 PUSH_FIELD_VALUE (cons, "vtable",
1781 dtable_decl == NULL_TREE ? null_pointer_node
1782 : build2 (PLUS_EXPR, dtable_ptr_type,
1783 build1 (ADDR_EXPR, dtable_ptr_type,
1784 dtable_decl),
1785 dtable_start_offset));
1786 if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1788 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1789 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1791 else
1793 PUSH_FIELD_VALUE (cons, "otable",
1794 build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1795 PUSH_FIELD_VALUE (cons, "otable_syms",
1796 build1 (ADDR_EXPR, symbols_array_ptr_type,
1797 TYPE_OTABLE_SYMS_DECL (type)));
1798 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1799 TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1801 if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1803 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1804 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1806 else
1808 PUSH_FIELD_VALUE (cons, "atable",
1809 build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1810 PUSH_FIELD_VALUE (cons, "atable_syms",
1811 build1 (ADDR_EXPR, symbols_array_ptr_type,
1812 TYPE_ATABLE_SYMS_DECL (type)));
1813 TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1814 TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1816 if (TYPE_ITABLE_METHODS(type) == NULL_TREE)
1818 PUSH_FIELD_VALUE (cons, "itable", null_pointer_node);
1819 PUSH_FIELD_VALUE (cons, "itable_syms", null_pointer_node);
1821 else
1823 PUSH_FIELD_VALUE (cons, "itable",
1824 build1 (ADDR_EXPR, itable_ptr_type, TYPE_ITABLE_DECL (type)));
1825 PUSH_FIELD_VALUE (cons, "itable_syms",
1826 build1 (ADDR_EXPR, symbols_array_ptr_type,
1827 TYPE_ITABLE_SYMS_DECL (type)));
1828 TREE_CONSTANT (TYPE_ITABLE_DECL (type)) = 1;
1829 TREE_INVARIANT (TYPE_ITABLE_DECL (type)) = 1;
1832 PUSH_FIELD_VALUE (cons, "catch_classes",
1833 build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
1834 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1835 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1836 PUSH_FIELD_VALUE (cons, "interface_count",
1837 build_int_cst (NULL_TREE, interface_len));
1838 PUSH_FIELD_VALUE
1839 (cons, "state",
1840 convert (byte_type_node,
1841 build_int_cst (NULL_TREE,
1842 flag_indirect_dispatch
1843 ? JV_STATE_PRELOADING
1844 : JV_STATE_COMPILED)));
1846 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1847 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1848 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1849 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1850 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1851 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1854 tree assertion_table_ref;
1855 if (TYPE_ASSERTIONS (type) == NULL)
1856 assertion_table_ref = null_pointer_node;
1857 else
1858 assertion_table_ref = build1 (ADDR_EXPR,
1859 build_pointer_type (assertion_table_type),
1860 emit_assertion_table (type));
1862 PUSH_FIELD_VALUE (cons, "assertion_table", assertion_table_ref);
1865 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1866 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1867 PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1868 PUSH_FIELD_VALUE (cons, "engine", null_pointer_node);
1870 FINISH_RECORD_CONSTRUCTOR (cons);
1872 DECL_INITIAL (decl) = cons;
1874 /* Hash synchronization requires at least 64-bit alignment. */
1875 if (flag_hash_synchronization && POINTER_SIZE < 64)
1876 DECL_ALIGN (decl) = 64;
1878 rest_of_decl_compilation (decl, 1, 0);
1880 TYPE_OTABLE_DECL (type) = NULL_TREE;
1881 TYPE_ATABLE_DECL (type) = NULL_TREE;
1882 TYPE_CTABLE_DECL (type) = NULL_TREE;
1885 void
1886 finish_class (void)
1888 if (TYPE_VERIFY_METHOD (output_class))
1890 tree verify_method = TYPE_VERIFY_METHOD (output_class);
1891 DECL_SAVED_TREE (verify_method)
1892 = add_stmt_to_compound (DECL_SAVED_TREE (verify_method), void_type_node,
1893 build (RETURN_EXPR, void_type_node, NULL));
1894 java_genericize (verify_method);
1895 cgraph_finalize_function (verify_method, false);
1896 TYPE_ASSERTIONS (current_class) = NULL;
1899 java_expand_catch_classes (current_class);
1901 current_function_decl = NULL_TREE;
1902 make_class_data (current_class);
1903 register_class ();
1904 rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
1907 /* Return 2 if CLASS is compiled by this compilation job;
1908 return 1 if CLASS can otherwise be assumed to be compiled;
1909 return 0 if we cannot assume that CLASS is compiled.
1910 Returns 1 for primitive and 0 for array types. */
1912 is_compiled_class (tree class)
1914 int seen_in_zip;
1915 if (TREE_CODE (class) == POINTER_TYPE)
1916 class = TREE_TYPE (class);
1917 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1918 return 1;
1919 if (TYPE_ARRAY_P (class))
1920 return 0;
1921 if (class == current_class)
1922 return 2;
1924 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1925 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1927 /* The class was seen in the current ZIP file and will be
1928 available as a compiled class in the future but may not have
1929 been loaded already. Load it if necessary. This prevent
1930 build_class_ref () from crashing. */
1932 if (seen_in_zip && !CLASS_LOADED_P (class))
1933 load_class (class, 1);
1935 /* We return 2 for class seen in ZIP and class from files
1936 belonging to the same compilation unit */
1937 return 2;
1940 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1942 if (!CLASS_LOADED_P (class))
1944 if (CLASS_FROM_SOURCE_P (class))
1945 safe_layout_class (class);
1946 else
1947 load_class (class, 1);
1949 return 1;
1952 return 0;
1955 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1957 tree
1958 build_dtable_decl (tree type)
1960 tree dtype;
1962 /* We need to build a new dtable type so that its size is uniquely
1963 computed when we're dealing with the class for real and not just
1964 faking it (like java.lang.Class during the initialization of the
1965 compiler.) We know we're not faking a class when CURRENT_CLASS is
1966 TYPE. */
1967 if (current_class == type)
1969 tree dummy = NULL_TREE;
1970 int n;
1972 dtype = make_node (RECORD_TYPE);
1974 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1975 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1977 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1978 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1980 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1981 TREE_CHAIN (dummy) = tmp_field;
1982 DECL_CONTEXT (tmp_field) = dtype;
1983 DECL_ARTIFICIAL (tmp_field) = 1;
1984 dummy = tmp_field;
1987 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1988 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1990 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1991 TREE_CHAIN (dummy) = tmp_field;
1992 DECL_CONTEXT (tmp_field) = dtype;
1993 DECL_ARTIFICIAL (tmp_field) = 1;
1994 dummy = tmp_field;
1997 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
1998 if (TARGET_VTABLE_USES_DESCRIPTORS)
1999 n *= TARGET_VTABLE_USES_DESCRIPTORS;
2001 PUSH_FIELD (dtype, dummy, "methods",
2002 build_prim_array_type (nativecode_ptr_type_node, n));
2003 layout_type (dtype);
2005 else
2006 dtype = dtable_type;
2008 return build_decl (VAR_DECL,
2009 java_mangle_vtable (&temporary_obstack, type), dtype);
2012 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2013 fields inherited from SUPER_CLASS. */
2015 void
2016 push_super_field (tree this_class, tree super_class)
2018 tree base_decl;
2019 /* Don't insert the field if we're just re-laying the class out. */
2020 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2021 return;
2022 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
2023 DECL_IGNORED_P (base_decl) = 1;
2024 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2025 TYPE_FIELDS (this_class) = base_decl;
2026 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2027 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2030 /* Handle the different manners we may have to lay out a super class. */
2032 static tree
2033 maybe_layout_super_class (tree super_class, tree this_class)
2035 if (TREE_CODE (super_class) == RECORD_TYPE)
2037 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
2038 safe_layout_class (super_class);
2039 if (!CLASS_LOADED_P (super_class))
2040 load_class (super_class, 1);
2042 /* We might have to layout the class before its dependency on
2043 the super class gets resolved by java_complete_class */
2044 else if (TREE_CODE (super_class) == POINTER_TYPE)
2046 if (TREE_TYPE (super_class) != NULL_TREE)
2047 super_class = TREE_TYPE (super_class);
2048 else
2050 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
2051 we give it one. */
2052 tree this_wrap = NULL_TREE;
2054 if (this_class)
2056 tree this_decl = TYPE_NAME (this_class);
2057 #ifdef USE_MAPPED_LOCATION
2058 this_wrap = build_expr_wfl (this_class,
2059 DECL_SOURCE_LOCATION (this_decl));
2060 #else
2061 this_wrap = build_expr_wfl (this_class,
2062 DECL_SOURCE_FILE (this_decl),
2063 DECL_SOURCE_LINE (this_decl), 0);
2064 #endif
2066 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
2067 super_class, NULL_TREE, this_wrap);
2068 if (!super_class)
2069 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
2070 super_class = TREE_TYPE (super_class);
2073 if (!TYPE_SIZE (super_class))
2074 safe_layout_class (super_class);
2076 return super_class;
2079 void
2080 layout_class (tree this_class)
2082 tree super_class = CLASSTYPE_SUPER (this_class);
2083 tree field;
2085 class_list = tree_cons (this_class, NULL_TREE, class_list);
2086 if (CLASS_BEING_LAIDOUT (this_class))
2088 char buffer [1024];
2089 char *report;
2090 tree current;
2092 sprintf (buffer, " with '%s'",
2093 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2094 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2096 for (current = TREE_CHAIN (class_list); current;
2097 current = TREE_CHAIN (current))
2099 tree decl = TYPE_NAME (TREE_PURPOSE (current));
2100 sprintf (buffer, "\n which inherits from '%s' (%s:%d)",
2101 IDENTIFIER_POINTER (DECL_NAME (decl)),
2102 DECL_SOURCE_FILE (decl),
2103 DECL_SOURCE_LINE (decl));
2104 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2106 obstack_1grow (&temporary_obstack, '\0');
2107 report = obstack_finish (&temporary_obstack);
2108 cyclic_inheritance_report = ggc_strdup (report);
2109 obstack_free (&temporary_obstack, report);
2110 TYPE_SIZE (this_class) = error_mark_node;
2111 return;
2113 CLASS_BEING_LAIDOUT (this_class) = 1;
2115 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2117 tree maybe_super_class
2118 = maybe_layout_super_class (super_class, this_class);
2119 if (maybe_super_class == NULL
2120 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2122 TYPE_SIZE (this_class) = error_mark_node;
2123 CLASS_BEING_LAIDOUT (this_class) = 0;
2124 class_list = TREE_CHAIN (class_list);
2125 return;
2127 if (TYPE_SIZE (this_class) == NULL_TREE)
2128 push_super_field (this_class, maybe_super_class);
2131 for (field = TYPE_FIELDS (this_class);
2132 field != NULL_TREE; field = TREE_CHAIN (field))
2134 if (FIELD_STATIC (field))
2136 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
2137 SET_DECL_ASSEMBLER_NAME (field,
2138 java_mangle_decl
2139 (&temporary_obstack, field));
2143 layout_type (this_class);
2145 /* Also recursively load/layout any superinterfaces, but only if
2146 class was loaded from bytecode. The source parser will take care
2147 of this itself. */
2148 if (!CLASS_FROM_SOURCE_P (this_class))
2150 int i;
2151 if (TYPE_BINFO (this_class))
2153 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2155 tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2156 tree super_interface = BINFO_TYPE (binfo);
2157 tree maybe_super_interface
2158 = maybe_layout_super_class (super_interface, NULL_TREE);
2159 if (maybe_super_interface == NULL
2160 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2162 TYPE_SIZE (this_class) = error_mark_node;
2163 CLASS_BEING_LAIDOUT (this_class) = 0;
2164 class_list = TREE_CHAIN (class_list);
2165 return;
2171 /* Convert the size back to an SI integer value. */
2172 TYPE_SIZE_UNIT (this_class) =
2173 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2175 CLASS_BEING_LAIDOUT (this_class) = 0;
2176 class_list = TREE_CHAIN (class_list);
2179 static void
2180 add_miranda_methods (tree base_class, tree search_class)
2182 int i;
2183 tree binfo, base_binfo;
2185 if (!CLASS_PARSED_P (search_class))
2186 load_class (search_class, 1);
2188 for (binfo = TYPE_BINFO (search_class), i = 1;
2189 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2191 tree method_decl;
2192 tree elt = BINFO_TYPE (base_binfo);
2194 /* FIXME: This is totally bogus. We should not be handling
2195 Miranda methods at all if we're using the BC ABI. */
2196 if (TYPE_DUMMY (elt))
2197 continue;
2199 /* Ensure that interface methods are seen in declared order. */
2200 if (!CLASS_LOADED_P (elt))
2201 load_class (elt, 1);
2202 layout_class_methods (elt);
2204 /* All base classes will have been laid out at this point, so the order
2205 will be correct. This code must match similar layout code in the
2206 runtime. */
2207 for (method_decl = TYPE_METHODS (elt);
2208 method_decl; method_decl = TREE_CHAIN (method_decl))
2210 tree sig, override;
2212 /* An interface can have <clinit>. */
2213 if (ID_CLINIT_P (DECL_NAME (method_decl)))
2214 continue;
2216 sig = build_java_argument_signature (TREE_TYPE (method_decl));
2217 override = lookup_argument_method (base_class,
2218 DECL_NAME (method_decl), sig);
2219 if (override == NULL_TREE)
2221 /* Found a Miranda method. Add it. */
2222 tree new_method;
2223 sig = build_java_signature (TREE_TYPE (method_decl));
2224 new_method
2225 = add_method (base_class,
2226 get_access_flags_from_decl (method_decl),
2227 DECL_NAME (method_decl), sig);
2228 METHOD_INVISIBLE (new_method) = 1;
2232 /* Try superinterfaces. */
2233 add_miranda_methods (base_class, elt);
2237 void
2238 layout_class_methods (tree this_class)
2240 tree method_decl, dtable_count;
2241 tree super_class, type_name;
2243 if (TYPE_NVIRTUALS (this_class))
2244 return;
2246 super_class = CLASSTYPE_SUPER (this_class);
2248 if (super_class)
2250 super_class = maybe_layout_super_class (super_class, this_class);
2251 if (!TYPE_NVIRTUALS (super_class))
2252 layout_class_methods (super_class);
2253 dtable_count = TYPE_NVIRTUALS (super_class);
2255 else
2256 dtable_count = integer_zero_node;
2258 type_name = TYPE_NAME (this_class);
2259 if (!flag_indirect_dispatch
2260 && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2262 /* An abstract class can have methods which are declared only in
2263 an implemented interface. These are called "Miranda
2264 methods". We make a dummy method entry for such methods
2265 here. */
2266 add_miranda_methods (this_class, this_class);
2269 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2271 for (method_decl = TYPE_METHODS (this_class);
2272 method_decl; method_decl = TREE_CHAIN (method_decl))
2273 dtable_count = layout_class_method (this_class, super_class,
2274 method_decl, dtable_count);
2276 TYPE_NVIRTUALS (this_class) = dtable_count;
2279 /* Return the index of METHOD in INTERFACE. This index begins at 1 and is used as an
2280 argument for _Jv_LookupInterfaceMethodIdx(). */
2282 get_interface_method_index (tree method, tree interface)
2284 tree meth;
2285 int i = 1;
2287 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
2289 if (meth == method)
2290 return i;
2291 if (meth == NULL_TREE)
2292 abort ();
2296 /* Lay METHOD_DECL out, returning a possibly new value of
2297 DTABLE_COUNT. Also mangle the method's name. */
2299 tree
2300 layout_class_method (tree this_class, tree super_class,
2301 tree method_decl, tree dtable_count)
2303 tree method_name = DECL_NAME (method_decl);
2305 TREE_PUBLIC (method_decl) = 1;
2306 /* Considered external until we know what classes are being
2307 compiled into this object file. */
2308 DECL_EXTERNAL (method_decl) = 1;
2310 /* This is a good occasion to mangle the method's name */
2311 SET_DECL_ASSEMBLER_NAME (method_decl,
2312 java_mangle_decl (&temporary_obstack,
2313 method_decl));
2314 /* We don't generate a RTL for the method if it's abstract, or if
2315 it's an interface method that isn't clinit. */
2316 if (! METHOD_ABSTRACT (method_decl)
2317 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2318 && (DECL_CLINIT_P (method_decl))))
2319 make_decl_rtl (method_decl);
2321 if (ID_INIT_P (method_name))
2323 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2324 const char *ptr;
2325 for (ptr = p; *ptr; )
2327 if (*ptr++ == '.')
2328 p = ptr;
2330 DECL_CONSTRUCTOR_P (method_decl) = 1;
2331 build_java_argument_signature (TREE_TYPE (method_decl));
2333 else if (! METHOD_STATIC (method_decl))
2335 tree method_sig =
2336 build_java_argument_signature (TREE_TYPE (method_decl));
2337 bool method_override = false;
2338 tree super_method = lookup_argument_method (super_class, method_name,
2339 method_sig);
2340 if (super_method != NULL_TREE
2341 && ! METHOD_DUMMY (super_method))
2343 method_override = true;
2344 if (! METHOD_PUBLIC (super_method) &&
2345 ! METHOD_PROTECTED (super_method))
2347 /* Don't override private method, or default-access method in
2348 another package. */
2349 if (METHOD_PRIVATE (super_method) ||
2350 ! in_same_package (TYPE_NAME (this_class),
2351 TYPE_NAME (super_class)))
2352 method_override = false;
2355 if (method_override)
2357 tree method_index = get_method_index (super_method);
2358 set_method_index (method_decl, method_index);
2359 if (method_index == NULL_TREE
2360 && !CLASS_FROM_SOURCE_P (this_class)
2361 && ! DECL_ARTIFICIAL (super_method))
2362 error ("%Jnon-static method '%D' overrides static method",
2363 method_decl, method_decl);
2365 else if (this_class == object_type_node
2366 && (METHOD_FINAL (method_decl)
2367 || METHOD_PRIVATE (method_decl)))
2369 /* We don't generate vtable entries for final Object
2370 methods. This is simply to save space, since every
2371 object would otherwise have to define them. */
2373 else if (! METHOD_PRIVATE (method_decl)
2374 && dtable_count)
2376 /* We generate vtable entries for final methods because they
2377 may one day be changed to non-final. */
2378 set_method_index (method_decl, dtable_count);
2379 dtable_count = fold (build2 (PLUS_EXPR, integer_type_node,
2380 dtable_count, integer_one_node));
2384 return dtable_count;
2387 void
2388 register_class (void)
2390 /* END does not need to be registered with the garbage collector
2391 because it always points into the list given by REGISTERED_CLASS,
2392 and that variable is registered with the collector. */
2393 static tree end;
2394 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2395 tree current = copy_node (node);
2397 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2398 if (!registered_class)
2399 registered_class = current;
2400 else
2401 TREE_CHAIN (end) = current;
2403 end = current;
2406 /* Emit something to register classes at start-up time.
2408 The preferred mechanism is through the .jcr section, which contain
2409 a list of pointers to classes which get registered during constructor
2410 invocation time.
2412 The fallback mechanism is to add statements to *LIST_P to call
2413 _Jv_RegisterClass for each class in this file. These statements will
2414 be added to a static constructor function for this translation unit. */
2416 void
2417 emit_register_classes (tree *list_p)
2419 if (registered_class == NULL)
2420 return;
2422 /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2423 TARGET_ASM_NAMED_SECTION, else 0. Some targets meet those conditions
2424 but lack suitable crtbegin/end objects or linker support. These
2425 targets can overide the default in tm.h to use the fallback mechanism. */
2426 if (TARGET_USE_JCR_SECTION)
2428 #ifdef JCR_SECTION_NAME
2429 tree t;
2430 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2431 assemble_align (POINTER_SIZE);
2432 for (t = registered_class; t; t = TREE_CHAIN (t))
2433 assemble_integer (XEXP (DECL_RTL (t), 0),
2434 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2435 #else
2436 /* A target has defined TARGET_USE_JCR_SECTION, but doesn't have a
2437 JCR_SECTION_NAME. */
2438 abort ();
2439 #endif
2441 else
2443 tree klass, t, register_class_fn;
2445 t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2446 t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2447 TREE_PUBLIC (t) = 1;
2448 DECL_EXTERNAL (t) = 1;
2449 register_class_fn = t;
2451 for (klass = registered_class; klass; klass = TREE_CHAIN (klass))
2453 t = build_fold_addr_expr (klass);
2454 t = tree_cons (NULL, t, NULL);
2455 t = build_function_call_expr (register_class_fn, t);
2456 append_to_statement_list (t, list_p);
2461 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2463 static tree
2464 build_symbol_entry (tree decl)
2466 tree clname, name, signature, sym;
2467 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2468 /* ??? Constructors are given the name foo.foo all the way through
2469 the compiler, but in the method table they're all renamed
2470 foo.<init>. So, we have to do the same here unless we want an
2471 unresolved reference at runtime. */
2472 name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
2473 && DECL_CONSTRUCTOR_P (decl))
2474 ? init_identifier_node
2475 : DECL_NAME (decl));
2476 signature = build_java_signature (TREE_TYPE (decl));
2477 signature = build_utf8_ref (unmangle_classname
2478 (IDENTIFIER_POINTER (signature),
2479 IDENTIFIER_LENGTH (signature)));
2481 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2482 PUSH_FIELD_VALUE (sym, "clname", clname);
2483 PUSH_FIELD_VALUE (sym, "name", name);
2484 PUSH_FIELD_VALUE (sym, "signature", signature);
2485 FINISH_RECORD_CONSTRUCTOR (sym);
2486 TREE_CONSTANT (sym) = 1;
2487 TREE_INVARIANT (sym) = 1;
2489 return sym;
2492 /* Emit a symbol table: used by -findirect-dispatch. */
2494 tree
2495 emit_symbol_table (tree name, tree the_table, tree decl_list,
2496 tree the_syms_decl, tree the_array_element_type,
2497 int element_size)
2499 tree method_list, method, table, list, null_symbol;
2500 tree table_size, the_array_type;
2501 int index;
2503 /* Only emit a table if this translation unit actually made any
2504 references via it. */
2505 if (decl_list == NULL_TREE)
2506 return the_table;
2508 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2509 index = 0;
2510 method_list = decl_list;
2511 list = NULL_TREE;
2512 while (method_list != NULL_TREE)
2514 method = TREE_VALUE (method_list);
2515 list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2516 method_list = TREE_CHAIN (method_list);
2517 index++;
2520 /* Terminate the list with a "null" entry. */
2521 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2522 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2523 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2524 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2525 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2526 TREE_CONSTANT (null_symbol) = 1;
2527 TREE_INVARIANT (null_symbol) = 1;
2528 list = tree_cons (NULL_TREE, null_symbol, list);
2530 /* Put the list in the right order and make it a constructor. */
2531 list = nreverse (list);
2532 table = build_constructor (symbols_array_type, list);
2534 /* Make it the initial value for otable_syms and emit the decl. */
2535 DECL_INITIAL (the_syms_decl) = table;
2536 DECL_ARTIFICIAL (the_syms_decl) = 1;
2537 DECL_IGNORED_P (the_syms_decl) = 1;
2538 rest_of_decl_compilation (the_syms_decl, 1, 0);
2540 /* Now that its size is known, redefine the table as an
2541 uninitialized static array of INDEX + 1 elements. The extra entry
2542 is used by the runtime to track whether the table has been
2543 initialized. */
2544 table_size
2545 = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2546 the_array_type = build_array_type (the_array_element_type, table_size);
2547 the_table = build_decl (VAR_DECL, name, the_array_type);
2548 TREE_STATIC (the_table) = 1;
2549 TREE_READONLY (the_table) = 1;
2550 rest_of_decl_compilation (the_table, 1, 0);
2552 return the_table;
2555 /* Make an entry for the catch_classes list. */
2556 tree
2557 make_catch_class_record (tree catch_class, tree classname)
2559 tree entry;
2560 tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2561 START_RECORD_CONSTRUCTOR (entry, type);
2562 PUSH_FIELD_VALUE (entry, "address", catch_class);
2563 PUSH_FIELD_VALUE (entry, "classname", classname);
2564 FINISH_RECORD_CONSTRUCTOR (entry);
2565 return entry;
2569 /* Generate the list of Throwable classes that are caught by exception
2570 handlers in this class. */
2571 tree
2572 emit_catch_table (tree this_class)
2574 tree table, table_size, array_type;
2575 TYPE_CATCH_CLASSES (this_class) =
2576 tree_cons (NULL,
2577 make_catch_class_record (null_pointer_node, null_pointer_node),
2578 TYPE_CATCH_CLASSES (this_class));
2579 TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2580 TYPE_CATCH_CLASSES (this_class) =
2581 tree_cons (NULL,
2582 make_catch_class_record (null_pointer_node, null_pointer_node),
2583 TYPE_CATCH_CLASSES (this_class));
2584 table_size = build_index_type
2585 (build_int_cst (NULL_TREE,
2586 list_length (TYPE_CATCH_CLASSES (this_class))));
2587 array_type
2588 = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2589 table_size);
2590 table =
2591 build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2592 DECL_INITIAL (table) =
2593 build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
2594 TREE_STATIC (table) = 1;
2595 TREE_READONLY (table) = 1;
2596 DECL_IGNORED_P (table) = 1;
2597 rest_of_decl_compilation (table, 1, 0);
2598 return table;
2601 /* Given a type, return the signature used by
2602 _Jv_FindClassFromSignature() in libgcj. This isn't exactly the
2603 same as build_java_signature() because we want the canonical array
2604 type. */
2606 static tree
2607 build_signature_for_libgcj (tree type)
2609 tree sig, ref;
2611 sig = build_java_signature (type);
2612 ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
2613 IDENTIFIER_LENGTH (sig)));
2614 return ref;
2617 /* Add an entry to the type assertion table. Callback used during hashtable
2618 traversal. */
2620 static int
2621 add_assertion_table_entry (void **htab_entry, void *ptr)
2623 tree entry;
2624 tree code_val, op1_utf8, op2_utf8;
2625 tree *list = (tree *) ptr;
2626 type_assertion *as = (type_assertion *) *htab_entry;
2628 code_val = build_int_cst (NULL_TREE, as->assertion_code);
2630 if (as->op1 == NULL_TREE)
2631 op1_utf8 = null_pointer_node;
2632 else
2633 op1_utf8 = build_signature_for_libgcj (as->op1);
2635 if (as->op2 == NULL_TREE)
2636 op2_utf8 = null_pointer_node;
2637 else
2638 op2_utf8 = build_signature_for_libgcj (as->op2);
2640 START_RECORD_CONSTRUCTOR (entry, assertion_entry_type);
2641 PUSH_FIELD_VALUE (entry, "assertion_code", code_val);
2642 PUSH_FIELD_VALUE (entry, "op1", op1_utf8);
2643 PUSH_FIELD_VALUE (entry, "op2", op2_utf8);
2644 FINISH_RECORD_CONSTRUCTOR (entry);
2646 *list = tree_cons (NULL_TREE, entry, *list);
2647 return true;
2650 /* Generate the type assertion table for CLASS, and return its DECL. */
2652 static tree
2653 emit_assertion_table (tree class)
2655 tree null_entry, ctor, table_decl;
2656 tree list = NULL_TREE;
2657 htab_t assertions_htab = TYPE_ASSERTIONS (class);
2659 /* Iterate through the hash table. */
2660 htab_traverse (assertions_htab, add_assertion_table_entry, &list);
2662 /* Finish with a null entry. */
2663 START_RECORD_CONSTRUCTOR (null_entry, assertion_entry_type);
2664 PUSH_FIELD_VALUE (null_entry, "assertion_code", integer_zero_node);
2665 PUSH_FIELD_VALUE (null_entry, "op1", null_pointer_node);
2666 PUSH_FIELD_VALUE (null_entry, "op2", null_pointer_node);
2667 FINISH_RECORD_CONSTRUCTOR (null_entry);
2669 list = tree_cons (NULL_TREE, null_entry, list);
2671 /* Put the list in the right order and make it a constructor. */
2672 list = nreverse (list);
2673 ctor = build_constructor (assertion_table_type, list);
2675 table_decl = build_decl (VAR_DECL, mangled_classname ("_type_assert_", class),
2676 assertion_table_type);
2678 TREE_STATIC (table_decl) = 1;
2679 TREE_READONLY (table_decl) = 1;
2680 TREE_CONSTANT (table_decl) = 1;
2681 DECL_IGNORED_P (table_decl) = 1;
2683 DECL_INITIAL (table_decl) = ctor;
2684 DECL_ARTIFICIAL (table_decl) = 1;
2685 rest_of_decl_compilation (table_decl, 1, 0);
2687 return table_decl;
2690 void
2691 init_class_processing (void)
2693 fields_ident = get_identifier ("fields");
2694 info_ident = get_identifier ("info");
2696 gcc_obstack_init (&temporary_obstack);
2699 static hashval_t java_treetreehash_hash (const void *);
2700 static int java_treetreehash_compare (const void *, const void *);
2702 /* A hash table mapping trees to trees. Used generally. */
2704 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2706 static hashval_t
2707 java_treetreehash_hash (const void *k_p)
2709 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2710 return JAVA_TREEHASHHASH_H (k->key);
2713 static int
2714 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2716 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2717 tree k2 = (tree) k2_p;
2718 return (k1->key == k2);
2721 tree
2722 java_treetreehash_find (htab_t ht, tree t)
2724 struct treetreehash_entry *e;
2725 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2726 e = htab_find_with_hash (ht, t, hv);
2727 if (e == NULL)
2728 return NULL;
2729 else
2730 return e->value;
2733 tree *
2734 java_treetreehash_new (htab_t ht, tree t)
2736 void **e;
2737 struct treetreehash_entry *tthe;
2738 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2740 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2741 if (*e == NULL)
2743 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2744 tthe->key = t;
2745 *e = tthe;
2747 else
2748 tthe = (struct treetreehash_entry *) *e;
2749 return &tthe->value;
2752 htab_t
2753 java_treetreehash_create (size_t size, int gc)
2755 if (gc)
2756 return htab_create_ggc (size, java_treetreehash_hash,
2757 java_treetreehash_compare, NULL);
2758 else
2759 return htab_create_alloc (size, java_treetreehash_hash,
2760 java_treetreehash_compare, free, xcalloc, free);
2763 /* Break down qualified IDENTIFIER into package and class-name components.
2764 For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
2765 "pkg.foo", and RIGHT to "Bar". */
2768 split_qualified_name (tree *left, tree *right, tree source)
2770 char *p, *base;
2771 int l = IDENTIFIER_LENGTH (source);
2773 base = alloca (l + 1);
2774 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
2776 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
2777 p = base + l - 1;
2778 while (*p != '.' && p != base)
2779 p--;
2781 /* We didn't find a '.'. Return an error. */
2782 if (p == base)
2783 return 1;
2785 *p = '\0';
2786 if (right)
2787 *right = get_identifier (p+1);
2788 *left = get_identifier (base);
2790 return 0;
2793 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
2794 if the classes are from the same package. */
2797 in_same_package (tree name1, tree name2)
2799 tree tmp;
2800 tree pkg1;
2801 tree pkg2;
2803 if (TREE_CODE (name1) == TYPE_DECL)
2804 name1 = DECL_NAME (name1);
2805 if (TREE_CODE (name2) == TYPE_DECL)
2806 name2 = DECL_NAME (name2);
2808 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
2809 /* One in empty package. */
2810 return 0;
2812 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
2813 /* Both in empty package. */
2814 return 1;
2816 split_qualified_name (&pkg1, &tmp, name1);
2817 split_qualified_name (&pkg2, &tmp, name2);
2819 return (pkg1 == pkg2);
2822 #include "gt-java-class.h"