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)
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> */
29 #include "coretypes.h"
33 #include "java-except.h"
36 #include "java-tree.h"
37 #include "diagnostic-core.h"
52 #ifdef HAVE_LANGINFO_CODESET
56 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
57 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
58 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
59 #define JPOOL_UTF_DATA(JCF, INDEX) \
60 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
61 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
63 unsigned char save; unsigned char *text; \
64 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
65 text = (JCF)->read_ptr; \
66 save = text[LENGTH]; \
68 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
69 text[LENGTH] = save; \
70 JCF_SKIP (JCF, LENGTH); } while (0)
74 extern struct obstack temporary_obstack;
76 static GTY(()) tree parse_roots[2];
78 /* The FIELD_DECL for the current field. */
79 #define current_field parse_roots[0]
81 /* The METHOD_DECL for the current method. */
82 #define current_method parse_roots[1]
84 /* Line 0 in current file, if compiling from bytecode. */
85 static location_t file_start_location;
87 /* The Java archive that provides main_class; the main input file. */
88 static GTY(()) struct JCF * main_jcf;
90 /* A list of all the class DECLs seen so far. */
91 static GTY(()) VEC(tree,gc) *all_class_list;
93 /* The number of source files passed to us by -fsource-filename and an
94 array of pointers to each name. Used by find_sourcefile(). */
95 static int num_files = 0;
96 static char **filenames;
98 static struct ZipFile *localToFile;
100 /* A map of byte offsets in the reflection data that are fields which
102 bitmap field_offsets;
103 bitmap_obstack bit_obstack;
105 /* Declarations of some functions used here. */
106 static void handle_innerclass_attribute (int count, JCF *, int len);
107 static tree give_name_to_class (JCF *jcf, int index);
108 static char *compute_class_name (struct ZipDirectory *zdir);
109 static int classify_zip_file (struct ZipDirectory *zdir);
110 static void parse_zip_file_entries (void);
111 static void process_zip_dir (FILE *);
112 static void parse_class_file (void);
113 static void handle_deprecated (void);
114 static void set_source_filename (JCF *, int);
115 static void jcf_parse (struct JCF*);
116 static void load_inner_classes (tree);
117 static void handle_annotation (JCF *jcf, int level);
118 static void java_layout_seen_class_methods (void);
120 /* Handle "Deprecated" attribute. */
122 handle_deprecated (void)
124 if (current_field != NULL_TREE)
125 FIELD_DEPRECATED (current_field) = 1;
126 else if (current_method != NULL_TREE)
127 METHOD_DEPRECATED (current_method) = 1;
128 else if (current_class != NULL_TREE)
129 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
132 /* Shouldn't happen. */
139 /* Reverse a string. */
141 reverse (const char *s)
147 int len = strlen (s);
148 char *d = XNEWVAR (char, len + 1);
153 for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
160 /* Compare two strings for qsort(). */
162 cmpstringp (const void *p1, const void *p2)
164 /* The arguments to this function are "pointers to
165 pointers to char", but strcmp() arguments are "pointers
166 to char", hence the following cast plus dereference */
168 return strcmp(*(const char *const*) p1, *(const char *const*) p2);
171 /* Create an array of strings, one for each source file that we've
172 seen. fsource_filename can either be the name of a single .java
173 file or a file that contains a list of filenames separated by
176 java_read_sourcefilenames (const char *fsource_filename)
180 && strlen (fsource_filename) > strlen (".java")
181 && strcmp ((fsource_filename
182 + strlen (fsource_filename)
186 /* fsource_filename isn't a .java file but a list of filenames
187 separated by newlines */
188 FILE *finput = fopen (fsource_filename, "r");
190 int longest_line = 0;
194 /* Find out how many files there are, and how long the filenames are. */
195 while (! feof (finput))
197 int ch = getc (finput);
201 if (len > longest_line)
213 /* Read the filenames. Put a pointer to each filename into the
216 char *linebuf = (char *) alloca (longest_line + 1);
220 filenames = XNEWVEC (char *, num_files);
225 int ch = getc (finput);
230 linebuf[charpos] = 0;
231 gcc_assert (i < num_files);
232 /* ??? Perhaps we should use lrealpath() here. Doing
233 so would tidy up things like /../ but the rest of
234 gcc seems to assume relative pathnames, not
235 absolute pathnames. */
236 /* realname = lrealpath (linebuf); */
237 filenames[i++] = reverse (linebuf);
241 gcc_assert (charpos < longest_line);
242 linebuf[charpos++] = ch;
246 qsort (filenames, num_files, sizeof (char *), cmpstringp);
252 filenames = XNEWVEC (char *, 1);
253 filenames[0] = reverse (fsource_filename);
258 /* Given a relative pathname such as foo/bar.java, attempt to find a
259 longer pathname with the same suffix.
261 This is a best guess heuristic; with some weird class hierarchies we
262 may fail to pick the correct source file. For example, if we have
263 the filenames foo/bar.java and also foo/foo/bar.java, we do not
264 have enough information to know which one is the right match for
268 find_sourcefile (const char *name)
270 int i = 0, j = num_files-1;
275 char *revname = reverse (name);
280 int cmp = strncmp (revname, filenames[k], strlen (revname));
283 /* OK, so we found one. But is it a unique match? */
285 && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
287 && (strncmp (revname, filenames[k+1], strlen (revname))
291 found = filenames[k];
304 if (found && strlen (found) > strlen (name))
305 return reverse (found);
312 /* Handle "SourceFile" attribute. */
315 set_source_filename (JCF *jcf, int index)
317 tree sfname_id = get_name_constant (jcf, index);
318 const char *sfname = IDENTIFIER_POINTER (sfname_id);
319 const char *old_filename = input_filename;
320 int new_len = IDENTIFIER_LENGTH (sfname_id);
321 if (old_filename != NULL)
323 int old_len = strlen (old_filename);
324 /* Use the current input_filename (derived from the class name)
325 if it has a directory prefix, but otherwise matches sfname. */
326 if (old_len > new_len
327 && strcmp (sfname, old_filename + old_len - new_len) == 0
328 && (old_filename[old_len - new_len - 1] == '/'
329 || old_filename[old_len - new_len - 1] == '\\'))
332 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
334 const char *class_name
335 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
336 const char *dot = strrchr (class_name, '.');
339 /* Length of prefix, not counting final dot. */
340 int i = dot - class_name;
341 /* Concatenate current package prefix with new sfname. */
342 char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
343 strcpy (buf + i + 1, sfname);
344 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
345 Note we start at the end with the final package dot. */
348 char c = class_name[i];
353 sfname_id = get_identifier (buf);
355 sfname = IDENTIFIER_POINTER (sfname_id);
359 sfname = find_sourcefile (sfname);
360 line_table->maps[line_table->used-1].to_file = sfname;
361 if (current_class == main_class) main_input_filename = sfname;
367 /* Annotation handling.
369 The technique we use here is to copy the annotation data directly
370 from the input class file into the output file. We don't decode the
371 data at all, merely rewriting constant indexes whenever we come
372 across them: this is necessary because the constant pool in the
373 output file isn't the same as the constant pool in in the input.
375 The main advantage of this technique is that the resulting
376 annotation data is pointer-free, so it doesn't have to be relocated
377 at startup time. As a consequence of this, annotations have no
378 performance impact unless they are used. Also, this representation
382 /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
383 the start of the newly allocated region. */
385 static unsigned char*
386 annotation_grow (int delta)
388 unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
389 long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
390 long len = *datasize;
394 *data = XNEWVAR (unsigned char, delta);
398 int newlen = *datasize + delta;
399 if (floor_log2 (newlen) != floor_log2 (*datasize))
400 *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
406 /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
407 byte order (i.e. big endian.) */
410 annotation_rewrite_byte (unsigned int n, unsigned char *p)
416 annotation_rewrite_short (unsigned int n, unsigned char *p)
423 annotation_rewrite_int (unsigned int n, unsigned char *p)
431 /* Read a 16-bit unsigned int in Java byte order (i.e. big
435 annotation_read_short (unsigned char *p)
438 tmp = (tmp << 8) | p[1];
442 /* annotation_write_TYPE. Rewrite various int types, appending them
443 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
447 annotation_write_byte (unsigned int n)
449 annotation_rewrite_byte (n, annotation_grow (1));
453 annotation_write_short (unsigned int n)
455 annotation_rewrite_short (n, annotation_grow (2));
459 annotation_write_int (unsigned int n)
461 annotation_rewrite_int (n, annotation_grow (4));
464 /* Create a 64-bit constant in the constant pool.
466 This is used for both integer and floating-point types. As a
467 consequence, it will not work if the target floating-point format
468 is anything other than IEEE-754. While this is arguably a bug, the
469 runtime library makes exactly the same assumption and it's unlikely
470 that Java will ever run on a non-IEEE machine. */
473 handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
474 int index, bool big_endian)
476 /* If we're on a 64-bit platform we can fit a long or double
477 into the same space as a jword. */
478 if (POINTER_SIZE >= 64)
479 index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
481 /* In a compiled program the constant pool is in native word
482 order. How weird is that??? */
484 index = find_constant2 (cpool, kind,
485 JPOOL_INT (jcf, index),
486 JPOOL_INT (jcf, index+1));
488 index = find_constant2 (cpool, kind,
489 JPOOL_INT (jcf, index+1),
490 JPOOL_INT (jcf, index));
495 /* Given a class file and an index into its constant pool, create an
496 entry in the outgoing constant pool for the same item. */
499 handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
502 CPool *cpool = cpool_for_class (output_class);
507 if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
508 error ("<constant pool index %d not in range>", index);
510 kind = JPOOL_TAG (jcf, index);
512 if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
514 if (purpose == CONSTANT_Class
515 && kind == CONSTANT_Utf8)
518 error ("<constant pool index %d unexpected type", index);
524 case CONSTANT_ResolvedClass:
526 /* For some reason I know not the what of, class names in
527 annotations are UTF-8 strings in the constant pool but
528 class names in EnclosingMethod attributes are real class
529 references. Set CONSTANT_LazyFlag here so that the VM
530 doesn't attempt to resolve them at class initialization
532 tree resolved_class, class_name;
533 resolved_class = get_class_constant (jcf, index);
534 class_name = build_internal_class_name (resolved_class);
535 index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
537 (IDENTIFIER_POINTER(class_name),
538 IDENTIFIER_LENGTH(class_name))));
543 tree utf8 = get_constant (jcf, index);
544 if (purpose == CONSTANT_Class)
545 /* Create a constant pool entry for a type signature. This
546 one has '.' rather than '/' because it isn't going into a
547 class file, it's going into a compiled object.
549 This has to match the logic in
550 _Jv_ClassReader::prepare_pool_entry(). */
551 utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
552 IDENTIFIER_LENGTH(utf8));
553 index = alloc_name_constant (kind, utf8);
558 index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
559 targetm.words_big_endian ());
562 case CONSTANT_Double:
563 index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
564 targetm.float_words_big_endian ());
568 case CONSTANT_Integer:
569 index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
572 case CONSTANT_NameAndType:
574 uint16 name = JPOOL_USHORT1 (jcf, index);
575 uint16 sig = JPOOL_USHORT2 (jcf, index);
576 uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
577 uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
578 jword new_index = (name_index << 16) | sig_index;
579 index = find_constant1 (cpool, kind, new_index);
590 /* Read an element_value structure from an annotation in JCF. Return
591 the constant pool index for the resulting constant pool entry. */
594 handle_element_value (JCF *jcf, int level)
596 uint8 tag = JCF_readu (jcf);
599 annotation_write_byte (tag);
608 uint16 cindex = JCF_readu2 (jcf);
609 index = handle_constant (jcf, cindex,
611 annotation_write_short (index);
616 uint16 cindex = JCF_readu2 (jcf);
617 index = handle_constant (jcf, cindex,
619 annotation_write_short (index);
624 uint16 cindex = JCF_readu2 (jcf);
625 index = handle_constant (jcf, cindex,
627 annotation_write_short (index);
632 uint16 cindex = JCF_readu2 (jcf);
633 index = handle_constant (jcf, cindex,
635 annotation_write_short (index);
640 uint16 cindex = JCF_readu2 (jcf);
641 /* Despite what the JVM spec says, compilers generate a Utf8
642 constant here, not a String. */
643 index = handle_constant (jcf, cindex,
645 annotation_write_short (index);
651 uint16 type_name_index = JCF_readu2 (jcf);
652 uint16 const_name_index = JCF_readu2 (jcf);
653 index = handle_constant (jcf, type_name_index,
655 annotation_write_short (index);
656 index = handle_constant (jcf, const_name_index,
658 annotation_write_short (index);
663 uint16 class_info_index = JCF_readu2 (jcf);
664 index = handle_constant (jcf, class_info_index,
666 annotation_write_short (index);
671 handle_annotation (jcf, level + 1);
676 uint16 n_array_elts = JCF_readu2 (jcf);
677 annotation_write_short (n_array_elts);
678 while (n_array_elts--)
679 handle_element_value (jcf, level + 1);
689 /* Read an annotation structure from JCF. Write it to the
690 reflection_data field of the outgoing class. */
693 handle_annotation (JCF *jcf, int level)
695 uint16 type_index = JCF_readu2 (jcf);
696 uint16 npairs = JCF_readu2 (jcf);
697 int index = handle_constant (jcf, type_index,
699 annotation_write_short (index);
700 annotation_write_short (npairs);
703 uint16 name_index = JCF_readu2 (jcf);
704 index = handle_constant (jcf, name_index,
706 annotation_write_short (index);
707 handle_element_value (jcf, level + 2);
711 /* Read an annotation count from JCF, and write the following
712 annotations to the reflection_data field of the outgoing class. */
715 handle_annotations (JCF *jcf, int level)
717 uint16 num = JCF_readu2 (jcf);
718 annotation_write_short (num);
720 handle_annotation (jcf, level);
723 /* As handle_annotations(), but perform a sanity check that we write
724 the same number of bytes that we were expecting. */
727 handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
730 long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
732 handle_annotations (jcf, 0);
734 gcc_assert (old_datasize + length
735 == TYPE_REFLECTION_DATASIZE (current_class));
738 /* gcj permutes its fields array after generating annotation_data, so
739 we have to fixup field indexes for fields that have moved. Given
740 ARG, a VEC_int, fixup the field indexes in the reflection_data of
741 the outgoing class. We use field_offsets to tell us where the
745 rewrite_reflection_indexes (void *arg)
749 VEC(int, heap) *map = (VEC(int, heap) *) arg;
750 unsigned char *data = TYPE_REFLECTION_DATA (current_class);
754 EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
756 uint16 index = annotation_read_short (data + offset);
757 annotation_rewrite_short
758 (VEC_index (int, map, index), data + offset);
763 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
764 reflection_data of the outgoing class. */
767 handle_member_annotations (int member_index, JCF *jcf,
768 const unsigned char *name ATTRIBUTE_UNUSED,
769 long len, jv_attr_type member_type)
771 int new_len = len + 1;
772 annotation_write_byte (member_type);
773 if (member_type != JV_CLASS_ATTR)
775 annotation_write_int (new_len);
776 annotation_write_byte (JV_ANNOTATIONS_KIND);
777 if (member_type == JV_FIELD_ATTR)
778 bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
779 if (member_type != JV_CLASS_ATTR)
780 annotation_write_short (member_index);
781 handle_annotation_attribute (member_index, jcf, len);
784 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
785 to the reflection_data of the outgoing class. */
788 handle_parameter_annotations (int member_index, JCF *jcf,
789 const unsigned char *name ATTRIBUTE_UNUSED,
790 long len, jv_attr_type member_type)
792 int new_len = len + 1;
794 annotation_write_byte (member_type);
795 if (member_type != JV_CLASS_ATTR)
797 annotation_write_int (new_len);
798 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
799 if (member_type != JV_CLASS_ATTR)
800 annotation_write_short (member_index);
801 num = JCF_readu (jcf);
802 annotation_write_byte (num);
804 handle_annotations (jcf, 0);
808 /* Read the AnnotationDefault data from JCF and write them to the
809 reflection_data of the outgoing class. */
812 handle_default_annotation (int member_index, JCF *jcf,
813 const unsigned char *name ATTRIBUTE_UNUSED,
814 long len, jv_attr_type member_type)
816 int new_len = len + 1;
817 annotation_write_byte (member_type);
818 if (member_type != JV_CLASS_ATTR)
820 annotation_write_int (new_len);
821 annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
822 if (member_type != JV_CLASS_ATTR)
823 annotation_write_short (member_index);
824 handle_element_value (jcf, 0);
827 /* As above, for the EnclosingMethod attribute. */
830 handle_enclosingmethod_attribute (int member_index, JCF *jcf,
831 const unsigned char *name ATTRIBUTE_UNUSED,
832 long len, jv_attr_type member_type)
834 int new_len = len + 1;
836 annotation_write_byte (member_type);
837 if (member_type != JV_CLASS_ATTR)
839 annotation_write_int (new_len);
840 annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
841 if (member_type != JV_CLASS_ATTR)
842 annotation_write_short (member_index);
844 index = JCF_readu2 (jcf);
845 index = handle_constant (jcf, index, CONSTANT_Class);
846 annotation_write_short (index);
848 index = JCF_readu2 (jcf);
849 index = handle_constant (jcf, index, CONSTANT_NameAndType);
850 annotation_write_short (index);
853 /* As above, for the Signature attribute. */
856 handle_signature_attribute (int member_index, JCF *jcf,
857 const unsigned char *name ATTRIBUTE_UNUSED,
858 long len, jv_attr_type member_type)
860 int new_len = len + 1;
862 annotation_write_byte (member_type);
863 if (member_type != JV_CLASS_ATTR)
865 annotation_write_int (new_len);
866 annotation_write_byte (JV_SIGNATURE_KIND);
867 if (member_type != JV_CLASS_ATTR)
868 annotation_write_short (member_index);
870 index = JCF_readu2 (jcf);
871 index = handle_constant (jcf, index, CONSTANT_Utf8);
872 annotation_write_short (index);
877 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
879 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
880 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
881 output_class = current_class = give_name_to_class (jcf, THIS); \
882 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
884 #define HANDLE_CLASS_INTERFACE(INDEX) \
885 add_interface (current_class, get_class_constant (jcf, INDEX))
887 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
888 { int sig_index = SIGNATURE; \
889 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
890 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
891 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
892 if ((ACCESS_FLAGS) & ACC_FINAL) \
893 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
896 #define HANDLE_END_FIELDS() \
897 (current_field = NULL_TREE)
899 #define HANDLE_CONSTANTVALUE(INDEX) \
900 { tree constant; int index = INDEX; \
901 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
902 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
903 constant = build_utf8_ref (name); \
906 constant = get_constant (jcf, index); \
907 set_constant_value (current_field, constant); }
909 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
910 (current_method = add_method (current_class, ACCESS_FLAGS, \
911 get_name_constant (jcf, NAME), \
912 get_name_constant (jcf, SIGNATURE)), \
913 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
914 DECL_LINENUMBERS_OFFSET (current_method) = 0)
916 #define HANDLE_END_METHODS() \
917 { current_method = NULL_TREE; }
919 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
920 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
921 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
922 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
923 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
925 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
927 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
928 JCF_SKIP (jcf, n * 10); }
930 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
932 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
933 JCF_SKIP (jcf, n * 4); }
935 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
938 VEC (tree,gc) *v = VEC_alloc (tree, gc, n); \
939 gcc_assert (DECL_FUNCTION_THROWS (current_method) == NULL); \
942 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
943 VEC_quick_push (tree, v, thrown_class); \
945 DECL_FUNCTION_THROWS (current_method) = v; \
948 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
950 /* Link seen inner classes to their outer context and register the
951 inner class to its outer context. They will be later loaded. */
952 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
953 handle_innerclass_attribute (COUNT, jcf, attribute_length)
955 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
957 /* Irrelevant decls should have been nullified by the END macros. \
958 DECL_ARTIFICIAL on fields is used for something else (See \
959 PUSH_FIELD in java-tree.h) */ \
960 if (current_method) \
961 DECL_ARTIFICIAL (current_method) = 1; \
962 else if (current_field) \
963 FIELD_SYNTHETIC (current_field) = 1; \
965 TYPE_SYNTHETIC (current_class) = 1; \
968 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
970 if (current_class == object_type_node) \
971 jcf->right_zip = 1; \
974 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
976 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
979 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
981 JCF_SKIP(jcf, attribute_length); \
984 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
986 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
989 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
991 JCF_SKIP(jcf, attribute_length); \
994 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
996 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
999 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
1001 handle_enclosingmethod_attribute (index, jcf, name_data, \
1002 attribute_length, attr_type); \
1005 #define HANDLE_SIGNATURE_ATTRIBUTE() \
1007 handle_signature_attribute (index, jcf, name_data, \
1008 attribute_length, attr_type); \
1011 #include "jcf-reader.c"
1014 parse_signature (JCF *jcf, int sig_index)
1016 gcc_assert (sig_index > 0
1017 && sig_index < JPOOL_SIZE (jcf)
1018 && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1020 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1021 JPOOL_UTF_LENGTH (jcf, sig_index));
1025 get_constant (JCF *jcf, int index)
1029 if (index <= 0 || index >= JPOOL_SIZE(jcf))
1031 tag = JPOOL_TAG (jcf, index);
1032 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
1033 return jcf->cpool.data[index].t;
1036 case CONSTANT_Integer:
1038 jint num = JPOOL_INT(jcf, index);
1039 value = build_int_cst (int_type_node, num);
1044 unsigned HOST_WIDE_INT num;
1047 num = JPOOL_UINT (jcf, index);
1048 val = double_int_lshift (uhwi_to_double_int (num), 32, 64, false);
1049 num = JPOOL_UINT (jcf, index + 1);
1050 val = double_int_ior (val, uhwi_to_double_int (num));
1052 value = double_int_to_tree (long_type_node, val);
1056 case CONSTANT_Float:
1058 jint num = JPOOL_INT(jcf, index);
1062 real_from_target_fmt (&d, &buf, &ieee_single_format);
1063 value = build_real (float_type_node, d);
1067 case CONSTANT_Double:
1069 long buf[2], lo, hi;
1072 hi = JPOOL_UINT (jcf, index);
1073 lo = JPOOL_UINT (jcf, index+1);
1075 if (targetm.float_words_big_endian ())
1076 buf[0] = hi, buf[1] = lo;
1078 buf[0] = lo, buf[1] = hi;
1080 real_from_target_fmt (&d, buf, &ieee_double_format);
1081 value = build_real (double_type_node, d);
1085 case CONSTANT_String:
1087 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
1088 const char *utf8_ptr = IDENTIFIER_POINTER (name);
1089 int utf8_len = IDENTIFIER_LENGTH (name);
1090 const unsigned char *utf8;
1093 /* Check for a malformed Utf8 string. */
1094 utf8 = (const unsigned char *) utf8_ptr;
1098 int char_len = UT8_CHAR_LENGTH (*utf8);
1099 if (char_len < 0 || char_len > 3 || char_len > i)
1100 fatal_error ("bad string constant");
1106 /* Allocate a new string value. */
1107 value = build_string (utf8_len, utf8_ptr);
1108 TREE_TYPE (value) = build_pointer_type (string_type_node);
1114 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
1115 jcf->cpool.data[index].t = value;
1118 internal_error ("bad value constant type %d, index %d",
1119 JPOOL_TAG (jcf, index), index);
1123 get_name_constant (JCF *jcf, int index)
1125 tree name = get_constant (jcf, index);
1126 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1130 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1131 a non anonymous entry) is found, We augment the inner class list of
1132 the outer context with the newly resolved innerclass. */
1135 handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
1139 annotation_write_byte (JV_CLASS_ATTR);
1140 annotation_write_int (attribute_length+1);
1141 annotation_write_byte (JV_INNER_CLASSES_KIND);
1142 annotation_write_short (count);
1146 /* Read inner_class_info_index. This may be 0 */
1147 int icii = JCF_readu2 (jcf);
1148 /* Read outer_class_info_index. If the innerclasses attribute
1149 entry isn't a member (like an inner class) the value is 0. */
1150 int ocii = JCF_readu2 (jcf);
1151 /* Read inner_name_index. If the class we're dealing with is
1152 an anonymous class, it must be 0. */
1153 int ini = JCF_readu2 (jcf);
1154 /* Read the access flag. */
1155 int acc = JCF_readu2 (jcf);
1157 annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1158 annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1159 annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1160 annotation_write_short (acc);
1162 /* If icii is 0, don't try to read the class. */
1165 tree klass = get_class_constant (jcf, icii);
1166 tree decl = TYPE_NAME (klass);
1167 /* Skip reading further if ocii is null */
1168 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1170 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1171 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
1172 set_class_decl_access_flags (acc, decl);
1173 DECL_CONTEXT (decl) = outer;
1174 DECL_INNER_CLASS_LIST (outer) =
1175 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1176 CLASS_COMPLETE_P (decl) = 1;
1183 give_name_to_class (JCF *jcf, int i)
1186 && i < JPOOL_SIZE (jcf)
1187 && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1190 tree package_name = NULL_TREE, tmp;
1192 int j = JPOOL_USHORT1 (jcf, i);
1193 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1194 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
1195 JPOOL_UTF_LENGTH (jcf, j));
1196 this_class = lookup_class (class_name);
1198 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
1199 const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
1200 linemap_add (line_table, LC_ENTER, false, sfname, 0);
1201 input_location = linemap_line_start (line_table, 0, 1);
1202 file_start_location = input_location;
1203 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1204 if (main_input_filename == NULL && jcf == main_jcf)
1205 main_input_filename = sfname;
1208 jcf->cpool.data[i].t = this_class;
1209 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1210 split_qualified_name (&package_name, &tmp,
1211 DECL_NAME (TYPE_NAME (this_class)));
1212 TYPE_PACKAGE (this_class) = package_name;
1217 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1220 get_class_constant (JCF *jcf, int i)
1224 && i < JPOOL_SIZE (jcf)
1225 && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
1227 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1229 int name_index = JPOOL_USHORT1 (jcf, i);
1230 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1231 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
1232 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
1234 if (name[0] == '[') /* Handle array "classes". */
1235 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
1238 tree cname = unmangle_classname (name, nlength);
1239 type = lookup_class (cname);
1241 jcf->cpool.data[i].t = type;
1242 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1245 type = jcf->cpool.data[i].t;
1249 /* Read a class with the fully qualified-name NAME.
1250 Return 1 iff we read the requested file.
1251 (It is still possible we failed if the file did not
1252 define the class it is supposed to.) */
1255 read_class (tree name)
1258 tree icv, klass = NULL_TREE;
1259 tree save_current_class = current_class;
1260 tree save_output_class = output_class;
1261 location_t save_location = input_location;
1262 JCF *save_current_jcf = current_jcf;
1264 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
1266 klass = TREE_TYPE (icv);
1267 jcf = TYPE_JCF (klass);
1274 const char* path_name;
1275 this_jcf.zipd = NULL;
1278 path_name = find_class (IDENTIFIER_POINTER (name),
1279 IDENTIFIER_LENGTH (name),
1284 free(CONST_CAST (char *, path_name));
1289 if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
1291 output_class = current_class = klass;
1292 if (JCF_SEEN_IN_ZIP (current_jcf))
1293 read_zip_member(current_jcf,
1294 current_jcf->zipd, current_jcf->zipd->zipf);
1295 jcf_parse (current_jcf);
1296 /* Parsing might change the class, in which case we have to
1297 put it back where we found it. */
1298 if (current_class != klass && icv != NULL_TREE)
1299 TREE_TYPE (icv) = current_class;
1300 klass = current_class;
1302 layout_class (klass);
1303 load_inner_classes (klass);
1305 output_class = save_output_class;
1306 current_class = save_current_class;
1307 input_location = save_location;
1308 current_jcf = save_current_jcf;
1312 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1313 called from the parser, otherwise it's a RECORD_TYPE node. If
1314 VERBOSE is 1, print error message on failure to load a class. */
1316 load_class (tree class_or_name, int verbose)
1319 int class_loaded = 0;
1320 tree class_decl = NULL_TREE;
1321 bool is_compiled_class = false;
1323 /* We've already failed, don't try again. */
1324 if (TREE_CODE (class_or_name) == RECORD_TYPE
1325 && TYPE_DUMMY (class_or_name))
1328 /* class_or_name can be the name of the class we want to load */
1329 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1330 name = class_or_name;
1331 /* In some cases, it's a dependency that we process earlier that
1333 else if (TREE_CODE (class_or_name) == TREE_LIST)
1334 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1335 /* Or it's a type in the making */
1337 name = DECL_NAME (TYPE_NAME (class_or_name));
1339 class_decl = IDENTIFIER_CLASS_VALUE (name);
1340 if (class_decl != NULL_TREE)
1342 tree type = TREE_TYPE (class_decl);
1344 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1345 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1350 /* If flag_verify_invocations is unset, we don't try to load a class
1351 unless we're looking for Object (which is fixed by the ABI) or
1352 it's a class that we're going to compile. */
1353 if (flag_verify_invocations
1354 || class_or_name == object_type_node
1355 || is_compiled_class
1356 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1360 const char *separator;
1362 /* We've already loaded it. */
1363 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1365 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1366 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1370 if (read_class (name))
1373 /* We failed loading name. Now consider that we might be looking
1374 for an inner class. */
1375 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1376 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
1377 name = get_identifier_with_length (IDENTIFIER_POINTER (name),
1379 - IDENTIFIER_POINTER (name)));
1380 /* Otherwise, we failed, we bail. */
1386 /* have we found the class we're looking for? */
1387 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1388 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1389 class_loaded = type && CLASS_PARSED_P (type);
1395 if (flag_verify_invocations || ! flag_indirect_dispatch)
1398 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1402 /* This is just a diagnostic during testing, not a real problem. */
1404 warning (0, "cannot find file for class %s",
1405 IDENTIFIER_POINTER (saved));
1408 if (TREE_CODE (class_or_name) == RECORD_TYPE)
1410 set_super_info (0, class_or_name, object_type_node, 0);
1411 TYPE_DUMMY (class_or_name) = 1;
1412 /* We won't be able to output any debug info for this class. */
1413 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1419 /* Parse the .class file JCF. */
1422 jcf_parse (JCF* jcf)
1426 bitmap_clear (field_offsets);
1428 if (jcf_parse_preamble (jcf) != 0)
1429 fatal_error ("not a valid Java .class file");
1430 code = jcf_parse_constant_pool (jcf);
1432 fatal_error ("error while parsing constant pool");
1433 code = verify_constant_pool (jcf);
1435 fatal_error ("error in constant pool entry #%d\n", code);
1437 jcf_parse_class (jcf);
1438 if (main_class == NULL_TREE)
1439 main_class = current_class;
1440 if (! quiet_flag && TYPE_NAME (current_class))
1441 fprintf (stderr, " %s %s",
1442 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
1443 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
1444 if (CLASS_PARSED_P (current_class))
1446 /* FIXME - where was first time */
1447 fatal_error ("reading class %s for the second time from %s",
1448 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1451 CLASS_PARSED_P (current_class) = 1;
1453 for (i = 1; i < JPOOL_SIZE(jcf); i++)
1455 switch (JPOOL_TAG (jcf, i))
1457 case CONSTANT_Class:
1458 get_class_constant (jcf, i);
1463 code = jcf_parse_fields (jcf);
1465 fatal_error ("error while parsing fields");
1466 code = jcf_parse_methods (jcf);
1468 fatal_error ("error while parsing methods");
1469 code = jcf_parse_final_attributes (jcf);
1471 fatal_error ("error while parsing final attributes");
1473 if (TYPE_REFLECTION_DATA (current_class))
1474 annotation_write_byte (JV_DONE_ATTR);
1476 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1478 /* The fields of class_type_node are already in correct order. */
1479 if (current_class != class_type_node && current_class != object_type_node)
1480 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1482 if (current_class == object_type_node)
1483 layout_class_methods (object_type_node);
1485 VEC_safe_push (tree, gc, all_class_list, TYPE_NAME (current_class));
1488 /* If we came across inner classes, load them now. */
1490 load_inner_classes (tree cur_class)
1493 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
1494 current = TREE_CHAIN (current))
1496 tree name = DECL_NAME (TREE_PURPOSE (current));
1497 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1498 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1499 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
1500 load_class (name, 1);
1505 duplicate_class_warning (const char *filename)
1507 location_t warn_loc;
1508 linemap_add (line_table, LC_RENAME, 0, filename, 0);
1509 warn_loc = linemap_line_start (line_table, 0, 1);
1510 warning_at (warn_loc, 0, "duplicate class will only be compiled once");
1514 java_layout_seen_class_methods (void)
1517 unsigned end = VEC_length (tree, all_class_list);
1522 unsigned new_length;
1524 for (ix = start; ix != end; ix++)
1526 tree decl = VEC_index (tree, all_class_list, ix);
1527 tree cls = TREE_TYPE (decl);
1529 input_location = DECL_SOURCE_LOCATION (decl);
1531 if (! CLASS_LOADED_P (cls))
1532 load_class (cls, 0);
1534 layout_class_methods (cls);
1537 /* Note that new classes might have been added while laying out
1538 methods, changing the value of all_class_list. */
1539 new_length = VEC_length (tree, all_class_list);
1540 if (end != new_length)
1551 parse_class_file (void)
1554 location_t save_location = input_location;
1556 java_layout_seen_class_methods ();
1558 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1560 /* Re-enter the current file. */
1561 expanded_location loc = expand_location (input_location);
1562 linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1564 file_start_location = input_location;
1565 (*debug_hooks->start_source_file) (input_line, input_filename);
1567 java_mark_class_local (current_class);
1569 gen_indirect_dispatch_tables (current_class);
1571 for (method = TYPE_METHODS (current_class);
1572 method != NULL_TREE; method = DECL_CHAIN (method))
1574 JCF *jcf = current_jcf;
1576 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
1579 if (METHOD_NATIVE (method))
1582 int decl_max_locals;
1586 /* We need to compute the DECL_MAX_LOCALS. We need to take
1587 the wide types into account too. */
1588 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
1589 arg != end_params_node;
1590 arg = TREE_CHAIN (arg), decl_max_locals += 1)
1592 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1593 decl_max_locals += 1;
1595 DECL_MAX_LOCALS (method) = decl_max_locals;
1596 start_java_method (method);
1597 give_name_to_locals (jcf);
1598 *get_stmts () = build_jni_stub (method);
1603 if (DECL_CODE_OFFSET (method) == 0)
1605 current_function_decl = method;
1606 error ("missing Code attribute");
1610 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1611 if (DECL_LINENUMBERS_OFFSET (method))
1616 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1617 linenumber_count = i = JCF_readu2 (jcf);
1618 linenumber_table = ptr = jcf->read_ptr;
1620 for (ptr += 2; --i >= 0; ptr += 4)
1622 int line = GET_u2 (ptr);
1623 /* Set initial input_line to smallest linenumber.
1624 * Needs to be set before init_function_start. */
1625 if (min_line == 0 || line < min_line)
1629 input_location = linemap_line_start (line_table, min_line, 1);
1633 linenumber_table = NULL;
1634 linenumber_count = 0;
1637 start_java_method (method);
1639 note_instructions (jcf, method);
1641 give_name_to_locals (jcf);
1643 /* Bump up start_label_pc_this_method so we get a unique label number
1644 and reset highest_label_pc_this_method. */
1645 if (highest_label_pc_this_method >= 0)
1647 /* We adjust to the next multiple of 1000. This is just a frill
1648 so the last 3 digits of the label number match the bytecode
1649 offset, which might make debugging marginally more convenient. */
1650 start_label_pc_this_method
1651 = ((((start_label_pc_this_method + highest_label_pc_this_method)
1655 highest_label_pc_this_method = -1;
1658 /* Convert bytecode to trees. */
1659 expand_byte_code (jcf, method);
1666 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
1667 input_location = save_location;
1670 static VEC(tree,gc) *predefined_filenames;
1673 add_predefined_file (tree name)
1675 VEC_safe_push (tree, gc, predefined_filenames, name);
1679 predefined_filename_p (tree node)
1684 FOR_EACH_VEC_ELT (tree, predefined_filenames, ix, f)
1691 /* Generate a function that does all static initialization for this
1692 translation unit. */
1695 java_emit_static_constructor (void)
1699 emit_register_classes (&body);
1700 write_resource_constructor (&body);
1704 tree name = get_identifier ("_Jv_global_static_constructor");
1707 = build_decl (input_location, FUNCTION_DECL, name,
1708 build_function_type_list (void_type_node, NULL_TREE));
1710 tree resdecl = build_decl (input_location,
1711 RESULT_DECL, NULL_TREE, void_type_node);
1712 DECL_ARTIFICIAL (resdecl) = 1;
1713 DECL_RESULT (decl) = resdecl;
1714 current_function_decl = decl;
1715 allocate_struct_function (decl, false);
1717 TREE_STATIC (decl) = 1;
1718 TREE_USED (decl) = 1;
1719 DECL_ARTIFICIAL (decl) = 1;
1720 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1721 DECL_SAVED_TREE (decl) = body;
1722 DECL_UNINLINABLE (decl) = 1;
1724 DECL_INITIAL (decl) = make_node (BLOCK);
1725 TREE_USED (DECL_INITIAL (decl)) = 1;
1727 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1728 java_genericize (decl);
1729 cgraph_finalize_function (decl, false);
1735 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
1737 int filename_count = 0;
1738 location_t save_location = input_location;
1739 char *file_list = NULL, *list, *next;
1741 FILE *finput = NULL;
1745 bitmap_obstack_initialize (&bit_obstack);
1746 field_offsets = BITMAP_ALLOC (&bit_obstack);
1748 if (flag_filelist_file)
1751 finput = fopen (main_input_filename, "r");
1753 fatal_error ("can%'t open %s: %m", input_filename);
1754 list = XNEWVEC (char, avail);
1761 count = next - list;
1762 avail = 2 * (count + avail);
1763 list = XRESIZEVEC (char, list, avail);
1764 next = list + count;
1765 avail = avail - count;
1767 /* Subtract to to guarantee space for final '\0'. */
1768 count = fread (next, 1, avail - 1, finput);
1771 if (! feof (finput))
1772 fatal_error ("error closing %s: %m", input_filename);
1784 list = CONST_CAST (char *, main_input_filename);
1788 for (next = list; ; )
1791 if (flag_filelist_file && ! in_quotes
1792 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1793 || ch == '&') /* FIXME */)
1807 if (flag_filelist_file && ch == '"')
1809 in_quotes = ! in_quotes;
1824 /* Exclude .java files. */
1825 if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1831 node = get_identifier (list);
1835 /* Exclude file that we see twice on the command line. */
1837 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1838 duplicate_class_warning (IDENTIFIER_POINTER (node));
1841 build_translation_unit_decl (node);
1842 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1848 if (file_list != NULL)
1851 if (filename_count == 0)
1852 warning (0, "no input file specified");
1856 const char *resource_filename;
1858 /* Only one resource file may be compiled at a time. */
1859 assert (VEC_length (tree, all_translation_units) == 1);
1862 = IDENTIFIER_POINTER
1863 (DECL_NAME (VEC_index (tree, all_translation_units, 0)));
1864 compile_resource_file (resource_name, resource_filename);
1869 current_jcf = main_jcf;
1870 FOR_EACH_VEC_ELT (tree, all_translation_units, ix, node)
1872 unsigned char magic_string[4];
1875 tree name = DECL_NAME (node);
1877 const char *filename = IDENTIFIER_POINTER (name);
1879 /* Skip already parsed files */
1880 real_path = lrealpath (filename);
1881 real_file = get_identifier (real_path);
1883 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1886 /* Close previous descriptor, if any */
1887 if (finput && fclose (finput))
1888 fatal_error ("can%'t close input file %s: %m", main_input_filename);
1890 finput = fopen (filename, "rb");
1892 fatal_error ("can%'t open %s: %m", filename);
1894 #ifdef IO_BUFFER_SIZE
1895 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1896 _IOFBF, IO_BUFFER_SIZE);
1899 /* Figure what kind of file we're dealing with */
1900 if (fread (magic_string, 1, 4, finput) == 4)
1902 fseek (finput, 0L, SEEK_SET);
1903 magic = GET_u4 (magic_string);
1905 if (magic == 0xcafebabe)
1907 CLASS_FILE_P (node) = 1;
1908 current_jcf = ggc_alloc_cleared_JCF ();
1909 current_jcf->read_state = finput;
1910 current_jcf->filbuf = jcf_filbuf_from_stdio;
1911 jcf_parse (current_jcf);
1912 DECL_SOURCE_LOCATION (node) = file_start_location;
1913 TYPE_JCF (current_class) = current_jcf;
1914 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1916 /* We've already compiled this class. */
1917 duplicate_class_warning (filename);
1920 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1921 TREE_TYPE (node) = current_class;
1923 else if (magic == (JCF_u4)ZIPMAGIC)
1925 main_jcf = ggc_alloc_cleared_JCF ();
1926 main_jcf->read_state = finput;
1927 main_jcf->filbuf = jcf_filbuf_from_stdio;
1928 linemap_add (line_table, LC_ENTER, false, filename, 0);
1929 input_location = linemap_line_start (line_table, 0, 1);
1930 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1931 fatal_error ("bad zip/jar file %s", filename);
1932 localToFile = SeenZipFiles;
1933 /* Register all the classes defined there. */
1934 process_zip_dir ((FILE *) main_jcf->read_state);
1935 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1936 parse_zip_file_entries ();
1938 else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1940 /* Ignore an empty input jar. */
1946 java_push_parser_context ();
1947 java_parser_context_save_global ();
1949 parse_source_file_1 (real_file, filename, finput);
1950 java_parser_context_restore_global ();
1951 java_pop_parser_context (1);
1952 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1957 FOR_EACH_VEC_ELT (tree, all_translation_units, ix, node)
1959 input_location = DECL_SOURCE_LOCATION (node);
1960 if (CLASS_FILE_P (node))
1962 /* FIXME: These two flags really should be independent. We
1963 should be able to compile fully binary compatible, but
1964 with flag_verify_invocations on. */
1965 flag_verify_invocations = ! flag_indirect_dispatch;
1966 output_class = current_class = TREE_TYPE (node);
1968 current_jcf = TYPE_JCF (current_class);
1969 layout_class (current_class);
1970 load_inner_classes (current_class);
1971 parse_class_file ();
1972 JCF_FINISH (current_jcf);
1975 input_location = save_location;
1977 bitmap_obstack_release (&bit_obstack);
1980 /* Arrange for any necessary initialization to happen. */
1981 java_emit_static_constructor ();
1982 gcc_assert (global_bindings_p ());
1986 /* Return the name of the class corresponding to the name of the file
1987 in this zip entry. The result is newly allocated using ALLOC. */
1989 compute_class_name (struct ZipDirectory *zdir)
1991 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1994 int filename_length = zdir->filename_length;
1996 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
1998 class_name_in_zip_dir += 2;
1999 filename_length -= 2;
2002 filename_length -= strlen (".class");
2003 class_name = XNEWVEC (char, filename_length + 1);
2004 memcpy (class_name, class_name_in_zip_dir, filename_length);
2005 class_name [filename_length] = '\0';
2007 for (i = 0; i < filename_length; i++)
2008 if (class_name[i] == '/')
2009 class_name[i] = '.';
2014 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2015 if it is a property file of some sort. */
2017 classify_zip_file (struct ZipDirectory *zdir)
2019 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2021 if (zdir->filename_length > 6
2022 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2026 /* For now we drop the manifest, but not other information. */
2027 if (zdir->filename_length == 20
2028 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
2031 /* Drop directory entries. */
2032 if (zdir->filename_length > 0
2033 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2039 /* Process all class entries found in the zip file. */
2041 parse_zip_file_entries (void)
2043 struct ZipDirectory *zdir;
2046 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2047 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2051 switch (classify_zip_file (zdir))
2058 char *class_name = compute_class_name (zdir);
2059 int previous_alias_set = -1;
2060 klass = lookup_class (get_identifier (class_name));
2062 current_jcf = TYPE_JCF (klass);
2063 output_class = current_class = klass;
2065 /* This is a dummy class, and now we're compiling it for
2067 gcc_assert (! TYPE_DUMMY (klass));
2069 /* This is for a corner case where we have a superclass
2070 but no superclass fields.
2072 This can happen if we earlier failed to lay out this
2073 class because its superclass was still in the process
2074 of being laid out; this occurs when we have recursive
2075 class dependencies via inner classes. We must record
2076 the previous alias set and restore it after laying out
2079 FIXME: this really is a kludge. We should figure out a
2080 way to lay out the class properly before this
2082 if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2083 && integer_zerop (TYPE_SIZE (klass)))
2085 TYPE_SIZE (klass) = NULL_TREE;
2086 previous_alias_set = TYPE_ALIAS_SET (klass);
2087 TYPE_ALIAS_SET (klass) = -1;
2090 if (! CLASS_LOADED_P (klass))
2092 if (! CLASS_PARSED_P (klass))
2094 read_zip_member (current_jcf, zdir, localToFile);
2095 jcf_parse (current_jcf);
2097 layout_class (current_class);
2098 load_inner_classes (current_class);
2101 if (previous_alias_set != -1)
2102 TYPE_ALIAS_SET (klass) = previous_alias_set;
2104 if (TYPE_SIZE (current_class) != error_mark_node)
2106 parse_class_file ();
2107 free (current_jcf->buffer); /* No longer necessary */
2108 /* Note: there is a way to free this buffer right after a
2109 class seen in a zip file has been parsed. The idea is the
2110 set its jcf in such a way that buffer will be reallocated
2111 the time the code for the class will be generated. FIXME. */
2118 char *file_name, *class_name_in_zip_dir, *buffer;
2120 file_name = XNEWVEC (char, zdir->filename_length + 1);
2121 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2122 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2123 file_name[zdir->filename_length] = '\0';
2126 jcf->read_state = finput;
2127 jcf->filbuf = jcf_filbuf_from_stdio;
2128 jcf->classname = NULL;
2129 jcf->filename = file_name;
2132 if (read_zip_member (jcf, zdir, localToFile) < 0)
2133 fatal_error ("error while reading %s from zip file", file_name);
2135 buffer = XNEWVEC (char, zdir->filename_length + 1 +
2136 (jcf->buffer_end - jcf->buffer));
2137 strcpy (buffer, file_name);
2138 /* This is not a typo: we overwrite the trailing \0 of the
2139 file name; this is just how the data is laid out. */
2140 memcpy (buffer + zdir->filename_length,
2141 jcf->buffer, jcf->buffer_end - jcf->buffer);
2143 compile_resource_data (file_name, buffer,
2144 jcf->buffer_end - jcf->buffer);
2157 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
2158 jcf up for further processing and link it to the created class. */
2161 process_zip_dir (FILE *finput)
2166 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2167 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2169 char *class_name, *file_name, *class_name_in_zip_dir;
2173 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2175 /* Here we skip non-class files; we handle them later. */
2176 if (classify_zip_file (zdir) != 1)
2179 class_name = compute_class_name (zdir);
2180 file_name = XNEWVEC (char, zdir->filename_length+1);
2181 jcf = ggc_alloc_cleared_JCF ();
2183 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2184 file_name [zdir->filename_length] = '\0';
2186 klass = lookup_class (get_identifier (class_name));
2188 if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2190 /* We've already compiled this class. */
2191 duplicate_class_warning (file_name);
2194 /* This function is only called when processing a zip file seen
2195 on the command line. */
2196 CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
2198 jcf->read_state = finput;
2199 jcf->filbuf = jcf_filbuf_from_stdio;
2200 jcf->classname = class_name;
2201 jcf->filename = file_name;
2204 TYPE_JCF (klass) = jcf;
2208 #include "gt-java-jcf-parse.h"
2209 #include "gtype-java.h"