]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/java/lang.c
lang.c (lang_f_options): Added optimize-static-class-initialization.
[thirdparty/gcc.git] / gcc / java / lang.c
1 /* Java(TM) language-specific utility routines.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
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.
11
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.
16
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.
21
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. */
25
26 /* Hacked by Per Bothner <bothner@cygnus.com> February 1996. */
27
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 "langhooks.h"
38 #include "langhooks-def.h"
39 #include "flags.h"
40 #include "xref.h"
41 #include "ggc.h"
42 #include "diagnostic.h"
43
44 struct string_option
45 {
46 const char *string;
47 int *variable;
48 int on_value;
49 };
50
51 static const char *java_init PARAMS ((const char *));
52 static void java_finish PARAMS ((void));
53 static void java_init_options PARAMS ((void));
54 static int java_decode_option PARAMS ((int, char **));
55 static void put_decl_string PARAMS ((const char *, int));
56 static void put_decl_node PARAMS ((tree));
57 static void java_dummy_print PARAMS ((diagnostic_context *, const char *));
58 static void lang_print_error PARAMS ((diagnostic_context *, const char *));
59 static int process_option_with_no PARAMS ((char *,
60 struct string_option *,
61 int));
62
63 #ifndef TARGET_OBJECT_SUFFIX
64 # define TARGET_OBJECT_SUFFIX ".o"
65 #endif
66
67 /* Table indexed by tree code giving a string containing a character
68 classifying the tree code. Possibilities are
69 t, d, s, c, r, <, 1 and 2. See java/java-tree.def for details. */
70
71 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
72
73 static const char java_tree_code_type[] = {
74 'x',
75 #include "java-tree.def"
76 };
77 #undef DEFTREECODE
78
79 /* Table indexed by tree code giving number of expression
80 operands beyond the fixed part of the node structure.
81 Not used for types or decls. */
82
83 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
84
85 static const int java_tree_code_length[] = {
86 0,
87 #include "java-tree.def"
88 };
89 #undef DEFTREECODE
90
91 /* Names of tree components.
92 Used for printing out the tree and error messages. */
93 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
94
95 static const char *const java_tree_code_name[] = {
96 "@@dummy",
97 #include "java-tree.def"
98 };
99 #undef DEFTREECODE
100
101 int compiling_from_source;
102
103 char * resource_name;
104
105 int flag_emit_class_files = 0;
106
107 /* Nonzero if input file is a file with a list of filenames to compile. */
108
109 int flag_filelist_file = 0;
110
111 /* When non zero, we emit xref strings. Values of the flag for xref
112 backends are defined in xref_flag_table, xref.c. */
113
114 int flag_emit_xref = 0;
115
116 /* When non zero, -Wall was turned on. */
117 int flag_wall = 0;
118
119 /* When non zero, check for redundant modifier uses. */
120 int flag_redundant = 0;
121
122 /* When non zero, call a library routine to do integer divisions. */
123 int flag_use_divide_subroutine = 1;
124
125 /* When non zero, generate code for the Boehm GC. */
126 int flag_use_boehm_gc = 0;
127
128 /* When non zero, assume the runtime uses a hash table to map an
129 object to its synchronization structure. */
130 int flag_hash_synchronization;
131
132 /* When non zero, assume all native functions are implemented with
133 JNI, not CNI. */
134 int flag_jni = 0;
135
136 /* When non zero, warn when source file is newer than matching class
137 file. */
138 int flag_newer = 1;
139
140 /* When non zero, generate checks for references to NULL. */
141 int flag_check_references = 0;
142
143 /* The encoding of the source file. */
144 const char *current_encoding = NULL;
145
146 /* When non zero, report the now deprecated empty statements. */
147 int flag_extraneous_semicolon;
148
149 /* When non zero, always check for a non gcj generated classes archive. */
150 int flag_force_classes_archive_check;
151
152 /* When zero, don't optimize static class initialization. This flag shouldn't
153 be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead. */
154 int flag_optimize_sci = 1;
155
156 /* When non zero, print extra version information. */
157 static int version_flag = 0;
158
159 /* Table of language-dependent -f options.
160 STRING is the option name. VARIABLE is the address of the variable.
161 ON_VALUE is the value to store in VARIABLE
162 if `-fSTRING' is seen as an option.
163 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
164
165 static struct string_option
166 lang_f_options[] =
167 {
168 {"emit-class-file", &flag_emit_class_files, 1},
169 {"emit-class-files", &flag_emit_class_files, 1},
170 {"filelist-file", &flag_filelist_file, 1},
171 {"use-divide-subroutine", &flag_use_divide_subroutine, 1},
172 {"use-boehm-gc", &flag_use_boehm_gc, 1},
173 {"hash-synchronization", &flag_hash_synchronization, 1},
174 {"jni", &flag_jni, 1},
175 {"check-references", &flag_check_references, 1},
176 {"force-classes-archive-check", &flag_force_classes_archive_check, 1},
177 {"optimize-static-class-initialization", &flag_optimize_sci, 1 }
178 };
179
180 static struct string_option
181 lang_W_options[] =
182 {
183 { "redundant-modifiers", &flag_redundant, 1 },
184 { "extraneous-semicolon", &flag_extraneous_semicolon, 1 },
185 { "out-of-date", &flag_newer, 1 }
186 };
187
188 JCF *current_jcf;
189
190 /* Variable controlling how dependency tracking is enabled in
191 java_init. */
192 static int dependency_tracking = 0;
193
194 /* Flag values for DEPENDENCY_TRACKING. */
195 #define DEPEND_SET_FILE 1
196 #define DEPEND_ENABLE 2
197 #define DEPEND_TARGET_SET 4
198 #define DEPEND_FILE_ALREADY_SET 8
199
200 #undef LANG_HOOKS_NAME
201 #define LANG_HOOKS_NAME "GNU Java"
202 #undef LANG_HOOKS_INIT
203 #define LANG_HOOKS_INIT java_init
204 #undef LANG_HOOKS_FINISH
205 #define LANG_HOOKS_FINISH java_finish
206 #undef LANG_HOOKS_INIT_OPTIONS
207 #define LANG_HOOKS_INIT_OPTIONS java_init_options
208 #undef LANG_HOOKS_DECODE_OPTION
209 #define LANG_HOOKS_DECODE_OPTION java_decode_option
210 #undef LANG_HOOKS_SET_YYDEBUG
211 #define LANG_HOOKS_SET_YYDEBUG java_set_yydebug
212
213 /* Each front end provides its own. */
214 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
215
216 /* Process an option that can accept a `no-' form.
217 Return 1 if option found, 0 otherwise. */
218 static int
219 process_option_with_no (p, table, table_size)
220 char *p;
221 struct string_option *table;
222 int table_size;
223 {
224 int j;
225
226 for (j = 0; j < table_size; j++)
227 {
228 if (!strcmp (p, table[j].string))
229 {
230 *table[j].variable = table[j].on_value;
231 return 1;
232 }
233 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
234 && ! strcmp (p+3, table[j].string))
235 {
236 *table[j].variable = ! table[j].on_value;
237 return 1;
238 }
239 }
240
241 return 0;
242 }
243
244 /*
245 * process java-specific compiler command-line options
246 * return 0, but do not complain if the option is not recognised.
247 */
248 static int
249 java_decode_option (argc, argv)
250 int argc __attribute__ ((__unused__));
251 char **argv;
252 {
253 char *p = argv[0];
254
255 if (strcmp (p, "-version") == 0)
256 {
257 version_flag = 1;
258 /* We return 0 so that the caller can process this. */
259 return 0;
260 }
261
262 #define CLARG "-fcompile-resource="
263 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
264 {
265 resource_name = p + sizeof (CLARG) - 1;
266 return 1;
267 }
268 #undef CLARG
269 #define CLARG "-fassume-compiled="
270 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
271 {
272 add_assume_compiled (p + sizeof (CLARG) - 1, 0);
273 return 1;
274 }
275 #undef CLARG
276 #define CLARG "-fno-assume-compiled="
277 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
278 {
279 add_assume_compiled (p + sizeof (CLARG) - 1, 1);
280 return 1;
281 }
282 #undef CLARG
283 #define CLARG "-fassume-compiled"
284 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
285 {
286 add_assume_compiled ("", 0);
287 return 1;
288 }
289 #undef CLARG
290 #define CLARG "-fno-assume-compiled"
291 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
292 {
293 add_assume_compiled ("", 1);
294 return 1;
295 }
296 #undef CLARG
297 #define CLARG "-fclasspath="
298 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
299 {
300 jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
301 return 1;
302 }
303 #undef CLARG
304 #define CLARG "-fCLASSPATH="
305 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
306 {
307 jcf_path_CLASSPATH_arg (p + sizeof (CLARG) - 1);
308 return 1;
309 }
310 #undef CLARG
311 else if (strncmp (p, "-I", 2) == 0)
312 {
313 jcf_path_include_arg (p + 2);
314 return 1;
315 }
316
317 #define ARG "-foutput-class-dir="
318 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
319 {
320 jcf_write_base_directory = p + sizeof (ARG) - 1;
321 return 1;
322 }
323 #undef ARG
324 #define ARG "-fencoding="
325 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
326 {
327 current_encoding = p + sizeof (ARG) - 1;
328 return 1;
329 }
330 #undef ARG
331
332 if (p[0] == '-' && p[1] == 'f')
333 {
334 /* Some kind of -f option.
335 P's value is the option sans `-f'.
336 Search for it in the table of options. */
337 p += 2;
338 return process_option_with_no (p, lang_f_options,
339 ARRAY_SIZE (lang_f_options));
340 }
341
342 if (strcmp (p, "-Wall") == 0)
343 {
344 flag_wall = 1;
345 flag_redundant = 1;
346 flag_extraneous_semicolon = 1;
347 /* When -Wall given, enable -Wunused. We do this because the C
348 compiler does it, and people expect it. */
349 set_Wunused (1);
350 return 1;
351 }
352
353 if (p[0] == '-' && p[1] == 'W')
354 {
355 /* Skip `-W' and see if we accept the option or its `no-' form. */
356 p += 2;
357 return process_option_with_no (p, lang_W_options,
358 ARRAY_SIZE (lang_W_options));
359 }
360
361 if (strcmp (p, "-MD") == 0)
362 {
363 jcf_dependency_init (1);
364 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
365 return 1;
366 }
367 else if (strcmp (p, "-MMD") == 0)
368 {
369 jcf_dependency_init (0);
370 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
371 return 1;
372 }
373 else if (strcmp (p, "-M") == 0)
374 {
375 jcf_dependency_init (1);
376 dependency_tracking |= DEPEND_ENABLE;
377 return 1;
378 }
379 else if (strcmp (p, "-MM") == 0)
380 {
381 jcf_dependency_init (0);
382 dependency_tracking |= DEPEND_ENABLE;
383 return 1;
384 }
385 else if (strcmp (p, "-MP") == 0)
386 {
387 jcf_dependency_print_dummies ();
388 return 1;
389 }
390 else if (strcmp (p, "-MT") == 0)
391 {
392 jcf_dependency_set_target (argv[1]);
393 dependency_tracking |= DEPEND_TARGET_SET;
394 return 2;
395 }
396 else if (strcmp (p, "-MF") == 0)
397 {
398 jcf_dependency_set_dep_file (argv[1]);
399 dependency_tracking |= DEPEND_FILE_ALREADY_SET;
400 return 2;
401 }
402
403 return 0;
404 }
405
406 /* Global open file. */
407 FILE *finput;
408
409 static const char *
410 java_init (filename)
411 const char *filename;
412 {
413 #if 0
414 extern int flag_minimal_debug;
415 flag_minimal_debug = 0;
416 #endif
417
418 /* Open input file. */
419
420 if (filename == 0 || !strcmp (filename, "-"))
421 {
422 finput = stdin;
423 filename = "stdin";
424
425 if (dependency_tracking)
426 error ("can't do dependency tracking with input from stdin");
427 }
428 else
429 {
430 if (dependency_tracking)
431 {
432 char *dot;
433
434 /* If the target is set and the output filename is set, then
435 there's no processing to do here. Otherwise we must
436 compute one or the other. */
437 if (! ((dependency_tracking & DEPEND_TARGET_SET)
438 && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
439 {
440 dot = strrchr (filename, '.');
441 if (dot == NULL)
442 error ("couldn't determine target name for dependency tracking");
443 else
444 {
445 char *buf = (char *) xmalloc (dot - filename +
446 3 + sizeof (TARGET_OBJECT_SUFFIX));
447 strncpy (buf, filename, dot - filename);
448
449 /* If emitting class files, we might have multiple
450 targets. The class generation code takes care of
451 registering them. Otherwise we compute the
452 target name here. */
453 if ((dependency_tracking & DEPEND_TARGET_SET))
454 ; /* Nothing. */
455 else if (flag_emit_class_files)
456 jcf_dependency_set_target (NULL);
457 else
458 {
459 strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
460 jcf_dependency_set_target (buf);
461 }
462
463 if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
464 ; /* Nothing. */
465 else if ((dependency_tracking & DEPEND_SET_FILE))
466 {
467 strcpy (buf + (dot - filename), ".d");
468 jcf_dependency_set_dep_file (buf);
469 }
470 else
471 jcf_dependency_set_dep_file ("-");
472
473 free (buf);
474 }
475 }
476 }
477 }
478
479 jcf_path_init ();
480 jcf_path_seal (version_flag);
481
482 decl_printable_name = lang_printable_name;
483 print_error_function = lang_print_error;
484 lang_expand_expr = java_lang_expand_expr;
485
486 /* Append to Gcc tree node definition arrays */
487
488 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
489 java_tree_code_type,
490 (int)LAST_JAVA_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
491 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
492 java_tree_code_length,
493 (LAST_JAVA_TREE_CODE -
494 (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
495 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
496 java_tree_code_name,
497 (LAST_JAVA_TREE_CODE -
498 (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
499 java_init_decl_processing ();
500
501 using_eh_for_cleanups ();
502
503 return filename;
504 }
505
506 static void
507 java_finish ()
508 {
509 jcf_dependency_write ();
510 }
511
512 /* Buffer used by lang_printable_name. */
513 static char *decl_buf = NULL;
514
515 /* Allocated size of decl_buf. */
516 static int decl_buflen = 0;
517
518 /* Length of used part of decl_buf; position for next character. */
519 static int decl_bufpos = 0;
520
521 /* Append the string STR to decl_buf.
522 It length is given by LEN; -1 means the string is nul-terminated. */
523
524 static void
525 put_decl_string (str, len)
526 const char *str;
527 int len;
528 {
529 if (len < 0)
530 len = strlen (str);
531 if (decl_bufpos + len >= decl_buflen)
532 {
533 if (decl_buf == NULL)
534 {
535 decl_buflen = len + 100;
536 decl_buf = (char *) xmalloc (decl_buflen);
537 }
538 else
539 {
540 decl_buflen *= 2;
541 decl_buf = (char *) xrealloc (decl_buf, decl_buflen);
542 }
543 }
544 strcpy (decl_buf + decl_bufpos, str);
545 decl_bufpos += len;
546 }
547
548 /* Append to decl_buf a printable name for NODE. */
549
550 static void
551 put_decl_node (node)
552 tree node;
553 {
554 int was_pointer = 0;
555 if (TREE_CODE (node) == POINTER_TYPE)
556 {
557 node = TREE_TYPE (node);
558 was_pointer = 1;
559 }
560 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd'
561 && DECL_NAME (node) != NULL_TREE)
562 {
563 if (TREE_CODE (node) == FUNCTION_DECL)
564 {
565 /* We want to print the type the DECL belongs to. We don't do
566 that when we handle constructors. */
567 if (! DECL_CONSTRUCTOR_P (node)
568 && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node))
569 {
570 put_decl_node (TYPE_NAME (DECL_CONTEXT (node)));
571 put_decl_string (".", 1);
572 }
573 if (! DECL_CONSTRUCTOR_P (node))
574 put_decl_node (DECL_NAME (node));
575 if (TREE_TYPE (node) != NULL_TREE)
576 {
577 int i = 0;
578 tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
579 if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
580 args = TREE_CHAIN (args);
581 put_decl_string ("(", 1);
582 for ( ; args != end_params_node; args = TREE_CHAIN (args), i++)
583 {
584 if (i > 0)
585 put_decl_string (",", 1);
586 put_decl_node (TREE_VALUE (args));
587 }
588 put_decl_string (")", 1);
589 }
590 }
591 else
592 put_decl_node (DECL_NAME (node));
593 }
594 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't'
595 && TYPE_NAME (node) != NULL_TREE)
596 {
597 if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node))
598 {
599 put_decl_node (TYPE_ARRAY_ELEMENT (node));
600 put_decl_string("[]", 2);
601 }
602 else if (node == promoted_byte_type_node)
603 put_decl_string ("byte", 4);
604 else if (node == promoted_short_type_node)
605 put_decl_string ("short", 5);
606 else if (node == promoted_char_type_node)
607 put_decl_string ("char", 4);
608 else if (node == promoted_boolean_type_node)
609 put_decl_string ("boolean", 7);
610 else if (node == void_type_node && was_pointer)
611 put_decl_string ("null", 4);
612 else
613 put_decl_node (TYPE_NAME (node));
614 }
615 else if (TREE_CODE (node) == IDENTIFIER_NODE)
616 put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
617 else
618 put_decl_string ("<unknown>", -1);
619 }
620
621 /* Return a user-friendly name for DECL.
622 The resulting string is only valid until the next call.
623 The value of the hook decl_printable_name is this function,
624 which is also called directly by lang_print_error. */
625
626 const char *
627 lang_printable_name (decl, v)
628 tree decl;
629 int v __attribute__ ((__unused__));
630 {
631 decl_bufpos = 0;
632 put_decl_node (decl);
633 put_decl_string ("", 1);
634 return decl_buf;
635 }
636
637 /* Does the same thing that lang_printable_name, but add a leading
638 space to the DECL name string -- With Leading Space. */
639
640 const char *
641 lang_printable_name_wls (decl, v)
642 tree decl;
643 int v __attribute__ ((__unused__));
644 {
645 decl_bufpos = 1;
646 put_decl_node (decl);
647 put_decl_string ("", 1);
648 decl_buf [0] = ' ';
649 return decl_buf;
650 }
651
652 /* Print on stderr the current class and method context. This function
653 is the value of the hook print_error_function, called from toplev.c. */
654
655 static void
656 lang_print_error (context, file)
657 diagnostic_context *context __attribute__((__unused__));
658 const char *file;
659 {
660 static tree last_error_function_context = NULL_TREE;
661 static tree last_error_function = NULL;
662 static int initialized_p;
663
664 /* Register LAST_ERROR_FUNCTION_CONTEXT and LAST_ERROR_FUNCTION with
665 the garbage collector. */
666 if (!initialized_p)
667 {
668 ggc_add_tree_root (&last_error_function_context, 1);
669 ggc_add_tree_root (&last_error_function, 1);
670 initialized_p = 1;
671 }
672
673 if (current_function_decl != NULL
674 && DECL_CONTEXT (current_function_decl) != last_error_function_context)
675 {
676 if (file)
677 fprintf (stderr, "%s: ", file);
678
679 last_error_function_context = DECL_CONTEXT (current_function_decl);
680 fprintf (stderr, "In class `%s':\n",
681 lang_printable_name (last_error_function_context, 0));
682 }
683 if (last_error_function != current_function_decl)
684 {
685 if (file)
686 fprintf (stderr, "%s: ", file);
687
688 if (current_function_decl == NULL)
689 fprintf (stderr, "At top level:\n");
690 else
691 {
692 const char *name = lang_printable_name (current_function_decl, 2);
693 fprintf (stderr, "In %s `%s':\n",
694 (DECL_CONSTRUCTOR_P (current_function_decl) ? "constructor"
695 : "method"),
696 name);
697 }
698
699 last_error_function = current_function_decl;
700 }
701
702 }
703
704 /* This doesn't do anything on purpose. It's used to satisfy the
705 print_error_function hook we don't print error messages with bogus
706 function prototypes. */
707
708 static void
709 java_dummy_print (c, s)
710 diagnostic_context *c __attribute__ ((__unused__));
711 const char *s __attribute__ ((__unused__));
712 {
713 }
714
715 /* Called to install the PRINT_ERROR_FUNCTION hook differently
716 according to LEVEL. LEVEL is 1 during early parsing, when function
717 prototypes aren't fully resolved. print_error_function is set so it
718 doesn't print incomplete function prototypes. When LEVEL is 2,
719 function prototypes are fully resolved and can be printed when
720 reporting errors. */
721
722 void lang_init_source (level)
723 int level;
724 {
725 if (level == 1)
726 print_error_function = java_dummy_print;
727 else
728 print_error_function = lang_print_error;
729 }
730
731 static void
732 java_init_options ()
733 {
734 flag_bounds_check = 1;
735 flag_exceptions = 1;
736 flag_non_call_exceptions = 1;
737 }