OSDN Git Service

* c-format.c (check_format_string, get_constant)
[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
46 #ifdef HAVE_LOCALE_H
47 #include <locale.h>
48 #endif
49
50 #ifdef HAVE_LANGINFO_CODESET
51 #include <langinfo.h>
52 #endif
53
54 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
55 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
56 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
57 #define JPOOL_UTF_DATA(JCF, INDEX) \
58   ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
59 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
60   do { \
61     unsigned char save;  unsigned char *text; \
62     JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
63     text = (JCF)->read_ptr; \
64     save = text[LENGTH]; \
65     text[LENGTH] = 0; \
66     (JCF)->cpool.data[INDEX].t = get_identifier (text); \
67     text[LENGTH] = save; \
68     JCF_SKIP (JCF, LENGTH); } while (0)
69
70 #include "jcf.h"
71
72 extern struct obstack temporary_obstack;
73
74 /* Set to nonzero value in order to emit class initialization code
75    before static field references.  */
76 extern int always_initialize_class_p;
77
78 static GTY(()) tree parse_roots[3];
79
80 /* The FIELD_DECL for the current field.  */
81 #define current_field parse_roots[0]
82
83 /* The METHOD_DECL for the current method.  */
84 #define current_method parse_roots[1]
85
86 /* A list of file names.  */
87 #define current_file_list parse_roots[2]
88
89 /* The Java archive that provides main_class;  the main input file. */
90 static GTY(()) struct JCF * main_jcf;
91
92 static struct ZipFile *localToFile;
93
94 /* Declarations of some functions used here.  */
95 static void handle_innerclass_attribute (int count, JCF *);
96 static tree give_name_to_class (JCF *jcf, int index);
97 static char *compute_class_name (struct ZipDirectory *zdir);
98 static int classify_zip_file (struct ZipDirectory *zdir);
99 static void parse_zip_file_entries (void);
100 static void process_zip_dir (FILE *);
101 static void parse_source_file_1 (tree, FILE *);
102 static void parse_source_file_2 (void);
103 static void parse_source_file_3 (void);
104 static void parse_class_file (void);
105 static void handle_deprecated (void);
106 static void set_source_filename (JCF *, int);
107 static void jcf_parse (struct JCF*);
108 static void load_inner_classes (tree);
109
110 /* Handle "Deprecated" attribute.  */
111 static void
112 handle_deprecated (void)
113 {
114   if (current_field != NULL_TREE)
115     FIELD_DEPRECATED (current_field) = 1;
116   else if (current_method != NULL_TREE)
117     METHOD_DEPRECATED (current_method) = 1;
118   else if (current_class != NULL_TREE)
119     CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
120   else
121     {
122       /* Shouldn't happen.  */
123       abort ();
124     }
125 }
126
127 /* Handle "SourceFile" attribute. */
128
129 static void
130 set_source_filename (JCF *jcf, int index)
131 {
132   tree sfname_id = get_name_constant (jcf, index);
133   const char *sfname = IDENTIFIER_POINTER (sfname_id);
134   if (input_filename != NULL)
135     {
136       int old_len = strlen (input_filename);
137       int new_len = IDENTIFIER_LENGTH (sfname_id);
138       /* Use the current input_filename (derived from the class name)
139          if it has a directory prefix, but otherwise matches sfname. */
140       if (old_len > new_len
141           && strcmp (sfname, input_filename + old_len - new_len) == 0
142           && (input_filename[old_len - new_len - 1] == '/'
143               || input_filename[old_len - new_len - 1] == '\\'))
144         return;
145     }
146   input_filename = sfname;
147   DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
148   if (current_class == main_class) main_input_filename = input_filename;
149 }
150
151 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
152
153 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
154 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
155   current_class = give_name_to_class (jcf, THIS); \
156   set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
157
158 #define HANDLE_CLASS_INTERFACE(INDEX) \
159   add_interface (current_class, get_class_constant (jcf, INDEX))
160
161 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
162 { int sig_index = SIGNATURE; \
163   current_field = add_field (current_class, get_name_constant (jcf, NAME), \
164                              parse_signature (jcf, sig_index), ACCESS_FLAGS); \
165  set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
166  if ((ACCESS_FLAGS) & ACC_FINAL) \
167    MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
168 }
169
170 #define HANDLE_END_FIELDS() \
171   (current_field = NULL_TREE)
172
173 #define HANDLE_CONSTANTVALUE(INDEX) \
174 { tree constant;  int index = INDEX; \
175   if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
176     tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
177     constant = build_utf8_ref (name); \
178   } \
179   else \
180     constant = get_constant (jcf, index); \
181   set_constant_value (current_field, constant); }
182
183 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
184  (current_method = add_method (current_class, ACCESS_FLAGS, \
185                                get_name_constant (jcf, NAME), \
186                                get_name_constant (jcf, SIGNATURE)), \
187   DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
188   DECL_LINENUMBERS_OFFSET (current_method) = 0)
189
190 #define HANDLE_END_METHODS() \
191 { current_method = NULL_TREE; }
192
193 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
194 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
195   DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
196   DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
197   DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
198
199 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
200 { int n = (COUNT); \
201   DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
202   JCF_SKIP (jcf, n * 10); }
203
204 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
205 { int n = (COUNT); \
206   DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
207   JCF_SKIP (jcf, n * 4); }
208
209 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
210 { \
211   int n = COUNT; \
212   tree list = DECL_FUNCTION_THROWS (current_method); \
213   while (--n >= 0) \
214     { \
215       tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
216       list = tree_cons (NULL_TREE, thrown_class, list); \
217     } \
218   DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
219 }
220
221 #define HANDLE_DEPRECATED_ATTRIBUTE()  handle_deprecated ()
222
223 /* Link seen inner classes to their outer context and register the
224    inner class to its outer context. They will be later loaded.  */
225 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
226   handle_innerclass_attribute (COUNT, jcf)
227
228 #define HANDLE_SYNTHETIC_ATTRIBUTE()                                    \
229 {                                                                       \
230   /* Irrelevant decls should have been nullified by the END macros.     \
231      We only handle the `Synthetic' attribute on method DECLs.          \
232      DECL_ARTIFICIAL on fields is used for something else (See          \
233      PUSH_FIELD in java-tree.h) */                                      \
234   if (current_method)                                                   \
235     DECL_ARTIFICIAL (current_method) = 1;                               \
236 }
237
238 #define HANDLE_GCJCOMPILED_ATTRIBUTE()          \
239 {                                               \
240   if (current_class == object_type_node)        \
241     jcf->right_zip = 1;                         \
242 }
243
244 #include "jcf-reader.c"
245
246 tree
247 parse_signature (JCF *jcf, int sig_index)
248 {
249   if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
250       || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
251     abort ();
252   else
253     return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
254                                    JPOOL_UTF_LENGTH (jcf, sig_index));
255 }
256
257 tree
258 get_constant (JCF *jcf, int index)
259 {
260   tree value;
261   int tag;
262   if (index <= 0 || index >= JPOOL_SIZE(jcf))
263     goto bad;
264   tag = JPOOL_TAG (jcf, index);
265   if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
266     return jcf->cpool.data[index].t;
267   switch (tag)
268     {
269     case CONSTANT_Integer:
270       {
271         jint num = JPOOL_INT(jcf, index);
272         value = build_int_2 (num, num < 0 ? -1 : 0);
273         TREE_TYPE (value) = int_type_node;
274         break;
275       }
276     case CONSTANT_Long:
277       {
278         unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
279         HOST_WIDE_INT lo, hi;
280         lshift_double (num, 0, 32, 64, &lo, &hi, 0);
281         num = JPOOL_UINT (jcf, index+1);
282         add_double (lo, hi, num, 0, &lo, &hi);
283         value = build_int_2 (lo, hi);
284         TREE_TYPE (value) = long_type_node;
285         force_fit_type (value, 0);
286         break;
287       }
288
289     case CONSTANT_Float:
290       {
291         jint num = JPOOL_INT(jcf, index);
292         long buf = num;
293         REAL_VALUE_TYPE d;
294
295         real_from_target_fmt (&d, &buf, &ieee_single_format);
296         value = build_real (float_type_node, d);
297         break;
298       }
299
300     case CONSTANT_Double:
301       {
302         long buf[2], lo, hi;
303         REAL_VALUE_TYPE d;
304
305         hi = JPOOL_UINT (jcf, index);
306         lo = JPOOL_UINT (jcf, index+1);
307
308         if (FLOAT_WORDS_BIG_ENDIAN)
309           buf[0] = hi, buf[1] = lo;
310         else
311           buf[0] = lo, buf[1] = hi;
312
313         real_from_target_fmt (&d, buf, &ieee_double_format);
314         value = build_real (double_type_node, d);
315         break;
316       }
317
318     case CONSTANT_String:
319       {
320         tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
321         const char *utf8_ptr = IDENTIFIER_POINTER (name);
322         int utf8_len = IDENTIFIER_LENGTH (name);
323         const unsigned char *utf8;
324         int i;
325
326         /* Check for a malformed Utf8 string.  */
327         utf8 = (const unsigned char *) utf8_ptr;
328         i = utf8_len;
329         while (i > 0)
330           {
331             int char_len = UT8_CHAR_LENGTH (*utf8);
332             if (char_len < 0 || char_len > 3 || char_len > i)
333               fatal_error ("bad string constant");
334
335             utf8 += char_len;
336             i -= char_len;
337           }
338
339         /* Allocate a new string value.  */
340         value = build_string (utf8_len, utf8_ptr);
341         TREE_TYPE (value) = build_pointer_type (string_type_node);
342       }
343       break;
344     default:
345       goto bad;
346     }
347   JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
348   jcf->cpool.data[index].t = value;
349   return value;
350  bad:
351   internal_error ("bad value constant type %d, index %d", 
352                   JPOOL_TAG (jcf, index), index);
353 }
354
355 tree
356 get_name_constant (JCF *jcf, int index)
357 {
358   tree name = get_constant (jcf, index);
359
360   if (TREE_CODE (name) != IDENTIFIER_NODE)
361     abort ();
362
363   return name;
364 }
365
366 /* Handle reading innerclass attributes. If a nonzero entry (denoting
367    a non anonymous entry) is found, We augment the inner class list of
368    the outer context with the newly resolved innerclass.  */
369
370 static void
371 handle_innerclass_attribute (int count, JCF *jcf)
372 {
373   int c = (count);
374   while (c--)
375     {
376       /* Read inner_class_info_index. This may be 0 */
377       int icii = JCF_readu2 (jcf);
378       /* Read outer_class_info_index. If the innerclasses attribute
379          entry isn't a member (like an inner class) the value is 0. */
380       int ocii = JCF_readu2 (jcf);
381       /* Read inner_name_index. If the class we're dealing with is
382          an anonymous class, it must be 0. */
383       int ini = JCF_readu2 (jcf);
384       /* Read the access flag. */
385       int acc = JCF_readu2 (jcf);
386       /* If icii is 0, don't try to read the class. */
387       if (icii >= 0)
388         {
389           tree class = get_class_constant (jcf, icii);
390           tree decl = TYPE_NAME (class);
391           /* Skip reading further if ocii is null */
392           if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
393             {
394               tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
395               tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
396               set_class_decl_access_flags (acc, decl);
397               DECL_CONTEXT (decl) = outer;
398               DECL_INNER_CLASS_LIST (outer) =
399                 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
400               CLASS_COMPLETE_P (decl) = 1;
401             }
402         }
403     }
404 }
405
406 static tree
407 give_name_to_class (JCF *jcf, int i)
408 {
409   if (i <= 0 || i >= JPOOL_SIZE (jcf)
410       || JPOOL_TAG (jcf, i) != CONSTANT_Class)
411     abort ();
412   else
413     {
414       tree this_class;
415       int j = JPOOL_USHORT1 (jcf, i);
416       /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
417       tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
418                                             JPOOL_UTF_LENGTH (jcf, j));
419       this_class = lookup_class (class_name);
420       input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
421       input_line = 0;
422       if (main_input_filename == NULL && jcf == main_jcf)
423         main_input_filename = input_filename;
424
425       jcf->cpool.data[i].t = this_class;
426       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
427       return this_class;
428     }
429 }
430
431 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
432
433 tree
434 get_class_constant (JCF *jcf, int i)
435 {
436   tree type;
437   if (i <= 0 || i >= JPOOL_SIZE (jcf)
438       || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
439     abort ();
440
441   if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
442     {
443       int name_index = JPOOL_USHORT1 (jcf, i);
444       /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
445       const char *name = JPOOL_UTF_DATA (jcf, name_index);
446       int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
447
448       if (name[0] == '[')  /* Handle array "classes". */
449           type = TREE_TYPE (parse_signature_string (name, nlength));
450       else
451         { 
452           tree cname = unmangle_classname (name, nlength);
453           type = lookup_class (cname);
454         }
455       jcf->cpool.data[i].t = type;
456       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
457     }
458   else
459     type = jcf->cpool.data[i].t;
460   return type;
461 }
462
463 /* Read a class with the fully qualified-name NAME.
464    Return 1 iff we read the requested file.
465    (It is still possible we failed if the file did not
466    define the class it is supposed to.) */
467
468 int
469 read_class (tree name)
470 {
471   JCF this_jcf, *jcf;
472   tree icv, class = NULL_TREE;
473   tree save_current_class = current_class;
474   const char *save_input_filename = input_filename;
475   JCF *save_current_jcf = current_jcf;
476
477   if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
478     {
479       class = TREE_TYPE (icv);
480       jcf = TYPE_JCF (class);
481     }
482   else
483     jcf = NULL;
484
485   if (jcf == NULL)
486     {
487       this_jcf.zipd = NULL;
488       jcf = &this_jcf;
489       if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
490                       &this_jcf, 1) == 0)
491         return 0;
492     }
493
494   current_jcf = jcf;
495
496   if (current_jcf->java_source)
497     {
498       const char *filename = current_jcf->filename;
499       tree file;
500       FILE *finput;
501       int generate;
502
503       java_parser_context_save_global ();
504       java_push_parser_context ();
505       BUILD_FILENAME_IDENTIFIER_NODE (file, filename);
506       generate = IS_A_COMMAND_LINE_FILENAME_P (file);
507       if (wfl_operator == NULL_TREE)
508         wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
509       EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
510       input_filename = ggc_strdup (filename);
511       current_class = NULL_TREE;
512       current_function_decl = NULL_TREE;
513       if (!HAS_BEEN_ALREADY_PARSED_P (file))
514         {
515           if (!(finput = fopen (input_filename, "r")))
516             fatal_error ("can't reopen %s: %m", input_filename);
517           parse_source_file_1 (file, finput);
518           parse_source_file_2 ();
519           parse_source_file_3 ();
520           if (fclose (finput))
521             fatal_error ("can't close %s: %m", input_filename);
522         }
523       JCF_FINISH (current_jcf);
524       java_pop_parser_context (generate);
525       java_parser_context_restore_global ();
526     }
527   else
528     {
529       if (class == NULL_TREE || ! CLASS_PARSED_P (class))
530         {
531           java_parser_context_save_global ();
532           java_push_parser_context ();
533           current_class = class;
534           input_filename = current_jcf->filename;
535           if (JCF_SEEN_IN_ZIP (current_jcf))
536             read_zip_member(current_jcf,
537                             current_jcf->zipd, current_jcf->zipd->zipf);
538           jcf_parse (current_jcf);
539           /* Parsing might change the class, in which case we have to
540              put it back where we found it.  */
541           if (current_class != class && icv != NULL_TREE)
542             TREE_TYPE (icv) = current_class;
543           class = current_class;
544           java_pop_parser_context (0);
545           java_parser_context_restore_global ();
546         }
547       layout_class (class);
548       load_inner_classes (class);
549     }
550
551   current_class = save_current_class;
552   input_filename = save_input_filename;
553   current_jcf = save_current_jcf;
554   return 1;
555 }
556
557 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
558    called from the parser, otherwise it's a RECORD_TYPE node. If
559    VERBOSE is 1, print error message on failure to load a class. */
560
561 /* Replace calls to load_class by having callers call read_class directly
562    - and then perhaps rename read_class to load_class.  FIXME */
563
564 void
565 load_class (tree class_or_name, int verbose)
566 {
567   tree name, saved;
568   int class_loaded;
569
570   /* class_or_name can be the name of the class we want to load */
571   if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
572     name = class_or_name;
573   /* In some cases, it's a dependency that we process earlier that
574      we though */
575   else if (TREE_CODE (class_or_name) == TREE_LIST)
576     name = TYPE_NAME (TREE_PURPOSE (class_or_name));
577   /* Or it's a type in the making */
578   else
579     name = DECL_NAME (TYPE_NAME (class_or_name));
580
581   saved = name;
582   while (1)
583     {
584       char *separator;
585
586       if ((class_loaded = read_class (name)))
587         break;
588
589       /* We failed loading name. Now consider that we might be looking
590          for a inner class. */
591       if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
592           || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
593         {
594           int c = *separator;
595           *separator = '\0';
596           name = get_identifier (IDENTIFIER_POINTER (name));
597           *separator = c;
598         }
599       /* Otherwise, we failed, we bail. */
600       else
601         break;
602     }
603
604   if (!class_loaded && verbose)
605     error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
606 }
607
608 /* Parse the .class file JCF. */
609
610 static void
611 jcf_parse (JCF* jcf)
612 {
613   int i, code;
614
615   if (jcf_parse_preamble (jcf) != 0)
616     fatal_error ("not a valid Java .class file");
617   code = jcf_parse_constant_pool (jcf);
618   if (code != 0)
619     fatal_error ("error while parsing constant pool");
620   code = verify_constant_pool (jcf);
621   if (code > 0)
622     fatal_error ("error in constant pool entry #%d\n", code);
623
624   jcf_parse_class (jcf);
625   if (main_class == NULL_TREE)
626     main_class = current_class;
627   if (! quiet_flag && TYPE_NAME (current_class))
628     fprintf (stderr, " %s %s",
629              (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class", 
630              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
631   if (CLASS_PARSED_P (current_class))
632     {
633       /* FIXME - where was first time */
634       fatal_error ("reading class %s for the second time from %s",
635                    IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
636                    jcf->filename);
637     }
638   CLASS_PARSED_P (current_class) = 1;
639
640   for (i = 1; i < JPOOL_SIZE(jcf); i++)
641     {
642       switch (JPOOL_TAG (jcf, i))
643         {
644         case CONSTANT_Class:
645           get_class_constant (jcf, i);
646           break;
647         }
648     }
649   
650   code = jcf_parse_fields (jcf);
651   if (code != 0)
652     fatal_error ("error while parsing fields");
653   code = jcf_parse_methods (jcf);
654   if (code != 0)
655     fatal_error ("error while parsing methods");
656   code = jcf_parse_final_attributes (jcf);
657   if (code != 0)
658     fatal_error ("error while parsing final attributes");
659
660   /* The fields of class_type_node are already in correct order. */
661   if (current_class != class_type_node && current_class != object_type_node)
662     TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
663
664   if (current_class == object_type_node)
665     {
666       layout_class_methods (object_type_node);
667       /* If we don't have the right archive, emit a verbose warning.
668          If we're generating bytecode, emit the warning only if
669          -fforce-classes-archive-check was specified. */
670       if (!jcf->right_zip
671           && (!flag_emit_class_files || flag_force_classes_archive_check))
672         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);
673     }
674   else
675     all_class_list = tree_cons (NULL_TREE,
676                                 TYPE_NAME (current_class), all_class_list );
677 }
678
679 /* If we came across inner classes, load them now. */
680 static void
681 load_inner_classes (tree cur_class)
682 {
683   tree current;
684   for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
685        current = TREE_CHAIN (current))
686     {
687       tree name = DECL_NAME (TREE_PURPOSE (current));
688       tree decl = IDENTIFIER_GLOBAL_VALUE (name);
689       if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
690           && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
691         load_class (name, 1);
692     }
693 }
694
695 void
696 init_outgoing_cpool (void)
697 {
698   outgoing_cpool = ggc_alloc_cleared (sizeof (struct CPool));
699 }
700
701 static void
702 parse_class_file (void)
703 {
704   tree method;
705   const char *save_input_filename = input_filename;
706   int save_lineno = input_line;
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_lineno);
802   input_filename = save_input_filename;
803   input_line = save_lineno;
804 }
805
806 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
807
808 static void
809 parse_source_file_1 (tree file, FILE *finput)
810 {
811   int save_error_count = java_error_count;
812   /* Mark the file as parsed */
813   HAS_BEEN_ALREADY_PARSED_P (file) = 1;
814
815   jcf_dependency_add_file (input_filename, 0);
816
817   lang_init_source (1);             /* Error msgs have no method prototypes */
818
819   /* There's no point in trying to find the current encoding unless we
820      are going to do something intelligent with it -- hence the test
821      for iconv.  */
822 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
823   setlocale (LC_CTYPE, "");
824   if (current_encoding == NULL)
825     current_encoding = nl_langinfo (CODESET);
826 #endif 
827   if (current_encoding == NULL || *current_encoding == '\0')
828     current_encoding = DEFAULT_ENCODING;
829
830   /* Initialize the parser */
831   java_init_lex (finput, current_encoding);
832   java_parse_abort_on_error ();
833
834   java_parse ();                    /* Parse and build partial tree nodes. */
835   java_parse_abort_on_error ();
836 }
837
838 /* Process a parsed source file, resolving names etc. */
839
840 static void
841 parse_source_file_2 (void)
842 {
843   int save_error_count = java_error_count;
844   java_complete_class ();           /* Parse unsatisfied class decl. */
845   java_parse_abort_on_error ();
846 }
847
848 static void
849 parse_source_file_3 (void)
850 {
851   int save_error_count = java_error_count;
852   java_check_circular_reference (); /* Check on circular references */
853   java_parse_abort_on_error ();
854   java_fix_constructors ();         /* Fix the constructors */
855   java_parse_abort_on_error ();
856   java_reorder_fields ();           /* Reorder the fields */
857 }
858
859 void
860 add_predefined_file (tree name)
861 {
862   predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
863 }
864
865 int
866 predefined_filename_p (tree node)
867 {
868   tree iter;
869
870   for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
871     {
872       if (TREE_VALUE (iter) == node)
873         return 1;
874     }
875   return 0;
876 }
877
878 void
879 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
880 {
881   int filename_count = 0;
882   char *list, *next;
883   tree node;
884   FILE *finput = NULL;
885
886   if (flag_filelist_file)
887     {
888       int avail = 2000;
889       finput = fopen (input_filename, "r");
890       if (finput == NULL)
891         fatal_error ("can't open %s: %m", input_filename);
892       list = xmalloc(avail);
893       next = list;
894       for (;;)
895         {
896           int count;
897           if (avail < 500)
898             {
899               count = next - list;
900               avail = 2 * (count + avail);
901               list = xrealloc (list, avail);
902               next = list + count;
903               avail = avail - count;
904             }
905           /* Subtract to to guarantee space for final '\0'. */
906           count = fread (next, 1, avail - 1, finput);
907           if (count == 0)
908             {
909               if (! feof (finput))
910                 fatal_error ("error closing %s: %m", input_filename);
911               *next = '\0';
912               break;
913             }
914           avail -= count;
915           next += count;
916         }
917       fclose (finput);
918       finput = NULL;
919     }
920   else
921     list = xstrdup (input_filename);
922
923   do 
924     {
925       for (next = list; ; )
926         {
927           char ch = *next;
928           if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
929               || ch == '&' /* FIXME */)
930             {
931               if (next == list)
932                 {
933                   next++;
934                   list = next;
935                   continue;
936                 }
937               else
938                 {
939                   *next++ = '\0';
940                   break;
941                 }
942             }
943           if (ch == '\0')
944             {
945               next = NULL;
946               break;
947             }
948           next++;
949         }
950
951       if (list[0]) 
952         {
953           char *value;
954           tree id;
955           int twice = 0;
956
957           int len = strlen (list);
958
959           obstack_grow0 (&temporary_obstack, list, len);
960           value = obstack_finish (&temporary_obstack);
961
962           filename_count++;
963
964           /* Exclude file that we see twice on the command line. For
965              all files except {Class,Error,Object,RuntimeException,String,
966              Throwable}.java we can rely on maybe_get_identifier. For
967              these files, we need to do a linear search of
968              current_file_list. This search happens only for these
969              files, presumably only when we're recompiling libgcj. */
970              
971           if ((id = maybe_get_identifier (value)))
972             {
973               if (predefined_filename_p (id))
974                 {
975                   tree c;
976                   for (c = current_file_list; c; c = TREE_CHAIN (c))
977                     if (TREE_VALUE (c) == id)
978                       twice = 1;
979                 }
980               else
981                 twice = 1;
982             }
983
984           if (twice)
985             {
986               const char *saved_input_filename = input_filename;
987               input_filename = value;
988               warning ("source file seen twice on command line and will be compiled only once");
989               input_filename = saved_input_filename;
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       emit_register_classes ();
1123       if (flag_indirect_dispatch)
1124         emit_offset_symbol_table ();
1125     }
1126
1127   write_resource_constructor ();
1128 }
1129
1130 /* Return the name of the class corresponding to the name of the file
1131    in this zip entry.  The result is newly allocated using ALLOC.  */
1132 static char *
1133 compute_class_name (struct ZipDirectory *zdir)
1134 {
1135   char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1136   char *class_name;
1137   int j;
1138
1139   class_name = ALLOC (zdir->filename_length + 1 - 6);
1140   strncpy (class_name, class_name_in_zip_dir, zdir->filename_length - 6);
1141   class_name [zdir->filename_length - 6] = '\0';
1142   for (j = 0; class_name[j]; ++j)
1143     class_name[j] = class_name[j] == '/' ? '.' : class_name[j];
1144   return class_name;
1145 }
1146
1147 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
1148    if it is a property file of some sort.  */
1149 static int
1150 classify_zip_file (struct ZipDirectory *zdir)
1151 {
1152   char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1153
1154   if (zdir->filename_length > 6
1155       && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
1156                    ".class", 6))
1157     return 1;
1158
1159   /* For now we drop the manifest and other information.  Maybe it
1160      would make more sense to compile it in?  */
1161   if (zdir->filename_length > 8
1162       && !strncmp (class_name_in_zip_dir, "META-INF/", 9))
1163     return 0;
1164
1165   /* Drop directory entries.  */
1166   if (zdir->filename_length > 0
1167       && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
1168     return 0;
1169
1170   return 2;
1171 }
1172
1173 /* Process all class entries found in the zip file.  */
1174 static void
1175 parse_zip_file_entries (void)
1176 {
1177   struct ZipDirectory *zdir;
1178   int i;
1179
1180   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1181        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1182     {
1183       tree class;
1184
1185       switch (classify_zip_file (zdir))
1186         {
1187         case 0:
1188           continue;
1189
1190         case 1:
1191           {
1192             char *class_name = compute_class_name (zdir);
1193             class = lookup_class (get_identifier (class_name));
1194             FREE (class_name);
1195             current_jcf = TYPE_JCF (class);
1196             current_class = class;
1197
1198             if (! CLASS_LOADED_P (class))
1199               {
1200                 if (! CLASS_PARSED_P (class))
1201                   {
1202                     read_zip_member (current_jcf, zdir, localToFile);
1203                     jcf_parse (current_jcf);
1204                   }
1205                 layout_class (current_class);
1206                 load_inner_classes (current_class);
1207               }
1208
1209             if (TYPE_SIZE (current_class) != error_mark_node)
1210               {
1211                 input_filename = current_jcf->filename;
1212                 parse_class_file ();
1213                 FREE (current_jcf->buffer); /* No longer necessary */
1214                 /* Note: there is a way to free this buffer right after a
1215                    class seen in a zip file has been parsed. The idea is the
1216                    set its jcf in such a way that buffer will be reallocated
1217                    the time the code for the class will be generated. FIXME. */
1218               }
1219           }
1220           break;
1221
1222         case 2:
1223           {
1224             char *file_name, *class_name_in_zip_dir, *buffer;
1225             JCF *jcf;
1226             file_name = ALLOC (zdir->filename_length + 1);
1227             class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1228             strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1229             file_name[zdir->filename_length] = '\0';
1230             jcf = ALLOC (sizeof (JCF));
1231             JCF_ZERO (jcf);
1232             jcf->read_state  = finput;
1233             jcf->filbuf      = jcf_filbuf_from_stdio;
1234             jcf->java_source = 0;
1235             jcf->classname   = NULL;
1236             jcf->filename    = file_name;
1237             jcf->zipd        = zdir;
1238
1239             if (read_zip_member (jcf, zdir, localToFile) < 0)
1240               fatal_error ("error while reading %s from zip file", file_name);
1241
1242             buffer = ALLOC (zdir->filename_length + 1 +
1243                             (jcf->buffer_end - jcf->buffer));
1244             strcpy (buffer, file_name);
1245             /* This is not a typo: we overwrite the trailing \0 of the
1246                file name; this is just how the data is laid out.  */
1247             memcpy (buffer + zdir->filename_length,
1248                     jcf->buffer, jcf->buffer_end - jcf->buffer);
1249
1250             compile_resource_data (file_name, buffer,
1251                                    jcf->buffer_end - jcf->buffer);
1252             JCF_FINISH (jcf);
1253             FREE (jcf);
1254             FREE (buffer);
1255           }
1256           break;
1257
1258         default:
1259           abort ();
1260         }
1261     }
1262 }
1263
1264 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1265    jcf up for further processing and link it to the created class.  */
1266
1267 static void
1268 process_zip_dir (FILE *finput)
1269 {
1270   int i;
1271   ZipDirectory *zdir;
1272
1273   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1274        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1275     {
1276       char *class_name, *file_name, *class_name_in_zip_dir;
1277       tree class;
1278       JCF  *jcf;
1279
1280       class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1281
1282       /* Here we skip non-class files; we handle them later.  */
1283       if (classify_zip_file (zdir) != 1)
1284         continue;
1285
1286       class_name = compute_class_name (zdir);
1287       file_name  = ALLOC (zdir->filename_length+1);
1288       jcf = ggc_alloc (sizeof (JCF));
1289       JCF_ZERO (jcf);
1290
1291       strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1292       file_name [zdir->filename_length] = '\0';
1293
1294       class = lookup_class (get_identifier (class_name));
1295
1296       jcf->read_state  = finput;
1297       jcf->filbuf      = jcf_filbuf_from_stdio;
1298       jcf->java_source = 0;
1299       jcf->classname   = class_name;
1300       jcf->filename    = file_name;
1301       jcf->zipd        = zdir;
1302
1303       TYPE_JCF (class) = jcf;
1304     }
1305 }
1306
1307 /* Initialization.  */
1308
1309 void
1310 init_jcf_parse (void)
1311 {
1312   init_src_parse ();
1313 }
1314
1315 #include "gt-java-jcf-parse.h"