* lang.opt (-freduced-reflection): New option.
[official-gcc.git] / gcc / java / jvgenmain.c
blobf3402798e55a6bfb4500728d59a6d83dc0b45139
1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC 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.
12 GCC 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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
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. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "obstack.h"
33 #include "jcf.h"
34 #include "tree.h"
35 #include "java-tree.h"
36 #include "intl.h"
38 static char * do_mangle_classname (const char *string);
40 struct obstack name_obstack;
41 struct obstack *mangle_obstack = &name_obstack;
43 static void usage (const char *) ATTRIBUTE_NORETURN;
45 static void
46 usage (const char *name)
48 fprintf (stderr, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"),
49 name);
50 exit (1);
53 int
54 main (int argc, char **argv)
56 char *classname, *p;
57 FILE *stream;
58 const char *mangled_classname;
59 int i, last_arg;
61 /* Unlock the stdio streams. */
62 unlock_std_streams ();
64 gcc_init_libintl ();
66 if (argc < 2)
67 usage (argv[0]);
69 for (i = 1; i < argc; ++i)
71 if (! strncmp (argv[i], "-D", 2))
73 /* Handled later. */
75 else
76 break;
79 if (i < argc - 2 || i == argc)
80 usage (argv[0]);
81 last_arg = i;
83 classname = argv[i];
85 /* gcj always appends `main' to classname. We need to strip this here. */
86 p = strrchr (classname, 'm');
87 if (p == NULL || p == classname || strcmp (p, "main") != 0)
88 usage (argv[0]);
89 else
90 *p = '\0';
92 gcc_obstack_init (mangle_obstack);
93 mangled_classname = do_mangle_classname (classname);
95 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
97 const char *outfile = argv[i + 1];
98 stream = fopen (outfile, "w");
99 if (stream == NULL)
101 fprintf (stderr, _("%s: Cannot open output file: %s\n"),
102 argv[0], outfile);
103 exit (1);
106 else
107 stream = stdout;
109 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
110 option. Process them appropriately. */
111 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
112 fprintf (stream, "static const char *props[] =\n{\n");
113 for (i = 1; i < last_arg; ++i)
115 const char *p;
116 fprintf (stream, " \"");
117 for (p = &argv[i][2]; *p; ++p)
119 if (! ISPRINT (*p))
120 fprintf (stream, "\\%o", *p);
121 else if (*p == '\\' || *p == '"')
122 fprintf (stream, "\\%c", *p);
123 else
124 putc (*p, stream);
126 fprintf (stream, "\",\n");
128 fprintf (stream, " 0\n};\n\n");
130 fprintf (stream, "extern int %s;\n", mangled_classname);
131 fprintf (stream, "int main (int argc, const char **argv)\n");
132 fprintf (stream, "{\n");
133 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
134 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
135 fprintf (stream, "}\n");
136 if (stream != stdout && fclose (stream) != 0)
138 fprintf (stderr, _("%s: Failed to close output file %s\n"),
139 argv[0], argv[2]);
140 exit (1);
142 return 0;
146 static char *
147 do_mangle_classname (const char *string)
149 const char *ptr;
150 int count = 0;
152 obstack_grow (&name_obstack, "_ZN", 3);
154 for (ptr = string; *ptr; ptr++ )
156 if (ptr[0] == '.')
158 append_gpp_mangled_name (&ptr [-count], count);
159 count = 0;
161 else
162 count++;
164 append_gpp_mangled_name (&ptr [-count], count);
165 obstack_grow (mangle_obstack, "6class$E", 8);
166 obstack_1grow (mangle_obstack, '\0');
167 return obstack_finish (mangle_obstack);