2002-04-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / java / class.c
blobc8290eabf2b99c27107d9a15beed2eac66e21538
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC 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 GNU CC 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 GNU CC; 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 "tree.h"
31 #include "rtl.h"
32 #include "flags.h"
33 #include "java-tree.h"
34 #include "jcf.h"
35 #include "obstack.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "parse.h"
39 #include "function.h"
40 #include "ggc.h"
41 #include "stdio.h"
42 #include "target.h"
44 /* DOS brain-damage */
45 #ifndef O_BINARY
46 #define O_BINARY 0 /* MS-DOS brain-damage */
47 #endif
49 static tree make_method_value PARAMS ((tree));
50 static tree build_java_method_type PARAMS ((tree, tree, int));
51 static int32 hashUtf8String PARAMS ((const char *, int));
52 static tree make_field_value PARAMS ((tree));
53 static tree get_dispatch_vector PARAMS ((tree));
54 static tree get_dispatch_table PARAMS ((tree, tree));
55 static void add_interface_do PARAMS ((tree, tree, int));
56 static tree maybe_layout_super_class PARAMS ((tree, tree));
57 static int assume_compiled PARAMS ((const char *));
58 static struct hash_entry *init_test_hash_newfunc PARAMS ((struct hash_entry *,
59 struct hash_table *,
60 hash_table_key));
61 static tree build_method_symbols_entry PARAMS ((tree));
63 static rtx registerClass_libfunc;
64 static rtx registerResource_libfunc;
66 extern struct obstack permanent_obstack;
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 assume_compiled_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 assume_compiled_node_struct
79 /* The class or package name. */
80 const char *ident;
82 /* Non-zero if this represents an exclusion. */
83 int excludep;
85 /* Pointers to other nodes in the tree. */
86 struct assume_compiled_node_struct *parent;
87 struct assume_compiled_node_struct *sibling;
88 struct assume_compiled_node_struct *child;
89 } assume_compiled_node;
91 static assume_compiled_node *find_assume_compiled_node
92 PARAMS ((assume_compiled_node *, const char *));
94 /* This is the root of the include/exclude tree. */
96 static assume_compiled_node *assume_compiled_tree;
98 static tree class_roots[5]
99 = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
100 #define registered_class class_roots[0]
101 #define fields_ident class_roots[1] /* get_identifier ("fields") */
102 #define info_ident class_roots[2] /* get_identifier ("info") */
103 #define class_list class_roots[3]
104 #define class_dtable_decl class_roots[4]
106 /* Return the node that most closely represents the class whose name
107 is IDENT. Start the search from NODE. Return NULL if an
108 appropriate node does not exist. */
110 static assume_compiled_node *
111 find_assume_compiled_node (node, ident)
112 assume_compiled_node *node;
113 const char *ident;
115 while (node)
117 size_t node_ident_length = strlen (node->ident);
119 /* node_ident_length is zero at the root of the tree. If the
120 identifiers are the same length, then we have matching
121 classes. Otherwise check if we've matched an enclosing
122 package name. */
124 if (node_ident_length == 0
125 || (strncmp (ident, node->ident, node_ident_length) == 0
126 && (strlen (ident) == node_ident_length
127 || ident[node_ident_length] == '.')))
129 /* We've found a match, however, there might be a more
130 specific match. */
132 assume_compiled_node *found = find_assume_compiled_node (node->child,
133 ident);
134 if (found)
135 return found;
136 else
137 return node;
140 /* No match yet. Continue through the sibling list. */
141 node = node->sibling;
144 /* No match at all in this tree. */
145 return NULL;
148 /* Add a new IDENT to the include/exclude tree. It's an exclusion
149 if EXCLUDEP is non-zero. */
151 void
152 add_assume_compiled (ident, excludep)
153 const char *ident;
154 int excludep;
156 assume_compiled_node *parent;
157 assume_compiled_node *node =
158 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
160 node->ident = xstrdup (ident);
161 node->excludep = excludep;
162 node->child = NULL;
164 /* Create the root of the tree if it doesn't exist yet. */
166 if (NULL == assume_compiled_tree)
168 assume_compiled_tree =
169 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
170 assume_compiled_tree->ident = "";
171 assume_compiled_tree->excludep = 0;
172 assume_compiled_tree->sibling = NULL;
173 assume_compiled_tree->child = NULL;
174 assume_compiled_tree->parent = NULL;
177 /* Calling the function with the empty string means we're setting
178 excludep for the root of the hierarchy. */
180 if (0 == ident[0])
182 assume_compiled_tree->excludep = excludep;
183 return;
186 /* Find the parent node for this new node. PARENT will either be a
187 class or a package name. Adjust PARENT accordingly. */
189 parent = find_assume_compiled_node (assume_compiled_tree, ident);
190 if (ident[strlen (parent->ident)] != '.')
191 parent = parent->parent;
193 /* Insert NODE into the tree. */
195 node->parent = parent;
196 node->sibling = parent->child;
197 parent->child = node;
200 /* Returns non-zero if IDENT is the name of a class that the compiler
201 should assume has been compiled to FIXME */
203 static int
204 assume_compiled (ident)
205 const char *ident;
207 assume_compiled_node *i;
208 int result;
210 if (NULL == assume_compiled_tree)
211 return 1;
213 i = find_assume_compiled_node (assume_compiled_tree,
214 ident);
216 result = ! i->excludep;
218 return (result);
221 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
222 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
223 Also, PREFIX is prepended, and SUFFIX is appended. */
225 tree
226 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
227 const char* old_name;
228 int old_length;
229 const char *prefix;
230 int old_char;
231 int new_char;
232 const char *suffix;
234 int prefix_len = strlen (prefix);
235 int suffix_len = strlen (suffix);
236 int i = prefix_len + old_length + suffix_len + 1;
237 #ifdef __GNUC__
238 char buffer[i];
239 #else
240 char *buffer = (char *)alloca (i);
241 #endif
242 strcpy (buffer, prefix);
243 for (i = 0; i < old_length; i++)
245 char ch = old_name[i];
246 if (ch == old_char)
247 ch = new_char;
248 buffer[prefix_len + i] = ch;
250 strcpy (buffer + prefix_len + old_length, suffix);
251 return get_identifier (buffer);
254 /* Return an IDENTIFIER_NODE the same as OLD_ID,
255 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
256 Also, PREFIX is prepended, and SUFFIX is appended. */
258 tree
259 identifier_subst (old_id, prefix, old_char, new_char, suffix)
260 const tree old_id;
261 const char *prefix;
262 int old_char;
263 int new_char;
264 const char *suffix;
266 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
267 prefix, old_char, new_char, suffix);
270 /* Generate a valid C identifier from the name of the class TYPE,
271 prefixed by PREFIX. */
273 tree
274 mangled_classname (prefix, type)
275 const char *prefix;
276 tree type;
278 tree ident = TYPE_NAME (type);
279 if (TREE_CODE (ident) != IDENTIFIER_NODE)
280 ident = DECL_NAME (ident);
281 return identifier_subst (ident, prefix, '.', '_', "");
284 tree
285 make_class ()
287 tree type;
288 type = make_node (RECORD_TYPE);
289 #ifdef JAVA_USE_HANDLES
290 tree field1 = build_decl (FIELD_DECL, get_identifier ("obj"),
291 build_pointer_type (type));
292 tree field2 = build_decl (FIELD_DECL, get_identifier ("methods"),
293 methodtable_ptr_type);
294 tree handle_type = make_node (RECORD_TYPE);
295 TREE_CHAIN (field1) = field2;
296 TYPE_FIELDS (handle_type) = field1;
297 TYPE_BINFO (type) = make_tree_vec (7);
298 TYPE_BINFO (handle_type) = make_tree_vec (7);
299 BINFO_HANDLE (TYPE_BINFO (handle_type)) = type;
300 BINFO_HANDLE (TYPE_BINFO (type)) = handle_type;
301 #else
302 TYPE_BINFO (type) = make_tree_vec (6);
303 #endif
304 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
306 return type;
309 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
310 and where each of the constituents is separated by '/',
311 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
313 tree
314 unmangle_classname (name, name_length)
315 const char *name; int name_length;
317 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
318 /* It's not sufficient to compare to_return and get_identifier
319 (name) to determine whether to_return is qualified. There are
320 cases in signature analysis where name will be stripped of a
321 trailing ';'. */
322 name = IDENTIFIER_POINTER (to_return);
323 while (*name)
324 if (*name++ == '.')
326 QUALIFIED_P (to_return) = 1;
327 break;
330 return to_return;
333 tree
334 push_class (class_type, class_name)
335 tree class_type, class_name;
337 tree decl, signature;
338 const char *save_input_filename = input_filename;
339 int save_lineno = lineno;
340 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
341 CLASS_P (class_type) = 1;
342 input_filename = IDENTIFIER_POINTER (source_name);
343 lineno = 0;
344 decl = build_decl (TYPE_DECL, class_name, class_type);
346 /* dbxout needs a DECL_SIZE if in gstabs mode */
347 DECL_SIZE (decl) = integer_zero_node;
349 input_filename = save_input_filename;
350 lineno = save_lineno;
351 signature = identifier_subst (class_name, "L", '.', '/', ";");
352 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
354 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
355 both a typedef and in the struct name-space. We may want to re-visit
356 this later, but for now it reduces the changes needed for gdb. */
357 DECL_ARTIFICIAL (decl) = 1;
359 pushdecl_top_level (decl);
360 #ifdef JAVA_USE_HANDLES
362 tree handle_name = identifier_subst (class_name,
363 "Handle$", '.', '.', "");
364 tree handle_decl = build_decl (TYPE_DECL, handle_name,
365 CLASS_TO_HANDLE_TYPE (class_type));
366 pushdecl (handle_decl);
368 #endif
370 return decl;
373 /* Finds the (global) class named NAME. Creates the class if not found.
374 Also creates associated TYPE_DECL.
375 Does not check if the class actually exists, load the class,
376 fill in field or methods, or do layout_type. */
378 tree
379 lookup_class (name)
380 tree name;
382 tree decl = IDENTIFIER_CLASS_VALUE (name);
383 if (decl == NULL_TREE)
384 decl = push_class (make_class (), name);
385 return TREE_TYPE (decl);
388 void
389 set_super_info (access_flags, this_class, super_class, interfaces_count)
390 int access_flags;
391 tree this_class;
392 tree super_class;
393 int interfaces_count;
395 int total_supers = interfaces_count;
396 tree class_decl = TYPE_NAME (this_class);
397 if (super_class)
398 total_supers++;
400 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
401 if (super_class)
403 tree super_binfo = make_tree_vec (6);
404 BINFO_TYPE (super_binfo) = super_class;
405 BINFO_OFFSET (super_binfo) = integer_zero_node;
406 TREE_VIA_PUBLIC (super_binfo) = 1;
407 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
408 = super_binfo;
409 CLASS_HAS_SUPER (this_class) = 1;
412 set_class_decl_access_flags (access_flags, class_decl);
415 void
416 set_class_decl_access_flags (access_flags, class_decl)
417 int access_flags;
418 tree class_decl;
420 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
421 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
422 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
423 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
424 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
425 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
426 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
427 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
428 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
431 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
432 direct sub-classes of Object are 1, and so on. */
435 class_depth (clas)
436 tree clas;
438 int depth = 0;
439 if (! CLASS_LOADED_P (clas))
440 load_class (clas, 1);
441 if (TYPE_SIZE (clas) == error_mark_node)
442 return -1;
443 while (clas != object_type_node)
445 depth++;
446 clas = TYPE_BINFO_BASETYPE (clas, 0);
448 return depth;
451 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
454 interface_of_p (type1, type2)
455 tree type1, type2;
457 int n, i;
458 tree basetype_vec;
460 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
461 return 0;
462 n = TREE_VEC_LENGTH (basetype_vec);
463 for (i = 0; i < n; i++)
465 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
466 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
467 return 1;
469 for (i = 0; i < n; i++)
471 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
472 if (vec_elt && BINFO_TYPE (vec_elt)
473 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
474 return 1;
476 return 0;
479 /* Return true iff TYPE1 inherits from TYPE2. */
482 inherits_from_p (type1, type2)
483 tree type1, type2;
485 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
487 if (type1 == type2)
488 return 1;
489 type1 = CLASSTYPE_SUPER (type1);
491 return 0;
494 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
497 enclosing_context_p (type1, type2)
498 tree type1, type2;
500 if (!INNER_CLASS_TYPE_P (type2))
501 return 0;
503 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
504 type2;
505 type2 = (INNER_CLASS_TYPE_P (type2) ?
506 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
508 if (type2 == type1)
509 return 1;
512 return 0;
515 /* Return 1 iff there exists a common enclosing context between TYPE1
516 and TYPE2. */
518 int common_enclosing_context_p (type1, type2)
519 tree type1, type2;
521 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
522 return 0;
524 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
525 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
526 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
528 tree current;
529 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
530 current = (PURE_INNER_CLASS_TYPE_P (current) ?
531 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
532 NULL_TREE))
533 if (type1 == current)
534 return 1;
536 return 0;
539 static void
540 add_interface_do (basetype_vec, interface_class, i)
541 tree basetype_vec, interface_class;
542 int i;
544 tree interface_binfo = make_tree_vec (6);
545 BINFO_TYPE (interface_binfo) = interface_class;
546 BINFO_OFFSET (interface_binfo) = integer_zero_node;
547 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
548 TREE_VIA_VIRTUAL (interface_binfo) = 1;
549 TREE_VIA_PUBLIC (interface_binfo) = 1;
550 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
553 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
554 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
555 if attempt is made to add it twice. */
557 tree
558 maybe_add_interface (this_class, interface_class)
559 tree this_class, interface_class;
561 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
562 int i;
563 int n = TREE_VEC_LENGTH (basetype_vec);
564 for (i = 0; ; i++)
566 if (i >= n)
568 error ("internal error - too many interface type");
569 return NULL_TREE;
571 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
572 break;
573 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
574 return interface_class;
576 add_interface_do (basetype_vec, interface_class, i);
577 return NULL_TREE;
580 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
582 void
583 add_interface (this_class, interface_class)
584 tree this_class, interface_class;
586 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
587 int i;
588 int n = TREE_VEC_LENGTH (basetype_vec);
589 for (i = 0; ; i++)
591 if (i >= n)
593 error ("internal error - too many interface type");
594 return;
596 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
597 break;
599 add_interface_do (basetype_vec, interface_class, i);
602 #if 0
603 /* Return the address of a pointer to the first FUNCTION_DECL
604 in the list (*LIST) whose DECL_NAME is NAME. */
606 static tree *
607 find_named_method (list, name)
608 tree *list;
609 tree name;
611 while (*list && DECL_NAME (*list) != name)
612 list = &TREE_CHAIN (*list);
613 return list;
615 #endif
617 static tree
618 build_java_method_type (fntype, this_class, access_flags)
619 tree fntype;
620 tree this_class;
621 int access_flags;
623 if (access_flags & ACC_STATIC)
624 return fntype;
625 return build_method_type (CLASS_TO_HANDLE_TYPE (this_class), fntype);
628 static struct hash_entry *
629 init_test_hash_newfunc (entry, table, string)
630 struct hash_entry *entry;
631 struct hash_table *table;
632 hash_table_key string ATTRIBUTE_UNUSED;
634 struct init_test_hash_entry *ret = (struct init_test_hash_entry *) entry;
635 if (ret == NULL)
637 ret = ((struct init_test_hash_entry *)
638 hash_allocate (table, sizeof (struct init_test_hash_entry)));
639 if (ret == NULL)
640 return NULL;
642 ret->init_test_decl = 0;
643 return (struct hash_entry *) ret;
646 /* Hash table helpers. Also reused in find_applicable_accessible_methods_list
647 (parse.y). The hash of a tree node is its pointer value, comparison
648 is direct. */
650 unsigned long
651 java_hash_hash_tree_node (k)
652 hash_table_key k;
654 return (long) k;
657 bool
658 java_hash_compare_tree_node (k1, k2)
659 hash_table_key k1;
660 hash_table_key k2;
662 return ((tree) k1 == (tree) k2);
665 tree
666 add_method_1 (handle_class, access_flags, name, function_type)
667 tree handle_class;
668 int access_flags;
669 tree name;
670 tree function_type;
672 tree method_type, fndecl;
674 method_type = build_java_method_type (function_type,
675 handle_class, access_flags);
677 fndecl = build_decl (FUNCTION_DECL, name, method_type);
678 DECL_CONTEXT (fndecl) = handle_class;
680 DECL_LANG_SPECIFIC (fndecl)
681 = (struct lang_decl *) ggc_alloc_cleared (sizeof (struct lang_decl));
683 /* Initialize the static initializer test table. */
684 hash_table_init (&DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
685 init_test_hash_newfunc, java_hash_hash_tree_node,
686 java_hash_compare_tree_node);
688 /* Initialize the initialized (static) class table. */
689 if (access_flags & ACC_STATIC)
690 hash_table_init (&DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl),
691 init_test_hash_newfunc, java_hash_hash_tree_node,
692 java_hash_compare_tree_node);
694 /* Initialize the static method invocation compound list */
695 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
697 TREE_CHAIN (fndecl) = TYPE_METHODS (handle_class);
698 TYPE_METHODS (handle_class) = fndecl;
700 /* Notice that this is a finalizer and update the class type
701 accordingly. This is used to optimize instance allocation. */
702 if (name == finalize_identifier_node
703 && TREE_TYPE (function_type) == void_type_node
704 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
705 HAS_FINALIZER_P (handle_class) = 1;
707 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
708 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
709 if (access_flags & ACC_PRIVATE)
710 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
711 if (access_flags & ACC_NATIVE)
713 METHOD_NATIVE (fndecl) = 1;
714 DECL_EXTERNAL (fndecl) = 1;
716 if (access_flags & ACC_STATIC)
717 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
718 if (access_flags & ACC_FINAL)
719 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
720 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
721 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
722 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
723 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
724 return fndecl;
727 /* Add a method to THIS_CLASS.
728 The method's name is NAME.
729 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
731 tree
732 add_method (this_class, access_flags, name, method_sig)
733 tree this_class;
734 int access_flags;
735 tree name;
736 tree method_sig;
738 tree handle_class = CLASS_TO_HANDLE_TYPE (this_class);
739 tree function_type, fndecl;
740 const unsigned char *sig
741 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
743 if (sig[0] != '(')
744 fatal_error ("bad method signature");
746 function_type = get_type_from_signature (method_sig);
747 fndecl = add_method_1 (handle_class, access_flags, name, function_type);
748 set_java_signature (TREE_TYPE (fndecl), method_sig);
749 return fndecl;
752 tree
753 add_field (class, name, field_type, flags)
754 tree class;
755 tree name;
756 tree field_type;
757 int flags;
759 int is_static = (flags & ACC_STATIC) != 0;
760 tree field;
761 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
762 TREE_CHAIN (field) = TYPE_FIELDS (class);
763 TYPE_FIELDS (class) = field;
764 DECL_CONTEXT (field) = class;
766 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
767 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
768 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
769 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
770 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
771 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
772 if (is_static)
774 FIELD_STATIC (field) = 1;
775 /* Always make field externally visible. This is required so
776 that native methods can always access the field. */
777 TREE_PUBLIC (field) = 1;
778 /* Considered external until we know what classes are being
779 compiled into this object file. */
780 DECL_EXTERNAL (field) = 1;
783 return field;
786 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
788 void
789 set_constant_value (field, constant)
790 tree field, constant;
792 if (field == NULL_TREE)
793 warning ("misplaced ConstantValue attribute (not in any field)");
794 else if (DECL_INITIAL (field) != NULL_TREE)
795 warning ("duplicate ConstantValue attribute for field '%s'",
796 IDENTIFIER_POINTER (DECL_NAME (field)));
797 else
799 DECL_INITIAL (field) = constant;
800 if (TREE_TYPE (constant) != TREE_TYPE (field)
801 && ! (TREE_TYPE (constant) == int_type_node
802 && INTEGRAL_TYPE_P (TREE_TYPE (field))
803 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
804 && ! (TREE_TYPE (constant) == utf8const_ptr_type
805 && TREE_TYPE (field) == string_ptr_type_node))
806 error ("ConstantValue attribute of field '%s' has wrong type",
807 IDENTIFIER_POINTER (DECL_NAME (field)));
808 if (FIELD_FINAL (field))
809 DECL_FIELD_FINAL_IUD (field) = 1;
813 /* Count the number of Unicode chars encoded in a given Ut8 string. */
815 #if 0
817 strLengthUtf8 (str, len)
818 char *str;
819 int len;
821 register unsigned char* ptr = (unsigned char*) str;
822 register unsigned char *limit = ptr + len;
823 int str_length = 0;
824 for (; ptr < limit; str_length++) {
825 if (UTF8_GET (ptr, limit) < 0)
826 return -1;
828 return str_length;
830 #endif
833 /* Calculate a hash value for a string encoded in Utf8 format.
834 * This returns the same hash value as specified for java.lang.String.hashCode.
837 static int32
838 hashUtf8String (str, len)
839 const char *str;
840 int len;
842 register const unsigned char* ptr = (const unsigned char*) str;
843 register 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 /* Generate a byte array representing the contents of FILENAME. The
856 array is assigned a unique local symbol. The array represents a
857 compiled Java resource, which is accessed by the runtime using
858 NAME. */
859 void
860 compile_resource_file (name, filename)
861 char *name;
862 const char *filename;
864 struct stat stat_buf;
865 int fd;
866 char *buffer;
867 char buf[60];
868 tree rtype, field = NULL_TREE, data_type, rinit, data, decl;
869 static int Jr_count = 0;
871 fd = open (filename, O_RDONLY | O_BINARY);
872 if (fd < 0)
874 perror ("Failed to read resource file");
875 return;
877 if (fstat (fd, &stat_buf) != 0
878 || ! S_ISREG (stat_buf.st_mode))
880 perror ("Could not figure length of resource file");
881 return;
883 buffer = xmalloc (strlen (name) + stat_buf.st_size);
884 strcpy (buffer, name);
885 read (fd, buffer + strlen (name), stat_buf.st_size);
886 close (fd);
887 data_type = build_prim_array_type (unsigned_byte_type_node,
888 strlen (name) + stat_buf.st_size);
889 rtype = make_node (RECORD_TYPE);
890 PUSH_FIELD (rtype, field, "name_length", unsigned_int_type_node);
891 PUSH_FIELD (rtype, field, "resource_length", unsigned_int_type_node);
892 PUSH_FIELD (rtype, field, "data", data_type);
893 FINISH_RECORD (rtype);
894 START_RECORD_CONSTRUCTOR (rinit, rtype);
895 PUSH_FIELD_VALUE (rinit, "name_length",
896 build_int_2 (strlen (name), 0));
897 PUSH_FIELD_VALUE (rinit, "resource_length",
898 build_int_2 (stat_buf.st_size, 0));
899 data = build_string (strlen(name) + stat_buf.st_size, buffer);
900 TREE_TYPE (data) = data_type;
901 PUSH_FIELD_VALUE (rinit, "data", data);
902 FINISH_RECORD_CONSTRUCTOR (rinit);
903 TREE_CONSTANT (rinit) = 1;
905 /* Generate a unique-enough identifier. */
906 sprintf(buf, "_Jr%d", ++Jr_count);
908 decl = build_decl (VAR_DECL, get_identifier (buf), rtype);
909 TREE_STATIC (decl) = 1;
910 DECL_ARTIFICIAL (decl) = 1;
911 DECL_IGNORED_P (decl) = 1;
912 TREE_READONLY (decl) = 1;
913 TREE_THIS_VOLATILE (decl) = 0;
914 DECL_INITIAL (decl) = rinit;
915 layout_decl (decl, 0);
916 pushdecl (decl);
917 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
918 make_decl_rtl (decl, (char*) 0);
919 assemble_variable (decl, 1, 0, 0);
922 tree init_name = get_file_function_name ('I');
923 tree init_type = build_function_type (void_type_node, end_params_node);
924 tree init_decl;
926 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
927 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
928 TREE_STATIC (init_decl) = 1;
929 current_function_decl = init_decl;
930 DECL_RESULT (init_decl) = build_decl (RESULT_DECL,
931 NULL_TREE, void_type_node);
933 /* It can be a static function as long as collect2 does not have
934 to scan the object file to find its ctor/dtor routine. */
935 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
937 pushlevel (0);
938 make_decl_rtl (init_decl, NULL);
939 init_function_start (init_decl, input_filename, 0);
940 expand_function_start (init_decl, 0);
942 emit_library_call (registerResource_libfunc, 0, VOIDmode, 1,
943 gen_rtx (SYMBOL_REF, Pmode, buf),
944 Pmode);
946 expand_function_end (input_filename, 0, 0);
947 poplevel (1, 0, 1);
949 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
950 int saved_flag = flag_inline_functions;
951 flag_inline_functions = 0;
952 rest_of_compilation (init_decl);
953 flag_inline_functions = saved_flag;
955 current_function_decl = NULL_TREE;
956 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
957 DEFAULT_INIT_PRIORITY);
961 tree utf8_decl_list = NULL_TREE;
963 tree
964 build_utf8_ref (name)
965 tree name;
967 const char * name_ptr = IDENTIFIER_POINTER(name);
968 int name_len = IDENTIFIER_LENGTH(name);
969 char buf[60];
970 tree ctype, field = NULL_TREE, str_type, cinit, string;
971 static int utf8_count = 0;
972 int name_hash;
973 tree ref = IDENTIFIER_UTF8_REF (name);
974 tree decl;
975 if (ref != NULL_TREE)
976 return ref;
978 ctype = make_node (RECORD_TYPE);
979 str_type = build_prim_array_type (unsigned_byte_type_node,
980 name_len + 1); /* Allow for final '\0'. */
981 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
982 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
983 PUSH_FIELD (ctype, field, "data", str_type);
984 FINISH_RECORD (ctype);
985 START_RECORD_CONSTRUCTOR (cinit, ctype);
986 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
987 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
988 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
989 string = build_string (name_len, name_ptr);
990 TREE_TYPE (string) = str_type;
991 PUSH_FIELD_VALUE (cinit, "data", string);
992 FINISH_RECORD_CONSTRUCTOR (cinit);
993 TREE_CONSTANT (cinit) = 1;
995 /* Generate a unique-enough identifier. */
996 sprintf(buf, "_Utf%d", ++utf8_count);
998 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
999 TREE_STATIC (decl) = 1;
1000 DECL_ARTIFICIAL (decl) = 1;
1001 DECL_IGNORED_P (decl) = 1;
1002 TREE_READONLY (decl) = 1;
1003 TREE_THIS_VOLATILE (decl) = 0;
1004 DECL_INITIAL (decl) = cinit;
1005 #ifdef HAVE_GAS_SHF_MERGE
1007 int decl_size;
1008 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
1009 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
1010 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
1011 if (flag_merge_constants && decl_size < 256)
1013 char buf[32];
1014 int flags = (SECTION_OVERRIDE
1015 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
1016 sprintf (buf, ".rodata.jutf8.%d", decl_size);
1017 named_section_flags (buf, flags);
1018 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
1021 #endif
1022 TREE_CHAIN (decl) = utf8_decl_list;
1023 layout_decl (decl, 0);
1024 pushdecl (decl);
1025 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
1026 utf8_decl_list = decl;
1027 make_decl_rtl (decl, (char*) 0);
1028 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
1029 IDENTIFIER_UTF8_REF (name) = ref;
1030 return ref;
1033 /* Build a reference to the class TYPE.
1034 Also handles primitive types and array types. */
1036 tree
1037 build_class_ref (type)
1038 tree type;
1040 int is_compiled = is_compiled_class (type);
1041 if (is_compiled)
1043 tree ref, decl_name, decl;
1044 if (TREE_CODE (type) == POINTER_TYPE)
1045 type = TREE_TYPE (type);
1046 if (TREE_CODE (type) == RECORD_TYPE)
1048 if (TYPE_SIZE (type) == error_mark_node)
1049 return null_pointer_node;
1050 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
1051 "", '/', '/', ".class");
1052 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1053 if (decl == NULL_TREE)
1055 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1056 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
1057 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
1058 TREE_STATIC (decl) = 1;
1059 TREE_PUBLIC (decl) = 1;
1060 DECL_IGNORED_P (decl) = 1;
1061 DECL_ARTIFICIAL (decl) = 1;
1062 if (is_compiled == 1)
1063 DECL_EXTERNAL (decl) = 1;
1064 SET_DECL_ASSEMBLER_NAME (decl,
1065 java_mangle_class_field
1066 (&temporary_obstack, type));
1067 make_decl_rtl (decl, NULL);
1068 pushdecl_top_level (decl);
1071 else
1073 const char *name;
1074 char buffer[25];
1075 if (flag_emit_class_files)
1077 const char *prim_class_name;
1078 tree prim_class;
1079 if (type == char_type_node)
1080 prim_class_name = "java.lang.Character";
1081 else if (type == boolean_type_node)
1082 prim_class_name = "java.lang.Boolean";
1083 else if (type == byte_type_node)
1084 prim_class_name = "java.lang.Byte";
1085 else if (type == short_type_node)
1086 prim_class_name = "java.lang.Short";
1087 else if (type == int_type_node)
1088 prim_class_name = "java.lang.Integer";
1089 else if (type == long_type_node)
1090 prim_class_name = "java.lang.Long";
1091 else if (type == float_type_node)
1092 prim_class_name = "java.lang.Float";
1093 else if (type == double_type_node)
1094 prim_class_name = "java.lang.Double";
1095 else if (type == void_type_node)
1096 prim_class_name = "java.lang.Void";
1097 else
1098 abort ();
1100 prim_class = lookup_class (get_identifier (prim_class_name));
1101 return build (COMPONENT_REF, NULL_TREE,
1102 prim_class, TYPE_identifier_node);
1104 decl_name = TYPE_NAME (type);
1105 if (TREE_CODE (decl_name) == TYPE_DECL)
1106 decl_name = DECL_NAME (decl_name);
1107 name = IDENTIFIER_POINTER (decl_name);
1108 if (strncmp (name, "promoted_", 9) == 0)
1109 name += 9;
1110 sprintf (buffer, "_Jv_%sClass", name);
1111 decl_name = get_identifier (buffer);
1112 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1113 if (decl == NULL_TREE)
1115 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1116 TREE_STATIC (decl) = 1;
1117 TREE_PUBLIC (decl) = 1;
1118 DECL_EXTERNAL (decl) = 1;
1119 make_decl_rtl (decl, NULL);
1120 pushdecl_top_level (decl);
1124 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1125 return ref;
1127 else
1129 int index;
1130 tree cl;
1131 index = alloc_class_constant (type);
1132 cl = build_ref_from_constant_pool (index);
1133 TREE_TYPE (cl) = promote_type (class_ptr_type);
1134 return cl;
1138 tree
1139 build_static_field_ref (fdecl)
1140 tree fdecl;
1142 tree fclass = DECL_CONTEXT (fdecl);
1143 int is_compiled = is_compiled_class (fclass);
1144 if (is_compiled)
1146 if (!DECL_RTL_SET_P (fdecl))
1148 if (is_compiled == 1)
1149 DECL_EXTERNAL (fdecl) = 1;
1150 make_decl_rtl (fdecl, NULL);
1152 return fdecl;
1154 else
1156 /* Compile as:
1157 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1158 tree ref = build_class_ref (fclass);
1159 tree fld;
1160 int field_index = 0;
1161 ref = build1 (INDIRECT_REF, class_type_node, ref);
1162 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1163 lookup_field (&class_type_node, fields_ident));
1165 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1167 if (fld == fdecl)
1168 break;
1169 if (fld == NULL_TREE)
1170 fatal_error ("field '%s' not found in class",
1171 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1172 if (FIELD_STATIC (fld))
1173 field_index++;
1175 field_index *= int_size_in_bytes (field_type_node);
1176 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1177 ref, build_int_2 (field_index, 0)));
1178 ref = build1 (INDIRECT_REF, field_type_node, ref);
1179 ref = build (COMPONENT_REF, field_info_union_node,
1180 ref, lookup_field (&field_type_node, info_ident));
1181 ref = build (COMPONENT_REF, ptr_type_node,
1182 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1183 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1188 get_access_flags_from_decl (decl)
1189 tree decl;
1191 int access_flags = 0;
1192 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1194 if (FIELD_STATIC (decl))
1195 access_flags |= ACC_STATIC;
1196 if (FIELD_PUBLIC (decl))
1197 access_flags |= ACC_PUBLIC;
1198 if (FIELD_PROTECTED (decl))
1199 access_flags |= ACC_PROTECTED;
1200 if (FIELD_PRIVATE (decl))
1201 access_flags |= ACC_PRIVATE;
1202 if (FIELD_FINAL (decl))
1203 access_flags |= ACC_FINAL;
1204 if (FIELD_VOLATILE (decl))
1205 access_flags |= ACC_VOLATILE;
1206 if (FIELD_TRANSIENT (decl))
1207 access_flags |= ACC_TRANSIENT;
1208 return access_flags;
1210 if (TREE_CODE (decl) == TYPE_DECL)
1212 if (CLASS_PUBLIC (decl))
1213 access_flags |= ACC_PUBLIC;
1214 if (CLASS_FINAL (decl))
1215 access_flags |= ACC_FINAL;
1216 if (CLASS_SUPER (decl))
1217 access_flags |= ACC_SUPER;
1218 if (CLASS_INTERFACE (decl))
1219 access_flags |= ACC_INTERFACE;
1220 if (CLASS_ABSTRACT (decl))
1221 access_flags |= ACC_ABSTRACT;
1222 if (CLASS_STATIC (decl))
1223 access_flags |= ACC_STATIC;
1224 if (CLASS_PRIVATE (decl))
1225 access_flags |= ACC_PRIVATE;
1226 if (CLASS_PROTECTED (decl))
1227 access_flags |= ACC_PROTECTED;
1228 if (CLASS_STRICTFP (decl))
1229 access_flags |= ACC_STRICT;
1230 return access_flags;
1232 if (TREE_CODE (decl) == FUNCTION_DECL)
1234 if (METHOD_PUBLIC (decl))
1235 access_flags |= ACC_PUBLIC;
1236 if (METHOD_PRIVATE (decl))
1237 access_flags |= ACC_PRIVATE;
1238 if (METHOD_PROTECTED (decl))
1239 access_flags |= ACC_PROTECTED;
1240 if (METHOD_STATIC (decl))
1241 access_flags |= ACC_STATIC;
1242 if (METHOD_FINAL (decl))
1243 access_flags |= ACC_FINAL;
1244 if (METHOD_SYNCHRONIZED (decl))
1245 access_flags |= ACC_SYNCHRONIZED;
1246 if (METHOD_NATIVE (decl))
1247 access_flags |= ACC_NATIVE;
1248 if (METHOD_ABSTRACT (decl))
1249 access_flags |= ACC_ABSTRACT;
1250 if (METHOD_TRANSIENT (decl))
1251 access_flags |= ACC_TRANSIENT;
1252 if (METHOD_STRICTFP (decl))
1253 access_flags |= ACC_STRICT;
1254 return access_flags;
1256 abort ();
1259 static tree
1260 make_field_value (fdecl)
1261 tree fdecl;
1263 tree finit;
1264 int flags;
1265 tree type = TREE_TYPE (fdecl);
1266 int resolved = is_compiled_class (type);
1268 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1269 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1270 if (resolved)
1271 type = build_class_ref (type);
1272 else
1274 tree signature = build_java_signature (type);
1276 type = build_utf8_ref (unmangle_classname
1277 (IDENTIFIER_POINTER (signature),
1278 IDENTIFIER_LENGTH (signature)));
1280 PUSH_FIELD_VALUE (finit, "type", type);
1282 flags = get_access_flags_from_decl (fdecl);
1283 if (! resolved)
1284 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1286 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1287 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1289 PUSH_FIELD_VALUE
1290 (finit, "info",
1291 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1292 build_tree_list
1293 ((FIELD_STATIC (fdecl)
1294 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1295 : TYPE_FIELDS (field_info_union_node)),
1296 (FIELD_STATIC (fdecl)
1297 ? build_address_of (build_static_field_ref (fdecl))
1298 : byte_position (fdecl)))));
1300 FINISH_RECORD_CONSTRUCTOR (finit);
1301 return finit;
1304 static tree
1305 make_method_value (mdecl)
1306 tree mdecl;
1308 static int method_name_count = 0;
1309 tree minit;
1310 tree index;
1311 tree code;
1312 #define ACC_TRANSLATED 0x4000
1313 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1315 if (!flag_indirect_dispatch && DECL_VINDEX (mdecl) != NULL_TREE)
1316 index = DECL_VINDEX (mdecl);
1317 else
1318 index = integer_minus_one_node;
1320 code = null_pointer_node;
1321 if (DECL_RTL_SET_P (mdecl))
1322 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1323 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1324 PUSH_FIELD_VALUE (minit, "name",
1325 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1326 init_identifier_node
1327 : DECL_NAME (mdecl)));
1329 tree signature = build_java_signature (TREE_TYPE (mdecl));
1330 PUSH_FIELD_VALUE (minit, "signature",
1331 (build_utf8_ref
1332 (unmangle_classname
1333 (IDENTIFIER_POINTER(signature),
1334 IDENTIFIER_LENGTH(signature)))));
1336 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1337 PUSH_FIELD_VALUE (minit, "index", index);
1338 PUSH_FIELD_VALUE (minit, "ncode", code);
1341 /* Compute the `throws' information for the method. */
1342 tree table = null_pointer_node;
1343 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1345 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1346 tree iter, type, array;
1347 char buf[60];
1349 table = tree_cons (NULL_TREE, table, NULL_TREE);
1350 for (iter = DECL_FUNCTION_THROWS (mdecl);
1351 iter != NULL_TREE;
1352 iter = TREE_CHAIN (iter))
1354 tree sig = build_java_signature (TREE_VALUE (iter));
1355 tree utf8
1356 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1357 IDENTIFIER_LENGTH (sig)));
1358 table = tree_cons (NULL_TREE, utf8, table);
1360 type = build_prim_array_type (ptr_type_node, length);
1361 table = build (CONSTRUCTOR, type, NULL_TREE, table);
1362 /* Compute something unique enough. */
1363 sprintf (buf, "_methods%d", method_name_count++);
1364 array = build_decl (VAR_DECL, get_identifier (buf), type);
1365 DECL_INITIAL (array) = table;
1366 TREE_STATIC (array) = 1;
1367 DECL_ARTIFICIAL (array) = 1;
1368 DECL_IGNORED_P (array) = 1;
1369 rest_of_decl_compilation (array, (char*) 0, 1, 0);
1371 table = build1 (ADDR_EXPR, ptr_type_node, array);
1374 PUSH_FIELD_VALUE (minit, "throws", table);
1377 FINISH_RECORD_CONSTRUCTOR (minit);
1378 return minit;
1381 static tree
1382 get_dispatch_vector (type)
1383 tree type;
1385 tree vtable = TYPE_VTABLE (type);
1386 if (vtable == NULL)
1388 HOST_WIDE_INT i;
1389 tree method;
1390 tree super = CLASSTYPE_SUPER (type);
1391 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1392 vtable = make_tree_vec (nvirtuals);
1393 TYPE_VTABLE (type) = vtable;
1394 if (super != NULL_TREE)
1396 tree super_vtable = get_dispatch_vector (super);
1398 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1399 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1402 for (method = TYPE_METHODS (type); method != NULL_TREE;
1403 method = TREE_CHAIN (method))
1404 if (DECL_VINDEX (method) != NULL_TREE
1405 && host_integerp (DECL_VINDEX (method), 0))
1406 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1407 = method;
1410 return vtable;
1413 static tree
1414 get_dispatch_table (type, this_class_addr)
1415 tree type, this_class_addr;
1417 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1418 tree vtable = get_dispatch_vector (type);
1419 int i, j;
1420 tree list = NULL_TREE;
1421 int nvirtuals = TREE_VEC_LENGTH (vtable);
1422 int arraysize;
1423 tree gc_descr;
1425 for (i = nvirtuals; --i >= 0; )
1427 tree method = TREE_VEC_ELT (vtable, i);
1428 if (METHOD_ABSTRACT (method))
1430 if (! abstract_p)
1431 warning_with_decl (method,
1432 "abstract method in non-abstract class");
1434 if (TARGET_VTABLE_USES_DESCRIPTORS)
1435 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1436 list = tree_cons (NULL_TREE, null_pointer_node, list);
1437 else
1438 list = tree_cons (NULL_TREE, null_pointer_node, list);
1440 else
1442 if (!DECL_RTL_SET_P (method))
1443 make_decl_rtl (method, NULL);
1445 if (TARGET_VTABLE_USES_DESCRIPTORS)
1446 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1448 tree fdesc = build (FDESC_EXPR, nativecode_ptr_type_node,
1449 method, build_int_2 (j, 0));
1450 TREE_CONSTANT (fdesc) = 1;
1451 list = tree_cons (NULL_TREE, fdesc, list);
1453 else
1454 list = tree_cons (NULL_TREE,
1455 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1456 method),
1457 list);
1461 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1462 using the Boehm GC we sometimes stash a GC type descriptor
1463 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1464 the emitted byte count during the output to the assembly file. */
1465 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1466 fake "function descriptor". It's first word is the is the class
1467 pointer, and subsequent words (usually one) contain the GC descriptor.
1468 In all other cases, we reserve two extra vtable slots. */
1469 gc_descr = get_boehm_type_descriptor (type);
1470 list = tree_cons (NULL_TREE, gc_descr, list);
1471 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1472 list = tree_cons (NULL_TREE, gc_descr, list);
1473 list = tree_cons (NULL_TREE, this_class_addr, list);
1475 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1476 list = tree_cons (NULL_TREE, null_pointer_node, list);
1477 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1478 list = tree_cons (integer_zero_node, null_pointer_node, list);
1480 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1481 if (TARGET_VTABLE_USES_DESCRIPTORS)
1482 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1483 arraysize += 2;
1484 return build (CONSTRUCTOR,
1485 build_prim_array_type (nativecode_ptr_type_node, arraysize),
1486 NULL_TREE, list);
1489 void
1490 make_class_data (type)
1491 tree type;
1493 tree decl, cons, temp;
1494 tree field, fields_decl;
1495 tree static_fields = NULL_TREE;
1496 tree instance_fields = NULL_TREE;
1497 HOST_WIDE_INT static_field_count = 0;
1498 HOST_WIDE_INT instance_field_count = 0;
1499 HOST_WIDE_INT field_count;
1500 tree field_array_type;
1501 tree method;
1502 tree methods = NULL_TREE;
1503 tree dtable_decl = NULL_TREE;
1504 HOST_WIDE_INT method_count = 0;
1505 tree method_array_type;
1506 tree methods_decl;
1507 tree super;
1508 tree this_class_addr;
1509 tree constant_pool_constructor;
1510 tree interfaces = null_pointer_node;
1511 int interface_len = 0;
1512 tree type_decl = TYPE_NAME (type);
1513 /** Offset from start of virtual function table declaration
1514 to where objects actually point at, following new g++ ABI. */
1515 tree dtable_start_offset = build_int_2 (2 * POINTER_SIZE / BITS_PER_UNIT, 0);
1517 this_class_addr = build_class_ref (type);
1518 decl = TREE_OPERAND (this_class_addr, 0);
1520 /* Build Field array. */
1521 field = TYPE_FIELDS (type);
1522 if (DECL_NAME (field) == NULL_TREE)
1523 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1524 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1526 if (! DECL_ARTIFICIAL (field))
1528 tree init = make_field_value (field);
1529 if (FIELD_STATIC (field))
1531 tree initial = DECL_INITIAL (field);
1532 static_field_count++;
1533 static_fields = tree_cons (NULL_TREE, init, static_fields);
1534 /* If the initial value is a string constant,
1535 prevent output_constant from trying to assemble the value. */
1536 if (initial != NULL_TREE
1537 && TREE_TYPE (initial) == string_ptr_type_node)
1538 DECL_INITIAL (field) = NULL_TREE;
1539 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1540 DECL_INITIAL (field) = initial;
1542 else
1544 instance_field_count++;
1545 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1549 field_count = static_field_count + instance_field_count;
1550 if (field_count > 0)
1552 static_fields = nreverse (static_fields);
1553 instance_fields = nreverse (instance_fields);
1554 static_fields = chainon (static_fields, instance_fields);
1555 field_array_type = build_prim_array_type (field_type_node, field_count);
1556 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1557 field_array_type);
1558 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1559 NULL_TREE, static_fields);
1560 TREE_STATIC (fields_decl) = 1;
1561 DECL_ARTIFICIAL (fields_decl) = 1;
1562 DECL_IGNORED_P (fields_decl) = 1;
1563 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1565 else
1566 fields_decl = NULL_TREE;
1568 /* Build Method array. */
1569 for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (type));
1570 method != NULL_TREE; method = TREE_CHAIN (method))
1572 tree init;
1573 if (METHOD_PRIVATE (method)
1574 && ! flag_keep_inline_functions
1575 && (flag_inline_functions || optimize))
1576 continue;
1577 init = make_method_value (method);
1578 method_count++;
1579 methods = tree_cons (NULL_TREE, init, methods);
1581 method_array_type = build_prim_array_type (method_type_node, method_count);
1582 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1583 method_array_type);
1584 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1585 NULL_TREE, nreverse (methods));
1586 TREE_STATIC (methods_decl) = 1;
1587 DECL_ARTIFICIAL (methods_decl) = 1;
1588 DECL_IGNORED_P (methods_decl) = 1;
1589 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1591 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1592 && ! CLASS_INTERFACE (type_decl) && !flag_indirect_dispatch)
1594 tree dtable = get_dispatch_table (type, this_class_addr);
1595 dtable_decl = build_dtable_decl (type);
1596 DECL_INITIAL (dtable_decl) = dtable;
1597 TREE_STATIC (dtable_decl) = 1;
1598 DECL_ARTIFICIAL (dtable_decl) = 1;
1599 DECL_IGNORED_P (dtable_decl) = 1;
1600 TREE_PUBLIC (dtable_decl) = 1;
1601 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1602 if (type == class_type_node)
1603 class_dtable_decl = dtable_decl;
1606 if (class_dtable_decl == NULL_TREE)
1608 class_dtable_decl = build_dtable_decl (class_type_node);
1609 TREE_STATIC (class_dtable_decl) = 1;
1610 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1611 DECL_IGNORED_P (class_dtable_decl) = 1;
1612 if (is_compiled_class (class_type_node) != 2)
1613 DECL_EXTERNAL (class_dtable_decl) = 1;
1614 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1617 super = CLASSTYPE_SUPER (type);
1618 if (super == NULL_TREE)
1619 super = null_pointer_node;
1620 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl))))
1621 super = build_class_ref (super);
1622 else
1624 int super_index = alloc_class_constant (super);
1625 super = build_int_2 (super_index, 0);
1626 TREE_TYPE (super) = ptr_type_node;
1629 /* Build and emit the array of implemented interfaces. */
1630 if (type != object_type_node)
1631 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1632 if (interface_len > 0)
1634 tree init = NULL_TREE;
1635 int i;
1636 tree interface_array_type, idecl;
1637 interface_array_type
1638 = build_prim_array_type (class_ptr_type, interface_len);
1639 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1640 interface_array_type);
1641 for (i = interface_len; i > 0; i--)
1643 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1644 tree iclass = BINFO_TYPE (child);
1645 tree index;
1646 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1647 index = build_class_ref (iclass);
1648 else
1650 int int_index = alloc_class_constant (iclass);
1651 index = build_int_2 (int_index, 0);
1652 TREE_TYPE (index) = ptr_type_node;
1654 init = tree_cons (NULL_TREE, index, init);
1656 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1657 NULL_TREE, init);
1658 TREE_STATIC (idecl) = 1;
1659 DECL_ARTIFICIAL (idecl) = 1;
1660 DECL_IGNORED_P (idecl) = 1;
1661 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1662 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1665 constant_pool_constructor = build_constants_constructor ();
1667 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1668 PUSH_FIELD_VALUE (temp, "vtable",
1669 build (PLUS_EXPR, dtable_ptr_type,
1670 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl),
1671 dtable_start_offset));
1672 if (! flag_hash_synchronization)
1673 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1674 FINISH_RECORD_CONSTRUCTOR (temp);
1675 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1676 PUSH_SUPER_VALUE (cons, temp);
1677 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1678 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1679 PUSH_FIELD_VALUE (cons, "accflags",
1680 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1682 PUSH_FIELD_VALUE (cons, "superclass",
1683 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1684 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1685 PUSH_FIELD_VALUE (cons, "methods",
1686 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1687 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1689 if (flag_indirect_dispatch)
1690 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node)
1691 else
1692 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1694 PUSH_FIELD_VALUE (cons, "fields",
1695 fields_decl == NULL_TREE ? null_pointer_node
1696 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1697 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1698 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1699 PUSH_FIELD_VALUE (cons, "static_field_count",
1700 build_int_2 (static_field_count, 0));
1702 if (flag_indirect_dispatch)
1703 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node)
1704 else
1705 PUSH_FIELD_VALUE (cons, "vtable",
1706 dtable_decl == NULL_TREE ? null_pointer_node
1707 : build (PLUS_EXPR, dtable_ptr_type,
1708 build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl),
1709 dtable_start_offset));
1711 if (otable_methods == NULL_TREE)
1713 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1714 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1716 else
1718 PUSH_FIELD_VALUE (cons, "otable",
1719 build1 (ADDR_EXPR, otable_ptr_type, otable_decl));
1720 PUSH_FIELD_VALUE (cons, "otable_syms",
1721 build1 (ADDR_EXPR, method_symbols_array_ptr_type,
1722 otable_syms_decl));
1724 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1725 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1726 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1727 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1729 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1730 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1731 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1732 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1733 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1734 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1736 FINISH_RECORD_CONSTRUCTOR (cons);
1738 DECL_INITIAL (decl) = cons;
1739 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1742 void
1743 finish_class ()
1745 tree method;
1746 tree type_methods = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
1747 int saw_native_method = 0;
1749 /* Find out if we have any native methods. We use this information
1750 later. */
1751 for (method = type_methods;
1752 method != NULL_TREE;
1753 method = TREE_CHAIN (method))
1755 if (METHOD_NATIVE (method))
1757 saw_native_method = 1;
1758 break;
1762 /* Emit deferred inline methods. */
1763 for (method = type_methods; method != NULL_TREE; )
1765 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1767 output_inline_function (method);
1768 /* Scan the list again to see if there are any earlier
1769 methods to emit. */
1770 method = type_methods;
1771 continue;
1773 method = TREE_CHAIN (method);
1776 current_function_decl = NULL_TREE;
1777 make_class_data (current_class);
1778 register_class ();
1779 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1782 /* Return 2 if CLASS is compiled by this compilation job;
1783 return 1 if CLASS can otherwise be assumed to be compiled;
1784 return 0 if we cannot assume that CLASS is compiled.
1785 Returns 1 for primitive and 0 for array types. */
1787 is_compiled_class (class)
1788 tree class;
1790 int seen_in_zip;
1791 if (TREE_CODE (class) == POINTER_TYPE)
1792 class = TREE_TYPE (class);
1793 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1794 return 1;
1795 if (TYPE_ARRAY_P (class))
1796 return 0;
1797 if (class == current_class)
1798 return 2;
1800 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1801 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1803 /* The class was seen in the current ZIP file and will be
1804 available as a compiled class in the future but may not have
1805 been loaded already. Load it if necessary. This prevent
1806 build_class_ref () from crashing. */
1808 if (seen_in_zip && !CLASS_LOADED_P (class))
1809 load_class (class, 1);
1811 /* We return 2 for class seen in ZIP and class from files
1812 belonging to the same compilation unit */
1813 return 2;
1816 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1818 if (!CLASS_LOADED_P (class))
1820 if (CLASS_FROM_SOURCE_P (class))
1821 safe_layout_class (class);
1822 else
1823 load_class (class, 1);
1825 return 1;
1828 return 0;
1831 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1833 tree
1834 build_dtable_decl (type)
1835 tree type;
1837 tree dtype;
1839 /* We need to build a new dtable type so that its size is uniquely
1840 computed when we're dealing with the class for real and not just
1841 faking it (like java.lang.Class during the initialization of the
1842 compiler.) We know we're not faking a class when CURRENT_CLASS is
1843 TYPE. */
1844 if (current_class == type)
1846 tree dummy = NULL_TREE;
1847 int n;
1849 dtype = make_node (RECORD_TYPE);
1851 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1852 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1854 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1855 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1857 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1858 TREE_CHAIN (dummy) = tmp_field;
1859 DECL_CONTEXT (tmp_field) = dtype;
1860 DECL_ARTIFICIAL (tmp_field) = 1;
1861 dummy = tmp_field;
1864 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1865 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1867 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1868 TREE_CHAIN (dummy) = tmp_field;
1869 DECL_CONTEXT (tmp_field) = dtype;
1870 DECL_ARTIFICIAL (tmp_field) = 1;
1871 dummy = tmp_field;
1874 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
1875 if (TARGET_VTABLE_USES_DESCRIPTORS)
1876 n *= TARGET_VTABLE_USES_DESCRIPTORS;
1878 PUSH_FIELD (dtype, dummy, "methods",
1879 build_prim_array_type (nativecode_ptr_type_node, n));
1880 layout_type (dtype);
1882 else
1883 dtype = dtable_type;
1885 return build_decl (VAR_DECL,
1886 java_mangle_vtable (&temporary_obstack, type), dtype);
1889 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1890 fields inherited from SUPER_CLASS. */
1892 void
1893 push_super_field (this_class, super_class)
1894 tree this_class, super_class;
1896 tree base_decl;
1897 /* Don't insert the field if we're just re-laying the class out. */
1898 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1899 return;
1900 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1901 DECL_IGNORED_P (base_decl) = 1;
1902 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1903 TYPE_FIELDS (this_class) = base_decl;
1904 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1905 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1908 /* Handle the different manners we may have to lay out a super class. */
1910 static tree
1911 maybe_layout_super_class (super_class, this_class)
1912 tree super_class;
1913 tree this_class;
1915 if (TREE_CODE (super_class) == RECORD_TYPE)
1917 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1918 safe_layout_class (super_class);
1919 if (!CLASS_LOADED_P (super_class))
1920 load_class (super_class, 1);
1922 /* We might have to layout the class before its dependency on
1923 the super class gets resolved by java_complete_class */
1924 else if (TREE_CODE (super_class) == POINTER_TYPE)
1926 if (TREE_TYPE (super_class) != NULL_TREE)
1927 super_class = TREE_TYPE (super_class);
1928 else
1930 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1931 super_class, NULL_TREE, this_class);
1932 if (!super_class)
1933 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1934 super_class = TREE_TYPE (super_class);
1937 if (!TYPE_SIZE (super_class))
1938 safe_layout_class (super_class);
1940 return super_class;
1943 void
1944 layout_class (this_class)
1945 tree this_class;
1947 tree super_class = CLASSTYPE_SUPER (this_class);
1948 tree field;
1950 class_list = tree_cons (this_class, NULL_TREE, class_list);
1951 if (CLASS_BEING_LAIDOUT (this_class))
1953 char buffer [1024];
1954 char *report;
1955 tree current;
1957 sprintf (buffer, " with `%s'",
1958 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1959 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1961 for (current = TREE_CHAIN (class_list); current;
1962 current = TREE_CHAIN (current))
1964 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1965 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1966 IDENTIFIER_POINTER (DECL_NAME (decl)),
1967 DECL_SOURCE_FILE (decl),
1968 DECL_SOURCE_LINE (decl));
1969 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1971 obstack_1grow (&temporary_obstack, '\0');
1972 report = obstack_finish (&temporary_obstack);
1973 cyclic_inheritance_report = ggc_strdup (report);
1974 obstack_free (&temporary_obstack, report);
1975 TYPE_SIZE (this_class) = error_mark_node;
1976 return;
1978 CLASS_BEING_LAIDOUT (this_class) = 1;
1980 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1982 tree maybe_super_class
1983 = maybe_layout_super_class (super_class, this_class);
1984 if (maybe_super_class == NULL
1985 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1987 TYPE_SIZE (this_class) = error_mark_node;
1988 CLASS_BEING_LAIDOUT (this_class) = 0;
1989 class_list = TREE_CHAIN (class_list);
1990 return;
1992 if (TYPE_SIZE (this_class) == NULL_TREE)
1993 push_super_field (this_class, maybe_super_class);
1996 for (field = TYPE_FIELDS (this_class);
1997 field != NULL_TREE; field = TREE_CHAIN (field))
1999 if (FIELD_STATIC (field))
2001 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
2002 SET_DECL_ASSEMBLER_NAME (field,
2003 java_mangle_decl
2004 (&temporary_obstack, field));
2008 layout_type (this_class);
2010 /* Also recursively load/layout any superinterfaces, but only if class was
2011 loaded from bytecode. The source parser will take care of this itself. */
2012 if (!CLASS_FROM_SOURCE_P (this_class))
2014 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
2016 if (basetype_vec)
2018 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
2019 int i;
2020 for (i = n; i > 0; i--)
2022 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
2023 tree super_interface = BINFO_TYPE (vec_elt);
2025 tree maybe_super_interface
2026 = maybe_layout_super_class (super_interface, NULL_TREE);
2027 if (maybe_super_interface == NULL
2028 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2030 TYPE_SIZE (this_class) = error_mark_node;
2031 CLASS_BEING_LAIDOUT (this_class) = 0;
2032 class_list = TREE_CHAIN (class_list);
2033 return;
2039 /* Convert the size back to an SI integer value */
2040 TYPE_SIZE_UNIT (this_class) =
2041 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2043 CLASS_BEING_LAIDOUT (this_class) = 0;
2044 class_list = TREE_CHAIN (class_list);
2047 void
2048 layout_class_methods (this_class)
2049 tree this_class;
2051 tree method_decl, dtable_count;
2052 tree super_class, handle_type;
2054 if (TYPE_NVIRTUALS (this_class))
2055 return;
2057 super_class = CLASSTYPE_SUPER (this_class);
2058 handle_type = CLASS_TO_HANDLE_TYPE (this_class);
2060 if (super_class)
2062 super_class = maybe_layout_super_class (super_class, this_class);
2063 if (!TYPE_NVIRTUALS (super_class))
2064 layout_class_methods (super_class);
2065 dtable_count = TYPE_NVIRTUALS (super_class);
2067 else
2068 dtable_count = integer_zero_node;
2070 TYPE_METHODS (handle_type) = nreverse (TYPE_METHODS (handle_type));
2072 for (method_decl = TYPE_METHODS (handle_type);
2073 method_decl; method_decl = TREE_CHAIN (method_decl))
2074 dtable_count = layout_class_method (this_class, super_class,
2075 method_decl, dtable_count);
2077 TYPE_NVIRTUALS (this_class) = dtable_count;
2079 #ifdef JAVA_USE_HANDLES
2080 layout_type (handle_type);
2081 #endif
2084 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
2085 and 1 if STR is "greater" than NAME. */
2087 /* Lay METHOD_DECL out, returning a possibly new value of
2088 DTABLE_COUNT. Also mangle the method's name. */
2090 tree
2091 layout_class_method (this_class, super_class, method_decl, dtable_count)
2092 tree this_class, super_class, method_decl, dtable_count;
2094 tree method_name = DECL_NAME (method_decl);
2096 TREE_PUBLIC (method_decl) = 1;
2098 /* This is a good occasion to mangle the method's name */
2099 SET_DECL_ASSEMBLER_NAME (method_decl,
2100 java_mangle_decl (&temporary_obstack,
2101 method_decl));
2102 /* We don't generate a RTL for the method if it's abstract, or if
2103 it's an interface method that isn't clinit. */
2104 if (! METHOD_ABSTRACT (method_decl)
2105 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2106 && (DECL_CLINIT_P (method_decl))))
2107 make_decl_rtl (method_decl, NULL);
2109 if (ID_INIT_P (method_name))
2111 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2112 const char *ptr;
2113 for (ptr = p; *ptr; )
2115 if (*ptr++ == '.')
2116 p = ptr;
2118 DECL_CONSTRUCTOR_P (method_decl) = 1;
2119 build_java_argument_signature (TREE_TYPE (method_decl));
2121 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
2123 tree method_sig =
2124 build_java_argument_signature (TREE_TYPE (method_decl));
2125 tree super_method = lookup_argument_method (super_class, method_name,
2126 method_sig);
2127 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
2129 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
2130 if (DECL_VINDEX (method_decl) == NULL_TREE
2131 && !CLASS_FROM_SOURCE_P (this_class))
2132 error_with_decl (method_decl,
2133 "non-static method '%s' overrides static method");
2135 else if (! METHOD_FINAL (method_decl)
2136 && ! METHOD_PRIVATE (method_decl)
2137 && ! CLASS_FINAL (TYPE_NAME (this_class))
2138 && dtable_count)
2140 DECL_VINDEX (method_decl) = dtable_count;
2141 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
2142 dtable_count, integer_one_node));
2146 return dtable_count;
2149 void
2150 register_class ()
2152 /* END does not need to be registered with the garbage collector
2153 because it always points into the list given by REGISTERED_CLASS,
2154 and that variable is registered with the collector. */
2155 static tree end;
2156 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2157 tree current = copy_node (node);
2159 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2160 if (!registered_class)
2161 registered_class = current;
2162 else
2163 TREE_CHAIN (end) = current;
2165 end = current;
2168 /* Emit something to register classes at start-up time.
2170 The preferred mechanism is through the .jcr section, which contain
2171 a list of pointers to classes which get registered during
2172 constructor invoction time. The fallback mechanism is to generate
2173 a `constructor' function which calls _Jv_RegisterClass for each
2174 class in this file. */
2176 void
2177 emit_register_classes ()
2179 /* ??? This isn't quite the correct test. We also have to know
2180 that the target is using gcc's crtbegin/crtend objects rather
2181 than the ones that come with the operating system. */
2182 if (SUPPORTS_WEAK && targetm.have_named_sections)
2184 #ifdef JCR_SECTION_NAME
2185 tree t;
2186 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2187 assemble_align (POINTER_SIZE);
2188 for (t = registered_class; t; t = TREE_CHAIN (t))
2189 assemble_integer (XEXP (DECL_RTL (t), 0),
2190 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2191 #else
2192 abort ();
2193 #endif
2195 else
2197 extern tree get_file_function_name PARAMS ((int));
2198 tree init_name = get_file_function_name ('I');
2199 tree init_type = build_function_type (void_type_node, end_params_node);
2200 tree init_decl;
2201 tree t;
2203 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
2204 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
2205 TREE_STATIC (init_decl) = 1;
2206 current_function_decl = init_decl;
2207 DECL_RESULT (init_decl) = build_decl (RESULT_DECL, NULL_TREE,
2208 void_type_node);
2210 /* It can be a static function as long as collect2 does not have
2211 to scan the object file to find its ctor/dtor routine. */
2212 TREE_PUBLIC (init_decl) = ! targetm.have_ctors_dtors;
2214 /* Suppress spurious warnings. */
2215 TREE_USED (init_decl) = 1;
2217 pushlevel (0);
2218 make_decl_rtl (init_decl, NULL);
2219 init_function_start (init_decl, input_filename, 0);
2220 expand_function_start (init_decl, 0);
2222 /* Do not allow the function to be deferred. */
2223 current_function_cannot_inline
2224 = "static constructors and destructors cannot be inlined";
2226 for ( t = registered_class; t; t = TREE_CHAIN (t))
2227 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
2228 XEXP (DECL_RTL (t), 0), Pmode);
2230 expand_function_end (input_filename, 0, 0);
2231 poplevel (1, 0, 1);
2232 rest_of_compilation (init_decl);
2233 current_function_decl = NULL_TREE;
2235 if (targetm.have_ctors_dtors)
2236 (* targetm.asm_out.constructor) (XEXP (DECL_RTL (init_decl), 0),
2237 DEFAULT_INIT_PRIORITY);
2241 /* Make a method_symbol_type (_Jv_MethodSymbol) node for METHOD. */
2243 tree
2244 build_method_symbols_entry (tree method)
2246 tree clname, name, signature, method_symbol;
2248 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (method))));
2249 name = build_utf8_ref (DECL_NAME (method));
2250 signature = build_java_signature (TREE_TYPE (method));
2251 signature = build_utf8_ref (unmangle_classname
2252 (IDENTIFIER_POINTER (signature),
2253 IDENTIFIER_LENGTH (signature)));
2255 START_RECORD_CONSTRUCTOR (method_symbol, method_symbol_type);
2256 PUSH_FIELD_VALUE (method_symbol, "clname", clname);
2257 PUSH_FIELD_VALUE (method_symbol, "name", name);
2258 PUSH_FIELD_VALUE (method_symbol, "signature", signature);
2259 FINISH_RECORD_CONSTRUCTOR (method_symbol);
2260 TREE_CONSTANT (method_symbol) = 1;
2262 return method_symbol;
2265 /* Emit the offset symbols table for indirect virtual dispatch. */
2267 void
2268 emit_offset_symbol_table ()
2270 tree method_list, method, table, list, null_symbol;
2271 tree otable_bound, otable_array_type;
2272 int index;
2274 /* Only emit an offset table if this translation unit actually made virtual
2275 calls. */
2276 if (otable_methods == NULL_TREE)
2277 return;
2279 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2280 index = 0;
2281 method_list = otable_methods;
2282 list = NULL_TREE;
2283 while (method_list != NULL_TREE)
2285 method = TREE_VALUE (method_list);
2286 list = tree_cons (NULL_TREE, build_method_symbols_entry (method), list);
2287 method_list = TREE_CHAIN (method_list);
2288 index++;
2291 /* Terminate the list with a "null" entry. */
2292 START_RECORD_CONSTRUCTOR (null_symbol, method_symbol_type);
2293 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2294 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2295 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2296 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2297 TREE_CONSTANT (null_symbol) = 1;
2298 list = tree_cons (NULL_TREE, null_symbol, list);
2300 /* Put the list in the right order and make it a constructor. */
2301 list = nreverse (list);
2302 table = build (CONSTRUCTOR, method_symbols_array_type, NULL_TREE, list);
2304 /* Make it the initial value for otable_syms and emit the decl. */
2305 DECL_INITIAL (otable_syms_decl) = table;
2306 DECL_ARTIFICIAL (otable_syms_decl) = 1;
2307 DECL_IGNORED_P (otable_syms_decl) = 1;
2308 rest_of_decl_compilation (otable_syms_decl, NULL, 1, 0);
2310 /* Now that its size is known, redefine otable as an uninitialized static
2311 array of INDEX + 1 integers. The extra entry is used by the runtime
2312 to track whether the otable has been initialized. */
2313 otable_bound = build_index_type (build_int_2 (index, 0));
2314 otable_array_type = build_array_type (integer_type_node, otable_bound);
2315 otable_decl = build_decl (VAR_DECL, get_identifier ("otable"),
2316 otable_array_type);
2317 TREE_STATIC (otable_decl) = 1;
2318 TREE_READONLY (otable_decl) = 1;
2319 rest_of_decl_compilation (otable_decl, NULL, 1, 0);
2322 void
2323 init_class_processing ()
2325 registerClass_libfunc = gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterClass");
2326 registerResource_libfunc =
2327 gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterResource");
2328 ggc_add_tree_root (class_roots, ARRAY_SIZE (class_roots));
2329 fields_ident = get_identifier ("fields");
2330 info_ident = get_identifier ("info");
2331 ggc_add_rtx_root (&registerClass_libfunc, 1);
2332 gcc_obstack_init (&temporary_obstack);