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_dummy_print
PARAMS ((diagnostic_context
*, const char *));
58 static void lang_print_error
PARAMS ((diagnostic_context
*, const char *));
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 int compiling_from_source
;
106 char * resource_name
;
108 int flag_emit_class_files
= 0;
110 /* Nonzero if input file is a file with a list of filenames to compile. */
112 int flag_filelist_file
= 0;
114 /* When non zero, we emit xref strings. Values of the flag for xref
115 backends are defined in xref_flag_table, xref.c. */
117 int flag_emit_xref
= 0;
119 /* When non zero, -Wall was turned on. */
122 /* When non zero, check for redundant modifier uses. */
123 int flag_redundant
= 0;
125 /* When non zero, call a library routine to do integer divisions. */
126 int flag_use_divide_subroutine
= 1;
128 /* When non zero, generate code for the Boehm GC. */
129 int flag_use_boehm_gc
= 0;
131 /* When non zero, assume the runtime uses a hash table to map an
132 object to its synchronization structure. */
133 int flag_hash_synchronization
;
135 /* When non zero, assume all native functions are implemented with
139 /* When non zero, warn when source file is newer than matching class
143 /* When non zero, generate checks for references to NULL. */
144 int flag_check_references
= 0;
146 /* The encoding of the source file. */
147 const char *current_encoding
= NULL
;
149 /* When non zero, report the now deprecated empty statements. */
150 int flag_extraneous_semicolon
;
152 /* When non zero, always check for a non gcj generated classes archive. */
153 int flag_force_classes_archive_check
;
155 /* When zero, don't optimize static class initialization. This flag shouldn't
156 be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead. */
157 int flag_optimize_sci
= 1;
159 /* When non zero, use offset tables for virtual method calls
160 in order to improve binary compatibility. */
161 int flag_indirect_dispatch
= 0;
163 /* When zero, don't generate runtime array store checks. */
164 int flag_store_check
= 1;
166 /* When non zero, print extra version information. */
167 static int version_flag
= 0;
169 /* Table of language-dependent -f options.
170 STRING is the option name. VARIABLE is the address of the variable.
171 ON_VALUE is the value to store in VARIABLE
172 if `-fSTRING' is seen as an option.
173 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
175 static const struct string_option
178 {"emit-class-file", &flag_emit_class_files
, 1},
179 {"emit-class-files", &flag_emit_class_files
, 1},
180 {"filelist-file", &flag_filelist_file
, 1},
181 {"use-divide-subroutine", &flag_use_divide_subroutine
, 1},
182 {"use-boehm-gc", &flag_use_boehm_gc
, 1},
183 {"hash-synchronization", &flag_hash_synchronization
, 1},
184 {"jni", &flag_jni
, 1},
185 {"check-references", &flag_check_references
, 1},
186 {"force-classes-archive-check", &flag_force_classes_archive_check
, 1},
187 {"optimize-static-class-initialization", &flag_optimize_sci
, 1 },
188 {"indirect-dispatch", &flag_indirect_dispatch
, 1},
189 {"store-check", &flag_store_check
, 1}
192 static const struct string_option
195 { "redundant-modifiers", &flag_redundant
, 1 },
196 { "extraneous-semicolon", &flag_extraneous_semicolon
, 1 },
197 { "out-of-date", &flag_newer
, 1 }
202 /* Variable controlling how dependency tracking is enabled in
204 static int dependency_tracking
= 0;
206 /* Flag values for DEPENDENCY_TRACKING. */
207 #define DEPEND_SET_FILE 1
208 #define DEPEND_ENABLE 2
209 #define DEPEND_TARGET_SET 4
210 #define DEPEND_FILE_ALREADY_SET 8
212 #undef LANG_HOOKS_NAME
213 #define LANG_HOOKS_NAME "GNU Java"
214 #undef LANG_HOOKS_INIT
215 #define LANG_HOOKS_INIT java_init
216 #undef LANG_HOOKS_FINISH
217 #define LANG_HOOKS_FINISH java_finish
218 #undef LANG_HOOKS_INIT_OPTIONS
219 #define LANG_HOOKS_INIT_OPTIONS java_init_options
220 #undef LANG_HOOKS_DECODE_OPTION
221 #define LANG_HOOKS_DECODE_OPTION java_decode_option
222 #undef LANG_HOOKS_SET_YYDEBUG
223 #define LANG_HOOKS_SET_YYDEBUG java_set_yydebug
224 #undef LANG_HOOKS_PARSE_FILE
225 #define LANG_HOOKS_PARSE_FILE java_parse_file
226 #undef LANG_HOOKS_MARK_TREE
227 #define LANG_HOOKS_MARK_TREE java_mark_tree
228 #undef LANG_HOOKS_EXPAND_EXPR
229 #define LANG_HOOKS_EXPAND_EXPR java_expand_expr
230 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
231 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
232 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
233 #define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
235 /* Each front end provides its own. */
236 const struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
238 /* Process an option that can accept a `no-' form.
239 Return 1 if option found, 0 otherwise. */
241 process_option_with_no (p
, table
, table_size
)
243 const struct string_option
*table
;
248 for (j
= 0; j
< table_size
; j
++)
250 if (!strcmp (p
, table
[j
].string
))
252 *table
[j
].variable
= table
[j
].on_value
;
255 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-'
256 && ! strcmp (p
+3, table
[j
].string
))
258 *table
[j
].variable
= ! table
[j
].on_value
;
267 * process java-specific compiler command-line options
268 * return 0, but do not complain if the option is not recognised.
271 java_decode_option (argc
, argv
)
272 int argc
__attribute__ ((__unused__
));
279 if (strcmp (p
, "-version") == 0)
282 /* We return 0 so that the caller can process this. */
286 #define CLARG "-fcompile-resource="
287 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
289 resource_name
= p
+ sizeof (CLARG
) - 1;
293 #define CLARG "-fassume-compiled="
294 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
296 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 0);
300 #define CLARG "-fno-assume-compiled="
301 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
303 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 1);
307 #define CLARG "-fassume-compiled"
308 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
310 add_assume_compiled ("", 0);
314 #define CLARG "-fno-assume-compiled"
315 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
317 add_assume_compiled ("", 1);
321 #define CLARG "-fCLASSPATH="
322 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
324 jcf_path_classpath_arg (p
+ sizeof (CLARG
) - 1);
328 #define CLARG "-fclasspath="
329 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
331 jcf_path_classpath_arg (p
+ sizeof (CLARG
) - 1);
335 #define CLARG "-fbootclasspath="
336 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
338 jcf_path_bootclasspath_arg (p
+ sizeof (CLARG
) - 1);
342 else if (strncmp (p
, "-I", 2) == 0)
344 jcf_path_include_arg (p
+ 2);
348 #define ARG "-foutput-class-dir="
349 if (strncmp (p
, ARG
, sizeof (ARG
) - 1) == 0)
351 jcf_write_base_directory
= p
+ sizeof (ARG
) - 1;
355 #define ARG "-fencoding="
356 if (strncmp (p
, ARG
, sizeof (ARG
) - 1) == 0)
358 current_encoding
= p
+ sizeof (ARG
) - 1;
363 if (p
[0] == '-' && p
[1] == 'f')
365 /* Some kind of -f option.
366 P's value is the option sans `-f'.
367 Search for it in the table of options. */
369 if (process_option_with_no (p
, lang_f_options
,
370 ARRAY_SIZE (lang_f_options
)))
372 return dump_switch_p (p
);
375 if (strcmp (p
, "-Wall") == 0)
379 flag_extraneous_semicolon
= 1;
380 /* When -Wall given, enable -Wunused. We do this because the C
381 compiler does it, and people expect it. */
386 if (p
[0] == '-' && p
[1] == 'W')
388 /* Skip `-W' and see if we accept the option or its `no-' form. */
390 return process_option_with_no (p
, lang_W_options
,
391 ARRAY_SIZE (lang_W_options
));
394 if (strcmp (p
, "-MD") == 0)
396 jcf_dependency_init (1);
397 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
400 else if (strcmp (p
, "-MMD") == 0)
402 jcf_dependency_init (0);
403 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
406 else if (strcmp (p
, "-M") == 0)
408 jcf_dependency_init (1);
409 dependency_tracking
|= DEPEND_ENABLE
;
412 else if (strcmp (p
, "-MM") == 0)
414 jcf_dependency_init (0);
415 dependency_tracking
|= DEPEND_ENABLE
;
418 else if (strcmp (p
, "-MP") == 0)
420 jcf_dependency_print_dummies ();
423 else if (strcmp (p
, "-MT") == 0)
425 jcf_dependency_set_target (argv
[1]);
426 dependency_tracking
|= DEPEND_TARGET_SET
;
429 else if (strcmp (p
, "-MF") == 0)
431 jcf_dependency_set_dep_file (argv
[1]);
432 dependency_tracking
|= DEPEND_FILE_ALREADY_SET
;
439 /* Global open file. */
444 const char *filename
;
447 extern int flag_minimal_debug
;
448 flag_minimal_debug
= 0;
451 /* Open input file. */
453 if (filename
== 0 || !strcmp (filename
, "-"))
458 if (dependency_tracking
)
459 error ("can't do dependency tracking with input from stdin");
463 if (dependency_tracking
)
467 /* If the target is set and the output filename is set, then
468 there's no processing to do here. Otherwise we must
469 compute one or the other. */
470 if (! ((dependency_tracking
& DEPEND_TARGET_SET
)
471 && (dependency_tracking
& DEPEND_FILE_ALREADY_SET
)))
473 dot
= strrchr (filename
, '.');
475 error ("couldn't determine target name for dependency tracking");
478 char *buf
= (char *) xmalloc (dot
- filename
+
479 3 + sizeof (TARGET_OBJECT_SUFFIX
));
480 strncpy (buf
, filename
, dot
- filename
);
482 /* If emitting class files, we might have multiple
483 targets. The class generation code takes care of
484 registering them. Otherwise we compute the
486 if ((dependency_tracking
& DEPEND_TARGET_SET
))
488 else if (flag_emit_class_files
)
489 jcf_dependency_set_target (NULL
);
492 strcpy (buf
+ (dot
- filename
), TARGET_OBJECT_SUFFIX
);
493 jcf_dependency_set_target (buf
);
496 if ((dependency_tracking
& DEPEND_FILE_ALREADY_SET
))
498 else if ((dependency_tracking
& DEPEND_SET_FILE
))
500 strcpy (buf
+ (dot
- filename
), ".d");
501 jcf_dependency_set_dep_file (buf
);
504 jcf_dependency_set_dep_file ("-");
513 jcf_path_seal (version_flag
);
515 print_error_function
= lang_print_error
;
517 java_init_decl_processing ();
519 using_eh_for_cleanups ();
527 jcf_dependency_write ();
530 /* Buffer used by lang_printable_name. */
531 static char *decl_buf
= NULL
;
533 /* Allocated size of decl_buf. */
534 static int decl_buflen
= 0;
536 /* Length of used part of decl_buf; position for next character. */
537 static int decl_bufpos
= 0;
539 /* Append the string STR to decl_buf.
540 It length is given by LEN; -1 means the string is nul-terminated. */
543 put_decl_string (str
, len
)
549 if (decl_bufpos
+ len
>= decl_buflen
)
551 if (decl_buf
== NULL
)
553 decl_buflen
= len
+ 100;
554 decl_buf
= (char *) xmalloc (decl_buflen
);
559 decl_buf
= (char *) xrealloc (decl_buf
, decl_buflen
);
562 strcpy (decl_buf
+ decl_bufpos
, str
);
566 /* Append to decl_buf a printable name for NODE. */
573 if (TREE_CODE (node
) == POINTER_TYPE
)
575 node
= TREE_TYPE (node
);
578 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd'
579 && DECL_NAME (node
) != NULL_TREE
)
581 if (TREE_CODE (node
) == FUNCTION_DECL
)
583 /* We want to print the type the DECL belongs to. We don't do
584 that when we handle constructors. */
585 if (! DECL_CONSTRUCTOR_P (node
)
586 && ! DECL_ARTIFICIAL (node
) && DECL_CONTEXT (node
))
588 put_decl_node (TYPE_NAME (DECL_CONTEXT (node
)));
589 put_decl_string (".", 1);
591 if (! DECL_CONSTRUCTOR_P (node
))
592 put_decl_node (DECL_NAME (node
));
593 if (TREE_TYPE (node
) != NULL_TREE
)
596 tree args
= TYPE_ARG_TYPES (TREE_TYPE (node
));
597 if (TREE_CODE (TREE_TYPE (node
)) == METHOD_TYPE
)
598 args
= TREE_CHAIN (args
);
599 put_decl_string ("(", 1);
600 for ( ; args
!= end_params_node
; args
= TREE_CHAIN (args
), i
++)
603 put_decl_string (",", 1);
604 put_decl_node (TREE_VALUE (args
));
606 put_decl_string (")", 1);
610 put_decl_node (DECL_NAME (node
));
612 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't'
613 && TYPE_NAME (node
) != NULL_TREE
)
615 if (TREE_CODE (node
) == RECORD_TYPE
&& TYPE_ARRAY_P (node
))
617 put_decl_node (TYPE_ARRAY_ELEMENT (node
));
618 put_decl_string("[]", 2);
620 else if (node
== promoted_byte_type_node
)
621 put_decl_string ("byte", 4);
622 else if (node
== promoted_short_type_node
)
623 put_decl_string ("short", 5);
624 else if (node
== promoted_char_type_node
)
625 put_decl_string ("char", 4);
626 else if (node
== promoted_boolean_type_node
)
627 put_decl_string ("boolean", 7);
628 else if (node
== void_type_node
&& was_pointer
)
629 put_decl_string ("null", 4);
631 put_decl_node (TYPE_NAME (node
));
633 else if (TREE_CODE (node
) == IDENTIFIER_NODE
)
634 put_decl_string (IDENTIFIER_POINTER (node
), IDENTIFIER_LENGTH (node
));
636 put_decl_string ("<unknown>", -1);
639 /* Return a user-friendly name for DECL.
640 The resulting string is only valid until the next call.
641 The value of the hook decl_printable_name is this function,
642 which is also called directly by lang_print_error. */
645 lang_printable_name (decl
, v
)
647 int v
__attribute__ ((__unused__
));
650 put_decl_node (decl
);
651 put_decl_string ("", 1);
655 /* Does the same thing that lang_printable_name, but add a leading
656 space to the DECL name string -- With Leading Space. */
659 lang_printable_name_wls (decl
, v
)
661 int v
__attribute__ ((__unused__
));
664 put_decl_node (decl
);
665 put_decl_string ("", 1);
670 /* Print on stderr the current class and method context. This function
671 is the value of the hook print_error_function, called from toplev.c. */
674 lang_print_error (context
, file
)
675 diagnostic_context
*context
__attribute__((__unused__
));
678 static tree last_error_function_context
= NULL_TREE
;
679 static tree last_error_function
= NULL
;
680 static int initialized_p
;
682 /* Register LAST_ERROR_FUNCTION_CONTEXT and LAST_ERROR_FUNCTION with
683 the garbage collector. */
686 ggc_add_tree_root (&last_error_function_context
, 1);
687 ggc_add_tree_root (&last_error_function
, 1);
691 if (current_function_decl
!= NULL
692 && DECL_CONTEXT (current_function_decl
) != last_error_function_context
)
695 fprintf (stderr
, "%s: ", file
);
697 last_error_function_context
= DECL_CONTEXT (current_function_decl
);
698 fprintf (stderr
, "In class `%s':\n",
699 lang_printable_name (last_error_function_context
, 0));
701 if (last_error_function
!= current_function_decl
)
704 fprintf (stderr
, "%s: ", file
);
706 if (current_function_decl
== NULL
)
707 fprintf (stderr
, "At top level:\n");
710 const char *name
= lang_printable_name (current_function_decl
, 2);
711 fprintf (stderr
, "In %s `%s':\n",
712 (DECL_CONSTRUCTOR_P (current_function_decl
) ? "constructor"
717 last_error_function
= current_function_decl
;
722 /* This doesn't do anything on purpose. It's used to satisfy the
723 print_error_function hook we don't print error messages with bogus
724 function prototypes. */
727 java_dummy_print (c
, s
)
728 diagnostic_context
*c
__attribute__ ((__unused__
));
729 const char *s
__attribute__ ((__unused__
));
733 /* Called to install the PRINT_ERROR_FUNCTION hook differently
734 according to LEVEL. LEVEL is 1 during early parsing, when function
735 prototypes aren't fully resolved. print_error_function is set so it
736 doesn't print incomplete function prototypes. When LEVEL is 2,
737 function prototypes are fully resolved and can be printed when
740 void lang_init_source (level
)
744 print_error_function
= java_dummy_print
;
746 print_error_function
= lang_print_error
;
752 flag_bounds_check
= 1;
754 flag_non_call_exceptions
= 1;
756 /* In Java floating point operations never trap. */
757 flag_trapping_math
= 0;