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)
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> */
32 #include "java-except.h"
34 #include "java-tree.h"
43 #ifdef HAVE_NL_LANGINFO
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) \
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]; \
59 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
60 text[LENGTH] = save; \
61 JCF_SKIP (JCF, LENGTH); } while (0)
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. */
110 JCF
*jcf
= *(JCF
**) elt
;
113 CPool
*cpool
= &jcf
->cpool
;
114 int size
= CPOOL_COUNT(cpool
);
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. */
128 set_source_filename (jcf
, 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] == '\\'))
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); \
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) \
203 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
204 JCF_SKIP (jcf, n * 10); }
206 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
208 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
209 JCF_SKIP (jcf, n * 4); }
211 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
214 tree list = DECL_FUNCTION_THROWS (current_method); \
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"
249 parse_signature (jcf
, sig_index
)
253 if (sig_index
<= 0 || sig_index
>= JPOOL_SIZE (jcf
)
254 || JPOOL_TAG (jcf
, sig_index
) != CONSTANT_Utf8
)
257 return parse_signature_string (JPOOL_UTF_DATA (jcf
, sig_index
),
258 JPOOL_UTF_LENGTH (jcf
, sig_index
));
264 /* Make identifier nodes long enough for the language-specific slots. */
265 set_identifier_size (sizeof (struct lang_identifier
));
276 get_constant (jcf
, index
)
282 if (index
<= 0 || index
>= JPOOL_SIZE(jcf
))
284 tag
= JPOOL_TAG (jcf
, index
);
285 if ((tag
& CONSTANT_ResolvedFlag
) || tag
== CONSTANT_Utf8
)
286 return (tree
) jcf
->cpool
.data
[index
];
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
;
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);
308 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
311 jint num
= JPOOL_INT(jcf
, index
);
313 #ifdef REAL_ARITHMETIC
314 d
= REAL_VALUE_FROM_TARGET_SINGLE (num
);
316 union { float f
; jint i
; } u
;
320 value
= build_real (float_type_node
, d
);
323 case CONSTANT_Double
:
325 HOST_WIDE_INT num
[2];
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
)
342 #ifdef REAL_ARITHMETIC
343 d
= REAL_VALUE_FROM_TARGET_DOUBLE (num
);
346 union { double d
; jint i
[2]; } u
;
347 u
.i
[0] = (jint
) num
[0];
348 u
.i
[1] = (jint
) num
[1];
352 value
= build_real (double_type_node
, d
);
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
;
363 const unsigned char *utf8
;
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
;
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");
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
);
386 utf8
= (const unsigned char *)utf8_ptr
;
388 for (i
= 0; i
< str_len
; i
++)
391 int char_len
= UT8_CHAR_LENGTH (*utf8
);
395 char_value
= *utf8
++;
398 char_value
= *utf8
++ & 0x1F;
399 char_value
= (char_value
<< 6) | (*utf8
++ & 0x3F);
402 char_value
= *utf8
++ & 0x0F;
403 char_value
= (char_value
<< 6) | (*utf8
++ & 0x3F);
404 char_value
= (char_value
<< 6) | (*utf8
++ & 0x3F);
409 if (BYTES_BIG_ENDIAN
)
411 *str
++ = char_value
>> 8;
412 *str
++ = char_value
& 0xFF;
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
);
427 JPOOL_TAG (jcf
, index
) = tag
| CONSTANT_ResolvedFlag
;
428 jcf
->cpool
.data
[index
] = (jword
) value
;
431 internal_error ("bad value constant type %d, index %d",
432 JPOOL_TAG (jcf
, index
), index
);
436 get_name_constant (jcf
, index
)
440 tree name
= get_constant (jcf
, index
);
442 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
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. */
453 handle_innerclass_attribute (count
, jcf
)
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 /* Read the access flag. */
469 int acc
= JCF_readu2 (jcf
);
470 /* If icii is 0, don't try to read the class. */
473 tree
class = get_class_constant (jcf
, icii
);
474 tree decl
= TYPE_NAME (class);
475 /* Skip reading further if ocii is null */
476 if (DECL_P (decl
) && !CLASS_COMPLETE_P (decl
) && ocii
)
478 tree outer
= TYPE_NAME (get_class_constant (jcf
, ocii
));
479 tree alias
= (ini
? get_name_constant (jcf
, ini
) : NULL_TREE
);
480 set_class_decl_access_flags (acc
, decl
);
481 DECL_CONTEXT (decl
) = outer
;
482 DECL_INNER_CLASS_LIST (outer
) =
483 tree_cons (decl
, alias
, DECL_INNER_CLASS_LIST (outer
));
484 CLASS_COMPLETE_P (decl
) = 1;
491 give_name_to_class (jcf
, i
)
495 if (i
<= 0 || i
>= JPOOL_SIZE (jcf
)
496 || JPOOL_TAG (jcf
, i
) != CONSTANT_Class
)
501 int j
= JPOOL_USHORT1 (jcf
, i
);
502 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
503 tree class_name
= unmangle_classname (JPOOL_UTF_DATA (jcf
, j
),
504 JPOOL_UTF_LENGTH (jcf
, j
));
505 this_class
= lookup_class (class_name
);
506 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (this_class
));
508 if (main_input_filename
== NULL
&& jcf
== main_jcf
)
509 main_input_filename
= input_filename
;
511 jcf
->cpool
.data
[i
] = (jword
) this_class
;
512 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
517 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
520 get_class_constant (JCF
*jcf
, int i
)
523 if (i
<= 0 || i
>= JPOOL_SIZE (jcf
)
524 || (JPOOL_TAG (jcf
, i
) & ~CONSTANT_ResolvedFlag
) != CONSTANT_Class
)
527 if (JPOOL_TAG (jcf
, i
) != CONSTANT_ResolvedClass
)
529 int name_index
= JPOOL_USHORT1 (jcf
, i
);
530 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
531 const char *name
= JPOOL_UTF_DATA (jcf
, name_index
);
532 int nlength
= JPOOL_UTF_LENGTH (jcf
, name_index
);
534 if (name
[0] == '[') /* Handle array "classes". */
535 type
= TREE_TYPE (parse_signature_string (name
, nlength
));
538 tree cname
= unmangle_classname (name
, nlength
);
539 type
= lookup_class (cname
);
541 jcf
->cpool
.data
[i
] = (jword
) type
;
542 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
545 type
= (tree
) jcf
->cpool
.data
[i
];
549 /* Read a class with the fully qualified-name NAME.
550 Return 1 iff we read the requested file.
551 (It is still possible we failed if the file did not
552 define the class it is supposed to.) */
559 tree icv
, class = NULL_TREE
;
560 tree save_current_class
= current_class
;
561 const char *save_input_filename
= input_filename
;
562 JCF
*save_current_jcf
= current_jcf
;
564 if ((icv
= IDENTIFIER_CLASS_VALUE (name
)) != NULL_TREE
)
566 class = TREE_TYPE (icv
);
567 jcf
= TYPE_JCF (class);
574 this_jcf
.zipd
= NULL
;
576 if (find_class (IDENTIFIER_POINTER (name
), IDENTIFIER_LENGTH (name
),
583 if (current_jcf
->java_source
)
585 const char *filename
= current_jcf
->filename
;
590 java_parser_context_save_global ();
591 java_push_parser_context ();
592 BUILD_FILENAME_IDENTIFIER_NODE (file
, filename
);
593 generate
= IS_A_COMMAND_LINE_FILENAME_P (file
);
594 if (wfl_operator
== NULL_TREE
)
595 wfl_operator
= build_expr_wfl (NULL_TREE
, NULL
, 0, 0);
596 EXPR_WFL_FILENAME_NODE (wfl_operator
) = file
;
597 input_filename
= ggc_strdup (filename
);
598 current_class
= NULL_TREE
;
599 current_function_decl
= NULL_TREE
;
600 if (!HAS_BEEN_ALREADY_PARSED_P (file
))
602 if (!(finput
= fopen (input_filename
, "r")))
603 fatal_io_error ("can't reopen %s", input_filename
);
604 parse_source_file_1 (file
, finput
);
605 parse_source_file_2 ();
607 fatal_io_error ("can't close %s", input_filename
);
609 JCF_FINISH (current_jcf
);
610 java_pop_parser_context (generate
);
611 java_parser_context_restore_global ();
615 if (class == NULL_TREE
|| ! CLASS_PARSED_P (class))
617 java_parser_context_save_global ();
618 java_push_parser_context ();
619 current_class
= class;
620 input_filename
= current_jcf
->filename
;
621 if (JCF_SEEN_IN_ZIP (current_jcf
))
622 read_zip_member(current_jcf
,
623 current_jcf
->zipd
, current_jcf
->zipd
->zipf
);
624 jcf_parse (current_jcf
);
625 class = current_class
;
626 java_pop_parser_context (0);
627 java_parser_context_restore_global ();
629 layout_class (class);
630 load_inner_classes (class);
633 current_class
= save_current_class
;
634 input_filename
= save_input_filename
;
635 current_jcf
= save_current_jcf
;
639 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
640 called from the parser, otherwise it's a RECORD_TYPE node. If
641 VERBOSE is 1, print error message on failure to load a class. */
643 /* Replace calls to load_class by having callers call read_class directly
644 - and then perhaps rename read_class to load_class. FIXME */
647 load_class (class_or_name
, verbose
)
653 /* class_or_name can be the name of the class we want to load */
654 if (TREE_CODE (class_or_name
) == IDENTIFIER_NODE
)
655 name
= class_or_name
;
656 /* In some cases, it's a dependency that we process earlier that
658 else if (TREE_CODE (class_or_name
) == TREE_LIST
)
659 name
= TYPE_NAME (TREE_PURPOSE (class_or_name
));
660 /* Or it's a type in the making */
662 name
= DECL_NAME (TYPE_NAME (class_or_name
));
664 if (read_class (name
) == 0 && verbose
)
665 error ("Cannot find file for class %s.", IDENTIFIER_POINTER (name
));
668 /* Parse the .class file JCF. */
676 if (jcf_parse_preamble (jcf
) != 0)
677 fatal_error ("not a valid Java .class file");
678 code
= jcf_parse_constant_pool (jcf
);
680 fatal_error ("error while parsing constant pool");
681 code
= verify_constant_pool (jcf
);
683 fatal_error ("error in constant pool entry #%d\n", code
);
685 jcf_parse_class (jcf
);
686 if (main_class
== NULL_TREE
)
687 main_class
= current_class
;
688 if (! quiet_flag
&& TYPE_NAME (current_class
))
689 fprintf (stderr
, " %s %s",
690 (jcf
->access_flags
& ACC_INTERFACE
) ? "interface" : "class",
691 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class
))));
692 if (CLASS_PARSED_P (current_class
))
694 /* FIXME - where was first time */
695 fatal_error ("reading class %s for the second time from %s",
696 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class
))),
699 CLASS_PARSED_P (current_class
) = 1;
701 for (i
= 1; i
< JPOOL_SIZE(jcf
); i
++)
703 switch (JPOOL_TAG (jcf
, i
))
706 get_class_constant (jcf
, i
);
711 code
= jcf_parse_fields (jcf
);
713 fatal_error ("error while parsing fields");
714 code
= jcf_parse_methods (jcf
);
716 fatal_error ("error while parsing methods");
717 code
= jcf_parse_final_attributes (jcf
);
719 fatal_error ("error while parsing final attributes");
721 /* The fields of class_type_node are already in correct order. */
722 if (current_class
!= class_type_node
&& current_class
!= object_type_node
)
723 TYPE_FIELDS (current_class
) = nreverse (TYPE_FIELDS (current_class
));
725 if (current_class
== object_type_node
)
727 layout_class_methods (object_type_node
);
728 /* If we don't have the right archive, emit a verbose warning.
729 If we're generating bytecode, emit the warning only if
730 -fforce-classes-archive-check was specified. */
732 && (!flag_emit_class_files
|| flag_force_classes_archive_check
))
733 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
);
736 all_class_list
= tree_cons (NULL_TREE
,
737 TYPE_NAME (current_class
), all_class_list
);
740 /* If we came across inner classes, load them now. */
742 load_inner_classes (cur_class
)
746 for (current
= DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class
)); current
;
747 current
= TREE_CHAIN (current
))
749 tree name
= DECL_NAME (TREE_PURPOSE (current
));
750 tree decl
= IDENTIFIER_GLOBAL_VALUE (name
);
751 if (decl
&& ! CLASS_LOADED_P (TREE_TYPE (decl
))
752 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl
)))
753 load_class (name
, 1);
758 init_outgoing_cpool ()
760 current_constant_pool_data_ref
= NULL_TREE
;
761 outgoing_cpool
= (struct CPool
*)xmalloc (sizeof (struct CPool
));
762 memset (outgoing_cpool
, 0, sizeof (struct CPool
));
769 const char *save_input_filename
= input_filename
;
770 int save_lineno
= lineno
;
772 java_layout_seen_class_methods ();
774 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (current_class
));
776 debug_start_source_file (lineno
, input_filename
);
777 init_outgoing_cpool ();
779 /* Currently we always have to emit calls to _Jv_InitClass when
780 compiling from class files. */
781 always_initialize_class_p
= 1;
783 for ( method
= TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class
));
784 method
!= NULL_TREE
; method
= TREE_CHAIN (method
))
786 JCF
*jcf
= current_jcf
;
788 if (METHOD_ABSTRACT (method
))
791 if (METHOD_NATIVE (method
))
798 /* We need to compute the DECL_MAX_LOCALS. We need to take
799 the wide types into account too. */
800 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (method
)), decl_max_locals
= 0;
801 arg
!= end_params_node
;
802 arg
= TREE_CHAIN (arg
), decl_max_locals
+= 1)
804 if (TREE_VALUE (arg
) && TYPE_IS_WIDE (TREE_VALUE (arg
)))
805 decl_max_locals
+= 1;
807 DECL_MAX_LOCALS (method
) = decl_max_locals
;
808 start_java_method (method
);
809 give_name_to_locals (jcf
);
810 expand_expr_stmt (build_jni_stub (method
));
815 if (DECL_CODE_OFFSET (method
) == 0)
817 error ("missing Code attribute");
822 if (DECL_LINENUMBERS_OFFSET (method
))
825 register unsigned char *ptr
;
826 JCF_SEEK (jcf
, DECL_LINENUMBERS_OFFSET (method
));
827 linenumber_count
= i
= JCF_readu2 (jcf
);
828 linenumber_table
= ptr
= jcf
->read_ptr
;
830 for (ptr
+= 2; --i
>= 0; ptr
+= 4)
832 int line
= GET_u2 (ptr
);
833 /* Set initial lineno lineno to smallest linenumber.
834 * Needs to be set before init_function_start. */
835 if (lineno
== 0 || line
< lineno
)
841 linenumber_table
= NULL
;
842 linenumber_count
= 0;
845 start_java_method (method
);
847 note_instructions (jcf
, method
);
849 give_name_to_locals (jcf
);
851 /* Actually generate code. */
852 expand_byte_code (jcf
, method
);
857 if (flag_emit_class_files
)
858 write_classfile (current_class
);
862 debug_end_source_file (save_lineno
);
863 input_filename
= save_input_filename
;
864 lineno
= save_lineno
;
867 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
870 parse_source_file_1 (file
, finput
)
874 int save_error_count
= java_error_count
;
875 /* Mark the file as parsed */
876 HAS_BEEN_ALREADY_PARSED_P (file
) = 1;
878 jcf_dependency_add_file (input_filename
, 0);
880 lang_init_source (1); /* Error msgs have no method prototypes */
882 /* There's no point in trying to find the current encoding unless we
883 are going to do something intelligent with it -- hence the test
886 #ifdef HAVE_NL_LANGINFO
887 setlocale (LC_CTYPE
, "");
888 if (current_encoding
== NULL
)
889 current_encoding
= nl_langinfo (CODESET
);
890 #endif /* HAVE_NL_LANGINFO */
891 #endif /* HAVE_ICONV */
892 if (current_encoding
== NULL
|| *current_encoding
== '\0')
893 current_encoding
= DEFAULT_ENCODING
;
895 /* Initialize the parser */
896 java_init_lex (finput
, current_encoding
);
897 java_parse_abort_on_error ();
899 java_parse (); /* Parse and build partial tree nodes. */
900 java_parse_abort_on_error ();
903 /* Process a parsed source file, resolving names etc. */
906 parse_source_file_2 ()
908 int save_error_count
= java_error_count
;
909 java_complete_class (); /* Parse unsatisfied class decl. */
910 java_parse_abort_on_error ();
911 java_check_circular_reference (); /* Check on circular references */
912 java_parse_abort_on_error ();
913 java_fix_constructors (); /* Fix the constructors */
914 java_parse_abort_on_error ();
915 java_reorder_fields (); /* Reorder the fields */
919 predefined_filename_p (node
)
923 for (i
= 0; i
< PREDEF_FILENAMES_SIZE
; i
++)
924 if (predef_filenames
[i
] == node
)
932 int filename_count
= 0;
937 if (flag_filelist_file
)
940 finput
= fopen (input_filename
, "r");
942 fatal_io_error ("can't open %s", input_filename
);
943 list
= xmalloc(avail
);
951 avail
= 2 * (count
+ avail
);
952 list
= xrealloc (list
, avail
);
954 avail
= avail
- count
;
956 /* Subtract to to guarantee space for final '\0'. */
957 count
= fread (next
, 1, avail
- 1, finput
);
961 fatal_io_error ("error closing %s", input_filename
);
972 list
= xstrdup (input_filename
);
976 for (next
= list
; ; )
979 if (ch
== '\n' || ch
== '\r' || ch
== '\t' || ch
== ' '
980 || ch
== '&' /* FIXME */)
1008 int len
= strlen (list
);
1010 if (*list
!= '/' && filename_count
> 0)
1011 obstack_grow (&temporary_obstack
, "./", 2);
1013 obstack_grow0 (&temporary_obstack
, list
, len
);
1014 value
= obstack_finish (&temporary_obstack
);
1018 /* Exclude file that we see twice on the command line. For
1019 all files except {Class,Error,Object,RuntimeException,String,
1020 Throwable}.java we can rely on maybe_get_identifier. For
1021 these files, we need to do a linear search of
1022 current_file_list. This search happens only for these
1023 files, presumably only when we're recompiling libgcj. */
1025 if ((id
= maybe_get_identifier (value
)))
1027 if (predefined_filename_p (id
))
1030 for (c
= current_file_list
; c
; c
= TREE_CHAIN (c
))
1031 if (TREE_VALUE (c
) == id
)
1040 const char *saved_input_filename
= input_filename
;
1041 input_filename
= value
;
1042 warning ("source file seen twice on command line and will be compiled only once.");
1043 input_filename
= saved_input_filename
;
1047 BUILD_FILENAME_IDENTIFIER_NODE (node
, value
);
1048 IS_A_COMMAND_LINE_FILENAME_P (node
) = 1;
1049 current_file_list
= tree_cons (NULL_TREE
, node
,
1057 if (filename_count
== 0)
1058 warning ("no input file specified");
1060 current_jcf
= main_jcf
;
1061 current_file_list
= nreverse (current_file_list
);
1062 for (node
= current_file_list
; node
; node
= TREE_CHAIN (node
))
1064 unsigned char magic_string
[4];
1066 tree name
= TREE_VALUE (node
);
1068 /* Skip already parsed files */
1069 if (HAS_BEEN_ALREADY_PARSED_P (name
))
1072 /* Close previous descriptor, if any */
1073 if (finput
&& fclose (finput
))
1074 fatal_io_error ("can't close input file %s", main_input_filename
);
1076 finput
= fopen (IDENTIFIER_POINTER (name
), "rb");
1078 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name
));
1080 #ifdef IO_BUFFER_SIZE
1081 setvbuf (finput
, (char *) xmalloc (IO_BUFFER_SIZE
),
1082 _IOFBF
, IO_BUFFER_SIZE
);
1084 input_filename
= IDENTIFIER_POINTER (name
);
1086 /* Figure what kind of file we're dealing with */
1087 if (fread (magic_string
, 1, 4, finput
) != 4)
1088 fatal_io_error ("Premature end of input file %s",
1089 IDENTIFIER_POINTER (name
));
1090 fseek (finput
, 0L, SEEK_SET
);
1091 magic
= GET_u4 (magic_string
);
1092 if (magic
== 0xcafebabe)
1094 CLASS_FILE_P (node
) = 1;
1095 current_jcf
= ALLOC (sizeof (JCF
));
1096 JCF_ZERO (current_jcf
);
1097 current_jcf
->read_state
= finput
;
1098 current_jcf
->filbuf
= jcf_filbuf_from_stdio
;
1099 jcf_parse (current_jcf
);
1100 TYPE_JCF (current_class
) = current_jcf
;
1101 CLASS_FROM_CURRENTLY_COMPILED_P (current_class
) = 1;
1102 TREE_PURPOSE (node
) = current_class
;
1104 else if (magic
== (JCF_u4
)ZIPMAGIC
)
1106 ZIP_FILE_P (node
) = 1;
1107 JCF_ZERO (main_jcf
);
1108 main_jcf
->read_state
= finput
;
1109 main_jcf
->filbuf
= jcf_filbuf_from_stdio
;
1110 if (open_in_zip (main_jcf
, input_filename
, NULL
, 0) < 0)
1111 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name
));
1112 localToFile
= SeenZipFiles
;
1113 /* Register all the class defined there. */
1114 process_zip_dir (main_jcf
->read_state
);
1115 parse_zip_file_entries ();
1118 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1123 JAVA_FILE_P (node
) = 1;
1124 java_push_parser_context ();
1125 java_parser_context_save_global ();
1126 parse_source_file_1 (name
, finput
);
1127 java_parser_context_restore_global ();
1128 java_pop_parser_context (1);
1132 for (ctxp
= ctxp_for_generation
; ctxp
; ctxp
= ctxp
->next
)
1134 input_filename
= ctxp
->filename
;
1135 parse_source_file_2 ();
1137 for (node
= current_file_list
; node
; node
= TREE_CHAIN (node
))
1139 input_filename
= IDENTIFIER_POINTER (TREE_VALUE (node
));
1140 if (CLASS_FILE_P (node
))
1142 current_class
= TREE_PURPOSE (node
);
1143 current_jcf
= TYPE_JCF (current_class
);
1144 layout_class (current_class
);
1145 load_inner_classes (current_class
);
1146 parse_class_file ();
1147 JCF_FINISH (current_jcf
);
1150 input_filename
= main_input_filename
;
1152 java_expand_classes ();
1153 if (!java_report_errors () && !flag_syntax_only
)
1154 emit_register_classes ();
1158 /* Process all class entries found in the zip file. */
1160 parse_zip_file_entries (void)
1162 struct ZipDirectory
*zdir
;
1165 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->central_directory
;
1166 i
< localToFile
->count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
1170 /* We don't need to consider those files. */
1171 if (!zdir
->size
|| !zdir
->filename_offset
)
1174 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir
)));
1175 current_jcf
= TYPE_JCF (class);
1176 current_class
= class;
1178 if ( !CLASS_LOADED_P (class))
1180 if (! CLASS_PARSED_P (class))
1182 read_zip_member(current_jcf
, zdir
, localToFile
);
1183 jcf_parse (current_jcf
);
1185 layout_class (current_class
);
1186 load_inner_classes (current_class
);
1189 if (TYPE_SIZE (current_class
) != error_mark_node
)
1191 input_filename
= current_jcf
->filename
;
1192 parse_class_file ();
1193 FREE (current_jcf
->buffer
); /* No longer necessary */
1194 /* Note: there is a way to free this buffer right after a
1195 class seen in a zip file has been parsed. The idea is the
1196 set its jcf in such a way that buffer will be reallocated
1197 the time the code for the class will be generated. FIXME. */
1202 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1203 jcf up for further processing and link it to the created class. */
1206 process_zip_dir (FILE *finput
)
1211 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->central_directory
;
1212 i
< localToFile
->count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
1214 char *class_name
, *file_name
, *class_name_in_zip_dir
;
1219 class_name_in_zip_dir
= ZIPDIR_FILENAME (zdir
);
1221 /* We choose to not to process entries with a zero size or entries
1222 not bearing the .class extention. */
1223 if (!zdir
->size
|| !zdir
->filename_offset
||
1224 strncmp (&class_name_in_zip_dir
[zdir
->filename_length
-6],
1227 /* So it will be skipped in parse_zip_file_entries */
1232 class_name
= ALLOC (zdir
->filename_length
+1-6);
1233 file_name
= ALLOC (zdir
->filename_length
+1);
1234 jcf
= ALLOC (sizeof (JCF
));
1237 strncpy (class_name
, class_name_in_zip_dir
, zdir
->filename_length
-6);
1238 class_name
[zdir
->filename_length
-6] = '\0';
1239 strncpy (file_name
, class_name_in_zip_dir
, zdir
->filename_length
);
1240 file_name
[zdir
->filename_length
] = '\0';
1242 for (j
=0; class_name
[j
]; j
++)
1243 class_name
[j
] = (class_name
[j
] == '/' ? '.' : class_name
[j
]);
1245 /* Yes, we write back the true class name into the zip directory. */
1246 strcpy (class_name_in_zip_dir
, class_name
);
1247 zdir
->filename_length
= j
;
1248 class = lookup_class (get_identifier (class_name
));
1250 jcf
->read_state
= finput
;
1251 jcf
->filbuf
= jcf_filbuf_from_stdio
;
1252 jcf
->java_source
= 0;
1253 jcf
->classname
= class_name
;
1254 jcf
->filename
= file_name
;
1257 TYPE_JCF (class) = jcf
;
1261 /* Initialization. */
1266 /* Register roots with the garbage collector. */
1267 ggc_add_tree_root (parse_roots
, sizeof (parse_roots
) / sizeof(tree
));
1269 ggc_add_root (¤t_jcf
, 1, sizeof (JCF
), (void (*)(void *))ggc_mark_jcf
);