1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996-2013 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)
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> */
28 #include "coretypes.h"
32 #include "java-except.h"
35 #include "java-tree.h"
36 #include "diagnostic-core.h"
48 #ifdef HAVE_LANGINFO_CODESET
52 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
53 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
54 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
55 #define JPOOL_UTF_DATA(JCF, INDEX) \
56 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
57 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
59 unsigned char save; unsigned char *text; \
60 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
61 text = (JCF)->read_ptr; \
62 save = text[LENGTH]; \
64 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
65 text[LENGTH] = save; \
66 JCF_SKIP (JCF, LENGTH); } while (0)
70 extern struct obstack temporary_obstack
;
72 static GTY(()) tree parse_roots
[2];
74 /* The FIELD_DECL for the current field. */
75 #define current_field parse_roots[0]
77 /* The METHOD_DECL for the current method. */
78 #define current_method parse_roots[1]
80 /* Line 0 in current file, if compiling from bytecode. */
81 static location_t file_start_location
;
83 /* The Java archive that provides main_class; the main input file. */
84 static GTY(()) struct JCF
* main_jcf
;
86 /* A list of all the class DECLs seen so far. */
87 static GTY(()) vec
<tree
, va_gc
> *all_class_list
;
89 /* The number of source files passed to us by -fsource-filename and an
90 array of pointers to each name. Used by find_sourcefile(). */
91 static int num_files
= 0;
92 static char **filenames
;
94 static struct ZipFile
*localToFile
;
96 /* A map of byte offsets in the reflection data that are fields which
99 bitmap_obstack bit_obstack
;
101 /* Declarations of some functions used here. */
102 static void handle_innerclass_attribute (int count
, JCF
*, int len
);
103 static tree
give_name_to_class (JCF
*jcf
, int index
);
104 static char *compute_class_name (struct ZipDirectory
*zdir
);
105 static int classify_zip_file (struct ZipDirectory
*zdir
);
106 static void parse_zip_file_entries (void);
107 static void process_zip_dir (FILE *);
108 static void parse_class_file (void);
109 static void handle_deprecated (void);
110 static void set_source_filename (JCF
*, int);
111 static void jcf_parse (struct JCF
*);
112 static void load_inner_classes (tree
);
113 static void handle_annotation (JCF
*jcf
, int level
);
114 static void java_layout_seen_class_methods (void);
116 /* Handle "Deprecated" attribute. */
118 handle_deprecated (void)
120 if (current_field
!= NULL_TREE
)
121 FIELD_DEPRECATED (current_field
) = 1;
122 else if (current_method
!= NULL_TREE
)
123 METHOD_DEPRECATED (current_method
) = 1;
124 else if (current_class
!= NULL_TREE
)
125 CLASS_DEPRECATED (TYPE_NAME (current_class
)) = 1;
128 /* Shouldn't happen. */
135 /* Reverse a string. */
137 reverse (const char *s
)
143 int len
= strlen (s
);
144 char *d
= XNEWVAR (char, len
+ 1);
149 for (dp
= &d
[0], sp
= &s
[len
-1]; sp
>= s
; dp
++, sp
--)
156 /* Compare two strings for qsort(). */
158 cmpstringp (const void *p1
, const void *p2
)
160 /* The arguments to this function are "pointers to
161 pointers to char", but strcmp() arguments are "pointers
162 to char", hence the following cast plus dereference */
164 return strcmp(*(const char *const*) p1
, *(const char *const*) p2
);
167 /* Create an array of strings, one for each source file that we've
168 seen. fsource_filename can either be the name of a single .java
169 file or a file that contains a list of filenames separated by
172 java_read_sourcefilenames (const char *fsource_filename
)
176 && strlen (fsource_filename
) > strlen (".java")
177 && filename_cmp ((fsource_filename
178 + strlen (fsource_filename
)
182 /* fsource_filename isn't a .java file but a list of filenames
183 separated by newlines */
184 FILE *finput
= fopen (fsource_filename
, "r");
186 int longest_line
= 0;
190 /* Find out how many files there are, and how long the filenames are. */
191 while (! feof (finput
))
193 int ch
= getc (finput
);
197 if (len
> longest_line
)
209 /* Read the filenames. Put a pointer to each filename into the
212 char *linebuf
= (char *) alloca (longest_line
+ 1);
216 filenames
= XNEWVEC (char *, num_files
);
221 int ch
= getc (finput
);
226 linebuf
[charpos
] = 0;
227 gcc_assert (i
< num_files
);
228 /* ??? Perhaps we should use lrealpath() here. Doing
229 so would tidy up things like /../ but the rest of
230 gcc seems to assume relative pathnames, not
231 absolute pathnames. */
232 /* realname = lrealpath (linebuf); */
233 filenames
[i
++] = reverse (linebuf
);
237 gcc_assert (charpos
< longest_line
);
238 linebuf
[charpos
++] = ch
;
242 qsort (filenames
, num_files
, sizeof (char *), cmpstringp
);
248 filenames
= XNEWVEC (char *, 1);
249 filenames
[0] = reverse (fsource_filename
);
254 /* Given a relative pathname such as foo/bar.java, attempt to find a
255 longer pathname with the same suffix.
257 This is a best guess heuristic; with some weird class hierarchies we
258 may fail to pick the correct source file. For example, if we have
259 the filenames foo/bar.java and also foo/foo/bar.java, we do not
260 have enough information to know which one is the right match for
264 find_sourcefile (const char *name
)
266 int i
= 0, j
= num_files
-1;
271 char *revname
= reverse (name
);
276 int cmp
= strncmp (revname
, filenames
[k
], strlen (revname
));
279 /* OK, so we found one. But is it a unique match? */
281 && strncmp (revname
, filenames
[k
-1], strlen (revname
)) == 0)
283 && (strncmp (revname
, filenames
[k
+1], strlen (revname
))
287 found
= filenames
[k
];
300 if (found
&& strlen (found
) > strlen (name
))
301 return reverse (found
);
308 /* Handle "SourceFile" attribute. */
311 set_source_filename (JCF
*jcf
, int index
)
313 tree sfname_id
= get_name_constant (jcf
, index
);
314 const char *sfname
= IDENTIFIER_POINTER (sfname_id
);
315 const char *old_filename
= input_filename
;
316 int new_len
= IDENTIFIER_LENGTH (sfname_id
);
317 if (old_filename
!= NULL
)
319 int old_len
= strlen (old_filename
);
320 /* Use the current input_filename (derived from the class name)
321 if it has a directory prefix, but otherwise matches sfname. */
322 if (old_len
> new_len
323 && filename_cmp (sfname
, old_filename
+ old_len
- new_len
) == 0
324 && (old_filename
[old_len
- new_len
- 1] == '/'
325 || old_filename
[old_len
- new_len
- 1] == '\\'))
328 if (strchr (sfname
, '/') == NULL
&& strchr (sfname
, '\\') == NULL
)
330 const char *class_name
331 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class
)));
332 const char *dot
= strrchr (class_name
, '.');
335 /* Length of prefix, not counting final dot. */
336 int i
= dot
- class_name
;
337 /* Concatenate current package prefix with new sfname. */
338 char *buf
= XNEWVEC (char, i
+ new_len
+ 2); /* Space for '.' and '\0'. */
339 strcpy (buf
+ i
+ 1, sfname
);
340 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
341 Note we start at the end with the final package dot. */
344 char c
= class_name
[i
];
349 sfname_id
= get_identifier (buf
);
351 sfname
= IDENTIFIER_POINTER (sfname_id
);
355 sfname
= find_sourcefile (sfname
);
356 ORDINARY_MAP_FILE_NAME (LINEMAPS_LAST_ORDINARY_MAP (line_table
)) = sfname
;
357 if (current_class
== main_class
) main_input_filename
= sfname
;
363 /* Annotation handling.
365 The technique we use here is to copy the annotation data directly
366 from the input class file into the output file. We don't decode the
367 data at all, merely rewriting constant indexes whenever we come
368 across them: this is necessary because the constant pool in the
369 output file isn't the same as the constant pool in the input.
371 The main advantage of this technique is that the resulting
372 annotation data is pointer-free, so it doesn't have to be relocated
373 at startup time. As a consequence of this, annotations have no
374 performance impact unless they are used. Also, this representation
378 /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
379 the start of the newly allocated region. */
381 static unsigned char*
382 annotation_grow (int delta
)
384 unsigned char **data
= &TYPE_REFLECTION_DATA (current_class
);
385 long *datasize
= &TYPE_REFLECTION_DATASIZE (current_class
);
386 long len
= *datasize
;
390 *data
= XNEWVAR (unsigned char, delta
);
394 int newlen
= *datasize
+ delta
;
395 if (floor_log2 (newlen
) != floor_log2 (*datasize
))
396 *data
= XRESIZEVAR (unsigned char, *data
, 2 << (floor_log2 (newlen
)));
402 /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
403 byte order (i.e. big endian.) */
406 annotation_rewrite_byte (unsigned int n
, unsigned char *p
)
412 annotation_rewrite_short (unsigned int n
, unsigned char *p
)
419 annotation_rewrite_int (unsigned int n
, unsigned char *p
)
427 /* Read a 16-bit unsigned int in Java byte order (i.e. big
431 annotation_read_short (unsigned char *p
)
434 tmp
= (tmp
<< 8) | p
[1];
438 /* annotation_write_TYPE. Rewrite various int types, appending them
439 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
443 annotation_write_byte (unsigned int n
)
445 annotation_rewrite_byte (n
, annotation_grow (1));
449 annotation_write_short (unsigned int n
)
451 annotation_rewrite_short (n
, annotation_grow (2));
455 annotation_write_int (unsigned int n
)
457 annotation_rewrite_int (n
, annotation_grow (4));
460 /* Create a 64-bit constant in the constant pool.
462 This is used for both integer and floating-point types. As a
463 consequence, it will not work if the target floating-point format
464 is anything other than IEEE-754. While this is arguably a bug, the
465 runtime library makes exactly the same assumption and it's unlikely
466 that Java will ever run on a non-IEEE machine. */
469 handle_long_constant (JCF
*jcf
, CPool
*cpool
, enum cpool_tag kind
,
470 int index
, bool big_endian
)
472 /* If we're on a 64-bit platform we can fit a long or double
473 into the same space as a jword. */
474 if (POINTER_SIZE
>= 64)
475 index
= find_constant1 (cpool
, kind
, JPOOL_LONG (jcf
, index
));
477 /* In a compiled program the constant pool is in native word
478 order. How weird is that??? */
480 index
= find_constant2 (cpool
, kind
,
481 JPOOL_INT (jcf
, index
),
482 JPOOL_INT (jcf
, index
+1));
484 index
= find_constant2 (cpool
, kind
,
485 JPOOL_INT (jcf
, index
+1),
486 JPOOL_INT (jcf
, index
));
491 /* Given a class file and an index into its constant pool, create an
492 entry in the outgoing constant pool for the same item. */
495 handle_constant (JCF
*jcf
, int index
, enum cpool_tag purpose
)
498 CPool
*cpool
= cpool_for_class (output_class
);
503 if (! CPOOL_INDEX_IN_RANGE (&jcf
->cpool
, index
))
504 error ("<constant pool index %d not in range>", index
);
506 kind
= JPOOL_TAG (jcf
, index
);
508 if ((kind
& ~CONSTANT_ResolvedFlag
) != purpose
)
510 if (purpose
== CONSTANT_Class
511 && kind
== CONSTANT_Utf8
)
514 error ("<constant pool index %d unexpected type", index
);
520 case CONSTANT_ResolvedClass
:
522 /* For some reason I know not the what of, class names in
523 annotations are UTF-8 strings in the constant pool but
524 class names in EnclosingMethod attributes are real class
525 references. Set CONSTANT_LazyFlag here so that the VM
526 doesn't attempt to resolve them at class initialization
528 tree resolved_class
, class_name
;
529 resolved_class
= get_class_constant (jcf
, index
);
530 class_name
= build_internal_class_name (resolved_class
);
531 index
= alloc_name_constant (CONSTANT_Class
| CONSTANT_LazyFlag
,
533 (IDENTIFIER_POINTER(class_name
),
534 IDENTIFIER_LENGTH(class_name
))));
539 tree utf8
= get_constant (jcf
, index
);
540 if (purpose
== CONSTANT_Class
)
541 /* Create a constant pool entry for a type signature. This
542 one has '.' rather than '/' because it isn't going into a
543 class file, it's going into a compiled object.
545 This has to match the logic in
546 _Jv_ClassReader::prepare_pool_entry(). */
547 utf8
= unmangle_classname (IDENTIFIER_POINTER(utf8
),
548 IDENTIFIER_LENGTH(utf8
));
549 index
= alloc_name_constant (kind
, utf8
);
554 index
= handle_long_constant (jcf
, cpool
, CONSTANT_Long
, index
,
555 targetm
.words_big_endian ());
558 case CONSTANT_Double
:
559 index
= handle_long_constant (jcf
, cpool
, CONSTANT_Double
, index
,
560 targetm
.float_words_big_endian ());
564 case CONSTANT_Integer
:
565 index
= find_constant1 (cpool
, kind
, JPOOL_INT (jcf
, index
));
568 case CONSTANT_NameAndType
:
570 uint16 name
= JPOOL_USHORT1 (jcf
, index
);
571 uint16 sig
= JPOOL_USHORT2 (jcf
, index
);
572 uint32 name_index
= handle_constant (jcf
, name
, CONSTANT_Utf8
);
573 uint32 sig_index
= handle_constant (jcf
, sig
, CONSTANT_Class
);
574 jword new_index
= (name_index
<< 16) | sig_index
;
575 index
= find_constant1 (cpool
, kind
, new_index
);
586 /* Read an element_value structure from an annotation in JCF. Return
587 the constant pool index for the resulting constant pool entry. */
590 handle_element_value (JCF
*jcf
, int level
)
592 uint8 tag
= JCF_readu (jcf
);
595 annotation_write_byte (tag
);
604 uint16 cindex
= JCF_readu2 (jcf
);
605 index
= handle_constant (jcf
, cindex
,
607 annotation_write_short (index
);
612 uint16 cindex
= JCF_readu2 (jcf
);
613 index
= handle_constant (jcf
, cindex
,
615 annotation_write_short (index
);
620 uint16 cindex
= JCF_readu2 (jcf
);
621 index
= handle_constant (jcf
, cindex
,
623 annotation_write_short (index
);
628 uint16 cindex
= JCF_readu2 (jcf
);
629 index
= handle_constant (jcf
, cindex
,
631 annotation_write_short (index
);
636 uint16 cindex
= JCF_readu2 (jcf
);
637 /* Despite what the JVM spec says, compilers generate a Utf8
638 constant here, not a String. */
639 index
= handle_constant (jcf
, cindex
,
641 annotation_write_short (index
);
647 uint16 type_name_index
= JCF_readu2 (jcf
);
648 uint16 const_name_index
= JCF_readu2 (jcf
);
649 index
= handle_constant (jcf
, type_name_index
,
651 annotation_write_short (index
);
652 index
= handle_constant (jcf
, const_name_index
,
654 annotation_write_short (index
);
659 uint16 class_info_index
= JCF_readu2 (jcf
);
660 index
= handle_constant (jcf
, class_info_index
,
662 annotation_write_short (index
);
667 handle_annotation (jcf
, level
+ 1);
672 uint16 n_array_elts
= JCF_readu2 (jcf
);
673 annotation_write_short (n_array_elts
);
674 while (n_array_elts
--)
675 handle_element_value (jcf
, level
+ 1);
685 /* Read an annotation structure from JCF. Write it to the
686 reflection_data field of the outgoing class. */
689 handle_annotation (JCF
*jcf
, int level
)
691 uint16 type_index
= JCF_readu2 (jcf
);
692 uint16 npairs
= JCF_readu2 (jcf
);
693 int index
= handle_constant (jcf
, type_index
,
695 annotation_write_short (index
);
696 annotation_write_short (npairs
);
699 uint16 name_index
= JCF_readu2 (jcf
);
700 index
= handle_constant (jcf
, name_index
,
702 annotation_write_short (index
);
703 handle_element_value (jcf
, level
+ 2);
707 /* Read an annotation count from JCF, and write the following
708 annotations to the reflection_data field of the outgoing class. */
711 handle_annotations (JCF
*jcf
, int level
)
713 uint16 num
= JCF_readu2 (jcf
);
714 annotation_write_short (num
);
716 handle_annotation (jcf
, level
);
719 /* As handle_annotations(), but perform a sanity check that we write
720 the same number of bytes that we were expecting. */
723 handle_annotation_attribute (int ATTRIBUTE_UNUSED index
, JCF
*jcf
,
726 long old_datasize
= TYPE_REFLECTION_DATASIZE (current_class
);
728 handle_annotations (jcf
, 0);
730 gcc_assert (old_datasize
+ length
731 == TYPE_REFLECTION_DATASIZE (current_class
));
734 /* gcj permutes its fields array after generating annotation_data, so
735 we have to fixup field indexes for fields that have moved. Given
736 ARG, a VEC_int, fixup the field indexes in the reflection_data of
737 the outgoing class. We use field_offsets to tell us where the
741 rewrite_reflection_indexes (void *arg
)
745 vec
<int> *map
= (vec
<int> *) arg
;
746 unsigned char *data
= TYPE_REFLECTION_DATA (current_class
);
750 EXECUTE_IF_SET_IN_BITMAP (field_offsets
, 0, offset
, bi
)
752 uint16 index
= annotation_read_short (data
+ offset
);
753 annotation_rewrite_short
754 ((*map
)[index
], data
+ offset
);
759 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
760 reflection_data of the outgoing class. */
763 handle_member_annotations (int member_index
, JCF
*jcf
,
764 const unsigned char *name ATTRIBUTE_UNUSED
,
765 long len
, jv_attr_type member_type
)
767 int new_len
= len
+ 1;
768 annotation_write_byte (member_type
);
769 if (member_type
!= JV_CLASS_ATTR
)
771 annotation_write_int (new_len
);
772 annotation_write_byte (JV_ANNOTATIONS_KIND
);
773 if (member_type
== JV_FIELD_ATTR
)
774 bitmap_set_bit (field_offsets
, TYPE_REFLECTION_DATASIZE (current_class
));
775 if (member_type
!= JV_CLASS_ATTR
)
776 annotation_write_short (member_index
);
777 handle_annotation_attribute (member_index
, jcf
, len
);
780 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
781 to the reflection_data of the outgoing class. */
784 handle_parameter_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;
790 annotation_write_byte (member_type
);
791 if (member_type
!= JV_CLASS_ATTR
)
793 annotation_write_int (new_len
);
794 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND
);
795 if (member_type
!= JV_CLASS_ATTR
)
796 annotation_write_short (member_index
);
797 num
= JCF_readu (jcf
);
798 annotation_write_byte (num
);
800 handle_annotations (jcf
, 0);
804 /* Read the AnnotationDefault data from JCF and write them to the
805 reflection_data of the outgoing class. */
808 handle_default_annotation (int member_index
, JCF
*jcf
,
809 const unsigned char *name ATTRIBUTE_UNUSED
,
810 long len
, jv_attr_type member_type
)
812 int new_len
= len
+ 1;
813 annotation_write_byte (member_type
);
814 if (member_type
!= JV_CLASS_ATTR
)
816 annotation_write_int (new_len
);
817 annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND
);
818 if (member_type
!= JV_CLASS_ATTR
)
819 annotation_write_short (member_index
);
820 handle_element_value (jcf
, 0);
823 /* As above, for the EnclosingMethod attribute. */
826 handle_enclosingmethod_attribute (int member_index
, JCF
*jcf
,
827 const unsigned char *name ATTRIBUTE_UNUSED
,
828 long len
, jv_attr_type member_type
)
830 int new_len
= len
+ 1;
832 annotation_write_byte (member_type
);
833 if (member_type
!= JV_CLASS_ATTR
)
835 annotation_write_int (new_len
);
836 annotation_write_byte (JV_ENCLOSING_METHOD_KIND
);
837 if (member_type
!= JV_CLASS_ATTR
)
838 annotation_write_short (member_index
);
840 index
= JCF_readu2 (jcf
);
841 index
= handle_constant (jcf
, index
, CONSTANT_Class
);
842 annotation_write_short (index
);
844 index
= JCF_readu2 (jcf
);
845 index
= handle_constant (jcf
, index
, CONSTANT_NameAndType
);
846 annotation_write_short (index
);
849 /* As above, for the Signature attribute. */
852 handle_signature_attribute (int member_index
, JCF
*jcf
,
853 const unsigned char *name ATTRIBUTE_UNUSED
,
854 long len
, jv_attr_type member_type
)
856 int new_len
= len
+ 1;
858 annotation_write_byte (member_type
);
859 if (member_type
!= JV_CLASS_ATTR
)
861 annotation_write_int (new_len
);
862 annotation_write_byte (JV_SIGNATURE_KIND
);
863 if (member_type
!= JV_CLASS_ATTR
)
864 annotation_write_short (member_index
);
866 index
= JCF_readu2 (jcf
);
867 index
= handle_constant (jcf
, index
, CONSTANT_Utf8
);
868 annotation_write_short (index
);
873 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
875 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
876 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
877 output_class = current_class = give_name_to_class (jcf, THIS); \
878 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
880 #define HANDLE_CLASS_INTERFACE(INDEX) \
881 add_interface (current_class, get_class_constant (jcf, INDEX))
883 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
884 { int sig_index = SIGNATURE; \
885 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
886 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
887 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
888 if ((ACCESS_FLAGS) & ACC_FINAL) \
889 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
892 #define HANDLE_END_FIELDS() \
893 (current_field = NULL_TREE)
895 #define HANDLE_CONSTANTVALUE(INDEX) \
896 { tree constant; int index = INDEX; \
897 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
898 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
899 constant = build_utf8_ref (name); \
902 constant = get_constant (jcf, index); \
903 set_constant_value (current_field, constant); }
905 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
906 (current_method = add_method (current_class, ACCESS_FLAGS, \
907 get_name_constant (jcf, NAME), \
908 get_name_constant (jcf, SIGNATURE)), \
909 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
910 DECL_LINENUMBERS_OFFSET (current_method) = 0)
912 #define HANDLE_END_METHODS() \
913 { current_method = NULL_TREE; }
915 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
916 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
917 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
918 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
919 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
921 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
923 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
924 JCF_SKIP (jcf, n * 10); }
926 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
928 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
929 JCF_SKIP (jcf, n * 4); }
931 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
934 vec<tree, va_gc> *v; \
936 gcc_assert (!DECL_FUNCTION_THROWS (current_method)); \
939 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
940 v->quick_push (thrown_class); \
942 DECL_FUNCTION_THROWS (current_method) = v; \
945 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
947 /* Link seen inner classes to their outer context and register the
948 inner class to its outer context. They will be later loaded. */
949 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
950 handle_innerclass_attribute (COUNT, jcf, attribute_length)
952 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
954 /* Irrelevant decls should have been nullified by the END macros. \
955 DECL_ARTIFICIAL on fields is used for something else (See \
956 PUSH_FIELD in java-tree.h) */ \
957 if (current_method) \
958 DECL_ARTIFICIAL (current_method) = 1; \
959 else if (current_field) \
960 FIELD_SYNTHETIC (current_field) = 1; \
962 TYPE_SYNTHETIC (current_class) = 1; \
965 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
967 if (current_class == object_type_node) \
968 jcf->right_zip = 1; \
971 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
973 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
976 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
978 JCF_SKIP(jcf, attribute_length); \
981 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
983 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
986 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
988 JCF_SKIP(jcf, attribute_length); \
991 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
993 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
996 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
998 handle_enclosingmethod_attribute (index, jcf, name_data, \
999 attribute_length, attr_type); \
1002 #define HANDLE_SIGNATURE_ATTRIBUTE() \
1004 handle_signature_attribute (index, jcf, name_data, \
1005 attribute_length, attr_type); \
1008 #include "jcf-reader.c"
1011 parse_signature (JCF
*jcf
, int sig_index
)
1013 gcc_assert (sig_index
> 0
1014 && sig_index
< JPOOL_SIZE (jcf
)
1015 && JPOOL_TAG (jcf
, sig_index
) == CONSTANT_Utf8
);
1017 return parse_signature_string (JPOOL_UTF_DATA (jcf
, sig_index
),
1018 JPOOL_UTF_LENGTH (jcf
, sig_index
));
1022 get_constant (JCF
*jcf
, int index
)
1026 if (index
<= 0 || index
>= JPOOL_SIZE(jcf
))
1028 tag
= JPOOL_TAG (jcf
, index
);
1029 if ((tag
& CONSTANT_ResolvedFlag
) || tag
== CONSTANT_Utf8
)
1030 return jcf
->cpool
.data
[index
].t
;
1033 case CONSTANT_Integer
:
1035 jint num
= JPOOL_INT(jcf
, index
);
1036 value
= build_int_cst (int_type_node
, num
);
1041 unsigned HOST_WIDE_INT num
;
1044 num
= JPOOL_UINT (jcf
, index
);
1045 val
= double_int::from_uhwi (num
).llshift (32, 64);
1046 num
= JPOOL_UINT (jcf
, index
+ 1);
1047 val
|= double_int::from_uhwi (num
);
1049 value
= double_int_to_tree (long_type_node
, val
);
1053 case CONSTANT_Float
:
1055 jint num
= JPOOL_INT(jcf
, index
);
1059 real_from_target_fmt (&d
, &buf
, &ieee_single_format
);
1060 value
= build_real (float_type_node
, d
);
1064 case CONSTANT_Double
:
1066 long buf
[2], lo
, hi
;
1069 hi
= JPOOL_UINT (jcf
, index
);
1070 lo
= JPOOL_UINT (jcf
, index
+1);
1072 if (targetm
.float_words_big_endian ())
1073 buf
[0] = hi
, buf
[1] = lo
;
1075 buf
[0] = lo
, buf
[1] = hi
;
1077 real_from_target_fmt (&d
, buf
, &ieee_double_format
);
1078 value
= build_real (double_type_node
, d
);
1082 case CONSTANT_String
:
1084 tree name
= get_name_constant (jcf
, JPOOL_USHORT1 (jcf
, index
));
1085 const char *utf8_ptr
= IDENTIFIER_POINTER (name
);
1086 int utf8_len
= IDENTIFIER_LENGTH (name
);
1087 const unsigned char *utf8
;
1090 /* Check for a malformed Utf8 string. */
1091 utf8
= (const unsigned char *) utf8_ptr
;
1095 int char_len
= UT8_CHAR_LENGTH (*utf8
);
1096 if (char_len
< 0 || char_len
> 3 || char_len
> i
)
1097 fatal_error ("bad string constant");
1103 /* Allocate a new string value. */
1104 value
= build_string (utf8_len
, utf8_ptr
);
1105 TREE_TYPE (value
) = build_pointer_type (string_type_node
);
1111 JPOOL_TAG (jcf
, index
) = tag
| CONSTANT_ResolvedFlag
;
1112 jcf
->cpool
.data
[index
].t
= value
;
1115 fatal_error ("bad value constant type %d, index %d",
1116 JPOOL_TAG (jcf
, index
), index
);
1120 get_name_constant (JCF
*jcf
, int index
)
1122 tree name
= get_constant (jcf
, index
);
1123 gcc_assert (TREE_CODE (name
) == IDENTIFIER_NODE
);
1127 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1128 a non anonymous entry) is found, We augment the inner class list of
1129 the outer context with the newly resolved innerclass. */
1132 handle_innerclass_attribute (int count
, JCF
*jcf
, int attribute_length
)
1136 annotation_write_byte (JV_CLASS_ATTR
);
1137 annotation_write_int (attribute_length
+1);
1138 annotation_write_byte (JV_INNER_CLASSES_KIND
);
1139 annotation_write_short (count
);
1143 /* Read inner_class_info_index. This may be 0 */
1144 int icii
= JCF_readu2 (jcf
);
1145 /* Read outer_class_info_index. If the innerclasses attribute
1146 entry isn't a member (like an inner class) the value is 0. */
1147 int ocii
= JCF_readu2 (jcf
);
1148 /* Read inner_name_index. If the class we're dealing with is
1149 an anonymous class, it must be 0. */
1150 int ini
= JCF_readu2 (jcf
);
1151 /* Read the access flag. */
1152 int acc
= JCF_readu2 (jcf
);
1154 annotation_write_short (handle_constant (jcf
, icii
, CONSTANT_Class
));
1155 annotation_write_short (handle_constant (jcf
, ocii
, CONSTANT_Class
));
1156 annotation_write_short (handle_constant (jcf
, ini
, CONSTANT_Utf8
));
1157 annotation_write_short (acc
);
1159 /* If icii is 0, don't try to read the class. */
1162 tree klass
= get_class_constant (jcf
, icii
);
1163 tree decl
= TYPE_NAME (klass
);
1164 /* Skip reading further if ocii is null */
1165 if (DECL_P (decl
) && !CLASS_COMPLETE_P (decl
) && ocii
)
1167 tree outer
= TYPE_NAME (get_class_constant (jcf
, ocii
));
1168 tree alias
= (ini
? get_name_constant (jcf
, ini
) : NULL_TREE
);
1169 set_class_decl_access_flags (acc
, decl
);
1170 DECL_CONTEXT (decl
) = outer
;
1171 DECL_INNER_CLASS_LIST (outer
) =
1172 tree_cons (decl
, alias
, DECL_INNER_CLASS_LIST (outer
));
1173 CLASS_COMPLETE_P (decl
) = 1;
1180 give_name_to_class (JCF
*jcf
, int i
)
1183 && i
< JPOOL_SIZE (jcf
)
1184 && JPOOL_TAG (jcf
, i
) == CONSTANT_Class
);
1187 tree package_name
= NULL_TREE
, tmp
;
1189 int j
= JPOOL_USHORT1 (jcf
, i
);
1190 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1191 tree class_name
= unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf
, j
),
1192 JPOOL_UTF_LENGTH (jcf
, j
));
1193 this_class
= lookup_class (class_name
);
1195 tree source_name
= identifier_subst (class_name
, "", '.', '/', ".java");
1196 const char *sfname
= find_sourcefile (IDENTIFIER_POINTER (source_name
));
1197 linemap_add (line_table
, LC_ENTER
, false, sfname
, 0);
1198 input_location
= linemap_line_start (line_table
, 0, 1);
1199 file_start_location
= input_location
;
1200 DECL_SOURCE_LOCATION (TYPE_NAME (this_class
)) = input_location
;
1201 if (main_input_filename
== NULL
&& jcf
== main_jcf
)
1202 main_input_filename
= sfname
;
1205 jcf
->cpool
.data
[i
].t
= this_class
;
1206 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
1207 split_qualified_name (&package_name
, &tmp
,
1208 DECL_NAME (TYPE_NAME (this_class
)));
1209 TYPE_PACKAGE (this_class
) = package_name
;
1214 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1217 get_class_constant (JCF
*jcf
, int i
)
1221 && i
< JPOOL_SIZE (jcf
)
1222 && (JPOOL_TAG (jcf
, i
) & ~CONSTANT_ResolvedFlag
) == CONSTANT_Class
);
1224 if (JPOOL_TAG (jcf
, i
) != CONSTANT_ResolvedClass
)
1226 int name_index
= JPOOL_USHORT1 (jcf
, i
);
1227 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1228 const char *name
= (const char *) JPOOL_UTF_DATA (jcf
, name_index
);
1229 int nlength
= JPOOL_UTF_LENGTH (jcf
, name_index
);
1231 if (name
[0] == '[') /* Handle array "classes". */
1232 type
= TREE_TYPE (parse_signature_string ((const unsigned char *) name
, nlength
));
1235 tree cname
= unmangle_classname (name
, nlength
);
1236 type
= lookup_class (cname
);
1238 jcf
->cpool
.data
[i
].t
= type
;
1239 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
1242 type
= jcf
->cpool
.data
[i
].t
;
1246 /* Read a class with the fully qualified-name NAME.
1247 Return 1 iff we read the requested file.
1248 (It is still possible we failed if the file did not
1249 define the class it is supposed to.) */
1252 read_class (tree name
)
1255 tree icv
, klass
= NULL_TREE
;
1256 tree save_current_class
= current_class
;
1257 tree save_output_class
= output_class
;
1258 location_t save_location
= input_location
;
1259 JCF
*save_current_jcf
= current_jcf
;
1261 if ((icv
= IDENTIFIER_CLASS_VALUE (name
)) != NULL_TREE
)
1263 klass
= TREE_TYPE (icv
);
1264 jcf
= TYPE_JCF (klass
);
1271 const char* path_name
;
1272 this_jcf
.zipd
= NULL
;
1275 path_name
= find_class (IDENTIFIER_POINTER (name
),
1276 IDENTIFIER_LENGTH (name
),
1281 free(CONST_CAST (char *, path_name
));
1286 if (klass
== NULL_TREE
|| ! CLASS_PARSED_P (klass
))
1288 output_class
= current_class
= klass
;
1289 if (JCF_SEEN_IN_ZIP (current_jcf
))
1290 read_zip_member(current_jcf
,
1291 current_jcf
->zipd
, current_jcf
->zipd
->zipf
);
1292 jcf_parse (current_jcf
);
1293 /* Parsing might change the class, in which case we have to
1294 put it back where we found it. */
1295 if (current_class
!= klass
&& icv
!= NULL_TREE
)
1296 TREE_TYPE (icv
) = current_class
;
1297 klass
= current_class
;
1299 layout_class (klass
);
1300 load_inner_classes (klass
);
1302 output_class
= save_output_class
;
1303 current_class
= save_current_class
;
1304 input_location
= save_location
;
1305 current_jcf
= save_current_jcf
;
1309 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1310 called from the parser, otherwise it's a RECORD_TYPE node. If
1311 VERBOSE is 1, print error message on failure to load a class. */
1313 load_class (tree class_or_name
, int verbose
)
1316 int class_loaded
= 0;
1317 tree class_decl
= NULL_TREE
;
1318 bool is_compiled_class
= false;
1320 /* We've already failed, don't try again. */
1321 if (TREE_CODE (class_or_name
) == RECORD_TYPE
1322 && TYPE_DUMMY (class_or_name
))
1325 /* class_or_name can be the name of the class we want to load */
1326 if (TREE_CODE (class_or_name
) == IDENTIFIER_NODE
)
1327 name
= class_or_name
;
1328 /* In some cases, it's a dependency that we process earlier that
1330 else if (TREE_CODE (class_or_name
) == TREE_LIST
)
1331 name
= TYPE_NAME (TREE_PURPOSE (class_or_name
));
1332 /* Or it's a type in the making */
1334 name
= DECL_NAME (TYPE_NAME (class_or_name
));
1336 class_decl
= IDENTIFIER_CLASS_VALUE (name
);
1337 if (class_decl
!= NULL_TREE
)
1339 tree type
= TREE_TYPE (class_decl
);
1341 = ((TYPE_JCF (type
) && JCF_SEEN_IN_ZIP (TYPE_JCF (type
)))
1342 || CLASS_FROM_CURRENTLY_COMPILED_P (type
));
1347 /* If flag_verify_invocations is unset, we don't try to load a class
1348 unless we're looking for Object (which is fixed by the ABI) or
1349 it's a class that we're going to compile. */
1350 if (flag_verify_invocations
1351 || class_or_name
== object_type_node
1352 || is_compiled_class
1353 || TREE_CODE (class_or_name
) == IDENTIFIER_NODE
)
1357 const char *separator
;
1359 /* We've already loaded it. */
1360 if (IDENTIFIER_CLASS_VALUE (name
) != NULL_TREE
)
1362 tree tmp_decl
= IDENTIFIER_CLASS_VALUE (name
);
1363 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl
)))
1367 if (read_class (name
))
1370 /* We failed loading name. Now consider that we might be looking
1371 for an inner class. */
1372 if ((separator
= strrchr (IDENTIFIER_POINTER (name
), '$'))
1373 || (separator
= strrchr (IDENTIFIER_POINTER (name
), '.')))
1374 name
= get_identifier_with_length (IDENTIFIER_POINTER (name
),
1376 - IDENTIFIER_POINTER (name
)));
1377 /* Otherwise, we failed, we bail. */
1383 /* have we found the class we're looking for? */
1384 tree type_decl
= IDENTIFIER_CLASS_VALUE (saved
);
1385 tree type
= type_decl
? TREE_TYPE (type_decl
) : NULL
;
1386 class_loaded
= type
&& CLASS_PARSED_P (type
);
1392 if (flag_verify_invocations
|| ! flag_indirect_dispatch
)
1395 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved
));
1399 /* This is just a diagnostic during testing, not a real problem. */
1401 warning (0, "cannot find file for class %s",
1402 IDENTIFIER_POINTER (saved
));
1405 if (TREE_CODE (class_or_name
) == RECORD_TYPE
)
1407 set_super_info (0, class_or_name
, object_type_node
, 0);
1408 TYPE_DUMMY (class_or_name
) = 1;
1409 /* We won't be able to output any debug info for this class. */
1410 DECL_IGNORED_P (TYPE_NAME (class_or_name
)) = 1;
1416 /* Parse the .class file JCF. */
1419 jcf_parse (JCF
* jcf
)
1423 bitmap_clear (field_offsets
);
1425 if (jcf_parse_preamble (jcf
) != 0)
1426 fatal_error ("not a valid Java .class file");
1427 code
= jcf_parse_constant_pool (jcf
);
1429 fatal_error ("error while parsing constant pool");
1430 code
= verify_constant_pool (jcf
);
1432 fatal_error ("error in constant pool entry #%d\n", code
);
1434 jcf_parse_class (jcf
);
1435 if (main_class
== NULL_TREE
)
1436 main_class
= current_class
;
1437 if (! quiet_flag
&& TYPE_NAME (current_class
))
1438 fprintf (stderr
, " %s %s",
1439 (jcf
->access_flags
& ACC_INTERFACE
) ? "interface" : "class",
1440 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class
))));
1441 if (CLASS_PARSED_P (current_class
))
1443 /* FIXME - where was first time */
1444 fatal_error ("reading class %s for the second time from %s",
1445 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class
))),
1448 CLASS_PARSED_P (current_class
) = 1;
1450 for (i
= 1; i
< JPOOL_SIZE(jcf
); i
++)
1452 switch (JPOOL_TAG (jcf
, i
))
1454 case CONSTANT_Class
:
1455 get_class_constant (jcf
, i
);
1460 code
= jcf_parse_fields (jcf
);
1462 fatal_error ("error while parsing fields");
1463 code
= jcf_parse_methods (jcf
);
1465 fatal_error ("error while parsing methods");
1466 code
= jcf_parse_final_attributes (jcf
);
1468 fatal_error ("error while parsing final attributes");
1470 if (TYPE_REFLECTION_DATA (current_class
))
1471 annotation_write_byte (JV_DONE_ATTR
);
1473 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1475 /* The fields of class_type_node are already in correct order. */
1476 if (current_class
!= class_type_node
&& current_class
!= object_type_node
)
1477 TYPE_FIELDS (current_class
) = nreverse (TYPE_FIELDS (current_class
));
1479 if (current_class
== object_type_node
)
1480 layout_class_methods (object_type_node
);
1482 vec_safe_push (all_class_list
, TYPE_NAME (current_class
));
1485 /* If we came across inner classes, load them now. */
1487 load_inner_classes (tree cur_class
)
1490 for (current
= DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class
)); current
;
1491 current
= TREE_CHAIN (current
))
1493 tree name
= DECL_NAME (TREE_PURPOSE (current
));
1494 tree decl
= IDENTIFIER_GLOBAL_VALUE (name
);
1495 if (decl
&& ! CLASS_LOADED_P (TREE_TYPE (decl
))
1496 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl
)))
1497 load_class (name
, 1);
1502 duplicate_class_warning (const char *filename
)
1504 location_t warn_loc
;
1505 linemap_add (line_table
, LC_RENAME
, 0, filename
, 0);
1506 warn_loc
= linemap_line_start (line_table
, 0, 1);
1507 warning_at (warn_loc
, 0, "duplicate class will only be compiled once");
1511 java_layout_seen_class_methods (void)
1514 unsigned end
= vec_safe_length (all_class_list
);
1519 unsigned new_length
;
1521 for (ix
= start
; ix
!= end
; ix
++)
1523 tree decl
= (*all_class_list
)[ix
];
1524 tree cls
= TREE_TYPE (decl
);
1526 input_location
= DECL_SOURCE_LOCATION (decl
);
1528 if (! CLASS_LOADED_P (cls
))
1529 load_class (cls
, 0);
1531 layout_class_methods (cls
);
1534 /* Note that new classes might have been added while laying out
1535 methods, changing the value of all_class_list. */
1536 new_length
= vec_safe_length (all_class_list
);
1537 if (end
!= new_length
)
1548 parse_class_file (void)
1551 location_t save_location
= input_location
;
1553 java_layout_seen_class_methods ();
1555 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (current_class
));
1557 /* Re-enter the current file. */
1558 expanded_location loc
= expand_location (input_location
);
1559 linemap_add (line_table
, LC_ENTER
, 0, loc
.file
, loc
.line
);
1561 file_start_location
= input_location
;
1562 (*debug_hooks
->start_source_file
) (input_line
, input_filename
);
1564 java_mark_class_local (current_class
);
1566 gen_indirect_dispatch_tables (current_class
);
1568 for (method
= TYPE_METHODS (current_class
);
1569 method
!= NULL_TREE
; method
= DECL_CHAIN (method
))
1571 JCF
*jcf
= current_jcf
;
1573 if (METHOD_ABSTRACT (method
) || METHOD_DUMMY (method
))
1576 if (METHOD_NATIVE (method
))
1579 int decl_max_locals
;
1583 /* We need to compute the DECL_MAX_LOCALS. We need to take
1584 the wide types into account too. */
1585 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (method
)), decl_max_locals
= 0;
1586 arg
!= end_params_node
;
1587 arg
= TREE_CHAIN (arg
), decl_max_locals
+= 1)
1589 if (TREE_VALUE (arg
) && TYPE_IS_WIDE (TREE_VALUE (arg
)))
1590 decl_max_locals
+= 1;
1592 DECL_MAX_LOCALS (method
) = decl_max_locals
;
1593 start_java_method (method
);
1594 give_name_to_locals (jcf
);
1595 *get_stmts () = build_jni_stub (method
);
1600 if (DECL_CODE_OFFSET (method
) == 0)
1602 current_function_decl
= method
;
1603 error ("missing Code attribute");
1607 input_location
= DECL_SOURCE_LOCATION (TYPE_NAME (current_class
));
1608 if (DECL_LINENUMBERS_OFFSET (method
))
1613 JCF_SEEK (jcf
, DECL_LINENUMBERS_OFFSET (method
));
1614 linenumber_count
= i
= JCF_readu2 (jcf
);
1615 linenumber_table
= ptr
= jcf
->read_ptr
;
1617 for (ptr
+= 2; --i
>= 0; ptr
+= 4)
1619 int line
= GET_u2 (ptr
);
1620 /* Set initial input_line to smallest linenumber.
1621 * Needs to be set before init_function_start. */
1622 if (min_line
== 0 || line
< min_line
)
1626 input_location
= linemap_line_start (line_table
, min_line
, 1);
1630 linenumber_table
= NULL
;
1631 linenumber_count
= 0;
1634 start_java_method (method
);
1636 note_instructions (jcf
, method
);
1638 give_name_to_locals (jcf
);
1640 /* Bump up start_label_pc_this_method so we get a unique label number
1641 and reset highest_label_pc_this_method. */
1642 if (highest_label_pc_this_method
>= 0)
1644 /* We adjust to the next multiple of 1000. This is just a frill
1645 so the last 3 digits of the label number match the bytecode
1646 offset, which might make debugging marginally more convenient. */
1647 start_label_pc_this_method
1648 = ((((start_label_pc_this_method
+ highest_label_pc_this_method
)
1652 highest_label_pc_this_method
= -1;
1655 /* Convert bytecode to trees. */
1656 expand_byte_code (jcf
, method
);
1663 (*debug_hooks
->end_source_file
) (LOCATION_LINE (save_location
));
1664 input_location
= save_location
;
1667 static vec
<tree
, va_gc
> *predefined_filenames
;
1670 add_predefined_file (tree name
)
1672 vec_safe_push (predefined_filenames
, name
);
1676 predefined_filename_p (tree node
)
1681 FOR_EACH_VEC_SAFE_ELT (predefined_filenames
, ix
, f
)
1688 /* Generate a function that does all static initialization for this
1689 translation unit. */
1692 java_emit_static_constructor (void)
1696 emit_register_classes (&body
);
1697 write_resource_constructor (&body
);
1701 tree name
= get_identifier ("_Jv_global_static_constructor");
1704 = build_decl (input_location
, FUNCTION_DECL
, name
,
1705 build_function_type_list (void_type_node
, NULL_TREE
));
1707 tree resdecl
= build_decl (input_location
,
1708 RESULT_DECL
, NULL_TREE
, void_type_node
);
1709 DECL_ARTIFICIAL (resdecl
) = 1;
1710 DECL_RESULT (decl
) = resdecl
;
1711 current_function_decl
= decl
;
1712 allocate_struct_function (decl
, false);
1714 TREE_STATIC (decl
) = 1;
1715 TREE_USED (decl
) = 1;
1716 DECL_ARTIFICIAL (decl
) = 1;
1717 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
1718 DECL_SAVED_TREE (decl
) = body
;
1719 DECL_UNINLINABLE (decl
) = 1;
1721 DECL_INITIAL (decl
) = make_node (BLOCK
);
1722 TREE_USED (DECL_INITIAL (decl
)) = 1;
1724 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
1725 java_genericize (decl
);
1726 cgraph_finalize_function (decl
, false);
1732 java_parse_file (void)
1734 int filename_count
= 0;
1735 location_t save_location
= input_location
;
1736 char *file_list
= NULL
, *list
, *next
;
1738 FILE *finput
= NULL
;
1742 bitmap_obstack_initialize (&bit_obstack
);
1743 field_offsets
= BITMAP_ALLOC (&bit_obstack
);
1745 if (flag_filelist_file
)
1748 finput
= fopen (main_input_filename
, "r");
1750 fatal_error ("can%'t open %s: %m", input_filename
);
1751 list
= XNEWVEC (char, avail
);
1758 count
= next
- list
;
1759 avail
= 2 * (count
+ avail
);
1760 list
= XRESIZEVEC (char, list
, avail
);
1761 next
= list
+ count
;
1762 avail
= avail
- count
;
1764 /* Subtract one to guarantee space for final '\0'. */
1765 count
= fread (next
, 1, avail
- 1, finput
);
1768 if (! feof (finput
))
1769 fatal_error ("error closing %s: %m", input_filename
);
1781 list
= CONST_CAST (char *, main_input_filename
);
1785 for (next
= list
; ; )
1788 if (flag_filelist_file
&& ! in_quotes
1789 && (ch
== '\n' || ch
== '\r' || ch
== '\t' || ch
== ' '
1790 || ch
== '&') /* FIXME */)
1804 if (flag_filelist_file
&& ch
== '"')
1806 in_quotes
= ! in_quotes
;
1821 /* Exclude .java files. */
1822 if (strlen (list
) > 5 && ! strcmp (list
+ strlen (list
) - 5, ".java"))
1828 node
= get_identifier (list
);
1832 /* Exclude file that we see twice on the command line. */
1834 if (IS_A_COMMAND_LINE_FILENAME_P (node
))
1835 duplicate_class_warning (IDENTIFIER_POINTER (node
));
1838 build_translation_unit_decl (node
);
1839 IS_A_COMMAND_LINE_FILENAME_P (node
) = 1;
1847 if (filename_count
== 0)
1848 warning (0, "no input file specified");
1852 const char *resource_filename
;
1854 /* Only one resource file may be compiled at a time. */
1855 gcc_assert (all_translation_units
->length () == 1);
1858 = IDENTIFIER_POINTER (DECL_NAME ((*all_translation_units
)[0]));
1859 compile_resource_file (resource_name
, resource_filename
);
1864 current_jcf
= main_jcf
;
1865 FOR_EACH_VEC_ELT (*all_translation_units
, ix
, node
)
1867 unsigned char magic_string
[4];
1870 tree name
= DECL_NAME (node
);
1872 const char *filename
= IDENTIFIER_POINTER (name
);
1874 /* Skip already parsed files */
1875 real_path
= lrealpath (filename
);
1876 real_file
= get_identifier (real_path
);
1878 if (HAS_BEEN_ALREADY_PARSED_P (real_file
))
1881 /* Close previous descriptor, if any */
1882 if (finput
&& fclose (finput
))
1883 fatal_error ("can%'t close input file %s: %m", main_input_filename
);
1885 finput
= fopen (filename
, "rb");
1887 fatal_error ("can%'t open %s: %m", filename
);
1889 #ifdef IO_BUFFER_SIZE
1890 setvbuf (finput
, xmalloc (IO_BUFFER_SIZE
),
1891 _IOFBF
, IO_BUFFER_SIZE
);
1894 /* Figure what kind of file we're dealing with */
1895 if (fread (magic_string
, 1, 4, finput
) == 4)
1897 fseek (finput
, 0L, SEEK_SET
);
1898 magic
= GET_u4 (magic_string
);
1900 if (magic
== 0xcafebabe)
1902 CLASS_FILE_P (node
) = 1;
1903 current_jcf
= ggc_alloc_cleared_JCF ();
1904 current_jcf
->read_state
= finput
;
1905 current_jcf
->filbuf
= jcf_filbuf_from_stdio
;
1906 jcf_parse (current_jcf
);
1907 DECL_SOURCE_LOCATION (node
) = file_start_location
;
1908 TYPE_JCF (current_class
) = current_jcf
;
1909 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class
))
1911 /* We've already compiled this class. */
1912 duplicate_class_warning (filename
);
1915 CLASS_FROM_CURRENTLY_COMPILED_P (current_class
) = 1;
1916 TREE_TYPE (node
) = current_class
;
1918 else if (magic
== (JCF_u4
)ZIPMAGIC
)
1920 main_jcf
= ggc_alloc_cleared_JCF ();
1921 main_jcf
->read_state
= finput
;
1922 main_jcf
->filbuf
= jcf_filbuf_from_stdio
;
1923 linemap_add (line_table
, LC_ENTER
, false, filename
, 0);
1924 input_location
= linemap_line_start (line_table
, 0, 1);
1925 if (open_in_zip (main_jcf
, filename
, NULL
, 0) < 0)
1926 fatal_error ("bad zip/jar file %s", filename
);
1927 localToFile
= SeenZipFiles
;
1928 /* Register all the classes defined there. */
1929 process_zip_dir ((FILE *) main_jcf
->read_state
);
1930 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1931 parse_zip_file_entries ();
1933 else if (magic
== (JCF_u4
) ZIPEMPTYMAGIC
)
1935 /* Ignore an empty input jar. */
1941 java_push_parser_context ();
1942 java_parser_context_save_global ();
1944 parse_source_file_1 (real_file
, filename
, finput
);
1945 java_parser_context_restore_global ();
1946 java_pop_parser_context (1);
1947 linemap_add (line_table
, LC_LEAVE
, false, NULL
, 0);
1952 FOR_EACH_VEC_ELT (*all_translation_units
, ix
, node
)
1954 input_location
= DECL_SOURCE_LOCATION (node
);
1955 if (CLASS_FILE_P (node
))
1957 /* FIXME: These two flags really should be independent. We
1958 should be able to compile fully binary compatible, but
1959 with flag_verify_invocations on. */
1960 flag_verify_invocations
= ! flag_indirect_dispatch
;
1961 output_class
= current_class
= TREE_TYPE (node
);
1963 current_jcf
= TYPE_JCF (current_class
);
1964 layout_class (current_class
);
1965 load_inner_classes (current_class
);
1966 parse_class_file ();
1967 JCF_FINISH (current_jcf
);
1970 input_location
= save_location
;
1972 bitmap_obstack_release (&bit_obstack
);
1975 /* Arrange for any necessary initialization to happen. */
1976 java_emit_static_constructor ();
1977 gcc_assert (global_bindings_p ());
1981 /* Return the name of the class corresponding to the name of the file
1982 in this zip entry. The result is newly allocated using ALLOC. */
1984 compute_class_name (struct ZipDirectory
*zdir
)
1986 char *class_name_in_zip_dir
= ZIPDIR_FILENAME (zdir
);
1989 int filename_length
= zdir
->filename_length
;
1991 while (filename_length
> 2 && strncmp (class_name_in_zip_dir
, "./", 2) == 0)
1993 class_name_in_zip_dir
+= 2;
1994 filename_length
-= 2;
1997 filename_length
-= strlen (".class");
1998 class_name
= XNEWVEC (char, filename_length
+ 1);
1999 memcpy (class_name
, class_name_in_zip_dir
, filename_length
);
2000 class_name
[filename_length
] = '\0';
2002 for (i
= 0; i
< filename_length
; i
++)
2003 if (class_name
[i
] == '/')
2004 class_name
[i
] = '.';
2009 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2010 if it is a property file of some sort. */
2012 classify_zip_file (struct ZipDirectory
*zdir
)
2014 char *class_name_in_zip_dir
= ZIPDIR_FILENAME (zdir
);
2016 if (zdir
->filename_length
> 6
2017 && !strncmp (&class_name_in_zip_dir
[zdir
->filename_length
- 6],
2021 /* For now we drop the manifest, but not other information. */
2022 if (zdir
->filename_length
== 20
2023 && !strncmp (class_name_in_zip_dir
, "META-INF/MANIFEST.MF", 20))
2026 /* Drop directory entries. */
2027 if (zdir
->filename_length
> 0
2028 && class_name_in_zip_dir
[zdir
->filename_length
- 1] == '/')
2034 /* Process all class entries found in the zip file. */
2036 parse_zip_file_entries (void)
2038 struct ZipDirectory
*zdir
;
2041 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->central_directory
;
2042 i
< localToFile
->count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
2046 switch (classify_zip_file (zdir
))
2053 char *class_name
= compute_class_name (zdir
);
2054 int previous_alias_set
= -1;
2055 klass
= lookup_class (get_identifier (class_name
));
2057 current_jcf
= TYPE_JCF (klass
);
2058 output_class
= current_class
= klass
;
2060 /* This is a dummy class, and now we're compiling it for
2062 gcc_assert (! TYPE_DUMMY (klass
));
2064 /* This is for a corner case where we have a superclass
2065 but no superclass fields.
2067 This can happen if we earlier failed to lay out this
2068 class because its superclass was still in the process
2069 of being laid out; this occurs when we have recursive
2070 class dependencies via inner classes. We must record
2071 the previous alias set and restore it after laying out
2074 FIXME: this really is a kludge. We should figure out a
2075 way to lay out the class properly before this
2077 if (TYPE_SIZE (klass
) && CLASSTYPE_SUPER (klass
)
2078 && integer_zerop (TYPE_SIZE (klass
)))
2080 TYPE_SIZE (klass
) = NULL_TREE
;
2081 previous_alias_set
= TYPE_ALIAS_SET (klass
);
2082 TYPE_ALIAS_SET (klass
) = -1;
2085 if (! CLASS_LOADED_P (klass
))
2087 if (! CLASS_PARSED_P (klass
))
2089 read_zip_member (current_jcf
, zdir
, localToFile
);
2090 jcf_parse (current_jcf
);
2092 layout_class (current_class
);
2093 load_inner_classes (current_class
);
2096 if (previous_alias_set
!= -1)
2097 TYPE_ALIAS_SET (klass
) = previous_alias_set
;
2099 if (TYPE_SIZE (current_class
) != error_mark_node
)
2101 parse_class_file ();
2102 free (current_jcf
->buffer
); /* No longer necessary */
2103 /* Note: there is a way to free this buffer right after a
2104 class seen in a zip file has been parsed. The idea is the
2105 set its jcf in such a way that buffer will be reallocated
2106 the time the code for the class will be generated. FIXME. */
2113 char *file_name
, *class_name_in_zip_dir
, *buffer
;
2115 file_name
= XNEWVEC (char, zdir
->filename_length
+ 1);
2116 class_name_in_zip_dir
= ZIPDIR_FILENAME (zdir
);
2117 strncpy (file_name
, class_name_in_zip_dir
, zdir
->filename_length
);
2118 file_name
[zdir
->filename_length
] = '\0';
2121 jcf
->read_state
= finput
;
2122 jcf
->filbuf
= jcf_filbuf_from_stdio
;
2123 jcf
->classname
= NULL
;
2124 jcf
->filename
= file_name
;
2127 if (read_zip_member (jcf
, zdir
, localToFile
) < 0)
2128 fatal_error ("error while reading %s from zip file", file_name
);
2130 buffer
= XNEWVEC (char, zdir
->filename_length
+ 1 +
2131 (jcf
->buffer_end
- jcf
->buffer
));
2132 strcpy (buffer
, file_name
);
2133 /* This is not a typo: we overwrite the trailing \0 of the
2134 file name; this is just how the data is laid out. */
2135 memcpy (buffer
+ zdir
->filename_length
,
2136 jcf
->buffer
, jcf
->buffer_end
- jcf
->buffer
);
2138 compile_resource_data (file_name
, buffer
,
2139 jcf
->buffer_end
- jcf
->buffer
);
2152 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
2153 jcf up for further processing and link it to the created class. */
2156 process_zip_dir (FILE *finput
)
2161 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->central_directory
;
2162 i
< localToFile
->count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
2164 char *class_name
, *file_name
, *class_name_in_zip_dir
;
2168 class_name_in_zip_dir
= ZIPDIR_FILENAME (zdir
);
2170 /* Here we skip non-class files; we handle them later. */
2171 if (classify_zip_file (zdir
) != 1)
2174 class_name
= compute_class_name (zdir
);
2175 file_name
= XNEWVEC (char, zdir
->filename_length
+1);
2176 jcf
= ggc_alloc_cleared_JCF ();
2178 strncpy (file_name
, class_name_in_zip_dir
, zdir
->filename_length
);
2179 file_name
[zdir
->filename_length
] = '\0';
2181 klass
= lookup_class (get_identifier (class_name
));
2183 if (CLASS_FROM_CURRENTLY_COMPILED_P (klass
))
2185 /* We've already compiled this class. */
2186 duplicate_class_warning (file_name
);
2189 /* This function is only called when processing a zip file seen
2190 on the command line. */
2191 CLASS_FROM_CURRENTLY_COMPILED_P (klass
) = 1;
2193 jcf
->read_state
= finput
;
2194 jcf
->filbuf
= jcf_filbuf_from_stdio
;
2195 jcf
->classname
= class_name
;
2196 jcf
->filename
= file_name
;
2199 TYPE_JCF (klass
) = jcf
;
2203 #include "gt-java-jcf-parse.h"
2204 #include "gtype-java.h"