OSDN Git Service

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