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