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, 2005, 2006
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, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, 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;
57 #define TOOLNAME "gjnih"
59 /* Nonzero if we're generating JNI output. */
62 #define TOOLNAME "gcjh"
67 /* When nonzero, warn when source file is newer than matching class
71 /* Directory to place resulting files in. Set by -d option. */
72 static const char *output_directory
= "";
74 /* Directory to place temporary file. Set by -td option. Currently unused. */
75 static const char *temp_directory
= "/tmp";
77 /* Number of friend functions we have to declare. */
78 static int friend_count
;
80 /* A class can optionally have a `friend' function declared. If
81 non-NULL, this is that function. */
82 static char **friend_specs
= NULL
;
84 /* Number of lines we are prepending before the class. */
85 static int prepend_count
;
87 /* We can prepend extra lines before the class's start. */
88 static char **prepend_specs
= NULL
;
90 /* Number of lines we are appending at the end of the class. */
93 /* We can append extra lines just before the class's end. */
94 static char **add_specs
= NULL
;
96 /* Number of lines we are appending after the class. */
97 static int append_count
;
99 /* We can append extra lines after the class's end. */
100 static char **append_specs
= NULL
;
106 struct JCF
*current_jcf
;
108 /* This holds access information for the last field we examined. They
109 let us generate "private:", "public:", and "protected:" properly.
110 If 0 then we haven't previously examined any field. */
111 static JCF_u2 last_access
;
113 /* Pass this macro the flags for a class and for a method. It will
114 return true if the method should be considered `final'. */
115 #define METHOD_IS_FINAL(Class, Method) \
116 (((Class) & ACC_FINAL) || ((Method) & (ACC_FINAL | ACC_PRIVATE)))
118 /* Pass this macro the flags for a method. It will return true if the
120 #define METHOD_IS_NATIVE(Method) \
121 ((Method) & ACC_NATIVE)
123 #define METHOD_IS_PRIVATE(Class, Method) \
124 (((Method) & ACC_PRIVATE) != 0)
126 /* We keep a linked list of all method names we have seen. This lets
127 us determine if a method name and a field name are in conflict. */
132 unsigned char *signature
;
135 struct method_name
*next
;
138 /* List of method names we've seen. */
139 static struct method_name
*method_name_list
;
141 static void print_field_info (FILE*, JCF
*, int, int, JCF_u2
);
142 static void print_mangled_classname (FILE*, JCF
*, const char*, int);
143 static int print_cxx_classname (FILE*, const char*, JCF
*, int, int);
144 static void print_method_info (FILE*, JCF
*, int, int, JCF_u2
);
145 static void print_c_decl (FILE*, JCF
*, int, int, int, const char *, int);
146 static void print_stub_or_jni (FILE*, JCF
*, int, int, int, const char *, int);
147 static void print_full_cxx_name (FILE*, JCF
*, int, int, int, const char *, int);
148 static void decompile_method (FILE*, JCF
*, int) ATTRIBUTE_UNUSED
;
149 static void add_class_decl (FILE*, JCF
*, JCF_u2
);
151 static void print_name (FILE *, JCF
*, int);
152 static void print_base_classname (FILE *, JCF
*, int);
153 static int utf8_cmp (const unsigned char *, int, const char *);
154 static char *cxx_keyword_subst (const unsigned char *, int);
155 static void generate_access (FILE *, JCF_u2
);
156 static int name_is_method_p (const unsigned char *, int);
157 static char *get_field_name (JCF
*, int, JCF_u2
);
158 static void print_field_name (FILE *, JCF
*, int, JCF_u2
);
159 static const unsigned char *super_class_name (JCF
*, int *);
160 static void print_include (FILE *, const unsigned char *, int);
161 static int gcjh_streq (const void *p1
, const void *p2
);
162 static int throwable_p (const unsigned char *signature
);
163 static const unsigned char *
164 decode_signature_piece (FILE *, const unsigned char *,
165 const unsigned char *, int *);
166 static void print_class_decls (FILE *, JCF
*, int);
167 static void error (const char *gmsgid
, ...) ATTRIBUTE_PRINTF_1
;
168 static void usage (void) ATTRIBUTE_NORETURN
;
169 static void help (void) ATTRIBUTE_NORETURN
;
170 static void version (void) ATTRIBUTE_NORETURN
;
171 static int overloaded_jni_method_exists_p (const unsigned char *, int,
173 static void jni_print_char (FILE *, int);
174 static void jni_print_float (FILE *, jfloat
);
175 static void jni_print_double (FILE *, jdouble
);
176 static void decompile_return_statement (FILE *, JCF
*, int, int, int);
178 static void handle_inner_classes (int);
180 JCF_u2 current_field_name
;
181 JCF_u2 current_field_value
;
182 JCF_u2 current_field_signature
;
183 JCF_u2 current_field_flags
;
185 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
186 ( current_field_name = (NAME), current_field_signature = (SIGNATURE), \
187 current_field_flags = (ACCESS_FLAGS), current_field_value = 0)
189 /* We pass over fields twice. The first time we just note the types
190 of the fields and then the start of the methods. Then we go back
191 and parse the fields for real. This is ugly. */
192 static int field_pass
;
193 /* Likewise we pass over methods twice. The first time we generate
194 class decl information; the second time we generate actual method
196 static int method_pass
;
198 #define HANDLE_END_FIELD() \
201 if (out && ! stubs) \
202 print_field_info (out, jcf, current_field_name, \
203 current_field_signature, \
204 current_field_flags); \
206 else if (! stubs && ! flag_jni) \
207 add_class_decl (out, jcf, current_field_signature);
209 #define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)
211 static int method_declared
= 0;
212 static int method_access
= 0;
213 static int method_printed
= 0;
214 static int method_synthetic
= 0;
215 static int method_signature
= 0;
217 /* Set to 1 while the very first data member of a class is being handled. */
218 static int is_first_data_member
= 0;
220 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
222 method_synthetic = 0; \
223 method_printed = 0; \
225 method_signature = SIGNATURE; \
226 if (ATTRIBUTE_COUNT) \
227 method_synthetic = peek_attribute (jcf, ATTRIBUTE_COUNT, \
228 (const char *)"Synthetic", 9); \
229 /* If a synthetic methods have been declared, its attribute aren't \
230 worth reading (and triggering side-effects). We skip them an \
231 set ATTRIBUTE_COUNT to zero so that they'll be skipped in \
232 jcf_parse_one_method. */ \
233 if (method_synthetic) \
235 skip_attribute (jcf, ATTRIBUTE_COUNT); \
236 ATTRIBUTE_COUNT = 0; \
238 if (method_pass && !method_synthetic) \
241 print_method_info (out, jcf, NAME, SIGNATURE, \
244 else if (!method_synthetic) \
246 print_method_info (NULL, jcf, NAME, SIGNATURE, \
248 if (! stubs && ! flag_jni) \
249 add_class_decl (out, jcf, SIGNATURE); \
253 /* Only include byte-code decompilation optimizations for ELF targets
254 since the generated headers are only known to work with ELF weak
255 symbol semantics. Specifically, these optimizations are known to
256 not work on PE-COFF and possibly others. */
257 #ifdef OBJECT_FORMAT_ELF
258 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
259 if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
262 static int decompiled
= 0;
263 #define HANDLE_END_METHOD() \
264 if (out && method_printed && !method_synthetic) \
265 fputs (decompiled || stubs ? "\n" : ";\n", out);
267 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) handle_inner_classes (COUNT)
269 /* We're going to need {peek,skip}_attribute, enable their definition. */
270 #define NEED_PEEK_ATTRIBUTE
271 #define NEED_SKIP_ATTRIBUTE
273 #include "jcf-reader.c"
275 /* Print an error message and set found_error.
276 Not really gcc-internal-format message, but as error elsewhere
277 uses it, assume all users will use intersection between
278 c-format and gcc-internal-format. */
280 error (const char *gmsgid
, ...)
284 va_start (ap
, gmsgid
);
286 fprintf (stderr
, TOOLNAME
": ");
287 vfprintf (stderr
, _(gmsgid
), ap
);
289 fprintf (stderr
, "\n");
293 /* Print a single-precision float, suitable for parsing by g++. */
295 jni_print_float (FILE *stream
, jfloat f
)
297 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
298 work in data initializers. FIXME. */
299 if (JFLOAT_FINITE (f
))
307 fprintf (stream
, "0x1.%.6xp%+df",
308 ((unsigned int)f
.mantissa
) << 1,
309 f
.exponent
- JFLOAT_EXP_BIAS
);
311 /* Exponent of 0x01 is -125; exponent of 0x00 is *also* -125,
312 because the implicit leading 1 bit is no longer present. */
313 fprintf (stream
, "0x0.%.6xp%+df",
314 ((unsigned int)f
.mantissa
) << 1,
315 f
.exponent
+ 1 - JFLOAT_EXP_BIAS
);
319 fputs (";\n", stream
);
322 /* Print a double-precision float, suitable for parsing by g++. */
324 jni_print_double (FILE *stream
, jdouble f
)
326 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
327 work in data initializers. FIXME. */
328 if (JDOUBLE_FINITE (f
))
336 fprintf (stream
, "0x1.%.5x%.8xp%+d",
337 f
.mantissa0
, f
.mantissa1
,
338 f
.exponent
- JDOUBLE_EXP_BIAS
);
340 /* Exponent of 0x001 is -1022; exponent of 0x000 is *also* -1022,
341 because the implicit leading 1 bit is no longer present. */
342 fprintf (stream
, "0x0.%.5x%.8xp%+d",
343 f
.mantissa0
, f
.mantissa1
,
344 f
.exponent
+ 1 - JDOUBLE_EXP_BIAS
);
347 fputs (flag_jni
? "\n" : ";\n", stream
);
350 /* Print a character, appropriately mangled for JNI. */
353 jni_print_char (FILE *stream
, int ch
)
356 jcf_print_char (stream
, ch
);
357 else if (ch
== '(' || ch
== ')')
362 fputs ("_1", stream
);
364 fputs ("_2", stream
);
366 fputs ("_3", stream
);
369 else if (ISALNUM (ch
))
373 /* "Unicode" character. */
374 fprintf (stream
, "_0%04x", ch
);
378 /* Print a name from the class data. If the index does not point to a
379 string, an error results. */
382 print_name (FILE* stream
, JCF
* jcf
, int name_index
)
384 if (JPOOL_TAG (jcf
, name_index
) != CONSTANT_Utf8
)
386 fprintf (stream
, "<not a UTF8 constant>");
390 jcf_print_utf8 (stream
, JPOOL_UTF_DATA (jcf
, name_index
),
391 JPOOL_UTF_LENGTH (jcf
, name_index
));
394 /* For JNI we must correctly quote each character. */
395 const unsigned char *str
= JPOOL_UTF_DATA (jcf
, name_index
);
396 int length
= JPOOL_UTF_LENGTH (jcf
, name_index
);
397 const unsigned char *limit
= str
+ length
;
400 int ch
= UTF8_GET (str
, limit
);
403 fprintf (stream
, "\\<invalid>");
406 jni_print_char (stream
, ch
);
411 /* Print base name of class. The base name is everything after the
415 print_base_classname (FILE *stream
, JCF
*jcf
, int index
)
417 int name_index
= JPOOL_USHORT1 (jcf
, index
);
419 const unsigned char *s
, *p
, *limit
;
421 s
= JPOOL_UTF_DATA (jcf
, name_index
);
422 len
= JPOOL_UTF_LENGTH (jcf
, name_index
);
427 int c
= UTF8_GET (s
, limit
);
434 int ch
= UTF8_GET (p
, limit
);
436 fputs ("::", stream
);
438 jcf_print_char (stream
, ch
);
442 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
443 and 1 if STR is "greater" than NAME. */
446 utf8_cmp (const unsigned char *str
, int length
, const char *name
)
448 const unsigned char *limit
= str
+ length
;
451 for (i
= 0; name
[i
]; ++i
)
453 int ch
= UTF8_GET (str
, limit
);
458 return str
== limit
? 0 : 1;
461 /* This is a sorted list of all C++ keywords. */
463 static const char *const cxx_keywords
[] =
572 /* If NAME is the name of a C++ keyword, then return an override name.
573 This is a name that can be used in place of the keyword.
574 Otherwise, return NULL. The return value is malloc()d. */
577 cxx_keyword_subst (const unsigned char *str
, int length
)
579 int last
= ARRAY_SIZE (cxx_keywords
);
581 int mid
= (last
+ first
) / 2;
584 for (mid
= (last
+ first
) / 2;
586 old
= mid
, mid
= (last
+ first
) / 2)
588 int kwl
= strlen (cxx_keywords
[mid
]);
589 int min_length
= kwl
> length
? length
: kwl
;
590 int r
= utf8_cmp (str
, min_length
, cxx_keywords
[mid
]);
596 /* Skip all trailing `$'. */
597 for (i
= min_length
; i
< length
&& str
[i
] == '$'; ++i
)
599 /* We've only found a match if all the remaining characters
603 char *dup
= XNEWVEC (char, 2 + length
- min_length
+ kwl
);
604 strcpy (dup
, cxx_keywords
[mid
]);
605 for (i
= kwl
; i
< length
+ 1; ++i
)
621 /* Generate an access control keyword based on FLAGS. */
624 generate_access (FILE *stream
, JCF_u2 flags
)
626 if ((flags
& ACC_VISIBILITY
) == last_access
)
628 last_access
= (flags
& ACC_VISIBILITY
);
633 fputs ("public: // actually package-private\n", stream
);
636 fputs ("public:\n", stream
);
639 fputs ("private:\n", stream
);
642 fputs ("public: // actually protected\n", stream
);
646 fprintf (stream
, "#error unrecognized visibility %d\n",
647 (flags
& ACC_VISIBILITY
));
652 /* See if NAME is already the name of a method. */
654 name_is_method_p (const unsigned char *name
, int length
)
656 struct method_name
*p
;
658 for (p
= method_name_list
; p
!= NULL
; p
= p
->next
)
660 if (p
->length
== length
&& ! memcmp (p
->name
, name
, length
))
666 /* Free the method name list. */
668 free_method_name_list (void)
670 struct method_name
*p
= method_name_list
;
673 struct method_name
*next
= p
->next
;
679 method_name_list
= NULL
;
682 /* If there is already a native method named NAME, whose signature is not
683 SIGNATURE, then return true. Otherwise return false. */
685 overloaded_jni_method_exists_p (const unsigned char *name
, int length
,
686 const char *signature
, int sig_length
)
688 struct method_name
*p
;
690 for (p
= method_name_list
; p
!= NULL
; p
= p
->next
)
693 && p
->length
== length
694 && ! memcmp (p
->name
, name
, length
)
695 && (p
->sig_length
!= sig_length
696 || memcmp (p
->signature
, signature
, sig_length
)))
702 /* Get name of a field. This handles renamings due to C++ clash. */
704 get_field_name (JCF
*jcf
, int name_index
, JCF_u2 flags
)
706 unsigned char *name
= JPOOL_UTF_DATA (jcf
, name_index
);
707 int length
= JPOOL_UTF_LENGTH (jcf
, name_index
);
710 if (name_is_method_p (name
, length
))
712 /* This field name matches a method. So override the name with
713 a dummy name. This is yucky, but it isn't clear what else to
714 do. FIXME: if the field is static, then we'll be in real
716 if ((flags
& ACC_STATIC
))
718 error ("static field has same name as method");
722 override
= XNEWVEC (char, length
+ 3);
723 memcpy (override
, name
, length
);
724 strcpy (override
+ length
, "__");
729 override
= cxx_keyword_subst (name
, length
);
734 /* Print a field name. Convenience function for use with
737 print_field_name (FILE *stream
, JCF
*jcf
, int name_index
, JCF_u2 flags
)
739 char *override
= get_field_name (jcf
, name_index
, flags
);
743 fputs (override
, stream
);
747 jcf_print_utf8 (stream
, JPOOL_UTF_DATA (jcf
, name_index
),
748 JPOOL_UTF_LENGTH (jcf
, name_index
));
752 print_field_info (FILE *stream
, JCF
* jcf
, int name_index
, int sig_index
,
755 char *override
= NULL
;
758 generate_access (stream
, flags
);
759 if (JPOOL_TAG (jcf
, name_index
) != CONSTANT_Utf8
)
761 fprintf (stream
, "<not a UTF8 constant>");
768 /* For JNI we only want to print real constants. */
770 if (! (flags
& ACC_STATIC
)
771 || ! (flags
& ACC_FINAL
)
772 || current_field_value
<= 0)
774 val
= JPOOL_TAG (jcf
, current_field_value
);
775 if (val
!= CONSTANT_Integer
&& val
!= CONSTANT_Long
776 && val
!= CONSTANT_Float
&& val
!= CONSTANT_Double
)
781 /* Initial indentation. */
785 if ((flags
& ACC_STATIC
))
789 print_cxx_classname (stream
, "#undef ", jcf
, jcf
->this_class
, 1);
791 print_field_name (stream
, jcf
, name_index
, 0);
792 fputs ("\n", stream
);
793 print_cxx_classname (stream
, "#define ", jcf
, jcf
->this_class
, 1);
797 fputs ("static ", stream
);
799 if ((flags
& ACC_FINAL
) && current_field_value
> 0)
804 switch (JPOOL_TAG (jcf
, current_field_value
))
806 case CONSTANT_Integer
:
809 int most_negative
= 0;
811 fputs ("const jint ", stream
);
812 print_field_name (stream
, jcf
, name_index
, 0);
813 fputs (flag_jni
? " " : " = ", stream
);
814 num
= JPOOL_INT (jcf
, current_field_value
);
815 /* We single out the most negative number to print
816 specially. This avoids later warnings from g++. */
817 if (num
== (jint
) 0x80000000)
822 format_int (buffer
, (jlong
) num
, 10);
823 fprintf (stream
, "%sL%s%s\n", buffer
,
824 most_negative
? " - 1" : "",
825 flag_jni
? "" : ";");
831 int most_negative
= 0;
833 fputs ("const jlong ", stream
);
834 print_field_name (stream
, jcf
, name_index
, 0);
835 fputs (flag_jni
? " " : " = ", stream
);
836 num
= JPOOL_LONG (jcf
, current_field_value
);
837 /* We single out the most negative number to print
838 specially.. This avoids later warnings from g++. */
839 if (num
== (jlong
) 0x8000000000000000LL
)
844 format_int (buffer
, num
, 10);
845 fprintf (stream
, "%sLL%s%s\n", buffer
,
846 most_negative
? " - 1" :"",
847 flag_jni
? "" : ";");
852 jfloat fnum
= JPOOL_FLOAT (jcf
, current_field_value
);
854 fputs ("const jfloat ", stream
);
855 print_field_name (stream
, jcf
, name_index
, 0);
856 jni_print_float (stream
, fnum
);
859 case CONSTANT_Double
:
861 jdouble dnum
= JPOOL_DOUBLE (jcf
, current_field_value
);
863 fputs ("const jdouble ", stream
);
864 print_field_name (stream
, jcf
, name_index
, 0);
865 jni_print_double (stream
, dnum
);
869 /* We can't print this as a constant, but we can still
870 print something sensible. */
880 /* assert (! flag_jni); */
881 override
= get_field_name (jcf
, name_index
, flags
);
882 print_c_decl (stream
, jcf
, name_index
, sig_index
, 0, override
, flags
);
883 fputs (";\n", stream
);
891 print_method_info (FILE *stream
, JCF
* jcf
, int name_index
, int sig_index
,
894 const unsigned char *str
;
895 int length
, is_init
= 0;
896 char *override
= NULL
;
899 method_access
= flags
;
900 if (stream
&& JPOOL_TAG (jcf
, name_index
) != CONSTANT_Utf8
)
901 fprintf (stream
, "<not a UTF8 constant>");
902 str
= JPOOL_UTF_DATA (jcf
, name_index
);
903 length
= JPOOL_UTF_LENGTH (jcf
, name_index
);
907 /* Ignore the internally generated method <clinit>. However,
908 treat <init> as a constructor. */
909 if (! utf8_cmp (str
, length
, "<init>"))
911 else if (! METHOD_IS_FINAL (jcf
->access_flags
, flags
)
912 && ! (flags
& ACC_STATIC
))
914 /* FIXME: i18n bug here. Order of prints should not be
916 fprintf (stderr
, _("ignored method '"));
917 jcf_print_utf8 (stderr
, str
, length
);
918 fprintf (stderr
, _("' marked virtual\n"));
926 /* During the first method pass, build a list of method names. This will
927 be used to determine if field names conflict with method names. */
930 struct method_name
*nn
;
932 nn
= XNEW (struct method_name
);
933 nn
->name
= XNEWVEC (unsigned char, length
);
934 memcpy (nn
->name
, str
, length
);
936 nn
->next
= method_name_list
;
937 nn
->sig_length
= JPOOL_UTF_LENGTH (jcf
, sig_index
);
938 nn
->signature
= XNEWVEC (unsigned char, nn
->sig_length
);
939 nn
->is_native
= METHOD_IS_NATIVE (flags
);
940 memcpy (nn
->signature
, JPOOL_UTF_DATA (jcf
, sig_index
),
942 method_name_list
= nn
;
944 /* The rest of this function doesn't matter. */
948 /* We don't worry about overrides in JNI mode. */
951 /* We can't generate a method whose name is a C++ reserved word.
952 We can't just ignore the function, because that will cause
953 incorrect code to be generated if the function is virtual
954 (not only for calls to this function for for other functions
955 after it in the vtbl). So we give it a dummy name instead. */
956 override
= cxx_keyword_subst (str
, length
);
959 if (! stubs
&& ! flag_jni
)
963 generate_access (stream
, flags
);
966 if ((flags
& ACC_STATIC
))
967 fputs ("static ", out
);
968 else if (! METHOD_IS_PRIVATE (jcf
->access_flags
, flags
))
970 /* Don't print `virtual' if we have a constructor. */
972 fputs ("virtual ", out
);
974 print_c_decl (out
, jcf
, name_index
, sig_index
, is_init
, override
, flags
);
976 if ((flags
& ACC_ABSTRACT
))
983 if (METHOD_IS_NATIVE (flags
))
986 print_stub_or_jni (out
, jcf
, name_index
, sig_index
,
987 is_init
, override
, flags
);
995 /* A helper for the decompiler which prints a `return' statement where
996 the type is a reference type. If METHODTYPE and OBJECTTYPE are not
997 identical, we emit a cast. We do this because the C++ compiler
998 doesn't know that a reference can be cast to the type of an
999 interface it implements. METHODTYPE is the index of the method's
1000 signature. NAMEINDEX is the index of the field name; -1 for
1001 `this'. OBJECTTYPE is the index of the object's type. */
1003 decompile_return_statement (FILE *out
, JCF
*jcf
, int methodtype
,
1004 int nameindex
, int objecttype
)
1007 int obj_name_len
, method_name_len
;
1008 const unsigned char *obj_data
, *method_data
;
1010 obj_name_len
= JPOOL_UTF_LENGTH (jcf
, objecttype
);
1011 obj_data
= JPOOL_UTF_DATA (jcf
, objecttype
);
1013 method_name_len
= JPOOL_UTF_LENGTH (jcf
, methodtype
);
1014 method_data
= JPOOL_UTF_DATA (jcf
, methodtype
);
1016 /* Skip forward to return type part of method. */
1017 while (*method_data
!= ')')
1022 /* Skip past `)'. */
1026 /* If we see an `L', skip it and the trailing `;'. */
1027 if (method_data
[0] == 'L' && method_data
[method_name_len
- 1] == ';')
1030 method_name_len
-= 2;
1032 if (obj_data
[0] == 'L' && obj_data
[obj_name_len
- 1] == ';')
1038 /* FIXME: if METHODTYPE is a superclass of OBJECTTYPE then we don't
1039 need a cast. Right now there is no way to determine if this is
1041 if (method_name_len
!= obj_name_len
)
1046 for (i
= 0; i
< method_name_len
; ++i
)
1048 if (method_data
[i
] != obj_data
[i
])
1056 fputs (" { return ", out
);
1060 int array_depth
= 0;
1061 const unsigned char *limit
;
1063 fputs ("reinterpret_cast<", out
);
1065 while (*method_data
== '[')
1070 fputs ("JArray<", out
);
1073 /* Leading space to avoid C++ digraphs. */
1076 /* If we see an `L', skip it and the trailing `;'. Only do this
1077 if we've seen an array specification. If we don't have an
1078 array then the `L' was stripped earlier. */
1079 if (array_depth
&& method_data
[0] == 'L'
1080 && method_data
[method_name_len
- 1] == ';')
1083 method_name_len
-= 2;
1086 limit
= method_data
+ method_name_len
;
1087 while (method_data
< limit
)
1089 int ch
= UTF8_GET (method_data
, limit
);
1093 jcf_print_char (out
, ch
);
1097 /* Close each array. */
1098 while (array_depth
> 0)
1104 /* Close the cast. */
1108 if (nameindex
== -1)
1109 fputs ("this", out
);
1111 print_field_name (out
, jcf
, nameindex
, 0);
1120 /* Try to decompile a method body. Right now we just try to handle a
1121 simple case that we can do. Expand as desired. */
1123 decompile_method (FILE *out
, JCF
*jcf
, int code_len
)
1125 const unsigned char *codes
= jcf
->read_ptr
;
1127 uint16 name_and_type
, name
;
1129 /* If the method is synchronized, don't touch it. */
1130 if ((method_access
& ACC_SYNCHRONIZED
))
1134 && codes
[0] == OPCODE_aload_0
1135 && codes
[1] == OPCODE_getfield
1136 && (codes
[4] == OPCODE_areturn
1137 || codes
[4] == OPCODE_dreturn
1138 || codes
[4] == OPCODE_freturn
1139 || codes
[4] == OPCODE_ireturn
1140 || codes
[4] == OPCODE_lreturn
))
1142 /* Found code like `return FIELD'. */
1143 index
= (codes
[2] << 8) | codes
[3];
1144 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
1145 name_and_type
= JPOOL_USHORT2 (jcf
, index
);
1146 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
1147 name
= JPOOL_USHORT1 (jcf
, name_and_type
);
1148 if (codes
[4] == OPCODE_areturn
)
1149 decompile_return_statement (out
, jcf
, method_signature
,
1150 name
, JPOOL_USHORT2 (jcf
, name_and_type
));
1153 fputs (" { return ", out
);
1155 print_field_name (out
, jcf
, name
, 0);
1160 else if (code_len
== 2
1161 && codes
[0] == OPCODE_aload_0
1162 && codes
[1] == OPCODE_areturn
1163 /* We're going to generate `return this'. This only makes
1164 sense for non-static methods. */
1165 && ! (method_access
& ACC_STATIC
))
1167 decompile_return_statement (out
, jcf
, method_signature
, -1,
1168 JPOOL_USHORT1 (jcf
, jcf
->this_class
));
1171 else if (code_len
== 1 && codes
[0] == OPCODE_return
)
1173 /* Found plain `return'. */
1174 fputs (" { }", out
);
1177 else if (code_len
== 2
1178 && codes
[0] == OPCODE_aconst_null
1179 && codes
[1] == OPCODE_areturn
)
1181 /* Found `return null'. We don't want to depend on NULL being
1183 fputs (" { return 0; }", out
);
1188 /* Like strcmp, but invert the return result for the hash table. This
1189 should probably be in hashtab.c to complement the existing string
1192 gcjh_streq (const void *p1
, const void *p2
)
1194 return ! strcmp ((char *) p1
, (char *) p2
);
1197 /* Return 1 if the initial part of CLNAME names a subclass of throwable,
1198 or 0 if not. CLNAME may be extracted from a signature, and can be
1199 terminated with either `;' or NULL. */
1201 throwable_p (const unsigned char *clname
)
1204 unsigned char *current
;
1208 /* We keep two hash tables of class names. In one we list all the
1209 classes which are subclasses of Throwable. In the other we will
1210 all other classes. We keep two tables to make the code a bit
1211 simpler; we don't have to have a structure mapping class name to
1212 a `throwable?' bit. */
1213 static htab_t throw_hash
;
1214 static htab_t non_throw_hash
;
1215 static int init_done
= 0;
1222 /* Self-initializing. The cost of this really doesn't matter.
1223 We also don't care about freeing these, either. */
1224 throw_hash
= htab_create (10, htab_hash_string
, gcjh_streq
,
1226 non_throw_hash
= htab_create (10, htab_hash_string
, gcjh_streq
,
1229 /* Make sure the root classes show up in the tables. */
1230 str
= (unsigned char *) xstrdup ("java.lang.Throwable");
1231 slot
= htab_find_slot (throw_hash
, str
, INSERT
);
1234 str
= (unsigned char *) xstrdup ("java.lang.Object");
1235 slot
= htab_find_slot (non_throw_hash
, str
, INSERT
);
1241 for (length
= 0; clname
[length
] != ';' && clname
[length
] != '\0'; ++length
)
1243 current
= XNEWVEC (unsigned char, length
+ 1);
1244 for (i
= 0; i
< length
; ++i
)
1245 current
[i
] = clname
[i
] == '/' ? '.' : clname
[i
];
1246 current
[length
] = '\0';
1248 /* We don't compute the hash slot here because the table might be
1249 modified by the recursion. In that case the slot could be
1251 if (htab_find (throw_hash
, current
))
1253 else if (htab_find (non_throw_hash
, current
))
1259 unsigned char *super
, *tmp
;
1260 int super_length
= -1;
1261 const char *classfile_name
= find_class ((char *) current
, strlen ((const char *) current
),
1264 if (! classfile_name
)
1266 error ("couldn't find class %s", current
);
1269 if (jcf_parse_preamble (&jcf
) != 0
1270 || jcf_parse_constant_pool (&jcf
) != 0
1271 || verify_constant_pool (&jcf
) > 0)
1273 error ("parse error while reading %s", classfile_name
);
1276 jcf_parse_class (&jcf
);
1278 tmp
= (unsigned char *) super_class_name (&jcf
, &super_length
);
1279 super
= XNEWVEC (unsigned char, super_length
+ 1);
1280 memcpy (super
, tmp
, super_length
);
1281 super
[super_length
] = '\0';
1283 result
= throwable_p (super
);
1284 slot
= htab_find_slot (result
? throw_hash
: non_throw_hash
,
1295 /* Print one piece of a signature. Returns pointer to next parseable
1296 character on success, NULL on error. */
1297 static const unsigned char *
1298 decode_signature_piece (FILE *stream
, const unsigned char *signature
,
1299 const unsigned char *limit
, int *need_space
)
1302 int array_depth
= 0;
1304 switch (signature
[0])
1307 /* More spaghetti. */
1310 for (signature
++; (signature
< limit
1311 && ISDIGIT (*signature
)); signature
++)
1316 ctype
= "jbyteArray";
1319 ctype
= "jcharArray";
1322 ctype
= "jdoubleArray";
1325 ctype
= "jfloatArray";
1328 ctype
= "jintArray";
1331 ctype
= "jshortArray";
1334 ctype
= "jlongArray";
1337 ctype
= "jbooleanArray";
1340 /* We have a nested array. */
1343 fputs ("JArray<", stream
);
1347 /* We have to generate a reference to JArray here, so that
1348 our output matches what the compiler does. */
1350 /* Space between `<' and `:' to avoid C++ digraphs. */
1352 fputs ("JArray< ::", stream
);
1353 while (signature
< limit
&& *signature
!= ';')
1355 int ch
= UTF8_GET (signature
, limit
);
1359 fputs ("::", stream
);
1361 jcf_print_char (stream
, ch
);
1365 fputs (" *> *", stream
);
1370 /* Unparseable signature. */
1374 /* If the previous iterations left us with something to print,
1375 print it. For JNI, we always print `jobjectArray' in the
1377 if (flag_jni
&& (ctype
== NULL
|| array_depth
> 0))
1379 ctype
= "jobjectArray";
1382 /* The `printit' case will advance SIGNATURE for us. If we
1383 don't go there, we must advance past the `;' ourselves. */
1391 /* This shouldn't happen. */
1394 case 'B': ctype
= "jbyte"; goto printit
;
1395 case 'C': ctype
= "jchar"; goto printit
;
1396 case 'D': ctype
= "jdouble"; goto printit
;
1397 case 'F': ctype
= "jfloat"; goto printit
;
1398 case 'I': ctype
= "jint"; goto printit
;
1399 case 'J': ctype
= "jlong"; goto printit
;
1400 case 'S': ctype
= "jshort"; goto printit
;
1401 case 'Z': ctype
= "jboolean"; goto printit
;
1402 case 'V': ctype
= "void"; goto printit
;
1406 /* We know about certain types and special-case their names. */
1407 if (! strncmp ((const char *) signature
, "Ljava/lang/String;",
1408 sizeof ("Ljava/lang/String;") -1))
1410 else if (! strncmp ((const char *) signature
, "Ljava/lang/Class;",
1411 sizeof ("Ljava/lang/Class;") - 1))
1413 /* Skip leading 'L' for throwable_p call. */
1414 else if (throwable_p (signature
+ 1))
1415 ctype
= "jthrowable";
1419 while (*signature
&& *signature
!= ';')
1424 /* Print a leading "::" so we look in the right namespace. */
1425 fputs ("::", stream
);
1427 while (*signature
&& *signature
!= ';')
1429 int ch
= UTF8_GET (signature
, limit
);
1431 fputs ("::", stream
);
1433 jcf_print_char (stream
, ch
);
1435 fputs (" *", stream
);
1436 if (*signature
== ';')
1442 jni_print_char (stream
, *signature
++);
1447 fputs (ctype
, stream
);
1453 while (array_depth
-- > 0)
1454 fputs ("> *", stream
);
1461 print_c_decl (FILE* stream
, JCF
* jcf
, int name_index
, int signature_index
,
1462 int is_init
, const char *name_override
, int flags
)
1464 if (JPOOL_TAG (jcf
, signature_index
) != CONSTANT_Utf8
)
1466 fprintf (stream
, "<not a UTF8 constant>");
1471 int length
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1472 const unsigned char *str0
= JPOOL_UTF_DATA (jcf
, signature_index
);
1473 const unsigned char *str
= str0
;
1474 const unsigned char *limit
= str
+ length
;
1476 int is_method
= str
[0] == '(';
1477 const unsigned char *next
;
1479 /* If printing a method, skip to the return signature and print
1480 that first. However, there is no return value if this is a
1482 if (is_method
&& ! is_init
)
1492 /* If printing a field or an ordinary method, then print the
1493 "return value" now. */
1494 if (! is_method
|| ! is_init
)
1496 next
= decode_signature_piece (stream
, str
, limit
, &need_space
);
1499 error ("unparseable signature: '%s'", str0
);
1504 /* Force the alignment of the first data member. This is
1505 because the "new" C++ ABI changed the alignment of non-POD
1506 classes. gcj, however, still uses the "old" alignment. */
1507 if (is_first_data_member
&& ! (flags
& ACC_STATIC
) && ! is_method
)
1509 is_first_data_member
= 0;
1510 print_cxx_classname (out
, " __attribute__((aligned(__alignof__( ",
1511 jcf
, jcf
->super_class
, 1);
1512 fputs (" )))) ", stream
);
1515 /* Now print the name of the thing. */
1517 fputs (" ", stream
);
1518 print_full_cxx_name (stream
, jcf
, name_index
,
1519 signature_index
, is_init
, name_override
,
1524 /* Print the unqualified method name followed by the signature. */
1526 print_full_cxx_name (FILE* stream
, JCF
* jcf
, int name_index
,
1527 int signature_index
, int is_init
,
1528 const char *name_override
, int flags
)
1530 int length
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1531 const unsigned char *str0
= JPOOL_UTF_DATA (jcf
, signature_index
);
1532 const unsigned char *str
= str0
;
1533 const unsigned char *limit
= str
+ length
;
1535 int is_method
= str
[0] == '(';
1536 const unsigned char *next
;
1539 fputs (name_override
, stream
);
1540 else if (name_index
)
1542 /* Declare constructors specially. */
1544 print_base_classname (stream
, jcf
, jcf
->this_class
);
1546 print_name (stream
, jcf
, name_index
);
1551 unsigned char *signature
= JPOOL_UTF_DATA (jcf
, signature_index
);
1552 int sig_len
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1553 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf
, name_index
),
1554 JPOOL_UTF_LENGTH (jcf
, name_index
),
1555 (const char *) signature
, sig_len
))
1557 /* If this method is overloaded by another native method,
1558 then include the argument information in the mangled
1560 unsigned char *limit
= signature
+ sig_len
;
1561 fputs ("__", stream
);
1562 while (signature
< limit
)
1564 int ch
= UTF8_GET (signature
, limit
);
1565 jni_print_char (stream
, ch
);
1577 /* Have a method or a constructor. Print signature pieces
1579 fputs (" (", stream
);
1583 /* In JNI mode, add extra arguments. */
1586 /* FIXME: it would be nice to know if we are printing a decl
1587 or a definition, and only print `env' for the latter. */
1588 fputs ("JNIEnv *env", stream
);
1590 fputs ((flags
& ACC_STATIC
) ? ", jclass" : ", jobject", stream
);
1593 fputs (", ", stream
);
1596 while (str
< limit
&& *str
!= ')')
1598 next
= decode_signature_piece (stream
, str
, limit
, &need_space
);
1601 error ("unparseable signature: '%s'", str0
);
1605 if (next
< limit
&& *next
!= ')')
1606 fputs (", ", stream
);
1610 fputs (")", stream
);
1614 /* This is a helper for print_stub_or_jni. */
1616 print_name_for_stub_or_jni (FILE *stream
, JCF
*jcf
, int name_index
,
1617 int signature_index
, int is_init
,
1618 const char *name_override
, int flags
)
1620 const char *const prefix
= flag_jni
? "Java_" : "";
1621 print_cxx_classname (stream
, prefix
, jcf
, jcf
->this_class
, 1);
1622 fputs (flag_jni
? "_" : "::", stream
);
1623 print_full_cxx_name (stream
, jcf
, name_index
,
1624 signature_index
, is_init
, name_override
,
1629 print_stub_or_jni (FILE* stream
, JCF
* jcf
, int name_index
,
1630 int signature_index
, int is_init
,
1631 const char *name_override
, int flags
)
1633 if (JPOOL_TAG (jcf
, signature_index
) != CONSTANT_Utf8
)
1635 fprintf (stream
, "<not a UTF8 constant>");
1640 int length
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1641 const unsigned char *str0
= JPOOL_UTF_DATA (jcf
, signature_index
);
1642 const unsigned char *str
= str0
;
1643 const unsigned char *limit
= str
+ length
;
1645 int is_method
= str
[0] == '(';
1646 const unsigned char *next
;
1648 /* Don't print fields in the JNI case. */
1649 if (! is_method
&& flag_jni
)
1652 if (flag_jni
&& ! stubs
)
1653 fputs ("JNIEXPORT ", stream
);
1655 /* If printing a method, skip to the return signature and print
1656 that first. However, there is no return value if this is a
1658 if (is_method
&& ! is_init
)
1668 /* If printing a field or an ordinary method, then print the
1669 "return value" now. Note that a constructor can't be native,
1670 so we don't bother checking this in the JNI case. */
1671 if (! is_method
|| ! is_init
)
1673 next
= decode_signature_piece (stream
, str
, limit
, &need_space
);
1676 error ("unparseable signature: '%s'", str0
);
1681 /* When printing a JNI header we need to respect the space. In
1682 other cases we're just going to insert a newline anyway. */
1683 fputs (need_space
&& ! stubs
? " " : "\n", stream
);
1685 if (flag_jni
&& ! stubs
)
1686 fputs ("JNICALL ", stream
);
1688 /* Now print the name of the thing. */
1689 print_name_for_stub_or_jni (stream
, jcf
, name_index
,
1690 signature_index
, is_init
, name_override
,
1693 /* Print the body. */
1697 fputs ("\n{\n (*env)->FatalError (env, \"", stream
);
1699 fputs ("\n{\n throw new ::java::lang::UnsupportedOperationException (JvNewStringLatin1 (\"", stream
);
1700 print_name_for_stub_or_jni (stream
, jcf
, name_index
,
1701 signature_index
, is_init
,
1704 fprintf (stream
, " not implemented\")%s;\n}\n\n",
1705 flag_jni
? "" : ")");
1711 print_mangled_classname (FILE *stream
, JCF
*jcf
, const char *prefix
, int index
)
1713 int name_index
= JPOOL_USHORT1 (jcf
, index
);
1714 fputs (prefix
, stream
);
1715 jcf_print_utf8_replace (out
,
1716 JPOOL_UTF_DATA (jcf
, name_index
),
1717 JPOOL_UTF_LENGTH (jcf
, name_index
),
1721 /* Print PREFIX, then a class name in C++ format. If the name refers
1722 to an array, ignore it and don't print PREFIX. Returns 1 if
1723 something was printed, 0 otherwise. */
1725 print_cxx_classname (FILE *stream
, const char *prefix
,
1726 JCF
*jcf
, int index
, int add_scope
)
1728 int name_index
= JPOOL_USHORT1 (jcf
, index
);
1730 const unsigned char *s
, *p
, *limit
;
1732 s
= JPOOL_UTF_DATA (jcf
, name_index
);
1733 len
= JPOOL_UTF_LENGTH (jcf
, name_index
);
1736 /* Explicitly omit arrays here. */
1738 c
= UTF8_GET (p
, limit
);
1742 fputs (prefix
, stream
);
1744 /* Print a leading "::" so we look in the right namespace. */
1745 if (! flag_jni
&& ! stubs
&& add_scope
)
1746 fputs ("::", stream
);
1750 c
= UTF8_GET (s
, limit
);
1752 fputs (flag_jni
? "_" : "::", stream
);
1754 jni_print_char (stream
, c
);
1760 int written_class_count
= 0;
1762 /* Return name of superclass. If LEN is not NULL, fill it with length
1764 static const unsigned char *
1765 super_class_name (JCF
*derived_jcf
, int *len
)
1767 int supername_index
= JPOOL_USHORT1 (derived_jcf
, derived_jcf
->super_class
);
1768 int supername_length
= JPOOL_UTF_LENGTH (derived_jcf
, supername_index
);
1769 const unsigned char *supername
=
1770 JPOOL_UTF_DATA (derived_jcf
, supername_index
);
1773 *len
= supername_length
;
1779 handle_inner_classes (int count
)
1783 if (out
&& ! flag_jni
&& ! stubs
&& count
> 0)
1784 fprintf (out
, "\n");
1786 for (i
= 0; i
< count
; ++i
)
1788 JCF_u2 inner_info_index
= JCF_readu2 (current_jcf
);
1790 /* There are a few more values here, but we don't care about
1791 them. The (void) cast is apparently the only way to avoid a
1793 (void) JCF_readu2 (current_jcf
);
1794 (void) JCF_readu2 (current_jcf
);
1795 (void) JCF_readu2 (current_jcf
);
1797 if (out
&& ! flag_jni
&& ! stubs
)
1799 print_mangled_classname (out
, current_jcf
, " friend class ",
1801 fprintf (out
, ";\n");
1808 /* We keep track of all the `#include's we generate, so we can avoid
1813 struct include
*next
;
1816 /* List of all includes. */
1817 static struct include
*all_includes
= NULL
;
1819 /* Generate a #include. */
1821 print_include (FILE *out
, const unsigned char *utf8
, int len
)
1823 struct include
*incl
;
1829 len
= strlen ((const char *) utf8
);
1831 for (incl
= all_includes
; incl
; incl
= incl
->next
)
1833 /* We check the length because we might have a proper prefix. */
1834 if (len
== (int) strlen (incl
->name
)
1835 && ! strncmp (incl
->name
, (const char *) utf8
, len
))
1839 incl
= XNEW (struct include
);
1840 incl
->name
= XNEWVEC (char, len
+ 1);
1841 strncpy (incl
->name
, (const char *) utf8
, len
);
1842 incl
->name
[len
] = '\0';
1843 incl
->next
= all_includes
;
1844 all_includes
= incl
;
1846 fputs ("#include <", out
);
1847 jcf_print_utf8_replace (out
, utf8
, len
,
1849 flag_jni
? '_' : '/');
1850 fputs (".h>\n", out
);
1855 /* This is used to represent part of a package or class name. */
1858 /* The text of this part of the name. */
1860 /* True if this represents a class. */
1862 /* Linked list of all classes and packages inside this one. */
1863 struct namelet
*subnamelets
;
1864 /* Pointer to next sibling. */
1865 struct namelet
*next
;
1868 static void add_namelet (const unsigned char *, const unsigned char *,
1870 static void print_namelet (FILE *, struct namelet
*, int);
1872 /* The special root namelet. */
1873 static struct namelet root
=
1881 /* This extracts the next name segment from the full UTF-8 encoded
1882 package or class name and links it into the tree. It does this
1885 add_namelet (const unsigned char *name
, const unsigned char *name_limit
,
1886 struct namelet
*parent
)
1888 const unsigned char *p
;
1889 struct namelet
*n
= NULL
, *np
;
1891 /* We want to skip the standard namespaces that we assume the
1892 runtime already knows about. We only do this at the top level,
1893 though, hence the check for `root'. */
1894 if (parent
== &root
)
1896 #define JAVALANG "java/lang/"
1897 #define JAVAIO "java/io/"
1898 #define JAVAUTIL "java/util/"
1899 if ((name_limit
- name
>= (int) sizeof (JAVALANG
) - 1
1900 && ! strncmp ((const char *) name
, JAVALANG
, sizeof (JAVALANG
) - 1))
1901 || (name_limit
- name
>= (int) sizeof (JAVAUTIL
) - 1
1902 && ! strncmp ((const char *) name
, JAVAUTIL
, sizeof (JAVAUTIL
) - 1))
1903 || (name_limit
- name
>= (int) sizeof (JAVAIO
) - 1
1904 && ! strncmp ((const char *) name
, JAVAIO
, sizeof (JAVAIO
) - 1)))
1908 for (p
= name
; p
< name_limit
&& *p
!= '/'; ++p
)
1911 /* Search for this name beneath the PARENT node. */
1912 for (np
= parent
->subnamelets
; np
!= NULL
; np
= np
->next
)
1914 /* We check the length because we might have a proper prefix. */
1915 if ((int) strlen (np
->name
) == p
- name
&&
1916 ! strncmp ((const char *) name
, np
->name
, p
- name
))
1925 n
= XNEW (struct namelet
);
1926 n
->name
= XNEWVEC (char, p
- name
+ 1);
1927 strncpy (n
->name
, (const char *) name
, p
- name
);
1928 n
->name
[p
- name
] = '\0';
1929 n
->is_class
= (p
== name_limit
);
1930 n
->subnamelets
= NULL
;
1931 n
->next
= parent
->subnamelets
;
1932 parent
->subnamelets
= n
;
1935 /* We recurse if there is more text, and if the trailing piece does
1936 not represent an inner class. */
1938 add_namelet (p
+ 1, name_limit
, n
);
1941 /* Print a single namelet. Destroys namelets while printing. */
1943 print_namelet (FILE *out
, struct namelet
*name
, int depth
)
1950 for (i
= 0; i
< depth
; ++i
)
1952 fprintf (out
, "%s %s", name
->is_class
? "class" : "namespace",
1954 if (name
->is_class
&& name
->subnamelets
== NULL
)
1960 for (i
= 0; i
< depth
; ++i
)
1966 c
= name
->subnamelets
;
1969 struct namelet
*next
= c
->next
;
1970 print_namelet (out
, c
, depth
+ 2);
1973 name
->subnamelets
= NULL
;
1979 for (i
= 0; i
< depth
; ++i
)
1982 /* Only print a `;' when printing a class. C++ is evil. */
1992 /* This is called to add some classes to the list of classes for which
1993 we need decls. The signature argument can be a function
1996 add_class_decl (FILE *out
, JCF
*jcf
, JCF_u2 signature
)
1998 const unsigned char *s
= JPOOL_UTF_DATA (jcf
, signature
);
1999 int len
= JPOOL_UTF_LENGTH (jcf
, signature
);
2002 for (i
= 0; i
< len
; ++i
)
2006 /* If we see an array, then we include the array header. */
2009 print_include (out
, (const unsigned char *) "gcj/array", -1);
2013 /* We're looking for `L<stuff>;' -- everything else is
2018 for (start
= ++i
; i
< len
&& s
[i
] != ';'; ++i
)
2021 add_namelet (&s
[start
], &s
[i
], &root
);
2025 /* Print declarations for all classes required by this class. Any
2026 class or package in the `java' package is assumed to be handled
2027 statically in libjava; we don't generate declarations for these.
2028 This makes the generated headers a bit easier to read. */
2030 print_class_decls (FILE *out
, JCF
*jcf
, int self
)
2032 /* Make sure to always add the current class to the list of things
2033 that should be declared. */
2034 int name_index
= JPOOL_USHORT1 (jcf
, self
);
2036 const unsigned char *s
;
2038 s
= JPOOL_UTF_DATA (jcf
, name_index
);
2039 len
= JPOOL_UTF_LENGTH (jcf
, name_index
);
2040 add_namelet (s
, s
+ len
, &root
);
2042 if (root
.subnamelets
)
2044 fputs ("extern \"Java\"\n{\n", out
);
2045 /* We use an initial offset of 0 because the root namelet
2046 doesn't cause anything to print. */
2047 print_namelet (out
, &root
, 0);
2048 fputs ("}\n\n", out
);
2055 process_file (JCF
*jcf
, FILE *out
)
2058 uint32 field_start
, method_end
, method_start
;
2064 if (jcf_parse_preamble (jcf
) != 0)
2066 error ("Not a valid Java .class file.");
2070 /* Parse and possibly print constant pool */
2071 code
= jcf_parse_constant_pool (jcf
);
2074 error ("error while parsing constant pool");
2077 code
= verify_constant_pool (jcf
);
2080 error ("error in constant pool entry #%d", code
);
2084 jcf_parse_class (jcf
);
2086 if (written_class_count
++ == 0 && out
)
2088 const char *cstart
, *cstart2
, *mode
, *cend
, *what
, *jflag
;
2103 mode
= " -*- c++ -*-";
2109 fprintf (out
, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
2110 cstart
, mode
, cend
);
2113 fprintf (out
, "%s This file was created by `" TOOLNAME
" -stubs%s'.%s\n\
2115 %s This file is intended to give you a head start on implementing native\n\
2116 %s methods using %s.\n\
2117 %s Be aware: running `" TOOLNAME
" -stubs %s' once more for this class may\n\
2118 %s overwrite any edits you have made to this file.%s\n\n",
2119 cstart
, jflag
, mode
,
2135 print_mangled_classname (out
, jcf
, "#ifndef __", jcf
->this_class
);
2136 fprintf (out
, "__\n");
2138 print_mangled_classname (out
, jcf
, "#define __", jcf
->this_class
);
2139 fprintf (out
, "__\n\n");
2143 fprintf (out
, "#include <jni.h>\n\n");
2144 fprintf (out
, "#ifdef __cplusplus\n");
2145 fprintf (out
, "extern \"C\"\n");
2146 fprintf (out
, "{\n");
2147 fprintf (out
, "#endif\n");
2151 /* We do this to ensure that inline methods won't be
2152 `outlined' by g++. This works as long as method and
2153 fields are not added by the user. */
2154 fprintf (out
, "#pragma interface\n");
2156 if (jcf
->super_class
)
2159 const unsigned char *supername
=
2160 super_class_name (jcf
, &super_length
);
2163 print_include (out
, supername
, super_length
);
2169 /* Strip off the ".class" portion of the name when printing
2170 the include file name. */
2172 int i
, len
= strlen (jcf
->classname
);
2173 if (len
> 6 && ! strcmp (&jcf
->classname
[len
- 6], ".class"))
2175 /* Turn the class name into a file name. */
2176 name
= XNEWVEC (char, len
+ 1);
2177 for (i
= 0; i
< len
; ++i
)
2178 name
[i
] = jcf
->classname
[i
] == '.' ? '/' : jcf
->classname
[i
];
2180 print_include (out
, (const unsigned char *) name
, len
);
2185 print_include (out
, (const unsigned char *) "gcj/cni", -1);
2186 print_include (out
, (const unsigned char *) "java/lang/UnsupportedOperationException",
2192 /* We want to parse the methods first. But we need to find where
2193 they start. So first we skip the fields, then parse the methods.
2194 Then we parse the fields and skip the methods. This is ugly, but
2195 not too bad since we need two full passes to get class decl
2196 information anyway. */
2198 field_start
= JCF_TELL (jcf
);
2199 jcf_parse_fields (jcf
);
2201 method_start
= JCF_TELL (jcf
);
2203 jcf_parse_methods (jcf
);
2208 if (out
&& ! flag_jni
)
2211 print_class_decls (out
, jcf
, jcf
->this_class
);
2213 for (i
= 0; i
< prepend_count
; ++i
)
2214 fprintf (out
, "%s\n", prepend_specs
[i
]);
2215 if (prepend_count
> 0)
2220 if (! print_cxx_classname (out
, "class ", jcf
,
2221 jcf
->this_class
, 0))
2223 error ("class is of array type\n");
2226 if (jcf
->super_class
)
2228 if (! print_cxx_classname (out
, " : public ",
2229 jcf
, jcf
->super_class
, 1))
2231 error ("base class is of array type");
2236 fputs ("\n{\n", out
);
2240 /* Now go back for second pass over methods and fields. */
2241 is_first_data_member
= 1;
2243 JCF_SEEK (jcf
, method_start
);
2245 jcf_parse_methods (jcf
);
2246 method_end
= JCF_TELL (jcf
);
2249 JCF_SEEK (jcf
, field_start
);
2250 jcf_parse_fields (jcf
);
2251 JCF_SEEK (jcf
, method_end
);
2253 jcf_parse_final_attributes (jcf
);
2259 fprintf (out
, "\n#ifdef __cplusplus\n");
2260 fprintf (out
, "}\n");
2261 fprintf (out
, "#endif\n");
2265 /* Generate friend decl if we still must. */
2266 for (i
= 0; i
< friend_count
; ++i
)
2267 fprintf (out
, " friend %s\n", friend_specs
[i
]);
2269 /* Generate extra declarations. */
2272 for (i
= 0; i
< add_count
; ++i
)
2273 fprintf (out
, " %s\n", add_specs
[i
]);
2275 /* Generate an entry for the class object. */
2276 generate_access (out
, ACC_PUBLIC
);
2277 fprintf (out
, "\n static ::java::lang::Class class$;\n");
2281 if (jcf
->access_flags
& ACC_INTERFACE
)
2282 fputs (" __attribute__ ((java_interface))", out
);
2286 if (append_count
> 0)
2288 for (i
= 0; i
< append_count
; ++i
)
2289 fprintf (out
, "%s\n", append_specs
[i
]);
2292 print_mangled_classname (out
, jcf
,
2293 "\n#endif /* __", jcf
->this_class
);
2294 fprintf (out
, "__ */\n");
2300 /* This is used to mark options with no short value. */
2301 #define LONG_OPT(Num) ((Num) + 128)
2303 #define OPT_classpath LONG_OPT (0)
2304 #define OPT_CLASSPATH OPT_classpath
2305 #define OPT_bootclasspath LONG_OPT (1)
2306 #define OPT_extdirs LONG_OPT (2)
2307 #define OPT_HELP LONG_OPT (3)
2308 #define OPT_TEMP LONG_OPT (4)
2309 #define OPT_VERSION LONG_OPT (5)
2310 #define OPT_PREPEND LONG_OPT (6)
2311 #define OPT_FRIEND LONG_OPT (7)
2312 #define OPT_ADD LONG_OPT (8)
2313 #define OPT_APPEND LONG_OPT (9)
2314 #define OPT_M LONG_OPT (10)
2315 #define OPT_MM LONG_OPT (11)
2316 #define OPT_MG LONG_OPT (12)
2317 #define OPT_MD LONG_OPT (13)
2318 #define OPT_MMD LONG_OPT (14)
2319 #define OPT_FORCE LONG_OPT (15)
2320 #define OPT_OLD LONG_OPT (16)
2321 #define OPT_TRACE LONG_OPT (17)
2323 static const struct option options
[] =
2325 { "classpath", required_argument
, NULL
, OPT_classpath
},
2326 { "bootclasspath", required_argument
, NULL
, OPT_bootclasspath
},
2327 { "extdirs", required_argument
, NULL
, OPT_extdirs
},
2328 { "CLASSPATH", required_argument
, NULL
, OPT_CLASSPATH
},
2329 { "help", no_argument
, NULL
, OPT_HELP
},
2330 { "stubs", no_argument
, &stubs
, 1 },
2331 { "td", required_argument
, NULL
, OPT_TEMP
},
2332 { "verbose", no_argument
, NULL
, 'v' },
2333 { "version", no_argument
, NULL
, OPT_VERSION
},
2334 { "prepend", required_argument
, NULL
, OPT_PREPEND
},
2335 { "friend", required_argument
, NULL
, OPT_FRIEND
},
2336 { "add", required_argument
, NULL
, OPT_ADD
},
2337 { "append", required_argument
, NULL
, OPT_APPEND
},
2338 { "M", no_argument
, NULL
, OPT_M
},
2339 { "MM", no_argument
, NULL
, OPT_MM
},
2340 { "MG", no_argument
, NULL
, OPT_MG
},
2341 { "MD", no_argument
, NULL
, OPT_MD
},
2342 { "MMD", no_argument
, NULL
, OPT_MMD
},
2343 { "jni", no_argument
, &flag_jni
, 1 },
2344 { "force", no_argument
, NULL
, OPT_FORCE
},
2345 /* If the output file should be named "ld" then a space is needed
2346 between -o and its argument, ld. */
2347 { "old", no_argument
, NULL
, OPT_OLD
},
2348 { "trace", no_argument
, NULL
, OPT_TRACE
},
2349 { NULL
, required_argument
, NULL
, 'J' },
2350 { NULL
, no_argument
, NULL
, 0 }
2356 fprintf (stderr
, _("Try '" TOOLNAME
" --help' for more information.\n"));
2363 printf (_("Usage: " TOOLNAME
" [OPTION]... CLASS...\n\n"));
2364 printf (_("Generate C or C++ header files from .class files\n\n"));
2365 printf (_(" -stubs Generate an implementation stub file\n"));
2366 printf (_(" -jni Generate a JNI header or stub\n"));
2367 printf (_(" -force Always overwrite output files\n"));
2368 printf (_(" -old Unused compatibility option\n"));
2369 printf (_(" -trace Unused compatibility option\n"));
2370 printf (_(" -J OPTION Unused compatibility option\n"));
2372 printf (_(" -add TEXT Insert TEXT into class body\n"));
2373 printf (_(" -append TEXT Insert TEXT after class declaration\n"));
2374 printf (_(" -friend TEXT Insert TEXT as 'friend' declaration\n"));
2375 printf (_(" -prepend TEXT Insert TEXT before start of class\n"));
2377 printf (_(" --classpath PATH Set path to find .class files\n"));
2378 printf (_(" -IDIR Append directory to class path\n"));
2379 printf (_(" --bootclasspath PATH Override built-in class path\n"));
2380 printf (_(" --extdirs PATH Set extensions directory path\n"));
2381 printf (_(" -d DIRECTORY Set output directory name\n"));
2382 printf (_(" -o FILE Set output file name\n"));
2383 printf (_(" -td DIRECTORY Set temporary directory name\n"));
2385 printf (_(" --help Print this help, then exit\n"));
2386 printf (_(" --version Print version number, then exit\n"));
2387 printf (_(" -v, --verbose Print extra information while running\n"));
2389 printf (_(" -M Print all dependencies to stdout;\n"
2390 " suppress ordinary output\n"));
2391 printf (_(" -MM Print non-system dependencies to stdout;\n"
2392 " suppress ordinary output\n"));
2393 printf (_(" -MD Print all dependencies to stdout\n"));
2394 printf (_(" -MMD Print non-system dependencies to stdout\n"));
2395 /* We omit -MG until it is implemented. */
2397 printf (_("For bug reporting instructions, please see:\n"
2398 "%s.\n"), bug_report_url
);
2405 printf (TOOLNAME
" (GCC) %s\n\n", version_string
);
2406 printf ("Copyright %s 2006 Free Software Foundation, Inc.\n", _("(C)"));
2407 printf (_("This is free software; see the source for copying conditions. There is NO\n"
2408 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
2413 main (int argc
, char** argv
)
2417 char *output_file
= NULL
;
2418 int emit_dependencies
= 0, suppress_output
= 0;
2420 int local_found_error
;
2422 /* Unlock the stdio streams. */
2423 unlock_std_streams ();
2425 gcc_init_libintl ();
2429 error ("no classes specified");
2435 /* We use getopt_long_only to allow single `-' long options. For
2436 some of our options this is more natural. */
2437 while ((opt
= getopt_long_only (argc
, argv
, "J:I:d:o:v", options
, NULL
)) != -1)
2442 /* Already handled. */
2446 output_file
= optarg
;
2450 output_directory
= optarg
;
2454 jcf_path_include_arg (optarg
);
2462 jcf_path_classpath_arg (optarg
);
2465 case OPT_bootclasspath
:
2466 jcf_path_bootclasspath_arg (optarg
);
2470 jcf_path_extdirs_arg (optarg
);
2478 temp_directory
= optarg
;
2486 if (prepend_count
== 0)
2487 prepend_specs
= XNEWVEC (char *, argc
);
2488 prepend_specs
[prepend_count
++] = optarg
;
2492 if (friend_count
== 0)
2493 friend_specs
= XNEWVEC (char *, argc
);
2494 friend_specs
[friend_count
++] = optarg
;
2499 add_specs
= XNEWVEC (char *, argc
);
2500 add_specs
[add_count
++] = optarg
;
2504 if (append_count
== 0)
2505 append_specs
= XNEWVEC (char *, argc
);
2506 append_specs
[append_count
++] = optarg
;
2510 emit_dependencies
= 1;
2511 suppress_output
= 1;
2512 jcf_dependency_init (1);
2516 emit_dependencies
= 1;
2517 suppress_output
= 1;
2518 jcf_dependency_init (0);
2522 error ("'-MG' option is unimplemented");
2526 emit_dependencies
= 1;
2527 jcf_dependency_init (1);
2531 emit_dependencies
= 1;
2532 jcf_dependency_init (0);
2545 /* Ignore -J options. */
2556 error ("no classes specified");
2560 jcf_path_seal (verbose
);
2562 if (output_file
&& emit_dependencies
)
2564 error ("can't specify both -o and -MD");
2568 local_found_error
= 0;
2569 for (argi
= optind
; argi
< argc
; argi
++)
2571 char *classname
= argv
[argi
];
2572 char *current_output_file
= NULL
;
2573 const char *classfile_name
;
2575 /* We reset the error state here so that we can detect errors
2576 that occur when processing this file, so the output can be
2577 unlinked if need be. */
2581 printf (_("Processing %s\n"), classname
);
2583 jcf_dependency_reset ();
2584 classfile_name
= find_class (classname
, strlen (classname
), &jcf
, 0);
2585 if (classfile_name
== NULL
)
2587 error ("%s: no such class", classname
);
2591 printf (_("Found in %s\n"), classfile_name
);
2594 if (strcmp (output_file
, "-") == 0)
2596 else if (out
== NULL
)
2598 out
= fopen (output_file
, "w");
2602 perror (output_file
);
2605 current_output_file
= output_file
;
2609 int dir_len
= strlen (output_directory
);
2610 int i
, classname_length
= strlen (classname
);
2611 current_output_file
= XNEWVEC (char, dir_len
+ classname_length
+ 5);
2612 strcpy (current_output_file
, output_directory
);
2613 if (dir_len
> 0 && output_directory
[dir_len
-1] != '/')
2614 current_output_file
[dir_len
++] = '/';
2615 for (i
= 0; classname
[i
] != '\0'; i
++)
2617 char ch
= classname
[i
];
2620 if (flag_jni
&& ch
== '/')
2622 current_output_file
[dir_len
++] = ch
;
2624 if (emit_dependencies
)
2626 if (suppress_output
)
2628 jcf_dependency_set_dep_file ("-");
2633 /* We use `.hd' and not `.d' to avoid clashes with
2634 dependency tracking from straight compilation. */
2635 strcpy (current_output_file
+ dir_len
, ".hd");
2636 jcf_dependency_set_dep_file (current_output_file
);
2639 strcpy (current_output_file
+ dir_len
,
2640 stubs
? (flag_jni
? ".c" : ".cc") : ".h");
2641 jcf_dependency_set_target (current_output_file
);
2642 if (! suppress_output
)
2644 out
= fopen (current_output_file
, "w");
2647 perror (current_output_file
);
2652 free_method_name_list ();
2653 process_file (&jcf
, out
);
2656 /* If we found an error and we're writing to a real file,
2658 if (found_error
&& ! suppress_output
&& current_output_file
!= NULL
2659 && strcmp (current_output_file
, "-"))
2660 unlink (current_output_file
);
2662 if (current_output_file
!= output_file
)
2663 free (current_output_file
);
2664 jcf_dependency_write ();
2666 local_found_error
|= found_error
;
2669 if (out
!= NULL
&& out
!= stdout
)
2672 return local_found_error
;