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