OSDN Git Service

* ggc-common.c (ggc_rlimit_bound): Don't check RSS limit.
[pf3gnuchains/gcc-fork.git] / gcc / java / jcf-parse.c
1 /* Parser for Java(TM) .class files.
2    Copyright (C) 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
25
26 /* Written by Per Bothner <bothner@cygnus.com> */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "real.h"
34 #include "obstack.h"
35 #include "flags.h"
36 #include "java-except.h"
37 #include "input.h"
38 #include "java-tree.h"
39 #include "toplev.h"
40 #include "parse.h"
41 #include "ggc.h"
42 #include "debug.h"
43 #include "assert.h"
44 #include "tm_p.h"
45 #include "cgraph.h"
46
47 #ifdef HAVE_LOCALE_H
48 #include <locale.h>
49 #endif
50
51 #ifdef HAVE_LANGINFO_CODESET
52 #include <langinfo.h>
53 #endif
54
55 /* A CONSTANT_Utf8 element is converted to an IDENTIFIER_NODE at parse time. */
56 #define JPOOL_UTF(JCF, INDEX) CPOOL_UTF(&(JCF)->cpool, INDEX)
57 #define JPOOL_UTF_LENGTH(JCF, INDEX) IDENTIFIER_LENGTH (JPOOL_UTF (JCF, INDEX))
58 #define JPOOL_UTF_DATA(JCF, INDEX) \
59   ((const unsigned char *) IDENTIFIER_POINTER (JPOOL_UTF (JCF, INDEX)))
60 #define HANDLE_CONSTANT_Utf8(JCF, INDEX, LENGTH) \
61   do { \
62     unsigned char save;  unsigned char *text; \
63     JCF_FILL (JCF, (LENGTH)+1); /* Make sure we read 1 byte beyond string. */ \
64     text = (JCF)->read_ptr; \
65     save = text[LENGTH]; \
66     text[LENGTH] = 0; \
67     (JCF)->cpool.data[INDEX].t = get_identifier ((const char *) text); \
68     text[LENGTH] = save; \
69     JCF_SKIP (JCF, LENGTH); } while (0)
70
71 #include "jcf.h"
72
73 extern struct obstack temporary_obstack;
74
75 static GTY(()) tree parse_roots[3];
76
77 /* The FIELD_DECL for the current field.  */
78 #define current_field parse_roots[0]
79
80 /* The METHOD_DECL for the current method.  */
81 #define current_method parse_roots[1]
82
83 /* A list of file names.  */
84 #define current_file_list parse_roots[2]
85
86 /* The Java archive that provides main_class;  the main input file. */
87 static GTY(()) struct JCF * main_jcf;
88
89 static struct ZipFile *localToFile;
90
91 /* Declarations of some functions used here.  */
92 static void handle_innerclass_attribute (int count, JCF *);
93 static tree give_name_to_class (JCF *jcf, int index);
94 static char *compute_class_name (struct ZipDirectory *zdir);
95 static int classify_zip_file (struct ZipDirectory *zdir);
96 static void parse_zip_file_entries (void);
97 static void process_zip_dir (FILE *);
98 static void parse_source_file_1 (tree, FILE *);
99 static void parse_source_file_2 (void);
100 static void parse_source_file_3 (void);
101 static void parse_class_file (void);
102 static void handle_deprecated (void);
103 static void set_source_filename (JCF *, int);
104 static void jcf_parse (struct JCF*);
105 static void load_inner_classes (tree);
106
107 /* Handle "Deprecated" attribute.  */
108 static void
109 handle_deprecated (void)
110 {
111   if (current_field != NULL_TREE)
112     FIELD_DEPRECATED (current_field) = 1;
113   else if (current_method != NULL_TREE)
114     METHOD_DEPRECATED (current_method) = 1;
115   else if (current_class != NULL_TREE)
116     CLASS_DEPRECATED (TYPE_NAME (current_class)) = 1;
117   else
118     {
119       /* Shouldn't happen.  */
120       abort ();
121     }
122 }
123
124 /* Handle "SourceFile" attribute. */
125
126 static void
127 set_source_filename (JCF *jcf, int index)
128 {
129   tree sfname_id = get_name_constant (jcf, index);
130   const char *sfname = IDENTIFIER_POINTER (sfname_id);
131   if (input_filename != NULL)
132     {
133       int old_len = strlen (input_filename);
134       int new_len = IDENTIFIER_LENGTH (sfname_id);
135       /* Use the current input_filename (derived from the class name)
136          if it has a directory prefix, but otherwise matches sfname. */
137       if (old_len > new_len
138           && strcmp (sfname, input_filename + old_len - new_len) == 0
139           && (input_filename[old_len - new_len - 1] == '/'
140               || input_filename[old_len - new_len - 1] == '\\'))
141         return;
142     }
143   input_filename = sfname;
144   DECL_SOURCE_FILE (TYPE_NAME (current_class)) = sfname;
145   if (current_class == main_class) main_input_filename = input_filename;
146 }
147
148 #define HANDLE_SOURCEFILE(INDEX) set_source_filename (jcf, INDEX)
149
150 #define HANDLE_CLASS_INFO(ACCESS_FLAGS, THIS, SUPER, INTERFACES_COUNT) \
151 { tree super_class = SUPER==0 ? NULL_TREE : get_class_constant (jcf, SUPER); \
152   output_class = current_class = give_name_to_class (jcf, THIS); \
153   set_super_info (ACCESS_FLAGS, current_class, super_class, INTERFACES_COUNT);}
154
155 #define HANDLE_CLASS_INTERFACE(INDEX) \
156   add_interface (current_class, get_class_constant (jcf, INDEX))
157
158 #define HANDLE_START_FIELD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
159 { int sig_index = SIGNATURE; \
160   current_field = add_field (current_class, get_name_constant (jcf, NAME), \
161                              parse_signature (jcf, sig_index), ACCESS_FLAGS); \
162  set_java_signature (TREE_TYPE (current_field), JPOOL_UTF (jcf, sig_index)); \
163  if ((ACCESS_FLAGS) & ACC_FINAL) \
164    MAYBE_CREATE_VAR_LANG_DECL_SPECIFIC (current_field); \
165 }
166
167 #define HANDLE_END_FIELDS() \
168   (current_field = NULL_TREE)
169
170 #define HANDLE_CONSTANTVALUE(INDEX) \
171 { tree constant;  int index = INDEX; \
172   if (! flag_emit_class_files && JPOOL_TAG (jcf, index) == CONSTANT_String) { \
173     tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index)); \
174     constant = build_utf8_ref (name); \
175   } \
176   else \
177     constant = get_constant (jcf, index); \
178   set_constant_value (current_field, constant); }
179
180 #define HANDLE_METHOD(ACCESS_FLAGS, NAME, SIGNATURE, ATTRIBUTE_COUNT) \
181  (current_method = add_method (current_class, ACCESS_FLAGS, \
182                                get_name_constant (jcf, NAME), \
183                                get_name_constant (jcf, SIGNATURE)), \
184   DECL_LOCALVARIABLES_OFFSET (current_method) = 0, \
185   DECL_LINENUMBERS_OFFSET (current_method) = 0)
186
187 #define HANDLE_END_METHODS() \
188 { current_method = NULL_TREE; }
189
190 #define HANDLE_CODE_ATTRIBUTE(MAX_STACK, MAX_LOCALS, CODE_LENGTH) \
191 { DECL_MAX_STACK (current_method) = (MAX_STACK); \
192   DECL_MAX_LOCALS (current_method) = (MAX_LOCALS); \
193   DECL_CODE_LENGTH (current_method) = (CODE_LENGTH); \
194   DECL_CODE_OFFSET (current_method) = JCF_TELL (jcf); }
195
196 #define HANDLE_LOCALVARIABLETABLE_ATTRIBUTE(COUNT) \
197 { int n = (COUNT); \
198   DECL_LOCALVARIABLES_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
199   JCF_SKIP (jcf, n * 10); }
200
201 #define HANDLE_LINENUMBERTABLE_ATTRIBUTE(COUNT) \
202 { int n = (COUNT); \
203   DECL_LINENUMBERS_OFFSET (current_method) = JCF_TELL (jcf) - 2; \
204   JCF_SKIP (jcf, n * 4); }
205
206 #define HANDLE_EXCEPTIONS_ATTRIBUTE(COUNT) \
207 { \
208   int n = COUNT; \
209   tree list = DECL_FUNCTION_THROWS (current_method); \
210   while (--n >= 0) \
211     { \
212       tree thrown_class = get_class_constant (jcf, JCF_readu2 (jcf)); \
213       list = tree_cons (NULL_TREE, thrown_class, list); \
214     } \
215   DECL_FUNCTION_THROWS (current_method) = nreverse (list); \
216 }
217
218 #define HANDLE_DEPRECATED_ATTRIBUTE()  handle_deprecated ()
219
220 /* Link seen inner classes to their outer context and register the
221    inner class to its outer context. They will be later loaded.  */
222 #define HANDLE_INNERCLASSES_ATTRIBUTE(COUNT) \
223   handle_innerclass_attribute (COUNT, jcf)
224
225 #define HANDLE_SYNTHETIC_ATTRIBUTE()                                    \
226 {                                                                       \
227   /* Irrelevant decls should have been nullified by the END macros.     \
228      We only handle the `Synthetic' attribute on method DECLs.          \
229      DECL_ARTIFICIAL on fields is used for something else (See          \
230      PUSH_FIELD in java-tree.h) */                                      \
231   if (current_method)                                                   \
232     DECL_ARTIFICIAL (current_method) = 1;                               \
233 }
234
235 #define HANDLE_GCJCOMPILED_ATTRIBUTE()          \
236 {                                               \
237   if (current_class == object_type_node)        \
238     jcf->right_zip = 1;                         \
239 }
240
241 #include "jcf-reader.c"
242
243 tree
244 parse_signature (JCF *jcf, int sig_index)
245 {
246   if (sig_index <= 0 || sig_index >= JPOOL_SIZE (jcf)
247       || JPOOL_TAG (jcf, sig_index) != CONSTANT_Utf8)
248     abort ();
249   else
250     return parse_signature_string (JPOOL_UTF_DATA (jcf, sig_index),
251                                    JPOOL_UTF_LENGTH (jcf, sig_index));
252 }
253
254 tree
255 get_constant (JCF *jcf, int index)
256 {
257   tree value;
258   int tag;
259   if (index <= 0 || index >= JPOOL_SIZE(jcf))
260     goto bad;
261   tag = JPOOL_TAG (jcf, index);
262   if ((tag & CONSTANT_ResolvedFlag) || tag == CONSTANT_Utf8)
263     return jcf->cpool.data[index].t;
264   switch (tag)
265     {
266     case CONSTANT_Integer:
267       {
268         jint num = JPOOL_INT(jcf, index);
269         value = build_int_2 (num, num < 0 ? -1 : 0);
270         TREE_TYPE (value) = int_type_node;
271         break;
272       }
273     case CONSTANT_Long:
274       {
275         unsigned HOST_WIDE_INT num = JPOOL_UINT (jcf, index);
276         unsigned HOST_WIDE_INT lo;
277         HOST_WIDE_INT hi;
278         
279         lshift_double (num, 0, 32, 64, &lo, &hi, 0);
280         num = JPOOL_UINT (jcf, index+1);
281         add_double (lo, hi, num, 0, &lo, &hi);
282         value = build_int_2 (lo, hi);
283         TREE_TYPE (value) = long_type_node;
284         value = force_fit_type (value, 0, false, false);
285         break;
286       }
287
288     case CONSTANT_Float:
289       {
290         jint num = JPOOL_INT(jcf, index);
291         long buf = num;
292         REAL_VALUE_TYPE d;
293
294         real_from_target_fmt (&d, &buf, &ieee_single_format);
295         value = build_real (float_type_node, d);
296         break;
297       }
298
299     case CONSTANT_Double:
300       {
301         long buf[2], lo, hi;
302         REAL_VALUE_TYPE d;
303
304         hi = JPOOL_UINT (jcf, index);
305         lo = JPOOL_UINT (jcf, index+1);
306
307         if (FLOAT_WORDS_BIG_ENDIAN)
308           buf[0] = hi, buf[1] = lo;
309         else
310           buf[0] = lo, buf[1] = hi;
311
312         real_from_target_fmt (&d, buf, &ieee_double_format);
313         value = build_real (double_type_node, d);
314         break;
315       }
316
317     case CONSTANT_String:
318       {
319         tree name = get_name_constant (jcf, JPOOL_USHORT1 (jcf, index));
320         const char *utf8_ptr = IDENTIFIER_POINTER (name);
321         int utf8_len = IDENTIFIER_LENGTH (name);
322         const unsigned char *utf8;
323         int i;
324
325         /* Check for a malformed Utf8 string.  */
326         utf8 = (const unsigned char *) utf8_ptr;
327         i = utf8_len;
328         while (i > 0)
329           {
330             int char_len = UT8_CHAR_LENGTH (*utf8);
331             if (char_len < 0 || char_len > 3 || char_len > i)
332               fatal_error ("bad string constant");
333
334             utf8 += char_len;
335             i -= char_len;
336           }
337
338         /* Allocate a new string value.  */
339         value = build_string (utf8_len, utf8_ptr);
340         TREE_TYPE (value) = build_pointer_type (string_type_node);
341       }
342       break;
343     default:
344       goto bad;
345     }
346   JPOOL_TAG (jcf, index) = tag | CONSTANT_ResolvedFlag;
347   jcf->cpool.data[index].t = value;
348   return value;
349  bad:
350   internal_error ("bad value constant type %d, index %d", 
351                   JPOOL_TAG (jcf, index), index);
352 }
353
354 tree
355 get_name_constant (JCF *jcf, int index)
356 {
357   tree name = get_constant (jcf, index);
358
359   if (TREE_CODE (name) != IDENTIFIER_NODE)
360     abort ();
361
362   return name;
363 }
364
365 /* Handle reading innerclass attributes. If a nonzero entry (denoting
366    a non anonymous entry) is found, We augment the inner class list of
367    the outer context with the newly resolved innerclass.  */
368
369 static void
370 handle_innerclass_attribute (int count, JCF *jcf)
371 {
372   int c = (count);
373   while (c--)
374     {
375       /* Read inner_class_info_index. This may be 0 */
376       int icii = JCF_readu2 (jcf);
377       /* Read outer_class_info_index. If the innerclasses attribute
378          entry isn't a member (like an inner class) the value is 0. */
379       int ocii = JCF_readu2 (jcf);
380       /* Read inner_name_index. If the class we're dealing with is
381          an anonymous class, it must be 0. */
382       int ini = JCF_readu2 (jcf);
383       /* Read the access flag. */
384       int acc = JCF_readu2 (jcf);
385       /* If icii is 0, don't try to read the class. */
386       if (icii >= 0)
387         {
388           tree class = get_class_constant (jcf, icii);
389           tree decl = TYPE_NAME (class);
390           /* Skip reading further if ocii is null */
391           if (DECL_P (decl) && !CLASS_COMPLETE_P (decl) && ocii)
392             {
393               tree outer = TYPE_NAME (get_class_constant (jcf, ocii));
394               tree alias = (ini ? get_name_constant (jcf, ini) : NULL_TREE);
395               set_class_decl_access_flags (acc, decl);
396               DECL_CONTEXT (decl) = outer;
397               DECL_INNER_CLASS_LIST (outer) =
398                 tree_cons (decl, alias, DECL_INNER_CLASS_LIST (outer));
399               CLASS_COMPLETE_P (decl) = 1;
400             }
401         }
402     }
403 }
404
405 static tree
406 give_name_to_class (JCF *jcf, int i)
407 {
408   if (i <= 0 || i >= JPOOL_SIZE (jcf)
409       || JPOOL_TAG (jcf, i) != CONSTANT_Class)
410     abort ();
411   else
412     {
413       tree this_class;
414       int j = JPOOL_USHORT1 (jcf, i);
415       /* verify_constant_pool confirmed that j is a CONSTANT_Utf8. */
416       tree class_name = unmangle_classname ((const char *) JPOOL_UTF_DATA (jcf, j),
417                                             JPOOL_UTF_LENGTH (jcf, j));
418       this_class = lookup_class (class_name);
419       input_filename = DECL_SOURCE_FILE (TYPE_NAME (this_class));
420       input_line = 0;
421       if (main_input_filename == NULL && jcf == main_jcf)
422         main_input_filename = input_filename;
423
424       jcf->cpool.data[i].t = this_class;
425       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
426       return this_class;
427     }
428 }
429
430 /* Get the class of the CONSTANT_Class whose constant pool index is I. */
431
432 tree
433 get_class_constant (JCF *jcf, int i)
434 {
435   tree type;
436   if (i <= 0 || i >= JPOOL_SIZE (jcf)
437       || (JPOOL_TAG (jcf, i) & ~CONSTANT_ResolvedFlag) != CONSTANT_Class)
438     abort ();
439
440   if (JPOOL_TAG (jcf, i) != CONSTANT_ResolvedClass)
441     {
442       int name_index = JPOOL_USHORT1 (jcf, i);
443       /* verify_constant_pool confirmed that name_index is a CONSTANT_Utf8. */
444       const char *name = (const char *) JPOOL_UTF_DATA (jcf, name_index);
445       int nlength = JPOOL_UTF_LENGTH (jcf, name_index);
446
447       if (name[0] == '[')  /* Handle array "classes". */
448           type = TREE_TYPE (parse_signature_string ((const unsigned char *) name, nlength));
449       else
450         { 
451           tree cname = unmangle_classname (name, nlength);
452           type = lookup_class (cname);
453         }
454       jcf->cpool.data[i].t = type;
455       JPOOL_TAG (jcf, i) = CONSTANT_ResolvedClass;
456     }
457   else
458     type = jcf->cpool.data[i].t;
459   return type;
460 }
461
462 /* Read a class with the fully qualified-name NAME.
463    Return 1 iff we read the requested file.
464    (It is still possible we failed if the file did not
465    define the class it is supposed to.) */
466
467 int
468 read_class (tree name)
469 {
470   JCF this_jcf, *jcf;
471   tree icv, class = NULL_TREE;
472   tree save_current_class = current_class;
473   tree save_output_class = output_class;
474   location_t save_location = input_location;
475   JCF *save_current_jcf = current_jcf;
476
477   if ((icv = IDENTIFIER_CLASS_VALUE (name)) != NULL_TREE)
478     {
479       class = TREE_TYPE (icv);
480       jcf = TYPE_JCF (class);
481     }
482   else
483     jcf = NULL;
484
485   if (jcf == NULL)
486     {
487       this_jcf.zipd = NULL;
488       jcf = &this_jcf;
489       if (find_class (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name),
490                       &this_jcf, 1) == 0)
491         return 0;
492     }
493
494   current_jcf = jcf;
495
496   if (current_jcf->java_source)
497     {
498       const char *filename = current_jcf->filename;
499       tree given_file, real_file;
500       FILE *finput;
501       int generate;
502
503       java_parser_context_save_global ();
504       java_push_parser_context ();
505
506       given_file = get_identifier (filename);
507       real_file = get_identifier (lrealpath (filename));
508
509       generate = IS_A_COMMAND_LINE_FILENAME_P (given_file);
510       if (wfl_operator == NULL_TREE)
511         wfl_operator = build_expr_wfl (NULL_TREE, NULL, 0, 0);
512       EXPR_WFL_FILENAME_NODE (wfl_operator) = given_file;
513       input_filename = ggc_strdup (filename);
514       output_class = current_class = NULL_TREE;
515       current_function_decl = NULL_TREE;
516
517       if (! HAS_BEEN_ALREADY_PARSED_P (real_file))
518         {
519           if (! (finput = fopen (input_filename, "r")))
520             fatal_error ("can't reopen %s: %m", input_filename);
521
522           parse_source_file_1 (real_file, finput);
523           parse_source_file_2 ();
524           parse_source_file_3 ();
525
526           if (fclose (finput))
527             fatal_error ("can't close %s: %m", input_filename);
528         }
529       JCF_FINISH (current_jcf);
530       java_pop_parser_context (generate);
531       java_parser_context_restore_global ();
532     }
533   else
534     {
535       if (class == NULL_TREE || ! CLASS_PARSED_P (class))
536         {
537           java_parser_context_save_global ();
538           java_push_parser_context ();
539           output_class = current_class = class;
540           input_filename = current_jcf->filename;
541           if (JCF_SEEN_IN_ZIP (current_jcf))
542             read_zip_member(current_jcf,
543                             current_jcf->zipd, current_jcf->zipd->zipf);
544           jcf_parse (current_jcf);
545           /* Parsing might change the class, in which case we have to
546              put it back where we found it.  */
547           if (current_class != class && icv != NULL_TREE)
548             TREE_TYPE (icv) = current_class;
549           class = current_class;
550           java_pop_parser_context (0);
551           java_parser_context_restore_global ();
552         }
553       layout_class (class);
554       load_inner_classes (class);
555     }
556
557   output_class = save_output_class;
558   current_class = save_current_class;
559   input_location = save_location;
560   current_jcf = save_current_jcf;
561   return 1;
562 }
563
564 /* Load CLASS_OR_NAME. CLASS_OR_NAME can be a mere identifier if
565    called from the parser, otherwise it's a RECORD_TYPE node. If
566    VERBOSE is 1, print error message on failure to load a class. */
567 void
568 load_class (tree class_or_name, int verbose)
569 {
570   tree name, saved;
571   int class_loaded;
572
573   /* class_or_name can be the name of the class we want to load */
574   if (TREE_CODE (class_or_name) == IDENTIFIER_NODE)
575     name = class_or_name;
576   /* In some cases, it's a dependency that we process earlier that
577      we though */
578   else if (TREE_CODE (class_or_name) == TREE_LIST)
579     name = TYPE_NAME (TREE_PURPOSE (class_or_name));
580   /* Or it's a type in the making */
581   else
582     {
583       /* If the class is from source code, then it must already be loaded.  */
584       if (CLASS_FROM_SOURCE_P (class_or_name))
585         return;
586       name = DECL_NAME (TYPE_NAME (class_or_name));
587     }
588
589   saved = name;
590   while (1)
591     {
592       char *separator;
593
594       if ((class_loaded = read_class (name)))
595         break;
596
597       /* We failed loading name. Now consider that we might be looking
598          for a inner class. */
599       if ((separator = strrchr (IDENTIFIER_POINTER (name), '$'))
600           || (separator = strrchr (IDENTIFIER_POINTER (name), '.')))
601         {
602           int c = *separator;
603           *separator = '\0';
604           name = get_identifier (IDENTIFIER_POINTER (name));
605           *separator = c;
606         }
607       /* Otherwise, we failed, we bail. */
608       else
609         break;
610     }
611
612   if (!class_loaded && verbose)
613     error ("cannot find file for class %s", IDENTIFIER_POINTER (saved));
614 }
615
616 /* Parse the .class file JCF. */
617
618 static void
619 jcf_parse (JCF* jcf)
620 {
621   int i, code;
622
623   if (jcf_parse_preamble (jcf) != 0)
624     fatal_error ("not a valid Java .class file");
625   code = jcf_parse_constant_pool (jcf);
626   if (code != 0)
627     fatal_error ("error while parsing constant pool");
628   code = verify_constant_pool (jcf);
629   if (code > 0)
630     fatal_error ("error in constant pool entry #%d\n", code);
631
632   jcf_parse_class (jcf);
633   if (main_class == NULL_TREE)
634     main_class = current_class;
635   if (! quiet_flag && TYPE_NAME (current_class))
636     fprintf (stderr, " %s %s",
637              (jcf->access_flags & ACC_INTERFACE) ? "interface" : "class", 
638              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))));
639   if (CLASS_PARSED_P (current_class))
640     {
641       /* FIXME - where was first time */
642       fatal_error ("reading class %s for the second time from %s",
643                    IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (current_class))),
644                    jcf->filename);
645     }
646   CLASS_PARSED_P (current_class) = 1;
647
648   for (i = 1; i < JPOOL_SIZE(jcf); i++)
649     {
650       switch (JPOOL_TAG (jcf, i))
651         {
652         case CONSTANT_Class:
653           get_class_constant (jcf, i);
654           break;
655         }
656     }
657   
658   code = jcf_parse_fields (jcf);
659   if (code != 0)
660     fatal_error ("error while parsing fields");
661   code = jcf_parse_methods (jcf);
662   if (code != 0)
663     fatal_error ("error while parsing methods");
664   code = jcf_parse_final_attributes (jcf);
665   if (code != 0)
666     fatal_error ("error while parsing final attributes");
667
668   /* The fields of class_type_node are already in correct order. */
669   if (current_class != class_type_node && current_class != object_type_node)
670     TYPE_FIELDS (current_class) = nreverse (TYPE_FIELDS (current_class));
671
672   if (current_class == object_type_node)
673     {
674       layout_class_methods (object_type_node);
675       /* If we don't have the right archive, emit a verbose warning.
676          If we're generating bytecode, emit the warning only if
677          -fforce-classes-archive-check was specified. */
678       if (!jcf->right_zip
679           && (!flag_emit_class_files || flag_force_classes_archive_check))
680         fatal_error ("the `java.lang.Object' that was found in `%s' didn't have the special zero-length `gnu.gcj.gcj-compiled' attribute.  This generally means that your classpath is incorrectly set.  Use `info gcj \"Input Options\"' to see the info page describing how to set the classpath", jcf->filename);
681     }
682   else
683     all_class_list = tree_cons (NULL_TREE,
684                                 TYPE_NAME (current_class), all_class_list );
685 }
686
687 /* If we came across inner classes, load them now. */
688 static void
689 load_inner_classes (tree cur_class)
690 {
691   tree current;
692   for (current = DECL_INNER_CLASS_LIST (TYPE_NAME (cur_class)); current;
693        current = TREE_CHAIN (current))
694     {
695       tree name = DECL_NAME (TREE_PURPOSE (current));
696       tree decl = IDENTIFIER_GLOBAL_VALUE (name);
697       if (decl && ! CLASS_LOADED_P (TREE_TYPE (decl))
698           && !CLASS_BEING_LAIDOUT (TREE_TYPE (decl)))
699         load_class (name, 1);
700     }
701 }
702
703 static void
704 parse_class_file (void)
705 {
706   tree method;
707   location_t save_location = input_location;
708
709   java_layout_seen_class_methods ();
710
711   input_filename = DECL_SOURCE_FILE (TYPE_NAME (current_class));
712   input_line = 0;
713   (*debug_hooks->start_source_file) (input_line, input_filename);
714
715   /* Currently we always have to emit calls to _Jv_InitClass when
716      compiling from class files.  */
717   always_initialize_class_p = 1;
718
719   gen_indirect_dispatch_tables (current_class);
720
721   java_mark_class_local (current_class);
722
723   for (method = TYPE_METHODS (current_class);
724        method != NULL_TREE; method = TREE_CHAIN (method))
725     {
726       JCF *jcf = current_jcf;
727
728       if (METHOD_ABSTRACT (method))
729         continue;
730
731       if (METHOD_NATIVE (method))
732         {
733           tree arg;
734           int  decl_max_locals;
735
736           if (! flag_jni)
737             continue;
738           /* We need to compute the DECL_MAX_LOCALS. We need to take
739              the wide types into account too. */
740           for (arg = TYPE_ARG_TYPES (TREE_TYPE (method)), decl_max_locals = 0; 
741                arg != end_params_node;
742                arg = TREE_CHAIN (arg), decl_max_locals += 1)
743             {
744               if (TREE_VALUE (arg) && TYPE_IS_WIDE (TREE_VALUE (arg)))
745                 decl_max_locals += 1;
746             }
747           DECL_MAX_LOCALS (method) = decl_max_locals;
748           start_java_method (method);
749           give_name_to_locals (jcf);
750           *get_stmts () = build_jni_stub (method);
751           end_java_method ();
752           continue;
753         }
754
755       if (DECL_CODE_OFFSET (method) == 0)
756         {
757           current_function_decl = method;
758           error ("missing Code attribute");
759           continue;
760         }
761
762       input_line = 0;
763       if (DECL_LINENUMBERS_OFFSET (method))
764         {
765           int i;
766           unsigned char *ptr;
767           JCF_SEEK (jcf, DECL_LINENUMBERS_OFFSET (method));
768           linenumber_count = i = JCF_readu2 (jcf);
769           linenumber_table = ptr = jcf->read_ptr;
770
771           for (ptr += 2; --i >= 0; ptr += 4)
772             {
773               int line = GET_u2 (ptr);
774               /* Set initial input_line to smallest linenumber.
775                * Needs to be set before init_function_start. */
776               if (input_line == 0 || line < input_line)
777                 input_line = line;
778             }  
779         }
780       else
781         {
782           linenumber_table = NULL;
783           linenumber_count = 0;
784         }
785
786       start_java_method (method);
787
788       note_instructions (jcf, method);
789
790       give_name_to_locals (jcf);
791
792       /* Convert bytecode to trees.  */
793       expand_byte_code (jcf, method);
794
795       end_java_method ();
796     }
797
798   if (flag_emit_class_files)
799     write_classfile (current_class);
800
801   finish_class ();
802
803   (*debug_hooks->end_source_file) (save_location.line);
804   input_location = save_location;
805 }
806
807 /* Parse a source file, as pointed by the current value of INPUT_FILENAME. */
808
809 static void
810 parse_source_file_1 (tree real_file, FILE *finput)
811 {
812   int save_error_count = java_error_count;
813
814   /* Mark the file as parsed.  */
815   HAS_BEEN_ALREADY_PARSED_P (real_file) = 1;
816
817   jcf_dependency_add_file (input_filename, 0);
818
819   lang_init_source (1);             /* Error msgs have no method prototypes */
820
821   /* There's no point in trying to find the current encoding unless we
822      are going to do something intelligent with it -- hence the test
823      for iconv.  */
824 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
825   setlocale (LC_CTYPE, "");
826   if (current_encoding == NULL)
827     current_encoding = nl_langinfo (CODESET);
828 #endif 
829   if (current_encoding == NULL || *current_encoding == '\0')
830     current_encoding = DEFAULT_ENCODING;
831
832   /* Initialize the parser */
833   java_init_lex (finput, current_encoding);
834   java_parse_abort_on_error ();
835
836   java_parse ();                    /* Parse and build partial tree nodes. */
837   java_parse_abort_on_error ();
838 }
839
840 /* Process a parsed source file, resolving names etc. */
841
842 static void
843 parse_source_file_2 (void)
844 {
845   int save_error_count = java_error_count;
846   java_complete_class ();           /* Parse unsatisfied class decl. */
847   java_parse_abort_on_error ();
848 }
849
850 static void
851 parse_source_file_3 (void)
852 {
853   int save_error_count = java_error_count;
854   java_check_circular_reference (); /* Check on circular references */
855   java_parse_abort_on_error ();
856   java_fix_constructors ();         /* Fix the constructors */
857   java_parse_abort_on_error ();
858   java_reorder_fields ();           /* Reorder the fields */
859 }
860
861 void
862 add_predefined_file (tree name)
863 {
864   predef_filenames = tree_cons (NULL_TREE, name, predef_filenames);
865 }
866
867 int
868 predefined_filename_p (tree node)
869 {
870   tree iter;
871
872   for (iter = predef_filenames; iter != NULL_TREE; iter = TREE_CHAIN (iter))
873     {
874       if (TREE_VALUE (iter) == node)
875         return 1;
876     }
877   return 0;
878 }
879
880 /* Generate a function that does all static initialization for this 
881    translation unit.  */
882
883 static void
884 java_emit_static_constructor (void)
885 {
886   tree body = NULL;
887
888   emit_register_classes (&body);
889   write_resource_constructor (&body);
890
891   if (body)
892     cgraph_build_static_cdtor ('I', body, DEFAULT_INIT_PRIORITY);
893 }
894
895 void
896 java_parse_file (int set_yydebug ATTRIBUTE_UNUSED)
897 {
898   int filename_count = 0;
899   char *list, *next;
900   tree node;
901   FILE *finput = NULL;
902
903   if (flag_filelist_file)
904     {
905       int avail = 2000;
906       finput = fopen (input_filename, "r");
907       if (finput == NULL)
908         fatal_error ("can't open %s: %m", input_filename);
909       list = xmalloc(avail);
910       next = list;
911       for (;;)
912         {
913           int count;
914           if (avail < 500)
915             {
916               count = next - list;
917               avail = 2 * (count + avail);
918               list = xrealloc (list, avail);
919               next = list + count;
920               avail = avail - count;
921             }
922           /* Subtract to to guarantee space for final '\0'. */
923           count = fread (next, 1, avail - 1, finput);
924           if (count == 0)
925             {
926               if (! feof (finput))
927                 fatal_error ("error closing %s: %m", input_filename);
928               *next = '\0';
929               break;
930             }
931           avail -= count;
932           next += count;
933         }
934       fclose (finput);
935       finput = NULL;
936     }
937   else
938     list = input_filename ? xstrdup (input_filename) : 0;
939
940   while (list)
941     {
942       for (next = list; ; )
943         {
944           char ch = *next;
945           if (ch == '\n' || ch == '\r' || ch == '\t' || ch == ' '
946               || ch == '&' /* FIXME */)
947             {
948               if (next == list)
949                 {
950                   next++;
951                   list = next;
952                   continue;
953                 }
954               else
955                 {
956                   *next++ = '\0';
957                   break;
958                 }
959             }
960           if (ch == '\0')
961             {
962               next = NULL;
963               break;
964             }
965           next++;
966         }
967
968       if (list[0]) 
969         {
970           char *value;
971           tree id;
972           int twice = 0;
973
974           int len = strlen (list);
975
976           obstack_grow0 (&temporary_obstack, list, len);
977           value = obstack_finish (&temporary_obstack);
978
979           filename_count++;
980
981           /* Exclude file that we see twice on the command line. For
982              all files except {Class,Error,Object,RuntimeException,String,
983              Throwable}.java we can rely on maybe_get_identifier. For
984              these files, we need to do a linear search of
985              current_file_list. This search happens only for these
986              files, presumably only when we're recompiling libgcj. */
987              
988           if ((id = maybe_get_identifier (value)))
989             {
990               if (predefined_filename_p (id))
991                 {
992                   tree c;
993                   for (c = current_file_list; c; c = TREE_CHAIN (c))
994                     if (TREE_VALUE (c) == id)
995                       twice = 1;
996                 }
997               else
998                 twice = 1;
999             }
1000
1001           if (twice)
1002             {
1003               location_t warn_loc;
1004               warn_loc.file = value;
1005               warn_loc.line = 0;
1006               warning ("%Hsource file seen twice on command line and "
1007                        "will be compiled only once", &warn_loc);
1008             }
1009           else
1010             {
1011               node = get_identifier (value);
1012               IS_A_COMMAND_LINE_FILENAME_P (node) = 1;
1013               current_file_list = tree_cons (NULL_TREE, node, 
1014                                              current_file_list);
1015             }
1016         }
1017       list = next;
1018     }
1019
1020   if (filename_count == 0)
1021     warning ("no input file specified");
1022
1023   if (resource_name)
1024     {
1025       const char *resource_filename;
1026       
1027       /* Only one resource file may be compiled at a time.  */
1028       assert (TREE_CHAIN (current_file_list) == NULL);
1029
1030       resource_filename = IDENTIFIER_POINTER (TREE_VALUE (current_file_list));
1031       compile_resource_file (resource_name, resource_filename);
1032
1033       goto finish;
1034     }
1035
1036   current_jcf = main_jcf;
1037   current_file_list = nreverse (current_file_list);
1038   for (node = current_file_list; node; node = TREE_CHAIN (node))
1039     {
1040       unsigned char magic_string[4];
1041       uint32 magic = 0;
1042       tree name = TREE_VALUE (node);
1043       tree real_file;
1044
1045       /* Skip already parsed files */
1046       real_file = get_identifier (lrealpath (IDENTIFIER_POINTER (name)));
1047       if (HAS_BEEN_ALREADY_PARSED_P (real_file))
1048         continue;
1049       
1050       /* Close previous descriptor, if any */
1051       if (finput && fclose (finput))
1052         fatal_error ("can't close input file %s: %m", main_input_filename);
1053       
1054       finput = fopen (IDENTIFIER_POINTER (name), "rb");
1055       if (finput == NULL)
1056         fatal_error ("can't open %s: %m", IDENTIFIER_POINTER (name));
1057
1058 #ifdef IO_BUFFER_SIZE
1059       setvbuf (finput, xmalloc (IO_BUFFER_SIZE),
1060                _IOFBF, IO_BUFFER_SIZE);
1061 #endif
1062       input_filename = IDENTIFIER_POINTER (name);
1063
1064       /* Figure what kind of file we're dealing with */
1065       if (fread (magic_string, 1, 4, finput) == 4)
1066         {
1067           fseek (finput, 0L, SEEK_SET);
1068           magic = GET_u4 (magic_string);
1069         }
1070       if (magic == 0xcafebabe)
1071         {
1072           CLASS_FILE_P (node) = 1;
1073           current_jcf = ggc_alloc (sizeof (JCF));
1074           JCF_ZERO (current_jcf);
1075           current_jcf->read_state = finput;
1076           current_jcf->filbuf = jcf_filbuf_from_stdio;
1077           jcf_parse (current_jcf);
1078           TYPE_JCF (current_class) = current_jcf;
1079           CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1080           TREE_PURPOSE (node) = current_class;
1081         }
1082       else if (magic == (JCF_u4)ZIPMAGIC)
1083         {
1084           ZIP_FILE_P (node) = 1;
1085           main_jcf = ggc_alloc (sizeof (JCF));
1086           JCF_ZERO (main_jcf);
1087           main_jcf->read_state = finput;
1088           main_jcf->filbuf = jcf_filbuf_from_stdio;
1089           if (open_in_zip (main_jcf, input_filename, NULL, 0) <  0)
1090             fatal_error ("bad zip/jar file %s", IDENTIFIER_POINTER (name));
1091           localToFile = SeenZipFiles;
1092           /* Register all the classes defined there.  */
1093           process_zip_dir (main_jcf->read_state);
1094           parse_zip_file_entries ();
1095           /*
1096           for (each entry)
1097             CLASS_FROM_CURRENTLY_COMPILED_P (current_class) = 1;
1098           */
1099         }
1100       else
1101         {
1102           JAVA_FILE_P (node) = 1;
1103           java_push_parser_context ();
1104           java_parser_context_save_global ();
1105
1106           parse_source_file_1 (real_file, finput);
1107           java_parser_context_restore_global ();
1108           java_pop_parser_context (1);
1109         }
1110     }
1111
1112   for (ctxp = ctxp_for_generation;  ctxp;  ctxp = ctxp->next)
1113     {
1114       input_filename = ctxp->filename;
1115       parse_source_file_2 ();
1116     }
1117
1118   for (ctxp = ctxp_for_generation; ctxp; ctxp = ctxp->next)
1119     {
1120       input_filename = ctxp->filename;
1121       parse_source_file_3 ();
1122     }
1123
1124   for (node = current_file_list; node; node = TREE_CHAIN (node))
1125     {
1126       input_filename = IDENTIFIER_POINTER (TREE_VALUE (node));
1127       if (CLASS_FILE_P (node))
1128         {
1129           output_class = current_class = TREE_PURPOSE (node);
1130           current_jcf = TYPE_JCF (current_class);
1131           layout_class (current_class);
1132           load_inner_classes (current_class);
1133           parse_class_file ();
1134           JCF_FINISH (current_jcf);
1135         }
1136     }
1137   input_filename = main_input_filename;
1138
1139   java_expand_classes ();
1140   if (java_report_errors () || flag_syntax_only)
1141     return;
1142     
1143   /* Expand all classes compiled from source.  */
1144   java_finish_classes ();
1145
1146  finish:
1147   /* Arrange for any necessary initialization to happen.  */
1148   java_emit_static_constructor ();
1149
1150   /* Only finalize the compilation unit after we've told cgraph which
1151      functions have their addresses stored.  */
1152   cgraph_finalize_compilation_unit ();
1153   cgraph_optimize ();
1154 }
1155
1156
1157 /* Return the name of the class corresponding to the name of the file
1158    in this zip entry.  The result is newly allocated using ALLOC.  */
1159 static char *
1160 compute_class_name (struct ZipDirectory *zdir)
1161 {
1162   char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1163   char *class_name;
1164   int j;
1165
1166   class_name = ALLOC (zdir->filename_length + 1 - 6);
1167   strncpy (class_name, class_name_in_zip_dir, zdir->filename_length - 6);
1168   class_name [zdir->filename_length - 6] = '\0';
1169   for (j = 0; class_name[j]; ++j)
1170     class_name[j] = class_name[j] == '/' ? '.' : class_name[j];
1171   return class_name;
1172 }
1173
1174 /* Return 0 if we should skip this entry, 1 if it is a .class file, 2
1175    if it is a property file of some sort.  */
1176 static int
1177 classify_zip_file (struct ZipDirectory *zdir)
1178 {
1179   char *class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1180
1181   if (zdir->filename_length > 6
1182       && !strncmp (&class_name_in_zip_dir[zdir->filename_length - 6],
1183                    ".class", 6))
1184     return 1;
1185
1186   /* For now we drop the manifest, but not other information.  */
1187   if (zdir->filename_length == 20
1188       && !strncmp (class_name_in_zip_dir, "META-INF/MANIFEST.MF", 20))
1189     return 0;
1190
1191   /* Drop directory entries.  */
1192   if (zdir->filename_length > 0
1193       && class_name_in_zip_dir[zdir->filename_length - 1] == '/')
1194     return 0;
1195
1196   return 2;
1197 }
1198
1199 /* Process all class entries found in the zip file.  */
1200 static void
1201 parse_zip_file_entries (void)
1202 {
1203   struct ZipDirectory *zdir;
1204   int i;
1205
1206   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1207        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1208     {
1209       tree class;
1210
1211       switch (classify_zip_file (zdir))
1212         {
1213         case 0:
1214           continue;
1215
1216         case 1:
1217           {
1218             char *class_name = compute_class_name (zdir);
1219             class = lookup_class (get_identifier (class_name));
1220             FREE (class_name);
1221             current_jcf = TYPE_JCF (class);
1222             output_class = current_class = class;
1223
1224             if (! CLASS_LOADED_P (class))
1225               {
1226                 if (! CLASS_PARSED_P (class))
1227                   {
1228                     read_zip_member (current_jcf, zdir, localToFile);
1229                     jcf_parse (current_jcf);
1230                   }
1231                 layout_class (current_class);
1232                 load_inner_classes (current_class);
1233               }
1234
1235             if (TYPE_SIZE (current_class) != error_mark_node)
1236               {
1237                 input_filename = current_jcf->filename;
1238                 parse_class_file ();
1239                 FREE (current_jcf->buffer); /* No longer necessary */
1240                 /* Note: there is a way to free this buffer right after a
1241                    class seen in a zip file has been parsed. The idea is the
1242                    set its jcf in such a way that buffer will be reallocated
1243                    the time the code for the class will be generated. FIXME. */
1244               }
1245           }
1246           break;
1247
1248         case 2:
1249           {
1250             char *file_name, *class_name_in_zip_dir, *buffer;
1251             JCF *jcf;
1252             file_name = ALLOC (zdir->filename_length + 1);
1253             class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1254             strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1255             file_name[zdir->filename_length] = '\0';
1256             jcf = ALLOC (sizeof (JCF));
1257             JCF_ZERO (jcf);
1258             jcf->read_state  = finput;
1259             jcf->filbuf      = jcf_filbuf_from_stdio;
1260             jcf->java_source = 0;
1261             jcf->classname   = NULL;
1262             jcf->filename    = file_name;
1263             jcf->zipd        = zdir;
1264
1265             if (read_zip_member (jcf, zdir, localToFile) < 0)
1266               fatal_error ("error while reading %s from zip file", file_name);
1267
1268             buffer = ALLOC (zdir->filename_length + 1 +
1269                             (jcf->buffer_end - jcf->buffer));
1270             strcpy (buffer, file_name);
1271             /* This is not a typo: we overwrite the trailing \0 of the
1272                file name; this is just how the data is laid out.  */
1273             memcpy (buffer + zdir->filename_length,
1274                     jcf->buffer, jcf->buffer_end - jcf->buffer);
1275
1276             compile_resource_data (file_name, buffer,
1277                                    jcf->buffer_end - jcf->buffer);
1278             JCF_FINISH (jcf);
1279             FREE (jcf);
1280             FREE (buffer);
1281           }
1282           break;
1283
1284         default:
1285           abort ();
1286         }
1287     }
1288 }
1289
1290 /* Read all the entries of the zip file, creates a class and a JCF. Sets the
1291    jcf up for further processing and link it to the created class.  */
1292
1293 static void
1294 process_zip_dir (FILE *finput)
1295 {
1296   int i;
1297   ZipDirectory *zdir;
1298
1299   for (i = 0, zdir = (ZipDirectory *)localToFile->central_directory;
1300        i < localToFile->count; i++, zdir = ZIPDIR_NEXT (zdir))
1301     {
1302       char *class_name, *file_name, *class_name_in_zip_dir;
1303       tree class;
1304       JCF  *jcf;
1305
1306       class_name_in_zip_dir = ZIPDIR_FILENAME (zdir);
1307
1308       /* Here we skip non-class files; we handle them later.  */
1309       if (classify_zip_file (zdir) != 1)
1310         continue;
1311
1312       class_name = compute_class_name (zdir);
1313       file_name  = ALLOC (zdir->filename_length+1);
1314       jcf = ggc_alloc (sizeof (JCF));
1315       JCF_ZERO (jcf);
1316
1317       strncpy (file_name, class_name_in_zip_dir, zdir->filename_length);
1318       file_name [zdir->filename_length] = '\0';
1319
1320       class = lookup_class (get_identifier (class_name));
1321
1322       jcf->read_state  = finput;
1323       jcf->filbuf      = jcf_filbuf_from_stdio;
1324       jcf->java_source = 0;
1325       jcf->classname   = class_name;
1326       jcf->filename    = file_name;
1327       jcf->zipd        = zdir;
1328
1329       TYPE_JCF (class) = jcf;
1330     }
1331 }
1332
1333 /* Initialization.  */
1334
1335 void
1336 init_jcf_parse (void)
1337 {
1338   init_src_parse ();
1339 }
1340
1341 #include "gt-java-jcf-parse.h"