6853f2ff9433ed2734f881202b7968ff70c276e0
[gcc.git] / gcc / java / jcf-parse.c
1 /* Parser for Java(TM) .class files.
2 Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
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.
16
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.
21
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. */
25
26 /* Written by Per Bothner <bothner@cygnus.com> */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "real.h"
34 #include "obstack.h"
35 #include "flags.h"
36 #include "java-except.h"
37 #include "input.h"
38 #include "java-tree.h"
39 #include "toplev.h"
40 #include "parse.h"
41 #include "ggc.h"
42 #include "debug.h"
43 #include "assert.h"
44
45 #ifdef HAVE_LOCALE_H
46 #include <locale.h>
47 #endif
48
49 #ifdef HAVE_NL_LANGINFO
50 #include <langinfo.h>
51 #endif
52
53 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
54 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
55 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
56 #define JPOOL_UTF_DATA(JCF, INDEX) \
57 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
58 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
59 do { \
60 unsigned char save; unsigned char *text; \
61 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
62 text = (JCF)->read_ptr; \
63 save = text[LENGTH]; \
64 text[LENGTH] = 0; \
65 (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
66 text[LENGTH] = save; \
67 JCF_SKIP (JCF, LENGTH); } while (0)
68
69 #include "jcf.h"
70
71 extern struct obstack temporary_obstack;
72
73 /* Set to nonzero value in order to emit class initilization code
74 before static field references. */
75 extern int always_initialize_class_p;
76
77 static GTY(()) tree parse_roots[3];
78
79 /* The FIELD_DECL for the current field. */
80 #define current_field parse_roots[0]
81
82 /* The METHOD_DECL for the current method. */
83 #define current_method parse_roots[1]
84
85 /* A list of file names. */
86 #define current_file_list parse_roots[2]
87
88 /* The Java archive that provides main_class; the main input file. */
89 static struct JCF main_jcf[1];
90
91 static struct ZipFile *localToFile;
92
93 /* Declarations of some functions used here. */
94 static void handle_innerclass_attribute PARAMS ((int count, JCF *));
95 static tree give_name_to_class PARAMS ((JCF *jcf, int index));
96 static void parse_zip_file_entries PARAMS ((void));
97 static void process_zip_dir PARAMS ((FILE *));
98 static void parse_source_file_1 PARAMS ((tree, FILE *));
99 static void parse_source_file_2 PARAMS ((void));
100 static void parse_source_file_3 PARAMS ((void));
101 static void parse_class_file PARAMS ((void));
102 static void set_source_filename PARAMS ((JCF *, int));
103 static void ggc_mark_jcf PARAMS ((void**));
104 static void jcf_parse PARAMS ((struct JCF*));
105 static void load_inner_classes PARAMS ((tree));
106
107 /* Mark (for garbage collection) all the tree nodes that are
108 referenced from JCF's constant pool table. Do that only if the JCF
109 hasn't been marked finished. */
110
111 static void
112 ggc_mark_jcf (elt)
113 void **elt;
114 {
115 JCF *jcf = *(JCF**) elt;
116 if (jcf != NULL && !jcf->finished)
117 {
118 CPool *cpool = &jcf->cpool;
119 int size = CPOOL_COUNT(cpool);
120 int index;
121 for (index = 1; index < size; index++)
122 {
123 int tag = JPOOL_TAG (jcf, index);
124 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
125 ggc_mark_tree ((tree) cpool->data[index]);
126 }
127 }
128 }
129
130 /* Handle "SourceFile" attribute. */
131
132 static void
133 set_source_filename (jcf, index)
134 JCF *jcf;
135 int index;
136 {
137 tree sfname_id = get_name_constant (jcf, index);
138 const char *sfname = IDENTIFIER_POINTER (sfname_id);
139 if (input_filename != NULL)
140 {
141 int old_len = strlen (input_filename);
142 int new_len = IDENTIFIER_LENGTH (sfname_id);
143 /* Use the current input_filename (derived from the class name)
144 if it has a directory prefix, but otherwise matches sfname. */
145 if (old_len > new_len
146 && strcmp (sfname, input_filename + old_len - new_len) == 0
147 && (input_filename[old_len - new_len - 1] == '/'
148 || input_filename[old_len - new_len - 1] == '\\'))
149 return;
150 }
151 input_filename = sfname;
152 DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
153 if (current_class == main_class) main_input_filename = input_filename;
154 }
155
156 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
157
158 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
159 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
160 current_class = give_name_to_class (jcf, THIS); \
161 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
162
163 #define HANDLE_CLASS_INTERFACE(INDEX) \
164 add_interface (current_class, get_class_constant (jcf, INDEX))
165
166 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
167 { int sig_index = SIGNATURE; \
168 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
169 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
170 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
171 if ((ACCESS_FLAGS) & ACC_FINAL) \
172 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
173 }
174
175 #define HANDLE_END_FIELDS() \
176 (current_field = NULL_TREE)
177
178 #define HANDLE_CONSTANTVALUE(INDEX) \
179 { tree constant; int index = INDEX; \
180 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
181 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
182 constant = build_utf8_ref (name); \
183 } \
184 else \
185 constant = get_constant (jcf, index); \
186 set_constant_value (current_field, constant); }
187
188 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
189 (current_method = add_method (current_class, ACCESS_FLAGS, \
190 get_name_constant (jcf, NAME), \
191 get_name_constant (jcf, SIGNATURE)), \
192 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
193 DECL_LINENUMBERS_OFFSET (current_method) = 0)
194
195 #define HANDLE_END_METHODS() \
196 { current_method = NULL_TREE; }
197
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); }
203
204 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
205 { int n = (COUNT); \
206 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
207 JCF_SKIP (jcf, n * 10); }
208
209 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
210 { int n = (COUNT); \
211 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
212 JCF_SKIP (jcf, n * 4); }
213
214 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
215 { \
216 int n = COUNT; \
217 tree list = DECL_FUNCTION_THROWS (current_method); \
218 while (--n >= 0) \
219 { \
220 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
221 list = tree_cons (NULL_TREE, thrown_class, list); \
222 } \
223 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
224 }
225
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)
230
231 #define HANDLE_SYNTHETIC_ATTRIBUTE() \
232 { \
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; \
239 }
240
241 #define HANDLE_GCJCOMPILED_ATTRIBUTE() \
242 { \
243 if (current_class == object_type_node) \
244 jcf->right_zip = 1; \
245 }
246
247 #include "jcf-reader.c"
248
249 tree
250 parse_signature (jcf, sig_index)
251 JCF *jcf;
252 int sig_index;
253 {
254 if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
255 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
256 abort ();
257 else
258 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
259 JPOOL_UTF_LENGTH (jcf, sig_index));
260 }
261
262 tree
263 get_constant (jcf, index)
264 JCF *jcf;
265 int index;
266 {
267 tree value;
268 int tag;
269 if (index <= 0 || index >= JPOOL_SIZE(jcf))
270 goto bad;
271 tag = JPOOL_TAG (jcf, index);
272 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
273 return (tree) jcf->cpool.data[index];
274 switch (tag)
275 {
276 case CONSTANT_Integer:
277 {
278 jint num = JPOOL_INT(jcf, index);
279 value = build_int_2 (num, num < 0 ? -1 : 0);
280 TREE_TYPE (value) = int_type_node;
281 break;
282 }
283 case CONSTANT_Long:
284 {
285 unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
286 HOST_WIDE_INT lo, hi;
287 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
288 num = JPOOL_UINT (jcf, index+1);
289 add_double (lo, hi, num, 0, &lo, &hi);
290 value = build_int_2 (lo, hi);
291 TREE_TYPE (value) = long_type_node;
292 force_fit_type (value, 0);
293 break;
294 }
295
296 case CONSTANT_Float:
297 {
298 jint num = JPOOL_INT(jcf, index);
299 long buf = num;
300 REAL_VALUE_TYPE d;
301
302 real_from_target_fmt (&d, &buf, &ieee_single_format);
303 value = build_real (float_type_node, d);
304 break;
305 }
306
307 case CONSTANT_Double:
308 {
309 long buf[2], lo, hi;
310 REAL_VALUE_TYPE d;
311
312 hi = JPOOL_UINT (jcf, index);
313 lo = JPOOL_UINT (jcf, index+1);
314
315 if (FLOAT_WORDS_BIG_ENDIAN)
316 buf[0] = hi, buf[1] = lo;
317 else
318 buf[0] = lo, buf[1] = hi;
319
320 real_from_target_fmt (&d, buf, &ieee_double_format);
321 value = build_real (double_type_node, d);
322 break;
323 }
324
325 case CONSTANT_String:
326 {
327 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
328 const char *utf8_ptr = IDENTIFIER_POINTER (name);
329 int utf8_len = IDENTIFIER_LENGTH (name);
330 const unsigned char *utf8;
331 int i;
332
333 /* Check for a malformed Utf8 string. */
334 utf8 = (const unsigned char *) utf8_ptr;
335 i = utf8_len;
336 while (i > 0)
337 {
338 int char_len = UT8_CHAR_LENGTH (*utf8);
339 if (char_len < 0 || char_len > 3 || char_len > i)
340 fatal_error ("bad string constant");
341
342 utf8 += char_len;
343 i -= char_len;
344 }
345
346 /* Allocate a new string value. */
347 value = build_string (utf8_len, utf8_ptr);
348 TREE_TYPE (value) = build_pointer_type (string_type_node);
349 }
350 break;
351 default:
352 goto bad;
353 }
354 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
355 jcf->cpool.data [index] = (jword) value;
356 return value;
357 bad:
358 internal_error ("bad value constant type %d, index %d",
359 JPOOL_TAG (jcf, index), index);
360 }
361
362 tree
363 get_name_constant (jcf, index)
364 JCF *jcf;
365 int index;
366 {
367 tree name = get_constant (jcf, index);
368
369 if (TREE_CODE (name) != IDENTIFIER_NODE)
370 abort ();
371
372 return name;
373 }
374
375 /* Handle reading innerclass attributes. If a nonzero entry (denoting
376 a non anonymous entry) is found, We augment the inner class list of
377 the outer context with the newly resolved innerclass. */
378
379 static void
380 handle_innerclass_attribute (count, jcf)
381 int count;
382 JCF *jcf;
383 {
384 int c = (count);
385 while (c--)
386 {
387 /* Read inner_class_info_index. This may be 0 */
388 int icii = JCF_readu2 (jcf);
389 /* Read outer_class_info_index. If the innerclasses attribute
390 entry isn't a member (like an inner class) the value is 0. */
391 int ocii = JCF_readu2 (jcf);
392 /* Read inner_name_index. If the class we're dealing with is
393 an annonymous class, it must be 0. */
394 int ini = JCF_readu2 (jcf);
395 /* Read the access flag. */
396 int acc = JCF_readu2 (jcf);
397 /* If icii is 0, don't try to read the class. */
398 if (icii >= 0)
399 {
400 tree class = get_class_constant (jcf, icii);
401 tree decl = TYPE_NAME (class);
402 /* Skip reading further if ocii is null */
403 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
404 {
405 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
406 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
407 set_class_decl_access_flags (acc, decl);
408 DECL_CONTEXT (decl) = outer;
409 DECL_INNER_CLASS_LIST (outer) =
410 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
411 CLASS_COMPLETE_P (decl) = 1;
412 }
413 }
414 }
415 }
416
417 static tree
418 give_name_to_class (jcf, i)
419 JCF *jcf;
420 int i;
421 {
422 if (i <= 0 || i >= JPOOL_SIZE (jcf)
423 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
424 abort ();
425 else
426 {
427 tree this_class;
428 int j = JPOOL_USHORT1 (jcf, i);
429 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
430 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
431 JPOOL_UTF_LENGTH (jcf, j));
432 this_class = lookup_class (class_name);
433 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
434 lineno = 0;
435 if (main_input_filename == NULL && jcf == main_jcf)
436 main_input_filename = input_filename;
437
438 jcf->cpool.data[i] = (jword) this_class;
439 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
440 return this_class;
441 }
442 }
443
444 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
445
446 tree
447 get_class_constant (JCF *jcf , int i)
448 {
449 tree type;
450 if (i <= 0 || i >= JPOOL_SIZE (jcf)
451 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
452 abort ();
453
454 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
455 {
456 int name_index = JPOOL_USHORT1 (jcf, i);
457 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
458 const char *name = JPOOL_UTF_DATA (jcf, name_index);
459 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
460
461 if (name[0] == '[') /* Handle array "classes". */
462 type = TREE_TYPE (parse_signature_string (name, nlength));
463 else
464 {
465 tree cname = unmangle_classname (name, nlength);
466 type = lookup_class (cname);
467 }
468 jcf->cpool.data[i] = (jword) type;
469 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
470 }
471 else
472 type = (tree) jcf->cpool.data[i];
473 return type;
474 }
475
476 /* Read a class with the fully qualified-name NAME.
477 Return 1 iff we read the requested file.
478 (It is still possible we failed if the file did not
479 define the class it is supposed to.) */
480
481 int
482 read_class (name)
483 tree name;
484 {
485 JCF this_jcf, *jcf;
486 tree icv, class = NULL_TREE;
487 tree save_current_class = current_class;
488 const char *save_input_filename = input_filename;
489 JCF *save_current_jcf = current_jcf;
490
491 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
492 {
493 class = TREE_TYPE (icv);
494 jcf = TYPE_JCF (class);
495 }
496 else
497 jcf = NULL;
498
499 if (jcf == NULL)
500 {
501 this_jcf.zipd = NULL;
502 jcf = &this_jcf;
503 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
504 &this_jcf, 1) == 0)
505 return 0;
506 }
507
508 current_jcf = jcf;
509
510 if (current_jcf->java_source)
511 {
512 const char *filename = current_jcf->filename;
513 tree file;
514 FILE *finput;
515 int generate;
516
517 java_parser_context_save_global ();
518 java_push_parser_context ();
519 BUILD_FILENAME_IDENTIFIER_NODE (file, filename);
520 generate = IS_A_COMMAND_LINE_FILENAME_P (file);
521 if (wfl_operator == NULL_TREE)
522 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
523 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
524 input_filename = ggc_strdup (filename);
525 current_class = NULL_TREE;
526 current_function_decl = NULL_TREE;
527 if (!HAS_BEEN_ALREADY_PARSED_P (file))
528 {
529 if (!(finput = fopen (input_filename, "r")))
530 fatal_io_error ("can't reopen %s", input_filename);
531 parse_source_file_1 (file, finput);
532 parse_source_file_2 ();
533 parse_source_file_3 ();
534 if (fclose (finput))
535 fatal_io_error ("can't close %s", input_filename);
536 }
537 JCF_FINISH (current_jcf);
538 java_pop_parser_context (generate);
539 java_parser_context_restore_global ();
540 }
541 else
542 {
543 if (class == NULL_TREE || ! CLASS_PARSED_P (class))
544 {
545 java_parser_context_save_global ();
546 java_push_parser_context ();
547 current_class = class;
548 input_filename = current_jcf->filename;
549 if (JCF_SEEN_IN_ZIP (current_jcf))
550 read_zip_member(current_jcf,
551 current_jcf->zipd, current_jcf->zipd->zipf);
552 jcf_parse (current_jcf);
553 class = current_class;
554 java_pop_parser_context (0);
555 java_parser_context_restore_global ();
556 }
557 layout_class (class);
558 load_inner_classes (class);
559 }
560
561 current_class = save_current_class;
562 input_filename = save_input_filename;
563 current_jcf = save_current_jcf;
564 return 1;
565 }
566
567 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
568 called from the parser, otherwise it's a RECORD_TYPE node. If
569 VERBOSE is 1, print error message on failure to load a class. */
570
571 /* Replace calls to load_class by having callers call read_class directly
572 - and then perhaps rename read_class to load_class. FIXME */
573
574 void
575 load_class (class_or_name, verbose)
576 tree class_or_name;
577 int verbose;
578 {
579 tree name, saved;
580 int class_loaded;
581
582 /* class_or_name can be the name of the class we want to load */
583 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
584 name = class_or_name;
585 /* In some cases, it's a dependency that we process earlier that
586 we though */
587 else if (TREE_CODE (class_or_name) == TREE_LIST)
588 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
589 /* Or it's a type in the making */
590 else
591 name = DECL_NAME (TYPE_NAME (class_or_name));
592
593 saved = name;
594 while (1)
595 {
596 char *separator;
597
598 if ((class_loaded = read_class (name)))
599 break;
600
601 /* We failed loading name. Now consider that we might be looking
602 for a inner class. */
603 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
604 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
605 {
606 int c = *separator;
607 *separator = '\0';
608 name = get_identifier (IDENTIFIER_POINTER (name));
609 *separator = c;
610 }
611 /* Otherwise, we failed, we bail. */
612 else
613 break;
614 }
615
616 if (!class_loaded && verbose)
617 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
618 }
619
620 /* Parse the .class file JCF. */
621
622 void
623 jcf_parse (jcf)
624 JCF* jcf;
625 {
626 int i, code;
627
628 if (jcf_parse_preamble (jcf) != 0)
629 fatal_error ("not a valid Java .class file");
630 code = jcf_parse_constant_pool (jcf);
631 if (code != 0)
632 fatal_error ("error while parsing constant pool");
633 code = verify_constant_pool (jcf);
634 if (code > 0)
635 fatal_error ("error in constant pool entry #%d\n", code);
636
637 jcf_parse_class (jcf);
638 if (main_class == NULL_TREE)
639 main_class = current_class;
640 if (! quiet_flag && TYPE_NAME (current_class))
641 fprintf (stderr, " %s %s",
642 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
643 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
644 if (CLASS_PARSED_P (current_class))
645 {
646 /* FIXME - where was first time */
647 fatal_error ("reading class %s for the second time from %s",
648 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
649 jcf->filename);
650 }
651 CLASS_PARSED_P (current_class) = 1;
652
653 for (i = 1; i < JPOOL_SIZE(jcf); i++)
654 {
655 switch (JPOOL_TAG (jcf, i))
656 {
657 case CONSTANT_Class:
658 get_class_constant (jcf, i);
659 break;
660 }
661 }
662
663 code = jcf_parse_fields (jcf);
664 if (code != 0)
665 fatal_error ("error while parsing fields");
666 code = jcf_parse_methods (jcf);
667 if (code != 0)
668 fatal_error ("error while parsing methods");
669 code = jcf_parse_final_attributes (jcf);
670 if (code != 0)
671 fatal_error ("error while parsing final attributes");
672
673 /* The fields of class_type_node are already in correct order. */
674 if (current_class != class_type_node && current_class != object_type_node)
675 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
676
677 if (current_class == object_type_node)
678 {
679 layout_class_methods (object_type_node);
680 /* If we don't have the right archive, emit a verbose warning.
681 If we're generating bytecode, emit the warning only if
682 -fforce-classes-archive-check was specified. */
683 if (!jcf->right_zip
684 && (!flag_emit_class_files || flag_force_classes_archive_check))
685 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);
686 }
687 else
688 all_class_list = tree_cons (NULL_TREE,
689 TYPE_NAME (current_class), all_class_list );
690 }
691
692 /* If we came across inner classes, load them now. */
693 static void
694 load_inner_classes (cur_class)
695 tree cur_class;
696 {
697 tree current;
698 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
699 current = TREE_CHAIN (current))
700 {
701 tree name = DECL_NAME (TREE_PURPOSE (current));
702 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
703 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
704 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
705 load_class (name, 1);
706 }
707 }
708
709 void
710 init_outgoing_cpool ()
711 {
712 outgoing_cpool = xmalloc (sizeof (struct CPool));
713 memset (outgoing_cpool, 0, sizeof (struct CPool));
714 }
715
716 static void
717 parse_class_file ()
718 {
719 tree method, field;
720 const char *save_input_filename = input_filename;
721 int save_lineno = lineno;
722
723 java_layout_seen_class_methods ();
724
725 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
726 lineno = 0;
727 (*debug_hooks->start_source_file) (lineno, input_filename);
728 init_outgoing_cpool ();
729
730 /* Currently we always have to emit calls to _Jv_InitClass when
731 compiling from class files. */
732 always_initialize_class_p = 1;
733
734 for (field = TYPE_FIELDS (current_class);
735 field != NULL_TREE; field = TREE_CHAIN (field))
736 if (FIELD_STATIC (field))
737 DECL_EXTERNAL (field) = 0;
738
739 for (method = TYPE_METHODS (current_class);
740 method != NULL_TREE; method = TREE_CHAIN (method))
741 {
742 JCF *jcf = current_jcf;
743
744 if (METHOD_ABSTRACT (method))
745 continue;
746
747 if (METHOD_NATIVE (method))
748 {
749 tree arg;
750 int decl_max_locals;
751
752 if (! flag_jni)
753 continue;
754 /* We need to compute the DECL_MAX_LOCALS. We need to take
755 the wide types into account too. */
756 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
757 arg != end_params_node;
758 arg = TREE_CHAIN (arg), decl_max_locals += 1)
759 {
760 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
761 decl_max_locals += 1;
762 }
763 DECL_MAX_LOCALS (method) = decl_max_locals;
764 start_java_method (method);
765 give_name_to_locals (jcf);
766 expand_expr_stmt (build_jni_stub (method));
767 end_java_method ();
768 continue;
769 }
770
771 if (DECL_CODE_OFFSET (method) == 0)
772 {
773 current_function_decl = method;
774 error ("missing Code attribute");
775 continue;
776 }
777
778 lineno = 0;
779 if (DECL_LINENUMBERS_OFFSET (method))
780 {
781 register int i;
782 register unsigned char *ptr;
783 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
784 linenumber_count = i = JCF_readu2 (jcf);
785 linenumber_table = ptr = jcf->read_ptr;
786
787 for (ptr += 2; --i >= 0; ptr += 4)
788 {
789 int line = GET_u2 (ptr);
790 /* Set initial lineno lineno to smallest linenumber.
791 * Needs to be set before init_function_start. */
792 if (lineno == 0 || line < lineno)
793 lineno = line;
794 }
795 }
796 else
797 {
798 linenumber_table = NULL;
799 linenumber_count = 0;
800 }
801
802 start_java_method (method);
803
804 note_instructions (jcf, method);
805
806 give_name_to_locals (jcf);
807
808 /* Actually generate code. */
809 expand_byte_code (jcf, method);
810
811 end_java_method ();
812 }
813
814 if (flag_emit_class_files)
815 write_classfile (current_class);
816
817 finish_class ();
818
819 (*debug_hooks->end_source_file) (save_lineno);
820 input_filename = save_input_filename;
821 lineno = save_lineno;
822 }
823
824 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
825
826 static void
827 parse_source_file_1 (file, finput)
828 tree file;
829 FILE *finput;
830 {
831 int save_error_count = java_error_count;
832 /* Mark the file as parsed */
833 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
834
835 jcf_dependency_add_file (input_filename, 0);
836
837 lang_init_source (1); /* Error msgs have no method prototypes */
838
839 /* There's no point in trying to find the current encoding unless we
840 are going to do something intelligent with it -- hence the test
841 for iconv. */
842 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_NL_LANGINFO)
843 setlocale (LC_CTYPE, "");
844 if (current_encoding == NULL)
845 current_encoding = nl_langinfo (CODESET);
846 #endif
847 if (current_encoding == NULL || *current_encoding == '\0')
848 current_encoding = DEFAULT_ENCODING;
849
850 /* Initialize the parser */
851 java_init_lex (finput, current_encoding);
852 java_parse_abort_on_error ();
853
854 java_parse (); /* Parse and build partial tree nodes. */
855 java_parse_abort_on_error ();
856 }
857
858 /* Process a parsed source file, resolving names etc. */
859
860 static void
861 parse_source_file_2 ()
862 {
863 int save_error_count = java_error_count;
864 java_complete_class (); /* Parse unsatisfied class decl. */
865 java_parse_abort_on_error ();
866 }
867
868 static void
869 parse_source_file_3 ()
870 {
871 int save_error_count = java_error_count;
872 java_check_circular_reference (); /* Check on circular references */
873 java_parse_abort_on_error ();
874 java_fix_constructors (); /* Fix the constructors */
875 java_parse_abort_on_error ();
876 java_reorder_fields (); /* Reorder the fields */
877 }
878
879 void
880 add_predefined_file (name)
881 tree name;
882 {
883 predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
884 }
885
886 int
887 predefined_filename_p (node)
888 tree node;
889 {
890 tree iter;
891
892 for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
893 {
894 if (TREE_VALUE (iter) == node)
895 return 1;
896 }
897 return 0;
898 }
899
900 void
901 java_parse_file (set_yydebug)
902 int set_yydebug ATTRIBUTE_UNUSED;
903 {
904 int filename_count = 0;
905 char *list, *next;
906 tree node;
907 FILE *finput = NULL;
908
909 if (flag_filelist_file)
910 {
911 int avail = 2000;
912 finput = fopen (input_filename, "r");
913 if (finput == NULL)
914 fatal_io_error ("can't open %s", input_filename);
915 list = xmalloc(avail);
916 next = list;
917 for (;;)
918 {
919 int count;
920 if (avail < 500)
921 {
922 count = next - list;
923 avail = 2 * (count + avail);
924 list = xrealloc (list, avail);
925 next = list + count;
926 avail = avail - count;
927 }
928 /* Subtract to to guarantee space for final '\0'. */
929 count = fread (next, 1, avail - 1, finput);
930 if (count == 0)
931 {
932 if (! feof (finput))
933 fatal_io_error ("error closing %s", input_filename);
934 *next = '\0';
935 break;
936 }
937 avail -= count;
938 next += count;
939 }
940 fclose (finput);
941 finput = NULL;
942 }
943 else
944 list = xstrdup (input_filename);
945
946 do
947 {
948 for (next = list; ; )
949 {
950 char ch = *next;
951 if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
952 || ch == '&' /* FIXME */)
953 {
954 if (next == list)
955 {
956 next++;
957 list = next;
958 continue;
959 }
960 else
961 {
962 *next++ = '\0';
963 break;
964 }
965 }
966 if (ch == '\0')
967 {
968 next = NULL;
969 break;
970 }
971 next++;
972 }
973
974 if (list[0])
975 {
976 char *value;
977 tree id;
978 int twice = 0;
979
980 int len = strlen (list);
981
982 obstack_grow0 (&temporary_obstack, list, len);
983 value = obstack_finish (&temporary_obstack);
984
985 filename_count++;
986
987 /* Exclude file that we see twice on the command line. For
988 all files except {Class,Error,Object,RuntimeException,String,
989 Throwable}.java we can rely on maybe_get_identifier. For
990 these files, we need to do a linear search of
991 current_file_list. This search happens only for these
992 files, presumably only when we're recompiling libgcj. */
993
994 if ((id = maybe_get_identifier (value)))
995 {
996 if (predefined_filename_p (id))
997 {
998 tree c;
999 for (c = current_file_list; c; c = TREE_CHAIN (c))
1000 if (TREE_VALUE (c) == id)
1001 twice = 1;
1002 }
1003 else
1004 twice = 1;
1005 }
1006
1007 if (twice)
1008 {
1009 const char *saved_input_filename = input_filename;
1010 input_filename = value;
1011 warning ("source file seen twice on command line and will be compiled only once");
1012 input_filename = saved_input_filename;
1013 }
1014 else
1015 {
1016 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
1017 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1018 current_file_list = tree_cons (NULL_TREE, node,
1019 current_file_list);
1020 }
1021 }
1022 list = next;
1023 }
1024 while (next);
1025
1026 if (filename_count == 0)
1027 warning ("no input file specified");
1028
1029 if (resource_name)
1030 {
1031 const char *resource_filename;
1032
1033 /* Only one resource file may be compiled at a time. */
1034 assert (TREE_CHAIN (current_file_list) == NULL);
1035
1036 resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
1037 compile_resource_file (resource_name, resource_filename);
1038
1039 return;
1040 }
1041
1042 current_jcf = main_jcf;
1043 current_file_list = nreverse (current_file_list);
1044 for (node = current_file_list; node; node = TREE_CHAIN (node))
1045 {
1046 unsigned char magic_string[4];
1047 uint32 magic = 0;
1048 tree name = TREE_VALUE (node);
1049
1050 /* Skip already parsed files */
1051 if (HAS_BEEN_ALREADY_PARSED_P (name))
1052 continue;
1053
1054 /* Close previous descriptor, if any */
1055 if (finput && fclose (finput))
1056 fatal_io_error ("can't close input file %s", main_input_filename);
1057
1058 finput = fopen (IDENTIFIER_POINTER (name), "rb");
1059 if (finput == NULL)
1060 fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
1061
1062 #ifdef IO_BUFFER_SIZE
1063 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1064 _IOFBF, IO_BUFFER_SIZE);
1065 #endif
1066 input_filename = IDENTIFIER_POINTER (name);
1067
1068 /* Figure what kind of file we're dealing with */
1069 if (fread (magic_string, 1, 4, finput) == 4)
1070 {
1071 fseek (finput, 0L, SEEK_SET);
1072 magic = GET_u4 (magic_string);
1073 }
1074 if (magic == 0xcafebabe)
1075 {
1076 CLASS_FILE_P (node) = 1;
1077 current_jcf = ALLOC (sizeof (JCF));
1078 JCF_ZERO (current_jcf);
1079 current_jcf->read_state = finput;
1080 current_jcf->filbuf = jcf_filbuf_from_stdio;
1081 jcf_parse (current_jcf);
1082 TYPE_JCF (current_class) = current_jcf;
1083 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1084 TREE_PURPOSE (node) = current_class;
1085 }
1086 else if (magic == (JCF_u4)ZIPMAGIC)
1087 {
1088 ZIP_FILE_P (node) = 1;
1089 JCF_ZERO (main_jcf);
1090 main_jcf->read_state = finput;
1091 main_jcf->filbuf = jcf_filbuf_from_stdio;
1092 if (open_in_zip (main_jcf, input_filename, NULL, 0) < 0)
1093 fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1094 localToFile = SeenZipFiles;
1095 /* Register all the class defined there. */
1096 process_zip_dir (main_jcf->read_state);
1097 parse_zip_file_entries ();
1098 /*
1099 for (each entry)
1100 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1101 */
1102 }
1103 else
1104 {
1105 JAVA_FILE_P (node) = 1;
1106 java_push_parser_context ();
1107 java_parser_context_save_global ();
1108 parse_source_file_1 (name, finput);
1109 java_parser_context_restore_global ();
1110 java_pop_parser_context (1);
1111 }
1112 }
1113
1114 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1115 {
1116 input_filename = ctxp->filename;
1117 parse_source_file_2 ();
1118 }
1119
1120 for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1121 {
1122 input_filename = ctxp->filename;
1123 parse_source_file_3 ();
1124 }
1125
1126 for (node = current_file_list; node; node = TREE_CHAIN (node))
1127 {
1128 input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1129 if (CLASS_FILE_P (node))
1130 {
1131 current_class = TREE_PURPOSE (node);
1132 current_jcf = TYPE_JCF (current_class);
1133 layout_class (current_class);
1134 load_inner_classes (current_class);
1135 parse_class_file ();
1136 JCF_FINISH (current_jcf);
1137 }
1138 }
1139 input_filename = main_input_filename;
1140
1141 java_expand_classes ();
1142 if (!java_report_errors () && !flag_syntax_only)
1143 {
1144 emit_register_classes ();
1145 if (flag_indirect_dispatch)
1146 emit_offset_symbol_table ();
1147 }
1148 }
1149
1150 /* Process all class entries found in the zip file. */
1151 static void
1152 parse_zip_file_entries (void)
1153 {
1154 struct ZipDirectory *zdir;
1155 int i;
1156
1157 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1158 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1159 {
1160 tree class;
1161
1162 /* We don't need to consider those files. */
1163 if (!zdir->size || !zdir->filename_offset)
1164 continue;
1165
1166 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
1167 current_jcf = TYPE_JCF (class);
1168 current_class = class;
1169
1170 if ( !CLASS_LOADED_P (class))
1171 {
1172 if (! CLASS_PARSED_P (class))
1173 {
1174 read_zip_member(current_jcf, zdir, localToFile);
1175 jcf_parse (current_jcf);
1176 }
1177 layout_class (current_class);
1178 load_inner_classes (current_class);
1179 }
1180
1181 if (TYPE_SIZE (current_class) != error_mark_node)
1182 {
1183 input_filename = current_jcf->filename;
1184 parse_class_file ();
1185 FREE (current_jcf->buffer); /* No longer necessary */
1186 /* Note: there is a way to free this buffer right after a
1187 class seen in a zip file has been parsed. The idea is the
1188 set its jcf in such a way that buffer will be reallocated
1189 the time the code for the class will be generated. FIXME. */
1190 }
1191 }
1192 }
1193
1194 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1195 jcf up for further processing and link it to the created class. */
1196
1197 static void
1198 process_zip_dir (FILE *finput)
1199 {
1200 int i;
1201 ZipDirectory *zdir;
1202
1203 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1204 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1205 {
1206 char *class_name, *file_name, *class_name_in_zip_dir;
1207 tree class;
1208 JCF *jcf;
1209 int j;
1210
1211 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1212
1213 /* We choose to not to process entries with a zero size or entries
1214 not bearing the .class extension. */
1215 if (!zdir->size || !zdir->filename_offset ||
1216 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
1217 ".class", 6))
1218 {
1219 /* So it will be skipped in parse_zip_file_entries */
1220 zdir->size = 0;
1221 continue;
1222 }
1223
1224 class_name = ALLOC (zdir->filename_length+1-6);
1225 file_name = ALLOC (zdir->filename_length+1);
1226 jcf = ALLOC (sizeof (JCF));
1227 JCF_ZERO (jcf);
1228
1229 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1230 class_name [zdir->filename_length-6] = '\0';
1231 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1232 file_name [zdir->filename_length] = '\0';
1233
1234 for (j=0; class_name[j]; j++)
1235 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1236
1237 /* Yes, we write back the true class name into the zip directory. */
1238 strcpy (class_name_in_zip_dir, class_name);
1239 zdir->filename_length = j;
1240 class = lookup_class (get_identifier (class_name));
1241
1242 jcf->read_state = finput;
1243 jcf->filbuf = jcf_filbuf_from_stdio;
1244 jcf->java_source = 0;
1245 jcf->classname = class_name;
1246 jcf->filename = file_name;
1247 jcf->zipd = zdir;
1248
1249 TYPE_JCF (class) = jcf;
1250 }
1251 }
1252
1253 /* Initialization. */
1254
1255 void
1256 init_jcf_parse ()
1257 {
1258 /* Register roots with the garbage collector. */
1259 ggc_add_root (&current_jcf, 1, sizeof (JCF), (void (*)(void *))ggc_mark_jcf);
1260
1261 init_src_parse ();
1262 }
1263
1264 #include "gt-java-jcf-parse.h"