* jcf-io.c (find_class): Use saw_java_source to determine when to
[official-gcc.git] / gcc / java / jcf-parse.c
blob81a1528e750a876f0431e90849defa46b85994f4
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)
9 any later version.
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> */
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "obstack.h"
31 #include "flags.h"
32 #include "java-except.h"
33 #include "input.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) \
42 do { \
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]; \
47 text[LENGTH] = 0; \
48 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
49 text[LENGTH] = save; \
50 JCF_SKIP (JCF, LENGTH); } while (0)
52 #include "jcf.h"
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. */
84 void
85 set_source_filename (jcf, index)
86 JCF *jcf;
87 int 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. */
97 if (old_len > new_len
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] == '\\'))
101 return;
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); \
133 else \
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) \
155 { int n = (COUNT); \
156 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
157 JCF_SKIP (jcf, n * 10); }
159 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
160 { int n = (COUNT); \
161 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
162 JCF_SKIP (jcf, n * 4); }
164 #include "jcf-reader.c"
166 static int yydebug;
168 tree
169 parse_signature (jcf, sig_index)
170 JCF *jcf;
171 int 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");
176 else
178 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
179 JPOOL_UTF_LENGTH (jcf, sig_index));
183 void
184 init_lex ()
186 /* Make identifier nodes long enough for the language-specific slots. */
187 set_identifier_size (sizeof (struct lang_identifier));
190 void
191 set_yydebug (value)
192 int value;
194 yydebug = value;
197 tree
198 get_constant (jcf, index)
199 JCF *jcf;
200 int index;
202 tree value;
203 int tag;
204 if (index <= 0 || index >= JPOOL_SIZE(jcf))
205 goto bad;
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);
210 switch (tag)
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;
217 break;
219 case CONSTANT_Long:
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);
229 break;
231 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
232 case CONSTANT_Float:
234 jint num = JPOOL_INT(jcf, index);
235 REAL_VALUE_TYPE d;
236 #ifdef REAL_ARITHMETIC
237 d = REAL_VALUE_FROM_TARGET_SINGLE (num);
238 #else
239 union { float f; jint i; } u;
240 u.i = num;
241 d = u.f;
242 #endif
243 value = build_real (float_type_node, d);
244 break;
246 case CONSTANT_Double:
248 HOST_WIDE_INT num[2];
249 REAL_VALUE_TYPE d;
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)
257 num[0] = hi;
258 num[1] = lo;
260 else
262 num[0] = lo;
263 num[1] = hi;
265 #ifdef REAL_ARITHMETIC
266 d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
267 #else
268 union { double d; jint i[2]; } u;
269 u.i[0] = (jint) num[0];
270 u.i[1] = (jint) num[1];
271 d = u.d;
272 #endif
273 value = build_real (double_type_node, d);
274 break;
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;
285 int i = utf8_len;
286 int str_len;
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");
295 str += char_len;
296 i -= char_len;
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++)
307 int char_value;
308 int char_len = UT8_CHAR_LENGTH (*str);
309 switch (char_len)
311 case 1:
312 char_value = *str++;
313 break;
314 case 2:
315 char_value = *str++ & 0x1F;
316 char_value = (char_value << 6) | (*str++ & 0x3F);
317 break;
318 case 3:
319 char_value = *str_ptr++ & 0x0F;
320 char_value = (char_value << 6) | (*str++ & 0x3F);
321 char_value = (char_value << 6) | (*str++ & 0x3F);
322 break;
323 default:
324 goto bad;
326 if (BYTES_BIG_ENDIAN)
328 *str_ptr++ = char_value >> 8;
329 *str_ptr++ = char_value & 0xFF;
331 else
333 *str_ptr++ = char_value & 0xFF;
334 *str_ptr++ = char_value >> 8;
338 break;
339 default:
340 goto bad;
342 pop_obstacks ();
343 JPOOL_TAG(jcf, index) = tag | CONSTANT_ResolvedFlag;
344 jcf->cpool.data [index] = (jword) value;
345 return value;
346 bad:
347 fatal ("bad value constant type %d, index %d",
348 JPOOL_TAG( jcf, index ), index);
351 tree
352 get_name_constant (jcf, index)
353 JCF *jcf;
354 int index;
356 tree name = get_constant (jcf, index);
357 if (TREE_CODE (name) != IDENTIFIER_NODE)
358 fatal ("bad nameandtype index %d", index);
359 return name;
362 static tree
363 give_name_to_class (jcf, i)
364 JCF *jcf;
365 int i;
367 if (i <= 0 || i >= JPOOL_SIZE(jcf)
368 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
369 fatal ("bad class index %d", i);
370 else
372 tree this_class;
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));
379 lineno = 0;
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;
385 return this_class;
389 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
391 tree
392 get_class_constant (JCF *jcf , int i)
394 tree type;
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));
407 else
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;
415 else
416 type = (tree) jcf->cpool.data[i];
417 return type;
420 void
421 DEFUN(jcf_out_of_synch, (jcf),
422 JCF *jcf)
424 char *source = strdup (jcf->filename);
425 int i = strlen (source);
427 while (source[i] != '.')
428 i--;
430 source [i] = '\0';
431 warning ("Class file `%s' out of synch with `%s.java'",
432 jcf->filename, source);
433 free (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. */
440 void
441 load_class (class_or_name, verbose)
442 tree class_or_name;
443 int verbose;
445 JCF this_jcf, *jcf;
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;
451 long saved_pos;
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),
461 &this_jcf, 1) == 0)
463 if (verbose)
465 error ("Cannot find class file for class %s.",
466 IDENTIFIER_POINTER (name));
467 TYPE_SIZE (class_or_name) = error_mark_node;
468 #if 0
469 /* FIXME: what to do here? */
470 if (!strcmp (classpath, DEFAULT_CLASS_PATH))
471 fatal ("giving up");
472 #endif
473 pop_obstacks (); /* FIXME: one pop_obstack() per function */
475 return;
477 else
479 this_jcf.seen_in_zip = 0;
480 current_jcf = &this_jcf;
481 if (this_jcf.outofsynch)
482 jcf_out_of_synch (current_jcf);
484 else
485 current_jcf = jcf;
487 if (current_jcf->java_source)
488 jcf_parse_source (current_jcf);
489 else {
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;*/
499 pop_obstacks ();
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)
512 JCF *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",
519 input_filename);
520 parse_source_file (1); /* Parse only */
521 java_parser_context_restore_global ();
524 /* Parse the .class file JCF. */
527 jcf_parse (jcf)
528 JCF* jcf;
530 int i, code;
532 if (jcf_parse_preamble (jcf) != 0)
533 fatal ("Not a valid Java .class file.\n");
534 code = jcf_parse_constant_pool (jcf);
535 if (code != 0)
536 fatal ("error while parsing constant pool");
537 code = verify_constant_pool (jcf);
538 if (code > 0)
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))
553 case CONSTANT_Class:
554 get_class_constant (jcf, i);
555 break;
559 code = jcf_parse_fields (jcf);
560 if (code != 0)
561 fatal ("error while parsing fields");
562 code = jcf_parse_methods (jcf);
563 if (code != 0)
564 fatal ("error while parsing methods");
565 code = jcf_parse_final_attributes (jcf);
566 if (code != 0)
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);
575 pop_obstacks ();
578 void
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);
588 else
590 CPOOL_REINIT(outgoing_cpool);
594 void
595 parse_class_file ()
597 tree method;
598 char *save_input_filename = input_filename;
599 int save_lineno = lineno;
601 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
602 lineno = 0;
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))
612 continue;
614 if (DECL_CODE_OFFSET (method) == 0)
616 error ("missing Code attribute");
617 continue;
620 lineno = 0;
621 if (DECL_LINENUMBERS_OFFSET (method))
623 register int i;
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)
635 lineno = line;
638 else
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);
651 end_java_method ();
654 if (flag_emit_class_files)
655 write_classfile (current_class);
656 make_class_data (current_class);
657 register_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. */
669 static void
670 parse_source_file (parse_only)
671 int 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);
706 yyparse ()
708 int several_files = 0;
709 char *list = strdup (input_filename), *next;
710 tree node, current_file_list = NULL_TREE;
714 next = strchr (list, '&');
715 if (next)
717 *next++ = '\0';
718 several_files = 1;
721 if (list[0])
723 char *value, len;
725 len = strlen (list);
726 if (len > 5 && ! strcmp (&list[len - 5], ".java"))
727 saw_java_source = 1;
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);
738 list = next;
740 while (next);
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 */
747 if (several_files)
749 tree name = TREE_VALUE (node);
751 /* Skip already parsed files */
752 if (HAS_BEEN_ALREADY_PARSED_P (name))
753 continue;
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>"));
760 /* Open new file */
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))
772 case JCF_ZIP:
773 parse_zip_file_entries ();
774 break;
775 case JCF_CLASS:
776 jcf_parse (current_jcf);
777 parse_class_file ();
778 break;
779 case JCF_SOURCE:
780 parse_source_file (0); /* Parse and generate */
781 break;
784 java_expand_classes ();
785 if (! flag_emit_class_files)
786 emit_register_classes ();
787 return 0;
790 static struct ZipFileCache *localToFile;
792 /* Process all class entries found in the zip file. */
793 void
794 parse_zip_file_entries (void)
796 struct ZipDirectory *zdir;
797 int i;
799 for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
800 i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
802 tree class;
804 /* We don't need to consider those files. */
805 if (!zdir->size || !zdir->filename_offset)
806 continue;
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;
820 parse_class_file ();
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()
834 int i;
835 ZipDirectory *zdir;
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;
841 tree class;
842 JCF *jcf;
843 int j;
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],
851 ".class", 6))
853 /* So it will be skipped in parse_zip_file_entries */
854 zdir->size = 0;
855 continue;
858 class_name = ALLOC (zdir->filename_length+1-6);
859 file_name = ALLOC (zdir->filename_length+1);
860 jcf = ALLOC (sizeof (JCF));
861 JCF_ZERO (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
891 zip file. */
893 DEFUN(find_in_current_zip, (name, length, jcf),
894 char *name AND int length AND JCF **jcf)
896 JCF *local_jcf;
897 tree class_name = maybe_get_identifier (name), class, icv;
899 if (!class_name)
900 return 0;
902 if (!(icv = IDENTIFIER_CLASS_VALUE (class_name)))
903 return 0;
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)
909 return 0;
911 *jcf = local_jcf = TYPE_LANG_SPECIFIC (class)->jcf;
912 fseek (local_jcf->read_state, local_jcf->zip_offset, SEEK_SET);
913 return 1;
916 /* Figure what kind of file we're dealing with */
918 DEFUN(jcf_figure_file_type, (jcf),
919 JCF *jcf)
921 unsigned char magic_string[4];
922 uint32 magic;
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)
931 return JCF_CLASS;
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 */
939 return JCF_ZIP;
942 return JCF_SOURCE;