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"
46 const char *const string
;
51 static const char *java_init
PARAMS ((const char *));
52 static void java_finish
PARAMS ((void));
53 static void java_init_options
PARAMS ((void));
54 static int java_decode_option
PARAMS ((int, char **));
55 static void put_decl_string
PARAMS ((const char *, int));
56 static void put_decl_node
PARAMS ((tree
));
57 static void java_print_error_function
PARAMS ((diagnostic_context
*,
59 static int process_option_with_no
PARAMS ((const char *,
60 const struct string_option
*,
63 #ifndef TARGET_OBJECT_SUFFIX
64 # define TARGET_OBJECT_SUFFIX ".o"
67 /* Table indexed by tree code giving a string containing a character
68 classifying the tree code. Possibilities are
69 t, d, s, c, r, <, 1 and 2. See java/java-tree.def for details. */
71 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
73 const char tree_code_type
[] = {
76 #include "java-tree.def"
80 /* Table indexed by tree code giving number of expression
81 operands beyond the fixed part of the node structure.
82 Not used for types or decls. */
84 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
86 const unsigned char tree_code_length
[] = {
89 #include "java-tree.def"
93 /* Names of tree components.
94 Used for printing out the tree and error messages. */
95 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
97 const char *const tree_code_name
[] = {
100 #include "java-tree.def"
104 /* Used to avoid printing error messages with bogus function
105 prototypes. Starts out false. */
106 static bool inhibit_error_function_printing
;
108 int compiling_from_source
;
110 char * resource_name
;
112 int flag_emit_class_files
= 0;
114 /* Nonzero if input file is a file with a list of filenames to compile. */
116 int flag_filelist_file
= 0;
118 /* When non zero, we emit xref strings. Values of the flag for xref
119 backends are defined in xref_flag_table, xref.c. */
121 int flag_emit_xref
= 0;
123 /* When non zero, -Wall was turned on. */
126 /* When non zero, check for redundant modifier uses. */
127 int flag_redundant
= 0;
129 /* When non zero, call a library routine to do integer divisions. */
130 int flag_use_divide_subroutine
= 1;
132 /* When non zero, generate code for the Boehm GC. */
133 int flag_use_boehm_gc
= 0;
135 /* When non zero, assume the runtime uses a hash table to map an
136 object to its synchronization structure. */
137 int flag_hash_synchronization
;
139 /* When non zero, assume all native functions are implemented with
143 /* When non zero, warn when source file is newer than matching class
147 /* When non zero, generate checks for references to NULL. */
148 int flag_check_references
= 0;
150 /* The encoding of the source file. */
151 const char *current_encoding
= NULL
;
153 /* When non zero, report the now deprecated empty statements. */
154 int flag_extraneous_semicolon
;
156 /* When non zero, always check for a non gcj generated classes archive. */
157 int flag_force_classes_archive_check
;
159 /* When zero, don't optimize static class initialization. This flag shouldn't
160 be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead. */
161 int flag_optimize_sci
= 1;
163 /* When non zero, use offset tables for virtual method calls
164 in order to improve binary compatibility. */
165 int flag_indirect_dispatch
= 0;
167 /* When zero, don't generate runtime array store checks. */
168 int flag_store_check
= 1;
170 /* When non zero, print extra version information. */
171 static int version_flag
= 0;
173 /* Table of language-dependent -f options.
174 STRING is the option name. VARIABLE is the address of the variable.
175 ON_VALUE is the value to store in VARIABLE
176 if `-fSTRING' is seen as an option.
177 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
179 static const struct string_option
182 {"emit-class-file", &flag_emit_class_files
, 1},
183 {"emit-class-files", &flag_emit_class_files
, 1},
184 {"filelist-file", &flag_filelist_file
, 1},
185 {"use-divide-subroutine", &flag_use_divide_subroutine
, 1},
186 {"use-boehm-gc", &flag_use_boehm_gc
, 1},
187 {"hash-synchronization", &flag_hash_synchronization
, 1},
188 {"jni", &flag_jni
, 1},
189 {"check-references", &flag_check_references
, 1},
190 {"force-classes-archive-check", &flag_force_classes_archive_check
, 1},
191 {"optimize-static-class-initialization", &flag_optimize_sci
, 1 },
192 {"indirect-dispatch", &flag_indirect_dispatch
, 1},
193 {"store-check", &flag_store_check
, 1}
196 static const struct string_option
199 { "redundant-modifiers", &flag_redundant
, 1 },
200 { "extraneous-semicolon", &flag_extraneous_semicolon
, 1 },
201 { "out-of-date", &flag_newer
, 1 }
206 /* Variable controlling how dependency tracking is enabled in
208 static int dependency_tracking
= 0;
210 /* Flag values for DEPENDENCY_TRACKING. */
211 #define DEPEND_SET_FILE 1
212 #define DEPEND_ENABLE 2
213 #define DEPEND_TARGET_SET 4
214 #define DEPEND_FILE_ALREADY_SET 8
216 #undef LANG_HOOKS_NAME
217 #define LANG_HOOKS_NAME "GNU Java"
218 #undef LANG_HOOKS_INIT
219 #define LANG_HOOKS_INIT java_init
220 #undef LANG_HOOKS_FINISH
221 #define LANG_HOOKS_FINISH java_finish
222 #undef LANG_HOOKS_INIT_OPTIONS
223 #define LANG_HOOKS_INIT_OPTIONS java_init_options
224 #undef LANG_HOOKS_DECODE_OPTION
225 #define LANG_HOOKS_DECODE_OPTION java_decode_option
226 #undef LANG_HOOKS_SET_YYDEBUG
227 #define LANG_HOOKS_SET_YYDEBUG java_set_yydebug
228 #undef LANG_HOOKS_PARSE_FILE
229 #define LANG_HOOKS_PARSE_FILE java_parse_file
230 #undef LANG_HOOKS_MARK_TREE
231 #define LANG_HOOKS_MARK_TREE java_mark_tree
232 #undef LANG_HOOKS_EXPAND_EXPR
233 #define LANG_HOOKS_EXPAND_EXPR java_expand_expr
234 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
235 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
236 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
237 #define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
238 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
239 #define LANG_HOOKS_PRINT_ERROR_FUNCTION java_print_error_function
241 #undef LANG_HOOKS_TYPE_FOR_MODE
242 #define LANG_HOOKS_TYPE_FOR_MODE java_type_for_mode
243 #undef LANG_HOOKS_TYPE_FOR_SIZE
244 #define LANG_HOOKS_TYPE_FOR_SIZE java_type_for_size
245 #undef LANG_HOOKS_SIGNED_TYPE
246 #define LANG_HOOKS_SIGNED_TYPE java_signed_type
247 #undef LANG_HOOKS_UNSIGNED_TYPE
248 #define LANG_HOOKS_UNSIGNED_TYPE java_unsigned_type
249 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
250 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE java_signed_or_unsigned_type
252 /* Each front end provides its own. */
253 const struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
255 /* Process an option that can accept a `no-' form.
256 Return 1 if option found, 0 otherwise. */
258 process_option_with_no (p
, table
, table_size
)
260 const struct string_option
*table
;
265 for (j
= 0; j
< table_size
; j
++)
267 if (!strcmp (p
, table
[j
].string
))
269 *table
[j
].variable
= table
[j
].on_value
;
272 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-'
273 && ! strcmp (p
+3, table
[j
].string
))
275 *table
[j
].variable
= ! table
[j
].on_value
;
284 * process java-specific compiler command-line options
285 * return 0, but do not complain if the option is not recognised.
288 java_decode_option (argc
, argv
)
289 int argc
__attribute__ ((__unused__
));
296 if (strcmp (p
, "-version") == 0)
299 /* We return 0 so that the caller can process this. */
303 #define CLARG "-fcompile-resource="
304 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
306 resource_name
= p
+ sizeof (CLARG
) - 1;
310 #define CLARG "-fassume-compiled="
311 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
313 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 0);
317 #define CLARG "-fno-assume-compiled="
318 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
320 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 1);
324 #define CLARG "-fassume-compiled"
325 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
327 add_assume_compiled ("", 0);
331 #define CLARG "-fno-assume-compiled"
332 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
334 add_assume_compiled ("", 1);
338 #define CLARG "-fCLASSPATH="
339 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
341 jcf_path_classpath_arg (p
+ sizeof (CLARG
) - 1);
345 #define CLARG "-fclasspath="
346 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
348 jcf_path_classpath_arg (p
+ sizeof (CLARG
) - 1);
352 #define CLARG "-fbootclasspath="
353 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
355 jcf_path_bootclasspath_arg (p
+ sizeof (CLARG
) - 1);
359 else if (strncmp (p
, "-I", 2) == 0)
361 jcf_path_include_arg (p
+ 2);
365 #define ARG "-foutput-class-dir="
366 if (strncmp (p
, ARG
, sizeof (ARG
) - 1) == 0)
368 jcf_write_base_directory
= p
+ sizeof (ARG
) - 1;
372 #define ARG "-fencoding="
373 if (strncmp (p
, ARG
, sizeof (ARG
) - 1) == 0)
375 current_encoding
= p
+ sizeof (ARG
) - 1;
380 if (p
[0] == '-' && p
[1] == 'f')
382 /* Some kind of -f option.
383 P's value is the option sans `-f'.
384 Search for it in the table of options. */
386 if (process_option_with_no (p
, lang_f_options
,
387 ARRAY_SIZE (lang_f_options
)))
389 return dump_switch_p (p
);
392 if (strcmp (p
, "-Wall") == 0)
396 flag_extraneous_semicolon
= 1;
397 /* When -Wall given, enable -Wunused. We do this because the C
398 compiler does it, and people expect it. */
403 if (p
[0] == '-' && p
[1] == 'W')
405 /* Skip `-W' and see if we accept the option or its `no-' form. */
407 return process_option_with_no (p
, lang_W_options
,
408 ARRAY_SIZE (lang_W_options
));
411 if (strcmp (p
, "-MD") == 0)
413 jcf_dependency_init (1);
414 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
417 else if (strcmp (p
, "-MMD") == 0)
419 jcf_dependency_init (0);
420 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
423 else if (strcmp (p
, "-M") == 0)
425 jcf_dependency_init (1);
426 dependency_tracking
|= DEPEND_ENABLE
;
429 else if (strcmp (p
, "-MM") == 0)
431 jcf_dependency_init (0);
432 dependency_tracking
|= DEPEND_ENABLE
;
435 else if (strcmp (p
, "-MP") == 0)
437 jcf_dependency_print_dummies ();
440 else if (strcmp (p
, "-MT") == 0)
442 jcf_dependency_set_target (argv
[1]);
443 dependency_tracking
|= DEPEND_TARGET_SET
;
446 else if (strcmp (p
, "-MF") == 0)
448 jcf_dependency_set_dep_file (argv
[1]);
449 dependency_tracking
|= DEPEND_FILE_ALREADY_SET
;
456 /* Global open file. */
461 const char *filename
;
464 extern int flag_minimal_debug
;
465 flag_minimal_debug
= 0;
468 /* Open input file. */
470 if (filename
== 0 || !strcmp (filename
, "-"))
475 if (dependency_tracking
)
476 error ("can't do dependency tracking with input from stdin");
480 if (dependency_tracking
)
484 /* If the target is set and the output filename is set, then
485 there's no processing to do here. Otherwise we must
486 compute one or the other. */
487 if (! ((dependency_tracking
& DEPEND_TARGET_SET
)
488 && (dependency_tracking
& DEPEND_FILE_ALREADY_SET
)))
490 dot
= strrchr (filename
, '.');
492 error ("couldn't determine target name for dependency tracking");
495 char *buf
= (char *) xmalloc (dot
- filename
+
496 3 + sizeof (TARGET_OBJECT_SUFFIX
));
497 strncpy (buf
, filename
, dot
- filename
);
499 /* If emitting class files, we might have multiple
500 targets. The class generation code takes care of
501 registering them. Otherwise we compute the
503 if ((dependency_tracking
& DEPEND_TARGET_SET
))
505 else if (flag_emit_class_files
)
506 jcf_dependency_set_target (NULL
);
509 strcpy (buf
+ (dot
- filename
), TARGET_OBJECT_SUFFIX
);
510 jcf_dependency_set_target (buf
);
513 if ((dependency_tracking
& DEPEND_FILE_ALREADY_SET
))
515 else if ((dependency_tracking
& DEPEND_SET_FILE
))
517 strcpy (buf
+ (dot
- filename
), ".d");
518 jcf_dependency_set_dep_file (buf
);
521 jcf_dependency_set_dep_file ("-");
530 jcf_path_seal (version_flag
);
532 java_init_decl_processing ();
534 using_eh_for_cleanups ();
542 jcf_dependency_write ();
545 /* Buffer used by lang_printable_name. */
546 static char *decl_buf
= NULL
;
548 /* Allocated size of decl_buf. */
549 static int decl_buflen
= 0;
551 /* Length of used part of decl_buf; position for next character. */
552 static int decl_bufpos
= 0;
554 /* Append the string STR to decl_buf.
555 It length is given by LEN; -1 means the string is nul-terminated. */
558 put_decl_string (str
, len
)
564 if (decl_bufpos
+ len
>= decl_buflen
)
566 if (decl_buf
== NULL
)
568 decl_buflen
= len
+ 100;
569 decl_buf
= (char *) xmalloc (decl_buflen
);
574 decl_buf
= (char *) xrealloc (decl_buf
, decl_buflen
);
577 strcpy (decl_buf
+ decl_bufpos
, str
);
581 /* Append to decl_buf a printable name for NODE. */
588 if (TREE_CODE (node
) == POINTER_TYPE
)
590 node
= TREE_TYPE (node
);
593 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd'
594 && DECL_NAME (node
) != NULL_TREE
)
596 if (TREE_CODE (node
) == FUNCTION_DECL
)
598 /* We want to print the type the DECL belongs to. We don't do
599 that when we handle constructors. */
600 if (! DECL_CONSTRUCTOR_P (node
)
601 && ! DECL_ARTIFICIAL (node
) && DECL_CONTEXT (node
))
603 put_decl_node (TYPE_NAME (DECL_CONTEXT (node
)));
604 put_decl_string (".", 1);
606 if (! DECL_CONSTRUCTOR_P (node
))
607 put_decl_node (DECL_NAME (node
));
608 if (TREE_TYPE (node
) != NULL_TREE
)
611 tree args
= TYPE_ARG_TYPES (TREE_TYPE (node
));
612 if (TREE_CODE (TREE_TYPE (node
)) == METHOD_TYPE
)
613 args
= TREE_CHAIN (args
);
614 put_decl_string ("(", 1);
615 for ( ; args
!= end_params_node
; args
= TREE_CHAIN (args
), i
++)
618 put_decl_string (",", 1);
619 put_decl_node (TREE_VALUE (args
));
621 put_decl_string (")", 1);
625 put_decl_node (DECL_NAME (node
));
627 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't'
628 && TYPE_NAME (node
) != NULL_TREE
)
630 if (TREE_CODE (node
) == RECORD_TYPE
&& TYPE_ARRAY_P (node
))
632 put_decl_node (TYPE_ARRAY_ELEMENT (node
));
633 put_decl_string("[]", 2);
635 else if (node
== promoted_byte_type_node
)
636 put_decl_string ("byte", 4);
637 else if (node
== promoted_short_type_node
)
638 put_decl_string ("short", 5);
639 else if (node
== promoted_char_type_node
)
640 put_decl_string ("char", 4);
641 else if (node
== promoted_boolean_type_node
)
642 put_decl_string ("boolean", 7);
643 else if (node
== void_type_node
&& was_pointer
)
644 put_decl_string ("null", 4);
646 put_decl_node (TYPE_NAME (node
));
648 else if (TREE_CODE (node
) == IDENTIFIER_NODE
)
649 put_decl_string (IDENTIFIER_POINTER (node
), IDENTIFIER_LENGTH (node
));
651 put_decl_string ("<unknown>", -1);
654 /* Return a user-friendly name for DECL.
655 The resulting string is only valid until the next call.
656 The value of the hook decl_printable_name is this function,
657 which is also called directly by java_print_error_function. */
660 lang_printable_name (decl
, v
)
662 int v
__attribute__ ((__unused__
));
665 put_decl_node (decl
);
666 put_decl_string ("", 1);
670 /* Does the same thing that lang_printable_name, but add a leading
671 space to the DECL name string -- With Leading Space. */
674 lang_printable_name_wls (decl
, v
)
676 int v
__attribute__ ((__unused__
));
679 put_decl_node (decl
);
680 put_decl_string ("", 1);
685 /* Print on stderr the current class and method context. This function
686 is the value of the hook print_error_function. */
689 java_print_error_function (context
, file
)
690 diagnostic_context
*context
__attribute__((__unused__
));
693 static tree last_error_function_context
= NULL_TREE
;
694 static tree last_error_function
= NULL
;
695 static int initialized_p
;
697 /* Register LAST_ERROR_FUNCTION_CONTEXT and LAST_ERROR_FUNCTION with
698 the garbage collector. */
701 ggc_add_tree_root (&last_error_function_context
, 1);
702 ggc_add_tree_root (&last_error_function
, 1);
706 /* Don't print error messages with bogus function prototypes. */
707 if (inhibit_error_function_printing
)
710 if (current_function_decl
!= NULL
711 && DECL_CONTEXT (current_function_decl
) != last_error_function_context
)
714 fprintf (stderr
, "%s: ", file
);
716 last_error_function_context
= DECL_CONTEXT (current_function_decl
);
717 fprintf (stderr
, "In class `%s':\n",
718 lang_printable_name (last_error_function_context
, 0));
720 if (last_error_function
!= current_function_decl
)
723 fprintf (stderr
, "%s: ", file
);
725 if (current_function_decl
== NULL
)
726 fprintf (stderr
, "At top level:\n");
729 const char *name
= lang_printable_name (current_function_decl
, 2);
730 fprintf (stderr
, "In %s `%s':\n",
731 (DECL_CONSTRUCTOR_P (current_function_decl
) ? "constructor"
736 last_error_function
= current_function_decl
;
741 /* Called to install the PRINT_ERROR_FUNCTION hook differently
742 according to LEVEL. LEVEL is 1 during early parsing, when function
743 prototypes aren't fully resolved. java_print_error_function is set
744 so it doesn't print incomplete function prototypes. When LEVEL is
745 2, function prototypes are fully resolved and can be printed when
748 void lang_init_source (level
)
751 inhibit_error_function_printing
= (level
== 1);
757 flag_bounds_check
= 1;
759 flag_non_call_exceptions
= 1;
761 /* In Java floating point operations never trap. */
762 flag_trapping_math
= 0;