OSDN Git Service

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