OSDN Git Service

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