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