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 const 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); \
193 print_method_info (NULL, jcf, NAME, SIGNATURE, ACCESS_FLAGS); \
194 else if (! stubs) add_class_decl (out, jcf, SIGNATURE);
196 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
197 if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
199 static int decompiled = 0;
200 #define HANDLE_END_METHOD() \
201 if (out && method_printed) fputs (decompiled || stubs ? "\n" : ";\n", out);
203 #include "jcf-reader.c"
205 /* Some useful constants. */
206 #define F_NAN_MASK 0x7f800000
207 #if (1 == HOST_FLOAT_WORDS_BIG_ENDIAN) && ! defined (HOST_WORDS_BIG_ENDIAN)
208 #define D_NAN_MASK 0x000000007ff00000LL
210 #define D_NAN_MASK 0x7ff0000000000000LL
213 /* Return 1 if F is not Inf or NaN. */
215 java_float_finite (f)
221 /* We happen to know that F_NAN_MASK will match all NaN values, and
222 also positive and negative infinity. That's why we only need one
223 test here. See The Java Language Specification, section 20.9. */
224 return (u.i & F_NAN_MASK) != F_NAN_MASK;
227 /* Return 1 if D is not Inf or NaN. */
229 java_double_finite (d)
235 /* Now check for all NaNs. */
236 return (u.l & D_NAN_MASK) != D_NAN_MASK;
239 /* Print a character, appropriately mangled for JNI. */
242 jni_print_char (stream, ch)
247 jcf_print_char (stream, ch);
248 else if (ch == '(' || ch == ')')
253 fputs ("_1", stream);
255 fputs ("_2", stream);
257 fputs ("_3", stream);
260 else if ((ch >= '0' && ch <= '9')
261 || (ch >= 'a' && ch <= 'z')
262 || (ch >= 'A' && ch <= 'Z'))
266 /* "Unicode" character. */
267 fprintf (stream, "_0%04x", ch);
271 /* Print a name from the class data. If the index does not point to a
272 string, an error results. */
275 DEFUN(print_name, (stream, jcf, name_index),
276 FILE* stream AND JCF* jcf AND int name_index)
278 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
280 fprintf (stream, "<not a UTF8 constant>");
284 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
285 JPOOL_UTF_LENGTH (jcf, name_index));
288 /* For JNI we must correctly quote each character. */
289 const unsigned char *str = JPOOL_UTF_DATA (jcf, name_index);
290 int length = JPOOL_UTF_LENGTH (jcf, name_index);
291 const unsigned char *limit = str + length;
294 int ch = UTF8_GET (str, limit);
297 fprintf (stream, "\\<invalid>");
300 jni_print_char (stream, ch);
305 /* Print base name of class. The base name is everything after the
309 print_base_classname (stream, jcf, index)
314 int name_index = JPOOL_USHORT1 (jcf, index);
316 const unsigned char *s, *p, *limit;
318 s = JPOOL_UTF_DATA (jcf, name_index);
319 len = JPOOL_UTF_LENGTH (jcf, name_index);
324 int c = UTF8_GET (s, limit);
331 int ch = UTF8_GET (p, limit);
333 fputs ("::", stream);
335 jcf_print_char (stream, ch);
339 /* Return 0 if NAME is equal to STR, nonzero otherwise. */
342 utf8_cmp (str, length, name)
343 const unsigned char *str;
347 const unsigned char *limit = str + length;
350 for (i = 0; name[i]; ++i)
352 int ch = UTF8_GET (str, limit);
360 /* If NAME is the name of a C++ keyword, then return an override name.
361 This is a name that can be used in place of the keyword.
362 Otherwise, return NULL. FIXME: for now, we only handle those
363 keywords we know to be a problem for libgcj. */
366 cxx_keyword_subst (str, length)
367 const unsigned char *str;
370 if (! utf8_cmp (str, length, "delete"))
371 return "__dummy_delete";
372 else if (! utf8_cmp (str, length, "enum"))
373 return "__dummy_enum";
377 /* Generate an access control keyword based on FLAGS. */
380 generate_access (stream, flags)
384 if ((flags & ACC_VISIBILITY) == last_access)
386 last_access = (flags & ACC_VISIBILITY);
391 fputs ("public: // actually package-private\n", stream);
394 fputs ("public:\n", stream);
397 fputs ("private:\n", stream);
400 fputs ("public: // actually protected\n", stream);
404 fprintf (stream, "#error unrecognized visibility %d\n",
405 (flags & ACC_VISIBILITY));
410 /* See if NAME is already the name of a method. */
412 name_is_method_p (name, length)
413 const unsigned char *name;
416 struct method_name *p;
418 for (p = method_name_list; p != NULL; p = p->next)
420 if (p->length == length && ! memcmp (p->name, name, length))
426 /* If there is already a method named NAME, whose signature is not
427 SIGNATURE, then return true. Otherwise return false. */
429 overloaded_jni_method_exists_p (name, length, signature, sig_length)
430 const unsigned char *name;
432 const char *signature;
435 struct method_name *p;
437 for (p = method_name_list; p != NULL; p = p->next)
439 if (p->length == length
440 && ! memcmp (p->name, name, length)
441 && (p->sig_length != sig_length
442 || memcmp (p->signature, signature, sig_length)))
448 /* Get name of a field. This handles renamings due to C++ clash. */
450 get_field_name (jcf, name_index, flags)
455 unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
456 int length = JPOOL_UTF_LENGTH (jcf, name_index);
458 const char *tmpconstptr;
460 if (name_is_method_p (name, length))
462 /* This field name matches a method. So override the name with
463 a dummy name. This is yucky, but it isn't clear what else to
464 do. FIXME: if the field is static, then we'll be in real
466 if ((flags & ACC_STATIC))
468 fprintf (stderr, "static field has same name as method\n");
473 override = xmalloc (length + 3);
474 memcpy (override, name, length);
475 strcpy (override + length, "__");
477 else if ((tmpconstptr = cxx_keyword_subst (name, length)) != NULL)
479 /* Must malloc OVERRIDE. */
480 override = xstrdup (tmpconstptr);
488 /* Print a field name. Convenience function for use with
491 print_field_name (stream, jcf, name_index, flags)
497 char *override = get_field_name (jcf, name_index, flags);
501 fputs (override, stream);
505 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
506 JPOOL_UTF_LENGTH (jcf, name_index));
510 DEFUN(print_field_info, (stream, jcf, name_index, sig_index, flags),
511 FILE *stream AND JCF* jcf
512 AND int name_index AND int sig_index AND JCF_u2 flags)
514 char *override = NULL;
516 generate_access (stream, flags);
517 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
519 fprintf (stream, "<not a UTF8 constant>");
525 if ((flags & ACC_STATIC))
526 fputs ("static ", out);
528 if ((flags & ACC_FINAL))
530 if (current_field_value > 0)
535 switch (JPOOL_TAG (jcf, current_field_value))
537 case CONSTANT_Integer:
540 int most_negative = 0;
541 fputs ("const jint ", out);
542 print_field_name (out, jcf, name_index, 0);
544 num = JPOOL_INT (jcf, current_field_value);
545 /* We single out the most negative number to print
546 specially. This avoids later warnings from g++. */
547 if (num == (jint) 0x80000000)
552 format_int (buffer, (jlong) num, 10);
553 fprintf (out, "%sL%s;\n", buffer, most_negative ? " - 1" : "");
559 int most_negative = 0;
560 fputs ("const jlong ", out);
561 print_field_name (out, jcf, name_index, 0);
563 num = JPOOL_LONG (jcf, current_field_value);
564 /* We single out the most negative number to print
565 specially.. This avoids later warnings from g++. */
566 if (num == (jlong) 0x8000000000000000LL)
571 format_int (buffer, num, 10);
572 fprintf (out, "%sLL%s;\n", buffer, most_negative ? " - 1" :"");
577 jfloat fnum = JPOOL_FLOAT (jcf, current_field_value);
578 fputs ("const jfloat ", out);
579 print_field_name (out, jcf, name_index, 0);
580 if (! java_float_finite (fnum))
583 fprintf (out, " = %.10g;\n", fnum);
586 case CONSTANT_Double:
588 jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value);
589 fputs ("const jdouble ", out);
590 print_field_name (out, jcf, name_index, 0);
591 if (! java_double_finite (dnum))
594 fprintf (out, " = %.17g;\n", dnum);
598 /* We can't print this as a constant, but we can still
599 print something sensible. */
609 override = get_field_name (jcf, name_index, flags);
610 print_c_decl (out, jcf, name_index, sig_index, 0, override, flags);
618 DEFUN(print_method_info, (stream, jcf, name_index, sig_index, flags),
619 FILE *stream AND JCF* jcf
620 AND int name_index AND int sig_index AND JCF_u2 flags)
622 const unsigned char *str;
623 int length, is_init = 0;
624 const char *override = NULL;
627 method_access = flags;
628 if (stream && JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
629 fprintf (stream, "<not a UTF8 constant>");
630 str = JPOOL_UTF_DATA (jcf, name_index);
631 length = JPOOL_UTF_LENGTH (jcf, name_index);
632 if (str[0] == '<' || str[0] == '$')
634 /* Ignore internally generated methods like <clinit> and
635 $finit$. However, treat <init> as a constructor. */
636 if (! utf8_cmp (str, length, "<init>"))
638 else if (! METHOD_IS_FINAL (jcf->access_flags, flags)
639 && ! (flags & ACC_STATIC))
641 /* FIXME: i18n bug here. Order of prints should not be
643 fprintf (stderr, "ignored method `");
644 jcf_print_utf8 (stderr, str, length);
645 fprintf (stderr, "' marked virtual\n");
654 struct method_name *nn;
656 nn = (struct method_name *) xmalloc (sizeof (struct method_name));
657 nn->name = (char *) xmalloc (length);
658 memcpy (nn->name, str, length);
660 nn->next = method_name_list;
661 nn->sig_length = JPOOL_UTF_LENGTH (jcf, sig_index);
662 nn->signature = (char *) xmalloc (nn->sig_length);
663 memcpy (nn->signature, JPOOL_UTF_DATA (jcf, sig_index),
665 method_name_list = nn;
668 /* If we're not printing, then the rest of this function doesn't
669 matter. This happens during the first method pass in JNI mode.
674 /* We don't worry about overrides in JNI mode. */
677 /* We can't generate a method whose name is a C++ reserved word.
678 We can't just ignore the function, because that will cause
679 incorrect code to be generated if the function is virtual
680 (not only for calls to this function for for other functions
681 after it in the vtbl). So we give it a dummy name instead. */
682 override = cxx_keyword_subst (str, length);
685 /* If the method is static or final, we can safely skip it.
686 If we don't skip it then we'll have problems since the
687 mangling will be wrong. FIXME. */
688 if (METHOD_IS_FINAL (jcf->access_flags, flags)
689 || (flags & ACC_STATIC))
694 if (! stubs && ! flag_jni)
698 generate_access (stream, flags);
701 if ((flags & ACC_STATIC))
702 fputs ("static ", out);
703 else if (! METHOD_IS_FINAL (jcf->access_flags, flags))
705 /* Don't print `virtual' if we have a constructor. */
707 fputs ("virtual ", out);
709 print_c_decl (out, jcf, name_index, sig_index, is_init, override, flags);
711 if ((flags & ACC_ABSTRACT))
718 if (METHOD_IS_NATIVE (flags))
721 print_stub_or_jni (out, jcf, name_index, sig_index,
722 is_init, override, flags);
727 /* Try to decompile a method body. Right now we just try to handle a
728 simple case that we can do. Expand as desired. */
730 decompile_method (out, jcf, code_len)
735 const unsigned char *codes = jcf->read_ptr;
737 uint16 name_and_type, name;
739 /* If the method is synchronized, don't touch it. */
740 if ((method_access & ACC_SYNCHRONIZED))
744 && codes[0] == OPCODE_aload_0
745 && codes[1] == OPCODE_getfield
746 && (codes[4] == OPCODE_areturn
747 || codes[4] == OPCODE_dreturn
748 || codes[4] == OPCODE_freturn
749 || codes[4] == OPCODE_ireturn
750 || codes[4] == OPCODE_lreturn))
752 /* Found code like `return FIELD'. */
753 fputs (" { return ", out);
754 index = (codes[2] << 8) | codes[3];
755 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
756 /* FIXME: ensure that the field's class is this class. */
757 name_and_type = JPOOL_USHORT2 (jcf, index);
758 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
759 name = JPOOL_USHORT1 (jcf, name_and_type);
761 print_field_name (out, jcf, name, 0);
765 else if (code_len == 2
766 && codes[0] == OPCODE_aload_0
767 && codes[1] == OPCODE_areturn)
769 /* Found `return this'. */
770 fputs (" { return this; }", out);
773 else if (code_len == 1 && codes[0] == OPCODE_return)
775 /* Found plain `return'. */
779 else if (code_len == 2
780 && codes[0] == OPCODE_aconst_null
781 && codes[1] == OPCODE_areturn)
783 /* Found `return null'. We don't want to depend on NULL being
785 fputs (" { return 0; }", out);
790 /* Print one piece of a signature. Returns pointer to next parseable
791 character on success, NULL on error. */
792 static const unsigned char *
793 decode_signature_piece (stream, signature, limit, need_space)
795 const unsigned char *signature, *limit;
801 switch (signature[0])
804 /* More spaghetti. */
807 for (signature++; (signature < limit
809 && *signature <= '9'); signature++)
814 ctype = "jbyteArray";
817 ctype = "jcharArray";
820 ctype = "jdoubleArray";
823 ctype = "jfloatArray";
829 ctype = "jshortArray";
832 ctype = "jlongArray";
835 ctype = "jbooleanArray";
838 /* We have a nested array. */
841 fputs ("JArray<", stream);
845 /* We have to generate a reference to JArray here, so that
846 our output matches what the compiler does. */
848 /* Space between `<' and `:' to avoid C++ digraphs. */
850 fputs ("JArray< ::", stream);
851 while (signature < limit && *signature != ';')
853 int ch = UTF8_GET (signature, limit);
857 fputs ("::", stream);
859 jcf_print_char (stream, ch);
863 fputs (" *> *", stream);
868 /* Unparseable signature. */
872 /* If the previous iterations left us with something to print,
873 print it. For JNI, we always print `jobjectArray' in the
875 if (flag_jni && ctype == NULL)
877 ctype = "jobjectArray";
880 /* The `printit' case will advance SIGNATURE for us. If we
881 don't go there, we must advance past the `;' ourselves. */
889 /* This shouldn't happen. */
892 case 'B': ctype = "jbyte"; goto printit;
893 case 'C': ctype = "jchar"; goto printit;
894 case 'D': ctype = "jdouble"; goto printit;
895 case 'F': ctype = "jfloat"; goto printit;
896 case 'I': ctype = "jint"; goto printit;
897 case 'J': ctype = "jlong"; goto printit;
898 case 'S': ctype = "jshort"; goto printit;
899 case 'Z': ctype = "jboolean"; goto printit;
900 case 'V': ctype = "void"; goto printit;
904 /* We know about certain types and special-case their
906 FIXME: something like java.lang.Exception should be
907 printed as `jthrowable', because it is a subclass. This
908 means that gcjh must read the entire hierarchy and
910 if (! strncmp (signature, "Ljava/lang/String;",
911 sizeof ("Ljava/lang/String;") -1))
913 else if (! strncmp (signature, "Ljava/lang/Class;",
914 sizeof ("Ljava/lang/Class;") - 1))
916 else if (! strncmp (signature, "Ljava/lang/Throwable;",
917 sizeof ("Ljava/lang/Throwable;") - 1))
918 ctype = "jthrowable";
919 else if (! strncmp (signature, "Ljava/lang/ref/WeakReference;",
920 sizeof ("Ljava/lang/ref/WeakReference;") - 1))
925 while (*signature && *signature != ';')
930 /* Print a leading "::" so we look in the right namespace. */
931 fputs ("::", stream);
933 while (*signature && *signature != ';')
935 int ch = UTF8_GET (signature, limit);
936 /* `$' is the separator for an inner class. */
937 if (ch == '/' || ch == '$')
938 fputs ("::", stream);
940 jcf_print_char (stream, ch);
942 fputs (" *", stream);
943 if (*signature == ';')
949 jni_print_char (stream, *signature++);
954 fputs (ctype, stream);
960 while (array_depth-- > 0)
961 fputs ("> *", stream);
968 DEFUN(print_c_decl, (stream, jcf, name_index, signature_index, is_init,
969 name_override, flags),
970 FILE* stream AND JCF* jcf
971 AND int name_index AND int signature_index
972 AND int is_init AND const char *name_override AND int flags)
974 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
976 fprintf (stream, "<not a UTF8 constant>");
981 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
982 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
983 register const unsigned char *str = str0;
984 const unsigned char *limit = str + length;
986 int is_method = str[0] == '(';
987 const unsigned char *next;
989 /* If printing a method, skip to the return signature and print
990 that first. However, there is no return value if this is a
992 if (is_method && ! is_init)
1002 /* If printing a field or an ordinary method, then print the
1003 "return value" now. */
1004 if (! is_method || ! is_init)
1006 next = decode_signature_piece (stream, str, limit, &need_space);
1009 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1015 /* Now print the name of the thing. */
1017 fputs (" ", stream);
1018 print_full_cxx_name (stream, jcf, name_index,
1019 signature_index, is_init, name_override,
1024 /* Print the unqualified method name followed by the signature. */
1026 DEFUN(print_full_cxx_name, (stream, jcf, name_index, signature_index,
1027 is_init, name_override, flags),
1028 FILE* stream AND JCF* jcf
1029 AND int name_index AND int signature_index AND int is_init
1030 AND const char *name_override AND int flags)
1032 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1033 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1034 register const unsigned char *str = str0;
1035 const unsigned char *limit = str + length;
1037 int is_method = str[0] == '(';
1038 const unsigned char *next;
1041 fputs (name_override, stream);
1042 else if (name_index)
1044 /* Declare constructors specially. */
1046 print_base_classname (stream, jcf, jcf->this_class);
1048 print_name (stream, jcf, name_index);
1053 unsigned char *signature = JPOOL_UTF_DATA (jcf, signature_index);
1054 int sig_len = JPOOL_UTF_LENGTH (jcf, signature_index);
1055 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf, name_index),
1056 JPOOL_UTF_LENGTH (jcf, name_index),
1057 signature, sig_len))
1059 /* If this method is overloaded by another native method,
1060 then include the argument information in the mangled
1062 unsigned char *limit = signature + sig_len;
1063 fputs ("__", stream);
1064 while (signature < limit)
1066 int ch = UTF8_GET (signature, limit);
1067 jni_print_char (stream, ch);
1079 /* Have a method or a constructor. Print signature pieces
1081 fputs (" (", stream);
1085 /* In JNI mode, add extra arguments. */
1088 /* FIXME: it would be nice to know if we are printing a decl
1089 or a definition, and only print `env' for the latter. */
1090 fputs ("JNIEnv *env", stream);
1092 fputs ((flags & ACC_STATIC) ? ", jclass" : ", jobject", stream);
1095 fputs (", ", stream);
1098 while (str < limit && *str != ')')
1100 next = decode_signature_piece (stream, str, limit, &need_space);
1103 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1108 if (next < limit && *next != ')')
1109 fputs (", ", stream);
1113 fputs (")", stream);
1117 /* This is a helper for print_stub_or_jni. */
1119 DEFUN (print_name_for_stub_or_jni, (stream, jcf, name_index, signature_index,
1120 is_init, name_override, flags),
1121 FILE *stream AND JCF *jcf
1122 AND int name_index AND int signature_index
1123 AND int is_init AND const char *name_override AND int flags)
1125 const char *const prefix = flag_jni ? "Java_" : "";
1126 print_cxx_classname (stream, prefix, jcf, jcf->this_class);
1127 fputs (flag_jni ? "_" : "::", stream);
1128 print_full_cxx_name (stream, jcf, name_index,
1129 signature_index, is_init, name_override,
1134 DEFUN(print_stub_or_jni, (stream, jcf, name_index, signature_index, is_init,
1135 name_override, flags),
1136 FILE* stream AND JCF* jcf
1137 AND int name_index AND int signature_index
1138 AND int is_init AND const char *name_override AND int flags)
1140 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1142 fprintf (stream, "<not a UTF8 constant>");
1147 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1148 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1149 register const unsigned char *str = str0;
1150 const unsigned char *limit = str + length;
1152 int is_method = str[0] == '(';
1153 const unsigned char *next;
1155 /* Don't print fields in the JNI case. */
1156 if (! is_method && flag_jni)
1159 if (flag_jni && ! stubs)
1160 fputs ("extern ", stream);
1162 /* If printing a method, skip to the return signature and print
1163 that first. However, there is no return value if this is a
1165 if (is_method && ! is_init)
1175 /* If printing a field or an ordinary method, then print the
1176 "return value" now. Note that a constructor can't be native,
1177 so we don't bother checking this in the JNI case. */
1178 if (! is_method || ! is_init)
1180 next = decode_signature_piece (stream, str, limit, &need_space);
1183 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1189 /* When printing a JNI header we need to respect the space. In
1190 other cases we're just going to insert a newline anyway. */
1191 fputs (need_space && ! stubs ? " " : "\n", stream);
1193 /* Now print the name of the thing. */
1194 print_name_for_stub_or_jni (stream, jcf, name_index,
1195 signature_index, is_init, name_override,
1198 /* Print the body. */
1202 fputs ("\n{\n (*env)->FatalError (\"", stream);
1204 fputs ("\n{\n JvFail (\"", stream);
1205 print_name_for_stub_or_jni (stream, jcf, name_index,
1206 signature_index, is_init,
1209 fputs (" not implemented\");\n}\n\n", stream);
1215 DEFUN(print_mangled_classname, (stream, jcf, prefix, index),
1216 FILE *stream AND JCF *jcf AND const char *prefix AND int index)
1218 int name_index = JPOOL_USHORT1 (jcf, index);
1219 fputs (prefix, stream);
1220 jcf_print_utf8_replace (out,
1221 JPOOL_UTF_DATA (jcf, name_index),
1222 JPOOL_UTF_LENGTH (jcf, name_index),
1226 /* Print PREFIX, then a class name in C++ format. If the name refers
1227 to an array, ignore it and don't print PREFIX. Returns 1 if
1228 something was printed, 0 otherwise. */
1230 print_cxx_classname (stream, prefix, jcf, index)
1236 int name_index = JPOOL_USHORT1 (jcf, index);
1238 const unsigned char *s, *p, *limit;
1240 s = JPOOL_UTF_DATA (jcf, name_index);
1241 len = JPOOL_UTF_LENGTH (jcf, name_index);
1244 /* Explicitly omit arrays here. */
1246 c = UTF8_GET (p, limit);
1250 fputs (prefix, stream);
1252 /* Print a leading "::" so we look in the right namespace. */
1253 if (! flag_jni && ! stubs)
1254 fputs ("::", stream);
1258 c = UTF8_GET (s, limit);
1260 fputs (flag_jni ? "_" : "::", stream);
1262 jni_print_char (stream, c);
1268 int written_class_count = 0;
1270 /* Return name of superclass. If LEN is not NULL, fill it with length
1272 static const unsigned char *
1273 super_class_name (derived_jcf, len)
1277 int supername_index = JPOOL_USHORT1 (derived_jcf, derived_jcf->super_class);
1278 int supername_length = JPOOL_UTF_LENGTH (derived_jcf, supername_index);
1279 const unsigned char *supername =
1280 JPOOL_UTF_DATA (derived_jcf, supername_index);
1283 *len = supername_length;
1290 /* We keep track of all the `#include's we generate, so we can avoid
1295 struct include *next;
1298 /* List of all includes. */
1299 static struct include *all_includes = NULL;
1301 /* Generate a #include. */
1303 print_include (out, utf8, len)
1305 const unsigned char *utf8;
1308 struct include *incl;
1314 len = strlen (utf8);
1316 for (incl = all_includes; incl; incl = incl->next)
1318 /* We check the length because we might have a proper prefix. */
1319 if (len == (int) strlen (incl->name)
1320 && ! strncmp (incl->name, utf8, len))
1324 incl = (struct include *) xmalloc (sizeof (struct include));
1325 incl->name = xmalloc (len + 1);
1326 strncpy (incl->name, utf8, len);
1327 incl->name[len] = '\0';
1328 incl->next = all_includes;
1329 all_includes = incl;
1331 fputs ("#include <", out);
1332 jcf_print_utf8_replace (out, utf8, len,
1334 flag_jni ? '_' : '/');
1335 fputs (".h>\n", out);
1340 /* This is used to represent part of a package or class name. */
1343 /* The text of this part of the name. */
1345 /* True if this represents a class. */
1347 /* Linked list of all classes and packages inside this one. */
1348 struct namelet *subnamelets;
1349 /* Pointer to next sibling. */
1350 struct namelet *next;
1353 static void add_namelet PARAMS ((const unsigned char *,
1354 const unsigned char *, struct namelet *));
1355 static void print_namelet PARAMS ((FILE *, struct namelet *, int));
1357 /* The special root namelet. */
1358 static struct namelet root =
1366 /* This extracts the next name segment from the full UTF-8 encoded
1367 package or class name and links it into the tree. It does this
1370 add_namelet (name, name_limit, parent)
1371 const unsigned char *name, *name_limit;
1372 struct namelet *parent;
1374 const unsigned char *p;
1375 struct namelet *n = NULL, *np;
1377 /* We want to skip the standard namespaces that we assume the
1378 runtime already knows about. We only do this at the top level,
1379 though, hence the check for `root'. */
1380 if (parent == &root)
1382 #define JAVALANG "java/lang/"
1383 #define JAVAIO "java/io/"
1384 #define JAVAUTIL "java/util/"
1385 if ((name_limit - name >= (int) sizeof (JAVALANG) - 1
1386 && ! strncmp (name, JAVALANG, sizeof (JAVALANG) - 1))
1387 || (name_limit - name >= (int) sizeof (JAVAUTIL) - 1
1388 && ! strncmp (name, JAVAUTIL, sizeof (JAVAUTIL) - 1))
1389 || (name_limit - name >= (int) sizeof (JAVAIO) - 1
1390 && ! strncmp (name, JAVAIO, sizeof (JAVAIO) - 1)))
1394 for (p = name; p < name_limit && *p != '/' && *p != '$'; ++p)
1397 /* Search for this name beneath the PARENT node. */
1398 for (np = parent->subnamelets; np != NULL; np = np->next)
1400 /* We check the length because we might have a proper prefix. */
1401 if ((int) strlen (np->name) == p - name &&
1402 ! strncmp (name, np->name, p - name))
1411 n = (struct namelet *) xmalloc (sizeof (struct namelet));
1412 n->name = xmalloc (p - name + 1);
1413 strncpy (n->name, name, p - name);
1414 n->name[p - name] = '\0';
1415 n->is_class = (p == name_limit || *p == '$');
1416 n->subnamelets = NULL;
1417 n->next = parent->subnamelets;
1418 parent->subnamelets = n;
1421 /* We recurse if there is more text, and if the trailing piece does
1422 not represent an inner class. */
1423 if (p < name_limit && *p != '$')
1424 add_namelet (p + 1, name_limit, n);
1427 /* Print a single namelet. Destroys namelets while printing. */
1429 print_namelet (out, name, depth)
1431 struct namelet *name;
1439 for (i = 0; i < depth; ++i)
1441 fprintf (out, "%s %s", name->is_class ? "class" : "namespace",
1443 if (name->is_class && name->subnamelets == NULL)
1449 for (i = 0; i < depth; ++i)
1455 c = name->subnamelets;
1458 struct namelet *next = c->next;
1459 print_namelet (out, c, depth + 2);
1467 for (i = 0; i < depth; ++i)
1470 /* Only print a `;' when printing a class. C++ is evil. */
1480 /* This is called to add some classes to the list of classes for which
1481 we need decls. The signature argument can be a function
1484 add_class_decl (out, jcf, signature)
1489 const unsigned char *s = JPOOL_UTF_DATA (jcf, signature);
1490 int len = JPOOL_UTF_LENGTH (jcf, signature);
1492 /* Name of class we are processing. */
1493 int name_index = JPOOL_USHORT1 (jcf, jcf->this_class);
1494 int tlen = JPOOL_UTF_LENGTH (jcf, name_index);
1495 const char *tname = JPOOL_UTF_DATA (jcf, name_index);
1497 for (i = 0; i < len; ++i)
1499 int start, saw_dollar;
1501 /* If we see an array, then we include the array header. */
1504 print_include (out, "gcj/array", -1);
1508 /* We're looking for `L<stuff>;' -- everything else is
1514 for (start = ++i; i < len && s[i] != ';'; ++i)
1516 if (! saw_dollar && s[i] == '$' && out)
1519 /* If this class represents an inner class, then
1520 generate a `#include' for the outer class. However,
1521 don't generate the include if the outer class is the
1522 class we are processing. */
1523 if (i - start < tlen || strncmp (&s[start], tname, i - start))
1524 print_include (out, &s[start], i - start);
1529 /* If we saw an inner class, then the generated #include will
1530 declare the class. So in this case we needn't bother. */
1532 add_namelet (&s[start], &s[i], &root);
1536 /* Print declarations for all classes required by this class. Any
1537 class or package in the `java' package is assumed to be handled
1538 statically in libjava; we don't generate declarations for these.
1539 This makes the generated headers a bit easier to read. */
1541 print_class_decls (out, jcf, self)
1546 /* Make sure to always add the current class to the list of things
1547 that should be declared. */
1548 int name_index = JPOOL_USHORT1 (jcf, self);
1550 const unsigned char *s;
1552 s = JPOOL_UTF_DATA (jcf, name_index);
1553 len = JPOOL_UTF_LENGTH (jcf, name_index);
1554 add_namelet (s, s + len, &root);
1556 if (root.subnamelets)
1558 fputs ("extern \"Java\"\n{\n", out);
1559 /* We use an initial offset of 0 because the root namelet
1560 doesn't cause anything to print. */
1561 print_namelet (out, &root, 0);
1562 fputs ("};\n\n", out);
1569 DEFUN(process_file, (jcf, out),
1570 JCF *jcf AND FILE *out)
1573 uint32 field_start, method_end, method_start;
1579 if (jcf_parse_preamble (jcf) != 0)
1581 fprintf (stderr, "Not a valid Java .class file.\n");
1586 /* Parse and possibly print constant pool */
1587 code = jcf_parse_constant_pool (jcf);
1590 fprintf (stderr, "error while parsing constant pool\n");
1594 code = verify_constant_pool (jcf);
1597 fprintf (stderr, "error in constant pool entry #%d\n", code);
1602 jcf_parse_class (jcf);
1604 if (written_class_count++ == 0 && out)
1606 const char *cstart, *cstart2, *mode, *cend, *what, *jflag;
1621 mode = " -*- c++ -*-";
1627 fprintf (out, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
1628 cstart, mode, cend);
1631 fprintf (out, "%s This file was created by `gcjh -stubs%s'.%s\n\
1633 %s This file is intended to give you a head start on implementing native\n\
1634 %s methods using %s.\n\
1635 %s Be aware: running `gcjh -stubs %s' once more for this class may\n\
1636 %s overwrite any edits you have made to this file.%s\n\n",
1637 cstart, jflag, mode,
1653 print_mangled_classname (out, jcf, "#ifndef __", jcf->this_class);
1654 fprintf (out, "__\n");
1656 print_mangled_classname (out, jcf, "#define __", jcf->this_class);
1657 fprintf (out, "__\n\n");
1661 fprintf (out, "#include <jni.h>\n\n");
1662 fprintf (out, "#ifdef __cplusplus\n");
1663 fprintf (out, "extern \"C\"\n");
1664 fprintf (out, "{\n");
1665 fprintf (out, "#endif\n");
1669 /* We do this to ensure that inline methods won't be
1670 `outlined' by g++. This works as long as method and
1671 fields are not added by the user. */
1672 fprintf (out, "#pragma interface\n");
1674 if (jcf->super_class)
1677 const unsigned char *supername =
1678 super_class_name (jcf, &super_length);
1681 print_include (out, supername, super_length);
1687 /* Strip off the ".class" portion of the name when printing
1688 the include file name. */
1689 int len = strlen (jcf->classname);
1690 if (len > 6 && ! strcmp (&jcf->classname[len - 6], ".class"))
1692 print_include (out, jcf->classname, len);
1693 print_include (out, "gcj/cni", -1);
1697 /* We want to parse the methods first. But we need to find where
1698 they start. So first we skip the fields, then parse the methods.
1699 Then we parse the fields and skip the methods. This is ugly, but
1700 not too bad since we need two full passes to get class decl
1701 information anyway. */
1703 field_start = JCF_TELL (jcf);
1704 jcf_parse_fields (jcf);
1706 method_start = JCF_TELL (jcf);
1708 jcf_parse_methods (jcf);
1713 if (out && ! flag_jni)
1716 print_class_decls (out, jcf, jcf->this_class);
1718 for (i = 0; i < prepend_count; ++i)
1719 fprintf (out, "%s\n", prepend_specs[i]);
1720 if (prepend_count > 0)
1725 if (! print_cxx_classname (out, "class ", jcf, jcf->this_class))
1727 fprintf (stderr, "class is of array type\n");
1731 if (jcf->super_class)
1733 if (! print_cxx_classname (out, " : public ",
1734 jcf, jcf->super_class))
1736 fprintf (stderr, "base class is of array type\n");
1742 fputs ("\n{\n", out);
1746 /* Now go back for second pass over methods and fields. */
1747 JCF_SEEK (jcf, method_start);
1749 jcf_parse_methods (jcf);
1750 method_end = JCF_TELL (jcf);
1753 JCF_SEEK (jcf, field_start);
1754 jcf_parse_fields (jcf);
1755 JCF_SEEK (jcf, method_end);
1757 jcf_parse_final_attributes (jcf);
1763 fprintf (out, "\n#ifdef __cplusplus\n");
1764 fprintf (out, "}\n");
1765 fprintf (out, "#endif\n");
1769 /* Generate friend decl if we still must. */
1770 for (i = 0; i < friend_count; ++i)
1771 fprintf (out, " friend %s\n", friend_specs[i]);
1773 /* Generate extra declarations. */
1776 for (i = 0; i < add_count; ++i)
1777 fprintf (out, " %s\n", add_specs[i]);
1780 fputs ("};\n", out);
1782 if (append_count > 0)
1784 for (i = 0; i < append_count; ++i)
1785 fprintf (out, "%s\n", append_specs[i]);
1788 print_mangled_classname (out, jcf,
1789 "\n#endif /* __", jcf->this_class);
1790 fprintf (out, "__ */\n");
1796 /* This is used to mark options with no short value. */
1797 #define LONG_OPT(Num) ((Num) + 128)
1799 #define OPT_classpath LONG_OPT (0)
1800 #define OPT_CLASSPATH LONG_OPT (1)
1801 #define OPT_HELP LONG_OPT (2)
1802 #define OPT_TEMP LONG_OPT (3)
1803 #define OPT_VERSION LONG_OPT (4)
1804 #define OPT_PREPEND LONG_OPT (5)
1805 #define OPT_FRIEND LONG_OPT (6)
1806 #define OPT_ADD LONG_OPT (7)
1807 #define OPT_APPEND LONG_OPT (8)
1808 #define OPT_M LONG_OPT (9)
1809 #define OPT_MM LONG_OPT (10)
1810 #define OPT_MG LONG_OPT (11)
1811 #define OPT_MD LONG_OPT (12)
1812 #define OPT_MMD LONG_OPT (13)
1814 static struct option options[] =
1816 { "classpath", required_argument, NULL, OPT_classpath },
1817 { "CLASSPATH", required_argument, NULL, OPT_CLASSPATH },
1818 { "help", no_argument, NULL, OPT_HELP },
1819 { "stubs", no_argument, &stubs, 1 },
1820 { "td", required_argument, NULL, OPT_TEMP },
1821 { "verbose", no_argument, NULL, 'v' },
1822 { "version", no_argument, NULL, OPT_VERSION },
1823 { "prepend", required_argument, NULL, OPT_PREPEND },
1824 { "friend", required_argument, NULL, OPT_FRIEND },
1825 { "add", required_argument, NULL, OPT_ADD },
1826 { "append", required_argument, NULL, OPT_APPEND },
1827 { "M", no_argument, NULL, OPT_M },
1828 { "MM", no_argument, NULL, OPT_MM },
1829 { "MG", no_argument, NULL, OPT_MG },
1830 { "MD", no_argument, NULL, OPT_MD },
1831 { "MMD", no_argument, NULL, OPT_MMD },
1832 { "jni", no_argument, &flag_jni, 1 },
1833 { NULL, no_argument, NULL, 0 }
1839 fprintf (stderr, "Try `gcjh --help' for more information.\n");
1846 printf ("Usage: gcjh [OPTION]... CLASS...\n\n");
1847 printf ("Generate C++ header files from .class files\n\n");
1848 printf (" -stubs Generate an implementation stub file\n");
1849 printf (" -jni Generate a JNI header or stub\n");
1851 printf (" -add TEXT Insert TEXT into class body\n");
1852 printf (" -append TEXT Insert TEXT after class declaration\n");
1853 printf (" -friend TEXT Insert TEXT as `friend' declaration\n");
1854 printf (" -prepend TEXT Insert TEXT before start of class\n");
1856 printf (" --classpath PATH Set path to find .class files\n");
1857 printf (" --CLASSPATH PATH Set path to find .class files\n");
1858 printf (" -IDIR Append directory to class path\n");
1859 printf (" -d DIRECTORY Set output directory name\n");
1860 printf (" -o FILE Set output file name\n");
1861 printf (" -td DIRECTORY Set temporary directory name\n");
1863 printf (" --help Print this help, then exit\n");
1864 printf (" --version Print version number, then exit\n");
1865 printf (" -v, --verbose Print extra information while running\n");
1867 printf (" -M Print all dependencies to stdout;\n");
1868 printf (" suppress ordinary output\n");
1869 printf (" -MM Print non-system dependencies to stdout;\n");
1870 printf (" suppress ordinary output\n");
1871 printf (" -MD Print all dependencies to stdout\n");
1872 printf (" -MMD Print non-system dependencies to stdout\n");
1873 /* We omit -MG until it is implemented. */
1875 printf ("For bug reporting instructions, please see:\n");
1876 printf ("%s.\n", GCCBUGURL);
1883 printf ("gcjh (%s)\n\n", version_string);
1884 printf ("Copyright (C) 1998, 1999 Free Software Foundation, Inc.\n");
1885 printf ("This is free software; see the source for copying conditions. There is NO\n");
1886 printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
1891 DEFUN(main, (argc, argv),
1892 int argc AND char** argv)
1896 char *output_file = NULL;
1897 int emit_dependencies = 0, suppress_output = 0;
1902 fprintf (stderr, "gcjh: no classes specified\n");
1908 /* We use getopt_long_only to allow single `-' long options. For
1909 some of our options this is more natural. */
1910 while ((opt = getopt_long_only (argc, argv, "I:d:o:v", options, NULL)) != -1)
1915 /* Already handled. */
1919 output_file = optarg;
1923 output_directory = optarg;
1927 jcf_path_include_arg (optarg);
1935 jcf_path_classpath_arg (optarg);
1939 jcf_path_CLASSPATH_arg (optarg);
1947 temp_directory = optarg;
1955 if (prepend_count == 0)
1956 prepend_specs = (char**) ALLOC (argc * sizeof (char*));
1957 prepend_specs[prepend_count++] = optarg;
1961 if (friend_count == 0)
1962 friend_specs = (char**) ALLOC (argc * sizeof (char*));
1963 friend_specs[friend_count++] = optarg;
1968 add_specs = (char**) ALLOC (argc * sizeof (char*));
1969 add_specs[add_count++] = optarg;
1973 if (append_count == 0)
1974 append_specs = (char**) ALLOC (argc * sizeof (char*));
1975 append_specs[append_count++] = optarg;
1979 emit_dependencies = 1;
1980 suppress_output = 1;
1981 jcf_dependency_init (1);
1985 emit_dependencies = 1;
1986 suppress_output = 1;
1987 jcf_dependency_init (0);
1991 fprintf (stderr, "gcjh: `-MG' option is unimplemented\n");
1995 emit_dependencies = 1;
1996 jcf_dependency_init (1);
2000 emit_dependencies = 1;
2001 jcf_dependency_init (0);
2012 fprintf (stderr, "gcjh: no classes specified\n");
2018 if (output_file && emit_dependencies)
2020 fprintf (stderr, "gcjh: can't specify both -o and -MD\n");
2024 for (argi = optind; argi < argc; argi++)
2026 char *classname = argv[argi];
2027 char *current_output_file;
2028 const char *classfile_name;
2031 fprintf (stderr, "Processing %s\n", classname);
2033 jcf_dependency_reset ();
2034 classfile_name = find_class (classname, strlen (classname), &jcf, 0);
2035 if (classfile_name == NULL)
2037 fprintf (stderr, "%s: no such class\n", classname);
2041 fprintf (stderr, "Found in %s\n", classfile_name);
2044 if (strcmp (output_file, "-") == 0)
2046 else if (out == NULL)
2048 out = fopen (output_file, "w");
2052 perror (output_file);
2055 current_output_file = output_file;
2059 int dir_len = strlen (output_directory);
2060 int i, classname_length = strlen (classname);
2061 current_output_file = (char*) ALLOC (dir_len + classname_length + 5);
2062 strcpy (current_output_file, output_directory);
2063 if (dir_len > 0 && output_directory[dir_len-1] != '/')
2064 current_output_file[dir_len++] = '/';
2065 for (i = 0; classname[i] != '\0'; i++)
2067 char ch = classname[i];
2070 if (flag_jni && ch == '/')
2072 current_output_file[dir_len++] = ch;
2074 if (emit_dependencies)
2076 if (suppress_output)
2078 jcf_dependency_set_dep_file ("-");
2083 /* We use `.hd' and not `.d' to avoid clashes with
2084 dependency tracking from straight compilation. */
2085 strcpy (current_output_file + dir_len, ".hd");
2086 jcf_dependency_set_dep_file (current_output_file);
2089 strcpy (current_output_file + dir_len,
2090 stubs ? (flag_jni ? ".c" : ".cc") : ".h");
2091 jcf_dependency_set_target (current_output_file);
2092 if (! suppress_output)
2094 out = fopen (current_output_file, "w");
2097 perror (current_output_file);
2102 process_file (&jcf, out);
2104 if (current_output_file != output_file)
2105 free (current_output_file);
2106 jcf_dependency_write ();
2109 if (out != NULL && out != stdout)