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 /* This is true if the user specified a `.java' file on the command
65 line. Otherwise it is 0. FIXME: this is temporary, until our
66 .java parser is fully working. */
67 int saw_java_source
= 0;
69 /* The FIELD_DECL for the current field. */
70 static tree current_field
= NULL_TREE
;
72 static tree current_method
= NULL_TREE
;
74 static tree give_name_to_class
PROTO ((JCF
*jcf
, int index
));
76 void parse_zip_file_entries (void);
77 void process_zip_dir();
79 /* Source file compilation declarations */
80 static void parse_source_file ();
82 /* Handle "SourceFile" attribute. */
85 set_source_filename (jcf
, index
)
89 tree sfname_id
= get_name_constant (jcf
, index
);
90 char *sfname
= IDENTIFIER_POINTER (sfname_id
);
91 if (input_filename
!= NULL
)
93 int old_len
= strlen (input_filename
);
94 int new_len
= IDENTIFIER_LENGTH (sfname_id
);
95 /* Use the current input_filename (derived from the class name)
96 if it has a directory prefix, but otherwise matches sfname. */
98 && strcmp (sfname
, input_filename
+ old_len
- new_len
) == 0
99 && (input_filename
[old_len
- new_len
- 1] == '/'
100 || input_filename
[old_len
- new_len
- 1] == '\\'))
103 input_filename
= sfname
;
104 DECL_SOURCE_FILE (TYPE_NAME (current_class
)) = sfname
;
105 if (current_class
== main_class
) main_input_filename
= input_filename
;
108 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
110 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
111 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
112 current_class = give_name_to_class (jcf, THIS); \
113 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
115 #define HANDLE_CLASS_INTERFACE(INDEX) \
116 add_interface (current_class, get_class_constant (jcf, INDEX))
118 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
119 { int sig_index = SIGNATURE; \
120 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
121 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
122 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); }
124 #define HANDLE_END_FIELDS() \
125 (current_field = NULL_TREE)
127 #define HANDLE_CONSTANTVALUE(INDEX) \
128 { tree constant; int index = INDEX; \
129 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
130 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
131 constant = build_utf8_ref (name); \
134 constant = get_constant (jcf, index); \
135 set_constant_value (current_field, constant); }
137 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
138 (current_method = add_method (current_class, ACCESS_FLAGS, \
139 get_name_constant (jcf, NAME), \
140 get_name_constant (jcf, SIGNATURE)), \
141 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
142 DECL_LINENUMBERS_OFFSET (current_method) = 0)
144 #define HANDLE_END_METHODS() \
145 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
146 if (handle_type != current_class) layout_type (handle_type); }
148 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
149 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
150 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
151 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
152 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
154 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
156 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
157 JCF_SKIP (jcf, n * 10); }
159 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
161 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
162 JCF_SKIP (jcf, n * 4); }
164 #include "jcf-reader.c"
169 parse_signature (jcf
, sig_index
)
173 if (sig_index
<= 0 || sig_index
>= JPOOL_SIZE(jcf
)
174 || JPOOL_TAG (jcf
, sig_index
) != CONSTANT_Utf8
)
175 fatal ("invalid field/method signature");
178 return parse_signature_string (JPOOL_UTF_DATA (jcf
, sig_index
),
179 JPOOL_UTF_LENGTH (jcf
, sig_index
));
186 /* Make identifier nodes long enough for the language-specific slots. */
187 set_identifier_size (sizeof (struct lang_identifier
));
198 get_constant (jcf
, index
)
204 if (index
<= 0 || index
>= JPOOL_SIZE(jcf
))
206 tag
= JPOOL_TAG (jcf
, index
);
207 if ((tag
& CONSTANT_ResolvedFlag
) || tag
== CONSTANT_Utf8
)
208 return (tree
) jcf
->cpool
.data
[index
];
209 push_obstacks (&permanent_obstack
, &permanent_obstack
);
212 case CONSTANT_Integer
:
214 jint num
= JPOOL_INT(jcf
, index
);
215 value
= build_int_2 (num
, num
< 0 ? -1 : 0);
216 TREE_TYPE (value
) = int_type_node
;
221 jint num
= JPOOL_INT (jcf
, index
);
222 HOST_WIDE_INT lo
, hi
;
223 lshift_double (num
, 0, 32, 64, &lo
, &hi
, 0);
224 num
= JPOOL_INT (jcf
, index
+1);
225 add_double (lo
, hi
, num
, 0, &lo
, &hi
);
226 value
= build_int_2 (lo
, hi
);
227 TREE_TYPE (value
) = long_type_node
;
228 force_fit_type (value
, 0);
231 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
234 jint num
= JPOOL_INT(jcf
, index
);
236 #ifdef REAL_ARITHMETIC
237 d
= REAL_VALUE_FROM_TARGET_SINGLE (num
);
239 union { float f
; jint i
; } u
;
243 value
= build_real (float_type_node
, d
);
246 case CONSTANT_Double
:
248 HOST_WIDE_INT num
[2];
250 HOST_WIDE_INT lo
, hi
;
251 num
[0] = JPOOL_INT (jcf
, index
);
252 lshift_double (num
[0], 0, 32, 64, &lo
, &hi
, 0);
253 num
[0] = JPOOL_INT (jcf
, index
+1);
254 add_double (lo
, hi
, num
[0], 0, &lo
, &hi
);
255 if (FLOAT_WORDS_BIG_ENDIAN
)
265 #ifdef REAL_ARITHMETIC
266 d
= REAL_VALUE_FROM_TARGET_DOUBLE (num
);
268 union { double d
; jint i
[2]; } u
;
269 u
.i
[0] = (jint
) num
[0];
270 u
.i
[1] = (jint
) num
[1];
273 value
= build_real (double_type_node
, d
);
276 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
277 case CONSTANT_String
:
279 extern struct obstack
*expression_obstack
;
280 tree name
= get_name_constant (jcf
, JPOOL_USHORT1 (jcf
, index
));
281 char *utf8_ptr
= IDENTIFIER_POINTER (name
);
282 unsigned char *str_ptr
;
283 int utf8_len
= IDENTIFIER_LENGTH (name
);
284 unsigned char *str
= (unsigned char*)utf8_ptr
;
288 /* Count the number of Unicode characters in the string,
289 while checking for a malformed Utf8 string. */
290 for (str_len
= 0; i
> 0; str_len
++)
292 int char_len
= UT8_CHAR_LENGTH (*str
);
293 if (char_len
< 0 || char_len
> 2 || char_len
> i
)
294 fatal ("bad string constant");
299 value
= make_node (STRING_CST
);
300 TREE_STRING_LENGTH (value
) = 2 * str_len
;
301 TREE_STRING_POINTER (value
)
302 = obstack_alloc (expression_obstack
, 2 * str_len
);
303 str_ptr
= (unsigned char *) TREE_STRING_POINTER (value
);
304 str
= (unsigned char*)utf8_ptr
;
305 for (i
= 0; i
< str_len
; i
++)
308 int char_len
= UT8_CHAR_LENGTH (*str
);
315 char_value
= *str
++ & 0x1F;
316 char_value
= (char_value
<< 6) | (*str
++ & 0x3F);
319 char_value
= *str_ptr
++ & 0x0F;
320 char_value
= (char_value
<< 6) | (*str
++ & 0x3F);
321 char_value
= (char_value
<< 6) | (*str
++ & 0x3F);
326 if (BYTES_BIG_ENDIAN
)
328 *str_ptr
++ = char_value
>> 8;
329 *str_ptr
++ = char_value
& 0xFF;
333 *str_ptr
++ = char_value
& 0xFF;
334 *str_ptr
++ = char_value
>> 8;
343 JPOOL_TAG(jcf
, index
) = tag
| CONSTANT_ResolvedFlag
;
344 jcf
->cpool
.data
[index
] = (jword
) value
;
347 fatal ("bad value constant type %d, index %d",
348 JPOOL_TAG( jcf
, index
), index
);
352 get_name_constant (jcf
, index
)
356 tree name
= get_constant (jcf
, index
);
357 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
358 fatal ("bad nameandtype index %d", index
);
363 give_name_to_class (jcf
, i
)
367 if (i
<= 0 || i
>= JPOOL_SIZE(jcf
)
368 || JPOOL_TAG (jcf
, i
) != CONSTANT_Class
)
369 fatal ("bad class index %d", i
);
373 int j
= JPOOL_USHORT1 (jcf
, i
);
374 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
375 tree class_name
= unmangle_classname (JPOOL_UTF_DATA (jcf
, j
),
376 JPOOL_UTF_LENGTH (jcf
, j
));
377 this_class
= lookup_class (class_name
);
378 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (this_class
));
380 if (main_input_filename
== NULL
&& jcf
== main_jcf
)
381 main_input_filename
= input_filename
;
383 jcf
->cpool
.data
[i
] = (jword
) this_class
;
384 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
389 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
392 get_class_constant (JCF
*jcf
, int i
)
395 if (i
<= 0 || i
>= JPOOL_SIZE(jcf
)
396 || (JPOOL_TAG (jcf
, i
) & ~CONSTANT_ResolvedFlag
) != CONSTANT_Class
)
397 fatal ("bad class index %d", i
);
399 if (JPOOL_TAG (jcf
, i
) != CONSTANT_ResolvedClass
)
401 int name_index
= JPOOL_USHORT1 (jcf
, i
);
402 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
403 char *name
= JPOOL_UTF_DATA (jcf
, name_index
);
404 int nlength
= JPOOL_UTF_LENGTH (jcf
, name_index
);
405 if (name
[0] == '[') /* Handle array "classes". */
406 type
= TREE_TYPE (parse_signature_string (name
, nlength
));
409 tree cname
= unmangle_classname (name
, nlength
);
410 type
= lookup_class (cname
);
412 jcf
->cpool
.data
[i
] = (jword
) type
;
413 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
416 type
= (tree
) jcf
->cpool
.data
[i
];
421 DEFUN(jcf_out_of_synch
, (jcf
),
424 char *source
= strdup (jcf
->filename
);
425 int i
= strlen (source
);
427 while (source
[i
] != '.')
431 warning ("Class file `%s' out of synch with `%s.java'",
432 jcf
->filename
, source
);
436 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
437 called from the parser, otherwise it's a RECORD_TYPE node. If
438 VERBOSE is 1, print error message on failure to load a class. */
441 load_class (class_or_name
, verbose
)
446 tree name
= (TREE_CODE (class_or_name
) == IDENTIFIER_NODE
?
447 class_or_name
: DECL_NAME (TYPE_NAME (class_or_name
)));
448 tree save_current_class
= current_class
;
449 char *save_input_filename
= input_filename
;
450 JCF
*save_current_jcf
= current_jcf
;
452 if (current_jcf
->read_state
)
453 saved_pos
= ftell (current_jcf
->read_state
);
455 push_obstacks (&permanent_obstack
, &permanent_obstack
);
457 /* Search in current zip first. */
458 if (find_in_current_zip (IDENTIFIER_POINTER (name
),
459 IDENTIFIER_LENGTH (name
), &jcf
) == 0)
460 if (find_class (IDENTIFIER_POINTER (name
), IDENTIFIER_LENGTH (name
),
465 error ("Cannot find class file for class %s.",
466 IDENTIFIER_POINTER (name
));
467 TYPE_SIZE (class_or_name
) = error_mark_node
;
469 /* FIXME: what to do here? */
470 if (!strcmp (classpath
, DEFAULT_CLASS_PATH
))
473 pop_obstacks (); /* FIXME: one pop_obstack() per function */
479 this_jcf
.seen_in_zip
= 0;
480 current_jcf
= &this_jcf
;
481 if (this_jcf
.outofsynch
)
482 jcf_out_of_synch (current_jcf
);
487 if (current_jcf
->java_source
)
488 jcf_parse_source (current_jcf
);
490 int saved_lineno
= lineno
;
491 input_filename
= current_jcf
->filename
;
492 jcf_parse (current_jcf
);
493 lineno
= saved_lineno
;
496 if (!current_jcf
->seen_in_zip
)
497 JCF_FINISH (current_jcf
);
498 /* DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;*/
501 current_class
= save_current_class
;
502 input_filename
= save_input_filename
;
503 current_jcf
= save_current_jcf
;
504 if (current_jcf
->read_state
)
505 fseek (current_jcf
->read_state
, saved_pos
, SEEK_SET
);
508 /* Parse a source file when JCF refers to a source file. */
511 jcf_parse_source (jcf
)
514 java_parser_context_save_global ();
516 input_filename
= current_jcf
->filename
;
517 if (!(finput
= fopen (input_filename
, "r")))
518 fatal ("input file `%s' just disappeared - jcf_parse_source",
520 parse_source_file (1); /* Parse only */
521 java_parser_context_restore_global ();
524 /* Parse the .class file JCF. */
532 if (jcf_parse_preamble (jcf
) != 0)
533 fatal ("Not a valid Java .class file.\n");
534 code
= jcf_parse_constant_pool (jcf
);
536 fatal ("error while parsing constant pool");
537 code
= verify_constant_pool (jcf
);
539 fatal ("error in constant pool entry #%d\n", code
);
541 jcf_parse_class (jcf
);
542 if (main_class
== NULL_TREE
)
543 main_class
= current_class
;
544 if (! quiet_flag
&& TYPE_NAME (current_class
))
545 fprintf (stderr
, " class %s",
546 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class
))));
547 CLASS_LOADED_P (current_class
) = 1;
549 for (i
= 1; i
< JPOOL_SIZE(jcf
); i
++)
551 switch (JPOOL_TAG (jcf
, i
))
554 get_class_constant (jcf
, i
);
559 code
= jcf_parse_fields (jcf
);
561 fatal ("error while parsing fields");
562 code
= jcf_parse_methods (jcf
);
564 fatal ("error while parsing methods");
565 code
= jcf_parse_final_attributes (jcf
);
567 fatal ("error while parsing final attributes");
569 /* The fields of class_type_node are already in correct order. */
570 if (current_class
!= class_type_node
&& current_class
!= object_type_node
)
571 TYPE_FIELDS (current_class
) = nreverse (TYPE_FIELDS (current_class
));
573 push_obstacks (&permanent_obstack
, &permanent_obstack
);
574 layout_class (current_class
);
579 init_outgoing_cpool ()
581 current_constant_pool_data_ref
= NULL_TREE
;
582 if (outgoing_cpool
== NULL
)
584 static CPool outgoing_cpool_buffer
;
585 outgoing_cpool
= &outgoing_cpool_buffer
;
586 CPOOL_INIT(outgoing_cpool
);
590 CPOOL_REINIT(outgoing_cpool
);
598 char *save_input_filename
= input_filename
;
599 int save_lineno
= lineno
;
601 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (current_class
));
603 debug_start_source_file (input_filename
);
604 init_outgoing_cpool ();
606 for ( method
= TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class
));
607 method
!= NULL_TREE
; method
= TREE_CHAIN (method
))
609 JCF
*jcf
= current_jcf
;
611 if (METHOD_NATIVE (method
) || METHOD_ABSTRACT (method
))
614 if (DECL_CODE_OFFSET (method
) == 0)
616 error ("missing Code attribute");
621 if (DECL_LINENUMBERS_OFFSET (method
))
624 register unsigned char *ptr
;
625 JCF_SEEK (jcf
, DECL_LINENUMBERS_OFFSET (method
));
626 linenumber_count
= i
= JCF_readu2 (jcf
);
627 linenumber_table
= ptr
= jcf
->read_ptr
;
629 for (ptr
+= 2; --i
>= 0; ptr
+= 4)
631 int line
= GET_u2 (ptr
);
632 /* Set initial lineno lineno to smallest linenumber.
633 * Needs to be set before init_function_start. */
634 if (lineno
== 0 || line
< lineno
)
640 linenumber_table
= NULL
;
641 linenumber_count
= 0;
644 start_java_method (method
);
646 give_name_to_locals (jcf
);
648 /* Actually generate code. */
649 expand_byte_code (jcf
, method
);
654 if (flag_emit_class_files
)
655 write_classfile (current_class
);
656 make_class_data (current_class
);
658 rest_of_decl_compilation (TYPE_NAME (current_class
), (char*) 0, 1, 0);
660 debug_end_source_file (save_lineno
);
661 input_filename
= save_input_filename
;
662 lineno
= save_lineno
;
665 /* Parse a source file, as pointed by the current JCF. If PARSE_ONLY
666 is non zero, we're not parsing a file found on the command line and
667 we skip things related to code generation. */
670 parse_source_file (parse_only
)
673 int remember_for_generation
;
674 tree filename
= get_identifier (input_filename
);
676 /* Mark the file as parsed */
677 HAS_BEEN_ALREADY_PARSED_P (filename
) = 1;
679 lang_init_source (1); /* Error msgs have no method prototypes */
680 java_push_parser_context ();
681 java_init_lex (); /* Initialize the parser */
682 java_parse_abort_on_error ();
683 java_parse (); /* Parse and build partial tree nodes. */
684 java_parse_abort_on_error ();
685 java_complete_class (); /* Parse unsatisfied class decl. */
686 java_parse_abort_on_error ();
687 java_check_circular_reference (); /* Check on circular references */
688 java_parse_abort_on_error ();
689 java_check_methods (); /* Check the methods */
690 java_parse_abort_on_error ();
691 java_layout_classes ();
692 java_parse_abort_on_error ();
694 if (flag_emit_class_files
)
695 write_classfile (current_class
);
697 /* If only parsing, make sure that the currently parsed file isn't
698 also present in the argument list. If it's the case, remember
699 that we should generate it. */
700 remember_for_generation
= !parse_only
701 || IS_A_COMMAND_LINE_FILENAME_P (filename
);
702 java_pop_parser_context (remember_for_generation
);
708 int several_files
= 0;
709 char *list
= strdup (input_filename
), *next
;
710 tree node
, current_file_list
= NULL_TREE
;
714 next
= strchr (list
, '&');
726 if (len
> 5 && ! strcmp (&list
[len
- 5], ".java"))
729 if (*list
!= '/' && several_files
)
730 obstack_grow (&temporary_obstack
, "./", 2);
732 obstack_grow0 (&temporary_obstack
, list
, len
);
733 value
= obstack_finish (&temporary_obstack
);
734 node
= get_identifier (value
);
735 IS_A_COMMAND_LINE_FILENAME_P (node
) = 1;
736 current_file_list
= tree_cons (NULL_TREE
, node
, current_file_list
);
742 current_file_list
= nreverse (current_file_list
);
743 for (node
= current_file_list
; node
; node
= TREE_CHAIN (node
))
745 /* Don't substitute if INPUT_FILENAME doesn't feature the &
746 separator: we have only one file to deal with, we're fine */
749 tree name
= TREE_VALUE (node
);
751 /* Skip already parsed files */
752 if (HAS_BEEN_ALREADY_PARSED_P (name
))
755 /* Close previous descriptor, if any */
756 if (main_jcf
->read_state
&& fclose (main_jcf
->read_state
))
757 fatal ("failed to close input file `%s' - yyparse",
758 (main_jcf
->filename
? main_jcf
->filename
: "<unknown>"));
761 main_jcf
->read_state
= fopen (IDENTIFIER_POINTER (name
), "r");
762 if (main_jcf
->read_state
== NULL
)
763 pfatal_with_name (IDENTIFIER_POINTER (name
));
765 /* Set new input_filename and finput */
766 input_filename
= IDENTIFIER_POINTER (name
);
767 finput
= main_jcf
->read_state
;
770 switch (jcf_figure_file_type (current_jcf
))
773 parse_zip_file_entries ();
776 jcf_parse (current_jcf
);
780 parse_source_file (0); /* Parse and generate */
784 java_expand_classes ();
785 if (! flag_emit_class_files
)
786 emit_register_classes ();
790 static struct ZipFileCache
*localToFile
;
792 /* Process all class entries found in the zip file. */
794 parse_zip_file_entries (void)
796 struct ZipDirectory
*zdir
;
799 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->z
.central_directory
;
800 i
< localToFile
->z
.count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
804 /* We don't need to consider those files. */
805 if (!zdir
->size
|| !zdir
->filename_offset
)
808 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir
)));
809 current_jcf
= TYPE_LANG_SPECIFIC (class)->jcf
;
810 current_class
= class;
812 if ( !CLASS_LOADED_P (class))
814 fseek (current_jcf
->read_state
, current_jcf
->zip_offset
, SEEK_SET
);
815 jcf_parse (current_jcf
);
818 input_filename
= current_jcf
->filename
;
821 FREE (current_jcf
->buffer
); /* No longer necessary */
822 /* Note: there is a way to free this buffer right after a class seen
823 in a zip file has been parsed. The idea is the set its jcf in such
824 a way that buffer will be reallocated the time the code for the class
825 will be generated. FIXME. */
829 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
830 jcf up for further processing and link it to the created class. */
832 void process_zip_dir()
837 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->z
.central_directory
;
838 i
< localToFile
->z
.count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
840 char *class_name
, *file_name
, *class_name_in_zip_dir
;
845 class_name_in_zip_dir
= ZIPDIR_FILENAME (zdir
);
847 /* We choose to not to process entries with a zero size or entries
848 not bearing the .class extention. */
849 if (!zdir
->size
|| !zdir
->filename_offset
||
850 strncmp (&class_name_in_zip_dir
[zdir
->filename_length
-6],
853 /* So it will be skipped in parse_zip_file_entries */
858 class_name
= ALLOC (zdir
->filename_length
+1-6);
859 file_name
= ALLOC (zdir
->filename_length
+1);
860 jcf
= ALLOC (sizeof (JCF
));
863 strncpy (class_name
, class_name_in_zip_dir
, zdir
->filename_length
-6);
864 class_name
[zdir
->filename_length
-6] = '\0';
865 strncpy (file_name
, class_name_in_zip_dir
, zdir
->filename_length
);
866 file_name
[zdir
->filename_length
] = '\0';
868 for (j
=0; class_name
[j
]; j
++)
869 class_name
[j
] = (class_name
[j
] == '/' ? '.' : class_name
[j
]);
871 /* Yes, we write back the true class name into the zip directory. */
872 strcpy (class_name_in_zip_dir
, class_name
);
873 zdir
->filename_length
= j
;
874 class = lookup_class (get_identifier (class_name
));
876 jcf
->read_state
= finput
;
877 jcf
->filbuf
= jcf_filbuf_from_stdio
;
878 jcf
->seen_in_zip
= 1;
879 jcf
->java_source
= 0;
880 jcf
->zip_offset
= zdir
->filestart
;
881 jcf
->classname
= class_name
;
882 jcf
->filename
= file_name
;
884 TYPE_LANG_SPECIFIC (class) =
885 (struct lang_type
*) perm_calloc (1, sizeof (struct lang_type
));
886 TYPE_LANG_SPECIFIC (class)->jcf
= jcf
;
890 /* Lookup class NAME and figure whether is a class already found in the current
893 DEFUN(find_in_current_zip
, (name
, length
, jcf
),
894 char *name AND
int length AND JCF
**jcf
)
897 tree class_name
= maybe_get_identifier (name
), class, icv
;
902 if (!(icv
= IDENTIFIER_CLASS_VALUE (class_name
)))
905 class = TREE_TYPE (icv
);
907 /* Doesn't have jcf specific info ? It's not ours */
908 if (!TYPE_LANG_SPECIFIC (class) || !TYPE_LANG_SPECIFIC (class)->jcf
)
911 *jcf
= local_jcf
= TYPE_LANG_SPECIFIC (class)->jcf
;
912 fseek (local_jcf
->read_state
, local_jcf
->zip_offset
, SEEK_SET
);
916 /* Figure what kind of file we're dealing with */
918 DEFUN(jcf_figure_file_type
, (jcf
),
921 unsigned char magic_string
[4];
924 if (fread (magic_string
, 1, 4, jcf
->read_state
) != 4)
925 jcf_unexpected_eof (jcf
, 4);
927 fseek (jcf
->read_state
, 0L, SEEK_SET
);
928 magic
= GET_u4 (magic_string
);
930 if (magic
== 0xcafebabe)
933 /* FIXME: is it a system file? */
934 if (!open_in_zip (jcf
, input_filename
, NULL
, 0))
936 localToFile
= ALLOC (sizeof (struct ZipFileCache
));
937 bcopy (SeenZipFiles
, localToFile
, sizeof (struct ZipFileCache
));
938 process_zip_dir (); /* Register all the class defined there */