* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / java / jcf-parse.c
blob20d412b89787817c05932fa720ef5b321f123530
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 "symtab.h"
31 #include "options.h"
32 #include "tree.h"
33 #include "stringpool.h"
34 #include "obstack.h"
35 #include "flags.h"
36 #include "java-except.h"
37 #include "javaop.h"
38 #include "java-tree.h"
39 #include "diagnostic-core.h"
40 #include "parse.h"
41 #include "debug.h"
42 #include "tm.h"
43 #include "hard-reg-set.h"
44 #include "function.h"
45 #include "cgraph.h"
46 #include "bitmap.h"
47 #include "target.h"
48 #include "toplev.h"
50 #ifdef HAVE_LOCALE_H
51 #include <locale.h>
52 #endif
54 #ifdef HAVE_LANGINFO_CODESET
55 #include <langinfo.h>
56 #endif
58 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
59 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
60 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
61 #define JPOOL_UTF_DATA(JCF, INDEX) \
62 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
63 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
64 do { \
65 unsigned char save; unsigned char *text; \
66 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
67 text = (JCF)->read_ptr; \
68 save = text[LENGTH]; \
69 text[LENGTH] = 0; \
70 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
71 text[LENGTH] = save; \
72 JCF_SKIP (JCF, LENGTH); } while (0)
74 #include "jcf.h"
76 extern struct obstack temporary_obstack;
78 static GTY(()) tree parse_roots[2];
80 /* The FIELD_DECL for the current field. */
81 #define current_field parse_roots[0]
83 /* The METHOD_DECL for the current method. */
84 #define current_method parse_roots[1]
86 /* Line 0 in current file, if compiling from bytecode. */
87 static location_t file_start_location;
89 /* The Java archive that provides main_class; the main input file. */
90 static GTY(()) struct JCF * main_jcf;
92 /* A list of all the class DECLs seen so far. */
93 static GTY(()) vec<tree, va_gc> *all_class_list;
95 /* The number of source files passed to us by -fsource-filename and an
96 array of pointers to each name. Used by find_sourcefile(). */
97 static int num_files = 0;
98 static char **filenames;
100 static struct ZipFile *localToFile;
102 /* A map of byte offsets in the reflection data that are fields which
103 need renumbering. */
104 bitmap field_offsets;
105 bitmap_obstack bit_obstack;
107 /* Declarations of some functions used here. */
108 static void handle_innerclass_attribute (int count, JCF *, int len);
109 static tree give_name_to_class (JCF *jcf, int index);
110 static char *compute_class_name (struct ZipDirectory *zdir);
111 static int classify_zip_file (struct ZipDirectory *zdir);
112 static void parse_zip_file_entries (void);
113 static void process_zip_dir (FILE *);
114 static void parse_class_file (void);
115 static void handle_deprecated (void);
116 static void set_source_filename (JCF *, int);
117 static void jcf_parse (struct JCF*);
118 static void load_inner_classes (tree);
119 static void handle_annotation (JCF *jcf, int level);
120 static void java_layout_seen_class_methods (void);
122 /* Handle "Deprecated" attribute. */
123 static void
124 handle_deprecated (void)
126 if (current_field != NULL_TREE)
127 FIELD_DEPRECATED (current_field) = 1;
128 else if (current_method != NULL_TREE)
129 METHOD_DEPRECATED (current_method) = 1;
130 else if (current_class != NULL_TREE)
131 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
132 else
134 /* Shouldn't happen. */
135 gcc_unreachable ();
141 /* Reverse a string. */
142 static char *
143 reverse (const char *s)
145 if (s == NULL)
146 return NULL;
147 else
149 int len = strlen (s);
150 char *d = XNEWVAR (char, len + 1);
151 const char *sp;
152 char *dp;
154 d[len] = 0;
155 for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
156 *dp = *sp;
158 return d;
162 /* Compare two strings for qsort(). */
163 static int
164 cmpstringp (const void *p1, const void *p2)
166 /* The arguments to this function are "pointers to
167 pointers to char", but strcmp() arguments are "pointers
168 to char", hence the following cast plus dereference */
170 return strcmp(*(const char *const*) p1, *(const char *const*) p2);
173 /* Create an array of strings, one for each source file that we've
174 seen. fsource_filename can either be the name of a single .java
175 file or a file that contains a list of filenames separated by
176 newlines. */
177 void
178 java_read_sourcefilenames (const char *fsource_filename)
180 if (fsource_filename
181 && filenames == 0
182 && strlen (fsource_filename) > strlen (".java")
183 && filename_cmp ((fsource_filename
184 + strlen (fsource_filename)
185 - strlen (".java")),
186 ".java") != 0)
188 /* fsource_filename isn't a .java file but a list of filenames
189 separated by newlines */
190 FILE *finput = fopen (fsource_filename, "r");
191 int len = 0;
192 int longest_line = 0;
194 gcc_assert (finput);
196 /* Find out how many files there are, and how long the filenames are. */
197 while (! feof (finput))
199 int ch = getc (finput);
200 if (ch == '\n')
202 num_files++;
203 if (len > longest_line)
204 longest_line = len;
205 len = 0;
206 continue;
208 if (ch == EOF)
209 break;
210 len++;
213 rewind (finput);
215 /* Read the filenames. Put a pointer to each filename into the
216 array FILENAMES. */
218 char *linebuf = (char *) alloca (longest_line + 1);
219 int i = 0;
220 int charpos;
222 filenames = XNEWVEC (char *, num_files);
224 charpos = 0;
225 for (;;)
227 int ch = getc (finput);
228 if (ch == EOF)
229 break;
230 if (ch == '\n')
232 linebuf[charpos] = 0;
233 gcc_assert (i < num_files);
234 /* ??? Perhaps we should use lrealpath() here. Doing
235 so would tidy up things like /../ but the rest of
236 gcc seems to assume relative pathnames, not
237 absolute pathnames. */
238 /* realname = lrealpath (linebuf); */
239 filenames[i++] = reverse (linebuf);
240 charpos = 0;
241 continue;
243 gcc_assert (charpos < longest_line);
244 linebuf[charpos++] = ch;
247 if (num_files > 1)
248 qsort (filenames, num_files, sizeof (char *), cmpstringp);
250 fclose (finput);
252 else
254 filenames = XNEWVEC (char *, 1);
255 filenames[0] = reverse (fsource_filename);
256 num_files = 1;
260 /* Given a relative pathname such as foo/bar.java, attempt to find a
261 longer pathname with the same suffix.
263 This is a best guess heuristic; with some weird class hierarchies we
264 may fail to pick the correct source file. For example, if we have
265 the filenames foo/bar.java and also foo/foo/bar.java, we do not
266 have enough information to know which one is the right match for
267 foo/bar.java. */
269 static const char *
270 find_sourcefile (const char *name)
272 int i = 0, j = num_files-1;
273 char *found = NULL;
275 if (filenames)
277 char *revname = reverse (name);
281 int k = (i+j) / 2;
282 int cmp = strncmp (revname, filenames[k], strlen (revname));
283 if (cmp == 0)
285 /* OK, so we found one. But is it a unique match? */
286 if ((k > i
287 && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
288 || (k < j
289 && (strncmp (revname, filenames[k+1], strlen (revname))
290 == 0)))
292 else
293 found = filenames[k];
294 break;
296 if (cmp > 0)
297 i = k+1;
298 else
299 j = k-1;
301 while (i <= j);
303 free (revname);
306 if (found && strlen (found) > strlen (name))
307 return reverse (found);
308 else
309 return name;
314 /* Handle "SourceFile" attribute. */
316 static void
317 set_source_filename (JCF *jcf, int index)
319 tree sfname_id = get_name_constant (jcf, index);
320 const char *sfname = IDENTIFIER_POINTER (sfname_id);
321 const char *old_filename = LOCATION_FILE (input_location);
322 int new_len = IDENTIFIER_LENGTH (sfname_id);
323 if (old_filename != NULL)
325 int old_len = strlen (old_filename);
326 /* Use the filename from current input_location (derived from the
327 class name) if it has a directory prefix, but otherwise matches
328 sfname. */
329 if (old_len > new_len
330 && filename_cmp (sfname, old_filename + old_len - new_len) == 0
331 && (old_filename[old_len - new_len - 1] == '/'
332 || old_filename[old_len - new_len - 1] == '\\'))
333 return;
335 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
337 const char *class_name
338 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
339 const char *dot = strrchr (class_name, '.');
340 if (dot != NULL)
342 /* Length of prefix, not counting final dot. */
343 int i = dot - class_name;
344 /* Concatenate current package prefix with new sfname. */
345 char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
346 strcpy (buf + i + 1, sfname);
347 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
348 Note we start at the end with the final package dot. */
349 for (; i >= 0; i--)
351 char c = class_name[i];
352 if (c == '.')
353 c = DIR_SEPARATOR;
354 buf[i] = c;
356 sfname_id = get_identifier (buf);
357 free (buf);
358 sfname = IDENTIFIER_POINTER (sfname_id);
362 sfname = find_sourcefile (sfname);
363 LINEMAPS_LAST_ORDINARY_MAP (line_table)->to_file = sfname;
364 if (current_class == main_class) main_input_filename = sfname;
370 /* Annotation handling.
372 The technique we use here is to copy the annotation data directly
373 from the input class file into the output file. We don't decode the
374 data at all, merely rewriting constant indexes whenever we come
375 across them: this is necessary because the constant pool in the
376 output file isn't the same as the constant pool in the input.
378 The main advantage of this technique is that the resulting
379 annotation data is pointer-free, so it doesn't have to be relocated
380 at startup time. As a consequence of this, annotations have no
381 performance impact unless they are used. Also, this representation
382 is very dense. */
385 /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
386 the start of the newly allocated region. */
388 static unsigned char*
389 annotation_grow (int delta)
391 unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
392 long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
393 long len = *datasize;
395 if (*data == NULL)
397 *data = XNEWVAR (unsigned char, delta);
399 else
401 int newlen = *datasize + delta;
402 if (floor_log2 (newlen) != floor_log2 (*datasize))
403 *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
405 *datasize += delta;
406 return *data + len;
409 /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
410 byte order (i.e. big endian.) */
412 static void
413 annotation_rewrite_byte (unsigned int n, unsigned char *p)
415 p[0] = n;
418 static void
419 annotation_rewrite_short (unsigned int n, unsigned char *p)
421 p[0] = n>>8;
422 p[1] = n;
425 static void
426 annotation_rewrite_int (unsigned int n, unsigned char *p)
428 p[0] = n>>24;
429 p[1] = n>>16;
430 p[2] = n>>8;
431 p[3] = n;
434 /* Read a 16-bit unsigned int in Java byte order (i.e. big
435 endian.) */
437 static uint16
438 annotation_read_short (unsigned char *p)
440 uint16 tmp = p[0];
441 tmp = (tmp << 8) | p[1];
442 return tmp;
445 /* annotation_write_TYPE. Rewrite various int types, appending them
446 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
447 endian.) */
449 static void
450 annotation_write_byte (unsigned int n)
452 annotation_rewrite_byte (n, annotation_grow (1));
455 static void
456 annotation_write_short (unsigned int n)
458 annotation_rewrite_short (n, annotation_grow (2));
461 static void
462 annotation_write_int (unsigned int n)
464 annotation_rewrite_int (n, annotation_grow (4));
467 /* Create a 64-bit constant in the constant pool.
469 This is used for both integer and floating-point types. As a
470 consequence, it will not work if the target floating-point format
471 is anything other than IEEE-754. While this is arguably a bug, the
472 runtime library makes exactly the same assumption and it's unlikely
473 that Java will ever run on a non-IEEE machine. */
475 static int
476 handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
477 int index, bool big_endian)
479 /* If we're on a 64-bit platform we can fit a long or double
480 into the same space as a jword. */
481 if (POINTER_SIZE >= 64)
482 index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
484 /* In a compiled program the constant pool is in native word
485 order. How weird is that??? */
486 else if (big_endian)
487 index = find_constant2 (cpool, kind,
488 JPOOL_INT (jcf, index),
489 JPOOL_INT (jcf, index+1));
490 else
491 index = find_constant2 (cpool, kind,
492 JPOOL_INT (jcf, index+1),
493 JPOOL_INT (jcf, index));
495 return index;
498 /* Given a class file and an index into its constant pool, create an
499 entry in the outgoing constant pool for the same item. */
501 static uint16
502 handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
504 unsigned int kind;
505 CPool *cpool = cpool_for_class (output_class);
507 if (index == 0)
508 return 0;
510 if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
511 error ("<constant pool index %d not in range>", index);
513 kind = JPOOL_TAG (jcf, index);
515 if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
517 if (purpose == CONSTANT_Class
518 && kind == CONSTANT_Utf8)
520 else
521 error ("<constant pool index %d unexpected type", index);
524 switch (kind)
526 case CONSTANT_Class:
527 case CONSTANT_ResolvedClass:
529 /* For some reason I know not the what of, class names in
530 annotations are UTF-8 strings in the constant pool but
531 class names in EnclosingMethod attributes are real class
532 references. Set CONSTANT_LazyFlag here so that the VM
533 doesn't attempt to resolve them at class initialization
534 time. */
535 tree resolved_class, class_name;
536 resolved_class = get_class_constant (jcf, index);
537 class_name = build_internal_class_name (resolved_class);
538 index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
539 (unmangle_classname
540 (IDENTIFIER_POINTER(class_name),
541 IDENTIFIER_LENGTH(class_name))));
542 break;
544 case CONSTANT_Utf8:
546 tree utf8 = get_constant (jcf, index);
547 if (purpose == CONSTANT_Class)
548 /* Create a constant pool entry for a type signature. This
549 one has '.' rather than '/' because it isn't going into a
550 class file, it's going into a compiled object.
552 This has to match the logic in
553 _Jv_ClassReader::prepare_pool_entry(). */
554 utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
555 IDENTIFIER_LENGTH(utf8));
556 index = alloc_name_constant (kind, utf8);
558 break;
560 case CONSTANT_Long:
561 index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
562 targetm.words_big_endian ());
563 break;
565 case CONSTANT_Double:
566 index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
567 targetm.float_words_big_endian ());
568 break;
570 case CONSTANT_Float:
571 case CONSTANT_Integer:
572 index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
573 break;
575 case CONSTANT_NameAndType:
577 uint16 name = JPOOL_USHORT1 (jcf, index);
578 uint16 sig = JPOOL_USHORT2 (jcf, index);
579 uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
580 uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
581 jword new_index = (name_index << 16) | sig_index;
582 index = find_constant1 (cpool, kind, new_index);
584 break;
586 default:
587 abort ();
590 return index;
593 /* Read an element_value structure from an annotation in JCF. Return
594 the constant pool index for the resulting constant pool entry. */
596 static int
597 handle_element_value (JCF *jcf, int level)
599 uint8 tag = JCF_readu (jcf);
600 int index = 0;
602 annotation_write_byte (tag);
603 switch (tag)
605 case 'B':
606 case 'C':
607 case 'S':
608 case 'Z':
609 case 'I':
611 uint16 cindex = JCF_readu2 (jcf);
612 index = handle_constant (jcf, cindex,
613 CONSTANT_Integer);
614 annotation_write_short (index);
616 break;
617 case 'D':
619 uint16 cindex = JCF_readu2 (jcf);
620 index = handle_constant (jcf, cindex,
621 CONSTANT_Double);
622 annotation_write_short (index);
624 break;
625 case 'F':
627 uint16 cindex = JCF_readu2 (jcf);
628 index = handle_constant (jcf, cindex,
629 CONSTANT_Float);
630 annotation_write_short (index);
632 break;
633 case 'J':
635 uint16 cindex = JCF_readu2 (jcf);
636 index = handle_constant (jcf, cindex,
637 CONSTANT_Long);
638 annotation_write_short (index);
640 break;
641 case 's':
643 uint16 cindex = JCF_readu2 (jcf);
644 /* Despite what the JVM spec says, compilers generate a Utf8
645 constant here, not a String. */
646 index = handle_constant (jcf, cindex,
647 CONSTANT_Utf8);
648 annotation_write_short (index);
650 break;
652 case 'e':
654 uint16 type_name_index = JCF_readu2 (jcf);
655 uint16 const_name_index = JCF_readu2 (jcf);
656 index = handle_constant (jcf, type_name_index,
657 CONSTANT_Class);
658 annotation_write_short (index);
659 index = handle_constant (jcf, const_name_index,
660 CONSTANT_Utf8);
661 annotation_write_short (index);
663 break;
664 case 'c':
666 uint16 class_info_index = JCF_readu2 (jcf);
667 index = handle_constant (jcf, class_info_index,
668 CONSTANT_Class);
669 annotation_write_short (index);
671 break;
672 case '@':
674 handle_annotation (jcf, level + 1);
676 break;
677 case '[':
679 uint16 n_array_elts = JCF_readu2 (jcf);
680 annotation_write_short (n_array_elts);
681 while (n_array_elts--)
682 handle_element_value (jcf, level + 1);
684 break;
685 default:
686 abort();
687 break;
689 return index;
692 /* Read an annotation structure from JCF. Write it to the
693 reflection_data field of the outgoing class. */
695 static void
696 handle_annotation (JCF *jcf, int level)
698 uint16 type_index = JCF_readu2 (jcf);
699 uint16 npairs = JCF_readu2 (jcf);
700 int index = handle_constant (jcf, type_index,
701 CONSTANT_Class);
702 annotation_write_short (index);
703 annotation_write_short (npairs);
704 while (npairs--)
706 uint16 name_index = JCF_readu2 (jcf);
707 index = handle_constant (jcf, name_index,
708 CONSTANT_Utf8);
709 annotation_write_short (index);
710 handle_element_value (jcf, level + 2);
714 /* Read an annotation count from JCF, and write the following
715 annotations to the reflection_data field of the outgoing class. */
717 static void
718 handle_annotations (JCF *jcf, int level)
720 uint16 num = JCF_readu2 (jcf);
721 annotation_write_short (num);
722 while (num--)
723 handle_annotation (jcf, level);
726 /* As handle_annotations(), but perform a sanity check that we write
727 the same number of bytes that we were expecting. */
729 static void
730 handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
731 long length)
733 long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
735 handle_annotations (jcf, 0);
737 gcc_assert (old_datasize + length
738 == TYPE_REFLECTION_DATASIZE (current_class));
741 /* gcj permutes its fields array after generating annotation_data, so
742 we have to fixup field indexes for fields that have moved. Given
743 ARG, a VEC_int, fixup the field indexes in the reflection_data of
744 the outgoing class. We use field_offsets to tell us where the
745 fixups must go. */
747 void
748 rewrite_reflection_indexes (void *arg)
750 bitmap_iterator bi;
751 unsigned int offset;
752 vec<int> *map = (vec<int> *) arg;
753 unsigned char *data = TYPE_REFLECTION_DATA (current_class);
755 if (map)
757 EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
759 uint16 index = annotation_read_short (data + offset);
760 annotation_rewrite_short
761 ((*map)[index], data + offset);
766 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
767 reflection_data of the outgoing class. */
769 static void
770 handle_member_annotations (int member_index, JCF *jcf,
771 const unsigned char *name ATTRIBUTE_UNUSED,
772 long len, jv_attr_type member_type)
774 int new_len = len + 1;
775 annotation_write_byte (member_type);
776 if (member_type != JV_CLASS_ATTR)
777 new_len += 2;
778 annotation_write_int (new_len);
779 annotation_write_byte (JV_ANNOTATIONS_KIND);
780 if (member_type == JV_FIELD_ATTR)
781 bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
782 if (member_type != JV_CLASS_ATTR)
783 annotation_write_short (member_index);
784 handle_annotation_attribute (member_index, jcf, len);
787 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
788 to the reflection_data of the outgoing class. */
790 static void
791 handle_parameter_annotations (int member_index, JCF *jcf,
792 const unsigned char *name ATTRIBUTE_UNUSED,
793 long len, jv_attr_type member_type)
795 int new_len = len + 1;
796 uint8 num;
797 annotation_write_byte (member_type);
798 if (member_type != JV_CLASS_ATTR)
799 new_len += 2;
800 annotation_write_int (new_len);
801 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
802 if (member_type != JV_CLASS_ATTR)
803 annotation_write_short (member_index);
804 num = JCF_readu (jcf);
805 annotation_write_byte (num);
806 while (num--)
807 handle_annotations (jcf, 0);
811 /* Read the AnnotationDefault data from JCF and write them to the
812 reflection_data of the outgoing class. */
814 static void
815 handle_default_annotation (int member_index, JCF *jcf,
816 const unsigned char *name ATTRIBUTE_UNUSED,
817 long len, jv_attr_type member_type)
819 int new_len = len + 1;
820 annotation_write_byte (member_type);
821 if (member_type != JV_CLASS_ATTR)
822 new_len += 2;
823 annotation_write_int (new_len);
824 annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
825 if (member_type != JV_CLASS_ATTR)
826 annotation_write_short (member_index);
827 handle_element_value (jcf, 0);
830 /* As above, for the EnclosingMethod attribute. */
832 static void
833 handle_enclosingmethod_attribute (int member_index, JCF *jcf,
834 const unsigned char *name ATTRIBUTE_UNUSED,
835 long len, jv_attr_type member_type)
837 int new_len = len + 1;
838 uint16 index;
839 annotation_write_byte (member_type);
840 if (member_type != JV_CLASS_ATTR)
841 new_len += 2;
842 annotation_write_int (new_len);
843 annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
844 if (member_type != JV_CLASS_ATTR)
845 annotation_write_short (member_index);
847 index = JCF_readu2 (jcf);
848 index = handle_constant (jcf, index, CONSTANT_Class);
849 annotation_write_short (index);
851 index = JCF_readu2 (jcf);
852 index = handle_constant (jcf, index, CONSTANT_NameAndType);
853 annotation_write_short (index);
856 /* As above, for the Signature attribute. */
858 static void
859 handle_signature_attribute (int member_index, JCF *jcf,
860 const unsigned char *name ATTRIBUTE_UNUSED,
861 long len, jv_attr_type member_type)
863 int new_len = len + 1;
864 uint16 index;
865 annotation_write_byte (member_type);
866 if (member_type != JV_CLASS_ATTR)
867 new_len += 2;
868 annotation_write_int (new_len);
869 annotation_write_byte (JV_SIGNATURE_KIND);
870 if (member_type != JV_CLASS_ATTR)
871 annotation_write_short (member_index);
873 index = JCF_readu2 (jcf);
874 index = handle_constant (jcf, index, CONSTANT_Utf8);
875 annotation_write_short (index);
880 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
882 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
883 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
884 output_class = current_class = give_name_to_class (jcf, THIS); \
885 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
887 #define HANDLE_CLASS_INTERFACE(INDEX) \
888 add_interface (current_class, get_class_constant (jcf, INDEX))
890 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
891 { int sig_index = SIGNATURE; \
892 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
893 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
894 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
895 if ((ACCESS_FLAGS) & ACC_FINAL) \
896 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
899 #define HANDLE_END_FIELDS() \
900 (current_field = NULL_TREE)
902 #define HANDLE_CONSTANTVALUE(INDEX) \
903 { tree constant; int index = INDEX; \
904 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
905 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
906 constant = build_utf8_ref (name); \
908 else \
909 constant = get_constant (jcf, index); \
910 set_constant_value (current_field, constant); }
912 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
913 (current_method = add_method (current_class, ACCESS_FLAGS, \
914 get_name_constant (jcf, NAME), \
915 get_name_constant (jcf, SIGNATURE)), \
916 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
917 DECL_LINENUMBERS_OFFSET (current_method) = 0)
919 #define HANDLE_END_METHODS() \
920 { current_method = NULL_TREE; }
922 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
923 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
924 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
925 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
926 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
928 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
929 { int n = (COUNT); \
930 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
931 JCF_SKIP (jcf, n * 10); }
933 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
934 { int n = (COUNT); \
935 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
936 JCF_SKIP (jcf, n * 4); }
938 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
940 int n = COUNT; \
941 vec<tree, va_gc> *v; \
942 vec_alloc (v, n); \
943 gcc_assert (!DECL_FUNCTION_THROWS (current_method)); \
944 while (--n >= 0) \
946 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
947 v->quick_push (thrown_class); \
949 DECL_FUNCTION_THROWS (current_method) = v; \
952 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
954 /* Link seen inner classes to their outer context and register the
955 inner class to its outer context. They will be later loaded. */
956 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
957 handle_innerclass_attribute (COUNT, jcf, attribute_length)
959 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
961 /* Irrelevant decls should have been nullified by the END macros. \
962 DECL_ARTIFICIAL on fields is used for something else (See \
963 PUSH_FIELD in java-tree.h) */ \
964 if (current_method) \
965 DECL_ARTIFICIAL (current_method) = 1; \
966 else if (current_field) \
967 FIELD_SYNTHETIC (current_field) = 1; \
968 else \
969 TYPE_SYNTHETIC (current_class) = 1; \
972 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
974 if (current_class == object_type_node) \
975 jcf->right_zip = 1; \
978 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
980 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
983 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
985 JCF_SKIP(jcf, attribute_length); \
988 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
990 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
993 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
995 JCF_SKIP(jcf, attribute_length); \
998 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
1000 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
1003 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
1005 handle_enclosingmethod_attribute (index, jcf, name_data, \
1006 attribute_length, attr_type); \
1009 #define HANDLE_SIGNATURE_ATTRIBUTE() \
1011 handle_signature_attribute (index, jcf, name_data, \
1012 attribute_length, attr_type); \
1015 #include "jcf-reader.c"
1017 tree
1018 parse_signature (JCF *jcf, int sig_index)
1020 gcc_assert (sig_index > 0
1021 && sig_index < JPOOL_SIZE (jcf)
1022 && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1024 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1025 JPOOL_UTF_LENGTH (jcf, sig_index));
1028 tree
1029 get_constant (JCF *jcf, int index)
1031 tree value;
1032 int tag;
1033 if (index <= 0 || index >= JPOOL_SIZE(jcf))
1034 goto bad;
1035 tag = JPOOL_TAG (jcf, index);
1036 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
1037 return jcf->cpool.data[index].t;
1038 switch (tag)
1040 case CONSTANT_Integer:
1042 jint num = JPOOL_INT(jcf, index);
1043 value = build_int_cst (int_type_node, num);
1044 break;
1046 case CONSTANT_Long:
1048 unsigned HOST_WIDE_INT num;
1050 num = JPOOL_UINT (jcf, index);
1051 wide_int val = wi::lshift (wide_int::from (num, 64, SIGNED), 32);
1052 num = JPOOL_UINT (jcf, index + 1);
1053 val |= num;
1055 value = wide_int_to_tree (long_type_node, val);
1056 break;
1059 case CONSTANT_Float:
1061 jint num = JPOOL_INT(jcf, index);
1062 long buf = num;
1063 REAL_VALUE_TYPE d;
1065 real_from_target_fmt (&d, &buf, &ieee_single_format);
1066 value = build_real (float_type_node, d);
1067 break;
1070 case CONSTANT_Double:
1072 long buf[2], lo, hi;
1073 REAL_VALUE_TYPE d;
1075 hi = JPOOL_UINT (jcf, index);
1076 lo = JPOOL_UINT (jcf, index+1);
1078 if (targetm.float_words_big_endian ())
1079 buf[0] = hi, buf[1] = lo;
1080 else
1081 buf[0] = lo, buf[1] = hi;
1083 real_from_target_fmt (&d, buf, &ieee_double_format);
1084 value = build_real (double_type_node, d);
1085 break;
1088 case CONSTANT_String:
1090 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
1091 const char *utf8_ptr = IDENTIFIER_POINTER (name);
1092 int utf8_len = IDENTIFIER_LENGTH (name);
1093 const unsigned char *utf8;
1094 int i;
1096 /* Check for a malformed Utf8 string. */
1097 utf8 = (const unsigned char *) utf8_ptr;
1098 i = utf8_len;
1099 while (i > 0)
1101 int char_len = UT8_CHAR_LENGTH (*utf8);
1102 if (char_len < 0 || char_len > 3 || char_len > i)
1103 fatal_error (input_location, "bad string constant");
1105 utf8 += char_len;
1106 i -= char_len;
1109 /* Allocate a new string value. */
1110 value = build_string (utf8_len, utf8_ptr);
1111 TREE_TYPE (value) = build_pointer_type (string_type_node);
1113 break;
1114 default:
1115 goto bad;
1117 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
1118 jcf->cpool.data[index].t = value;
1119 return value;
1120 bad:
1121 fatal_error (input_location, "bad value constant type %d, index %d",
1122 JPOOL_TAG (jcf, index), index);
1125 tree
1126 get_name_constant (JCF *jcf, int index)
1128 tree name = get_constant (jcf, index);
1129 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1130 return name;
1133 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1134 a non anonymous entry) is found, We augment the inner class list of
1135 the outer context with the newly resolved innerclass. */
1137 static void
1138 handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
1140 int c = count;
1142 annotation_write_byte (JV_CLASS_ATTR);
1143 annotation_write_int (attribute_length+1);
1144 annotation_write_byte (JV_INNER_CLASSES_KIND);
1145 annotation_write_short (count);
1147 while (c--)
1149 /* Read inner_class_info_index. This may be 0 */
1150 int icii = JCF_readu2 (jcf);
1151 /* Read outer_class_info_index. If the innerclasses attribute
1152 entry isn't a member (like an inner class) the value is 0. */
1153 int ocii = JCF_readu2 (jcf);
1154 /* Read inner_name_index. If the class we're dealing with is
1155 an anonymous class, it must be 0. */
1156 int ini = JCF_readu2 (jcf);
1157 /* Read the access flag. */
1158 int acc = JCF_readu2 (jcf);
1160 annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1161 annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1162 annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1163 annotation_write_short (acc);
1165 /* If icii is 0, don't try to read the class. */
1166 if (icii >= 0)
1168 tree klass = get_class_constant (jcf, icii);
1169 tree decl = TYPE_NAME (klass);
1170 /* Skip reading further if ocii is null */
1171 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1173 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1174 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
1175 set_class_decl_access_flags (acc, decl);
1176 DECL_CONTEXT (decl) = outer;
1177 DECL_INNER_CLASS_LIST (outer) =
1178 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1179 CLASS_COMPLETE_P (decl) = 1;
1185 static tree
1186 give_name_to_class (JCF *jcf, int i)
1188 gcc_assert (i > 0
1189 && i < JPOOL_SIZE (jcf)
1190 && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1193 tree package_name = NULL_TREE, tmp;
1194 tree this_class;
1195 int j = JPOOL_USHORT1 (jcf, i);
1196 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1197 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
1198 JPOOL_UTF_LENGTH (jcf, j));
1199 this_class = lookup_class (class_name);
1201 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
1202 const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
1203 linemap_add (line_table, LC_ENTER, false, sfname, 0);
1204 input_location = linemap_line_start (line_table, 0, 1);
1205 file_start_location = input_location;
1206 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1207 if (main_input_filename == NULL && jcf == main_jcf)
1208 main_input_filename = sfname;
1211 jcf->cpool.data[i].t = this_class;
1212 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1213 split_qualified_name (&package_name, &tmp,
1214 DECL_NAME (TYPE_NAME (this_class)));
1215 TYPE_PACKAGE (this_class) = package_name;
1216 return this_class;
1220 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1222 tree
1223 get_class_constant (JCF *jcf, int i)
1225 tree type;
1226 gcc_assert (i > 0
1227 && i < JPOOL_SIZE (jcf)
1228 && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
1230 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1232 int name_index = JPOOL_USHORT1 (jcf, i);
1233 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1234 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
1235 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
1237 if (name[0] == '[') /* Handle array "classes". */
1238 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
1239 else
1241 tree cname = unmangle_classname (name, nlength);
1242 type = lookup_class (cname);
1244 jcf->cpool.data[i].t = type;
1245 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1247 else
1248 type = jcf->cpool.data[i].t;
1249 return type;
1252 /* Read a class with the fully qualified-name NAME.
1253 Return 1 iff we read the requested file.
1254 (It is still possible we failed if the file did not
1255 define the class it is supposed to.) */
1258 read_class (tree name)
1260 JCF this_jcf, *jcf;
1261 tree icv, klass = NULL_TREE;
1262 tree save_current_class = current_class;
1263 tree save_output_class = output_class;
1264 location_t save_location = input_location;
1265 JCF *save_current_jcf = current_jcf;
1267 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
1269 klass = TREE_TYPE (icv);
1270 jcf = TYPE_JCF (klass);
1272 else
1273 jcf = NULL;
1275 if (jcf == NULL)
1277 const char* path_name;
1278 this_jcf.zipd = NULL;
1279 jcf = &this_jcf;
1281 path_name = find_class (IDENTIFIER_POINTER (name),
1282 IDENTIFIER_LENGTH (name),
1283 &this_jcf);
1284 if (path_name == 0)
1285 return 0;
1286 else
1287 free(CONST_CAST (char *, path_name));
1290 current_jcf = jcf;
1292 if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
1294 output_class = current_class = klass;
1295 if (JCF_SEEN_IN_ZIP (current_jcf))
1296 read_zip_member(current_jcf,
1297 current_jcf->zipd, current_jcf->zipd->zipf);
1298 jcf_parse (current_jcf);
1299 /* Parsing might change the class, in which case we have to
1300 put it back where we found it. */
1301 if (current_class != klass && icv != NULL_TREE)
1302 TREE_TYPE (icv) = current_class;
1303 klass = current_class;
1305 layout_class (klass);
1306 load_inner_classes (klass);
1308 output_class = save_output_class;
1309 current_class = save_current_class;
1310 input_location = save_location;
1311 current_jcf = save_current_jcf;
1312 return 1;
1315 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1316 called from the parser, otherwise it's a RECORD_TYPE node. If
1317 VERBOSE is 1, print error message on failure to load a class. */
1318 void
1319 load_class (tree class_or_name, int verbose)
1321 tree name, saved;
1322 int class_loaded = 0;
1323 tree class_decl = NULL_TREE;
1324 bool is_compiled_class = false;
1326 /* We've already failed, don't try again. */
1327 if (TREE_CODE (class_or_name) == RECORD_TYPE
1328 && TYPE_DUMMY (class_or_name))
1329 return;
1331 /* class_or_name can be the name of the class we want to load */
1332 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1333 name = class_or_name;
1334 /* In some cases, it's a dependency that we process earlier that
1335 we though */
1336 else if (TREE_CODE (class_or_name) == TREE_LIST)
1337 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1338 /* Or it's a type in the making */
1339 else
1340 name = DECL_NAME (TYPE_NAME (class_or_name));
1342 class_decl = IDENTIFIER_CLASS_VALUE (name);
1343 if (class_decl != NULL_TREE)
1345 tree type = TREE_TYPE (class_decl);
1346 is_compiled_class
1347 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1348 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1351 saved = name;
1353 /* If flag_verify_invocations is unset, we don't try to load a class
1354 unless we're looking for Object (which is fixed by the ABI) or
1355 it's a class that we're going to compile. */
1356 if (flag_verify_invocations
1357 || class_or_name == object_type_node
1358 || is_compiled_class
1359 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1361 while (1)
1363 const char *separator;
1365 /* We've already loaded it. */
1366 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1368 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1369 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1370 break;
1373 if (read_class (name))
1374 break;
1376 /* We failed loading name. Now consider that we might be looking
1377 for an inner class. */
1378 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1379 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
1380 name = get_identifier_with_length (IDENTIFIER_POINTER (name),
1381 (separator
1382 - IDENTIFIER_POINTER (name)));
1383 /* Otherwise, we failed, we bail. */
1384 else
1385 break;
1389 /* have we found the class we're looking for? */
1390 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1391 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1392 class_loaded = type && CLASS_PARSED_P (type);
1396 if (!class_loaded)
1398 if (flag_verify_invocations || ! flag_indirect_dispatch)
1400 if (verbose)
1401 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1403 else if (verbose)
1405 /* This is just a diagnostic during testing, not a real problem. */
1406 if (!quiet_flag)
1407 warning (0, "cannot find file for class %s",
1408 IDENTIFIER_POINTER (saved));
1410 /* Fake it. */
1411 if (TREE_CODE (class_or_name) == RECORD_TYPE)
1413 set_super_info (0, class_or_name, object_type_node, 0);
1414 TYPE_DUMMY (class_or_name) = 1;
1415 /* We won't be able to output any debug info for this class. */
1416 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1422 /* Parse the .class file JCF. */
1424 static void
1425 jcf_parse (JCF* jcf)
1427 int i, code;
1429 bitmap_clear (field_offsets);
1431 if (jcf_parse_preamble (jcf) != 0)
1432 fatal_error (input_location, "not a valid Java .class file");
1433 code = jcf_parse_constant_pool (jcf);
1434 if (code != 0)
1435 fatal_error (input_location, "error while parsing constant pool");
1436 code = verify_constant_pool (jcf);
1437 if (code > 0)
1438 fatal_error (input_location, "error in constant pool entry #%d\n", code);
1440 jcf_parse_class (jcf);
1441 if (main_class == NULL_TREE)
1442 main_class = current_class;
1443 if (! quiet_flag && TYPE_NAME (current_class))
1444 fprintf (stderr, " %s %s",
1445 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
1446 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
1447 if (CLASS_PARSED_P (current_class))
1449 /* FIXME - where was first time */
1450 fatal_error (input_location,
1451 "reading class %s for the second time from %s",
1452 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1453 jcf->filename);
1455 CLASS_PARSED_P (current_class) = 1;
1457 for (i = 1; i < JPOOL_SIZE(jcf); i++)
1459 switch (JPOOL_TAG (jcf, i))
1461 case CONSTANT_Class:
1462 get_class_constant (jcf, i);
1463 break;
1467 code = jcf_parse_fields (jcf);
1468 if (code != 0)
1469 fatal_error (input_location, "error while parsing fields");
1470 code = jcf_parse_methods (jcf);
1471 if (code != 0)
1472 fatal_error (input_location, "error while parsing methods");
1473 code = jcf_parse_final_attributes (jcf);
1474 if (code != 0)
1475 fatal_error (input_location, "error while parsing final attributes");
1477 if (TYPE_REFLECTION_DATA (current_class))
1478 annotation_write_byte (JV_DONE_ATTR);
1480 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1482 /* The fields of class_type_node are already in correct order. */
1483 if (current_class != class_type_node && current_class != object_type_node)
1484 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1486 if (current_class == object_type_node)
1487 layout_class_methods (object_type_node);
1488 else
1489 vec_safe_push (all_class_list, TYPE_NAME (current_class));
1492 /* If we came across inner classes, load them now. */
1493 static void
1494 load_inner_classes (tree cur_class)
1496 tree current;
1497 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
1498 current = TREE_CHAIN (current))
1500 tree name = DECL_NAME (TREE_PURPOSE (current));
1501 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1502 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1503 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
1504 load_class (name, 1);
1508 static void
1509 duplicate_class_warning (const char *filename)
1511 location_t warn_loc;
1512 linemap_add (line_table, LC_RENAME, 0, filename, 0);
1513 warn_loc = linemap_line_start (line_table, 0, 1);
1514 warning_at (warn_loc, 0, "duplicate class will only be compiled once");
1517 static void
1518 java_layout_seen_class_methods (void)
1520 unsigned start = 0;
1521 unsigned end = vec_safe_length (all_class_list);
1523 while (1)
1525 unsigned ix;
1526 unsigned new_length;
1528 for (ix = start; ix != end; ix++)
1530 tree decl = (*all_class_list)[ix];
1531 tree cls = TREE_TYPE (decl);
1533 input_location = DECL_SOURCE_LOCATION (decl);
1535 if (! CLASS_LOADED_P (cls))
1536 load_class (cls, 0);
1538 layout_class_methods (cls);
1541 /* Note that new classes might have been added while laying out
1542 methods, changing the value of all_class_list. */
1543 new_length = vec_safe_length (all_class_list);
1544 if (end != new_length)
1546 start = end;
1547 end = new_length;
1549 else
1550 break;
1554 static void
1555 parse_class_file (void)
1557 tree method;
1558 location_t save_location = input_location;
1560 java_layout_seen_class_methods ();
1562 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1564 /* Re-enter the current file. */
1565 expanded_location loc = expand_location (input_location);
1566 linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1568 file_start_location = input_location;
1569 (*debug_hooks->start_source_file) (LOCATION_LINE (input_location),
1570 LOCATION_FILE (input_location));
1572 java_mark_class_local (current_class);
1574 gen_indirect_dispatch_tables (current_class);
1576 for (method = TYPE_METHODS (current_class);
1577 method != NULL_TREE; method = DECL_CHAIN (method))
1579 JCF *jcf = current_jcf;
1581 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
1582 continue;
1584 if (METHOD_NATIVE (method))
1586 tree arg;
1587 int decl_max_locals;
1589 if (! flag_jni)
1590 continue;
1591 /* We need to compute the DECL_MAX_LOCALS. We need to take
1592 the wide types into account too. */
1593 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
1594 arg != end_params_node;
1595 arg = TREE_CHAIN (arg), decl_max_locals += 1)
1597 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1598 decl_max_locals += 1;
1600 DECL_MAX_LOCALS (method) = decl_max_locals;
1601 start_java_method (method);
1602 give_name_to_locals (jcf);
1603 *get_stmts () = build_jni_stub (method);
1604 end_java_method ();
1605 continue;
1608 if (DECL_CODE_OFFSET (method) == 0)
1610 current_function_decl = method;
1611 error ("missing Code attribute");
1612 continue;
1615 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1616 if (DECL_LINENUMBERS_OFFSET (method))
1618 int i;
1619 int min_line = 0;
1620 unsigned char *ptr;
1621 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1622 linenumber_count = i = JCF_readu2 (jcf);
1623 linenumber_table = ptr = jcf->read_ptr;
1625 for (ptr += 2; --i >= 0; ptr += 4)
1627 int line = GET_u2 (ptr);
1628 /* Set initial line of input_location to smallest
1629 * linenumber.
1630 * Needs to be set before init_function_start. */
1631 if (min_line == 0 || line < min_line)
1632 min_line = line;
1634 if (min_line != 0)
1635 input_location = linemap_line_start (line_table, min_line, 1);
1637 else
1639 linenumber_table = NULL;
1640 linenumber_count = 0;
1643 start_java_method (method);
1645 note_instructions (jcf, method);
1647 give_name_to_locals (jcf);
1649 /* Bump up start_label_pc_this_method so we get a unique label number
1650 and reset highest_label_pc_this_method. */
1651 if (highest_label_pc_this_method >= 0)
1653 /* We adjust to the next multiple of 1000. This is just a frill
1654 so the last 3 digits of the label number match the bytecode
1655 offset, which might make debugging marginally more convenient. */
1656 start_label_pc_this_method
1657 = ((((start_label_pc_this_method + highest_label_pc_this_method)
1658 / 1000)
1659 + 1)
1660 * 1000);
1661 highest_label_pc_this_method = -1;
1664 /* Convert bytecode to trees. */
1665 expand_byte_code (jcf, method);
1667 end_java_method ();
1670 finish_class ();
1672 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
1673 input_location = save_location;
1676 static vec<tree, va_gc> *predefined_filenames;
1678 void
1679 add_predefined_file (tree name)
1681 vec_safe_push (predefined_filenames, name);
1685 predefined_filename_p (tree node)
1687 unsigned ix;
1688 tree f;
1690 FOR_EACH_VEC_SAFE_ELT (predefined_filenames, ix, f)
1691 if (f == node)
1692 return 1;
1694 return 0;
1697 /* Generate a function that does all static initialization for this
1698 translation unit. */
1700 static void
1701 java_emit_static_constructor (void)
1703 tree body = NULL;
1705 emit_register_classes (&body);
1706 write_resource_constructor (&body);
1708 if (body)
1710 tree name = get_identifier ("_Jv_global_static_constructor");
1712 tree decl
1713 = build_decl (input_location, FUNCTION_DECL, name,
1714 build_function_type_list (void_type_node, NULL_TREE));
1716 tree resdecl = build_decl (input_location,
1717 RESULT_DECL, NULL_TREE, void_type_node);
1718 DECL_ARTIFICIAL (resdecl) = 1;
1719 DECL_RESULT (decl) = resdecl;
1720 current_function_decl = decl;
1721 allocate_struct_function (decl, false);
1723 TREE_STATIC (decl) = 1;
1724 TREE_USED (decl) = 1;
1725 DECL_ARTIFICIAL (decl) = 1;
1726 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1727 DECL_SAVED_TREE (decl) = body;
1728 DECL_UNINLINABLE (decl) = 1;
1730 DECL_INITIAL (decl) = make_node (BLOCK);
1731 TREE_USED (DECL_INITIAL (decl)) = 1;
1733 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1734 java_genericize (decl);
1735 cgraph_node::finalize_function (decl, false);
1740 void
1741 java_parse_file (void)
1743 int filename_count = 0;
1744 location_t save_location = input_location;
1745 char *file_list = NULL, *list, *next;
1746 tree node;
1747 FILE *finput = NULL;
1748 int in_quotes = 0;
1749 unsigned ix;
1751 bitmap_obstack_initialize (&bit_obstack);
1752 field_offsets = BITMAP_ALLOC (&bit_obstack);
1754 if (flag_filelist_file)
1756 int avail = 2000;
1757 finput = fopen (main_input_filename, "r");
1758 if (finput == NULL)
1759 fatal_error (input_location,
1760 "can%'t open %s: %m", LOCATION_FILE (input_location));
1761 list = XNEWVEC (char, avail);
1762 next = list;
1763 for (;;)
1765 int count;
1766 if (avail < 500)
1768 count = next - list;
1769 avail = 2 * (count + avail);
1770 list = XRESIZEVEC (char, list, avail);
1771 next = list + count;
1772 avail = avail - count;
1774 /* Subtract one to guarantee space for final '\0'. */
1775 count = fread (next, 1, avail - 1, finput);
1776 if (count == 0)
1778 if (! feof (finput))
1779 fatal_error (input_location, "error closing %s: %m",
1780 LOCATION_FILE (input_location));
1781 *next = '\0';
1782 break;
1784 avail -= count;
1785 next += count;
1787 fclose (finput);
1788 finput = NULL;
1789 file_list = list;
1791 else
1792 list = CONST_CAST (char *, main_input_filename);
1794 while (list)
1796 for (next = list; ; )
1798 char ch = *next;
1799 if (flag_filelist_file && ! in_quotes
1800 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1801 || ch == '&') /* FIXME */)
1803 if (next == list)
1805 next++;
1806 list = next;
1807 continue;
1809 else
1811 *next++ = '\0';
1812 break;
1815 if (flag_filelist_file && ch == '"')
1817 in_quotes = ! in_quotes;
1818 *next++ = '\0';
1819 if (in_quotes)
1820 list = next;
1821 else
1822 break;
1824 if (ch == '\0')
1826 next = NULL;
1827 break;
1829 next++;
1832 /* Exclude .java files. */
1833 if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1835 /* Nothing. */
1837 else if (list[0])
1839 node = get_identifier (list);
1841 filename_count++;
1843 /* Exclude file that we see twice on the command line. */
1845 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1846 duplicate_class_warning (IDENTIFIER_POINTER (node));
1847 else
1849 build_translation_unit_decl (node);
1850 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1853 list = next;
1856 free (file_list);
1858 if (filename_count == 0)
1859 warning (0, "no input file specified");
1861 if (resource_name)
1863 const char *resource_filename;
1865 /* Only one resource file may be compiled at a time. */
1866 gcc_assert (all_translation_units->length () == 1);
1868 resource_filename
1869 = IDENTIFIER_POINTER (DECL_NAME ((*all_translation_units)[0]));
1870 compile_resource_file (resource_name, resource_filename);
1872 goto finish;
1875 current_jcf = main_jcf;
1876 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1878 unsigned char magic_string[4];
1879 char *real_path;
1880 uint32 magic = 0;
1881 tree name = DECL_NAME (node);
1882 tree real_file;
1883 const char *filename = IDENTIFIER_POINTER (name);
1885 /* Skip already parsed files */
1886 real_path = lrealpath (filename);
1887 real_file = get_identifier (real_path);
1888 free (real_path);
1889 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1890 continue;
1892 /* Close previous descriptor, if any */
1893 if (finput && fclose (finput))
1894 fatal_error (input_location,
1895 "can%'t close input file %s: %m", main_input_filename);
1897 finput = fopen (filename, "rb");
1898 if (finput == NULL)
1899 fatal_error (input_location, "can%'t open %s: %m", filename);
1901 #ifdef IO_BUFFER_SIZE
1902 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1903 _IOFBF, IO_BUFFER_SIZE);
1904 #endif
1906 /* Figure what kind of file we're dealing with */
1907 if (fread (magic_string, 1, 4, finput) == 4)
1909 fseek (finput, 0L, SEEK_SET);
1910 magic = GET_u4 (magic_string);
1912 if (magic == 0xcafebabe)
1914 CLASS_FILE_P (node) = 1;
1915 current_jcf = ggc_cleared_alloc<JCF> ();
1916 current_jcf->read_state = finput;
1917 current_jcf->filbuf = jcf_filbuf_from_stdio;
1918 jcf_parse (current_jcf);
1919 DECL_SOURCE_LOCATION (node) = file_start_location;
1920 TYPE_JCF (current_class) = current_jcf;
1921 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1923 /* We've already compiled this class. */
1924 duplicate_class_warning (filename);
1925 continue;
1927 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1928 TREE_TYPE (node) = current_class;
1930 else if (magic == (JCF_u4)ZIPMAGIC)
1932 main_jcf = ggc_cleared_alloc<JCF> ();
1933 main_jcf->read_state = finput;
1934 main_jcf->filbuf = jcf_filbuf_from_stdio;
1935 linemap_add (line_table, LC_ENTER, false, filename, 0);
1936 input_location = linemap_line_start (line_table, 0, 1);
1937 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1938 fatal_error (input_location, "bad zip/jar file %s", filename);
1939 localToFile = SeenZipFiles;
1940 /* Register all the classes defined there. */
1941 process_zip_dir ((FILE *) main_jcf->read_state);
1942 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1943 parse_zip_file_entries ();
1945 else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1947 /* Ignore an empty input jar. */
1949 else
1951 gcc_unreachable ();
1952 #if 0
1953 java_push_parser_context ();
1954 java_parser_context_save_global ();
1956 parse_source_file_1 (real_file, filename, finput);
1957 java_parser_context_restore_global ();
1958 java_pop_parser_context (1);
1959 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1960 #endif
1964 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
1966 input_location = DECL_SOURCE_LOCATION (node);
1967 if (CLASS_FILE_P (node))
1969 /* FIXME: These two flags really should be independent. We
1970 should be able to compile fully binary compatible, but
1971 with flag_verify_invocations on. */
1972 flag_verify_invocations = ! flag_indirect_dispatch;
1973 output_class = current_class = TREE_TYPE (node);
1975 current_jcf = TYPE_JCF (current_class);
1976 layout_class (current_class);
1977 load_inner_classes (current_class);
1978 parse_class_file ();
1979 JCF_FINISH (current_jcf);
1982 input_location = save_location;
1984 bitmap_obstack_release (&bit_obstack);
1986 finish:
1987 /* Arrange for any necessary initialization to happen. */
1988 java_emit_static_constructor ();
1989 gcc_assert (global_bindings_p ());
1991 /* Do final processing on globals. */
1992 global_decl_processing ();
1996 /* Return the name of the class corresponding to the name of the file
1997 in this zip entry. The result is newly allocated using ALLOC. */
1998 static char *
1999 compute_class_name (struct ZipDirectory *zdir)
2001 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2002 char *class_name;
2003 int i;
2004 int filename_length = zdir->filename_length;
2006 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
2008 class_name_in_zip_dir += 2;
2009 filename_length -= 2;
2012 filename_length -= strlen (".class");
2013 class_name = XNEWVEC (char, filename_length + 1);
2014 memcpy (class_name, class_name_in_zip_dir, filename_length);
2015 class_name [filename_length] = '\0';
2017 for (i = 0; i < filename_length; i++)
2018 if (class_name[i] == '/')
2019 class_name[i] = '.';
2021 return class_name;
2024 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2025 if it is a property file of some sort. */
2026 static int
2027 classify_zip_file (struct ZipDirectory *zdir)
2029 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2031 if (zdir->filename_length > 6
2032 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2033 ".class", 6))
2034 return 1;
2036 /* For now we drop the manifest, but not other information. */
2037 if (zdir->filename_length == 20
2038 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
2039 return 0;
2041 /* Drop directory entries. */
2042 if (zdir->filename_length > 0
2043 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2044 return 0;
2046 return 2;
2049 /* Process all class entries found in the zip file. */
2050 static void
2051 parse_zip_file_entries (void)
2053 struct ZipDirectory *zdir;
2054 int i;
2056 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2057 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2059 tree klass;
2061 switch (classify_zip_file (zdir))
2063 case 0:
2064 continue;
2066 case 1:
2068 char *class_name = compute_class_name (zdir);
2069 int previous_alias_set = -1;
2070 klass = lookup_class (get_identifier (class_name));
2071 FREE (class_name);
2072 current_jcf = TYPE_JCF (klass);
2073 output_class = current_class = klass;
2075 /* This is a dummy class, and now we're compiling it for
2076 real. */
2077 gcc_assert (! TYPE_DUMMY (klass));
2079 /* This is for a corner case where we have a superclass
2080 but no superclass fields.
2082 This can happen if we earlier failed to lay out this
2083 class because its superclass was still in the process
2084 of being laid out; this occurs when we have recursive
2085 class dependencies via inner classes. We must record
2086 the previous alias set and restore it after laying out
2087 the class.
2089 FIXME: this really is a kludge. We should figure out a
2090 way to lay out the class properly before this
2091 happens. */
2092 if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2093 && integer_zerop (TYPE_SIZE (klass)))
2095 TYPE_SIZE (klass) = NULL_TREE;
2096 previous_alias_set = TYPE_ALIAS_SET (klass);
2097 TYPE_ALIAS_SET (klass) = -1;
2100 if (! CLASS_LOADED_P (klass))
2102 if (! CLASS_PARSED_P (klass))
2104 read_zip_member (current_jcf, zdir, localToFile);
2105 jcf_parse (current_jcf);
2107 layout_class (current_class);
2108 load_inner_classes (current_class);
2111 if (previous_alias_set != -1)
2112 TYPE_ALIAS_SET (klass) = previous_alias_set;
2114 if (TYPE_SIZE (current_class) != error_mark_node)
2116 parse_class_file ();
2117 free (current_jcf->buffer); /* No longer necessary */
2118 /* Note: there is a way to free this buffer right after a
2119 class seen in a zip file has been parsed. The idea is the
2120 set its jcf in such a way that buffer will be reallocated
2121 the time the code for the class will be generated. FIXME. */
2124 break;
2126 case 2:
2128 char *file_name, *class_name_in_zip_dir, *buffer;
2129 JCF *jcf;
2130 file_name = XNEWVEC (char, zdir->filename_length + 1);
2131 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2132 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2133 file_name[zdir->filename_length] = '\0';
2134 jcf = XNEW (JCF);
2135 JCF_ZERO (jcf);
2136 jcf->read_state = finput;
2137 jcf->filbuf = jcf_filbuf_from_stdio;
2138 jcf->classname = NULL;
2139 jcf->filename = file_name;
2140 jcf->zipd = zdir;
2142 if (read_zip_member (jcf, zdir, localToFile) < 0)
2143 fatal_error (input_location,
2144 "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"