OSDN Git Service

(gcc)
[pf3gnuchains/gcc-fork.git] / gcc / java / lang.c
1 /* Java(TM) language-specific utility routines.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. 
21
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
25
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "tree.h"
33 #include "input.h"
34 #include "rtl.h"
35 #include "expr.h"
36 #include "java-tree.h"
37 #include "jcf.h"
38 #include "toplev.h"
39 #include "langhooks.h"
40 #include "langhooks-def.h"
41 #include "flags.h"
42 #include "xref.h"
43 #include "ggc.h"
44 #include "diagnostic.h"
45 #include "tree-inline.h"
46 #include "splay-tree.h"
47 #include "tree-dump.h"
48
49 struct string_option
50 {
51   const char *const string;
52   int *const variable;
53   const int on_value;
54 };
55
56 static bool java_init (void);
57 static void java_finish (void);
58 static void java_init_options (void);
59 static bool java_post_options (const char **);
60
61 static int java_decode_option (int, char **);
62 static void put_decl_string (const char *, int);
63 static void put_decl_node (tree);
64 static void java_print_error_function (diagnostic_context *, const char *);
65 static int process_option_with_no (const char *, const struct string_option *, int);
66 static tree java_tree_inlining_walk_subtrees (tree *, int *, walk_tree_fn,
67                                               void *, void *);
68 static int java_unsafe_for_reeval (tree);
69 static int merge_init_test_initialization (void * *, void *);
70 static int inline_init_test_initialization (void * *, void *);
71 static bool java_can_use_bit_fields_p (void);
72 static bool java_dump_tree (void *, tree);
73 static void dump_compound_expr (dump_info_p, tree);
74
75 #ifndef TARGET_OBJECT_SUFFIX
76 # define TARGET_OBJECT_SUFFIX ".o"
77 #endif
78
79 /* Table indexed by tree code giving a string containing a character
80    classifying the tree code.  Possibilities are
81    t, d, s, c, r, <, 1 and 2.  See java/java-tree.def for details.  */
82
83 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
84
85 const char tree_code_type[] = {
86 #include "tree.def"
87   'x',
88 #include "java-tree.def"
89 };
90 #undef DEFTREECODE
91
92 /* Table indexed by tree code giving number of expression
93    operands beyond the fixed part of the node structure.
94    Not used for types or decls.  */
95
96 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
97
98 const unsigned char tree_code_length[] = {
99 #include "tree.def"
100   0,
101 #include "java-tree.def"
102 };
103 #undef DEFTREECODE
104
105 /* Names of tree components.
106    Used for printing out the tree and error messages.  */
107 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
108
109 const char *const tree_code_name[] = {
110 #include "tree.def"
111   "@@dummy",
112 #include "java-tree.def"
113 };
114 #undef DEFTREECODE
115
116 /* Used to avoid printing error messages with bogus function
117    prototypes.  Starts out false.  */
118 static bool inhibit_error_function_printing;
119
120 int compiling_from_source;
121
122 char * resource_name;
123
124 int flag_emit_class_files = 0;
125
126 /* Nonzero if input file is a file with a list of filenames to compile. */
127
128 int flag_filelist_file = 0;
129
130 /* When nonzero, we emit xref strings. Values of the flag for xref
131    backends are defined in xref_flag_table, xref.c.  */
132
133 int flag_emit_xref = 0;
134
135 /* When nonzero, -Wall was turned on.  */
136 int flag_wall = 0;
137
138 /* When nonzero, check for redundant modifier uses.  */
139 int flag_redundant = 0;
140
141 /* When nonzero, call a library routine to do integer divisions. */
142 int flag_use_divide_subroutine = 1;
143
144 /* When nonzero, generate code for the Boehm GC.  */
145 int flag_use_boehm_gc = 0;
146
147 /* When nonzero, assume the runtime uses a hash table to map an
148    object to its synchronization structure.  */
149 int flag_hash_synchronization;
150
151 /* When nonzero, permit the use of the assert keyword.  */
152 int flag_assert = 1;
153
154 /* When nonzero, assume all native functions are implemented with
155    JNI, not CNI.  */
156 int flag_jni = 0;
157
158 /* When nonzero, warn when source file is newer than matching class
159    file.  */
160 int flag_newer = 1;
161
162 /* When nonzero, generate checks for references to NULL.  */
163 int flag_check_references = 0;
164
165 /* The encoding of the source file.  */
166 const char *current_encoding = NULL;
167
168 /* When nonzero, report the now deprecated empty statements.  */
169 int flag_extraneous_semicolon;
170
171 /* When nonzero, report use of deprecated classes, methods, or fields.  */
172 int flag_deprecated = 1;
173
174 /* When nonzero, always check for a non gcj generated classes archive.  */
175 int flag_force_classes_archive_check;
176
177 /* When zero, don't optimize static class initialization. This flag shouldn't
178    be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead.  */
179 int flag_optimize_sci = 1;
180
181 /* When nonzero, use offset tables for virtual method calls
182    in order to improve binary compatibility. */
183 int flag_indirect_dispatch = 0;
184
185 /* When zero, don't generate runtime array store checks. */
186 int flag_store_check = 1;
187
188 /* When nonzero, print extra version information.  */
189 static int version_flag = 0;
190
191 /* Set nonzero if the user specified -finline-functions on the command
192    line.  */
193 int flag_really_inline = 0;
194
195 /* Table of language-dependent -f options.
196    STRING is the option name.  VARIABLE is the address of the variable.
197    ON_VALUE is the value to store in VARIABLE
198     if `-fSTRING' is seen as an option.
199    (If `-fno-STRING' is seen as an option, the opposite value is stored.)  */
200
201 static const struct string_option
202 lang_f_options[] =
203 {
204   {"emit-class-file", &flag_emit_class_files, 1},
205   {"emit-class-files", &flag_emit_class_files, 1},
206   {"filelist-file", &flag_filelist_file, 1},
207   {"use-divide-subroutine", &flag_use_divide_subroutine, 1},
208   {"use-boehm-gc", &flag_use_boehm_gc, 1},
209   {"hash-synchronization", &flag_hash_synchronization, 1},
210   {"jni", &flag_jni, 1},
211   {"check-references", &flag_check_references, 1},
212   {"force-classes-archive-check", &flag_force_classes_archive_check, 1},
213   {"optimize-static-class-initialization", &flag_optimize_sci, 1 },
214   {"indirect-dispatch", &flag_indirect_dispatch, 1},
215   {"store-check", &flag_store_check, 1},
216   {"assert", &flag_assert, 1}
217 };
218
219 static const struct string_option
220 lang_W_options[] =
221 {
222   { "redundant-modifiers", &flag_redundant, 1 },
223   { "extraneous-semicolon", &flag_extraneous_semicolon, 1 },
224   { "out-of-date", &flag_newer, 1 },
225   { "deprecated", &flag_deprecated, 1 }
226 };
227
228 JCF *current_jcf;
229
230 /* Variable controlling how dependency tracking is enabled in
231    java_init.  */
232 static int dependency_tracking = 0;
233
234 /* Flag values for DEPENDENCY_TRACKING.  */
235 #define DEPEND_SET_FILE 1
236 #define DEPEND_ENABLE   2
237 #define DEPEND_TARGET_SET 4
238 #define DEPEND_FILE_ALREADY_SET 8
239
240 struct language_function GTY(())
241 {
242   int unused;
243 };
244
245 #undef LANG_HOOKS_NAME
246 #define LANG_HOOKS_NAME "GNU Java"
247 #undef LANG_HOOKS_INIT
248 #define LANG_HOOKS_INIT java_init
249 #undef LANG_HOOKS_FINISH
250 #define LANG_HOOKS_FINISH java_finish
251 #undef LANG_HOOKS_INIT_OPTIONS
252 #define LANG_HOOKS_INIT_OPTIONS java_init_options
253 #undef LANG_HOOKS_DECODE_OPTION
254 #define LANG_HOOKS_DECODE_OPTION java_decode_option
255 #undef LANG_HOOKS_POST_OPTIONS
256 #define LANG_HOOKS_POST_OPTIONS java_post_options
257 #undef LANG_HOOKS_PARSE_FILE
258 #define LANG_HOOKS_PARSE_FILE java_parse_file
259 #undef LANG_HOOKS_UNSAFE_FOR_REEVAL
260 #define LANG_HOOKS_UNSAFE_FOR_REEVAL java_unsafe_for_reeval
261 #undef LANG_HOOKS_MARK_ADDRESSABLE
262 #define LANG_HOOKS_MARK_ADDRESSABLE java_mark_addressable
263 #undef LANG_HOOKS_EXPAND_EXPR
264 #define LANG_HOOKS_EXPAND_EXPR java_expand_expr
265 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
266 #define LANG_HOOKS_TRUTHVALUE_CONVERSION java_truthvalue_conversion
267 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
268 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
269 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
270 #define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
271 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
272 #define LANG_HOOKS_PRINT_ERROR_FUNCTION java_print_error_function
273 #undef LANG_HOOKS_CAN_USE_BIT_FIELDS_P
274 #define LANG_HOOKS_CAN_USE_BIT_FIELDS_P java_can_use_bit_fields_p
275
276 #undef LANG_HOOKS_TYPE_FOR_MODE
277 #define LANG_HOOKS_TYPE_FOR_MODE java_type_for_mode
278 #undef LANG_HOOKS_TYPE_FOR_SIZE
279 #define LANG_HOOKS_TYPE_FOR_SIZE java_type_for_size
280 #undef LANG_HOOKS_SIGNED_TYPE
281 #define LANG_HOOKS_SIGNED_TYPE java_signed_type
282 #undef LANG_HOOKS_UNSIGNED_TYPE
283 #define LANG_HOOKS_UNSIGNED_TYPE java_unsigned_type
284 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
285 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE java_signed_or_unsigned_type
286
287 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
288 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES java_tree_inlining_walk_subtrees
289
290 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
291 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN java_dump_tree
292
293 /* Each front end provides its own.  */
294 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
295
296 /* Process an option that can accept a `no-' form.
297    Return 1 if option found, 0 otherwise.  */
298 static int
299 process_option_with_no (const char *p, const struct string_option *table,
300                         int table_size)
301 {
302   int j;
303
304   for (j = 0; j < table_size; j++)
305     {
306       if (!strcmp (p, table[j].string))
307         {
308           *table[j].variable = table[j].on_value;
309           return 1;
310         }
311       if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
312           && ! strcmp (p+3, table[j].string))
313         {
314           *table[j].variable = ! table[j].on_value;
315           return 1;
316         }
317     }
318
319   return 0;
320 }
321
322 /*
323  * process java-specific compiler command-line options
324  * return 0, but do not complain if the option is not recognized.
325  */
326 static int
327 java_decode_option (int argc __attribute__ ((__unused__)), char **argv)
328 {
329   char *p = argv[0];
330
331   jcf_path_init ();
332
333   if (strcmp (p, "-version") == 0)
334     {
335       version_flag = 1;
336       /* We return 0 so that the caller can process this.  */
337       return 0;
338     }
339
340 #define CLARG "-fcompile-resource="
341   if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
342     {
343       resource_name = p + sizeof (CLARG) - 1;
344       return 1;
345     }
346 #undef CLARG
347 #define CLARG "-fassume-compiled="
348   if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
349     {
350       add_assume_compiled (p + sizeof (CLARG) - 1, 0);
351       return 1;
352     }
353 #undef CLARG
354 #define CLARG "-fno-assume-compiled="
355   if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
356     {
357       add_assume_compiled (p + sizeof (CLARG) - 1, 1);
358       return 1;
359     }
360 #undef CLARG
361 #define CLARG "-fassume-compiled"
362   if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
363     {
364       add_assume_compiled ("", 0);
365       return 1;
366     }
367 #undef CLARG
368 #define CLARG "-fno-assume-compiled"
369   if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
370     {
371       add_assume_compiled ("", 1);
372       return 1;
373     }
374 #undef CLARG
375 #define CLARG "-fCLASSPATH="
376   if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
377     {
378       jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
379       return 1;
380     }
381 #undef CLARG
382 #define CLARG "-fclasspath="
383   if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
384     {
385       jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
386       return 1;
387     }
388 #undef CLARG
389 #define CLARG "-fbootclasspath="
390   if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
391     {
392       jcf_path_bootclasspath_arg (p + sizeof (CLARG) - 1);
393       return 1;
394     }
395 #undef CLARG
396 #define CLARG "-fextdirs="
397   if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
398     {
399       jcf_path_extdirs_arg (p + sizeof (CLARG) - 1);
400       return 1;
401     }
402 #undef CLARG
403   else if (strncmp (p, "-I", 2) == 0)
404     {
405       jcf_path_include_arg (p + 2);
406       return 1;
407     }
408
409 #define ARG "-foutput-class-dir="
410   if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
411     {
412       jcf_write_base_directory = p + sizeof (ARG) - 1;
413       return 1;
414     }
415 #undef ARG
416 #define ARG "-fencoding="
417   if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
418     {
419       current_encoding = p + sizeof (ARG) - 1;
420       return 1;
421     }
422 #undef ARG
423 #define ARG "-finline-functions"
424   if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
425     {
426       flag_inline_functions = 1;
427       flag_really_inline = 1;
428       return 1;
429     }
430 #undef ARG
431
432   if (p[0] == '-' && p[1] == 'f')
433     {
434       /* Some kind of -f option.
435          P's value is the option sans `-f'.
436          Search for it in the table of options.  */
437       p += 2;
438       if (process_option_with_no (p, lang_f_options,
439                                   ARRAY_SIZE (lang_f_options)))
440         return 1;
441       return dump_switch_p (p);
442     }
443
444   if (strcmp (p, "-Wall") == 0)
445     {
446       flag_wall = 1;
447       flag_redundant = 1;
448       flag_extraneous_semicolon = 1;
449       /* When -Wall given, enable -Wunused.  We do this because the C
450          compiler does it, and people expect it.  */
451       set_Wunused (1);
452       return 1;
453     }
454
455   if (p[0] == '-' && p[1] == 'W')
456     {
457       /* Skip `-W' and see if we accept the option or its `no-' form.  */
458       p += 2;
459       return process_option_with_no (p, lang_W_options,
460                                      ARRAY_SIZE (lang_W_options));
461     }
462
463   if (strcmp (p, "-MD") == 0)
464     {
465       jcf_dependency_init (1);
466       dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
467       return 1;
468     }
469   else if (strcmp (p, "-MMD") == 0)
470     {
471       jcf_dependency_init (0);
472       dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
473       return 1;
474     }
475   else if (strcmp (p, "-M") == 0)
476     {
477       jcf_dependency_init (1);
478       dependency_tracking |= DEPEND_ENABLE;
479       return 1;
480     }
481   else if (strcmp (p, "-MM") == 0)
482     {
483       jcf_dependency_init (0);
484       dependency_tracking |= DEPEND_ENABLE;
485       return 1;
486     }
487   else if (strcmp (p, "-MP") == 0)
488     {
489       jcf_dependency_print_dummies ();
490       return 1;
491     }
492   else if (strcmp (p, "-MT") == 0)
493     {
494       jcf_dependency_set_target (argv[1]);
495       dependency_tracking |= DEPEND_TARGET_SET;
496       return 2;
497     }
498   else if (strcmp (p, "-MF") == 0)
499     {
500       jcf_dependency_set_dep_file (argv[1]);
501       dependency_tracking |= DEPEND_FILE_ALREADY_SET;
502       return 2;
503     }
504
505   return 0;
506 }
507
508 /* Global open file.  */
509 FILE *finput;
510
511 static bool
512 java_init (void)
513 {
514 #if 0
515   extern int flag_minimal_debug;
516   flag_minimal_debug = 0;
517 #endif
518
519   if (flag_inline_functions)
520     flag_inline_trees = 1;
521
522   /* Force minimum function alignment if g++ uses the least significant
523      bit of function pointers to store the virtual bit. This is required
524      to keep vtables compatible.  */
525   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
526       && force_align_functions_log < 1)
527     force_align_functions_log = 1;
528
529   jcf_path_init ();
530   jcf_path_seal (version_flag);
531
532   java_init_decl_processing ();
533
534   using_eh_for_cleanups ();
535
536   return true;
537 }
538
539 static void
540 java_finish (void)
541 {
542   jcf_dependency_write ();
543 }
544
545 /* Buffer used by lang_printable_name. */
546 static char *decl_buf = NULL;
547
548 /* Allocated size of decl_buf. */
549 static int decl_buflen = 0;
550
551 /* Length of used part of decl_buf;  position for next character. */
552 static int decl_bufpos = 0;
553
554 /* Append the string STR to decl_buf.
555    It length is given by LEN;  -1 means the string is nul-terminated. */
556
557 static void
558 put_decl_string (const char *str, int len)
559 {
560   if (len < 0)
561     len = strlen (str);
562   if (decl_bufpos + len >= decl_buflen)
563     {
564       if (decl_buf == NULL)
565         {
566           decl_buflen = len + 100;
567           decl_buf = xmalloc (decl_buflen);
568         }
569       else
570         {
571           decl_buflen *= 2;
572           decl_buf = xrealloc (decl_buf, decl_buflen);
573         }
574     }
575   strcpy (decl_buf + decl_bufpos, str);
576   decl_bufpos += len;
577 }
578
579 /* Append to decl_buf a printable name for NODE. */
580
581 static void
582 put_decl_node (tree node)
583 {
584   int was_pointer = 0;
585   if (TREE_CODE (node) == POINTER_TYPE)
586     {
587       node = TREE_TYPE (node);
588       was_pointer = 1;
589     }
590   if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd'
591       && DECL_NAME (node) != NULL_TREE)
592     {
593       if (TREE_CODE (node) == FUNCTION_DECL)
594         {
595           /* We want to print the type the DECL belongs to. We don't do
596              that when we handle constructors. */
597           if (! DECL_CONSTRUCTOR_P (node)
598               && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node))
599             {
600               put_decl_node (TYPE_NAME (DECL_CONTEXT (node)));
601               put_decl_string (".", 1);
602             }
603           if (! DECL_CONSTRUCTOR_P (node))
604             put_decl_node (DECL_NAME (node));
605           if (TREE_TYPE (node) != NULL_TREE)
606             {
607               int i = 0;
608               tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
609               if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
610                 args = TREE_CHAIN (args);
611               put_decl_string ("(", 1);
612               for ( ; args != end_params_node;  args = TREE_CHAIN (args), i++)
613                 {
614                   if (i > 0)
615                     put_decl_string (",", 1);
616                   put_decl_node (TREE_VALUE (args));
617                 }
618               put_decl_string (")", 1);
619             }
620         }
621       else
622         put_decl_node (DECL_NAME (node));
623     }
624   else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't'
625       && TYPE_NAME (node) != NULL_TREE)
626     {
627       if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node))
628         {
629           put_decl_node (TYPE_ARRAY_ELEMENT (node));
630           put_decl_string("[]", 2);
631         }
632       else if (node == promoted_byte_type_node)
633         put_decl_string ("byte", 4);
634       else if (node == promoted_short_type_node)
635         put_decl_string ("short", 5);
636       else if (node == promoted_char_type_node)
637         put_decl_string ("char", 4);
638       else if (node == promoted_boolean_type_node)
639         put_decl_string ("boolean", 7);
640       else if (node == void_type_node && was_pointer)
641         put_decl_string ("null", 4);
642       else
643         put_decl_node (TYPE_NAME (node));
644     }
645   else if (TREE_CODE (node) == IDENTIFIER_NODE)
646     put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
647   else
648     put_decl_string ("<unknown>", -1);
649 }
650
651 /* Return a user-friendly name for DECL.
652    The resulting string is only valid until the next call.
653    The value of the hook decl_printable_name is this function,
654    which is also called directly by java_print_error_function. */
655
656 const char *
657 lang_printable_name (tree decl, int v  __attribute__ ((__unused__)))
658 {
659   decl_bufpos = 0;
660   put_decl_node (decl);
661   put_decl_string ("", 1);
662   return decl_buf;
663 }
664
665 /* Does the same thing that lang_printable_name, but add a leading
666    space to the DECL name string -- With Leading Space.  */
667
668 const char *
669 lang_printable_name_wls (tree decl, int v  __attribute__ ((__unused__)))
670 {
671   decl_bufpos = 1;
672   put_decl_node (decl);
673   put_decl_string ("", 1);
674   decl_buf [0] = ' ';
675   return decl_buf;
676 }
677
678 /* Print on stderr the current class and method context.  This function
679    is the value of the hook print_error_function. */
680
681 static GTY(()) tree last_error_function_context;
682 static GTY(()) tree last_error_function;
683 static void
684 java_print_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
685                            const char *file)
686 {
687   /* Don't print error messages with bogus function prototypes.  */
688   if (inhibit_error_function_printing)
689     return;
690
691   if (current_function_decl != NULL
692       && DECL_CONTEXT (current_function_decl) != last_error_function_context)
693     {
694       if (file)
695         fprintf (stderr, "%s: ", file);
696
697       last_error_function_context = DECL_CONTEXT (current_function_decl);
698       fprintf (stderr, "In class `%s':\n",
699                lang_printable_name (last_error_function_context, 0));
700     }
701   if (last_error_function != current_function_decl)
702     {
703       if (file)
704         fprintf (stderr, "%s: ", file);
705
706       if (current_function_decl == NULL)
707         fprintf (stderr, "At top level:\n");
708       else
709         {
710           const char *name = lang_printable_name (current_function_decl, 2);
711           fprintf (stderr, "In %s `%s':\n",
712                    (DECL_CONSTRUCTOR_P (current_function_decl) ? "constructor" 
713                     : "method"),
714                    name);
715         }
716
717       last_error_function = current_function_decl;
718     }
719
720 }
721
722 /* Called to install the PRINT_ERROR_FUNCTION hook differently
723    according to LEVEL. LEVEL is 1 during early parsing, when function
724    prototypes aren't fully resolved. java_print_error_function is set
725    so it doesn't print incomplete function prototypes. When LEVEL is
726    2, function prototypes are fully resolved and can be printed when
727    reporting errors.  */
728
729 void lang_init_source (int level)
730 {
731   inhibit_error_function_printing = (level == 1);
732 }
733
734 static void
735 java_init_options (void)
736 {
737   flag_bounds_check = 1;
738   flag_exceptions = 1;
739   flag_non_call_exceptions = 1;
740
741   /* In Java floating point operations never trap.  */
742   flag_trapping_math = 0;
743 }
744
745 static bool
746 java_can_use_bit_fields_p (void)
747 {
748   /* The bit-field optimizations cause problems when generating class
749      files.  */
750   return flag_emit_class_files ? false : true;
751 }
752
753 /* Post-switch processing.  */
754 static bool
755 java_post_options (const char **pfilename)
756 {
757   const char *filename = *pfilename;
758
759  /* Use tree inlining if possible.  Function instrumentation is only
760      done in the RTL level, so we disable tree inlining.  */
761   if (! flag_instrument_function_entry_exit)
762     {
763       if (!flag_no_inline)
764         flag_no_inline = 1;
765       if (flag_inline_functions)
766         {
767           flag_inline_trees = 2;
768           flag_inline_functions = 0;
769         }
770     }
771
772   /* Open input file.  */
773
774   if (filename == 0 || !strcmp (filename, "-"))
775     {
776       finput = stdin;
777       filename = "stdin";
778
779       if (dependency_tracking)
780         error ("can't do dependency tracking with input from stdin");
781     }
782   else
783     {
784       if (dependency_tracking)
785         {
786           char *dot;
787
788           /* If the target is set and the output filename is set, then
789              there's no processing to do here.  Otherwise we must
790              compute one or the other.  */
791           if (! ((dependency_tracking & DEPEND_TARGET_SET)
792                  && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
793             {
794               dot = strrchr (filename, '.');
795               if (dot == NULL)
796                 error ("couldn't determine target name for dependency tracking");
797               else
798                 {
799                   char *buf = xmalloc (dot - filename +
800                                        3 + sizeof (TARGET_OBJECT_SUFFIX));
801                   strncpy (buf, filename, dot - filename);
802
803                   /* If emitting class files, we might have multiple
804                      targets.  The class generation code takes care of
805                      registering them.  Otherwise we compute the
806                      target name here.  */
807                   if ((dependency_tracking & DEPEND_TARGET_SET))
808                     ; /* Nothing.  */
809                   else if (flag_emit_class_files)
810                     jcf_dependency_set_target (NULL);
811                   else
812                     {
813                       strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
814                       jcf_dependency_set_target (buf);
815                     }
816
817                   if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
818                     ; /* Nothing.  */
819                   else if ((dependency_tracking & DEPEND_SET_FILE))
820                     {
821                       strcpy (buf + (dot - filename), ".d");
822                       jcf_dependency_set_dep_file (buf);
823                     }
824                   else
825                     jcf_dependency_set_dep_file ("-");
826
827                   free (buf);
828                 }
829             }
830         }
831     }
832
833   /* Initialize the compiler back end.  */
834   return false;
835 }
836
837 /* Return either DECL or its known constant value (if it has one).  */
838
839 tree
840 decl_constant_value (tree decl)
841 {
842   if (/* Don't change a variable array bound or initial value to a constant
843          in a place where a variable is invalid.  */
844       current_function_decl != 0
845       && ! TREE_THIS_VOLATILE (decl)
846       && TREE_READONLY (decl)
847       && DECL_INITIAL (decl) != 0
848       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
849       /* This is invalid if initial value is not constant.
850          If it has either a function call, a memory reference,
851          or a variable, then re-evaluating it could give different results.  */
852       && TREE_CONSTANT (DECL_INITIAL (decl))
853       /* Check for cases where this is sub-optimal, even though valid.  */
854       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
855     return DECL_INITIAL (decl);
856   return decl;
857 }
858
859 /* Walk the language specific tree nodes during inlining.  */
860
861 static tree
862 java_tree_inlining_walk_subtrees (tree *tp ATTRIBUTE_UNUSED,
863                                   int *subtrees ATTRIBUTE_UNUSED,
864                                   walk_tree_fn func ATTRIBUTE_UNUSED,
865                                   void *data ATTRIBUTE_UNUSED,
866                                   void *htab ATTRIBUTE_UNUSED)
867 {
868   enum tree_code code;
869   tree result;
870
871 #define WALK_SUBTREE(NODE)                              \
872   do                                                    \
873     {                                                   \
874       result = walk_tree (&(NODE), func, data, htab);   \
875       if (result)                                       \
876         return result;                                  \
877     }                                                   \
878   while (0)
879
880   tree t = *tp;
881   if (!t)
882     return NULL_TREE;
883
884   code = TREE_CODE (t);
885   switch (code)
886     {
887     case BLOCK:
888       if (BLOCK_EXPR_BODY (t))
889         {
890           tree *prev = &BLOCK_EXPR_BODY (*tp);
891           while (*prev)
892             {
893               WALK_SUBTREE (*prev);
894               prev = &TREE_CHAIN (*prev);
895             }       
896         }
897       return NULL_TREE;
898       break;
899
900     default:
901       return NULL_TREE;
902     }
903 }
904
905 /* Called from unsafe_for_reeval.  */
906 static int
907 java_unsafe_for_reeval (tree t)
908 {
909   switch (TREE_CODE (t))
910     {
911     case BLOCK:
912       /* Our expander tries to expand the variables twice.  Boom.  */
913       if (BLOCK_EXPR_DECLS (t) != NULL)
914         return 2;
915       return unsafe_for_reeval (BLOCK_EXPR_BODY (t));
916
917     default:
918       break;
919     }
920
921   return -1;
922 }
923
924 /* Every call to a static constructor has an associated boolean
925    variable which is in the outermost scope of the calling method.
926    This variable is used to avoid multiple calls to the static
927    constructor for each class.  
928
929    It looks something like this:
930
931    foo ()
932    {
933       boolean dummy = OtherClass.is_initialized;
934   
935      ...
936   
937      if (! dummy)
938        OtherClass.initialize();
939
940      ... use OtherClass.data ...
941    }
942
943    Each of these boolean variables has an entry in the
944    DECL_FUNCTION_INIT_TEST_TABLE of a method.  When inlining a method
945    we must merge the DECL_FUNCTION_INIT_TEST_TABLE from the function
946    being inlined and create the boolean variables in the outermost
947    scope of the method being inlined into.  */
948
949 /* Create a mapping from a boolean variable in a method being inlined
950    to one in the scope of the method being inlined into.  */
951
952 static int
953 merge_init_test_initialization (void **entry, void *x)
954 {
955   struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
956   splay_tree decl_map = (splay_tree)x;
957   splay_tree_node n;
958   tree *init_test_decl;
959   
960   /* See if we have remapped this declaration.  If we haven't there's
961      a bug in the inliner.  */
962   n = splay_tree_lookup (decl_map, (splay_tree_key) ite->value);
963   if (! n)
964     abort ();
965
966   /* Create a new entry for the class and its remapped boolean
967      variable.  If we already have a mapping for this class we've
968      already initialized it, so don't overwrite the value.  */
969   init_test_decl = java_treetreehash_new
970     (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
971   if (!*init_test_decl)
972     *init_test_decl = (tree)n->value;
973
974   return true;
975 }
976
977 /* Merge the DECL_FUNCTION_INIT_TEST_TABLE from the function we're
978    inlining.  */
979
980 void
981 java_inlining_merge_static_initializers (tree fn, void *decl_map)
982 {
983   htab_traverse 
984     (DECL_FUNCTION_INIT_TEST_TABLE (fn),
985      merge_init_test_initialization, decl_map);
986 }
987
988 /* Lookup a DECL_FUNCTION_INIT_TEST_TABLE entry in the method we're
989    inlining into.  If we already have a corresponding entry in that
990    class we don't need to create another one, so we create a mapping
991    from the variable in the inlined class to the corresponding
992    pre-existing one.  */
993
994 static int
995 inline_init_test_initialization (void **entry, void *x)
996 {
997   struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
998   splay_tree decl_map = (splay_tree)x;
999   
1000   tree h = java_treetreehash_find 
1001     (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
1002   if (! h)
1003     return true;
1004
1005   splay_tree_insert (decl_map,
1006                      (splay_tree_key) ite->value,
1007                      (splay_tree_value) h);
1008
1009   return true;
1010 }
1011
1012 /* Look up the boolean variables in the DECL_FUNCTION_INIT_TEST_TABLE
1013    of a method being inlined.  For each hone, if we already have a
1014    variable associated with the same class in the method being inlined
1015    into, create a new mapping for it.  */
1016
1017 void
1018 java_inlining_map_static_initializers (tree fn, void *decl_map)
1019 {
1020   htab_traverse 
1021     (DECL_FUNCTION_INIT_TEST_TABLE (fn),
1022      inline_init_test_initialization, decl_map);
1023 }
1024
1025 /* Avoid voluminous output for deep recursion of compound exprs.  */
1026
1027 static void
1028 dump_compound_expr (dump_info_p di, tree t)
1029 {
1030   int i;
1031
1032   for (i=0; i<2; i++)
1033     {
1034       switch (TREE_CODE (TREE_OPERAND (t, i)))
1035         {
1036         case COMPOUND_EXPR:
1037           dump_compound_expr (di, TREE_OPERAND (t, i));
1038           break;
1039
1040         case EXPR_WITH_FILE_LOCATION:
1041             {
1042               tree wfl_node = EXPR_WFL_NODE (TREE_OPERAND (t, i));
1043               dump_child ("expr", wfl_node);
1044               break;
1045             }
1046
1047         default:
1048           dump_child ("expr", TREE_OPERAND (t, i));
1049         }
1050     }
1051 }
1052   
1053 static bool
1054 java_dump_tree (void *dump_info, tree t)
1055 {
1056   enum tree_code code;
1057   dump_info_p di = (dump_info_p) dump_info;
1058
1059   /* Figure out what kind of node this is.  */
1060   code = TREE_CODE (t);
1061
1062   switch (code)
1063     {
1064     case FUNCTION_DECL:
1065       dump_child ("args", DECL_ARGUMENTS (t));
1066       if (DECL_EXTERNAL (t))
1067         dump_string (di, "undefined");
1068       if (TREE_PUBLIC (t))
1069         dump_string (di, "extern");
1070       else
1071         dump_string (di, "static");
1072       if (DECL_LANG_SPECIFIC (t))
1073         dump_child ("body", DECL_FUNCTION_BODY (t));
1074       if (DECL_LANG_SPECIFIC (t) && !dump_flag (di, TDF_SLIM, t))
1075         dump_child ("inline body", DECL_SAVED_TREE (t));
1076       return true;
1077
1078     case RETURN_EXPR:
1079       dump_child ("expr", TREE_OPERAND (t, 0));
1080       return true;
1081
1082     case GOTO_EXPR:
1083       dump_child ("goto", TREE_OPERAND (t, 0));
1084       return true;
1085
1086     case LABEL_EXPR:
1087       dump_child ("label", TREE_OPERAND (t, 0));
1088       return true;
1089
1090     case LABELED_BLOCK_EXPR:
1091       dump_child ("label", TREE_OPERAND (t, 0));
1092       dump_child ("block", TREE_OPERAND (t, 1));
1093       return true;
1094
1095     case EXIT_BLOCK_EXPR:
1096       dump_child ("block", TREE_OPERAND (t, 0));
1097       dump_child ("val", TREE_OPERAND (t, 1));
1098       return true;
1099
1100     case BLOCK:
1101       if (BLOCK_EXPR_BODY (t))
1102         {
1103           tree local = BLOCK_VARS (t);
1104           while (local)
1105             {
1106               tree next = TREE_CHAIN (local);
1107               dump_child ("var", local);
1108               local = next;
1109             }
1110           
1111           {
1112             tree block = BLOCK_EXPR_BODY (t);
1113             dump_child ("body", block);
1114             block = TREE_CHAIN (block);
1115           }
1116         }
1117       return true;
1118       
1119     case COMPOUND_EXPR:
1120       if (!dump_flag (di, TDF_SLIM, t))
1121         return false;
1122       dump_compound_expr (di, t);
1123       return true;
1124
1125     default:
1126       break;
1127     }
1128   return false;
1129 }
1130 #include "gt-java-lang.h"