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 TRANSLATION_UNIT_DECLs for the files to be compiled. */
84 #define current_file_list parse_roots[2]
86 /* Line 0 in current file, if compiling from bytecode. */
87 static location_t file_start_location;
89 /* The Java archive that provides main_class; the main input file. */
90 static GTY(()) struct JCF * main_jcf;
92 static struct ZipFile *localToFile;
94 /* Declarations of some functions used here. */
95 static void handle_innerclass_attribute (int count, JCF *);
96 static tree give_name_to_class (JCF *jcf, int index);
97 static char *compute_class_name (struct ZipDirectory *zdir);
98 static int classify_zip_file (struct ZipDirectory *zdir);
99 static void parse_zip_file_entries (void);
100 static void process_zip_dir (FILE *);
101 static void parse_source_file_1 (tree, const char *, FILE *);
102 static void parse_source_file_2 (void);
103 static void parse_source_file_3 (void);
104 static void parse_class_file (void);
105 static void handle_deprecated (void);
106 static void set_source_filename (JCF *, int);
107 static void jcf_parse (struct JCF*);
108 static void load_inner_classes (tree);
110 /* Handle "Deprecated" attribute. */
112 handle_deprecated (void)
114 if (current_field != NULL_TREE)
115 FIELD_DEPRECATED (current_field) = 1;
116 else if (current_method != NULL_TREE)
117 METHOD_DEPRECATED (current_method) = 1;
118 else if (current_class != NULL_TREE)
119 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
122 /* Shouldn't happen. */
127 /* Handle "SourceFile" attribute. */
130 set_source_filename (JCF *jcf, int index)
132 tree sfname_id = get_name_constant (jcf, index);
133 const char *sfname = IDENTIFIER_POINTER (sfname_id);
134 const char *old_filename = input_filename;
135 int new_len = IDENTIFIER_LENGTH (sfname_id);
136 if (old_filename != NULL)
138 int old_len = strlen (old_filename);
139 /* Use the current input_filename (derived from the class name)
140 if it has a directory prefix, but otherwise matches sfname. */
141 if (old_len > new_len
142 && strcmp (sfname, old_filename + old_len - new_len) == 0
143 && (old_filename[old_len - new_len - 1] == '/'
144 || old_filename[old_len - new_len - 1] == '\\'))
147 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
149 const char *class_name
150 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
151 char *dot = strrchr (class_name, '.');
154 int i = dot - class_name;
155 /* Concatenate current package prefix with new sfname. */
156 char *buf = xmalloc (i+new_len+3);
157 /* Replace '.' by DIR_SEPARATOR. */
161 buf[i] = DIR_SEPARATOR;
163 sfname_id = get_identifier (buf);
164 sfname = IDENTIFIER_POINTER (sfname_id);
168 #ifdef USE_MAPPED_LOCATION
169 line_table.maps[line_table.used-1].to_file = sfname;
171 input_filename = sfname;
172 DECL_SOURCE_LOCATION (TYPE_NAME (current_class)) = input_location;
173 file_start_location = input_location;
175 if (current_class == main_class) main_input_filename = sfname;
178 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
180 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
181 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
182 output_class = current_class = give_name_to_class (jcf, THIS); \
183 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
185 #define HANDLE_CLASS_INTERFACE(INDEX) \
186 add_interface (current_class, get_class_constant (jcf, INDEX))
188 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
189 { int sig_index = SIGNATURE; \
190 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
191 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
192 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
193 if ((ACCESS_FLAGS) & ACC_FINAL) \
194 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
197 #define HANDLE_END_FIELDS() \
198 (current_field = NULL_TREE)
200 #define HANDLE_CONSTANTVALUE(INDEX) \
201 { tree constant; int index = INDEX; \
202 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
203 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
204 constant = build_utf8_ref (name); \
207 constant = get_constant (jcf, index); \
208 set_constant_value (current_field, constant); }
210 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
211 (current_method = add_method (current_class, ACCESS_FLAGS, \
212 get_name_constant (jcf, NAME), \
213 get_name_constant (jcf, SIGNATURE)), \
214 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
215 DECL_LINENUMBERS_OFFSET (current_method) = 0)
217 #define HANDLE_END_METHODS() \
218 { current_method = NULL_TREE; }
220 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
221 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
222 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
223 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
224 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
226 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
228 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
229 JCF_SKIP (jcf, n * 10); }
231 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
233 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
234 JCF_SKIP (jcf, n * 4); }
236 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
239 tree list = DECL_FUNCTION_THROWS (current_method); \
242 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
243 list = tree_cons (NULL_TREE, thrown_class, list); \
245 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
248 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
250 /* Link seen inner classes to their outer context and register the
251 inner class to its outer context. They will be later loaded. */
252 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
253 handle_innerclass_attribute (COUNT, jcf)
255 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
257 /* Irrelevant decls should have been nullified by the END macros. \
258 We only handle the `Synthetic' attribute on method DECLs. \
259 DECL_ARTIFICIAL on fields is used for something else (See \
260 PUSH_FIELD in java-tree.h) */ \
261 if (current_method) \
262 DECL_ARTIFICIAL (current_method) = 1; \
265 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
267 if (current_class == object_type_node) \
268 jcf->right_zip = 1; \
271 #include "jcf-reader.c"
274 parse_signature (JCF *jcf, int sig_index)
276 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
277 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
280 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
281 JPOOL_UTF_LENGTH (jcf, sig_index));
285 get_constant (JCF *jcf, int index)
289 if (index <= 0 || index >= JPOOL_SIZE(jcf))
291 tag = JPOOL_TAG (jcf, index);
292 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
293 return jcf->cpool.data[index].t;
296 case CONSTANT_Integer:
298 jint num = JPOOL_INT(jcf, index);
299 value = build_int_cst (int_type_node, num);
304 unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
305 unsigned HOST_WIDE_INT lo;
308 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
309 num = JPOOL_UINT (jcf, index+1);
310 add_double (lo, hi, num, 0, &lo, &hi);
311 value = build_int_cst_wide (long_type_node, lo, hi);
312 value = force_fit_type (value, 0, false, false);
318 jint num = JPOOL_INT(jcf, index);
322 real_from_target_fmt (&d, &buf, &ieee_single_format);
323 value = build_real (float_type_node, d);
327 case CONSTANT_Double:
332 hi = JPOOL_UINT (jcf, index);
333 lo = JPOOL_UINT (jcf, index+1);
335 if (FLOAT_WORDS_BIG_ENDIAN)
336 buf[0] = hi, buf[1] = lo;
338 buf[0] = lo, buf[1] = hi;
340 real_from_target_fmt (&d, buf, &ieee_double_format);
341 value = build_real (double_type_node, d);
345 case CONSTANT_String:
347 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
348 const char *utf8_ptr = IDENTIFIER_POINTER (name);
349 int utf8_len = IDENTIFIER_LENGTH (name);
350 const unsigned char *utf8;
353 /* Check for a malformed Utf8 string. */
354 utf8 = (const unsigned char *) utf8_ptr;
358 int char_len = UT8_CHAR_LENGTH (*utf8);
359 if (char_len < 0 || char_len > 3 || char_len > i)
360 fatal_error ("bad string constant");
366 /* Allocate a new string value. */
367 value = build_string (utf8_len, utf8_ptr);
368 TREE_TYPE (value) = build_pointer_type (string_type_node);
374 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
375 jcf->cpool.data[index].t = value;
378 internal_error ("bad value constant type %d, index %d",
379 JPOOL_TAG (jcf, index), index);
383 get_name_constant (JCF *jcf, int index)
385 tree name = get_constant (jcf, index);
387 if (TREE_CODE (name) != IDENTIFIER_NODE)
393 /* Handle reading innerclass attributes. If a nonzero entry (denoting
394 a non anonymous entry) is found, We augment the inner class list of
395 the outer context with the newly resolved innerclass. */
398 handle_innerclass_attribute (int count, JCF *jcf)
403 /* Read inner_class_info_index. This may be 0 */
404 int icii = JCF_readu2 (jcf);
405 /* Read outer_class_info_index. If the innerclasses attribute
406 entry isn't a member (like an inner class) the value is 0. */
407 int ocii = JCF_readu2 (jcf);
408 /* Read inner_name_index. If the class we're dealing with is
409 an anonymous class, it must be 0. */
410 int ini = JCF_readu2 (jcf);
411 /* Read the access flag. */
412 int acc = JCF_readu2 (jcf);
413 /* If icii is 0, don't try to read the class. */
416 tree class = get_class_constant (jcf, icii);
417 tree decl = TYPE_NAME (class);
418 /* Skip reading further if ocii is null */
419 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
421 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
422 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
423 set_class_decl_access_flags (acc, decl);
424 DECL_CONTEXT (decl) = outer;
425 DECL_INNER_CLASS_LIST (outer) =
426 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
427 CLASS_COMPLETE_P (decl) = 1;
434 give_name_to_class (JCF *jcf, int i)
436 if (i <= 0 || i >= JPOOL_SIZE (jcf)
437 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
442 int j = JPOOL_USHORT1 (jcf, i);
443 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
444 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
445 JPOOL_UTF_LENGTH (jcf, j));
446 this_class = lookup_class (class_name);
447 #ifdef USE_MAPPED_LOCATION
449 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
450 const char *sfname = IDENTIFIER_POINTER (source_name);
451 linemap_add (&line_table, LC_ENTER, false, sfname, 0);
452 input_location = linemap_line_start (&line_table, 0, 1);
453 file_start_location = input_location;
454 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
455 if (main_input_filename == NULL && jcf == main_jcf)
456 main_input_filename = sfname;
459 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (this_class));
460 if (main_input_filename == NULL && jcf == main_jcf)
461 main_input_filename = input_filename;
464 jcf->cpool.data[i].t = this_class;
465 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
470 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
473 get_class_constant (JCF *jcf, int i)
476 if (i <= 0 || i >= JPOOL_SIZE (jcf)
477 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
480 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
482 int name_index = JPOOL_USHORT1 (jcf, i);
483 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
484 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
485 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
487 if (name[0] == '[') /* Handle array "classes". */
488 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
491 tree cname = unmangle_classname (name, nlength);
492 type = lookup_class (cname);
494 jcf->cpool.data[i].t = type;
495 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
498 type = jcf->cpool.data[i].t;
502 /* Read a class with the fully qualified-name NAME.
503 Return 1 iff we read the requested file.
504 (It is still possible we failed if the file did not
505 define the class it is supposed to.) */
508 read_class (tree name)
511 tree icv, class = NULL_TREE;
512 tree save_current_class = current_class;
513 tree save_output_class = output_class;
514 location_t save_location = input_location;
515 JCF *save_current_jcf = current_jcf;
517 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
519 class = TREE_TYPE (icv);
520 jcf = TYPE_JCF (class);
527 this_jcf.zipd = NULL;
529 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
536 if (current_jcf->java_source)
538 const char *filename = current_jcf->filename;
539 tree given_file, real_file;
543 java_parser_context_save_global ();
544 java_push_parser_context ();
546 given_file = get_identifier (filename);
547 filename = IDENTIFIER_POINTER (given_file);
548 real_file = get_identifier (lrealpath (filename));
550 generate = IS_A_COMMAND_LINE_FILENAME_P (given_file);
551 output_class = current_class = NULL_TREE;
552 current_function_decl = NULL_TREE;
554 if (! HAS_BEEN_ALREADY_PARSED_P (real_file))
556 if (! (finput = fopen (filename, "r")))
557 fatal_error ("can't reopen %s: %m", filename);
559 parse_source_file_1 (real_file, filename, finput);
560 parse_source_file_2 ();
561 parse_source_file_3 ();
564 fatal_error ("can't close %s: %m", input_filename);
565 #ifdef USE_MAPPED_LOCATION
566 linemap_add (&line_table, LC_LEAVE, false, NULL, 0);
569 JCF_FINISH (current_jcf);
570 java_pop_parser_context (generate);
571 java_parser_context_restore_global ();
575 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
577 java_parser_context_save_global ();
578 java_push_parser_context ();
579 output_class = current_class = class;
580 ctxp->save_location = input_location;
581 if (JCF_SEEN_IN_ZIP (current_jcf))
582 read_zip_member(current_jcf,
583 current_jcf->zipd, current_jcf->zipd->zipf);
584 jcf_parse (current_jcf);
585 /* Parsing might change the class, in which case we have to
586 put it back where we found it. */
587 if (current_class != class && icv != NULL_TREE)
588 TREE_TYPE (icv) = current_class;
589 class = current_class;
590 java_pop_parser_context (0);
591 java_parser_context_restore_global ();
593 layout_class (class);
594 load_inner_classes (class);
597 output_class = save_output_class;
598 current_class = save_current_class;
599 input_location = save_location;
600 current_jcf = save_current_jcf;
604 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
605 called from the parser, otherwise it's a RECORD_TYPE node. If
606 VERBOSE is 1, print error message on failure to load a class. */
608 load_class (tree class_or_name, int verbose)
614 /* class_or_name can be the name of the class we want to load */
615 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
616 name = class_or_name;
617 /* In some cases, it's a dependency that we process earlier that
619 else if (TREE_CODE (class_or_name) == TREE_LIST)
620 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
621 /* Or it's a type in the making */
623 name = DECL_NAME (TYPE_NAME (class_or_name));
625 /* If the class is from source code, then it must already be loaded. */
626 class_decl = IDENTIFIER_CLASS_VALUE (name);
627 if (class_decl && CLASS_FROM_SOURCE_P (TREE_TYPE (class_decl)))
635 if ((class_loaded = read_class (name)))
638 /* We failed loading name. Now consider that we might be looking
639 for a inner class. */
640 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
641 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
645 name = get_identifier (IDENTIFIER_POINTER (name));
648 /* Otherwise we might get infinite recursion, if say we have
649 String.class but not String$CaseInsensitiveComparator.class. */
650 if (current_jcf && current_jcf->java_source == 0)
653 /* Otherwise, we failed, we bail. */
658 if (!class_loaded && verbose)
659 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
662 /* Parse the .class file JCF. */
669 if (jcf_parse_preamble (jcf) != 0)
670 fatal_error ("not a valid Java .class file");
671 code = jcf_parse_constant_pool (jcf);
673 fatal_error ("error while parsing constant pool");
674 code = verify_constant_pool (jcf);
676 fatal_error ("error in constant pool entry #%d\n", code);
678 jcf_parse_class (jcf);
679 if (main_class == NULL_TREE)
680 main_class = current_class;
681 if (! quiet_flag && TYPE_NAME (current_class))
682 fprintf (stderr, " %s %s",
683 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
684 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
685 if (CLASS_PARSED_P (current_class))
687 /* FIXME - where was first time */
688 fatal_error ("reading class %s for the second time from %s",
689 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
692 CLASS_PARSED_P (current_class) = 1;
694 for (i = 1; i < JPOOL_SIZE(jcf); i++)
696 switch (JPOOL_TAG (jcf, i))
699 get_class_constant (jcf, i);
704 code = jcf_parse_fields (jcf);
706 fatal_error ("error while parsing fields");
707 code = jcf_parse_methods (jcf);
709 fatal_error ("error while parsing methods");
710 code = jcf_parse_final_attributes (jcf);
712 fatal_error ("error while parsing final attributes");
713 #ifdef USE_MAPPED_LOCATION
714 linemap_add (&line_table, LC_LEAVE, false, NULL, 0);
717 /* The fields of class_type_node are already in correct order. */
718 if (current_class != class_type_node && current_class != object_type_node)
719 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
721 if (current_class == object_type_node)
723 layout_class_methods (object_type_node);
724 /* If we don't have the right archive, emit a verbose warning.
725 If we're generating bytecode, emit the warning only if
726 -fforce-classes-archive-check was specified. */
728 && (!flag_emit_class_files || flag_force_classes_archive_check))
729 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);
732 all_class_list = tree_cons (NULL_TREE,
733 TYPE_NAME (current_class), all_class_list );
736 /* If we came across inner classes, load them now. */
738 load_inner_classes (tree cur_class)
741 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
742 current = TREE_CHAIN (current))
744 tree name = DECL_NAME (TREE_PURPOSE (current));
745 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
746 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
747 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
748 load_class (name, 1);
753 parse_class_file (void)
756 location_t save_location = input_location;
758 java_layout_seen_class_methods ();
760 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
761 (*debug_hooks->start_source_file) (input_line, input_filename);
763 /* Currently we always have to emit calls to _Jv_InitClass when
764 compiling from class files. */
765 always_initialize_class_p = 1;
767 gen_indirect_dispatch_tables (current_class);
769 java_mark_class_local (current_class);
771 for (method = TYPE_METHODS (current_class);
772 method != NULL_TREE; method = TREE_CHAIN (method))
774 JCF *jcf = current_jcf;
776 if (METHOD_ABSTRACT (method))
779 if (METHOD_NATIVE (method))
786 /* We need to compute the DECL_MAX_LOCALS. We need to take
787 the wide types into account too. */
788 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
789 arg != end_params_node;
790 arg = TREE_CHAIN (arg), decl_max_locals += 1)
792 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
793 decl_max_locals += 1;
795 DECL_MAX_LOCALS (method) = decl_max_locals;
796 start_java_method (method);
797 give_name_to_locals (jcf);
798 *get_stmts () = build_jni_stub (method);
803 if (DECL_CODE_OFFSET (method) == 0)
805 current_function_decl = method;
806 error ("missing Code attribute");
810 input_location = file_start_location;
811 if (DECL_LINENUMBERS_OFFSET (method))
816 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
817 linenumber_count = i = JCF_readu2 (jcf);
818 linenumber_table = ptr = jcf->read_ptr;
820 for (ptr += 2; --i >= 0; ptr += 4)
822 int line = GET_u2 (ptr);
823 /* Set initial input_line to smallest linenumber.
824 * Needs to be set before init_function_start. */
825 if (min_line == 0 || line < min_line)
828 #ifdef USE_MAPPED_LOCATION
830 input_location = linemap_line_start (&line_table, min_line, 1);
833 input_line = min_line;
838 linenumber_table = NULL;
839 linenumber_count = 0;
842 start_java_method (method);
844 note_instructions (jcf, method);
846 give_name_to_locals (jcf);
848 /* Convert bytecode to trees. */
849 expand_byte_code (jcf, method);
854 if (flag_emit_class_files)
855 write_classfile (current_class);
859 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
860 input_location = save_location;
863 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
866 parse_source_file_1 (tree real_file, const char *filename, FILE *finput)
868 int save_error_count = java_error_count;
870 /* Mark the file as parsed. */
871 HAS_BEEN_ALREADY_PARSED_P (real_file) = 1;
873 lang_init_source (1); /* Error msgs have no method prototypes */
875 /* There's no point in trying to find the current encoding unless we
876 are going to do something intelligent with it -- hence the test
878 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
879 setlocale (LC_CTYPE, "");
880 if (current_encoding == NULL)
881 current_encoding = nl_langinfo (CODESET);
883 if (current_encoding == NULL || *current_encoding == '\0')
884 current_encoding = DEFAULT_ENCODING;
886 #ifdef USE_MAPPED_LOCATION
887 linemap_add (&line_table, LC_ENTER, false, filename, 0);
888 input_location = linemap_line_start (&line_table, 0, 125);
890 input_filename = filename;
893 ctxp->file_start_location = input_location;
894 ctxp->filename = filename;
896 jcf_dependency_add_file (input_filename, 0);
898 /* Initialize the parser */
899 java_init_lex (finput, current_encoding);
900 java_parse_abort_on_error ();
902 java_parse (); /* Parse and build partial tree nodes. */
903 java_parse_abort_on_error ();
906 /* Process a parsed source file, resolving names etc. */
909 parse_source_file_2 (void)
911 int save_error_count = java_error_count;
912 java_complete_class (); /* Parse unsatisfied class decl. */
913 java_parse_abort_on_error ();
917 parse_source_file_3 (void)
919 int save_error_count = java_error_count;
920 java_check_circular_reference (); /* Check on circular references */
921 java_parse_abort_on_error ();
922 java_fix_constructors (); /* Fix the constructors */
923 java_parse_abort_on_error ();
924 java_reorder_fields (); /* Reorder the fields */
928 add_predefined_file (tree name)
930 predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
934 predefined_filename_p (tree node)
938 for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
940 if (TREE_VALUE (iter) == node)
946 /* Generate a function that does all static initialization for this
950 java_emit_static_constructor (void)
954 emit_register_classes (&body);
955 write_resource_constructor (&body);
958 cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
962 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
964 int filename_count = 0;
965 location_t save_location = input_location;
971 if (flag_filelist_file)
974 finput = fopen (main_input_filename, "r");
976 fatal_error ("can't open %s: %m", input_filename);
977 list = xmalloc(avail);
985 avail = 2 * (count + avail);
986 list = xrealloc (list, avail);
988 avail = avail - count;
990 /* Subtract to to guarantee space for final '\0'. */
991 count = fread (next, 1, avail - 1, finput);
995 fatal_error ("error closing %s: %m", input_filename);
1006 list = (char *) main_input_filename;
1010 for (next = list; ; )
1013 if (flag_filelist_file && ! in_quotes
1014 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1015 || ch == '&') /* FIXME */)
1029 if (flag_filelist_file && ch == '"')
1031 in_quotes = ! in_quotes;
1048 node = get_identifier (list);
1052 /* Exclude file that we see twice on the command line. */
1054 if (IS_A_COMMAND_LINE_FILENAME_P (node))
1056 location_t warn_loc;
1057 #ifdef USE_MAPPED_LOCATION
1058 linemap_add (&line_table, LC_RENAME, 0,
1059 IDENTIFIER_POINTER (node), 0);
1060 warn_loc = linemap_line_start (&line_table, 0, 1);
1062 warn_loc.file = IDENTIFIER_POINTER (node);
1065 warning ("%Hsource file seen twice on command line and "
1066 "will be compiled only once", &warn_loc);
1070 tree file_decl = build_decl (TRANSLATION_UNIT_DECL, node, NULL);
1071 TREE_CHAIN (file_decl) = current_file_list;
1072 current_file_list = file_decl;
1073 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1079 if (filename_count == 0)
1080 warning ("no input file specified");
1084 const char *resource_filename;
1086 /* Only one resource file may be compiled at a time. */
1087 assert (TREE_CHAIN (current_file_list) == NULL);
1089 resource_filename = IDENTIFIER_POINTER (DECL_NAME (current_file_list));
1090 compile_resource_file (resource_name, resource_filename);
1095 current_jcf = main_jcf;
1096 current_file_list = nreverse (current_file_list);
1097 for (node = current_file_list; node; node = TREE_CHAIN (node))
1099 unsigned char magic_string[4];
1101 tree name = DECL_NAME (node);
1103 const char *filename = IDENTIFIER_POINTER (name);
1105 /* Skip already parsed files */
1106 real_file = get_identifier (lrealpath (filename));
1107 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1110 /* Close previous descriptor, if any */
1111 if (finput && fclose (finput))
1112 fatal_error ("can't close input file %s: %m", main_input_filename);
1114 finput = fopen (filename, "rb");
1116 fatal_error ("can't open %s: %m", filename);
1118 #ifdef IO_BUFFER_SIZE
1119 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1120 _IOFBF, IO_BUFFER_SIZE);
1123 /* Figure what kind of file we're dealing with */
1124 if (fread (magic_string, 1, 4, finput) == 4)
1126 fseek (finput, 0L, SEEK_SET);
1127 magic = GET_u4 (magic_string);
1129 if (magic == 0xcafebabe)
1131 CLASS_FILE_P (node) = 1;
1132 current_jcf = ggc_alloc (sizeof (JCF));
1133 JCF_ZERO (current_jcf);
1134 current_jcf->read_state = finput;
1135 current_jcf->filbuf = jcf_filbuf_from_stdio;
1136 jcf_parse (current_jcf);
1137 DECL_SOURCE_LOCATION (node) = file_start_location;
1138 TYPE_JCF (current_class) = current_jcf;
1139 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1140 TREE_TYPE (node) = current_class;
1142 else if (magic == (JCF_u4)ZIPMAGIC)
1144 main_jcf = ggc_alloc (sizeof (JCF));
1145 JCF_ZERO (main_jcf);
1146 main_jcf->read_state = finput;
1147 main_jcf->filbuf = jcf_filbuf_from_stdio;
1148 #ifdef USE_MAPPED_LOCATION
1149 linemap_add (&line_table, LC_ENTER, false, filename, 0);
1150 input_location = linemap_line_start (&line_table, 0, 1);
1152 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
1153 fatal_error ("bad zip/jar file %s", filename);
1154 localToFile = SeenZipFiles;
1155 /* Register all the classes defined there. */
1156 process_zip_dir (main_jcf->read_state);
1157 #ifdef USE_MAPPED_LOCATION
1158 linemap_add (&line_table, LC_LEAVE, false, NULL, 0);
1160 parse_zip_file_entries ();
1163 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1168 java_push_parser_context ();
1169 java_parser_context_save_global ();
1171 parse_source_file_1 (real_file, filename, finput);
1172 java_parser_context_restore_global ();
1173 java_pop_parser_context (1);
1174 #ifdef USE_MAPPED_LOCATION
1175 linemap_add (&line_table, LC_LEAVE, false, NULL, 0);
1180 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1182 input_location = ctxp->file_start_location;
1183 parse_source_file_2 ();
1186 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1188 input_location = ctxp->file_start_location;
1189 parse_source_file_3 ();
1192 for (node = current_file_list; node; node = TREE_CHAIN (node))
1194 input_location = DECL_SOURCE_LOCATION (node);
1195 if (CLASS_FILE_P (node))
1197 output_class = current_class = TREE_TYPE (node);
1198 current_jcf = TYPE_JCF (current_class);
1199 layout_class (current_class);
1200 load_inner_classes (current_class);
1201 parse_class_file ();
1202 JCF_FINISH (current_jcf);
1205 input_location = save_location;
1207 java_expand_classes ();
1208 if (java_report_errors () || flag_syntax_only)
1211 /* Expand all classes compiled from source. */
1212 java_finish_classes ();
1215 /* Arrange for any necessary initialization to happen. */
1216 java_emit_static_constructor ();
1218 /* Only finalize the compilation unit after we've told cgraph which
1219 functions have their addresses stored. */
1220 cgraph_finalize_compilation_unit ();
1225 /* Return the name of the class corresponding to the name of the file
1226 in this zip entry. The result is newly allocated using ALLOC. */
1228 compute_class_name (struct ZipDirectory *zdir)
1230 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1234 class_name = ALLOC (zdir->filename_length + 1 - 6);
1235 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length - 6);
1236 class_name [zdir->filename_length - 6] = '\0';
1237 for (j = 0; class_name[j]; ++j)
1238 class_name[j] = class_name[j] == '/' ? '.' : class_name[j];
1242 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
1243 if it is a property file of some sort. */
1245 classify_zip_file (struct ZipDirectory *zdir)
1247 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1249 if (zdir->filename_length > 6
1250 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
1254 /* For now we drop the manifest, but not other information. */
1255 if (zdir->filename_length == 20
1256 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
1259 /* Drop directory entries. */
1260 if (zdir->filename_length > 0
1261 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
1267 /* Process all class entries found in the zip file. */
1269 parse_zip_file_entries (void)
1271 struct ZipDirectory *zdir;
1274 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1275 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1279 switch (classify_zip_file (zdir))
1286 char *class_name = compute_class_name (zdir);
1287 class = lookup_class (get_identifier (class_name));
1289 current_jcf = TYPE_JCF (class);
1290 output_class = current_class = class;
1292 if (! CLASS_LOADED_P (class))
1294 if (! CLASS_PARSED_P (class))
1296 read_zip_member (current_jcf, zdir, localToFile);
1297 jcf_parse (current_jcf);
1299 layout_class (current_class);
1300 load_inner_classes (current_class);
1303 if (TYPE_SIZE (current_class) != error_mark_node)
1305 parse_class_file ();
1306 FREE (current_jcf->buffer); /* No longer necessary */
1307 /* Note: there is a way to free this buffer right after a
1308 class seen in a zip file has been parsed. The idea is the
1309 set its jcf in such a way that buffer will be reallocated
1310 the time the code for the class will be generated. FIXME. */
1317 char *file_name, *class_name_in_zip_dir, *buffer;
1319 file_name = ALLOC (zdir->filename_length + 1);
1320 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1321 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1322 file_name[zdir->filename_length] = '\0';
1323 jcf = ALLOC (sizeof (JCF));
1325 jcf->read_state = finput;
1326 jcf->filbuf = jcf_filbuf_from_stdio;
1327 jcf->java_source = 0;
1328 jcf->classname = NULL;
1329 jcf->filename = file_name;
1332 if (read_zip_member (jcf, zdir, localToFile) < 0)
1333 fatal_error ("error while reading %s from zip file", file_name);
1335 buffer = ALLOC (zdir->filename_length + 1 +
1336 (jcf->buffer_end - jcf->buffer));
1337 strcpy (buffer, file_name);
1338 /* This is not a typo: we overwrite the trailing \0 of the
1339 file name; this is just how the data is laid out. */
1340 memcpy (buffer + zdir->filename_length,
1341 jcf->buffer, jcf->buffer_end - jcf->buffer);
1343 compile_resource_data (file_name, buffer,
1344 jcf->buffer_end - jcf->buffer);
1357 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1358 jcf up for further processing and link it to the created class. */
1361 process_zip_dir (FILE *finput)
1366 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1367 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1369 char *class_name, *file_name, *class_name_in_zip_dir;
1373 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1375 /* Here we skip non-class files; we handle them later. */
1376 if (classify_zip_file (zdir) != 1)
1379 class_name = compute_class_name (zdir);
1380 file_name = ALLOC (zdir->filename_length+1);
1381 jcf = ggc_alloc (sizeof (JCF));
1384 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1385 file_name [zdir->filename_length] = '\0';
1387 class = lookup_class (get_identifier (class_name));
1389 jcf->read_state = finput;
1390 jcf->filbuf = jcf_filbuf_from_stdio;
1391 jcf->java_source = 0;
1392 jcf->classname = class_name;
1393 jcf->filename = file_name;
1396 TYPE_JCF (class) = jcf;
1400 /* Initialization. */
1403 init_jcf_parse (void)
1408 #include "gt-java-jcf-parse.h"