OSDN Git Service

2008-11-04 Andrew Haley <aph@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-parse.c
1 /* Parser for Java(TM) .class files.
2    Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3    2005, 2006, 2007, 2008 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.
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> */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tm.h"
31 #include "tree.h"
32 #include "real.h"
33 #include "obstack.h"
34 #include "flags.h"
35 #include "java-except.h"
36 #include "input.h"
37 #include "javaop.h"
38 #include "java-tree.h"
39 #include "toplev.h"
40 #include "parse.h"
41 #include "ggc.h"
42 #include "debug.h"
43 #include "assert.h"
44 #include "tm_p.h"
45 #include "cgraph.h"
46 #include "vecprim.h"
47
48 #ifdef HAVE_LOCALE_H
49 #include <locale.h>
50 #endif
51
52 #ifdef HAVE_LANGINFO_CODESET
53 #include <langinfo.h>
54 #endif
55
56 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
57 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
58 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
59 #define JPOOL_UTF_DATA(JCF, INDEX) \
60   ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
61 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
62   do { \
63     unsigned char save;  unsigned char *text; \
64     JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
65     text = (JCF)->read_ptr; \
66     save = text[LENGTH]; \
67     text[LENGTH] = 0; \
68     (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
69     text[LENGTH] = save; \
70     JCF_SKIP (JCF, LENGTH); } while (0)
71
72 #include "jcf.h"
73
74 extern struct obstack temporary_obstack;
75
76 static GTY(()) tree parse_roots[3];
77
78 /* The FIELD_DECL for the current field.  */
79 #define current_field parse_roots[0]
80
81 /* The METHOD_DECL for the current method.  */
82 #define current_method parse_roots[1]
83
84 /* A list of TRANSLATION_UNIT_DECLs for the files to be compiled.  */
85 #define current_file_list parse_roots[2]
86
87 /* Line 0 in current file, if compiling from bytecode. */
88 static location_t file_start_location;
89
90 /* The Java archive that provides main_class;  the main input file. */
91 static GTY(()) struct JCF * main_jcf;
92
93 /* The number of source files passed to us by -fsource-filename and an
94    array of pointers to each name.  Used by find_sourcefile().  */
95 static int num_files = 0;
96 static char **filenames;
97
98 static struct ZipFile *localToFile;
99
100 /* A map of byte offsets in the reflection data that are fields which
101    need renumbering.  */
102 bitmap field_offsets;
103 bitmap_obstack bit_obstack;
104
105 /* Declarations of some functions used here.  */
106 static void handle_innerclass_attribute (int count, JCF *, int len);
107 static tree give_name_to_class (JCF *jcf, int index);
108 static char *compute_class_name (struct ZipDirectory *zdir);
109 static int classify_zip_file (struct ZipDirectory *zdir);
110 static void parse_zip_file_entries (void);
111 static void process_zip_dir (FILE *);
112 static void parse_class_file (void);
113 static void handle_deprecated (void);
114 static void set_source_filename (JCF *, int);
115 static void jcf_parse (struct JCF*);
116 static void load_inner_classes (tree);
117 static void handle_annotation (JCF *jcf, int level);
118 static void java_layout_seen_class_methods (void);
119
120 /* Handle "Deprecated" attribute.  */
121 static void
122 handle_deprecated (void)
123 {
124   if (current_field != NULL_TREE)
125     FIELD_DEPRECATED (current_field) = 1;
126   else if (current_method != NULL_TREE)
127     METHOD_DEPRECATED (current_method) = 1;
128   else if (current_class != NULL_TREE)
129     CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
130   else
131     {
132       /* Shouldn't happen.  */
133       gcc_unreachable ();
134     }
135 }
136
137 \f
138
139 /* Reverse a string.  */
140 static char *
141 reverse (const char *s)
142 {
143   if (s == NULL)
144     return NULL;
145   else
146     {
147       int len = strlen (s);
148       char *d = XNEWVAR (char, len + 1);
149       const char *sp;
150       char *dp;
151       
152       d[len] = 0;
153       for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
154         *dp = *sp;
155
156       return d;
157     }
158 }
159
160 /* Compare two strings for qsort().  */
161 static int
162 cmpstringp (const void *p1, const void *p2)
163 {
164   /* The arguments to this function are "pointers to
165      pointers to char", but strcmp() arguments are "pointers
166      to char", hence the following cast plus dereference */
167
168   return strcmp(*(const char *const*) p1, *(const char *const*) p2);
169 }
170
171 /* Create an array of strings, one for each source file that we've
172    seen.  fsource_filename can either be the name of a single .java
173    file or a file that contains a list of filenames separated by
174    newlines.  */
175 void 
176 java_read_sourcefilenames (const char *fsource_filename)
177 {
178   if (fsource_filename 
179       && filenames == 0
180       && strlen (fsource_filename) > strlen (".java")
181       && strcmp ((fsource_filename 
182                   + strlen (fsource_filename)
183                   - strlen (".java")),
184                  ".java") != 0)
185     {
186 /*       fsource_filename isn't a .java file but a list of filenames
187        separated by newlines */
188       FILE *finput = fopen (fsource_filename, "r");
189       int len = 0;
190       int longest_line = 0;
191
192       gcc_assert (finput);
193
194       /* Find out how many files there are, and how long the filenames are.  */
195       while (! feof (finput))
196         {
197           int ch = getc (finput);
198           if (ch == '\n')
199             {
200               num_files++;
201               if (len > longest_line)
202                 longest_line = len;
203               len = 0;
204               continue;
205             }
206           if (ch == EOF)
207             break;
208           len++;
209         }
210
211       rewind (finput);
212
213       /* Read the filenames.  Put a pointer to each filename into the
214          array FILENAMES.  */
215       {
216         char *linebuf = (char *) alloca (longest_line + 1);
217         int i = 0;
218         int charpos;
219
220         filenames = XNEWVEC (char *, num_files);
221
222         charpos = 0;
223         for (;;)
224           {
225             int ch = getc (finput);
226             if (ch == EOF)
227               break;
228             if (ch == '\n')
229               {
230                 linebuf[charpos] = 0;
231                 gcc_assert (i < num_files);             
232                 /* ???  Perhaps we should use lrealpath() here.  Doing
233                    so would tidy up things like /../ but the rest of
234                    gcc seems to assume relative pathnames, not
235                    absolute pathnames.  */
236 /*              realname = lrealpath (linebuf); */
237                 filenames[i++] = reverse (linebuf);
238                 charpos = 0;
239                 continue;
240               }
241             gcc_assert (charpos < longest_line);
242             linebuf[charpos++] = ch;
243           }
244
245         if (num_files > 1)
246           qsort (filenames, num_files, sizeof (char *), cmpstringp);
247       }
248       fclose (finput);
249     }
250   else
251     {
252       filenames = XNEWVEC (char *, 1);      
253       filenames[0] = reverse (fsource_filename);
254       num_files = 1;
255     }
256 }
257
258 /* Given a relative pathname such as foo/bar.java, attempt to find a
259    longer pathname with the same suffix.  
260
261    This is a best guess heuristic; with some weird class hierarchies we
262    may fail to pick the correct source file.  For example, if we have
263    the filenames foo/bar.java and also foo/foo/bar.java, we do not
264    have enough information to know which one is the right match for
265    foo/bar.java.  */
266
267 static const char *
268 find_sourcefile (const char *name)
269 {
270   int i = 0, j = num_files-1;
271   char *found = NULL;
272   
273   if (filenames)
274     {
275       char *revname = reverse (name);
276
277       do
278         {
279           int k = (i+j) / 2;
280           int cmp = strncmp (revname, filenames[k], strlen (revname));
281           if (cmp == 0)
282             {
283               /*  OK, so we found one.  But is it a unique match?  */
284               if ((k > i
285                    && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
286                   || (k < j
287                       && (strncmp (revname, filenames[k+1], strlen (revname)) 
288                           == 0)))
289                 ;
290               else
291                 found = filenames[k];
292               break;
293             }
294           if (cmp > 0)
295             i = k+1;
296           else
297             j = k-1;
298         }
299       while (i <= j);
300
301       free (revname);
302     }
303
304   if (found && strlen (found) > strlen (name))
305     return reverse (found);
306   else
307     return name;
308 }
309
310 \f
311
312 /* Handle "SourceFile" attribute. */
313
314 static void
315 set_source_filename (JCF *jcf, int index)
316 {
317   tree sfname_id = get_name_constant (jcf, index);
318   const char *sfname = IDENTIFIER_POINTER (sfname_id);
319   const char *old_filename = input_filename;
320   int new_len = IDENTIFIER_LENGTH (sfname_id);
321   if (old_filename != NULL)
322     {
323       int old_len = strlen (old_filename);
324       /* Use the current input_filename (derived from the class name)
325          if it has a directory prefix, but otherwise matches sfname. */
326       if (old_len > new_len
327           && strcmp (sfname, old_filename + old_len - new_len) == 0
328           && (old_filename[old_len - new_len - 1] == '/'
329               || old_filename[old_len - new_len - 1] == '\\'))
330         return;
331     }
332   if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
333     {
334       const char *class_name
335         = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
336       char *dot = strrchr (class_name, '.');
337       if (dot != NULL)
338         {
339           /* Length of prefix, not counting final dot. */
340           int i = dot - class_name;
341           /* Concatenate current package prefix with new sfname. */
342           char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
343           strcpy (buf + i + 1, sfname);
344           /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
345              Note we start at the end with the final package dot. */
346           for (; i >= 0;  i--)
347             {
348               char c = class_name[i];
349               if (c == '.')
350                 c = DIR_SEPARATOR;
351               buf[i] = c;
352             }
353           sfname_id = get_identifier (buf);
354           free (buf);
355           sfname = IDENTIFIER_POINTER (sfname_id);
356         }
357     }
358       
359   sfname = find_sourcefile (sfname);
360   line_table->maps[line_table->used-1].to_file = sfname;
361   if (current_class == main_class) main_input_filename = sfname;
362 }
363
364
365 \f
366
367 /* Annotation handling.  
368
369    The technique we use here is to copy the annotation data directly
370    from the input class file into the output file.  We don't decode the
371    data at all, merely rewriting constant indexes whenever we come
372    across them: this is necessary because the constant pool in the
373    output file isn't the same as the constant pool in in the input.
374
375    The main advantage of this technique is that the resulting
376    annotation data is pointer-free, so it doesn't have to be relocated
377    at startup time.  As a consequence of this, annotations have no
378    performance impact unless they are used.  Also, this representation
379    is very dense.  */
380
381
382 /* Expand TYPE_REFLECTION_DATA by DELTA bytes.  Return the address of
383    the start of the newly allocated region.  */
384
385 static unsigned char*
386 annotation_grow (int delta)
387 {
388   unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
389   long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
390   long len = *datasize;
391
392   if (*data == NULL)
393     {
394       *data = XNEWVAR (unsigned char, delta);
395     }
396   else
397     {
398       int newlen = *datasize + delta;
399       if (floor_log2 (newlen) != floor_log2 (*datasize))
400         *data = XRESIZEVAR (unsigned char, *data,  2 << (floor_log2 (newlen)));
401     }
402   *datasize += delta;
403   return *data + len;
404 }
405
406 /* annotation_rewrite_TYPE.  Rewrite various int types at p.  Use Java
407    byte order (i.e. big endian.)  */
408
409 static void
410 annotation_rewrite_byte (unsigned int n, unsigned char *p)
411 {
412   p[0] = n;
413 }
414
415 static void
416 annotation_rewrite_short (unsigned int n, unsigned char *p)
417 {
418   p[0] = n>>8;
419   p[1] = n;
420 }
421
422 static void
423 annotation_rewrite_int (unsigned int n, unsigned char *p)
424 {
425   p[0] = n>>24;
426   p[1] = n>>16;
427   p[2] = n>>8;
428   p[3] = n;
429 }
430
431 /* Read a 16-bit unsigned int in Java byte order (i.e. big
432    endian.)  */
433
434 static uint16
435 annotation_read_short (unsigned char *p)
436 {
437   uint16 tmp = p[0];
438   tmp = (tmp << 8) | p[1];
439   return tmp;
440 }
441
442 /* annotation_write_TYPE.  Rewrite various int types, appending them
443    to TYPE_REFLECTION_DATA.  Use Java byte order (i.e. big
444    endian.)  */
445
446 static void
447 annotation_write_byte (unsigned int n)
448 {
449   annotation_rewrite_byte (n, annotation_grow (1));
450 }
451
452 static void
453 annotation_write_short (unsigned int n)
454 {
455   annotation_rewrite_short (n, annotation_grow (2));
456 }
457
458 static void
459 annotation_write_int (unsigned int n)
460 {
461   annotation_rewrite_int (n, annotation_grow (4));
462 }
463
464 /* Create a 64-bit constant in the constant pool.
465
466    This is used for both integer and floating-point types.  As a
467    consequence, it will not work if the target floating-point format
468    is anything other than IEEE-754.  While this is arguably a bug, the
469    runtime library makes exactly the same assumption and it's unlikely
470    that Java will ever run on a non-IEEE machine.  */
471
472 static int 
473 handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
474                     int index, bool big_endian)
475 {
476   /* If we're on a 64-bit platform we can fit a long or double
477      into the same space as a jword.  */
478   if (POINTER_SIZE >= 64)
479     index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
480
481   /* In a compiled program the constant pool is in native word
482      order.  How weird is that???  */
483   else if (big_endian)
484     index = find_constant2 (cpool, kind,
485                             JPOOL_INT (jcf, index), 
486                             JPOOL_INT (jcf, index+1));
487   else
488     index = find_constant2 (cpool, kind,
489                             JPOOL_INT (jcf, index+1), 
490                             JPOOL_INT (jcf, index));
491   
492   return index;
493 }
494
495 /* Given a class file and an index into its constant pool, create an
496    entry in the outgoing constant pool for the same item.  */
497
498 static uint16
499 handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
500 {
501   enum cpool_tag kind;
502   CPool *cpool = cpool_for_class (output_class);
503   
504   if (index == 0)
505     return 0;
506
507   if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
508     error ("<constant pool index %d not in range>", index);
509   
510   kind = JPOOL_TAG (jcf, index);
511
512   if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
513     {
514       if (purpose == CONSTANT_Class
515           && kind == CONSTANT_Utf8)
516         ;
517       else
518         error ("<constant pool index %d unexpected type", index);
519     }
520
521   switch (kind)
522     {
523     case CONSTANT_Class:
524     case CONSTANT_ResolvedClass:
525       {
526         /* For some reason I know not the what of, class names in
527            annotations are UTF-8 strings in the constant pool but
528            class names in EnclosingMethod attributes are real class
529            references.  Set CONSTANT_LazyFlag here so that the VM
530            doesn't attempt to resolve them at class initialization
531            time.  */
532         tree resolved_class, class_name;
533         resolved_class = get_class_constant (jcf, index);
534         class_name = build_internal_class_name (resolved_class);
535         index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
536                                      (unmangle_classname 
537                                       (IDENTIFIER_POINTER(class_name),
538                                        IDENTIFIER_LENGTH(class_name))));
539         break;
540       }
541     case CONSTANT_Utf8:
542       {
543         tree utf8 = get_constant (jcf, index);
544         if (purpose == CONSTANT_Class)
545           /* Create a constant pool entry for a type signature.  This
546              one has '.' rather than '/' because it isn't going into a
547              class file, it's going into a compiled object.
548              
549              This has to match the logic in
550              _Jv_ClassReader::prepare_pool_entry().  */
551           utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
552                                      IDENTIFIER_LENGTH(utf8));
553         index = alloc_name_constant (kind, utf8);
554       }
555       break;
556
557     case CONSTANT_Long:
558       index = handle_long_constant (jcf, cpool, kind, index, 
559                                     WORDS_BIG_ENDIAN);
560       break;
561       
562     case CONSTANT_Double:
563       index = handle_long_constant (jcf, cpool, kind, index, 
564                                     FLOAT_WORDS_BIG_ENDIAN);
565       break;
566
567     case CONSTANT_Float:
568     case CONSTANT_Integer:
569       index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
570       break;
571       
572     case CONSTANT_NameAndType:
573       {
574         uint16 name = JPOOL_USHORT1 (jcf, index);
575         uint16 sig = JPOOL_USHORT2 (jcf, index);
576         uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
577         uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
578         jword new_index = (name_index << 16) | sig_index;
579         index = find_constant1 (cpool, kind, new_index);
580       }
581       break;
582
583     default:
584       abort ();
585     }
586   
587   return index;
588 }
589
590 /* Read an element_value structure from an annotation in JCF.  Return
591    the constant pool index for the resulting constant pool entry.  */
592
593 static int
594 handle_element_value (JCF *jcf, int level)
595 {
596   uint8 tag = JCF_readu (jcf);
597   int index = 0;
598
599   annotation_write_byte (tag);
600   switch (tag)
601     {
602     case 'B':
603     case 'C':
604     case 'S':
605     case 'Z':
606     case 'I':
607       {
608         uint16 cindex = JCF_readu2 (jcf);
609         index = handle_constant (jcf, cindex,
610                                  CONSTANT_Integer);
611         annotation_write_short (index);
612       }
613       break;
614     case 'D':
615       {
616         uint16 cindex = JCF_readu2 (jcf);
617         index = handle_constant (jcf, cindex,
618                                  CONSTANT_Double);
619         annotation_write_short (index);
620       }
621       break;
622     case 'F':
623       {
624         uint16 cindex = JCF_readu2 (jcf);
625         index = handle_constant (jcf, cindex,
626                                  CONSTANT_Float);
627         annotation_write_short (index);
628       }
629       break;
630     case 'J':
631       {
632         uint16 cindex = JCF_readu2 (jcf);
633         index = handle_constant (jcf, cindex,
634                                  CONSTANT_Long);
635         annotation_write_short (index);
636       }
637       break;
638     case 's':
639       {
640         uint16 cindex = JCF_readu2 (jcf);
641         /* Despite what the JVM spec says, compilers generate a Utf8
642            constant here, not a String.  */
643         index = handle_constant (jcf, cindex,
644                                  CONSTANT_Utf8);
645         annotation_write_short (index);
646       }
647       break;
648
649     case 'e':
650       {
651         uint16 type_name_index = JCF_readu2 (jcf);
652         uint16 const_name_index = JCF_readu2 (jcf);
653         index = handle_constant (jcf, type_name_index,
654                                  CONSTANT_Class);
655         annotation_write_short (index);
656         index = handle_constant (jcf, const_name_index,
657                                  CONSTANT_Utf8);
658         annotation_write_short (index);
659      }
660       break;
661     case 'c':
662       {
663         uint16 class_info_index = JCF_readu2 (jcf);
664         index = handle_constant (jcf, class_info_index,
665                                  CONSTANT_Class);
666         annotation_write_short (index);
667       }
668       break;
669     case '@':
670       {
671         handle_annotation (jcf, level + 1);
672       }
673       break;
674     case '[':
675       {
676         uint16 n_array_elts = JCF_readu2 (jcf);
677         annotation_write_short (n_array_elts);
678         while (n_array_elts--)
679           handle_element_value (jcf, level + 1);
680       }
681       break;
682     default:
683       abort();
684       break;
685     }
686   return index;
687 }
688
689 /* Read an annotation structure from JCF.  Write it to the
690    reflection_data field of the outgoing class.  */
691
692 static void
693 handle_annotation (JCF *jcf, int level)
694 {
695   uint16 type_index = JCF_readu2 (jcf);
696   uint16 npairs = JCF_readu2 (jcf);
697   int index = handle_constant (jcf, type_index,
698                                CONSTANT_Class);
699   annotation_write_short (index);
700   annotation_write_short (npairs);
701   while (npairs--)
702     {
703       uint16 name_index = JCF_readu2 (jcf);
704       index = handle_constant (jcf, name_index,
705                                CONSTANT_Utf8);
706       annotation_write_short (index);
707       handle_element_value (jcf, level + 2);
708     }
709 }
710
711 /* Read an annotation count from JCF, and write the following
712    annotations to the reflection_data field of the outgoing class.  */
713
714 static void
715 handle_annotations (JCF *jcf, int level)
716 {
717   uint16 num = JCF_readu2 (jcf);
718   annotation_write_short (num);
719   while (num--)
720     handle_annotation (jcf, level);
721 }
722
723 /* As handle_annotations(), but perform a sanity check that we write
724    the same number of bytes that we were expecting.  */
725
726 static void
727 handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf, 
728                              long length)
729 {
730   long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
731
732   handle_annotations (jcf, 0);
733
734   gcc_assert (old_datasize + length
735               == TYPE_REFLECTION_DATASIZE (current_class));
736 }
737
738 /* gcj permutes its fields array after generating annotation_data, so
739    we have to fixup field indexes for fields that have moved.  Given
740    ARG, a VEC_int, fixup the field indexes in the reflection_data of
741    the outgoing class.  We use field_offsets to tell us where the
742    fixups must go.  */
743
744 void
745 rewrite_reflection_indexes (void *arg)
746 {
747   bitmap_iterator bi;
748   unsigned int offset;
749   VEC(int, heap) *map = (VEC(int, heap) *) arg;
750   unsigned char *data = TYPE_REFLECTION_DATA (current_class);
751
752   if (map)
753     {
754       EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
755         {
756           uint16 index = annotation_read_short (data + offset);
757           annotation_rewrite_short 
758             (VEC_index (int, map, index), data + offset);
759         }
760     }
761 }
762
763 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
764    reflection_data of the outgoing class.  */
765
766 static void
767 handle_member_annotations (int member_index, JCF *jcf, 
768                            const unsigned char *name ATTRIBUTE_UNUSED, 
769                            long len, jv_attr_type member_type)
770 {
771   int new_len = len + 1;
772   annotation_write_byte (member_type);
773   if (member_type != JV_CLASS_ATTR)
774     new_len += 2;
775   annotation_write_int (new_len);
776   annotation_write_byte (JV_ANNOTATIONS_KIND);
777   if (member_type == JV_FIELD_ATTR)
778     bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
779   if (member_type != JV_CLASS_ATTR)
780     annotation_write_short (member_index);
781   handle_annotation_attribute (member_index, jcf, len);
782 }
783
784 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
785    to the reflection_data of the outgoing class.  */
786
787 static void
788 handle_parameter_annotations (int member_index, JCF *jcf, 
789                               const unsigned char *name ATTRIBUTE_UNUSED, 
790                               long len, jv_attr_type member_type)
791 {
792   int new_len = len + 1;
793   uint8 num;
794   annotation_write_byte (member_type);
795   if (member_type != JV_CLASS_ATTR)
796     new_len += 2;
797   annotation_write_int (new_len);
798   annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
799   if (member_type != JV_CLASS_ATTR)
800     annotation_write_short (member_index);
801   num = JCF_readu (jcf);
802   annotation_write_byte (num);
803   while (num--)
804     handle_annotations (jcf, 0);
805 }
806
807
808 /* Read the AnnotationDefault data from JCF and write them to the
809    reflection_data of the outgoing class.  */
810
811 static void
812 handle_default_annotation (int member_index, JCF *jcf, 
813                            const unsigned char *name ATTRIBUTE_UNUSED, 
814                            long len, jv_attr_type member_type)
815 {
816   int new_len = len + 1;
817   annotation_write_byte (member_type);
818   if (member_type != JV_CLASS_ATTR)
819     new_len += 2;
820   annotation_write_int (new_len);
821   annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
822   if (member_type != JV_CLASS_ATTR)
823     annotation_write_short (member_index);
824   handle_element_value (jcf, 0);
825 }
826
827 /* As above, for the EnclosingMethod attribute.  */
828
829 static void
830 handle_enclosingmethod_attribute (int member_index, JCF *jcf, 
831                            const unsigned char *name ATTRIBUTE_UNUSED, 
832                            long len, jv_attr_type member_type)
833 {
834   int new_len = len + 1;
835   uint16 index;
836   annotation_write_byte (member_type);
837   if (member_type != JV_CLASS_ATTR)
838     new_len += 2;
839   annotation_write_int (new_len);
840   annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
841   if (member_type != JV_CLASS_ATTR)
842     annotation_write_short (member_index);
843
844   index = JCF_readu2 (jcf);
845   index = handle_constant (jcf, index, CONSTANT_Class);
846   annotation_write_short (index);
847
848   index = JCF_readu2 (jcf);
849   index = handle_constant (jcf, index, CONSTANT_NameAndType);
850   annotation_write_short (index);
851 }
852
853 /* As above, for the Signature attribute.  */
854
855 static void
856 handle_signature_attribute (int member_index, JCF *jcf, 
857                            const unsigned char *name ATTRIBUTE_UNUSED, 
858                            long len, jv_attr_type member_type)
859 {
860   int new_len = len + 1;
861   uint16 index;
862   annotation_write_byte (member_type);
863   if (member_type != JV_CLASS_ATTR)
864     new_len += 2;
865   annotation_write_int (new_len);
866   annotation_write_byte (JV_SIGNATURE_KIND);
867   if (member_type != JV_CLASS_ATTR)
868     annotation_write_short (member_index);
869
870   index = JCF_readu2 (jcf);
871   index = handle_constant (jcf, index, CONSTANT_Utf8);
872   annotation_write_short (index);
873 }
874   
875 \f
876
877 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
878
879 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
880 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
881   output_class = current_class = give_name_to_class (jcf, THIS); \
882   set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
883
884 #define HANDLE_CLASS_INTERFACE(INDEX) \
885   add_interface (current_class, get_class_constant (jcf, INDEX))
886
887 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
888 { int sig_index = SIGNATURE; \
889   current_field = add_field (current_class, get_name_constant (jcf, NAME), \
890                              parse_signature (jcf, sig_index), ACCESS_FLAGS); \
891  set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
892  if ((ACCESS_FLAGS) & ACC_FINAL) \
893    MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
894 }
895
896 #define HANDLE_END_FIELDS() \
897   (current_field = NULL_TREE)
898
899 #define HANDLE_CONSTANTVALUE(INDEX) \
900 { tree constant;  int index = INDEX; \
901   if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
902     tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
903     constant = build_utf8_ref (name); \
904   } \
905   else \
906     constant = get_constant (jcf, index); \
907   set_constant_value (current_field, constant); }
908
909 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
910  (current_method = add_method (current_class, ACCESS_FLAGS, \
911                                get_name_constant (jcf, NAME), \
912                                get_name_constant (jcf, SIGNATURE)), \
913   DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
914   DECL_LINENUMBERS_OFFSET (current_method) = 0)
915
916 #define HANDLE_END_METHODS() \
917 { current_method = NULL_TREE; }
918
919 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
920 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
921   DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
922   DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
923   DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
924
925 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
926 { int n = (COUNT); \
927   DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
928   JCF_SKIP (jcf, n * 10); }
929
930 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
931 { int n = (COUNT); \
932   DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
933   JCF_SKIP (jcf, n * 4); }
934
935 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
936 { \
937   int n = COUNT; \
938   tree list = DECL_FUNCTION_THROWS (current_method); \
939   while (--n >= 0) \
940     { \
941       tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
942       list = tree_cons (NULL_TREE, thrown_class, list); \
943     } \
944   DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
945 }
946
947 #define HANDLE_DEPRECATED_ATTRIBUTE()  handle_deprecated ()
948
949 /* Link seen inner classes to their outer context and register the
950    inner class to its outer context. They will be later loaded.  */
951 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
952   handle_innerclass_attribute (COUNT, jcf, attribute_length)
953
954 #define HANDLE_SYNTHETIC_ATTRIBUTE()                                    \
955 {                                                                       \
956   /* Irrelevant decls should have been nullified by the END macros.     \
957      DECL_ARTIFICIAL on fields is used for something else (See          \
958      PUSH_FIELD in java-tree.h) */                                      \
959   if (current_method)                                                   \
960     DECL_ARTIFICIAL (current_method) = 1;                               \
961   else if (current_field)                                               \
962     FIELD_SYNTHETIC (current_field) = 1;                                \
963   else                                                                  \
964     TYPE_SYNTHETIC (current_class) = 1;                                 \
965 }
966
967 #define HANDLE_GCJCOMPILED_ATTRIBUTE()          \
968 {                                               \
969   if (current_class == object_type_node)        \
970     jcf->right_zip = 1;                         \
971 }
972
973 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE()                    \
974 {                                                                       \
975   handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
976 }
977
978 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE()  \
979 {                                                       \
980   JCF_SKIP(jcf, attribute_length);                      \
981 }
982
983 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE()           \
984 {                                                                       \
985   handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
986 }
987
988 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
989 {                                                               \
990   JCF_SKIP(jcf, attribute_length);                              \
991 }
992
993 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE()                            \
994 {                                                                       \
995   handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
996 }
997
998 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE()                              \
999 {                                                                       \
1000   handle_enclosingmethod_attribute (index, jcf, name_data,              \
1001                                     attribute_length, attr_type);       \
1002 }
1003
1004 #define HANDLE_SIGNATURE_ATTRIBUTE()                            \
1005 {                                                               \
1006   handle_signature_attribute (index, jcf, name_data,            \
1007                               attribute_length, attr_type);     \
1008 }
1009
1010 #include "jcf-reader.c"
1011
1012 tree
1013 parse_signature (JCF *jcf, int sig_index)
1014 {
1015   gcc_assert (sig_index > 0
1016               && sig_index < JPOOL_SIZE (jcf)
1017               && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1018
1019   return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1020                                  JPOOL_UTF_LENGTH (jcf, sig_index));
1021 }
1022
1023 tree
1024 get_constant (JCF *jcf, int index)
1025 {
1026   tree value;
1027   int tag;
1028   if (index <= 0 || index >= JPOOL_SIZE(jcf))
1029     goto bad;
1030   tag = JPOOL_TAG (jcf, index);
1031   if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
1032     return jcf->cpool.data[index].t;
1033   switch (tag)
1034     {
1035     case CONSTANT_Integer:
1036       {
1037         jint num = JPOOL_INT(jcf, index);
1038         value = build_int_cst (int_type_node, num);
1039         break;
1040       }
1041     case CONSTANT_Long:
1042       {
1043         unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
1044         unsigned HOST_WIDE_INT lo;
1045         HOST_WIDE_INT hi;
1046         
1047         lshift_double (num, 0, 32, 64, &lo, &hi, 0);
1048         num = JPOOL_UINT (jcf, index+1);
1049         add_double (lo, hi, num, 0, &lo, &hi);
1050         value = build_int_cst_wide_type (long_type_node, lo, hi);
1051         break;
1052       }
1053
1054     case CONSTANT_Float:
1055       {
1056         jint num = JPOOL_INT(jcf, index);
1057         long buf = num;
1058         REAL_VALUE_TYPE d;
1059
1060         real_from_target_fmt (&d, &buf, &ieee_single_format);
1061         value = build_real (float_type_node, d);
1062         break;
1063       }
1064
1065     case CONSTANT_Double:
1066       {
1067         long buf[2], lo, hi;
1068         REAL_VALUE_TYPE d;
1069
1070         hi = JPOOL_UINT (jcf, index);
1071         lo = JPOOL_UINT (jcf, index+1);
1072
1073         if (FLOAT_WORDS_BIG_ENDIAN)
1074           buf[0] = hi, buf[1] = lo;
1075         else
1076           buf[0] = lo, buf[1] = hi;
1077
1078         real_from_target_fmt (&d, buf, &ieee_double_format);
1079         value = build_real (double_type_node, d);
1080         break;
1081       }
1082
1083     case CONSTANT_String:
1084       {
1085         tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
1086         const char *utf8_ptr = IDENTIFIER_POINTER (name);
1087         int utf8_len = IDENTIFIER_LENGTH (name);
1088         const unsigned char *utf8;
1089         int i;
1090
1091         /* Check for a malformed Utf8 string.  */
1092         utf8 = (const unsigned char *) utf8_ptr;
1093         i = utf8_len;
1094         while (i > 0)
1095           {
1096             int char_len = UT8_CHAR_LENGTH (*utf8);
1097             if (char_len < 0 || char_len > 3 || char_len > i)
1098               fatal_error ("bad string constant");
1099
1100             utf8 += char_len;
1101             i -= char_len;
1102           }
1103
1104         /* Allocate a new string value.  */
1105         value = build_string (utf8_len, utf8_ptr);
1106         TREE_TYPE (value) = build_pointer_type (string_type_node);
1107       }
1108       break;
1109     default:
1110       goto bad;
1111     }
1112   JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
1113   jcf->cpool.data[index].t = value;
1114   return value;
1115  bad:
1116   internal_error ("bad value constant type %d, index %d", 
1117                   JPOOL_TAG (jcf, index), index);
1118 }
1119
1120 tree
1121 get_name_constant (JCF *jcf, int index)
1122 {
1123   tree name = get_constant (jcf, index);
1124   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1125   return name;
1126 }
1127
1128 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1129    a non anonymous entry) is found, We augment the inner class list of
1130    the outer context with the newly resolved innerclass.  */
1131
1132 static void
1133 handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
1134 {
1135   int c = count;
1136
1137   annotation_write_byte (JV_CLASS_ATTR);
1138   annotation_write_int (attribute_length+1);
1139   annotation_write_byte (JV_INNER_CLASSES_KIND);
1140   annotation_write_short (count);
1141
1142   while (c--)
1143     {
1144       /* Read inner_class_info_index. This may be 0 */
1145       int icii = JCF_readu2 (jcf);
1146       /* Read outer_class_info_index. If the innerclasses attribute
1147          entry isn't a member (like an inner class) the value is 0. */
1148       int ocii = JCF_readu2 (jcf);
1149       /* Read inner_name_index. If the class we're dealing with is
1150          an anonymous class, it must be 0. */
1151       int ini = JCF_readu2 (jcf);
1152       /* Read the access flag. */
1153       int acc = JCF_readu2 (jcf);
1154
1155       annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1156       annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1157       annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1158       annotation_write_short (acc);
1159
1160       /* If icii is 0, don't try to read the class. */
1161       if (icii >= 0)
1162         {
1163           tree klass = get_class_constant (jcf, icii);
1164           tree decl = TYPE_NAME (klass);
1165           /* Skip reading further if ocii is null */
1166           if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1167             {
1168               tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1169               tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
1170               set_class_decl_access_flags (acc, decl);
1171               DECL_CONTEXT (decl) = outer;
1172               DECL_INNER_CLASS_LIST (outer) =
1173                 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1174               CLASS_COMPLETE_P (decl) = 1;
1175             }
1176         }
1177     }
1178 }
1179
1180 static tree
1181 give_name_to_class (JCF *jcf, int i)
1182 {
1183   gcc_assert (i > 0
1184               && i < JPOOL_SIZE (jcf)
1185               && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1186
1187     {
1188       tree package_name = NULL_TREE, tmp;
1189       tree this_class;
1190       int j = JPOOL_USHORT1 (jcf, i);
1191       /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1192       tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
1193                                             JPOOL_UTF_LENGTH (jcf, j));
1194       this_class = lookup_class (class_name);
1195       {
1196       tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
1197       const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
1198       linemap_add (line_table, LC_ENTER, false, sfname, 0);
1199       input_location = linemap_line_start (line_table, 0, 1);
1200       file_start_location = input_location;
1201       DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1202       if (main_input_filename == NULL && jcf == main_jcf)
1203         main_input_filename = sfname;
1204       }
1205
1206       jcf->cpool.data[i].t = this_class;
1207       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1208       split_qualified_name (&package_name, &tmp, 
1209                             DECL_NAME (TYPE_NAME (this_class)));
1210       TYPE_PACKAGE (this_class) = package_name;
1211       return this_class;
1212     }
1213 }
1214
1215 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1216
1217 tree
1218 get_class_constant (JCF *jcf, int i)
1219 {
1220   tree type;
1221   gcc_assert (i > 0
1222               && i < JPOOL_SIZE (jcf)
1223               && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
1224
1225   if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1226     {
1227       int name_index = JPOOL_USHORT1 (jcf, i);
1228       /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1229       const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
1230       int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
1231
1232       if (name[0] == '[')  /* Handle array "classes". */
1233           type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
1234       else
1235         { 
1236           tree cname = unmangle_classname (name, nlength);
1237           type = lookup_class (cname);
1238         }
1239       jcf->cpool.data[i].t = type;
1240       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1241     }
1242   else
1243     type = jcf->cpool.data[i].t;
1244   return type;
1245 }
1246
1247 /* Read a class with the fully qualified-name NAME.
1248    Return 1 iff we read the requested file.
1249    (It is still possible we failed if the file did not
1250    define the class it is supposed to.) */
1251
1252 int
1253 read_class (tree name)
1254 {
1255   JCF this_jcf, *jcf;
1256   tree icv, klass = NULL_TREE;
1257   tree save_current_class = current_class;
1258   tree save_output_class = output_class;
1259   location_t save_location = input_location;
1260   JCF *save_current_jcf = current_jcf;
1261
1262   if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
1263     {
1264       klass = TREE_TYPE (icv);
1265       jcf = TYPE_JCF (klass);
1266     }
1267   else
1268     jcf = NULL;
1269
1270   if (jcf == NULL)
1271     {
1272       const char* path_name;
1273       this_jcf.zipd = NULL;
1274       jcf = &this_jcf;
1275       
1276       path_name = find_class (IDENTIFIER_POINTER (name),
1277                               IDENTIFIER_LENGTH (name),
1278                               &this_jcf);
1279       if (path_name == 0)
1280         return 0;
1281       else
1282         free(CONST_CAST (char *, path_name));
1283     }
1284
1285   current_jcf = jcf;
1286
1287   if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
1288     {
1289       output_class = current_class = klass;
1290       if (JCF_SEEN_IN_ZIP (current_jcf))
1291         read_zip_member(current_jcf,
1292                         current_jcf->zipd, current_jcf->zipd->zipf);
1293       jcf_parse (current_jcf);
1294       /* Parsing might change the class, in which case we have to
1295          put it back where we found it.  */
1296       if (current_class != klass && icv != NULL_TREE)
1297         TREE_TYPE (icv) = current_class;
1298       klass = current_class;
1299     }
1300   layout_class (klass);
1301   load_inner_classes (klass);
1302
1303   output_class = save_output_class;
1304   current_class = save_current_class;
1305   input_location = save_location;
1306   current_jcf = save_current_jcf;
1307   return 1;
1308 }
1309
1310 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1311    called from the parser, otherwise it's a RECORD_TYPE node. If
1312    VERBOSE is 1, print error message on failure to load a class. */
1313 void
1314 load_class (tree class_or_name, int verbose)
1315 {
1316   tree name, saved;
1317   int class_loaded = 0;
1318   tree class_decl = NULL_TREE;
1319   bool is_compiled_class = false;
1320
1321   /* We've already failed, don't try again.  */
1322   if (TREE_CODE (class_or_name) == RECORD_TYPE
1323       && TYPE_DUMMY (class_or_name))
1324     return;
1325
1326   /* class_or_name can be the name of the class we want to load */
1327   if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1328     name = class_or_name;
1329   /* In some cases, it's a dependency that we process earlier that
1330      we though */
1331   else if (TREE_CODE (class_or_name) == TREE_LIST)
1332     name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1333   /* Or it's a type in the making */
1334   else
1335     name = DECL_NAME (TYPE_NAME (class_or_name));
1336
1337   class_decl = IDENTIFIER_CLASS_VALUE (name);
1338   if (class_decl != NULL_TREE)
1339     {
1340       tree type = TREE_TYPE (class_decl);
1341       is_compiled_class
1342         = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1343            || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1344     }
1345
1346   saved = name;
1347   
1348   /* If flag_verify_invocations is unset, we don't try to load a class
1349      unless we're looking for Object (which is fixed by the ABI) or
1350      it's a class that we're going to compile.  */
1351   if (flag_verify_invocations
1352       || class_or_name == object_type_node
1353       || is_compiled_class
1354       || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1355     {
1356       while (1)
1357         {
1358           char *separator;
1359
1360           /* We've already loaded it.  */
1361           if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1362             {
1363               tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1364               if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1365                 break;
1366             }
1367         
1368           if (read_class (name))
1369             break;
1370
1371           /* We failed loading name. Now consider that we might be looking
1372              for an inner class.  */
1373           if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1374               || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
1375             {
1376               int c = *separator;
1377               *separator = '\0';
1378               name = get_identifier (IDENTIFIER_POINTER (name));
1379               *separator = c;
1380             }
1381           /* Otherwise, we failed, we bail. */
1382           else
1383             break;
1384         }
1385
1386       {
1387         /* have we found the class we're looking for?  */
1388         tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1389         tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1390         class_loaded = type && CLASS_PARSED_P (type);
1391       }       
1392     }
1393   
1394   if (!class_loaded)
1395     {
1396       if (flag_verify_invocations || ! flag_indirect_dispatch)
1397         {
1398           if (verbose)
1399             error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1400         }
1401       else if (verbose)
1402         {
1403           /* This is just a diagnostic during testing, not a real problem.  */
1404           if (!quiet_flag)
1405             warning (0, "cannot find file for class %s", 
1406                      IDENTIFIER_POINTER (saved));
1407           
1408           /* Fake it.  */
1409           if (TREE_CODE (class_or_name) == RECORD_TYPE)
1410             {
1411               set_super_info (0, class_or_name, object_type_node, 0);
1412               TYPE_DUMMY (class_or_name) = 1;
1413               /* We won't be able to output any debug info for this class.  */
1414               DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1415             }
1416         }
1417     }
1418 }
1419
1420 /* Parse the .class file JCF. */
1421
1422 static void
1423 jcf_parse (JCF* jcf)
1424 {
1425   int i, code;
1426
1427   bitmap_clear (field_offsets);
1428
1429   if (jcf_parse_preamble (jcf) != 0)
1430     fatal_error ("not a valid Java .class file");
1431   code = jcf_parse_constant_pool (jcf);
1432   if (code != 0)
1433     fatal_error ("error while parsing constant pool");
1434   code = verify_constant_pool (jcf);
1435   if (code > 0)
1436     fatal_error ("error in constant pool entry #%d\n", code);
1437
1438   jcf_parse_class (jcf);
1439   if (main_class == NULL_TREE)
1440     main_class = current_class;
1441   if (! quiet_flag && TYPE_NAME (current_class))
1442     fprintf (stderr, " %s %s",
1443              (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class", 
1444              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
1445   if (CLASS_PARSED_P (current_class))
1446     {
1447       /* FIXME - where was first time */
1448       fatal_error ("reading class %s for the second time from %s",
1449                    IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1450                    jcf->filename);
1451     }
1452   CLASS_PARSED_P (current_class) = 1;
1453
1454   for (i = 1; i < JPOOL_SIZE(jcf); i++)
1455     {
1456       switch (JPOOL_TAG (jcf, i))
1457         {
1458         case CONSTANT_Class:
1459           get_class_constant (jcf, i);
1460           break;
1461         }
1462     }
1463   
1464   code = jcf_parse_fields (jcf);
1465   if (code != 0)
1466     fatal_error ("error while parsing fields");
1467   code = jcf_parse_methods (jcf);
1468   if (code != 0)
1469     fatal_error ("error while parsing methods");
1470   code = jcf_parse_final_attributes (jcf);
1471   if (code != 0)
1472     fatal_error ("error while parsing final attributes");
1473
1474   if (TYPE_REFLECTION_DATA (current_class))
1475     annotation_write_byte (JV_DONE_ATTR);
1476
1477   linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1478
1479   /* The fields of class_type_node are already in correct order. */
1480   if (current_class != class_type_node && current_class != object_type_node)
1481     TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1482
1483   if (current_class == object_type_node)
1484     layout_class_methods (object_type_node);
1485   else
1486     all_class_list = tree_cons (NULL_TREE,
1487                                 TYPE_NAME (current_class), all_class_list );
1488 }
1489
1490 /* If we came across inner classes, load them now. */
1491 static void
1492 load_inner_classes (tree cur_class)
1493 {
1494   tree current;
1495   for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
1496        current = TREE_CHAIN (current))
1497     {
1498       tree name = DECL_NAME (TREE_PURPOSE (current));
1499       tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1500       if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1501           && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
1502         load_class (name, 1);
1503     }
1504 }
1505
1506 static void
1507 duplicate_class_warning (const char *filename)
1508 {
1509   location_t warn_loc;
1510   linemap_add (line_table, LC_RENAME, 0, filename, 0);
1511   warn_loc = linemap_line_start (line_table, 0, 1);
1512   warning (0, "%Hduplicate class will only be compiled once", &warn_loc);
1513 }
1514
1515 static void
1516 java_layout_seen_class_methods (void)
1517 {
1518   tree previous_list = all_class_list;
1519   tree end = NULL_TREE;
1520   tree current;
1521
1522   while (1)
1523     {
1524       for (current = previous_list;
1525            current != end; current = TREE_CHAIN (current))
1526         {
1527           tree decl = TREE_VALUE (current);
1528           tree cls = TREE_TYPE (decl);
1529
1530           input_location = DECL_SOURCE_LOCATION (decl);
1531
1532           if (! CLASS_LOADED_P (cls))
1533             load_class (cls, 0);
1534
1535           layout_class_methods (cls);
1536         }
1537
1538       /* Note that new classes might have been added while laying out
1539          methods, changing the value of all_class_list.  */
1540
1541       if (previous_list != all_class_list)
1542         {
1543           end = previous_list;
1544           previous_list = all_class_list;
1545         }
1546       else
1547         break;
1548     }
1549 }
1550
1551 static void
1552 parse_class_file (void)
1553 {
1554   tree method;
1555   location_t save_location = input_location;
1556
1557   java_layout_seen_class_methods ();
1558
1559   input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1560   {
1561     /* Re-enter the current file.  */
1562     expanded_location loc = expand_location (input_location);
1563     linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1564   }
1565   file_start_location = input_location;
1566   (*debug_hooks->start_source_file) (input_line, input_filename);
1567
1568   java_mark_class_local (current_class);
1569
1570   gen_indirect_dispatch_tables (current_class);
1571
1572   for (method = TYPE_METHODS (current_class);
1573        method != NULL_TREE; method = TREE_CHAIN (method))
1574     {
1575       JCF *jcf = current_jcf;
1576
1577       if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
1578         continue;
1579
1580       if (METHOD_NATIVE (method))
1581         {
1582           tree arg;
1583           int  decl_max_locals;
1584
1585           if (! flag_jni)
1586             continue;
1587           /* We need to compute the DECL_MAX_LOCALS. We need to take
1588              the wide types into account too. */
1589           for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0; 
1590                arg != end_params_node;
1591                arg = TREE_CHAIN (arg), decl_max_locals += 1)
1592             {
1593               if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1594                 decl_max_locals += 1;
1595             }
1596           DECL_MAX_LOCALS (method) = decl_max_locals;
1597           start_java_method (method);
1598           give_name_to_locals (jcf);
1599           *get_stmts () = build_jni_stub (method);
1600           end_java_method ();
1601           continue;
1602         }
1603
1604       if (DECL_CODE_OFFSET (method) == 0)
1605         {
1606           current_function_decl = method;
1607           error ("missing Code attribute");
1608           continue;
1609         }
1610
1611       input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1612       if (DECL_LINENUMBERS_OFFSET (method))
1613         {
1614           int i;
1615           int min_line = 0;
1616           unsigned char *ptr;
1617           JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1618           linenumber_count = i = JCF_readu2 (jcf);
1619           linenumber_table = ptr = jcf->read_ptr;
1620
1621           for (ptr += 2; --i >= 0; ptr += 4)
1622             {
1623               int line = GET_u2 (ptr);
1624               /* Set initial input_line to smallest linenumber.
1625                * Needs to be set before init_function_start. */
1626               if (min_line == 0 || line < min_line)
1627                 min_line = line;
1628             }
1629           if (min_line != 0)
1630             input_location = linemap_line_start (line_table, min_line, 1);
1631         }
1632       else
1633         {
1634           linenumber_table = NULL;
1635           linenumber_count = 0;
1636         }
1637
1638       start_java_method (method);
1639
1640       note_instructions (jcf, method);
1641
1642       give_name_to_locals (jcf);
1643
1644       /* Bump up start_label_pc_this_method so we get a unique label number
1645          and reset highest_label_pc_this_method. */
1646       if (highest_label_pc_this_method >= 0)
1647         {
1648           /* We adjust to the next multiple of 1000.  This is just a frill
1649              so the last 3 digits of the label number match the bytecode
1650              offset, which might make debugging marginally more convenient. */
1651           start_label_pc_this_method
1652             = ((((start_label_pc_this_method + highest_label_pc_this_method)
1653                  / 1000)
1654                 + 1)
1655                * 1000);
1656           highest_label_pc_this_method = -1;
1657         }
1658
1659       /* Convert bytecode to trees.  */
1660       expand_byte_code (jcf, method);
1661
1662       end_java_method ();
1663     }
1664
1665   finish_class ();
1666
1667   (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
1668   input_location = save_location;
1669 }
1670
1671 void
1672 add_predefined_file (tree name)
1673 {
1674   predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
1675 }
1676
1677 int
1678 predefined_filename_p (tree node)
1679 {
1680   tree iter;
1681
1682   for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
1683     {
1684       if (TREE_VALUE (iter) == node)
1685         return 1;
1686     }
1687   return 0;
1688 }
1689
1690 /* Generate a function that does all static initialization for this 
1691    translation unit.  */
1692
1693 static void
1694 java_emit_static_constructor (void)
1695 {
1696   tree body = NULL;
1697
1698   emit_register_classes (&body);
1699   write_resource_constructor (&body);
1700
1701   if (body)
1702     {
1703       tree name = get_identifier ("_Jv_global_static_constructor");
1704
1705       tree decl 
1706         = build_decl (FUNCTION_DECL, name,
1707                       build_function_type (void_type_node, void_list_node));
1708
1709       tree resdecl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
1710       DECL_ARTIFICIAL (resdecl) = 1;
1711       DECL_RESULT (decl) = resdecl;
1712       current_function_decl = decl;
1713       allocate_struct_function (decl, false);
1714
1715       TREE_STATIC (decl) = 1;
1716       TREE_USED (decl) = 1;
1717       DECL_ARTIFICIAL (decl) = 1;
1718       DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1719       DECL_SAVED_TREE (decl) = body;
1720       DECL_UNINLINABLE (decl) = 1;
1721
1722       DECL_INITIAL (decl) = make_node (BLOCK);
1723       TREE_USED (DECL_INITIAL (decl)) = 1;
1724
1725       DECL_STATIC_CONSTRUCTOR (decl) = 1;
1726       java_genericize (decl);
1727       cgraph_finalize_function (decl, false);
1728     }
1729 }
1730
1731
1732 void
1733 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
1734 {
1735   int filename_count = 0;
1736   location_t save_location = input_location;
1737   char *file_list = NULL, *list, *next;
1738   tree node;
1739   FILE *finput = NULL;
1740   int in_quotes = 0;
1741  
1742   bitmap_obstack_initialize (&bit_obstack);
1743   field_offsets = BITMAP_ALLOC (&bit_obstack);
1744
1745   if (flag_filelist_file)
1746     {
1747       int avail = 2000;
1748       finput = fopen (main_input_filename, "r");
1749       if (finput == NULL)
1750         fatal_error ("can't open %s: %m", input_filename);
1751       list = XNEWVEC (char, avail);
1752       next = list;
1753       for (;;)
1754         {
1755           int count;
1756           if (avail < 500)
1757             {
1758               count = next - list;
1759               avail = 2 * (count + avail);
1760               list = XRESIZEVEC (char, list, avail);
1761               next = list + count;
1762               avail = avail - count;
1763             }
1764           /* Subtract to to guarantee space for final '\0'. */
1765           count = fread (next, 1, avail - 1, finput);
1766           if (count == 0)
1767             {
1768               if (! feof (finput))
1769                 fatal_error ("error closing %s: %m", input_filename);
1770               *next = '\0';
1771               break;
1772             }
1773           avail -= count;
1774           next += count;
1775         }
1776       fclose (finput);
1777       finput = NULL;
1778       file_list = list;
1779     }
1780   else
1781     list = CONST_CAST (char *, main_input_filename);
1782
1783   while (list)
1784     {
1785       for (next = list; ; )
1786         {
1787           char ch = *next;
1788           if (flag_filelist_file && ! in_quotes
1789               && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1790                   || ch == '&') /* FIXME */)
1791             {
1792               if (next == list)
1793                 {
1794                   next++;
1795                   list = next;
1796                   continue;
1797                 }
1798               else
1799                 {
1800                   *next++ = '\0';
1801                   break;
1802                 }
1803             }
1804           if (flag_filelist_file && ch == '"')
1805             {
1806               in_quotes = ! in_quotes;
1807               *next++ = '\0';
1808               if (in_quotes) 
1809                 list = next;
1810               else 
1811                 break;
1812             }
1813           if (ch == '\0')
1814             {
1815               next = NULL;
1816               break;
1817             }
1818           next++;
1819         }
1820
1821       /* Exclude .java files.  */
1822       if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1823         {
1824           /* Nothing. */
1825         }
1826       else if (list[0]) 
1827         {
1828           node = get_identifier (list);
1829
1830           filename_count++;
1831
1832           /* Exclude file that we see twice on the command line. */
1833              
1834           if (IS_A_COMMAND_LINE_FILENAME_P (node))
1835             duplicate_class_warning (IDENTIFIER_POINTER (node));
1836           else
1837             {
1838               tree file_decl = build_decl (TRANSLATION_UNIT_DECL, node, NULL);
1839               TREE_CHAIN (file_decl) = current_file_list;
1840               current_file_list = file_decl;
1841               IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1842             }
1843         }
1844       list = next;
1845     }
1846
1847   if (file_list != NULL)
1848     free (file_list);
1849
1850   if (filename_count == 0)
1851     warning (0, "no input file specified");
1852
1853   if (resource_name)
1854     {
1855       const char *resource_filename;
1856       
1857       /* Only one resource file may be compiled at a time.  */
1858       assert (TREE_CHAIN (current_file_list) == NULL);
1859
1860       resource_filename = IDENTIFIER_POINTER (DECL_NAME (current_file_list));
1861       compile_resource_file (resource_name, resource_filename);
1862
1863       goto finish;
1864     }
1865
1866   current_jcf = main_jcf;
1867   current_file_list = nreverse (current_file_list);
1868   for (node = current_file_list; node; node = TREE_CHAIN (node))
1869     {
1870       unsigned char magic_string[4];
1871       char *real_path;
1872       uint32 magic = 0;
1873       tree name = DECL_NAME (node);
1874       tree real_file;
1875       const char *filename = IDENTIFIER_POINTER (name);
1876
1877       /* Skip already parsed files */
1878       real_path = lrealpath (filename);
1879       real_file = get_identifier (real_path);
1880       free (real_path);
1881       if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1882         continue;
1883
1884       /* Close previous descriptor, if any */
1885       if (finput && fclose (finput))
1886         fatal_error ("can't close input file %s: %m", main_input_filename);
1887       
1888       finput = fopen (filename, "rb");
1889       if (finput == NULL)
1890         fatal_error ("can't open %s: %m", filename);
1891
1892 #ifdef IO_BUFFER_SIZE
1893       setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1894                _IOFBF, IO_BUFFER_SIZE);
1895 #endif
1896
1897       /* Figure what kind of file we're dealing with */
1898       if (fread (magic_string, 1, 4, finput) == 4)
1899         {
1900           fseek (finput, 0L, SEEK_SET);
1901           magic = GET_u4 (magic_string);
1902         }
1903       if (magic == 0xcafebabe)
1904         {
1905           CLASS_FILE_P (node) = 1;
1906           current_jcf = GGC_NEW (JCF);
1907           JCF_ZERO (current_jcf);
1908           current_jcf->read_state = finput;
1909           current_jcf->filbuf = jcf_filbuf_from_stdio;
1910           jcf_parse (current_jcf);
1911           DECL_SOURCE_LOCATION (node) = file_start_location;
1912           TYPE_JCF (current_class) = current_jcf;
1913           if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1914             {
1915               /* We've already compiled this class.  */
1916               duplicate_class_warning (filename);
1917               continue;
1918             }
1919           CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1920           TREE_TYPE (node) = current_class;
1921         }
1922       else if (magic == (JCF_u4)ZIPMAGIC)
1923         {
1924           main_jcf = GGC_NEW (JCF);
1925           JCF_ZERO (main_jcf);
1926           main_jcf->read_state = finput;
1927           main_jcf->filbuf = jcf_filbuf_from_stdio;
1928           linemap_add (line_table, LC_ENTER, false, filename, 0);
1929           input_location = linemap_line_start (line_table, 0, 1);
1930           if (open_in_zip (main_jcf, filename, NULL, 0) <  0)
1931             fatal_error ("bad zip/jar file %s", filename);
1932           localToFile = SeenZipFiles;
1933           /* Register all the classes defined there.  */
1934           process_zip_dir ((FILE *) main_jcf->read_state);
1935           linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1936           parse_zip_file_entries ();
1937         }
1938       else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1939         {
1940           /* Ignore an empty input jar.  */
1941         }
1942       else
1943         {
1944           gcc_unreachable ();
1945 #if 0
1946           java_push_parser_context ();
1947           java_parser_context_save_global ();
1948
1949           parse_source_file_1 (real_file, filename, finput);
1950           java_parser_context_restore_global ();
1951           java_pop_parser_context (1);
1952           linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1953 #endif
1954         }
1955     }
1956
1957   for (node = current_file_list; node; node = TREE_CHAIN (node))
1958     {
1959       input_location = DECL_SOURCE_LOCATION (node);
1960       if (CLASS_FILE_P (node))
1961         {
1962           /* FIXME: These two flags really should be independent.  We
1963              should be able to compile fully binary compatible, but
1964              with flag_verify_invocations on.  */
1965           flag_verify_invocations = ! flag_indirect_dispatch;
1966           output_class = current_class = TREE_TYPE (node);
1967
1968           current_jcf = TYPE_JCF (current_class);
1969           layout_class (current_class);
1970           load_inner_classes (current_class);
1971           parse_class_file ();
1972           JCF_FINISH (current_jcf);
1973         }
1974     }
1975   input_location = save_location;
1976
1977   bitmap_obstack_release (&bit_obstack);
1978
1979  finish:
1980   /* Arrange for any necessary initialization to happen.  */
1981   java_emit_static_constructor ();
1982   gcc_assert (global_bindings_p ());
1983
1984   /* Only finalize the compilation unit after we've told cgraph which
1985      functions have their addresses stored.  */
1986   cgraph_finalize_compilation_unit ();
1987   cgraph_optimize ();
1988 }
1989
1990
1991 /* Return the name of the class corresponding to the name of the file
1992    in this zip entry.  The result is newly allocated using ALLOC.  */
1993 static char *
1994 compute_class_name (struct ZipDirectory *zdir)
1995 {
1996   char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1997   char *class_name;
1998   int i;
1999   int filename_length = zdir->filename_length;
2000
2001   while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
2002     {
2003       class_name_in_zip_dir += 2;
2004       filename_length -= 2;
2005     }
2006
2007   filename_length -= strlen (".class");
2008   class_name = XNEWVEC (char, filename_length + 1);
2009   memcpy (class_name, class_name_in_zip_dir, filename_length);
2010   class_name [filename_length] = '\0';
2011
2012   for (i = 0; i < filename_length; i++)
2013     if (class_name[i] == '/')
2014       class_name[i] = '.';
2015
2016   return class_name;
2017 }
2018
2019 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2020    if it is a property file of some sort.  */
2021 static int
2022 classify_zip_file (struct ZipDirectory *zdir)
2023 {
2024   char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2025
2026   if (zdir->filename_length > 6
2027       && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2028                    ".class", 6))
2029     return 1;
2030
2031   /* For now we drop the manifest, but not other information.  */
2032   if (zdir->filename_length == 20
2033       && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
2034     return 0;
2035
2036   /* Drop directory entries.  */
2037   if (zdir->filename_length > 0
2038       && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2039     return 0;
2040
2041   return 2;
2042 }
2043
2044 /* Process all class entries found in the zip file.  */
2045 static void
2046 parse_zip_file_entries (void)
2047 {
2048   struct ZipDirectory *zdir;
2049   int i;
2050
2051   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2052        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2053     {
2054       tree klass;
2055
2056       switch (classify_zip_file (zdir))
2057         {
2058         case 0:
2059           continue;
2060
2061         case 1:
2062           {
2063             char *class_name = compute_class_name (zdir);
2064             int previous_alias_set = -1;
2065             klass = lookup_class (get_identifier (class_name));
2066             FREE (class_name);
2067             current_jcf = TYPE_JCF (klass);
2068             output_class = current_class = klass;
2069
2070             /* This is a dummy class, and now we're compiling it for
2071                real.  */
2072             gcc_assert (! TYPE_DUMMY (klass));
2073
2074             /* This is for a corner case where we have a superclass
2075                but no superclass fields.
2076
2077                This can happen if we earlier failed to lay out this
2078                class because its superclass was still in the process
2079                of being laid out; this occurs when we have recursive
2080                class dependencies via inner classes.  We must record
2081                the previous alias set and restore it after laying out
2082                the class.
2083
2084                FIXME: this really is a kludge.  We should figure out a
2085                way to lay out the class properly before this
2086                happens.  */
2087             if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2088                 && integer_zerop (TYPE_SIZE (klass)))
2089               {
2090                 TYPE_SIZE (klass) = NULL_TREE;
2091                 previous_alias_set = TYPE_ALIAS_SET (klass);
2092                 TYPE_ALIAS_SET (klass) = -1;
2093               }
2094
2095             if (! CLASS_LOADED_P (klass))
2096               {
2097                 if (! CLASS_PARSED_P (klass))
2098                   {
2099                     read_zip_member (current_jcf, zdir, localToFile);
2100                     jcf_parse (current_jcf);
2101                   }
2102                 layout_class (current_class);
2103                 load_inner_classes (current_class);
2104               }
2105
2106             if (previous_alias_set != -1)
2107               TYPE_ALIAS_SET (klass) = previous_alias_set;
2108
2109             if (TYPE_SIZE (current_class) != error_mark_node)
2110               {
2111                 parse_class_file ();
2112                 free (current_jcf->buffer); /* No longer necessary */
2113                 /* Note: there is a way to free this buffer right after a
2114                    class seen in a zip file has been parsed. The idea is the
2115                    set its jcf in such a way that buffer will be reallocated
2116                    the time the code for the class will be generated. FIXME. */
2117               }
2118           }
2119           break;
2120
2121         case 2:
2122           {
2123             char *file_name, *class_name_in_zip_dir, *buffer;
2124             JCF *jcf;
2125             file_name = XNEWVEC (char, zdir->filename_length + 1);
2126             class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2127             strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2128             file_name[zdir->filename_length] = '\0';
2129             jcf = XNEW (JCF);
2130             JCF_ZERO (jcf);
2131             jcf->read_state  = finput;
2132             jcf->filbuf      = jcf_filbuf_from_stdio;
2133             jcf->classname   = NULL;
2134             jcf->filename    = file_name;
2135             jcf->zipd        = zdir;
2136
2137             if (read_zip_member (jcf, zdir, localToFile) < 0)
2138               fatal_error ("error while reading %s from zip file", file_name);
2139
2140             buffer = XNEWVEC (char, zdir->filename_length + 1 +
2141                             (jcf->buffer_end - jcf->buffer));
2142             strcpy (buffer, file_name);
2143             /* This is not a typo: we overwrite the trailing \0 of the
2144                file name; this is just how the data is laid out.  */
2145             memcpy (buffer + zdir->filename_length,
2146                     jcf->buffer, jcf->buffer_end - jcf->buffer);
2147
2148             compile_resource_data (file_name, buffer,
2149                                    jcf->buffer_end - jcf->buffer);
2150             JCF_FINISH (jcf);
2151             free (jcf);
2152             free (buffer);
2153           }
2154           break;
2155
2156         default:
2157           gcc_unreachable ();
2158         }
2159     }
2160 }
2161
2162 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
2163    jcf up for further processing and link it to the created class.  */
2164
2165 static void
2166 process_zip_dir (FILE *finput)
2167 {
2168   int i;
2169   ZipDirectory *zdir;
2170
2171   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2172        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2173     {
2174       char *class_name, *file_name, *class_name_in_zip_dir;
2175       tree klass;
2176       JCF  *jcf;
2177
2178       class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2179
2180       /* Here we skip non-class files; we handle them later.  */
2181       if (classify_zip_file (zdir) != 1)
2182         continue;
2183
2184       class_name = compute_class_name (zdir);
2185       file_name  = XNEWVEC (char, zdir->filename_length+1);
2186       jcf = GGC_NEW (JCF);
2187       JCF_ZERO (jcf);
2188
2189       strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2190       file_name [zdir->filename_length] = '\0';
2191
2192       klass = lookup_class (get_identifier (class_name));
2193
2194       if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2195         {
2196           /* We've already compiled this class.  */
2197           duplicate_class_warning (file_name);
2198           continue;
2199         }
2200       /* This function is only called when processing a zip file seen
2201          on the command line.  */
2202       CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
2203
2204       jcf->read_state  = finput;
2205       jcf->filbuf      = jcf_filbuf_from_stdio;
2206       jcf->classname   = class_name;
2207       jcf->filename    = file_name;
2208       jcf->zipd        = zdir;
2209
2210       TYPE_JCF (klass) = jcf;
2211     }
2212 }
2213
2214 #include "gt-java-jcf-parse.h"
2215 #include "gtype-java.h"