* tree.c (walk_tree): Don't recurse into DECL_INITIAL or DECL_SIZE
[official-gcc.git] / gcc / java / lang.c
blob11be76d6495d89226b3bc9f6502519f06b01d07b
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 {"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},
132 JCF *current_jcf;
134 /* Variable controlling how dependency tracking is enabled in
135 init_parse. */
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__));
149 char **argv;
151 char *p = argv[0];
153 #define CLARG "-fassume-compiled="
154 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
156 add_assume_compiled (p + sizeof (CLARG) - 1, 0);
157 return 1;
159 #undef CLARG
160 #define CLARG "-fno-assume-compiled="
161 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
163 add_assume_compiled (p + sizeof (CLARG) - 1, 1);
164 return 1;
166 #undef CLARG
167 #define CLARG "-fassume-compiled"
168 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
170 add_assume_compiled ("", 0);
171 return 1;
173 #undef CLARG
174 #define CLARG "-fno-assume-compiled"
175 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
177 add_assume_compiled ("", 1);
178 return 1;
180 #undef CLARG
181 #define CLARG "-fclasspath="
182 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
184 jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
185 return 1;
187 #undef CLARG
188 #define CLARG "-fCLASSPATH="
189 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
191 jcf_path_CLASSPATH_arg (p + sizeof (CLARG) - 1);
192 return 1;
194 #undef CLARG
195 else if (strncmp (p, "-I", 2) == 0)
197 jcf_path_include_arg (p + 2);
198 return 1;
201 #define ARG "-foutput-class-dir="
202 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
204 jcf_write_base_directory = p + sizeof (ARG) - 1;
205 return 1;
207 #undef ARG
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. */
214 int found = 0, j;
216 p += 2;
218 for (j = 0;
219 !found
220 && j < (int)(sizeof (lang_f_options) / sizeof (lang_f_options[0]));
221 j++)
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. */
228 found = 1;
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;
234 found = 1;
238 return found;
241 if (strcmp (p, "-Wall") == 0)
243 flag_wall = 1;
244 flag_redundant = 1;
245 return 1;
248 if (strcmp (p, "-Wunsupported-jdk11") == 0)
250 flag_static_local_jdk1_1 = 1;
251 return 1;
254 if (strcmp (p, "-Wredundant-modifiers") == 0)
256 flag_redundant = 1;
257 return 1;
260 if (strcmp (p, "-MD") == 0)
262 jcf_dependency_init (1);
263 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
264 return 1;
266 else if (strcmp (p, "-MMD") == 0)
268 jcf_dependency_init (0);
269 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
270 return 1;
272 else if (strcmp (p, "-M") == 0)
274 jcf_dependency_init (1);
275 dependency_tracking |= DEPEND_ENABLE;
276 return 1;
278 else if (strcmp (p, "-MM") == 0)
280 jcf_dependency_init (0);
281 dependency_tracking |= DEPEND_ENABLE;
282 return 1;
285 return 0;
288 FILE *finput;
289 char *
290 init_parse (filename)
291 char *filename;
293 /* Open input file. */
295 if (filename == 0 || !strcmp (filename, "-"))
297 finput = stdin;
298 filename = "stdin";
300 if (dependency_tracking)
301 error ("can't do dependency tracking with input from stdin");
303 else
305 if (dependency_tracking)
307 char *dot;
308 dot = strrchr (filename, '.');
309 if (dot == NULL)
310 error ("couldn't determine target name for dependency tracking");
311 else
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
320 name here. */
321 if (flag_emit_class_files)
322 jcf_dependency_set_target (NULL);
323 else
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);
334 else
335 jcf_dependency_set_dep_file ("-");
337 free (buf);
341 init_lex ();
343 return filename;
346 void
347 finish_parse ()
349 fclose (finput);
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. */
365 static void
366 put_decl_string (str, len)
367 const char *str;
368 int len;
370 if (len < 0)
371 len = strlen (str);
372 if (decl_bufpos + len >= decl_buflen)
374 if (decl_buf == NULL)
376 decl_buflen = len + 100;
377 decl_buf = (char *) xmalloc (decl_buflen);
379 else
381 decl_buflen *= 2;
382 decl_buf = (char *) xrealloc (decl_buf, decl_buflen);
385 strcpy (decl_buf + decl_bufpos, str);
386 decl_bufpos += len;
389 /* Append to decl_buf a printable name for NODE. */
391 static void
392 put_decl_node (node)
393 tree node;
395 int was_pointer = 0;
396 if (TREE_CODE (node) == POINTER_TYPE)
398 node = TREE_TYPE (node);
399 was_pointer = 1;
401 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd'
402 && DECL_NAME (node) != NULL_TREE)
404 #if 0
405 if (DECL_CONTEXT (node) != NULL_TREE)
407 put_decl_node (DECL_CONTEXT (node));
408 put_decl_string (".", 1);
410 #endif
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));
415 else
416 put_decl_node (DECL_NAME (node));
417 if (TREE_CODE (node) == FUNCTION_DECL && TREE_TYPE (node) != NULL_TREE)
419 int i = 0;
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++)
426 if (i > 0)
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);
451 else
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));
456 else
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. */
465 const char *
466 lang_printable_name (decl, v)
467 tree decl;
468 int v __attribute__ ((__unused__));
470 decl_bufpos = 0;
471 put_decl_node (decl);
472 put_decl_string ("", 1);
473 return decl_buf;
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. */
479 static void
480 lang_print_error (file)
481 const char *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)
489 if (file)
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)
498 if (file)
499 fprintf (stderr, "%s: ", file);
501 if (current_function_decl == NULL)
502 fprintf (stderr, "At top level:\n");
503 else
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;
514 void
515 lang_init ()
517 #if 0
518 extern int flag_minimal_debug;
519 flag_minimal_debug = 0;
520 #endif
522 jcf_path_init ();
523 jcf_path_seal ();
525 decl_printable_name = lang_printable_name;
526 print_error_function = lang_print_error;
527 lang_expand_expr = java_lang_expand_expr;
529 flag_exceptions = 1;
531 /* Append to Gcc tree node definition arrays */
533 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
534 java_tree_code_type,
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,
541 java_tree_code_name,
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. */
552 static void
553 java_dummy_print (s)
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
563 reporting errors. */
565 void lang_init_source (level)
566 int level;
568 if (level == 1)
569 print_error_function = java_dummy_print;
570 else
571 print_error_function = lang_print_error;
574 void
575 lang_init_options ()
577 flag_new_exceptions = 1;
578 flag_bounds_check = 1;
581 void
582 lang_finish ()
586 const char *
587 lang_identify ()
589 return "Java";
592 /* Hooks for print_node. */
594 void
595 print_lang_decl (file, node, indent)
596 FILE *file __attribute ((__unused__));
597 tree node __attribute ((__unused__));
598 int indent __attribute ((__unused__));
602 void
603 print_lang_type (file, node, indent)
604 FILE *file __attribute ((__unused__));
605 tree node __attribute ((__unused__));
606 int indent __attribute ((__unused__));
610 void
611 print_lang_identifier (file, node, indent)
612 FILE *file __attribute ((__unused__));
613 tree node __attribute ((__unused__));
614 int indent __attribute ((__unused__));
618 void
619 print_lang_statistics ()
623 /* used by print-tree.c */
625 void
626 lang_print_xnode (file, node, indent)
627 FILE *file __attribute ((__unused__));
628 tree node __attribute ((__unused__));
629 int indent __attribute ((__unused__));