OSDN Git Service

2001-03-20 Matt Kraai <kraai@alumni.carnegiemellon.edu>
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-parse.c
1 /* Parser for Java(TM) .class files.
2    Copyright (C) 1996, 1998, 1999, 2000, 2001 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 static tree parse_roots[3] = { NULL_TREE, NULL_TREE, NULL_TREE };
74
75 /* The FIELD_DECL for the current field.  */
76 #define current_field parse_roots[0]
77
78 /* The METHOD_DECL for the current method.  */
79 #define current_method parse_roots[1]
80
81 /* A list of file names.  */
82 #define current_file_list parse_roots[2]
83
84 /* The Java archive that provides main_class;  the main input file. */
85 static struct JCF main_jcf[1];
86
87 static struct ZipFile *localToFile;
88
89 /* Declarations of some functions used here.  */
90 static void handle_innerclass_attribute PARAMS ((int count, JCF *));
91 static tree give_name_to_class PARAMS ((JCF *jcf, int index));
92 static void parse_zip_file_entries PARAMS ((void));
93 static void process_zip_dir PARAMS ((FILE *));
94 static void parse_source_file_1 PARAMS ((tree, FILE *));
95 static void parse_source_file_2 PARAMS ((void));
96 static void jcf_parse_source PARAMS ((void));
97 static void parse_class_file PARAMS ((void));
98 static void set_source_filename PARAMS ((JCF *, int));
99 static int predefined_filename_p PARAMS ((tree));
100 static void ggc_mark_jcf PARAMS ((void**));
101 static void jcf_parse PARAMS ((struct JCF*));
102 static void load_inner_classes PARAMS ((tree));
103
104 /* Mark (for garbage collection) all the tree nodes that are
105    referenced from JCF's constant pool table. */
106
107 static void
108 ggc_mark_jcf (elt)
109      void **elt;
110 {
111   JCF *jcf = *(JCF**) elt;
112   if (jcf != NULL)
113     {
114       CPool *cpool = &jcf->cpool;
115       int size = CPOOL_COUNT(cpool);
116       int index;
117       for (index = 1; index < size;  index++)
118         {
119           int tag = JPOOL_TAG (jcf, index);
120           if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
121             ggc_mark_tree ((tree) cpool->data[index]);
122         }
123     }
124 }
125
126 /* Handle "SourceFile" attribute. */
127
128 static void
129 set_source_filename (jcf, index)
130      JCF *jcf;
131      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 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
193   if (handle_type != current_class) layout_type (handle_type); \
194   current_method = NULL_TREE; }
195
196 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
197 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
198   DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
199   DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
200   DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
201
202 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
203 { int n = (COUNT); \
204   DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
205   JCF_SKIP (jcf, n * 10); }
206
207 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
208 { int n = (COUNT); \
209   DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
210   JCF_SKIP (jcf, n * 4); }
211
212 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
213 { \
214   int n = COUNT; \
215   tree list = DECL_FUNCTION_THROWS (current_method); \
216   while (--n >= 0) \
217     { \
218       tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
219       list = tree_cons (NULL_TREE, thrown_class, list); \
220     } \
221   DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
222 }
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 #include "jcf-reader.c"
240
241 static int yydebug;
242
243 tree
244 parse_signature (jcf, sig_index)
245      JCF *jcf;
246      int sig_index;
247 {
248   if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
249       || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
250     abort ();
251   else
252     return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
253                                    JPOOL_UTF_LENGTH (jcf, sig_index));
254 }
255
256 void
257 init_lex ()
258 {
259   /* Make identifier nodes long enough for the language-specific slots.  */
260   set_identifier_size (sizeof (struct lang_identifier));
261 }
262
263 void
264 set_yydebug (value)
265      int value;
266 {
267   yydebug = value;
268 }
269
270 tree
271 get_constant (jcf, index)
272   JCF *jcf;
273   int index;
274 {
275   tree value;
276   int tag;
277   if (index <= 0 || index >= JPOOL_SIZE(jcf))
278     goto bad;
279   tag = JPOOL_TAG (jcf, index);
280   if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
281     return (tree) jcf->cpool.data[index];
282   switch (tag)
283     {
284     case CONSTANT_Integer:
285       {
286         jint num = JPOOL_INT(jcf, index);
287         value = build_int_2 (num, num < 0 ? -1 : 0);
288         TREE_TYPE (value) = int_type_node;
289         break;
290       }
291     case CONSTANT_Long:
292       {
293         jint num = JPOOL_INT (jcf, index);
294         HOST_WIDE_INT lo, hi;
295         lshift_double (num, 0, 32, 64, &lo, &hi, 0);
296         num = JPOOL_INT (jcf, index+1) & 0xffffffff;
297         add_double (lo, hi, num, 0, &lo, &hi);
298         value = build_int_2 (lo, hi);
299         TREE_TYPE (value) = long_type_node;
300         force_fit_type (value, 0);
301         break;
302       }
303 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
304     case CONSTANT_Float:
305       {
306         jint num = JPOOL_INT(jcf, index);
307         REAL_VALUE_TYPE d;
308 #ifdef REAL_ARITHMETIC
309         d = REAL_VALUE_FROM_TARGET_SINGLE (num);
310 #else
311         union { float f;  jint i; } u;
312         u.i = num;
313         d = u.f;
314 #endif
315         value = build_real (float_type_node, d);
316         break;
317       }
318     case CONSTANT_Double:
319       {
320         HOST_WIDE_INT num[2];
321         REAL_VALUE_TYPE d;
322         HOST_WIDE_INT lo, hi;
323         num[0] = JPOOL_INT (jcf, index);
324         lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
325         num[0] = JPOOL_INT (jcf, index+1);
326         add_double (lo, hi, num[0], 0, &lo, &hi);
327         if (FLOAT_WORDS_BIG_ENDIAN)
328           {
329             num[0] = hi;
330             num[1] = lo;
331           }
332         else
333           {
334             num[0] = lo;
335             num[1] = hi;
336           }
337 #ifdef REAL_ARITHMETIC
338         d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
339 #else
340         {
341           union { double d;  jint i[2]; } u;
342           u.i[0] = (jint) num[0];
343           u.i[1] = (jint) num[1];
344           d = u.d;
345         }
346 #endif
347         value = build_real (double_type_node, d);
348         break;
349       }
350 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
351     case CONSTANT_String:
352       {
353         tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
354         const char *utf8_ptr = IDENTIFIER_POINTER (name);
355         int utf8_len = IDENTIFIER_LENGTH (name);
356         unsigned char *str_ptr;
357         unsigned char *str;
358         const unsigned char *utf8;
359         int i, str_len;
360
361         /* Count the number of Unicode characters in the string,
362            while checking for a malformed Utf8 string. */
363         utf8 = (const unsigned char *) utf8_ptr;
364         i = utf8_len;
365         str_len = 0;
366         while (i > 0)
367           {
368             int char_len = UT8_CHAR_LENGTH (*utf8);
369             if (char_len < 0 || char_len > 3 || char_len > i)
370               fatal_error ("bad string constant");
371
372             utf8 += char_len;
373             i -= char_len;
374             str_len++;
375           }
376
377         /* Allocate a scratch buffer, convert the string to UCS2, and copy it
378            into the new space.  */
379         str_ptr = (unsigned char *) alloca (2 * str_len);
380         str = str_ptr;
381         utf8 = (const unsigned char *)utf8_ptr;
382
383         for (i = 0; i < str_len; i++)
384           {
385             int char_value;
386             int char_len = UT8_CHAR_LENGTH (*utf8);
387             switch (char_len)
388               {
389               case 1:
390                 char_value = *utf8++;
391                 break;
392               case 2:
393                 char_value = *utf8++ & 0x1F;
394                 char_value = (char_value << 6) | (*utf8++ & 0x3F);
395                 break;
396               case 3:
397                 char_value = *utf8++ & 0x0F;
398                 char_value = (char_value << 6) | (*utf8++ & 0x3F);
399                 char_value = (char_value << 6) | (*utf8++ & 0x3F);
400                 break;
401               default:
402                 goto bad;
403               }
404             if (BYTES_BIG_ENDIAN)
405               {
406                 *str++ = char_value >> 8;
407                 *str++ = char_value & 0xFF;
408               }
409             else
410               {
411                 *str++ = char_value & 0xFF;
412                 *str++ = char_value >> 8;
413               }
414           }
415         value = build_string (str - str_ptr, str_ptr);
416         TREE_TYPE (value) = build_pointer_type (string_type_node);
417       }
418       break;
419     default:
420       goto bad;
421     }
422   JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
423   jcf->cpool.data [index] = (jword) value;
424   return value;
425  bad:
426   internal_error ("bad value constant type %d, index %d", 
427                   JPOOL_TAG (jcf, index), index);
428 }
429
430 tree
431 get_name_constant (jcf, index)
432   JCF *jcf;
433   int index;
434 {
435   tree name = get_constant (jcf, index);
436
437   if (TREE_CODE (name) != IDENTIFIER_NODE)
438     abort ();
439
440   return name;
441 }
442
443 /* Handle reading innerclass attributes. If a non zero entry (denoting
444    a non anonymous entry) is found, We augment the inner class list of
445    the outer context with the newly resolved innerclass.  */
446
447 static void
448 handle_innerclass_attribute (count, jcf)
449      int count;
450      JCF *jcf;
451 {
452   int c = (count);
453   while (c--)
454     {
455       /* Read inner_class_info_index. This may be 0 */
456       int icii = JCF_readu2 (jcf);
457       /* Read outer_class_info_index. If the innerclasses attribute
458          entry isn't a member (like an inner class) the value is 0. */
459       int ocii = JCF_readu2 (jcf);
460       /* Read inner_name_index. If the class we're dealing with is
461          an annonymous class, it must be 0. */
462       int ini = JCF_readu2 (jcf);
463       /* If icii is 0, don't try to read the class. */
464       if (icii >= 0)
465         {
466           tree class = get_class_constant (jcf, icii);
467           tree decl = TYPE_NAME (class);
468           /* Skip reading further if ocii is null */
469           if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
470             {
471               tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
472               tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
473               DECL_CONTEXT (decl) = outer;
474               DECL_INNER_CLASS_LIST (outer) =
475                 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
476               CLASS_COMPLETE_P (decl) = 1;
477             }
478         }
479       JCF_SKIP (jcf, 2);
480     }
481 }
482
483 static tree
484 give_name_to_class (jcf, i)
485      JCF *jcf;
486      int i;
487 {
488   if (i <= 0 || i >= JPOOL_SIZE (jcf)
489       || JPOOL_TAG (jcf, i) != CONSTANT_Class)
490     abort ();
491   else
492     {
493       tree this_class;
494       int j = JPOOL_USHORT1 (jcf, i);
495       /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
496       tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
497                                             JPOOL_UTF_LENGTH (jcf, j));
498       this_class = lookup_class (class_name);
499       input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
500       lineno = 0;
501       if (main_input_filename == NULL && jcf == main_jcf)
502         main_input_filename = input_filename;
503
504       jcf->cpool.data[i] = (jword) this_class;
505       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
506       return this_class;
507     }
508 }
509
510 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
511
512 tree
513 get_class_constant (JCF *jcf , int i)
514 {
515   tree type;
516   if (i <= 0 || i >= JPOOL_SIZE (jcf)
517       || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
518     abort ();
519
520   if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
521     {
522       int name_index = JPOOL_USHORT1 (jcf, i);
523       /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
524       const char *name = JPOOL_UTF_DATA (jcf, name_index);
525       int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
526
527       if (name[0] == '[')  /* Handle array "classes". */
528           type = TREE_TYPE (parse_signature_string (name, nlength));
529       else
530         { 
531           tree cname = unmangle_classname (name, nlength);
532           type = lookup_class (cname);
533         }
534       jcf->cpool.data[i] = (jword) type;
535       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
536     }
537   else
538     type = (tree) jcf->cpool.data[i];
539   return type;
540 }
541
542 /* Read a class with the fully qualified-name NAME.
543    Return 1 iff we read the requested file.
544    (It is still possible we failed if the file did not
545    define the class it is supposed to.) */
546
547 int
548 read_class (name)
549      tree name;
550 {
551   JCF this_jcf, *jcf;
552   tree icv, class;
553   tree save_current_class = current_class;
554   const char *save_input_filename = input_filename;
555   JCF *save_current_jcf = current_jcf;
556
557   if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
558     {
559       class = TREE_TYPE (icv);
560       jcf = TYPE_JCF (class);
561     }
562   else
563     jcf = NULL;
564
565   if (jcf == NULL)
566     {
567       this_jcf.zipd = NULL;
568       jcf = &this_jcf;
569       if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
570                       &this_jcf, 1) == 0)
571         return 0;
572     }
573
574   current_jcf = jcf;
575
576   if (current_jcf->java_source)
577     jcf_parse_source ();
578   else {
579     java_parser_context_save_global ();
580     java_push_parser_context ();
581     input_filename = current_jcf->filename;
582     current_class = class;
583     if (JCF_SEEN_IN_ZIP (current_jcf))
584       read_zip_member(current_jcf, current_jcf->zipd, current_jcf->zipd->zipf);
585     jcf_parse (current_jcf);
586     load_inner_classes (current_class);
587     java_pop_parser_context (0);
588     java_parser_context_restore_global ();
589   }
590
591   if (! JCF_SEEN_IN_ZIP (current_jcf))
592     JCF_FINISH (current_jcf);
593
594   current_class = save_current_class;
595   input_filename = save_input_filename;
596   current_jcf = save_current_jcf;
597   return 1;
598 }
599
600 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
601    called from the parser, otherwise it's a RECORD_TYPE node. If
602    VERBOSE is 1, print error message on failure to load a class. */
603
604 /* Replace calls to load_class by having callers call read_class directly
605    - and then perhaps rename read_class to load_class.  FIXME */
606
607 void
608 load_class (class_or_name, verbose)
609      tree class_or_name;
610      int verbose;
611 {
612   tree name;
613
614   /* class_or_name can be the name of the class we want to load */
615   if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
616     name = class_or_name;
617   /* In some cases, it's a dependency that we process earlier that
618      we though */
619   else if (TREE_CODE (class_or_name) == TREE_LIST)
620     name = TYPE_NAME (TREE_PURPOSE (class_or_name));
621   /* Or it's a type in the making */
622   else
623     name = DECL_NAME (TYPE_NAME (class_or_name));
624
625   if (read_class (name) == 0 && verbose)
626     error ("Cannot find file for class %s.", IDENTIFIER_POINTER (name));
627 }
628
629 /* Parse a source file when JCF refers to a source file.  */
630
631 static void
632 jcf_parse_source ()
633 {
634   tree file;
635   FILE *finput;
636
637   java_parser_context_save_global ();
638   java_push_parser_context ();
639   BUILD_FILENAME_IDENTIFIER_NODE (file, current_jcf->filename);
640   if (wfl_operator == NULL_TREE)
641     wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
642   EXPR_WFL_FILENAME_NODE (wfl_operator) = file;
643   input_filename = ggc_strdup (current_jcf->filename);
644   current_class = NULL_TREE;
645   current_function_decl = NULL_TREE;
646   if (!HAS_BEEN_ALREADY_PARSED_P (file))
647     {
648       if (!(finput = fopen (input_filename, "r")))
649         fatal_io_error ("can't reopen %s", input_filename);
650       parse_source_file_1 (file, finput);
651       parse_source_file_2 ();
652       if (fclose (finput))
653         fatal_io_error ("can't close %s", input_filename);
654     }
655   java_pop_parser_context (IS_A_COMMAND_LINE_FILENAME_P (file));
656   java_parser_context_restore_global ();
657 }
658
659 /* Parse the .class file JCF. */
660
661 void
662 jcf_parse (jcf)
663      JCF* jcf;
664 {
665   int i, code;
666
667   if (jcf_parse_preamble (jcf) != 0)
668     fatal_error ("not a valid Java .class file");
669   code = jcf_parse_constant_pool (jcf);
670   if (code != 0)
671     fatal_error ("error while parsing constant pool");
672   code = verify_constant_pool (jcf);
673   if (code > 0)
674     fatal_error ("error in constant pool entry #%d\n", code);
675
676   jcf_parse_class (jcf);
677   if (main_class == NULL_TREE)
678     main_class = current_class;
679   if (! quiet_flag && TYPE_NAME (current_class))
680     fprintf (stderr, " %s %s",
681              (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class", 
682              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
683   if (CLASS_LOADED_P (current_class))
684     return;
685   CLASS_LOADED_P (current_class) = 1;
686
687   for (i = 1; i < JPOOL_SIZE(jcf); i++)
688     {
689       switch (JPOOL_TAG (jcf, i))
690         {
691         case CONSTANT_Class:
692           get_class_constant (jcf, i);
693           break;
694         }
695     }
696   
697   code = jcf_parse_fields (jcf);
698   if (code != 0)
699     fatal_error ("error while parsing fields");
700   code = jcf_parse_methods (jcf);
701   if (code != 0)
702     fatal_error ("error while parsing methods");
703   code = jcf_parse_final_attributes (jcf);
704   if (code != 0)
705     fatal_error ("error while parsing final attributes");
706
707   /* The fields of class_type_node are already in correct order. */
708   if (current_class != class_type_node && current_class != object_type_node)
709     TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
710
711   layout_class (current_class);
712   if (current_class == object_type_node)
713     layout_class_methods (object_type_node);
714   else
715     all_class_list = tree_cons (NULL_TREE,
716                                 TYPE_NAME (current_class), all_class_list );
717 }
718
719 /* If we came across inner classes, load them now. */
720 static void
721 load_inner_classes (cur_class)
722      tree cur_class;
723 {
724   tree current;
725   for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
726        current = TREE_CHAIN (current))
727     {
728       tree name = DECL_NAME (TREE_PURPOSE (current));
729       tree decl = IDENTIFIER_GLOBAL_VALUE (name);
730       if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
731           && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
732         load_class (name, 1);
733     }
734 }
735
736 void
737 init_outgoing_cpool ()
738 {
739   current_constant_pool_data_ref = NULL_TREE;
740   outgoing_cpool = (struct CPool *)xmalloc (sizeof (struct CPool));
741   memset (outgoing_cpool, 0, sizeof (struct CPool));
742 }
743
744 static void
745 parse_class_file ()
746 {
747   tree method;
748   const char *save_input_filename = input_filename;
749   int save_lineno = lineno;
750
751   java_layout_seen_class_methods ();
752
753   input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
754   lineno = 0;
755   debug_start_source_file (input_filename);
756   init_outgoing_cpool ();
757
758   /* Currently we always have to emit calls to _Jv_InitClass when
759      compiling from class files.  */
760   always_initialize_class_p = 1;
761
762   for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
763         method != NULL_TREE; method = TREE_CHAIN (method))
764     {
765       JCF *jcf = current_jcf;
766
767       if (METHOD_ABSTRACT (method))
768         continue;
769
770       if (METHOD_NATIVE (method))
771         {
772           if (! flag_jni)
773             continue;
774           DECL_MAX_LOCALS (method)
775             = list_length (TYPE_ARG_TYPES (TREE_TYPE (method)));
776           start_java_method (method);
777           give_name_to_locals (jcf);
778           expand_expr_stmt (build_jni_stub (method));
779           end_java_method ();
780           continue;
781         }
782
783       if (DECL_CODE_OFFSET (method) == 0)
784         {
785           error ("missing Code attribute");
786           continue;
787         }
788
789       lineno = 0;
790       if (DECL_LINENUMBERS_OFFSET (method))
791         {
792           register int i;
793           register unsigned char *ptr;
794           JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
795           linenumber_count = i = JCF_readu2 (jcf);
796           linenumber_table = ptr = jcf->read_ptr;
797
798           for (ptr += 2; --i >= 0; ptr += 4)
799             {
800               int line = GET_u2 (ptr);
801               /* Set initial lineno lineno to smallest linenumber.
802                * Needs to be set before init_function_start. */
803               if (lineno == 0 || line < lineno)
804                 lineno = line;
805             }  
806         }
807       else
808         {
809           linenumber_table = NULL;
810           linenumber_count = 0;
811         }
812
813       start_java_method (method);
814
815       note_instructions (jcf, method);
816
817       give_name_to_locals (jcf);
818
819       /* Actually generate code. */
820       expand_byte_code (jcf, method);
821
822       end_java_method ();
823     }
824
825   if (flag_emit_class_files)
826     write_classfile (current_class);
827
828   finish_class ();
829
830   debug_end_source_file (save_lineno);
831   input_filename = save_input_filename;
832   lineno = save_lineno;
833 }
834
835 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
836
837 static void
838 parse_source_file_1 (file, finput)
839      tree file;
840      FILE *finput;
841 {
842   int save_error_count = java_error_count;
843   /* Mark the file as parsed */
844   HAS_BEEN_ALREADY_PARSED_P (file) = 1;
845
846   jcf_dependency_add_file (input_filename, 0);
847
848   lang_init_source (1);             /* Error msgs have no method prototypes */
849
850   /* There's no point in trying to find the current encoding unless we
851      are going to do something intelligent with it -- hence the test
852      for iconv.  */
853 #ifdef HAVE_ICONV
854 #ifdef HAVE_NL_LANGINFO
855   setlocale (LC_CTYPE, "");
856   if (current_encoding == NULL)
857     current_encoding = nl_langinfo (CODESET);
858 #endif /* HAVE_NL_LANGINFO */
859 #endif /* HAVE_ICONV */
860   if (current_encoding == NULL || *current_encoding == '\0')
861     current_encoding = DEFAULT_ENCODING;
862
863   /* Initialize the parser */
864   java_init_lex (finput, current_encoding);
865   java_parse_abort_on_error ();
866
867   java_parse ();                    /* Parse and build partial tree nodes. */
868   java_parse_abort_on_error ();
869 }
870
871 /* Process a parsed source file, resolving names etc. */
872
873 static void
874 parse_source_file_2 ()
875 {
876   int save_error_count = java_error_count;
877   java_complete_class ();           /* Parse unsatisfied class decl. */
878   java_parse_abort_on_error ();
879   java_check_circular_reference (); /* Check on circular references */
880   java_parse_abort_on_error ();
881   java_fix_constructors ();         /* Fix the constructors */
882   java_parse_abort_on_error ();
883   java_reorder_fields ();           /* Reorder the fields */
884 }
885
886 static int
887 predefined_filename_p (node)
888      tree node;
889 {
890   int i;
891   for (i = 0; i < PREDEF_FILENAMES_SIZE; i++)
892     if (predef_filenames [i] == node)
893       return 1;
894   return 0;
895 }
896
897 int
898 yyparse ()
899 {
900   int filename_count = 0;
901   char *list, *next;
902   tree node;
903   FILE *finput = NULL;
904
905   if (flag_filelist_file)
906     {
907       int avail = 2000;
908       finput = fopen (input_filename, "r");
909       if (finput == NULL)
910         fatal_io_error ("can't open %s", input_filename);
911       list = xmalloc(avail);
912       next = list;
913       for (;;)
914         {
915           int count;
916           if (avail < 500)
917             {
918               count = next - list;
919               avail = 2 * (count + avail);
920               list = xrealloc (list, avail);
921               next = list + count;
922               avail = avail - count;
923             }
924           /* Subtract to to guarantee space for final '\0'. */
925           count = fread (next, 1, avail - 1, finput);
926           if (count == 0)
927             {
928               if (! feof (finput))
929                 fatal_io_error ("error closing %s", input_filename);
930               *next = '\0';
931               break;
932             }
933           avail -= count;
934           next += count;
935         }
936       fclose (finput);
937       finput = NULL;
938     }
939   else
940     list = xstrdup (input_filename);
941
942   do 
943     {
944       for (next = list; ; )
945         {
946           char ch = *next;
947           if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
948               || ch == '&' /* FIXME */)
949             {
950               if (next == list)
951                 {
952                   next++;
953                   list = next;
954                   continue;
955                 }
956               else
957                 {
958                   *next++ = '\0';
959                   break;
960                 }
961             }
962           if (ch == '\0')
963             {
964               next = NULL;
965               break;
966             }
967           next++;
968         }
969
970       if (list[0]) 
971         {
972           char *value;
973           tree id;
974           int twice = 0;
975
976           int len = strlen (list);
977
978           if (*list != '/' && filename_count > 0)
979             obstack_grow (&temporary_obstack, "./", 2);
980
981           obstack_grow0 (&temporary_obstack, list, len);
982           value = obstack_finish (&temporary_obstack);
983
984           filename_count++;
985
986           /* Exclude file that we see twice on the command line. For
987              all files except {Class,Error,Object,RuntimeException,String,
988              Throwable}.java we can rely on maybe_get_identifier. For
989              these files, we need to do a linear search of
990              current_file_list. This search happens only for these
991              files, presumably only when we're recompiling libgcj. */
992              
993           if ((id = maybe_get_identifier (value)))
994             {
995               if (predefined_filename_p (id))
996                 {
997                   tree c;
998                   for (c = current_file_list; c; c = TREE_CHAIN (c))
999                     if (TREE_VALUE (c) == id)
1000                       twice = 1;
1001                 }
1002               else
1003                 twice = 1;
1004             }
1005
1006           if (twice)
1007             {
1008               const char *saved_input_filename = input_filename;
1009               input_filename = value;
1010               warning ("source file seen twice on command line and will be compiled only once.");
1011               input_filename = saved_input_filename;
1012             }
1013           else
1014             {
1015               BUILD_FILENAME_IDENTIFIER_NODE (node, value);
1016               IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1017               current_file_list = tree_cons (NULL_TREE, node, 
1018                                              current_file_list);
1019             }
1020         }
1021       list = next;
1022     }
1023   while (next);
1024
1025   if (filename_count == 0)
1026     warning ("no input file specified");
1027
1028   current_jcf = main_jcf;
1029   current_file_list = nreverse (current_file_list);
1030   for (node = current_file_list; node; node = TREE_CHAIN (node))
1031     {
1032       unsigned char magic_string[4];
1033       uint32 magic;
1034       tree name = TREE_VALUE (node);
1035
1036       /* Skip already parsed files */
1037       if (HAS_BEEN_ALREADY_PARSED_P (name))
1038         continue;
1039       
1040       /* Close previous descriptor, if any */
1041       if (finput && fclose (finput))
1042         fatal_io_error ("can't close input file %s", main_input_filename);
1043       
1044       finput = fopen (IDENTIFIER_POINTER (name), "rb");
1045       if (finput == NULL)
1046         fatal_io_error ("can't open %s", IDENTIFIER_POINTER (name));
1047       
1048 #ifdef IO_BUFFER_SIZE
1049       setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
1050                _IOFBF, IO_BUFFER_SIZE);
1051 #endif
1052       input_filename = IDENTIFIER_POINTER (name);
1053
1054       /* Figure what kind of file we're dealing with */
1055       if (fread (magic_string, 1, 4, finput) != 4)
1056         fatal_io_error ("Premature end of input file %s", 
1057                         IDENTIFIER_POINTER (name));
1058       fseek (finput, 0L, SEEK_SET);
1059       magic = GET_u4 (magic_string);
1060       if (magic == 0xcafebabe)
1061         {
1062           CLASS_FILE_P (node) = 1;
1063           current_jcf = ALLOC (sizeof (JCF));
1064           JCF_ZERO (current_jcf);
1065           current_jcf->read_state = finput;
1066           current_jcf->filbuf = jcf_filbuf_from_stdio;
1067           jcf_parse (current_jcf);
1068           TYPE_JCF (current_class) = current_jcf;
1069           CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1070           TREE_PURPOSE (node) = current_class;
1071         }
1072       else if (magic == (JCF_u4)ZIPMAGIC)
1073         {
1074           ZIP_FILE_P (node) = 1;
1075           JCF_ZERO (main_jcf);
1076           main_jcf->read_state = finput;
1077           main_jcf->filbuf = jcf_filbuf_from_stdio;
1078           if (open_in_zip (main_jcf, input_filename, NULL, 0) <  0)
1079             fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1080           localToFile = SeenZipFiles;
1081           /* Register all the class defined there.  */
1082           process_zip_dir (main_jcf->read_state);
1083           parse_zip_file_entries ();
1084           /*
1085           for (each entry)
1086             CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1087           */
1088         }
1089       else
1090         {
1091           JAVA_FILE_P (node) = 1;
1092           java_push_parser_context ();
1093           java_parser_context_save_global ();
1094           parse_source_file_1 (name, finput);
1095           java_parser_context_restore_global ();
1096           java_pop_parser_context (1);
1097         }
1098     }
1099
1100   for (ctxp = ctxp_for_generation;  ctxp;  ctxp = ctxp->next)
1101     {
1102       input_filename = ctxp->filename;
1103       parse_source_file_2 ();
1104     }
1105   for (node = current_file_list; node; node = TREE_CHAIN (node))
1106     {
1107       input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1108       if (CLASS_FILE_P (node))
1109         {
1110           current_class = TREE_PURPOSE (node);
1111           current_jcf = TYPE_JCF (current_class);
1112           load_inner_classes (current_class);
1113           parse_class_file ();
1114         }
1115     }
1116   input_filename = main_input_filename;
1117
1118   java_expand_classes ();
1119   if (!java_report_errors () && !flag_syntax_only)
1120     emit_register_classes ();
1121   return 0;
1122 }
1123
1124 /* Process all class entries found in the zip file.  */
1125 static void
1126 parse_zip_file_entries (void)
1127 {
1128   struct ZipDirectory *zdir;
1129   int i;
1130
1131   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1132        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1133     {
1134       tree class;
1135       
1136       /* We don't need to consider those files.  */
1137       if (!zdir->size || !zdir->filename_offset)
1138         continue;
1139
1140       class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
1141       current_jcf = TYPE_JCF (class);
1142       current_class = class;
1143
1144       if ( !CLASS_LOADED_P (class))
1145         {
1146           read_zip_member(current_jcf, zdir, localToFile);
1147           jcf_parse (current_jcf);
1148           load_inner_classes (current_class);
1149         }
1150
1151       if (TYPE_SIZE (current_class) != error_mark_node)
1152         {
1153           input_filename = current_jcf->filename;
1154           parse_class_file ();
1155           FREE (current_jcf->buffer); /* No longer necessary */
1156           /* Note: there is a way to free this buffer right after a
1157              class seen in a zip file has been parsed. The idea is the
1158              set its jcf in such a way that buffer will be reallocated
1159              the time the code for the class will be generated. FIXME. */
1160         }
1161     }
1162 }
1163
1164 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1165    jcf up for further processing and link it to the created class.  */
1166
1167 static void
1168 process_zip_dir (FILE *finput)
1169 {
1170   int i;
1171   ZipDirectory *zdir;
1172
1173   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1174        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1175     {
1176       char *class_name, *file_name, *class_name_in_zip_dir;
1177       tree class;
1178       JCF  *jcf;
1179       int   j;
1180
1181       class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1182
1183       /* We choose to not to process entries with a zero size or entries
1184          not bearing the .class extention.  */
1185       if (!zdir->size || !zdir->filename_offset ||
1186           strncmp (&class_name_in_zip_dir[zdir->filename_length-6], 
1187                    ".class", 6))
1188         {
1189           /* So it will be skipped in parse_zip_file_entries  */
1190           zdir->size = 0;  
1191           continue;
1192         }
1193
1194       class_name = ALLOC (zdir->filename_length+1-6);
1195       file_name  = ALLOC (zdir->filename_length+1);
1196       jcf = ALLOC (sizeof (JCF));
1197       JCF_ZERO (jcf);
1198
1199       strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
1200       class_name [zdir->filename_length-6] = '\0';
1201       strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1202       file_name [zdir->filename_length] = '\0';
1203
1204       for (j=0; class_name[j]; j++)
1205         class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
1206
1207       /* Yes, we write back the true class name into the zip directory.  */
1208       strcpy (class_name_in_zip_dir, class_name);
1209       zdir->filename_length = j;
1210       class = lookup_class (get_identifier (class_name));
1211
1212       jcf->read_state  = finput;
1213       jcf->filbuf      = jcf_filbuf_from_stdio;
1214       jcf->java_source = 0;
1215       jcf->classname   = class_name;
1216       jcf->filename    = file_name;
1217       jcf->zipd        = zdir;
1218
1219       TYPE_JCF (class) = jcf;
1220     }
1221 }
1222
1223 /* Initialization.  */
1224
1225 void
1226 init_jcf_parse ()
1227 {
1228   /* Register roots with the garbage collector.  */
1229   ggc_add_tree_root (parse_roots, sizeof (parse_roots) / sizeof(tree));
1230
1231   ggc_add_root (&current_jcf, 1, sizeof (JCF), (void (*)(void *))ggc_mark_jcf);
1232
1233   init_src_parse ();
1234 }