class.c (push_class): By default, suppress debug output.
[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, 2003, 2004, 2005
3 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
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 "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "rtl.h"
34 #include "flags.h"
35 #include "java-tree.h"
36 #include "jcf.h"
37 #include "obstack.h"
38 #include "toplev.h"
39 #include "output.h"
40 #include "parse.h"
41 #include "function.h"
42 #include "ggc.h"
43 #include "stdio.h"
44 #include "target.h"
45 #include "except.h"
46 #include "cgraph.h"
47 #include "tree-iterator.h"
48 #include "cgraph.h"
49
50 /* DOS brain-damage */
51 #ifndef O_BINARY
52 #define O_BINARY 0 /* MS-DOS brain-damage */
53 #endif
54
55 static tree make_method_value (tree);
56 static tree build_java_method_type (tree, tree, int);
57 static int32 hashUtf8String (const char *, int);
58 static tree make_field_value (tree);
59 static tree get_dispatch_vector (tree);
60 static tree get_dispatch_table (tree, tree);
61 static int supers_all_compiled (tree type);
62 static tree maybe_layout_super_class (tree, tree);
63 static void add_miranda_methods (tree, tree);
64 static int assume_compiled (const char *);
65 static tree build_symbol_entry (tree);
66 static tree emit_assertion_table (tree);
67
68 struct obstack temporary_obstack;
69
70 /* The compiler generates different code depending on whether or not
71 it can assume certain classes have been compiled down to native
72 code or not. The compiler options -fassume-compiled= and
73 -fno-assume-compiled= are used to create a tree of
74 class_flag_node objects. This tree is queried to determine if
75 a class is assume to be compiled or not. Each node in the tree
76 represents either a package or a specific class. */
77
78 typedef struct class_flag_node_struct
79 {
80 /* The class or package name. */
81 const char *ident;
82
83 /* Nonzero if this represents an exclusion. */
84 int value;
85
86 /* Pointers to other nodes in the tree. */
87 struct class_flag_node_struct *parent;
88 struct class_flag_node_struct *sibling;
89 struct class_flag_node_struct *child;
90 } class_flag_node;
91
92 static class_flag_node *find_class_flag_node (class_flag_node *, const char *);
93 static void add_class_flag (class_flag_node **, const char *, int);
94
95 /* This is the root of the include/exclude tree. */
96
97 static class_flag_node *assume_compiled_tree;
98
99 static class_flag_node *enable_assert_tree;
100
101 static GTY(()) tree class_roots[5];
102 #define registered_class class_roots[0]
103 #define fields_ident class_roots[1] /* get_identifier ("fields") */
104 #define info_ident class_roots[2] /* get_identifier ("info") */
105 #define class_list class_roots[3]
106 #define class_dtable_decl class_roots[4]
107
108 /* Return the node that most closely represents the class whose name
109 is IDENT. Start the search from NODE (followed by its siblings).
110 Return NULL if an appropriate node does not exist. */
111
112 static class_flag_node *
113 find_class_flag_node (class_flag_node *node, 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 && (ident[node_ident_length] == '\0'
127 || ident[node_ident_length] == '.')))
128 {
129 /* We've found a match, however, there might be a more
130 specific match. */
131
132 class_flag_node *found = find_class_flag_node (node->child, ident);
133 if (found)
134 return found;
135 else
136 return node;
137 }
138
139 /* No match yet. Continue through the sibling list. */
140 node = node->sibling;
141 }
142
143 /* No match at all in this tree. */
144 return NULL;
145 }
146
147 void
148 add_class_flag (class_flag_node **rootp, const char *ident, int value)
149 {
150 class_flag_node *root = *rootp;
151 class_flag_node *parent, *node;
152
153 /* Create the root of the tree if it doesn't exist yet. */
154
155 if (NULL == root)
156 {
157 root = xmalloc (sizeof (class_flag_node));
158 root->ident = "";
159 root->value = 0;
160 root->sibling = NULL;
161 root->child = NULL;
162 root->parent = NULL;
163 *rootp = root;
164 }
165
166 /* Calling the function with the empty string means we're setting
167 value for the root of the hierarchy. */
168
169 if (0 == ident[0])
170 {
171 root->value = value;
172 return;
173 }
174
175 /* Find the parent node for this new node. PARENT will either be a
176 class or a package name. Adjust PARENT accordingly. */
177
178 parent = find_class_flag_node (root, ident);
179 if (strcmp (ident, parent->ident) == 0)
180 parent->value = value;
181 else
182 {
183 /* Insert new node into the tree. */
184 node = xmalloc (sizeof (class_flag_node));
185
186 node->ident = xstrdup (ident);
187 node->value = value;
188 node->child = NULL;
189
190 node->parent = parent;
191 node->sibling = parent->child;
192 parent->child = node;
193 }
194 }
195
196 /* Add a new IDENT to the include/exclude tree. It's an exclusion
197 if EXCLUDEP is nonzero. */
198
199 void
200 add_assume_compiled (const char *ident, int excludep)
201 {
202 add_class_flag (&assume_compiled_tree, ident, excludep);
203 }
204
205 /* The default value returned by enable_assertions. */
206
207 #define DEFAULT_ENABLE_ASSERT (flag_emit_class_files || optimize == 0)
208
209 /* Enter IDENT (a class or package name) into the enable-assertions table.
210 VALUE is true to enable and false to disable. */
211
212 void
213 add_enable_assert (const char *ident, int value)
214 {
215 if (enable_assert_tree == NULL)
216 add_class_flag (&enable_assert_tree, "", DEFAULT_ENABLE_ASSERT);
217 add_class_flag (&enable_assert_tree, ident, value);
218 }
219
220 /* Returns nonzero if IDENT is the name of a class that the compiler
221 should assume has been compiled to object code. */
222
223 static int
224 assume_compiled (const char *ident)
225 {
226 class_flag_node *i;
227 int result;
228
229 if (NULL == assume_compiled_tree)
230 return 1;
231
232 i = find_class_flag_node (assume_compiled_tree, ident);
233
234 result = ! i->value;
235
236 return (result);
237 }
238
239 /* Return true if we should generate code to check assertions within KLASS. */
240
241 bool
242 enable_assertions (tree klass)
243 {
244 /* Check if command-line specifies whether we should check assertions. */
245
246 if (klass != NULL_TREE && DECL_NAME (klass) && enable_assert_tree != NULL)
247 {
248 const char *ident = IDENTIFIER_POINTER (DECL_NAME (klass));
249 class_flag_node *node
250 = find_class_flag_node (enable_assert_tree, ident);
251 return node->value;
252 }
253
254 /* The default is to enable assertions if generating class files,
255 or not optimizing. */
256 return DEFAULT_ENABLE_ASSERT;
257 }
258
259 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
260 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
261 Also, PREFIX is prepended, and SUFFIX is appended. */
262
263 tree
264 ident_subst (const char* old_name,
265 int old_length,
266 const char *prefix,
267 int old_char,
268 int new_char,
269 const char *suffix)
270 {
271 int prefix_len = strlen (prefix);
272 int suffix_len = strlen (suffix);
273 int i = prefix_len + old_length + suffix_len + 1;
274 char *buffer = alloca (i);
275
276 strcpy (buffer, prefix);
277 for (i = 0; i < old_length; i++)
278 {
279 char ch = old_name[i];
280 if (ch == old_char)
281 ch = new_char;
282 buffer[prefix_len + i] = ch;
283 }
284 strcpy (buffer + prefix_len + old_length, suffix);
285 return get_identifier (buffer);
286 }
287
288 /* Return an IDENTIFIER_NODE the same as OLD_ID,
289 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
290 Also, PREFIX is prepended, and SUFFIX is appended. */
291
292 tree
293 identifier_subst (const tree old_id,
294 const char *prefix,
295 int old_char,
296 int new_char,
297 const char *suffix)
298 {
299 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
300 prefix, old_char, new_char, suffix);
301 }
302
303 /* Generate a valid C identifier from the name of the class TYPE,
304 prefixed by PREFIX. */
305
306 tree
307 mangled_classname (const char *prefix, tree type)
308 {
309 tree ident = TYPE_NAME (type);
310 if (TREE_CODE (ident) != IDENTIFIER_NODE)
311 ident = DECL_NAME (ident);
312 return identifier_subst (ident, prefix, '.', '_', "");
313 }
314
315 tree
316 make_class (void)
317 {
318 tree type;
319 type = make_node (RECORD_TYPE);
320 MAYBE_CREATE_TYPE_TYPE_LANG_SPECIFIC (type);
321
322 return type;
323 }
324
325 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
326 and where each of the constituents is separated by '/',
327 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
328
329 tree
330 unmangle_classname (const char *name, int name_length)
331 {
332 tree to_return = ident_subst (name, name_length, "", '/', '.', "");
333 /* It's not sufficient to compare to_return and get_identifier
334 (name) to determine whether to_return is qualified. There are
335 cases in signature analysis where name will be stripped of a
336 trailing ';'. */
337 name = IDENTIFIER_POINTER (to_return);
338 while (*name)
339 if (*name++ == '.')
340 {
341 QUALIFIED_P (to_return) = 1;
342 break;
343 }
344
345 return to_return;
346 }
347
348 #define GEN_TABLE(TABLE, NAME, TABLE_TYPE, TYPE) \
349 do \
350 { \
351 const char *typename = IDENTIFIER_POINTER (mangled_classname ("", TYPE)); \
352 char *buf = alloca (strlen (typename) + strlen (#NAME "_syms_") + 1); \
353 tree decl; \
354 \
355 sprintf (buf, #NAME "_%s", typename); \
356 TYPE_## TABLE ##_DECL (type) = decl = \
357 build_decl (VAR_DECL, get_identifier (buf), TABLE_TYPE); \
358 DECL_EXTERNAL (decl) = 1; \
359 TREE_STATIC (decl) = 1; \
360 TREE_READONLY (decl) = 1; \
361 TREE_CONSTANT (decl) = 1; \
362 DECL_IGNORED_P (decl) = 1; \
363 /* Mark the table as belonging to this class. */ \
364 pushdecl (decl); \
365 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (decl); \
366 DECL_OWNER (decl) = TYPE; \
367 sprintf (buf, #NAME "_syms_%s", typename); \
368 TYPE_## TABLE ##_SYMS_DECL (TYPE) = \
369 build_decl (VAR_DECL, get_identifier (buf), symbols_array_type); \
370 TREE_STATIC (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
371 TREE_CONSTANT (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
372 DECL_IGNORED_P (TYPE_## TABLE ##_SYMS_DECL (TYPE)) = 1; \
373 pushdecl (TYPE_## TABLE ##_SYMS_DECL (TYPE)); \
374 } \
375 while (0)
376
377 /* Given a class, create the DECLs for all its associated indirect
378 dispatch tables. */
379 void
380 gen_indirect_dispatch_tables (tree type)
381 {
382 const char *typename = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
383 {
384 tree field = NULL;
385 char *buf = alloca (strlen (typename) + strlen ("_catch_classes_") + 1);
386 tree catch_class_type = make_node (RECORD_TYPE);
387
388 sprintf (buf, "_catch_classes_%s", typename);
389 PUSH_FIELD (catch_class_type, field, "address", utf8const_ptr_type);
390 PUSH_FIELD (catch_class_type, field, "classname", ptr_type_node);
391 FINISH_RECORD (catch_class_type);
392
393 TYPE_CTABLE_DECL (type)
394 = build_decl (VAR_DECL, get_identifier (buf),
395 build_array_type (catch_class_type, 0));
396 DECL_EXTERNAL (TYPE_CTABLE_DECL (type)) = 1;
397 TREE_STATIC (TYPE_CTABLE_DECL (type)) = 1;
398 TREE_READONLY (TYPE_CTABLE_DECL (type)) = 1;
399 TREE_CONSTANT (TYPE_CTABLE_DECL (type)) = 1;
400 DECL_IGNORED_P (TYPE_CTABLE_DECL (type)) = 1;
401 pushdecl (TYPE_CTABLE_DECL (type));
402 }
403
404 if (flag_indirect_dispatch)
405 {
406 GEN_TABLE (ATABLE, _atable, atable_type, type);
407 GEN_TABLE (OTABLE, _otable, otable_type, type);
408 GEN_TABLE (ITABLE, _itable, itable_type, type);
409 }
410 }
411
412 #undef GEN_TABLE
413
414 tree
415 push_class (tree class_type, tree class_name)
416 {
417 tree decl, signature;
418 location_t saved_loc = input_location;
419 #ifndef USE_MAPPED_LOCATION
420 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
421 input_filename = IDENTIFIER_POINTER (source_name);
422 input_line = 0;
423 #endif
424 CLASS_P (class_type) = 1;
425 decl = build_decl (TYPE_DECL, class_name, class_type);
426 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
427
428 /* dbxout needs a DECL_SIZE if in gstabs mode */
429 DECL_SIZE (decl) = integer_zero_node;
430
431 input_location = saved_loc;
432 signature = identifier_subst (class_name, "L", '.', '/', ";");
433 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
434
435 /* Setting DECL_ARTIFICIAL forces dbxout.c to specific the type is
436 both a typedef and in the struct name-space. We may want to re-visit
437 this later, but for now it reduces the changes needed for gdb. */
438 DECL_ARTIFICIAL (decl) = 1;
439
440 pushdecl_top_level (decl);
441
442 return decl;
443 }
444
445 /* Finds the (global) class named NAME. Creates the class if not found.
446 Also creates associated TYPE_DECL.
447 Does not check if the class actually exists, load the class,
448 fill in field or methods, or do layout_type. */
449
450 tree
451 lookup_class (tree name)
452 {
453 tree decl = IDENTIFIER_CLASS_VALUE (name);
454 if (decl == NULL_TREE)
455 decl = push_class (make_class (), name);
456 return TREE_TYPE (decl);
457 }
458
459 void
460 set_super_info (int access_flags, tree this_class,
461 tree super_class, int interfaces_count)
462 {
463 int total_supers = interfaces_count;
464 tree class_decl = TYPE_NAME (this_class);
465
466 if (super_class)
467 total_supers++;
468
469 TYPE_BINFO (this_class) = make_tree_binfo (total_supers);
470 TYPE_VFIELD (this_class) = TYPE_VFIELD (object_type_node);
471 if (super_class)
472 {
473 tree super_binfo = make_tree_binfo (0);
474 BINFO_TYPE (super_binfo) = super_class;
475 BINFO_OFFSET (super_binfo) = integer_zero_node;
476 BINFO_BASE_APPEND (TYPE_BINFO (this_class), super_binfo);
477 CLASS_HAS_SUPER_FLAG (TYPE_BINFO (this_class)) = 1;
478 }
479
480 set_class_decl_access_flags (access_flags, class_decl);
481 }
482
483 void
484 set_class_decl_access_flags (int access_flags, tree class_decl)
485 {
486 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
487 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
488 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
489 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
490 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
491 if (access_flags & ACC_STATIC) CLASS_STATIC (class_decl) = 1;
492 if (access_flags & ACC_PRIVATE) CLASS_PRIVATE (class_decl) = 1;
493 if (access_flags & ACC_PROTECTED) CLASS_PROTECTED (class_decl) = 1;
494 if (access_flags & ACC_STRICT) CLASS_STRICTFP (class_decl) = 1;
495 }
496
497 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
498 direct sub-classes of Object are 1, and so on. */
499
500 int
501 class_depth (tree clas)
502 {
503 int depth = 0;
504 if (! CLASS_LOADED_P (clas))
505 load_class (clas, 1);
506 if (TYPE_SIZE (clas) == error_mark_node)
507 return -1;
508 while (clas != object_type_node)
509 {
510 depth++;
511 clas = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (clas), 0));
512 }
513 return depth;
514 }
515
516 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
517
518 int
519 interface_of_p (tree type1, tree type2)
520 {
521 int i;
522 tree binfo, base_binfo;
523
524 if (! TYPE_BINFO (type2))
525 return 0;
526
527 for (binfo = TYPE_BINFO (type2), i = 0;
528 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
529 if (BINFO_TYPE (base_binfo) == type1)
530 return 1;
531
532 for (binfo = TYPE_BINFO (type2), i = 0;
533 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) /* */
534 if (BINFO_TYPE (base_binfo)
535 && interface_of_p (type1, BINFO_TYPE (base_binfo)))
536 return 1;
537
538 return 0;
539 }
540
541 /* Return true iff TYPE1 inherits from TYPE2. */
542
543 int
544 inherits_from_p (tree type1, tree type2)
545 {
546 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
547 {
548 if (type1 == type2)
549 return 1;
550 type1 = CLASSTYPE_SUPER (type1);
551 }
552 return 0;
553 }
554
555 /* Return a 1 iff TYPE1 is an enclosing context for TYPE2 */
556
557 int
558 enclosing_context_p (tree type1, tree type2)
559 {
560 if (!INNER_CLASS_TYPE_P (type2))
561 return 0;
562
563 for (type2 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2)));
564 type2;
565 type2 = (INNER_CLASS_TYPE_P (type2) ?
566 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))) : NULL_TREE))
567 {
568 if (type2 == type1)
569 return 1;
570 }
571
572 return 0;
573 }
574
575
576 /* Return 1 iff TYPE1 and TYPE2 share a common enclosing class, regardless of
577 nesting level. */
578
579 int
580 common_enclosing_context_p (tree type1, tree type2)
581 {
582 while (type1)
583 {
584 tree current;
585 for (current = type2; current;
586 current = (INNER_CLASS_TYPE_P (current) ?
587 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
588 NULL_TREE))
589 if (type1 == current)
590 return 1;
591
592 if (INNER_CLASS_TYPE_P (type1))
593 type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1)));
594 else
595 break;
596 }
597 return 0;
598 }
599
600 /* Return 1 iff there exists a common enclosing "this" between TYPE1
601 and TYPE2, without crossing any static context. */
602
603 int
604 common_enclosing_instance_p (tree type1, tree type2)
605 {
606 if (!PURE_INNER_CLASS_TYPE_P (type1) || !PURE_INNER_CLASS_TYPE_P (type2))
607 return 0;
608
609 for (type1 = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))); type1;
610 type1 = (PURE_INNER_CLASS_TYPE_P (type1) ?
611 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type1))) : NULL_TREE))
612 {
613 tree current;
614 for (current = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (type2))); current;
615 current = (PURE_INNER_CLASS_TYPE_P (current) ?
616 TREE_TYPE (DECL_CONTEXT (TYPE_NAME (current))) :
617 NULL_TREE))
618 if (type1 == current)
619 return 1;
620 }
621 return 0;
622 }
623
624 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
625 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
626 if attempt is made to add it twice. */
627
628 tree
629 maybe_add_interface (tree this_class, tree interface_class)
630 {
631 tree binfo, base_binfo;
632 int i;
633
634 for (binfo = TYPE_BINFO (this_class), i = 0;
635 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
636 if (BINFO_TYPE (base_binfo) == interface_class)
637 return interface_class;
638 add_interface (this_class, interface_class);
639 return NULL_TREE;
640 }
641
642 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
643
644 void
645 add_interface (tree this_class, tree interface_class)
646 {
647 tree interface_binfo = make_tree_binfo (0);
648
649 BINFO_TYPE (interface_binfo) = interface_class;
650 BINFO_OFFSET (interface_binfo) = integer_zero_node;
651 BINFO_VPTR_FIELD (interface_binfo) = integer_zero_node;
652 BINFO_VIRTUAL_P (interface_binfo) = 1;
653
654 BINFO_BASE_APPEND (TYPE_BINFO (this_class), interface_binfo);
655 }
656
657 #if 0
658 /* Return the address of a pointer to the first FUNCTION_DECL
659 in the list (*LIST) whose DECL_NAME is NAME. */
660
661 static tree *
662 find_named_method (tree *list, tree name)
663 {
664 while (*list && DECL_NAME (*list) != name)
665 list = &TREE_CHAIN (*list);
666 return list;
667 }
668 #endif
669
670 static tree
671 build_java_method_type (tree fntype, tree this_class, int access_flags)
672 {
673 if (access_flags & ACC_STATIC)
674 return fntype;
675 return build_method_type (this_class, fntype);
676 }
677
678 tree
679 add_method_1 (tree this_class, int access_flags, tree name, tree function_type)
680 {
681 tree method_type, fndecl;
682
683 method_type = build_java_method_type (function_type,
684 this_class, access_flags);
685
686 fndecl = build_decl (FUNCTION_DECL, name, method_type);
687 DECL_CONTEXT (fndecl) = this_class;
688
689 DECL_LANG_SPECIFIC (fndecl)
690 = ggc_alloc_cleared (sizeof (struct lang_decl));
691 DECL_LANG_SPECIFIC (fndecl)->desc = LANG_DECL_FUNC;
692
693 /* Initialize the static initializer test table. */
694
695 DECL_FUNCTION_INIT_TEST_TABLE (fndecl) =
696 java_treetreehash_create (10, 1);
697
698 /* Initialize the initialized (static) class table. */
699 if (access_flags & ACC_STATIC)
700 DECL_FUNCTION_INITIALIZED_CLASS_TABLE (fndecl) =
701 htab_create_ggc (50, htab_hash_pointer, htab_eq_pointer, NULL);
702
703 /* Initialize the static method invocation compound list */
704 DECL_FUNCTION_STATIC_METHOD_INVOCATION_COMPOUND (fndecl) = NULL_TREE;
705
706 TREE_CHAIN (fndecl) = TYPE_METHODS (this_class);
707 TYPE_METHODS (this_class) = fndecl;
708
709 /* Notice that this is a finalizer and update the class type
710 accordingly. This is used to optimize instance allocation. */
711 if (name == finalize_identifier_node
712 && TREE_TYPE (function_type) == void_type_node
713 && TREE_VALUE (TYPE_ARG_TYPES (function_type)) == void_type_node)
714 HAS_FINALIZER_P (this_class) = 1;
715
716 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
717 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
718 if (access_flags & ACC_PRIVATE)
719 METHOD_PRIVATE (fndecl) = DECL_INLINE (fndecl) = 1;
720 if (access_flags & ACC_NATIVE)
721 {
722 METHOD_NATIVE (fndecl) = 1;
723 DECL_EXTERNAL (fndecl) = 1;
724 }
725 if (access_flags & ACC_STATIC)
726 METHOD_STATIC (fndecl) = DECL_INLINE (fndecl) = 1;
727 if (access_flags & ACC_FINAL)
728 METHOD_FINAL (fndecl) = DECL_INLINE (fndecl) = 1;
729 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
730 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
731 if (access_flags & ACC_STRICT) METHOD_STRICTFP (fndecl) = 1;
732 return fndecl;
733 }
734
735 /* Add a method to THIS_CLASS.
736 The method's name is NAME.
737 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
738
739 tree
740 add_method (tree this_class, int access_flags, tree name, tree method_sig)
741 {
742 tree function_type, fndecl;
743 const unsigned char *sig
744 = (const unsigned char *) IDENTIFIER_POINTER (method_sig);
745
746 if (sig[0] != '(')
747 fatal_error ("bad method signature");
748
749 function_type = get_type_from_signature (method_sig);
750 fndecl = add_method_1 (this_class, access_flags, name, function_type);
751 set_java_signature (TREE_TYPE (fndecl), method_sig);
752 return fndecl;
753 }
754
755 tree
756 add_field (tree class, tree name, tree field_type, int flags)
757 {
758 int is_static = (flags & ACC_STATIC) != 0;
759 tree field;
760 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
761 TREE_CHAIN (field) = TYPE_FIELDS (class);
762 TYPE_FIELDS (class) = field;
763 DECL_CONTEXT (field) = class;
764
765 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
766 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
767 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
768 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
769 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
770 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
771 if (is_static)
772 {
773 FIELD_STATIC (field) = 1;
774 /* Always make field externally visible. This is required so
775 that native methods can always access the field. */
776 TREE_PUBLIC (field) = 1;
777 /* Considered external until we know what classes are being
778 compiled into this object file. */
779 DECL_EXTERNAL (field) = 1;
780 }
781
782 return field;
783 }
784
785 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
786
787 void
788 set_constant_value (tree field, tree constant)
789 {
790 if (field == NULL_TREE)
791 warning ("misplaced ConstantValue attribute (not in any field)");
792 else if (DECL_INITIAL (field) != NULL_TREE)
793 warning ("duplicate ConstantValue attribute for field '%s'",
794 IDENTIFIER_POINTER (DECL_NAME (field)));
795 else
796 {
797 DECL_INITIAL (field) = constant;
798 if (TREE_TYPE (constant) != TREE_TYPE (field)
799 && ! (TREE_TYPE (constant) == int_type_node
800 && INTEGRAL_TYPE_P (TREE_TYPE (field))
801 && TYPE_PRECISION (TREE_TYPE (field)) <= 32)
802 && ! (TREE_TYPE (constant) == utf8const_ptr_type
803 && TREE_TYPE (field) == string_ptr_type_node))
804 error ("ConstantValue attribute of field '%s' has wrong type",
805 IDENTIFIER_POINTER (DECL_NAME (field)));
806 if (FIELD_FINAL (field))
807 DECL_FIELD_FINAL_IUD (field) = 1;
808 }
809 }
810
811 /* Count the number of Unicode chars encoded in a given Ut8 string. */
812
813 #if 0
814 int
815 strLengthUtf8 (char *str, int len)
816 {
817 register unsigned char* ptr = (unsigned char*) str;
818 register unsigned char *limit = ptr + len;
819 int str_length = 0;
820 for (; ptr < limit; str_length++) {
821 if (UTF8_GET (ptr, limit) < 0)
822 return -1;
823 }
824 return str_length;
825 }
826 #endif
827
828
829 /* Calculate a hash value for a string encoded in Utf8 format.
830 * This returns the same hash value as specified for java.lang.String.hashCode.
831 */
832
833 static int32
834 hashUtf8String (const char *str, int len)
835 {
836 const unsigned char* ptr = (const unsigned char*) str;
837 const unsigned char *limit = ptr + len;
838 int32 hash = 0;
839 for (; ptr < limit;)
840 {
841 int ch = UTF8_GET (ptr, limit);
842 /* Updated specification from
843 http://www.javasoft.com/docs/books/jls/clarify.html. */
844 hash = (31 * hash) + ch;
845 }
846 return hash;
847 }
848
849 static GTY(()) tree utf8_decl_list = NULL_TREE;
850
851 tree
852 build_utf8_ref (tree name)
853 {
854 const char * name_ptr = IDENTIFIER_POINTER(name);
855 int name_len = IDENTIFIER_LENGTH(name);
856 char buf[60];
857 tree ctype, field = NULL_TREE, str_type, cinit, string;
858 static int utf8_count = 0;
859 int name_hash;
860 tree ref = IDENTIFIER_UTF8_REF (name);
861 tree decl;
862 if (ref != NULL_TREE)
863 return ref;
864
865 ctype = make_node (RECORD_TYPE);
866 str_type = build_prim_array_type (unsigned_byte_type_node,
867 name_len + 1); /* Allow for final '\0'. */
868 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
869 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
870 PUSH_FIELD (ctype, field, "data", str_type);
871 FINISH_RECORD (ctype);
872 START_RECORD_CONSTRUCTOR (cinit, ctype);
873 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
874 PUSH_FIELD_VALUE (cinit, "hash", build_int_cst (NULL_TREE, name_hash));
875 PUSH_FIELD_VALUE (cinit, "length", build_int_cst (NULL_TREE, name_len));
876 string = build_string (name_len, name_ptr);
877 TREE_TYPE (string) = str_type;
878 PUSH_FIELD_VALUE (cinit, "data", string);
879 FINISH_RECORD_CONSTRUCTOR (cinit);
880 TREE_CONSTANT (cinit) = 1;
881 TREE_INVARIANT (cinit) = 1;
882
883 /* Generate a unique-enough identifier. */
884 sprintf(buf, "_Utf%d", ++utf8_count);
885
886 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
887 TREE_STATIC (decl) = 1;
888 DECL_ARTIFICIAL (decl) = 1;
889 DECL_IGNORED_P (decl) = 1;
890 TREE_READONLY (decl) = 1;
891 TREE_THIS_VOLATILE (decl) = 0;
892 DECL_INITIAL (decl) = cinit;
893
894 if (HAVE_GAS_SHF_MERGE)
895 {
896 int decl_size;
897 /* Ensure decl_size is a multiple of utf8const_type's alignment. */
898 decl_size = (name_len + 5 + TYPE_ALIGN_UNIT (utf8const_type) - 1)
899 & ~(TYPE_ALIGN_UNIT (utf8const_type) - 1);
900 if (flag_merge_constants && decl_size < 256)
901 {
902 char buf[32];
903 int flags = (SECTION_OVERRIDE
904 | SECTION_MERGE | (SECTION_ENTSIZE & decl_size));
905 sprintf (buf, ".rodata.jutf8.%d", decl_size);
906 named_section_flags (buf, flags);
907 DECL_SECTION_NAME (decl) = build_string (strlen (buf), buf);
908 }
909 }
910
911 TREE_CHAIN (decl) = utf8_decl_list;
912 layout_decl (decl, 0);
913 pushdecl (decl);
914 rest_of_decl_compilation (decl, global_bindings_p (), 0);
915 cgraph_varpool_mark_needed_node (cgraph_varpool_node (decl));
916 utf8_decl_list = decl;
917 make_decl_rtl (decl);
918 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
919 IDENTIFIER_UTF8_REF (name) = ref;
920 return ref;
921 }
922
923 /* Like build_class_ref, but instead of a direct reference generate a
924 pointer into the constant pool. */
925
926 static tree
927 build_indirect_class_ref (tree type)
928 {
929 int index;
930 tree cl;
931 index = alloc_class_constant (type);
932 cl = build_ref_from_constant_pool (index);
933 return convert (promote_type (class_ptr_type), cl);
934 }
935
936 /* Build a reference to the class TYPE.
937 Also handles primitive types and array types. */
938
939 tree
940 build_class_ref (tree type)
941 {
942 int is_compiled = is_compiled_class (type);
943 if (is_compiled)
944 {
945 tree ref, decl_name, decl;
946 if (TREE_CODE (type) == POINTER_TYPE)
947 type = TREE_TYPE (type);
948
949 if (flag_indirect_dispatch
950 && type != output_class
951 && TREE_CODE (type) == RECORD_TYPE)
952 return build_indirect_class_ref (type);
953
954 if (TREE_CODE (type) == RECORD_TYPE)
955 {
956 if (TYPE_SIZE (type) == error_mark_node)
957 return null_pointer_node;
958 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
959 "", '/', '/', ".class");
960 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
961 if (decl == NULL_TREE)
962 {
963 decl = build_decl (VAR_DECL, decl_name, class_type_node);
964 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
965 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (class_type_node);
966 TREE_STATIC (decl) = 1;
967 TREE_PUBLIC (decl) = 1;
968 DECL_IGNORED_P (decl) = 1;
969 DECL_ARTIFICIAL (decl) = 1;
970 if (is_compiled == 1)
971 DECL_EXTERNAL (decl) = 1;
972 SET_DECL_ASSEMBLER_NAME (decl,
973 java_mangle_class_field
974 (&temporary_obstack, type));
975 make_decl_rtl (decl);
976 pushdecl_top_level (decl);
977 }
978 }
979 else
980 {
981 const char *name;
982 char buffer[25];
983 if (flag_emit_class_files)
984 {
985 const char *prim_class_name;
986 tree prim_class;
987 if (type == char_type_node)
988 prim_class_name = "java.lang.Character";
989 else if (type == boolean_type_node)
990 prim_class_name = "java.lang.Boolean";
991 else if (type == byte_type_node)
992 prim_class_name = "java.lang.Byte";
993 else if (type == short_type_node)
994 prim_class_name = "java.lang.Short";
995 else if (type == int_type_node)
996 prim_class_name = "java.lang.Integer";
997 else if (type == long_type_node)
998 prim_class_name = "java.lang.Long";
999 else if (type == float_type_node)
1000 prim_class_name = "java.lang.Float";
1001 else if (type == double_type_node)
1002 prim_class_name = "java.lang.Double";
1003 else if (type == void_type_node)
1004 prim_class_name = "java.lang.Void";
1005 else
1006 abort ();
1007
1008 prim_class = lookup_class (get_identifier (prim_class_name));
1009 return build3 (COMPONENT_REF, NULL_TREE,
1010 prim_class, TYPE_identifier_node, NULL_TREE);
1011 }
1012 decl_name = TYPE_NAME (type);
1013 if (TREE_CODE (decl_name) == TYPE_DECL)
1014 decl_name = DECL_NAME (decl_name);
1015 name = IDENTIFIER_POINTER (decl_name);
1016 if (strncmp (name, "promoted_", 9) == 0)
1017 name += 9;
1018 sprintf (buffer, "_Jv_%sClass", name);
1019 decl_name = get_identifier (buffer);
1020 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1021 if (decl == NULL_TREE)
1022 {
1023 decl = build_decl (VAR_DECL, decl_name, class_type_node);
1024 TREE_STATIC (decl) = 1;
1025 TREE_PUBLIC (decl) = 1;
1026 DECL_EXTERNAL (decl) = 1;
1027 DECL_ARTIFICIAL (decl) = 1;
1028 make_decl_rtl (decl);
1029 pushdecl_top_level (decl);
1030 }
1031 }
1032
1033 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
1034 return ref;
1035 }
1036 else
1037 return build_indirect_class_ref (type);
1038 }
1039
1040 tree
1041 build_static_field_ref (tree fdecl)
1042 {
1043 tree fclass = DECL_CONTEXT (fdecl);
1044 int is_compiled = is_compiled_class (fclass);
1045
1046 /* Allow static final fields to fold to a constant. When using
1047 -fno-assume-compiled, gcj will sometimes try to fold a field from
1048 an uncompiled class. This is required when the field in question
1049 meets the appropriate criteria for a compile-time constant.
1050 However, currently sometimes gcj is too eager and will end up
1051 returning the field itself, leading to an incorrect external
1052 reference being generated. */
1053 if ((is_compiled && !flag_indirect_dispatch)
1054 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1055 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1056 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1057 && TREE_CONSTANT (DECL_INITIAL (fdecl))))
1058 {
1059 if (!DECL_RTL_SET_P (fdecl))
1060 {
1061 if (is_compiled == 1)
1062 DECL_EXTERNAL (fdecl) = 1;
1063 make_decl_rtl (fdecl);
1064 }
1065 return fdecl;
1066 }
1067
1068 if (flag_indirect_dispatch)
1069 {
1070 tree table_index
1071 = build_int_cst (NULL_TREE, get_symbol_table_index
1072 (fdecl, &TYPE_ATABLE_METHODS (output_class)));
1073 tree field_address
1074 = build4 (ARRAY_REF,
1075 TREE_TYPE (TREE_TYPE (TYPE_ATABLE_DECL (output_class))),
1076 TYPE_ATABLE_DECL (output_class), table_index,
1077 NULL_TREE, NULL_TREE);
1078 field_address = convert (build_pointer_type (TREE_TYPE (fdecl)),
1079 field_address);
1080 return fold (build1 (INDIRECT_REF, TREE_TYPE (fdecl),
1081 field_address));
1082 }
1083 else
1084 {
1085 /* Compile as:
1086 *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr */
1087 tree ref = build_class_ref (fclass);
1088 tree fld;
1089 int field_index = 0;
1090 ref = build1 (INDIRECT_REF, class_type_node, ref);
1091 ref = build3 (COMPONENT_REF, field_ptr_type_node, ref,
1092 lookup_field (&class_type_node, fields_ident),
1093 NULL_TREE);
1094
1095 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
1096 {
1097 if (fld == fdecl)
1098 break;
1099 if (fld == NULL_TREE)
1100 fatal_error ("field '%s' not found in class",
1101 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1102 if (FIELD_STATIC (fld))
1103 field_index++;
1104 }
1105 field_index *= int_size_in_bytes (field_type_node);
1106 ref = fold (build2 (PLUS_EXPR, field_ptr_type_node,
1107 ref, build_int_cst (NULL_TREE, field_index)));
1108 ref = build1 (INDIRECT_REF, field_type_node, ref);
1109 ref = build3 (COMPONENT_REF, field_info_union_node,
1110 ref, lookup_field (&field_type_node, info_ident),
1111 NULL_TREE);
1112 ref = build3 (COMPONENT_REF, ptr_type_node,
1113 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)),
1114 NULL_TREE);
1115 ref = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (fdecl)), ref);
1116 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
1117 }
1118 }
1119
1120 int
1121 get_access_flags_from_decl (tree decl)
1122 {
1123 int access_flags = 0;
1124 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1125 {
1126 if (FIELD_STATIC (decl))
1127 access_flags |= ACC_STATIC;
1128 if (FIELD_PUBLIC (decl))
1129 access_flags |= ACC_PUBLIC;
1130 if (FIELD_PROTECTED (decl))
1131 access_flags |= ACC_PROTECTED;
1132 if (FIELD_PRIVATE (decl))
1133 access_flags |= ACC_PRIVATE;
1134 if (FIELD_FINAL (decl))
1135 access_flags |= ACC_FINAL;
1136 if (FIELD_VOLATILE (decl))
1137 access_flags |= ACC_VOLATILE;
1138 if (FIELD_TRANSIENT (decl))
1139 access_flags |= ACC_TRANSIENT;
1140 return access_flags;
1141 }
1142 if (TREE_CODE (decl) == TYPE_DECL)
1143 {
1144 if (CLASS_PUBLIC (decl))
1145 access_flags |= ACC_PUBLIC;
1146 if (CLASS_FINAL (decl))
1147 access_flags |= ACC_FINAL;
1148 if (CLASS_SUPER (decl))
1149 access_flags |= ACC_SUPER;
1150 if (CLASS_INTERFACE (decl))
1151 access_flags |= ACC_INTERFACE;
1152 if (CLASS_ABSTRACT (decl))
1153 access_flags |= ACC_ABSTRACT;
1154 if (CLASS_STATIC (decl))
1155 access_flags |= ACC_STATIC;
1156 if (CLASS_PRIVATE (decl))
1157 access_flags |= ACC_PRIVATE;
1158 if (CLASS_PROTECTED (decl))
1159 access_flags |= ACC_PROTECTED;
1160 if (CLASS_STRICTFP (decl))
1161 access_flags |= ACC_STRICT;
1162 return access_flags;
1163 }
1164 if (TREE_CODE (decl) == FUNCTION_DECL)
1165 {
1166 if (METHOD_PUBLIC (decl))
1167 access_flags |= ACC_PUBLIC;
1168 if (METHOD_PRIVATE (decl))
1169 access_flags |= ACC_PRIVATE;
1170 if (METHOD_PROTECTED (decl))
1171 access_flags |= ACC_PROTECTED;
1172 if (METHOD_STATIC (decl))
1173 access_flags |= ACC_STATIC;
1174 if (METHOD_FINAL (decl))
1175 access_flags |= ACC_FINAL;
1176 if (METHOD_SYNCHRONIZED (decl))
1177 access_flags |= ACC_SYNCHRONIZED;
1178 if (METHOD_NATIVE (decl))
1179 access_flags |= ACC_NATIVE;
1180 if (METHOD_ABSTRACT (decl))
1181 access_flags |= ACC_ABSTRACT;
1182 if (METHOD_STRICTFP (decl))
1183 access_flags |= ACC_STRICT;
1184 if (METHOD_INVISIBLE (decl))
1185 access_flags |= ACC_INVISIBLE;
1186 return access_flags;
1187 }
1188 abort ();
1189 }
1190
1191 static GTY (()) int alias_labelno = 0;
1192
1193 /* Create a private alias for METHOD. Using this alias instead of the method
1194 decl ensures that ncode entries in the method table point to the real function
1195 at runtime, not a PLT entry. */
1196
1197 static tree
1198 make_local_function_alias (tree method)
1199 {
1200 #ifdef ASM_OUTPUT_DEF
1201 tree alias;
1202
1203 const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1204 char *name = alloca (strlen (method_name) + 2);
1205 char *buf = alloca (strlen (method_name) + 128);
1206
1207 /* Only create aliases for local functions. */
1208 if (DECL_EXTERNAL (method))
1209 return method;
1210
1211 /* Prefix method_name with 'L' for the alias label. */
1212 *name = 'L';
1213 strcpy (name + 1, method_name);
1214
1215 ASM_GENERATE_INTERNAL_LABEL (buf, name, alias_labelno++);
1216 alias = build_decl (FUNCTION_DECL, get_identifier (buf),
1217 TREE_TYPE (method));
1218 DECL_CONTEXT (alias) = NULL;
1219 TREE_READONLY (alias) = TREE_READONLY (method);
1220 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1221 TREE_PUBLIC (alias) = 0;
1222 DECL_EXTERNAL (alias) = 0;
1223 DECL_ARTIFICIAL (alias) = 1;
1224 DECL_INLINE (alias) = 0;
1225 DECL_INITIAL (alias) = error_mark_node;
1226 TREE_ADDRESSABLE (alias) = 1;
1227 TREE_USED (alias) = 1;
1228 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
1229 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
1230 if (!flag_syntax_only)
1231 assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1232 return alias;
1233 #else
1234 return method;
1235 #endif
1236 }
1237
1238 /** Make reflection data (_Jv_Field) for field FDECL. */
1239
1240 static tree
1241 make_field_value (tree fdecl)
1242 {
1243 tree finit;
1244 int flags;
1245 tree type = TREE_TYPE (fdecl);
1246 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1247
1248 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1249 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1250 if (resolved)
1251 type = build_class_ref (type);
1252 else
1253 {
1254 tree signature = build_java_signature (type);
1255
1256 type = build_utf8_ref (unmangle_classname
1257 (IDENTIFIER_POINTER (signature),
1258 IDENTIFIER_LENGTH (signature)));
1259 }
1260 PUSH_FIELD_VALUE (finit, "type", type);
1261
1262 flags = get_access_flags_from_decl (fdecl);
1263 if (! resolved)
1264 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1265
1266 PUSH_FIELD_VALUE (finit, "accflags", build_int_cst (NULL_TREE, flags));
1267 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1268
1269 PUSH_FIELD_VALUE
1270 (finit, "info",
1271 build_constructor (field_info_union_node,
1272 build_tree_list
1273 ((FIELD_STATIC (fdecl)
1274 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1275 : TYPE_FIELDS (field_info_union_node)),
1276 (FIELD_STATIC (fdecl)
1277 ? build_address_of (fdecl)
1278 : byte_position (fdecl)))));
1279
1280 FINISH_RECORD_CONSTRUCTOR (finit);
1281 return finit;
1282 }
1283
1284 /** Make reflection data (_Jv_Method) for method MDECL. */
1285
1286 static tree
1287 make_method_value (tree mdecl)
1288 {
1289 static int method_name_count = 0;
1290 tree minit;
1291 tree index;
1292 tree code;
1293 tree class_decl;
1294 #define ACC_TRANSLATED 0x4000
1295 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1296
1297 class_decl = DECL_CONTEXT (mdecl);
1298 /* For interfaces, the index field contains the dispatch index. */
1299 if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1300 index = build_int_cst (NULL_TREE,
1301 get_interface_method_index (mdecl, class_decl));
1302 else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1303 index = get_method_index (mdecl);
1304 else
1305 index = integer_minus_one_node;
1306
1307 code = null_pointer_node;
1308 if (DECL_RTL_SET_P (mdecl))
1309 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1310 make_local_function_alias (mdecl));
1311 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1312 PUSH_FIELD_VALUE (minit, "name",
1313 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1314 init_identifier_node
1315 : DECL_NAME (mdecl)));
1316 {
1317 tree signature = build_java_signature (TREE_TYPE (mdecl));
1318 PUSH_FIELD_VALUE (minit, "signature",
1319 (build_utf8_ref
1320 (unmangle_classname
1321 (IDENTIFIER_POINTER(signature),
1322 IDENTIFIER_LENGTH(signature)))));
1323 }
1324 PUSH_FIELD_VALUE (minit, "accflags", build_int_cst (NULL_TREE, accflags));
1325 PUSH_FIELD_VALUE (minit, "index", index);
1326 PUSH_FIELD_VALUE (minit, "ncode", code);
1327
1328 {
1329 /* Compute the `throws' information for the method. */
1330 tree table = null_pointer_node;
1331 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1332 {
1333 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1334 tree iter, type, array;
1335 char buf[60];
1336
1337 table = tree_cons (NULL_TREE, table, NULL_TREE);
1338 for (iter = DECL_FUNCTION_THROWS (mdecl);
1339 iter != NULL_TREE;
1340 iter = TREE_CHAIN (iter))
1341 {
1342 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1343 tree utf8
1344 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1345 IDENTIFIER_LENGTH (sig)));
1346 table = tree_cons (NULL_TREE, utf8, table);
1347 }
1348 type = build_prim_array_type (ptr_type_node, length);
1349 table = build_constructor (type, table);
1350 /* Compute something unique enough. */
1351 sprintf (buf, "_methods%d", method_name_count++);
1352 array = build_decl (VAR_DECL, get_identifier (buf), type);
1353 DECL_INITIAL (array) = table;
1354 TREE_STATIC (array) = 1;
1355 DECL_ARTIFICIAL (array) = 1;
1356 DECL_IGNORED_P (array) = 1;
1357 rest_of_decl_compilation (array, 1, 0);
1358
1359 table = build1 (ADDR_EXPR, ptr_type_node, array);
1360 }
1361
1362 PUSH_FIELD_VALUE (minit, "throws", table);
1363 }
1364
1365 FINISH_RECORD_CONSTRUCTOR (minit);
1366 return minit;
1367 }
1368
1369 static tree
1370 get_dispatch_vector (tree type)
1371 {
1372 tree vtable = TYPE_VTABLE (type);
1373
1374 if (vtable == NULL_TREE)
1375 {
1376 HOST_WIDE_INT i;
1377 tree method;
1378 tree super = CLASSTYPE_SUPER (type);
1379 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1380 vtable = make_tree_vec (nvirtuals);
1381 TYPE_VTABLE (type) = vtable;
1382 if (super != NULL_TREE)
1383 {
1384 tree super_vtable = get_dispatch_vector (super);
1385
1386 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1387 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1388 }
1389
1390 for (method = TYPE_METHODS (type); method != NULL_TREE;
1391 method = TREE_CHAIN (method))
1392 {
1393 tree method_index = get_method_index (method);
1394 if (method_index != NULL_TREE
1395 && host_integerp (method_index, 0))
1396 TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1397 }
1398 }
1399
1400 return vtable;
1401 }
1402
1403 static tree
1404 get_dispatch_table (tree type, tree this_class_addr)
1405 {
1406 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1407 tree vtable = get_dispatch_vector (type);
1408 int i, j;
1409 tree list = NULL_TREE;
1410 int nvirtuals = TREE_VEC_LENGTH (vtable);
1411 int arraysize;
1412 tree gc_descr;
1413
1414 for (i = nvirtuals; --i >= 0; )
1415 {
1416 tree method = TREE_VEC_ELT (vtable, i);
1417 if (METHOD_ABSTRACT (method))
1418 {
1419 if (! abstract_p)
1420 warning ("%Jabstract method in non-abstract class", method);
1421
1422 if (TARGET_VTABLE_USES_DESCRIPTORS)
1423 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1424 list = tree_cons (NULL_TREE, null_pointer_node, list);
1425 else
1426 list = tree_cons (NULL_TREE, null_pointer_node, list);
1427 }
1428 else
1429 {
1430 if (!DECL_RTL_SET_P (method))
1431 make_decl_rtl (method);
1432
1433 if (TARGET_VTABLE_USES_DESCRIPTORS)
1434 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1435 {
1436 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1437 method, build_int_cst (NULL_TREE, j));
1438 TREE_CONSTANT (fdesc) = 1;
1439 TREE_INVARIANT (fdesc) = 1;
1440 list = tree_cons (NULL_TREE, fdesc, list);
1441 }
1442 else
1443 list = tree_cons (NULL_TREE,
1444 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1445 method),
1446 list);
1447 }
1448 }
1449
1450 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1451 using the Boehm GC we sometimes stash a GC type descriptor
1452 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1453 the emitted byte count during the output to the assembly file. */
1454 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1455 fake "function descriptor". It's first word is the is the class
1456 pointer, and subsequent words (usually one) contain the GC descriptor.
1457 In all other cases, we reserve two extra vtable slots. */
1458 gc_descr = get_boehm_type_descriptor (type);
1459 list = tree_cons (NULL_TREE, gc_descr, list);
1460 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1461 list = tree_cons (NULL_TREE, gc_descr, list);
1462 list = tree_cons (NULL_TREE, this_class_addr, list);
1463
1464 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1465 list = tree_cons (NULL_TREE, null_pointer_node, list);
1466 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1467 list = tree_cons (integer_zero_node, null_pointer_node, list);
1468
1469 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1470 if (TARGET_VTABLE_USES_DESCRIPTORS)
1471 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1472 arraysize += 2;
1473 return build_constructor (build_prim_array_type (nativecode_ptr_type_node,
1474 arraysize), list);
1475 }
1476
1477
1478 /* Set the method_index for a method decl. */
1479 void
1480 set_method_index (tree decl, tree method_index)
1481 {
1482 if (method_index != NULL_TREE)
1483 {
1484 /* method_index is null if we're using indirect dispatch. */
1485 method_index = fold (convert (sizetype, method_index));
1486
1487 if (TARGET_VTABLE_USES_DESCRIPTORS)
1488 /* Add one to skip bogus descriptor for class and GC descriptor. */
1489 method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1490 else
1491 /* Add 1 to skip "class" field of dtable, and 1 to skip GC
1492 descriptor. */
1493 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1494 }
1495
1496 DECL_VINDEX (decl) = method_index;
1497 }
1498
1499 /* Get the method_index for a method decl. */
1500 tree
1501 get_method_index (tree decl)
1502 {
1503 tree method_index = DECL_VINDEX (decl);
1504
1505 if (! method_index)
1506 return NULL;
1507
1508 if (TARGET_VTABLE_USES_DESCRIPTORS)
1509 /* Sub one to skip bogus descriptor for class and GC descriptor. */
1510 method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1511 else
1512 /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1513 method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1514
1515 return method_index;
1516 }
1517
1518 static int
1519 supers_all_compiled (tree type)
1520 {
1521 while (type != NULL_TREE)
1522 {
1523 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1524 return 0;
1525 type = CLASSTYPE_SUPER (type);
1526 }
1527 return 1;
1528 }
1529
1530 void
1531 make_class_data (tree type)
1532 {
1533 tree decl, cons, temp;
1534 tree field, fields_decl;
1535 tree static_fields = NULL_TREE;
1536 tree instance_fields = NULL_TREE;
1537 HOST_WIDE_INT static_field_count = 0;
1538 HOST_WIDE_INT instance_field_count = 0;
1539 HOST_WIDE_INT field_count;
1540 tree field_array_type;
1541 tree method;
1542 tree methods = NULL_TREE;
1543 tree dtable_decl = NULL_TREE;
1544 HOST_WIDE_INT method_count = 0;
1545 tree method_array_type;
1546 tree methods_decl;
1547 tree super;
1548 tree this_class_addr;
1549 tree constant_pool_constructor;
1550 tree interfaces = null_pointer_node;
1551 int interface_len = 0;
1552 tree type_decl = TYPE_NAME (type);
1553 /** Offset from start of virtual function table declaration
1554 to where objects actually point at, following new g++ ABI. */
1555 tree dtable_start_offset = build_int_cst (NULL_TREE,
1556 2 * POINTER_SIZE / BITS_PER_UNIT);
1557
1558 this_class_addr = build_class_ref (type);
1559 decl = TREE_OPERAND (this_class_addr, 0);
1560
1561 /* Build Field array. */
1562 field = TYPE_FIELDS (type);
1563 while (field && DECL_ARTIFICIAL (field))
1564 field = TREE_CHAIN (field); /* Skip dummy fields. */
1565 if (field && DECL_NAME (field) == NULL_TREE)
1566 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1567 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1568 {
1569 if (! DECL_ARTIFICIAL (field))
1570 {
1571 tree init = make_field_value (field);
1572 if (FIELD_STATIC (field))
1573 {
1574 tree initial = DECL_INITIAL (field);
1575 static_field_count++;
1576 static_fields = tree_cons (NULL_TREE, init, static_fields);
1577 /* If the initial value is a string constant,
1578 prevent output_constant from trying to assemble the value. */
1579 if (initial != NULL_TREE
1580 && TREE_TYPE (initial) == string_ptr_type_node)
1581 DECL_INITIAL (field) = NULL_TREE;
1582 rest_of_decl_compilation (field, 1, 1);
1583 DECL_INITIAL (field) = initial;
1584 }
1585 else
1586 {
1587 instance_field_count++;
1588 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1589 }
1590 }
1591 }
1592 field_count = static_field_count + instance_field_count;
1593 if (field_count > 0)
1594 {
1595 static_fields = nreverse (static_fields);
1596 instance_fields = nreverse (instance_fields);
1597 static_fields = chainon (static_fields, instance_fields);
1598 field_array_type = build_prim_array_type (field_type_node, field_count);
1599 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1600 field_array_type);
1601 DECL_INITIAL (fields_decl) = build_constructor (field_array_type,
1602 static_fields);
1603 TREE_STATIC (fields_decl) = 1;
1604 DECL_ARTIFICIAL (fields_decl) = 1;
1605 DECL_IGNORED_P (fields_decl) = 1;
1606 rest_of_decl_compilation (fields_decl, 1, 0);
1607 }
1608 else
1609 fields_decl = NULL_TREE;
1610
1611 /* Build Method array. */
1612 for (method = TYPE_METHODS (type);
1613 method != NULL_TREE; method = TREE_CHAIN (method))
1614 {
1615 tree init;
1616 if (METHOD_PRIVATE (method)
1617 && ! flag_keep_inline_functions
1618 && optimize)
1619 continue;
1620 /* Even if we have a decl, we don't necessarily have the code.
1621 This can happen if we inherit a method from a superclass for
1622 which we don't have a .class file. */
1623 if (METHOD_DUMMY (method))
1624 continue;
1625 init = make_method_value (method);
1626 method_count++;
1627 methods = tree_cons (NULL_TREE, init, methods);
1628 }
1629 method_array_type = build_prim_array_type (method_type_node, method_count);
1630 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1631 method_array_type);
1632 DECL_INITIAL (methods_decl) = build_constructor (method_array_type,
1633 nreverse (methods));
1634 TREE_STATIC (methods_decl) = 1;
1635 DECL_ARTIFICIAL (methods_decl) = 1;
1636 DECL_IGNORED_P (methods_decl) = 1;
1637 rest_of_decl_compilation (methods_decl, 1, 0);
1638
1639 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1640 && !flag_indirect_dispatch)
1641 {
1642 tree dtable = get_dispatch_table (type, this_class_addr);
1643 dtable_decl = build_dtable_decl (type);
1644 DECL_INITIAL (dtable_decl) = dtable;
1645 TREE_STATIC (dtable_decl) = 1;
1646 DECL_ARTIFICIAL (dtable_decl) = 1;
1647 DECL_IGNORED_P (dtable_decl) = 1;
1648 TREE_PUBLIC (dtable_decl) = 1;
1649 rest_of_decl_compilation (dtable_decl, 1, 0);
1650 if (type == class_type_node)
1651 class_dtable_decl = dtable_decl;
1652 }
1653
1654 if (class_dtable_decl == NULL_TREE)
1655 {
1656 class_dtable_decl = build_dtable_decl (class_type_node);
1657 TREE_STATIC (class_dtable_decl) = 1;
1658 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1659 DECL_IGNORED_P (class_dtable_decl) = 1;
1660 if (is_compiled_class (class_type_node) != 2)
1661 DECL_EXTERNAL (class_dtable_decl) = 1;
1662 rest_of_decl_compilation (class_dtable_decl, 1, 0);
1663 }
1664
1665 super = CLASSTYPE_SUPER (type);
1666 if (super == NULL_TREE)
1667 super = null_pointer_node;
1668 else if (! flag_indirect_dispatch
1669 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1670 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1671 super = build_class_ref (super);
1672 else
1673 {
1674 int super_index = alloc_class_constant (super);
1675 super = build_int_cst (ptr_type_node, super_index);
1676 }
1677
1678 /* Build and emit the array of implemented interfaces. */
1679 if (type != object_type_node)
1680 interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
1681
1682 if (interface_len > 0)
1683 {
1684 tree init = NULL_TREE;
1685 int i;
1686 tree interface_array_type, idecl;
1687 interface_array_type
1688 = build_prim_array_type (class_ptr_type, interface_len);
1689 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1690 interface_array_type);
1691
1692 for (i = interface_len; i > 0; i--)
1693 {
1694 tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1695 tree iclass = BINFO_TYPE (child);
1696 tree index;
1697 if (! flag_indirect_dispatch
1698 && (assume_compiled
1699 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1700 index = build_class_ref (iclass);
1701 else
1702 {
1703 int int_index = alloc_class_constant (iclass);
1704 index = build_int_cst (ptr_type_node, int_index);
1705 }
1706 init = tree_cons (NULL_TREE, index, init);
1707 }
1708 DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
1709 TREE_STATIC (idecl) = 1;
1710 DECL_ARTIFICIAL (idecl) = 1;
1711 DECL_IGNORED_P (idecl) = 1;
1712 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1713 rest_of_decl_compilation (idecl, 1, 0);
1714 }
1715
1716 constant_pool_constructor = build_constants_constructor ();
1717
1718 if (flag_indirect_dispatch)
1719 {
1720 TYPE_OTABLE_DECL (type)
1721 = emit_symbol_table
1722 (DECL_NAME (TYPE_OTABLE_DECL (type)),
1723 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
1724 TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
1725
1726 TYPE_ATABLE_DECL (type)
1727 = emit_symbol_table
1728 (DECL_NAME (TYPE_ATABLE_DECL (type)),
1729 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
1730 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
1731
1732 TYPE_ITABLE_DECL (type)
1733 = emit_symbol_table
1734 (DECL_NAME (TYPE_ITABLE_DECL (type)),
1735 TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
1736 TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
1737 }
1738
1739 TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1740
1741 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1742 PUSH_FIELD_VALUE (temp, "vtable",
1743 build2 (PLUS_EXPR, dtable_ptr_type,
1744 build1 (ADDR_EXPR, dtable_ptr_type,
1745 class_dtable_decl),
1746 dtable_start_offset));
1747 if (! flag_hash_synchronization)
1748 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1749 FINISH_RECORD_CONSTRUCTOR (temp);
1750 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1751 PUSH_SUPER_VALUE (cons, temp);
1752 PUSH_FIELD_VALUE (cons, "next_or_version", gcj_abi_version);
1753 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1754 PUSH_FIELD_VALUE (cons, "accflags",
1755 build_int_cst (NULL_TREE,
1756 get_access_flags_from_decl (type_decl)));
1757
1758 PUSH_FIELD_VALUE (cons, "superclass",
1759 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1760 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1761 PUSH_FIELD_VALUE (cons, "methods",
1762 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1763 PUSH_FIELD_VALUE (cons, "method_count",
1764 build_int_cst (NULL_TREE, method_count));
1765
1766 if (flag_indirect_dispatch)
1767 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1768 else
1769 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1770
1771 PUSH_FIELD_VALUE (cons, "fields",
1772 fields_decl == NULL_TREE ? null_pointer_node
1773 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1774 /* If we're using the binary compatibility ABI we don't know the
1775 size until load time. */
1776 PUSH_FIELD_VALUE (cons, "size_in_bytes",
1777 (flag_indirect_dispatch
1778 ? integer_minus_one_node
1779 : size_in_bytes (type)));
1780 PUSH_FIELD_VALUE (cons, "field_count",
1781 build_int_cst (NULL_TREE, field_count));
1782 PUSH_FIELD_VALUE (cons, "static_field_count",
1783 build_int_cst (NULL_TREE, static_field_count));
1784
1785 if (flag_indirect_dispatch)
1786 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1787 else
1788 PUSH_FIELD_VALUE (cons, "vtable",
1789 dtable_decl == NULL_TREE ? null_pointer_node
1790 : build2 (PLUS_EXPR, dtable_ptr_type,
1791 build1 (ADDR_EXPR, dtable_ptr_type,
1792 dtable_decl),
1793 dtable_start_offset));
1794 if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1795 {
1796 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1797 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1798 }
1799 else
1800 {
1801 PUSH_FIELD_VALUE (cons, "otable",
1802 build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1803 PUSH_FIELD_VALUE (cons, "otable_syms",
1804 build1 (ADDR_EXPR, symbols_array_ptr_type,
1805 TYPE_OTABLE_SYMS_DECL (type)));
1806 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1807 TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1808 }
1809 if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1810 {
1811 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1812 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1813 }
1814 else
1815 {
1816 PUSH_FIELD_VALUE (cons, "atable",
1817 build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1818 PUSH_FIELD_VALUE (cons, "atable_syms",
1819 build1 (ADDR_EXPR, symbols_array_ptr_type,
1820 TYPE_ATABLE_SYMS_DECL (type)));
1821 TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1822 TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1823 }
1824 if (TYPE_ITABLE_METHODS(type) == NULL_TREE)
1825 {
1826 PUSH_FIELD_VALUE (cons, "itable", null_pointer_node);
1827 PUSH_FIELD_VALUE (cons, "itable_syms", null_pointer_node);
1828 }
1829 else
1830 {
1831 PUSH_FIELD_VALUE (cons, "itable",
1832 build1 (ADDR_EXPR, itable_ptr_type, TYPE_ITABLE_DECL (type)));
1833 PUSH_FIELD_VALUE (cons, "itable_syms",
1834 build1 (ADDR_EXPR, symbols_array_ptr_type,
1835 TYPE_ITABLE_SYMS_DECL (type)));
1836 TREE_CONSTANT (TYPE_ITABLE_DECL (type)) = 1;
1837 TREE_INVARIANT (TYPE_ITABLE_DECL (type)) = 1;
1838 }
1839
1840 PUSH_FIELD_VALUE (cons, "catch_classes",
1841 build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
1842 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1843 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1844 PUSH_FIELD_VALUE (cons, "interface_count",
1845 build_int_cst (NULL_TREE, interface_len));
1846 PUSH_FIELD_VALUE
1847 (cons, "state",
1848 convert (byte_type_node,
1849 build_int_cst (NULL_TREE,
1850 flag_indirect_dispatch
1851 ? JV_STATE_PRELOADING
1852 : JV_STATE_COMPILED)));
1853
1854 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1855 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1856 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1857 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1858 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1859 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1860
1861 {
1862 tree assertion_table_ref;
1863 if (TYPE_ASSERTIONS (type) == NULL)
1864 assertion_table_ref = null_pointer_node;
1865 else
1866 assertion_table_ref = build1 (ADDR_EXPR,
1867 build_pointer_type (assertion_table_type),
1868 emit_assertion_table (type));
1869
1870 PUSH_FIELD_VALUE (cons, "assertion_table", assertion_table_ref);
1871 }
1872
1873 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1874 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1875 PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1876 PUSH_FIELD_VALUE (cons, "engine", null_pointer_node);
1877
1878 FINISH_RECORD_CONSTRUCTOR (cons);
1879
1880 DECL_INITIAL (decl) = cons;
1881
1882 /* Hash synchronization requires at least 64-bit alignment. */
1883 if (flag_hash_synchronization && POINTER_SIZE < 64)
1884 DECL_ALIGN (decl) = 64;
1885
1886 rest_of_decl_compilation (decl, 1, 0);
1887
1888 TYPE_OTABLE_DECL (type) = NULL_TREE;
1889 TYPE_ATABLE_DECL (type) = NULL_TREE;
1890 TYPE_CTABLE_DECL (type) = NULL_TREE;
1891 }
1892
1893 void
1894 finish_class (void)
1895 {
1896 if (TYPE_VERIFY_METHOD (output_class))
1897 {
1898 tree verify_method = TYPE_VERIFY_METHOD (output_class);
1899 DECL_SAVED_TREE (verify_method)
1900 = add_stmt_to_compound (DECL_SAVED_TREE (verify_method), void_type_node,
1901 build (RETURN_EXPR, void_type_node, NULL));
1902 java_genericize (verify_method);
1903 cgraph_finalize_function (verify_method, false);
1904 TYPE_ASSERTIONS (current_class) = NULL;
1905 }
1906
1907 java_expand_catch_classes (current_class);
1908
1909 current_function_decl = NULL_TREE;
1910 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (current_class)) = 0;
1911 make_class_data (current_class);
1912 register_class ();
1913 rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
1914 }
1915
1916 /* Return 2 if CLASS is compiled by this compilation job;
1917 return 1 if CLASS can otherwise be assumed to be compiled;
1918 return 0 if we cannot assume that CLASS is compiled.
1919 Returns 1 for primitive and 0 for array types. */
1920 int
1921 is_compiled_class (tree class)
1922 {
1923 int seen_in_zip;
1924 if (TREE_CODE (class) == POINTER_TYPE)
1925 class = TREE_TYPE (class);
1926 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1927 return 1;
1928 if (TYPE_ARRAY_P (class))
1929 return 0;
1930 if (class == current_class)
1931 return 2;
1932
1933 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1934 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1935 {
1936 /* The class was seen in the current ZIP file and will be
1937 available as a compiled class in the future but may not have
1938 been loaded already. Load it if necessary. This prevent
1939 build_class_ref () from crashing. */
1940
1941 if (seen_in_zip && !CLASS_LOADED_P (class))
1942 load_class (class, 1);
1943
1944 /* We return 2 for class seen in ZIP and class from files
1945 belonging to the same compilation unit */
1946 return 2;
1947 }
1948
1949 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1950 {
1951 if (!CLASS_LOADED_P (class))
1952 {
1953 if (CLASS_FROM_SOURCE_P (class))
1954 safe_layout_class (class);
1955 else
1956 load_class (class, 1);
1957 }
1958 return 1;
1959 }
1960
1961 return 0;
1962 }
1963
1964 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1965
1966 tree
1967 build_dtable_decl (tree type)
1968 {
1969 tree dtype;
1970
1971 /* We need to build a new dtable type so that its size is uniquely
1972 computed when we're dealing with the class for real and not just
1973 faking it (like java.lang.Class during the initialization of the
1974 compiler.) We know we're not faking a class when CURRENT_CLASS is
1975 TYPE. */
1976 if (current_class == type)
1977 {
1978 tree dummy = NULL_TREE;
1979 int n;
1980
1981 dtype = make_node (RECORD_TYPE);
1982
1983 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1984 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1985
1986 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
1987 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1988 {
1989 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
1990 TREE_CHAIN (dummy) = tmp_field;
1991 DECL_CONTEXT (tmp_field) = dtype;
1992 DECL_ARTIFICIAL (tmp_field) = 1;
1993 dummy = tmp_field;
1994 }
1995
1996 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
1997 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
1998 {
1999 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2000 TREE_CHAIN (dummy) = tmp_field;
2001 DECL_CONTEXT (tmp_field) = dtype;
2002 DECL_ARTIFICIAL (tmp_field) = 1;
2003 dummy = tmp_field;
2004 }
2005
2006 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
2007 if (TARGET_VTABLE_USES_DESCRIPTORS)
2008 n *= TARGET_VTABLE_USES_DESCRIPTORS;
2009
2010 PUSH_FIELD (dtype, dummy, "methods",
2011 build_prim_array_type (nativecode_ptr_type_node, n));
2012 layout_type (dtype);
2013 }
2014 else
2015 dtype = dtable_type;
2016
2017 return build_decl (VAR_DECL,
2018 java_mangle_vtable (&temporary_obstack, type), dtype);
2019 }
2020
2021 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2022 fields inherited from SUPER_CLASS. */
2023
2024 void
2025 push_super_field (tree this_class, tree super_class)
2026 {
2027 tree base_decl;
2028 /* Don't insert the field if we're just re-laying the class out. */
2029 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2030 return;
2031 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
2032 DECL_IGNORED_P (base_decl) = 1;
2033 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2034 TYPE_FIELDS (this_class) = base_decl;
2035 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2036 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2037 }
2038
2039 /* Handle the different manners we may have to lay out a super class. */
2040
2041 static tree
2042 maybe_layout_super_class (tree super_class, tree this_class)
2043 {
2044 if (TREE_CODE (super_class) == RECORD_TYPE)
2045 {
2046 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
2047 safe_layout_class (super_class);
2048 if (!CLASS_LOADED_P (super_class))
2049 load_class (super_class, 1);
2050 }
2051 /* We might have to layout the class before its dependency on
2052 the super class gets resolved by java_complete_class */
2053 else if (TREE_CODE (super_class) == POINTER_TYPE)
2054 {
2055 if (TREE_TYPE (super_class) != NULL_TREE)
2056 super_class = TREE_TYPE (super_class);
2057 else
2058 {
2059 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
2060 we give it one. */
2061 tree this_wrap = NULL_TREE;
2062
2063 if (this_class)
2064 {
2065 tree this_decl = TYPE_NAME (this_class);
2066 #ifdef USE_MAPPED_LOCATION
2067 this_wrap = build_expr_wfl (this_class,
2068 DECL_SOURCE_LOCATION (this_decl));
2069 #else
2070 this_wrap = build_expr_wfl (this_class,
2071 DECL_SOURCE_FILE (this_decl),
2072 DECL_SOURCE_LINE (this_decl), 0);
2073 #endif
2074 }
2075 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
2076 super_class, NULL_TREE, this_wrap);
2077 if (!super_class)
2078 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
2079 super_class = TREE_TYPE (super_class);
2080 }
2081 }
2082 if (!TYPE_SIZE (super_class))
2083 safe_layout_class (super_class);
2084
2085 return super_class;
2086 }
2087
2088 void
2089 layout_class (tree this_class)
2090 {
2091 tree super_class = CLASSTYPE_SUPER (this_class);
2092 tree field;
2093
2094 class_list = tree_cons (this_class, NULL_TREE, class_list);
2095 if (CLASS_BEING_LAIDOUT (this_class))
2096 {
2097 char buffer [1024];
2098 char *report;
2099 tree current;
2100
2101 sprintf (buffer, " with '%s'",
2102 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2103 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2104
2105 for (current = TREE_CHAIN (class_list); current;
2106 current = TREE_CHAIN (current))
2107 {
2108 tree decl = TYPE_NAME (TREE_PURPOSE (current));
2109 sprintf (buffer, "\n which inherits from '%s' (%s:%d)",
2110 IDENTIFIER_POINTER (DECL_NAME (decl)),
2111 DECL_SOURCE_FILE (decl),
2112 DECL_SOURCE_LINE (decl));
2113 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2114 }
2115 obstack_1grow (&temporary_obstack, '\0');
2116 report = obstack_finish (&temporary_obstack);
2117 cyclic_inheritance_report = ggc_strdup (report);
2118 obstack_free (&temporary_obstack, report);
2119 TYPE_SIZE (this_class) = error_mark_node;
2120 return;
2121 }
2122 CLASS_BEING_LAIDOUT (this_class) = 1;
2123
2124 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2125 {
2126 tree maybe_super_class
2127 = maybe_layout_super_class (super_class, this_class);
2128 if (maybe_super_class == NULL
2129 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2130 {
2131 TYPE_SIZE (this_class) = error_mark_node;
2132 CLASS_BEING_LAIDOUT (this_class) = 0;
2133 class_list = TREE_CHAIN (class_list);
2134 return;
2135 }
2136 if (TYPE_SIZE (this_class) == NULL_TREE)
2137 push_super_field (this_class, maybe_super_class);
2138 }
2139
2140 for (field = TYPE_FIELDS (this_class);
2141 field != NULL_TREE; field = TREE_CHAIN (field))
2142 {
2143 if (FIELD_STATIC (field))
2144 {
2145 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
2146 SET_DECL_ASSEMBLER_NAME (field,
2147 java_mangle_decl
2148 (&temporary_obstack, field));
2149 }
2150 }
2151
2152 layout_type (this_class);
2153
2154 /* Also recursively load/layout any superinterfaces, but only if
2155 class was loaded from bytecode. The source parser will take care
2156 of this itself. */
2157 if (!CLASS_FROM_SOURCE_P (this_class))
2158 {
2159 int i;
2160 if (TYPE_BINFO (this_class))
2161 {
2162 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2163 {
2164 tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2165 tree super_interface = BINFO_TYPE (binfo);
2166 tree maybe_super_interface
2167 = maybe_layout_super_class (super_interface, NULL_TREE);
2168 if (maybe_super_interface == NULL
2169 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2170 {
2171 TYPE_SIZE (this_class) = error_mark_node;
2172 CLASS_BEING_LAIDOUT (this_class) = 0;
2173 class_list = TREE_CHAIN (class_list);
2174 return;
2175 }
2176 }
2177 }
2178 }
2179
2180 /* Convert the size back to an SI integer value. */
2181 TYPE_SIZE_UNIT (this_class) =
2182 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2183
2184 CLASS_BEING_LAIDOUT (this_class) = 0;
2185 class_list = TREE_CHAIN (class_list);
2186 }
2187
2188 static void
2189 add_miranda_methods (tree base_class, tree search_class)
2190 {
2191 int i;
2192 tree binfo, base_binfo;
2193
2194 if (!CLASS_PARSED_P (search_class))
2195 load_class (search_class, 1);
2196
2197 for (binfo = TYPE_BINFO (search_class), i = 1;
2198 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2199 {
2200 tree method_decl;
2201 tree elt = BINFO_TYPE (base_binfo);
2202
2203 /* FIXME: This is totally bogus. We should not be handling
2204 Miranda methods at all if we're using the BC ABI. */
2205 if (TYPE_DUMMY (elt))
2206 continue;
2207
2208 /* Ensure that interface methods are seen in declared order. */
2209 if (!CLASS_LOADED_P (elt))
2210 load_class (elt, 1);
2211 layout_class_methods (elt);
2212
2213 /* All base classes will have been laid out at this point, so the order
2214 will be correct. This code must match similar layout code in the
2215 runtime. */
2216 for (method_decl = TYPE_METHODS (elt);
2217 method_decl; method_decl = TREE_CHAIN (method_decl))
2218 {
2219 tree sig, override;
2220
2221 /* An interface can have <clinit>. */
2222 if (ID_CLINIT_P (DECL_NAME (method_decl)))
2223 continue;
2224
2225 sig = build_java_argument_signature (TREE_TYPE (method_decl));
2226 override = lookup_argument_method (base_class,
2227 DECL_NAME (method_decl), sig);
2228 if (override == NULL_TREE)
2229 {
2230 /* Found a Miranda method. Add it. */
2231 tree new_method;
2232 sig = build_java_signature (TREE_TYPE (method_decl));
2233 new_method
2234 = add_method (base_class,
2235 get_access_flags_from_decl (method_decl),
2236 DECL_NAME (method_decl), sig);
2237 METHOD_INVISIBLE (new_method) = 1;
2238 }
2239 }
2240
2241 /* Try superinterfaces. */
2242 add_miranda_methods (base_class, elt);
2243 }
2244 }
2245
2246 void
2247 layout_class_methods (tree this_class)
2248 {
2249 tree method_decl, dtable_count;
2250 tree super_class, type_name;
2251
2252 if (TYPE_NVIRTUALS (this_class))
2253 return;
2254
2255 super_class = CLASSTYPE_SUPER (this_class);
2256
2257 if (super_class)
2258 {
2259 super_class = maybe_layout_super_class (super_class, this_class);
2260 if (!TYPE_NVIRTUALS (super_class))
2261 layout_class_methods (super_class);
2262 dtable_count = TYPE_NVIRTUALS (super_class);
2263 }
2264 else
2265 dtable_count = integer_zero_node;
2266
2267 type_name = TYPE_NAME (this_class);
2268 if (!flag_indirect_dispatch
2269 && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2270 {
2271 /* An abstract class can have methods which are declared only in
2272 an implemented interface. These are called "Miranda
2273 methods". We make a dummy method entry for such methods
2274 here. */
2275 add_miranda_methods (this_class, this_class);
2276 }
2277
2278 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2279
2280 for (method_decl = TYPE_METHODS (this_class);
2281 method_decl; method_decl = TREE_CHAIN (method_decl))
2282 dtable_count = layout_class_method (this_class, super_class,
2283 method_decl, dtable_count);
2284
2285 TYPE_NVIRTUALS (this_class) = dtable_count;
2286 }
2287
2288 /* Return the index of METHOD in INTERFACE. This index begins at 1 and is used as an
2289 argument for _Jv_LookupInterfaceMethodIdx(). */
2290 int
2291 get_interface_method_index (tree method, tree interface)
2292 {
2293 tree meth;
2294 int i = 1;
2295
2296 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
2297 {
2298 if (meth == method)
2299 return i;
2300 if (meth == NULL_TREE)
2301 abort ();
2302 }
2303 }
2304
2305 /* Lay METHOD_DECL out, returning a possibly new value of
2306 DTABLE_COUNT. Also mangle the method's name. */
2307
2308 tree
2309 layout_class_method (tree this_class, tree super_class,
2310 tree method_decl, tree dtable_count)
2311 {
2312 tree method_name = DECL_NAME (method_decl);
2313
2314 TREE_PUBLIC (method_decl) = 1;
2315 /* Considered external until we know what classes are being
2316 compiled into this object file. */
2317 DECL_EXTERNAL (method_decl) = 1;
2318
2319 /* This is a good occasion to mangle the method's name */
2320 SET_DECL_ASSEMBLER_NAME (method_decl,
2321 java_mangle_decl (&temporary_obstack,
2322 method_decl));
2323 /* We don't generate a RTL for the method if it's abstract, or if
2324 it's an interface method that isn't clinit. */
2325 if (! METHOD_ABSTRACT (method_decl)
2326 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2327 && (DECL_CLINIT_P (method_decl))))
2328 make_decl_rtl (method_decl);
2329
2330 if (ID_INIT_P (method_name))
2331 {
2332 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2333 const char *ptr;
2334 for (ptr = p; *ptr; )
2335 {
2336 if (*ptr++ == '.')
2337 p = ptr;
2338 }
2339 DECL_CONSTRUCTOR_P (method_decl) = 1;
2340 build_java_argument_signature (TREE_TYPE (method_decl));
2341 }
2342 else if (! METHOD_STATIC (method_decl))
2343 {
2344 tree method_sig =
2345 build_java_argument_signature (TREE_TYPE (method_decl));
2346 bool method_override = false;
2347 tree super_method = lookup_argument_method (super_class, method_name,
2348 method_sig);
2349 if (super_method != NULL_TREE
2350 && ! METHOD_DUMMY (super_method))
2351 {
2352 method_override = true;
2353 if (! METHOD_PUBLIC (super_method) &&
2354 ! METHOD_PROTECTED (super_method))
2355 {
2356 /* Don't override private method, or default-access method in
2357 another package. */
2358 if (METHOD_PRIVATE (super_method) ||
2359 ! in_same_package (TYPE_NAME (this_class),
2360 TYPE_NAME (super_class)))
2361 method_override = false;
2362 }
2363 }
2364 if (method_override)
2365 {
2366 tree method_index = get_method_index (super_method);
2367 set_method_index (method_decl, method_index);
2368 if (method_index == NULL_TREE
2369 && ! flag_indirect_dispatch
2370 && !CLASS_FROM_SOURCE_P (this_class)
2371 && ! DECL_ARTIFICIAL (super_method))
2372 error ("%Jnon-static method '%D' overrides static method",
2373 method_decl, method_decl);
2374 }
2375 else if (this_class == object_type_node
2376 && (METHOD_FINAL (method_decl)
2377 || METHOD_PRIVATE (method_decl)))
2378 {
2379 /* We don't generate vtable entries for final Object
2380 methods. This is simply to save space, since every
2381 object would otherwise have to define them. */
2382 }
2383 else if (! METHOD_PRIVATE (method_decl)
2384 && dtable_count)
2385 {
2386 /* We generate vtable entries for final methods because they
2387 may one day be changed to non-final. */
2388 set_method_index (method_decl, dtable_count);
2389 dtable_count = fold (build2 (PLUS_EXPR, integer_type_node,
2390 dtable_count, integer_one_node));
2391 }
2392 }
2393
2394 return dtable_count;
2395 }
2396
2397 void
2398 register_class (void)
2399 {
2400 /* END does not need to be registered with the garbage collector
2401 because it always points into the list given by REGISTERED_CLASS,
2402 and that variable is registered with the collector. */
2403 static tree end;
2404 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2405 tree current = copy_node (node);
2406
2407 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2408 if (!registered_class)
2409 registered_class = current;
2410 else
2411 TREE_CHAIN (end) = current;
2412
2413 end = current;
2414 }
2415
2416 /* Emit something to register classes at start-up time.
2417
2418 The preferred mechanism is through the .jcr section, which contain
2419 a list of pointers to classes which get registered during constructor
2420 invocation time.
2421
2422 The fallback mechanism is to add statements to *LIST_P to call
2423 _Jv_RegisterClass for each class in this file. These statements will
2424 be added to a static constructor function for this translation unit. */
2425
2426 void
2427 emit_register_classes (tree *list_p)
2428 {
2429 if (registered_class == NULL)
2430 return;
2431
2432 /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2433 TARGET_ASM_NAMED_SECTION, else 0. Some targets meet those conditions
2434 but lack suitable crtbegin/end objects or linker support. These
2435 targets can overide the default in tm.h to use the fallback mechanism. */
2436 if (TARGET_USE_JCR_SECTION)
2437 {
2438 #ifdef JCR_SECTION_NAME
2439 tree t;
2440 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2441 assemble_align (POINTER_SIZE);
2442 for (t = registered_class; t; t = TREE_CHAIN (t))
2443 {
2444 mark_decl_referenced (t);
2445 assemble_integer (XEXP (DECL_RTL (t), 0),
2446 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2447 }
2448 #else
2449 /* A target has defined TARGET_USE_JCR_SECTION, but doesn't have a
2450 JCR_SECTION_NAME. */
2451 abort ();
2452 #endif
2453 }
2454 else
2455 {
2456 tree klass, t, register_class_fn;
2457
2458 t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2459 t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2460 TREE_PUBLIC (t) = 1;
2461 DECL_EXTERNAL (t) = 1;
2462 register_class_fn = t;
2463
2464 for (klass = registered_class; klass; klass = TREE_CHAIN (klass))
2465 {
2466 t = build_fold_addr_expr (klass);
2467 t = tree_cons (NULL, t, NULL);
2468 t = build_function_call_expr (register_class_fn, t);
2469 append_to_statement_list (t, list_p);
2470 }
2471 }
2472 }
2473
2474 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2475
2476 static tree
2477 build_symbol_entry (tree decl)
2478 {
2479 tree clname, name, signature, sym;
2480 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2481 /* ??? Constructors are given the name foo.foo all the way through
2482 the compiler, but in the method table they're all renamed
2483 foo.<init>. So, we have to do the same here unless we want an
2484 unresolved reference at runtime. */
2485 name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
2486 && DECL_CONSTRUCTOR_P (decl))
2487 ? init_identifier_node
2488 : DECL_NAME (decl));
2489 signature = build_java_signature (TREE_TYPE (decl));
2490 signature = build_utf8_ref (unmangle_classname
2491 (IDENTIFIER_POINTER (signature),
2492 IDENTIFIER_LENGTH (signature)));
2493
2494 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2495 PUSH_FIELD_VALUE (sym, "clname", clname);
2496 PUSH_FIELD_VALUE (sym, "name", name);
2497 PUSH_FIELD_VALUE (sym, "signature", signature);
2498 FINISH_RECORD_CONSTRUCTOR (sym);
2499 TREE_CONSTANT (sym) = 1;
2500 TREE_INVARIANT (sym) = 1;
2501
2502 return sym;
2503 }
2504
2505 /* Emit a symbol table: used by -findirect-dispatch. */
2506
2507 tree
2508 emit_symbol_table (tree name, tree the_table, tree decl_list,
2509 tree the_syms_decl, tree the_array_element_type,
2510 int element_size)
2511 {
2512 tree method_list, method, table, list, null_symbol;
2513 tree table_size, the_array_type;
2514 int index;
2515
2516 /* Only emit a table if this translation unit actually made any
2517 references via it. */
2518 if (decl_list == NULL_TREE)
2519 return the_table;
2520
2521 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2522 index = 0;
2523 method_list = decl_list;
2524 list = NULL_TREE;
2525 while (method_list != NULL_TREE)
2526 {
2527 method = TREE_VALUE (method_list);
2528 list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2529 method_list = TREE_CHAIN (method_list);
2530 index++;
2531 }
2532
2533 /* Terminate the list with a "null" entry. */
2534 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2535 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2536 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2537 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2538 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2539 TREE_CONSTANT (null_symbol) = 1;
2540 TREE_INVARIANT (null_symbol) = 1;
2541 list = tree_cons (NULL_TREE, null_symbol, list);
2542
2543 /* Put the list in the right order and make it a constructor. */
2544 list = nreverse (list);
2545 table = build_constructor (symbols_array_type, list);
2546
2547 /* Make it the initial value for otable_syms and emit the decl. */
2548 DECL_INITIAL (the_syms_decl) = table;
2549 DECL_ARTIFICIAL (the_syms_decl) = 1;
2550 DECL_IGNORED_P (the_syms_decl) = 1;
2551 rest_of_decl_compilation (the_syms_decl, 1, 0);
2552
2553 /* Now that its size is known, redefine the table as an
2554 uninitialized static array of INDEX + 1 elements. The extra entry
2555 is used by the runtime to track whether the table has been
2556 initialized. */
2557 table_size
2558 = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2559 the_array_type = build_array_type (the_array_element_type, table_size);
2560 the_table = build_decl (VAR_DECL, name, the_array_type);
2561 TREE_STATIC (the_table) = 1;
2562 TREE_READONLY (the_table) = 1;
2563 rest_of_decl_compilation (the_table, 1, 0);
2564
2565 return the_table;
2566 }
2567
2568 /* Make an entry for the catch_classes list. */
2569 tree
2570 make_catch_class_record (tree catch_class, tree classname)
2571 {
2572 tree entry;
2573 tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2574 START_RECORD_CONSTRUCTOR (entry, type);
2575 PUSH_FIELD_VALUE (entry, "address", catch_class);
2576 PUSH_FIELD_VALUE (entry, "classname", classname);
2577 FINISH_RECORD_CONSTRUCTOR (entry);
2578 return entry;
2579 }
2580
2581
2582 /* Generate the list of Throwable classes that are caught by exception
2583 handlers in this class. */
2584 tree
2585 emit_catch_table (tree this_class)
2586 {
2587 tree table, table_size, array_type;
2588 TYPE_CATCH_CLASSES (this_class) =
2589 tree_cons (NULL,
2590 make_catch_class_record (null_pointer_node, null_pointer_node),
2591 TYPE_CATCH_CLASSES (this_class));
2592 TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2593 TYPE_CATCH_CLASSES (this_class) =
2594 tree_cons (NULL,
2595 make_catch_class_record (null_pointer_node, null_pointer_node),
2596 TYPE_CATCH_CLASSES (this_class));
2597 table_size = build_index_type
2598 (build_int_cst (NULL_TREE,
2599 list_length (TYPE_CATCH_CLASSES (this_class))));
2600 array_type
2601 = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2602 table_size);
2603 table =
2604 build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2605 DECL_INITIAL (table) =
2606 build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
2607 TREE_STATIC (table) = 1;
2608 TREE_READONLY (table) = 1;
2609 DECL_IGNORED_P (table) = 1;
2610 rest_of_decl_compilation (table, 1, 0);
2611 return table;
2612 }
2613
2614 /* Given a type, return the signature used by
2615 _Jv_FindClassFromSignature() in libgcj. This isn't exactly the
2616 same as build_java_signature() because we want the canonical array
2617 type. */
2618
2619 static tree
2620 build_signature_for_libgcj (tree type)
2621 {
2622 tree sig, ref;
2623
2624 sig = build_java_signature (type);
2625 ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
2626 IDENTIFIER_LENGTH (sig)));
2627 return ref;
2628 }
2629
2630 /* Add an entry to the type assertion table. Callback used during hashtable
2631 traversal. */
2632
2633 static int
2634 add_assertion_table_entry (void **htab_entry, void *ptr)
2635 {
2636 tree entry;
2637 tree code_val, op1_utf8, op2_utf8;
2638 tree *list = (tree *) ptr;
2639 type_assertion *as = (type_assertion *) *htab_entry;
2640
2641 code_val = build_int_cst (NULL_TREE, as->assertion_code);
2642
2643 if (as->op1 == NULL_TREE)
2644 op1_utf8 = null_pointer_node;
2645 else
2646 op1_utf8 = build_signature_for_libgcj (as->op1);
2647
2648 if (as->op2 == NULL_TREE)
2649 op2_utf8 = null_pointer_node;
2650 else
2651 op2_utf8 = build_signature_for_libgcj (as->op2);
2652
2653 START_RECORD_CONSTRUCTOR (entry, assertion_entry_type);
2654 PUSH_FIELD_VALUE (entry, "assertion_code", code_val);
2655 PUSH_FIELD_VALUE (entry, "op1", op1_utf8);
2656 PUSH_FIELD_VALUE (entry, "op2", op2_utf8);
2657 FINISH_RECORD_CONSTRUCTOR (entry);
2658
2659 *list = tree_cons (NULL_TREE, entry, *list);
2660 return true;
2661 }
2662
2663 /* Generate the type assertion table for CLASS, and return its DECL. */
2664
2665 static tree
2666 emit_assertion_table (tree class)
2667 {
2668 tree null_entry, ctor, table_decl;
2669 tree list = NULL_TREE;
2670 htab_t assertions_htab = TYPE_ASSERTIONS (class);
2671
2672 /* Iterate through the hash table. */
2673 htab_traverse (assertions_htab, add_assertion_table_entry, &list);
2674
2675 /* Finish with a null entry. */
2676 START_RECORD_CONSTRUCTOR (null_entry, assertion_entry_type);
2677 PUSH_FIELD_VALUE (null_entry, "assertion_code", integer_zero_node);
2678 PUSH_FIELD_VALUE (null_entry, "op1", null_pointer_node);
2679 PUSH_FIELD_VALUE (null_entry, "op2", null_pointer_node);
2680 FINISH_RECORD_CONSTRUCTOR (null_entry);
2681
2682 list = tree_cons (NULL_TREE, null_entry, list);
2683
2684 /* Put the list in the right order and make it a constructor. */
2685 list = nreverse (list);
2686 ctor = build_constructor (assertion_table_type, list);
2687
2688 table_decl = build_decl (VAR_DECL, mangled_classname ("_type_assert_", class),
2689 assertion_table_type);
2690
2691 TREE_STATIC (table_decl) = 1;
2692 TREE_READONLY (table_decl) = 1;
2693 TREE_CONSTANT (table_decl) = 1;
2694 DECL_IGNORED_P (table_decl) = 1;
2695
2696 DECL_INITIAL (table_decl) = ctor;
2697 DECL_ARTIFICIAL (table_decl) = 1;
2698 rest_of_decl_compilation (table_decl, 1, 0);
2699
2700 return table_decl;
2701 }
2702
2703 void
2704 init_class_processing (void)
2705 {
2706 fields_ident = get_identifier ("fields");
2707 info_ident = get_identifier ("info");
2708
2709 gcc_obstack_init (&temporary_obstack);
2710 }
2711 \f
2712 static hashval_t java_treetreehash_hash (const void *);
2713 static int java_treetreehash_compare (const void *, const void *);
2714
2715 /* A hash table mapping trees to trees. Used generally. */
2716
2717 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2718
2719 static hashval_t
2720 java_treetreehash_hash (const void *k_p)
2721 {
2722 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2723 return JAVA_TREEHASHHASH_H (k->key);
2724 }
2725
2726 static int
2727 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2728 {
2729 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2730 tree k2 = (tree) k2_p;
2731 return (k1->key == k2);
2732 }
2733
2734 tree
2735 java_treetreehash_find (htab_t ht, tree t)
2736 {
2737 struct treetreehash_entry *e;
2738 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2739 e = htab_find_with_hash (ht, t, hv);
2740 if (e == NULL)
2741 return NULL;
2742 else
2743 return e->value;
2744 }
2745
2746 tree *
2747 java_treetreehash_new (htab_t ht, tree t)
2748 {
2749 void **e;
2750 struct treetreehash_entry *tthe;
2751 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2752
2753 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2754 if (*e == NULL)
2755 {
2756 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2757 tthe->key = t;
2758 *e = tthe;
2759 }
2760 else
2761 tthe = (struct treetreehash_entry *) *e;
2762 return &tthe->value;
2763 }
2764
2765 htab_t
2766 java_treetreehash_create (size_t size, int gc)
2767 {
2768 if (gc)
2769 return htab_create_ggc (size, java_treetreehash_hash,
2770 java_treetreehash_compare, NULL);
2771 else
2772 return htab_create_alloc (size, java_treetreehash_hash,
2773 java_treetreehash_compare, free, xcalloc, free);
2774 }
2775
2776 /* Break down qualified IDENTIFIER into package and class-name components.
2777 For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
2778 "pkg.foo", and RIGHT to "Bar". */
2779
2780 int
2781 split_qualified_name (tree *left, tree *right, tree source)
2782 {
2783 char *p, *base;
2784 int l = IDENTIFIER_LENGTH (source);
2785
2786 base = alloca (l + 1);
2787 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
2788
2789 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
2790 p = base + l - 1;
2791 while (*p != '.' && p != base)
2792 p--;
2793
2794 /* We didn't find a '.'. Return an error. */
2795 if (p == base)
2796 return 1;
2797
2798 *p = '\0';
2799 if (right)
2800 *right = get_identifier (p+1);
2801 *left = get_identifier (base);
2802
2803 return 0;
2804 }
2805
2806 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
2807 if the classes are from the same package. */
2808
2809 int
2810 in_same_package (tree name1, tree name2)
2811 {
2812 tree tmp;
2813 tree pkg1;
2814 tree pkg2;
2815
2816 if (TREE_CODE (name1) == TYPE_DECL)
2817 name1 = DECL_NAME (name1);
2818 if (TREE_CODE (name2) == TYPE_DECL)
2819 name2 = DECL_NAME (name2);
2820
2821 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
2822 /* One in empty package. */
2823 return 0;
2824
2825 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
2826 /* Both in empty package. */
2827 return 1;
2828
2829 split_qualified_name (&pkg1, &tmp, name1);
2830 split_qualified_name (&pkg2, &tmp, name2);
2831
2832 return (pkg1 == pkg2);
2833 }
2834
2835 #include "gt-java-class.h"