* lib/objc.exp: Add -lposix4 on Solaris 2.6 and Solaris 2.7.
[official-gcc.git] / gcc / java / jcf-parse.c
blob4be3453cc41c99e3563ea0b1302440aaf1054db5
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 archive that provides main_class; the main input file. */
85 static struct JCF main_jcf[1];
87 static struct ZipFile *localToFile;
89 /* Declarations of some functions used here. */
90 static void handle_innerclass_attribute PARAMS ((int count, JCF *));
91 static tree give_name_to_class PARAMS ((JCF *jcf, int index));
92 static void parse_zip_file_entries PARAMS ((void));
93 static void process_zip_dir PARAMS ((FILE *));
94 static void parse_source_file_1 PARAMS ((tree, FILE *));
95 static void parse_source_file_2 PARAMS ((void));
96 static void parse_class_file PARAMS ((void));
97 static void set_source_filename PARAMS ((JCF *, int));
98 static int predefined_filename_p PARAMS ((tree));
99 static void ggc_mark_jcf PARAMS ((void**));
100 static void jcf_parse PARAMS ((struct JCF*));
101 static void load_inner_classes PARAMS ((tree));
103 /* Mark (for garbage collection) all the tree nodes that are
104 referenced from JCF's constant pool table. */
106 static void
107 ggc_mark_jcf (elt)
108 void **elt;
110 JCF *jcf = *(JCF**) elt;
111 if (jcf != NULL)
113 CPool *cpool = &jcf->cpool;
114 int size = CPOOL_COUNT(cpool);
115 int index;
116 for (index = 1; index < size; index++)
118 int tag = JPOOL_TAG (jcf, index);
119 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
120 ggc_mark_tree ((tree) cpool->data[index]);
125 /* Handle "SourceFile" attribute. */
127 static void
128 set_source_filename (jcf, index)
129 JCF *jcf;
130 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 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
192 if (handle_type != current_class) layout_type (handle_type); \
193 current_method = NULL_TREE; }
195 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
196 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
197 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
198 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
199 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
201 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
202 { int n = (COUNT); \
203 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
204 JCF_SKIP (jcf, n * 10); }
206 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
207 { int n = (COUNT); \
208 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
209 JCF_SKIP (jcf, n * 4); }
211 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
213 int n = COUNT; \
214 tree list = DECL_FUNCTION_THROWS (current_method); \
215 while (--n >= 0) \
217 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
218 list = tree_cons (NULL_TREE, thrown_class, list); \
220 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
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 static int yydebug;
248 tree
249 parse_signature (jcf, sig_index)
250 JCF *jcf;
251 int sig_index;
253 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
254 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
255 abort ();
256 else
257 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
258 JPOOL_UTF_LENGTH (jcf, sig_index));
261 void
262 init_lex ()
264 /* Make identifier nodes long enough for the language-specific slots. */
265 set_identifier_size (sizeof (struct lang_identifier));
268 void
269 set_yydebug (value)
270 int value;
272 yydebug = value;
275 tree
276 get_constant (jcf, index)
277 JCF *jcf;
278 int index;
280 tree value;
281 int tag;
282 if (index <= 0 || index >= JPOOL_SIZE(jcf))
283 goto bad;
284 tag = JPOOL_TAG (jcf, index);
285 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
286 return (tree) jcf->cpool.data[index];
287 switch (tag)
289 case CONSTANT_Integer:
291 jint num = JPOOL_INT(jcf, index);
292 value = build_int_2 (num, num < 0 ? -1 : 0);
293 TREE_TYPE (value) = int_type_node;
294 break;
296 case CONSTANT_Long:
298 jint num = JPOOL_INT (jcf, index);
299 HOST_WIDE_INT lo, hi;
300 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
301 num = JPOOL_INT (jcf, index+1) & 0xffffffff;
302 add_double (lo, hi, num, 0, &lo, &hi);
303 value = build_int_2 (lo, hi);
304 TREE_TYPE (value) = long_type_node;
305 force_fit_type (value, 0);
306 break;
308 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
309 case CONSTANT_Float:
311 jint num = JPOOL_INT(jcf, index);
312 REAL_VALUE_TYPE d;
313 #ifdef REAL_ARITHMETIC
314 d = REAL_VALUE_FROM_TARGET_SINGLE (num);
315 #else
316 union { float f; jint i; } u;
317 u.i = num;
318 d = u.f;
319 #endif
320 value = build_real (float_type_node, d);
321 break;
323 case CONSTANT_Double:
325 HOST_WIDE_INT num[2];
326 REAL_VALUE_TYPE d;
327 HOST_WIDE_INT lo, hi;
328 num[0] = JPOOL_INT (jcf, index);
329 lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
330 num[0] = JPOOL_INT (jcf, index+1);
331 add_double (lo, hi, num[0], 0, &lo, &hi);
332 if (FLOAT_WORDS_BIG_ENDIAN)
334 num[0] = hi;
335 num[1] = lo;
337 else
339 num[0] = lo;
340 num[1] = hi;
342 #ifdef REAL_ARITHMETIC
343 d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
344 #else
346 union { double d; jint i[2]; } u;
347 u.i[0] = (jint) num[0];
348 u.i[1] = (jint) num[1];
349 d = u.d;
351 #endif
352 value = build_real (double_type_node, d);
353 break;
355 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
356 case CONSTANT_String:
358 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
359 const char *utf8_ptr = IDENTIFIER_POINTER (name);
360 int utf8_len = IDENTIFIER_LENGTH (name);
361 unsigned char *str_ptr;
362 unsigned char *str;
363 const unsigned char *utf8;
364 int i, str_len;
366 /* Count the number of Unicode characters in the string,
367 while checking for a malformed Utf8 string. */
368 utf8 = (const unsigned char *) utf8_ptr;
369 i = utf8_len;
370 str_len = 0;
371 while (i > 0)
373 int char_len = UT8_CHAR_LENGTH (*utf8);
374 if (char_len < 0 || char_len > 3 || char_len > i)
375 fatal_error ("bad string constant");
377 utf8 += char_len;
378 i -= char_len;
379 str_len++;
382 /* Allocate a scratch buffer, convert the string to UCS2, and copy it
383 into the new space. */
384 str_ptr = (unsigned char *) alloca (2 * str_len);
385 str = str_ptr;
386 utf8 = (const unsigned char *)utf8_ptr;
388 for (i = 0; i < str_len; i++)
390 int char_value;
391 int char_len = UT8_CHAR_LENGTH (*utf8);
392 switch (char_len)
394 case 1:
395 char_value = *utf8++;
396 break;
397 case 2:
398 char_value = *utf8++ & 0x1F;
399 char_value = (char_value << 6) | (*utf8++ & 0x3F);
400 break;
401 case 3:
402 char_value = *utf8++ & 0x0F;
403 char_value = (char_value << 6) | (*utf8++ & 0x3F);
404 char_value = (char_value << 6) | (*utf8++ & 0x3F);
405 break;
406 default:
407 goto bad;
409 if (BYTES_BIG_ENDIAN)
411 *str++ = char_value >> 8;
412 *str++ = char_value & 0xFF;
414 else
416 *str++ = char_value & 0xFF;
417 *str++ = char_value >> 8;
420 value = build_string (str - str_ptr, str_ptr);
421 TREE_TYPE (value) = build_pointer_type (string_type_node);
423 break;
424 default:
425 goto bad;
427 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
428 jcf->cpool.data [index] = (jword) value;
429 return value;
430 bad:
431 internal_error ("bad value constant type %d, index %d",
432 JPOOL_TAG (jcf, index), index);
435 tree
436 get_name_constant (jcf, index)
437 JCF *jcf;
438 int index;
440 tree name = get_constant (jcf, index);
442 if (TREE_CODE (name) != IDENTIFIER_NODE)
443 abort ();
445 return name;
448 /* Handle reading innerclass attributes. If a non zero entry (denoting
449 a non anonymous entry) is found, We augment the inner class list of
450 the outer context with the newly resolved innerclass. */
452 static void
453 handle_innerclass_attribute (count, jcf)
454 int count;
455 JCF *jcf;
457 int c = (count);
458 while (c--)
460 /* Read inner_class_info_index. This may be 0 */
461 int icii = JCF_readu2 (jcf);
462 /* Read outer_class_info_index. If the innerclasses attribute
463 entry isn't a member (like an inner class) the value is 0. */
464 int ocii = JCF_readu2 (jcf);
465 /* Read inner_name_index. If the class we're dealing with is
466 an annonymous class, it must be 0. */
467 int ini = JCF_readu2 (jcf);
468 /* If icii is 0, don't try to read the class. */
469 if (icii >= 0)
471 tree class = get_class_constant (jcf, icii);
472 tree decl = TYPE_NAME (class);
473 /* Skip reading further if ocii is null */
474 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
476 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
477 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
478 DECL_CONTEXT (decl) = outer;
479 DECL_INNER_CLASS_LIST (outer) =
480 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
481 CLASS_COMPLETE_P (decl) = 1;
484 JCF_SKIP (jcf, 2);
488 static tree
489 give_name_to_class (jcf, i)
490 JCF *jcf;
491 int i;
493 if (i <= 0 || i >= JPOOL_SIZE (jcf)
494 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
495 abort ();
496 else
498 tree this_class;
499 int j = JPOOL_USHORT1 (jcf, i);
500 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
501 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
502 JPOOL_UTF_LENGTH (jcf, j));
503 this_class = lookup_class (class_name);
504 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
505 lineno = 0;
506 if (main_input_filename == NULL && jcf == main_jcf)
507 main_input_filename = input_filename;
509 jcf->cpool.data[i] = (jword) this_class;
510 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
511 return this_class;
515 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
517 tree
518 get_class_constant (JCF *jcf , int i)
520 tree type;
521 if (i <= 0 || i >= JPOOL_SIZE (jcf)
522 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
523 abort ();
525 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
527 int name_index = JPOOL_USHORT1 (jcf, i);
528 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
529 const char *name = JPOOL_UTF_DATA (jcf, name_index);
530 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
532 if (name[0] == '[') /* Handle array "classes". */
533 type = TREE_TYPE (parse_signature_string (name, nlength));
534 else
536 tree cname = unmangle_classname (name, nlength);
537 type = lookup_class (cname);
539 jcf->cpool.data[i] = (jword) type;
540 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
542 else
543 type = (tree) jcf->cpool.data[i];
544 return type;
547 /* Read a class with the fully qualified-name NAME.
548 Return 1 iff we read the requested file.
549 (It is still possible we failed if the file did not
550 define the class it is supposed to.) */
553 read_class (name)
554 tree name;
556 JCF this_jcf, *jcf;
557 tree icv, class = NULL_TREE;
558 tree save_current_class = current_class;
559 const char *save_input_filename = input_filename;
560 JCF *save_current_jcf = current_jcf;
561 int generate;
563 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
565 class = TREE_TYPE (icv);
566 jcf = TYPE_JCF (class);
568 else
569 jcf = NULL;
571 if (jcf == NULL)
573 this_jcf.zipd = NULL;
574 jcf = &this_jcf;
575 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
576 &this_jcf, 1) == 0)
577 return 0;
580 current_jcf = jcf;
582 java_parser_context_save_global ();
583 java_push_parser_context ();
584 if (current_jcf->java_source)
586 const char *filename = current_jcf->filename;
587 tree file;
588 FILE *finput;
590 BUILD_FILENAME_IDENTIFIER_NODE (file, filename);
591 generate = IS_A_COMMAND_LINE_FILENAME_P (file);
592 if (wfl_operator == NULL_TREE)
593 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
594 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
595 input_filename = ggc_strdup (filename);
596 current_class = NULL_TREE;
597 current_function_decl = NULL_TREE;
598 if (!HAS_BEEN_ALREADY_PARSED_P (file))
600 if (!(finput = fopen (input_filename, "r")))
601 fatal_io_error ("can't reopen %s", input_filename);
602 parse_source_file_1 (file, finput);
603 parse_source_file_2 ();
604 if (fclose (finput))
605 fatal_io_error ("can't close %s", input_filename);
607 JCF_FINISH (current_jcf);
609 else
611 input_filename = current_jcf->filename;
612 current_class = class;
613 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
615 if (JCF_SEEN_IN_ZIP (current_jcf))
616 read_zip_member(current_jcf,
617 current_jcf->zipd, current_jcf->zipd->zipf);
618 jcf_parse (current_jcf);
620 layout_class (current_class);
621 load_inner_classes (current_class);
622 generate = 0;
624 java_pop_parser_context (generate);
625 java_parser_context_restore_global ();
627 current_class = save_current_class;
628 input_filename = save_input_filename;
629 current_jcf = save_current_jcf;
630 return 1;
633 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
634 called from the parser, otherwise it's a RECORD_TYPE node. If
635 VERBOSE is 1, print error message on failure to load a class. */
637 /* Replace calls to load_class by having callers call read_class directly
638 - and then perhaps rename read_class to load_class. FIXME */
640 void
641 load_class (class_or_name, verbose)
642 tree class_or_name;
643 int verbose;
645 tree name;
647 /* class_or_name can be the name of the class we want to load */
648 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
649 name = class_or_name;
650 /* In some cases, it's a dependency that we process earlier that
651 we though */
652 else if (TREE_CODE (class_or_name) == TREE_LIST)
653 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
654 /* Or it's a type in the making */
655 else
656 name = DECL_NAME (TYPE_NAME (class_or_name));
658 if (read_class (name) == 0 && verbose)
659 error ("Cannot find file for class %s.", IDENTIFIER_POINTER (name));
662 /* Parse the .class file JCF. */
664 void
665 jcf_parse (jcf)
666 JCF* jcf;
668 int i, code;
670 if (jcf_parse_preamble (jcf) != 0)
671 fatal_error ("not a valid Java .class file");
672 code = jcf_parse_constant_pool (jcf);
673 if (code != 0)
674 fatal_error ("error while parsing constant pool");
675 code = verify_constant_pool (jcf);
676 if (code > 0)
677 fatal_error ("error in constant pool entry #%d\n", code);
679 jcf_parse_class (jcf);
680 if (main_class == NULL_TREE)
681 main_class = current_class;
682 if (! quiet_flag && TYPE_NAME (current_class))
683 fprintf (stderr, " %s %s",
684 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
685 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
686 if (CLASS_PARSED_P (current_class))
688 /* FIXME - where was first time */
689 fatal_error ("reading class %s for the second time from %s",
690 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
691 jcf->filename);
693 CLASS_PARSED_P (current_class) = 1;
695 for (i = 1; i < JPOOL_SIZE(jcf); i++)
697 switch (JPOOL_TAG (jcf, i))
699 case CONSTANT_Class:
700 get_class_constant (jcf, i);
701 break;
705 code = jcf_parse_fields (jcf);
706 if (code != 0)
707 fatal_error ("error while parsing fields");
708 code = jcf_parse_methods (jcf);
709 if (code != 0)
710 fatal_error ("error while parsing methods");
711 code = jcf_parse_final_attributes (jcf);
712 if (code != 0)
713 fatal_error ("error while parsing final attributes");
715 /* The fields of class_type_node are already in correct order. */
716 if (current_class != class_type_node && current_class != object_type_node)
717 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
719 if (current_class == object_type_node)
721 layout_class_methods (object_type_node);
722 /* If we don't have the right archive, emit a verbose warning.
723 If we're generating bytecode, emit the warning only if
724 -fforce-classes-archive-check was specified. */
725 if (!jcf->right_zip
726 && (!flag_emit_class_files || flag_force_classes_archive_check))
727 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 incorrect set. Use `info gcj \"Input Options\"' to see the info page describing how to set the classpath.", jcf->filename);
729 else
730 all_class_list = tree_cons (NULL_TREE,
731 TYPE_NAME (current_class), all_class_list );
734 /* If we came across inner classes, load them now. */
735 static void
736 load_inner_classes (cur_class)
737 tree cur_class;
739 tree current;
740 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
741 current = TREE_CHAIN (current))
743 tree name = DECL_NAME (TREE_PURPOSE (current));
744 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
745 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
746 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
747 load_class (name, 1);
751 void
752 init_outgoing_cpool ()
754 current_constant_pool_data_ref = NULL_TREE;
755 outgoing_cpool = (struct CPool *)xmalloc (sizeof (struct CPool));
756 memset (outgoing_cpool, 0, sizeof (struct CPool));
759 static void
760 parse_class_file ()
762 tree method;
763 const char *save_input_filename = input_filename;
764 int save_lineno = lineno;
766 java_layout_seen_class_methods ();
768 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
769 lineno = 0;
770 debug_start_source_file (input_filename);
771 init_outgoing_cpool ();
773 /* Currently we always have to emit calls to _Jv_InitClass when
774 compiling from class files. */
775 always_initialize_class_p = 1;
777 for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
778 method != NULL_TREE; method = TREE_CHAIN (method))
780 JCF *jcf = current_jcf;
782 if (METHOD_ABSTRACT (method))
783 continue;
785 if (METHOD_NATIVE (method))
787 if (! flag_jni)
788 continue;
789 DECL_MAX_LOCALS (method)
790 = list_length (TYPE_ARG_TYPES (TREE_TYPE (method)));
791 start_java_method (method);
792 give_name_to_locals (jcf);
793 expand_expr_stmt (build_jni_stub (method));
794 end_java_method ();
795 continue;
798 if (DECL_CODE_OFFSET (method) == 0)
800 error ("missing Code attribute");
801 continue;
804 lineno = 0;
805 if (DECL_LINENUMBERS_OFFSET (method))
807 register int i;
808 register unsigned char *ptr;
809 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
810 linenumber_count = i = JCF_readu2 (jcf);
811 linenumber_table = ptr = jcf->read_ptr;
813 for (ptr += 2; --i >= 0; ptr += 4)
815 int line = GET_u2 (ptr);
816 /* Set initial lineno lineno to smallest linenumber.
817 * Needs to be set before init_function_start. */
818 if (lineno == 0 || line < lineno)
819 lineno = line;
822 else
824 linenumber_table = NULL;
825 linenumber_count = 0;
828 start_java_method (method);
830 note_instructions (jcf, method);
832 give_name_to_locals (jcf);
834 /* Actually generate code. */
835 expand_byte_code (jcf, method);
837 end_java_method ();
840 if (flag_emit_class_files)
841 write_classfile (current_class);
843 finish_class ();
845 debug_end_source_file (save_lineno);
846 input_filename = save_input_filename;
847 lineno = save_lineno;
850 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
852 static void
853 parse_source_file_1 (file, finput)
854 tree file;
855 FILE *finput;
857 int save_error_count = java_error_count;
858 /* Mark the file as parsed */
859 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
861 jcf_dependency_add_file (input_filename, 0);
863 lang_init_source (1); /* Error msgs have no method prototypes */
865 /* There's no point in trying to find the current encoding unless we
866 are going to do something intelligent with it -- hence the test
867 for iconv. */
868 #ifdef HAVE_ICONV
869 #ifdef HAVE_NL_LANGINFO
870 setlocale (LC_CTYPE, "");
871 if (current_encoding == NULL)
872 current_encoding = nl_langinfo (CODESET);
873 #endif /* HAVE_NL_LANGINFO */
874 #endif /* HAVE_ICONV */
875 if (current_encoding == NULL || *current_encoding == '\0')
876 current_encoding = DEFAULT_ENCODING;
878 /* Initialize the parser */
879 java_init_lex (finput, current_encoding);
880 java_parse_abort_on_error ();
882 java_parse (); /* Parse and build partial tree nodes. */
883 java_parse_abort_on_error ();
886 /* Process a parsed source file, resolving names etc. */
888 static void
889 parse_source_file_2 ()
891 int save_error_count = java_error_count;
892 java_complete_class (); /* Parse unsatisfied class decl. */
893 java_parse_abort_on_error ();
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 static int
902 predefined_filename_p (node)
903 tree node;
905 int i;
906 for (i = 0; i < PREDEF_FILENAMES_SIZE; i++)
907 if (predef_filenames [i] == node)
908 return 1;
909 return 0;
913 yyparse ()
915 int filename_count = 0;
916 char *list, *next;
917 tree node;
918 FILE *finput = NULL;
920 if (flag_filelist_file)
922 int avail = 2000;
923 finput = fopen (input_filename, "r");
924 if (finput == NULL)
925 fatal_io_error ("can't open %s", input_filename);
926 list = xmalloc(avail);
927 next = list;
928 for (;;)
930 int count;
931 if (avail < 500)
933 count = next - list;
934 avail = 2 * (count + avail);
935 list = xrealloc (list, avail);
936 next = list + count;
937 avail = avail - count;
939 /* Subtract to to guarantee space for final '\0'. */
940 count = fread (next, 1, avail - 1, finput);
941 if (count == 0)
943 if (! feof (finput))
944 fatal_io_error ("error closing %s", input_filename);
945 *next = '\0';
946 break;
948 avail -= count;
949 next += count;
951 fclose (finput);
952 finput = NULL;
954 else
955 list = xstrdup (input_filename);
959 for (next = list; ; )
961 char ch = *next;
962 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
963 || ch == '&' /* FIXME */)
965 if (next == list)
967 next++;
968 list = next;
969 continue;
971 else
973 *next++ = '\0';
974 break;
977 if (ch == '\0')
979 next = NULL;
980 break;
982 next++;
985 if (list[0])
987 char *value;
988 tree id;
989 int twice = 0;
991 int len = strlen (list);
993 if (*list != '/' && filename_count > 0)
994 obstack_grow (&temporary_obstack, "./", 2);
996 obstack_grow0 (&temporary_obstack, list, len);
997 value = obstack_finish (&temporary_obstack);
999 filename_count++;
1001 /* Exclude file that we see twice on the command line. For
1002 all files except {Class,Error,Object,RuntimeException,String,
1003 Throwable}.java we can rely on maybe_get_identifier. For
1004 these files, we need to do a linear search of
1005 current_file_list. This search happens only for these
1006 files, presumably only when we're recompiling libgcj. */
1008 if ((id = maybe_get_identifier (value)))
1010 if (predefined_filename_p (id))
1012 tree c;
1013 for (c = current_file_list; c; c = TREE_CHAIN (c))
1014 if (TREE_VALUE (c) == id)
1015 twice = 1;
1017 else
1018 twice = 1;
1021 if (twice)
1023 const char *saved_input_filename = input_filename;
1024 input_filename = value;
1025 warning ("source file seen twice on command line and will be compiled only once.");
1026 input_filename = saved_input_filename;
1028 else
1030 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
1031 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1032 current_file_list = tree_cons (NULL_TREE, node,
1033 current_file_list);
1036 list = next;
1038 while (next);
1040 if (filename_count == 0)
1041 warning ("no input file specified");
1043 current_jcf = main_jcf;
1044 current_file_list = nreverse (current_file_list);
1045 for (node = current_file_list; node; node = TREE_CHAIN (node))
1047 unsigned char magic_string[4];
1048 uint32 magic;
1049 tree name = TREE_VALUE (node);
1051 /* Skip already parsed files */
1052 if (HAS_BEEN_ALREADY_PARSED_P (name))
1053 continue;
1055 /* Close previous descriptor, if any */
1056 if (finput && fclose (finput))
1057 fatal_io_error ("can't close input file %s", main_input_filename);
1059 finput = fopen (IDENTIFIER_POINTER (name), "rb");
1060 if (finput == NULL)
1061 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
1063 #ifdef IO_BUFFER_SIZE
1064 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
1065 _IOFBF, IO_BUFFER_SIZE);
1066 #endif
1067 input_filename = IDENTIFIER_POINTER (name);
1069 /* Figure what kind of file we're dealing with */
1070 if (fread (magic_string, 1, 4, finput) != 4)
1071 fatal_io_error ("Premature end of input file %s",
1072 IDENTIFIER_POINTER (name));
1073 fseek (finput, 0L, SEEK_SET);
1074 magic = GET_u4 (magic_string);
1075 if (magic == 0xcafebabe)
1077 CLASS_FILE_P (node) = 1;
1078 current_jcf = ALLOC (sizeof (JCF));
1079 JCF_ZERO (current_jcf);
1080 current_jcf->read_state = finput;
1081 current_jcf->filbuf = jcf_filbuf_from_stdio;
1082 jcf_parse (current_jcf);
1083 TYPE_JCF (current_class) = current_jcf;
1084 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1085 TREE_PURPOSE (node) = current_class;
1087 else if (magic == (JCF_u4)ZIPMAGIC)
1089 ZIP_FILE_P (node) = 1;
1090 JCF_ZERO (main_jcf);
1091 main_jcf->read_state = finput;
1092 main_jcf->filbuf = jcf_filbuf_from_stdio;
1093 if (open_in_zip (main_jcf, input_filename, NULL, 0) < 0)
1094 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1095 localToFile = SeenZipFiles;
1096 /* Register all the class defined there. */
1097 process_zip_dir (main_jcf->read_state);
1098 parse_zip_file_entries ();
1100 for (each entry)
1101 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1104 else
1106 JAVA_FILE_P (node) = 1;
1107 java_push_parser_context ();
1108 java_parser_context_save_global ();
1109 parse_source_file_1 (name, finput);
1110 java_parser_context_restore_global ();
1111 java_pop_parser_context (1);
1115 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1117 input_filename = ctxp->filename;
1118 parse_source_file_2 ();
1120 for (node = current_file_list; node; node = TREE_CHAIN (node))
1122 input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1123 if (CLASS_FILE_P (node))
1125 current_class = TREE_PURPOSE (node);
1126 current_jcf = TYPE_JCF (current_class);
1127 layout_class (current_class);
1128 load_inner_classes (current_class);
1129 parse_class_file ();
1130 JCF_FINISH (current_jcf);
1133 input_filename = main_input_filename;
1135 java_expand_classes ();
1136 if (!java_report_errors () && !flag_syntax_only)
1137 emit_register_classes ();
1138 return 0;
1141 /* Process all class entries found in the zip file. */
1142 static void
1143 parse_zip_file_entries (void)
1145 struct ZipDirectory *zdir;
1146 int i;
1148 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1149 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1151 tree class;
1153 /* We don't need to consider those files. */
1154 if (!zdir->size || !zdir->filename_offset)
1155 continue;
1157 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
1158 current_jcf = TYPE_JCF (class);
1159 current_class = class;
1161 if ( !CLASS_LOADED_P (class))
1163 if (! CLASS_PARSED_P (class))
1165 read_zip_member(current_jcf, zdir, localToFile);
1166 jcf_parse (current_jcf);
1168 layout_class (current_class);
1169 load_inner_classes (current_class);
1172 if (TYPE_SIZE (current_class) != error_mark_node)
1174 input_filename = current_jcf->filename;
1175 parse_class_file ();
1176 FREE (current_jcf->buffer); /* No longer necessary */
1177 /* Note: there is a way to free this buffer right after a
1178 class seen in a zip file has been parsed. The idea is the
1179 set its jcf in such a way that buffer will be reallocated
1180 the time the code for the class will be generated. FIXME. */
1185 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1186 jcf up for further processing and link it to the created class. */
1188 static void
1189 process_zip_dir (FILE *finput)
1191 int i;
1192 ZipDirectory *zdir;
1194 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1195 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1197 char *class_name, *file_name, *class_name_in_zip_dir;
1198 tree class;
1199 JCF *jcf;
1200 int j;
1202 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1204 /* We choose to not to process entries with a zero size or entries
1205 not bearing the .class extention. */
1206 if (!zdir->size || !zdir->filename_offset ||
1207 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
1208 ".class", 6))
1210 /* So it will be skipped in parse_zip_file_entries */
1211 zdir->size = 0;
1212 continue;
1215 class_name = ALLOC (zdir->filename_length+1-6);
1216 file_name = ALLOC (zdir->filename_length+1);
1217 jcf = ALLOC (sizeof (JCF));
1218 JCF_ZERO (jcf);
1220 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1221 class_name [zdir->filename_length-6] = '\0';
1222 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1223 file_name [zdir->filename_length] = '\0';
1225 for (j=0; class_name[j]; j++)
1226 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1228 /* Yes, we write back the true class name into the zip directory. */
1229 strcpy (class_name_in_zip_dir, class_name);
1230 zdir->filename_length = j;
1231 class = lookup_class (get_identifier (class_name));
1233 jcf->read_state = finput;
1234 jcf->filbuf = jcf_filbuf_from_stdio;
1235 jcf->java_source = 0;
1236 jcf->classname = class_name;
1237 jcf->filename = file_name;
1238 jcf->zipd = zdir;
1240 TYPE_JCF (class) = jcf;
1244 /* Initialization. */
1246 void
1247 init_jcf_parse ()
1249 /* Register roots with the garbage collector. */
1250 ggc_add_tree_root (parse_roots, sizeof (parse_roots) / sizeof(tree));
1252 ggc_add_root (&current_jcf, 1, sizeof (JCF), (void (*)(void *))ggc_mark_jcf);
1254 init_src_parse ();