OSDN Git Service

* decl.c (java_optimize_inline, dump_function): Remove.
[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   const char *save_input_filename = input_filename;
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_filename = save_input_filename;
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 void
697 init_outgoing_cpool (void)
698 {
699   outgoing_cpool = ggc_alloc_cleared (sizeof (struct CPool));
700 }
701
702 static void
703 parse_class_file (void)
704 {
705   tree method;
706   const char *save_input_filename = input_filename;
707   int save_lineno = input_line;
708
709   java_layout_seen_class_methods ();
710
711   input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
712   input_line = 0;
713   (*debug_hooks->start_source_file) (input_line, input_filename);
714   init_outgoing_cpool ();
715
716   /* Currently we always have to emit calls to _Jv_InitClass when
717      compiling from class files.  */
718   always_initialize_class_p = 1;
719
720   java_mark_class_local (current_class);
721
722   for (method = TYPE_METHODS (current_class);
723        method != NULL_TREE; method = TREE_CHAIN (method))
724     {
725       JCF *jcf = current_jcf;
726
727       if (METHOD_ABSTRACT (method))
728         continue;
729
730       if (METHOD_NATIVE (method))
731         {
732           tree arg;
733           int  decl_max_locals;
734
735           if (! flag_jni)
736             continue;
737           /* We need to compute the DECL_MAX_LOCALS. We need to take
738              the wide types into account too. */
739           for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0; 
740                arg != end_params_node;
741                arg = TREE_CHAIN (arg), decl_max_locals += 1)
742             {
743               if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
744                 decl_max_locals += 1;
745             }
746           DECL_MAX_LOCALS (method) = decl_max_locals;
747           start_java_method (method);
748           give_name_to_locals (jcf);
749           expand_expr_stmt (build_jni_stub (method));
750           end_java_method ();
751           continue;
752         }
753
754       if (DECL_CODE_OFFSET (method) == 0)
755         {
756           current_function_decl = method;
757           error ("missing Code attribute");
758           continue;
759         }
760
761       input_line = 0;
762       if (DECL_LINENUMBERS_OFFSET (method))
763         {
764           register int i;
765           register unsigned char *ptr;
766           JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
767           linenumber_count = i = JCF_readu2 (jcf);
768           linenumber_table = ptr = jcf->read_ptr;
769
770           for (ptr += 2; --i >= 0; ptr += 4)
771             {
772               int line = GET_u2 (ptr);
773               /* Set initial lineno lineno to smallest linenumber.
774                * Needs to be set before init_function_start. */
775               if (input_line == 0 || line < input_line)
776                 input_line = line;
777             }  
778         }
779       else
780         {
781           linenumber_table = NULL;
782           linenumber_count = 0;
783         }
784
785       start_java_method (method);
786
787       note_instructions (jcf, method);
788
789       give_name_to_locals (jcf);
790
791       /* Actually generate code. */
792       expand_byte_code (jcf, method);
793
794       end_java_method ();
795     }
796
797   if (flag_emit_class_files)
798     write_classfile (current_class);
799
800   finish_class ();
801
802   (*debug_hooks->end_source_file) (save_lineno);
803   input_filename = save_input_filename;
804   input_line = save_lineno;
805 }
806
807 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
808
809 static void
810 parse_source_file_1 (tree file, FILE *finput)
811 {
812   int save_error_count = java_error_count;
813   /* Mark the file as parsed */
814   HAS_BEEN_ALREADY_PARSED_P (file) = 1;
815
816   jcf_dependency_add_file (input_filename, 0);
817
818   lang_init_source (1);             /* Error msgs have no method prototypes */
819
820   /* There's no point in trying to find the current encoding unless we
821      are going to do something intelligent with it -- hence the test
822      for iconv.  */
823 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
824   setlocale (LC_CTYPE, "");
825   if (current_encoding == NULL)
826     current_encoding = nl_langinfo (CODESET);
827 #endif 
828   if (current_encoding == NULL || *current_encoding == '\0')
829     current_encoding = DEFAULT_ENCODING;
830
831   /* Initialize the parser */
832   java_init_lex (finput, current_encoding);
833   java_parse_abort_on_error ();
834
835   java_parse ();                    /* Parse and build partial tree nodes. */
836   java_parse_abort_on_error ();
837 }
838
839 /* Process a parsed source file, resolving names etc. */
840
841 static void
842 parse_source_file_2 (void)
843 {
844   int save_error_count = java_error_count;
845   java_complete_class ();           /* Parse unsatisfied class decl. */
846   java_parse_abort_on_error ();
847 }
848
849 static void
850 parse_source_file_3 (void)
851 {
852   int save_error_count = java_error_count;
853   java_check_circular_reference (); /* Check on circular references */
854   java_parse_abort_on_error ();
855   java_fix_constructors ();         /* Fix the constructors */
856   java_parse_abort_on_error ();
857   java_reorder_fields ();           /* Reorder the fields */
858 }
859
860 void
861 add_predefined_file (tree name)
862 {
863   predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
864 }
865
866 int
867 predefined_filename_p (tree node)
868 {
869   tree iter;
870
871   for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
872     {
873       if (TREE_VALUE (iter) == node)
874         return 1;
875     }
876   return 0;
877 }
878
879 void
880 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
881 {
882   int filename_count = 0;
883   char *list, *next;
884   tree node;
885   FILE *finput = NULL;
886
887   if (flag_filelist_file)
888     {
889       int avail = 2000;
890       finput = fopen (input_filename, "r");
891       if (finput == NULL)
892         fatal_error ("can't open %s: %m", input_filename);
893       list = xmalloc(avail);
894       next = list;
895       for (;;)
896         {
897           int count;
898           if (avail < 500)
899             {
900               count = next - list;
901               avail = 2 * (count + avail);
902               list = xrealloc (list, avail);
903               next = list + count;
904               avail = avail - count;
905             }
906           /* Subtract to to guarantee space for final '\0'. */
907           count = fread (next, 1, avail - 1, finput);
908           if (count == 0)
909             {
910               if (! feof (finput))
911                 fatal_error ("error closing %s: %m", input_filename);
912               *next = '\0';
913               break;
914             }
915           avail -= count;
916           next += count;
917         }
918       fclose (finput);
919       finput = NULL;
920     }
921   else
922     list = xstrdup (input_filename);
923
924   do 
925     {
926       for (next = list; ; )
927         {
928           char ch = *next;
929           if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
930               || ch == '&' /* FIXME */)
931             {
932               if (next == list)
933                 {
934                   next++;
935                   list = next;
936                   continue;
937                 }
938               else
939                 {
940                   *next++ = '\0';
941                   break;
942                 }
943             }
944           if (ch == '\0')
945             {
946               next = NULL;
947               break;
948             }
949           next++;
950         }
951
952       if (list[0]) 
953         {
954           char *value;
955           tree id;
956           int twice = 0;
957
958           int len = strlen (list);
959
960           obstack_grow0 (&temporary_obstack, list, len);
961           value = obstack_finish (&temporary_obstack);
962
963           filename_count++;
964
965           /* Exclude file that we see twice on the command line. For
966              all files except {Class,Error,Object,RuntimeException,String,
967              Throwable}.java we can rely on maybe_get_identifier. For
968              these files, we need to do a linear search of
969              current_file_list. This search happens only for these
970              files, presumably only when we're recompiling libgcj. */
971              
972           if ((id = maybe_get_identifier (value)))
973             {
974               if (predefined_filename_p (id))
975                 {
976                   tree c;
977                   for (c = current_file_list; c; c = TREE_CHAIN (c))
978                     if (TREE_VALUE (c) == id)
979                       twice = 1;
980                 }
981               else
982                 twice = 1;
983             }
984
985           if (twice)
986             {
987               const char *saved_input_filename = input_filename;
988               input_filename = value;
989               warning ("source file seen twice on command line and will be compiled only once");
990               input_filename = saved_input_filename;
991             }
992           else
993             {
994               BUILD_FILENAME_IDENTIFIER_NODE (node, value);
995               IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
996               current_file_list = tree_cons (NULL_TREE, node, 
997                                              current_file_list);
998             }
999         }
1000       list = next;
1001     }
1002   while (next);
1003
1004   if (filename_count == 0)
1005     warning ("no input file specified");
1006
1007   if (resource_name)
1008     {
1009       const char *resource_filename;
1010       
1011       /* Only one resource file may be compiled at a time.  */
1012       assert (TREE_CHAIN (current_file_list) == NULL);
1013
1014       resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
1015       compile_resource_file (resource_name, resource_filename);
1016
1017       return;
1018     }
1019
1020   current_jcf = main_jcf;
1021   current_file_list = nreverse (current_file_list);
1022   for (node = current_file_list; node; node = TREE_CHAIN (node))
1023     {
1024       unsigned char magic_string[4];
1025       uint32 magic = 0;
1026       tree name = TREE_VALUE (node);
1027
1028       /* Skip already parsed files */
1029       if (HAS_BEEN_ALREADY_PARSED_P (name))
1030         continue;
1031       
1032       /* Close previous descriptor, if any */
1033       if (finput && fclose (finput))
1034         fatal_error ("can't close input file %s: %m", main_input_filename);
1035       
1036       finput = fopen (IDENTIFIER_POINTER (name), "rb");
1037       if (finput == NULL)
1038         fatal_error ("can't open %s: %m", IDENTIFIER_POINTER (name));
1039
1040 #ifdef IO_BUFFER_SIZE
1041       setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1042                _IOFBF, IO_BUFFER_SIZE);
1043 #endif
1044       input_filename = IDENTIFIER_POINTER (name);
1045
1046       /* Figure what kind of file we're dealing with */
1047       if (fread (magic_string, 1, 4, finput) == 4)
1048         {
1049           fseek (finput, 0L, SEEK_SET);
1050           magic = GET_u4 (magic_string);
1051         }
1052       if (magic == 0xcafebabe)
1053         {
1054           CLASS_FILE_P (node) = 1;
1055           current_jcf = ggc_alloc (sizeof (JCF));
1056           JCF_ZERO (current_jcf);
1057           current_jcf->read_state = finput;
1058           current_jcf->filbuf = jcf_filbuf_from_stdio;
1059           jcf_parse (current_jcf);
1060           TYPE_JCF (current_class) = current_jcf;
1061           CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1062           TREE_PURPOSE (node) = current_class;
1063         }
1064       else if (magic == (JCF_u4)ZIPMAGIC)
1065         {
1066           ZIP_FILE_P (node) = 1;
1067           main_jcf = ggc_alloc (sizeof (JCF));
1068           JCF_ZERO (main_jcf);
1069           main_jcf->read_state = finput;
1070           main_jcf->filbuf = jcf_filbuf_from_stdio;
1071           if (open_in_zip (main_jcf, input_filename, NULL, 0) <  0)
1072             fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1073           localToFile = SeenZipFiles;
1074           /* Register all the classes defined there.  */
1075           process_zip_dir (main_jcf->read_state);
1076           parse_zip_file_entries ();
1077           /*
1078           for (each entry)
1079             CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1080           */
1081         }
1082       else
1083         {
1084           JAVA_FILE_P (node) = 1;
1085           java_push_parser_context ();
1086           java_parser_context_save_global ();
1087           parse_source_file_1 (name, finput);
1088           java_parser_context_restore_global ();
1089           java_pop_parser_context (1);
1090         }
1091     }
1092
1093   for (ctxp = ctxp_for_generation;  ctxp;  ctxp = ctxp->next)
1094     {
1095       input_filename = ctxp->filename;
1096       parse_source_file_2 ();
1097     }
1098
1099   for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1100     {
1101       input_filename = ctxp->filename;
1102       parse_source_file_3 ();
1103     }
1104
1105   for (node = current_file_list; node; node = TREE_CHAIN (node))
1106     {
1107       input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1108       if (CLASS_FILE_P (node))
1109         {
1110           current_class = TREE_PURPOSE (node);
1111           current_jcf = TYPE_JCF (current_class);
1112           layout_class (current_class);
1113           load_inner_classes (current_class);
1114           parse_class_file ();
1115           JCF_FINISH (current_jcf);
1116         }
1117     }
1118   input_filename = main_input_filename;
1119
1120   java_expand_classes ();
1121   if (!java_report_errors () && !flag_syntax_only)
1122     {
1123       /* Optimize and expand all classes compiled from source.  */
1124       cgraph_finalize_compilation_unit ();
1125       cgraph_optimize ();
1126       java_finish_classes ();
1127
1128       /* Emit the .jcf section.  */
1129       emit_register_classes ();
1130       if (flag_indirect_dispatch)
1131         emit_offset_symbol_table ();
1132     }
1133
1134   write_resource_constructor ();
1135 }
1136
1137 /* Return the name of the class corresponding to the name of the file
1138    in this zip entry.  The result is newly allocated using ALLOC.  */
1139 static char *
1140 compute_class_name (struct ZipDirectory *zdir)
1141 {
1142   char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1143   char *class_name;
1144   int j;
1145
1146   class_name = ALLOC (zdir->filename_length + 1 - 6);
1147   strncpy (class_name, class_name_in_zip_dir, zdir->filename_length - 6);
1148   class_name [zdir->filename_length - 6] = '\0';
1149   for (j = 0; class_name[j]; ++j)
1150     class_name[j] = class_name[j] == '/' ? '.' : class_name[j];
1151   return class_name;
1152 }
1153
1154 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
1155    if it is a property file of some sort.  */
1156 static int
1157 classify_zip_file (struct ZipDirectory *zdir)
1158 {
1159   char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1160
1161   if (zdir->filename_length > 6
1162       && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
1163                    ".class", 6))
1164     return 1;
1165
1166   /* For now we drop the manifest and other information.  Maybe it
1167      would make more sense to compile it in?  */
1168   if (zdir->filename_length > 8
1169       && !strncmp (class_name_in_zip_dir, "META-INF/", 9))
1170     return 0;
1171
1172   /* Drop directory entries.  */
1173   if (zdir->filename_length > 0
1174       && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
1175     return 0;
1176
1177   return 2;
1178 }
1179
1180 /* Process all class entries found in the zip file.  */
1181 static void
1182 parse_zip_file_entries (void)
1183 {
1184   struct ZipDirectory *zdir;
1185   int i;
1186
1187   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1188        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1189     {
1190       tree class;
1191
1192       switch (classify_zip_file (zdir))
1193         {
1194         case 0:
1195           continue;
1196
1197         case 1:
1198           {
1199             char *class_name = compute_class_name (zdir);
1200             class = lookup_class (get_identifier (class_name));
1201             FREE (class_name);
1202             current_jcf = TYPE_JCF (class);
1203             current_class = class;
1204
1205             if (! CLASS_LOADED_P (class))
1206               {
1207                 if (! CLASS_PARSED_P (class))
1208                   {
1209                     read_zip_member (current_jcf, zdir, localToFile);
1210                     jcf_parse (current_jcf);
1211                   }
1212                 layout_class (current_class);
1213                 load_inner_classes (current_class);
1214               }
1215
1216             if (TYPE_SIZE (current_class) != error_mark_node)
1217               {
1218                 input_filename = current_jcf->filename;
1219                 parse_class_file ();
1220                 FREE (current_jcf->buffer); /* No longer necessary */
1221                 /* Note: there is a way to free this buffer right after a
1222                    class seen in a zip file has been parsed. The idea is the
1223                    set its jcf in such a way that buffer will be reallocated
1224                    the time the code for the class will be generated. FIXME. */
1225               }
1226           }
1227           break;
1228
1229         case 2:
1230           {
1231             char *file_name, *class_name_in_zip_dir, *buffer;
1232             JCF *jcf;
1233             file_name = ALLOC (zdir->filename_length + 1);
1234             class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1235             strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1236             file_name[zdir->filename_length] = '\0';
1237             jcf = ALLOC (sizeof (JCF));
1238             JCF_ZERO (jcf);
1239             jcf->read_state  = finput;
1240             jcf->filbuf      = jcf_filbuf_from_stdio;
1241             jcf->java_source = 0;
1242             jcf->classname   = NULL;
1243             jcf->filename    = file_name;
1244             jcf->zipd        = zdir;
1245
1246             if (read_zip_member (jcf, zdir, localToFile) < 0)
1247               fatal_error ("error while reading %s from zip file", file_name);
1248
1249             buffer = ALLOC (zdir->filename_length + 1 +
1250                             (jcf->buffer_end - jcf->buffer));
1251             strcpy (buffer, file_name);
1252             /* This is not a typo: we overwrite the trailing \0 of the
1253                file name; this is just how the data is laid out.  */
1254             memcpy (buffer + zdir->filename_length,
1255                     jcf->buffer, jcf->buffer_end - jcf->buffer);
1256
1257             compile_resource_data (file_name, buffer,
1258                                    jcf->buffer_end - jcf->buffer);
1259             JCF_FINISH (jcf);
1260             FREE (jcf);
1261             FREE (buffer);
1262           }
1263           break;
1264
1265         default:
1266           abort ();
1267         }
1268     }
1269 }
1270
1271 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1272    jcf up for further processing and link it to the created class.  */
1273
1274 static void
1275 process_zip_dir (FILE *finput)
1276 {
1277   int i;
1278   ZipDirectory *zdir;
1279
1280   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1281        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1282     {
1283       char *class_name, *file_name, *class_name_in_zip_dir;
1284       tree class;
1285       JCF  *jcf;
1286
1287       class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1288
1289       /* Here we skip non-class files; we handle them later.  */
1290       if (classify_zip_file (zdir) != 1)
1291         continue;
1292
1293       class_name = compute_class_name (zdir);
1294       file_name  = ALLOC (zdir->filename_length+1);
1295       jcf = ggc_alloc (sizeof (JCF));
1296       JCF_ZERO (jcf);
1297
1298       strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1299       file_name [zdir->filename_length] = '\0';
1300
1301       class = lookup_class (get_identifier (class_name));
1302
1303       jcf->read_state  = finput;
1304       jcf->filbuf      = jcf_filbuf_from_stdio;
1305       jcf->java_source = 0;
1306       jcf->classname   = class_name;
1307       jcf->filename    = file_name;
1308       jcf->zipd        = zdir;
1309
1310       TYPE_JCF (class) = jcf;
1311     }
1312 }
1313
1314 /* Initialization.  */
1315
1316 void
1317 init_jcf_parse (void)
1318 {
1319   init_src_parse ();
1320 }
1321
1322 #include "gt-java-jcf-parse.h"