OSDN Git Service

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