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