* class.c (make_class_data): Renamed fields: nmethods to
[official-gcc.git] / gcc / java / class.c
blobd0fb94b26240b1f08b5b73cf6ecf51b6506330f2
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Per Bothner <bothner@cygnus.com> */
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "java-tree.h"
32 #include "jcf.h"
33 #include "obstack.h"
34 #include "toplev.h"
36 static tree mangle_class_field PROTO ((tree class));
38 static rtx registerClass_libfunc;
40 extern struct obstack permanent_obstack;
41 extern struct obstack temporary_obstack;
43 /* Return an IDENTIFIER_NODE the same as (OLD_NAME, OLD_LENGTH).
44 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
45 Also, PREFIX is prepended, and SUFFIX is appended. */
47 tree
48 ident_subst (old_name, old_length, prefix, old_char, new_char, suffix)
49 const char* old_name;
50 int old_length;
51 const char *prefix;
52 int old_char;
53 int new_char;
54 const char *suffix;
56 int prefix_len = strlen (prefix);
57 int suffix_len = strlen (suffix);
58 int i = prefix_len + old_length + suffix_len + 1;
59 #ifdef __GNUC__
60 char buffer[i];
61 #else
62 char *buffer = (char *)alloca (i);
63 #endif
64 strcpy (buffer, prefix);
65 for (i = 0; i < old_length; i++)
67 char ch = old_name[i];
68 if (ch == old_char)
69 ch = new_char;
70 buffer[prefix_len + i] = ch;
72 strcpy (buffer + prefix_len + old_length, suffix);
73 return get_identifier (buffer);
76 /* Return an IDENTIFIER_NODE the same as OLD_ID,
77 except that characters matching OLD_CHAR are substituted by NEW_CHAR.
78 Also, PREFIX is prepended, and SUFFIX is appended. */
80 tree
81 identifier_subst (old_id, prefix, old_char, new_char, suffix)
82 const tree old_id;
83 const char *prefix;
84 int old_char;
85 int new_char;
86 const char *suffix;
88 return ident_subst (IDENTIFIER_POINTER (old_id), IDENTIFIER_LENGTH (old_id),
89 prefix, old_char, new_char, suffix);
92 /* Generate a valid C identifier from the name of the class TYPE,
93 prefixed by PREFIX. */
95 tree
96 mangled_classname (prefix, type)
97 char *prefix;
98 tree type;
100 tree ident = TYPE_NAME (type);
101 if (TREE_CODE (ident) != IDENTIFIER_NODE)
102 ident = DECL_NAME (ident);
103 return identifier_subst (ident, prefix, '/', '_', "");
106 tree
107 make_class ()
109 tree type;
110 push_obstacks (&permanent_obstack, &permanent_obstack);
111 type = make_node (RECORD_TYPE);
112 #ifdef JAVA_USE_HANDLES
113 tree field1 = build_decl (FIELD_DECL, get_identifier ("obj"),
114 build_pointer_type (type));
115 tree field2 = build_decl (FIELD_DECL, get_identifier ("methods"),
116 methodtable_ptr_type);
117 tree handle_type = make_node (RECORD_TYPE);
118 TREE_CHAIN (field1) = field2;
119 TYPE_FIELDS (handle_type) = field1;
120 TYPE_BINFO (type) = make_tree_vec (7);
121 TYPE_BINFO (handle_type) = make_tree_vec (7);
122 BINFO_HANDLE (TYPE_BINFO (handle_type)) = type;
123 BINFO_HANDLE (TYPE_BINFO (type)) = handle_type;
124 #else
125 TYPE_BINFO (type) = make_tree_vec (6);
126 #endif
127 CLASS_P (type) = 1;
128 pop_obstacks ();
130 return type;
133 /* Given a fully-qualified classname in NAME (whose length is NAME_LENGTH),
134 and where each of the constituents is separated by '/',
135 return a corresponding IDENTIFIER_NODE, except using '.' as separator. */
137 tree
138 unmangle_classname (name, name_length)
139 const char *name; int name_length;
141 return ident_subst (name, name_length, "", '/', '.', "");
144 tree
145 push_class (class_type, class_name)
146 tree class_type, class_name;
148 tree decl, signature;
149 char *save_input_filename = input_filename;
150 int save_lineno = lineno;
151 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
152 push_obstacks (&permanent_obstack, &permanent_obstack);
153 input_filename = IDENTIFIER_POINTER (source_name);
154 lineno = 0;
155 decl = build_decl (TYPE_DECL, class_name, class_type);
156 input_filename = save_input_filename;
157 lineno = save_lineno;
158 signature = identifier_subst (class_name, "L", '.', '/', ";");
159 IDENTIFIER_SIGNATURE_TYPE (signature) = build_pointer_type (class_type);
161 /* Setting DECL_ARTIFICAL forces dbxout.c to specific the type is
162 both a typedef and in the struct name-space. We may want to re-visit
163 this later, but for now it reduces the changes needed for gdb. */
164 DECL_ARTIFICIAL (decl) = 1;
166 pushdecl_top_level (decl);
167 #ifdef JAVA_USE_HANDLES
169 tree handle_name = identifier_subst (class_name,
170 "Handle$", '.', '.', "");
171 tree handle_decl = build_decl (TYPE_DECL, handle_name,
172 CLASS_TO_HANDLE_TYPE (class_type));
173 pushdecl (handle_decl);
175 #endif
177 pop_obstacks ();
178 return decl;
181 /* Finds the (global) class named NAME. Creates the class if not found.
182 Also creates associated TYPE_DECL.
183 Does not check if the class actually exists, load the class,
184 fill in field or methods, or do layout_type. */
186 tree
187 lookup_class (name)
188 tree name;
190 tree decl = IDENTIFIER_CLASS_VALUE (name);
191 if (decl == NULL_TREE)
192 decl = push_class (make_class (), name);
193 return TREE_TYPE (decl);
196 void
197 set_super_info (access_flags, this_class, super_class, interfaces_count)
198 int access_flags;
199 tree this_class;
200 tree super_class;
201 int interfaces_count;
203 int total_supers = interfaces_count;
204 tree class_decl = TYPE_NAME (this_class);
205 if (super_class)
206 total_supers++;
208 push_obstacks (&permanent_obstack, &permanent_obstack);
209 TYPE_BINFO_BASETYPES (this_class) = make_tree_vec (total_supers);
210 if (super_class)
212 tree super_binfo = make_tree_vec (6);
213 BINFO_TYPE (super_binfo) = super_class;
214 BINFO_OFFSET (super_binfo) = integer_zero_node;
215 TREE_VIA_PUBLIC (super_binfo) = 1;
216 TREE_VEC_ELT (BINFO_BASETYPES (TYPE_BINFO (this_class)), 0)
217 = super_binfo;
218 CLASS_HAS_SUPER (this_class) = 1;
220 pop_obstacks ();
222 if (access_flags & ACC_PUBLIC) CLASS_PUBLIC (class_decl) = 1;
223 if (access_flags & ACC_FINAL) CLASS_FINAL (class_decl) = 1;
224 if (access_flags & ACC_SUPER) CLASS_SUPER (class_decl) = 1;
225 if (access_flags & ACC_INTERFACE) CLASS_INTERFACE (class_decl) = 1;
226 if (access_flags & ACC_ABSTRACT) CLASS_ABSTRACT (class_decl) = 1;
229 /* Return length of inheritance chain of CLAS, where java.lang.Object is 0,
230 direct sub-classes of Object are 1, and so on. */
233 class_depth (clas)
234 tree clas;
236 int depth = 0;
237 if (! CLASS_LOADED_P (clas))
238 load_class (clas, 1);
239 while (clas != object_type_node)
241 depth++;
242 clas = TYPE_BINFO_BASETYPE (clas, 0);
244 return depth;
247 /* Return true iff TYPE2 is an interface that extends interface TYPE1 */
250 interface_of_p (type1, type2)
251 tree type1, type2;
253 int n, i;
254 tree basetype_vec;
256 if (!(basetype_vec = TYPE_BINFO_BASETYPES (type2)))
257 return 0;
258 n = TREE_VEC_LENGTH (basetype_vec);
259 for (i = 0; i < n; i++)
261 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
262 if (vec_elt && BINFO_TYPE (vec_elt) == type1)
263 return 1;
265 for (i = 0; i < n; i++)
267 tree vec_elt = TREE_VEC_ELT (basetype_vec, i);
268 if (vec_elt && BINFO_TYPE (vec_elt)
269 && interface_of_p (type1, BINFO_TYPE (vec_elt)))
270 return 1;
272 return 0;
275 /* Return true iff TYPE1 inherits from TYPE2. */
278 inherits_from_p (type1, type2)
279 tree type1, type2;
281 while (type1 != NULL_TREE && TREE_CODE (type1) == RECORD_TYPE)
283 if (type1 == type2)
284 return 1;
285 type1 = CLASSTYPE_SUPER (type1);
287 return 0;
290 static void
291 add_interface_do (basetype_vec, interface_class, i)
292 tree basetype_vec, interface_class;
293 int i;
295 tree interface_binfo = make_tree_vec (6);
296 BINFO_TYPE (interface_binfo) = interface_class;
297 BINFO_OFFSET (interface_binfo) = integer_zero_node;
298 TREE_VIA_VIRTUAL (interface_binfo) = 1;
299 TREE_VIA_PUBLIC (interface_binfo) = 1;
300 TREE_VEC_ELT (basetype_vec, i) = interface_binfo;
303 /* Add INTERFACE_CLASS to THIS_CLASS iff INTERFACE_CLASS can't be
304 found in THIS_CLASS. Returns NULL_TREE upon success, INTERFACE_CLASS
305 if attempt is made to add it twice. */
307 tree
308 maybe_add_interface (this_class, interface_class)
309 tree this_class, interface_class;
311 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
312 tree interface_binfo = make_tree_vec (6);
313 int i;
314 int n = TREE_VEC_LENGTH (basetype_vec);
315 for (i = 0; ; i++)
317 if (i >= n)
319 error ("internal error - too many interface type");
320 return NULL_TREE;
322 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
323 break;
324 else if (BINFO_TYPE (TREE_VEC_ELT (basetype_vec, i)) == interface_class)
325 return interface_class;
327 add_interface_do (basetype_vec, interface_class, i);
328 return NULL_TREE;
331 /* Add the INTERFACE_CLASS as one of the interfaces of THIS_CLASS. */
333 void
334 add_interface (this_class, interface_class)
335 tree this_class, interface_class;
337 tree basetype_vec = TYPE_BINFO_BASETYPES (this_class);
338 int i;
339 int n = TREE_VEC_LENGTH (basetype_vec);
340 for (i = 0; ; i++)
342 if (i >= n)
344 error ("internal error - too many interface type");
345 return;
347 else if (TREE_VEC_ELT (basetype_vec, i) == NULL_TREE)
348 break;
350 add_interface_do (basetype_vec, interface_class, i);
353 /* Return the address of a pointer to the first FUNCTION_DECL
354 in the list (*LIST) whose DECL_NAME is NAME. */
356 static tree *
357 find_named_method (list, name)
358 tree *list;
359 tree name;
361 while (*list && DECL_NAME (*list) != name)
362 list = &TREE_CHAIN (*list);
363 return list;
366 tree
367 build_java_method_type (fntype, this_class, access_flags)
368 tree fntype;
369 tree this_class;
370 int access_flags;
372 if (access_flags & ACC_STATIC)
373 return fntype;
374 return build_method_type (CLASS_TO_HANDLE_TYPE (this_class), fntype);
377 tree
378 add_method_1 (handle_class, access_flags, name, function_type)
379 tree handle_class;
380 int access_flags;
381 tree name;
382 tree function_type;
384 tree method_type, fndecl;
385 push_obstacks (&permanent_obstack, &permanent_obstack);
387 method_type = build_java_method_type (function_type,
388 handle_class, access_flags);
390 fndecl = build_decl (FUNCTION_DECL, name, method_type);
391 DECL_CONTEXT (fndecl) = handle_class;
393 DECL_LANG_SPECIFIC (fndecl)
394 = (struct lang_decl *) permalloc (sizeof (struct lang_decl));
395 bzero (DECL_LANG_SPECIFIC (fndecl), sizeof (struct lang_decl));
397 TREE_CHAIN (fndecl) = TYPE_METHODS (handle_class);
398 TYPE_METHODS (handle_class) = fndecl;
399 pop_obstacks ();
401 if (access_flags & ACC_PUBLIC) METHOD_PUBLIC (fndecl) = 1;
402 if (access_flags & ACC_PROTECTED) METHOD_PROTECTED (fndecl) = 1;
403 if (access_flags & ACC_PRIVATE) METHOD_PRIVATE (fndecl) = 1;
404 if (access_flags & ACC_NATIVE) METHOD_NATIVE (fndecl) = 1;
405 if (access_flags & ACC_STATIC) METHOD_STATIC (fndecl) = 1;
406 if (access_flags & ACC_FINAL) METHOD_FINAL (fndecl) = 1;
407 if (access_flags & ACC_SYNCHRONIZED) METHOD_SYNCHRONIZED (fndecl) = 1;
408 if (access_flags & ACC_ABSTRACT) METHOD_ABSTRACT (fndecl) = 1;
409 if (access_flags & ACC_TRANSIENT) METHOD_TRANSIENT (fndecl) = 1;
410 return fndecl;
413 /* Add a method to THIS_CLASS.
414 The method's name is NAME.
415 Its signature (mangled type) is METHOD_SIG (an IDENTIFIER_NODE). */
417 tree
418 add_method (this_class, access_flags, name, method_sig)
419 tree this_class;
420 int access_flags;
421 tree name;
422 tree method_sig;
424 tree handle_class = CLASS_TO_HANDLE_TYPE (this_class);
425 tree function_type, method_type, fndecl;
426 unsigned char *sig = (unsigned char*)IDENTIFIER_POINTER (method_sig);
427 push_obstacks (&permanent_obstack, &permanent_obstack);
428 if (sig[0] != '(')
429 fatal ("bad method signature");
430 function_type = get_type_from_signature (method_sig);
431 fndecl = add_method_1 (handle_class, access_flags, name, function_type);
432 set_java_signature (TREE_TYPE (fndecl), method_sig);
433 pop_obstacks ();
434 return fndecl;
437 tree
438 add_field (class, name, field_type, flags)
439 tree class;
440 tree name;
441 tree field_type;
442 int flags;
444 int is_static = (flags & ACC_STATIC) != 0;
445 tree field;
446 /* Push the obstack of field_type ? FIXME */
447 push_obstacks (&permanent_obstack, &permanent_obstack);
448 field = build_decl (is_static ? VAR_DECL : FIELD_DECL, name, field_type);
449 pop_obstacks ();
450 TREE_CHAIN (field) = TYPE_FIELDS (class);
451 TYPE_FIELDS (class) = field;
452 DECL_CONTEXT (field) = class;
454 if (flags & ACC_PUBLIC) FIELD_PUBLIC (field) = 1;
455 if (flags & ACC_PROTECTED) FIELD_PROTECTED (field) = 1;
456 if (flags & ACC_PRIVATE) FIELD_PRIVATE (field) = 1;
457 if (flags & ACC_FINAL) FIELD_FINAL (field) = 1;
458 if (flags & ACC_VOLATILE) FIELD_VOLATILE (field) = 1;
459 if (flags & ACC_TRANSIENT) FIELD_TRANSIENT (field) = 1;
460 if (is_static)
462 FIELD_STATIC (field) = 1;
463 /* Always make field externally visible. This is required so
464 that native methods can always access the field. */
465 TREE_PUBLIC (field) = 1;
467 return field;
470 /* Associate a constant value CONSTANT with VAR_DECL FIELD. */
472 void
473 set_constant_value (field, constant)
474 tree field, constant;
476 if (field == NULL_TREE)
477 warning ("misplaced ConstantValue attribute (not in any field)");
478 else if (DECL_INITIAL (field) != NULL_TREE)
479 warning ("duplicate ConstanValue atribute for field '%s'",
480 IDENTIFIER_POINTER (DECL_NAME (field)));
481 else
482 DECL_INITIAL (field) = constant;
485 /* Count the number of Unicode chars encoded in a given Ut8 string. */
488 strLengthUtf8 (str, len)
489 char *str;
490 int len;
492 register unsigned char* ptr = (unsigned char*) str;
493 register unsigned char *limit = ptr + len;
494 int str_length = 0;
495 for (; ptr < limit; str_length++) {
496 if (UTF8_GET (ptr, limit) < 0)
497 return -1;
499 return str_length;
503 /* Calculate a hash value for a string encoded in Utf8 format.
504 * This returns the same hash value as specified for java.lang.String.hashCode.
507 int32
508 hashUtf8String (str, len)
509 char *str;
510 int len;
512 register unsigned char* ptr = (unsigned char*) str;
513 register unsigned char *limit = ptr + len;
514 int32 hash = 0;
515 for (; ptr < limit;)
517 int ch = UTF8_GET (ptr, limit);
518 /* Updated specification from
519 http://www.javasoft.com/docs/books/jls/clarify.html. */
520 hash = (31 * hash) + ch;
522 return hash;
525 tree utf8_decl_list = NULL_TREE;
527 tree
528 build_utf8_ref (name)
529 tree name;
531 char* name_ptr = IDENTIFIER_POINTER(name);
532 int name_len = IDENTIFIER_LENGTH(name);
533 char buf[60];
534 char *buf_ptr;
535 tree ctype, field, str_type, cinit, string;
536 static int utf8_count = 0;
537 int name_hash;
538 tree ref = IDENTIFIER_UTF8_REF (name);
539 tree decl;
540 if (ref != NULL_TREE)
541 return ref;
543 push_obstacks (&permanent_obstack, &permanent_obstack);
544 ctype = make_node (RECORD_TYPE);
545 str_type = build_prim_array_type (unsigned_byte_type_node,
546 name_len + 1); /* Allow for final '\0'. */
547 PUSH_FIELD (ctype, field, "hash", unsigned_short_type_node);
548 PUSH_FIELD (ctype, field, "length", unsigned_short_type_node);
549 PUSH_FIELD (ctype, field, "data", str_type);
550 FINISH_RECORD (ctype);
551 START_RECORD_CONSTRUCTOR (cinit, ctype);
552 name_hash = hashUtf8String (name_ptr, name_len) & 0xFFFF;
553 PUSH_FIELD_VALUE (cinit, "hash", build_int_2 (name_hash, 0));
554 PUSH_FIELD_VALUE (cinit, "length", build_int_2 (name_len, 0));
555 string = build_string (name_len, name_ptr);
556 TREE_TYPE (string) = str_type;
557 PUSH_FIELD_VALUE (cinit, "data", string);
558 FINISH_RECORD_CONSTRUCTOR (cinit);
560 /* Build a unique identifier based on buf. */
561 sprintf(buf, "_Utf%d", ++utf8_count);
562 buf_ptr = &buf[strlen (buf)];
563 while (--name_len >= 0)
565 char c = *name_ptr++;
566 if (c & 0x80)
567 continue;
568 if (!isalpha(c) && !isdigit(c))
569 c = '_';
570 *buf_ptr++ = c;
571 if (buf_ptr >= buf + 50)
572 break;
574 *buf_ptr = '\0';
576 decl = build_decl (VAR_DECL, get_identifier (buf), utf8const_type);
577 /* FIXME get some way to force this into .text, not .data. */
578 TREE_STATIC (decl) = 1;
579 DECL_ARTIFICIAL (decl) = 1;
580 DECL_IGNORED_P (decl) = 1;
581 TREE_READONLY (decl) = 1;
582 DECL_INITIAL (decl) = cinit;
583 TREE_CHAIN (decl) = utf8_decl_list;
584 layout_decl (decl, 0);
585 pushdecl (decl);
586 rest_of_decl_compilation (decl, (char*) 0, global_bindings_p (), 0);
587 utf8_decl_list = decl;
588 make_decl_rtl (decl, (char*) 0, 1);
589 ref = build1 (ADDR_EXPR, utf8const_ptr_type, decl);
590 IDENTIFIER_UTF8_REF (name) = ref;
591 pop_obstacks ();
592 return ref;
595 /* Build a reference to the class TYPE.
596 Also handles primitive types and array types. */
598 tree
599 build_class_ref (type)
600 tree type;
602 int is_compiled = is_compiled_class (type);
603 if (is_compiled)
605 tree ref, decl_name, decl;
606 if (TREE_CODE (type) == POINTER_TYPE)
607 type = TREE_TYPE (type);
608 if (TREE_CODE (type) == RECORD_TYPE)
610 if (TYPE_SIZE (type) == error_mark_node)
611 return null_pointer_node;
612 decl_name = identifier_subst (DECL_NAME (TYPE_NAME (type)),
613 "", '/', '/', ".class");
614 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
615 if (decl == NULL_TREE)
617 push_obstacks (&permanent_obstack, &permanent_obstack);
618 decl = build_decl (VAR_DECL, decl_name, class_type_node);
619 DECL_SIZE (decl) = TYPE_SIZE (class_type_node);
620 TREE_STATIC (decl) = 1;
621 TREE_PUBLIC (decl) = 1;
622 DECL_IGNORED_P (decl) = 1;
623 DECL_ARTIFICIAL (decl) = 1;
624 DECL_ASSEMBLER_NAME (decl) = mangle_class_field (type);
625 make_decl_rtl (decl, NULL, 1);
626 pushdecl_top_level (decl);
627 if (is_compiled == 1)
628 DECL_EXTERNAL (decl) = 1;
629 pop_obstacks ();
632 else
634 char *name;
635 char buffer[20];
636 decl_name = TYPE_NAME (type);
637 if (TREE_CODE (decl_name) == TYPE_DECL)
638 decl_name = DECL_NAME (decl_name);
639 name = IDENTIFIER_POINTER (decl_name);
640 if (strncmp (name, "promoted_", 9) == 0)
641 name += 9;
642 sprintf (buffer, "%sClass", name);
643 decl_name = get_identifier (buffer);
644 decl = IDENTIFIER_GLOBAL_VALUE (decl_name);
645 if (decl == NULL_TREE)
647 push_obstacks (&permanent_obstack, &permanent_obstack);
648 decl = build_decl (VAR_DECL, decl_name, class_type_node);
649 TREE_STATIC (decl) = 1;
650 TREE_PUBLIC (decl) = 1;
651 make_decl_rtl (decl, NULL, 1);
652 pushdecl_top_level (decl);
653 if (is_compiled == 1)
654 DECL_EXTERNAL (decl) = 1;
655 pop_obstacks ();
659 ref = build1 (ADDR_EXPR, class_ptr_type, decl);
660 return ref;
662 else
664 int index;
665 tree cl;
666 push_obstacks (&permanent_obstack, &permanent_obstack);
667 index = alloc_class_constant (type);
668 cl = build_ref_from_constant_pool (index);
669 TREE_TYPE (cl) = promote_type (class_ptr_type);
670 pop_obstacks ();
671 return cl;
675 tree
676 build_static_field_ref (fdecl)
677 tree fdecl;
679 tree fclass = DECL_CONTEXT (fdecl);
680 int is_compiled = is_compiled_class (fclass);
681 if (is_compiled)
683 if (DECL_RTL (fdecl) == 0)
685 push_obstacks (&permanent_obstack, &permanent_obstack);
686 make_decl_rtl (fdecl, NULL, 1);
687 pop_obstacks ();
688 if (is_compiled == 1)
689 DECL_EXTERNAL (fdecl) = 1;
691 return fdecl;
693 else
695 /* Compile as:
696 * *(FTYPE*)build_class_ref(FCLASS)->fields[INDEX].info.addr
698 static tree fields_ident = NULL_TREE;
699 static tree info_ident = NULL_TREE;
700 tree ref = build_class_ref (fclass);
701 tree fld;
702 int field_index = 0;
703 ref = build1 (INDIRECT_REF, class_type_node, ref);
704 if (fields_ident == NULL_TREE)
705 fields_ident = get_identifier ("fields");
706 if (info_ident == NULL_TREE)
707 info_ident = get_identifier ("info");
708 ref = build (COMPONENT_REF, field_ptr_type_node, ref,
709 lookup_field (&class_type_node, fields_ident));
711 for (fld = TYPE_FIELDS (fclass); ; fld = TREE_CHAIN (fld))
713 if (fld == fdecl)
714 break;
715 if (fld == NULL_TREE)
716 fatal ("field '%s' not found in class",
717 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
718 if (FIELD_STATIC (fld))
719 field_index++;
721 field_index *= int_size_in_bytes (field_type_node);
722 ref = fold (build (PLUS_EXPR, field_ptr_type_node,
723 ref, build_int_2 (field_index, 0)));
724 ref = build1 (INDIRECT_REF, field_type_node, ref);
725 ref = build (COMPONENT_REF, field_info_union_node,
726 ref, lookup_field (&field_type_node, info_ident));
727 ref = build (COMPONENT_REF, ptr_type_node,
728 ref, TREE_CHAIN (TYPE_FIELDS (field_info_union_node)));
729 return fold (build1 (INDIRECT_REF, TREE_TYPE(fdecl), ref));
734 get_access_flags_from_decl (decl)
735 tree decl;
737 int access_flags = 0;
738 if (TREE_CODE (decl) == FIELD_DECL || TREE_CODE (decl) == VAR_DECL)
740 if (FIELD_STATIC (decl))
741 access_flags |= ACC_STATIC;
742 if (FIELD_PUBLIC (decl))
743 access_flags |= ACC_PUBLIC;
744 if (FIELD_PROTECTED (decl))
745 access_flags |= ACC_PROTECTED;
746 if (FIELD_PRIVATE (decl))
747 access_flags |= ACC_PRIVATE;
748 if (FIELD_FINAL (decl))
749 access_flags |= ACC_FINAL;
750 if (FIELD_VOLATILE (decl))
751 access_flags |= ACC_VOLATILE;
752 if (FIELD_TRANSIENT (decl))
753 access_flags |= ACC_TRANSIENT;
754 return access_flags;
756 if (TREE_CODE (decl) == TYPE_DECL)
758 if (CLASS_PUBLIC (decl))
759 access_flags |= ACC_PUBLIC;
760 if (CLASS_FINAL (decl))
761 access_flags |= ACC_FINAL;
762 if (CLASS_SUPER (decl))
763 access_flags |= ACC_SUPER;
764 if (CLASS_INTERFACE (decl))
765 access_flags |= ACC_INTERFACE;
766 if (CLASS_ABSTRACT (decl))
767 access_flags |= ACC_ABSTRACT;
768 return access_flags;
770 if (TREE_CODE (decl) == FUNCTION_DECL)
772 if (METHOD_PUBLIC (decl))
773 access_flags |= ACC_PUBLIC;
774 if (METHOD_PRIVATE (decl))
775 access_flags |= ACC_PRIVATE;
776 if (METHOD_PROTECTED (decl))
777 access_flags |= ACC_PROTECTED;
778 if (METHOD_STATIC (decl))
779 access_flags |= ACC_STATIC;
780 if (METHOD_FINAL (decl))
781 access_flags |= ACC_FINAL;
782 if (METHOD_SYNCHRONIZED (decl))
783 access_flags |= ACC_SYNCHRONIZED;
784 if (METHOD_NATIVE (decl))
785 access_flags |= ACC_NATIVE;
786 if (METHOD_ABSTRACT (decl))
787 access_flags |= ACC_ABSTRACT;
788 if (METHOD_TRANSIENT (decl))
789 access_flags |= ACC_TRANSIENT;
790 return access_flags;
792 abort ();
795 tree
796 make_field_value (tree fdecl)
798 tree finit, info;
799 int bsize, flags;
800 tree type = TREE_TYPE (fdecl);
801 int resolved = is_compiled_class (type);
802 START_RECORD_CONSTRUCTOR (finit, field_type_node);
803 PUSH_FIELD_VALUE (finit, "name", build_utf8_ref (DECL_NAME (fdecl)));
804 if (resolved)
805 type = build_class_ref (type);
806 else
807 type = build_utf8_ref (build_java_signature (type));
808 PUSH_FIELD_VALUE (finit, "type", type);
809 flags = get_access_flags_from_decl (fdecl);
810 if (! resolved)
811 flags |= 0x8000 /* FIELD_UNRESOLVED_FLAG */;
812 PUSH_FIELD_VALUE (finit, "accflags", build_int_2 (flags, 0));
813 bsize = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (fdecl))) / BITS_PER_UNIT;
814 PUSH_FIELD_VALUE (finit, "bsize", build_int_2 (bsize, 0));
815 if (FIELD_STATIC (fdecl))
817 tree cfield = TREE_CHAIN (TYPE_FIELDS(field_info_union_node));
818 tree faddr = build_address_of (build_static_field_ref (fdecl));
819 info = build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
820 build_tree_list (cfield, faddr));
822 else
824 int boffset
825 = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (fdecl)) / BITS_PER_UNIT;
826 info = build (CONSTRUCTOR, field_info_union_node, NULL_TREE,
827 build_tree_list (TYPE_FIELDS(field_info_union_node),
828 build_int_2 (boffset, 0)));
830 PUSH_FIELD_VALUE (finit, "info", info);
832 FINISH_RECORD_CONSTRUCTOR (finit);
833 return finit;
836 tree
837 make_method_value (mdecl, this_class_addr)
838 tree mdecl;
839 tree this_class_addr;
841 tree minit;
842 tree code;
843 #define ACC_TRANSLATED 0x4000
844 int accflags = get_access_flags_from_decl (mdecl) | ACC_TRANSLATED;
845 code = null_pointer_node;
846 if (DECL_RTL (mdecl))
847 code = build1 (ADDR_EXPR, nativecode_ptr_type_node, mdecl);
848 START_RECORD_CONSTRUCTOR (minit, method_type_node);
849 PUSH_FIELD_VALUE (minit, "name",
850 build_utf8_ref (DECL_CONSTRUCTOR_P (mdecl) ?
851 init_identifier_node
852 : DECL_NAME (mdecl)));
853 PUSH_FIELD_VALUE (minit, "signature",
854 build_utf8_ref (build_java_signature (TREE_TYPE (mdecl))));
855 PUSH_FIELD_VALUE (minit, "accflags", build_int_2 (accflags, 0));
856 PUSH_FIELD_VALUE (minit, "ncode", code);
857 FINISH_RECORD_CONSTRUCTOR (minit);
858 return minit;
861 tree
862 get_dispatch_vector (type)
863 tree type;
865 tree vtable = TYPE_VTABLE (type);
866 if (vtable == NULL)
868 int i;
869 tree method;
870 tree super = CLASSTYPE_SUPER (type);
871 int nvirtuals = TREE_INT_CST_LOW (TYPE_NVIRTUALS (type));
872 vtable = make_tree_vec (nvirtuals);
873 TYPE_VTABLE (type) = vtable;
874 if (super != NULL_TREE)
876 tree super_vtable = get_dispatch_vector (super);
877 for ( i = TREE_INT_CST_LOW (TYPE_NVIRTUALS (super)); --i >= 0; )
878 TREE_VEC_ELT (vtable, i) = TREE_VEC_ELT (super_vtable, i);
880 for (method = TYPE_METHODS (type); method != NULL_TREE;
881 method = TREE_CHAIN (method))
883 if (DECL_VINDEX (method) != NULL_TREE
884 && TREE_CODE (DECL_VINDEX (method)) == INTEGER_CST)
886 TREE_VEC_ELT (vtable, TREE_INT_CST_LOW (DECL_VINDEX (method)))
887 = method;
891 return vtable;
894 tree
895 get_dispatch_table (type, this_class_addr)
896 tree type, this_class_addr;
898 tree vtable = get_dispatch_vector (type);
899 int i;
900 tree list = NULL_TREE;
901 int nvirtuals = TREE_VEC_LENGTH (vtable);
902 for (i = nvirtuals; --i >= 0; )
904 tree method = TREE_VEC_ELT (vtable, i);
905 if (METHOD_ABSTRACT (method))
906 warning_with_decl (method, "abstract method in non-abstract class");
907 if (DECL_RTL (method) == 0)
908 make_decl_rtl (method, NULL, 1);
909 list = tree_cons (NULL_TREE /*DECL_VINDEX (method) + 2*/,
910 build1 (ADDR_EXPR, nativecode_ptr_type_node, method),
911 list);
913 /* Dummy entry for compatibility with G++ -fvtable-thunks. */
914 list = tree_cons (integer_zero_node, null_pointer_node, list);
915 list = tree_cons (integer_zero_node, this_class_addr, list);
916 return build (CONSTRUCTOR, build_prim_array_type (nativecode_ptr_type_node,
917 nvirtuals + 2),
918 NULL_TREE, list);
921 void
922 make_class_data (type)
923 tree type;
925 tree decl, cons, temp;
926 tree field, fields_decl;
927 tree static_fields = NULL_TREE;
928 tree instance_fields = NULL_TREE;
929 HOST_WIDE_INT static_field_count = 0;
930 HOST_WIDE_INT instance_field_count = 0;
931 HOST_WIDE_INT field_count;
932 tree field_array_type;
933 tree method;
934 tree methods = NULL_TREE;
935 tree dtable_decl = NULL_TREE;
936 HOST_WIDE_INT method_count = 0;
937 tree method_array_type;
938 tree methods_decl;
939 tree super;
940 tree this_class_addr;
941 tree constant_pool_constructor;
942 tree interfaces = null_pointer_node;
943 int interface_len = 0;
944 tree type_decl = TYPE_NAME (type);
946 this_class_addr = build_class_ref (type);
947 decl = TREE_OPERAND (this_class_addr, 0);
949 /* Build Field array. */
950 field = TYPE_FIELDS (type);
951 if (DECL_NAME (field) == NULL_TREE)
952 field = TREE_CHAIN (field); /* Skip dummy field for inherited data. */
953 for ( ; field != NULL_TREE; field = TREE_CHAIN (field))
955 if (! DECL_ARTIFICIAL (field))
957 tree init = make_field_value (field);
958 if (FIELD_STATIC (field))
960 static_field_count++;
961 static_fields = tree_cons (NULL_TREE, init, static_fields);
962 rest_of_decl_compilation (field, (char*) 0, 1, 1);
964 else
966 instance_field_count++;
967 instance_fields = tree_cons (NULL_TREE, init, instance_fields);
971 field_count = static_field_count + instance_field_count;
972 if (field_count > 0)
974 static_fields = nreverse (static_fields);
975 instance_fields = nreverse (instance_fields);
976 static_fields = chainon (static_fields, instance_fields);
977 field_array_type = build_prim_array_type (field_type_node, field_count);
978 fields_decl = build_decl (VAR_DECL, mangled_classname ("_FL_", type),
979 field_array_type);
980 DECL_INITIAL (fields_decl) = build (CONSTRUCTOR, field_array_type,
981 NULL_TREE, static_fields);
982 TREE_STATIC (fields_decl) = 1;
983 DECL_ARTIFICIAL (fields_decl) = 1;
984 DECL_IGNORED_P (fields_decl) = 1;
985 rest_of_decl_compilation (fields_decl, (char*) 0, 1, 0);
987 else
988 fields_decl = NULL_TREE;
990 /* Build Method array. */
991 for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (type));
992 method != NULL_TREE; method = TREE_CHAIN (method))
994 tree init = make_method_value (method, this_class_addr);
995 method_count++;
996 methods = tree_cons (NULL_TREE, init, methods);
998 method_array_type = build_prim_array_type (method_type_node, method_count);
999 methods_decl = build_decl (VAR_DECL, mangled_classname ("_MT_", type),
1000 method_array_type);
1001 DECL_INITIAL (methods_decl) = build (CONSTRUCTOR, method_array_type,
1002 NULL_TREE, nreverse (methods));
1003 TREE_STATIC (methods_decl) = 1;
1004 DECL_ARTIFICIAL (methods_decl) = 1;
1005 DECL_IGNORED_P (methods_decl) = 1;
1006 rest_of_decl_compilation (methods_decl, (char*) 0, 1, 0);
1008 if (flag_assume_compiled
1009 && ! CLASS_ABSTRACT (type_decl) && ! CLASS_INTERFACE (type_decl))
1011 tree dtable = get_dispatch_table (type, this_class_addr);
1012 dtable_decl = build_dtable_decl (type);
1013 DECL_INITIAL (dtable_decl) = dtable;
1014 TREE_STATIC (dtable_decl) = 1;
1015 DECL_ARTIFICIAL (dtable_decl) = 1;
1016 DECL_IGNORED_P (dtable_decl) = 1;
1017 TREE_PUBLIC (dtable_decl) = 1;
1018 rest_of_decl_compilation (dtable_decl, (char*) 0, 1, 0);
1021 super = CLASSTYPE_SUPER (type);
1022 if (super == NULL_TREE)
1023 super = null_pointer_node;
1024 else if (flag_assume_compiled)
1025 super = build_class_ref (super);
1026 else
1028 int super_index = alloc_class_constant (super);
1029 super = build_int_2 (super_index, 0);
1030 TREE_TYPE (super) == ptr_type_node;
1033 /* Build and emit the array of implemented interfaces. */
1034 if (type != object_type_node)
1035 interface_len = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)) - 1;
1036 if (interface_len > 0)
1038 tree init = NULL_TREE;
1039 int i;
1040 tree interface_array_type, idecl;
1041 interface_array_type
1042 = build_prim_array_type (class_ptr_type, interface_len);
1043 idecl = build_decl (VAR_DECL, mangled_classname ("_IF_", type),
1044 interface_array_type);
1045 for (i = interface_len; i > 0; i--)
1047 tree child = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
1048 tree iclass = BINFO_TYPE (child);
1049 tree index;
1050 if (flag_assume_compiled)
1051 index = build_class_ref (iclass);
1052 else
1054 int int_index = alloc_class_constant (iclass);
1055 index = build_int_2 (int_index, 0);
1056 TREE_TYPE (index) == ptr_type_node;
1058 init = tree_cons (NULL_TREE, index, init);
1060 DECL_INITIAL (idecl) = build (CONSTRUCTOR, interface_array_type,
1061 NULL_TREE, init);
1062 TREE_STATIC (idecl) = 1;
1063 DECL_ARTIFICIAL (idecl) = 1;
1064 DECL_IGNORED_P (idecl) = 1;
1065 interfaces = build1 (ADDR_EXPR, ptr_type_node, idecl);
1066 rest_of_decl_compilation (idecl, (char*) 0, 1, 0);
1069 constant_pool_constructor = build_constants_constructor ();
1071 START_RECORD_CONSTRUCTOR (temp, object_type_node);
1072 #if 0
1073 PUSH_FIELD_VALUE (temp, "dtable", NULL_TREE);
1074 #else
1075 PUSH_FIELD_VALUE (temp, "dtable",
1076 build1 (ADDR_EXPR, dtable_ptr_type, class_dtable_decl));
1077 #endif
1078 PUSH_FIELD_VALUE (temp, "sync_info", null_pointer_node);
1079 FINISH_RECORD_CONSTRUCTOR (temp);
1080 START_RECORD_CONSTRUCTOR (cons, class_type_node);
1081 PUSH_SUPER_VALUE (cons, temp);
1082 PUSH_FIELD_VALUE (cons, "next", null_pointer_node);
1083 PUSH_FIELD_VALUE (cons, "name",
1084 build_utf8_ref (build_internal_class_name (type)));
1085 PUSH_FIELD_VALUE (cons, "accflags",
1086 build_int_2 (get_access_flags_from_decl (type_decl), 0));
1088 PUSH_FIELD_VALUE (cons, "superclass", super);
1089 PUSH_FIELD_VALUE (cons, "constants", constant_pool_constructor);
1090 PUSH_FIELD_VALUE (cons, "methods",
1091 build1 (ADDR_EXPR, method_ptr_type_node, methods_decl));
1092 PUSH_FIELD_VALUE (cons, "method_count", build_int_2 (method_count, 0));
1093 PUSH_FIELD_VALUE (cons, "dtable_method_count", TYPE_NVIRTUALS (type));
1094 PUSH_FIELD_VALUE (cons, "fields",
1095 fields_decl == NULL_TREE ? null_pointer_node
1096 : build1 (ADDR_EXPR, field_ptr_type_node, fields_decl));
1097 PUSH_FIELD_VALUE (cons, "size_in_bytes", size_in_bytes (type));
1098 PUSH_FIELD_VALUE (cons, "field_count", build_int_2 (field_count, 0));
1099 PUSH_FIELD_VALUE (cons, "static_field_count",
1100 build_int_2 (static_field_count, 0));
1101 /* For now, we let Kaffe fill in the dtable. */
1102 PUSH_FIELD_VALUE (cons, "dtable",
1103 dtable_decl == NULL_TREE ? null_pointer_node
1104 : build1 (ADDR_EXPR, dtable_ptr_type, dtable_decl));
1105 PUSH_FIELD_VALUE (cons, "interfaces", interfaces);
1106 PUSH_FIELD_VALUE (cons, "loader", null_pointer_node);
1107 PUSH_FIELD_VALUE (cons, "interface_count", build_int_2 (interface_len, 0));
1108 PUSH_FIELD_VALUE (cons, "state", integer_zero_node);
1110 PUSH_FIELD_VALUE (cons, "thread", null_pointer_node);
1112 FINISH_RECORD_CONSTRUCTOR (cons);
1114 DECL_INITIAL (decl) = cons;
1115 rest_of_decl_compilation (decl, (char*) 0, 1, 0);
1118 /* Return 2 if CLASS is compiled by this compilation job;
1119 return 1 if CLASS can otherwise be assumed to be compiled;
1120 return 0 if we cannot assume that CLASS is compiled.
1121 Returns 1 for primitive and 0 for array types. */
1123 is_compiled_class (class)
1124 tree class;
1126 int seen_in_zip;
1127 if (TREE_CODE (class) == POINTER_TYPE)
1128 class = TREE_TYPE (class);
1129 if (TREE_CODE (class) != RECORD_TYPE) /* Primitive types are static. */
1130 return 1;
1131 if (TYPE_ARRAY_P (class))
1132 return 0;
1133 if (class == current_class)
1134 return 2;
1136 seen_in_zip = (TYPE_LANG_SPECIFIC (class) && TYPE_LANG_SPECIFIC (class)->jcf
1137 && TYPE_LANG_SPECIFIC (class)->jcf->seen_in_zip);
1138 if (CLASS_FROM_CURRENTLY_COMPILED_SOURCE_P (class) || seen_in_zip)
1140 /* The class was seen in the current ZIP file and will be
1141 available as a compiled class in the future but may not have
1142 been loaded already. Load it if necessary. This prevent
1143 build_class_ref () from crashing. */
1145 if (seen_in_zip && !CLASS_LOADED_P (class))
1146 load_class (class, 1);
1148 /* We return 2 for class seen in ZIP and class from files
1149 belonging to the same compilation unit */
1150 return 2;
1153 if (flag_assume_compiled)
1155 if (!CLASS_LOADED_P (class))
1156 load_class (class, 1);
1157 return 1;
1160 return 0;
1163 /* Append the mangled name of TYPE onto OBSTACK. */
1165 void
1166 append_gpp_mangled_type (obstack, type)
1167 struct obstack *obstack;
1168 tree type;
1170 char buf[8];
1171 int len;
1172 char *ptr;
1173 switch (TREE_CODE (type))
1175 char code;
1176 case BOOLEAN_TYPE: code = 'b'; goto primitive;
1177 case CHAR_TYPE: code = 'w'; goto primitive;
1178 case VOID_TYPE: code = 'v'; goto primitive;
1179 case INTEGER_TYPE:
1180 /* Get the original type instead of the arguments promoted type.
1181 Avoid symbol name clashes. Should call a function to do that.
1182 FIXME. */
1183 if (type == promoted_short_type_node)
1184 type = short_type_node;
1185 if (type == promoted_byte_type_node)
1186 type = byte_type_node;
1187 switch (TYPE_PRECISION (type))
1189 case 8: code = 'c'; goto primitive;
1190 case 16: code = 's'; goto primitive;
1191 case 32: code = 'i'; goto primitive;
1192 case 64: code = 'x'; goto primitive;
1193 default: goto bad_type;
1195 primitive:
1196 obstack_1grow (obstack, code);
1197 break;
1198 case REAL_TYPE:
1199 switch (TYPE_PRECISION (type))
1201 case 32: code = 'f'; goto primitive;
1202 case 64: code = 'd'; goto primitive;
1203 default: goto bad_type;
1205 case POINTER_TYPE:
1206 type = TREE_TYPE (type);
1207 obstack_1grow (obstack, 'P');
1208 case RECORD_TYPE:
1209 if (TYPE_ARRAY_P (type))
1211 obstack_grow (obstack, "t6JArray1Z", sizeof("t6JArray1Z")-1);
1212 append_gpp_mangled_type (obstack, TYPE_ARRAY_ELEMENT (type));
1214 else
1216 char *class_name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
1217 append_gpp_mangled_classtype (obstack, class_name);
1219 break;
1220 bad_type:
1221 default:
1222 fatal ("internal error - trying to mangle unknown type");
1226 /* Build the mangled name of the `class' field. */
1228 static tree
1229 mangle_class_field (class)
1230 tree class;
1232 tree name;
1233 obstack_grow (&temporary_obstack, "_CL_", 4);
1234 append_gpp_mangled_type (&temporary_obstack, class);
1235 obstack_1grow (&temporary_obstack, '\0');
1236 name = get_identifier (obstack_base (&temporary_obstack));
1237 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
1238 return name;
1241 /* Build the mangled (assembly-level) name of the static field FIELD. */
1243 tree
1244 mangle_static_field (field)
1245 tree field;
1247 tree class = DECL_CONTEXT (field);
1248 tree name = DECL_NAME (field);
1249 int encoded_len;
1250 #if ! defined (NO_DOLLAR_IN_LABEL) || ! defined (NO_DOT_IN_LABEL)
1251 obstack_1grow (&temporary_obstack, '_');
1252 #else
1253 obstack_grow (&temporary_obstack, "__static_", 9);
1254 #endif
1255 append_gpp_mangled_type (&temporary_obstack, class);
1256 encoded_len = unicode_mangling_length (IDENTIFIER_POINTER (name),
1257 IDENTIFIER_LENGTH (name));
1258 if (encoded_len > 0)
1260 obstack_1grow (&temporary_obstack, 'U');
1262 #ifndef NO_DOLLAR_IN_LABEL
1263 obstack_1grow (&temporary_obstack, '$');
1264 #else /* NO_DOLLAR_IN_LABEL */
1265 #ifndef NO_DOT_IN_LABEL
1266 obstack_1grow (&temporary_obstack, '.');
1267 #else /* NO_DOT_IN_LABEL */
1268 obstack_1grow (&temporary_obstack, '_');
1269 #endif /* NO_DOT_IN_LABEL */
1270 #endif /* NO_DOLLAR_IN_LABEL */
1271 if (encoded_len > 0)
1273 emit_unicode_mangled_name (&temporary_obstack,
1274 IDENTIFIER_POINTER (name),
1275 IDENTIFIER_LENGTH (name));
1277 else
1279 obstack_grow (&temporary_obstack,
1280 IDENTIFIER_POINTER (name),
1281 IDENTIFIER_LENGTH (name));
1283 obstack_1grow (&temporary_obstack, '\0');
1284 name = get_identifier (obstack_base (&temporary_obstack));
1285 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
1286 return name;
1289 /* Build a VAR_DECL for the dispatch table (vtable) for class TYPE. */
1291 tree
1292 build_dtable_decl (type)
1293 tree type;
1295 tree name;
1296 obstack_grow (&temporary_obstack, "__vt_", 5);
1297 append_gpp_mangled_type (&temporary_obstack, type);
1298 obstack_1grow (&temporary_obstack, '\0');
1299 name = get_identifier (obstack_base (&temporary_obstack));
1300 obstack_free (&temporary_obstack, obstack_base (&temporary_obstack));
1301 return build_decl (VAR_DECL, name, dtable_type);
1304 /* Pre-pend the TYPE_FIELDS of THIS_CLASS with a dummy FIELD_DECL for the
1305 fields inherited from SUPER_CLASS. */
1307 void
1308 push_super_field (this_class, super_class)
1309 tree this_class, super_class;
1311 tree base_decl;
1312 push_obstacks (&permanent_obstack, &permanent_obstack);
1313 base_decl = build_decl (FIELD_DECL, NULL_TREE, super_class);
1314 pop_obstacks ();
1315 DECL_IGNORED_P (base_decl) = 1;
1316 TREE_CHAIN (base_decl) = TYPE_FIELDS (this_class);
1317 TYPE_FIELDS (this_class) = base_decl;
1318 DECL_SIZE (base_decl) = TYPE_SIZE (super_class);
1321 void
1322 layout_class (this_class)
1323 tree this_class;
1325 tree super_class = CLASSTYPE_SUPER (this_class);
1326 tree handle_type = CLASS_TO_HANDLE_TYPE (this_class);
1327 tree method_decl, field;
1328 tree dtable_count;
1329 int i;
1331 if (super_class)
1333 /* Class seen in source are now complete and can be layed out.
1334 Once layed out, a class seen in the source has its
1335 CLASS_LOADED_P flag set */
1336 if (CLASS_FROM_SOURCE_P (super_class) && !CLASS_LOADED_P (super_class))
1337 safe_layout_class (super_class);
1338 if (! CLASS_LOADED_P (super_class))
1339 load_class (super_class, 1);
1340 if (TREE_CODE (TYPE_SIZE (super_class)) == ERROR_MARK)
1342 TYPE_SIZE (this_class) = error_mark_node;
1343 return;
1345 dtable_count = TYPE_NVIRTUALS (super_class);
1347 if (TYPE_SIZE (this_class) == NULL_TREE)
1348 push_super_field (this_class, super_class);
1350 else
1352 dtable_count = integer_zero_node;
1355 for (field = TYPE_FIELDS (this_class);
1356 field != NULL_TREE; field = TREE_CHAIN (field))
1358 if (FIELD_STATIC (field))
1360 /* Set DECL_ASSEMBLER_NAME to something suitably mangled. */
1361 DECL_ASSEMBLER_NAME (field) = mangle_static_field (field);
1365 layout_type (this_class);
1367 TYPE_METHODS (handle_type) = nreverse (TYPE_METHODS (handle_type));
1369 for (method_decl = TYPE_METHODS (handle_type), i = 0;
1370 method_decl; method_decl = TREE_CHAIN (method_decl), i++)
1372 char *ptr;
1373 char buf[8];
1374 char *asm_name;
1375 tree method_name = DECL_NAME (method_decl);
1376 #if 1
1377 /* Remove this once we no longer need old (Kaffe / JDK 1.0) mangling. */
1378 if (! flag_assume_compiled && METHOD_NATIVE (method_decl))
1380 for (ptr = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1381 *ptr; )
1383 int ch = *ptr++;
1384 if (ch == '.')
1385 ch = '_';
1386 obstack_1grow (&temporary_obstack, (char) ch);
1388 obstack_1grow (&temporary_obstack, (char) '_');
1389 if (method_name == init_identifier_node)
1390 obstack_grow (&temporary_obstack, "INIT", 4);
1391 else
1392 obstack_grow (&temporary_obstack,
1393 IDENTIFIER_POINTER (method_name),
1394 IDENTIFIER_LENGTH (method_name));
1396 else
1397 #endif
1399 int len; tree arg, arglist, t;
1400 int method_name_needs_escapes = 0;
1401 if (method_name != init_identifier_node
1402 && method_name != finit_identifier_node)
1404 int encoded_len
1405 = unicode_mangling_length (IDENTIFIER_POINTER (method_name),
1406 IDENTIFIER_LENGTH (method_name));
1407 if (encoded_len > 0)
1409 method_name_needs_escapes = 1;
1410 emit_unicode_mangled_name (&temporary_obstack,
1411 IDENTIFIER_POINTER (method_name),
1412 IDENTIFIER_LENGTH (method_name));
1414 else
1416 obstack_grow (&temporary_obstack,
1417 IDENTIFIER_POINTER (method_name),
1418 IDENTIFIER_LENGTH (method_name));
1422 obstack_grow (&temporary_obstack, "__", 2);
1423 if (method_name == finit_identifier_node)
1424 obstack_grow (&temporary_obstack, "finit", 5);
1425 append_gpp_mangled_type (&temporary_obstack, this_class);
1426 TREE_PUBLIC (method_decl) = 1;
1428 t = TREE_TYPE (method_decl);
1429 arglist = TYPE_ARG_TYPES (t);
1430 if (TREE_CODE (t) == METHOD_TYPE)
1431 arglist = TREE_CHAIN (arglist);
1432 for (arg = arglist; arg != NULL_TREE; )
1434 tree a = arglist;
1435 tree argtype = TREE_VALUE (arg);
1436 int tindex = 1;
1437 if (TREE_CODE (argtype) == POINTER_TYPE)
1439 /* This is O(N**2). Do we care? Cfr gcc/cp/method.c. */
1440 while (a != arg && argtype != TREE_VALUE (a))
1441 a = TREE_CHAIN (a), tindex++;
1443 else
1444 a = arg;
1445 if (a != arg)
1447 char buf[12];
1448 int nrepeats = 0;
1451 arg = TREE_CHAIN (arg); nrepeats++;
1453 while (arg != NULL_TREE && argtype == TREE_VALUE (arg));
1454 if (nrepeats > 1)
1456 obstack_1grow (&temporary_obstack, 'N');
1457 sprintf (buf, "%d", nrepeats);
1458 obstack_grow (&temporary_obstack, buf, strlen (buf));
1459 if (nrepeats > 9)
1460 obstack_1grow (&temporary_obstack, '_');
1462 else
1463 obstack_1grow (&temporary_obstack, 'T');
1464 sprintf (buf, "%d", tindex);
1465 obstack_grow (&temporary_obstack, buf, strlen (buf));
1466 if (tindex > 9)
1467 obstack_1grow (&temporary_obstack, '_');
1469 else
1471 append_gpp_mangled_type (&temporary_obstack, argtype);
1472 arg = TREE_CHAIN (arg);
1475 if (method_name_needs_escapes)
1476 obstack_1grow (&temporary_obstack, 'U');
1478 obstack_1grow (&temporary_obstack, '\0');
1479 asm_name = obstack_finish (&temporary_obstack);
1480 DECL_ASSEMBLER_NAME (method_decl) = get_identifier (asm_name);
1481 if (! METHOD_ABSTRACT (method_decl))
1482 make_function_rtl (method_decl);
1483 obstack_free (&temporary_obstack, asm_name);
1485 if (method_name == init_identifier_node)
1487 char *p = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (this_class)));
1488 for (ptr = p; *ptr; )
1490 if (*ptr++ == '.')
1491 p = ptr;
1493 DECL_NAME (method_decl) = get_identifier (p);
1494 DECL_CONSTRUCTOR_P (method_decl) = 1;
1496 else if (! METHOD_STATIC (method_decl) && !DECL_ARTIFICIAL (method_decl))
1498 tree method_sig = build_java_argument_signature (TREE_TYPE (method_decl));
1499 tree super_method = lookup_argument_method (super_class, method_name,
1500 method_sig);
1501 if (super_method != NULL_TREE)
1503 DECL_VINDEX (method_decl) = DECL_VINDEX (super_method);
1504 if (DECL_VINDEX (method_decl) == NULL_TREE)
1505 error_with_decl (method_decl,
1506 "non-static method '%s' overrides static method");
1507 #if 0
1508 else if (TREE_TYPE (TREE_TYPE (method_decl))
1509 != TREE_TYPE (TREE_TYPE (super_method)))
1511 error_with_decl (method_decl,
1512 "Method `%s' redefined with different return type");
1513 error_with_decl (super_method,
1514 "Overridden decl is here");
1516 #endif
1518 else if (! METHOD_FINAL (method_decl)
1519 && ! CLASS_FINAL (TYPE_NAME (this_class)))
1521 DECL_VINDEX (method_decl) = dtable_count;
1522 dtable_count = build_int_2 (1+TREE_INT_CST_LOW (dtable_count), 0);
1526 TYPE_NVIRTUALS (this_class) = dtable_count;
1528 #ifdef JAVA_USE_HANDLES
1529 layout_type (handle_type);
1530 #endif
1533 static tree registered_class = NULL_TREE;
1535 void
1536 register_class ()
1538 static tree end;
1539 tree node = TREE_OPERAND (build_class_ref (current_class), 0);
1540 tree current = copy_node (node);
1542 XEXP (DECL_RTL (current), 0) = copy_rtx (XEXP (DECL_RTL(node), 0));
1543 if (!registered_class)
1544 registered_class = current;
1545 else
1546 TREE_CHAIN (end) = current;
1548 end = current;
1551 /* Generate a function that gets called at start-up (static contructor) time,
1552 which calls registerClass for all the compiled classes. */
1554 void
1555 emit_register_classes ()
1557 tree decl = getdecls ();
1559 extern tree get_file_function_name PROTO((int));
1560 tree init_name = get_file_function_name ('I');
1561 tree init_type = build_function_type (void_type_node, NULL_TREE);
1562 tree init_decl;
1563 tree t;
1565 start_sequence ();
1566 init_decl = build_decl (FUNCTION_DECL, init_name, init_type);
1567 DECL_ASSEMBLER_NAME (init_decl) = init_name;
1568 TREE_STATIC (init_decl) = 1;
1569 current_function_decl = init_decl;
1570 DECL_RESULT (init_decl) = build_decl(RESULT_DECL, NULL_TREE, void_type_node);
1571 /* DECL_EXTERNAL (init_decl) = 1;*/
1572 TREE_PUBLIC (init_decl) = 1;
1573 pushlevel (0);
1574 make_function_rtl (init_decl);
1575 init_function_start (init_decl, input_filename, 0);
1576 expand_function_start (init_decl, 0);
1578 for ( t = registered_class; t; t = TREE_CHAIN (t))
1579 emit_library_call (registerClass_libfunc, 0, VOIDmode, 1,
1580 XEXP (DECL_RTL (t), 0), Pmode);
1582 expand_function_end (input_filename, 0, 0);
1583 poplevel (1, 0, 1);
1585 /* Force generation, even with -O3 or deeper. Gross hack. FIXME */
1586 extern int flag_inline_functions;
1587 int saved_flag = flag_inline_functions;
1588 flag_inline_functions = 0;
1589 rest_of_compilation (init_decl);
1590 flag_inline_functions = saved_flag;
1592 current_function_decl = NULL_TREE;
1593 assemble_constructor (IDENTIFIER_POINTER (init_name));
1596 void
1597 init_class_processing ()
1599 registerClass_libfunc = gen_rtx (SYMBOL_REF, Pmode, "_Jv_RegisterClass");