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