ipa-cp.c (ipcp_cloning_candidate_p): Use opt_for_fn.
[gcc.git] / gcc / java / jvgenmain.c
1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC 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 3, or (at your option)
9 any later version.
10
11 GCC 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 GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
19
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
23
24 /* Written by Per Bothner <bothner@cygnus.com> */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "obstack.h"
30 #include "jcf.h"
31 #include "tree.h"
32 #include "java-tree.h"
33 #include "intl.h"
34 #include "diagnostic.h"
35
36 static char * do_mangle_classname (const char *string);
37
38 struct obstack name_obstack;
39 struct obstack *mangle_obstack = &name_obstack;
40
41 static void usage (const char *) ATTRIBUTE_NORETURN;
42
43 static void
44 usage (const char *name)
45 {
46 fprintf (stderr, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"),
47 name);
48 exit (1);
49 }
50
51 int
52 main (int argc, char **argv)
53 {
54 char *classname, *p;
55 FILE *stream;
56 const char *mangled_classname;
57 int i, last_arg;
58 int indirect = 0;
59 char *prog_name = argv[0];
60
61 p = argv[0] + strlen (argv[0]);
62 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
63 --p;
64 progname = p;
65
66 xmalloc_set_program_name (progname);
67
68 /* Unlock the stdio streams. */
69 unlock_std_streams ();
70
71 gcc_init_libintl ();
72
73 diagnostic_initialize (global_dc, 0);
74
75 if (argc > 1 && ! strcmp (argv[1], "-findirect-dispatch"))
76 {
77 indirect = 1;
78 ++argv;
79 --argc;
80 }
81
82 if (argc < 2)
83 usage (prog_name);
84
85 for (i = 1; i < argc; ++i)
86 {
87 if (! strncmp (argv[i], "-D", 2))
88 {
89 /* Handled later. Check "-D XXX=YYY". */
90 if (argv[i][2] == '\0')
91 i++;
92 }
93 else
94 break;
95 }
96
97 if (i < argc - 2 || i == argc)
98 usage (prog_name);
99 last_arg = i;
100
101 classname = argv[i];
102
103 /* gcj always appends `main' to classname. We need to strip this here. */
104 p = strrchr (classname, 'm');
105 if (p == NULL || p == classname || strcmp (p, "main") != 0)
106 usage (prog_name);
107 else
108 *p = '\0';
109
110 gcc_obstack_init (mangle_obstack);
111 mangled_classname = do_mangle_classname (classname);
112
113 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
114 {
115 const char *outfile = argv[i + 1];
116 stream = fopen (outfile, "w");
117 if (stream == NULL)
118 {
119 fprintf (stderr, _("%s: Cannot open output file: %s\n"),
120 prog_name, outfile);
121 exit (1);
122 }
123 }
124 else
125 stream = stdout;
126
127 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
128 option. Process them appropriately. */
129 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
130 if (indirect)
131 fprintf (stream, "extern void JvRunMainName ();\n");
132 else
133 fprintf (stream, "extern void JvRunMain ();\n");
134 fprintf (stream, "static const char *props[] =\n{\n");
135 for (i = 1; i < last_arg; ++i)
136 {
137 const char *p;
138
139 if (strcmp (argv[i], "-D") == 0)
140 continue;
141
142 fprintf (stream, " \"");
143 for (p = argv[i]; *p; ++p)
144 {
145 if (! ISPRINT (*p))
146 fprintf (stream, "\\%o", *p);
147 else if (*p == '\\' || *p == '"')
148 fprintf (stream, "\\%c", *p);
149 else
150 putc (*p, stream);
151 }
152 fprintf (stream, "\",\n");
153 }
154 fprintf (stream, " 0\n};\n\n");
155
156 fprintf (stream, "int main (int argc, const char **argv)\n");
157 fprintf (stream, "{\n");
158 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
159 if (indirect)
160 fprintf (stream, " JvRunMainName (\"%s\", argc, argv);\n", classname);
161 else
162 {
163 fprintf (stream, " extern char %s;\n", mangled_classname);
164 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
165 }
166 fprintf (stream, "}\n");
167 if (stream != stdout && fclose (stream) != 0)
168 {
169 fprintf (stderr, _("%s: Failed to close output file %s\n"),
170 prog_name, argv[2]);
171 exit (1);
172 }
173 return 0;
174 }
175
176
177 static char *
178 do_mangle_classname (const char *string)
179 {
180 const char *ptr;
181 int count = 0;
182
183 obstack_grow (&name_obstack, "_ZN", 3);
184
185 for (ptr = string; *ptr; ptr++ )
186 {
187 if (*ptr == '.')
188 {
189 append_gpp_mangled_name (ptr - count, count);
190 count = 0;
191 }
192 else
193 count++;
194 }
195 append_gpp_mangled_name (&ptr [-count], count);
196 obstack_grow (mangle_obstack, "6class$E", strlen ("6class$E"));
197 obstack_1grow (mangle_obstack, '\0');
198 return XOBFINISH (mangle_obstack, char *);
199 }