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 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
34 #include "java-tree.h"
35 #include "java-opcodes.h"
41 /* The output file. */
44 /* Nonzero on failure. */
45 static int found_error = 0;
47 /* Nonzero if we're generating JNI output. */
48 static int flag_jni = 0;
50 /* Directory to place resulting files in. Set by -d option. */
51 const char *output_directory = "";
53 /* Directory to place temporary file. Set by -td option. Currently unused. */
54 const char *temp_directory = "/tmp";
56 /* Number of friend functions we have to declare. */
57 static int friend_count;
59 /* A class can optionally have a `friend' function declared. If
60 non-NULL, this is that function. */
61 static char **friend_specs = NULL;
63 /* Number of lines we are prepending before the class. */
64 static int prepend_count;
66 /* We can prepend extra lines before the class's start. */
67 static char **prepend_specs = NULL;
69 /* Number of lines we are appending at the end of the class. */
72 /* We can append extra lines just before the class's end. */
73 static char **add_specs = NULL;
75 /* Number of lines we are appending after the class. */
76 static int append_count;
78 /* We can append extra lines after the class's end. */
79 static char **append_specs = NULL;
85 struct JCF *current_jcf;
87 /* This holds access information for the last field we examined. They
88 let us generate "private:", "public:", and "protected:" properly.
89 If 0 then we haven't previously examined any field. */
90 static JCF_u2 last_access;
92 /* Pass this macro the flags for a class and for a method. It will
93 return true if the method should be considered `final'. */
94 #define METHOD_IS_FINAL(Class, Method) \
95 (((Class) & ACC_FINAL) || ((Method) & (ACC_FINAL | ACC_PRIVATE)))
97 /* Pass this macro the flags for a method. It will return true if the
99 #define METHOD_IS_NATIVE(Method) \
100 ((Method) & ACC_NATIVE)
102 /* We keep a linked list of all method names we have seen. This lets
103 us determine if a method name and a field name are in conflict. */
108 unsigned char *signature;
110 struct method_name *next;
113 /* List of method names we've seen. */
114 static struct method_name *method_name_list;
116 static void print_field_info PARAMS ((FILE*, JCF*, int, int, JCF_u2));
117 static void print_mangled_classname PARAMS ((FILE*, JCF*, const char*, int));
118 static int print_cxx_classname PARAMS ((FILE*, const char*, JCF*, int));
119 static void print_method_info PARAMS ((FILE*, JCF*, int, int, JCF_u2));
120 static void print_c_decl PARAMS ((FILE*, JCF*, int, int, int, const char *,
122 static void print_stub_or_jni PARAMS ((FILE*, JCF*, int, int, int,
124 static void print_full_cxx_name PARAMS ((FILE*, JCF*, int, int, int,
126 static void decompile_method PARAMS ((FILE*, JCF*, int));
127 static void add_class_decl PARAMS ((FILE*, JCF*, JCF_u2));
129 static int java_float_finite PARAMS ((jfloat));
130 static int java_double_finite PARAMS ((jdouble));
131 static void print_name PARAMS ((FILE *, JCF *, int));
132 static void print_base_classname PARAMS ((FILE *, JCF *, int));
133 static int utf8_cmp PARAMS ((const unsigned char *, int, const char *));
134 static char *cxx_keyword_subst PARAMS ((const unsigned char *, int));
135 static void generate_access PARAMS ((FILE *, JCF_u2));
136 static int name_is_method_p PARAMS ((const unsigned char *, int));
137 static char *get_field_name PARAMS ((JCF *, int, JCF_u2));
138 static void print_field_name PARAMS ((FILE *, JCF *, int, JCF_u2));
139 static const unsigned char *super_class_name PARAMS ((JCF *, int *));
140 static void print_include PARAMS ((FILE *, const unsigned char *, int));
141 static const unsigned char *decode_signature_piece
142 PARAMS ((FILE *, const unsigned char *, const unsigned char *, int *));
143 static void print_class_decls PARAMS ((FILE *, JCF *, int));
144 static void usage PARAMS ((void)) ATTRIBUTE_NORETURN;
145 static void help PARAMS ((void)) ATTRIBUTE_NORETURN;
146 static void version PARAMS ((void)) ATTRIBUTE_NORETURN;
147 static int overloaded_jni_method_exists_p PARAMS ((const unsigned char *, int,
149 static void jni_print_char PARAMS ((FILE *, int));
151 JCF_u2 current_field_name;
152 JCF_u2 current_field_value;
153 JCF_u2 current_field_signature;
154 JCF_u2 current_field_flags;
156 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
157 ( current_field_name = (NAME), current_field_signature = (SIGNATURE), \
158 current_field_flags = (ACCESS_FLAGS), current_field_value = 0)
160 /* We pass over fields twice. The first time we just note the types
161 of the fields and then the start of the methods. Then we go back
162 and parse the fields for real. This is ugly. */
163 static int field_pass;
164 /* Likewise we pass over methods twice. The first time we generate
165 class decl information; the second time we generate actual method
167 static int method_pass;
169 #define HANDLE_END_FIELD() \
172 if (out && ! stubs && ! flag_jni) \
173 print_field_info (out, jcf, current_field_name, \
174 current_field_signature, \
175 current_field_flags); \
177 else if (! stubs && ! flag_jni) \
178 add_class_decl (out, jcf, current_field_signature);
180 #define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)
182 static int method_declared = 0;
183 static int method_access = 0;
184 static int method_printed = 0;
185 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
188 decompiled = 0; method_printed = 0; \
190 print_method_info (out, jcf, NAME, SIGNATURE, ACCESS_FLAGS); \
194 print_method_info (NULL, jcf, NAME, SIGNATURE, ACCESS_FLAGS); \
195 if (! stubs && ! flag_jni) \
196 add_class_decl (out, jcf, SIGNATURE); \
199 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
200 if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
202 static int decompiled = 0;
203 #define HANDLE_END_METHOD() \
204 if (out && method_printed) fputs (decompiled || stubs ? "\n" : ";\n", out);
206 #include "jcf-reader.c"
208 /* Some useful constants. */
209 #define F_NAN_MASK 0x7f800000
210 #if (1 == HOST_FLOAT_WORDS_BIG_ENDIAN) && ! defined (HOST_WORDS_BIG_ENDIAN)
211 #define D_NAN_MASK 0x000000007ff00000LL
213 #define D_NAN_MASK 0x7ff0000000000000LL
216 /* Return 1 if F is not Inf or NaN. */
218 java_float_finite (f)
224 /* We happen to know that F_NAN_MASK will match all NaN values, and
225 also positive and negative infinity. That's why we only need one
226 test here. See The Java Language Specification, section 20.9. */
227 return (u.i & F_NAN_MASK) != F_NAN_MASK;
230 /* Return 1 if D is not Inf or NaN. */
232 java_double_finite (d)
238 /* Now check for all NaNs. */
239 return (u.l & D_NAN_MASK) != D_NAN_MASK;
242 /* Print a character, appropriately mangled for JNI. */
245 jni_print_char (stream, ch)
250 jcf_print_char (stream, ch);
251 else if (ch == '(' || ch == ')')
256 fputs ("_1", stream);
258 fputs ("_2", stream);
260 fputs ("_3", stream);
263 else if ((ch >= '0' && ch <= '9')
264 || (ch >= 'a' && ch <= 'z')
265 || (ch >= 'A' && ch <= 'Z'))
269 /* "Unicode" character. */
270 fprintf (stream, "_0%04x", ch);
274 /* Print a name from the class data. If the index does not point to a
275 string, an error results. */
278 DEFUN(print_name, (stream, jcf, name_index),
279 FILE* stream AND JCF* jcf AND int name_index)
281 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
283 fprintf (stream, "<not a UTF8 constant>");
287 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
288 JPOOL_UTF_LENGTH (jcf, name_index));
291 /* For JNI we must correctly quote each character. */
292 const unsigned char *str = JPOOL_UTF_DATA (jcf, name_index);
293 int length = JPOOL_UTF_LENGTH (jcf, name_index);
294 const unsigned char *limit = str + length;
297 int ch = UTF8_GET (str, limit);
300 fprintf (stream, "\\<invalid>");
303 jni_print_char (stream, ch);
308 /* Print base name of class. The base name is everything after the
312 print_base_classname (stream, jcf, index)
317 int name_index = JPOOL_USHORT1 (jcf, index);
319 const unsigned char *s, *p, *limit;
321 s = JPOOL_UTF_DATA (jcf, name_index);
322 len = JPOOL_UTF_LENGTH (jcf, name_index);
327 int c = UTF8_GET (s, limit);
334 int ch = UTF8_GET (p, limit);
336 fputs ("::", stream);
338 jcf_print_char (stream, ch);
342 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
343 and 1 if STR is "greater" than NAME. */
346 utf8_cmp (str, length, name)
347 const unsigned char *str;
351 const unsigned char *limit = str + length;
354 for (i = 0; name[i]; ++i)
356 int ch = UTF8_GET (str, limit);
361 return str == limit ? 0 : 1;
364 /* This is a sorted list of all C++ keywords. */
366 static const char *cxx_keywords[] =
402 /* If NAME is the name of a C++ keyword, then return an override name.
403 This is a name that can be used in place of the keyword.
404 Otherwise, return NULL. The return value is malloc()d. */
407 cxx_keyword_subst (str, length)
408 const unsigned char *str;
411 int last = sizeof (cxx_keywords) / sizeof (const char *);
413 int mid = (last + first) / 2;
416 for (mid = (last + first) / 2;
418 old = mid, mid = (last + first) / 2)
420 int r = utf8_cmp (str, length, cxx_keywords[mid]);
424 char *str = xmalloc (9 + strlen (cxx_keywords[mid]));
425 strcpy (str, "__dummy_");
426 strcat (str, cxx_keywords[mid]);
437 /* Generate an access control keyword based on FLAGS. */
440 generate_access (stream, flags)
444 if ((flags & ACC_VISIBILITY) == last_access)
446 last_access = (flags & ACC_VISIBILITY);
451 fputs ("public: // actually package-private\n", stream);
454 fputs ("public:\n", stream);
457 fputs ("private:\n", stream);
460 fputs ("public: // actually protected\n", stream);
464 fprintf (stream, "#error unrecognized visibility %d\n",
465 (flags & ACC_VISIBILITY));
470 /* See if NAME is already the name of a method. */
472 name_is_method_p (name, length)
473 const unsigned char *name;
476 struct method_name *p;
478 for (p = method_name_list; p != NULL; p = p->next)
480 if (p->length == length && ! memcmp (p->name, name, length))
486 /* If there is already a method named NAME, whose signature is not
487 SIGNATURE, then return true. Otherwise return false. */
489 overloaded_jni_method_exists_p (name, length, signature, sig_length)
490 const unsigned char *name;
492 const char *signature;
495 struct method_name *p;
497 for (p = method_name_list; p != NULL; p = p->next)
499 if (p->length == length
500 && ! memcmp (p->name, name, length)
501 && (p->sig_length != sig_length
502 || memcmp (p->signature, signature, sig_length)))
508 /* Get name of a field. This handles renamings due to C++ clash. */
510 get_field_name (jcf, name_index, flags)
515 unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
516 int length = JPOOL_UTF_LENGTH (jcf, name_index);
519 if (name_is_method_p (name, length))
521 /* This field name matches a method. So override the name with
522 a dummy name. This is yucky, but it isn't clear what else to
523 do. FIXME: if the field is static, then we'll be in real
525 if ((flags & ACC_STATIC))
527 fprintf (stderr, "static field has same name as method\n");
532 override = xmalloc (length + 3);
533 memcpy (override, name, length);
534 strcpy (override + length, "__");
537 override = cxx_keyword_subst (name, length);
542 /* Print a field name. Convenience function for use with
545 print_field_name (stream, jcf, name_index, flags)
551 char *override = get_field_name (jcf, name_index, flags);
555 fputs (override, stream);
559 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
560 JPOOL_UTF_LENGTH (jcf, name_index));
564 DEFUN(print_field_info, (stream, jcf, name_index, sig_index, flags),
565 FILE *stream AND JCF* jcf
566 AND int name_index AND int sig_index AND JCF_u2 flags)
568 char *override = NULL;
570 generate_access (stream, flags);
571 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
573 fprintf (stream, "<not a UTF8 constant>");
579 if ((flags & ACC_STATIC))
580 fputs ("static ", out);
582 if ((flags & ACC_FINAL))
584 if (current_field_value > 0)
589 switch (JPOOL_TAG (jcf, current_field_value))
591 case CONSTANT_Integer:
594 int most_negative = 0;
595 fputs ("const jint ", out);
596 print_field_name (out, jcf, name_index, 0);
598 num = JPOOL_INT (jcf, current_field_value);
599 /* We single out the most negative number to print
600 specially. This avoids later warnings from g++. */
601 if (num == (jint) 0x80000000)
606 format_int (buffer, (jlong) num, 10);
607 fprintf (out, "%sL%s;\n", buffer, most_negative ? " - 1" : "");
613 int most_negative = 0;
614 fputs ("const jlong ", out);
615 print_field_name (out, jcf, name_index, 0);
617 num = JPOOL_LONG (jcf, current_field_value);
618 /* We single out the most negative number to print
619 specially.. This avoids later warnings from g++. */
620 if (num == (jlong) 0x8000000000000000LL)
625 format_int (buffer, num, 10);
626 fprintf (out, "%sLL%s;\n", buffer, most_negative ? " - 1" :"");
631 jfloat fnum = JPOOL_FLOAT (jcf, current_field_value);
632 fputs ("const jfloat ", out);
633 print_field_name (out, jcf, name_index, 0);
634 if (! java_float_finite (fnum))
637 fprintf (out, " = %.10g;\n", fnum);
640 case CONSTANT_Double:
642 jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value);
643 fputs ("const jdouble ", out);
644 print_field_name (out, jcf, name_index, 0);
645 if (! java_double_finite (dnum))
648 fprintf (out, " = %.17g;\n", dnum);
652 /* We can't print this as a constant, but we can still
653 print something sensible. */
663 override = get_field_name (jcf, name_index, flags);
664 print_c_decl (out, jcf, name_index, sig_index, 0, override, flags);
673 DEFUN(print_method_info, (stream, jcf, name_index, sig_index, flags),
674 FILE *stream AND JCF* jcf
675 AND int name_index AND int sig_index AND JCF_u2 flags)
677 const unsigned char *str;
678 int length, is_init = 0;
679 char *override = NULL;
682 method_access = flags;
683 if (stream && JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
684 fprintf (stream, "<not a UTF8 constant>");
685 str = JPOOL_UTF_DATA (jcf, name_index);
686 length = JPOOL_UTF_LENGTH (jcf, name_index);
687 if (str[0] == '<' || str[0] == '$')
689 /* Ignore internally generated methods like <clinit> and
690 $finit$. However, treat <init> as a constructor. */
691 if (! utf8_cmp (str, length, "<init>"))
693 else if (! METHOD_IS_FINAL (jcf->access_flags, flags)
694 && ! (flags & ACC_STATIC))
696 /* FIXME: i18n bug here. Order of prints should not be
698 fprintf (stderr, "ignored method `");
699 jcf_print_utf8 (stderr, str, length);
700 fprintf (stderr, "' marked virtual\n");
708 /* During the first method pass, build a list of method names. This will
709 be used to determine if field names conflict with method names. */
712 struct method_name *nn;
714 nn = (struct method_name *) xmalloc (sizeof (struct method_name));
715 nn->name = (char *) xmalloc (length);
716 memcpy (nn->name, str, length);
718 nn->next = method_name_list;
719 nn->sig_length = JPOOL_UTF_LENGTH (jcf, sig_index);
720 nn->signature = (char *) xmalloc (nn->sig_length);
721 memcpy (nn->signature, JPOOL_UTF_DATA (jcf, sig_index),
723 method_name_list = nn;
725 /* The rest of this function doesn't matter. */
729 /* We don't worry about overrides in JNI mode. */
732 /* We can't generate a method whose name is a C++ reserved word.
733 We can't just ignore the function, because that will cause
734 incorrect code to be generated if the function is virtual
735 (not only for calls to this function for for other functions
736 after it in the vtbl). So we give it a dummy name instead. */
737 override = cxx_keyword_subst (str, length);
740 /* If the method is static or final, we can safely skip it.
741 If we don't skip it then we'll have problems since the
742 mangling will be wrong. FIXME. */
743 if (METHOD_IS_FINAL (jcf->access_flags, flags)
744 || (flags & ACC_STATIC))
752 if (! stubs && ! flag_jni)
756 generate_access (stream, flags);
759 if ((flags & ACC_STATIC))
760 fputs ("static ", out);
761 else if (! METHOD_IS_FINAL (jcf->access_flags, flags))
763 /* Don't print `virtual' if we have a constructor. */
765 fputs ("virtual ", out);
767 print_c_decl (out, jcf, name_index, sig_index, is_init, override, flags);
769 if ((flags & ACC_ABSTRACT))
776 if (METHOD_IS_NATIVE (flags))
779 print_stub_or_jni (out, jcf, name_index, sig_index,
780 is_init, override, flags);
788 /* Try to decompile a method body. Right now we just try to handle a
789 simple case that we can do. Expand as desired. */
791 decompile_method (out, jcf, code_len)
796 const unsigned char *codes = jcf->read_ptr;
798 uint16 name_and_type, name;
800 /* If the method is synchronized, don't touch it. */
801 if ((method_access & ACC_SYNCHRONIZED))
805 && codes[0] == OPCODE_aload_0
806 && codes[1] == OPCODE_getfield
807 && (codes[4] == OPCODE_areturn
808 || codes[4] == OPCODE_dreturn
809 || codes[4] == OPCODE_freturn
810 || codes[4] == OPCODE_ireturn
811 || codes[4] == OPCODE_lreturn))
813 /* Found code like `return FIELD'. */
814 fputs (" { return ", out);
815 index = (codes[2] << 8) | codes[3];
816 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
817 /* FIXME: ensure that the field's class is this class. */
818 name_and_type = JPOOL_USHORT2 (jcf, index);
819 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
820 name = JPOOL_USHORT1 (jcf, name_and_type);
822 print_field_name (out, jcf, name, 0);
826 else if (code_len == 2
827 && codes[0] == OPCODE_aload_0
828 && codes[1] == OPCODE_areturn)
830 /* Found `return this'. */
831 fputs (" { return this; }", out);
834 else if (code_len == 1 && codes[0] == OPCODE_return)
836 /* Found plain `return'. */
840 else if (code_len == 2
841 && codes[0] == OPCODE_aconst_null
842 && codes[1] == OPCODE_areturn)
844 /* Found `return null'. We don't want to depend on NULL being
846 fputs (" { return 0; }", out);
851 /* Print one piece of a signature. Returns pointer to next parseable
852 character on success, NULL on error. */
853 static const unsigned char *
854 decode_signature_piece (stream, signature, limit, need_space)
856 const unsigned char *signature, *limit;
862 switch (signature[0])
865 /* More spaghetti. */
868 for (signature++; (signature < limit
870 && *signature <= '9'); signature++)
875 ctype = "jbyteArray";
878 ctype = "jcharArray";
881 ctype = "jdoubleArray";
884 ctype = "jfloatArray";
890 ctype = "jshortArray";
893 ctype = "jlongArray";
896 ctype = "jbooleanArray";
899 /* We have a nested array. */
902 fputs ("JArray<", stream);
906 /* We have to generate a reference to JArray here, so that
907 our output matches what the compiler does. */
909 /* Space between `<' and `:' to avoid C++ digraphs. */
911 fputs ("JArray< ::", stream);
912 while (signature < limit && *signature != ';')
914 int ch = UTF8_GET (signature, limit);
918 fputs ("::", stream);
920 jcf_print_char (stream, ch);
924 fputs (" *> *", stream);
929 /* Unparseable signature. */
933 /* If the previous iterations left us with something to print,
934 print it. For JNI, we always print `jobjectArray' in the
936 if (flag_jni && ctype == NULL)
938 ctype = "jobjectArray";
941 /* The `printit' case will advance SIGNATURE for us. If we
942 don't go there, we must advance past the `;' ourselves. */
950 /* This shouldn't happen. */
953 case 'B': ctype = "jbyte"; goto printit;
954 case 'C': ctype = "jchar"; goto printit;
955 case 'D': ctype = "jdouble"; goto printit;
956 case 'F': ctype = "jfloat"; goto printit;
957 case 'I': ctype = "jint"; goto printit;
958 case 'J': ctype = "jlong"; goto printit;
959 case 'S': ctype = "jshort"; goto printit;
960 case 'Z': ctype = "jboolean"; goto printit;
961 case 'V': ctype = "void"; goto printit;
965 /* We know about certain types and special-case their
967 FIXME: something like java.lang.Exception should be
968 printed as `jthrowable', because it is a subclass. This
969 means that gcjh must read the entire hierarchy and
971 if (! strncmp (signature, "Ljava/lang/String;",
972 sizeof ("Ljava/lang/String;") -1))
974 else if (! strncmp (signature, "Ljava/lang/Class;",
975 sizeof ("Ljava/lang/Class;") - 1))
977 else if (! strncmp (signature, "Ljava/lang/Throwable;",
978 sizeof ("Ljava/lang/Throwable;") - 1))
979 ctype = "jthrowable";
980 else if (! strncmp (signature, "Ljava/lang/ref/WeakReference;",
981 sizeof ("Ljava/lang/ref/WeakReference;") - 1))
986 while (*signature && *signature != ';')
991 /* Print a leading "::" so we look in the right namespace. */
992 fputs ("::", stream);
994 while (*signature && *signature != ';')
996 int ch = UTF8_GET (signature, limit);
998 fputs ("::", stream);
1000 jcf_print_char (stream, ch);
1002 fputs (" *", stream);
1003 if (*signature == ';')
1009 jni_print_char (stream, *signature++);
1014 fputs (ctype, stream);
1020 while (array_depth-- > 0)
1021 fputs ("> *", stream);
1028 DEFUN(print_c_decl, (stream, jcf, name_index, signature_index, is_init,
1029 name_override, flags),
1030 FILE* stream AND JCF* jcf
1031 AND int name_index AND int signature_index
1032 AND int is_init AND const char *name_override AND int flags)
1034 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1036 fprintf (stream, "<not a UTF8 constant>");
1041 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1042 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1043 register const unsigned char *str = str0;
1044 const unsigned char *limit = str + length;
1046 int is_method = str[0] == '(';
1047 const unsigned char *next;
1049 /* If printing a method, skip to the return signature and print
1050 that first. However, there is no return value if this is a
1052 if (is_method && ! is_init)
1062 /* If printing a field or an ordinary method, then print the
1063 "return value" now. */
1064 if (! is_method || ! is_init)
1066 next = decode_signature_piece (stream, str, limit, &need_space);
1069 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1075 /* Now print the name of the thing. */
1077 fputs (" ", stream);
1078 print_full_cxx_name (stream, jcf, name_index,
1079 signature_index, is_init, name_override,
1084 /* Print the unqualified method name followed by the signature. */
1086 DEFUN(print_full_cxx_name, (stream, jcf, name_index, signature_index,
1087 is_init, name_override, flags),
1088 FILE* stream AND JCF* jcf
1089 AND int name_index AND int signature_index AND int is_init
1090 AND const char *name_override AND int flags)
1092 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1093 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1094 register const unsigned char *str = str0;
1095 const unsigned char *limit = str + length;
1097 int is_method = str[0] == '(';
1098 const unsigned char *next;
1101 fputs (name_override, stream);
1102 else if (name_index)
1104 /* Declare constructors specially. */
1106 print_base_classname (stream, jcf, jcf->this_class);
1108 print_name (stream, jcf, name_index);
1113 unsigned char *signature = JPOOL_UTF_DATA (jcf, signature_index);
1114 int sig_len = JPOOL_UTF_LENGTH (jcf, signature_index);
1115 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf, name_index),
1116 JPOOL_UTF_LENGTH (jcf, name_index),
1117 signature, sig_len))
1119 /* If this method is overloaded by another native method,
1120 then include the argument information in the mangled
1122 unsigned char *limit = signature + sig_len;
1123 fputs ("__", stream);
1124 while (signature < limit)
1126 int ch = UTF8_GET (signature, limit);
1127 jni_print_char (stream, ch);
1139 /* Have a method or a constructor. Print signature pieces
1141 fputs (" (", stream);
1145 /* In JNI mode, add extra arguments. */
1148 /* FIXME: it would be nice to know if we are printing a decl
1149 or a definition, and only print `env' for the latter. */
1150 fputs ("JNIEnv *env", stream);
1152 fputs ((flags & ACC_STATIC) ? ", jclass" : ", jobject", stream);
1155 fputs (", ", stream);
1158 while (str < limit && *str != ')')
1160 next = decode_signature_piece (stream, str, limit, &need_space);
1163 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1168 if (next < limit && *next != ')')
1169 fputs (", ", stream);
1173 fputs (")", stream);
1177 /* This is a helper for print_stub_or_jni. */
1179 DEFUN (print_name_for_stub_or_jni, (stream, jcf, name_index, signature_index,
1180 is_init, name_override, flags),
1181 FILE *stream AND JCF *jcf
1182 AND int name_index AND int signature_index
1183 AND int is_init AND const char *name_override AND int flags)
1185 const char *const prefix = flag_jni ? "Java_" : "";
1186 print_cxx_classname (stream, prefix, jcf, jcf->this_class);
1187 fputs (flag_jni ? "_" : "::", stream);
1188 print_full_cxx_name (stream, jcf, name_index,
1189 signature_index, is_init, name_override,
1194 DEFUN(print_stub_or_jni, (stream, jcf, name_index, signature_index, is_init,
1195 name_override, flags),
1196 FILE* stream AND JCF* jcf
1197 AND int name_index AND int signature_index
1198 AND int is_init AND const char *name_override AND int flags)
1200 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1202 fprintf (stream, "<not a UTF8 constant>");
1207 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1208 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1209 register const unsigned char *str = str0;
1210 const unsigned char *limit = str + length;
1212 int is_method = str[0] == '(';
1213 const unsigned char *next;
1215 /* Don't print fields in the JNI case. */
1216 if (! is_method && flag_jni)
1219 if (flag_jni && ! stubs)
1220 fputs ("extern ", stream);
1222 /* If printing a method, skip to the return signature and print
1223 that first. However, there is no return value if this is a
1225 if (is_method && ! is_init)
1235 /* If printing a field or an ordinary method, then print the
1236 "return value" now. Note that a constructor can't be native,
1237 so we don't bother checking this in the JNI case. */
1238 if (! is_method || ! is_init)
1240 next = decode_signature_piece (stream, str, limit, &need_space);
1243 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1249 /* When printing a JNI header we need to respect the space. In
1250 other cases we're just going to insert a newline anyway. */
1251 fputs (need_space && ! stubs ? " " : "\n", stream);
1253 /* Now print the name of the thing. */
1254 print_name_for_stub_or_jni (stream, jcf, name_index,
1255 signature_index, is_init, name_override,
1258 /* Print the body. */
1262 fputs ("\n{\n (*env)->FatalError (\"", stream);
1264 fputs ("\n{\n JvFail (\"", stream);
1265 print_name_for_stub_or_jni (stream, jcf, name_index,
1266 signature_index, is_init,
1269 fputs (" not implemented\");\n}\n\n", stream);
1275 DEFUN(print_mangled_classname, (stream, jcf, prefix, index),
1276 FILE *stream AND JCF *jcf AND const char *prefix AND int index)
1278 int name_index = JPOOL_USHORT1 (jcf, index);
1279 fputs (prefix, stream);
1280 jcf_print_utf8_replace (out,
1281 JPOOL_UTF_DATA (jcf, name_index),
1282 JPOOL_UTF_LENGTH (jcf, name_index),
1286 /* Print PREFIX, then a class name in C++ format. If the name refers
1287 to an array, ignore it and don't print PREFIX. Returns 1 if
1288 something was printed, 0 otherwise. */
1290 print_cxx_classname (stream, prefix, jcf, index)
1296 int name_index = JPOOL_USHORT1 (jcf, index);
1298 const unsigned char *s, *p, *limit;
1300 s = JPOOL_UTF_DATA (jcf, name_index);
1301 len = JPOOL_UTF_LENGTH (jcf, name_index);
1304 /* Explicitly omit arrays here. */
1306 c = UTF8_GET (p, limit);
1310 fputs (prefix, stream);
1312 /* Print a leading "::" so we look in the right namespace. */
1313 if (! flag_jni && ! stubs)
1314 fputs ("::", stream);
1318 c = UTF8_GET (s, limit);
1320 fputs (flag_jni ? "_" : "::", stream);
1322 jni_print_char (stream, c);
1328 int written_class_count = 0;
1330 /* Return name of superclass. If LEN is not NULL, fill it with length
1332 static const unsigned char *
1333 super_class_name (derived_jcf, len)
1337 int supername_index = JPOOL_USHORT1 (derived_jcf, derived_jcf->super_class);
1338 int supername_length = JPOOL_UTF_LENGTH (derived_jcf, supername_index);
1339 const unsigned char *supername =
1340 JPOOL_UTF_DATA (derived_jcf, supername_index);
1343 *len = supername_length;
1350 /* We keep track of all the `#include's we generate, so we can avoid
1355 struct include *next;
1358 /* List of all includes. */
1359 static struct include *all_includes = NULL;
1361 /* Generate a #include. */
1363 print_include (out, utf8, len)
1365 const unsigned char *utf8;
1368 struct include *incl;
1374 len = strlen (utf8);
1376 for (incl = all_includes; incl; incl = incl->next)
1378 /* We check the length because we might have a proper prefix. */
1379 if (len == (int) strlen (incl->name)
1380 && ! strncmp (incl->name, utf8, len))
1384 incl = (struct include *) xmalloc (sizeof (struct include));
1385 incl->name = xmalloc (len + 1);
1386 strncpy (incl->name, utf8, len);
1387 incl->name[len] = '\0';
1388 incl->next = all_includes;
1389 all_includes = incl;
1391 fputs ("#include <", out);
1392 jcf_print_utf8_replace (out, utf8, len,
1394 flag_jni ? '_' : '/');
1395 fputs (".h>\n", out);
1400 /* This is used to represent part of a package or class name. */
1403 /* The text of this part of the name. */
1405 /* True if this represents a class. */
1407 /* Linked list of all classes and packages inside this one. */
1408 struct namelet *subnamelets;
1409 /* Pointer to next sibling. */
1410 struct namelet *next;
1413 static void add_namelet PARAMS ((const unsigned char *,
1414 const unsigned char *, struct namelet *));
1415 static void print_namelet PARAMS ((FILE *, struct namelet *, int));
1417 /* The special root namelet. */
1418 static struct namelet root =
1426 /* This extracts the next name segment from the full UTF-8 encoded
1427 package or class name and links it into the tree. It does this
1430 add_namelet (name, name_limit, parent)
1431 const unsigned char *name, *name_limit;
1432 struct namelet *parent;
1434 const unsigned char *p;
1435 struct namelet *n = NULL, *np;
1437 /* We want to skip the standard namespaces that we assume the
1438 runtime already knows about. We only do this at the top level,
1439 though, hence the check for `root'. */
1440 if (parent == &root)
1442 #define JAVALANG "java/lang/"
1443 #define JAVAIO "java/io/"
1444 #define JAVAUTIL "java/util/"
1445 if ((name_limit - name >= (int) sizeof (JAVALANG) - 1
1446 && ! strncmp (name, JAVALANG, sizeof (JAVALANG) - 1))
1447 || (name_limit - name >= (int) sizeof (JAVAUTIL) - 1
1448 && ! strncmp (name, JAVAUTIL, sizeof (JAVAUTIL) - 1))
1449 || (name_limit - name >= (int) sizeof (JAVAIO) - 1
1450 && ! strncmp (name, JAVAIO, sizeof (JAVAIO) - 1)))
1454 for (p = name; p < name_limit && *p != '/' && *p != '$'; ++p)
1457 /* Search for this name beneath the PARENT node. */
1458 for (np = parent->subnamelets; np != NULL; np = np->next)
1460 /* We check the length because we might have a proper prefix. */
1461 if ((int) strlen (np->name) == p - name &&
1462 ! strncmp (name, np->name, p - name))
1471 n = (struct namelet *) xmalloc (sizeof (struct namelet));
1472 n->name = xmalloc (p - name + 1);
1473 strncpy (n->name, name, p - name);
1474 n->name[p - name] = '\0';
1475 n->is_class = (p == name_limit || *p == '$');
1476 n->subnamelets = NULL;
1477 n->next = parent->subnamelets;
1478 parent->subnamelets = n;
1481 /* We recurse if there is more text, and if the trailing piece does
1482 not represent an inner class. */
1483 if (p < name_limit && *p != '$')
1484 add_namelet (p + 1, name_limit, n);
1487 /* Print a single namelet. Destroys namelets while printing. */
1489 print_namelet (out, name, depth)
1491 struct namelet *name;
1499 for (i = 0; i < depth; ++i)
1501 fprintf (out, "%s %s", name->is_class ? "class" : "namespace",
1503 if (name->is_class && name->subnamelets == NULL)
1509 for (i = 0; i < depth; ++i)
1515 c = name->subnamelets;
1518 struct namelet *next = c->next;
1519 print_namelet (out, c, depth + 2);
1527 for (i = 0; i < depth; ++i)
1530 /* Only print a `;' when printing a class. C++ is evil. */
1540 /* This is called to add some classes to the list of classes for which
1541 we need decls. The signature argument can be a function
1544 add_class_decl (out, jcf, signature)
1549 const unsigned char *s = JPOOL_UTF_DATA (jcf, signature);
1550 int len = JPOOL_UTF_LENGTH (jcf, signature);
1552 /* Name of class we are processing. */
1553 int name_index = JPOOL_USHORT1 (jcf, jcf->this_class);
1554 int tlen = JPOOL_UTF_LENGTH (jcf, name_index);
1555 const char *tname = JPOOL_UTF_DATA (jcf, name_index);
1557 for (i = 0; i < len; ++i)
1559 int start, saw_dollar;
1561 /* If we see an array, then we include the array header. */
1564 print_include (out, "gcj/array", -1);
1568 /* We're looking for `L<stuff>;' -- everything else is
1574 for (start = ++i; i < len && s[i] != ';'; ++i)
1576 if (! saw_dollar && s[i] == '$' && out)
1579 /* If this class represents an inner class, then
1580 generate a `#include' for the outer class. However,
1581 don't generate the include if the outer class is the
1582 class we are processing. */
1583 if (i - start < tlen || strncmp (&s[start], tname, i - start))
1584 print_include (out, &s[start], i - start);
1589 /* If we saw an inner class, then the generated #include will
1590 declare the class. So in this case we needn't bother. */
1592 add_namelet (&s[start], &s[i], &root);
1596 /* Print declarations for all classes required by this class. Any
1597 class or package in the `java' package is assumed to be handled
1598 statically in libjava; we don't generate declarations for these.
1599 This makes the generated headers a bit easier to read. */
1601 print_class_decls (out, jcf, self)
1606 /* Make sure to always add the current class to the list of things
1607 that should be declared. */
1608 int name_index = JPOOL_USHORT1 (jcf, self);
1610 const unsigned char *s;
1612 s = JPOOL_UTF_DATA (jcf, name_index);
1613 len = JPOOL_UTF_LENGTH (jcf, name_index);
1614 add_namelet (s, s + len, &root);
1616 if (root.subnamelets)
1618 fputs ("extern \"Java\"\n{\n", out);
1619 /* We use an initial offset of 0 because the root namelet
1620 doesn't cause anything to print. */
1621 print_namelet (out, &root, 0);
1622 fputs ("};\n\n", out);
1629 DEFUN(process_file, (jcf, out),
1630 JCF *jcf AND FILE *out)
1633 uint32 field_start, method_end, method_start;
1639 if (jcf_parse_preamble (jcf) != 0)
1641 fprintf (stderr, "Not a valid Java .class file.\n");
1646 /* Parse and possibly print constant pool */
1647 code = jcf_parse_constant_pool (jcf);
1650 fprintf (stderr, "error while parsing constant pool\n");
1654 code = verify_constant_pool (jcf);
1657 fprintf (stderr, "error in constant pool entry #%d\n", code);
1662 jcf_parse_class (jcf);
1664 if (written_class_count++ == 0 && out)
1666 const char *cstart, *cstart2, *mode, *cend, *what, *jflag;
1681 mode = " -*- c++ -*-";
1687 fprintf (out, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
1688 cstart, mode, cend);
1691 fprintf (out, "%s This file was created by `gcjh -stubs%s'.%s\n\
1693 %s This file is intended to give you a head start on implementing native\n\
1694 %s methods using %s.\n\
1695 %s Be aware: running `gcjh -stubs %s' once more for this class may\n\
1696 %s overwrite any edits you have made to this file.%s\n\n",
1697 cstart, jflag, mode,
1713 print_mangled_classname (out, jcf, "#ifndef __", jcf->this_class);
1714 fprintf (out, "__\n");
1716 print_mangled_classname (out, jcf, "#define __", jcf->this_class);
1717 fprintf (out, "__\n\n");
1721 fprintf (out, "#include <jni.h>\n\n");
1722 fprintf (out, "#ifdef __cplusplus\n");
1723 fprintf (out, "extern \"C\"\n");
1724 fprintf (out, "{\n");
1725 fprintf (out, "#endif\n");
1729 /* We do this to ensure that inline methods won't be
1730 `outlined' by g++. This works as long as method and
1731 fields are not added by the user. */
1732 fprintf (out, "#pragma interface\n");
1734 if (jcf->super_class)
1737 const unsigned char *supername =
1738 super_class_name (jcf, &super_length);
1741 print_include (out, supername, super_length);
1747 /* Strip off the ".class" portion of the name when printing
1748 the include file name. */
1749 int len = strlen (jcf->classname);
1750 if (len > 6 && ! strcmp (&jcf->classname[len - 6], ".class"))
1752 print_include (out, jcf->classname, len);
1753 print_include (out, "gcj/cni", -1);
1757 /* We want to parse the methods first. But we need to find where
1758 they start. So first we skip the fields, then parse the methods.
1759 Then we parse the fields and skip the methods. This is ugly, but
1760 not too bad since we need two full passes to get class decl
1761 information anyway. */
1763 field_start = JCF_TELL (jcf);
1764 jcf_parse_fields (jcf);
1766 method_start = JCF_TELL (jcf);
1768 jcf_parse_methods (jcf);
1773 if (out && ! flag_jni)
1776 print_class_decls (out, jcf, jcf->this_class);
1778 for (i = 0; i < prepend_count; ++i)
1779 fprintf (out, "%s\n", prepend_specs[i]);
1780 if (prepend_count > 0)
1785 if (! print_cxx_classname (out, "class ", jcf, jcf->this_class))
1787 fprintf (stderr, "class is of array type\n");
1791 if (jcf->super_class)
1793 if (! print_cxx_classname (out, " : public ",
1794 jcf, jcf->super_class))
1796 fprintf (stderr, "base class is of array type\n");
1802 fputs ("\n{\n", out);
1806 /* Now go back for second pass over methods and fields. */
1807 JCF_SEEK (jcf, method_start);
1809 jcf_parse_methods (jcf);
1810 method_end = JCF_TELL (jcf);
1813 JCF_SEEK (jcf, field_start);
1814 jcf_parse_fields (jcf);
1815 JCF_SEEK (jcf, method_end);
1817 jcf_parse_final_attributes (jcf);
1823 fprintf (out, "\n#ifdef __cplusplus\n");
1824 fprintf (out, "}\n");
1825 fprintf (out, "#endif\n");
1829 /* Generate friend decl if we still must. */
1830 for (i = 0; i < friend_count; ++i)
1831 fprintf (out, " friend %s\n", friend_specs[i]);
1833 /* Generate extra declarations. */
1836 for (i = 0; i < add_count; ++i)
1837 fprintf (out, " %s\n", add_specs[i]);
1840 fputs ("};\n", out);
1842 if (append_count > 0)
1844 for (i = 0; i < append_count; ++i)
1845 fprintf (out, "%s\n", append_specs[i]);
1848 print_mangled_classname (out, jcf,
1849 "\n#endif /* __", jcf->this_class);
1850 fprintf (out, "__ */\n");
1856 /* This is used to mark options with no short value. */
1857 #define LONG_OPT(Num) ((Num) + 128)
1859 #define OPT_classpath LONG_OPT (0)
1860 #define OPT_CLASSPATH LONG_OPT (1)
1861 #define OPT_HELP LONG_OPT (2)
1862 #define OPT_TEMP LONG_OPT (3)
1863 #define OPT_VERSION LONG_OPT (4)
1864 #define OPT_PREPEND LONG_OPT (5)
1865 #define OPT_FRIEND LONG_OPT (6)
1866 #define OPT_ADD LONG_OPT (7)
1867 #define OPT_APPEND LONG_OPT (8)
1868 #define OPT_M LONG_OPT (9)
1869 #define OPT_MM LONG_OPT (10)
1870 #define OPT_MG LONG_OPT (11)
1871 #define OPT_MD LONG_OPT (12)
1872 #define OPT_MMD LONG_OPT (13)
1874 static struct option options[] =
1876 { "classpath", required_argument, NULL, OPT_classpath },
1877 { "CLASSPATH", required_argument, NULL, OPT_CLASSPATH },
1878 { "help", no_argument, NULL, OPT_HELP },
1879 { "stubs", no_argument, &stubs, 1 },
1880 { "td", required_argument, NULL, OPT_TEMP },
1881 { "verbose", no_argument, NULL, 'v' },
1882 { "version", no_argument, NULL, OPT_VERSION },
1883 { "prepend", required_argument, NULL, OPT_PREPEND },
1884 { "friend", required_argument, NULL, OPT_FRIEND },
1885 { "add", required_argument, NULL, OPT_ADD },
1886 { "append", required_argument, NULL, OPT_APPEND },
1887 { "M", no_argument, NULL, OPT_M },
1888 { "MM", no_argument, NULL, OPT_MM },
1889 { "MG", no_argument, NULL, OPT_MG },
1890 { "MD", no_argument, NULL, OPT_MD },
1891 { "MMD", no_argument, NULL, OPT_MMD },
1892 { "jni", no_argument, &flag_jni, 1 },
1893 { NULL, no_argument, NULL, 0 }
1899 fprintf (stderr, "Try `gcjh --help' for more information.\n");
1906 printf ("Usage: gcjh [OPTION]... CLASS...\n\n");
1907 printf ("Generate C++ header files from .class files\n\n");
1908 printf (" -stubs Generate an implementation stub file\n");
1909 printf (" -jni Generate a JNI header or stub\n");
1911 printf (" -add TEXT Insert TEXT into class body\n");
1912 printf (" -append TEXT Insert TEXT after class declaration\n");
1913 printf (" -friend TEXT Insert TEXT as `friend' declaration\n");
1914 printf (" -prepend TEXT Insert TEXT before start of class\n");
1916 printf (" --classpath PATH Set path to find .class files\n");
1917 printf (" --CLASSPATH PATH Set path to find .class files\n");
1918 printf (" -IDIR Append directory to class path\n");
1919 printf (" -d DIRECTORY Set output directory name\n");
1920 printf (" -o FILE Set output file name\n");
1921 printf (" -td DIRECTORY Set temporary directory name\n");
1923 printf (" --help Print this help, then exit\n");
1924 printf (" --version Print version number, then exit\n");
1925 printf (" -v, --verbose Print extra information while running\n");
1927 printf (" -M Print all dependencies to stdout;\n");
1928 printf (" suppress ordinary output\n");
1929 printf (" -MM Print non-system dependencies to stdout;\n");
1930 printf (" suppress ordinary output\n");
1931 printf (" -MD Print all dependencies to stdout\n");
1932 printf (" -MMD Print non-system dependencies to stdout\n");
1933 /* We omit -MG until it is implemented. */
1935 printf ("For bug reporting instructions, please see:\n");
1936 printf ("%s.\n", GCCBUGURL);
1943 printf ("gcjh (%s)\n\n", version_string);
1944 printf ("Copyright (C) 1998, 1999 Free Software Foundation, Inc.\n");
1945 printf ("This is free software; see the source for copying conditions. There is NO\n");
1946 printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
1951 DEFUN(main, (argc, argv),
1952 int argc AND char** argv)
1956 char *output_file = NULL;
1957 int emit_dependencies = 0, suppress_output = 0;
1962 fprintf (stderr, "gcjh: no classes specified\n");
1968 /* We use getopt_long_only to allow single `-' long options. For
1969 some of our options this is more natural. */
1970 while ((opt = getopt_long_only (argc, argv, "I:d:o:v", options, NULL)) != -1)
1975 /* Already handled. */
1979 output_file = optarg;
1983 output_directory = optarg;
1987 jcf_path_include_arg (optarg);
1995 jcf_path_classpath_arg (optarg);
1999 jcf_path_CLASSPATH_arg (optarg);
2007 temp_directory = optarg;
2015 if (prepend_count == 0)
2016 prepend_specs = (char**) ALLOC (argc * sizeof (char*));
2017 prepend_specs[prepend_count++] = optarg;
2021 if (friend_count == 0)
2022 friend_specs = (char**) ALLOC (argc * sizeof (char*));
2023 friend_specs[friend_count++] = optarg;
2028 add_specs = (char**) ALLOC (argc * sizeof (char*));
2029 add_specs[add_count++] = optarg;
2033 if (append_count == 0)
2034 append_specs = (char**) ALLOC (argc * sizeof (char*));
2035 append_specs[append_count++] = optarg;
2039 emit_dependencies = 1;
2040 suppress_output = 1;
2041 jcf_dependency_init (1);
2045 emit_dependencies = 1;
2046 suppress_output = 1;
2047 jcf_dependency_init (0);
2051 fprintf (stderr, "gcjh: `-MG' option is unimplemented\n");
2055 emit_dependencies = 1;
2056 jcf_dependency_init (1);
2060 emit_dependencies = 1;
2061 jcf_dependency_init (0);
2072 fprintf (stderr, "gcjh: no classes specified\n");
2078 if (output_file && emit_dependencies)
2080 fprintf (stderr, "gcjh: can't specify both -o and -MD\n");
2084 for (argi = optind; argi < argc; argi++)
2086 char *classname = argv[argi];
2087 char *current_output_file;
2088 const char *classfile_name;
2091 fprintf (stderr, "Processing %s\n", classname);
2093 jcf_dependency_reset ();
2094 classfile_name = find_class (classname, strlen (classname), &jcf, 0);
2095 if (classfile_name == NULL)
2097 fprintf (stderr, "%s: no such class\n", classname);
2101 fprintf (stderr, "Found in %s\n", classfile_name);
2104 if (strcmp (output_file, "-") == 0)
2106 else if (out == NULL)
2108 out = fopen (output_file, "w");
2112 perror (output_file);
2115 current_output_file = output_file;
2119 int dir_len = strlen (output_directory);
2120 int i, classname_length = strlen (classname);
2121 current_output_file = (char*) ALLOC (dir_len + classname_length + 5);
2122 strcpy (current_output_file, output_directory);
2123 if (dir_len > 0 && output_directory[dir_len-1] != '/')
2124 current_output_file[dir_len++] = '/';
2125 for (i = 0; classname[i] != '\0'; i++)
2127 char ch = classname[i];
2130 if (flag_jni && ch == '/')
2132 current_output_file[dir_len++] = ch;
2134 if (emit_dependencies)
2136 if (suppress_output)
2138 jcf_dependency_set_dep_file ("-");
2143 /* We use `.hd' and not `.d' to avoid clashes with
2144 dependency tracking from straight compilation. */
2145 strcpy (current_output_file + dir_len, ".hd");
2146 jcf_dependency_set_dep_file (current_output_file);
2149 strcpy (current_output_file + dir_len,
2150 stubs ? (flag_jni ? ".c" : ".cc") : ".h");
2151 jcf_dependency_set_target (current_output_file);
2152 if (! suppress_output)
2154 out = fopen (current_output_file, "w");
2157 perror (current_output_file);
2162 process_file (&jcf, out);
2164 if (current_output_file != output_file)
2165 free (current_output_file);
2166 jcf_dependency_write ();
2169 if (out != NULL && out != stdout)