1 /* Java(TM) language-specific utility routines.
2    Copyright (C) 1996-2016 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10 
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.
19 
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc.  */
23 
24 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
25 
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "target.h"
30 #include "tree.h"
31 #include "diagnostic.h"
32 #include "java-tree.h"
33 #include "jcf.h"
34 #include "langhooks.h"
35 #include "langhooks-def.h"
36 #include "tree-dump.h"
37 #include "opts.h"
38 #include "context.h"
39 
40 static bool java_init (void);
41 static void java_finish (void);
42 static unsigned int java_option_lang_mask (void);
43 static void java_init_options_struct (struct gcc_options *);
44 static void java_init_options (unsigned int, struct cl_decoded_option *);
45 static bool java_post_options (const char **);
46 
47 static bool java_handle_option (size_t, const char *, int, int, location_t,
48 				const struct cl_option_handlers *);
49 static void put_decl_string (const char *, int);
50 static void put_decl_node (tree, int);
51 static void java_print_error_function (diagnostic_context *, const char *,
52 				       diagnostic_info *);
53 static bool java_dump_tree (void *, tree);
54 static void dump_compound_expr (dump_info_p, tree);
55 static bool java_decl_ok_for_sibcall (const_tree);
56 
57 static enum classify_record java_classify_record (tree type);
58 
59 static tree java_eh_personality (void);
60 
61 #ifndef TARGET_OBJECT_SUFFIX
62 # define TARGET_OBJECT_SUFFIX ".o"
63 #endif
64 
65 /* Table of machine-independent attributes.  */
66 const struct attribute_spec java_attribute_table[] =
67 {
68  { "nonnull",                0, -1, false, true, true,
69 			      NULL, false },
70   { NULL,                     0, 0, false, false, false, NULL, false }
71 };
72 
73 /* Used to avoid printing error messages with bogus function
74    prototypes.  Starts out false.  */
75 static bool inhibit_error_function_printing;
76 
77 const char *resource_name;
78 
79 /* When nonzero, -Wall was turned on.  */
80 int flag_wall = 0;
81 
82 /* When nonzero, report use of deprecated classes, methods, or fields.  */
83 int flag_deprecated = 1;
84 
85 /* When zero, don't optimize static class initialization. This flag shouldn't
86    be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead.  */
87 /* FIXME: Make this work with gimplify.  */
88 /* int flag_optimize_sci = 0;  */
89 
90 /* Don't attempt to verify invocations.  */
91 int flag_verify_invocations = 0;
92 
93 /* When nonzero, print extra version information.  */
94 static int v_flag = 0;
95 
96 JCF *current_jcf;
97 
98 /* Variable controlling how dependency tracking is enabled in
99    java_init.  */
100 static int dependency_tracking = 0;
101 
102 /* Flag values for DEPENDENCY_TRACKING.  */
103 #define DEPEND_SET_FILE 1
104 #define DEPEND_ENABLE   2
105 #define DEPEND_TARGET_SET 4
106 #define DEPEND_FILE_ALREADY_SET 8
107 
108 struct GTY(()) language_function {
109   int unused;
110 };
111 
112 #undef LANG_HOOKS_NAME
113 #define LANG_HOOKS_NAME "GNU Java"
114 #undef LANG_HOOKS_INIT
115 #define LANG_HOOKS_INIT java_init
116 #undef LANG_HOOKS_FINISH
117 #define LANG_HOOKS_FINISH java_finish
118 #undef LANG_HOOKS_OPTION_LANG_MASK
119 #define LANG_HOOKS_OPTION_LANG_MASK java_option_lang_mask
120 #undef LANG_HOOKS_INIT_OPTIONS_STRUCT
121 #define LANG_HOOKS_INIT_OPTIONS_STRUCT java_init_options_struct
122 #undef LANG_HOOKS_INIT_OPTIONS
123 #define LANG_HOOKS_INIT_OPTIONS java_init_options
124 #undef LANG_HOOKS_HANDLE_OPTION
125 #define LANG_HOOKS_HANDLE_OPTION java_handle_option
126 #undef LANG_HOOKS_POST_OPTIONS
127 #define LANG_HOOKS_POST_OPTIONS java_post_options
128 #undef LANG_HOOKS_PARSE_FILE
129 #define LANG_HOOKS_PARSE_FILE java_parse_file
130 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
131 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
132 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
133 #define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
134 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
135 #define LANG_HOOKS_PRINT_ERROR_FUNCTION	java_print_error_function
136 
137 #undef LANG_HOOKS_TYPE_FOR_MODE
138 #define LANG_HOOKS_TYPE_FOR_MODE java_type_for_mode
139 #undef LANG_HOOKS_TYPE_FOR_SIZE
140 #define LANG_HOOKS_TYPE_FOR_SIZE java_type_for_size
141 #undef LANG_HOOKS_CLASSIFY_RECORD
142 #define LANG_HOOKS_CLASSIFY_RECORD java_classify_record
143 
144 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
145 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN java_dump_tree
146 
147 #undef LANG_HOOKS_GIMPLIFY_EXPR
148 #define LANG_HOOKS_GIMPLIFY_EXPR java_gimplify_expr
149 
150 #undef LANG_HOOKS_DECL_OK_FOR_SIBCALL
151 #define LANG_HOOKS_DECL_OK_FOR_SIBCALL java_decl_ok_for_sibcall
152 
153 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
154 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME java_mangle_decl
155 
156 #undef LANG_HOOKS_ATTRIBUTE_TABLE
157 #define LANG_HOOKS_ATTRIBUTE_TABLE java_attribute_table
158 
159 #undef LANG_HOOKS_EH_PERSONALITY
160 #define LANG_HOOKS_EH_PERSONALITY java_eh_personality
161 
162 #undef LANG_HOOKS_EH_USE_CXA_END_CLEANUP
163 #define LANG_HOOKS_EH_USE_CXA_END_CLEANUP  true
164 
165 /* Each front end provides its own.  */
166 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
167 
168 /*
169  * process java-specific compiler command-line options
170  * return false, but do not complain if the option is not recognized.
171  */
172 static bool
java_handle_option(size_t scode,const char * arg,int value,int kind ATTRIBUTE_UNUSED,location_t loc ATTRIBUTE_UNUSED,const struct cl_option_handlers * handlers ATTRIBUTE_UNUSED)173 java_handle_option (size_t scode, const char *arg, int value,
174 		    int kind ATTRIBUTE_UNUSED, location_t loc ATTRIBUTE_UNUSED,
175 		    const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED)
176 {
177   enum opt_code code = (enum opt_code) scode;
178 
179   switch (code)
180     {
181     case OPT_I:
182       jcf_path_include_arg (arg);
183       break;
184 
185     case OPT_M:
186       jcf_dependency_init (1);
187       dependency_tracking |= DEPEND_ENABLE;
188       break;
189 
190     case OPT_MD_:
191       jcf_dependency_init (1);
192       dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
193       break;
194 
195     case OPT_MF:
196       jcf_dependency_set_dep_file (arg);
197       dependency_tracking |= DEPEND_FILE_ALREADY_SET;
198       break;
199 
200     case OPT_MM:
201       jcf_dependency_init (0);
202       dependency_tracking |= DEPEND_ENABLE;
203       break;
204 
205     case OPT_MMD_:
206       jcf_dependency_init (0);
207       dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
208       break;
209 
210     case OPT_MP:
211       jcf_dependency_print_dummies ();
212       break;
213 
214     case OPT_MT:
215       jcf_dependency_set_target (arg);
216       dependency_tracking |= DEPEND_TARGET_SET;
217       break;
218 
219     case OPT_Wall:
220       flag_wall = value;
221       /* When -Wall given, enable -Wunused.  We do this because the C
222 	 compiler does it, and people expect it.  */
223       warn_unused = value;
224       break;
225 
226     case OPT_fenable_assertions_:
227       add_enable_assert (arg, value);
228       break;
229 
230     case OPT_fenable_assertions:
231       add_enable_assert ("", value);
232       break;
233 
234     case OPT_fdisable_assertions_:
235       add_enable_assert (arg, !value);
236       break;
237 
238     case OPT_fdisable_assertions:
239       add_enable_assert ("", !value);
240       break;
241 
242     case OPT_fassume_compiled_:
243       add_assume_compiled (arg, !value);
244       break;
245 
246     case OPT_fassume_compiled:
247       add_assume_compiled ("", !value);
248       break;
249 
250     case OPT_fbootclasspath_:
251       jcf_path_bootclasspath_arg (arg);
252       break;
253 
254     case OPT_faux_classpath:
255     case OPT_fclasspath_:
256       jcf_path_classpath_arg (arg);
257       break;
258 
259     case OPT_fcompile_resource_:
260       resource_name = arg;
261       break;
262 
263     case OPT_fdump_:
264       if (!g->get_dumps ()->dump_switch_p (arg))
265 	return false;
266       break;
267 
268     case OPT_fencoding_:
269       /* Nothing.  */
270       break;
271 
272     case OPT_fextdirs_:
273       jcf_path_extdirs_arg (arg);
274       break;
275 
276     case OPT_foutput_class_dir_:
277       /* FIXME: remove; this is handled by ecj1 now.  */
278       break;
279 
280     case OPT_version:
281       v_flag = 1;
282       break;
283 
284     case OPT_fsource_filename_:
285       java_read_sourcefilenames (arg);
286       break;
287 
288     default:
289       if (cl_options[code].flags & CL_Java)
290 	break;
291       gcc_unreachable ();
292     }
293 
294   return true;
295 }
296 
297 /* Global open file.  */
298 FILE *finput;
299 
300 static bool
java_init(void)301 java_init (void)
302 {
303   /* FIXME: Indirect dispatch isn't yet compatible with static class
304      init optimization.  */
305   if (flag_indirect_dispatch)
306     always_initialize_class_p = true;
307 
308   if (!flag_indirect_dispatch)
309     flag_indirect_classes = false;
310 
311   jcf_path_seal (v_flag);
312 
313   java_init_decl_processing ();
314 
315   using_eh_for_cleanups ();
316 
317   return true;
318 }
319 
320 static void
java_finish(void)321 java_finish (void)
322 {
323   jcf_dependency_write ();
324 }
325 
326 /* Buffer used by lang_printable_name. */
327 static char *decl_buf = NULL;
328 
329 /* Allocated size of decl_buf. */
330 static int decl_buflen = 0;
331 
332 /* Length of used part of decl_buf;  position for next character. */
333 static int decl_bufpos = 0;
334 
335 /* Append the string STR to decl_buf.
336    It length is given by LEN;  -1 means the string is nul-terminated. */
337 
338 static void
put_decl_string(const char * str,int len)339 put_decl_string (const char *str, int len)
340 {
341   if (len < 0)
342     len = strlen (str);
343   if (decl_bufpos + len >= decl_buflen)
344     {
345       if (decl_buf == NULL)
346 	{
347 	  decl_buflen = len + 100;
348 	  decl_buf = XNEWVEC (char, decl_buflen);
349 	}
350       else
351 	{
352 	  decl_buflen *= 2;
353 	  decl_buf = XRESIZEVAR (char, decl_buf, decl_buflen);
354 	}
355     }
356   strcpy (decl_buf + decl_bufpos, str);
357   decl_bufpos += len;
358 }
359 
360 /* Append to decl_buf a printable name for NODE.
361    Depending on VERBOSITY, more information about NODE
362    is printed. Read the comments of decl_printable_name in
363    langhooks.h for more.  */
364 
365 static void
put_decl_node(tree node,int verbosity)366 put_decl_node (tree node, int verbosity)
367 {
368   int was_pointer = 0;
369   if (TREE_CODE (node) == POINTER_TYPE)
370     {
371       node = TREE_TYPE (node);
372       was_pointer = 1;
373     }
374   if (DECL_P (node) && DECL_NAME (node) != NULL_TREE)
375     {
376       if (TREE_CODE (node) == FUNCTION_DECL)
377 	{
378 	  if (verbosity == 0 && DECL_NAME (node))
379 	  /* We have been instructed to just print the bare name
380 	     of the function.  */
381 	    {
382 	      put_decl_node (DECL_NAME (node), 0);
383 	      return;
384 	    }
385 
386 	  /* We want to print the type the DECL belongs to. We don't do
387 	     that when we handle constructors. */
388 	  if (! DECL_CONSTRUCTOR_P (node)
389 	      && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node)
390               /* We want to print qualified DECL names only
391                  if verbosity is higher than 1.  */
392               && verbosity >= 1)
393 	    {
394 	      put_decl_node (TREE_CODE (DECL_CONTEXT (node)) == FUNCTION_DECL
395 			     ? DECL_CONTEXT (node)
396 			     : TYPE_NAME (DECL_CONTEXT (node)),
397                                verbosity);
398 	      put_decl_string (".", 1);
399 	    }
400 	  if (! DECL_CONSTRUCTOR_P (node))
401 	    put_decl_node (DECL_NAME (node), verbosity);
402 	  if (TREE_TYPE (node) != NULL_TREE
403               /* We want to print function parameters only if verbosity
404                  is higher than 2.  */
405               && verbosity >= 2)
406 	    {
407 	      int i = 0;
408 	      tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
409 	      if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
410 		args = TREE_CHAIN (args);
411 	      put_decl_string ("(", 1);
412 	      for ( ; args != NULL_TREE && args != end_params_node;
413 		   args = TREE_CHAIN (args), i++)
414 		{
415 		  if (i > 0)
416 		    put_decl_string (",", 1);
417 		  put_decl_node (TREE_VALUE (args), verbosity);
418 		}
419 	      put_decl_string (")", 1);
420 	    }
421 	}
422       else
423 	put_decl_node (DECL_NAME (node), verbosity);
424     }
425   else if (TYPE_P (node) && TYPE_NAME (node) != NULL_TREE)
426     {
427       if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node)
428           /* Print detailed array information only if verbosity is higher
429             than 2.  */
430           && verbosity >= 2)
431 	{
432 	  put_decl_node (TYPE_ARRAY_ELEMENT (node), verbosity);
433 	  put_decl_string("[]", 2);
434 	}
435       else if (node == promoted_byte_type_node)
436 	put_decl_string ("byte", 4);
437       else if (node == promoted_short_type_node)
438 	put_decl_string ("short", 5);
439       else if (node == promoted_char_type_node)
440 	put_decl_string ("char", 4);
441       else if (node == promoted_boolean_type_node)
442 	put_decl_string ("boolean", 7);
443       else if (node == void_type_node && was_pointer)
444 	put_decl_string ("null", 4);
445       else
446 	put_decl_node (TYPE_NAME (node), verbosity);
447     }
448   else if (TREE_CODE (node) == IDENTIFIER_NODE)
449     put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
450   else
451     put_decl_string ("<unknown>", -1);
452 }
453 
454 /* Return a user-friendly name for DECL.
455    The resulting string is only valid until the next call.
456    The value of the hook decl_printable_name is this function,
457    which is also called directly by java_print_error_function. */
458 
459 const char *
lang_printable_name(tree decl,int v)460 lang_printable_name (tree decl, int v)
461 {
462   decl_bufpos = 0;
463   put_decl_node (decl, v);
464   put_decl_string ("", 1);
465   return decl_buf;
466 }
467 
468 /* Print on stderr the current class and method context.  This function
469    is the value of the hook print_error_function. */
470 
471 static GTY(()) tree last_error_function_context;
472 static GTY(()) tree last_error_function;
473 static void
java_print_error_function(diagnostic_context * context ATTRIBUTE_UNUSED,const char * file,diagnostic_info * diagnostic ATTRIBUTE_UNUSED)474 java_print_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
475 			   const char *file,
476 			   diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
477 {
478   /* Don't print error messages with bogus function prototypes.  */
479   if (inhibit_error_function_printing)
480     return;
481 
482   if (current_function_decl != NULL
483       && DECL_CONTEXT (current_function_decl) != NULL
484       && DECL_CONTEXT (current_function_decl) != last_error_function_context)
485     {
486       if (file)
487 	fprintf (stderr, "%s: ", file);
488 
489       last_error_function_context = DECL_CONTEXT (current_function_decl);
490       fprintf (stderr, "In class '%s':\n",
491 	       lang_printable_name (last_error_function_context, 0));
492     }
493   if (last_error_function != current_function_decl)
494     {
495       if (file)
496 	fprintf (stderr, "%s: ", file);
497 
498       if (current_function_decl == NULL)
499 	fprintf (stderr, "At top level:\n");
500       else
501 	{
502 	  const char *name = lang_printable_name (current_function_decl, 2);
503 	  fprintf (stderr, "In %s '%s':\n",
504 		   (DECL_CONSTRUCTOR_P (current_function_decl) ? "constructor"
505 		    : "method"),
506 		   name);
507 	}
508 
509       last_error_function = current_function_decl;
510     }
511 
512 }
513 
514 /* Called to install the PRINT_ERROR_FUNCTION hook differently
515    according to LEVEL. LEVEL is 1 during early parsing, when function
516    prototypes aren't fully resolved. java_print_error_function is set
517    so it doesn't print incomplete function prototypes. When LEVEL is
518    2, function prototypes are fully resolved and can be printed when
519    reporting errors.  */
520 
521 void
lang_init_source(int level)522 lang_init_source (int level)
523 {
524   inhibit_error_function_printing = (level == 1);
525 }
526 
527 static unsigned int
java_option_lang_mask(void)528 java_option_lang_mask (void)
529 {
530   return CL_Java;
531 }
532 
533 /* Initialize options structure OPTS.  */
534 
535 static void
java_init_options_struct(struct gcc_options * opts)536 java_init_options_struct (struct gcc_options *opts)
537 {
538   opts->x_flag_bounds_check = 1;
539   opts->x_flag_exceptions = 1;
540   opts->x_flag_non_call_exceptions = 1;
541 
542   /* In Java floating point operations never trap.  */
543   opts->x_flag_trapping_math = 0;
544   opts->frontend_set_flag_trapping_math = true;
545 
546   /* In Java arithmetic overflow always wraps around.  */
547   opts->x_flag_wrapv = 1;
548 
549   /* Java requires left-to-right evaluation of subexpressions.  */
550   opts->x_flag_evaluation_order = 1;
551 
552   /* Java catches NULL pointer exceptions, thus we can not necessarily
553      rely on a pointer having a non-NULL value after a dereference.  */
554   opts->x_flag_delete_null_pointer_checks = 0;
555 }
556 
557 static void
java_init_options(unsigned int decoded_options_count ATTRIBUTE_UNUSED,struct cl_decoded_option * decoded_options ATTRIBUTE_UNUSED)558 java_init_options (unsigned int decoded_options_count ATTRIBUTE_UNUSED,
559 		   struct cl_decoded_option *decoded_options ATTRIBUTE_UNUSED)
560 {
561   jcf_path_init ();
562 }
563 
564 /* Post-switch processing.  */
565 static bool
java_post_options(const char ** pfilename)566 java_post_options (const char **pfilename)
567 {
568   const char *filename = *pfilename;
569 
570   /* Excess precision other than "fast" requires front-end
571      support.  */
572   if (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
573       && TARGET_FLT_EVAL_METHOD_NON_DEFAULT)
574     sorry ("-fexcess-precision=standard for Java");
575   flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
576 
577   /* An absolute requirement: if we're not using indirect dispatch, we
578      must always verify everything.  */
579   if (! flag_indirect_dispatch)
580     flag_verify_invocations = true;
581 
582   if (flag_reduced_reflection)
583     {
584       if (flag_indirect_dispatch)
585         error ("-findirect-dispatch is incompatible "
586                "with -freduced-reflection");
587       if (flag_jni)
588         error ("-fjni is incompatible with -freduced-reflection");
589     }
590 
591   /* Open input file.  */
592 
593   if (filename == 0 || !strcmp (filename, "-"))
594     {
595       finput = stdin;
596       filename = "stdin";
597 
598       if (dependency_tracking)
599 	error ("can%'t do dependency tracking with input from stdin");
600     }
601   else
602     {
603       if (dependency_tracking)
604 	{
605 	  const char *dot;
606 
607 	  /* If the target is set and the output filename is set, then
608 	     there's no processing to do here.  Otherwise we must
609 	     compute one or the other.  */
610 	  if (! ((dependency_tracking & DEPEND_TARGET_SET)
611 		 && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
612 	    {
613 	      dot = strrchr (filename, '.');
614 	      if (dot == NULL)
615 		error ("couldn%'t determine target name for dependency tracking");
616 	      else
617 		{
618 		  char *buf = XNEWVEC (char, dot - filename +
619 				       3 + sizeof (TARGET_OBJECT_SUFFIX));
620 		  strncpy (buf, filename, dot - filename);
621 
622 		  /* If emitting class files, we might have multiple
623 		     targets.  The class generation code takes care of
624 		     registering them.  Otherwise we compute the
625 		     target name here.  */
626 		  if ((dependency_tracking & DEPEND_TARGET_SET))
627 		    ; /* Nothing.  */
628 		  else
629 		    {
630 		      strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
631 		      jcf_dependency_set_target (buf);
632 		    }
633 
634 		  if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
635 		    ; /* Nothing.  */
636 		  else if ((dependency_tracking & DEPEND_SET_FILE))
637 		    {
638 		      strcpy (buf + (dot - filename), ".d");
639 		      jcf_dependency_set_dep_file (buf);
640 		    }
641 		  else
642 		    jcf_dependency_set_dep_file ("-");
643 
644 		  free (buf);
645 		}
646 	    }
647 	}
648     }
649   linemap_add (line_table, LC_ENTER, false, filename, 0);
650   linemap_add (line_table, LC_RENAME, false, "<built-in>", 0);
651 
652   /* Initialize the compiler back end.  */
653   return false;
654 }
655 
656 /* Return either DECL or its known constant value (if it has one).  */
657 
658 tree
decl_constant_value(tree decl)659 decl_constant_value (tree decl)
660 {
661   if (/* Don't change a variable array bound or initial value to a constant
662 	 in a place where a variable is invalid.  */
663       current_function_decl != 0
664       && ! TREE_THIS_VOLATILE (decl)
665       && TREE_READONLY (decl)
666       && DECL_INITIAL (decl) != 0
667       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
668       /* This is invalid if initial value is not constant.
669 	 If it has either a function call, a memory reference,
670 	 or a variable, then re-evaluating it could give different results.  */
671       && TREE_CONSTANT (DECL_INITIAL (decl))
672       /* Check for cases where this is sub-optimal, even though valid.  */
673       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
674     return DECL_INITIAL (decl);
675   return decl;
676 }
677 
678 /* Every call to a static constructor has an associated boolean
679    variable which is in the outermost scope of the calling method.
680    This variable is used to avoid multiple calls to the static
681    constructor for each class.
682 
683    It looks something like this:
684 
685    foo ()
686    {
687       boolean dummy = OtherClass.is_initialized;
688 
689      ...
690 
691      if (! dummy)
692        OtherClass.initialize();
693 
694      ... use OtherClass.data ...
695    }
696 
697    Each of these boolean variables has an entry in the
698    DECL_FUNCTION_INIT_TEST_TABLE of a method.  When inlining a method
699    we must merge the DECL_FUNCTION_INIT_TEST_TABLE from the function
700    being inlined and create the boolean variables in the outermost
701    scope of the method being inlined into.  */
702 
703 /* Create a mapping from a boolean variable in a method being inlined
704    to one in the scope of the method being inlined into.  */
705 
706 int
merge_init_test_initialization(treetreehash_entry ** entry,void * x)707 merge_init_test_initialization (treetreehash_entry **entry, void *x)
708 {
709   struct treetreehash_entry *ite = *entry;
710   splay_tree decl_map = (splay_tree)x;
711   splay_tree_node n;
712   tree *init_test_decl;
713 
714   /* See if we have remapped this declaration.  If we haven't there's
715      a bug in the inliner.  */
716   n = splay_tree_lookup (decl_map, (splay_tree_key) ite->value);
717   gcc_assert (n);
718 
719   /* Create a new entry for the class and its remapped boolean
720      variable.  If we already have a mapping for this class we've
721      already initialized it, so don't overwrite the value.  */
722   init_test_decl = java_treetreehash_new
723     (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
724   if (!*init_test_decl)
725     *init_test_decl = (tree)n->value;
726 
727   /* This fixes a weird case.
728 
729   The front end assumes that once we have called a method that
730   initializes some class, we can assume the class is initialized.  It
731   does this by setting the DECL_INITIAL of the init_test_decl for that
732   class, and no initializations are emitted for that class.
733 
734   However, what if the method that is supposed to do the initialization
735   is itself inlined in the caller?  When expanding the called method
736   we'll assume that the class initialization has already been done,
737   because the DECL_INITIAL of the init_test_decl is set.
738 
739   To fix this we remove the DECL_INITIAL (in the caller scope) of all
740   the init_test_decls corresponding to classes initialized by the
741   inlined method.  This makes the caller no longer assume that the
742   method being inlined does any class initializations.  */
743   DECL_INITIAL (*init_test_decl) = NULL;
744 
745   return true;
746 }
747 
748 /* Merge the DECL_FUNCTION_INIT_TEST_TABLE from the function we're
749    inlining.  */
750 
751 void
java_inlining_merge_static_initializers(tree fn,void * decl_map)752 java_inlining_merge_static_initializers (tree fn, void *decl_map)
753 {
754     DECL_FUNCTION_INIT_TEST_TABLE (fn)
755       ->traverse<void *, merge_init_test_initialization> (decl_map);
756 }
757 
758 /* Lookup a DECL_FUNCTION_INIT_TEST_TABLE entry in the method we're
759    inlining into.  If we already have a corresponding entry in that
760    class we don't need to create another one, so we create a mapping
761    from the variable in the inlined class to the corresponding
762    pre-existing one.  */
763 
764 int
inline_init_test_initialization(treetreehash_entry ** entry,void * x)765 inline_init_test_initialization (treetreehash_entry **entry, void *x)
766 {
767   struct treetreehash_entry *ite = *entry;
768   splay_tree decl_map = (splay_tree)x;
769 
770   tree h = java_treetreehash_find
771     (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
772   if (! h)
773     return true;
774   splay_tree_insert (decl_map,
775 		     (splay_tree_key) ite->value,
776 		     (splay_tree_value) h);
777   return true;
778 }
779 
780 /* Look up the boolean variables in the DECL_FUNCTION_INIT_TEST_TABLE
781    of a method being inlined.  For each hone, if we already have a
782    variable associated with the same class in the method being inlined
783    into, create a new mapping for it.  */
784 
785 void
java_inlining_map_static_initializers(tree fn,void * decl_map)786 java_inlining_map_static_initializers (tree fn, void *decl_map)
787 {
788   DECL_FUNCTION_INIT_TEST_TABLE (fn)
789     ->traverse<void *, inline_init_test_initialization> (decl_map);
790 }
791 
792 /* Avoid voluminous output for deep recursion of compound exprs.  */
793 
794 static void
dump_compound_expr(dump_info_p di,tree t)795 dump_compound_expr (dump_info_p di, tree t)
796 {
797   int i;
798 
799   for (i=0; i<2; i++)
800     {
801       switch (TREE_CODE (TREE_OPERAND (t, i)))
802 	{
803 	case COMPOUND_EXPR:
804 	  dump_compound_expr (di, TREE_OPERAND (t, i));
805 	  break;
806 
807 	default:
808 	  dump_child ("expr", TREE_OPERAND (t, i));
809 	}
810     }
811 }
812 
813 static bool
java_dump_tree(void * dump_info,tree t)814 java_dump_tree (void *dump_info, tree t)
815 {
816   enum tree_code code;
817   dump_info_p di = (dump_info_p) dump_info;
818 
819   /* Figure out what kind of node this is.  */
820   code = TREE_CODE (t);
821 
822   switch (code)
823     {
824     case FUNCTION_DECL:
825       dump_child ("args", DECL_ARGUMENTS (t));
826       if (DECL_EXTERNAL (t))
827 	dump_string (di, "undefined");
828       if (TREE_PUBLIC (t))
829 	dump_string (di, "extern");
830       else
831 	dump_string (di, "static");
832       if (DECL_LANG_SPECIFIC (t) && !dump_flag (di, TDF_SLIM, t))
833 	dump_child ("inline body", DECL_SAVED_TREE (t));
834       return true;
835 
836     case RETURN_EXPR:
837       dump_child ("expr", TREE_OPERAND (t, 0));
838       return true;
839 
840     case GOTO_EXPR:
841       dump_child ("goto", TREE_OPERAND (t, 0));
842       return true;
843 
844     case LABEL_EXPR:
845       dump_child ("label", TREE_OPERAND (t, 0));
846       return true;
847 
848     case BLOCK:
849       if (BLOCK_EXPR_BODY (t))
850 	{
851 	  tree local = BLOCK_VARS (t);
852 	  while (local)
853 	    {
854 	      tree next = TREE_CHAIN (local);
855 	      dump_child ("var", local);
856 	      local = next;
857 	    }
858 
859 	  {
860 	    tree block = BLOCK_EXPR_BODY (t);
861 	    dump_child ("body", block);
862 	    block = TREE_CHAIN (block);
863 	  }
864 	}
865       return true;
866 
867     case COMPOUND_EXPR:
868       if (!dump_flag (di, TDF_SLIM, t))
869 	return false;
870       dump_compound_expr (di, t);
871       return true;
872 
873     default:
874       break;
875     }
876   return false;
877 }
878 
879 /* Java calls can't, in general, be sibcalls because we need an
880    accurate stack trace in order to guarantee correct operation of
881    methods such as Class.forName(String) and
882    SecurityManager.getClassContext().  */
883 
884 static bool
java_decl_ok_for_sibcall(const_tree decl)885 java_decl_ok_for_sibcall (const_tree decl)
886 {
887   return (decl != NULL && DECL_CONTEXT (decl) == output_class
888           && !DECL_UNINLINABLE (decl));
889 }
890 
891 static enum classify_record
java_classify_record(tree type)892 java_classify_record (tree type)
893 {
894   if (! CLASS_P (type))
895     return RECORD_IS_STRUCT;
896 
897   if (CLASS_INTERFACE (TYPE_NAME (type)))
898     return RECORD_IS_INTERFACE;
899 
900   return RECORD_IS_CLASS;
901 }
902 
903 static GTY(()) tree java_eh_personality_decl;
904 
905 static tree
java_eh_personality(void)906 java_eh_personality (void)
907 {
908   if (!java_eh_personality_decl)
909     java_eh_personality_decl = build_personality_function ("gcj");
910   return java_eh_personality_decl;
911 }
912 
913 #include "gt-java-lang.h"
914