1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Per Bothner <bothner@cygnus.com> */
32 #include "java-except.h"
34 #include "java-tree.h"
36 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
37 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
38 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
39 #define JPOOL_UTF_DATA(JCF, INDEX) \
40 ((unsigned char*) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
41 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
43 unsigned char save; unsigned char *text; \
44 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
45 text = (JCF)->read_ptr; \
46 save = text[LENGTH]; \
48 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
49 text[LENGTH] = save; \
50 JCF_SKIP (JCF, LENGTH); } while (0)
54 extern struct obstack *saveable_obstack;
55 extern struct obstack temporary_obstack;
56 extern struct obstack permanent_obstack;
58 /* The class we are currently processing. */
59 tree current_class = NULL_TREE;
61 /* The class we started with. */
62 tree main_class = NULL_TREE;
64 /* List of all class DECL seen so far. */
65 tree all_class_list = NULL_TREE;
67 /* The FIELD_DECL for the current field. */
68 static tree current_field = NULL_TREE;
70 /* The METHOD_DECL for the current method. */
71 static tree current_method = NULL_TREE;
73 /* Declarations of some functions used here. */
74 static tree give_name_to_class PROTO ((JCF *jcf, int index));
75 void parse_zip_file_entries PROTO (());
76 void process_zip_dir PROTO (());
77 static void parse_source_file PROTO ((tree));
78 static void jcf_parse_source PROTO ((JCF *));
80 /* Handle "SourceFile" attribute. */
83 set_source_filename (jcf, index)
87 tree sfname_id = get_name_constant (jcf, index);
88 char *sfname = IDENTIFIER_POINTER (sfname_id);
89 if (input_filename != NULL)
91 int old_len = strlen (input_filename);
92 int new_len = IDENTIFIER_LENGTH (sfname_id);
93 /* Use the current input_filename (derived from the class name)
94 if it has a directory prefix, but otherwise matches sfname. */
96 && strcmp (sfname, input_filename + old_len - new_len) == 0
97 && (input_filename[old_len - new_len - 1] == '/'
98 || input_filename[old_len - new_len - 1] == '\\'))
101 input_filename = sfname;
102 DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
103 if (current_class == main_class) main_input_filename = input_filename;
106 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
108 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
109 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
110 current_class = give_name_to_class (jcf, THIS); \
111 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
113 #define HANDLE_CLASS_INTERFACE(INDEX) \
114 add_interface (current_class, get_class_constant (jcf, INDEX))
116 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
117 { int sig_index = SIGNATURE; \
118 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
119 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
120 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); }
122 #define HANDLE_END_FIELDS() \
123 (current_field = NULL_TREE)
125 #define HANDLE_CONSTANTVALUE(INDEX) \
126 { tree constant; int index = INDEX; \
127 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
128 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
129 constant = build_utf8_ref (name); \
132 constant = get_constant (jcf, index); \
133 set_constant_value (current_field, constant); }
135 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
136 (current_method = add_method (current_class, ACCESS_FLAGS, \
137 get_name_constant (jcf, NAME), \
138 get_name_constant (jcf, SIGNATURE)), \
139 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
140 DECL_LINENUMBERS_OFFSET (current_method) = 0)
142 #define HANDLE_END_METHODS() \
143 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
144 if (handle_type != current_class) layout_type (handle_type); }
146 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
147 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
148 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
149 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
150 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
152 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
154 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
155 JCF_SKIP (jcf, n * 10); }
157 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
159 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
160 JCF_SKIP (jcf, n * 4); }
162 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
165 tree list = DECL_FUNCTION_THROWS (current_method); \
168 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
169 list = tree_cons (NULL_TREE, thrown_class, list); \
171 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
174 #include "jcf-reader.c"
179 parse_signature (jcf, sig_index)
183 if (sig_index <= 0 || sig_index >= JPOOL_SIZE(jcf)
184 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
185 fatal ("invalid field/method signature");
188 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
189 JPOOL_UTF_LENGTH (jcf, sig_index));
196 /* Make identifier nodes long enough for the language-specific slots. */
197 set_identifier_size (sizeof (struct lang_identifier));
208 get_constant (jcf, index)
214 if (index <= 0 || index >= JPOOL_SIZE(jcf))
216 tag = JPOOL_TAG (jcf, index);
217 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
218 return (tree) jcf->cpool.data[index];
219 push_obstacks (&permanent_obstack, &permanent_obstack);
222 case CONSTANT_Integer:
224 jint num = JPOOL_INT(jcf, index);
225 value = build_int_2 (num, num < 0 ? -1 : 0);
226 TREE_TYPE (value) = int_type_node;
231 jint num = JPOOL_INT (jcf, index);
232 HOST_WIDE_INT lo, hi;
233 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
234 num = JPOOL_INT (jcf, index+1);
235 add_double (lo, hi, num, 0, &lo, &hi);
236 value = build_int_2 (lo, hi);
237 TREE_TYPE (value) = long_type_node;
238 force_fit_type (value, 0);
241 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
244 jint num = JPOOL_INT(jcf, index);
246 #ifdef REAL_ARITHMETIC
247 d = REAL_VALUE_FROM_TARGET_SINGLE (num);
249 union { float f; jint i; } u;
253 value = build_real (float_type_node, d);
256 case CONSTANT_Double:
258 HOST_WIDE_INT num[2];
260 HOST_WIDE_INT lo, hi;
261 num[0] = JPOOL_INT (jcf, index);
262 lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
263 num[0] = JPOOL_INT (jcf, index+1);
264 add_double (lo, hi, num[0], 0, &lo, &hi);
265 if (FLOAT_WORDS_BIG_ENDIAN)
275 #ifdef REAL_ARITHMETIC
276 d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
278 union { double d; jint i[2]; } u;
279 u.i[0] = (jint) num[0];
280 u.i[1] = (jint) num[1];
283 value = build_real (double_type_node, d);
286 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
287 case CONSTANT_String:
289 extern struct obstack *expression_obstack;
290 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
291 char *utf8_ptr = IDENTIFIER_POINTER (name);
292 unsigned char *str_ptr;
293 int utf8_len = IDENTIFIER_LENGTH (name);
294 unsigned char *str = (unsigned char*)utf8_ptr;
298 /* Count the number of Unicode characters in the string,
299 while checking for a malformed Utf8 string. */
300 for (str_len = 0; i > 0; str_len++)
302 int char_len = UT8_CHAR_LENGTH (*str);
303 if (char_len < 0 || char_len > 2 || char_len > i)
304 fatal ("bad string constant");
309 value = make_node (STRING_CST);
310 TREE_STRING_LENGTH (value) = 2 * str_len;
311 TREE_STRING_POINTER (value)
312 = obstack_alloc (expression_obstack, 2 * str_len);
313 str_ptr = (unsigned char *) TREE_STRING_POINTER (value);
314 str = (unsigned char*)utf8_ptr;
315 for (i = 0; i < str_len; i++)
318 int char_len = UT8_CHAR_LENGTH (*str);
325 char_value = *str++ & 0x1F;
326 char_value = (char_value << 6) | (*str++ & 0x3F);
329 char_value = *str_ptr++ & 0x0F;
330 char_value = (char_value << 6) | (*str++ & 0x3F);
331 char_value = (char_value << 6) | (*str++ & 0x3F);
336 if (BYTES_BIG_ENDIAN)
338 *str_ptr++ = char_value >> 8;
339 *str_ptr++ = char_value & 0xFF;
343 *str_ptr++ = char_value & 0xFF;
344 *str_ptr++ = char_value >> 8;
353 JPOOL_TAG(jcf, index) = tag | CONSTANT_ResolvedFlag;
354 jcf->cpool.data [index] = (jword) value;
357 fatal ("bad value constant type %d, index %d",
358 JPOOL_TAG( jcf, index ), index);
362 get_name_constant (jcf, index)
366 tree name = get_constant (jcf, index);
367 if (TREE_CODE (name) != IDENTIFIER_NODE)
368 fatal ("bad nameandtype index %d", index);
373 give_name_to_class (jcf, i)
377 if (i <= 0 || i >= JPOOL_SIZE(jcf)
378 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
379 fatal ("bad class index %d", i);
383 int j = JPOOL_USHORT1 (jcf, i);
384 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
385 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
386 JPOOL_UTF_LENGTH (jcf, j));
387 this_class = lookup_class (class_name);
388 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
390 if (main_input_filename == NULL && jcf == main_jcf)
391 main_input_filename = input_filename;
393 jcf->cpool.data[i] = (jword) this_class;
394 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
399 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
402 get_class_constant (JCF *jcf , int i)
405 if (i <= 0 || i >= JPOOL_SIZE(jcf)
406 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
407 fatal ("bad class index %d", i);
409 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
411 int name_index = JPOOL_USHORT1 (jcf, i);
412 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
413 char *name = JPOOL_UTF_DATA (jcf, name_index);
414 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
415 if (name[0] == '[') /* Handle array "classes". */
416 type = TREE_TYPE (parse_signature_string (name, nlength));
419 tree cname = unmangle_classname (name, nlength);
420 type = lookup_class (cname);
422 jcf->cpool.data[i] = (jword) type;
423 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
426 type = (tree) jcf->cpool.data[i];
431 DEFUN(jcf_out_of_synch, (jcf),
434 char *source = strdup (jcf->filename);
435 int i = strlen (source);
437 while (source[i] != '.')
441 warning ("Class file `%s' out of synch with `%s.java'",
442 jcf->filename, source);
446 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
447 called from the parser, otherwise it's a RECORD_TYPE node. If
448 VERBOSE is 1, print error message on failure to load a class. */
451 load_class (class_or_name, verbose)
457 tree save_current_class = current_class;
458 char *save_input_filename = input_filename;
459 JCF *save_current_jcf = current_jcf;
461 if (current_jcf->read_state)
462 saved_pos = ftell (current_jcf->read_state);
464 /* class_or_name can be the name of the class we want to load */
465 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
466 name = class_or_name;
467 /* In some cases, it's a dependency that we process earlier that
469 else if (TREE_CODE (class_or_name) == TREE_LIST)
470 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
471 /* Or it's a type in the making */
473 name = DECL_NAME (TYPE_NAME (class_or_name));
475 push_obstacks (&permanent_obstack, &permanent_obstack);
477 /* Search in current zip first. */
478 if (find_in_current_zip (IDENTIFIER_POINTER (name),
479 IDENTIFIER_LENGTH (name), &jcf) == 0)
480 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
485 error ("Cannot find class file for class %s.",
486 IDENTIFIER_POINTER (name));
487 TYPE_SIZE (class_or_name) = error_mark_node;
489 /* FIXME: what to do here? */
490 if (!strcmp (classpath, DEFAULT_CLASS_PATH))
493 pop_obstacks (); /* FIXME: one pop_obstack() per function */
499 this_jcf.seen_in_zip = 0;
500 current_jcf = &this_jcf;
501 if (this_jcf.outofsynch)
502 jcf_out_of_synch (current_jcf);
507 if (current_jcf->java_source)
508 jcf_parse_source (current_jcf);
510 java_parser_context_save_global ();
511 java_push_parser_context ();
512 input_filename = current_jcf->filename;
513 jcf_parse (current_jcf);
514 java_pop_parser_context (0);
515 java_parser_context_restore_global ();
518 if (!current_jcf->seen_in_zip)
519 JCF_FINISH (current_jcf);
520 /* DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;*/
523 current_class = save_current_class;
524 input_filename = save_input_filename;
525 current_jcf = save_current_jcf;
526 if (current_jcf->read_state)
527 fseek (current_jcf->read_state, saved_pos, SEEK_SET);
530 /* Parse a source file when JCF refers to a source file. */
533 jcf_parse_source (jcf)
538 java_parser_context_save_global ();
539 java_push_parser_context ();
540 input_filename = current_jcf->filename;
541 file = get_identifier (input_filename);
542 if (!HAS_BEEN_ALREADY_PARSED_P (file))
544 if (!(finput = fopen (input_filename, "r")))
545 fatal ("input file `%s' just disappeared - jcf_parse_source",
547 parse_source_file (file);
549 fatal ("can't close input file `%s' stream - jcf_parse_source",
552 java_pop_parser_context (IS_A_COMMAND_LINE_FILENAME_P (file));
553 java_parser_context_restore_global ();
556 /* Parse the .class file JCF. */
564 if (jcf_parse_preamble (jcf) != 0)
565 fatal ("Not a valid Java .class file.\n");
566 code = jcf_parse_constant_pool (jcf);
568 fatal ("error while parsing constant pool");
569 code = verify_constant_pool (jcf);
571 fatal ("error in constant pool entry #%d\n", code);
573 jcf_parse_class (jcf);
574 if (main_class == NULL_TREE)
575 main_class = current_class;
576 if (! quiet_flag && TYPE_NAME (current_class))
577 fprintf (stderr, " class %s",
578 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
579 CLASS_LOADED_P (current_class) = 1;
581 for (i = 1; i < JPOOL_SIZE(jcf); i++)
583 switch (JPOOL_TAG (jcf, i))
586 get_class_constant (jcf, i);
591 code = jcf_parse_fields (jcf);
593 fatal ("error while parsing fields");
594 code = jcf_parse_methods (jcf);
596 fatal ("error while parsing methods");
597 code = jcf_parse_final_attributes (jcf);
599 fatal ("error while parsing final attributes");
601 /* The fields of class_type_node are already in correct order. */
602 if (current_class != class_type_node && current_class != object_type_node)
603 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
605 push_obstacks (&permanent_obstack, &permanent_obstack);
606 layout_class (current_class);
607 if (current_class == object_type_node)
608 layout_class_methods (object_type_node);
610 all_class_list = tree_cons (NULL_TREE,
611 TYPE_NAME (current_class), all_class_list );
616 init_outgoing_cpool ()
618 current_constant_pool_data_ref = NULL_TREE;
619 if (outgoing_cpool == NULL)
621 static CPool outgoing_cpool_buffer;
622 outgoing_cpool = &outgoing_cpool_buffer;
623 CPOOL_INIT(outgoing_cpool);
627 CPOOL_REINIT(outgoing_cpool);
635 char *save_input_filename = input_filename;
636 int save_lineno = lineno;
638 LAYOUT_SEEN_CLASS_METHODS ();
640 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
642 debug_start_source_file (input_filename);
643 init_outgoing_cpool ();
645 for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
646 method != NULL_TREE; method = TREE_CHAIN (method))
648 JCF *jcf = current_jcf;
650 if (METHOD_NATIVE (method) || METHOD_ABSTRACT (method))
653 if (DECL_CODE_OFFSET (method) == 0)
655 error ("missing Code attribute");
660 if (DECL_LINENUMBERS_OFFSET (method))
663 register unsigned char *ptr;
664 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
665 linenumber_count = i = JCF_readu2 (jcf);
666 linenumber_table = ptr = jcf->read_ptr;
668 for (ptr += 2; --i >= 0; ptr += 4)
670 int line = GET_u2 (ptr);
671 /* Set initial lineno lineno to smallest linenumber.
672 * Needs to be set before init_function_start. */
673 if (lineno == 0 || line < lineno)
679 linenumber_table = NULL;
680 linenumber_count = 0;
683 start_java_method (method);
685 give_name_to_locals (jcf);
687 /* Actually generate code. */
688 expand_byte_code (jcf, method);
693 if (flag_emit_class_files)
694 write_classfile (current_class);
695 make_class_data (current_class);
697 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
699 debug_end_source_file (save_lineno);
700 input_filename = save_input_filename;
701 lineno = save_lineno;
704 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
707 parse_source_file (file)
710 /* Mark the file as parsed */
711 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
713 lang_init_source (1); /* Error msgs have no method prototypes */
714 java_init_lex (); /* Initialize the parser */
715 java_parse_abort_on_error ();
716 java_parse (); /* Parse and build partial tree nodes. */
717 java_parse_abort_on_error ();
718 java_complete_class (); /* Parse unsatisfied class decl. */
719 java_parse_abort_on_error ();
720 java_check_circular_reference (); /* Check on circular references */
721 java_parse_abort_on_error ();
727 int several_files = 0;
728 char *list = strdup (input_filename), *next;
729 tree node, current_file_list = NULL_TREE;
733 next = strchr (list, '&');
743 extern int saw_java_source; /* FIXME: temporary. */
746 /* FIXME: this test is only needed until our .java parser is
748 if (len > 5 && ! strcmp (&list[len - 5], ".java"))
751 if (*list != '/' && several_files)
752 obstack_grow (&temporary_obstack, "./", 2);
754 obstack_grow0 (&temporary_obstack, list, len);
755 value = obstack_finish (&temporary_obstack);
756 node = get_identifier (value);
757 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
758 current_file_list = tree_cons (NULL_TREE, node, current_file_list);
764 current_jcf = main_jcf;
765 current_file_list = nreverse (current_file_list);
766 for (node = current_file_list; node; node = TREE_CHAIN (node))
768 tree name = TREE_VALUE (node);
770 /* Skip already parsed files */
771 if (HAS_BEEN_ALREADY_PARSED_P (name))
774 /* Close previous descriptor, if any */
775 if (main_jcf->read_state && fclose (main_jcf->read_state))
776 fatal ("failed to close input file `%s' - yyparse",
777 (main_jcf->filename ? main_jcf->filename : "<unknown>"));
779 /* Set jcf up and open a new file */
781 main_jcf->read_state = fopen (IDENTIFIER_POINTER (name), "rb");
782 if (main_jcf->read_state == NULL)
783 pfatal_with_name (IDENTIFIER_POINTER (name));
785 /* Set new input_filename and finput */
786 finput = main_jcf->read_state;
787 #ifdef IO_BUFFER_SIZE
788 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
789 _IOFBF, IO_BUFFER_SIZE);
791 input_filename = IDENTIFIER_POINTER (name);
792 main_jcf->filbuf = jcf_filbuf_from_stdio;
794 switch (jcf_figure_file_type (current_jcf))
797 parse_zip_file_entries ();
800 jcf_parse (current_jcf);
804 java_push_parser_context ();
805 java_parser_context_save_global ();
806 parse_source_file (name);
807 java_parser_context_restore_global ();
808 java_pop_parser_context (1);
813 java_expand_classes ();
814 if (!java_report_errors () && !flag_emit_class_files)
815 emit_register_classes ();
819 static struct ZipFileCache *localToFile;
821 /* Process all class entries found in the zip file. */
823 parse_zip_file_entries (void)
825 struct ZipDirectory *zdir;
828 for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
829 i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
833 /* We don't need to consider those files. */
834 if (!zdir->size || !zdir->filename_offset)
837 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
838 current_jcf = TYPE_LANG_SPECIFIC (class)->jcf;
839 current_class = class;
841 if ( !CLASS_LOADED_P (class))
843 fseek (current_jcf->read_state, current_jcf->zip_offset, SEEK_SET);
844 jcf_parse (current_jcf);
847 if (TYPE_SIZE (current_class) != error_mark_node)
849 input_filename = current_jcf->filename;
851 FREE (current_jcf->buffer); /* No longer necessary */
852 /* Note: there is a way to free this buffer right after a
853 class seen in a zip file has been parsed. The idea is the
854 set its jcf in such a way that buffer will be reallocated
855 the time the code for the class will be generated. FIXME. */
860 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
861 jcf up for further processing and link it to the created class. */
863 void process_zip_dir()
868 for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
869 i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
871 char *class_name, *file_name, *class_name_in_zip_dir;
876 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
878 /* We choose to not to process entries with a zero size or entries
879 not bearing the .class extention. */
880 if (!zdir->size || !zdir->filename_offset ||
881 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
884 /* So it will be skipped in parse_zip_file_entries */
889 class_name = ALLOC (zdir->filename_length+1-6);
890 file_name = ALLOC (zdir->filename_length+1);
891 jcf = ALLOC (sizeof (JCF));
894 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
895 class_name [zdir->filename_length-6] = '\0';
896 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
897 file_name [zdir->filename_length] = '\0';
899 for (j=0; class_name[j]; j++)
900 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
902 /* Yes, we write back the true class name into the zip directory. */
903 strcpy (class_name_in_zip_dir, class_name);
904 zdir->filename_length = j;
905 class = lookup_class (get_identifier (class_name));
907 jcf->read_state = finput;
908 jcf->filbuf = jcf_filbuf_from_stdio;
909 jcf->seen_in_zip = 1;
910 jcf->java_source = 0;
911 jcf->zip_offset = zdir->filestart;
912 jcf->classname = class_name;
913 jcf->filename = file_name;
915 TYPE_LANG_SPECIFIC (class) =
916 (struct lang_type *) perm_calloc (1, sizeof (struct lang_type));
917 TYPE_LANG_SPECIFIC (class)->jcf = jcf;
921 /* Lookup class NAME and figure whether is a class already found in the current
924 DEFUN(find_in_current_zip, (name, length, jcf),
925 char *name AND int length AND JCF **jcf)
928 tree class_name = maybe_get_identifier (name), class, icv;
933 if (!(icv = IDENTIFIER_CLASS_VALUE (class_name)))
936 class = TREE_TYPE (icv);
938 /* Doesn't have jcf specific info ? It's not ours */
939 if (!TYPE_LANG_SPECIFIC (class) || !TYPE_LANG_SPECIFIC (class)->jcf)
942 *jcf = local_jcf = TYPE_LANG_SPECIFIC (class)->jcf;
943 fseek (local_jcf->read_state, local_jcf->zip_offset, SEEK_SET);
947 /* Figure what kind of file we're dealing with */
949 DEFUN(jcf_figure_file_type, (jcf),
952 unsigned char magic_string[4];
955 if (fread (magic_string, 1, 4, jcf->read_state) != 4)
956 jcf_unexpected_eof (jcf, 4);
958 fseek (jcf->read_state, 0L, SEEK_SET);
959 magic = GET_u4 (magic_string);
961 if (magic == 0xcafebabe)
964 /* FIXME: is it a system file? */
965 if (!open_in_zip (jcf, input_filename, NULL, 0))
967 localToFile = ALLOC (sizeof (struct ZipFileCache));
968 bcopy (SeenZipFiles, localToFile, sizeof (struct ZipFileCache));
969 process_zip_dir (); /* Register all the class defined there */