2001-07-18 Alexandre Petit-Bianco <apbianco@redhat.com>
[official-gcc.git] / gcc / java / class.c
blob3b56f53ff51f4202722347b7b531eccc0bcc80cc
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
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 "ggc.h"
41 static tree make_method_value PARAMS ((tree));
42 static tree build_java_method_type PARAMS ((tree, tree, int));
43 static int32 hashUtf8String PARAMS ((const char *, int));
44 static tree make_field_value PARAMS ((tree));
45 static tree get_dispatch_vector PARAMS ((tree));
46 static tree get_dispatch_table PARAMS ((tree, tree));
47 static void add_interface_do PARAMS ((tree, tree, int));
48 static tree maybe_layout_super_class PARAMS ((tree, tree));
49 static int assume_compiled PARAMS ((const char *));
50 static struct hash_entry *init_test_hash_newfunc PARAMS ((struct hash_entry *,
51 struct hash_table *,
52 hash_table_key));
53 static rtx registerClass_libfunc;
55 extern struct obstack permanent_obstack;
56 struct obstack temporary_obstack;
58 /* The compiler generates different code depending on whether or not
59 it can assume certain classes have been compiled down to native
60 code or not. The compiler options -fassume-compiled= and
61 -fno-assume-compiled= are used to create a tree of
62 assume_compiled_node objects. This tree is queried to determine if
63 a class is assume to be compiled or not. Each node in the tree
64 represents either a package or a specific class. */
66 typedef struct assume_compiled_node_struct
68 /* The class or package name. */
69 const char *ident;
71 /* Non-zero if this represents an exclusion. */
72 int excludep;
74 /* Pointers to other nodes in the tree. */
75 struct assume_compiled_node_struct *parent;
76 struct assume_compiled_node_struct *sibling;
77 struct assume_compiled_node_struct *child;
78 } assume_compiled_node;
80 static assume_compiled_node *find_assume_compiled_node
81 PARAMS ((assume_compiled_node *, const char *));
83 /* This is the root of the include/exclude tree. */
85 static assume_compiled_node *assume_compiled_tree;
87 static tree class_roots[5]
88 = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
89 #define registered_class class_roots[0]
90 #define fields_ident class_roots[1] /* get_identifier ("fields") */
91 #define info_ident class_roots[2] /* get_identifier ("info") */
92 #define class_list class_roots[3]
93 #define class_dtable_decl class_roots[4]
95 /* Return the node that most closely represents the class whose name
96 is IDENT. Start the search from NODE. Return NULL if an
97 appropriate node does not exist. */
99 static assume_compiled_node *
100 find_assume_compiled_node (node, ident)
101 assume_compiled_node *node;
102 const char *ident;
104 while (node)
106 size_t node_ident_length = strlen (node->ident);
108 /* node_ident_length is zero at the root of the tree. If the
109 identifiers are the same length, then we have matching
110 classes. Otherwise check if we've matched an enclosing
111 package name. */
113 if (node_ident_length == 0
114 || (strncmp (ident, node->ident, node_ident_length) == 0
115 && (strlen (ident) == node_ident_length
116 || ident[node_ident_length] == '.')))
118 /* We've found a match, however, there might be a more
119 specific match. */
121 assume_compiled_node *found = find_assume_compiled_node (node->child,
122 ident);
123 if (found)
124 return found;
125 else
126 return node;
129 /* No match yet. Continue through the sibling list. */
130 node = node->sibling;
133 /* No match at all in this tree. */
134 return NULL;
137 /* Add a new IDENT to the include/exclude tree. It's an exclusion
138 if EXCLUDEP is non-zero. */
140 void
141 add_assume_compiled (ident, excludep)
142 const char *ident;
143 int excludep;
145 assume_compiled_node *parent;
146 assume_compiled_node *node =
147 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
149 node->ident = xstrdup (ident);
150 node->excludep = excludep;
151 node->child = NULL;
153 /* Create the root of the tree if it doesn't exist yet. */
155 if (NULL == assume_compiled_tree)
157 assume_compiled_tree =
158 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
159 assume_compiled_tree->ident = "";
160 assume_compiled_tree->excludep = 0;
161 assume_compiled_tree->sibling = NULL;
162 assume_compiled_tree->child = NULL;
163 assume_compiled_tree->parent = NULL;
166 /* Calling the function with the empty string means we're setting
167 excludep for the root of the hierarchy. */
169 if (0 == ident[0])
171 assume_compiled_tree->excludep = excludep;
172 return;
175 /* Find the parent node for this new node. PARENT will either be a
176 class or a package name. Adjust PARENT accordingly. */
178 parent = find_assume_compiled_node (assume_compiled_tree, ident);
179 if (ident[strlen (parent->ident)] != '.')
180 parent = parent->parent;
182 /* Insert NODE into the tree. */
184 node->parent = parent;
185 node->sibling = parent->child;
186 parent->child = node;
189 /* Returns non-zero if IDENT is the name of a class that the compiler
190 should assume has been compiled to FIXME */
192 static int
193 assume_compiled (ident)
194 const char *ident;
196 assume_compiled_node *i;
197 int result;
199 if (NULL == assume_compiled_tree)
200 return 1;
202 i = find_assume_compiled_node (assume_compiled_tree,
203 ident);
205 result = ! i->excludep;
207 return (result);
210 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
211 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
212 Also, PREFIX is prepended, and SUFFIX is appended. */
214 tree
215 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
216 const char* old_name;
217 int old_length;
218 const char *prefix;
219 int old_char;
220 int new_char;
221 const char *suffix;
223 int prefix_len = strlen (prefix);
224 int suffix_len = strlen (suffix);
225 int i = prefix_len + old_length + suffix_len + 1;
226 #ifdef __GNUC__
227 char buffer[i];
228 #else
229 char *buffer = (char *)alloca (i);
230 #endif
231 strcpy (buffer, prefix);
232 for (i = 0; i < old_length; i++)
234 char ch = old_name[i];
235 if (ch == old_char)
236 ch = new_char;
237 buffer[prefix_len + i] = ch;
239 strcpy (buffer + prefix_len + old_length, suffix);
240 return get_identifier (buffer);
243 /* Return an IDENTIFIER_NODE the same as OLD_ID,
244 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
245 Also, PREFIX is prepended, and SUFFIX is appended. */
247 tree
248 identifier_subst (old_id, prefix, old_char, new_char, suffix)
249 const tree old_id;
250 const char *prefix;
251 int old_char;
252 int new_char;
253 const char *suffix;
255 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
256 prefix, old_char, new_char, suffix);
259 /* Generate a valid C identifier from the name of the class TYPE,
260 prefixed by PREFIX. */
262 tree
263 mangled_classname (prefix, type)
264 const char *prefix;
265 tree type;
267 tree ident = TYPE_NAME (type);
268 if (TREE_CODE (ident) != IDENTIFIER_NODE)
269 ident = DECL_NAME (ident);
270 return identifier_subst (ident, prefix, '.', '_', "");
273 tree
274 make_class ()
276 tree type;
277 type = make_node (RECORD_TYPE);
278 #ifdef JAVA_USE_HANDLES
279 tree field1 = build_decl (FIELD_DECL, get_identifier ("obj"),
280 build_pointer_type (type));
281 tree field2 = build_decl (FIELD_DECL, get_identifier ("methods"),
282 methodtable_ptr_type);
283 tree handle_type = make_node (RECORD_TYPE);
284 TREE_CHAIN (field1) = field2;
285 TYPE_FIELDS (handle_type) = field1;
286 TYPE_BINFO (type) = make_tree_vec (7);
287 TYPE_BINFO (handle_type) = make_tree_vec (7);
288 BINFO_HANDLE (TYPE_BINFO (handle_type)) = type;
289 BINFO_HANDLE (TYPE_BINFO (type)) = handle_type;
290 #else
291 TYPE_BINFO (type) = make_tree_vec (6);
292 #endif
293 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
295 return type;
298 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
299 and where each of the constituents is separated by '/',
300 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
302 tree
303 unmangle_classname (name, name_length)
304 const char *name; int name_length;
306 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
307 /* It's not sufficient to compare to_return and get_identifier
308 (name) to determine whether to_return is qualified. There are
309 cases in signature analysis where name will be stripped of a
310 trailing ';'. */
311 name = IDENTIFIER_POINTER (to_return);
312 while (*name)
313 if (*name++ == '.')
315 QUALIFIED_P (to_return) = 1;
316 break;
319 return to_return;
322 tree
323 push_class (class_type, class_name)
324 tree class_type, class_name;
326 tree decl, signature;
327 const char *save_input_filename = input_filename;
328 int save_lineno = lineno;
329 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
330 CLASS_P (class_type) = 1;
331 input_filename = IDENTIFIER_POINTER (source_name);
332 lineno = 0;
333 decl = build_decl (TYPE_DECL, class_name, class_type);
335 /* dbxout needs a DECL_SIZE if in gstabs mode */
336 DECL_SIZE (decl) = integer_zero_node;
338 input_filename = save_input_filename;
339 lineno = save_lineno;
340 signature = identifier_subst (class_name, "L", '.', '/', ";");
341 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
343 /* Setting DECL_ARTIFICAL forces dbxout.c to specific the type is
344 both a typedef and in the struct name-space. We may want to re-visit
345 this later, but for now it reduces the changes needed for gdb. */
346 DECL_ARTIFICIAL (decl) = 1;
348 pushdecl_top_level (decl);
349 #ifdef JAVA_USE_HANDLES
351 tree handle_name = identifier_subst (class_name,
352 "Handle$", '.', '.', "");
353 tree handle_decl = build_decl (TYPE_DECL, handle_name,
354 CLASS_TO_HANDLE_TYPE (class_type));
355 pushdecl (handle_decl);
357 #endif
359 return decl;
362 /* Finds the (global) class named NAME. Creates the class if not found.
363 Also creates associated TYPE_DECL.
364 Does not check if the class actually exists, load the class,
365 fill in field or methods, or do layout_type. */
367 tree
368 lookup_class (name)
369 tree name;
371 tree decl = IDENTIFIER_CLASS_VALUE (name);
372 if (decl == NULL_TREE)
373 decl = push_class (make_class (), name);
374 return TREE_TYPE (decl);
377 void
378 set_super_info (access_flags, this_class, super_class, interfaces_count)
379 int access_flags;
380 tree this_class;
381 tree super_class;
382 int interfaces_count;
384 int total_supers = interfaces_count;
385 tree class_decl = TYPE_NAME (this_class);
386 if (super_class)
387 total_supers++;
389 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
390 if (super_class)
392 tree super_binfo = make_tree_vec (6);
393 BINFO_TYPE (super_binfo) = super_class;
394 BINFO_OFFSET (super_binfo) = integer_zero_node;
395 TREE_VIA_PUBLIC (super_binfo) = 1;
396 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
397 = super_binfo;
398 CLASS_HAS_SUPER (this_class) = 1;
401 set_class_decl_access_flags (access_flags, class_decl);
404 void
405 set_class_decl_access_flags (access_flags, class_decl)
406 int access_flags;
407 tree class_decl;
409 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
410 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
411 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
412 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
413 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
414 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
415 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
416 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
419 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
420 direct sub-classes of Object are 1, and so on. */
423 class_depth (clas)
424 tree clas;
426 int depth = 0;
427 if (! CLASS_LOADED_P (clas))
428 load_class (clas, 1);
429 if (TYPE_SIZE (clas) == error_mark_node)
430 return -1;
431 while (clas != object_type_node)
433 depth++;
434 clas = TYPE_BINFO_BASETYPE (clas, 0);
436 return depth;
439 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
442 interface_of_p (type1, type2)
443 tree type1, type2;
445 int n, i;
446 tree basetype_vec;
448 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
449 return 0;
450 n = TREE_VEC_LENGTH (basetype_vec);
451 for (i = 0; i < n; i++)
453 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
454 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
455 return 1;
457 for (i = 0; i < n; i++)
459 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
460 if (vec_elt && BINFO_TYPE (vec_elt)
461 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
462 return 1;
464 return 0;
467 /* Return true iff TYPE1 inherits from TYPE2. */
470 inherits_from_p (type1, type2)
471 tree type1, type2;
473 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
475 if (type1 == type2)
476 return 1;
477 type1 = CLASSTYPE_SUPER (type1);
479 return 0;
482 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
485 enclosing_context_p (type1, type2)
486 tree type1, type2;
488 if (!INNER_CLASS_TYPE_P (type2))
489 return 0;
491 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
492 type2;
493 type2 = (INNER_CLASS_TYPE_P (type2) ?
494 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
496 if (type2 == type1)
497 return 1;
500 return 0;
503 /* Return 1 iff there exists a common enclosing context between TYPE1
504 and TYPE2. */
506 int common_enclosing_context_p (type1, type2)
507 tree type1, type2;
509 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
510 return 0;
512 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
513 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
514 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
516 tree current;
517 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
518 current = (PURE_INNER_CLASS_TYPE_P (current) ?
519 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
520 NULL_TREE))
521 if (type1 == current)
522 return 1;
524 return 0;
527 static void
528 add_interface_do (basetype_vec, interface_class, i)
529 tree basetype_vec, interface_class;
530 int i;
532 tree interface_binfo = make_tree_vec (6);
533 BINFO_TYPE (interface_binfo) = interface_class;
534 BINFO_OFFSET (interface_binfo) = integer_zero_node;
535 TREE_VIA_VIRTUAL (interface_binfo) = 1;
536 TREE_VIA_PUBLIC (interface_binfo) = 1;
537 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
540 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
541 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
542 if attempt is made to add it twice. */
544 tree
545 maybe_add_interface (this_class, interface_class)
546 tree this_class, interface_class;
548 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
549 int i;
550 int n = TREE_VEC_LENGTH (basetype_vec);
551 for (i = 0; ; i++)
553 if (i >= n)
555 error ("internal error - too many interface type");
556 return NULL_TREE;
558 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
559 break;
560 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
561 return interface_class;
563 add_interface_do (basetype_vec, interface_class, i);
564 return NULL_TREE;
567 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
569 void
570 add_interface (this_class, interface_class)
571 tree this_class, interface_class;
573 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
574 int i;
575 int n = TREE_VEC_LENGTH (basetype_vec);
576 for (i = 0; ; i++)
578 if (i >= n)
580 error ("internal error - too many interface type");
581 return;
583 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
584 break;
586 add_interface_do (basetype_vec, interface_class, i);
589 #if 0
590 /* Return the address of a pointer to the first FUNCTION_DECL
591 in the list (*LIST) whose DECL_NAME is NAME. */
593 static tree *
594 find_named_method (list, name)
595 tree *list;
596 tree name;
598 while (*list && DECL_NAME (*list) != name)
599 list = &TREE_CHAIN (*list);
600 return list;
602 #endif
604 static tree
605 build_java_method_type (fntype, this_class, access_flags)
606 tree fntype;
607 tree this_class;
608 int access_flags;
610 if (access_flags & ACC_STATIC)
611 return fntype;
612 return build_method_type (CLASS_TO_HANDLE_TYPE (this_class), fntype);
615 static struct hash_entry *
616 init_test_hash_newfunc (entry, table, string)
617 struct hash_entry *entry;
618 struct hash_table *table;
619 hash_table_key string ATTRIBUTE_UNUSED;
621 struct init_test_hash_entry *ret = (struct init_test_hash_entry *) entry;
622 if (ret == NULL)
624 ret = ((struct init_test_hash_entry *)
625 hash_allocate (table, sizeof (struct init_test_hash_entry)));
626 if (ret == NULL)
627 return NULL;
629 ret->init_test_decl = 0;
630 return (struct hash_entry *) ret;
633 /* Hash table helpers. Also reused in find_applicable_accessible_methods_list
634 (parse.y). The hash of a tree node is its pointer value, comparison
635 is direct. */
637 unsigned long
638 java_hash_hash_tree_node (k)
639 hash_table_key k;
641 return (long) k;
644 bool
645 java_hash_compare_tree_node (k1, k2)
646 hash_table_key k1;
647 hash_table_key k2;
649 return ((char*) k1 == (char*) k2);
652 tree
653 add_method_1 (handle_class, access_flags, name, function_type)
654 tree handle_class;
655 int access_flags;
656 tree name;
657 tree function_type;
659 tree method_type, fndecl;
661 method_type = build_java_method_type (function_type,
662 handle_class, access_flags);
664 fndecl = build_decl (FUNCTION_DECL, name, method_type);
665 DECL_CONTEXT (fndecl) = handle_class;
667 DECL_LANG_SPECIFIC (fndecl)
668 = (struct lang_decl *) ggc_alloc_cleared (sizeof (struct lang_decl));
670 /* Initialize the static initializer test table. */
671 hash_table_init (&DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
672 init_test_hash_newfunc, java_hash_hash_tree_node,
673 java_hash_compare_tree_node);
675 TREE_CHAIN (fndecl) = TYPE_METHODS (handle_class);
676 TYPE_METHODS (handle_class) = fndecl;
678 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
679 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
680 if (access_flags & ACC_PRIVATE)
681 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
682 if (access_flags & ACC_NATIVE)
684 METHOD_NATIVE (fndecl) = 1;
685 DECL_EXTERNAL (fndecl) = 1;
687 if (access_flags & ACC_STATIC)
688 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
689 if (access_flags & ACC_FINAL)
690 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
691 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
692 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
693 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
694 return fndecl;
697 /* Add a method to THIS_CLASS.
698 The method's name is NAME.
699 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
701 tree
702 add_method (this_class, access_flags, name, method_sig)
703 tree this_class;
704 int access_flags;
705 tree name;
706 tree method_sig;
708 tree handle_class = CLASS_TO_HANDLE_TYPE (this_class);
709 tree function_type, fndecl;
710 const unsigned char *sig
711 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
713 if (sig[0] != '(')
714 fatal_error ("bad method signature");
716 function_type = get_type_from_signature (method_sig);
717 fndecl = add_method_1 (handle_class, access_flags, name, function_type);
718 set_java_signature (TREE_TYPE (fndecl), method_sig);
719 return fndecl;
722 tree
723 add_field (class, name, field_type, flags)
724 tree class;
725 tree name;
726 tree field_type;
727 int flags;
729 int is_static = (flags & ACC_STATIC) != 0;
730 tree field;
731 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
732 TREE_CHAIN (field) = TYPE_FIELDS (class);
733 TYPE_FIELDS (class) = field;
734 DECL_CONTEXT (field) = class;
736 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
737 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
738 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
739 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
740 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
741 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
742 if (is_static)
744 FIELD_STATIC (field) = 1;
745 /* Always make field externally visible. This is required so
746 that native methods can always access the field. */
747 TREE_PUBLIC (field) = 1;
749 return field;
752 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
754 void
755 set_constant_value (field, constant)
756 tree field, constant;
758 if (field == NULL_TREE)
759 warning ("misplaced ConstantValue attribute (not in any field)");
760 else if (DECL_INITIAL (field) != NULL_TREE)
761 warning ("duplicate ConstanValue atribute for field '%s'",
762 IDENTIFIER_POINTER (DECL_NAME (field)));
763 else
765 DECL_INITIAL (field) = constant;
766 if (FIELD_FINAL (field))
767 DECL_FIELD_FINAL_IUD (field) = 1;
771 /* Count the number of Unicode chars encoded in a given Ut8 string. */
773 #if 0
775 strLengthUtf8 (str, len)
776 char *str;
777 int len;
779 register unsigned char* ptr = (unsigned char*) str;
780 register unsigned char *limit = ptr + len;
781 int str_length = 0;
782 for (; ptr < limit; str_length++) {
783 if (UTF8_GET (ptr, limit) < 0)
784 return -1;
786 return str_length;
788 #endif
791 /* Calculate a hash value for a string encoded in Utf8 format.
792 * This returns the same hash value as specified for java.lang.String.hashCode.
795 static int32
796 hashUtf8String (str, len)
797 const char *str;
798 int len;
800 register const unsigned char* ptr = (const unsigned char*) str;
801 register const unsigned char *limit = ptr + len;
802 int32 hash = 0;
803 for (; ptr < limit;)
805 int ch = UTF8_GET (ptr, limit);
806 /* Updated specification from
807 http://www.javasoft.com/docs/books/jls/clarify.html. */
808 hash = (31 * hash) + ch;
810 return hash;
813 tree utf8_decl_list = NULL_TREE;
815 tree
816 build_utf8_ref (name)
817 tree name;
819 const char * name_ptr = IDENTIFIER_POINTER(name);
820 int name_len = IDENTIFIER_LENGTH(name);
821 char buf[60];
822 tree ctype, field = NULL_TREE, str_type, cinit, string;
823 static int utf8_count = 0;
824 int name_hash;
825 tree ref = IDENTIFIER_UTF8_REF (name);
826 tree decl;
827 if (ref != NULL_TREE)
828 return ref;
830 ctype = make_node (RECORD_TYPE);
831 str_type = build_prim_array_type (unsigned_byte_type_node,
832 name_len + 1); /* Allow for final '\0'. */
833 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
834 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
835 PUSH_FIELD (ctype, field, "data", str_type);
836 FINISH_RECORD (ctype);
837 START_RECORD_CONSTRUCTOR (cinit, ctype);
838 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
839 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
840 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
841 string = build_string (name_len, name_ptr);
842 TREE_TYPE (string) = str_type;
843 PUSH_FIELD_VALUE (cinit, "data", string);
844 FINISH_RECORD_CONSTRUCTOR (cinit);
845 TREE_CONSTANT (cinit) = 1;
847 /* Generate a unique-enough identifier. */
848 sprintf(buf, "_Utf%d", ++utf8_count);
850 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
851 /* FIXME get some way to force this into .text, not .data. */
852 TREE_STATIC (decl) = 1;
853 DECL_ARTIFICIAL (decl) = 1;
854 DECL_IGNORED_P (decl) = 1;
855 TREE_READONLY (decl) = 1;
856 TREE_THIS_VOLATILE (decl) = 0;
857 DECL_INITIAL (decl) = cinit;
858 TREE_CHAIN (decl) = utf8_decl_list;
859 layout_decl (decl, 0);
860 pushdecl (decl);
861 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
862 utf8_decl_list = decl;
863 make_decl_rtl (decl, (char*) 0);
864 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
865 IDENTIFIER_UTF8_REF (name) = ref;
866 return ref;
869 /* Build a reference to the class TYPE.
870 Also handles primitive types and array types. */
872 tree
873 build_class_ref (type)
874 tree type;
876 int is_compiled = is_compiled_class (type);
877 if (is_compiled)
879 tree ref, decl_name, decl;
880 if (TREE_CODE (type) == POINTER_TYPE)
881 type = TREE_TYPE (type);
882 if (TREE_CODE (type) == RECORD_TYPE)
884 if (TYPE_SIZE (type) == error_mark_node)
885 return null_pointer_node;
886 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
887 "", '/', '/', ".class");
888 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
889 if (decl == NULL_TREE)
891 decl = build_decl (VAR_DECL, decl_name, class_type_node);
892 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
893 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
894 TREE_STATIC (decl) = 1;
895 TREE_PUBLIC (decl) = 1;
896 DECL_IGNORED_P (decl) = 1;
897 DECL_ARTIFICIAL (decl) = 1;
898 SET_DECL_ASSEMBLER_NAME (decl,
899 java_mangle_class_field
900 (&temporary_obstack, type));
901 make_decl_rtl (decl, NULL);
902 pushdecl_top_level (decl);
903 if (is_compiled == 1)
904 DECL_EXTERNAL (decl) = 1;
907 else
909 const char *name;
910 char buffer[25];
911 if (flag_emit_class_files)
913 const char *prim_class_name;
914 tree prim_class;
915 if (type == char_type_node)
916 prim_class_name = "java.lang.Character";
917 else if (type == boolean_type_node)
918 prim_class_name = "java.lang.Boolean";
919 else if (type == byte_type_node)
920 prim_class_name = "java.lang.Byte";
921 else if (type == short_type_node)
922 prim_class_name = "java.lang.Short";
923 else if (type == int_type_node)
924 prim_class_name = "java.lang.Integer";
925 else if (type == long_type_node)
926 prim_class_name = "java.lang.Long";
927 else if (type == float_type_node)
928 prim_class_name = "java.lang.Float";
929 else if (type == double_type_node)
930 prim_class_name = "java.lang.Double";
931 else if (type == void_type_node)
932 prim_class_name = "java.lang.Void";
933 else
934 abort ();
936 prim_class = lookup_class (get_identifier (prim_class_name));
937 return build (COMPONENT_REF, NULL_TREE,
938 prim_class, TYPE_identifier_node);
940 decl_name = TYPE_NAME (type);
941 if (TREE_CODE (decl_name) == TYPE_DECL)
942 decl_name = DECL_NAME (decl_name);
943 name = IDENTIFIER_POINTER (decl_name);
944 if (strncmp (name, "promoted_", 9) == 0)
945 name += 9;
946 sprintf (buffer, "_Jv_%sClass", name);
947 decl_name = get_identifier (buffer);
948 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
949 if (decl == NULL_TREE)
951 decl = build_decl (VAR_DECL, decl_name, class_type_node);
952 TREE_STATIC (decl) = 1;
953 TREE_PUBLIC (decl) = 1;
954 make_decl_rtl (decl, NULL);
955 pushdecl_top_level (decl);
956 if (is_compiled == 1)
957 DECL_EXTERNAL (decl) = 1;
961 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
962 return ref;
964 else
966 int index;
967 tree cl;
968 index = alloc_class_constant (type);
969 cl = build_ref_from_constant_pool (index);
970 TREE_TYPE (cl) = promote_type (class_ptr_type);
971 return cl;
975 tree
976 build_static_field_ref (fdecl)
977 tree fdecl;
979 tree fclass = DECL_CONTEXT (fdecl);
980 int is_compiled = is_compiled_class (fclass);
981 if (is_compiled)
983 if (!DECL_RTL_SET_P (fdecl))
985 if (is_compiled == 1)
986 DECL_EXTERNAL (fdecl) = 1;
987 make_decl_rtl (fdecl, NULL);
989 return fdecl;
991 else
993 /* Compile as:
994 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
995 tree ref = build_class_ref (fclass);
996 tree fld;
997 int field_index = 0;
998 ref = build1 (INDIRECT_REF, class_type_node, ref);
999 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1000 lookup_field (&class_type_node, fields_ident));
1002 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1004 if (fld == fdecl)
1005 break;
1006 if (fld == NULL_TREE)
1007 fatal_error ("field '%s' not found in class",
1008 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1009 if (FIELD_STATIC (fld))
1010 field_index++;
1012 field_index *= int_size_in_bytes (field_type_node);
1013 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1014 ref, build_int_2 (field_index, 0)));
1015 ref = build1 (INDIRECT_REF, field_type_node, ref);
1016 ref = build (COMPONENT_REF, field_info_union_node,
1017 ref, lookup_field (&field_type_node, info_ident));
1018 ref = build (COMPONENT_REF, ptr_type_node,
1019 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1020 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1025 get_access_flags_from_decl (decl)
1026 tree decl;
1028 int access_flags = 0;
1029 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1031 if (FIELD_STATIC (decl))
1032 access_flags |= ACC_STATIC;
1033 if (FIELD_PUBLIC (decl))
1034 access_flags |= ACC_PUBLIC;
1035 if (FIELD_PROTECTED (decl))
1036 access_flags |= ACC_PROTECTED;
1037 if (FIELD_PRIVATE (decl))
1038 access_flags |= ACC_PRIVATE;
1039 if (FIELD_FINAL (decl))
1040 access_flags |= ACC_FINAL;
1041 if (FIELD_VOLATILE (decl))
1042 access_flags |= ACC_VOLATILE;
1043 if (FIELD_TRANSIENT (decl))
1044 access_flags |= ACC_TRANSIENT;
1045 return access_flags;
1047 if (TREE_CODE (decl) == TYPE_DECL)
1049 if (CLASS_PUBLIC (decl))
1050 access_flags |= ACC_PUBLIC;
1051 if (CLASS_FINAL (decl))
1052 access_flags |= ACC_FINAL;
1053 if (CLASS_SUPER (decl))
1054 access_flags |= ACC_SUPER;
1055 if (CLASS_INTERFACE (decl))
1056 access_flags |= ACC_INTERFACE;
1057 if (CLASS_ABSTRACT (decl))
1058 access_flags |= ACC_ABSTRACT;
1059 if (CLASS_STATIC (decl))
1060 access_flags |= ACC_STATIC;
1061 if (CLASS_PRIVATE (decl))
1062 access_flags |= ACC_PRIVATE;
1063 if (CLASS_PROTECTED (decl))
1064 access_flags |= ACC_PROTECTED;
1065 return access_flags;
1067 if (TREE_CODE (decl) == FUNCTION_DECL)
1069 if (METHOD_PUBLIC (decl))
1070 access_flags |= ACC_PUBLIC;
1071 if (METHOD_PRIVATE (decl))
1072 access_flags |= ACC_PRIVATE;
1073 if (METHOD_PROTECTED (decl))
1074 access_flags |= ACC_PROTECTED;
1075 if (METHOD_STATIC (decl))
1076 access_flags |= ACC_STATIC;
1077 if (METHOD_FINAL (decl))
1078 access_flags |= ACC_FINAL;
1079 if (METHOD_SYNCHRONIZED (decl))
1080 access_flags |= ACC_SYNCHRONIZED;
1081 if (METHOD_NATIVE (decl))
1082 access_flags |= ACC_NATIVE;
1083 if (METHOD_ABSTRACT (decl))
1084 access_flags |= ACC_ABSTRACT;
1085 if (METHOD_TRANSIENT (decl))
1086 access_flags |= ACC_TRANSIENT;
1087 return access_flags;
1089 abort ();
1092 static tree
1093 make_field_value (fdecl)
1094 tree fdecl;
1096 tree finit;
1097 int flags;
1098 tree type = TREE_TYPE (fdecl);
1099 int resolved = is_compiled_class (type);
1101 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1102 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1103 if (resolved)
1104 type = build_class_ref (type);
1105 else
1107 tree signature = build_java_signature (type);
1109 type = build_utf8_ref (unmangle_classname
1110 (IDENTIFIER_POINTER (signature),
1111 IDENTIFIER_LENGTH (signature)));
1113 PUSH_FIELD_VALUE (finit, "type", type);
1115 flags = get_access_flags_from_decl (fdecl);
1116 if (! resolved)
1117 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1119 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1120 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1122 PUSH_FIELD_VALUE
1123 (finit, "info",
1124 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1125 build_tree_list
1126 ((FIELD_STATIC (fdecl)
1127 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1128 : TYPE_FIELDS (field_info_union_node)),
1129 (FIELD_STATIC (fdecl)
1130 ? build_address_of (build_static_field_ref (fdecl))
1131 : byte_position (fdecl)))));
1133 FINISH_RECORD_CONSTRUCTOR (finit);
1134 return finit;
1137 static tree
1138 make_method_value (mdecl)
1139 tree mdecl;
1141 tree minit;
1142 tree code;
1143 #define ACC_TRANSLATED 0x4000
1144 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1145 code = null_pointer_node;
1146 if (DECL_RTL_SET_P (mdecl))
1147 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1148 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1149 PUSH_FIELD_VALUE (minit, "name",
1150 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1151 init_identifier_node
1152 : DECL_NAME (mdecl)));
1154 tree signature = build_java_signature (TREE_TYPE (mdecl));
1155 PUSH_FIELD_VALUE (minit, "signature",
1156 (build_utf8_ref
1157 (unmangle_classname
1158 (IDENTIFIER_POINTER(signature),
1159 IDENTIFIER_LENGTH(signature)))));
1161 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1162 PUSH_FIELD_VALUE (minit, "ncode", code);
1163 FINISH_RECORD_CONSTRUCTOR (minit);
1164 return minit;
1167 static tree
1168 get_dispatch_vector (type)
1169 tree type;
1171 tree vtable = TYPE_VTABLE (type);
1172 if (vtable == NULL)
1174 HOST_WIDE_INT i;
1175 tree method;
1176 tree super = CLASSTYPE_SUPER (type);
1177 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1178 vtable = make_tree_vec (nvirtuals);
1179 TYPE_VTABLE (type) = vtable;
1180 if (super != NULL_TREE)
1182 tree super_vtable = get_dispatch_vector (super);
1184 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1185 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1188 for (method = TYPE_METHODS (type); method != NULL_TREE;
1189 method = TREE_CHAIN (method))
1190 if (DECL_VINDEX (method) != NULL_TREE
1191 && host_integerp (DECL_VINDEX (method), 0))
1192 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1193 = method;
1196 return vtable;
1199 static tree
1200 get_dispatch_table (type, this_class_addr)
1201 tree type, this_class_addr;
1203 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1204 tree vtable = get_dispatch_vector (type);
1205 int i;
1206 tree list = NULL_TREE;
1207 int nvirtuals = TREE_VEC_LENGTH (vtable);
1208 for (i = nvirtuals; --i >= 0; )
1210 tree method = TREE_VEC_ELT (vtable, i);
1211 if (METHOD_ABSTRACT (method))
1213 if (! abstract_p)
1214 warning_with_decl (method,
1215 "abstract method in non-abstract class");
1216 method = null_pointer_node;
1218 else
1220 if (!DECL_RTL_SET_P (method))
1221 make_decl_rtl (method, NULL);
1222 method = build1 (ADDR_EXPR, nativecode_ptr_type_node, method);
1224 list = tree_cons (NULL_TREE /*DECL_VINDEX (method) + 2*/,
1225 method, list);
1227 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1228 using the Boehm GC we sometimes stash a GC type descriptor
1229 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1230 the emitted byte count during the output to the assembly file. */
1231 list = tree_cons (NULL_TREE, get_boehm_type_descriptor (type),
1232 list);
1233 list = tree_cons (integer_zero_node, this_class_addr, list);
1234 return build (CONSTRUCTOR, build_prim_array_type (nativecode_ptr_type_node,
1235 nvirtuals + 2),
1236 NULL_TREE, list);
1239 void
1240 make_class_data (type)
1241 tree type;
1243 tree decl, cons, temp;
1244 tree field, fields_decl;
1245 tree static_fields = NULL_TREE;
1246 tree instance_fields = NULL_TREE;
1247 HOST_WIDE_INT static_field_count = 0;
1248 HOST_WIDE_INT instance_field_count = 0;
1249 HOST_WIDE_INT field_count;
1250 tree field_array_type;
1251 tree method;
1252 tree methods = NULL_TREE;
1253 tree dtable_decl = NULL_TREE;
1254 HOST_WIDE_INT method_count = 0;
1255 tree method_array_type;
1256 tree methods_decl;
1257 tree super;
1258 tree this_class_addr;
1259 tree constant_pool_constructor;
1260 tree interfaces = null_pointer_node;
1261 int interface_len = 0;
1262 tree type_decl = TYPE_NAME (type);
1264 this_class_addr = build_class_ref (type);
1265 decl = TREE_OPERAND (this_class_addr, 0);
1267 /* Build Field array. */
1268 field = TYPE_FIELDS (type);
1269 if (DECL_NAME (field) == NULL_TREE)
1270 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1271 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1273 if (! DECL_ARTIFICIAL (field))
1275 tree init = make_field_value (field);
1276 if (FIELD_STATIC (field))
1278 tree initial = DECL_INITIAL (field);
1279 static_field_count++;
1280 static_fields = tree_cons (NULL_TREE, init, static_fields);
1281 /* If the initial value is a string constant,
1282 prevent output_constant from trying to assemble the value. */
1283 if (initial != NULL_TREE
1284 && TREE_TYPE (initial) == string_ptr_type_node)
1285 DECL_INITIAL (field) = NULL_TREE;
1286 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1287 DECL_INITIAL (field) = initial;
1289 else
1291 instance_field_count++;
1292 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1296 field_count = static_field_count + instance_field_count;
1297 if (field_count > 0)
1299 static_fields = nreverse (static_fields);
1300 instance_fields = nreverse (instance_fields);
1301 static_fields = chainon (static_fields, instance_fields);
1302 field_array_type = build_prim_array_type (field_type_node, field_count);
1303 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1304 field_array_type);
1305 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1306 NULL_TREE, static_fields);
1307 TREE_STATIC (fields_decl) = 1;
1308 DECL_ARTIFICIAL (fields_decl) = 1;
1309 DECL_IGNORED_P (fields_decl) = 1;
1310 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1312 else
1313 fields_decl = NULL_TREE;
1315 /* Build Method array. */
1316 for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (type));
1317 method != NULL_TREE; method = TREE_CHAIN (method))
1319 tree init;
1320 if (METHOD_PRIVATE (method)
1321 && ! flag_keep_inline_functions
1322 && (flag_inline_functions || optimize))
1323 continue;
1324 init = make_method_value (method);
1325 method_count++;
1326 methods = tree_cons (NULL_TREE, init, methods);
1328 method_array_type = build_prim_array_type (method_type_node, method_count);
1329 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1330 method_array_type);
1331 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1332 NULL_TREE, nreverse (methods));
1333 TREE_STATIC (methods_decl) = 1;
1334 DECL_ARTIFICIAL (methods_decl) = 1;
1335 DECL_IGNORED_P (methods_decl) = 1;
1336 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1338 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1339 && ! CLASS_INTERFACE (type_decl))
1341 tree dtable = get_dispatch_table (type, this_class_addr);
1342 dtable_decl = build_dtable_decl (type);
1343 DECL_INITIAL (dtable_decl) = dtable;
1344 TREE_STATIC (dtable_decl) = 1;
1345 DECL_ARTIFICIAL (dtable_decl) = 1;
1346 DECL_IGNORED_P (dtable_decl) = 1;
1347 TREE_PUBLIC (dtable_decl) = 1;
1348 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1349 if (type == class_type_node)
1350 class_dtable_decl = dtable_decl;
1353 if (class_dtable_decl == NULL_TREE)
1355 class_dtable_decl = build_dtable_decl (class_type_node);
1356 TREE_STATIC (class_dtable_decl) = 1;
1357 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1358 DECL_IGNORED_P (class_dtable_decl) = 1;
1359 if (is_compiled_class (class_type_node) != 2)
1360 DECL_EXTERNAL (class_dtable_decl) = 1;
1361 rest_of_decl_compilation (class_dtable_decl, (char*) 0, 1, 0);
1364 super = CLASSTYPE_SUPER (type);
1365 if (super == NULL_TREE)
1366 super = null_pointer_node;
1367 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl))))
1368 super = build_class_ref (super);
1369 else
1371 int super_index = alloc_class_constant (super);
1372 super = build_int_2 (super_index, 0);
1373 TREE_TYPE (super) = ptr_type_node;
1376 /* Build and emit the array of implemented interfaces. */
1377 if (type != object_type_node)
1378 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1379 if (interface_len > 0)
1381 tree init = NULL_TREE;
1382 int i;
1383 tree interface_array_type, idecl;
1384 interface_array_type
1385 = build_prim_array_type (class_ptr_type, interface_len);
1386 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1387 interface_array_type);
1388 for (i = interface_len; i > 0; i--)
1390 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1391 tree iclass = BINFO_TYPE (child);
1392 tree index;
1393 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1394 index = build_class_ref (iclass);
1395 else
1397 int int_index = alloc_class_constant (iclass);
1398 index = build_int_2 (int_index, 0);
1399 TREE_TYPE (index) = ptr_type_node;
1401 init = tree_cons (NULL_TREE, index, init);
1403 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1404 NULL_TREE, init);
1405 TREE_STATIC (idecl) = 1;
1406 DECL_ARTIFICIAL (idecl) = 1;
1407 DECL_IGNORED_P (idecl) = 1;
1408 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1409 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1412 constant_pool_constructor = build_constants_constructor ();
1414 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1415 PUSH_FIELD_VALUE (temp, "vtable",
1416 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl));
1417 if (! flag_hash_synchronization)
1418 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1419 FINISH_RECORD_CONSTRUCTOR (temp);
1420 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1421 PUSH_SUPER_VALUE (cons, temp);
1422 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1423 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1424 PUSH_FIELD_VALUE (cons, "accflags",
1425 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1427 PUSH_FIELD_VALUE (cons, "superclass",
1428 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1429 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1430 PUSH_FIELD_VALUE (cons, "methods",
1431 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1432 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1433 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1434 PUSH_FIELD_VALUE (cons, "fields",
1435 fields_decl == NULL_TREE ? null_pointer_node
1436 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1437 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1438 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1439 PUSH_FIELD_VALUE (cons, "static_field_count",
1440 build_int_2 (static_field_count, 0));
1441 PUSH_FIELD_VALUE (cons, "vtable",
1442 dtable_decl == NULL_TREE ? null_pointer_node
1443 : build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl));
1444 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1445 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1446 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1447 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1449 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1450 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1451 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1452 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1453 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1454 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1456 FINISH_RECORD_CONSTRUCTOR (cons);
1458 DECL_INITIAL (decl) = cons;
1459 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1462 void
1463 finish_class ()
1465 tree method;
1466 tree type_methods = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
1467 int saw_native_method = 0;
1469 /* Find out if we have any native methods. We use this information
1470 later. */
1471 for (method = type_methods;
1472 method != NULL_TREE;
1473 method = TREE_CHAIN (method))
1475 if (METHOD_NATIVE (method))
1477 saw_native_method = 1;
1478 break;
1482 /* Emit deferred inline methods. */
1483 for (method = type_methods; method != NULL_TREE; )
1485 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1487 /* It's a deferred inline method. Decide if we need to emit it. */
1488 if (flag_keep_inline_functions
1489 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (method))
1490 || ! METHOD_PRIVATE (method)
1491 || saw_native_method)
1493 output_inline_function (method);
1494 /* Scan the list again to see if there are any earlier
1495 methods to emit. */
1496 method = type_methods;
1497 continue;
1500 method = TREE_CHAIN (method);
1503 current_function_decl = NULL_TREE;
1504 make_class_data (current_class);
1505 register_class ();
1506 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1509 /* Return 2 if CLASS is compiled by this compilation job;
1510 return 1 if CLASS can otherwise be assumed to be compiled;
1511 return 0 if we cannot assume that CLASS is compiled.
1512 Returns 1 for primitive and 0 for array types. */
1514 is_compiled_class (class)
1515 tree class;
1517 int seen_in_zip;
1518 if (TREE_CODE (class) == POINTER_TYPE)
1519 class = TREE_TYPE (class);
1520 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1521 return 1;
1522 if (TYPE_ARRAY_P (class))
1523 return 0;
1524 if (class == current_class)
1525 return 2;
1527 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1528 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1530 /* The class was seen in the current ZIP file and will be
1531 available as a compiled class in the future but may not have
1532 been loaded already. Load it if necessary. This prevent
1533 build_class_ref () from crashing. */
1535 if (seen_in_zip && !CLASS_LOADED_P (class))
1536 load_class (class, 1);
1538 /* We return 2 for class seen in ZIP and class from files
1539 belonging to the same compilation unit */
1540 return 2;
1543 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1545 if (!CLASS_LOADED_P (class))
1547 if (CLASS_FROM_SOURCE_P (class))
1548 safe_layout_class (class);
1549 else
1550 load_class (class, 1);
1552 return 1;
1555 return 0;
1558 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1560 tree
1561 build_dtable_decl (type)
1562 tree type;
1564 tree dtype;
1566 /* We need to build a new dtable type so that its size is uniquely
1567 computed when we're dealing with the class for real and not just
1568 faking it (like java.lang.Class during the initialization of the
1569 compiler.) We now we're not faking a class when CURRENT_CLASS is
1570 TYPE. */
1571 if (current_class == type)
1573 tree dummy = NULL_TREE, aomt, n;
1575 dtype = make_node (RECORD_TYPE);
1576 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1577 n = build_int_2 (TREE_VEC_LENGTH (get_dispatch_vector (type)), 0);
1578 aomt = build_array_type (ptr_type_node, build_index_type (n));
1579 PUSH_FIELD (dtype, dummy, "methods", aomt);
1580 layout_type (dtype);
1582 else
1583 dtype = dtable_type;
1585 return build_decl (VAR_DECL,
1586 java_mangle_vtable (&temporary_obstack, type), dtype);
1589 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1590 fields inherited from SUPER_CLASS. */
1592 void
1593 push_super_field (this_class, super_class)
1594 tree this_class, super_class;
1596 tree base_decl;
1597 /* Don't insert the field if we're just re-laying the class out. */
1598 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1599 return;
1600 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1601 DECL_IGNORED_P (base_decl) = 1;
1602 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1603 TYPE_FIELDS (this_class) = base_decl;
1604 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1605 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1608 /* Handle the different manners we may have to lay out a super class. */
1610 static tree
1611 maybe_layout_super_class (super_class, this_class)
1612 tree super_class;
1613 tree this_class;
1615 if (TREE_CODE (super_class) == RECORD_TYPE)
1617 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1618 safe_layout_class (super_class);
1619 if (!CLASS_LOADED_P (super_class))
1620 load_class (super_class, 1);
1622 /* We might have to layout the class before its dependency on
1623 the super class gets resolved by java_complete_class */
1624 else if (TREE_CODE (super_class) == POINTER_TYPE)
1626 if (TREE_TYPE (super_class) != NULL_TREE)
1627 super_class = TREE_TYPE (super_class);
1628 else
1630 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1631 super_class, NULL_TREE, this_class);
1632 if (!super_class)
1633 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1634 super_class = TREE_TYPE (super_class);
1637 if (!TYPE_SIZE (super_class))
1638 safe_layout_class (super_class);
1640 return super_class;
1643 void
1644 layout_class (this_class)
1645 tree this_class;
1647 tree super_class = CLASSTYPE_SUPER (this_class);
1648 tree field;
1650 class_list = tree_cons (this_class, NULL_TREE, class_list);
1651 if (CLASS_BEING_LAIDOUT (this_class))
1653 char buffer [1024];
1654 char *report;
1655 tree current;
1657 sprintf (buffer, " with `%s'",
1658 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1659 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1661 for (current = TREE_CHAIN (class_list); current;
1662 current = TREE_CHAIN (current))
1664 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1665 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1666 IDENTIFIER_POINTER (DECL_NAME (decl)),
1667 DECL_SOURCE_FILE (decl),
1668 DECL_SOURCE_LINE (decl));
1669 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1671 obstack_1grow (&temporary_obstack, '\0');
1672 report = obstack_finish (&temporary_obstack);
1673 cyclic_inheritance_report = ggc_strdup (report);
1674 obstack_free (&temporary_obstack, report);
1675 TYPE_SIZE (this_class) = error_mark_node;
1676 return;
1678 CLASS_BEING_LAIDOUT (this_class) = 1;
1680 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1682 tree maybe_super_class
1683 = maybe_layout_super_class (super_class, this_class);
1684 if (maybe_super_class == NULL
1685 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1687 TYPE_SIZE (this_class) = error_mark_node;
1688 CLASS_BEING_LAIDOUT (this_class) = 0;
1689 class_list = TREE_CHAIN (class_list);
1690 return;
1692 if (TYPE_SIZE (this_class) == NULL_TREE)
1693 push_super_field (this_class, maybe_super_class);
1696 for (field = TYPE_FIELDS (this_class);
1697 field != NULL_TREE; field = TREE_CHAIN (field))
1699 if (FIELD_STATIC (field))
1701 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1702 SET_DECL_ASSEMBLER_NAME (field,
1703 java_mangle_decl
1704 (&temporary_obstack, field));
1708 layout_type (this_class);
1710 /* Also recursively load/layout any superinterfaces, but only if class was
1711 loaded from bytecode. The source parser will take care of this itself. */
1712 if (!CLASS_FROM_SOURCE_P (this_class))
1714 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1716 if (basetype_vec)
1718 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1719 int i;
1720 for (i = n; i > 0; i--)
1722 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1723 tree super_interface = BINFO_TYPE (vec_elt);
1725 tree maybe_super_interface
1726 = maybe_layout_super_class (super_interface, NULL_TREE);
1727 if (maybe_super_interface == NULL
1728 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1730 TYPE_SIZE (this_class) = error_mark_node;
1731 CLASS_BEING_LAIDOUT (this_class) = 0;
1732 class_list = TREE_CHAIN (class_list);
1733 return;
1739 /* Convert the size back to an SI integer value */
1740 TYPE_SIZE_UNIT (this_class) =
1741 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1743 CLASS_BEING_LAIDOUT (this_class) = 0;
1744 class_list = TREE_CHAIN (class_list);
1747 void
1748 layout_class_methods (this_class)
1749 tree this_class;
1751 tree method_decl, dtable_count;
1752 tree super_class, handle_type;
1754 if (TYPE_NVIRTUALS (this_class))
1755 return;
1757 super_class = CLASSTYPE_SUPER (this_class);
1758 handle_type = CLASS_TO_HANDLE_TYPE (this_class);
1760 if (super_class)
1762 super_class = maybe_layout_super_class (super_class, this_class);
1763 if (!TYPE_NVIRTUALS (super_class))
1764 layout_class_methods (super_class);
1765 dtable_count = TYPE_NVIRTUALS (super_class);
1767 else
1768 dtable_count = integer_zero_node;
1770 TYPE_METHODS (handle_type) = nreverse (TYPE_METHODS (handle_type));
1772 for (method_decl = TYPE_METHODS (handle_type);
1773 method_decl; method_decl = TREE_CHAIN (method_decl))
1774 dtable_count = layout_class_method (this_class, super_class,
1775 method_decl, dtable_count);
1777 TYPE_NVIRTUALS (this_class) = dtable_count;
1779 #ifdef JAVA_USE_HANDLES
1780 layout_type (handle_type);
1781 #endif
1784 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
1785 and 1 if STR is "greater" than NAME. */
1787 /* Lay METHOD_DECL out, returning a possibly new value of
1788 DTABLE_COUNT. Also mangle the method's name. */
1790 tree
1791 layout_class_method (this_class, super_class, method_decl, dtable_count)
1792 tree this_class, super_class, method_decl, dtable_count;
1794 tree method_name = DECL_NAME (method_decl);
1796 TREE_PUBLIC (method_decl) = 1;
1798 /* This is a good occasion to mangle the method's name */
1799 SET_DECL_ASSEMBLER_NAME (method_decl,
1800 java_mangle_decl (&temporary_obstack,
1801 method_decl));
1802 /* We don't generate a RTL for the method if it's abstract, or if
1803 it's an interface method that isn't clinit. */
1804 if (! METHOD_ABSTRACT (method_decl)
1805 || (CLASS_INTERFACE (TYPE_NAME (this_class))
1806 && (DECL_CLINIT_P (method_decl))))
1807 make_decl_rtl (method_decl, NULL);
1809 if (ID_INIT_P (method_name))
1811 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1812 const char *ptr;
1813 for (ptr = p; *ptr; )
1815 if (*ptr++ == '.')
1816 p = ptr;
1818 DECL_CONSTRUCTOR_P (method_decl) = 1;
1819 build_java_argument_signature (TREE_TYPE (method_decl));
1821 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
1823 tree method_sig =
1824 build_java_argument_signature (TREE_TYPE (method_decl));
1825 tree super_method = lookup_argument_method (super_class, method_name,
1826 method_sig);
1827 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
1829 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
1830 if (DECL_VINDEX (method_decl) == NULL_TREE
1831 && !CLASS_FROM_SOURCE_P (this_class))
1832 error_with_decl (method_decl,
1833 "non-static method '%s' overrides static method");
1835 else if (! METHOD_FINAL (method_decl)
1836 && ! METHOD_PRIVATE (method_decl)
1837 && ! CLASS_FINAL (TYPE_NAME (this_class))
1838 && dtable_count)
1840 DECL_VINDEX (method_decl) = dtable_count;
1841 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
1842 dtable_count, integer_one_node));
1846 return dtable_count;
1849 void
1850 register_class ()
1852 /* END does not need to be registered with the garbage collector
1853 because it always points into the list given by REGISTERED_CLASS,
1854 and that variable is registered with the collector. */
1855 static tree end;
1856 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
1857 tree current = copy_node (node);
1859 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
1860 if (!registered_class)
1861 registered_class = current;
1862 else
1863 TREE_CHAIN (end) = current;
1865 end = current;
1868 /* Generate a function that gets called at start-up (static contructor) time,
1869 which calls registerClass for all the compiled classes. */
1871 void
1872 emit_register_classes ()
1874 extern tree get_file_function_name PARAMS ((int));
1875 tree init_name = get_file_function_name ('I');
1876 tree init_type = build_function_type (void_type_node, end_params_node);
1877 tree init_decl;
1878 tree t;
1880 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
1881 SET_DECL_ASSEMBLER_NAME (init_decl, init_name);
1882 TREE_STATIC (init_decl) = 1;
1883 current_function_decl = init_decl;
1884 DECL_RESULT (init_decl) = build_decl(RESULT_DECL, NULL_TREE, void_type_node);
1885 /* DECL_EXTERNAL (init_decl) = 1;*/
1886 TREE_PUBLIC (init_decl) = 1;
1887 pushlevel (0);
1888 make_decl_rtl (init_decl, NULL);
1889 init_function_start (init_decl, input_filename, 0);
1890 expand_function_start (init_decl, 0);
1892 for ( t = registered_class; t; t = TREE_CHAIN (t))
1893 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
1894 XEXP (DECL_RTL (t), 0), Pmode);
1896 expand_function_end (input_filename, 0, 0);
1897 poplevel (1, 0, 1);
1899 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
1900 int saved_flag = flag_inline_functions;
1901 flag_inline_functions = 0;
1902 rest_of_compilation (init_decl);
1903 flag_inline_functions = saved_flag;
1905 current_function_decl = NULL_TREE;
1906 assemble_constructor (IDENTIFIER_POINTER (init_name));
1909 void
1910 init_class_processing ()
1912 registerClass_libfunc = gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterClass");
1913 ggc_add_tree_root (class_roots, sizeof (class_roots) / sizeof (tree));
1914 fields_ident = get_identifier ("fields");
1915 info_ident = get_identifier ("info");
1916 ggc_add_rtx_root (&registerClass_libfunc, 1);
1917 gcc_obstack_init (&temporary_obstack);