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 /* When non zero, warn when source file is newer than matching class
54 /* Directory to place resulting files in. Set by -d option. */
55 const char *output_directory = "";
57 /* Directory to place temporary file. Set by -td option. Currently unused. */
58 const char *temp_directory = "/tmp";
60 /* Number of friend functions we have to declare. */
61 static int friend_count;
63 /* A class can optionally have a `friend' function declared. If
64 non-NULL, this is that function. */
65 static char **friend_specs = NULL;
67 /* Number of lines we are prepending before the class. */
68 static int prepend_count;
70 /* We can prepend extra lines before the class's start. */
71 static char **prepend_specs = NULL;
73 /* Number of lines we are appending at the end of the class. */
76 /* We can append extra lines just before the class's end. */
77 static char **add_specs = NULL;
79 /* Number of lines we are appending after the class. */
80 static int append_count;
82 /* We can append extra lines after the class's end. */
83 static char **append_specs = NULL;
89 struct JCF *current_jcf;
91 /* This holds access information for the last field we examined. They
92 let us generate "private:", "public:", and "protected:" properly.
93 If 0 then we haven't previously examined any field. */
94 static JCF_u2 last_access;
96 /* Pass this macro the flags for a class and for a method. It will
97 return true if the method should be considered `final'. */
98 #define METHOD_IS_FINAL(Class, Method) \
99 (((Class) & ACC_FINAL) || ((Method) & (ACC_FINAL | ACC_PRIVATE)))
101 /* Pass this macro the flags for a method. It will return true if the
103 #define METHOD_IS_NATIVE(Method) \
104 ((Method) & ACC_NATIVE)
106 /* We keep a linked list of all method names we have seen. This lets
107 us determine if a method name and a field name are in conflict. */
112 unsigned char *signature;
114 struct method_name *next;
117 /* List of method names we've seen. */
118 static struct method_name *method_name_list;
120 static void print_field_info PARAMS ((FILE*, JCF*, int, int, JCF_u2));
121 static void print_mangled_classname PARAMS ((FILE*, JCF*, const char*, int));
122 static int print_cxx_classname PARAMS ((FILE*, const char*, JCF*, int));
123 static void print_method_info PARAMS ((FILE*, JCF*, int, int, JCF_u2, int));
124 static void print_c_decl PARAMS ((FILE*, JCF*, int, int, int, const char *,
126 static void print_stub_or_jni PARAMS ((FILE*, JCF*, int, int, int,
128 static void print_full_cxx_name PARAMS ((FILE*, JCF*, int, int, int,
130 static void decompile_method PARAMS ((FILE*, JCF*, int));
131 static void add_class_decl PARAMS ((FILE*, JCF*, JCF_u2));
133 static int java_float_finite PARAMS ((jfloat));
134 static int java_double_finite PARAMS ((jdouble));
135 static void print_name PARAMS ((FILE *, JCF *, int));
136 static void print_base_classname PARAMS ((FILE *, JCF *, int));
137 static int utf8_cmp PARAMS ((const unsigned char *, int, const char *));
138 static char *cxx_keyword_subst PARAMS ((const unsigned char *, int));
139 static void generate_access PARAMS ((FILE *, JCF_u2));
140 static int name_is_method_p PARAMS ((const unsigned char *, int));
141 static char *get_field_name PARAMS ((JCF *, int, JCF_u2));
142 static void print_field_name PARAMS ((FILE *, JCF *, int, JCF_u2));
143 static const unsigned char *super_class_name PARAMS ((JCF *, int *));
144 static void print_include PARAMS ((FILE *, const unsigned char *, int));
145 static const unsigned char *decode_signature_piece
146 PARAMS ((FILE *, const unsigned char *, const unsigned char *, int *));
147 static void print_class_decls PARAMS ((FILE *, JCF *, int));
148 static void usage PARAMS ((void)) ATTRIBUTE_NORETURN;
149 static void help PARAMS ((void)) ATTRIBUTE_NORETURN;
150 static void version PARAMS ((void)) ATTRIBUTE_NORETURN;
151 static int overloaded_jni_method_exists_p PARAMS ((const unsigned char *, int,
153 static void jni_print_char PARAMS ((FILE *, int));
155 JCF_u2 current_field_name;
156 JCF_u2 current_field_value;
157 JCF_u2 current_field_signature;
158 JCF_u2 current_field_flags;
160 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
161 ( current_field_name = (NAME), current_field_signature = (SIGNATURE), \
162 current_field_flags = (ACCESS_FLAGS), current_field_value = 0)
164 /* We pass over fields twice. The first time we just note the types
165 of the fields and then the start of the methods. Then we go back
166 and parse the fields for real. This is ugly. */
167 static int field_pass;
168 /* Likewise we pass over methods twice. The first time we generate
169 class decl information; the second time we generate actual method
171 static int method_pass;
173 #define HANDLE_END_FIELD() \
176 if (out && ! stubs && ! flag_jni) \
177 print_field_info (out, jcf, current_field_name, \
178 current_field_signature, \
179 current_field_flags); \
181 else if (! stubs && ! flag_jni) \
182 add_class_decl (out, jcf, current_field_signature);
184 #define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)
186 static int method_declared = 0;
187 static int method_access = 0;
188 static int method_printed = 0;
189 static int method_synthetic = 0;
190 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
192 method_synthetic = 0; \
193 if (ATTRIBUTE_COUNT) \
194 method_synthetic = peek_attribute (jcf, ATTRIBUTE_COUNT, \
195 (const char *)"Synthetic", 9); \
196 /* If a synthetic methods have been declared, its attribute aren't \
197 worth reading (and triggering side-effects). We skip them an \
198 set ATTRIBUTE_COUNT to zero so that they'll be skipped in \
199 jcf_parse_one_method. */ \
200 if (method_synthetic) \
202 skip_attribute (jcf, ATTRIBUTE_COUNT); \
203 ATTRIBUTE_COUNT = 0; \
205 if (method_pass && !method_synthetic) \
207 decompiled = 0; method_printed = 0; \
209 print_method_info (out, jcf, NAME, SIGNATURE, \
210 ACCESS_FLAGS, method_synthetic); \
212 else if (!method_synthetic) \
214 print_method_info (NULL, jcf, NAME, SIGNATURE, \
215 ACCESS_FLAGS, method_synthetic); \
216 if (! stubs && ! flag_jni) \
217 add_class_decl (out, jcf, SIGNATURE); \
221 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
222 if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
224 static int decompiled = 0;
225 #define HANDLE_END_METHOD() \
226 if (out && method_printed && !method_synthetic) \
227 fputs (decompiled || stubs ? "\n" : ";\n", out);
229 /* We're going to need {peek,skip}_attribute, enable their definition. */
230 #define NEED_PEEK_ATTRIBUTE
231 #define NEED_SKIP_ATTRIBUTE
233 #include "jcf-reader.c"
235 /* Some useful constants. */
236 #define F_NAN_MASK 0x7f800000
237 #if (1 == HOST_FLOAT_WORDS_BIG_ENDIAN) && ! defined (HOST_WORDS_BIG_ENDIAN)
238 #define D_NAN_MASK 0x000000007ff00000LL
240 #define D_NAN_MASK 0x7ff0000000000000LL
243 /* Return 1 if F is not Inf or NaN. */
245 java_float_finite (f)
251 /* We happen to know that F_NAN_MASK will match all NaN values, and
252 also positive and negative infinity. That's why we only need one
253 test here. See The Java Language Specification, section 20.9. */
254 return (u.i & F_NAN_MASK) != F_NAN_MASK;
257 /* Return 1 if D is not Inf or NaN. */
259 java_double_finite (d)
265 /* Now check for all NaNs. */
266 return (u.l & D_NAN_MASK) != D_NAN_MASK;
269 /* Print a character, appropriately mangled for JNI. */
272 jni_print_char (stream, ch)
277 jcf_print_char (stream, ch);
278 else if (ch == '(' || ch == ')')
283 fputs ("_1", stream);
285 fputs ("_2", stream);
287 fputs ("_3", stream);
290 else if ((ch >= '0' && ch <= '9')
291 || (ch >= 'a' && ch <= 'z')
292 || (ch >= 'A' && ch <= 'Z'))
296 /* "Unicode" character. */
297 fprintf (stream, "_0%04x", ch);
301 /* Print a name from the class data. If the index does not point to a
302 string, an error results. */
305 DEFUN(print_name, (stream, jcf, name_index),
306 FILE* stream AND JCF* jcf AND int name_index)
308 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
310 fprintf (stream, "<not a UTF8 constant>");
314 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
315 JPOOL_UTF_LENGTH (jcf, name_index));
318 /* For JNI we must correctly quote each character. */
319 const unsigned char *str = JPOOL_UTF_DATA (jcf, name_index);
320 int length = JPOOL_UTF_LENGTH (jcf, name_index);
321 const unsigned char *limit = str + length;
324 int ch = UTF8_GET (str, limit);
327 fprintf (stream, "\\<invalid>");
330 jni_print_char (stream, ch);
335 /* Print base name of class. The base name is everything after the
339 print_base_classname (stream, jcf, index)
344 int name_index = JPOOL_USHORT1 (jcf, index);
346 const unsigned char *s, *p, *limit;
348 s = JPOOL_UTF_DATA (jcf, name_index);
349 len = JPOOL_UTF_LENGTH (jcf, name_index);
354 int c = UTF8_GET (s, limit);
361 int ch = UTF8_GET (p, limit);
363 fputs ("::", stream);
365 jcf_print_char (stream, ch);
369 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
370 and 1 if STR is "greater" than NAME. */
373 utf8_cmp (str, length, name)
374 const unsigned char *str;
378 const unsigned char *limit = str + length;
381 for (i = 0; name[i]; ++i)
383 int ch = UTF8_GET (str, limit);
388 return str == limit ? 0 : 1;
391 /* This is a sorted list of all C++ keywords. */
393 static const char *cxx_keywords[] =
429 /* If NAME is the name of a C++ keyword, then return an override name.
430 This is a name that can be used in place of the keyword.
431 Otherwise, return NULL. The return value is malloc()d. */
434 cxx_keyword_subst (str, length)
435 const unsigned char *str;
438 int last = ARRAY_SIZE (cxx_keywords);
440 int mid = (last + first) / 2;
443 for (mid = (last + first) / 2;
445 old = mid, mid = (last + first) / 2)
447 int kwl = strlen (cxx_keywords[mid]);
448 int min_length = kwl > length ? length : kwl;
449 int r = utf8_cmp (str, min_length, cxx_keywords[mid]);
455 /* Skip all trailing `$'. */
456 for (i = min_length; i < length && str[i] == '$'; ++i)
458 /* We've only found a match if all the remaining characters
462 char *dup = xmalloc (2 + length - min_length + kwl);
463 strcpy (dup, cxx_keywords[mid]);
464 for (i = kwl; i < length + 1; ++i)
480 /* Generate an access control keyword based on FLAGS. */
483 generate_access (stream, flags)
487 if ((flags & ACC_VISIBILITY) == last_access)
489 last_access = (flags & ACC_VISIBILITY);
494 fputs ("public: // actually package-private\n", stream);
497 fputs ("public:\n", stream);
500 fputs ("private:\n", stream);
503 fputs ("public: // actually protected\n", stream);
507 fprintf (stream, "#error unrecognized visibility %d\n",
508 (flags & ACC_VISIBILITY));
513 /* See if NAME is already the name of a method. */
515 name_is_method_p (name, length)
516 const unsigned char *name;
519 struct method_name *p;
521 for (p = method_name_list; p != NULL; p = p->next)
523 if (p->length == length && ! memcmp (p->name, name, length))
529 /* If there is already a method named NAME, whose signature is not
530 SIGNATURE, then return true. Otherwise return false. */
532 overloaded_jni_method_exists_p (name, length, signature, sig_length)
533 const unsigned char *name;
535 const char *signature;
538 struct method_name *p;
540 for (p = method_name_list; p != NULL; p = p->next)
542 if (p->length == length
543 && ! memcmp (p->name, name, length)
544 && (p->sig_length != sig_length
545 || memcmp (p->signature, signature, sig_length)))
551 /* Get name of a field. This handles renamings due to C++ clash. */
553 get_field_name (jcf, name_index, flags)
558 unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
559 int length = JPOOL_UTF_LENGTH (jcf, name_index);
562 if (name_is_method_p (name, length))
564 /* This field name matches a method. So override the name with
565 a dummy name. This is yucky, but it isn't clear what else to
566 do. FIXME: if the field is static, then we'll be in real
568 if ((flags & ACC_STATIC))
570 fprintf (stderr, "static field has same name as method\n");
575 override = xmalloc (length + 3);
576 memcpy (override, name, length);
577 strcpy (override + length, "__");
580 override = cxx_keyword_subst (name, length);
585 /* Print a field name. Convenience function for use with
588 print_field_name (stream, jcf, name_index, flags)
594 char *override = get_field_name (jcf, name_index, flags);
598 fputs (override, stream);
602 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
603 JPOOL_UTF_LENGTH (jcf, name_index));
607 DEFUN(print_field_info, (stream, jcf, name_index, sig_index, flags),
608 FILE *stream AND JCF* jcf
609 AND int name_index AND int sig_index AND JCF_u2 flags)
611 char *override = NULL;
613 generate_access (stream, flags);
614 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
616 fprintf (stream, "<not a UTF8 constant>");
622 if ((flags & ACC_STATIC))
623 fputs ("static ", out);
625 if ((flags & ACC_FINAL))
627 if (current_field_value > 0)
632 switch (JPOOL_TAG (jcf, current_field_value))
634 case CONSTANT_Integer:
637 int most_negative = 0;
638 fputs ("const jint ", out);
639 print_field_name (out, jcf, name_index, 0);
641 num = JPOOL_INT (jcf, current_field_value);
642 /* We single out the most negative number to print
643 specially. This avoids later warnings from g++. */
644 if (num == (jint) 0x80000000)
649 format_int (buffer, (jlong) num, 10);
650 fprintf (out, "%sL%s;\n", buffer, most_negative ? " - 1" : "");
656 int most_negative = 0;
657 fputs ("const jlong ", out);
658 print_field_name (out, jcf, name_index, 0);
660 num = JPOOL_LONG (jcf, current_field_value);
661 /* We single out the most negative number to print
662 specially.. This avoids later warnings from g++. */
663 if (num == (jlong) 0x8000000000000000LL)
668 format_int (buffer, num, 10);
669 fprintf (out, "%sLL%s;\n", buffer, most_negative ? " - 1" :"");
674 jfloat fnum = JPOOL_FLOAT (jcf, current_field_value);
675 fputs ("const jfloat ", out);
676 print_field_name (out, jcf, name_index, 0);
677 if (! java_float_finite (fnum))
680 fprintf (out, " = %.10g;\n", fnum);
683 case CONSTANT_Double:
685 jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value);
686 fputs ("const jdouble ", out);
687 print_field_name (out, jcf, name_index, 0);
688 if (! java_double_finite (dnum))
691 fprintf (out, " = %.17g;\n", dnum);
695 /* We can't print this as a constant, but we can still
696 print something sensible. */
706 override = get_field_name (jcf, name_index, flags);
707 print_c_decl (out, jcf, name_index, sig_index, 0, override, flags);
716 DEFUN(print_method_info, (stream, jcf, name_index, sig_index, flags, synth),
717 FILE *stream AND JCF* jcf
718 AND int name_index AND int sig_index AND JCF_u2 flags AND int synth)
720 const unsigned char *str;
721 int length, is_init = 0;
722 char *override = NULL;
725 method_access = flags;
726 if (stream && JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
727 fprintf (stream, "<not a UTF8 constant>");
728 str = JPOOL_UTF_DATA (jcf, name_index);
729 length = JPOOL_UTF_LENGTH (jcf, name_index);
731 /* Ignore synthetic methods. */
737 /* Ignore the internally generated method <clinit>. However,
738 treat <init> as a constructor. */
739 if (! utf8_cmp (str, length, "<init>"))
741 else if (! METHOD_IS_FINAL (jcf->access_flags, flags)
742 && ! (flags & ACC_STATIC))
744 /* FIXME: i18n bug here. Order of prints should not be
746 fprintf (stderr, "ignored method `");
747 jcf_print_utf8 (stderr, str, length);
748 fprintf (stderr, "' marked virtual\n");
756 /* During the first method pass, build a list of method names. This will
757 be used to determine if field names conflict with method names. */
760 struct method_name *nn;
762 nn = (struct method_name *) xmalloc (sizeof (struct method_name));
763 nn->name = (char *) xmalloc (length);
764 memcpy (nn->name, str, length);
766 nn->next = method_name_list;
767 nn->sig_length = JPOOL_UTF_LENGTH (jcf, sig_index);
768 nn->signature = (char *) xmalloc (nn->sig_length);
769 memcpy (nn->signature, JPOOL_UTF_DATA (jcf, sig_index),
771 method_name_list = nn;
773 /* The rest of this function doesn't matter. */
777 /* We don't worry about overrides in JNI mode. */
780 /* We can't generate a method whose name is a C++ reserved word.
781 We can't just ignore the function, because that will cause
782 incorrect code to be generated if the function is virtual
783 (not only for calls to this function for for other functions
784 after it in the vtbl). So we give it a dummy name instead. */
785 override = cxx_keyword_subst (str, length);
788 if (! stubs && ! flag_jni)
792 generate_access (stream, flags);
795 if ((flags & ACC_STATIC))
796 fputs ("static ", out);
797 else if (! METHOD_IS_FINAL (jcf->access_flags, flags))
799 /* Don't print `virtual' if we have a constructor. */
801 fputs ("virtual ", out);
803 print_c_decl (out, jcf, name_index, sig_index, is_init, override, flags);
805 if ((flags & ACC_ABSTRACT))
812 if (METHOD_IS_NATIVE (flags))
815 print_stub_or_jni (out, jcf, name_index, sig_index,
816 is_init, override, flags);
824 /* Try to decompile a method body. Right now we just try to handle a
825 simple case that we can do. Expand as desired. */
827 decompile_method (out, jcf, code_len)
832 const unsigned char *codes = jcf->read_ptr;
834 uint16 name_and_type, name;
836 /* If the method is synchronized, don't touch it. */
837 if ((method_access & ACC_SYNCHRONIZED))
841 && codes[0] == OPCODE_aload_0
842 && codes[1] == OPCODE_getfield
843 && (codes[4] == OPCODE_areturn
844 || codes[4] == OPCODE_dreturn
845 || codes[4] == OPCODE_freturn
846 || codes[4] == OPCODE_ireturn
847 || codes[4] == OPCODE_lreturn))
849 /* Found code like `return FIELD'. */
850 fputs (" { return ", out);
851 index = (codes[2] << 8) | codes[3];
852 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
853 /* FIXME: ensure that the field's class is this class. */
854 name_and_type = JPOOL_USHORT2 (jcf, index);
855 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
856 name = JPOOL_USHORT1 (jcf, name_and_type);
858 print_field_name (out, jcf, name, 0);
862 else if (code_len == 2
863 && codes[0] == OPCODE_aload_0
864 && codes[1] == OPCODE_areturn)
866 /* Found `return this'. */
867 fputs (" { return this; }", out);
870 else if (code_len == 1 && codes[0] == OPCODE_return)
872 /* Found plain `return'. */
876 else if (code_len == 2
877 && codes[0] == OPCODE_aconst_null
878 && codes[1] == OPCODE_areturn)
880 /* Found `return null'. We don't want to depend on NULL being
882 fputs (" { return 0; }", out);
887 /* Print one piece of a signature. Returns pointer to next parseable
888 character on success, NULL on error. */
889 static const unsigned char *
890 decode_signature_piece (stream, signature, limit, need_space)
892 const unsigned char *signature, *limit;
898 switch (signature[0])
901 /* More spaghetti. */
904 for (signature++; (signature < limit
906 && *signature <= '9'); signature++)
911 ctype = "jbyteArray";
914 ctype = "jcharArray";
917 ctype = "jdoubleArray";
920 ctype = "jfloatArray";
926 ctype = "jshortArray";
929 ctype = "jlongArray";
932 ctype = "jbooleanArray";
935 /* We have a nested array. */
938 fputs ("JArray<", stream);
942 /* We have to generate a reference to JArray here, so that
943 our output matches what the compiler does. */
945 /* Space between `<' and `:' to avoid C++ digraphs. */
947 fputs ("JArray< ::", stream);
948 while (signature < limit && *signature != ';')
950 int ch = UTF8_GET (signature, limit);
954 fputs ("::", stream);
956 jcf_print_char (stream, ch);
960 fputs (" *> *", stream);
965 /* Unparseable signature. */
969 /* If the previous iterations left us with something to print,
970 print it. For JNI, we always print `jobjectArray' in the
972 if (flag_jni && ctype == NULL)
974 ctype = "jobjectArray";
977 /* The `printit' case will advance SIGNATURE for us. If we
978 don't go there, we must advance past the `;' ourselves. */
986 /* This shouldn't happen. */
989 case 'B': ctype = "jbyte"; goto printit;
990 case 'C': ctype = "jchar"; goto printit;
991 case 'D': ctype = "jdouble"; goto printit;
992 case 'F': ctype = "jfloat"; goto printit;
993 case 'I': ctype = "jint"; goto printit;
994 case 'J': ctype = "jlong"; goto printit;
995 case 'S': ctype = "jshort"; goto printit;
996 case 'Z': ctype = "jboolean"; goto printit;
997 case 'V': ctype = "void"; goto printit;
1001 /* We know about certain types and special-case their
1003 FIXME: something like java.lang.Exception should be
1004 printed as `jthrowable', because it is a subclass. This
1005 means that gcjh must read the entire hierarchy and
1007 if (! strncmp (signature, "Ljava/lang/String;",
1008 sizeof ("Ljava/lang/String;") -1))
1010 else if (! strncmp (signature, "Ljava/lang/Class;",
1011 sizeof ("Ljava/lang/Class;") - 1))
1013 else if (! strncmp (signature, "Ljava/lang/Throwable;",
1014 sizeof ("Ljava/lang/Throwable;") - 1))
1015 ctype = "jthrowable";
1016 else if (! strncmp (signature, "Ljava/lang/ref/WeakReference;",
1017 sizeof ("Ljava/lang/ref/WeakReference;") - 1))
1022 while (*signature && *signature != ';')
1027 /* Print a leading "::" so we look in the right namespace. */
1028 fputs ("::", stream);
1030 while (*signature && *signature != ';')
1032 int ch = UTF8_GET (signature, limit);
1034 fputs ("::", stream);
1036 jcf_print_char (stream, ch);
1038 fputs (" *", stream);
1039 if (*signature == ';')
1045 jni_print_char (stream, *signature++);
1050 fputs (ctype, stream);
1056 while (array_depth-- > 0)
1057 fputs ("> *", stream);
1064 DEFUN(print_c_decl, (stream, jcf, name_index, signature_index, is_init,
1065 name_override, flags),
1066 FILE* stream AND JCF* jcf
1067 AND int name_index AND int signature_index
1068 AND int is_init AND const char *name_override AND int flags)
1070 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1072 fprintf (stream, "<not a UTF8 constant>");
1077 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1078 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1079 register const unsigned char *str = str0;
1080 const unsigned char *limit = str + length;
1082 int is_method = str[0] == '(';
1083 const unsigned char *next;
1085 /* If printing a method, skip to the return signature and print
1086 that first. However, there is no return value if this is a
1088 if (is_method && ! is_init)
1098 /* If printing a field or an ordinary method, then print the
1099 "return value" now. */
1100 if (! is_method || ! is_init)
1102 next = decode_signature_piece (stream, str, limit, &need_space);
1105 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1111 /* Now print the name of the thing. */
1113 fputs (" ", stream);
1114 print_full_cxx_name (stream, jcf, name_index,
1115 signature_index, is_init, name_override,
1120 /* Print the unqualified method name followed by the signature. */
1122 DEFUN(print_full_cxx_name, (stream, jcf, name_index, signature_index,
1123 is_init, name_override, flags),
1124 FILE* stream AND JCF* jcf
1125 AND int name_index AND int signature_index AND int is_init
1126 AND const char *name_override AND int flags)
1128 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1129 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1130 register const unsigned char *str = str0;
1131 const unsigned char *limit = str + length;
1133 int is_method = str[0] == '(';
1134 const unsigned char *next;
1137 fputs (name_override, stream);
1138 else if (name_index)
1140 /* Declare constructors specially. */
1142 print_base_classname (stream, jcf, jcf->this_class);
1144 print_name (stream, jcf, name_index);
1149 unsigned char *signature = JPOOL_UTF_DATA (jcf, signature_index);
1150 int sig_len = JPOOL_UTF_LENGTH (jcf, signature_index);
1151 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf, name_index),
1152 JPOOL_UTF_LENGTH (jcf, name_index),
1153 signature, sig_len))
1155 /* If this method is overloaded by another native method,
1156 then include the argument information in the mangled
1158 unsigned char *limit = signature + sig_len;
1159 fputs ("__", stream);
1160 while (signature < limit)
1162 int ch = UTF8_GET (signature, limit);
1163 jni_print_char (stream, ch);
1175 /* Have a method or a constructor. Print signature pieces
1177 fputs (" (", stream);
1181 /* In JNI mode, add extra arguments. */
1184 /* FIXME: it would be nice to know if we are printing a decl
1185 or a definition, and only print `env' for the latter. */
1186 fputs ("JNIEnv *env", stream);
1188 fputs ((flags & ACC_STATIC) ? ", jclass" : ", jobject", stream);
1191 fputs (", ", stream);
1194 while (str < limit && *str != ')')
1196 next = decode_signature_piece (stream, str, limit, &need_space);
1199 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1204 if (next < limit && *next != ')')
1205 fputs (", ", stream);
1209 fputs (")", stream);
1213 /* This is a helper for print_stub_or_jni. */
1215 DEFUN (print_name_for_stub_or_jni, (stream, jcf, name_index, signature_index,
1216 is_init, name_override, flags),
1217 FILE *stream AND JCF *jcf
1218 AND int name_index AND int signature_index
1219 AND int is_init AND const char *name_override AND int flags)
1221 const char *const prefix = flag_jni ? "Java_" : "";
1222 print_cxx_classname (stream, prefix, jcf, jcf->this_class);
1223 fputs (flag_jni ? "_" : "::", stream);
1224 print_full_cxx_name (stream, jcf, name_index,
1225 signature_index, is_init, name_override,
1230 DEFUN(print_stub_or_jni, (stream, jcf, name_index, signature_index, is_init,
1231 name_override, flags),
1232 FILE* stream AND JCF* jcf
1233 AND int name_index AND int signature_index
1234 AND int is_init AND const char *name_override AND int flags)
1236 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1238 fprintf (stream, "<not a UTF8 constant>");
1243 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1244 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1245 register const unsigned char *str = str0;
1246 const unsigned char *limit = str + length;
1248 int is_method = str[0] == '(';
1249 const unsigned char *next;
1251 /* Don't print fields in the JNI case. */
1252 if (! is_method && flag_jni)
1255 if (flag_jni && ! stubs)
1256 fputs ("extern ", stream);
1258 /* If printing a method, skip to the return signature and print
1259 that first. However, there is no return value if this is a
1261 if (is_method && ! is_init)
1271 /* If printing a field or an ordinary method, then print the
1272 "return value" now. Note that a constructor can't be native,
1273 so we don't bother checking this in the JNI case. */
1274 if (! is_method || ! is_init)
1276 next = decode_signature_piece (stream, str, limit, &need_space);
1279 fprintf (stderr, "unparseable signature: `%s'\n", str0);
1285 /* When printing a JNI header we need to respect the space. In
1286 other cases we're just going to insert a newline anyway. */
1287 fputs (need_space && ! stubs ? " " : "\n", stream);
1289 /* Now print the name of the thing. */
1290 print_name_for_stub_or_jni (stream, jcf, name_index,
1291 signature_index, is_init, name_override,
1294 /* Print the body. */
1298 fputs ("\n{\n (*env)->FatalError (\"", stream);
1300 fputs ("\n{\n JvFail (\"", stream);
1301 print_name_for_stub_or_jni (stream, jcf, name_index,
1302 signature_index, is_init,
1305 fputs (" not implemented\");\n}\n\n", stream);
1311 DEFUN(print_mangled_classname, (stream, jcf, prefix, index),
1312 FILE *stream AND JCF *jcf AND const char *prefix AND int index)
1314 int name_index = JPOOL_USHORT1 (jcf, index);
1315 fputs (prefix, stream);
1316 jcf_print_utf8_replace (out,
1317 JPOOL_UTF_DATA (jcf, name_index),
1318 JPOOL_UTF_LENGTH (jcf, name_index),
1322 /* Print PREFIX, then a class name in C++ format. If the name refers
1323 to an array, ignore it and don't print PREFIX. Returns 1 if
1324 something was printed, 0 otherwise. */
1326 print_cxx_classname (stream, prefix, jcf, index)
1332 int name_index = JPOOL_USHORT1 (jcf, index);
1334 const unsigned char *s, *p, *limit;
1336 s = JPOOL_UTF_DATA (jcf, name_index);
1337 len = JPOOL_UTF_LENGTH (jcf, name_index);
1340 /* Explicitly omit arrays here. */
1342 c = UTF8_GET (p, limit);
1346 fputs (prefix, stream);
1348 /* Print a leading "::" so we look in the right namespace. */
1349 if (! flag_jni && ! stubs)
1350 fputs ("::", stream);
1354 c = UTF8_GET (s, limit);
1356 fputs (flag_jni ? "_" : "::", stream);
1358 jni_print_char (stream, c);
1364 int written_class_count = 0;
1366 /* Return name of superclass. If LEN is not NULL, fill it with length
1368 static const unsigned char *
1369 super_class_name (derived_jcf, len)
1373 int supername_index = JPOOL_USHORT1 (derived_jcf, derived_jcf->super_class);
1374 int supername_length = JPOOL_UTF_LENGTH (derived_jcf, supername_index);
1375 const unsigned char *supername =
1376 JPOOL_UTF_DATA (derived_jcf, supername_index);
1379 *len = supername_length;
1386 /* We keep track of all the `#include's we generate, so we can avoid
1391 struct include *next;
1394 /* List of all includes. */
1395 static struct include *all_includes = NULL;
1397 /* Generate a #include. */
1399 print_include (out, utf8, len)
1401 const unsigned char *utf8;
1404 struct include *incl;
1410 len = strlen (utf8);
1412 for (incl = all_includes; incl; incl = incl->next)
1414 /* We check the length because we might have a proper prefix. */
1415 if (len == (int) strlen (incl->name)
1416 && ! strncmp (incl->name, utf8, len))
1420 incl = (struct include *) xmalloc (sizeof (struct include));
1421 incl->name = xmalloc (len + 1);
1422 strncpy (incl->name, utf8, len);
1423 incl->name[len] = '\0';
1424 incl->next = all_includes;
1425 all_includes = incl;
1427 fputs ("#include <", out);
1428 jcf_print_utf8_replace (out, utf8, len,
1430 flag_jni ? '_' : '/');
1431 fputs (".h>\n", out);
1436 /* This is used to represent part of a package or class name. */
1439 /* The text of this part of the name. */
1441 /* True if this represents a class. */
1443 /* Linked list of all classes and packages inside this one. */
1444 struct namelet *subnamelets;
1445 /* Pointer to next sibling. */
1446 struct namelet *next;
1449 static void add_namelet PARAMS ((const unsigned char *,
1450 const unsigned char *, struct namelet *));
1451 static void print_namelet PARAMS ((FILE *, struct namelet *, int));
1453 /* The special root namelet. */
1454 static struct namelet root =
1462 /* This extracts the next name segment from the full UTF-8 encoded
1463 package or class name and links it into the tree. It does this
1466 add_namelet (name, name_limit, parent)
1467 const unsigned char *name, *name_limit;
1468 struct namelet *parent;
1470 const unsigned char *p;
1471 struct namelet *n = NULL, *np;
1473 /* We want to skip the standard namespaces that we assume the
1474 runtime already knows about. We only do this at the top level,
1475 though, hence the check for `root'. */
1476 if (parent == &root)
1478 #define JAVALANG "java/lang/"
1479 #define JAVAIO "java/io/"
1480 #define JAVAUTIL "java/util/"
1481 if ((name_limit - name >= (int) sizeof (JAVALANG) - 1
1482 && ! strncmp (name, JAVALANG, sizeof (JAVALANG) - 1))
1483 || (name_limit - name >= (int) sizeof (JAVAUTIL) - 1
1484 && ! strncmp (name, JAVAUTIL, sizeof (JAVAUTIL) - 1))
1485 || (name_limit - name >= (int) sizeof (JAVAIO) - 1
1486 && ! strncmp (name, JAVAIO, sizeof (JAVAIO) - 1)))
1490 for (p = name; p < name_limit && *p != '/'; ++p)
1493 /* Search for this name beneath the PARENT node. */
1494 for (np = parent->subnamelets; np != NULL; np = np->next)
1496 /* We check the length because we might have a proper prefix. */
1497 if ((int) strlen (np->name) == p - name &&
1498 ! strncmp (name, np->name, p - name))
1507 n = (struct namelet *) xmalloc (sizeof (struct namelet));
1508 n->name = xmalloc (p - name + 1);
1509 strncpy (n->name, name, p - name);
1510 n->name[p - name] = '\0';
1511 n->is_class = (p == name_limit);
1512 n->subnamelets = NULL;
1513 n->next = parent->subnamelets;
1514 parent->subnamelets = n;
1517 /* We recurse if there is more text, and if the trailing piece does
1518 not represent an inner class. */
1520 add_namelet (p + 1, name_limit, n);
1523 /* Print a single namelet. Destroys namelets while printing. */
1525 print_namelet (out, name, depth)
1527 struct namelet *name;
1535 for (i = 0; i < depth; ++i)
1537 fprintf (out, "%s %s", name->is_class ? "class" : "namespace",
1539 if (name->is_class && name->subnamelets == NULL)
1545 for (i = 0; i < depth; ++i)
1551 c = name->subnamelets;
1554 struct namelet *next = c->next;
1555 print_namelet (out, c, depth + 2);
1563 for (i = 0; i < depth; ++i)
1566 /* Only print a `;' when printing a class. C++ is evil. */
1576 /* This is called to add some classes to the list of classes for which
1577 we need decls. The signature argument can be a function
1580 add_class_decl (out, jcf, signature)
1585 const unsigned char *s = JPOOL_UTF_DATA (jcf, signature);
1586 int len = JPOOL_UTF_LENGTH (jcf, signature);
1589 for (i = 0; i < len; ++i)
1593 /* If we see an array, then we include the array header. */
1596 print_include (out, "gcj/array", -1);
1600 /* We're looking for `L<stuff>;' -- everything else is
1605 for (start = ++i; i < len && s[i] != ';'; ++i)
1608 add_namelet (&s[start], &s[i], &root);
1612 /* Print declarations for all classes required by this class. Any
1613 class or package in the `java' package is assumed to be handled
1614 statically in libjava; we don't generate declarations for these.
1615 This makes the generated headers a bit easier to read. */
1617 print_class_decls (out, jcf, self)
1622 /* Make sure to always add the current class to the list of things
1623 that should be declared. */
1624 int name_index = JPOOL_USHORT1 (jcf, self);
1626 const unsigned char *s;
1628 s = JPOOL_UTF_DATA (jcf, name_index);
1629 len = JPOOL_UTF_LENGTH (jcf, name_index);
1630 add_namelet (s, s + len, &root);
1632 if (root.subnamelets)
1634 fputs ("extern \"Java\"\n{\n", out);
1635 /* We use an initial offset of 0 because the root namelet
1636 doesn't cause anything to print. */
1637 print_namelet (out, &root, 0);
1638 fputs ("};\n\n", out);
1645 DEFUN(process_file, (jcf, out),
1646 JCF *jcf AND FILE *out)
1649 uint32 field_start, method_end, method_start;
1655 if (jcf_parse_preamble (jcf) != 0)
1657 fprintf (stderr, "Not a valid Java .class file.\n");
1662 /* Parse and possibly print constant pool */
1663 code = jcf_parse_constant_pool (jcf);
1666 fprintf (stderr, "error while parsing constant pool\n");
1670 code = verify_constant_pool (jcf);
1673 fprintf (stderr, "error in constant pool entry #%d\n", code);
1678 jcf_parse_class (jcf);
1680 if (written_class_count++ == 0 && out)
1682 const char *cstart, *cstart2, *mode, *cend, *what, *jflag;
1697 mode = " -*- c++ -*-";
1703 fprintf (out, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
1704 cstart, mode, cend);
1707 fprintf (out, "%s This file was created by `gcjh -stubs%s'.%s\n\
1709 %s This file is intended to give you a head start on implementing native\n\
1710 %s methods using %s.\n\
1711 %s Be aware: running `gcjh -stubs %s' once more for this class may\n\
1712 %s overwrite any edits you have made to this file.%s\n\n",
1713 cstart, jflag, mode,
1729 print_mangled_classname (out, jcf, "#ifndef __", jcf->this_class);
1730 fprintf (out, "__\n");
1732 print_mangled_classname (out, jcf, "#define __", jcf->this_class);
1733 fprintf (out, "__\n\n");
1737 fprintf (out, "#include <jni.h>\n\n");
1738 fprintf (out, "#ifdef __cplusplus\n");
1739 fprintf (out, "extern \"C\"\n");
1740 fprintf (out, "{\n");
1741 fprintf (out, "#endif\n");
1745 /* We do this to ensure that inline methods won't be
1746 `outlined' by g++. This works as long as method and
1747 fields are not added by the user. */
1748 fprintf (out, "#pragma interface\n");
1750 if (jcf->super_class)
1753 const unsigned char *supername =
1754 super_class_name (jcf, &super_length);
1757 print_include (out, supername, super_length);
1763 /* Strip off the ".class" portion of the name when printing
1764 the include file name. */
1765 int len = strlen (jcf->classname);
1766 if (len > 6 && ! strcmp (&jcf->classname[len - 6], ".class"))
1768 print_include (out, jcf->classname, len);
1769 print_include (out, "gcj/cni", -1);
1773 /* We want to parse the methods first. But we need to find where
1774 they start. So first we skip the fields, then parse the methods.
1775 Then we parse the fields and skip the methods. This is ugly, but
1776 not too bad since we need two full passes to get class decl
1777 information anyway. */
1779 field_start = JCF_TELL (jcf);
1780 jcf_parse_fields (jcf);
1782 method_start = JCF_TELL (jcf);
1784 jcf_parse_methods (jcf);
1789 if (out && ! flag_jni)
1792 print_class_decls (out, jcf, jcf->this_class);
1794 for (i = 0; i < prepend_count; ++i)
1795 fprintf (out, "%s\n", prepend_specs[i]);
1796 if (prepend_count > 0)
1801 if (! print_cxx_classname (out, "class ", jcf, jcf->this_class))
1803 fprintf (stderr, "class is of array type\n");
1807 if (jcf->super_class)
1809 if (! print_cxx_classname (out, " : public ",
1810 jcf, jcf->super_class))
1812 fprintf (stderr, "base class is of array type\n");
1818 fputs ("\n{\n", out);
1822 /* Now go back for second pass over methods and fields. */
1823 JCF_SEEK (jcf, method_start);
1825 jcf_parse_methods (jcf);
1826 method_end = JCF_TELL (jcf);
1829 JCF_SEEK (jcf, field_start);
1830 jcf_parse_fields (jcf);
1831 JCF_SEEK (jcf, method_end);
1833 jcf_parse_final_attributes (jcf);
1839 fprintf (out, "\n#ifdef __cplusplus\n");
1840 fprintf (out, "}\n");
1841 fprintf (out, "#endif\n");
1845 /* Generate friend decl if we still must. */
1846 for (i = 0; i < friend_count; ++i)
1847 fprintf (out, " friend %s\n", friend_specs[i]);
1849 /* Generate extra declarations. */
1852 for (i = 0; i < add_count; ++i)
1853 fprintf (out, " %s\n", add_specs[i]);
1855 /* Generate an entry for the class object. */
1856 generate_access (out, ACC_PUBLIC);
1857 fprintf (out, "\n static ::java::lang::Class class$;\n");
1859 fputs ("};\n", out);
1861 if (append_count > 0)
1863 for (i = 0; i < append_count; ++i)
1864 fprintf (out, "%s\n", append_specs[i]);
1867 print_mangled_classname (out, jcf,
1868 "\n#endif /* __", jcf->this_class);
1869 fprintf (out, "__ */\n");
1875 /* This is used to mark options with no short value. */
1876 #define LONG_OPT(Num) ((Num) + 128)
1878 #define OPT_classpath LONG_OPT (0)
1879 #define OPT_CLASSPATH LONG_OPT (1)
1880 #define OPT_HELP LONG_OPT (2)
1881 #define OPT_TEMP LONG_OPT (3)
1882 #define OPT_VERSION LONG_OPT (4)
1883 #define OPT_PREPEND LONG_OPT (5)
1884 #define OPT_FRIEND LONG_OPT (6)
1885 #define OPT_ADD LONG_OPT (7)
1886 #define OPT_APPEND LONG_OPT (8)
1887 #define OPT_M LONG_OPT (9)
1888 #define OPT_MM LONG_OPT (10)
1889 #define OPT_MG LONG_OPT (11)
1890 #define OPT_MD LONG_OPT (12)
1891 #define OPT_MMD LONG_OPT (13)
1893 static struct option options[] =
1895 { "classpath", required_argument, NULL, OPT_classpath },
1896 { "CLASSPATH", required_argument, NULL, OPT_CLASSPATH },
1897 { "help", no_argument, NULL, OPT_HELP },
1898 { "stubs", no_argument, &stubs, 1 },
1899 { "td", required_argument, NULL, OPT_TEMP },
1900 { "verbose", no_argument, NULL, 'v' },
1901 { "version", no_argument, NULL, OPT_VERSION },
1902 { "prepend", required_argument, NULL, OPT_PREPEND },
1903 { "friend", required_argument, NULL, OPT_FRIEND },
1904 { "add", required_argument, NULL, OPT_ADD },
1905 { "append", required_argument, NULL, OPT_APPEND },
1906 { "M", no_argument, NULL, OPT_M },
1907 { "MM", no_argument, NULL, OPT_MM },
1908 { "MG", no_argument, NULL, OPT_MG },
1909 { "MD", no_argument, NULL, OPT_MD },
1910 { "MMD", no_argument, NULL, OPT_MMD },
1911 { "jni", no_argument, &flag_jni, 1 },
1912 { NULL, no_argument, NULL, 0 }
1918 fprintf (stderr, "Try `gcjh --help' for more information.\n");
1925 printf ("Usage: gcjh [OPTION]... CLASS...\n\n");
1926 printf ("Generate C++ header files from .class files\n\n");
1927 printf (" -stubs Generate an implementation stub file\n");
1928 printf (" -jni Generate a JNI header or stub\n");
1930 printf (" -add TEXT Insert TEXT into class body\n");
1931 printf (" -append TEXT Insert TEXT after class declaration\n");
1932 printf (" -friend TEXT Insert TEXT as `friend' declaration\n");
1933 printf (" -prepend TEXT Insert TEXT before start of class\n");
1935 printf (" --classpath PATH Set path to find .class files\n");
1936 printf (" --CLASSPATH PATH Set path to find .class files\n");
1937 printf (" -IDIR Append directory to class path\n");
1938 printf (" -d DIRECTORY Set output directory name\n");
1939 printf (" -o FILE Set output file name\n");
1940 printf (" -td DIRECTORY Set temporary directory name\n");
1942 printf (" --help Print this help, then exit\n");
1943 printf (" --version Print version number, then exit\n");
1944 printf (" -v, --verbose Print extra information while running\n");
1946 printf (" -M Print all dependencies to stdout;\n");
1947 printf (" suppress ordinary output\n");
1948 printf (" -MM Print non-system dependencies to stdout;\n");
1949 printf (" suppress ordinary output\n");
1950 printf (" -MD Print all dependencies to stdout\n");
1951 printf (" -MMD Print non-system dependencies to stdout\n");
1952 /* We omit -MG until it is implemented. */
1954 printf ("For bug reporting instructions, please see:\n");
1955 printf ("%s.\n", GCCBUGURL);
1962 printf ("gcjh (%s)\n\n", version_string);
1963 printf ("Copyright (C) 1998, 1999 Free Software Foundation, Inc.\n");
1964 printf ("This is free software; see the source for copying conditions. There is NO\n");
1965 printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
1970 DEFUN(main, (argc, argv),
1971 int argc AND char** argv)
1975 char *output_file = NULL;
1976 int emit_dependencies = 0, suppress_output = 0;
1981 fprintf (stderr, "gcjh: no classes specified\n");
1987 /* We use getopt_long_only to allow single `-' long options. For
1988 some of our options this is more natural. */
1989 while ((opt = getopt_long_only (argc, argv, "I:d:o:v", options, NULL)) != -1)
1994 /* Already handled. */
1998 output_file = optarg;
2002 output_directory = optarg;
2006 jcf_path_include_arg (optarg);
2014 jcf_path_classpath_arg (optarg);
2018 jcf_path_CLASSPATH_arg (optarg);
2026 temp_directory = optarg;
2034 if (prepend_count == 0)
2035 prepend_specs = (char**) ALLOC (argc * sizeof (char*));
2036 prepend_specs[prepend_count++] = optarg;
2040 if (friend_count == 0)
2041 friend_specs = (char**) ALLOC (argc * sizeof (char*));
2042 friend_specs[friend_count++] = optarg;
2047 add_specs = (char**) ALLOC (argc * sizeof (char*));
2048 add_specs[add_count++] = optarg;
2052 if (append_count == 0)
2053 append_specs = (char**) ALLOC (argc * sizeof (char*));
2054 append_specs[append_count++] = optarg;
2058 emit_dependencies = 1;
2059 suppress_output = 1;
2060 jcf_dependency_init (1);
2064 emit_dependencies = 1;
2065 suppress_output = 1;
2066 jcf_dependency_init (0);
2070 fprintf (stderr, "gcjh: `-MG' option is unimplemented\n");
2074 emit_dependencies = 1;
2075 jcf_dependency_init (1);
2079 emit_dependencies = 1;
2080 jcf_dependency_init (0);
2091 fprintf (stderr, "gcjh: no classes specified\n");
2097 if (output_file && emit_dependencies)
2099 fprintf (stderr, "gcjh: can't specify both -o and -MD\n");
2103 for (argi = optind; argi < argc; argi++)
2105 char *classname = argv[argi];
2106 char *current_output_file;
2107 const char *classfile_name;
2110 fprintf (stderr, "Processing %s\n", classname);
2112 jcf_dependency_reset ();
2113 classfile_name = find_class (classname, strlen (classname), &jcf, 0);
2114 if (classfile_name == NULL)
2116 fprintf (stderr, "%s: no such class\n", classname);
2120 fprintf (stderr, "Found in %s\n", classfile_name);
2123 if (strcmp (output_file, "-") == 0)
2125 else if (out == NULL)
2127 out = fopen (output_file, "w");
2131 perror (output_file);
2134 current_output_file = output_file;
2138 int dir_len = strlen (output_directory);
2139 int i, classname_length = strlen (classname);
2140 current_output_file = (char*) ALLOC (dir_len + classname_length + 5);
2141 strcpy (current_output_file, output_directory);
2142 if (dir_len > 0 && output_directory[dir_len-1] != '/')
2143 current_output_file[dir_len++] = '/';
2144 for (i = 0; classname[i] != '\0'; i++)
2146 char ch = classname[i];
2149 if (flag_jni && ch == '/')
2151 current_output_file[dir_len++] = ch;
2153 if (emit_dependencies)
2155 if (suppress_output)
2157 jcf_dependency_set_dep_file ("-");
2162 /* We use `.hd' and not `.d' to avoid clashes with
2163 dependency tracking from straight compilation. */
2164 strcpy (current_output_file + dir_len, ".hd");
2165 jcf_dependency_set_dep_file (current_output_file);
2168 strcpy (current_output_file + dir_len,
2169 stubs ? (flag_jni ? ".c" : ".cc") : ".h");
2170 jcf_dependency_set_target (current_output_file);
2171 if (! suppress_output)
2173 out = fopen (current_output_file, "w");
2176 perror (current_output_file);
2181 process_file (&jcf, out);
2183 if (current_output_file != output_file)
2184 free (current_output_file);
2185 jcf_dependency_write ();
2188 if (out != NULL && out != stdout)