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