]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/java/jcf-parse.c
Update copyright years.
[thirdparty/gcc.git] / gcc / java / jcf-parse.c
CommitLineData
377029eb 1/* Parser for Java(TM) .class files.
f1717362 2 Copyright (C) 1996-2016 Free Software Foundation, Inc.
377029eb 3
7d82ed5e 4This file is part of GCC.
377029eb 5
7d82ed5e 6GCC is free software; you can redistribute it and/or modify
377029eb 7it under the terms of the GNU General Public License as published by
e4b52719 8the Free Software Foundation; either version 3, or (at your option)
377029eb 9any later version.
10
7d82ed5e 11GCC is distributed in the hope that it will be useful,
377029eb 12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
e4b52719 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>.
377029eb 19
20Java and all Java-based marks are trademarks or registered trademarks
21of Sun Microsystems, Inc. in the United States and other countries.
22The Free Software Foundation is independent of Sun Microsystems, Inc. */
23
24/* Written by Per Bothner <bothner@cygnus.com> */
25
377029eb 26#include "config.h"
014e6e0c 27#include "system.h"
805e22b2 28#include "coretypes.h"
4cba6f60 29#include "target.h"
30#include "function.h"
4cba6f60 31#include "bitmap.h"
377029eb 32#include "tree.h"
9ed99284 33#include "stringpool.h"
4cba6f60 34#include "cgraph.h"
35#include "diagnostic-core.h"
65bf3316 36#include "javaop.h"
377029eb 37#include "java-tree.h"
c140b944 38#include "debug.h"
47369d8b 39#include "toplev.h"
377029eb 40
a0d0cc41 41#ifdef HAVE_LOCALE_H
42#include <locale.h>
43#endif
44
bea73acf 45#ifdef HAVE_LANGINFO_CODESET
f1afe160 46#include <langinfo.h>
47#endif
48
377029eb 49/* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
50#define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
51#define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
52#define JPOOL_UTF_DATA(JCF, INDEX) \
68ddcc2a 53 ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
377029eb 54#define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
55 do { \
56 unsigned char save; unsigned char *text; \
57 JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
58 text = (JCF)->read_ptr; \
59 save = text[LENGTH]; \
60 text[LENGTH] = 0; \
8e452f9c 61 (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
377029eb 62 text[LENGTH] = save; \
63 JCF_SKIP (JCF, LENGTH); } while (0)
64
65#include "jcf.h"
377029eb 66
377029eb 67extern struct obstack temporary_obstack;
377029eb 68
43d03f3a 69static GTY(()) tree parse_roots[2];
0453a7ca 70
7523afef 71/* The FIELD_DECL for the current field. */
0453a7ca 72#define current_field parse_roots[0]
377029eb 73
23b7f2e1 74/* The METHOD_DECL for the current method. */
0453a7ca 75#define current_method parse_roots[1]
377029eb 76
da37f909 77/* Line 0 in current file, if compiling from bytecode. */
78static location_t file_start_location;
79
cb18c572 80/* The Java archive that provides main_class; the main input file. */
573aba85 81static GTY(()) struct JCF * main_jcf;
0381e6c0 82
43d03f3a 83/* A list of all the class DECLs seen so far. */
f1f41a6c 84static GTY(()) vec<tree, va_gc> *all_class_list;
43d03f3a 85
2f7ab72c 86/* The number of source files passed to us by -fsource-filename and an
65bf3316 87 array of pointers to each name. Used by find_sourcefile(). */
88static int num_files = 0;
89static char **filenames;
90
cb18c572 91static struct ZipFile *localToFile;
92
65bf3316 93/* A map of byte offsets in the reflection data that are fields which
94 need renumbering. */
95bitmap field_offsets;
96bitmap_obstack bit_obstack;
97
23b7f2e1 98/* Declarations of some functions used here. */
65bf3316 99static void handle_innerclass_attribute (int count, JCF *, int len);
6852521a 100static tree give_name_to_class (JCF *jcf, int index);
caa8fa37 101static char *compute_class_name (struct ZipDirectory *zdir);
102static int classify_zip_file (struct ZipDirectory *zdir);
6852521a 103static void parse_zip_file_entries (void);
104static void process_zip_dir (FILE *);
6852521a 105static void parse_class_file (void);
7e3e53bc 106static void handle_deprecated (void);
6852521a 107static void set_source_filename (JCF *, int);
6852521a 108static void jcf_parse (struct JCF*);
109static void load_inner_classes (tree);
65bf3316 110static void handle_annotation (JCF *jcf, int level);
111static void java_layout_seen_class_methods (void);
f4e60d16 112
7e3e53bc 113/* Handle "Deprecated" attribute. */
114static void
115handle_deprecated (void)
116{
117 if (current_field != NULL_TREE)
118 FIELD_DEPRECATED (current_field) = 1;
119 else if (current_method != NULL_TREE)
120 METHOD_DEPRECATED (current_method) = 1;
121 else if (current_class != NULL_TREE)
122 CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
123 else
124 {
125 /* Shouldn't happen. */
bc031ffe 126 gcc_unreachable ();
7e3e53bc 127 }
128}
129
65bf3316 130\f
131
132/* Reverse a string. */
133static char *
134reverse (const char *s)
135{
136 if (s == NULL)
137 return NULL;
138 else
139 {
140 int len = strlen (s);
25a1c410 141 char *d = XNEWVAR (char, len + 1);
65bf3316 142 const char *sp;
143 char *dp;
144
145 d[len] = 0;
146 for (dp = &d[0], sp = &s[len-1]; sp >= s; dp++, sp--)
147 *dp = *sp;
148
149 return d;
150 }
151}
152
153/* Compare two strings for qsort(). */
154static int
155cmpstringp (const void *p1, const void *p2)
156{
157 /* The arguments to this function are "pointers to
158 pointers to char", but strcmp() arguments are "pointers
159 to char", hence the following cast plus dereference */
160
aae87fc3 161 return strcmp(*(const char *const*) p1, *(const char *const*) p2);
65bf3316 162}
163
164/* Create an array of strings, one for each source file that we've
165 seen. fsource_filename can either be the name of a single .java
166 file or a file that contains a list of filenames separated by
167 newlines. */
168void
169java_read_sourcefilenames (const char *fsource_filename)
170{
171 if (fsource_filename
172 && filenames == 0
173 && strlen (fsource_filename) > strlen (".java")
82715bcd 174 && filename_cmp ((fsource_filename
175 + strlen (fsource_filename)
176 - strlen (".java")),
65bf3316 177 ".java") != 0)
178 {
179/* fsource_filename isn't a .java file but a list of filenames
180 separated by newlines */
181 FILE *finput = fopen (fsource_filename, "r");
182 int len = 0;
183 int longest_line = 0;
184
185 gcc_assert (finput);
186
187 /* Find out how many files there are, and how long the filenames are. */
188 while (! feof (finput))
189 {
190 int ch = getc (finput);
191 if (ch == '\n')
192 {
193 num_files++;
194 if (len > longest_line)
195 longest_line = len;
196 len = 0;
197 continue;
198 }
199 if (ch == EOF)
200 break;
201 len++;
202 }
203
204 rewind (finput);
205
206 /* Read the filenames. Put a pointer to each filename into the
207 array FILENAMES. */
208 {
25a1c410 209 char *linebuf = (char *) alloca (longest_line + 1);
65bf3316 210 int i = 0;
211 int charpos;
212
25a1c410 213 filenames = XNEWVEC (char *, num_files);
65bf3316 214
215 charpos = 0;
216 for (;;)
217 {
218 int ch = getc (finput);
219 if (ch == EOF)
220 break;
221 if (ch == '\n')
222 {
223 linebuf[charpos] = 0;
224 gcc_assert (i < num_files);
225 /* ??? Perhaps we should use lrealpath() here. Doing
226 so would tidy up things like /../ but the rest of
227 gcc seems to assume relative pathnames, not
228 absolute pathnames. */
229/* realname = lrealpath (linebuf); */
230 filenames[i++] = reverse (linebuf);
231 charpos = 0;
232 continue;
233 }
234 gcc_assert (charpos < longest_line);
235 linebuf[charpos++] = ch;
236 }
237
238 if (num_files > 1)
239 qsort (filenames, num_files, sizeof (char *), cmpstringp);
240 }
241 fclose (finput);
242 }
243 else
244 {
25a1c410 245 filenames = XNEWVEC (char *, 1);
65bf3316 246 filenames[0] = reverse (fsource_filename);
247 num_files = 1;
248 }
249}
250
251/* Given a relative pathname such as foo/bar.java, attempt to find a
252 longer pathname with the same suffix.
253
2f7e7db3 254 This is a best guess heuristic; with some weird class hierarchies we
65bf3316 255 may fail to pick the correct source file. For example, if we have
256 the filenames foo/bar.java and also foo/foo/bar.java, we do not
257 have enough information to know which one is the right match for
258 foo/bar.java. */
259
260static const char *
261find_sourcefile (const char *name)
262{
263 int i = 0, j = num_files-1;
264 char *found = NULL;
265
266 if (filenames)
267 {
268 char *revname = reverse (name);
269
270 do
271 {
272 int k = (i+j) / 2;
273 int cmp = strncmp (revname, filenames[k], strlen (revname));
274 if (cmp == 0)
275 {
276 /* OK, so we found one. But is it a unique match? */
277 if ((k > i
278 && strncmp (revname, filenames[k-1], strlen (revname)) == 0)
279 || (k < j
280 && (strncmp (revname, filenames[k+1], strlen (revname))
281 == 0)))
282 ;
283 else
284 found = filenames[k];
285 break;
286 }
287 if (cmp > 0)
288 i = k+1;
289 else
290 j = k-1;
291 }
292 while (i <= j);
293
294 free (revname);
295 }
296
297 if (found && strlen (found) > strlen (name))
298 return reverse (found);
299 else
300 return name;
301}
302
303\f
304
377029eb 305/* Handle "SourceFile" attribute. */
306
68ddcc2a 307static void
2883a3ed 308set_source_filename (JCF *jcf, int index)
377029eb 309{
310 tree sfname_id = get_name_constant (jcf, index);
71d9fc9b 311 const char *sfname = IDENTIFIER_POINTER (sfname_id);
3df42822 312 const char *old_filename = LOCATION_FILE (input_location);
da37f909 313 int new_len = IDENTIFIER_LENGTH (sfname_id);
314 if (old_filename != NULL)
377029eb 315 {
da37f909 316 int old_len = strlen (old_filename);
3df42822 317 /* Use the filename from current input_location (derived from the
318 class name) if it has a directory prefix, but otherwise matches
319 sfname. */
377029eb 320 if (old_len > new_len
82715bcd 321 && filename_cmp (sfname, old_filename + old_len - new_len) == 0
da37f909 322 && (old_filename[old_len - new_len - 1] == '/'
323 || old_filename[old_len - new_len - 1] == '\\'))
9c85a98a 324 return;
377029eb 325 }
da37f909 326 if (strchr (sfname, '/') == NULL && strchr (sfname, '\\') == NULL)
327 {
328 const char *class_name
329 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class)));
384eaf80 330 const char *dot = strrchr (class_name, '.');
da37f909 331 if (dot != NULL)
332 {
4423a35c 333 /* Length of prefix, not counting final dot. */
334 int i = dot - class_name;
da37f909 335 /* Concatenate current package prefix with new sfname. */
4c36ffe6 336 char *buf = XNEWVEC (char, i + new_len + 2); /* Space for '.' and '\0'. */
4423a35c 337 strcpy (buf + i + 1, sfname);
338 /* Copy package from class_name, replacing '.' by DIR_SEPARATOR.
339 Note we start at the end with the final package dot. */
da37f909 340 for (; i >= 0; i--)
341 {
4423a35c 342 char c = class_name[i];
343 if (c == '.')
344 c = DIR_SEPARATOR;
345 buf[i] = c;
da37f909 346 }
347 sfname_id = get_identifier (buf);
4423a35c 348 free (buf);
da37f909 349 sfname = IDENTIFIER_POINTER (sfname_id);
350 }
351 }
352
65bf3316 353 sfname = find_sourcefile (sfname);
3ce1a212 354 LINEMAPS_LAST_ORDINARY_MAP (line_table)->to_file = sfname;
da37f909 355 if (current_class == main_class) main_input_filename = sfname;
377029eb 356}
357
65bf3316 358
359\f
360
361/* Annotation handling.
362
363 The technique we use here is to copy the annotation data directly
2f7ab72c 364 from the input class file into the output file. We don't decode the
65bf3316 365 data at all, merely rewriting constant indexes whenever we come
2f7ab72c 366 across them: this is necessary because the constant pool in the
e66d204f 367 output file isn't the same as the constant pool in the input.
65bf3316 368
369 The main advantage of this technique is that the resulting
370 annotation data is pointer-free, so it doesn't have to be relocated
371 at startup time. As a consequence of this, annotations have no
2f7ab72c 372 performance impact unless they are used. Also, this representation
65bf3316 373 is very dense. */
374
375
376/* Expand TYPE_REFLECTION_DATA by DELTA bytes. Return the address of
377 the start of the newly allocated region. */
378
379static unsigned char*
380annotation_grow (int delta)
381{
382 unsigned char **data = &TYPE_REFLECTION_DATA (current_class);
383 long *datasize = &TYPE_REFLECTION_DATASIZE (current_class);
384 long len = *datasize;
385
386 if (*data == NULL)
387 {
25a1c410 388 *data = XNEWVAR (unsigned char, delta);
65bf3316 389 }
390 else
391 {
392 int newlen = *datasize + delta;
393 if (floor_log2 (newlen) != floor_log2 (*datasize))
25a1c410 394 *data = XRESIZEVAR (unsigned char, *data, 2 << (floor_log2 (newlen)));
65bf3316 395 }
396 *datasize += delta;
397 return *data + len;
398}
399
400/* annotation_rewrite_TYPE. Rewrite various int types at p. Use Java
401 byte order (i.e. big endian.) */
402
403static void
404annotation_rewrite_byte (unsigned int n, unsigned char *p)
405{
406 p[0] = n;
407}
408
409static void
410annotation_rewrite_short (unsigned int n, unsigned char *p)
411{
412 p[0] = n>>8;
413 p[1] = n;
414}
415
416static void
417annotation_rewrite_int (unsigned int n, unsigned char *p)
418{
419 p[0] = n>>24;
420 p[1] = n>>16;
421 p[2] = n>>8;
422 p[3] = n;
423}
424
425/* Read a 16-bit unsigned int in Java byte order (i.e. big
426 endian.) */
427
428static uint16
429annotation_read_short (unsigned char *p)
430{
431 uint16 tmp = p[0];
432 tmp = (tmp << 8) | p[1];
433 return tmp;
434}
435
436/* annotation_write_TYPE. Rewrite various int types, appending them
437 to TYPE_REFLECTION_DATA. Use Java byte order (i.e. big
438 endian.) */
439
440static void
441annotation_write_byte (unsigned int n)
442{
443 annotation_rewrite_byte (n, annotation_grow (1));
444}
445
446static void
447annotation_write_short (unsigned int n)
448{
449 annotation_rewrite_short (n, annotation_grow (2));
450}
451
452static void
453annotation_write_int (unsigned int n)
454{
455 annotation_rewrite_int (n, annotation_grow (4));
456}
457
458/* Create a 64-bit constant in the constant pool.
459
460 This is used for both integer and floating-point types. As a
461 consequence, it will not work if the target floating-point format
462 is anything other than IEEE-754. While this is arguably a bug, the
463 runtime library makes exactly the same assumption and it's unlikely
464 that Java will ever run on a non-IEEE machine. */
465
466static int
467handle_long_constant (JCF *jcf, CPool *cpool, enum cpool_tag kind,
468 int index, bool big_endian)
469{
470 /* If we're on a 64-bit platform we can fit a long or double
471 into the same space as a jword. */
472 if (POINTER_SIZE >= 64)
473 index = find_constant1 (cpool, kind, JPOOL_LONG (jcf, index));
474
475 /* In a compiled program the constant pool is in native word
476 order. How weird is that??? */
477 else if (big_endian)
478 index = find_constant2 (cpool, kind,
479 JPOOL_INT (jcf, index),
480 JPOOL_INT (jcf, index+1));
481 else
482 index = find_constant2 (cpool, kind,
483 JPOOL_INT (jcf, index+1),
484 JPOOL_INT (jcf, index));
485
486 return index;
487}
488
489/* Given a class file and an index into its constant pool, create an
490 entry in the outgoing constant pool for the same item. */
491
492static uint16
493handle_constant (JCF *jcf, int index, enum cpool_tag purpose)
494{
561017b5 495 unsigned int kind;
65bf3316 496 CPool *cpool = cpool_for_class (output_class);
497
498 if (index == 0)
499 return 0;
500
501 if (! CPOOL_INDEX_IN_RANGE (&jcf->cpool, index))
502 error ("<constant pool index %d not in range>", index);
503
561017b5 504 kind = JPOOL_TAG (jcf, index);
65bf3316 505
506 if ((kind & ~CONSTANT_ResolvedFlag) != purpose)
507 {
508 if (purpose == CONSTANT_Class
509 && kind == CONSTANT_Utf8)
510 ;
511 else
512 error ("<constant pool index %d unexpected type", index);
513 }
514
515 switch (kind)
516 {
517 case CONSTANT_Class:
518 case CONSTANT_ResolvedClass:
519 {
520 /* For some reason I know not the what of, class names in
521 annotations are UTF-8 strings in the constant pool but
522 class names in EnclosingMethod attributes are real class
523 references. Set CONSTANT_LazyFlag here so that the VM
524 doesn't attempt to resolve them at class initialization
525 time. */
526 tree resolved_class, class_name;
527 resolved_class = get_class_constant (jcf, index);
528 class_name = build_internal_class_name (resolved_class);
529 index = alloc_name_constant (CONSTANT_Class | CONSTANT_LazyFlag,
530 (unmangle_classname
531 (IDENTIFIER_POINTER(class_name),
532 IDENTIFIER_LENGTH(class_name))));
533 break;
534 }
535 case CONSTANT_Utf8:
536 {
537 tree utf8 = get_constant (jcf, index);
538 if (purpose == CONSTANT_Class)
539 /* Create a constant pool entry for a type signature. This
540 one has '.' rather than '/' because it isn't going into a
541 class file, it's going into a compiled object.
542
543 This has to match the logic in
544 _Jv_ClassReader::prepare_pool_entry(). */
545 utf8 = unmangle_classname (IDENTIFIER_POINTER(utf8),
546 IDENTIFIER_LENGTH(utf8));
547 index = alloc_name_constant (kind, utf8);
548 }
549 break;
550
551 case CONSTANT_Long:
561017b5 552 index = handle_long_constant (jcf, cpool, CONSTANT_Long, index,
183dc110 553 targetm.words_big_endian ());
65bf3316 554 break;
555
556 case CONSTANT_Double:
561017b5 557 index = handle_long_constant (jcf, cpool, CONSTANT_Double, index,
183dc110 558 targetm.float_words_big_endian ());
65bf3316 559 break;
560
561 case CONSTANT_Float:
562 case CONSTANT_Integer:
563 index = find_constant1 (cpool, kind, JPOOL_INT (jcf, index));
564 break;
565
566 case CONSTANT_NameAndType:
567 {
568 uint16 name = JPOOL_USHORT1 (jcf, index);
569 uint16 sig = JPOOL_USHORT2 (jcf, index);
570 uint32 name_index = handle_constant (jcf, name, CONSTANT_Utf8);
571 uint32 sig_index = handle_constant (jcf, sig, CONSTANT_Class);
572 jword new_index = (name_index << 16) | sig_index;
573 index = find_constant1 (cpool, kind, new_index);
574 }
575 break;
576
577 default:
578 abort ();
579 }
580
581 return index;
582}
583
584/* Read an element_value structure from an annotation in JCF. Return
585 the constant pool index for the resulting constant pool entry. */
586
587static int
588handle_element_value (JCF *jcf, int level)
589{
590 uint8 tag = JCF_readu (jcf);
591 int index = 0;
592
593 annotation_write_byte (tag);
594 switch (tag)
595 {
596 case 'B':
597 case 'C':
598 case 'S':
599 case 'Z':
600 case 'I':
601 {
602 uint16 cindex = JCF_readu2 (jcf);
603 index = handle_constant (jcf, cindex,
604 CONSTANT_Integer);
605 annotation_write_short (index);
606 }
607 break;
608 case 'D':
609 {
610 uint16 cindex = JCF_readu2 (jcf);
611 index = handle_constant (jcf, cindex,
612 CONSTANT_Double);
613 annotation_write_short (index);
614 }
615 break;
616 case 'F':
617 {
618 uint16 cindex = JCF_readu2 (jcf);
619 index = handle_constant (jcf, cindex,
620 CONSTANT_Float);
621 annotation_write_short (index);
622 }
623 break;
624 case 'J':
625 {
626 uint16 cindex = JCF_readu2 (jcf);
627 index = handle_constant (jcf, cindex,
628 CONSTANT_Long);
629 annotation_write_short (index);
630 }
631 break;
632 case 's':
633 {
634 uint16 cindex = JCF_readu2 (jcf);
635 /* Despite what the JVM spec says, compilers generate a Utf8
636 constant here, not a String. */
637 index = handle_constant (jcf, cindex,
638 CONSTANT_Utf8);
639 annotation_write_short (index);
640 }
641 break;
642
643 case 'e':
644 {
645 uint16 type_name_index = JCF_readu2 (jcf);
646 uint16 const_name_index = JCF_readu2 (jcf);
647 index = handle_constant (jcf, type_name_index,
648 CONSTANT_Class);
649 annotation_write_short (index);
650 index = handle_constant (jcf, const_name_index,
651 CONSTANT_Utf8);
652 annotation_write_short (index);
653 }
654 break;
655 case 'c':
656 {
657 uint16 class_info_index = JCF_readu2 (jcf);
658 index = handle_constant (jcf, class_info_index,
659 CONSTANT_Class);
660 annotation_write_short (index);
661 }
662 break;
663 case '@':
664 {
665 handle_annotation (jcf, level + 1);
666 }
667 break;
668 case '[':
669 {
670 uint16 n_array_elts = JCF_readu2 (jcf);
671 annotation_write_short (n_array_elts);
672 while (n_array_elts--)
673 handle_element_value (jcf, level + 1);
674 }
675 break;
676 default:
677 abort();
678 break;
679 }
680 return index;
681}
682
683/* Read an annotation structure from JCF. Write it to the
684 reflection_data field of the outgoing class. */
685
686static void
687handle_annotation (JCF *jcf, int level)
688{
689 uint16 type_index = JCF_readu2 (jcf);
690 uint16 npairs = JCF_readu2 (jcf);
691 int index = handle_constant (jcf, type_index,
692 CONSTANT_Class);
693 annotation_write_short (index);
694 annotation_write_short (npairs);
695 while (npairs--)
696 {
697 uint16 name_index = JCF_readu2 (jcf);
698 index = handle_constant (jcf, name_index,
699 CONSTANT_Utf8);
700 annotation_write_short (index);
701 handle_element_value (jcf, level + 2);
702 }
703}
704
705/* Read an annotation count from JCF, and write the following
2f7ab72c 706 annotations to the reflection_data field of the outgoing class. */
65bf3316 707
708static void
709handle_annotations (JCF *jcf, int level)
710{
711 uint16 num = JCF_readu2 (jcf);
712 annotation_write_short (num);
713 while (num--)
714 handle_annotation (jcf, level);
715}
716
717/* As handle_annotations(), but perform a sanity check that we write
718 the same number of bytes that we were expecting. */
719
720static void
721handle_annotation_attribute (int ATTRIBUTE_UNUSED index, JCF *jcf,
722 long length)
723{
724 long old_datasize = TYPE_REFLECTION_DATASIZE (current_class);
725
726 handle_annotations (jcf, 0);
727
728 gcc_assert (old_datasize + length
729 == TYPE_REFLECTION_DATASIZE (current_class));
730}
731
732/* gcj permutes its fields array after generating annotation_data, so
733 we have to fixup field indexes for fields that have moved. Given
734 ARG, a VEC_int, fixup the field indexes in the reflection_data of
735 the outgoing class. We use field_offsets to tell us where the
736 fixups must go. */
737
738void
739rewrite_reflection_indexes (void *arg)
740{
741 bitmap_iterator bi;
742 unsigned int offset;
f1f41a6c 743 vec<int> *map = (vec<int> *) arg;
65bf3316 744 unsigned char *data = TYPE_REFLECTION_DATA (current_class);
745
746 if (map)
747 {
748 EXECUTE_IF_SET_IN_BITMAP (field_offsets, 0, offset, bi)
749 {
750 uint16 index = annotation_read_short (data + offset);
751 annotation_rewrite_short
f1f41a6c 752 ((*map)[index], data + offset);
65bf3316 753 }
754 }
755}
756
757/* Read the RuntimeVisibleAnnotations from JCF and write them to the
758 reflection_data of the outgoing class. */
759
760static void
761handle_member_annotations (int member_index, JCF *jcf,
762 const unsigned char *name ATTRIBUTE_UNUSED,
763 long len, jv_attr_type member_type)
764{
765 int new_len = len + 1;
766 annotation_write_byte (member_type);
767 if (member_type != JV_CLASS_ATTR)
768 new_len += 2;
769 annotation_write_int (new_len);
770 annotation_write_byte (JV_ANNOTATIONS_KIND);
771 if (member_type == JV_FIELD_ATTR)
772 bitmap_set_bit (field_offsets, TYPE_REFLECTION_DATASIZE (current_class));
773 if (member_type != JV_CLASS_ATTR)
774 annotation_write_short (member_index);
775 handle_annotation_attribute (member_index, jcf, len);
776}
777
778/* Read the RuntimeVisibleParameterAnnotations from JCF and write them
779 to the reflection_data of the outgoing class. */
780
781static void
782handle_parameter_annotations (int member_index, JCF *jcf,
783 const unsigned char *name ATTRIBUTE_UNUSED,
784 long len, jv_attr_type member_type)
785{
786 int new_len = len + 1;
787 uint8 num;
788 annotation_write_byte (member_type);
789 if (member_type != JV_CLASS_ATTR)
790 new_len += 2;
791 annotation_write_int (new_len);
792 annotation_write_byte (JV_PARAMETER_ANNOTATIONS_KIND);
793 if (member_type != JV_CLASS_ATTR)
794 annotation_write_short (member_index);
795 num = JCF_readu (jcf);
796 annotation_write_byte (num);
797 while (num--)
798 handle_annotations (jcf, 0);
799}
800
801
802/* Read the AnnotationDefault data from JCF and write them to the
803 reflection_data of the outgoing class. */
804
805static void
806handle_default_annotation (int member_index, JCF *jcf,
807 const unsigned char *name ATTRIBUTE_UNUSED,
808 long len, jv_attr_type member_type)
809{
810 int new_len = len + 1;
811 annotation_write_byte (member_type);
812 if (member_type != JV_CLASS_ATTR)
813 new_len += 2;
814 annotation_write_int (new_len);
815 annotation_write_byte (JV_ANNOTATION_DEFAULT_KIND);
816 if (member_type != JV_CLASS_ATTR)
817 annotation_write_short (member_index);
818 handle_element_value (jcf, 0);
819}
820
821/* As above, for the EnclosingMethod attribute. */
822
823static void
824handle_enclosingmethod_attribute (int member_index, JCF *jcf,
825 const unsigned char *name ATTRIBUTE_UNUSED,
826 long len, jv_attr_type member_type)
827{
828 int new_len = len + 1;
829 uint16 index;
830 annotation_write_byte (member_type);
831 if (member_type != JV_CLASS_ATTR)
832 new_len += 2;
833 annotation_write_int (new_len);
834 annotation_write_byte (JV_ENCLOSING_METHOD_KIND);
835 if (member_type != JV_CLASS_ATTR)
836 annotation_write_short (member_index);
837
838 index = JCF_readu2 (jcf);
839 index = handle_constant (jcf, index, CONSTANT_Class);
840 annotation_write_short (index);
841
842 index = JCF_readu2 (jcf);
843 index = handle_constant (jcf, index, CONSTANT_NameAndType);
844 annotation_write_short (index);
845}
846
847/* As above, for the Signature attribute. */
848
849static void
850handle_signature_attribute (int member_index, JCF *jcf,
851 const unsigned char *name ATTRIBUTE_UNUSED,
852 long len, jv_attr_type member_type)
853{
854 int new_len = len + 1;
855 uint16 index;
856 annotation_write_byte (member_type);
857 if (member_type != JV_CLASS_ATTR)
858 new_len += 2;
859 annotation_write_int (new_len);
860 annotation_write_byte (JV_SIGNATURE_KIND);
861 if (member_type != JV_CLASS_ATTR)
862 annotation_write_short (member_index);
863
864 index = JCF_readu2 (jcf);
865 index = handle_constant (jcf, index, CONSTANT_Utf8);
866 annotation_write_short (index);
867}
868
869\f
870
377029eb 871#define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
872
873#define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
874{ tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
757f6c6c 875 output_class = current_class = give_name_to_class (jcf, THIS); \
377029eb 876 set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
877
878#define HANDLE_CLASS_INTERFACE(INDEX) \
879 add_interface (current_class, get_class_constant (jcf, INDEX))
880
881#define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
882{ int sig_index = SIGNATURE; \
883 current_field = add_field (current_class, get_name_constant (jcf, NAME), \
884 parse_signature (jcf, sig_index), ACCESS_FLAGS); \
a24242f9 885 set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
886 if ((ACCESS_FLAGS) & ACC_FINAL) \
887 MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
888}
377029eb 889
890#define HANDLE_END_FIELDS() \
891 (current_field = NULL_TREE)
892
893#define HANDLE_CONSTANTVALUE(INDEX) \
894{ tree constant; int index = INDEX; \
6548e808 895 if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
377029eb 896 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
897 constant = build_utf8_ref (name); \
898 } \
899 else \
900 constant = get_constant (jcf, index); \
901 set_constant_value (current_field, constant); }
902
903#define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
904 (current_method = add_method (current_class, ACCESS_FLAGS, \
905 get_name_constant (jcf, NAME), \
906 get_name_constant (jcf, SIGNATURE)), \
907 DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
908 DECL_LINENUMBERS_OFFSET (current_method) = 0)
909
910#define HANDLE_END_METHODS() \
e6ec293d 911{ current_method = NULL_TREE; }
377029eb 912
913#define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
914{ DECL_MAX_STACK (current_method) = (MAX_STACK); \
915 DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
916 DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
917 DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
918
919#define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
920{ int n = (COUNT); \
921 DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
922 JCF_SKIP (jcf, n * 10); }
923
924#define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
925{ int n = (COUNT); \
926 DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
927 JCF_SKIP (jcf, n * 4); }
928
d1016559 929#define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
930{ \
931 int n = COUNT; \
f1f41a6c 932 vec<tree, va_gc> *v; \
933 vec_alloc (v, n); \
934 gcc_assert (!DECL_FUNCTION_THROWS (current_method)); \
d1016559 935 while (--n >= 0) \
936 { \
937 tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
f1f41a6c 938 v->quick_push (thrown_class); \
d1016559 939 } \
3d5bf92f 940 DECL_FUNCTION_THROWS (current_method) = v; \
d1016559 941}
942
7e3e53bc 943#define HANDLE_DEPRECATED_ATTRIBUTE() handle_deprecated ()
944
4654e794 945/* Link seen inner classes to their outer context and register the
946 inner class to its outer context. They will be later loaded. */
a24242f9 947#define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
65bf3316 948 handle_innerclass_attribute (COUNT, jcf, attribute_length)
a24242f9 949
950#define HANDLE_SYNTHETIC_ATTRIBUTE() \
951{ \
301dfdef 952 /* Irrelevant decls should have been nullified by the END macros. \
301dfdef 953 DECL_ARTIFICIAL on fields is used for something else (See \
954 PUSH_FIELD in java-tree.h) */ \
a24242f9 955 if (current_method) \
956 DECL_ARTIFICIAL (current_method) = 1; \
65bf3316 957 else if (current_field) \
958 FIELD_SYNTHETIC (current_field) = 1; \
959 else \
751d3443 960 TYPE_SYNTHETIC (current_class) = 1; \
4654e794 961}
962
99878175 963#define HANDLE_GCJCOMPILED_ATTRIBUTE() \
964{ \
965 if (current_class == object_type_node) \
966 jcf->right_zip = 1; \
967}
968
65bf3316 969#define HANDLE_RUNTIMEVISIBLEANNOTATIONS_ATTRIBUTE() \
970{ \
971 handle_member_annotations (index, jcf, name_data, attribute_length, attr_type); \
972}
973
974#define HANDLE_RUNTIMEINVISIBLEANNOTATIONS_ATTRIBUTE() \
975{ \
976 JCF_SKIP(jcf, attribute_length); \
977}
978
979#define HANDLE_RUNTIMEVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
980{ \
981 handle_parameter_annotations (index, jcf, name_data, attribute_length, attr_type); \
982}
983
984#define HANDLE_RUNTIMEINVISIBLEPARAMETERANNOTATIONS_ATTRIBUTE() \
985{ \
986 JCF_SKIP(jcf, attribute_length); \
987}
988
989#define HANDLE_ANNOTATIONDEFAULT_ATTRIBUTE() \
990{ \
991 handle_default_annotation (index, jcf, name_data, attribute_length, attr_type); \
992}
993
994#define HANDLE_ENCLOSINGMETHOD_ATTRIBUTE() \
995{ \
996 handle_enclosingmethod_attribute (index, jcf, name_data, \
997 attribute_length, attr_type); \
998}
999
1000#define HANDLE_SIGNATURE_ATTRIBUTE() \
1001{ \
1002 handle_signature_attribute (index, jcf, name_data, \
1003 attribute_length, attr_type); \
1004}
1005
377029eb 1006#include "jcf-reader.c"
1007
377029eb 1008tree
2883a3ed 1009parse_signature (JCF *jcf, int sig_index)
377029eb 1010{
bc031ffe 1011 gcc_assert (sig_index > 0
1012 && sig_index < JPOOL_SIZE (jcf)
1013 && JPOOL_TAG (jcf, sig_index) == CONSTANT_Utf8);
1014
1015 return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
1016 JPOOL_UTF_LENGTH (jcf, sig_index));
377029eb 1017}
1018
377029eb 1019tree
2883a3ed 1020get_constant (JCF *jcf, int index)
377029eb 1021{
1022 tree value;
1023 int tag;
1024 if (index <= 0 || index >= JPOOL_SIZE(jcf))
1025 goto bad;
1026 tag = JPOOL_TAG (jcf, index);
1027 if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
573aba85 1028 return jcf->cpool.data[index].t;
377029eb 1029 switch (tag)
1030 {
1031 case CONSTANT_Integer:
1032 {
1033 jint num = JPOOL_INT(jcf, index);
7016c612 1034 value = build_int_cst (int_type_node, num);
377029eb 1035 break;
1036 }
1037 case CONSTANT_Long:
1038 {
c5083e8b 1039 unsigned HOST_WIDE_INT num;
c5083e8b 1040
1041 num = JPOOL_UINT (jcf, index);
ab2c1de8 1042 wide_int val = wi::lshift (wide_int::from (num, 64, SIGNED), 32);
c5083e8b 1043 num = JPOOL_UINT (jcf, index + 1);
796b6678 1044 val |= num;
c5083e8b 1045
e913b5cd 1046 value = wide_int_to_tree (long_type_node, val);
377029eb 1047 break;
1048 }
aa870c1b 1049
377029eb 1050 case CONSTANT_Float:
83b94e03 1051 {
1052 jint num = JPOOL_INT(jcf, index);
1053 long buf = num;
1054 REAL_VALUE_TYPE d;
1055
d55fadad 1056 real_from_target (&d, &buf, &ieee_single_format);
83b94e03 1057 value = build_real (float_type_node, d);
1058 break;
1059 }
aa870c1b 1060
377029eb 1061 case CONSTANT_Double:
83b94e03 1062 {
1063 long buf[2], lo, hi;
1064 REAL_VALUE_TYPE d;
aa870c1b 1065
83b94e03 1066 hi = JPOOL_UINT (jcf, index);
1067 lo = JPOOL_UINT (jcf, index+1);
aa870c1b 1068
183dc110 1069 if (targetm.float_words_big_endian ())
83b94e03 1070 buf[0] = hi, buf[1] = lo;
1071 else
1072 buf[0] = lo, buf[1] = hi;
aa870c1b 1073
d55fadad 1074 real_from_target (&d, buf, &ieee_double_format);
83b94e03 1075 value = build_real (double_type_node, d);
1076 break;
1077 }
aa870c1b 1078
377029eb 1079 case CONSTANT_String:
1080 {
377029eb 1081 tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
68ddcc2a 1082 const char *utf8_ptr = IDENTIFIER_POINTER (name);
377029eb 1083 int utf8_len = IDENTIFIER_LENGTH (name);
44acf429 1084 const unsigned char *utf8;
2c8c01c1 1085 int i;
377029eb 1086
2c8c01c1 1087 /* Check for a malformed Utf8 string. */
44acf429 1088 utf8 = (const unsigned char *) utf8_ptr;
1089 i = utf8_len;
44acf429 1090 while (i > 0)
377029eb 1091 {
44acf429 1092 int char_len = UT8_CHAR_LENGTH (*utf8);
4177eed1 1093 if (char_len < 0 || char_len > 3 || char_len > i)
c05be867 1094 fatal_error (input_location, "bad string constant");
f060a027 1095
44acf429 1096 utf8 += char_len;
377029eb 1097 i -= char_len;
1098 }
1099
2c8c01c1 1100 /* Allocate a new string value. */
1101 value = build_string (utf8_len, utf8_ptr);
44acf429 1102 TREE_TYPE (value) = build_pointer_type (string_type_node);
377029eb 1103 }
1104 break;
1105 default:
1106 goto bad;
1107 }
f060a027 1108 JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
573aba85 1109 jcf->cpool.data[index].t = value;
377029eb 1110 return value;
1111 bad:
c05be867 1112 fatal_error (input_location, "bad value constant type %d, index %d",
39101666 1113 JPOOL_TAG (jcf, index), index);
377029eb 1114}
1115
1116tree
2883a3ed 1117get_name_constant (JCF *jcf, int index)
377029eb 1118{
1119 tree name = get_constant (jcf, index);
bc031ffe 1120 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
377029eb 1121 return name;
1122}
1123
86523f9c 1124/* Handle reading innerclass attributes. If a nonzero entry (denoting
a24242f9 1125 a non anonymous entry) is found, We augment the inner class list of
1126 the outer context with the newly resolved innerclass. */
1127
1128static void
65bf3316 1129handle_innerclass_attribute (int count, JCF *jcf, int attribute_length)
a24242f9 1130{
65bf3316 1131 int c = count;
1132
1133 annotation_write_byte (JV_CLASS_ATTR);
1134 annotation_write_int (attribute_length+1);
1135 annotation_write_byte (JV_INNER_CLASSES_KIND);
1136 annotation_write_short (count);
1137
a24242f9 1138 while (c--)
1139 {
1140 /* Read inner_class_info_index. This may be 0 */
1141 int icii = JCF_readu2 (jcf);
1142 /* Read outer_class_info_index. If the innerclasses attribute
1143 entry isn't a member (like an inner class) the value is 0. */
1144 int ocii = JCF_readu2 (jcf);
1145 /* Read inner_name_index. If the class we're dealing with is
4f9026a9 1146 an anonymous class, it must be 0. */
a24242f9 1147 int ini = JCF_readu2 (jcf);
728b4c29 1148 /* Read the access flag. */
1149 int acc = JCF_readu2 (jcf);
65bf3316 1150
1151 annotation_write_short (handle_constant (jcf, icii, CONSTANT_Class));
1152 annotation_write_short (handle_constant (jcf, ocii, CONSTANT_Class));
1153 annotation_write_short (handle_constant (jcf, ini, CONSTANT_Utf8));
1154 annotation_write_short (acc);
1155
a24242f9 1156 /* If icii is 0, don't try to read the class. */
1157 if (icii >= 0)
1158 {
ead29d98 1159 tree klass = get_class_constant (jcf, icii);
1160 tree decl = TYPE_NAME (klass);
a24242f9 1161 /* Skip reading further if ocii is null */
1162 if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
1163 {
1164 tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
1165 tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
728b4c29 1166 set_class_decl_access_flags (acc, decl);
a24242f9 1167 DECL_CONTEXT (decl) = outer;
1168 DECL_INNER_CLASS_LIST (outer) =
1169 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
1170 CLASS_COMPLETE_P (decl) = 1;
1171 }
1172 }
a24242f9 1173 }
1174}
1175
377029eb 1176static tree
2883a3ed 1177give_name_to_class (JCF *jcf, int i)
377029eb 1178{
bc031ffe 1179 gcc_assert (i > 0
1180 && i < JPOOL_SIZE (jcf)
1181 && JPOOL_TAG (jcf, i) == CONSTANT_Class);
1182
377029eb 1183 {
8f6353e5 1184 tree package_name = NULL_TREE, tmp;
377029eb 1185 tree this_class;
1186 int j = JPOOL_USHORT1 (jcf, i);
1187 /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
8e452f9c 1188 tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
377029eb 1189 JPOOL_UTF_LENGTH (jcf, j));
1190 this_class = lookup_class (class_name);
da37f909 1191 {
1192 tree source_name = identifier_subst (class_name, "", '.', '/', ".java");
9cc6dbce 1193 const char *sfname = find_sourcefile (IDENTIFIER_POINTER (source_name));
931b0a0f 1194 linemap_add (line_table, LC_ENTER, false, sfname, 0);
1195 input_location = linemap_line_start (line_table, 0, 1);
da37f909 1196 file_start_location = input_location;
1197 DECL_SOURCE_LOCATION (TYPE_NAME (this_class)) = input_location;
1198 if (main_input_filename == NULL && jcf == main_jcf)
1199 main_input_filename = sfname;
1200 }
377029eb 1201
573aba85 1202 jcf->cpool.data[i].t = this_class;
377029eb 1203 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
8f6353e5 1204 split_qualified_name (&package_name, &tmp,
1205 DECL_NAME (TYPE_NAME (this_class)));
1206 TYPE_PACKAGE (this_class) = package_name;
377029eb 1207 return this_class;
1208 }
1209}
1210
1211/* Get the class of the CONSTANT_Class whose constant pool index is I. */
1212
1213tree
2883a3ed 1214get_class_constant (JCF *jcf, int i)
377029eb 1215{
1216 tree type;
bc031ffe 1217 gcc_assert (i > 0
1218 && i < JPOOL_SIZE (jcf)
1219 && (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) == CONSTANT_Class);
377029eb 1220
1221 if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
1222 {
1223 int name_index = JPOOL_USHORT1 (jcf, i);
1224 /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
8e452f9c 1225 const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
377029eb 1226 int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
f060a027 1227
377029eb 1228 if (name[0] == '[') /* Handle array "classes". */
8e452f9c 1229 type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
377029eb 1230 else
1231 {
1232 tree cname = unmangle_classname (name, nlength);
1233 type = lookup_class (cname);
1234 }
573aba85 1235 jcf->cpool.data[i].t = type;
377029eb 1236 JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
1237 }
1238 else
573aba85 1239 type = jcf->cpool.data[i].t;
377029eb 1240 return type;
1241}
1242
57fc05de 1243/* Read a class with the fully qualified-name NAME.
1244 Return 1 iff we read the requested file.
1245 (It is still possible we failed if the file did not
1246 define the class it is supposed to.) */
1247
1248int
2883a3ed 1249read_class (tree name)
377029eb 1250{
1251 JCF this_jcf, *jcf;
ead29d98 1252 tree icv, klass = NULL_TREE;
377029eb 1253 tree save_current_class = current_class;
7d709f85 1254 tree save_output_class = output_class;
7512c2db 1255 location_t save_location = input_location;
377029eb 1256 JCF *save_current_jcf = current_jcf;
377029eb 1257
de9c2bdc 1258 if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
74aad24c 1259 {
ead29d98 1260 klass = TREE_TYPE (icv);
1261 jcf = TYPE_JCF (klass);
de9c2bdc 1262 }
1263 else
1264 jcf = NULL;
1265
1266 if (jcf == NULL)
1267 {
121bd24d 1268 const char* path_name;
de9c2bdc 1269 this_jcf.zipd = NULL;
1270 jcf = &this_jcf;
121bd24d 1271
1272 path_name = find_class (IDENTIFIER_POINTER (name),
1273 IDENTIFIER_LENGTH (name),
7e476713 1274 &this_jcf);
121bd24d 1275 if (path_name == 0)
d7c47c0e 1276 return 0;
121bd24d 1277 else
e47a6f81 1278 free(CONST_CAST (char *, path_name));
74aad24c 1279 }
de9c2bdc 1280
1281 current_jcf = jcf;
377029eb 1282
ead29d98 1283 if (klass == NULL_TREE || ! CLASS_PARSED_P (klass))
0574932f 1284 {
ead29d98 1285 output_class = current_class = klass;
7e476713 1286 if (JCF_SEEN_IN_ZIP (current_jcf))
1287 read_zip_member(current_jcf,
1288 current_jcf->zipd, current_jcf->zipd->zipf);
1289 jcf_parse (current_jcf);
1290 /* Parsing might change the class, in which case we have to
1291 put it back where we found it. */
ead29d98 1292 if (current_class != klass && icv != NULL_TREE)
7e476713 1293 TREE_TYPE (icv) = current_class;
ead29d98 1294 klass = current_class;
0574932f 1295 }
ead29d98 1296 layout_class (klass);
1297 load_inner_classes (klass);
377029eb 1298
7d709f85 1299 output_class = save_output_class;
1300 current_class = save_current_class;
7512c2db 1301 input_location = save_location;
377029eb 1302 current_jcf = save_current_jcf;
57fc05de 1303 return 1;
1304}
1305
1306/* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
1307 called from the parser, otherwise it's a RECORD_TYPE node. If
1308 VERBOSE is 1, print error message on failure to load a class. */
57fc05de 1309void
2883a3ed 1310load_class (tree class_or_name, int verbose)
57fc05de 1311{
6eb95932 1312 tree name, saved;
a4ccc41f 1313 int class_loaded = 0;
1314 tree class_decl = NULL_TREE;
1315 bool is_compiled_class = false;
1316
1317 /* We've already failed, don't try again. */
1318 if (TREE_CODE (class_or_name) == RECORD_TYPE
1319 && TYPE_DUMMY (class_or_name))
1320 return;
57fc05de 1321
1322 /* class_or_name can be the name of the class we want to load */
1323 if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
1324 name = class_or_name;
1325 /* In some cases, it's a dependency that we process earlier that
1326 we though */
1327 else if (TREE_CODE (class_or_name) == TREE_LIST)
1328 name = TYPE_NAME (TREE_PURPOSE (class_or_name));
1329 /* Or it's a type in the making */
1330 else
ddfa1692 1331 name = DECL_NAME (TYPE_NAME (class_or_name));
1332
a4ccc41f 1333 class_decl = IDENTIFIER_CLASS_VALUE (name);
1334 if (class_decl != NULL_TREE)
1335 {
1336 tree type = TREE_TYPE (class_decl);
1337 is_compiled_class
1338 = ((TYPE_JCF (type) && JCF_SEEN_IN_ZIP (TYPE_JCF (type)))
1339 || CLASS_FROM_CURRENTLY_COMPILED_P (type));
1340 }
1341
6eb95932 1342 saved = name;
a4ccc41f 1343
1344 /* If flag_verify_invocations is unset, we don't try to load a class
1345 unless we're looking for Object (which is fixed by the ABI) or
1346 it's a class that we're going to compile. */
1347 if (flag_verify_invocations
1348 || class_or_name == object_type_node
1349 || is_compiled_class
1350 || TREE_CODE (class_or_name) == IDENTIFIER_NODE)
6eb95932 1351 {
a4ccc41f 1352 while (1)
1353 {
384eaf80 1354 const char *separator;
6eb95932 1355
a4ccc41f 1356 /* We've already loaded it. */
1357 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE)
1358 {
1359 tree tmp_decl = IDENTIFIER_CLASS_VALUE (name);
1360 if (CLASS_PARSED_P (TREE_TYPE (tmp_decl)))
1361 break;
1362 }
1363
1364 if (read_class (name))
1365 break;
6eb95932 1366
a4ccc41f 1367 /* We failed loading name. Now consider that we might be looking
7a974467 1368 for an inner class. */
a4ccc41f 1369 if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
1370 || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
384eaf80 1371 name = get_identifier_with_length (IDENTIFIER_POINTER (name),
1372 (separator
1373 - IDENTIFIER_POINTER (name)));
a4ccc41f 1374 /* Otherwise, we failed, we bail. */
1375 else
da37f909 1376 break;
6eb95932 1377 }
6eb95932 1378
a4ccc41f 1379 {
1380 /* have we found the class we're looking for? */
1381 tree type_decl = IDENTIFIER_CLASS_VALUE (saved);
1382 tree type = type_decl ? TREE_TYPE (type_decl) : NULL;
1383 class_loaded = type && CLASS_PARSED_P (type);
1384 }
1385 }
1386
1387 if (!class_loaded)
1388 {
6548e808 1389 if (flag_verify_invocations || ! flag_indirect_dispatch)
a4ccc41f 1390 {
1391 if (verbose)
1392 error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
1393 }
1394 else if (verbose)
1395 {
1396 /* This is just a diagnostic during testing, not a real problem. */
1397 if (!quiet_flag)
c3ceba8e 1398 warning (0, "cannot find file for class %s",
1399 IDENTIFIER_POINTER (saved));
a4ccc41f 1400
1401 /* Fake it. */
1402 if (TREE_CODE (class_or_name) == RECORD_TYPE)
1403 {
1404 set_super_info (0, class_or_name, object_type_node, 0);
1405 TYPE_DUMMY (class_or_name) = 1;
1406 /* We won't be able to output any debug info for this class. */
1407 DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;
1408 }
1409 }
1410 }
377029eb 1411}
1412
377029eb 1413/* Parse the .class file JCF. */
1414
9140e457 1415static void
2883a3ed 1416jcf_parse (JCF* jcf)
377029eb 1417{
1418 int i, code;
7e37b1c3 1419
1420 bitmap_clear (field_offsets);
377029eb 1421
1422 if (jcf_parse_preamble (jcf) != 0)
c05be867 1423 fatal_error (input_location, "not a valid Java .class file");
377029eb 1424 code = jcf_parse_constant_pool (jcf);
1425 if (code != 0)
c05be867 1426 fatal_error (input_location, "error while parsing constant pool");
377029eb 1427 code = verify_constant_pool (jcf);
1428 if (code > 0)
c05be867 1429 fatal_error (input_location, "error in constant pool entry #%d\n", code);
377029eb 1430
1431 jcf_parse_class (jcf);
1432 if (main_class == NULL_TREE)
1433 main_class = current_class;
1434 if (! quiet_flag && TYPE_NAME (current_class))
c0c735e9 1435 fprintf (stderr, " %s %s",
1436 (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class",
377029eb 1437 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
0574932f 1438 if (CLASS_PARSED_P (current_class))
1439 {
1440 /* FIXME - where was first time */
c05be867 1441 fatal_error (input_location,
1442 "reading class %s for the second time from %s",
0574932f 1443 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
1444 jcf->filename);
1445 }
1446 CLASS_PARSED_P (current_class) = 1;
377029eb 1447
1448 for (i = 1; i < JPOOL_SIZE(jcf); i++)
1449 {
1450 switch (JPOOL_TAG (jcf, i))
1451 {
1452 case CONSTANT_Class:
1453 get_class_constant (jcf, i);
1454 break;
1455 }
1456 }
1457
1458 code = jcf_parse_fields (jcf);
1459 if (code != 0)
c05be867 1460 fatal_error (input_location, "error while parsing fields");
377029eb 1461 code = jcf_parse_methods (jcf);
1462 if (code != 0)
c05be867 1463 fatal_error (input_location, "error while parsing methods");
377029eb 1464 code = jcf_parse_final_attributes (jcf);
1465 if (code != 0)
c05be867 1466 fatal_error (input_location, "error while parsing final attributes");
65bf3316 1467
1468 if (TYPE_REFLECTION_DATA (current_class))
1469 annotation_write_byte (JV_DONE_ATTR);
1470
931b0a0f 1471 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
377029eb 1472
1473 /* The fields of class_type_node are already in correct order. */
1474 if (current_class != class_type_node && current_class != object_type_node)
1475 TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
1476
7523afef 1477 if (current_class == object_type_node)
7e476713 1478 layout_class_methods (object_type_node);
7523afef 1479 else
f1f41a6c 1480 vec_safe_push (all_class_list, TYPE_NAME (current_class));
cb18c572 1481}
4654e794 1482
cb18c572 1483/* If we came across inner classes, load them now. */
1484static void
2883a3ed 1485load_inner_classes (tree cur_class)
cb18c572 1486{
1487 tree current;
1488 for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
4654e794 1489 current = TREE_CHAIN (current))
a24242f9 1490 {
1491 tree name = DECL_NAME (TREE_PURPOSE (current));
1492 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
7c779a81 1493 if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
1494 && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
a24242f9 1495 load_class (name, 1);
1496 }
377029eb 1497}
1498
95df7640 1499static void
1500duplicate_class_warning (const char *filename)
1501{
1502 location_t warn_loc;
931b0a0f 1503 linemap_add (line_table, LC_RENAME, 0, filename, 0);
1504 warn_loc = linemap_line_start (line_table, 0, 1);
5fb6a912 1505 warning_at (warn_loc, 0, "duplicate class will only be compiled once");
95df7640 1506}
1507
65bf3316 1508static void
1509java_layout_seen_class_methods (void)
1510{
43d03f3a 1511 unsigned start = 0;
f1f41a6c 1512 unsigned end = vec_safe_length (all_class_list);
65bf3316 1513
1514 while (1)
1515 {
43d03f3a 1516 unsigned ix;
1517 unsigned new_length;
1518
1519 for (ix = start; ix != end; ix++)
65bf3316 1520 {
f1f41a6c 1521 tree decl = (*all_class_list)[ix];
65bf3316 1522 tree cls = TREE_TYPE (decl);
1523
1524 input_location = DECL_SOURCE_LOCATION (decl);
1525
1526 if (! CLASS_LOADED_P (cls))
1527 load_class (cls, 0);
1528
1529 layout_class_methods (cls);
1530 }
1531
1532 /* Note that new classes might have been added while laying out
1533 methods, changing the value of all_class_list. */
f1f41a6c 1534 new_length = vec_safe_length (all_class_list);
43d03f3a 1535 if (end != new_length)
65bf3316 1536 {
43d03f3a 1537 start = end;
1538 end = new_length;
65bf3316 1539 }
1540 else
1541 break;
1542 }
1543}
1544
003019ba 1545static void
2883a3ed 1546parse_class_file (void)
377029eb 1547{
a82984ec 1548 tree method;
7512c2db 1549 location_t save_location = input_location;
377029eb 1550
b82a0849 1551 java_layout_seen_class_methods ();
7523afef 1552
da37f909 1553 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
3e04ac91 1554 {
1555 /* Re-enter the current file. */
1556 expanded_location loc = expand_location (input_location);
1557 linemap_add (line_table, LC_ENTER, 0, loc.file, loc.line);
1558 }
a4ccc41f 1559 file_start_location = input_location;
3df42822 1560 (*debug_hooks->start_source_file) (LOCATION_LINE (input_location),
1561 LOCATION_FILE (input_location));
377029eb 1562
699a0022 1563 java_mark_class_local (current_class);
1564
757f6c6c 1565 gen_indirect_dispatch_tables (current_class);
1566
e6ec293d 1567 for (method = TYPE_METHODS (current_class);
1767a056 1568 method != NULL_TREE; method = DECL_CHAIN (method))
377029eb 1569 {
1570 JCF *jcf = current_jcf;
1571
a4ccc41f 1572 if (METHOD_ABSTRACT (method) || METHOD_DUMMY (method))
377029eb 1573 continue;
1574
47efb553 1575 if (METHOD_NATIVE (method))
1576 {
728b4c29 1577 tree arg;
1578 int decl_max_locals;
1579
47efb553 1580 if (! flag_jni)
1581 continue;
728b4c29 1582 /* We need to compute the DECL_MAX_LOCALS. We need to take
1583 the wide types into account too. */
1584 for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0;
1585 arg != end_params_node;
1586 arg = TREE_CHAIN (arg), decl_max_locals += 1)
1587 {
1588 if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
1589 decl_max_locals += 1;
1590 }
1591 DECL_MAX_LOCALS (method) = decl_max_locals;
47efb553 1592 start_java_method (method);
1593 give_name_to_locals (jcf);
4ee9c684 1594 *get_stmts () = build_jni_stub (method);
47efb553 1595 end_java_method ();
1596 continue;
1597 }
1598
377029eb 1599 if (DECL_CODE_OFFSET (method) == 0)
1600 {
9fb34998 1601 current_function_decl = method;
377029eb 1602 error ("missing Code attribute");
1603 continue;
1604 }
1605
ce4f2a3c 1606 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (current_class));
377029eb 1607 if (DECL_LINENUMBERS_OFFSET (method))
1608 {
fdbb243d 1609 int i;
cd31eb43 1610 int min_line = 0;
fdbb243d 1611 unsigned char *ptr;
377029eb 1612 JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
1613 linenumber_count = i = JCF_readu2 (jcf);
1614 linenumber_table = ptr = jcf->read_ptr;
1615
1616 for (ptr += 2; --i >= 0; ptr += 4)
1617 {
1618 int line = GET_u2 (ptr);
3df42822 1619 /* Set initial line of input_location to smallest
1620 * linenumber.
377029eb 1621 * Needs to be set before init_function_start. */
cd31eb43 1622 if (min_line == 0 || line < min_line)
1623 min_line = line;
1624 }
cd31eb43 1625 if (min_line != 0)
931b0a0f 1626 input_location = linemap_line_start (line_table, min_line, 1);
377029eb 1627 }
1628 else
1629 {
1630 linenumber_table = NULL;
1631 linenumber_count = 0;
1632 }
1633
1634 start_java_method (method);
1635
713ada26 1636 note_instructions (jcf, method);
1637
377029eb 1638 give_name_to_locals (jcf);
1639
d3dc666e 1640 /* Bump up start_label_pc_this_method so we get a unique label number
1641 and reset highest_label_pc_this_method. */
1642 if (highest_label_pc_this_method >= 0)
1643 {
1644 /* We adjust to the next multiple of 1000. This is just a frill
1645 so the last 3 digits of the label number match the bytecode
1646 offset, which might make debugging marginally more convenient. */
1647 start_label_pc_this_method
1648 = ((((start_label_pc_this_method + highest_label_pc_this_method)
1649 / 1000)
1650 + 1)
1651 * 1000);
1652 highest_label_pc_this_method = -1;
1653 }
1654
4ee9c684 1655 /* Convert bytecode to trees. */
377029eb 1656 expand_byte_code (jcf, method);
1657
1658 end_java_method ();
1659 }
1660
5b0a320e 1661 finish_class ();
377029eb 1662
cd31eb43 1663 (*debug_hooks->end_source_file) (LOCATION_LINE (save_location));
7512c2db 1664 input_location = save_location;
377029eb 1665}
1666
f1f41a6c 1667static vec<tree, va_gc> *predefined_filenames;
43d03f3a 1668
fcffb974 1669void
2883a3ed 1670add_predefined_file (tree name)
fcffb974 1671{
f1f41a6c 1672 vec_safe_push (predefined_filenames, name);
fcffb974 1673}
1674
1675int
2883a3ed 1676predefined_filename_p (tree node)
53bd31b5 1677{
43d03f3a 1678 unsigned ix;
1679 tree f;
1680
f1f41a6c 1681 FOR_EACH_VEC_SAFE_ELT (predefined_filenames, ix, f)
43d03f3a 1682 if (f == node)
1683 return 1;
fcffb974 1684
53bd31b5 1685 return 0;
1686}
1687
b5530559 1688/* Generate a function that does all static initialization for this
1689 translation unit. */
1690
1691static void
1692java_emit_static_constructor (void)
1693{
1694 tree body = NULL;
1695
1696 emit_register_classes (&body);
1697 write_resource_constructor (&body);
1698
1699 if (body)
acd48105 1700 {
1701 tree name = get_identifier ("_Jv_global_static_constructor");
1702
1703 tree decl
e60a6f7b 1704 = build_decl (input_location, FUNCTION_DECL, name,
bfa624b3 1705 build_function_type_list (void_type_node, NULL_TREE));
acd48105 1706
e60a6f7b 1707 tree resdecl = build_decl (input_location,
1708 RESULT_DECL, NULL_TREE, void_type_node);
acd48105 1709 DECL_ARTIFICIAL (resdecl) = 1;
1710 DECL_RESULT (decl) = resdecl;
1711 current_function_decl = decl;
1712 allocate_struct_function (decl, false);
1713
1714 TREE_STATIC (decl) = 1;
1715 TREE_USED (decl) = 1;
1716 DECL_ARTIFICIAL (decl) = 1;
1717 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1718 DECL_SAVED_TREE (decl) = body;
1719 DECL_UNINLINABLE (decl) = 1;
1720
1721 DECL_INITIAL (decl) = make_node (BLOCK);
1722 TREE_USED (DECL_INITIAL (decl)) = 1;
1723
1724 DECL_STATIC_CONSTRUCTOR (decl) = 1;
1725 java_genericize (decl);
35ee1c66 1726 cgraph_node::finalize_function (decl, false);
acd48105 1727 }
b5530559 1728}
1729
c616456b 1730
b78207a0 1731void
b8ba44e7 1732java_parse_file (void)
377029eb 1733{
489283da 1734 int filename_count = 0;
da37f909 1735 location_t save_location = input_location;
121bd24d 1736 char *file_list = NULL, *list, *next;
9d1847a0 1737 tree node;
cb18c572 1738 FILE *finput = NULL;
0750f30d 1739 int in_quotes = 0;
43d03f3a 1740 unsigned ix;
0750f30d 1741
65bf3316 1742 bitmap_obstack_initialize (&bit_obstack);
1743 field_offsets = BITMAP_ALLOC (&bit_obstack);
1744
489283da 1745 if (flag_filelist_file)
1746 {
1747 int avail = 2000;
da37f909 1748 finput = fopen (main_input_filename, "r");
489283da 1749 if (finput == NULL)
c05be867 1750 fatal_error (input_location,
1751 "can%'t open %s: %m", LOCATION_FILE (input_location));
4c36ffe6 1752 list = XNEWVEC (char, avail);
489283da 1753 next = list;
1754 for (;;)
1755 {
1756 int count;
1757 if (avail < 500)
1758 {
1759 count = next - list;
1760 avail = 2 * (count + avail);
25a1c410 1761 list = XRESIZEVEC (char, list, avail);
489283da 1762 next = list + count;
1763 avail = avail - count;
1764 }
851d9296 1765 /* Subtract one to guarantee space for final '\0'. */
489283da 1766 count = fread (next, 1, avail - 1, finput);
1767 if (count == 0)
1768 {
1769 if (! feof (finput))
c05be867 1770 fatal_error (input_location, "error closing %s: %m",
3df42822 1771 LOCATION_FILE (input_location));
489283da 1772 *next = '\0';
1773 break;
1774 }
1775 avail -= count;
1776 next += count;
1777 }
1778 fclose (finput);
cb18c572 1779 finput = NULL;
121bd24d 1780 file_list = list;
489283da 1781 }
1782 else
e47a6f81 1783 list = CONST_CAST (char *, main_input_filename);
489283da 1784
905df476 1785 while (list)
377029eb 1786 {
489283da 1787 for (next = list; ; )
c31939b7 1788 {
cb18c572 1789 char ch = *next;
0750f30d 1790 if (flag_filelist_file && ! in_quotes
1791 && (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
1792 || ch == '&') /* FIXME */)
489283da 1793 {
1794 if (next == list)
1795 {
1796 next++;
1797 list = next;
1798 continue;
1799 }
1800 else
1801 {
1802 *next++ = '\0';
1803 break;
1804 }
1805 }
0750f30d 1806 if (flag_filelist_file && ch == '"')
1807 {
1808 in_quotes = ! in_quotes;
1809 *next++ = '\0';
1810 if (in_quotes)
1811 list = next;
1812 else
1813 break;
1814 }
489283da 1815 if (ch == '\0')
1816 {
1817 next = NULL;
1818 break;
1819 }
1820 next++;
c31939b7 1821 }
1822
65bf3316 1823 /* Exclude .java files. */
1824 if (strlen (list) > 5 && ! strcmp (list + strlen (list) - 5, ".java"))
1825 {
1826 /* Nothing. */
1827 }
1828 else if (list[0])
c31939b7 1829 {
da37f909 1830 node = get_identifier (list);
53bd31b5 1831
489283da 1832 filename_count++;
1833
da37f909 1834 /* Exclude file that we see twice on the command line. */
53bd31b5 1835
da37f909 1836 if (IS_A_COMMAND_LINE_FILENAME_P (node))
95df7640 1837 duplicate_class_warning (IDENTIFIER_POINTER (node));
53bd31b5 1838 else
1839 {
23bdc9ca 1840 build_translation_unit_decl (node);
53bd31b5 1841 IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
53bd31b5 1842 }
c31939b7 1843 }
1844 list = next;
1845 }
c31939b7 1846
dd045aee 1847 free (file_list);
121bd24d 1848
489283da 1849 if (filename_count == 0)
c3ceba8e 1850 warning (0, "no input file specified");
489283da 1851
7ad6876a 1852 if (resource_name)
1853 {
76c6f48d 1854 const char *resource_filename;
7ad6876a 1855
1856 /* Only one resource file may be compiled at a time. */
f1f41a6c 1857 gcc_assert (all_translation_units->length () == 1);
7ad6876a 1858
23bdc9ca 1859 resource_filename
f1f41a6c 1860 = IDENTIFIER_POINTER (DECL_NAME ((*all_translation_units)[0]));
7ad6876a 1861 compile_resource_file (resource_name, resource_filename);
657424c4 1862
b5530559 1863 goto finish;
7ad6876a 1864 }
1865
a31ac40f 1866 current_jcf = main_jcf;
f1f41a6c 1867 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
c31939b7 1868 {
cb18c572 1869 unsigned char magic_string[4];
121bd24d 1870 char *real_path;
57763f86 1871 uint32 magic = 0;
da37f909 1872 tree name = DECL_NAME (node);
f1d4bfad 1873 tree real_file;
da37f909 1874 const char *filename = IDENTIFIER_POINTER (name);
c31939b7 1875
a31ac40f 1876 /* Skip already parsed files */
121bd24d 1877 real_path = lrealpath (filename);
1878 real_file = get_identifier (real_path);
1879 free (real_path);
f1d4bfad 1880 if (HAS_BEEN_ALREADY_PARSED_P (real_file))
a31ac40f 1881 continue;
da37f909 1882
a31ac40f 1883 /* Close previous descriptor, if any */
cb18c572 1884 if (finput && fclose (finput))
c05be867 1885 fatal_error (input_location,
1886 "can%'t close input file %s: %m", main_input_filename);
a31ac40f 1887
da37f909 1888 finput = fopen (filename, "rb");
cb18c572 1889 if (finput == NULL)
c05be867 1890 fatal_error (input_location, "can%'t open %s: %m", filename);
caa8fa37 1891
a31ac40f 1892#ifdef IO_BUFFER_SIZE
0cc50a21 1893 setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
a31ac40f 1894 _IOFBF, IO_BUFFER_SIZE);
1895#endif
c31939b7 1896
cb18c572 1897 /* Figure what kind of file we're dealing with */
57763f86 1898 if (fread (magic_string, 1, 4, finput) == 4)
1899 {
1900 fseek (finput, 0L, SEEK_SET);
1901 magic = GET_u4 (magic_string);
1902 }
cb18c572 1903 if (magic == 0xcafebabe)
c31939b7 1904 {
cb18c572 1905 CLASS_FILE_P (node) = 1;
25a27413 1906 current_jcf = ggc_cleared_alloc<JCF> ();
cb18c572 1907 current_jcf->read_state = finput;
1908 current_jcf->filbuf = jcf_filbuf_from_stdio;
c31939b7 1909 jcf_parse (current_jcf);
da37f909 1910 DECL_SOURCE_LOCATION (node) = file_start_location;
cb18c572 1911 TYPE_JCF (current_class) = current_jcf;
95df7640 1912 if (CLASS_FROM_CURRENTLY_COMPILED_P (current_class))
1913 {
1914 /* We've already compiled this class. */
1915 duplicate_class_warning (filename);
1916 continue;
1917 }
cb18c572 1918 CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
da37f909 1919 TREE_TYPE (node) = current_class;
cb18c572 1920 }
1921 else if (magic == (JCF_u4)ZIPMAGIC)
1922 {
25a27413 1923 main_jcf = ggc_cleared_alloc<JCF> ();
cb18c572 1924 main_jcf->read_state = finput;
1925 main_jcf->filbuf = jcf_filbuf_from_stdio;
931b0a0f 1926 linemap_add (line_table, LC_ENTER, false, filename, 0);
1927 input_location = linemap_line_start (line_table, 0, 1);
da37f909 1928 if (open_in_zip (main_jcf, filename, NULL, 0) < 0)
c05be867 1929 fatal_error (input_location, "bad zip/jar file %s", filename);
cb18c572 1930 localToFile = SeenZipFiles;
caa8fa37 1931 /* Register all the classes defined there. */
25a1c410 1932 process_zip_dir ((FILE *) main_jcf->read_state);
931b0a0f 1933 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
cb18c572 1934 parse_zip_file_entries ();
cb18c572 1935 }
3a6ff634 1936 else if (magic == (JCF_u4) ZIPEMPTYMAGIC)
1937 {
1938 /* Ignore an empty input jar. */
1939 }
cb18c572 1940 else
1941 {
65bf3316 1942 gcc_unreachable ();
1943#if 0
23b7f2e1 1944 java_push_parser_context ();
1945 java_parser_context_save_global ();
f1d4bfad 1946
cd31eb43 1947 parse_source_file_1 (real_file, filename, finput);
23b7f2e1 1948 java_parser_context_restore_global ();
1949 java_pop_parser_context (1);
931b0a0f 1950 linemap_add (line_table, LC_LEAVE, false, NULL, 0);
cd31eb43 1951#endif
c31939b7 1952 }
377029eb 1953 }
6b0c8920 1954
f1f41a6c 1955 FOR_EACH_VEC_ELT (*all_translation_units, ix, node)
cb18c572 1956 {
da37f909 1957 input_location = DECL_SOURCE_LOCATION (node);
cb18c572 1958 if (CLASS_FILE_P (node))
1959 {
a4ccc41f 1960 /* FIXME: These two flags really should be independent. We
1961 should be able to compile fully binary compatible, but
1962 with flag_verify_invocations on. */
1963 flag_verify_invocations = ! flag_indirect_dispatch;
da37f909 1964 output_class = current_class = TREE_TYPE (node);
a4ccc41f 1965
cb18c572 1966 current_jcf = TYPE_JCF (current_class);
0574932f 1967 layout_class (current_class);
cb18c572 1968 load_inner_classes (current_class);
1969 parse_class_file ();
0574932f 1970 JCF_FINISH (current_jcf);
cb18c572 1971 }
1972 }
da37f909 1973 input_location = save_location;
489283da 1974
65bf3316 1975 bitmap_obstack_release (&bit_obstack);
1976
b5530559 1977 finish:
1978 /* Arrange for any necessary initialization to happen. */
1979 java_emit_static_constructor ();
c616456b 1980 gcc_assert (global_bindings_p ());
3a1c9df2 1981
1982 /* Do final processing on globals. */
1983 global_decl_processing ();
caa8fa37 1984}
1985
b5530559 1986
caa8fa37 1987/* Return the name of the class corresponding to the name of the file
1988 in this zip entry. The result is newly allocated using ALLOC. */
1989static char *
1990compute_class_name (struct ZipDirectory *zdir)
1991{
1992 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1993 char *class_name;
4d905f63 1994 int i;
a4ccc41f 1995 int filename_length = zdir->filename_length;
4d905f63 1996
a4ccc41f 1997 while (filename_length > 2 && strncmp (class_name_in_zip_dir, "./", 2) == 0)
1998 {
1999 class_name_in_zip_dir += 2;
2000 filename_length -= 2;
2001 }
4d905f63 2002
a4ccc41f 2003 filename_length -= strlen (".class");
4c36ffe6 2004 class_name = XNEWVEC (char, filename_length + 1);
4d905f63 2005 memcpy (class_name, class_name_in_zip_dir, filename_length);
2006 class_name [filename_length] = '\0';
2007
2008 for (i = 0; i < filename_length; i++)
2009 if (class_name[i] == '/')
2010 class_name[i] = '.';
caa8fa37 2011
caa8fa37 2012 return class_name;
2013}
2014
2015/* Return 0 if we should skip this entry, 1 if it is a .class file, 2
2016 if it is a property file of some sort. */
2017static int
2018classify_zip_file (struct ZipDirectory *zdir)
2019{
2020 char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2021
2022 if (zdir->filename_length > 6
2023 && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
2024 ".class", 6))
2025 return 1;
2026
639e4191 2027 /* For now we drop the manifest, but not other information. */
90483006 2028 if (zdir->filename_length == 20
639e4191 2029 && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
caa8fa37 2030 return 0;
2031
2032 /* Drop directory entries. */
2033 if (zdir->filename_length > 0
2034 && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
2035 return 0;
2036
2037 return 2;
377029eb 2038}
2039
377029eb 2040/* Process all class entries found in the zip file. */
003019ba 2041static void
377029eb 2042parse_zip_file_entries (void)
2043{
2044 struct ZipDirectory *zdir;
2045 int i;
2046
de9c2bdc 2047 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2048 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
377029eb 2049 {
ead29d98 2050 tree klass;
377029eb 2051
caa8fa37 2052 switch (classify_zip_file (zdir))
377029eb 2053 {
caa8fa37 2054 case 0:
2055 continue;
377029eb 2056
caa8fa37 2057 case 1:
2058 {
2059 char *class_name = compute_class_name (zdir);
6a609ad9 2060 int previous_alias_set = -1;
ead29d98 2061 klass = lookup_class (get_identifier (class_name));
caa8fa37 2062 FREE (class_name);
ead29d98 2063 current_jcf = TYPE_JCF (klass);
2064 output_class = current_class = klass;
caa8fa37 2065
bc031ffe 2066 /* This is a dummy class, and now we're compiling it for
2067 real. */
ead29d98 2068 gcc_assert (! TYPE_DUMMY (klass));
a4ccc41f 2069
4d905f63 2070 /* This is for a corner case where we have a superclass
6a609ad9 2071 but no superclass fields.
4d905f63 2072
2073 This can happen if we earlier failed to lay out this
2074 class because its superclass was still in the process
2075 of being laid out; this occurs when we have recursive
6a609ad9 2076 class dependencies via inner classes. We must record
2077 the previous alias set and restore it after laying out
2078 the class.
2079
2080 FIXME: this really is a kludge. We should figure out a
2081 way to lay out the class properly before this
2082 happens. */
ead29d98 2083 if (TYPE_SIZE (klass) && CLASSTYPE_SUPER (klass)
2084 && integer_zerop (TYPE_SIZE (klass)))
6a609ad9 2085 {
ead29d98 2086 TYPE_SIZE (klass) = NULL_TREE;
2087 previous_alias_set = TYPE_ALIAS_SET (klass);
2088 TYPE_ALIAS_SET (klass) = -1;
6a609ad9 2089 }
4d905f63 2090
ead29d98 2091 if (! CLASS_LOADED_P (klass))
caa8fa37 2092 {
ead29d98 2093 if (! CLASS_PARSED_P (klass))
caa8fa37 2094 {
2095 read_zip_member (current_jcf, zdir, localToFile);
2096 jcf_parse (current_jcf);
2097 }
2098 layout_class (current_class);
2099 load_inner_classes (current_class);
2100 }
2101
6a609ad9 2102 if (previous_alias_set != -1)
ead29d98 2103 TYPE_ALIAS_SET (klass) = previous_alias_set;
6a609ad9 2104
caa8fa37 2105 if (TYPE_SIZE (current_class) != error_mark_node)
2106 {
caa8fa37 2107 parse_class_file ();
4c36ffe6 2108 free (current_jcf->buffer); /* No longer necessary */
caa8fa37 2109 /* Note: there is a way to free this buffer right after a
2110 class seen in a zip file has been parsed. The idea is the
2111 set its jcf in such a way that buffer will be reallocated
2112 the time the code for the class will be generated. FIXME. */
2113 }
2114 }
2115 break;
2116
2117 case 2:
2118 {
2119 char *file_name, *class_name_in_zip_dir, *buffer;
2120 JCF *jcf;
4c36ffe6 2121 file_name = XNEWVEC (char, zdir->filename_length + 1);
caa8fa37 2122 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2123 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2124 file_name[zdir->filename_length] = '\0';
4c36ffe6 2125 jcf = XNEW (JCF);
caa8fa37 2126 JCF_ZERO (jcf);
2127 jcf->read_state = finput;
2128 jcf->filbuf = jcf_filbuf_from_stdio;
caa8fa37 2129 jcf->classname = NULL;
2130 jcf->filename = file_name;
2131 jcf->zipd = zdir;
2132
2133 if (read_zip_member (jcf, zdir, localToFile) < 0)
c05be867 2134 fatal_error (input_location,
2135 "error while reading %s from zip file", file_name);
caa8fa37 2136
4c36ffe6 2137 buffer = XNEWVEC (char, zdir->filename_length + 1 +
caa8fa37 2138 (jcf->buffer_end - jcf->buffer));
2139 strcpy (buffer, file_name);
97d9c1af 2140 /* This is not a typo: we overwrite the trailing \0 of the
2141 file name; this is just how the data is laid out. */
2142 memcpy (buffer + zdir->filename_length,
caa8fa37 2143 jcf->buffer, jcf->buffer_end - jcf->buffer);
2144
2145 compile_resource_data (file_name, buffer,
2146 jcf->buffer_end - jcf->buffer);
2147 JCF_FINISH (jcf);
4c36ffe6 2148 free (jcf);
2149 free (buffer);
caa8fa37 2150 }
2151 break;
2152
2153 default:
bc031ffe 2154 gcc_unreachable ();
a31ac40f 2155 }
377029eb 2156 }
2157}
2158
2159/* Read all the entries of the zip file, creates a class and a JCF. Sets the
2160 jcf up for further processing and link it to the created class. */
2161
92b90214 2162static void
2163process_zip_dir (FILE *finput)
377029eb 2164{
2165 int i;
2166 ZipDirectory *zdir;
2167
de9c2bdc 2168 for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
2169 i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
377029eb 2170 {
2171 char *class_name, *file_name, *class_name_in_zip_dir;
ead29d98 2172 tree klass;
377029eb 2173 JCF *jcf;
377029eb 2174
2175 class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
2176
caa8fa37 2177 /* Here we skip non-class files; we handle them later. */
2178 if (classify_zip_file (zdir) != 1)
2179 continue;
377029eb 2180
caa8fa37 2181 class_name = compute_class_name (zdir);
4c36ffe6 2182 file_name = XNEWVEC (char, zdir->filename_length+1);
25a27413 2183 jcf = ggc_cleared_alloc<JCF> ();
377029eb 2184
377029eb 2185 strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
2186 file_name [zdir->filename_length] = '\0';
2187
ead29d98 2188 klass = lookup_class (get_identifier (class_name));
377029eb 2189
ead29d98 2190 if (CLASS_FROM_CURRENTLY_COMPILED_P (klass))
65bf3316 2191 {
2192 /* We've already compiled this class. */
2193 duplicate_class_warning (file_name);
2194 continue;
2195 }
2196 /* This function is only called when processing a zip file seen
2197 on the command line. */
ead29d98 2198 CLASS_FROM_CURRENTLY_COMPILED_P (klass) = 1;
65bf3316 2199
377029eb 2200 jcf->read_state = finput;
2201 jcf->filbuf = jcf_filbuf_from_stdio;
377029eb 2202 jcf->classname = class_name;
2203 jcf->filename = file_name;
de9c2bdc 2204 jcf->zipd = zdir;
377029eb 2205
ead29d98 2206 TYPE_JCF (klass) = jcf;
377029eb 2207 }
2208}
2209
1f3233d1 2210#include "gt-java-jcf-parse.h"
65bf3316 2211#include "gtype-java.h"