1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003
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"
50 #ifdef HAVE_NL_LANGINFO
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) \
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]; \
66 (JCF)->cpool.data[INDEX].t = get_identifier (text); \
67 text[LENGTH] = save; \
68 JCF_SKIP (JCF, LENGTH); } while (0)
72 extern struct obstack temporary_obstack;
74 /* Set to nonzero value in order to emit class initialization code
75 before static field references. */
76 extern int always_initialize_class_p;
78 static GTY(()) tree parse_roots[3];
80 /* The FIELD_DECL for the current field. */
81 #define current_field parse_roots[0]
83 /* The METHOD_DECL for the current method. */
84 #define current_method parse_roots[1]
86 /* A list of file names. */
87 #define current_file_list parse_roots[2]
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, 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 set_source_filename (JCF *, int);
106 static void jcf_parse (struct JCF*);
107 static void load_inner_classes (tree);
109 /* Handle "SourceFile" attribute. */
112 set_source_filename (JCF *jcf, int index)
114 tree sfname_id = get_name_constant (jcf, index);
115 const char *sfname = IDENTIFIER_POINTER (sfname_id);
116 if (input_filename != NULL)
118 int old_len = strlen (input_filename);
119 int new_len = IDENTIFIER_LENGTH (sfname_id);
120 /* Use the current input_filename (derived from the class name)
121 if it has a directory prefix, but otherwise matches sfname. */
122 if (old_len > new_len
123 && strcmp (sfname, input_filename + old_len - new_len) == 0
124 && (input_filename[old_len - new_len - 1] == '/'
125 || input_filename[old_len - new_len - 1] == '\\'))
128 input_filename = sfname;
129 DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
130 if (current_class == main_class) main_input_filename = input_filename;
133 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
135 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
136 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
137 current_class = give_name_to_class (jcf, THIS); \
138 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
140 #define HANDLE_CLASS_INTERFACE(INDEX) \
141 add_interface (current_class, get_class_constant (jcf, INDEX))
143 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
144 { int sig_index = SIGNATURE; \
145 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
146 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
147 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
148 if ((ACCESS_FLAGS) & ACC_FINAL) \
149 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
152 #define HANDLE_END_FIELDS() \
153 (current_field = NULL_TREE)
155 #define HANDLE_CONSTANTVALUE(INDEX) \
156 { tree constant; int index = INDEX; \
157 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
158 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
159 constant = build_utf8_ref (name); \
162 constant = get_constant (jcf, index); \
163 set_constant_value (current_field, constant); }
165 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
166 (current_method = add_method (current_class, ACCESS_FLAGS, \
167 get_name_constant (jcf, NAME), \
168 get_name_constant (jcf, SIGNATURE)), \
169 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
170 DECL_LINENUMBERS_OFFSET (current_method) = 0)
172 #define HANDLE_END_METHODS() \
173 { current_method = NULL_TREE; }
175 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
176 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
177 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
178 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
179 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
181 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
183 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
184 JCF_SKIP (jcf, n * 10); }
186 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
188 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
189 JCF_SKIP (jcf, n * 4); }
191 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
194 tree list = DECL_FUNCTION_THROWS (current_method); \
197 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
198 list = tree_cons (NULL_TREE, thrown_class, list); \
200 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
203 /* Link seen inner classes to their outer context and register the
204 inner class to its outer context. They will be later loaded. */
205 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
206 handle_innerclass_attribute (COUNT, jcf)
208 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
210 /* Irrelevant decls should have been nullified by the END macros. \
211 We only handle the `Synthetic' attribute on method DECLs. \
212 DECL_ARTIFICIAL on fields is used for something else (See \
213 PUSH_FIELD in java-tree.h) */ \
214 if (current_method) \
215 DECL_ARTIFICIAL (current_method) = 1; \
218 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
220 if (current_class == object_type_node) \
221 jcf->right_zip = 1; \
224 #include "jcf-reader.c"
227 parse_signature (JCF *jcf, int sig_index)
229 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
230 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
233 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
234 JPOOL_UTF_LENGTH (jcf, sig_index));
238 get_constant (JCF *jcf, int index)
242 if (index <= 0 || index >= JPOOL_SIZE(jcf))
244 tag = JPOOL_TAG (jcf, index);
245 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
246 return jcf->cpool.data[index].t;
249 case CONSTANT_Integer:
251 jint num = JPOOL_INT(jcf, index);
252 value = build_int_2 (num, num < 0 ? -1 : 0);
253 TREE_TYPE (value) = int_type_node;
258 unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
259 HOST_WIDE_INT lo, hi;
260 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
261 num = JPOOL_UINT (jcf, index+1);
262 add_double (lo, hi, num, 0, &lo, &hi);
263 value = build_int_2 (lo, hi);
264 TREE_TYPE (value) = long_type_node;
265 force_fit_type (value, 0);
271 jint num = JPOOL_INT(jcf, index);
275 real_from_target_fmt (&d, &buf, &ieee_single_format);
276 value = build_real (float_type_node, d);
280 case CONSTANT_Double:
285 hi = JPOOL_UINT (jcf, index);
286 lo = JPOOL_UINT (jcf, index+1);
288 if (FLOAT_WORDS_BIG_ENDIAN)
289 buf[0] = hi, buf[1] = lo;
291 buf[0] = lo, buf[1] = hi;
293 real_from_target_fmt (&d, buf, &ieee_double_format);
294 value = build_real (double_type_node, d);
298 case CONSTANT_String:
300 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
301 const char *utf8_ptr = IDENTIFIER_POINTER (name);
302 int utf8_len = IDENTIFIER_LENGTH (name);
303 const unsigned char *utf8;
306 /* Check for a malformed Utf8 string. */
307 utf8 = (const unsigned char *) utf8_ptr;
311 int char_len = UT8_CHAR_LENGTH (*utf8);
312 if (char_len < 0 || char_len > 3 || char_len > i)
313 fatal_error ("bad string constant");
319 /* Allocate a new string value. */
320 value = build_string (utf8_len, utf8_ptr);
321 TREE_TYPE (value) = build_pointer_type (string_type_node);
327 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
328 jcf->cpool.data[index].t = value;
331 internal_error ("bad value constant type %d, index %d",
332 JPOOL_TAG (jcf, index), index);
336 get_name_constant (JCF *jcf, int index)
338 tree name = get_constant (jcf, index);
340 if (TREE_CODE (name) != IDENTIFIER_NODE)
346 /* Handle reading innerclass attributes. If a nonzero entry (denoting
347 a non anonymous entry) is found, We augment the inner class list of
348 the outer context with the newly resolved innerclass. */
351 handle_innerclass_attribute (int count, JCF *jcf)
356 /* Read inner_class_info_index. This may be 0 */
357 int icii = JCF_readu2 (jcf);
358 /* Read outer_class_info_index. If the innerclasses attribute
359 entry isn't a member (like an inner class) the value is 0. */
360 int ocii = JCF_readu2 (jcf);
361 /* Read inner_name_index. If the class we're dealing with is
362 an anonymous class, it must be 0. */
363 int ini = JCF_readu2 (jcf);
364 /* Read the access flag. */
365 int acc = JCF_readu2 (jcf);
366 /* If icii is 0, don't try to read the class. */
369 tree class = get_class_constant (jcf, icii);
370 tree decl = TYPE_NAME (class);
371 /* Skip reading further if ocii is null */
372 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
374 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
375 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
376 set_class_decl_access_flags (acc, decl);
377 DECL_CONTEXT (decl) = outer;
378 DECL_INNER_CLASS_LIST (outer) =
379 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
380 CLASS_COMPLETE_P (decl) = 1;
387 give_name_to_class (JCF *jcf, int i)
389 if (i <= 0 || i >= JPOOL_SIZE (jcf)
390 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
395 int j = JPOOL_USHORT1 (jcf, i);
396 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
397 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
398 JPOOL_UTF_LENGTH (jcf, j));
399 this_class = lookup_class (class_name);
400 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
402 if (main_input_filename == NULL && jcf == main_jcf)
403 main_input_filename = input_filename;
405 jcf->cpool.data[i].t = this_class;
406 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
411 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
414 get_class_constant (JCF *jcf, int i)
417 if (i <= 0 || i >= JPOOL_SIZE (jcf)
418 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
421 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
423 int name_index = JPOOL_USHORT1 (jcf, i);
424 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
425 const char *name = JPOOL_UTF_DATA (jcf, name_index);
426 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
428 if (name[0] == '[') /* Handle array "classes". */
429 type = TREE_TYPE (parse_signature_string (name, nlength));
432 tree cname = unmangle_classname (name, nlength);
433 type = lookup_class (cname);
435 jcf->cpool.data[i].t = type;
436 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
439 type = jcf->cpool.data[i].t;
443 /* Read a class with the fully qualified-name NAME.
444 Return 1 iff we read the requested file.
445 (It is still possible we failed if the file did not
446 define the class it is supposed to.) */
449 read_class (tree name)
452 tree icv, class = NULL_TREE;
453 tree save_current_class = current_class;
454 const char *save_input_filename = input_filename;
455 JCF *save_current_jcf = current_jcf;
457 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
459 class = TREE_TYPE (icv);
460 jcf = TYPE_JCF (class);
467 this_jcf.zipd = NULL;
469 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
476 if (current_jcf->java_source)
478 const char *filename = current_jcf->filename;
483 java_parser_context_save_global ();
484 java_push_parser_context ();
485 BUILD_FILENAME_IDENTIFIER_NODE (file, filename);
486 generate = IS_A_COMMAND_LINE_FILENAME_P (file);
487 if (wfl_operator == NULL_TREE)
488 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
489 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
490 input_filename = ggc_strdup (filename);
491 current_class = NULL_TREE;
492 current_function_decl = NULL_TREE;
493 if (!HAS_BEEN_ALREADY_PARSED_P (file))
495 if (!(finput = fopen (input_filename, "r")))
496 fatal_io_error ("can't reopen %s", input_filename);
497 parse_source_file_1 (file, finput);
498 parse_source_file_2 ();
499 parse_source_file_3 ();
501 fatal_io_error ("can't close %s", input_filename);
503 JCF_FINISH (current_jcf);
504 java_pop_parser_context (generate);
505 java_parser_context_restore_global ();
509 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
511 java_parser_context_save_global ();
512 java_push_parser_context ();
513 current_class = class;
514 input_filename = current_jcf->filename;
515 if (JCF_SEEN_IN_ZIP (current_jcf))
516 read_zip_member(current_jcf,
517 current_jcf->zipd, current_jcf->zipd->zipf);
518 jcf_parse (current_jcf);
519 class = current_class;
520 java_pop_parser_context (0);
521 java_parser_context_restore_global ();
523 layout_class (class);
524 load_inner_classes (class);
527 current_class = save_current_class;
528 input_filename = save_input_filename;
529 current_jcf = save_current_jcf;
533 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
534 called from the parser, otherwise it's a RECORD_TYPE node. If
535 VERBOSE is 1, print error message on failure to load a class. */
537 /* Replace calls to load_class by having callers call read_class directly
538 - and then perhaps rename read_class to load_class. FIXME */
541 load_class (tree class_or_name, int verbose)
546 /* class_or_name can be the name of the class we want to load */
547 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
548 name = class_or_name;
549 /* In some cases, it's a dependency that we process earlier that
551 else if (TREE_CODE (class_or_name) == TREE_LIST)
552 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
553 /* Or it's a type in the making */
555 name = DECL_NAME (TYPE_NAME (class_or_name));
562 if ((class_loaded = read_class (name)))
565 /* We failed loading name. Now consider that we might be looking
566 for a inner class. */
567 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
568 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
572 name = get_identifier (IDENTIFIER_POINTER (name));
575 /* Otherwise, we failed, we bail. */
580 if (!class_loaded && verbose)
581 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
584 /* Parse the .class file JCF. */
591 if (jcf_parse_preamble (jcf) != 0)
592 fatal_error ("not a valid Java .class file");
593 code = jcf_parse_constant_pool (jcf);
595 fatal_error ("error while parsing constant pool");
596 code = verify_constant_pool (jcf);
598 fatal_error ("error in constant pool entry #%d\n", code);
600 jcf_parse_class (jcf);
601 if (main_class == NULL_TREE)
602 main_class = current_class;
603 if (! quiet_flag && TYPE_NAME (current_class))
604 fprintf (stderr, " %s %s",
605 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
606 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
607 if (CLASS_PARSED_P (current_class))
609 /* FIXME - where was first time */
610 fatal_error ("reading class %s for the second time from %s",
611 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
614 CLASS_PARSED_P (current_class) = 1;
616 for (i = 1; i < JPOOL_SIZE(jcf); i++)
618 switch (JPOOL_TAG (jcf, i))
621 get_class_constant (jcf, i);
626 code = jcf_parse_fields (jcf);
628 fatal_error ("error while parsing fields");
629 code = jcf_parse_methods (jcf);
631 fatal_error ("error while parsing methods");
632 code = jcf_parse_final_attributes (jcf);
634 fatal_error ("error while parsing final attributes");
636 /* The fields of class_type_node are already in correct order. */
637 if (current_class != class_type_node && current_class != object_type_node)
638 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
640 if (current_class == object_type_node)
642 layout_class_methods (object_type_node);
643 /* If we don't have the right archive, emit a verbose warning.
644 If we're generating bytecode, emit the warning only if
645 -fforce-classes-archive-check was specified. */
647 && (!flag_emit_class_files || flag_force_classes_archive_check))
648 fatal_error ("the `java.lang.Object' that was found in `%s' 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);
651 all_class_list = tree_cons (NULL_TREE,
652 TYPE_NAME (current_class), all_class_list );
655 /* If we came across inner classes, load them now. */
657 load_inner_classes (tree cur_class)
660 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
661 current = TREE_CHAIN (current))
663 tree name = DECL_NAME (TREE_PURPOSE (current));
664 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
665 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
666 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
667 load_class (name, 1);
672 init_outgoing_cpool (void)
674 outgoing_cpool = ggc_alloc_cleared (sizeof (struct CPool));
678 parse_class_file (void)
681 const char *save_input_filename = input_filename;
682 int save_lineno = lineno;
684 java_layout_seen_class_methods ();
686 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
688 (*debug_hooks->start_source_file) (lineno, input_filename);
689 init_outgoing_cpool ();
691 /* Currently we always have to emit calls to _Jv_InitClass when
692 compiling from class files. */
693 always_initialize_class_p = 1;
695 for (field = TYPE_FIELDS (current_class);
696 field != NULL_TREE; field = TREE_CHAIN (field))
697 if (FIELD_STATIC (field))
698 DECL_EXTERNAL (field) = 0;
700 for (method = TYPE_METHODS (current_class);
701 method != NULL_TREE; method = TREE_CHAIN (method))
703 JCF *jcf = current_jcf;
705 if (METHOD_ABSTRACT (method))
708 if (METHOD_NATIVE (method))
715 /* We need to compute the DECL_MAX_LOCALS. We need to take
716 the wide types into account too. */
717 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
718 arg != end_params_node;
719 arg = TREE_CHAIN (arg), decl_max_locals += 1)
721 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
722 decl_max_locals += 1;
724 DECL_MAX_LOCALS (method) = decl_max_locals;
725 start_java_method (method);
726 give_name_to_locals (jcf);
727 expand_expr_stmt (build_jni_stub (method));
732 if (DECL_CODE_OFFSET (method) == 0)
734 current_function_decl = method;
735 error ("missing Code attribute");
740 if (DECL_LINENUMBERS_OFFSET (method))
743 register unsigned char *ptr;
744 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
745 linenumber_count = i = JCF_readu2 (jcf);
746 linenumber_table = ptr = jcf->read_ptr;
748 for (ptr += 2; --i >= 0; ptr += 4)
750 int line = GET_u2 (ptr);
751 /* Set initial lineno lineno to smallest linenumber.
752 * Needs to be set before init_function_start. */
753 if (lineno == 0 || line < lineno)
759 linenumber_table = NULL;
760 linenumber_count = 0;
763 start_java_method (method);
765 note_instructions (jcf, method);
767 give_name_to_locals (jcf);
769 /* Actually generate code. */
770 expand_byte_code (jcf, method);
775 if (flag_emit_class_files)
776 write_classfile (current_class);
780 (*debug_hooks->end_source_file) (save_lineno);
781 input_filename = save_input_filename;
782 lineno = save_lineno;
785 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
788 parse_source_file_1 (tree file, FILE *finput)
790 int save_error_count = java_error_count;
791 /* Mark the file as parsed */
792 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
794 jcf_dependency_add_file (input_filename, 0);
796 lang_init_source (1); /* Error msgs have no method prototypes */
798 /* There's no point in trying to find the current encoding unless we
799 are going to do something intelligent with it -- hence the test
801 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_NL_LANGINFO)
802 setlocale (LC_CTYPE, "");
803 if (current_encoding == NULL)
804 current_encoding = nl_langinfo (CODESET);
806 if (current_encoding == NULL || *current_encoding == '\0')
807 current_encoding = DEFAULT_ENCODING;
809 /* Initialize the parser */
810 java_init_lex (finput, current_encoding);
811 java_parse_abort_on_error ();
813 java_parse (); /* Parse and build partial tree nodes. */
814 java_parse_abort_on_error ();
817 /* Process a parsed source file, resolving names etc. */
820 parse_source_file_2 (void)
822 int save_error_count = java_error_count;
823 java_complete_class (); /* Parse unsatisfied class decl. */
824 java_parse_abort_on_error ();
828 parse_source_file_3 (void)
830 int save_error_count = java_error_count;
831 java_check_circular_reference (); /* Check on circular references */
832 java_parse_abort_on_error ();
833 java_fix_constructors (); /* Fix the constructors */
834 java_parse_abort_on_error ();
835 java_reorder_fields (); /* Reorder the fields */
839 add_predefined_file (tree name)
841 predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
845 predefined_filename_p (tree node)
849 for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
851 if (TREE_VALUE (iter) == node)
858 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
860 int filename_count = 0;
865 if (flag_filelist_file)
868 finput = fopen (input_filename, "r");
870 fatal_io_error ("can't open %s", input_filename);
871 list = xmalloc(avail);
879 avail = 2 * (count + avail);
880 list = xrealloc (list, avail);
882 avail = avail - count;
884 /* Subtract to to guarantee space for final '\0'. */
885 count = fread (next, 1, avail - 1, finput);
889 fatal_io_error ("error closing %s", input_filename);
900 list = xstrdup (input_filename);
904 for (next = list; ; )
907 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
908 || ch == '&' /* FIXME */)
936 int len = strlen (list);
938 obstack_grow0 (&temporary_obstack, list, len);
939 value = obstack_finish (&temporary_obstack);
943 /* Exclude file that we see twice on the command line. For
944 all files except {Class,Error,Object,RuntimeException,String,
945 Throwable}.java we can rely on maybe_get_identifier. For
946 these files, we need to do a linear search of
947 current_file_list. This search happens only for these
948 files, presumably only when we're recompiling libgcj. */
950 if ((id = maybe_get_identifier (value)))
952 if (predefined_filename_p (id))
955 for (c = current_file_list; c; c = TREE_CHAIN (c))
956 if (TREE_VALUE (c) == id)
965 const char *saved_input_filename = input_filename;
966 input_filename = value;
967 warning ("source file seen twice on command line and will be compiled only once");
968 input_filename = saved_input_filename;
972 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
973 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
974 current_file_list = tree_cons (NULL_TREE, node,
982 if (filename_count == 0)
983 warning ("no input file specified");
987 const char *resource_filename;
989 /* Only one resource file may be compiled at a time. */
990 assert (TREE_CHAIN (current_file_list) == NULL);
992 resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
993 compile_resource_file (resource_name, resource_filename);
998 current_jcf = main_jcf;
999 current_file_list = nreverse (current_file_list);
1000 for (node = current_file_list; node; node = TREE_CHAIN (node))
1002 unsigned char magic_string[4];
1004 tree name = TREE_VALUE (node);
1006 /* Skip already parsed files */
1007 if (HAS_BEEN_ALREADY_PARSED_P (name))
1010 /* Close previous descriptor, if any */
1011 if (finput && fclose (finput))
1012 fatal_io_error ("can't close input file %s", main_input_filename);
1014 finput = fopen (IDENTIFIER_POINTER (name), "rb");
1016 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
1018 #ifdef IO_BUFFER_SIZE
1019 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1020 _IOFBF, IO_BUFFER_SIZE);
1022 input_filename = IDENTIFIER_POINTER (name);
1024 /* Figure what kind of file we're dealing with */
1025 if (fread (magic_string, 1, 4, finput) == 4)
1027 fseek (finput, 0L, SEEK_SET);
1028 magic = GET_u4 (magic_string);
1030 if (magic == 0xcafebabe)
1032 CLASS_FILE_P (node) = 1;
1033 current_jcf = ggc_alloc (sizeof (JCF));
1034 JCF_ZERO (current_jcf);
1035 current_jcf->read_state = finput;
1036 current_jcf->filbuf = jcf_filbuf_from_stdio;
1037 jcf_parse (current_jcf);
1038 TYPE_JCF (current_class) = current_jcf;
1039 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1040 TREE_PURPOSE (node) = current_class;
1042 else if (magic == (JCF_u4)ZIPMAGIC)
1044 ZIP_FILE_P (node) = 1;
1045 main_jcf = ggc_alloc (sizeof (JCF));
1046 JCF_ZERO (main_jcf);
1047 main_jcf->read_state = finput;
1048 main_jcf->filbuf = jcf_filbuf_from_stdio;
1049 if (open_in_zip (main_jcf, input_filename, NULL, 0) < 0)
1050 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1051 localToFile = SeenZipFiles;
1052 /* Register all the classes defined there. */
1053 process_zip_dir (main_jcf->read_state);
1054 parse_zip_file_entries ();
1057 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1062 JAVA_FILE_P (node) = 1;
1063 java_push_parser_context ();
1064 java_parser_context_save_global ();
1065 parse_source_file_1 (name, finput);
1066 java_parser_context_restore_global ();
1067 java_pop_parser_context (1);
1071 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1073 input_filename = ctxp->filename;
1074 parse_source_file_2 ();
1077 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1079 input_filename = ctxp->filename;
1080 parse_source_file_3 ();
1083 for (node = current_file_list; node; node = TREE_CHAIN (node))
1085 input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1086 if (CLASS_FILE_P (node))
1088 current_class = TREE_PURPOSE (node);
1089 current_jcf = TYPE_JCF (current_class);
1090 layout_class (current_class);
1091 load_inner_classes (current_class);
1092 parse_class_file ();
1093 JCF_FINISH (current_jcf);
1096 input_filename = main_input_filename;
1098 java_expand_classes ();
1099 if (!java_report_errors () && !flag_syntax_only)
1101 emit_register_classes ();
1102 if (flag_indirect_dispatch)
1103 emit_offset_symbol_table ();
1106 write_resource_constructor ();
1109 /* Return the name of the class corresponding to the name of the file
1110 in this zip entry. The result is newly allocated using ALLOC. */
1112 compute_class_name (struct ZipDirectory *zdir)
1114 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1118 class_name = ALLOC (zdir->filename_length + 1 - 6);
1119 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length - 6);
1120 class_name [zdir->filename_length - 6] = '\0';
1121 for (j = 0; class_name[j]; ++j)
1122 class_name[j] = class_name[j] == '/' ? '.' : class_name[j];
1126 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
1127 if it is a property file of some sort. */
1129 classify_zip_file (struct ZipDirectory *zdir)
1131 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1133 if (zdir->filename_length > 6
1134 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
1138 /* For now we drop the manifest and other information. Maybe it
1139 would make more sense to compile it in? */
1140 if (zdir->filename_length > 8
1141 && !strncmp (class_name_in_zip_dir, "META-INF/", 9))
1144 /* Drop directory entries. */
1145 if (zdir->filename_length > 0
1146 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
1152 /* Process all class entries found in the zip file. */
1154 parse_zip_file_entries (void)
1156 struct ZipDirectory *zdir;
1159 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1160 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1164 switch (classify_zip_file (zdir))
1171 char *class_name = compute_class_name (zdir);
1172 class = lookup_class (get_identifier (class_name));
1174 current_jcf = TYPE_JCF (class);
1175 current_class = class;
1177 if (! CLASS_LOADED_P (class))
1179 if (! CLASS_PARSED_P (class))
1181 read_zip_member (current_jcf, zdir, localToFile);
1182 jcf_parse (current_jcf);
1184 layout_class (current_class);
1185 load_inner_classes (current_class);
1188 if (TYPE_SIZE (current_class) != error_mark_node)
1190 input_filename = current_jcf->filename;
1191 parse_class_file ();
1192 FREE (current_jcf->buffer); /* No longer necessary */
1193 /* Note: there is a way to free this buffer right after a
1194 class seen in a zip file has been parsed. The idea is the
1195 set its jcf in such a way that buffer will be reallocated
1196 the time the code for the class will be generated. FIXME. */
1203 char *file_name, *class_name_in_zip_dir, *buffer;
1205 file_name = ALLOC (zdir->filename_length + 1);
1206 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1207 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1208 file_name[zdir->filename_length] = '\0';
1209 jcf = ALLOC (sizeof (JCF));
1211 jcf->read_state = finput;
1212 jcf->filbuf = jcf_filbuf_from_stdio;
1213 jcf->java_source = 0;
1214 jcf->classname = NULL;
1215 jcf->filename = file_name;
1218 if (read_zip_member (jcf, zdir, localToFile) < 0)
1219 fatal_error ("error while reading %s from zip file", file_name);
1221 buffer = ALLOC (zdir->filename_length + 1 +
1222 (jcf->buffer_end - jcf->buffer));
1223 strcpy (buffer, file_name);
1224 /* This is not a typo: we overwrite the trailing \0 of the
1225 file name; this is just how the data is laid out. */
1226 memcpy (buffer + zdir->filename_length,
1227 jcf->buffer, jcf->buffer_end - jcf->buffer);
1229 compile_resource_data (file_name, buffer,
1230 jcf->buffer_end - jcf->buffer);
1243 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1244 jcf up for further processing and link it to the created class. */
1247 process_zip_dir (FILE *finput)
1252 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1253 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1255 char *class_name, *file_name, *class_name_in_zip_dir;
1259 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1261 /* Here we skip non-class files; we handle them later. */
1262 if (classify_zip_file (zdir) != 1)
1265 class_name = compute_class_name (zdir);
1266 file_name = ALLOC (zdir->filename_length+1);
1267 jcf = ggc_alloc (sizeof (JCF));
1270 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1271 file_name [zdir->filename_length] = '\0';
1273 class = lookup_class (get_identifier (class_name));
1275 jcf->read_state = finput;
1276 jcf->filbuf = jcf_filbuf_from_stdio;
1277 jcf->java_source = 0;
1278 jcf->classname = class_name;
1279 jcf->filename = file_name;
1282 TYPE_JCF (class) = jcf;
1286 /* Initialization. */
1289 init_jcf_parse (void)
1294 #include "gt-java-jcf-parse.h"