re PR java/19285 (Interfaces not initialized by static field access)
[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 (0, "misplaced ConstantValue attribute (not in any field)");
792 else if (DECL_INITIAL (field) != NULL_TREE)
793 warning (0, "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 /* Create a local statically allocated variable that will hold a
1041 pointer to a static field. */
1042
1043 static tree
1044 build_fieldref_cache_entry (int index, tree fdecl ATTRIBUTE_UNUSED)
1045 {
1046 tree decl, decl_name;
1047 const char *name = IDENTIFIER_POINTER (mangled_classname ("_cpool_", output_class));
1048 char *buf = alloca (strlen (name) + 20);
1049 sprintf (buf, "%s_%d_ref", name, index);
1050 decl_name = get_identifier (buf);
1051 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
1052 if (decl == NULL_TREE)
1053 {
1054 decl = build_decl (VAR_DECL, decl_name, ptr_type_node);
1055 TREE_STATIC (decl) = 1;
1056 TREE_PUBLIC (decl) = 0;
1057 DECL_EXTERNAL (decl) = 0;
1058 DECL_ARTIFICIAL (decl) = 1;
1059 make_decl_rtl (decl);
1060 pushdecl_top_level (decl);
1061 }
1062 return decl;
1063 }
1064
1065 tree
1066 build_static_field_ref (tree fdecl)
1067 {
1068 tree fclass = DECL_CONTEXT (fdecl);
1069 int is_compiled = is_compiled_class (fclass);
1070
1071 /* Allow static final fields to fold to a constant. When using
1072 -fno-assume-compiled, gcj will sometimes try to fold a field from
1073 an uncompiled class. This is required when the field in question
1074 meets the appropriate criteria for a compile-time constant.
1075 However, currently sometimes gcj is too eager and will end up
1076 returning the field itself, leading to an incorrect external
1077 reference being generated. */
1078 if ((is_compiled && !flag_indirect_dispatch)
1079 || (FIELD_FINAL (fdecl) && DECL_INITIAL (fdecl) != NULL_TREE
1080 && (JSTRING_TYPE_P (TREE_TYPE (fdecl))
1081 || JNUMERIC_TYPE_P (TREE_TYPE (fdecl)))
1082 && TREE_CONSTANT (DECL_INITIAL (fdecl))))
1083 {
1084 if (!DECL_RTL_SET_P (fdecl))
1085 {
1086 if (is_compiled == 1)
1087 DECL_EXTERNAL (fdecl) = 1;
1088 make_decl_rtl (fdecl);
1089 }
1090 }
1091 else
1092 {
1093 /* Generate a CONSTANT_FieldRef for FDECL in the constant pool
1094 and a class local static variable CACHE_ENTRY, then
1095
1096 *(fdecl **)((__builtin_expect (cache_entry == null, false))
1097 ? cache_entry = _Jv_ResolvePoolEntry (output_class, cpool_index)
1098 : cache_entry)
1099
1100 This can mostly be optimized away, so that the usual path is a
1101 load followed by a test and branch. _Jv_ResolvePoolEntry is
1102 only called once for each constant pool entry.
1103
1104 There is an optimization that we don't do: at the start of a
1105 method, create a local copy of CACHE_ENTRY and use that instead.
1106
1107 */
1108
1109 int cpool_index = alloc_constant_fieldref (output_class, fdecl);
1110 tree cache_entry = build_fieldref_cache_entry (cpool_index, fdecl);
1111 tree test
1112 = build3 (CALL_EXPR, boolean_type_node,
1113 build_address_of (built_in_decls[BUILT_IN_EXPECT]),
1114 tree_cons (NULL_TREE, build2 (EQ_EXPR, boolean_type_node,
1115 cache_entry, null_pointer_node),
1116 build_tree_list (NULL_TREE, boolean_false_node)),
1117 NULL_TREE);
1118 tree cpool_index_cst = build_int_cst (NULL_TREE, cpool_index);
1119 tree init
1120 = build3 (CALL_EXPR, ptr_type_node,
1121 build_address_of (soft_resolvepoolentry_node),
1122 tree_cons (NULL_TREE, build_class_ref (output_class),
1123 build_tree_list (NULL_TREE, cpool_index_cst)),
1124 NULL_TREE);
1125 init = build2 (MODIFY_EXPR, ptr_type_node, cache_entry, init);
1126 init = build3 (COND_EXPR, ptr_type_node, test, init, cache_entry);
1127 init = fold_convert (build_pointer_type (TREE_TYPE (fdecl)), init);
1128 fdecl = build1 (INDIRECT_REF, TREE_TYPE (fdecl), init);
1129 }
1130 return fdecl;
1131 }
1132
1133 int
1134 get_access_flags_from_decl (tree decl)
1135 {
1136 int access_flags = 0;
1137 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
1138 {
1139 if (FIELD_STATIC (decl))
1140 access_flags |= ACC_STATIC;
1141 if (FIELD_PUBLIC (decl))
1142 access_flags |= ACC_PUBLIC;
1143 if (FIELD_PROTECTED (decl))
1144 access_flags |= ACC_PROTECTED;
1145 if (FIELD_PRIVATE (decl))
1146 access_flags |= ACC_PRIVATE;
1147 if (FIELD_FINAL (decl))
1148 access_flags |= ACC_FINAL;
1149 if (FIELD_VOLATILE (decl))
1150 access_flags |= ACC_VOLATILE;
1151 if (FIELD_TRANSIENT (decl))
1152 access_flags |= ACC_TRANSIENT;
1153 return access_flags;
1154 }
1155 if (TREE_CODE (decl) == TYPE_DECL)
1156 {
1157 if (CLASS_PUBLIC (decl))
1158 access_flags |= ACC_PUBLIC;
1159 if (CLASS_FINAL (decl))
1160 access_flags |= ACC_FINAL;
1161 if (CLASS_SUPER (decl))
1162 access_flags |= ACC_SUPER;
1163 if (CLASS_INTERFACE (decl))
1164 access_flags |= ACC_INTERFACE;
1165 if (CLASS_ABSTRACT (decl))
1166 access_flags |= ACC_ABSTRACT;
1167 if (CLASS_STATIC (decl))
1168 access_flags |= ACC_STATIC;
1169 if (CLASS_PRIVATE (decl))
1170 access_flags |= ACC_PRIVATE;
1171 if (CLASS_PROTECTED (decl))
1172 access_flags |= ACC_PROTECTED;
1173 if (CLASS_STRICTFP (decl))
1174 access_flags |= ACC_STRICT;
1175 return access_flags;
1176 }
1177 if (TREE_CODE (decl) == FUNCTION_DECL)
1178 {
1179 if (METHOD_PUBLIC (decl))
1180 access_flags |= ACC_PUBLIC;
1181 if (METHOD_PRIVATE (decl))
1182 access_flags |= ACC_PRIVATE;
1183 if (METHOD_PROTECTED (decl))
1184 access_flags |= ACC_PROTECTED;
1185 if (METHOD_STATIC (decl))
1186 access_flags |= ACC_STATIC;
1187 if (METHOD_FINAL (decl))
1188 access_flags |= ACC_FINAL;
1189 if (METHOD_SYNCHRONIZED (decl))
1190 access_flags |= ACC_SYNCHRONIZED;
1191 if (METHOD_NATIVE (decl))
1192 access_flags |= ACC_NATIVE;
1193 if (METHOD_ABSTRACT (decl))
1194 access_flags |= ACC_ABSTRACT;
1195 if (METHOD_STRICTFP (decl))
1196 access_flags |= ACC_STRICT;
1197 if (METHOD_INVISIBLE (decl))
1198 access_flags |= ACC_INVISIBLE;
1199 return access_flags;
1200 }
1201 abort ();
1202 }
1203
1204 static GTY (()) int alias_labelno = 0;
1205
1206 /* Create a private alias for METHOD. Using this alias instead of the method
1207 decl ensures that ncode entries in the method table point to the real function
1208 at runtime, not a PLT entry. */
1209
1210 static tree
1211 make_local_function_alias (tree method)
1212 {
1213 #ifdef ASM_OUTPUT_DEF
1214 tree alias;
1215
1216 const char *method_name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (method));
1217 char *name = alloca (strlen (method_name) + 2);
1218 char *buf = alloca (strlen (method_name) + 128);
1219
1220 /* Only create aliases for local functions. */
1221 if (DECL_EXTERNAL (method))
1222 return method;
1223
1224 /* Prefix method_name with 'L' for the alias label. */
1225 *name = 'L';
1226 strcpy (name + 1, method_name);
1227
1228 ASM_GENERATE_INTERNAL_LABEL (buf, name, alias_labelno++);
1229 alias = build_decl (FUNCTION_DECL, get_identifier (buf),
1230 TREE_TYPE (method));
1231 DECL_CONTEXT (alias) = NULL;
1232 TREE_READONLY (alias) = TREE_READONLY (method);
1233 TREE_THIS_VOLATILE (alias) = TREE_THIS_VOLATILE (method);
1234 TREE_PUBLIC (alias) = 0;
1235 DECL_EXTERNAL (alias) = 0;
1236 DECL_ARTIFICIAL (alias) = 1;
1237 DECL_INLINE (alias) = 0;
1238 DECL_INITIAL (alias) = error_mark_node;
1239 TREE_ADDRESSABLE (alias) = 1;
1240 TREE_USED (alias) = 1;
1241 SET_DECL_ASSEMBLER_NAME (alias, DECL_NAME (alias));
1242 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias)) = 1;
1243 if (!flag_syntax_only)
1244 assemble_alias (alias, DECL_ASSEMBLER_NAME (method));
1245 return alias;
1246 #else
1247 return method;
1248 #endif
1249 }
1250
1251 /** Make reflection data (_Jv_Field) for field FDECL. */
1252
1253 static tree
1254 make_field_value (tree fdecl)
1255 {
1256 tree finit;
1257 int flags;
1258 tree type = TREE_TYPE (fdecl);
1259 int resolved = is_compiled_class (type) && ! flag_indirect_dispatch;
1260
1261 START_RECORD_CONSTRUCTOR (finit, field_type_node);
1262 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
1263 if (resolved)
1264 type = build_class_ref (type);
1265 else
1266 {
1267 tree signature = build_java_signature (type);
1268
1269 type = build_utf8_ref (unmangle_classname
1270 (IDENTIFIER_POINTER (signature),
1271 IDENTIFIER_LENGTH (signature)));
1272 }
1273 PUSH_FIELD_VALUE (finit, "type", type);
1274
1275 flags = get_access_flags_from_decl (fdecl);
1276 if (! resolved)
1277 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
1278
1279 PUSH_FIELD_VALUE (finit, "accflags", build_int_cst (NULL_TREE, flags));
1280 PUSH_FIELD_VALUE (finit, "bsize", TYPE_SIZE_UNIT (TREE_TYPE (fdecl)));
1281
1282 PUSH_FIELD_VALUE
1283 (finit, "info",
1284 build_constructor (field_info_union_node,
1285 build_tree_list
1286 ((FIELD_STATIC (fdecl)
1287 ? TREE_CHAIN (TYPE_FIELDS (field_info_union_node))
1288 : TYPE_FIELDS (field_info_union_node)),
1289 (FIELD_STATIC (fdecl)
1290 ? build_address_of (fdecl)
1291 : byte_position (fdecl)))));
1292
1293 FINISH_RECORD_CONSTRUCTOR (finit);
1294 return finit;
1295 }
1296
1297 /** Make reflection data (_Jv_Method) for method MDECL. */
1298
1299 static tree
1300 make_method_value (tree mdecl)
1301 {
1302 static int method_name_count = 0;
1303 tree minit;
1304 tree index;
1305 tree code;
1306 tree class_decl;
1307 #define ACC_TRANSLATED 0x4000
1308 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
1309
1310 class_decl = DECL_CONTEXT (mdecl);
1311 /* For interfaces, the index field contains the dispatch index. */
1312 if (CLASS_INTERFACE (TYPE_NAME (class_decl)))
1313 index = build_int_cst (NULL_TREE,
1314 get_interface_method_index (mdecl, class_decl));
1315 else if (!flag_indirect_dispatch && get_method_index (mdecl) != NULL_TREE)
1316 index = get_method_index (mdecl);
1317 else
1318 index = integer_minus_one_node;
1319
1320 code = null_pointer_node;
1321 if (DECL_RTL_SET_P (mdecl))
1322 code = build1 (ADDR_EXPR, nativecode_ptr_type_node,
1323 make_local_function_alias (mdecl));
1324 START_RECORD_CONSTRUCTOR (minit, method_type_node);
1325 PUSH_FIELD_VALUE (minit, "name",
1326 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
1327 init_identifier_node
1328 : DECL_NAME (mdecl)));
1329 {
1330 tree signature = build_java_signature (TREE_TYPE (mdecl));
1331 PUSH_FIELD_VALUE (minit, "signature",
1332 (build_utf8_ref
1333 (unmangle_classname
1334 (IDENTIFIER_POINTER(signature),
1335 IDENTIFIER_LENGTH(signature)))));
1336 }
1337 PUSH_FIELD_VALUE (minit, "accflags", build_int_cst (NULL_TREE, accflags));
1338 PUSH_FIELD_VALUE (minit, "index", index);
1339 PUSH_FIELD_VALUE (minit, "ncode", code);
1340
1341 {
1342 /* Compute the `throws' information for the method. */
1343 tree table = null_pointer_node;
1344 if (DECL_FUNCTION_THROWS (mdecl) != NULL_TREE)
1345 {
1346 int length = 1 + list_length (DECL_FUNCTION_THROWS (mdecl));
1347 tree iter, type, array;
1348 char buf[60];
1349
1350 table = tree_cons (NULL_TREE, table, NULL_TREE);
1351 for (iter = DECL_FUNCTION_THROWS (mdecl);
1352 iter != NULL_TREE;
1353 iter = TREE_CHAIN (iter))
1354 {
1355 tree sig = DECL_NAME (TYPE_NAME (TREE_VALUE (iter)));
1356 tree utf8
1357 = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
1358 IDENTIFIER_LENGTH (sig)));
1359 table = tree_cons (NULL_TREE, utf8, table);
1360 }
1361 type = build_prim_array_type (ptr_type_node, length);
1362 table = build_constructor (type, table);
1363 /* Compute something unique enough. */
1364 sprintf (buf, "_methods%d", method_name_count++);
1365 array = build_decl (VAR_DECL, get_identifier (buf), type);
1366 DECL_INITIAL (array) = table;
1367 TREE_STATIC (array) = 1;
1368 DECL_ARTIFICIAL (array) = 1;
1369 DECL_IGNORED_P (array) = 1;
1370 rest_of_decl_compilation (array, 1, 0);
1371
1372 table = build1 (ADDR_EXPR, ptr_type_node, array);
1373 }
1374
1375 PUSH_FIELD_VALUE (minit, "throws", table);
1376 }
1377
1378 FINISH_RECORD_CONSTRUCTOR (minit);
1379 return minit;
1380 }
1381
1382 static tree
1383 get_dispatch_vector (tree type)
1384 {
1385 tree vtable = TYPE_VTABLE (type);
1386
1387 if (vtable == NULL_TREE)
1388 {
1389 HOST_WIDE_INT i;
1390 tree method;
1391 tree super = CLASSTYPE_SUPER (type);
1392 HOST_WIDE_INT nvirtuals = tree_low_cst (TYPE_NVIRTUALS (type), 0);
1393 vtable = make_tree_vec (nvirtuals);
1394 TYPE_VTABLE (type) = vtable;
1395 if (super != NULL_TREE)
1396 {
1397 tree super_vtable = get_dispatch_vector (super);
1398
1399 for (i = tree_low_cst (TYPE_NVIRTUALS (super), 0); --i >= 0; )
1400 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
1401 }
1402
1403 for (method = TYPE_METHODS (type); method != NULL_TREE;
1404 method = TREE_CHAIN (method))
1405 {
1406 tree method_index = get_method_index (method);
1407 if (method_index != NULL_TREE
1408 && host_integerp (method_index, 0))
1409 TREE_VEC_ELT (vtable, tree_low_cst (method_index, 0)) = method;
1410 }
1411 }
1412
1413 return vtable;
1414 }
1415
1416 static tree
1417 get_dispatch_table (tree type, tree this_class_addr)
1418 {
1419 int abstract_p = CLASS_ABSTRACT (TYPE_NAME (type));
1420 tree vtable = get_dispatch_vector (type);
1421 int i, j;
1422 tree list = NULL_TREE;
1423 int nvirtuals = TREE_VEC_LENGTH (vtable);
1424 int arraysize;
1425 tree gc_descr;
1426
1427 for (i = nvirtuals; --i >= 0; )
1428 {
1429 tree method = TREE_VEC_ELT (vtable, i);
1430 if (METHOD_ABSTRACT (method))
1431 {
1432 if (! abstract_p)
1433 warning (0, "%Jabstract method in non-abstract class", method);
1434
1435 if (TARGET_VTABLE_USES_DESCRIPTORS)
1436 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1437 list = tree_cons (NULL_TREE, null_pointer_node, list);
1438 else
1439 list = tree_cons (NULL_TREE, null_pointer_node, list);
1440 }
1441 else
1442 {
1443 if (!DECL_RTL_SET_P (method))
1444 make_decl_rtl (method);
1445
1446 if (TARGET_VTABLE_USES_DESCRIPTORS)
1447 for (j = 0; j < TARGET_VTABLE_USES_DESCRIPTORS; ++j)
1448 {
1449 tree fdesc = build2 (FDESC_EXPR, nativecode_ptr_type_node,
1450 method, build_int_cst (NULL_TREE, j));
1451 TREE_CONSTANT (fdesc) = 1;
1452 TREE_INVARIANT (fdesc) = 1;
1453 list = tree_cons (NULL_TREE, fdesc, list);
1454 }
1455 else
1456 list = tree_cons (NULL_TREE,
1457 build1 (ADDR_EXPR, nativecode_ptr_type_node,
1458 method),
1459 list);
1460 }
1461 }
1462
1463 /* Dummy entry for compatibility with G++ -fvtable-thunks. When
1464 using the Boehm GC we sometimes stash a GC type descriptor
1465 there. We set the PURPOSE to NULL_TREE not to interfere (reset)
1466 the emitted byte count during the output to the assembly file. */
1467 /* With TARGET_VTABLE_USES_DESCRIPTORS, we only add one extra
1468 fake "function descriptor". It's first word is the is the class
1469 pointer, and subsequent words (usually one) contain the GC descriptor.
1470 In all other cases, we reserve two extra vtable slots. */
1471 gc_descr = get_boehm_type_descriptor (type);
1472 list = tree_cons (NULL_TREE, gc_descr, list);
1473 for (j = 1; j < TARGET_VTABLE_USES_DESCRIPTORS-1; ++j)
1474 list = tree_cons (NULL_TREE, gc_descr, list);
1475 list = tree_cons (NULL_TREE, this_class_addr, list);
1476
1477 /** Pointer to type_info object (to be implemented), according to g++ ABI. */
1478 list = tree_cons (NULL_TREE, null_pointer_node, list);
1479 /** Offset to start of whole object. Always (ptrdiff_t)0 for Java. */
1480 list = tree_cons (integer_zero_node, null_pointer_node, list);
1481
1482 arraysize = (TARGET_VTABLE_USES_DESCRIPTORS? nvirtuals + 1 : nvirtuals + 2);
1483 if (TARGET_VTABLE_USES_DESCRIPTORS)
1484 arraysize *= TARGET_VTABLE_USES_DESCRIPTORS;
1485 arraysize += 2;
1486 return build_constructor (build_prim_array_type (nativecode_ptr_type_node,
1487 arraysize), list);
1488 }
1489
1490
1491 /* Set the method_index for a method decl. */
1492 void
1493 set_method_index (tree decl, tree method_index)
1494 {
1495 if (method_index != NULL_TREE)
1496 {
1497 /* method_index is null if we're using indirect dispatch. */
1498 method_index = fold (convert (sizetype, method_index));
1499
1500 if (TARGET_VTABLE_USES_DESCRIPTORS)
1501 /* Add one to skip bogus descriptor for class and GC descriptor. */
1502 method_index = size_binop (PLUS_EXPR, method_index, size_int (1));
1503 else
1504 /* Add 1 to skip "class" field of dtable, and 1 to skip GC
1505 descriptor. */
1506 method_index = size_binop (PLUS_EXPR, method_index, size_int (2));
1507 }
1508
1509 DECL_VINDEX (decl) = method_index;
1510 }
1511
1512 /* Get the method_index for a method decl. */
1513 tree
1514 get_method_index (tree decl)
1515 {
1516 tree method_index = DECL_VINDEX (decl);
1517
1518 if (! method_index)
1519 return NULL;
1520
1521 if (TARGET_VTABLE_USES_DESCRIPTORS)
1522 /* Sub one to skip bogus descriptor for class and GC descriptor. */
1523 method_index = size_binop (MINUS_EXPR, method_index, size_int (1));
1524 else
1525 /* Sub 1 to skip "class" field of dtable, and 1 to skip GC descriptor. */
1526 method_index = size_binop (MINUS_EXPR, method_index, size_int (2));
1527
1528 return method_index;
1529 }
1530
1531 static int
1532 supers_all_compiled (tree type)
1533 {
1534 while (type != NULL_TREE)
1535 {
1536 if (!assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)))))
1537 return 0;
1538 type = CLASSTYPE_SUPER (type);
1539 }
1540 return 1;
1541 }
1542
1543 void
1544 make_class_data (tree type)
1545 {
1546 tree decl, cons, temp;
1547 tree field, fields_decl;
1548 tree static_fields = NULL_TREE;
1549 tree instance_fields = NULL_TREE;
1550 HOST_WIDE_INT static_field_count = 0;
1551 HOST_WIDE_INT instance_field_count = 0;
1552 HOST_WIDE_INT field_count;
1553 tree field_array_type;
1554 tree method;
1555 tree methods = NULL_TREE;
1556 tree dtable_decl = NULL_TREE;
1557 HOST_WIDE_INT method_count = 0;
1558 tree method_array_type;
1559 tree methods_decl;
1560 tree super;
1561 tree this_class_addr;
1562 tree constant_pool_constructor;
1563 tree interfaces = null_pointer_node;
1564 int interface_len = 0;
1565 tree type_decl = TYPE_NAME (type);
1566 /** Offset from start of virtual function table declaration
1567 to where objects actually point at, following new g++ ABI. */
1568 tree dtable_start_offset = build_int_cst (NULL_TREE,
1569 2 * POINTER_SIZE / BITS_PER_UNIT);
1570
1571 this_class_addr = build_class_ref (type);
1572 decl = TREE_OPERAND (this_class_addr, 0);
1573
1574 /* Build Field array. */
1575 field = TYPE_FIELDS (type);
1576 while (field && DECL_ARTIFICIAL (field))
1577 field = TREE_CHAIN (field); /* Skip dummy fields. */
1578 if (field && DECL_NAME (field) == NULL_TREE)
1579 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
1580 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
1581 {
1582 if (! DECL_ARTIFICIAL (field))
1583 {
1584 tree init = make_field_value (field);
1585 if (FIELD_STATIC (field))
1586 {
1587 tree initial = DECL_INITIAL (field);
1588 static_field_count++;
1589 static_fields = tree_cons (NULL_TREE, init, static_fields);
1590 /* If the initial value is a string constant,
1591 prevent output_constant from trying to assemble the value. */
1592 if (initial != NULL_TREE
1593 && TREE_TYPE (initial) == string_ptr_type_node)
1594 DECL_INITIAL (field) = NULL_TREE;
1595 rest_of_decl_compilation (field, 1, 1);
1596 DECL_INITIAL (field) = initial;
1597 }
1598 else
1599 {
1600 instance_field_count++;
1601 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
1602 }
1603 }
1604 }
1605 field_count = static_field_count + instance_field_count;
1606 if (field_count > 0)
1607 {
1608 static_fields = nreverse (static_fields);
1609 instance_fields = nreverse (instance_fields);
1610 static_fields = chainon (static_fields, instance_fields);
1611 field_array_type = build_prim_array_type (field_type_node, field_count);
1612 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
1613 field_array_type);
1614 DECL_INITIAL (fields_decl) = build_constructor (field_array_type,
1615 static_fields);
1616 TREE_STATIC (fields_decl) = 1;
1617 DECL_ARTIFICIAL (fields_decl) = 1;
1618 DECL_IGNORED_P (fields_decl) = 1;
1619 rest_of_decl_compilation (fields_decl, 1, 0);
1620 }
1621 else
1622 fields_decl = NULL_TREE;
1623
1624 /* Build Method array. */
1625 for (method = TYPE_METHODS (type);
1626 method != NULL_TREE; method = TREE_CHAIN (method))
1627 {
1628 tree init;
1629 if (METHOD_PRIVATE (method)
1630 && ! flag_keep_inline_functions
1631 && optimize)
1632 continue;
1633 /* Even if we have a decl, we don't necessarily have the code.
1634 This can happen if we inherit a method from a superclass for
1635 which we don't have a .class file. */
1636 if (METHOD_DUMMY (method))
1637 continue;
1638 init = make_method_value (method);
1639 method_count++;
1640 methods = tree_cons (NULL_TREE, init, methods);
1641 }
1642 method_array_type = build_prim_array_type (method_type_node, method_count);
1643 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1644 method_array_type);
1645 DECL_INITIAL (methods_decl) = build_constructor (method_array_type,
1646 nreverse (methods));
1647 TREE_STATIC (methods_decl) = 1;
1648 DECL_ARTIFICIAL (methods_decl) = 1;
1649 DECL_IGNORED_P (methods_decl) = 1;
1650 rest_of_decl_compilation (methods_decl, 1, 0);
1651
1652 if (supers_all_compiled (type) && ! CLASS_INTERFACE (type_decl)
1653 && !flag_indirect_dispatch)
1654 {
1655 tree dtable = get_dispatch_table (type, this_class_addr);
1656 dtable_decl = build_dtable_decl (type);
1657 DECL_INITIAL (dtable_decl) = dtable;
1658 TREE_STATIC (dtable_decl) = 1;
1659 DECL_ARTIFICIAL (dtable_decl) = 1;
1660 DECL_IGNORED_P (dtable_decl) = 1;
1661 TREE_PUBLIC (dtable_decl) = 1;
1662 rest_of_decl_compilation (dtable_decl, 1, 0);
1663 if (type == class_type_node)
1664 class_dtable_decl = dtable_decl;
1665 }
1666
1667 if (class_dtable_decl == NULL_TREE)
1668 {
1669 class_dtable_decl = build_dtable_decl (class_type_node);
1670 TREE_STATIC (class_dtable_decl) = 1;
1671 DECL_ARTIFICIAL (class_dtable_decl) = 1;
1672 DECL_IGNORED_P (class_dtable_decl) = 1;
1673 if (is_compiled_class (class_type_node) != 2)
1674 DECL_EXTERNAL (class_dtable_decl) = 1;
1675 rest_of_decl_compilation (class_dtable_decl, 1, 0);
1676 }
1677
1678 super = CLASSTYPE_SUPER (type);
1679 if (super == NULL_TREE)
1680 super = null_pointer_node;
1681 else if (! flag_indirect_dispatch
1682 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (type_decl)))
1683 && assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (super)))))
1684 super = build_class_ref (super);
1685 else
1686 {
1687 int super_index = alloc_class_constant (super);
1688 super = build_int_cst (ptr_type_node, super_index);
1689 }
1690
1691 /* Build and emit the array of implemented interfaces. */
1692 if (type != object_type_node)
1693 interface_len = BINFO_N_BASE_BINFOS (TYPE_BINFO (type)) - 1;
1694
1695 if (interface_len > 0)
1696 {
1697 tree init = NULL_TREE;
1698 int i;
1699 tree interface_array_type, idecl;
1700 interface_array_type
1701 = build_prim_array_type (class_ptr_type, interface_len);
1702 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1703 interface_array_type);
1704
1705 for (i = interface_len; i > 0; i--)
1706 {
1707 tree child = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
1708 tree iclass = BINFO_TYPE (child);
1709 tree index;
1710 if (! flag_indirect_dispatch
1711 && (assume_compiled
1712 (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (iclass))))))
1713 index = build_class_ref (iclass);
1714 else
1715 {
1716 int int_index = alloc_class_constant (iclass);
1717 index = build_int_cst (ptr_type_node, int_index);
1718 }
1719 init = tree_cons (NULL_TREE, index, init);
1720 }
1721 DECL_INITIAL (idecl) = build_constructor (interface_array_type, init);
1722 TREE_STATIC (idecl) = 1;
1723 DECL_ARTIFICIAL (idecl) = 1;
1724 DECL_IGNORED_P (idecl) = 1;
1725 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1726 rest_of_decl_compilation (idecl, 1, 0);
1727 }
1728
1729 constant_pool_constructor = build_constants_constructor ();
1730
1731 if (flag_indirect_dispatch)
1732 {
1733 TYPE_OTABLE_DECL (type)
1734 = emit_symbol_table
1735 (DECL_NAME (TYPE_OTABLE_DECL (type)),
1736 TYPE_OTABLE_DECL (type), TYPE_OTABLE_METHODS (type),
1737 TYPE_OTABLE_SYMS_DECL (type), integer_type_node, 1);
1738
1739 TYPE_ATABLE_DECL (type)
1740 = emit_symbol_table
1741 (DECL_NAME (TYPE_ATABLE_DECL (type)),
1742 TYPE_ATABLE_DECL (type), TYPE_ATABLE_METHODS (type),
1743 TYPE_ATABLE_SYMS_DECL (type), ptr_type_node, 1);
1744
1745 TYPE_ITABLE_DECL (type)
1746 = emit_symbol_table
1747 (DECL_NAME (TYPE_ITABLE_DECL (type)),
1748 TYPE_ITABLE_DECL (type), TYPE_ITABLE_METHODS (type),
1749 TYPE_ITABLE_SYMS_DECL (type), ptr_type_node, 2);
1750 }
1751
1752 TYPE_CTABLE_DECL (type) = emit_catch_table (type);
1753
1754 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1755 PUSH_FIELD_VALUE (temp, "vtable",
1756 build2 (PLUS_EXPR, dtable_ptr_type,
1757 build1 (ADDR_EXPR, dtable_ptr_type,
1758 class_dtable_decl),
1759 dtable_start_offset));
1760 if (! flag_hash_synchronization)
1761 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1762 FINISH_RECORD_CONSTRUCTOR (temp);
1763 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1764 PUSH_SUPER_VALUE (cons, temp);
1765 PUSH_FIELD_VALUE (cons, "next_or_version", gcj_abi_version);
1766 PUSH_FIELD_VALUE (cons, "name", build_utf8_ref (DECL_NAME (type_decl)));
1767 PUSH_FIELD_VALUE (cons, "accflags",
1768 build_int_cst (NULL_TREE,
1769 get_access_flags_from_decl (type_decl)));
1770
1771 PUSH_FIELD_VALUE (cons, "superclass",
1772 CLASS_INTERFACE (type_decl) ? null_pointer_node : super);
1773 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1774 PUSH_FIELD_VALUE (cons, "methods",
1775 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1776 PUSH_FIELD_VALUE (cons, "method_count",
1777 build_int_cst (NULL_TREE, method_count));
1778
1779 if (flag_indirect_dispatch)
1780 PUSH_FIELD_VALUE (cons, "vtable_method_count", integer_minus_one_node);
1781 else
1782 PUSH_FIELD_VALUE (cons, "vtable_method_count", TYPE_NVIRTUALS (type));
1783
1784 PUSH_FIELD_VALUE (cons, "fields",
1785 fields_decl == NULL_TREE ? null_pointer_node
1786 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1787 /* If we're using the binary compatibility ABI we don't know the
1788 size until load time. */
1789 PUSH_FIELD_VALUE (cons, "size_in_bytes",
1790 (flag_indirect_dispatch
1791 ? integer_minus_one_node
1792 : size_in_bytes (type)));
1793 PUSH_FIELD_VALUE (cons, "field_count",
1794 build_int_cst (NULL_TREE, field_count));
1795 PUSH_FIELD_VALUE (cons, "static_field_count",
1796 build_int_cst (NULL_TREE, static_field_count));
1797
1798 if (flag_indirect_dispatch)
1799 PUSH_FIELD_VALUE (cons, "vtable", null_pointer_node);
1800 else
1801 PUSH_FIELD_VALUE (cons, "vtable",
1802 dtable_decl == NULL_TREE ? null_pointer_node
1803 : build2 (PLUS_EXPR, dtable_ptr_type,
1804 build1 (ADDR_EXPR, dtable_ptr_type,
1805 dtable_decl),
1806 dtable_start_offset));
1807 if (TYPE_OTABLE_METHODS (type) == NULL_TREE)
1808 {
1809 PUSH_FIELD_VALUE (cons, "otable", null_pointer_node);
1810 PUSH_FIELD_VALUE (cons, "otable_syms", null_pointer_node);
1811 }
1812 else
1813 {
1814 PUSH_FIELD_VALUE (cons, "otable",
1815 build1 (ADDR_EXPR, otable_ptr_type, TYPE_OTABLE_DECL (type)));
1816 PUSH_FIELD_VALUE (cons, "otable_syms",
1817 build1 (ADDR_EXPR, symbols_array_ptr_type,
1818 TYPE_OTABLE_SYMS_DECL (type)));
1819 TREE_CONSTANT (TYPE_OTABLE_DECL (type)) = 1;
1820 TREE_INVARIANT (TYPE_OTABLE_DECL (type)) = 1;
1821 }
1822 if (TYPE_ATABLE_METHODS(type) == NULL_TREE)
1823 {
1824 PUSH_FIELD_VALUE (cons, "atable", null_pointer_node);
1825 PUSH_FIELD_VALUE (cons, "atable_syms", null_pointer_node);
1826 }
1827 else
1828 {
1829 PUSH_FIELD_VALUE (cons, "atable",
1830 build1 (ADDR_EXPR, atable_ptr_type, TYPE_ATABLE_DECL (type)));
1831 PUSH_FIELD_VALUE (cons, "atable_syms",
1832 build1 (ADDR_EXPR, symbols_array_ptr_type,
1833 TYPE_ATABLE_SYMS_DECL (type)));
1834 TREE_CONSTANT (TYPE_ATABLE_DECL (type)) = 1;
1835 TREE_INVARIANT (TYPE_ATABLE_DECL (type)) = 1;
1836 }
1837 if (TYPE_ITABLE_METHODS(type) == NULL_TREE)
1838 {
1839 PUSH_FIELD_VALUE (cons, "itable", null_pointer_node);
1840 PUSH_FIELD_VALUE (cons, "itable_syms", null_pointer_node);
1841 }
1842 else
1843 {
1844 PUSH_FIELD_VALUE (cons, "itable",
1845 build1 (ADDR_EXPR, itable_ptr_type, TYPE_ITABLE_DECL (type)));
1846 PUSH_FIELD_VALUE (cons, "itable_syms",
1847 build1 (ADDR_EXPR, symbols_array_ptr_type,
1848 TYPE_ITABLE_SYMS_DECL (type)));
1849 TREE_CONSTANT (TYPE_ITABLE_DECL (type)) = 1;
1850 TREE_INVARIANT (TYPE_ITABLE_DECL (type)) = 1;
1851 }
1852
1853 PUSH_FIELD_VALUE (cons, "catch_classes",
1854 build1 (ADDR_EXPR, ptr_type_node, TYPE_CTABLE_DECL (type)));
1855 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1856 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1857 PUSH_FIELD_VALUE (cons, "interface_count",
1858 build_int_cst (NULL_TREE, interface_len));
1859 PUSH_FIELD_VALUE
1860 (cons, "state",
1861 convert (byte_type_node,
1862 build_int_cst (NULL_TREE,
1863 flag_indirect_dispatch
1864 ? JV_STATE_PRELOADING
1865 : JV_STATE_COMPILED)));
1866
1867 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1868 PUSH_FIELD_VALUE (cons, "depth", integer_zero_node);
1869 PUSH_FIELD_VALUE (cons, "ancestors", null_pointer_node);
1870 PUSH_FIELD_VALUE (cons, "idt", null_pointer_node);
1871 PUSH_FIELD_VALUE (cons, "arrayclass", null_pointer_node);
1872 PUSH_FIELD_VALUE (cons, "protectionDomain", null_pointer_node);
1873
1874 {
1875 tree assertion_table_ref;
1876 if (TYPE_ASSERTIONS (type) == NULL)
1877 assertion_table_ref = null_pointer_node;
1878 else
1879 assertion_table_ref = build1 (ADDR_EXPR,
1880 build_pointer_type (assertion_table_type),
1881 emit_assertion_table (type));
1882
1883 PUSH_FIELD_VALUE (cons, "assertion_table", assertion_table_ref);
1884 }
1885
1886 PUSH_FIELD_VALUE (cons, "hack_signers", null_pointer_node);
1887 PUSH_FIELD_VALUE (cons, "chain", null_pointer_node);
1888 PUSH_FIELD_VALUE (cons, "aux_info", null_pointer_node);
1889 PUSH_FIELD_VALUE (cons, "engine", null_pointer_node);
1890
1891 FINISH_RECORD_CONSTRUCTOR (cons);
1892
1893 DECL_INITIAL (decl) = cons;
1894
1895 /* Hash synchronization requires at least 64-bit alignment. */
1896 if (flag_hash_synchronization && POINTER_SIZE < 64)
1897 DECL_ALIGN (decl) = 64;
1898
1899 rest_of_decl_compilation (decl, 1, 0);
1900
1901 TYPE_OTABLE_DECL (type) = NULL_TREE;
1902 TYPE_ATABLE_DECL (type) = NULL_TREE;
1903 TYPE_CTABLE_DECL (type) = NULL_TREE;
1904 }
1905
1906 void
1907 finish_class (void)
1908 {
1909 if (TYPE_VERIFY_METHOD (output_class))
1910 {
1911 tree verify_method = TYPE_VERIFY_METHOD (output_class);
1912 DECL_SAVED_TREE (verify_method)
1913 = add_stmt_to_compound (DECL_SAVED_TREE (verify_method), void_type_node,
1914 build (RETURN_EXPR, void_type_node, NULL));
1915 java_genericize (verify_method);
1916 cgraph_finalize_function (verify_method, false);
1917 TYPE_ASSERTIONS (current_class) = NULL;
1918 }
1919
1920 java_expand_catch_classes (current_class);
1921
1922 current_function_decl = NULL_TREE;
1923 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (current_class)) = 0;
1924 make_class_data (current_class);
1925 register_class ();
1926 rest_of_decl_compilation (TYPE_NAME (current_class), 1, 0);
1927 }
1928
1929 /* Return 2 if CLASS is compiled by this compilation job;
1930 return 1 if CLASS can otherwise be assumed to be compiled;
1931 return 0 if we cannot assume that CLASS is compiled.
1932 Returns 1 for primitive and 0 for array types. */
1933 int
1934 is_compiled_class (tree class)
1935 {
1936 int seen_in_zip;
1937 if (TREE_CODE (class) == POINTER_TYPE)
1938 class = TREE_TYPE (class);
1939 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1940 return 1;
1941 if (TYPE_ARRAY_P (class))
1942 return 0;
1943 if (class == current_class)
1944 return 2;
1945
1946 seen_in_zip = (TYPE_JCF (class) && JCF_SEEN_IN_ZIP (TYPE_JCF (class)));
1947 if (CLASS_FROM_CURRENTLY_COMPILED_P (class) || seen_in_zip)
1948 {
1949 /* The class was seen in the current ZIP file and will be
1950 available as a compiled class in the future but may not have
1951 been loaded already. Load it if necessary. This prevent
1952 build_class_ref () from crashing. */
1953
1954 if (seen_in_zip && !CLASS_LOADED_P (class))
1955 load_class (class, 1);
1956
1957 /* We return 2 for class seen in ZIP and class from files
1958 belonging to the same compilation unit */
1959 return 2;
1960 }
1961
1962 if (assume_compiled (IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (class)))))
1963 {
1964 if (!CLASS_LOADED_P (class))
1965 {
1966 if (CLASS_FROM_SOURCE_P (class))
1967 safe_layout_class (class);
1968 else
1969 load_class (class, 1);
1970 }
1971 return 1;
1972 }
1973
1974 return 0;
1975 }
1976
1977 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1978
1979 tree
1980 build_dtable_decl (tree type)
1981 {
1982 tree dtype;
1983
1984 /* We need to build a new dtable type so that its size is uniquely
1985 computed when we're dealing with the class for real and not just
1986 faking it (like java.lang.Class during the initialization of the
1987 compiler.) We know we're not faking a class when CURRENT_CLASS is
1988 TYPE. */
1989 if (current_class == type)
1990 {
1991 tree dummy = NULL_TREE;
1992 int n;
1993
1994 dtype = make_node (RECORD_TYPE);
1995
1996 PUSH_FIELD (dtype, dummy, "top_offset", ptr_type_node);
1997 PUSH_FIELD (dtype, dummy, "type_info", ptr_type_node);
1998
1999 PUSH_FIELD (dtype, dummy, "class", class_ptr_type);
2000 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2001 {
2002 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2003 TREE_CHAIN (dummy) = tmp_field;
2004 DECL_CONTEXT (tmp_field) = dtype;
2005 DECL_ARTIFICIAL (tmp_field) = 1;
2006 dummy = tmp_field;
2007 }
2008
2009 PUSH_FIELD (dtype, dummy, "gc_descr", ptr_type_node);
2010 for (n = 1; n < TARGET_VTABLE_USES_DESCRIPTORS; ++n)
2011 {
2012 tree tmp_field = build_decl (FIELD_DECL, NULL_TREE, ptr_type_node);
2013 TREE_CHAIN (dummy) = tmp_field;
2014 DECL_CONTEXT (tmp_field) = dtype;
2015 DECL_ARTIFICIAL (tmp_field) = 1;
2016 dummy = tmp_field;
2017 }
2018
2019 n = TREE_VEC_LENGTH (get_dispatch_vector (type));
2020 if (TARGET_VTABLE_USES_DESCRIPTORS)
2021 n *= TARGET_VTABLE_USES_DESCRIPTORS;
2022
2023 PUSH_FIELD (dtype, dummy, "methods",
2024 build_prim_array_type (nativecode_ptr_type_node, n));
2025 layout_type (dtype);
2026 }
2027 else
2028 dtype = dtable_type;
2029
2030 return build_decl (VAR_DECL,
2031 java_mangle_vtable (&temporary_obstack, type), dtype);
2032 }
2033
2034 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
2035 fields inherited from SUPER_CLASS. */
2036
2037 void
2038 push_super_field (tree this_class, tree super_class)
2039 {
2040 tree base_decl;
2041 /* Don't insert the field if we're just re-laying the class out. */
2042 if (TYPE_FIELDS (this_class) && !DECL_NAME (TYPE_FIELDS (this_class)))
2043 return;
2044 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
2045 DECL_IGNORED_P (base_decl) = 1;
2046 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
2047 TYPE_FIELDS (this_class) = base_decl;
2048 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
2049 DECL_SIZE_UNIT (base_decl) = TYPE_SIZE_UNIT (super_class);
2050 }
2051
2052 /* Handle the different manners we may have to lay out a super class. */
2053
2054 static tree
2055 maybe_layout_super_class (tree super_class, tree this_class)
2056 {
2057 if (TREE_CODE (super_class) == RECORD_TYPE)
2058 {
2059 if (!CLASS_LOADED_P (super_class) && CLASS_FROM_SOURCE_P (super_class))
2060 safe_layout_class (super_class);
2061 if (!CLASS_LOADED_P (super_class))
2062 load_class (super_class, 1);
2063 }
2064 /* We might have to layout the class before its dependency on
2065 the super class gets resolved by java_complete_class */
2066 else if (TREE_CODE (super_class) == POINTER_TYPE)
2067 {
2068 if (TREE_TYPE (super_class) != NULL_TREE)
2069 super_class = TREE_TYPE (super_class);
2070 else
2071 {
2072 /* do_resolve_class expects an EXPR_WITH_FILE_LOCATION, so
2073 we give it one. */
2074 tree this_wrap = NULL_TREE;
2075
2076 if (this_class)
2077 {
2078 tree this_decl = TYPE_NAME (this_class);
2079 #ifdef USE_MAPPED_LOCATION
2080 this_wrap = build_expr_wfl (this_class,
2081 DECL_SOURCE_LOCATION (this_decl));
2082 #else
2083 this_wrap = build_expr_wfl (this_class,
2084 DECL_SOURCE_FILE (this_decl),
2085 DECL_SOURCE_LINE (this_decl), 0);
2086 #endif
2087 }
2088 super_class = do_resolve_class (NULL_TREE, /* FIXME? */
2089 super_class, NULL_TREE, this_wrap);
2090 if (!super_class)
2091 return NULL_TREE; /* FIXME, NULL_TREE not checked by caller. */
2092 super_class = TREE_TYPE (super_class);
2093 }
2094 }
2095 if (!TYPE_SIZE (super_class))
2096 safe_layout_class (super_class);
2097
2098 return super_class;
2099 }
2100
2101 void
2102 layout_class (tree this_class)
2103 {
2104 tree super_class = CLASSTYPE_SUPER (this_class);
2105 tree field;
2106
2107 class_list = tree_cons (this_class, NULL_TREE, class_list);
2108 if (CLASS_BEING_LAIDOUT (this_class))
2109 {
2110 char buffer [1024];
2111 char *report;
2112 tree current;
2113
2114 sprintf (buffer, " with '%s'",
2115 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class))));
2116 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2117
2118 for (current = TREE_CHAIN (class_list); current;
2119 current = TREE_CHAIN (current))
2120 {
2121 tree decl = TYPE_NAME (TREE_PURPOSE (current));
2122 sprintf (buffer, "\n which inherits from '%s' (%s:%d)",
2123 IDENTIFIER_POINTER (DECL_NAME (decl)),
2124 DECL_SOURCE_FILE (decl),
2125 DECL_SOURCE_LINE (decl));
2126 obstack_grow (&temporary_obstack, buffer, strlen (buffer));
2127 }
2128 obstack_1grow (&temporary_obstack, '\0');
2129 report = obstack_finish (&temporary_obstack);
2130 cyclic_inheritance_report = ggc_strdup (report);
2131 obstack_free (&temporary_obstack, report);
2132 TYPE_SIZE (this_class) = error_mark_node;
2133 return;
2134 }
2135 CLASS_BEING_LAIDOUT (this_class) = 1;
2136
2137 if (super_class && !CLASS_BEING_LAIDOUT (super_class))
2138 {
2139 tree maybe_super_class
2140 = maybe_layout_super_class (super_class, this_class);
2141 if (maybe_super_class == NULL
2142 || TREE_CODE (TYPE_SIZE (maybe_super_class)) == ERROR_MARK)
2143 {
2144 TYPE_SIZE (this_class) = error_mark_node;
2145 CLASS_BEING_LAIDOUT (this_class) = 0;
2146 class_list = TREE_CHAIN (class_list);
2147 return;
2148 }
2149 if (TYPE_SIZE (this_class) == NULL_TREE)
2150 push_super_field (this_class, maybe_super_class);
2151 }
2152
2153 for (field = TYPE_FIELDS (this_class);
2154 field != NULL_TREE; field = TREE_CHAIN (field))
2155 {
2156 if (FIELD_STATIC (field))
2157 {
2158 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
2159 SET_DECL_ASSEMBLER_NAME (field,
2160 java_mangle_decl
2161 (&temporary_obstack, field));
2162 }
2163 }
2164
2165 layout_type (this_class);
2166
2167 /* Also recursively load/layout any superinterfaces, but only if
2168 class was loaded from bytecode. The source parser will take care
2169 of this itself. */
2170 if (!CLASS_FROM_SOURCE_P (this_class))
2171 {
2172 int i;
2173 if (TYPE_BINFO (this_class))
2174 {
2175 for (i = BINFO_N_BASE_BINFOS (TYPE_BINFO (this_class)) - 1; i > 0; i--)
2176 {
2177 tree binfo = BINFO_BASE_BINFO (TYPE_BINFO (this_class), i);
2178 tree super_interface = BINFO_TYPE (binfo);
2179 tree maybe_super_interface
2180 = maybe_layout_super_class (super_interface, NULL_TREE);
2181 if (maybe_super_interface == NULL
2182 || TREE_CODE (TYPE_SIZE (maybe_super_interface)) == ERROR_MARK)
2183 {
2184 TYPE_SIZE (this_class) = error_mark_node;
2185 CLASS_BEING_LAIDOUT (this_class) = 0;
2186 class_list = TREE_CHAIN (class_list);
2187 return;
2188 }
2189 }
2190 }
2191 }
2192
2193 /* Convert the size back to an SI integer value. */
2194 TYPE_SIZE_UNIT (this_class) =
2195 fold (convert (int_type_node, TYPE_SIZE_UNIT (this_class)));
2196
2197 CLASS_BEING_LAIDOUT (this_class) = 0;
2198 class_list = TREE_CHAIN (class_list);
2199 }
2200
2201 static void
2202 add_miranda_methods (tree base_class, tree search_class)
2203 {
2204 int i;
2205 tree binfo, base_binfo;
2206
2207 if (!CLASS_PARSED_P (search_class))
2208 load_class (search_class, 1);
2209
2210 for (binfo = TYPE_BINFO (search_class), i = 1;
2211 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2212 {
2213 tree method_decl;
2214 tree elt = BINFO_TYPE (base_binfo);
2215
2216 /* FIXME: This is totally bogus. We should not be handling
2217 Miranda methods at all if we're using the BC ABI. */
2218 if (TYPE_DUMMY (elt))
2219 continue;
2220
2221 /* Ensure that interface methods are seen in declared order. */
2222 if (!CLASS_LOADED_P (elt))
2223 load_class (elt, 1);
2224 layout_class_methods (elt);
2225
2226 /* All base classes will have been laid out at this point, so the order
2227 will be correct. This code must match similar layout code in the
2228 runtime. */
2229 for (method_decl = TYPE_METHODS (elt);
2230 method_decl; method_decl = TREE_CHAIN (method_decl))
2231 {
2232 tree sig, override;
2233
2234 /* An interface can have <clinit>. */
2235 if (ID_CLINIT_P (DECL_NAME (method_decl)))
2236 continue;
2237
2238 sig = build_java_argument_signature (TREE_TYPE (method_decl));
2239 override = lookup_argument_method (base_class,
2240 DECL_NAME (method_decl), sig);
2241 if (override == NULL_TREE)
2242 {
2243 /* Found a Miranda method. Add it. */
2244 tree new_method;
2245 sig = build_java_signature (TREE_TYPE (method_decl));
2246 new_method
2247 = add_method (base_class,
2248 get_access_flags_from_decl (method_decl),
2249 DECL_NAME (method_decl), sig);
2250 METHOD_INVISIBLE (new_method) = 1;
2251 }
2252 }
2253
2254 /* Try superinterfaces. */
2255 add_miranda_methods (base_class, elt);
2256 }
2257 }
2258
2259 void
2260 layout_class_methods (tree this_class)
2261 {
2262 tree method_decl, dtable_count;
2263 tree super_class, type_name;
2264
2265 if (TYPE_NVIRTUALS (this_class))
2266 return;
2267
2268 super_class = CLASSTYPE_SUPER (this_class);
2269
2270 if (super_class)
2271 {
2272 super_class = maybe_layout_super_class (super_class, this_class);
2273 if (!TYPE_NVIRTUALS (super_class))
2274 layout_class_methods (super_class);
2275 dtable_count = TYPE_NVIRTUALS (super_class);
2276 }
2277 else
2278 dtable_count = integer_zero_node;
2279
2280 type_name = TYPE_NAME (this_class);
2281 if (!flag_indirect_dispatch
2282 && (CLASS_ABSTRACT (type_name) || CLASS_INTERFACE (type_name)))
2283 {
2284 /* An abstract class can have methods which are declared only in
2285 an implemented interface. These are called "Miranda
2286 methods". We make a dummy method entry for such methods
2287 here. */
2288 add_miranda_methods (this_class, this_class);
2289 }
2290
2291 TYPE_METHODS (this_class) = nreverse (TYPE_METHODS (this_class));
2292
2293 for (method_decl = TYPE_METHODS (this_class);
2294 method_decl; method_decl = TREE_CHAIN (method_decl))
2295 dtable_count = layout_class_method (this_class, super_class,
2296 method_decl, dtable_count);
2297
2298 TYPE_NVIRTUALS (this_class) = dtable_count;
2299 }
2300
2301 /* Return the index of METHOD in INTERFACE. This index begins at 1 and is used as an
2302 argument for _Jv_LookupInterfaceMethodIdx(). */
2303 int
2304 get_interface_method_index (tree method, tree interface)
2305 {
2306 tree meth;
2307 int i = 1;
2308
2309 for (meth = TYPE_METHODS (interface); ; meth = TREE_CHAIN (meth), i++)
2310 {
2311 if (meth == method)
2312 return i;
2313 if (meth == NULL_TREE)
2314 abort ();
2315 }
2316 }
2317
2318 /* Lay METHOD_DECL out, returning a possibly new value of
2319 DTABLE_COUNT. Also mangle the method's name. */
2320
2321 tree
2322 layout_class_method (tree this_class, tree super_class,
2323 tree method_decl, tree dtable_count)
2324 {
2325 tree method_name = DECL_NAME (method_decl);
2326
2327 TREE_PUBLIC (method_decl) = 1;
2328 /* Considered external until we know what classes are being
2329 compiled into this object file. */
2330 DECL_EXTERNAL (method_decl) = 1;
2331
2332 /* This is a good occasion to mangle the method's name */
2333 SET_DECL_ASSEMBLER_NAME (method_decl,
2334 java_mangle_decl (&temporary_obstack,
2335 method_decl));
2336 /* We don't generate a RTL for the method if it's abstract, or if
2337 it's an interface method that isn't clinit. */
2338 if (! METHOD_ABSTRACT (method_decl)
2339 || (CLASS_INTERFACE (TYPE_NAME (this_class))
2340 && (DECL_CLINIT_P (method_decl))))
2341 make_decl_rtl (method_decl);
2342
2343 if (ID_INIT_P (method_name))
2344 {
2345 const char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
2346 const char *ptr;
2347 for (ptr = p; *ptr; )
2348 {
2349 if (*ptr++ == '.')
2350 p = ptr;
2351 }
2352 DECL_CONSTRUCTOR_P (method_decl) = 1;
2353 build_java_argument_signature (TREE_TYPE (method_decl));
2354 }
2355 else if (! METHOD_STATIC (method_decl))
2356 {
2357 tree method_sig =
2358 build_java_argument_signature (TREE_TYPE (method_decl));
2359 bool method_override = false;
2360 tree super_method = lookup_argument_method (super_class, method_name,
2361 method_sig);
2362 if (super_method != NULL_TREE
2363 && ! METHOD_DUMMY (super_method))
2364 {
2365 method_override = true;
2366 if (! METHOD_PUBLIC (super_method) &&
2367 ! METHOD_PROTECTED (super_method))
2368 {
2369 /* Don't override private method, or default-access method in
2370 another package. */
2371 if (METHOD_PRIVATE (super_method) ||
2372 ! in_same_package (TYPE_NAME (this_class),
2373 TYPE_NAME (super_class)))
2374 method_override = false;
2375 }
2376 }
2377 if (method_override)
2378 {
2379 tree method_index = get_method_index (super_method);
2380 set_method_index (method_decl, method_index);
2381 if (method_index == NULL_TREE
2382 && ! flag_indirect_dispatch
2383 && !CLASS_FROM_SOURCE_P (this_class)
2384 && ! DECL_ARTIFICIAL (super_method))
2385 error ("%Jnon-static method '%D' overrides static method",
2386 method_decl, method_decl);
2387 }
2388 else if (this_class == object_type_node
2389 && (METHOD_FINAL (method_decl)
2390 || METHOD_PRIVATE (method_decl)))
2391 {
2392 /* We don't generate vtable entries for final Object
2393 methods. This is simply to save space, since every
2394 object would otherwise have to define them. */
2395 }
2396 else if (! METHOD_PRIVATE (method_decl)
2397 && dtable_count)
2398 {
2399 /* We generate vtable entries for final methods because they
2400 may one day be changed to non-final. */
2401 set_method_index (method_decl, dtable_count);
2402 dtable_count = fold (build2 (PLUS_EXPR, integer_type_node,
2403 dtable_count, integer_one_node));
2404 }
2405 }
2406
2407 return dtable_count;
2408 }
2409
2410 void
2411 register_class (void)
2412 {
2413 /* END does not need to be registered with the garbage collector
2414 because it always points into the list given by REGISTERED_CLASS,
2415 and that variable is registered with the collector. */
2416 static tree end;
2417 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
2418 tree current = copy_node (node);
2419
2420 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
2421 if (!registered_class)
2422 registered_class = current;
2423 else
2424 TREE_CHAIN (end) = current;
2425
2426 end = current;
2427 }
2428
2429 /* Emit something to register classes at start-up time.
2430
2431 The preferred mechanism is through the .jcr section, which contain
2432 a list of pointers to classes which get registered during constructor
2433 invocation time.
2434
2435 The fallback mechanism is to add statements to *LIST_P to call
2436 _Jv_RegisterClass for each class in this file. These statements will
2437 be added to a static constructor function for this translation unit. */
2438
2439 void
2440 emit_register_classes (tree *list_p)
2441 {
2442 if (registered_class == NULL)
2443 return;
2444
2445 /* TARGET_USE_JCR_SECTION defaults to 1 if SUPPORTS_WEAK and
2446 TARGET_ASM_NAMED_SECTION, else 0. Some targets meet those conditions
2447 but lack suitable crtbegin/end objects or linker support. These
2448 targets can overide the default in tm.h to use the fallback mechanism. */
2449 if (TARGET_USE_JCR_SECTION)
2450 {
2451 #ifdef JCR_SECTION_NAME
2452 tree t;
2453 named_section_flags (JCR_SECTION_NAME, SECTION_WRITE);
2454 assemble_align (POINTER_SIZE);
2455 for (t = registered_class; t; t = TREE_CHAIN (t))
2456 {
2457 mark_decl_referenced (t);
2458 assemble_integer (XEXP (DECL_RTL (t), 0),
2459 POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2460 }
2461 #else
2462 /* A target has defined TARGET_USE_JCR_SECTION, but doesn't have a
2463 JCR_SECTION_NAME. */
2464 abort ();
2465 #endif
2466 }
2467 else
2468 {
2469 tree klass, t, register_class_fn;
2470
2471 t = build_function_type_list (void_type_node, class_ptr_type, NULL);
2472 t = build_decl (FUNCTION_DECL, get_identifier ("_Jv_RegisterClass"), t);
2473 TREE_PUBLIC (t) = 1;
2474 DECL_EXTERNAL (t) = 1;
2475 register_class_fn = t;
2476
2477 for (klass = registered_class; klass; klass = TREE_CHAIN (klass))
2478 {
2479 t = build_fold_addr_expr (klass);
2480 t = tree_cons (NULL, t, NULL);
2481 t = build_function_call_expr (register_class_fn, t);
2482 append_to_statement_list (t, list_p);
2483 }
2484 }
2485 }
2486
2487 /* Make a symbol_type (_Jv_MethodSymbol) node for DECL. */
2488
2489 static tree
2490 build_symbol_entry (tree decl)
2491 {
2492 tree clname, name, signature, sym;
2493 clname = build_utf8_ref (DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))));
2494 /* ??? Constructors are given the name foo.foo all the way through
2495 the compiler, but in the method table they're all renamed
2496 foo.<init>. So, we have to do the same here unless we want an
2497 unresolved reference at runtime. */
2498 name = build_utf8_ref ((TREE_CODE (decl) == FUNCTION_DECL
2499 && DECL_CONSTRUCTOR_P (decl))
2500 ? init_identifier_node
2501 : DECL_NAME (decl));
2502 signature = build_java_signature (TREE_TYPE (decl));
2503 signature = build_utf8_ref (unmangle_classname
2504 (IDENTIFIER_POINTER (signature),
2505 IDENTIFIER_LENGTH (signature)));
2506
2507 START_RECORD_CONSTRUCTOR (sym, symbol_type);
2508 PUSH_FIELD_VALUE (sym, "clname", clname);
2509 PUSH_FIELD_VALUE (sym, "name", name);
2510 PUSH_FIELD_VALUE (sym, "signature", signature);
2511 FINISH_RECORD_CONSTRUCTOR (sym);
2512 TREE_CONSTANT (sym) = 1;
2513 TREE_INVARIANT (sym) = 1;
2514
2515 return sym;
2516 }
2517
2518 /* Emit a symbol table: used by -findirect-dispatch. */
2519
2520 tree
2521 emit_symbol_table (tree name, tree the_table, tree decl_list,
2522 tree the_syms_decl, tree the_array_element_type,
2523 int element_size)
2524 {
2525 tree method_list, method, table, list, null_symbol;
2526 tree table_size, the_array_type;
2527 int index;
2528
2529 /* Only emit a table if this translation unit actually made any
2530 references via it. */
2531 if (decl_list == NULL_TREE)
2532 return the_table;
2533
2534 /* Build a list of _Jv_MethodSymbols for each entry in otable_methods. */
2535 index = 0;
2536 method_list = decl_list;
2537 list = NULL_TREE;
2538 while (method_list != NULL_TREE)
2539 {
2540 method = TREE_VALUE (method_list);
2541 list = tree_cons (NULL_TREE, build_symbol_entry (method), list);
2542 method_list = TREE_CHAIN (method_list);
2543 index++;
2544 }
2545
2546 /* Terminate the list with a "null" entry. */
2547 START_RECORD_CONSTRUCTOR (null_symbol, symbol_type);
2548 PUSH_FIELD_VALUE (null_symbol, "clname", null_pointer_node);
2549 PUSH_FIELD_VALUE (null_symbol, "name", null_pointer_node);
2550 PUSH_FIELD_VALUE (null_symbol, "signature", null_pointer_node);
2551 FINISH_RECORD_CONSTRUCTOR (null_symbol);
2552 TREE_CONSTANT (null_symbol) = 1;
2553 TREE_INVARIANT (null_symbol) = 1;
2554 list = tree_cons (NULL_TREE, null_symbol, list);
2555
2556 /* Put the list in the right order and make it a constructor. */
2557 list = nreverse (list);
2558 table = build_constructor (symbols_array_type, list);
2559
2560 /* Make it the initial value for otable_syms and emit the decl. */
2561 DECL_INITIAL (the_syms_decl) = table;
2562 DECL_ARTIFICIAL (the_syms_decl) = 1;
2563 DECL_IGNORED_P (the_syms_decl) = 1;
2564 rest_of_decl_compilation (the_syms_decl, 1, 0);
2565
2566 /* Now that its size is known, redefine the table as an
2567 uninitialized static array of INDEX + 1 elements. The extra entry
2568 is used by the runtime to track whether the table has been
2569 initialized. */
2570 table_size
2571 = build_index_type (build_int_cst (NULL_TREE, index * element_size + 1));
2572 the_array_type = build_array_type (the_array_element_type, table_size);
2573 the_table = build_decl (VAR_DECL, name, the_array_type);
2574 TREE_STATIC (the_table) = 1;
2575 TREE_READONLY (the_table) = 1;
2576 rest_of_decl_compilation (the_table, 1, 0);
2577
2578 return the_table;
2579 }
2580
2581 /* Make an entry for the catch_classes list. */
2582 tree
2583 make_catch_class_record (tree catch_class, tree classname)
2584 {
2585 tree entry;
2586 tree type = TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (output_class)));
2587 START_RECORD_CONSTRUCTOR (entry, type);
2588 PUSH_FIELD_VALUE (entry, "address", catch_class);
2589 PUSH_FIELD_VALUE (entry, "classname", classname);
2590 FINISH_RECORD_CONSTRUCTOR (entry);
2591 return entry;
2592 }
2593
2594
2595 /* Generate the list of Throwable classes that are caught by exception
2596 handlers in this class. */
2597 tree
2598 emit_catch_table (tree this_class)
2599 {
2600 tree table, table_size, array_type;
2601 TYPE_CATCH_CLASSES (this_class) =
2602 tree_cons (NULL,
2603 make_catch_class_record (null_pointer_node, null_pointer_node),
2604 TYPE_CATCH_CLASSES (this_class));
2605 TYPE_CATCH_CLASSES (this_class) = nreverse (TYPE_CATCH_CLASSES (this_class));
2606 TYPE_CATCH_CLASSES (this_class) =
2607 tree_cons (NULL,
2608 make_catch_class_record (null_pointer_node, null_pointer_node),
2609 TYPE_CATCH_CLASSES (this_class));
2610 table_size = build_index_type
2611 (build_int_cst (NULL_TREE,
2612 list_length (TYPE_CATCH_CLASSES (this_class))));
2613 array_type
2614 = build_array_type (TREE_TYPE (TREE_TYPE (TYPE_CTABLE_DECL (this_class))),
2615 table_size);
2616 table =
2617 build_decl (VAR_DECL, DECL_NAME (TYPE_CTABLE_DECL (this_class)), array_type);
2618 DECL_INITIAL (table) =
2619 build_constructor (array_type, TYPE_CATCH_CLASSES (this_class));
2620 TREE_STATIC (table) = 1;
2621 TREE_READONLY (table) = 1;
2622 DECL_IGNORED_P (table) = 1;
2623 rest_of_decl_compilation (table, 1, 0);
2624 return table;
2625 }
2626
2627 /* Given a type, return the signature used by
2628 _Jv_FindClassFromSignature() in libgcj. This isn't exactly the
2629 same as build_java_signature() because we want the canonical array
2630 type. */
2631
2632 static tree
2633 build_signature_for_libgcj (tree type)
2634 {
2635 tree sig, ref;
2636
2637 sig = build_java_signature (type);
2638 ref = build_utf8_ref (unmangle_classname (IDENTIFIER_POINTER (sig),
2639 IDENTIFIER_LENGTH (sig)));
2640 return ref;
2641 }
2642
2643 /* Add an entry to the type assertion table. Callback used during hashtable
2644 traversal. */
2645
2646 static int
2647 add_assertion_table_entry (void **htab_entry, void *ptr)
2648 {
2649 tree entry;
2650 tree code_val, op1_utf8, op2_utf8;
2651 tree *list = (tree *) ptr;
2652 type_assertion *as = (type_assertion *) *htab_entry;
2653
2654 code_val = build_int_cst (NULL_TREE, as->assertion_code);
2655
2656 if (as->op1 == NULL_TREE)
2657 op1_utf8 = null_pointer_node;
2658 else
2659 op1_utf8 = build_signature_for_libgcj (as->op1);
2660
2661 if (as->op2 == NULL_TREE)
2662 op2_utf8 = null_pointer_node;
2663 else
2664 op2_utf8 = build_signature_for_libgcj (as->op2);
2665
2666 START_RECORD_CONSTRUCTOR (entry, assertion_entry_type);
2667 PUSH_FIELD_VALUE (entry, "assertion_code", code_val);
2668 PUSH_FIELD_VALUE (entry, "op1", op1_utf8);
2669 PUSH_FIELD_VALUE (entry, "op2", op2_utf8);
2670 FINISH_RECORD_CONSTRUCTOR (entry);
2671
2672 *list = tree_cons (NULL_TREE, entry, *list);
2673 return true;
2674 }
2675
2676 /* Generate the type assertion table for CLASS, and return its DECL. */
2677
2678 static tree
2679 emit_assertion_table (tree class)
2680 {
2681 tree null_entry, ctor, table_decl;
2682 tree list = NULL_TREE;
2683 htab_t assertions_htab = TYPE_ASSERTIONS (class);
2684
2685 /* Iterate through the hash table. */
2686 htab_traverse (assertions_htab, add_assertion_table_entry, &list);
2687
2688 /* Finish with a null entry. */
2689 START_RECORD_CONSTRUCTOR (null_entry, assertion_entry_type);
2690 PUSH_FIELD_VALUE (null_entry, "assertion_code", integer_zero_node);
2691 PUSH_FIELD_VALUE (null_entry, "op1", null_pointer_node);
2692 PUSH_FIELD_VALUE (null_entry, "op2", null_pointer_node);
2693 FINISH_RECORD_CONSTRUCTOR (null_entry);
2694
2695 list = tree_cons (NULL_TREE, null_entry, list);
2696
2697 /* Put the list in the right order and make it a constructor. */
2698 list = nreverse (list);
2699 ctor = build_constructor (assertion_table_type, list);
2700
2701 table_decl = build_decl (VAR_DECL, mangled_classname ("_type_assert_", class),
2702 assertion_table_type);
2703
2704 TREE_STATIC (table_decl) = 1;
2705 TREE_READONLY (table_decl) = 1;
2706 TREE_CONSTANT (table_decl) = 1;
2707 DECL_IGNORED_P (table_decl) = 1;
2708
2709 DECL_INITIAL (table_decl) = ctor;
2710 DECL_ARTIFICIAL (table_decl) = 1;
2711 rest_of_decl_compilation (table_decl, 1, 0);
2712
2713 return table_decl;
2714 }
2715
2716 void
2717 init_class_processing (void)
2718 {
2719 fields_ident = get_identifier ("fields");
2720 info_ident = get_identifier ("info");
2721
2722 gcc_obstack_init (&temporary_obstack);
2723 }
2724 \f
2725 static hashval_t java_treetreehash_hash (const void *);
2726 static int java_treetreehash_compare (const void *, const void *);
2727
2728 /* A hash table mapping trees to trees. Used generally. */
2729
2730 #define JAVA_TREEHASHHASH_H(t) (htab_hash_pointer (t))
2731
2732 static hashval_t
2733 java_treetreehash_hash (const void *k_p)
2734 {
2735 struct treetreehash_entry *k = (struct treetreehash_entry *) k_p;
2736 return JAVA_TREEHASHHASH_H (k->key);
2737 }
2738
2739 static int
2740 java_treetreehash_compare (const void * k1_p, const void * k2_p)
2741 {
2742 struct treetreehash_entry * k1 = (struct treetreehash_entry *) k1_p;
2743 tree k2 = (tree) k2_p;
2744 return (k1->key == k2);
2745 }
2746
2747 tree
2748 java_treetreehash_find (htab_t ht, tree t)
2749 {
2750 struct treetreehash_entry *e;
2751 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2752 e = htab_find_with_hash (ht, t, hv);
2753 if (e == NULL)
2754 return NULL;
2755 else
2756 return e->value;
2757 }
2758
2759 tree *
2760 java_treetreehash_new (htab_t ht, tree t)
2761 {
2762 void **e;
2763 struct treetreehash_entry *tthe;
2764 hashval_t hv = JAVA_TREEHASHHASH_H (t);
2765
2766 e = htab_find_slot_with_hash (ht, t, hv, INSERT);
2767 if (*e == NULL)
2768 {
2769 tthe = (*ht->alloc_f) (1, sizeof (*tthe));
2770 tthe->key = t;
2771 *e = tthe;
2772 }
2773 else
2774 tthe = (struct treetreehash_entry *) *e;
2775 return &tthe->value;
2776 }
2777
2778 htab_t
2779 java_treetreehash_create (size_t size, int gc)
2780 {
2781 if (gc)
2782 return htab_create_ggc (size, java_treetreehash_hash,
2783 java_treetreehash_compare, NULL);
2784 else
2785 return htab_create_alloc (size, java_treetreehash_hash,
2786 java_treetreehash_compare, free, xcalloc, free);
2787 }
2788
2789 /* Break down qualified IDENTIFIER into package and class-name components.
2790 For example, given SOURCE "pkg.foo.Bar", LEFT will be set to
2791 "pkg.foo", and RIGHT to "Bar". */
2792
2793 int
2794 split_qualified_name (tree *left, tree *right, tree source)
2795 {
2796 char *p, *base;
2797 int l = IDENTIFIER_LENGTH (source);
2798
2799 base = alloca (l + 1);
2800 memcpy (base, IDENTIFIER_POINTER (source), l + 1);
2801
2802 /* Breakdown NAME into REMAINDER . IDENTIFIER. */
2803 p = base + l - 1;
2804 while (*p != '.' && p != base)
2805 p--;
2806
2807 /* We didn't find a '.'. Return an error. */
2808 if (p == base)
2809 return 1;
2810
2811 *p = '\0';
2812 if (right)
2813 *right = get_identifier (p+1);
2814 *left = get_identifier (base);
2815
2816 return 0;
2817 }
2818
2819 /* Given two classes (TYPE_DECL) or class names (IDENTIFIER), return TRUE
2820 if the classes are from the same package. */
2821
2822 int
2823 in_same_package (tree name1, tree name2)
2824 {
2825 tree tmp;
2826 tree pkg1;
2827 tree pkg2;
2828
2829 if (TREE_CODE (name1) == TYPE_DECL)
2830 name1 = DECL_NAME (name1);
2831 if (TREE_CODE (name2) == TYPE_DECL)
2832 name2 = DECL_NAME (name2);
2833
2834 if (QUALIFIED_P (name1) != QUALIFIED_P (name2))
2835 /* One in empty package. */
2836 return 0;
2837
2838 if (QUALIFIED_P (name1) == 0 && QUALIFIED_P (name2) == 0)
2839 /* Both in empty package. */
2840 return 1;
2841
2842 split_qualified_name (&pkg1, &tmp, name1);
2843 split_qualified_name (&pkg2, &tmp, name2);
2844
2845 return (pkg1 == pkg2);
2846 }
2847
2848 #include "gt-java-class.h"