OSDN Git Service

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