1 /* Java(TM) language-specific utility routines.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002
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 #include "tree-inline.h"
44 #include "splay-tree.h"
45 #include "tree-dump.h"
46
47 struct string_option
48 {
49 const char *const string;
50 int *const variable;
51 const int on_value;
52 };
53
54 static const char *java_init PARAMS ((const char *));
55 static void java_finish PARAMS ((void));
56 static void java_init_options PARAMS ((void));
57 static bool java_post_options PARAMS ((void));
58
59 static int java_decode_option PARAMS ((int, char **));
60 static void put_decl_string PARAMS ((const char *, int));
61 static void put_decl_node PARAMS ((tree));
62 static void java_print_error_function PARAMS ((diagnostic_context *,
63 const char *));
64 static int process_option_with_no PARAMS ((const char *,
65 const struct string_option *,
66 int));
67 static tree java_tree_inlining_walk_subtrees PARAMS ((tree *,
68 int *,
69 walk_tree_fn,
70 void *,
71 void *));
72 static int java_unsafe_for_reeval PARAMS ((tree));
73 static int merge_init_test_initialization PARAMS ((void * *,
74 void *));
75 static int inline_init_test_initialization PARAMS ((void * *,
76 void *));
77 static bool java_can_use_bit_fields_p PARAMS ((void));
78 static bool java_decl_ok_for_sibcall (tree);
79 static int java_dump_tree PARAMS ((void *, tree));
80 static void dump_compound_expr PARAMS ((dump_info_p, tree));
81
82 #ifndef TARGET_OBJECT_SUFFIX
83 # define TARGET_OBJECT_SUFFIX ".o"
84 #endif
85
86 /* Table indexed by tree code giving a string containing a character
87 classifying the tree code. Possibilities are
88 t, d, s, c, r, <, 1 and 2. See java/java-tree.def for details. */
89
90 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
91
92 const char tree_code_type[] = {
93 #include "tree.def"
94 'x',
95 #include "java-tree.def"
96 };
97 #undef DEFTREECODE
98
99 /* Table indexed by tree code giving number of expression
100 operands beyond the fixed part of the node structure.
101 Not used for types or decls. */
102
103 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
104
105 const unsigned char tree_code_length[] = {
106 #include "tree.def"
107 0,
108 #include "java-tree.def"
109 };
110 #undef DEFTREECODE
111
112 /* Names of tree components.
113 Used for printing out the tree and error messages. */
114 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
115
116 const char *const tree_code_name[] = {
117 #include "tree.def"
118 "@@dummy",
119 #include "java-tree.def"
120 };
121 #undef DEFTREECODE
122
123 /* Used to avoid printing error messages with bogus function
124 prototypes. Starts out false. */
125 static bool inhibit_error_function_printing;
126
127 int compiling_from_source;
128
129 char * resource_name;
130
131 int flag_emit_class_files = 0;
132
133 /* Nonzero if input file is a file with a list of filenames to compile. */
134
135 int flag_filelist_file = 0;
136
137 /* When nonzero, we emit xref strings. Values of the flag for xref
138 backends are defined in xref_flag_table, xref.c. */
139
140 int flag_emit_xref = 0;
141
142 /* When nonzero, -Wall was turned on. */
143 int flag_wall = 0;
144
145 /* When nonzero, check for redundant modifier uses. */
146 int flag_redundant = 0;
147
148 /* When nonzero, call a library routine to do integer divisions. */
149 int flag_use_divide_subroutine = 1;
150
151 /* When nonzero, generate code for the Boehm GC. */
152 int flag_use_boehm_gc = 0;
153
154 /* When nonzero, assume the runtime uses a hash table to map an
155 object to its synchronization structure. */
156 int flag_hash_synchronization;
157
158 /* When nonzero, permit the use of the assert keyword. */
159 int flag_assert = 1;
160
161 /* When nonzero, assume all native functions are implemented with
162 JNI, not CNI. */
163 int flag_jni = 0;
164
165 /* When nonzero, warn when source file is newer than matching class
166 file. */
167 int flag_newer = 1;
168
169 /* When nonzero, generate checks for references to NULL. */
170 int flag_check_references = 0;
171
172 /* The encoding of the source file. */
173 const char *current_encoding = NULL;
174
175 /* When nonzero, report the now deprecated empty statements. */
176 int flag_extraneous_semicolon;
177
178 /* When nonzero, always check for a non gcj generated classes archive. */
179 int flag_force_classes_archive_check;
180
181 /* When zero, don't optimize static class initialization. This flag shouldn't
182 be tested alone, use STATIC_CLASS_INITIALIZATION_OPTIMIZATION_P instead. */
183 int flag_optimize_sci = 1;
184
185 /* When nonzero, use offset tables for virtual method calls
186 in order to improve binary compatibility. */
187 int flag_indirect_dispatch = 0;
188
189 /* When zero, don't generate runtime array store checks. */
190 int flag_store_check = 1;
191
192 /* When nonzero, print extra version information. */
193 static int version_flag = 0;
194
195 /* Set nonzero if the user specified -finline-functions on the command
196 line. */
197 int flag_really_inline = 0;
198
199 /* Table of language-dependent -f options.
200 STRING is the option name. VARIABLE is the address of the variable.
201 ON_VALUE is the value to store in VARIABLE
202 if `-fSTRING' is seen as an option.
203 (If `-fno-STRING' is seen as an option, the opposite value is stored.) */
204
205 static const struct string_option
206 lang_f_options[] =
207 {
208 {"emit-class-file", &flag_emit_class_files, 1},
209 {"emit-class-files", &flag_emit_class_files, 1},
210 {"filelist-file", &flag_filelist_file, 1},
211 {"use-divide-subroutine", &flag_use_divide_subroutine, 1},
212 {"use-boehm-gc", &flag_use_boehm_gc, 1},
213 {"hash-synchronization", &flag_hash_synchronization, 1},
214 {"jni", &flag_jni, 1},
215 {"check-references", &flag_check_references, 1},
216 {"force-classes-archive-check", &flag_force_classes_archive_check, 1},
217 {"optimize-static-class-initialization", &flag_optimize_sci, 1 },
218 {"indirect-dispatch", &flag_indirect_dispatch, 1},
219 {"store-check", &flag_store_check, 1},
220 {"assert", &flag_assert, 1}
221 };
222
223 static const struct string_option
224 lang_W_options[] =
225 {
226 { "redundant-modifiers", &flag_redundant, 1 },
227 { "extraneous-semicolon", &flag_extraneous_semicolon, 1 },
228 { "out-of-date", &flag_newer, 1 }
229 };
230
231 JCF *current_jcf;
232
233 /* Variable controlling how dependency tracking is enabled in
234 java_init. */
235 static int dependency_tracking = 0;
236
237 /* Flag values for DEPENDENCY_TRACKING. */
238 #define DEPEND_SET_FILE 1
239 #define DEPEND_ENABLE 2
240 #define DEPEND_TARGET_SET 4
241 #define DEPEND_FILE_ALREADY_SET 8
242
243 struct language_function GTY(())
244 {
245 int unused;
246 };
247
248 #undef LANG_HOOKS_NAME
249 #define LANG_HOOKS_NAME "GNU Java"
250 #undef LANG_HOOKS_INIT
251 #define LANG_HOOKS_INIT java_init
252 #undef LANG_HOOKS_FINISH
253 #define LANG_HOOKS_FINISH java_finish
254 #undef LANG_HOOKS_INIT_OPTIONS
255 #define LANG_HOOKS_INIT_OPTIONS java_init_options
256 #undef LANG_HOOKS_DECODE_OPTION
257 #define LANG_HOOKS_DECODE_OPTION java_decode_option
258 #undef LANG_HOOKS_POST_OPTIONS
259 #define LANG_HOOKS_POST_OPTIONS java_post_options
260 #undef LANG_HOOKS_PARSE_FILE
261 #define LANG_HOOKS_PARSE_FILE java_parse_file
262 #undef LANG_HOOKS_UNSAFE_FOR_REEVAL
263 #define LANG_HOOKS_UNSAFE_FOR_REEVAL java_unsafe_for_reeval
264 #undef LANG_HOOKS_MARK_ADDRESSABLE
265 #define LANG_HOOKS_MARK_ADDRESSABLE java_mark_addressable
266 #undef LANG_HOOKS_EXPAND_EXPR
267 #define LANG_HOOKS_EXPAND_EXPR java_expand_expr
268 #undef LANG_HOOKS_TRUTHVALUE_CONVERSION
269 #define LANG_HOOKS_TRUTHVALUE_CONVERSION java_truthvalue_conversion
270 #undef LANG_HOOKS_DUP_LANG_SPECIFIC_DECL
271 #define LANG_HOOKS_DUP_LANG_SPECIFIC_DECL java_dup_lang_specific_decl
272 #undef LANG_HOOKS_DECL_PRINTABLE_NAME
273 #define LANG_HOOKS_DECL_PRINTABLE_NAME lang_printable_name
274 #undef LANG_HOOKS_PRINT_ERROR_FUNCTION
275 #define LANG_HOOKS_PRINT_ERROR_FUNCTION java_print_error_function
276 #undef LANG_HOOKS_CAN_USE_BIT_FIELDS_P
277 #define LANG_HOOKS_CAN_USE_BIT_FIELDS_P java_can_use_bit_fields_p
278
279 #undef LANG_HOOKS_TYPE_FOR_MODE
280 #define LANG_HOOKS_TYPE_FOR_MODE java_type_for_mode
281 #undef LANG_HOOKS_TYPE_FOR_SIZE
282 #define LANG_HOOKS_TYPE_FOR_SIZE java_type_for_size
283 #undef LANG_HOOKS_SIGNED_TYPE
284 #define LANG_HOOKS_SIGNED_TYPE java_signed_type
285 #undef LANG_HOOKS_UNSIGNED_TYPE
286 #define LANG_HOOKS_UNSIGNED_TYPE java_unsigned_type
287 #undef LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE
288 #define LANG_HOOKS_SIGNED_OR_UNSIGNED_TYPE java_signed_or_unsigned_type
289
290 #undef LANG_HOOKS_TREE_INLINING_WALK_SUBTREES
291 #define LANG_HOOKS_TREE_INLINING_WALK_SUBTREES java_tree_inlining_walk_subtrees
292
293 #undef LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN
294 #define LANG_HOOKS_TREE_DUMP_DUMP_TREE_FN java_dump_tree
295
296 #undef LANG_HOOKS_DECL_OK_FOR_SIBCALL
297 #define LANG_HOOKS_DECL_OK_FOR_SIBCALL java_decl_ok_for_sibcall
298
299 /* Each front end provides its own. */
300 const struct lang_hooks lang_hooks = LANG_HOOKS_INITIALIZER;
301
302 /* Process an option that can accept a `no-' form.
303 Return 1 if option found, 0 otherwise. */
304 static int
process_option_with_no(p,table,table_size)305 process_option_with_no (p, table, table_size)
306 const char *p;
307 const struct string_option *table;
308 int table_size;
309 {
310 int j;
311
312 for (j = 0; j < table_size; j++)
313 {
314 if (!strcmp (p, table[j].string))
315 {
316 *table[j].variable = table[j].on_value;
317 return 1;
318 }
319 if (p[0] == 'n' && p[1] == 'o' && p[2] == '-'
320 && ! strcmp (p+3, table[j].string))
321 {
322 *table[j].variable = ! table[j].on_value;
323 return 1;
324 }
325 }
326
327 return 0;
328 }
329
330 /*
331 * process java-specific compiler command-line options
332 * return 0, but do not complain if the option is not recognized.
333 */
334 static int
java_decode_option(argc,argv)335 java_decode_option (argc, argv)
336 int argc __attribute__ ((__unused__));
337 char **argv;
338 {
339 char *p = argv[0];
340
341 jcf_path_init ();
342
343 if (strcmp (p, "-version") == 0)
344 {
345 version_flag = 1;
346 /* We return 0 so that the caller can process this. */
347 return 0;
348 }
349
350 #define CLARG "-fcompile-resource="
351 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
352 {
353 resource_name = p + sizeof (CLARG) - 1;
354 return 1;
355 }
356 #undef CLARG
357 #define CLARG "-fassume-compiled="
358 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
359 {
360 add_assume_compiled (p + sizeof (CLARG) - 1, 0);
361 return 1;
362 }
363 #undef CLARG
364 #define CLARG "-fno-assume-compiled="
365 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
366 {
367 add_assume_compiled (p + sizeof (CLARG) - 1, 1);
368 return 1;
369 }
370 #undef CLARG
371 #define CLARG "-fassume-compiled"
372 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
373 {
374 add_assume_compiled ("", 0);
375 return 1;
376 }
377 #undef CLARG
378 #define CLARG "-fno-assume-compiled"
379 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
380 {
381 add_assume_compiled ("", 1);
382 return 1;
383 }
384 #undef CLARG
385 #define CLARG "-fCLASSPATH="
386 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
387 {
388 jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
389 return 1;
390 }
391 #undef CLARG
392 #define CLARG "-fclasspath="
393 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
394 {
395 jcf_path_classpath_arg (p + sizeof (CLARG) - 1);
396 return 1;
397 }
398 #undef CLARG
399 #define CLARG "-fbootclasspath="
400 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
401 {
402 jcf_path_bootclasspath_arg (p + sizeof (CLARG) - 1);
403 return 1;
404 }
405 #undef CLARG
406 #define CLARG "-fextdirs="
407 if (strncmp (p, CLARG, sizeof (CLARG) - 1) == 0)
408 {
409 jcf_path_extdirs_arg (p + sizeof (CLARG) - 1);
410 return 1;
411 }
412 #undef CLARG
413 else if (strncmp (p, "-I", 2) == 0)
414 {
415 jcf_path_include_arg (p + 2);
416 return 1;
417 }
418
419 #define ARG "-foutput-class-dir="
420 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
421 {
422 jcf_write_base_directory = p + sizeof (ARG) - 1;
423 return 1;
424 }
425 #undef ARG
426 #define ARG "-fencoding="
427 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
428 {
429 current_encoding = p + sizeof (ARG) - 1;
430 return 1;
431 }
432 #undef ARG
433 #define ARG "-finline-functions"
434 if (strncmp (p, ARG, sizeof (ARG) - 1) == 0)
435 {
436 flag_inline_functions = 1;
437 flag_really_inline = 1;
438 return 1;
439 }
440 #undef ARG
441
442 if (p[0] == '-' && p[1] == 'f')
443 {
444 /* Some kind of -f option.
445 P's value is the option sans `-f'.
446 Search for it in the table of options. */
447 p += 2;
448 if (process_option_with_no (p, lang_f_options,
449 ARRAY_SIZE (lang_f_options)))
450 return 1;
451 return dump_switch_p (p);
452 }
453
454 if (strcmp (p, "-Wall") == 0)
455 {
456 flag_wall = 1;
457 flag_redundant = 1;
458 flag_extraneous_semicolon = 1;
459 /* When -Wall given, enable -Wunused. We do this because the C
460 compiler does it, and people expect it. */
461 set_Wunused (1);
462 return 1;
463 }
464
465 if (p[0] == '-' && p[1] == 'W')
466 {
467 /* Skip `-W' and see if we accept the option or its `no-' form. */
468 p += 2;
469 return process_option_with_no (p, lang_W_options,
470 ARRAY_SIZE (lang_W_options));
471 }
472
473 if (strcmp (p, "-MD") == 0)
474 {
475 jcf_dependency_init (1);
476 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
477 return 1;
478 }
479 else if (strcmp (p, "-MMD") == 0)
480 {
481 jcf_dependency_init (0);
482 dependency_tracking |= DEPEND_SET_FILE | DEPEND_ENABLE;
483 return 1;
484 }
485 else if (strcmp (p, "-M") == 0)
486 {
487 jcf_dependency_init (1);
488 dependency_tracking |= DEPEND_ENABLE;
489 return 1;
490 }
491 else if (strcmp (p, "-MM") == 0)
492 {
493 jcf_dependency_init (0);
494 dependency_tracking |= DEPEND_ENABLE;
495 return 1;
496 }
497 else if (strcmp (p, "-MP") == 0)
498 {
499 jcf_dependency_print_dummies ();
500 return 1;
501 }
502 else if (strcmp (p, "-MT") == 0)
503 {
504 jcf_dependency_set_target (argv[1]);
505 dependency_tracking |= DEPEND_TARGET_SET;
506 return 2;
507 }
508 else if (strcmp (p, "-MF") == 0)
509 {
510 jcf_dependency_set_dep_file (argv[1]);
511 dependency_tracking |= DEPEND_FILE_ALREADY_SET;
512 return 2;
513 }
514
515 return 0;
516 }
517
518 /* Global open file. */
519 FILE *finput;
520
521 static const char *
java_init(filename)522 java_init (filename)
523 const char *filename;
524 {
525 #if 0
526 extern int flag_minimal_debug;
527 flag_minimal_debug = 0;
528 #endif
529
530 if (flag_inline_functions)
531 flag_inline_trees = 1;
532
533 /* Force minimum function alignment if g++ uses the least significant
534 bit of function pointers to store the virtual bit. This is required
535 to keep vtables compatible. */
536 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
537 && force_align_functions_log < 1)
538 force_align_functions_log = 1;
539
540 /* Open input file. */
541
542 if (filename == 0 || !strcmp (filename, "-"))
543 {
544 finput = stdin;
545 filename = "stdin";
546
547 if (dependency_tracking)
548 error ("can't do dependency tracking with input from stdin");
549 }
550 else
551 {
552 if (dependency_tracking)
553 {
554 char *dot;
555
556 /* If the target is set and the output filename is set, then
557 there's no processing to do here. Otherwise we must
558 compute one or the other. */
559 if (! ((dependency_tracking & DEPEND_TARGET_SET)
560 && (dependency_tracking & DEPEND_FILE_ALREADY_SET)))
561 {
562 dot = strrchr (filename, '.');
563 if (dot == NULL)
564 error ("couldn't determine target name for dependency tracking");
565 else
566 {
567 char *buf = xmalloc (dot - filename +
568 3 + sizeof (TARGET_OBJECT_SUFFIX));
569 strncpy (buf, filename, dot - filename);
570
571 /* If emitting class files, we might have multiple
572 targets. The class generation code takes care of
573 registering them. Otherwise we compute the
574 target name here. */
575 if ((dependency_tracking & DEPEND_TARGET_SET))
576 ; /* Nothing. */
577 else if (flag_emit_class_files)
578 jcf_dependency_set_target (NULL);
579 else
580 {
581 strcpy (buf + (dot - filename), TARGET_OBJECT_SUFFIX);
582 jcf_dependency_set_target (buf);
583 }
584
585 if ((dependency_tracking & DEPEND_FILE_ALREADY_SET))
586 ; /* Nothing. */
587 else if ((dependency_tracking & DEPEND_SET_FILE))
588 {
589 strcpy (buf + (dot - filename), ".d");
590 jcf_dependency_set_dep_file (buf);
591 }
592 else
593 jcf_dependency_set_dep_file ("-");
594
595 free (buf);
596 }
597 }
598 }
599 }
600
601 jcf_path_init ();
602 jcf_path_seal (version_flag);
603
604 java_init_decl_processing ();
605
606 using_eh_for_cleanups ();
607
608 return filename;
609 }
610
611 static void
java_finish()612 java_finish ()
613 {
614 jcf_dependency_write ();
615 }
616
617 /* Buffer used by lang_printable_name. */
618 static char *decl_buf = NULL;
619
620 /* Allocated size of decl_buf. */
621 static int decl_buflen = 0;
622
623 /* Length of used part of decl_buf; position for next character. */
624 static int decl_bufpos = 0;
625
626 /* Append the string STR to decl_buf.
627 It length is given by LEN; -1 means the string is nul-terminated. */
628
629 static void
put_decl_string(str,len)630 put_decl_string (str, len)
631 const char *str;
632 int len;
633 {
634 if (len < 0)
635 len = strlen (str);
636 if (decl_bufpos + len >= decl_buflen)
637 {
638 if (decl_buf == NULL)
639 {
640 decl_buflen = len + 100;
641 decl_buf = xmalloc (decl_buflen);
642 }
643 else
644 {
645 decl_buflen *= 2;
646 decl_buf = xrealloc (decl_buf, decl_buflen);
647 }
648 }
649 strcpy (decl_buf + decl_bufpos, str);
650 decl_bufpos += len;
651 }
652
653 /* Append to decl_buf a printable name for NODE. */
654
655 static void
put_decl_node(node)656 put_decl_node (node)
657 tree node;
658 {
659 int was_pointer = 0;
660 if (TREE_CODE (node) == POINTER_TYPE)
661 {
662 node = TREE_TYPE (node);
663 was_pointer = 1;
664 }
665 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd'
666 && DECL_NAME (node) != NULL_TREE)
667 {
668 if (TREE_CODE (node) == FUNCTION_DECL)
669 {
670 /* We want to print the type the DECL belongs to. We don't do
671 that when we handle constructors. */
672 if (! DECL_CONSTRUCTOR_P (node)
673 && ! DECL_ARTIFICIAL (node) && DECL_CONTEXT (node))
674 {
675 put_decl_node (TYPE_NAME (DECL_CONTEXT (node)));
676 put_decl_string (".", 1);
677 }
678 if (! DECL_CONSTRUCTOR_P (node))
679 put_decl_node (DECL_NAME (node));
680 if (TREE_TYPE (node) != NULL_TREE)
681 {
682 int i = 0;
683 tree args = TYPE_ARG_TYPES (TREE_TYPE (node));
684 if (TREE_CODE (TREE_TYPE (node)) == METHOD_TYPE)
685 args = TREE_CHAIN (args);
686 put_decl_string ("(", 1);
687 for ( ; args != end_params_node; args = TREE_CHAIN (args), i++)
688 {
689 if (i > 0)
690 put_decl_string (",", 1);
691 put_decl_node (TREE_VALUE (args));
692 }
693 put_decl_string (")", 1);
694 }
695 }
696 else
697 put_decl_node (DECL_NAME (node));
698 }
699 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't'
700 && TYPE_NAME (node) != NULL_TREE)
701 {
702 if (TREE_CODE (node) == RECORD_TYPE && TYPE_ARRAY_P (node))
703 {
704 put_decl_node (TYPE_ARRAY_ELEMENT (node));
705 put_decl_string("[]", 2);
706 }
707 else if (node == promoted_byte_type_node)
708 put_decl_string ("byte", 4);
709 else if (node == promoted_short_type_node)
710 put_decl_string ("short", 5);
711 else if (node == promoted_char_type_node)
712 put_decl_string ("char", 4);
713 else if (node == promoted_boolean_type_node)
714 put_decl_string ("boolean", 7);
715 else if (node == void_type_node && was_pointer)
716 put_decl_string ("null", 4);
717 else
718 put_decl_node (TYPE_NAME (node));
719 }
720 else if (TREE_CODE (node) == IDENTIFIER_NODE)
721 put_decl_string (IDENTIFIER_POINTER (node), IDENTIFIER_LENGTH (node));
722 else
723 put_decl_string ("<unknown>", -1);
724 }
725
726 /* Return a user-friendly name for DECL.
727 The resulting string is only valid until the next call.
728 The value of the hook decl_printable_name is this function,
729 which is also called directly by java_print_error_function. */
730
731 const char *
lang_printable_name(decl,v)732 lang_printable_name (decl, v)
733 tree decl;
734 int v __attribute__ ((__unused__));
735 {
736 decl_bufpos = 0;
737 put_decl_node (decl);
738 put_decl_string ("", 1);
739 return decl_buf;
740 }
741
742 /* Does the same thing that lang_printable_name, but add a leading
743 space to the DECL name string -- With Leading Space. */
744
745 const char *
lang_printable_name_wls(decl,v)746 lang_printable_name_wls (decl, v)
747 tree decl;
748 int v __attribute__ ((__unused__));
749 {
750 decl_bufpos = 1;
751 put_decl_node (decl);
752 put_decl_string ("", 1);
753 decl_buf [0] = ' ';
754 return decl_buf;
755 }
756
757 /* Print on stderr the current class and method context. This function
758 is the value of the hook print_error_function. */
759
760 static GTY(()) tree last_error_function_context;
761 static GTY(()) tree last_error_function;
762 static void
java_print_error_function(context,file)763 java_print_error_function (context, file)
764 diagnostic_context *context __attribute__((__unused__));
765 const char *file;
766 {
767 /* Don't print error messages with bogus function prototypes. */
768 if (inhibit_error_function_printing)
769 return;
770
771 if (current_function_decl != NULL
772 && DECL_CONTEXT (current_function_decl) != last_error_function_context)
773 {
774 if (file)
775 fprintf (stderr, "%s: ", file);
776
777 last_error_function_context = DECL_CONTEXT (current_function_decl);
778 fprintf (stderr, "In class `%s':\n",
779 lang_printable_name (last_error_function_context, 0));
780 }
781 if (last_error_function != current_function_decl)
782 {
783 if (file)
784 fprintf (stderr, "%s: ", file);
785
786 if (current_function_decl == NULL)
787 fprintf (stderr, "At top level:\n");
788 else
789 {
790 const char *name = lang_printable_name (current_function_decl, 2);
791 fprintf (stderr, "In %s `%s':\n",
792 (DECL_CONSTRUCTOR_P (current_function_decl) ? "constructor"
793 : "method"),
794 name);
795 }
796
797 last_error_function = current_function_decl;
798 }
799
800 }
801
802 /* Called to install the PRINT_ERROR_FUNCTION hook differently
803 according to LEVEL. LEVEL is 1 during early parsing, when function
804 prototypes aren't fully resolved. java_print_error_function is set
805 so it doesn't print incomplete function prototypes. When LEVEL is
806 2, function prototypes are fully resolved and can be printed when
807 reporting errors. */
808
lang_init_source(level)809 void lang_init_source (level)
810 int level;
811 {
812 inhibit_error_function_printing = (level == 1);
813 }
814
815 static void
java_init_options()816 java_init_options ()
817 {
818 flag_bounds_check = 1;
819 flag_exceptions = 1;
820 flag_non_call_exceptions = 1;
821
822 /* In Java floating point operations never trap. */
823 flag_trapping_math = 0;
824 }
825
826 static bool
java_can_use_bit_fields_p()827 java_can_use_bit_fields_p ()
828 {
829 /* The bit-field optimizations cause problems when generating class
830 files. */
831 return flag_emit_class_files ? false : true;
832 }
833
834 /* Post-switch processing. */
835 static bool
java_post_options()836 java_post_options ()
837 {
838 /* Use tree inlining if possible. Function instrumentation is only
839 done in the RTL level, so we disable tree inlining. */
840 if (! flag_instrument_function_entry_exit)
841 {
842 if (!flag_no_inline)
843 flag_no_inline = 1;
844 if (flag_inline_functions)
845 {
846 flag_inline_trees = 2;
847 flag_inline_functions = 0;
848 }
849 }
850
851 /* Initialize the compiler back end. */
852 return false;
853 }
854
855 /* Return either DECL or its known constant value (if it has one). */
856
857 tree
decl_constant_value(decl)858 decl_constant_value (decl)
859 tree decl;
860 {
861 if (/* Don't change a variable array bound or initial value to a constant
862 in a place where a variable is invalid. */
863 current_function_decl != 0
864 && ! TREE_THIS_VOLATILE (decl)
865 && TREE_READONLY (decl)
866 && DECL_INITIAL (decl) != 0
867 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
868 /* This is invalid if initial value is not constant.
869 If it has either a function call, a memory reference,
870 or a variable, then re-evaluating it could give different results. */
871 && TREE_CONSTANT (DECL_INITIAL (decl))
872 /* Check for cases where this is sub-optimal, even though valid. */
873 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
874 return DECL_INITIAL (decl);
875 return decl;
876 }
877
878 /* Walk the language specific tree nodes during inlining. */
879
880 static tree
java_tree_inlining_walk_subtrees(tp,subtrees,func,data,htab)881 java_tree_inlining_walk_subtrees (tp,subtrees,func,data,htab)
882 tree *tp ATTRIBUTE_UNUSED;
883 int *subtrees ATTRIBUTE_UNUSED;
884 walk_tree_fn func ATTRIBUTE_UNUSED;
885 void *data ATTRIBUTE_UNUSED;
886 void *htab ATTRIBUTE_UNUSED;
887 {
888 enum tree_code code;
889 tree result;
890
891 #define WALK_SUBTREE(NODE) \
892 do \
893 { \
894 result = walk_tree (&(NODE), func, data, htab); \
895 if (result) \
896 return result; \
897 } \
898 while (0)
899
900 tree t = *tp;
901 if (!t)
902 return NULL_TREE;
903
904 code = TREE_CODE (t);
905 switch (code)
906 {
907 case BLOCK:
908 if (BLOCK_EXPR_BODY (t))
909 {
910 tree *prev = &BLOCK_EXPR_BODY (*tp);
911 while (*prev)
912 {
913 WALK_SUBTREE (*prev);
914 prev = &TREE_CHAIN (*prev);
915 }
916 }
917 return NULL_TREE;
918 break;
919
920 default:
921 return NULL_TREE;
922 }
923 }
924
925 /* Called from unsafe_for_reeval. */
926 static int
java_unsafe_for_reeval(t)927 java_unsafe_for_reeval (t)
928 tree t;
929 {
930 switch (TREE_CODE (t))
931 {
932 case BLOCK:
933 /* Our expander tries to expand the variables twice. Boom. */
934 if (BLOCK_EXPR_DECLS (t) != NULL)
935 return 2;
936 return unsafe_for_reeval (BLOCK_EXPR_BODY (t));
937
938 default:
939 break;
940 }
941
942 return -1;
943 }
944
945 /* Every call to a static constructor has an associated boolean
946 variable which is in the outermost scope of the calling method.
947 This variable is used to avoid multiple calls to the static
948 constructor for each class.
949
950 It looks somthing like this:
951
952 foo ()
953 {
954 boolean dummy = OtherClass.is_initialized;
955
956 ...
957
958 if (! dummy)
959 OtherClass.initialize();
960
961 ... use OtherClass.data ...
962 }
963
964 Each of these boolean variables has an entry in the
965 DECL_FUNCTION_INIT_TEST_TABLE of a method. When inlining a method
966 we must merge the DECL_FUNCTION_INIT_TEST_TABLE from the function
967 being linlined and create the boolean variables in the outermost
968 scope of the method being inlined into. */
969
970 /* Create a mapping from a boolean variable in a method being inlined
971 to one in the scope of the method being inlined into. */
972
973 static int
merge_init_test_initialization(entry,x)974 merge_init_test_initialization (entry, x)
975 void * * entry;
976 void * x;
977 {
978 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
979 splay_tree decl_map = (splay_tree)x;
980 splay_tree_node n;
981 tree *init_test_decl;
982
983 /* See if we have remapped this declaration. If we haven't there's
984 a bug in the inliner. */
985 n = splay_tree_lookup (decl_map, (splay_tree_key) ite->value);
986 if (! n)
987 abort ();
988
989 /* Create a new entry for the class and its remapped boolean
990 variable. If we already have a mapping for this class we've
991 already initialized it, so don't overwrite the value. */
992 init_test_decl = java_treetreehash_new
993 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
994 if (!*init_test_decl)
995 *init_test_decl = (tree)n->value;
996
997 return true;
998 }
999
1000 /* Merge the DECL_FUNCTION_INIT_TEST_TABLE from the function we're
1001 inlining. */
1002
1003 void
java_inlining_merge_static_initializers(fn,decl_map)1004 java_inlining_merge_static_initializers (fn, decl_map)
1005 tree fn;
1006 void *decl_map;
1007 {
1008 htab_traverse
1009 (DECL_FUNCTION_INIT_TEST_TABLE (fn),
1010 merge_init_test_initialization, decl_map);
1011 }
1012
1013 /* Lookup a DECL_FUNCTION_INIT_TEST_TABLE entry in the method we're
1014 inlining into. If we already have a corresponding entry in that
1015 class we don't need to create another one, so we create a mapping
1016 from the variable in the inlined class to the corresponding
1017 pre-existing one. */
1018
1019 static int
inline_init_test_initialization(entry,x)1020 inline_init_test_initialization (entry, x)
1021 void * * entry;
1022 void * x;
1023 {
1024 struct treetreehash_entry *ite = (struct treetreehash_entry *) *entry;
1025 splay_tree decl_map = (splay_tree)x;
1026
1027 tree h = java_treetreehash_find
1028 (DECL_FUNCTION_INIT_TEST_TABLE (current_function_decl), ite->key);
1029 if (! h)
1030 return true;
1031
1032 splay_tree_insert (decl_map,
1033 (splay_tree_key) ite->value,
1034 (splay_tree_value) h);
1035
1036 return true;
1037 }
1038
1039 /* Look up the boolean variables in the DECL_FUNCTION_INIT_TEST_TABLE
1040 of a method being inlined. For each hone, if we already have a
1041 variable associated with the same class in the method being inlined
1042 into, create a new mapping for it. */
1043
1044 void
java_inlining_map_static_initializers(fn,decl_map)1045 java_inlining_map_static_initializers (fn, decl_map)
1046 tree fn;
1047 void *decl_map;
1048 {
1049 htab_traverse
1050 (DECL_FUNCTION_INIT_TEST_TABLE (fn),
1051 inline_init_test_initialization, decl_map);
1052 }
1053
1054 /* Avoid voluminous output for deep recursion of compound exprs. */
1055
1056 static void
dump_compound_expr(di,t)1057 dump_compound_expr (di, t)
1058 dump_info_p di;
1059 tree t;
1060 {
1061 int i;
1062
1063 for (i=0; i<2; i++)
1064 {
1065 switch (TREE_CODE (TREE_OPERAND (t, i)))
1066 {
1067 case COMPOUND_EXPR:
1068 dump_compound_expr (di, TREE_OPERAND (t, i));
1069 break;
1070
1071 case EXPR_WITH_FILE_LOCATION:
1072 {
1073 tree wfl_node = EXPR_WFL_NODE (TREE_OPERAND (t, i));
1074 dump_child ("expr", wfl_node);
1075 break;
1076 }
1077
1078 default:
1079 dump_child ("expr", TREE_OPERAND (t, i));
1080 }
1081 }
1082 }
1083
1084 static int
java_dump_tree(dump_info,t)1085 java_dump_tree (dump_info, t)
1086 void *dump_info;
1087 tree t;
1088 {
1089 enum tree_code code;
1090 dump_info_p di = (dump_info_p) dump_info;
1091
1092 /* Figure out what kind of node this is. */
1093 code = TREE_CODE (t);
1094
1095 switch (code)
1096 {
1097 case FUNCTION_DECL:
1098 dump_child ("args", DECL_ARGUMENTS (t));
1099 if (DECL_EXTERNAL (t))
1100 dump_string (di, "undefined");
1101 if (TREE_PUBLIC (t))
1102 dump_string (di, "extern");
1103 else
1104 dump_string (di, "static");
1105 if (DECL_LANG_SPECIFIC (t))
1106 dump_child ("body", DECL_FUNCTION_BODY (t));
1107 if (DECL_LANG_SPECIFIC (t) && !dump_flag (di, TDF_SLIM, t))
1108 dump_child ("inline body", DECL_SAVED_TREE (t));
1109 return 1;
1110
1111 case RETURN_EXPR:
1112 dump_child ("expr", TREE_OPERAND (t, 0));
1113 return 1;
1114
1115 case GOTO_EXPR:
1116 dump_child ("goto", TREE_OPERAND (t, 0));
1117 return 1;
1118
1119 case LABEL_EXPR:
1120 dump_child ("label", TREE_OPERAND (t, 0));
1121 return 1;
1122
1123 case LABELED_BLOCK_EXPR:
1124 dump_child ("label", TREE_OPERAND (t, 0));
1125 dump_child ("block", TREE_OPERAND (t, 1));
1126 return 1;
1127
1128 case EXIT_BLOCK_EXPR:
1129 dump_child ("block", TREE_OPERAND (t, 0));
1130 dump_child ("val", TREE_OPERAND (t, 1));
1131 return 1;
1132
1133 case BLOCK:
1134 if (BLOCK_EXPR_BODY (t))
1135 {
1136 tree local = BLOCK_VARS (t);
1137 while (local)
1138 {
1139 tree next = TREE_CHAIN (local);
1140 dump_child ("var", local);
1141 local = next;
1142 }
1143
1144 {
1145 tree block = BLOCK_EXPR_BODY (t);
1146 dump_child ("body", block);
1147 block = TREE_CHAIN (block);
1148 }
1149 }
1150 return 1;
1151
1152 case COMPOUND_EXPR:
1153 if (!dump_flag (di, TDF_SLIM, t))
1154 return 0;
1155 dump_compound_expr (di, t);
1156 return 1;
1157
1158 default:
1159 break;
1160 }
1161 return 0;
1162 }
1163
1164 /* Java calls can't, in general, be sibcalls because we need an
1165 accurate stack trace in order to guarantee correct operation of
1166 methods such as Class.forName(String) and
1167 SecurityManager.getClassContext(). */
1168
1169 static bool
java_decl_ok_for_sibcall(tree decl)1170 java_decl_ok_for_sibcall (tree decl)
1171 {
1172 return decl != NULL && DECL_CONTEXT (decl) == current_class;
1173 }
1174
1175 #include "gt-java-lang.h"
1176