re PR libfortran/68744 (FAIL: gfortran.dg/backtrace_1.f90 -O0 execution test)
[official-gcc.git] / gcc / java / jcf-parse.c
blobdd3ad3f34b81978222697b987c0d9af5c142ec1e
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Per Bothner <bothner@cygnus.com> */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "function.h"
31 #include "bitmap.h"
32 #include "tree.h"
33 #include "stringpool.h"
34 #include "cgraph.h"
35 #include "diagnostic-core.h"
36 #include "javaop.h"
37 #include "java-tree.h"
38 #include "debug.h"
39 #include "toplev.h"
41 #ifdef HAVE_LOCALE_H
42 #include <locale.h>
43 #endif
45 #ifdef HAVE_LANGINFO_CODESET
46 #include <langinfo.h>
47 #endif
49 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
50 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
51 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
52 #define JPOOL_UTF_DATA(JCF, INDEX) \
53 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
54 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
55 do { \
56 unsigned char save; unsigned char *text; \
57 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
58 text = (JCF)->read_ptr; \
59 save = text[LENGTH]; \
60 text[LENGTH] = 0; \
61 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
62 text[LENGTH] = save; \
63 JCF_SKIP (JCF, LENGTH); } while (0)
65 #include "jcf.h"
67 extern struct obstack temporary_obstack;
69 static GTY(()) tree parse_roots[2];
71 /* The FIELD_DECL for the current field. */
72 #define current_field parse_roots[0]
74 /* The METHOD_DECL for the current method. */
75 #define current_method parse_roots[1]
77 /* Line 0 in current file, if compiling from bytecode. */
78 static location_t file_start_location;
80 /* The Java archive that provides main_class; the main input file. */
81 static GTY(()) struct JCF * main_jcf;
83 /* A list of all the class DECLs seen so far. */
84 static GTY(()) vec<tree, va_gc> *all_class_list;
86 /* The number of source files passed to us by -fsource-filename and an
87 array of pointers to each name. Used by find_sourcefile(). */
88 static int num_files = 0;
89 static char **filenames;
91 static struct ZipFile *localToFile;
93 /* A map of byte offsets in the reflection data that are fields which
94 need renumbering. */
95 bitmap field_offsets;
96 bitmap_obstack bit_obstack;
98 /* Declarations of some functions used here. */
99 static void handle_innerclass_attribute (int count, JCF *, int len);
100 static tree give_name_to_class (JCF *jcf, int index);
101 static char *compute_class_name (struct ZipDirectory *zdir);
102 static int classify_zip_file (struct ZipDirectory *zdir);
103 static void parse_zip_file_entries (void);
104 static void process_zip_dir (FILE *);
105 static void parse_class_file (void);
106 static void handle_deprecated (void);
107 static void set_source_filename (JCF *, int);
108 static void jcf_parse (struct JCF*);
109 static void load_inner_classes (tree);
110 static void handle_annotation (JCF *jcf, int level);
111 static void java_layout_seen_class_methods (void);
113 /* Handle "Deprecated" attribute. */
114 static void
115 handle_deprecated (void)
117 if (current_field != NULL_TREE)
118 FIELD_DEPRECATED (current_field) = 1;
119 else if (current_method != NULL_TREE)
120 METHOD_DEPRECATED (current_method) = 1;
121 else if (current_class != NULL_TREE)
122 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
123 else
125 /* Shouldn't happen. */
126 gcc_unreachable ();
132 /* Reverse a string. */
133 static char *
134 reverse (const char *s)
136 if (s == NULL)
137 return NULL;
138 else
140 int len = strlen (s);
141 char *d = XNEWVAR (char, len + 1);
142 const char *sp;
143 char *dp;
145 d[len] = 0;
146 for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
147 *dp = *sp;
149 return d;
153 /* Compare two strings for qsort(). */
154 static int
155 cmpstringp (const void *p1, const void *p2)
157 /* The arguments to this function are "pointers to
158 pointers to char", but strcmp() arguments are "pointers
159 to char", hence the following cast plus dereference */
161 return strcmp(*(const char *const*) p1, *(const char *const*) p2);
164 /* Create an array of strings, one for each source file that we've
165 seen. fsource_filename can either be the name of a single .java
166 file or a file that contains a list of filenames separated by
167 newlines. */
168 void
169 java_read_sourcefilenames (const char *fsource_filename)
171 if (fsource_filename
172 && filenames == 0
173 && strlen (fsource_filename) > strlen (".java")
174 && filename_cmp ((fsource_filename
175 + strlen (fsource_filename)
176 - strlen (".java")),
177 ".java") != 0)
179 /* fsource_filename isn't a .java file but a list of filenames
180 separated by newlines */
181 FILE *finput = fopen (fsource_filename, "r");
182 int len = 0;
183 int longest_line = 0;
185 gcc_assert (finput);
187 /* Find out how many files there are, and how long the filenames are. */
188 while (! feof (finput))
190 int ch = getc (finput);
191 if (ch == '\n')
193 num_files++;
194 if (len > longest_line)
195 longest_line = len;
196 len = 0;
197 continue;
199 if (ch == EOF)
200 break;
201 len++;
204 rewind (finput);
206 /* Read the filenames. Put a pointer to each filename into the
207 array FILENAMES. */
209 char *linebuf = (char *) alloca (longest_line + 1);
210 int i = 0;
211 int charpos;
213 filenames = XNEWVEC (char *, num_files);
215 charpos = 0;
216 for (;;)
218 int ch = getc (finput);
219 if (ch == EOF)
220 break;
221 if (ch == '\n')
223 linebuf[charpos] = 0;
224 gcc_assert (i < num_files);
225 /* ??? Perhaps we should use lrealpath() here. Doing
226 so would tidy up things like /../ but the rest of
227 gcc seems to assume relative pathnames, not
228 absolute pathnames. */
229 /* realname = lrealpath (linebuf); */
230 filenames[i++] = reverse (linebuf);
231 charpos = 0;
232 continue;
234 gcc_assert (charpos < longest_line);
235 linebuf[charpos++] = ch;
238 if (num_files > 1)
239 qsort (filenames, num_files, sizeof (char *), cmpstringp);
241 fclose (finput);
243 else
245 filenames = XNEWVEC (char *, 1);
246 filenames[0] = reverse (fsource_filename);
247 num_files = 1;
251 /* Given a relative pathname such as foo/bar.java, attempt to find a
252 longer pathname with the same suffix.
254 This is a best guess heuristic; with some weird class hierarchies we
255 may fail to pick the correct source file. For example, if we have
256 the filenames foo/bar.java and also foo/foo/bar.java, we do not
257 have enough information to know which one is the right match for
258 foo/bar.java. */
260 static const char *
261 find_sourcefile (const char *name)
263 int i = 0, j = num_files-1;
264 char *found = NULL;
266 if (filenames)
268 char *revname = reverse (name);
272 int k = (i+j) / 2;
273 int cmp = strncmp (revname, filenames[k], strlen (revname));
274 if (cmp == 0)
276 /* OK, so we found one. But is it a unique match? */
277 if ((k > i
278 && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
279 || (k < j
280 && (strncmp (revname, filenames[k+1], strlen (revname))
281 == 0)))
283 else
284 found = filenames[k];
285 break;
287 if (cmp > 0)
288 i = k+1;
289 else
290 j = k-1;
292 while (i <= j);
294 free (revname);
297 if (found && strlen (found) > strlen (name))
298 return reverse (found);
299 else
300 return name;
305 /* Handle "SourceFile" attribute. */
307 static void
308 set_source_filename (JCF *jcf, int index)
310 tree sfname_id = get_name_constant (jcf, index);
311 const char *sfname = IDENTIFIER_POINTER (sfname_id);
312 const char *old_filename = LOCATION_FILE (input_location);
313 int new_len = IDENTIFIER_LENGTH (sfname_id);
314 if (old_filename != NULL)
316 int old_len = strlen (old_filename);
317 /* Use the filename from current input_location (derived from the
318 class name) if it has a directory prefix, but otherwise matches
319 sfname. */
320 if (old_len > new_len
321 && filename_cmp (sfname, old_filename + old_len - new_len) == 0
322 && (old_filename[old_len - new_len - 1] == '/'
323 || old_filename[old_len - new_len - 1] == '\\'))
324 return;
326 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
328 const char *class_name
329 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
330 const char *dot = strrchr (class_name, '.');
331 if (dot != NULL)
333 /* Length of prefix, not counting final dot. */
334 int i = dot - class_name;
335 /* Concatenate current package prefix with new sfname. */
336 char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
337 strcpy (buf + i + 1, sfname);
338 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
339 Note we start at the end with the final package dot. */
340 for (; i >= 0; i--)
342 char c = class_name[i];
343 if (c == '.')
344 c = DIR_SEPARATOR;
345 buf[i] = c;
347 sfname_id = get_identifier (buf);
348 free (buf);
349 sfname = IDENTIFIER_POINTER (sfname_id);
353 sfname = find_sourcefile (sfname);
354 LINEMAPS_LAST_ORDINARY_MAP (line_table)->to_file = sfname;
355 if (current_class == main_class) main_input_filename = sfname;
361 /* Annotation handling.
363 The technique we use here is to copy the annotation data directly
364 from the input class file into the output file. We don't decode the
365 data at all, merely rewriting constant indexes whenever we come
366 across them: this is necessary because the constant pool in the
367 output file isn't the same as the constant pool in the input.
369 The main advantage of this technique is that the resulting
370 annotation data is pointer-free, so it doesn't have to be relocated
371 at startup time. As a consequence of this, annotations have no
372 performance impact unless they are used. Also, this representation
373 is very dense. */
376 /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
377 the start of the newly allocated region. */
379 static unsigned char*
380 annotation_grow (int delta)
382 unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
383 long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
384 long len = *datasize;
386 if (*data == NULL)
388 *data = XNEWVAR (unsigned char, delta);
390 else
392 int newlen = *datasize + delta;
393 if (floor_log2 (newlen) != floor_log2 (*datasize))
394 *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
396 *datasize += delta;
397 return *data + len;
400 /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
401 byte order (i.e. big endian.) */
403 static void
404 annotation_rewrite_byte (unsigned int n, unsigned char *p)
406 p[0] = n;
409 static void
410 annotation_rewrite_short (unsigned int n, unsigned char *p)
412 p[0] = n>>8;
413 p[1] = n;
416 static void
417 annotation_rewrite_int (unsigned int n, unsigned char *p)
419 p[0] = n>>24;
420 p[1] = n>>16;
421 p[2] = n>>8;
422 p[3] = n;
425 /* Read a 16-bit unsigned int in Java byte order (i.e. big
426 endian.) */
428 static uint16
429 annotation_read_short (unsigned char *p)
431 uint16 tmp = p[0];
432 tmp = (tmp << 8) | p[1];
433 return tmp;
436 /* annotation_write_TYPE. Rewrite various int types, appending them
437 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
438 endian.) */
440 static void
441 annotation_write_byte (unsigned int n)
443 annotation_rewrite_byte (n, annotation_grow (1));
446 static void
447 annotation_write_short (unsigned int n)
449 annotation_rewrite_short (n, annotation_grow (2));
452 static void
453 annotation_write_int (unsigned int n)
455 annotation_rewrite_int (n, annotation_grow (4));
458 /* Create a 64-bit constant in the constant pool.
460 This is used for both integer and floating-point types. As a
461 consequence, it will not work if the target floating-point format
462 is anything other than IEEE-754. While this is arguably a bug, the
463 runtime library makes exactly the same assumption and it's unlikely
464 that Java will ever run on a non-IEEE machine. */
466 static int
467 handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
468 int index, bool big_endian)
470 /* If we're on a 64-bit platform we can fit a long or double
471 into the same space as a jword. */
472 if (POINTER_SIZE >= 64)
473 index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
475 /* In a compiled program the constant pool is in native word
476 order. How weird is that??? */
477 else if (big_endian)
478 index = find_constant2 (cpool, kind,
479 JPOOL_INT (jcf, index),
480 JPOOL_INT (jcf, index+1));
481 else
482 index = find_constant2 (cpool, kind,
483 JPOOL_INT (jcf, index+1),
484 JPOOL_INT (jcf, index));
486 return index;
489 /* Given a class file and an index into its constant pool, create an
490 entry in the outgoing constant pool for the same item. */
492 static uint16
493 handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
495 unsigned int kind;
496 CPool *cpool = cpool_for_class (output_class);
498 if (index == 0)
499 return 0;
501 if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
502 error ("<constant pool index %d not in range>", index);
504 kind = JPOOL_TAG (jcf, index);
506 if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
508 if (purpose == CONSTANT_Class
509 && kind == CONSTANT_Utf8)
511 else
512 error ("<constant pool index %d unexpected type", index);
515 switch (kind)
517 case CONSTANT_Class:
518 case CONSTANT_ResolvedClass:
520 /* For some reason I know not the what of, class names in
521 annotations are UTF-8 strings in the constant pool but
522 class names in EnclosingMethod attributes are real class
523 references. Set CONSTANT_LazyFlag here so that the VM
524 doesn't attempt to resolve them at class initialization
525 time. */
526 tree resolved_class, class_name;
527 resolved_class = get_class_constant (jcf, index);
528 class_name = build_internal_class_name (resolved_class);
529 index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
530 (unmangle_classname
531 (IDENTIFIER_POINTER(class_name),
532 IDENTIFIER_LENGTH(class_name))));
533 break;
535 case CONSTANT_Utf8:
537 tree utf8 = get_constant (jcf, index);
538 if (purpose == CONSTANT_Class)
539 /* Create a constant pool entry for a type signature. This
540 one has '.' rather than '/' because it isn't going into a
541 class file, it's going into a compiled object.
543 This has to match the logic in
544 _Jv_ClassReader::prepare_pool_entry(). */
545 utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
546 IDENTIFIER_LENGTH(utf8));
547 index = alloc_name_constant (kind, utf8);
549 break;
551 case CONSTANT_Long:
552 index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
553 targetm.words_big_endian ());
554 break;
556 case CONSTANT_Double:
557 index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
558 targetm.float_words_big_endian ());
559 break;
561 case CONSTANT_Float:
562 case CONSTANT_Integer:
563 index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
564 break;
566 case CONSTANT_NameAndType:
568 uint16 name = JPOOL_USHORT1 (jcf, index);
569 uint16 sig = JPOOL_USHORT2 (jcf, index);
570 uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
571 uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
572 jword new_index = (name_index << 16) | sig_index;
573 index = find_constant1 (cpool, kind, new_index);
575 break;
577 default:
578 abort ();
581 return index;
584 /* Read an element_value structure from an annotation in JCF. Return
585 the constant pool index for the resulting constant pool entry. */
587 static int
588 handle_element_value (JCF *jcf, int level)
590 uint8 tag = JCF_readu (jcf);
591 int index = 0;
593 annotation_write_byte (tag);
594 switch (tag)
596 case 'B':
597 case 'C':
598 case 'S':
599 case 'Z':
600 case 'I':
602 uint16 cindex = JCF_readu2 (jcf);
603 index = handle_constant (jcf, cindex,
604 CONSTANT_Integer);
605 annotation_write_short (index);
607 break;
608 case 'D':
610 uint16 cindex = JCF_readu2 (jcf);
611 index = handle_constant (jcf, cindex,
612 CONSTANT_Double);
613 annotation_write_short (index);
615 break;
616 case 'F':
618 uint16 cindex = JCF_readu2 (jcf);
619 index = handle_constant (jcf, cindex,
620 CONSTANT_Float);
621 annotation_write_short (index);
623 break;
624 case 'J':
626 uint16 cindex = JCF_readu2 (jcf);
627 index = handle_constant (jcf, cindex,
628 CONSTANT_Long);
629 annotation_write_short (index);
631 break;
632 case 's':
634 uint16 cindex = JCF_readu2 (jcf);
635 /* Despite what the JVM spec says, compilers generate a Utf8
636 constant here, not a String. */
637 index = handle_constant (jcf, cindex,
638 CONSTANT_Utf8);
639 annotation_write_short (index);
641 break;
643 case 'e':
645 uint16 type_name_index = JCF_readu2 (jcf);
646 uint16 const_name_index = JCF_readu2 (jcf);
647 index = handle_constant (jcf, type_name_index,
648 CONSTANT_Class);
649 annotation_write_short (index);
650 index = handle_constant (jcf, const_name_index,
651 CONSTANT_Utf8);
652 annotation_write_short (index);
654 break;
655 case 'c':
657 uint16 class_info_index = JCF_readu2 (jcf);
658 index = handle_constant (jcf, class_info_index,
659 CONSTANT_Class);
660 annotation_write_short (index);
662 break;
663 case '@':
665 handle_annotation (jcf, level + 1);
667 break;
668 case '[':
670 uint16 n_array_elts = JCF_readu2 (jcf);
671 annotation_write_short (n_array_elts);
672 while (n_array_elts--)
673 handle_element_value (jcf, level + 1);
675 break;
676 default:
677 abort();
678 break;
680 return index;
683 /* Read an annotation structure from JCF. Write it to the
684 reflection_data field of the outgoing class. */
686 static void
687 handle_annotation (JCF *jcf, int level)
689 uint16 type_index = JCF_readu2 (jcf);
690 uint16 npairs = JCF_readu2 (jcf);
691 int index = handle_constant (jcf, type_index,
692 CONSTANT_Class);
693 annotation_write_short (index);
694 annotation_write_short (npairs);
695 while (npairs--)
697 uint16 name_index = JCF_readu2 (jcf);
698 index = handle_constant (jcf, name_index,
699 CONSTANT_Utf8);
700 annotation_write_short (index);
701 handle_element_value (jcf, level + 2);
705 /* Read an annotation count from JCF, and write the following
706 annotations to the reflection_data field of the outgoing class. */
708 static void
709 handle_annotations (JCF *jcf, int level)
711 uint16 num = JCF_readu2 (jcf);
712 annotation_write_short (num);
713 while (num--)
714 handle_annotation (jcf, level);
717 /* As handle_annotations(), but perform a sanity check that we write
718 the same number of bytes that we were expecting. */
720 static void
721 handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
722 long length)
724 long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
726 handle_annotations (jcf, 0);
728 gcc_assert (old_datasize + length
729 == TYPE_REFLECTION_DATASIZE (current_class));
732 /* gcj permutes its fields array after generating annotation_data, so
733 we have to fixup field indexes for fields that have moved. Given
734 ARG, a VEC_int, fixup the field indexes in the reflection_data of
735 the outgoing class. We use field_offsets to tell us where the
736 fixups must go. */
738 void
739 rewrite_reflection_indexes (void *arg)
741 bitmap_iterator bi;
742 unsigned int offset;
743 vec<int> *map = (vec<int> *) arg;
744 unsigned char *data = TYPE_REFLECTION_DATA (current_class);
746 if (map)
748 EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
750 uint16 index = annotation_read_short (data + offset);
751 annotation_rewrite_short
752 ((*map)[index], data + offset);
757 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
758 reflection_data of the outgoing class. */
760 static void
761 handle_member_annotations (int member_index, JCF *jcf,
762 const unsigned char *name ATTRIBUTE_UNUSED,
763 long len, jv_attr_type member_type)
765 int new_len = len + 1;
766 annotation_write_byte (member_type);
767 if (member_type != JV_CLASS_ATTR)
768 new_len += 2;
769 annotation_write_int (new_len);
770 annotation_write_byte (JV_ANNOTATIONS_KIND);
771 if (member_type == JV_FIELD_ATTR)
772 bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
773 if (member_type != JV_CLASS_ATTR)
774 annotation_write_short (member_index);
775 handle_annotation_attribute (member_index, jcf, len);
778 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
779 to the reflection_data of the outgoing class. */
781 static void
782 handle_parameter_annotations (int member_index, JCF *jcf,
783 const unsigned char *name ATTRIBUTE_UNUSED,
784 long len, jv_attr_type member_type)
786 int new_len = len + 1;
787 uint8 num;
788 annotation_write_byte (member_type);
789 if (member_type != JV_CLASS_ATTR)
790 new_len += 2;
791 annotation_write_int (new_len);
792 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
793 if (member_type != JV_CLASS_ATTR)
794 annotation_write_short (member_index);
795 num = JCF_readu (jcf);
796 annotation_write_byte (num);
797 while (num--)
798 handle_annotations (jcf, 0);
802 /* Read the AnnotationDefault data from JCF and write them to the
803 reflection_data of the outgoing class. */
805 static void
806 handle_default_annotation (int member_index, JCF *jcf,
807 const unsigned char *name ATTRIBUTE_UNUSED,
808 long len, jv_attr_type member_type)
810 int new_len = len + 1;
811 annotation_write_byte (member_type);
812 if (member_type != JV_CLASS_ATTR)
813 new_len += 2;
814 annotation_write_int (new_len);
815 annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
816 if (member_type != JV_CLASS_ATTR)
817 annotation_write_short (member_index);
818 handle_element_value (jcf, 0);
821 /* As above, for the EnclosingMethod attribute. */
823 static void
824 handle_enclosingmethod_attribute (int member_index, JCF *jcf,
825 const unsigned char *name ATTRIBUTE_UNUSED,
826 long len, jv_attr_type member_type)
828 int new_len = len + 1;
829 uint16 index;
830 annotation_write_byte (member_type);
831 if (member_type != JV_CLASS_ATTR)
832 new_len += 2;
833 annotation_write_int (new_len);
834 annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
835 if (member_type != JV_CLASS_ATTR)
836 annotation_write_short (member_index);
838 index = JCF_readu2 (jcf);
839 index = handle_constant (jcf, index, CONSTANT_Class);
840 annotation_write_short (index);
842 index = JCF_readu2 (jcf);
843 index = handle_constant (jcf, index, CONSTANT_NameAndType);
844 annotation_write_short (index);
847 /* As above, for the Signature attribute. */
849 static void
850 handle_signature_attribute (int member_index, JCF *jcf,
851 const unsigned char *name ATTRIBUTE_UNUSED,
852 long len, jv_attr_type member_type)
854 int new_len = len + 1;
855 uint16 index;
856 annotation_write_byte (member_type);
857 if (member_type != JV_CLASS_ATTR)
858 new_len += 2;
859 annotation_write_int (new_len);
860 annotation_write_byte (JV_SIGNATURE_KIND);
861 if (member_type != JV_CLASS_ATTR)
862 annotation_write_short (member_index);
864 index = JCF_readu2 (jcf);
865 index = handle_constant (jcf, index, CONSTANT_Utf8);
866 annotation_write_short (index);
871 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
873 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
874 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
875 output_class = current_class = give_name_to_class (jcf, THIS); \
876 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
878 #define HANDLE_CLASS_INTERFACE(INDEX) \
879 add_interface (current_class, get_class_constant (jcf, INDEX))
881 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
882 { int sig_index = SIGNATURE; \
883 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
884 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
885 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
886 if ((ACCESS_FLAGS) & ACC_FINAL) \
887 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
890 #define HANDLE_END_FIELDS() \
891 (current_field = NULL_TREE)
893 #define HANDLE_CONSTANTVALUE(INDEX) \
894 { tree constant; int index = INDEX; \
895 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
896 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
897 constant = build_utf8_ref (name); \
899 else \
900 constant = get_constant (jcf, index); \
901 set_constant_value (current_field, constant); }
903 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
904 (current_method = add_method (current_class, ACCESS_FLAGS, \
905 get_name_constant (jcf, NAME), \
906 get_name_constant (jcf, SIGNATURE)), \
907 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
908 DECL_LINENUMBERS_OFFSET (current_method) = 0)
910 #define HANDLE_END_METHODS() \
911 { current_method = NULL_TREE; }
913 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
914 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
915 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
916 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
917 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
919 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
920 { int n = (COUNT); \
921 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
922 JCF_SKIP (jcf, n * 10); }
924 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
925 { int n = (COUNT); \
926 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
927 JCF_SKIP (jcf, n * 4); }
929 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
931 int n = COUNT; \
932 vec<tree, va_gc> *v; \
933 vec_alloc (v, n); \
934 gcc_assert (!DECL_FUNCTION_THROWS (current_method)); \
935 while (--n >= 0) \
937 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
938 v->quick_push (thrown_class); \
940 DECL_FUNCTION_THROWS (current_method) = v; \
943 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
945 /* Link seen inner classes to their outer context and register the
946 inner class to its outer context. They will be later loaded. */
947 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
948 handle_innerclass_attribute (COUNT, jcf, attribute_length)
950 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
952 /* Irrelevant decls should have been nullified by the END macros. \
953 DECL_ARTIFICIAL on fields is used for something else (See \
954 PUSH_FIELD in java-tree.h) */ \
955 if (current_method) \
956 DECL_ARTIFICIAL (current_method) = 1; \
957 else if (current_field) \
958 FIELD_SYNTHETIC (current_field) = 1; \
959 else \
960 TYPE_SYNTHETIC (current_class) = 1; \
963 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
965 if (current_class == object_type_node) \
966 jcf->right_zip = 1; \
969 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
971 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
974 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
976 JCF_SKIP(jcf, attribute_length); \
979 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
981 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
984 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
986 JCF_SKIP(jcf, attribute_length); \
989 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
991 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
994 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
996 handle_enclosingmethod_attribute (index, jcf, name_data, \
997 attribute_length, attr_type); \
1000 #define HANDLE_SIGNATURE_ATTRIBUTE() \
1002 handle_signature_attribute (index, jcf, name_data, \
1003 attribute_length, attr_type); \
1006 #include "jcf-reader.c"
1008 tree
1009 parse_signature (JCF *jcf, int sig_index)
1011 gcc_assert (sig_index > 0
1012 && sig_index < JPOOL_SIZE (jcf)
1013 && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1015 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1016 JPOOL_UTF_LENGTH (jcf, sig_index));
1019 tree
1020 get_constant (JCF *jcf, int index)
1022 tree value;
1023 int tag;
1024 if (index <= 0 || index >= JPOOL_SIZE(jcf))
1025 goto bad;
1026 tag = JPOOL_TAG (jcf, index);
1027 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
1028 return jcf->cpool.data[index].t;
1029 switch (tag)
1031 case CONSTANT_Integer:
1033 jint num = JPOOL_INT(jcf, index);
1034 value = build_int_cst (int_type_node, num);
1035 break;
1037 case CONSTANT_Long:
1039 unsigned HOST_WIDE_INT num;
1041 num = JPOOL_UINT (jcf, index);
1042 wide_int val = wi::lshift (wide_int::from (num, 64, SIGNED), 32);
1043 num = JPOOL_UINT (jcf, index + 1);
1044 val |= num;
1046 value = wide_int_to_tree (long_type_node, val);
1047 break;
1050 case CONSTANT_Float:
1052 jint num = JPOOL_INT(jcf, index);
1053 long buf = num;
1054 REAL_VALUE_TYPE d;
1056 real_from_target (&d, &buf, &ieee_single_format);
1057 value = build_real (float_type_node, d);
1058 break;
1061 case CONSTANT_Double:
1063 long buf[2], lo, hi;
1064 REAL_VALUE_TYPE d;
1066 hi = JPOOL_UINT (jcf, index);
1067 lo = JPOOL_UINT (jcf, index+1);
1069 if (targetm.float_words_big_endian ())
1070 buf[0] = hi, buf[1] = lo;
1071 else
1072 buf[0] = lo, buf[1] = hi;
1074 real_from_target (&d, buf, &ieee_double_format);
1075 value = build_real (double_type_node, d);
1076 break;
1079 case CONSTANT_String:
1081 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
1082 const char *utf8_ptr = IDENTIFIER_POINTER (name);
1083 int utf8_len = IDENTIFIER_LENGTH (name);
1084 const unsigned char *utf8;
1085 int i;
1087 /* Check for a malformed Utf8 string. */
1088 utf8 = (const unsigned char *) utf8_ptr;
1089 i = utf8_len;
1090 while (i > 0)
1092 int char_len = UT8_CHAR_LENGTH (*utf8);
1093 if (char_len < 0 || char_len > 3 || char_len > i)
1094 fatal_error (input_location, "bad string constant");
1096 utf8 += char_len;
1097 i -= char_len;
1100 /* Allocate a new string value. */
1101 value = build_string (utf8_len, utf8_ptr);
1102 TREE_TYPE (value) = build_pointer_type (string_type_node);
1104 break;
1105 default:
1106 goto bad;
1108 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
1109 jcf->cpool.data[index].t = value;
1110 return value;
1111 bad:
1112 fatal_error (input_location, "bad value constant type %d, index %d",
1113 JPOOL_TAG (jcf, index), index);
1116 tree
1117 get_name_constant (JCF *jcf, int index)
1119 tree name = get_constant (jcf, index);
1120 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1121 return name;
1124 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1125 a non anonymous entry) is found, We augment the inner class list of
1126 the outer context with the newly resolved innerclass. */
1128 static void
1129 handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
1131 int c = count;
1133 annotation_write_byte (JV_CLASS_ATTR);
1134 annotation_write_int (attribute_length+1);
1135 annotation_write_byte (JV_INNER_CLASSES_KIND);
1136 annotation_write_short (count);
1138 while (c--)
1140 /* Read inner_class_info_index. This may be 0 */
1141 int icii = JCF_readu2 (jcf);
1142 /* Read outer_class_info_index. If the innerclasses attribute
1143 entry isn't a member (like an inner class) the value is 0. */
1144 int ocii = JCF_readu2 (jcf);
1145 /* Read inner_name_index. If the class we're dealing with is
1146 an anonymous class, it must be 0. */
1147 int ini = JCF_readu2 (jcf);
1148 /* Read the access flag. */
1149 int acc = JCF_readu2 (jcf);
1151 annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1152 annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1153 annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1154 annotation_write_short (acc);
1156 /* If icii is 0, don't try to read the class. */
1157 if (icii >= 0)
1159 tree klass = get_class_constant (jcf, icii);
1160 tree decl = TYPE_NAME (klass);
1161 /* Skip reading further if ocii is null */
1162 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1164 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1165 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
1166 set_class_decl_access_flags (acc, decl);
1167 DECL_CONTEXT (decl) = outer;
1168 DECL_INNER_CLASS_LIST (outer) =
1169 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1170 CLASS_COMPLETE_P (decl) = 1;
1176 static tree
1177 give_name_to_class (JCF *jcf, int i)
1179 gcc_assert (i > 0
1180 && i < JPOOL_SIZE (jcf)
1181 && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1184 tree package_name = NULL_TREE, tmp;
1185 tree this_class;
1186 int j = JPOOL_USHORT1 (jcf, i);
1187 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1188 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
1189 JPOOL_UTF_LENGTH (jcf, j));
1190 this_class = lookup_class (class_name);
1192 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
1193 const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
1194 linemap_add (line_table, LC_ENTER, false, sfname, 0);
1195 input_location = linemap_line_start (line_table, 0, 1);
1196 file_start_location = input_location;
1197 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1198 if (main_input_filename == NULL && jcf == main_jcf)
1199 main_input_filename = sfname;
1202 jcf->cpool.data[i].t = this_class;
1203 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1204 split_qualified_name (&package_name, &tmp,
1205 DECL_NAME (TYPE_NAME (this_class)));
1206 TYPE_PACKAGE (this_class) = package_name;
1207 return this_class;
1211 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1213 tree
1214 get_class_constant (JCF *jcf, int i)
1216 tree type;
1217 gcc_assert (i > 0
1218 && i < JPOOL_SIZE (jcf)
1219 && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
1221 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1223 int name_index = JPOOL_USHORT1 (jcf, i);
1224 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1225 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
1226 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
1228 if (name[0] == '[') /* Handle array "classes". */
1229 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
1230 else
1232 tree cname = unmangle_classname (name, nlength);
1233 type = lookup_class (cname);
1235 jcf->cpool.data[i].t = type;
1236 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1238 else
1239 type = jcf->cpool.data[i].t;
1240 return type;
1243 /* Read a class with the fully qualified-name NAME.
1244 Return 1 iff we read the requested file.
1245 (It is still possible we failed if the file did not
1246 define the class it is supposed to.) */
1249 read_class (tree name)
1251 JCF this_jcf, *jcf;
1252 tree icv, klass = NULL_TREE;
1253 tree save_current_class = current_class;
1254 tree save_output_class = output_class;
1255 location_t save_location = input_location;
1256 JCF *save_current_jcf = current_jcf;
1258 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
1260 klass = TREE_TYPE (icv);
1261 jcf = TYPE_JCF (klass);
1263 else
1264 jcf = NULL;
1266 if (jcf == NULL)
1268 const char* path_name;
1269 this_jcf.zipd = NULL;
1270 jcf = &this_jcf;
1272 path_name = find_class (IDENTIFIER_POINTER (name),
1273 IDENTIFIER_LENGTH (name),
1274 &this_jcf);
1275 if (path_name == 0)
1276 return 0;
1277 else
1278 free(CONST_CAST (char *, path_name));
1281 current_jcf = jcf;
1283 if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
1285 output_class = current_class = klass;
1286 if (JCF_SEEN_IN_ZIP (current_jcf))
1287 read_zip_member(current_jcf,
1288 current_jcf->zipd, current_jcf->zipd->zipf);
1289 jcf_parse (current_jcf);
1290 /* Parsing might change the class, in which case we have to
1291 put it back where we found it. */
1292 if (current_class != klass && icv != NULL_TREE)
1293 TREE_TYPE (icv) = current_class;
1294 klass = current_class;
1296 layout_class (klass);
1297 load_inner_classes (klass);
1299 output_class = save_output_class;
1300 current_class = save_current_class;
1301 input_location = save_location;
1302 current_jcf = save_current_jcf;
1303 return 1;
1306 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1307 called from the parser, otherwise it's a RECORD_TYPE node. If
1308 VERBOSE is 1, print error message on failure to load a class. */
1309 void
1310 load_class (tree class_or_name, int verbose)
1312 tree name, saved;
1313 int class_loaded = 0;
1314 tree class_decl = NULL_TREE;
1315 bool is_compiled_class = false;
1317 /* We've already failed, don't try again. */
1318 if (TREE_CODE (class_or_name) == RECORD_TYPE
1319 && TYPE_DUMMY (class_or_name))
1320 return;
1322 /* class_or_name can be the name of the class we want to load */
1323 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1324 name = class_or_name;
1325 /* In some cases, it's a dependency that we process earlier that
1326 we though */
1327 else if (TREE_CODE (class_or_name) == TREE_LIST)
1328 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1329 /* Or it's a type in the making */
1330 else
1331 name = DECL_NAME (TYPE_NAME (class_or_name));
1333 class_decl = IDENTIFIER_CLASS_VALUE (name);
1334 if (class_decl != NULL_TREE)
1336 tree type = TREE_TYPE (class_decl);
1337 is_compiled_class
1338 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1339 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1342 saved = name;
1344 /* If flag_verify_invocations is unset, we don't try to load a class
1345 unless we're looking for Object (which is fixed by the ABI) or
1346 it's a class that we're going to compile. */
1347 if (flag_verify_invocations
1348 || class_or_name == object_type_node
1349 || is_compiled_class
1350 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1352 while (1)
1354 const char *separator;
1356 /* We've already loaded it. */
1357 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1359 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1360 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1361 break;
1364 if (read_class (name))
1365 break;
1367 /* We failed loading name. Now consider that we might be looking
1368 for an inner class. */
1369 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1370 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
1371 name = get_identifier_with_length (IDENTIFIER_POINTER (name),
1372 (separator
1373 - IDENTIFIER_POINTER (name)));
1374 /* Otherwise, we failed, we bail. */
1375 else
1376 break;
1380 /* have we found the class we're looking for? */
1381 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1382 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1383 class_loaded = type && CLASS_PARSED_P (type);
1387 if (!class_loaded)
1389 if (flag_verify_invocations || ! flag_indirect_dispatch)
1391 if (verbose)
1392 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1394 else if (verbose)
1396 /* This is just a diagnostic during testing, not a real problem. */
1397 if (!quiet_flag)
1398 warning (0, "cannot find file for class %s",
1399 IDENTIFIER_POINTER (saved));
1401 /* Fake it. */
1402 if (TREE_CODE (class_or_name) == RECORD_TYPE)
1404 set_super_info (0, class_or_name, object_type_node, 0);
1405 TYPE_DUMMY (class_or_name) = 1;
1406 /* We won't be able to output any debug info for this class. */
1407 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1413 /* Parse the .class file JCF. */
1415 static void
1416 jcf_parse (JCF* jcf)
1418 int i, code;
1420 bitmap_clear (field_offsets);
1422 if (jcf_parse_preamble (jcf) != 0)
1423 fatal_error (input_location, "not a valid Java .class file");
1424 code = jcf_parse_constant_pool (jcf);
1425 if (code != 0)
1426 fatal_error (input_location, "error while parsing constant pool");
1427 code = verify_constant_pool (jcf);
1428 if (code > 0)
1429 fatal_error (input_location, "error in constant pool entry #%d\n", code);
1431 jcf_parse_class (jcf);
1432 if (main_class == NULL_TREE)
1433 main_class = current_class;
1434 if (! quiet_flag && TYPE_NAME (current_class))
1435 fprintf (stderr, " %s %s",
1436 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
1437 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
1438 if (CLASS_PARSED_P (current_class))
1440 /* FIXME - where was first time */
1441 fatal_error (input_location,
1442 "reading class %s for the second time from %s",
1443 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1444 jcf->filename);
1446 CLASS_PARSED_P (current_class) = 1;
1448 for (i = 1; i < JPOOL_SIZE(jcf); i++)
1450 switch (JPOOL_TAG (jcf, i))
1452 case CONSTANT_Class:
1453 get_class_constant (jcf, i);
1454 break;
1458 code = jcf_parse_fields (jcf);
1459 if (code != 0)
1460 fatal_error (input_location, "error while parsing fields");
1461 code = jcf_parse_methods (jcf);
1462 if (code != 0)
1463 fatal_error (input_location, "error while parsing methods");
1464 code = jcf_parse_final_attributes (jcf);
1465 if (code != 0)
1466 fatal_error (input_location, "error while parsing final attributes");
1468 if (TYPE_REFLECTION_DATA (current_class))
1469 annotation_write_byte (JV_DONE_ATTR);
1471 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1473 /* The fields of class_type_node are already in correct order. */
1474 if (current_class != class_type_node && current_class != object_type_node)
1475 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1477 if (current_class == object_type_node)
1478 layout_class_methods (object_type_node);
1479 else
1480 vec_safe_push (all_class_list, TYPE_NAME (current_class));
1483 /* If we came across inner classes, load them now. */
1484 static void
1485 load_inner_classes (tree cur_class)
1487 tree current;
1488 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
1489 current = TREE_CHAIN (current))
1491 tree name = DECL_NAME (TREE_PURPOSE (current));
1492 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1493 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1494 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
1495 load_class (name, 1);
1499 static void
1500 duplicate_class_warning (const char *filename)
1502 location_t warn_loc;
1503 linemap_add (line_table, LC_RENAME, 0, filename, 0);
1504 warn_loc = linemap_line_start (line_table, 0, 1);
1505 warning_at (warn_loc, 0, "duplicate class will only be compiled once");
1508 static void
1509 java_layout_seen_class_methods (void)
1511 unsigned start = 0;
1512 unsigned end = vec_safe_length (all_class_list);
1514 while (1)
1516 unsigned ix;
1517 unsigned new_length;
1519 for (ix = start; ix != end; ix++)
1521 tree decl = (*all_class_list)[ix];
1522 tree cls = TREE_TYPE (decl);
1524 input_location = DECL_SOURCE_LOCATION (decl);
1526 if (! CLASS_LOADED_P (cls))
1527 load_class (cls, 0);
1529 layout_class_methods (cls);
1532 /* Note that new classes might have been added while laying out
1533 methods, changing the value of all_class_list. */
1534 new_length = vec_safe_length (all_class_list);
1535 if (end != new_length)
1537 start = end;
1538 end = new_length;
1540 else
1541 break;
1545 static void
1546 parse_class_file (void)
1548 tree method;
1549 location_t save_location = input_location;
1551 java_layout_seen_class_methods ();
1553 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1555 /* Re-enter the current file. */
1556 expanded_location loc = expand_location (input_location);
1557 linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1559 file_start_location = input_location;
1560 (*debug_hooks->start_source_file) (LOCATION_LINE (input_location),
1561 LOCATION_FILE (input_location));
1563 java_mark_class_local (current_class);
1565 gen_indirect_dispatch_tables (current_class);
1567 for (method = TYPE_METHODS (current_class);
1568 method != NULL_TREE; method = DECL_CHAIN (method))
1570 JCF *jcf = current_jcf;
1572 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
1573 continue;
1575 if (METHOD_NATIVE (method))
1577 tree arg;
1578 int decl_max_locals;
1580 if (! flag_jni)
1581 continue;
1582 /* We need to compute the DECL_MAX_LOCALS. We need to take
1583 the wide types into account too. */
1584 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
1585 arg != end_params_node;
1586 arg = TREE_CHAIN (arg), decl_max_locals += 1)
1588 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1589 decl_max_locals += 1;
1591 DECL_MAX_LOCALS (method) = decl_max_locals;
1592 start_java_method (method);
1593 give_name_to_locals (jcf);
1594 *get_stmts () = build_jni_stub (method);
1595 end_java_method ();
1596 continue;
1599 if (DECL_CODE_OFFSET (method) == 0)
1601 current_function_decl = method;
1602 error ("missing Code attribute");
1603 continue;
1606 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1607 if (DECL_LINENUMBERS_OFFSET (method))
1609 int i;
1610 int min_line = 0;
1611 unsigned char *ptr;
1612 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1613 linenumber_count = i = JCF_readu2 (jcf);
1614 linenumber_table = ptr = jcf->read_ptr;
1616 for (ptr += 2; --i >= 0; ptr += 4)
1618 int line = GET_u2 (ptr);
1619 /* Set initial line of input_location to smallest
1620 * linenumber.
1621 * Needs to be set before init_function_start. */
1622 if (min_line == 0 || line < min_line)
1623 min_line = line;
1625 if (min_line != 0)
1626 input_location = linemap_line_start (line_table, min_line, 1);
1628 else
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)
1649 / 1000)
1650 + 1)
1651 * 1000);
1652 highest_label_pc_this_method = -1;
1655 /* Convert bytecode to trees. */
1656 expand_byte_code (jcf, method);
1658 end_java_method ();
1661 finish_class ();
1663 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
1664 input_location = save_location;
1667 static vec<tree, va_gc> *predefined_filenames;
1669 void
1670 add_predefined_file (tree name)
1672 vec_safe_push (predefined_filenames, name);
1676 predefined_filename_p (tree node)
1678 unsigned ix;
1679 tree f;
1681 FOR_EACH_VEC_SAFE_ELT (predefined_filenames, ix, f)
1682 if (f == node)
1683 return 1;
1685 return 0;
1688 /* Generate a function that does all static initialization for this
1689 translation unit. */
1691 static void
1692 java_emit_static_constructor (void)
1694 tree body = NULL;
1696 emit_register_classes (&body);
1697 write_resource_constructor (&body);
1699 if (body)
1701 tree name = get_identifier ("_Jv_global_static_constructor");
1703 tree decl
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_node::finalize_function (decl, false);
1731 void
1732 java_parse_file (void)
1734 int filename_count = 0;
1735 location_t save_location = input_location;
1736 char *file_list = NULL, *list, *next;
1737 tree node;
1738 FILE *finput = NULL;
1739 int in_quotes = 0;
1740 unsigned ix;
1742 bitmap_obstack_initialize (&bit_obstack);
1743 field_offsets = BITMAP_ALLOC (&bit_obstack);
1745 if (flag_filelist_file)
1747 int avail = 2000;
1748 finput = fopen (main_input_filename, "r");
1749 if (finput == NULL)
1750 fatal_error (input_location,
1751 "can%'t open %s: %m", LOCATION_FILE (input_location));
1752 list = XNEWVEC (char, avail);
1753 next = list;
1754 for (;;)
1756 int count;
1757 if (avail < 500)
1759 count = next - list;
1760 avail = 2 * (count + avail);
1761 list = XRESIZEVEC (char, list, avail);
1762 next = list + count;
1763 avail = avail - count;
1765 /* Subtract one to guarantee space for final '\0'. */
1766 count = fread (next, 1, avail - 1, finput);
1767 if (count == 0)
1769 if (! feof (finput))
1770 fatal_error (input_location, "error closing %s: %m",
1771 LOCATION_FILE (input_location));
1772 *next = '\0';
1773 break;
1775 avail -= count;
1776 next += count;
1778 fclose (finput);
1779 finput = NULL;
1780 file_list = list;
1782 else
1783 list = CONST_CAST (char *, main_input_filename);
1785 while (list)
1787 for (next = list; ; )
1789 char ch = *next;
1790 if (flag_filelist_file && ! in_quotes
1791 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1792 || ch == '&') /* FIXME */)
1794 if (next == list)
1796 next++;
1797 list = next;
1798 continue;
1800 else
1802 *next++ = '\0';
1803 break;
1806 if (flag_filelist_file && ch == '"')
1808 in_quotes = ! in_quotes;
1809 *next++ = '\0';
1810 if (in_quotes)
1811 list = next;
1812 else
1813 break;
1815 if (ch == '\0')
1817 next = NULL;
1818 break;
1820 next++;
1823 /* Exclude .java files. */
1824 if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1826 /* Nothing. */
1828 else if (list[0])
1830 node = get_identifier (list);
1832 filename_count++;
1834 /* Exclude file that we see twice on the command line. */
1836 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1837 duplicate_class_warning (IDENTIFIER_POINTER (node));
1838 else
1840 build_translation_unit_decl (node);
1841 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1844 list = next;
1847 free (file_list);
1849 if (filename_count == 0)
1850 warning (0, "no input file specified");
1852 if (resource_name)
1854 const char *resource_filename;
1856 /* Only one resource file may be compiled at a time. */
1857 gcc_assert (all_translation_units->length () == 1);
1859 resource_filename
1860 = IDENTIFIER_POINTER (DECL_NAME ((*all_translation_units)[0]));
1861 compile_resource_file (resource_name, resource_filename);
1863 goto finish;
1866 current_jcf = main_jcf;
1867 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1869 unsigned char magic_string[4];
1870 char *real_path;
1871 uint32 magic = 0;
1872 tree name = DECL_NAME (node);
1873 tree real_file;
1874 const char *filename = IDENTIFIER_POINTER (name);
1876 /* Skip already parsed files */
1877 real_path = lrealpath (filename);
1878 real_file = get_identifier (real_path);
1879 free (real_path);
1880 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1881 continue;
1883 /* Close previous descriptor, if any */
1884 if (finput && fclose (finput))
1885 fatal_error (input_location,
1886 "can%'t close input file %s: %m", main_input_filename);
1888 finput = fopen (filename, "rb");
1889 if (finput == NULL)
1890 fatal_error (input_location, "can%'t open %s: %m", filename);
1892 #ifdef IO_BUFFER_SIZE
1893 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1894 _IOFBF, IO_BUFFER_SIZE);
1895 #endif
1897 /* Figure what kind of file we're dealing with */
1898 if (fread (magic_string, 1, 4, finput) == 4)
1900 fseek (finput, 0L, SEEK_SET);
1901 magic = GET_u4 (magic_string);
1903 if (magic == 0xcafebabe)
1905 CLASS_FILE_P (node) = 1;
1906 current_jcf = ggc_cleared_alloc<JCF> ();
1907 current_jcf->read_state = finput;
1908 current_jcf->filbuf = jcf_filbuf_from_stdio;
1909 jcf_parse (current_jcf);
1910 DECL_SOURCE_LOCATION (node) = file_start_location;
1911 TYPE_JCF (current_class) = current_jcf;
1912 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1914 /* We've already compiled this class. */
1915 duplicate_class_warning (filename);
1916 continue;
1918 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1919 TREE_TYPE (node) = current_class;
1921 else if (magic == (JCF_u4)ZIPMAGIC)
1923 main_jcf = ggc_cleared_alloc<JCF> ();
1924 main_jcf->read_state = finput;
1925 main_jcf->filbuf = jcf_filbuf_from_stdio;
1926 linemap_add (line_table, LC_ENTER, false, filename, 0);
1927 input_location = linemap_line_start (line_table, 0, 1);
1928 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1929 fatal_error (input_location, "bad zip/jar file %s", filename);
1930 localToFile = SeenZipFiles;
1931 /* Register all the classes defined there. */
1932 process_zip_dir ((FILE *) main_jcf->read_state);
1933 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1934 parse_zip_file_entries ();
1936 else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1938 /* Ignore an empty input jar. */
1940 else
1942 gcc_unreachable ();
1943 #if 0
1944 java_push_parser_context ();
1945 java_parser_context_save_global ();
1947 parse_source_file_1 (real_file, filename, finput);
1948 java_parser_context_restore_global ();
1949 java_pop_parser_context (1);
1950 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1951 #endif
1955 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1957 input_location = DECL_SOURCE_LOCATION (node);
1958 if (CLASS_FILE_P (node))
1960 /* FIXME: These two flags really should be independent. We
1961 should be able to compile fully binary compatible, but
1962 with flag_verify_invocations on. */
1963 flag_verify_invocations = ! flag_indirect_dispatch;
1964 output_class = current_class = TREE_TYPE (node);
1966 current_jcf = TYPE_JCF (current_class);
1967 layout_class (current_class);
1968 load_inner_classes (current_class);
1969 parse_class_file ();
1970 JCF_FINISH (current_jcf);
1973 input_location = save_location;
1975 bitmap_obstack_release (&bit_obstack);
1977 finish:
1978 /* Arrange for any necessary initialization to happen. */
1979 java_emit_static_constructor ();
1980 gcc_assert (global_bindings_p ());
1982 /* Do final processing on globals. */
1983 global_decl_processing ();
1987 /* Return the name of the class corresponding to the name of the file
1988 in this zip entry. The result is newly allocated using ALLOC. */
1989 static char *
1990 compute_class_name (struct ZipDirectory *zdir)
1992 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1993 char *class_name;
1994 int i;
1995 int filename_length = zdir->filename_length;
1997 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
1999 class_name_in_zip_dir += 2;
2000 filename_length -= 2;
2003 filename_length -= strlen (".class");
2004 class_name = XNEWVEC (char, filename_length + 1);
2005 memcpy (class_name, class_name_in_zip_dir, filename_length);
2006 class_name [filename_length] = '\0';
2008 for (i = 0; i < filename_length; i++)
2009 if (class_name[i] == '/')
2010 class_name[i] = '.';
2012 return class_name;
2015 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2016 if it is a property file of some sort. */
2017 static int
2018 classify_zip_file (struct ZipDirectory *zdir)
2020 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2022 if (zdir->filename_length > 6
2023 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2024 ".class", 6))
2025 return 1;
2027 /* For now we drop the manifest, but not other information. */
2028 if (zdir->filename_length == 20
2029 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
2030 return 0;
2032 /* Drop directory entries. */
2033 if (zdir->filename_length > 0
2034 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2035 return 0;
2037 return 2;
2040 /* Process all class entries found in the zip file. */
2041 static void
2042 parse_zip_file_entries (void)
2044 struct ZipDirectory *zdir;
2045 int i;
2047 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2048 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2050 tree klass;
2052 switch (classify_zip_file (zdir))
2054 case 0:
2055 continue;
2057 case 1:
2059 char *class_name = compute_class_name (zdir);
2060 int previous_alias_set = -1;
2061 klass = lookup_class (get_identifier (class_name));
2062 FREE (class_name);
2063 current_jcf = TYPE_JCF (klass);
2064 output_class = current_class = klass;
2066 /* This is a dummy class, and now we're compiling it for
2067 real. */
2068 gcc_assert (! TYPE_DUMMY (klass));
2070 /* This is for a corner case where we have a superclass
2071 but no superclass fields.
2073 This can happen if we earlier failed to lay out this
2074 class because its superclass was still in the process
2075 of being laid out; this occurs when we have recursive
2076 class dependencies via inner classes. We must record
2077 the previous alias set and restore it after laying out
2078 the class.
2080 FIXME: this really is a kludge. We should figure out a
2081 way to lay out the class properly before this
2082 happens. */
2083 if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2084 && integer_zerop (TYPE_SIZE (klass)))
2086 TYPE_SIZE (klass) = NULL_TREE;
2087 previous_alias_set = TYPE_ALIAS_SET (klass);
2088 TYPE_ALIAS_SET (klass) = -1;
2091 if (! CLASS_LOADED_P (klass))
2093 if (! CLASS_PARSED_P (klass))
2095 read_zip_member (current_jcf, zdir, localToFile);
2096 jcf_parse (current_jcf);
2098 layout_class (current_class);
2099 load_inner_classes (current_class);
2102 if (previous_alias_set != -1)
2103 TYPE_ALIAS_SET (klass) = previous_alias_set;
2105 if (TYPE_SIZE (current_class) != error_mark_node)
2107 parse_class_file ();
2108 free (current_jcf->buffer); /* No longer necessary */
2109 /* Note: there is a way to free this buffer right after a
2110 class seen in a zip file has been parsed. The idea is the
2111 set its jcf in such a way that buffer will be reallocated
2112 the time the code for the class will be generated. FIXME. */
2115 break;
2117 case 2:
2119 char *file_name, *class_name_in_zip_dir, *buffer;
2120 JCF *jcf;
2121 file_name = XNEWVEC (char, zdir->filename_length + 1);
2122 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2123 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2124 file_name[zdir->filename_length] = '\0';
2125 jcf = XNEW (JCF);
2126 JCF_ZERO (jcf);
2127 jcf->read_state = finput;
2128 jcf->filbuf = jcf_filbuf_from_stdio;
2129 jcf->classname = NULL;
2130 jcf->filename = file_name;
2131 jcf->zipd = zdir;
2133 if (read_zip_member (jcf, zdir, localToFile) < 0)
2134 fatal_error (input_location,
2135 "error while reading %s from zip file", file_name);
2137 buffer = XNEWVEC (char, zdir->filename_length + 1 +
2138 (jcf->buffer_end - jcf->buffer));
2139 strcpy (buffer, file_name);
2140 /* This is not a typo: we overwrite the trailing \0 of the
2141 file name; this is just how the data is laid out. */
2142 memcpy (buffer + zdir->filename_length,
2143 jcf->buffer, jcf->buffer_end - jcf->buffer);
2145 compile_resource_data (file_name, buffer,
2146 jcf->buffer_end - jcf->buffer);
2147 JCF_FINISH (jcf);
2148 free (jcf);
2149 free (buffer);
2151 break;
2153 default:
2154 gcc_unreachable ();
2159 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
2160 jcf up for further processing and link it to the created class. */
2162 static void
2163 process_zip_dir (FILE *finput)
2165 int i;
2166 ZipDirectory *zdir;
2168 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2169 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2171 char *class_name, *file_name, *class_name_in_zip_dir;
2172 tree klass;
2173 JCF *jcf;
2175 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2177 /* Here we skip non-class files; we handle them later. */
2178 if (classify_zip_file (zdir) != 1)
2179 continue;
2181 class_name = compute_class_name (zdir);
2182 file_name = XNEWVEC (char, zdir->filename_length+1);
2183 jcf = ggc_cleared_alloc<JCF> ();
2185 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2186 file_name [zdir->filename_length] = '\0';
2188 klass = lookup_class (get_identifier (class_name));
2190 if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2192 /* We've already compiled this class. */
2193 duplicate_class_warning (file_name);
2194 continue;
2196 /* This function is only called when processing a zip file seen
2197 on the command line. */
2198 CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
2200 jcf->read_state = finput;
2201 jcf->filbuf = jcf_filbuf_from_stdio;
2202 jcf->classname = class_name;
2203 jcf->filename = file_name;
2204 jcf->zipd = zdir;
2206 TYPE_JCF (klass) = jcf;
2210 #include "gt-java-jcf-parse.h"
2211 #include "gtype-java.h"