OSDN Git Service

* jcf-parse.c (init_jcf_parse): Register current_file_list root.
[pf3gnuchains/gcc-fork.git] / gcc / java / gjavah.c
1 /* Program to write C++-suitable header files from a Java(TM) .class
2    file.  This is similar to SUN's javah.
3
4 Copyright (C) 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  
20
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
24
25 /* Written by Per Bothner <bothner@cygnus.com>, February 1996. */
26
27 #include "config.h"
28 #include "system.h"
29 #include <math.h>
30
31 #include "jcf.h"
32 #include "tree.h"
33 #include "javaop.h"
34 #include "java-tree.h"
35 #include "java-opcodes.h"
36
37 #include <getopt.h>
38
39 \f
40
41 /* The output file.  */
42 FILE *out = NULL;
43
44 /* Nonzero on failure.  */
45 static int found_error = 0;
46
47 /* Nonzero if we're generating JNI output.  */
48 static int flag_jni = 0;
49
50 /* When non zero, warn when source file is newer than matching class
51    file.  */
52 int flag_newer = 1;
53
54 /* Directory to place resulting files in. Set by -d option. */
55 const char *output_directory = "";
56
57 /* Directory to place temporary file.  Set by -td option.  Currently unused. */
58 const char *temp_directory = "/tmp";
59
60 /* Number of friend functions we have to declare.  */
61 static int friend_count;
62
63 /* A class can optionally have a `friend' function declared.  If
64    non-NULL, this is that function.  */
65 static char **friend_specs = NULL;
66
67 /* Number of lines we are prepending before the class.  */
68 static int prepend_count;
69
70 /* We can prepend extra lines before the class's start. */
71 static char **prepend_specs = NULL;
72
73 /* Number of lines we are appending at the end of the class.  */
74 static int add_count;
75
76 /* We can append extra lines just before the class's end. */
77 static char **add_specs = NULL;
78
79 /* Number of lines we are appending after the class.  */
80 static int append_count;
81
82 /* We can append extra lines after the class's end. */
83 static char **append_specs = NULL;
84
85 int verbose = 0;
86
87 int stubs = 0;
88
89 struct JCF *current_jcf;
90
91 /* This holds access information for the last field we examined.  They
92    let us generate "private:", "public:", and "protected:" properly.
93    If 0 then we haven't previously examined any field.  */
94 static JCF_u2 last_access;
95
96 /* Pass this macro the flags for a class and for a method.  It will
97    return true if the method should be considered `final'.  */
98 #define METHOD_IS_FINAL(Class, Method) \
99    (((Class) & ACC_FINAL) || ((Method) & (ACC_FINAL | ACC_PRIVATE)))
100
101 /* Pass this macro the flags for a method.  It will return true if the
102    method is native.  */
103 #define METHOD_IS_NATIVE(Method) \
104    ((Method) & ACC_NATIVE)
105
106 /* We keep a linked list of all method names we have seen.  This lets
107    us determine if a method name and a field name are in conflict.  */
108 struct method_name
109 {
110   unsigned char *name;
111   int length;
112   unsigned char *signature;
113   int sig_length;
114   struct method_name *next;
115 };
116
117 /* List of method names we've seen.  */
118 static struct method_name *method_name_list;
119
120 static void print_field_info PARAMS ((FILE*, JCF*, int, int, JCF_u2));
121 static void print_mangled_classname PARAMS ((FILE*, JCF*, const char*, int));
122 static int  print_cxx_classname PARAMS ((FILE*, const char*, JCF*, int));
123 static void print_method_info PARAMS ((FILE*, JCF*, int, int, JCF_u2, int));
124 static void print_c_decl PARAMS ((FILE*, JCF*, int, int, int, const char *,
125                                   int));
126 static void print_stub_or_jni PARAMS ((FILE*, JCF*, int, int, int,
127                                        const char *, int));
128 static void print_full_cxx_name PARAMS ((FILE*, JCF*, int, int, int,
129                                          const char *, int));
130 static void decompile_method PARAMS ((FILE*, JCF*, int));
131 static void add_class_decl PARAMS ((FILE*, JCF*, JCF_u2));
132
133 static int java_float_finite PARAMS ((jfloat));
134 static int java_double_finite PARAMS ((jdouble));
135 static void print_name PARAMS ((FILE *, JCF *, int));
136 static void print_base_classname PARAMS ((FILE *, JCF *, int));
137 static int utf8_cmp PARAMS ((const unsigned char *, int, const char *));
138 static char *cxx_keyword_subst PARAMS ((const unsigned char *, int));
139 static void generate_access PARAMS ((FILE *, JCF_u2));
140 static int name_is_method_p PARAMS ((const unsigned char *, int));
141 static char *get_field_name PARAMS ((JCF *, int, JCF_u2));
142 static void print_field_name PARAMS ((FILE *, JCF *, int, JCF_u2));
143 static const unsigned char *super_class_name PARAMS ((JCF *, int *));
144 static void print_include PARAMS ((FILE *, const unsigned char *, int));
145 static const unsigned char *decode_signature_piece
146   PARAMS ((FILE *, const unsigned char *, const unsigned char *, int *));
147 static void print_class_decls PARAMS ((FILE *, JCF *, int));
148 static void usage PARAMS ((void)) ATTRIBUTE_NORETURN;
149 static void help PARAMS ((void)) ATTRIBUTE_NORETURN;
150 static void version PARAMS ((void)) ATTRIBUTE_NORETURN;
151 static int overloaded_jni_method_exists_p PARAMS ((const unsigned char *, int,
152                                                    const char *, int));
153 static void jni_print_char PARAMS ((FILE *, int));
154
155 JCF_u2 current_field_name;
156 JCF_u2 current_field_value;
157 JCF_u2 current_field_signature;
158 JCF_u2 current_field_flags;
159
160 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
161 ( current_field_name = (NAME), current_field_signature = (SIGNATURE), \
162   current_field_flags = (ACCESS_FLAGS), current_field_value = 0)
163
164 /* We pass over fields twice.  The first time we just note the types
165    of the fields and then the start of the methods.  Then we go back
166    and parse the fields for real.  This is ugly.  */
167 static int field_pass;
168 /* Likewise we pass over methods twice.  The first time we generate
169    class decl information; the second time we generate actual method
170    decls.  */
171 static int method_pass;
172
173 #define HANDLE_END_FIELD()                                                    \
174   if (field_pass)                                                             \
175     {                                                                         \
176       if (out && ! stubs && ! flag_jni)                                       \
177         print_field_info (out, jcf, current_field_name,                       \
178                           current_field_signature,                            \
179                           current_field_flags);                               \
180     }                                                                         \
181   else if (! stubs && ! flag_jni)                                             \
182     add_class_decl (out, jcf, current_field_signature);
183
184 #define HANDLE_CONSTANTVALUE(VALUEINDEX) current_field_value = (VALUEINDEX)
185
186 static int method_declared = 0;
187 static int method_access = 0;
188 static int method_printed = 0;
189 static int method_synthetic = 0;
190 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT)   \
191   {                                                                     \
192     method_synthetic = 0;                                               \
193     if (ATTRIBUTE_COUNT)                                                \
194       method_synthetic = peek_attribute (jcf, ATTRIBUTE_COUNT,          \
195                                   (const char *)"Synthetic", 9);        \
196     /* If a synthetic methods have been declared, its attribute aren't  \
197        worth reading (and triggering side-effects). We skip them an     \
198        set ATTRIBUTE_COUNT to zero so that they'll be skipped in        \
199        jcf_parse_one_method.  */                                        \
200     if (method_synthetic)                                               \
201       {                                                                 \
202         skip_attribute (jcf, ATTRIBUTE_COUNT);                          \
203         ATTRIBUTE_COUNT = 0;                                            \
204       }                                                                 \
205     if (method_pass && !method_synthetic)                               \
206       {                                                                 \
207         decompiled = 0; method_printed = 0;                             \
208         if (out)                                                        \
209           print_method_info (out, jcf, NAME, SIGNATURE,                 \
210                              ACCESS_FLAGS, method_synthetic);           \
211       }                                                                 \
212     else if (!method_synthetic)                                         \
213       {                                                                 \
214         print_method_info (NULL, jcf, NAME, SIGNATURE,                  \
215                            ACCESS_FLAGS, method_synthetic);             \
216         if (! stubs && ! flag_jni)                                      \
217           add_class_decl (out, jcf, SIGNATURE);                         \
218       }                                                                 \
219   }
220
221 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH)       \
222   if (out && method_declared) decompile_method (out, jcf, CODE_LENGTH);
223
224 static int decompiled = 0;
225 #define HANDLE_END_METHOD()                             \
226   if (out && method_printed && !method_synthetic)       \
227     fputs (decompiled || stubs ? "\n" : ";\n", out);
228
229 /* We're going to need {peek,skip}_attribute, enable their definition.   */
230 #define NEED_PEEK_ATTRIBUTE
231 #define NEED_SKIP_ATTRIBUTE
232
233 #include "jcf-reader.c"
234
235 /* Some useful constants.  */
236 #define F_NAN_MASK 0x7f800000
237 #if (1 == HOST_FLOAT_WORDS_BIG_ENDIAN) && ! defined (HOST_WORDS_BIG_ENDIAN)
238 #define D_NAN_MASK 0x000000007ff00000LL
239 #else
240 #define D_NAN_MASK 0x7ff0000000000000LL
241 #endif
242
243 /* Return 1 if F is not Inf or NaN.  */
244 static int
245 java_float_finite (f)
246      jfloat f;
247 {
248   union Word u;
249   u.f = f;
250
251   /* We happen to know that F_NAN_MASK will match all NaN values, and
252      also positive and negative infinity.  That's why we only need one
253      test here.  See The Java Language Specification, section 20.9.  */
254   return (u.i & F_NAN_MASK) != F_NAN_MASK;
255 }
256
257 /* Return 1 if D is not Inf or NaN.  */
258 static int
259 java_double_finite (d)
260      jdouble d;
261 {
262   union DWord u;
263   u.d = d;
264
265   /* Now check for all NaNs.  */
266   return (u.l & D_NAN_MASK) != D_NAN_MASK;
267 }
268
269 /* Print a character, appropriately mangled for JNI.  */
270
271 static void
272 jni_print_char (stream, ch)
273      FILE *stream;
274      int ch;
275 {
276   if (! flag_jni)
277     jcf_print_char (stream, ch);
278   else if (ch == '(' || ch == ')')
279     {
280       /* Ignore.  */
281     }
282   else if (ch == '_')
283     fputs ("_1", stream);
284   else if (ch == ';')
285     fputs ("_2", stream);
286   else if (ch == '[')
287     fputs ("_3", stream);
288   else if (ch == '/')
289     fputs ("_", stream);
290   else if ((ch >= '0' && ch <= '9')
291            || (ch >= 'a' && ch <= 'z')
292            || (ch >= 'A' && ch <= 'Z'))
293     fputc (ch, stream);
294   else
295     {
296       /* "Unicode" character.  */
297       fprintf (stream, "_0%04x", ch);
298     }
299 }
300
301 /* Print a name from the class data.  If the index does not point to a
302    string, an error results.  */
303
304 static void
305 DEFUN(print_name, (stream, jcf, name_index),
306       FILE* stream AND JCF* jcf AND int name_index)
307 {
308   if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
309     {
310       fprintf (stream, "<not a UTF8 constant>");
311       found_error = 1;
312     }
313   else if (! flag_jni)
314     jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
315                     JPOOL_UTF_LENGTH (jcf, name_index));
316   else
317     {
318       /* For JNI we must correctly quote each character.  */
319       const unsigned char *str = JPOOL_UTF_DATA (jcf, name_index);
320       int length = JPOOL_UTF_LENGTH (jcf, name_index);
321       const unsigned char *limit = str + length;
322       while (str < limit)
323         {
324           int ch = UTF8_GET (str, limit);
325           if (ch < 0)
326             {
327               fprintf (stream, "\\<invalid>");
328               return;
329             }
330           jni_print_char (stream, ch);
331         }
332     }
333 }
334
335 /* Print base name of class.  The base name is everything after the
336    final separator.  */
337
338 static void
339 print_base_classname (stream, jcf, index)
340      FILE *stream;
341      JCF *jcf;
342      int index;
343 {
344   int name_index = JPOOL_USHORT1 (jcf, index);
345   int len;
346   const unsigned char *s, *p, *limit;
347
348   s = JPOOL_UTF_DATA (jcf, name_index);
349   len = JPOOL_UTF_LENGTH (jcf, name_index);
350   limit = s + len;
351   p = s;
352   while (s < limit)
353     {
354       int c = UTF8_GET (s, limit);
355       if (c == '/')
356         p = s;
357     }
358
359   while (p < limit)
360     {
361       int ch = UTF8_GET (p, limit);
362       if (ch == '/')
363         fputs ("::", stream);
364       else
365         jcf_print_char (stream, ch);
366     }
367 }
368
369 /* Return 0 if NAME is equal to STR, -1 if STR is "less" than NAME,
370    and 1 if STR is "greater" than NAME.  */
371
372 static int
373 utf8_cmp (str, length, name)
374      const unsigned char *str;
375      int length;
376      const char *name;
377 {
378   const unsigned char *limit = str + length;
379   int i;
380
381   for (i = 0; name[i]; ++i)
382     {
383       int ch = UTF8_GET (str, limit);
384       if (ch != name[i])
385         return ch - name[i];
386     }
387
388   return str == limit ? 0 : 1;
389 }
390
391 /* This is a sorted list of all C++ keywords.  */
392
393 static const char *cxx_keywords[] =
394 {
395   "asm",
396   "auto",
397   "bool",
398   "const_cast",
399   "delete",
400   "dynamic_cast",
401   "enum",
402   "explicit",
403   "extern",
404   "friend",
405   "inline",
406   "mutable",
407   "namespace",
408   "overload",
409   "register",
410   "reinterpret_cast",
411   "signed",
412   "sizeof",
413   "static_cast",
414   "struct",
415   "template",
416   "typedef",
417   "typeid",
418   "typename",
419   "typenameopt",
420   "union",
421   "unsigned",
422   "using",
423   "virtual",
424   "volatile",
425   "wchar_t"
426 };
427
428
429 /* If NAME is the name of a C++ keyword, then return an override name.
430    This is a name that can be used in place of the keyword.
431    Otherwise, return NULL.  The return value is malloc()d.  */
432
433 static char *
434 cxx_keyword_subst (str, length)
435      const unsigned char *str;
436      int length;
437 {
438   int last = ARRAY_SIZE (cxx_keywords);
439   int first = 0;
440   int mid = (last + first) / 2;
441   int old = -1;
442
443   for (mid = (last + first) / 2;
444        mid != old;
445        old = mid, mid = (last + first) / 2)
446     {
447       int kwl = strlen (cxx_keywords[mid]);
448       int min_length = kwl > length ? length : kwl;
449       int r = utf8_cmp (str, min_length, cxx_keywords[mid]);
450
451       if (r == 0)
452         {
453           int i;
454
455           /* Skip all trailing `$'.  */
456           for (i = min_length; i < length && str[i] == '$'; ++i)
457             ;
458           /* We've only found a match if all the remaining characters
459              are `$'.  */
460           if (i == length)
461             {
462               char *dup = xmalloc (2 + length - min_length + kwl);
463               strcpy (dup, cxx_keywords[mid]);
464               for (i = kwl; i < length + 1; ++i)
465                 dup[i] = '$';
466               dup[i] = '\0';
467               return dup;
468             }
469           r = 1;
470         }
471         
472       if (r < 0)
473         last = mid;
474       else
475         first = mid;
476     }
477   return NULL;
478 }
479
480 /* Generate an access control keyword based on FLAGS.  */
481
482 static void
483 generate_access (stream, flags)
484      FILE *stream;
485      JCF_u2 flags;
486 {
487   if ((flags & ACC_VISIBILITY) == last_access)
488     return;
489   last_access = (flags & ACC_VISIBILITY);
490
491   switch (last_access)
492     {
493     case 0:
494       fputs ("public: // actually package-private\n", stream);
495       break;
496     case ACC_PUBLIC:
497       fputs ("public:\n", stream);
498       break;
499     case ACC_PRIVATE:
500       fputs ("private:\n", stream);
501       break;
502     case ACC_PROTECTED:
503       fputs ("public:  // actually protected\n", stream);
504       break;
505     default:
506       found_error = 1;
507       fprintf (stream, "#error unrecognized visibility %d\n",
508                (flags & ACC_VISIBILITY));
509       break;
510     }
511 }
512
513 /* See if NAME is already the name of a method.  */
514 static int
515 name_is_method_p (name, length)
516      const unsigned char *name;
517      int length;
518 {
519   struct method_name *p;
520
521   for (p = method_name_list; p != NULL; p = p->next)
522     {
523       if (p->length == length && ! memcmp (p->name, name, length))
524         return 1;
525     }
526   return 0;
527 }
528
529 /* If there is already a method named NAME, whose signature is not
530    SIGNATURE, then return true.  Otherwise return false.  */
531 static int
532 overloaded_jni_method_exists_p (name, length, signature, sig_length)
533      const unsigned char *name;
534      int length;
535      const char *signature;
536      int sig_length;
537 {
538   struct method_name *p;
539
540   for (p = method_name_list; p != NULL; p = p->next)
541     {
542       if (p->length == length
543           && ! memcmp (p->name, name, length)
544           && (p->sig_length != sig_length
545               || memcmp (p->signature, signature, sig_length)))
546         return 1;
547     }
548   return 0;
549 }
550
551 /* Get name of a field.  This handles renamings due to C++ clash.  */
552 static char *
553 get_field_name (jcf, name_index, flags)
554      JCF *jcf;
555      int name_index;
556      JCF_u2 flags;
557 {
558   unsigned char *name = JPOOL_UTF_DATA (jcf, name_index);
559   int length = JPOOL_UTF_LENGTH (jcf, name_index);
560   char *override;
561
562   if (name_is_method_p (name, length))
563     {
564       /* This field name matches a method.  So override the name with
565          a dummy name.  This is yucky, but it isn't clear what else to
566          do.  FIXME: if the field is static, then we'll be in real
567          trouble.  */
568       if ((flags & ACC_STATIC))
569         {
570           fprintf (stderr, "static field has same name as method\n");
571           found_error = 1;
572           return NULL;
573         }
574
575       override = xmalloc (length + 3);
576       memcpy (override, name, length);
577       strcpy (override + length, "__");
578     }
579   else
580     override = cxx_keyword_subst (name, length);
581
582   return override;
583 }
584
585 /* Print a field name.  Convenience function for use with
586    get_field_name.  */
587 static void
588 print_field_name (stream, jcf, name_index, flags)
589      FILE *stream;
590      JCF *jcf;
591      int name_index;
592      JCF_u2 flags;
593 {
594   char *override = get_field_name (jcf, name_index, flags);
595
596   if (override)
597     {
598       fputs (override, stream);
599       free (override);
600     }
601   else
602     jcf_print_utf8 (stream, JPOOL_UTF_DATA (jcf, name_index),
603                     JPOOL_UTF_LENGTH (jcf, name_index));
604 }
605
606 static void
607 DEFUN(print_field_info, (stream, jcf, name_index, sig_index, flags),
608       FILE *stream AND JCF* jcf
609       AND int name_index AND int sig_index AND JCF_u2 flags)
610 {
611   char *override = NULL;
612
613   generate_access (stream, flags);
614   if (JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
615     {
616       fprintf (stream, "<not a UTF8 constant>");
617       found_error = 1;
618       return;
619     }
620
621   fputs ("  ", out);
622   if ((flags & ACC_STATIC))
623     fputs ("static ", out);
624
625   if ((flags & ACC_FINAL))
626     {
627       if (current_field_value > 0)
628         {
629           char buffer[25];
630           int done = 1;
631
632           switch (JPOOL_TAG (jcf, current_field_value))
633             {
634             case CONSTANT_Integer:
635               {
636                 jint num;
637                 int most_negative = 0;
638                 fputs ("const jint ", out);
639                 print_field_name (out, jcf, name_index, 0);
640                 fputs (" = ", out);
641                 num = JPOOL_INT (jcf, current_field_value);
642                 /* We single out the most negative number to print
643                    specially.  This avoids later warnings from g++.  */
644                 if (num == (jint) 0x80000000)
645                   {
646                     most_negative = 1;
647                     ++num;
648                   }
649                 format_int (buffer, (jlong) num, 10);
650                 fprintf (out, "%sL%s;\n", buffer, most_negative ? " - 1" : "");
651               }
652               break;
653             case CONSTANT_Long:
654               {
655                 jlong num;
656                 int most_negative = 0;
657                 fputs ("const jlong ", out);
658                 print_field_name (out, jcf, name_index, 0);
659                 fputs (" = ", out);
660                 num = JPOOL_LONG (jcf, current_field_value);
661                 /* We single out the most negative number to print
662                    specially..  This avoids later warnings from g++.  */
663                 if (num == (jlong) 0x8000000000000000LL)
664                   {
665                     most_negative = 1;
666                     ++num;
667                   }
668                 format_int (buffer, num, 10);
669                 fprintf (out, "%sLL%s;\n", buffer, most_negative ? " - 1" :"");
670               }
671               break;
672             case CONSTANT_Float:
673               {
674                 jfloat fnum = JPOOL_FLOAT (jcf, current_field_value);
675                 fputs ("const jfloat ", out);
676                 print_field_name (out, jcf, name_index, 0);
677                 if (! java_float_finite (fnum))
678                   fputs (";\n", out);
679                 else
680                   fprintf (out, " = %.10g;\n",  fnum);
681               }
682               break;
683             case CONSTANT_Double:
684               {
685                 jdouble dnum = JPOOL_DOUBLE (jcf, current_field_value);
686                 fputs ("const jdouble ", out);
687                 print_field_name (out, jcf, name_index, 0);
688                 if (! java_double_finite (dnum))
689                   fputs (";\n", out);
690                 else
691                   fprintf (out, " = %.17g;\n",  dnum);
692               }
693               break;
694             default:
695               /* We can't print this as a constant, but we can still
696                  print something sensible.  */
697               done = 0;
698               break;
699             }
700
701           if (done)
702             return;
703         }
704     }
705
706   override = get_field_name (jcf, name_index, flags);
707   print_c_decl (out, jcf, name_index, sig_index, 0, override, flags);
708   fputs (";\n", out);
709
710   if (override)
711     free (override);
712 }
713
714
715 static void
716 DEFUN(print_method_info, (stream, jcf, name_index, sig_index, flags, synth),
717       FILE *stream AND JCF* jcf
718       AND int name_index AND int sig_index AND JCF_u2 flags AND int synth)
719 {
720   const unsigned char *str;
721   int length, is_init = 0;
722   char *override = NULL;
723
724   method_declared = 0;
725   method_access = flags;
726   if (stream && JPOOL_TAG (jcf, name_index) != CONSTANT_Utf8)
727     fprintf (stream, "<not a UTF8 constant>");
728   str = JPOOL_UTF_DATA (jcf, name_index);
729   length = JPOOL_UTF_LENGTH (jcf, name_index);
730
731   /* Ignore synthetic methods. */
732   if (synth)
733     return;
734
735   if (str[0] == '<')
736     {
737       /* Ignore the internally generated method <clinit>. However,
738          treat <init> as a constructor.  */
739       if (! utf8_cmp (str, length, "<init>"))
740         is_init = 1;
741       else if (! METHOD_IS_FINAL (jcf->access_flags, flags)
742                && ! (flags & ACC_STATIC))
743         {
744           /* FIXME: i18n bug here.  Order of prints should not be
745              fixed.  */
746           fprintf (stderr, "ignored method `");
747           jcf_print_utf8 (stderr, str, length);
748           fprintf (stderr, "' marked virtual\n");
749           found_error = 1;
750           return;
751         }
752       else
753         return;
754     }
755
756   /* During the first method pass, build a list of method names. This will
757   be used to determine if field names conflict with method names. */
758   if (! stream)
759     {
760       struct method_name *nn;
761
762       nn = (struct method_name *) xmalloc (sizeof (struct method_name));
763       nn->name = (char *) xmalloc (length);
764       memcpy (nn->name, str, length);
765       nn->length = length;
766       nn->next = method_name_list;
767       nn->sig_length = JPOOL_UTF_LENGTH (jcf, sig_index);
768       nn->signature = (char *) xmalloc (nn->sig_length);
769       memcpy (nn->signature, JPOOL_UTF_DATA (jcf, sig_index),
770               nn->sig_length);
771       method_name_list = nn;
772       
773       /* The rest of this function doesn't matter. */
774       return;
775     }
776
777   /* We don't worry about overrides in JNI mode.  */
778   if (! flag_jni)
779     {
780       /* We can't generate a method whose name is a C++ reserved word.
781          We can't just ignore the function, because that will cause
782          incorrect code to be generated if the function is virtual
783          (not only for calls to this function for for other functions
784          after it in the vtbl).  So we give it a dummy name instead.  */
785       override = cxx_keyword_subst (str, length);
786     }
787
788   if (! stubs && ! flag_jni)
789     {
790       method_printed = 1;
791
792       generate_access (stream, flags);
793       
794       fputs ("  ", out);
795       if ((flags & ACC_STATIC))
796         fputs ("static ", out);
797       else if (! METHOD_IS_FINAL (jcf->access_flags, flags))
798         {
799           /* Don't print `virtual' if we have a constructor.  */
800           if (! is_init)
801             fputs ("virtual ", out);
802         }
803       print_c_decl (out, jcf, name_index, sig_index, is_init, override, flags);
804       
805       if ((flags & ACC_ABSTRACT))
806         fputs (" = 0", out);
807       else
808         method_declared = 1;
809     }
810   else
811     {
812       if (METHOD_IS_NATIVE (flags)) 
813         {
814           method_printed = 1;
815           print_stub_or_jni (out, jcf, name_index, sig_index,
816                              is_init, override, flags);
817         }
818     }
819
820   if (override)
821     free (override);
822 }
823
824 /* Try to decompile a method body.  Right now we just try to handle a
825    simple case that we can do.  Expand as desired.  */
826 static void
827 decompile_method (out, jcf, code_len)
828      FILE *out;
829      JCF *jcf;
830      int code_len;
831 {
832   const unsigned char *codes = jcf->read_ptr;
833   int index;
834   uint16 name_and_type, name;
835
836   /* If the method is synchronized, don't touch it.  */
837   if ((method_access & ACC_SYNCHRONIZED))
838     return;
839
840   if (code_len == 5
841       && codes[0] == OPCODE_aload_0
842       && codes[1] == OPCODE_getfield
843       && (codes[4] == OPCODE_areturn
844           || codes[4] == OPCODE_dreturn
845           || codes[4] == OPCODE_freturn
846           || codes[4] == OPCODE_ireturn
847           || codes[4] == OPCODE_lreturn))
848     {
849       /* Found code like `return FIELD'.  */
850       fputs (" { return ", out);
851       index = (codes[2] << 8) | codes[3];
852       /* FIXME: ensure that tag is CONSTANT_Fieldref.  */
853       /* FIXME: ensure that the field's class is this class.  */
854       name_and_type = JPOOL_USHORT2 (jcf, index);
855       /* FIXME: ensure that tag is CONSTANT_NameAndType.  */
856       name = JPOOL_USHORT1 (jcf, name_and_type);
857       /* FIXME: flags.  */
858       print_field_name (out, jcf, name, 0);
859       fputs ("; }", out);
860       decompiled = 1;
861     }
862   else if (code_len == 2
863            && codes[0] == OPCODE_aload_0
864            && codes[1] == OPCODE_areturn)
865     {
866       /* Found `return this'.  */
867       fputs (" { return this; }", out);
868       decompiled = 1;
869     }
870   else if (code_len == 1 && codes[0] == OPCODE_return)
871     {
872       /* Found plain `return'.  */
873       fputs (" { }", out);
874       decompiled = 1;
875     }
876   else if (code_len == 2
877            && codes[0] == OPCODE_aconst_null
878            && codes[1] == OPCODE_areturn)
879     {
880       /* Found `return null'.  We don't want to depend on NULL being
881          defined.  */
882       fputs (" { return 0; }", out);
883       decompiled = 1;
884     }
885 }
886
887 /* Print one piece of a signature.  Returns pointer to next parseable
888    character on success, NULL on error.  */
889 static const unsigned char *
890 decode_signature_piece (stream, signature, limit, need_space)
891      FILE *stream;
892      const unsigned char *signature, *limit;
893      int *need_space;
894 {
895   const char *ctype;
896   int array_depth = 0;
897
898   switch (signature[0])
899     {
900     case '[':
901       /* More spaghetti.  */
902
903     array_loop:
904       for (signature++; (signature < limit
905                          && *signature >= '0'
906                          && *signature <= '9'); signature++)
907         ;
908       switch (*signature)
909         {
910         case 'B':
911           ctype = "jbyteArray";
912           break;
913         case 'C':
914           ctype = "jcharArray";
915           break;
916         case 'D':
917           ctype = "jdoubleArray";
918           break;
919         case 'F':
920           ctype = "jfloatArray";
921           break;
922         case 'I':
923           ctype = "jintArray";
924           break;
925         case 'S':
926           ctype = "jshortArray";
927           break;
928         case 'J':
929           ctype = "jlongArray";
930           break;
931         case 'Z':
932           ctype = "jbooleanArray";
933           break;
934         case '[':
935           /* We have a nested array.  */
936           ++array_depth;
937           if (! flag_jni)
938             fputs ("JArray<", stream);
939           goto array_loop;
940
941         case 'L':
942           /* We have to generate a reference to JArray here, so that
943              our output matches what the compiler does.  */
944           ++signature;
945           /* Space between `<' and `:' to avoid C++ digraphs.  */
946           if (! flag_jni)
947             fputs ("JArray< ::", stream);
948           while (signature < limit && *signature != ';')
949             {
950               int ch = UTF8_GET (signature, limit);
951               if (! flag_jni)
952                 {
953                   if (ch == '/')
954                     fputs ("::", stream);
955                   else
956                     jcf_print_char (stream, ch);
957                 }
958             }
959           if (! flag_jni)
960             fputs (" *> *", stream);
961           *need_space = 0;
962           ctype = NULL;
963           break;
964         default:
965           /* Unparseable signature.  */
966           return NULL;
967         }
968
969       /* If the previous iterations left us with something to print,
970          print it.  For JNI, we always print `jobjectArray' in the
971          nested cases.  */
972       if (flag_jni && ctype == NULL)
973         {
974           ctype = "jobjectArray";
975           *need_space = 1;
976         }
977       /* The `printit' case will advance SIGNATURE for us.  If we
978          don't go there, we must advance past the `;' ourselves.  */
979       if (ctype != NULL)
980         goto printit;
981       ++signature;
982       break;
983
984     case '(':
985     case ')':
986       /* This shouldn't happen.  */
987       return NULL;
988
989     case 'B': ctype = "jbyte";  goto printit;
990     case 'C': ctype = "jchar";  goto printit;
991     case 'D': ctype = "jdouble";  goto printit;
992     case 'F': ctype = "jfloat";  goto printit;
993     case 'I': ctype = "jint";  goto printit;
994     case 'J': ctype = "jlong";  goto printit;
995     case 'S': ctype = "jshort";  goto printit;
996     case 'Z': ctype = "jboolean";  goto printit;
997     case 'V': ctype = "void";  goto printit;
998     case 'L':
999       if (flag_jni)
1000         {
1001           /* We know about certain types and special-case their
1002              names.
1003              FIXME: something like java.lang.Exception should be
1004              printed as `jthrowable', because it is a subclass.  This
1005              means that gcjh must read the entire hierarchy and
1006              comprehend it.  */
1007           if (! strncmp (signature, "Ljava/lang/String;",
1008                          sizeof ("Ljava/lang/String;") -1))
1009             ctype = "jstring";
1010           else if (! strncmp (signature, "Ljava/lang/Class;",
1011                               sizeof ("Ljava/lang/Class;") - 1))
1012             ctype = "jclass";
1013           else if (! strncmp (signature, "Ljava/lang/Throwable;",
1014                               sizeof ("Ljava/lang/Throwable;") - 1))
1015             ctype = "jthrowable";
1016           else if (! strncmp (signature, "Ljava/lang/ref/WeakReference;",
1017                               sizeof ("Ljava/lang/ref/WeakReference;") - 1))
1018             ctype = "jweak";
1019           else
1020             ctype = "jobject";
1021
1022           while (*signature && *signature != ';')
1023             ++signature;
1024
1025           goto printit;
1026         }
1027       /* Print a leading "::" so we look in the right namespace.  */
1028       fputs ("::", stream);
1029       ++signature;
1030       while (*signature && *signature != ';')
1031         {
1032           int ch = UTF8_GET (signature, limit);
1033           if (ch == '/')
1034             fputs ("::", stream);
1035           else
1036             jcf_print_char (stream, ch);
1037         }
1038       fputs (" *", stream);
1039       if (*signature == ';')
1040         signature++;
1041       *need_space = 0;
1042       break;
1043     default:
1044       *need_space = 1;
1045       jni_print_char (stream, *signature++);
1046       break;
1047     printit:
1048       signature++;
1049       *need_space = 1;
1050       fputs (ctype, stream);
1051       break;
1052     }
1053
1054   if (! flag_jni)
1055     {
1056       while (array_depth-- > 0)
1057         fputs ("> *", stream);
1058     }
1059
1060   return signature;
1061 }
1062
1063 static void
1064 DEFUN(print_c_decl, (stream, jcf, name_index, signature_index, is_init,
1065                      name_override, flags),
1066       FILE* stream AND JCF* jcf
1067       AND int name_index AND int signature_index
1068       AND int is_init AND const char *name_override AND int flags)
1069 {
1070   if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1071     {
1072       fprintf (stream, "<not a UTF8 constant>");
1073       found_error = 1;
1074     }
1075   else
1076     {
1077       int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1078       const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1079       register const  unsigned char *str = str0;
1080       const unsigned char *limit = str + length;
1081       int need_space = 0;
1082       int is_method = str[0] == '(';
1083       const unsigned char *next;
1084
1085       /* If printing a method, skip to the return signature and print
1086          that first.  However, there is no return value if this is a
1087          constructor.  */
1088       if (is_method && ! is_init)
1089         {
1090           while (str < limit)
1091             {
1092               int ch = *str++;
1093               if (ch == ')')
1094                 break;
1095             }
1096         }
1097
1098       /* If printing a field or an ordinary method, then print the
1099          "return value" now.  */
1100       if (! is_method || ! is_init)
1101         {
1102           next = decode_signature_piece (stream, str, limit, &need_space);
1103           if (! next)
1104             {
1105               fprintf (stderr, "unparseable signature: `%s'\n", str0);
1106               found_error = 1;
1107               return;
1108             }
1109         }
1110
1111       /* Now print the name of the thing.  */
1112       if (need_space)
1113         fputs (" ", stream);
1114       print_full_cxx_name (stream, jcf, name_index, 
1115                            signature_index, is_init, name_override,
1116                            flags);
1117     }
1118 }
1119
1120 /* Print the unqualified method name followed by the signature. */
1121 static void
1122 DEFUN(print_full_cxx_name, (stream, jcf, name_index, signature_index,
1123                             is_init, name_override, flags),
1124       FILE* stream AND JCF* jcf
1125       AND int name_index AND int signature_index AND int is_init 
1126       AND const char *name_override AND int flags)
1127 {
1128   int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1129   const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1130   register const unsigned char *str = str0;
1131   const unsigned char *limit = str + length;
1132   int need_space = 0;
1133   int is_method = str[0] == '(';
1134   const unsigned char *next;
1135
1136   if (name_override)
1137     fputs (name_override, stream);
1138   else if (name_index)
1139     {
1140       /* Declare constructors specially.  */
1141       if (is_init)
1142         print_base_classname (stream, jcf, jcf->this_class);
1143       else
1144         print_name (stream, jcf, name_index);
1145     }
1146
1147   if (flag_jni)
1148     {
1149       unsigned char *signature = JPOOL_UTF_DATA (jcf, signature_index);
1150       int sig_len = JPOOL_UTF_LENGTH (jcf, signature_index);
1151       if (overloaded_jni_method_exists_p (JPOOL_UTF_DATA (jcf, name_index),
1152                                           JPOOL_UTF_LENGTH (jcf, name_index),
1153                                           signature, sig_len))
1154         {
1155           /* If this method is overloaded by another native method,
1156              then include the argument information in the mangled
1157              name.  */
1158           unsigned char *limit = signature + sig_len;
1159           fputs ("__", stream);
1160           while (signature < limit)
1161             {
1162               int ch = UTF8_GET (signature, limit);
1163               jni_print_char (stream, ch);
1164               if (ch == ')')
1165                 {
1166                   /* Done.  */
1167                   break;
1168                 }
1169             }
1170         }
1171     }
1172
1173   if (is_method)
1174     {
1175       /* Have a method or a constructor.  Print signature pieces
1176          until done.  */
1177       fputs (" (", stream);
1178
1179       str = str0 + 1;
1180
1181       /* In JNI mode, add extra arguments.  */
1182       if (flag_jni)
1183         {
1184           /* FIXME: it would be nice to know if we are printing a decl
1185              or a definition, and only print `env' for the latter.  */
1186           fputs ("JNIEnv *env", stream);
1187
1188           fputs ((flags & ACC_STATIC) ? ", jclass" : ", jobject", stream);
1189
1190           if (*str != ')')
1191             fputs (", ", stream);
1192         }
1193
1194       while (str < limit && *str != ')')
1195         {
1196           next = decode_signature_piece (stream, str, limit, &need_space);
1197           if (! next)
1198             {
1199               fprintf (stderr, "unparseable signature: `%s'\n", str0);
1200               found_error = 1;
1201               return;
1202             }
1203           
1204           if (next < limit && *next != ')')
1205             fputs (", ", stream);
1206           str = next;
1207         }
1208       
1209       fputs (")", stream);
1210     }
1211 }
1212
1213 /* This is a helper for print_stub_or_jni.  */
1214 static void
1215 DEFUN (print_name_for_stub_or_jni, (stream, jcf, name_index, signature_index,
1216                                     is_init, name_override, flags),
1217        FILE *stream AND JCF *jcf
1218        AND int name_index AND int signature_index
1219        AND int is_init AND const char *name_override AND int flags)
1220 {
1221   const char *const prefix = flag_jni ? "Java_" : "";
1222   print_cxx_classname (stream, prefix, jcf, jcf->this_class);
1223   fputs (flag_jni ? "_" : "::", stream);
1224   print_full_cxx_name (stream, jcf, name_index, 
1225                        signature_index, is_init, name_override,
1226                        flags);
1227 }
1228
1229 static void
1230 DEFUN(print_stub_or_jni, (stream, jcf, name_index, signature_index, is_init,
1231                           name_override, flags),
1232       FILE* stream AND JCF* jcf
1233       AND int name_index AND int signature_index
1234       AND int is_init AND const char *name_override AND int flags)
1235 {
1236   if (JPOOL_TAG (jcf, signature_index) != CONSTANT_Utf8)
1237     {
1238       fprintf (stream, "<not a UTF8 constant>");
1239       found_error = 1;
1240     }
1241   else
1242     {
1243       int length = JPOOL_UTF_LENGTH (jcf, signature_index);
1244       const unsigned char *str0 = JPOOL_UTF_DATA (jcf, signature_index);
1245       register const unsigned char *str = str0;
1246       const unsigned char *limit = str + length;
1247       int need_space = 0;
1248       int is_method = str[0] == '(';
1249       const unsigned char *next;
1250
1251       /* Don't print fields in the JNI case.  */
1252       if (! is_method && flag_jni)
1253         return;
1254
1255       if (flag_jni && ! stubs)
1256         fputs ("extern ", stream);
1257
1258       /* If printing a method, skip to the return signature and print
1259          that first.  However, there is no return value if this is a
1260          constructor.  */
1261       if (is_method && ! is_init)
1262         {
1263           while (str < limit)
1264             {
1265               int ch = *str++;
1266               if (ch == ')')
1267                 break;
1268             }
1269         }
1270
1271       /* If printing a field or an ordinary method, then print the
1272          "return value" now.  Note that a constructor can't be native,
1273          so we don't bother checking this in the JNI case.  */
1274       if (! is_method || ! is_init)
1275         {
1276           next = decode_signature_piece (stream, str, limit, &need_space);
1277           if (! next)
1278             {
1279               fprintf (stderr, "unparseable signature: `%s'\n", str0);
1280               found_error = 1;
1281               return;
1282             }
1283         }
1284
1285       /* When printing a JNI header we need to respect the space.  In
1286          other cases we're just going to insert a newline anyway.  */
1287       fputs (need_space && ! stubs ? " " : "\n", stream);
1288       
1289       /* Now print the name of the thing.  */
1290       print_name_for_stub_or_jni (stream, jcf, name_index,
1291                                   signature_index, is_init, name_override,
1292                                   flags);
1293
1294       /* Print the body.  */
1295       if (stubs)
1296         {
1297           if (flag_jni)
1298             fputs ("\n{\n  (*env)->FatalError (\"", stream);
1299           else
1300             fputs ("\n{\n  JvFail (\"", stream);
1301           print_name_for_stub_or_jni (stream, jcf, name_index,
1302                                       signature_index, is_init,
1303                                       name_override,
1304                                       flags);
1305           fputs (" not implemented\");\n}\n\n", stream);
1306         }
1307     }
1308 }
1309
1310 static void
1311 DEFUN(print_mangled_classname, (stream, jcf, prefix, index),
1312       FILE *stream AND JCF *jcf AND const char *prefix AND int index)
1313 {
1314   int name_index = JPOOL_USHORT1 (jcf, index);
1315   fputs (prefix, stream);
1316   jcf_print_utf8_replace (out,
1317                           JPOOL_UTF_DATA (jcf, name_index),
1318                           JPOOL_UTF_LENGTH (jcf, name_index),
1319                           '/', '_');
1320 }
1321
1322 /* Print PREFIX, then a class name in C++ format.  If the name refers
1323    to an array, ignore it and don't print PREFIX.  Returns 1 if
1324    something was printed, 0 otherwise.  */
1325 static int
1326 print_cxx_classname (stream, prefix, jcf, index)
1327      FILE *stream;
1328      const char *prefix;
1329      JCF *jcf;
1330      int index;
1331 {
1332   int name_index = JPOOL_USHORT1 (jcf, index);
1333   int len, c;
1334   const unsigned char *s, *p, *limit;
1335
1336   s = JPOOL_UTF_DATA (jcf, name_index);
1337   len = JPOOL_UTF_LENGTH (jcf, name_index);
1338   limit = s + len;
1339
1340   /* Explicitly omit arrays here.  */
1341   p = s;
1342   c = UTF8_GET (p, limit);
1343   if (c == '[')
1344     return 0;
1345
1346   fputs (prefix, stream);
1347
1348   /* Print a leading "::" so we look in the right namespace.  */
1349   if (! flag_jni && ! stubs)
1350     fputs ("::", stream);
1351
1352   while (s < limit)
1353     {
1354       c = UTF8_GET (s, limit);
1355       if (c == '/')
1356         fputs (flag_jni ? "_" : "::", stream);
1357       else
1358         jni_print_char (stream, c);
1359     }
1360
1361   return 1;
1362 }
1363
1364 int written_class_count = 0;
1365
1366 /* Return name of superclass.  If LEN is not NULL, fill it with length
1367    of name.  */
1368 static const unsigned char *
1369 super_class_name (derived_jcf, len)
1370      JCF *derived_jcf;
1371      int *len;
1372 {
1373   int supername_index = JPOOL_USHORT1 (derived_jcf, derived_jcf->super_class);
1374   int supername_length = JPOOL_UTF_LENGTH (derived_jcf, supername_index);
1375   const unsigned char *supername =
1376     JPOOL_UTF_DATA (derived_jcf, supername_index);
1377
1378   if (len)
1379     *len = supername_length;
1380
1381   return supername;
1382 }
1383
1384 \f
1385
1386 /* We keep track of all the `#include's we generate, so we can avoid
1387    duplicates.  */
1388 struct include
1389 {
1390   char *name;
1391   struct include *next;
1392 };
1393
1394 /* List of all includes.  */
1395 static struct include *all_includes = NULL;
1396
1397 /* Generate a #include.  */
1398 static void
1399 print_include (out, utf8, len)
1400      FILE *out;
1401      const unsigned char *utf8;
1402      int len;
1403 {
1404   struct include *incl;
1405
1406   if (! out)
1407     return;
1408
1409   if (len == -1)
1410     len = strlen (utf8);
1411
1412   for (incl = all_includes; incl; incl = incl->next)
1413     {
1414       /* We check the length because we might have a proper prefix.  */
1415       if (len == (int) strlen (incl->name)
1416           && ! strncmp (incl->name, utf8, len))
1417         return;
1418     }
1419
1420   incl = (struct include *) xmalloc (sizeof (struct include));
1421   incl->name = xmalloc (len + 1);
1422   strncpy (incl->name, utf8, len);
1423   incl->name[len] = '\0';
1424   incl->next = all_includes;
1425   all_includes = incl;
1426
1427   fputs ("#include <", out);
1428   jcf_print_utf8_replace (out, utf8, len,
1429                           '/',
1430                           flag_jni ? '_' : '/');
1431   fputs (".h>\n", out);
1432 }
1433
1434 \f
1435
1436 /* This is used to represent part of a package or class name.  */
1437 struct namelet
1438 {
1439   /* The text of this part of the name.  */
1440   char *name;
1441   /* True if this represents a class.  */
1442   int is_class;
1443   /* Linked list of all classes and packages inside this one.  */
1444   struct namelet *subnamelets;
1445   /* Pointer to next sibling.  */
1446   struct namelet *next;
1447 };
1448
1449 static void add_namelet PARAMS ((const unsigned char *,
1450                                 const unsigned char *, struct namelet *));
1451 static void print_namelet PARAMS ((FILE *, struct namelet *, int));
1452
1453 /* The special root namelet.  */
1454 static struct namelet root =
1455 {
1456   NULL,
1457   0,
1458   NULL,
1459   NULL
1460 };
1461
1462 /* This extracts the next name segment from the full UTF-8 encoded
1463    package or class name and links it into the tree.  It does this
1464    recursively.  */
1465 static void
1466 add_namelet (name, name_limit, parent)
1467      const unsigned char *name, *name_limit;
1468      struct namelet *parent;
1469 {
1470   const unsigned char *p;
1471   struct namelet *n = NULL, *np;
1472
1473   /* We want to skip the standard namespaces that we assume the
1474      runtime already knows about.  We only do this at the top level,
1475      though, hence the check for `root'.  */
1476   if (parent == &root)
1477     {
1478 #define JAVALANG "java/lang/"
1479 #define JAVAIO "java/io/"
1480 #define JAVAUTIL "java/util/"
1481       if ((name_limit - name >= (int) sizeof (JAVALANG) - 1
1482            && ! strncmp (name, JAVALANG, sizeof (JAVALANG) - 1))
1483           || (name_limit - name >= (int) sizeof (JAVAUTIL) - 1
1484               && ! strncmp (name, JAVAUTIL, sizeof (JAVAUTIL) - 1))
1485           || (name_limit - name >= (int) sizeof (JAVAIO) - 1
1486               && ! strncmp (name, JAVAIO, sizeof (JAVAIO) - 1)))
1487         return;
1488     }
1489
1490   for (p = name; p < name_limit && *p != '/'; ++p)
1491     ;
1492
1493   /* Search for this name beneath the PARENT node.  */
1494   for (np = parent->subnamelets; np != NULL; np = np->next)
1495     {
1496       /* We check the length because we might have a proper prefix.  */
1497       if ((int) strlen (np->name) == p - name &&
1498           ! strncmp (name, np->name, p - name))
1499         {
1500           n = np;
1501           break;
1502         }
1503     }
1504
1505   if (n == NULL)
1506     {
1507       n = (struct namelet *) xmalloc (sizeof (struct namelet));
1508       n->name = xmalloc (p - name + 1);
1509       strncpy (n->name, name, p - name);
1510       n->name[p - name] = '\0';
1511       n->is_class = (p == name_limit);
1512       n->subnamelets = NULL;
1513       n->next = parent->subnamelets;
1514       parent->subnamelets = n;
1515     }
1516
1517   /* We recurse if there is more text, and if the trailing piece does
1518      not represent an inner class. */
1519   if (p < name_limit)
1520     add_namelet (p + 1, name_limit, n);
1521 }
1522
1523 /* Print a single namelet.  Destroys namelets while printing.  */
1524 static void
1525 print_namelet (out, name, depth)
1526      FILE *out;
1527      struct namelet *name;
1528      int depth;
1529 {
1530   int i, term = 0;
1531   struct namelet *c;
1532
1533   if (name->name)
1534     {
1535       for (i = 0; i < depth; ++i)
1536         fputc (' ', out);
1537       fprintf (out, "%s %s", name->is_class ? "class" : "namespace",
1538                name->name);
1539       if (name->is_class && name->subnamelets == NULL)
1540         fputs (";\n", out);
1541       else
1542         {
1543           term = 1;
1544           fputs ("\n", out);
1545           for (i = 0; i < depth; ++i)
1546             fputc (' ', out);
1547           fputs ("{\n", out);
1548         }
1549     }
1550
1551   c = name->subnamelets;
1552   while (c != NULL)
1553     {
1554       struct namelet *next = c->next;
1555       print_namelet (out, c, depth + 2);
1556       c = next;
1557     }
1558
1559   if (name->name)
1560     {
1561       if (term)
1562         {
1563           for (i = 0; i < depth; ++i)
1564             fputc (' ', out);
1565           fputs ("}\n", out);
1566           /* Only print a `;' when printing a class.  C++ is evil.  */
1567           if (name->is_class)
1568             fputs (";", out);
1569         }
1570
1571       free (name->name);
1572       free (name);
1573     }
1574 }
1575
1576 /* This is called to add some classes to the list of classes for which
1577    we need decls.  The signature argument can be a function
1578    signature.  */
1579 static void
1580 add_class_decl (out, jcf, signature)
1581      FILE *out;
1582      JCF *jcf;
1583      JCF_u2 signature;
1584 {
1585   const unsigned char *s = JPOOL_UTF_DATA (jcf, signature);
1586   int len = JPOOL_UTF_LENGTH (jcf, signature);
1587   int i;
1588
1589   for (i = 0; i < len; ++i)
1590     {
1591       int start;
1592
1593       /* If we see an array, then we include the array header.  */
1594       if (s[i] == '[')
1595         {
1596           print_include (out, "gcj/array", -1);
1597           continue;
1598         }
1599
1600       /* We're looking for `L<stuff>;' -- everything else is
1601          ignorable.  */
1602       if (s[i] != 'L')
1603         continue;
1604
1605       for (start = ++i; i < len && s[i] != ';'; ++i)
1606         ;
1607
1608       add_namelet (&s[start], &s[i], &root);
1609     }
1610 }
1611
1612 /* Print declarations for all classes required by this class.  Any
1613    class or package in the `java' package is assumed to be handled
1614    statically in libjava; we don't generate declarations for these.
1615    This makes the generated headers a bit easier to read.  */
1616 static void
1617 print_class_decls (out, jcf, self)
1618      FILE *out;
1619      JCF *jcf;
1620      int self;
1621 {
1622   /* Make sure to always add the current class to the list of things
1623      that should be declared.  */
1624   int name_index = JPOOL_USHORT1 (jcf, self);
1625   int len;
1626   const unsigned char *s;
1627
1628   s = JPOOL_UTF_DATA (jcf, name_index);
1629   len = JPOOL_UTF_LENGTH (jcf, name_index);
1630   add_namelet (s, s + len, &root);
1631
1632   if (root.subnamelets)
1633     {
1634       fputs ("extern \"Java\"\n{\n", out);
1635       /* We use an initial offset of 0 because the root namelet
1636          doesn't cause anything to print.  */
1637       print_namelet (out, &root, 0);
1638       fputs ("};\n\n", out);
1639     }
1640 }
1641
1642 \f
1643
1644 static void
1645 DEFUN(process_file, (jcf, out),
1646       JCF *jcf AND FILE *out)
1647 {
1648   int code, i;
1649   uint32 field_start, method_end, method_start;
1650
1651   current_jcf = jcf;
1652
1653   last_access = -1;
1654
1655   if (jcf_parse_preamble (jcf) != 0)
1656     {
1657       fprintf (stderr, "Not a valid Java .class file.\n");
1658       found_error = 1;
1659       return;
1660     }
1661
1662   /* Parse and possibly print constant pool */
1663   code = jcf_parse_constant_pool (jcf);
1664   if (code != 0)
1665     {
1666       fprintf (stderr, "error while parsing constant pool\n");
1667       found_error = 1;
1668       return;
1669     }
1670   code = verify_constant_pool (jcf);
1671   if (code > 0)
1672     {
1673       fprintf (stderr, "error in constant pool entry #%d\n", code);
1674       found_error = 1;
1675       return;
1676     }
1677
1678   jcf_parse_class (jcf);
1679
1680   if (written_class_count++ == 0 && out)
1681     {
1682       const char *cstart, *cstart2, *mode, *cend, *what, *jflag;
1683       if (flag_jni)
1684         {
1685           cstart = "/*";
1686           cstart2 = "  ";
1687           cend = " */";
1688           mode = "";
1689           what = "JNI";
1690           jflag = " -jni";
1691         }
1692       else
1693         {
1694           cstart = "//";
1695           cstart2 = "//";
1696           cend = "";
1697           mode = " -*- c++ -*-";
1698           what = "CNI";
1699           jflag = "";
1700         }
1701
1702       if (! stubs)
1703         fprintf (out, "%s DO NOT EDIT THIS FILE - it is machine generated%s%s\n\n",
1704                  cstart, mode, cend);
1705       else
1706         {
1707           fprintf (out, "%s This file was created by `gcjh -stubs%s'.%s\n\
1708 %s\n\
1709 %s This file is intended to give you a head start on implementing native\n\
1710 %s methods using %s.\n\
1711 %s Be aware: running `gcjh -stubs %s' once more for this class may\n\
1712 %s overwrite any edits you have made to this file.%s\n\n",
1713                    cstart, jflag, mode,
1714                    cstart2,
1715                    cstart2,
1716                    cstart2,
1717                    what,
1718                    cstart2,
1719                    jflag,
1720                    cstart2,
1721                    cend);
1722         }
1723     }
1724
1725   if (out)
1726     {
1727       if (! stubs)
1728         {
1729           print_mangled_classname (out, jcf, "#ifndef __", jcf->this_class);
1730           fprintf (out, "__\n");
1731
1732           print_mangled_classname (out, jcf, "#define __", jcf->this_class);
1733           fprintf (out, "__\n\n");
1734
1735           if (flag_jni)
1736             {
1737               fprintf (out, "#include <jni.h>\n\n");
1738               fprintf (out, "#ifdef __cplusplus\n");
1739               fprintf (out, "extern \"C\"\n");
1740               fprintf (out, "{\n");
1741               fprintf (out, "#endif\n");
1742             }
1743           else  
1744             {
1745               /* We do this to ensure that inline methods won't be
1746                  `outlined' by g++.  This works as long as method and
1747                  fields are not added by the user.  */
1748               fprintf (out, "#pragma interface\n");
1749
1750               if (jcf->super_class)
1751                 {
1752                   int super_length;
1753                   const unsigned char *supername =
1754                     super_class_name (jcf, &super_length);
1755
1756                   fputs ("\n", out);
1757                   print_include (out, supername, super_length);
1758                 }
1759             }
1760         }
1761       else
1762         {
1763           /* Strip off the ".class" portion of the name when printing
1764              the include file name.  */
1765           int len = strlen (jcf->classname);
1766           if (len > 6 && ! strcmp (&jcf->classname[len - 6], ".class"))
1767             len -= 6;
1768           print_include (out, jcf->classname, len);
1769           if (! flag_jni)
1770             print_include (out, "gcj/cni", -1);
1771         }
1772     }
1773
1774   /* We want to parse the methods first.  But we need to find where
1775      they start.  So first we skip the fields, then parse the methods.
1776      Then we parse the fields and skip the methods.  This is ugly, but
1777      not too bad since we need two full passes to get class decl
1778      information anyway.  */
1779   field_pass = 0;
1780   field_start = JCF_TELL (jcf);
1781   jcf_parse_fields (jcf);
1782
1783   method_start = JCF_TELL (jcf);
1784   method_pass = 0;
1785   jcf_parse_methods (jcf);
1786
1787   if (out)
1788     fputs ("\n", out);
1789
1790   if (out && ! flag_jni)
1791     {
1792       if (! stubs)
1793         print_class_decls (out, jcf, jcf->this_class);
1794
1795       for (i = 0; i < prepend_count; ++i)
1796         fprintf (out, "%s\n", prepend_specs[i]);
1797       if (prepend_count > 0)
1798         fputc ('\n', out);
1799
1800       if (! stubs)
1801         {
1802           if (! print_cxx_classname (out, "class ", jcf, jcf->this_class))
1803             {
1804               fprintf (stderr, "class is of array type\n");
1805               found_error = 1;
1806               return;
1807             }
1808           if (jcf->super_class)
1809             {
1810               if (! print_cxx_classname (out, " : public ", 
1811                                          jcf, jcf->super_class))
1812                 {
1813                   fprintf (stderr, "base class is of array type\n");
1814                   found_error = 1;
1815                   return;
1816                 }
1817             }
1818
1819           fputs ("\n{\n", out);
1820         }
1821     }
1822
1823   /* Now go back for second pass over methods and fields.  */
1824   JCF_SEEK (jcf, method_start);
1825   method_pass = 1;
1826   jcf_parse_methods (jcf);
1827   method_end = JCF_TELL (jcf);
1828
1829   field_pass = 1;
1830   JCF_SEEK (jcf, field_start);
1831   jcf_parse_fields (jcf);
1832   JCF_SEEK (jcf, method_end);
1833
1834   jcf_parse_final_attributes (jcf);
1835
1836   if (out && ! stubs)
1837     {
1838       if (flag_jni)
1839         {
1840           fprintf (out, "\n#ifdef __cplusplus\n");
1841           fprintf (out, "}\n");
1842           fprintf (out, "#endif\n");
1843         }
1844       else
1845         {
1846           /* Generate friend decl if we still must.  */
1847           for (i = 0; i < friend_count; ++i)
1848             fprintf (out, "  friend %s\n", friend_specs[i]);
1849
1850           /* Generate extra declarations.  */
1851           if (add_count > 0)
1852             fputc ('\n', out);
1853           for (i = 0; i < add_count; ++i)
1854             fprintf (out, "  %s\n", add_specs[i]);
1855
1856           /* Generate an entry for the class object.  */
1857           generate_access (out, ACC_PUBLIC);
1858           fprintf (out, "\n  static ::java::lang::Class class$;\n");
1859
1860           fputs ("};\n", out);
1861
1862           if (append_count > 0)
1863             fputc ('\n', out);
1864           for (i = 0; i < append_count; ++i)
1865             fprintf (out, "%s\n", append_specs[i]);
1866         }
1867
1868       print_mangled_classname (out, jcf, 
1869                                "\n#endif /* __", jcf->this_class);
1870       fprintf (out, "__ */\n");
1871     }
1872 }
1873
1874 \f
1875
1876 /* This is used to mark options with no short value.  */
1877 #define LONG_OPT(Num)  ((Num) + 128)
1878
1879 #define OPT_classpath LONG_OPT (0)
1880 #define OPT_CLASSPATH LONG_OPT (1)
1881 #define OPT_HELP      LONG_OPT (2)
1882 #define OPT_TEMP      LONG_OPT (3)
1883 #define OPT_VERSION   LONG_OPT (4)
1884 #define OPT_PREPEND   LONG_OPT (5)
1885 #define OPT_FRIEND    LONG_OPT (6)
1886 #define OPT_ADD       LONG_OPT (7)
1887 #define OPT_APPEND    LONG_OPT (8)
1888 #define OPT_M         LONG_OPT (9)
1889 #define OPT_MM        LONG_OPT (10)
1890 #define OPT_MG        LONG_OPT (11)
1891 #define OPT_MD        LONG_OPT (12)
1892 #define OPT_MMD       LONG_OPT (13)
1893
1894 static struct option options[] =
1895 {
1896   { "classpath", required_argument, NULL, OPT_classpath },
1897   { "CLASSPATH", required_argument, NULL, OPT_CLASSPATH },
1898   { "help",      no_argument,       NULL, OPT_HELP },
1899   { "stubs",     no_argument,       &stubs, 1 },
1900   { "td",        required_argument, NULL, OPT_TEMP },
1901   { "verbose",   no_argument,       NULL, 'v' },
1902   { "version",   no_argument,       NULL, OPT_VERSION },
1903   { "prepend",   required_argument, NULL, OPT_PREPEND },
1904   { "friend",    required_argument, NULL, OPT_FRIEND },
1905   { "add",       required_argument, NULL, OPT_ADD },
1906   { "append",    required_argument, NULL, OPT_APPEND },
1907   { "M",         no_argument,       NULL, OPT_M   },
1908   { "MM",        no_argument,       NULL, OPT_MM  },
1909   { "MG",        no_argument,       NULL, OPT_MG  },
1910   { "MD",        no_argument,       NULL, OPT_MD  },
1911   { "MMD",       no_argument,       NULL, OPT_MMD },
1912   { "jni",       no_argument,       &flag_jni, 1 },
1913   { NULL,        no_argument,       NULL, 0 }
1914 };
1915
1916 static void
1917 usage ()
1918 {
1919   fprintf (stderr, "Try `gcjh --help' for more information.\n");
1920   exit (1);
1921 }
1922
1923 static void
1924 help ()
1925 {
1926   printf ("Usage: gcjh [OPTION]... CLASS...\n\n");
1927   printf ("Generate C++ header files from .class files\n\n");
1928   printf ("  -stubs                  Generate an implementation stub file\n");
1929   printf ("  -jni                    Generate a JNI header or stub\n");
1930   printf ("\n");
1931   printf ("  -add TEXT               Insert TEXT into class body\n");
1932   printf ("  -append TEXT            Insert TEXT after class declaration\n");
1933   printf ("  -friend TEXT            Insert TEXT as `friend' declaration\n");
1934   printf ("  -prepend TEXT           Insert TEXT before start of class\n");
1935   printf ("\n");
1936   printf ("  --classpath PATH        Set path to find .class files\n");
1937   printf ("  --CLASSPATH PATH        Set path to find .class files\n");
1938   printf ("  -IDIR                   Append directory to class path\n");
1939   printf ("  -d DIRECTORY            Set output directory name\n");
1940   printf ("  -o FILE                 Set output file name\n");
1941   printf ("  -td DIRECTORY           Set temporary directory name\n");
1942   printf ("\n");
1943   printf ("  --help                  Print this help, then exit\n");
1944   printf ("  --version               Print version number, then exit\n");
1945   printf ("  -v, --verbose           Print extra information while running\n");
1946   printf ("\n");
1947   printf ("  -M                      Print all dependencies to stdout;\n");
1948   printf ("                             suppress ordinary output\n");
1949   printf ("  -MM                     Print non-system dependencies to stdout;\n");
1950   printf ("                             suppress ordinary output\n");
1951   printf ("  -MD                     Print all dependencies to stdout\n");
1952   printf ("  -MMD                    Print non-system dependencies to stdout\n");
1953   /* We omit -MG until it is implemented.  */
1954   printf ("\n");
1955   printf ("For bug reporting instructions, please see:\n");
1956   printf ("%s.\n", GCCBUGURL);
1957   exit (0);
1958 }
1959
1960 static void
1961 version ()
1962 {
1963   printf ("gcjh (%s)\n\n", version_string);
1964   printf ("Copyright (C) 1998, 1999 Free Software Foundation, Inc.\n");
1965   printf ("This is free software; see the source for copying conditions.  There is NO\n");
1966   printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
1967   exit (0);
1968 }
1969
1970 int
1971 DEFUN(main, (argc, argv),
1972       int argc AND char** argv)
1973 {
1974   JCF jcf;
1975   int argi;
1976   char *output_file = NULL;
1977   int emit_dependencies = 0, suppress_output = 0;
1978   int opt;
1979
1980   if (argc <= 1)
1981     {
1982       fprintf (stderr, "gcjh: no classes specified\n");
1983       usage ();
1984     }
1985
1986   jcf_path_init ();
1987
1988   /* We use getopt_long_only to allow single `-' long options.  For
1989      some of our options this is more natural.  */
1990   while ((opt = getopt_long_only (argc, argv, "I:d:o:v", options, NULL)) != -1)
1991     {
1992       switch (opt)
1993         {
1994         case 0:
1995           /* Already handled.  */
1996           break;
1997
1998         case 'o':
1999           output_file = optarg;
2000           break;
2001
2002         case 'd':
2003           output_directory = optarg;
2004           break;
2005
2006         case 'I':
2007           jcf_path_include_arg (optarg);
2008           break;
2009
2010         case 'v':
2011           verbose++;
2012           break;
2013
2014         case OPT_classpath:
2015           jcf_path_classpath_arg (optarg);
2016           break;
2017
2018         case OPT_CLASSPATH:
2019           jcf_path_CLASSPATH_arg (optarg);
2020           break;
2021
2022         case OPT_HELP:
2023           help ();
2024           break;
2025
2026         case OPT_TEMP:
2027           temp_directory = optarg;
2028           break;
2029
2030         case OPT_VERSION:
2031           version ();
2032           break;
2033
2034         case OPT_PREPEND:
2035           if (prepend_count == 0)
2036             prepend_specs = (char**) ALLOC (argc * sizeof (char*));
2037           prepend_specs[prepend_count++] = optarg;
2038           break;
2039
2040         case OPT_FRIEND:
2041           if (friend_count == 0)
2042             friend_specs = (char**) ALLOC (argc * sizeof (char*));
2043           friend_specs[friend_count++] = optarg;
2044           break;
2045
2046         case OPT_ADD:
2047           if (add_count == 0)
2048             add_specs = (char**) ALLOC (argc * sizeof (char*));
2049           add_specs[add_count++] = optarg;
2050           break;
2051
2052         case OPT_APPEND:
2053           if (append_count == 0)
2054             append_specs = (char**) ALLOC (argc * sizeof (char*));
2055           append_specs[append_count++] = optarg;
2056           break;
2057
2058         case OPT_M:
2059           emit_dependencies = 1;
2060           suppress_output = 1;
2061           jcf_dependency_init (1);
2062           break;
2063
2064         case OPT_MM:
2065           emit_dependencies = 1;
2066           suppress_output = 1;
2067           jcf_dependency_init (0);
2068           break;
2069
2070         case OPT_MG:
2071           fprintf (stderr, "gcjh: `-MG' option is unimplemented\n");
2072           exit (1);
2073
2074         case OPT_MD:
2075           emit_dependencies = 1;
2076           jcf_dependency_init (1);
2077           break;
2078
2079         case OPT_MMD:
2080           emit_dependencies = 1;
2081           jcf_dependency_init (0);
2082           break;
2083
2084         default:
2085           usage ();
2086           break;
2087         }
2088     }
2089
2090   if (optind == argc)
2091     {
2092       fprintf (stderr, "gcjh: no classes specified\n");
2093       usage ();
2094     }
2095
2096   jcf_path_seal ();
2097
2098   if (output_file && emit_dependencies)
2099     {
2100       fprintf (stderr, "gcjh: can't specify both -o and -MD\n");
2101       exit (1);
2102     }
2103
2104   for (argi = optind; argi < argc; argi++)
2105     {
2106       char *classname = argv[argi];
2107       char *current_output_file;
2108       const char *classfile_name;
2109
2110       if (verbose)
2111         fprintf (stderr, "Processing %s\n", classname);
2112       if (! output_file)
2113         jcf_dependency_reset ();
2114       classfile_name = find_class (classname, strlen (classname), &jcf, 0);
2115       if (classfile_name == NULL)
2116         {
2117           fprintf (stderr, "%s: no such class\n", classname);
2118           exit (1);
2119         }
2120       if (verbose)
2121         fprintf (stderr, "Found in %s\n", classfile_name);
2122       if (output_file)
2123         {
2124           if (strcmp (output_file, "-") == 0)
2125             out = stdout;
2126           else if (out == NULL)
2127             {
2128               out = fopen (output_file, "w");
2129             }
2130           if (out == NULL)
2131             {
2132               perror (output_file);
2133               exit (1);
2134             }
2135           current_output_file = output_file;
2136         }
2137       else
2138         {
2139           int dir_len = strlen (output_directory);
2140           int i, classname_length = strlen (classname);
2141           current_output_file = (char*) ALLOC (dir_len + classname_length + 5);
2142           strcpy (current_output_file, output_directory);
2143           if (dir_len > 0 && output_directory[dir_len-1] != '/')
2144             current_output_file[dir_len++] = '/';
2145           for (i = 0; classname[i] != '\0'; i++)
2146             {
2147               char ch = classname[i];
2148               if (ch == '.')
2149                 ch = '/';
2150               if (flag_jni && ch == '/')
2151                 ch = '_';
2152               current_output_file[dir_len++] = ch;
2153             }
2154           if (emit_dependencies)
2155             {
2156               if (suppress_output)
2157                 {
2158                   jcf_dependency_set_dep_file ("-");
2159                   out = NULL;
2160                 }
2161               else
2162                 {
2163                   /* We use `.hd' and not `.d' to avoid clashes with
2164                      dependency tracking from straight compilation.  */
2165                   strcpy (current_output_file + dir_len, ".hd");
2166                   jcf_dependency_set_dep_file (current_output_file);
2167                 }
2168             }
2169           strcpy (current_output_file + dir_len, 
2170                   stubs ? (flag_jni ? ".c" : ".cc") : ".h");
2171           jcf_dependency_set_target (current_output_file);
2172           if (! suppress_output)
2173             {
2174               out = fopen (current_output_file, "w");
2175               if (out == NULL)
2176                 {
2177                   perror (current_output_file);
2178                   exit (1);
2179                 }
2180             }
2181         }
2182       process_file (&jcf, out);
2183       JCF_FINISH (&jcf);
2184       if (current_output_file != output_file)
2185         free (current_output_file);
2186       jcf_dependency_write ();
2187     }
2188
2189   if (out != NULL && out != stdout)
2190     fclose (out);
2191
2192   return found_error;
2193 }