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 static const char java_tree_code_type
[] = {
75 #include "java-tree.def"
79 /* Table indexed by tree code giving number of expression
80 operands beyond the fixed part of the node structure.
81 Not used for types or decls. */
83 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
85 static const int java_tree_code_length
[] = {
87 #include "java-tree.def"
91 /* Names of tree components.
92 Used for printing out the tree and error messages. */
93 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
95 static const char *const java_tree_code_name
[] = {
97 #include "java-tree.def"
101 int compiling_from_source
;
103 char * resource_name
;
105 int flag_emit_class_files
= 0;
107 /* Nonzero if input file is a file with a list of filenames to compile. */
109 int flag_filelist_file
= 0;
111 /* When non zero, we emit xref strings. Values of the flag for xref
112 backends are defined in xref_flag_table, xref.c. */
114 int flag_emit_xref
= 0;
116 /* When non zero, -Wall was turned on. */
119 /* When non zero, check for redundant modifier uses. */
120 int flag_redundant
= 0;
122 /* When non zero, call a library routine to do integer divisions. */
123 int flag_use_divide_subroutine
= 1;
125 /* When non zero, generate code for the Boehm GC. */
126 int flag_use_boehm_gc
= 0;
128 /* When non zero, assume the runtime uses a hash table to map an
129 object to its synchronization structure. */
130 int flag_hash_synchronization
;
132 /* When non zero, assume all native functions are implemented with
136 /* When non zero, warn when source file is newer than matching class
140 /* When non zero, generate checks for references to NULL. */
141 int flag_check_references
= 0;
143 /* The encoding of the source file. */
144 const char *current_encoding
= NULL
;
146 /* When non zero, report the now deprecated empty statements. */
147 int flag_extraneous_semicolon
;
149 /* When non zero, always check for a non gcj generated classes archive. */
150 int flag_force_classes_archive_check
;
152 /* When zero, don't optimize static class initialization. This flag shouldn't
153 be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead. */
154 int flag_optimize_sci
= 1;
156 /* When non zero, use offset tables for virtual method calls
157 in order to improve binary compatibility. */
158 int flag_indirect_dispatch
= 0;
160 /* When zero, don't generate runtime array store checks. */
161 int flag_store_check
= 1;
163 /* When non zero, print extra version information. */
164 static int version_flag
= 0;
166 /* Table of language-dependent -f options.
167 STRING is the option name. VARIABLE is the address of the variable.
168 ON_VALUE is the value to store in VARIABLE
169 if `-fSTRING' is seen as an option.
170 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
172 static const struct string_option
175 {"emit-class-file", &flag_emit_class_files
, 1},
176 {"emit-class-files", &flag_emit_class_files
, 1},
177 {"filelist-file", &flag_filelist_file
, 1},
178 {"use-divide-subroutine", &flag_use_divide_subroutine
, 1},
179 {"use-boehm-gc", &flag_use_boehm_gc
, 1},
180 {"hash-synchronization", &flag_hash_synchronization
, 1},
181 {"jni", &flag_jni
, 1},
182 {"check-references", &flag_check_references
, 1},
183 {"force-classes-archive-check", &flag_force_classes_archive_check
, 1},
184 {"optimize-static-class-initialization", &flag_optimize_sci
, 1 },
185 {"indirect-dispatch", &flag_indirect_dispatch
, 1},
186 {"store-check", &flag_store_check
, 1}
189 static const struct string_option
192 { "redundant-modifiers", &flag_redundant
, 1 },
193 { "extraneous-semicolon", &flag_extraneous_semicolon
, 1 },
194 { "out-of-date", &flag_newer
, 1 }
199 /* Variable controlling how dependency tracking is enabled in
201 static int dependency_tracking
= 0;
203 /* Flag values for DEPENDENCY_TRACKING. */
204 #define DEPEND_SET_FILE 1
205 #define DEPEND_ENABLE 2
206 #define DEPEND_TARGET_SET 4
207 #define DEPEND_FILE_ALREADY_SET 8
209 #undef LANG_HOOKS_NAME
210 #define LANG_HOOKS_NAME "GNU Java"
211 #undef LANG_HOOKS_INIT
212 #define LANG_HOOKS_INIT java_init
213 #undef LANG_HOOKS_FINISH
214 #define LANG_HOOKS_FINISH java_finish
215 #undef LANG_HOOKS_INIT_OPTIONS
216 #define LANG_HOOKS_INIT_OPTIONS java_init_options
217 #undef LANG_HOOKS_DECODE_OPTION
218 #define LANG_HOOKS_DECODE_OPTION java_decode_option
219 #undef LANG_HOOKS_SET_YYDEBUG
220 #define LANG_HOOKS_SET_YYDEBUG java_set_yydebug
221 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
222 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
224 /* Each front end provides its own. */
225 const struct lang_hooks lang_hooks
= LANG_HOOKS_INITIALIZER
;
227 /* Process an option that can accept a `no-' form.
228 Return 1 if option found, 0 otherwise. */
230 process_option_with_no (p
, table
, table_size
)
232 const struct string_option
*table
;
237 for (j
= 0; j
< table_size
; j
++)
239 if (!strcmp (p
, table
[j
].string
))
241 *table
[j
].variable
= table
[j
].on_value
;
244 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-'
245 && ! strcmp (p
+3, table
[j
].string
))
247 *table
[j
].variable
= ! table
[j
].on_value
;
256 * process java-specific compiler command-line options
257 * return 0, but do not complain if the option is not recognised.
260 java_decode_option (argc
, argv
)
261 int argc
__attribute__ ((__unused__
));
268 if (strcmp (p
, "-version") == 0)
271 /* We return 0 so that the caller can process this. */
275 #define CLARG "-fcompile-resource="
276 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
278 resource_name
= p
+ sizeof (CLARG
) - 1;
282 #define CLARG "-fassume-compiled="
283 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
285 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 0);
289 #define CLARG "-fno-assume-compiled="
290 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
292 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 1);
296 #define CLARG "-fassume-compiled"
297 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
299 add_assume_compiled ("", 0);
303 #define CLARG "-fno-assume-compiled"
304 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
306 add_assume_compiled ("", 1);
310 #define CLARG "-fCLASSPATH="
311 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
313 jcf_path_classpath_arg (p
+ sizeof (CLARG
) - 1);
317 #define CLARG "-fclasspath="
318 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
320 jcf_path_classpath_arg (p
+ sizeof (CLARG
) - 1);
324 #define CLARG "-fbootclasspath="
325 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
327 jcf_path_bootclasspath_arg (p
+ sizeof (CLARG
) - 1);
331 else if (strncmp (p
, "-I", 2) == 0)
333 jcf_path_include_arg (p
+ 2);
337 #define ARG "-foutput-class-dir="
338 if (strncmp (p
, ARG
, sizeof (ARG
) - 1) == 0)
340 jcf_write_base_directory
= p
+ sizeof (ARG
) - 1;
344 #define ARG "-fencoding="
345 if (strncmp (p
, ARG
, sizeof (ARG
) - 1) == 0)
347 current_encoding
= p
+ sizeof (ARG
) - 1;
352 if (p
[0] == '-' && p
[1] == 'f')
354 /* Some kind of -f option.
355 P's value is the option sans `-f'.
356 Search for it in the table of options. */
358 if (process_option_with_no (p
, lang_f_options
,
359 ARRAY_SIZE (lang_f_options
)))
361 return dump_switch_p (p
);
364 if (strcmp (p
, "-Wall") == 0)
368 flag_extraneous_semicolon
= 1;
369 /* When -Wall given, enable -Wunused. We do this because the C
370 compiler does it, and people expect it. */
375 if (p
[0] == '-' && p
[1] == 'W')
377 /* Skip `-W' and see if we accept the option or its `no-' form. */
379 return process_option_with_no (p
, lang_W_options
,
380 ARRAY_SIZE (lang_W_options
));
383 if (strcmp (p
, "-MD") == 0)
385 jcf_dependency_init (1);
386 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
389 else if (strcmp (p
, "-MMD") == 0)
391 jcf_dependency_init (0);
392 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
395 else if (strcmp (p
, "-M") == 0)
397 jcf_dependency_init (1);
398 dependency_tracking
|= DEPEND_ENABLE
;
401 else if (strcmp (p
, "-MM") == 0)
403 jcf_dependency_init (0);
404 dependency_tracking
|= DEPEND_ENABLE
;
407 else if (strcmp (p
, "-MP") == 0)
409 jcf_dependency_print_dummies ();
412 else if (strcmp (p
, "-MT") == 0)
414 jcf_dependency_set_target (argv
[1]);
415 dependency_tracking
|= DEPEND_TARGET_SET
;
418 else if (strcmp (p
, "-MF") == 0)
420 jcf_dependency_set_dep_file (argv
[1]);
421 dependency_tracking
|= DEPEND_FILE_ALREADY_SET
;
428 /* Global open file. */
433 const char *filename
;
436 extern int flag_minimal_debug
;
437 flag_minimal_debug
= 0;
440 /* Open input file. */
442 if (filename
== 0 || !strcmp (filename
, "-"))
447 if (dependency_tracking
)
448 error ("can't do dependency tracking with input from stdin");
452 if (dependency_tracking
)
456 /* If the target is set and the output filename is set, then
457 there's no processing to do here. Otherwise we must
458 compute one or the other. */
459 if (! ((dependency_tracking
& DEPEND_TARGET_SET
)
460 && (dependency_tracking
& DEPEND_FILE_ALREADY_SET
)))
462 dot
= strrchr (filename
, '.');
464 error ("couldn't determine target name for dependency tracking");
467 char *buf
= (char *) xmalloc (dot
- filename
+
468 3 + sizeof (TARGET_OBJECT_SUFFIX
));
469 strncpy (buf
, filename
, dot
- filename
);
471 /* If emitting class files, we might have multiple
472 targets. The class generation code takes care of
473 registering them. Otherwise we compute the
475 if ((dependency_tracking
& DEPEND_TARGET_SET
))
477 else if (flag_emit_class_files
)
478 jcf_dependency_set_target (NULL
);
481 strcpy (buf
+ (dot
- filename
), TARGET_OBJECT_SUFFIX
);
482 jcf_dependency_set_target (buf
);
485 if ((dependency_tracking
& DEPEND_FILE_ALREADY_SET
))
487 else if ((dependency_tracking
& DEPEND_SET_FILE
))
489 strcpy (buf
+ (dot
- filename
), ".d");
490 jcf_dependency_set_dep_file (buf
);
493 jcf_dependency_set_dep_file ("-");
502 jcf_path_seal (version_flag
);
504 decl_printable_name
= lang_printable_name
;
505 print_error_function
= lang_print_error
;
506 lang_expand_expr
= java_lang_expand_expr
;
508 /* Append to Gcc tree node definition arrays */
510 memcpy (tree_code_type
+ (int) LAST_AND_UNUSED_TREE_CODE
,
512 (int)LAST_JAVA_TREE_CODE
- (int)LAST_AND_UNUSED_TREE_CODE
);
513 memcpy (tree_code_length
+ (int) LAST_AND_UNUSED_TREE_CODE
,
514 java_tree_code_length
,
515 (LAST_JAVA_TREE_CODE
-
516 (int)LAST_AND_UNUSED_TREE_CODE
) * sizeof (int));
517 memcpy (tree_code_name
+ (int) LAST_AND_UNUSED_TREE_CODE
,
519 (LAST_JAVA_TREE_CODE
-
520 (int)LAST_AND_UNUSED_TREE_CODE
) * sizeof (char *));
521 java_init_decl_processing ();
523 using_eh_for_cleanups ();
531 jcf_dependency_write ();
534 /* Buffer used by lang_printable_name. */
535 static char *decl_buf
= NULL
;
537 /* Allocated size of decl_buf. */
538 static int decl_buflen
= 0;
540 /* Length of used part of decl_buf; position for next character. */
541 static int decl_bufpos
= 0;
543 /* Append the string STR to decl_buf.
544 It length is given by LEN; -1 means the string is nul-terminated. */
547 put_decl_string (str
, len
)
553 if (decl_bufpos
+ len
>= decl_buflen
)
555 if (decl_buf
== NULL
)
557 decl_buflen
= len
+ 100;
558 decl_buf
= (char *) xmalloc (decl_buflen
);
563 decl_buf
= (char *) xrealloc (decl_buf
, decl_buflen
);
566 strcpy (decl_buf
+ decl_bufpos
, str
);
570 /* Append to decl_buf a printable name for NODE. */
577 if (TREE_CODE (node
) == POINTER_TYPE
)
579 node
= TREE_TYPE (node
);
582 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd'
583 && DECL_NAME (node
) != NULL_TREE
)
585 if (TREE_CODE (node
) == FUNCTION_DECL
)
587 /* We want to print the type the DECL belongs to. We don't do
588 that when we handle constructors. */
589 if (! DECL_CONSTRUCTOR_P (node
)
590 && ! DECL_ARTIFICIAL (node
) && DECL_CONTEXT (node
))
592 put_decl_node (TYPE_NAME (DECL_CONTEXT (node
)));
593 put_decl_string (".", 1);
595 if (! DECL_CONSTRUCTOR_P (node
))
596 put_decl_node (DECL_NAME (node
));
597 if (TREE_TYPE (node
) != NULL_TREE
)
600 tree args
= TYPE_ARG_TYPES (TREE_TYPE (node
));
601 if (TREE_CODE (TREE_TYPE (node
)) == METHOD_TYPE
)
602 args
= TREE_CHAIN (args
);
603 put_decl_string ("(", 1);
604 for ( ; args
!= end_params_node
; args
= TREE_CHAIN (args
), i
++)
607 put_decl_string (",", 1);
608 put_decl_node (TREE_VALUE (args
));
610 put_decl_string (")", 1);
614 put_decl_node (DECL_NAME (node
));
616 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't'
617 && TYPE_NAME (node
) != NULL_TREE
)
619 if (TREE_CODE (node
) == RECORD_TYPE
&& TYPE_ARRAY_P (node
))
621 put_decl_node (TYPE_ARRAY_ELEMENT (node
));
622 put_decl_string("[]", 2);
624 else if (node
== promoted_byte_type_node
)
625 put_decl_string ("byte", 4);
626 else if (node
== promoted_short_type_node
)
627 put_decl_string ("short", 5);
628 else if (node
== promoted_char_type_node
)
629 put_decl_string ("char", 4);
630 else if (node
== promoted_boolean_type_node
)
631 put_decl_string ("boolean", 7);
632 else if (node
== void_type_node
&& was_pointer
)
633 put_decl_string ("null", 4);
635 put_decl_node (TYPE_NAME (node
));
637 else if (TREE_CODE (node
) == IDENTIFIER_NODE
)
638 put_decl_string (IDENTIFIER_POINTER (node
), IDENTIFIER_LENGTH (node
));
640 put_decl_string ("<unknown>", -1);
643 /* Return a user-friendly name for DECL.
644 The resulting string is only valid until the next call.
645 The value of the hook decl_printable_name is this function,
646 which is also called directly by lang_print_error. */
649 lang_printable_name (decl
, v
)
651 int v
__attribute__ ((__unused__
));
654 put_decl_node (decl
);
655 put_decl_string ("", 1);
659 /* Does the same thing that lang_printable_name, but add a leading
660 space to the DECL name string -- With Leading Space. */
663 lang_printable_name_wls (decl
, v
)
665 int v
__attribute__ ((__unused__
));
668 put_decl_node (decl
);
669 put_decl_string ("", 1);
674 /* Print on stderr the current class and method context. This function
675 is the value of the hook print_error_function, called from toplev.c. */
678 lang_print_error (context
, file
)
679 diagnostic_context
*context
__attribute__((__unused__
));
682 static tree last_error_function_context
= NULL_TREE
;
683 static tree last_error_function
= NULL
;
684 static int initialized_p
;
686 /* Register LAST_ERROR_FUNCTION_CONTEXT and LAST_ERROR_FUNCTION with
687 the garbage collector. */
690 ggc_add_tree_root (&last_error_function_context
, 1);
691 ggc_add_tree_root (&last_error_function
, 1);
695 if (current_function_decl
!= NULL
696 && DECL_CONTEXT (current_function_decl
) != last_error_function_context
)
699 fprintf (stderr
, "%s: ", file
);
701 last_error_function_context
= DECL_CONTEXT (current_function_decl
);
702 fprintf (stderr
, "In class `%s':\n",
703 lang_printable_name (last_error_function_context
, 0));
705 if (last_error_function
!= current_function_decl
)
708 fprintf (stderr
, "%s: ", file
);
710 if (current_function_decl
== NULL
)
711 fprintf (stderr
, "At top level:\n");
714 const char *name
= lang_printable_name (current_function_decl
, 2);
715 fprintf (stderr
, "In %s `%s':\n",
716 (DECL_CONSTRUCTOR_P (current_function_decl
) ? "constructor"
721 last_error_function
= current_function_decl
;
726 /* This doesn't do anything on purpose. It's used to satisfy the
727 print_error_function hook we don't print error messages with bogus
728 function prototypes. */
731 java_dummy_print (c
, s
)
732 diagnostic_context
*c
__attribute__ ((__unused__
));
733 const char *s
__attribute__ ((__unused__
));
737 /* Called to install the PRINT_ERROR_FUNCTION hook differently
738 according to LEVEL. LEVEL is 1 during early parsing, when function
739 prototypes aren't fully resolved. print_error_function is set so it
740 doesn't print incomplete function prototypes. When LEVEL is 2,
741 function prototypes are fully resolved and can be printed when
744 void lang_init_source (level
)
748 print_error_function
= java_dummy_print
;
750 print_error_function
= lang_print_error
;
756 flag_bounds_check
= 1;
758 flag_non_call_exceptions
= 1;