1 /* Java(TM) language-specific utility routines.
2 Copyright (C) 1996, 97-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
33 #include "java-tree.h"
39 static void put_decl_string
PROTO ((const char *, int));
40 static void put_decl_node
PROTO ((tree
));
41 static void java_dummy_print
PROTO ((const char *));
42 static void lang_print_error
PROTO ((const char *));
45 # define OBJECT_SUFFIX ".o"
48 /* Table indexed by tree code giving a string containing a character
49 classifying the tree code. Possibilities are
50 t, d, s, c, r, <, 1 and 2. See java/java-tree.def for details. */
52 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
54 char java_tree_code_type
[] = {
56 #include "java-tree.def"
60 /* Table indexed by tree code giving number of expression
61 operands beyond the fixed part of the node structure.
62 Not used for types or decls. */
64 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
66 int java_tree_code_length
[] = {
68 #include "java-tree.def"
72 /* Names of tree components.
73 Used for printing out the tree and error messages. */
74 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
76 const char *java_tree_code_name
[] = {
78 #include "java-tree.def"
82 int compiling_from_source
;
84 const char * const language_string
= "GNU Java";
86 /* Nonzero if we should make is_compiled_class always return 1 for
87 appropriate classes that we're referencing. */
89 int flag_assume_compiled
= 1;
91 int flag_emit_class_files
= 0;
93 /* When non zero, we emit xref strings. Values of the flag for xref
94 backends are defined in xref_flag_table, xref.c. */
96 int flag_emit_xref
= 0;
98 /* When non zero, -Wall was turned on. */
101 /* When non zero, check for redundant modifier uses. */
102 int flag_redundant
= 0;
104 /* When non zero, warns about overridings that don't occur. */
105 int flag_not_overriding
= 0;
107 /* When non zero, warns that final local are treated as non final. */
108 int flag_static_local_jdk1_1
= 0;
110 /* When non zero, call a library routine to do integer divisions. */
111 int flag_use_divide_subroutine
= 1;
113 /* From gcc/flags.h, and indicates if exceptions are turned on or not. */
115 extern int flag_new_exceptions
;
116 extern int flag_exceptions
;
118 /* Table of language-dependent -f options.
119 STRING is the option name. VARIABLE is the address of the variable.
120 ON_VALUE is the value to store in VARIABLE
121 if `-fSTRING' is seen as an option.
122 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
124 static struct { const char *string
; int *variable
; int on_value
;}
127 {"emit-class-file", &flag_emit_class_files
, 1},
128 {"emit-class-files", &flag_emit_class_files
, 1},
129 {"use-divide-subroutine", &flag_use_divide_subroutine
, 1},
134 /* Variable controlling how dependency tracking is enabled in
136 static int dependency_tracking
= 0;
138 /* Flag values for DEPENDENCY_TRACKING. */
139 #define DEPEND_SET_FILE 1
140 #define DEPEND_ENABLE 2
143 * process java-specific compiler command-line options
144 * return 0, but do not complain if the option is not recognised.
147 lang_decode_option (argc
, argv
)
148 int argc
__attribute__ ((__unused__
));
153 #define CLARG "-fassume-compiled="
154 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
156 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 0);
160 #define CLARG "-fno-assume-compiled="
161 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
163 add_assume_compiled (p
+ sizeof (CLARG
) - 1, 1);
167 #define CLARG "-fassume-compiled"
168 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
170 add_assume_compiled ("", 0);
174 #define CLARG "-fno-assume-compiled"
175 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
177 add_assume_compiled ("", 1);
181 #define CLARG "-fclasspath="
182 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
184 jcf_path_classpath_arg (p
+ sizeof (CLARG
) - 1);
188 #define CLARG "-fCLASSPATH="
189 if (strncmp (p
, CLARG
, sizeof (CLARG
) - 1) == 0)
191 jcf_path_CLASSPATH_arg (p
+ sizeof (CLARG
) - 1);
195 else if (strncmp (p
, "-I", 2) == 0)
197 jcf_path_include_arg (p
+ 2);
201 #define ARG "-foutput-class-dir="
202 if (strncmp (p
, ARG
, sizeof (ARG
) - 1) == 0)
204 jcf_write_base_directory
= p
+ sizeof (ARG
) - 1;
209 if (p
[0] == '-' && p
[1] == 'f')
211 /* Some kind of -f option.
212 P's value is the option sans `-f'.
213 Search for it in the table of options. */
220 && j
< (int)(sizeof (lang_f_options
) / sizeof (lang_f_options
[0]));
223 if (!strcmp (p
, lang_f_options
[j
].string
))
225 *lang_f_options
[j
].variable
= lang_f_options
[j
].on_value
;
226 /* A goto here would be cleaner,
227 but breaks the vax pcc. */
230 if (p
[0] == 'n' && p
[1] == 'o' && p
[2] == '-'
231 && ! strcmp (p
+3, lang_f_options
[j
].string
))
233 *lang_f_options
[j
].variable
= ! lang_f_options
[j
].on_value
;
241 if (strcmp (p
, "-Wall") == 0)
248 if (strcmp (p
, "-Wunsupported-jdk11") == 0)
250 flag_static_local_jdk1_1
= 1;
254 if (strcmp (p
, "-Wredundant-modifiers") == 0)
260 if (strcmp (p
, "-MD") == 0)
262 jcf_dependency_init (1);
263 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
266 else if (strcmp (p
, "-MMD") == 0)
268 jcf_dependency_init (0);
269 dependency_tracking
|= DEPEND_SET_FILE
| DEPEND_ENABLE
;
272 else if (strcmp (p
, "-M") == 0)
274 jcf_dependency_init (1);
275 dependency_tracking
|= DEPEND_ENABLE
;
278 else if (strcmp (p
, "-MM") == 0)
280 jcf_dependency_init (0);
281 dependency_tracking
|= DEPEND_ENABLE
;
290 init_parse (filename
)
293 /* Open input file. */
295 if (filename
== 0 || !strcmp (filename
, "-"))
300 if (dependency_tracking
)
301 error ("can't do dependency tracking with input from stdin");
305 if (dependency_tracking
)
308 dot
= strrchr (filename
, '.');
310 error ("couldn't determine target name for dependency tracking");
313 char *buf
= (char *) xmalloc (dot
- filename
+
314 3 + sizeof (OBJECT_SUFFIX
));
315 strncpy (buf
, filename
, dot
- filename
);
317 /* If emitting class files, we might have multiple
318 targets. The class generation code takes care of
319 registering them. Otherwise we compute the target
321 if (flag_emit_class_files
)
322 jcf_dependency_set_target (NULL
);
325 strcpy (buf
+ (dot
- filename
), OBJECT_SUFFIX
);
326 jcf_dependency_set_target (buf
);
329 if ((dependency_tracking
& DEPEND_SET_FILE
))
331 strcpy (buf
+ (dot
- filename
), ".d");
332 jcf_dependency_set_dep_file (buf
);
335 jcf_dependency_set_dep_file ("-");
350 jcf_dependency_write ();
353 /* Buffer used by lang_printable_name. */
354 static char *decl_buf
= NULL
;
356 /* Allocated size of decl_buf. */
357 static int decl_buflen
= 0;
359 /* Length of used part of decl_buf; position for next character. */
360 static int decl_bufpos
= 0;
362 /* Append the string STR to decl_buf.
363 It length is given by LEN; -1 means the string is nul-terminated. */
366 put_decl_string (str
, len
)
372 if (decl_bufpos
+ len
>= decl_buflen
)
374 if (decl_buf
== NULL
)
376 decl_buflen
= len
+ 100;
377 decl_buf
= (char *) xmalloc (decl_buflen
);
382 decl_buf
= (char *) xrealloc (decl_buf
, decl_buflen
);
385 strcpy (decl_buf
+ decl_bufpos
, str
);
389 /* Append to decl_buf a printable name for NODE. */
396 if (TREE_CODE (node
) == POINTER_TYPE
)
398 node
= TREE_TYPE (node
);
401 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd'
402 && DECL_NAME (node
) != NULL_TREE
)
405 if (DECL_CONTEXT (node
) != NULL_TREE
)
407 put_decl_node (DECL_CONTEXT (node
));
408 put_decl_string (".", 1);
411 if (TREE_CODE (node
) == FUNCTION_DECL
412 && DECL_NAME (node
) == init_identifier_node
413 && !DECL_ARTIFICIAL (node
) && current_class
)
414 put_decl_node (TYPE_NAME (current_class
));
416 put_decl_node (DECL_NAME (node
));
417 if (TREE_CODE (node
) == FUNCTION_DECL
&& TREE_TYPE (node
) != NULL_TREE
)
420 tree args
= TYPE_ARG_TYPES (TREE_TYPE (node
));
421 if (TREE_CODE (TREE_TYPE (node
)) == METHOD_TYPE
)
422 args
= TREE_CHAIN (args
);
423 put_decl_string ("(", 1);
424 for ( ; args
!= end_params_node
; args
= TREE_CHAIN (args
), i
++)
427 put_decl_string (",", 1);
428 put_decl_node (TREE_VALUE (args
));
430 put_decl_string (")", 1);
433 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't'
434 && TYPE_NAME (node
) != NULL_TREE
)
436 if (TREE_CODE (node
) == RECORD_TYPE
&& TYPE_ARRAY_P (node
))
438 put_decl_node (TYPE_ARRAY_ELEMENT (node
));
439 put_decl_string("[]", 2);
441 else if (node
== promoted_byte_type_node
)
442 put_decl_string ("byte", 4);
443 else if (node
== promoted_short_type_node
)
444 put_decl_string ("short", 5);
445 else if (node
== promoted_char_type_node
)
446 put_decl_string ("char", 4);
447 else if (node
== promoted_boolean_type_node
)
448 put_decl_string ("boolean", 7);
449 else if (node
== void_type_node
&& was_pointer
)
450 put_decl_string ("null", 4);
452 put_decl_node (TYPE_NAME (node
));
454 else if (TREE_CODE (node
) == IDENTIFIER_NODE
)
455 put_decl_string (IDENTIFIER_POINTER (node
), IDENTIFIER_LENGTH (node
));
457 put_decl_string ("<unknown>", -1);
460 /* Return a user-friendly name for DECL.
461 The resulting string is only valid until the next call.
462 The value of the hook decl_printable_name is this function,
463 which is also called directly by lang_print_error. */
466 lang_printable_name (decl
, v
)
468 int v
__attribute__ ((__unused__
));
471 put_decl_node (decl
);
472 put_decl_string ("", 1);
476 /* Print on stderr the current class and method context. This function
477 is the value of the hook print_error_function, called from toplev.c. */
480 lang_print_error (file
)
483 static tree last_error_function_context
= NULL_TREE
;
484 static tree last_error_function
= NULL
;
486 if (current_function_decl
!= NULL
487 && DECL_CONTEXT (current_function_decl
) != last_error_function_context
)
490 fprintf (stderr
, "%s: ", file
);
492 last_error_function_context
= DECL_CONTEXT (current_function_decl
);
493 fprintf (stderr
, "In class `%s':\n",
494 lang_printable_name (last_error_function_context
, 0));
496 if (last_error_function
!= current_function_decl
)
499 fprintf (stderr
, "%s: ", file
);
501 if (current_function_decl
== NULL
)
502 fprintf (stderr
, "At top level:\n");
505 const char *name
= lang_printable_name (current_function_decl
, 2);
506 fprintf (stderr
, "In method `%s':\n", name
);
509 last_error_function
= current_function_decl
;
518 extern int flag_minimal_debug
;
519 flag_minimal_debug
= 0;
525 decl_printable_name
= lang_printable_name
;
526 print_error_function
= lang_print_error
;
527 lang_expand_expr
= java_lang_expand_expr
;
531 /* Append to Gcc tree node definition arrays */
533 memcpy (tree_code_type
+ (int) LAST_AND_UNUSED_TREE_CODE
,
535 (int)LAST_JAVA_TREE_CODE
- (int)LAST_AND_UNUSED_TREE_CODE
);
536 memcpy (tree_code_length
+ (int) LAST_AND_UNUSED_TREE_CODE
,
537 java_tree_code_length
,
538 (LAST_JAVA_TREE_CODE
-
539 (int)LAST_AND_UNUSED_TREE_CODE
) * sizeof (int));
540 memcpy (tree_code_name
+ (int) LAST_AND_UNUSED_TREE_CODE
,
542 (LAST_JAVA_TREE_CODE
-
543 (int)LAST_AND_UNUSED_TREE_CODE
) * sizeof (char *));
545 using_eh_for_cleanups ();
548 /* This doesn't do anything on purpose. It's used to satisfy the
549 print_error_function hook we don't print error messages with bogus
550 function prototypes. */
554 const char *s
__attribute__ ((__unused__
));
558 /* Called to install the PRINT_ERROR_FUNCTION hook differently
559 according to LEVEL. LEVEL is 1 during early parsing, when function
560 prototypes aren't fully resolved. print_error_function is set so it
561 doesn't print incomplete function prototypes. When LEVEL is 2,
562 function prototypes are fully resolved and can be printed when
565 void lang_init_source (level
)
569 print_error_function
= java_dummy_print
;
571 print_error_function
= lang_print_error
;
577 flag_new_exceptions
= 1;
578 flag_bounds_check
= 1;
592 /* Hooks for print_node. */
595 print_lang_decl (file
, node
, indent
)
596 FILE *file
__attribute ((__unused__
));
597 tree node
__attribute ((__unused__
));
598 int indent
__attribute ((__unused__
));
603 print_lang_type (file
, node
, indent
)
604 FILE *file
__attribute ((__unused__
));
605 tree node
__attribute ((__unused__
));
606 int indent
__attribute ((__unused__
));
611 print_lang_identifier (file
, node
, indent
)
612 FILE *file
__attribute ((__unused__
));
613 tree node
__attribute ((__unused__
));
614 int indent
__attribute ((__unused__
));
619 print_lang_statistics ()
623 /* used by print-tree.c */
626 lang_print_xnode (file
, node
, indent
)
627 FILE *file
__attribute ((__unused__
));
628 tree node
__attribute ((__unused__
));
629 int indent
__attribute ((__unused__
));