]> gcc.gnu.org Git - gcc.git/blob - gcc/java/jcf-parse.c
Minor optimization of static ggc roots.
[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 tree give_name_to_class PARAMS ((JCF *jcf, int index));
89 static void parse_zip_file_entries PARAMS ((void));
90 static void process_zip_dir PARAMS ((FILE *));
91 static void parse_source_file PARAMS ((tree, FILE *));
92 static void jcf_parse_source PARAMS ((void));
93 static int jcf_figure_file_type PARAMS ((JCF *));
94 static int find_in_current_zip PARAMS ((const char *, struct JCF **));
95 static void parse_class_file PARAMS ((void));
96 static void set_source_filename PARAMS ((JCF *, int));
97 static int predefined_filename_p PARAMS ((tree));
98 static void ggc_mark_jcf PARAMS ((void**));
99
100 /* Mark (for garbage collection) all the tree nodes that are
101 referenced from JCF's constant pool table. */
102
103 static void
104 ggc_mark_jcf (elt)
105 void **elt;
106 {
107 JCF *jcf = *(JCF**) elt;
108 if (jcf != NULL)
109 {
110 CPool *cpool = &jcf->cpool;
111 int size = CPOOL_COUNT(cpool);
112 int index;
113 for (index = 1; index < size; index++)
114 {
115 int tag = JPOOL_TAG (jcf, index);
116 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
117 ggc_mark_tree ((tree) cpool->data[index]);
118 }
119 }
120 }
121
122 /* Handle "SourceFile" attribute. */
123
124 static void
125 set_source_filename (jcf, index)
126 JCF *jcf;
127 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 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
164 #define HANDLE_END_FIELDS() \
165 (current_field = NULL_TREE)
166
167 #define HANDLE_CONSTANTVALUE(INDEX) \
168 { tree constant; int index = INDEX; \
169 if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
170 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
171 constant = build_utf8_ref (name); \
172 } \
173 else \
174 constant = get_constant (jcf, index); \
175 set_constant_value (current_field, constant); }
176
177 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
178 (current_method = add_method (current_class, ACCESS_FLAGS, \
179 get_name_constant (jcf, NAME), \
180 get_name_constant (jcf, SIGNATURE)), \
181 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
182 DECL_LINENUMBERS_OFFSET (current_method) = 0)
183
184 #define HANDLE_END_METHODS() \
185 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
186 if (handle_type != current_class) layout_type (handle_type); }
187
188 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
189 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
190 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
191 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
192 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
193
194 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
195 { int n = (COUNT); \
196 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
197 JCF_SKIP (jcf, n * 10); }
198
199 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
200 { int n = (COUNT); \
201 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
202 JCF_SKIP (jcf, n * 4); }
203
204 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
205 { \
206 int n = COUNT; \
207 tree list = DECL_FUNCTION_THROWS (current_method); \
208 while (--n >= 0) \
209 { \
210 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
211 list = tree_cons (NULL_TREE, thrown_class, list); \
212 } \
213 DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
214 }
215
216 /* Link seen inner classes to their outer context and register the
217 inner class to its outer context. They will be later loaded. */
218 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
219 { \
220 int c = (count); \
221 while (c--) \
222 { \
223 tree class = get_class_constant (jcf, JCF_readu2 (jcf)); \
224 tree decl = TYPE_NAME (class); \
225 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl)) \
226 { \
227 tree outer = TYPE_NAME (get_class_constant (jcf, \
228 JCF_readu2 (jcf))); \
229 tree alias = get_name_constant (jcf, JCF_readu2 (jcf)); \
230 JCF_SKIP (jcf, 2); \
231 DECL_CONTEXT (decl) = outer; \
232 DECL_INNER_CLASS_LIST (outer) = \
233 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer)); \
234 CLASS_COMPLETE_P (decl) = 1; \
235 } \
236 else \
237 JCF_SKIP (jcf, 6); \
238 } \
239 }
240
241 #include "jcf-reader.c"
242
243 static int yydebug;
244
245 tree
246 parse_signature (jcf, sig_index)
247 JCF *jcf;
248 int sig_index;
249 {
250 if (sig_index <= 0 || sig_index >= JPOOL_SIZE(jcf)
251 || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
252 fatal ("invalid field/method signature");
253 else
254 {
255 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
256 JPOOL_UTF_LENGTH (jcf, sig_index));
257 }
258 }
259
260 void
261 init_lex ()
262 {
263 /* Make identifier nodes long enough for the language-specific slots. */
264 set_identifier_size (sizeof (struct lang_identifier));
265 }
266
267 void
268 set_yydebug (value)
269 int value;
270 {
271 yydebug = value;
272 }
273
274 tree
275 get_constant (jcf, index)
276 JCF *jcf;
277 int index;
278 {
279 tree value;
280 int tag;
281 if (index <= 0 || index >= JPOOL_SIZE(jcf))
282 goto bad;
283 tag = JPOOL_TAG (jcf, index);
284 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
285 return (tree) jcf->cpool.data[index];
286 switch (tag)
287 {
288 case CONSTANT_Integer:
289 {
290 jint num = JPOOL_INT(jcf, index);
291 value = build_int_2 (num, num < 0 ? -1 : 0);
292 TREE_TYPE (value) = int_type_node;
293 break;
294 }
295 case CONSTANT_Long:
296 {
297 jint num = JPOOL_INT (jcf, index);
298 HOST_WIDE_INT lo, hi;
299 lshift_double (num, 0, 32, 64, &lo, &hi, 0);
300 num = JPOOL_INT (jcf, index+1) & 0xffffffff;
301 add_double (lo, hi, num, 0, &lo, &hi);
302 value = build_int_2 (lo, hi);
303 TREE_TYPE (value) = long_type_node;
304 force_fit_type (value, 0);
305 break;
306 }
307 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
308 case CONSTANT_Float:
309 {
310 jint num = JPOOL_INT(jcf, index);
311 REAL_VALUE_TYPE d;
312 #ifdef REAL_ARITHMETIC
313 d = REAL_VALUE_FROM_TARGET_SINGLE (num);
314 #else
315 union { float f; jint i; } u;
316 u.i = num;
317 d = u.f;
318 #endif
319 value = build_real (float_type_node, d);
320 break;
321 }
322 case CONSTANT_Double:
323 {
324 HOST_WIDE_INT num[2];
325 REAL_VALUE_TYPE d;
326 HOST_WIDE_INT lo, hi;
327 num[0] = JPOOL_INT (jcf, index);
328 lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
329 num[0] = JPOOL_INT (jcf, index+1);
330 add_double (lo, hi, num[0], 0, &lo, &hi);
331 if (FLOAT_WORDS_BIG_ENDIAN)
332 {
333 num[0] = hi;
334 num[1] = lo;
335 }
336 else
337 {
338 num[0] = lo;
339 num[1] = hi;
340 }
341 #ifdef REAL_ARITHMETIC
342 d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
343 #else
344 {
345 union { double d; jint i[2]; } u;
346 u.i[0] = (jint) num[0];
347 u.i[1] = (jint) num[1];
348 d = u.d;
349 }
350 #endif
351 value = build_real (double_type_node, d);
352 break;
353 }
354 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
355 case CONSTANT_String:
356 {
357 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
358 const char *utf8_ptr = IDENTIFIER_POINTER (name);
359 int utf8_len = IDENTIFIER_LENGTH (name);
360 unsigned char *str_ptr;
361 unsigned char *str;
362 const unsigned char *utf8;
363 int i, str_len;
364
365 /* Count the number of Unicode characters in the string,
366 while checking for a malformed Utf8 string. */
367 utf8 = (const unsigned char *) utf8_ptr;
368 i = utf8_len;
369 str_len = 0;
370 while (i > 0)
371 {
372 int char_len = UT8_CHAR_LENGTH (*utf8);
373 if (char_len < 0 || char_len > 3 || char_len > i)
374 fatal ("bad string constant");
375 utf8 += char_len;
376 i -= char_len;
377 str_len++;
378 }
379
380 /* Allocate a scratch buffer, convert the string to UCS2, and copy it
381 into the new space. */
382 str_ptr = (unsigned char *) alloca (2 * str_len);
383 str = str_ptr;
384 utf8 = (const unsigned char *)utf8_ptr;
385
386 for (i = 0; i < str_len; i++)
387 {
388 int char_value;
389 int char_len = UT8_CHAR_LENGTH (*utf8);
390 switch (char_len)
391 {
392 case 1:
393 char_value = *utf8++;
394 break;
395 case 2:
396 char_value = *utf8++ & 0x1F;
397 char_value = (char_value << 6) | (*utf8++ & 0x3F);
398 break;
399 case 3:
400 char_value = *utf8++ & 0x0F;
401 char_value = (char_value << 6) | (*utf8++ & 0x3F);
402 char_value = (char_value << 6) | (*utf8++ & 0x3F);
403 break;
404 default:
405 goto bad;
406 }
407 if (BYTES_BIG_ENDIAN)
408 {
409 *str++ = char_value >> 8;
410 *str++ = char_value & 0xFF;
411 }
412 else
413 {
414 *str++ = char_value & 0xFF;
415 *str++ = char_value >> 8;
416 }
417 }
418 value = build_string (str - str_ptr, str_ptr);
419 TREE_TYPE (value) = build_pointer_type (string_type_node);
420 }
421 break;
422 default:
423 goto bad;
424 }
425 JPOOL_TAG(jcf, index) = tag | CONSTANT_ResolvedFlag;
426 jcf->cpool.data [index] = (jword) value;
427 return value;
428 bad:
429 fatal ("bad value constant type %d, index %d",
430 JPOOL_TAG( jcf, index ), index);
431 }
432
433 tree
434 get_name_constant (jcf, index)
435 JCF *jcf;
436 int index;
437 {
438 tree name = get_constant (jcf, index);
439 if (TREE_CODE (name) != IDENTIFIER_NODE)
440 fatal ("bad nameandtype index %d", index);
441 return name;
442 }
443
444 static tree
445 give_name_to_class (jcf, i)
446 JCF *jcf;
447 int i;
448 {
449 if (i <= 0 || i >= JPOOL_SIZE(jcf)
450 || JPOOL_TAG (jcf, i) != CONSTANT_Class)
451 fatal ("bad class index %d", i);
452 else
453 {
454 tree this_class;
455 int j = JPOOL_USHORT1 (jcf, i);
456 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
457 tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
458 JPOOL_UTF_LENGTH (jcf, j));
459 this_class = lookup_class (class_name);
460 input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
461 lineno = 0;
462 if (main_input_filename == NULL && jcf == main_jcf)
463 main_input_filename = input_filename;
464
465 jcf->cpool.data[i] = (jword) this_class;
466 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
467 return this_class;
468 }
469 }
470
471 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
472
473 tree
474 get_class_constant (JCF *jcf , int i)
475 {
476 tree type;
477 if (i <= 0 || i >= JPOOL_SIZE(jcf)
478 || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
479 fatal ("bad class index %d", i);
480
481 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
482 {
483 int name_index = JPOOL_USHORT1 (jcf, i);
484 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
485 const char *name = JPOOL_UTF_DATA (jcf, name_index);
486 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
487 if (name[0] == '[') /* Handle array "classes". */
488 type = TREE_TYPE (parse_signature_string (name, nlength));
489 else
490 {
491 tree cname = unmangle_classname (name, nlength);
492 type = lookup_class (cname);
493 }
494 jcf->cpool.data[i] = (jword) type;
495 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
496 }
497 else
498 type = (tree) jcf->cpool.data[i];
499 return type;
500 }
501
502 /* Read a class with the fully qualified-name NAME.
503 Return 1 iff we read the requested file.
504 (It is still possible we failed if the file did not
505 define the class it is supposed to.) */
506
507 int
508 read_class (name)
509 tree name;
510 {
511 JCF this_jcf, *jcf;
512 tree save_current_class = current_class;
513 const char *save_input_filename = input_filename;
514 JCF *save_current_jcf = current_jcf;
515 long saved_pos = 0;
516 if (current_jcf->read_state)
517 saved_pos = ftell (current_jcf->read_state);
518
519 /* Search in current zip first. */
520 if (find_in_current_zip (IDENTIFIER_POINTER (name), &jcf) == 0)
521 {
522 if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
523 &this_jcf, 1) == 0)
524 return 0;
525 else
526 {
527 this_jcf.seen_in_zip = 0;
528 current_jcf = &this_jcf;
529 }
530 }
531 else
532 current_jcf = jcf;
533
534 if (current_jcf->java_source)
535 jcf_parse_source ();
536 else {
537 java_parser_context_save_global ();
538 java_push_parser_context ();
539 input_filename = current_jcf->filename;
540 jcf_parse (current_jcf);
541 java_pop_parser_context (0);
542 java_parser_context_restore_global ();
543 }
544
545 if (!current_jcf->seen_in_zip)
546 JCF_FINISH (current_jcf);
547
548 current_class = save_current_class;
549 input_filename = save_input_filename;
550 current_jcf = save_current_jcf;
551 if (current_jcf->read_state)
552 fseek (current_jcf->read_state, saved_pos, SEEK_SET);
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 (class_or_name, verbose)
565 tree class_or_name;
566 int verbose;
567 {
568 tree name;
569
570 /* class_or_name can be the name of the class we want to load */
571 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
572 name = class_or_name;
573 /* In some cases, it's a dependency that we process earlier that
574 we though */
575 else if (TREE_CODE (class_or_name) == TREE_LIST)
576 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
577 /* Or it's a type in the making */
578 else
579 name = DECL_NAME (TYPE_NAME (class_or_name));
580
581 if (read_class (name) == 0 && verbose)
582 fatal ("Cannot find file for class %s.", IDENTIFIER_POINTER (name));
583 }
584
585 /* Parse a source file when JCF refers to a source file. */
586
587 static void
588 jcf_parse_source ()
589 {
590 tree file;
591 FILE *finput;
592
593 java_parser_context_save_global ();
594 java_push_parser_context ();
595 BUILD_FILENAME_IDENTIFIER_NODE (file, current_jcf->filename);
596 if (wfl_operator == NULL_TREE)
597 wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
598 EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
599 input_filename = ggc_strdup (current_jcf->filename);
600 current_class = NULL_TREE;
601 current_function_decl = NULL_TREE;
602 if (!HAS_BEEN_ALREADY_PARSED_P (file))
603 {
604 if (!(finput = fopen (input_filename, "r")))
605 fatal ("input file `%s' just disappeared - jcf_parse_source",
606 input_filename);
607 parse_source_file (file, finput);
608 if (fclose (finput))
609 fatal ("can't close input file `%s' stream - jcf_parse_source",
610 input_filename);
611 }
612 java_pop_parser_context (IS_A_COMMAND_LINE_FILENAME_P (file));
613 java_parser_context_restore_global ();
614 }
615
616 /* Parse the .class file JCF. */
617
618 void
619 jcf_parse (jcf)
620 JCF* jcf;
621 {
622 int i, code;
623 tree current;
624
625 if (jcf_parse_preamble (jcf) != 0)
626 fatal ("Not a valid Java .class file.\n");
627 code = jcf_parse_constant_pool (jcf);
628 if (code != 0)
629 fatal ("error while parsing constant pool");
630 code = verify_constant_pool (jcf);
631 if (code > 0)
632 fatal ("error in constant pool entry #%d\n", code);
633
634 jcf_parse_class (jcf);
635 if (main_class == NULL_TREE)
636 main_class = current_class;
637 if (! quiet_flag && TYPE_NAME (current_class))
638 fprintf (stderr, " %s %s",
639 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
640 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
641 if (CLASS_LOADED_P (current_class))
642 return;
643 CLASS_LOADED_P (current_class) = 1;
644
645 for (i = 1; i < JPOOL_SIZE(jcf); i++)
646 {
647 switch (JPOOL_TAG (jcf, i))
648 {
649 case CONSTANT_Class:
650 get_class_constant (jcf, i);
651 break;
652 }
653 }
654
655 code = jcf_parse_fields (jcf);
656 if (code != 0)
657 fatal ("error while parsing fields");
658 code = jcf_parse_methods (jcf);
659 if (code != 0)
660 fatal ("error while parsing methods");
661 code = jcf_parse_final_attributes (jcf);
662 if (code != 0)
663 fatal ("error while parsing final attributes");
664
665 /* The fields of class_type_node are already in correct order. */
666 if (current_class != class_type_node && current_class != object_type_node)
667 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
668
669 layout_class (current_class);
670 if (current_class == object_type_node)
671 layout_class_methods (object_type_node);
672 else
673 all_class_list = tree_cons (NULL_TREE,
674 TYPE_NAME (current_class), all_class_list );
675
676 /* And if we came accross inner classes, load them now. */
677 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (current_class)); current;
678 current = TREE_CHAIN (current))
679 load_class (DECL_NAME (TREE_PURPOSE (current)), 1);
680 }
681
682 void
683 init_outgoing_cpool ()
684 {
685 current_constant_pool_data_ref = NULL_TREE;
686 outgoing_cpool = (struct CPool *)xmalloc (sizeof (struct CPool));
687 memset (outgoing_cpool, 0, sizeof (struct CPool));
688 }
689
690 static void
691 parse_class_file ()
692 {
693 tree method;
694 const char *save_input_filename = input_filename;
695 int save_lineno = lineno;
696
697 java_layout_seen_class_methods ();
698
699 input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
700 lineno = 0;
701 debug_start_source_file (input_filename);
702 init_outgoing_cpool ();
703
704 /* Currently we always have to emit calls to _Jv_InitClass when
705 compiling from class files. */
706 always_initialize_class_p = 1;
707
708 for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
709 method != NULL_TREE; method = TREE_CHAIN (method))
710 {
711 JCF *jcf = current_jcf;
712
713 if (METHOD_ABSTRACT (method))
714 continue;
715
716 if (METHOD_NATIVE (method))
717 {
718 if (! flag_jni)
719 continue;
720 DECL_MAX_LOCALS (method)
721 = list_length (TYPE_ARG_TYPES (TREE_TYPE (method)));
722 start_java_method (method);
723 give_name_to_locals (jcf);
724 expand_expr_stmt (build_jni_stub (method));
725 end_java_method ();
726 continue;
727 }
728
729 if (DECL_CODE_OFFSET (method) == 0)
730 {
731 error ("missing Code attribute");
732 continue;
733 }
734
735 lineno = 0;
736 if (DECL_LINENUMBERS_OFFSET (method))
737 {
738 register int i;
739 register unsigned char *ptr;
740 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
741 linenumber_count = i = JCF_readu2 (jcf);
742 linenumber_table = ptr = jcf->read_ptr;
743
744 for (ptr += 2; --i >= 0; ptr += 4)
745 {
746 int line = GET_u2 (ptr);
747 /* Set initial lineno lineno to smallest linenumber.
748 * Needs to be set before init_function_start. */
749 if (lineno == 0 || line < lineno)
750 lineno = line;
751 }
752 }
753 else
754 {
755 linenumber_table = NULL;
756 linenumber_count = 0;
757 }
758
759 start_java_method (method);
760
761 note_instructions (jcf, method);
762
763 give_name_to_locals (jcf);
764
765 /* Actually generate code. */
766 expand_byte_code (jcf, method);
767
768 end_java_method ();
769 }
770
771 if (flag_emit_class_files)
772 write_classfile (current_class);
773
774 finish_class ();
775
776 debug_end_source_file (save_lineno);
777 input_filename = save_input_filename;
778 lineno = save_lineno;
779 }
780
781 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
782
783 static void
784 parse_source_file (file, finput)
785 tree file;
786 FILE *finput;
787 {
788 int save_error_count = java_error_count;
789 /* Mark the file as parsed */
790 HAS_BEEN_ALREADY_PARSED_P (file) = 1;
791
792 jcf_dependency_add_file (input_filename, 0);
793
794 lang_init_source (1); /* Error msgs have no method prototypes */
795
796 /* There's no point in trying to find the current encoding unless we
797 are going to do something intelligent with it -- hence the test
798 for iconv. */
799 #ifdef HAVE_ICONV
800 #ifdef HAVE_NL_LANGINFO
801 setlocale (LC_CTYPE, "");
802 if (current_encoding == NULL)
803 current_encoding = nl_langinfo (CODESET);
804 #endif /* HAVE_NL_LANGINFO */
805 #endif /* HAVE_ICONV */
806 if (current_encoding == NULL || *current_encoding == '\0')
807 current_encoding = DEFAULT_ENCODING;
808
809 /* Initialize the parser */
810 java_init_lex (finput, current_encoding);
811 java_parse_abort_on_error ();
812
813 java_parse (); /* Parse and build partial tree nodes. */
814 java_parse_abort_on_error ();
815 java_complete_class (); /* Parse unsatisfied class decl. */
816 java_parse_abort_on_error ();
817 java_check_circular_reference (); /* Check on circular references */
818 java_parse_abort_on_error ();
819 java_fix_constructors (); /* Fix the constructors */
820 java_parse_abort_on_error ();
821 java_reorder_fields (); /* Reorder the fields */
822 }
823
824 static int
825 predefined_filename_p (node)
826 tree node;
827 {
828 int i;
829 for (i = 0; i < PREDEF_FILENAMES_SIZE; i++)
830 if (predef_filenames [i] == node)
831 return 1;
832 return 0;
833 }
834
835 int
836 yyparse ()
837 {
838 int several_files = 0;
839 char *list = xstrdup (input_filename), *next;
840 tree node;
841 FILE *finput;
842
843 do
844 {
845 next = strchr (list, '&');
846 if (next)
847 {
848 *next++ = '\0';
849 several_files = 1;
850 }
851
852 if (list[0])
853 {
854 char *value;
855 tree id;
856 int twice = 0;
857
858 int len = strlen (list);
859
860 if (*list != '/' && several_files)
861 obstack_grow (&temporary_obstack, "./", 2);
862
863 obstack_grow0 (&temporary_obstack, list, len);
864 value = obstack_finish (&temporary_obstack);
865
866 /* Exclude file that we see twice on the command line. For
867 all files except {Class,Error,Object,RuntimeException,String,
868 Throwable}.java we can rely on maybe_get_identifier. For
869 these files, we need to do a linear search of
870 current_file_list. This search happens only for these
871 files, presumably only when we're recompiling libgcj. */
872
873 if ((id = maybe_get_identifier (value)))
874 {
875 if (predefined_filename_p (id))
876 {
877 tree c;
878 for (c = current_file_list; c; c = TREE_CHAIN (c))
879 if (TREE_VALUE (c) == id)
880 twice = 1;
881 }
882 else
883 twice = 1;
884 }
885
886 if (twice)
887 {
888 const char *saved_input_filename = input_filename;
889 input_filename = value;
890 warning ("source file seen twice on command line and will be compiled only once.");
891 input_filename = saved_input_filename;
892 }
893 else
894 {
895 BUILD_FILENAME_IDENTIFIER_NODE (node, value);
896 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
897 current_file_list = tree_cons (NULL_TREE, node,
898 current_file_list);
899 }
900 }
901 list = next;
902 }
903 while (next);
904
905 current_jcf = main_jcf;
906 current_file_list = nreverse (current_file_list);
907 for (node = current_file_list; node; node = TREE_CHAIN (node))
908 {
909 tree name = TREE_VALUE (node);
910
911 /* Skip already parsed files */
912 if (HAS_BEEN_ALREADY_PARSED_P (name))
913 continue;
914
915 /* Close previous descriptor, if any */
916 if (main_jcf->read_state && fclose (main_jcf->read_state))
917 fatal ("failed to close input file `%s' - yyparse",
918 (main_jcf->filename ? main_jcf->filename : "<unknown>"));
919
920 /* Set jcf up and open a new file */
921 JCF_ZERO (main_jcf);
922 main_jcf->read_state = fopen (IDENTIFIER_POINTER (name), "rb");
923 if (main_jcf->read_state == NULL)
924 pfatal_with_name (IDENTIFIER_POINTER (name));
925
926 /* Set new input_filename and finput */
927 finput = main_jcf->read_state;
928 #ifdef IO_BUFFER_SIZE
929 setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
930 _IOFBF, IO_BUFFER_SIZE);
931 #endif
932 input_filename = IDENTIFIER_POINTER (name);
933 main_jcf->filbuf = jcf_filbuf_from_stdio;
934
935 switch (jcf_figure_file_type (current_jcf))
936 {
937 case JCF_ZIP:
938 parse_zip_file_entries ();
939 break;
940 case JCF_CLASS:
941 jcf_parse (current_jcf);
942 parse_class_file ();
943 break;
944 case JCF_SOURCE:
945 java_push_parser_context ();
946 java_parser_context_save_global ();
947 parse_source_file (name, finput);
948 java_parser_context_restore_global ();
949 java_pop_parser_context (1);
950 break;
951 }
952 }
953
954 java_expand_classes ();
955 if (!java_report_errors () && !flag_syntax_only)
956 emit_register_classes ();
957 return 0;
958 }
959
960 static struct ZipFileCache *localToFile;
961
962 /* Process all class entries found in the zip file. */
963 static void
964 parse_zip_file_entries (void)
965 {
966 struct ZipDirectory *zdir;
967 int i;
968
969 for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
970 i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
971 {
972 tree class;
973
974 /* We don't need to consider those files. */
975 if (!zdir->size || !zdir->filename_offset)
976 continue;
977
978 class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
979 current_jcf = TYPE_JCF (class);
980 current_class = class;
981
982 if ( !CLASS_LOADED_P (class))
983 {
984 fseek (current_jcf->read_state, current_jcf->zip_offset, SEEK_SET);
985 jcf_parse (current_jcf);
986 }
987
988 if (TYPE_SIZE (current_class) != error_mark_node)
989 {
990 input_filename = current_jcf->filename;
991 parse_class_file ();
992 FREE (current_jcf->buffer); /* No longer necessary */
993 /* Note: there is a way to free this buffer right after a
994 class seen in a zip file has been parsed. The idea is the
995 set its jcf in such a way that buffer will be reallocated
996 the time the code for the class will be generated. FIXME. */
997 }
998 }
999 }
1000
1001 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1002 jcf up for further processing and link it to the created class. */
1003
1004 static void
1005 process_zip_dir (FILE *finput)
1006 {
1007 int i;
1008 ZipDirectory *zdir;
1009
1010 for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
1011 i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
1012 {
1013 char *class_name, *file_name, *class_name_in_zip_dir;
1014 tree class;
1015 JCF *jcf;
1016 int j;
1017
1018 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1019
1020 /* We choose to not to process entries with a zero size or entries
1021 not bearing the .class extention. */
1022 if (!zdir->size || !zdir->filename_offset ||
1023 strncmp (&class_name_in_zip_dir[zdir->filename_length-6],
1024 ".class", 6))
1025 {
1026 /* So it will be skipped in parse_zip_file_entries */
1027 zdir->size = 0;
1028 continue;
1029 }
1030
1031 class_name = ALLOC (zdir->filename_length+1-6);
1032 file_name = ALLOC (zdir->filename_length+1);
1033 jcf = ALLOC (sizeof (JCF));
1034 JCF_ZERO (jcf);
1035
1036 strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1037 class_name [zdir->filename_length-6] = '\0';
1038 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1039 file_name [zdir->filename_length] = '\0';
1040
1041 for (j=0; class_name[j]; j++)
1042 class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1043
1044 /* Yes, we write back the true class name into the zip directory. */
1045 strcpy (class_name_in_zip_dir, class_name);
1046 zdir->filename_length = j;
1047 class = lookup_class (get_identifier (class_name));
1048
1049 jcf->read_state = finput;
1050 jcf->filbuf = jcf_filbuf_from_stdio;
1051 jcf->seen_in_zip = 1;
1052 jcf->java_source = 0;
1053 jcf->zip_offset = zdir->filestart;
1054 jcf->classname = class_name;
1055 jcf->filename = file_name;
1056
1057 TYPE_JCF (class) = jcf;
1058 }
1059 }
1060
1061 /* Lookup class NAME and figure whether is a class already found in the current
1062 zip file. */
1063 static int
1064 DEFUN(find_in_current_zip, (name, length, jcf),
1065 const char *name AND JCF **jcf)
1066 {
1067 JCF *local_jcf;
1068 tree class_name = maybe_get_identifier (name), class, icv;
1069
1070 if (!class_name)
1071 return 0;
1072
1073 if (!(icv = IDENTIFIER_CLASS_VALUE (class_name)))
1074 return 0;
1075
1076 class = TREE_TYPE (icv);
1077
1078 /* Doesn't have jcf specific info ? It's not ours */
1079 if (!TYPE_JCF (class))
1080 return 0;
1081
1082 *jcf = local_jcf = TYPE_JCF (class);
1083 fseek (local_jcf->read_state, local_jcf->zip_offset, SEEK_SET);
1084 return 1;
1085 }
1086
1087 /* Figure what kind of file we're dealing with */
1088 static int
1089 DEFUN(jcf_figure_file_type, (jcf),
1090 JCF *jcf)
1091 {
1092 unsigned char magic_string[4];
1093 uint32 magic;
1094
1095 if (fread (magic_string, 1, 4, jcf->read_state) != 4)
1096 jcf_unexpected_eof (jcf, 4);
1097
1098 fseek (jcf->read_state, 0L, SEEK_SET);
1099 magic = GET_u4 (magic_string);
1100
1101 if (magic == 0xcafebabe)
1102 return JCF_CLASS;
1103
1104 /* FIXME: is it a system file? */
1105 if (magic == (JCF_u4)ZIPMAGIC
1106 && !open_in_zip (jcf, input_filename, NULL, 0))
1107 {
1108 localToFile = ALLOC (sizeof (struct ZipFileCache));
1109 memcpy (localToFile, SeenZipFiles, sizeof (struct ZipFileCache));
1110 /* Register all the class defined there. */
1111 process_zip_dir (jcf->read_state);
1112 return JCF_ZIP;
1113 }
1114
1115 return JCF_SOURCE;
1116 }
1117
1118 /* Initialization. */
1119
1120 void
1121 init_jcf_parse ()
1122 {
1123 /* Register roots with the garbage collector. */
1124 ggc_add_tree_root (parse_roots, sizeof (parse_roots) / sizeof(tree));
1125
1126 ggc_add_root (&current_jcf, 1, sizeof (JCF), ggc_mark_jcf);
1127 }
This page took 0.090729 seconds and 6 git commands to generate.