1 /* Program to write C++-suitable header files from a Java(TM) .class
2 file. This is similar to SUN's javah.
4 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
5 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
32 #include "coretypes.h"
40 #include "java-tree.h"
41 #include "java-opcodes.h"
50 /* The output file. */
53 /* Nonzero on failure. */
54 static int found_error = 0;
56 /* Nonzero if we're generating JNI output. */
59 /* When nonzero, warn when source file is newer than matching class
63 /* Directory to place resulting files in. Set by -d option. */
64 const char *output_directory = "";
66 /* Directory to place temporary file. Set by -td option. Currently unused. */
67 const char *temp_directory = "/tmp";
69 /* Number of friend functions we have to declare. */
70 static int friend_count;
72 /* A class can optionally have a `friend' function declared. If
73 non-NULL, this is that function. */
74 static char **friend_specs = NULL;
76 /* Number of lines we are prepending before the class. */
77 static int prepend_count;
79 /* We can prepend extra lines before the class's start. */
80 static char **prepend_specs = NULL;
82 /* Number of lines we are appending at the end of the class. */
85 /* We can append extra lines just before the class's end. */
86 static char **add_specs = NULL;
88 /* Number of lines we are appending after the class. */
89 static int append_count;
91 /* We can append extra lines after the class's end. */
92 static char **append_specs = NULL;
98 struct JCF *current_jcf;
100 /* This holds access information for the last field we examined. They
101 let us generate "private:", "public:", and "protected:" properly.
102 If 0 then we haven't previously examined any field. */
103 static JCF_u2 last_access;
105 /* Pass this macro the flags for a class and for a method. It will
106 return true if the method should be considered `final'. */
107 #define METHOD_IS_FINAL(Class, Method) \
108 (((Class) & ACC_FINAL) || ((Method) & (ACC_FINAL | ACC_PRIVATE)))
110 /* Pass this macro the flags for a method. It will return true if the
112 #define METHOD_IS_NATIVE(Method) \
113 ((Method) & ACC_NATIVE)
115 /* We keep a linked list of all method names we have seen. This lets
116 us determine if a method name and a field name are in conflict. */
121 unsigned char *signature;
124 struct method_name *next;
127 /* List of method names we've seen. */
128 static struct method_name *method_name_list;
130 static void print_field_info (FILE*, JCF*, int, int, JCF_u2);
131 static void print_mangled_classname (FILE*, JCF*, const char*, int);
132 static int print_cxx_classname (FILE*, const char*, JCF*, int, int);
133 static void print_method_info (FILE*, JCF*, int, int, JCF_u2);
134 static void print_c_decl (FILE*, JCF*, int, int, int, const char *, int);
135 static void print_stub_or_jni (FILE*, JCF*, int, int, int, const char *, int);
136 static void print_full_cxx_name (FILE*, JCF*, int, int, int, const char *, int);
137 static void decompile_method (FILE*, JCF*, int);
138 static void add_class_decl (FILE*, JCF*, JCF_u2);
140 static void print_name (FILE *, JCF *, int);
141 static void print_base_classname (FILE *, JCF *, int);
142 static int utf8_cmp (const unsigned char *, int, const char *);
143 static char *cxx_keyword_subst (const unsigned char *, int);
144 static void generate_access (FILE *, JCF_u2);
145 static int name_is_method_p (const unsigned char *, int);
146 static char *get_field_name (JCF *, int, JCF_u2);
147 static void print_field_name (FILE *, JCF *, int, JCF_u2);
148 static const unsigned char *super_class_name (JCF *, int *);
149 static void print_include (FILE *, const unsigned char *, int);
150 static int gcjh_streq (const void *p1, const void *p2);
151 static int throwable_p (const unsigned char *signature);
152 static const unsigned char *
153 decode_signature_piece (FILE *, const unsigned char *,
154 const unsigned char *, int *);
155 static void print_class_decls (FILE *, JCF *, int);
156 static void error (const char *msgid, ...);
157 static void usage (void) ATTRIBUTE_NORETURN;
158 static void help (void) ATTRIBUTE_NORETURN;
159 static void version (void) ATTRIBUTE_NORETURN;
160 static int overloaded_jni_method_exists_p (const unsigned char *, int,
162 static void jni_print_char (FILE *, int);
163 static void jni_print_float (FILE *, jfloat);
164 static void jni_print_double (FILE *, jdouble);
165 static void decompile_return_statement (FILE *, JCF *, int, int, int);
167 static void handle_inner_classes (int);
169 JCF_u2 current_field_name;
170 JCF_u2 current_field_value;
171 JCF_u2 current_field_signature;
172 JCF_u2 current_field_flags;
174 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
175 ( current_field_name = (NAME), current_field_signature = (SIGNATURE), \
176 current_field_flags = (ACCESS_FLAGS), current_field_value = 0)
178 /* We pass over fields twice. The first time we just note the types
179 of the fields and then the start of the methods. Then we go back
180 and parse the fields for real. This is ugly. */
181 static int field_pass;
182 /* Likewise we pass over methods twice. The first time we generate
183 class decl information; the second time we generate actual method
185 static int method_pass;
187 #define HANDLE_END_FIELD() \
190 if (out && ! stubs && ! flag_jni) \
191 print_field_info (out, jcf, current_field_name, \
192 current_field_signature, \
193 current_field_flags); \
195 else if (! stubs && ! flag_jni) \
196 add_class_decl (out, jcf, current_field_signature);
198 #define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)
200 static int method_declared = 0;
201 static int method_access = 0;
202 static int method_printed = 0;
203 static int method_synthetic = 0;
204 static int method_signature = 0;
206 /* Set to 1 while the very first data member of a class is being handled. */
207 static int is_first_data_member = 0;
209 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
211 method_synthetic = 0; \
212 method_printed = 0; \
214 method_signature = SIGNATURE; \
215 if (ATTRIBUTE_COUNT) \
216 method_synthetic = peek_attribute (jcf, ATTRIBUTE_COUNT, \
217 (const char *)"Synthetic", 9); \
218 /* If a synthetic methods have been declared, its attribute aren't \
219 worth reading (and triggering side-effects). We skip them an \
220 set ATTRIBUTE_COUNT to zero so that they'll be skipped in \
221 jcf_parse_one_method. */ \
222 if (method_synthetic) \
224 skip_attribute (jcf, ATTRIBUTE_COUNT); \
225 ATTRIBUTE_COUNT = 0; \
227 if (method_pass && !method_synthetic) \
230 print_method_info (out, jcf, NAME, SIGNATURE, \
233 else if (!method_synthetic) \
235 print_method_info (NULL, jcf, NAME, SIGNATURE, \
237 if (! stubs && ! flag_jni) \
238 add_class_decl (out, jcf, SIGNATURE); \
242 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
243 if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
245 static int decompiled = 0;
246 #define HANDLE_END_METHOD() \
247 if (out && method_printed && !method_synthetic) \
248 fputs (decompiled || stubs ? "\n" : ";\n", out);
250 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) handle_inner_classes (COUNT)
252 /* We're going to need {peek,skip}_attribute, enable their definition. */
253 #define NEED_PEEK_ATTRIBUTE
254 #define NEED_SKIP_ATTRIBUTE
256 #include "jcf-reader.c"
258 /* Print an error message and set found_error. */
260 error (const char *msgid, ...)
264 va_start (ap, msgid);
266 fprintf (stderr, "gcjh: ");
267 vfprintf (stderr, _(msgid), ap);
269 fprintf (stderr, "\n");
273 /* Print a single-precision float, suitable for parsing by g++. */
275 jni_print_float (FILE *stream, jfloat f)
277 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
278 work in data initializers. FIXME. */
279 if (JFLOAT_FINITE (f))
281 fputs (" = ", stream);
285 fprintf (stream, "0x1.%.6xp%+df",
286 ((unsigned int)f.mantissa) << 1,
287 f.exponent - JFLOAT_EXP_BIAS);
289 /* Exponent of 0x01 is -125; exponent of 0x00 is *also* -125,
290 because the implicit leading 1 bit is no longer present. */
291 fprintf (stream, "0x0.%.6xp%+df",
292 ((unsigned int)f.mantissa) << 1,
293 f.exponent + 1 - JFLOAT_EXP_BIAS);
295 fputs (";\n", stream);
298 /* Print a double-precision float, suitable for parsing by g++. */
300 jni_print_double (FILE *stream, jdouble f)
302 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
303 work in data initializers. FIXME. */
304 if (JDOUBLE_FINITE (f))
306 fputs (" = ", stream);
310 fprintf (stream, "0x1.%.5x%.8xp%+d",
311 f.mantissa0, f.mantissa1,
312 f.exponent - JDOUBLE_EXP_BIAS);
314 /* Exponent of 0x001 is -1022; exponent of 0x000 is *also* -1022,
315 because the implicit leading 1 bit is no longer present. */
316 fprintf (stream, "0x0.%.5x%.8xp%+d",
317 f.mantissa0, f.mantissa1,
318 f.exponent + 1 - JDOUBLE_EXP_BIAS);
320 fputs (";\n", stream);
323 /* Print a character, appropriately mangled for JNI. */
326 jni_print_char (FILE *stream, int ch)
329 jcf_print_char (stream, ch);
330 else if (ch == '(' || ch == ')')
335 fputs ("_1", stream);
337 fputs ("_2", stream);
339 fputs ("_3", stream);
342 else if (ISALNUM (ch))
346 /* "Unicode" character. */
347 fprintf (stream, "_0%04x", ch);
351 /* Print a name from the class data. If the index does not point to a
352 string, an error results. */
355 print_name (FILE* stream, JCF* jcf, int name_index)
357 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
359 fprintf (stream, "<not a UTF8 constant>");
363 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
364 JPOOL_UTF_LENGTH (jcf, name_index));
367 /* For JNI we must correctly quote each character. */
368 const unsigned char *str = JPOOL_UTF_DATA (jcf, name_index);
369 int length = JPOOL_UTF_LENGTH (jcf, name_index);
370 const unsigned char *limit = str + length;
373 int ch = UTF8_GET (str, limit);
376 fprintf (stream, "\\<invalid>");
379 jni_print_char (stream, ch);
384 /* Print base name of class. The base name is everything after the
388 print_base_classname (FILE *stream, JCF *jcf, int index)
390 int name_index = JPOOL_USHORT1 (jcf, index);
392 const unsigned char *s, *p, *limit;
394 s = JPOOL_UTF_DATA (jcf, name_index);
395 len = JPOOL_UTF_LENGTH (jcf, name_index);
400 int c = UTF8_GET (s, limit);
407 int ch = UTF8_GET (p, limit);
409 fputs ("::", stream);
411 jcf_print_char (stream, ch);
415 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
416 and 1 if STR is "greater" than NAME. */
419 utf8_cmp (const unsigned char *str, int length, const char *name)
421 const unsigned char *limit = str + length;
424 for (i = 0; name[i]; ++i)
426 int ch = UTF8_GET (str, limit);
431 return str == limit ? 0 : 1;
434 /* This is a sorted list of all C++ keywords. */
436 static const char *const cxx_keywords[] =
545 /* If NAME is the name of a C++ keyword, then return an override name.
546 This is a name that can be used in place of the keyword.
547 Otherwise, return NULL. The return value is malloc()d. */
550 cxx_keyword_subst (const unsigned char *str, int length)
552 int last = ARRAY_SIZE (cxx_keywords);
554 int mid = (last + first) / 2;
557 for (mid = (last + first) / 2;
559 old = mid, mid = (last + first) / 2)
561 int kwl = strlen (cxx_keywords[mid]);
562 int min_length = kwl > length ? length : kwl;
563 int r = utf8_cmp (str, min_length, cxx_keywords[mid]);
569 /* Skip all trailing `$'. */
570 for (i = min_length; i < length && str[i] == '$'; ++i)
572 /* We've only found a match if all the remaining characters
576 char *dup = xmalloc (2 + length - min_length + kwl);
577 strcpy (dup, cxx_keywords[mid]);
578 for (i = kwl; i < length + 1; ++i)
594 /* Generate an access control keyword based on FLAGS. */
597 generate_access (FILE *stream, JCF_u2 flags)
599 if ((flags & ACC_VISIBILITY) == last_access)
601 last_access = (flags & ACC_VISIBILITY);
606 fputs ("public: // actually package-private\n", stream);
609 fputs ("public:\n", stream);
612 fputs ("private:\n", stream);
615 fputs ("public: // actually protected\n", stream);
619 fprintf (stream, "#error unrecognized visibility %d\n",
620 (flags & ACC_VISIBILITY));
625 /* See if NAME is already the name of a method. */
627 name_is_method_p (const unsigned char *name, int length)
629 struct method_name *p;
631 for (p = method_name_list; p != NULL; p = p->next)
633 if (p->length == length && ! memcmp (p->name, name, length))
639 /* Free the method name list. */
641 free_method_name_list (void)
643 struct method_name *p = method_name_list;
646 struct method_name *next = p->next;
652 method_name_list = NULL;
655 /* If there is already a native method named NAME, whose signature is not
656 SIGNATURE, then return true. Otherwise return false. */
658 overloaded_jni_method_exists_p (const unsigned char *name, int length,
659 const char *signature, int sig_length)
661 struct method_name *p;
663 for (p = method_name_list; p != NULL; p = p->next)
666 && p->length == length
667 && ! memcmp (p->name, name, length)
668 && (p->sig_length != sig_length
669 || memcmp (p->signature, signature, sig_length)))
675 /* Get name of a field. This handles renamings due to C++ clash. */
677 get_field_name (JCF *jcf, int name_index, JCF_u2 flags)
679 unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
680 int length = JPOOL_UTF_LENGTH (jcf, name_index);
683 if (name_is_method_p (name, length))
685 /* This field name matches a method. So override the name with
686 a dummy name. This is yucky, but it isn't clear what else to
687 do. FIXME: if the field is static, then we'll be in real
689 if ((flags & ACC_STATIC))
691 error ("static field has same name as method");
695 override = xmalloc (length + 3);
696 memcpy (override, name, length);
697 strcpy (override + length, "__");
700 override = cxx_keyword_subst (name, length);
705 /* Print a field name. Convenience function for use with
708 print_field_name (FILE *stream, JCF *jcf, int name_index, JCF_u2 flags)
710 char *override = get_field_name (jcf, name_index, flags);
714 fputs (override, stream);
718 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
719 JPOOL_UTF_LENGTH (jcf, name_index));
723 print_field_info (FILE *stream, JCF* jcf, int name_index, int sig_index,
726 char *override = NULL;
728 generate_access (stream, flags);
729 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
731 fprintf (stream, "<not a UTF8 constant>");
737 if ((flags & ACC_STATIC))
739 fputs ("static ", out);
741 if ((flags & ACC_FINAL) && current_field_value > 0)
746 switch (JPOOL_TAG (jcf, current_field_value))
748 case CONSTANT_Integer:
751 int most_negative = 0;
752 fputs ("const jint ", out);
753 print_field_name (out, jcf, name_index, 0);
755 num = JPOOL_INT (jcf, current_field_value);
756 /* We single out the most negative number to print
757 specially. This avoids later warnings from g++. */
758 if (num == (jint) 0x80000000)
763 format_int (buffer, (jlong) num, 10);
764 fprintf (out, "%sL%s;\n", buffer, most_negative ? " - 1" : "");
770 int most_negative = 0;
771 fputs ("const jlong ", out);
772 print_field_name (out, jcf, name_index, 0);
774 num = JPOOL_LONG (jcf, current_field_value);
775 /* We single out the most negative number to print
776 specially.. This avoids later warnings from g++. */
777 if (num == (jlong) 0x8000000000000000LL)
782 format_int (buffer, num, 10);
783 fprintf (out, "%sLL%s;\n", buffer, most_negative ? " - 1" :"");
788 jfloat fnum = JPOOL_FLOAT (jcf, current_field_value);
789 fputs ("const jfloat ", out);
790 print_field_name (out, jcf, name_index, 0);
791 jni_print_float (out, fnum);
794 case CONSTANT_Double:
796 jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value);
797 fputs ("const jdouble ", out);
798 print_field_name (out, jcf, name_index, 0);
799 jni_print_double (out, dnum);
803 /* We can't print this as a constant, but we can still
804 print something sensible. */
814 override = get_field_name (jcf, name_index, flags);
815 print_c_decl (out, jcf, name_index, sig_index, 0, override, flags);
824 print_method_info (FILE *stream, JCF* jcf, int name_index, int sig_index,
827 const unsigned char *str;
828 int length, is_init = 0;
829 char *override = NULL;
832 method_access = flags;
833 if (stream && JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
834 fprintf (stream, "<not a UTF8 constant>");
835 str = JPOOL_UTF_DATA (jcf, name_index);
836 length = JPOOL_UTF_LENGTH (jcf, name_index);
840 /* Ignore the internally generated method <clinit>. However,
841 treat <init> as a constructor. */
842 if (! utf8_cmp (str, length, "<init>"))
844 else if (! METHOD_IS_FINAL (jcf->access_flags, flags)
845 && ! (flags & ACC_STATIC))
847 /* FIXME: i18n bug here. Order of prints should not be
849 fprintf (stderr, _("ignored method `"));
850 jcf_print_utf8 (stderr, str, length);
851 fprintf (stderr, _("' marked virtual\n"));
859 /* During the first method pass, build a list of method names. This will
860 be used to determine if field names conflict with method names. */
863 struct method_name *nn;
865 nn = xmalloc (sizeof (struct method_name));
866 nn->name = xmalloc (length);
867 memcpy (nn->name, str, length);
869 nn->next = method_name_list;
870 nn->sig_length = JPOOL_UTF_LENGTH (jcf, sig_index);
871 nn->signature = xmalloc (nn->sig_length);
872 nn->is_native = METHOD_IS_NATIVE (flags);
873 memcpy (nn->signature, JPOOL_UTF_DATA (jcf, sig_index),
875 method_name_list = nn;
877 /* The rest of this function doesn't matter. */
881 /* We don't worry about overrides in JNI mode. */
884 /* We can't generate a method whose name is a C++ reserved word.
885 We can't just ignore the function, because that will cause
886 incorrect code to be generated if the function is virtual
887 (not only for calls to this function for for other functions
888 after it in the vtbl). So we give it a dummy name instead. */
889 override = cxx_keyword_subst (str, length);
892 if (! stubs && ! flag_jni)
896 generate_access (stream, flags);
899 if ((flags & ACC_STATIC))
900 fputs ("static ", out);
901 else if (! METHOD_IS_FINAL (jcf->access_flags, flags))
903 /* Don't print `virtual' if we have a constructor. */
905 fputs ("virtual ", out);
907 print_c_decl (out, jcf, name_index, sig_index, is_init, override, flags);
909 if ((flags & ACC_ABSTRACT))
916 if (METHOD_IS_NATIVE (flags))
919 print_stub_or_jni (out, jcf, name_index, sig_index,
920 is_init, override, flags);
928 /* A helper for the decompiler which prints a `return' statement where
929 the type is a reference type. If METHODTYPE and OBJECTTYPE are not
930 identical, we emit a cast. We do this because the C++ compiler
931 doesn't know that a reference can be cast to the type of an
932 interface it implements. METHODTYPE is the index of the method's
933 signature. NAMEINDEX is the index of the field name; -1 for
934 `this'. OBJECTTYPE is the index of the object's type. */
936 decompile_return_statement (FILE *out, JCF *jcf, int methodtype,
937 int nameindex, int objecttype)
940 int obj_name_len, method_name_len;
941 const unsigned char *obj_data, *method_data;
943 obj_name_len = JPOOL_UTF_LENGTH (jcf, objecttype);
944 obj_data = JPOOL_UTF_DATA (jcf, objecttype);
946 method_name_len = JPOOL_UTF_LENGTH (jcf, methodtype);
947 method_data = JPOOL_UTF_DATA (jcf, methodtype);
949 /* Skip forward to return type part of method. */
950 while (*method_data != ')')
959 /* If we see an `L', skip it and the trailing `;'. */
960 if (method_data[0] == 'L' && method_data[method_name_len - 1] == ';')
963 method_name_len -= 2;
965 if (obj_data[0] == 'L' && obj_data[obj_name_len - 1] == ';')
971 /* FIXME: if METHODTYPE is a superclass of OBJECTTYPE then we don't
972 need a cast. Right now there is no way to determine if this is
974 if (method_name_len != obj_name_len)
979 for (i = 0; i < method_name_len; ++i)
981 if (method_data[i] != obj_data[i])
989 fputs (" { return ", out);
994 const unsigned char *limit;
996 fputs ("reinterpret_cast<", out);
998 while (*method_data == '[')
1003 fputs ("JArray<", out);
1006 /* Leading space to avoid C++ digraphs. */
1009 /* If we see an `L', skip it and the trailing `;'. Only do this
1010 if we've seen an array specification. If we don't have an
1011 array then the `L' was stripped earlier. */
1012 if (array_depth && method_data[0] == 'L'
1013 && method_data[method_name_len - 1] == ';')
1016 method_name_len -= 2;
1019 limit = method_data + method_name_len;
1020 while (method_data < limit)
1022 int ch = UTF8_GET (method_data, limit);
1026 jcf_print_char (out, ch);
1030 /* Close each array. */
1031 while (array_depth > 0)
1037 /* Close the cast. */
1041 if (nameindex == -1)
1042 fputs ("this", out);
1044 print_field_name (out, jcf, nameindex, 0);
1053 /* Try to decompile a method body. Right now we just try to handle a
1054 simple case that we can do. Expand as desired. */
1056 decompile_method (FILE *out, JCF *jcf, int code_len)
1058 const unsigned char *codes = jcf->read_ptr;
1060 uint16 name_and_type, name;
1062 /* If the method is synchronized, don't touch it. */
1063 if ((method_access & ACC_SYNCHRONIZED))
1067 && codes[0] == OPCODE_aload_0
1068 && codes[1] == OPCODE_getfield
1069 && (codes[4] == OPCODE_areturn
1070 || codes[4] == OPCODE_dreturn
1071 || codes[4] == OPCODE_freturn
1072 || codes[4] == OPCODE_ireturn
1073 || codes[4] == OPCODE_lreturn))
1075 /* Found code like `return FIELD'. */
1076 index = (codes[2] << 8) | codes[3];
1077 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
1078 name_and_type = JPOOL_USHORT2 (jcf, index);
1079 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
1080 name = JPOOL_USHORT1 (jcf, name_and_type);
1081 if (codes[4] == OPCODE_areturn)
1082 decompile_return_statement (out, jcf, method_signature,
1083 name, JPOOL_USHORT2 (jcf, name_and_type));
1086 fputs (" { return ", out);
1088 print_field_name (out, jcf, name, 0);
1093 else if (code_len == 2
1094 && codes[0] == OPCODE_aload_0
1095 && codes[1] == OPCODE_areturn
1096 /* We're going to generate `return this'. This only makes
1097 sense for non-static methods. */
1098 && ! (method_access & ACC_STATIC))
1100 decompile_return_statement (out, jcf, method_signature, -1,
1101 JPOOL_USHORT1 (jcf, jcf->this_class));
1104 else if (code_len == 1 && codes[0] == OPCODE_return)
1106 /* Found plain `return'. */
1107 fputs (" { }", out);
1110 else if (code_len == 2
1111 && codes[0] == OPCODE_aconst_null
1112 && codes[1] == OPCODE_areturn)
1114 /* Found `return null'. We don't want to depend on NULL being
1116 fputs (" { return 0; }", out);
1121 /* Like strcmp, but invert the return result for the hash table. This
1122 should probably be in hashtab.c to complement the existing string
1125 gcjh_streq (const void *p1, const void *p2)
1127 return ! strcmp ((char *) p1, (char *) p2);
1130 /* Return 1 if the initial part of CLNAME names a subclass of throwable,
1131 or 0 if not. CLNAME may be extracted from a signature, and can be
1132 terminated with either `;' or NULL. */
1134 throwable_p (const unsigned char *clname)
1137 unsigned char *current;
1141 /* We keep two hash tables of class names. In one we list all the
1142 classes which are subclasses of Throwable. In the other we will
1143 all other classes. We keep two tables to make the code a bit
1144 simpler; we don't have to have a structure mapping class name to
1145 a `throwable?' bit. */
1146 static htab_t throw_hash;
1147 static htab_t non_throw_hash;
1148 static int init_done = 0;
1155 /* Self-initializing. The cost of this really doesn't matter.
1156 We also don't care about freeing these, either. */
1157 throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
1159 non_throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
1162 /* Make sure the root classes show up in the tables. */
1163 str = (unsigned char *) xstrdup ("java.lang.Throwable");
1164 slot = htab_find_slot (throw_hash, str, INSERT);
1167 str = (unsigned char *) xstrdup ("java.lang.Object");
1168 slot = htab_find_slot (non_throw_hash, str, INSERT);
1174 for (length = 0; clname[length] != ';' && clname[length] != '\0'; ++length)
1176 current = ALLOC (length + 1);
1177 for (i = 0; i < length; ++i)
1178 current[i] = clname[i] == '/' ? '.' : clname[i];
1179 current[length] = '\0';
1181 /* We don't compute the hash slot here because the table might be
1182 modified by the recursion. In that case the slot could be
1184 if (htab_find (throw_hash, current))
1186 else if (htab_find (non_throw_hash, current))
1192 unsigned char *super, *tmp;
1193 int super_length = -1;
1194 const char *classfile_name = find_class ((char *) current, strlen ((const char *) current),
1197 if (! classfile_name)
1199 error ("couldn't find class %s", current);
1202 if (jcf_parse_preamble (&jcf) != 0
1203 || jcf_parse_constant_pool (&jcf) != 0
1204 || verify_constant_pool (&jcf) > 0)
1206 error ("parse error while reading %s", classfile_name);
1209 jcf_parse_class (&jcf);
1211 tmp = (unsigned char *) super_class_name (&jcf, &super_length);
1212 super = ALLOC (super_length + 1);
1213 memcpy (super, tmp, super_length);
1214 super[super_length] = '\0';
1216 result = throwable_p (super);
1217 slot = htab_find_slot (result ? throw_hash : non_throw_hash,
1228 /* Print one piece of a signature. Returns pointer to next parseable
1229 character on success, NULL on error. */
1230 static const unsigned char *
1231 decode_signature_piece (FILE *stream, const unsigned char *signature,
1232 const unsigned char *limit, int *need_space)
1235 int array_depth = 0;
1237 switch (signature[0])
1240 /* More spaghetti. */
1243 for (signature++; (signature < limit
1244 && ISDIGIT (*signature)); signature++)
1249 ctype = "jbyteArray";
1252 ctype = "jcharArray";
1255 ctype = "jdoubleArray";
1258 ctype = "jfloatArray";
1261 ctype = "jintArray";
1264 ctype = "jshortArray";
1267 ctype = "jlongArray";
1270 ctype = "jbooleanArray";
1273 /* We have a nested array. */
1276 fputs ("JArray<", stream);
1280 /* We have to generate a reference to JArray here, so that
1281 our output matches what the compiler does. */
1283 /* Space between `<' and `:' to avoid C++ digraphs. */
1285 fputs ("JArray< ::", stream);
1286 while (signature < limit && *signature != ';')
1288 int ch = UTF8_GET (signature, limit);
1292 fputs ("::", stream);
1294 jcf_print_char (stream, ch);
1298 fputs (" *> *", stream);
1303 /* Unparseable signature. */
1307 /* If the previous iterations left us with something to print,
1308 print it. For JNI, we always print `jobjectArray' in the
1310 if (flag_jni && (ctype == NULL || array_depth > 0))
1312 ctype = "jobjectArray";
1315 /* The `printit' case will advance SIGNATURE for us. If we
1316 don't go there, we must advance past the `;' ourselves. */
1324 /* This shouldn't happen. */
1327 case 'B': ctype = "jbyte"; goto printit;
1328 case 'C': ctype = "jchar"; goto printit;
1329 case 'D': ctype = "jdouble"; goto printit;
1330 case 'F': ctype = "jfloat"; goto printit;
1331 case 'I': ctype = "jint"; goto printit;
1332 case 'J': ctype = "jlong"; goto printit;
1333 case 'S': ctype = "jshort"; goto printit;
1334 case 'Z': ctype = "jboolean"; goto printit;
1335 case 'V': ctype = "void"; goto printit;
1339 /* We know about certain types and special-case their names. */
1340 if (! strncmp ((const char *) signature, "Ljava/lang/String;",
1341 sizeof ("Ljava/lang/String;") -1))
1343 else if (! strncmp ((const char *) signature, "Ljava/lang/Class;",
1344 sizeof ("Ljava/lang/Class;") - 1))
1346 /* Skip leading 'L' for throwable_p call. */
1347 else if (throwable_p (signature + 1))
1348 ctype = "jthrowable";
1352 while (*signature && *signature != ';')
1357 /* Print a leading "::" so we look in the right namespace. */
1358 fputs ("::", stream);
1360 while (*signature && *signature != ';')
1362 int ch = UTF8_GET (signature, limit);
1364 fputs ("::", stream);
1366 jcf_print_char (stream, ch);
1368 fputs (" *", stream);
1369 if (*signature == ';')
1375 jni_print_char (stream, *signature++);
1380 fputs (ctype, stream);
1386 while (array_depth-- > 0)
1387 fputs ("> *", stream);
1394 print_c_decl (FILE* stream, JCF* jcf, int name_index, int signature_index,
1395 int is_init, const char *name_override, int flags)
1397 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1399 fprintf (stream, "<not a UTF8 constant>");
1404 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1405 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1406 const unsigned char *str = str0;
1407 const unsigned char *limit = str + length;
1409 int is_method = str[0] == '(';
1410 const unsigned char *next;
1412 /* If printing a method, skip to the return signature and print
1413 that first. However, there is no return value if this is a
1415 if (is_method && ! is_init)
1425 /* If printing a field or an ordinary method, then print the
1426 "return value" now. */
1427 if (! is_method || ! is_init)
1429 next = decode_signature_piece (stream, str, limit, &need_space);
1432 error ("unparseable signature: `%s'", str0);
1437 /* Force the alignment of the first data member. This is
1438 because the "new" C++ ABI changed the alignment of non-POD
1439 classes. gcj, however, still uses the "old" alignment. */
1440 if (is_first_data_member && ! (flags & ACC_STATIC) && ! is_method)
1442 is_first_data_member = 0;
1443 print_cxx_classname (out, " __attribute__((aligned(__alignof__( ",
1444 jcf, jcf->super_class, 1);
1445 fputs (" )))) ", stream);
1448 /* Now print the name of the thing. */
1450 fputs (" ", stream);
1451 print_full_cxx_name (stream, jcf, name_index,
1452 signature_index, is_init, name_override,
1457 /* Print the unqualified method name followed by the signature. */
1459 print_full_cxx_name (FILE* stream, JCF* jcf, int name_index,
1460 int signature_index, int is_init,
1461 const char *name_override, int flags)
1463 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1464 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1465 const unsigned char *str = str0;
1466 const unsigned char *limit = str + length;
1468 int is_method = str[0] == '(';
1469 const unsigned char *next;
1472 fputs (name_override, stream);
1473 else if (name_index)
1475 /* Declare constructors specially. */
1477 print_base_classname (stream, jcf, jcf->this_class);
1479 print_name (stream, jcf, name_index);
1484 unsigned char *signature = JPOOL_UTF_DATA (jcf, signature_index);
1485 int sig_len = JPOOL_UTF_LENGTH (jcf, signature_index);
1486 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf, name_index),
1487 JPOOL_UTF_LENGTH (jcf, name_index),
1488 (const char *) signature, sig_len))
1490 /* If this method is overloaded by another native method,
1491 then include the argument information in the mangled
1493 unsigned char *limit = signature + sig_len;
1494 fputs ("__", stream);
1495 while (signature < limit)
1497 int ch = UTF8_GET (signature, limit);
1498 jni_print_char (stream, ch);
1510 /* Have a method or a constructor. Print signature pieces
1512 fputs (" (", stream);
1516 /* In JNI mode, add extra arguments. */
1519 /* FIXME: it would be nice to know if we are printing a decl
1520 or a definition, and only print `env' for the latter. */
1521 fputs ("JNIEnv *env", stream);
1523 fputs ((flags & ACC_STATIC) ? ", jclass" : ", jobject", stream);
1526 fputs (", ", stream);
1529 while (str < limit && *str != ')')
1531 next = decode_signature_piece (stream, str, limit, &need_space);
1534 error ("unparseable signature: `%s'", str0);
1538 if (next < limit && *next != ')')
1539 fputs (", ", stream);
1543 fputs (")", stream);
1547 /* This is a helper for print_stub_or_jni. */
1549 print_name_for_stub_or_jni (FILE *stream, JCF *jcf, int name_index,
1550 int signature_index, int is_init,
1551 const char *name_override, int flags)
1553 const char *const prefix = flag_jni ? "Java_" : "";
1554 print_cxx_classname (stream, prefix, jcf, jcf->this_class, 1);
1555 fputs (flag_jni ? "_" : "::", stream);
1556 print_full_cxx_name (stream, jcf, name_index,
1557 signature_index, is_init, name_override,
1562 print_stub_or_jni (FILE* stream, JCF* jcf, int name_index,
1563 int signature_index, int is_init,
1564 const char *name_override, int flags)
1566 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1568 fprintf (stream, "<not a UTF8 constant>");
1573 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1574 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1575 const unsigned char *str = str0;
1576 const unsigned char *limit = str + length;
1578 int is_method = str[0] == '(';
1579 const unsigned char *next;
1581 /* Don't print fields in the JNI case. */
1582 if (! is_method && flag_jni)
1585 if (flag_jni && ! stubs)
1586 fputs ("JNIEXPORT ", stream);
1588 /* If printing a method, skip to the return signature and print
1589 that first. However, there is no return value if this is a
1591 if (is_method && ! is_init)
1601 /* If printing a field or an ordinary method, then print the
1602 "return value" now. Note that a constructor can't be native,
1603 so we don't bother checking this in the JNI case. */
1604 if (! is_method || ! is_init)
1606 next = decode_signature_piece (stream, str, limit, &need_space);
1609 error ("unparseable signature: `%s'", str0);
1614 /* When printing a JNI header we need to respect the space. In
1615 other cases we're just going to insert a newline anyway. */
1616 fputs (need_space && ! stubs ? " " : "\n", stream);
1618 if (flag_jni && ! stubs)
1619 fputs ("JNICALL ", stream);
1621 /* Now print the name of the thing. */
1622 print_name_for_stub_or_jni (stream, jcf, name_index,
1623 signature_index, is_init, name_override,
1626 /* Print the body. */
1630 fputs ("\n{\n (*env)->FatalError (env, \"", stream);
1632 fputs ("\n{\n throw new ::java::lang::UnsupportedOperationException (JvNewStringLatin1 (\"", stream);
1633 print_name_for_stub_or_jni (stream, jcf, name_index,
1634 signature_index, is_init,
1637 fprintf (stream, " not implemented\")%s;\n}\n\n",
1638 flag_jni ? "" : ")");
1644 print_mangled_classname (FILE *stream, JCF *jcf, const char *prefix, int index)
1646 int name_index = JPOOL_USHORT1 (jcf, index);
1647 fputs (prefix, stream);
1648 jcf_print_utf8_replace (out,
1649 JPOOL_UTF_DATA (jcf, name_index),
1650 JPOOL_UTF_LENGTH (jcf, name_index),
1654 /* Print PREFIX, then a class name in C++ format. If the name refers
1655 to an array, ignore it and don't print PREFIX. Returns 1 if
1656 something was printed, 0 otherwise. */
1658 print_cxx_classname (FILE *stream, const char *prefix,
1659 JCF *jcf, int index, int add_scope)
1661 int name_index = JPOOL_USHORT1 (jcf, index);
1663 const unsigned char *s, *p, *limit;
1665 s = JPOOL_UTF_DATA (jcf, name_index);
1666 len = JPOOL_UTF_LENGTH (jcf, name_index);
1669 /* Explicitly omit arrays here. */
1671 c = UTF8_GET (p, limit);
1675 fputs (prefix, stream);
1677 /* Print a leading "::" so we look in the right namespace. */
1678 if (! flag_jni && ! stubs && add_scope)
1679 fputs ("::", stream);
1683 c = UTF8_GET (s, limit);
1685 fputs (flag_jni ? "_" : "::", stream);
1687 jni_print_char (stream, c);
1693 int written_class_count = 0;
1695 /* Return name of superclass. If LEN is not NULL, fill it with length
1697 static const unsigned char *
1698 super_class_name (JCF *derived_jcf, int *len)
1700 int supername_index = JPOOL_USHORT1 (derived_jcf, derived_jcf->super_class);
1701 int supername_length = JPOOL_UTF_LENGTH (derived_jcf, supername_index);
1702 const unsigned char *supername =
1703 JPOOL_UTF_DATA (derived_jcf, supername_index);
1706 *len = supername_length;
1712 handle_inner_classes (int count)
1716 if (out && ! flag_jni && ! stubs && count > 0)
1717 fprintf (out, "\n");
1719 for (i = 0; i < count; ++i)
1721 JCF_u2 inner_info_index = JCF_readu2 (current_jcf);
1723 /* There are a few more values here, but we don't care about
1724 them. The (void) cast is apparently the only way to avoid a
1726 (void) JCF_readu2 (current_jcf);
1727 (void) JCF_readu2 (current_jcf);
1728 (void) JCF_readu2 (current_jcf);
1730 if (out && ! flag_jni && ! stubs)
1732 print_mangled_classname (out, current_jcf, " friend class ",
1734 fprintf (out, ";\n");
1741 /* We keep track of all the `#include's we generate, so we can avoid
1746 struct include *next;
1749 /* List of all includes. */
1750 static struct include *all_includes = NULL;
1752 /* Generate a #include. */
1754 print_include (FILE *out, const unsigned char *utf8, int len)
1756 struct include *incl;
1762 len = strlen ((const char *) utf8);
1764 for (incl = all_includes; incl; incl = incl->next)
1766 /* We check the length because we might have a proper prefix. */
1767 if (len == (int) strlen (incl->name)
1768 && ! strncmp (incl->name, (const char *) utf8, len))
1772 incl = xmalloc (sizeof (struct include));
1773 incl->name = xmalloc (len + 1);
1774 strncpy (incl->name, (const char *) utf8, len);
1775 incl->name[len] = '\0';
1776 incl->next = all_includes;
1777 all_includes = incl;
1779 fputs ("#include <", out);
1780 jcf_print_utf8_replace (out, utf8, len,
1782 flag_jni ? '_' : '/');
1783 fputs (".h>\n", out);
1788 /* This is used to represent part of a package or class name. */
1791 /* The text of this part of the name. */
1793 /* True if this represents a class. */
1795 /* Linked list of all classes and packages inside this one. */
1796 struct namelet *subnamelets;
1797 /* Pointer to next sibling. */
1798 struct namelet *next;
1801 static void add_namelet (const unsigned char *, const unsigned char *,
1803 static void print_namelet (FILE *, struct namelet *, int);
1805 /* The special root namelet. */
1806 static struct namelet root =
1814 /* This extracts the next name segment from the full UTF-8 encoded
1815 package or class name and links it into the tree. It does this
1818 add_namelet (const unsigned char *name, const unsigned char *name_limit,
1819 struct namelet *parent)
1821 const unsigned char *p;
1822 struct namelet *n = NULL, *np;
1824 /* We want to skip the standard namespaces that we assume the
1825 runtime already knows about. We only do this at the top level,
1826 though, hence the check for `root'. */
1827 if (parent == &root)
1829 #define JAVALANG "java/lang/"
1830 #define JAVAIO "java/io/"
1831 #define JAVAUTIL "java/util/"
1832 if ((name_limit - name >= (int) sizeof (JAVALANG) - 1
1833 && ! strncmp ((const char *) name, JAVALANG, sizeof (JAVALANG) - 1))
1834 || (name_limit - name >= (int) sizeof (JAVAUTIL) - 1
1835 && ! strncmp ((const char *) name, JAVAUTIL, sizeof (JAVAUTIL) - 1))
1836 || (name_limit - name >= (int) sizeof (JAVAIO) - 1
1837 && ! strncmp ((const char *) name, JAVAIO, sizeof (JAVAIO) - 1)))
1841 for (p = name; p < name_limit && *p != '/'; ++p)
1844 /* Search for this name beneath the PARENT node. */
1845 for (np = parent->subnamelets; np != NULL; np = np->next)
1847 /* We check the length because we might have a proper prefix. */
1848 if ((int) strlen (np->name) == p - name &&
1849 ! strncmp ((const char *) name, np->name, p - name))
1858 n = xmalloc (sizeof (struct namelet));
1859 n->name = xmalloc (p - name + 1);
1860 strncpy (n->name, (const char *) name, p - name);
1861 n->name[p - name] = '\0';
1862 n->is_class = (p == name_limit);
1863 n->subnamelets = NULL;
1864 n->next = parent->subnamelets;
1865 parent->subnamelets = n;
1868 /* We recurse if there is more text, and if the trailing piece does
1869 not represent an inner class. */
1871 add_namelet (p + 1, name_limit, n);
1874 /* Print a single namelet. Destroys namelets while printing. */
1876 print_namelet (FILE *out, struct namelet *name, int depth)
1883 for (i = 0; i < depth; ++i)
1885 fprintf (out, "%s %s", name->is_class ? "class" : "namespace",
1887 if (name->is_class && name->subnamelets == NULL)
1893 for (i = 0; i < depth; ++i)
1899 c = name->subnamelets;
1902 struct namelet *next = c->next;
1903 print_namelet (out, c, depth + 2);
1906 name->subnamelets = NULL;
1912 for (i = 0; i < depth; ++i)
1915 /* Only print a `;' when printing a class. C++ is evil. */
1925 /* This is called to add some classes to the list of classes for which
1926 we need decls. The signature argument can be a function
1929 add_class_decl (FILE *out, JCF *jcf, JCF_u2 signature)
1931 const unsigned char *s = JPOOL_UTF_DATA (jcf, signature);
1932 int len = JPOOL_UTF_LENGTH (jcf, signature);
1935 for (i = 0; i < len; ++i)
1939 /* If we see an array, then we include the array header. */
1942 print_include (out, (const unsigned char *) "gcj/array", -1);
1946 /* We're looking for `L<stuff>;' -- everything else is
1951 for (start = ++i; i < len && s[i] != ';'; ++i)
1954 add_namelet (&s[start], &s[i], &root);
1958 /* Print declarations for all classes required by this class. Any
1959 class or package in the `java' package is assumed to be handled
1960 statically in libjava; we don't generate declarations for these.
1961 This makes the generated headers a bit easier to read. */
1963 print_class_decls (FILE *out, JCF *jcf, int self)
1965 /* Make sure to always add the current class to the list of things
1966 that should be declared. */
1967 int name_index = JPOOL_USHORT1 (jcf, self);
1969 const unsigned char *s;
1971 s = JPOOL_UTF_DATA (jcf, name_index);
1972 len = JPOOL_UTF_LENGTH (jcf, name_index);
1973 add_namelet (s, s + len, &root);
1975 if (root.subnamelets)
1977 fputs ("extern \"Java\"\n{\n", out);
1978 /* We use an initial offset of 0 because the root namelet
1979 doesn't cause anything to print. */
1980 print_namelet (out, &root, 0);
1981 fputs ("}\n\n", out);
1988 process_file (JCF *jcf, FILE *out)
1991 uint32 field_start, method_end, method_start;
1997 if (jcf_parse_preamble (jcf) != 0)
1999 error ("Not a valid Java .class file.");
2003 /* Parse and possibly print constant pool */
2004 code = jcf_parse_constant_pool (jcf);
2007 error ("error while parsing constant pool");
2010 code = verify_constant_pool (jcf);
2013 error ("error in constant pool entry #%d", code);
2017 jcf_parse_class (jcf);
2019 if (written_class_count++ == 0 && out)
2021 const char *cstart, *cstart2, *mode, *cend, *what, *jflag;
2036 mode = " -*- c++ -*-";
2042 fprintf (out, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
2043 cstart, mode, cend);
2046 fprintf (out, "%s This file was created by `gcjh -stubs%s'.%s\n\
2048 %s This file is intended to give you a head start on implementing native\n\
2049 %s methods using %s.\n\
2050 %s Be aware: running `gcjh -stubs %s' once more for this class may\n\
2051 %s overwrite any edits you have made to this file.%s\n\n",
2052 cstart, jflag, mode,
2068 print_mangled_classname (out, jcf, "#ifndef __", jcf->this_class);
2069 fprintf (out, "__\n");
2071 print_mangled_classname (out, jcf, "#define __", jcf->this_class);
2072 fprintf (out, "__\n\n");
2076 fprintf (out, "#include <jni.h>\n\n");
2077 fprintf (out, "#ifdef __cplusplus\n");
2078 fprintf (out, "extern \"C\"\n");
2079 fprintf (out, "{\n");
2080 fprintf (out, "#endif\n");
2084 /* We do this to ensure that inline methods won't be
2085 `outlined' by g++. This works as long as method and
2086 fields are not added by the user. */
2087 fprintf (out, "#pragma interface\n");
2089 if (jcf->super_class)
2092 const unsigned char *supername =
2093 super_class_name (jcf, &super_length);
2096 print_include (out, supername, super_length);
2102 /* Strip off the ".class" portion of the name when printing
2103 the include file name. */
2105 int i, len = strlen (jcf->classname);
2106 if (len > 6 && ! strcmp (&jcf->classname[len - 6], ".class"))
2108 /* Turn the class name into a file name. */
2109 name = xmalloc (len + 1);
2110 for (i = 0; i < len; ++i)
2111 name[i] = jcf->classname[i] == '.' ? '/' : jcf->classname[i];
2113 print_include (out, (const unsigned char *) name, len);
2118 print_include (out, (const unsigned char *) "gcj/cni", -1);
2119 print_include (out, (const unsigned char *) "java/lang/UnsupportedOperationException",
2125 /* We want to parse the methods first. But we need to find where
2126 they start. So first we skip the fields, then parse the methods.
2127 Then we parse the fields and skip the methods. This is ugly, but
2128 not too bad since we need two full passes to get class decl
2129 information anyway. */
2131 field_start = JCF_TELL (jcf);
2132 jcf_parse_fields (jcf);
2134 method_start = JCF_TELL (jcf);
2136 jcf_parse_methods (jcf);
2141 if (out && ! flag_jni)
2144 print_class_decls (out, jcf, jcf->this_class);
2146 for (i = 0; i < prepend_count; ++i)
2147 fprintf (out, "%s\n", prepend_specs[i]);
2148 if (prepend_count > 0)
2153 if (! print_cxx_classname (out, "class ", jcf,
2154 jcf->this_class, 0))
2156 error ("class is of array type\n");
2159 if (jcf->super_class)
2161 if (! print_cxx_classname (out, " : public ",
2162 jcf, jcf->super_class, 1))
2164 error ("base class is of array type");
2169 fputs ("\n{\n", out);
2173 /* Now go back for second pass over methods and fields. */
2174 is_first_data_member = 1;
2176 JCF_SEEK (jcf, method_start);
2178 jcf_parse_methods (jcf);
2179 method_end = JCF_TELL (jcf);
2182 JCF_SEEK (jcf, field_start);
2183 jcf_parse_fields (jcf);
2184 JCF_SEEK (jcf, method_end);
2186 jcf_parse_final_attributes (jcf);
2192 fprintf (out, "\n#ifdef __cplusplus\n");
2193 fprintf (out, "}\n");
2194 fprintf (out, "#endif\n");
2198 /* Generate friend decl if we still must. */
2199 for (i = 0; i < friend_count; ++i)
2200 fprintf (out, " friend %s\n", friend_specs[i]);
2202 /* Generate extra declarations. */
2205 for (i = 0; i < add_count; ++i)
2206 fprintf (out, " %s\n", add_specs[i]);
2208 /* Generate an entry for the class object. */
2209 generate_access (out, ACC_PUBLIC);
2210 fprintf (out, "\n static ::java::lang::Class class$;\n");
2214 if (jcf->access_flags & ACC_INTERFACE)
2215 fputs (" __attribute__ ((java_interface))", out);
2219 if (append_count > 0)
2221 for (i = 0; i < append_count; ++i)
2222 fprintf (out, "%s\n", append_specs[i]);
2225 print_mangled_classname (out, jcf,
2226 "\n#endif /* __", jcf->this_class);
2227 fprintf (out, "__ */\n");
2233 /* This is used to mark options with no short value. */
2234 #define LONG_OPT(Num) ((Num) + 128)
2236 #define OPT_classpath LONG_OPT (0)
2237 #define OPT_CLASSPATH OPT_classpath
2238 #define OPT_bootclasspath LONG_OPT (1)
2239 #define OPT_extdirs LONG_OPT (2)
2240 #define OPT_HELP LONG_OPT (3)
2241 #define OPT_TEMP LONG_OPT (4)
2242 #define OPT_VERSION LONG_OPT (5)
2243 #define OPT_PREPEND LONG_OPT (6)
2244 #define OPT_FRIEND LONG_OPT (7)
2245 #define OPT_ADD LONG_OPT (8)
2246 #define OPT_APPEND LONG_OPT (9)
2247 #define OPT_M LONG_OPT (10)
2248 #define OPT_MM LONG_OPT (11)
2249 #define OPT_MG LONG_OPT (12)
2250 #define OPT_MD LONG_OPT (13)
2251 #define OPT_MMD LONG_OPT (14)
2253 static const struct option options[] =
2255 { "classpath", required_argument, NULL, OPT_classpath },
2256 { "bootclasspath", required_argument, NULL, OPT_bootclasspath },
2257 { "extdirs", required_argument, NULL, OPT_extdirs },
2258 { "CLASSPATH", required_argument, NULL, OPT_CLASSPATH },
2259 { "help", no_argument, NULL, OPT_HELP },
2260 { "stubs", no_argument, &stubs, 1 },
2261 { "td", required_argument, NULL, OPT_TEMP },
2262 { "verbose", no_argument, NULL, 'v' },
2263 { "version", no_argument, NULL, OPT_VERSION },
2264 { "prepend", required_argument, NULL, OPT_PREPEND },
2265 { "friend", required_argument, NULL, OPT_FRIEND },
2266 { "add", required_argument, NULL, OPT_ADD },
2267 { "append", required_argument, NULL, OPT_APPEND },
2268 { "M", no_argument, NULL, OPT_M },
2269 { "MM", no_argument, NULL, OPT_MM },
2270 { "MG", no_argument, NULL, OPT_MG },
2271 { "MD", no_argument, NULL, OPT_MD },
2272 { "MMD", no_argument, NULL, OPT_MMD },
2273 { "jni", no_argument, &flag_jni, 1 },
2274 { NULL, no_argument, NULL, 0 }
2280 fprintf (stderr, _("Try `gcjh --help' for more information.\n"));
2287 printf (_("Usage: gcjh [OPTION]... CLASS...\n\n"));
2288 printf (_("Generate C++ header files from .class files\n\n"));
2289 printf (_(" -stubs Generate an implementation stub file\n"));
2290 printf (_(" -jni Generate a JNI header or stub\n"));
2292 printf (_(" -add TEXT Insert TEXT into class body\n"));
2293 printf (_(" -append TEXT Insert TEXT after class declaration\n"));
2294 printf (_(" -friend TEXT Insert TEXT as `friend' declaration\n"));
2295 printf (_(" -prepend TEXT Insert TEXT before start of class\n"));
2297 printf (_(" --classpath PATH Set path to find .class files\n"));
2298 printf (_(" -IDIR Append directory to class path\n"));
2299 printf (_(" --bootclasspath PATH Override built-in class path\n"));
2300 printf (_(" --extdirs PATH Set extensions directory path\n"));
2301 printf (_(" -d DIRECTORY Set output directory name\n"));
2302 printf (_(" -o FILE Set output file name\n"));
2303 printf (_(" -td DIRECTORY Set temporary directory name\n"));
2305 printf (_(" --help Print this help, then exit\n"));
2306 printf (_(" --version Print version number, then exit\n"));
2307 printf (_(" -v, --verbose Print extra information while running\n"));
2309 printf (_(" -M Print all dependencies to stdout;\n"
2310 " suppress ordinary output\n"));
2311 printf (_(" -MM Print non-system dependencies to stdout;\n"
2312 " suppress ordinary output\n"));
2313 printf (_(" -MD Print all dependencies to stdout\n"));
2314 printf (_(" -MMD Print non-system dependencies to stdout\n"));
2315 /* We omit -MG until it is implemented. */
2317 printf (_("For bug reporting instructions, please see:\n"
2318 "%s.\n"), bug_report_url);
2325 printf ("gcjh (GCC) %s\n\n", version_string);
2326 printf ("Copyright %s 2004 Free Software Foundation, Inc.\n", _("(C)"));
2327 printf (_("This is free software; see the source for copying conditions. There is NO\n"
2328 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
2333 main (int argc, char** argv)
2337 char *output_file = NULL;
2338 int emit_dependencies = 0, suppress_output = 0;
2341 gcc_init_libintl ();
2345 error ("no classes specified");
2351 /* We use getopt_long_only to allow single `-' long options. For
2352 some of our options this is more natural. */
2353 while ((opt = getopt_long_only (argc, argv, "I:d:o:v", options, NULL)) != -1)
2358 /* Already handled. */
2362 output_file = optarg;
2366 output_directory = optarg;
2370 jcf_path_include_arg (optarg);
2378 jcf_path_classpath_arg (optarg);
2381 case OPT_bootclasspath:
2382 jcf_path_bootclasspath_arg (optarg);
2386 jcf_path_extdirs_arg (optarg);
2394 temp_directory = optarg;
2402 if (prepend_count == 0)
2403 prepend_specs = ALLOC (argc * sizeof (char*));
2404 prepend_specs[prepend_count++] = optarg;
2408 if (friend_count == 0)
2409 friend_specs = ALLOC (argc * sizeof (char*));
2410 friend_specs[friend_count++] = optarg;
2415 add_specs = ALLOC (argc * sizeof (char*));
2416 add_specs[add_count++] = optarg;
2420 if (append_count == 0)
2421 append_specs = ALLOC (argc * sizeof (char*));
2422 append_specs[append_count++] = optarg;
2426 emit_dependencies = 1;
2427 suppress_output = 1;
2428 jcf_dependency_init (1);
2432 emit_dependencies = 1;
2433 suppress_output = 1;
2434 jcf_dependency_init (0);
2438 error ("`-MG' option is unimplemented");
2442 emit_dependencies = 1;
2443 jcf_dependency_init (1);
2447 emit_dependencies = 1;
2448 jcf_dependency_init (0);
2459 error ("no classes specified");
2463 jcf_path_seal (verbose);
2465 if (output_file && emit_dependencies)
2467 error ("can't specify both -o and -MD");
2471 for (argi = optind; argi < argc; argi++)
2473 char *classname = argv[argi];
2474 char *current_output_file;
2475 const char *classfile_name;
2478 printf (_("Processing %s\n"), classname);
2480 jcf_dependency_reset ();
2481 classfile_name = find_class (classname, strlen (classname), &jcf, 0);
2482 if (classfile_name == NULL)
2484 error ("%s: no such class", classname);
2488 printf (_("Found in %s\n"), classfile_name);
2491 if (strcmp (output_file, "-") == 0)
2493 else if (out == NULL)
2495 out = fopen (output_file, "w");
2499 perror (output_file);
2502 current_output_file = output_file;
2506 int dir_len = strlen (output_directory);
2507 int i, classname_length = strlen (classname);
2508 current_output_file = ALLOC (dir_len + classname_length + 5);
2509 strcpy (current_output_file, output_directory);
2510 if (dir_len > 0 && output_directory[dir_len-1] != '/')
2511 current_output_file[dir_len++] = '/';
2512 for (i = 0; classname[i] != '\0'; i++)
2514 char ch = classname[i];
2517 if (flag_jni && ch == '/')
2519 current_output_file[dir_len++] = ch;
2521 if (emit_dependencies)
2523 if (suppress_output)
2525 jcf_dependency_set_dep_file ("-");
2530 /* We use `.hd' and not `.d' to avoid clashes with
2531 dependency tracking from straight compilation. */
2532 strcpy (current_output_file + dir_len, ".hd");
2533 jcf_dependency_set_dep_file (current_output_file);
2536 strcpy (current_output_file + dir_len,
2537 stubs ? (flag_jni ? ".c" : ".cc") : ".h");
2538 jcf_dependency_set_target (current_output_file);
2539 if (! suppress_output)
2541 out = fopen (current_output_file, "w");
2544 perror (current_output_file);
2549 free_method_name_list ();
2550 process_file (&jcf, out);
2552 if (current_output_file != output_file)
2553 free (current_output_file);
2554 jcf_dependency_write ();
2557 if (out != NULL && out != stdout)