2001-02-14 Tom Tromey <tromey@redhat.com>
[official-gcc.git] / gcc / java / jcf-parse.c
blobe8eacc5dee7e2cb4453091d8070142f84cee0aec
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Per Bothner <bothner@cygnus.com> */
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "obstack.h"
31 #include "flags.h"
32 #include "java-except.h"
33 #include "input.h"
34 #include "java-tree.h"
35 #include "toplev.h"
36 #include "parse.h"
37 #include "ggc.h"
39 #ifdef HAVE_LOCALE_H
40 #include <locale.h>
41 #endif
43 #ifdef HAVE_NL_LANGINFO
44 #include <langinfo.h>
45 #endif
47 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
48 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
49 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
50 #define JPOOL_UTF_DATA(JCF, INDEX) \
51 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
52 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
53 do { \
54 unsigned char save; unsigned char *text; \
55 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
56 text = (JCF)->read_ptr; \
57 save = text[LENGTH]; \
58 text[LENGTH] = 0; \
59 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
60 text[LENGTH] = save; \
61 JCF_SKIP (JCF, LENGTH); } while (0)
63 #include "jcf.h"
65 extern struct obstack *saveable_obstack;
66 extern struct obstack temporary_obstack;
67 extern struct obstack permanent_obstack;
69 /* Set to non-zero value in order to emit class initilization code
70 before static field references. */
71 extern int always_initialize_class_p;
73 static tree parse_roots[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
75 /* The FIELD_DECL for the current field. */
76 #define current_field parse_roots[0]
78 /* The METHOD_DECL for the current method. */
79 #define current_method parse_roots[1]
81 /* A list of file names. */
82 #define current_file_list parse_roots[2]
84 /* The Java .class file that provides main_class; the main input file. */
85 static struct JCF main_jcf[1];
87 /* Declarations of some functions used here. */
88 static void handle_innerclass_attribute PARAMS ((int count, JCF *));
89 static tree give_name_to_class PARAMS ((JCF *jcf, int index));
90 static void parse_zip_file_entries PARAMS ((void));
91 static void process_zip_dir PARAMS ((FILE *));
92 static void parse_source_file PARAMS ((tree, FILE *));
93 static void jcf_parse_source PARAMS ((void));
94 static int jcf_figure_file_type PARAMS ((JCF *));
95 static void parse_class_file PARAMS ((void));
96 static void set_source_filename PARAMS ((JCF *, int));
97 static int predefined_filename_p PARAMS ((tree));
98 static void ggc_mark_jcf PARAMS ((void**));
100 /* Mark (for garbage collection) all the tree nodes that are
101 referenced from JCF's constant pool table. */
103 static void
104 ggc_mark_jcf (elt)
105 void **elt;
107 JCF *jcf = *(JCF**) elt;
108 if (jcf != NULL)
110 CPool *cpool = &jcf->cpool;
111 int size = CPOOL_COUNT(cpool);
112 int index;
113 for (index = 1; index < size; index++)
115 int tag = JPOOL_TAG (jcf, index);
116 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
117 ggc_mark_tree ((tree) cpool->data[index]);
122 /* Handle "SourceFile" attribute. */
124 static void
125 set_source_filename (jcf, index)
126 JCF *jcf;
127 int index;
129 tree sfname_id = get_name_constant (jcf, index);
130 const char *sfname = IDENTIFIER_POINTER (sfname_id);
131 if (input_filename != NULL)
133 int old_len = strlen (input_filename);
134 int new_len = IDENTIFIER_LENGTH (sfname_id);
135 /* Use the current input_filename (derived from the class name)
136 if it has a directory prefix, but otherwise matches sfname. */
137 if (old_len > new_len
138 && strcmp (sfname, input_filename + old_len - new_len) == 0
139 && (input_filename[old_len - new_len - 1] == '/'
140 || input_filename[old_len - new_len - 1] == '\\'))
141 return;
143 input_filename = sfname;
144 DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
145 if (current_class == main_class) main_input_filename = input_filename;
148 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
150 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
151 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
152 current_class = give_name_to_class (jcf, THIS); \
153 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
155 #define HANDLE_CLASS_INTERFACE(INDEX) \
156 add_interface (current_class, get_class_constant (jcf, INDEX))
158 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
159 { int sig_index = SIGNATURE; \
160 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
161 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
162 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
163 if ((ACCESS_FLAGS) & ACC_FINAL) \
164 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
167 #define HANDLE_END_FIELDS() \
168 (current_field = NULL_TREE)
170 #define HANDLE_CONSTANTVALUE(INDEX) \
171 { tree constant; int index = INDEX; \
172 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
173 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
174 constant = build_utf8_ref (name); \
176 else \
177 constant = get_constant (jcf, index); \
178 set_constant_value (current_field, constant); }
180 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
181 (current_method = add_method (current_class, ACCESS_FLAGS, \
182 get_name_constant (jcf, NAME), \
183 get_name_constant (jcf, SIGNATURE)), \
184 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
185 DECL_LINENUMBERS_OFFSET (current_method) = 0)
187 #define HANDLE_END_METHODS() \
188 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
189 if (handle_type != current_class) layout_type (handle_type); \
190 current_method = NULL_TREE; }
192 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
193 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
194 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
195 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
196 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
198 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
199 { int n = (COUNT); \
200 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
201 JCF_SKIP (jcf, n * 10); }
203 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
204 { int n = (COUNT); \
205 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
206 JCF_SKIP (jcf, n * 4); }
208 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
210 int n = COUNT; \
211 tree list = DECL_FUNCTION_THROWS (current_method); \
212 while (--n >= 0) \
214 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
215 list = tree_cons (NULL_TREE, thrown_class, list); \
217 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
220 /* Link seen inner classes to their outer context and register the
221 inner class to its outer context. They will be later loaded. */
222 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
223 handle_innerclass_attribute (COUNT, jcf)
225 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
227 /* Irrelevant decls should have been nullified by the END macros. \
228 We only handle the `Synthetic' attribute on method DECLs. \
229 DECL_ARTIFICIAL on fields is used for something else (See \
230 PUSH_FIELD in java-tree.h) */ \
231 if (current_method) \
232 DECL_ARTIFICIAL (current_method) = 1; \
235 #include "jcf-reader.c"
237 static int yydebug;
239 tree
240 parse_signature (jcf, sig_index)
241 JCF *jcf;
242 int sig_index;
244 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
245 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
246 abort ();
247 else
248 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
249 JPOOL_UTF_LENGTH (jcf, sig_index));
252 void
253 init_lex ()
255 /* Make identifier nodes long enough for the language-specific slots. */
256 set_identifier_size (sizeof (struct lang_identifier));
259 void
260 set_yydebug (value)
261 int value;
263 yydebug = value;
266 tree
267 get_constant (jcf, index)
268 JCF *jcf;
269 int index;
271 tree value;
272 int tag;
273 if (index <= 0 || index >= JPOOL_SIZE(jcf))
274 goto bad;
275 tag = JPOOL_TAG (jcf, index);
276 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
277 return (tree) jcf->cpool.data[index];
278 switch (tag)
280 case CONSTANT_Integer:
282 jint num = JPOOL_INT(jcf, index);
283 value = build_int_2 (num, num < 0 ? -1 : 0);
284 TREE_TYPE (value) = int_type_node;
285 break;
287 case CONSTANT_Long:
289 jint num = JPOOL_INT (jcf, index);
290 HOST_WIDE_INT lo, hi;
291 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
292 num = JPOOL_INT (jcf, index+1) & 0xffffffff;
293 add_double (lo, hi, num, 0, &lo, &hi);
294 value = build_int_2 (lo, hi);
295 TREE_TYPE (value) = long_type_node;
296 force_fit_type (value, 0);
297 break;
299 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
300 case CONSTANT_Float:
302 jint num = JPOOL_INT(jcf, index);
303 REAL_VALUE_TYPE d;
304 #ifdef REAL_ARITHMETIC
305 d = REAL_VALUE_FROM_TARGET_SINGLE (num);
306 #else
307 union { float f; jint i; } u;
308 u.i = num;
309 d = u.f;
310 #endif
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);
323 if (FLOAT_WORDS_BIG_ENDIAN)
325 num[0] = hi;
326 num[1] = lo;
328 else
330 num[0] = lo;
331 num[1] = hi;
333 #ifdef REAL_ARITHMETIC
334 d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
335 #else
337 union { double d; jint i[2]; } u;
338 u.i[0] = (jint) num[0];
339 u.i[1] = (jint) num[1];
340 d = u.d;
342 #endif
343 value = build_real (double_type_node, d);
344 break;
346 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
347 case CONSTANT_String:
349 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
350 const char *utf8_ptr = IDENTIFIER_POINTER (name);
351 int utf8_len = IDENTIFIER_LENGTH (name);
352 unsigned char *str_ptr;
353 unsigned char *str;
354 const unsigned char *utf8;
355 int i, str_len;
357 /* Count the number of Unicode characters in the string,
358 while checking for a malformed Utf8 string. */
359 utf8 = (const unsigned char *) utf8_ptr;
360 i = utf8_len;
361 str_len = 0;
362 while (i > 0)
364 int char_len = UT8_CHAR_LENGTH (*utf8);
365 if (char_len < 0 || char_len > 3 || char_len > i)
366 fatal_error ("bad string constant");
368 utf8 += char_len;
369 i -= char_len;
370 str_len++;
373 /* Allocate a scratch buffer, convert the string to UCS2, and copy it
374 into the new space. */
375 str_ptr = (unsigned char *) alloca (2 * str_len);
376 str = str_ptr;
377 utf8 = (const unsigned char *)utf8_ptr;
379 for (i = 0; i < str_len; i++)
381 int char_value;
382 int char_len = UT8_CHAR_LENGTH (*utf8);
383 switch (char_len)
385 case 1:
386 char_value = *utf8++;
387 break;
388 case 2:
389 char_value = *utf8++ & 0x1F;
390 char_value = (char_value << 6) | (*utf8++ & 0x3F);
391 break;
392 case 3:
393 char_value = *utf8++ & 0x0F;
394 char_value = (char_value << 6) | (*utf8++ & 0x3F);
395 char_value = (char_value << 6) | (*utf8++ & 0x3F);
396 break;
397 default:
398 goto bad;
400 if (BYTES_BIG_ENDIAN)
402 *str++ = char_value >> 8;
403 *str++ = char_value & 0xFF;
405 else
407 *str++ = char_value & 0xFF;
408 *str++ = char_value >> 8;
411 value = build_string (str - str_ptr, str_ptr);
412 TREE_TYPE (value) = build_pointer_type (string_type_node);
414 break;
415 default:
416 goto bad;
418 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
419 jcf->cpool.data [index] = (jword) value;
420 return value;
421 bad:
422 internal_error ("bad value constant type %d, index %d",
423 JPOOL_TAG (jcf, index), index);
426 tree
427 get_name_constant (jcf, index)
428 JCF *jcf;
429 int index;
431 tree name = get_constant (jcf, index);
433 if (TREE_CODE (name) != IDENTIFIER_NODE)
434 abort ();
436 return name;
439 /* Handle reading innerclass attributes. If a non zero entry (denoting
440 a non anonymous entry) is found, We augment the inner class list of
441 the outer context with the newly resolved innerclass. */
443 static void
444 handle_innerclass_attribute (count, jcf)
445 int count;
446 JCF *jcf;
448 int c = (count);
449 while (c--)
451 /* Read inner_class_info_index. This may be 0 */
452 int icii = JCF_readu2 (jcf);
453 /* Read outer_class_info_index. If the innerclasses attribute
454 entry isn't a member (like an inner class) the value is 0. */
455 int ocii = JCF_readu2 (jcf);
456 /* Read inner_name_index. If the class we're dealing with is
457 an annonymous class, it must be 0. */
458 int ini = JCF_readu2 (jcf);
459 /* If icii is 0, don't try to read the class. */
460 if (icii >= 0)
462 tree class = get_class_constant (jcf, icii);
463 tree decl = TYPE_NAME (class);
464 /* Skip reading further if ocii is null */
465 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
467 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
468 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
469 DECL_CONTEXT (decl) = outer;
470 DECL_INNER_CLASS_LIST (outer) =
471 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
472 CLASS_COMPLETE_P (decl) = 1;
475 JCF_SKIP (jcf, 2);
479 static tree
480 give_name_to_class (jcf, i)
481 JCF *jcf;
482 int i;
484 if (i <= 0 || i >= JPOOL_SIZE (jcf)
485 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
486 abort ();
487 else
489 tree this_class;
490 int j = JPOOL_USHORT1 (jcf, i);
491 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
492 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
493 JPOOL_UTF_LENGTH (jcf, j));
494 this_class = lookup_class (class_name);
495 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
496 lineno = 0;
497 if (main_input_filename == NULL && jcf == main_jcf)
498 main_input_filename = input_filename;
500 jcf->cpool.data[i] = (jword) this_class;
501 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
502 return this_class;
506 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
508 tree
509 get_class_constant (JCF *jcf , int i)
511 tree type;
512 if (i <= 0 || i >= JPOOL_SIZE (jcf)
513 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
514 abort ();
516 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
518 int name_index = JPOOL_USHORT1 (jcf, i);
519 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
520 const char *name = JPOOL_UTF_DATA (jcf, name_index);
521 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
523 if (name[0] == '[') /* Handle array "classes". */
524 type = TREE_TYPE (parse_signature_string (name, nlength));
525 else
527 tree cname = unmangle_classname (name, nlength);
528 type = lookup_class (cname);
530 jcf->cpool.data[i] = (jword) type;
531 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
533 else
534 type = (tree) jcf->cpool.data[i];
535 return type;
538 /* Read a class with the fully qualified-name NAME.
539 Return 1 iff we read the requested file.
540 (It is still possible we failed if the file did not
541 define the class it is supposed to.) */
544 read_class (name)
545 tree name;
547 JCF this_jcf, *jcf;
548 tree icv, class;
549 tree save_current_class = current_class;
550 const char *save_input_filename = input_filename;
551 JCF *save_current_jcf = current_jcf;
553 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
555 class = TREE_TYPE (icv);
556 jcf = TYPE_JCF (class);
558 else
559 jcf = NULL;
561 if (jcf == NULL)
563 this_jcf.zipd = NULL;
564 jcf = &this_jcf;
565 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
566 &this_jcf, 1) == 0)
567 return 0;
570 current_jcf = jcf;
572 if (current_jcf->java_source)
573 jcf_parse_source ();
574 else {
575 java_parser_context_save_global ();
576 java_push_parser_context ();
577 input_filename = current_jcf->filename;
578 if (JCF_SEEN_IN_ZIP (current_jcf))
579 read_zip_member(current_jcf, current_jcf->zipd, current_jcf->zipd->zipf);
580 jcf_parse (current_jcf);
581 java_pop_parser_context (0);
582 java_parser_context_restore_global ();
585 if (! JCF_SEEN_IN_ZIP (current_jcf))
586 JCF_FINISH (current_jcf);
588 current_class = save_current_class;
589 input_filename = save_input_filename;
590 current_jcf = save_current_jcf;
591 return 1;
594 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
595 called from the parser, otherwise it's a RECORD_TYPE node. If
596 VERBOSE is 1, print error message on failure to load a class. */
598 /* Replace calls to load_class by having callers call read_class directly
599 - and then perhaps rename read_class to load_class. FIXME */
601 void
602 load_class (class_or_name, verbose)
603 tree class_or_name;
604 int verbose;
606 tree name;
608 /* class_or_name can be the name of the class we want to load */
609 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
610 name = class_or_name;
611 /* In some cases, it's a dependency that we process earlier that
612 we though */
613 else if (TREE_CODE (class_or_name) == TREE_LIST)
614 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
615 /* Or it's a type in the making */
616 else
617 name = DECL_NAME (TYPE_NAME (class_or_name));
619 if (read_class (name) == 0 && verbose)
620 error ("Cannot find file for class %s.", IDENTIFIER_POINTER (name));
623 /* Parse a source file when JCF refers to a source file. */
625 static void
626 jcf_parse_source ()
628 tree file;
629 FILE *finput;
631 java_parser_context_save_global ();
632 java_push_parser_context ();
633 BUILD_FILENAME_IDENTIFIER_NODE (file, current_jcf->filename);
634 if (wfl_operator == NULL_TREE)
635 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
636 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
637 input_filename = ggc_strdup (current_jcf->filename);
638 current_class = NULL_TREE;
639 current_function_decl = NULL_TREE;
640 if (!HAS_BEEN_ALREADY_PARSED_P (file))
642 if (!(finput = fopen (input_filename, "r")))
643 fatal_io_error ("can't reopen %s", input_filename);
644 parse_source_file (file, finput);
645 if (fclose (finput))
646 fatal_io_error ("can't close %s", input_filename);
648 java_pop_parser_context (IS_A_COMMAND_LINE_FILENAME_P (file));
649 java_parser_context_restore_global ();
652 /* Parse the .class file JCF. */
654 void
655 jcf_parse (jcf)
656 JCF* jcf;
658 int i, code;
659 tree current;
661 if (jcf_parse_preamble (jcf) != 0)
662 fatal_error ("not a valid Java .class file");
663 code = jcf_parse_constant_pool (jcf);
664 if (code != 0)
665 fatal_error ("error while parsing constant pool");
666 code = verify_constant_pool (jcf);
667 if (code > 0)
668 fatal_error ("error in constant pool entry #%d\n", code);
670 jcf_parse_class (jcf);
671 if (main_class == NULL_TREE)
672 main_class = current_class;
673 if (! quiet_flag && TYPE_NAME (current_class))
674 fprintf (stderr, " %s %s",
675 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
676 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
677 if (CLASS_LOADED_P (current_class))
678 return;
679 CLASS_LOADED_P (current_class) = 1;
681 for (i = 1; i < JPOOL_SIZE(jcf); i++)
683 switch (JPOOL_TAG (jcf, i))
685 case CONSTANT_Class:
686 get_class_constant (jcf, i);
687 break;
691 code = jcf_parse_fields (jcf);
692 if (code != 0)
693 fatal_error ("error while parsing fields");
694 code = jcf_parse_methods (jcf);
695 if (code != 0)
696 fatal_error ("error while parsing methods");
697 code = jcf_parse_final_attributes (jcf);
698 if (code != 0)
699 fatal_error ("error while parsing final attributes");
701 /* The fields of class_type_node are already in correct order. */
702 if (current_class != class_type_node && current_class != object_type_node)
703 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
705 layout_class (current_class);
706 if (current_class == object_type_node)
707 layout_class_methods (object_type_node);
708 else
709 all_class_list = tree_cons (NULL_TREE,
710 TYPE_NAME (current_class), all_class_list );
712 /* And if we came across inner classes, load them now. */
713 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (current_class)); current;
714 current = TREE_CHAIN (current))
716 tree name = DECL_NAME (TREE_PURPOSE (current));
717 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
718 if (decl && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
719 load_class (name, 1);
723 void
724 init_outgoing_cpool ()
726 current_constant_pool_data_ref = NULL_TREE;
727 outgoing_cpool = (struct CPool *)xmalloc (sizeof (struct CPool));
728 memset (outgoing_cpool, 0, sizeof (struct CPool));
731 static void
732 parse_class_file ()
734 tree method;
735 const char *save_input_filename = input_filename;
736 int save_lineno = lineno;
738 java_layout_seen_class_methods ();
740 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
741 lineno = 0;
742 debug_start_source_file (input_filename);
743 init_outgoing_cpool ();
745 /* Currently we always have to emit calls to _Jv_InitClass when
746 compiling from class files. */
747 always_initialize_class_p = 1;
749 for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
750 method != NULL_TREE; method = TREE_CHAIN (method))
752 JCF *jcf = current_jcf;
754 if (METHOD_ABSTRACT (method))
755 continue;
757 if (METHOD_NATIVE (method))
759 if (! flag_jni)
760 continue;
761 DECL_MAX_LOCALS (method)
762 = list_length (TYPE_ARG_TYPES (TREE_TYPE (method)));
763 start_java_method (method);
764 give_name_to_locals (jcf);
765 expand_expr_stmt (build_jni_stub (method));
766 end_java_method ();
767 continue;
770 if (DECL_CODE_OFFSET (method) == 0)
772 error ("missing Code attribute");
773 continue;
776 lineno = 0;
777 if (DECL_LINENUMBERS_OFFSET (method))
779 register int i;
780 register unsigned char *ptr;
781 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
782 linenumber_count = i = JCF_readu2 (jcf);
783 linenumber_table = ptr = jcf->read_ptr;
785 for (ptr += 2; --i >= 0; ptr += 4)
787 int line = GET_u2 (ptr);
788 /* Set initial lineno lineno to smallest linenumber.
789 * Needs to be set before init_function_start. */
790 if (lineno == 0 || line < lineno)
791 lineno = line;
794 else
796 linenumber_table = NULL;
797 linenumber_count = 0;
800 start_java_method (method);
802 note_instructions (jcf, method);
804 give_name_to_locals (jcf);
806 /* Actually generate code. */
807 expand_byte_code (jcf, method);
809 end_java_method ();
812 if (flag_emit_class_files)
813 write_classfile (current_class);
815 finish_class ();
817 debug_end_source_file (save_lineno);
818 input_filename = save_input_filename;
819 lineno = save_lineno;
822 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
824 static void
825 parse_source_file (file, finput)
826 tree file;
827 FILE *finput;
829 int save_error_count = java_error_count;
830 /* Mark the file as parsed */
831 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
833 jcf_dependency_add_file (input_filename, 0);
835 lang_init_source (1); /* Error msgs have no method prototypes */
837 /* There's no point in trying to find the current encoding unless we
838 are going to do something intelligent with it -- hence the test
839 for iconv. */
840 #ifdef HAVE_ICONV
841 #ifdef HAVE_NL_LANGINFO
842 setlocale (LC_CTYPE, "");
843 if (current_encoding == NULL)
844 current_encoding = nl_langinfo (CODESET);
845 #endif /* HAVE_NL_LANGINFO */
846 #endif /* HAVE_ICONV */
847 if (current_encoding == NULL || *current_encoding == '\0')
848 current_encoding = DEFAULT_ENCODING;
850 /* Initialize the parser */
851 java_init_lex (finput, current_encoding);
852 java_parse_abort_on_error ();
854 java_parse (); /* Parse and build partial tree nodes. */
855 java_parse_abort_on_error ();
856 java_complete_class (); /* Parse unsatisfied class decl. */
857 java_parse_abort_on_error ();
858 java_check_circular_reference (); /* Check on circular references */
859 java_parse_abort_on_error ();
860 java_fix_constructors (); /* Fix the constructors */
861 java_parse_abort_on_error ();
862 java_reorder_fields (); /* Reorder the fields */
865 static int
866 predefined_filename_p (node)
867 tree node;
869 int i;
870 for (i = 0; i < PREDEF_FILENAMES_SIZE; i++)
871 if (predef_filenames [i] == node)
872 return 1;
873 return 0;
877 yyparse ()
879 int several_files = 0;
880 char *list = xstrdup (input_filename), *next;
881 tree node;
882 FILE *finput;
886 next = strchr (list, '&');
887 if (next)
889 *next++ = '\0';
890 several_files = 1;
893 if (list[0])
895 char *value;
896 tree id;
897 int twice = 0;
899 int len = strlen (list);
901 if (*list != '/' && several_files)
902 obstack_grow (&temporary_obstack, "./", 2);
904 obstack_grow0 (&temporary_obstack, list, len);
905 value = obstack_finish (&temporary_obstack);
907 /* Exclude file that we see twice on the command line. For
908 all files except {Class,Error,Object,RuntimeException,String,
909 Throwable}.java we can rely on maybe_get_identifier. For
910 these files, we need to do a linear search of
911 current_file_list. This search happens only for these
912 files, presumably only when we're recompiling libgcj. */
914 if ((id = maybe_get_identifier (value)))
916 if (predefined_filename_p (id))
918 tree c;
919 for (c = current_file_list; c; c = TREE_CHAIN (c))
920 if (TREE_VALUE (c) == id)
921 twice = 1;
923 else
924 twice = 1;
927 if (twice)
929 const char *saved_input_filename = input_filename;
930 input_filename = value;
931 warning ("source file seen twice on command line and will be compiled only once.");
932 input_filename = saved_input_filename;
934 else
936 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
937 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
938 current_file_list = tree_cons (NULL_TREE, node,
939 current_file_list);
942 list = next;
944 while (next);
946 current_jcf = main_jcf;
947 current_file_list = nreverse (current_file_list);
948 for (node = current_file_list; node; node = TREE_CHAIN (node))
950 tree name = TREE_VALUE (node);
952 /* Skip already parsed files */
953 if (HAS_BEEN_ALREADY_PARSED_P (name))
954 continue;
956 /* Close previous descriptor, if any */
957 if (main_jcf->read_state && fclose (main_jcf->read_state))
958 fatal_io_error ("can't close %s",
959 main_jcf->filename ? main_jcf->filename : "<unknown>");
961 /* Set jcf up and open a new file */
962 JCF_ZERO (main_jcf);
963 main_jcf->read_state = fopen (IDENTIFIER_POINTER (name), "rb");
964 if (main_jcf->read_state == NULL)
965 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
967 /* Set new input_filename and finput */
968 finput = main_jcf->read_state;
969 #ifdef IO_BUFFER_SIZE
970 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
971 _IOFBF, IO_BUFFER_SIZE);
972 #endif
973 input_filename = IDENTIFIER_POINTER (name);
974 main_jcf->filbuf = jcf_filbuf_from_stdio;
976 switch (jcf_figure_file_type (current_jcf))
978 case JCF_ZIP:
979 parse_zip_file_entries ();
980 break;
981 case JCF_CLASS:
982 jcf_parse (current_jcf);
983 parse_class_file ();
984 break;
985 case JCF_SOURCE:
986 java_push_parser_context ();
987 java_parser_context_save_global ();
988 parse_source_file (name, finput);
989 java_parser_context_restore_global ();
990 java_pop_parser_context (1);
991 break;
995 java_expand_classes ();
996 if (!java_report_errors () && !flag_syntax_only)
997 emit_register_classes ();
998 return 0;
1001 static struct ZipFile *localToFile;
1003 /* Process all class entries found in the zip file. */
1004 static void
1005 parse_zip_file_entries (void)
1007 struct ZipDirectory *zdir;
1008 int i;
1010 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1011 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1013 tree class;
1015 /* We don't need to consider those files. */
1016 if (!zdir->size || !zdir->filename_offset)
1017 continue;
1019 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
1020 current_jcf = TYPE_JCF (class);
1021 current_class = class;
1023 if ( !CLASS_LOADED_P (class))
1025 read_zip_member(current_jcf, zdir, localToFile);
1026 jcf_parse (current_jcf);
1029 if (TYPE_SIZE (current_class) != error_mark_node)
1031 input_filename = current_jcf->filename;
1032 parse_class_file ();
1033 FREE (current_jcf->buffer); /* No longer necessary */
1034 /* Note: there is a way to free this buffer right after a
1035 class seen in a zip file has been parsed. The idea is the
1036 set its jcf in such a way that buffer will be reallocated
1037 the time the code for the class will be generated. FIXME. */
1042 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1043 jcf up for further processing and link it to the created class. */
1045 static void
1046 process_zip_dir (FILE *finput)
1048 int i;
1049 ZipDirectory *zdir;
1051 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1052 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1054 char *class_name, *file_name, *class_name_in_zip_dir;
1055 tree class;
1056 JCF *jcf;
1057 int j;
1059 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1061 /* We choose to not to process entries with a zero size or entries
1062 not bearing the .class extention. */
1063 if (!zdir->size || !zdir->filename_offset ||
1064 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
1065 ".class", 6))
1067 /* So it will be skipped in parse_zip_file_entries */
1068 zdir->size = 0;
1069 continue;
1072 class_name = ALLOC (zdir->filename_length+1-6);
1073 file_name = ALLOC (zdir->filename_length+1);
1074 jcf = ALLOC (sizeof (JCF));
1075 JCF_ZERO (jcf);
1077 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1078 class_name [zdir->filename_length-6] = '\0';
1079 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1080 file_name [zdir->filename_length] = '\0';
1082 for (j=0; class_name[j]; j++)
1083 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1085 /* Yes, we write back the true class name into the zip directory. */
1086 strcpy (class_name_in_zip_dir, class_name);
1087 zdir->filename_length = j;
1088 class = lookup_class (get_identifier (class_name));
1090 jcf->read_state = finput;
1091 jcf->filbuf = jcf_filbuf_from_stdio;
1092 jcf->java_source = 0;
1093 jcf->classname = class_name;
1094 jcf->filename = file_name;
1095 jcf->zipd = zdir;
1097 TYPE_JCF (class) = jcf;
1101 /* Figure what kind of file we're dealing with */
1102 static int
1103 DEFUN(jcf_figure_file_type, (jcf),
1104 JCF *jcf)
1106 unsigned char magic_string[4];
1107 uint32 magic;
1109 if (fread (magic_string, 1, 4, jcf->read_state) != 4)
1110 jcf_unexpected_eof (jcf, 4);
1112 fseek (jcf->read_state, 0L, SEEK_SET);
1113 magic = GET_u4 (magic_string);
1115 if (magic == 0xcafebabe)
1116 return JCF_CLASS;
1118 /* FIXME: is it a system file? */
1119 if (magic == (JCF_u4)ZIPMAGIC
1120 && !open_in_zip (jcf, input_filename, NULL, 0))
1122 localToFile = SeenZipFiles;
1123 /* Register all the class defined there. */
1124 process_zip_dir (jcf->read_state);
1125 return JCF_ZIP;
1128 return JCF_SOURCE;
1131 /* Initialization. */
1133 void
1134 init_jcf_parse ()
1136 /* Register roots with the garbage collector. */
1137 ggc_add_tree_root (parse_roots, sizeof (parse_roots) / sizeof(tree));
1139 ggc_add_root (&current_jcf, 1, sizeof (JCF), (void (*)(void *))ggc_mark_jcf);