1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 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 2, or (at your option)
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 COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
30 #include "coretypes.h"
36 #include "java-except.h"
38 #include "java-tree.h"
51 #ifdef HAVE_LANGINFO_CODESET
55 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
56 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
57 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
58 #define JPOOL_UTF_DATA(JCF, INDEX) \
59 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
60 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
62 unsigned char save; unsigned char *text; \
63 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
64 text = (JCF)->read_ptr; \
65 save = text[LENGTH]; \
67 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
68 text[LENGTH] = save; \
69 JCF_SKIP (JCF, LENGTH); } while (0)
73 extern struct obstack temporary_obstack;
75 static GTY(()) tree parse_roots[3];
77 /* The FIELD_DECL for the current field. */
78 #define current_field parse_roots[0]
80 /* The METHOD_DECL for the current method. */
81 #define current_method parse_roots[1]
83 /* A list of TRANSLATION_UNIT_DECLs for the files to be compiled. */
84 #define current_file_list parse_roots[2]
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 static struct ZipFile *localToFile;
94 /* Declarations of some functions used here. */
95 static void handle_innerclass_attribute (int count, JCF *);
96 static tree give_name_to_class (JCF *jcf, int index);
97 static char *compute_class_name (struct ZipDirectory *zdir);
98 static int classify_zip_file (struct ZipDirectory *zdir);
99 static void parse_zip_file_entries (void);
100 static void process_zip_dir (FILE *);
101 static void parse_source_file_1 (tree, const char *, FILE *);
102 static void parse_source_file_2 (void);
103 static void parse_source_file_3 (void);
104 static void parse_class_file (void);
105 static void handle_deprecated (void);
106 static void set_source_filename (JCF *, int);
107 static void jcf_parse (struct JCF*);
108 static void load_inner_classes (tree);
110 /* Handle "Deprecated" attribute. */
112 handle_deprecated (void)
114 if (current_field != NULL_TREE)
115 FIELD_DEPRECATED (current_field) = 1;
116 else if (current_method != NULL_TREE)
117 METHOD_DEPRECATED (current_method) = 1;
118 else if (current_class != NULL_TREE)
119 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
122 /* Shouldn't happen. */
127 /* Handle "SourceFile" attribute. */
130 set_source_filename (JCF *jcf, int index)
132 tree sfname_id = get_name_constant (jcf, index);
133 const char *sfname = IDENTIFIER_POINTER (sfname_id);
134 const char *old_filename = input_filename;
135 int new_len = IDENTIFIER_LENGTH (sfname_id);
136 if (old_filename != NULL)
138 int old_len = strlen (old_filename);
139 /* Use the current input_filename (derived from the class name)
140 if it has a directory prefix, but otherwise matches sfname. */
141 if (old_len > new_len
142 && strcmp (sfname, old_filename + old_len - new_len) == 0
143 && (old_filename[old_len - new_len - 1] == '/'
144 || old_filename[old_len - new_len - 1] == '\\'))
147 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
149 const char *class_name
150 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
151 char *dot = strrchr (class_name, '.');
154 /* Length of prefix, not counting final dot. */
155 int i = dot - class_name;
156 /* Concatenate current package prefix with new sfname. */
157 char *buf = xmalloc (i + new_len + 2); /* Space for '.' and '\0'. */
158 strcpy (buf + i + 1, sfname);
159 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
160 Note we start at the end with the final package dot. */
163 char c = class_name[i];
168 sfname_id = get_identifier (buf);
170 sfname = IDENTIFIER_POINTER (sfname_id);
174 #ifdef USE_MAPPED_LOCATION
175 line_table.maps[line_table.used-1].to_file = sfname;
177 input_filename = sfname;
178 DECL_SOURCE_LOCATION (TYPE_NAME (current_class)) = input_location;
179 file_start_location = input_location;
181 if (current_class == main_class) main_input_filename = sfname;
184 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
186 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
187 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
188 output_class = current_class = give_name_to_class (jcf, THIS); \
189 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
191 #define HANDLE_CLASS_INTERFACE(INDEX) \
192 add_interface (current_class, get_class_constant (jcf, INDEX))
194 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
195 { int sig_index = SIGNATURE; \
196 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
197 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
198 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
199 if ((ACCESS_FLAGS) & ACC_FINAL) \
200 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
203 #define HANDLE_END_FIELDS() \
204 (current_field = NULL_TREE)
206 #define HANDLE_CONSTANTVALUE(INDEX) \
207 { tree constant; int index = INDEX; \
208 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
209 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
210 constant = build_utf8_ref (name); \
213 constant = get_constant (jcf, index); \
214 set_constant_value (current_field, constant); }
216 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
217 (current_method = add_method (current_class, ACCESS_FLAGS, \
218 get_name_constant (jcf, NAME), \
219 get_name_constant (jcf, SIGNATURE)), \
220 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
221 DECL_LINENUMBERS_OFFSET (current_method) = 0)
223 #define HANDLE_END_METHODS() \
224 { current_method = NULL_TREE; }
226 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
227 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
228 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
229 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
230 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
232 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
234 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
235 JCF_SKIP (jcf, n * 10); }
237 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
239 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
240 JCF_SKIP (jcf, n * 4); }
242 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
245 tree list = DECL_FUNCTION_THROWS (current_method); \
248 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
249 list = tree_cons (NULL_TREE, thrown_class, list); \
251 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
254 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
256 /* Link seen inner classes to their outer context and register the
257 inner class to its outer context. They will be later loaded. */
258 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
259 handle_innerclass_attribute (COUNT, jcf)
261 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
263 /* Irrelevant decls should have been nullified by the END macros. \
264 We only handle the `Synthetic' attribute on method DECLs. \
265 DECL_ARTIFICIAL on fields is used for something else (See \
266 PUSH_FIELD in java-tree.h) */ \
267 if (current_method) \
268 DECL_ARTIFICIAL (current_method) = 1; \
271 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
273 if (current_class == object_type_node) \
274 jcf->right_zip = 1; \
277 #include "jcf-reader.c"
280 parse_signature (JCF *jcf, int sig_index)
282 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
283 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
286 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
287 JPOOL_UTF_LENGTH (jcf, sig_index));
291 get_constant (JCF *jcf, int index)
295 if (index <= 0 || index >= JPOOL_SIZE(jcf))
297 tag = JPOOL_TAG (jcf, index);
298 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
299 return jcf->cpool.data[index].t;
302 case CONSTANT_Integer:
304 jint num = JPOOL_INT(jcf, index);
305 value = build_int_cst (int_type_node, num);
310 unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
311 unsigned HOST_WIDE_INT lo;
314 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
315 num = JPOOL_UINT (jcf, index+1);
316 add_double (lo, hi, num, 0, &lo, &hi);
317 value = build_int_cst_wide (long_type_node, lo, hi);
318 value = force_fit_type (value, 0, false, false);
324 jint num = JPOOL_INT(jcf, index);
328 real_from_target_fmt (&d, &buf, &ieee_single_format);
329 value = build_real (float_type_node, d);
333 case CONSTANT_Double:
338 hi = JPOOL_UINT (jcf, index);
339 lo = JPOOL_UINT (jcf, index+1);
341 if (FLOAT_WORDS_BIG_ENDIAN)
342 buf[0] = hi, buf[1] = lo;
344 buf[0] = lo, buf[1] = hi;
346 real_from_target_fmt (&d, buf, &ieee_double_format);
347 value = build_real (double_type_node, d);
351 case CONSTANT_String:
353 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
354 const char *utf8_ptr = IDENTIFIER_POINTER (name);
355 int utf8_len = IDENTIFIER_LENGTH (name);
356 const unsigned char *utf8;
359 /* Check for a malformed Utf8 string. */
360 utf8 = (const unsigned char *) utf8_ptr;
364 int char_len = UT8_CHAR_LENGTH (*utf8);
365 if (char_len < 0 || char_len > 3 || char_len > i)
366 fatal_error ("bad string constant");
372 /* Allocate a new string value. */
373 value = build_string (utf8_len, utf8_ptr);
374 TREE_TYPE (value) = build_pointer_type (string_type_node);
380 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
381 jcf->cpool.data[index].t = value;
384 internal_error ("bad value constant type %d, index %d",
385 JPOOL_TAG (jcf, index), index);
389 get_name_constant (JCF *jcf, int index)
391 tree name = get_constant (jcf, index);
393 if (TREE_CODE (name) != IDENTIFIER_NODE)
399 /* Handle reading innerclass attributes. If a nonzero entry (denoting
400 a non anonymous entry) is found, We augment the inner class list of
401 the outer context with the newly resolved innerclass. */
404 handle_innerclass_attribute (int count, JCF *jcf)
409 /* Read inner_class_info_index. This may be 0 */
410 int icii = JCF_readu2 (jcf);
411 /* Read outer_class_info_index. If the innerclasses attribute
412 entry isn't a member (like an inner class) the value is 0. */
413 int ocii = JCF_readu2 (jcf);
414 /* Read inner_name_index. If the class we're dealing with is
415 an anonymous class, it must be 0. */
416 int ini = JCF_readu2 (jcf);
417 /* Read the access flag. */
418 int acc = JCF_readu2 (jcf);
419 /* If icii is 0, don't try to read the class. */
422 tree class = get_class_constant (jcf, icii);
423 tree decl = TYPE_NAME (class);
424 /* Skip reading further if ocii is null */
425 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
427 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
428 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
429 set_class_decl_access_flags (acc, decl);
430 DECL_CONTEXT (decl) = outer;
431 DECL_INNER_CLASS_LIST (outer) =
432 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
433 CLASS_COMPLETE_P (decl) = 1;
440 give_name_to_class (JCF *jcf, int i)
442 if (i <= 0 || i >= JPOOL_SIZE (jcf)
443 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
448 int j = JPOOL_USHORT1 (jcf, i);
449 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
450 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
451 JPOOL_UTF_LENGTH (jcf, j));
452 this_class = lookup_class (class_name);
453 #ifdef USE_MAPPED_LOCATION
455 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
456 const char *sfname = IDENTIFIER_POINTER (source_name);
457 linemap_add (&line_table, LC_ENTER, false, sfname, 0);
458 input_location = linemap_line_start (&line_table, 0, 1);
459 file_start_location = input_location;
460 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
461 if (main_input_filename == NULL && jcf == main_jcf)
462 main_input_filename = sfname;
465 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (this_class));
466 if (main_input_filename == NULL && jcf == main_jcf)
467 main_input_filename = input_filename;
470 jcf->cpool.data[i].t = this_class;
471 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
476 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
479 get_class_constant (JCF *jcf, int i)
482 if (i <= 0 || i >= JPOOL_SIZE (jcf)
483 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
486 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
488 int name_index = JPOOL_USHORT1 (jcf, i);
489 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
490 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
491 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
493 if (name[0] == '[') /* Handle array "classes". */
494 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
497 tree cname = unmangle_classname (name, nlength);
498 type = lookup_class (cname);
500 jcf->cpool.data[i].t = type;
501 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
504 type = jcf->cpool.data[i].t;
508 /* Read a class with the fully qualified-name NAME.
509 Return 1 iff we read the requested file.
510 (It is still possible we failed if the file did not
511 define the class it is supposed to.) */
514 read_class (tree name)
517 tree icv, class = NULL_TREE;
518 tree save_current_class = current_class;
519 tree save_output_class = output_class;
520 location_t save_location = input_location;
521 JCF *save_current_jcf = current_jcf;
523 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
525 class = TREE_TYPE (icv);
526 jcf = TYPE_JCF (class);
533 this_jcf.zipd = NULL;
535 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
542 if (current_jcf->java_source)
544 const char *filename = current_jcf->filename;
545 tree given_file, real_file;
549 java_parser_context_save_global ();
550 java_push_parser_context ();
552 given_file = get_identifier (filename);
553 filename = IDENTIFIER_POINTER (given_file);
554 real_file = get_identifier (lrealpath (filename));
556 generate = IS_A_COMMAND_LINE_FILENAME_P (given_file);
557 output_class = current_class = NULL_TREE;
558 current_function_decl = NULL_TREE;
560 if (! HAS_BEEN_ALREADY_PARSED_P (real_file))
562 if (! (finput = fopen (filename, "r")))
563 fatal_error ("can't reopen %s: %m", filename);
565 parse_source_file_1 (real_file, filename, finput);
566 parse_source_file_2 ();
567 parse_source_file_3 ();
570 fatal_error ("can't close %s: %m", input_filename);
571 #ifdef USE_MAPPED_LOCATION
572 linemap_add (&line_table, LC_LEAVE, false, NULL, 0);
575 JCF_FINISH (current_jcf);
576 java_pop_parser_context (generate);
577 java_parser_context_restore_global ();
581 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
583 java_parser_context_save_global ();
584 java_push_parser_context ();
585 output_class = current_class = class;
586 ctxp->save_location = input_location;
587 if (JCF_SEEN_IN_ZIP (current_jcf))
588 read_zip_member(current_jcf,
589 current_jcf->zipd, current_jcf->zipd->zipf);
590 jcf_parse (current_jcf);
591 /* Parsing might change the class, in which case we have to
592 put it back where we found it. */
593 if (current_class != class && icv != NULL_TREE)
594 TREE_TYPE (icv) = current_class;
595 class = current_class;
596 java_pop_parser_context (0);
597 java_parser_context_restore_global ();
599 layout_class (class);
600 load_inner_classes (class);
603 output_class = save_output_class;
604 current_class = save_current_class;
605 input_location = save_location;
606 current_jcf = save_current_jcf;
610 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
611 called from the parser, otherwise it's a RECORD_TYPE node. If
612 VERBOSE is 1, print error message on failure to load a class. */
614 load_class (tree class_or_name, int verbose)
617 int class_loaded = 0;
618 tree class_decl = NULL_TREE;
619 bool is_compiled_class = false;
621 /* We've already failed, don't try again. */
622 if (TREE_CODE (class_or_name) == RECORD_TYPE
623 && TYPE_DUMMY (class_or_name))
626 /* class_or_name can be the name of the class we want to load */
627 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
628 name = class_or_name;
629 /* In some cases, it's a dependency that we process earlier that
631 else if (TREE_CODE (class_or_name) == TREE_LIST)
632 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
633 /* Or it's a type in the making */
635 name = DECL_NAME (TYPE_NAME (class_or_name));
637 class_decl = IDENTIFIER_CLASS_VALUE (name);
638 if (class_decl != NULL_TREE)
640 tree type = TREE_TYPE (class_decl);
642 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
643 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
646 /* If the class is from source code, then it must already be loaded. */
647 class_decl = IDENTIFIER_CLASS_VALUE (name);
648 if (class_decl && CLASS_FROM_SOURCE_P (TREE_TYPE (class_decl)))
653 /* If flag_verify_invocations is unset, we don't try to load a class
654 unless we're looking for Object (which is fixed by the ABI) or
655 it's a class that we're going to compile. */
656 if (flag_verify_invocations
657 || class_or_name == object_type_node
659 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
665 /* We've already loaded it. */
666 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
668 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
669 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
673 if (read_class (name))
676 /* We failed loading name. Now consider that we might be looking
677 for a inner class. */
678 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
679 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
683 name = get_identifier (IDENTIFIER_POINTER (name));
686 /* Otherwise, we failed, we bail. */
692 /* have we found the class we're looking for? */
693 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
694 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
695 class_loaded = type && CLASS_PARSED_P (type);
701 if (flag_verify_invocations || ! flag_indirect_dispatch
702 || flag_emit_class_files)
705 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
709 /* This is just a diagnostic during testing, not a real problem. */
711 warning("cannot find file for class %s",
712 IDENTIFIER_POINTER (saved));
715 if (TREE_CODE (class_or_name) == RECORD_TYPE)
717 set_super_info (0, class_or_name, object_type_node, 0);
718 TYPE_DUMMY (class_or_name) = 1;
719 /* We won't be able to output any debug info for this class. */
720 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
726 /* Parse the .class file JCF. */
733 if (jcf_parse_preamble (jcf) != 0)
734 fatal_error ("not a valid Java .class file");
735 code = jcf_parse_constant_pool (jcf);
737 fatal_error ("error while parsing constant pool");
738 code = verify_constant_pool (jcf);
740 fatal_error ("error in constant pool entry #%d\n", code);
742 jcf_parse_class (jcf);
743 if (main_class == NULL_TREE)
744 main_class = current_class;
745 if (! quiet_flag && TYPE_NAME (current_class))
746 fprintf (stderr, " %s %s",
747 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
748 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
749 if (CLASS_PARSED_P (current_class))
751 /* FIXME - where was first time */
752 fatal_error ("reading class %s for the second time from %s",
753 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
756 CLASS_PARSED_P (current_class) = 1;
758 for (i = 1; i < JPOOL_SIZE(jcf); i++)
760 switch (JPOOL_TAG (jcf, i))
763 get_class_constant (jcf, i);
768 code = jcf_parse_fields (jcf);
770 fatal_error ("error while parsing fields");
771 code = jcf_parse_methods (jcf);
773 fatal_error ("error while parsing methods");
774 code = jcf_parse_final_attributes (jcf);
776 fatal_error ("error while parsing final attributes");
777 #ifdef USE_MAPPED_LOCATION
778 linemap_add (&line_table, LC_LEAVE, false, NULL, 0);
781 /* The fields of class_type_node are already in correct order. */
782 if (current_class != class_type_node && current_class != object_type_node)
783 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
785 if (current_class == object_type_node)
787 layout_class_methods (object_type_node);
788 /* If we don't have the right archive, emit a verbose warning.
789 If we're generating bytecode, emit the warning only if
790 -fforce-classes-archive-check was specified. */
792 && (!flag_emit_class_files || flag_force_classes_archive_check))
793 fatal_error ("the %<java.lang.Object%> that was found in %qs didn't have the special zero-length %<gnu.gcj.gcj-compiled%> attribute. This generally means that your classpath is incorrectly set. Use %<info gcj \"Input Options\"%> to see the info page describing how to set the classpath", jcf->filename);
796 all_class_list = tree_cons (NULL_TREE,
797 TYPE_NAME (current_class), all_class_list );
800 /* If we came across inner classes, load them now. */
802 load_inner_classes (tree cur_class)
805 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
806 current = TREE_CHAIN (current))
808 tree name = DECL_NAME (TREE_PURPOSE (current));
809 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
810 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
811 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
812 load_class (name, 1);
817 parse_class_file (void)
820 location_t save_location = input_location;
822 java_layout_seen_class_methods ();
824 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
825 file_start_location = input_location;
826 (*debug_hooks->start_source_file) (input_line, input_filename);
828 gen_indirect_dispatch_tables (current_class);
830 java_mark_class_local (current_class);
832 for (method = TYPE_METHODS (current_class);
833 method != NULL_TREE; method = TREE_CHAIN (method))
835 JCF *jcf = current_jcf;
837 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
840 if (METHOD_NATIVE (method))
847 /* We need to compute the DECL_MAX_LOCALS. We need to take
848 the wide types into account too. */
849 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
850 arg != end_params_node;
851 arg = TREE_CHAIN (arg), decl_max_locals += 1)
853 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
854 decl_max_locals += 1;
856 DECL_MAX_LOCALS (method) = decl_max_locals;
857 start_java_method (method);
858 give_name_to_locals (jcf);
859 *get_stmts () = build_jni_stub (method);
864 if (DECL_CODE_OFFSET (method) == 0)
866 current_function_decl = method;
867 error ("missing Code attribute");
871 input_location = file_start_location;
872 if (DECL_LINENUMBERS_OFFSET (method))
877 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
878 linenumber_count = i = JCF_readu2 (jcf);
879 linenumber_table = ptr = jcf->read_ptr;
881 for (ptr += 2; --i >= 0; ptr += 4)
883 int line = GET_u2 (ptr);
884 /* Set initial input_line to smallest linenumber.
885 * Needs to be set before init_function_start. */
886 if (min_line == 0 || line < min_line)
889 #ifdef USE_MAPPED_LOCATION
891 input_location = linemap_line_start (&line_table, min_line, 1);
894 input_line = min_line;
899 linenumber_table = NULL;
900 linenumber_count = 0;
903 start_java_method (method);
905 note_instructions (jcf, method);
907 give_name_to_locals (jcf);
909 /* Convert bytecode to trees. */
910 expand_byte_code (jcf, method);
915 if (flag_emit_class_files)
916 write_classfile (current_class);
920 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
921 input_location = save_location;
924 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
927 parse_source_file_1 (tree real_file, const char *filename, FILE *finput)
929 int save_error_count = java_error_count;
931 /* Mark the file as parsed. */
932 HAS_BEEN_ALREADY_PARSED_P (real_file) = 1;
934 lang_init_source (1); /* Error msgs have no method prototypes */
936 /* There's no point in trying to find the current encoding unless we
937 are going to do something intelligent with it -- hence the test
939 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
940 setlocale (LC_CTYPE, "");
941 if (current_encoding == NULL)
942 current_encoding = nl_langinfo (CODESET);
944 if (current_encoding == NULL || *current_encoding == '\0')
945 current_encoding = DEFAULT_ENCODING;
947 #ifdef USE_MAPPED_LOCATION
948 linemap_add (&line_table, LC_ENTER, false, filename, 0);
949 input_location = linemap_line_start (&line_table, 0, 125);
951 input_filename = filename;
954 ctxp->file_start_location = input_location;
955 ctxp->filename = filename;
957 jcf_dependency_add_file (input_filename, 0);
959 /* Initialize the parser */
960 java_init_lex (finput, current_encoding);
961 java_parse_abort_on_error ();
963 java_parse (); /* Parse and build partial tree nodes. */
964 java_parse_abort_on_error ();
967 /* Process a parsed source file, resolving names etc. */
970 parse_source_file_2 (void)
972 int save_error_count = java_error_count;
973 flag_verify_invocations = true;
974 java_complete_class (); /* Parse unsatisfied class decl. */
975 java_parse_abort_on_error ();
979 parse_source_file_3 (void)
981 int save_error_count = java_error_count;
982 java_check_circular_reference (); /* Check on circular references */
983 java_parse_abort_on_error ();
984 java_fix_constructors (); /* Fix the constructors */
985 java_parse_abort_on_error ();
986 java_reorder_fields (); /* Reorder the fields */
990 add_predefined_file (tree name)
992 predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
996 predefined_filename_p (tree node)
1000 for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
1002 if (TREE_VALUE (iter) == node)
1008 /* Generate a function that does all static initialization for this
1009 translation unit. */
1012 java_emit_static_constructor (void)
1016 emit_register_classes (&body);
1017 write_resource_constructor (&body);
1020 cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
1024 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
1026 int filename_count = 0;
1027 location_t save_location = input_location;
1030 FILE *finput = NULL;
1033 if (flag_filelist_file)
1036 finput = fopen (main_input_filename, "r");
1038 fatal_error ("can't open %s: %m", input_filename);
1039 list = xmalloc(avail);
1046 count = next - list;
1047 avail = 2 * (count + avail);
1048 list = xrealloc (list, avail);
1049 next = list + count;
1050 avail = avail - count;
1052 /* Subtract to to guarantee space for final '\0'. */
1053 count = fread (next, 1, avail - 1, finput);
1056 if (! feof (finput))
1057 fatal_error ("error closing %s: %m", input_filename);
1068 list = (char *) main_input_filename;
1072 for (next = list; ; )
1075 if (flag_filelist_file && ! in_quotes
1076 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1077 || ch == '&') /* FIXME */)
1091 if (flag_filelist_file && ch == '"')
1093 in_quotes = ! in_quotes;
1110 node = get_identifier (list);
1114 /* Exclude file that we see twice on the command line. */
1116 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1118 location_t warn_loc;
1119 #ifdef USE_MAPPED_LOCATION
1120 linemap_add (&line_table, LC_RENAME, 0,
1121 IDENTIFIER_POINTER (node), 0);
1122 warn_loc = linemap_line_start (&line_table, 0, 1);
1124 warn_loc.file = IDENTIFIER_POINTER (node);
1127 warning ("%Hsource file seen twice on command line and "
1128 "will be compiled only once", &warn_loc);
1132 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, node, NULL);
1133 TREE_CHAIN (file_decl) = current_file_list;
1134 current_file_list = file_decl;
1135 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1141 if (filename_count == 0)
1142 warning ("no input file specified");
1146 const char *resource_filename;
1148 /* Only one resource file may be compiled at a time. */
1149 assert (TREE_CHAIN (current_file_list) == NULL);
1151 resource_filename = IDENTIFIER_POINTER (DECL_NAME (current_file_list));
1152 compile_resource_file (resource_name, resource_filename);
1157 current_jcf = main_jcf;
1158 current_file_list = nreverse (current_file_list);
1159 for (node = current_file_list; node; node = TREE_CHAIN (node))
1161 unsigned char magic_string[4];
1163 tree name = DECL_NAME (node);
1165 const char *filename = IDENTIFIER_POINTER (name);
1167 /* Skip already parsed files */
1168 real_file = get_identifier (lrealpath (filename));
1169 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1172 /* Close previous descriptor, if any */
1173 if (finput && fclose (finput))
1174 fatal_error ("can't close input file %s: %m", main_input_filename);
1176 finput = fopen (filename, "rb");
1178 fatal_error ("can't open %s: %m", filename);
1180 #ifdef IO_BUFFER_SIZE
1181 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1182 _IOFBF, IO_BUFFER_SIZE);
1185 /* Figure what kind of file we're dealing with */
1186 if (fread (magic_string, 1, 4, finput) == 4)
1188 fseek (finput, 0L, SEEK_SET);
1189 magic = GET_u4 (magic_string);
1191 if (magic == 0xcafebabe)
1193 CLASS_FILE_P (node) = 1;
1194 current_jcf = ggc_alloc (sizeof (JCF));
1195 JCF_ZERO (current_jcf);
1196 current_jcf->read_state = finput;
1197 current_jcf->filbuf = jcf_filbuf_from_stdio;
1198 jcf_parse (current_jcf);
1199 DECL_SOURCE_LOCATION (node) = file_start_location;
1200 TYPE_JCF (current_class) = current_jcf;
1201 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1202 TREE_TYPE (node) = current_class;
1204 else if (magic == (JCF_u4)ZIPMAGIC)
1206 main_jcf = ggc_alloc (sizeof (JCF));
1207 JCF_ZERO (main_jcf);
1208 main_jcf->read_state = finput;
1209 main_jcf->filbuf = jcf_filbuf_from_stdio;
1210 #ifdef USE_MAPPED_LOCATION
1211 linemap_add (&line_table, LC_ENTER, false, filename, 0);
1212 input_location = linemap_line_start (&line_table, 0, 1);
1214 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1215 fatal_error ("bad zip/jar file %s", filename);
1216 localToFile = SeenZipFiles;
1217 /* Register all the classes defined there. */
1218 process_zip_dir (main_jcf->read_state);
1219 #ifdef USE_MAPPED_LOCATION
1220 linemap_add (&line_table, LC_LEAVE, false, NULL, 0);
1222 parse_zip_file_entries ();
1225 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1230 java_push_parser_context ();
1231 java_parser_context_save_global ();
1233 parse_source_file_1 (real_file, filename, finput);
1234 java_parser_context_restore_global ();
1235 java_pop_parser_context (1);
1236 #ifdef USE_MAPPED_LOCATION
1237 linemap_add (&line_table, LC_LEAVE, false, NULL, 0);
1242 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1244 input_location = ctxp->file_start_location;
1245 parse_source_file_2 ();
1248 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1250 input_location = ctxp->file_start_location;
1251 parse_source_file_3 ();
1254 for (node = current_file_list; node; node = TREE_CHAIN (node))
1256 input_location = DECL_SOURCE_LOCATION (node);
1257 if (CLASS_FILE_P (node))
1259 /* FIXME: These two flags really should be independent. We
1260 should be able to compile fully binary compatible, but
1261 with flag_verify_invocations on. */
1262 flag_verify_invocations = ! flag_indirect_dispatch;
1263 output_class = current_class = TREE_TYPE (node);
1265 current_jcf = TYPE_JCF (current_class);
1266 layout_class (current_class);
1267 load_inner_classes (current_class);
1268 parse_class_file ();
1269 JCF_FINISH (current_jcf);
1272 input_location = save_location;
1274 java_expand_classes ();
1275 if (java_report_errors () || flag_syntax_only)
1278 /* Expand all classes compiled from source. */
1279 java_finish_classes ();
1282 /* Arrange for any necessary initialization to happen. */
1283 java_emit_static_constructor ();
1285 /* Only finalize the compilation unit after we've told cgraph which
1286 functions have their addresses stored. */
1287 cgraph_finalize_compilation_unit ();
1292 /* Return the name of the class corresponding to the name of the file
1293 in this zip entry. The result is newly allocated using ALLOC. */
1295 compute_class_name (struct ZipDirectory *zdir)
1297 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1300 int filename_length = zdir->filename_length;
1302 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
1304 class_name_in_zip_dir += 2;
1305 filename_length -= 2;
1308 filename_length -= strlen (".class");
1309 class_name = ALLOC (filename_length + 1);
1310 memcpy (class_name, class_name_in_zip_dir, filename_length);
1311 class_name [filename_length] = '\0';
1313 for (i = 0; i < filename_length; i++)
1314 if (class_name[i] == '/')
1315 class_name[i] = '.';
1320 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
1321 if it is a property file of some sort. */
1323 classify_zip_file (struct ZipDirectory *zdir)
1325 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1327 if (zdir->filename_length > 6
1328 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
1332 /* For now we drop the manifest, but not other information. */
1333 if (zdir->filename_length == 20
1334 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
1337 /* Drop directory entries. */
1338 if (zdir->filename_length > 0
1339 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
1345 /* Process all class entries found in the zip file. */
1347 parse_zip_file_entries (void)
1349 struct ZipDirectory *zdir;
1352 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1353 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1357 switch (classify_zip_file (zdir))
1364 char *class_name = compute_class_name (zdir);
1365 class = lookup_class (get_identifier (class_name));
1367 current_jcf = TYPE_JCF (class);
1368 output_class = current_class = class;
1370 if (TYPE_DUMMY (class))
1372 /* This is a dummy class, and now we're compiling it
1377 /* This is for a corner case where we have a superclass
1378 but no superclass fields.
1380 This can happen if we earlier failed to lay out this
1381 class because its superclass was still in the process
1382 of being laid out; this occurs when we have recursive
1383 class dependencies via inner classes. Setting
1384 TYPE_SIZE to null here causes CLASS_LOADED_P to return
1385 false, so layout_class() will be called again. */
1386 if (TYPE_SIZE (class) && CLASSTYPE_SUPER (class)
1387 && integer_zerop (TYPE_SIZE (class)))
1388 TYPE_SIZE (class) = NULL_TREE;
1390 if (! CLASS_LOADED_P (class))
1392 if (! CLASS_PARSED_P (class))
1394 read_zip_member (current_jcf, zdir, localToFile);
1395 jcf_parse (current_jcf);
1397 layout_class (current_class);
1398 load_inner_classes (current_class);
1401 if (TYPE_SIZE (current_class) != error_mark_node)
1403 parse_class_file ();
1404 FREE (current_jcf->buffer); /* No longer necessary */
1405 /* Note: there is a way to free this buffer right after a
1406 class seen in a zip file has been parsed. The idea is the
1407 set its jcf in such a way that buffer will be reallocated
1408 the time the code for the class will be generated. FIXME. */
1415 char *file_name, *class_name_in_zip_dir, *buffer;
1417 file_name = ALLOC (zdir->filename_length + 1);
1418 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1419 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1420 file_name[zdir->filename_length] = '\0';
1421 jcf = ALLOC (sizeof (JCF));
1423 jcf->read_state = finput;
1424 jcf->filbuf = jcf_filbuf_from_stdio;
1425 jcf->java_source = 0;
1426 jcf->classname = NULL;
1427 jcf->filename = file_name;
1430 if (read_zip_member (jcf, zdir, localToFile) < 0)
1431 fatal_error ("error while reading %s from zip file", file_name);
1433 buffer = ALLOC (zdir->filename_length + 1 +
1434 (jcf->buffer_end - jcf->buffer));
1435 strcpy (buffer, file_name);
1436 /* This is not a typo: we overwrite the trailing \0 of the
1437 file name; this is just how the data is laid out. */
1438 memcpy (buffer + zdir->filename_length,
1439 jcf->buffer, jcf->buffer_end - jcf->buffer);
1441 compile_resource_data (file_name, buffer,
1442 jcf->buffer_end - jcf->buffer);
1455 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1456 jcf up for further processing and link it to the created class. */
1459 process_zip_dir (FILE *finput)
1464 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1465 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1467 char *class_name, *file_name, *class_name_in_zip_dir;
1471 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1473 /* Here we skip non-class files; we handle them later. */
1474 if (classify_zip_file (zdir) != 1)
1477 class_name = compute_class_name (zdir);
1478 file_name = ALLOC (zdir->filename_length+1);
1479 jcf = ggc_alloc (sizeof (JCF));
1482 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1483 file_name [zdir->filename_length] = '\0';
1485 class = lookup_class (get_identifier (class_name));
1487 jcf->read_state = finput;
1488 jcf->filbuf = jcf_filbuf_from_stdio;
1489 jcf->java_source = 0;
1490 jcf->classname = class_name;
1491 jcf->filename = file_name;
1494 TYPE_JCF (class) = jcf;
1498 /* Initialization. */
1501 init_jcf_parse (void)
1506 #include "gt-java-jcf-parse.h"