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