[ARM] Remove an unused reload hook.
[official-gcc.git] / gcc / java / jcf-parse.c
blobe609331ae6aadb8297439e0a8f1d58b729f7dbb5
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>.
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. */
24 /* Written by Per Bothner <bothner@cygnus.com> */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "hash-set.h"
30 #include "machmode.h"
31 #include "vec.h"
32 #include "double-int.h"
33 #include "input.h"
34 #include "alias.h"
35 #include "symtab.h"
36 #include "options.h"
37 #include "real.h"
38 #include "wide-int.h"
39 #include "inchash.h"
40 #include "tree.h"
41 #include "stringpool.h"
42 #include "obstack.h"
43 #include "flags.h"
44 #include "java-except.h"
45 #include "input.h"
46 #include "javaop.h"
47 #include "java-tree.h"
48 #include "diagnostic-core.h"
49 #include "parse.h"
50 #include "ggc.h"
51 #include "debug.h"
52 #include "hash-map.h"
53 #include "is-a.h"
54 #include "plugin-api.h"
55 #include "tm.h"
56 #include "hard-reg-set.h"
57 #include "function.h"
58 #include "ipa-ref.h"
59 #include "cgraph.h"
60 #include "bitmap.h"
61 #include "target.h"
62 #include "wide-int.h"
64 #ifdef HAVE_LOCALE_H
65 #include <locale.h>
66 #endif
68 #ifdef HAVE_LANGINFO_CODESET
69 #include <langinfo.h>
70 #endif
72 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
73 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
74 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
75 #define JPOOL_UTF_DATA(JCF, INDEX) \
76 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
77 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
78 do { \
79 unsigned char save; unsigned char *text; \
80 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
81 text = (JCF)->read_ptr; \
82 save = text[LENGTH]; \
83 text[LENGTH] = 0; \
84 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
85 text[LENGTH] = save; \
86 JCF_SKIP (JCF, LENGTH); } while (0)
88 #include "jcf.h"
90 extern struct obstack temporary_obstack;
92 static GTY(()) tree parse_roots[2];
94 /* The FIELD_DECL for the current field. */
95 #define current_field parse_roots[0]
97 /* The METHOD_DECL for the current method. */
98 #define current_method parse_roots[1]
100 /* Line 0 in current file, if compiling from bytecode. */
101 static location_t file_start_location;
103 /* The Java archive that provides main_class; the main input file. */
104 static GTY(()) struct JCF * main_jcf;
106 /* A list of all the class DECLs seen so far. */
107 static GTY(()) vec<tree, va_gc> *all_class_list;
109 /* The number of source files passed to us by -fsource-filename and an
110 array of pointers to each name. Used by find_sourcefile(). */
111 static int num_files = 0;
112 static char **filenames;
114 static struct ZipFile *localToFile;
116 /* A map of byte offsets in the reflection data that are fields which
117 need renumbering. */
118 bitmap field_offsets;
119 bitmap_obstack bit_obstack;
121 /* Declarations of some functions used here. */
122 static void handle_innerclass_attribute (int count, JCF *, int len);
123 static tree give_name_to_class (JCF *jcf, int index);
124 static char *compute_class_name (struct ZipDirectory *zdir);
125 static int classify_zip_file (struct ZipDirectory *zdir);
126 static void parse_zip_file_entries (void);
127 static void process_zip_dir (FILE *);
128 static void parse_class_file (void);
129 static void handle_deprecated (void);
130 static void set_source_filename (JCF *, int);
131 static void jcf_parse (struct JCF*);
132 static void load_inner_classes (tree);
133 static void handle_annotation (JCF *jcf, int level);
134 static void java_layout_seen_class_methods (void);
136 /* Handle "Deprecated" attribute. */
137 static void
138 handle_deprecated (void)
140 if (current_field != NULL_TREE)
141 FIELD_DEPRECATED (current_field) = 1;
142 else if (current_method != NULL_TREE)
143 METHOD_DEPRECATED (current_method) = 1;
144 else if (current_class != NULL_TREE)
145 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
146 else
148 /* Shouldn't happen. */
149 gcc_unreachable ();
155 /* Reverse a string. */
156 static char *
157 reverse (const char *s)
159 if (s == NULL)
160 return NULL;
161 else
163 int len = strlen (s);
164 char *d = XNEWVAR (char, len + 1);
165 const char *sp;
166 char *dp;
168 d[len] = 0;
169 for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
170 *dp = *sp;
172 return d;
176 /* Compare two strings for qsort(). */
177 static int
178 cmpstringp (const void *p1, const void *p2)
180 /* The arguments to this function are "pointers to
181 pointers to char", but strcmp() arguments are "pointers
182 to char", hence the following cast plus dereference */
184 return strcmp(*(const char *const*) p1, *(const char *const*) p2);
187 /* Create an array of strings, one for each source file that we've
188 seen. fsource_filename can either be the name of a single .java
189 file or a file that contains a list of filenames separated by
190 newlines. */
191 void
192 java_read_sourcefilenames (const char *fsource_filename)
194 if (fsource_filename
195 && filenames == 0
196 && strlen (fsource_filename) > strlen (".java")
197 && filename_cmp ((fsource_filename
198 + strlen (fsource_filename)
199 - strlen (".java")),
200 ".java") != 0)
202 /* fsource_filename isn't a .java file but a list of filenames
203 separated by newlines */
204 FILE *finput = fopen (fsource_filename, "r");
205 int len = 0;
206 int longest_line = 0;
208 gcc_assert (finput);
210 /* Find out how many files there are, and how long the filenames are. */
211 while (! feof (finput))
213 int ch = getc (finput);
214 if (ch == '\n')
216 num_files++;
217 if (len > longest_line)
218 longest_line = len;
219 len = 0;
220 continue;
222 if (ch == EOF)
223 break;
224 len++;
227 rewind (finput);
229 /* Read the filenames. Put a pointer to each filename into the
230 array FILENAMES. */
232 char *linebuf = (char *) alloca (longest_line + 1);
233 int i = 0;
234 int charpos;
236 filenames = XNEWVEC (char *, num_files);
238 charpos = 0;
239 for (;;)
241 int ch = getc (finput);
242 if (ch == EOF)
243 break;
244 if (ch == '\n')
246 linebuf[charpos] = 0;
247 gcc_assert (i < num_files);
248 /* ??? Perhaps we should use lrealpath() here. Doing
249 so would tidy up things like /../ but the rest of
250 gcc seems to assume relative pathnames, not
251 absolute pathnames. */
252 /* realname = lrealpath (linebuf); */
253 filenames[i++] = reverse (linebuf);
254 charpos = 0;
255 continue;
257 gcc_assert (charpos < longest_line);
258 linebuf[charpos++] = ch;
261 if (num_files > 1)
262 qsort (filenames, num_files, sizeof (char *), cmpstringp);
264 fclose (finput);
266 else
268 filenames = XNEWVEC (char *, 1);
269 filenames[0] = reverse (fsource_filename);
270 num_files = 1;
274 /* Given a relative pathname such as foo/bar.java, attempt to find a
275 longer pathname with the same suffix.
277 This is a best guess heuristic; with some weird class hierarchies we
278 may fail to pick the correct source file. For example, if we have
279 the filenames foo/bar.java and also foo/foo/bar.java, we do not
280 have enough information to know which one is the right match for
281 foo/bar.java. */
283 static const char *
284 find_sourcefile (const char *name)
286 int i = 0, j = num_files-1;
287 char *found = NULL;
289 if (filenames)
291 char *revname = reverse (name);
295 int k = (i+j) / 2;
296 int cmp = strncmp (revname, filenames[k], strlen (revname));
297 if (cmp == 0)
299 /* OK, so we found one. But is it a unique match? */
300 if ((k > i
301 && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
302 || (k < j
303 && (strncmp (revname, filenames[k+1], strlen (revname))
304 == 0)))
306 else
307 found = filenames[k];
308 break;
310 if (cmp > 0)
311 i = k+1;
312 else
313 j = k-1;
315 while (i <= j);
317 free (revname);
320 if (found && strlen (found) > strlen (name))
321 return reverse (found);
322 else
323 return name;
328 /* Handle "SourceFile" attribute. */
330 static void
331 set_source_filename (JCF *jcf, int index)
333 tree sfname_id = get_name_constant (jcf, index);
334 const char *sfname = IDENTIFIER_POINTER (sfname_id);
335 const char *old_filename = LOCATION_FILE (input_location);
336 int new_len = IDENTIFIER_LENGTH (sfname_id);
337 if (old_filename != NULL)
339 int old_len = strlen (old_filename);
340 /* Use the filename from current input_location (derived from the
341 class name) if it has a directory prefix, but otherwise matches
342 sfname. */
343 if (old_len > new_len
344 && filename_cmp (sfname, old_filename + old_len - new_len) == 0
345 && (old_filename[old_len - new_len - 1] == '/'
346 || old_filename[old_len - new_len - 1] == '\\'))
347 return;
349 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
351 const char *class_name
352 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
353 const char *dot = strrchr (class_name, '.');
354 if (dot != NULL)
356 /* Length of prefix, not counting final dot. */
357 int i = dot - class_name;
358 /* Concatenate current package prefix with new sfname. */
359 char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
360 strcpy (buf + i + 1, sfname);
361 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
362 Note we start at the end with the final package dot. */
363 for (; i >= 0; i--)
365 char c = class_name[i];
366 if (c == '.')
367 c = DIR_SEPARATOR;
368 buf[i] = c;
370 sfname_id = get_identifier (buf);
371 free (buf);
372 sfname = IDENTIFIER_POINTER (sfname_id);
376 sfname = find_sourcefile (sfname);
377 ORDINARY_MAP_FILE_NAME (LINEMAPS_LAST_ORDINARY_MAP (line_table)) = sfname;
378 if (current_class == main_class) main_input_filename = sfname;
384 /* Annotation handling.
386 The technique we use here is to copy the annotation data directly
387 from the input class file into the output file. We don't decode the
388 data at all, merely rewriting constant indexes whenever we come
389 across them: this is necessary because the constant pool in the
390 output file isn't the same as the constant pool in the input.
392 The main advantage of this technique is that the resulting
393 annotation data is pointer-free, so it doesn't have to be relocated
394 at startup time. As a consequence of this, annotations have no
395 performance impact unless they are used. Also, this representation
396 is very dense. */
399 /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
400 the start of the newly allocated region. */
402 static unsigned char*
403 annotation_grow (int delta)
405 unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
406 long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
407 long len = *datasize;
409 if (*data == NULL)
411 *data = XNEWVAR (unsigned char, delta);
413 else
415 int newlen = *datasize + delta;
416 if (floor_log2 (newlen) != floor_log2 (*datasize))
417 *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
419 *datasize += delta;
420 return *data + len;
423 /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
424 byte order (i.e. big endian.) */
426 static void
427 annotation_rewrite_byte (unsigned int n, unsigned char *p)
429 p[0] = n;
432 static void
433 annotation_rewrite_short (unsigned int n, unsigned char *p)
435 p[0] = n>>8;
436 p[1] = n;
439 static void
440 annotation_rewrite_int (unsigned int n, unsigned char *p)
442 p[0] = n>>24;
443 p[1] = n>>16;
444 p[2] = n>>8;
445 p[3] = n;
448 /* Read a 16-bit unsigned int in Java byte order (i.e. big
449 endian.) */
451 static uint16
452 annotation_read_short (unsigned char *p)
454 uint16 tmp = p[0];
455 tmp = (tmp << 8) | p[1];
456 return tmp;
459 /* annotation_write_TYPE. Rewrite various int types, appending them
460 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
461 endian.) */
463 static void
464 annotation_write_byte (unsigned int n)
466 annotation_rewrite_byte (n, annotation_grow (1));
469 static void
470 annotation_write_short (unsigned int n)
472 annotation_rewrite_short (n, annotation_grow (2));
475 static void
476 annotation_write_int (unsigned int n)
478 annotation_rewrite_int (n, annotation_grow (4));
481 /* Create a 64-bit constant in the constant pool.
483 This is used for both integer and floating-point types. As a
484 consequence, it will not work if the target floating-point format
485 is anything other than IEEE-754. While this is arguably a bug, the
486 runtime library makes exactly the same assumption and it's unlikely
487 that Java will ever run on a non-IEEE machine. */
489 static int
490 handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
491 int index, bool big_endian)
493 /* If we're on a 64-bit platform we can fit a long or double
494 into the same space as a jword. */
495 if (POINTER_SIZE >= 64)
496 index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
498 /* In a compiled program the constant pool is in native word
499 order. How weird is that??? */
500 else if (big_endian)
501 index = find_constant2 (cpool, kind,
502 JPOOL_INT (jcf, index),
503 JPOOL_INT (jcf, index+1));
504 else
505 index = find_constant2 (cpool, kind,
506 JPOOL_INT (jcf, index+1),
507 JPOOL_INT (jcf, index));
509 return index;
512 /* Given a class file and an index into its constant pool, create an
513 entry in the outgoing constant pool for the same item. */
515 static uint16
516 handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
518 unsigned int kind;
519 CPool *cpool = cpool_for_class (output_class);
521 if (index == 0)
522 return 0;
524 if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
525 error ("<constant pool index %d not in range>", index);
527 kind = JPOOL_TAG (jcf, index);
529 if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
531 if (purpose == CONSTANT_Class
532 && kind == CONSTANT_Utf8)
534 else
535 error ("<constant pool index %d unexpected type", index);
538 switch (kind)
540 case CONSTANT_Class:
541 case CONSTANT_ResolvedClass:
543 /* For some reason I know not the what of, class names in
544 annotations are UTF-8 strings in the constant pool but
545 class names in EnclosingMethod attributes are real class
546 references. Set CONSTANT_LazyFlag here so that the VM
547 doesn't attempt to resolve them at class initialization
548 time. */
549 tree resolved_class, class_name;
550 resolved_class = get_class_constant (jcf, index);
551 class_name = build_internal_class_name (resolved_class);
552 index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
553 (unmangle_classname
554 (IDENTIFIER_POINTER(class_name),
555 IDENTIFIER_LENGTH(class_name))));
556 break;
558 case CONSTANT_Utf8:
560 tree utf8 = get_constant (jcf, index);
561 if (purpose == CONSTANT_Class)
562 /* Create a constant pool entry for a type signature. This
563 one has '.' rather than '/' because it isn't going into a
564 class file, it's going into a compiled object.
566 This has to match the logic in
567 _Jv_ClassReader::prepare_pool_entry(). */
568 utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
569 IDENTIFIER_LENGTH(utf8));
570 index = alloc_name_constant (kind, utf8);
572 break;
574 case CONSTANT_Long:
575 index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
576 targetm.words_big_endian ());
577 break;
579 case CONSTANT_Double:
580 index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
581 targetm.float_words_big_endian ());
582 break;
584 case CONSTANT_Float:
585 case CONSTANT_Integer:
586 index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
587 break;
589 case CONSTANT_NameAndType:
591 uint16 name = JPOOL_USHORT1 (jcf, index);
592 uint16 sig = JPOOL_USHORT2 (jcf, index);
593 uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
594 uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
595 jword new_index = (name_index << 16) | sig_index;
596 index = find_constant1 (cpool, kind, new_index);
598 break;
600 default:
601 abort ();
604 return index;
607 /* Read an element_value structure from an annotation in JCF. Return
608 the constant pool index for the resulting constant pool entry. */
610 static int
611 handle_element_value (JCF *jcf, int level)
613 uint8 tag = JCF_readu (jcf);
614 int index = 0;
616 annotation_write_byte (tag);
617 switch (tag)
619 case 'B':
620 case 'C':
621 case 'S':
622 case 'Z':
623 case 'I':
625 uint16 cindex = JCF_readu2 (jcf);
626 index = handle_constant (jcf, cindex,
627 CONSTANT_Integer);
628 annotation_write_short (index);
630 break;
631 case 'D':
633 uint16 cindex = JCF_readu2 (jcf);
634 index = handle_constant (jcf, cindex,
635 CONSTANT_Double);
636 annotation_write_short (index);
638 break;
639 case 'F':
641 uint16 cindex = JCF_readu2 (jcf);
642 index = handle_constant (jcf, cindex,
643 CONSTANT_Float);
644 annotation_write_short (index);
646 break;
647 case 'J':
649 uint16 cindex = JCF_readu2 (jcf);
650 index = handle_constant (jcf, cindex,
651 CONSTANT_Long);
652 annotation_write_short (index);
654 break;
655 case 's':
657 uint16 cindex = JCF_readu2 (jcf);
658 /* Despite what the JVM spec says, compilers generate a Utf8
659 constant here, not a String. */
660 index = handle_constant (jcf, cindex,
661 CONSTANT_Utf8);
662 annotation_write_short (index);
664 break;
666 case 'e':
668 uint16 type_name_index = JCF_readu2 (jcf);
669 uint16 const_name_index = JCF_readu2 (jcf);
670 index = handle_constant (jcf, type_name_index,
671 CONSTANT_Class);
672 annotation_write_short (index);
673 index = handle_constant (jcf, const_name_index,
674 CONSTANT_Utf8);
675 annotation_write_short (index);
677 break;
678 case 'c':
680 uint16 class_info_index = JCF_readu2 (jcf);
681 index = handle_constant (jcf, class_info_index,
682 CONSTANT_Class);
683 annotation_write_short (index);
685 break;
686 case '@':
688 handle_annotation (jcf, level + 1);
690 break;
691 case '[':
693 uint16 n_array_elts = JCF_readu2 (jcf);
694 annotation_write_short (n_array_elts);
695 while (n_array_elts--)
696 handle_element_value (jcf, level + 1);
698 break;
699 default:
700 abort();
701 break;
703 return index;
706 /* Read an annotation structure from JCF. Write it to the
707 reflection_data field of the outgoing class. */
709 static void
710 handle_annotation (JCF *jcf, int level)
712 uint16 type_index = JCF_readu2 (jcf);
713 uint16 npairs = JCF_readu2 (jcf);
714 int index = handle_constant (jcf, type_index,
715 CONSTANT_Class);
716 annotation_write_short (index);
717 annotation_write_short (npairs);
718 while (npairs--)
720 uint16 name_index = JCF_readu2 (jcf);
721 index = handle_constant (jcf, name_index,
722 CONSTANT_Utf8);
723 annotation_write_short (index);
724 handle_element_value (jcf, level + 2);
728 /* Read an annotation count from JCF, and write the following
729 annotations to the reflection_data field of the outgoing class. */
731 static void
732 handle_annotations (JCF *jcf, int level)
734 uint16 num = JCF_readu2 (jcf);
735 annotation_write_short (num);
736 while (num--)
737 handle_annotation (jcf, level);
740 /* As handle_annotations(), but perform a sanity check that we write
741 the same number of bytes that we were expecting. */
743 static void
744 handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
745 long length)
747 long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
749 handle_annotations (jcf, 0);
751 gcc_assert (old_datasize + length
752 == TYPE_REFLECTION_DATASIZE (current_class));
755 /* gcj permutes its fields array after generating annotation_data, so
756 we have to fixup field indexes for fields that have moved. Given
757 ARG, a VEC_int, fixup the field indexes in the reflection_data of
758 the outgoing class. We use field_offsets to tell us where the
759 fixups must go. */
761 void
762 rewrite_reflection_indexes (void *arg)
764 bitmap_iterator bi;
765 unsigned int offset;
766 vec<int> *map = (vec<int> *) arg;
767 unsigned char *data = TYPE_REFLECTION_DATA (current_class);
769 if (map)
771 EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
773 uint16 index = annotation_read_short (data + offset);
774 annotation_rewrite_short
775 ((*map)[index], data + offset);
780 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
781 reflection_data of the outgoing class. */
783 static void
784 handle_member_annotations (int member_index, JCF *jcf,
785 const unsigned char *name ATTRIBUTE_UNUSED,
786 long len, jv_attr_type member_type)
788 int new_len = len + 1;
789 annotation_write_byte (member_type);
790 if (member_type != JV_CLASS_ATTR)
791 new_len += 2;
792 annotation_write_int (new_len);
793 annotation_write_byte (JV_ANNOTATIONS_KIND);
794 if (member_type == JV_FIELD_ATTR)
795 bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
796 if (member_type != JV_CLASS_ATTR)
797 annotation_write_short (member_index);
798 handle_annotation_attribute (member_index, jcf, len);
801 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
802 to the reflection_data of the outgoing class. */
804 static void
805 handle_parameter_annotations (int member_index, JCF *jcf,
806 const unsigned char *name ATTRIBUTE_UNUSED,
807 long len, jv_attr_type member_type)
809 int new_len = len + 1;
810 uint8 num;
811 annotation_write_byte (member_type);
812 if (member_type != JV_CLASS_ATTR)
813 new_len += 2;
814 annotation_write_int (new_len);
815 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
816 if (member_type != JV_CLASS_ATTR)
817 annotation_write_short (member_index);
818 num = JCF_readu (jcf);
819 annotation_write_byte (num);
820 while (num--)
821 handle_annotations (jcf, 0);
825 /* Read the AnnotationDefault data from JCF and write them to the
826 reflection_data of the outgoing class. */
828 static void
829 handle_default_annotation (int member_index, JCF *jcf,
830 const unsigned char *name ATTRIBUTE_UNUSED,
831 long len, jv_attr_type member_type)
833 int new_len = len + 1;
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_ANNOTATION_DEFAULT_KIND);
839 if (member_type != JV_CLASS_ATTR)
840 annotation_write_short (member_index);
841 handle_element_value (jcf, 0);
844 /* As above, for the EnclosingMethod attribute. */
846 static void
847 handle_enclosingmethod_attribute (int member_index, JCF *jcf,
848 const unsigned char *name ATTRIBUTE_UNUSED,
849 long len, jv_attr_type member_type)
851 int new_len = len + 1;
852 uint16 index;
853 annotation_write_byte (member_type);
854 if (member_type != JV_CLASS_ATTR)
855 new_len += 2;
856 annotation_write_int (new_len);
857 annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
858 if (member_type != JV_CLASS_ATTR)
859 annotation_write_short (member_index);
861 index = JCF_readu2 (jcf);
862 index = handle_constant (jcf, index, CONSTANT_Class);
863 annotation_write_short (index);
865 index = JCF_readu2 (jcf);
866 index = handle_constant (jcf, index, CONSTANT_NameAndType);
867 annotation_write_short (index);
870 /* As above, for the Signature attribute. */
872 static void
873 handle_signature_attribute (int member_index, JCF *jcf,
874 const unsigned char *name ATTRIBUTE_UNUSED,
875 long len, jv_attr_type member_type)
877 int new_len = len + 1;
878 uint16 index;
879 annotation_write_byte (member_type);
880 if (member_type != JV_CLASS_ATTR)
881 new_len += 2;
882 annotation_write_int (new_len);
883 annotation_write_byte (JV_SIGNATURE_KIND);
884 if (member_type != JV_CLASS_ATTR)
885 annotation_write_short (member_index);
887 index = JCF_readu2 (jcf);
888 index = handle_constant (jcf, index, CONSTANT_Utf8);
889 annotation_write_short (index);
894 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
896 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
897 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
898 output_class = current_class = give_name_to_class (jcf, THIS); \
899 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
901 #define HANDLE_CLASS_INTERFACE(INDEX) \
902 add_interface (current_class, get_class_constant (jcf, INDEX))
904 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
905 { int sig_index = SIGNATURE; \
906 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
907 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
908 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
909 if ((ACCESS_FLAGS) & ACC_FINAL) \
910 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
913 #define HANDLE_END_FIELDS() \
914 (current_field = NULL_TREE)
916 #define HANDLE_CONSTANTVALUE(INDEX) \
917 { tree constant; int index = INDEX; \
918 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
919 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
920 constant = build_utf8_ref (name); \
922 else \
923 constant = get_constant (jcf, index); \
924 set_constant_value (current_field, constant); }
926 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
927 (current_method = add_method (current_class, ACCESS_FLAGS, \
928 get_name_constant (jcf, NAME), \
929 get_name_constant (jcf, SIGNATURE)), \
930 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
931 DECL_LINENUMBERS_OFFSET (current_method) = 0)
933 #define HANDLE_END_METHODS() \
934 { current_method = NULL_TREE; }
936 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
937 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
938 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
939 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
940 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
942 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
943 { int n = (COUNT); \
944 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
945 JCF_SKIP (jcf, n * 10); }
947 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
948 { int n = (COUNT); \
949 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
950 JCF_SKIP (jcf, n * 4); }
952 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
954 int n = COUNT; \
955 vec<tree, va_gc> *v; \
956 vec_alloc (v, n); \
957 gcc_assert (!DECL_FUNCTION_THROWS (current_method)); \
958 while (--n >= 0) \
960 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
961 v->quick_push (thrown_class); \
963 DECL_FUNCTION_THROWS (current_method) = v; \
966 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
968 /* Link seen inner classes to their outer context and register the
969 inner class to its outer context. They will be later loaded. */
970 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
971 handle_innerclass_attribute (COUNT, jcf, attribute_length)
973 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
975 /* Irrelevant decls should have been nullified by the END macros. \
976 DECL_ARTIFICIAL on fields is used for something else (See \
977 PUSH_FIELD in java-tree.h) */ \
978 if (current_method) \
979 DECL_ARTIFICIAL (current_method) = 1; \
980 else if (current_field) \
981 FIELD_SYNTHETIC (current_field) = 1; \
982 else \
983 TYPE_SYNTHETIC (current_class) = 1; \
986 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
988 if (current_class == object_type_node) \
989 jcf->right_zip = 1; \
992 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
994 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
997 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
999 JCF_SKIP(jcf, attribute_length); \
1002 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
1004 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
1007 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
1009 JCF_SKIP(jcf, attribute_length); \
1012 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
1014 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
1017 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
1019 handle_enclosingmethod_attribute (index, jcf, name_data, \
1020 attribute_length, attr_type); \
1023 #define HANDLE_SIGNATURE_ATTRIBUTE() \
1025 handle_signature_attribute (index, jcf, name_data, \
1026 attribute_length, attr_type); \
1029 #include "jcf-reader.c"
1031 tree
1032 parse_signature (JCF *jcf, int sig_index)
1034 gcc_assert (sig_index > 0
1035 && sig_index < JPOOL_SIZE (jcf)
1036 && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1038 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1039 JPOOL_UTF_LENGTH (jcf, sig_index));
1042 tree
1043 get_constant (JCF *jcf, int index)
1045 tree value;
1046 int tag;
1047 if (index <= 0 || index >= JPOOL_SIZE(jcf))
1048 goto bad;
1049 tag = JPOOL_TAG (jcf, index);
1050 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
1051 return jcf->cpool.data[index].t;
1052 switch (tag)
1054 case CONSTANT_Integer:
1056 jint num = JPOOL_INT(jcf, index);
1057 value = build_int_cst (int_type_node, num);
1058 break;
1060 case CONSTANT_Long:
1062 unsigned HOST_WIDE_INT num;
1064 num = JPOOL_UINT (jcf, index);
1065 wide_int val = wi::lshift (wide_int::from (num, 64, SIGNED), 32);
1066 num = JPOOL_UINT (jcf, index + 1);
1067 val |= num;
1069 value = wide_int_to_tree (long_type_node, val);
1070 break;
1073 case CONSTANT_Float:
1075 jint num = JPOOL_INT(jcf, index);
1076 long buf = num;
1077 REAL_VALUE_TYPE d;
1079 real_from_target_fmt (&d, &buf, &ieee_single_format);
1080 value = build_real (float_type_node, d);
1081 break;
1084 case CONSTANT_Double:
1086 long buf[2], lo, hi;
1087 REAL_VALUE_TYPE d;
1089 hi = JPOOL_UINT (jcf, index);
1090 lo = JPOOL_UINT (jcf, index+1);
1092 if (targetm.float_words_big_endian ())
1093 buf[0] = hi, buf[1] = lo;
1094 else
1095 buf[0] = lo, buf[1] = hi;
1097 real_from_target_fmt (&d, buf, &ieee_double_format);
1098 value = build_real (double_type_node, d);
1099 break;
1102 case CONSTANT_String:
1104 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
1105 const char *utf8_ptr = IDENTIFIER_POINTER (name);
1106 int utf8_len = IDENTIFIER_LENGTH (name);
1107 const unsigned char *utf8;
1108 int i;
1110 /* Check for a malformed Utf8 string. */
1111 utf8 = (const unsigned char *) utf8_ptr;
1112 i = utf8_len;
1113 while (i > 0)
1115 int char_len = UT8_CHAR_LENGTH (*utf8);
1116 if (char_len < 0 || char_len > 3 || char_len > i)
1117 fatal_error (input_location, "bad string constant");
1119 utf8 += char_len;
1120 i -= char_len;
1123 /* Allocate a new string value. */
1124 value = build_string (utf8_len, utf8_ptr);
1125 TREE_TYPE (value) = build_pointer_type (string_type_node);
1127 break;
1128 default:
1129 goto bad;
1131 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
1132 jcf->cpool.data[index].t = value;
1133 return value;
1134 bad:
1135 fatal_error (input_location, "bad value constant type %d, index %d",
1136 JPOOL_TAG (jcf, index), index);
1139 tree
1140 get_name_constant (JCF *jcf, int index)
1142 tree name = get_constant (jcf, index);
1143 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1144 return name;
1147 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1148 a non anonymous entry) is found, We augment the inner class list of
1149 the outer context with the newly resolved innerclass. */
1151 static void
1152 handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
1154 int c = count;
1156 annotation_write_byte (JV_CLASS_ATTR);
1157 annotation_write_int (attribute_length+1);
1158 annotation_write_byte (JV_INNER_CLASSES_KIND);
1159 annotation_write_short (count);
1161 while (c--)
1163 /* Read inner_class_info_index. This may be 0 */
1164 int icii = JCF_readu2 (jcf);
1165 /* Read outer_class_info_index. If the innerclasses attribute
1166 entry isn't a member (like an inner class) the value is 0. */
1167 int ocii = JCF_readu2 (jcf);
1168 /* Read inner_name_index. If the class we're dealing with is
1169 an anonymous class, it must be 0. */
1170 int ini = JCF_readu2 (jcf);
1171 /* Read the access flag. */
1172 int acc = JCF_readu2 (jcf);
1174 annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1175 annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1176 annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1177 annotation_write_short (acc);
1179 /* If icii is 0, don't try to read the class. */
1180 if (icii >= 0)
1182 tree klass = get_class_constant (jcf, icii);
1183 tree decl = TYPE_NAME (klass);
1184 /* Skip reading further if ocii is null */
1185 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1187 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1188 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
1189 set_class_decl_access_flags (acc, decl);
1190 DECL_CONTEXT (decl) = outer;
1191 DECL_INNER_CLASS_LIST (outer) =
1192 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1193 CLASS_COMPLETE_P (decl) = 1;
1199 static tree
1200 give_name_to_class (JCF *jcf, int i)
1202 gcc_assert (i > 0
1203 && i < JPOOL_SIZE (jcf)
1204 && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1207 tree package_name = NULL_TREE, tmp;
1208 tree this_class;
1209 int j = JPOOL_USHORT1 (jcf, i);
1210 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1211 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
1212 JPOOL_UTF_LENGTH (jcf, j));
1213 this_class = lookup_class (class_name);
1215 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
1216 const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
1217 linemap_add (line_table, LC_ENTER, false, sfname, 0);
1218 input_location = linemap_line_start (line_table, 0, 1);
1219 file_start_location = input_location;
1220 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1221 if (main_input_filename == NULL && jcf == main_jcf)
1222 main_input_filename = sfname;
1225 jcf->cpool.data[i].t = this_class;
1226 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1227 split_qualified_name (&package_name, &tmp,
1228 DECL_NAME (TYPE_NAME (this_class)));
1229 TYPE_PACKAGE (this_class) = package_name;
1230 return this_class;
1234 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1236 tree
1237 get_class_constant (JCF *jcf, int i)
1239 tree type;
1240 gcc_assert (i > 0
1241 && i < JPOOL_SIZE (jcf)
1242 && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
1244 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1246 int name_index = JPOOL_USHORT1 (jcf, i);
1247 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1248 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
1249 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
1251 if (name[0] == '[') /* Handle array "classes". */
1252 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
1253 else
1255 tree cname = unmangle_classname (name, nlength);
1256 type = lookup_class (cname);
1258 jcf->cpool.data[i].t = type;
1259 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1261 else
1262 type = jcf->cpool.data[i].t;
1263 return type;
1266 /* Read a class with the fully qualified-name NAME.
1267 Return 1 iff we read the requested file.
1268 (It is still possible we failed if the file did not
1269 define the class it is supposed to.) */
1272 read_class (tree name)
1274 JCF this_jcf, *jcf;
1275 tree icv, klass = NULL_TREE;
1276 tree save_current_class = current_class;
1277 tree save_output_class = output_class;
1278 location_t save_location = input_location;
1279 JCF *save_current_jcf = current_jcf;
1281 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
1283 klass = TREE_TYPE (icv);
1284 jcf = TYPE_JCF (klass);
1286 else
1287 jcf = NULL;
1289 if (jcf == NULL)
1291 const char* path_name;
1292 this_jcf.zipd = NULL;
1293 jcf = &this_jcf;
1295 path_name = find_class (IDENTIFIER_POINTER (name),
1296 IDENTIFIER_LENGTH (name),
1297 &this_jcf);
1298 if (path_name == 0)
1299 return 0;
1300 else
1301 free(CONST_CAST (char *, path_name));
1304 current_jcf = jcf;
1306 if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
1308 output_class = current_class = klass;
1309 if (JCF_SEEN_IN_ZIP (current_jcf))
1310 read_zip_member(current_jcf,
1311 current_jcf->zipd, current_jcf->zipd->zipf);
1312 jcf_parse (current_jcf);
1313 /* Parsing might change the class, in which case we have to
1314 put it back where we found it. */
1315 if (current_class != klass && icv != NULL_TREE)
1316 TREE_TYPE (icv) = current_class;
1317 klass = current_class;
1319 layout_class (klass);
1320 load_inner_classes (klass);
1322 output_class = save_output_class;
1323 current_class = save_current_class;
1324 input_location = save_location;
1325 current_jcf = save_current_jcf;
1326 return 1;
1329 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1330 called from the parser, otherwise it's a RECORD_TYPE node. If
1331 VERBOSE is 1, print error message on failure to load a class. */
1332 void
1333 load_class (tree class_or_name, int verbose)
1335 tree name, saved;
1336 int class_loaded = 0;
1337 tree class_decl = NULL_TREE;
1338 bool is_compiled_class = false;
1340 /* We've already failed, don't try again. */
1341 if (TREE_CODE (class_or_name) == RECORD_TYPE
1342 && TYPE_DUMMY (class_or_name))
1343 return;
1345 /* class_or_name can be the name of the class we want to load */
1346 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1347 name = class_or_name;
1348 /* In some cases, it's a dependency that we process earlier that
1349 we though */
1350 else if (TREE_CODE (class_or_name) == TREE_LIST)
1351 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1352 /* Or it's a type in the making */
1353 else
1354 name = DECL_NAME (TYPE_NAME (class_or_name));
1356 class_decl = IDENTIFIER_CLASS_VALUE (name);
1357 if (class_decl != NULL_TREE)
1359 tree type = TREE_TYPE (class_decl);
1360 is_compiled_class
1361 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1362 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1365 saved = name;
1367 /* If flag_verify_invocations is unset, we don't try to load a class
1368 unless we're looking for Object (which is fixed by the ABI) or
1369 it's a class that we're going to compile. */
1370 if (flag_verify_invocations
1371 || class_or_name == object_type_node
1372 || is_compiled_class
1373 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1375 while (1)
1377 const char *separator;
1379 /* We've already loaded it. */
1380 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1382 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1383 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1384 break;
1387 if (read_class (name))
1388 break;
1390 /* We failed loading name. Now consider that we might be looking
1391 for an inner class. */
1392 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1393 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
1394 name = get_identifier_with_length (IDENTIFIER_POINTER (name),
1395 (separator
1396 - IDENTIFIER_POINTER (name)));
1397 /* Otherwise, we failed, we bail. */
1398 else
1399 break;
1403 /* have we found the class we're looking for? */
1404 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1405 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1406 class_loaded = type && CLASS_PARSED_P (type);
1410 if (!class_loaded)
1412 if (flag_verify_invocations || ! flag_indirect_dispatch)
1414 if (verbose)
1415 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1417 else if (verbose)
1419 /* This is just a diagnostic during testing, not a real problem. */
1420 if (!quiet_flag)
1421 warning (0, "cannot find file for class %s",
1422 IDENTIFIER_POINTER (saved));
1424 /* Fake it. */
1425 if (TREE_CODE (class_or_name) == RECORD_TYPE)
1427 set_super_info (0, class_or_name, object_type_node, 0);
1428 TYPE_DUMMY (class_or_name) = 1;
1429 /* We won't be able to output any debug info for this class. */
1430 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1436 /* Parse the .class file JCF. */
1438 static void
1439 jcf_parse (JCF* jcf)
1441 int i, code;
1443 bitmap_clear (field_offsets);
1445 if (jcf_parse_preamble (jcf) != 0)
1446 fatal_error (input_location, "not a valid Java .class file");
1447 code = jcf_parse_constant_pool (jcf);
1448 if (code != 0)
1449 fatal_error (input_location, "error while parsing constant pool");
1450 code = verify_constant_pool (jcf);
1451 if (code > 0)
1452 fatal_error (input_location, "error in constant pool entry #%d\n", code);
1454 jcf_parse_class (jcf);
1455 if (main_class == NULL_TREE)
1456 main_class = current_class;
1457 if (! quiet_flag && TYPE_NAME (current_class))
1458 fprintf (stderr, " %s %s",
1459 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
1460 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
1461 if (CLASS_PARSED_P (current_class))
1463 /* FIXME - where was first time */
1464 fatal_error (input_location,
1465 "reading class %s for the second time from %s",
1466 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1467 jcf->filename);
1469 CLASS_PARSED_P (current_class) = 1;
1471 for (i = 1; i < JPOOL_SIZE(jcf); i++)
1473 switch (JPOOL_TAG (jcf, i))
1475 case CONSTANT_Class:
1476 get_class_constant (jcf, i);
1477 break;
1481 code = jcf_parse_fields (jcf);
1482 if (code != 0)
1483 fatal_error (input_location, "error while parsing fields");
1484 code = jcf_parse_methods (jcf);
1485 if (code != 0)
1486 fatal_error (input_location, "error while parsing methods");
1487 code = jcf_parse_final_attributes (jcf);
1488 if (code != 0)
1489 fatal_error (input_location, "error while parsing final attributes");
1491 if (TYPE_REFLECTION_DATA (current_class))
1492 annotation_write_byte (JV_DONE_ATTR);
1494 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1496 /* The fields of class_type_node are already in correct order. */
1497 if (current_class != class_type_node && current_class != object_type_node)
1498 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1500 if (current_class == object_type_node)
1501 layout_class_methods (object_type_node);
1502 else
1503 vec_safe_push (all_class_list, TYPE_NAME (current_class));
1506 /* If we came across inner classes, load them now. */
1507 static void
1508 load_inner_classes (tree cur_class)
1510 tree current;
1511 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
1512 current = TREE_CHAIN (current))
1514 tree name = DECL_NAME (TREE_PURPOSE (current));
1515 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1516 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1517 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
1518 load_class (name, 1);
1522 static void
1523 duplicate_class_warning (const char *filename)
1525 location_t warn_loc;
1526 linemap_add (line_table, LC_RENAME, 0, filename, 0);
1527 warn_loc = linemap_line_start (line_table, 0, 1);
1528 warning_at (warn_loc, 0, "duplicate class will only be compiled once");
1531 static void
1532 java_layout_seen_class_methods (void)
1534 unsigned start = 0;
1535 unsigned end = vec_safe_length (all_class_list);
1537 while (1)
1539 unsigned ix;
1540 unsigned new_length;
1542 for (ix = start; ix != end; ix++)
1544 tree decl = (*all_class_list)[ix];
1545 tree cls = TREE_TYPE (decl);
1547 input_location = DECL_SOURCE_LOCATION (decl);
1549 if (! CLASS_LOADED_P (cls))
1550 load_class (cls, 0);
1552 layout_class_methods (cls);
1555 /* Note that new classes might have been added while laying out
1556 methods, changing the value of all_class_list. */
1557 new_length = vec_safe_length (all_class_list);
1558 if (end != new_length)
1560 start = end;
1561 end = new_length;
1563 else
1564 break;
1568 static void
1569 parse_class_file (void)
1571 tree method;
1572 location_t save_location = input_location;
1574 java_layout_seen_class_methods ();
1576 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1578 /* Re-enter the current file. */
1579 expanded_location loc = expand_location (input_location);
1580 linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1582 file_start_location = input_location;
1583 (*debug_hooks->start_source_file) (LOCATION_LINE (input_location),
1584 LOCATION_FILE (input_location));
1586 java_mark_class_local (current_class);
1588 gen_indirect_dispatch_tables (current_class);
1590 for (method = TYPE_METHODS (current_class);
1591 method != NULL_TREE; method = DECL_CHAIN (method))
1593 JCF *jcf = current_jcf;
1595 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
1596 continue;
1598 if (METHOD_NATIVE (method))
1600 tree arg;
1601 int decl_max_locals;
1603 if (! flag_jni)
1604 continue;
1605 /* We need to compute the DECL_MAX_LOCALS. We need to take
1606 the wide types into account too. */
1607 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
1608 arg != end_params_node;
1609 arg = TREE_CHAIN (arg), decl_max_locals += 1)
1611 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1612 decl_max_locals += 1;
1614 DECL_MAX_LOCALS (method) = decl_max_locals;
1615 start_java_method (method);
1616 give_name_to_locals (jcf);
1617 *get_stmts () = build_jni_stub (method);
1618 end_java_method ();
1619 continue;
1622 if (DECL_CODE_OFFSET (method) == 0)
1624 current_function_decl = method;
1625 error ("missing Code attribute");
1626 continue;
1629 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1630 if (DECL_LINENUMBERS_OFFSET (method))
1632 int i;
1633 int min_line = 0;
1634 unsigned char *ptr;
1635 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1636 linenumber_count = i = JCF_readu2 (jcf);
1637 linenumber_table = ptr = jcf->read_ptr;
1639 for (ptr += 2; --i >= 0; ptr += 4)
1641 int line = GET_u2 (ptr);
1642 /* Set initial line of input_location to smallest
1643 * linenumber.
1644 * Needs to be set before init_function_start. */
1645 if (min_line == 0 || line < min_line)
1646 min_line = line;
1648 if (min_line != 0)
1649 input_location = linemap_line_start (line_table, min_line, 1);
1651 else
1653 linenumber_table = NULL;
1654 linenumber_count = 0;
1657 start_java_method (method);
1659 note_instructions (jcf, method);
1661 give_name_to_locals (jcf);
1663 /* Bump up start_label_pc_this_method so we get a unique label number
1664 and reset highest_label_pc_this_method. */
1665 if (highest_label_pc_this_method >= 0)
1667 /* We adjust to the next multiple of 1000. This is just a frill
1668 so the last 3 digits of the label number match the bytecode
1669 offset, which might make debugging marginally more convenient. */
1670 start_label_pc_this_method
1671 = ((((start_label_pc_this_method + highest_label_pc_this_method)
1672 / 1000)
1673 + 1)
1674 * 1000);
1675 highest_label_pc_this_method = -1;
1678 /* Convert bytecode to trees. */
1679 expand_byte_code (jcf, method);
1681 end_java_method ();
1684 finish_class ();
1686 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
1687 input_location = save_location;
1690 static vec<tree, va_gc> *predefined_filenames;
1692 void
1693 add_predefined_file (tree name)
1695 vec_safe_push (predefined_filenames, name);
1699 predefined_filename_p (tree node)
1701 unsigned ix;
1702 tree f;
1704 FOR_EACH_VEC_SAFE_ELT (predefined_filenames, ix, f)
1705 if (f == node)
1706 return 1;
1708 return 0;
1711 /* Generate a function that does all static initialization for this
1712 translation unit. */
1714 static void
1715 java_emit_static_constructor (void)
1717 tree body = NULL;
1719 emit_register_classes (&body);
1720 write_resource_constructor (&body);
1722 if (body)
1724 tree name = get_identifier ("_Jv_global_static_constructor");
1726 tree decl
1727 = build_decl (input_location, FUNCTION_DECL, name,
1728 build_function_type_list (void_type_node, NULL_TREE));
1730 tree resdecl = build_decl (input_location,
1731 RESULT_DECL, NULL_TREE, void_type_node);
1732 DECL_ARTIFICIAL (resdecl) = 1;
1733 DECL_RESULT (decl) = resdecl;
1734 current_function_decl = decl;
1735 allocate_struct_function (decl, false);
1737 TREE_STATIC (decl) = 1;
1738 TREE_USED (decl) = 1;
1739 DECL_ARTIFICIAL (decl) = 1;
1740 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1741 DECL_SAVED_TREE (decl) = body;
1742 DECL_UNINLINABLE (decl) = 1;
1744 DECL_INITIAL (decl) = make_node (BLOCK);
1745 TREE_USED (DECL_INITIAL (decl)) = 1;
1747 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1748 java_genericize (decl);
1749 cgraph_node::finalize_function (decl, false);
1754 void
1755 java_parse_file (void)
1757 int filename_count = 0;
1758 location_t save_location = input_location;
1759 char *file_list = NULL, *list, *next;
1760 tree node;
1761 FILE *finput = NULL;
1762 int in_quotes = 0;
1763 unsigned ix;
1765 bitmap_obstack_initialize (&bit_obstack);
1766 field_offsets = BITMAP_ALLOC (&bit_obstack);
1768 if (flag_filelist_file)
1770 int avail = 2000;
1771 finput = fopen (main_input_filename, "r");
1772 if (finput == NULL)
1773 fatal_error (input_location,
1774 "can%'t open %s: %m", LOCATION_FILE (input_location));
1775 list = XNEWVEC (char, avail);
1776 next = list;
1777 for (;;)
1779 int count;
1780 if (avail < 500)
1782 count = next - list;
1783 avail = 2 * (count + avail);
1784 list = XRESIZEVEC (char, list, avail);
1785 next = list + count;
1786 avail = avail - count;
1788 /* Subtract one to guarantee space for final '\0'. */
1789 count = fread (next, 1, avail - 1, finput);
1790 if (count == 0)
1792 if (! feof (finput))
1793 fatal_error (input_location, "error closing %s: %m",
1794 LOCATION_FILE (input_location));
1795 *next = '\0';
1796 break;
1798 avail -= count;
1799 next += count;
1801 fclose (finput);
1802 finput = NULL;
1803 file_list = list;
1805 else
1806 list = CONST_CAST (char *, main_input_filename);
1808 while (list)
1810 for (next = list; ; )
1812 char ch = *next;
1813 if (flag_filelist_file && ! in_quotes
1814 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1815 || ch == '&') /* FIXME */)
1817 if (next == list)
1819 next++;
1820 list = next;
1821 continue;
1823 else
1825 *next++ = '\0';
1826 break;
1829 if (flag_filelist_file && ch == '"')
1831 in_quotes = ! in_quotes;
1832 *next++ = '\0';
1833 if (in_quotes)
1834 list = next;
1835 else
1836 break;
1838 if (ch == '\0')
1840 next = NULL;
1841 break;
1843 next++;
1846 /* Exclude .java files. */
1847 if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1849 /* Nothing. */
1851 else if (list[0])
1853 node = get_identifier (list);
1855 filename_count++;
1857 /* Exclude file that we see twice on the command line. */
1859 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1860 duplicate_class_warning (IDENTIFIER_POINTER (node));
1861 else
1863 build_translation_unit_decl (node);
1864 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1867 list = next;
1870 free (file_list);
1872 if (filename_count == 0)
1873 warning (0, "no input file specified");
1875 if (resource_name)
1877 const char *resource_filename;
1879 /* Only one resource file may be compiled at a time. */
1880 gcc_assert (all_translation_units->length () == 1);
1882 resource_filename
1883 = IDENTIFIER_POINTER (DECL_NAME ((*all_translation_units)[0]));
1884 compile_resource_file (resource_name, resource_filename);
1886 goto finish;
1889 current_jcf = main_jcf;
1890 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1892 unsigned char magic_string[4];
1893 char *real_path;
1894 uint32 magic = 0;
1895 tree name = DECL_NAME (node);
1896 tree real_file;
1897 const char *filename = IDENTIFIER_POINTER (name);
1899 /* Skip already parsed files */
1900 real_path = lrealpath (filename);
1901 real_file = get_identifier (real_path);
1902 free (real_path);
1903 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1904 continue;
1906 /* Close previous descriptor, if any */
1907 if (finput && fclose (finput))
1908 fatal_error (input_location,
1909 "can%'t close input file %s: %m", main_input_filename);
1911 finput = fopen (filename, "rb");
1912 if (finput == NULL)
1913 fatal_error (input_location, "can%'t open %s: %m", filename);
1915 #ifdef IO_BUFFER_SIZE
1916 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1917 _IOFBF, IO_BUFFER_SIZE);
1918 #endif
1920 /* Figure what kind of file we're dealing with */
1921 if (fread (magic_string, 1, 4, finput) == 4)
1923 fseek (finput, 0L, SEEK_SET);
1924 magic = GET_u4 (magic_string);
1926 if (magic == 0xcafebabe)
1928 CLASS_FILE_P (node) = 1;
1929 current_jcf = ggc_cleared_alloc<JCF> ();
1930 current_jcf->read_state = finput;
1931 current_jcf->filbuf = jcf_filbuf_from_stdio;
1932 jcf_parse (current_jcf);
1933 DECL_SOURCE_LOCATION (node) = file_start_location;
1934 TYPE_JCF (current_class) = current_jcf;
1935 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1937 /* We've already compiled this class. */
1938 duplicate_class_warning (filename);
1939 continue;
1941 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1942 TREE_TYPE (node) = current_class;
1944 else if (magic == (JCF_u4)ZIPMAGIC)
1946 main_jcf = ggc_cleared_alloc<JCF> ();
1947 main_jcf->read_state = finput;
1948 main_jcf->filbuf = jcf_filbuf_from_stdio;
1949 linemap_add (line_table, LC_ENTER, false, filename, 0);
1950 input_location = linemap_line_start (line_table, 0, 1);
1951 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1952 fatal_error (input_location, "bad zip/jar file %s", filename);
1953 localToFile = SeenZipFiles;
1954 /* Register all the classes defined there. */
1955 process_zip_dir ((FILE *) main_jcf->read_state);
1956 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1957 parse_zip_file_entries ();
1959 else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1961 /* Ignore an empty input jar. */
1963 else
1965 gcc_unreachable ();
1966 #if 0
1967 java_push_parser_context ();
1968 java_parser_context_save_global ();
1970 parse_source_file_1 (real_file, filename, finput);
1971 java_parser_context_restore_global ();
1972 java_pop_parser_context (1);
1973 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1974 #endif
1978 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1980 input_location = DECL_SOURCE_LOCATION (node);
1981 if (CLASS_FILE_P (node))
1983 /* FIXME: These two flags really should be independent. We
1984 should be able to compile fully binary compatible, but
1985 with flag_verify_invocations on. */
1986 flag_verify_invocations = ! flag_indirect_dispatch;
1987 output_class = current_class = TREE_TYPE (node);
1989 current_jcf = TYPE_JCF (current_class);
1990 layout_class (current_class);
1991 load_inner_classes (current_class);
1992 parse_class_file ();
1993 JCF_FINISH (current_jcf);
1996 input_location = save_location;
1998 bitmap_obstack_release (&bit_obstack);
2000 finish:
2001 /* Arrange for any necessary initialization to happen. */
2002 java_emit_static_constructor ();
2003 gcc_assert (global_bindings_p ());
2007 /* Return the name of the class corresponding to the name of the file
2008 in this zip entry. The result is newly allocated using ALLOC. */
2009 static char *
2010 compute_class_name (struct ZipDirectory *zdir)
2012 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2013 char *class_name;
2014 int i;
2015 int filename_length = zdir->filename_length;
2017 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
2019 class_name_in_zip_dir += 2;
2020 filename_length -= 2;
2023 filename_length -= strlen (".class");
2024 class_name = XNEWVEC (char, filename_length + 1);
2025 memcpy (class_name, class_name_in_zip_dir, filename_length);
2026 class_name [filename_length] = '\0';
2028 for (i = 0; i < filename_length; i++)
2029 if (class_name[i] == '/')
2030 class_name[i] = '.';
2032 return class_name;
2035 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2036 if it is a property file of some sort. */
2037 static int
2038 classify_zip_file (struct ZipDirectory *zdir)
2040 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2042 if (zdir->filename_length > 6
2043 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2044 ".class", 6))
2045 return 1;
2047 /* For now we drop the manifest, but not other information. */
2048 if (zdir->filename_length == 20
2049 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
2050 return 0;
2052 /* Drop directory entries. */
2053 if (zdir->filename_length > 0
2054 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2055 return 0;
2057 return 2;
2060 /* Process all class entries found in the zip file. */
2061 static void
2062 parse_zip_file_entries (void)
2064 struct ZipDirectory *zdir;
2065 int i;
2067 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2068 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2070 tree klass;
2072 switch (classify_zip_file (zdir))
2074 case 0:
2075 continue;
2077 case 1:
2079 char *class_name = compute_class_name (zdir);
2080 int previous_alias_set = -1;
2081 klass = lookup_class (get_identifier (class_name));
2082 FREE (class_name);
2083 current_jcf = TYPE_JCF (klass);
2084 output_class = current_class = klass;
2086 /* This is a dummy class, and now we're compiling it for
2087 real. */
2088 gcc_assert (! TYPE_DUMMY (klass));
2090 /* This is for a corner case where we have a superclass
2091 but no superclass fields.
2093 This can happen if we earlier failed to lay out this
2094 class because its superclass was still in the process
2095 of being laid out; this occurs when we have recursive
2096 class dependencies via inner classes. We must record
2097 the previous alias set and restore it after laying out
2098 the class.
2100 FIXME: this really is a kludge. We should figure out a
2101 way to lay out the class properly before this
2102 happens. */
2103 if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2104 && integer_zerop (TYPE_SIZE (klass)))
2106 TYPE_SIZE (klass) = NULL_TREE;
2107 previous_alias_set = TYPE_ALIAS_SET (klass);
2108 TYPE_ALIAS_SET (klass) = -1;
2111 if (! CLASS_LOADED_P (klass))
2113 if (! CLASS_PARSED_P (klass))
2115 read_zip_member (current_jcf, zdir, localToFile);
2116 jcf_parse (current_jcf);
2118 layout_class (current_class);
2119 load_inner_classes (current_class);
2122 if (previous_alias_set != -1)
2123 TYPE_ALIAS_SET (klass) = previous_alias_set;
2125 if (TYPE_SIZE (current_class) != error_mark_node)
2127 parse_class_file ();
2128 free (current_jcf->buffer); /* No longer necessary */
2129 /* Note: there is a way to free this buffer right after a
2130 class seen in a zip file has been parsed. The idea is the
2131 set its jcf in such a way that buffer will be reallocated
2132 the time the code for the class will be generated. FIXME. */
2135 break;
2137 case 2:
2139 char *file_name, *class_name_in_zip_dir, *buffer;
2140 JCF *jcf;
2141 file_name = XNEWVEC (char, zdir->filename_length + 1);
2142 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2143 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2144 file_name[zdir->filename_length] = '\0';
2145 jcf = XNEW (JCF);
2146 JCF_ZERO (jcf);
2147 jcf->read_state = finput;
2148 jcf->filbuf = jcf_filbuf_from_stdio;
2149 jcf->classname = NULL;
2150 jcf->filename = file_name;
2151 jcf->zipd = zdir;
2153 if (read_zip_member (jcf, zdir, localToFile) < 0)
2154 fatal_error (input_location,
2155 "error while reading %s from zip file", file_name);
2157 buffer = XNEWVEC (char, zdir->filename_length + 1 +
2158 (jcf->buffer_end - jcf->buffer));
2159 strcpy (buffer, file_name);
2160 /* This is not a typo: we overwrite the trailing \0 of the
2161 file name; this is just how the data is laid out. */
2162 memcpy (buffer + zdir->filename_length,
2163 jcf->buffer, jcf->buffer_end - jcf->buffer);
2165 compile_resource_data (file_name, buffer,
2166 jcf->buffer_end - jcf->buffer);
2167 JCF_FINISH (jcf);
2168 free (jcf);
2169 free (buffer);
2171 break;
2173 default:
2174 gcc_unreachable ();
2179 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
2180 jcf up for further processing and link it to the created class. */
2182 static void
2183 process_zip_dir (FILE *finput)
2185 int i;
2186 ZipDirectory *zdir;
2188 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2189 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2191 char *class_name, *file_name, *class_name_in_zip_dir;
2192 tree klass;
2193 JCF *jcf;
2195 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2197 /* Here we skip non-class files; we handle them later. */
2198 if (classify_zip_file (zdir) != 1)
2199 continue;
2201 class_name = compute_class_name (zdir);
2202 file_name = XNEWVEC (char, zdir->filename_length+1);
2203 jcf = ggc_cleared_alloc<JCF> ();
2205 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2206 file_name [zdir->filename_length] = '\0';
2208 klass = lookup_class (get_identifier (class_name));
2210 if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2212 /* We've already compiled this class. */
2213 duplicate_class_warning (file_name);
2214 continue;
2216 /* This function is only called when processing a zip file seen
2217 on the command line. */
2218 CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
2220 jcf->read_state = finput;
2221 jcf->filbuf = jcf_filbuf_from_stdio;
2222 jcf->classname = class_name;
2223 jcf->filename = file_name;
2224 jcf->zipd = zdir;
2226 TYPE_JCF (klass) = jcf;
2230 #include "gt-java-jcf-parse.h"
2231 #include "gtype-java.h"