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