1 /* Java(TM) language-specific utility routines.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5 This file is part of GCC.
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 3, or (at your option)
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
29 #include "coretypes.h"
35 #include "java-tree.h"
38 #include "langhooks.h"
39 #include "langhooks-def.h"
42 #include "diagnostic.h"
43 #include "tree-inline.h"
44 #include "splay-tree.h"
45 #include "tree-dump.h"
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 **);
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, int);
58 static void java_print_error_function (diagnostic_context *, const char *,
60 static int merge_init_test_initialization (void * *, void *);
61 static int inline_init_test_initialization (void * *, void *);
62 static bool java_dump_tree (void *, tree);
63 static void dump_compound_expr (dump_info_p, tree);
64 static bool java_decl_ok_for_sibcall (const_tree);
66 static enum classify_record java_classify_record (tree type);
68 static tree java_eh_personality (void);
70 #ifndef TARGET_OBJECT_SUFFIX
71 # define TARGET_OBJECT_SUFFIX ".o"
74 /* Table of machine-independent attributes. */
75 const struct attribute_spec java_attribute_table[] =
77 { "nonnull", 0, -1, false, true, true,
79 { NULL, 0, 0, false, false, false, NULL }
82 /* Used to avoid printing error messages with bogus function
83 prototypes. Starts out false. */
84 static bool inhibit_error_function_printing;
86 const char *resource_name;
88 /* When nonzero, -Wall was turned on. */
91 /* When nonzero, report use of deprecated classes, methods, or fields. */
92 int flag_deprecated = 1;
94 /* When zero, don't optimize static class initialization. This flag shouldn't
95 be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead. */
96 /* FIXME: Make this work with gimplify. */
97 /* int flag_optimize_sci = 0; */
99 /* Don't attempt to verify invocations. */
100 int flag_verify_invocations = 0;
102 /* When nonzero, print extra version information. */
103 static int v_flag = 0;
107 /* Variable controlling how dependency tracking is enabled in
109 static int dependency_tracking = 0;
111 /* Flag values for DEPENDENCY_TRACKING. */
112 #define DEPEND_SET_FILE 1
113 #define DEPEND_ENABLE 2
114 #define DEPEND_TARGET_SET 4
115 #define DEPEND_FILE_ALREADY_SET 8
117 struct GTY(()) language_function {
121 #undef LANG_HOOKS_NAME
122 #define LANG_HOOKS_NAME "GNU Java"
123 #undef LANG_HOOKS_INIT
124 #define LANG_HOOKS_INIT java_init
125 #undef LANG_HOOKS_FINISH
126 #define LANG_HOOKS_FINISH java_finish
127 #undef LANG_HOOKS_INIT_OPTIONS
128 #define LANG_HOOKS_INIT_OPTIONS java_init_options
129 #undef LANG_HOOKS_HANDLE_OPTION
130 #define LANG_HOOKS_HANDLE_OPTION java_handle_option
131 #undef LANG_HOOKS_POST_OPTIONS
132 #define LANG_HOOKS_POST_OPTIONS java_post_options
133 #undef LANG_HOOKS_PARSE_FILE
134 #define LANG_HOOKS_PARSE_FILE java_parse_file
135 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
136 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
137 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
138 #define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
139 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
140 #define LANG_HOOKS_PRINT_ERROR_FUNCTION java_print_error_function
142 #undef LANG_HOOKS_TYPE_FOR_MODE
143 #define LANG_HOOKS_TYPE_FOR_MODE java_type_for_mode
144 #undef LANG_HOOKS_TYPE_FOR_SIZE
145 #define LANG_HOOKS_TYPE_FOR_SIZE java_type_for_size
146 #undef LANG_HOOKS_CLASSIFY_RECORD
147 #define LANG_HOOKS_CLASSIFY_RECORD java_classify_record
149 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
150 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN java_dump_tree
152 #undef LANG_HOOKS_GIMPLIFY_EXPR
153 #define LANG_HOOKS_GIMPLIFY_EXPR java_gimplify_expr
155 #undef LANG_HOOKS_DECL_OK_FOR_SIBCALL
156 #define LANG_HOOKS_DECL_OK_FOR_SIBCALL java_decl_ok_for_sibcall
158 #undef LANG_HOOKS_SET_DECL_ASSEMBLER_NAME
159 #define LANG_HOOKS_SET_DECL_ASSEMBLER_NAME java_mangle_decl
161 #undef LANG_HOOKS_ATTRIBUTE_TABLE
162 #define LANG_HOOKS_ATTRIBUTE_TABLE java_attribute_table
164 #undef LANG_HOOKS_EH_PERSONALITY
165 #define LANG_HOOKS_EH_PERSONALITY java_eh_personality
167 /* Each front end provides its own. */
168 struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
171 * process java-specific compiler command-line options
172 * return 0, but do not complain if the option is not recognized.
175 java_handle_option (size_t scode, const char *arg, int value)
177 enum opt_code code = (enum opt_code) scode;
182 jcf_path_include_arg (arg);
186 jcf_dependency_init (1);
187 dependency_tracking |= DEPEND_ENABLE;
191 jcf_dependency_init (1);
192 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
196 jcf_dependency_set_dep_file (arg);
197 dependency_tracking |= DEPEND_FILE_ALREADY_SET;
201 jcf_dependency_init (0);
202 dependency_tracking |= DEPEND_ENABLE;
206 jcf_dependency_init (0);
207 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
211 jcf_dependency_print_dummies ();
215 jcf_dependency_set_target (arg);
216 dependency_tracking |= DEPEND_TARGET_SET;
221 /* When -Wall given, enable -Wunused. We do this because the C
222 compiler does it, and people expect it. */
226 case OPT_fenable_assertions_:
227 add_enable_assert (arg, value);
230 case OPT_fenable_assertions:
231 add_enable_assert ("", value);
234 case OPT_fdisable_assertions_:
235 add_enable_assert (arg, !value);
238 case OPT_fdisable_assertions:
239 add_enable_assert ("", !value);
242 case OPT_fassume_compiled_:
243 add_assume_compiled (arg, !value);
246 case OPT_fassume_compiled:
247 add_assume_compiled ("", !value);
250 case OPT_fbootclasspath_:
251 jcf_path_bootclasspath_arg (arg);
254 case OPT_faux_classpath:
255 case OPT_fclasspath_:
256 case OPT_fCLASSPATH_:
257 jcf_path_classpath_arg (arg);
260 case OPT_fcompile_resource_:
265 if (!dump_switch_p (arg))
274 jcf_path_extdirs_arg (arg);
277 case OPT_foutput_class_dir_:
278 /* FIXME: remove; this is handled by ecj1 now. */
285 case OPT_fsource_filename_:
286 java_read_sourcefilenames (arg);
290 if (cl_options[code].flags & CL_Java)
298 /* Global open file. */
304 /* FIXME: Indirect dispatch isn't yet compatible with static class
305 init optimization. */
306 if (flag_indirect_dispatch)
307 always_initialize_class_p = true;
309 if (!flag_indirect_dispatch)
310 flag_indirect_classes = false;
312 jcf_path_seal (v_flag);
314 java_init_decl_processing ();
316 using_eh_for_cleanups ();
324 jcf_dependency_write ();
327 /* Buffer used by lang_printable_name. */
328 static char *decl_buf = NULL;
330 /* Allocated size of decl_buf. */
331 static int decl_buflen = 0;
333 /* Length of used part of decl_buf; position for next character. */
334 static int decl_bufpos = 0;
336 /* Append the string STR to decl_buf.
337 It length is given by LEN; -1 means the string is nul-terminated. */
340 put_decl_string (const char *str, int len)
344 if (decl_bufpos + len >= decl_buflen)
346 if (decl_buf == NULL)
348 decl_buflen = len + 100;
349 decl_buf = XNEWVEC (char, decl_buflen);
354 decl_buf = XRESIZEVAR (char, decl_buf, decl_buflen);
357 strcpy (decl_buf + decl_bufpos, str);
361 /* Append to decl_buf a printable name for NODE.
362 Depending on VERBOSITY, more information about NODE
363 is printed. Read the comments of decl_printable_name in
364 langhooks.h for more. */
367 put_decl_node (tree node, int verbosity)
370 if (TREE_CODE (node) == POINTER_TYPE)
372 node = TREE_TYPE (node);
375 if (DECL_P (node) && DECL_NAME (node) != NULL_TREE)
377 if (TREE_CODE (node) == FUNCTION_DECL)
379 if (verbosity == 0 && DECL_NAME (node))
380 /* We have been instructed to just print the bare name
383 put_decl_node (DECL_NAME (node), 0);
387 /* We want to print the type the DECL belongs to. We don't do
388 that when we handle constructors. */
389 if (! DECL_CONSTRUCTOR_P (node)
390 && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node)
391 /* We want to print qualified DECL names only
392 if verbosity is higher than 1. */
395 put_decl_node (TYPE_NAME (DECL_CONTEXT (node)),
397 put_decl_string (".", 1);
399 if (! DECL_CONSTRUCTOR_P (node))
400 put_decl_node (DECL_NAME (node), verbosity);
401 if (TREE_TYPE (node) != NULL_TREE
402 /* We want to print function parameters only if verbosity
407 tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
408 if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
409 args = TREE_CHAIN (args);
410 put_decl_string ("(", 1);
411 for ( ; args != end_params_node; args = TREE_CHAIN (args), i++)
414 put_decl_string (",", 1);
415 put_decl_node (TREE_VALUE (args), verbosity);
417 put_decl_string (")", 1);
421 put_decl_node (DECL_NAME (node), verbosity);
423 else if (TYPE_P (node) && TYPE_NAME (node) != NULL_TREE)
425 if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node)
426 /* Print detailed array information only if verbosity is higher
430 put_decl_node (TYPE_ARRAY_ELEMENT (node), verbosity);
431 put_decl_string("[]", 2);
433 else if (node == promoted_byte_type_node)
434 put_decl_string ("byte", 4);
435 else if (node == promoted_short_type_node)
436 put_decl_string ("short", 5);
437 else if (node == promoted_char_type_node)
438 put_decl_string ("char", 4);
439 else if (node == promoted_boolean_type_node)
440 put_decl_string ("boolean", 7);
441 else if (node == void_type_node && was_pointer)
442 put_decl_string ("null", 4);
444 put_decl_node (TYPE_NAME (node), verbosity);
446 else if (TREE_CODE (node) == IDENTIFIER_NODE)
447 put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
449 put_decl_string ("<unknown>", -1);
452 /* Return a user-friendly name for DECL.
453 The resulting string is only valid until the next call.
454 The value of the hook decl_printable_name is this function,
455 which is also called directly by java_print_error_function. */
458 lang_printable_name (tree decl, int v)
461 put_decl_node (decl, v);
462 put_decl_string ("", 1);
466 /* Print on stderr the current class and method context. This function
467 is the value of the hook print_error_function. */
469 static GTY(()) tree last_error_function_context;
470 static GTY(()) tree last_error_function;
472 java_print_error_function (diagnostic_context *context ATTRIBUTE_UNUSED,
474 diagnostic_info *diagnostic ATTRIBUTE_UNUSED)
476 /* Don't print error messages with bogus function prototypes. */
477 if (inhibit_error_function_printing)
480 if (current_function_decl != NULL
481 && DECL_CONTEXT (current_function_decl) != last_error_function_context)
484 fprintf (stderr, "%s: ", file);
486 last_error_function_context = DECL_CONTEXT (current_function_decl);
487 fprintf (stderr, "In class '%s':\n",
488 lang_printable_name (last_error_function_context, 0));
490 if (last_error_function != current_function_decl)
493 fprintf (stderr, "%s: ", file);
495 if (current_function_decl == NULL)
496 fprintf (stderr, "At top level:\n");
499 const char *name = lang_printable_name (current_function_decl, 2);
500 fprintf (stderr, "In %s '%s':\n",
501 (DECL_CONSTRUCTOR_P (current_function_decl) ? "constructor"
506 last_error_function = current_function_decl;
511 /* Called to install the PRINT_ERROR_FUNCTION hook differently
512 according to LEVEL. LEVEL is 1 during early parsing, when function
513 prototypes aren't fully resolved. java_print_error_function is set
514 so it doesn't print incomplete function prototypes. When LEVEL is
515 2, function prototypes are fully resolved and can be printed when
519 lang_init_source (int level)
521 inhibit_error_function_printing = (level == 1);
525 java_init_options (unsigned int argc ATTRIBUTE_UNUSED,
526 const char **argv ATTRIBUTE_UNUSED)
528 flag_bounds_check = 1;
530 flag_non_call_exceptions = 1;
532 /* In Java floating point operations never trap. */
533 flag_trapping_math = 0;
535 /* In Java arithmetic overflow always wraps around. */
538 /* Java requires left-to-right evaluation of subexpressions. */
539 flag_evaluation_order = 1;
546 /* Post-switch processing. */
548 java_post_options (const char **pfilename)
550 const char *filename = *pfilename;
552 /* Excess precision other than "fast" requires front-end
554 if (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
555 && TARGET_FLT_EVAL_METHOD_NON_DEFAULT)
556 sorry ("-fexcess-precision=standard for Java");
557 flag_excess_precision_cmdline = EXCESS_PRECISION_FAST;
559 /* An absolute requirement: if we're not using indirect dispatch, we
560 must always verify everything. */
561 if (! flag_indirect_dispatch)
562 flag_verify_invocations = true;
564 if (flag_reduced_reflection)
566 if (flag_indirect_dispatch)
567 error ("-findirect-dispatch is incompatible "
568 "with -freduced-reflection");
570 error ("-fjni is incompatible with -freduced-reflection");
573 /* Open input file. */
575 if (filename == 0 || !strcmp (filename, "-"))
580 if (dependency_tracking)
581 error ("can't do dependency tracking with input from stdin");
585 if (dependency_tracking)
589 /* If the target is set and the output filename is set, then
590 there's no processing to do here. Otherwise we must
591 compute one or the other. */
592 if (! ((dependency_tracking & DEPEND_TARGET_SET)
593 && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
595 dot = strrchr (filename, '.');
597 error ("couldn't determine target name for dependency tracking");
600 char *buf = XNEWVEC (char, dot - filename +
601 3 + sizeof (TARGET_OBJECT_SUFFIX));
602 strncpy (buf, filename, dot - filename);
604 /* If emitting class files, we might have multiple
605 targets. The class generation code takes care of
606 registering them. Otherwise we compute the
608 if ((dependency_tracking & DEPEND_TARGET_SET))
612 strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
613 jcf_dependency_set_target (buf);
616 if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
618 else if ((dependency_tracking & DEPEND_SET_FILE))
620 strcpy (buf + (dot - filename), ".d");
621 jcf_dependency_set_dep_file (buf);
624 jcf_dependency_set_dep_file ("-");
631 linemap_add (line_table, LC_ENTER, false, filename, 0);
632 linemap_add (line_table, LC_RENAME, false, "<built-in>", 0);
634 /* Initialize the compiler back end. */
638 /* Return either DECL or its known constant value (if it has one). */
641 decl_constant_value (tree decl)
643 if (/* Don't change a variable array bound or initial value to a constant
644 in a place where a variable is invalid. */
645 current_function_decl != 0
646 && ! TREE_THIS_VOLATILE (decl)
647 && TREE_READONLY (decl)
648 && DECL_INITIAL (decl) != 0
649 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
650 /* This is invalid if initial value is not constant.
651 If it has either a function call, a memory reference,
652 or a variable, then re-evaluating it could give different results. */
653 && TREE_CONSTANT (DECL_INITIAL (decl))
654 /* Check for cases where this is sub-optimal, even though valid. */
655 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
656 return DECL_INITIAL (decl);
660 /* Every call to a static constructor has an associated boolean
661 variable which is in the outermost scope of the calling method.
662 This variable is used to avoid multiple calls to the static
663 constructor for each class.
665 It looks something like this:
669 boolean dummy = OtherClass.is_initialized;
674 OtherClass.initialize();
676 ... use OtherClass.data ...
679 Each of these boolean variables has an entry in the
680 DECL_FUNCTION_INIT_TEST_TABLE of a method. When inlining a method
681 we must merge the DECL_FUNCTION_INIT_TEST_TABLE from the function
682 being inlined and create the boolean variables in the outermost
683 scope of the method being inlined into. */
685 /* Create a mapping from a boolean variable in a method being inlined
686 to one in the scope of the method being inlined into. */
689 merge_init_test_initialization (void **entry, void *x)
691 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
692 splay_tree decl_map = (splay_tree)x;
694 tree *init_test_decl;
696 /* See if we have remapped this declaration. If we haven't there's
697 a bug in the inliner. */
698 n = splay_tree_lookup (decl_map, (splay_tree_key) ite->value);
701 /* Create a new entry for the class and its remapped boolean
702 variable. If we already have a mapping for this class we've
703 already initialized it, so don't overwrite the value. */
704 init_test_decl = java_treetreehash_new
705 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
706 if (!*init_test_decl)
707 *init_test_decl = (tree)n->value;
709 /* This fixes a weird case.
711 The front end assumes that once we have called a method that
712 initializes some class, we can assume the class is initialized. It
713 does this by setting the DECL_INITIAL of the init_test_decl for that
714 class, and no initializations are emitted for that class.
716 However, what if the method that is supposed to do the initialization
717 is itself inlined in the caller? When expanding the called method
718 we'll assume that the class initialization has already been done,
719 because the DECL_INITIAL of the init_test_decl is set.
721 To fix this we remove the DECL_INITIAL (in the caller scope) of all
722 the init_test_decls corresponding to classes initialized by the
723 inlined method. This makes the caller no longer assume that the
724 method being inlined does any class initializations. */
725 DECL_INITIAL (*init_test_decl) = NULL;
730 /* Merge the DECL_FUNCTION_INIT_TEST_TABLE from the function we're
734 java_inlining_merge_static_initializers (tree fn, void *decl_map)
737 (DECL_FUNCTION_INIT_TEST_TABLE (fn),
738 merge_init_test_initialization, decl_map);
741 /* Lookup a DECL_FUNCTION_INIT_TEST_TABLE entry in the method we're
742 inlining into. If we already have a corresponding entry in that
743 class we don't need to create another one, so we create a mapping
744 from the variable in the inlined class to the corresponding
748 inline_init_test_initialization (void **entry, void *x)
750 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
751 splay_tree decl_map = (splay_tree)x;
753 tree h = java_treetreehash_find
754 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
757 splay_tree_insert (decl_map,
758 (splay_tree_key) ite->value,
759 (splay_tree_value) h);
763 /* Look up the boolean variables in the DECL_FUNCTION_INIT_TEST_TABLE
764 of a method being inlined. For each hone, if we already have a
765 variable associated with the same class in the method being inlined
766 into, create a new mapping for it. */
769 java_inlining_map_static_initializers (tree fn, void *decl_map)
772 (DECL_FUNCTION_INIT_TEST_TABLE (fn),
773 inline_init_test_initialization, decl_map);
776 /* Avoid voluminous output for deep recursion of compound exprs. */
779 dump_compound_expr (dump_info_p di, tree t)
785 switch (TREE_CODE (TREE_OPERAND (t, i)))
788 dump_compound_expr (di, TREE_OPERAND (t, i));
792 dump_child ("expr", TREE_OPERAND (t, i));
798 java_dump_tree (void *dump_info, tree t)
801 dump_info_p di = (dump_info_p) dump_info;
803 /* Figure out what kind of node this is. */
804 code = TREE_CODE (t);
809 dump_child ("args", DECL_ARGUMENTS (t));
810 if (DECL_EXTERNAL (t))
811 dump_string (di, "undefined");
813 dump_string (di, "extern");
815 dump_string (di, "static");
816 if (DECL_LANG_SPECIFIC (t) && !dump_flag (di, TDF_SLIM, t))
817 dump_child ("inline body", DECL_SAVED_TREE (t));
821 dump_child ("expr", TREE_OPERAND (t, 0));
825 dump_child ("goto", TREE_OPERAND (t, 0));
829 dump_child ("label", TREE_OPERAND (t, 0));
833 if (BLOCK_EXPR_BODY (t))
835 tree local = BLOCK_VARS (t);
838 tree next = TREE_CHAIN (local);
839 dump_child ("var", local);
844 tree block = BLOCK_EXPR_BODY (t);
845 dump_child ("body", block);
846 block = TREE_CHAIN (block);
852 if (!dump_flag (di, TDF_SLIM, t))
854 dump_compound_expr (di, t);
863 /* Java calls can't, in general, be sibcalls because we need an
864 accurate stack trace in order to guarantee correct operation of
865 methods such as Class.forName(String) and
866 SecurityManager.getClassContext(). */
869 java_decl_ok_for_sibcall (const_tree decl)
871 return (decl != NULL && DECL_CONTEXT (decl) == output_class
872 && !DECL_UNINLINABLE (decl));
875 static enum classify_record
876 java_classify_record (tree type)
878 if (! CLASS_P (type))
879 return RECORD_IS_STRUCT;
881 /* ??? GDB does not support DW_TAG_interface_type as of December,
882 2007. Re-enable this at a later time. */
883 if (0 && CLASS_INTERFACE (TYPE_NAME (type)))
884 return RECORD_IS_INTERFACE;
886 return RECORD_IS_CLASS;
889 static GTY(()) tree java_eh_personality_decl;
892 java_eh_personality (void)
894 if (!java_eh_personality_decl)
895 java_eh_personality_decl
896 = build_personality_function (USING_SJLJ_EXCEPTIONS
897 ? "__gcj_personality_sj0"
898 : "__gcj_personality_v0");
900 return java_eh_personality_decl;
903 #include "gt-java-lang.h"