* xvasprintf.c: New file.
[official-gcc.git] / gcc / java / jcf-parse.c
bloba94b15110a746726a06539d366f2621d1943cc35
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996-2014 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 "tree.h"
30 #include "stringpool.h"
31 #include "obstack.h"
32 #include "flags.h"
33 #include "java-except.h"
34 #include "input.h"
35 #include "javaop.h"
36 #include "java-tree.h"
37 #include "diagnostic-core.h"
38 #include "parse.h"
39 #include "ggc.h"
40 #include "debug.h"
41 #include "hash-map.h"
42 #include "is-a.h"
43 #include "plugin-api.h"
44 #include "vec.h"
45 #include "hashtab.h"
46 #include "hash-set.h"
47 #include "machmode.h"
48 #include "tm.h"
49 #include "hard-reg-set.h"
50 #include "function.h"
51 #include "ipa-ref.h"
52 #include "cgraph.h"
53 #include "bitmap.h"
54 #include "target.h"
55 #include "wide-int.h"
57 #ifdef HAVE_LOCALE_H
58 #include <locale.h>
59 #endif
61 #ifdef HAVE_LANGINFO_CODESET
62 #include <langinfo.h>
63 #endif
65 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
66 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
67 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
68 #define JPOOL_UTF_DATA(JCF, INDEX) \
69 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
70 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
71 do { \
72 unsigned char save; unsigned char *text; \
73 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
74 text = (JCF)->read_ptr; \
75 save = text[LENGTH]; \
76 text[LENGTH] = 0; \
77 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
78 text[LENGTH] = save; \
79 JCF_SKIP (JCF, LENGTH); } while (0)
81 #include "jcf.h"
83 extern struct obstack temporary_obstack;
85 static GTY(()) tree parse_roots[2];
87 /* The FIELD_DECL for the current field. */
88 #define current_field parse_roots[0]
90 /* The METHOD_DECL for the current method. */
91 #define current_method parse_roots[1]
93 /* Line 0 in current file, if compiling from bytecode. */
94 static location_t file_start_location;
96 /* The Java archive that provides main_class; the main input file. */
97 static GTY(()) struct JCF * main_jcf;
99 /* A list of all the class DECLs seen so far. */
100 static GTY(()) vec<tree, va_gc> *all_class_list;
102 /* The number of source files passed to us by -fsource-filename and an
103 array of pointers to each name. Used by find_sourcefile(). */
104 static int num_files = 0;
105 static char **filenames;
107 static struct ZipFile *localToFile;
109 /* A map of byte offsets in the reflection data that are fields which
110 need renumbering. */
111 bitmap field_offsets;
112 bitmap_obstack bit_obstack;
114 /* Declarations of some functions used here. */
115 static void handle_innerclass_attribute (int count, JCF *, int len);
116 static tree give_name_to_class (JCF *jcf, int index);
117 static char *compute_class_name (struct ZipDirectory *zdir);
118 static int classify_zip_file (struct ZipDirectory *zdir);
119 static void parse_zip_file_entries (void);
120 static void process_zip_dir (FILE *);
121 static void parse_class_file (void);
122 static void handle_deprecated (void);
123 static void set_source_filename (JCF *, int);
124 static void jcf_parse (struct JCF*);
125 static void load_inner_classes (tree);
126 static void handle_annotation (JCF *jcf, int level);
127 static void java_layout_seen_class_methods (void);
129 /* Handle "Deprecated" attribute. */
130 static void
131 handle_deprecated (void)
133 if (current_field != NULL_TREE)
134 FIELD_DEPRECATED (current_field) = 1;
135 else if (current_method != NULL_TREE)
136 METHOD_DEPRECATED (current_method) = 1;
137 else if (current_class != NULL_TREE)
138 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
139 else
141 /* Shouldn't happen. */
142 gcc_unreachable ();
148 /* Reverse a string. */
149 static char *
150 reverse (const char *s)
152 if (s == NULL)
153 return NULL;
154 else
156 int len = strlen (s);
157 char *d = XNEWVAR (char, len + 1);
158 const char *sp;
159 char *dp;
161 d[len] = 0;
162 for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
163 *dp = *sp;
165 return d;
169 /* Compare two strings for qsort(). */
170 static int
171 cmpstringp (const void *p1, const void *p2)
173 /* The arguments to this function are "pointers to
174 pointers to char", but strcmp() arguments are "pointers
175 to char", hence the following cast plus dereference */
177 return strcmp(*(const char *const*) p1, *(const char *const*) p2);
180 /* Create an array of strings, one for each source file that we've
181 seen. fsource_filename can either be the name of a single .java
182 file or a file that contains a list of filenames separated by
183 newlines. */
184 void
185 java_read_sourcefilenames (const char *fsource_filename)
187 if (fsource_filename
188 && filenames == 0
189 && strlen (fsource_filename) > strlen (".java")
190 && filename_cmp ((fsource_filename
191 + strlen (fsource_filename)
192 - strlen (".java")),
193 ".java") != 0)
195 /* fsource_filename isn't a .java file but a list of filenames
196 separated by newlines */
197 FILE *finput = fopen (fsource_filename, "r");
198 int len = 0;
199 int longest_line = 0;
201 gcc_assert (finput);
203 /* Find out how many files there are, and how long the filenames are. */
204 while (! feof (finput))
206 int ch = getc (finput);
207 if (ch == '\n')
209 num_files++;
210 if (len > longest_line)
211 longest_line = len;
212 len = 0;
213 continue;
215 if (ch == EOF)
216 break;
217 len++;
220 rewind (finput);
222 /* Read the filenames. Put a pointer to each filename into the
223 array FILENAMES. */
225 char *linebuf = (char *) alloca (longest_line + 1);
226 int i = 0;
227 int charpos;
229 filenames = XNEWVEC (char *, num_files);
231 charpos = 0;
232 for (;;)
234 int ch = getc (finput);
235 if (ch == EOF)
236 break;
237 if (ch == '\n')
239 linebuf[charpos] = 0;
240 gcc_assert (i < num_files);
241 /* ??? Perhaps we should use lrealpath() here. Doing
242 so would tidy up things like /../ but the rest of
243 gcc seems to assume relative pathnames, not
244 absolute pathnames. */
245 /* realname = lrealpath (linebuf); */
246 filenames[i++] = reverse (linebuf);
247 charpos = 0;
248 continue;
250 gcc_assert (charpos < longest_line);
251 linebuf[charpos++] = ch;
254 if (num_files > 1)
255 qsort (filenames, num_files, sizeof (char *), cmpstringp);
257 fclose (finput);
259 else
261 filenames = XNEWVEC (char *, 1);
262 filenames[0] = reverse (fsource_filename);
263 num_files = 1;
267 /* Given a relative pathname such as foo/bar.java, attempt to find a
268 longer pathname with the same suffix.
270 This is a best guess heuristic; with some weird class hierarchies we
271 may fail to pick the correct source file. For example, if we have
272 the filenames foo/bar.java and also foo/foo/bar.java, we do not
273 have enough information to know which one is the right match for
274 foo/bar.java. */
276 static const char *
277 find_sourcefile (const char *name)
279 int i = 0, j = num_files-1;
280 char *found = NULL;
282 if (filenames)
284 char *revname = reverse (name);
288 int k = (i+j) / 2;
289 int cmp = strncmp (revname, filenames[k], strlen (revname));
290 if (cmp == 0)
292 /* OK, so we found one. But is it a unique match? */
293 if ((k > i
294 && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
295 || (k < j
296 && (strncmp (revname, filenames[k+1], strlen (revname))
297 == 0)))
299 else
300 found = filenames[k];
301 break;
303 if (cmp > 0)
304 i = k+1;
305 else
306 j = k-1;
308 while (i <= j);
310 free (revname);
313 if (found && strlen (found) > strlen (name))
314 return reverse (found);
315 else
316 return name;
321 /* Handle "SourceFile" attribute. */
323 static void
324 set_source_filename (JCF *jcf, int index)
326 tree sfname_id = get_name_constant (jcf, index);
327 const char *sfname = IDENTIFIER_POINTER (sfname_id);
328 const char *old_filename = LOCATION_FILE (input_location);
329 int new_len = IDENTIFIER_LENGTH (sfname_id);
330 if (old_filename != NULL)
332 int old_len = strlen (old_filename);
333 /* Use the filename from current input_location (derived from the
334 class name) if it has a directory prefix, but otherwise matches
335 sfname. */
336 if (old_len > new_len
337 && filename_cmp (sfname, old_filename + old_len - new_len) == 0
338 && (old_filename[old_len - new_len - 1] == '/'
339 || old_filename[old_len - new_len - 1] == '\\'))
340 return;
342 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
344 const char *class_name
345 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
346 const char *dot = strrchr (class_name, '.');
347 if (dot != NULL)
349 /* Length of prefix, not counting final dot. */
350 int i = dot - class_name;
351 /* Concatenate current package prefix with new sfname. */
352 char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
353 strcpy (buf + i + 1, sfname);
354 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
355 Note we start at the end with the final package dot. */
356 for (; i >= 0; i--)
358 char c = class_name[i];
359 if (c == '.')
360 c = DIR_SEPARATOR;
361 buf[i] = c;
363 sfname_id = get_identifier (buf);
364 free (buf);
365 sfname = IDENTIFIER_POINTER (sfname_id);
369 sfname = find_sourcefile (sfname);
370 ORDINARY_MAP_FILE_NAME (LINEMAPS_LAST_ORDINARY_MAP (line_table)) = sfname;
371 if (current_class == main_class) main_input_filename = sfname;
377 /* Annotation handling.
379 The technique we use here is to copy the annotation data directly
380 from the input class file into the output file. We don't decode the
381 data at all, merely rewriting constant indexes whenever we come
382 across them: this is necessary because the constant pool in the
383 output file isn't the same as the constant pool in the input.
385 The main advantage of this technique is that the resulting
386 annotation data is pointer-free, so it doesn't have to be relocated
387 at startup time. As a consequence of this, annotations have no
388 performance impact unless they are used. Also, this representation
389 is very dense. */
392 /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
393 the start of the newly allocated region. */
395 static unsigned char*
396 annotation_grow (int delta)
398 unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
399 long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
400 long len = *datasize;
402 if (*data == NULL)
404 *data = XNEWVAR (unsigned char, delta);
406 else
408 int newlen = *datasize + delta;
409 if (floor_log2 (newlen) != floor_log2 (*datasize))
410 *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
412 *datasize += delta;
413 return *data + len;
416 /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
417 byte order (i.e. big endian.) */
419 static void
420 annotation_rewrite_byte (unsigned int n, unsigned char *p)
422 p[0] = n;
425 static void
426 annotation_rewrite_short (unsigned int n, unsigned char *p)
428 p[0] = n>>8;
429 p[1] = n;
432 static void
433 annotation_rewrite_int (unsigned int n, unsigned char *p)
435 p[0] = n>>24;
436 p[1] = n>>16;
437 p[2] = n>>8;
438 p[3] = n;
441 /* Read a 16-bit unsigned int in Java byte order (i.e. big
442 endian.) */
444 static uint16
445 annotation_read_short (unsigned char *p)
447 uint16 tmp = p[0];
448 tmp = (tmp << 8) | p[1];
449 return tmp;
452 /* annotation_write_TYPE. Rewrite various int types, appending them
453 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
454 endian.) */
456 static void
457 annotation_write_byte (unsigned int n)
459 annotation_rewrite_byte (n, annotation_grow (1));
462 static void
463 annotation_write_short (unsigned int n)
465 annotation_rewrite_short (n, annotation_grow (2));
468 static void
469 annotation_write_int (unsigned int n)
471 annotation_rewrite_int (n, annotation_grow (4));
474 /* Create a 64-bit constant in the constant pool.
476 This is used for both integer and floating-point types. As a
477 consequence, it will not work if the target floating-point format
478 is anything other than IEEE-754. While this is arguably a bug, the
479 runtime library makes exactly the same assumption and it's unlikely
480 that Java will ever run on a non-IEEE machine. */
482 static int
483 handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
484 int index, bool big_endian)
486 /* If we're on a 64-bit platform we can fit a long or double
487 into the same space as a jword. */
488 if (POINTER_SIZE >= 64)
489 index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
491 /* In a compiled program the constant pool is in native word
492 order. How weird is that??? */
493 else if (big_endian)
494 index = find_constant2 (cpool, kind,
495 JPOOL_INT (jcf, index),
496 JPOOL_INT (jcf, index+1));
497 else
498 index = find_constant2 (cpool, kind,
499 JPOOL_INT (jcf, index+1),
500 JPOOL_INT (jcf, index));
502 return index;
505 /* Given a class file and an index into its constant pool, create an
506 entry in the outgoing constant pool for the same item. */
508 static uint16
509 handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
511 unsigned int kind;
512 CPool *cpool = cpool_for_class (output_class);
514 if (index == 0)
515 return 0;
517 if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
518 error ("<constant pool index %d not in range>", index);
520 kind = JPOOL_TAG (jcf, index);
522 if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
524 if (purpose == CONSTANT_Class
525 && kind == CONSTANT_Utf8)
527 else
528 error ("<constant pool index %d unexpected type", index);
531 switch (kind)
533 case CONSTANT_Class:
534 case CONSTANT_ResolvedClass:
536 /* For some reason I know not the what of, class names in
537 annotations are UTF-8 strings in the constant pool but
538 class names in EnclosingMethod attributes are real class
539 references. Set CONSTANT_LazyFlag here so that the VM
540 doesn't attempt to resolve them at class initialization
541 time. */
542 tree resolved_class, class_name;
543 resolved_class = get_class_constant (jcf, index);
544 class_name = build_internal_class_name (resolved_class);
545 index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
546 (unmangle_classname
547 (IDENTIFIER_POINTER(class_name),
548 IDENTIFIER_LENGTH(class_name))));
549 break;
551 case CONSTANT_Utf8:
553 tree utf8 = get_constant (jcf, index);
554 if (purpose == CONSTANT_Class)
555 /* Create a constant pool entry for a type signature. This
556 one has '.' rather than '/' because it isn't going into a
557 class file, it's going into a compiled object.
559 This has to match the logic in
560 _Jv_ClassReader::prepare_pool_entry(). */
561 utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
562 IDENTIFIER_LENGTH(utf8));
563 index = alloc_name_constant (kind, utf8);
565 break;
567 case CONSTANT_Long:
568 index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
569 targetm.words_big_endian ());
570 break;
572 case CONSTANT_Double:
573 index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
574 targetm.float_words_big_endian ());
575 break;
577 case CONSTANT_Float:
578 case CONSTANT_Integer:
579 index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
580 break;
582 case CONSTANT_NameAndType:
584 uint16 name = JPOOL_USHORT1 (jcf, index);
585 uint16 sig = JPOOL_USHORT2 (jcf, index);
586 uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
587 uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
588 jword new_index = (name_index << 16) | sig_index;
589 index = find_constant1 (cpool, kind, new_index);
591 break;
593 default:
594 abort ();
597 return index;
600 /* Read an element_value structure from an annotation in JCF. Return
601 the constant pool index for the resulting constant pool entry. */
603 static int
604 handle_element_value (JCF *jcf, int level)
606 uint8 tag = JCF_readu (jcf);
607 int index = 0;
609 annotation_write_byte (tag);
610 switch (tag)
612 case 'B':
613 case 'C':
614 case 'S':
615 case 'Z':
616 case 'I':
618 uint16 cindex = JCF_readu2 (jcf);
619 index = handle_constant (jcf, cindex,
620 CONSTANT_Integer);
621 annotation_write_short (index);
623 break;
624 case 'D':
626 uint16 cindex = JCF_readu2 (jcf);
627 index = handle_constant (jcf, cindex,
628 CONSTANT_Double);
629 annotation_write_short (index);
631 break;
632 case 'F':
634 uint16 cindex = JCF_readu2 (jcf);
635 index = handle_constant (jcf, cindex,
636 CONSTANT_Float);
637 annotation_write_short (index);
639 break;
640 case 'J':
642 uint16 cindex = JCF_readu2 (jcf);
643 index = handle_constant (jcf, cindex,
644 CONSTANT_Long);
645 annotation_write_short (index);
647 break;
648 case 's':
650 uint16 cindex = JCF_readu2 (jcf);
651 /* Despite what the JVM spec says, compilers generate a Utf8
652 constant here, not a String. */
653 index = handle_constant (jcf, cindex,
654 CONSTANT_Utf8);
655 annotation_write_short (index);
657 break;
659 case 'e':
661 uint16 type_name_index = JCF_readu2 (jcf);
662 uint16 const_name_index = JCF_readu2 (jcf);
663 index = handle_constant (jcf, type_name_index,
664 CONSTANT_Class);
665 annotation_write_short (index);
666 index = handle_constant (jcf, const_name_index,
667 CONSTANT_Utf8);
668 annotation_write_short (index);
670 break;
671 case 'c':
673 uint16 class_info_index = JCF_readu2 (jcf);
674 index = handle_constant (jcf, class_info_index,
675 CONSTANT_Class);
676 annotation_write_short (index);
678 break;
679 case '@':
681 handle_annotation (jcf, level + 1);
683 break;
684 case '[':
686 uint16 n_array_elts = JCF_readu2 (jcf);
687 annotation_write_short (n_array_elts);
688 while (n_array_elts--)
689 handle_element_value (jcf, level + 1);
691 break;
692 default:
693 abort();
694 break;
696 return index;
699 /* Read an annotation structure from JCF. Write it to the
700 reflection_data field of the outgoing class. */
702 static void
703 handle_annotation (JCF *jcf, int level)
705 uint16 type_index = JCF_readu2 (jcf);
706 uint16 npairs = JCF_readu2 (jcf);
707 int index = handle_constant (jcf, type_index,
708 CONSTANT_Class);
709 annotation_write_short (index);
710 annotation_write_short (npairs);
711 while (npairs--)
713 uint16 name_index = JCF_readu2 (jcf);
714 index = handle_constant (jcf, name_index,
715 CONSTANT_Utf8);
716 annotation_write_short (index);
717 handle_element_value (jcf, level + 2);
721 /* Read an annotation count from JCF, and write the following
722 annotations to the reflection_data field of the outgoing class. */
724 static void
725 handle_annotations (JCF *jcf, int level)
727 uint16 num = JCF_readu2 (jcf);
728 annotation_write_short (num);
729 while (num--)
730 handle_annotation (jcf, level);
733 /* As handle_annotations(), but perform a sanity check that we write
734 the same number of bytes that we were expecting. */
736 static void
737 handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
738 long length)
740 long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
742 handle_annotations (jcf, 0);
744 gcc_assert (old_datasize + length
745 == TYPE_REFLECTION_DATASIZE (current_class));
748 /* gcj permutes its fields array after generating annotation_data, so
749 we have to fixup field indexes for fields that have moved. Given
750 ARG, a VEC_int, fixup the field indexes in the reflection_data of
751 the outgoing class. We use field_offsets to tell us where the
752 fixups must go. */
754 void
755 rewrite_reflection_indexes (void *arg)
757 bitmap_iterator bi;
758 unsigned int offset;
759 vec<int> *map = (vec<int> *) arg;
760 unsigned char *data = TYPE_REFLECTION_DATA (current_class);
762 if (map)
764 EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
766 uint16 index = annotation_read_short (data + offset);
767 annotation_rewrite_short
768 ((*map)[index], data + offset);
773 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
774 reflection_data of the outgoing class. */
776 static void
777 handle_member_annotations (int member_index, JCF *jcf,
778 const unsigned char *name ATTRIBUTE_UNUSED,
779 long len, jv_attr_type member_type)
781 int new_len = len + 1;
782 annotation_write_byte (member_type);
783 if (member_type != JV_CLASS_ATTR)
784 new_len += 2;
785 annotation_write_int (new_len);
786 annotation_write_byte (JV_ANNOTATIONS_KIND);
787 if (member_type == JV_FIELD_ATTR)
788 bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
789 if (member_type != JV_CLASS_ATTR)
790 annotation_write_short (member_index);
791 handle_annotation_attribute (member_index, jcf, len);
794 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
795 to the reflection_data of the outgoing class. */
797 static void
798 handle_parameter_annotations (int member_index, JCF *jcf,
799 const unsigned char *name ATTRIBUTE_UNUSED,
800 long len, jv_attr_type member_type)
802 int new_len = len + 1;
803 uint8 num;
804 annotation_write_byte (member_type);
805 if (member_type != JV_CLASS_ATTR)
806 new_len += 2;
807 annotation_write_int (new_len);
808 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
809 if (member_type != JV_CLASS_ATTR)
810 annotation_write_short (member_index);
811 num = JCF_readu (jcf);
812 annotation_write_byte (num);
813 while (num--)
814 handle_annotations (jcf, 0);
818 /* Read the AnnotationDefault data from JCF and write them to the
819 reflection_data of the outgoing class. */
821 static void
822 handle_default_annotation (int member_index, JCF *jcf,
823 const unsigned char *name ATTRIBUTE_UNUSED,
824 long len, jv_attr_type member_type)
826 int new_len = len + 1;
827 annotation_write_byte (member_type);
828 if (member_type != JV_CLASS_ATTR)
829 new_len += 2;
830 annotation_write_int (new_len);
831 annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
832 if (member_type != JV_CLASS_ATTR)
833 annotation_write_short (member_index);
834 handle_element_value (jcf, 0);
837 /* As above, for the EnclosingMethod attribute. */
839 static void
840 handle_enclosingmethod_attribute (int member_index, JCF *jcf,
841 const unsigned char *name ATTRIBUTE_UNUSED,
842 long len, jv_attr_type member_type)
844 int new_len = len + 1;
845 uint16 index;
846 annotation_write_byte (member_type);
847 if (member_type != JV_CLASS_ATTR)
848 new_len += 2;
849 annotation_write_int (new_len);
850 annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
851 if (member_type != JV_CLASS_ATTR)
852 annotation_write_short (member_index);
854 index = JCF_readu2 (jcf);
855 index = handle_constant (jcf, index, CONSTANT_Class);
856 annotation_write_short (index);
858 index = JCF_readu2 (jcf);
859 index = handle_constant (jcf, index, CONSTANT_NameAndType);
860 annotation_write_short (index);
863 /* As above, for the Signature attribute. */
865 static void
866 handle_signature_attribute (int member_index, JCF *jcf,
867 const unsigned char *name ATTRIBUTE_UNUSED,
868 long len, jv_attr_type member_type)
870 int new_len = len + 1;
871 uint16 index;
872 annotation_write_byte (member_type);
873 if (member_type != JV_CLASS_ATTR)
874 new_len += 2;
875 annotation_write_int (new_len);
876 annotation_write_byte (JV_SIGNATURE_KIND);
877 if (member_type != JV_CLASS_ATTR)
878 annotation_write_short (member_index);
880 index = JCF_readu2 (jcf);
881 index = handle_constant (jcf, index, CONSTANT_Utf8);
882 annotation_write_short (index);
887 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
889 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
890 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
891 output_class = current_class = give_name_to_class (jcf, THIS); \
892 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
894 #define HANDLE_CLASS_INTERFACE(INDEX) \
895 add_interface (current_class, get_class_constant (jcf, INDEX))
897 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
898 { int sig_index = SIGNATURE; \
899 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
900 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
901 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
902 if ((ACCESS_FLAGS) & ACC_FINAL) \
903 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
906 #define HANDLE_END_FIELDS() \
907 (current_field = NULL_TREE)
909 #define HANDLE_CONSTANTVALUE(INDEX) \
910 { tree constant; int index = INDEX; \
911 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
912 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
913 constant = build_utf8_ref (name); \
915 else \
916 constant = get_constant (jcf, index); \
917 set_constant_value (current_field, constant); }
919 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
920 (current_method = add_method (current_class, ACCESS_FLAGS, \
921 get_name_constant (jcf, NAME), \
922 get_name_constant (jcf, SIGNATURE)), \
923 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
924 DECL_LINENUMBERS_OFFSET (current_method) = 0)
926 #define HANDLE_END_METHODS() \
927 { current_method = NULL_TREE; }
929 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
930 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
931 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
932 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
933 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
935 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
936 { int n = (COUNT); \
937 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
938 JCF_SKIP (jcf, n * 10); }
940 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
941 { int n = (COUNT); \
942 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
943 JCF_SKIP (jcf, n * 4); }
945 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
947 int n = COUNT; \
948 vec<tree, va_gc> *v; \
949 vec_alloc (v, n); \
950 gcc_assert (!DECL_FUNCTION_THROWS (current_method)); \
951 while (--n >= 0) \
953 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
954 v->quick_push (thrown_class); \
956 DECL_FUNCTION_THROWS (current_method) = v; \
959 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
961 /* Link seen inner classes to their outer context and register the
962 inner class to its outer context. They will be later loaded. */
963 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
964 handle_innerclass_attribute (COUNT, jcf, attribute_length)
966 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
968 /* Irrelevant decls should have been nullified by the END macros. \
969 DECL_ARTIFICIAL on fields is used for something else (See \
970 PUSH_FIELD in java-tree.h) */ \
971 if (current_method) \
972 DECL_ARTIFICIAL (current_method) = 1; \
973 else if (current_field) \
974 FIELD_SYNTHETIC (current_field) = 1; \
975 else \
976 TYPE_SYNTHETIC (current_class) = 1; \
979 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
981 if (current_class == object_type_node) \
982 jcf->right_zip = 1; \
985 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
987 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
990 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
992 JCF_SKIP(jcf, attribute_length); \
995 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
997 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
1000 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
1002 JCF_SKIP(jcf, attribute_length); \
1005 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
1007 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
1010 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
1012 handle_enclosingmethod_attribute (index, jcf, name_data, \
1013 attribute_length, attr_type); \
1016 #define HANDLE_SIGNATURE_ATTRIBUTE() \
1018 handle_signature_attribute (index, jcf, name_data, \
1019 attribute_length, attr_type); \
1022 #include "jcf-reader.c"
1024 tree
1025 parse_signature (JCF *jcf, int sig_index)
1027 gcc_assert (sig_index > 0
1028 && sig_index < JPOOL_SIZE (jcf)
1029 && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1031 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1032 JPOOL_UTF_LENGTH (jcf, sig_index));
1035 tree
1036 get_constant (JCF *jcf, int index)
1038 tree value;
1039 int tag;
1040 if (index <= 0 || index >= JPOOL_SIZE(jcf))
1041 goto bad;
1042 tag = JPOOL_TAG (jcf, index);
1043 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
1044 return jcf->cpool.data[index].t;
1045 switch (tag)
1047 case CONSTANT_Integer:
1049 jint num = JPOOL_INT(jcf, index);
1050 value = build_int_cst (int_type_node, num);
1051 break;
1053 case CONSTANT_Long:
1055 unsigned HOST_WIDE_INT num;
1057 num = JPOOL_UINT (jcf, index);
1058 wide_int val = wi::lshift (wide_int::from (num, 64, SIGNED), 32);
1059 num = JPOOL_UINT (jcf, index + 1);
1060 val |= num;
1062 value = wide_int_to_tree (long_type_node, val);
1063 break;
1066 case CONSTANT_Float:
1068 jint num = JPOOL_INT(jcf, index);
1069 long buf = num;
1070 REAL_VALUE_TYPE d;
1072 real_from_target_fmt (&d, &buf, &ieee_single_format);
1073 value = build_real (float_type_node, d);
1074 break;
1077 case CONSTANT_Double:
1079 long buf[2], lo, hi;
1080 REAL_VALUE_TYPE d;
1082 hi = JPOOL_UINT (jcf, index);
1083 lo = JPOOL_UINT (jcf, index+1);
1085 if (targetm.float_words_big_endian ())
1086 buf[0] = hi, buf[1] = lo;
1087 else
1088 buf[0] = lo, buf[1] = hi;
1090 real_from_target_fmt (&d, buf, &ieee_double_format);
1091 value = build_real (double_type_node, d);
1092 break;
1095 case CONSTANT_String:
1097 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
1098 const char *utf8_ptr = IDENTIFIER_POINTER (name);
1099 int utf8_len = IDENTIFIER_LENGTH (name);
1100 const unsigned char *utf8;
1101 int i;
1103 /* Check for a malformed Utf8 string. */
1104 utf8 = (const unsigned char *) utf8_ptr;
1105 i = utf8_len;
1106 while (i > 0)
1108 int char_len = UT8_CHAR_LENGTH (*utf8);
1109 if (char_len < 0 || char_len > 3 || char_len > i)
1110 fatal_error ("bad string constant");
1112 utf8 += char_len;
1113 i -= char_len;
1116 /* Allocate a new string value. */
1117 value = build_string (utf8_len, utf8_ptr);
1118 TREE_TYPE (value) = build_pointer_type (string_type_node);
1120 break;
1121 default:
1122 goto bad;
1124 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
1125 jcf->cpool.data[index].t = value;
1126 return value;
1127 bad:
1128 fatal_error ("bad value constant type %d, index %d",
1129 JPOOL_TAG (jcf, index), index);
1132 tree
1133 get_name_constant (JCF *jcf, int index)
1135 tree name = get_constant (jcf, index);
1136 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1137 return name;
1140 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1141 a non anonymous entry) is found, We augment the inner class list of
1142 the outer context with the newly resolved innerclass. */
1144 static void
1145 handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
1147 int c = count;
1149 annotation_write_byte (JV_CLASS_ATTR);
1150 annotation_write_int (attribute_length+1);
1151 annotation_write_byte (JV_INNER_CLASSES_KIND);
1152 annotation_write_short (count);
1154 while (c--)
1156 /* Read inner_class_info_index. This may be 0 */
1157 int icii = JCF_readu2 (jcf);
1158 /* Read outer_class_info_index. If the innerclasses attribute
1159 entry isn't a member (like an inner class) the value is 0. */
1160 int ocii = JCF_readu2 (jcf);
1161 /* Read inner_name_index. If the class we're dealing with is
1162 an anonymous class, it must be 0. */
1163 int ini = JCF_readu2 (jcf);
1164 /* Read the access flag. */
1165 int acc = JCF_readu2 (jcf);
1167 annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1168 annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1169 annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1170 annotation_write_short (acc);
1172 /* If icii is 0, don't try to read the class. */
1173 if (icii >= 0)
1175 tree klass = get_class_constant (jcf, icii);
1176 tree decl = TYPE_NAME (klass);
1177 /* Skip reading further if ocii is null */
1178 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1180 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1181 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
1182 set_class_decl_access_flags (acc, decl);
1183 DECL_CONTEXT (decl) = outer;
1184 DECL_INNER_CLASS_LIST (outer) =
1185 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1186 CLASS_COMPLETE_P (decl) = 1;
1192 static tree
1193 give_name_to_class (JCF *jcf, int i)
1195 gcc_assert (i > 0
1196 && i < JPOOL_SIZE (jcf)
1197 && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1200 tree package_name = NULL_TREE, tmp;
1201 tree this_class;
1202 int j = JPOOL_USHORT1 (jcf, i);
1203 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1204 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
1205 JPOOL_UTF_LENGTH (jcf, j));
1206 this_class = lookup_class (class_name);
1208 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
1209 const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
1210 linemap_add (line_table, LC_ENTER, false, sfname, 0);
1211 input_location = linemap_line_start (line_table, 0, 1);
1212 file_start_location = input_location;
1213 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1214 if (main_input_filename == NULL && jcf == main_jcf)
1215 main_input_filename = sfname;
1218 jcf->cpool.data[i].t = this_class;
1219 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1220 split_qualified_name (&package_name, &tmp,
1221 DECL_NAME (TYPE_NAME (this_class)));
1222 TYPE_PACKAGE (this_class) = package_name;
1223 return this_class;
1227 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1229 tree
1230 get_class_constant (JCF *jcf, int i)
1232 tree type;
1233 gcc_assert (i > 0
1234 && i < JPOOL_SIZE (jcf)
1235 && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
1237 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1239 int name_index = JPOOL_USHORT1 (jcf, i);
1240 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1241 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
1242 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
1244 if (name[0] == '[') /* Handle array "classes". */
1245 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
1246 else
1248 tree cname = unmangle_classname (name, nlength);
1249 type = lookup_class (cname);
1251 jcf->cpool.data[i].t = type;
1252 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1254 else
1255 type = jcf->cpool.data[i].t;
1256 return type;
1259 /* Read a class with the fully qualified-name NAME.
1260 Return 1 iff we read the requested file.
1261 (It is still possible we failed if the file did not
1262 define the class it is supposed to.) */
1265 read_class (tree name)
1267 JCF this_jcf, *jcf;
1268 tree icv, klass = NULL_TREE;
1269 tree save_current_class = current_class;
1270 tree save_output_class = output_class;
1271 location_t save_location = input_location;
1272 JCF *save_current_jcf = current_jcf;
1274 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
1276 klass = TREE_TYPE (icv);
1277 jcf = TYPE_JCF (klass);
1279 else
1280 jcf = NULL;
1282 if (jcf == NULL)
1284 const char* path_name;
1285 this_jcf.zipd = NULL;
1286 jcf = &this_jcf;
1288 path_name = find_class (IDENTIFIER_POINTER (name),
1289 IDENTIFIER_LENGTH (name),
1290 &this_jcf);
1291 if (path_name == 0)
1292 return 0;
1293 else
1294 free(CONST_CAST (char *, path_name));
1297 current_jcf = jcf;
1299 if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
1301 output_class = current_class = klass;
1302 if (JCF_SEEN_IN_ZIP (current_jcf))
1303 read_zip_member(current_jcf,
1304 current_jcf->zipd, current_jcf->zipd->zipf);
1305 jcf_parse (current_jcf);
1306 /* Parsing might change the class, in which case we have to
1307 put it back where we found it. */
1308 if (current_class != klass && icv != NULL_TREE)
1309 TREE_TYPE (icv) = current_class;
1310 klass = current_class;
1312 layout_class (klass);
1313 load_inner_classes (klass);
1315 output_class = save_output_class;
1316 current_class = save_current_class;
1317 input_location = save_location;
1318 current_jcf = save_current_jcf;
1319 return 1;
1322 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1323 called from the parser, otherwise it's a RECORD_TYPE node. If
1324 VERBOSE is 1, print error message on failure to load a class. */
1325 void
1326 load_class (tree class_or_name, int verbose)
1328 tree name, saved;
1329 int class_loaded = 0;
1330 tree class_decl = NULL_TREE;
1331 bool is_compiled_class = false;
1333 /* We've already failed, don't try again. */
1334 if (TREE_CODE (class_or_name) == RECORD_TYPE
1335 && TYPE_DUMMY (class_or_name))
1336 return;
1338 /* class_or_name can be the name of the class we want to load */
1339 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1340 name = class_or_name;
1341 /* In some cases, it's a dependency that we process earlier that
1342 we though */
1343 else if (TREE_CODE (class_or_name) == TREE_LIST)
1344 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1345 /* Or it's a type in the making */
1346 else
1347 name = DECL_NAME (TYPE_NAME (class_or_name));
1349 class_decl = IDENTIFIER_CLASS_VALUE (name);
1350 if (class_decl != NULL_TREE)
1352 tree type = TREE_TYPE (class_decl);
1353 is_compiled_class
1354 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1355 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1358 saved = name;
1360 /* If flag_verify_invocations is unset, we don't try to load a class
1361 unless we're looking for Object (which is fixed by the ABI) or
1362 it's a class that we're going to compile. */
1363 if (flag_verify_invocations
1364 || class_or_name == object_type_node
1365 || is_compiled_class
1366 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1368 while (1)
1370 const char *separator;
1372 /* We've already loaded it. */
1373 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1375 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1376 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1377 break;
1380 if (read_class (name))
1381 break;
1383 /* We failed loading name. Now consider that we might be looking
1384 for an inner class. */
1385 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1386 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
1387 name = get_identifier_with_length (IDENTIFIER_POINTER (name),
1388 (separator
1389 - IDENTIFIER_POINTER (name)));
1390 /* Otherwise, we failed, we bail. */
1391 else
1392 break;
1396 /* have we found the class we're looking for? */
1397 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1398 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1399 class_loaded = type && CLASS_PARSED_P (type);
1403 if (!class_loaded)
1405 if (flag_verify_invocations || ! flag_indirect_dispatch)
1407 if (verbose)
1408 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1410 else if (verbose)
1412 /* This is just a diagnostic during testing, not a real problem. */
1413 if (!quiet_flag)
1414 warning (0, "cannot find file for class %s",
1415 IDENTIFIER_POINTER (saved));
1417 /* Fake it. */
1418 if (TREE_CODE (class_or_name) == RECORD_TYPE)
1420 set_super_info (0, class_or_name, object_type_node, 0);
1421 TYPE_DUMMY (class_or_name) = 1;
1422 /* We won't be able to output any debug info for this class. */
1423 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1429 /* Parse the .class file JCF. */
1431 static void
1432 jcf_parse (JCF* jcf)
1434 int i, code;
1436 bitmap_clear (field_offsets);
1438 if (jcf_parse_preamble (jcf) != 0)
1439 fatal_error ("not a valid Java .class file");
1440 code = jcf_parse_constant_pool (jcf);
1441 if (code != 0)
1442 fatal_error ("error while parsing constant pool");
1443 code = verify_constant_pool (jcf);
1444 if (code > 0)
1445 fatal_error ("error in constant pool entry #%d\n", code);
1447 jcf_parse_class (jcf);
1448 if (main_class == NULL_TREE)
1449 main_class = current_class;
1450 if (! quiet_flag && TYPE_NAME (current_class))
1451 fprintf (stderr, " %s %s",
1452 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
1453 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
1454 if (CLASS_PARSED_P (current_class))
1456 /* FIXME - where was first time */
1457 fatal_error ("reading class %s for the second time from %s",
1458 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1459 jcf->filename);
1461 CLASS_PARSED_P (current_class) = 1;
1463 for (i = 1; i < JPOOL_SIZE(jcf); i++)
1465 switch (JPOOL_TAG (jcf, i))
1467 case CONSTANT_Class:
1468 get_class_constant (jcf, i);
1469 break;
1473 code = jcf_parse_fields (jcf);
1474 if (code != 0)
1475 fatal_error ("error while parsing fields");
1476 code = jcf_parse_methods (jcf);
1477 if (code != 0)
1478 fatal_error ("error while parsing methods");
1479 code = jcf_parse_final_attributes (jcf);
1480 if (code != 0)
1481 fatal_error ("error while parsing final attributes");
1483 if (TYPE_REFLECTION_DATA (current_class))
1484 annotation_write_byte (JV_DONE_ATTR);
1486 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1488 /* The fields of class_type_node are already in correct order. */
1489 if (current_class != class_type_node && current_class != object_type_node)
1490 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1492 if (current_class == object_type_node)
1493 layout_class_methods (object_type_node);
1494 else
1495 vec_safe_push (all_class_list, TYPE_NAME (current_class));
1498 /* If we came across inner classes, load them now. */
1499 static void
1500 load_inner_classes (tree cur_class)
1502 tree current;
1503 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
1504 current = TREE_CHAIN (current))
1506 tree name = DECL_NAME (TREE_PURPOSE (current));
1507 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1508 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1509 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
1510 load_class (name, 1);
1514 static void
1515 duplicate_class_warning (const char *filename)
1517 location_t warn_loc;
1518 linemap_add (line_table, LC_RENAME, 0, filename, 0);
1519 warn_loc = linemap_line_start (line_table, 0, 1);
1520 warning_at (warn_loc, 0, "duplicate class will only be compiled once");
1523 static void
1524 java_layout_seen_class_methods (void)
1526 unsigned start = 0;
1527 unsigned end = vec_safe_length (all_class_list);
1529 while (1)
1531 unsigned ix;
1532 unsigned new_length;
1534 for (ix = start; ix != end; ix++)
1536 tree decl = (*all_class_list)[ix];
1537 tree cls = TREE_TYPE (decl);
1539 input_location = DECL_SOURCE_LOCATION (decl);
1541 if (! CLASS_LOADED_P (cls))
1542 load_class (cls, 0);
1544 layout_class_methods (cls);
1547 /* Note that new classes might have been added while laying out
1548 methods, changing the value of all_class_list. */
1549 new_length = vec_safe_length (all_class_list);
1550 if (end != new_length)
1552 start = end;
1553 end = new_length;
1555 else
1556 break;
1560 static void
1561 parse_class_file (void)
1563 tree method;
1564 location_t save_location = input_location;
1566 java_layout_seen_class_methods ();
1568 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1570 /* Re-enter the current file. */
1571 expanded_location loc = expand_location (input_location);
1572 linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1574 file_start_location = input_location;
1575 (*debug_hooks->start_source_file) (LOCATION_LINE (input_location),
1576 LOCATION_FILE (input_location));
1578 java_mark_class_local (current_class);
1580 gen_indirect_dispatch_tables (current_class);
1582 for (method = TYPE_METHODS (current_class);
1583 method != NULL_TREE; method = DECL_CHAIN (method))
1585 JCF *jcf = current_jcf;
1587 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
1588 continue;
1590 if (METHOD_NATIVE (method))
1592 tree arg;
1593 int decl_max_locals;
1595 if (! flag_jni)
1596 continue;
1597 /* We need to compute the DECL_MAX_LOCALS. We need to take
1598 the wide types into account too. */
1599 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
1600 arg != end_params_node;
1601 arg = TREE_CHAIN (arg), decl_max_locals += 1)
1603 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1604 decl_max_locals += 1;
1606 DECL_MAX_LOCALS (method) = decl_max_locals;
1607 start_java_method (method);
1608 give_name_to_locals (jcf);
1609 *get_stmts () = build_jni_stub (method);
1610 end_java_method ();
1611 continue;
1614 if (DECL_CODE_OFFSET (method) == 0)
1616 current_function_decl = method;
1617 error ("missing Code attribute");
1618 continue;
1621 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1622 if (DECL_LINENUMBERS_OFFSET (method))
1624 int i;
1625 int min_line = 0;
1626 unsigned char *ptr;
1627 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1628 linenumber_count = i = JCF_readu2 (jcf);
1629 linenumber_table = ptr = jcf->read_ptr;
1631 for (ptr += 2; --i >= 0; ptr += 4)
1633 int line = GET_u2 (ptr);
1634 /* Set initial line of input_location to smallest
1635 * linenumber.
1636 * Needs to be set before init_function_start. */
1637 if (min_line == 0 || line < min_line)
1638 min_line = line;
1640 if (min_line != 0)
1641 input_location = linemap_line_start (line_table, min_line, 1);
1643 else
1645 linenumber_table = NULL;
1646 linenumber_count = 0;
1649 start_java_method (method);
1651 note_instructions (jcf, method);
1653 give_name_to_locals (jcf);
1655 /* Bump up start_label_pc_this_method so we get a unique label number
1656 and reset highest_label_pc_this_method. */
1657 if (highest_label_pc_this_method >= 0)
1659 /* We adjust to the next multiple of 1000. This is just a frill
1660 so the last 3 digits of the label number match the bytecode
1661 offset, which might make debugging marginally more convenient. */
1662 start_label_pc_this_method
1663 = ((((start_label_pc_this_method + highest_label_pc_this_method)
1664 / 1000)
1665 + 1)
1666 * 1000);
1667 highest_label_pc_this_method = -1;
1670 /* Convert bytecode to trees. */
1671 expand_byte_code (jcf, method);
1673 end_java_method ();
1676 finish_class ();
1678 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
1679 input_location = save_location;
1682 static vec<tree, va_gc> *predefined_filenames;
1684 void
1685 add_predefined_file (tree name)
1687 vec_safe_push (predefined_filenames, name);
1691 predefined_filename_p (tree node)
1693 unsigned ix;
1694 tree f;
1696 FOR_EACH_VEC_SAFE_ELT (predefined_filenames, ix, f)
1697 if (f == node)
1698 return 1;
1700 return 0;
1703 /* Generate a function that does all static initialization for this
1704 translation unit. */
1706 static void
1707 java_emit_static_constructor (void)
1709 tree body = NULL;
1711 emit_register_classes (&body);
1712 write_resource_constructor (&body);
1714 if (body)
1716 tree name = get_identifier ("_Jv_global_static_constructor");
1718 tree decl
1719 = build_decl (input_location, FUNCTION_DECL, name,
1720 build_function_type_list (void_type_node, NULL_TREE));
1722 tree resdecl = build_decl (input_location,
1723 RESULT_DECL, NULL_TREE, void_type_node);
1724 DECL_ARTIFICIAL (resdecl) = 1;
1725 DECL_RESULT (decl) = resdecl;
1726 current_function_decl = decl;
1727 allocate_struct_function (decl, false);
1729 TREE_STATIC (decl) = 1;
1730 TREE_USED (decl) = 1;
1731 DECL_ARTIFICIAL (decl) = 1;
1732 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1733 DECL_SAVED_TREE (decl) = body;
1734 DECL_UNINLINABLE (decl) = 1;
1736 DECL_INITIAL (decl) = make_node (BLOCK);
1737 TREE_USED (DECL_INITIAL (decl)) = 1;
1739 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1740 java_genericize (decl);
1741 cgraph_node::finalize_function (decl, false);
1746 void
1747 java_parse_file (void)
1749 int filename_count = 0;
1750 location_t save_location = input_location;
1751 char *file_list = NULL, *list, *next;
1752 tree node;
1753 FILE *finput = NULL;
1754 int in_quotes = 0;
1755 unsigned ix;
1757 bitmap_obstack_initialize (&bit_obstack);
1758 field_offsets = BITMAP_ALLOC (&bit_obstack);
1760 if (flag_filelist_file)
1762 int avail = 2000;
1763 finput = fopen (main_input_filename, "r");
1764 if (finput == NULL)
1765 fatal_error ("can%'t open %s: %m", LOCATION_FILE (input_location));
1766 list = XNEWVEC (char, avail);
1767 next = list;
1768 for (;;)
1770 int count;
1771 if (avail < 500)
1773 count = next - list;
1774 avail = 2 * (count + avail);
1775 list = XRESIZEVEC (char, list, avail);
1776 next = list + count;
1777 avail = avail - count;
1779 /* Subtract one to guarantee space for final '\0'. */
1780 count = fread (next, 1, avail - 1, finput);
1781 if (count == 0)
1783 if (! feof (finput))
1784 fatal_error ("error closing %s: %m",
1785 LOCATION_FILE (input_location));
1786 *next = '\0';
1787 break;
1789 avail -= count;
1790 next += count;
1792 fclose (finput);
1793 finput = NULL;
1794 file_list = list;
1796 else
1797 list = CONST_CAST (char *, main_input_filename);
1799 while (list)
1801 for (next = list; ; )
1803 char ch = *next;
1804 if (flag_filelist_file && ! in_quotes
1805 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1806 || ch == '&') /* FIXME */)
1808 if (next == list)
1810 next++;
1811 list = next;
1812 continue;
1814 else
1816 *next++ = '\0';
1817 break;
1820 if (flag_filelist_file && ch == '"')
1822 in_quotes = ! in_quotes;
1823 *next++ = '\0';
1824 if (in_quotes)
1825 list = next;
1826 else
1827 break;
1829 if (ch == '\0')
1831 next = NULL;
1832 break;
1834 next++;
1837 /* Exclude .java files. */
1838 if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1840 /* Nothing. */
1842 else if (list[0])
1844 node = get_identifier (list);
1846 filename_count++;
1848 /* Exclude file that we see twice on the command line. */
1850 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1851 duplicate_class_warning (IDENTIFIER_POINTER (node));
1852 else
1854 build_translation_unit_decl (node);
1855 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1858 list = next;
1861 free (file_list);
1863 if (filename_count == 0)
1864 warning (0, "no input file specified");
1866 if (resource_name)
1868 const char *resource_filename;
1870 /* Only one resource file may be compiled at a time. */
1871 gcc_assert (all_translation_units->length () == 1);
1873 resource_filename
1874 = IDENTIFIER_POINTER (DECL_NAME ((*all_translation_units)[0]));
1875 compile_resource_file (resource_name, resource_filename);
1877 goto finish;
1880 current_jcf = main_jcf;
1881 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1883 unsigned char magic_string[4];
1884 char *real_path;
1885 uint32 magic = 0;
1886 tree name = DECL_NAME (node);
1887 tree real_file;
1888 const char *filename = IDENTIFIER_POINTER (name);
1890 /* Skip already parsed files */
1891 real_path = lrealpath (filename);
1892 real_file = get_identifier (real_path);
1893 free (real_path);
1894 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1895 continue;
1897 /* Close previous descriptor, if any */
1898 if (finput && fclose (finput))
1899 fatal_error ("can%'t close input file %s: %m", main_input_filename);
1901 finput = fopen (filename, "rb");
1902 if (finput == NULL)
1903 fatal_error ("can%'t open %s: %m", filename);
1905 #ifdef IO_BUFFER_SIZE
1906 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1907 _IOFBF, IO_BUFFER_SIZE);
1908 #endif
1910 /* Figure what kind of file we're dealing with */
1911 if (fread (magic_string, 1, 4, finput) == 4)
1913 fseek (finput, 0L, SEEK_SET);
1914 magic = GET_u4 (magic_string);
1916 if (magic == 0xcafebabe)
1918 CLASS_FILE_P (node) = 1;
1919 current_jcf = ggc_cleared_alloc<JCF> ();
1920 current_jcf->read_state = finput;
1921 current_jcf->filbuf = jcf_filbuf_from_stdio;
1922 jcf_parse (current_jcf);
1923 DECL_SOURCE_LOCATION (node) = file_start_location;
1924 TYPE_JCF (current_class) = current_jcf;
1925 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1927 /* We've already compiled this class. */
1928 duplicate_class_warning (filename);
1929 continue;
1931 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1932 TREE_TYPE (node) = current_class;
1934 else if (magic == (JCF_u4)ZIPMAGIC)
1936 main_jcf = ggc_cleared_alloc<JCF> ();
1937 main_jcf->read_state = finput;
1938 main_jcf->filbuf = jcf_filbuf_from_stdio;
1939 linemap_add (line_table, LC_ENTER, false, filename, 0);
1940 input_location = linemap_line_start (line_table, 0, 1);
1941 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1942 fatal_error ("bad zip/jar file %s", filename);
1943 localToFile = SeenZipFiles;
1944 /* Register all the classes defined there. */
1945 process_zip_dir ((FILE *) main_jcf->read_state);
1946 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1947 parse_zip_file_entries ();
1949 else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1951 /* Ignore an empty input jar. */
1953 else
1955 gcc_unreachable ();
1956 #if 0
1957 java_push_parser_context ();
1958 java_parser_context_save_global ();
1960 parse_source_file_1 (real_file, filename, finput);
1961 java_parser_context_restore_global ();
1962 java_pop_parser_context (1);
1963 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1964 #endif
1968 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1970 input_location = DECL_SOURCE_LOCATION (node);
1971 if (CLASS_FILE_P (node))
1973 /* FIXME: These two flags really should be independent. We
1974 should be able to compile fully binary compatible, but
1975 with flag_verify_invocations on. */
1976 flag_verify_invocations = ! flag_indirect_dispatch;
1977 output_class = current_class = TREE_TYPE (node);
1979 current_jcf = TYPE_JCF (current_class);
1980 layout_class (current_class);
1981 load_inner_classes (current_class);
1982 parse_class_file ();
1983 JCF_FINISH (current_jcf);
1986 input_location = save_location;
1988 bitmap_obstack_release (&bit_obstack);
1990 finish:
1991 /* Arrange for any necessary initialization to happen. */
1992 java_emit_static_constructor ();
1993 gcc_assert (global_bindings_p ());
1997 /* Return the name of the class corresponding to the name of the file
1998 in this zip entry. The result is newly allocated using ALLOC. */
1999 static char *
2000 compute_class_name (struct ZipDirectory *zdir)
2002 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2003 char *class_name;
2004 int i;
2005 int filename_length = zdir->filename_length;
2007 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
2009 class_name_in_zip_dir += 2;
2010 filename_length -= 2;
2013 filename_length -= strlen (".class");
2014 class_name = XNEWVEC (char, filename_length + 1);
2015 memcpy (class_name, class_name_in_zip_dir, filename_length);
2016 class_name [filename_length] = '\0';
2018 for (i = 0; i < filename_length; i++)
2019 if (class_name[i] == '/')
2020 class_name[i] = '.';
2022 return class_name;
2025 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2026 if it is a property file of some sort. */
2027 static int
2028 classify_zip_file (struct ZipDirectory *zdir)
2030 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2032 if (zdir->filename_length > 6
2033 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2034 ".class", 6))
2035 return 1;
2037 /* For now we drop the manifest, but not other information. */
2038 if (zdir->filename_length == 20
2039 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
2040 return 0;
2042 /* Drop directory entries. */
2043 if (zdir->filename_length > 0
2044 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2045 return 0;
2047 return 2;
2050 /* Process all class entries found in the zip file. */
2051 static void
2052 parse_zip_file_entries (void)
2054 struct ZipDirectory *zdir;
2055 int i;
2057 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2058 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2060 tree klass;
2062 switch (classify_zip_file (zdir))
2064 case 0:
2065 continue;
2067 case 1:
2069 char *class_name = compute_class_name (zdir);
2070 int previous_alias_set = -1;
2071 klass = lookup_class (get_identifier (class_name));
2072 FREE (class_name);
2073 current_jcf = TYPE_JCF (klass);
2074 output_class = current_class = klass;
2076 /* This is a dummy class, and now we're compiling it for
2077 real. */
2078 gcc_assert (! TYPE_DUMMY (klass));
2080 /* This is for a corner case where we have a superclass
2081 but no superclass fields.
2083 This can happen if we earlier failed to lay out this
2084 class because its superclass was still in the process
2085 of being laid out; this occurs when we have recursive
2086 class dependencies via inner classes. We must record
2087 the previous alias set and restore it after laying out
2088 the class.
2090 FIXME: this really is a kludge. We should figure out a
2091 way to lay out the class properly before this
2092 happens. */
2093 if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2094 && integer_zerop (TYPE_SIZE (klass)))
2096 TYPE_SIZE (klass) = NULL_TREE;
2097 previous_alias_set = TYPE_ALIAS_SET (klass);
2098 TYPE_ALIAS_SET (klass) = -1;
2101 if (! CLASS_LOADED_P (klass))
2103 if (! CLASS_PARSED_P (klass))
2105 read_zip_member (current_jcf, zdir, localToFile);
2106 jcf_parse (current_jcf);
2108 layout_class (current_class);
2109 load_inner_classes (current_class);
2112 if (previous_alias_set != -1)
2113 TYPE_ALIAS_SET (klass) = previous_alias_set;
2115 if (TYPE_SIZE (current_class) != error_mark_node)
2117 parse_class_file ();
2118 free (current_jcf->buffer); /* No longer necessary */
2119 /* Note: there is a way to free this buffer right after a
2120 class seen in a zip file has been parsed. The idea is the
2121 set its jcf in such a way that buffer will be reallocated
2122 the time the code for the class will be generated. FIXME. */
2125 break;
2127 case 2:
2129 char *file_name, *class_name_in_zip_dir, *buffer;
2130 JCF *jcf;
2131 file_name = XNEWVEC (char, zdir->filename_length + 1);
2132 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2133 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2134 file_name[zdir->filename_length] = '\0';
2135 jcf = XNEW (JCF);
2136 JCF_ZERO (jcf);
2137 jcf->read_state = finput;
2138 jcf->filbuf = jcf_filbuf_from_stdio;
2139 jcf->classname = NULL;
2140 jcf->filename = file_name;
2141 jcf->zipd = zdir;
2143 if (read_zip_member (jcf, zdir, localToFile) < 0)
2144 fatal_error ("error while reading %s from zip file", file_name);
2146 buffer = XNEWVEC (char, zdir->filename_length + 1 +
2147 (jcf->buffer_end - jcf->buffer));
2148 strcpy (buffer, file_name);
2149 /* This is not a typo: we overwrite the trailing \0 of the
2150 file name; this is just how the data is laid out. */
2151 memcpy (buffer + zdir->filename_length,
2152 jcf->buffer, jcf->buffer_end - jcf->buffer);
2154 compile_resource_data (file_name, buffer,
2155 jcf->buffer_end - jcf->buffer);
2156 JCF_FINISH (jcf);
2157 free (jcf);
2158 free (buffer);
2160 break;
2162 default:
2163 gcc_unreachable ();
2168 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
2169 jcf up for further processing and link it to the created class. */
2171 static void
2172 process_zip_dir (FILE *finput)
2174 int i;
2175 ZipDirectory *zdir;
2177 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2178 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2180 char *class_name, *file_name, *class_name_in_zip_dir;
2181 tree klass;
2182 JCF *jcf;
2184 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2186 /* Here we skip non-class files; we handle them later. */
2187 if (classify_zip_file (zdir) != 1)
2188 continue;
2190 class_name = compute_class_name (zdir);
2191 file_name = XNEWVEC (char, zdir->filename_length+1);
2192 jcf = ggc_cleared_alloc<JCF> ();
2194 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2195 file_name [zdir->filename_length] = '\0';
2197 klass = lookup_class (get_identifier (class_name));
2199 if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2201 /* We've already compiled this class. */
2202 duplicate_class_warning (file_name);
2203 continue;
2205 /* This function is only called when processing a zip file seen
2206 on the command line. */
2207 CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
2209 jcf->read_state = finput;
2210 jcf->filbuf = jcf_filbuf_from_stdio;
2211 jcf->classname = class_name;
2212 jcf->filename = file_name;
2213 jcf->zipd = zdir;
2215 TYPE_JCF (klass) = jcf;
2219 #include "gt-java-jcf-parse.h"
2220 #include "gtype-java.h"