Remove spurious warnings of the like '__objc_class_ref_NSObject defined but
[official-gcc.git] / gcc / java / jcf-parse.c
blob2841d5f5f8464a92112ab0ac191d6c9c904e34ef
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_class_file PARAMS ((void));
100 static void set_source_filename PARAMS ((JCF *, int));
101 static int predefined_filename_p PARAMS ((tree));
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 #ifdef REAL_ARITHMETIC
311 d = REAL_VALUE_FROM_TARGET_SINGLE (num);
312 #else
313 union { float f; jint i; } u;
314 u.i = num;
315 d = u.f;
316 #endif
317 value = build_real (float_type_node, d);
318 break;
320 case CONSTANT_Double:
322 HOST_WIDE_INT num[2];
323 REAL_VALUE_TYPE d;
324 HOST_WIDE_INT lo, hi;
325 num[0] = JPOOL_INT (jcf, index);
326 lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
327 num[0] = JPOOL_INT (jcf, index+1);
328 add_double (lo, hi, num[0], 0, &lo, &hi);
329 if (FLOAT_WORDS_BIG_ENDIAN)
331 num[0] = hi;
332 num[1] = lo;
334 else
336 num[0] = lo;
337 num[1] = hi;
339 #ifdef REAL_ARITHMETIC
340 d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
341 #else
343 union { double d; jint i[2]; } u;
344 u.i[0] = (jint) num[0];
345 u.i[1] = (jint) num[1];
346 d = u.d;
348 #endif
349 value = build_real (double_type_node, d);
350 break;
352 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
353 case CONSTANT_String:
355 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
356 const char *utf8_ptr = IDENTIFIER_POINTER (name);
357 int utf8_len = IDENTIFIER_LENGTH (name);
358 unsigned char *str_ptr;
359 unsigned char *str;
360 const unsigned char *utf8;
361 int i, str_len;
363 /* Count the number of Unicode characters in the string,
364 while checking for a malformed Utf8 string. */
365 utf8 = (const unsigned char *) utf8_ptr;
366 i = utf8_len;
367 str_len = 0;
368 while (i > 0)
370 int char_len = UT8_CHAR_LENGTH (*utf8);
371 if (char_len < 0 || char_len > 3 || char_len > i)
372 fatal_error ("bad string constant");
374 utf8 += char_len;
375 i -= char_len;
376 str_len++;
379 /* Allocate a scratch buffer, convert the string to UCS2, and copy it
380 into the new space. */
381 str_ptr = (unsigned char *) alloca (2 * str_len);
382 str = str_ptr;
383 utf8 = (const unsigned char *)utf8_ptr;
385 for (i = 0; i < str_len; i++)
387 int char_value;
388 int char_len = UT8_CHAR_LENGTH (*utf8);
389 switch (char_len)
391 case 1:
392 char_value = *utf8++;
393 break;
394 case 2:
395 char_value = *utf8++ & 0x1F;
396 char_value = (char_value << 6) | (*utf8++ & 0x3F);
397 break;
398 case 3:
399 char_value = *utf8++ & 0x0F;
400 char_value = (char_value << 6) | (*utf8++ & 0x3F);
401 char_value = (char_value << 6) | (*utf8++ & 0x3F);
402 break;
403 default:
404 goto bad;
406 if (BYTES_BIG_ENDIAN)
408 *str++ = char_value >> 8;
409 *str++ = char_value & 0xFF;
411 else
413 *str++ = char_value & 0xFF;
414 *str++ = char_value >> 8;
417 value = build_string (str - str_ptr, str_ptr);
418 TREE_TYPE (value) = build_pointer_type (string_type_node);
420 break;
421 default:
422 goto bad;
424 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
425 jcf->cpool.data [index] = (jword) value;
426 return value;
427 bad:
428 internal_error ("bad value constant type %d, index %d",
429 JPOOL_TAG (jcf, index), index);
432 tree
433 get_name_constant (jcf, index)
434 JCF *jcf;
435 int index;
437 tree name = get_constant (jcf, index);
439 if (TREE_CODE (name) != IDENTIFIER_NODE)
440 abort ();
442 return name;
445 /* Handle reading innerclass attributes. If a non zero entry (denoting
446 a non anonymous entry) is found, We augment the inner class list of
447 the outer context with the newly resolved innerclass. */
449 static void
450 handle_innerclass_attribute (count, jcf)
451 int count;
452 JCF *jcf;
454 int c = (count);
455 while (c--)
457 /* Read inner_class_info_index. This may be 0 */
458 int icii = JCF_readu2 (jcf);
459 /* Read outer_class_info_index. If the innerclasses attribute
460 entry isn't a member (like an inner class) the value is 0. */
461 int ocii = JCF_readu2 (jcf);
462 /* Read inner_name_index. If the class we're dealing with is
463 an annonymous class, it must be 0. */
464 int ini = JCF_readu2 (jcf);
465 /* Read the access flag. */
466 int acc = JCF_readu2 (jcf);
467 /* If icii is 0, don't try to read the class. */
468 if (icii >= 0)
470 tree class = get_class_constant (jcf, icii);
471 tree decl = TYPE_NAME (class);
472 /* Skip reading further if ocii is null */
473 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
475 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
476 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
477 set_class_decl_access_flags (acc, decl);
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;
487 static tree
488 give_name_to_class (jcf, i)
489 JCF *jcf;
490 int i;
492 if (i <= 0 || i >= JPOOL_SIZE (jcf)
493 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
494 abort ();
495 else
497 tree this_class;
498 int j = JPOOL_USHORT1 (jcf, i);
499 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
500 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
501 JPOOL_UTF_LENGTH (jcf, j));
502 this_class = lookup_class (class_name);
503 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
504 lineno = 0;
505 if (main_input_filename == NULL && jcf == main_jcf)
506 main_input_filename = input_filename;
508 jcf->cpool.data[i] = (jword) this_class;
509 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
510 return this_class;
514 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
516 tree
517 get_class_constant (JCF *jcf , int i)
519 tree type;
520 if (i <= 0 || i >= JPOOL_SIZE (jcf)
521 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
522 abort ();
524 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
526 int name_index = JPOOL_USHORT1 (jcf, i);
527 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
528 const char *name = JPOOL_UTF_DATA (jcf, name_index);
529 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
531 if (name[0] == '[') /* Handle array "classes". */
532 type = TREE_TYPE (parse_signature_string (name, nlength));
533 else
535 tree cname = unmangle_classname (name, nlength);
536 type = lookup_class (cname);
538 jcf->cpool.data[i] = (jword) type;
539 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
541 else
542 type = (tree) jcf->cpool.data[i];
543 return type;
546 /* Read a class with the fully qualified-name NAME.
547 Return 1 iff we read the requested file.
548 (It is still possible we failed if the file did not
549 define the class it is supposed to.) */
552 read_class (name)
553 tree name;
555 JCF this_jcf, *jcf;
556 tree icv, class = NULL_TREE;
557 tree save_current_class = current_class;
558 const char *save_input_filename = input_filename;
559 JCF *save_current_jcf = current_jcf;
561 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
563 class = TREE_TYPE (icv);
564 jcf = TYPE_JCF (class);
566 else
567 jcf = NULL;
569 if (jcf == NULL)
571 this_jcf.zipd = NULL;
572 jcf = &this_jcf;
573 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
574 &this_jcf, 1) == 0)
575 return 0;
578 current_jcf = jcf;
580 if (current_jcf->java_source)
582 const char *filename = current_jcf->filename;
583 tree file;
584 FILE *finput;
585 int generate;
587 java_parser_context_save_global ();
588 java_push_parser_context ();
589 BUILD_FILENAME_IDENTIFIER_NODE (file, filename);
590 generate = IS_A_COMMAND_LINE_FILENAME_P (file);
591 if (wfl_operator == NULL_TREE)
592 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
593 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
594 input_filename = ggc_strdup (filename);
595 current_class = NULL_TREE;
596 current_function_decl = NULL_TREE;
597 if (!HAS_BEEN_ALREADY_PARSED_P (file))
599 if (!(finput = fopen (input_filename, "r")))
600 fatal_io_error ("can't reopen %s", input_filename);
601 parse_source_file_1 (file, finput);
602 parse_source_file_2 ();
603 if (fclose (finput))
604 fatal_io_error ("can't close %s", input_filename);
606 JCF_FINISH (current_jcf);
607 java_pop_parser_context (generate);
608 java_parser_context_restore_global ();
610 else
612 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
614 java_parser_context_save_global ();
615 java_push_parser_context ();
616 current_class = class;
617 input_filename = current_jcf->filename;
618 if (JCF_SEEN_IN_ZIP (current_jcf))
619 read_zip_member(current_jcf,
620 current_jcf->zipd, current_jcf->zipd->zipf);
621 jcf_parse (current_jcf);
622 class = current_class;
623 java_pop_parser_context (0);
624 java_parser_context_restore_global ();
626 layout_class (class);
627 load_inner_classes (class);
630 current_class = save_current_class;
631 input_filename = save_input_filename;
632 current_jcf = save_current_jcf;
633 return 1;
636 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
637 called from the parser, otherwise it's a RECORD_TYPE node. If
638 VERBOSE is 1, print error message on failure to load a class. */
640 /* Replace calls to load_class by having callers call read_class directly
641 - and then perhaps rename read_class to load_class. FIXME */
643 void
644 load_class (class_or_name, verbose)
645 tree class_or_name;
646 int verbose;
648 tree name, saved;
649 int class_loaded;
651 /* class_or_name can be the name of the class we want to load */
652 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
653 name = class_or_name;
654 /* In some cases, it's a dependency that we process earlier that
655 we though */
656 else if (TREE_CODE (class_or_name) == TREE_LIST)
657 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
658 /* Or it's a type in the making */
659 else
660 name = DECL_NAME (TYPE_NAME (class_or_name));
662 saved = name;
663 while (1)
665 char *dollar;
667 if ((class_loaded = read_class (name)))
668 break;
670 /* We failed loading name. Now consider that we might be looking
671 for a inner class but it's only available in source for in
672 its enclosing context. */
673 if ((dollar = strrchr (IDENTIFIER_POINTER (name), '$')))
675 int c = *dollar;
676 *dollar = '\0';
677 name = get_identifier (IDENTIFIER_POINTER (name));
678 *dollar = c;
680 /* Otherwise, we failed, we bail. */
681 else
682 break;
685 if (!class_loaded && verbose)
686 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
689 /* Parse the .class file JCF. */
691 void
692 jcf_parse (jcf)
693 JCF* jcf;
695 int i, code;
697 if (jcf_parse_preamble (jcf) != 0)
698 fatal_error ("not a valid Java .class file");
699 code = jcf_parse_constant_pool (jcf);
700 if (code != 0)
701 fatal_error ("error while parsing constant pool");
702 code = verify_constant_pool (jcf);
703 if (code > 0)
704 fatal_error ("error in constant pool entry #%d\n", code);
706 jcf_parse_class (jcf);
707 if (main_class == NULL_TREE)
708 main_class = current_class;
709 if (! quiet_flag && TYPE_NAME (current_class))
710 fprintf (stderr, " %s %s",
711 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
712 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
713 if (CLASS_PARSED_P (current_class))
715 /* FIXME - where was first time */
716 fatal_error ("reading class %s for the second time from %s",
717 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
718 jcf->filename);
720 CLASS_PARSED_P (current_class) = 1;
722 for (i = 1; i < JPOOL_SIZE(jcf); i++)
724 switch (JPOOL_TAG (jcf, i))
726 case CONSTANT_Class:
727 get_class_constant (jcf, i);
728 break;
732 code = jcf_parse_fields (jcf);
733 if (code != 0)
734 fatal_error ("error while parsing fields");
735 code = jcf_parse_methods (jcf);
736 if (code != 0)
737 fatal_error ("error while parsing methods");
738 code = jcf_parse_final_attributes (jcf);
739 if (code != 0)
740 fatal_error ("error while parsing final attributes");
742 /* The fields of class_type_node are already in correct order. */
743 if (current_class != class_type_node && current_class != object_type_node)
744 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
746 if (current_class == object_type_node)
748 layout_class_methods (object_type_node);
749 /* If we don't have the right archive, emit a verbose warning.
750 If we're generating bytecode, emit the warning only if
751 -fforce-classes-archive-check was specified. */
752 if (!jcf->right_zip
753 && (!flag_emit_class_files || flag_force_classes_archive_check))
754 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);
756 else
757 all_class_list = tree_cons (NULL_TREE,
758 TYPE_NAME (current_class), all_class_list );
761 /* If we came across inner classes, load them now. */
762 static void
763 load_inner_classes (cur_class)
764 tree cur_class;
766 tree current;
767 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
768 current = TREE_CHAIN (current))
770 tree name = DECL_NAME (TREE_PURPOSE (current));
771 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
772 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
773 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
774 load_class (name, 1);
778 void
779 init_outgoing_cpool ()
781 current_constant_pool_data_ref = NULL_TREE;
782 outgoing_cpool = (struct CPool *)xmalloc (sizeof (struct CPool));
783 memset (outgoing_cpool, 0, sizeof (struct CPool));
786 static void
787 parse_class_file ()
789 tree method;
790 const char *save_input_filename = input_filename;
791 int save_lineno = lineno;
793 java_layout_seen_class_methods ();
795 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
796 lineno = 0;
797 (*debug_hooks->start_source_file) (lineno, input_filename);
798 init_outgoing_cpool ();
800 /* Currently we always have to emit calls to _Jv_InitClass when
801 compiling from class files. */
802 always_initialize_class_p = 1;
804 for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
805 method != NULL_TREE; method = TREE_CHAIN (method))
807 JCF *jcf = current_jcf;
809 if (METHOD_ABSTRACT (method))
810 continue;
812 if (METHOD_NATIVE (method))
814 tree arg;
815 int decl_max_locals;
817 if (! flag_jni)
818 continue;
819 /* We need to compute the DECL_MAX_LOCALS. We need to take
820 the wide types into account too. */
821 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
822 arg != end_params_node;
823 arg = TREE_CHAIN (arg), decl_max_locals += 1)
825 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
826 decl_max_locals += 1;
828 DECL_MAX_LOCALS (method) = decl_max_locals;
829 start_java_method (method);
830 give_name_to_locals (jcf);
831 expand_expr_stmt (build_jni_stub (method));
832 end_java_method ();
833 continue;
836 if (DECL_CODE_OFFSET (method) == 0)
838 current_function_decl = method;
839 error ("missing Code attribute");
840 continue;
843 lineno = 0;
844 if (DECL_LINENUMBERS_OFFSET (method))
846 register int i;
847 register unsigned char *ptr;
848 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
849 linenumber_count = i = JCF_readu2 (jcf);
850 linenumber_table = ptr = jcf->read_ptr;
852 for (ptr += 2; --i >= 0; ptr += 4)
854 int line = GET_u2 (ptr);
855 /* Set initial lineno lineno to smallest linenumber.
856 * Needs to be set before init_function_start. */
857 if (lineno == 0 || line < lineno)
858 lineno = line;
861 else
863 linenumber_table = NULL;
864 linenumber_count = 0;
867 start_java_method (method);
869 note_instructions (jcf, method);
871 give_name_to_locals (jcf);
873 /* Actually generate code. */
874 expand_byte_code (jcf, method);
876 end_java_method ();
879 if (flag_emit_class_files)
880 write_classfile (current_class);
882 finish_class ();
884 (*debug_hooks->end_source_file) (save_lineno);
885 input_filename = save_input_filename;
886 lineno = save_lineno;
889 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
891 static void
892 parse_source_file_1 (file, finput)
893 tree file;
894 FILE *finput;
896 int save_error_count = java_error_count;
897 /* Mark the file as parsed */
898 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
900 jcf_dependency_add_file (input_filename, 0);
902 lang_init_source (1); /* Error msgs have no method prototypes */
904 /* There's no point in trying to find the current encoding unless we
905 are going to do something intelligent with it -- hence the test
906 for iconv. */
907 #ifdef HAVE_ICONV
908 #ifdef HAVE_NL_LANGINFO
909 setlocale (LC_CTYPE, "");
910 if (current_encoding == NULL)
911 current_encoding = nl_langinfo (CODESET);
912 #endif /* HAVE_NL_LANGINFO */
913 #endif /* HAVE_ICONV */
914 if (current_encoding == NULL || *current_encoding == '\0')
915 current_encoding = DEFAULT_ENCODING;
917 /* Initialize the parser */
918 java_init_lex (finput, current_encoding);
919 java_parse_abort_on_error ();
921 java_parse (); /* Parse and build partial tree nodes. */
922 java_parse_abort_on_error ();
925 /* Process a parsed source file, resolving names etc. */
927 static void
928 parse_source_file_2 ()
930 int save_error_count = java_error_count;
931 java_complete_class (); /* Parse unsatisfied class decl. */
932 java_parse_abort_on_error ();
933 java_check_circular_reference (); /* Check on circular references */
934 java_parse_abort_on_error ();
935 java_fix_constructors (); /* Fix the constructors */
936 java_parse_abort_on_error ();
937 java_reorder_fields (); /* Reorder the fields */
940 static int
941 predefined_filename_p (node)
942 tree node;
944 int i;
945 for (i = 0; i < PREDEF_FILENAMES_SIZE; i++)
946 if (predef_filenames [i] == node)
947 return 1;
948 return 0;
952 yyparse ()
954 int filename_count = 0;
955 char *list, *next;
956 tree node;
957 FILE *finput = NULL;
959 if (flag_filelist_file)
961 int avail = 2000;
962 finput = fopen (input_filename, "r");
963 if (finput == NULL)
964 fatal_io_error ("can't open %s", input_filename);
965 list = xmalloc(avail);
966 next = list;
967 for (;;)
969 int count;
970 if (avail < 500)
972 count = next - list;
973 avail = 2 * (count + avail);
974 list = xrealloc (list, avail);
975 next = list + count;
976 avail = avail - count;
978 /* Subtract to to guarantee space for final '\0'. */
979 count = fread (next, 1, avail - 1, finput);
980 if (count == 0)
982 if (! feof (finput))
983 fatal_io_error ("error closing %s", input_filename);
984 *next = '\0';
985 break;
987 avail -= count;
988 next += count;
990 fclose (finput);
991 finput = NULL;
993 else
994 list = xstrdup (input_filename);
998 for (next = list; ; )
1000 char ch = *next;
1001 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1002 || ch == '&' /* FIXME */)
1004 if (next == list)
1006 next++;
1007 list = next;
1008 continue;
1010 else
1012 *next++ = '\0';
1013 break;
1016 if (ch == '\0')
1018 next = NULL;
1019 break;
1021 next++;
1024 if (list[0])
1026 char *value;
1027 tree id;
1028 int twice = 0;
1030 int len = strlen (list);
1032 if (*list != '/' && filename_count > 0)
1033 obstack_grow (&temporary_obstack, "./", 2);
1035 obstack_grow0 (&temporary_obstack, list, len);
1036 value = obstack_finish (&temporary_obstack);
1038 filename_count++;
1040 /* Exclude file that we see twice on the command line. For
1041 all files except {Class,Error,Object,RuntimeException,String,
1042 Throwable}.java we can rely on maybe_get_identifier. For
1043 these files, we need to do a linear search of
1044 current_file_list. This search happens only for these
1045 files, presumably only when we're recompiling libgcj. */
1047 if ((id = maybe_get_identifier (value)))
1049 if (predefined_filename_p (id))
1051 tree c;
1052 for (c = current_file_list; c; c = TREE_CHAIN (c))
1053 if (TREE_VALUE (c) == id)
1054 twice = 1;
1056 else
1057 twice = 1;
1060 if (twice)
1062 const char *saved_input_filename = input_filename;
1063 input_filename = value;
1064 warning ("source file seen twice on command line and will be compiled only once");
1065 input_filename = saved_input_filename;
1067 else
1069 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
1070 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1071 current_file_list = tree_cons (NULL_TREE, node,
1072 current_file_list);
1075 list = next;
1077 while (next);
1079 if (filename_count == 0)
1080 warning ("no input file specified");
1082 if (resource_name)
1084 const char *resource_filename;
1086 /* Only one resource file may be compiled at a time. */
1087 assert (TREE_CHAIN (current_file_list) == NULL);
1089 resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
1090 compile_resource_file (resource_name, resource_filename);
1092 java_expand_classes ();
1093 if (!java_report_errors ())
1094 emit_register_classes ();
1095 return 0;
1098 current_jcf = main_jcf;
1099 current_file_list = nreverse (current_file_list);
1100 for (node = current_file_list; node; node = TREE_CHAIN (node))
1102 unsigned char magic_string[4];
1103 uint32 magic = 0;
1104 tree name = TREE_VALUE (node);
1106 /* Skip already parsed files */
1107 if (HAS_BEEN_ALREADY_PARSED_P (name))
1108 continue;
1110 /* Close previous descriptor, if any */
1111 if (finput && fclose (finput))
1112 fatal_io_error ("can't close input file %s", main_input_filename);
1114 finput = fopen (IDENTIFIER_POINTER (name), "rb");
1115 if (finput == NULL)
1116 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
1118 #ifdef IO_BUFFER_SIZE
1119 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
1120 _IOFBF, IO_BUFFER_SIZE);
1121 #endif
1122 input_filename = IDENTIFIER_POINTER (name);
1124 /* Figure what kind of file we're dealing with */
1125 if (fread (magic_string, 1, 4, finput) == 4)
1127 fseek (finput, 0L, SEEK_SET);
1128 magic = GET_u4 (magic_string);
1130 if (magic == 0xcafebabe)
1132 CLASS_FILE_P (node) = 1;
1133 current_jcf = ALLOC (sizeof (JCF));
1134 JCF_ZERO (current_jcf);
1135 current_jcf->read_state = finput;
1136 current_jcf->filbuf = jcf_filbuf_from_stdio;
1137 jcf_parse (current_jcf);
1138 TYPE_JCF (current_class) = current_jcf;
1139 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1140 TREE_PURPOSE (node) = current_class;
1142 else if (magic == (JCF_u4)ZIPMAGIC)
1144 ZIP_FILE_P (node) = 1;
1145 JCF_ZERO (main_jcf);
1146 main_jcf->read_state = finput;
1147 main_jcf->filbuf = jcf_filbuf_from_stdio;
1148 if (open_in_zip (main_jcf, input_filename, NULL, 0) < 0)
1149 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1150 localToFile = SeenZipFiles;
1151 /* Register all the class defined there. */
1152 process_zip_dir (main_jcf->read_state);
1153 parse_zip_file_entries ();
1155 for (each entry)
1156 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1159 else
1161 JAVA_FILE_P (node) = 1;
1162 java_push_parser_context ();
1163 java_parser_context_save_global ();
1164 parse_source_file_1 (name, finput);
1165 java_parser_context_restore_global ();
1166 java_pop_parser_context (1);
1170 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1172 input_filename = ctxp->filename;
1173 parse_source_file_2 ();
1175 for (node = current_file_list; node; node = TREE_CHAIN (node))
1177 input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1178 if (CLASS_FILE_P (node))
1180 current_class = TREE_PURPOSE (node);
1181 current_jcf = TYPE_JCF (current_class);
1182 layout_class (current_class);
1183 load_inner_classes (current_class);
1184 parse_class_file ();
1185 JCF_FINISH (current_jcf);
1188 input_filename = main_input_filename;
1190 java_expand_classes ();
1191 if (!java_report_errors () && !flag_syntax_only)
1193 emit_register_classes ();
1194 if (flag_indirect_dispatch)
1195 emit_offset_symbol_table ();
1197 return 0;
1200 /* Process all class entries found in the zip file. */
1201 static void
1202 parse_zip_file_entries (void)
1204 struct ZipDirectory *zdir;
1205 int i;
1207 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1208 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1210 tree class;
1212 /* We don't need to consider those files. */
1213 if (!zdir->size || !zdir->filename_offset)
1214 continue;
1216 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
1217 current_jcf = TYPE_JCF (class);
1218 current_class = class;
1220 if ( !CLASS_LOADED_P (class))
1222 if (! CLASS_PARSED_P (class))
1224 read_zip_member(current_jcf, zdir, localToFile);
1225 jcf_parse (current_jcf);
1227 layout_class (current_class);
1228 load_inner_classes (current_class);
1231 if (TYPE_SIZE (current_class) != error_mark_node)
1233 input_filename = current_jcf->filename;
1234 parse_class_file ();
1235 FREE (current_jcf->buffer); /* No longer necessary */
1236 /* Note: there is a way to free this buffer right after a
1237 class seen in a zip file has been parsed. The idea is the
1238 set its jcf in such a way that buffer will be reallocated
1239 the time the code for the class will be generated. FIXME. */
1244 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1245 jcf up for further processing and link it to the created class. */
1247 static void
1248 process_zip_dir (FILE *finput)
1250 int i;
1251 ZipDirectory *zdir;
1253 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1254 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1256 char *class_name, *file_name, *class_name_in_zip_dir;
1257 tree class;
1258 JCF *jcf;
1259 int j;
1261 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1263 /* We choose to not to process entries with a zero size or entries
1264 not bearing the .class extension. */
1265 if (!zdir->size || !zdir->filename_offset ||
1266 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
1267 ".class", 6))
1269 /* So it will be skipped in parse_zip_file_entries */
1270 zdir->size = 0;
1271 continue;
1274 class_name = ALLOC (zdir->filename_length+1-6);
1275 file_name = ALLOC (zdir->filename_length+1);
1276 jcf = ALLOC (sizeof (JCF));
1277 JCF_ZERO (jcf);
1279 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1280 class_name [zdir->filename_length-6] = '\0';
1281 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1282 file_name [zdir->filename_length] = '\0';
1284 for (j=0; class_name[j]; j++)
1285 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1287 /* Yes, we write back the true class name into the zip directory. */
1288 strcpy (class_name_in_zip_dir, class_name);
1289 zdir->filename_length = j;
1290 class = lookup_class (get_identifier (class_name));
1292 jcf->read_state = finput;
1293 jcf->filbuf = jcf_filbuf_from_stdio;
1294 jcf->java_source = 0;
1295 jcf->classname = class_name;
1296 jcf->filename = file_name;
1297 jcf->zipd = zdir;
1299 TYPE_JCF (class) = jcf;
1303 /* Initialization. */
1305 void
1306 init_jcf_parse ()
1308 /* Register roots with the garbage collector. */
1309 ggc_add_tree_root (parse_roots, sizeof (parse_roots) / sizeof(tree));
1311 ggc_add_root (&current_jcf, 1, sizeof (JCF), (void (*)(void *))ggc_mark_jcf);
1313 init_src_parse ();