* rtl.h (rtunion_def): Constify member `rtstr'.
[official-gcc.git] / gcc / java / lang.c
blob8992c57a55efc6d27d7169ba26c267390b780bcf
1 /* Java(TM) language-specific utility routines.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 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 PARAMS ((const char *, int));
40 static void put_decl_node PARAMS ((tree));
41 static void java_dummy_print PARAMS ((const char *));
42 static void lang_print_error PARAMS ((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 /* When non zero, generate code for the Boehm GC. */
114 int flag_use_boehm_gc = 0;
116 /* From gcc/flags.h, and indicates if exceptions are turned on or not. */
118 extern int flag_new_exceptions;
119 extern int flag_exceptions;
121 /* Table of language-dependent -f options.
122 STRING is the option name. VARIABLE is the address of the variable.
123 ON_VALUE is the value to store in VARIABLE
124 if `-fSTRING' is seen as an option.
125 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
127 static struct { const char *string; int *variable; int on_value;}
128 lang_f_options[] =
130 {"emit-class-file", &flag_emit_class_files, 1},
131 {"emit-class-files", &flag_emit_class_files, 1},
132 {"use-divide-subroutine", &flag_use_divide_subroutine, 1},
133 {"use-boehm-gc", &flag_use_boehm_gc, 1}
136 JCF *current_jcf;
138 /* Variable controlling how dependency tracking is enabled in
139 init_parse. */
140 static int dependency_tracking = 0;
142 /* Flag values for DEPENDENCY_TRACKING. */
143 #define DEPEND_SET_FILE 1
144 #define DEPEND_ENABLE 2
147 * process java-specific compiler command-line options
148 * return 0, but do not complain if the option is not recognised.
151 lang_decode_option (argc, argv)
152 int argc __attribute__ ((__unused__));
153 char **argv;
155 char *p = argv[0];
157 #define CLARG "-fassume-compiled="
158 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
160 add_assume_compiled (p + sizeof (CLARG) - 1, 0);
161 return 1;
163 #undef CLARG
164 #define CLARG "-fno-assume-compiled="
165 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
167 add_assume_compiled (p + sizeof (CLARG) - 1, 1);
168 return 1;
170 #undef CLARG
171 #define CLARG "-fassume-compiled"
172 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
174 add_assume_compiled ("", 0);
175 return 1;
177 #undef CLARG
178 #define CLARG "-fno-assume-compiled"
179 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
181 add_assume_compiled ("", 1);
182 return 1;
184 #undef CLARG
185 #define CLARG "-fclasspath="
186 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
188 jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
189 return 1;
191 #undef CLARG
192 #define CLARG "-fCLASSPATH="
193 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
195 jcf_path_CLASSPATH_arg (p + sizeof (CLARG) - 1);
196 return 1;
198 #undef CLARG
199 else if (strncmp (p, "-I", 2) == 0)
201 jcf_path_include_arg (p + 2);
202 return 1;
205 #define ARG "-foutput-class-dir="
206 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
208 jcf_write_base_directory = p + sizeof (ARG) - 1;
209 return 1;
211 #undef ARG
213 if (p[0] == '-' && p[1] == 'f')
215 /* Some kind of -f option.
216 P's value is the option sans `-f'.
217 Search for it in the table of options. */
218 int found = 0, j;
220 p += 2;
222 for (j = 0;
223 !found
224 && j < (int)(sizeof (lang_f_options) / sizeof (lang_f_options[0]));
225 j++)
227 if (!strcmp (p, lang_f_options[j].string))
229 *lang_f_options[j].variable = lang_f_options[j].on_value;
230 /* A goto here would be cleaner,
231 but breaks the vax pcc. */
232 found = 1;
234 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
235 && ! strcmp (p+3, lang_f_options[j].string))
237 *lang_f_options[j].variable = ! lang_f_options[j].on_value;
238 found = 1;
242 return found;
245 if (strcmp (p, "-Wall") == 0)
247 flag_wall = 1;
248 flag_redundant = 1;
249 return 1;
252 if (strcmp (p, "-Wunsupported-jdk11") == 0)
254 flag_static_local_jdk1_1 = 1;
255 return 1;
258 if (strcmp (p, "-Wredundant-modifiers") == 0)
260 flag_redundant = 1;
261 return 1;
264 if (strcmp (p, "-MD") == 0)
266 jcf_dependency_init (1);
267 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
268 return 1;
270 else if (strcmp (p, "-MMD") == 0)
272 jcf_dependency_init (0);
273 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
274 return 1;
276 else if (strcmp (p, "-M") == 0)
278 jcf_dependency_init (1);
279 dependency_tracking |= DEPEND_ENABLE;
280 return 1;
282 else if (strcmp (p, "-MM") == 0)
284 jcf_dependency_init (0);
285 dependency_tracking |= DEPEND_ENABLE;
286 return 1;
289 return 0;
292 FILE *finput;
293 char *
294 init_parse (filename)
295 char *filename;
297 /* Open input file. */
299 if (filename == 0 || !strcmp (filename, "-"))
301 finput = stdin;
302 filename = "stdin";
304 if (dependency_tracking)
305 error ("can't do dependency tracking with input from stdin");
307 else
309 if (dependency_tracking)
311 char *dot;
312 dot = strrchr (filename, '.');
313 if (dot == NULL)
314 error ("couldn't determine target name for dependency tracking");
315 else
317 char *buf = (char *) xmalloc (dot - filename +
318 3 + sizeof (OBJECT_SUFFIX));
319 strncpy (buf, filename, dot - filename);
321 /* If emitting class files, we might have multiple
322 targets. The class generation code takes care of
323 registering them. Otherwise we compute the target
324 name here. */
325 if (flag_emit_class_files)
326 jcf_dependency_set_target (NULL);
327 else
329 strcpy (buf + (dot - filename), OBJECT_SUFFIX);
330 jcf_dependency_set_target (buf);
333 if ((dependency_tracking & DEPEND_SET_FILE))
335 strcpy (buf + (dot - filename), ".d");
336 jcf_dependency_set_dep_file (buf);
338 else
339 jcf_dependency_set_dep_file ("-");
341 free (buf);
345 init_lex ();
347 return filename;
350 void
351 finish_parse ()
353 fclose (finput);
354 jcf_dependency_write ();
357 /* Buffer used by lang_printable_name. */
358 static char *decl_buf = NULL;
360 /* Allocated size of decl_buf. */
361 static int decl_buflen = 0;
363 /* Length of used part of decl_buf; position for next character. */
364 static int decl_bufpos = 0;
366 /* Append the string STR to decl_buf.
367 It length is given by LEN; -1 means the string is nul-terminated. */
369 static void
370 put_decl_string (str, len)
371 const char *str;
372 int len;
374 if (len < 0)
375 len = strlen (str);
376 if (decl_bufpos + len >= decl_buflen)
378 if (decl_buf == NULL)
380 decl_buflen = len + 100;
381 decl_buf = (char *) xmalloc (decl_buflen);
383 else
385 decl_buflen *= 2;
386 decl_buf = (char *) xrealloc (decl_buf, decl_buflen);
389 strcpy (decl_buf + decl_bufpos, str);
390 decl_bufpos += len;
393 /* Append to decl_buf a printable name for NODE. */
395 static void
396 put_decl_node (node)
397 tree node;
399 int was_pointer = 0;
400 if (TREE_CODE (node) == POINTER_TYPE)
402 node = TREE_TYPE (node);
403 was_pointer = 1;
405 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd'
406 && DECL_NAME (node) != NULL_TREE)
408 #if 0
409 if (DECL_CONTEXT (node) != NULL_TREE)
411 put_decl_node (DECL_CONTEXT (node));
412 put_decl_string (".", 1);
414 #endif
415 if (TREE_CODE (node) == FUNCTION_DECL
416 && DECL_NAME (node) == init_identifier_node
417 && !DECL_ARTIFICIAL (node) && current_class)
418 put_decl_node (TYPE_NAME (current_class));
419 else
420 put_decl_node (DECL_NAME (node));
421 if (TREE_CODE (node) == FUNCTION_DECL && TREE_TYPE (node) != NULL_TREE)
423 int i = 0;
424 tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
425 if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
426 args = TREE_CHAIN (args);
427 put_decl_string ("(", 1);
428 for ( ; args != end_params_node; args = TREE_CHAIN (args), i++)
430 if (i > 0)
431 put_decl_string (",", 1);
432 put_decl_node (TREE_VALUE (args));
434 put_decl_string (")", 1);
437 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't'
438 && TYPE_NAME (node) != NULL_TREE)
440 if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node))
442 put_decl_node (TYPE_ARRAY_ELEMENT (node));
443 put_decl_string("[]", 2);
445 else if (node == promoted_byte_type_node)
446 put_decl_string ("byte", 4);
447 else if (node == promoted_short_type_node)
448 put_decl_string ("short", 5);
449 else if (node == promoted_char_type_node)
450 put_decl_string ("char", 4);
451 else if (node == promoted_boolean_type_node)
452 put_decl_string ("boolean", 7);
453 else if (node == void_type_node && was_pointer)
454 put_decl_string ("null", 4);
455 else
456 put_decl_node (TYPE_NAME (node));
458 else if (TREE_CODE (node) == IDENTIFIER_NODE)
459 put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
460 else
461 put_decl_string ("<unknown>", -1);
464 /* Return a user-friendly name for DECL.
465 The resulting string is only valid until the next call.
466 The value of the hook decl_printable_name is this function,
467 which is also called directly by lang_print_error. */
469 const char *
470 lang_printable_name (decl, v)
471 tree decl;
472 int v __attribute__ ((__unused__));
474 decl_bufpos = 0;
475 put_decl_node (decl);
476 put_decl_string ("", 1);
477 return decl_buf;
480 /* Print on stderr the current class and method context. This function
481 is the value of the hook print_error_function, called from toplev.c. */
483 static void
484 lang_print_error (file)
485 const char *file;
487 static tree last_error_function_context = NULL_TREE;
488 static tree last_error_function = NULL;
490 if (current_function_decl != NULL
491 && DECL_CONTEXT (current_function_decl) != last_error_function_context)
493 if (file)
494 fprintf (stderr, "%s: ", file);
496 last_error_function_context = DECL_CONTEXT (current_function_decl);
497 fprintf (stderr, "In class `%s':\n",
498 lang_printable_name (last_error_function_context, 0));
500 if (last_error_function != current_function_decl)
502 if (file)
503 fprintf (stderr, "%s: ", file);
505 if (current_function_decl == NULL)
506 fprintf (stderr, "At top level:\n");
507 else
509 const char *name = lang_printable_name (current_function_decl, 2);
510 fprintf (stderr, "In method `%s':\n", name);
513 last_error_function = current_function_decl;
518 void
519 lang_init ()
521 #if 0
522 extern int flag_minimal_debug;
523 flag_minimal_debug = 0;
524 #endif
526 jcf_path_init ();
527 jcf_path_seal ();
529 decl_printable_name = lang_printable_name;
530 print_error_function = lang_print_error;
531 lang_expand_expr = java_lang_expand_expr;
533 flag_exceptions = 1;
535 /* Append to Gcc tree node definition arrays */
537 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
538 java_tree_code_type,
539 (int)LAST_JAVA_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
540 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
541 java_tree_code_length,
542 (LAST_JAVA_TREE_CODE -
543 (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
544 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
545 java_tree_code_name,
546 (LAST_JAVA_TREE_CODE -
547 (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
549 using_eh_for_cleanups ();
552 /* This doesn't do anything on purpose. It's used to satisfy the
553 print_error_function hook we don't print error messages with bogus
554 function prototypes. */
556 static void
557 java_dummy_print (s)
558 const char *s __attribute__ ((__unused__));
562 /* Called to install the PRINT_ERROR_FUNCTION hook differently
563 according to LEVEL. LEVEL is 1 during early parsing, when function
564 prototypes aren't fully resolved. print_error_function is set so it
565 doesn't print incomplete function prototypes. When LEVEL is 2,
566 function prototypes are fully resolved and can be printed when
567 reporting errors. */
569 void lang_init_source (level)
570 int level;
572 if (level == 1)
573 print_error_function = java_dummy_print;
574 else
575 print_error_function = lang_print_error;
578 void
579 lang_init_options ()
581 flag_new_exceptions = 1;
582 flag_bounds_check = 1;
585 void
586 lang_finish ()
590 const char *
591 lang_identify ()
593 return "Java";
596 /* Hooks for print_node. */
598 void
599 print_lang_decl (file, node, indent)
600 FILE *file __attribute ((__unused__));
601 tree node __attribute ((__unused__));
602 int indent __attribute ((__unused__));
606 void
607 print_lang_type (file, node, indent)
608 FILE *file __attribute ((__unused__));
609 tree node __attribute ((__unused__));
610 int indent __attribute ((__unused__));
614 void
615 print_lang_identifier (file, node, indent)
616 FILE *file __attribute ((__unused__));
617 tree node __attribute ((__unused__));
618 int indent __attribute ((__unused__));
622 void
623 print_lang_statistics ()
627 /* used by print-tree.c */
629 void
630 lang_print_xnode (file, node, indent)
631 FILE *file __attribute ((__unused__));
632 tree node __attribute ((__unused__));
633 int indent __attribute ((__unused__));