OSDN Git Service

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