OSDN Git Service

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