2001-08-08 Alexandre Petit-Bianco <apbianco@redhat.com>
[official-gcc.git] / gcc / java / lang.c
blobb9cb0fea7b6f64e609eb50c9a6c3cec681773ec2
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)
10 any later version.
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. */
28 #include "config.h"
29 #include "system.h"
30 #include "tree.h"
31 #include "input.h"
32 #include "rtl.h"
33 #include "expr.h"
34 #include "java-tree.h"
35 #include "jcf.h"
36 #include "toplev.h"
37 #include "flags.h"
38 #include "xref.h"
39 #include "ggc.h"
40 #include "diagnostic.h"
42 struct string_option
44 const char *string;
45 int *variable;
46 int on_value;
49 static void java_init PARAMS ((void));
50 static void java_init_options PARAMS ((void));
51 static int java_decode_option PARAMS ((int, char **));
52 static void put_decl_string PARAMS ((const char *, int));
53 static void put_decl_node PARAMS ((tree));
54 static void java_dummy_print PARAMS ((diagnostic_context *, const char *));
55 static void lang_print_error PARAMS ((diagnostic_context *, const char *));
56 static int process_option_with_no PARAMS ((char *,
57 struct string_option *,
58 int));
60 #ifndef TARGET_OBJECT_SUFFIX
61 # define TARGET_OBJECT_SUFFIX ".o"
62 #endif
64 /* Table indexed by tree code giving a string containing a character
65 classifying the tree code. Possibilities are
66 t, d, s, c, r, <, 1 and 2. See java/java-tree.def for details. */
68 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
70 char java_tree_code_type[] = {
71 'x',
72 #include "java-tree.def"
74 #undef DEFTREECODE
76 /* Table indexed by tree code giving number of expression
77 operands beyond the fixed part of the node structure.
78 Not used for types or decls. */
80 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
82 int java_tree_code_length[] = {
84 #include "java-tree.def"
86 #undef DEFTREECODE
88 /* Names of tree components.
89 Used for printing out the tree and error messages. */
90 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
92 const char *java_tree_code_name[] = {
93 "@@dummy",
94 #include "java-tree.def"
96 #undef DEFTREECODE
98 int compiling_from_source;
100 const char * const language_string = "GNU Java";
102 int flag_emit_class_files = 0;
104 /* Nonzero if input file is a file with a list of filenames to compile. */
106 int flag_filelist_file = 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. */
114 int flag_wall = 0;
116 /* When non zero, check for redundant modifier uses. */
117 int flag_redundant = 0;
119 /* When non zero, call a library routine to do integer divisions. */
120 int flag_use_divide_subroutine = 1;
122 /* When non zero, generate code for the Boehm GC. */
123 int flag_use_boehm_gc = 0;
125 /* When non zero, assume the runtime uses a hash table to map an
126 object to its synchronization structure. */
127 int flag_hash_synchronization;
129 /* When non zero, assume all native functions are implemented with
130 JNI, not CNI. */
131 int flag_jni = 0;
133 /* When non zero, warn when source file is newer than matching class
134 file. */
135 int flag_newer = 1;
137 /* When non zero, generate checks for references to NULL. */
138 int flag_check_references = 0;
140 /* The encoding of the source file. */
141 const char *current_encoding = NULL;
143 /* When non zero, report the now deprecated empty statements. */
144 int flag_extraneous_semicolon;
146 /* When non zero, always check for a non gcj generated classes archive. */
147 int flag_force_classes_archive_check;
149 /* When zero, don't optimize static class initialization. This flag shouldn't
150 be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead. */
151 int flag_optimize_sci = 1;
153 /* Table of language-dependent -f options.
154 STRING is the option name. VARIABLE is the address of the variable.
155 ON_VALUE is the value to store in VARIABLE
156 if `-fSTRING' is seen as an option.
157 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
159 static struct string_option
160 lang_f_options[] =
162 {"emit-class-file", &flag_emit_class_files, 1},
163 {"emit-class-files", &flag_emit_class_files, 1},
164 {"filelist-file", &flag_filelist_file, 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},
169 {"check-references", &flag_check_references, 1},
170 {"force-classes-archive-check", &flag_force_classes_archive_check, 1}
173 static struct string_option
174 lang_W_options[] =
176 { "redundant-modifiers", &flag_redundant, 1 },
177 { "extraneous-semicolon", &flag_extraneous_semicolon, 1 },
178 { "out-of-date", &flag_newer, 1 }
181 JCF *current_jcf;
183 /* Variable controlling how dependency tracking is enabled in
184 init_parse. */
185 static int dependency_tracking = 0;
187 /* Flag values for DEPENDENCY_TRACKING. */
188 #define DEPEND_SET_FILE 1
189 #define DEPEND_ENABLE 2
190 #define DEPEND_TARGET_SET 4
191 #define DEPEND_FILE_ALREADY_SET 8
193 /* Each front end provides its own. */
194 struct lang_hooks lang_hooks = {java_init,
195 NULL, /* java_finish */
196 java_init_options,
197 java_decode_option,
198 NULL /* post_options */};
200 /* Process an option that can accept a `no-' form.
201 Return 1 if option found, 0 otherwise. */
202 static int
203 process_option_with_no (p, table, table_size)
204 char *p;
205 struct string_option *table;
206 int table_size;
208 int j;
210 for (j = 0; j < table_size; j++)
212 if (!strcmp (p, table[j].string))
214 *table[j].variable = table[j].on_value;
215 return 1;
217 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
218 && ! strcmp (p+3, table[j].string))
220 *table[j].variable = ! table[j].on_value;
221 return 1;
225 return 0;
229 * process java-specific compiler command-line options
230 * return 0, but do not complain if the option is not recognised.
232 static int
233 java_decode_option (argc, argv)
234 int argc __attribute__ ((__unused__));
235 char **argv;
237 char *p = argv[0];
239 #define CLARG "-fassume-compiled="
240 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
242 add_assume_compiled (p + sizeof (CLARG) - 1, 0);
243 return 1;
245 #undef CLARG
246 #define CLARG "-fno-assume-compiled="
247 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
249 add_assume_compiled (p + sizeof (CLARG) - 1, 1);
250 return 1;
252 #undef CLARG
253 #define CLARG "-fassume-compiled"
254 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
256 add_assume_compiled ("", 0);
257 return 1;
259 #undef CLARG
260 #define CLARG "-fno-assume-compiled"
261 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
263 add_assume_compiled ("", 1);
264 return 1;
266 #undef CLARG
267 #define CLARG "-fclasspath="
268 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
270 jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
271 return 1;
273 #undef CLARG
274 #define CLARG "-fCLASSPATH="
275 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
277 jcf_path_CLASSPATH_arg (p + sizeof (CLARG) - 1);
278 return 1;
280 #undef CLARG
281 else if (strncmp (p, "-I", 2) == 0)
283 jcf_path_include_arg (p + 2);
284 return 1;
287 #define ARG "-foutput-class-dir="
288 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
290 jcf_write_base_directory = p + sizeof (ARG) - 1;
291 return 1;
293 #undef ARG
294 #define ARG "-fencoding="
295 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
297 current_encoding = p + sizeof (ARG) - 1;
298 return 1;
300 #undef ARG
302 #undef ARG
303 #define ARG "-fno-optimize-static-class-initialization"
304 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
306 flag_optimize_sci = 0;
307 return 1;
309 #undef ARG
311 if (p[0] == '-' && p[1] == 'f')
313 /* Some kind of -f option.
314 P's value is the option sans `-f'.
315 Search for it in the table of options. */
316 p += 2;
317 return process_option_with_no (p, lang_f_options,
318 ARRAY_SIZE (lang_f_options));
321 if (strcmp (p, "-Wall") == 0)
323 flag_wall = 1;
324 flag_redundant = 1;
325 flag_extraneous_semicolon = 1;
326 /* When -Wall given, enable -Wunused. We do this because the C
327 compiler does it, and people expect it. */
328 set_Wunused (1);
329 return 1;
332 if (p[0] == '-' && p[1] == 'W')
334 /* Skip `-W' and see if we accept the option or its `no-' form. */
335 p += 2;
336 return process_option_with_no (p, lang_W_options,
337 ARRAY_SIZE (lang_W_options));
340 if (strcmp (p, "-MD") == 0)
342 jcf_dependency_init (1);
343 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
344 return 1;
346 else if (strcmp (p, "-MMD") == 0)
348 jcf_dependency_init (0);
349 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
350 return 1;
352 else if (strcmp (p, "-M") == 0)
354 jcf_dependency_init (1);
355 dependency_tracking |= DEPEND_ENABLE;
356 return 1;
358 else if (strcmp (p, "-MM") == 0)
360 jcf_dependency_init (0);
361 dependency_tracking |= DEPEND_ENABLE;
362 return 1;
364 else if (strcmp (p, "-MP") == 0)
366 jcf_dependency_print_dummies ();
367 return 1;
369 else if (strcmp (p, "-MT") == 0)
371 jcf_dependency_set_target (argv[1]);
372 dependency_tracking |= DEPEND_TARGET_SET;
373 return 2;
375 else if (strcmp (p, "-MF") == 0)
377 jcf_dependency_set_dep_file (argv[1]);
378 dependency_tracking |= DEPEND_FILE_ALREADY_SET;
379 return 2;
382 return 0;
385 /* Global open file. */
386 FILE *finput;
388 const char *
389 init_parse (filename)
390 const char *filename;
392 /* Open input file. */
394 if (filename == 0 || !strcmp (filename, "-"))
396 finput = stdin;
397 filename = "stdin";
399 if (dependency_tracking)
400 error ("can't do dependency tracking with input from stdin");
402 else
404 if (dependency_tracking)
406 char *dot;
408 /* If the target is set and the output filename is set, then
409 there's no processing to do here. Otherwise we must
410 compute one or the other. */
411 if (! ((dependency_tracking & DEPEND_TARGET_SET)
412 && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
414 dot = strrchr (filename, '.');
415 if (dot == NULL)
416 error ("couldn't determine target name for dependency tracking");
417 else
419 char *buf = (char *) xmalloc (dot - filename +
420 3 + sizeof (TARGET_OBJECT_SUFFIX));
421 strncpy (buf, filename, dot - filename);
423 /* If emitting class files, we might have multiple
424 targets. The class generation code takes care of
425 registering them. Otherwise we compute the
426 target name here. */
427 if ((dependency_tracking & DEPEND_TARGET_SET))
428 ; /* Nothing. */
429 else if (flag_emit_class_files)
430 jcf_dependency_set_target (NULL);
431 else
433 strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
434 jcf_dependency_set_target (buf);
437 if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
438 ; /* Nothing. */
439 else if ((dependency_tracking & DEPEND_SET_FILE))
441 strcpy (buf + (dot - filename), ".d");
442 jcf_dependency_set_dep_file (buf);
444 else
445 jcf_dependency_set_dep_file ("-");
447 free (buf);
453 init_lex ();
455 return filename;
458 void
459 finish_parse ()
461 jcf_dependency_write ();
464 /* Buffer used by lang_printable_name. */
465 static char *decl_buf = NULL;
467 /* Allocated size of decl_buf. */
468 static int decl_buflen = 0;
470 /* Length of used part of decl_buf; position for next character. */
471 static int decl_bufpos = 0;
473 /* Append the string STR to decl_buf.
474 It length is given by LEN; -1 means the string is nul-terminated. */
476 static void
477 put_decl_string (str, len)
478 const char *str;
479 int len;
481 if (len < 0)
482 len = strlen (str);
483 if (decl_bufpos + len >= decl_buflen)
485 if (decl_buf == NULL)
487 decl_buflen = len + 100;
488 decl_buf = (char *) xmalloc (decl_buflen);
490 else
492 decl_buflen *= 2;
493 decl_buf = (char *) xrealloc (decl_buf, decl_buflen);
496 strcpy (decl_buf + decl_bufpos, str);
497 decl_bufpos += len;
500 /* Append to decl_buf a printable name for NODE. */
502 static void
503 put_decl_node (node)
504 tree node;
506 int was_pointer = 0;
507 if (TREE_CODE (node) == POINTER_TYPE)
509 node = TREE_TYPE (node);
510 was_pointer = 1;
512 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd'
513 && DECL_NAME (node) != NULL_TREE)
515 /* We want to print the type the DECL belongs to. We don't do
516 that when we handle constructors. */
517 if (TREE_CODE (node) == FUNCTION_DECL
518 && ! DECL_CONSTRUCTOR_P (node)
519 && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node))
521 put_decl_node (TYPE_NAME (DECL_CONTEXT (node)));
522 put_decl_string (".", 1);
524 if (! DECL_CONSTRUCTOR_P (node))
525 put_decl_node (DECL_NAME (node));
526 if (TREE_CODE (node) == FUNCTION_DECL && TREE_TYPE (node) != NULL_TREE)
528 int i = 0;
529 tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
530 if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
531 args = TREE_CHAIN (args);
532 put_decl_string ("(", 1);
533 for ( ; args != end_params_node; args = TREE_CHAIN (args), i++)
535 if (i > 0)
536 put_decl_string (",", 1);
537 put_decl_node (TREE_VALUE (args));
539 put_decl_string (")", 1);
542 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't'
543 && TYPE_NAME (node) != NULL_TREE)
545 if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node))
547 put_decl_node (TYPE_ARRAY_ELEMENT (node));
548 put_decl_string("[]", 2);
550 else if (node == promoted_byte_type_node)
551 put_decl_string ("byte", 4);
552 else if (node == promoted_short_type_node)
553 put_decl_string ("short", 5);
554 else if (node == promoted_char_type_node)
555 put_decl_string ("char", 4);
556 else if (node == promoted_boolean_type_node)
557 put_decl_string ("boolean", 7);
558 else if (node == void_type_node && was_pointer)
559 put_decl_string ("null", 4);
560 else
561 put_decl_node (TYPE_NAME (node));
563 else if (TREE_CODE (node) == IDENTIFIER_NODE)
564 put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
565 else
566 put_decl_string ("<unknown>", -1);
569 /* Return a user-friendly name for DECL.
570 The resulting string is only valid until the next call.
571 The value of the hook decl_printable_name is this function,
572 which is also called directly by lang_print_error. */
574 const char *
575 lang_printable_name (decl, v)
576 tree decl;
577 int v __attribute__ ((__unused__));
579 decl_bufpos = 0;
580 put_decl_node (decl);
581 put_decl_string ("", 1);
582 return decl_buf;
585 /* Does the same thing that lang_printable_name, but add a leading
586 space to the DECL name string -- With Leading Space. */
588 const char *
589 lang_printable_name_wls (decl, v)
590 tree decl;
591 int v __attribute__ ((__unused__));
593 decl_bufpos = 1;
594 put_decl_node (decl);
595 put_decl_string ("", 1);
596 decl_buf [0] = ' ';
597 return decl_buf;
600 /* Print on stderr the current class and method context. This function
601 is the value of the hook print_error_function, called from toplev.c. */
603 static void
604 lang_print_error (context, file)
605 diagnostic_context *context __attribute__((__unused__));
606 const char *file;
608 static tree last_error_function_context = NULL_TREE;
609 static tree last_error_function = NULL;
610 static int initialized_p;
612 /* Register LAST_ERROR_FUNCTION_CONTEXT and LAST_ERROR_FUNCTION with
613 the garbage collector. */
614 if (!initialized_p)
616 ggc_add_tree_root (&last_error_function_context, 1);
617 ggc_add_tree_root (&last_error_function, 1);
618 initialized_p = 1;
621 if (current_function_decl != NULL
622 && DECL_CONTEXT (current_function_decl) != last_error_function_context)
624 if (file)
625 fprintf (stderr, "%s: ", file);
627 last_error_function_context = DECL_CONTEXT (current_function_decl);
628 fprintf (stderr, "In class `%s':\n",
629 lang_printable_name (last_error_function_context, 0));
631 if (last_error_function != current_function_decl)
633 if (file)
634 fprintf (stderr, "%s: ", file);
636 if (current_function_decl == NULL)
637 fprintf (stderr, "At top level:\n");
638 else
640 const char *name = lang_printable_name (current_function_decl, 2);
641 fprintf (stderr, "In method `%s':\n", name);
644 last_error_function = current_function_decl;
649 static void
650 java_init ()
652 #if 0
653 extern int flag_minimal_debug;
654 flag_minimal_debug = 0;
655 #endif
657 jcf_path_init ();
658 jcf_path_seal ();
660 decl_printable_name = lang_printable_name;
661 print_error_function = lang_print_error;
662 lang_expand_expr = java_lang_expand_expr;
664 /* Append to Gcc tree node definition arrays */
666 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
667 java_tree_code_type,
668 (int)LAST_JAVA_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
669 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
670 java_tree_code_length,
671 (LAST_JAVA_TREE_CODE -
672 (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
673 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
674 java_tree_code_name,
675 (LAST_JAVA_TREE_CODE -
676 (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
678 using_eh_for_cleanups ();
681 /* This doesn't do anything on purpose. It's used to satisfy the
682 print_error_function hook we don't print error messages with bogus
683 function prototypes. */
685 static void
686 java_dummy_print (c, s)
687 diagnostic_context *c __attribute__ ((__unused__));
688 const char *s __attribute__ ((__unused__));
692 /* Called to install the PRINT_ERROR_FUNCTION hook differently
693 according to LEVEL. LEVEL is 1 during early parsing, when function
694 prototypes aren't fully resolved. print_error_function is set so it
695 doesn't print incomplete function prototypes. When LEVEL is 2,
696 function prototypes are fully resolved and can be printed when
697 reporting errors. */
699 void lang_init_source (level)
700 int level;
702 if (level == 1)
703 print_error_function = java_dummy_print;
704 else
705 print_error_function = lang_print_error;
708 static void
709 java_init_options ()
711 flag_bounds_check = 1;
712 flag_exceptions = 1;
713 flag_non_call_exceptions = 1;
716 const char *
717 lang_identify ()
719 return "Java";
722 /* Hooks for print_node. */
724 void
725 print_lang_decl (file, node, indent)
726 FILE *file __attribute ((__unused__));
727 tree node __attribute ((__unused__));
728 int indent __attribute ((__unused__));
732 void
733 print_lang_type (file, node, indent)
734 FILE *file __attribute ((__unused__));
735 tree node __attribute ((__unused__));
736 int indent __attribute ((__unused__));
740 void
741 print_lang_identifier (file, node, indent)
742 FILE *file __attribute ((__unused__));
743 tree node __attribute ((__unused__));
744 int indent __attribute ((__unused__));
748 void
749 print_lang_statistics ()
753 /* used by print-tree.c */
755 void
756 lang_print_xnode (file, node, indent)
757 FILE *file __attribute ((__unused__));
758 tree node __attribute ((__unused__));
759 int indent __attribute ((__unused__));
763 /* Return the typed-based alias set for T, which may be an expression
764 or a type. Return -1 if we don't do anything special. */
766 HOST_WIDE_INT
767 lang_get_alias_set (t)
768 tree t ATTRIBUTE_UNUSED;
770 return -1;