1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC 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 GNU CC 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 GNU CC; 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"
49 #ifdef HAVE_NL_LANGINFO
53 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
54 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
55 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
56 #define JPOOL_UTF_DATA(JCF, INDEX) \
57 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
58 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
60 unsigned char save; unsigned char *text; \
61 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
62 text = (JCF)->read_ptr; \
63 save = text[LENGTH]; \
65 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
66 text[LENGTH] = save; \
67 JCF_SKIP (JCF, LENGTH); } while (0)
71 extern struct obstack temporary_obstack;
73 /* Set to nonzero value in order to emit class initilization code
74 before static field references. */
75 extern int always_initialize_class_p;
77 static GTY(()) tree parse_roots[3];
79 /* The FIELD_DECL for the current field. */
80 #define current_field parse_roots[0]
82 /* The METHOD_DECL for the current method. */
83 #define current_method parse_roots[1]
85 /* A list of file names. */
86 #define current_file_list parse_roots[2]
88 /* The Java archive that provides main_class; the main input file. */
89 static struct JCF main_jcf[1];
91 static struct ZipFile *localToFile;
93 /* Declarations of some functions used here. */
94 static void handle_innerclass_attribute PARAMS ((int count, JCF *));
95 static tree give_name_to_class PARAMS ((JCF *jcf, int index));
96 static void parse_zip_file_entries PARAMS ((void));
97 static void process_zip_dir PARAMS ((FILE *));
98 static void parse_source_file_1 PARAMS ((tree, FILE *));
99 static void parse_source_file_2 PARAMS ((void));
100 static void parse_source_file_3 PARAMS ((void));
101 static void parse_class_file PARAMS ((void));
102 static void set_source_filename PARAMS ((JCF *, int));
103 static void ggc_mark_jcf PARAMS ((void**));
104 static void jcf_parse PARAMS ((struct JCF*));
105 static void load_inner_classes PARAMS ((tree));
107 /* Mark (for garbage collection) all the tree nodes that are
108 referenced from JCF's constant pool table. Do that only if the JCF
109 hasn't been marked finished. */
115 JCF *jcf = *(JCF**) elt;
116 if (jcf != NULL && !jcf->finished)
118 CPool *cpool = &jcf->cpool;
119 int size = CPOOL_COUNT(cpool);
121 for (index = 1; index < size; index++)
123 int tag = JPOOL_TAG (jcf, index);
124 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
125 ggc_mark_tree ((tree) cpool->data[index]);
130 /* Handle "SourceFile" attribute. */
133 set_source_filename (jcf, index)
137 tree sfname_id = get_name_constant (jcf, index);
138 const char *sfname = IDENTIFIER_POINTER (sfname_id);
139 if (input_filename != NULL)
141 int old_len = strlen (input_filename);
142 int new_len = IDENTIFIER_LENGTH (sfname_id);
143 /* Use the current input_filename (derived from the class name)
144 if it has a directory prefix, but otherwise matches sfname. */
145 if (old_len > new_len
146 && strcmp (sfname, input_filename + old_len - new_len) == 0
147 && (input_filename[old_len - new_len - 1] == '/'
148 || input_filename[old_len - new_len - 1] == '\\'))
151 input_filename = sfname;
152 DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
153 if (current_class == main_class) main_input_filename = input_filename;
156 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
158 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
159 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
160 current_class = give_name_to_class (jcf, THIS); \
161 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
163 #define HANDLE_CLASS_INTERFACE(INDEX) \
164 add_interface (current_class, get_class_constant (jcf, INDEX))
166 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
167 { int sig_index = SIGNATURE; \
168 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
169 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
170 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
171 if ((ACCESS_FLAGS) & ACC_FINAL) \
172 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
175 #define HANDLE_END_FIELDS() \
176 (current_field = NULL_TREE)
178 #define HANDLE_CONSTANTVALUE(INDEX) \
179 { tree constant; int index = INDEX; \
180 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
181 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
182 constant = build_utf8_ref (name); \
185 constant = get_constant (jcf, index); \
186 set_constant_value (current_field, constant); }
188 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
189 (current_method = add_method (current_class, ACCESS_FLAGS, \
190 get_name_constant (jcf, NAME), \
191 get_name_constant (jcf, SIGNATURE)), \
192 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
193 DECL_LINENUMBERS_OFFSET (current_method) = 0)
195 #define HANDLE_END_METHODS() \
196 { current_method = NULL_TREE; }
198 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
199 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
200 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
201 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
202 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
204 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
206 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
207 JCF_SKIP (jcf, n * 10); }
209 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
211 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
212 JCF_SKIP (jcf, n * 4); }
214 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
217 tree list = DECL_FUNCTION_THROWS (current_method); \
220 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
221 list = tree_cons (NULL_TREE, thrown_class, list); \
223 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
226 /* Link seen inner classes to their outer context and register the
227 inner class to its outer context. They will be later loaded. */
228 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
229 handle_innerclass_attribute (COUNT, jcf)
231 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
233 /* Irrelevant decls should have been nullified by the END macros. \
234 We only handle the `Synthetic' attribute on method DECLs. \
235 DECL_ARTIFICIAL on fields is used for something else (See \
236 PUSH_FIELD in java-tree.h) */ \
237 if (current_method) \
238 DECL_ARTIFICIAL (current_method) = 1; \
241 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
243 if (current_class == object_type_node) \
244 jcf->right_zip = 1; \
247 #include "jcf-reader.c"
250 parse_signature (jcf, sig_index)
254 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
255 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
258 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
259 JPOOL_UTF_LENGTH (jcf, sig_index));
263 get_constant (jcf, index)
269 if (index <= 0 || index >= JPOOL_SIZE(jcf))
271 tag = JPOOL_TAG (jcf, index);
272 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
273 return (tree) jcf->cpool.data[index];
276 case CONSTANT_Integer:
278 jint num = JPOOL_INT(jcf, index);
279 value = build_int_2 (num, num < 0 ? -1 : 0);
280 TREE_TYPE (value) = int_type_node;
285 unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
286 HOST_WIDE_INT lo, hi;
287 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
288 num = JPOOL_UINT (jcf, index+1);
289 add_double (lo, hi, num, 0, &lo, &hi);
290 value = build_int_2 (lo, hi);
291 TREE_TYPE (value) = long_type_node;
292 force_fit_type (value, 0);
298 jint num = JPOOL_INT(jcf, index);
302 real_from_target_fmt (&d, &buf, &ieee_single_format);
303 value = build_real (float_type_node, d);
307 case CONSTANT_Double:
312 hi = JPOOL_UINT (jcf, index);
313 lo = JPOOL_UINT (jcf, index+1);
315 if (FLOAT_WORDS_BIG_ENDIAN)
316 buf[0] = hi, buf[1] = lo;
318 buf[0] = lo, buf[1] = hi;
320 real_from_target_fmt (&d, buf, &ieee_double_format);
321 value = build_real (double_type_node, d);
325 case CONSTANT_String:
327 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
328 const char *utf8_ptr = IDENTIFIER_POINTER (name);
329 int utf8_len = IDENTIFIER_LENGTH (name);
330 const unsigned char *utf8;
333 /* Check for a malformed Utf8 string. */
334 utf8 = (const unsigned char *) utf8_ptr;
338 int char_len = UT8_CHAR_LENGTH (*utf8);
339 if (char_len < 0 || char_len > 3 || char_len > i)
340 fatal_error ("bad string constant");
346 /* Allocate a new string value. */
347 value = build_string (utf8_len, utf8_ptr);
348 TREE_TYPE (value) = build_pointer_type (string_type_node);
354 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
355 jcf->cpool.data [index] = (jword) value;
358 internal_error ("bad value constant type %d, index %d",
359 JPOOL_TAG (jcf, index), index);
363 get_name_constant (jcf, index)
367 tree name = get_constant (jcf, index);
369 if (TREE_CODE (name) != IDENTIFIER_NODE)
375 /* Handle reading innerclass attributes. If a nonzero entry (denoting
376 a non anonymous entry) is found, We augment the inner class list of
377 the outer context with the newly resolved innerclass. */
380 handle_innerclass_attribute (count, jcf)
387 /* Read inner_class_info_index. This may be 0 */
388 int icii = JCF_readu2 (jcf);
389 /* Read outer_class_info_index. If the innerclasses attribute
390 entry isn't a member (like an inner class) the value is 0. */
391 int ocii = JCF_readu2 (jcf);
392 /* Read inner_name_index. If the class we're dealing with is
393 an annonymous class, it must be 0. */
394 int ini = JCF_readu2 (jcf);
395 /* Read the access flag. */
396 int acc = JCF_readu2 (jcf);
397 /* If icii is 0, don't try to read the class. */
400 tree class = get_class_constant (jcf, icii);
401 tree decl = TYPE_NAME (class);
402 /* Skip reading further if ocii is null */
403 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
405 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
406 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
407 set_class_decl_access_flags (acc, decl);
408 DECL_CONTEXT (decl) = outer;
409 DECL_INNER_CLASS_LIST (outer) =
410 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
411 CLASS_COMPLETE_P (decl) = 1;
418 give_name_to_class (jcf, i)
422 if (i <= 0 || i >= JPOOL_SIZE (jcf)
423 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
428 int j = JPOOL_USHORT1 (jcf, i);
429 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
430 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
431 JPOOL_UTF_LENGTH (jcf, j));
432 this_class = lookup_class (class_name);
433 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
435 if (main_input_filename == NULL && jcf == main_jcf)
436 main_input_filename = input_filename;
438 jcf->cpool.data[i] = (jword) this_class;
439 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
444 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
447 get_class_constant (JCF *jcf , int i)
450 if (i <= 0 || i >= JPOOL_SIZE (jcf)
451 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
454 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
456 int name_index = JPOOL_USHORT1 (jcf, i);
457 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
458 const char *name = JPOOL_UTF_DATA (jcf, name_index);
459 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
461 if (name[0] == '[') /* Handle array "classes". */
462 type = TREE_TYPE (parse_signature_string (name, nlength));
465 tree cname = unmangle_classname (name, nlength);
466 type = lookup_class (cname);
468 jcf->cpool.data[i] = (jword) type;
469 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
472 type = (tree) jcf->cpool.data[i];
476 /* Read a class with the fully qualified-name NAME.
477 Return 1 iff we read the requested file.
478 (It is still possible we failed if the file did not
479 define the class it is supposed to.) */
486 tree icv, class = NULL_TREE;
487 tree save_current_class = current_class;
488 const char *save_input_filename = input_filename;
489 JCF *save_current_jcf = current_jcf;
491 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
493 class = TREE_TYPE (icv);
494 jcf = TYPE_JCF (class);
501 this_jcf.zipd = NULL;
503 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
510 if (current_jcf->java_source)
512 const char *filename = current_jcf->filename;
517 java_parser_context_save_global ();
518 java_push_parser_context ();
519 BUILD_FILENAME_IDENTIFIER_NODE (file, filename);
520 generate = IS_A_COMMAND_LINE_FILENAME_P (file);
521 if (wfl_operator == NULL_TREE)
522 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
523 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
524 input_filename = ggc_strdup (filename);
525 current_class = NULL_TREE;
526 current_function_decl = NULL_TREE;
527 if (!HAS_BEEN_ALREADY_PARSED_P (file))
529 if (!(finput = fopen (input_filename, "r")))
530 fatal_io_error ("can't reopen %s", input_filename);
531 parse_source_file_1 (file, finput);
532 parse_source_file_2 ();
533 parse_source_file_3 ();
535 fatal_io_error ("can't close %s", input_filename);
537 JCF_FINISH (current_jcf);
538 java_pop_parser_context (generate);
539 java_parser_context_restore_global ();
543 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
545 java_parser_context_save_global ();
546 java_push_parser_context ();
547 current_class = class;
548 input_filename = current_jcf->filename;
549 if (JCF_SEEN_IN_ZIP (current_jcf))
550 read_zip_member(current_jcf,
551 current_jcf->zipd, current_jcf->zipd->zipf);
552 jcf_parse (current_jcf);
553 class = current_class;
554 java_pop_parser_context (0);
555 java_parser_context_restore_global ();
557 layout_class (class);
558 load_inner_classes (class);
561 current_class = save_current_class;
562 input_filename = save_input_filename;
563 current_jcf = save_current_jcf;
567 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
568 called from the parser, otherwise it's a RECORD_TYPE node. If
569 VERBOSE is 1, print error message on failure to load a class. */
571 /* Replace calls to load_class by having callers call read_class directly
572 - and then perhaps rename read_class to load_class. FIXME */
575 load_class (class_or_name, verbose)
582 /* class_or_name can be the name of the class we want to load */
583 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
584 name = class_or_name;
585 /* In some cases, it's a dependency that we process earlier that
587 else if (TREE_CODE (class_or_name) == TREE_LIST)
588 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
589 /* Or it's a type in the making */
591 name = DECL_NAME (TYPE_NAME (class_or_name));
598 if ((class_loaded = read_class (name)))
601 /* We failed loading name. Now consider that we might be looking
602 for a inner class. */
603 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
604 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
608 name = get_identifier (IDENTIFIER_POINTER (name));
611 /* Otherwise, we failed, we bail. */
616 if (!class_loaded && verbose)
617 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
620 /* Parse the .class file JCF. */
628 if (jcf_parse_preamble (jcf) != 0)
629 fatal_error ("not a valid Java .class file");
630 code = jcf_parse_constant_pool (jcf);
632 fatal_error ("error while parsing constant pool");
633 code = verify_constant_pool (jcf);
635 fatal_error ("error in constant pool entry #%d\n", code);
637 jcf_parse_class (jcf);
638 if (main_class == NULL_TREE)
639 main_class = current_class;
640 if (! quiet_flag && TYPE_NAME (current_class))
641 fprintf (stderr, " %s %s",
642 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
643 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
644 if (CLASS_PARSED_P (current_class))
646 /* FIXME - where was first time */
647 fatal_error ("reading class %s for the second time from %s",
648 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
651 CLASS_PARSED_P (current_class) = 1;
653 for (i = 1; i < JPOOL_SIZE(jcf); i++)
655 switch (JPOOL_TAG (jcf, i))
658 get_class_constant (jcf, i);
663 code = jcf_parse_fields (jcf);
665 fatal_error ("error while parsing fields");
666 code = jcf_parse_methods (jcf);
668 fatal_error ("error while parsing methods");
669 code = jcf_parse_final_attributes (jcf);
671 fatal_error ("error while parsing final attributes");
673 /* The fields of class_type_node are already in correct order. */
674 if (current_class != class_type_node && current_class != object_type_node)
675 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
677 if (current_class == object_type_node)
679 layout_class_methods (object_type_node);
680 /* If we don't have the right archive, emit a verbose warning.
681 If we're generating bytecode, emit the warning only if
682 -fforce-classes-archive-check was specified. */
684 && (!flag_emit_class_files || flag_force_classes_archive_check))
685 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);
688 all_class_list = tree_cons (NULL_TREE,
689 TYPE_NAME (current_class), all_class_list );
692 /* If we came across inner classes, load them now. */
694 load_inner_classes (cur_class)
698 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
699 current = TREE_CHAIN (current))
701 tree name = DECL_NAME (TREE_PURPOSE (current));
702 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
703 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
704 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
705 load_class (name, 1);
710 init_outgoing_cpool ()
712 outgoing_cpool = xmalloc (sizeof (struct CPool));
713 memset (outgoing_cpool, 0, sizeof (struct CPool));
720 const char *save_input_filename = input_filename;
721 int save_lineno = lineno;
723 java_layout_seen_class_methods ();
725 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
727 (*debug_hooks->start_source_file) (lineno, input_filename);
728 init_outgoing_cpool ();
730 /* Currently we always have to emit calls to _Jv_InitClass when
731 compiling from class files. */
732 always_initialize_class_p = 1;
734 for (field = TYPE_FIELDS (current_class);
735 field != NULL_TREE; field = TREE_CHAIN (field))
736 if (FIELD_STATIC (field))
737 DECL_EXTERNAL (field) = 0;
739 for (method = TYPE_METHODS (current_class);
740 method != NULL_TREE; method = TREE_CHAIN (method))
742 JCF *jcf = current_jcf;
744 if (METHOD_ABSTRACT (method))
747 if (METHOD_NATIVE (method))
754 /* We need to compute the DECL_MAX_LOCALS. We need to take
755 the wide types into account too. */
756 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
757 arg != end_params_node;
758 arg = TREE_CHAIN (arg), decl_max_locals += 1)
760 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
761 decl_max_locals += 1;
763 DECL_MAX_LOCALS (method) = decl_max_locals;
764 start_java_method (method);
765 give_name_to_locals (jcf);
766 expand_expr_stmt (build_jni_stub (method));
771 if (DECL_CODE_OFFSET (method) == 0)
773 current_function_decl = method;
774 error ("missing Code attribute");
779 if (DECL_LINENUMBERS_OFFSET (method))
782 register unsigned char *ptr;
783 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
784 linenumber_count = i = JCF_readu2 (jcf);
785 linenumber_table = ptr = jcf->read_ptr;
787 for (ptr += 2; --i >= 0; ptr += 4)
789 int line = GET_u2 (ptr);
790 /* Set initial lineno lineno to smallest linenumber.
791 * Needs to be set before init_function_start. */
792 if (lineno == 0 || line < lineno)
798 linenumber_table = NULL;
799 linenumber_count = 0;
802 start_java_method (method);
804 note_instructions (jcf, method);
806 give_name_to_locals (jcf);
808 /* Actually generate code. */
809 expand_byte_code (jcf, method);
814 if (flag_emit_class_files)
815 write_classfile (current_class);
819 (*debug_hooks->end_source_file) (save_lineno);
820 input_filename = save_input_filename;
821 lineno = save_lineno;
824 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
827 parse_source_file_1 (file, finput)
831 int save_error_count = java_error_count;
832 /* Mark the file as parsed */
833 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
835 jcf_dependency_add_file (input_filename, 0);
837 lang_init_source (1); /* Error msgs have no method prototypes */
839 /* There's no point in trying to find the current encoding unless we
840 are going to do something intelligent with it -- hence the test
842 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_NL_LANGINFO)
843 setlocale (LC_CTYPE, "");
844 if (current_encoding == NULL)
845 current_encoding = nl_langinfo (CODESET);
847 if (current_encoding == NULL || *current_encoding == '\0')
848 current_encoding = DEFAULT_ENCODING;
850 /* Initialize the parser */
851 java_init_lex (finput, current_encoding);
852 java_parse_abort_on_error ();
854 java_parse (); /* Parse and build partial tree nodes. */
855 java_parse_abort_on_error ();
858 /* Process a parsed source file, resolving names etc. */
861 parse_source_file_2 ()
863 int save_error_count = java_error_count;
864 java_complete_class (); /* Parse unsatisfied class decl. */
865 java_parse_abort_on_error ();
869 parse_source_file_3 ()
871 int save_error_count = java_error_count;
872 java_check_circular_reference (); /* Check on circular references */
873 java_parse_abort_on_error ();
874 java_fix_constructors (); /* Fix the constructors */
875 java_parse_abort_on_error ();
876 java_reorder_fields (); /* Reorder the fields */
880 add_predefined_file (name)
883 predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
887 predefined_filename_p (node)
892 for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
894 if (TREE_VALUE (iter) == node)
901 java_parse_file (set_yydebug)
902 int set_yydebug ATTRIBUTE_UNUSED;
904 int filename_count = 0;
909 if (flag_filelist_file)
912 finput = fopen (input_filename, "r");
914 fatal_io_error ("can't open %s", input_filename);
915 list = xmalloc(avail);
923 avail = 2 * (count + avail);
924 list = xrealloc (list, avail);
926 avail = avail - count;
928 /* Subtract to to guarantee space for final '\0'. */
929 count = fread (next, 1, avail - 1, finput);
933 fatal_io_error ("error closing %s", input_filename);
944 list = xstrdup (input_filename);
948 for (next = list; ; )
951 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
952 || ch == '&' /* FIXME */)
980 int len = strlen (list);
982 obstack_grow0 (&temporary_obstack, list, len);
983 value = obstack_finish (&temporary_obstack);
987 /* Exclude file that we see twice on the command line. For
988 all files except {Class,Error,Object,RuntimeException,String,
989 Throwable}.java we can rely on maybe_get_identifier. For
990 these files, we need to do a linear search of
991 current_file_list. This search happens only for these
992 files, presumably only when we're recompiling libgcj. */
994 if ((id = maybe_get_identifier (value)))
996 if (predefined_filename_p (id))
999 for (c = current_file_list; c; c = TREE_CHAIN (c))
1000 if (TREE_VALUE (c) == id)
1009 const char *saved_input_filename = input_filename;
1010 input_filename = value;
1011 warning ("source file seen twice on command line and will be compiled only once");
1012 input_filename = saved_input_filename;
1016 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
1017 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1018 current_file_list = tree_cons (NULL_TREE, node,
1026 if (filename_count == 0)
1027 warning ("no input file specified");
1031 const char *resource_filename;
1033 /* Only one resource file may be compiled at a time. */
1034 assert (TREE_CHAIN (current_file_list) == NULL);
1036 resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
1037 compile_resource_file (resource_name, resource_filename);
1042 current_jcf = main_jcf;
1043 current_file_list = nreverse (current_file_list);
1044 for (node = current_file_list; node; node = TREE_CHAIN (node))
1046 unsigned char magic_string[4];
1048 tree name = TREE_VALUE (node);
1050 /* Skip already parsed files */
1051 if (HAS_BEEN_ALREADY_PARSED_P (name))
1054 /* Close previous descriptor, if any */
1055 if (finput && fclose (finput))
1056 fatal_io_error ("can't close input file %s", main_input_filename);
1058 finput = fopen (IDENTIFIER_POINTER (name), "rb");
1060 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
1062 #ifdef IO_BUFFER_SIZE
1063 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1064 _IOFBF, IO_BUFFER_SIZE);
1066 input_filename = IDENTIFIER_POINTER (name);
1068 /* Figure what kind of file we're dealing with */
1069 if (fread (magic_string, 1, 4, finput) == 4)
1071 fseek (finput, 0L, SEEK_SET);
1072 magic = GET_u4 (magic_string);
1074 if (magic == 0xcafebabe)
1076 CLASS_FILE_P (node) = 1;
1077 current_jcf = ALLOC (sizeof (JCF));
1078 JCF_ZERO (current_jcf);
1079 current_jcf->read_state = finput;
1080 current_jcf->filbuf = jcf_filbuf_from_stdio;
1081 jcf_parse (current_jcf);
1082 TYPE_JCF (current_class) = current_jcf;
1083 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1084 TREE_PURPOSE (node) = current_class;
1086 else if (magic == (JCF_u4)ZIPMAGIC)
1088 ZIP_FILE_P (node) = 1;
1089 JCF_ZERO (main_jcf);
1090 main_jcf->read_state = finput;
1091 main_jcf->filbuf = jcf_filbuf_from_stdio;
1092 if (open_in_zip (main_jcf, input_filename, NULL, 0) < 0)
1093 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1094 localToFile = SeenZipFiles;
1095 /* Register all the class defined there. */
1096 process_zip_dir (main_jcf->read_state);
1097 parse_zip_file_entries ();
1100 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1105 JAVA_FILE_P (node) = 1;
1106 java_push_parser_context ();
1107 java_parser_context_save_global ();
1108 parse_source_file_1 (name, finput);
1109 java_parser_context_restore_global ();
1110 java_pop_parser_context (1);
1114 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1116 input_filename = ctxp->filename;
1117 parse_source_file_2 ();
1120 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1122 input_filename = ctxp->filename;
1123 parse_source_file_3 ();
1126 for (node = current_file_list; node; node = TREE_CHAIN (node))
1128 input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1129 if (CLASS_FILE_P (node))
1131 current_class = TREE_PURPOSE (node);
1132 current_jcf = TYPE_JCF (current_class);
1133 layout_class (current_class);
1134 load_inner_classes (current_class);
1135 parse_class_file ();
1136 JCF_FINISH (current_jcf);
1139 input_filename = main_input_filename;
1141 java_expand_classes ();
1142 if (!java_report_errors () && !flag_syntax_only)
1144 emit_register_classes ();
1145 if (flag_indirect_dispatch)
1146 emit_offset_symbol_table ();
1150 /* Process all class entries found in the zip file. */
1152 parse_zip_file_entries (void)
1154 struct ZipDirectory *zdir;
1157 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1158 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1162 /* We don't need to consider those files. */
1163 if (!zdir->size || !zdir->filename_offset)
1166 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
1167 current_jcf = TYPE_JCF (class);
1168 current_class = class;
1170 if ( !CLASS_LOADED_P (class))
1172 if (! CLASS_PARSED_P (class))
1174 read_zip_member(current_jcf, zdir, localToFile);
1175 jcf_parse (current_jcf);
1177 layout_class (current_class);
1178 load_inner_classes (current_class);
1181 if (TYPE_SIZE (current_class) != error_mark_node)
1183 input_filename = current_jcf->filename;
1184 parse_class_file ();
1185 FREE (current_jcf->buffer); /* No longer necessary */
1186 /* Note: there is a way to free this buffer right after a
1187 class seen in a zip file has been parsed. The idea is the
1188 set its jcf in such a way that buffer will be reallocated
1189 the time the code for the class will be generated. FIXME. */
1194 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1195 jcf up for further processing and link it to the created class. */
1198 process_zip_dir (FILE *finput)
1203 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1204 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1206 char *class_name, *file_name, *class_name_in_zip_dir;
1211 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1213 /* We choose to not to process entries with a zero size or entries
1214 not bearing the .class extension. */
1215 if (!zdir->size || !zdir->filename_offset ||
1216 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
1219 /* So it will be skipped in parse_zip_file_entries */
1224 class_name = ALLOC (zdir->filename_length+1-6);
1225 file_name = ALLOC (zdir->filename_length+1);
1226 jcf = ALLOC (sizeof (JCF));
1229 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1230 class_name [zdir->filename_length-6] = '\0';
1231 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1232 file_name [zdir->filename_length] = '\0';
1234 for (j=0; class_name[j]; j++)
1235 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1237 /* Yes, we write back the true class name into the zip directory. */
1238 strcpy (class_name_in_zip_dir, class_name);
1239 zdir->filename_length = j;
1240 class = lookup_class (get_identifier (class_name));
1242 jcf->read_state = finput;
1243 jcf->filbuf = jcf_filbuf_from_stdio;
1244 jcf->java_source = 0;
1245 jcf->classname = class_name;
1246 jcf->filename = file_name;
1249 TYPE_JCF (class) = jcf;
1253 /* Initialization. */
1258 /* Register roots with the garbage collector. */
1259 ggc_add_root (¤t_jcf, 1, sizeof (JCF), (void (*)(void *))ggc_mark_jcf);
1264 #include "gt-java-jcf-parse.h"