2002-04-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / java / jcf-parse.c
blob53e647cb2c6313114a5e652a439f990752ce9b7b
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC 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 GNU CC 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 GNU CC; 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 "tree.h"
31 #include "obstack.h"
32 #include "flags.h"
33 #include "java-except.h"
34 #include "input.h"
35 #include "java-tree.h"
36 #include "toplev.h"
37 #include "parse.h"
38 #include "ggc.h"
39 #include "debug.h"
40 #include "assert.h"
42 #ifdef HAVE_LOCALE_H
43 #include <locale.h>
44 #endif
46 #ifdef HAVE_NL_LANGINFO
47 #include <langinfo.h>
48 #endif
50 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
51 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
52 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
53 #define JPOOL_UTF_DATA(JCF, INDEX) \
54 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
55 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
56 do { \
57 unsigned char save; unsigned char *text; \
58 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
59 text = (JCF)->read_ptr; \
60 save = text[LENGTH]; \
61 text[LENGTH] = 0; \
62 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
63 text[LENGTH] = save; \
64 JCF_SKIP (JCF, LENGTH); } while (0)
66 #include "jcf.h"
68 extern struct obstack *saveable_obstack;
69 extern struct obstack temporary_obstack;
70 extern struct obstack permanent_obstack;
72 /* Set to non-zero value in order to emit class initilization code
73 before static field references. */
74 extern int always_initialize_class_p;
76 static tree parse_roots[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
78 /* The FIELD_DECL for the current field. */
79 #define current_field parse_roots[0]
81 /* The METHOD_DECL for the current method. */
82 #define current_method parse_roots[1]
84 /* A list of file names. */
85 #define current_file_list parse_roots[2]
87 /* The Java archive that provides main_class; the main input file. */
88 static struct JCF main_jcf[1];
90 static struct ZipFile *localToFile;
92 /* Declarations of some functions used here. */
93 static void handle_innerclass_attribute PARAMS ((int count, JCF *));
94 static tree give_name_to_class PARAMS ((JCF *jcf, int index));
95 static void parse_zip_file_entries PARAMS ((void));
96 static void process_zip_dir PARAMS ((FILE *));
97 static void parse_source_file_1 PARAMS ((tree, FILE *));
98 static void parse_source_file_2 PARAMS ((void));
99 static void parse_source_file_3 PARAMS ((void));
100 static void parse_class_file PARAMS ((void));
101 static void set_source_filename PARAMS ((JCF *, int));
102 static void ggc_mark_jcf PARAMS ((void**));
103 static void jcf_parse PARAMS ((struct JCF*));
104 static void load_inner_classes PARAMS ((tree));
106 /* Mark (for garbage collection) all the tree nodes that are
107 referenced from JCF's constant pool table. Do that only if the JCF
108 hasn't been marked finished. */
110 static void
111 ggc_mark_jcf (elt)
112 void **elt;
114 JCF *jcf = *(JCF**) elt;
115 if (jcf != NULL && !jcf->finished)
117 CPool *cpool = &jcf->cpool;
118 int size = CPOOL_COUNT(cpool);
119 int index;
120 for (index = 1; index < size; index++)
122 int tag = JPOOL_TAG (jcf, index);
123 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
124 ggc_mark_tree ((tree) cpool->data[index]);
129 /* Handle "SourceFile" attribute. */
131 static void
132 set_source_filename (jcf, index)
133 JCF *jcf;
134 int index;
136 tree sfname_id = get_name_constant (jcf, index);
137 const char *sfname = IDENTIFIER_POINTER (sfname_id);
138 if (input_filename != NULL)
140 int old_len = strlen (input_filename);
141 int new_len = IDENTIFIER_LENGTH (sfname_id);
142 /* Use the current input_filename (derived from the class name)
143 if it has a directory prefix, but otherwise matches sfname. */
144 if (old_len > new_len
145 && strcmp (sfname, input_filename + old_len - new_len) == 0
146 && (input_filename[old_len - new_len - 1] == '/'
147 || input_filename[old_len - new_len - 1] == '\\'))
148 return;
150 input_filename = sfname;
151 DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
152 if (current_class == main_class) main_input_filename = input_filename;
155 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
157 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
158 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
159 current_class = give_name_to_class (jcf, THIS); \
160 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
162 #define HANDLE_CLASS_INTERFACE(INDEX) \
163 add_interface (current_class, get_class_constant (jcf, INDEX))
165 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
166 { int sig_index = SIGNATURE; \
167 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
168 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
169 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
170 if ((ACCESS_FLAGS) & ACC_FINAL) \
171 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
174 #define HANDLE_END_FIELDS() \
175 (current_field = NULL_TREE)
177 #define HANDLE_CONSTANTVALUE(INDEX) \
178 { tree constant; int index = INDEX; \
179 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
180 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
181 constant = build_utf8_ref (name); \
183 else \
184 constant = get_constant (jcf, index); \
185 set_constant_value (current_field, constant); }
187 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
188 (current_method = add_method (current_class, ACCESS_FLAGS, \
189 get_name_constant (jcf, NAME), \
190 get_name_constant (jcf, SIGNATURE)), \
191 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
192 DECL_LINENUMBERS_OFFSET (current_method) = 0)
194 #define HANDLE_END_METHODS() \
195 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
196 if (handle_type != current_class) layout_type (handle_type); \
197 current_method = NULL_TREE; }
199 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
200 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
201 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
202 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
203 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
205 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
206 { int n = (COUNT); \
207 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
208 JCF_SKIP (jcf, n * 10); }
210 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
211 { int n = (COUNT); \
212 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
213 JCF_SKIP (jcf, n * 4); }
215 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
217 int n = COUNT; \
218 tree list = DECL_FUNCTION_THROWS (current_method); \
219 while (--n >= 0) \
221 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
222 list = tree_cons (NULL_TREE, thrown_class, list); \
224 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
227 /* Link seen inner classes to their outer context and register the
228 inner class to its outer context. They will be later loaded. */
229 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
230 handle_innerclass_attribute (COUNT, jcf)
232 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
234 /* Irrelevant decls should have been nullified by the END macros. \
235 We only handle the `Synthetic' attribute on method DECLs. \
236 DECL_ARTIFICIAL on fields is used for something else (See \
237 PUSH_FIELD in java-tree.h) */ \
238 if (current_method) \
239 DECL_ARTIFICIAL (current_method) = 1; \
242 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
244 if (current_class == object_type_node) \
245 jcf->right_zip = 1; \
248 #include "jcf-reader.c"
250 static int yydebug;
252 tree
253 parse_signature (jcf, sig_index)
254 JCF *jcf;
255 int sig_index;
257 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
258 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
259 abort ();
260 else
261 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
262 JPOOL_UTF_LENGTH (jcf, sig_index));
265 void
266 java_set_yydebug (value)
267 int value;
269 yydebug = value;
272 tree
273 get_constant (jcf, index)
274 JCF *jcf;
275 int index;
277 tree value;
278 int tag;
279 if (index <= 0 || index >= JPOOL_SIZE(jcf))
280 goto bad;
281 tag = JPOOL_TAG (jcf, index);
282 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
283 return (tree) jcf->cpool.data[index];
284 switch (tag)
286 case CONSTANT_Integer:
288 jint num = JPOOL_INT(jcf, index);
289 value = build_int_2 (num, num < 0 ? -1 : 0);
290 TREE_TYPE (value) = int_type_node;
291 break;
293 case CONSTANT_Long:
295 jint num = JPOOL_INT (jcf, index);
296 HOST_WIDE_INT lo, hi;
297 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
298 num = JPOOL_INT (jcf, index+1) & 0xffffffff;
299 add_double (lo, hi, num, 0, &lo, &hi);
300 value = build_int_2 (lo, hi);
301 TREE_TYPE (value) = long_type_node;
302 force_fit_type (value, 0);
303 break;
305 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
306 case CONSTANT_Float:
308 jint num = JPOOL_INT(jcf, index);
309 REAL_VALUE_TYPE d;
310 d = REAL_VALUE_FROM_TARGET_SINGLE (num);
311 value = build_real (float_type_node, d);
312 break;
314 case CONSTANT_Double:
316 HOST_WIDE_INT num[2];
317 REAL_VALUE_TYPE d;
318 HOST_WIDE_INT lo, hi;
319 num[0] = JPOOL_INT (jcf, index);
320 lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
321 num[0] = JPOOL_INT (jcf, index+1);
322 add_double (lo, hi, num[0], 0, &lo, &hi);
324 /* Since ereal_from_double expects an array of HOST_WIDE_INT
325 in the target's format, we swap the elements for big endian
326 targets, unless HOST_WIDE_INT is sufficiently large to
327 contain a target double, in which case the 2nd element
328 is ignored.
330 FIXME: Is this always right for cross targets? */
331 if (FLOAT_WORDS_BIG_ENDIAN && sizeof(num[0]) < 8)
333 num[0] = hi;
334 num[1] = lo;
336 else
338 num[0] = lo;
339 num[1] = hi;
341 d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
342 value = build_real (double_type_node, d);
343 break;
345 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
346 case CONSTANT_String:
348 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
349 const char *utf8_ptr = IDENTIFIER_POINTER (name);
350 int utf8_len = IDENTIFIER_LENGTH (name);
351 const unsigned char *utf8;
352 int i;
354 /* Check for a malformed Utf8 string. */
355 utf8 = (const unsigned char *) utf8_ptr;
356 i = utf8_len;
357 while (i > 0)
359 int char_len = UT8_CHAR_LENGTH (*utf8);
360 if (char_len < 0 || char_len > 3 || char_len > i)
361 fatal_error ("bad string constant");
363 utf8 += char_len;
364 i -= char_len;
367 /* Allocate a new string value. */
368 value = build_string (utf8_len, utf8_ptr);
369 TREE_TYPE (value) = build_pointer_type (string_type_node);
371 break;
372 default:
373 goto bad;
375 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
376 jcf->cpool.data [index] = (jword) value;
377 return value;
378 bad:
379 internal_error ("bad value constant type %d, index %d",
380 JPOOL_TAG (jcf, index), index);
383 tree
384 get_name_constant (jcf, index)
385 JCF *jcf;
386 int index;
388 tree name = get_constant (jcf, index);
390 if (TREE_CODE (name) != IDENTIFIER_NODE)
391 abort ();
393 return name;
396 /* Handle reading innerclass attributes. If a non zero entry (denoting
397 a non anonymous entry) is found, We augment the inner class list of
398 the outer context with the newly resolved innerclass. */
400 static void
401 handle_innerclass_attribute (count, jcf)
402 int count;
403 JCF *jcf;
405 int c = (count);
406 while (c--)
408 /* Read inner_class_info_index. This may be 0 */
409 int icii = JCF_readu2 (jcf);
410 /* Read outer_class_info_index. If the innerclasses attribute
411 entry isn't a member (like an inner class) the value is 0. */
412 int ocii = JCF_readu2 (jcf);
413 /* Read inner_name_index. If the class we're dealing with is
414 an annonymous class, it must be 0. */
415 int ini = JCF_readu2 (jcf);
416 /* Read the access flag. */
417 int acc = JCF_readu2 (jcf);
418 /* If icii is 0, don't try to read the class. */
419 if (icii >= 0)
421 tree class = get_class_constant (jcf, icii);
422 tree decl = TYPE_NAME (class);
423 /* Skip reading further if ocii is null */
424 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
426 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
427 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
428 set_class_decl_access_flags (acc, decl);
429 DECL_CONTEXT (decl) = outer;
430 DECL_INNER_CLASS_LIST (outer) =
431 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
432 CLASS_COMPLETE_P (decl) = 1;
438 static tree
439 give_name_to_class (jcf, i)
440 JCF *jcf;
441 int i;
443 if (i <= 0 || i >= JPOOL_SIZE (jcf)
444 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
445 abort ();
446 else
448 tree this_class;
449 int j = JPOOL_USHORT1 (jcf, i);
450 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
451 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
452 JPOOL_UTF_LENGTH (jcf, j));
453 this_class = lookup_class (class_name);
454 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
455 lineno = 0;
456 if (main_input_filename == NULL && jcf == main_jcf)
457 main_input_filename = input_filename;
459 jcf->cpool.data[i] = (jword) this_class;
460 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
461 return this_class;
465 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
467 tree
468 get_class_constant (JCF *jcf , int i)
470 tree type;
471 if (i <= 0 || i >= JPOOL_SIZE (jcf)
472 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
473 abort ();
475 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
477 int name_index = JPOOL_USHORT1 (jcf, i);
478 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
479 const char *name = JPOOL_UTF_DATA (jcf, name_index);
480 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
482 if (name[0] == '[') /* Handle array "classes". */
483 type = TREE_TYPE (parse_signature_string (name, nlength));
484 else
486 tree cname = unmangle_classname (name, nlength);
487 type = lookup_class (cname);
489 jcf->cpool.data[i] = (jword) type;
490 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
492 else
493 type = (tree) jcf->cpool.data[i];
494 return type;
497 /* Read a class with the fully qualified-name NAME.
498 Return 1 iff we read the requested file.
499 (It is still possible we failed if the file did not
500 define the class it is supposed to.) */
503 read_class (name)
504 tree name;
506 JCF this_jcf, *jcf;
507 tree icv, class = NULL_TREE;
508 tree save_current_class = current_class;
509 const char *save_input_filename = input_filename;
510 JCF *save_current_jcf = current_jcf;
512 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
514 class = TREE_TYPE (icv);
515 jcf = TYPE_JCF (class);
517 else
518 jcf = NULL;
520 if (jcf == NULL)
522 this_jcf.zipd = NULL;
523 jcf = &this_jcf;
524 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
525 &this_jcf, 1) == 0)
526 return 0;
529 current_jcf = jcf;
531 if (current_jcf->java_source)
533 const char *filename = current_jcf->filename;
534 tree file;
535 FILE *finput;
536 int generate;
538 java_parser_context_save_global ();
539 java_push_parser_context ();
540 BUILD_FILENAME_IDENTIFIER_NODE (file, filename);
541 generate = IS_A_COMMAND_LINE_FILENAME_P (file);
542 if (wfl_operator == NULL_TREE)
543 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
544 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
545 input_filename = ggc_strdup (filename);
546 current_class = NULL_TREE;
547 current_function_decl = NULL_TREE;
548 if (!HAS_BEEN_ALREADY_PARSED_P (file))
550 if (!(finput = fopen (input_filename, "r")))
551 fatal_io_error ("can't reopen %s", input_filename);
552 parse_source_file_1 (file, finput);
553 parse_source_file_2 ();
554 parse_source_file_3 ();
555 if (fclose (finput))
556 fatal_io_error ("can't close %s", input_filename);
558 JCF_FINISH (current_jcf);
559 java_pop_parser_context (generate);
560 java_parser_context_restore_global ();
562 else
564 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
566 java_parser_context_save_global ();
567 java_push_parser_context ();
568 current_class = class;
569 input_filename = current_jcf->filename;
570 if (JCF_SEEN_IN_ZIP (current_jcf))
571 read_zip_member(current_jcf,
572 current_jcf->zipd, current_jcf->zipd->zipf);
573 jcf_parse (current_jcf);
574 class = current_class;
575 java_pop_parser_context (0);
576 java_parser_context_restore_global ();
578 layout_class (class);
579 load_inner_classes (class);
582 current_class = save_current_class;
583 input_filename = save_input_filename;
584 current_jcf = save_current_jcf;
585 return 1;
588 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
589 called from the parser, otherwise it's a RECORD_TYPE node. If
590 VERBOSE is 1, print error message on failure to load a class. */
592 /* Replace calls to load_class by having callers call read_class directly
593 - and then perhaps rename read_class to load_class. FIXME */
595 void
596 load_class (class_or_name, verbose)
597 tree class_or_name;
598 int verbose;
600 tree name, saved;
601 int class_loaded;
603 /* class_or_name can be the name of the class we want to load */
604 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
605 name = class_or_name;
606 /* In some cases, it's a dependency that we process earlier that
607 we though */
608 else if (TREE_CODE (class_or_name) == TREE_LIST)
609 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
610 /* Or it's a type in the making */
611 else
612 name = DECL_NAME (TYPE_NAME (class_or_name));
614 saved = name;
615 while (1)
617 char *separator;
619 if ((class_loaded = read_class (name)))
620 break;
622 /* We failed loading name. Now consider that we might be looking
623 for a inner class. */
624 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
625 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
627 int c = *separator;
628 *separator = '\0';
629 name = get_identifier (IDENTIFIER_POINTER (name));
630 *separator = c;
632 /* Otherwise, we failed, we bail. */
633 else
634 break;
637 if (!class_loaded && verbose)
638 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
641 /* Parse the .class file JCF. */
643 void
644 jcf_parse (jcf)
645 JCF* jcf;
647 int i, code;
649 if (jcf_parse_preamble (jcf) != 0)
650 fatal_error ("not a valid Java .class file");
651 code = jcf_parse_constant_pool (jcf);
652 if (code != 0)
653 fatal_error ("error while parsing constant pool");
654 code = verify_constant_pool (jcf);
655 if (code > 0)
656 fatal_error ("error in constant pool entry #%d\n", code);
658 jcf_parse_class (jcf);
659 if (main_class == NULL_TREE)
660 main_class = current_class;
661 if (! quiet_flag && TYPE_NAME (current_class))
662 fprintf (stderr, " %s %s",
663 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
664 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
665 if (CLASS_PARSED_P (current_class))
667 /* FIXME - where was first time */
668 fatal_error ("reading class %s for the second time from %s",
669 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
670 jcf->filename);
672 CLASS_PARSED_P (current_class) = 1;
674 for (i = 1; i < JPOOL_SIZE(jcf); i++)
676 switch (JPOOL_TAG (jcf, i))
678 case CONSTANT_Class:
679 get_class_constant (jcf, i);
680 break;
684 code = jcf_parse_fields (jcf);
685 if (code != 0)
686 fatal_error ("error while parsing fields");
687 code = jcf_parse_methods (jcf);
688 if (code != 0)
689 fatal_error ("error while parsing methods");
690 code = jcf_parse_final_attributes (jcf);
691 if (code != 0)
692 fatal_error ("error while parsing final attributes");
694 /* The fields of class_type_node are already in correct order. */
695 if (current_class != class_type_node && current_class != object_type_node)
696 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
698 if (current_class == object_type_node)
700 layout_class_methods (object_type_node);
701 /* If we don't have the right archive, emit a verbose warning.
702 If we're generating bytecode, emit the warning only if
703 -fforce-classes-archive-check was specified. */
704 if (!jcf->right_zip
705 && (!flag_emit_class_files || flag_force_classes_archive_check))
706 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);
708 else
709 all_class_list = tree_cons (NULL_TREE,
710 TYPE_NAME (current_class), all_class_list );
713 /* If we came across inner classes, load them now. */
714 static void
715 load_inner_classes (cur_class)
716 tree cur_class;
718 tree current;
719 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
720 current = TREE_CHAIN (current))
722 tree name = DECL_NAME (TREE_PURPOSE (current));
723 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
724 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
725 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
726 load_class (name, 1);
730 void
731 init_outgoing_cpool ()
733 current_constant_pool_data_ref = NULL_TREE;
734 outgoing_cpool = (struct CPool *)xmalloc (sizeof (struct CPool));
735 memset (outgoing_cpool, 0, sizeof (struct CPool));
738 static void
739 parse_class_file ()
741 tree method, field;
742 const char *save_input_filename = input_filename;
743 int save_lineno = lineno;
745 java_layout_seen_class_methods ();
747 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
748 lineno = 0;
749 (*debug_hooks->start_source_file) (lineno, input_filename);
750 init_outgoing_cpool ();
752 /* Currently we always have to emit calls to _Jv_InitClass when
753 compiling from class files. */
754 always_initialize_class_p = 1;
756 for (field = TYPE_FIELDS (CLASS_TO_HANDLE_TYPE (current_class));
757 field != NULL_TREE; field = TREE_CHAIN (field))
758 if (FIELD_STATIC (field))
759 DECL_EXTERNAL (field) = 0;
761 for (method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
762 method != NULL_TREE; method = TREE_CHAIN (method))
764 JCF *jcf = current_jcf;
766 if (METHOD_ABSTRACT (method))
767 continue;
769 if (METHOD_NATIVE (method))
771 tree arg;
772 int decl_max_locals;
774 if (! flag_jni)
775 continue;
776 /* We need to compute the DECL_MAX_LOCALS. We need to take
777 the wide types into account too. */
778 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
779 arg != end_params_node;
780 arg = TREE_CHAIN (arg), decl_max_locals += 1)
782 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
783 decl_max_locals += 1;
785 DECL_MAX_LOCALS (method) = decl_max_locals;
786 start_java_method (method);
787 give_name_to_locals (jcf);
788 expand_expr_stmt (build_jni_stub (method));
789 end_java_method ();
790 continue;
793 if (DECL_CODE_OFFSET (method) == 0)
795 current_function_decl = method;
796 error ("missing Code attribute");
797 continue;
800 lineno = 0;
801 if (DECL_LINENUMBERS_OFFSET (method))
803 register int i;
804 register unsigned char *ptr;
805 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
806 linenumber_count = i = JCF_readu2 (jcf);
807 linenumber_table = ptr = jcf->read_ptr;
809 for (ptr += 2; --i >= 0; ptr += 4)
811 int line = GET_u2 (ptr);
812 /* Set initial lineno lineno to smallest linenumber.
813 * Needs to be set before init_function_start. */
814 if (lineno == 0 || line < lineno)
815 lineno = line;
818 else
820 linenumber_table = NULL;
821 linenumber_count = 0;
824 start_java_method (method);
826 note_instructions (jcf, method);
828 give_name_to_locals (jcf);
830 /* Actually generate code. */
831 expand_byte_code (jcf, method);
833 end_java_method ();
836 if (flag_emit_class_files)
837 write_classfile (current_class);
839 finish_class ();
841 (*debug_hooks->end_source_file) (save_lineno);
842 input_filename = save_input_filename;
843 lineno = save_lineno;
846 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
848 static void
849 parse_source_file_1 (file, finput)
850 tree file;
851 FILE *finput;
853 int save_error_count = java_error_count;
854 /* Mark the file as parsed */
855 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
857 jcf_dependency_add_file (input_filename, 0);
859 lang_init_source (1); /* Error msgs have no method prototypes */
861 /* There's no point in trying to find the current encoding unless we
862 are going to do something intelligent with it -- hence the test
863 for iconv. */
864 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_NL_LANGINFO)
865 setlocale (LC_CTYPE, "");
866 if (current_encoding == NULL)
867 current_encoding = nl_langinfo (CODESET);
868 #endif
869 if (current_encoding == NULL || *current_encoding == '\0')
870 current_encoding = DEFAULT_ENCODING;
872 /* Initialize the parser */
873 java_init_lex (finput, current_encoding);
874 java_parse_abort_on_error ();
876 java_parse (); /* Parse and build partial tree nodes. */
877 java_parse_abort_on_error ();
880 /* Process a parsed source file, resolving names etc. */
882 static void
883 parse_source_file_2 ()
885 int save_error_count = java_error_count;
886 java_complete_class (); /* Parse unsatisfied class decl. */
887 java_parse_abort_on_error ();
890 static void
891 parse_source_file_3 ()
893 int save_error_count = java_error_count;
894 java_check_circular_reference (); /* Check on circular references */
895 java_parse_abort_on_error ();
896 java_fix_constructors (); /* Fix the constructors */
897 java_parse_abort_on_error ();
898 java_reorder_fields (); /* Reorder the fields */
901 void
902 add_predefined_file (name)
903 tree name;
905 predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
909 predefined_filename_p (node)
910 tree node;
912 tree iter;
914 for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
916 if (TREE_VALUE (iter) == node)
917 return 1;
919 return 0;
922 void
923 java_parse_file ()
925 int filename_count = 0;
926 char *list, *next;
927 tree node;
928 FILE *finput = NULL;
930 if (flag_filelist_file)
932 int avail = 2000;
933 finput = fopen (input_filename, "r");
934 if (finput == NULL)
935 fatal_io_error ("can't open %s", input_filename);
936 list = xmalloc(avail);
937 next = list;
938 for (;;)
940 int count;
941 if (avail < 500)
943 count = next - list;
944 avail = 2 * (count + avail);
945 list = xrealloc (list, avail);
946 next = list + count;
947 avail = avail - count;
949 /* Subtract to to guarantee space for final '\0'. */
950 count = fread (next, 1, avail - 1, finput);
951 if (count == 0)
953 if (! feof (finput))
954 fatal_io_error ("error closing %s", input_filename);
955 *next = '\0';
956 break;
958 avail -= count;
959 next += count;
961 fclose (finput);
962 finput = NULL;
964 else
965 list = xstrdup (input_filename);
969 for (next = list; ; )
971 char ch = *next;
972 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
973 || ch == '&' /* FIXME */)
975 if (next == list)
977 next++;
978 list = next;
979 continue;
981 else
983 *next++ = '\0';
984 break;
987 if (ch == '\0')
989 next = NULL;
990 break;
992 next++;
995 if (list[0])
997 char *value;
998 tree id;
999 int twice = 0;
1001 int len = strlen (list);
1003 if (*list != '/' && filename_count > 0)
1004 obstack_grow (&temporary_obstack, "./", 2);
1006 obstack_grow0 (&temporary_obstack, list, len);
1007 value = obstack_finish (&temporary_obstack);
1009 filename_count++;
1011 /* Exclude file that we see twice on the command line. For
1012 all files except {Class,Error,Object,RuntimeException,String,
1013 Throwable}.java we can rely on maybe_get_identifier. For
1014 these files, we need to do a linear search of
1015 current_file_list. This search happens only for these
1016 files, presumably only when we're recompiling libgcj. */
1018 if ((id = maybe_get_identifier (value)))
1020 if (predefined_filename_p (id))
1022 tree c;
1023 for (c = current_file_list; c; c = TREE_CHAIN (c))
1024 if (TREE_VALUE (c) == id)
1025 twice = 1;
1027 else
1028 twice = 1;
1031 if (twice)
1033 const char *saved_input_filename = input_filename;
1034 input_filename = value;
1035 warning ("source file seen twice on command line and will be compiled only once");
1036 input_filename = saved_input_filename;
1038 else
1040 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
1041 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1042 current_file_list = tree_cons (NULL_TREE, node,
1043 current_file_list);
1046 list = next;
1048 while (next);
1050 if (filename_count == 0)
1051 warning ("no input file specified");
1053 if (resource_name)
1055 const char *resource_filename;
1057 /* Only one resource file may be compiled at a time. */
1058 assert (TREE_CHAIN (current_file_list) == NULL);
1060 resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
1061 compile_resource_file (resource_name, resource_filename);
1063 return;
1066 current_jcf = main_jcf;
1067 current_file_list = nreverse (current_file_list);
1068 for (node = current_file_list; node; node = TREE_CHAIN (node))
1070 unsigned char magic_string[4];
1071 uint32 magic = 0;
1072 tree name = TREE_VALUE (node);
1074 /* Skip already parsed files */
1075 if (HAS_BEEN_ALREADY_PARSED_P (name))
1076 continue;
1078 /* Close previous descriptor, if any */
1079 if (finput && fclose (finput))
1080 fatal_io_error ("can't close input file %s", main_input_filename);
1082 finput = fopen (IDENTIFIER_POINTER (name), "rb");
1083 if (finput == NULL)
1084 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
1086 #ifdef IO_BUFFER_SIZE
1087 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
1088 _IOFBF, IO_BUFFER_SIZE);
1089 #endif
1090 input_filename = IDENTIFIER_POINTER (name);
1092 /* Figure what kind of file we're dealing with */
1093 if (fread (magic_string, 1, 4, finput) == 4)
1095 fseek (finput, 0L, SEEK_SET);
1096 magic = GET_u4 (magic_string);
1098 if (magic == 0xcafebabe)
1100 CLASS_FILE_P (node) = 1;
1101 current_jcf = ALLOC (sizeof (JCF));
1102 JCF_ZERO (current_jcf);
1103 current_jcf->read_state = finput;
1104 current_jcf->filbuf = jcf_filbuf_from_stdio;
1105 jcf_parse (current_jcf);
1106 TYPE_JCF (current_class) = current_jcf;
1107 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1108 TREE_PURPOSE (node) = current_class;
1110 else if (magic == (JCF_u4)ZIPMAGIC)
1112 ZIP_FILE_P (node) = 1;
1113 JCF_ZERO (main_jcf);
1114 main_jcf->read_state = finput;
1115 main_jcf->filbuf = jcf_filbuf_from_stdio;
1116 if (open_in_zip (main_jcf, input_filename, NULL, 0) < 0)
1117 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1118 localToFile = SeenZipFiles;
1119 /* Register all the class defined there. */
1120 process_zip_dir (main_jcf->read_state);
1121 parse_zip_file_entries ();
1123 for (each entry)
1124 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1127 else
1129 JAVA_FILE_P (node) = 1;
1130 java_push_parser_context ();
1131 java_parser_context_save_global ();
1132 parse_source_file_1 (name, finput);
1133 java_parser_context_restore_global ();
1134 java_pop_parser_context (1);
1138 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1140 input_filename = ctxp->filename;
1141 parse_source_file_2 ();
1144 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1146 input_filename = ctxp->filename;
1147 parse_source_file_3 ();
1150 for (node = current_file_list; node; node = TREE_CHAIN (node))
1152 input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1153 if (CLASS_FILE_P (node))
1155 current_class = TREE_PURPOSE (node);
1156 current_jcf = TYPE_JCF (current_class);
1157 layout_class (current_class);
1158 load_inner_classes (current_class);
1159 parse_class_file ();
1160 JCF_FINISH (current_jcf);
1163 input_filename = main_input_filename;
1165 java_expand_classes ();
1166 if (!java_report_errors () && !flag_syntax_only)
1168 emit_register_classes ();
1169 if (flag_indirect_dispatch)
1170 emit_offset_symbol_table ();
1174 /* Process all class entries found in the zip file. */
1175 static void
1176 parse_zip_file_entries (void)
1178 struct ZipDirectory *zdir;
1179 int i;
1181 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1182 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1184 tree class;
1186 /* We don't need to consider those files. */
1187 if (!zdir->size || !zdir->filename_offset)
1188 continue;
1190 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
1191 current_jcf = TYPE_JCF (class);
1192 current_class = class;
1194 if ( !CLASS_LOADED_P (class))
1196 if (! CLASS_PARSED_P (class))
1198 read_zip_member(current_jcf, zdir, localToFile);
1199 jcf_parse (current_jcf);
1201 layout_class (current_class);
1202 load_inner_classes (current_class);
1205 if (TYPE_SIZE (current_class) != error_mark_node)
1207 input_filename = current_jcf->filename;
1208 parse_class_file ();
1209 FREE (current_jcf->buffer); /* No longer necessary */
1210 /* Note: there is a way to free this buffer right after a
1211 class seen in a zip file has been parsed. The idea is the
1212 set its jcf in such a way that buffer will be reallocated
1213 the time the code for the class will be generated. FIXME. */
1218 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1219 jcf up for further processing and link it to the created class. */
1221 static void
1222 process_zip_dir (FILE *finput)
1224 int i;
1225 ZipDirectory *zdir;
1227 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1228 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1230 char *class_name, *file_name, *class_name_in_zip_dir;
1231 tree class;
1232 JCF *jcf;
1233 int j;
1235 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1237 /* We choose to not to process entries with a zero size or entries
1238 not bearing the .class extension. */
1239 if (!zdir->size || !zdir->filename_offset ||
1240 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
1241 ".class", 6))
1243 /* So it will be skipped in parse_zip_file_entries */
1244 zdir->size = 0;
1245 continue;
1248 class_name = ALLOC (zdir->filename_length+1-6);
1249 file_name = ALLOC (zdir->filename_length+1);
1250 jcf = ALLOC (sizeof (JCF));
1251 JCF_ZERO (jcf);
1253 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1254 class_name [zdir->filename_length-6] = '\0';
1255 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1256 file_name [zdir->filename_length] = '\0';
1258 for (j=0; class_name[j]; j++)
1259 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1261 /* Yes, we write back the true class name into the zip directory. */
1262 strcpy (class_name_in_zip_dir, class_name);
1263 zdir->filename_length = j;
1264 class = lookup_class (get_identifier (class_name));
1266 jcf->read_state = finput;
1267 jcf->filbuf = jcf_filbuf_from_stdio;
1268 jcf->java_source = 0;
1269 jcf->classname = class_name;
1270 jcf->filename = file_name;
1271 jcf->zipd = zdir;
1273 TYPE_JCF (class) = jcf;
1277 /* Initialization. */
1279 void
1280 init_jcf_parse ()
1282 /* Register roots with the garbage collector. */
1283 ggc_add_tree_root (parse_roots, ARRAY_SIZE (parse_roots));
1285 ggc_add_root (&current_jcf, 1, sizeof (JCF), (void (*)(void *))ggc_mark_jcf);
1287 init_src_parse ();