1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
30 #include "coretypes.h"
36 #include "java-except.h"
38 #include "java-tree.h"
51 #ifdef HAVE_LANGINFO_CODESET
55 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
56 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
57 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
58 #define JPOOL_UTF_DATA(JCF, INDEX) \
59 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
60 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
62 unsigned char save; unsigned char *text; \
63 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
64 text = (JCF)->read_ptr; \
65 save = text[LENGTH]; \
67 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
68 text[LENGTH] = save; \
69 JCF_SKIP (JCF, LENGTH); } while (0)
73 extern struct obstack temporary_obstack;
75 static GTY(()) tree parse_roots[3];
77 /* The FIELD_DECL for the current field. */
78 #define current_field parse_roots[0]
80 /* The METHOD_DECL for the current method. */
81 #define current_method parse_roots[1]
83 /* A list of file names. */
84 #define current_file_list parse_roots[2]
86 /* The Java archive that provides main_class; the main input file. */
87 static GTY(()) struct JCF * main_jcf;
89 static struct ZipFile *localToFile;
91 /* Declarations of some functions used here. */
92 static void handle_innerclass_attribute (int count, JCF *);
93 static tree give_name_to_class (JCF *jcf, int index);
94 static char *compute_class_name (struct ZipDirectory *zdir);
95 static int classify_zip_file (struct ZipDirectory *zdir);
96 static void parse_zip_file_entries (void);
97 static void process_zip_dir (FILE *);
98 static void parse_source_file_1 (tree, FILE *);
99 static void parse_source_file_2 (void);
100 static void parse_source_file_3 (void);
101 static void parse_class_file (void);
102 static void handle_deprecated (void);
103 static void set_source_filename (JCF *, int);
104 static void jcf_parse (struct JCF*);
105 static void load_inner_classes (tree);
107 /* Handle "Deprecated" attribute. */
109 handle_deprecated (void)
111 if (current_field != NULL_TREE)
112 FIELD_DEPRECATED (current_field) = 1;
113 else if (current_method != NULL_TREE)
114 METHOD_DEPRECATED (current_method) = 1;
115 else if (current_class != NULL_TREE)
116 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
119 /* Shouldn't happen. */
124 /* Handle "SourceFile" attribute. */
127 set_source_filename (JCF *jcf, int 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 output_class = 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 { current_method = NULL_TREE; }
190 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
191 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
192 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
193 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
194 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
196 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
198 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
199 JCF_SKIP (jcf, n * 10); }
201 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
203 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
204 JCF_SKIP (jcf, n * 4); }
206 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
209 tree list = DECL_FUNCTION_THROWS (current_method); \
212 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
213 list = tree_cons (NULL_TREE, thrown_class, list); \
215 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
218 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
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 We only handle the `Synthetic' attribute on method DECLs. \
229 DECL_ARTIFICIAL on fields is used for something else (See \
230 PUSH_FIELD in java-tree.h) */ \
231 if (current_method) \
232 DECL_ARTIFICIAL (current_method) = 1; \
235 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
237 if (current_class == object_type_node) \
238 jcf->right_zip = 1; \
241 #include "jcf-reader.c"
244 parse_signature (JCF *jcf, int sig_index)
246 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
247 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
250 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
251 JPOOL_UTF_LENGTH (jcf, sig_index));
255 get_constant (JCF *jcf, int index)
259 if (index <= 0 || index >= JPOOL_SIZE(jcf))
261 tag = JPOOL_TAG (jcf, index);
262 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
263 return jcf->cpool.data[index].t;
266 case CONSTANT_Integer:
268 jint num = JPOOL_INT(jcf, index);
269 value = build_int_2 (num, num < 0 ? -1 : 0);
270 TREE_TYPE (value) = int_type_node;
275 unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
276 unsigned HOST_WIDE_INT lo;
278 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
279 num = JPOOL_UINT (jcf, index+1);
280 add_double (lo, hi, num, 0, &lo, &hi);
281 value = build_int_2 (lo, hi);
282 TREE_TYPE (value) = long_type_node;
283 force_fit_type (value, 0);
289 jint num = JPOOL_INT(jcf, index);
293 real_from_target_fmt (&d, &buf, &ieee_single_format);
294 value = build_real (float_type_node, d);
298 case CONSTANT_Double:
303 hi = JPOOL_UINT (jcf, index);
304 lo = JPOOL_UINT (jcf, index+1);
306 if (FLOAT_WORDS_BIG_ENDIAN)
307 buf[0] = hi, buf[1] = lo;
309 buf[0] = lo, buf[1] = hi;
311 real_from_target_fmt (&d, buf, &ieee_double_format);
312 value = build_real (double_type_node, d);
316 case CONSTANT_String:
318 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
319 const char *utf8_ptr = IDENTIFIER_POINTER (name);
320 int utf8_len = IDENTIFIER_LENGTH (name);
321 const unsigned char *utf8;
324 /* Check for a malformed Utf8 string. */
325 utf8 = (const unsigned char *) utf8_ptr;
329 int char_len = UT8_CHAR_LENGTH (*utf8);
330 if (char_len < 0 || char_len > 3 || char_len > i)
331 fatal_error ("bad string constant");
337 /* Allocate a new string value. */
338 value = build_string (utf8_len, utf8_ptr);
339 TREE_TYPE (value) = build_pointer_type (string_type_node);
345 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
346 jcf->cpool.data[index].t = value;
349 internal_error ("bad value constant type %d, index %d",
350 JPOOL_TAG (jcf, index), index);
354 get_name_constant (JCF *jcf, int index)
356 tree name = get_constant (jcf, index);
358 if (TREE_CODE (name) != IDENTIFIER_NODE)
364 /* Handle reading innerclass attributes. If a nonzero entry (denoting
365 a non anonymous entry) is found, We augment the inner class list of
366 the outer context with the newly resolved innerclass. */
369 handle_innerclass_attribute (int count, JCF *jcf)
374 /* Read inner_class_info_index. This may be 0 */
375 int icii = JCF_readu2 (jcf);
376 /* Read outer_class_info_index. If the innerclasses attribute
377 entry isn't a member (like an inner class) the value is 0. */
378 int ocii = JCF_readu2 (jcf);
379 /* Read inner_name_index. If the class we're dealing with is
380 an anonymous class, it must be 0. */
381 int ini = JCF_readu2 (jcf);
382 /* Read the access flag. */
383 int acc = JCF_readu2 (jcf);
384 /* If icii is 0, don't try to read the class. */
387 tree class = get_class_constant (jcf, icii);
388 tree decl = TYPE_NAME (class);
389 /* Skip reading further if ocii is null */
390 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
392 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
393 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
394 set_class_decl_access_flags (acc, decl);
395 DECL_CONTEXT (decl) = outer;
396 DECL_INNER_CLASS_LIST (outer) =
397 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
398 CLASS_COMPLETE_P (decl) = 1;
405 give_name_to_class (JCF *jcf, int i)
407 if (i <= 0 || i >= JPOOL_SIZE (jcf)
408 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
413 int j = JPOOL_USHORT1 (jcf, i);
414 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
415 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
416 JPOOL_UTF_LENGTH (jcf, j));
417 this_class = lookup_class (class_name);
418 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
420 if (main_input_filename == NULL && jcf == main_jcf)
421 main_input_filename = input_filename;
423 jcf->cpool.data[i].t = this_class;
424 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
429 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
432 get_class_constant (JCF *jcf, int i)
435 if (i <= 0 || i >= JPOOL_SIZE (jcf)
436 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
439 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
441 int name_index = JPOOL_USHORT1 (jcf, i);
442 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
443 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
444 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
446 if (name[0] == '[') /* Handle array "classes". */
447 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
450 tree cname = unmangle_classname (name, nlength);
451 type = lookup_class (cname);
453 jcf->cpool.data[i].t = type;
454 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
457 type = jcf->cpool.data[i].t;
461 /* Read a class with the fully qualified-name NAME.
462 Return 1 iff we read the requested file.
463 (It is still possible we failed if the file did not
464 define the class it is supposed to.) */
467 read_class (tree name)
470 tree icv, class = NULL_TREE;
471 tree save_current_class = current_class;
472 tree save_output_class = output_class;
473 location_t save_location = input_location;
474 JCF *save_current_jcf = current_jcf;
476 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
478 class = TREE_TYPE (icv);
479 jcf = TYPE_JCF (class);
486 this_jcf.zipd = NULL;
488 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
495 if (current_jcf->java_source)
497 const char *filename = current_jcf->filename;
498 tree given_file, real_file;
502 java_parser_context_save_global ();
503 java_push_parser_context ();
505 given_file = get_identifier (filename);
506 real_file = get_identifier (lrealpath (filename));
508 generate = IS_A_COMMAND_LINE_FILENAME_P (given_file);
509 if (wfl_operator == NULL_TREE)
510 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
511 EXPR_WFL_FILENAME_NODE (wfl_operator) = given_file;
512 input_filename = ggc_strdup (filename);
513 output_class = current_class = NULL_TREE;
514 current_function_decl = NULL_TREE;
516 if (! HAS_BEEN_ALREADY_PARSED_P (real_file))
518 if (! (finput = fopen (input_filename, "r")))
519 fatal_error ("can't reopen %s: %m", input_filename);
521 parse_source_file_1 (real_file, finput);
522 parse_source_file_2 ();
523 parse_source_file_3 ();
526 fatal_error ("can't close %s: %m", input_filename);
528 JCF_FINISH (current_jcf);
529 java_pop_parser_context (generate);
530 java_parser_context_restore_global ();
534 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
536 java_parser_context_save_global ();
537 java_push_parser_context ();
538 output_class = current_class = class;
539 input_filename = current_jcf->filename;
540 if (JCF_SEEN_IN_ZIP (current_jcf))
541 read_zip_member(current_jcf,
542 current_jcf->zipd, current_jcf->zipd->zipf);
543 jcf_parse (current_jcf);
544 /* Parsing might change the class, in which case we have to
545 put it back where we found it. */
546 if (current_class != class && icv != NULL_TREE)
547 TREE_TYPE (icv) = current_class;
548 class = current_class;
549 java_pop_parser_context (0);
550 java_parser_context_restore_global ();
552 layout_class (class);
553 load_inner_classes (class);
556 output_class = save_output_class;
557 current_class = save_current_class;
558 input_location = save_location;
559 current_jcf = save_current_jcf;
563 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
564 called from the parser, otherwise it's a RECORD_TYPE node. If
565 VERBOSE is 1, print error message on failure to load a class. */
567 load_class (tree class_or_name, int verbose)
572 /* class_or_name can be the name of the class we want to load */
573 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
574 name = class_or_name;
575 /* In some cases, it's a dependency that we process earlier that
577 else if (TREE_CODE (class_or_name) == TREE_LIST)
578 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
579 /* Or it's a type in the making */
582 /* If the class is from source code, then it must already be loaded. */
583 if (CLASS_FROM_SOURCE_P (class_or_name))
585 name = DECL_NAME (TYPE_NAME (class_or_name));
593 if ((class_loaded = read_class (name)))
596 /* We failed loading name. Now consider that we might be looking
597 for a inner class. */
598 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
599 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
603 name = get_identifier (IDENTIFIER_POINTER (name));
606 /* Otherwise, we failed, we bail. */
611 if (!class_loaded && verbose)
612 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
615 /* Parse the .class file JCF. */
622 if (jcf_parse_preamble (jcf) != 0)
623 fatal_error ("not a valid Java .class file");
624 code = jcf_parse_constant_pool (jcf);
626 fatal_error ("error while parsing constant pool");
627 code = verify_constant_pool (jcf);
629 fatal_error ("error in constant pool entry #%d\n", code);
631 jcf_parse_class (jcf);
632 if (main_class == NULL_TREE)
633 main_class = current_class;
634 if (! quiet_flag && TYPE_NAME (current_class))
635 fprintf (stderr, " %s %s",
636 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
637 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
638 if (CLASS_PARSED_P (current_class))
640 /* FIXME - where was first time */
641 fatal_error ("reading class %s for the second time from %s",
642 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
645 CLASS_PARSED_P (current_class) = 1;
647 for (i = 1; i < JPOOL_SIZE(jcf); i++)
649 switch (JPOOL_TAG (jcf, i))
652 get_class_constant (jcf, i);
657 code = jcf_parse_fields (jcf);
659 fatal_error ("error while parsing fields");
660 code = jcf_parse_methods (jcf);
662 fatal_error ("error while parsing methods");
663 code = jcf_parse_final_attributes (jcf);
665 fatal_error ("error while parsing final attributes");
667 /* The fields of class_type_node are already in correct order. */
668 if (current_class != class_type_node && current_class != object_type_node)
669 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
671 if (current_class == object_type_node)
673 layout_class_methods (object_type_node);
674 /* If we don't have the right archive, emit a verbose warning.
675 If we're generating bytecode, emit the warning only if
676 -fforce-classes-archive-check was specified. */
678 && (!flag_emit_class_files || flag_force_classes_archive_check))
679 fatal_error ("the `java.lang.Object' that was found in `%s' didn't have the special zero-length `gnu.gcj.gcj-compiled' attribute. This generally means that your classpath is incorrectly set. Use `info gcj \"Input Options\"' to see the info page describing how to set the classpath", jcf->filename);
682 all_class_list = tree_cons (NULL_TREE,
683 TYPE_NAME (current_class), all_class_list );
686 /* If we came across inner classes, load them now. */
688 load_inner_classes (tree cur_class)
691 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
692 current = TREE_CHAIN (current))
694 tree name = DECL_NAME (TREE_PURPOSE (current));
695 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
696 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
697 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
698 load_class (name, 1);
703 parse_class_file (void)
706 location_t save_location = input_location;
708 java_layout_seen_class_methods ();
710 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
712 (*debug_hooks->start_source_file) (input_line, input_filename);
714 /* Currently we always have to emit calls to _Jv_InitClass when
715 compiling from class files. */
716 always_initialize_class_p = 1;
718 gen_indirect_dispatch_tables (current_class);
720 java_mark_class_local (current_class);
722 for (method = TYPE_METHODS (current_class);
723 method != NULL_TREE; method = TREE_CHAIN (method))
725 JCF *jcf = current_jcf;
727 if (METHOD_ABSTRACT (method))
730 if (METHOD_NATIVE (method))
737 /* We need to compute the DECL_MAX_LOCALS. We need to take
738 the wide types into account too. */
739 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
740 arg != end_params_node;
741 arg = TREE_CHAIN (arg), decl_max_locals += 1)
743 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
744 decl_max_locals += 1;
746 DECL_MAX_LOCALS (method) = decl_max_locals;
747 start_java_method (method);
748 give_name_to_locals (jcf);
749 *get_stmts () = build_jni_stub (method);
754 if (DECL_CODE_OFFSET (method) == 0)
756 current_function_decl = method;
757 error ("missing Code attribute");
762 if (DECL_LINENUMBERS_OFFSET (method))
766 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
767 linenumber_count = i = JCF_readu2 (jcf);
768 linenumber_table = ptr = jcf->read_ptr;
770 for (ptr += 2; --i >= 0; ptr += 4)
772 int line = GET_u2 (ptr);
773 /* Set initial input_line to smallest linenumber.
774 * Needs to be set before init_function_start. */
775 if (input_line == 0 || line < input_line)
781 linenumber_table = NULL;
782 linenumber_count = 0;
785 start_java_method (method);
787 note_instructions (jcf, method);
789 give_name_to_locals (jcf);
791 /* Convert bytecode to trees. */
792 expand_byte_code (jcf, method);
797 if (flag_emit_class_files)
798 write_classfile (current_class);
802 (*debug_hooks->end_source_file) (save_location.line);
803 input_location = save_location;
806 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
809 parse_source_file_1 (tree real_file, FILE *finput)
811 int save_error_count = java_error_count;
813 /* Mark the file as parsed. */
814 HAS_BEEN_ALREADY_PARSED_P (real_file) = 1;
816 jcf_dependency_add_file (input_filename, 0);
818 lang_init_source (1); /* Error msgs have no method prototypes */
820 /* There's no point in trying to find the current encoding unless we
821 are going to do something intelligent with it -- hence the test
823 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
824 setlocale (LC_CTYPE, "");
825 if (current_encoding == NULL)
826 current_encoding = nl_langinfo (CODESET);
828 if (current_encoding == NULL || *current_encoding == '\0')
829 current_encoding = DEFAULT_ENCODING;
831 /* Initialize the parser */
832 java_init_lex (finput, current_encoding);
833 java_parse_abort_on_error ();
835 java_parse (); /* Parse and build partial tree nodes. */
836 java_parse_abort_on_error ();
839 /* Process a parsed source file, resolving names etc. */
842 parse_source_file_2 (void)
844 int save_error_count = java_error_count;
845 java_complete_class (); /* Parse unsatisfied class decl. */
846 java_parse_abort_on_error ();
850 parse_source_file_3 (void)
852 int save_error_count = java_error_count;
853 java_check_circular_reference (); /* Check on circular references */
854 java_parse_abort_on_error ();
855 java_fix_constructors (); /* Fix the constructors */
856 java_parse_abort_on_error ();
857 java_reorder_fields (); /* Reorder the fields */
861 add_predefined_file (tree name)
863 predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
867 predefined_filename_p (tree node)
871 for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
873 if (TREE_VALUE (iter) == node)
879 /* Generate a function that does all static initialization for this
883 java_emit_static_constructor (void)
887 emit_register_classes (&body);
888 write_resource_constructor (&body);
891 cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
895 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
897 int filename_count = 0;
902 if (flag_filelist_file)
905 finput = fopen (input_filename, "r");
907 fatal_error ("can't open %s: %m", input_filename);
908 list = xmalloc(avail);
916 avail = 2 * (count + avail);
917 list = xrealloc (list, avail);
919 avail = avail - count;
921 /* Subtract to to guarantee space for final '\0'. */
922 count = fread (next, 1, avail - 1, finput);
926 fatal_error ("error closing %s: %m", input_filename);
937 list = input_filename ? xstrdup (input_filename) : 0;
941 for (next = list; ; )
944 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
945 || ch == '&' /* FIXME */)
973 int len = strlen (list);
975 obstack_grow0 (&temporary_obstack, list, len);
976 value = obstack_finish (&temporary_obstack);
980 /* Exclude file that we see twice on the command line. For
981 all files except {Class,Error,Object,RuntimeException,String,
982 Throwable}.java we can rely on maybe_get_identifier. For
983 these files, we need to do a linear search of
984 current_file_list. This search happens only for these
985 files, presumably only when we're recompiling libgcj. */
987 if ((id = maybe_get_identifier (value)))
989 if (predefined_filename_p (id))
992 for (c = current_file_list; c; c = TREE_CHAIN (c))
993 if (TREE_VALUE (c) == id)
1002 location_t warn_loc;
1003 warn_loc.file = value;
1005 warning ("%Hsource file seen twice on command line and "
1006 "will be compiled only once", &warn_loc);
1010 node = get_identifier (value);
1011 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1012 current_file_list = tree_cons (NULL_TREE, node,
1019 if (filename_count == 0)
1020 warning ("no input file specified");
1024 const char *resource_filename;
1026 /* Only one resource file may be compiled at a time. */
1027 assert (TREE_CHAIN (current_file_list) == NULL);
1029 resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
1030 compile_resource_file (resource_name, resource_filename);
1035 current_jcf = main_jcf;
1036 current_file_list = nreverse (current_file_list);
1037 for (node = current_file_list; node; node = TREE_CHAIN (node))
1039 unsigned char magic_string[4];
1041 tree name = TREE_VALUE (node);
1044 /* Skip already parsed files */
1045 real_file = get_identifier (lrealpath (IDENTIFIER_POINTER (name)));
1046 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1049 /* Close previous descriptor, if any */
1050 if (finput && fclose (finput))
1051 fatal_error ("can't close input file %s: %m", main_input_filename);
1053 finput = fopen (IDENTIFIER_POINTER (name), "rb");
1055 fatal_error ("can't open %s: %m", IDENTIFIER_POINTER (name));
1057 #ifdef IO_BUFFER_SIZE
1058 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1059 _IOFBF, IO_BUFFER_SIZE);
1061 input_filename = IDENTIFIER_POINTER (name);
1063 /* Figure what kind of file we're dealing with */
1064 if (fread (magic_string, 1, 4, finput) == 4)
1066 fseek (finput, 0L, SEEK_SET);
1067 magic = GET_u4 (magic_string);
1069 if (magic == 0xcafebabe)
1071 CLASS_FILE_P (node) = 1;
1072 current_jcf = ggc_alloc (sizeof (JCF));
1073 JCF_ZERO (current_jcf);
1074 current_jcf->read_state = finput;
1075 current_jcf->filbuf = jcf_filbuf_from_stdio;
1076 jcf_parse (current_jcf);
1077 TYPE_JCF (current_class) = current_jcf;
1078 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1079 TREE_PURPOSE (node) = current_class;
1081 else if (magic == (JCF_u4)ZIPMAGIC)
1083 ZIP_FILE_P (node) = 1;
1084 main_jcf = ggc_alloc (sizeof (JCF));
1085 JCF_ZERO (main_jcf);
1086 main_jcf->read_state = finput;
1087 main_jcf->filbuf = jcf_filbuf_from_stdio;
1088 if (open_in_zip (main_jcf, input_filename, NULL, 0) < 0)
1089 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1090 localToFile = SeenZipFiles;
1091 /* Register all the classes defined there. */
1092 process_zip_dir (main_jcf->read_state);
1093 parse_zip_file_entries ();
1096 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1101 JAVA_FILE_P (node) = 1;
1102 java_push_parser_context ();
1103 java_parser_context_save_global ();
1105 parse_source_file_1 (real_file, finput);
1106 java_parser_context_restore_global ();
1107 java_pop_parser_context (1);
1111 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1113 input_filename = ctxp->filename;
1114 parse_source_file_2 ();
1117 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1119 input_filename = ctxp->filename;
1120 parse_source_file_3 ();
1123 for (node = current_file_list; node; node = TREE_CHAIN (node))
1125 input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1126 if (CLASS_FILE_P (node))
1128 output_class = current_class = TREE_PURPOSE (node);
1129 current_jcf = TYPE_JCF (current_class);
1130 layout_class (current_class);
1131 load_inner_classes (current_class);
1132 parse_class_file ();
1133 JCF_FINISH (current_jcf);
1136 input_filename = main_input_filename;
1138 java_expand_classes ();
1139 if (java_report_errors () || flag_syntax_only)
1142 /* Expand all classes compiled from source. */
1143 java_finish_classes ();
1146 /* Arrange for any necessary initialization to happen. */
1147 java_emit_static_constructor ();
1149 /* Only finalize the compilation unit after we've told cgraph which
1150 functions have their addresses stored. */
1151 cgraph_finalize_compilation_unit ();
1156 /* Return the name of the class corresponding to the name of the file
1157 in this zip entry. The result is newly allocated using ALLOC. */
1159 compute_class_name (struct ZipDirectory *zdir)
1161 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1165 class_name = ALLOC (zdir->filename_length + 1 - 6);
1166 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length - 6);
1167 class_name [zdir->filename_length - 6] = '\0';
1168 for (j = 0; class_name[j]; ++j)
1169 class_name[j] = class_name[j] == '/' ? '.' : class_name[j];
1173 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
1174 if it is a property file of some sort. */
1176 classify_zip_file (struct ZipDirectory *zdir)
1178 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1180 if (zdir->filename_length > 6
1181 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
1185 /* For now we drop the manifest, but not other information. */
1186 if (zdir->filename_length == 20
1187 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
1190 /* Drop directory entries. */
1191 if (zdir->filename_length > 0
1192 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
1198 /* Process all class entries found in the zip file. */
1200 parse_zip_file_entries (void)
1202 struct ZipDirectory *zdir;
1205 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1206 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1210 switch (classify_zip_file (zdir))
1217 char *class_name = compute_class_name (zdir);
1218 class = lookup_class (get_identifier (class_name));
1220 current_jcf = TYPE_JCF (class);
1221 output_class = current_class = class;
1223 if (! CLASS_LOADED_P (class))
1225 if (! CLASS_PARSED_P (class))
1227 read_zip_member (current_jcf, zdir, localToFile);
1228 jcf_parse (current_jcf);
1230 layout_class (current_class);
1231 load_inner_classes (current_class);
1234 if (TYPE_SIZE (current_class) != error_mark_node)
1236 input_filename = current_jcf->filename;
1237 parse_class_file ();
1238 FREE (current_jcf->buffer); /* No longer necessary */
1239 /* Note: there is a way to free this buffer right after a
1240 class seen in a zip file has been parsed. The idea is the
1241 set its jcf in such a way that buffer will be reallocated
1242 the time the code for the class will be generated. FIXME. */
1249 char *file_name, *class_name_in_zip_dir, *buffer;
1251 file_name = ALLOC (zdir->filename_length + 1);
1252 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1253 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1254 file_name[zdir->filename_length] = '\0';
1255 jcf = ALLOC (sizeof (JCF));
1257 jcf->read_state = finput;
1258 jcf->filbuf = jcf_filbuf_from_stdio;
1259 jcf->java_source = 0;
1260 jcf->classname = NULL;
1261 jcf->filename = file_name;
1264 if (read_zip_member (jcf, zdir, localToFile) < 0)
1265 fatal_error ("error while reading %s from zip file", file_name);
1267 buffer = ALLOC (zdir->filename_length + 1 +
1268 (jcf->buffer_end - jcf->buffer));
1269 strcpy (buffer, file_name);
1270 /* This is not a typo: we overwrite the trailing \0 of the
1271 file name; this is just how the data is laid out. */
1272 memcpy (buffer + zdir->filename_length,
1273 jcf->buffer, jcf->buffer_end - jcf->buffer);
1275 compile_resource_data (file_name, buffer,
1276 jcf->buffer_end - jcf->buffer);
1289 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1290 jcf up for further processing and link it to the created class. */
1293 process_zip_dir (FILE *finput)
1298 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1299 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1301 char *class_name, *file_name, *class_name_in_zip_dir;
1305 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1307 /* Here we skip non-class files; we handle them later. */
1308 if (classify_zip_file (zdir) != 1)
1311 class_name = compute_class_name (zdir);
1312 file_name = ALLOC (zdir->filename_length+1);
1313 jcf = ggc_alloc (sizeof (JCF));
1316 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1317 file_name [zdir->filename_length] = '\0';
1319 class = lookup_class (get_identifier (class_name));
1321 jcf->read_state = finput;
1322 jcf->filbuf = jcf_filbuf_from_stdio;
1323 jcf->java_source = 0;
1324 jcf->classname = class_name;
1325 jcf->filename = file_name;
1328 TYPE_JCF (class) = jcf;
1332 /* Initialization. */
1335 init_jcf_parse (void)
1340 #include "gt-java-jcf-parse.h"