2001-02-14 Tom Tromey <tromey@redhat.com>
[official-gcc.git] / gcc / java / class.c
blob615c0754e6065cce6672e99b7327d9e19ff4cae0
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[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
88 #define registered_class class_roots[0]
89 #define fields_ident class_roots[1] /* get_identifier ("fields") */
90 #define info_ident class_roots[2] /* get_identifier ("info") */
91 #define class_list class_roots[3]
93 /* Return the node that most closely represents the class whose name
94 is IDENT. Start the search from NODE. Return NULL if an
95 appropriate node does not exist. */
97 static assume_compiled_node *
98 find_assume_compiled_node (node, ident)
99 assume_compiled_node *node;
100 const char *ident;
102 while (node)
104 size_t node_ident_length = strlen (node->ident);
106 /* node_ident_length is zero at the root of the tree. If the
107 identifiers are the same length, then we have matching
108 classes. Otherwise check if we've matched an enclosing
109 package name. */
111 if (node_ident_length == 0
112 || (strncmp (ident, node->ident, node_ident_length) == 0
113 && (strlen (ident) == node_ident_length
114 || ident[node_ident_length] == '.')))
116 /* We've found a match, however, there might be a more
117 specific match. */
119 assume_compiled_node *found = find_assume_compiled_node (node->child,
120 ident);
121 if (found)
122 return found;
123 else
124 return node;
127 /* No match yet. Continue through the sibling list. */
128 node = node->sibling;
131 /* No match at all in this tree. */
132 return NULL;
135 /* Add a new IDENT to the include/exclude tree. It's an exclusion
136 if EXCLUDEP is non-zero. */
138 void
139 add_assume_compiled (ident, excludep)
140 const char *ident;
141 int excludep;
143 assume_compiled_node *parent;
144 assume_compiled_node *node =
145 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
147 node->ident = xstrdup (ident);
148 node->excludep = excludep;
149 node->child = NULL;
151 /* Create the root of the tree if it doesn't exist yet. */
153 if (NULL == assume_compiled_tree)
155 assume_compiled_tree =
156 (assume_compiled_node *) xmalloc (sizeof (assume_compiled_node));
157 assume_compiled_tree->ident = "";
158 assume_compiled_tree->excludep = 0;
159 assume_compiled_tree->sibling = NULL;
160 assume_compiled_tree->child = NULL;
161 assume_compiled_tree->parent = NULL;
164 /* Calling the function with the empty string means we're setting
165 excludep for the root of the hierarchy. */
167 if (0 == ident[0])
169 assume_compiled_tree->excludep = excludep;
170 return;
173 /* Find the parent node for this new node. PARENT will either be a
174 class or a package name. Adjust PARENT accordingly. */
176 parent = find_assume_compiled_node (assume_compiled_tree, ident);
177 if (ident[strlen (parent->ident)] != '.')
178 parent = parent->parent;
180 /* Insert NODE into the tree. */
182 node->parent = parent;
183 node->sibling = parent->child;
184 parent->child = node;
187 /* Returns non-zero if IDENT is the name of a class that the compiler
188 should assume has been compiled to FIXME */
190 static int
191 assume_compiled (ident)
192 const char *ident;
194 assume_compiled_node *i;
195 int result;
197 if (NULL == assume_compiled_tree)
198 return 1;
200 i = find_assume_compiled_node (assume_compiled_tree,
201 ident);
203 result = ! i->excludep;
205 return (result);
208 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
209 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
210 Also, PREFIX is prepended, and SUFFIX is appended. */
212 tree
213 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
214 const char* old_name;
215 int old_length;
216 const char *prefix;
217 int old_char;
218 int new_char;
219 const char *suffix;
221 int prefix_len = strlen (prefix);
222 int suffix_len = strlen (suffix);
223 int i = prefix_len + old_length + suffix_len + 1;
224 #ifdef __GNUC__
225 char buffer[i];
226 #else
227 char *buffer = (char *)alloca (i);
228 #endif
229 strcpy (buffer, prefix);
230 for (i = 0; i < old_length; i++)
232 char ch = old_name[i];
233 if (ch == old_char)
234 ch = new_char;
235 buffer[prefix_len + i] = ch;
237 strcpy (buffer + prefix_len + old_length, suffix);
238 return get_identifier (buffer);
241 /* Return an IDENTIFIER_NODE the same as OLD_ID,
242 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
243 Also, PREFIX is prepended, and SUFFIX is appended. */
245 tree
246 identifier_subst (old_id, prefix, old_char, new_char, suffix)
247 const tree old_id;
248 const char *prefix;
249 int old_char;
250 int new_char;
251 const char *suffix;
253 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
254 prefix, old_char, new_char, suffix);
257 /* Generate a valid C identifier from the name of the class TYPE,
258 prefixed by PREFIX. */
260 tree
261 mangled_classname (prefix, type)
262 const char *prefix;
263 tree type;
265 tree ident = TYPE_NAME (type);
266 if (TREE_CODE (ident) != IDENTIFIER_NODE)
267 ident = DECL_NAME (ident);
268 return identifier_subst (ident, prefix, '.', '_', "");
271 tree
272 make_class ()
274 tree type;
275 type = make_node (RECORD_TYPE);
276 #ifdef JAVA_USE_HANDLES
277 tree field1 = build_decl (FIELD_DECL, get_identifier ("obj"),
278 build_pointer_type (type));
279 tree field2 = build_decl (FIELD_DECL, get_identifier ("methods"),
280 methodtable_ptr_type);
281 tree handle_type = make_node (RECORD_TYPE);
282 TREE_CHAIN (field1) = field2;
283 TYPE_FIELDS (handle_type) = field1;
284 TYPE_BINFO (type) = make_tree_vec (7);
285 TYPE_BINFO (handle_type) = make_tree_vec (7);
286 BINFO_HANDLE (TYPE_BINFO (handle_type)) = type;
287 BINFO_HANDLE (TYPE_BINFO (type)) = handle_type;
288 #else
289 TYPE_BINFO (type) = make_tree_vec (6);
290 #endif
291 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
293 return type;
296 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
297 and where each of the constituents is separated by '/',
298 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
300 tree
301 unmangle_classname (name, name_length)
302 const char *name; int name_length;
304 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
305 /* It's not sufficient to compare to_return and get_identifier
306 (name) to determine whether to_return is qualified. There are
307 cases in signature analysis where name will be stripped of a
308 trailing ';'. */
309 name = IDENTIFIER_POINTER (to_return);
310 while (*name)
311 if (*name++ == '.')
313 QUALIFIED_P (to_return) = 1;
314 break;
317 return to_return;
320 tree
321 push_class (class_type, class_name)
322 tree class_type, class_name;
324 tree decl, signature;
325 const char *save_input_filename = input_filename;
326 int save_lineno = lineno;
327 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
328 CLASS_P (class_type) = 1;
329 input_filename = IDENTIFIER_POINTER (source_name);
330 lineno = 0;
331 decl = build_decl (TYPE_DECL, class_name, class_type);
332 input_filename = save_input_filename;
333 lineno = save_lineno;
334 signature = identifier_subst (class_name, "L", '.', '/', ";");
335 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
337 /* Setting DECL_ARTIFICAL forces dbxout.c to specific the type is
338 both a typedef and in the struct name-space. We may want to re-visit
339 this later, but for now it reduces the changes needed for gdb. */
340 DECL_ARTIFICIAL (decl) = 1;
342 pushdecl_top_level (decl);
343 #ifdef JAVA_USE_HANDLES
345 tree handle_name = identifier_subst (class_name,
346 "Handle$", '.', '.', "");
347 tree handle_decl = build_decl (TYPE_DECL, handle_name,
348 CLASS_TO_HANDLE_TYPE (class_type));
349 pushdecl (handle_decl);
351 #endif
353 return decl;
356 /* Finds the (global) class named NAME. Creates the class if not found.
357 Also creates associated TYPE_DECL.
358 Does not check if the class actually exists, load the class,
359 fill in field or methods, or do layout_type. */
361 tree
362 lookup_class (name)
363 tree name;
365 tree decl = IDENTIFIER_CLASS_VALUE (name);
366 if (decl == NULL_TREE)
367 decl = push_class (make_class (), name);
368 return TREE_TYPE (decl);
371 void
372 set_super_info (access_flags, this_class, super_class, interfaces_count)
373 int access_flags;
374 tree this_class;
375 tree super_class;
376 int interfaces_count;
378 int total_supers = interfaces_count;
379 tree class_decl = TYPE_NAME (this_class);
380 if (super_class)
381 total_supers++;
383 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
384 if (super_class)
386 tree super_binfo = make_tree_vec (6);
387 BINFO_TYPE (super_binfo) = super_class;
388 BINFO_OFFSET (super_binfo) = integer_zero_node;
389 TREE_VIA_PUBLIC (super_binfo) = 1;
390 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
391 = super_binfo;
392 CLASS_HAS_SUPER (this_class) = 1;
395 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
396 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
397 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
398 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
399 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
400 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
401 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
402 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
405 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
406 direct sub-classes of Object are 1, and so on. */
409 class_depth (clas)
410 tree clas;
412 int depth = 0;
413 if (! CLASS_LOADED_P (clas))
414 load_class (clas, 1);
415 if (TYPE_SIZE (clas) == error_mark_node)
416 return -1;
417 while (clas != object_type_node)
419 depth++;
420 clas = TYPE_BINFO_BASETYPE (clas, 0);
422 return depth;
425 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
428 interface_of_p (type1, type2)
429 tree type1, type2;
431 int n, i;
432 tree basetype_vec;
434 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
435 return 0;
436 n = TREE_VEC_LENGTH (basetype_vec);
437 for (i = 0; i < n; i++)
439 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
440 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
441 return 1;
443 for (i = 0; i < n; i++)
445 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
446 if (vec_elt && BINFO_TYPE (vec_elt)
447 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
448 return 1;
450 return 0;
453 /* Return true iff TYPE1 inherits from TYPE2. */
456 inherits_from_p (type1, type2)
457 tree type1, type2;
459 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
461 if (type1 == type2)
462 return 1;
463 type1 = CLASSTYPE_SUPER (type1);
465 return 0;
468 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
471 enclosing_context_p (type1, type2)
472 tree type1, type2;
474 if (!INNER_CLASS_TYPE_P (type2))
475 return 0;
477 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
478 type2;
479 type2 = (INNER_CLASS_TYPE_P (type2) ?
480 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
482 if (type2 == type1)
483 return 1;
486 return 0;
489 /* Return 1 iff there exists a common enclosing context between TYPE1
490 and TYPE2. */
492 int common_enclosing_context_p (type1, type2)
493 tree type1, type2;
495 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
496 return 0;
498 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
499 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
500 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
502 tree current;
503 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
504 current = (PURE_INNER_CLASS_TYPE_P (current) ?
505 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
506 NULL_TREE))
507 if (type1 == current)
508 return 1;
510 return 0;
513 static void
514 add_interface_do (basetype_vec, interface_class, i)
515 tree basetype_vec, interface_class;
516 int i;
518 tree interface_binfo = make_tree_vec (6);
519 BINFO_TYPE (interface_binfo) = interface_class;
520 BINFO_OFFSET (interface_binfo) = integer_zero_node;
521 TREE_VIA_VIRTUAL (interface_binfo) = 1;
522 TREE_VIA_PUBLIC (interface_binfo) = 1;
523 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
526 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
527 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
528 if attempt is made to add it twice. */
530 tree
531 maybe_add_interface (this_class, interface_class)
532 tree this_class, interface_class;
534 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
535 int i;
536 int n = TREE_VEC_LENGTH (basetype_vec);
537 for (i = 0; ; i++)
539 if (i >= n)
541 error ("internal error - too many interface type");
542 return NULL_TREE;
544 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
545 break;
546 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
547 return interface_class;
549 add_interface_do (basetype_vec, interface_class, i);
550 return NULL_TREE;
553 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
555 void
556 add_interface (this_class, interface_class)
557 tree this_class, interface_class;
559 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
560 int i;
561 int n = TREE_VEC_LENGTH (basetype_vec);
562 for (i = 0; ; i++)
564 if (i >= n)
566 error ("internal error - too many interface type");
567 return;
569 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
570 break;
572 add_interface_do (basetype_vec, interface_class, i);
575 #if 0
576 /* Return the address of a pointer to the first FUNCTION_DECL
577 in the list (*LIST) whose DECL_NAME is NAME. */
579 static tree *
580 find_named_method (list, name)
581 tree *list;
582 tree name;
584 while (*list && DECL_NAME (*list) != name)
585 list = &TREE_CHAIN (*list);
586 return list;
588 #endif
590 static tree
591 build_java_method_type (fntype, this_class, access_flags)
592 tree fntype;
593 tree this_class;
594 int access_flags;
596 if (access_flags & ACC_STATIC)
597 return fntype;
598 return build_method_type (CLASS_TO_HANDLE_TYPE (this_class), fntype);
601 static struct hash_entry *
602 init_test_hash_newfunc (entry, table, string)
603 struct hash_entry *entry;
604 struct hash_table *table;
605 hash_table_key string ATTRIBUTE_UNUSED;
607 struct init_test_hash_entry *ret = (struct init_test_hash_entry *) entry;
608 if (ret == NULL)
610 ret = ((struct init_test_hash_entry *)
611 hash_allocate (table, sizeof (struct init_test_hash_entry)));
612 if (ret == NULL)
613 return NULL;
615 ret->init_test_decl = 0;
616 return (struct hash_entry *) ret;
619 /* Hash table helpers. Also reused in find_applicable_accessible_methods_list
620 (parse.y). The hash of a tree node is it's pointer value,
621 comparison is direct. */
623 unsigned long
624 java_hash_hash_tree_node (k)
625 hash_table_key k;
627 return (long) k;
630 boolean
631 java_hash_compare_tree_node (k1, k2)
632 hash_table_key k1;
633 hash_table_key k2;
635 return ((char*) k1 == (char*) k2);
638 tree
639 add_method_1 (handle_class, access_flags, name, function_type)
640 tree handle_class;
641 int access_flags;
642 tree name;
643 tree function_type;
645 tree method_type, fndecl;
647 method_type = build_java_method_type (function_type,
648 handle_class, access_flags);
650 fndecl = build_decl (FUNCTION_DECL, name, method_type);
651 DECL_CONTEXT (fndecl) = handle_class;
653 DECL_LANG_SPECIFIC (fndecl)
654 = (struct lang_decl *) ggc_alloc_cleared (sizeof (struct lang_decl));
656 /* Initialize the static initializer test table. */
657 hash_table_init (&DECL_FUNCTION_INIT_TEST_TABLE (fndecl),
658 init_test_hash_newfunc, java_hash_hash_tree_node,
659 java_hash_compare_tree_node);
661 TREE_CHAIN (fndecl) = TYPE_METHODS (handle_class);
662 TYPE_METHODS (handle_class) = fndecl;
664 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
665 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
666 if (access_flags & ACC_PRIVATE)
667 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
668 if (access_flags & ACC_NATIVE)
670 METHOD_NATIVE (fndecl) = 1;
671 DECL_EXTERNAL (fndecl) = 1;
673 if (access_flags & ACC_STATIC)
674 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
675 if (access_flags & ACC_FINAL)
676 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
677 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
678 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
679 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
680 return fndecl;
683 /* Add a method to THIS_CLASS.
684 The method's name is NAME.
685 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
687 tree
688 add_method (this_class, access_flags, name, method_sig)
689 tree this_class;
690 int access_flags;
691 tree name;
692 tree method_sig;
694 tree handle_class = CLASS_TO_HANDLE_TYPE (this_class);
695 tree function_type, fndecl;
696 const unsigned char *sig
697 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
699 if (sig[0] != '(')
700 fatal_error ("bad method signature");
702 function_type = get_type_from_signature (method_sig);
703 fndecl = add_method_1 (handle_class, access_flags, name, function_type);
704 set_java_signature (TREE_TYPE (fndecl), method_sig);
705 return fndecl;
708 tree
709 add_field (class, name, field_type, flags)
710 tree class;
711 tree name;
712 tree field_type;
713 int flags;
715 int is_static = (flags & ACC_STATIC) != 0;
716 tree field;
717 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
718 TREE_CHAIN (field) = TYPE_FIELDS (class);
719 TYPE_FIELDS (class) = field;
720 DECL_CONTEXT (field) = class;
722 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
723 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
724 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
725 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
726 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
727 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
728 if (is_static)
730 FIELD_STATIC (field) = 1;
731 /* Always make field externally visible. This is required so
732 that native methods can always access the field. */
733 TREE_PUBLIC (field) = 1;
735 return field;
738 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
740 void
741 set_constant_value (field, constant)
742 tree field, constant;
744 if (field == NULL_TREE)
745 warning ("misplaced ConstantValue attribute (not in any field)");
746 else if (DECL_INITIAL (field) != NULL_TREE)
747 warning ("duplicate ConstanValue atribute for field '%s'",
748 IDENTIFIER_POINTER (DECL_NAME (field)));
749 else
751 DECL_INITIAL (field) = constant;
752 if (FIELD_FINAL (field))
753 DECL_FIELD_FINAL_IUD (field) = 1;
757 /* Count the number of Unicode chars encoded in a given Ut8 string. */
759 #if 0
761 strLengthUtf8 (str, len)
762 char *str;
763 int len;
765 register unsigned char* ptr = (unsigned char*) str;
766 register unsigned char *limit = ptr + len;
767 int str_length = 0;
768 for (; ptr < limit; str_length++) {
769 if (UTF8_GET (ptr, limit) < 0)
770 return -1;
772 return str_length;
774 #endif
777 /* Calculate a hash value for a string encoded in Utf8 format.
778 * This returns the same hash value as specified for java.lang.String.hashCode.
781 static int32
782 hashUtf8String (str, len)
783 const char *str;
784 int len;
786 register const unsigned char* ptr = (const unsigned char*) str;
787 register const unsigned char *limit = ptr + len;
788 int32 hash = 0;
789 for (; ptr < limit;)
791 int ch = UTF8_GET (ptr, limit);
792 /* Updated specification from
793 http://www.javasoft.com/docs/books/jls/clarify.html. */
794 hash = (31 * hash) + ch;
796 return hash;
799 tree utf8_decl_list = NULL_TREE;
801 tree
802 build_utf8_ref (name)
803 tree name;
805 const char * name_ptr = IDENTIFIER_POINTER(name);
806 int name_len = IDENTIFIER_LENGTH(name);
807 char buf[60];
808 char *buf_ptr;
809 tree ctype, field = NULL_TREE, str_type, cinit, string;
810 static int utf8_count = 0;
811 int name_hash;
812 tree ref = IDENTIFIER_UTF8_REF (name);
813 tree decl;
814 if (ref != NULL_TREE)
815 return ref;
817 ctype = make_node (RECORD_TYPE);
818 str_type = build_prim_array_type (unsigned_byte_type_node,
819 name_len + 1); /* Allow for final '\0'. */
820 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
821 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
822 PUSH_FIELD (ctype, field, "data", str_type);
823 FINISH_RECORD (ctype);
824 START_RECORD_CONSTRUCTOR (cinit, ctype);
825 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
826 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
827 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
828 string = build_string (name_len, name_ptr);
829 TREE_TYPE (string) = str_type;
830 PUSH_FIELD_VALUE (cinit, "data", string);
831 FINISH_RECORD_CONSTRUCTOR (cinit);
832 TREE_CONSTANT (cinit) = 1;
834 /* Build a unique identifier based on buf. */
835 sprintf(buf, "_Utf%d", ++utf8_count);
836 buf_ptr = &buf[strlen (buf)];
837 if (name_len > 0 && name_ptr[0] >= '0' && name_ptr[0] <= '9')
838 *buf_ptr++ = '_';
839 while (--name_len >= 0)
841 unsigned char c = *name_ptr++;
842 if (c & 0x80)
843 continue;
844 if (!ISALPHA(c) && !ISDIGIT(c))
845 c = '_';
846 *buf_ptr++ = c;
847 if (buf_ptr >= buf + 50)
848 break;
850 *buf_ptr = '\0';
852 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
853 /* FIXME get some way to force this into .text, not .data. */
854 TREE_STATIC (decl) = 1;
855 DECL_ARTIFICIAL (decl) = 1;
856 DECL_IGNORED_P (decl) = 1;
857 TREE_READONLY (decl) = 1;
858 TREE_THIS_VOLATILE (decl) = 0;
859 DECL_INITIAL (decl) = cinit;
860 TREE_CHAIN (decl) = utf8_decl_list;
861 layout_decl (decl, 0);
862 pushdecl (decl);
863 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
864 utf8_decl_list = decl;
865 make_decl_rtl (decl, (char*) 0);
866 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
867 IDENTIFIER_UTF8_REF (name) = ref;
868 return ref;
871 /* Build a reference to the class TYPE.
872 Also handles primitive types and array types. */
874 tree
875 build_class_ref (type)
876 tree type;
878 int is_compiled = is_compiled_class (type);
879 if (is_compiled)
881 tree ref, decl_name, decl;
882 if (TREE_CODE (type) == POINTER_TYPE)
883 type = TREE_TYPE (type);
884 if (TREE_CODE (type) == RECORD_TYPE)
886 if (TYPE_SIZE (type) == error_mark_node)
887 return null_pointer_node;
888 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
889 "", '/', '/', ".class");
890 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
891 if (decl == NULL_TREE)
893 decl = build_decl (VAR_DECL, decl_name, class_type_node);
894 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
895 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
896 TREE_STATIC (decl) = 1;
897 TREE_PUBLIC (decl) = 1;
898 DECL_IGNORED_P (decl) = 1;
899 DECL_ARTIFICIAL (decl) = 1;
900 DECL_ASSEMBLER_NAME (decl) =
901 java_mangle_class_field (&temporary_obstack, type);
902 make_decl_rtl (decl, NULL);
903 pushdecl_top_level (decl);
904 if (is_compiled == 1)
905 DECL_EXTERNAL (decl) = 1;
908 else
910 const char *name;
911 char buffer[25];
912 if (flag_emit_class_files)
914 const char *prim_class_name;
915 tree prim_class;
916 if (type == char_type_node)
917 prim_class_name = "java.lang.Character";
918 else if (type == boolean_type_node)
919 prim_class_name = "java.lang.Boolean";
920 else if (type == byte_type_node)
921 prim_class_name = "java.lang.Byte";
922 else if (type == short_type_node)
923 prim_class_name = "java.lang.Short";
924 else if (type == int_type_node)
925 prim_class_name = "java.lang.Integer";
926 else if (type == long_type_node)
927 prim_class_name = "java.lang.Long";
928 else if (type == float_type_node)
929 prim_class_name = "java.lang.Float";
930 else if (type == double_type_node)
931 prim_class_name = "java.lang.Double";
932 else if (type == void_type_node)
933 prim_class_name = "java.lang.Void";
934 else
935 abort ();
937 prim_class = lookup_class (get_identifier (prim_class_name));
938 return build (COMPONENT_REF, NULL_TREE,
939 prim_class, TYPE_identifier_node);
941 decl_name = TYPE_NAME (type);
942 if (TREE_CODE (decl_name) == TYPE_DECL)
943 decl_name = DECL_NAME (decl_name);
944 name = IDENTIFIER_POINTER (decl_name);
945 if (strncmp (name, "promoted_", 9) == 0)
946 name += 9;
947 sprintf (buffer, "_Jv_%sClass", name);
948 decl_name = get_identifier (buffer);
949 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
950 if (decl == NULL_TREE)
952 decl = build_decl (VAR_DECL, decl_name, class_type_node);
953 TREE_STATIC (decl) = 1;
954 TREE_PUBLIC (decl) = 1;
955 make_decl_rtl (decl, NULL);
956 pushdecl_top_level (decl);
957 if (is_compiled == 1)
958 DECL_EXTERNAL (decl) = 1;
962 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
963 return ref;
965 else
967 int index;
968 tree cl;
969 index = alloc_class_constant (type);
970 cl = build_ref_from_constant_pool (index);
971 TREE_TYPE (cl) = promote_type (class_ptr_type);
972 return cl;
976 tree
977 build_static_field_ref (fdecl)
978 tree fdecl;
980 tree fclass = DECL_CONTEXT (fdecl);
981 int is_compiled = is_compiled_class (fclass);
982 if (is_compiled)
984 if (DECL_RTL (fdecl) == 0)
986 make_decl_rtl (fdecl, NULL);
987 if (is_compiled == 1)
988 DECL_EXTERNAL (fdecl) = 1;
990 return fdecl;
992 else
994 /* Compile as:
995 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
996 tree ref = build_class_ref (fclass);
997 tree fld;
998 int field_index = 0;
999 ref = build1 (INDIRECT_REF, class_type_node, ref);
1000 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
1001 lookup_field (&class_type_node, fields_ident));
1003 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1005 if (fld == fdecl)
1006 break;
1007 if (fld == NULL_TREE)
1008 fatal_error ("field '%s' not found in class",
1009 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1010 if (FIELD_STATIC (fld))
1011 field_index++;
1013 field_index *= int_size_in_bytes (field_type_node);
1014 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
1015 ref, build_int_2 (field_index, 0)));
1016 ref = build1 (INDIRECT_REF, field_type_node, ref);
1017 ref = build (COMPONENT_REF, field_info_union_node,
1018 ref, lookup_field (&field_type_node, info_ident));
1019 ref = build (COMPONENT_REF, ptr_type_node,
1020 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
1021 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1026 get_access_flags_from_decl (decl)
1027 tree decl;
1029 int access_flags = 0;
1030 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1032 if (FIELD_STATIC (decl))
1033 access_flags |= ACC_STATIC;
1034 if (FIELD_PUBLIC (decl))
1035 access_flags |= ACC_PUBLIC;
1036 if (FIELD_PROTECTED (decl))
1037 access_flags |= ACC_PROTECTED;
1038 if (FIELD_PRIVATE (decl))
1039 access_flags |= ACC_PRIVATE;
1040 if (FIELD_FINAL (decl))
1041 access_flags |= ACC_FINAL;
1042 if (FIELD_VOLATILE (decl))
1043 access_flags |= ACC_VOLATILE;
1044 if (FIELD_TRANSIENT (decl))
1045 access_flags |= ACC_TRANSIENT;
1046 return access_flags;
1048 if (TREE_CODE (decl) == TYPE_DECL)
1050 if (CLASS_PUBLIC (decl))
1051 access_flags |= ACC_PUBLIC;
1052 if (CLASS_FINAL (decl))
1053 access_flags |= ACC_FINAL;
1054 if (CLASS_SUPER (decl))
1055 access_flags |= ACC_SUPER;
1056 if (CLASS_INTERFACE (decl))
1057 access_flags |= ACC_INTERFACE;
1058 if (CLASS_ABSTRACT (decl))
1059 access_flags |= ACC_ABSTRACT;
1060 if (CLASS_STATIC (decl))
1061 access_flags |= ACC_STATIC;
1062 if (CLASS_PRIVATE (decl))
1063 access_flags |= ACC_PRIVATE;
1064 if (CLASS_PROTECTED (decl))
1065 access_flags |= ACC_PROTECTED;
1066 return access_flags;
1068 if (TREE_CODE (decl) == FUNCTION_DECL)
1070 if (METHOD_PUBLIC (decl))
1071 access_flags |= ACC_PUBLIC;
1072 if (METHOD_PRIVATE (decl))
1073 access_flags |= ACC_PRIVATE;
1074 if (METHOD_PROTECTED (decl))
1075 access_flags |= ACC_PROTECTED;
1076 if (METHOD_STATIC (decl))
1077 access_flags |= ACC_STATIC;
1078 if (METHOD_FINAL (decl))
1079 access_flags |= ACC_FINAL;
1080 if (METHOD_SYNCHRONIZED (decl))
1081 access_flags |= ACC_SYNCHRONIZED;
1082 if (METHOD_NATIVE (decl))
1083 access_flags |= ACC_NATIVE;
1084 if (METHOD_ABSTRACT (decl))
1085 access_flags |= ACC_ABSTRACT;
1086 if (METHOD_TRANSIENT (decl))
1087 access_flags |= ACC_TRANSIENT;
1088 return access_flags;
1090 abort ();
1093 static tree
1094 make_field_value (fdecl)
1095 tree fdecl;
1097 tree finit;
1098 int flags;
1099 tree type = TREE_TYPE (fdecl);
1100 int resolved = is_compiled_class (type);
1102 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1103 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1104 if (resolved)
1105 type = build_class_ref (type);
1106 else
1108 tree signature = build_java_signature (type);
1110 type = build_utf8_ref (unmangle_classname
1111 (IDENTIFIER_POINTER (signature),
1112 IDENTIFIER_LENGTH (signature)));
1114 PUSH_FIELD_VALUE (finit, "type", type);
1116 flags = get_access_flags_from_decl (fdecl);
1117 if (! resolved)
1118 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1120 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
1121 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1123 PUSH_FIELD_VALUE
1124 (finit, "info",
1125 build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
1126 build_tree_list
1127 ((FIELD_STATIC (fdecl)
1128 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1129 : TYPE_FIELDS (field_info_union_node)),
1130 (FIELD_STATIC (fdecl)
1131 ? build_address_of (build_static_field_ref (fdecl))
1132 : byte_position (fdecl)))));
1134 FINISH_RECORD_CONSTRUCTOR (finit);
1135 return finit;
1138 static tree
1139 make_method_value (mdecl)
1140 tree mdecl;
1142 tree minit;
1143 tree code;
1144 #define ACC_TRANSLATED 0x4000
1145 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1146 code = null_pointer_node;
1147 if (DECL_RTL (mdecl))
1148 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
1149 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1150 PUSH_FIELD_VALUE (minit, "name",
1151 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1152 init_identifier_node
1153 : DECL_NAME (mdecl)));
1155 tree signature = build_java_signature (TREE_TYPE (mdecl));
1156 PUSH_FIELD_VALUE (minit, "signature",
1157 (build_utf8_ref
1158 (unmangle_classname
1159 (IDENTIFIER_POINTER(signature),
1160 IDENTIFIER_LENGTH(signature)))));
1162 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
1163 PUSH_FIELD_VALUE (minit, "ncode", code);
1164 FINISH_RECORD_CONSTRUCTOR (minit);
1165 return minit;
1168 static tree
1169 get_dispatch_vector (type)
1170 tree type;
1172 tree vtable = TYPE_VTABLE (type);
1173 if (vtable == NULL)
1175 HOST_WIDE_INT i;
1176 tree method;
1177 tree super = CLASSTYPE_SUPER (type);
1178 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1179 vtable = make_tree_vec (nvirtuals);
1180 TYPE_VTABLE (type) = vtable;
1181 if (super != NULL_TREE)
1183 tree super_vtable = get_dispatch_vector (super);
1185 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1186 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1189 for (method = TYPE_METHODS (type); method != NULL_TREE;
1190 method = TREE_CHAIN (method))
1191 if (DECL_VINDEX (method) != NULL_TREE
1192 && host_integerp (DECL_VINDEX (method), 0))
1193 TREE_VEC_ELT (vtable, tree_low_cst (DECL_VINDEX (method), 0))
1194 = method;
1197 return vtable;
1200 static tree
1201 get_dispatch_table (type, this_class_addr)
1202 tree type, this_class_addr;
1204 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1205 tree vtable = get_dispatch_vector (type);
1206 int i;
1207 tree list = NULL_TREE;
1208 int nvirtuals = TREE_VEC_LENGTH (vtable);
1209 for (i = nvirtuals; --i >= 0; )
1211 tree method = TREE_VEC_ELT (vtable, i);
1212 if (METHOD_ABSTRACT (method))
1214 if (! abstract_p)
1215 warning_with_decl (method,
1216 "abstract method in non-abstract class");
1217 method = null_pointer_node;
1219 else
1221 if (DECL_RTL (method) == 0)
1222 make_decl_rtl (method, NULL);
1223 method = build1 (ADDR_EXPR, nativecode_ptr_type_node, method);
1225 list = tree_cons (NULL_TREE /*DECL_VINDEX (method) + 2*/,
1226 method, list);
1228 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1229 using the Boehm GC we sometimes stash a GC type descriptor
1230 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1231 the emitted byte count during the output to the assembly file. */
1232 list = tree_cons (NULL_TREE, get_boehm_type_descriptor (type),
1233 list);
1234 list = tree_cons (integer_zero_node, this_class_addr, list);
1235 return build (CONSTRUCTOR, build_prim_array_type (nativecode_ptr_type_node,
1236 nvirtuals + 2),
1237 NULL_TREE, list);
1240 void
1241 make_class_data (type)
1242 tree type;
1244 tree decl, cons, temp;
1245 tree field, fields_decl;
1246 tree static_fields = NULL_TREE;
1247 tree instance_fields = NULL_TREE;
1248 HOST_WIDE_INT static_field_count = 0;
1249 HOST_WIDE_INT instance_field_count = 0;
1250 HOST_WIDE_INT field_count;
1251 tree field_array_type;
1252 tree method;
1253 tree methods = NULL_TREE;
1254 tree dtable_decl = NULL_TREE;
1255 HOST_WIDE_INT method_count = 0;
1256 tree method_array_type;
1257 tree methods_decl;
1258 tree super;
1259 tree this_class_addr;
1260 tree constant_pool_constructor;
1261 tree interfaces = null_pointer_node;
1262 int interface_len = 0;
1263 tree type_decl = TYPE_NAME (type);
1265 this_class_addr = build_class_ref (type);
1266 decl = TREE_OPERAND (this_class_addr, 0);
1268 /* Build Field array. */
1269 field = TYPE_FIELDS (type);
1270 if (DECL_NAME (field) == NULL_TREE)
1271 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1272 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1274 if (! DECL_ARTIFICIAL (field))
1276 tree init = make_field_value (field);
1277 if (FIELD_STATIC (field))
1279 tree initial = DECL_INITIAL (field);
1280 static_field_count++;
1281 static_fields = tree_cons (NULL_TREE, init, static_fields);
1282 /* If the initial value is a string constant,
1283 prevent output_constant from trying to assemble the value. */
1284 if (initial != NULL_TREE
1285 && TREE_TYPE (initial) == string_ptr_type_node)
1286 DECL_INITIAL (field) = NULL_TREE;
1287 rest_of_decl_compilation (field, (char*) 0, 1, 1);
1288 DECL_INITIAL (field) = initial;
1290 else
1292 instance_field_count++;
1293 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1297 field_count = static_field_count + instance_field_count;
1298 if (field_count > 0)
1300 static_fields = nreverse (static_fields);
1301 instance_fields = nreverse (instance_fields);
1302 static_fields = chainon (static_fields, instance_fields);
1303 field_array_type = build_prim_array_type (field_type_node, field_count);
1304 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1305 field_array_type);
1306 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
1307 NULL_TREE, static_fields);
1308 TREE_STATIC (fields_decl) = 1;
1309 DECL_ARTIFICIAL (fields_decl) = 1;
1310 DECL_IGNORED_P (fields_decl) = 1;
1311 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
1313 else
1314 fields_decl = NULL_TREE;
1316 /* Build Method array. */
1317 for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (type));
1318 method != NULL_TREE; method = TREE_CHAIN (method))
1320 tree init;
1321 if (METHOD_PRIVATE (method)
1322 && ! flag_keep_inline_functions
1323 && (flag_inline_functions || optimize))
1324 continue;
1325 init = make_method_value (method);
1326 method_count++;
1327 methods = tree_cons (NULL_TREE, init, methods);
1329 method_array_type = build_prim_array_type (method_type_node, method_count);
1330 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1331 method_array_type);
1332 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1333 NULL_TREE, nreverse (methods));
1334 TREE_STATIC (methods_decl) = 1;
1335 DECL_ARTIFICIAL (methods_decl) = 1;
1336 DECL_IGNORED_P (methods_decl) = 1;
1337 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1339 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1340 && ! CLASS_INTERFACE (type_decl))
1342 tree dtable = get_dispatch_table (type, this_class_addr);
1343 dtable_decl = build_dtable_decl (type);
1344 DECL_INITIAL (dtable_decl) = dtable;
1345 TREE_STATIC (dtable_decl) = 1;
1346 DECL_ARTIFICIAL (dtable_decl) = 1;
1347 DECL_IGNORED_P (dtable_decl) = 1;
1348 TREE_PUBLIC (dtable_decl) = 1;
1349 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1352 super = CLASSTYPE_SUPER (type);
1353 if (super == NULL_TREE)
1354 super = null_pointer_node;
1355 else if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl))))
1356 super = build_class_ref (super);
1357 else
1359 int super_index = alloc_class_constant (super);
1360 super = build_int_2 (super_index, 0);
1361 TREE_TYPE (super) = ptr_type_node;
1364 /* Build and emit the array of implemented interfaces. */
1365 if (type != object_type_node)
1366 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1367 if (interface_len > 0)
1369 tree init = NULL_TREE;
1370 int i;
1371 tree interface_array_type, idecl;
1372 interface_array_type
1373 = build_prim_array_type (class_ptr_type, interface_len);
1374 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1375 interface_array_type);
1376 for (i = interface_len; i > 0; i--)
1378 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1379 tree iclass = BINFO_TYPE (child);
1380 tree index;
1381 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass)))))
1382 index = build_class_ref (iclass);
1383 else
1385 int int_index = alloc_class_constant (iclass);
1386 index = build_int_2 (int_index, 0);
1387 TREE_TYPE (index) = ptr_type_node;
1389 init = tree_cons (NULL_TREE, index, init);
1391 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1392 NULL_TREE, init);
1393 TREE_STATIC (idecl) = 1;
1394 DECL_ARTIFICIAL (idecl) = 1;
1395 DECL_IGNORED_P (idecl) = 1;
1396 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1397 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1400 constant_pool_constructor = build_constants_constructor ();
1402 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1403 PUSH_FIELD_VALUE (temp, "vtable",
1404 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl));
1405 if (! flag_hash_synchronization)
1406 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1407 FINISH_RECORD_CONSTRUCTOR (temp);
1408 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1409 PUSH_SUPER_VALUE (cons, temp);
1410 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1411 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1412 PUSH_FIELD_VALUE (cons, "accflags",
1413 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1415 PUSH_FIELD_VALUE (cons, "superclass",
1416 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1417 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1418 PUSH_FIELD_VALUE (cons, "methods",
1419 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1420 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1421 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1422 PUSH_FIELD_VALUE (cons, "fields",
1423 fields_decl == NULL_TREE ? null_pointer_node
1424 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1425 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1426 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1427 PUSH_FIELD_VALUE (cons, "static_field_count",
1428 build_int_2 (static_field_count, 0));
1429 PUSH_FIELD_VALUE (cons, "vtable",
1430 dtable_decl == NULL_TREE ? null_pointer_node
1431 : build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl));
1432 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1433 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1434 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1435 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1437 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1438 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1439 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1440 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1441 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1443 FINISH_RECORD_CONSTRUCTOR (cons);
1445 DECL_INITIAL (decl) = cons;
1446 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1449 void
1450 finish_class ()
1452 tree method;
1453 tree type_methods = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
1454 int saw_native_method = 0;
1456 /* Find out if we have any native methods. We use this information
1457 later. */
1458 for (method = type_methods;
1459 method != NULL_TREE;
1460 method = TREE_CHAIN (method))
1462 if (METHOD_NATIVE (method))
1464 saw_native_method = 1;
1465 break;
1469 /* Emit deferred inline methods. */
1470 for (method = type_methods; method != NULL_TREE; )
1472 if (! TREE_ASM_WRITTEN (method) && DECL_SAVED_INSNS (method) != 0)
1474 /* It's a deferred inline method. Decide if we need to emit it. */
1475 if (flag_keep_inline_functions
1476 || TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (method))
1477 || ! METHOD_PRIVATE (method)
1478 || saw_native_method)
1480 output_inline_function (method);
1481 /* Scan the list again to see if there are any earlier
1482 methods to emit. */
1483 method = type_methods;
1484 continue;
1487 method = TREE_CHAIN (method);
1490 current_function_decl = NULL_TREE;
1491 make_class_data (current_class);
1492 register_class ();
1493 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
1496 /* Return 2 if CLASS is compiled by this compilation job;
1497 return 1 if CLASS can otherwise be assumed to be compiled;
1498 return 0 if we cannot assume that CLASS is compiled.
1499 Returns 1 for primitive and 0 for array types. */
1501 is_compiled_class (class)
1502 tree class;
1504 int seen_in_zip;
1505 if (TREE_CODE (class) == POINTER_TYPE)
1506 class = TREE_TYPE (class);
1507 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1508 return 1;
1509 if (TYPE_ARRAY_P (class))
1510 return 0;
1511 if (class == current_class)
1512 return 2;
1514 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1515 if (CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (class) || seen_in_zip)
1517 /* The class was seen in the current ZIP file and will be
1518 available as a compiled class in the future but may not have
1519 been loaded already. Load it if necessary. This prevent
1520 build_class_ref () from crashing. */
1522 if (seen_in_zip && !CLASS_LOADED_P (class))
1523 load_class (class, 1);
1525 /* We return 2 for class seen in ZIP and class from files
1526 belonging to the same compilation unit */
1527 return 2;
1530 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1532 if (!CLASS_LOADED_P (class))
1534 if (CLASS_FROM_SOURCE_P (class))
1535 safe_layout_class (class);
1536 else
1537 load_class (class, 1);
1539 return 1;
1542 return 0;
1545 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1547 tree
1548 build_dtable_decl (type)
1549 tree type;
1551 tree dtype;
1553 /* We need to build a new dtable type so that its size is uniquely
1554 computed when we're dealing with the class for real and not just
1555 faking it (like java.lang.Class during the initialization of the
1556 compiler.) We now we're not faking a class when CURRENT_CLASS is
1557 TYPE. */
1558 if (current_class == type)
1560 tree dummy = NULL_TREE, aomt, n;
1562 dtype = make_node (RECORD_TYPE);
1563 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1564 n = build_int_2 (TREE_VEC_LENGTH (get_dispatch_vector (type)), 0);
1565 aomt = build_array_type (ptr_type_node, build_index_type (n));
1566 PUSH_FIELD (dtype, dummy, "methods", aomt);
1567 layout_type (dtype);
1569 else
1570 dtype = dtable_type;
1572 return build_decl (VAR_DECL,
1573 java_mangle_vtable (&temporary_obstack, type), dtype);
1576 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1577 fields inherited from SUPER_CLASS. */
1579 void
1580 push_super_field (this_class, super_class)
1581 tree this_class, super_class;
1583 tree base_decl;
1584 /* Don't insert the field if we're just re-laying the class out. */
1585 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
1586 return;
1587 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1588 DECL_IGNORED_P (base_decl) = 1;
1589 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1590 TYPE_FIELDS (this_class) = base_decl;
1591 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1592 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
1595 /* Handle the different manners we may have to lay out a super class. */
1597 static tree
1598 maybe_layout_super_class (super_class, this_class)
1599 tree super_class;
1600 tree this_class;
1602 if (TREE_CODE (super_class) == RECORD_TYPE)
1604 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
1605 safe_layout_class (super_class);
1606 if (!CLASS_LOADED_P (super_class))
1607 load_class (super_class, 1);
1609 /* We might have to layout the class before its dependency on
1610 the super class gets resolved by java_complete_class */
1611 else if (TREE_CODE (super_class) == POINTER_TYPE)
1613 if (TREE_TYPE (super_class) != NULL_TREE)
1614 super_class = TREE_TYPE (super_class);
1615 else
1617 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
1618 super_class, NULL_TREE, this_class);
1619 if (!super_class)
1620 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
1621 super_class = TREE_TYPE (super_class);
1624 if (!TYPE_SIZE (super_class))
1625 safe_layout_class (super_class);
1627 return super_class;
1630 void
1631 layout_class (this_class)
1632 tree this_class;
1634 tree super_class = CLASSTYPE_SUPER (this_class);
1635 tree field;
1637 class_list = tree_cons (this_class, NULL_TREE, class_list);
1638 if (CLASS_BEING_LAIDOUT (this_class))
1640 char buffer [1024];
1641 char *report;
1642 tree current;
1644 sprintf (buffer, " with `%s'",
1645 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
1646 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1648 for (current = TREE_CHAIN (class_list); current;
1649 current = TREE_CHAIN (current))
1651 tree decl = TYPE_NAME (TREE_PURPOSE (current));
1652 sprintf (buffer, "\n which inherits from `%s' (%s:%d)",
1653 IDENTIFIER_POINTER (DECL_NAME (decl)),
1654 DECL_SOURCE_FILE (decl),
1655 DECL_SOURCE_LINE (decl));
1656 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
1658 obstack_1grow (&temporary_obstack, '\0');
1659 report = obstack_finish (&temporary_obstack);
1660 cyclic_inheritance_report = ggc_strdup (report);
1661 obstack_free (&temporary_obstack, report);
1662 TYPE_SIZE (this_class) = error_mark_node;
1663 return;
1665 CLASS_BEING_LAIDOUT (this_class) = 1;
1667 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
1669 tree maybe_super_class
1670 = maybe_layout_super_class (super_class, this_class);
1671 if (maybe_super_class == NULL
1672 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
1674 TYPE_SIZE (this_class) = error_mark_node;
1675 CLASS_BEING_LAIDOUT (this_class) = 0;
1676 class_list = TREE_CHAIN (class_list);
1677 return;
1679 if (TYPE_SIZE (this_class) == NULL_TREE)
1680 push_super_field (this_class, super_class);
1683 for (field = TYPE_FIELDS (this_class);
1684 field != NULL_TREE; field = TREE_CHAIN (field))
1686 if (FIELD_STATIC (field))
1688 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1689 DECL_ASSEMBLER_NAME (field) =
1690 java_mangle_decl (&temporary_obstack, field);
1694 layout_type (this_class);
1696 /* Also recursively load/layout any superinterfaces, but only if class was
1697 loaded from bytecode. The source parser will take care of this itself. */
1698 if (!CLASS_FROM_SOURCE_P (this_class))
1700 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
1702 if (basetype_vec)
1704 int n = TREE_VEC_LENGTH (basetype_vec) - 1;
1705 int i;
1706 for (i = n; i > 0; i--)
1708 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
1709 tree super_interface = BINFO_TYPE (vec_elt);
1711 tree maybe_super_interface
1712 = maybe_layout_super_class (super_interface, NULL_TREE);
1713 if (maybe_super_interface == NULL
1714 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
1716 TYPE_SIZE (this_class) = error_mark_node;
1717 CLASS_BEING_LAIDOUT (this_class) = 0;
1718 class_list = TREE_CHAIN (class_list);
1719 return;
1725 /* Convert the size back to an SI integer value */
1726 TYPE_SIZE_UNIT (this_class) =
1727 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
1729 CLASS_BEING_LAIDOUT (this_class) = 0;
1730 class_list = TREE_CHAIN (class_list);
1733 void
1734 layout_class_methods (this_class)
1735 tree this_class;
1737 tree method_decl, dtable_count;
1738 tree super_class, handle_type;
1740 if (TYPE_NVIRTUALS (this_class))
1741 return;
1743 super_class = CLASSTYPE_SUPER (this_class);
1744 handle_type = CLASS_TO_HANDLE_TYPE (this_class);
1746 if (super_class)
1748 super_class = maybe_layout_super_class (super_class, this_class);
1749 if (!TYPE_NVIRTUALS (super_class))
1750 layout_class_methods (super_class);
1751 dtable_count = TYPE_NVIRTUALS (super_class);
1753 else
1754 dtable_count = integer_zero_node;
1756 TYPE_METHODS (handle_type) = nreverse (TYPE_METHODS (handle_type));
1758 for (method_decl = TYPE_METHODS (handle_type);
1759 method_decl; method_decl = TREE_CHAIN (method_decl))
1760 dtable_count = layout_class_method (this_class, super_class,
1761 method_decl, dtable_count);
1763 TYPE_NVIRTUALS (this_class) = dtable_count;
1765 #ifdef JAVA_USE_HANDLES
1766 layout_type (handle_type);
1767 #endif
1770 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
1771 and 1 if STR is "greater" than NAME. */
1773 /* Lay METHOD_DECL out, returning a possibly new value of
1774 DTABLE_COUNT. Also mangle the method's name. */
1776 tree
1777 layout_class_method (this_class, super_class, method_decl, dtable_count)
1778 tree this_class, super_class, method_decl, dtable_count;
1780 tree method_name = DECL_NAME (method_decl);
1782 TREE_PUBLIC (method_decl) = 1;
1784 /* This is a good occasion to mangle the method's name */
1785 DECL_ASSEMBLER_NAME (method_decl) =
1786 java_mangle_decl (&temporary_obstack, method_decl);
1787 /* We don't generate a RTL for the method if it's abstract, or if
1788 it's an interface method that isn't clinit. */
1789 if (! METHOD_ABSTRACT (method_decl)
1790 || (CLASS_INTERFACE (TYPE_NAME (this_class))
1791 && (DECL_CLINIT_P (method_decl))))
1792 make_decl_rtl (method_decl, NULL);
1794 if (ID_INIT_P (method_name))
1796 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1797 const char *ptr;
1798 for (ptr = p; *ptr; )
1800 if (*ptr++ == '.')
1801 p = ptr;
1803 DECL_CONSTRUCTOR_P (method_decl) = 1;
1804 build_java_argument_signature (TREE_TYPE (method_decl));
1806 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
1808 tree method_sig =
1809 build_java_argument_signature (TREE_TYPE (method_decl));
1810 tree super_method = lookup_argument_method (super_class, method_name,
1811 method_sig);
1812 if (super_method != NULL_TREE && ! METHOD_PRIVATE (super_method))
1814 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
1815 if (DECL_VINDEX (method_decl) == NULL_TREE
1816 && !CLASS_FROM_SOURCE_P (this_class))
1817 error_with_decl (method_decl,
1818 "non-static method '%s' overrides static method");
1820 else if (! METHOD_FINAL (method_decl)
1821 && ! METHOD_PRIVATE (method_decl)
1822 && ! CLASS_FINAL (TYPE_NAME (this_class))
1823 && dtable_count)
1825 DECL_VINDEX (method_decl) = dtable_count;
1826 dtable_count = fold (build (PLUS_EXPR, integer_type_node,
1827 dtable_count, integer_one_node));
1831 return dtable_count;
1834 void
1835 register_class ()
1837 /* END does not need to be registered with the garbage collector
1838 because it always points into the list given by REGISTERED_CLASS,
1839 and that variable is registered with the collector. */
1840 static tree end;
1841 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
1842 tree current = copy_node (node);
1844 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
1845 if (!registered_class)
1846 registered_class = current;
1847 else
1848 TREE_CHAIN (end) = current;
1850 end = current;
1853 /* Generate a function that gets called at start-up (static contructor) time,
1854 which calls registerClass for all the compiled classes. */
1856 void
1857 emit_register_classes ()
1859 extern tree get_file_function_name PARAMS ((int));
1860 tree init_name = get_file_function_name ('I');
1861 tree init_type = build_function_type (void_type_node, end_params_node);
1862 tree init_decl;
1863 tree t;
1865 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
1866 DECL_ASSEMBLER_NAME (init_decl) = init_name;
1867 TREE_STATIC (init_decl) = 1;
1868 current_function_decl = init_decl;
1869 DECL_RESULT (init_decl) = build_decl(RESULT_DECL, NULL_TREE, void_type_node);
1870 /* DECL_EXTERNAL (init_decl) = 1;*/
1871 TREE_PUBLIC (init_decl) = 1;
1872 pushlevel (0);
1873 make_decl_rtl (init_decl, NULL);
1874 init_function_start (init_decl, input_filename, 0);
1875 expand_function_start (init_decl, 0);
1877 for ( t = registered_class; t; t = TREE_CHAIN (t))
1878 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
1879 XEXP (DECL_RTL (t), 0), Pmode);
1881 expand_function_end (input_filename, 0, 0);
1882 poplevel (1, 0, 1);
1884 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
1885 int saved_flag = flag_inline_functions;
1886 flag_inline_functions = 0;
1887 rest_of_compilation (init_decl);
1888 flag_inline_functions = saved_flag;
1890 current_function_decl = NULL_TREE;
1891 assemble_constructor (IDENTIFIER_POINTER (init_name));
1894 void
1895 init_class_processing ()
1897 registerClass_libfunc = gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterClass");
1898 ggc_add_tree_root (class_roots, sizeof (class_roots) / sizeof (tree));
1899 fields_ident = get_identifier ("fields");
1900 info_ident = get_identifier ("info");
1901 ggc_add_rtx_root (&registerClass_libfunc, 1);
1902 gcc_obstack_init (&temporary_obstack);