OSDN Git Service

Uniquization of constants at the Tree level
[pf3gnuchains/gcc-fork.git] / gcc / java / jvgenmain.c
index 8f59192..b75f46b 100644 (file)
@@ -1,22 +1,22 @@
 /* Program to generate "main" a Java(TM) class containing a main method.
-   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
+   2007, 2008 Free Software Foundation, Inc.
 
-This file is part of GNU CC.
+This file is part of GCC.
 
-GNU CC is free software; you can redistribute it and/or modify
+GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
-GNU CC is distributed in the hope that it will be useful,
+GCC is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA. 
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>. 
 
 Java and all Java-based marks are trademarks or registered trademarks
 of Sun Microsystems, Inc. in the United States and other countries.
@@ -26,55 +26,53 @@ The Free Software Foundation is independent of Sun Microsystems, Inc.  */
 
 #include "config.h"
 #include "system.h"
+#include "coretypes.h"
+#include "tm.h"
 #include "obstack.h"
-#include "gansidecl.h"
 #include "jcf.h"
 #include "tree.h"
 #include "java-tree.h"
+#include "intl.h"
 
-static char * do_mangle_classname PARAMS ((const char *string));
+static char * do_mangle_classname (const char *string);
 
-struct obstack name_obstack;
-
-void
-gcc_obstack_init (obstack)
-     struct obstack *obstack;
-{
-  /* Let particular systems override the size of a chunk.  */
-#ifndef OBSTACK_CHUNK_SIZE
-#define OBSTACK_CHUNK_SIZE 0
-#endif
-  /* Let them override the alloc and free routines too.  */
-#ifndef OBSTACK_CHUNK_ALLOC
-#define OBSTACK_CHUNK_ALLOC xmalloc
-#endif
-#ifndef OBSTACK_CHUNK_FREE
-#define OBSTACK_CHUNK_FREE free
-#endif
-  _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
-                 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
-                 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
-}
+struct obstack  name_obstack;
+struct obstack *mangle_obstack = &name_obstack;
 
 static void usage (const char *) ATTRIBUTE_NORETURN;
 
 static void
 usage (const char *name)
 {
-  fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAME [OUTFILE]\n", name);
+  fprintf (stderr, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"),
+          name);
   exit (1);
 }
 
 int
-main (int argc, const char **argv)
+main (int argc, char **argv)
 {
-  const char *classname;
+  char *classname, *p;
   FILE *stream;
   const char *mangled_classname;
   int i, last_arg;
+  int indirect = 0;
+  char *prog_name = argv[0];
+
+  /* Unlock the stdio streams.  */
+  unlock_std_streams ();
+
+  gcc_init_libintl ();
+
+  if (argc > 1 && ! strcmp (argv[1], "-findirect-dispatch"))
+    {
+      indirect = 1;
+      ++argv;
+      --argc;
+    }
 
   if (argc < 2)
-    usage (argv[0]);
+    usage (prog_name);
 
   for (i = 1; i < argc; ++i)
     {
@@ -87,12 +85,19 @@ main (int argc, const char **argv)
     }
 
   if (i < argc - 2 || i == argc)
-    usage (argv[0]);
+    usage (prog_name);
   last_arg = i;
 
   classname = argv[i];
 
-  gcc_obstack_init (&name_obstack);
+  /* gcj always appends `main' to classname.  We need to strip this here.  */
+  p = strrchr (classname, 'm');
+  if (p == NULL || p == classname || strcmp (p, "main") != 0)
+    usage (prog_name);
+  else
+    *p = '\0';
+
+  gcc_obstack_init (mangle_obstack);
   mangled_classname = do_mangle_classname (classname);
 
   if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
@@ -101,8 +106,8 @@ main (int argc, const char **argv)
       stream = fopen (outfile, "w");
       if (stream == NULL)
        {
-         fprintf (stderr, "%s: Cannot open output file: %s\n",
-                  argv[0], outfile);
+         fprintf (stderr, _("%s: Cannot open output file: %s\n"),
+                  prog_name, outfile);
          exit (1);
        }
     }
@@ -130,16 +135,21 @@ main (int argc, const char **argv)
     }
   fprintf (stream, "  0\n};\n\n");
 
-  fprintf (stream, "extern int %s;\n", mangled_classname);
   fprintf (stream, "int main (int argc, const char **argv)\n");
   fprintf (stream, "{\n");
   fprintf (stream, "   _Jv_Compiler_Properties = props;\n");
-  fprintf (stream, "   JvRunMain (&%s, argc, argv);\n", mangled_classname);
+  if (indirect)
+    fprintf (stream, "   JvRunMainName (\"%s\", argc, argv);\n", classname);
+  else
+    {
+      fprintf (stream, "   extern char %s;\n", mangled_classname);
+      fprintf (stream, "   JvRunMain (&%s, argc, argv);\n", mangled_classname);
+    }
   fprintf (stream, "}\n");
   if (stream != stdout && fclose (stream) != 0)
     {
-      fprintf (stderr, "%s: Failed to close output file %s\n",
-              argv[0], argv[2]);
+      fprintf (stderr, _("%s: Failed to close output file %s\n"),
+              prog_name, argv[2]);
       exit (1);
     }
   return 0;
@@ -147,33 +157,25 @@ main (int argc, const char **argv)
 
 
 static char *
-do_mangle_classname (string)
-     const char *string;
+do_mangle_classname (const char *string)
 {
-  char *ptr;
+  const char *ptr;
   int count = 0;
 
-#define MANGLE_NAME()                                          \
-  {                                                            \
-    char buffer [128];                                         \
-    sprintf (buffer, "%d", count);                             \
-    obstack_grow (&name_obstack, buffer, strlen (buffer));     \
-    obstack_grow (&name_obstack, & ptr [-count], count);       \
-    count = 0;                                                 \
-  }
-
   obstack_grow (&name_obstack, "_ZN", 3);
 
-  for (ptr = (char *)string; *ptr; ptr++ )
+  for (ptr = string; *ptr; ptr++ )
     {
-      if (ptr[0] == '.')
+      if (*ptr == '.')
        {
-         MANGLE_NAME ();
+         append_gpp_mangled_name (ptr - count, count);
+         count = 0;
        }
       else
        count++;
     }
-  MANGLE_NAME ();
-  obstack_grow0 (&name_obstack, "6class$E", 8);
-  return obstack_finish (&name_obstack);
+  append_gpp_mangled_name (&ptr [-count], count);
+  obstack_grow (mangle_obstack, "6class$E", strlen ("6class$E"));
+  obstack_1grow (mangle_obstack, '\0');
+  return XOBFINISH (mangle_obstack, char *);
 }