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
5 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
32 #include "coretypes.h"
40 #include "java-tree.h"
41 #include "java-opcodes.h"
50 /* The output file. */
53 /* Nonzero on failure. */
54 static int found_error
= 0;
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);
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 *msgid
, ...);
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 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
254 if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
256 static int decompiled
= 0;
257 #define HANDLE_END_METHOD() \
258 if (out && method_printed && !method_synthetic) \
259 fputs (decompiled || stubs ? "\n" : ";\n", out);
261 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) handle_inner_classes (COUNT)
263 /* We're going to need {peek,skip}_attribute, enable their definition. */
264 #define NEED_PEEK_ATTRIBUTE
265 #define NEED_SKIP_ATTRIBUTE
267 #include "jcf-reader.c"
269 /* Print an error message and set found_error. */
271 error (const char *msgid
, ...)
275 va_start (ap
, msgid
);
277 fprintf (stderr
, TOOLNAME
": ");
278 vfprintf (stderr
, _(msgid
), ap
);
280 fprintf (stderr
, "\n");
284 /* Print a single-precision float, suitable for parsing by g++. */
286 jni_print_float (FILE *stream
, jfloat f
)
288 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
289 work in data initializers. FIXME. */
290 if (JFLOAT_FINITE (f
))
298 fprintf (stream
, "0x1.%.6xp%+df",
299 ((unsigned int)f
.mantissa
) << 1,
300 f
.exponent
- JFLOAT_EXP_BIAS
);
302 /* Exponent of 0x01 is -125; exponent of 0x00 is *also* -125,
303 because the implicit leading 1 bit is no longer present. */
304 fprintf (stream
, "0x0.%.6xp%+df",
305 ((unsigned int)f
.mantissa
) << 1,
306 f
.exponent
+ 1 - JFLOAT_EXP_BIAS
);
310 fputs (";\n", stream
);
313 /* Print a double-precision float, suitable for parsing by g++. */
315 jni_print_double (FILE *stream
, jdouble f
)
317 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
318 work in data initializers. FIXME. */
319 if (JDOUBLE_FINITE (f
))
327 fprintf (stream
, "0x1.%.5x%.8xp%+d",
328 f
.mantissa0
, f
.mantissa1
,
329 f
.exponent
- JDOUBLE_EXP_BIAS
);
331 /* Exponent of 0x001 is -1022; exponent of 0x000 is *also* -1022,
332 because the implicit leading 1 bit is no longer present. */
333 fprintf (stream
, "0x0.%.5x%.8xp%+d",
334 f
.mantissa0
, f
.mantissa1
,
335 f
.exponent
+ 1 - JDOUBLE_EXP_BIAS
);
338 fputs (flag_jni
? "\n" : ";\n", stream
);
341 /* Print a character, appropriately mangled for JNI. */
344 jni_print_char (FILE *stream
, int ch
)
347 jcf_print_char (stream
, ch
);
348 else if (ch
== '(' || ch
== ')')
353 fputs ("_1", stream
);
355 fputs ("_2", stream
);
357 fputs ("_3", stream
);
360 else if (ISALNUM (ch
))
364 /* "Unicode" character. */
365 fprintf (stream
, "_0%04x", ch
);
369 /* Print a name from the class data. If the index does not point to a
370 string, an error results. */
373 print_name (FILE* stream
, JCF
* jcf
, int name_index
)
375 if (JPOOL_TAG (jcf
, name_index
) != CONSTANT_Utf8
)
377 fprintf (stream
, "<not a UTF8 constant>");
381 jcf_print_utf8 (stream
, JPOOL_UTF_DATA (jcf
, name_index
),
382 JPOOL_UTF_LENGTH (jcf
, name_index
));
385 /* For JNI we must correctly quote each character. */
386 const unsigned char *str
= JPOOL_UTF_DATA (jcf
, name_index
);
387 int length
= JPOOL_UTF_LENGTH (jcf
, name_index
);
388 const unsigned char *limit
= str
+ length
;
391 int ch
= UTF8_GET (str
, limit
);
394 fprintf (stream
, "\\<invalid>");
397 jni_print_char (stream
, ch
);
402 /* Print base name of class. The base name is everything after the
406 print_base_classname (FILE *stream
, JCF
*jcf
, int index
)
408 int name_index
= JPOOL_USHORT1 (jcf
, index
);
410 const unsigned char *s
, *p
, *limit
;
412 s
= JPOOL_UTF_DATA (jcf
, name_index
);
413 len
= JPOOL_UTF_LENGTH (jcf
, name_index
);
418 int c
= UTF8_GET (s
, limit
);
425 int ch
= UTF8_GET (p
, limit
);
427 fputs ("::", stream
);
429 jcf_print_char (stream
, ch
);
433 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
434 and 1 if STR is "greater" than NAME. */
437 utf8_cmp (const unsigned char *str
, int length
, const char *name
)
439 const unsigned char *limit
= str
+ length
;
442 for (i
= 0; name
[i
]; ++i
)
444 int ch
= UTF8_GET (str
, limit
);
449 return str
== limit
? 0 : 1;
452 /* This is a sorted list of all C++ keywords. */
454 static const char *const cxx_keywords
[] =
563 /* If NAME is the name of a C++ keyword, then return an override name.
564 This is a name that can be used in place of the keyword.
565 Otherwise, return NULL. The return value is malloc()d. */
568 cxx_keyword_subst (const unsigned char *str
, int length
)
570 int last
= ARRAY_SIZE (cxx_keywords
);
572 int mid
= (last
+ first
) / 2;
575 for (mid
= (last
+ first
) / 2;
577 old
= mid
, mid
= (last
+ first
) / 2)
579 int kwl
= strlen (cxx_keywords
[mid
]);
580 int min_length
= kwl
> length
? length
: kwl
;
581 int r
= utf8_cmp (str
, min_length
, cxx_keywords
[mid
]);
587 /* Skip all trailing `$'. */
588 for (i
= min_length
; i
< length
&& str
[i
] == '$'; ++i
)
590 /* We've only found a match if all the remaining characters
594 char *dup
= xmalloc (2 + length
- min_length
+ kwl
);
595 strcpy (dup
, cxx_keywords
[mid
]);
596 for (i
= kwl
; i
< length
+ 1; ++i
)
612 /* Generate an access control keyword based on FLAGS. */
615 generate_access (FILE *stream
, JCF_u2 flags
)
617 if ((flags
& ACC_VISIBILITY
) == last_access
)
619 last_access
= (flags
& ACC_VISIBILITY
);
624 fputs ("public: // actually package-private\n", stream
);
627 fputs ("public:\n", stream
);
630 fputs ("private:\n", stream
);
633 fputs ("public: // actually protected\n", stream
);
637 fprintf (stream
, "#error unrecognized visibility %d\n",
638 (flags
& ACC_VISIBILITY
));
643 /* See if NAME is already the name of a method. */
645 name_is_method_p (const unsigned char *name
, int length
)
647 struct method_name
*p
;
649 for (p
= method_name_list
; p
!= NULL
; p
= p
->next
)
651 if (p
->length
== length
&& ! memcmp (p
->name
, name
, length
))
657 /* Free the method name list. */
659 free_method_name_list (void)
661 struct method_name
*p
= method_name_list
;
664 struct method_name
*next
= p
->next
;
670 method_name_list
= NULL
;
673 /* If there is already a native method named NAME, whose signature is not
674 SIGNATURE, then return true. Otherwise return false. */
676 overloaded_jni_method_exists_p (const unsigned char *name
, int length
,
677 const char *signature
, int sig_length
)
679 struct method_name
*p
;
681 for (p
= method_name_list
; p
!= NULL
; p
= p
->next
)
684 && p
->length
== length
685 && ! memcmp (p
->name
, name
, length
)
686 && (p
->sig_length
!= sig_length
687 || memcmp (p
->signature
, signature
, sig_length
)))
693 /* Get name of a field. This handles renamings due to C++ clash. */
695 get_field_name (JCF
*jcf
, int name_index
, JCF_u2 flags
)
697 unsigned char *name
= JPOOL_UTF_DATA (jcf
, name_index
);
698 int length
= JPOOL_UTF_LENGTH (jcf
, name_index
);
701 if (name_is_method_p (name
, length
))
703 /* This field name matches a method. So override the name with
704 a dummy name. This is yucky, but it isn't clear what else to
705 do. FIXME: if the field is static, then we'll be in real
707 if ((flags
& ACC_STATIC
))
709 error ("static field has same name as method");
713 override
= xmalloc (length
+ 3);
714 memcpy (override
, name
, length
);
715 strcpy (override
+ length
, "__");
720 override
= cxx_keyword_subst (name
, length
);
725 /* Print a field name. Convenience function for use with
728 print_field_name (FILE *stream
, JCF
*jcf
, int name_index
, JCF_u2 flags
)
730 char *override
= get_field_name (jcf
, name_index
, flags
);
734 fputs (override
, stream
);
738 jcf_print_utf8 (stream
, JPOOL_UTF_DATA (jcf
, name_index
),
739 JPOOL_UTF_LENGTH (jcf
, name_index
));
743 print_field_info (FILE *stream
, JCF
* jcf
, int name_index
, int sig_index
,
746 char *override
= NULL
;
749 generate_access (stream
, flags
);
750 if (JPOOL_TAG (jcf
, name_index
) != CONSTANT_Utf8
)
752 fprintf (stream
, "<not a UTF8 constant>");
759 /* For JNI we only want to print real constants. */
761 if (! (flags
& ACC_STATIC
)
762 || ! (flags
& ACC_FINAL
)
763 || current_field_value
<= 0)
765 val
= JPOOL_TAG (jcf
, current_field_value
);
766 if (val
!= CONSTANT_Integer
&& val
!= CONSTANT_Long
767 && val
!= CONSTANT_Float
&& val
!= CONSTANT_Double
)
772 /* Initial indentation. */
776 if ((flags
& ACC_STATIC
))
780 print_cxx_classname (stream
, "#undef ", jcf
, jcf
->this_class
, 1);
782 print_field_name (stream
, jcf
, name_index
, 0);
783 fputs ("\n", stream
);
784 print_cxx_classname (stream
, "#define ", jcf
, jcf
->this_class
, 1);
788 fputs ("static ", stream
);
790 if ((flags
& ACC_FINAL
) && current_field_value
> 0)
795 switch (JPOOL_TAG (jcf
, current_field_value
))
797 case CONSTANT_Integer
:
800 int most_negative
= 0;
802 fputs ("const jint ", stream
);
803 print_field_name (stream
, jcf
, name_index
, 0);
804 fputs (flag_jni
? " " : " = ", stream
);
805 num
= JPOOL_INT (jcf
, current_field_value
);
806 /* We single out the most negative number to print
807 specially. This avoids later warnings from g++. */
808 if (num
== (jint
) 0x80000000)
813 format_int (buffer
, (jlong
) num
, 10);
814 fprintf (stream
, "%sL%s%s\n", buffer
,
815 most_negative
? " - 1" : "",
816 flag_jni
? "" : ";");
822 int most_negative
= 0;
824 fputs ("const jlong ", stream
);
825 print_field_name (stream
, jcf
, name_index
, 0);
826 fputs (flag_jni
? " " : " = ", stream
);
827 num
= JPOOL_LONG (jcf
, current_field_value
);
828 /* We single out the most negative number to print
829 specially.. This avoids later warnings from g++. */
830 if (num
== (jlong
) 0x8000000000000000LL
)
835 format_int (buffer
, num
, 10);
836 fprintf (stream
, "%sLL%s%s\n", buffer
,
837 most_negative
? " - 1" :"",
838 flag_jni
? "" : ";");
843 jfloat fnum
= JPOOL_FLOAT (jcf
, current_field_value
);
845 fputs ("const jfloat ", stream
);
846 print_field_name (stream
, jcf
, name_index
, 0);
847 jni_print_float (stream
, fnum
);
850 case CONSTANT_Double
:
852 jdouble dnum
= JPOOL_DOUBLE (jcf
, current_field_value
);
854 fputs ("const jdouble ", stream
);
855 print_field_name (stream
, jcf
, name_index
, 0);
856 jni_print_double (stream
, dnum
);
860 /* We can't print this as a constant, but we can still
861 print something sensible. */
871 /* assert (! flag_jni); */
872 override
= get_field_name (jcf
, name_index
, flags
);
873 print_c_decl (stream
, jcf
, name_index
, sig_index
, 0, override
, flags
);
874 fputs (";\n", stream
);
882 print_method_info (FILE *stream
, JCF
* jcf
, int name_index
, int sig_index
,
885 const unsigned char *str
;
886 int length
, is_init
= 0;
887 char *override
= NULL
;
890 method_access
= flags
;
891 if (stream
&& JPOOL_TAG (jcf
, name_index
) != CONSTANT_Utf8
)
892 fprintf (stream
, "<not a UTF8 constant>");
893 str
= JPOOL_UTF_DATA (jcf
, name_index
);
894 length
= JPOOL_UTF_LENGTH (jcf
, name_index
);
898 /* Ignore the internally generated method <clinit>. However,
899 treat <init> as a constructor. */
900 if (! utf8_cmp (str
, length
, "<init>"))
902 else if (! METHOD_IS_FINAL (jcf
->access_flags
, flags
)
903 && ! (flags
& ACC_STATIC
))
905 /* FIXME: i18n bug here. Order of prints should not be
907 fprintf (stderr
, _("ignored method '"));
908 jcf_print_utf8 (stderr
, str
, length
);
909 fprintf (stderr
, _("' marked virtual\n"));
917 /* During the first method pass, build a list of method names. This will
918 be used to determine if field names conflict with method names. */
921 struct method_name
*nn
;
923 nn
= xmalloc (sizeof (struct method_name
));
924 nn
->name
= xmalloc (length
);
925 memcpy (nn
->name
, str
, length
);
927 nn
->next
= method_name_list
;
928 nn
->sig_length
= JPOOL_UTF_LENGTH (jcf
, sig_index
);
929 nn
->signature
= xmalloc (nn
->sig_length
);
930 nn
->is_native
= METHOD_IS_NATIVE (flags
);
931 memcpy (nn
->signature
, JPOOL_UTF_DATA (jcf
, sig_index
),
933 method_name_list
= nn
;
935 /* The rest of this function doesn't matter. */
939 /* We don't worry about overrides in JNI mode. */
942 /* We can't generate a method whose name is a C++ reserved word.
943 We can't just ignore the function, because that will cause
944 incorrect code to be generated if the function is virtual
945 (not only for calls to this function for for other functions
946 after it in the vtbl). So we give it a dummy name instead. */
947 override
= cxx_keyword_subst (str
, length
);
950 if (! stubs
&& ! flag_jni
)
954 generate_access (stream
, flags
);
957 if ((flags
& ACC_STATIC
))
958 fputs ("static ", out
);
959 else if (! METHOD_IS_PRIVATE (jcf
->access_flags
, flags
))
961 /* Don't print `virtual' if we have a constructor. */
963 fputs ("virtual ", out
);
965 print_c_decl (out
, jcf
, name_index
, sig_index
, is_init
, override
, flags
);
967 if ((flags
& ACC_ABSTRACT
))
974 if (METHOD_IS_NATIVE (flags
))
977 print_stub_or_jni (out
, jcf
, name_index
, sig_index
,
978 is_init
, override
, flags
);
986 /* A helper for the decompiler which prints a `return' statement where
987 the type is a reference type. If METHODTYPE and OBJECTTYPE are not
988 identical, we emit a cast. We do this because the C++ compiler
989 doesn't know that a reference can be cast to the type of an
990 interface it implements. METHODTYPE is the index of the method's
991 signature. NAMEINDEX is the index of the field name; -1 for
992 `this'. OBJECTTYPE is the index of the object's type. */
994 decompile_return_statement (FILE *out
, JCF
*jcf
, int methodtype
,
995 int nameindex
, int objecttype
)
998 int obj_name_len
, method_name_len
;
999 const unsigned char *obj_data
, *method_data
;
1001 obj_name_len
= JPOOL_UTF_LENGTH (jcf
, objecttype
);
1002 obj_data
= JPOOL_UTF_DATA (jcf
, objecttype
);
1004 method_name_len
= JPOOL_UTF_LENGTH (jcf
, methodtype
);
1005 method_data
= JPOOL_UTF_DATA (jcf
, methodtype
);
1007 /* Skip forward to return type part of method. */
1008 while (*method_data
!= ')')
1013 /* Skip past `)'. */
1017 /* If we see an `L', skip it and the trailing `;'. */
1018 if (method_data
[0] == 'L' && method_data
[method_name_len
- 1] == ';')
1021 method_name_len
-= 2;
1023 if (obj_data
[0] == 'L' && obj_data
[obj_name_len
- 1] == ';')
1029 /* FIXME: if METHODTYPE is a superclass of OBJECTTYPE then we don't
1030 need a cast. Right now there is no way to determine if this is
1032 if (method_name_len
!= obj_name_len
)
1037 for (i
= 0; i
< method_name_len
; ++i
)
1039 if (method_data
[i
] != obj_data
[i
])
1047 fputs (" { return ", out
);
1051 int array_depth
= 0;
1052 const unsigned char *limit
;
1054 fputs ("reinterpret_cast<", out
);
1056 while (*method_data
== '[')
1061 fputs ("JArray<", out
);
1064 /* Leading space to avoid C++ digraphs. */
1067 /* If we see an `L', skip it and the trailing `;'. Only do this
1068 if we've seen an array specification. If we don't have an
1069 array then the `L' was stripped earlier. */
1070 if (array_depth
&& method_data
[0] == 'L'
1071 && method_data
[method_name_len
- 1] == ';')
1074 method_name_len
-= 2;
1077 limit
= method_data
+ method_name_len
;
1078 while (method_data
< limit
)
1080 int ch
= UTF8_GET (method_data
, limit
);
1084 jcf_print_char (out
, ch
);
1088 /* Close each array. */
1089 while (array_depth
> 0)
1095 /* Close the cast. */
1099 if (nameindex
== -1)
1100 fputs ("this", out
);
1102 print_field_name (out
, jcf
, nameindex
, 0);
1111 /* Try to decompile a method body. Right now we just try to handle a
1112 simple case that we can do. Expand as desired. */
1114 decompile_method (FILE *out
, JCF
*jcf
, int code_len
)
1116 const unsigned char *codes
= jcf
->read_ptr
;
1118 uint16 name_and_type
, name
;
1120 /* If the method is synchronized, don't touch it. */
1121 if ((method_access
& ACC_SYNCHRONIZED
))
1125 && codes
[0] == OPCODE_aload_0
1126 && codes
[1] == OPCODE_getfield
1127 && (codes
[4] == OPCODE_areturn
1128 || codes
[4] == OPCODE_dreturn
1129 || codes
[4] == OPCODE_freturn
1130 || codes
[4] == OPCODE_ireturn
1131 || codes
[4] == OPCODE_lreturn
))
1133 /* Found code like `return FIELD'. */
1134 index
= (codes
[2] << 8) | codes
[3];
1135 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
1136 name_and_type
= JPOOL_USHORT2 (jcf
, index
);
1137 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
1138 name
= JPOOL_USHORT1 (jcf
, name_and_type
);
1139 if (codes
[4] == OPCODE_areturn
)
1140 decompile_return_statement (out
, jcf
, method_signature
,
1141 name
, JPOOL_USHORT2 (jcf
, name_and_type
));
1144 fputs (" { return ", out
);
1146 print_field_name (out
, jcf
, name
, 0);
1151 else if (code_len
== 2
1152 && codes
[0] == OPCODE_aload_0
1153 && codes
[1] == OPCODE_areturn
1154 /* We're going to generate `return this'. This only makes
1155 sense for non-static methods. */
1156 && ! (method_access
& ACC_STATIC
))
1158 decompile_return_statement (out
, jcf
, method_signature
, -1,
1159 JPOOL_USHORT1 (jcf
, jcf
->this_class
));
1162 else if (code_len
== 1 && codes
[0] == OPCODE_return
)
1164 /* Found plain `return'. */
1165 fputs (" { }", out
);
1168 else if (code_len
== 2
1169 && codes
[0] == OPCODE_aconst_null
1170 && codes
[1] == OPCODE_areturn
)
1172 /* Found `return null'. We don't want to depend on NULL being
1174 fputs (" { return 0; }", out
);
1179 /* Like strcmp, but invert the return result for the hash table. This
1180 should probably be in hashtab.c to complement the existing string
1183 gcjh_streq (const void *p1
, const void *p2
)
1185 return ! strcmp ((char *) p1
, (char *) p2
);
1188 /* Return 1 if the initial part of CLNAME names a subclass of throwable,
1189 or 0 if not. CLNAME may be extracted from a signature, and can be
1190 terminated with either `;' or NULL. */
1192 throwable_p (const unsigned char *clname
)
1195 unsigned char *current
;
1199 /* We keep two hash tables of class names. In one we list all the
1200 classes which are subclasses of Throwable. In the other we will
1201 all other classes. We keep two tables to make the code a bit
1202 simpler; we don't have to have a structure mapping class name to
1203 a `throwable?' bit. */
1204 static htab_t throw_hash
;
1205 static htab_t non_throw_hash
;
1206 static int init_done
= 0;
1213 /* Self-initializing. The cost of this really doesn't matter.
1214 We also don't care about freeing these, either. */
1215 throw_hash
= htab_create (10, htab_hash_string
, gcjh_streq
,
1217 non_throw_hash
= htab_create (10, htab_hash_string
, gcjh_streq
,
1220 /* Make sure the root classes show up in the tables. */
1221 str
= (unsigned char *) xstrdup ("java.lang.Throwable");
1222 slot
= htab_find_slot (throw_hash
, str
, INSERT
);
1225 str
= (unsigned char *) xstrdup ("java.lang.Object");
1226 slot
= htab_find_slot (non_throw_hash
, str
, INSERT
);
1232 for (length
= 0; clname
[length
] != ';' && clname
[length
] != '\0'; ++length
)
1234 current
= ALLOC (length
+ 1);
1235 for (i
= 0; i
< length
; ++i
)
1236 current
[i
] = clname
[i
] == '/' ? '.' : clname
[i
];
1237 current
[length
] = '\0';
1239 /* We don't compute the hash slot here because the table might be
1240 modified by the recursion. In that case the slot could be
1242 if (htab_find (throw_hash
, current
))
1244 else if (htab_find (non_throw_hash
, current
))
1250 unsigned char *super
, *tmp
;
1251 int super_length
= -1;
1252 const char *classfile_name
= find_class ((char *) current
, strlen ((const char *) current
),
1255 if (! classfile_name
)
1257 error ("couldn't find class %s", current
);
1260 if (jcf_parse_preamble (&jcf
) != 0
1261 || jcf_parse_constant_pool (&jcf
) != 0
1262 || verify_constant_pool (&jcf
) > 0)
1264 error ("parse error while reading %s", classfile_name
);
1267 jcf_parse_class (&jcf
);
1269 tmp
= (unsigned char *) super_class_name (&jcf
, &super_length
);
1270 super
= ALLOC (super_length
+ 1);
1271 memcpy (super
, tmp
, super_length
);
1272 super
[super_length
] = '\0';
1274 result
= throwable_p (super
);
1275 slot
= htab_find_slot (result
? throw_hash
: non_throw_hash
,
1286 /* Print one piece of a signature. Returns pointer to next parseable
1287 character on success, NULL on error. */
1288 static const unsigned char *
1289 decode_signature_piece (FILE *stream
, const unsigned char *signature
,
1290 const unsigned char *limit
, int *need_space
)
1293 int array_depth
= 0;
1295 switch (signature
[0])
1298 /* More spaghetti. */
1301 for (signature
++; (signature
< limit
1302 && ISDIGIT (*signature
)); signature
++)
1307 ctype
= "jbyteArray";
1310 ctype
= "jcharArray";
1313 ctype
= "jdoubleArray";
1316 ctype
= "jfloatArray";
1319 ctype
= "jintArray";
1322 ctype
= "jshortArray";
1325 ctype
= "jlongArray";
1328 ctype
= "jbooleanArray";
1331 /* We have a nested array. */
1334 fputs ("JArray<", stream
);
1338 /* We have to generate a reference to JArray here, so that
1339 our output matches what the compiler does. */
1341 /* Space between `<' and `:' to avoid C++ digraphs. */
1343 fputs ("JArray< ::", stream
);
1344 while (signature
< limit
&& *signature
!= ';')
1346 int ch
= UTF8_GET (signature
, limit
);
1350 fputs ("::", stream
);
1352 jcf_print_char (stream
, ch
);
1356 fputs (" *> *", stream
);
1361 /* Unparseable signature. */
1365 /* If the previous iterations left us with something to print,
1366 print it. For JNI, we always print `jobjectArray' in the
1368 if (flag_jni
&& (ctype
== NULL
|| array_depth
> 0))
1370 ctype
= "jobjectArray";
1373 /* The `printit' case will advance SIGNATURE for us. If we
1374 don't go there, we must advance past the `;' ourselves. */
1382 /* This shouldn't happen. */
1385 case 'B': ctype
= "jbyte"; goto printit
;
1386 case 'C': ctype
= "jchar"; goto printit
;
1387 case 'D': ctype
= "jdouble"; goto printit
;
1388 case 'F': ctype
= "jfloat"; goto printit
;
1389 case 'I': ctype
= "jint"; goto printit
;
1390 case 'J': ctype
= "jlong"; goto printit
;
1391 case 'S': ctype
= "jshort"; goto printit
;
1392 case 'Z': ctype
= "jboolean"; goto printit
;
1393 case 'V': ctype
= "void"; goto printit
;
1397 /* We know about certain types and special-case their names. */
1398 if (! strncmp ((const char *) signature
, "Ljava/lang/String;",
1399 sizeof ("Ljava/lang/String;") -1))
1401 else if (! strncmp ((const char *) signature
, "Ljava/lang/Class;",
1402 sizeof ("Ljava/lang/Class;") - 1))
1404 /* Skip leading 'L' for throwable_p call. */
1405 else if (throwable_p (signature
+ 1))
1406 ctype
= "jthrowable";
1410 while (*signature
&& *signature
!= ';')
1415 /* Print a leading "::" so we look in the right namespace. */
1416 fputs ("::", stream
);
1418 while (*signature
&& *signature
!= ';')
1420 int ch
= UTF8_GET (signature
, limit
);
1422 fputs ("::", stream
);
1424 jcf_print_char (stream
, ch
);
1426 fputs (" *", stream
);
1427 if (*signature
== ';')
1433 jni_print_char (stream
, *signature
++);
1438 fputs (ctype
, stream
);
1444 while (array_depth
-- > 0)
1445 fputs ("> *", stream
);
1452 print_c_decl (FILE* stream
, JCF
* jcf
, int name_index
, int signature_index
,
1453 int is_init
, const char *name_override
, int flags
)
1455 if (JPOOL_TAG (jcf
, signature_index
) != CONSTANT_Utf8
)
1457 fprintf (stream
, "<not a UTF8 constant>");
1462 int length
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1463 const unsigned char *str0
= JPOOL_UTF_DATA (jcf
, signature_index
);
1464 const unsigned char *str
= str0
;
1465 const unsigned char *limit
= str
+ length
;
1467 int is_method
= str
[0] == '(';
1468 const unsigned char *next
;
1470 /* If printing a method, skip to the return signature and print
1471 that first. However, there is no return value if this is a
1473 if (is_method
&& ! is_init
)
1483 /* If printing a field or an ordinary method, then print the
1484 "return value" now. */
1485 if (! is_method
|| ! is_init
)
1487 next
= decode_signature_piece (stream
, str
, limit
, &need_space
);
1490 error ("unparseable signature: '%s'", str0
);
1495 /* Force the alignment of the first data member. This is
1496 because the "new" C++ ABI changed the alignment of non-POD
1497 classes. gcj, however, still uses the "old" alignment. */
1498 if (is_first_data_member
&& ! (flags
& ACC_STATIC
) && ! is_method
)
1500 is_first_data_member
= 0;
1501 print_cxx_classname (out
, " __attribute__((aligned(__alignof__( ",
1502 jcf
, jcf
->super_class
, 1);
1503 fputs (" )))) ", stream
);
1506 /* Now print the name of the thing. */
1508 fputs (" ", stream
);
1509 print_full_cxx_name (stream
, jcf
, name_index
,
1510 signature_index
, is_init
, name_override
,
1515 /* Print the unqualified method name followed by the signature. */
1517 print_full_cxx_name (FILE* stream
, JCF
* jcf
, int name_index
,
1518 int signature_index
, int is_init
,
1519 const char *name_override
, int flags
)
1521 int length
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1522 const unsigned char *str0
= JPOOL_UTF_DATA (jcf
, signature_index
);
1523 const unsigned char *str
= str0
;
1524 const unsigned char *limit
= str
+ length
;
1526 int is_method
= str
[0] == '(';
1527 const unsigned char *next
;
1530 fputs (name_override
, stream
);
1531 else if (name_index
)
1533 /* Declare constructors specially. */
1535 print_base_classname (stream
, jcf
, jcf
->this_class
);
1537 print_name (stream
, jcf
, name_index
);
1542 unsigned char *signature
= JPOOL_UTF_DATA (jcf
, signature_index
);
1543 int sig_len
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1544 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf
, name_index
),
1545 JPOOL_UTF_LENGTH (jcf
, name_index
),
1546 (const char *) signature
, sig_len
))
1548 /* If this method is overloaded by another native method,
1549 then include the argument information in the mangled
1551 unsigned char *limit
= signature
+ sig_len
;
1552 fputs ("__", stream
);
1553 while (signature
< limit
)
1555 int ch
= UTF8_GET (signature
, limit
);
1556 jni_print_char (stream
, ch
);
1568 /* Have a method or a constructor. Print signature pieces
1570 fputs (" (", stream
);
1574 /* In JNI mode, add extra arguments. */
1577 /* FIXME: it would be nice to know if we are printing a decl
1578 or a definition, and only print `env' for the latter. */
1579 fputs ("JNIEnv *env", stream
);
1581 fputs ((flags
& ACC_STATIC
) ? ", jclass" : ", jobject", stream
);
1584 fputs (", ", stream
);
1587 while (str
< limit
&& *str
!= ')')
1589 next
= decode_signature_piece (stream
, str
, limit
, &need_space
);
1592 error ("unparseable signature: '%s'", str0
);
1596 if (next
< limit
&& *next
!= ')')
1597 fputs (", ", stream
);
1601 fputs (")", stream
);
1605 /* This is a helper for print_stub_or_jni. */
1607 print_name_for_stub_or_jni (FILE *stream
, JCF
*jcf
, int name_index
,
1608 int signature_index
, int is_init
,
1609 const char *name_override
, int flags
)
1611 const char *const prefix
= flag_jni
? "Java_" : "";
1612 print_cxx_classname (stream
, prefix
, jcf
, jcf
->this_class
, 1);
1613 fputs (flag_jni
? "_" : "::", stream
);
1614 print_full_cxx_name (stream
, jcf
, name_index
,
1615 signature_index
, is_init
, name_override
,
1620 print_stub_or_jni (FILE* stream
, JCF
* jcf
, int name_index
,
1621 int signature_index
, int is_init
,
1622 const char *name_override
, int flags
)
1624 if (JPOOL_TAG (jcf
, signature_index
) != CONSTANT_Utf8
)
1626 fprintf (stream
, "<not a UTF8 constant>");
1631 int length
= JPOOL_UTF_LENGTH (jcf
, signature_index
);
1632 const unsigned char *str0
= JPOOL_UTF_DATA (jcf
, signature_index
);
1633 const unsigned char *str
= str0
;
1634 const unsigned char *limit
= str
+ length
;
1636 int is_method
= str
[0] == '(';
1637 const unsigned char *next
;
1639 /* Don't print fields in the JNI case. */
1640 if (! is_method
&& flag_jni
)
1643 if (flag_jni
&& ! stubs
)
1644 fputs ("JNIEXPORT ", stream
);
1646 /* If printing a method, skip to the return signature and print
1647 that first. However, there is no return value if this is a
1649 if (is_method
&& ! is_init
)
1659 /* If printing a field or an ordinary method, then print the
1660 "return value" now. Note that a constructor can't be native,
1661 so we don't bother checking this in the JNI case. */
1662 if (! is_method
|| ! is_init
)
1664 next
= decode_signature_piece (stream
, str
, limit
, &need_space
);
1667 error ("unparseable signature: '%s'", str0
);
1672 /* When printing a JNI header we need to respect the space. In
1673 other cases we're just going to insert a newline anyway. */
1674 fputs (need_space
&& ! stubs
? " " : "\n", stream
);
1676 if (flag_jni
&& ! stubs
)
1677 fputs ("JNICALL ", stream
);
1679 /* Now print the name of the thing. */
1680 print_name_for_stub_or_jni (stream
, jcf
, name_index
,
1681 signature_index
, is_init
, name_override
,
1684 /* Print the body. */
1688 fputs ("\n{\n (*env)->FatalError (env, \"", stream
);
1690 fputs ("\n{\n throw new ::java::lang::UnsupportedOperationException (JvNewStringLatin1 (\"", stream
);
1691 print_name_for_stub_or_jni (stream
, jcf
, name_index
,
1692 signature_index
, is_init
,
1695 fprintf (stream
, " not implemented\")%s;\n}\n\n",
1696 flag_jni
? "" : ")");
1702 print_mangled_classname (FILE *stream
, JCF
*jcf
, const char *prefix
, int index
)
1704 int name_index
= JPOOL_USHORT1 (jcf
, index
);
1705 fputs (prefix
, stream
);
1706 jcf_print_utf8_replace (out
,
1707 JPOOL_UTF_DATA (jcf
, name_index
),
1708 JPOOL_UTF_LENGTH (jcf
, name_index
),
1712 /* Print PREFIX, then a class name in C++ format. If the name refers
1713 to an array, ignore it and don't print PREFIX. Returns 1 if
1714 something was printed, 0 otherwise. */
1716 print_cxx_classname (FILE *stream
, const char *prefix
,
1717 JCF
*jcf
, int index
, int add_scope
)
1719 int name_index
= JPOOL_USHORT1 (jcf
, index
);
1721 const unsigned char *s
, *p
, *limit
;
1723 s
= JPOOL_UTF_DATA (jcf
, name_index
);
1724 len
= JPOOL_UTF_LENGTH (jcf
, name_index
);
1727 /* Explicitly omit arrays here. */
1729 c
= UTF8_GET (p
, limit
);
1733 fputs (prefix
, stream
);
1735 /* Print a leading "::" so we look in the right namespace. */
1736 if (! flag_jni
&& ! stubs
&& add_scope
)
1737 fputs ("::", stream
);
1741 c
= UTF8_GET (s
, limit
);
1743 fputs (flag_jni
? "_" : "::", stream
);
1745 jni_print_char (stream
, c
);
1751 int written_class_count
= 0;
1753 /* Return name of superclass. If LEN is not NULL, fill it with length
1755 static const unsigned char *
1756 super_class_name (JCF
*derived_jcf
, int *len
)
1758 int supername_index
= JPOOL_USHORT1 (derived_jcf
, derived_jcf
->super_class
);
1759 int supername_length
= JPOOL_UTF_LENGTH (derived_jcf
, supername_index
);
1760 const unsigned char *supername
=
1761 JPOOL_UTF_DATA (derived_jcf
, supername_index
);
1764 *len
= supername_length
;
1770 handle_inner_classes (int count
)
1774 if (out
&& ! flag_jni
&& ! stubs
&& count
> 0)
1775 fprintf (out
, "\n");
1777 for (i
= 0; i
< count
; ++i
)
1779 JCF_u2 inner_info_index
= JCF_readu2 (current_jcf
);
1781 /* There are a few more values here, but we don't care about
1782 them. The (void) cast is apparently the only way to avoid a
1784 (void) JCF_readu2 (current_jcf
);
1785 (void) JCF_readu2 (current_jcf
);
1786 (void) JCF_readu2 (current_jcf
);
1788 if (out
&& ! flag_jni
&& ! stubs
)
1790 print_mangled_classname (out
, current_jcf
, " friend class ",
1792 fprintf (out
, ";\n");
1799 /* We keep track of all the `#include's we generate, so we can avoid
1804 struct include
*next
;
1807 /* List of all includes. */
1808 static struct include
*all_includes
= NULL
;
1810 /* Generate a #include. */
1812 print_include (FILE *out
, const unsigned char *utf8
, int len
)
1814 struct include
*incl
;
1820 len
= strlen ((const char *) utf8
);
1822 for (incl
= all_includes
; incl
; incl
= incl
->next
)
1824 /* We check the length because we might have a proper prefix. */
1825 if (len
== (int) strlen (incl
->name
)
1826 && ! strncmp (incl
->name
, (const char *) utf8
, len
))
1830 incl
= xmalloc (sizeof (struct include
));
1831 incl
->name
= xmalloc (len
+ 1);
1832 strncpy (incl
->name
, (const char *) utf8
, len
);
1833 incl
->name
[len
] = '\0';
1834 incl
->next
= all_includes
;
1835 all_includes
= incl
;
1837 fputs ("#include <", out
);
1838 jcf_print_utf8_replace (out
, utf8
, len
,
1840 flag_jni
? '_' : '/');
1841 fputs (".h>\n", out
);
1846 /* This is used to represent part of a package or class name. */
1849 /* The text of this part of the name. */
1851 /* True if this represents a class. */
1853 /* Linked list of all classes and packages inside this one. */
1854 struct namelet
*subnamelets
;
1855 /* Pointer to next sibling. */
1856 struct namelet
*next
;
1859 static void add_namelet (const unsigned char *, const unsigned char *,
1861 static void print_namelet (FILE *, struct namelet
*, int);
1863 /* The special root namelet. */
1864 static struct namelet root
=
1872 /* This extracts the next name segment from the full UTF-8 encoded
1873 package or class name and links it into the tree. It does this
1876 add_namelet (const unsigned char *name
, const unsigned char *name_limit
,
1877 struct namelet
*parent
)
1879 const unsigned char *p
;
1880 struct namelet
*n
= NULL
, *np
;
1882 /* We want to skip the standard namespaces that we assume the
1883 runtime already knows about. We only do this at the top level,
1884 though, hence the check for `root'. */
1885 if (parent
== &root
)
1887 #define JAVALANG "java/lang/"
1888 #define JAVAIO "java/io/"
1889 #define JAVAUTIL "java/util/"
1890 if ((name_limit
- name
>= (int) sizeof (JAVALANG
) - 1
1891 && ! strncmp ((const char *) name
, JAVALANG
, sizeof (JAVALANG
) - 1))
1892 || (name_limit
- name
>= (int) sizeof (JAVAUTIL
) - 1
1893 && ! strncmp ((const char *) name
, JAVAUTIL
, sizeof (JAVAUTIL
) - 1))
1894 || (name_limit
- name
>= (int) sizeof (JAVAIO
) - 1
1895 && ! strncmp ((const char *) name
, JAVAIO
, sizeof (JAVAIO
) - 1)))
1899 for (p
= name
; p
< name_limit
&& *p
!= '/'; ++p
)
1902 /* Search for this name beneath the PARENT node. */
1903 for (np
= parent
->subnamelets
; np
!= NULL
; np
= np
->next
)
1905 /* We check the length because we might have a proper prefix. */
1906 if ((int) strlen (np
->name
) == p
- name
&&
1907 ! strncmp ((const char *) name
, np
->name
, p
- name
))
1916 n
= xmalloc (sizeof (struct namelet
));
1917 n
->name
= xmalloc (p
- name
+ 1);
1918 strncpy (n
->name
, (const char *) name
, p
- name
);
1919 n
->name
[p
- name
] = '\0';
1920 n
->is_class
= (p
== name_limit
);
1921 n
->subnamelets
= NULL
;
1922 n
->next
= parent
->subnamelets
;
1923 parent
->subnamelets
= n
;
1926 /* We recurse if there is more text, and if the trailing piece does
1927 not represent an inner class. */
1929 add_namelet (p
+ 1, name_limit
, n
);
1932 /* Print a single namelet. Destroys namelets while printing. */
1934 print_namelet (FILE *out
, struct namelet
*name
, int depth
)
1941 for (i
= 0; i
< depth
; ++i
)
1943 fprintf (out
, "%s %s", name
->is_class
? "class" : "namespace",
1945 if (name
->is_class
&& name
->subnamelets
== NULL
)
1951 for (i
= 0; i
< depth
; ++i
)
1957 c
= name
->subnamelets
;
1960 struct namelet
*next
= c
->next
;
1961 print_namelet (out
, c
, depth
+ 2);
1964 name
->subnamelets
= NULL
;
1970 for (i
= 0; i
< depth
; ++i
)
1973 /* Only print a `;' when printing a class. C++ is evil. */
1983 /* This is called to add some classes to the list of classes for which
1984 we need decls. The signature argument can be a function
1987 add_class_decl (FILE *out
, JCF
*jcf
, JCF_u2 signature
)
1989 const unsigned char *s
= JPOOL_UTF_DATA (jcf
, signature
);
1990 int len
= JPOOL_UTF_LENGTH (jcf
, signature
);
1993 for (i
= 0; i
< len
; ++i
)
1997 /* If we see an array, then we include the array header. */
2000 print_include (out
, (const unsigned char *) "gcj/array", -1);
2004 /* We're looking for `L<stuff>;' -- everything else is
2009 for (start
= ++i
; i
< len
&& s
[i
] != ';'; ++i
)
2012 add_namelet (&s
[start
], &s
[i
], &root
);
2016 /* Print declarations for all classes required by this class. Any
2017 class or package in the `java' package is assumed to be handled
2018 statically in libjava; we don't generate declarations for these.
2019 This makes the generated headers a bit easier to read. */
2021 print_class_decls (FILE *out
, JCF
*jcf
, int self
)
2023 /* Make sure to always add the current class to the list of things
2024 that should be declared. */
2025 int name_index
= JPOOL_USHORT1 (jcf
, self
);
2027 const unsigned char *s
;
2029 s
= JPOOL_UTF_DATA (jcf
, name_index
);
2030 len
= JPOOL_UTF_LENGTH (jcf
, name_index
);
2031 add_namelet (s
, s
+ len
, &root
);
2033 if (root
.subnamelets
)
2035 fputs ("extern \"Java\"\n{\n", out
);
2036 /* We use an initial offset of 0 because the root namelet
2037 doesn't cause anything to print. */
2038 print_namelet (out
, &root
, 0);
2039 fputs ("}\n\n", out
);
2046 process_file (JCF
*jcf
, FILE *out
)
2049 uint32 field_start
, method_end
, method_start
;
2055 if (jcf_parse_preamble (jcf
) != 0)
2057 error ("Not a valid Java .class file.");
2061 /* Parse and possibly print constant pool */
2062 code
= jcf_parse_constant_pool (jcf
);
2065 error ("error while parsing constant pool");
2068 code
= verify_constant_pool (jcf
);
2071 error ("error in constant pool entry #%d", code
);
2075 jcf_parse_class (jcf
);
2077 if (written_class_count
++ == 0 && out
)
2079 const char *cstart
, *cstart2
, *mode
, *cend
, *what
, *jflag
;
2094 mode
= " -*- c++ -*-";
2100 fprintf (out
, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
2101 cstart
, mode
, cend
);
2104 fprintf (out
, "%s This file was created by `" TOOLNAME
" -stubs%s'.%s\n\
2106 %s This file is intended to give you a head start on implementing native\n\
2107 %s methods using %s.\n\
2108 %s Be aware: running `" TOOLNAME
" -stubs %s' once more for this class may\n\
2109 %s overwrite any edits you have made to this file.%s\n\n",
2110 cstart
, jflag
, mode
,
2126 print_mangled_classname (out
, jcf
, "#ifndef __", jcf
->this_class
);
2127 fprintf (out
, "__\n");
2129 print_mangled_classname (out
, jcf
, "#define __", jcf
->this_class
);
2130 fprintf (out
, "__\n\n");
2134 fprintf (out
, "#include <jni.h>\n\n");
2135 fprintf (out
, "#ifdef __cplusplus\n");
2136 fprintf (out
, "extern \"C\"\n");
2137 fprintf (out
, "{\n");
2138 fprintf (out
, "#endif\n");
2142 /* We do this to ensure that inline methods won't be
2143 `outlined' by g++. This works as long as method and
2144 fields are not added by the user. */
2145 fprintf (out
, "#pragma interface\n");
2147 if (jcf
->super_class
)
2150 const unsigned char *supername
=
2151 super_class_name (jcf
, &super_length
);
2154 print_include (out
, supername
, super_length
);
2160 /* Strip off the ".class" portion of the name when printing
2161 the include file name. */
2163 int i
, len
= strlen (jcf
->classname
);
2164 if (len
> 6 && ! strcmp (&jcf
->classname
[len
- 6], ".class"))
2166 /* Turn the class name into a file name. */
2167 name
= xmalloc (len
+ 1);
2168 for (i
= 0; i
< len
; ++i
)
2169 name
[i
] = jcf
->classname
[i
] == '.' ? '/' : jcf
->classname
[i
];
2171 print_include (out
, (const unsigned char *) name
, len
);
2176 print_include (out
, (const unsigned char *) "gcj/cni", -1);
2177 print_include (out
, (const unsigned char *) "java/lang/UnsupportedOperationException",
2183 /* We want to parse the methods first. But we need to find where
2184 they start. So first we skip the fields, then parse the methods.
2185 Then we parse the fields and skip the methods. This is ugly, but
2186 not too bad since we need two full passes to get class decl
2187 information anyway. */
2189 field_start
= JCF_TELL (jcf
);
2190 jcf_parse_fields (jcf
);
2192 method_start
= JCF_TELL (jcf
);
2194 jcf_parse_methods (jcf
);
2199 if (out
&& ! flag_jni
)
2202 print_class_decls (out
, jcf
, jcf
->this_class
);
2204 for (i
= 0; i
< prepend_count
; ++i
)
2205 fprintf (out
, "%s\n", prepend_specs
[i
]);
2206 if (prepend_count
> 0)
2211 if (! print_cxx_classname (out
, "class ", jcf
,
2212 jcf
->this_class
, 0))
2214 error ("class is of array type\n");
2217 if (jcf
->super_class
)
2219 if (! print_cxx_classname (out
, " : public ",
2220 jcf
, jcf
->super_class
, 1))
2222 error ("base class is of array type");
2227 fputs ("\n{\n", out
);
2231 /* Now go back for second pass over methods and fields. */
2232 is_first_data_member
= 1;
2234 JCF_SEEK (jcf
, method_start
);
2236 jcf_parse_methods (jcf
);
2237 method_end
= JCF_TELL (jcf
);
2240 JCF_SEEK (jcf
, field_start
);
2241 jcf_parse_fields (jcf
);
2242 JCF_SEEK (jcf
, method_end
);
2244 jcf_parse_final_attributes (jcf
);
2250 fprintf (out
, "\n#ifdef __cplusplus\n");
2251 fprintf (out
, "}\n");
2252 fprintf (out
, "#endif\n");
2256 /* Generate friend decl if we still must. */
2257 for (i
= 0; i
< friend_count
; ++i
)
2258 fprintf (out
, " friend %s\n", friend_specs
[i
]);
2260 /* Generate extra declarations. */
2263 for (i
= 0; i
< add_count
; ++i
)
2264 fprintf (out
, " %s\n", add_specs
[i
]);
2266 /* Generate an entry for the class object. */
2267 generate_access (out
, ACC_PUBLIC
);
2268 fprintf (out
, "\n static ::java::lang::Class class$;\n");
2272 if (jcf
->access_flags
& ACC_INTERFACE
)
2273 fputs (" __attribute__ ((java_interface))", out
);
2277 if (append_count
> 0)
2279 for (i
= 0; i
< append_count
; ++i
)
2280 fprintf (out
, "%s\n", append_specs
[i
]);
2283 print_mangled_classname (out
, jcf
,
2284 "\n#endif /* __", jcf
->this_class
);
2285 fprintf (out
, "__ */\n");
2291 /* This is used to mark options with no short value. */
2292 #define LONG_OPT(Num) ((Num) + 128)
2294 #define OPT_classpath LONG_OPT (0)
2295 #define OPT_CLASSPATH OPT_classpath
2296 #define OPT_bootclasspath LONG_OPT (1)
2297 #define OPT_extdirs LONG_OPT (2)
2298 #define OPT_HELP LONG_OPT (3)
2299 #define OPT_TEMP LONG_OPT (4)
2300 #define OPT_VERSION LONG_OPT (5)
2301 #define OPT_PREPEND LONG_OPT (6)
2302 #define OPT_FRIEND LONG_OPT (7)
2303 #define OPT_ADD LONG_OPT (8)
2304 #define OPT_APPEND LONG_OPT (9)
2305 #define OPT_M LONG_OPT (10)
2306 #define OPT_MM LONG_OPT (11)
2307 #define OPT_MG LONG_OPT (12)
2308 #define OPT_MD LONG_OPT (13)
2309 #define OPT_MMD LONG_OPT (14)
2310 #define OPT_FORCE LONG_OPT (15)
2311 #define OPT_OLD LONG_OPT (16)
2312 #define OPT_TRACE LONG_OPT (17)
2314 static const struct option options
[] =
2316 { "classpath", required_argument
, NULL
, OPT_classpath
},
2317 { "bootclasspath", required_argument
, NULL
, OPT_bootclasspath
},
2318 { "extdirs", required_argument
, NULL
, OPT_extdirs
},
2319 { "CLASSPATH", required_argument
, NULL
, OPT_CLASSPATH
},
2320 { "help", no_argument
, NULL
, OPT_HELP
},
2321 { "stubs", no_argument
, &stubs
, 1 },
2322 { "td", required_argument
, NULL
, OPT_TEMP
},
2323 { "verbose", no_argument
, NULL
, 'v' },
2324 { "version", no_argument
, NULL
, OPT_VERSION
},
2325 { "prepend", required_argument
, NULL
, OPT_PREPEND
},
2326 { "friend", required_argument
, NULL
, OPT_FRIEND
},
2327 { "add", required_argument
, NULL
, OPT_ADD
},
2328 { "append", required_argument
, NULL
, OPT_APPEND
},
2329 { "M", no_argument
, NULL
, OPT_M
},
2330 { "MM", no_argument
, NULL
, OPT_MM
},
2331 { "MG", no_argument
, NULL
, OPT_MG
},
2332 { "MD", no_argument
, NULL
, OPT_MD
},
2333 { "MMD", no_argument
, NULL
, OPT_MMD
},
2334 { "jni", no_argument
, &flag_jni
, 1 },
2335 { "force", no_argument
, NULL
, OPT_FORCE
},
2336 /* If the output file should be named "ld" then a space is needed
2337 between -o and its argument, ld. */
2338 { "old", no_argument
, NULL
, OPT_OLD
},
2339 { "trace", no_argument
, NULL
, OPT_TRACE
},
2340 { NULL
, required_argument
, NULL
, 'J' },
2341 { NULL
, no_argument
, NULL
, 0 }
2347 fprintf (stderr
, _("Try '" TOOLNAME
" --help' for more information.\n"));
2354 printf (_("Usage: " TOOLNAME
" [OPTION]... CLASS...\n\n"));
2355 printf (_("Generate C or C++ header files from .class files\n\n"));
2356 printf (_(" -stubs Generate an implementation stub file\n"));
2357 printf (_(" -jni Generate a JNI header or stub\n"));
2358 printf (_(" -force Always overwrite output files\n"));
2359 printf (_(" -old Unused compatibility option\n"));
2360 printf (_(" -trace Unused compatibility option\n"));
2361 printf (_(" -J OPTION Unused compatibility option\n"));
2363 printf (_(" -add TEXT Insert TEXT into class body\n"));
2364 printf (_(" -append TEXT Insert TEXT after class declaration\n"));
2365 printf (_(" -friend TEXT Insert TEXT as 'friend' declaration\n"));
2366 printf (_(" -prepend TEXT Insert TEXT before start of class\n"));
2368 printf (_(" --classpath PATH Set path to find .class files\n"));
2369 printf (_(" -IDIR Append directory to class path\n"));
2370 printf (_(" --bootclasspath PATH Override built-in class path\n"));
2371 printf (_(" --extdirs PATH Set extensions directory path\n"));
2372 printf (_(" -d DIRECTORY Set output directory name\n"));
2373 printf (_(" -o FILE Set output file name\n"));
2374 printf (_(" -td DIRECTORY Set temporary directory name\n"));
2376 printf (_(" --help Print this help, then exit\n"));
2377 printf (_(" --version Print version number, then exit\n"));
2378 printf (_(" -v, --verbose Print extra information while running\n"));
2380 printf (_(" -M Print all dependencies to stdout;\n"
2381 " suppress ordinary output\n"));
2382 printf (_(" -MM Print non-system dependencies to stdout;\n"
2383 " suppress ordinary output\n"));
2384 printf (_(" -MD Print all dependencies to stdout\n"));
2385 printf (_(" -MMD Print non-system dependencies to stdout\n"));
2386 /* We omit -MG until it is implemented. */
2388 printf (_("For bug reporting instructions, please see:\n"
2389 "%s.\n"), bug_report_url
);
2396 printf (TOOLNAME
" (GCC) %s\n\n", version_string
);
2397 printf ("Copyright %s 2004 Free Software Foundation, Inc.\n", _("(C)"));
2398 printf (_("This is free software; see the source for copying conditions. There is NO\n"
2399 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
2404 main (int argc
, char** argv
)
2408 char *output_file
= NULL
;
2409 int emit_dependencies
= 0, suppress_output
= 0;
2411 int local_found_error
;
2413 /* Unlock the stdio streams. */
2414 unlock_std_streams ();
2416 gcc_init_libintl ();
2420 error ("no classes specified");
2426 /* We use getopt_long_only to allow single `-' long options. For
2427 some of our options this is more natural. */
2428 while ((opt
= getopt_long_only (argc
, argv
, "J:I:d:o:v", options
, NULL
)) != -1)
2433 /* Already handled. */
2437 output_file
= optarg
;
2441 output_directory
= optarg
;
2445 jcf_path_include_arg (optarg
);
2453 jcf_path_classpath_arg (optarg
);
2456 case OPT_bootclasspath
:
2457 jcf_path_bootclasspath_arg (optarg
);
2461 jcf_path_extdirs_arg (optarg
);
2469 temp_directory
= optarg
;
2477 if (prepend_count
== 0)
2478 prepend_specs
= ALLOC (argc
* sizeof (char*));
2479 prepend_specs
[prepend_count
++] = optarg
;
2483 if (friend_count
== 0)
2484 friend_specs
= ALLOC (argc
* sizeof (char*));
2485 friend_specs
[friend_count
++] = optarg
;
2490 add_specs
= ALLOC (argc
* sizeof (char*));
2491 add_specs
[add_count
++] = optarg
;
2495 if (append_count
== 0)
2496 append_specs
= ALLOC (argc
* sizeof (char*));
2497 append_specs
[append_count
++] = optarg
;
2501 emit_dependencies
= 1;
2502 suppress_output
= 1;
2503 jcf_dependency_init (1);
2507 emit_dependencies
= 1;
2508 suppress_output
= 1;
2509 jcf_dependency_init (0);
2513 error ("'-MG' option is unimplemented");
2517 emit_dependencies
= 1;
2518 jcf_dependency_init (1);
2522 emit_dependencies
= 1;
2523 jcf_dependency_init (0);
2536 /* Ignore -J options. */
2547 error ("no classes specified");
2551 jcf_path_seal (verbose
);
2553 if (output_file
&& emit_dependencies
)
2555 error ("can't specify both -o and -MD");
2559 local_found_error
= 0;
2560 for (argi
= optind
; argi
< argc
; argi
++)
2562 char *classname
= argv
[argi
];
2563 char *current_output_file
= NULL
;
2564 const char *classfile_name
;
2566 /* We reset the error state here so that we can detect errors
2567 that occur when processing this file, so the output can be
2568 unlinked if need be. */
2572 printf (_("Processing %s\n"), classname
);
2574 jcf_dependency_reset ();
2575 classfile_name
= find_class (classname
, strlen (classname
), &jcf
, 0);
2576 if (classfile_name
== NULL
)
2578 error ("%s: no such class", classname
);
2582 printf (_("Found in %s\n"), classfile_name
);
2585 if (strcmp (output_file
, "-") == 0)
2587 else if (out
== NULL
)
2589 out
= fopen (output_file
, "w");
2593 perror (output_file
);
2596 current_output_file
= output_file
;
2600 int dir_len
= strlen (output_directory
);
2601 int i
, classname_length
= strlen (classname
);
2602 current_output_file
= ALLOC (dir_len
+ classname_length
+ 5);
2603 strcpy (current_output_file
, output_directory
);
2604 if (dir_len
> 0 && output_directory
[dir_len
-1] != '/')
2605 current_output_file
[dir_len
++] = '/';
2606 for (i
= 0; classname
[i
] != '\0'; i
++)
2608 char ch
= classname
[i
];
2611 if (flag_jni
&& ch
== '/')
2613 current_output_file
[dir_len
++] = ch
;
2615 if (emit_dependencies
)
2617 if (suppress_output
)
2619 jcf_dependency_set_dep_file ("-");
2624 /* We use `.hd' and not `.d' to avoid clashes with
2625 dependency tracking from straight compilation. */
2626 strcpy (current_output_file
+ dir_len
, ".hd");
2627 jcf_dependency_set_dep_file (current_output_file
);
2630 strcpy (current_output_file
+ dir_len
,
2631 stubs
? (flag_jni
? ".c" : ".cc") : ".h");
2632 jcf_dependency_set_target (current_output_file
);
2633 if (! suppress_output
)
2635 out
= fopen (current_output_file
, "w");
2638 perror (current_output_file
);
2643 free_method_name_list ();
2644 process_file (&jcf
, out
);
2647 /* If we found an error and we're writing to a real file,
2649 if (found_error
&& ! suppress_output
&& current_output_file
!= NULL
2650 && strcmp (current_output_file
, "-"))
2651 unlink (current_output_file
);
2653 if (current_output_file
!= output_file
)
2654 free (current_output_file
);
2655 jcf_dependency_write ();
2657 local_found_error
|= found_error
;
2660 if (out
!= NULL
&& out
!= stdout
)
2663 return local_found_error
;