OSDN Git Service

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