2006-08-07 Andrew John Hughes <gnu_andrew@member.fsf.org>
[official-gcc.git] / gcc / java / gjavah.c
blob5907e95548a255610532c6abd53a625c6354f549
1 /* Program to write C++-suitable header files from a Java(TM) .class
2 file. This is similar to SUN's javah.
4 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
5 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include <math.h>
36 #include "jcf.h"
37 #include "tree.h"
38 #include "version.h"
39 #include "javaop.h"
40 #include "java-tree.h"
41 #include "java-opcodes.h"
42 #include "ggc.h"
43 #include "hashtab.h"
44 #include "intl.h"
46 #include <getopt.h>
50 /* The output file. */
51 FILE *out = NULL;
53 /* Nonzero on failure. */
54 static int found_error = 0;
56 #ifdef JNI_DEFAULT
57 #define TOOLNAME "gjnih"
59 /* Nonzero if we're generating JNI output. */
60 int flag_jni = 1;
61 #else
62 #define TOOLNAME "gcjh"
64 int flag_jni = 0;
65 #endif
67 /* When nonzero, warn when source file is newer than matching class
68 file. */
69 int flag_newer = 1;
71 /* Directory to place resulting files in. Set by -d option. */
72 static const char *output_directory = "";
74 /* Directory to place temporary file. Set by -td option. Currently unused. */
75 static const char *temp_directory = "/tmp";
77 /* Number of friend functions we have to declare. */
78 static int friend_count;
80 /* A class can optionally have a `friend' function declared. If
81 non-NULL, this is that function. */
82 static char **friend_specs = NULL;
84 /* Number of lines we are prepending before the class. */
85 static int prepend_count;
87 /* We can prepend extra lines before the class's start. */
88 static char **prepend_specs = NULL;
90 /* Number of lines we are appending at the end of the class. */
91 static int add_count;
93 /* We can append extra lines just before the class's end. */
94 static char **add_specs = NULL;
96 /* Number of lines we are appending after the class. */
97 static int append_count;
99 /* We can append extra lines after the class's end. */
100 static char **append_specs = NULL;
102 int verbose = 0;
104 int stubs = 0;
106 struct JCF *current_jcf;
108 /* This holds access information for the last field we examined. They
109 let us generate "private:", "public:", and "protected:" properly.
110 If 0 then we haven't previously examined any field. */
111 static JCF_u2 last_access;
113 /* Pass this macro the flags for a class and for a method. It will
114 return true if the method should be considered `final'. */
115 #define METHOD_IS_FINAL(Class, Method) \
116 (((Class) & ACC_FINAL) || ((Method) & (ACC_FINAL | ACC_PRIVATE)))
118 /* Pass this macro the flags for a method. It will return true if the
119 method is native. */
120 #define METHOD_IS_NATIVE(Method) \
121 ((Method) & ACC_NATIVE)
123 #define METHOD_IS_PRIVATE(Class, Method) \
124 (((Method) & ACC_PRIVATE) != 0)
126 /* We keep a linked list of all method names we have seen. This lets
127 us determine if a method name and a field name are in conflict. */
128 struct method_name
130 unsigned char *name;
131 int length;
132 unsigned char *signature;
133 int sig_length;
134 int is_native;
135 struct method_name *next;
138 /* List of method names we've seen. */
139 static struct method_name *method_name_list;
141 static void print_field_info (FILE*, JCF*, int, int, JCF_u2);
142 static void print_mangled_classname (FILE*, JCF*, const char*, int);
143 static int print_cxx_classname (FILE*, const char*, JCF*, int, int);
144 static void print_method_info (FILE*, JCF*, int, int, JCF_u2);
145 static void print_c_decl (FILE*, JCF*, int, int, int, const char *, int);
146 static void print_stub_or_jni (FILE*, JCF*, int, int, int, const char *, int);
147 static void print_full_cxx_name (FILE*, JCF*, int, int, int, const char *, int);
148 static void decompile_method (FILE*, JCF*, int) ATTRIBUTE_UNUSED;
149 static void add_class_decl (FILE*, JCF*, JCF_u2);
151 static void print_name (FILE *, JCF *, int);
152 static void print_base_classname (FILE *, JCF *, int);
153 static int utf8_cmp (const unsigned char *, int, const char *);
154 static char *cxx_keyword_subst (const unsigned char *, int);
155 static void generate_access (FILE *, JCF_u2);
156 static int name_is_method_p (const unsigned char *, int);
157 static char *get_field_name (JCF *, int, JCF_u2);
158 static void print_field_name (FILE *, JCF *, int, JCF_u2);
159 static const unsigned char *super_class_name (JCF *, int *);
160 static void print_include (FILE *, const unsigned char *, int);
161 static int gcjh_streq (const void *p1, const void *p2);
162 static int throwable_p (const unsigned char *signature);
163 static const unsigned char *
164 decode_signature_piece (FILE *, const unsigned char *,
165 const unsigned char *, int *);
166 static void print_class_decls (FILE *, JCF *, int);
167 static void error (const char *gmsgid, ...) ATTRIBUTE_PRINTF_1;
168 static void usage (void) ATTRIBUTE_NORETURN;
169 static void help (void) ATTRIBUTE_NORETURN;
170 static void version (void) ATTRIBUTE_NORETURN;
171 static int overloaded_jni_method_exists_p (const unsigned char *, int,
172 const char *, int);
173 static void jni_print_char (FILE *, int);
174 static void jni_print_float (FILE *, jfloat);
175 static void jni_print_double (FILE *, jdouble);
176 static void decompile_return_statement (FILE *, JCF *, int, int, int);
178 static void handle_inner_classes (int);
180 JCF_u2 current_field_name;
181 JCF_u2 current_field_value;
182 JCF_u2 current_field_signature;
183 JCF_u2 current_field_flags;
185 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
186 ( current_field_name = (NAME), current_field_signature = (SIGNATURE), \
187 current_field_flags = (ACCESS_FLAGS), current_field_value = 0)
189 /* We pass over fields twice. The first time we just note the types
190 of the fields and then the start of the methods. Then we go back
191 and parse the fields for real. This is ugly. */
192 static int field_pass;
193 /* Likewise we pass over methods twice. The first time we generate
194 class decl information; the second time we generate actual method
195 decls. */
196 static int method_pass;
198 #define HANDLE_END_FIELD() \
199 if (field_pass) \
201 if (out && ! stubs) \
202 print_field_info (out, jcf, current_field_name, \
203 current_field_signature, \
204 current_field_flags); \
206 else if (! stubs && ! flag_jni) \
207 add_class_decl (out, jcf, current_field_signature);
209 #define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)
211 static int method_declared = 0;
212 static int method_access = 0;
213 static int method_printed = 0;
214 static int method_synthetic = 0;
215 static int method_signature = 0;
217 /* Set to 1 while the very first data member of a class is being handled. */
218 static int is_first_data_member = 0;
220 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
222 method_synthetic = 0; \
223 method_printed = 0; \
224 decompiled = 0; \
225 method_signature = SIGNATURE; \
226 if (ATTRIBUTE_COUNT) \
227 method_synthetic = peek_attribute (jcf, ATTRIBUTE_COUNT, \
228 (const char *)"Synthetic", 9); \
229 /* If a synthetic methods have been declared, its attribute aren't \
230 worth reading (and triggering side-effects). We skip them an \
231 set ATTRIBUTE_COUNT to zero so that they'll be skipped in \
232 jcf_parse_one_method. */ \
233 if (method_synthetic) \
235 skip_attribute (jcf, ATTRIBUTE_COUNT); \
236 ATTRIBUTE_COUNT = 0; \
238 if (method_pass && !method_synthetic) \
240 if (out) \
241 print_method_info (out, jcf, NAME, SIGNATURE, \
242 ACCESS_FLAGS); \
244 else if (!method_synthetic) \
246 print_method_info (NULL, jcf, NAME, SIGNATURE, \
247 ACCESS_FLAGS); \
248 if (! stubs && ! flag_jni) \
249 add_class_decl (out, jcf, SIGNATURE); \
253 /* Only include byte-code decompilation optimizations for ELF targets
254 since the generated headers are only known to work with ELF weak
255 symbol semantics. Specifically, these optimizations are known to
256 not work on PE-COFF and possibly others. */
257 #ifdef OBJECT_FORMAT_ELF
258 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
259 if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
260 #endif
262 static int decompiled = 0;
263 #define HANDLE_END_METHOD() \
264 if (out && method_printed && !method_synthetic) \
265 fputs (decompiled || stubs ? "\n" : ";\n", out);
267 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) handle_inner_classes (COUNT)
269 /* We're going to need {peek,skip}_attribute, enable their definition. */
270 #define NEED_PEEK_ATTRIBUTE
271 #define NEED_SKIP_ATTRIBUTE
273 #include "jcf-reader.c"
275 /* Print an error message and set found_error.
276 Not really gcc-internal-format message, but as error elsewhere
277 uses it, assume all users will use intersection between
278 c-format and gcc-internal-format. */
279 static void
280 error (const char *gmsgid, ...)
282 va_list ap;
284 va_start (ap, gmsgid);
286 fprintf (stderr, TOOLNAME ": ");
287 vfprintf (stderr, _(gmsgid), ap);
288 va_end (ap);
289 fprintf (stderr, "\n");
290 found_error = 1;
293 /* Print a single-precision float, suitable for parsing by g++. */
294 static void
295 jni_print_float (FILE *stream, jfloat f)
297 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
298 work in data initializers. FIXME. */
299 if (JFLOAT_FINITE (f))
301 if (flag_jni)
303 fputs (" ", out);
304 if (f.negative)
305 putc ('-', stream);
306 if (f.exponent)
307 fprintf (stream, "0x1.%.6xp%+df",
308 ((unsigned int)f.mantissa) << 1,
309 f.exponent - JFLOAT_EXP_BIAS);
310 else
311 /* Exponent of 0x01 is -125; exponent of 0x00 is *also* -125,
312 because the implicit leading 1 bit is no longer present. */
313 fprintf (stream, "0x0.%.6xp%+df",
314 ((unsigned int)f.mantissa) << 1,
315 f.exponent + 1 - JFLOAT_EXP_BIAS);
318 if (! flag_jni)
319 fputs (";\n", stream);
322 /* Print a double-precision float, suitable for parsing by g++. */
323 static void
324 jni_print_double (FILE *stream, jdouble f)
326 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
327 work in data initializers. FIXME. */
328 if (JDOUBLE_FINITE (f))
330 if (flag_jni)
332 fputs (" ", out);
333 if (f.negative)
334 putc ('-', stream);
335 if (f.exponent)
336 fprintf (stream, "0x1.%.5x%.8xp%+d",
337 f.mantissa0, f.mantissa1,
338 f.exponent - JDOUBLE_EXP_BIAS);
339 else
340 /* Exponent of 0x001 is -1022; exponent of 0x000 is *also* -1022,
341 because the implicit leading 1 bit is no longer present. */
342 fprintf (stream, "0x0.%.5x%.8xp%+d",
343 f.mantissa0, f.mantissa1,
344 f.exponent + 1 - JDOUBLE_EXP_BIAS);
347 fputs (flag_jni ? "\n" : ";\n", stream);
350 /* Print a character, appropriately mangled for JNI. */
352 static void
353 jni_print_char (FILE *stream, int ch)
355 if (! flag_jni)
356 jcf_print_char (stream, ch);
357 else if (ch == '(' || ch == ')')
359 /* Ignore. */
361 else if (ch == '_')
362 fputs ("_1", stream);
363 else if (ch == ';')
364 fputs ("_2", stream);
365 else if (ch == '[')
366 fputs ("_3", stream);
367 else if (ch == '/')
368 fputs ("_", stream);
369 else if (ISALNUM (ch))
370 fputc (ch, stream);
371 else
373 /* "Unicode" character. */
374 fprintf (stream, "_0%04x", ch);
378 /* Print a name from the class data. If the index does not point to a
379 string, an error results. */
381 static void
382 print_name (FILE* stream, JCF* jcf, int name_index)
384 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
386 fprintf (stream, "<not a UTF8 constant>");
387 found_error = 1;
389 else if (! flag_jni)
390 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
391 JPOOL_UTF_LENGTH (jcf, name_index));
392 else
394 /* For JNI we must correctly quote each character. */
395 const unsigned char *str = JPOOL_UTF_DATA (jcf, name_index);
396 int length = JPOOL_UTF_LENGTH (jcf, name_index);
397 const unsigned char *limit = str + length;
398 while (str < limit)
400 int ch = UTF8_GET (str, limit);
401 if (ch < 0)
403 fprintf (stream, "\\<invalid>");
404 return;
406 jni_print_char (stream, ch);
411 /* Print base name of class. The base name is everything after the
412 final separator. */
414 static void
415 print_base_classname (FILE *stream, JCF *jcf, int index)
417 int name_index = JPOOL_USHORT1 (jcf, index);
418 int len;
419 const unsigned char *s, *p, *limit;
421 s = JPOOL_UTF_DATA (jcf, name_index);
422 len = JPOOL_UTF_LENGTH (jcf, name_index);
423 limit = s + len;
424 p = s;
425 while (s < limit)
427 int c = UTF8_GET (s, limit);
428 if (c == '/')
429 p = s;
432 while (p < limit)
434 int ch = UTF8_GET (p, limit);
435 if (ch == '/')
436 fputs ("::", stream);
437 else
438 jcf_print_char (stream, ch);
442 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
443 and 1 if STR is "greater" than NAME. */
445 static int
446 utf8_cmp (const unsigned char *str, int length, const char *name)
448 const unsigned char *limit = str + length;
449 int i;
451 for (i = 0; name[i]; ++i)
453 int ch = UTF8_GET (str, limit);
454 if (ch != name[i])
455 return ch - name[i];
458 return str == limit ? 0 : 1;
461 /* This is a sorted list of all C++ keywords. */
463 static const char *const cxx_keywords[] =
465 "_Complex",
466 "__alignof",
467 "__alignof__",
468 "__asm",
469 "__asm__",
470 "__attribute",
471 "__attribute__",
472 "__builtin_va_arg",
473 "__complex",
474 "__complex__",
475 "__const",
476 "__const__",
477 "__extension__",
478 "__imag",
479 "__imag__",
480 "__inline",
481 "__inline__",
482 "__label__",
483 "__null",
484 "__real",
485 "__real__",
486 "__restrict",
487 "__restrict__",
488 "__signed",
489 "__signed__",
490 "__typeof",
491 "__typeof__",
492 "__volatile",
493 "__volatile__",
494 "and",
495 "and_eq",
496 "asm",
497 "auto",
498 "bitand",
499 "bitor",
500 "bool",
501 "break",
502 "case",
503 "catch",
504 "char",
505 "class",
506 "compl",
507 "const",
508 "const_cast",
509 "continue",
510 "default",
511 "delete",
512 "do",
513 "double",
514 "dynamic_cast",
515 "else",
516 "enum",
517 "explicit",
518 "export",
519 "extern",
520 "false",
521 "float",
522 "for",
523 "friend",
524 "goto",
525 "if",
526 "inline",
527 "int",
528 "long",
529 "mutable",
530 "namespace",
531 "new",
532 "not",
533 "not_eq",
534 "operator",
535 "or",
536 "or_eq",
537 "private",
538 "protected",
539 "public",
540 "register",
541 "reinterpret_cast",
542 "return",
543 "short",
544 "signed",
545 "sizeof",
546 "static",
547 "static_cast",
548 "struct",
549 "switch",
550 "template",
551 "this",
552 "throw",
553 "true",
554 "try",
555 "typedef",
556 "typeid",
557 "typename",
558 "typeof",
559 "union",
560 "unsigned",
561 "using",
562 "virtual",
563 "void",
564 "volatile",
565 "wchar_t",
566 "while",
567 "xor",
568 "xor_eq"
572 /* If NAME is the name of a C++ keyword, then return an override name.
573 This is a name that can be used in place of the keyword.
574 Otherwise, return NULL. The return value is malloc()d. */
576 static char *
577 cxx_keyword_subst (const unsigned char *str, int length)
579 int last = ARRAY_SIZE (cxx_keywords);
580 int first = 0;
581 int mid = (last + first) / 2;
582 int old = -1;
584 for (mid = (last + first) / 2;
585 mid != old;
586 old = mid, mid = (last + first) / 2)
588 int kwl = strlen (cxx_keywords[mid]);
589 int min_length = kwl > length ? length : kwl;
590 int r = utf8_cmp (str, min_length, cxx_keywords[mid]);
592 if (r == 0)
594 int i;
596 /* Skip all trailing `$'. */
597 for (i = min_length; i < length && str[i] == '$'; ++i)
599 /* We've only found a match if all the remaining characters
600 are `$'. */
601 if (i == length)
603 char *dup = XNEWVEC (char, 2 + length - min_length + kwl);
604 strcpy (dup, cxx_keywords[mid]);
605 for (i = kwl; i < length + 1; ++i)
606 dup[i] = '$';
607 dup[i] = '\0';
608 return dup;
610 r = 1;
613 if (r < 0)
614 last = mid;
615 else
616 first = mid;
618 return NULL;
621 /* Generate an access control keyword based on FLAGS. */
623 static void
624 generate_access (FILE *stream, JCF_u2 flags)
626 if ((flags & ACC_VISIBILITY) == last_access)
627 return;
628 last_access = (flags & ACC_VISIBILITY);
630 switch (last_access)
632 case 0:
633 fputs ("public: // actually package-private\n", stream);
634 break;
635 case ACC_PUBLIC:
636 fputs ("public:\n", stream);
637 break;
638 case ACC_PRIVATE:
639 fputs ("private:\n", stream);
640 break;
641 case ACC_PROTECTED:
642 fputs ("public: // actually protected\n", stream);
643 break;
644 default:
645 found_error = 1;
646 fprintf (stream, "#error unrecognized visibility %d\n",
647 (flags & ACC_VISIBILITY));
648 break;
652 /* See if NAME is already the name of a method. */
653 static int
654 name_is_method_p (const unsigned char *name, int length)
656 struct method_name *p;
658 for (p = method_name_list; p != NULL; p = p->next)
660 if (p->length == length && ! memcmp (p->name, name, length))
661 return 1;
663 return 0;
666 /* Free the method name list. */
667 static void
668 free_method_name_list (void)
670 struct method_name *p = method_name_list;
671 while (p != NULL)
673 struct method_name *next = p->next;
674 free (p->name);
675 free (p->signature);
676 free (p);
677 p = next;
679 method_name_list = NULL;
682 /* If there is already a native method named NAME, whose signature is not
683 SIGNATURE, then return true. Otherwise return false. */
684 static int
685 overloaded_jni_method_exists_p (const unsigned char *name, int length,
686 const char *signature, int sig_length)
688 struct method_name *p;
690 for (p = method_name_list; p != NULL; p = p->next)
692 if (p->is_native
693 && p->length == length
694 && ! memcmp (p->name, name, length)
695 && (p->sig_length != sig_length
696 || memcmp (p->signature, signature, sig_length)))
697 return 1;
699 return 0;
702 /* Get name of a field. This handles renamings due to C++ clash. */
703 static char *
704 get_field_name (JCF *jcf, int name_index, JCF_u2 flags)
706 unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
707 int length = JPOOL_UTF_LENGTH (jcf, name_index);
708 char *override;
710 if (name_is_method_p (name, length))
712 /* This field name matches a method. So override the name with
713 a dummy name. This is yucky, but it isn't clear what else to
714 do. FIXME: if the field is static, then we'll be in real
715 trouble. */
716 if ((flags & ACC_STATIC))
718 error ("static field has same name as method");
719 return NULL;
722 override = XNEWVEC (char, length + 3);
723 memcpy (override, name, length);
724 strcpy (override + length, "__");
726 else if (flag_jni)
727 override = NULL;
728 else
729 override = cxx_keyword_subst (name, length);
731 return override;
734 /* Print a field name. Convenience function for use with
735 get_field_name. */
736 static void
737 print_field_name (FILE *stream, JCF *jcf, int name_index, JCF_u2 flags)
739 char *override = get_field_name (jcf, name_index, flags);
741 if (override)
743 fputs (override, stream);
744 free (override);
746 else
747 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
748 JPOOL_UTF_LENGTH (jcf, name_index));
751 static void
752 print_field_info (FILE *stream, JCF* jcf, int name_index, int sig_index,
753 JCF_u2 flags)
755 char *override = NULL;
757 if (! flag_jni)
758 generate_access (stream, flags);
759 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
761 fprintf (stream, "<not a UTF8 constant>");
762 found_error = 1;
763 return;
766 if (flag_jni)
768 /* For JNI we only want to print real constants. */
769 int val;
770 if (! (flags & ACC_STATIC)
771 || ! (flags & ACC_FINAL)
772 || current_field_value <= 0)
773 return;
774 val = JPOOL_TAG (jcf, current_field_value);
775 if (val != CONSTANT_Integer && val != CONSTANT_Long
776 && val != CONSTANT_Float && val != CONSTANT_Double)
777 return;
779 else
781 /* Initial indentation. */
782 fputs (" ", stream);
785 if ((flags & ACC_STATIC))
787 if (flag_jni)
789 print_cxx_classname (stream, "#undef ", jcf, jcf->this_class, 1);
790 fputs ("_", stream);
791 print_field_name (stream, jcf, name_index, 0);
792 fputs ("\n", stream);
793 print_cxx_classname (stream, "#define ", jcf, jcf->this_class, 1);
794 fputs ("_", stream);
796 else
797 fputs ("static ", stream);
799 if ((flags & ACC_FINAL) && current_field_value > 0)
801 char buffer[25];
802 int done = 1;
804 switch (JPOOL_TAG (jcf, current_field_value))
806 case CONSTANT_Integer:
808 jint num;
809 int most_negative = 0;
810 if (! flag_jni)
811 fputs ("const jint ", stream);
812 print_field_name (stream, jcf, name_index, 0);
813 fputs (flag_jni ? " " : " = ", stream);
814 num = JPOOL_INT (jcf, current_field_value);
815 /* We single out the most negative number to print
816 specially. This avoids later warnings from g++. */
817 if (num == (jint) 0x80000000)
819 most_negative = 1;
820 ++num;
822 format_int (buffer, (jlong) num, 10);
823 fprintf (stream, "%sL%s%s\n", buffer,
824 most_negative ? " - 1" : "",
825 flag_jni ? "" : ";");
827 break;
828 case CONSTANT_Long:
830 jlong num;
831 int most_negative = 0;
832 if (! flag_jni)
833 fputs ("const jlong ", stream);
834 print_field_name (stream, jcf, name_index, 0);
835 fputs (flag_jni ? " " : " = ", stream);
836 num = JPOOL_LONG (jcf, current_field_value);
837 /* We single out the most negative number to print
838 specially.. This avoids later warnings from g++. */
839 if (num == (jlong) 0x8000000000000000LL)
841 most_negative = 1;
842 ++num;
844 format_int (buffer, num, 10);
845 fprintf (stream, "%sLL%s%s\n", buffer,
846 most_negative ? " - 1" :"",
847 flag_jni ? "" : ";");
849 break;
850 case CONSTANT_Float:
852 jfloat fnum = JPOOL_FLOAT (jcf, current_field_value);
853 if (! flag_jni)
854 fputs ("const jfloat ", stream);
855 print_field_name (stream, jcf, name_index, 0);
856 jni_print_float (stream, fnum);
858 break;
859 case CONSTANT_Double:
861 jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value);
862 if (! flag_jni)
863 fputs ("const jdouble ", stream);
864 print_field_name (stream, jcf, name_index, 0);
865 jni_print_double (stream, dnum);
867 break;
868 default:
869 /* We can't print this as a constant, but we can still
870 print something sensible. */
871 done = 0;
872 break;
875 if (done)
876 return;
880 /* assert (! flag_jni); */
881 override = get_field_name (jcf, name_index, flags);
882 print_c_decl (stream, jcf, name_index, sig_index, 0, override, flags);
883 fputs (";\n", stream);
885 if (override)
886 free (override);
890 static void
891 print_method_info (FILE *stream, JCF* jcf, int name_index, int sig_index,
892 JCF_u2 flags)
894 const unsigned char *str;
895 int length, is_init = 0;
896 char *override = NULL;
898 method_declared = 0;
899 method_access = flags;
900 if (stream && JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
901 fprintf (stream, "<not a UTF8 constant>");
902 str = JPOOL_UTF_DATA (jcf, name_index);
903 length = JPOOL_UTF_LENGTH (jcf, name_index);
905 if (str[0] == '<')
907 /* Ignore the internally generated method <clinit>. However,
908 treat <init> as a constructor. */
909 if (! utf8_cmp (str, length, "<init>"))
910 is_init = 1;
911 else if (! METHOD_IS_FINAL (jcf->access_flags, flags)
912 && ! (flags & ACC_STATIC))
914 /* FIXME: i18n bug here. Order of prints should not be
915 fixed. */
916 fprintf (stderr, _("ignored method '"));
917 jcf_print_utf8 (stderr, str, length);
918 fprintf (stderr, _("' marked virtual\n"));
919 found_error = 1;
920 return;
922 else
923 return;
926 /* During the first method pass, build a list of method names. This will
927 be used to determine if field names conflict with method names. */
928 if (! stream)
930 struct method_name *nn;
932 nn = XNEW (struct method_name);
933 nn->name = XNEWVEC (unsigned char, length);
934 memcpy (nn->name, str, length);
935 nn->length = length;
936 nn->next = method_name_list;
937 nn->sig_length = JPOOL_UTF_LENGTH (jcf, sig_index);
938 nn->signature = XNEWVEC (unsigned char, nn->sig_length);
939 nn->is_native = METHOD_IS_NATIVE (flags);
940 memcpy (nn->signature, JPOOL_UTF_DATA (jcf, sig_index),
941 nn->sig_length);
942 method_name_list = nn;
944 /* The rest of this function doesn't matter. */
945 return;
948 /* We don't worry about overrides in JNI mode. */
949 if (! flag_jni)
951 /* We can't generate a method whose name is a C++ reserved word.
952 We can't just ignore the function, because that will cause
953 incorrect code to be generated if the function is virtual
954 (not only for calls to this function for for other functions
955 after it in the vtbl). So we give it a dummy name instead. */
956 override = cxx_keyword_subst (str, length);
959 if (! stubs && ! flag_jni)
961 method_printed = 1;
963 generate_access (stream, flags);
965 fputs (" ", out);
966 if ((flags & ACC_STATIC))
967 fputs ("static ", out);
968 else if (! METHOD_IS_PRIVATE (jcf->access_flags, flags))
970 /* Don't print `virtual' if we have a constructor. */
971 if (! is_init)
972 fputs ("virtual ", out);
974 print_c_decl (out, jcf, name_index, sig_index, is_init, override, flags);
976 if ((flags & ACC_ABSTRACT))
977 fputs (" = 0", out);
978 else
979 method_declared = 1;
981 else
983 if (METHOD_IS_NATIVE (flags))
985 method_printed = 1;
986 print_stub_or_jni (out, jcf, name_index, sig_index,
987 is_init, override, flags);
991 if (override)
992 free (override);
995 /* A helper for the decompiler which prints a `return' statement where
996 the type is a reference type. If METHODTYPE and OBJECTTYPE are not
997 identical, we emit a cast. We do this because the C++ compiler
998 doesn't know that a reference can be cast to the type of an
999 interface it implements. METHODTYPE is the index of the method's
1000 signature. NAMEINDEX is the index of the field name; -1 for
1001 `this'. OBJECTTYPE is the index of the object's type. */
1002 static void
1003 decompile_return_statement (FILE *out, JCF *jcf, int methodtype,
1004 int nameindex, int objecttype)
1006 int cast = 0;
1007 int obj_name_len, method_name_len;
1008 const unsigned char *obj_data, *method_data;
1010 obj_name_len = JPOOL_UTF_LENGTH (jcf, objecttype);
1011 obj_data = JPOOL_UTF_DATA (jcf, objecttype);
1013 method_name_len = JPOOL_UTF_LENGTH (jcf, methodtype);
1014 method_data = JPOOL_UTF_DATA (jcf, methodtype);
1016 /* Skip forward to return type part of method. */
1017 while (*method_data != ')')
1019 ++method_data;
1020 --method_name_len;
1022 /* Skip past `)'. */
1023 ++method_data;
1024 --method_name_len;
1026 /* If we see an `L', skip it and the trailing `;'. */
1027 if (method_data[0] == 'L' && method_data[method_name_len - 1] == ';')
1029 ++method_data;
1030 method_name_len -= 2;
1032 if (obj_data[0] == 'L' && obj_data[obj_name_len - 1] == ';')
1034 ++obj_data;
1035 obj_name_len -= 2;
1038 /* FIXME: if METHODTYPE is a superclass of OBJECTTYPE then we don't
1039 need a cast. Right now there is no way to determine if this is
1040 the case. */
1041 if (method_name_len != obj_name_len)
1042 cast = 1;
1043 else
1045 int i;
1046 for (i = 0; i < method_name_len; ++i)
1048 if (method_data[i] != obj_data[i])
1050 cast = 1;
1051 break;
1056 fputs (" { return ", out);
1058 if (cast)
1060 int array_depth = 0;
1061 const unsigned char *limit;
1063 fputs ("reinterpret_cast<", out);
1065 while (*method_data == '[')
1067 ++method_data;
1068 ++array_depth;
1069 --method_name_len;
1070 fputs ("JArray<", out);
1073 /* Leading space to avoid C++ digraphs. */
1074 fputs (" ::", out);
1076 /* If we see an `L', skip it and the trailing `;'. Only do this
1077 if we've seen an array specification. If we don't have an
1078 array then the `L' was stripped earlier. */
1079 if (array_depth && method_data[0] == 'L'
1080 && method_data[method_name_len - 1] == ';')
1082 ++method_data;
1083 method_name_len -= 2;
1086 limit = method_data + method_name_len;
1087 while (method_data < limit)
1089 int ch = UTF8_GET (method_data, limit);
1090 if (ch == '/')
1091 fputs ("::", out);
1092 else
1093 jcf_print_char (out, ch);
1095 fputs (" *", out);
1097 /* Close each array. */
1098 while (array_depth > 0)
1100 fputs ("> *", out);
1101 --array_depth;
1104 /* Close the cast. */
1105 fputs ("> (", out);
1108 if (nameindex == -1)
1109 fputs ("this", out);
1110 else
1111 print_field_name (out, jcf, nameindex, 0);
1113 if (cast)
1114 fputs (")", out);
1116 fputs ("; }", out);
1120 /* Try to decompile a method body. Right now we just try to handle a
1121 simple case that we can do. Expand as desired. */
1122 static void
1123 decompile_method (FILE *out, JCF *jcf, int code_len)
1125 const unsigned char *codes = jcf->read_ptr;
1126 int index;
1127 uint16 name_and_type, name;
1129 /* If the method is synchronized, don't touch it. */
1130 if ((method_access & ACC_SYNCHRONIZED))
1131 return;
1133 /* The first case is 'return field'; but we only want to do this for
1134 non-static methods. */
1135 if (! (method_access & ACC_STATIC)
1136 && code_len == 5
1137 && codes[0] == OPCODE_aload_0
1138 && codes[1] == OPCODE_getfield
1139 && (codes[4] == OPCODE_areturn
1140 || codes[4] == OPCODE_dreturn
1141 || codes[4] == OPCODE_freturn
1142 || codes[4] == OPCODE_ireturn
1143 || codes[4] == OPCODE_lreturn))
1145 /* Found code like `return FIELD'. */
1146 index = (codes[2] << 8) | codes[3];
1147 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
1148 name_and_type = JPOOL_USHORT2 (jcf, index);
1149 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
1150 name = JPOOL_USHORT1 (jcf, name_and_type);
1151 if (codes[4] == OPCODE_areturn)
1152 decompile_return_statement (out, jcf, method_signature,
1153 name, JPOOL_USHORT2 (jcf, name_and_type));
1154 else
1156 fputs (" { return ", out);
1157 /* FIXME: flags. */
1158 print_field_name (out, jcf, name, 0);
1159 fputs ("; }", out);
1161 decompiled = 1;
1163 else if (code_len == 2
1164 && codes[0] == OPCODE_aload_0
1165 && codes[1] == OPCODE_areturn
1166 /* We're going to generate `return this'. This only makes
1167 sense for non-static methods. */
1168 && ! (method_access & ACC_STATIC))
1170 decompile_return_statement (out, jcf, method_signature, -1,
1171 JPOOL_USHORT1 (jcf, jcf->this_class));
1172 decompiled = 1;
1174 else if (code_len == 1 && codes[0] == OPCODE_return)
1176 /* Found plain `return'. */
1177 fputs (" { }", out);
1178 decompiled = 1;
1180 else if (code_len == 2
1181 && codes[0] == OPCODE_aconst_null
1182 && codes[1] == OPCODE_areturn)
1184 /* Found `return null'. We don't want to depend on NULL being
1185 defined. */
1186 fputs (" { return 0; }", out);
1187 decompiled = 1;
1191 /* Like strcmp, but invert the return result for the hash table. This
1192 should probably be in hashtab.c to complement the existing string
1193 hash function. */
1194 static int
1195 gcjh_streq (const void *p1, const void *p2)
1197 return ! strcmp ((char *) p1, (char *) p2);
1200 /* Return 1 if the initial part of CLNAME names a subclass of throwable,
1201 or 0 if not. CLNAME may be extracted from a signature, and can be
1202 terminated with either `;' or NULL. */
1203 static int
1204 throwable_p (const unsigned char *clname)
1206 int length;
1207 unsigned char *current;
1208 int i;
1209 int result = 0;
1211 /* We keep two hash tables of class names. In one we list all the
1212 classes which are subclasses of Throwable. In the other we will
1213 all other classes. We keep two tables to make the code a bit
1214 simpler; we don't have to have a structure mapping class name to
1215 a `throwable?' bit. */
1216 static htab_t throw_hash;
1217 static htab_t non_throw_hash;
1218 static int init_done = 0;
1220 if (! init_done)
1222 void **slot;
1223 unsigned char *str;
1225 /* Self-initializing. The cost of this really doesn't matter.
1226 We also don't care about freeing these, either. */
1227 throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
1228 (htab_del) free);
1229 non_throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
1230 (htab_del) free);
1232 /* Make sure the root classes show up in the tables. */
1233 str = (unsigned char *) xstrdup ("java.lang.Throwable");
1234 slot = htab_find_slot (throw_hash, str, INSERT);
1235 *slot = str;
1237 str = (unsigned char *) xstrdup ("java.lang.Object");
1238 slot = htab_find_slot (non_throw_hash, str, INSERT);
1239 *slot = str;
1241 init_done = 1;
1244 for (length = 0; clname[length] != ';' && clname[length] != '\0'; ++length)
1246 current = XNEWVEC (unsigned char, length + 1);
1247 for (i = 0; i < length; ++i)
1248 current[i] = clname[i] == '/' ? '.' : clname[i];
1249 current[length] = '\0';
1251 /* We don't compute the hash slot here because the table might be
1252 modified by the recursion. In that case the slot could be
1253 invalidated. */
1254 if (htab_find (throw_hash, current))
1255 result = 1;
1256 else if (htab_find (non_throw_hash, current))
1257 result = 0;
1258 else
1260 JCF jcf;
1261 void **slot;
1262 unsigned char *super, *tmp;
1263 int super_length = -1;
1264 const char *classfile_name = find_class ((char *) current, strlen ((const char *) current),
1265 &jcf, 0);
1267 if (! classfile_name)
1269 error ("couldn't find class %s", current);
1270 return 0;
1272 if (jcf_parse_preamble (&jcf) != 0
1273 || jcf_parse_constant_pool (&jcf) != 0
1274 || verify_constant_pool (&jcf) > 0)
1276 error ("parse error while reading %s", classfile_name);
1277 return 0;
1279 jcf_parse_class (&jcf);
1281 tmp = (unsigned char *) super_class_name (&jcf, &super_length);
1282 super = XNEWVEC (unsigned char, super_length + 1);
1283 memcpy (super, tmp, super_length);
1284 super[super_length] = '\0';
1286 result = throwable_p (super);
1287 slot = htab_find_slot (result ? throw_hash : non_throw_hash,
1288 current, INSERT);
1289 *slot = current;
1290 current = NULL;
1292 JCF_FINISH (&jcf);
1295 return result;
1298 /* Print one piece of a signature. Returns pointer to next parseable
1299 character on success, NULL on error. */
1300 static const unsigned char *
1301 decode_signature_piece (FILE *stream, const unsigned char *signature,
1302 const unsigned char *limit, int *need_space)
1304 const char *ctype;
1305 int array_depth = 0;
1307 switch (signature[0])
1309 case '[':
1310 /* More spaghetti. */
1312 array_loop:
1313 for (signature++; (signature < limit
1314 && ISDIGIT (*signature)); signature++)
1316 switch (*signature)
1318 case 'B':
1319 ctype = "jbyteArray";
1320 break;
1321 case 'C':
1322 ctype = "jcharArray";
1323 break;
1324 case 'D':
1325 ctype = "jdoubleArray";
1326 break;
1327 case 'F':
1328 ctype = "jfloatArray";
1329 break;
1330 case 'I':
1331 ctype = "jintArray";
1332 break;
1333 case 'S':
1334 ctype = "jshortArray";
1335 break;
1336 case 'J':
1337 ctype = "jlongArray";
1338 break;
1339 case 'Z':
1340 ctype = "jbooleanArray";
1341 break;
1342 case '[':
1343 /* We have a nested array. */
1344 ++array_depth;
1345 if (! flag_jni)
1346 fputs ("JArray<", stream);
1347 goto array_loop;
1349 case 'L':
1350 /* We have to generate a reference to JArray here, so that
1351 our output matches what the compiler does. */
1352 ++signature;
1353 /* Space between `<' and `:' to avoid C++ digraphs. */
1354 if (! flag_jni)
1355 fputs ("JArray< ::", stream);
1356 while (signature < limit && *signature != ';')
1358 int ch = UTF8_GET (signature, limit);
1359 if (! flag_jni)
1361 if (ch == '/')
1362 fputs ("::", stream);
1363 else
1364 jcf_print_char (stream, ch);
1367 if (! flag_jni)
1368 fputs (" *> *", stream);
1369 *need_space = 0;
1370 ctype = NULL;
1371 break;
1372 default:
1373 /* Unparseable signature. */
1374 return NULL;
1377 /* If the previous iterations left us with something to print,
1378 print it. For JNI, we always print `jobjectArray' in the
1379 nested cases. */
1380 if (flag_jni && (ctype == NULL || array_depth > 0))
1382 ctype = "jobjectArray";
1383 *need_space = 1;
1385 /* The `printit' case will advance SIGNATURE for us. If we
1386 don't go there, we must advance past the `;' ourselves. */
1387 if (ctype != NULL)
1388 goto printit;
1389 ++signature;
1390 break;
1392 case '(':
1393 case ')':
1394 /* This shouldn't happen. */
1395 return NULL;
1397 case 'B': ctype = "jbyte"; goto printit;
1398 case 'C': ctype = "jchar"; goto printit;
1399 case 'D': ctype = "jdouble"; goto printit;
1400 case 'F': ctype = "jfloat"; goto printit;
1401 case 'I': ctype = "jint"; goto printit;
1402 case 'J': ctype = "jlong"; goto printit;
1403 case 'S': ctype = "jshort"; goto printit;
1404 case 'Z': ctype = "jboolean"; goto printit;
1405 case 'V': ctype = "void"; goto printit;
1406 case 'L':
1407 if (flag_jni)
1409 /* We know about certain types and special-case their names. */
1410 if (! strncmp ((const char *) signature, "Ljava/lang/String;",
1411 sizeof ("Ljava/lang/String;") -1))
1412 ctype = "jstring";
1413 else if (! strncmp ((const char *) signature, "Ljava/lang/Class;",
1414 sizeof ("Ljava/lang/Class;") - 1))
1415 ctype = "jclass";
1416 /* Skip leading 'L' for throwable_p call. */
1417 else if (throwable_p (signature + 1))
1418 ctype = "jthrowable";
1419 else
1420 ctype = "jobject";
1422 while (*signature && *signature != ';')
1423 ++signature;
1425 goto printit;
1427 /* Print a leading "::" so we look in the right namespace. */
1428 fputs ("::", stream);
1429 ++signature;
1430 while (*signature && *signature != ';')
1432 int ch = UTF8_GET (signature, limit);
1433 if (ch == '/')
1434 fputs ("::", stream);
1435 else
1436 jcf_print_char (stream, ch);
1438 fputs (" *", stream);
1439 if (*signature == ';')
1440 signature++;
1441 *need_space = 0;
1442 break;
1443 default:
1444 *need_space = 1;
1445 jni_print_char (stream, *signature++);
1446 break;
1447 printit:
1448 signature++;
1449 *need_space = 1;
1450 fputs (ctype, stream);
1451 break;
1454 if (! flag_jni)
1456 while (array_depth-- > 0)
1457 fputs ("> *", stream);
1460 return signature;
1463 static void
1464 print_c_decl (FILE* stream, JCF* jcf, int name_index, int signature_index,
1465 int is_init, const char *name_override, int flags)
1467 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1469 fprintf (stream, "<not a UTF8 constant>");
1470 found_error = 1;
1472 else
1474 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1475 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1476 const unsigned char *str = str0;
1477 const unsigned char *limit = str + length;
1478 int need_space = 0;
1479 int is_method = str[0] == '(';
1480 const unsigned char *next;
1482 /* If printing a method, skip to the return signature and print
1483 that first. However, there is no return value if this is a
1484 constructor. */
1485 if (is_method && ! is_init)
1487 while (str < limit)
1489 int ch = *str++;
1490 if (ch == ')')
1491 break;
1495 /* If printing a field or an ordinary method, then print the
1496 "return value" now. */
1497 if (! is_method || ! is_init)
1499 next = decode_signature_piece (stream, str, limit, &need_space);
1500 if (! next)
1502 error ("unparseable signature: '%s'", str0);
1503 return;
1507 /* Force the alignment of the first data member. This is
1508 because the "new" C++ ABI changed the alignment of non-POD
1509 classes. gcj, however, still uses the "old" alignment. */
1510 if (is_first_data_member && ! (flags & ACC_STATIC) && ! is_method)
1512 is_first_data_member = 0;
1513 print_cxx_classname (out, " __attribute__((aligned(__alignof__( ",
1514 jcf, jcf->super_class, 1);
1515 fputs (" )))) ", stream);
1518 /* Now print the name of the thing. */
1519 if (need_space)
1520 fputs (" ", stream);
1521 print_full_cxx_name (stream, jcf, name_index,
1522 signature_index, is_init, name_override,
1523 flags);
1527 /* Print the unqualified method name followed by the signature. */
1528 static void
1529 print_full_cxx_name (FILE* stream, JCF* jcf, int name_index,
1530 int signature_index, int is_init,
1531 const char *name_override, int flags)
1533 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1534 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1535 const unsigned char *str = str0;
1536 const unsigned char *limit = str + length;
1537 int need_space = 0;
1538 int is_method = str[0] == '(';
1539 const unsigned char *next;
1541 if (name_override)
1542 fputs (name_override, stream);
1543 else if (name_index)
1545 /* Declare constructors specially. */
1546 if (is_init)
1547 print_base_classname (stream, jcf, jcf->this_class);
1548 else
1549 print_name (stream, jcf, name_index);
1552 if (flag_jni)
1554 unsigned char *signature = JPOOL_UTF_DATA (jcf, signature_index);
1555 int sig_len = JPOOL_UTF_LENGTH (jcf, signature_index);
1556 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf, name_index),
1557 JPOOL_UTF_LENGTH (jcf, name_index),
1558 (const char *) signature, sig_len))
1560 /* If this method is overloaded by another native method,
1561 then include the argument information in the mangled
1562 name. */
1563 unsigned char *limit = signature + sig_len;
1564 fputs ("__", stream);
1565 while (signature < limit)
1567 int ch = UTF8_GET (signature, limit);
1568 jni_print_char (stream, ch);
1569 if (ch == ')')
1571 /* Done. */
1572 break;
1578 if (is_method)
1580 /* Have a method or a constructor. Print signature pieces
1581 until done. */
1582 fputs (" (", stream);
1584 str = str0 + 1;
1586 /* In JNI mode, add extra arguments. */
1587 if (flag_jni)
1589 /* FIXME: it would be nice to know if we are printing a decl
1590 or a definition, and only print `env' for the latter. */
1591 fputs ("JNIEnv *env", stream);
1593 fputs ((flags & ACC_STATIC) ? ", jclass" : ", jobject", stream);
1595 if (*str != ')')
1596 fputs (", ", stream);
1599 while (str < limit && *str != ')')
1601 next = decode_signature_piece (stream, str, limit, &need_space);
1602 if (! next)
1604 error ("unparseable signature: '%s'", str0);
1605 return;
1608 if (next < limit && *next != ')')
1609 fputs (", ", stream);
1610 str = next;
1613 fputs (")", stream);
1617 /* This is a helper for print_stub_or_jni. */
1618 static void
1619 print_name_for_stub_or_jni (FILE *stream, JCF *jcf, int name_index,
1620 int signature_index, int is_init,
1621 const char *name_override, int flags)
1623 const char *const prefix = flag_jni ? "Java_" : "";
1624 print_cxx_classname (stream, prefix, jcf, jcf->this_class, 1);
1625 fputs (flag_jni ? "_" : "::", stream);
1626 print_full_cxx_name (stream, jcf, name_index,
1627 signature_index, is_init, name_override,
1628 flags);
1631 static void
1632 print_stub_or_jni (FILE* stream, JCF* jcf, int name_index,
1633 int signature_index, int is_init,
1634 const char *name_override, int flags)
1636 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1638 fprintf (stream, "<not a UTF8 constant>");
1639 found_error = 1;
1641 else
1643 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1644 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1645 const unsigned char *str = str0;
1646 const unsigned char *limit = str + length;
1647 int need_space = 0;
1648 int is_method = str[0] == '(';
1649 const unsigned char *next;
1651 /* Don't print fields in the JNI case. */
1652 if (! is_method && flag_jni)
1653 return;
1655 if (flag_jni && ! stubs)
1656 fputs ("JNIEXPORT ", stream);
1658 /* If printing a method, skip to the return signature and print
1659 that first. However, there is no return value if this is a
1660 constructor. */
1661 if (is_method && ! is_init)
1663 while (str < limit)
1665 int ch = *str++;
1666 if (ch == ')')
1667 break;
1671 /* If printing a field or an ordinary method, then print the
1672 "return value" now. Note that a constructor can't be native,
1673 so we don't bother checking this in the JNI case. */
1674 if (! is_method || ! is_init)
1676 next = decode_signature_piece (stream, str, limit, &need_space);
1677 if (! next)
1679 error ("unparseable signature: '%s'", str0);
1680 return;
1684 /* When printing a JNI header we need to respect the space. In
1685 other cases we're just going to insert a newline anyway. */
1686 fputs (need_space && ! stubs ? " " : "\n", stream);
1688 if (flag_jni && ! stubs)
1689 fputs ("JNICALL ", stream);
1691 /* Now print the name of the thing. */
1692 print_name_for_stub_or_jni (stream, jcf, name_index,
1693 signature_index, is_init, name_override,
1694 flags);
1696 /* Print the body. */
1697 if (stubs)
1699 if (flag_jni)
1700 fputs ("\n{\n (*env)->FatalError (env, \"", stream);
1701 else
1702 fputs ("\n{\n throw new ::java::lang::UnsupportedOperationException (JvNewStringLatin1 (\"", stream);
1703 print_name_for_stub_or_jni (stream, jcf, name_index,
1704 signature_index, is_init,
1705 name_override,
1706 flags);
1707 fprintf (stream, " not implemented\")%s;\n}\n\n",
1708 flag_jni ? "" : ")");
1713 static void
1714 print_mangled_classname (FILE *stream, JCF *jcf, const char *prefix, int index)
1716 int name_index = JPOOL_USHORT1 (jcf, index);
1717 fputs (prefix, stream);
1718 jcf_print_utf8_replace (out,
1719 JPOOL_UTF_DATA (jcf, name_index),
1720 JPOOL_UTF_LENGTH (jcf, name_index),
1721 '/', '_');
1724 /* Print PREFIX, then a class name in C++ format. If the name refers
1725 to an array, ignore it and don't print PREFIX. Returns 1 if
1726 something was printed, 0 otherwise. */
1727 static int
1728 print_cxx_classname (FILE *stream, const char *prefix,
1729 JCF *jcf, int index, int add_scope)
1731 int name_index = JPOOL_USHORT1 (jcf, index);
1732 int len, c;
1733 const unsigned char *s, *p, *limit;
1735 s = JPOOL_UTF_DATA (jcf, name_index);
1736 len = JPOOL_UTF_LENGTH (jcf, name_index);
1737 limit = s + len;
1739 /* Explicitly omit arrays here. */
1740 p = s;
1741 c = UTF8_GET (p, limit);
1742 if (c == '[')
1743 return 0;
1745 fputs (prefix, stream);
1747 /* Print a leading "::" so we look in the right namespace. */
1748 if (! flag_jni && ! stubs && add_scope)
1749 fputs ("::", stream);
1751 while (s < limit)
1753 c = UTF8_GET (s, limit);
1754 if (c == '/')
1755 fputs (flag_jni ? "_" : "::", stream);
1756 else
1757 jni_print_char (stream, c);
1760 return 1;
1763 int written_class_count = 0;
1765 /* Return name of superclass. If LEN is not NULL, fill it with length
1766 of name. */
1767 static const unsigned char *
1768 super_class_name (JCF *derived_jcf, int *len)
1770 int supername_index = JPOOL_USHORT1 (derived_jcf, derived_jcf->super_class);
1771 int supername_length = JPOOL_UTF_LENGTH (derived_jcf, supername_index);
1772 const unsigned char *supername =
1773 JPOOL_UTF_DATA (derived_jcf, supername_index);
1775 if (len)
1776 *len = supername_length;
1778 return supername;
1781 static void
1782 handle_inner_classes (int count)
1784 int i;
1786 if (out && ! flag_jni && ! stubs && count > 0)
1787 fprintf (out, "\n");
1789 for (i = 0; i < count; ++i)
1791 JCF_u2 inner_info_index = JCF_readu2 (current_jcf);
1793 /* There are a few more values here, but we don't care about
1794 them. The (void) cast is apparently the only way to avoid a
1795 warning here. */
1796 (void) JCF_readu2 (current_jcf);
1797 (void) JCF_readu2 (current_jcf);
1798 (void) JCF_readu2 (current_jcf);
1800 if (out && ! flag_jni && ! stubs)
1802 print_mangled_classname (out, current_jcf, " friend class ",
1803 inner_info_index);
1804 fprintf (out, ";\n");
1811 /* We keep track of all the `#include's we generate, so we can avoid
1812 duplicates. */
1813 struct include
1815 char *name;
1816 struct include *next;
1819 /* List of all includes. */
1820 static struct include *all_includes = NULL;
1822 /* Generate a #include. */
1823 static void
1824 print_include (FILE *out, const unsigned char *utf8, int len)
1826 struct include *incl;
1828 if (! out)
1829 return;
1831 if (len == -1)
1832 len = strlen ((const char *) utf8);
1834 for (incl = all_includes; incl; incl = incl->next)
1836 /* We check the length because we might have a proper prefix. */
1837 if (len == (int) strlen (incl->name)
1838 && ! strncmp (incl->name, (const char *) utf8, len))
1839 return;
1842 incl = XNEW (struct include);
1843 incl->name = XNEWVEC (char, len + 1);
1844 strncpy (incl->name, (const char *) utf8, len);
1845 incl->name[len] = '\0';
1846 incl->next = all_includes;
1847 all_includes = incl;
1849 fputs ("#include <", out);
1850 jcf_print_utf8_replace (out, utf8, len,
1851 '/',
1852 flag_jni ? '_' : '/');
1853 fputs (".h>\n", out);
1858 /* This is used to represent part of a package or class name. */
1859 struct namelet
1861 /* The text of this part of the name. */
1862 char *name;
1863 /* True if this represents a class. */
1864 int is_class;
1865 /* Linked list of all classes and packages inside this one. */
1866 struct namelet *subnamelets;
1867 /* Pointer to next sibling. */
1868 struct namelet *next;
1871 static void add_namelet (const unsigned char *, const unsigned char *,
1872 struct namelet *);
1873 static void print_namelet (FILE *, struct namelet *, int);
1875 /* The special root namelet. */
1876 static struct namelet root =
1878 NULL,
1880 NULL,
1881 NULL
1884 /* This extracts the next name segment from the full UTF-8 encoded
1885 package or class name and links it into the tree. It does this
1886 recursively. */
1887 static void
1888 add_namelet (const unsigned char *name, const unsigned char *name_limit,
1889 struct namelet *parent)
1891 const unsigned char *p;
1892 struct namelet *n = NULL, *np;
1894 /* We want to skip the standard namespaces that we assume the
1895 runtime already knows about. We only do this at the top level,
1896 though, hence the check for `root'. */
1897 if (parent == &root)
1899 #define JAVALANG "java/lang/"
1900 #define JAVAIO "java/io/"
1901 #define JAVAUTIL "java/util/"
1902 if ((name_limit - name >= (int) sizeof (JAVALANG) - 1
1903 && ! strncmp ((const char *) name, JAVALANG, sizeof (JAVALANG) - 1))
1904 || (name_limit - name >= (int) sizeof (JAVAUTIL) - 1
1905 && ! strncmp ((const char *) name, JAVAUTIL, sizeof (JAVAUTIL) - 1))
1906 || (name_limit - name >= (int) sizeof (JAVAIO) - 1
1907 && ! strncmp ((const char *) name, JAVAIO, sizeof (JAVAIO) - 1)))
1908 return;
1911 for (p = name; p < name_limit && *p != '/'; ++p)
1914 /* Search for this name beneath the PARENT node. */
1915 for (np = parent->subnamelets; np != NULL; np = np->next)
1917 /* We check the length because we might have a proper prefix. */
1918 if ((int) strlen (np->name) == p - name &&
1919 ! strncmp ((const char *) name, np->name, p - name))
1921 n = np;
1922 break;
1926 if (n == NULL)
1928 n = XNEW (struct namelet);
1929 n->name = XNEWVEC (char, p - name + 1);
1930 strncpy (n->name, (const char *) name, p - name);
1931 n->name[p - name] = '\0';
1932 n->is_class = (p == name_limit);
1933 n->subnamelets = NULL;
1934 n->next = parent->subnamelets;
1935 parent->subnamelets = n;
1938 /* We recurse if there is more text, and if the trailing piece does
1939 not represent an inner class. */
1940 if (p < name_limit)
1941 add_namelet (p + 1, name_limit, n);
1944 /* Print a single namelet. Destroys namelets while printing. */
1945 static void
1946 print_namelet (FILE *out, struct namelet *name, int depth)
1948 int i, term = 0;
1949 struct namelet *c;
1951 if (name->name)
1953 for (i = 0; i < depth; ++i)
1954 fputc (' ', out);
1955 fprintf (out, "%s %s", name->is_class ? "class" : "namespace",
1956 name->name);
1957 if (name->is_class && name->subnamelets == NULL)
1958 fputs (";\n", out);
1959 else
1961 term = 1;
1962 fputs ("\n", out);
1963 for (i = 0; i < depth; ++i)
1964 fputc (' ', out);
1965 fputs ("{\n", out);
1969 c = name->subnamelets;
1970 while (c != NULL)
1972 struct namelet *next = c->next;
1973 print_namelet (out, c, depth + 2);
1974 c = next;
1976 name->subnamelets = NULL;
1978 if (name->name)
1980 if (term)
1982 for (i = 0; i < depth; ++i)
1983 fputc (' ', out);
1984 fputs ("}\n", out);
1985 /* Only print a `;' when printing a class. C++ is evil. */
1986 if (name->is_class)
1987 fputs (";", out);
1990 free (name->name);
1991 free (name);
1995 /* This is called to add some classes to the list of classes for which
1996 we need decls. The signature argument can be a function
1997 signature. */
1998 static void
1999 add_class_decl (FILE *out, JCF *jcf, JCF_u2 signature)
2001 const unsigned char *s = JPOOL_UTF_DATA (jcf, signature);
2002 int len = JPOOL_UTF_LENGTH (jcf, signature);
2003 int i;
2005 for (i = 0; i < len; ++i)
2007 int start;
2009 /* If we see an array, then we include the array header. */
2010 if (s[i] == '[')
2012 print_include (out, (const unsigned char *) "gcj/array", -1);
2013 continue;
2016 /* We're looking for `L<stuff>;' -- everything else is
2017 ignorable. */
2018 if (s[i] != 'L')
2019 continue;
2021 for (start = ++i; i < len && s[i] != ';'; ++i)
2024 add_namelet (&s[start], &s[i], &root);
2028 /* Print declarations for all classes required by this class. Any
2029 class or package in the `java' package is assumed to be handled
2030 statically in libjava; we don't generate declarations for these.
2031 This makes the generated headers a bit easier to read. */
2032 static void
2033 print_class_decls (FILE *out, JCF *jcf, int self)
2035 /* Make sure to always add the current class to the list of things
2036 that should be declared. */
2037 int name_index = JPOOL_USHORT1 (jcf, self);
2038 int len;
2039 const unsigned char *s;
2041 s = JPOOL_UTF_DATA (jcf, name_index);
2042 len = JPOOL_UTF_LENGTH (jcf, name_index);
2043 add_namelet (s, s + len, &root);
2045 if (root.subnamelets)
2047 fputs ("extern \"Java\"\n{\n", out);
2048 /* We use an initial offset of 0 because the root namelet
2049 doesn't cause anything to print. */
2050 print_namelet (out, &root, 0);
2051 fputs ("}\n\n", out);
2057 static void
2058 process_file (JCF *jcf, FILE *out)
2060 int code, i;
2061 uint32 field_start, method_end, method_start;
2063 current_jcf = jcf;
2065 last_access = -1;
2067 if (jcf_parse_preamble (jcf) != 0)
2069 error ("Not a valid Java .class file.");
2070 return;
2073 /* Parse and possibly print constant pool */
2074 code = jcf_parse_constant_pool (jcf);
2075 if (code != 0)
2077 error ("error while parsing constant pool");
2078 return;
2080 code = verify_constant_pool (jcf);
2081 if (code > 0)
2083 error ("error in constant pool entry #%d", code);
2084 return;
2087 jcf_parse_class (jcf);
2089 if (written_class_count++ == 0 && out)
2091 const char *cstart, *cstart2, *mode, *cend, *what, *jflag;
2092 if (flag_jni)
2094 cstart = "/*";
2095 cstart2 = " ";
2096 cend = " */";
2097 mode = "";
2098 what = "JNI";
2099 jflag = " -jni";
2101 else
2103 cstart = "//";
2104 cstart2 = "//";
2105 cend = "";
2106 mode = " -*- c++ -*-";
2107 what = "CNI";
2108 jflag = "";
2111 if (! stubs)
2112 fprintf (out, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
2113 cstart, mode, cend);
2114 else
2116 fprintf (out, "%s This file was created by `" TOOLNAME " -stubs%s'.%s\n\
2117 %s\n\
2118 %s This file is intended to give you a head start on implementing native\n\
2119 %s methods using %s.\n\
2120 %s Be aware: running `" TOOLNAME " -stubs %s' once more for this class may\n\
2121 %s overwrite any edits you have made to this file.%s\n\n",
2122 cstart, jflag, mode,
2123 cstart2,
2124 cstart2,
2125 cstart2,
2126 what,
2127 cstart2,
2128 jflag,
2129 cstart2,
2130 cend);
2134 if (out)
2136 if (! stubs)
2138 print_mangled_classname (out, jcf, "#ifndef __", jcf->this_class);
2139 fprintf (out, "__\n");
2141 print_mangled_classname (out, jcf, "#define __", jcf->this_class);
2142 fprintf (out, "__\n\n");
2144 if (flag_jni)
2146 fprintf (out, "#include <jni.h>\n\n");
2147 fprintf (out, "#ifdef __cplusplus\n");
2148 fprintf (out, "extern \"C\"\n");
2149 fprintf (out, "{\n");
2150 fprintf (out, "#endif\n");
2152 else
2154 /* We do this to ensure that inline methods won't be
2155 `outlined' by g++. This works as long as method and
2156 fields are not added by the user. */
2157 fprintf (out, "#pragma interface\n");
2159 if (jcf->super_class)
2161 int super_length;
2162 const unsigned char *supername =
2163 super_class_name (jcf, &super_length);
2165 fputs ("\n", out);
2166 print_include (out, supername, super_length);
2170 else
2172 /* Strip off the ".class" portion of the name when printing
2173 the include file name. */
2174 char *name;
2175 int i, len = strlen (jcf->classname);
2176 if (len > 6 && ! strcmp (&jcf->classname[len - 6], ".class"))
2177 len -= 6;
2178 /* Turn the class name into a file name. */
2179 name = XNEWVEC (char, len + 1);
2180 for (i = 0; i < len; ++i)
2181 name[i] = jcf->classname[i] == '.' ? '/' : jcf->classname[i];
2182 name[i] = '\0';
2183 print_include (out, (const unsigned char *) name, len);
2184 free (name);
2186 if (! flag_jni)
2188 print_include (out, (const unsigned char *) "gcj/cni", -1);
2189 print_include (out, (const unsigned char *) "java/lang/UnsupportedOperationException",
2190 -1);
2195 /* We want to parse the methods first. But we need to find where
2196 they start. So first we skip the fields, then parse the methods.
2197 Then we parse the fields and skip the methods. This is ugly, but
2198 not too bad since we need two full passes to get class decl
2199 information anyway. */
2200 field_pass = 0;
2201 field_start = JCF_TELL (jcf);
2202 jcf_parse_fields (jcf);
2204 method_start = JCF_TELL (jcf);
2205 method_pass = 0;
2206 jcf_parse_methods (jcf);
2208 if (out)
2209 fputs ("\n", out);
2211 if (out && ! flag_jni)
2213 if (! stubs)
2214 print_class_decls (out, jcf, jcf->this_class);
2216 for (i = 0; i < prepend_count; ++i)
2217 fprintf (out, "%s\n", prepend_specs[i]);
2218 if (prepend_count > 0)
2219 fputc ('\n', out);
2221 if (! stubs)
2223 if (! print_cxx_classname (out, "class ", jcf,
2224 jcf->this_class, 0))
2226 error ("class is of array type\n");
2227 return;
2229 if (jcf->super_class)
2231 if (! print_cxx_classname (out, " : public ",
2232 jcf, jcf->super_class, 1))
2234 error ("base class is of array type");
2235 return;
2239 fputs ("\n{\n", out);
2243 /* Now go back for second pass over methods and fields. */
2244 is_first_data_member = 1;
2246 JCF_SEEK (jcf, method_start);
2247 method_pass = 1;
2248 jcf_parse_methods (jcf);
2249 method_end = JCF_TELL (jcf);
2251 field_pass = 1;
2252 JCF_SEEK (jcf, field_start);
2253 jcf_parse_fields (jcf);
2254 JCF_SEEK (jcf, method_end);
2256 jcf_parse_final_attributes (jcf);
2258 if (out && ! stubs)
2260 if (flag_jni)
2262 fprintf (out, "\n#ifdef __cplusplus\n");
2263 fprintf (out, "}\n");
2264 fprintf (out, "#endif\n");
2266 else
2268 /* Generate friend decl if we still must. */
2269 for (i = 0; i < friend_count; ++i)
2270 fprintf (out, " friend %s\n", friend_specs[i]);
2272 /* Generate extra declarations. */
2273 if (add_count > 0)
2274 fputc ('\n', out);
2275 for (i = 0; i < add_count; ++i)
2276 fprintf (out, " %s\n", add_specs[i]);
2278 /* Generate an entry for the class object. */
2279 generate_access (out, ACC_PUBLIC);
2280 fprintf (out, "\n static ::java::lang::Class class$;\n");
2282 fputs ("}", out);
2284 if (jcf->access_flags & ACC_INTERFACE)
2285 fputs (" __attribute__ ((java_interface))", out);
2287 fputs (";\n", out);
2289 if (append_count > 0)
2290 fputc ('\n', out);
2291 for (i = 0; i < append_count; ++i)
2292 fprintf (out, "%s\n", append_specs[i]);
2295 print_mangled_classname (out, jcf,
2296 "\n#endif /* __", jcf->this_class);
2297 fprintf (out, "__ */\n");
2303 /* This is used to mark options with no short value. */
2304 #define LONG_OPT(Num) ((Num) + 128)
2306 #define OPT_classpath LONG_OPT (0)
2307 #define OPT_CLASSPATH OPT_classpath
2308 #define OPT_bootclasspath LONG_OPT (1)
2309 #define OPT_extdirs LONG_OPT (2)
2310 #define OPT_HELP LONG_OPT (3)
2311 #define OPT_TEMP LONG_OPT (4)
2312 #define OPT_VERSION LONG_OPT (5)
2313 #define OPT_PREPEND LONG_OPT (6)
2314 #define OPT_FRIEND LONG_OPT (7)
2315 #define OPT_ADD LONG_OPT (8)
2316 #define OPT_APPEND LONG_OPT (9)
2317 #define OPT_M LONG_OPT (10)
2318 #define OPT_MM LONG_OPT (11)
2319 #define OPT_MG LONG_OPT (12)
2320 #define OPT_MD LONG_OPT (13)
2321 #define OPT_MMD LONG_OPT (14)
2322 #define OPT_FORCE LONG_OPT (15)
2323 #define OPT_OLD LONG_OPT (16)
2324 #define OPT_TRACE LONG_OPT (17)
2326 static const struct option options[] =
2328 { "classpath", required_argument, NULL, OPT_classpath },
2329 { "bootclasspath", required_argument, NULL, OPT_bootclasspath },
2330 { "extdirs", required_argument, NULL, OPT_extdirs },
2331 { "CLASSPATH", required_argument, NULL, OPT_CLASSPATH },
2332 { "help", no_argument, NULL, OPT_HELP },
2333 { "stubs", no_argument, &stubs, 1 },
2334 { "td", required_argument, NULL, OPT_TEMP },
2335 { "verbose", no_argument, NULL, 'v' },
2336 { "version", no_argument, NULL, OPT_VERSION },
2337 { "prepend", required_argument, NULL, OPT_PREPEND },
2338 { "friend", required_argument, NULL, OPT_FRIEND },
2339 { "add", required_argument, NULL, OPT_ADD },
2340 { "append", required_argument, NULL, OPT_APPEND },
2341 { "M", no_argument, NULL, OPT_M },
2342 { "MM", no_argument, NULL, OPT_MM },
2343 { "MG", no_argument, NULL, OPT_MG },
2344 { "MD", no_argument, NULL, OPT_MD },
2345 { "MMD", no_argument, NULL, OPT_MMD },
2346 { "jni", no_argument, &flag_jni, 1 },
2347 { "force", no_argument, NULL, OPT_FORCE },
2348 /* If the output file should be named "ld" then a space is needed
2349 between -o and its argument, ld. */
2350 { "old", no_argument, NULL, OPT_OLD },
2351 { "trace", no_argument, NULL, OPT_TRACE },
2352 { NULL, required_argument, NULL, 'J' },
2353 { NULL, no_argument, NULL, 0 }
2356 static void
2357 usage (void)
2359 fprintf (stderr, _("Try '" TOOLNAME " --help' for more information.\n"));
2360 exit (1);
2363 static void
2364 help (void)
2366 printf (_("Usage: " TOOLNAME " [OPTION]... CLASS...\n\n"));
2367 printf (_("Generate C or C++ header files from .class files\n\n"));
2368 printf (_(" -stubs Generate an implementation stub file\n"));
2369 printf (_(" -jni Generate a JNI header or stub\n"));
2370 printf (_(" -force Always overwrite output files\n"));
2371 printf (_(" -old Unused compatibility option\n"));
2372 printf (_(" -trace Unused compatibility option\n"));
2373 printf (_(" -J OPTION Unused compatibility option\n"));
2374 printf ("\n");
2375 printf (_(" -add TEXT Insert TEXT into class body\n"));
2376 printf (_(" -append TEXT Insert TEXT after class declaration\n"));
2377 printf (_(" -friend TEXT Insert TEXT as 'friend' declaration\n"));
2378 printf (_(" -prepend TEXT Insert TEXT before start of class\n"));
2379 printf ("\n");
2380 printf (_(" --classpath PATH Set path to find .class files\n"));
2381 printf (_(" -IDIR Append directory to class path\n"));
2382 printf (_(" --bootclasspath PATH Override built-in class path\n"));
2383 printf (_(" --extdirs PATH Set extensions directory path\n"));
2384 printf (_(" -d DIRECTORY Set output directory name\n"));
2385 printf (_(" -o FILE Set output file name\n"));
2386 printf (_(" -td DIRECTORY Set temporary directory name\n"));
2387 printf ("\n");
2388 printf (_(" --help Print this help, then exit\n"));
2389 printf (_(" --version Print version number, then exit\n"));
2390 printf (_(" -v, --verbose Print extra information while running\n"));
2391 printf ("\n");
2392 printf (_(" -M Print all dependencies to stdout;\n"
2393 " suppress ordinary output\n"));
2394 printf (_(" -MM Print non-system dependencies to stdout;\n"
2395 " suppress ordinary output\n"));
2396 printf (_(" -MD Print all dependencies to stdout\n"));
2397 printf (_(" -MMD Print non-system dependencies to stdout\n"));
2398 /* We omit -MG until it is implemented. */
2399 printf ("\n");
2400 printf (_("For bug reporting instructions, please see:\n"
2401 "%s.\n"), bug_report_url);
2402 exit (0);
2405 static void
2406 version (void)
2408 printf (TOOLNAME " (GCC) %s\n\n", version_string);
2409 printf ("Copyright %s 2006 Free Software Foundation, Inc.\n", _("(C)"));
2410 printf (_("This is free software; see the source for copying conditions. There is NO\n"
2411 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
2412 exit (0);
2416 main (int argc, char** argv)
2418 JCF jcf;
2419 int argi;
2420 char *output_file = NULL;
2421 int emit_dependencies = 0, suppress_output = 0;
2422 int opt;
2423 int local_found_error;
2425 /* Unlock the stdio streams. */
2426 unlock_std_streams ();
2428 gcc_init_libintl ();
2430 if (argc <= 1)
2432 error ("no classes specified");
2433 usage ();
2436 jcf_path_init ();
2438 /* We use getopt_long_only to allow single `-' long options. For
2439 some of our options this is more natural. */
2440 while ((opt = getopt_long_only (argc, argv, "J:I:d:o:v", options, NULL)) != -1)
2442 switch (opt)
2444 case 0:
2445 /* Already handled. */
2446 break;
2448 case 'o':
2449 output_file = optarg;
2450 break;
2452 case 'd':
2453 output_directory = optarg;
2454 break;
2456 case 'I':
2457 jcf_path_include_arg (optarg);
2458 break;
2460 case 'v':
2461 verbose++;
2462 break;
2464 case OPT_classpath:
2465 jcf_path_classpath_arg (optarg);
2466 break;
2468 case OPT_bootclasspath:
2469 jcf_path_bootclasspath_arg (optarg);
2470 break;
2472 case OPT_extdirs:
2473 jcf_path_extdirs_arg (optarg);
2474 break;
2476 case OPT_HELP:
2477 help ();
2478 break;
2480 case OPT_TEMP:
2481 temp_directory = optarg;
2482 break;
2484 case OPT_VERSION:
2485 version ();
2486 break;
2488 case OPT_PREPEND:
2489 if (prepend_count == 0)
2490 prepend_specs = XNEWVEC (char *, argc);
2491 prepend_specs[prepend_count++] = optarg;
2492 break;
2494 case OPT_FRIEND:
2495 if (friend_count == 0)
2496 friend_specs = XNEWVEC (char *, argc);
2497 friend_specs[friend_count++] = optarg;
2498 break;
2500 case OPT_ADD:
2501 if (add_count == 0)
2502 add_specs = XNEWVEC (char *, argc);
2503 add_specs[add_count++] = optarg;
2504 break;
2506 case OPT_APPEND:
2507 if (append_count == 0)
2508 append_specs = XNEWVEC (char *, argc);
2509 append_specs[append_count++] = optarg;
2510 break;
2512 case OPT_M:
2513 emit_dependencies = 1;
2514 suppress_output = 1;
2515 jcf_dependency_init (1);
2516 break;
2518 case OPT_MM:
2519 emit_dependencies = 1;
2520 suppress_output = 1;
2521 jcf_dependency_init (0);
2522 break;
2524 case OPT_MG:
2525 error ("'-MG' option is unimplemented");
2526 exit (1);
2528 case OPT_MD:
2529 emit_dependencies = 1;
2530 jcf_dependency_init (1);
2531 break;
2533 case OPT_MMD:
2534 emit_dependencies = 1;
2535 jcf_dependency_init (0);
2536 break;
2538 case OPT_FORCE:
2539 break;
2541 case OPT_OLD:
2542 break;
2544 case OPT_TRACE:
2545 break;
2547 case 'J':
2548 /* Ignore -J options. */
2549 break;
2551 default:
2552 usage ();
2553 break;
2557 if (optind == argc)
2559 error ("no classes specified");
2560 usage ();
2563 jcf_path_seal (verbose);
2565 if (output_file && emit_dependencies)
2567 error ("can't specify both -o and -MD");
2568 exit (1);
2571 local_found_error = 0;
2572 for (argi = optind; argi < argc; argi++)
2574 char *classname = argv[argi];
2575 char *current_output_file = NULL;
2576 const char *classfile_name;
2578 /* We reset the error state here so that we can detect errors
2579 that occur when processing this file, so the output can be
2580 unlinked if need be. */
2581 found_error = 0;
2583 if (verbose)
2584 printf (_("Processing %s\n"), classname);
2585 if (! output_file)
2586 jcf_dependency_reset ();
2587 classfile_name = find_class (classname, strlen (classname), &jcf, 0);
2588 if (classfile_name == NULL)
2590 error ("%s: no such class", classname);
2591 exit (1);
2593 if (verbose)
2594 printf (_("Found in %s\n"), classfile_name);
2595 if (output_file)
2597 if (strcmp (output_file, "-") == 0)
2598 out = stdout;
2599 else if (out == NULL)
2601 out = fopen (output_file, "w");
2603 if (out == NULL)
2605 perror (output_file);
2606 exit (1);
2608 current_output_file = output_file;
2610 else
2612 int dir_len = strlen (output_directory);
2613 int i, classname_length = strlen (classname);
2614 current_output_file = XNEWVEC (char, dir_len + classname_length + 5);
2615 strcpy (current_output_file, output_directory);
2616 if (dir_len > 0 && output_directory[dir_len-1] != '/')
2617 current_output_file[dir_len++] = '/';
2618 for (i = 0; classname[i] != '\0'; i++)
2620 char ch = classname[i];
2621 if (ch == '.')
2622 ch = '/';
2623 if (flag_jni && ch == '/')
2624 ch = '_';
2625 current_output_file[dir_len++] = ch;
2627 if (emit_dependencies)
2629 if (suppress_output)
2631 jcf_dependency_set_dep_file ("-");
2632 out = NULL;
2634 else
2636 /* We use `.hd' and not `.d' to avoid clashes with
2637 dependency tracking from straight compilation. */
2638 strcpy (current_output_file + dir_len, ".hd");
2639 jcf_dependency_set_dep_file (current_output_file);
2642 strcpy (current_output_file + dir_len,
2643 stubs ? (flag_jni ? ".c" : ".cc") : ".h");
2644 jcf_dependency_set_target (current_output_file);
2645 if (! suppress_output)
2647 out = fopen (current_output_file, "w");
2648 if (out == NULL)
2650 perror (current_output_file);
2651 exit (1);
2655 free_method_name_list ();
2656 process_file (&jcf, out);
2657 JCF_FINISH (&jcf);
2659 /* If we found an error and we're writing to a real file,
2660 delete it. */
2661 if (found_error && ! suppress_output && current_output_file != NULL
2662 && strcmp (current_output_file, "-"))
2663 unlink (current_output_file);
2665 if (current_output_file != output_file)
2666 free (current_output_file);
2667 jcf_dependency_write ();
2669 local_found_error |= found_error;
2672 if (out != NULL && out != stdout)
2673 fclose (out);
2675 return local_found_error;