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