* config/xtensa/lib2funcs.S: Fix whitespace.
[official-gcc.git] / gcc / java / jcf-parse.c
blob23939271061b9d2ac88ae3cd35d7da4a4dbd4de2
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 input_line = 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_error ("can't reopen %s: %m", 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_error ("can't close %s: %m", 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 static 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;
705 const char *save_input_filename = input_filename;
706 int save_lineno = input_line;
708 java_layout_seen_class_methods ();
710 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
711 input_line = 0;
712 (*debug_hooks->start_source_file) (input_line, 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 java_mark_class_local (current_class);
721 for (method = TYPE_METHODS (current_class);
722 method != NULL_TREE; method = TREE_CHAIN (method))
724 JCF *jcf = current_jcf;
726 if (METHOD_ABSTRACT (method))
727 continue;
729 if (METHOD_NATIVE (method))
731 tree arg;
732 int decl_max_locals;
734 if (! flag_jni)
735 continue;
736 /* We need to compute the DECL_MAX_LOCALS. We need to take
737 the wide types into account too. */
738 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
739 arg != end_params_node;
740 arg = TREE_CHAIN (arg), decl_max_locals += 1)
742 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
743 decl_max_locals += 1;
745 DECL_MAX_LOCALS (method) = decl_max_locals;
746 start_java_method (method);
747 give_name_to_locals (jcf);
748 expand_expr_stmt (build_jni_stub (method));
749 end_java_method ();
750 continue;
753 if (DECL_CODE_OFFSET (method) == 0)
755 current_function_decl = method;
756 error ("missing Code attribute");
757 continue;
760 input_line = 0;
761 if (DECL_LINENUMBERS_OFFSET (method))
763 register int i;
764 register unsigned char *ptr;
765 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
766 linenumber_count = i = JCF_readu2 (jcf);
767 linenumber_table = ptr = jcf->read_ptr;
769 for (ptr += 2; --i >= 0; ptr += 4)
771 int line = GET_u2 (ptr);
772 /* Set initial lineno lineno to smallest linenumber.
773 * Needs to be set before init_function_start. */
774 if (input_line == 0 || line < input_line)
775 input_line = line;
778 else
780 linenumber_table = NULL;
781 linenumber_count = 0;
784 start_java_method (method);
786 note_instructions (jcf, method);
788 give_name_to_locals (jcf);
790 /* Actually generate code. */
791 expand_byte_code (jcf, method);
793 end_java_method ();
796 if (flag_emit_class_files)
797 write_classfile (current_class);
799 finish_class ();
801 (*debug_hooks->end_source_file) (save_lineno);
802 input_filename = save_input_filename;
803 input_line = save_lineno;
806 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
808 static void
809 parse_source_file_1 (tree file, FILE *finput)
811 int save_error_count = java_error_count;
812 /* Mark the file as parsed */
813 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
815 jcf_dependency_add_file (input_filename, 0);
817 lang_init_source (1); /* Error msgs have no method prototypes */
819 /* There's no point in trying to find the current encoding unless we
820 are going to do something intelligent with it -- hence the test
821 for iconv. */
822 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
823 setlocale (LC_CTYPE, "");
824 if (current_encoding == NULL)
825 current_encoding = nl_langinfo (CODESET);
826 #endif
827 if (current_encoding == NULL || *current_encoding == '\0')
828 current_encoding = DEFAULT_ENCODING;
830 /* Initialize the parser */
831 java_init_lex (finput, current_encoding);
832 java_parse_abort_on_error ();
834 java_parse (); /* Parse and build partial tree nodes. */
835 java_parse_abort_on_error ();
838 /* Process a parsed source file, resolving names etc. */
840 static void
841 parse_source_file_2 (void)
843 int save_error_count = java_error_count;
844 java_complete_class (); /* Parse unsatisfied class decl. */
845 java_parse_abort_on_error ();
848 static void
849 parse_source_file_3 (void)
851 int save_error_count = java_error_count;
852 java_check_circular_reference (); /* Check on circular references */
853 java_parse_abort_on_error ();
854 java_fix_constructors (); /* Fix the constructors */
855 java_parse_abort_on_error ();
856 java_reorder_fields (); /* Reorder the fields */
859 void
860 add_predefined_file (tree name)
862 predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
866 predefined_filename_p (tree node)
868 tree iter;
870 for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
872 if (TREE_VALUE (iter) == node)
873 return 1;
875 return 0;
878 void
879 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
881 int filename_count = 0;
882 char *list, *next;
883 tree node;
884 FILE *finput = NULL;
886 if (flag_filelist_file)
888 int avail = 2000;
889 finput = fopen (input_filename, "r");
890 if (finput == NULL)
891 fatal_error ("can't open %s: %m", input_filename);
892 list = xmalloc(avail);
893 next = list;
894 for (;;)
896 int count;
897 if (avail < 500)
899 count = next - list;
900 avail = 2 * (count + avail);
901 list = xrealloc (list, avail);
902 next = list + count;
903 avail = avail - count;
905 /* Subtract to to guarantee space for final '\0'. */
906 count = fread (next, 1, avail - 1, finput);
907 if (count == 0)
909 if (! feof (finput))
910 fatal_error ("error closing %s: %m", input_filename);
911 *next = '\0';
912 break;
914 avail -= count;
915 next += count;
917 fclose (finput);
918 finput = NULL;
920 else
921 list = xstrdup (input_filename);
925 for (next = list; ; )
927 char ch = *next;
928 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
929 || ch == '&' /* FIXME */)
931 if (next == list)
933 next++;
934 list = next;
935 continue;
937 else
939 *next++ = '\0';
940 break;
943 if (ch == '\0')
945 next = NULL;
946 break;
948 next++;
951 if (list[0])
953 char *value;
954 tree id;
955 int twice = 0;
957 int len = strlen (list);
959 obstack_grow0 (&temporary_obstack, list, len);
960 value = obstack_finish (&temporary_obstack);
962 filename_count++;
964 /* Exclude file that we see twice on the command line. For
965 all files except {Class,Error,Object,RuntimeException,String,
966 Throwable}.java we can rely on maybe_get_identifier. For
967 these files, we need to do a linear search of
968 current_file_list. This search happens only for these
969 files, presumably only when we're recompiling libgcj. */
971 if ((id = maybe_get_identifier (value)))
973 if (predefined_filename_p (id))
975 tree c;
976 for (c = current_file_list; c; c = TREE_CHAIN (c))
977 if (TREE_VALUE (c) == id)
978 twice = 1;
980 else
981 twice = 1;
984 if (twice)
986 const char *saved_input_filename = input_filename;
987 input_filename = value;
988 warning ("source file seen twice on command line and will be compiled only once");
989 input_filename = saved_input_filename;
991 else
993 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
994 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
995 current_file_list = tree_cons (NULL_TREE, node,
996 current_file_list);
999 list = next;
1001 while (next);
1003 if (filename_count == 0)
1004 warning ("no input file specified");
1006 if (resource_name)
1008 const char *resource_filename;
1010 /* Only one resource file may be compiled at a time. */
1011 assert (TREE_CHAIN (current_file_list) == NULL);
1013 resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
1014 compile_resource_file (resource_name, resource_filename);
1016 return;
1019 current_jcf = main_jcf;
1020 current_file_list = nreverse (current_file_list);
1021 for (node = current_file_list; node; node = TREE_CHAIN (node))
1023 unsigned char magic_string[4];
1024 uint32 magic = 0;
1025 tree name = TREE_VALUE (node);
1027 /* Skip already parsed files */
1028 if (HAS_BEEN_ALREADY_PARSED_P (name))
1029 continue;
1031 /* Close previous descriptor, if any */
1032 if (finput && fclose (finput))
1033 fatal_error ("can't close input file %s: %m", main_input_filename);
1035 finput = fopen (IDENTIFIER_POINTER (name), "rb");
1036 if (finput == NULL)
1037 fatal_error ("can't open %s: %m", IDENTIFIER_POINTER (name));
1039 #ifdef IO_BUFFER_SIZE
1040 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1041 _IOFBF, IO_BUFFER_SIZE);
1042 #endif
1043 input_filename = IDENTIFIER_POINTER (name);
1045 /* Figure what kind of file we're dealing with */
1046 if (fread (magic_string, 1, 4, finput) == 4)
1048 fseek (finput, 0L, SEEK_SET);
1049 magic = GET_u4 (magic_string);
1051 if (magic == 0xcafebabe)
1053 CLASS_FILE_P (node) = 1;
1054 current_jcf = ggc_alloc (sizeof (JCF));
1055 JCF_ZERO (current_jcf);
1056 current_jcf->read_state = finput;
1057 current_jcf->filbuf = jcf_filbuf_from_stdio;
1058 jcf_parse (current_jcf);
1059 TYPE_JCF (current_class) = current_jcf;
1060 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1061 TREE_PURPOSE (node) = current_class;
1063 else if (magic == (JCF_u4)ZIPMAGIC)
1065 ZIP_FILE_P (node) = 1;
1066 main_jcf = ggc_alloc (sizeof (JCF));
1067 JCF_ZERO (main_jcf);
1068 main_jcf->read_state = finput;
1069 main_jcf->filbuf = jcf_filbuf_from_stdio;
1070 if (open_in_zip (main_jcf, input_filename, NULL, 0) < 0)
1071 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1072 localToFile = SeenZipFiles;
1073 /* Register all the classes defined there. */
1074 process_zip_dir (main_jcf->read_state);
1075 parse_zip_file_entries ();
1077 for (each entry)
1078 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1081 else
1083 JAVA_FILE_P (node) = 1;
1084 java_push_parser_context ();
1085 java_parser_context_save_global ();
1086 parse_source_file_1 (name, finput);
1087 java_parser_context_restore_global ();
1088 java_pop_parser_context (1);
1092 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1094 input_filename = ctxp->filename;
1095 parse_source_file_2 ();
1098 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1100 input_filename = ctxp->filename;
1101 parse_source_file_3 ();
1104 for (node = current_file_list; node; node = TREE_CHAIN (node))
1106 input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1107 if (CLASS_FILE_P (node))
1109 current_class = TREE_PURPOSE (node);
1110 current_jcf = TYPE_JCF (current_class);
1111 layout_class (current_class);
1112 load_inner_classes (current_class);
1113 parse_class_file ();
1114 JCF_FINISH (current_jcf);
1117 input_filename = main_input_filename;
1119 java_expand_classes ();
1120 if (!java_report_errors () && !flag_syntax_only)
1122 emit_register_classes ();
1123 if (flag_indirect_dispatch)
1124 emit_offset_symbol_table ();
1127 write_resource_constructor ();
1130 /* Return the name of the class corresponding to the name of the file
1131 in this zip entry. The result is newly allocated using ALLOC. */
1132 static char *
1133 compute_class_name (struct ZipDirectory *zdir)
1135 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1136 char *class_name;
1137 int j;
1139 class_name = ALLOC (zdir->filename_length + 1 - 6);
1140 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length - 6);
1141 class_name [zdir->filename_length - 6] = '\0';
1142 for (j = 0; class_name[j]; ++j)
1143 class_name[j] = class_name[j] == '/' ? '.' : class_name[j];
1144 return class_name;
1147 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
1148 if it is a property file of some sort. */
1149 static int
1150 classify_zip_file (struct ZipDirectory *zdir)
1152 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1154 if (zdir->filename_length > 6
1155 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
1156 ".class", 6))
1157 return 1;
1159 /* For now we drop the manifest and other information. Maybe it
1160 would make more sense to compile it in? */
1161 if (zdir->filename_length > 8
1162 && !strncmp (class_name_in_zip_dir, "META-INF/", 9))
1163 return 0;
1165 /* Drop directory entries. */
1166 if (zdir->filename_length > 0
1167 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
1168 return 0;
1170 return 2;
1173 /* Process all class entries found in the zip file. */
1174 static void
1175 parse_zip_file_entries (void)
1177 struct ZipDirectory *zdir;
1178 int i;
1180 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1181 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1183 tree class;
1185 switch (classify_zip_file (zdir))
1187 case 0:
1188 continue;
1190 case 1:
1192 char *class_name = compute_class_name (zdir);
1193 class = lookup_class (get_identifier (class_name));
1194 FREE (class_name);
1195 current_jcf = TYPE_JCF (class);
1196 current_class = class;
1198 if (! CLASS_LOADED_P (class))
1200 if (! CLASS_PARSED_P (class))
1202 read_zip_member (current_jcf, zdir, localToFile);
1203 jcf_parse (current_jcf);
1205 layout_class (current_class);
1206 load_inner_classes (current_class);
1209 if (TYPE_SIZE (current_class) != error_mark_node)
1211 input_filename = current_jcf->filename;
1212 parse_class_file ();
1213 FREE (current_jcf->buffer); /* No longer necessary */
1214 /* Note: there is a way to free this buffer right after a
1215 class seen in a zip file has been parsed. The idea is the
1216 set its jcf in such a way that buffer will be reallocated
1217 the time the code for the class will be generated. FIXME. */
1220 break;
1222 case 2:
1224 char *file_name, *class_name_in_zip_dir, *buffer;
1225 JCF *jcf;
1226 file_name = ALLOC (zdir->filename_length + 1);
1227 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1228 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1229 file_name[zdir->filename_length] = '\0';
1230 jcf = ALLOC (sizeof (JCF));
1231 JCF_ZERO (jcf);
1232 jcf->read_state = finput;
1233 jcf->filbuf = jcf_filbuf_from_stdio;
1234 jcf->java_source = 0;
1235 jcf->classname = NULL;
1236 jcf->filename = file_name;
1237 jcf->zipd = zdir;
1239 if (read_zip_member (jcf, zdir, localToFile) < 0)
1240 fatal_error ("error while reading %s from zip file", file_name);
1242 buffer = ALLOC (zdir->filename_length + 1 +
1243 (jcf->buffer_end - jcf->buffer));
1244 strcpy (buffer, file_name);
1245 /* This is not a typo: we overwrite the trailing \0 of the
1246 file name; this is just how the data is laid out. */
1247 memcpy (buffer + zdir->filename_length,
1248 jcf->buffer, jcf->buffer_end - jcf->buffer);
1250 compile_resource_data (file_name, buffer,
1251 jcf->buffer_end - jcf->buffer);
1252 JCF_FINISH (jcf);
1253 FREE (jcf);
1254 FREE (buffer);
1256 break;
1258 default:
1259 abort ();
1264 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1265 jcf up for further processing and link it to the created class. */
1267 static void
1268 process_zip_dir (FILE *finput)
1270 int i;
1271 ZipDirectory *zdir;
1273 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1274 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1276 char *class_name, *file_name, *class_name_in_zip_dir;
1277 tree class;
1278 JCF *jcf;
1280 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1282 /* Here we skip non-class files; we handle them later. */
1283 if (classify_zip_file (zdir) != 1)
1284 continue;
1286 class_name = compute_class_name (zdir);
1287 file_name = ALLOC (zdir->filename_length+1);
1288 jcf = ggc_alloc (sizeof (JCF));
1289 JCF_ZERO (jcf);
1291 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1292 file_name [zdir->filename_length] = '\0';
1294 class = lookup_class (get_identifier (class_name));
1296 jcf->read_state = finput;
1297 jcf->filbuf = jcf_filbuf_from_stdio;
1298 jcf->java_source = 0;
1299 jcf->classname = class_name;
1300 jcf->filename = file_name;
1301 jcf->zipd = zdir;
1303 TYPE_JCF (class) = jcf;
1307 /* Initialization. */
1309 void
1310 init_jcf_parse (void)
1312 init_src_parse ();
1315 #include "gt-java-jcf-parse.h"