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