* ggc-common.c (ggc_print_statistics): Make arguments to fprintf
[official-gcc.git] / gcc / java / lang.c
blob018f93d693a3e873d59e3326218f0ea63331a988
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)
9 any later version.
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. */
27 #include "config.h"
28 #include "system.h"
29 #include "tree.h"
30 #include "input.h"
31 #include "rtl.h"
32 #include "expr.h"
33 #include "java-tree.h"
34 #include "jcf.h"
35 #include "toplev.h"
36 #include "flags.h"
37 #include "xref.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 *));
44 #ifndef OBJECT_SUFFIX
45 # define OBJECT_SUFFIX ".o"
46 #endif
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[] = {
55 'x',
56 #include "java-tree.def"
58 #undef DEFTREECODE
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"
70 #undef DEFTREECODE
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[] = {
77 "@@dummy",
78 #include "java-tree.def"
80 #undef DEFTREECODE
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. */
99 int flag_wall = 0;
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;}
125 lang_f_options[] =
127 {"assume-compiled", &flag_assume_compiled, 1},
128 {"emit-class-file", &flag_emit_class_files, 1},
129 {"emit-class-files", &flag_emit_class_files, 1},
130 {"use-divide-subroutine", &flag_use_divide_subroutine, 1},
133 JCF *current_jcf;
135 /* Variable controlling how dependency tracking is enabled in
136 init_parse. */
137 static int dependency_tracking = 0;
139 /* Flag values for DEPENDENCY_TRACKING. */
140 #define DEPEND_SET_FILE 1
141 #define DEPEND_ENABLE 2
144 * process java-specific compiler command-line options
145 * return 0, but do not complain if the option is not recognised.
148 lang_decode_option (argc, argv)
149 int argc __attribute__ ((__unused__));
150 char **argv;
152 char *p = argv[0];
154 #define CLARG "-fclasspath="
155 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
157 jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
158 return 1;
160 #undef CLARG
161 #define CLARG "-fCLASSPATH="
162 else if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
164 jcf_path_CLASSPATH_arg (p + sizeof (CLARG) - 1);
165 return 1;
167 #undef CLARG
168 else if (strncmp (p, "-I", 2) == 0)
170 jcf_path_include_arg (p + 2);
171 return 1;
174 #define ARG "-foutput-class-dir="
175 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
177 jcf_write_base_directory = p + sizeof (ARG) - 1;
178 return 1;
180 #undef ARG
182 if (p[0] == '-' && p[1] == 'f')
184 /* Some kind of -f option.
185 P's value is the option sans `-f'.
186 Search for it in the table of options. */
187 int found = 0, j;
189 p += 2;
191 for (j = 0;
192 !found
193 && j < (int)(sizeof (lang_f_options) / sizeof (lang_f_options[0]));
194 j++)
196 if (!strcmp (p, lang_f_options[j].string))
198 *lang_f_options[j].variable = lang_f_options[j].on_value;
199 /* A goto here would be cleaner,
200 but breaks the vax pcc. */
201 found = 1;
203 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
204 && ! strcmp (p+3, lang_f_options[j].string))
206 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
207 found = 1;
211 return found;
214 if (strcmp (p, "-Wall") == 0)
216 flag_wall = 1;
217 flag_redundant = 1;
218 return 1;
221 if (strcmp (p, "-Wunsupported-jdk11") == 0)
223 flag_static_local_jdk1_1 = 1;
224 return 1;
227 if (strcmp (p, "-Wredundant-modifiers") == 0)
229 flag_redundant = 1;
230 return 1;
233 if (strcmp (p, "-MD") == 0)
235 jcf_dependency_init (1);
236 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
237 return 1;
239 else if (strcmp (p, "-MMD") == 0)
241 jcf_dependency_init (0);
242 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
243 return 1;
245 else if (strcmp (p, "-M") == 0)
247 jcf_dependency_init (1);
248 dependency_tracking |= DEPEND_ENABLE;
249 return 1;
251 else if (strcmp (p, "-MM") == 0)
253 jcf_dependency_init (0);
254 dependency_tracking |= DEPEND_ENABLE;
255 return 1;
258 return 0;
261 FILE *finput;
262 char *
263 init_parse (filename)
264 char *filename;
266 /* Open input file. */
268 if (filename == 0 || !strcmp (filename, "-"))
270 finput = stdin;
271 filename = "stdin";
273 if (dependency_tracking)
274 error ("can't do dependency tracking with input from stdin");
276 else
278 if (dependency_tracking)
280 char *dot;
281 dot = strrchr (filename, '.');
282 if (dot == NULL)
283 error ("couldn't determine target name for dependency tracking");
284 else
286 char *buf = (char *) xmalloc (dot - filename +
287 3 + sizeof (OBJECT_SUFFIX));
288 strncpy (buf, filename, dot - filename);
290 /* If emitting class files, we might have multiple
291 targets. The class generation code takes care of
292 registering them. Otherwise we compute the target
293 name here. */
294 if (flag_emit_class_files)
295 jcf_dependency_set_target (NULL);
296 else
298 strcpy (buf + (dot - filename), OBJECT_SUFFIX);
299 jcf_dependency_set_target (buf);
302 if ((dependency_tracking & DEPEND_SET_FILE))
304 strcpy (buf + (dot - filename), ".d");
305 jcf_dependency_set_dep_file (buf);
307 else
308 jcf_dependency_set_dep_file ("-");
310 free (buf);
314 init_lex ();
316 return filename;
319 void
320 finish_parse ()
322 fclose (finput);
323 jcf_dependency_write ();
326 /* Buffer used by lang_printable_name. */
327 static char *decl_buf = NULL;
329 /* Allocated size of decl_buf. */
330 static int decl_buflen = 0;
332 /* Length of used part of decl_buf; position for next character. */
333 static int decl_bufpos = 0;
335 /* Append the string STR to decl_buf.
336 It length is given by LEN; -1 means the string is nul-terminated. */
338 static void
339 put_decl_string (str, len)
340 const char *str;
341 int len;
343 if (len < 0)
344 len = strlen (str);
345 if (decl_bufpos + len >= decl_buflen)
347 if (decl_buf == NULL)
349 decl_buflen = len + 100;
350 decl_buf = (char *) xmalloc (decl_buflen);
352 else
354 decl_buflen *= 2;
355 decl_buf = (char *) xrealloc (decl_buf, decl_buflen);
358 strcpy (decl_buf + decl_bufpos, str);
359 decl_bufpos += len;
362 /* Append to decl_buf a printable name for NODE. */
364 static void
365 put_decl_node (node)
366 tree node;
368 int was_pointer = 0;
369 if (TREE_CODE (node) == POINTER_TYPE)
371 node = TREE_TYPE (node);
372 was_pointer = 1;
374 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd'
375 && DECL_NAME (node) != NULL_TREE)
377 #if 0
378 if (DECL_CONTEXT (node) != NULL_TREE)
380 put_decl_node (DECL_CONTEXT (node));
381 put_decl_string (".", 1);
383 #endif
384 if (TREE_CODE (node) == FUNCTION_DECL
385 && DECL_NAME (node) == init_identifier_node
386 && !DECL_ARTIFICIAL (node) && current_class)
387 put_decl_node (TYPE_NAME (current_class));
388 else
389 put_decl_node (DECL_NAME (node));
390 if (TREE_CODE (node) == FUNCTION_DECL && TREE_TYPE (node) != NULL_TREE)
392 int i = 0;
393 tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
394 if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
395 args = TREE_CHAIN (args);
396 put_decl_string ("(", 1);
397 for ( ; args != end_params_node; args = TREE_CHAIN (args), i++)
399 if (i > 0)
400 put_decl_string (",", 1);
401 put_decl_node (TREE_VALUE (args));
403 put_decl_string (")", 1);
406 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't'
407 && TYPE_NAME (node) != NULL_TREE)
409 if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node))
411 put_decl_node (TYPE_ARRAY_ELEMENT (node));
412 put_decl_string("[]", 2);
414 else if (node == promoted_byte_type_node)
415 put_decl_string ("byte", 4);
416 else if (node == promoted_short_type_node)
417 put_decl_string ("short", 5);
418 else if (node == promoted_char_type_node)
419 put_decl_string ("char", 4);
420 else if (node == promoted_boolean_type_node)
421 put_decl_string ("boolean", 7);
422 else if (node == void_type_node && was_pointer)
423 put_decl_string ("null", 4);
424 else
425 put_decl_node (TYPE_NAME (node));
427 else if (TREE_CODE (node) == IDENTIFIER_NODE)
428 put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
429 else
430 put_decl_string ("<unknown>", -1);
433 /* Return a user-friendly name for DECL.
434 The resulting string is only valid until the next call.
435 The value of the hook decl_printable_name is this function,
436 which is also called directly by lang_print_error. */
438 const char *
439 lang_printable_name (decl, v)
440 tree decl;
441 int v __attribute__ ((__unused__));
443 decl_bufpos = 0;
444 put_decl_node (decl);
445 put_decl_string ("", 1);
446 return decl_buf;
449 /* Print on stderr the current class and method context. This function
450 is the value of the hook print_error_function, called from toplev.c. */
452 static void
453 lang_print_error (file)
454 const char *file;
456 static tree last_error_function_context = NULL_TREE;
457 static tree last_error_function = NULL;
459 if (current_function_decl != NULL
460 && DECL_CONTEXT (current_function_decl) != last_error_function_context)
462 if (file)
463 fprintf (stderr, "%s: ", file);
465 last_error_function_context = DECL_CONTEXT (current_function_decl);
466 fprintf (stderr, "In class `%s':\n",
467 lang_printable_name (last_error_function_context, 0));
469 if (last_error_function != current_function_decl)
471 if (file)
472 fprintf (stderr, "%s: ", file);
474 if (current_function_decl == NULL)
475 fprintf (stderr, "At top level:\n");
476 else
478 const char *name = lang_printable_name (current_function_decl, 2);
479 fprintf (stderr, "In method `%s':\n", name);
482 last_error_function = current_function_decl;
487 void
488 lang_init ()
490 #if 0
491 extern int flag_minimal_debug;
492 flag_minimal_debug = 0;
493 #endif
495 jcf_path_init ();
496 jcf_path_seal ();
498 decl_printable_name = lang_printable_name;
499 print_error_function = lang_print_error;
500 lang_expand_expr = java_lang_expand_expr;
502 flag_exceptions = 1;
504 /* Append to Gcc tree node definition arrays */
506 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
507 java_tree_code_type,
508 (int)LAST_JAVA_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
509 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
510 java_tree_code_length,
511 (LAST_JAVA_TREE_CODE -
512 (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
513 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
514 java_tree_code_name,
515 (LAST_JAVA_TREE_CODE -
516 (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
518 using_eh_for_cleanups ();
521 /* This doesn't do anything on purpose. It's used to satisfy the
522 print_error_function hook we don't print error messages with bogus
523 function prototypes. */
525 static void
526 java_dummy_print (s)
527 const char *s __attribute__ ((__unused__));
531 /* Called to install the PRINT_ERROR_FUNCTION hook differently
532 according to LEVEL. LEVEL is 1 during early parsing, when function
533 prototypes aren't fully resolved. print_error_function is set so it
534 doesn't print incomplete function prototypes. When LEVEL is 2,
535 function prototypes are fully resolved and can be printed when
536 reporting errors. */
538 void lang_init_source (level)
539 int level;
541 if (level == 1)
542 print_error_function = java_dummy_print;
543 else
544 print_error_function = lang_print_error;
547 void
548 lang_init_options ()
550 flag_new_exceptions = 1;
551 flag_bounds_check = 1;
554 void
555 lang_finish ()
559 const char *
560 lang_identify ()
562 return "Java";
565 /* Hooks for print_node. */
567 void
568 print_lang_decl (file, node, indent)
569 FILE *file __attribute ((__unused__));
570 tree node __attribute ((__unused__));
571 int indent __attribute ((__unused__));
575 void
576 print_lang_type (file, node, indent)
577 FILE *file __attribute ((__unused__));
578 tree node __attribute ((__unused__));
579 int indent __attribute ((__unused__));
583 void
584 print_lang_identifier (file, node, indent)
585 FILE *file __attribute ((__unused__));
586 tree node __attribute ((__unused__));
587 int indent __attribute ((__unused__));
591 void
592 print_lang_statistics ()
596 /* used by print-tree.c */
598 void
599 lang_print_xnode (file, node, indent)
600 FILE *file __attribute ((__unused__));
601 tree node __attribute ((__unused__));
602 int indent __attribute ((__unused__));