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