2002-03-31 Segher Boessenkool <segher@koffie.nl>
[official-gcc.git] / gcc / java / jcf-parse.c
blob8daceccf4dd17e6919d23f8c200637ea0751f5e4
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003
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)
10 any later version.
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> */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "real.h"
34 #include "obstack.h"
35 #include "flags.h"
36 #include "java-except.h"
37 #include "input.h"
38 #include "java-tree.h"
39 #include "toplev.h"
40 #include "parse.h"
41 #include "ggc.h"
42 #include "debug.h"
43 #include "assert.h"
44 #include "tm_p.h"
46 #ifdef HAVE_LOCALE_H
47 #include <locale.h>
48 #endif
50 #ifdef HAVE_LANGINFO_CODESET
51 #include <langinfo.h>
52 #endif
54 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
55 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
56 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
57 #define JPOOL_UTF_DATA(JCF, INDEX) \
58 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
59 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
60 do { \
61 unsigned char save; unsigned char *text; \
62 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
63 text = (JCF)->read_ptr; \
64 save = text[LENGTH]; \
65 text[LENGTH] = 0; \
66 (JCF)->cpool.data[INDEX].t = get_identifier (text); \
67 text[LENGTH] = save; \
68 JCF_SKIP (JCF, LENGTH); } while (0)
70 #include "jcf.h"
72 extern struct obstack temporary_obstack;
74 /* Set to nonzero value in order to emit class initialization code
75 before static field references. */
76 extern int always_initialize_class_p;
78 static GTY(()) tree parse_roots[3];
80 /* The FIELD_DECL for the current field. */
81 #define current_field parse_roots[0]
83 /* The METHOD_DECL for the current method. */
84 #define current_method parse_roots[1]
86 /* A list of file names. */
87 #define current_file_list parse_roots[2]
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, 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. */
111 static void
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;
120 else
122 /* Shouldn't happen. */
123 abort ();
127 /* Handle "SourceFile" attribute. */
129 static void
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 if (input_filename != NULL)
136 int old_len = strlen (input_filename);
137 int new_len = IDENTIFIER_LENGTH (sfname_id);
138 /* Use the current input_filename (derived from the class name)
139 if it has a directory prefix, but otherwise matches sfname. */
140 if (old_len > new_len
141 && strcmp (sfname, input_filename + old_len - new_len) == 0
142 && (input_filename[old_len - new_len - 1] == '/'
143 || input_filename[old_len - new_len - 1] == '\\'))
144 return;
146 input_filename = sfname;
147 DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
148 if (current_class == main_class) main_input_filename = input_filename;
151 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
153 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
154 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
155 current_class = give_name_to_class (jcf, THIS); \
156 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
158 #define HANDLE_CLASS_INTERFACE(INDEX) \
159 add_interface (current_class, get_class_constant (jcf, INDEX))
161 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
162 { int sig_index = SIGNATURE; \
163 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
164 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
165 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
166 if ((ACCESS_FLAGS) & ACC_FINAL) \
167 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
170 #define HANDLE_END_FIELDS() \
171 (current_field = NULL_TREE)
173 #define HANDLE_CONSTANTVALUE(INDEX) \
174 { tree constant; int index = INDEX; \
175 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
176 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
177 constant = build_utf8_ref (name); \
179 else \
180 constant = get_constant (jcf, index); \
181 set_constant_value (current_field, constant); }
183 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
184 (current_method = add_method (current_class, ACCESS_FLAGS, \
185 get_name_constant (jcf, NAME), \
186 get_name_constant (jcf, SIGNATURE)), \
187 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
188 DECL_LINENUMBERS_OFFSET (current_method) = 0)
190 #define HANDLE_END_METHODS() \
191 { current_method = NULL_TREE; }
193 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
194 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
195 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
196 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
197 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
199 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
200 { int n = (COUNT); \
201 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
202 JCF_SKIP (jcf, n * 10); }
204 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
205 { int n = (COUNT); \
206 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
207 JCF_SKIP (jcf, n * 4); }
209 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
211 int n = COUNT; \
212 tree list = DECL_FUNCTION_THROWS (current_method); \
213 while (--n >= 0) \
215 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
216 list = tree_cons (NULL_TREE, thrown_class, list); \
218 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
221 #define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
223 /* Link seen inner classes to their outer context and register the
224 inner class to its outer context. They will be later loaded. */
225 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
226 handle_innerclass_attribute (COUNT, jcf)
228 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
230 /* Irrelevant decls should have been nullified by the END macros. \
231 We only handle the `Synthetic' attribute on method DECLs. \
232 DECL_ARTIFICIAL on fields is used for something else (See \
233 PUSH_FIELD in java-tree.h) */ \
234 if (current_method) \
235 DECL_ARTIFICIAL (current_method) = 1; \
238 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
240 if (current_class == object_type_node) \
241 jcf->right_zip = 1; \
244 #include "jcf-reader.c"
246 tree
247 parse_signature (JCF *jcf, int sig_index)
249 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
250 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
251 abort ();
252 else
253 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
254 JPOOL_UTF_LENGTH (jcf, sig_index));
257 tree
258 get_constant (JCF *jcf, int index)
260 tree value;
261 int tag;
262 if (index <= 0 || index >= JPOOL_SIZE(jcf))
263 goto bad;
264 tag = JPOOL_TAG (jcf, index);
265 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
266 return jcf->cpool.data[index].t;
267 switch (tag)
269 case CONSTANT_Integer:
271 jint num = JPOOL_INT(jcf, index);
272 value = build_int_2 (num, num < 0 ? -1 : 0);
273 TREE_TYPE (value) = int_type_node;
274 break;
276 case CONSTANT_Long:
278 unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
279 HOST_WIDE_INT lo, hi;
280 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
281 num = JPOOL_UINT (jcf, index+1);
282 add_double (lo, hi, num, 0, &lo, &hi);
283 value = build_int_2 (lo, hi);
284 TREE_TYPE (value) = long_type_node;
285 force_fit_type (value, 0);
286 break;
289 case CONSTANT_Float:
291 jint num = JPOOL_INT(jcf, index);
292 long buf = num;
293 REAL_VALUE_TYPE d;
295 real_from_target_fmt (&d, &buf, &ieee_single_format);
296 value = build_real (float_type_node, d);
297 break;
300 case CONSTANT_Double:
302 long buf[2], lo, hi;
303 REAL_VALUE_TYPE d;
305 hi = JPOOL_UINT (jcf, index);
306 lo = JPOOL_UINT (jcf, index+1);
308 if (FLOAT_WORDS_BIG_ENDIAN)
309 buf[0] = hi, buf[1] = lo;
310 else
311 buf[0] = lo, buf[1] = hi;
313 real_from_target_fmt (&d, buf, &ieee_double_format);
314 value = build_real (double_type_node, d);
315 break;
318 case CONSTANT_String:
320 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
321 const char *utf8_ptr = IDENTIFIER_POINTER (name);
322 int utf8_len = IDENTIFIER_LENGTH (name);
323 const unsigned char *utf8;
324 int i;
326 /* Check for a malformed Utf8 string. */
327 utf8 = (const unsigned char *) utf8_ptr;
328 i = utf8_len;
329 while (i > 0)
331 int char_len = UT8_CHAR_LENGTH (*utf8);
332 if (char_len < 0 || char_len > 3 || char_len > i)
333 fatal_error ("bad string constant");
335 utf8 += char_len;
336 i -= char_len;
339 /* Allocate a new string value. */
340 value = build_string (utf8_len, utf8_ptr);
341 TREE_TYPE (value) = build_pointer_type (string_type_node);
343 break;
344 default:
345 goto bad;
347 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
348 jcf->cpool.data[index].t = value;
349 return value;
350 bad:
351 internal_error ("bad value constant type %d, index %d",
352 JPOOL_TAG (jcf, index), index);
355 tree
356 get_name_constant (JCF *jcf, int index)
358 tree name = get_constant (jcf, index);
360 if (TREE_CODE (name) != IDENTIFIER_NODE)
361 abort ();
363 return name;
366 /* Handle reading innerclass attributes. If a nonzero entry (denoting
367 a non anonymous entry) is found, We augment the inner class list of
368 the outer context with the newly resolved innerclass. */
370 static void
371 handle_innerclass_attribute (int count, JCF *jcf)
373 int c = (count);
374 while (c--)
376 /* Read inner_class_info_index. This may be 0 */
377 int icii = JCF_readu2 (jcf);
378 /* Read outer_class_info_index. If the innerclasses attribute
379 entry isn't a member (like an inner class) the value is 0. */
380 int ocii = JCF_readu2 (jcf);
381 /* Read inner_name_index. If the class we're dealing with is
382 an anonymous class, it must be 0. */
383 int ini = JCF_readu2 (jcf);
384 /* Read the access flag. */
385 int acc = JCF_readu2 (jcf);
386 /* If icii is 0, don't try to read the class. */
387 if (icii >= 0)
389 tree class = get_class_constant (jcf, icii);
390 tree decl = TYPE_NAME (class);
391 /* Skip reading further if ocii is null */
392 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
394 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
395 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
396 set_class_decl_access_flags (acc, decl);
397 DECL_CONTEXT (decl) = outer;
398 DECL_INNER_CLASS_LIST (outer) =
399 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
400 CLASS_COMPLETE_P (decl) = 1;
406 static tree
407 give_name_to_class (JCF *jcf, int i)
409 if (i <= 0 || i >= JPOOL_SIZE (jcf)
410 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
411 abort ();
412 else
414 tree this_class;
415 int j = JPOOL_USHORT1 (jcf, i);
416 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
417 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
418 JPOOL_UTF_LENGTH (jcf, j));
419 this_class = lookup_class (class_name);
420 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
421 lineno = 0;
422 if (main_input_filename == NULL && jcf == main_jcf)
423 main_input_filename = input_filename;
425 jcf->cpool.data[i].t = this_class;
426 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
427 return this_class;
431 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
433 tree
434 get_class_constant (JCF *jcf, int i)
436 tree type;
437 if (i <= 0 || i >= JPOOL_SIZE (jcf)
438 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
439 abort ();
441 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
443 int name_index = JPOOL_USHORT1 (jcf, i);
444 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
445 const char *name = JPOOL_UTF_DATA (jcf, name_index);
446 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
448 if (name[0] == '[') /* Handle array "classes". */
449 type = TREE_TYPE (parse_signature_string (name, nlength));
450 else
452 tree cname = unmangle_classname (name, nlength);
453 type = lookup_class (cname);
455 jcf->cpool.data[i].t = type;
456 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
458 else
459 type = jcf->cpool.data[i].t;
460 return type;
463 /* Read a class with the fully qualified-name NAME.
464 Return 1 iff we read the requested file.
465 (It is still possible we failed if the file did not
466 define the class it is supposed to.) */
469 read_class (tree name)
471 JCF this_jcf, *jcf;
472 tree icv, class = NULL_TREE;
473 tree save_current_class = current_class;
474 const char *save_input_filename = input_filename;
475 JCF *save_current_jcf = current_jcf;
477 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
479 class = TREE_TYPE (icv);
480 jcf = TYPE_JCF (class);
482 else
483 jcf = NULL;
485 if (jcf == NULL)
487 this_jcf.zipd = NULL;
488 jcf = &this_jcf;
489 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
490 &this_jcf, 1) == 0)
491 return 0;
494 current_jcf = jcf;
496 if (current_jcf->java_source)
498 const char *filename = current_jcf->filename;
499 tree file;
500 FILE *finput;
501 int generate;
503 java_parser_context_save_global ();
504 java_push_parser_context ();
505 BUILD_FILENAME_IDENTIFIER_NODE (file, filename);
506 generate = IS_A_COMMAND_LINE_FILENAME_P (file);
507 if (wfl_operator == NULL_TREE)
508 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
509 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
510 input_filename = ggc_strdup (filename);
511 current_class = NULL_TREE;
512 current_function_decl = NULL_TREE;
513 if (!HAS_BEEN_ALREADY_PARSED_P (file))
515 if (!(finput = fopen (input_filename, "r")))
516 fatal_io_error ("can't reopen %s", input_filename);
517 parse_source_file_1 (file, finput);
518 parse_source_file_2 ();
519 parse_source_file_3 ();
520 if (fclose (finput))
521 fatal_io_error ("can't close %s", input_filename);
523 JCF_FINISH (current_jcf);
524 java_pop_parser_context (generate);
525 java_parser_context_restore_global ();
527 else
529 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
531 java_parser_context_save_global ();
532 java_push_parser_context ();
533 current_class = class;
534 input_filename = current_jcf->filename;
535 if (JCF_SEEN_IN_ZIP (current_jcf))
536 read_zip_member(current_jcf,
537 current_jcf->zipd, current_jcf->zipd->zipf);
538 jcf_parse (current_jcf);
539 /* Parsing might change the class, in which case we have to
540 put it back where we found it. */
541 if (current_class != class && icv != NULL_TREE)
542 TREE_TYPE (icv) = current_class;
543 class = current_class;
544 java_pop_parser_context (0);
545 java_parser_context_restore_global ();
547 layout_class (class);
548 load_inner_classes (class);
551 current_class = save_current_class;
552 input_filename = save_input_filename;
553 current_jcf = save_current_jcf;
554 return 1;
557 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
558 called from the parser, otherwise it's a RECORD_TYPE node. If
559 VERBOSE is 1, print error message on failure to load a class. */
561 /* Replace calls to load_class by having callers call read_class directly
562 - and then perhaps rename read_class to load_class. FIXME */
564 void
565 load_class (tree class_or_name, int verbose)
567 tree name, saved;
568 int class_loaded;
570 /* class_or_name can be the name of the class we want to load */
571 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
572 name = class_or_name;
573 /* In some cases, it's a dependency that we process earlier that
574 we though */
575 else if (TREE_CODE (class_or_name) == TREE_LIST)
576 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
577 /* Or it's a type in the making */
578 else
579 name = DECL_NAME (TYPE_NAME (class_or_name));
581 saved = name;
582 while (1)
584 char *separator;
586 if ((class_loaded = read_class (name)))
587 break;
589 /* We failed loading name. Now consider that we might be looking
590 for a inner class. */
591 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
592 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
594 int c = *separator;
595 *separator = '\0';
596 name = get_identifier (IDENTIFIER_POINTER (name));
597 *separator = c;
599 /* Otherwise, we failed, we bail. */
600 else
601 break;
604 if (!class_loaded && verbose)
605 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
608 /* Parse the .class file JCF. */
610 void
611 jcf_parse (JCF* jcf)
613 int i, code;
615 if (jcf_parse_preamble (jcf) != 0)
616 fatal_error ("not a valid Java .class file");
617 code = jcf_parse_constant_pool (jcf);
618 if (code != 0)
619 fatal_error ("error while parsing constant pool");
620 code = verify_constant_pool (jcf);
621 if (code > 0)
622 fatal_error ("error in constant pool entry #%d\n", code);
624 jcf_parse_class (jcf);
625 if (main_class == NULL_TREE)
626 main_class = current_class;
627 if (! quiet_flag && TYPE_NAME (current_class))
628 fprintf (stderr, " %s %s",
629 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
630 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
631 if (CLASS_PARSED_P (current_class))
633 /* FIXME - where was first time */
634 fatal_error ("reading class %s for the second time from %s",
635 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
636 jcf->filename);
638 CLASS_PARSED_P (current_class) = 1;
640 for (i = 1; i < JPOOL_SIZE(jcf); i++)
642 switch (JPOOL_TAG (jcf, i))
644 case CONSTANT_Class:
645 get_class_constant (jcf, i);
646 break;
650 code = jcf_parse_fields (jcf);
651 if (code != 0)
652 fatal_error ("error while parsing fields");
653 code = jcf_parse_methods (jcf);
654 if (code != 0)
655 fatal_error ("error while parsing methods");
656 code = jcf_parse_final_attributes (jcf);
657 if (code != 0)
658 fatal_error ("error while parsing final attributes");
660 /* The fields of class_type_node are already in correct order. */
661 if (current_class != class_type_node && current_class != object_type_node)
662 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
664 if (current_class == object_type_node)
666 layout_class_methods (object_type_node);
667 /* If we don't have the right archive, emit a verbose warning.
668 If we're generating bytecode, emit the warning only if
669 -fforce-classes-archive-check was specified. */
670 if (!jcf->right_zip
671 && (!flag_emit_class_files || flag_force_classes_archive_check))
672 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);
674 else
675 all_class_list = tree_cons (NULL_TREE,
676 TYPE_NAME (current_class), all_class_list );
679 /* If we came across inner classes, load them now. */
680 static void
681 load_inner_classes (tree cur_class)
683 tree current;
684 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
685 current = TREE_CHAIN (current))
687 tree name = DECL_NAME (TREE_PURPOSE (current));
688 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
689 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
690 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
691 load_class (name, 1);
695 void
696 init_outgoing_cpool (void)
698 outgoing_cpool = ggc_alloc_cleared (sizeof (struct CPool));
701 static void
702 parse_class_file (void)
704 tree method, field;
705 const char *save_input_filename = input_filename;
706 int save_lineno = lineno;
708 java_layout_seen_class_methods ();
710 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
711 lineno = 0;
712 (*debug_hooks->start_source_file) (lineno, input_filename);
713 init_outgoing_cpool ();
715 /* Currently we always have to emit calls to _Jv_InitClass when
716 compiling from class files. */
717 always_initialize_class_p = 1;
719 for (field = TYPE_FIELDS (current_class);
720 field != NULL_TREE; field = TREE_CHAIN (field))
721 if (FIELD_STATIC (field))
722 DECL_EXTERNAL (field) = 0;
724 for (method = TYPE_METHODS (current_class);
725 method != NULL_TREE; method = TREE_CHAIN (method))
727 JCF *jcf = current_jcf;
729 if (METHOD_ABSTRACT (method))
730 continue;
732 if (METHOD_NATIVE (method))
734 tree arg;
735 int decl_max_locals;
737 if (! flag_jni)
738 continue;
739 /* We need to compute the DECL_MAX_LOCALS. We need to take
740 the wide types into account too. */
741 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
742 arg != end_params_node;
743 arg = TREE_CHAIN (arg), decl_max_locals += 1)
745 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
746 decl_max_locals += 1;
748 DECL_MAX_LOCALS (method) = decl_max_locals;
749 start_java_method (method);
750 give_name_to_locals (jcf);
751 expand_expr_stmt (build_jni_stub (method));
752 end_java_method ();
753 continue;
756 if (DECL_CODE_OFFSET (method) == 0)
758 current_function_decl = method;
759 error ("missing Code attribute");
760 continue;
763 lineno = 0;
764 if (DECL_LINENUMBERS_OFFSET (method))
766 register int i;
767 register unsigned char *ptr;
768 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
769 linenumber_count = i = JCF_readu2 (jcf);
770 linenumber_table = ptr = jcf->read_ptr;
772 for (ptr += 2; --i >= 0; ptr += 4)
774 int line = GET_u2 (ptr);
775 /* Set initial lineno lineno to smallest linenumber.
776 * Needs to be set before init_function_start. */
777 if (lineno == 0 || line < lineno)
778 lineno = line;
781 else
783 linenumber_table = NULL;
784 linenumber_count = 0;
787 start_java_method (method);
789 note_instructions (jcf, method);
791 give_name_to_locals (jcf);
793 /* Actually generate code. */
794 expand_byte_code (jcf, method);
796 end_java_method ();
799 if (flag_emit_class_files)
800 write_classfile (current_class);
802 finish_class ();
804 (*debug_hooks->end_source_file) (save_lineno);
805 input_filename = save_input_filename;
806 lineno = save_lineno;
809 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
811 static void
812 parse_source_file_1 (tree file, FILE *finput)
814 int save_error_count = java_error_count;
815 /* Mark the file as parsed */
816 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
818 jcf_dependency_add_file (input_filename, 0);
820 lang_init_source (1); /* Error msgs have no method prototypes */
822 /* There's no point in trying to find the current encoding unless we
823 are going to do something intelligent with it -- hence the test
824 for iconv. */
825 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
826 setlocale (LC_CTYPE, "");
827 if (current_encoding == NULL)
828 current_encoding = nl_langinfo (CODESET);
829 #endif
830 if (current_encoding == NULL || *current_encoding == '\0')
831 current_encoding = DEFAULT_ENCODING;
833 /* Initialize the parser */
834 java_init_lex (finput, current_encoding);
835 java_parse_abort_on_error ();
837 java_parse (); /* Parse and build partial tree nodes. */
838 java_parse_abort_on_error ();
841 /* Process a parsed source file, resolving names etc. */
843 static void
844 parse_source_file_2 (void)
846 int save_error_count = java_error_count;
847 java_complete_class (); /* Parse unsatisfied class decl. */
848 java_parse_abort_on_error ();
851 static void
852 parse_source_file_3 (void)
854 int save_error_count = java_error_count;
855 java_check_circular_reference (); /* Check on circular references */
856 java_parse_abort_on_error ();
857 java_fix_constructors (); /* Fix the constructors */
858 java_parse_abort_on_error ();
859 java_reorder_fields (); /* Reorder the fields */
862 void
863 add_predefined_file (tree name)
865 predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
869 predefined_filename_p (tree node)
871 tree iter;
873 for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
875 if (TREE_VALUE (iter) == node)
876 return 1;
878 return 0;
881 void
882 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
884 int filename_count = 0;
885 char *list, *next;
886 tree node;
887 FILE *finput = NULL;
889 if (flag_filelist_file)
891 int avail = 2000;
892 finput = fopen (input_filename, "r");
893 if (finput == NULL)
894 fatal_io_error ("can't open %s", input_filename);
895 list = xmalloc(avail);
896 next = list;
897 for (;;)
899 int count;
900 if (avail < 500)
902 count = next - list;
903 avail = 2 * (count + avail);
904 list = xrealloc (list, avail);
905 next = list + count;
906 avail = avail - count;
908 /* Subtract to to guarantee space for final '\0'. */
909 count = fread (next, 1, avail - 1, finput);
910 if (count == 0)
912 if (! feof (finput))
913 fatal_io_error ("error closing %s", input_filename);
914 *next = '\0';
915 break;
917 avail -= count;
918 next += count;
920 fclose (finput);
921 finput = NULL;
923 else
924 list = xstrdup (input_filename);
928 for (next = list; ; )
930 char ch = *next;
931 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
932 || ch == '&' /* FIXME */)
934 if (next == list)
936 next++;
937 list = next;
938 continue;
940 else
942 *next++ = '\0';
943 break;
946 if (ch == '\0')
948 next = NULL;
949 break;
951 next++;
954 if (list[0])
956 char *value;
957 tree id;
958 int twice = 0;
960 int len = strlen (list);
962 obstack_grow0 (&temporary_obstack, list, len);
963 value = obstack_finish (&temporary_obstack);
965 filename_count++;
967 /* Exclude file that we see twice on the command line. For
968 all files except {Class,Error,Object,RuntimeException,String,
969 Throwable}.java we can rely on maybe_get_identifier. For
970 these files, we need to do a linear search of
971 current_file_list. This search happens only for these
972 files, presumably only when we're recompiling libgcj. */
974 if ((id = maybe_get_identifier (value)))
976 if (predefined_filename_p (id))
978 tree c;
979 for (c = current_file_list; c; c = TREE_CHAIN (c))
980 if (TREE_VALUE (c) == id)
981 twice = 1;
983 else
984 twice = 1;
987 if (twice)
989 const char *saved_input_filename = input_filename;
990 input_filename = value;
991 warning ("source file seen twice on command line and will be compiled only once");
992 input_filename = saved_input_filename;
994 else
996 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
997 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
998 current_file_list = tree_cons (NULL_TREE, node,
999 current_file_list);
1002 list = next;
1004 while (next);
1006 if (filename_count == 0)
1007 warning ("no input file specified");
1009 if (resource_name)
1011 const char *resource_filename;
1013 /* Only one resource file may be compiled at a time. */
1014 assert (TREE_CHAIN (current_file_list) == NULL);
1016 resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
1017 compile_resource_file (resource_name, resource_filename);
1019 return;
1022 current_jcf = main_jcf;
1023 current_file_list = nreverse (current_file_list);
1024 for (node = current_file_list; node; node = TREE_CHAIN (node))
1026 unsigned char magic_string[4];
1027 uint32 magic = 0;
1028 tree name = TREE_VALUE (node);
1030 /* Skip already parsed files */
1031 if (HAS_BEEN_ALREADY_PARSED_P (name))
1032 continue;
1034 /* Close previous descriptor, if any */
1035 if (finput && fclose (finput))
1036 fatal_io_error ("can't close input file %s", main_input_filename);
1038 finput = fopen (IDENTIFIER_POINTER (name), "rb");
1039 if (finput == NULL)
1040 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
1042 #ifdef IO_BUFFER_SIZE
1043 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1044 _IOFBF, IO_BUFFER_SIZE);
1045 #endif
1046 input_filename = IDENTIFIER_POINTER (name);
1048 /* Figure what kind of file we're dealing with */
1049 if (fread (magic_string, 1, 4, finput) == 4)
1051 fseek (finput, 0L, SEEK_SET);
1052 magic = GET_u4 (magic_string);
1054 if (magic == 0xcafebabe)
1056 CLASS_FILE_P (node) = 1;
1057 current_jcf = ggc_alloc (sizeof (JCF));
1058 JCF_ZERO (current_jcf);
1059 current_jcf->read_state = finput;
1060 current_jcf->filbuf = jcf_filbuf_from_stdio;
1061 jcf_parse (current_jcf);
1062 TYPE_JCF (current_class) = current_jcf;
1063 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1064 TREE_PURPOSE (node) = current_class;
1066 else if (magic == (JCF_u4)ZIPMAGIC)
1068 ZIP_FILE_P (node) = 1;
1069 main_jcf = ggc_alloc (sizeof (JCF));
1070 JCF_ZERO (main_jcf);
1071 main_jcf->read_state = finput;
1072 main_jcf->filbuf = jcf_filbuf_from_stdio;
1073 if (open_in_zip (main_jcf, input_filename, NULL, 0) < 0)
1074 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1075 localToFile = SeenZipFiles;
1076 /* Register all the classes defined there. */
1077 process_zip_dir (main_jcf->read_state);
1078 parse_zip_file_entries ();
1080 for (each entry)
1081 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1084 else
1086 JAVA_FILE_P (node) = 1;
1087 java_push_parser_context ();
1088 java_parser_context_save_global ();
1089 parse_source_file_1 (name, finput);
1090 java_parser_context_restore_global ();
1091 java_pop_parser_context (1);
1095 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1097 input_filename = ctxp->filename;
1098 parse_source_file_2 ();
1101 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1103 input_filename = ctxp->filename;
1104 parse_source_file_3 ();
1107 for (node = current_file_list; node; node = TREE_CHAIN (node))
1109 input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1110 if (CLASS_FILE_P (node))
1112 current_class = TREE_PURPOSE (node);
1113 current_jcf = TYPE_JCF (current_class);
1114 layout_class (current_class);
1115 load_inner_classes (current_class);
1116 parse_class_file ();
1117 JCF_FINISH (current_jcf);
1120 input_filename = main_input_filename;
1122 java_expand_classes ();
1123 if (!java_report_errors () && !flag_syntax_only)
1125 emit_register_classes ();
1126 if (flag_indirect_dispatch)
1127 emit_offset_symbol_table ();
1130 write_resource_constructor ();
1133 /* Return the name of the class corresponding to the name of the file
1134 in this zip entry. The result is newly allocated using ALLOC. */
1135 static char *
1136 compute_class_name (struct ZipDirectory *zdir)
1138 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1139 char *class_name;
1140 int j;
1142 class_name = ALLOC (zdir->filename_length + 1 - 6);
1143 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length - 6);
1144 class_name [zdir->filename_length - 6] = '\0';
1145 for (j = 0; class_name[j]; ++j)
1146 class_name[j] = class_name[j] == '/' ? '.' : class_name[j];
1147 return class_name;
1150 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
1151 if it is a property file of some sort. */
1152 static int
1153 classify_zip_file (struct ZipDirectory *zdir)
1155 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1157 if (zdir->filename_length > 6
1158 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
1159 ".class", 6))
1160 return 1;
1162 /* For now we drop the manifest and other information. Maybe it
1163 would make more sense to compile it in? */
1164 if (zdir->filename_length > 8
1165 && !strncmp (class_name_in_zip_dir, "META-INF/", 9))
1166 return 0;
1168 /* Drop directory entries. */
1169 if (zdir->filename_length > 0
1170 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
1171 return 0;
1173 return 2;
1176 /* Process all class entries found in the zip file. */
1177 static void
1178 parse_zip_file_entries (void)
1180 struct ZipDirectory *zdir;
1181 int i;
1183 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1184 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1186 tree class;
1188 switch (classify_zip_file (zdir))
1190 case 0:
1191 continue;
1193 case 1:
1195 char *class_name = compute_class_name (zdir);
1196 class = lookup_class (get_identifier (class_name));
1197 FREE (class_name);
1198 current_jcf = TYPE_JCF (class);
1199 current_class = class;
1201 if (! CLASS_LOADED_P (class))
1203 if (! CLASS_PARSED_P (class))
1205 read_zip_member (current_jcf, zdir, localToFile);
1206 jcf_parse (current_jcf);
1208 layout_class (current_class);
1209 load_inner_classes (current_class);
1212 if (TYPE_SIZE (current_class) != error_mark_node)
1214 input_filename = current_jcf->filename;
1215 parse_class_file ();
1216 FREE (current_jcf->buffer); /* No longer necessary */
1217 /* Note: there is a way to free this buffer right after a
1218 class seen in a zip file has been parsed. The idea is the
1219 set its jcf in such a way that buffer will be reallocated
1220 the time the code for the class will be generated. FIXME. */
1223 break;
1225 case 2:
1227 char *file_name, *class_name_in_zip_dir, *buffer;
1228 JCF *jcf;
1229 file_name = ALLOC (zdir->filename_length + 1);
1230 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1231 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1232 file_name[zdir->filename_length] = '\0';
1233 jcf = ALLOC (sizeof (JCF));
1234 JCF_ZERO (jcf);
1235 jcf->read_state = finput;
1236 jcf->filbuf = jcf_filbuf_from_stdio;
1237 jcf->java_source = 0;
1238 jcf->classname = NULL;
1239 jcf->filename = file_name;
1240 jcf->zipd = zdir;
1242 if (read_zip_member (jcf, zdir, localToFile) < 0)
1243 fatal_error ("error while reading %s from zip file", file_name);
1245 buffer = ALLOC (zdir->filename_length + 1 +
1246 (jcf->buffer_end - jcf->buffer));
1247 strcpy (buffer, file_name);
1248 /* This is not a typo: we overwrite the trailing \0 of the
1249 file name; this is just how the data is laid out. */
1250 memcpy (buffer + zdir->filename_length,
1251 jcf->buffer, jcf->buffer_end - jcf->buffer);
1253 compile_resource_data (file_name, buffer,
1254 jcf->buffer_end - jcf->buffer);
1255 JCF_FINISH (jcf);
1256 FREE (jcf);
1257 FREE (buffer);
1259 break;
1261 default:
1262 abort ();
1267 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1268 jcf up for further processing and link it to the created class. */
1270 static void
1271 process_zip_dir (FILE *finput)
1273 int i;
1274 ZipDirectory *zdir;
1276 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1277 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1279 char *class_name, *file_name, *class_name_in_zip_dir;
1280 tree class;
1281 JCF *jcf;
1283 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1285 /* Here we skip non-class files; we handle them later. */
1286 if (classify_zip_file (zdir) != 1)
1287 continue;
1289 class_name = compute_class_name (zdir);
1290 file_name = ALLOC (zdir->filename_length+1);
1291 jcf = ggc_alloc (sizeof (JCF));
1292 JCF_ZERO (jcf);
1294 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1295 file_name [zdir->filename_length] = '\0';
1297 class = lookup_class (get_identifier (class_name));
1299 jcf->read_state = finput;
1300 jcf->filbuf = jcf_filbuf_from_stdio;
1301 jcf->java_source = 0;
1302 jcf->classname = class_name;
1303 jcf->filename = file_name;
1304 jcf->zipd = zdir;
1306 TYPE_JCF (class) = jcf;
1310 /* Initialization. */
1312 void
1313 init_jcf_parse (void)
1315 init_src_parse ();
1318 #include "gt-java-jcf-parse.h"