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