OSDN Git Service

91e8ae072701bfe20e115da4e090da0c0ab8097d
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-parse.c
1 /* Parser for Java(TM) .class files.
2    Copyright (C) 1996, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
24
25 /* Written by Per Bothner <bothner@cygnus.com> */
26
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "obstack.h"
31 #include "flags.h"
32 #include "java-except.h"
33 #include "input.h"
34 #include "java-tree.h"
35 #include "toplev.h"
36 #include "parse.h"
37 #include "ggc.h"
38
39 #ifdef HAVE_LOCALE_H
40 #include <locale.h>
41 #endif
42
43 #ifdef HAVE_NL_LANGINFO
44 #include <langinfo.h>
45 #endif
46
47 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
48 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
49 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
50 #define JPOOL_UTF_DATA(JCF, INDEX) \
51   ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
52 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
53   do { \
54     unsigned char save;  unsigned char *text; \
55     JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
56     text = (JCF)->read_ptr; \
57     save = text[LENGTH]; \
58     text[LENGTH] = 0; \
59     (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
60     text[LENGTH] = save; \
61     JCF_SKIP (JCF, LENGTH); } while (0)
62
63 #include "jcf.h"
64
65 extern struct obstack *saveable_obstack;
66 extern struct obstack temporary_obstack;
67 extern struct obstack permanent_obstack;
68
69 /* Set to non-zero value in order to emit class initilization code
70    before static field references.  */
71 extern int always_initialize_class_p;
72
73 /* The FIELD_DECL for the current field.  */
74 static tree current_field = NULL_TREE;
75
76 /* The METHOD_DECL for the current method.  */
77 static tree current_method = NULL_TREE;
78
79 /* A list of file names.  */
80 static tree current_file_list = NULL_TREE;
81
82 /* The Java .class file that provides main_class;  the main input file. */
83 static struct JCF main_jcf[1];
84
85 /* Declarations of some functions used here.  */
86 static tree give_name_to_class PARAMS ((JCF *jcf, int index));
87 static void parse_zip_file_entries PARAMS ((void));
88 static void process_zip_dir PARAMS ((FILE *));
89 static void parse_source_file PARAMS ((tree, FILE *));
90 static void jcf_parse_source PARAMS ((void));
91 static int jcf_figure_file_type PARAMS ((JCF *));
92 static int find_in_current_zip PARAMS ((const char *, struct JCF **));
93 static void parse_class_file PARAMS ((void));
94 static void set_source_filename PARAMS ((JCF *, int));
95 static int predefined_filename_p PARAMS ((tree));
96
97 /* Handle "SourceFile" attribute. */
98
99 static void
100 set_source_filename (jcf, index)
101      JCF *jcf;
102      int index;
103 {
104   tree sfname_id = get_name_constant (jcf, index);
105   const char *sfname = IDENTIFIER_POINTER (sfname_id);
106   if (input_filename != NULL)
107     {
108       int old_len = strlen (input_filename);
109       int new_len = IDENTIFIER_LENGTH (sfname_id);
110       /* Use the current input_filename (derived from the class name)
111          if it has a directory prefix, but otherwise matches sfname. */
112       if (old_len > new_len
113           && strcmp (sfname, input_filename + old_len - new_len) == 0
114           && (input_filename[old_len - new_len - 1] == '/'
115               || input_filename[old_len - new_len - 1] == '\\'))
116         return;
117     }
118   input_filename = sfname;
119   DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
120   if (current_class == main_class) main_input_filename = input_filename;
121 }
122
123 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
124
125 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
126 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
127   current_class = give_name_to_class (jcf, THIS); \
128   set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
129
130 #define HANDLE_CLASS_INTERFACE(INDEX) \
131   add_interface (current_class, get_class_constant (jcf, INDEX))
132
133 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
134 { int sig_index = SIGNATURE; \
135   current_field = add_field (current_class, get_name_constant (jcf, NAME), \
136                              parse_signature (jcf, sig_index), ACCESS_FLAGS); \
137  set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); }
138
139 #define HANDLE_END_FIELDS() \
140   (current_field = NULL_TREE)
141
142 #define HANDLE_CONSTANTVALUE(INDEX) \
143 { tree constant;  int index = INDEX; \
144   if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
145     tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
146     constant = build_utf8_ref (name); \
147   } \
148   else \
149     constant = get_constant (jcf, index); \
150   set_constant_value (current_field, constant); }
151
152 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
153  (current_method = add_method (current_class, ACCESS_FLAGS, \
154                                get_name_constant (jcf, NAME), \
155                                get_name_constant (jcf, SIGNATURE)), \
156   DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
157   DECL_LINENUMBERS_OFFSET (current_method) = 0)
158
159 #define HANDLE_END_METHODS() \
160 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
161   if (handle_type != current_class) layout_type (handle_type); }
162
163 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
164 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
165   DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
166   DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
167   DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
168
169 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
170 { int n = (COUNT); \
171   DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
172   JCF_SKIP (jcf, n * 10); }
173
174 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
175 { int n = (COUNT); \
176   DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
177   JCF_SKIP (jcf, n * 4); }
178
179 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
180 { \
181   int n = COUNT; \
182   tree list = DECL_FUNCTION_THROWS (current_method); \
183   while (--n >= 0) \
184     { \
185       tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
186       list = tree_cons (NULL_TREE, thrown_class, list); \
187     } \
188   DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
189 }
190
191 /* Link seen inner classes to their outer context and register the
192    inner class to its outer context. They will be later loaded.  */
193 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT)                              \
194 {                                                                         \
195   int c = (count);                                                        \
196   while (c--)                                                             \
197     {                                                                     \
198       tree class = get_class_constant (jcf, JCF_readu2 (jcf));            \
199       tree decl = TYPE_NAME (class);                                      \
200       if (DECL_P (decl) && !CLASS_COMPLETE_P (decl))                      \
201         {                                                                 \
202           tree outer = TYPE_NAME (get_class_constant (jcf,                \
203                                                       JCF_readu2 (jcf))); \
204           tree alias = get_name_constant (jcf, JCF_readu2 (jcf));         \
205           JCF_SKIP (jcf, 2);                                              \
206           DECL_CONTEXT (decl) = outer;                                    \
207           DECL_INNER_CLASS_LIST (outer) =                                 \
208             tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));       \
209           CLASS_COMPLETE_P (decl) = 1;                                    \
210         }                                                                 \
211       else                                                                \
212         JCF_SKIP (jcf, 6);                                                \
213     }                                                                     \
214 }
215
216 #include "jcf-reader.c"
217
218 static int yydebug;
219
220 tree
221 parse_signature (jcf, sig_index)
222      JCF *jcf;
223      int sig_index;
224 {
225   if (sig_index <= 0 || sig_index >= JPOOL_SIZE(jcf)
226       || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
227     fatal ("invalid field/method signature");
228   else
229     {
230       return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
231                                      JPOOL_UTF_LENGTH (jcf, sig_index));
232     }
233 }
234
235 void
236 init_lex ()
237 {
238   /* Make identifier nodes long enough for the language-specific slots.  */
239   set_identifier_size (sizeof (struct lang_identifier));
240 }
241
242 void
243 set_yydebug (value)
244      int value;
245 {
246   yydebug = value;
247 }
248
249 tree
250 get_constant (jcf, index)
251   JCF *jcf;
252   int index;
253 {
254   tree value;
255   int tag;
256   if (index <= 0 || index >= JPOOL_SIZE(jcf))
257     goto bad;
258   tag = JPOOL_TAG (jcf, index);
259   if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
260     return (tree) jcf->cpool.data[index];
261   switch (tag)
262     {
263     case CONSTANT_Integer:
264       {
265         jint num = JPOOL_INT(jcf, index);
266         value = build_int_2 (num, num < 0 ? -1 : 0);
267         TREE_TYPE (value) = int_type_node;
268         break;
269       }
270     case CONSTANT_Long:
271       {
272         jint num = JPOOL_INT (jcf, index);
273         HOST_WIDE_INT lo, hi;
274         lshift_double (num, 0, 32, 64, &lo, &hi, 0);
275         num = JPOOL_INT (jcf, index+1) & 0xffffffff;
276         add_double (lo, hi, num, 0, &lo, &hi);
277         value = build_int_2 (lo, hi);
278         TREE_TYPE (value) = long_type_node;
279         force_fit_type (value, 0);
280         break;
281       }
282 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
283     case CONSTANT_Float:
284       {
285         jint num = JPOOL_INT(jcf, index);
286         REAL_VALUE_TYPE d;
287 #ifdef REAL_ARITHMETIC
288         d = REAL_VALUE_FROM_TARGET_SINGLE (num);
289 #else
290         union { float f;  jint i; } u;
291         u.i = num;
292         d = u.f;
293 #endif
294         value = build_real (float_type_node, d);
295         break;
296       }
297     case CONSTANT_Double:
298       {
299         HOST_WIDE_INT num[2];
300         REAL_VALUE_TYPE d;
301         HOST_WIDE_INT lo, hi;
302         num[0] = JPOOL_INT (jcf, index);
303         lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
304         num[0] = JPOOL_INT (jcf, index+1);
305         add_double (lo, hi, num[0], 0, &lo, &hi);
306         if (FLOAT_WORDS_BIG_ENDIAN)
307           {
308             num[0] = hi;
309             num[1] = lo;
310           }
311         else
312           {
313             num[0] = lo;
314             num[1] = hi;
315           }
316 #ifdef REAL_ARITHMETIC
317         d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
318 #else
319         {
320           union { double d;  jint i[2]; } u;
321           u.i[0] = (jint) num[0];
322           u.i[1] = (jint) num[1];
323           d = u.d;
324         }
325 #endif
326         value = build_real (double_type_node, d);
327         break;
328       }
329 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
330     case CONSTANT_String:
331       {
332         tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
333         const char *utf8_ptr = IDENTIFIER_POINTER (name);
334         int utf8_len = IDENTIFIER_LENGTH (name);
335         unsigned char *str_ptr;
336         unsigned char *str;
337         const unsigned char *utf8;
338         int i, str_len;
339
340         /* Count the number of Unicode characters in the string,
341            while checking for a malformed Utf8 string. */
342         utf8 = (const unsigned char *) utf8_ptr;
343         i = utf8_len;
344         str_len = 0;
345         while (i > 0)
346           {
347             int char_len = UT8_CHAR_LENGTH (*utf8);
348             if (char_len < 0 || char_len > 3 || char_len > i)
349               fatal ("bad string constant");
350             utf8 += char_len;
351             i -= char_len;
352             str_len++;
353           }
354
355         /* Allocate a scratch buffer, convert the string to UCS2, and copy it
356            into the new space.  */
357         str_ptr = (unsigned char *) alloca (2 * str_len);
358         str = str_ptr;
359         utf8 = (const unsigned char *)utf8_ptr;
360
361         for (i = 0; i < str_len; i++)
362           {
363             int char_value;
364             int char_len = UT8_CHAR_LENGTH (*utf8);
365             switch (char_len)
366               {
367               case 1:
368                 char_value = *utf8++;
369                 break;
370               case 2:
371                 char_value = *utf8++ & 0x1F;
372                 char_value = (char_value << 6) | (*utf8++ & 0x3F);
373                 break;
374               case 3:
375                 char_value = *utf8++ & 0x0F;
376                 char_value = (char_value << 6) | (*utf8++ & 0x3F);
377                 char_value = (char_value << 6) | (*utf8++ & 0x3F);
378                 break;
379               default:
380                 goto bad;
381               }
382             if (BYTES_BIG_ENDIAN)
383               {
384                 *str++ = char_value >> 8;
385                 *str++ = char_value & 0xFF;
386               }
387             else
388               {
389                 *str++ = char_value & 0xFF;
390                 *str++ = char_value >> 8;
391               }
392           }
393         value = build_string (str - str_ptr, str_ptr);
394         TREE_TYPE (value) = build_pointer_type (string_type_node);
395       }
396       break;
397     default:
398       goto bad;
399     }
400   JPOOL_TAG(jcf, index) = tag | CONSTANT_ResolvedFlag;
401   jcf->cpool.data [index] = (jword) value;
402   return value;
403  bad:
404   fatal ("bad value constant type %d, index %d", 
405          JPOOL_TAG( jcf, index ), index);
406 }
407
408 tree
409 get_name_constant (jcf, index)
410   JCF *jcf;
411   int index;
412 {
413   tree name = get_constant (jcf, index);
414   if (TREE_CODE (name) != IDENTIFIER_NODE)
415     fatal ("bad nameandtype index %d", index);
416   return name;
417 }
418
419 static tree
420 give_name_to_class (jcf, i)
421      JCF *jcf;
422      int i;
423 {
424   if (i <= 0 || i >= JPOOL_SIZE(jcf)
425       || JPOOL_TAG (jcf, i) != CONSTANT_Class)
426     fatal ("bad class index %d", i);
427   else
428     {
429       tree this_class;
430       int j = JPOOL_USHORT1 (jcf, i);
431       /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
432       tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
433                                             JPOOL_UTF_LENGTH (jcf, j));
434       this_class = lookup_class (class_name);
435       input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
436       lineno = 0;
437       if (main_input_filename == NULL && jcf == main_jcf)
438         main_input_filename = input_filename;
439
440       jcf->cpool.data[i] = (jword) this_class;
441       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
442       return this_class;
443     }
444 }
445
446 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
447
448 tree
449 get_class_constant (JCF *jcf , int i)
450 {
451   tree type;
452   if (i <= 0 || i >= JPOOL_SIZE(jcf)
453       || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
454     fatal ("bad class index %d", i);
455
456   if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
457     {
458       int name_index = JPOOL_USHORT1 (jcf, i);
459       /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
460       const char *name = JPOOL_UTF_DATA (jcf, name_index);
461       int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
462       if (name[0] == '[')  /* Handle array "classes". */
463           type = TREE_TYPE (parse_signature_string (name, nlength));
464       else
465         { 
466           tree cname = unmangle_classname (name, nlength);
467           type = lookup_class (cname);
468         }
469       jcf->cpool.data[i] = (jword) type;
470       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
471     }
472   else
473     type = (tree) jcf->cpool.data[i];
474   return type;
475 }
476
477 /* Read a class with the fully qualified-name NAME.
478    Return 1 iff we read the requested file.
479    (It is still possible we failed if the file did not
480    define the class it is supposed to.) */
481
482 int
483 read_class (name)
484      tree name;
485 {
486   JCF this_jcf, *jcf;
487   tree save_current_class = current_class;
488   const char *save_input_filename = input_filename;
489   JCF *save_current_jcf = current_jcf;
490   long saved_pos = 0;
491   if (current_jcf->read_state)
492     saved_pos = ftell (current_jcf->read_state);
493
494   /* Search in current zip first.  */
495   if (find_in_current_zip (IDENTIFIER_POINTER (name), &jcf) == 0)
496     {
497       if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
498                       &this_jcf, 1) == 0)
499         return 0;
500       else
501         {
502           this_jcf.seen_in_zip = 0;
503           current_jcf = &this_jcf;
504         }
505     }
506   else
507     current_jcf = jcf;
508
509   if (current_jcf->java_source)
510     jcf_parse_source ();
511   else {
512     java_parser_context_save_global ();
513     java_push_parser_context ();
514     input_filename = current_jcf->filename;
515     jcf_parse (current_jcf);
516     java_pop_parser_context (0);
517     java_parser_context_restore_global ();
518   }
519
520   if (!current_jcf->seen_in_zip)
521     JCF_FINISH (current_jcf);
522
523   current_class = save_current_class;
524   input_filename = save_input_filename;
525   current_jcf = save_current_jcf;
526   if (current_jcf->read_state)
527     fseek (current_jcf->read_state, saved_pos, SEEK_SET);
528   return 1;
529 }
530
531 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
532    called from the parser, otherwise it's a RECORD_TYPE node. If
533    VERBOSE is 1, print error message on failure to load a class. */
534
535 /* Replace calls to load_class by having callers call read_class directly
536    - and then perhaps rename read_class to load_class.  FIXME */
537
538 void
539 load_class (class_or_name, verbose)
540      tree class_or_name;
541      int verbose;
542 {
543   tree name;
544
545   /* class_or_name can be the name of the class we want to load */
546   if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
547     name = class_or_name;
548   /* In some cases, it's a dependency that we process earlier that
549      we though */
550   else if (TREE_CODE (class_or_name) == TREE_LIST)
551     name = TYPE_NAME (TREE_PURPOSE (class_or_name));
552   /* Or it's a type in the making */
553   else
554     name = DECL_NAME (TYPE_NAME (class_or_name));
555
556   if (read_class (name) == 0 && verbose)
557     fatal ("Cannot find file for class %s.", IDENTIFIER_POINTER (name));
558 }
559
560 /* Parse a source file when JCF refers to a source file.  */
561
562 static void
563 jcf_parse_source ()
564 {
565   tree file;
566   FILE *finput;
567
568   java_parser_context_save_global ();
569   java_push_parser_context ();
570   BUILD_FILENAME_IDENTIFIER_NODE (file, current_jcf->filename);
571   EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
572   input_filename = ggc_strdup (current_jcf->filename);
573   current_class = NULL_TREE;
574   current_function_decl = NULL_TREE;
575   if (!HAS_BEEN_ALREADY_PARSED_P (file))
576     {
577       if (!(finput = fopen (input_filename, "r")))
578         fatal ("input file `%s' just disappeared - jcf_parse_source",
579                input_filename);
580       parse_source_file (file, finput);
581       if (fclose (finput))
582         fatal ("can't close input file `%s' stream - jcf_parse_source",
583                input_filename);
584     }
585   java_pop_parser_context (IS_A_COMMAND_LINE_FILENAME_P (file));
586   java_parser_context_restore_global ();
587 }
588
589 /* Parse the .class file JCF. */
590
591 void
592 jcf_parse (jcf)
593      JCF* jcf;
594 {
595   int i, code;
596   tree current;
597
598   if (jcf_parse_preamble (jcf) != 0)
599     fatal ("Not a valid Java .class file.\n");
600   code = jcf_parse_constant_pool (jcf);
601   if (code != 0)
602     fatal ("error while parsing constant pool");
603   code = verify_constant_pool (jcf);
604   if (code > 0)
605     fatal ("error in constant pool entry #%d\n", code);
606
607   jcf_parse_class (jcf);
608   if (main_class == NULL_TREE)
609     main_class = current_class;
610   if (! quiet_flag && TYPE_NAME (current_class))
611     fprintf (stderr, " %s %s",
612              (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class", 
613              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
614   if (CLASS_LOADED_P (current_class))
615     return;
616   CLASS_LOADED_P (current_class) = 1;
617
618   for (i = 1; i < JPOOL_SIZE(jcf); i++)
619     {
620       switch (JPOOL_TAG (jcf, i))
621         {
622         case CONSTANT_Class:
623           get_class_constant (jcf, i);
624           break;
625         }
626     }
627   
628   code = jcf_parse_fields (jcf);
629   if (code != 0)
630     fatal ("error while parsing fields");
631   code = jcf_parse_methods (jcf);
632   if (code != 0)
633     fatal ("error while parsing methods");
634   code = jcf_parse_final_attributes (jcf);
635   if (code != 0)
636     fatal ("error while parsing final attributes");
637
638   /* The fields of class_type_node are already in correct order. */
639   if (current_class != class_type_node && current_class != object_type_node)
640     TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
641
642   layout_class (current_class);
643   if (current_class == object_type_node)
644     layout_class_methods (object_type_node);
645   else
646     all_class_list = tree_cons (NULL_TREE, 
647                                 TYPE_NAME (current_class), all_class_list );
648
649   /* And if we came accross inner classes, load them now. */
650   for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (current_class)); current;
651        current = TREE_CHAIN (current))
652     load_class (DECL_NAME (TREE_PURPOSE (current)), 1);
653 }
654
655 void
656 init_outgoing_cpool ()
657 {
658   current_constant_pool_data_ref = NULL_TREE;
659   outgoing_cpool = (struct CPool *)xmalloc (sizeof (struct CPool));
660   memset (outgoing_cpool, 0, sizeof (struct CPool));
661 }
662
663 static void
664 parse_class_file ()
665 {
666   tree method;
667   const char *save_input_filename = input_filename;
668   int save_lineno = lineno;
669
670   java_layout_seen_class_methods ();
671
672   input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
673   lineno = 0;
674   debug_start_source_file (input_filename);
675   init_outgoing_cpool ();
676
677   /* Currently we always have to emit calls to _Jv_InitClass when
678      compiling from class files.  */
679   always_initialize_class_p = 1;
680
681   for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
682         method != NULL_TREE; method = TREE_CHAIN (method))
683     {
684       JCF *jcf = current_jcf;
685
686       if (METHOD_ABSTRACT (method))
687         continue;
688
689       if (METHOD_NATIVE (method))
690         {
691           if (! flag_jni)
692             continue;
693           DECL_MAX_LOCALS (method)
694             = list_length (TYPE_ARG_TYPES (TREE_TYPE (method)));
695           start_java_method (method);
696           give_name_to_locals (jcf);
697           expand_expr_stmt (build_jni_stub (method));
698           end_java_method ();
699           continue;
700         }
701
702       if (DECL_CODE_OFFSET (method) == 0)
703         {
704           error ("missing Code attribute");
705           continue;
706         }
707
708       lineno = 0;
709       if (DECL_LINENUMBERS_OFFSET (method))
710         {
711           register int i;
712           register unsigned char *ptr;
713           JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
714           linenumber_count = i = JCF_readu2 (jcf);
715           linenumber_table = ptr = jcf->read_ptr;
716
717           for (ptr += 2; --i >= 0; ptr += 4)
718             {
719               int line = GET_u2 (ptr);
720               /* Set initial lineno lineno to smallest linenumber.
721                * Needs to be set before init_function_start. */
722               if (lineno == 0 || line < lineno)
723                 lineno = line;
724             }  
725         }
726       else
727         {
728           linenumber_table = NULL;
729           linenumber_count = 0;
730         }
731
732       start_java_method (method);
733
734       note_instructions (jcf, method);
735
736       give_name_to_locals (jcf);
737
738       /* Actually generate code. */
739       expand_byte_code (jcf, method);
740
741       end_java_method ();
742     }
743
744   if (flag_emit_class_files)
745     write_classfile (current_class);
746
747   finish_class ();
748
749   debug_end_source_file (save_lineno);
750   input_filename = save_input_filename;
751   lineno = save_lineno;
752 }
753
754 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
755
756 static void
757 parse_source_file (file, finput)
758      tree file;
759      FILE *finput;
760 {
761   int save_error_count = java_error_count;
762   /* Mark the file as parsed */
763   HAS_BEEN_ALREADY_PARSED_P (file) = 1;
764
765   jcf_dependency_add_file (input_filename, 0);
766
767   lang_init_source (1);             /* Error msgs have no method prototypes */
768
769   /* There's no point in trying to find the current encoding unless we
770      are going to do something intelligent with it -- hence the test
771      for iconv.  */
772 #ifdef HAVE_ICONV
773 #ifdef HAVE_NL_LANGINFO
774   setlocale (LC_CTYPE, "");
775   if (current_encoding == NULL)
776     current_encoding = nl_langinfo (CODESET);
777 #endif /* HAVE_NL_LANGINFO */
778 #endif /* HAVE_ICONV */
779   if (current_encoding == NULL || *current_encoding == '\0')
780     current_encoding = DEFAULT_ENCODING;
781
782   /* Initialize the parser */
783   java_init_lex (finput, current_encoding);
784   java_parse_abort_on_error ();
785
786   java_parse ();                    /* Parse and build partial tree nodes. */
787   java_parse_abort_on_error ();
788   java_complete_class ();           /* Parse unsatisfied class decl. */
789   java_parse_abort_on_error ();
790   java_check_circular_reference (); /* Check on circular references */
791   java_parse_abort_on_error ();
792   java_fix_constructors ();         /* Fix the constructors */
793   java_parse_abort_on_error ();
794   java_reorder_fields ();           /* Reorder the fields */
795 }
796
797 static int
798 predefined_filename_p (node)
799      tree node;
800 {
801   int i;
802   for (i = 0; i < PREDEF_FILENAMES_SIZE; i++)
803     if (predef_filenames [i] == node)
804       return 1;
805   return 0;
806 }
807
808 int
809 yyparse ()
810 {
811   int several_files = 0;
812   char *list = xstrdup (input_filename), *next;
813   tree node;
814   FILE *finput;
815
816   do 
817     {
818       next = strchr (list, '&');
819       if (next)
820         {
821           *next++ = '\0';
822           several_files = 1;
823         }
824
825       if (list[0]) 
826         {
827           char *value;
828           tree id;
829           int twice = 0;
830
831           int len = strlen (list);
832
833           if (*list != '/' && several_files)
834             obstack_grow (&temporary_obstack, "./", 2);
835
836           obstack_grow0 (&temporary_obstack, list, len);
837           value = obstack_finish (&temporary_obstack);
838
839           /* Exclude file that we see twice on the command line. For
840              all files except {Class,Error,Object,RuntimeException,String,
841              Throwable}.java we can rely on maybe_get_identifier. For
842              these files, we need to do a linear search of
843              current_file_list. This search happens only for these
844              files, presumably only when we're recompiling libgcj. */
845              
846           if ((id = maybe_get_identifier (value)))
847             {
848               if (predefined_filename_p (id))
849                 {
850                   tree c;
851                   for (c = current_file_list; c; c = TREE_CHAIN (c))
852                     if (TREE_VALUE (c) == id)
853                       twice = 1;
854                 }
855               else
856                 twice = 1;
857             }
858
859           if (twice)
860             {
861               const char *saved_input_filename = input_filename;
862               input_filename = value;
863               warning ("source file seen twice on command line and will be compiled only once.");
864               input_filename = saved_input_filename;
865             }
866           else
867             {
868               BUILD_FILENAME_IDENTIFIER_NODE (node, value);
869               IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
870               current_file_list = tree_cons (NULL_TREE, node, 
871                                              current_file_list);
872             }
873         }
874       list = next;
875     }
876   while (next);
877
878   current_jcf = main_jcf;
879   current_file_list = nreverse (current_file_list);
880   for (node = current_file_list; node; node = TREE_CHAIN (node))
881     {
882       tree name = TREE_VALUE (node);
883
884       /* Skip already parsed files */
885       if (HAS_BEEN_ALREADY_PARSED_P (name))
886         continue;
887       
888       /* Close previous descriptor, if any */
889       if (main_jcf->read_state && fclose (main_jcf->read_state))
890         fatal ("failed to close input file `%s' - yyparse",
891                (main_jcf->filename ? main_jcf->filename : "<unknown>"));
892       
893       /* Set jcf up and open a new file */
894       JCF_ZERO (main_jcf);
895       main_jcf->read_state = fopen (IDENTIFIER_POINTER (name), "rb");
896       if (main_jcf->read_state == NULL)
897         pfatal_with_name (IDENTIFIER_POINTER (name));
898       
899       /* Set new input_filename and finput */
900       finput = main_jcf->read_state;
901 #ifdef IO_BUFFER_SIZE
902       setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
903                _IOFBF, IO_BUFFER_SIZE);
904 #endif
905       input_filename = IDENTIFIER_POINTER (name);
906       main_jcf->filbuf = jcf_filbuf_from_stdio;
907
908       switch (jcf_figure_file_type (current_jcf))
909         {
910         case JCF_ZIP:
911           parse_zip_file_entries ();
912           break;
913         case JCF_CLASS:
914           jcf_parse (current_jcf);
915           parse_class_file ();
916           break;
917         case JCF_SOURCE:
918           java_push_parser_context ();
919           java_parser_context_save_global ();
920           parse_source_file (name, finput);
921           java_parser_context_restore_global ();
922           java_pop_parser_context (1);
923           break;
924         }
925     }
926
927   java_expand_classes ();
928   if (!java_report_errors () && !flag_syntax_only)
929     emit_register_classes ();
930   return 0;
931 }
932
933 static struct ZipFileCache *localToFile;
934
935 /* Process all class entries found in the zip file.  */
936 static void
937 parse_zip_file_entries (void)
938 {
939   struct ZipDirectory *zdir;
940   int i;
941
942   for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
943        i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
944     {
945       tree class;
946       
947       /* We don't need to consider those files.  */
948       if (!zdir->size || !zdir->filename_offset)
949         continue;
950
951       class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
952       current_jcf = TYPE_JCF (class);
953       current_class = class;
954
955       if ( !CLASS_LOADED_P (class))
956         {
957           fseek (current_jcf->read_state, current_jcf->zip_offset, SEEK_SET);
958           jcf_parse (current_jcf);
959         }
960
961       if (TYPE_SIZE (current_class) != error_mark_node)
962         {
963           input_filename = current_jcf->filename;
964           parse_class_file ();
965           FREE (current_jcf->buffer); /* No longer necessary */
966           /* Note: there is a way to free this buffer right after a
967              class seen in a zip file has been parsed. The idea is the
968              set its jcf in such a way that buffer will be reallocated
969              the time the code for the class will be generated. FIXME. */
970         }
971     }
972 }
973
974 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
975    jcf up for further processing and link it to the created class.  */
976
977 static void
978 process_zip_dir (FILE *finput)
979 {
980   int i;
981   ZipDirectory *zdir;
982
983   for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
984        i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
985     {
986       char *class_name, *file_name, *class_name_in_zip_dir;
987       tree class;
988       JCF  *jcf;
989       int   j;
990
991       class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
992
993       /* We choose to not to process entries with a zero size or entries
994          not bearing the .class extention.  */
995       if (!zdir->size || !zdir->filename_offset ||
996           strncmp (&class_name_in_zip_dir[zdir->filename_length-6], 
997                    ".class", 6))
998         {
999           /* So it will be skipped in parse_zip_file_entries  */
1000           zdir->size = 0;  
1001           continue;
1002         }
1003
1004       class_name = ALLOC (zdir->filename_length+1-6);
1005       file_name  = ALLOC (zdir->filename_length+1);
1006       jcf = ALLOC (sizeof (JCF));
1007       JCF_ZERO (jcf);
1008
1009       strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1010       class_name [zdir->filename_length-6] = '\0';
1011       strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1012       file_name [zdir->filename_length] = '\0';
1013
1014       for (j=0; class_name[j]; j++)
1015         class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1016
1017       /* Yes, we write back the true class name into the zip directory.  */
1018       strcpy (class_name_in_zip_dir, class_name);
1019       zdir->filename_length = j;
1020       class = lookup_class (get_identifier (class_name));
1021
1022       jcf->read_state  = finput;
1023       jcf->filbuf      = jcf_filbuf_from_stdio;
1024       jcf->seen_in_zip = 1;
1025       jcf->java_source = 0;
1026       jcf->zip_offset  = zdir->filestart;
1027       jcf->classname   = class_name;
1028       jcf->filename    = file_name;
1029
1030       TYPE_JCF (class) = jcf;
1031     }
1032 }
1033
1034 /* Lookup class NAME and figure whether is a class already found in the current
1035    zip file.  */
1036 static int
1037 DEFUN(find_in_current_zip, (name, length, jcf),
1038       const char *name AND JCF **jcf)
1039 {
1040   JCF *local_jcf;
1041   tree class_name = maybe_get_identifier (name), class, icv;
1042
1043   if (!class_name)
1044     return 0;
1045
1046   if (!(icv = IDENTIFIER_CLASS_VALUE (class_name)))
1047     return 0;
1048
1049   class = TREE_TYPE (icv);
1050
1051   /* Doesn't have jcf specific info ? It's not ours */
1052   if (!TYPE_JCF (class))
1053     return 0;
1054
1055   *jcf = local_jcf = TYPE_JCF (class);
1056   fseek (local_jcf->read_state, local_jcf->zip_offset, SEEK_SET);
1057   return 1;
1058 }
1059
1060 /* Figure what kind of file we're dealing with */
1061 static int
1062 DEFUN(jcf_figure_file_type, (jcf),
1063       JCF *jcf)
1064 {
1065   unsigned char magic_string[4];
1066   uint32 magic;
1067
1068   if (fread (magic_string, 1, 4, jcf->read_state) != 4)
1069     jcf_unexpected_eof (jcf, 4);
1070
1071   fseek (jcf->read_state, 0L, SEEK_SET);
1072   magic = GET_u4 (magic_string);
1073
1074   if (magic == 0xcafebabe)
1075     return JCF_CLASS;
1076
1077   /* FIXME: is it a system file?  */
1078   if (magic ==  (JCF_u4)ZIPMAGIC
1079       && !open_in_zip (jcf, input_filename, NULL, 0))
1080     {
1081       localToFile = ALLOC (sizeof (struct ZipFileCache));
1082       memcpy (localToFile, SeenZipFiles, sizeof (struct ZipFileCache));
1083       /* Register all the class defined there.  */
1084       process_zip_dir (jcf->read_state);
1085       return JCF_ZIP;
1086     }
1087
1088   return JCF_SOURCE;
1089 }
1090
1091 /* Initialization.  */
1092
1093 void
1094 init_jcf_parse ()
1095 {
1096   /* Register roots with the garbage collector.  */
1097   ggc_add_tree_root (&current_field, 1);
1098   ggc_add_tree_root (&current_method, 1);
1099   ggc_add_tree_root (&current_file_list, 1);
1100 }