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