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
))
1133 /* The first case is 'return field'; but we only want to do this for
1134 non-static methods. */
1135 if (! (method_access
& ACC_STATIC
)
1137 && codes
[0] == OPCODE_aload_0
1138 && codes
[1] == OPCODE_getfield
1139 && (codes
[4] == OPCODE_areturn
1140 || codes
[4] == OPCODE_dreturn
1141 || codes
[4] == OPCODE_freturn
1142 || codes
[4] == OPCODE_ireturn
1143 || codes
[4] == OPCODE_lreturn
))
1145 /* Found code like `return FIELD'. */
1146 index
= (codes
[2] << 8) | codes
[3];
1147 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
1148 name_and_type
= JPOOL_USHORT2 (jcf
, index
);
1149 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
1150 name
= JPOOL_USHORT1 (jcf
, name_and_type
);
1151 if (codes
[4] == OPCODE_areturn
)
1152 decompile_return_statement (out
, jcf
, method_signature
,
1153 name
, JPOOL_USHORT2 (jcf
, name_and_type
));
1156 fputs (" { return ", out
);
1158 print_field_name (out
, jcf
, name
, 0);
1163 else if (code_len
== 2
1164 && codes
[0] == OPCODE_aload_0
1165 && codes
[1] == OPCODE_areturn
1166 /* We're going to generate `return this'. This only makes
1167 sense for non-static methods. */
1168 && ! (method_access
& ACC_STATIC
))
1170 decompile_return_statement (out
, jcf
, method_signature
, -1,
1171 JPOOL_USHORT1 (jcf
, jcf
->this_class
));
1174 else if (code_len
== 1 && codes
[0] == OPCODE_return
)
1176 /* Found plain `return'. */
1177 fputs (" { }", out
);
1180 else if (code_len
== 2
1181 && codes
[0] == OPCODE_aconst_null
1182 && codes
[1] == OPCODE_areturn
)
1184 /* Found `return null'. We don't want to depend on NULL being
1186 fputs (" { return 0; }", out
);
1191 /* Like strcmp, but invert the return result for the hash table. This
1192 should probably be in hashtab.c to complement the existing string
1195 gcjh_streq (const void *p1
, const void *p2
)
1197 return ! strcmp ((char *) p1
, (char *) p2
);
1200 /* Return 1 if the initial part of CLNAME names a subclass of throwable,
1201 or 0 if not. CLNAME may be extracted from a signature, and can be
1202 terminated with either `;' or NULL. */
1204 throwable_p (const unsigned char *clname
)
1207 unsigned char *current
;
1211 /* We keep two hash tables of class names. In one we list all the
1212 classes which are subclasses of Throwable. In the other we will
1213 all other classes. We keep two tables to make the code a bit
1214 simpler; we don't have to have a structure mapping class name to
1215 a `throwable?' bit. */
1216 static htab_t throw_hash
;
1217 static htab_t non_throw_hash
;
1218 static int init_done
= 0;
1225 /* Self-initializing. The cost of this really doesn't matter.
1226 We also don't care about freeing these, either. */
1227 throw_hash
= htab_create (10, htab_hash_string
, gcjh_streq
,
1229 non_throw_hash
= htab_create (10, htab_hash_string
, gcjh_streq
,
1232 /* Make sure the root classes show up in the tables. */
1233 str
= (unsigned char *) xstrdup ("java.lang.Throwable");
1234 slot
= htab_find_slot (throw_hash
, str
, INSERT
);
1237 str
= (unsigned char *) xstrdup ("java.lang.Object");
1238 slot
= htab_find_slot (non_throw_hash
, str
, INSERT
);
1244 for (length
= 0; clname
[length
] != ';' && clname
[length
] != '\0'; ++length
)
1246 current
= XNEWVEC (unsigned char, length
+ 1);
1247 for (i
= 0; i
< length
; ++i
)
1248 current
[i
] = clname
[i
] == '/' ? '.' : clname
[i
];
1249 current
[length
] = '\0';
1251 /* We don't compute the hash slot here because the table might be
1252 modified by the recursion. In that case the slot could be
1254 if (htab_find (throw_hash
, current
))
1256 else if (htab_find (non_throw_hash
, current
))
1262 unsigned char *super
, *tmp
;
1263 int super_length
= -1;
1264 const char *classfile_name
= find_class ((char *) current
, strlen ((const char *) current
),
1267 if (! classfile_name
)
1269 error ("couldn't find class %s", current
);
1272 if (jcf_parse_preamble (&jcf
) != 0
1273 || jcf_parse_constant_pool (&jcf
) != 0
1274 || verify_constant_pool (&jcf
) > 0)
1276 error ("parse error while reading %s", classfile_name
);
1279 jcf_parse_class (&jcf
);
1281 tmp
= (unsigned char *) super_class_name (&jcf
, &super_length
);
1282 super
= XNEWVEC (unsigned char, super_length
+ 1);
1283 memcpy (super
, tmp
, super_length
);
1284 super
[super_length
] = '\0';
1286 result
= throwable_p (super
);
1287 slot
= htab_find_slot (result
? throw_hash
: non_throw_hash
,
1298 /* Print one piece of a signature. Returns pointer to next parseable
1299 character on success, NULL on error. */
1300 static const unsigned char *
1301 decode_signature_piece (FILE *stream
, const unsigned char *signature
,
1302 const unsigned char *limit
, int *need_space
)
1305 int array_depth
= 0;
1307 switch (signature
[0])
1310 /* More spaghetti. */
1313 for (signature
++; (signature
< limit
1314 && ISDIGIT (*signature
)); signature
++)
1319 ctype
= "jbyteArray";
1322 ctype
= "jcharArray";
1325 ctype
= "jdoubleArray";
1328 ctype
= "jfloatArray";
1331 ctype
= "jintArray";
1334 ctype
= "jshortArray";
1337 ctype
= "jlongArray";
1340 ctype
= "jbooleanArray";
1343 /* We have a nested array. */
1346 fputs ("JArray<", stream
);
1350 /* We have to generate a reference to JArray here, so that
1351 our output matches what the compiler does. */
1353 /* Space between `<' and `:' to avoid C++ digraphs. */
1355 fputs ("JArray< ::", stream
);
1356 while (signature
< limit
&& *signature
!= ';')
1358 int ch
= UTF8_GET (signature
, limit
);
1362 fputs ("::", stream
);
1364 jcf_print_char (stream
, ch
);
1368 fputs (" *> *", stream
);
1373 /* Unparseable signature. */
1377 /* If the previous iterations left us with something to print,
1378 print it. For JNI, we always print `jobjectArray' in the
1380 if (flag_jni
&& (ctype
== NULL
|| array_depth
> 0))
1382 ctype
= "jobjectArray";
1385 /* The `printit' case will advance SIGNATURE for us. If we
1386 don't go there, we must advance past the `;' ourselves. */
1394 /* This shouldn't happen. */
1397 case 'B': ctype
= "jbyte"; goto printit
;
1398 case 'C': ctype
= "jchar"; goto printit
;
1399 case 'D': ctype
= "jdouble"; goto printit
;
1400 case 'F': ctype
= "jfloat"; goto printit
;
1401 case 'I': ctype
= "jint"; goto printit
;
1402 case 'J': ctype
= "jlong"; goto printit
;
1403 case 'S': ctype
= "jshort"; goto printit
;
1404 case 'Z': ctype
= "jboolean"; goto printit
;
1405 case 'V': ctype
= "void"; goto printit
;
1409 /* We know about certain types and special-case their names. */
1410 if (! strncmp ((const char *) signature
, "Ljava/lang/String;",
1411 sizeof ("Ljava/lang/String;") -1))
1413 else if (! strncmp ((const char *) signature
, "Ljava/lang/Class;",
1414 sizeof ("Ljava/lang/Class;") - 1))
1416 /* Skip leading 'L' for throwable_p call. */
1417 else if (throwable_p (signature
+ 1))
1418 ctype
= "jthrowable";
1422 while (*signature
&& *signature
!= ';')
1427 /* Print a leading "::" so we look in the right namespace. */
1428 fputs ("::", stream
);
1430 while (*signature
&& *signature
!= ';')
1432 int ch
= UTF8_GET (signature
, limit
);
1434 fputs ("::", stream
);
1436 jcf_print_char (stream
, ch
);
1438 fputs (" *", stream
);
1439 if (*signature
== ';')
1445 jni_print_char (stream
, *signature
++);
1450 fputs (ctype
, stream
);
1456 while (array_depth
-- > 0)
1457 fputs ("> *", stream
);
1464 print_c_decl (FILE* stream
, JCF
* jcf
, int name_index
, int signature_index
,
1465 int is_init
, const char *name_override
, int flags
)
1467 if (JPOOL_TAG (jcf
, signature_index
) != CONSTANT_Utf8
)
1469 fprintf (stream
, "<not a UTF8 constant>");
1474 int length
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1475 const unsigned char *str0
= JPOOL_UTF_DATA (jcf
, signature_index
);
1476 const unsigned char *str
= str0
;
1477 const unsigned char *limit
= str
+ length
;
1479 int is_method
= str
[0] == '(';
1480 const unsigned char *next
;
1482 /* If printing a method, skip to the return signature and print
1483 that first. However, there is no return value if this is a
1485 if (is_method
&& ! is_init
)
1495 /* If printing a field or an ordinary method, then print the
1496 "return value" now. */
1497 if (! is_method
|| ! is_init
)
1499 next
= decode_signature_piece (stream
, str
, limit
, &need_space
);
1502 error ("unparseable signature: '%s'", str0
);
1507 /* Force the alignment of the first data member. This is
1508 because the "new" C++ ABI changed the alignment of non-POD
1509 classes. gcj, however, still uses the "old" alignment. */
1510 if (is_first_data_member
&& ! (flags
& ACC_STATIC
) && ! is_method
)
1512 is_first_data_member
= 0;
1513 print_cxx_classname (out
, " __attribute__((aligned(__alignof__( ",
1514 jcf
, jcf
->super_class
, 1);
1515 fputs (" )))) ", stream
);
1518 /* Now print the name of the thing. */
1520 fputs (" ", stream
);
1521 print_full_cxx_name (stream
, jcf
, name_index
,
1522 signature_index
, is_init
, name_override
,
1527 /* Print the unqualified method name followed by the signature. */
1529 print_full_cxx_name (FILE* stream
, JCF
* jcf
, int name_index
,
1530 int signature_index
, int is_init
,
1531 const char *name_override
, int flags
)
1533 int length
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1534 const unsigned char *str0
= JPOOL_UTF_DATA (jcf
, signature_index
);
1535 const unsigned char *str
= str0
;
1536 const unsigned char *limit
= str
+ length
;
1538 int is_method
= str
[0] == '(';
1539 const unsigned char *next
;
1542 fputs (name_override
, stream
);
1543 else if (name_index
)
1545 /* Declare constructors specially. */
1547 print_base_classname (stream
, jcf
, jcf
->this_class
);
1549 print_name (stream
, jcf
, name_index
);
1554 unsigned char *signature
= JPOOL_UTF_DATA (jcf
, signature_index
);
1555 int sig_len
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1556 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf
, name_index
),
1557 JPOOL_UTF_LENGTH (jcf
, name_index
),
1558 (const char *) signature
, sig_len
))
1560 /* If this method is overloaded by another native method,
1561 then include the argument information in the mangled
1563 unsigned char *limit
= signature
+ sig_len
;
1564 fputs ("__", stream
);
1565 while (signature
< limit
)
1567 int ch
= UTF8_GET (signature
, limit
);
1568 jni_print_char (stream
, ch
);
1580 /* Have a method or a constructor. Print signature pieces
1582 fputs (" (", stream
);
1586 /* In JNI mode, add extra arguments. */
1589 /* FIXME: it would be nice to know if we are printing a decl
1590 or a definition, and only print `env' for the latter. */
1591 fputs ("JNIEnv *env", stream
);
1593 fputs ((flags
& ACC_STATIC
) ? ", jclass" : ", jobject", stream
);
1596 fputs (", ", stream
);
1599 while (str
< limit
&& *str
!= ')')
1601 next
= decode_signature_piece (stream
, str
, limit
, &need_space
);
1604 error ("unparseable signature: '%s'", str0
);
1608 if (next
< limit
&& *next
!= ')')
1609 fputs (", ", stream
);
1613 fputs (")", stream
);
1617 /* This is a helper for print_stub_or_jni. */
1619 print_name_for_stub_or_jni (FILE *stream
, JCF
*jcf
, int name_index
,
1620 int signature_index
, int is_init
,
1621 const char *name_override
, int flags
)
1623 const char *const prefix
= flag_jni
? "Java_" : "";
1624 print_cxx_classname (stream
, prefix
, jcf
, jcf
->this_class
, 1);
1625 fputs (flag_jni
? "_" : "::", stream
);
1626 print_full_cxx_name (stream
, jcf
, name_index
,
1627 signature_index
, is_init
, name_override
,
1632 print_stub_or_jni (FILE* stream
, JCF
* jcf
, int name_index
,
1633 int signature_index
, int is_init
,
1634 const char *name_override
, int flags
)
1636 if (JPOOL_TAG (jcf
, signature_index
) != CONSTANT_Utf8
)
1638 fprintf (stream
, "<not a UTF8 constant>");
1643 int length
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1644 const unsigned char *str0
= JPOOL_UTF_DATA (jcf
, signature_index
);
1645 const unsigned char *str
= str0
;
1646 const unsigned char *limit
= str
+ length
;
1648 int is_method
= str
[0] == '(';
1649 const unsigned char *next
;
1651 /* Don't print fields in the JNI case. */
1652 if (! is_method
&& flag_jni
)
1655 if (flag_jni
&& ! stubs
)
1656 fputs ("JNIEXPORT ", stream
);
1658 /* If printing a method, skip to the return signature and print
1659 that first. However, there is no return value if this is a
1661 if (is_method
&& ! is_init
)
1671 /* If printing a field or an ordinary method, then print the
1672 "return value" now. Note that a constructor can't be native,
1673 so we don't bother checking this in the JNI case. */
1674 if (! is_method
|| ! is_init
)
1676 next
= decode_signature_piece (stream
, str
, limit
, &need_space
);
1679 error ("unparseable signature: '%s'", str0
);
1684 /* When printing a JNI header we need to respect the space. In
1685 other cases we're just going to insert a newline anyway. */
1686 fputs (need_space
&& ! stubs
? " " : "\n", stream
);
1688 if (flag_jni
&& ! stubs
)
1689 fputs ("JNICALL ", stream
);
1691 /* Now print the name of the thing. */
1692 print_name_for_stub_or_jni (stream
, jcf
, name_index
,
1693 signature_index
, is_init
, name_override
,
1696 /* Print the body. */
1700 fputs ("\n{\n (*env)->FatalError (env, \"", stream
);
1702 fputs ("\n{\n throw new ::java::lang::UnsupportedOperationException (JvNewStringLatin1 (\"", stream
);
1703 print_name_for_stub_or_jni (stream
, jcf
, name_index
,
1704 signature_index
, is_init
,
1707 fprintf (stream
, " not implemented\")%s;\n}\n\n",
1708 flag_jni
? "" : ")");
1714 print_mangled_classname (FILE *stream
, JCF
*jcf
, const char *prefix
, int index
)
1716 int name_index
= JPOOL_USHORT1 (jcf
, index
);
1717 fputs (prefix
, stream
);
1718 jcf_print_utf8_replace (out
,
1719 JPOOL_UTF_DATA (jcf
, name_index
),
1720 JPOOL_UTF_LENGTH (jcf
, name_index
),
1724 /* Print PREFIX, then a class name in C++ format. If the name refers
1725 to an array, ignore it and don't print PREFIX. Returns 1 if
1726 something was printed, 0 otherwise. */
1728 print_cxx_classname (FILE *stream
, const char *prefix
,
1729 JCF
*jcf
, int index
, int add_scope
)
1731 int name_index
= JPOOL_USHORT1 (jcf
, index
);
1733 const unsigned char *s
, *p
, *limit
;
1735 s
= JPOOL_UTF_DATA (jcf
, name_index
);
1736 len
= JPOOL_UTF_LENGTH (jcf
, name_index
);
1739 /* Explicitly omit arrays here. */
1741 c
= UTF8_GET (p
, limit
);
1745 fputs (prefix
, stream
);
1747 /* Print a leading "::" so we look in the right namespace. */
1748 if (! flag_jni
&& ! stubs
&& add_scope
)
1749 fputs ("::", stream
);
1753 c
= UTF8_GET (s
, limit
);
1755 fputs (flag_jni
? "_" : "::", stream
);
1757 jni_print_char (stream
, c
);
1763 int written_class_count
= 0;
1765 /* Return name of superclass. If LEN is not NULL, fill it with length
1767 static const unsigned char *
1768 super_class_name (JCF
*derived_jcf
, int *len
)
1770 int supername_index
= JPOOL_USHORT1 (derived_jcf
, derived_jcf
->super_class
);
1771 int supername_length
= JPOOL_UTF_LENGTH (derived_jcf
, supername_index
);
1772 const unsigned char *supername
=
1773 JPOOL_UTF_DATA (derived_jcf
, supername_index
);
1776 *len
= supername_length
;
1782 handle_inner_classes (int count
)
1786 if (out
&& ! flag_jni
&& ! stubs
&& count
> 0)
1787 fprintf (out
, "\n");
1789 for (i
= 0; i
< count
; ++i
)
1791 JCF_u2 inner_info_index
= JCF_readu2 (current_jcf
);
1793 /* There are a few more values here, but we don't care about
1794 them. The (void) cast is apparently the only way to avoid a
1796 (void) JCF_readu2 (current_jcf
);
1797 (void) JCF_readu2 (current_jcf
);
1798 (void) JCF_readu2 (current_jcf
);
1800 if (out
&& ! flag_jni
&& ! stubs
)
1802 print_mangled_classname (out
, current_jcf
, " friend class ",
1804 fprintf (out
, ";\n");
1811 /* We keep track of all the `#include's we generate, so we can avoid
1816 struct include
*next
;
1819 /* List of all includes. */
1820 static struct include
*all_includes
= NULL
;
1822 /* Generate a #include. */
1824 print_include (FILE *out
, const unsigned char *utf8
, int len
)
1826 struct include
*incl
;
1832 len
= strlen ((const char *) utf8
);
1834 for (incl
= all_includes
; incl
; incl
= incl
->next
)
1836 /* We check the length because we might have a proper prefix. */
1837 if (len
== (int) strlen (incl
->name
)
1838 && ! strncmp (incl
->name
, (const char *) utf8
, len
))
1842 incl
= XNEW (struct include
);
1843 incl
->name
= XNEWVEC (char, len
+ 1);
1844 strncpy (incl
->name
, (const char *) utf8
, len
);
1845 incl
->name
[len
] = '\0';
1846 incl
->next
= all_includes
;
1847 all_includes
= incl
;
1849 fputs ("#include <", out
);
1850 jcf_print_utf8_replace (out
, utf8
, len
,
1852 flag_jni
? '_' : '/');
1853 fputs (".h>\n", out
);
1858 /* This is used to represent part of a package or class name. */
1861 /* The text of this part of the name. */
1863 /* True if this represents a class. */
1865 /* Linked list of all classes and packages inside this one. */
1866 struct namelet
*subnamelets
;
1867 /* Pointer to next sibling. */
1868 struct namelet
*next
;
1871 static void add_namelet (const unsigned char *, const unsigned char *,
1873 static void print_namelet (FILE *, struct namelet
*, int);
1875 /* The special root namelet. */
1876 static struct namelet root
=
1884 /* This extracts the next name segment from the full UTF-8 encoded
1885 package or class name and links it into the tree. It does this
1888 add_namelet (const unsigned char *name
, const unsigned char *name_limit
,
1889 struct namelet
*parent
)
1891 const unsigned char *p
;
1892 struct namelet
*n
= NULL
, *np
;
1894 /* We want to skip the standard namespaces that we assume the
1895 runtime already knows about. We only do this at the top level,
1896 though, hence the check for `root'. */
1897 if (parent
== &root
)
1899 #define JAVALANG "java/lang/"
1900 #define JAVAIO "java/io/"
1901 #define JAVAUTIL "java/util/"
1902 if ((name_limit
- name
>= (int) sizeof (JAVALANG
) - 1
1903 && ! strncmp ((const char *) name
, JAVALANG
, sizeof (JAVALANG
) - 1))
1904 || (name_limit
- name
>= (int) sizeof (JAVAUTIL
) - 1
1905 && ! strncmp ((const char *) name
, JAVAUTIL
, sizeof (JAVAUTIL
) - 1))
1906 || (name_limit
- name
>= (int) sizeof (JAVAIO
) - 1
1907 && ! strncmp ((const char *) name
, JAVAIO
, sizeof (JAVAIO
) - 1)))
1911 for (p
= name
; p
< name_limit
&& *p
!= '/'; ++p
)
1914 /* Search for this name beneath the PARENT node. */
1915 for (np
= parent
->subnamelets
; np
!= NULL
; np
= np
->next
)
1917 /* We check the length because we might have a proper prefix. */
1918 if ((int) strlen (np
->name
) == p
- name
&&
1919 ! strncmp ((const char *) name
, np
->name
, p
- name
))
1928 n
= XNEW (struct namelet
);
1929 n
->name
= XNEWVEC (char, p
- name
+ 1);
1930 strncpy (n
->name
, (const char *) name
, p
- name
);
1931 n
->name
[p
- name
] = '\0';
1932 n
->is_class
= (p
== name_limit
);
1933 n
->subnamelets
= NULL
;
1934 n
->next
= parent
->subnamelets
;
1935 parent
->subnamelets
= n
;
1938 /* We recurse if there is more text, and if the trailing piece does
1939 not represent an inner class. */
1941 add_namelet (p
+ 1, name_limit
, n
);
1944 /* Print a single namelet. Destroys namelets while printing. */
1946 print_namelet (FILE *out
, struct namelet
*name
, int depth
)
1953 for (i
= 0; i
< depth
; ++i
)
1955 fprintf (out
, "%s %s", name
->is_class
? "class" : "namespace",
1957 if (name
->is_class
&& name
->subnamelets
== NULL
)
1963 for (i
= 0; i
< depth
; ++i
)
1969 c
= name
->subnamelets
;
1972 struct namelet
*next
= c
->next
;
1973 print_namelet (out
, c
, depth
+ 2);
1976 name
->subnamelets
= NULL
;
1982 for (i
= 0; i
< depth
; ++i
)
1985 /* Only print a `;' when printing a class. C++ is evil. */
1995 /* This is called to add some classes to the list of classes for which
1996 we need decls. The signature argument can be a function
1999 add_class_decl (FILE *out
, JCF
*jcf
, JCF_u2 signature
)
2001 const unsigned char *s
= JPOOL_UTF_DATA (jcf
, signature
);
2002 int len
= JPOOL_UTF_LENGTH (jcf
, signature
);
2005 for (i
= 0; i
< len
; ++i
)
2009 /* If we see an array, then we include the array header. */
2012 print_include (out
, (const unsigned char *) "gcj/array", -1);
2016 /* We're looking for `L<stuff>;' -- everything else is
2021 for (start
= ++i
; i
< len
&& s
[i
] != ';'; ++i
)
2024 add_namelet (&s
[start
], &s
[i
], &root
);
2028 /* Print declarations for all classes required by this class. Any
2029 class or package in the `java' package is assumed to be handled
2030 statically in libjava; we don't generate declarations for these.
2031 This makes the generated headers a bit easier to read. */
2033 print_class_decls (FILE *out
, JCF
*jcf
, int self
)
2035 /* Make sure to always add the current class to the list of things
2036 that should be declared. */
2037 int name_index
= JPOOL_USHORT1 (jcf
, self
);
2039 const unsigned char *s
;
2041 s
= JPOOL_UTF_DATA (jcf
, name_index
);
2042 len
= JPOOL_UTF_LENGTH (jcf
, name_index
);
2043 add_namelet (s
, s
+ len
, &root
);
2045 if (root
.subnamelets
)
2047 fputs ("extern \"Java\"\n{\n", out
);
2048 /* We use an initial offset of 0 because the root namelet
2049 doesn't cause anything to print. */
2050 print_namelet (out
, &root
, 0);
2051 fputs ("}\n\n", out
);
2058 process_file (JCF
*jcf
, FILE *out
)
2061 uint32 field_start
, method_end
, method_start
;
2067 if (jcf_parse_preamble (jcf
) != 0)
2069 error ("Not a valid Java .class file.");
2073 /* Parse and possibly print constant pool */
2074 code
= jcf_parse_constant_pool (jcf
);
2077 error ("error while parsing constant pool");
2080 code
= verify_constant_pool (jcf
);
2083 error ("error in constant pool entry #%d", code
);
2087 jcf_parse_class (jcf
);
2089 if (written_class_count
++ == 0 && out
)
2091 const char *cstart
, *cstart2
, *mode
, *cend
, *what
, *jflag
;
2106 mode
= " -*- c++ -*-";
2112 fprintf (out
, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
2113 cstart
, mode
, cend
);
2116 fprintf (out
, "%s This file was created by `" TOOLNAME
" -stubs%s'.%s\n\
2118 %s This file is intended to give you a head start on implementing native\n\
2119 %s methods using %s.\n\
2120 %s Be aware: running `" TOOLNAME
" -stubs %s' once more for this class may\n\
2121 %s overwrite any edits you have made to this file.%s\n\n",
2122 cstart
, jflag
, mode
,
2138 print_mangled_classname (out
, jcf
, "#ifndef __", jcf
->this_class
);
2139 fprintf (out
, "__\n");
2141 print_mangled_classname (out
, jcf
, "#define __", jcf
->this_class
);
2142 fprintf (out
, "__\n\n");
2146 fprintf (out
, "#include <jni.h>\n\n");
2147 fprintf (out
, "#ifdef __cplusplus\n");
2148 fprintf (out
, "extern \"C\"\n");
2149 fprintf (out
, "{\n");
2150 fprintf (out
, "#endif\n");
2154 /* We do this to ensure that inline methods won't be
2155 `outlined' by g++. This works as long as method and
2156 fields are not added by the user. */
2157 fprintf (out
, "#pragma interface\n");
2159 if (jcf
->super_class
)
2162 const unsigned char *supername
=
2163 super_class_name (jcf
, &super_length
);
2166 print_include (out
, supername
, super_length
);
2172 /* Strip off the ".class" portion of the name when printing
2173 the include file name. */
2175 int i
, len
= strlen (jcf
->classname
);
2176 if (len
> 6 && ! strcmp (&jcf
->classname
[len
- 6], ".class"))
2178 /* Turn the class name into a file name. */
2179 name
= XNEWVEC (char, len
+ 1);
2180 for (i
= 0; i
< len
; ++i
)
2181 name
[i
] = jcf
->classname
[i
] == '.' ? '/' : jcf
->classname
[i
];
2183 print_include (out
, (const unsigned char *) name
, len
);
2188 print_include (out
, (const unsigned char *) "gcj/cni", -1);
2189 print_include (out
, (const unsigned char *) "java/lang/UnsupportedOperationException",
2195 /* We want to parse the methods first. But we need to find where
2196 they start. So first we skip the fields, then parse the methods.
2197 Then we parse the fields and skip the methods. This is ugly, but
2198 not too bad since we need two full passes to get class decl
2199 information anyway. */
2201 field_start
= JCF_TELL (jcf
);
2202 jcf_parse_fields (jcf
);
2204 method_start
= JCF_TELL (jcf
);
2206 jcf_parse_methods (jcf
);
2211 if (out
&& ! flag_jni
)
2214 print_class_decls (out
, jcf
, jcf
->this_class
);
2216 for (i
= 0; i
< prepend_count
; ++i
)
2217 fprintf (out
, "%s\n", prepend_specs
[i
]);
2218 if (prepend_count
> 0)
2223 if (! print_cxx_classname (out
, "class ", jcf
,
2224 jcf
->this_class
, 0))
2226 error ("class is of array type\n");
2229 if (jcf
->super_class
)
2231 if (! print_cxx_classname (out
, " : public ",
2232 jcf
, jcf
->super_class
, 1))
2234 error ("base class is of array type");
2239 fputs ("\n{\n", out
);
2243 /* Now go back for second pass over methods and fields. */
2244 is_first_data_member
= 1;
2246 JCF_SEEK (jcf
, method_start
);
2248 jcf_parse_methods (jcf
);
2249 method_end
= JCF_TELL (jcf
);
2252 JCF_SEEK (jcf
, field_start
);
2253 jcf_parse_fields (jcf
);
2254 JCF_SEEK (jcf
, method_end
);
2256 jcf_parse_final_attributes (jcf
);
2262 fprintf (out
, "\n#ifdef __cplusplus\n");
2263 fprintf (out
, "}\n");
2264 fprintf (out
, "#endif\n");
2268 /* Generate friend decl if we still must. */
2269 for (i
= 0; i
< friend_count
; ++i
)
2270 fprintf (out
, " friend %s\n", friend_specs
[i
]);
2272 /* Generate extra declarations. */
2275 for (i
= 0; i
< add_count
; ++i
)
2276 fprintf (out
, " %s\n", add_specs
[i
]);
2278 /* Generate an entry for the class object. */
2279 generate_access (out
, ACC_PUBLIC
);
2280 fprintf (out
, "\n static ::java::lang::Class class$;\n");
2284 if (jcf
->access_flags
& ACC_INTERFACE
)
2285 fputs (" __attribute__ ((java_interface))", out
);
2289 if (append_count
> 0)
2291 for (i
= 0; i
< append_count
; ++i
)
2292 fprintf (out
, "%s\n", append_specs
[i
]);
2295 print_mangled_classname (out
, jcf
,
2296 "\n#endif /* __", jcf
->this_class
);
2297 fprintf (out
, "__ */\n");
2303 /* This is used to mark options with no short value. */
2304 #define LONG_OPT(Num) ((Num) + 128)
2306 #define OPT_classpath LONG_OPT (0)
2307 #define OPT_CLASSPATH OPT_classpath
2308 #define OPT_bootclasspath LONG_OPT (1)
2309 #define OPT_extdirs LONG_OPT (2)
2310 #define OPT_HELP LONG_OPT (3)
2311 #define OPT_TEMP LONG_OPT (4)
2312 #define OPT_VERSION LONG_OPT (5)
2313 #define OPT_PREPEND LONG_OPT (6)
2314 #define OPT_FRIEND LONG_OPT (7)
2315 #define OPT_ADD LONG_OPT (8)
2316 #define OPT_APPEND LONG_OPT (9)
2317 #define OPT_M LONG_OPT (10)
2318 #define OPT_MM LONG_OPT (11)
2319 #define OPT_MG LONG_OPT (12)
2320 #define OPT_MD LONG_OPT (13)
2321 #define OPT_MMD LONG_OPT (14)
2322 #define OPT_FORCE LONG_OPT (15)
2323 #define OPT_OLD LONG_OPT (16)
2324 #define OPT_TRACE LONG_OPT (17)
2326 static const struct option options
[] =
2328 { "classpath", required_argument
, NULL
, OPT_classpath
},
2329 { "bootclasspath", required_argument
, NULL
, OPT_bootclasspath
},
2330 { "extdirs", required_argument
, NULL
, OPT_extdirs
},
2331 { "CLASSPATH", required_argument
, NULL
, OPT_CLASSPATH
},
2332 { "help", no_argument
, NULL
, OPT_HELP
},
2333 { "stubs", no_argument
, &stubs
, 1 },
2334 { "td", required_argument
, NULL
, OPT_TEMP
},
2335 { "verbose", no_argument
, NULL
, 'v' },
2336 { "version", no_argument
, NULL
, OPT_VERSION
},
2337 { "prepend", required_argument
, NULL
, OPT_PREPEND
},
2338 { "friend", required_argument
, NULL
, OPT_FRIEND
},
2339 { "add", required_argument
, NULL
, OPT_ADD
},
2340 { "append", required_argument
, NULL
, OPT_APPEND
},
2341 { "M", no_argument
, NULL
, OPT_M
},
2342 { "MM", no_argument
, NULL
, OPT_MM
},
2343 { "MG", no_argument
, NULL
, OPT_MG
},
2344 { "MD", no_argument
, NULL
, OPT_MD
},
2345 { "MMD", no_argument
, NULL
, OPT_MMD
},
2346 { "jni", no_argument
, &flag_jni
, 1 },
2347 { "force", no_argument
, NULL
, OPT_FORCE
},
2348 /* If the output file should be named "ld" then a space is needed
2349 between -o and its argument, ld. */
2350 { "old", no_argument
, NULL
, OPT_OLD
},
2351 { "trace", no_argument
, NULL
, OPT_TRACE
},
2352 { NULL
, required_argument
, NULL
, 'J' },
2353 { NULL
, no_argument
, NULL
, 0 }
2359 fprintf (stderr
, _("Try '" TOOLNAME
" --help' for more information.\n"));
2366 printf (_("Usage: " TOOLNAME
" [OPTION]... CLASS...\n\n"));
2367 printf (_("Generate C or C++ header files from .class files\n\n"));
2368 printf (_(" -stubs Generate an implementation stub file\n"));
2369 printf (_(" -jni Generate a JNI header or stub\n"));
2370 printf (_(" -force Always overwrite output files\n"));
2371 printf (_(" -old Unused compatibility option\n"));
2372 printf (_(" -trace Unused compatibility option\n"));
2373 printf (_(" -J OPTION Unused compatibility option\n"));
2375 printf (_(" -add TEXT Insert TEXT into class body\n"));
2376 printf (_(" -append TEXT Insert TEXT after class declaration\n"));
2377 printf (_(" -friend TEXT Insert TEXT as 'friend' declaration\n"));
2378 printf (_(" -prepend TEXT Insert TEXT before start of class\n"));
2380 printf (_(" --classpath PATH Set path to find .class files\n"));
2381 printf (_(" -IDIR Append directory to class path\n"));
2382 printf (_(" --bootclasspath PATH Override built-in class path\n"));
2383 printf (_(" --extdirs PATH Set extensions directory path\n"));
2384 printf (_(" -d DIRECTORY Set output directory name\n"));
2385 printf (_(" -o FILE Set output file name\n"));
2386 printf (_(" -td DIRECTORY Set temporary directory name\n"));
2388 printf (_(" --help Print this help, then exit\n"));
2389 printf (_(" --version Print version number, then exit\n"));
2390 printf (_(" -v, --verbose Print extra information while running\n"));
2392 printf (_(" -M Print all dependencies to stdout;\n"
2393 " suppress ordinary output\n"));
2394 printf (_(" -MM Print non-system dependencies to stdout;\n"
2395 " suppress ordinary output\n"));
2396 printf (_(" -MD Print all dependencies to stdout\n"));
2397 printf (_(" -MMD Print non-system dependencies to stdout\n"));
2398 /* We omit -MG until it is implemented. */
2400 printf (_("For bug reporting instructions, please see:\n"
2401 "%s.\n"), bug_report_url
);
2408 printf (TOOLNAME
" (GCC) %s\n\n", version_string
);
2409 printf ("Copyright %s 2006 Free Software Foundation, Inc.\n", _("(C)"));
2410 printf (_("This is free software; see the source for copying conditions. There is NO\n"
2411 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
2416 main (int argc
, char** argv
)
2420 char *output_file
= NULL
;
2421 int emit_dependencies
= 0, suppress_output
= 0;
2423 int local_found_error
;
2425 /* Unlock the stdio streams. */
2426 unlock_std_streams ();
2428 gcc_init_libintl ();
2432 error ("no classes specified");
2438 /* We use getopt_long_only to allow single `-' long options. For
2439 some of our options this is more natural. */
2440 while ((opt
= getopt_long_only (argc
, argv
, "J:I:d:o:v", options
, NULL
)) != -1)
2445 /* Already handled. */
2449 output_file
= optarg
;
2453 output_directory
= optarg
;
2457 jcf_path_include_arg (optarg
);
2465 jcf_path_classpath_arg (optarg
);
2468 case OPT_bootclasspath
:
2469 jcf_path_bootclasspath_arg (optarg
);
2473 jcf_path_extdirs_arg (optarg
);
2481 temp_directory
= optarg
;
2489 if (prepend_count
== 0)
2490 prepend_specs
= XNEWVEC (char *, argc
);
2491 prepend_specs
[prepend_count
++] = optarg
;
2495 if (friend_count
== 0)
2496 friend_specs
= XNEWVEC (char *, argc
);
2497 friend_specs
[friend_count
++] = optarg
;
2502 add_specs
= XNEWVEC (char *, argc
);
2503 add_specs
[add_count
++] = optarg
;
2507 if (append_count
== 0)
2508 append_specs
= XNEWVEC (char *, argc
);
2509 append_specs
[append_count
++] = optarg
;
2513 emit_dependencies
= 1;
2514 suppress_output
= 1;
2515 jcf_dependency_init (1);
2519 emit_dependencies
= 1;
2520 suppress_output
= 1;
2521 jcf_dependency_init (0);
2525 error ("'-MG' option is unimplemented");
2529 emit_dependencies
= 1;
2530 jcf_dependency_init (1);
2534 emit_dependencies
= 1;
2535 jcf_dependency_init (0);
2548 /* Ignore -J options. */
2559 error ("no classes specified");
2563 jcf_path_seal (verbose
);
2565 if (output_file
&& emit_dependencies
)
2567 error ("can't specify both -o and -MD");
2571 local_found_error
= 0;
2572 for (argi
= optind
; argi
< argc
; argi
++)
2574 char *classname
= argv
[argi
];
2575 char *current_output_file
= NULL
;
2576 const char *classfile_name
;
2578 /* We reset the error state here so that we can detect errors
2579 that occur when processing this file, so the output can be
2580 unlinked if need be. */
2584 printf (_("Processing %s\n"), classname
);
2586 jcf_dependency_reset ();
2587 classfile_name
= find_class (classname
, strlen (classname
), &jcf
, 0);
2588 if (classfile_name
== NULL
)
2590 error ("%s: no such class", classname
);
2594 printf (_("Found in %s\n"), classfile_name
);
2597 if (strcmp (output_file
, "-") == 0)
2599 else if (out
== NULL
)
2601 out
= fopen (output_file
, "w");
2605 perror (output_file
);
2608 current_output_file
= output_file
;
2612 int dir_len
= strlen (output_directory
);
2613 int i
, classname_length
= strlen (classname
);
2614 current_output_file
= XNEWVEC (char, dir_len
+ classname_length
+ 5);
2615 strcpy (current_output_file
, output_directory
);
2616 if (dir_len
> 0 && output_directory
[dir_len
-1] != '/')
2617 current_output_file
[dir_len
++] = '/';
2618 for (i
= 0; classname
[i
] != '\0'; i
++)
2620 char ch
= classname
[i
];
2623 if (flag_jni
&& ch
== '/')
2625 current_output_file
[dir_len
++] = ch
;
2627 if (emit_dependencies
)
2629 if (suppress_output
)
2631 jcf_dependency_set_dep_file ("-");
2636 /* We use `.hd' and not `.d' to avoid clashes with
2637 dependency tracking from straight compilation. */
2638 strcpy (current_output_file
+ dir_len
, ".hd");
2639 jcf_dependency_set_dep_file (current_output_file
);
2642 strcpy (current_output_file
+ dir_len
,
2643 stubs
? (flag_jni
? ".c" : ".cc") : ".h");
2644 jcf_dependency_set_target (current_output_file
);
2645 if (! suppress_output
)
2647 out
= fopen (current_output_file
, "w");
2650 perror (current_output_file
);
2655 free_method_name_list ();
2656 process_file (&jcf
, out
);
2659 /* If we found an error and we're writing to a real file,
2661 if (found_error
&& ! suppress_output
&& current_output_file
!= NULL
2662 && strcmp (current_output_file
, "-"))
2663 unlink (current_output_file
);
2665 if (current_output_file
!= output_file
)
2666 free (current_output_file
);
2667 jcf_dependency_write ();
2669 local_found_error
|= found_error
;
2672 if (out
!= NULL
&& out
!= stdout
)
2675 return local_found_error
;