OSDN Git Service

* parser.c (cp_parser_class_specifier): Set class location to that
[pf3gnuchains/gcc-fork.git] / gcc / ada / adadecode.c
index cafd1c3..43f14f1 100644 (file)
@@ -2,50 +2,65 @@
  *                                                                          *
  *                         GNAT COMPILER COMPONENTS                         *
  *                                                                          *
- *                             G N A T D E C O                              *
- *                                                                          *
- *                            $Revision$
+ *                            A D A D E C O D E                             *
  *                                                                          *
  *                          C Implementation File                           *
  *                                                                          *
- *           Copyright (C) 2001-2002, Free Software Foundation, Inc.        *
+ *           Copyright (C) 2001-2009, Free Software Foundation, Inc.        *
  *                                                                          *
  * GNAT is free software;  you can  redistribute it  and/or modify it under *
  * terms of the  GNU General Public License as published  by the Free Soft- *
- * ware  Foundation;  either version 2,  or (at your option) any later ver- *
+ * ware  Foundation;  either version 3,  or (at your option) any later ver- *
  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
  * OUT 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  distributed with GNAT;  see file COPYING.  If not, write *
- * to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, *
- * MA 02111-1307, USA.                                                      *
+ * or FITNESS FOR A PARTICULAR PURPOSE.                                     *
+ *                                                                          *
+ * As a special exception under Section 7 of GPL version 3, you are granted *
+ * additional permissions described in the GCC Runtime Library Exception,   *
+ * version 3.1, as published by the Free Software Foundation.               *
  *                                                                          *
- * As a  special  exception,  if you  link  this file  with other  files to *
- * produce an executable,  this file does not by itself cause the resulting *
- * executable to be covered by the GNU General Public License. This except- *
- * ion does not  however invalidate  any other reasons  why the  executable *
- * file might be covered by the  GNU Public License.                        *
+ * You should have received a copy of the GNU General Public License and    *
+ * a copy of the GCC Runtime Library Exception along with this program;     *
+ * see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    *
+ * <http://www.gnu.org/licenses/>.                                          *
  *                                                                          *
  * GNAT was originally developed  by the GNAT team at  New York University. *
- * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
+ * Extensive contributions were provided by Ada Core Technologies Inc.      *
  *                                                                          *
  ****************************************************************************/
 
-#ifdef IN_GCC
+
+#if defined(IN_RTS)
+#include "tconfig.h"
+#include "tsystem.h"
+#elif defined(IN_GCC)
 #include "config.h"
 #include "system.h"
-#else
+#endif
+
+#include <string.h>
 #include <stdio.h>
+#include <ctype.h>
+
+#include "adaint.h"
+
+#ifndef ISDIGIT
+#define ISDIGIT(c) isdigit(c)
+#endif
+
+#ifndef PARMS
 #define PARMS(ARGS) ARGS
 #endif
 
-#include "ctype.h"
 #include "adadecode.h"
 
-static void add_verbose        PARAMS ((const char *, char *));
-static int has_prefix  PARAMS ((char *, const char *));
-static int has_suffix  PARAMS ((char *, const char *));
+static void add_verbose (const char *, char *);
+static int has_prefix (const char *, const char *);
+static int has_suffix (const char *, const char *);
+
+/* This is a safe version of strcpy that can be used with overlapped
+   pointers. Does nothing if s2 <= s1.  */
+static void ostrcpy (char *s1, char *s2);
 
 /* Set to nonzero if we have written any verbose info.  */
 static int verbose_info;
@@ -53,9 +68,7 @@ static int verbose_info;
 /* Add TEXT to end of ADA_NAME, putting a leading " (" or ", ", depending
    on VERBOSE_INFO.  */
 
-static void add_verbose (text, ada_name)
-     const char *text;
-     char *ada_name;
+static void add_verbose (const char *text, char *ada_name)
 {
   strcat (ada_name, verbose_info ? ", " : " (");
   strcat (ada_name, text);
@@ -66,9 +79,7 @@ static void add_verbose (text, ada_name)
 /* Returns 1 if NAME starts with PREFIX.  */
 
 static int
-has_prefix (name, prefix)
-     char *name;
-     const char *prefix;
+has_prefix (const char *name, const char *prefix)
 {
   return strncmp (name, prefix, strlen (prefix)) == 0;
 }
@@ -76,9 +87,7 @@ has_prefix (name, prefix)
 /* Returns 1 if NAME ends with SUFFIX.  */
 
 static int
-has_suffix (name, suffix)
-     char *name;
-     const char *suffix;
+has_suffix (const char *name, const char *suffix)
 {
   int nlen = strlen (name);
   int slen = strlen (suffix);
@@ -86,11 +95,23 @@ has_suffix (name, suffix)
   return nlen > slen && strncmp (name + nlen - slen, suffix, slen) == 0;
 }
 
+/* Safe overlapped pointers version of strcpy.  */
+
+static void
+ostrcpy (char *s1, char *s2)
+{
+  if (s2 > s1)
+    {
+      while (*s2) *s1++ = *s2++;
+      *s1 = '\0';
+    }
+}
+
 /* This function will return the Ada name from the encoded form.
    The Ada coding is done in exp_dbug.ads and this is the inverse function.
    see exp_dbug.ads for full encoding rules, a short description is added
-   below. Right now only objects and routines are handled. There is no support
-   for Ada types.
+   below. Right now only objects and routines are handled. Ada types are
+   stripped of their encodings.
 
    CODED_NAME is the encoded entity name.
 
@@ -133,10 +154,7 @@ has_suffix (name, suffix)
   x__Oexpon               "**"     */
 
 void
-__gnat_decode (coded_name, ada_name, verbose)
-     const char *coded_name;
-     char *ada_name;
-     int verbose;
+__gnat_decode (const char *coded_name, char *ada_name, int verbose)
 {
   int lib_subprog = 0;
   int overloaded = 0;
@@ -144,16 +162,26 @@ __gnat_decode (coded_name, ada_name, verbose)
   int in_task = 0;
   int body_nested = 0;
 
-  /* Copy the coded name into the ada name string, the rest of the code will
-     just replace or add characters into the ada_name.  */
-  strcpy (ada_name, coded_name);
-
   /* Check for library level subprogram.  */
-  if (has_prefix (ada_name, "_ada_"))
+  if (has_prefix (coded_name, "_ada_"))
     {
-      strcpy (ada_name, ada_name + 5);
+      strcpy (ada_name, coded_name + 5);
       lib_subprog = 1;
     }
+  else
+    strcpy (ada_name, coded_name);
+
+  /* Check for the first triple underscore in the name. This indicates
+     that the name represents a type with encodings; in this case, we
+     need to strip the encodings.  */
+  {
+    char *encodings;
+
+    if ((encodings = (char *) strstr (ada_name, "___")) != NULL)
+      {
+       *encodings = '\0';
+      }
+  }
 
   /* Check for task body.  */
   if (has_suffix (ada_name, "TKB"))
@@ -193,7 +221,7 @@ __gnat_decode (coded_name, ada_name, verbose)
 
     while ((tktoken = (char *) strstr (ada_name, "TK__")) != NULL)
       {
-       strcpy (tktoken, tktoken + 2);
+       ostrcpy (tktoken, tktoken + 2);
        in_task = 1;
       }
   }
@@ -204,7 +232,7 @@ __gnat_decode (coded_name, ada_name, verbose)
     int n_digits = 0;
 
     if (len > 1)
-      while (isdigit ((int) ada_name[(int) len - 1 - n_digits]))
+      while (ISDIGIT ((int) ada_name[(int) len - 1 - n_digits]))
        n_digits++;
 
     /* Check if we have $ or __ before digits.  */
@@ -221,6 +249,21 @@ __gnat_decode (coded_name, ada_name, verbose)
       }
   }
 
+  /* Check for nested subprogram ending in .nnnn and strip suffix. */
+  {
+    int last = strlen (ada_name) - 1;
+
+    while (ISDIGIT (ada_name[last]) && last > 0)
+      {
+        last--;
+      }
+
+    if (ada_name[last] == '.')
+      {
+        ada_name[last] = (char) 0;
+      }
+  }
+
   /* Change all "__" to ".". */
   {
     int len = strlen (ada_name);
@@ -231,7 +274,7 @@ __gnat_decode (coded_name, ada_name, verbose)
        if (ada_name[k] == '_' && ada_name[k+1] == '_')
          {
            ada_name[k] = '.';
-           strcpy (ada_name + k + 1, ada_name + k + 2);
+           ostrcpy (ada_name + k + 1, ada_name + k + 2);
            len = len - 1;
          }
        k++;
@@ -261,7 +304,7 @@ __gnat_decode (coded_name, ada_name, verbose)
 
            if (codedlen > oplen)
              /* We shrink the space.  */
-             strcpy (optoken, optoken + codedlen - oplen);
+             ostrcpy (optoken, optoken + codedlen - oplen);
            else if (oplen > codedlen)
              {
                /* We need more space.  */
@@ -287,7 +330,7 @@ __gnat_decode (coded_name, ada_name, verbose)
   }
 
   /* If verbose mode is on, we add some information to the Ada name.  */
-  if (verbose) 
+  if (verbose)
     {
       if (overloaded)
        add_verbose ("overloaded", ada_name);
@@ -309,17 +352,53 @@ __gnat_decode (coded_name, ada_name, verbose)
     }
 }
 
+#ifdef IN_GCC
 char *
-ada_demangle (coded_name)
-     const char *coded_name;
+ada_demangle (const char *coded_name)
 {
   char ada_name[2048];
-  char *result;
 
   __gnat_decode (coded_name, ada_name, 0);
+  return xstrdup (ada_name);
+}
+#endif
 
-  result = (char *) xmalloc (strlen (ada_name) + 1);
-  strcpy (result, ada_name);
+void
+get_encoding (const char *coded_name, char *encoding)
+{
+  char * dest_index = encoding;
+  const char *p;
+  int found = 0;
+  int count = 0;
+
+  /* The heuristics is the following: we assume that the first triple
+     underscore in an encoded name indicates the beginning of the
+     first encoding, and that subsequent triple underscores indicate
+     the next encodings. We assume that the encodings are always at the
+     end of encoded names.  */
+
+  for (p = coded_name; *p != '\0'; p++)
+    {
+      if (*p != '_')
+       count = 0;
+      else
+       if (++count == 3)
+         {
+           count = 0;
+
+           if (found)
+             {
+               dest_index = dest_index - 2;
+               *dest_index++ = ':';
+             }
+
+           p++;
+           found = 1;
+         }
+
+      if (found)
+       *dest_index++ = *p;
+    }
 
-  return result;
+  *dest_index = '\0';
 }