1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000 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 /* The FIELD_DECL for the current field. */
74 static tree current_field
= NULL_TREE
;
76 /* The METHOD_DECL for the current method. */
77 static tree current_method
= NULL_TREE
;
79 /* The Java .class file that provides main_class; the main input file. */
80 static struct JCF main_jcf
[1];
82 /* Declarations of some functions used here. */
83 static tree give_name_to_class
PARAMS ((JCF
*jcf
, int index
));
84 static void parse_zip_file_entries
PARAMS ((void));
85 static void process_zip_dir
PARAMS ((void));
86 static void parse_source_file
PARAMS ((tree
, FILE *));
87 static void jcf_parse_source
PARAMS ((void));
88 static int jcf_figure_file_type
PARAMS ((JCF
*));
89 static int find_in_current_zip
PARAMS ((const char *, struct JCF
**));
90 static void parse_class_file
PARAMS ((void));
91 static void set_source_filename
PARAMS ((JCF
*, int));
92 static int predefined_filename_p
PARAMS ((tree
));
94 /* Handle "SourceFile" attribute. */
97 set_source_filename (jcf
, index
)
101 tree sfname_id
= get_name_constant (jcf
, index
);
102 const char *sfname
= IDENTIFIER_POINTER (sfname_id
);
103 if (input_filename
!= NULL
)
105 int old_len
= strlen (input_filename
);
106 int new_len
= IDENTIFIER_LENGTH (sfname_id
);
107 /* Use the current input_filename (derived from the class name)
108 if it has a directory prefix, but otherwise matches sfname. */
109 if (old_len
> new_len
110 && strcmp (sfname
, input_filename
+ old_len
- new_len
) == 0
111 && (input_filename
[old_len
- new_len
- 1] == '/'
112 || input_filename
[old_len
- new_len
- 1] == '\\'))
115 input_filename
= sfname
;
116 DECL_SOURCE_FILE (TYPE_NAME (current_class
)) = sfname
;
117 if (current_class
== main_class
) main_input_filename
= input_filename
;
120 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
122 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
123 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
124 current_class = give_name_to_class (jcf, THIS); \
125 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
127 #define HANDLE_CLASS_INTERFACE(INDEX) \
128 add_interface (current_class, get_class_constant (jcf, INDEX))
130 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
131 { int sig_index = SIGNATURE; \
132 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
133 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
134 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); }
136 #define HANDLE_END_FIELDS() \
137 (current_field = NULL_TREE)
139 #define HANDLE_CONSTANTVALUE(INDEX) \
140 { tree constant; int index = INDEX; \
141 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
142 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
143 constant = build_utf8_ref (name); \
146 constant = get_constant (jcf, index); \
147 set_constant_value (current_field, constant); }
149 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
150 (current_method = add_method (current_class, ACCESS_FLAGS, \
151 get_name_constant (jcf, NAME), \
152 get_name_constant (jcf, SIGNATURE)), \
153 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
154 DECL_LINENUMBERS_OFFSET (current_method) = 0)
156 #define HANDLE_END_METHODS() \
157 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
158 if (handle_type != current_class) layout_type (handle_type); }
160 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
161 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
162 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
163 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
164 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
166 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
168 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
169 JCF_SKIP (jcf, n * 10); }
171 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
173 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
174 JCF_SKIP (jcf, n * 4); }
176 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
179 tree list = DECL_FUNCTION_THROWS (current_method); \
182 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
183 list = tree_cons (NULL_TREE, thrown_class, list); \
185 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
188 /* Link seen inner classes to their outer context and register the
189 inner class to its outer context. They will be later loaded. */
190 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
195 tree class = get_class_constant (jcf, JCF_readu2 (jcf)); \
196 tree decl = TYPE_NAME (class); \
197 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl)) \
199 tree outer = TYPE_NAME (get_class_constant (jcf, \
200 JCF_readu2 (jcf))); \
201 tree alias = get_name_constant (jcf, JCF_readu2 (jcf)); \
203 DECL_CONTEXT (decl) = outer; \
204 DECL_INNER_CLASS_LIST (outer) = \
205 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer)); \
206 CLASS_COMPLETE_P (decl) = 1; \
213 #include "jcf-reader.c"
218 parse_signature (jcf
, sig_index
)
222 if (sig_index
<= 0 || sig_index
>= JPOOL_SIZE(jcf
)
223 || JPOOL_TAG (jcf
, sig_index
) != CONSTANT_Utf8
)
224 fatal ("invalid field/method signature");
227 return parse_signature_string (JPOOL_UTF_DATA (jcf
, sig_index
),
228 JPOOL_UTF_LENGTH (jcf
, sig_index
));
235 /* Make identifier nodes long enough for the language-specific slots. */
236 set_identifier_size (sizeof (struct lang_identifier
));
247 get_constant (jcf
, index
)
253 if (index
<= 0 || index
>= JPOOL_SIZE(jcf
))
255 tag
= JPOOL_TAG (jcf
, index
);
256 if ((tag
& CONSTANT_ResolvedFlag
) || tag
== CONSTANT_Utf8
)
257 return (tree
) jcf
->cpool
.data
[index
];
260 case CONSTANT_Integer
:
262 jint num
= JPOOL_INT(jcf
, index
);
263 value
= build_int_2 (num
, num
< 0 ? -1 : 0);
264 TREE_TYPE (value
) = int_type_node
;
269 jint num
= JPOOL_INT (jcf
, index
);
270 HOST_WIDE_INT lo
, hi
;
271 lshift_double (num
, 0, 32, 64, &lo
, &hi
, 0);
272 num
= JPOOL_INT (jcf
, index
+1) & 0xffffffff;
273 add_double (lo
, hi
, num
, 0, &lo
, &hi
);
274 value
= build_int_2 (lo
, hi
);
275 TREE_TYPE (value
) = long_type_node
;
276 force_fit_type (value
, 0);
279 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
282 jint num
= JPOOL_INT(jcf
, index
);
284 #ifdef REAL_ARITHMETIC
285 d
= REAL_VALUE_FROM_TARGET_SINGLE (num
);
287 union { float f
; jint i
; } u
;
291 value
= build_real (float_type_node
, d
);
294 case CONSTANT_Double
:
296 HOST_WIDE_INT num
[2];
298 HOST_WIDE_INT lo
, hi
;
299 num
[0] = JPOOL_INT (jcf
, index
);
300 lshift_double (num
[0], 0, 32, 64, &lo
, &hi
, 0);
301 num
[0] = JPOOL_INT (jcf
, index
+1);
302 add_double (lo
, hi
, num
[0], 0, &lo
, &hi
);
303 if (FLOAT_WORDS_BIG_ENDIAN
)
313 #ifdef REAL_ARITHMETIC
314 d
= REAL_VALUE_FROM_TARGET_DOUBLE (num
);
317 union { double d
; jint i
[2]; } u
;
318 u
.i
[0] = (jint
) num
[0];
319 u
.i
[1] = (jint
) num
[1];
323 value
= build_real (double_type_node
, d
);
326 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
327 case CONSTANT_String
:
329 tree name
= get_name_constant (jcf
, JPOOL_USHORT1 (jcf
, index
));
330 const char *utf8_ptr
= IDENTIFIER_POINTER (name
);
331 unsigned char *str_ptr
;
332 int utf8_len
= IDENTIFIER_LENGTH (name
);
333 const unsigned char *str
= (const unsigned char *)utf8_ptr
;
337 /* Count the number of Unicode characters in the string,
338 while checking for a malformed Utf8 string. */
339 for (str_len
= 0; i
> 0; str_len
++)
341 int char_len
= UT8_CHAR_LENGTH (*str
);
342 if (char_len
< 0 || char_len
> 3 || char_len
> i
)
343 fatal ("bad string constant");
348 value
= make_node (STRING_CST
);
349 TREE_TYPE (value
) = build_pointer_type (string_type_node
);
350 TREE_STRING_LENGTH (value
) = 2 * str_len
;
351 TREE_STRING_POINTER (value
) = ggc_alloc (2 * str_len
);
352 str_ptr
= (unsigned char *) TREE_STRING_POINTER (value
);
353 str
= (const unsigned char *)utf8_ptr
;
354 for (i
= 0; i
< str_len
; i
++)
357 int char_len
= UT8_CHAR_LENGTH (*str
);
364 char_value
= *str
++ & 0x1F;
365 char_value
= (char_value
<< 6) | (*str
++ & 0x3F);
368 char_value
= *str
++ & 0x0F;
369 char_value
= (char_value
<< 6) | (*str
++ & 0x3F);
370 char_value
= (char_value
<< 6) | (*str
++ & 0x3F);
375 if (BYTES_BIG_ENDIAN
)
377 *str_ptr
++ = char_value
>> 8;
378 *str_ptr
++ = char_value
& 0xFF;
382 *str_ptr
++ = char_value
& 0xFF;
383 *str_ptr
++ = char_value
>> 8;
391 JPOOL_TAG(jcf
, index
) = tag
| CONSTANT_ResolvedFlag
;
392 jcf
->cpool
.data
[index
] = (jword
) value
;
395 fatal ("bad value constant type %d, index %d",
396 JPOOL_TAG( jcf
, index
), index
);
400 get_name_constant (jcf
, index
)
404 tree name
= get_constant (jcf
, index
);
405 if (TREE_CODE (name
) != IDENTIFIER_NODE
)
406 fatal ("bad nameandtype index %d", index
);
411 give_name_to_class (jcf
, i
)
415 if (i
<= 0 || i
>= JPOOL_SIZE(jcf
)
416 || JPOOL_TAG (jcf
, i
) != CONSTANT_Class
)
417 fatal ("bad class index %d", i
);
421 int j
= JPOOL_USHORT1 (jcf
, i
);
422 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
423 tree class_name
= unmangle_classname (JPOOL_UTF_DATA (jcf
, j
),
424 JPOOL_UTF_LENGTH (jcf
, j
));
425 this_class
= lookup_class (class_name
);
426 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (this_class
));
428 if (main_input_filename
== NULL
&& jcf
== main_jcf
)
429 main_input_filename
= input_filename
;
431 jcf
->cpool
.data
[i
] = (jword
) this_class
;
432 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
437 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
440 get_class_constant (JCF
*jcf
, int i
)
443 if (i
<= 0 || i
>= JPOOL_SIZE(jcf
)
444 || (JPOOL_TAG (jcf
, i
) & ~CONSTANT_ResolvedFlag
) != CONSTANT_Class
)
445 fatal ("bad class index %d", i
);
447 if (JPOOL_TAG (jcf
, i
) != CONSTANT_ResolvedClass
)
449 int name_index
= JPOOL_USHORT1 (jcf
, i
);
450 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
451 const char *name
= JPOOL_UTF_DATA (jcf
, name_index
);
452 int nlength
= JPOOL_UTF_LENGTH (jcf
, name_index
);
453 if (name
[0] == '[') /* Handle array "classes". */
454 type
= TREE_TYPE (parse_signature_string (name
, nlength
));
457 tree cname
= unmangle_classname (name
, nlength
);
458 type
= lookup_class (cname
);
460 jcf
->cpool
.data
[i
] = (jword
) type
;
461 JPOOL_TAG (jcf
, i
) = CONSTANT_ResolvedClass
;
464 type
= (tree
) jcf
->cpool
.data
[i
];
468 /* Read a class with the fully qualified-name NAME.
469 Return 1 iff we read the requested file.
470 (It is still possible we failed if the file did not
471 define the class it is supposed to.) */
478 tree save_current_class
= current_class
;
479 const char *save_input_filename
= input_filename
;
480 JCF
*save_current_jcf
= current_jcf
;
482 if (current_jcf
->read_state
)
483 saved_pos
= ftell (current_jcf
->read_state
);
485 /* Search in current zip first. */
486 if (find_in_current_zip (IDENTIFIER_POINTER (name
), &jcf
) == 0)
488 if (find_class (IDENTIFIER_POINTER (name
), IDENTIFIER_LENGTH (name
),
493 this_jcf
.seen_in_zip
= 0;
494 current_jcf
= &this_jcf
;
500 if (current_jcf
->java_source
)
503 java_parser_context_save_global ();
504 java_push_parser_context ();
505 input_filename
= current_jcf
->filename
;
506 jcf_parse (current_jcf
);
507 java_pop_parser_context (0);
508 java_parser_context_restore_global ();
511 if (!current_jcf
->seen_in_zip
)
512 JCF_FINISH (current_jcf
);
514 current_class
= save_current_class
;
515 input_filename
= save_input_filename
;
516 current_jcf
= save_current_jcf
;
517 if (current_jcf
->read_state
)
518 fseek (current_jcf
->read_state
, saved_pos
, SEEK_SET
);
522 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
523 called from the parser, otherwise it's a RECORD_TYPE node. If
524 VERBOSE is 1, print error message on failure to load a class. */
526 /* Replace calls to load_class by having callers call read_class directly
527 - and then perhaps rename read_class to load_class. FIXME */
530 load_class (class_or_name
, verbose
)
536 /* class_or_name can be the name of the class we want to load */
537 if (TREE_CODE (class_or_name
) == IDENTIFIER_NODE
)
538 name
= class_or_name
;
539 /* In some cases, it's a dependency that we process earlier that
541 else if (TREE_CODE (class_or_name
) == TREE_LIST
)
542 name
= TYPE_NAME (TREE_PURPOSE (class_or_name
));
543 /* Or it's a type in the making */
545 name
= DECL_NAME (TYPE_NAME (class_or_name
));
547 if (read_class (name
) == 0 && verbose
)
548 fatal ("Cannot find file for class %s.", IDENTIFIER_POINTER (name
));
551 /* Parse a source file when JCF refers to a source file. */
559 java_parser_context_save_global ();
560 java_push_parser_context ();
561 BUILD_FILENAME_IDENTIFIER_NODE (file
, current_jcf
->filename
);
562 EXPR_WFL_FILENAME_NODE (wfl_operator
) = file
;
563 input_filename
= ggc_strdup (current_jcf
->filename
);
564 current_class
= NULL_TREE
;
565 current_function_decl
= NULL_TREE
;
566 if (!HAS_BEEN_ALREADY_PARSED_P (file
))
568 if (!(finput
= fopen (input_filename
, "r")))
569 fatal ("input file `%s' just disappeared - jcf_parse_source",
571 parse_source_file (file
, finput
);
573 fatal ("can't close input file `%s' stream - jcf_parse_source",
576 java_pop_parser_context (IS_A_COMMAND_LINE_FILENAME_P (file
));
577 java_parser_context_restore_global ();
580 /* Parse the .class file JCF. */
589 if (jcf_parse_preamble (jcf
) != 0)
590 fatal ("Not a valid Java .class file.\n");
591 code
= jcf_parse_constant_pool (jcf
);
593 fatal ("error while parsing constant pool");
594 code
= verify_constant_pool (jcf
);
596 fatal ("error in constant pool entry #%d\n", code
);
598 jcf_parse_class (jcf
);
599 if (main_class
== NULL_TREE
)
600 main_class
= current_class
;
601 if (! quiet_flag
&& TYPE_NAME (current_class
))
602 fprintf (stderr
, " %s %s",
603 (jcf
->access_flags
& ACC_INTERFACE
) ? "interface" : "class",
604 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class
))));
605 if (CLASS_LOADED_P (current_class
))
607 CLASS_LOADED_P (current_class
) = 1;
609 for (i
= 1; i
< JPOOL_SIZE(jcf
); i
++)
611 switch (JPOOL_TAG (jcf
, i
))
614 get_class_constant (jcf
, i
);
619 code
= jcf_parse_fields (jcf
);
621 fatal ("error while parsing fields");
622 code
= jcf_parse_methods (jcf
);
624 fatal ("error while parsing methods");
625 code
= jcf_parse_final_attributes (jcf
);
627 fatal ("error while parsing final attributes");
629 /* The fields of class_type_node are already in correct order. */
630 if (current_class
!= class_type_node
&& current_class
!= object_type_node
)
631 TYPE_FIELDS (current_class
) = nreverse (TYPE_FIELDS (current_class
));
633 layout_class (current_class
);
634 if (current_class
== object_type_node
)
635 layout_class_methods (object_type_node
);
637 all_class_list
= tree_cons (NULL_TREE
,
638 TYPE_NAME (current_class
), all_class_list
);
640 /* And if we came accross inner classes, load them now. */
641 for (current
= DECL_INNER_CLASS_LIST (TYPE_NAME (current_class
)); current
;
642 current
= TREE_CHAIN (current
))
643 load_class (DECL_NAME (TREE_PURPOSE (current
)), 1);
647 init_outgoing_cpool ()
649 current_constant_pool_data_ref
= NULL_TREE
;
650 outgoing_cpool
= (struct CPool
*)xmalloc (sizeof (struct CPool
));
651 bzero (outgoing_cpool
, sizeof (struct CPool
));
658 const char *save_input_filename
= input_filename
;
659 int save_lineno
= lineno
;
661 java_layout_seen_class_methods ();
663 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (current_class
));
665 debug_start_source_file (input_filename
);
666 init_outgoing_cpool ();
668 /* Currently we always have to emit calls to _Jv_InitClass when
669 compiling from class files. */
670 always_initialize_class_p
= 1;
672 for ( method
= TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class
));
673 method
!= NULL_TREE
; method
= TREE_CHAIN (method
))
675 JCF
*jcf
= current_jcf
;
677 if (METHOD_ABSTRACT (method
))
680 if (METHOD_NATIVE (method
))
684 DECL_MAX_LOCALS (method
)
685 = list_length (TYPE_ARG_TYPES (TREE_TYPE (method
)));
686 start_java_method (method
);
687 give_name_to_locals (jcf
);
688 expand_expr_stmt (build_jni_stub (method
));
693 if (DECL_CODE_OFFSET (method
) == 0)
695 error ("missing Code attribute");
700 if (DECL_LINENUMBERS_OFFSET (method
))
703 register unsigned char *ptr
;
704 JCF_SEEK (jcf
, DECL_LINENUMBERS_OFFSET (method
));
705 linenumber_count
= i
= JCF_readu2 (jcf
);
706 linenumber_table
= ptr
= jcf
->read_ptr
;
708 for (ptr
+= 2; --i
>= 0; ptr
+= 4)
710 int line
= GET_u2 (ptr
);
711 /* Set initial lineno lineno to smallest linenumber.
712 * Needs to be set before init_function_start. */
713 if (lineno
== 0 || line
< lineno
)
719 linenumber_table
= NULL
;
720 linenumber_count
= 0;
723 start_java_method (method
);
725 note_instructions (jcf
, method
);
727 give_name_to_locals (jcf
);
729 /* Actually generate code. */
730 expand_byte_code (jcf
, method
);
735 if (flag_emit_class_files
)
736 write_classfile (current_class
);
740 debug_end_source_file (save_lineno
);
741 input_filename
= save_input_filename
;
742 lineno
= save_lineno
;
745 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
748 parse_source_file (file
, finput
)
752 int save_error_count
= java_error_count
;
753 /* Mark the file as parsed */
754 HAS_BEEN_ALREADY_PARSED_P (file
) = 1;
756 jcf_dependency_add_file (input_filename
, 0);
758 lang_init_source (1); /* Error msgs have no method prototypes */
760 /* There's no point in trying to find the current encoding unless we
761 are going to do something intelligent with it -- hence the test
764 #ifdef HAVE_NL_LANGINFO
765 setlocale (LC_CTYPE
, "");
766 if (current_encoding
== NULL
)
767 current_encoding
= nl_langinfo (CODESET
);
768 #endif /* HAVE_NL_LANGINFO */
769 #endif /* HAVE_ICONV */
770 if (current_encoding
== NULL
|| *current_encoding
== '\0')
771 current_encoding
= DEFAULT_ENCODING
;
773 /* Initialize the parser */
774 java_init_lex (finput
, current_encoding
);
775 java_parse_abort_on_error ();
777 java_parse (); /* Parse and build partial tree nodes. */
778 java_parse_abort_on_error ();
779 java_complete_class (); /* Parse unsatisfied class decl. */
780 java_parse_abort_on_error ();
781 java_check_circular_reference (); /* Check on circular references */
782 java_parse_abort_on_error ();
783 java_fix_constructors (); /* Fix the constructors */
784 java_parse_abort_on_error ();
785 java_reorder_fields (); /* Reorder the fields */
789 predefined_filename_p (node
)
793 for (i
= 0; i
< PREDEF_FILENAMES_SIZE
; i
++)
794 if (predef_filenames
[i
] == node
)
802 int several_files
= 0;
803 char *list
= xstrdup (input_filename
), *next
;
804 tree node
, current_file_list
= NULL_TREE
;
809 next
= strchr (list
, '&');
822 int len
= strlen (list
);
824 if (*list
!= '/' && several_files
)
825 obstack_grow (&temporary_obstack
, "./", 2);
827 obstack_grow0 (&temporary_obstack
, list
, len
);
828 value
= obstack_finish (&temporary_obstack
);
830 /* Exclude file that we see twice on the command line. For
831 all files except {Class,Error,Object,RuntimeException,String,
832 Throwable}.java we can rely on maybe_get_identifier. For
833 these files, we need to do a linear search of
834 current_file_list. This search happens only for these
835 files, presumably only when we're recompiling libgcj. */
837 if ((id
= maybe_get_identifier (value
)))
839 if (predefined_filename_p (id
))
842 for (c
= current_file_list
; c
; c
= TREE_CHAIN (c
))
843 if (TREE_VALUE (c
) == id
)
852 const char *saved_input_filename
= input_filename
;
853 input_filename
= value
;
854 warning ("source file seen twice on command line and will be compiled only once.");
855 input_filename
= saved_input_filename
;
859 BUILD_FILENAME_IDENTIFIER_NODE (node
, value
);
860 IS_A_COMMAND_LINE_FILENAME_P (node
) = 1;
861 current_file_list
= tree_cons (NULL_TREE
, node
,
869 current_jcf
= main_jcf
;
870 current_file_list
= nreverse (current_file_list
);
871 for (node
= current_file_list
; node
; node
= TREE_CHAIN (node
))
873 tree name
= TREE_VALUE (node
);
875 /* Skip already parsed files */
876 if (HAS_BEEN_ALREADY_PARSED_P (name
))
879 /* Close previous descriptor, if any */
880 if (main_jcf
->read_state
&& fclose (main_jcf
->read_state
))
881 fatal ("failed to close input file `%s' - yyparse",
882 (main_jcf
->filename
? main_jcf
->filename
: "<unknown>"));
884 /* Set jcf up and open a new file */
886 main_jcf
->read_state
= fopen (IDENTIFIER_POINTER (name
), "rb");
887 if (main_jcf
->read_state
== NULL
)
888 pfatal_with_name (IDENTIFIER_POINTER (name
));
890 /* Set new input_filename and finput */
891 finput
= main_jcf
->read_state
;
892 #ifdef IO_BUFFER_SIZE
893 setvbuf (finput
, (char *) xmalloc (IO_BUFFER_SIZE
),
894 _IOFBF
, IO_BUFFER_SIZE
);
896 input_filename
= IDENTIFIER_POINTER (name
);
897 main_jcf
->filbuf
= jcf_filbuf_from_stdio
;
899 switch (jcf_figure_file_type (current_jcf
))
902 parse_zip_file_entries ();
905 jcf_parse (current_jcf
);
909 java_push_parser_context ();
910 java_parser_context_save_global ();
911 parse_source_file (name
, finput
);
912 java_parser_context_restore_global ();
913 java_pop_parser_context (1);
918 java_expand_classes ();
919 if (!java_report_errors () && !flag_syntax_only
)
920 emit_register_classes ();
924 static struct ZipFileCache
*localToFile
;
926 /* Process all class entries found in the zip file. */
928 parse_zip_file_entries (void)
930 struct ZipDirectory
*zdir
;
933 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->z
.central_directory
;
934 i
< localToFile
->z
.count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
938 /* We don't need to consider those files. */
939 if (!zdir
->size
|| !zdir
->filename_offset
)
942 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir
)));
943 current_jcf
= TYPE_JCF (class);
944 current_class
= class;
946 if ( !CLASS_LOADED_P (class))
948 fseek (current_jcf
->read_state
, current_jcf
->zip_offset
, SEEK_SET
);
949 jcf_parse (current_jcf
);
952 if (TYPE_SIZE (current_class
) != error_mark_node
)
954 input_filename
= current_jcf
->filename
;
956 FREE (current_jcf
->buffer
); /* No longer necessary */
957 /* Note: there is a way to free this buffer right after a
958 class seen in a zip file has been parsed. The idea is the
959 set its jcf in such a way that buffer will be reallocated
960 the time the code for the class will be generated. FIXME. */
965 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
966 jcf up for further processing and link it to the created class. */
968 static void process_zip_dir()
973 for (i
= 0, zdir
= (ZipDirectory
*)localToFile
->z
.central_directory
;
974 i
< localToFile
->z
.count
; i
++, zdir
= ZIPDIR_NEXT (zdir
))
976 char *class_name
, *file_name
, *class_name_in_zip_dir
;
981 class_name_in_zip_dir
= ZIPDIR_FILENAME (zdir
);
983 /* We choose to not to process entries with a zero size or entries
984 not bearing the .class extention. */
985 if (!zdir
->size
|| !zdir
->filename_offset
||
986 strncmp (&class_name_in_zip_dir
[zdir
->filename_length
-6],
989 /* So it will be skipped in parse_zip_file_entries */
994 class_name
= ALLOC (zdir
->filename_length
+1-6);
995 file_name
= ALLOC (zdir
->filename_length
+1);
996 jcf
= ALLOC (sizeof (JCF
));
999 strncpy (class_name
, class_name_in_zip_dir
, zdir
->filename_length
-6);
1000 class_name
[zdir
->filename_length
-6] = '\0';
1001 strncpy (file_name
, class_name_in_zip_dir
, zdir
->filename_length
);
1002 file_name
[zdir
->filename_length
] = '\0';
1004 for (j
=0; class_name
[j
]; j
++)
1005 class_name
[j
] = (class_name
[j
] == '/' ? '.' : class_name
[j
]);
1007 /* Yes, we write back the true class name into the zip directory. */
1008 strcpy (class_name_in_zip_dir
, class_name
);
1009 zdir
->filename_length
= j
;
1010 class = lookup_class (get_identifier (class_name
));
1012 jcf
->read_state
= finput
;
1013 jcf
->filbuf
= jcf_filbuf_from_stdio
;
1014 jcf
->seen_in_zip
= 1;
1015 jcf
->java_source
= 0;
1016 jcf
->zip_offset
= zdir
->filestart
;
1017 jcf
->classname
= class_name
;
1018 jcf
->filename
= file_name
;
1020 TYPE_JCF (class) = jcf
;
1024 /* Lookup class NAME and figure whether is a class already found in the current
1027 DEFUN(find_in_current_zip
, (name
, length
, jcf
),
1028 const char *name AND JCF
**jcf
)
1031 tree class_name
= maybe_get_identifier (name
), class, icv
;
1036 if (!(icv
= IDENTIFIER_CLASS_VALUE (class_name
)))
1039 class = TREE_TYPE (icv
);
1041 /* Doesn't have jcf specific info ? It's not ours */
1042 if (!TYPE_JCF (class))
1045 *jcf
= local_jcf
= TYPE_JCF (class);
1046 fseek (local_jcf
->read_state
, local_jcf
->zip_offset
, SEEK_SET
);
1050 /* Figure what kind of file we're dealing with */
1052 DEFUN(jcf_figure_file_type
, (jcf
),
1055 unsigned char magic_string
[4];
1058 if (fread (magic_string
, 1, 4, jcf
->read_state
) != 4)
1059 jcf_unexpected_eof (jcf
, 4);
1061 fseek (jcf
->read_state
, 0L, SEEK_SET
);
1062 magic
= GET_u4 (magic_string
);
1064 if (magic
== 0xcafebabe)
1067 /* FIXME: is it a system file? */
1068 if (magic
== (JCF_u4
)ZIPMAGIC
1069 && !open_in_zip (jcf
, input_filename
, NULL
, 0))
1071 localToFile
= ALLOC (sizeof (struct ZipFileCache
));
1072 bcopy ((PTR
) SeenZipFiles
, (PTR
) localToFile
,
1073 sizeof (struct ZipFileCache
));
1074 process_zip_dir (); /* Register all the class defined there */
1081 /* Initialization. */
1086 /* Register roots with the garbage collector. */
1087 ggc_add_tree_root (¤t_field
, 1);
1088 ggc_add_tree_root (¤t_method
, 1);