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