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