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)
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> */
33 #include "java-except.h"
35 #include "java-tree.h"
46 #ifdef HAVE_NL_LANGINFO
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) \
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]; \
62 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
63 text[LENGTH] = save; \
64 JCF_SKIP (JCF, LENGTH); } while (0)
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 void ggc_mark_jcf
PARAMS ((void**));
102 static void jcf_parse
PARAMS ((struct JCF
*));
103 static void load_inner_classes
PARAMS ((tree
));
105 /* Mark (for garbage collection) all the tree nodes that are
106 referenced from JCF's constant pool table. Do that only if the JCF
107 hasn't been marked finished. */
113 JCF
*jcf
= *(JCF
**) elt
;
114 if (jcf
!= NULL
&& !jcf
->finished
)
116 CPool
*cpool
= &jcf
->cpool
;
117 int size
= CPOOL_COUNT(cpool
);
119 for (index
= 1; index
< size
; index
++)
121 int tag
= JPOOL_TAG (jcf
, index
);
122 if ((tag
& CONSTANT_ResolvedFlag
) || tag
== CONSTANT_Utf8
)
123 ggc_mark_tree ((tree
) cpool
->data
[index
]);
128 /* Handle "SourceFile" attribute. */
131 set_source_filename (jcf
, index
)
135 tree sfname_id
= get_name_constant (jcf
, index
);
136 const char *sfname
= IDENTIFIER_POINTER (sfname_id
);
137 if (input_filename
!= NULL
)
139 int old_len
= strlen (input_filename
);
140 int new_len
= IDENTIFIER_LENGTH (sfname_id
);
141 /* Use the current input_filename (derived from the class name)
142 if it has a directory prefix, but otherwise matches sfname. */
143 if (old_len
> new_len
144 && strcmp (sfname
, input_filename
+ old_len
- new_len
) == 0
145 && (input_filename
[old_len
- new_len
- 1] == '/'
146 || input_filename
[old_len
- new_len
- 1] == '\\'))
149 input_filename
= sfname
;
150 DECL_SOURCE_FILE (TYPE_NAME (current_class
)) = sfname
;
151 if (current_class
== main_class
) main_input_filename
= input_filename
;
154 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
156 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
157 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
158 current_class = give_name_to_class (jcf, THIS); \
159 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
161 #define HANDLE_CLASS_INTERFACE(INDEX) \
162 add_interface (current_class, get_class_constant (jcf, INDEX))
164 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
165 { int sig_index = SIGNATURE; \
166 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
167 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
168 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
169 if ((ACCESS_FLAGS) & ACC_FINAL) \
170 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
173 #define HANDLE_END_FIELDS() \
174 (current_field = NULL_TREE)
176 #define HANDLE_CONSTANTVALUE(INDEX) \
177 { tree constant; int index = INDEX; \
178 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
179 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
180 constant = build_utf8_ref (name); \
183 constant = get_constant (jcf, index); \
184 set_constant_value (current_field, constant); }
186 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
187 (current_method = add_method (current_class, ACCESS_FLAGS, \
188 get_name_constant (jcf, NAME), \
189 get_name_constant (jcf, SIGNATURE)), \
190 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
191 DECL_LINENUMBERS_OFFSET (current_method) = 0)
193 #define HANDLE_END_METHODS() \
194 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
195 if (handle_type != current_class) layout_type (handle_type); \
196 current_method = NULL_TREE; }
198 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
199 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
200 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
201 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
202 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
204 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
206 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
207 JCF_SKIP (jcf, n * 10); }
209 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
211 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
212 JCF_SKIP (jcf, n * 4); }
214 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
217 tree list = DECL_FUNCTION_THROWS (current_method); \
220 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
221 list = tree_cons (NULL_TREE, thrown_class, list); \
223 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
226 /* Link seen inner classes to their outer context and register the
227 inner class to its outer context. They will be later loaded. */
228 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
229 handle_innerclass_attribute (COUNT, jcf)
231 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
233 /* Irrelevant decls should have been nullified by the END macros. \
234 We only handle the `Synthetic' attribute on method DECLs. \
235 DECL_ARTIFICIAL on fields is used for something else (See \
236 PUSH_FIELD in java-tree.h) */ \
237 if (current_method) \
238 DECL_ARTIFICIAL (current_method) = 1; \
241 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
243 if (current_class == object_type_node) \
244 jcf->right_zip = 1; \
247 #include "jcf-reader.c"
252 parse_signature (jcf
, sig_index
)
256 if (sig_index
<= 0 || sig_index
>= JPOOL_SIZE (jcf
)
257 || JPOOL_TAG (jcf
, sig_index
) != CONSTANT_Utf8
)
260 return parse_signature_string (JPOOL_UTF_DATA (jcf
, sig_index
),
261 JPOOL_UTF_LENGTH (jcf
, sig_index
));
265 java_set_yydebug (value
)
272 get_constant (jcf
, index
)
278 if (index
<= 0 || index
>= JPOOL_SIZE(jcf
))
280 tag
= JPOOL_TAG (jcf
, index
);
281 if ((tag
& CONSTANT_ResolvedFlag
) || tag
== CONSTANT_Utf8
)
282 return (tree
) jcf
->cpool
.data
[index
];
285 case CONSTANT_Integer
:
287 jint num
= JPOOL_INT(jcf
, index
);
288 value
= build_int_2 (num
, num
< 0 ? -1 : 0);
289 TREE_TYPE (value
) = int_type_node
;
294 jint num
= JPOOL_INT (jcf
, index
);
295 HOST_WIDE_INT lo
, hi
;
296 lshift_double (num
, 0, 32, 64, &lo
, &hi
, 0);
297 num
= JPOOL_INT (jcf
, index
+1) & 0xffffffff;
298 add_double (lo
, hi
, num
, 0, &lo
, &hi
);
299 value
= build_int_2 (lo
, hi
);
300 TREE_TYPE (value
) = long_type_node
;
301 force_fit_type (value
, 0);
304 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
307 jint num
= JPOOL_INT(jcf
, index
);
309 d
= REAL_VALUE_FROM_TARGET_SINGLE (num
);
310 value
= build_real (float_type_node
, d
);
313 case CONSTANT_Double
:
315 HOST_WIDE_INT num
[2];
317 HOST_WIDE_INT lo
, hi
;
318 num
[0] = JPOOL_INT (jcf
, index
);
319 lshift_double (num
[0], 0, 32, 64, &lo
, &hi
, 0);
320 num
[0] = JPOOL_INT (jcf
, index
+1);
321 add_double (lo
, hi
, num
[0], 0, &lo
, &hi
);
323 /* Since ereal_from_double expects an array of HOST_WIDE_INT
324 in the target's format, we swap the elements for big endian
325 targets, unless HOST_WIDE_INT is sufficiently large to
326 contain a target double, in which case the 2nd element
329 FIXME: Is this always right for cross targets? */
330 if (FLOAT_WORDS_BIG_ENDIAN
&& sizeof(num
[0]) < 8)
340 d
= REAL_VALUE_FROM_TARGET_DOUBLE (num
);
341 value
= build_real (double_type_node
, d
);
344 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
345 case CONSTANT_String
:
347 tree name
= get_name_constant (jcf
, JPOOL_USHORT1 (jcf
, index
));
348 const char *utf8_ptr
= IDENTIFIER_POINTER (name
);
349 int utf8_len
= IDENTIFIER_LENGTH (name
);
350 unsigned char *str_ptr
;
352 const unsigned char *utf8
;
355 /* Count the number of Unicode characters in the string,
356 while checking for a malformed Utf8 string. */
357 utf8
= (const unsigned char *) utf8_ptr
;
362 int char_len
= UT8_CHAR_LENGTH (*utf8
);
363 if (char_len
< 0 || char_len
> 3 || char_len
> i
)
364 fatal_error ("bad string constant");
371 /* Allocate a scratch buffer, convert the string to UCS2, and copy it
372 into the new space. */
373 str_ptr
= (unsigned char *) alloca (2 * str_len
);
375 utf8
= (const unsigned char *)utf8_ptr
;
377 for (i
= 0; i
< str_len
; i
++)
380 int char_len
= UT8_CHAR_LENGTH (*utf8
);
384 char_value
= *utf8
++;
387 char_value
= *utf8
++ & 0x1F;
388 char_value
= (char_value
<< 6) | (*utf8
++ & 0x3F);
391 char_value
= *utf8
++ & 0x0F;
392 char_value
= (char_value
<< 6) | (*utf8
++ & 0x3F);
393 char_value
= (char_value
<< 6) | (*utf8
++ & 0x3F);
398 if (BYTES_BIG_ENDIAN
)
400 *str
++ = char_value
>> 8;
401 *str
++ = char_value
& 0xFF;
405 *str
++ = char_value
& 0xFF;
406 *str
++ = char_value
>> 8;
409 value
= build_string (str
- str_ptr
, str_ptr
);
410 TREE_TYPE (value
) = build_pointer_type (string_type_node
);
416 JPOOL_TAG (jcf
, index
) = tag
| CONSTANT_ResolvedFlag
;
417 jcf
->cpool
.data
[index
] = (jword
) value
;
420 internal_error ("bad value constant type %d, index %d",
421 JPOOL_TAG (jcf
, index
), index
);
425 get_name_constant (jcf
, index
)
429 tree name
= get_constant (jcf
, index
);
431 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
437 /* Handle reading innerclass attributes. If a non zero entry (denoting
438 a non anonymous entry) is found, We augment the inner class list of
439 the outer context with the newly resolved innerclass. */
442 handle_innerclass_attribute (count
, jcf
)
449 /* Read inner_class_info_index. This may be 0 */
450 int icii
= JCF_readu2 (jcf
);
451 /* Read outer_class_info_index. If the innerclasses attribute
452 entry isn't a member (like an inner class) the value is 0. */
453 int ocii
= JCF_readu2 (jcf
);
454 /* Read inner_name_index. If the class we're dealing with is
455 an annonymous class, it must be 0. */
456 int ini
= JCF_readu2 (jcf
);
457 /* Read the access flag. */
458 int acc
= JCF_readu2 (jcf
);
459 /* If icii is 0, don't try to read the class. */
462 tree
class = get_class_constant (jcf
, icii
);
463 tree decl
= TYPE_NAME (class);
464 /* Skip reading further if ocii is null */
465 if (DECL_P (decl
) && !CLASS_COMPLETE_P (decl
) && ocii
)
467 tree outer
= TYPE_NAME (get_class_constant (jcf
, ocii
));
468 tree alias
= (ini
? get_name_constant (jcf
, ini
) : NULL_TREE
);
469 set_class_decl_access_flags (acc
, decl
);
470 DECL_CONTEXT (decl
) = outer
;
471 DECL_INNER_CLASS_LIST (outer
) =
472 tree_cons (decl
, alias
, DECL_INNER_CLASS_LIST (outer
));
473 CLASS_COMPLETE_P (decl
) = 1;
480 give_name_to_class (jcf
, i
)
484 if (i
<= 0 || i
>= JPOOL_SIZE (jcf
)
485 || JPOOL_TAG (jcf
, i
) != CONSTANT_Class
)
490 int j
= JPOOL_USHORT1 (jcf
, i
);
491 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
492 tree class_name
= unmangle_classname (JPOOL_UTF_DATA (jcf
, j
),
493 JPOOL_UTF_LENGTH (jcf
, j
));
494 this_class
= lookup_class (class_name
);
495 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (this_class
));
497 if (main_input_filename
== NULL
&& jcf
== main_jcf
)
498 main_input_filename
= input_filename
;
500 jcf
->cpool
.data
[i
] = (jword
) this_class
;
501 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
506 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
509 get_class_constant (JCF
*jcf
, int i
)
512 if (i
<= 0 || i
>= JPOOL_SIZE (jcf
)
513 || (JPOOL_TAG (jcf
, i
) & ~CONSTANT_ResolvedFlag
) != CONSTANT_Class
)
516 if (JPOOL_TAG (jcf
, i
) != CONSTANT_ResolvedClass
)
518 int name_index
= JPOOL_USHORT1 (jcf
, i
);
519 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
520 const char *name
= JPOOL_UTF_DATA (jcf
, name_index
);
521 int nlength
= JPOOL_UTF_LENGTH (jcf
, name_index
);
523 if (name
[0] == '[') /* Handle array "classes". */
524 type
= TREE_TYPE (parse_signature_string (name
, nlength
));
527 tree cname
= unmangle_classname (name
, nlength
);
528 type
= lookup_class (cname
);
530 jcf
->cpool
.data
[i
] = (jword
) type
;
531 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
534 type
= (tree
) jcf
->cpool
.data
[i
];
538 /* Read a class with the fully qualified-name NAME.
539 Return 1 iff we read the requested file.
540 (It is still possible we failed if the file did not
541 define the class it is supposed to.) */
548 tree icv
, class = NULL_TREE
;
549 tree save_current_class
= current_class
;
550 const char *save_input_filename
= input_filename
;
551 JCF
*save_current_jcf
= current_jcf
;
553 if ((icv
= IDENTIFIER_CLASS_VALUE (name
)) != NULL_TREE
)
555 class = TREE_TYPE (icv
);
556 jcf
= TYPE_JCF (class);
563 this_jcf
.zipd
= NULL
;
565 if (find_class (IDENTIFIER_POINTER (name
), IDENTIFIER_LENGTH (name
),
572 if (current_jcf
->java_source
)
574 const char *filename
= current_jcf
->filename
;
579 java_parser_context_save_global ();
580 java_push_parser_context ();
581 BUILD_FILENAME_IDENTIFIER_NODE (file
, filename
);
582 generate
= IS_A_COMMAND_LINE_FILENAME_P (file
);
583 if (wfl_operator
== NULL_TREE
)
584 wfl_operator
= build_expr_wfl (NULL_TREE
, NULL
, 0, 0);
585 EXPR_WFL_FILENAME_NODE (wfl_operator
) = file
;
586 input_filename
= ggc_strdup (filename
);
587 current_class
= NULL_TREE
;
588 current_function_decl
= NULL_TREE
;
589 if (!HAS_BEEN_ALREADY_PARSED_P (file
))
591 if (!(finput
= fopen (input_filename
, "r")))
592 fatal_io_error ("can't reopen %s", input_filename
);
593 parse_source_file_1 (file
, finput
);
594 parse_source_file_2 ();
596 fatal_io_error ("can't close %s", input_filename
);
598 JCF_FINISH (current_jcf
);
599 java_pop_parser_context (generate
);
600 java_parser_context_restore_global ();
604 if (class == NULL_TREE
|| ! CLASS_PARSED_P (class))
606 java_parser_context_save_global ();
607 java_push_parser_context ();
608 current_class
= class;
609 input_filename
= current_jcf
->filename
;
610 if (JCF_SEEN_IN_ZIP (current_jcf
))
611 read_zip_member(current_jcf
,
612 current_jcf
->zipd
, current_jcf
->zipd
->zipf
);
613 jcf_parse (current_jcf
);
614 class = current_class
;
615 java_pop_parser_context (0);
616 java_parser_context_restore_global ();
618 layout_class (class);
619 load_inner_classes (class);
622 current_class
= save_current_class
;
623 input_filename
= save_input_filename
;
624 current_jcf
= save_current_jcf
;
628 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
629 called from the parser, otherwise it's a RECORD_TYPE node. If
630 VERBOSE is 1, print error message on failure to load a class. */
632 /* Replace calls to load_class by having callers call read_class directly
633 - and then perhaps rename read_class to load_class. FIXME */
636 load_class (class_or_name
, verbose
)
643 /* class_or_name can be the name of the class we want to load */
644 if (TREE_CODE (class_or_name
) == IDENTIFIER_NODE
)
645 name
= class_or_name
;
646 /* In some cases, it's a dependency that we process earlier that
648 else if (TREE_CODE (class_or_name
) == TREE_LIST
)
649 name
= TYPE_NAME (TREE_PURPOSE (class_or_name
));
650 /* Or it's a type in the making */
652 name
= DECL_NAME (TYPE_NAME (class_or_name
));
659 if ((class_loaded
= read_class (name
)))
662 /* We failed loading name. Now consider that we might be looking
663 for a inner class. */
664 if ((separator
= strrchr (IDENTIFIER_POINTER (name
), '$'))
665 || (separator
= strrchr (IDENTIFIER_POINTER (name
), '.')))
669 name
= get_identifier (IDENTIFIER_POINTER (name
));
672 /* Otherwise, we failed, we bail. */
677 if (!class_loaded
&& verbose
)
678 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved
));
681 /* Parse the .class file JCF. */
689 if (jcf_parse_preamble (jcf
) != 0)
690 fatal_error ("not a valid Java .class file");
691 code
= jcf_parse_constant_pool (jcf
);
693 fatal_error ("error while parsing constant pool");
694 code
= verify_constant_pool (jcf
);
696 fatal_error ("error in constant pool entry #%d\n", code
);
698 jcf_parse_class (jcf
);
699 if (main_class
== NULL_TREE
)
700 main_class
= current_class
;
701 if (! quiet_flag
&& TYPE_NAME (current_class
))
702 fprintf (stderr
, " %s %s",
703 (jcf
->access_flags
& ACC_INTERFACE
) ? "interface" : "class",
704 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class
))));
705 if (CLASS_PARSED_P (current_class
))
707 /* FIXME - where was first time */
708 fatal_error ("reading class %s for the second time from %s",
709 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class
))),
712 CLASS_PARSED_P (current_class
) = 1;
714 for (i
= 1; i
< JPOOL_SIZE(jcf
); i
++)
716 switch (JPOOL_TAG (jcf
, i
))
719 get_class_constant (jcf
, i
);
724 code
= jcf_parse_fields (jcf
);
726 fatal_error ("error while parsing fields");
727 code
= jcf_parse_methods (jcf
);
729 fatal_error ("error while parsing methods");
730 code
= jcf_parse_final_attributes (jcf
);
732 fatal_error ("error while parsing final attributes");
734 /* The fields of class_type_node are already in correct order. */
735 if (current_class
!= class_type_node
&& current_class
!= object_type_node
)
736 TYPE_FIELDS (current_class
) = nreverse (TYPE_FIELDS (current_class
));
738 if (current_class
== object_type_node
)
740 layout_class_methods (object_type_node
);
741 /* If we don't have the right archive, emit a verbose warning.
742 If we're generating bytecode, emit the warning only if
743 -fforce-classes-archive-check was specified. */
745 && (!flag_emit_class_files
|| flag_force_classes_archive_check
))
746 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
);
749 all_class_list
= tree_cons (NULL_TREE
,
750 TYPE_NAME (current_class
), all_class_list
);
753 /* If we came across inner classes, load them now. */
755 load_inner_classes (cur_class
)
759 for (current
= DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class
)); current
;
760 current
= TREE_CHAIN (current
))
762 tree name
= DECL_NAME (TREE_PURPOSE (current
));
763 tree decl
= IDENTIFIER_GLOBAL_VALUE (name
);
764 if (decl
&& ! CLASS_LOADED_P (TREE_TYPE (decl
))
765 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl
)))
766 load_class (name
, 1);
771 init_outgoing_cpool ()
773 current_constant_pool_data_ref
= NULL_TREE
;
774 outgoing_cpool
= (struct CPool
*)xmalloc (sizeof (struct CPool
));
775 memset (outgoing_cpool
, 0, sizeof (struct CPool
));
782 const char *save_input_filename
= input_filename
;
783 int save_lineno
= lineno
;
785 java_layout_seen_class_methods ();
787 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (current_class
));
789 (*debug_hooks
->start_source_file
) (lineno
, input_filename
);
790 init_outgoing_cpool ();
792 /* Currently we always have to emit calls to _Jv_InitClass when
793 compiling from class files. */
794 always_initialize_class_p
= 1;
796 for (field
= TYPE_FIELDS (CLASS_TO_HANDLE_TYPE (current_class
));
797 field
!= NULL_TREE
; field
= TREE_CHAIN (field
))
798 if (FIELD_STATIC (field
))
799 DECL_EXTERNAL (field
) = 0;
801 for (method
= TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class
));
802 method
!= NULL_TREE
; method
= TREE_CHAIN (method
))
804 JCF
*jcf
= current_jcf
;
806 if (METHOD_ABSTRACT (method
))
809 if (METHOD_NATIVE (method
))
816 /* We need to compute the DECL_MAX_LOCALS. We need to take
817 the wide types into account too. */
818 for (arg
= TYPE_ARG_TYPES (TREE_TYPE (method
)), decl_max_locals
= 0;
819 arg
!= end_params_node
;
820 arg
= TREE_CHAIN (arg
), decl_max_locals
+= 1)
822 if (TREE_VALUE (arg
) && TYPE_IS_WIDE (TREE_VALUE (arg
)))
823 decl_max_locals
+= 1;
825 DECL_MAX_LOCALS (method
) = decl_max_locals
;
826 start_java_method (method
);
827 give_name_to_locals (jcf
);
828 expand_expr_stmt (build_jni_stub (method
));
833 if (DECL_CODE_OFFSET (method
) == 0)
835 current_function_decl
= method
;
836 error ("missing Code attribute");
841 if (DECL_LINENUMBERS_OFFSET (method
))
844 register unsigned char *ptr
;
845 JCF_SEEK (jcf
, DECL_LINENUMBERS_OFFSET (method
));
846 linenumber_count
= i
= JCF_readu2 (jcf
);
847 linenumber_table
= ptr
= jcf
->read_ptr
;
849 for (ptr
+= 2; --i
>= 0; ptr
+= 4)
851 int line
= GET_u2 (ptr
);
852 /* Set initial lineno lineno to smallest linenumber.
853 * Needs to be set before init_function_start. */
854 if (lineno
== 0 || line
< lineno
)
860 linenumber_table
= NULL
;
861 linenumber_count
= 0;
864 start_java_method (method
);
866 note_instructions (jcf
, method
);
868 give_name_to_locals (jcf
);
870 /* Actually generate code. */
871 expand_byte_code (jcf
, method
);
876 if (flag_emit_class_files
)
877 write_classfile (current_class
);
881 (*debug_hooks
->end_source_file
) (save_lineno
);
882 input_filename
= save_input_filename
;
883 lineno
= save_lineno
;
886 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
889 parse_source_file_1 (file
, finput
)
893 int save_error_count
= java_error_count
;
894 /* Mark the file as parsed */
895 HAS_BEEN_ALREADY_PARSED_P (file
) = 1;
897 jcf_dependency_add_file (input_filename
, 0);
899 lang_init_source (1); /* Error msgs have no method prototypes */
901 /* There's no point in trying to find the current encoding unless we
902 are going to do something intelligent with it -- hence the test
904 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_NL_LANGINFO)
905 setlocale (LC_CTYPE
, "");
906 if (current_encoding
== NULL
)
907 current_encoding
= nl_langinfo (CODESET
);
909 if (current_encoding
== NULL
|| *current_encoding
== '\0')
910 current_encoding
= DEFAULT_ENCODING
;
912 /* Initialize the parser */
913 java_init_lex (finput
, current_encoding
);
914 java_parse_abort_on_error ();
916 java_parse (); /* Parse and build partial tree nodes. */
917 java_parse_abort_on_error ();
920 /* Process a parsed source file, resolving names etc. */
923 parse_source_file_2 ()
925 int save_error_count
= java_error_count
;
926 java_complete_class (); /* Parse unsatisfied class decl. */
927 java_parse_abort_on_error ();
928 java_check_circular_reference (); /* Check on circular references */
929 java_parse_abort_on_error ();
930 java_fix_constructors (); /* Fix the constructors */
931 java_parse_abort_on_error ();
932 java_reorder_fields (); /* Reorder the fields */
936 add_predefined_file (name
)
939 predef_filenames
= tree_cons (NULL_TREE
, name
, predef_filenames
);
943 predefined_filename_p (node
)
948 for (iter
= predef_filenames
; iter
!= NULL_TREE
; iter
= TREE_CHAIN (iter
))
950 if (TREE_VALUE (iter
) == node
)
959 int filename_count
= 0;
964 if (flag_filelist_file
)
967 finput
= fopen (input_filename
, "r");
969 fatal_io_error ("can't open %s", input_filename
);
970 list
= xmalloc(avail
);
978 avail
= 2 * (count
+ avail
);
979 list
= xrealloc (list
, avail
);
981 avail
= avail
- count
;
983 /* Subtract to to guarantee space for final '\0'. */
984 count
= fread (next
, 1, avail
- 1, finput
);
988 fatal_io_error ("error closing %s", input_filename
);
999 list
= xstrdup (input_filename
);
1003 for (next
= list
; ; )
1006 if (ch
== '\n' || ch
== '\r' || ch
== '\t' || ch
== ' '
1007 || ch
== '&' /* FIXME */)
1035 int len
= strlen (list
);
1037 if (*list
!= '/' && filename_count
> 0)
1038 obstack_grow (&temporary_obstack
, "./", 2);
1040 obstack_grow0 (&temporary_obstack
, list
, len
);
1041 value
= obstack_finish (&temporary_obstack
);
1045 /* Exclude file that we see twice on the command line. For
1046 all files except {Class,Error,Object,RuntimeException,String,
1047 Throwable}.java we can rely on maybe_get_identifier. For
1048 these files, we need to do a linear search of
1049 current_file_list. This search happens only for these
1050 files, presumably only when we're recompiling libgcj. */
1052 if ((id
= maybe_get_identifier (value
)))
1054 if (predefined_filename_p (id
))
1057 for (c
= current_file_list
; c
; c
= TREE_CHAIN (c
))
1058 if (TREE_VALUE (c
) == id
)
1067 const char *saved_input_filename
= input_filename
;
1068 input_filename
= value
;
1069 warning ("source file seen twice on command line and will be compiled only once");
1070 input_filename
= saved_input_filename
;
1074 BUILD_FILENAME_IDENTIFIER_NODE (node
, value
);
1075 IS_A_COMMAND_LINE_FILENAME_P (node
) = 1;
1076 current_file_list
= tree_cons (NULL_TREE
, node
,
1084 if (filename_count
== 0)
1085 warning ("no input file specified");
1089 const char *resource_filename
;
1091 /* Only one resource file may be compiled at a time. */
1092 assert (TREE_CHAIN (current_file_list
) == NULL
);
1094 resource_filename
= IDENTIFIER_POINTER (TREE_VALUE (current_file_list
));
1095 compile_resource_file (resource_name
, resource_filename
);
1097 java_expand_classes ();
1098 if (!java_report_errors ())
1099 emit_register_classes ();
1103 current_jcf
= main_jcf
;
1104 current_file_list
= nreverse (current_file_list
);
1105 for (node
= current_file_list
; node
; node
= TREE_CHAIN (node
))
1107 unsigned char magic_string
[4];
1109 tree name
= TREE_VALUE (node
);
1111 /* Skip already parsed files */
1112 if (HAS_BEEN_ALREADY_PARSED_P (name
))
1115 /* Close previous descriptor, if any */
1116 if (finput
&& fclose (finput
))
1117 fatal_io_error ("can't close input file %s", main_input_filename
);
1119 finput
= fopen (IDENTIFIER_POINTER (name
), "rb");
1121 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name
));
1123 #ifdef IO_BUFFER_SIZE
1124 setvbuf (finput
, (char *) xmalloc (IO_BUFFER_SIZE
),
1125 _IOFBF
, IO_BUFFER_SIZE
);
1127 input_filename
= IDENTIFIER_POINTER (name
);
1129 /* Figure what kind of file we're dealing with */
1130 if (fread (magic_string
, 1, 4, finput
) == 4)
1132 fseek (finput
, 0L, SEEK_SET
);
1133 magic
= GET_u4 (magic_string
);
1135 if (magic
== 0xcafebabe)
1137 CLASS_FILE_P (node
) = 1;
1138 current_jcf
= ALLOC (sizeof (JCF
));
1139 JCF_ZERO (current_jcf
);
1140 current_jcf
->read_state
= finput
;
1141 current_jcf
->filbuf
= jcf_filbuf_from_stdio
;
1142 jcf_parse (current_jcf
);
1143 TYPE_JCF (current_class
) = current_jcf
;
1144 CLASS_FROM_CURRENTLY_COMPILED_P (current_class
) = 1;
1145 TREE_PURPOSE (node
) = current_class
;
1147 else if (magic
== (JCF_u4
)ZIPMAGIC
)
1149 ZIP_FILE_P (node
) = 1;
1150 JCF_ZERO (main_jcf
);
1151 main_jcf
->read_state
= finput
;
1152 main_jcf
->filbuf
= jcf_filbuf_from_stdio
;
1153 if (open_in_zip (main_jcf
, input_filename
, NULL
, 0) < 0)
1154 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name
));
1155 localToFile
= SeenZipFiles
;
1156 /* Register all the class defined there. */
1157 process_zip_dir (main_jcf
->read_state
);
1158 parse_zip_file_entries ();
1161 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1166 JAVA_FILE_P (node
) = 1;
1167 java_push_parser_context ();
1168 java_parser_context_save_global ();
1169 parse_source_file_1 (name
, finput
);
1170 java_parser_context_restore_global ();
1171 java_pop_parser_context (1);
1175 for (ctxp
= ctxp_for_generation
; ctxp
; ctxp
= ctxp
->next
)
1177 input_filename
= ctxp
->filename
;
1178 parse_source_file_2 ();
1180 for (node
= current_file_list
; node
; node
= TREE_CHAIN (node
))
1182 input_filename
= IDENTIFIER_POINTER (TREE_VALUE (node
));
1183 if (CLASS_FILE_P (node
))
1185 current_class
= TREE_PURPOSE (node
);
1186 current_jcf
= TYPE_JCF (current_class
);
1187 layout_class (current_class
);
1188 load_inner_classes (current_class
);
1189 parse_class_file ();
1190 JCF_FINISH (current_jcf
);
1193 input_filename
= main_input_filename
;
1195 java_expand_classes ();
1196 if (!java_report_errors () && !flag_syntax_only
)
1198 emit_register_classes ();
1199 if (flag_indirect_dispatch
)
1200 emit_offset_symbol_table ();
1205 /* Process all class entries found in the zip file. */
1207 parse_zip_file_entries (void)
1209 struct ZipDirectory
*zdir
;
1212 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->central_directory
;
1213 i
< localToFile
->count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
1217 /* We don't need to consider those files. */
1218 if (!zdir
->size
|| !zdir
->filename_offset
)
1221 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir
)));
1222 current_jcf
= TYPE_JCF (class);
1223 current_class
= class;
1225 if ( !CLASS_LOADED_P (class))
1227 if (! CLASS_PARSED_P (class))
1229 read_zip_member(current_jcf
, zdir
, localToFile
);
1230 jcf_parse (current_jcf
);
1232 layout_class (current_class
);
1233 load_inner_classes (current_class
);
1236 if (TYPE_SIZE (current_class
) != error_mark_node
)
1238 input_filename
= current_jcf
->filename
;
1239 parse_class_file ();
1240 FREE (current_jcf
->buffer
); /* No longer necessary */
1241 /* Note: there is a way to free this buffer right after a
1242 class seen in a zip file has been parsed. The idea is the
1243 set its jcf in such a way that buffer will be reallocated
1244 the time the code for the class will be generated. FIXME. */
1249 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1250 jcf up for further processing and link it to the created class. */
1253 process_zip_dir (FILE *finput
)
1258 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->central_directory
;
1259 i
< localToFile
->count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
1261 char *class_name
, *file_name
, *class_name_in_zip_dir
;
1266 class_name_in_zip_dir
= ZIPDIR_FILENAME (zdir
);
1268 /* We choose to not to process entries with a zero size or entries
1269 not bearing the .class extension. */
1270 if (!zdir
->size
|| !zdir
->filename_offset
||
1271 strncmp (&class_name_in_zip_dir
[zdir
->filename_length
-6],
1274 /* So it will be skipped in parse_zip_file_entries */
1279 class_name
= ALLOC (zdir
->filename_length
+1-6);
1280 file_name
= ALLOC (zdir
->filename_length
+1);
1281 jcf
= ALLOC (sizeof (JCF
));
1284 strncpy (class_name
, class_name_in_zip_dir
, zdir
->filename_length
-6);
1285 class_name
[zdir
->filename_length
-6] = '\0';
1286 strncpy (file_name
, class_name_in_zip_dir
, zdir
->filename_length
);
1287 file_name
[zdir
->filename_length
] = '\0';
1289 for (j
=0; class_name
[j
]; j
++)
1290 class_name
[j
] = (class_name
[j
] == '/' ? '.' : class_name
[j
]);
1292 /* Yes, we write back the true class name into the zip directory. */
1293 strcpy (class_name_in_zip_dir
, class_name
);
1294 zdir
->filename_length
= j
;
1295 class = lookup_class (get_identifier (class_name
));
1297 jcf
->read_state
= finput
;
1298 jcf
->filbuf
= jcf_filbuf_from_stdio
;
1299 jcf
->java_source
= 0;
1300 jcf
->classname
= class_name
;
1301 jcf
->filename
= file_name
;
1304 TYPE_JCF (class) = jcf
;
1308 /* Initialization. */
1313 /* Register roots with the garbage collector. */
1314 ggc_add_tree_root (parse_roots
, ARRAY_SIZE (parse_roots
));
1316 ggc_add_root (¤t_jcf
, 1, sizeof (JCF
), (void (*)(void *))ggc_mark_jcf
);