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