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