OSDN Git Service

Tue Nov 24 17:06:38 1998 Per Bothner <bothner@cygnus.com>
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-parse.c
1 /* Parser for Java(TM) .class files.
2    Copyright (C) 1996, 1998 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
36 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
37 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
38 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
39 #define JPOOL_UTF_DATA(JCF, INDEX) \
40   ((unsigned char*) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
41 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
42   do { \
43     unsigned char save;  unsigned char *text; \
44     JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
45     text = (JCF)->read_ptr; \
46     save = text[LENGTH]; \
47     text[LENGTH] = 0; \
48     (JCF)->cpool.data[INDEX] = (jword) get_identifier (text); \
49     text[LENGTH] = save; \
50     JCF_SKIP (JCF, LENGTH); } while (0)
51
52 #include "jcf.h"
53
54 extern struct obstack *saveable_obstack;
55 extern struct obstack temporary_obstack;
56 extern struct obstack permanent_obstack;
57
58 /* The class we are currently processing. */
59 tree current_class = NULL_TREE;
60
61 /* The class we started with. */
62 tree main_class = NULL_TREE;
63
64 /* List of all class DECL seen so far.  */
65 tree all_class_list = NULL_TREE;
66
67 /* The FIELD_DECL for the current field.  */
68 static tree current_field = NULL_TREE;
69
70 /* The METHOD_DECL for the current method.  */
71 static tree current_method = NULL_TREE;
72
73 /* Declarations of some functions used here.  */
74 static tree give_name_to_class PROTO ((JCF *jcf, int index));
75 void parse_zip_file_entries PROTO (());
76 void process_zip_dir PROTO (());
77 static void parse_source_file PROTO ((tree));
78 static void jcf_parse_source PROTO ((JCF *));
79
80 /* Handle "SourceFile" attribute. */
81
82 void
83 set_source_filename (jcf, index)
84      JCF *jcf;
85      int index;
86 {
87   tree sfname_id = get_name_constant (jcf, index);
88   char *sfname = IDENTIFIER_POINTER (sfname_id);
89   if (input_filename != NULL)
90     {
91       int old_len = strlen (input_filename);
92       int new_len = IDENTIFIER_LENGTH (sfname_id);
93       /* Use the current input_filename (derived from the class name)
94          if it has a directory prefix, but otherwise matches sfname. */
95       if (old_len > new_len
96           && strcmp (sfname, input_filename + old_len - new_len) == 0
97           && (input_filename[old_len - new_len - 1] == '/'
98               || input_filename[old_len - new_len - 1] == '\\'))
99         return;
100     }
101   input_filename = sfname;
102   DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
103   if (current_class == main_class) main_input_filename = input_filename;
104 }
105
106 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
107
108 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
109 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
110   current_class = give_name_to_class (jcf, THIS); \
111   set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
112
113 #define HANDLE_CLASS_INTERFACE(INDEX) \
114   add_interface (current_class, get_class_constant (jcf, INDEX))
115
116 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
117 { int sig_index = SIGNATURE; \
118   current_field = add_field (current_class, get_name_constant (jcf, NAME), \
119                              parse_signature (jcf, sig_index), ACCESS_FLAGS); \
120  set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); }
121
122 #define HANDLE_END_FIELDS() \
123   (current_field = NULL_TREE)
124
125 #define HANDLE_CONSTANTVALUE(INDEX) \
126 { tree constant;  int index = INDEX; \
127   if (JPOOL_TAG (jcf, index) == CONSTANT_String) { \
128     tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
129     constant = build_utf8_ref (name); \
130   } \
131   else \
132     constant = get_constant (jcf, index); \
133   set_constant_value (current_field, constant); }
134
135 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
136  (current_method = add_method (current_class, ACCESS_FLAGS, \
137                                get_name_constant (jcf, NAME), \
138                                get_name_constant (jcf, SIGNATURE)), \
139   DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
140   DECL_LINENUMBERS_OFFSET (current_method) = 0)
141
142 #define HANDLE_END_METHODS() \
143 { tree handle_type = CLASS_TO_HANDLE_TYPE (current_class); \
144   if (handle_type != current_class) layout_type (handle_type); }
145
146 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
147 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
148   DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
149   DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
150   DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
151
152 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
153 { int n = (COUNT); \
154   DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
155   JCF_SKIP (jcf, n * 10); }
156
157 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
158 { int n = (COUNT); \
159   DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
160   JCF_SKIP (jcf, n * 4); }
161
162 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
163 { \
164   int n = COUNT; \
165   tree list = DECL_FUNCTION_THROWS (current_method); \
166   while (--n >= 0) \
167     { \
168       tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
169       list = tree_cons (NULL_TREE, thrown_class, list); \
170     } \
171   DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
172 }
173
174 #include "jcf-reader.c"
175
176 static int yydebug;
177
178 tree
179 parse_signature (jcf, sig_index)
180      JCF *jcf;
181      int sig_index;
182 {
183   if (sig_index <= 0 || sig_index >= JPOOL_SIZE(jcf)
184       || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
185     fatal ("invalid field/method signature");
186   else
187     {
188       return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
189                                      JPOOL_UTF_LENGTH (jcf, sig_index));
190     }
191 }
192
193 void
194 init_lex ()
195 {
196   /* Make identifier nodes long enough for the language-specific slots.  */
197   set_identifier_size (sizeof (struct lang_identifier));
198 }
199
200 void
201 set_yydebug (value)
202      int value;
203 {
204   yydebug = value;
205 }
206
207 tree
208 get_constant (jcf, index)
209   JCF *jcf;
210   int index;
211 {
212   tree value;
213   int tag;
214   if (index <= 0 || index >= JPOOL_SIZE(jcf))
215     goto bad;
216   tag = JPOOL_TAG (jcf, index);
217   if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
218     return (tree) jcf->cpool.data[index];
219   push_obstacks (&permanent_obstack, &permanent_obstack);
220   switch (tag)
221     {
222     case CONSTANT_Integer:
223       {
224         jint num = JPOOL_INT(jcf, index);
225         value = build_int_2 (num, num < 0 ? -1 : 0);
226         TREE_TYPE (value) = int_type_node;
227         break;
228       }
229     case CONSTANT_Long:
230       {
231         jint num = JPOOL_INT (jcf, index);
232         HOST_WIDE_INT lo, hi;
233         lshift_double (num, 0, 32, 64, &lo, &hi, 0);
234         num = JPOOL_INT (jcf, index+1);
235         add_double (lo, hi, num, 0, &lo, &hi);
236         value = build_int_2 (lo, hi);
237         TREE_TYPE (value) = long_type_node;
238         force_fit_type (value, 0);
239         break;
240       }
241 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
242     case CONSTANT_Float:
243       {
244         jint num = JPOOL_INT(jcf, index);
245         REAL_VALUE_TYPE d;
246 #ifdef REAL_ARITHMETIC
247         d = REAL_VALUE_FROM_TARGET_SINGLE (num);
248 #else
249         union { float f;  jint i; } u;
250         u.i = num;
251         d = u.f;
252 #endif
253         value = build_real (float_type_node, d);
254         break;
255       }
256     case CONSTANT_Double:
257       {
258         HOST_WIDE_INT num[2];
259         REAL_VALUE_TYPE d;
260         HOST_WIDE_INT lo, hi;
261         num[0] = JPOOL_INT (jcf, index);
262         lshift_double (num[0], 0, 32, 64, &lo, &hi, 0);
263         num[0] = JPOOL_INT (jcf, index+1);
264         add_double (lo, hi, num[0], 0, &lo, &hi);
265         if (FLOAT_WORDS_BIG_ENDIAN)
266           {
267             num[0] = hi;
268             num[1] = lo;
269           }
270         else
271           {
272             num[0] = lo;
273             num[1] = hi;
274           }
275 #ifdef REAL_ARITHMETIC
276         d = REAL_VALUE_FROM_TARGET_DOUBLE (num);
277 #else
278         union { double d;  jint i[2]; } u;
279         u.i[0] = (jint) num[0];
280         u.i[1] = (jint) num[1];
281         d = u.d;
282 #endif
283         value = build_real (double_type_node, d);
284         break;
285       }
286 #endif /* TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT */
287     case CONSTANT_String:
288       {
289         extern struct obstack *expression_obstack;
290         tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
291         char *utf8_ptr = IDENTIFIER_POINTER (name);
292         unsigned char *str_ptr;
293         int utf8_len = IDENTIFIER_LENGTH (name);
294         unsigned char *str = (unsigned char*)utf8_ptr;
295         int i = utf8_len;
296         int str_len;
297
298         /* Count the number of Unicode characters in the string,
299            while checking for a malformed Utf8 string. */
300         for (str_len = 0; i > 0; str_len++)
301           {
302             int char_len = UT8_CHAR_LENGTH (*str);
303             if (char_len < 0 || char_len > 2 || char_len > i)
304               fatal ("bad string constant");
305             str += char_len;
306             i -= char_len;
307           }
308
309         value = make_node (STRING_CST);
310         TREE_STRING_LENGTH (value) = 2 * str_len;
311         TREE_STRING_POINTER (value)
312           = obstack_alloc (expression_obstack, 2 * str_len);
313         str_ptr = (unsigned char *) TREE_STRING_POINTER (value);
314         str = (unsigned char*)utf8_ptr;
315         for (i = 0; i < str_len; i++)
316           {
317             int char_value;
318             int char_len = UT8_CHAR_LENGTH (*str);
319             switch (char_len)
320               {
321               case 1:
322                 char_value = *str++;
323                 break;
324               case 2:
325                 char_value = *str++ & 0x1F;
326                 char_value = (char_value << 6) | (*str++ & 0x3F);
327                 break;
328               case 3:
329                 char_value = *str_ptr++ & 0x0F;
330                 char_value = (char_value << 6) | (*str++ & 0x3F);
331                 char_value = (char_value << 6) | (*str++ & 0x3F);
332                 break;
333               default:
334                 goto bad;
335               }
336             if (BYTES_BIG_ENDIAN)
337               {
338                 *str_ptr++ = char_value >> 8;
339                 *str_ptr++ = char_value & 0xFF;
340               }
341             else
342               {
343                 *str_ptr++ = char_value & 0xFF;
344                 *str_ptr++ = char_value >> 8;
345               }
346           }
347       }
348       break;
349     default:
350       goto bad;
351     }
352   pop_obstacks ();
353   JPOOL_TAG(jcf, index) = tag | CONSTANT_ResolvedFlag;
354   jcf->cpool.data [index] = (jword) value;
355   return value;
356  bad:
357   fatal ("bad value constant type %d, index %d", 
358          JPOOL_TAG( jcf, index ), index);
359 }
360
361 tree
362 get_name_constant (jcf, index)
363   JCF *jcf;
364   int index;
365 {
366   tree name = get_constant (jcf, index);
367   if (TREE_CODE (name) != IDENTIFIER_NODE)
368     fatal ("bad nameandtype index %d", index);
369   return name;
370 }
371
372 static tree
373 give_name_to_class (jcf, i)
374      JCF *jcf;
375      int i;
376 {
377   if (i <= 0 || i >= JPOOL_SIZE(jcf)
378       || JPOOL_TAG (jcf, i) != CONSTANT_Class)
379     fatal ("bad class index %d", i);
380   else
381     {
382       tree this_class;
383       int j = JPOOL_USHORT1 (jcf, i);
384       /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
385       tree class_name = unmangle_classname (JPOOL_UTF_DATA (jcf, j),
386                                             JPOOL_UTF_LENGTH (jcf, j));
387       this_class = lookup_class (class_name);
388       input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
389       lineno = 0;
390       if (main_input_filename == NULL && jcf == main_jcf)
391         main_input_filename = input_filename;
392
393       jcf->cpool.data[i] = (jword) this_class;
394       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
395       return this_class;
396     }
397 }
398
399 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
400
401 tree
402 get_class_constant (JCF *jcf , int i)
403 {
404   tree type;
405   if (i <= 0 || i >= JPOOL_SIZE(jcf)
406       || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
407     fatal ("bad class index %d", i);
408
409   if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
410     {
411       int name_index = JPOOL_USHORT1 (jcf, i);
412       /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
413       char *name = JPOOL_UTF_DATA (jcf, name_index);
414       int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
415       if (name[0] == '[')  /* Handle array "classes". */
416           type = TREE_TYPE (parse_signature_string (name, nlength));
417       else
418         { 
419           tree cname = unmangle_classname (name, nlength);
420           type = lookup_class (cname);
421         }
422       jcf->cpool.data[i] = (jword) type;
423       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
424     }
425   else
426     type = (tree) jcf->cpool.data[i];
427   return type;
428 }
429
430 void
431 DEFUN(jcf_out_of_synch, (jcf),
432       JCF *jcf)
433 {
434   char *source = strdup (jcf->filename);
435   int i = strlen (source);
436
437   while (source[i] != '.')
438     i--;
439
440   source [i] = '\0';
441   warning ("Class file `%s' out of synch with `%s.java'", 
442            jcf->filename, source);
443   free (source);
444 }
445
446 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
447    called from the parser, otherwise it's a RECORD_TYPE node. If
448    VERBOSE is 1, print error message on failure to load a class. */
449
450 void
451 load_class (class_or_name, verbose)
452      tree class_or_name;
453      int verbose;
454 {
455   JCF this_jcf, *jcf;
456   tree name;
457   tree save_current_class = current_class;
458   char *save_input_filename = input_filename;
459   JCF *save_current_jcf = current_jcf;
460   long saved_pos;
461   if (current_jcf->read_state)
462     saved_pos = ftell (current_jcf->read_state);
463
464   /* class_or_name can be the name of the class we want to load */
465   if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
466     name = class_or_name;
467   /* In some cases, it's a dependency that we process earlier that
468      we though */
469   else if (TREE_CODE (class_or_name) == TREE_LIST)
470     name = TYPE_NAME (TREE_PURPOSE (class_or_name));
471   /* Or it's a type in the making */
472   else
473     name = DECL_NAME (TYPE_NAME (class_or_name));
474
475   push_obstacks (&permanent_obstack, &permanent_obstack);
476
477   /* Search in current zip first.  */
478   if (find_in_current_zip (IDENTIFIER_POINTER (name),
479                            IDENTIFIER_LENGTH (name), &jcf) == 0)
480     if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
481                      &this_jcf, 1) == 0)
482       {
483         if (verbose)
484           {
485             error ("Cannot find class file for class %s.",
486                    IDENTIFIER_POINTER (name));
487             TYPE_SIZE (class_or_name) = error_mark_node;
488 #if 0
489             /* FIXME: what to do here?  */
490             if (!strcmp (classpath, DEFAULT_CLASS_PATH))
491               fatal ("giving up");
492 #endif
493             pop_obstacks ();    /* FIXME: one pop_obstack() per function */
494           }
495         return;
496       }
497     else
498       {
499         this_jcf.seen_in_zip = 0;
500         current_jcf = &this_jcf;
501         if (this_jcf.outofsynch)
502           jcf_out_of_synch (current_jcf);
503       }
504   else
505     current_jcf = jcf;
506
507   if (current_jcf->java_source)
508     jcf_parse_source (current_jcf);
509   else {
510     java_parser_context_save_global ();
511     java_push_parser_context ();
512     input_filename = current_jcf->filename;
513     jcf_parse (current_jcf);
514     java_pop_parser_context (0);
515     java_parser_context_restore_global ();
516   }
517
518   if (!current_jcf->seen_in_zip)
519     JCF_FINISH (current_jcf);
520 /*  DECL_IGNORED_P (TYPE_NAME (class_or_name)) = 1;*/
521   pop_obstacks ();
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 }
529
530 /* Parse a source file when JCF refers to a source file.  */
531
532 void
533 jcf_parse_source (jcf)
534      JCF *jcf;
535 {
536   tree file;
537
538   java_parser_context_save_global ();
539   java_push_parser_context ();
540   input_filename = current_jcf->filename;
541   file = get_identifier (input_filename);
542   if (!HAS_BEEN_ALREADY_PARSED_P (file))
543     {
544       if (!(finput = fopen (input_filename, "r")))
545         fatal ("input file `%s' just disappeared - jcf_parse_source",
546                input_filename);
547       parse_source_file (file);
548       if (fclose (finput))
549         fatal ("can't close input file `%s' stream - jcf_parse_source",
550                input_filename);
551     }
552   java_pop_parser_context (IS_A_COMMAND_LINE_FILENAME_P (file));
553   java_parser_context_restore_global ();
554 }
555
556 /* Parse the .class file JCF. */
557
558 void
559 jcf_parse (jcf)
560      JCF* jcf;
561 {
562   int i, code;
563
564   if (jcf_parse_preamble (jcf) != 0)
565     fatal ("Not a valid Java .class file.\n");
566   code = jcf_parse_constant_pool (jcf);
567   if (code != 0)
568     fatal ("error while parsing constant pool");
569   code = verify_constant_pool (jcf);
570   if (code > 0)
571     fatal ("error in constant pool entry #%d\n", code);
572
573   jcf_parse_class (jcf);
574   if (main_class == NULL_TREE)
575     main_class = current_class;
576   if (! quiet_flag && TYPE_NAME (current_class))
577     fprintf (stderr, " class %s",
578              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
579   CLASS_LOADED_P (current_class) = 1;
580
581   for (i = 1; i < JPOOL_SIZE(jcf); i++)
582     {
583       switch (JPOOL_TAG (jcf, i))
584         {
585         case CONSTANT_Class:
586           get_class_constant (jcf, i);
587           break;
588         }
589     }
590   
591   code = jcf_parse_fields (jcf);
592   if (code != 0)
593     fatal ("error while parsing fields");
594   code = jcf_parse_methods (jcf);
595   if (code != 0)
596     fatal ("error while parsing methods");
597   code = jcf_parse_final_attributes (jcf);
598   if (code != 0)
599     fatal ("error while parsing final attributes");
600
601   /* The fields of class_type_node are already in correct order. */
602   if (current_class != class_type_node && current_class != object_type_node)
603     TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
604
605   push_obstacks (&permanent_obstack, &permanent_obstack);
606   layout_class (current_class);
607   if (current_class == object_type_node)
608     layout_class_methods (object_type_node);
609   else
610     all_class_list = tree_cons (NULL_TREE, 
611                                 TYPE_NAME (current_class), all_class_list );
612   pop_obstacks ();
613 }
614
615 void
616 init_outgoing_cpool ()
617 {
618   current_constant_pool_data_ref = NULL_TREE; 
619   if (outgoing_cpool == NULL)
620     {
621       static CPool outgoing_cpool_buffer;
622       outgoing_cpool = &outgoing_cpool_buffer;
623       CPOOL_INIT(outgoing_cpool);
624     }
625   else
626     {
627       CPOOL_REINIT(outgoing_cpool);
628     }
629 }
630
631 void
632 parse_class_file ()
633 {
634   tree method;
635   char *save_input_filename = input_filename;
636   int save_lineno = lineno;
637
638   LAYOUT_SEEN_CLASS_METHODS ();
639
640   input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
641   lineno = 0;
642   debug_start_source_file (input_filename);
643   init_outgoing_cpool ();
644
645   for ( method = TYPE_METHODS (CLASS_TO_HANDLE_TYPE (current_class));
646         method != NULL_TREE; method = TREE_CHAIN (method))
647     {
648       JCF *jcf = current_jcf;
649
650       if (METHOD_NATIVE (method) || METHOD_ABSTRACT (method))
651         continue;
652
653       if (DECL_CODE_OFFSET (method) == 0)
654         {
655           error ("missing Code attribute");
656           continue;
657         }
658
659       lineno = 0;
660       if (DECL_LINENUMBERS_OFFSET (method))
661         {
662           register int i;
663           register unsigned char *ptr;
664           JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
665           linenumber_count = i = JCF_readu2 (jcf);
666           linenumber_table = ptr = jcf->read_ptr;
667
668           for (ptr += 2; --i >= 0; ptr += 4)
669             {
670               int line = GET_u2 (ptr);
671               /* Set initial lineno lineno to smallest linenumber.
672                * Needs to be set before init_function_start. */
673               if (lineno == 0 || line < lineno)
674                 lineno = line;
675             }  
676         }
677       else
678         {
679           linenumber_table = NULL;
680           linenumber_count = 0;
681         }
682
683       start_java_method (method);
684
685       give_name_to_locals (jcf);
686
687       /* Actually generate code. */
688       expand_byte_code (jcf, method);
689
690       end_java_method ();
691     }
692
693   if (flag_emit_class_files)
694     write_classfile (current_class);
695   make_class_data (current_class);
696   register_class ();
697   rest_of_decl_compilation (TYPE_NAME (current_class), (char*) 0, 1, 0);
698
699   debug_end_source_file (save_lineno);
700   input_filename = save_input_filename;
701   lineno = save_lineno;
702 }
703
704 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
705
706 static void
707 parse_source_file (file)
708      tree file;
709 {
710   /* Mark the file as parsed */
711   HAS_BEEN_ALREADY_PARSED_P (file) = 1;
712
713   lang_init_source (1);             /* Error msgs have no method prototypes */
714   java_init_lex ();                 /* Initialize the parser */
715   java_parse_abort_on_error ();
716   java_parse ();                    /* Parse and build partial tree nodes. */
717   java_parse_abort_on_error ();
718   java_complete_class ();           /* Parse unsatisfied class decl. */
719   java_parse_abort_on_error ();
720   java_check_circular_reference (); /* Check on circular references */
721   java_parse_abort_on_error ();
722 }
723
724 int
725 yyparse ()
726 {
727   int several_files = 0;
728   char *list = strdup (input_filename), *next;
729   tree node, current_file_list = NULL_TREE;
730
731   do 
732     {
733       next = strchr (list, '&');
734       if (next)
735         {
736           *next++ = '\0';
737           several_files = 1;
738         }
739
740       if (list[0]) 
741         {
742           char *value, len;
743           extern int saw_java_source; /* FIXME: temporary.  */
744
745           len = strlen (list);
746           /* FIXME: this test is only needed until our .java parser is
747              fully capable.  */
748           if (len > 5 && ! strcmp (&list[len - 5], ".java"))
749             saw_java_source = 1;
750
751           if (*list != '/' && several_files)
752             obstack_grow (&temporary_obstack, "./", 2);
753
754           obstack_grow0 (&temporary_obstack, list, len);
755           value = obstack_finish (&temporary_obstack);
756           node = get_identifier (value);
757           IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
758           current_file_list = tree_cons (NULL_TREE, node, current_file_list);
759         }
760       list = next;
761     }
762   while (next);
763
764   current_jcf = main_jcf;
765   current_file_list = nreverse (current_file_list);
766   for (node = current_file_list; node; node = TREE_CHAIN (node))
767     {
768       tree name = TREE_VALUE (node);
769
770       /* Skip already parsed files */
771       if (HAS_BEEN_ALREADY_PARSED_P (name))
772         continue;
773       
774       /* Close previous descriptor, if any */
775       if (main_jcf->read_state && fclose (main_jcf->read_state))
776         fatal ("failed to close input file `%s' - yyparse",
777                (main_jcf->filename ? main_jcf->filename : "<unknown>"));
778       
779       /* Set jcf up and open a new file */
780       JCF_ZERO (main_jcf);
781       main_jcf->read_state = fopen (IDENTIFIER_POINTER (name), "rb");
782       if (main_jcf->read_state == NULL)
783         pfatal_with_name (IDENTIFIER_POINTER (name));
784       
785       /* Set new input_filename and finput */
786       finput = main_jcf->read_state;
787 #ifdef IO_BUFFER_SIZE
788       setvbuf (finput, (char *) xmalloc (IO_BUFFER_SIZE),
789                _IOFBF, IO_BUFFER_SIZE);
790 #endif
791       input_filename = IDENTIFIER_POINTER (name);
792       main_jcf->filbuf = jcf_filbuf_from_stdio;
793
794       switch (jcf_figure_file_type (current_jcf))
795         {
796         case JCF_ZIP:
797           parse_zip_file_entries ();
798           break;
799         case JCF_CLASS:
800           jcf_parse (current_jcf);
801           parse_class_file ();
802           break;
803         case JCF_SOURCE:
804           java_push_parser_context ();
805           java_parser_context_save_global ();
806           parse_source_file (name);
807           java_parser_context_restore_global ();
808           java_pop_parser_context (1);
809           break;
810         }
811     }
812
813   java_expand_classes ();
814   if (!java_report_errors () && !flag_emit_class_files)
815     emit_register_classes ();
816   return 0;
817 }
818
819 static struct ZipFileCache *localToFile;
820
821 /* Process all class entries found in the zip file.  */
822 void
823 parse_zip_file_entries (void)
824 {
825   struct ZipDirectory *zdir;
826   int i;
827
828   for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
829        i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
830     {
831       tree class;
832       
833       /* We don't need to consider those files.  */
834       if (!zdir->size || !zdir->filename_offset)
835         continue;
836
837       class = lookup_class (get_identifier (ZIPDIR_FILENAME (zdir)));
838       current_jcf = TYPE_LANG_SPECIFIC (class)->jcf;
839       current_class = class;
840
841       if ( !CLASS_LOADED_P (class))
842         {
843           fseek (current_jcf->read_state, current_jcf->zip_offset, SEEK_SET);
844           jcf_parse (current_jcf);
845         }
846
847       if (TYPE_SIZE (current_class) != error_mark_node)
848         {
849           input_filename = current_jcf->filename;
850           parse_class_file ();
851           FREE (current_jcf->buffer); /* No longer necessary */
852           /* Note: there is a way to free this buffer right after a
853              class seen in a zip file has been parsed. The idea is the
854              set its jcf in such a way that buffer will be reallocated
855              the time the code for the class will be generated. FIXME. */
856         }
857     }
858 }
859
860 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
861    jcf up for further processing and link it to the created class.  */
862
863 void process_zip_dir()
864 {
865   int i;
866   ZipDirectory *zdir;
867
868   for (i = 0, zdir = (ZipDirectory *)localToFile->z.central_directory;
869        i < localToFile->z.count; i++, zdir = ZIPDIR_NEXT (zdir))
870     {
871       char *class_name, *file_name, *class_name_in_zip_dir;
872       tree class;
873       JCF  *jcf;
874       int   j;
875
876       class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
877
878       /* We choose to not to process entries with a zero size or entries
879          not bearing the .class extention.  */
880       if (!zdir->size || !zdir->filename_offset ||
881           strncmp (&class_name_in_zip_dir[zdir->filename_length-6], 
882                    ".class", 6))
883         {
884           /* So it will be skipped in parse_zip_file_entries  */
885           zdir->size = 0;  
886           continue;
887         }
888
889       class_name = ALLOC (zdir->filename_length+1-6);
890       file_name  = ALLOC (zdir->filename_length+1);
891       jcf = ALLOC (sizeof (JCF));
892       JCF_ZERO (jcf);
893
894       strncpy (class_name, class_name_in_zip_dir, zdir->filename_length-6);
895       class_name [zdir->filename_length-6] = '\0';
896       strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
897       file_name [zdir->filename_length] = '\0';
898
899       for (j=0; class_name[j]; j++)
900         class_name [j] = (class_name [j] == '/' ? '.' : class_name [j]);
901
902       /* Yes, we write back the true class name into the zip directory.  */
903       strcpy (class_name_in_zip_dir, class_name);
904       zdir->filename_length = j;
905       class = lookup_class (get_identifier (class_name));
906
907       jcf->read_state  = finput;
908       jcf->filbuf      = jcf_filbuf_from_stdio;
909       jcf->seen_in_zip = 1;
910       jcf->java_source = 0;
911       jcf->zip_offset  = zdir->filestart;
912       jcf->classname   = class_name;
913       jcf->filename    = file_name;
914
915       TYPE_LANG_SPECIFIC (class) = 
916         (struct lang_type *) perm_calloc (1, sizeof (struct lang_type));
917       TYPE_LANG_SPECIFIC (class)->jcf = jcf;
918     }
919 }
920
921 /* Lookup class NAME and figure whether is a class already found in the current
922    zip file.  */
923 int
924 DEFUN(find_in_current_zip, (name, length, jcf),
925       char *name AND int length AND JCF **jcf)
926 {
927   JCF *local_jcf;
928   tree class_name = maybe_get_identifier (name), class, icv;
929
930   if (!class_name)
931     return 0;
932
933   if (!(icv = IDENTIFIER_CLASS_VALUE (class_name)))
934     return 0;
935
936   class = TREE_TYPE (icv);
937
938   /* Doesn't have jcf specific info ? It's not ours */
939   if (!TYPE_LANG_SPECIFIC (class) || !TYPE_LANG_SPECIFIC (class)->jcf)
940     return 0;
941
942   *jcf = local_jcf = TYPE_LANG_SPECIFIC (class)->jcf;
943   fseek (local_jcf->read_state, local_jcf->zip_offset, SEEK_SET);
944   return 1;
945 }
946
947 /* Figure what kind of file we're dealing with */
948 int
949 DEFUN(jcf_figure_file_type, (jcf),
950       JCF *jcf)
951 {
952   unsigned char magic_string[4];
953   uint32 magic;
954
955   if (fread (magic_string, 1, 4, jcf->read_state) != 4)
956     jcf_unexpected_eof (jcf, 4);
957
958   fseek (jcf->read_state, 0L, SEEK_SET);
959   magic = GET_u4 (magic_string);
960
961   if (magic == 0xcafebabe)
962     return JCF_CLASS;
963
964   /* FIXME: is it a system file?  */
965   if (!open_in_zip (jcf, input_filename, NULL, 0))
966     {
967       localToFile = ALLOC (sizeof (struct ZipFileCache));
968       bcopy (SeenZipFiles, localToFile, sizeof (struct ZipFileCache));
969       process_zip_dir ();       /* Register all the class defined there */
970       return JCF_ZIP;
971     }
972
973   return JCF_SOURCE;
974 }
975