This commit was manufactured by cvs2svn to create branch 'egcs'.
[official-gcc.git] / gcc / java / jcf-parse.c
blob0e8c51ccbde745c4dcc24e38706ad7b42de2d4ae
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996 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 <stdio.h>
28 #include <ctype.h>
29 #include "config.h"
30 #include "tree.h"
31 #include "obstack.h"
32 #include "flags.h"
33 #include "java-except.h"
34 #include "input.h"
35 #include "java-tree.h"
37 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
38 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
39 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
40 #define JPOOL_UTF_DATA(JCF, INDEX) \
41 ((unsigned char*) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
42 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
43 do { \
44 unsigned char save; unsigned char *text; \
45 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
46 text = (JCF)->read_ptr; \
47 save = text[LENGTH]; \
48 text[LENGTH] = 0; \
49 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
50 text[LENGTH] = save; \
51 JCF_SKIP (JCF, LENGTH); } while (0)
53 #include "jcf.h"
54 #ifdef __STDC__
55 /* For getenv */
56 #include <stdlib.h>
57 #endif
59 #ifndef SEEK_SET
60 #include <unistd.h>
61 #endif
63 extern struct obstack *saveable_obstack;
64 extern struct obstack temporary_obstack;
65 extern struct obstack permanent_obstack;
67 /* The class we are currently processing. */
68 tree current_class = NULL_TREE;
70 /* The class we started with. */
71 tree main_class = NULL_TREE;
73 /* The FIELD_DECL for the current field. */
74 static tree current_field = NULL_TREE;
76 static tree current_method = NULL_TREE;
78 static tree give_name_to_class PROTO ((JCF *jcf, int index));
80 void parse_zip_file_entries (void);
81 void process_zip_dir();
83 /* Source file compilation declarations */
84 static void parse_source_file ();
85 extern int java_error_count;
86 #define java_parse_abort_on_error() \
87 { \
88 if (java_error_count) \
89 { \
90 java_report_errors (); \
91 java_pop_parser_context (); \
92 return; \
93 } \
96 /* Handle "SourceFile" attribute. */
98 void
99 set_source_filename (jcf, index)
100 JCF *jcf;
101 int index;
103 tree sfname_id = get_name_constant (jcf, index);
104 char *sfname = IDENTIFIER_POINTER (sfname_id);
105 if (input_filename != NULL)
107 int old_len = strlen (input_filename);
108 int new_len = IDENTIFIER_LENGTH (sfname_id);
109 /* Use the current input_filename (derived from the class name)
110 if it has a directory prefix, but otherwise matches sfname. */
111 if (old_len > new_len
112 && strcmp (sfname, input_filename + old_len - new_len) == 0
113 && (input_filename[old_len - new_len - 1] == '/'
114 || input_filename[old_len - new_len - 1] == '\\'))
115 return;
117 input_filename = sfname;
118 DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
119 if (current_class == main_class) main_input_filename = input_filename;
122 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
124 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
125 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
126 current_class = give_name_to_class (jcf, THIS); \
127 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
129 #define HANDLE_CLASS_INTERFACE(INDEX) \
130 add_interface (current_class, get_class_constant (jcf, INDEX))
132 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
133 { int sig_index = SIGNATURE; \
134 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
135 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
136 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); }
138 #define HANDLE_END_FIELDS() \
139 (current_field = NULL_TREE)
141 #define HANDLE_CONSTANTVALUE(INDEX) \
142 { tree constant; int index = INDEX; \
143 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
144 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
145 constant = build_utf8_ref (name); \
147 else \
148 constant = get_constant (jcf, index); \
149 set_constant_value (current_field, constant); }
151 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
152 (current_method = add_method (current_class, ACCESS_FLAGS, \
153 get_name_constant (jcf, NAME), \
154 get_name_constant (jcf, SIGNATURE)), \
155 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
156 DECL_LINENUMBERS_OFFSET (current_method) = 0)
158 #define HANDLE_END_METHODS() \
159 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
160 if (handle_type != current_class) layout_type (handle_type); }
162 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
163 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
164 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
165 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
166 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
168 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
169 { int n = (COUNT); \
170 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
171 JCF_SKIP (jcf, n * 10); }
173 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
174 { int n = (COUNT); \
175 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
176 JCF_SKIP (jcf, n * 4); }
178 #include "jcf-reader.c"
180 static int yydebug;
182 tree
183 parse_signature (jcf, sig_index)
184 JCF *jcf;
185 int sig_index;
187 if (sig_index <= 0 || sig_index >= JPOOL_SIZE(jcf)
188 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
189 fatal ("invalid field/method signature");
190 else
192 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
193 JPOOL_UTF_LENGTH (jcf, sig_index));
197 void
198 init_lex ()
200 /* Make identifier nodes long enough for the language-specific slots. */
201 set_identifier_size (sizeof (struct lang_identifier));
204 void
205 set_yydebug (value)
206 int value;
208 yydebug = value;
211 tree
212 get_constant (jcf, index)
213 JCF *jcf;
214 int index;
216 tree value;
217 int tag;
218 if (index <= 0 || index >= JPOOL_SIZE(jcf))
219 goto bad;
220 tag = JPOOL_TAG (jcf, index);
221 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
222 return (tree) jcf->cpool.data[index];
223 push_obstacks (&permanent_obstack, &permanent_obstack);
224 switch (tag)
226 case CONSTANT_Integer:
228 jint num = JPOOL_INT(jcf, index);
229 value = build_int_2 (num, num < 0 ? -1 : 0);
230 TREE_TYPE (value) = int_type_node;
231 break;
233 case CONSTANT_Long:
235 jint num = JPOOL_INT (jcf, index);
236 HOST_WIDE_INT lo, hi;
237 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
238 num = JPOOL_INT (jcf, index+1);
239 add_double (lo, hi, num, 0, &lo, &hi);
240 value = build_int_2 (lo, hi);
241 TREE_TYPE (value) = long_type_node;
242 force_fit_type (value, 0);
243 break;
245 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
246 case CONSTANT_Float:
248 jint num = JPOOL_INT(jcf, index);
249 REAL_VALUE_TYPE d;
250 #ifdef REAL_ARITHMETIC
251 d = REAL_VALUE_FROM_TARGET_SINGLE (num);
252 #else
253 union { float f; jint i; } u;
254 u.i = num;
255 d = u.f;
256 #endif
257 value = build_real (float_type_node, d);
258 break;
260 case CONSTANT_Double:
262 HOST_WIDE_INT num[2];
263 REAL_VALUE_TYPE d;
264 HOST_WIDE_INT lo, hi;
265 num[0] = JPOOL_INT (jcf, index);
266 lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
267 num[0] = JPOOL_INT (jcf, index+1);
268 add_double (lo, hi, num[0], 0, &lo, &hi);
269 if (FLOAT_WORDS_BIG_ENDIAN)
271 num[0] = hi;
272 num[1] = lo;
274 else
276 num[0] = lo;
277 num[1] = hi;
279 #ifdef REAL_ARITHMETIC
280 d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
281 #else
282 union { double d; jint i[2]; } u;
283 u.i[0] = (jint) num[0];
284 u.i[1] = (jint) num[1];
285 d = u.d;
286 #endif
287 value = build_real (double_type_node, d);
288 break;
290 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
291 case CONSTANT_String:
293 extern struct obstack *expression_obstack;
294 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
295 char *utf8_ptr = IDENTIFIER_POINTER (name);
296 unsigned char *str_ptr;
297 int utf8_len = IDENTIFIER_LENGTH (name);
298 unsigned char *str = (unsigned char*)utf8_ptr;
299 int i = utf8_len;
300 int str_len;
302 /* Count the number of Unicode characters in the string,
303 while checking for a malformed Utf8 string. */
304 for (str_len = 0; i > 0; str_len++)
306 int char_len = UT8_CHAR_LENGTH (*str);
307 if (char_len < 0 || char_len > 2 || char_len > i)
308 fatal ("bad string constant");
309 str += char_len;
310 i -= char_len;
313 value = make_node (STRING_CST);
314 TREE_STRING_LENGTH (value) = 2 * str_len;
315 TREE_STRING_POINTER (value)
316 = obstack_alloc (expression_obstack, 2 * str_len);
317 str_ptr = (unsigned char *) TREE_STRING_POINTER (value);
318 str = (unsigned char*)utf8_ptr;
319 for (i = 0; i < str_len; i++)
321 int char_value;
322 int char_len = UT8_CHAR_LENGTH (*str);
323 switch (char_len)
325 case 1:
326 char_value = *str++;
327 break;
328 case 2:
329 char_value = *str++ & 0x1F;
330 char_value = (char_value << 6) | (*str++ & 0x3F);
331 break;
332 case 3:
333 char_value = *str_ptr++ & 0x0F;
334 char_value = (char_value << 6) | (*str++ & 0x3F);
335 char_value = (char_value << 6) | (*str++ & 0x3F);
336 break;
337 default:
338 goto bad;
340 if (BYTES_BIG_ENDIAN)
342 *str_ptr++ = char_value >> 8;
343 *str_ptr++ = char_value & 0xFF;
345 else
347 *str_ptr++ = char_value & 0xFF;
348 *str_ptr++ = char_value >> 8;
352 break;
353 default:
354 goto bad;
356 pop_obstacks ();
357 JPOOL_TAG(jcf, index) = tag | CONSTANT_ResolvedFlag;
358 jcf->cpool.data [index] = (jword) value;
359 return value;
360 bad:
361 fatal ("bad value constant type %d, index %d",
362 JPOOL_TAG( jcf, index ), index);
365 tree
366 get_name_constant (jcf, index)
367 JCF *jcf;
368 int index;
370 tree name = get_constant (jcf, index);
371 if (TREE_CODE (name) != IDENTIFIER_NODE)
372 fatal ("bad nameandtype index %d", index);
373 return name;
376 static tree
377 give_name_to_class (jcf, i)
378 JCF *jcf;
379 int i;
381 if (i <= 0 || i >= JPOOL_SIZE(jcf)
382 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
383 fatal ("bad class index %d", i);
384 else
386 tree this_class;
387 int j = JPOOL_USHORT1 (jcf, i);
388 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
389 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
390 JPOOL_UTF_LENGTH (jcf, j));
391 this_class = lookup_class (class_name);
392 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
393 lineno = 0;
394 if (main_input_filename == NULL && jcf == main_jcf)
395 main_input_filename = input_filename;
397 jcf->cpool.data[i] = (jword) this_class;
398 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
399 return this_class;
403 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
405 tree
406 get_class_constant (JCF *jcf , int i)
408 tree type;
409 if (i <= 0 || i >= JPOOL_SIZE(jcf)
410 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
411 fatal ("bad class index %d", i);
413 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
415 int name_index = JPOOL_USHORT1 (jcf, i);
416 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
417 char *name = JPOOL_UTF_DATA (jcf, name_index);
418 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
419 if (name[0] == '[') /* Handle array "classes". */
420 type = parse_signature_string (name, nlength);
421 else
423 tree cname = unmangle_classname (name, nlength);
424 type = lookup_class (cname);
426 jcf->cpool.data[i] = (jword) type;
427 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
429 else
430 type = (tree) jcf->cpool.data[i];
431 return type;
434 void
435 fix_classpath ()
437 static char default_path[] = DEFAULT_CLASS_PATH;
439 if (classpath == NULL)
441 classpath = (char *) getenv ("CLASSPATH");
442 if (classpath == NULL)
444 warning ("CLASSPATH not set");
445 classpath = default_path;
450 void
451 DEFUN(jcf_out_of_synch, (jcf),
452 JCF *jcf)
454 char *source = strdup (jcf->filename);
455 int i = strlen (source);
457 while (source[i] != '.')
458 i--;
460 source [i] = '\0';
461 warning ("Class file `%s' out of synch with `%s.java'",
462 jcf->filename, source);
463 free (source);
466 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
467 called from the parser, otherwise it's a RECORD_TYPE node. If
468 VERBOSE is 1, print error message on failure to load a class. */
470 void
471 load_class (class_or_name, verbose)
472 tree class_or_name;
473 int verbose;
475 JCF this_jcf, *jcf;
476 tree name = (TREE_CODE (class_or_name) == IDENTIFIER_NODE ?
477 class_or_name : DECL_NAME (TYPE_NAME (class_or_name)));
478 tree save_current_class = current_class;
479 char *save_input_filename = input_filename;
480 JCF *save_current_jcf = current_jcf;
481 long saved_pos;
482 if (current_jcf->read_state)
483 saved_pos = ftell (current_jcf->read_state);
485 push_obstacks (&permanent_obstack, &permanent_obstack);
487 if (!classpath)
488 fix_classpath ();
489 /* Search in current zip first. */
490 if (find_in_current_zip (IDENTIFIER_POINTER (name),
491 IDENTIFIER_LENGTH (name), &jcf) == 0)
492 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
493 &this_jcf, 1) == 0)
495 if (verbose)
497 error ("Cannot find class file class %s.",
498 IDENTIFIER_POINTER (name));
499 TYPE_SIZE (class_or_name) = error_mark_node;
500 if (!strcmp (classpath, DEFAULT_CLASS_PATH))
501 fatal ("giving up");
502 pop_obstacks (); /* FIXME: one pop_obstack() per function */
504 return;
506 else
508 this_jcf.seen_in_zip = 0;
509 current_jcf = &this_jcf;
510 if (this_jcf.outofsynch)
511 jcf_out_of_synch (current_jcf);
513 else
514 current_jcf = jcf;
516 if (current_jcf->java_source)
517 jcf_parse_source (current_jcf);
518 else {
519 int saved_lineno = lineno;
520 input_filename = current_jcf->filename;
521 jcf_parse (current_jcf);
522 lineno = saved_lineno;
525 if (!current_jcf->seen_in_zip)
526 JCF_FINISH (current_jcf);
527 /* DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;*/
528 pop_obstacks ();
530 current_class = save_current_class;
531 input_filename = save_input_filename;
532 current_jcf = save_current_jcf;
533 if (current_jcf->read_state)
534 fseek (current_jcf->read_state, saved_pos, SEEK_SET);
537 /* Parse a source file when JCF refers to a source file. This piece
538 needs further work as far as error handling and report. */
541 jcf_parse_source (jcf)
542 JCF *jcf;
544 java_parser_context_save_global ();
546 input_filename = current_jcf->filename;
547 if (!(finput = fopen (input_filename, "r")))
548 fatal ("input file `%s' just disappeared - jcf_parse_source",
549 input_filename);
551 parse_source_file (1); /* Parse only */
552 if (current_class && TREE_TYPE (current_class))
553 CLASS_FROM_SOURCE_P (TREE_TYPE (current_class)) = 1;
555 fclose (finput);
556 java_parser_context_restore_global ();
559 /* Parse the .class file JCF. */
562 jcf_parse (jcf)
563 JCF* jcf;
565 int i, code;
567 if (jcf_parse_preamble (jcf) != 0)
568 fatal ("Not a valid Java .class file.\n");
569 code = jcf_parse_constant_pool (jcf);
570 if (code != 0)
571 fatal ("error while parsing constant pool");
572 code = verify_constant_pool (jcf);
573 if (code > 0)
574 fatal ("error in constant pool entry #%d\n", code);
576 jcf_parse_class (jcf);
577 if (main_class == NULL_TREE)
578 main_class = current_class;
579 if (! quiet_flag && TYPE_NAME (current_class))
580 fprintf (stderr, " class %s",
581 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
582 CLASS_LOADED_P (current_class) = 1;
584 for (i = 1; i < JPOOL_SIZE(jcf); i++)
586 switch (JPOOL_TAG (jcf, i))
588 case CONSTANT_Class:
589 get_class_constant (jcf, i);
590 break;
594 code = jcf_parse_fields (jcf);
595 if (code != 0)
596 fatal ("error while parsing fields");
597 code = jcf_parse_methods (jcf);
598 if (code != 0)
599 fatal ("error while parsing methods");
600 code = jcf_parse_final_attributes (jcf);
601 if (code != 0)
602 fatal ("error while parsing final attributes");
604 /* The fields of class_type_node are already in correct order. */
605 if (current_class != class_type_node && current_class != object_type_node)
606 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
608 push_obstacks (&permanent_obstack, &permanent_obstack);
609 layout_class (current_class);
610 pop_obstacks ();
613 void
614 init_outgoing_cpool ()
616 current_constant_pool_data_ref = NULL_TREE;
617 if (outgoing_cpool == NULL)
619 static CPool outgoing_cpool_buffer;
620 outgoing_cpool = &outgoing_cpool_buffer;
621 CPOOL_INIT(outgoing_cpool);
623 else
625 CPOOL_REINIT(outgoing_cpool);
629 void
630 parse_class_file ()
632 tree method;
633 char *save_input_filename = input_filename;
634 int save_lineno = lineno;
636 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
637 lineno = 0;
638 debug_start_source_file (input_filename);
639 init_outgoing_cpool ();
641 for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
642 method != NULL_TREE; method = TREE_CHAIN (method))
644 JCF *jcf = current_jcf;
646 if (METHOD_NATIVE (method) || METHOD_ABSTRACT (method))
647 continue;
649 if (DECL_CODE_OFFSET (method) == 0)
651 error ("missing Code attribute");
652 continue;
655 lineno = 0;
656 if (DECL_LINENUMBERS_OFFSET (method))
658 register int i;
659 register unsigned char *ptr;
660 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
661 linenumber_count = i = JCF_readu2 (jcf);
662 linenumber_table = ptr = jcf->read_ptr;
664 for (ptr += 2; --i >= 0; ptr += 4)
666 int line = GET_u2 (ptr);
667 /* Set initial lineno lineno to smallest linenumber.
668 * Needs to be set before init_function_start. */
669 if (lineno == 0 || line < lineno)
670 lineno = line;
673 else
675 linenumber_table = NULL;
676 linenumber_count = 0;
679 start_java_method (method);
681 give_name_to_locals (jcf);
683 /* Actually generate code. */
684 expand_byte_code (jcf, method);
686 end_java_method ();
689 if (flag_emit_class_files)
690 write_classfile (current_class);
691 make_class_data (current_class);
692 register_class ();
693 rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
695 debug_end_source_file (save_lineno);
696 input_filename = save_input_filename;
697 lineno = save_lineno;
700 /* Parse a source file, as pointed by the current JCF. If PARSE_ONLY
701 is non zero, we're not parsing a file found on the command line and
702 we skip things related to code generation. */
704 static void
705 parse_source_file (parse_only)
706 int parse_only;
708 lang_init_source (1); /* Error msgs have no method prototypes */
709 java_push_parser_context ();
710 java_init_lex (); /* Initialize the parser */
711 java_parse_abort_on_error ();
712 java_parse (); /* Parse and build partial tree nodes. */
713 java_parse_abort_on_error ();
714 java_complete_class (); /* Parse unsatisfied class decl. */
715 java_parse_abort_on_error ();
716 java_check_circular_reference (); /* Check on circular references */
717 java_parse_abort_on_error ();
718 java_check_methods (); /* Check the methods */
719 java_parse_abort_on_error ();
720 java_layout_classes ();
721 java_parse_abort_on_error ();
722 if (!parse_only)
724 lang_init_source (2); /* Error msgs have method prototypes */
725 java_complete_expand_methods (); /* Complete and expand method bodies */
726 java_parse_abort_on_error ();
727 java_expand_finals (); /* Expand and check the finals */
728 java_parse_abort_on_error ();
729 java_check_final (); /* Check unitialized final */
730 java_parse_abort_on_error ();
731 if (! flag_emit_class_files)
732 emit_register_class ();
733 java_report_errors (); /* Final step for this file */
735 if (flag_emit_class_files)
736 write_classfile (current_class);
737 java_pop_parser_context ();
741 yyparse ()
743 /* Everything migh be enclosed within a loop processing each file after
744 the other one. */
746 switch (jcf_figure_file_type (current_jcf))
748 case JCF_ZIP:
749 parse_zip_file_entries ();
750 emit_register_class ();
751 break;
752 case JCF_CLASS:
753 jcf_parse (current_jcf);
754 parse_class_file ();
755 emit_register_class ();
756 break;
757 case JCF_SOURCE:
758 parse_source_file (0); /* Parse and generate */
759 break;
761 return 0;
764 static struct ZipFileCache *localToFile;
766 /* Process all class entries found in the zip file. */
767 void
768 parse_zip_file_entries (void)
770 struct ZipDirectory *zdir;
771 int i;
773 for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
774 i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
776 tree class;
778 /* We don't need to consider those files. */
779 if (!zdir->size || !zdir->filename_offset)
780 continue;
782 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
783 current_jcf = TYPE_LANG_SPECIFIC (class)->jcf;
784 current_class = class;
786 if ( !CLASS_LOADED_P (class))
788 fseek (current_jcf->read_state, current_jcf->zip_offset, SEEK_SET);
789 jcf_parse (current_jcf);
792 input_filename = current_jcf->filename;
794 parse_class_file ();
795 FREE (current_jcf->buffer); /* No longer necessary */
796 /* Note: there is a way to free this buffer right after a class seen
797 in a zip file has been parsed. The idea is the set its jcf in such
798 a way that buffer will be reallocated the time the code for the class
799 will be generated. FIXME. */
803 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
804 jcf up for further processing and link it to the created class. */
806 void process_zip_dir()
808 int i;
809 ZipDirectory *zdir;
811 for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
812 i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
814 char *class_name, *file_name, *class_name_in_zip_dir;
815 tree class;
816 JCF *jcf;
817 int j;
819 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
821 /* We choose to not to process entries with a zero size or entries
822 not bearing the .class extention. */
823 if (!zdir->size || !zdir->filename_offset ||
824 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
825 ".class", 6))
827 /* So it will be skipped in parse_zip_file_entries */
828 zdir->size = 0;
829 continue;
832 class_name = ALLOC (zdir->filename_length+1-6);
833 file_name = ALLOC (zdir->filename_length+1);
834 jcf = ALLOC (sizeof (JCF));
835 JCF_ZERO (jcf);
837 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
838 class_name [zdir->filename_length-6] = '\0';
839 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
840 file_name [zdir->filename_length] = '\0';
842 for (j=0; class_name[j]; j++)
843 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
845 /* Yes, we write back the true class name into the zip directory. */
846 strcpy (class_name_in_zip_dir, class_name);
847 zdir->filename_length = j;
848 class = lookup_class (get_identifier (class_name));
850 jcf->read_state = finput;
851 jcf->filbuf = jcf_filbuf_from_stdio;
852 jcf->seen_in_zip = 1;
853 jcf->java_source = 0;
854 jcf->zip_offset = zdir->filestart;
855 jcf->classname = class_name;
856 jcf->filename = file_name;
858 TYPE_LANG_SPECIFIC (class) =
859 (struct lang_type *) perm_calloc (1, sizeof (struct lang_type));
860 TYPE_LANG_SPECIFIC (class)->jcf = jcf;
864 /* Lookup class NAME and figure whether is a class already found in the current
865 zip file. */
867 DEFUN(find_in_current_zip, (name, length, jcf),
868 char *name AND int length AND JCF **jcf)
870 JCF *local_jcf;
871 tree class_name = maybe_get_identifier (name), class, icv;
873 if (!class_name)
874 return 0;
876 if (!(icv = IDENTIFIER_CLASS_VALUE (class_name)))
877 return 0;
879 class = TREE_TYPE (icv);
881 /* Doesn't have jcf specific info ? It's not ours */
882 if (!TYPE_LANG_SPECIFIC (class) || !TYPE_LANG_SPECIFIC (class)->jcf)
883 return 0;
885 *jcf = local_jcf = TYPE_LANG_SPECIFIC (class)->jcf;
886 fseek (local_jcf->read_state, local_jcf->zip_offset, SEEK_SET);
887 return 1;
890 /* Figure what kind of file we're dealing with */
892 DEFUN(jcf_figure_file_type, (jcf),
893 JCF *jcf)
895 unsigned char magic_string[4];
896 uint32 magic;
898 if (fread (magic_string, 1, 4, jcf->read_state) != 4)
899 jcf_unexpected_eof (jcf, 4);
901 fseek (jcf->read_state, 0L, SEEK_SET);
902 magic = GET_u4 (magic_string);
904 if (magic == 0xcafebabe)
905 return JCF_CLASS;
907 if (!open_in_zip (jcf, input_filename, NULL))
909 localToFile = ALLOC (sizeof (struct ZipFileCache));
910 bcopy (SeenZipFiles, localToFile, sizeof (struct ZipFileCache));
911 process_zip_dir (); /* Register all the class defined there */
912 return JCF_ZIP;
915 return JCF_SOURCE;