1 /* Java(TM) language-specific utility routines.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
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. */
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
34 #include "java-tree.h"
37 #include "langhooks.h"
38 #include "langhooks-def.h"
42 #include "diagnostic.h"
43 #include "tree-inline.h"
47 const char *const string;
52 static const char *java_init PARAMS ((const char *));
53 static void java_finish PARAMS ((void));
54 static void java_init_options PARAMS ((void));
55 static bool java_post_options PARAMS ((void));
57 static int java_decode_option PARAMS ((int, char **));
58 static void put_decl_string PARAMS ((const char *, int));
59 static void put_decl_node PARAMS ((tree));
60 static void java_print_error_function PARAMS ((diagnostic_context *,
62 static int process_option_with_no PARAMS ((const char *,
63 const struct string_option *,
65 static tree java_tree_inlining_walk_subtrees PARAMS ((tree *,
70 static int java_unsafe_for_reeval PARAMS ((tree));
72 #ifndef TARGET_OBJECT_SUFFIX
73 # define TARGET_OBJECT_SUFFIX ".o"
76 /* Table indexed by tree code giving a string containing a character
77 classifying the tree code. Possibilities are
78 t, d, s, c, r, <, 1 and 2. See java/java-tree.def for details. */
80 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
82 const char tree_code_type[] = {
85 #include "java-tree.def"
89 /* Table indexed by tree code giving number of expression
90 operands beyond the fixed part of the node structure.
91 Not used for types or decls. */
93 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
95 const unsigned char tree_code_length[] = {
98 #include "java-tree.def"
102 /* Names of tree components.
103 Used for printing out the tree and error messages. */
104 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
106 const char *const tree_code_name[] = {
109 #include "java-tree.def"
113 /* Used to avoid printing error messages with bogus function
114 prototypes. Starts out false. */
115 static bool inhibit_error_function_printing;
117 int compiling_from_source;
119 char * resource_name;
121 int flag_emit_class_files = 0;
123 /* Nonzero if input file is a file with a list of filenames to compile. */
125 int flag_filelist_file = 0;
127 /* When non zero, we emit xref strings. Values of the flag for xref
128 backends are defined in xref_flag_table, xref.c. */
130 int flag_emit_xref = 0;
132 /* When non zero, -Wall was turned on. */
135 /* When non zero, check for redundant modifier uses. */
136 int flag_redundant = 0;
138 /* When non zero, call a library routine to do integer divisions. */
139 int flag_use_divide_subroutine = 1;
141 /* When non zero, generate code for the Boehm GC. */
142 int flag_use_boehm_gc = 0;
144 /* When non zero, assume the runtime uses a hash table to map an
145 object to its synchronization structure. */
146 int flag_hash_synchronization;
148 /* When non zero, assume all native functions are implemented with
152 /* When non zero, warn when source file is newer than matching class
156 /* When non zero, generate checks for references to NULL. */
157 int flag_check_references = 0;
159 /* The encoding of the source file. */
160 const char *current_encoding = NULL;
162 /* When non zero, report the now deprecated empty statements. */
163 int flag_extraneous_semicolon;
165 /* When non zero, always check for a non gcj generated classes archive. */
166 int flag_force_classes_archive_check;
168 /* When zero, don't optimize static class initialization. This flag shouldn't
169 be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead. */
170 int flag_optimize_sci = 1;
172 /* When non zero, use offset tables for virtual method calls
173 in order to improve binary compatibility. */
174 int flag_indirect_dispatch = 0;
176 /* When zero, don't generate runtime array store checks. */
177 int flag_store_check = 1;
179 /* When non zero, print extra version information. */
180 static int version_flag = 0;
182 /* Set non-zero if the user specified -finline-functions on the command
184 int flag_really_inline = 0;
186 /* Table of language-dependent -f options.
187 STRING is the option name. VARIABLE is the address of the variable.
188 ON_VALUE is the value to store in VARIABLE
189 if `-fSTRING' is seen as an option.
190 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
192 static const struct string_option
195 {"emit-class-file", &flag_emit_class_files, 1},
196 {"emit-class-files", &flag_emit_class_files, 1},
197 {"filelist-file", &flag_filelist_file, 1},
198 {"use-divide-subroutine", &flag_use_divide_subroutine, 1},
199 {"use-boehm-gc", &flag_use_boehm_gc, 1},
200 {"hash-synchronization", &flag_hash_synchronization, 1},
201 {"jni", &flag_jni, 1},
202 {"check-references", &flag_check_references, 1},
203 {"force-classes-archive-check", &flag_force_classes_archive_check, 1},
204 {"optimize-static-class-initialization", &flag_optimize_sci, 1 },
205 {"indirect-dispatch", &flag_indirect_dispatch, 1},
206 {"store-check", &flag_store_check, 1}
209 static const struct string_option
212 { "redundant-modifiers", &flag_redundant, 1 },
213 { "extraneous-semicolon", &flag_extraneous_semicolon, 1 },
214 { "out-of-date", &flag_newer, 1 }
219 /* Variable controlling how dependency tracking is enabled in
221 static int dependency_tracking = 0;
223 /* Flag values for DEPENDENCY_TRACKING. */
224 #define DEPEND_SET_FILE 1
225 #define DEPEND_ENABLE 2
226 #define DEPEND_TARGET_SET 4
227 #define DEPEND_FILE_ALREADY_SET 8
229 struct language_function GTY(())
234 #undef LANG_HOOKS_NAME
235 #define LANG_HOOKS_NAME "GNU Java"
236 #undef LANG_HOOKS_INIT
237 #define LANG_HOOKS_INIT java_init
238 #undef LANG_HOOKS_FINISH
239 #define LANG_HOOKS_FINISH java_finish
240 #undef LANG_HOOKS_INIT_OPTIONS
241 #define LANG_HOOKS_INIT_OPTIONS java_init_options
242 #undef LANG_HOOKS_DECODE_OPTION
243 #define LANG_HOOKS_DECODE_OPTION java_decode_option
244 #undef LANG_HOOKS_POST_OPTIONS
245 #define LANG_HOOKS_POST_OPTIONS java_post_options
246 #undef LANG_HOOKS_PARSE_FILE
247 #define LANG_HOOKS_PARSE_FILE java_parse_file
248 #undef LANG_HOOKS_UNSAFE_FOR_REEVAL
249 #define LANG_HOOKS_UNSAFE_FOR_REEVAL java_unsafe_for_reeval
250 #undef LANG_HOOKS_MARK_ADDRESSABLE
251 #define LANG_HOOKS_MARK_ADDRESSABLE java_mark_addressable
252 #undef LANG_HOOKS_EXPAND_EXPR
253 #define LANG_HOOKS_EXPAND_EXPR java_expand_expr
254 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
255 #define LANG_HOOKS_TRUTHVALUE_CONVERSION java_truthvalue_conversion
256 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
257 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
258 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
259 #define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
260 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
261 #define LANG_HOOKS_PRINT_ERROR_FUNCTION java_print_error_function
263 #undef LANG_HOOKS_TYPE_FOR_MODE
264 #define LANG_HOOKS_TYPE_FOR_MODE java_type_for_mode
265 #undef LANG_HOOKS_TYPE_FOR_SIZE
266 #define LANG_HOOKS_TYPE_FOR_SIZE java_type_for_size
267 #undef LANG_HOOKS_SIGNED_TYPE
268 #define LANG_HOOKS_SIGNED_TYPE java_signed_type
269 #undef LANG_HOOKS_UNSIGNED_TYPE
270 #define LANG_HOOKS_UNSIGNED_TYPE java_unsigned_type
271 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
272 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE java_signed_or_unsigned_type
274 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
275 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES java_tree_inlining_walk_subtrees
277 /* Each front end provides its own. */
278 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
280 /* Process an option that can accept a `no-' form.
281 Return 1 if option found, 0 otherwise. */
283 process_option_with_no (p, table, table_size)
285 const struct string_option *table;
290 for (j = 0; j < table_size; j++)
292 if (!strcmp (p, table[j].string))
294 *table[j].variable = table[j].on_value;
297 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
298 && ! strcmp (p+3, table[j].string))
300 *table[j].variable = ! table[j].on_value;
309 * process java-specific compiler command-line options
310 * return 0, but do not complain if the option is not recognised.
313 java_decode_option (argc, argv)
314 int argc __attribute__ ((__unused__));
321 if (strcmp (p, "-version") == 0)
324 /* We return 0 so that the caller can process this. */
328 #define CLARG "-fcompile-resource="
329 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
331 resource_name = p + sizeof (CLARG) - 1;
335 #define CLARG "-fassume-compiled="
336 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
338 add_assume_compiled (p + sizeof (CLARG) - 1, 0);
342 #define CLARG "-fno-assume-compiled="
343 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
345 add_assume_compiled (p + sizeof (CLARG) - 1, 1);
349 #define CLARG "-fassume-compiled"
350 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
352 add_assume_compiled ("", 0);
356 #define CLARG "-fno-assume-compiled"
357 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
359 add_assume_compiled ("", 1);
363 #define CLARG "-fCLASSPATH="
364 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
366 jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
370 #define CLARG "-fclasspath="
371 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
373 jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
377 #define CLARG "-fbootclasspath="
378 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
380 jcf_path_bootclasspath_arg (p + sizeof (CLARG) - 1);
384 #define CLARG "-fextdirs="
385 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
387 jcf_path_extdirs_arg (p + sizeof (CLARG) - 1);
391 else if (strncmp (p, "-I", 2) == 0)
393 jcf_path_include_arg (p + 2);
397 #define ARG "-foutput-class-dir="
398 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
400 jcf_write_base_directory = p + sizeof (ARG) - 1;
404 #define ARG "-fencoding="
405 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
407 current_encoding = p + sizeof (ARG) - 1;
411 #define ARG "-finline-functions"
412 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
414 flag_inline_functions = 1;
415 flag_really_inline = 1;
420 if (p[0] == '-' && p[1] == 'f')
422 /* Some kind of -f option.
423 P's value is the option sans `-f'.
424 Search for it in the table of options. */
426 if (process_option_with_no (p, lang_f_options,
427 ARRAY_SIZE (lang_f_options)))
429 return dump_switch_p (p);
432 if (strcmp (p, "-Wall") == 0)
436 flag_extraneous_semicolon = 1;
437 /* When -Wall given, enable -Wunused. We do this because the C
438 compiler does it, and people expect it. */
443 if (p[0] == '-' && p[1] == 'W')
445 /* Skip `-W' and see if we accept the option or its `no-' form. */
447 return process_option_with_no (p, lang_W_options,
448 ARRAY_SIZE (lang_W_options));
451 if (strcmp (p, "-MD") == 0)
453 jcf_dependency_init (1);
454 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
457 else if (strcmp (p, "-MMD") == 0)
459 jcf_dependency_init (0);
460 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
463 else if (strcmp (p, "-M") == 0)
465 jcf_dependency_init (1);
466 dependency_tracking |= DEPEND_ENABLE;
469 else if (strcmp (p, "-MM") == 0)
471 jcf_dependency_init (0);
472 dependency_tracking |= DEPEND_ENABLE;
475 else if (strcmp (p, "-MP") == 0)
477 jcf_dependency_print_dummies ();
480 else if (strcmp (p, "-MT") == 0)
482 jcf_dependency_set_target (argv[1]);
483 dependency_tracking |= DEPEND_TARGET_SET;
486 else if (strcmp (p, "-MF") == 0)
488 jcf_dependency_set_dep_file (argv[1]);
489 dependency_tracking |= DEPEND_FILE_ALREADY_SET;
496 /* Global open file. */
501 const char *filename;
504 extern int flag_minimal_debug;
505 flag_minimal_debug = 0;
508 if (flag_inline_functions)
509 flag_inline_trees = 1;
511 /* Open input file. */
513 if (filename == 0 || !strcmp (filename, "-"))
518 if (dependency_tracking)
519 error ("can't do dependency tracking with input from stdin");
523 if (dependency_tracking)
527 /* If the target is set and the output filename is set, then
528 there's no processing to do here. Otherwise we must
529 compute one or the other. */
530 if (! ((dependency_tracking & DEPEND_TARGET_SET)
531 && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
533 dot = strrchr (filename, '.');
535 error ("couldn't determine target name for dependency tracking");
538 char *buf = (char *) xmalloc (dot - filename +
539 3 + sizeof (TARGET_OBJECT_SUFFIX));
540 strncpy (buf, filename, dot - filename);
542 /* If emitting class files, we might have multiple
543 targets. The class generation code takes care of
544 registering them. Otherwise we compute the
546 if ((dependency_tracking & DEPEND_TARGET_SET))
548 else if (flag_emit_class_files)
549 jcf_dependency_set_target (NULL);
552 strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
553 jcf_dependency_set_target (buf);
556 if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
558 else if ((dependency_tracking & DEPEND_SET_FILE))
560 strcpy (buf + (dot - filename), ".d");
561 jcf_dependency_set_dep_file (buf);
564 jcf_dependency_set_dep_file ("-");
573 jcf_path_seal (version_flag);
575 java_init_decl_processing ();
577 using_eh_for_cleanups ();
585 jcf_dependency_write ();
588 /* Buffer used by lang_printable_name. */
589 static char *decl_buf = NULL;
591 /* Allocated size of decl_buf. */
592 static int decl_buflen = 0;
594 /* Length of used part of decl_buf; position for next character. */
595 static int decl_bufpos = 0;
597 /* Append the string STR to decl_buf.
598 It length is given by LEN; -1 means the string is nul-terminated. */
601 put_decl_string (str, len)
607 if (decl_bufpos + len >= decl_buflen)
609 if (decl_buf == NULL)
611 decl_buflen = len + 100;
612 decl_buf = (char *) xmalloc (decl_buflen);
617 decl_buf = (char *) xrealloc (decl_buf, decl_buflen);
620 strcpy (decl_buf + decl_bufpos, str);
624 /* Append to decl_buf a printable name for NODE. */
631 if (TREE_CODE (node) == POINTER_TYPE)
633 node = TREE_TYPE (node);
636 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd'
637 && DECL_NAME (node) != NULL_TREE)
639 if (TREE_CODE (node) == FUNCTION_DECL)
641 /* We want to print the type the DECL belongs to. We don't do
642 that when we handle constructors. */
643 if (! DECL_CONSTRUCTOR_P (node)
644 && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node))
646 put_decl_node (TYPE_NAME (DECL_CONTEXT (node)));
647 put_decl_string (".", 1);
649 if (! DECL_CONSTRUCTOR_P (node))
650 put_decl_node (DECL_NAME (node));
651 if (TREE_TYPE (node) != NULL_TREE)
654 tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
655 if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
656 args = TREE_CHAIN (args);
657 put_decl_string ("(", 1);
658 for ( ; args != end_params_node; args = TREE_CHAIN (args), i++)
661 put_decl_string (",", 1);
662 put_decl_node (TREE_VALUE (args));
664 put_decl_string (")", 1);
668 put_decl_node (DECL_NAME (node));
670 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't'
671 && TYPE_NAME (node) != NULL_TREE)
673 if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node))
675 put_decl_node (TYPE_ARRAY_ELEMENT (node));
676 put_decl_string("[]", 2);
678 else if (node == promoted_byte_type_node)
679 put_decl_string ("byte", 4);
680 else if (node == promoted_short_type_node)
681 put_decl_string ("short", 5);
682 else if (node == promoted_char_type_node)
683 put_decl_string ("char", 4);
684 else if (node == promoted_boolean_type_node)
685 put_decl_string ("boolean", 7);
686 else if (node == void_type_node && was_pointer)
687 put_decl_string ("null", 4);
689 put_decl_node (TYPE_NAME (node));
691 else if (TREE_CODE (node) == IDENTIFIER_NODE)
692 put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
694 put_decl_string ("<unknown>", -1);
697 /* Return a user-friendly name for DECL.
698 The resulting string is only valid until the next call.
699 The value of the hook decl_printable_name is this function,
700 which is also called directly by java_print_error_function. */
703 lang_printable_name (decl, v)
705 int v __attribute__ ((__unused__));
708 put_decl_node (decl);
709 put_decl_string ("", 1);
713 /* Does the same thing that lang_printable_name, but add a leading
714 space to the DECL name string -- With Leading Space. */
717 lang_printable_name_wls (decl, v)
719 int v __attribute__ ((__unused__));
722 put_decl_node (decl);
723 put_decl_string ("", 1);
728 /* Print on stderr the current class and method context. This function
729 is the value of the hook print_error_function. */
731 static GTY(()) tree last_error_function_context;
732 static GTY(()) tree last_error_function;
734 java_print_error_function (context, file)
735 diagnostic_context *context __attribute__((__unused__));
738 /* Don't print error messages with bogus function prototypes. */
739 if (inhibit_error_function_printing)
742 if (current_function_decl != NULL
743 && DECL_CONTEXT (current_function_decl) != last_error_function_context)
746 fprintf (stderr, "%s: ", file);
748 last_error_function_context = DECL_CONTEXT (current_function_decl);
749 fprintf (stderr, "In class `%s':\n",
750 lang_printable_name (last_error_function_context, 0));
752 if (last_error_function != current_function_decl)
755 fprintf (stderr, "%s: ", file);
757 if (current_function_decl == NULL)
758 fprintf (stderr, "At top level:\n");
761 const char *name = lang_printable_name (current_function_decl, 2);
762 fprintf (stderr, "In %s `%s':\n",
763 (DECL_CONSTRUCTOR_P (current_function_decl) ? "constructor"
768 last_error_function = current_function_decl;
773 /* Called to install the PRINT_ERROR_FUNCTION hook differently
774 according to LEVEL. LEVEL is 1 during early parsing, when function
775 prototypes aren't fully resolved. java_print_error_function is set
776 so it doesn't print incomplete function prototypes. When LEVEL is
777 2, function prototypes are fully resolved and can be printed when
780 void lang_init_source (level)
783 inhibit_error_function_printing = (level == 1);
789 flag_bounds_check = 1;
791 flag_non_call_exceptions = 1;
793 /* In Java floating point operations never trap. */
794 flag_trapping_math = 0;
797 /* Post-switch processing. */
801 /* Use tree inlining if possible. Function instrumentation is only
802 done in the RTL level, so we disable tree inlining. */
803 if (! flag_instrument_function_entry_exit)
807 if (flag_inline_functions)
809 flag_inline_trees = 2;
810 flag_inline_functions = 0;
814 /* Initialize the compiler back end. */
818 /* Return either DECL or its known constant value (if it has one). */
821 decl_constant_value (decl)
824 if (/* Don't change a variable array bound or initial value to a constant
825 in a place where a variable is invalid. */
826 current_function_decl != 0
827 && ! TREE_THIS_VOLATILE (decl)
828 && TREE_READONLY (decl)
829 && DECL_INITIAL (decl) != 0
830 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
831 /* This is invalid if initial value is not constant.
832 If it has either a function call, a memory reference,
833 or a variable, then re-evaluating it could give different results. */
834 && TREE_CONSTANT (DECL_INITIAL (decl))
835 /* Check for cases where this is sub-optimal, even though valid. */
836 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
837 return DECL_INITIAL (decl);
841 /* Walk the language specific tree nodes during inlining. */
844 java_tree_inlining_walk_subtrees (tp,subtrees,func,data,htab)
845 tree *tp ATTRIBUTE_UNUSED;
846 int *subtrees ATTRIBUTE_UNUSED;
847 walk_tree_fn func ATTRIBUTE_UNUSED;
848 void *data ATTRIBUTE_UNUSED;
849 void *htab ATTRIBUTE_UNUSED;
854 #define WALK_SUBTREE(NODE) \
857 result = walk_tree (&(NODE), func, data, htab); \
867 code = TREE_CODE (t);
871 if (BLOCK_EXPR_BODY (t))
873 tree *prev = &BLOCK_EXPR_BODY (*tp);
876 WALK_SUBTREE (*prev);
877 prev = &TREE_CHAIN (*prev);
888 /* Called from unsafe_for_reeval. */
890 java_unsafe_for_reeval (t)
893 switch (TREE_CODE (t))
896 /* Our expander tries to expand the variables twice. Boom. */
897 if (BLOCK_EXPR_DECLS (t) != NULL)
899 return unsafe_for_reeval (BLOCK_EXPR_BODY (t));
908 #include "gt-java-lang.h"