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"
48 static void java_init
PARAMS ((void));
49 static void java_init_options
PARAMS ((void));
50 static int java_decode_option
PARAMS ((int, char **));
51 static void put_decl_string
PARAMS ((const char *, int));
52 static void put_decl_node
PARAMS ((tree
));
53 static void java_dummy_print
PARAMS ((const char *));
54 static void lang_print_error
PARAMS ((const char *));
55 static int process_option_with_no
PARAMS ((char *,
56 struct string_option
*,
60 # define OBJECT_SUFFIX ".o"
63 /* Table indexed by tree code giving a string containing a character
64 classifying the tree code. Possibilities are
65 t, d, s, c, r, <, 1 and 2. See java/java-tree.def for details. */
67 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
69 char java_tree_code_type
[] = {
71 #include "java-tree.def"
75 /* Table indexed by tree code giving number of expression
76 operands beyond the fixed part of the node structure.
77 Not used for types or decls. */
79 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
81 int java_tree_code_length
[] = {
83 #include "java-tree.def"
87 /* Names of tree components.
88 Used for printing out the tree and error messages. */
89 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
91 const char *java_tree_code_name
[] = {
93 #include "java-tree.def"
97 int compiling_from_source
;
99 const char * const language_string
= "GNU Java";
101 /* Nonzero if we should make is_compiled_class always return 1 for
102 appropriate classes that we're referencing. */
104 int flag_assume_compiled
= 1;
106 int flag_emit_class_files
= 0;
108 /* When non zero, we emit xref strings. Values of the flag for xref
109 backends are defined in xref_flag_table, xref.c. */
111 int flag_emit_xref
= 0;
113 /* When non zero, -Wall was turned on. */
116 /* When non zero, check for redundant modifier uses. */
117 int flag_redundant
= 0;
119 /* When non zero, warns about overridings that don't occur. */
120 int flag_not_overriding
= 0;
122 /* When non zero, warns that final local are treated as non final. */
123 int flag_static_local_jdk1_1
= 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 /* 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 /* From gcc/flags.h, and indicates if exceptions are turned on or not. */
151 extern int flag_new_exceptions
;
152 extern int flag_exceptions
;
154 /* Table of language-dependent -f options.
155 STRING is the option name. VARIABLE is the address of the variable.
156 ON_VALUE is the value to store in VARIABLE
157 if `-fSTRING' is seen as an option.
158 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
160 static struct string_option
163 {"emit-class-file", &flag_emit_class_files
, 1},
164 {"emit-class-files", &flag_emit_class_files
, 1},
165 {"use-divide-subroutine", &flag_use_divide_subroutine
, 1},
166 {"use-boehm-gc", &flag_use_boehm_gc
, 1},
167 {"hash-synchronization", &flag_hash_synchronization
, 1},
168 {"jni", &flag_jni
, 1}
171 static struct string_option
174 { "unsupported-jdk11", &flag_static_local_jdk1_1
, 1 },
175 { "redundant-modifiers", &flag_redundant
, 1 },
176 { "extraneous-semicolon", &flag_extraneous_semicolon
, 1 },
177 { "out-of-date", &flag_newer
, 1 }
182 /* Variable controlling how dependency tracking is enabled in
184 static int dependency_tracking
= 0;
186 /* Flag values for DEPENDENCY_TRACKING. */
187 #define DEPEND_SET_FILE 1
188 #define DEPEND_ENABLE 2
189 #define DEPEND_TARGET_SET 4
190 #define DEPEND_FILE_ALREADY_SET 8
192 /* Each front end provides its own. */
193 struct lang_hooks lang_hooks
= {java_init
,
194 NULL
, /* java_finish */
197 NULL
/* post_options */};
199 /* Process an option that can accept a `no-' form.
200 Return 1 if option found, 0 otherwise. */
202 process_option_with_no (p
, table
, table_size
)
204 struct string_option
*table
;
209 for (j
= 0; j
< table_size
; j
++)
211 if (!strcmp (p
, table
[j
].string
))
213 *table
[j
].variable
= table
[j
].on_value
;
216 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-'
217 && ! strcmp (p
+3, table
[j
].string
))
219 *table
[j
].variable
= ! table
[j
].on_value
;
228 * process java-specific compiler command-line options
229 * return 0, but do not complain if the option is not recognised.
232 java_decode_option (argc
, argv
)
233 int argc
__attribute__ ((__unused__
));
238 #define CLARG "-fassume-compiled="
239 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
241 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 0);
245 #define CLARG "-fno-assume-compiled="
246 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
248 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 1);
252 #define CLARG "-fassume-compiled"
253 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
255 add_assume_compiled ("", 0);
259 #define CLARG "-fno-assume-compiled"
260 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
262 add_assume_compiled ("", 1);
266 #define CLARG "-fclasspath="
267 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
269 jcf_path_classpath_arg (p
+ sizeof (CLARG
) - 1);
273 #define CLARG "-fCLASSPATH="
274 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
276 jcf_path_CLASSPATH_arg (p
+ sizeof (CLARG
) - 1);
280 else if (strncmp (p
, "-I", 2) == 0)
282 jcf_path_include_arg (p
+ 2);
286 #define ARG "-foutput-class-dir="
287 if (strncmp (p
, ARG
, sizeof (ARG
) - 1) == 0)
289 jcf_write_base_directory
= p
+ sizeof (ARG
) - 1;
293 #define ARG "-fencoding="
294 if (strncmp (p
, ARG
, sizeof (ARG
) - 1) == 0)
296 current_encoding
= p
+ sizeof (ARG
) - 1;
301 if (p
[0] == '-' && p
[1] == 'f')
303 /* Some kind of -f option.
304 P's value is the option sans `-f'.
305 Search for it in the table of options. */
307 return process_option_with_no (p
, lang_f_options
,
308 ARRAY_SIZE (lang_f_options
));
311 if (strcmp (p
, "-Wall") == 0)
315 flag_extraneous_semicolon
= 1;
316 /* When -Wall given, enable -Wunused. We do this because the C
317 compiler does it, and people expect it. */
322 if (p
[0] == '-' && p
[1] == 'W')
324 /* Skip `-W' and see if we accept the option or its `no-' form. */
326 return process_option_with_no (p
, lang_W_options
,
327 ARRAY_SIZE (lang_W_options
));
330 if (strcmp (p
, "-MD") == 0)
332 jcf_dependency_init (1);
333 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
336 else if (strcmp (p
, "-MMD") == 0)
338 jcf_dependency_init (0);
339 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
342 else if (strcmp (p
, "-M") == 0)
344 jcf_dependency_init (1);
345 dependency_tracking
|= DEPEND_ENABLE
;
348 else if (strcmp (p
, "-MM") == 0)
350 jcf_dependency_init (0);
351 dependency_tracking
|= DEPEND_ENABLE
;
354 else if (strcmp (p
, "-MP") == 0)
356 jcf_dependency_print_dummies ();
359 else if (strcmp (p
, "-MT") == 0)
361 jcf_dependency_set_target (argv
[1]);
362 dependency_tracking
|= DEPEND_TARGET_SET
;
365 else if (strcmp (p
, "-MF") == 0)
367 jcf_dependency_set_dep_file (argv
[1]);
368 dependency_tracking
|= DEPEND_FILE_ALREADY_SET
;
375 /* Global open file. */
379 init_parse (filename
)
380 const char *filename
;
382 /* Open input file. */
384 if (filename
== 0 || !strcmp (filename
, "-"))
389 if (dependency_tracking
)
390 error ("can't do dependency tracking with input from stdin");
394 if (dependency_tracking
)
398 /* If the target is set and the output filename is set, then
399 there's no processing to do here. Otherwise we must
400 compute one or the other. */
401 if (! ((dependency_tracking
& DEPEND_TARGET_SET
)
402 && (dependency_tracking
& DEPEND_FILE_ALREADY_SET
)))
404 dot
= strrchr (filename
, '.');
406 error ("couldn't determine target name for dependency tracking");
409 char *buf
= (char *) xmalloc (dot
- filename
+
410 3 + sizeof (OBJECT_SUFFIX
));
411 strncpy (buf
, filename
, dot
- filename
);
413 /* If emitting class files, we might have multiple
414 targets. The class generation code takes care of
415 registering them. Otherwise we compute the
417 if ((dependency_tracking
& DEPEND_TARGET_SET
))
419 else if (flag_emit_class_files
)
420 jcf_dependency_set_target (NULL
);
423 strcpy (buf
+ (dot
- filename
), OBJECT_SUFFIX
);
424 jcf_dependency_set_target (buf
);
427 if ((dependency_tracking
& DEPEND_FILE_ALREADY_SET
))
429 else if ((dependency_tracking
& DEPEND_SET_FILE
))
431 strcpy (buf
+ (dot
- filename
), ".d");
432 jcf_dependency_set_dep_file (buf
);
435 jcf_dependency_set_dep_file ("-");
451 jcf_dependency_write ();
454 /* Buffer used by lang_printable_name. */
455 static char *decl_buf
= NULL
;
457 /* Allocated size of decl_buf. */
458 static int decl_buflen
= 0;
460 /* Length of used part of decl_buf; position for next character. */
461 static int decl_bufpos
= 0;
463 /* Append the string STR to decl_buf.
464 It length is given by LEN; -1 means the string is nul-terminated. */
467 put_decl_string (str
, len
)
473 if (decl_bufpos
+ len
>= decl_buflen
)
475 if (decl_buf
== NULL
)
477 decl_buflen
= len
+ 100;
478 decl_buf
= (char *) xmalloc (decl_buflen
);
483 decl_buf
= (char *) xrealloc (decl_buf
, decl_buflen
);
486 strcpy (decl_buf
+ decl_bufpos
, str
);
490 /* Append to decl_buf a printable name for NODE. */
497 if (TREE_CODE (node
) == POINTER_TYPE
)
499 node
= TREE_TYPE (node
);
502 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd'
503 && DECL_NAME (node
) != NULL_TREE
)
506 if (DECL_CONTEXT (node
) != NULL_TREE
)
508 put_decl_node (DECL_CONTEXT (node
));
509 put_decl_string (".", 1);
512 if (TREE_CODE (node
) == FUNCTION_DECL
513 && DECL_INIT_P (node
)
514 && !DECL_ARTIFICIAL (node
) && current_class
)
515 put_decl_node (TYPE_NAME (current_class
));
517 put_decl_node (DECL_NAME (node
));
518 if (TREE_CODE (node
) == FUNCTION_DECL
&& TREE_TYPE (node
) != NULL_TREE
)
521 tree args
= TYPE_ARG_TYPES (TREE_TYPE (node
));
522 if (TREE_CODE (TREE_TYPE (node
)) == METHOD_TYPE
)
523 args
= TREE_CHAIN (args
);
524 put_decl_string ("(", 1);
525 for ( ; args
!= end_params_node
; args
= TREE_CHAIN (args
), i
++)
528 put_decl_string (",", 1);
529 put_decl_node (TREE_VALUE (args
));
531 put_decl_string (")", 1);
534 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't'
535 && TYPE_NAME (node
) != NULL_TREE
)
537 if (TREE_CODE (node
) == RECORD_TYPE
&& TYPE_ARRAY_P (node
))
539 put_decl_node (TYPE_ARRAY_ELEMENT (node
));
540 put_decl_string("[]", 2);
542 else if (node
== promoted_byte_type_node
)
543 put_decl_string ("byte", 4);
544 else if (node
== promoted_short_type_node
)
545 put_decl_string ("short", 5);
546 else if (node
== promoted_char_type_node
)
547 put_decl_string ("char", 4);
548 else if (node
== promoted_boolean_type_node
)
549 put_decl_string ("boolean", 7);
550 else if (node
== void_type_node
&& was_pointer
)
551 put_decl_string ("null", 4);
553 put_decl_node (TYPE_NAME (node
));
555 else if (TREE_CODE (node
) == IDENTIFIER_NODE
)
556 put_decl_string (IDENTIFIER_POINTER (node
), IDENTIFIER_LENGTH (node
));
558 put_decl_string ("<unknown>", -1);
561 /* Return a user-friendly name for DECL.
562 The resulting string is only valid until the next call.
563 The value of the hook decl_printable_name is this function,
564 which is also called directly by lang_print_error. */
567 lang_printable_name (decl
, v
)
569 int v
__attribute__ ((__unused__
));
572 put_decl_node (decl
);
573 put_decl_string ("", 1);
577 /* Print on stderr the current class and method context. This function
578 is the value of the hook print_error_function, called from toplev.c. */
581 lang_print_error (file
)
584 static tree last_error_function_context
= NULL_TREE
;
585 static tree last_error_function
= NULL
;
586 static int initialized_p
;
588 /* Register LAST_ERROR_FUNCTION_CONTEXT and LAST_ERROR_FUNCTION with
589 the garbage collector. */
592 ggc_add_tree_root (&last_error_function_context
, 1);
593 ggc_add_tree_root (&last_error_function
, 1);
597 if (current_function_decl
!= NULL
598 && DECL_CONTEXT (current_function_decl
) != last_error_function_context
)
601 fprintf (stderr
, "%s: ", file
);
603 last_error_function_context
= DECL_CONTEXT (current_function_decl
);
604 fprintf (stderr
, "In class `%s':\n",
605 lang_printable_name (last_error_function_context
, 0));
607 if (last_error_function
!= current_function_decl
)
610 fprintf (stderr
, "%s: ", file
);
612 if (current_function_decl
== NULL
)
613 fprintf (stderr
, "At top level:\n");
616 const char *name
= lang_printable_name (current_function_decl
, 2);
617 fprintf (stderr
, "In method `%s':\n", name
);
620 last_error_function
= current_function_decl
;
629 extern int flag_minimal_debug
;
630 flag_minimal_debug
= 0;
636 decl_printable_name
= lang_printable_name
;
637 print_error_function
= lang_print_error
;
638 lang_expand_expr
= java_lang_expand_expr
;
642 /* Append to Gcc tree node definition arrays */
644 memcpy (tree_code_type
+ (int) LAST_AND_UNUSED_TREE_CODE
,
646 (int)LAST_JAVA_TREE_CODE
- (int)LAST_AND_UNUSED_TREE_CODE
);
647 memcpy (tree_code_length
+ (int) LAST_AND_UNUSED_TREE_CODE
,
648 java_tree_code_length
,
649 (LAST_JAVA_TREE_CODE
-
650 (int)LAST_AND_UNUSED_TREE_CODE
) * sizeof (int));
651 memcpy (tree_code_name
+ (int) LAST_AND_UNUSED_TREE_CODE
,
653 (LAST_JAVA_TREE_CODE
-
654 (int)LAST_AND_UNUSED_TREE_CODE
) * sizeof (char *));
656 using_eh_for_cleanups ();
659 /* This doesn't do anything on purpose. It's used to satisfy the
660 print_error_function hook we don't print error messages with bogus
661 function prototypes. */
665 const char *s
__attribute__ ((__unused__
));
669 /* Called to install the PRINT_ERROR_FUNCTION hook differently
670 according to LEVEL. LEVEL is 1 during early parsing, when function
671 prototypes aren't fully resolved. print_error_function is set so it
672 doesn't print incomplete function prototypes. When LEVEL is 2,
673 function prototypes are fully resolved and can be printed when
676 void lang_init_source (level
)
680 print_error_function
= java_dummy_print
;
682 print_error_function
= lang_print_error
;
688 flag_new_exceptions
= 1;
689 flag_bounds_check
= 1;
698 /* Hooks for print_node. */
701 print_lang_decl (file
, node
, indent
)
702 FILE *file
__attribute ((__unused__
));
703 tree node
__attribute ((__unused__
));
704 int indent
__attribute ((__unused__
));
709 print_lang_type (file
, node
, indent
)
710 FILE *file
__attribute ((__unused__
));
711 tree node
__attribute ((__unused__
));
712 int indent
__attribute ((__unused__
));
717 print_lang_identifier (file
, node
, indent
)
718 FILE *file
__attribute ((__unused__
));
719 tree node
__attribute ((__unused__
));
720 int indent
__attribute ((__unused__
));
725 print_lang_statistics ()
729 /* used by print-tree.c */
732 lang_print_xnode (file
, node
, indent
)
733 FILE *file
__attribute ((__unused__
));
734 tree node
__attribute ((__unused__
));
735 int indent
__attribute ((__unused__
));
739 /* Return the typed-based alias set for T, which may be an expression
740 or a type. Return -1 if we don't do anything special. */
743 lang_get_alias_set (t
)
744 tree t ATTRIBUTE_UNUSED
;