OSDN Git Service

* jvspec.c (jvgenmain_spec): Added `-fdollars-in-identifiers'.
[pf3gnuchains/gcc-fork.git] / gcc / java / jvgenmain.c
1 /* Program to generate "main" a Java(TM) class containing a main method.
2    Copyright (C) 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 /* Written by Per Bothner <bothner@cygnus.com> */
26
27 #include "config.h"
28 #include "system.h"
29 #include "obstack.h"
30 #include "gansidecl.h"
31 #include "jcf.h"
32 #include "tree.h"
33 #include "java-tree.h"
34
35 const char main_method_prefix[] = "main__";
36 const char main_method_suffix[] = "Pt6JArray1ZPQ34java4lang6String";
37 const char class_mangling_suffix[] = ".class$";
38
39 struct obstack name_obstack;
40
41 extern void error                       PARAMS ((const char *, ...))
42   ATTRIBUTE_PRINTF_1;
43
44 void
45 error VPARAMS ((const char *msgid, ...))
46 {
47 #ifndef ANSI_PROTOTYPES
48   const char *msgid;
49 #endif
50   va_list ap;
51  
52   VA_START (ap, msgid);
53  
54 #ifndef ANSI_PROTOTYPES
55   msgid = va_arg (ap, const char *);
56 #endif
57  
58   vfprintf (stderr, msgid, ap);
59   va_end (ap);
60 }
61
62 void
63 gcc_obstack_init (obstack)
64      struct obstack *obstack;
65 {
66   /* Let particular systems override the size of a chunk.  */
67 #ifndef OBSTACK_CHUNK_SIZE
68 #define OBSTACK_CHUNK_SIZE 0
69 #endif
70   /* Let them override the alloc and free routines too.  */
71 #ifndef OBSTACK_CHUNK_ALLOC
72 #define OBSTACK_CHUNK_ALLOC xmalloc
73 #endif
74 #ifndef OBSTACK_CHUNK_FREE
75 #define OBSTACK_CHUNK_FREE free
76 #endif
77   _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
78                   (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
79                   (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
80 }
81
82 static void usage (const char *) ATTRIBUTE_NORETURN;
83
84 static void
85 usage (const char *name)
86 {
87   fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAME [OUTFILE]\n", name);
88   exit (1);
89 }
90
91 int
92 main (int argc, const char **argv)
93 {
94   const char *classname;
95   FILE *stream;
96   const char *mangled_classname;
97   int i, last_arg;
98
99   if (argc < 2)
100     usage (argv[0]);
101
102   for (i = 1; i < argc; ++i)
103     {
104       if (! strncmp (argv[i], "-D", 2))
105         {
106           /* Handled later.  */
107         }
108       else
109         break;
110     }
111
112   if (i < argc - 2 || i == argc)
113     usage (argv[0]);
114   last_arg = i;
115
116   classname = argv[i];
117
118   gcc_obstack_init (&name_obstack);
119   append_gpp_mangled_classtype (&name_obstack, classname);
120   obstack_1grow (&name_obstack, '\0');
121   mangled_classname = obstack_finish (&name_obstack);
122
123   if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
124     {
125       const char *outfile = argv[i + 1];
126       stream = fopen (outfile, "w");
127       if (stream == NULL)
128         {
129           fprintf (stderr, "%s: Cannot open output file: %s\n",
130                    argv[0], outfile);
131           exit (1);
132         }
133     }
134   else
135     stream = stdout;
136
137   /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
138      option.  Process them appropriately.  */
139   fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
140   fprintf (stream, "static const char *props[] =\n{\n");
141   for (i = 1; i < last_arg; ++i)
142     {
143       const char *p;
144       fprintf (stream, "  \"");
145       for (p = &argv[i][2]; *p; ++p)
146         {
147           if (! isascii (*p))
148             fprintf (stream, "\\%o", *p);
149           else if (*p == '\\' || *p == '"')
150             fprintf (stream, "\\%c", *p);
151           else
152             putc (*p, stream);
153         }
154       fprintf (stream, "\",\n");
155     }
156   fprintf (stream, "  0\n};\n\n");
157
158   fprintf (stream, "extern int class __attribute__ ((alias (\"_%s%s\")));\n",
159            mangled_classname, class_mangling_suffix);
160   fprintf (stream, "int main (int argc, const char **argv)\n");
161   fprintf (stream, "{\n");
162   fprintf (stream, "   _Jv_Compiler_Properties = props;\n");
163   fprintf (stream, "   JvRunMain (&class, argc, argv);\n");
164   fprintf (stream, "}\n");
165   if (stream != stdout && fclose (stream) != 0)
166     {
167       fprintf (stderr, "%s: Failed to close output file %s\n",
168                argv[0], argv[2]);
169       exit (1);
170     }
171   return 0;
172 }