OSDN Git Service

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