OSDN Git Service

* java-tree.h (push_labeled_block, pop_labeled_block): Remove.
[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, 2004,
3    2005, 2006, 2007 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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 "ggc.h"
43 #include "diagnostic.h"
44 #include "tree-inline.h"
45 #include "splay-tree.h"
46 #include "tree-dump.h"
47 #include "opts.h"
48 #include "options.h"
49
50 static bool java_init (void);
51 static void java_finish (void);
52 static unsigned int java_init_options (unsigned int, const char **);
53 static bool java_post_options (const char **);
54
55 static int java_handle_option (size_t scode, const char *arg, int value);
56 static void put_decl_string (const char *, int);
57 static void put_decl_node (tree);
58 static void java_print_error_function (diagnostic_context *, const char *);
59 static tree java_tree_inlining_walk_subtrees (tree *, int *, walk_tree_fn,
60                                               void *, struct pointer_set_t *);
61 static int merge_init_test_initialization (void * *, void *);
62 static int inline_init_test_initialization (void * *, void *);
63 static bool java_dump_tree (void *, tree);
64 static void dump_compound_expr (dump_info_p, tree);
65 static bool java_decl_ok_for_sibcall (tree);
66 static tree java_get_callee_fndecl (tree);
67 static void java_clear_binding_stack (void);
68
69 #ifndef TARGET_OBJECT_SUFFIX
70 # define TARGET_OBJECT_SUFFIX ".o"
71 #endif
72
73 /* Table indexed by tree code giving a string containing a character
74    classifying the tree code.  Possibilities are
75    t, d, s, c, r, <, 1 and 2.  See java/java-tree.def for details.  */
76
77 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
78
79 const enum tree_code_class tree_code_type[] = {
80 #include "tree.def"
81   tcc_exceptional,
82 #include "java-tree.def"
83 };
84 #undef DEFTREECODE
85
86 /* Table indexed by tree code giving number of expression
87    operands beyond the fixed part of the node structure.
88    Not used for types or decls.  */
89
90 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
91
92 const unsigned char tree_code_length[] = {
93 #include "tree.def"
94   0,
95 #include "java-tree.def"
96 };
97 #undef DEFTREECODE
98
99 /* Names of tree components.
100    Used for printing out the tree and error messages.  */
101 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
102
103 const char *const tree_code_name[] = {
104 #include "tree.def"
105   "@@dummy",
106 #include "java-tree.def"
107 };
108 #undef DEFTREECODE
109
110 /* Table of machine-independent attributes.  */
111 const struct attribute_spec java_attribute_table[] =
112 {
113  { "nonnull",                0, -1, false, true, true,
114                               NULL },
115   { NULL,                     0, 0, false, false, false, NULL }
116 };
117
118 /* Used to avoid printing error messages with bogus function
119    prototypes.  Starts out false.  */
120 static bool inhibit_error_function_printing;
121
122 const char *resource_name;
123
124 /* When nonzero, -Wall was turned on.  */
125 int flag_wall = 0;
126
127 /* When nonzero, report use of deprecated classes, methods, or fields.  */
128 int flag_deprecated = 1;
129
130 /* When zero, don't optimize static class initialization. This flag shouldn't
131    be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead.  */
132 /* FIXME: Make this work with gimplify.  */
133 /* int flag_optimize_sci = 0;  */
134
135 /* Don't attempt to verify invocations.  */
136 int flag_verify_invocations = 0; 
137
138 /* When nonzero, print extra version information.  */
139 static int v_flag = 0;
140
141 JCF *current_jcf;
142
143 /* Variable controlling how dependency tracking is enabled in
144    java_init.  */
145 static int dependency_tracking = 0;
146
147 /* Flag values for DEPENDENCY_TRACKING.  */
148 #define DEPEND_SET_FILE 1
149 #define DEPEND_ENABLE   2
150 #define DEPEND_TARGET_SET 4
151 #define DEPEND_FILE_ALREADY_SET 8
152
153 struct language_function GTY(())
154 {
155   int unused;
156 };
157
158 #undef LANG_HOOKS_NAME
159 #define LANG_HOOKS_NAME "GNU Java"
160 #undef LANG_HOOKS_INIT
161 #define LANG_HOOKS_INIT java_init
162 #undef LANG_HOOKS_FINISH
163 #define LANG_HOOKS_FINISH java_finish
164 #undef LANG_HOOKS_INIT_OPTIONS
165 #define LANG_HOOKS_INIT_OPTIONS java_init_options
166 #undef LANG_HOOKS_HANDLE_OPTION
167 #define LANG_HOOKS_HANDLE_OPTION java_handle_option
168 #undef LANG_HOOKS_POST_OPTIONS
169 #define LANG_HOOKS_POST_OPTIONS java_post_options
170 #undef LANG_HOOKS_PARSE_FILE
171 #define LANG_HOOKS_PARSE_FILE java_parse_file
172 #undef LANG_HOOKS_MARK_ADDRESSABLE
173 #define LANG_HOOKS_MARK_ADDRESSABLE java_mark_addressable
174 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
175 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
176 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
177 #define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
178 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
179 #define LANG_HOOKS_PRINT_ERROR_FUNCTION java_print_error_function
180
181 #undef LANG_HOOKS_TYPE_FOR_MODE
182 #define LANG_HOOKS_TYPE_FOR_MODE java_type_for_mode
183 #undef LANG_HOOKS_TYPE_FOR_SIZE
184 #define LANG_HOOKS_TYPE_FOR_SIZE java_type_for_size
185
186 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
187 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN java_dump_tree
188
189 #undef LANG_HOOKS_GIMPLIFY_EXPR
190 #define LANG_HOOKS_GIMPLIFY_EXPR java_gimplify_expr
191
192 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
193 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES java_tree_inlining_walk_subtrees
194
195 #undef LANG_HOOKS_DECL_OK_FOR_SIBCALL
196 #define LANG_HOOKS_DECL_OK_FOR_SIBCALL java_decl_ok_for_sibcall
197
198 #undef LANG_HOOKS_GET_CALLEE_FNDECL
199 #define LANG_HOOKS_GET_CALLEE_FNDECL java_get_callee_fndecl
200
201 #undef LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION
202 #define LANG_HOOKS_CALLGRAPH_EXPAND_FUNCTION java_expand_body
203
204 #undef LANG_HOOKS_CLEAR_BINDING_STACK
205 #define LANG_HOOKS_CLEAR_BINDING_STACK java_clear_binding_stack
206
207 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
208 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME java_mangle_decl
209
210 #undef LANG_HOOKS_ATTRIBUTE_TABLE
211 #define LANG_HOOKS_ATTRIBUTE_TABLE java_attribute_table
212
213 /* Each front end provides its own.  */
214 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
215
216 /*
217  * process java-specific compiler command-line options
218  * return 0, but do not complain if the option is not recognized.
219  */
220 static int
221 java_handle_option (size_t scode, const char *arg, int value)
222 {
223   enum opt_code code = (enum opt_code) scode;
224
225   switch (code)
226     {
227     case OPT_I:
228       jcf_path_include_arg (arg);
229       break;
230
231     case OPT_M:
232       jcf_dependency_init (1);
233       dependency_tracking |= DEPEND_ENABLE;
234       break;
235
236     case OPT_MD_:
237       jcf_dependency_init (1);
238       dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
239       break;
240
241     case OPT_MF:
242       jcf_dependency_set_dep_file (arg);
243       dependency_tracking |= DEPEND_FILE_ALREADY_SET;
244       break;
245
246     case OPT_MM:
247       jcf_dependency_init (0);
248       dependency_tracking |= DEPEND_ENABLE;
249       break;
250
251     case OPT_MMD_:
252       jcf_dependency_init (0);
253       dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
254       break;
255
256     case OPT_MP:
257       jcf_dependency_print_dummies ();
258       break;
259
260     case OPT_MT:
261       jcf_dependency_set_target (arg);
262       dependency_tracking |= DEPEND_TARGET_SET;
263       break;
264
265     case OPT_Wall:
266       flag_wall = value;
267       /* When -Wall given, enable -Wunused.  We do this because the C
268          compiler does it, and people expect it.  */
269       set_Wunused (value);
270       break;
271
272     case OPT_fenable_assertions_:
273       add_enable_assert (arg, value);
274       break;
275
276     case OPT_fenable_assertions:
277       add_enable_assert ("", value);
278       break;
279
280     case OPT_fdisable_assertions_:
281       add_enable_assert (arg, !value);
282       break;
283
284     case OPT_fdisable_assertions:
285       add_enable_assert ("", !value);
286       break;
287
288     case OPT_fassume_compiled_:
289       add_assume_compiled (arg, !value);
290       break;
291
292     case OPT_fassume_compiled:
293       add_assume_compiled ("", !value);
294       break;
295
296     case OPT_fbootclasspath_:
297       jcf_path_bootclasspath_arg (arg);
298       break;
299
300     case OPT_faux_classpath:
301     case OPT_fclasspath_:
302     case OPT_fCLASSPATH_:
303       jcf_path_classpath_arg (arg);
304       break;
305
306     case OPT_fcompile_resource_:
307       resource_name = arg;
308       break;
309
310     case OPT_fdump_:
311       if (!dump_switch_p (arg))
312         return 0;
313       break;
314
315     case OPT_fencoding_:
316       /* Nothing.  */
317       break;
318
319     case OPT_fextdirs_:
320       jcf_path_extdirs_arg (arg);
321       break;
322
323     case OPT_foutput_class_dir_:
324       /* FIXME: remove; this is handled by ecj1 now.  */
325       break;
326
327     case OPT_version:
328       v_flag = 1;
329       break;
330       
331     case OPT_fsource_filename_:
332       java_read_sourcefilenames (arg);
333       break;
334       
335     default:
336       if (cl_options[code].flags & CL_Java)
337         break;
338       gcc_unreachable ();
339     }
340
341   return 1;
342 }
343
344 /* Global open file.  */
345 FILE *finput;
346
347 static bool
348 java_init (void)
349 {
350   /* FIXME: Indirect dispatch isn't yet compatible with static class
351      init optimization.  */
352   if (flag_indirect_dispatch)
353     always_initialize_class_p = true;
354
355   if (!flag_indirect_dispatch)
356     flag_indirect_classes = false;
357
358   jcf_path_seal (v_flag);
359
360   java_init_decl_processing ();
361
362   using_eh_for_cleanups ();
363
364   return true;
365 }
366
367 static void
368 java_finish (void)
369 {
370   jcf_dependency_write ();
371 }
372
373 /* Buffer used by lang_printable_name. */
374 static char *decl_buf = NULL;
375
376 /* Allocated size of decl_buf. */
377 static int decl_buflen = 0;
378
379 /* Length of used part of decl_buf;  position for next character. */
380 static int decl_bufpos = 0;
381
382 /* Append the string STR to decl_buf.
383    It length is given by LEN;  -1 means the string is nul-terminated. */
384
385 static void
386 put_decl_string (const char *str, int len)
387 {
388   if (len < 0)
389     len = strlen (str);
390   if (decl_bufpos + len >= decl_buflen)
391     {
392       if (decl_buf == NULL)
393         {
394           decl_buflen = len + 100;
395           decl_buf = XNEWVEC (char, decl_buflen);
396         }
397       else
398         {
399           decl_buflen *= 2;
400           decl_buf = xrealloc (decl_buf, decl_buflen);
401         }
402     }
403   strcpy (decl_buf + decl_bufpos, str);
404   decl_bufpos += len;
405 }
406
407 /* Append to decl_buf a printable name for NODE. */
408
409 static void
410 put_decl_node (tree node)
411 {
412   int was_pointer = 0;
413   if (TREE_CODE (node) == POINTER_TYPE)
414     {
415       node = TREE_TYPE (node);
416       was_pointer = 1;
417     }
418   if (DECL_P (node) && DECL_NAME (node) != NULL_TREE)
419     {
420       if (TREE_CODE (node) == FUNCTION_DECL)
421         {
422           /* We want to print the type the DECL belongs to. We don't do
423              that when we handle constructors. */
424           if (! DECL_CONSTRUCTOR_P (node)
425               && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node))
426             {
427               put_decl_node (TYPE_NAME (DECL_CONTEXT (node)));
428               put_decl_string (".", 1);
429             }
430           if (! DECL_CONSTRUCTOR_P (node))
431             put_decl_node (DECL_NAME (node));
432           if (TREE_TYPE (node) != NULL_TREE)
433             {
434               int i = 0;
435               tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
436               if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
437                 args = TREE_CHAIN (args);
438               put_decl_string ("(", 1);
439               for ( ; args != end_params_node;  args = TREE_CHAIN (args), i++)
440                 {
441                   if (i > 0)
442                     put_decl_string (",", 1);
443                   put_decl_node (TREE_VALUE (args));
444                 }
445               put_decl_string (")", 1);
446             }
447         }
448       else
449         put_decl_node (DECL_NAME (node));
450     }
451   else if (TYPE_P (node) && TYPE_NAME (node) != NULL_TREE)
452     {
453       if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node))
454         {
455           put_decl_node (TYPE_ARRAY_ELEMENT (node));
456           put_decl_string("[]", 2);
457         }
458       else if (node == promoted_byte_type_node)
459         put_decl_string ("byte", 4);
460       else if (node == promoted_short_type_node)
461         put_decl_string ("short", 5);
462       else if (node == promoted_char_type_node)
463         put_decl_string ("char", 4);
464       else if (node == promoted_boolean_type_node)
465         put_decl_string ("boolean", 7);
466       else if (node == void_type_node && was_pointer)
467         put_decl_string ("null", 4);
468       else
469         put_decl_node (TYPE_NAME (node));
470     }
471   else if (TREE_CODE (node) == IDENTIFIER_NODE)
472     put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
473   else
474     put_decl_string ("<unknown>", -1);
475 }
476
477 /* Return a user-friendly name for DECL.
478    The resulting string is only valid until the next call.
479    The value of the hook decl_printable_name is this function,
480    which is also called directly by java_print_error_function. */
481
482 const char *
483 lang_printable_name (tree decl, int v)
484 {
485   decl_bufpos = 0;
486   if (v == 0 && TREE_CODE (decl) == FUNCTION_DECL)
487     put_decl_node (DECL_NAME (decl));
488   else
489     put_decl_node (decl);
490   put_decl_string ("", 1);
491   return decl_buf;
492 }
493
494 /* Print on stderr the current class and method context.  This function
495    is the value of the hook print_error_function. */
496
497 static GTY(()) tree last_error_function_context;
498 static GTY(()) tree last_error_function;
499 static void
500 java_print_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
501                            const char *file)
502 {
503   /* Don't print error messages with bogus function prototypes.  */
504   if (inhibit_error_function_printing)
505     return;
506
507   if (current_function_decl != NULL
508       && DECL_CONTEXT (current_function_decl) != last_error_function_context)
509     {
510       if (file)
511         fprintf (stderr, "%s: ", file);
512
513       last_error_function_context = DECL_CONTEXT (current_function_decl);
514       fprintf (stderr, "In class '%s':\n",
515                lang_printable_name (last_error_function_context, 0));
516     }
517   if (last_error_function != current_function_decl)
518     {
519       if (file)
520         fprintf (stderr, "%s: ", file);
521
522       if (current_function_decl == NULL)
523         fprintf (stderr, "At top level:\n");
524       else
525         {
526           const char *name = lang_printable_name (current_function_decl, 2);
527           fprintf (stderr, "In %s '%s':\n",
528                    (DECL_CONSTRUCTOR_P (current_function_decl) ? "constructor"
529                     : "method"),
530                    name);
531         }
532
533       last_error_function = current_function_decl;
534     }
535
536 }
537
538 /* Called to install the PRINT_ERROR_FUNCTION hook differently
539    according to LEVEL. LEVEL is 1 during early parsing, when function
540    prototypes aren't fully resolved. java_print_error_function is set
541    so it doesn't print incomplete function prototypes. When LEVEL is
542    2, function prototypes are fully resolved and can be printed when
543    reporting errors.  */
544
545 void
546 lang_init_source (int level)
547 {
548   inhibit_error_function_printing = (level == 1);
549 }
550
551 static unsigned int
552 java_init_options (unsigned int argc ATTRIBUTE_UNUSED,
553                    const char **argv ATTRIBUTE_UNUSED)
554 {
555   flag_bounds_check = 1;
556   flag_exceptions = 1;
557   flag_non_call_exceptions = 1;
558
559   /* In Java floating point operations never trap.  */
560   flag_trapping_math = 0;
561
562   /* In Java arithmetic overflow always wraps around.  */
563   flag_wrapv = 1;
564
565   /* Java requires left-to-right evaluation of subexpressions.  */
566   flag_evaluation_order = 1;
567
568   /* Unit at a time is disabled for Java because it is considered
569      too expensive.  */
570   no_unit_at_a_time_default = 1;
571
572   jcf_path_init ();
573
574   return CL_Java;
575 }
576
577 /* Post-switch processing.  */
578 static bool
579 java_post_options (const char **pfilename)
580 {
581   const char *filename = *pfilename;
582
583   /* Use tree inlining.  */
584   if (!flag_no_inline)
585     flag_no_inline = 1;
586   if (flag_inline_functions)
587     flag_inline_trees = 2;
588
589   /* An absolute requirement: if we're not using indirect dispatch, we
590      must always verify everything.  */
591   if (! flag_indirect_dispatch)
592     flag_verify_invocations = true;
593
594   if (flag_reduced_reflection)
595     {
596       if (flag_indirect_dispatch)
597         error ("-findirect-dispatch is incompatible "
598                "with -freduced-reflection");
599       if (flag_jni)
600         error ("-fjni is incompatible with -freduced-reflection");
601     }
602
603   /* Open input file.  */
604
605   if (filename == 0 || !strcmp (filename, "-"))
606     {
607       finput = stdin;
608       filename = "stdin";
609
610       if (dependency_tracking)
611         error ("can't do dependency tracking with input from stdin");
612     }
613   else
614     {
615       if (dependency_tracking)
616         {
617           char *dot;
618
619           /* If the target is set and the output filename is set, then
620              there's no processing to do here.  Otherwise we must
621              compute one or the other.  */
622           if (! ((dependency_tracking & DEPEND_TARGET_SET)
623                  && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
624             {
625               dot = strrchr (filename, '.');
626               if (dot == NULL)
627                 error ("couldn't determine target name for dependency tracking");
628               else
629                 {
630                   char *buf = XNEWVEC (char, dot - filename +
631                                        3 + sizeof (TARGET_OBJECT_SUFFIX));
632                   strncpy (buf, filename, dot - filename);
633
634                   /* If emitting class files, we might have multiple
635                      targets.  The class generation code takes care of
636                      registering them.  Otherwise we compute the
637                      target name here.  */
638                   if ((dependency_tracking & DEPEND_TARGET_SET))
639                     ; /* Nothing.  */
640                   else
641                     {
642                       strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
643                       jcf_dependency_set_target (buf);
644                     }
645
646                   if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
647                     ; /* Nothing.  */
648                   else if ((dependency_tracking & DEPEND_SET_FILE))
649                     {
650                       strcpy (buf + (dot - filename), ".d");
651                       jcf_dependency_set_dep_file (buf);
652                     }
653                   else
654                     jcf_dependency_set_dep_file ("-");
655
656                   free (buf);
657                 }
658             }
659         }
660     }
661 #ifdef USE_MAPPED_LOCATION
662   linemap_add (&line_table, LC_ENTER, false, filename, 0);
663   linemap_add (&line_table, LC_RENAME, false, "<built-in>", 0);
664 #endif
665
666   /* Initialize the compiler back end.  */
667   return false;
668 }
669
670 /* Return either DECL or its known constant value (if it has one).  */
671
672 tree
673 decl_constant_value (tree decl)
674 {
675   if (/* Don't change a variable array bound or initial value to a constant
676          in a place where a variable is invalid.  */
677       current_function_decl != 0
678       && ! TREE_THIS_VOLATILE (decl)
679       && TREE_READONLY (decl)
680       && DECL_INITIAL (decl) != 0
681       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
682       /* This is invalid if initial value is not constant.
683          If it has either a function call, a memory reference,
684          or a variable, then re-evaluating it could give different results.  */
685       && TREE_CONSTANT (DECL_INITIAL (decl))
686       /* Check for cases where this is sub-optimal, even though valid.  */
687       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
688     return DECL_INITIAL (decl);
689   return decl;
690 }
691
692 /* Walk the language specific tree nodes during inlining.  */
693
694 static tree
695 java_tree_inlining_walk_subtrees (tree *tp ATTRIBUTE_UNUSED,
696                                   int *subtrees ATTRIBUTE_UNUSED,
697                                   walk_tree_fn func ATTRIBUTE_UNUSED,
698                                   void *data ATTRIBUTE_UNUSED,
699                                   struct pointer_set_t *pset ATTRIBUTE_UNUSED)
700 {
701   enum tree_code code;
702   tree result;
703
704 #define WALK_SUBTREE(NODE)                              \
705   do                                                    \
706     {                                                   \
707       result = walk_tree (&(NODE), func, data, pset);   \
708       if (result)                                       \
709         return result;                                  \
710     }                                                   \
711   while (0)
712
713   tree t = *tp;
714   if (!t)
715     return NULL_TREE;
716
717   code = TREE_CODE (t);
718   switch (code)
719     {
720     case BLOCK:
721       WALK_SUBTREE (BLOCK_EXPR_BODY (t));
722       return NULL_TREE;
723
724     default:
725       return NULL_TREE;
726     }
727
728   #undef WALK_SUBTREE
729 }
730
731 /* Every call to a static constructor has an associated boolean
732    variable which is in the outermost scope of the calling method.
733    This variable is used to avoid multiple calls to the static
734    constructor for each class.
735
736    It looks something like this:
737
738    foo ()
739    {
740       boolean dummy = OtherClass.is_initialized;
741
742      ...
743
744      if (! dummy)
745        OtherClass.initialize();
746
747      ... use OtherClass.data ...
748    }
749
750    Each of these boolean variables has an entry in the
751    DECL_FUNCTION_INIT_TEST_TABLE of a method.  When inlining a method
752    we must merge the DECL_FUNCTION_INIT_TEST_TABLE from the function
753    being inlined and create the boolean variables in the outermost
754    scope of the method being inlined into.  */
755
756 /* Create a mapping from a boolean variable in a method being inlined
757    to one in the scope of the method being inlined into.  */
758
759 static int
760 merge_init_test_initialization (void **entry, void *x)
761 {
762   struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
763   splay_tree decl_map = (splay_tree)x;
764   splay_tree_node n;
765   tree *init_test_decl;
766
767   /* See if we have remapped this declaration.  If we haven't there's
768      a bug in the inliner.  */
769   n = splay_tree_lookup (decl_map, (splay_tree_key) ite->value);
770   gcc_assert (n);
771
772   /* Create a new entry for the class and its remapped boolean
773      variable.  If we already have a mapping for this class we've
774      already initialized it, so don't overwrite the value.  */
775   init_test_decl = java_treetreehash_new
776     (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
777   if (!*init_test_decl)
778     *init_test_decl = (tree)n->value;
779
780   /* This fixes a weird case.
781
782   The front end assumes that once we have called a method that
783   initializes some class, we can assume the class is initialized.  It
784   does this by setting the DECL_INITIAL of the init_test_decl for that
785   class, and no initializations are emitted for that class.
786
787   However, what if the method that is supposed to do the initialization
788   is itself inlined in the caller?  When expanding the called method
789   we'll assume that the class initialization has already been done,
790   because the DECL_INITIAL of the init_test_decl is set.
791
792   To fix this we remove the DECL_INITIAL (in the caller scope) of all
793   the init_test_decls corresponding to classes initialized by the
794   inlined method.  This makes the caller no longer assume that the
795   method being inlined does any class initializations.  */
796   DECL_INITIAL (*init_test_decl) = NULL;
797
798   return true;
799 }
800
801 /* Merge the DECL_FUNCTION_INIT_TEST_TABLE from the function we're
802    inlining.  */
803
804 void
805 java_inlining_merge_static_initializers (tree fn, void *decl_map)
806 {
807   htab_traverse
808     (DECL_FUNCTION_INIT_TEST_TABLE (fn),
809      merge_init_test_initialization, decl_map);
810 }
811
812 /* Lookup a DECL_FUNCTION_INIT_TEST_TABLE entry in the method we're
813    inlining into.  If we already have a corresponding entry in that
814    class we don't need to create another one, so we create a mapping
815    from the variable in the inlined class to the corresponding
816    pre-existing one.  */
817
818 static int
819 inline_init_test_initialization (void **entry, void *x)
820 {
821   struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
822   splay_tree decl_map = (splay_tree)x;
823
824   tree h = java_treetreehash_find
825     (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
826   if (! h)
827     return true;
828   splay_tree_insert (decl_map,
829                      (splay_tree_key) ite->value,
830                      (splay_tree_value) h);
831   return true;
832 }
833
834 /* Look up the boolean variables in the DECL_FUNCTION_INIT_TEST_TABLE
835    of a method being inlined.  For each hone, if we already have a
836    variable associated with the same class in the method being inlined
837    into, create a new mapping for it.  */
838
839 void
840 java_inlining_map_static_initializers (tree fn, void *decl_map)
841 {
842   htab_traverse
843     (DECL_FUNCTION_INIT_TEST_TABLE (fn),
844      inline_init_test_initialization, decl_map);
845 }
846
847 /* Avoid voluminous output for deep recursion of compound exprs.  */
848
849 static void
850 dump_compound_expr (dump_info_p di, tree t)
851 {
852   int i;
853
854   for (i=0; i<2; i++)
855     {
856       switch (TREE_CODE (TREE_OPERAND (t, i)))
857         {
858         case COMPOUND_EXPR:
859           dump_compound_expr (di, TREE_OPERAND (t, i));
860           break;
861
862         default:
863           dump_child ("expr", TREE_OPERAND (t, i));
864         }
865     }
866 }
867
868 static bool
869 java_dump_tree (void *dump_info, tree t)
870 {
871   enum tree_code code;
872   dump_info_p di = (dump_info_p) dump_info;
873
874   /* Figure out what kind of node this is.  */
875   code = TREE_CODE (t);
876
877   switch (code)
878     {
879     case FUNCTION_DECL:
880       dump_child ("args", DECL_ARGUMENTS (t));
881       if (DECL_EXTERNAL (t))
882         dump_string (di, "undefined");
883       if (TREE_PUBLIC (t))
884         dump_string (di, "extern");
885       else
886         dump_string (di, "static");
887       if (DECL_LANG_SPECIFIC (t))
888         dump_child ("body", DECL_FUNCTION_BODY (t));
889       if (DECL_LANG_SPECIFIC (t) && !dump_flag (di, TDF_SLIM, t))
890         dump_child ("inline body", DECL_SAVED_TREE (t));
891       return true;
892
893     case RETURN_EXPR:
894       dump_child ("expr", TREE_OPERAND (t, 0));
895       return true;
896
897     case GOTO_EXPR:
898       dump_child ("goto", TREE_OPERAND (t, 0));
899       return true;
900
901     case LABEL_EXPR:
902       dump_child ("label", TREE_OPERAND (t, 0));
903       return true;
904
905     case BLOCK:
906       if (BLOCK_EXPR_BODY (t))
907         {
908           tree local = BLOCK_VARS (t);
909           while (local)
910             {
911               tree next = TREE_CHAIN (local);
912               dump_child ("var", local);
913               local = next;
914             }
915
916           {
917             tree block = BLOCK_EXPR_BODY (t);
918             dump_child ("body", block);
919             block = TREE_CHAIN (block);
920           }
921         }
922       return true;
923
924     case COMPOUND_EXPR:
925       if (!dump_flag (di, TDF_SLIM, t))
926         return false;
927       dump_compound_expr (di, t);
928       return true;
929
930     default:
931       break;
932     }
933   return false;
934 }
935
936 /* Java calls can't, in general, be sibcalls because we need an
937    accurate stack trace in order to guarantee correct operation of
938    methods such as Class.forName(String) and
939    SecurityManager.getClassContext().  */
940
941 static bool
942 java_decl_ok_for_sibcall (tree decl)
943 {
944   return (decl != NULL && DECL_CONTEXT (decl) == output_class
945           && DECL_INLINE (decl));
946 }
947
948 /* Given a call_expr, try to figure out what its target might be.  In
949    the case of an indirection via the atable, search for the decl.  If
950    the decl is external, we return NULL.  If we don't, the optimizer
951    will replace the indirection with a direct call, which undoes the
952    purpose of the atable indirection.  */
953 static tree
954 java_get_callee_fndecl (tree call_expr)
955 {
956   tree method, table, element, atable_methods;
957
958   HOST_WIDE_INT index;
959
960   /* FIXME: This is disabled because we end up passing calls through
961      the PLT, and we do NOT want to do that.  */
962   return NULL;
963
964   if (TREE_CODE (call_expr) != CALL_EXPR)
965     return NULL;
966   method = CALL_EXPR_FN (call_expr);
967   STRIP_NOPS (method);
968   if (TREE_CODE (method) != ARRAY_REF)
969     return NULL;
970   table = TREE_OPERAND (method, 0);
971   if (! DECL_LANG_SPECIFIC(table)
972       || !DECL_OWNER (table)
973       || TYPE_ATABLE_DECL (DECL_OWNER (table)) != table)
974     return NULL;
975
976   atable_methods = TYPE_ATABLE_METHODS (DECL_OWNER (table));
977   index = TREE_INT_CST_LOW (TREE_OPERAND (method, 1));
978
979   /* FIXME: Replace this for loop with a hash table lookup.  */
980   for (element = atable_methods; element; element = TREE_CHAIN (element))
981     {
982       if (index == 1)
983         {
984           tree purpose = TREE_PURPOSE (element);
985           if (TREE_CODE (purpose) == FUNCTION_DECL
986               && ! DECL_EXTERNAL (purpose))
987             return purpose;
988           else
989             return NULL;
990         }
991       --index;
992     }
993
994   return NULL;
995 }
996
997
998 /* Clear the binding stack.  */
999 static void
1000 java_clear_binding_stack (void)
1001 {
1002   while (!global_bindings_p ())
1003     poplevel (0, 0, 0);
1004 }
1005
1006 #include "gt-java-lang.h"