1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001 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"
43 #ifdef HAVE_NL_LANGINFO
47 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
48 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
49 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
50 #define JPOOL_UTF_DATA(JCF, INDEX) \
51 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
52 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
54 unsigned char save; unsigned char *text; \
55 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
56 text = (JCF)->read_ptr; \
57 save = text[LENGTH]; \
59 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
60 text[LENGTH] = save; \
61 JCF_SKIP (JCF, LENGTH); } while (0)
65 extern struct obstack *saveable_obstack;
66 extern struct obstack temporary_obstack;
67 extern struct obstack permanent_obstack;
69 /* Set to non-zero value in order to emit class initilization code
70 before static field references. */
71 extern int always_initialize_class_p;
73 static tree parse_roots[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
75 /* The FIELD_DECL for the current field. */
76 #define current_field parse_roots[0]
78 /* The METHOD_DECL for the current method. */
79 #define current_method parse_roots[1]
81 /* A list of file names. */
82 #define current_file_list parse_roots[2]
84 /* The Java .class file that provides main_class; the main input file. */
85 static struct JCF main_jcf[1];
87 /* Declarations of some functions used here. */
88 static void handle_innerclass_attribute PARAMS ((int count, JCF *));
89 static tree give_name_to_class PARAMS ((JCF *jcf, int index));
90 static void parse_zip_file_entries PARAMS ((void));
91 static void process_zip_dir PARAMS ((FILE *));
92 static void parse_source_file PARAMS ((tree, FILE *));
93 static void jcf_parse_source PARAMS ((void));
94 static int jcf_figure_file_type PARAMS ((JCF *));
95 static void parse_class_file PARAMS ((void));
96 static void set_source_filename PARAMS ((JCF *, int));
97 static int predefined_filename_p PARAMS ((tree));
98 static void ggc_mark_jcf PARAMS ((void**));
100 /* Mark (for garbage collection) all the tree nodes that are
101 referenced from JCF's constant pool table. */
107 JCF *jcf = *(JCF**) elt;
110 CPool *cpool = &jcf->cpool;
111 int size = CPOOL_COUNT(cpool);
113 for (index = 1; index < size; index++)
115 int tag = JPOOL_TAG (jcf, index);
116 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
117 ggc_mark_tree ((tree) cpool->data[index]);
122 /* Handle "SourceFile" attribute. */
125 set_source_filename (jcf, index)
129 tree sfname_id = get_name_constant (jcf, index);
130 const char *sfname = IDENTIFIER_POINTER (sfname_id);
131 if (input_filename != NULL)
133 int old_len = strlen (input_filename);
134 int new_len = IDENTIFIER_LENGTH (sfname_id);
135 /* Use the current input_filename (derived from the class name)
136 if it has a directory prefix, but otherwise matches sfname. */
137 if (old_len > new_len
138 && strcmp (sfname, input_filename + old_len - new_len) == 0
139 && (input_filename[old_len - new_len - 1] == '/'
140 || input_filename[old_len - new_len - 1] == '\\'))
143 input_filename = sfname;
144 DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
145 if (current_class == main_class) main_input_filename = input_filename;
148 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
150 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
151 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
152 current_class = give_name_to_class (jcf, THIS); \
153 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
155 #define HANDLE_CLASS_INTERFACE(INDEX) \
156 add_interface (current_class, get_class_constant (jcf, INDEX))
158 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
159 { int sig_index = SIGNATURE; \
160 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
161 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
162 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
163 if ((ACCESS_FLAGS) & ACC_FINAL) \
164 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
167 #define HANDLE_END_FIELDS() \
168 (current_field = NULL_TREE)
170 #define HANDLE_CONSTANTVALUE(INDEX) \
171 { tree constant; int index = INDEX; \
172 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
173 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
174 constant = build_utf8_ref (name); \
177 constant = get_constant (jcf, index); \
178 set_constant_value (current_field, constant); }
180 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
181 (current_method = add_method (current_class, ACCESS_FLAGS, \
182 get_name_constant (jcf, NAME), \
183 get_name_constant (jcf, SIGNATURE)), \
184 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
185 DECL_LINENUMBERS_OFFSET (current_method) = 0)
187 #define HANDLE_END_METHODS() \
188 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
189 if (handle_type != current_class) layout_type (handle_type); \
190 current_method = NULL_TREE; }
192 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
193 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
194 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
195 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
196 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
198 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
200 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
201 JCF_SKIP (jcf, n * 10); }
203 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
205 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
206 JCF_SKIP (jcf, n * 4); }
208 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
211 tree list = DECL_FUNCTION_THROWS (current_method); \
214 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
215 list = tree_cons (NULL_TREE, thrown_class, list); \
217 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
220 /* Link seen inner classes to their outer context and register the
221 inner class to its outer context. They will be later loaded. */
222 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
223 handle_innerclass_attribute (COUNT, jcf)
225 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
227 /* Irrelevant decls should have been nullified by the END macros. */ \
228 if (current_method) \
229 DECL_ARTIFICIAL (current_method) = 1; \
231 DECL_ARTIFICIAL (current_field) = 1; \
234 #include "jcf-reader.c"
239 parse_signature (jcf, sig_index)
243 if (sig_index <= 0 || sig_index >= JPOOL_SIZE(jcf)
244 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
245 fatal ("invalid field/method signature");
248 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
249 JPOOL_UTF_LENGTH (jcf, sig_index));
256 /* Make identifier nodes long enough for the language-specific slots. */
257 set_identifier_size (sizeof (struct lang_identifier));
268 get_constant (jcf, index)
274 if (index <= 0 || index >= JPOOL_SIZE(jcf))
276 tag = JPOOL_TAG (jcf, index);
277 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
278 return (tree) jcf->cpool.data[index];
281 case CONSTANT_Integer:
283 jint num = JPOOL_INT(jcf, index);
284 value = build_int_2 (num, num < 0 ? -1 : 0);
285 TREE_TYPE (value) = int_type_node;
290 jint num = JPOOL_INT (jcf, index);
291 HOST_WIDE_INT lo, hi;
292 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
293 num = JPOOL_INT (jcf, index+1) & 0xffffffff;
294 add_double (lo, hi, num, 0, &lo, &hi);
295 value = build_int_2 (lo, hi);
296 TREE_TYPE (value) = long_type_node;
297 force_fit_type (value, 0);
300 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
303 jint num = JPOOL_INT(jcf, index);
305 #ifdef REAL_ARITHMETIC
306 d = REAL_VALUE_FROM_TARGET_SINGLE (num);
308 union { float f; jint i; } u;
312 value = build_real (float_type_node, d);
315 case CONSTANT_Double:
317 HOST_WIDE_INT num[2];
319 HOST_WIDE_INT lo, hi;
320 num[0] = JPOOL_INT (jcf, index);
321 lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
322 num[0] = JPOOL_INT (jcf, index+1);
323 add_double (lo, hi, num[0], 0, &lo, &hi);
324 if (FLOAT_WORDS_BIG_ENDIAN)
334 #ifdef REAL_ARITHMETIC
335 d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
338 union { double d; jint i[2]; } u;
339 u.i[0] = (jint) num[0];
340 u.i[1] = (jint) num[1];
344 value = build_real (double_type_node, d);
347 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
348 case CONSTANT_String:
350 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
351 const char *utf8_ptr = IDENTIFIER_POINTER (name);
352 int utf8_len = IDENTIFIER_LENGTH (name);
353 unsigned char *str_ptr;
355 const unsigned char *utf8;
358 /* Count the number of Unicode characters in the string,
359 while checking for a malformed Utf8 string. */
360 utf8 = (const unsigned char *) utf8_ptr;
365 int char_len = UT8_CHAR_LENGTH (*utf8);
366 if (char_len < 0 || char_len > 3 || char_len > i)
367 fatal ("bad string constant");
373 /* Allocate a scratch buffer, convert the string to UCS2, and copy it
374 into the new space. */
375 str_ptr = (unsigned char *) alloca (2 * str_len);
377 utf8 = (const unsigned char *)utf8_ptr;
379 for (i = 0; i < str_len; i++)
382 int char_len = UT8_CHAR_LENGTH (*utf8);
386 char_value = *utf8++;
389 char_value = *utf8++ & 0x1F;
390 char_value = (char_value << 6) | (*utf8++ & 0x3F);
393 char_value = *utf8++ & 0x0F;
394 char_value = (char_value << 6) | (*utf8++ & 0x3F);
395 char_value = (char_value << 6) | (*utf8++ & 0x3F);
400 if (BYTES_BIG_ENDIAN)
402 *str++ = char_value >> 8;
403 *str++ = char_value & 0xFF;
407 *str++ = char_value & 0xFF;
408 *str++ = char_value >> 8;
411 value = build_string (str - str_ptr, str_ptr);
412 TREE_TYPE (value) = build_pointer_type (string_type_node);
418 JPOOL_TAG(jcf, index) = tag | CONSTANT_ResolvedFlag;
419 jcf->cpool.data [index] = (jword) value;
422 fatal ("bad value constant type %d, index %d",
423 JPOOL_TAG( jcf, index ), index);
427 get_name_constant (jcf, index)
431 tree name = get_constant (jcf, index);
432 if (TREE_CODE (name) != IDENTIFIER_NODE)
433 fatal ("bad nameandtype index %d", index);
437 /* Handle reading innerclass attributes. If a non zero entry (denoting
438 a non anonymous entry) is found, We augment the inner class list of
439 the outer context with the newly resolved innerclass. */
442 handle_innerclass_attribute (count, jcf)
449 /* Read inner_class_info_index. This may be 0 */
450 int icii = JCF_readu2 (jcf);
451 /* Read outer_class_info_index. If the innerclasses attribute
452 entry isn't a member (like an inner class) the value is 0. */
453 int ocii = JCF_readu2 (jcf);
454 /* Read inner_name_index. If the class we're dealing with is
455 an annonymous class, it must be 0. */
456 int ini = JCF_readu2 (jcf);
457 /* If icii is 0, don't try to read the class. */
460 tree class = get_class_constant (jcf, icii);
461 tree decl = TYPE_NAME (class);
462 /* Skip reading further if ocii is null */
463 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
465 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
466 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
467 DECL_CONTEXT (decl) = outer;
468 DECL_INNER_CLASS_LIST (outer) =
469 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
470 CLASS_COMPLETE_P (decl) = 1;
478 give_name_to_class (jcf, i)
482 if (i <= 0 || i >= JPOOL_SIZE(jcf)
483 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
484 fatal ("bad class index %d", i);
488 int j = JPOOL_USHORT1 (jcf, i);
489 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
490 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
491 JPOOL_UTF_LENGTH (jcf, j));
492 this_class = lookup_class (class_name);
493 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
495 if (main_input_filename == NULL && jcf == main_jcf)
496 main_input_filename = input_filename;
498 jcf->cpool.data[i] = (jword) this_class;
499 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
504 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
507 get_class_constant (JCF *jcf , int i)
510 if (i <= 0 || i >= JPOOL_SIZE(jcf)
511 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
512 fatal ("bad class index %d", i);
514 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
516 int name_index = JPOOL_USHORT1 (jcf, i);
517 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
518 const char *name = JPOOL_UTF_DATA (jcf, name_index);
519 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
520 if (name[0] == '[') /* Handle array "classes". */
521 type = TREE_TYPE (parse_signature_string (name, nlength));
524 tree cname = unmangle_classname (name, nlength);
525 type = lookup_class (cname);
527 jcf->cpool.data[i] = (jword) type;
528 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
531 type = (tree) jcf->cpool.data[i];
535 /* Read a class with the fully qualified-name NAME.
536 Return 1 iff we read the requested file.
537 (It is still possible we failed if the file did not
538 define the class it is supposed to.) */
546 tree save_current_class = current_class;
547 const char *save_input_filename = input_filename;
548 JCF *save_current_jcf = current_jcf;
550 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
552 class = TREE_TYPE (icv);
553 jcf = TYPE_JCF (class);
560 this_jcf.zipd = NULL;
562 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
569 if (current_jcf->java_source)
572 java_parser_context_save_global ();
573 java_push_parser_context ();
574 input_filename = current_jcf->filename;
575 if (JCF_SEEN_IN_ZIP (current_jcf))
576 read_zip_member(current_jcf, current_jcf->zipd, current_jcf->zipd->zipf);
577 jcf_parse (current_jcf);
578 java_pop_parser_context (0);
579 java_parser_context_restore_global ();
582 if (! JCF_SEEN_IN_ZIP (current_jcf))
583 JCF_FINISH (current_jcf);
585 current_class = save_current_class;
586 input_filename = save_input_filename;
587 current_jcf = save_current_jcf;
591 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
592 called from the parser, otherwise it's a RECORD_TYPE node. If
593 VERBOSE is 1, print error message on failure to load a class. */
595 /* Replace calls to load_class by having callers call read_class directly
596 - and then perhaps rename read_class to load_class. FIXME */
599 load_class (class_or_name, verbose)
605 /* class_or_name can be the name of the class we want to load */
606 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
607 name = class_or_name;
608 /* In some cases, it's a dependency that we process earlier that
610 else if (TREE_CODE (class_or_name) == TREE_LIST)
611 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
612 /* Or it's a type in the making */
614 name = DECL_NAME (TYPE_NAME (class_or_name));
616 if (read_class (name) == 0 && verbose)
617 fatal ("Cannot find file for class %s.", IDENTIFIER_POINTER (name));
620 /* Parse a source file when JCF refers to a source file. */
628 java_parser_context_save_global ();
629 java_push_parser_context ();
630 BUILD_FILENAME_IDENTIFIER_NODE (file, current_jcf->filename);
631 if (wfl_operator == NULL_TREE)
632 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
633 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
634 input_filename = ggc_strdup (current_jcf->filename);
635 current_class = NULL_TREE;
636 current_function_decl = NULL_TREE;
637 if (!HAS_BEEN_ALREADY_PARSED_P (file))
639 if (!(finput = fopen (input_filename, "r")))
640 fatal ("input file `%s' just disappeared - jcf_parse_source",
642 parse_source_file (file, finput);
644 fatal ("can't close input file `%s' stream - jcf_parse_source",
647 java_pop_parser_context (IS_A_COMMAND_LINE_FILENAME_P (file));
648 java_parser_context_restore_global ();
651 /* Parse the .class file JCF. */
660 if (jcf_parse_preamble (jcf) != 0)
661 fatal ("Not a valid Java .class file.\n");
662 code = jcf_parse_constant_pool (jcf);
664 fatal ("error while parsing constant pool");
665 code = verify_constant_pool (jcf);
667 fatal ("error in constant pool entry #%d\n", code);
669 jcf_parse_class (jcf);
670 if (main_class == NULL_TREE)
671 main_class = current_class;
672 if (! quiet_flag && TYPE_NAME (current_class))
673 fprintf (stderr, " %s %s",
674 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
675 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
676 if (CLASS_LOADED_P (current_class))
678 CLASS_LOADED_P (current_class) = 1;
680 for (i = 1; i < JPOOL_SIZE(jcf); i++)
682 switch (JPOOL_TAG (jcf, i))
685 get_class_constant (jcf, i);
690 code = jcf_parse_fields (jcf);
692 fatal ("error while parsing fields");
693 code = jcf_parse_methods (jcf);
695 fatal ("error while parsing methods");
696 code = jcf_parse_final_attributes (jcf);
698 fatal ("error while parsing final attributes");
700 /* The fields of class_type_node are already in correct order. */
701 if (current_class != class_type_node && current_class != object_type_node)
702 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
704 layout_class (current_class);
705 if (current_class == object_type_node)
706 layout_class_methods (object_type_node);
708 all_class_list = tree_cons (NULL_TREE,
709 TYPE_NAME (current_class), all_class_list );
711 /* And if we came across inner classes, load them now. */
712 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (current_class)); current;
713 current = TREE_CHAIN (current))
715 tree name = DECL_NAME (TREE_PURPOSE (current));
716 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
717 if (decl && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
718 load_class (name, 1);
723 init_outgoing_cpool ()
725 current_constant_pool_data_ref = NULL_TREE;
726 outgoing_cpool = (struct CPool *)xmalloc (sizeof (struct CPool));
727 memset (outgoing_cpool, 0, sizeof (struct CPool));
734 const char *save_input_filename = input_filename;
735 int save_lineno = lineno;
737 java_layout_seen_class_methods ();
739 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
741 debug_start_source_file (input_filename);
742 init_outgoing_cpool ();
744 /* Currently we always have to emit calls to _Jv_InitClass when
745 compiling from class files. */
746 always_initialize_class_p = 1;
748 for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
749 method != NULL_TREE; method = TREE_CHAIN (method))
751 JCF *jcf = current_jcf;
753 if (METHOD_ABSTRACT (method))
756 if (METHOD_NATIVE (method))
760 DECL_MAX_LOCALS (method)
761 = list_length (TYPE_ARG_TYPES (TREE_TYPE (method)));
762 start_java_method (method);
763 give_name_to_locals (jcf);
764 expand_expr_stmt (build_jni_stub (method));
769 if (DECL_CODE_OFFSET (method) == 0)
771 error ("missing Code attribute");
776 if (DECL_LINENUMBERS_OFFSET (method))
779 register unsigned char *ptr;
780 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
781 linenumber_count = i = JCF_readu2 (jcf);
782 linenumber_table = ptr = jcf->read_ptr;
784 for (ptr += 2; --i >= 0; ptr += 4)
786 int line = GET_u2 (ptr);
787 /* Set initial lineno lineno to smallest linenumber.
788 * Needs to be set before init_function_start. */
789 if (lineno == 0 || line < lineno)
795 linenumber_table = NULL;
796 linenumber_count = 0;
799 start_java_method (method);
801 note_instructions (jcf, method);
803 give_name_to_locals (jcf);
805 /* Actually generate code. */
806 expand_byte_code (jcf, method);
811 if (flag_emit_class_files)
812 write_classfile (current_class);
816 debug_end_source_file (save_lineno);
817 input_filename = save_input_filename;
818 lineno = save_lineno;
821 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
824 parse_source_file (file, finput)
828 int save_error_count = java_error_count;
829 /* Mark the file as parsed */
830 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
832 jcf_dependency_add_file (input_filename, 0);
834 lang_init_source (1); /* Error msgs have no method prototypes */
836 /* There's no point in trying to find the current encoding unless we
837 are going to do something intelligent with it -- hence the test
840 #ifdef HAVE_NL_LANGINFO
841 setlocale (LC_CTYPE, "");
842 if (current_encoding == NULL)
843 current_encoding = nl_langinfo (CODESET);
844 #endif /* HAVE_NL_LANGINFO */
845 #endif /* HAVE_ICONV */
846 if (current_encoding == NULL || *current_encoding == '\0')
847 current_encoding = DEFAULT_ENCODING;
849 /* Initialize the parser */
850 java_init_lex (finput, current_encoding);
851 java_parse_abort_on_error ();
853 java_parse (); /* Parse and build partial tree nodes. */
854 java_parse_abort_on_error ();
855 java_complete_class (); /* Parse unsatisfied class decl. */
856 java_parse_abort_on_error ();
857 java_check_circular_reference (); /* Check on circular references */
858 java_parse_abort_on_error ();
859 java_fix_constructors (); /* Fix the constructors */
860 java_parse_abort_on_error ();
861 java_reorder_fields (); /* Reorder the fields */
865 predefined_filename_p (node)
869 for (i = 0; i < PREDEF_FILENAMES_SIZE; i++)
870 if (predef_filenames [i] == node)
878 int several_files = 0;
879 char *list = xstrdup (input_filename), *next;
885 next = strchr (list, '&');
898 int len = strlen (list);
900 if (*list != '/' && several_files)
901 obstack_grow (&temporary_obstack, "./", 2);
903 obstack_grow0 (&temporary_obstack, list, len);
904 value = obstack_finish (&temporary_obstack);
906 /* Exclude file that we see twice on the command line. For
907 all files except {Class,Error,Object,RuntimeException,String,
908 Throwable}.java we can rely on maybe_get_identifier. For
909 these files, we need to do a linear search of
910 current_file_list. This search happens only for these
911 files, presumably only when we're recompiling libgcj. */
913 if ((id = maybe_get_identifier (value)))
915 if (predefined_filename_p (id))
918 for (c = current_file_list; c; c = TREE_CHAIN (c))
919 if (TREE_VALUE (c) == id)
928 const char *saved_input_filename = input_filename;
929 input_filename = value;
930 warning ("source file seen twice on command line and will be compiled only once.");
931 input_filename = saved_input_filename;
935 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
936 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
937 current_file_list = tree_cons (NULL_TREE, node,
945 current_jcf = main_jcf;
946 current_file_list = nreverse (current_file_list);
947 for (node = current_file_list; node; node = TREE_CHAIN (node))
949 tree name = TREE_VALUE (node);
951 /* Skip already parsed files */
952 if (HAS_BEEN_ALREADY_PARSED_P (name))
955 /* Close previous descriptor, if any */
956 if (main_jcf->read_state && fclose (main_jcf->read_state))
957 fatal ("failed to close input file `%s' - yyparse",
958 (main_jcf->filename ? main_jcf->filename : "<unknown>"));
960 /* Set jcf up and open a new file */
962 main_jcf->read_state = fopen (IDENTIFIER_POINTER (name), "rb");
963 if (main_jcf->read_state == NULL)
964 pfatal_with_name (IDENTIFIER_POINTER (name));
966 /* Set new input_filename and finput */
967 finput = main_jcf->read_state;
968 #ifdef IO_BUFFER_SIZE
969 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
970 _IOFBF, IO_BUFFER_SIZE);
972 input_filename = IDENTIFIER_POINTER (name);
973 main_jcf->filbuf = jcf_filbuf_from_stdio;
975 switch (jcf_figure_file_type (current_jcf))
978 parse_zip_file_entries ();
981 jcf_parse (current_jcf);
985 java_push_parser_context ();
986 java_parser_context_save_global ();
987 parse_source_file (name, finput);
988 java_parser_context_restore_global ();
989 java_pop_parser_context (1);
994 java_expand_classes ();
995 if (!java_report_errors () && !flag_syntax_only)
996 emit_register_classes ();
1000 static struct ZipFile *localToFile;
1002 /* Process all class entries found in the zip file. */
1004 parse_zip_file_entries (void)
1006 struct ZipDirectory *zdir;
1009 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1010 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1014 /* We don't need to consider those files. */
1015 if (!zdir->size || !zdir->filename_offset)
1018 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
1019 current_jcf = TYPE_JCF (class);
1020 current_class = class;
1022 if ( !CLASS_LOADED_P (class))
1024 read_zip_member(current_jcf, zdir, localToFile);
1025 jcf_parse (current_jcf);
1028 if (TYPE_SIZE (current_class) != error_mark_node)
1030 input_filename = current_jcf->filename;
1031 parse_class_file ();
1032 FREE (current_jcf->buffer); /* No longer necessary */
1033 /* Note: there is a way to free this buffer right after a
1034 class seen in a zip file has been parsed. The idea is the
1035 set its jcf in such a way that buffer will be reallocated
1036 the time the code for the class will be generated. FIXME. */
1041 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1042 jcf up for further processing and link it to the created class. */
1045 process_zip_dir (FILE *finput)
1050 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1051 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1053 char *class_name, *file_name, *class_name_in_zip_dir;
1058 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1060 /* We choose to not to process entries with a zero size or entries
1061 not bearing the .class extention. */
1062 if (!zdir->size || !zdir->filename_offset ||
1063 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
1066 /* So it will be skipped in parse_zip_file_entries */
1071 class_name = ALLOC (zdir->filename_length+1-6);
1072 file_name = ALLOC (zdir->filename_length+1);
1073 jcf = ALLOC (sizeof (JCF));
1076 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1077 class_name [zdir->filename_length-6] = '\0';
1078 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1079 file_name [zdir->filename_length] = '\0';
1081 for (j=0; class_name[j]; j++)
1082 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1084 /* Yes, we write back the true class name into the zip directory. */
1085 strcpy (class_name_in_zip_dir, class_name);
1086 zdir->filename_length = j;
1087 class = lookup_class (get_identifier (class_name));
1089 jcf->read_state = finput;
1090 jcf->filbuf = jcf_filbuf_from_stdio;
1091 jcf->java_source = 0;
1092 jcf->classname = class_name;
1093 jcf->filename = file_name;
1096 TYPE_JCF (class) = jcf;
1100 /* Figure what kind of file we're dealing with */
1102 DEFUN(jcf_figure_file_type, (jcf),
1105 unsigned char magic_string[4];
1108 if (fread (magic_string, 1, 4, jcf->read_state) != 4)
1109 jcf_unexpected_eof (jcf, 4);
1111 fseek (jcf->read_state, 0L, SEEK_SET);
1112 magic = GET_u4 (magic_string);
1114 if (magic == 0xcafebabe)
1117 /* FIXME: is it a system file? */
1118 if (magic == (JCF_u4)ZIPMAGIC
1119 && !open_in_zip (jcf, input_filename, NULL, 0))
1121 localToFile = SeenZipFiles;
1122 /* Register all the class defined there. */
1123 process_zip_dir (jcf->read_state);
1130 /* Initialization. */
1135 /* Register roots with the garbage collector. */
1136 ggc_add_tree_root (parse_roots, sizeof (parse_roots) / sizeof(tree));
1138 ggc_add_root (¤t_jcf, 1, sizeof (JCF), ggc_mark_jcf);