* opt-functions.awk (var_type): New function.
[official-gcc.git] / gcc / java / gjavah.c
blobd25dab8d0f790344d4327076e9a3c645e60d4537
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
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, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
24 Java and all Java-based marks are trademarks or registered trademarks
25 of Sun Microsystems, Inc. in the United States and other countries.
26 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
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);
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 *msgid, ...);
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 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
254 if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
256 static int decompiled = 0;
257 #define HANDLE_END_METHOD() \
258 if (out && method_printed && !method_synthetic) \
259 fputs (decompiled || stubs ? "\n" : ";\n", out);
261 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) handle_inner_classes (COUNT)
263 /* We're going to need {peek,skip}_attribute, enable their definition. */
264 #define NEED_PEEK_ATTRIBUTE
265 #define NEED_SKIP_ATTRIBUTE
267 #include "jcf-reader.c"
269 /* Print an error message and set found_error. */
270 static void
271 error (const char *msgid, ...)
273 va_list ap;
275 va_start (ap, msgid);
277 fprintf (stderr, TOOLNAME ": ");
278 vfprintf (stderr, _(msgid), ap);
279 va_end (ap);
280 fprintf (stderr, "\n");
281 found_error = 1;
284 /* Print a single-precision float, suitable for parsing by g++. */
285 static void
286 jni_print_float (FILE *stream, jfloat f)
288 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
289 work in data initializers. FIXME. */
290 if (JFLOAT_FINITE (f))
292 if (flag_jni)
294 fputs (" ", out);
295 if (f.negative)
296 putc ('-', stream);
297 if (f.exponent)
298 fprintf (stream, "0x1.%.6xp%+df",
299 ((unsigned int)f.mantissa) << 1,
300 f.exponent - JFLOAT_EXP_BIAS);
301 else
302 /* Exponent of 0x01 is -125; exponent of 0x00 is *also* -125,
303 because the implicit leading 1 bit is no longer present. */
304 fprintf (stream, "0x0.%.6xp%+df",
305 ((unsigned int)f.mantissa) << 1,
306 f.exponent + 1 - JFLOAT_EXP_BIAS);
309 if (! flag_jni)
310 fputs (";\n", stream);
313 /* Print a double-precision float, suitable for parsing by g++. */
314 static void
315 jni_print_double (FILE *stream, jdouble f)
317 /* It'd be nice to use __builtin_nan/__builtin_inf here but they don't
318 work in data initializers. FIXME. */
319 if (JDOUBLE_FINITE (f))
321 if (flag_jni)
323 fputs (" ", out);
324 if (f.negative)
325 putc ('-', stream);
326 if (f.exponent)
327 fprintf (stream, "0x1.%.5x%.8xp%+d",
328 f.mantissa0, f.mantissa1,
329 f.exponent - JDOUBLE_EXP_BIAS);
330 else
331 /* Exponent of 0x001 is -1022; exponent of 0x000 is *also* -1022,
332 because the implicit leading 1 bit is no longer present. */
333 fprintf (stream, "0x0.%.5x%.8xp%+d",
334 f.mantissa0, f.mantissa1,
335 f.exponent + 1 - JDOUBLE_EXP_BIAS);
338 fputs (flag_jni ? "\n" : ";\n", stream);
341 /* Print a character, appropriately mangled for JNI. */
343 static void
344 jni_print_char (FILE *stream, int ch)
346 if (! flag_jni)
347 jcf_print_char (stream, ch);
348 else if (ch == '(' || ch == ')')
350 /* Ignore. */
352 else if (ch == '_')
353 fputs ("_1", stream);
354 else if (ch == ';')
355 fputs ("_2", stream);
356 else if (ch == '[')
357 fputs ("_3", stream);
358 else if (ch == '/')
359 fputs ("_", stream);
360 else if (ISALNUM (ch))
361 fputc (ch, stream);
362 else
364 /* "Unicode" character. */
365 fprintf (stream, "_0%04x", ch);
369 /* Print a name from the class data. If the index does not point to a
370 string, an error results. */
372 static void
373 print_name (FILE* stream, JCF* jcf, int name_index)
375 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
377 fprintf (stream, "<not a UTF8 constant>");
378 found_error = 1;
380 else if (! flag_jni)
381 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
382 JPOOL_UTF_LENGTH (jcf, name_index));
383 else
385 /* For JNI we must correctly quote each character. */
386 const unsigned char *str = JPOOL_UTF_DATA (jcf, name_index);
387 int length = JPOOL_UTF_LENGTH (jcf, name_index);
388 const unsigned char *limit = str + length;
389 while (str < limit)
391 int ch = UTF8_GET (str, limit);
392 if (ch < 0)
394 fprintf (stream, "\\<invalid>");
395 return;
397 jni_print_char (stream, ch);
402 /* Print base name of class. The base name is everything after the
403 final separator. */
405 static void
406 print_base_classname (FILE *stream, JCF *jcf, int index)
408 int name_index = JPOOL_USHORT1 (jcf, index);
409 int len;
410 const unsigned char *s, *p, *limit;
412 s = JPOOL_UTF_DATA (jcf, name_index);
413 len = JPOOL_UTF_LENGTH (jcf, name_index);
414 limit = s + len;
415 p = s;
416 while (s < limit)
418 int c = UTF8_GET (s, limit);
419 if (c == '/')
420 p = s;
423 while (p < limit)
425 int ch = UTF8_GET (p, limit);
426 if (ch == '/')
427 fputs ("::", stream);
428 else
429 jcf_print_char (stream, ch);
433 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
434 and 1 if STR is "greater" than NAME. */
436 static int
437 utf8_cmp (const unsigned char *str, int length, const char *name)
439 const unsigned char *limit = str + length;
440 int i;
442 for (i = 0; name[i]; ++i)
444 int ch = UTF8_GET (str, limit);
445 if (ch != name[i])
446 return ch - name[i];
449 return str == limit ? 0 : 1;
452 /* This is a sorted list of all C++ keywords. */
454 static const char *const cxx_keywords[] =
456 "_Complex",
457 "__alignof",
458 "__alignof__",
459 "__asm",
460 "__asm__",
461 "__attribute",
462 "__attribute__",
463 "__builtin_va_arg",
464 "__complex",
465 "__complex__",
466 "__const",
467 "__const__",
468 "__extension__",
469 "__imag",
470 "__imag__",
471 "__inline",
472 "__inline__",
473 "__label__",
474 "__null",
475 "__real",
476 "__real__",
477 "__restrict",
478 "__restrict__",
479 "__signed",
480 "__signed__",
481 "__typeof",
482 "__typeof__",
483 "__volatile",
484 "__volatile__",
485 "and",
486 "and_eq",
487 "asm",
488 "auto",
489 "bitand",
490 "bitor",
491 "bool",
492 "break",
493 "case",
494 "catch",
495 "char",
496 "class",
497 "compl",
498 "const",
499 "const_cast",
500 "continue",
501 "default",
502 "delete",
503 "do",
504 "double",
505 "dynamic_cast",
506 "else",
507 "enum",
508 "explicit",
509 "export",
510 "extern",
511 "false",
512 "float",
513 "for",
514 "friend",
515 "goto",
516 "if",
517 "inline",
518 "int",
519 "long",
520 "mutable",
521 "namespace",
522 "new",
523 "not",
524 "not_eq",
525 "operator",
526 "or",
527 "or_eq",
528 "private",
529 "protected",
530 "public",
531 "register",
532 "reinterpret_cast",
533 "return",
534 "short",
535 "signed",
536 "sizeof",
537 "static",
538 "static_cast",
539 "struct",
540 "switch",
541 "template",
542 "this",
543 "throw",
544 "true",
545 "try",
546 "typedef",
547 "typeid",
548 "typename",
549 "typeof",
550 "union",
551 "unsigned",
552 "using",
553 "virtual",
554 "void",
555 "volatile",
556 "wchar_t",
557 "while",
558 "xor",
559 "xor_eq"
563 /* If NAME is the name of a C++ keyword, then return an override name.
564 This is a name that can be used in place of the keyword.
565 Otherwise, return NULL. The return value is malloc()d. */
567 static char *
568 cxx_keyword_subst (const unsigned char *str, int length)
570 int last = ARRAY_SIZE (cxx_keywords);
571 int first = 0;
572 int mid = (last + first) / 2;
573 int old = -1;
575 for (mid = (last + first) / 2;
576 mid != old;
577 old = mid, mid = (last + first) / 2)
579 int kwl = strlen (cxx_keywords[mid]);
580 int min_length = kwl > length ? length : kwl;
581 int r = utf8_cmp (str, min_length, cxx_keywords[mid]);
583 if (r == 0)
585 int i;
587 /* Skip all trailing `$'. */
588 for (i = min_length; i < length && str[i] == '$'; ++i)
590 /* We've only found a match if all the remaining characters
591 are `$'. */
592 if (i == length)
594 char *dup = xmalloc (2 + length - min_length + kwl);
595 strcpy (dup, cxx_keywords[mid]);
596 for (i = kwl; i < length + 1; ++i)
597 dup[i] = '$';
598 dup[i] = '\0';
599 return dup;
601 r = 1;
604 if (r < 0)
605 last = mid;
606 else
607 first = mid;
609 return NULL;
612 /* Generate an access control keyword based on FLAGS. */
614 static void
615 generate_access (FILE *stream, JCF_u2 flags)
617 if ((flags & ACC_VISIBILITY) == last_access)
618 return;
619 last_access = (flags & ACC_VISIBILITY);
621 switch (last_access)
623 case 0:
624 fputs ("public: // actually package-private\n", stream);
625 break;
626 case ACC_PUBLIC:
627 fputs ("public:\n", stream);
628 break;
629 case ACC_PRIVATE:
630 fputs ("private:\n", stream);
631 break;
632 case ACC_PROTECTED:
633 fputs ("public: // actually protected\n", stream);
634 break;
635 default:
636 found_error = 1;
637 fprintf (stream, "#error unrecognized visibility %d\n",
638 (flags & ACC_VISIBILITY));
639 break;
643 /* See if NAME is already the name of a method. */
644 static int
645 name_is_method_p (const unsigned char *name, int length)
647 struct method_name *p;
649 for (p = method_name_list; p != NULL; p = p->next)
651 if (p->length == length && ! memcmp (p->name, name, length))
652 return 1;
654 return 0;
657 /* Free the method name list. */
658 static void
659 free_method_name_list (void)
661 struct method_name *p = method_name_list;
662 while (p != NULL)
664 struct method_name *next = p->next;
665 free (p->name);
666 free (p->signature);
667 free (p);
668 p = next;
670 method_name_list = NULL;
673 /* If there is already a native method named NAME, whose signature is not
674 SIGNATURE, then return true. Otherwise return false. */
675 static int
676 overloaded_jni_method_exists_p (const unsigned char *name, int length,
677 const char *signature, int sig_length)
679 struct method_name *p;
681 for (p = method_name_list; p != NULL; p = p->next)
683 if (p->is_native
684 && p->length == length
685 && ! memcmp (p->name, name, length)
686 && (p->sig_length != sig_length
687 || memcmp (p->signature, signature, sig_length)))
688 return 1;
690 return 0;
693 /* Get name of a field. This handles renamings due to C++ clash. */
694 static char *
695 get_field_name (JCF *jcf, int name_index, JCF_u2 flags)
697 unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
698 int length = JPOOL_UTF_LENGTH (jcf, name_index);
699 char *override;
701 if (name_is_method_p (name, length))
703 /* This field name matches a method. So override the name with
704 a dummy name. This is yucky, but it isn't clear what else to
705 do. FIXME: if the field is static, then we'll be in real
706 trouble. */
707 if ((flags & ACC_STATIC))
709 error ("static field has same name as method");
710 return NULL;
713 override = xmalloc (length + 3);
714 memcpy (override, name, length);
715 strcpy (override + length, "__");
717 else if (flag_jni)
718 override = NULL;
719 else
720 override = cxx_keyword_subst (name, length);
722 return override;
725 /* Print a field name. Convenience function for use with
726 get_field_name. */
727 static void
728 print_field_name (FILE *stream, JCF *jcf, int name_index, JCF_u2 flags)
730 char *override = get_field_name (jcf, name_index, flags);
732 if (override)
734 fputs (override, stream);
735 free (override);
737 else
738 jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
739 JPOOL_UTF_LENGTH (jcf, name_index));
742 static void
743 print_field_info (FILE *stream, JCF* jcf, int name_index, int sig_index,
744 JCF_u2 flags)
746 char *override = NULL;
748 if (! flag_jni)
749 generate_access (stream, flags);
750 if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
752 fprintf (stream, "<not a UTF8 constant>");
753 found_error = 1;
754 return;
757 if (flag_jni)
759 /* For JNI we only want to print real constants. */
760 int val;
761 if (! (flags & ACC_STATIC)
762 || ! (flags & ACC_FINAL)
763 || current_field_value <= 0)
764 return;
765 val = JPOOL_TAG (jcf, current_field_value);
766 if (val != CONSTANT_Integer && val != CONSTANT_Long
767 && val != CONSTANT_Float && val != CONSTANT_Double)
768 return;
770 else
772 /* Initial indentation. */
773 fputs (" ", stream);
776 if ((flags & ACC_STATIC))
778 if (flag_jni)
780 print_cxx_classname (stream, "#undef ", jcf, jcf->this_class, 1);
781 fputs ("_", stream);
782 print_field_name (stream, jcf, name_index, 0);
783 fputs ("\n", stream);
784 print_cxx_classname (stream, "#define ", jcf, jcf->this_class, 1);
785 fputs ("_", stream);
787 else
788 fputs ("static ", stream);
790 if ((flags & ACC_FINAL) && current_field_value > 0)
792 char buffer[25];
793 int done = 1;
795 switch (JPOOL_TAG (jcf, current_field_value))
797 case CONSTANT_Integer:
799 jint num;
800 int most_negative = 0;
801 if (! flag_jni)
802 fputs ("const jint ", stream);
803 print_field_name (stream, jcf, name_index, 0);
804 fputs (flag_jni ? " " : " = ", stream);
805 num = JPOOL_INT (jcf, current_field_value);
806 /* We single out the most negative number to print
807 specially. This avoids later warnings from g++. */
808 if (num == (jint) 0x80000000)
810 most_negative = 1;
811 ++num;
813 format_int (buffer, (jlong) num, 10);
814 fprintf (stream, "%sL%s%s\n", buffer,
815 most_negative ? " - 1" : "",
816 flag_jni ? "" : ";");
818 break;
819 case CONSTANT_Long:
821 jlong num;
822 int most_negative = 0;
823 if (! flag_jni)
824 fputs ("const jlong ", stream);
825 print_field_name (stream, jcf, name_index, 0);
826 fputs (flag_jni ? " " : " = ", stream);
827 num = JPOOL_LONG (jcf, current_field_value);
828 /* We single out the most negative number to print
829 specially.. This avoids later warnings from g++. */
830 if (num == (jlong) 0x8000000000000000LL)
832 most_negative = 1;
833 ++num;
835 format_int (buffer, num, 10);
836 fprintf (stream, "%sLL%s%s\n", buffer,
837 most_negative ? " - 1" :"",
838 flag_jni ? "" : ";");
840 break;
841 case CONSTANT_Float:
843 jfloat fnum = JPOOL_FLOAT (jcf, current_field_value);
844 if (! flag_jni)
845 fputs ("const jfloat ", stream);
846 print_field_name (stream, jcf, name_index, 0);
847 jni_print_float (stream, fnum);
849 break;
850 case CONSTANT_Double:
852 jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value);
853 if (! flag_jni)
854 fputs ("const jdouble ", stream);
855 print_field_name (stream, jcf, name_index, 0);
856 jni_print_double (stream, dnum);
858 break;
859 default:
860 /* We can't print this as a constant, but we can still
861 print something sensible. */
862 done = 0;
863 break;
866 if (done)
867 return;
871 /* assert (! flag_jni); */
872 override = get_field_name (jcf, name_index, flags);
873 print_c_decl (stream, jcf, name_index, sig_index, 0, override, flags);
874 fputs (";\n", stream);
876 if (override)
877 free (override);
881 static void
882 print_method_info (FILE *stream, JCF* jcf, int name_index, int sig_index,
883 JCF_u2 flags)
885 const unsigned char *str;
886 int length, is_init = 0;
887 char *override = NULL;
889 method_declared = 0;
890 method_access = flags;
891 if (stream && JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
892 fprintf (stream, "<not a UTF8 constant>");
893 str = JPOOL_UTF_DATA (jcf, name_index);
894 length = JPOOL_UTF_LENGTH (jcf, name_index);
896 if (str[0] == '<')
898 /* Ignore the internally generated method <clinit>. However,
899 treat <init> as a constructor. */
900 if (! utf8_cmp (str, length, "<init>"))
901 is_init = 1;
902 else if (! METHOD_IS_FINAL (jcf->access_flags, flags)
903 && ! (flags & ACC_STATIC))
905 /* FIXME: i18n bug here. Order of prints should not be
906 fixed. */
907 fprintf (stderr, _("ignored method '"));
908 jcf_print_utf8 (stderr, str, length);
909 fprintf (stderr, _("' marked virtual\n"));
910 found_error = 1;
911 return;
913 else
914 return;
917 /* During the first method pass, build a list of method names. This will
918 be used to determine if field names conflict with method names. */
919 if (! stream)
921 struct method_name *nn;
923 nn = xmalloc (sizeof (struct method_name));
924 nn->name = xmalloc (length);
925 memcpy (nn->name, str, length);
926 nn->length = length;
927 nn->next = method_name_list;
928 nn->sig_length = JPOOL_UTF_LENGTH (jcf, sig_index);
929 nn->signature = xmalloc (nn->sig_length);
930 nn->is_native = METHOD_IS_NATIVE (flags);
931 memcpy (nn->signature, JPOOL_UTF_DATA (jcf, sig_index),
932 nn->sig_length);
933 method_name_list = nn;
935 /* The rest of this function doesn't matter. */
936 return;
939 /* We don't worry about overrides in JNI mode. */
940 if (! flag_jni)
942 /* We can't generate a method whose name is a C++ reserved word.
943 We can't just ignore the function, because that will cause
944 incorrect code to be generated if the function is virtual
945 (not only for calls to this function for for other functions
946 after it in the vtbl). So we give it a dummy name instead. */
947 override = cxx_keyword_subst (str, length);
950 if (! stubs && ! flag_jni)
952 method_printed = 1;
954 generate_access (stream, flags);
956 fputs (" ", out);
957 if ((flags & ACC_STATIC))
958 fputs ("static ", out);
959 else if (! METHOD_IS_PRIVATE (jcf->access_flags, flags))
961 /* Don't print `virtual' if we have a constructor. */
962 if (! is_init)
963 fputs ("virtual ", out);
965 print_c_decl (out, jcf, name_index, sig_index, is_init, override, flags);
967 if ((flags & ACC_ABSTRACT))
968 fputs (" = 0", out);
969 else
970 method_declared = 1;
972 else
974 if (METHOD_IS_NATIVE (flags))
976 method_printed = 1;
977 print_stub_or_jni (out, jcf, name_index, sig_index,
978 is_init, override, flags);
982 if (override)
983 free (override);
986 /* A helper for the decompiler which prints a `return' statement where
987 the type is a reference type. If METHODTYPE and OBJECTTYPE are not
988 identical, we emit a cast. We do this because the C++ compiler
989 doesn't know that a reference can be cast to the type of an
990 interface it implements. METHODTYPE is the index of the method's
991 signature. NAMEINDEX is the index of the field name; -1 for
992 `this'. OBJECTTYPE is the index of the object's type. */
993 static void
994 decompile_return_statement (FILE *out, JCF *jcf, int methodtype,
995 int nameindex, int objecttype)
997 int cast = 0;
998 int obj_name_len, method_name_len;
999 const unsigned char *obj_data, *method_data;
1001 obj_name_len = JPOOL_UTF_LENGTH (jcf, objecttype);
1002 obj_data = JPOOL_UTF_DATA (jcf, objecttype);
1004 method_name_len = JPOOL_UTF_LENGTH (jcf, methodtype);
1005 method_data = JPOOL_UTF_DATA (jcf, methodtype);
1007 /* Skip forward to return type part of method. */
1008 while (*method_data != ')')
1010 ++method_data;
1011 --method_name_len;
1013 /* Skip past `)'. */
1014 ++method_data;
1015 --method_name_len;
1017 /* If we see an `L', skip it and the trailing `;'. */
1018 if (method_data[0] == 'L' && method_data[method_name_len - 1] == ';')
1020 ++method_data;
1021 method_name_len -= 2;
1023 if (obj_data[0] == 'L' && obj_data[obj_name_len - 1] == ';')
1025 ++obj_data;
1026 obj_name_len -= 2;
1029 /* FIXME: if METHODTYPE is a superclass of OBJECTTYPE then we don't
1030 need a cast. Right now there is no way to determine if this is
1031 the case. */
1032 if (method_name_len != obj_name_len)
1033 cast = 1;
1034 else
1036 int i;
1037 for (i = 0; i < method_name_len; ++i)
1039 if (method_data[i] != obj_data[i])
1041 cast = 1;
1042 break;
1047 fputs (" { return ", out);
1049 if (cast)
1051 int array_depth = 0;
1052 const unsigned char *limit;
1054 fputs ("reinterpret_cast<", out);
1056 while (*method_data == '[')
1058 ++method_data;
1059 ++array_depth;
1060 --method_name_len;
1061 fputs ("JArray<", out);
1064 /* Leading space to avoid C++ digraphs. */
1065 fputs (" ::", out);
1067 /* If we see an `L', skip it and the trailing `;'. Only do this
1068 if we've seen an array specification. If we don't have an
1069 array then the `L' was stripped earlier. */
1070 if (array_depth && method_data[0] == 'L'
1071 && method_data[method_name_len - 1] == ';')
1073 ++method_data;
1074 method_name_len -= 2;
1077 limit = method_data + method_name_len;
1078 while (method_data < limit)
1080 int ch = UTF8_GET (method_data, limit);
1081 if (ch == '/')
1082 fputs ("::", out);
1083 else
1084 jcf_print_char (out, ch);
1086 fputs (" *", out);
1088 /* Close each array. */
1089 while (array_depth > 0)
1091 fputs ("> *", out);
1092 --array_depth;
1095 /* Close the cast. */
1096 fputs ("> (", out);
1099 if (nameindex == -1)
1100 fputs ("this", out);
1101 else
1102 print_field_name (out, jcf, nameindex, 0);
1104 if (cast)
1105 fputs (")", out);
1107 fputs ("; }", out);
1111 /* Try to decompile a method body. Right now we just try to handle a
1112 simple case that we can do. Expand as desired. */
1113 static void
1114 decompile_method (FILE *out, JCF *jcf, int code_len)
1116 const unsigned char *codes = jcf->read_ptr;
1117 int index;
1118 uint16 name_and_type, name;
1120 /* If the method is synchronized, don't touch it. */
1121 if ((method_access & ACC_SYNCHRONIZED))
1122 return;
1124 if (code_len == 5
1125 && codes[0] == OPCODE_aload_0
1126 && codes[1] == OPCODE_getfield
1127 && (codes[4] == OPCODE_areturn
1128 || codes[4] == OPCODE_dreturn
1129 || codes[4] == OPCODE_freturn
1130 || codes[4] == OPCODE_ireturn
1131 || codes[4] == OPCODE_lreturn))
1133 /* Found code like `return FIELD'. */
1134 index = (codes[2] << 8) | codes[3];
1135 /* FIXME: ensure that tag is CONSTANT_Fieldref. */
1136 name_and_type = JPOOL_USHORT2 (jcf, index);
1137 /* FIXME: ensure that tag is CONSTANT_NameAndType. */
1138 name = JPOOL_USHORT1 (jcf, name_and_type);
1139 if (codes[4] == OPCODE_areturn)
1140 decompile_return_statement (out, jcf, method_signature,
1141 name, JPOOL_USHORT2 (jcf, name_and_type));
1142 else
1144 fputs (" { return ", out);
1145 /* FIXME: flags. */
1146 print_field_name (out, jcf, name, 0);
1147 fputs ("; }", out);
1149 decompiled = 1;
1151 else if (code_len == 2
1152 && codes[0] == OPCODE_aload_0
1153 && codes[1] == OPCODE_areturn
1154 /* We're going to generate `return this'. This only makes
1155 sense for non-static methods. */
1156 && ! (method_access & ACC_STATIC))
1158 decompile_return_statement (out, jcf, method_signature, -1,
1159 JPOOL_USHORT1 (jcf, jcf->this_class));
1160 decompiled = 1;
1162 else if (code_len == 1 && codes[0] == OPCODE_return)
1164 /* Found plain `return'. */
1165 fputs (" { }", out);
1166 decompiled = 1;
1168 else if (code_len == 2
1169 && codes[0] == OPCODE_aconst_null
1170 && codes[1] == OPCODE_areturn)
1172 /* Found `return null'. We don't want to depend on NULL being
1173 defined. */
1174 fputs (" { return 0; }", out);
1175 decompiled = 1;
1179 /* Like strcmp, but invert the return result for the hash table. This
1180 should probably be in hashtab.c to complement the existing string
1181 hash function. */
1182 static int
1183 gcjh_streq (const void *p1, const void *p2)
1185 return ! strcmp ((char *) p1, (char *) p2);
1188 /* Return 1 if the initial part of CLNAME names a subclass of throwable,
1189 or 0 if not. CLNAME may be extracted from a signature, and can be
1190 terminated with either `;' or NULL. */
1191 static int
1192 throwable_p (const unsigned char *clname)
1194 int length;
1195 unsigned char *current;
1196 int i;
1197 int result = 0;
1199 /* We keep two hash tables of class names. In one we list all the
1200 classes which are subclasses of Throwable. In the other we will
1201 all other classes. We keep two tables to make the code a bit
1202 simpler; we don't have to have a structure mapping class name to
1203 a `throwable?' bit. */
1204 static htab_t throw_hash;
1205 static htab_t non_throw_hash;
1206 static int init_done = 0;
1208 if (! init_done)
1210 void **slot;
1211 unsigned char *str;
1213 /* Self-initializing. The cost of this really doesn't matter.
1214 We also don't care about freeing these, either. */
1215 throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
1216 (htab_del) free);
1217 non_throw_hash = htab_create (10, htab_hash_string, gcjh_streq,
1218 (htab_del) free);
1220 /* Make sure the root classes show up in the tables. */
1221 str = (unsigned char *) xstrdup ("java.lang.Throwable");
1222 slot = htab_find_slot (throw_hash, str, INSERT);
1223 *slot = str;
1225 str = (unsigned char *) xstrdup ("java.lang.Object");
1226 slot = htab_find_slot (non_throw_hash, str, INSERT);
1227 *slot = str;
1229 init_done = 1;
1232 for (length = 0; clname[length] != ';' && clname[length] != '\0'; ++length)
1234 current = ALLOC (length + 1);
1235 for (i = 0; i < length; ++i)
1236 current[i] = clname[i] == '/' ? '.' : clname[i];
1237 current[length] = '\0';
1239 /* We don't compute the hash slot here because the table might be
1240 modified by the recursion. In that case the slot could be
1241 invalidated. */
1242 if (htab_find (throw_hash, current))
1243 result = 1;
1244 else if (htab_find (non_throw_hash, current))
1245 result = 0;
1246 else
1248 JCF jcf;
1249 void **slot;
1250 unsigned char *super, *tmp;
1251 int super_length = -1;
1252 const char *classfile_name = find_class ((char *) current, strlen ((const char *) current),
1253 &jcf, 0);
1255 if (! classfile_name)
1257 error ("couldn't find class %s", current);
1258 return 0;
1260 if (jcf_parse_preamble (&jcf) != 0
1261 || jcf_parse_constant_pool (&jcf) != 0
1262 || verify_constant_pool (&jcf) > 0)
1264 error ("parse error while reading %s", classfile_name);
1265 return 0;
1267 jcf_parse_class (&jcf);
1269 tmp = (unsigned char *) super_class_name (&jcf, &super_length);
1270 super = ALLOC (super_length + 1);
1271 memcpy (super, tmp, super_length);
1272 super[super_length] = '\0';
1274 result = throwable_p (super);
1275 slot = htab_find_slot (result ? throw_hash : non_throw_hash,
1276 current, INSERT);
1277 *slot = current;
1278 current = NULL;
1280 JCF_FINISH (&jcf);
1283 return result;
1286 /* Print one piece of a signature. Returns pointer to next parseable
1287 character on success, NULL on error. */
1288 static const unsigned char *
1289 decode_signature_piece (FILE *stream, const unsigned char *signature,
1290 const unsigned char *limit, int *need_space)
1292 const char *ctype;
1293 int array_depth = 0;
1295 switch (signature[0])
1297 case '[':
1298 /* More spaghetti. */
1300 array_loop:
1301 for (signature++; (signature < limit
1302 && ISDIGIT (*signature)); signature++)
1304 switch (*signature)
1306 case 'B':
1307 ctype = "jbyteArray";
1308 break;
1309 case 'C':
1310 ctype = "jcharArray";
1311 break;
1312 case 'D':
1313 ctype = "jdoubleArray";
1314 break;
1315 case 'F':
1316 ctype = "jfloatArray";
1317 break;
1318 case 'I':
1319 ctype = "jintArray";
1320 break;
1321 case 'S':
1322 ctype = "jshortArray";
1323 break;
1324 case 'J':
1325 ctype = "jlongArray";
1326 break;
1327 case 'Z':
1328 ctype = "jbooleanArray";
1329 break;
1330 case '[':
1331 /* We have a nested array. */
1332 ++array_depth;
1333 if (! flag_jni)
1334 fputs ("JArray<", stream);
1335 goto array_loop;
1337 case 'L':
1338 /* We have to generate a reference to JArray here, so that
1339 our output matches what the compiler does. */
1340 ++signature;
1341 /* Space between `<' and `:' to avoid C++ digraphs. */
1342 if (! flag_jni)
1343 fputs ("JArray< ::", stream);
1344 while (signature < limit && *signature != ';')
1346 int ch = UTF8_GET (signature, limit);
1347 if (! flag_jni)
1349 if (ch == '/')
1350 fputs ("::", stream);
1351 else
1352 jcf_print_char (stream, ch);
1355 if (! flag_jni)
1356 fputs (" *> *", stream);
1357 *need_space = 0;
1358 ctype = NULL;
1359 break;
1360 default:
1361 /* Unparseable signature. */
1362 return NULL;
1365 /* If the previous iterations left us with something to print,
1366 print it. For JNI, we always print `jobjectArray' in the
1367 nested cases. */
1368 if (flag_jni && (ctype == NULL || array_depth > 0))
1370 ctype = "jobjectArray";
1371 *need_space = 1;
1373 /* The `printit' case will advance SIGNATURE for us. If we
1374 don't go there, we must advance past the `;' ourselves. */
1375 if (ctype != NULL)
1376 goto printit;
1377 ++signature;
1378 break;
1380 case '(':
1381 case ')':
1382 /* This shouldn't happen. */
1383 return NULL;
1385 case 'B': ctype = "jbyte"; goto printit;
1386 case 'C': ctype = "jchar"; goto printit;
1387 case 'D': ctype = "jdouble"; goto printit;
1388 case 'F': ctype = "jfloat"; goto printit;
1389 case 'I': ctype = "jint"; goto printit;
1390 case 'J': ctype = "jlong"; goto printit;
1391 case 'S': ctype = "jshort"; goto printit;
1392 case 'Z': ctype = "jboolean"; goto printit;
1393 case 'V': ctype = "void"; goto printit;
1394 case 'L':
1395 if (flag_jni)
1397 /* We know about certain types and special-case their names. */
1398 if (! strncmp ((const char *) signature, "Ljava/lang/String;",
1399 sizeof ("Ljava/lang/String;") -1))
1400 ctype = "jstring";
1401 else if (! strncmp ((const char *) signature, "Ljava/lang/Class;",
1402 sizeof ("Ljava/lang/Class;") - 1))
1403 ctype = "jclass";
1404 /* Skip leading 'L' for throwable_p call. */
1405 else if (throwable_p (signature + 1))
1406 ctype = "jthrowable";
1407 else
1408 ctype = "jobject";
1410 while (*signature && *signature != ';')
1411 ++signature;
1413 goto printit;
1415 /* Print a leading "::" so we look in the right namespace. */
1416 fputs ("::", stream);
1417 ++signature;
1418 while (*signature && *signature != ';')
1420 int ch = UTF8_GET (signature, limit);
1421 if (ch == '/')
1422 fputs ("::", stream);
1423 else
1424 jcf_print_char (stream, ch);
1426 fputs (" *", stream);
1427 if (*signature == ';')
1428 signature++;
1429 *need_space = 0;
1430 break;
1431 default:
1432 *need_space = 1;
1433 jni_print_char (stream, *signature++);
1434 break;
1435 printit:
1436 signature++;
1437 *need_space = 1;
1438 fputs (ctype, stream);
1439 break;
1442 if (! flag_jni)
1444 while (array_depth-- > 0)
1445 fputs ("> *", stream);
1448 return signature;
1451 static void
1452 print_c_decl (FILE* stream, JCF* jcf, int name_index, int signature_index,
1453 int is_init, const char *name_override, int flags)
1455 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1457 fprintf (stream, "<not a UTF8 constant>");
1458 found_error = 1;
1460 else
1462 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1463 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1464 const unsigned char *str = str0;
1465 const unsigned char *limit = str + length;
1466 int need_space = 0;
1467 int is_method = str[0] == '(';
1468 const unsigned char *next;
1470 /* If printing a method, skip to the return signature and print
1471 that first. However, there is no return value if this is a
1472 constructor. */
1473 if (is_method && ! is_init)
1475 while (str < limit)
1477 int ch = *str++;
1478 if (ch == ')')
1479 break;
1483 /* If printing a field or an ordinary method, then print the
1484 "return value" now. */
1485 if (! is_method || ! is_init)
1487 next = decode_signature_piece (stream, str, limit, &need_space);
1488 if (! next)
1490 error ("unparseable signature: '%s'", str0);
1491 return;
1495 /* Force the alignment of the first data member. This is
1496 because the "new" C++ ABI changed the alignment of non-POD
1497 classes. gcj, however, still uses the "old" alignment. */
1498 if (is_first_data_member && ! (flags & ACC_STATIC) && ! is_method)
1500 is_first_data_member = 0;
1501 print_cxx_classname (out, " __attribute__((aligned(__alignof__( ",
1502 jcf, jcf->super_class, 1);
1503 fputs (" )))) ", stream);
1506 /* Now print the name of the thing. */
1507 if (need_space)
1508 fputs (" ", stream);
1509 print_full_cxx_name (stream, jcf, name_index,
1510 signature_index, is_init, name_override,
1511 flags);
1515 /* Print the unqualified method name followed by the signature. */
1516 static void
1517 print_full_cxx_name (FILE* stream, JCF* jcf, int name_index,
1518 int signature_index, int is_init,
1519 const char *name_override, int flags)
1521 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1522 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1523 const unsigned char *str = str0;
1524 const unsigned char *limit = str + length;
1525 int need_space = 0;
1526 int is_method = str[0] == '(';
1527 const unsigned char *next;
1529 if (name_override)
1530 fputs (name_override, stream);
1531 else if (name_index)
1533 /* Declare constructors specially. */
1534 if (is_init)
1535 print_base_classname (stream, jcf, jcf->this_class);
1536 else
1537 print_name (stream, jcf, name_index);
1540 if (flag_jni)
1542 unsigned char *signature = JPOOL_UTF_DATA (jcf, signature_index);
1543 int sig_len = JPOOL_UTF_LENGTH (jcf, signature_index);
1544 if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf, name_index),
1545 JPOOL_UTF_LENGTH (jcf, name_index),
1546 (const char *) signature, sig_len))
1548 /* If this method is overloaded by another native method,
1549 then include the argument information in the mangled
1550 name. */
1551 unsigned char *limit = signature + sig_len;
1552 fputs ("__", stream);
1553 while (signature < limit)
1555 int ch = UTF8_GET (signature, limit);
1556 jni_print_char (stream, ch);
1557 if (ch == ')')
1559 /* Done. */
1560 break;
1566 if (is_method)
1568 /* Have a method or a constructor. Print signature pieces
1569 until done. */
1570 fputs (" (", stream);
1572 str = str0 + 1;
1574 /* In JNI mode, add extra arguments. */
1575 if (flag_jni)
1577 /* FIXME: it would be nice to know if we are printing a decl
1578 or a definition, and only print `env' for the latter. */
1579 fputs ("JNIEnv *env", stream);
1581 fputs ((flags & ACC_STATIC) ? ", jclass" : ", jobject", stream);
1583 if (*str != ')')
1584 fputs (", ", stream);
1587 while (str < limit && *str != ')')
1589 next = decode_signature_piece (stream, str, limit, &need_space);
1590 if (! next)
1592 error ("unparseable signature: '%s'", str0);
1593 return;
1596 if (next < limit && *next != ')')
1597 fputs (", ", stream);
1598 str = next;
1601 fputs (")", stream);
1605 /* This is a helper for print_stub_or_jni. */
1606 static void
1607 print_name_for_stub_or_jni (FILE *stream, JCF *jcf, int name_index,
1608 int signature_index, int is_init,
1609 const char *name_override, int flags)
1611 const char *const prefix = flag_jni ? "Java_" : "";
1612 print_cxx_classname (stream, prefix, jcf, jcf->this_class, 1);
1613 fputs (flag_jni ? "_" : "::", stream);
1614 print_full_cxx_name (stream, jcf, name_index,
1615 signature_index, is_init, name_override,
1616 flags);
1619 static void
1620 print_stub_or_jni (FILE* stream, JCF* jcf, int name_index,
1621 int signature_index, int is_init,
1622 const char *name_override, int flags)
1624 if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1626 fprintf (stream, "<not a UTF8 constant>");
1627 found_error = 1;
1629 else
1631 int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1632 const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1633 const unsigned char *str = str0;
1634 const unsigned char *limit = str + length;
1635 int need_space = 0;
1636 int is_method = str[0] == '(';
1637 const unsigned char *next;
1639 /* Don't print fields in the JNI case. */
1640 if (! is_method && flag_jni)
1641 return;
1643 if (flag_jni && ! stubs)
1644 fputs ("JNIEXPORT ", stream);
1646 /* If printing a method, skip to the return signature and print
1647 that first. However, there is no return value if this is a
1648 constructor. */
1649 if (is_method && ! is_init)
1651 while (str < limit)
1653 int ch = *str++;
1654 if (ch == ')')
1655 break;
1659 /* If printing a field or an ordinary method, then print the
1660 "return value" now. Note that a constructor can't be native,
1661 so we don't bother checking this in the JNI case. */
1662 if (! is_method || ! is_init)
1664 next = decode_signature_piece (stream, str, limit, &need_space);
1665 if (! next)
1667 error ("unparseable signature: '%s'", str0);
1668 return;
1672 /* When printing a JNI header we need to respect the space. In
1673 other cases we're just going to insert a newline anyway. */
1674 fputs (need_space && ! stubs ? " " : "\n", stream);
1676 if (flag_jni && ! stubs)
1677 fputs ("JNICALL ", stream);
1679 /* Now print the name of the thing. */
1680 print_name_for_stub_or_jni (stream, jcf, name_index,
1681 signature_index, is_init, name_override,
1682 flags);
1684 /* Print the body. */
1685 if (stubs)
1687 if (flag_jni)
1688 fputs ("\n{\n (*env)->FatalError (env, \"", stream);
1689 else
1690 fputs ("\n{\n throw new ::java::lang::UnsupportedOperationException (JvNewStringLatin1 (\"", stream);
1691 print_name_for_stub_or_jni (stream, jcf, name_index,
1692 signature_index, is_init,
1693 name_override,
1694 flags);
1695 fprintf (stream, " not implemented\")%s;\n}\n\n",
1696 flag_jni ? "" : ")");
1701 static void
1702 print_mangled_classname (FILE *stream, JCF *jcf, const char *prefix, int index)
1704 int name_index = JPOOL_USHORT1 (jcf, index);
1705 fputs (prefix, stream);
1706 jcf_print_utf8_replace (out,
1707 JPOOL_UTF_DATA (jcf, name_index),
1708 JPOOL_UTF_LENGTH (jcf, name_index),
1709 '/', '_');
1712 /* Print PREFIX, then a class name in C++ format. If the name refers
1713 to an array, ignore it and don't print PREFIX. Returns 1 if
1714 something was printed, 0 otherwise. */
1715 static int
1716 print_cxx_classname (FILE *stream, const char *prefix,
1717 JCF *jcf, int index, int add_scope)
1719 int name_index = JPOOL_USHORT1 (jcf, index);
1720 int len, c;
1721 const unsigned char *s, *p, *limit;
1723 s = JPOOL_UTF_DATA (jcf, name_index);
1724 len = JPOOL_UTF_LENGTH (jcf, name_index);
1725 limit = s + len;
1727 /* Explicitly omit arrays here. */
1728 p = s;
1729 c = UTF8_GET (p, limit);
1730 if (c == '[')
1731 return 0;
1733 fputs (prefix, stream);
1735 /* Print a leading "::" so we look in the right namespace. */
1736 if (! flag_jni && ! stubs && add_scope)
1737 fputs ("::", stream);
1739 while (s < limit)
1741 c = UTF8_GET (s, limit);
1742 if (c == '/')
1743 fputs (flag_jni ? "_" : "::", stream);
1744 else
1745 jni_print_char (stream, c);
1748 return 1;
1751 int written_class_count = 0;
1753 /* Return name of superclass. If LEN is not NULL, fill it with length
1754 of name. */
1755 static const unsigned char *
1756 super_class_name (JCF *derived_jcf, int *len)
1758 int supername_index = JPOOL_USHORT1 (derived_jcf, derived_jcf->super_class);
1759 int supername_length = JPOOL_UTF_LENGTH (derived_jcf, supername_index);
1760 const unsigned char *supername =
1761 JPOOL_UTF_DATA (derived_jcf, supername_index);
1763 if (len)
1764 *len = supername_length;
1766 return supername;
1769 static void
1770 handle_inner_classes (int count)
1772 int i;
1774 if (out && ! flag_jni && ! stubs && count > 0)
1775 fprintf (out, "\n");
1777 for (i = 0; i < count; ++i)
1779 JCF_u2 inner_info_index = JCF_readu2 (current_jcf);
1781 /* There are a few more values here, but we don't care about
1782 them. The (void) cast is apparently the only way to avoid a
1783 warning here. */
1784 (void) JCF_readu2 (current_jcf);
1785 (void) JCF_readu2 (current_jcf);
1786 (void) JCF_readu2 (current_jcf);
1788 if (out && ! flag_jni && ! stubs)
1790 print_mangled_classname (out, current_jcf, " friend class ",
1791 inner_info_index);
1792 fprintf (out, ";\n");
1799 /* We keep track of all the `#include's we generate, so we can avoid
1800 duplicates. */
1801 struct include
1803 char *name;
1804 struct include *next;
1807 /* List of all includes. */
1808 static struct include *all_includes = NULL;
1810 /* Generate a #include. */
1811 static void
1812 print_include (FILE *out, const unsigned char *utf8, int len)
1814 struct include *incl;
1816 if (! out)
1817 return;
1819 if (len == -1)
1820 len = strlen ((const char *) utf8);
1822 for (incl = all_includes; incl; incl = incl->next)
1824 /* We check the length because we might have a proper prefix. */
1825 if (len == (int) strlen (incl->name)
1826 && ! strncmp (incl->name, (const char *) utf8, len))
1827 return;
1830 incl = xmalloc (sizeof (struct include));
1831 incl->name = xmalloc (len + 1);
1832 strncpy (incl->name, (const char *) utf8, len);
1833 incl->name[len] = '\0';
1834 incl->next = all_includes;
1835 all_includes = incl;
1837 fputs ("#include <", out);
1838 jcf_print_utf8_replace (out, utf8, len,
1839 '/',
1840 flag_jni ? '_' : '/');
1841 fputs (".h>\n", out);
1846 /* This is used to represent part of a package or class name. */
1847 struct namelet
1849 /* The text of this part of the name. */
1850 char *name;
1851 /* True if this represents a class. */
1852 int is_class;
1853 /* Linked list of all classes and packages inside this one. */
1854 struct namelet *subnamelets;
1855 /* Pointer to next sibling. */
1856 struct namelet *next;
1859 static void add_namelet (const unsigned char *, const unsigned char *,
1860 struct namelet *);
1861 static void print_namelet (FILE *, struct namelet *, int);
1863 /* The special root namelet. */
1864 static struct namelet root =
1866 NULL,
1868 NULL,
1869 NULL
1872 /* This extracts the next name segment from the full UTF-8 encoded
1873 package or class name and links it into the tree. It does this
1874 recursively. */
1875 static void
1876 add_namelet (const unsigned char *name, const unsigned char *name_limit,
1877 struct namelet *parent)
1879 const unsigned char *p;
1880 struct namelet *n = NULL, *np;
1882 /* We want to skip the standard namespaces that we assume the
1883 runtime already knows about. We only do this at the top level,
1884 though, hence the check for `root'. */
1885 if (parent == &root)
1887 #define JAVALANG "java/lang/"
1888 #define JAVAIO "java/io/"
1889 #define JAVAUTIL "java/util/"
1890 if ((name_limit - name >= (int) sizeof (JAVALANG) - 1
1891 && ! strncmp ((const char *) name, JAVALANG, sizeof (JAVALANG) - 1))
1892 || (name_limit - name >= (int) sizeof (JAVAUTIL) - 1
1893 && ! strncmp ((const char *) name, JAVAUTIL, sizeof (JAVAUTIL) - 1))
1894 || (name_limit - name >= (int) sizeof (JAVAIO) - 1
1895 && ! strncmp ((const char *) name, JAVAIO, sizeof (JAVAIO) - 1)))
1896 return;
1899 for (p = name; p < name_limit && *p != '/'; ++p)
1902 /* Search for this name beneath the PARENT node. */
1903 for (np = parent->subnamelets; np != NULL; np = np->next)
1905 /* We check the length because we might have a proper prefix. */
1906 if ((int) strlen (np->name) == p - name &&
1907 ! strncmp ((const char *) name, np->name, p - name))
1909 n = np;
1910 break;
1914 if (n == NULL)
1916 n = xmalloc (sizeof (struct namelet));
1917 n->name = xmalloc (p - name + 1);
1918 strncpy (n->name, (const char *) name, p - name);
1919 n->name[p - name] = '\0';
1920 n->is_class = (p == name_limit);
1921 n->subnamelets = NULL;
1922 n->next = parent->subnamelets;
1923 parent->subnamelets = n;
1926 /* We recurse if there is more text, and if the trailing piece does
1927 not represent an inner class. */
1928 if (p < name_limit)
1929 add_namelet (p + 1, name_limit, n);
1932 /* Print a single namelet. Destroys namelets while printing. */
1933 static void
1934 print_namelet (FILE *out, struct namelet *name, int depth)
1936 int i, term = 0;
1937 struct namelet *c;
1939 if (name->name)
1941 for (i = 0; i < depth; ++i)
1942 fputc (' ', out);
1943 fprintf (out, "%s %s", name->is_class ? "class" : "namespace",
1944 name->name);
1945 if (name->is_class && name->subnamelets == NULL)
1946 fputs (";\n", out);
1947 else
1949 term = 1;
1950 fputs ("\n", out);
1951 for (i = 0; i < depth; ++i)
1952 fputc (' ', out);
1953 fputs ("{\n", out);
1957 c = name->subnamelets;
1958 while (c != NULL)
1960 struct namelet *next = c->next;
1961 print_namelet (out, c, depth + 2);
1962 c = next;
1964 name->subnamelets = NULL;
1966 if (name->name)
1968 if (term)
1970 for (i = 0; i < depth; ++i)
1971 fputc (' ', out);
1972 fputs ("}\n", out);
1973 /* Only print a `;' when printing a class. C++ is evil. */
1974 if (name->is_class)
1975 fputs (";", out);
1978 free (name->name);
1979 free (name);
1983 /* This is called to add some classes to the list of classes for which
1984 we need decls. The signature argument can be a function
1985 signature. */
1986 static void
1987 add_class_decl (FILE *out, JCF *jcf, JCF_u2 signature)
1989 const unsigned char *s = JPOOL_UTF_DATA (jcf, signature);
1990 int len = JPOOL_UTF_LENGTH (jcf, signature);
1991 int i;
1993 for (i = 0; i < len; ++i)
1995 int start;
1997 /* If we see an array, then we include the array header. */
1998 if (s[i] == '[')
2000 print_include (out, (const unsigned char *) "gcj/array", -1);
2001 continue;
2004 /* We're looking for `L<stuff>;' -- everything else is
2005 ignorable. */
2006 if (s[i] != 'L')
2007 continue;
2009 for (start = ++i; i < len && s[i] != ';'; ++i)
2012 add_namelet (&s[start], &s[i], &root);
2016 /* Print declarations for all classes required by this class. Any
2017 class or package in the `java' package is assumed to be handled
2018 statically in libjava; we don't generate declarations for these.
2019 This makes the generated headers a bit easier to read. */
2020 static void
2021 print_class_decls (FILE *out, JCF *jcf, int self)
2023 /* Make sure to always add the current class to the list of things
2024 that should be declared. */
2025 int name_index = JPOOL_USHORT1 (jcf, self);
2026 int len;
2027 const unsigned char *s;
2029 s = JPOOL_UTF_DATA (jcf, name_index);
2030 len = JPOOL_UTF_LENGTH (jcf, name_index);
2031 add_namelet (s, s + len, &root);
2033 if (root.subnamelets)
2035 fputs ("extern \"Java\"\n{\n", out);
2036 /* We use an initial offset of 0 because the root namelet
2037 doesn't cause anything to print. */
2038 print_namelet (out, &root, 0);
2039 fputs ("}\n\n", out);
2045 static void
2046 process_file (JCF *jcf, FILE *out)
2048 int code, i;
2049 uint32 field_start, method_end, method_start;
2051 current_jcf = jcf;
2053 last_access = -1;
2055 if (jcf_parse_preamble (jcf) != 0)
2057 error ("Not a valid Java .class file.");
2058 return;
2061 /* Parse and possibly print constant pool */
2062 code = jcf_parse_constant_pool (jcf);
2063 if (code != 0)
2065 error ("error while parsing constant pool");
2066 return;
2068 code = verify_constant_pool (jcf);
2069 if (code > 0)
2071 error ("error in constant pool entry #%d", code);
2072 return;
2075 jcf_parse_class (jcf);
2077 if (written_class_count++ == 0 && out)
2079 const char *cstart, *cstart2, *mode, *cend, *what, *jflag;
2080 if (flag_jni)
2082 cstart = "/*";
2083 cstart2 = " ";
2084 cend = " */";
2085 mode = "";
2086 what = "JNI";
2087 jflag = " -jni";
2089 else
2091 cstart = "//";
2092 cstart2 = "//";
2093 cend = "";
2094 mode = " -*- c++ -*-";
2095 what = "CNI";
2096 jflag = "";
2099 if (! stubs)
2100 fprintf (out, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
2101 cstart, mode, cend);
2102 else
2104 fprintf (out, "%s This file was created by `" TOOLNAME " -stubs%s'.%s\n\
2105 %s\n\
2106 %s This file is intended to give you a head start on implementing native\n\
2107 %s methods using %s.\n\
2108 %s Be aware: running `" TOOLNAME " -stubs %s' once more for this class may\n\
2109 %s overwrite any edits you have made to this file.%s\n\n",
2110 cstart, jflag, mode,
2111 cstart2,
2112 cstart2,
2113 cstart2,
2114 what,
2115 cstart2,
2116 jflag,
2117 cstart2,
2118 cend);
2122 if (out)
2124 if (! stubs)
2126 print_mangled_classname (out, jcf, "#ifndef __", jcf->this_class);
2127 fprintf (out, "__\n");
2129 print_mangled_classname (out, jcf, "#define __", jcf->this_class);
2130 fprintf (out, "__\n\n");
2132 if (flag_jni)
2134 fprintf (out, "#include <jni.h>\n\n");
2135 fprintf (out, "#ifdef __cplusplus\n");
2136 fprintf (out, "extern \"C\"\n");
2137 fprintf (out, "{\n");
2138 fprintf (out, "#endif\n");
2140 else
2142 /* We do this to ensure that inline methods won't be
2143 `outlined' by g++. This works as long as method and
2144 fields are not added by the user. */
2145 fprintf (out, "#pragma interface\n");
2147 if (jcf->super_class)
2149 int super_length;
2150 const unsigned char *supername =
2151 super_class_name (jcf, &super_length);
2153 fputs ("\n", out);
2154 print_include (out, supername, super_length);
2158 else
2160 /* Strip off the ".class" portion of the name when printing
2161 the include file name. */
2162 char *name;
2163 int i, len = strlen (jcf->classname);
2164 if (len > 6 && ! strcmp (&jcf->classname[len - 6], ".class"))
2165 len -= 6;
2166 /* Turn the class name into a file name. */
2167 name = xmalloc (len + 1);
2168 for (i = 0; i < len; ++i)
2169 name[i] = jcf->classname[i] == '.' ? '/' : jcf->classname[i];
2170 name[i] = '\0';
2171 print_include (out, (const unsigned char *) name, len);
2172 free (name);
2174 if (! flag_jni)
2176 print_include (out, (const unsigned char *) "gcj/cni", -1);
2177 print_include (out, (const unsigned char *) "java/lang/UnsupportedOperationException",
2178 -1);
2183 /* We want to parse the methods first. But we need to find where
2184 they start. So first we skip the fields, then parse the methods.
2185 Then we parse the fields and skip the methods. This is ugly, but
2186 not too bad since we need two full passes to get class decl
2187 information anyway. */
2188 field_pass = 0;
2189 field_start = JCF_TELL (jcf);
2190 jcf_parse_fields (jcf);
2192 method_start = JCF_TELL (jcf);
2193 method_pass = 0;
2194 jcf_parse_methods (jcf);
2196 if (out)
2197 fputs ("\n", out);
2199 if (out && ! flag_jni)
2201 if (! stubs)
2202 print_class_decls (out, jcf, jcf->this_class);
2204 for (i = 0; i < prepend_count; ++i)
2205 fprintf (out, "%s\n", prepend_specs[i]);
2206 if (prepend_count > 0)
2207 fputc ('\n', out);
2209 if (! stubs)
2211 if (! print_cxx_classname (out, "class ", jcf,
2212 jcf->this_class, 0))
2214 error ("class is of array type\n");
2215 return;
2217 if (jcf->super_class)
2219 if (! print_cxx_classname (out, " : public ",
2220 jcf, jcf->super_class, 1))
2222 error ("base class is of array type");
2223 return;
2227 fputs ("\n{\n", out);
2231 /* Now go back for second pass over methods and fields. */
2232 is_first_data_member = 1;
2234 JCF_SEEK (jcf, method_start);
2235 method_pass = 1;
2236 jcf_parse_methods (jcf);
2237 method_end = JCF_TELL (jcf);
2239 field_pass = 1;
2240 JCF_SEEK (jcf, field_start);
2241 jcf_parse_fields (jcf);
2242 JCF_SEEK (jcf, method_end);
2244 jcf_parse_final_attributes (jcf);
2246 if (out && ! stubs)
2248 if (flag_jni)
2250 fprintf (out, "\n#ifdef __cplusplus\n");
2251 fprintf (out, "}\n");
2252 fprintf (out, "#endif\n");
2254 else
2256 /* Generate friend decl if we still must. */
2257 for (i = 0; i < friend_count; ++i)
2258 fprintf (out, " friend %s\n", friend_specs[i]);
2260 /* Generate extra declarations. */
2261 if (add_count > 0)
2262 fputc ('\n', out);
2263 for (i = 0; i < add_count; ++i)
2264 fprintf (out, " %s\n", add_specs[i]);
2266 /* Generate an entry for the class object. */
2267 generate_access (out, ACC_PUBLIC);
2268 fprintf (out, "\n static ::java::lang::Class class$;\n");
2270 fputs ("}", out);
2272 if (jcf->access_flags & ACC_INTERFACE)
2273 fputs (" __attribute__ ((java_interface))", out);
2275 fputs (";\n", out);
2277 if (append_count > 0)
2278 fputc ('\n', out);
2279 for (i = 0; i < append_count; ++i)
2280 fprintf (out, "%s\n", append_specs[i]);
2283 print_mangled_classname (out, jcf,
2284 "\n#endif /* __", jcf->this_class);
2285 fprintf (out, "__ */\n");
2291 /* This is used to mark options with no short value. */
2292 #define LONG_OPT(Num) ((Num) + 128)
2294 #define OPT_classpath LONG_OPT (0)
2295 #define OPT_CLASSPATH OPT_classpath
2296 #define OPT_bootclasspath LONG_OPT (1)
2297 #define OPT_extdirs LONG_OPT (2)
2298 #define OPT_HELP LONG_OPT (3)
2299 #define OPT_TEMP LONG_OPT (4)
2300 #define OPT_VERSION LONG_OPT (5)
2301 #define OPT_PREPEND LONG_OPT (6)
2302 #define OPT_FRIEND LONG_OPT (7)
2303 #define OPT_ADD LONG_OPT (8)
2304 #define OPT_APPEND LONG_OPT (9)
2305 #define OPT_M LONG_OPT (10)
2306 #define OPT_MM LONG_OPT (11)
2307 #define OPT_MG LONG_OPT (12)
2308 #define OPT_MD LONG_OPT (13)
2309 #define OPT_MMD LONG_OPT (14)
2310 #define OPT_FORCE LONG_OPT (15)
2311 #define OPT_OLD LONG_OPT (16)
2312 #define OPT_TRACE LONG_OPT (17)
2314 static const struct option options[] =
2316 { "classpath", required_argument, NULL, OPT_classpath },
2317 { "bootclasspath", required_argument, NULL, OPT_bootclasspath },
2318 { "extdirs", required_argument, NULL, OPT_extdirs },
2319 { "CLASSPATH", required_argument, NULL, OPT_CLASSPATH },
2320 { "help", no_argument, NULL, OPT_HELP },
2321 { "stubs", no_argument, &stubs, 1 },
2322 { "td", required_argument, NULL, OPT_TEMP },
2323 { "verbose", no_argument, NULL, 'v' },
2324 { "version", no_argument, NULL, OPT_VERSION },
2325 { "prepend", required_argument, NULL, OPT_PREPEND },
2326 { "friend", required_argument, NULL, OPT_FRIEND },
2327 { "add", required_argument, NULL, OPT_ADD },
2328 { "append", required_argument, NULL, OPT_APPEND },
2329 { "M", no_argument, NULL, OPT_M },
2330 { "MM", no_argument, NULL, OPT_MM },
2331 { "MG", no_argument, NULL, OPT_MG },
2332 { "MD", no_argument, NULL, OPT_MD },
2333 { "MMD", no_argument, NULL, OPT_MMD },
2334 { "jni", no_argument, &flag_jni, 1 },
2335 { "force", no_argument, NULL, OPT_FORCE },
2336 /* If the output file should be named "ld" then a space is needed
2337 between -o and its argument, ld. */
2338 { "old", no_argument, NULL, OPT_OLD },
2339 { "trace", no_argument, NULL, OPT_TRACE },
2340 { NULL, required_argument, NULL, 'J' },
2341 { NULL, no_argument, NULL, 0 }
2344 static void
2345 usage (void)
2347 fprintf (stderr, _("Try '" TOOLNAME " --help' for more information.\n"));
2348 exit (1);
2351 static void
2352 help (void)
2354 printf (_("Usage: " TOOLNAME " [OPTION]... CLASS...\n\n"));
2355 printf (_("Generate C or C++ header files from .class files\n\n"));
2356 printf (_(" -stubs Generate an implementation stub file\n"));
2357 printf (_(" -jni Generate a JNI header or stub\n"));
2358 printf (_(" -force Always overwrite output files\n"));
2359 printf (_(" -old Unused compatibility option\n"));
2360 printf (_(" -trace Unused compatibility option\n"));
2361 printf (_(" -J OPTION Unused compatibility option\n"));
2362 printf ("\n");
2363 printf (_(" -add TEXT Insert TEXT into class body\n"));
2364 printf (_(" -append TEXT Insert TEXT after class declaration\n"));
2365 printf (_(" -friend TEXT Insert TEXT as 'friend' declaration\n"));
2366 printf (_(" -prepend TEXT Insert TEXT before start of class\n"));
2367 printf ("\n");
2368 printf (_(" --classpath PATH Set path to find .class files\n"));
2369 printf (_(" -IDIR Append directory to class path\n"));
2370 printf (_(" --bootclasspath PATH Override built-in class path\n"));
2371 printf (_(" --extdirs PATH Set extensions directory path\n"));
2372 printf (_(" -d DIRECTORY Set output directory name\n"));
2373 printf (_(" -o FILE Set output file name\n"));
2374 printf (_(" -td DIRECTORY Set temporary directory name\n"));
2375 printf ("\n");
2376 printf (_(" --help Print this help, then exit\n"));
2377 printf (_(" --version Print version number, then exit\n"));
2378 printf (_(" -v, --verbose Print extra information while running\n"));
2379 printf ("\n");
2380 printf (_(" -M Print all dependencies to stdout;\n"
2381 " suppress ordinary output\n"));
2382 printf (_(" -MM Print non-system dependencies to stdout;\n"
2383 " suppress ordinary output\n"));
2384 printf (_(" -MD Print all dependencies to stdout\n"));
2385 printf (_(" -MMD Print non-system dependencies to stdout\n"));
2386 /* We omit -MG until it is implemented. */
2387 printf ("\n");
2388 printf (_("For bug reporting instructions, please see:\n"
2389 "%s.\n"), bug_report_url);
2390 exit (0);
2393 static void
2394 version (void)
2396 printf (TOOLNAME " (GCC) %s\n\n", version_string);
2397 printf ("Copyright %s 2004 Free Software Foundation, Inc.\n", _("(C)"));
2398 printf (_("This is free software; see the source for copying conditions. There is NO\n"
2399 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
2400 exit (0);
2404 main (int argc, char** argv)
2406 JCF jcf;
2407 int argi;
2408 char *output_file = NULL;
2409 int emit_dependencies = 0, suppress_output = 0;
2410 int opt;
2411 int local_found_error;
2413 /* Unlock the stdio streams. */
2414 unlock_std_streams ();
2416 gcc_init_libintl ();
2418 if (argc <= 1)
2420 error ("no classes specified");
2421 usage ();
2424 jcf_path_init ();
2426 /* We use getopt_long_only to allow single `-' long options. For
2427 some of our options this is more natural. */
2428 while ((opt = getopt_long_only (argc, argv, "J:I:d:o:v", options, NULL)) != -1)
2430 switch (opt)
2432 case 0:
2433 /* Already handled. */
2434 break;
2436 case 'o':
2437 output_file = optarg;
2438 break;
2440 case 'd':
2441 output_directory = optarg;
2442 break;
2444 case 'I':
2445 jcf_path_include_arg (optarg);
2446 break;
2448 case 'v':
2449 verbose++;
2450 break;
2452 case OPT_classpath:
2453 jcf_path_classpath_arg (optarg);
2454 break;
2456 case OPT_bootclasspath:
2457 jcf_path_bootclasspath_arg (optarg);
2458 break;
2460 case OPT_extdirs:
2461 jcf_path_extdirs_arg (optarg);
2462 break;
2464 case OPT_HELP:
2465 help ();
2466 break;
2468 case OPT_TEMP:
2469 temp_directory = optarg;
2470 break;
2472 case OPT_VERSION:
2473 version ();
2474 break;
2476 case OPT_PREPEND:
2477 if (prepend_count == 0)
2478 prepend_specs = ALLOC (argc * sizeof (char*));
2479 prepend_specs[prepend_count++] = optarg;
2480 break;
2482 case OPT_FRIEND:
2483 if (friend_count == 0)
2484 friend_specs = ALLOC (argc * sizeof (char*));
2485 friend_specs[friend_count++] = optarg;
2486 break;
2488 case OPT_ADD:
2489 if (add_count == 0)
2490 add_specs = ALLOC (argc * sizeof (char*));
2491 add_specs[add_count++] = optarg;
2492 break;
2494 case OPT_APPEND:
2495 if (append_count == 0)
2496 append_specs = ALLOC (argc * sizeof (char*));
2497 append_specs[append_count++] = optarg;
2498 break;
2500 case OPT_M:
2501 emit_dependencies = 1;
2502 suppress_output = 1;
2503 jcf_dependency_init (1);
2504 break;
2506 case OPT_MM:
2507 emit_dependencies = 1;
2508 suppress_output = 1;
2509 jcf_dependency_init (0);
2510 break;
2512 case OPT_MG:
2513 error ("'-MG' option is unimplemented");
2514 exit (1);
2516 case OPT_MD:
2517 emit_dependencies = 1;
2518 jcf_dependency_init (1);
2519 break;
2521 case OPT_MMD:
2522 emit_dependencies = 1;
2523 jcf_dependency_init (0);
2524 break;
2526 case OPT_FORCE:
2527 break;
2529 case OPT_OLD:
2530 break;
2532 case OPT_TRACE:
2533 break;
2535 case 'J':
2536 /* Ignore -J options. */
2537 break;
2539 default:
2540 usage ();
2541 break;
2545 if (optind == argc)
2547 error ("no classes specified");
2548 usage ();
2551 jcf_path_seal (verbose);
2553 if (output_file && emit_dependencies)
2555 error ("can't specify both -o and -MD");
2556 exit (1);
2559 local_found_error = 0;
2560 for (argi = optind; argi < argc; argi++)
2562 char *classname = argv[argi];
2563 char *current_output_file = NULL;
2564 const char *classfile_name;
2566 /* We reset the error state here so that we can detect errors
2567 that occur when processing this file, so the output can be
2568 unlinked if need be. */
2569 found_error = 0;
2571 if (verbose)
2572 printf (_("Processing %s\n"), classname);
2573 if (! output_file)
2574 jcf_dependency_reset ();
2575 classfile_name = find_class (classname, strlen (classname), &jcf, 0);
2576 if (classfile_name == NULL)
2578 error ("%s: no such class", classname);
2579 exit (1);
2581 if (verbose)
2582 printf (_("Found in %s\n"), classfile_name);
2583 if (output_file)
2585 if (strcmp (output_file, "-") == 0)
2586 out = stdout;
2587 else if (out == NULL)
2589 out = fopen (output_file, "w");
2591 if (out == NULL)
2593 perror (output_file);
2594 exit (1);
2596 current_output_file = output_file;
2598 else
2600 int dir_len = strlen (output_directory);
2601 int i, classname_length = strlen (classname);
2602 current_output_file = ALLOC (dir_len + classname_length + 5);
2603 strcpy (current_output_file, output_directory);
2604 if (dir_len > 0 && output_directory[dir_len-1] != '/')
2605 current_output_file[dir_len++] = '/';
2606 for (i = 0; classname[i] != '\0'; i++)
2608 char ch = classname[i];
2609 if (ch == '.')
2610 ch = '/';
2611 if (flag_jni && ch == '/')
2612 ch = '_';
2613 current_output_file[dir_len++] = ch;
2615 if (emit_dependencies)
2617 if (suppress_output)
2619 jcf_dependency_set_dep_file ("-");
2620 out = NULL;
2622 else
2624 /* We use `.hd' and not `.d' to avoid clashes with
2625 dependency tracking from straight compilation. */
2626 strcpy (current_output_file + dir_len, ".hd");
2627 jcf_dependency_set_dep_file (current_output_file);
2630 strcpy (current_output_file + dir_len,
2631 stubs ? (flag_jni ? ".c" : ".cc") : ".h");
2632 jcf_dependency_set_target (current_output_file);
2633 if (! suppress_output)
2635 out = fopen (current_output_file, "w");
2636 if (out == NULL)
2638 perror (current_output_file);
2639 exit (1);
2643 free_method_name_list ();
2644 process_file (&jcf, out);
2645 JCF_FINISH (&jcf);
2647 /* If we found an error and we're writing to a real file,
2648 delete it. */
2649 if (found_error && ! suppress_output && current_output_file != NULL
2650 && strcmp (current_output_file, "-"))
2651 unlink (current_output_file);
2653 if (current_output_file != output_file)
2654 free (current_output_file);
2655 jcf_dependency_write ();
2657 local_found_error |= found_error;
2660 if (out != NULL && out != stdout)
2661 fclose (out);
2663 return local_found_error;