2010-07-27 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / gcc / java / jcf-parse.c
blobc27d4b553d8fc6c22fdd913574aeb350c51a0b43
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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/>.
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. */
25 /* Written by Per Bothner <bothner@cygnus.com> */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tree.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 "toplev.h"
39 #include "parse.h"
40 #include "ggc.h"
41 #include "debug.h"
42 #include "assert.h"
43 #include "cgraph.h"
44 #include "vecprim.h"
45 #include "bitmap.h"
47 #ifdef HAVE_LOCALE_H
48 #include <locale.h>
49 #endif
51 #ifdef HAVE_LANGINFO_CODESET
52 #include <langinfo.h>
53 #endif
55 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
56 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
57 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
58 #define JPOOL_UTF_DATA(JCF, INDEX) \
59 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
60 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
61 do { \
62 unsigned char save; unsigned char *text; \
63 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
64 text = (JCF)->read_ptr; \
65 save = text[LENGTH]; \
66 text[LENGTH] = 0; \
67 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
68 text[LENGTH] = save; \
69 JCF_SKIP (JCF, LENGTH); } while (0)
71 #include "jcf.h"
73 extern struct obstack temporary_obstack;
75 static GTY(()) tree parse_roots[2];
77 /* The FIELD_DECL for the current field. */
78 #define current_field parse_roots[0]
80 /* The METHOD_DECL for the current method. */
81 #define current_method parse_roots[1]
83 /* A list of TRANSLATION_UNIT_DECLs for the files to be compiled. */
84 static GTY(()) VEC(tree,gc) *current_file_list;
86 /* Line 0 in current file, if compiling from bytecode. */
87 static location_t file_start_location;
89 /* The Java archive that provides main_class; the main input file. */
90 static GTY(()) struct JCF * main_jcf;
92 /* A list of all the class DECLs seen so far. */
93 static GTY(()) VEC(tree,gc) *all_class_list;
95 /* The number of source files passed to us by -fsource-filename and an
96 array of pointers to each name. Used by find_sourcefile(). */
97 static int num_files = 0;
98 static char **filenames;
100 static struct ZipFile *localToFile;
102 /* A map of byte offsets in the reflection data that are fields which
103 need renumbering. */
104 bitmap field_offsets;
105 bitmap_obstack bit_obstack;
107 /* Declarations of some functions used here. */
108 static void handle_innerclass_attribute (int count, JCF *, int len);
109 static tree give_name_to_class (JCF *jcf, int index);
110 static char *compute_class_name (struct ZipDirectory *zdir);
111 static int classify_zip_file (struct ZipDirectory *zdir);
112 static void parse_zip_file_entries (void);
113 static void process_zip_dir (FILE *);
114 static void parse_class_file (void);
115 static void handle_deprecated (void);
116 static void set_source_filename (JCF *, int);
117 static void jcf_parse (struct JCF*);
118 static void load_inner_classes (tree);
119 static void handle_annotation (JCF *jcf, int level);
120 static void java_layout_seen_class_methods (void);
122 /* Handle "Deprecated" attribute. */
123 static void
124 handle_deprecated (void)
126 if (current_field != NULL_TREE)
127 FIELD_DEPRECATED (current_field) = 1;
128 else if (current_method != NULL_TREE)
129 METHOD_DEPRECATED (current_method) = 1;
130 else if (current_class != NULL_TREE)
131 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
132 else
134 /* Shouldn't happen. */
135 gcc_unreachable ();
141 /* Reverse a string. */
142 static char *
143 reverse (const char *s)
145 if (s == NULL)
146 return NULL;
147 else
149 int len = strlen (s);
150 char *d = XNEWVAR (char, len + 1);
151 const char *sp;
152 char *dp;
154 d[len] = 0;
155 for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
156 *dp = *sp;
158 return d;
162 /* Compare two strings for qsort(). */
163 static int
164 cmpstringp (const void *p1, const void *p2)
166 /* The arguments to this function are "pointers to
167 pointers to char", but strcmp() arguments are "pointers
168 to char", hence the following cast plus dereference */
170 return strcmp(*(const char *const*) p1, *(const char *const*) p2);
173 /* Create an array of strings, one for each source file that we've
174 seen. fsource_filename can either be the name of a single .java
175 file or a file that contains a list of filenames separated by
176 newlines. */
177 void
178 java_read_sourcefilenames (const char *fsource_filename)
180 if (fsource_filename
181 && filenames == 0
182 && strlen (fsource_filename) > strlen (".java")
183 && strcmp ((fsource_filename
184 + strlen (fsource_filename)
185 - strlen (".java")),
186 ".java") != 0)
188 /* fsource_filename isn't a .java file but a list of filenames
189 separated by newlines */
190 FILE *finput = fopen (fsource_filename, "r");
191 int len = 0;
192 int longest_line = 0;
194 gcc_assert (finput);
196 /* Find out how many files there are, and how long the filenames are. */
197 while (! feof (finput))
199 int ch = getc (finput);
200 if (ch == '\n')
202 num_files++;
203 if (len > longest_line)
204 longest_line = len;
205 len = 0;
206 continue;
208 if (ch == EOF)
209 break;
210 len++;
213 rewind (finput);
215 /* Read the filenames. Put a pointer to each filename into the
216 array FILENAMES. */
218 char *linebuf = (char *) alloca (longest_line + 1);
219 int i = 0;
220 int charpos;
222 filenames = XNEWVEC (char *, num_files);
224 charpos = 0;
225 for (;;)
227 int ch = getc (finput);
228 if (ch == EOF)
229 break;
230 if (ch == '\n')
232 linebuf[charpos] = 0;
233 gcc_assert (i < num_files);
234 /* ??? Perhaps we should use lrealpath() here. Doing
235 so would tidy up things like /../ but the rest of
236 gcc seems to assume relative pathnames, not
237 absolute pathnames. */
238 /* realname = lrealpath (linebuf); */
239 filenames[i++] = reverse (linebuf);
240 charpos = 0;
241 continue;
243 gcc_assert (charpos < longest_line);
244 linebuf[charpos++] = ch;
247 if (num_files > 1)
248 qsort (filenames, num_files, sizeof (char *), cmpstringp);
250 fclose (finput);
252 else
254 filenames = XNEWVEC (char *, 1);
255 filenames[0] = reverse (fsource_filename);
256 num_files = 1;
260 /* Given a relative pathname such as foo/bar.java, attempt to find a
261 longer pathname with the same suffix.
263 This is a best guess heuristic; with some weird class hierarchies we
264 may fail to pick the correct source file. For example, if we have
265 the filenames foo/bar.java and also foo/foo/bar.java, we do not
266 have enough information to know which one is the right match for
267 foo/bar.java. */
269 static const char *
270 find_sourcefile (const char *name)
272 int i = 0, j = num_files-1;
273 char *found = NULL;
275 if (filenames)
277 char *revname = reverse (name);
281 int k = (i+j) / 2;
282 int cmp = strncmp (revname, filenames[k], strlen (revname));
283 if (cmp == 0)
285 /* OK, so we found one. But is it a unique match? */
286 if ((k > i
287 && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
288 || (k < j
289 && (strncmp (revname, filenames[k+1], strlen (revname))
290 == 0)))
292 else
293 found = filenames[k];
294 break;
296 if (cmp > 0)
297 i = k+1;
298 else
299 j = k-1;
301 while (i <= j);
303 free (revname);
306 if (found && strlen (found) > strlen (name))
307 return reverse (found);
308 else
309 return name;
314 /* Handle "SourceFile" attribute. */
316 static void
317 set_source_filename (JCF *jcf, int index)
319 tree sfname_id = get_name_constant (jcf, index);
320 const char *sfname = IDENTIFIER_POINTER (sfname_id);
321 const char *old_filename = input_filename;
322 int new_len = IDENTIFIER_LENGTH (sfname_id);
323 if (old_filename != NULL)
325 int old_len = strlen (old_filename);
326 /* Use the current input_filename (derived from the class name)
327 if it has a directory prefix, but otherwise matches sfname. */
328 if (old_len > new_len
329 && strcmp (sfname, old_filename + old_len - new_len) == 0
330 && (old_filename[old_len - new_len - 1] == '/'
331 || old_filename[old_len - new_len - 1] == '\\'))
332 return;
334 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
336 const char *class_name
337 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
338 const char *dot = strrchr (class_name, '.');
339 if (dot != NULL)
341 /* Length of prefix, not counting final dot. */
342 int i = dot - class_name;
343 /* Concatenate current package prefix with new sfname. */
344 char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
345 strcpy (buf + i + 1, sfname);
346 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
347 Note we start at the end with the final package dot. */
348 for (; i >= 0; i--)
350 char c = class_name[i];
351 if (c == '.')
352 c = DIR_SEPARATOR;
353 buf[i] = c;
355 sfname_id = get_identifier (buf);
356 free (buf);
357 sfname = IDENTIFIER_POINTER (sfname_id);
361 sfname = find_sourcefile (sfname);
362 line_table->maps[line_table->used-1].to_file = sfname;
363 if (current_class == main_class) main_input_filename = sfname;
369 /* Annotation handling.
371 The technique we use here is to copy the annotation data directly
372 from the input class file into the output file. We don't decode the
373 data at all, merely rewriting constant indexes whenever we come
374 across them: this is necessary because the constant pool in the
375 output file isn't the same as the constant pool in in the input.
377 The main advantage of this technique is that the resulting
378 annotation data is pointer-free, so it doesn't have to be relocated
379 at startup time. As a consequence of this, annotations have no
380 performance impact unless they are used. Also, this representation
381 is very dense. */
384 /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
385 the start of the newly allocated region. */
387 static unsigned char*
388 annotation_grow (int delta)
390 unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
391 long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
392 long len = *datasize;
394 if (*data == NULL)
396 *data = XNEWVAR (unsigned char, delta);
398 else
400 int newlen = *datasize + delta;
401 if (floor_log2 (newlen) != floor_log2 (*datasize))
402 *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
404 *datasize += delta;
405 return *data + len;
408 /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
409 byte order (i.e. big endian.) */
411 static void
412 annotation_rewrite_byte (unsigned int n, unsigned char *p)
414 p[0] = n;
417 static void
418 annotation_rewrite_short (unsigned int n, unsigned char *p)
420 p[0] = n>>8;
421 p[1] = n;
424 static void
425 annotation_rewrite_int (unsigned int n, unsigned char *p)
427 p[0] = n>>24;
428 p[1] = n>>16;
429 p[2] = n>>8;
430 p[3] = n;
433 /* Read a 16-bit unsigned int in Java byte order (i.e. big
434 endian.) */
436 static uint16
437 annotation_read_short (unsigned char *p)
439 uint16 tmp = p[0];
440 tmp = (tmp << 8) | p[1];
441 return tmp;
444 /* annotation_write_TYPE. Rewrite various int types, appending them
445 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
446 endian.) */
448 static void
449 annotation_write_byte (unsigned int n)
451 annotation_rewrite_byte (n, annotation_grow (1));
454 static void
455 annotation_write_short (unsigned int n)
457 annotation_rewrite_short (n, annotation_grow (2));
460 static void
461 annotation_write_int (unsigned int n)
463 annotation_rewrite_int (n, annotation_grow (4));
466 /* Create a 64-bit constant in the constant pool.
468 This is used for both integer and floating-point types. As a
469 consequence, it will not work if the target floating-point format
470 is anything other than IEEE-754. While this is arguably a bug, the
471 runtime library makes exactly the same assumption and it's unlikely
472 that Java will ever run on a non-IEEE machine. */
474 static int
475 handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
476 int index, bool big_endian)
478 /* If we're on a 64-bit platform we can fit a long or double
479 into the same space as a jword. */
480 if (POINTER_SIZE >= 64)
481 index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
483 /* In a compiled program the constant pool is in native word
484 order. How weird is that??? */
485 else if (big_endian)
486 index = find_constant2 (cpool, kind,
487 JPOOL_INT (jcf, index),
488 JPOOL_INT (jcf, index+1));
489 else
490 index = find_constant2 (cpool, kind,
491 JPOOL_INT (jcf, index+1),
492 JPOOL_INT (jcf, index));
494 return index;
497 /* Given a class file and an index into its constant pool, create an
498 entry in the outgoing constant pool for the same item. */
500 static uint16
501 handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
503 unsigned int kind;
504 CPool *cpool = cpool_for_class (output_class);
506 if (index == 0)
507 return 0;
509 if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
510 error ("<constant pool index %d not in range>", index);
512 kind = JPOOL_TAG (jcf, index);
514 if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
516 if (purpose == CONSTANT_Class
517 && kind == CONSTANT_Utf8)
519 else
520 error ("<constant pool index %d unexpected type", index);
523 switch (kind)
525 case CONSTANT_Class:
526 case CONSTANT_ResolvedClass:
528 /* For some reason I know not the what of, class names in
529 annotations are UTF-8 strings in the constant pool but
530 class names in EnclosingMethod attributes are real class
531 references. Set CONSTANT_LazyFlag here so that the VM
532 doesn't attempt to resolve them at class initialization
533 time. */
534 tree resolved_class, class_name;
535 resolved_class = get_class_constant (jcf, index);
536 class_name = build_internal_class_name (resolved_class);
537 index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
538 (unmangle_classname
539 (IDENTIFIER_POINTER(class_name),
540 IDENTIFIER_LENGTH(class_name))));
541 break;
543 case CONSTANT_Utf8:
545 tree utf8 = get_constant (jcf, index);
546 if (purpose == CONSTANT_Class)
547 /* Create a constant pool entry for a type signature. This
548 one has '.' rather than '/' because it isn't going into a
549 class file, it's going into a compiled object.
551 This has to match the logic in
552 _Jv_ClassReader::prepare_pool_entry(). */
553 utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
554 IDENTIFIER_LENGTH(utf8));
555 index = alloc_name_constant (kind, utf8);
557 break;
559 case CONSTANT_Long:
560 index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
561 WORDS_BIG_ENDIAN);
562 break;
564 case CONSTANT_Double:
565 index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
566 FLOAT_WORDS_BIG_ENDIAN);
567 break;
569 case CONSTANT_Float:
570 case CONSTANT_Integer:
571 index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
572 break;
574 case CONSTANT_NameAndType:
576 uint16 name = JPOOL_USHORT1 (jcf, index);
577 uint16 sig = JPOOL_USHORT2 (jcf, index);
578 uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
579 uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
580 jword new_index = (name_index << 16) | sig_index;
581 index = find_constant1 (cpool, kind, new_index);
583 break;
585 default:
586 abort ();
589 return index;
592 /* Read an element_value structure from an annotation in JCF. Return
593 the constant pool index for the resulting constant pool entry. */
595 static int
596 handle_element_value (JCF *jcf, int level)
598 uint8 tag = JCF_readu (jcf);
599 int index = 0;
601 annotation_write_byte (tag);
602 switch (tag)
604 case 'B':
605 case 'C':
606 case 'S':
607 case 'Z':
608 case 'I':
610 uint16 cindex = JCF_readu2 (jcf);
611 index = handle_constant (jcf, cindex,
612 CONSTANT_Integer);
613 annotation_write_short (index);
615 break;
616 case 'D':
618 uint16 cindex = JCF_readu2 (jcf);
619 index = handle_constant (jcf, cindex,
620 CONSTANT_Double);
621 annotation_write_short (index);
623 break;
624 case 'F':
626 uint16 cindex = JCF_readu2 (jcf);
627 index = handle_constant (jcf, cindex,
628 CONSTANT_Float);
629 annotation_write_short (index);
631 break;
632 case 'J':
634 uint16 cindex = JCF_readu2 (jcf);
635 index = handle_constant (jcf, cindex,
636 CONSTANT_Long);
637 annotation_write_short (index);
639 break;
640 case 's':
642 uint16 cindex = JCF_readu2 (jcf);
643 /* Despite what the JVM spec says, compilers generate a Utf8
644 constant here, not a String. */
645 index = handle_constant (jcf, cindex,
646 CONSTANT_Utf8);
647 annotation_write_short (index);
649 break;
651 case 'e':
653 uint16 type_name_index = JCF_readu2 (jcf);
654 uint16 const_name_index = JCF_readu2 (jcf);
655 index = handle_constant (jcf, type_name_index,
656 CONSTANT_Class);
657 annotation_write_short (index);
658 index = handle_constant (jcf, const_name_index,
659 CONSTANT_Utf8);
660 annotation_write_short (index);
662 break;
663 case 'c':
665 uint16 class_info_index = JCF_readu2 (jcf);
666 index = handle_constant (jcf, class_info_index,
667 CONSTANT_Class);
668 annotation_write_short (index);
670 break;
671 case '@':
673 handle_annotation (jcf, level + 1);
675 break;
676 case '[':
678 uint16 n_array_elts = JCF_readu2 (jcf);
679 annotation_write_short (n_array_elts);
680 while (n_array_elts--)
681 handle_element_value (jcf, level + 1);
683 break;
684 default:
685 abort();
686 break;
688 return index;
691 /* Read an annotation structure from JCF. Write it to the
692 reflection_data field of the outgoing class. */
694 static void
695 handle_annotation (JCF *jcf, int level)
697 uint16 type_index = JCF_readu2 (jcf);
698 uint16 npairs = JCF_readu2 (jcf);
699 int index = handle_constant (jcf, type_index,
700 CONSTANT_Class);
701 annotation_write_short (index);
702 annotation_write_short (npairs);
703 while (npairs--)
705 uint16 name_index = JCF_readu2 (jcf);
706 index = handle_constant (jcf, name_index,
707 CONSTANT_Utf8);
708 annotation_write_short (index);
709 handle_element_value (jcf, level + 2);
713 /* Read an annotation count from JCF, and write the following
714 annotations to the reflection_data field of the outgoing class. */
716 static void
717 handle_annotations (JCF *jcf, int level)
719 uint16 num = JCF_readu2 (jcf);
720 annotation_write_short (num);
721 while (num--)
722 handle_annotation (jcf, level);
725 /* As handle_annotations(), but perform a sanity check that we write
726 the same number of bytes that we were expecting. */
728 static void
729 handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
730 long length)
732 long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
734 handle_annotations (jcf, 0);
736 gcc_assert (old_datasize + length
737 == TYPE_REFLECTION_DATASIZE (current_class));
740 /* gcj permutes its fields array after generating annotation_data, so
741 we have to fixup field indexes for fields that have moved. Given
742 ARG, a VEC_int, fixup the field indexes in the reflection_data of
743 the outgoing class. We use field_offsets to tell us where the
744 fixups must go. */
746 void
747 rewrite_reflection_indexes (void *arg)
749 bitmap_iterator bi;
750 unsigned int offset;
751 VEC(int, heap) *map = (VEC(int, heap) *) arg;
752 unsigned char *data = TYPE_REFLECTION_DATA (current_class);
754 if (map)
756 EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
758 uint16 index = annotation_read_short (data + offset);
759 annotation_rewrite_short
760 (VEC_index (int, map, index), data + offset);
765 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
766 reflection_data of the outgoing class. */
768 static void
769 handle_member_annotations (int member_index, JCF *jcf,
770 const unsigned char *name ATTRIBUTE_UNUSED,
771 long len, jv_attr_type member_type)
773 int new_len = len + 1;
774 annotation_write_byte (member_type);
775 if (member_type != JV_CLASS_ATTR)
776 new_len += 2;
777 annotation_write_int (new_len);
778 annotation_write_byte (JV_ANNOTATIONS_KIND);
779 if (member_type == JV_FIELD_ATTR)
780 bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
781 if (member_type != JV_CLASS_ATTR)
782 annotation_write_short (member_index);
783 handle_annotation_attribute (member_index, jcf, len);
786 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
787 to the reflection_data of the outgoing class. */
789 static void
790 handle_parameter_annotations (int member_index, JCF *jcf,
791 const unsigned char *name ATTRIBUTE_UNUSED,
792 long len, jv_attr_type member_type)
794 int new_len = len + 1;
795 uint8 num;
796 annotation_write_byte (member_type);
797 if (member_type != JV_CLASS_ATTR)
798 new_len += 2;
799 annotation_write_int (new_len);
800 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
801 if (member_type != JV_CLASS_ATTR)
802 annotation_write_short (member_index);
803 num = JCF_readu (jcf);
804 annotation_write_byte (num);
805 while (num--)
806 handle_annotations (jcf, 0);
810 /* Read the AnnotationDefault data from JCF and write them to the
811 reflection_data of the outgoing class. */
813 static void
814 handle_default_annotation (int member_index, JCF *jcf,
815 const unsigned char *name ATTRIBUTE_UNUSED,
816 long len, jv_attr_type member_type)
818 int new_len = len + 1;
819 annotation_write_byte (member_type);
820 if (member_type != JV_CLASS_ATTR)
821 new_len += 2;
822 annotation_write_int (new_len);
823 annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
824 if (member_type != JV_CLASS_ATTR)
825 annotation_write_short (member_index);
826 handle_element_value (jcf, 0);
829 /* As above, for the EnclosingMethod attribute. */
831 static void
832 handle_enclosingmethod_attribute (int member_index, JCF *jcf,
833 const unsigned char *name ATTRIBUTE_UNUSED,
834 long len, jv_attr_type member_type)
836 int new_len = len + 1;
837 uint16 index;
838 annotation_write_byte (member_type);
839 if (member_type != JV_CLASS_ATTR)
840 new_len += 2;
841 annotation_write_int (new_len);
842 annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
843 if (member_type != JV_CLASS_ATTR)
844 annotation_write_short (member_index);
846 index = JCF_readu2 (jcf);
847 index = handle_constant (jcf, index, CONSTANT_Class);
848 annotation_write_short (index);
850 index = JCF_readu2 (jcf);
851 index = handle_constant (jcf, index, CONSTANT_NameAndType);
852 annotation_write_short (index);
855 /* As above, for the Signature attribute. */
857 static void
858 handle_signature_attribute (int member_index, JCF *jcf,
859 const unsigned char *name ATTRIBUTE_UNUSED,
860 long len, jv_attr_type member_type)
862 int new_len = len + 1;
863 uint16 index;
864 annotation_write_byte (member_type);
865 if (member_type != JV_CLASS_ATTR)
866 new_len += 2;
867 annotation_write_int (new_len);
868 annotation_write_byte (JV_SIGNATURE_KIND);
869 if (member_type != JV_CLASS_ATTR)
870 annotation_write_short (member_index);
872 index = JCF_readu2 (jcf);
873 index = handle_constant (jcf, index, CONSTANT_Utf8);
874 annotation_write_short (index);
879 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
881 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
882 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
883 output_class = current_class = give_name_to_class (jcf, THIS); \
884 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
886 #define HANDLE_CLASS_INTERFACE(INDEX) \
887 add_interface (current_class, get_class_constant (jcf, INDEX))
889 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
890 { int sig_index = SIGNATURE; \
891 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
892 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
893 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
894 if ((ACCESS_FLAGS) & ACC_FINAL) \
895 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
898 #define HANDLE_END_FIELDS() \
899 (current_field = NULL_TREE)
901 #define HANDLE_CONSTANTVALUE(INDEX) \
902 { tree constant; int index = INDEX; \
903 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
904 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
905 constant = build_utf8_ref (name); \
907 else \
908 constant = get_constant (jcf, index); \
909 set_constant_value (current_field, constant); }
911 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
912 (current_method = add_method (current_class, ACCESS_FLAGS, \
913 get_name_constant (jcf, NAME), \
914 get_name_constant (jcf, SIGNATURE)), \
915 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
916 DECL_LINENUMBERS_OFFSET (current_method) = 0)
918 #define HANDLE_END_METHODS() \
919 { current_method = NULL_TREE; }
921 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
922 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
923 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
924 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
925 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
927 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
928 { int n = (COUNT); \
929 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
930 JCF_SKIP (jcf, n * 10); }
932 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
933 { int n = (COUNT); \
934 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
935 JCF_SKIP (jcf, n * 4); }
937 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
939 int n = COUNT; \
940 VEC (tree,gc) *v = VEC_alloc (tree, gc, n); \
941 gcc_assert (DECL_FUNCTION_THROWS (current_method) == NULL); \
942 while (--n >= 0) \
944 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
945 VEC_quick_push (tree, v, thrown_class); \
947 DECL_FUNCTION_THROWS (current_method) = v; \
950 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
952 /* Link seen inner classes to their outer context and register the
953 inner class to its outer context. They will be later loaded. */
954 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
955 handle_innerclass_attribute (COUNT, jcf, attribute_length)
957 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
959 /* Irrelevant decls should have been nullified by the END macros. \
960 DECL_ARTIFICIAL on fields is used for something else (See \
961 PUSH_FIELD in java-tree.h) */ \
962 if (current_method) \
963 DECL_ARTIFICIAL (current_method) = 1; \
964 else if (current_field) \
965 FIELD_SYNTHETIC (current_field) = 1; \
966 else \
967 TYPE_SYNTHETIC (current_class) = 1; \
970 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
972 if (current_class == object_type_node) \
973 jcf->right_zip = 1; \
976 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
978 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
981 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
983 JCF_SKIP(jcf, attribute_length); \
986 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
988 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
991 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
993 JCF_SKIP(jcf, attribute_length); \
996 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
998 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
1001 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
1003 handle_enclosingmethod_attribute (index, jcf, name_data, \
1004 attribute_length, attr_type); \
1007 #define HANDLE_SIGNATURE_ATTRIBUTE() \
1009 handle_signature_attribute (index, jcf, name_data, \
1010 attribute_length, attr_type); \
1013 #include "jcf-reader.c"
1015 tree
1016 parse_signature (JCF *jcf, int sig_index)
1018 gcc_assert (sig_index > 0
1019 && sig_index < JPOOL_SIZE (jcf)
1020 && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1022 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1023 JPOOL_UTF_LENGTH (jcf, sig_index));
1026 tree
1027 get_constant (JCF *jcf, int index)
1029 tree value;
1030 int tag;
1031 if (index <= 0 || index >= JPOOL_SIZE(jcf))
1032 goto bad;
1033 tag = JPOOL_TAG (jcf, index);
1034 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
1035 return jcf->cpool.data[index].t;
1036 switch (tag)
1038 case CONSTANT_Integer:
1040 jint num = JPOOL_INT(jcf, index);
1041 value = build_int_cst (int_type_node, num);
1042 break;
1044 case CONSTANT_Long:
1046 unsigned HOST_WIDE_INT num;
1047 double_int val;
1049 num = JPOOL_UINT (jcf, index);
1050 val = double_int_lshift (uhwi_to_double_int (num), 32, 64, false);
1051 num = JPOOL_UINT (jcf, index + 1);
1052 val = double_int_ior (val, uhwi_to_double_int (num));
1054 value = double_int_to_tree (long_type_node, val);
1055 break;
1058 case CONSTANT_Float:
1060 jint num = JPOOL_INT(jcf, index);
1061 long buf = num;
1062 REAL_VALUE_TYPE d;
1064 real_from_target_fmt (&d, &buf, &ieee_single_format);
1065 value = build_real (float_type_node, d);
1066 break;
1069 case CONSTANT_Double:
1071 long buf[2], lo, hi;
1072 REAL_VALUE_TYPE d;
1074 hi = JPOOL_UINT (jcf, index);
1075 lo = JPOOL_UINT (jcf, index+1);
1077 if (FLOAT_WORDS_BIG_ENDIAN)
1078 buf[0] = hi, buf[1] = lo;
1079 else
1080 buf[0] = lo, buf[1] = hi;
1082 real_from_target_fmt (&d, buf, &ieee_double_format);
1083 value = build_real (double_type_node, d);
1084 break;
1087 case CONSTANT_String:
1089 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
1090 const char *utf8_ptr = IDENTIFIER_POINTER (name);
1091 int utf8_len = IDENTIFIER_LENGTH (name);
1092 const unsigned char *utf8;
1093 int i;
1095 /* Check for a malformed Utf8 string. */
1096 utf8 = (const unsigned char *) utf8_ptr;
1097 i = utf8_len;
1098 while (i > 0)
1100 int char_len = UT8_CHAR_LENGTH (*utf8);
1101 if (char_len < 0 || char_len > 3 || char_len > i)
1102 fatal_error ("bad string constant");
1104 utf8 += char_len;
1105 i -= char_len;
1108 /* Allocate a new string value. */
1109 value = build_string (utf8_len, utf8_ptr);
1110 TREE_TYPE (value) = build_pointer_type (string_type_node);
1112 break;
1113 default:
1114 goto bad;
1116 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
1117 jcf->cpool.data[index].t = value;
1118 return value;
1119 bad:
1120 internal_error ("bad value constant type %d, index %d",
1121 JPOOL_TAG (jcf, index), index);
1124 tree
1125 get_name_constant (JCF *jcf, int index)
1127 tree name = get_constant (jcf, index);
1128 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1129 return name;
1132 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1133 a non anonymous entry) is found, We augment the inner class list of
1134 the outer context with the newly resolved innerclass. */
1136 static void
1137 handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
1139 int c = count;
1141 annotation_write_byte (JV_CLASS_ATTR);
1142 annotation_write_int (attribute_length+1);
1143 annotation_write_byte (JV_INNER_CLASSES_KIND);
1144 annotation_write_short (count);
1146 while (c--)
1148 /* Read inner_class_info_index. This may be 0 */
1149 int icii = JCF_readu2 (jcf);
1150 /* Read outer_class_info_index. If the innerclasses attribute
1151 entry isn't a member (like an inner class) the value is 0. */
1152 int ocii = JCF_readu2 (jcf);
1153 /* Read inner_name_index. If the class we're dealing with is
1154 an anonymous class, it must be 0. */
1155 int ini = JCF_readu2 (jcf);
1156 /* Read the access flag. */
1157 int acc = JCF_readu2 (jcf);
1159 annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1160 annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1161 annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1162 annotation_write_short (acc);
1164 /* If icii is 0, don't try to read the class. */
1165 if (icii >= 0)
1167 tree klass = get_class_constant (jcf, icii);
1168 tree decl = TYPE_NAME (klass);
1169 /* Skip reading further if ocii is null */
1170 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1172 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1173 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
1174 set_class_decl_access_flags (acc, decl);
1175 DECL_CONTEXT (decl) = outer;
1176 DECL_INNER_CLASS_LIST (outer) =
1177 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1178 CLASS_COMPLETE_P (decl) = 1;
1184 static tree
1185 give_name_to_class (JCF *jcf, int i)
1187 gcc_assert (i > 0
1188 && i < JPOOL_SIZE (jcf)
1189 && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1192 tree package_name = NULL_TREE, tmp;
1193 tree this_class;
1194 int j = JPOOL_USHORT1 (jcf, i);
1195 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1196 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
1197 JPOOL_UTF_LENGTH (jcf, j));
1198 this_class = lookup_class (class_name);
1200 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
1201 const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
1202 linemap_add (line_table, LC_ENTER, false, sfname, 0);
1203 input_location = linemap_line_start (line_table, 0, 1);
1204 file_start_location = input_location;
1205 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1206 if (main_input_filename == NULL && jcf == main_jcf)
1207 main_input_filename = sfname;
1210 jcf->cpool.data[i].t = this_class;
1211 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1212 split_qualified_name (&package_name, &tmp,
1213 DECL_NAME (TYPE_NAME (this_class)));
1214 TYPE_PACKAGE (this_class) = package_name;
1215 return this_class;
1219 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1221 tree
1222 get_class_constant (JCF *jcf, int i)
1224 tree type;
1225 gcc_assert (i > 0
1226 && i < JPOOL_SIZE (jcf)
1227 && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
1229 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1231 int name_index = JPOOL_USHORT1 (jcf, i);
1232 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1233 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
1234 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
1236 if (name[0] == '[') /* Handle array "classes". */
1237 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
1238 else
1240 tree cname = unmangle_classname (name, nlength);
1241 type = lookup_class (cname);
1243 jcf->cpool.data[i].t = type;
1244 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1246 else
1247 type = jcf->cpool.data[i].t;
1248 return type;
1251 /* Read a class with the fully qualified-name NAME.
1252 Return 1 iff we read the requested file.
1253 (It is still possible we failed if the file did not
1254 define the class it is supposed to.) */
1257 read_class (tree name)
1259 JCF this_jcf, *jcf;
1260 tree icv, klass = NULL_TREE;
1261 tree save_current_class = current_class;
1262 tree save_output_class = output_class;
1263 location_t save_location = input_location;
1264 JCF *save_current_jcf = current_jcf;
1266 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
1268 klass = TREE_TYPE (icv);
1269 jcf = TYPE_JCF (klass);
1271 else
1272 jcf = NULL;
1274 if (jcf == NULL)
1276 const char* path_name;
1277 this_jcf.zipd = NULL;
1278 jcf = &this_jcf;
1280 path_name = find_class (IDENTIFIER_POINTER (name),
1281 IDENTIFIER_LENGTH (name),
1282 &this_jcf);
1283 if (path_name == 0)
1284 return 0;
1285 else
1286 free(CONST_CAST (char *, path_name));
1289 current_jcf = jcf;
1291 if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
1293 output_class = current_class = klass;
1294 if (JCF_SEEN_IN_ZIP (current_jcf))
1295 read_zip_member(current_jcf,
1296 current_jcf->zipd, current_jcf->zipd->zipf);
1297 jcf_parse (current_jcf);
1298 /* Parsing might change the class, in which case we have to
1299 put it back where we found it. */
1300 if (current_class != klass && icv != NULL_TREE)
1301 TREE_TYPE (icv) = current_class;
1302 klass = current_class;
1304 layout_class (klass);
1305 load_inner_classes (klass);
1307 output_class = save_output_class;
1308 current_class = save_current_class;
1309 input_location = save_location;
1310 current_jcf = save_current_jcf;
1311 return 1;
1314 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1315 called from the parser, otherwise it's a RECORD_TYPE node. If
1316 VERBOSE is 1, print error message on failure to load a class. */
1317 void
1318 load_class (tree class_or_name, int verbose)
1320 tree name, saved;
1321 int class_loaded = 0;
1322 tree class_decl = NULL_TREE;
1323 bool is_compiled_class = false;
1325 /* We've already failed, don't try again. */
1326 if (TREE_CODE (class_or_name) == RECORD_TYPE
1327 && TYPE_DUMMY (class_or_name))
1328 return;
1330 /* class_or_name can be the name of the class we want to load */
1331 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1332 name = class_or_name;
1333 /* In some cases, it's a dependency that we process earlier that
1334 we though */
1335 else if (TREE_CODE (class_or_name) == TREE_LIST)
1336 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1337 /* Or it's a type in the making */
1338 else
1339 name = DECL_NAME (TYPE_NAME (class_or_name));
1341 class_decl = IDENTIFIER_CLASS_VALUE (name);
1342 if (class_decl != NULL_TREE)
1344 tree type = TREE_TYPE (class_decl);
1345 is_compiled_class
1346 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1347 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1350 saved = name;
1352 /* If flag_verify_invocations is unset, we don't try to load a class
1353 unless we're looking for Object (which is fixed by the ABI) or
1354 it's a class that we're going to compile. */
1355 if (flag_verify_invocations
1356 || class_or_name == object_type_node
1357 || is_compiled_class
1358 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1360 while (1)
1362 const char *separator;
1364 /* We've already loaded it. */
1365 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1367 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1368 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1369 break;
1372 if (read_class (name))
1373 break;
1375 /* We failed loading name. Now consider that we might be looking
1376 for an inner class. */
1377 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1378 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
1379 name = get_identifier_with_length (IDENTIFIER_POINTER (name),
1380 (separator
1381 - IDENTIFIER_POINTER (name)));
1382 /* Otherwise, we failed, we bail. */
1383 else
1384 break;
1388 /* have we found the class we're looking for? */
1389 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1390 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1391 class_loaded = type && CLASS_PARSED_P (type);
1395 if (!class_loaded)
1397 if (flag_verify_invocations || ! flag_indirect_dispatch)
1399 if (verbose)
1400 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1402 else if (verbose)
1404 /* This is just a diagnostic during testing, not a real problem. */
1405 if (!quiet_flag)
1406 warning (0, "cannot find file for class %s",
1407 IDENTIFIER_POINTER (saved));
1409 /* Fake it. */
1410 if (TREE_CODE (class_or_name) == RECORD_TYPE)
1412 set_super_info (0, class_or_name, object_type_node, 0);
1413 TYPE_DUMMY (class_or_name) = 1;
1414 /* We won't be able to output any debug info for this class. */
1415 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1421 /* Parse the .class file JCF. */
1423 static void
1424 jcf_parse (JCF* jcf)
1426 int i, code;
1428 bitmap_clear (field_offsets);
1430 if (jcf_parse_preamble (jcf) != 0)
1431 fatal_error ("not a valid Java .class file");
1432 code = jcf_parse_constant_pool (jcf);
1433 if (code != 0)
1434 fatal_error ("error while parsing constant pool");
1435 code = verify_constant_pool (jcf);
1436 if (code > 0)
1437 fatal_error ("error in constant pool entry #%d\n", code);
1439 jcf_parse_class (jcf);
1440 if (main_class == NULL_TREE)
1441 main_class = current_class;
1442 if (! quiet_flag && TYPE_NAME (current_class))
1443 fprintf (stderr, " %s %s",
1444 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
1445 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
1446 if (CLASS_PARSED_P (current_class))
1448 /* FIXME - where was first time */
1449 fatal_error ("reading class %s for the second time from %s",
1450 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1451 jcf->filename);
1453 CLASS_PARSED_P (current_class) = 1;
1455 for (i = 1; i < JPOOL_SIZE(jcf); i++)
1457 switch (JPOOL_TAG (jcf, i))
1459 case CONSTANT_Class:
1460 get_class_constant (jcf, i);
1461 break;
1465 code = jcf_parse_fields (jcf);
1466 if (code != 0)
1467 fatal_error ("error while parsing fields");
1468 code = jcf_parse_methods (jcf);
1469 if (code != 0)
1470 fatal_error ("error while parsing methods");
1471 code = jcf_parse_final_attributes (jcf);
1472 if (code != 0)
1473 fatal_error ("error while parsing final attributes");
1475 if (TYPE_REFLECTION_DATA (current_class))
1476 annotation_write_byte (JV_DONE_ATTR);
1478 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1480 /* The fields of class_type_node are already in correct order. */
1481 if (current_class != class_type_node && current_class != object_type_node)
1482 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1484 if (current_class == object_type_node)
1485 layout_class_methods (object_type_node);
1486 else
1487 VEC_safe_push (tree, gc, all_class_list, TYPE_NAME (current_class));
1490 /* If we came across inner classes, load them now. */
1491 static void
1492 load_inner_classes (tree cur_class)
1494 tree current;
1495 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
1496 current = TREE_CHAIN (current))
1498 tree name = DECL_NAME (TREE_PURPOSE (current));
1499 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1500 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1501 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
1502 load_class (name, 1);
1506 static void
1507 duplicate_class_warning (const char *filename)
1509 location_t warn_loc;
1510 linemap_add (line_table, LC_RENAME, 0, filename, 0);
1511 warn_loc = linemap_line_start (line_table, 0, 1);
1512 warning_at (warn_loc, 0, "duplicate class will only be compiled once");
1515 static void
1516 java_layout_seen_class_methods (void)
1518 unsigned start = 0;
1519 unsigned end = VEC_length (tree, all_class_list);
1521 while (1)
1523 unsigned ix;
1524 unsigned new_length;
1526 for (ix = start; ix != end; ix++)
1528 tree decl = VEC_index (tree, all_class_list, ix);
1529 tree cls = TREE_TYPE (decl);
1531 input_location = DECL_SOURCE_LOCATION (decl);
1533 if (! CLASS_LOADED_P (cls))
1534 load_class (cls, 0);
1536 layout_class_methods (cls);
1539 /* Note that new classes might have been added while laying out
1540 methods, changing the value of all_class_list. */
1541 new_length = VEC_length (tree, all_class_list);
1542 if (end != new_length)
1544 start = end;
1545 end = new_length;
1547 else
1548 break;
1552 static void
1553 parse_class_file (void)
1555 tree method;
1556 location_t save_location = input_location;
1558 java_layout_seen_class_methods ();
1560 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1562 /* Re-enter the current file. */
1563 expanded_location loc = expand_location (input_location);
1564 linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1566 file_start_location = input_location;
1567 (*debug_hooks->start_source_file) (input_line, input_filename);
1569 java_mark_class_local (current_class);
1571 gen_indirect_dispatch_tables (current_class);
1573 for (method = TYPE_METHODS (current_class);
1574 method != NULL_TREE; method = DECL_CHAIN (method))
1576 JCF *jcf = current_jcf;
1578 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
1579 continue;
1581 if (METHOD_NATIVE (method))
1583 tree arg;
1584 int decl_max_locals;
1586 if (! flag_jni)
1587 continue;
1588 /* We need to compute the DECL_MAX_LOCALS. We need to take
1589 the wide types into account too. */
1590 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
1591 arg != end_params_node;
1592 arg = TREE_CHAIN (arg), decl_max_locals += 1)
1594 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1595 decl_max_locals += 1;
1597 DECL_MAX_LOCALS (method) = decl_max_locals;
1598 start_java_method (method);
1599 give_name_to_locals (jcf);
1600 *get_stmts () = build_jni_stub (method);
1601 end_java_method ();
1602 continue;
1605 if (DECL_CODE_OFFSET (method) == 0)
1607 current_function_decl = method;
1608 error ("missing Code attribute");
1609 continue;
1612 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1613 if (DECL_LINENUMBERS_OFFSET (method))
1615 int i;
1616 int min_line = 0;
1617 unsigned char *ptr;
1618 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1619 linenumber_count = i = JCF_readu2 (jcf);
1620 linenumber_table = ptr = jcf->read_ptr;
1622 for (ptr += 2; --i >= 0; ptr += 4)
1624 int line = GET_u2 (ptr);
1625 /* Set initial input_line to smallest linenumber.
1626 * Needs to be set before init_function_start. */
1627 if (min_line == 0 || line < min_line)
1628 min_line = line;
1630 if (min_line != 0)
1631 input_location = linemap_line_start (line_table, min_line, 1);
1633 else
1635 linenumber_table = NULL;
1636 linenumber_count = 0;
1639 start_java_method (method);
1641 note_instructions (jcf, method);
1643 give_name_to_locals (jcf);
1645 /* Bump up start_label_pc_this_method so we get a unique label number
1646 and reset highest_label_pc_this_method. */
1647 if (highest_label_pc_this_method >= 0)
1649 /* We adjust to the next multiple of 1000. This is just a frill
1650 so the last 3 digits of the label number match the bytecode
1651 offset, which might make debugging marginally more convenient. */
1652 start_label_pc_this_method
1653 = ((((start_label_pc_this_method + highest_label_pc_this_method)
1654 / 1000)
1655 + 1)
1656 * 1000);
1657 highest_label_pc_this_method = -1;
1660 /* Convert bytecode to trees. */
1661 expand_byte_code (jcf, method);
1663 end_java_method ();
1666 finish_class ();
1668 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
1669 input_location = save_location;
1672 static VEC(tree,gc) *predefined_filenames;
1674 void
1675 add_predefined_file (tree name)
1677 VEC_safe_push (tree, gc, predefined_filenames, name);
1681 predefined_filename_p (tree node)
1683 unsigned ix;
1684 tree f;
1686 for (ix = 0; VEC_iterate (tree, predefined_filenames, ix, f); ix++)
1687 if (f == node)
1688 return 1;
1690 return 0;
1693 /* Generate a function that does all static initialization for this
1694 translation unit. */
1696 static void
1697 java_emit_static_constructor (void)
1699 tree body = NULL;
1701 emit_register_classes (&body);
1702 write_resource_constructor (&body);
1704 if (body)
1706 tree name = get_identifier ("_Jv_global_static_constructor");
1708 tree decl
1709 = build_decl (input_location, FUNCTION_DECL, name,
1710 build_function_type (void_type_node, void_list_node));
1712 tree resdecl = build_decl (input_location,
1713 RESULT_DECL, NULL_TREE, void_type_node);
1714 DECL_ARTIFICIAL (resdecl) = 1;
1715 DECL_RESULT (decl) = resdecl;
1716 current_function_decl = decl;
1717 allocate_struct_function (decl, false);
1719 TREE_STATIC (decl) = 1;
1720 TREE_USED (decl) = 1;
1721 DECL_ARTIFICIAL (decl) = 1;
1722 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1723 DECL_SAVED_TREE (decl) = body;
1724 DECL_UNINLINABLE (decl) = 1;
1726 DECL_INITIAL (decl) = make_node (BLOCK);
1727 TREE_USED (DECL_INITIAL (decl)) = 1;
1729 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1730 java_genericize (decl);
1731 cgraph_finalize_function (decl, false);
1736 void
1737 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
1739 int filename_count = 0;
1740 location_t save_location = input_location;
1741 char *file_list = NULL, *list, *next;
1742 tree node;
1743 FILE *finput = NULL;
1744 int in_quotes = 0;
1745 unsigned ix;
1747 bitmap_obstack_initialize (&bit_obstack);
1748 field_offsets = BITMAP_ALLOC (&bit_obstack);
1750 if (flag_filelist_file)
1752 int avail = 2000;
1753 finput = fopen (main_input_filename, "r");
1754 if (finput == NULL)
1755 fatal_error ("can't open %s: %m", input_filename);
1756 list = XNEWVEC (char, avail);
1757 next = list;
1758 for (;;)
1760 int count;
1761 if (avail < 500)
1763 count = next - list;
1764 avail = 2 * (count + avail);
1765 list = XRESIZEVEC (char, list, avail);
1766 next = list + count;
1767 avail = avail - count;
1769 /* Subtract to to guarantee space for final '\0'. */
1770 count = fread (next, 1, avail - 1, finput);
1771 if (count == 0)
1773 if (! feof (finput))
1774 fatal_error ("error closing %s: %m", input_filename);
1775 *next = '\0';
1776 break;
1778 avail -= count;
1779 next += count;
1781 fclose (finput);
1782 finput = NULL;
1783 file_list = list;
1785 else
1786 list = CONST_CAST (char *, main_input_filename);
1788 while (list)
1790 for (next = list; ; )
1792 char ch = *next;
1793 if (flag_filelist_file && ! in_quotes
1794 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1795 || ch == '&') /* FIXME */)
1797 if (next == list)
1799 next++;
1800 list = next;
1801 continue;
1803 else
1805 *next++ = '\0';
1806 break;
1809 if (flag_filelist_file && ch == '"')
1811 in_quotes = ! in_quotes;
1812 *next++ = '\0';
1813 if (in_quotes)
1814 list = next;
1815 else
1816 break;
1818 if (ch == '\0')
1820 next = NULL;
1821 break;
1823 next++;
1826 /* Exclude .java files. */
1827 if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1829 /* Nothing. */
1831 else if (list[0])
1833 node = get_identifier (list);
1835 filename_count++;
1837 /* Exclude file that we see twice on the command line. */
1839 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1840 duplicate_class_warning (IDENTIFIER_POINTER (node));
1841 else
1843 tree file_decl = build_decl (input_location,
1844 TRANSLATION_UNIT_DECL, node, NULL);
1845 VEC_safe_push (tree, gc, current_file_list, file_decl);
1846 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1849 list = next;
1852 if (file_list != NULL)
1853 free (file_list);
1855 if (filename_count == 0)
1856 warning (0, "no input file specified");
1858 if (resource_name)
1860 const char *resource_filename;
1862 /* Only one resource file may be compiled at a time. */
1863 assert (VEC_length (tree, current_file_list) == 1);
1865 resource_filename = IDENTIFIER_POINTER (DECL_NAME (VEC_index (tree, current_file_list, 0)));
1866 compile_resource_file (resource_name, resource_filename);
1868 goto finish;
1871 current_jcf = main_jcf;
1872 for (ix = 0; VEC_iterate (tree, current_file_list, ix, node); ix++)
1874 unsigned char magic_string[4];
1875 char *real_path;
1876 uint32 magic = 0;
1877 tree name = DECL_NAME (node);
1878 tree real_file;
1879 const char *filename = IDENTIFIER_POINTER (name);
1881 /* Skip already parsed files */
1882 real_path = lrealpath (filename);
1883 real_file = get_identifier (real_path);
1884 free (real_path);
1885 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1886 continue;
1888 /* Close previous descriptor, if any */
1889 if (finput && fclose (finput))
1890 fatal_error ("can't close input file %s: %m", main_input_filename);
1892 finput = fopen (filename, "rb");
1893 if (finput == NULL)
1894 fatal_error ("can't open %s: %m", filename);
1896 #ifdef IO_BUFFER_SIZE
1897 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1898 _IOFBF, IO_BUFFER_SIZE);
1899 #endif
1901 /* Figure what kind of file we're dealing with */
1902 if (fread (magic_string, 1, 4, finput) == 4)
1904 fseek (finput, 0L, SEEK_SET);
1905 magic = GET_u4 (magic_string);
1907 if (magic == 0xcafebabe)
1909 CLASS_FILE_P (node) = 1;
1910 current_jcf = ggc_alloc_cleared_JCF ();
1911 current_jcf->read_state = finput;
1912 current_jcf->filbuf = jcf_filbuf_from_stdio;
1913 jcf_parse (current_jcf);
1914 DECL_SOURCE_LOCATION (node) = file_start_location;
1915 TYPE_JCF (current_class) = current_jcf;
1916 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1918 /* We've already compiled this class. */
1919 duplicate_class_warning (filename);
1920 continue;
1922 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1923 TREE_TYPE (node) = current_class;
1925 else if (magic == (JCF_u4)ZIPMAGIC)
1927 main_jcf = ggc_alloc_cleared_JCF ();
1928 main_jcf->read_state = finput;
1929 main_jcf->filbuf = jcf_filbuf_from_stdio;
1930 linemap_add (line_table, LC_ENTER, false, filename, 0);
1931 input_location = linemap_line_start (line_table, 0, 1);
1932 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1933 fatal_error ("bad zip/jar file %s", filename);
1934 localToFile = SeenZipFiles;
1935 /* Register all the classes defined there. */
1936 process_zip_dir ((FILE *) main_jcf->read_state);
1937 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1938 parse_zip_file_entries ();
1940 else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1942 /* Ignore an empty input jar. */
1944 else
1946 gcc_unreachable ();
1947 #if 0
1948 java_push_parser_context ();
1949 java_parser_context_save_global ();
1951 parse_source_file_1 (real_file, filename, finput);
1952 java_parser_context_restore_global ();
1953 java_pop_parser_context (1);
1954 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1955 #endif
1959 for (ix = 0; VEC_iterate (tree, current_file_list, ix, node); ix++)
1961 input_location = DECL_SOURCE_LOCATION (node);
1962 if (CLASS_FILE_P (node))
1964 /* FIXME: These two flags really should be independent. We
1965 should be able to compile fully binary compatible, but
1966 with flag_verify_invocations on. */
1967 flag_verify_invocations = ! flag_indirect_dispatch;
1968 output_class = current_class = TREE_TYPE (node);
1970 current_jcf = TYPE_JCF (current_class);
1971 layout_class (current_class);
1972 load_inner_classes (current_class);
1973 parse_class_file ();
1974 JCF_FINISH (current_jcf);
1977 input_location = save_location;
1979 bitmap_obstack_release (&bit_obstack);
1981 finish:
1982 /* Arrange for any necessary initialization to happen. */
1983 java_emit_static_constructor ();
1984 gcc_assert (global_bindings_p ());
1988 /* Return the name of the class corresponding to the name of the file
1989 in this zip entry. The result is newly allocated using ALLOC. */
1990 static char *
1991 compute_class_name (struct ZipDirectory *zdir)
1993 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1994 char *class_name;
1995 int i;
1996 int filename_length = zdir->filename_length;
1998 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
2000 class_name_in_zip_dir += 2;
2001 filename_length -= 2;
2004 filename_length -= strlen (".class");
2005 class_name = XNEWVEC (char, filename_length + 1);
2006 memcpy (class_name, class_name_in_zip_dir, filename_length);
2007 class_name [filename_length] = '\0';
2009 for (i = 0; i < filename_length; i++)
2010 if (class_name[i] == '/')
2011 class_name[i] = '.';
2013 return class_name;
2016 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2017 if it is a property file of some sort. */
2018 static int
2019 classify_zip_file (struct ZipDirectory *zdir)
2021 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2023 if (zdir->filename_length > 6
2024 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2025 ".class", 6))
2026 return 1;
2028 /* For now we drop the manifest, but not other information. */
2029 if (zdir->filename_length == 20
2030 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
2031 return 0;
2033 /* Drop directory entries. */
2034 if (zdir->filename_length > 0
2035 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2036 return 0;
2038 return 2;
2041 /* Process all class entries found in the zip file. */
2042 static void
2043 parse_zip_file_entries (void)
2045 struct ZipDirectory *zdir;
2046 int i;
2048 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2049 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2051 tree klass;
2053 switch (classify_zip_file (zdir))
2055 case 0:
2056 continue;
2058 case 1:
2060 char *class_name = compute_class_name (zdir);
2061 int previous_alias_set = -1;
2062 klass = lookup_class (get_identifier (class_name));
2063 FREE (class_name);
2064 current_jcf = TYPE_JCF (klass);
2065 output_class = current_class = klass;
2067 /* This is a dummy class, and now we're compiling it for
2068 real. */
2069 gcc_assert (! TYPE_DUMMY (klass));
2071 /* This is for a corner case where we have a superclass
2072 but no superclass fields.
2074 This can happen if we earlier failed to lay out this
2075 class because its superclass was still in the process
2076 of being laid out; this occurs when we have recursive
2077 class dependencies via inner classes. We must record
2078 the previous alias set and restore it after laying out
2079 the class.
2081 FIXME: this really is a kludge. We should figure out a
2082 way to lay out the class properly before this
2083 happens. */
2084 if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2085 && integer_zerop (TYPE_SIZE (klass)))
2087 TYPE_SIZE (klass) = NULL_TREE;
2088 previous_alias_set = TYPE_ALIAS_SET (klass);
2089 TYPE_ALIAS_SET (klass) = -1;
2092 if (! CLASS_LOADED_P (klass))
2094 if (! CLASS_PARSED_P (klass))
2096 read_zip_member (current_jcf, zdir, localToFile);
2097 jcf_parse (current_jcf);
2099 layout_class (current_class);
2100 load_inner_classes (current_class);
2103 if (previous_alias_set != -1)
2104 TYPE_ALIAS_SET (klass) = previous_alias_set;
2106 if (TYPE_SIZE (current_class) != error_mark_node)
2108 parse_class_file ();
2109 free (current_jcf->buffer); /* No longer necessary */
2110 /* Note: there is a way to free this buffer right after a
2111 class seen in a zip file has been parsed. The idea is the
2112 set its jcf in such a way that buffer will be reallocated
2113 the time the code for the class will be generated. FIXME. */
2116 break;
2118 case 2:
2120 char *file_name, *class_name_in_zip_dir, *buffer;
2121 JCF *jcf;
2122 file_name = XNEWVEC (char, zdir->filename_length + 1);
2123 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2124 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2125 file_name[zdir->filename_length] = '\0';
2126 jcf = XNEW (JCF);
2127 JCF_ZERO (jcf);
2128 jcf->read_state = finput;
2129 jcf->filbuf = jcf_filbuf_from_stdio;
2130 jcf->classname = NULL;
2131 jcf->filename = file_name;
2132 jcf->zipd = zdir;
2134 if (read_zip_member (jcf, zdir, localToFile) < 0)
2135 fatal_error ("error while reading %s from zip file", file_name);
2137 buffer = XNEWVEC (char, zdir->filename_length + 1 +
2138 (jcf->buffer_end - jcf->buffer));
2139 strcpy (buffer, file_name);
2140 /* This is not a typo: we overwrite the trailing \0 of the
2141 file name; this is just how the data is laid out. */
2142 memcpy (buffer + zdir->filename_length,
2143 jcf->buffer, jcf->buffer_end - jcf->buffer);
2145 compile_resource_data (file_name, buffer,
2146 jcf->buffer_end - jcf->buffer);
2147 JCF_FINISH (jcf);
2148 free (jcf);
2149 free (buffer);
2151 break;
2153 default:
2154 gcc_unreachable ();
2159 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
2160 jcf up for further processing and link it to the created class. */
2162 static void
2163 process_zip_dir (FILE *finput)
2165 int i;
2166 ZipDirectory *zdir;
2168 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2169 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2171 char *class_name, *file_name, *class_name_in_zip_dir;
2172 tree klass;
2173 JCF *jcf;
2175 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2177 /* Here we skip non-class files; we handle them later. */
2178 if (classify_zip_file (zdir) != 1)
2179 continue;
2181 class_name = compute_class_name (zdir);
2182 file_name = XNEWVEC (char, zdir->filename_length+1);
2183 jcf = ggc_alloc_cleared_JCF ();
2185 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2186 file_name [zdir->filename_length] = '\0';
2188 klass = lookup_class (get_identifier (class_name));
2190 if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2192 /* We've already compiled this class. */
2193 duplicate_class_warning (file_name);
2194 continue;
2196 /* This function is only called when processing a zip file seen
2197 on the command line. */
2198 CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
2200 jcf->read_state = finput;
2201 jcf->filbuf = jcf_filbuf_from_stdio;
2202 jcf->classname = class_name;
2203 jcf->filename = file_name;
2204 jcf->zipd = zdir;
2206 TYPE_JCF (klass) = jcf;
2210 #include "gt-java-jcf-parse.h"
2211 #include "gtype-java.h"