Pass name cleanups
[official-gcc.git] / gcc / java / jcf-parse.c
blob37cea2870a872de4ddee4fceb083da467eafe152
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Per Bothner <bothner@cygnus.com> */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "tree.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 "cgraph.h"
42 #include "vecprim.h"
43 #include "bitmap.h"
44 #include "target.h"
46 #ifdef HAVE_LOCALE_H
47 #include <locale.h>
48 #endif
50 #ifdef HAVE_LANGINFO_CODESET
51 #include <langinfo.h>
52 #endif
54 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
55 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
56 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
57 #define JPOOL_UTF_DATA(JCF, INDEX) \
58 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
59 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
60 do { \
61 unsigned char save; unsigned char *text; \
62 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
63 text = (JCF)->read_ptr; \
64 save = text[LENGTH]; \
65 text[LENGTH] = 0; \
66 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
67 text[LENGTH] = save; \
68 JCF_SKIP (JCF, LENGTH); } while (0)
70 #include "jcf.h"
72 extern struct obstack temporary_obstack;
74 static GTY(()) tree parse_roots[2];
76 /* The FIELD_DECL for the current field. */
77 #define current_field parse_roots[0]
79 /* The METHOD_DECL for the current method. */
80 #define current_method parse_roots[1]
82 /* Line 0 in current file, if compiling from bytecode. */
83 static location_t file_start_location;
85 /* The Java archive that provides main_class; the main input file. */
86 static GTY(()) struct JCF * main_jcf;
88 /* A list of all the class DECLs seen so far. */
89 static GTY(()) VEC(tree,gc) *all_class_list;
91 /* The number of source files passed to us by -fsource-filename and an
92 array of pointers to each name. Used by find_sourcefile(). */
93 static int num_files = 0;
94 static char **filenames;
96 static struct ZipFile *localToFile;
98 /* A map of byte offsets in the reflection data that are fields which
99 need renumbering. */
100 bitmap field_offsets;
101 bitmap_obstack bit_obstack;
103 /* Declarations of some functions used here. */
104 static void handle_innerclass_attribute (int count, JCF *, int len);
105 static tree give_name_to_class (JCF *jcf, int index);
106 static char *compute_class_name (struct ZipDirectory *zdir);
107 static int classify_zip_file (struct ZipDirectory *zdir);
108 static void parse_zip_file_entries (void);
109 static void process_zip_dir (FILE *);
110 static void parse_class_file (void);
111 static void handle_deprecated (void);
112 static void set_source_filename (JCF *, int);
113 static void jcf_parse (struct JCF*);
114 static void load_inner_classes (tree);
115 static void handle_annotation (JCF *jcf, int level);
116 static void java_layout_seen_class_methods (void);
118 /* Handle "Deprecated" attribute. */
119 static void
120 handle_deprecated (void)
122 if (current_field != NULL_TREE)
123 FIELD_DEPRECATED (current_field) = 1;
124 else if (current_method != NULL_TREE)
125 METHOD_DEPRECATED (current_method) = 1;
126 else if (current_class != NULL_TREE)
127 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
128 else
130 /* Shouldn't happen. */
131 gcc_unreachable ();
137 /* Reverse a string. */
138 static char *
139 reverse (const char *s)
141 if (s == NULL)
142 return NULL;
143 else
145 int len = strlen (s);
146 char *d = XNEWVAR (char, len + 1);
147 const char *sp;
148 char *dp;
150 d[len] = 0;
151 for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
152 *dp = *sp;
154 return d;
158 /* Compare two strings for qsort(). */
159 static int
160 cmpstringp (const void *p1, const void *p2)
162 /* The arguments to this function are "pointers to
163 pointers to char", but strcmp() arguments are "pointers
164 to char", hence the following cast plus dereference */
166 return strcmp(*(const char *const*) p1, *(const char *const*) p2);
169 /* Create an array of strings, one for each source file that we've
170 seen. fsource_filename can either be the name of a single .java
171 file or a file that contains a list of filenames separated by
172 newlines. */
173 void
174 java_read_sourcefilenames (const char *fsource_filename)
176 if (fsource_filename
177 && filenames == 0
178 && strlen (fsource_filename) > strlen (".java")
179 && filename_cmp ((fsource_filename
180 + strlen (fsource_filename)
181 - strlen (".java")),
182 ".java") != 0)
184 /* fsource_filename isn't a .java file but a list of filenames
185 separated by newlines */
186 FILE *finput = fopen (fsource_filename, "r");
187 int len = 0;
188 int longest_line = 0;
190 gcc_assert (finput);
192 /* Find out how many files there are, and how long the filenames are. */
193 while (! feof (finput))
195 int ch = getc (finput);
196 if (ch == '\n')
198 num_files++;
199 if (len > longest_line)
200 longest_line = len;
201 len = 0;
202 continue;
204 if (ch == EOF)
205 break;
206 len++;
209 rewind (finput);
211 /* Read the filenames. Put a pointer to each filename into the
212 array FILENAMES. */
214 char *linebuf = (char *) alloca (longest_line + 1);
215 int i = 0;
216 int charpos;
218 filenames = XNEWVEC (char *, num_files);
220 charpos = 0;
221 for (;;)
223 int ch = getc (finput);
224 if (ch == EOF)
225 break;
226 if (ch == '\n')
228 linebuf[charpos] = 0;
229 gcc_assert (i < num_files);
230 /* ??? Perhaps we should use lrealpath() here. Doing
231 so would tidy up things like /../ but the rest of
232 gcc seems to assume relative pathnames, not
233 absolute pathnames. */
234 /* realname = lrealpath (linebuf); */
235 filenames[i++] = reverse (linebuf);
236 charpos = 0;
237 continue;
239 gcc_assert (charpos < longest_line);
240 linebuf[charpos++] = ch;
243 if (num_files > 1)
244 qsort (filenames, num_files, sizeof (char *), cmpstringp);
246 fclose (finput);
248 else
250 filenames = XNEWVEC (char *, 1);
251 filenames[0] = reverse (fsource_filename);
252 num_files = 1;
256 /* Given a relative pathname such as foo/bar.java, attempt to find a
257 longer pathname with the same suffix.
259 This is a best guess heuristic; with some weird class hierarchies we
260 may fail to pick the correct source file. For example, if we have
261 the filenames foo/bar.java and also foo/foo/bar.java, we do not
262 have enough information to know which one is the right match for
263 foo/bar.java. */
265 static const char *
266 find_sourcefile (const char *name)
268 int i = 0, j = num_files-1;
269 char *found = NULL;
271 if (filenames)
273 char *revname = reverse (name);
277 int k = (i+j) / 2;
278 int cmp = strncmp (revname, filenames[k], strlen (revname));
279 if (cmp == 0)
281 /* OK, so we found one. But is it a unique match? */
282 if ((k > i
283 && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
284 || (k < j
285 && (strncmp (revname, filenames[k+1], strlen (revname))
286 == 0)))
288 else
289 found = filenames[k];
290 break;
292 if (cmp > 0)
293 i = k+1;
294 else
295 j = k-1;
297 while (i <= j);
299 free (revname);
302 if (found && strlen (found) > strlen (name))
303 return reverse (found);
304 else
305 return name;
310 /* Handle "SourceFile" attribute. */
312 static void
313 set_source_filename (JCF *jcf, int index)
315 tree sfname_id = get_name_constant (jcf, index);
316 const char *sfname = IDENTIFIER_POINTER (sfname_id);
317 const char *old_filename = input_filename;
318 int new_len = IDENTIFIER_LENGTH (sfname_id);
319 if (old_filename != NULL)
321 int old_len = strlen (old_filename);
322 /* Use the current input_filename (derived from the class name)
323 if it has a directory prefix, but otherwise matches sfname. */
324 if (old_len > new_len
325 && filename_cmp (sfname, old_filename + old_len - new_len) == 0
326 && (old_filename[old_len - new_len - 1] == '/'
327 || old_filename[old_len - new_len - 1] == '\\'))
328 return;
330 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
332 const char *class_name
333 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
334 const char *dot = strrchr (class_name, '.');
335 if (dot != NULL)
337 /* Length of prefix, not counting final dot. */
338 int i = dot - class_name;
339 /* Concatenate current package prefix with new sfname. */
340 char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
341 strcpy (buf + i + 1, sfname);
342 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
343 Note we start at the end with the final package dot. */
344 for (; i >= 0; i--)
346 char c = class_name[i];
347 if (c == '.')
348 c = DIR_SEPARATOR;
349 buf[i] = c;
351 sfname_id = get_identifier (buf);
352 free (buf);
353 sfname = IDENTIFIER_POINTER (sfname_id);
357 sfname = find_sourcefile (sfname);
358 line_table->maps[line_table->used-1].to_file = sfname;
359 if (current_class == main_class) main_input_filename = sfname;
365 /* Annotation handling.
367 The technique we use here is to copy the annotation data directly
368 from the input class file into the output file. We don't decode the
369 data at all, merely rewriting constant indexes whenever we come
370 across them: this is necessary because the constant pool in the
371 output file isn't the same as the constant pool in the input.
373 The main advantage of this technique is that the resulting
374 annotation data is pointer-free, so it doesn't have to be relocated
375 at startup time. As a consequence of this, annotations have no
376 performance impact unless they are used. Also, this representation
377 is very dense. */
380 /* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
381 the start of the newly allocated region. */
383 static unsigned char*
384 annotation_grow (int delta)
386 unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
387 long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
388 long len = *datasize;
390 if (*data == NULL)
392 *data = XNEWVAR (unsigned char, delta);
394 else
396 int newlen = *datasize + delta;
397 if (floor_log2 (newlen) != floor_log2 (*datasize))
398 *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
400 *datasize += delta;
401 return *data + len;
404 /* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
405 byte order (i.e. big endian.) */
407 static void
408 annotation_rewrite_byte (unsigned int n, unsigned char *p)
410 p[0] = n;
413 static void
414 annotation_rewrite_short (unsigned int n, unsigned char *p)
416 p[0] = n>>8;
417 p[1] = n;
420 static void
421 annotation_rewrite_int (unsigned int n, unsigned char *p)
423 p[0] = n>>24;
424 p[1] = n>>16;
425 p[2] = n>>8;
426 p[3] = n;
429 /* Read a 16-bit unsigned int in Java byte order (i.e. big
430 endian.) */
432 static uint16
433 annotation_read_short (unsigned char *p)
435 uint16 tmp = p[0];
436 tmp = (tmp << 8) | p[1];
437 return tmp;
440 /* annotation_write_TYPE. Rewrite various int types, appending them
441 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
442 endian.) */
444 static void
445 annotation_write_byte (unsigned int n)
447 annotation_rewrite_byte (n, annotation_grow (1));
450 static void
451 annotation_write_short (unsigned int n)
453 annotation_rewrite_short (n, annotation_grow (2));
456 static void
457 annotation_write_int (unsigned int n)
459 annotation_rewrite_int (n, annotation_grow (4));
462 /* Create a 64-bit constant in the constant pool.
464 This is used for both integer and floating-point types. As a
465 consequence, it will not work if the target floating-point format
466 is anything other than IEEE-754. While this is arguably a bug, the
467 runtime library makes exactly the same assumption and it's unlikely
468 that Java will ever run on a non-IEEE machine. */
470 static int
471 handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
472 int index, bool big_endian)
474 /* If we're on a 64-bit platform we can fit a long or double
475 into the same space as a jword. */
476 if (POINTER_SIZE >= 64)
477 index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
479 /* In a compiled program the constant pool is in native word
480 order. How weird is that??? */
481 else if (big_endian)
482 index = find_constant2 (cpool, kind,
483 JPOOL_INT (jcf, index),
484 JPOOL_INT (jcf, index+1));
485 else
486 index = find_constant2 (cpool, kind,
487 JPOOL_INT (jcf, index+1),
488 JPOOL_INT (jcf, index));
490 return index;
493 /* Given a class file and an index into its constant pool, create an
494 entry in the outgoing constant pool for the same item. */
496 static uint16
497 handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
499 unsigned int kind;
500 CPool *cpool = cpool_for_class (output_class);
502 if (index == 0)
503 return 0;
505 if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
506 error ("<constant pool index %d not in range>", index);
508 kind = JPOOL_TAG (jcf, index);
510 if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
512 if (purpose == CONSTANT_Class
513 && kind == CONSTANT_Utf8)
515 else
516 error ("<constant pool index %d unexpected type", index);
519 switch (kind)
521 case CONSTANT_Class:
522 case CONSTANT_ResolvedClass:
524 /* For some reason I know not the what of, class names in
525 annotations are UTF-8 strings in the constant pool but
526 class names in EnclosingMethod attributes are real class
527 references. Set CONSTANT_LazyFlag here so that the VM
528 doesn't attempt to resolve them at class initialization
529 time. */
530 tree resolved_class, class_name;
531 resolved_class = get_class_constant (jcf, index);
532 class_name = build_internal_class_name (resolved_class);
533 index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
534 (unmangle_classname
535 (IDENTIFIER_POINTER(class_name),
536 IDENTIFIER_LENGTH(class_name))));
537 break;
539 case CONSTANT_Utf8:
541 tree utf8 = get_constant (jcf, index);
542 if (purpose == CONSTANT_Class)
543 /* Create a constant pool entry for a type signature. This
544 one has '.' rather than '/' because it isn't going into a
545 class file, it's going into a compiled object.
547 This has to match the logic in
548 _Jv_ClassReader::prepare_pool_entry(). */
549 utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
550 IDENTIFIER_LENGTH(utf8));
551 index = alloc_name_constant (kind, utf8);
553 break;
555 case CONSTANT_Long:
556 index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
557 targetm.words_big_endian ());
558 break;
560 case CONSTANT_Double:
561 index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
562 targetm.float_words_big_endian ());
563 break;
565 case CONSTANT_Float:
566 case CONSTANT_Integer:
567 index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
568 break;
570 case CONSTANT_NameAndType:
572 uint16 name = JPOOL_USHORT1 (jcf, index);
573 uint16 sig = JPOOL_USHORT2 (jcf, index);
574 uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
575 uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
576 jword new_index = (name_index << 16) | sig_index;
577 index = find_constant1 (cpool, kind, new_index);
579 break;
581 default:
582 abort ();
585 return index;
588 /* Read an element_value structure from an annotation in JCF. Return
589 the constant pool index for the resulting constant pool entry. */
591 static int
592 handle_element_value (JCF *jcf, int level)
594 uint8 tag = JCF_readu (jcf);
595 int index = 0;
597 annotation_write_byte (tag);
598 switch (tag)
600 case 'B':
601 case 'C':
602 case 'S':
603 case 'Z':
604 case 'I':
606 uint16 cindex = JCF_readu2 (jcf);
607 index = handle_constant (jcf, cindex,
608 CONSTANT_Integer);
609 annotation_write_short (index);
611 break;
612 case 'D':
614 uint16 cindex = JCF_readu2 (jcf);
615 index = handle_constant (jcf, cindex,
616 CONSTANT_Double);
617 annotation_write_short (index);
619 break;
620 case 'F':
622 uint16 cindex = JCF_readu2 (jcf);
623 index = handle_constant (jcf, cindex,
624 CONSTANT_Float);
625 annotation_write_short (index);
627 break;
628 case 'J':
630 uint16 cindex = JCF_readu2 (jcf);
631 index = handle_constant (jcf, cindex,
632 CONSTANT_Long);
633 annotation_write_short (index);
635 break;
636 case 's':
638 uint16 cindex = JCF_readu2 (jcf);
639 /* Despite what the JVM spec says, compilers generate a Utf8
640 constant here, not a String. */
641 index = handle_constant (jcf, cindex,
642 CONSTANT_Utf8);
643 annotation_write_short (index);
645 break;
647 case 'e':
649 uint16 type_name_index = JCF_readu2 (jcf);
650 uint16 const_name_index = JCF_readu2 (jcf);
651 index = handle_constant (jcf, type_name_index,
652 CONSTANT_Class);
653 annotation_write_short (index);
654 index = handle_constant (jcf, const_name_index,
655 CONSTANT_Utf8);
656 annotation_write_short (index);
658 break;
659 case 'c':
661 uint16 class_info_index = JCF_readu2 (jcf);
662 index = handle_constant (jcf, class_info_index,
663 CONSTANT_Class);
664 annotation_write_short (index);
666 break;
667 case '@':
669 handle_annotation (jcf, level + 1);
671 break;
672 case '[':
674 uint16 n_array_elts = JCF_readu2 (jcf);
675 annotation_write_short (n_array_elts);
676 while (n_array_elts--)
677 handle_element_value (jcf, level + 1);
679 break;
680 default:
681 abort();
682 break;
684 return index;
687 /* Read an annotation structure from JCF. Write it to the
688 reflection_data field of the outgoing class. */
690 static void
691 handle_annotation (JCF *jcf, int level)
693 uint16 type_index = JCF_readu2 (jcf);
694 uint16 npairs = JCF_readu2 (jcf);
695 int index = handle_constant (jcf, type_index,
696 CONSTANT_Class);
697 annotation_write_short (index);
698 annotation_write_short (npairs);
699 while (npairs--)
701 uint16 name_index = JCF_readu2 (jcf);
702 index = handle_constant (jcf, name_index,
703 CONSTANT_Utf8);
704 annotation_write_short (index);
705 handle_element_value (jcf, level + 2);
709 /* Read an annotation count from JCF, and write the following
710 annotations to the reflection_data field of the outgoing class. */
712 static void
713 handle_annotations (JCF *jcf, int level)
715 uint16 num = JCF_readu2 (jcf);
716 annotation_write_short (num);
717 while (num--)
718 handle_annotation (jcf, level);
721 /* As handle_annotations(), but perform a sanity check that we write
722 the same number of bytes that we were expecting. */
724 static void
725 handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
726 long length)
728 long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
730 handle_annotations (jcf, 0);
732 gcc_assert (old_datasize + length
733 == TYPE_REFLECTION_DATASIZE (current_class));
736 /* gcj permutes its fields array after generating annotation_data, so
737 we have to fixup field indexes for fields that have moved. Given
738 ARG, a VEC_int, fixup the field indexes in the reflection_data of
739 the outgoing class. We use field_offsets to tell us where the
740 fixups must go. */
742 void
743 rewrite_reflection_indexes (void *arg)
745 bitmap_iterator bi;
746 unsigned int offset;
747 VEC(int, heap) *map = (VEC(int, heap) *) arg;
748 unsigned char *data = TYPE_REFLECTION_DATA (current_class);
750 if (map)
752 EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
754 uint16 index = annotation_read_short (data + offset);
755 annotation_rewrite_short
756 (VEC_index (int, map, index), data + offset);
761 /* Read the RuntimeVisibleAnnotations from JCF and write them to the
762 reflection_data of the outgoing class. */
764 static void
765 handle_member_annotations (int member_index, JCF *jcf,
766 const unsigned char *name ATTRIBUTE_UNUSED,
767 long len, jv_attr_type member_type)
769 int new_len = len + 1;
770 annotation_write_byte (member_type);
771 if (member_type != JV_CLASS_ATTR)
772 new_len += 2;
773 annotation_write_int (new_len);
774 annotation_write_byte (JV_ANNOTATIONS_KIND);
775 if (member_type == JV_FIELD_ATTR)
776 bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
777 if (member_type != JV_CLASS_ATTR)
778 annotation_write_short (member_index);
779 handle_annotation_attribute (member_index, jcf, len);
782 /* Read the RuntimeVisibleParameterAnnotations from JCF and write them
783 to the reflection_data of the outgoing class. */
785 static void
786 handle_parameter_annotations (int member_index, JCF *jcf,
787 const unsigned char *name ATTRIBUTE_UNUSED,
788 long len, jv_attr_type member_type)
790 int new_len = len + 1;
791 uint8 num;
792 annotation_write_byte (member_type);
793 if (member_type != JV_CLASS_ATTR)
794 new_len += 2;
795 annotation_write_int (new_len);
796 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
797 if (member_type != JV_CLASS_ATTR)
798 annotation_write_short (member_index);
799 num = JCF_readu (jcf);
800 annotation_write_byte (num);
801 while (num--)
802 handle_annotations (jcf, 0);
806 /* Read the AnnotationDefault data from JCF and write them to the
807 reflection_data of the outgoing class. */
809 static void
810 handle_default_annotation (int member_index, JCF *jcf,
811 const unsigned char *name ATTRIBUTE_UNUSED,
812 long len, jv_attr_type member_type)
814 int new_len = len + 1;
815 annotation_write_byte (member_type);
816 if (member_type != JV_CLASS_ATTR)
817 new_len += 2;
818 annotation_write_int (new_len);
819 annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
820 if (member_type != JV_CLASS_ATTR)
821 annotation_write_short (member_index);
822 handle_element_value (jcf, 0);
825 /* As above, for the EnclosingMethod attribute. */
827 static void
828 handle_enclosingmethod_attribute (int member_index, JCF *jcf,
829 const unsigned char *name ATTRIBUTE_UNUSED,
830 long len, jv_attr_type member_type)
832 int new_len = len + 1;
833 uint16 index;
834 annotation_write_byte (member_type);
835 if (member_type != JV_CLASS_ATTR)
836 new_len += 2;
837 annotation_write_int (new_len);
838 annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
839 if (member_type != JV_CLASS_ATTR)
840 annotation_write_short (member_index);
842 index = JCF_readu2 (jcf);
843 index = handle_constant (jcf, index, CONSTANT_Class);
844 annotation_write_short (index);
846 index = JCF_readu2 (jcf);
847 index = handle_constant (jcf, index, CONSTANT_NameAndType);
848 annotation_write_short (index);
851 /* As above, for the Signature attribute. */
853 static void
854 handle_signature_attribute (int member_index, JCF *jcf,
855 const unsigned char *name ATTRIBUTE_UNUSED,
856 long len, jv_attr_type member_type)
858 int new_len = len + 1;
859 uint16 index;
860 annotation_write_byte (member_type);
861 if (member_type != JV_CLASS_ATTR)
862 new_len += 2;
863 annotation_write_int (new_len);
864 annotation_write_byte (JV_SIGNATURE_KIND);
865 if (member_type != JV_CLASS_ATTR)
866 annotation_write_short (member_index);
868 index = JCF_readu2 (jcf);
869 index = handle_constant (jcf, index, CONSTANT_Utf8);
870 annotation_write_short (index);
875 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
877 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
878 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
879 output_class = current_class = give_name_to_class (jcf, THIS); \
880 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
882 #define HANDLE_CLASS_INTERFACE(INDEX) \
883 add_interface (current_class, get_class_constant (jcf, INDEX))
885 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
886 { int sig_index = SIGNATURE; \
887 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
888 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
889 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
890 if ((ACCESS_FLAGS) & ACC_FINAL) \
891 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
894 #define HANDLE_END_FIELDS() \
895 (current_field = NULL_TREE)
897 #define HANDLE_CONSTANTVALUE(INDEX) \
898 { tree constant; int index = INDEX; \
899 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
900 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
901 constant = build_utf8_ref (name); \
903 else \
904 constant = get_constant (jcf, index); \
905 set_constant_value (current_field, constant); }
907 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
908 (current_method = add_method (current_class, ACCESS_FLAGS, \
909 get_name_constant (jcf, NAME), \
910 get_name_constant (jcf, SIGNATURE)), \
911 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
912 DECL_LINENUMBERS_OFFSET (current_method) = 0)
914 #define HANDLE_END_METHODS() \
915 { current_method = NULL_TREE; }
917 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
918 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
919 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
920 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
921 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
923 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
924 { int n = (COUNT); \
925 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
926 JCF_SKIP (jcf, n * 10); }
928 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
929 { int n = (COUNT); \
930 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
931 JCF_SKIP (jcf, n * 4); }
933 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
935 int n = COUNT; \
936 VEC (tree,gc) *v = VEC_alloc (tree, gc, n); \
937 gcc_assert (DECL_FUNCTION_THROWS (current_method) == NULL); \
938 while (--n >= 0) \
940 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
941 VEC_quick_push (tree, v, thrown_class); \
943 DECL_FUNCTION_THROWS (current_method) = v; \
946 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
948 /* Link seen inner classes to their outer context and register the
949 inner class to its outer context. They will be later loaded. */
950 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
951 handle_innerclass_attribute (COUNT, jcf, attribute_length)
953 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
955 /* Irrelevant decls should have been nullified by the END macros. \
956 DECL_ARTIFICIAL on fields is used for something else (See \
957 PUSH_FIELD in java-tree.h) */ \
958 if (current_method) \
959 DECL_ARTIFICIAL (current_method) = 1; \
960 else if (current_field) \
961 FIELD_SYNTHETIC (current_field) = 1; \
962 else \
963 TYPE_SYNTHETIC (current_class) = 1; \
966 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
968 if (current_class == object_type_node) \
969 jcf->right_zip = 1; \
972 #define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
974 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
977 #define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
979 JCF_SKIP(jcf, attribute_length); \
982 #define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
984 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
987 #define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
989 JCF_SKIP(jcf, attribute_length); \
992 #define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
994 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
997 #define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
999 handle_enclosingmethod_attribute (index, jcf, name_data, \
1000 attribute_length, attr_type); \
1003 #define HANDLE_SIGNATURE_ATTRIBUTE() \
1005 handle_signature_attribute (index, jcf, name_data, \
1006 attribute_length, attr_type); \
1009 #include "jcf-reader.c"
1011 tree
1012 parse_signature (JCF *jcf, int sig_index)
1014 gcc_assert (sig_index > 0
1015 && sig_index < JPOOL_SIZE (jcf)
1016 && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1018 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1019 JPOOL_UTF_LENGTH (jcf, sig_index));
1022 tree
1023 get_constant (JCF *jcf, int index)
1025 tree value;
1026 int tag;
1027 if (index <= 0 || index >= JPOOL_SIZE(jcf))
1028 goto bad;
1029 tag = JPOOL_TAG (jcf, index);
1030 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
1031 return jcf->cpool.data[index].t;
1032 switch (tag)
1034 case CONSTANT_Integer:
1036 jint num = JPOOL_INT(jcf, index);
1037 value = build_int_cst (int_type_node, num);
1038 break;
1040 case CONSTANT_Long:
1042 unsigned HOST_WIDE_INT num;
1043 double_int val;
1045 num = JPOOL_UINT (jcf, index);
1046 val = double_int_lshift (uhwi_to_double_int (num), 32, 64, false);
1047 num = JPOOL_UINT (jcf, index + 1);
1048 val = double_int_ior (val, uhwi_to_double_int (num));
1050 value = double_int_to_tree (long_type_node, val);
1051 break;
1054 case CONSTANT_Float:
1056 jint num = JPOOL_INT(jcf, index);
1057 long buf = num;
1058 REAL_VALUE_TYPE d;
1060 real_from_target_fmt (&d, &buf, &ieee_single_format);
1061 value = build_real (float_type_node, d);
1062 break;
1065 case CONSTANT_Double:
1067 long buf[2], lo, hi;
1068 REAL_VALUE_TYPE d;
1070 hi = JPOOL_UINT (jcf, index);
1071 lo = JPOOL_UINT (jcf, index+1);
1073 if (targetm.float_words_big_endian ())
1074 buf[0] = hi, buf[1] = lo;
1075 else
1076 buf[0] = lo, buf[1] = hi;
1078 real_from_target_fmt (&d, buf, &ieee_double_format);
1079 value = build_real (double_type_node, d);
1080 break;
1083 case CONSTANT_String:
1085 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
1086 const char *utf8_ptr = IDENTIFIER_POINTER (name);
1087 int utf8_len = IDENTIFIER_LENGTH (name);
1088 const unsigned char *utf8;
1089 int i;
1091 /* Check for a malformed Utf8 string. */
1092 utf8 = (const unsigned char *) utf8_ptr;
1093 i = utf8_len;
1094 while (i > 0)
1096 int char_len = UT8_CHAR_LENGTH (*utf8);
1097 if (char_len < 0 || char_len > 3 || char_len > i)
1098 fatal_error ("bad string constant");
1100 utf8 += char_len;
1101 i -= char_len;
1104 /* Allocate a new string value. */
1105 value = build_string (utf8_len, utf8_ptr);
1106 TREE_TYPE (value) = build_pointer_type (string_type_node);
1108 break;
1109 default:
1110 goto bad;
1112 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
1113 jcf->cpool.data[index].t = value;
1114 return value;
1115 bad:
1116 internal_error ("bad value constant type %d, index %d",
1117 JPOOL_TAG (jcf, index), index);
1120 tree
1121 get_name_constant (JCF *jcf, int index)
1123 tree name = get_constant (jcf, index);
1124 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
1125 return name;
1128 /* Handle reading innerclass attributes. If a nonzero entry (denoting
1129 a non anonymous entry) is found, We augment the inner class list of
1130 the outer context with the newly resolved innerclass. */
1132 static void
1133 handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
1135 int c = count;
1137 annotation_write_byte (JV_CLASS_ATTR);
1138 annotation_write_int (attribute_length+1);
1139 annotation_write_byte (JV_INNER_CLASSES_KIND);
1140 annotation_write_short (count);
1142 while (c--)
1144 /* Read inner_class_info_index. This may be 0 */
1145 int icii = JCF_readu2 (jcf);
1146 /* Read outer_class_info_index. If the innerclasses attribute
1147 entry isn't a member (like an inner class) the value is 0. */
1148 int ocii = JCF_readu2 (jcf);
1149 /* Read inner_name_index. If the class we're dealing with is
1150 an anonymous class, it must be 0. */
1151 int ini = JCF_readu2 (jcf);
1152 /* Read the access flag. */
1153 int acc = JCF_readu2 (jcf);
1155 annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1156 annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1157 annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1158 annotation_write_short (acc);
1160 /* If icii is 0, don't try to read the class. */
1161 if (icii >= 0)
1163 tree klass = get_class_constant (jcf, icii);
1164 tree decl = TYPE_NAME (klass);
1165 /* Skip reading further if ocii is null */
1166 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1168 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1169 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
1170 set_class_decl_access_flags (acc, decl);
1171 DECL_CONTEXT (decl) = outer;
1172 DECL_INNER_CLASS_LIST (outer) =
1173 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1174 CLASS_COMPLETE_P (decl) = 1;
1180 static tree
1181 give_name_to_class (JCF *jcf, int i)
1183 gcc_assert (i > 0
1184 && i < JPOOL_SIZE (jcf)
1185 && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1188 tree package_name = NULL_TREE, tmp;
1189 tree this_class;
1190 int j = JPOOL_USHORT1 (jcf, i);
1191 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
1192 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
1193 JPOOL_UTF_LENGTH (jcf, j));
1194 this_class = lookup_class (class_name);
1196 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
1197 const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
1198 linemap_add (line_table, LC_ENTER, false, sfname, 0);
1199 input_location = linemap_line_start (line_table, 0, 1);
1200 file_start_location = input_location;
1201 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1202 if (main_input_filename == NULL && jcf == main_jcf)
1203 main_input_filename = sfname;
1206 jcf->cpool.data[i].t = this_class;
1207 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1208 split_qualified_name (&package_name, &tmp,
1209 DECL_NAME (TYPE_NAME (this_class)));
1210 TYPE_PACKAGE (this_class) = package_name;
1211 return this_class;
1215 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
1217 tree
1218 get_class_constant (JCF *jcf, int i)
1220 tree type;
1221 gcc_assert (i > 0
1222 && i < JPOOL_SIZE (jcf)
1223 && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
1225 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1227 int name_index = JPOOL_USHORT1 (jcf, i);
1228 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
1229 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
1230 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
1232 if (name[0] == '[') /* Handle array "classes". */
1233 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
1234 else
1236 tree cname = unmangle_classname (name, nlength);
1237 type = lookup_class (cname);
1239 jcf->cpool.data[i].t = type;
1240 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1242 else
1243 type = jcf->cpool.data[i].t;
1244 return type;
1247 /* Read a class with the fully qualified-name NAME.
1248 Return 1 iff we read the requested file.
1249 (It is still possible we failed if the file did not
1250 define the class it is supposed to.) */
1253 read_class (tree name)
1255 JCF this_jcf, *jcf;
1256 tree icv, klass = NULL_TREE;
1257 tree save_current_class = current_class;
1258 tree save_output_class = output_class;
1259 location_t save_location = input_location;
1260 JCF *save_current_jcf = current_jcf;
1262 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
1264 klass = TREE_TYPE (icv);
1265 jcf = TYPE_JCF (klass);
1267 else
1268 jcf = NULL;
1270 if (jcf == NULL)
1272 const char* path_name;
1273 this_jcf.zipd = NULL;
1274 jcf = &this_jcf;
1276 path_name = find_class (IDENTIFIER_POINTER (name),
1277 IDENTIFIER_LENGTH (name),
1278 &this_jcf);
1279 if (path_name == 0)
1280 return 0;
1281 else
1282 free(CONST_CAST (char *, path_name));
1285 current_jcf = jcf;
1287 if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
1289 output_class = current_class = klass;
1290 if (JCF_SEEN_IN_ZIP (current_jcf))
1291 read_zip_member(current_jcf,
1292 current_jcf->zipd, current_jcf->zipd->zipf);
1293 jcf_parse (current_jcf);
1294 /* Parsing might change the class, in which case we have to
1295 put it back where we found it. */
1296 if (current_class != klass && icv != NULL_TREE)
1297 TREE_TYPE (icv) = current_class;
1298 klass = current_class;
1300 layout_class (klass);
1301 load_inner_classes (klass);
1303 output_class = save_output_class;
1304 current_class = save_current_class;
1305 input_location = save_location;
1306 current_jcf = save_current_jcf;
1307 return 1;
1310 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1311 called from the parser, otherwise it's a RECORD_TYPE node. If
1312 VERBOSE is 1, print error message on failure to load a class. */
1313 void
1314 load_class (tree class_or_name, int verbose)
1316 tree name, saved;
1317 int class_loaded = 0;
1318 tree class_decl = NULL_TREE;
1319 bool is_compiled_class = false;
1321 /* We've already failed, don't try again. */
1322 if (TREE_CODE (class_or_name) == RECORD_TYPE
1323 && TYPE_DUMMY (class_or_name))
1324 return;
1326 /* class_or_name can be the name of the class we want to load */
1327 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1328 name = class_or_name;
1329 /* In some cases, it's a dependency that we process earlier that
1330 we though */
1331 else if (TREE_CODE (class_or_name) == TREE_LIST)
1332 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1333 /* Or it's a type in the making */
1334 else
1335 name = DECL_NAME (TYPE_NAME (class_or_name));
1337 class_decl = IDENTIFIER_CLASS_VALUE (name);
1338 if (class_decl != NULL_TREE)
1340 tree type = TREE_TYPE (class_decl);
1341 is_compiled_class
1342 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1343 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1346 saved = name;
1348 /* If flag_verify_invocations is unset, we don't try to load a class
1349 unless we're looking for Object (which is fixed by the ABI) or
1350 it's a class that we're going to compile. */
1351 if (flag_verify_invocations
1352 || class_or_name == object_type_node
1353 || is_compiled_class
1354 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1356 while (1)
1358 const char *separator;
1360 /* We've already loaded it. */
1361 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1363 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1364 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1365 break;
1368 if (read_class (name))
1369 break;
1371 /* We failed loading name. Now consider that we might be looking
1372 for an inner class. */
1373 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1374 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
1375 name = get_identifier_with_length (IDENTIFIER_POINTER (name),
1376 (separator
1377 - IDENTIFIER_POINTER (name)));
1378 /* Otherwise, we failed, we bail. */
1379 else
1380 break;
1384 /* have we found the class we're looking for? */
1385 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1386 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1387 class_loaded = type && CLASS_PARSED_P (type);
1391 if (!class_loaded)
1393 if (flag_verify_invocations || ! flag_indirect_dispatch)
1395 if (verbose)
1396 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1398 else if (verbose)
1400 /* This is just a diagnostic during testing, not a real problem. */
1401 if (!quiet_flag)
1402 warning (0, "cannot find file for class %s",
1403 IDENTIFIER_POINTER (saved));
1405 /* Fake it. */
1406 if (TREE_CODE (class_or_name) == RECORD_TYPE)
1408 set_super_info (0, class_or_name, object_type_node, 0);
1409 TYPE_DUMMY (class_or_name) = 1;
1410 /* We won't be able to output any debug info for this class. */
1411 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1417 /* Parse the .class file JCF. */
1419 static void
1420 jcf_parse (JCF* jcf)
1422 int i, code;
1424 bitmap_clear (field_offsets);
1426 if (jcf_parse_preamble (jcf) != 0)
1427 fatal_error ("not a valid Java .class file");
1428 code = jcf_parse_constant_pool (jcf);
1429 if (code != 0)
1430 fatal_error ("error while parsing constant pool");
1431 code = verify_constant_pool (jcf);
1432 if (code > 0)
1433 fatal_error ("error in constant pool entry #%d\n", code);
1435 jcf_parse_class (jcf);
1436 if (main_class == NULL_TREE)
1437 main_class = current_class;
1438 if (! quiet_flag && TYPE_NAME (current_class))
1439 fprintf (stderr, " %s %s",
1440 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
1441 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
1442 if (CLASS_PARSED_P (current_class))
1444 /* FIXME - where was first time */
1445 fatal_error ("reading class %s for the second time from %s",
1446 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1447 jcf->filename);
1449 CLASS_PARSED_P (current_class) = 1;
1451 for (i = 1; i < JPOOL_SIZE(jcf); i++)
1453 switch (JPOOL_TAG (jcf, i))
1455 case CONSTANT_Class:
1456 get_class_constant (jcf, i);
1457 break;
1461 code = jcf_parse_fields (jcf);
1462 if (code != 0)
1463 fatal_error ("error while parsing fields");
1464 code = jcf_parse_methods (jcf);
1465 if (code != 0)
1466 fatal_error ("error while parsing methods");
1467 code = jcf_parse_final_attributes (jcf);
1468 if (code != 0)
1469 fatal_error ("error while parsing final attributes");
1471 if (TYPE_REFLECTION_DATA (current_class))
1472 annotation_write_byte (JV_DONE_ATTR);
1474 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1476 /* The fields of class_type_node are already in correct order. */
1477 if (current_class != class_type_node && current_class != object_type_node)
1478 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1480 if (current_class == object_type_node)
1481 layout_class_methods (object_type_node);
1482 else
1483 VEC_safe_push (tree, gc, all_class_list, TYPE_NAME (current_class));
1486 /* If we came across inner classes, load them now. */
1487 static void
1488 load_inner_classes (tree cur_class)
1490 tree current;
1491 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
1492 current = TREE_CHAIN (current))
1494 tree name = DECL_NAME (TREE_PURPOSE (current));
1495 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1496 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1497 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
1498 load_class (name, 1);
1502 static void
1503 duplicate_class_warning (const char *filename)
1505 location_t warn_loc;
1506 linemap_add (line_table, LC_RENAME, 0, filename, 0);
1507 warn_loc = linemap_line_start (line_table, 0, 1);
1508 warning_at (warn_loc, 0, "duplicate class will only be compiled once");
1511 static void
1512 java_layout_seen_class_methods (void)
1514 unsigned start = 0;
1515 unsigned end = VEC_length (tree, all_class_list);
1517 while (1)
1519 unsigned ix;
1520 unsigned new_length;
1522 for (ix = start; ix != end; ix++)
1524 tree decl = VEC_index (tree, all_class_list, ix);
1525 tree cls = TREE_TYPE (decl);
1527 input_location = DECL_SOURCE_LOCATION (decl);
1529 if (! CLASS_LOADED_P (cls))
1530 load_class (cls, 0);
1532 layout_class_methods (cls);
1535 /* Note that new classes might have been added while laying out
1536 methods, changing the value of all_class_list. */
1537 new_length = VEC_length (tree, all_class_list);
1538 if (end != new_length)
1540 start = end;
1541 end = new_length;
1543 else
1544 break;
1548 static void
1549 parse_class_file (void)
1551 tree method;
1552 location_t save_location = input_location;
1554 java_layout_seen_class_methods ();
1556 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1558 /* Re-enter the current file. */
1559 expanded_location loc = expand_location (input_location);
1560 linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1562 file_start_location = input_location;
1563 (*debug_hooks->start_source_file) (input_line, input_filename);
1565 java_mark_class_local (current_class);
1567 gen_indirect_dispatch_tables (current_class);
1569 for (method = TYPE_METHODS (current_class);
1570 method != NULL_TREE; method = DECL_CHAIN (method))
1572 JCF *jcf = current_jcf;
1574 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
1575 continue;
1577 if (METHOD_NATIVE (method))
1579 tree arg;
1580 int decl_max_locals;
1582 if (! flag_jni)
1583 continue;
1584 /* We need to compute the DECL_MAX_LOCALS. We need to take
1585 the wide types into account too. */
1586 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
1587 arg != end_params_node;
1588 arg = TREE_CHAIN (arg), decl_max_locals += 1)
1590 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1591 decl_max_locals += 1;
1593 DECL_MAX_LOCALS (method) = decl_max_locals;
1594 start_java_method (method);
1595 give_name_to_locals (jcf);
1596 *get_stmts () = build_jni_stub (method);
1597 end_java_method ();
1598 continue;
1601 if (DECL_CODE_OFFSET (method) == 0)
1603 current_function_decl = method;
1604 error ("missing Code attribute");
1605 continue;
1608 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
1609 if (DECL_LINENUMBERS_OFFSET (method))
1611 int i;
1612 int min_line = 0;
1613 unsigned char *ptr;
1614 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1615 linenumber_count = i = JCF_readu2 (jcf);
1616 linenumber_table = ptr = jcf->read_ptr;
1618 for (ptr += 2; --i >= 0; ptr += 4)
1620 int line = GET_u2 (ptr);
1621 /* Set initial input_line to smallest linenumber.
1622 * Needs to be set before init_function_start. */
1623 if (min_line == 0 || line < min_line)
1624 min_line = line;
1626 if (min_line != 0)
1627 input_location = linemap_line_start (line_table, min_line, 1);
1629 else
1631 linenumber_table = NULL;
1632 linenumber_count = 0;
1635 start_java_method (method);
1637 note_instructions (jcf, method);
1639 give_name_to_locals (jcf);
1641 /* Bump up start_label_pc_this_method so we get a unique label number
1642 and reset highest_label_pc_this_method. */
1643 if (highest_label_pc_this_method >= 0)
1645 /* We adjust to the next multiple of 1000. This is just a frill
1646 so the last 3 digits of the label number match the bytecode
1647 offset, which might make debugging marginally more convenient. */
1648 start_label_pc_this_method
1649 = ((((start_label_pc_this_method + highest_label_pc_this_method)
1650 / 1000)
1651 + 1)
1652 * 1000);
1653 highest_label_pc_this_method = -1;
1656 /* Convert bytecode to trees. */
1657 expand_byte_code (jcf, method);
1659 end_java_method ();
1662 finish_class ();
1664 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
1665 input_location = save_location;
1668 static VEC(tree,gc) *predefined_filenames;
1670 void
1671 add_predefined_file (tree name)
1673 VEC_safe_push (tree, gc, predefined_filenames, name);
1677 predefined_filename_p (tree node)
1679 unsigned ix;
1680 tree f;
1682 FOR_EACH_VEC_ELT (tree, predefined_filenames, ix, f)
1683 if (f == node)
1684 return 1;
1686 return 0;
1689 /* Generate a function that does all static initialization for this
1690 translation unit. */
1692 static void
1693 java_emit_static_constructor (void)
1695 tree body = NULL;
1697 emit_register_classes (&body);
1698 write_resource_constructor (&body);
1700 if (body)
1702 tree name = get_identifier ("_Jv_global_static_constructor");
1704 tree decl
1705 = build_decl (input_location, FUNCTION_DECL, name,
1706 build_function_type_list (void_type_node, NULL_TREE));
1708 tree resdecl = build_decl (input_location,
1709 RESULT_DECL, NULL_TREE, void_type_node);
1710 DECL_ARTIFICIAL (resdecl) = 1;
1711 DECL_RESULT (decl) = resdecl;
1712 current_function_decl = decl;
1713 allocate_struct_function (decl, false);
1715 TREE_STATIC (decl) = 1;
1716 TREE_USED (decl) = 1;
1717 DECL_ARTIFICIAL (decl) = 1;
1718 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1719 DECL_SAVED_TREE (decl) = body;
1720 DECL_UNINLINABLE (decl) = 1;
1722 DECL_INITIAL (decl) = make_node (BLOCK);
1723 TREE_USED (DECL_INITIAL (decl)) = 1;
1725 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1726 java_genericize (decl);
1727 cgraph_finalize_function (decl, false);
1732 void
1733 java_parse_file (void)
1735 int filename_count = 0;
1736 location_t save_location = input_location;
1737 char *file_list = NULL, *list, *next;
1738 tree node;
1739 FILE *finput = NULL;
1740 int in_quotes = 0;
1741 unsigned ix;
1743 bitmap_obstack_initialize (&bit_obstack);
1744 field_offsets = BITMAP_ALLOC (&bit_obstack);
1746 if (flag_filelist_file)
1748 int avail = 2000;
1749 finput = fopen (main_input_filename, "r");
1750 if (finput == NULL)
1751 fatal_error ("can%'t open %s: %m", input_filename);
1752 list = XNEWVEC (char, avail);
1753 next = list;
1754 for (;;)
1756 int count;
1757 if (avail < 500)
1759 count = next - list;
1760 avail = 2 * (count + avail);
1761 list = XRESIZEVEC (char, list, avail);
1762 next = list + count;
1763 avail = avail - count;
1765 /* Subtract one to guarantee space for final '\0'. */
1766 count = fread (next, 1, avail - 1, finput);
1767 if (count == 0)
1769 if (! feof (finput))
1770 fatal_error ("error closing %s: %m", input_filename);
1771 *next = '\0';
1772 break;
1774 avail -= count;
1775 next += count;
1777 fclose (finput);
1778 finput = NULL;
1779 file_list = list;
1781 else
1782 list = CONST_CAST (char *, main_input_filename);
1784 while (list)
1786 for (next = list; ; )
1788 char ch = *next;
1789 if (flag_filelist_file && ! in_quotes
1790 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1791 || ch == '&') /* FIXME */)
1793 if (next == list)
1795 next++;
1796 list = next;
1797 continue;
1799 else
1801 *next++ = '\0';
1802 break;
1805 if (flag_filelist_file && ch == '"')
1807 in_quotes = ! in_quotes;
1808 *next++ = '\0';
1809 if (in_quotes)
1810 list = next;
1811 else
1812 break;
1814 if (ch == '\0')
1816 next = NULL;
1817 break;
1819 next++;
1822 /* Exclude .java files. */
1823 if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1825 /* Nothing. */
1827 else if (list[0])
1829 node = get_identifier (list);
1831 filename_count++;
1833 /* Exclude file that we see twice on the command line. */
1835 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1836 duplicate_class_warning (IDENTIFIER_POINTER (node));
1837 else
1839 build_translation_unit_decl (node);
1840 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1843 list = next;
1846 free (file_list);
1848 if (filename_count == 0)
1849 warning (0, "no input file specified");
1851 if (resource_name)
1853 const char *resource_filename;
1855 /* Only one resource file may be compiled at a time. */
1856 gcc_assert (VEC_length (tree, all_translation_units) == 1);
1858 resource_filename
1859 = IDENTIFIER_POINTER
1860 (DECL_NAME (VEC_index (tree, all_translation_units, 0)));
1861 compile_resource_file (resource_name, resource_filename);
1863 goto finish;
1866 current_jcf = main_jcf;
1867 FOR_EACH_VEC_ELT (tree, all_translation_units, ix, node)
1869 unsigned char magic_string[4];
1870 char *real_path;
1871 uint32 magic = 0;
1872 tree name = DECL_NAME (node);
1873 tree real_file;
1874 const char *filename = IDENTIFIER_POINTER (name);
1876 /* Skip already parsed files */
1877 real_path = lrealpath (filename);
1878 real_file = get_identifier (real_path);
1879 free (real_path);
1880 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1881 continue;
1883 /* Close previous descriptor, if any */
1884 if (finput && fclose (finput))
1885 fatal_error ("can%'t close input file %s: %m", main_input_filename);
1887 finput = fopen (filename, "rb");
1888 if (finput == NULL)
1889 fatal_error ("can%'t open %s: %m", filename);
1891 #ifdef IO_BUFFER_SIZE
1892 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1893 _IOFBF, IO_BUFFER_SIZE);
1894 #endif
1896 /* Figure what kind of file we're dealing with */
1897 if (fread (magic_string, 1, 4, finput) == 4)
1899 fseek (finput, 0L, SEEK_SET);
1900 magic = GET_u4 (magic_string);
1902 if (magic == 0xcafebabe)
1904 CLASS_FILE_P (node) = 1;
1905 current_jcf = ggc_alloc_cleared_JCF ();
1906 current_jcf->read_state = finput;
1907 current_jcf->filbuf = jcf_filbuf_from_stdio;
1908 jcf_parse (current_jcf);
1909 DECL_SOURCE_LOCATION (node) = file_start_location;
1910 TYPE_JCF (current_class) = current_jcf;
1911 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1913 /* We've already compiled this class. */
1914 duplicate_class_warning (filename);
1915 continue;
1917 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1918 TREE_TYPE (node) = current_class;
1920 else if (magic == (JCF_u4)ZIPMAGIC)
1922 main_jcf = ggc_alloc_cleared_JCF ();
1923 main_jcf->read_state = finput;
1924 main_jcf->filbuf = jcf_filbuf_from_stdio;
1925 linemap_add (line_table, LC_ENTER, false, filename, 0);
1926 input_location = linemap_line_start (line_table, 0, 1);
1927 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1928 fatal_error ("bad zip/jar file %s", filename);
1929 localToFile = SeenZipFiles;
1930 /* Register all the classes defined there. */
1931 process_zip_dir ((FILE *) main_jcf->read_state);
1932 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1933 parse_zip_file_entries ();
1935 else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1937 /* Ignore an empty input jar. */
1939 else
1941 gcc_unreachable ();
1942 #if 0
1943 java_push_parser_context ();
1944 java_parser_context_save_global ();
1946 parse_source_file_1 (real_file, filename, finput);
1947 java_parser_context_restore_global ();
1948 java_pop_parser_context (1);
1949 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
1950 #endif
1954 FOR_EACH_VEC_ELT (tree, all_translation_units, ix, node)
1956 input_location = DECL_SOURCE_LOCATION (node);
1957 if (CLASS_FILE_P (node))
1959 /* FIXME: These two flags really should be independent. We
1960 should be able to compile fully binary compatible, but
1961 with flag_verify_invocations on. */
1962 flag_verify_invocations = ! flag_indirect_dispatch;
1963 output_class = current_class = TREE_TYPE (node);
1965 current_jcf = TYPE_JCF (current_class);
1966 layout_class (current_class);
1967 load_inner_classes (current_class);
1968 parse_class_file ();
1969 JCF_FINISH (current_jcf);
1972 input_location = save_location;
1974 bitmap_obstack_release (&bit_obstack);
1976 finish:
1977 /* Arrange for any necessary initialization to happen. */
1978 java_emit_static_constructor ();
1979 gcc_assert (global_bindings_p ());
1983 /* Return the name of the class corresponding to the name of the file
1984 in this zip entry. The result is newly allocated using ALLOC. */
1985 static char *
1986 compute_class_name (struct ZipDirectory *zdir)
1988 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1989 char *class_name;
1990 int i;
1991 int filename_length = zdir->filename_length;
1993 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
1995 class_name_in_zip_dir += 2;
1996 filename_length -= 2;
1999 filename_length -= strlen (".class");
2000 class_name = XNEWVEC (char, filename_length + 1);
2001 memcpy (class_name, class_name_in_zip_dir, filename_length);
2002 class_name [filename_length] = '\0';
2004 for (i = 0; i < filename_length; i++)
2005 if (class_name[i] == '/')
2006 class_name[i] = '.';
2008 return class_name;
2011 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2012 if it is a property file of some sort. */
2013 static int
2014 classify_zip_file (struct ZipDirectory *zdir)
2016 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2018 if (zdir->filename_length > 6
2019 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2020 ".class", 6))
2021 return 1;
2023 /* For now we drop the manifest, but not other information. */
2024 if (zdir->filename_length == 20
2025 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
2026 return 0;
2028 /* Drop directory entries. */
2029 if (zdir->filename_length > 0
2030 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2031 return 0;
2033 return 2;
2036 /* Process all class entries found in the zip file. */
2037 static void
2038 parse_zip_file_entries (void)
2040 struct ZipDirectory *zdir;
2041 int i;
2043 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2044 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2046 tree klass;
2048 switch (classify_zip_file (zdir))
2050 case 0:
2051 continue;
2053 case 1:
2055 char *class_name = compute_class_name (zdir);
2056 int previous_alias_set = -1;
2057 klass = lookup_class (get_identifier (class_name));
2058 FREE (class_name);
2059 current_jcf = TYPE_JCF (klass);
2060 output_class = current_class = klass;
2062 /* This is a dummy class, and now we're compiling it for
2063 real. */
2064 gcc_assert (! TYPE_DUMMY (klass));
2066 /* This is for a corner case where we have a superclass
2067 but no superclass fields.
2069 This can happen if we earlier failed to lay out this
2070 class because its superclass was still in the process
2071 of being laid out; this occurs when we have recursive
2072 class dependencies via inner classes. We must record
2073 the previous alias set and restore it after laying out
2074 the class.
2076 FIXME: this really is a kludge. We should figure out a
2077 way to lay out the class properly before this
2078 happens. */
2079 if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2080 && integer_zerop (TYPE_SIZE (klass)))
2082 TYPE_SIZE (klass) = NULL_TREE;
2083 previous_alias_set = TYPE_ALIAS_SET (klass);
2084 TYPE_ALIAS_SET (klass) = -1;
2087 if (! CLASS_LOADED_P (klass))
2089 if (! CLASS_PARSED_P (klass))
2091 read_zip_member (current_jcf, zdir, localToFile);
2092 jcf_parse (current_jcf);
2094 layout_class (current_class);
2095 load_inner_classes (current_class);
2098 if (previous_alias_set != -1)
2099 TYPE_ALIAS_SET (klass) = previous_alias_set;
2101 if (TYPE_SIZE (current_class) != error_mark_node)
2103 parse_class_file ();
2104 free (current_jcf->buffer); /* No longer necessary */
2105 /* Note: there is a way to free this buffer right after a
2106 class seen in a zip file has been parsed. The idea is the
2107 set its jcf in such a way that buffer will be reallocated
2108 the time the code for the class will be generated. FIXME. */
2111 break;
2113 case 2:
2115 char *file_name, *class_name_in_zip_dir, *buffer;
2116 JCF *jcf;
2117 file_name = XNEWVEC (char, zdir->filename_length + 1);
2118 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2119 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2120 file_name[zdir->filename_length] = '\0';
2121 jcf = XNEW (JCF);
2122 JCF_ZERO (jcf);
2123 jcf->read_state = finput;
2124 jcf->filbuf = jcf_filbuf_from_stdio;
2125 jcf->classname = NULL;
2126 jcf->filename = file_name;
2127 jcf->zipd = zdir;
2129 if (read_zip_member (jcf, zdir, localToFile) < 0)
2130 fatal_error ("error while reading %s from zip file", file_name);
2132 buffer = XNEWVEC (char, zdir->filename_length + 1 +
2133 (jcf->buffer_end - jcf->buffer));
2134 strcpy (buffer, file_name);
2135 /* This is not a typo: we overwrite the trailing \0 of the
2136 file name; this is just how the data is laid out. */
2137 memcpy (buffer + zdir->filename_length,
2138 jcf->buffer, jcf->buffer_end - jcf->buffer);
2140 compile_resource_data (file_name, buffer,
2141 jcf->buffer_end - jcf->buffer);
2142 JCF_FINISH (jcf);
2143 free (jcf);
2144 free (buffer);
2146 break;
2148 default:
2149 gcc_unreachable ();
2154 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
2155 jcf up for further processing and link it to the created class. */
2157 static void
2158 process_zip_dir (FILE *finput)
2160 int i;
2161 ZipDirectory *zdir;
2163 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2164 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
2166 char *class_name, *file_name, *class_name_in_zip_dir;
2167 tree klass;
2168 JCF *jcf;
2170 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2172 /* Here we skip non-class files; we handle them later. */
2173 if (classify_zip_file (zdir) != 1)
2174 continue;
2176 class_name = compute_class_name (zdir);
2177 file_name = XNEWVEC (char, zdir->filename_length+1);
2178 jcf = ggc_alloc_cleared_JCF ();
2180 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2181 file_name [zdir->filename_length] = '\0';
2183 klass = lookup_class (get_identifier (class_name));
2185 if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
2187 /* We've already compiled this class. */
2188 duplicate_class_warning (file_name);
2189 continue;
2191 /* This function is only called when processing a zip file seen
2192 on the command line. */
2193 CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
2195 jcf->read_state = finput;
2196 jcf->filbuf = jcf_filbuf_from_stdio;
2197 jcf->classname = class_name;
2198 jcf->filename = file_name;
2199 jcf->zipd = zdir;
2201 TYPE_JCF (klass) = jcf;
2205 #include "gt-java-jcf-parse.h"
2206 #include "gtype-java.h"