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
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"
39 #include "java-tree.h"
40 #include "java-opcodes.h"
47 /* The output file. */
50 /* Nonzero on failure. */
51 static int found_error = 0;
53 /* Nonzero if we're generating JNI output. */
54 static int flag_jni = 0;
56 /* When nonzero, warn when source file is newer than matching class
60 /* Directory to place resulting files in. Set by -d option. */
61 const char *output_directory = "";
63 /* Directory to place temporary file. Set by -td option. Currently unused. */
64 const char *temp_directory = "/tmp";
66 /* Number of friend functions we have to declare. */
67 static int friend_count;
69 /* A class can optionally have a `friend' function declared. If
70 non-NULL, this is that function. */
71 static char **friend_specs = NULL;
73 /* Number of lines we are prepending before the class. */
74 static int prepend_count;
76 /* We can prepend extra lines before the class's start. */
77 static char **prepend_specs = NULL;
79 /* Number of lines we are appending at the end of the class. */
82 /* We can append extra lines just before the class's end. */
83 static char **add_specs = NULL;
85 /* Number of lines we are appending after the class. */
86 static int append_count;
88 /* We can append extra lines after the class's end. */
89 static char **append_specs = NULL;
95 struct JCF *current_jcf;
97 /* This holds access information for the last field we examined. They
98 let us generate "private:", "public:", and "protected:" properly.
99 If 0 then we haven't previously examined any field. */
100 static JCF_u2 last_access;
102 /* Pass this macro the flags for a class and for a method. It will
103 return true if the method should be considered `final'. */
104 #define METHOD_IS_FINAL(Class, Method) \
105 (((Class) & ACC_FINAL) || ((Method) & (ACC_FINAL | ACC_PRIVATE)))
107 /* Pass this macro the flags for a method. It will return true if the
109 #define METHOD_IS_NATIVE(Method) \
110 ((Method) & ACC_NATIVE)
112 /* We keep a linked list of all method names we have seen. This lets
113 us determine if a method name and a field name are in conflict. */
118 unsigned char *signature;
120 struct method_name *next;
123 /* List of method names we've seen. */
124 static struct method_name *method_name_list;
126 static void print_field_info PARAMS ((FILE*, JCF*, int, int, JCF_u2));
127 static void print_mangled_classname PARAMS ((FILE*, JCF*, const char*, int));
128 static int print_cxx_classname PARAMS ((FILE*, const char*, JCF*, int, int));
129 static void print_method_info PARAMS ((FILE*, JCF*, int, int, JCF_u2));
130 static void print_c_decl PARAMS ((FILE*, JCF*, int, int, int, const char *,
132 static void print_stub_or_jni PARAMS ((FILE*, JCF*, int, int, int,
134 static void print_full_cxx_name PARAMS ((FILE*, JCF*, int, int, int,
136 static void decompile_method PARAMS ((FILE*, JCF*, int));
137 static void add_class_decl PARAMS ((FILE*, JCF*, JCF_u2));
139 static int java_float_finite PARAMS ((jfloat));
140 static int java_double_finite PARAMS ((jdouble));
141 static void print_name PARAMS ((FILE *, JCF *, int));
142 static void print_base_classname PARAMS ((FILE *, JCF *, int));
143 static int utf8_cmp PARAMS ((const unsigned char *, int, const char *));
144 static char *cxx_keyword_subst PARAMS ((const unsigned char *, int));
145 static void generate_access PARAMS ((FILE *, JCF_u2));
146 static int name_is_method_p PARAMS ((const unsigned char *, int));
147 static char *get_field_name PARAMS ((JCF *, int, JCF_u2));
148 static void print_field_name PARAMS ((FILE *, JCF *, int, JCF_u2));
149 static const unsigned char *super_class_name PARAMS ((JCF *, int *));
150 static void print_include PARAMS ((FILE *, const unsigned char *, int));
151 static int gcjh_streq PARAMS ((const void *p1, const void *p2));
152 static int throwable_p PARAMS ((const unsigned char *signature));
153 static const unsigned char *decode_signature_piece
154 PARAMS ((FILE *, const unsigned char *, const unsigned char *, int *));
155 static void print_class_decls PARAMS ((FILE *, JCF *, int));
156 static void usage PARAMS ((void)) ATTRIBUTE_NORETURN;
157 static void help PARAMS ((void)) ATTRIBUTE_NORETURN;
158 static void version PARAMS ((void)) ATTRIBUTE_NORETURN;
159 static int overloaded_jni_method_exists_p PARAMS ((const unsigned char *, int,
161 static void jni_print_char PARAMS ((FILE *, int));
162 static void decompile_return_statement PARAMS ((FILE *, JCF *, int, int, int));
164 JCF_u2 current_field_name;
165 JCF_u2 current_field_value;
166 JCF_u2 current_field_signature;
167 JCF_u2 current_field_flags;
169 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
170 ( current_field_name = (NAME), current_field_signature = (SIGNATURE), \
171 current_field_flags = (ACCESS_FLAGS), current_field_value = 0)
173 /* We pass over fields twice. The first time we just note the types
174 of the fields and then the start of the methods. Then we go back
175 and parse the fields for real. This is ugly. */
176 static int field_pass;
177 /* Likewise we pass over methods twice. The first time we generate
178 class decl information; the second time we generate actual method
180 static int method_pass;
182 #define HANDLE_END_FIELD() \
185 if (out && ! stubs && ! flag_jni) \
186 print_field_info (out, jcf, current_field_name, \
187 current_field_signature, \
188 current_field_flags); \
190 else if (! stubs && ! flag_jni) \
191 add_class_decl (out, jcf, current_field_signature);
193 #define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)
195 static int method_declared = 0;
196 static int method_access = 0;
197 static int method_printed = 0;
198 static int method_synthetic = 0;
199 static int method_signature = 0;
201 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
203 method_synthetic = 0; \
204 method_printed = 0; \
206 method_signature = SIGNATURE; \
207 if (ATTRIBUTE_COUNT) \
208 method_synthetic = peek_attribute (jcf, ATTRIBUTE_COUNT, \
209 (const char *)"Synthetic", 9); \
210 /* If a synthetic methods have been declared, its attribute aren't \
211 worth reading (and triggering side-effects). We skip them an \
212 set ATTRIBUTE_COUNT to zero so that they'll be skipped in \
213 jcf_parse_one_method. */ \
214 if (method_synthetic) \
216 skip_attribute (jcf, ATTRIBUTE_COUNT); \
217 ATTRIBUTE_COUNT = 0; \
219 if (method_pass && !method_synthetic) \
222 print_method_info (out, jcf, NAME, SIGNATURE, \
225 else if (!method_synthetic) \
227 print_method_info (NULL, jcf, NAME, SIGNATURE, \
229 if (! stubs && ! flag_jni) \
230 add_class_decl (out, jcf, SIGNATURE); \
234 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
235 if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
237 static int decompiled = 0;
238 #define HANDLE_END_METHOD() \
239 if (out && method_printed && !method_synthetic) \
240 fputs (decompiled || stubs ? "\n" : ";\n", out);
242 /* We're going to need {peek,skip}_attribute, enable their definition. */
243 #define NEED_PEEK_ATTRIBUTE
244 #define NEED_SKIP_ATTRIBUTE
246 #include "jcf-reader.c"
248 /* Some useful constants. */
249 #define F_NAN_MASK 0x7f800000
250 #if (1 == HOST_FLOAT_WORDS_BIG_ENDIAN) && ! defined (HOST_WORDS_BIG_ENDIAN)
251 #define D_NAN_MASK 0x000000007ff00000LL
253 #define D_NAN_MASK 0x7ff0000000000000LL
256 /* Return 1 if F is not Inf or NaN. */
258 java_float_finite (f)
264 /* We happen to know that F_NAN_MASK will match all NaN values, and
265 also positive and negative infinity. That's why we only need one
266 test here. See The Java Language Specification, section 20.9. */
267 return (u.i & F_NAN_MASK) != F_NAN_MASK;
270 /* Return 1 if D is not Inf or NaN. */
272 java_double_finite (d)
278 /* Now check for all NaNs. */
279 return (u.l & D_NAN_MASK) != D_NAN_MASK;
282 /* Print a character, appropriately mangled for JNI. */
285 jni_print_char (stream, ch)
290 jcf_print_char (stream, ch);
291 else if (ch == '(' || ch == ')')
296 fputs ("_1", stream);
298 fputs ("_2", stream);
300 fputs ("_3", stream);
303 else if (ISALNUM (ch))
307 /* "Unicode" character. */
308 fprintf (stream, "_0%04x", ch);
312 /* Print a name from the class data. If the index does not point to a
313 string, an error results. */
316 print_name (FILE* stream, JCF* jcf, int name_index)
318 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
320 fprintf (stream, "<not a UTF8 constant>");
324 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
325 JPOOL_UTF_LENGTH (jcf, name_index));
328 /* For JNI we must correctly quote each character. */
329 const unsigned char *str = JPOOL_UTF_DATA (jcf, name_index);
330 int length = JPOOL_UTF_LENGTH (jcf, name_index);
331 const unsigned char *limit = str + length;
334 int ch = UTF8_GET (str, limit);
337 fprintf (stream, "\\<invalid>");
340 jni_print_char (stream, ch);
345 /* Print base name of class. The base name is everything after the
349 print_base_classname (stream, jcf, index)
354 int name_index = JPOOL_USHORT1 (jcf, index);
356 const unsigned char *s, *p, *limit;
358 s = JPOOL_UTF_DATA (jcf, name_index);
359 len = JPOOL_UTF_LENGTH (jcf, name_index);
364 int c = UTF8_GET (s, limit);
371 int ch = UTF8_GET (p, limit);
373 fputs ("::", stream);
375 jcf_print_char (stream, ch);
379 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
380 and 1 if STR is "greater" than NAME. */
383 utf8_cmp (str, length, name)
384 const unsigned char *str;
388 const unsigned char *limit = str + length;
391 for (i = 0; name[i]; ++i)
393 int ch = UTF8_GET (str, limit);
398 return str == limit ? 0 : 1;
401 /* This is a sorted list of all C++ keywords. */
403 static const char *const cxx_keywords[] =
512 /* If NAME is the name of a C++ keyword, then return an override name.
513 This is a name that can be used in place of the keyword.
514 Otherwise, return NULL. The return value is malloc()d. */
517 cxx_keyword_subst (str, length)
518 const unsigned char *str;
521 int last = ARRAY_SIZE (cxx_keywords);
523 int mid = (last + first) / 2;
526 for (mid = (last + first) / 2;
528 old = mid, mid = (last + first) / 2)
530 int kwl = strlen (cxx_keywords[mid]);
531 int min_length = kwl > length ? length : kwl;
532 int r = utf8_cmp (str, min_length, cxx_keywords[mid]);
538 /* Skip all trailing `$'. */
539 for (i = min_length; i < length && str[i] == '$'; ++i)
541 /* We've only found a match if all the remaining characters
545 char *dup = xmalloc (2 + length - min_length + kwl);
546 strcpy (dup, cxx_keywords[mid]);
547 for (i = kwl; i < length + 1; ++i)
563 /* Generate an access control keyword based on FLAGS. */
566 generate_access (stream, flags)
570 if ((flags & ACC_VISIBILITY) == last_access)
572 last_access = (flags & ACC_VISIBILITY);
577 fputs ("public: // actually package-private\n", stream);
580 fputs ("public:\n", stream);
583 fputs ("private:\n", stream);
586 fputs ("public: // actually protected\n", stream);
590 fprintf (stream, "#error unrecognized visibility %d\n",
591 (flags & ACC_VISIBILITY));
596 /* See if NAME is already the name of a method. */
598 name_is_method_p (name, length)
599 const unsigned char *name;
602 struct method_name *p;
604 for (p = method_name_list; p != NULL; p = p->next)
606 if (p->length == length && ! memcmp (p->name, name, length))
612 /* If there is already a method named NAME, whose signature is not
613 SIGNATURE, then return true. Otherwise return false. */
615 overloaded_jni_method_exists_p (name, length, signature, sig_length)
616 const unsigned char *name;
618 const char *signature;
621 struct method_name *p;
623 for (p = method_name_list; p != NULL; p = p->next)
625 if (p->length == length
626 && ! memcmp (p->name, name, length)
627 && (p->sig_length != sig_length
628 || memcmp (p->signature, signature, sig_length)))
634 /* Get name of a field. This handles renamings due to C++ clash. */
636 get_field_name (jcf, name_index, flags)
641 unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
642 int length = JPOOL_UTF_LENGTH (jcf, name_index);
645 if (name_is_method_p (name, length))
647 /* This field name matches a method. So override the name with
648 a dummy name. This is yucky, but it isn't clear what else to
649 do. FIXME: if the field is static, then we'll be in real
651 if ((flags & ACC_STATIC))
653 fprintf (stderr, "static field has same name as method\n");
658 override = xmalloc (length + 3);
659 memcpy (override, name, length);
660 strcpy (override + length, "__");
663 override = cxx_keyword_subst (name, length);
668 /* Print a field name. Convenience function for use with
671 print_field_name (stream, jcf, name_index, flags)
677 char *override = get_field_name (jcf, name_index, flags);
681 fputs (override, stream);
685 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
686 JPOOL_UTF_LENGTH (jcf, name_index));
690 print_field_info (FILE *stream, JCF* jcf, int name_index, int sig_index,
693 char *override = NULL;
695 generate_access (stream, flags);
696 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
698 fprintf (stream, "<not a UTF8 constant>");
704 if ((flags & ACC_STATIC))
705 fputs ("static ", out);
707 if ((flags & ACC_FINAL))
709 if (current_field_value > 0)
714 switch (JPOOL_TAG (jcf, current_field_value))
716 case CONSTANT_Integer:
719 int most_negative = 0;
720 fputs ("const jint ", out);
721 print_field_name (out, jcf, name_index, 0);
723 num = JPOOL_INT (jcf, current_field_value);
724 /* We single out the most negative number to print
725 specially. This avoids later warnings from g++. */
726 if (num == (jint) 0x80000000)
731 format_int (buffer, (jlong) num, 10);
732 fprintf (out, "%sL%s;\n", buffer, most_negative ? " - 1" : "");
738 int most_negative = 0;
739 fputs ("const jlong ", out);
740 print_field_name (out, jcf, name_index, 0);
742 num = JPOOL_LONG (jcf, current_field_value);
743 /* We single out the most negative number to print
744 specially.. This avoids later warnings from g++. */
745 if (num == (jlong) 0x8000000000000000LL)
750 format_int (buffer, num, 10);
751 fprintf (out, "%sLL%s;\n", buffer, most_negative ? " - 1" :"");
756 jfloat fnum = JPOOL_FLOAT (jcf, current_field_value);
757 fputs ("const jfloat ", out);
758 print_field_name (out, jcf, name_index, 0);
759 if (! java_float_finite (fnum))
762 fprintf (out, " = %.10g;\n", fnum);
765 case CONSTANT_Double:
767 jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value);
768 fputs ("const jdouble ", out);
769 print_field_name (out, jcf, name_index, 0);
770 if (! java_double_finite (dnum))
773 fprintf (out, " = %.17g;\n", dnum);
777 /* We can't print this as a constant, but we can still
778 print something sensible. */
788 override = get_field_name (jcf, name_index, flags);
789 print_c_decl (out, jcf, name_index, sig_index, 0, override, flags);
798 print_method_info (FILE *stream, JCF* jcf, int name_index, int sig_index,
801 const unsigned char *str;
802 int length, is_init = 0;
803 char *override = NULL;
806 method_access = flags;
807 if (stream && JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
808 fprintf (stream, "<not a UTF8 constant>");
809 str = JPOOL_UTF_DATA (jcf, name_index);
810 length = JPOOL_UTF_LENGTH (jcf, name_index);
814 /* Ignore the internally generated method <clinit>. However,
815 treat <init> as a constructor. */
816 if (! utf8_cmp (str, length, "<init>"))
818 else if (! METHOD_IS_FINAL (jcf->access_flags, flags)
819 && ! (flags & ACC_STATIC))
821 /* FIXME: i18n bug here. Order of prints should not be
823 fprintf (stderr, "ignored method `");
824 jcf_print_utf8 (stderr, str, length);
825 fprintf (stderr, "' marked virtual\n");
833 /* During the first method pass, build a list of method names. This will
834 be used to determine if field names conflict with method names. */
837 struct method_name *nn;
839 nn = xmalloc (sizeof (struct method_name));
840 nn->name = xmalloc (length);
841 memcpy (nn->name, str, length);
843 nn->next = method_name_list;
844 nn->sig_length = JPOOL_UTF_LENGTH (jcf, sig_index);
845 nn->signature = xmalloc (nn->sig_length);
846 memcpy (nn->signature, JPOOL_UTF_DATA (jcf, sig_index),
848 method_name_list = nn;
850 /* The rest of this function doesn't matter. */
854 /* We don't worry about overrides in JNI mode. */
857 /* We can't generate a method whose name is a C++ reserved word.
858 We can't just ignore the function, because that will cause
859 incorrect code to be generated if the function is virtual
860 (not only for calls to this function for for other functions
861 after it in the vtbl). So we give it a dummy name instead. */
862 override = cxx_keyword_subst (str, length);
865 if (! stubs && ! flag_jni)
869 generate_access (stream, flags);
872 if ((flags & ACC_STATIC))
873 fputs ("static ", out);
874 else if (! METHOD_IS_FINAL (jcf->access_flags, flags))
876 /* Don't print `virtual' if we have a constructor. */
878 fputs ("virtual ", out);
880 print_c_decl (out, jcf, name_index, sig_index, is_init, override, flags);
882 if ((flags & ACC_ABSTRACT))
889 if (METHOD_IS_NATIVE (flags))
892 print_stub_or_jni (out, jcf, name_index, sig_index,
893 is_init, override, flags);
901 /* A helper for the decompiler which prints a `return' statement where
902 the type is a reference type. If METHODTYPE and OBJECTTYPE are not
903 identical, we emit a cast. We do this because the C++ compiler
904 doesn't know that a reference can be cast to the type of an
905 interface it implements. METHODTYPE is the index of the method's
906 signature. NAMEINDEX is the index of the field name; -1 for
907 `this'. OBJECTTYPE is the index of the object's type. */
909 decompile_return_statement (out, jcf, methodtype, nameindex, objecttype)
912 int methodtype, nameindex, objecttype;
915 int obj_name_len, method_name_len;
916 const unsigned char *obj_data, *method_data;
918 obj_name_len = JPOOL_UTF_LENGTH (jcf, objecttype);
919 obj_data = JPOOL_UTF_DATA (jcf, objecttype);
921 method_name_len = JPOOL_UTF_LENGTH (jcf, methodtype);
922 method_data = JPOOL_UTF_DATA (jcf, methodtype);
924 /* Skip forward to return type part of method. */
925 while (*method_data != ')')
934 /* If we see an `L', skip it and the trailing `;'. */
935 if (method_data[0] == 'L' && method_data[method_name_len - 1] == ';')
938 method_name_len -= 2;
940 if (obj_data[0] == 'L' && obj_data[obj_name_len - 1] == ';')
946 /* FIXME: if METHODTYPE is a superclass of OBJECTTYPE then we don't
947 need a cast. Right now there is no way to determine if this is
949 if (method_name_len != obj_name_len)
954 for (i = 0; i < method_name_len; ++i)
956 if (method_data[i] != obj_data[i])
964 fputs (" { return ", out);
969 const unsigned char *limit;
971 fputs ("reinterpret_cast<", out);
973 while (*method_data == '[')
978 fputs ("JArray<", out);
981 /* Leading space to avoid C++ digraphs. */
984 /* If we see an `L', skip it and the trailing `;'. Only do this
985 if we've seen an array specification. If we don't have an
986 array then the `L' was stripped earlier. */
987 if (array_depth && method_data[0] == 'L'
988 && method_data[method_name_len - 1] == ';')
991 method_name_len -= 2;
994 limit = method_data + method_name_len;
995 while (method_data < limit)
997 int ch = UTF8_GET (method_data, limit);
1001 jcf_print_char (out, ch);
1005 /* Close each array. */
1006 while (array_depth > 0)
1012 /* Close the cast. */
1016 if (nameindex == -1)
1017 fputs ("this", out);
1019 print_field_name (out, jcf, nameindex, 0);
1028 /* Try to decompile a method body. Right now we just try to handle a
1029 simple case that we can do. Expand as desired. */
1031 decompile_method (out, jcf, code_len)
1036 const unsigned char *codes = jcf->read_ptr;
1038 uint16 name_and_type, name;
1040 /* If the method is synchronized, don't touch it. */
1041 if ((method_access & ACC_SYNCHRONIZED))
1045 && codes[0] == OPCODE_aload_0
1046 && codes[1] == OPCODE_getfield
1047 && (codes[4] == OPCODE_areturn
1048 || codes[4] == OPCODE_dreturn
1049 || codes[4] == OPCODE_freturn
1050 || codes[4] == OPCODE_ireturn
1051 || codes[4] == OPCODE_lreturn))
1053 /* Found code like `return FIELD'. */
1054 index = (codes[2] << 8) | codes[3];
1055 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
1056 name_and_type = JPOOL_USHORT2 (jcf, index);
1057 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
1058 name = JPOOL_USHORT1 (jcf, name_and_type);
1059 if (codes[4] == OPCODE_areturn)
1060 decompile_return_statement (out, jcf, method_signature,
1061 name, JPOOL_USHORT2 (jcf, name_and_type));
1064 fputs (" { return ", out);
1066 print_field_name (out, jcf, name, 0);
1071 else if (code_len == 2
1072 && codes[0] == OPCODE_aload_0
1073 && codes[1] == OPCODE_areturn
1074 /* We're going to generate `return this'. This only makes
1075 sense for non-static methods. */
1076 && ! (method_access & ACC_STATIC))
1078 decompile_return_statement (out, jcf, method_signature, -1,
1079 JPOOL_USHORT1 (jcf, jcf->this_class));
1082 else if (code_len == 1 && codes[0] == OPCODE_return)
1084 /* Found plain `return'. */
1085 fputs (" { }", out);
1088 else if (code_len == 2
1089 && codes[0] == OPCODE_aconst_null
1090 && codes[1] == OPCODE_areturn)
1092 /* Found `return null'. We don't want to depend on NULL being
1094 fputs (" { return 0; }", out);
1099 /* Like strcmp, but invert the return result for the hash table. This
1100 should probably be in hashtab.c to complement the existing string
1104 const void *p1, *p2;
1106 return ! strcmp ((char *) p1, (char *) p2);
1109 /* Return 1 if the initial part of CLNAME names a subclass of throwable,
1110 or 0 if not. CLNAME may be extracted from a signature, and can be
1111 terminated with either `;' or NULL. */
1113 throwable_p (clname)
1114 const unsigned char *clname;
1117 unsigned char *current;
1121 /* We keep two hash tables of class names. In one we list all the
1122 classes which are subclasses of Throwable. In the other we will
1123 all other classes. We keep two tables to make the code a bit
1124 simpler; we don't have to have a structure mapping class name to
1125 a `throwable?' bit. */
1126 static htab_t throw_hash;
1127 static htab_t non_throw_hash;
1128 static int init_done = 0;
1133 const unsigned char *str;
1135 /* Self-initializing. The cost of this really doesn't matter.
1136 We also don't care about freeing these, either. */
1137 throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
1139 non_throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
1142 /* Make sure the root classes show up in the tables. */
1143 str = xstrdup ("java.lang.Throwable");
1144 slot = htab_find_slot (throw_hash, str, INSERT);
1147 str = xstrdup ("java.lang.Object");
1148 slot = htab_find_slot (non_throw_hash, str, INSERT);
1154 for (length = 0; clname[length] != ';' && clname[length] != '\0'; ++length)
1156 current = ALLOC (length);
1157 for (i = 0; i < length; ++i)
1158 current[i] = clname[i] == '/' ? '.' : clname[i];
1159 current[length] = '\0';
1161 /* We don't compute the hash slot here because the table might be
1162 modified by the recursion. In that case the slot could be
1164 if (htab_find (throw_hash, current))
1166 else if (htab_find (non_throw_hash, current))
1172 unsigned char *super, *tmp;
1173 int super_length = -1;
1174 const char *classfile_name = find_class (current, strlen (current),
1177 if (! classfile_name)
1179 fprintf (stderr, "couldn't find class %s\n", current);
1183 if (jcf_parse_preamble (&jcf) != 0
1184 || jcf_parse_constant_pool (&jcf) != 0
1185 || verify_constant_pool (&jcf) > 0)
1187 fprintf (stderr, "parse error while reading %s\n", classfile_name);
1191 jcf_parse_class (&jcf);
1193 tmp = (unsigned char *) super_class_name (&jcf, &super_length);
1194 super = ALLOC (super_length + 1);
1195 memcpy (super, tmp, super_length);
1196 super[super_length] = '\0';
1198 result = throwable_p (super);
1199 slot = htab_find_slot (result ? throw_hash : non_throw_hash,
1210 /* Print one piece of a signature. Returns pointer to next parseable
1211 character on success, NULL on error. */
1212 static const unsigned char *
1213 decode_signature_piece (stream, signature, limit, need_space)
1215 const unsigned char *signature, *limit;
1219 int array_depth = 0;
1221 switch (signature[0])
1224 /* More spaghetti. */
1227 for (signature++; (signature < limit
1228 && ISDIGIT (*signature)); signature++)
1233 ctype = "jbyteArray";
1236 ctype = "jcharArray";
1239 ctype = "jdoubleArray";
1242 ctype = "jfloatArray";
1245 ctype = "jintArray";
1248 ctype = "jshortArray";
1251 ctype = "jlongArray";
1254 ctype = "jbooleanArray";
1257 /* We have a nested array. */
1260 fputs ("JArray<", stream);
1264 /* We have to generate a reference to JArray here, so that
1265 our output matches what the compiler does. */
1267 /* Space between `<' and `:' to avoid C++ digraphs. */
1269 fputs ("JArray< ::", stream);
1270 while (signature < limit && *signature != ';')
1272 int ch = UTF8_GET (signature, limit);
1276 fputs ("::", stream);
1278 jcf_print_char (stream, ch);
1282 fputs (" *> *", stream);
1287 /* Unparseable signature. */
1291 /* If the previous iterations left us with something to print,
1292 print it. For JNI, we always print `jobjectArray' in the
1294 if (flag_jni && (ctype == NULL || array_depth > 0))
1296 ctype = "jobjectArray";
1299 /* The `printit' case will advance SIGNATURE for us. If we
1300 don't go there, we must advance past the `;' ourselves. */
1308 /* This shouldn't happen. */
1311 case 'B': ctype = "jbyte"; goto printit;
1312 case 'C': ctype = "jchar"; goto printit;
1313 case 'D': ctype = "jdouble"; goto printit;
1314 case 'F': ctype = "jfloat"; goto printit;
1315 case 'I': ctype = "jint"; goto printit;
1316 case 'J': ctype = "jlong"; goto printit;
1317 case 'S': ctype = "jshort"; goto printit;
1318 case 'Z': ctype = "jboolean"; goto printit;
1319 case 'V': ctype = "void"; goto printit;
1323 /* We know about certain types and special-case their names. */
1324 if (! strncmp (signature, "Ljava/lang/String;",
1325 sizeof ("Ljava/lang/String;") -1))
1327 else if (! strncmp (signature, "Ljava/lang/Class;",
1328 sizeof ("Ljava/lang/Class;") - 1))
1330 /* Skip leading 'L' for throwable_p call. */
1331 else if (throwable_p (signature + 1))
1332 ctype = "jthrowable";
1336 while (*signature && *signature != ';')
1341 /* Print a leading "::" so we look in the right namespace. */
1342 fputs ("::", stream);
1344 while (*signature && *signature != ';')
1346 int ch = UTF8_GET (signature, limit);
1348 fputs ("::", stream);
1350 jcf_print_char (stream, ch);
1352 fputs (" *", stream);
1353 if (*signature == ';')
1359 jni_print_char (stream, *signature++);
1364 fputs (ctype, stream);
1370 while (array_depth-- > 0)
1371 fputs ("> *", stream);
1378 print_c_decl (FILE* stream, JCF* jcf, int name_index, int signature_index,
1379 int is_init, const char *name_override, int flags)
1381 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1383 fprintf (stream, "<not a UTF8 constant>");
1388 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1389 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1390 register const unsigned char *str = str0;
1391 const unsigned char *limit = str + length;
1393 int is_method = str[0] == '(';
1394 const unsigned char *next;
1396 /* If printing a method, skip to the return signature and print
1397 that first. However, there is no return value if this is a
1399 if (is_method && ! is_init)
1409 /* If printing a field or an ordinary method, then print the
1410 "return value" now. */
1411 if (! is_method || ! is_init)
1413 next = decode_signature_piece (stream, str, limit, &need_space);
1416 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1422 /* Now print the name of the thing. */
1424 fputs (" ", stream);
1425 print_full_cxx_name (stream, jcf, name_index,
1426 signature_index, is_init, name_override,
1431 /* Print the unqualified method name followed by the signature. */
1433 print_full_cxx_name (FILE* stream, JCF* jcf, int name_index,
1434 int signature_index, int is_init,
1435 const char *name_override, int flags)
1437 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1438 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1439 register const unsigned char *str = str0;
1440 const unsigned char *limit = str + length;
1442 int is_method = str[0] == '(';
1443 const unsigned char *next;
1446 fputs (name_override, stream);
1447 else if (name_index)
1449 /* Declare constructors specially. */
1451 print_base_classname (stream, jcf, jcf->this_class);
1453 print_name (stream, jcf, name_index);
1458 unsigned char *signature = JPOOL_UTF_DATA (jcf, signature_index);
1459 int sig_len = JPOOL_UTF_LENGTH (jcf, signature_index);
1460 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf, name_index),
1461 JPOOL_UTF_LENGTH (jcf, name_index),
1462 signature, sig_len))
1464 /* If this method is overloaded by another native method,
1465 then include the argument information in the mangled
1467 unsigned char *limit = signature + sig_len;
1468 fputs ("__", stream);
1469 while (signature < limit)
1471 int ch = UTF8_GET (signature, limit);
1472 jni_print_char (stream, ch);
1484 /* Have a method or a constructor. Print signature pieces
1486 fputs (" (", stream);
1490 /* In JNI mode, add extra arguments. */
1493 /* FIXME: it would be nice to know if we are printing a decl
1494 or a definition, and only print `env' for the latter. */
1495 fputs ("JNIEnv *env", stream);
1497 fputs ((flags & ACC_STATIC) ? ", jclass" : ", jobject", stream);
1500 fputs (", ", stream);
1503 while (str < limit && *str != ')')
1505 next = decode_signature_piece (stream, str, limit, &need_space);
1508 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1513 if (next < limit && *next != ')')
1514 fputs (", ", stream);
1518 fputs (")", stream);
1522 /* This is a helper for print_stub_or_jni. */
1524 print_name_for_stub_or_jni (FILE *stream, JCF *jcf, int name_index,
1525 int signature_index, int is_init,
1526 const char *name_override, int flags)
1528 const char *const prefix = flag_jni ? "Java_" : "";
1529 print_cxx_classname (stream, prefix, jcf, jcf->this_class, 1);
1530 fputs (flag_jni ? "_" : "::", stream);
1531 print_full_cxx_name (stream, jcf, name_index,
1532 signature_index, is_init, name_override,
1537 print_stub_or_jni (FILE* stream, JCF* jcf, int name_index,
1538 int signature_index, int is_init,
1539 const char *name_override, int flags)
1541 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1543 fprintf (stream, "<not a UTF8 constant>");
1548 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1549 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1550 register const unsigned char *str = str0;
1551 const unsigned char *limit = str + length;
1553 int is_method = str[0] == '(';
1554 const unsigned char *next;
1556 /* Don't print fields in the JNI case. */
1557 if (! is_method && flag_jni)
1560 if (flag_jni && ! stubs)
1561 fputs ("extern JNIEXPORT ", stream);
1563 /* If printing a method, skip to the return signature and print
1564 that first. However, there is no return value if this is a
1566 if (is_method && ! is_init)
1576 /* If printing a field or an ordinary method, then print the
1577 "return value" now. Note that a constructor can't be native,
1578 so we don't bother checking this in the JNI case. */
1579 if (! is_method || ! is_init)
1581 next = decode_signature_piece (stream, str, limit, &need_space);
1584 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1590 /* When printing a JNI header we need to respect the space. In
1591 other cases we're just going to insert a newline anyway. */
1592 fputs (need_space && ! stubs ? " " : "\n", stream);
1594 if (flag_jni && ! stubs)
1595 fputs ("JNICALL ", stream);
1597 /* Now print the name of the thing. */
1598 print_name_for_stub_or_jni (stream, jcf, name_index,
1599 signature_index, is_init, name_override,
1602 /* Print the body. */
1606 fputs ("\n{\n (*env)->FatalError (\"", stream);
1608 fputs ("\n{\n throw new ::java::lang::UnsupportedOperationException (JvNewStringLatin1 (\"", stream);
1609 print_name_for_stub_or_jni (stream, jcf, name_index,
1610 signature_index, is_init,
1613 fprintf (stream, " not implemented\")%s;\n}\n\n",
1614 flag_jni ? "" : ")");
1620 print_mangled_classname (FILE *stream, JCF *jcf, const char *prefix, int index)
1622 int name_index = JPOOL_USHORT1 (jcf, index);
1623 fputs (prefix, stream);
1624 jcf_print_utf8_replace (out,
1625 JPOOL_UTF_DATA (jcf, name_index),
1626 JPOOL_UTF_LENGTH (jcf, name_index),
1630 /* Print PREFIX, then a class name in C++ format. If the name refers
1631 to an array, ignore it and don't print PREFIX. Returns 1 if
1632 something was printed, 0 otherwise. */
1634 print_cxx_classname (stream, prefix, jcf, index, add_scope)
1641 int name_index = JPOOL_USHORT1 (jcf, index);
1643 const unsigned char *s, *p, *limit;
1645 s = JPOOL_UTF_DATA (jcf, name_index);
1646 len = JPOOL_UTF_LENGTH (jcf, name_index);
1649 /* Explicitly omit arrays here. */
1651 c = UTF8_GET (p, limit);
1655 fputs (prefix, stream);
1657 /* Print a leading "::" so we look in the right namespace. */
1658 if (! flag_jni && ! stubs && add_scope)
1659 fputs ("::", stream);
1663 c = UTF8_GET (s, limit);
1665 fputs (flag_jni ? "_" : "::", stream);
1667 jni_print_char (stream, c);
1673 int written_class_count = 0;
1675 /* Return name of superclass. If LEN is not NULL, fill it with length
1677 static const unsigned char *
1678 super_class_name (derived_jcf, len)
1682 int supername_index = JPOOL_USHORT1 (derived_jcf, derived_jcf->super_class);
1683 int supername_length = JPOOL_UTF_LENGTH (derived_jcf, supername_index);
1684 const unsigned char *supername =
1685 JPOOL_UTF_DATA (derived_jcf, supername_index);
1688 *len = supername_length;
1695 /* We keep track of all the `#include's we generate, so we can avoid
1700 struct include *next;
1703 /* List of all includes. */
1704 static struct include *all_includes = NULL;
1706 /* Generate a #include. */
1708 print_include (out, utf8, len)
1710 const unsigned char *utf8;
1713 struct include *incl;
1719 len = strlen (utf8);
1721 for (incl = all_includes; incl; incl = incl->next)
1723 /* We check the length because we might have a proper prefix. */
1724 if (len == (int) strlen (incl->name)
1725 && ! strncmp (incl->name, utf8, len))
1729 incl = xmalloc (sizeof (struct include));
1730 incl->name = xmalloc (len + 1);
1731 strncpy (incl->name, utf8, len);
1732 incl->name[len] = '\0';
1733 incl->next = all_includes;
1734 all_includes = incl;
1736 fputs ("#include <", out);
1737 jcf_print_utf8_replace (out, utf8, len,
1739 flag_jni ? '_' : '/');
1740 fputs (".h>\n", out);
1745 /* This is used to represent part of a package or class name. */
1748 /* The text of this part of the name. */
1750 /* True if this represents a class. */
1752 /* Linked list of all classes and packages inside this one. */
1753 struct namelet *subnamelets;
1754 /* Pointer to next sibling. */
1755 struct namelet *next;
1758 static void add_namelet PARAMS ((const unsigned char *,
1759 const unsigned char *, struct namelet *));
1760 static void print_namelet PARAMS ((FILE *, struct namelet *, int));
1762 /* The special root namelet. */
1763 static struct namelet root =
1771 /* This extracts the next name segment from the full UTF-8 encoded
1772 package or class name and links it into the tree. It does this
1775 add_namelet (name, name_limit, parent)
1776 const unsigned char *name, *name_limit;
1777 struct namelet *parent;
1779 const unsigned char *p;
1780 struct namelet *n = NULL, *np;
1782 /* We want to skip the standard namespaces that we assume the
1783 runtime already knows about. We only do this at the top level,
1784 though, hence the check for `root'. */
1785 if (parent == &root)
1787 #define JAVALANG "java/lang/"
1788 #define JAVAIO "java/io/"
1789 #define JAVAUTIL "java/util/"
1790 if ((name_limit - name >= (int) sizeof (JAVALANG) - 1
1791 && ! strncmp (name, JAVALANG, sizeof (JAVALANG) - 1))
1792 || (name_limit - name >= (int) sizeof (JAVAUTIL) - 1
1793 && ! strncmp (name, JAVAUTIL, sizeof (JAVAUTIL) - 1))
1794 || (name_limit - name >= (int) sizeof (JAVAIO) - 1
1795 && ! strncmp (name, JAVAIO, sizeof (JAVAIO) - 1)))
1799 for (p = name; p < name_limit && *p != '/'; ++p)
1802 /* Search for this name beneath the PARENT node. */
1803 for (np = parent->subnamelets; np != NULL; np = np->next)
1805 /* We check the length because we might have a proper prefix. */
1806 if ((int) strlen (np->name) == p - name &&
1807 ! strncmp (name, np->name, p - name))
1816 n = xmalloc (sizeof (struct namelet));
1817 n->name = xmalloc (p - name + 1);
1818 strncpy (n->name, name, p - name);
1819 n->name[p - name] = '\0';
1820 n->is_class = (p == name_limit);
1821 n->subnamelets = NULL;
1822 n->next = parent->subnamelets;
1823 parent->subnamelets = n;
1826 /* We recurse if there is more text, and if the trailing piece does
1827 not represent an inner class. */
1829 add_namelet (p + 1, name_limit, n);
1832 /* Print a single namelet. Destroys namelets while printing. */
1834 print_namelet (out, name, depth)
1836 struct namelet *name;
1844 for (i = 0; i < depth; ++i)
1846 fprintf (out, "%s %s", name->is_class ? "class" : "namespace",
1848 if (name->is_class && name->subnamelets == NULL)
1854 for (i = 0; i < depth; ++i)
1860 c = name->subnamelets;
1863 struct namelet *next = c->next;
1864 print_namelet (out, c, depth + 2);
1867 name->subnamelets = NULL;
1873 for (i = 0; i < depth; ++i)
1876 /* Only print a `;' when printing a class. C++ is evil. */
1886 /* This is called to add some classes to the list of classes for which
1887 we need decls. The signature argument can be a function
1890 add_class_decl (out, jcf, signature)
1895 const unsigned char *s = JPOOL_UTF_DATA (jcf, signature);
1896 int len = JPOOL_UTF_LENGTH (jcf, signature);
1899 for (i = 0; i < len; ++i)
1903 /* If we see an array, then we include the array header. */
1906 print_include (out, "gcj/array", -1);
1910 /* We're looking for `L<stuff>;' -- everything else is
1915 for (start = ++i; i < len && s[i] != ';'; ++i)
1918 add_namelet (&s[start], &s[i], &root);
1922 /* Print declarations for all classes required by this class. Any
1923 class or package in the `java' package is assumed to be handled
1924 statically in libjava; we don't generate declarations for these.
1925 This makes the generated headers a bit easier to read. */
1927 print_class_decls (out, jcf, self)
1932 /* Make sure to always add the current class to the list of things
1933 that should be declared. */
1934 int name_index = JPOOL_USHORT1 (jcf, self);
1936 const unsigned char *s;
1938 s = JPOOL_UTF_DATA (jcf, name_index);
1939 len = JPOOL_UTF_LENGTH (jcf, name_index);
1940 add_namelet (s, s + len, &root);
1942 if (root.subnamelets)
1944 fputs ("extern \"Java\"\n{\n", out);
1945 /* We use an initial offset of 0 because the root namelet
1946 doesn't cause anything to print. */
1947 print_namelet (out, &root, 0);
1948 fputs ("}\n\n", out);
1955 process_file (JCF *jcf, FILE *out)
1958 uint32 field_start, method_end, method_start;
1964 if (jcf_parse_preamble (jcf) != 0)
1966 fprintf (stderr, "Not a valid Java .class file.\n");
1971 /* Parse and possibly print constant pool */
1972 code = jcf_parse_constant_pool (jcf);
1975 fprintf (stderr, "error while parsing constant pool\n");
1979 code = verify_constant_pool (jcf);
1982 fprintf (stderr, "error in constant pool entry #%d\n", code);
1987 jcf_parse_class (jcf);
1989 if (written_class_count++ == 0 && out)
1991 const char *cstart, *cstart2, *mode, *cend, *what, *jflag;
2006 mode = " -*- c++ -*-";
2012 fprintf (out, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
2013 cstart, mode, cend);
2016 fprintf (out, "%s This file was created by `gcjh -stubs%s'.%s\n\
2018 %s This file is intended to give you a head start on implementing native\n\
2019 %s methods using %s.\n\
2020 %s Be aware: running `gcjh -stubs %s' once more for this class may\n\
2021 %s overwrite any edits you have made to this file.%s\n\n",
2022 cstart, jflag, mode,
2038 print_mangled_classname (out, jcf, "#ifndef __", jcf->this_class);
2039 fprintf (out, "__\n");
2041 print_mangled_classname (out, jcf, "#define __", jcf->this_class);
2042 fprintf (out, "__\n\n");
2046 fprintf (out, "#include <jni.h>\n\n");
2047 fprintf (out, "#ifdef __cplusplus\n");
2048 fprintf (out, "extern \"C\"\n");
2049 fprintf (out, "{\n");
2050 fprintf (out, "#endif\n");
2054 /* We do this to ensure that inline methods won't be
2055 `outlined' by g++. This works as long as method and
2056 fields are not added by the user. */
2057 fprintf (out, "#pragma interface\n");
2059 if (jcf->super_class)
2062 const unsigned char *supername =
2063 super_class_name (jcf, &super_length);
2066 print_include (out, supername, super_length);
2072 /* Strip off the ".class" portion of the name when printing
2073 the include file name. */
2075 int i, len = strlen (jcf->classname);
2076 if (len > 6 && ! strcmp (&jcf->classname[len - 6], ".class"))
2078 /* Turn the class name into a file name. */
2079 name = xmalloc (len + 1);
2080 for (i = 0; i < len; ++i)
2081 name[i] = jcf->classname[i] == '.' ? '/' : jcf->classname[i];
2083 print_include (out, name, len);
2088 print_include (out, "gcj/cni", -1);
2089 print_include (out, "java/lang/UnsupportedOperationException",
2095 /* We want to parse the methods first. But we need to find where
2096 they start. So first we skip the fields, then parse the methods.
2097 Then we parse the fields and skip the methods. This is ugly, but
2098 not too bad since we need two full passes to get class decl
2099 information anyway. */
2101 field_start = JCF_TELL (jcf);
2102 jcf_parse_fields (jcf);
2104 method_start = JCF_TELL (jcf);
2106 jcf_parse_methods (jcf);
2111 if (out && ! flag_jni)
2114 print_class_decls (out, jcf, jcf->this_class);
2116 for (i = 0; i < prepend_count; ++i)
2117 fprintf (out, "%s\n", prepend_specs[i]);
2118 if (prepend_count > 0)
2123 if (! print_cxx_classname (out, "class ", jcf,
2124 jcf->this_class, 0))
2126 fprintf (stderr, "class is of array type\n");
2130 if (jcf->super_class)
2132 if (! print_cxx_classname (out, " : public ",
2133 jcf, jcf->super_class, 1))
2135 fprintf (stderr, "base class is of array type\n");
2141 fputs ("\n{\n", out);
2145 /* Now go back for second pass over methods and fields. */
2146 JCF_SEEK (jcf, method_start);
2148 jcf_parse_methods (jcf);
2149 method_end = JCF_TELL (jcf);
2152 JCF_SEEK (jcf, field_start);
2153 jcf_parse_fields (jcf);
2154 JCF_SEEK (jcf, method_end);
2156 jcf_parse_final_attributes (jcf);
2162 fprintf (out, "\n#ifdef __cplusplus\n");
2163 fprintf (out, "}\n");
2164 fprintf (out, "#endif\n");
2168 /* Generate friend decl if we still must. */
2169 for (i = 0; i < friend_count; ++i)
2170 fprintf (out, " friend %s\n", friend_specs[i]);
2172 /* Generate extra declarations. */
2175 for (i = 0; i < add_count; ++i)
2176 fprintf (out, " %s\n", add_specs[i]);
2178 /* Generate an entry for the class object. */
2179 generate_access (out, ACC_PUBLIC);
2180 fprintf (out, "\n static ::java::lang::Class class$;\n");
2184 if (jcf->access_flags & ACC_INTERFACE)
2185 fputs (" __attribute__ ((java_interface))", out);
2189 if (append_count > 0)
2191 for (i = 0; i < append_count; ++i)
2192 fprintf (out, "%s\n", append_specs[i]);
2195 print_mangled_classname (out, jcf,
2196 "\n#endif /* __", jcf->this_class);
2197 fprintf (out, "__ */\n");
2203 /* This is used to mark options with no short value. */
2204 #define LONG_OPT(Num) ((Num) + 128)
2206 #define OPT_classpath LONG_OPT (0)
2207 #define OPT_CLASSPATH OPT_classpath
2208 #define OPT_bootclasspath LONG_OPT (1)
2209 #define OPT_extdirs LONG_OPT (2)
2210 #define OPT_HELP LONG_OPT (3)
2211 #define OPT_TEMP LONG_OPT (4)
2212 #define OPT_VERSION LONG_OPT (5)
2213 #define OPT_PREPEND LONG_OPT (6)
2214 #define OPT_FRIEND LONG_OPT (7)
2215 #define OPT_ADD LONG_OPT (8)
2216 #define OPT_APPEND LONG_OPT (9)
2217 #define OPT_M LONG_OPT (10)
2218 #define OPT_MM LONG_OPT (11)
2219 #define OPT_MG LONG_OPT (12)
2220 #define OPT_MD LONG_OPT (13)
2221 #define OPT_MMD LONG_OPT (14)
2223 static const struct option options[] =
2225 { "classpath", required_argument, NULL, OPT_classpath },
2226 { "bootclasspath", required_argument, NULL, OPT_bootclasspath },
2227 { "extdirs", required_argument, NULL, OPT_extdirs },
2228 { "CLASSPATH", required_argument, NULL, OPT_CLASSPATH },
2229 { "help", no_argument, NULL, OPT_HELP },
2230 { "stubs", no_argument, &stubs, 1 },
2231 { "td", required_argument, NULL, OPT_TEMP },
2232 { "verbose", no_argument, NULL, 'v' },
2233 { "version", no_argument, NULL, OPT_VERSION },
2234 { "prepend", required_argument, NULL, OPT_PREPEND },
2235 { "friend", required_argument, NULL, OPT_FRIEND },
2236 { "add", required_argument, NULL, OPT_ADD },
2237 { "append", required_argument, NULL, OPT_APPEND },
2238 { "M", no_argument, NULL, OPT_M },
2239 { "MM", no_argument, NULL, OPT_MM },
2240 { "MG", no_argument, NULL, OPT_MG },
2241 { "MD", no_argument, NULL, OPT_MD },
2242 { "MMD", no_argument, NULL, OPT_MMD },
2243 { "jni", no_argument, &flag_jni, 1 },
2244 { NULL, no_argument, NULL, 0 }
2250 fprintf (stderr, "Try `gcjh --help' for more information.\n");
2257 printf ("Usage: gcjh [OPTION]... CLASS...\n\n");
2258 printf ("Generate C++ header files from .class files\n\n");
2259 printf (" -stubs Generate an implementation stub file\n");
2260 printf (" -jni Generate a JNI header or stub\n");
2262 printf (" -add TEXT Insert TEXT into class body\n");
2263 printf (" -append TEXT Insert TEXT after class declaration\n");
2264 printf (" -friend TEXT Insert TEXT as `friend' declaration\n");
2265 printf (" -prepend TEXT Insert TEXT before start of class\n");
2267 printf (" --classpath PATH Set path to find .class files\n");
2268 printf (" -IDIR Append directory to class path\n");
2269 printf (" --bootclasspath PATH Override built-in class path\n");
2270 printf (" --extdirs PATH Set extensions directory path\n");
2271 printf (" -d DIRECTORY Set output directory name\n");
2272 printf (" -o FILE Set output file name\n");
2273 printf (" -td DIRECTORY Set temporary directory name\n");
2275 printf (" --help Print this help, then exit\n");
2276 printf (" --version Print version number, then exit\n");
2277 printf (" -v, --verbose Print extra information while running\n");
2279 printf (" -M Print all dependencies to stdout;\n");
2280 printf (" suppress ordinary output\n");
2281 printf (" -MM Print non-system dependencies to stdout;\n");
2282 printf (" suppress ordinary output\n");
2283 printf (" -MD Print all dependencies to stdout\n");
2284 printf (" -MMD Print non-system dependencies to stdout\n");
2285 /* We omit -MG until it is implemented. */
2287 printf ("For bug reporting instructions, please see:\n");
2288 printf ("%s.\n", bug_report_url);
2295 printf ("gcjh (GCC) %s\n\n", version_string);
2296 printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
2297 printf ("This is free software; see the source for copying conditions. There is NO\n");
2298 printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
2303 main (int argc, char** argv)
2307 char *output_file = NULL;
2308 int emit_dependencies = 0, suppress_output = 0;
2313 fprintf (stderr, "gcjh: no classes specified\n");
2319 /* We use getopt_long_only to allow single `-' long options. For
2320 some of our options this is more natural. */
2321 while ((opt = getopt_long_only (argc, argv, "I:d:o:v", options, NULL)) != -1)
2326 /* Already handled. */
2330 output_file = optarg;
2334 output_directory = optarg;
2338 jcf_path_include_arg (optarg);
2346 jcf_path_classpath_arg (optarg);
2349 case OPT_bootclasspath:
2350 jcf_path_bootclasspath_arg (optarg);
2354 jcf_path_extdirs_arg (optarg);
2362 temp_directory = optarg;
2370 if (prepend_count == 0)
2371 prepend_specs = ALLOC (argc * sizeof (char*));
2372 prepend_specs[prepend_count++] = optarg;
2376 if (friend_count == 0)
2377 friend_specs = ALLOC (argc * sizeof (char*));
2378 friend_specs[friend_count++] = optarg;
2383 add_specs = ALLOC (argc * sizeof (char*));
2384 add_specs[add_count++] = optarg;
2388 if (append_count == 0)
2389 append_specs = ALLOC (argc * sizeof (char*));
2390 append_specs[append_count++] = optarg;
2394 emit_dependencies = 1;
2395 suppress_output = 1;
2396 jcf_dependency_init (1);
2400 emit_dependencies = 1;
2401 suppress_output = 1;
2402 jcf_dependency_init (0);
2406 fprintf (stderr, "gcjh: `-MG' option is unimplemented\n");
2410 emit_dependencies = 1;
2411 jcf_dependency_init (1);
2415 emit_dependencies = 1;
2416 jcf_dependency_init (0);
2427 fprintf (stderr, "gcjh: no classes specified\n");
2431 jcf_path_seal (verbose);
2433 if (output_file && emit_dependencies)
2435 fprintf (stderr, "gcjh: can't specify both -o and -MD\n");
2439 for (argi = optind; argi < argc; argi++)
2441 char *classname = argv[argi];
2442 char *current_output_file;
2443 const char *classfile_name;
2446 fprintf (stderr, "Processing %s\n", classname);
2448 jcf_dependency_reset ();
2449 classfile_name = find_class (classname, strlen (classname), &jcf, 0);
2450 if (classfile_name == NULL)
2452 fprintf (stderr, "%s: no such class\n", classname);
2456 fprintf (stderr, "Found in %s\n", classfile_name);
2459 if (strcmp (output_file, "-") == 0)
2461 else if (out == NULL)
2463 out = fopen (output_file, "w");
2467 perror (output_file);
2470 current_output_file = output_file;
2474 int dir_len = strlen (output_directory);
2475 int i, classname_length = strlen (classname);
2476 current_output_file = ALLOC (dir_len + classname_length + 5);
2477 strcpy (current_output_file, output_directory);
2478 if (dir_len > 0 && output_directory[dir_len-1] != '/')
2479 current_output_file[dir_len++] = '/';
2480 for (i = 0; classname[i] != '\0'; i++)
2482 char ch = classname[i];
2485 if (flag_jni && ch == '/')
2487 current_output_file[dir_len++] = ch;
2489 if (emit_dependencies)
2491 if (suppress_output)
2493 jcf_dependency_set_dep_file ("-");
2498 /* We use `.hd' and not `.d' to avoid clashes with
2499 dependency tracking from straight compilation. */
2500 strcpy (current_output_file + dir_len, ".hd");
2501 jcf_dependency_set_dep_file (current_output_file);
2504 strcpy (current_output_file + dir_len,
2505 stubs ? (flag_jni ? ".c" : ".cc") : ".h");
2506 jcf_dependency_set_target (current_output_file);
2507 if (! suppress_output)
2509 out = fopen (current_output_file, "w");
2512 perror (current_output_file);
2517 process_file (&jcf, out);
2519 if (current_output_file != output_file)
2520 free (current_output_file);
2521 jcf_dependency_write ();
2524 if (out != NULL && out != stdout)