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