2015-08-24 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / java / jcf-parse.c
blobbb6e743e2b47c35c4bb67e5ea0c4f02144219d62
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Per Bothner <bothner@cygnus.com> */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "alias.h"
30 #include "tree.h"
31 #include "options.h"
32 #include "stringpool.h"
33 #include "obstack.h"
34 #include "flags.h"
35 #include "java-except.h"
36 #include "javaop.h"
37 #include "java-tree.h"
38 #include "diagnostic-core.h"
39 #include "parse.h"
40 #include "debug.h"
41 #include "tm.h"
42 #include "hard-reg-set.h"
43 #include "function.h"
44 #include "cgraph.h"
45 #include "bitmap.h"
46 #include "target.h"
47 #include "toplev.h"
49 #ifdef HAVE_LOCALE_H
50 #include <locale.h>
51 #endif
53 #ifdef HAVE_LANGINFO_CODESET
54 #include <langinfo.h>
55 #endif
57 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
58 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
59 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
60 #define JPOOL_UTF_DATA(JCF, INDEX) \
61 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
62 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
63 do { \
64 unsigned char save; unsigned char *text; \
65 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
66 text = (JCF)->read_ptr; \
67 save = text[LENGTH]; \
68 text[LENGTH] = 0; \
69 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
70 text[LENGTH] = save; \
71 JCF_SKIP (JCF, LENGTH); } while (0)
73 #include "jcf.h"
75 extern struct obstack temporary_obstack;
77 static GTY(()) tree parse_roots[2];
79 /* The FIELD_DECL for the current field. */
80 #define current_field parse_roots[0]
82 /* The METHOD_DECL for the current method. */
83 #define current_method parse_roots[1]
85 /* Line 0 in current file, if compiling from bytecode. */
86 static location_t file_start_location;
88 /* The Java archive that provides main_class; the main input file. */
89 static GTY(()) struct JCF * main_jcf;
91 /* A list of all the class DECLs seen so far. */
92 static GTY(()) vec<tree, va_gc> *all_class_list;
94 /* The number of source files passed to us by -fsource-filename and an
95 array of pointers to each name. Used by find_sourcefile(). */
96 static int num_files = 0;
97 static char **filenames;
99 static struct ZipFile *localToFile;
101 /* A map of byte offsets in the reflection data that are fields which
102 need renumbering. */
103 bitmap field_offsets;
104 bitmap_obstack bit_obstack;
106 /* Declarations of some functions used here. */
107 static void handle_innerclass_attribute (int count, JCF *, int len);
108 static tree give_name_to_class (JCF *jcf, int index);
109 static char *compute_class_name (struct ZipDirectory *zdir);
110 static int classify_zip_file (struct ZipDirectory *zdir);
111 static void parse_zip_file_entries (void);
112 static void process_zip_dir (FILE *);
113 static void parse_class_file (void);
114 static void handle_deprecated (void);
115 static void set_source_filename (JCF *, int);
116 static void jcf_parse (struct JCF*);
117 static void load_inner_classes (tree);
118 static void handle_annotation (JCF *jcf, int level);
119 static void java_layout_seen_class_methods (void);
121 /* Handle "Deprecated" attribute. */
122 static void
123 handle_deprecated (void)
125 if (current_field != NULL_TREE)
126 FIELD_DEPRECATED (current_field) = 1;
127 else if (current_method != NULL_TREE)
128 METHOD_DEPRECATED (current_method) = 1;
129 else if (current_class != NULL_TREE)
130 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
131 else
133 /* Shouldn't happen. */
134 gcc_unreachable ();
140 /* Reverse a string. */
141 static char *
142 reverse (const char *s)
144 if (s == NULL)
145 return NULL;
146 else
148 int len = strlen (s);
149 char *d = XNEWVAR (char, len + 1);
150 const char *sp;
151 char *dp;
153 d[len] = 0;
154 for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
155 *dp = *sp;
157 return d;
161 /* Compare two strings for qsort(). */
162 static int
163 cmpstringp (const void *p1, const void *p2)
165 /* The arguments to this function are "pointers to
166 pointers to char", but strcmp() arguments are "pointers
167 to char", hence the following cast plus dereference */
169 return strcmp(*(const char *const*) p1, *(const char *const*) p2);
172 /* Create an array of strings, one for each source file that we've
173 seen. fsource_filename can either be the name of a single .java
174 file or a file that contains a list of filenames separated by
175 newlines. */
176 void
177 java_read_sourcefilenames (const char *fsource_filename)
179 if (fsource_filename
180 && filenames == 0
181 && strlen (fsource_filename) > strlen (".java")
182 && filename_cmp ((fsource_filename
183 + strlen (fsource_filename)
184 - strlen (".java")),
185 ".java") != 0)
187 /* fsource_filename isn't a .java file but a list of filenames
188 separated by newlines */
189 FILE *finput = fopen (fsource_filename, "r");
190 int len = 0;
191 int longest_line = 0;
193 gcc_assert (finput);
195 /* Find out how many files there are, and how long the filenames are. */
196 while (! feof (finput))
198 int ch = getc (finput);
199 if (ch == '\n')
201 num_files++;
202 if (len > longest_line)
203 longest_line = len;
204 len = 0;
205 continue;
207 if (ch == EOF)
208 break;
209 len++;
212 rewind (finput);
214 /* Read the filenames. Put a pointer to each filename into the
215 array FILENAMES. */
217 char *linebuf = (char *) alloca (longest_line + 1);
218 int i = 0;
219 int charpos;
221 filenames = XNEWVEC (char *, num_files);
223 charpos = 0;
224 for (;;)
226 int ch = getc (finput);
227 if (ch == EOF)
228 break;
229 if (ch == '\n')
231 linebuf[charpos] = 0;
232 gcc_assert (i < num_files);
233 /* ??? Perhaps we should use lrealpath() here. Doing
234 so would tidy up things like /../ but the rest of
235 gcc seems to assume relative pathnames, not
236 absolute pathnames. */
237 /* realname = lrealpath (linebuf); */
238 filenames[i++] = reverse (linebuf);
239 charpos = 0;
240 continue;
242 gcc_assert (charpos < longest_line);
243 linebuf[charpos++] = ch;
246 if (num_files > 1)
247 qsort (filenames, num_files, sizeof (char *), cmpstringp);
249 fclose (finput);
251 else
253 filenames = XNEWVEC (char *, 1);
254 filenames[0] = reverse (fsource_filename);
255 num_files = 1;
259 /* Given a relative pathname such as foo/bar.java, attempt to find a
260 longer pathname with the same suffix.
262 This is a best guess heuristic; with some weird class hierarchies we
263 may fail to pick the correct source file. For example, if we have
264 the filenames foo/bar.java and also foo/foo/bar.java, we do not
265 have enough information to know which one is the right match for
266 foo/bar.java. */
268 static const char *
269 find_sourcefile (const char *name)
271 int i = 0, j = num_files-1;
272 char *found = NULL;
274 if (filenames)
276 char *revname = reverse (name);
280 int k = (i+j) / 2;
281 int cmp = strncmp (revname, filenames[k], strlen (revname));
282 if (cmp == 0)
284 /* OK, so we found one. But is it a unique match? */
285 if ((k > i
286 && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
287 || (k < j
288 && (strncmp (revname, filenames[k+1], strlen (revname))
289 == 0)))
291 else
292 found = filenames[k];
293 break;
295 if (cmp > 0)
296 i = k+1;
297 else
298 j = k-1;
300 while (i <= j);
302 free (revname);
305 if (found && strlen (found) > strlen (name))
306 return reverse (found);
307 else
308 return name;
313 /* Handle "SourceFile" attribute. */
315 static void
316 set_source_filename (JCF *jcf, int index)
318 tree sfname_id = get_name_constant (jcf, index);
319 const char *sfname = IDENTIFIER_POINTER (sfname_id);
320 const char *old_filename = LOCATION_FILE (input_location);
321 int new_len = IDENTIFIER_LENGTH (sfname_id);
322 if (old_filename != NULL)
324 int old_len = strlen (old_filename);
325 /* Use the filename from current input_location (derived from the
326 class name) if it has a directory prefix, but otherwise matches
327 sfname. */
328 if (old_len > new_len
329 && filename_cmp (sfname, old_filename + old_len - new_len) == 0
330 && (old_filename[old_len - new_len - 1] == '/'
331 || old_filename[old_len - new_len - 1] == '\\'))
332 return;
334 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
336 const char *class_name
337 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
338 const char *dot = strrchr (class_name, '.');
339 if (dot != NULL)
341 /* Length of prefix, not counting final dot. */
342 int i = dot - class_name;
343 /* Concatenate current package prefix with new sfname. */
344 char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
345 strcpy (buf + i + 1, sfname);
346 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
347 Note we start at the end with the final package dot. */
348 for (; i >= 0; i--)
350 char c = class_name[i];
351 if (c == '.')
352 c = DIR_SEPARATOR;
353 buf[i] = c;
355 sfname_id = get_identifier (buf);
356 free (buf);
357 sfname = IDENTIFIER_POINTER (sfname_id);
361 sfname = find_sourcefile (sfname);
362 LINEMAPS_LAST_ORDINARY_MAP (line_table)->to_file = sfname;
363 if (current_class == main_class) main_input_filename = sfname;
369 /* Annotation handling.
371 The technique we use here is to copy the annotation data directly
372 from the input class file into the output file. We don't decode the
373 data at all, merely rewriting constant indexes whenever we come
374 across them: this is necessary because the constant pool in the
375 output file isn't the same as the constant pool in the input.
377 The main advantage of this technique is that the resulting
378 annotation data is pointer-free, so it doesn't have to be relocated
379 at startup time. As a consequence of this, annotations have no
380 performance impact unless they are used. Also, this representation
381 is very dense. */
384 /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
385 the start of the newly allocated region. */
387 static unsigned char*
388 annotation_grow (int delta)
390 unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
391 long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
392 long len = *datasize;
394 if (*data == NULL)
396 *data = XNEWVAR (unsigned char, delta);
398 else
400 int newlen = *datasize + delta;
401 if (floor_log2 (newlen) != floor_log2 (*datasize))
402 *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
404 *datasize += delta;
405 return *data + len;
408 /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
409 byte order (i.e. big endian.) */
411 static void
412 annotation_rewrite_byte (unsigned int n, unsigned char *p)
414 p[0] = n;
417 static void
418 annotation_rewrite_short (unsigned int n, unsigned char *p)
420 p[0] = n>>8;
421 p[1] = n;
424 static void
425 annotation_rewrite_int (unsigned int n, unsigned char *p)
427 p[0] = n>>24;
428 p[1] = n>>16;
429 p[2] = n>>8;
430 p[3] = n;
433 /* Read a 16-bit unsigned int in Java byte order (i.e. big
434 endian.) */
436 static uint16
437 annotation_read_short (unsigned char *p)
439 uint16 tmp = p[0];
440 tmp = (tmp << 8) | p[1];
441 return tmp;
444 /* annotation_write_TYPE. Rewrite various int types, appending them
445 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
446 endian.) */
448 static void
449 annotation_write_byte (unsigned int n)
451 annotation_rewrite_byte (n, annotation_grow (1));
454 static void
455 annotation_write_short (unsigned int n)
457 annotation_rewrite_short (n, annotation_grow (2));
460 static void
461 annotation_write_int (unsigned int n)
463 annotation_rewrite_int (n, annotation_grow (4));
466 /* Create a 64-bit constant in the constant pool.
468 This is used for both integer and floating-point types. As a
469 consequence, it will not work if the target floating-point format
470 is anything other than IEEE-754. While this is arguably a bug, the
471 runtime library makes exactly the same assumption and it's unlikely
472 that Java will ever run on a non-IEEE machine. */
474 static int
475 handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
476 int index, bool big_endian)
478 /* If we're on a 64-bit platform we can fit a long or double
479 into the same space as a jword. */
480 if (POINTER_SIZE >= 64)
481 index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
483 /* In a compiled program the constant pool is in native word
484 order. How weird is that??? */
485 else if (big_endian)
486 index = find_constant2 (cpool, kind,
487 JPOOL_INT (jcf, index),
488 JPOOL_INT (jcf, index+1));
489 else
490 index = find_constant2 (cpool, kind,
491 JPOOL_INT (jcf, index+1),
492 JPOOL_INT (jcf, index));
494 return index;
497 /* Given a class file and an index into its constant pool, create an
498 entry in the outgoing constant pool for the same item. */
500 static uint16
501 handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
503 unsigned int kind;
504 CPool *cpool = cpool_for_class (output_class);
506 if (index == 0)
507 return 0;
509 if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
510 error ("<constant pool index %d not in range>", index);
512 kind = JPOOL_TAG (jcf, index);
514 if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
516 if (purpose == CONSTANT_Class
517 && kind == CONSTANT_Utf8)
519 else
520 error ("<constant pool index %d unexpected type", index);
523 switch (kind)
525 case CONSTANT_Class:
526 case CONSTANT_ResolvedClass:
528 /* For some reason I know not the what of, class names in
529 annotations are UTF-8 strings in the constant pool but
530 class names in EnclosingMethod attributes are real class
531 references. Set CONSTANT_LazyFlag here so that the VM
532 doesn't attempt to resolve them at class initialization
533 time. */
534 tree resolved_class, class_name;
535 resolved_class = get_class_constant (jcf, index);
536 class_name = build_internal_class_name (resolved_class);
537 index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
538 (unmangle_classname
539 (IDENTIFIER_POINTER(class_name),
540 IDENTIFIER_LENGTH(class_name))));
541 break;
543 case CONSTANT_Utf8:
545 tree utf8 = get_constant (jcf, index);
546 if (purpose == CONSTANT_Class)
547 /* Create a constant pool entry for a type signature. This
548 one has '.' rather than '/' because it isn't going into a
549 class file, it's going into a compiled object.
551 This has to match the logic in
552 _Jv_ClassReader::prepare_pool_entry(). */
553 utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
554 IDENTIFIER_LENGTH(utf8));
555 index = alloc_name_constant (kind, utf8);
557 break;
559 case CONSTANT_Long:
560 index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
561 targetm.words_big_endian ());
562 break;
564 case CONSTANT_Double:
565 index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
566 targetm.float_words_big_endian ());
567 break;
569 case CONSTANT_Float:
570 case CONSTANT_Integer:
571 index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
572 break;
574 case CONSTANT_NameAndType:
576 uint16 name = JPOOL_USHORT1 (jcf, index);
577 uint16 sig = JPOOL_USHORT2 (jcf, index);
578 uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
579 uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
580 jword new_index = (name_index << 16) | sig_index;
581 index = find_constant1 (cpool, kind, new_index);
583 break;
585 default:
586 abort ();
589 return index;
592 /* Read an element_value structure from an annotation in JCF. Return
593 the constant pool index for the resulting constant pool entry. */
595 static int
596 handle_element_value (JCF *jcf, int level)
598 uint8 tag = JCF_readu (jcf);
599 int index = 0;
601 annotation_write_byte (tag);
602 switch (tag)
604 case 'B':
605 case 'C':
606 case 'S':
607 case 'Z':
608 case 'I':
610 uint16 cindex = JCF_readu2 (jcf);
611 index = handle_constant (jcf, cindex,
612 CONSTANT_Integer);
613 annotation_write_short (index);
615 break;
616 case 'D':
618 uint16 cindex = JCF_readu2 (jcf);
619 index = handle_constant (jcf, cindex,
620 CONSTANT_Double);
621 annotation_write_short (index);
623 break;
624 case 'F':
626 uint16 cindex = JCF_readu2 (jcf);
627 index = handle_constant (jcf, cindex,
628 CONSTANT_Float);
629 annotation_write_short (index);
631 break;
632 case 'J':
634 uint16 cindex = JCF_readu2 (jcf);
635 index = handle_constant (jcf, cindex,
636 CONSTANT_Long);
637 annotation_write_short (index);
639 break;
640 case 's':
642 uint16 cindex = JCF_readu2 (jcf);
643 /* Despite what the JVM spec says, compilers generate a Utf8
644 constant here, not a String. */
645 index = handle_constant (jcf, cindex,
646 CONSTANT_Utf8);
647 annotation_write_short (index);
649 break;
651 case 'e':
653 uint16 type_name_index = JCF_readu2 (jcf);
654 uint16 const_name_index = JCF_readu2 (jcf);
655 index = handle_constant (jcf, type_name_index,
656 CONSTANT_Class);
657 annotation_write_short (index);
658 index = handle_constant (jcf, const_name_index,
659 CONSTANT_Utf8);
660 annotation_write_short (index);
662 break;
663 case 'c':
665 uint16 class_info_index = JCF_readu2 (jcf);
666 index = handle_constant (jcf, class_info_index,
667 CONSTANT_Class);
668 annotation_write_short (index);
670 break;
671 case '@':
673 handle_annotation (jcf, level + 1);
675 break;
676 case '[':
678 uint16 n_array_elts = JCF_readu2 (jcf);
679 annotation_write_short (n_array_elts);
680 while (n_array_elts--)
681 handle_element_value (jcf, level + 1);
683 break;
684 default:
685 abort();
686 break;
688 return index;
691 /* Read an annotation structure from JCF. Write it to the
692 reflection_data field of the outgoing class. */
694 static void
695 handle_annotation (JCF *jcf, int level)
697 uint16 type_index = JCF_readu2 (jcf);
698 uint16 npairs = JCF_readu2 (jcf);
699 int index = handle_constant (jcf, type_index,
700 CONSTANT_Class);
701 annotation_write_short (index);
702 annotation_write_short (npairs);
703 while (npairs--)
705 uint16 name_index = JCF_readu2 (jcf);
706 index = handle_constant (jcf, name_index,
707 CONSTANT_Utf8);
708 annotation_write_short (index);
709 handle_element_value (jcf, level + 2);
713 /* Read an annotation count from JCF, and write the following
714 annotations to the reflection_data field of the outgoing class. */
716 static void
717 handle_annotations (JCF *jcf, int level)
719 uint16 num = JCF_readu2 (jcf);
720 annotation_write_short (num);
721 while (num--)
722 handle_annotation (jcf, level);
725 /* As handle_annotations(), but perform a sanity check that we write
726 the same number of bytes that we were expecting. */
728 static void
729 handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
730 long length)
732 long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
734 handle_annotations (jcf, 0);
736 gcc_assert (old_datasize + length
737 == TYPE_REFLECTION_DATASIZE (current_class));
740 /* gcj permutes its fields array after generating annotation_data, so
741 we have to fixup field indexes for fields that have moved. Given
742 ARG, a VEC_int, fixup the field indexes in the reflection_data of
743 the outgoing class. We use field_offsets to tell us where the
744 fixups must go. */
746 void
747 rewrite_reflection_indexes (void *arg)
749 bitmap_iterator bi;
750 unsigned int offset;
751 vec<int> *map = (vec<int> *) arg;
752 unsigned char *data = TYPE_REFLECTION_DATA (current_class);
754 if (map)
756 EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
758 uint16 index = annotation_read_short (data + offset);
759 annotation_rewrite_short
760 ((*map)[index], data + offset);
765 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
766 reflection_data of the outgoing class. */
768 static void
769 handle_member_annotations (int member_index, JCF *jcf,
770 const unsigned char *name ATTRIBUTE_UNUSED,
771 long len, jv_attr_type member_type)
773 int new_len = len + 1;
774 annotation_write_byte (member_type);
775 if (member_type != JV_CLASS_ATTR)
776 new_len += 2;
777 annotation_write_int (new_len);
778 annotation_write_byte (JV_ANNOTATIONS_KIND);
779 if (member_type == JV_FIELD_ATTR)
780 bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
781 if (member_type != JV_CLASS_ATTR)
782 annotation_write_short (member_index);
783 handle_annotation_attribute (member_index, jcf, len);
786 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
787 to the reflection_data of the outgoing class. */
789 static void
790 handle_parameter_annotations (int member_index, JCF *jcf,
791 const unsigned char *name ATTRIBUTE_UNUSED,
792 long len, jv_attr_type member_type)
794 int new_len = len + 1;
795 uint8 num;
796 annotation_write_byte (member_type);
797 if (member_type != JV_CLASS_ATTR)
798 new_len += 2;
799 annotation_write_int (new_len);
800 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
801 if (member_type != JV_CLASS_ATTR)
802 annotation_write_short (member_index);
803 num = JCF_readu (jcf);
804 annotation_write_byte (num);
805 while (num--)
806 handle_annotations (jcf, 0);
810 /* Read the AnnotationDefault data from JCF and write them to the
811 reflection_data of the outgoing class. */
813 static void
814 handle_default_annotation (int member_index, JCF *jcf,
815 const unsigned char *name ATTRIBUTE_UNUSED,
816 long len, jv_attr_type member_type)
818 int new_len = len + 1;
819 annotation_write_byte (member_type);
820 if (member_type != JV_CLASS_ATTR)
821 new_len += 2;
822 annotation_write_int (new_len);
823 annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
824 if (member_type != JV_CLASS_ATTR)
825 annotation_write_short (member_index);
826 handle_element_value (jcf, 0);
829 /* As above, for the EnclosingMethod attribute. */
831 static void
832 handle_enclosingmethod_attribute (int member_index, JCF *jcf,
833 const unsigned char *name ATTRIBUTE_UNUSED,
834 long len, jv_attr_type member_type)
836 int new_len = len + 1;
837 uint16 index;
838 annotation_write_byte (member_type);
839 if (member_type != JV_CLASS_ATTR)
840 new_len += 2;
841 annotation_write_int (new_len);
842 annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
843 if (member_type != JV_CLASS_ATTR)
844 annotation_write_short (member_index);
846 index = JCF_readu2 (jcf);
847 index = handle_constant (jcf, index, CONSTANT_Class);
848 annotation_write_short (index);
850 index = JCF_readu2 (jcf);
851 index = handle_constant (jcf, index, CONSTANT_NameAndType);
852 annotation_write_short (index);
855 /* As above, for the Signature attribute. */
857 static void
858 handle_signature_attribute (int member_index, JCF *jcf,
859 const unsigned char *name ATTRIBUTE_UNUSED,
860 long len, jv_attr_type member_type)
862 int new_len = len + 1;
863 uint16 index;
864 annotation_write_byte (member_type);
865 if (member_type != JV_CLASS_ATTR)
866 new_len += 2;
867 annotation_write_int (new_len);
868 annotation_write_byte (JV_SIGNATURE_KIND);
869 if (member_type != JV_CLASS_ATTR)
870 annotation_write_short (member_index);
872 index = JCF_readu2 (jcf);
873 index = handle_constant (jcf, index, CONSTANT_Utf8);
874 annotation_write_short (index);
879 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
881 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
882 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
883 output_class = current_class = give_name_to_class (jcf, THIS); \
884 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
886 #define HANDLE_CLASS_INTERFACE(INDEX) \
887 add_interface (current_class, get_class_constant (jcf, INDEX))
889 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
890 { int sig_index = SIGNATURE; \
891 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
892 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
893 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
894 if ((ACCESS_FLAGS) & ACC_FINAL) \
895 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
898 #define HANDLE_END_FIELDS() \
899 (current_field = NULL_TREE)
901 #define HANDLE_CONSTANTVALUE(INDEX) \
902 { tree constant; int index = INDEX; \
903 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
904 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
905 constant = build_utf8_ref (name); \
907 else \
908 constant = get_constant (jcf, index); \
909 set_constant_value (current_field, constant); }
911 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
912 (current_method = add_method (current_class, ACCESS_FLAGS, \
913 get_name_constant (jcf, NAME), \
914 get_name_constant (jcf, SIGNATURE)), \
915 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
916 DECL_LINENUMBERS_OFFSET (current_method) = 0)
918 #define HANDLE_END_METHODS() \
919 { current_method = NULL_TREE; }
921 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
922 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
923 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
924 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
925 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
927 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
928 { int n = (COUNT); \
929 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
930 JCF_SKIP (jcf, n * 10); }
932 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
933 { int n = (COUNT); \
934 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
935 JCF_SKIP (jcf, n * 4); }
937 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
939 int n = COUNT; \
940 vec<tree, va_gc> *v; \
941 vec_alloc (v, n); \
942 gcc_assert (!DECL_FUNCTION_THROWS (current_method)); \
943 while (--n >= 0) \
945 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
946 v->quick_push (thrown_class); \
948 DECL_FUNCTION_THROWS (current_method) = v; \
951 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
953 /* Link seen inner classes to their outer context and register the
954 inner class to its outer context. They will be later loaded. */
955 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
956 handle_innerclass_attribute (COUNT, jcf, attribute_length)
958 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
960 /* Irrelevant decls should have been nullified by the END macros. \
961 DECL_ARTIFICIAL on fields is used for something else (See \
962 PUSH_FIELD in java-tree.h) */ \
963 if (current_method) \
964 DECL_ARTIFICIAL (current_method) = 1; \
965 else if (current_field) \
966 FIELD_SYNTHETIC (current_field) = 1; \
967 else \
968 TYPE_SYNTHETIC (current_class) = 1; \
971 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
973 if (current_class == object_type_node) \
974 jcf->right_zip = 1; \
977 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
979 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
982 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
984 JCF_SKIP(jcf, attribute_length); \
987 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
989 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
992 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
994 JCF_SKIP(jcf, attribute_length); \
997 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
999 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
1002 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
1004 handle_enclosingmethod_attribute (index, jcf, name_data, \
1005 attribute_length, attr_type); \
1008 #define HANDLE_SIGNATURE_ATTRIBUTE() \
1010 handle_signature_attribute (index, jcf, name_data, \
1011 attribute_length, attr_type); \
1014 #include "jcf-reader.c"
1016 tree
1017 parse_signature (JCF *jcf, int sig_index)
1019 gcc_assert (sig_index > 0
1020 && sig_index < JPOOL_SIZE (jcf)
1021 && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1023 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1024 JPOOL_UTF_LENGTH (jcf, sig_index));
1027 tree
1028 get_constant (JCF *jcf, int index)
1030 tree value;
1031 int tag;
1032 if (index <= 0 || index >= JPOOL_SIZE(jcf))
1033 goto bad;
1034 tag = JPOOL_TAG (jcf, index);
1035 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
1036 return jcf->cpool.data[index].t;
1037 switch (tag)
1039 case CONSTANT_Integer:
1041 jint num = JPOOL_INT(jcf, index);
1042 value = build_int_cst (int_type_node, num);
1043 break;
1045 case CONSTANT_Long:
1047 unsigned HOST_WIDE_INT num;
1049 num = JPOOL_UINT (jcf, index);
1050 wide_int val = wi::lshift (wide_int::from (num, 64, SIGNED), 32);
1051 num = JPOOL_UINT (jcf, index + 1);
1052 val |= num;
1054 value = wide_int_to_tree (long_type_node, val);
1055 break;
1058 case CONSTANT_Float:
1060 jint num = JPOOL_INT(jcf, index);
1061 long buf = num;
1062 REAL_VALUE_TYPE d;
1064 real_from_target_fmt (&d, &buf, &ieee_single_format);
1065 value = build_real (float_type_node, d);
1066 break;
1069 case CONSTANT_Double:
1071 long buf[2], lo, hi;
1072 REAL_VALUE_TYPE d;
1074 hi = JPOOL_UINT (jcf, index);
1075 lo = JPOOL_UINT (jcf, index+1);
1077 if (targetm.float_words_big_endian ())
1078 buf[0] = hi, buf[1] = lo;
1079 else
1080 buf[0] = lo, buf[1] = hi;
1082 real_from_target_fmt (&d, buf, &ieee_double_format);
1083 value = build_real (double_type_node, d);
1084 break;
1087 case CONSTANT_String:
1089 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
1090 const char *utf8_ptr = IDENTIFIER_POINTER (name);
1091 int utf8_len = IDENTIFIER_LENGTH (name);
1092 const unsigned char *utf8;
1093 int i;
1095 /* Check for a malformed Utf8 string. */
1096 utf8 = (const unsigned char *) utf8_ptr;
1097 i = utf8_len;
1098 while (i > 0)
1100 int char_len = UT8_CHAR_LENGTH (*utf8);
1101 if (char_len < 0 || char_len > 3 || char_len > i)
1102 fatal_error (input_location, "bad string constant");
1104 utf8 += char_len;
1105 i -= char_len;
1108 /* Allocate a new string value. */
1109 value = build_string (utf8_len, utf8_ptr);
1110 TREE_TYPE (value) = build_pointer_type (string_type_node);
1112 break;
1113 default:
1114 goto bad;
1116 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
1117 jcf->cpool.data[index].t = value;
1118 return value;
1119 bad:
1120 fatal_error (input_location, "bad value constant type %d, index %d",
1121 JPOOL_TAG (jcf, index), index);
1124 tree
1125 get_name_constant (JCF *jcf, int index)
1127 tree name = get_constant (jcf, index);
1128 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1129 return name;
1132 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1133 a non anonymous entry) is found, We augment the inner class list of
1134 the outer context with the newly resolved innerclass. */
1136 static void
1137 handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
1139 int c = count;
1141 annotation_write_byte (JV_CLASS_ATTR);
1142 annotation_write_int (attribute_length+1);
1143 annotation_write_byte (JV_INNER_CLASSES_KIND);
1144 annotation_write_short (count);
1146 while (c--)
1148 /* Read inner_class_info_index. This may be 0 */
1149 int icii = JCF_readu2 (jcf);
1150 /* Read outer_class_info_index. If the innerclasses attribute
1151 entry isn't a member (like an inner class) the value is 0. */
1152 int ocii = JCF_readu2 (jcf);
1153 /* Read inner_name_index. If the class we're dealing with is
1154 an anonymous class, it must be 0. */
1155 int ini = JCF_readu2 (jcf);
1156 /* Read the access flag. */
1157 int acc = JCF_readu2 (jcf);
1159 annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1160 annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1161 annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1162 annotation_write_short (acc);
1164 /* If icii is 0, don't try to read the class. */
1165 if (icii >= 0)
1167 tree klass = get_class_constant (jcf, icii);
1168 tree decl = TYPE_NAME (klass);
1169 /* Skip reading further if ocii is null */
1170 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1172 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1173 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
1174 set_class_decl_access_flags (acc, decl);
1175 DECL_CONTEXT (decl) = outer;
1176 DECL_INNER_CLASS_LIST (outer) =
1177 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1178 CLASS_COMPLETE_P (decl) = 1;
1184 static tree
1185 give_name_to_class (JCF *jcf, int i)
1187 gcc_assert (i > 0
1188 && i < JPOOL_SIZE (jcf)
1189 && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1192 tree package_name = NULL_TREE, tmp;
1193 tree this_class;
1194 int j = JPOOL_USHORT1 (jcf, i);
1195 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1196 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
1197 JPOOL_UTF_LENGTH (jcf, j));
1198 this_class = lookup_class (class_name);
1200 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
1201 const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
1202 linemap_add (line_table, LC_ENTER, false, sfname, 0);
1203 input_location = linemap_line_start (line_table, 0, 1);
1204 file_start_location = input_location;
1205 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1206 if (main_input_filename == NULL && jcf == main_jcf)
1207 main_input_filename = sfname;
1210 jcf->cpool.data[i].t = this_class;
1211 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1212 split_qualified_name (&package_name, &tmp,
1213 DECL_NAME (TYPE_NAME (this_class)));
1214 TYPE_PACKAGE (this_class) = package_name;
1215 return this_class;
1219 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1221 tree
1222 get_class_constant (JCF *jcf, int i)
1224 tree type;
1225 gcc_assert (i > 0
1226 && i < JPOOL_SIZE (jcf)
1227 && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
1229 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1231 int name_index = JPOOL_USHORT1 (jcf, i);
1232 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1233 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
1234 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
1236 if (name[0] == '[') /* Handle array "classes". */
1237 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
1238 else
1240 tree cname = unmangle_classname (name, nlength);
1241 type = lookup_class (cname);
1243 jcf->cpool.data[i].t = type;
1244 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1246 else
1247 type = jcf->cpool.data[i].t;
1248 return type;
1251 /* Read a class with the fully qualified-name NAME.
1252 Return 1 iff we read the requested file.
1253 (It is still possible we failed if the file did not
1254 define the class it is supposed to.) */
1257 read_class (tree name)
1259 JCF this_jcf, *jcf;
1260 tree icv, klass = NULL_TREE;
1261 tree save_current_class = current_class;
1262 tree save_output_class = output_class;
1263 location_t save_location = input_location;
1264 JCF *save_current_jcf = current_jcf;
1266 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
1268 klass = TREE_TYPE (icv);
1269 jcf = TYPE_JCF (klass);
1271 else
1272 jcf = NULL;
1274 if (jcf == NULL)
1276 const char* path_name;
1277 this_jcf.zipd = NULL;
1278 jcf = &this_jcf;
1280 path_name = find_class (IDENTIFIER_POINTER (name),
1281 IDENTIFIER_LENGTH (name),
1282 &this_jcf);
1283 if (path_name == 0)
1284 return 0;
1285 else
1286 free(CONST_CAST (char *, path_name));
1289 current_jcf = jcf;
1291 if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
1293 output_class = current_class = klass;
1294 if (JCF_SEEN_IN_ZIP (current_jcf))
1295 read_zip_member(current_jcf,
1296 current_jcf->zipd, current_jcf->zipd->zipf);
1297 jcf_parse (current_jcf);
1298 /* Parsing might change the class, in which case we have to
1299 put it back where we found it. */
1300 if (current_class != klass && icv != NULL_TREE)
1301 TREE_TYPE (icv) = current_class;
1302 klass = current_class;
1304 layout_class (klass);
1305 load_inner_classes (klass);
1307 output_class = save_output_class;
1308 current_class = save_current_class;
1309 input_location = save_location;
1310 current_jcf = save_current_jcf;
1311 return 1;
1314 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1315 called from the parser, otherwise it's a RECORD_TYPE node. If
1316 VERBOSE is 1, print error message on failure to load a class. */
1317 void
1318 load_class (tree class_or_name, int verbose)
1320 tree name, saved;
1321 int class_loaded = 0;
1322 tree class_decl = NULL_TREE;
1323 bool is_compiled_class = false;
1325 /* We've already failed, don't try again. */
1326 if (TREE_CODE (class_or_name) == RECORD_TYPE
1327 && TYPE_DUMMY (class_or_name))
1328 return;
1330 /* class_or_name can be the name of the class we want to load */
1331 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1332 name = class_or_name;
1333 /* In some cases, it's a dependency that we process earlier that
1334 we though */
1335 else if (TREE_CODE (class_or_name) == TREE_LIST)
1336 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1337 /* Or it's a type in the making */
1338 else
1339 name = DECL_NAME (TYPE_NAME (class_or_name));
1341 class_decl = IDENTIFIER_CLASS_VALUE (name);
1342 if (class_decl != NULL_TREE)
1344 tree type = TREE_TYPE (class_decl);
1345 is_compiled_class
1346 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1347 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1350 saved = name;
1352 /* If flag_verify_invocations is unset, we don't try to load a class
1353 unless we're looking for Object (which is fixed by the ABI) or
1354 it's a class that we're going to compile. */
1355 if (flag_verify_invocations
1356 || class_or_name == object_type_node
1357 || is_compiled_class
1358 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1360 while (1)
1362 const char *separator;
1364 /* We've already loaded it. */
1365 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1367 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1368 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1369 break;
1372 if (read_class (name))
1373 break;
1375 /* We failed loading name. Now consider that we might be looking
1376 for an inner class. */
1377 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1378 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
1379 name = get_identifier_with_length (IDENTIFIER_POINTER (name),
1380 (separator
1381 - IDENTIFIER_POINTER (name)));
1382 /* Otherwise, we failed, we bail. */
1383 else
1384 break;
1388 /* have we found the class we're looking for? */
1389 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1390 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1391 class_loaded = type && CLASS_PARSED_P (type);
1395 if (!class_loaded)
1397 if (flag_verify_invocations || ! flag_indirect_dispatch)
1399 if (verbose)
1400 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1402 else if (verbose)
1404 /* This is just a diagnostic during testing, not a real problem. */
1405 if (!quiet_flag)
1406 warning (0, "cannot find file for class %s",
1407 IDENTIFIER_POINTER (saved));
1409 /* Fake it. */
1410 if (TREE_CODE (class_or_name) == RECORD_TYPE)
1412 set_super_info (0, class_or_name, object_type_node, 0);
1413 TYPE_DUMMY (class_or_name) = 1;
1414 /* We won't be able to output any debug info for this class. */
1415 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1421 /* Parse the .class file JCF. */
1423 static void
1424 jcf_parse (JCF* jcf)
1426 int i, code;
1428 bitmap_clear (field_offsets);
1430 if (jcf_parse_preamble (jcf) != 0)
1431 fatal_error (input_location, "not a valid Java .class file");
1432 code = jcf_parse_constant_pool (jcf);
1433 if (code != 0)
1434 fatal_error (input_location, "error while parsing constant pool");
1435 code = verify_constant_pool (jcf);
1436 if (code > 0)
1437 fatal_error (input_location, "error in constant pool entry #%d\n", code);
1439 jcf_parse_class (jcf);
1440 if (main_class == NULL_TREE)
1441 main_class = current_class;
1442 if (! quiet_flag && TYPE_NAME (current_class))
1443 fprintf (stderr, " %s %s",
1444 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
1445 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
1446 if (CLASS_PARSED_P (current_class))
1448 /* FIXME - where was first time */
1449 fatal_error (input_location,
1450 "reading class %s for the second time from %s",
1451 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1452 jcf->filename);
1454 CLASS_PARSED_P (current_class) = 1;
1456 for (i = 1; i < JPOOL_SIZE(jcf); i++)
1458 switch (JPOOL_TAG (jcf, i))
1460 case CONSTANT_Class:
1461 get_class_constant (jcf, i);
1462 break;
1466 code = jcf_parse_fields (jcf);
1467 if (code != 0)
1468 fatal_error (input_location, "error while parsing fields");
1469 code = jcf_parse_methods (jcf);
1470 if (code != 0)
1471 fatal_error (input_location, "error while parsing methods");
1472 code = jcf_parse_final_attributes (jcf);
1473 if (code != 0)
1474 fatal_error (input_location, "error while parsing final attributes");
1476 if (TYPE_REFLECTION_DATA (current_class))
1477 annotation_write_byte (JV_DONE_ATTR);
1479 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1481 /* The fields of class_type_node are already in correct order. */
1482 if (current_class != class_type_node && current_class != object_type_node)
1483 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1485 if (current_class == object_type_node)
1486 layout_class_methods (object_type_node);
1487 else
1488 vec_safe_push (all_class_list, TYPE_NAME (current_class));
1491 /* If we came across inner classes, load them now. */
1492 static void
1493 load_inner_classes (tree cur_class)
1495 tree current;
1496 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
1497 current = TREE_CHAIN (current))
1499 tree name = DECL_NAME (TREE_PURPOSE (current));
1500 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1501 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1502 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
1503 load_class (name, 1);
1507 static void
1508 duplicate_class_warning (const char *filename)
1510 location_t warn_loc;
1511 linemap_add (line_table, LC_RENAME, 0, filename, 0);
1512 warn_loc = linemap_line_start (line_table, 0, 1);
1513 warning_at (warn_loc, 0, "duplicate class will only be compiled once");
1516 static void
1517 java_layout_seen_class_methods (void)
1519 unsigned start = 0;
1520 unsigned end = vec_safe_length (all_class_list);
1522 while (1)
1524 unsigned ix;
1525 unsigned new_length;
1527 for (ix = start; ix != end; ix++)
1529 tree decl = (*all_class_list)[ix];
1530 tree cls = TREE_TYPE (decl);
1532 input_location = DECL_SOURCE_LOCATION (decl);
1534 if (! CLASS_LOADED_P (cls))
1535 load_class (cls, 0);
1537 layout_class_methods (cls);
1540 /* Note that new classes might have been added while laying out
1541 methods, changing the value of all_class_list. */
1542 new_length = vec_safe_length (all_class_list);
1543 if (end != new_length)
1545 start = end;
1546 end = new_length;
1548 else
1549 break;
1553 static void
1554 parse_class_file (void)
1556 tree method;
1557 location_t save_location = input_location;
1559 java_layout_seen_class_methods ();
1561 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1563 /* Re-enter the current file. */
1564 expanded_location loc = expand_location (input_location);
1565 linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1567 file_start_location = input_location;
1568 (*debug_hooks->start_source_file) (LOCATION_LINE (input_location),
1569 LOCATION_FILE (input_location));
1571 java_mark_class_local (current_class);
1573 gen_indirect_dispatch_tables (current_class);
1575 for (method = TYPE_METHODS (current_class);
1576 method != NULL_TREE; method = DECL_CHAIN (method))
1578 JCF *jcf = current_jcf;
1580 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
1581 continue;
1583 if (METHOD_NATIVE (method))
1585 tree arg;
1586 int decl_max_locals;
1588 if (! flag_jni)
1589 continue;
1590 /* We need to compute the DECL_MAX_LOCALS. We need to take
1591 the wide types into account too. */
1592 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
1593 arg != end_params_node;
1594 arg = TREE_CHAIN (arg), decl_max_locals += 1)
1596 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1597 decl_max_locals += 1;
1599 DECL_MAX_LOCALS (method) = decl_max_locals;
1600 start_java_method (method);
1601 give_name_to_locals (jcf);
1602 *get_stmts () = build_jni_stub (method);
1603 end_java_method ();
1604 continue;
1607 if (DECL_CODE_OFFSET (method) == 0)
1609 current_function_decl = method;
1610 error ("missing Code attribute");
1611 continue;
1614 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1615 if (DECL_LINENUMBERS_OFFSET (method))
1617 int i;
1618 int min_line = 0;
1619 unsigned char *ptr;
1620 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1621 linenumber_count = i = JCF_readu2 (jcf);
1622 linenumber_table = ptr = jcf->read_ptr;
1624 for (ptr += 2; --i >= 0; ptr += 4)
1626 int line = GET_u2 (ptr);
1627 /* Set initial line of input_location to smallest
1628 * linenumber.
1629 * Needs to be set before init_function_start. */
1630 if (min_line == 0 || line < min_line)
1631 min_line = line;
1633 if (min_line != 0)
1634 input_location = linemap_line_start (line_table, min_line, 1);
1636 else
1638 linenumber_table = NULL;
1639 linenumber_count = 0;
1642 start_java_method (method);
1644 note_instructions (jcf, method);
1646 give_name_to_locals (jcf);
1648 /* Bump up start_label_pc_this_method so we get a unique label number
1649 and reset highest_label_pc_this_method. */
1650 if (highest_label_pc_this_method >= 0)
1652 /* We adjust to the next multiple of 1000. This is just a frill
1653 so the last 3 digits of the label number match the bytecode
1654 offset, which might make debugging marginally more convenient. */
1655 start_label_pc_this_method
1656 = ((((start_label_pc_this_method + highest_label_pc_this_method)
1657 / 1000)
1658 + 1)
1659 * 1000);
1660 highest_label_pc_this_method = -1;
1663 /* Convert bytecode to trees. */
1664 expand_byte_code (jcf, method);
1666 end_java_method ();
1669 finish_class ();
1671 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
1672 input_location = save_location;
1675 static vec<tree, va_gc> *predefined_filenames;
1677 void
1678 add_predefined_file (tree name)
1680 vec_safe_push (predefined_filenames, name);
1684 predefined_filename_p (tree node)
1686 unsigned ix;
1687 tree f;
1689 FOR_EACH_VEC_SAFE_ELT (predefined_filenames, ix, f)
1690 if (f == node)
1691 return 1;
1693 return 0;
1696 /* Generate a function that does all static initialization for this
1697 translation unit. */
1699 static void
1700 java_emit_static_constructor (void)
1702 tree body = NULL;
1704 emit_register_classes (&body);
1705 write_resource_constructor (&body);
1707 if (body)
1709 tree name = get_identifier ("_Jv_global_static_constructor");
1711 tree decl
1712 = build_decl (input_location, FUNCTION_DECL, name,
1713 build_function_type_list (void_type_node, NULL_TREE));
1715 tree resdecl = build_decl (input_location,
1716 RESULT_DECL, NULL_TREE, void_type_node);
1717 DECL_ARTIFICIAL (resdecl) = 1;
1718 DECL_RESULT (decl) = resdecl;
1719 current_function_decl = decl;
1720 allocate_struct_function (decl, false);
1722 TREE_STATIC (decl) = 1;
1723 TREE_USED (decl) = 1;
1724 DECL_ARTIFICIAL (decl) = 1;
1725 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1726 DECL_SAVED_TREE (decl) = body;
1727 DECL_UNINLINABLE (decl) = 1;
1729 DECL_INITIAL (decl) = make_node (BLOCK);
1730 TREE_USED (DECL_INITIAL (decl)) = 1;
1732 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1733 java_genericize (decl);
1734 cgraph_node::finalize_function (decl, false);
1739 void
1740 java_parse_file (void)
1742 int filename_count = 0;
1743 location_t save_location = input_location;
1744 char *file_list = NULL, *list, *next;
1745 tree node;
1746 FILE *finput = NULL;
1747 int in_quotes = 0;
1748 unsigned ix;
1750 bitmap_obstack_initialize (&bit_obstack);
1751 field_offsets = BITMAP_ALLOC (&bit_obstack);
1753 if (flag_filelist_file)
1755 int avail = 2000;
1756 finput = fopen (main_input_filename, "r");
1757 if (finput == NULL)
1758 fatal_error (input_location,
1759 "can%'t open %s: %m", LOCATION_FILE (input_location));
1760 list = XNEWVEC (char, avail);
1761 next = list;
1762 for (;;)
1764 int count;
1765 if (avail < 500)
1767 count = next - list;
1768 avail = 2 * (count + avail);
1769 list = XRESIZEVEC (char, list, avail);
1770 next = list + count;
1771 avail = avail - count;
1773 /* Subtract one to guarantee space for final '\0'. */
1774 count = fread (next, 1, avail - 1, finput);
1775 if (count == 0)
1777 if (! feof (finput))
1778 fatal_error (input_location, "error closing %s: %m",
1779 LOCATION_FILE (input_location));
1780 *next = '\0';
1781 break;
1783 avail -= count;
1784 next += count;
1786 fclose (finput);
1787 finput = NULL;
1788 file_list = list;
1790 else
1791 list = CONST_CAST (char *, main_input_filename);
1793 while (list)
1795 for (next = list; ; )
1797 char ch = *next;
1798 if (flag_filelist_file && ! in_quotes
1799 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1800 || ch == '&') /* FIXME */)
1802 if (next == list)
1804 next++;
1805 list = next;
1806 continue;
1808 else
1810 *next++ = '\0';
1811 break;
1814 if (flag_filelist_file && ch == '"')
1816 in_quotes = ! in_quotes;
1817 *next++ = '\0';
1818 if (in_quotes)
1819 list = next;
1820 else
1821 break;
1823 if (ch == '\0')
1825 next = NULL;
1826 break;
1828 next++;
1831 /* Exclude .java files. */
1832 if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1834 /* Nothing. */
1836 else if (list[0])
1838 node = get_identifier (list);
1840 filename_count++;
1842 /* Exclude file that we see twice on the command line. */
1844 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1845 duplicate_class_warning (IDENTIFIER_POINTER (node));
1846 else
1848 build_translation_unit_decl (node);
1849 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1852 list = next;
1855 free (file_list);
1857 if (filename_count == 0)
1858 warning (0, "no input file specified");
1860 if (resource_name)
1862 const char *resource_filename;
1864 /* Only one resource file may be compiled at a time. */
1865 gcc_assert (all_translation_units->length () == 1);
1867 resource_filename
1868 = IDENTIFIER_POINTER (DECL_NAME ((*all_translation_units)[0]));
1869 compile_resource_file (resource_name, resource_filename);
1871 goto finish;
1874 current_jcf = main_jcf;
1875 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1877 unsigned char magic_string[4];
1878 char *real_path;
1879 uint32 magic = 0;
1880 tree name = DECL_NAME (node);
1881 tree real_file;
1882 const char *filename = IDENTIFIER_POINTER (name);
1884 /* Skip already parsed files */
1885 real_path = lrealpath (filename);
1886 real_file = get_identifier (real_path);
1887 free (real_path);
1888 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1889 continue;
1891 /* Close previous descriptor, if any */
1892 if (finput && fclose (finput))
1893 fatal_error (input_location,
1894 "can%'t close input file %s: %m", main_input_filename);
1896 finput = fopen (filename, "rb");
1897 if (finput == NULL)
1898 fatal_error (input_location, "can%'t open %s: %m", filename);
1900 #ifdef IO_BUFFER_SIZE
1901 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1902 _IOFBF, IO_BUFFER_SIZE);
1903 #endif
1905 /* Figure what kind of file we're dealing with */
1906 if (fread (magic_string, 1, 4, finput) == 4)
1908 fseek (finput, 0L, SEEK_SET);
1909 magic = GET_u4 (magic_string);
1911 if (magic == 0xcafebabe)
1913 CLASS_FILE_P (node) = 1;
1914 current_jcf = ggc_cleared_alloc<JCF> ();
1915 current_jcf->read_state = finput;
1916 current_jcf->filbuf = jcf_filbuf_from_stdio;
1917 jcf_parse (current_jcf);
1918 DECL_SOURCE_LOCATION (node) = file_start_location;
1919 TYPE_JCF (current_class) = current_jcf;
1920 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1922 /* We've already compiled this class. */
1923 duplicate_class_warning (filename);
1924 continue;
1926 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1927 TREE_TYPE (node) = current_class;
1929 else if (magic == (JCF_u4)ZIPMAGIC)
1931 main_jcf = ggc_cleared_alloc<JCF> ();
1932 main_jcf->read_state = finput;
1933 main_jcf->filbuf = jcf_filbuf_from_stdio;
1934 linemap_add (line_table, LC_ENTER, false, filename, 0);
1935 input_location = linemap_line_start (line_table, 0, 1);
1936 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1937 fatal_error (input_location, "bad zip/jar file %s", filename);
1938 localToFile = SeenZipFiles;
1939 /* Register all the classes defined there. */
1940 process_zip_dir ((FILE *) main_jcf->read_state);
1941 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1942 parse_zip_file_entries ();
1944 else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1946 /* Ignore an empty input jar. */
1948 else
1950 gcc_unreachable ();
1951 #if 0
1952 java_push_parser_context ();
1953 java_parser_context_save_global ();
1955 parse_source_file_1 (real_file, filename, finput);
1956 java_parser_context_restore_global ();
1957 java_pop_parser_context (1);
1958 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1959 #endif
1963 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1965 input_location = DECL_SOURCE_LOCATION (node);
1966 if (CLASS_FILE_P (node))
1968 /* FIXME: These two flags really should be independent. We
1969 should be able to compile fully binary compatible, but
1970 with flag_verify_invocations on. */
1971 flag_verify_invocations = ! flag_indirect_dispatch;
1972 output_class = current_class = TREE_TYPE (node);
1974 current_jcf = TYPE_JCF (current_class);
1975 layout_class (current_class);
1976 load_inner_classes (current_class);
1977 parse_class_file ();
1978 JCF_FINISH (current_jcf);
1981 input_location = save_location;
1983 bitmap_obstack_release (&bit_obstack);
1985 finish:
1986 /* Arrange for any necessary initialization to happen. */
1987 java_emit_static_constructor ();
1988 gcc_assert (global_bindings_p ());
1990 /* Do final processing on globals. */
1991 global_decl_processing ();
1995 /* Return the name of the class corresponding to the name of the file
1996 in this zip entry. The result is newly allocated using ALLOC. */
1997 static char *
1998 compute_class_name (struct ZipDirectory *zdir)
2000 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2001 char *class_name;
2002 int i;
2003 int filename_length = zdir->filename_length;
2005 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
2007 class_name_in_zip_dir += 2;
2008 filename_length -= 2;
2011 filename_length -= strlen (".class");
2012 class_name = XNEWVEC (char, filename_length + 1);
2013 memcpy (class_name, class_name_in_zip_dir, filename_length);
2014 class_name [filename_length] = '\0';
2016 for (i = 0; i < filename_length; i++)
2017 if (class_name[i] == '/')
2018 class_name[i] = '.';
2020 return class_name;
2023 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2024 if it is a property file of some sort. */
2025 static int
2026 classify_zip_file (struct ZipDirectory *zdir)
2028 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2030 if (zdir->filename_length > 6
2031 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2032 ".class", 6))
2033 return 1;
2035 /* For now we drop the manifest, but not other information. */
2036 if (zdir->filename_length == 20
2037 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
2038 return 0;
2040 /* Drop directory entries. */
2041 if (zdir->filename_length > 0
2042 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2043 return 0;
2045 return 2;
2048 /* Process all class entries found in the zip file. */
2049 static void
2050 parse_zip_file_entries (void)
2052 struct ZipDirectory *zdir;
2053 int i;
2055 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2056 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2058 tree klass;
2060 switch (classify_zip_file (zdir))
2062 case 0:
2063 continue;
2065 case 1:
2067 char *class_name = compute_class_name (zdir);
2068 int previous_alias_set = -1;
2069 klass = lookup_class (get_identifier (class_name));
2070 FREE (class_name);
2071 current_jcf = TYPE_JCF (klass);
2072 output_class = current_class = klass;
2074 /* This is a dummy class, and now we're compiling it for
2075 real. */
2076 gcc_assert (! TYPE_DUMMY (klass));
2078 /* This is for a corner case where we have a superclass
2079 but no superclass fields.
2081 This can happen if we earlier failed to lay out this
2082 class because its superclass was still in the process
2083 of being laid out; this occurs when we have recursive
2084 class dependencies via inner classes. We must record
2085 the previous alias set and restore it after laying out
2086 the class.
2088 FIXME: this really is a kludge. We should figure out a
2089 way to lay out the class properly before this
2090 happens. */
2091 if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2092 && integer_zerop (TYPE_SIZE (klass)))
2094 TYPE_SIZE (klass) = NULL_TREE;
2095 previous_alias_set = TYPE_ALIAS_SET (klass);
2096 TYPE_ALIAS_SET (klass) = -1;
2099 if (! CLASS_LOADED_P (klass))
2101 if (! CLASS_PARSED_P (klass))
2103 read_zip_member (current_jcf, zdir, localToFile);
2104 jcf_parse (current_jcf);
2106 layout_class (current_class);
2107 load_inner_classes (current_class);
2110 if (previous_alias_set != -1)
2111 TYPE_ALIAS_SET (klass) = previous_alias_set;
2113 if (TYPE_SIZE (current_class) != error_mark_node)
2115 parse_class_file ();
2116 free (current_jcf->buffer); /* No longer necessary */
2117 /* Note: there is a way to free this buffer right after a
2118 class seen in a zip file has been parsed. The idea is the
2119 set its jcf in such a way that buffer will be reallocated
2120 the time the code for the class will be generated. FIXME. */
2123 break;
2125 case 2:
2127 char *file_name, *class_name_in_zip_dir, *buffer;
2128 JCF *jcf;
2129 file_name = XNEWVEC (char, zdir->filename_length + 1);
2130 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2131 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2132 file_name[zdir->filename_length] = '\0';
2133 jcf = XNEW (JCF);
2134 JCF_ZERO (jcf);
2135 jcf->read_state = finput;
2136 jcf->filbuf = jcf_filbuf_from_stdio;
2137 jcf->classname = NULL;
2138 jcf->filename = file_name;
2139 jcf->zipd = zdir;
2141 if (read_zip_member (jcf, zdir, localToFile) < 0)
2142 fatal_error (input_location,
2143 "error while reading %s from zip file", file_name);
2145 buffer = XNEWVEC (char, zdir->filename_length + 1 +
2146 (jcf->buffer_end - jcf->buffer));
2147 strcpy (buffer, file_name);
2148 /* This is not a typo: we overwrite the trailing \0 of the
2149 file name; this is just how the data is laid out. */
2150 memcpy (buffer + zdir->filename_length,
2151 jcf->buffer, jcf->buffer_end - jcf->buffer);
2153 compile_resource_data (file_name, buffer,
2154 jcf->buffer_end - jcf->buffer);
2155 JCF_FINISH (jcf);
2156 free (jcf);
2157 free (buffer);
2159 break;
2161 default:
2162 gcc_unreachable ();
2167 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
2168 jcf up for further processing and link it to the created class. */
2170 static void
2171 process_zip_dir (FILE *finput)
2173 int i;
2174 ZipDirectory *zdir;
2176 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2177 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2179 char *class_name, *file_name, *class_name_in_zip_dir;
2180 tree klass;
2181 JCF *jcf;
2183 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2185 /* Here we skip non-class files; we handle them later. */
2186 if (classify_zip_file (zdir) != 1)
2187 continue;
2189 class_name = compute_class_name (zdir);
2190 file_name = XNEWVEC (char, zdir->filename_length+1);
2191 jcf = ggc_cleared_alloc<JCF> ();
2193 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2194 file_name [zdir->filename_length] = '\0';
2196 klass = lookup_class (get_identifier (class_name));
2198 if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2200 /* We've already compiled this class. */
2201 duplicate_class_warning (file_name);
2202 continue;
2204 /* This function is only called when processing a zip file seen
2205 on the command line. */
2206 CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
2208 jcf->read_state = finput;
2209 jcf->filbuf = jcf_filbuf_from_stdio;
2210 jcf->classname = class_name;
2211 jcf->filename = file_name;
2212 jcf->zipd = zdir;
2214 TYPE_JCF (klass) = jcf;
2218 #include "gt-java-jcf-parse.h"
2219 #include "gtype-java.h"