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