1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2001-2009, Free Software Foundation, Inc. *
11 * GNAT is free software; you can redistribute it and/or modify it under *
12 * terms of the GNU General Public License as published by the Free Soft- *
13 * ware Foundation; either version 3, or (at your option) any later ver- *
14 * sion. GNAT is distributed in the hope that it will be useful, but WITH- *
15 * OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *
16 * or FITNESS FOR A PARTICULAR PURPOSE. *
18 * As a special exception under Section 7 of GPL version 3, you are granted *
19 * additional permissions described in the GCC Runtime Library Exception, *
20 * version 3.1, as published by the Free Software Foundation. *
22 * You should have received a copy of the GNU General Public License and *
23 * a copy of the GCC Runtime Library Exception along with this program; *
24 * see the files COPYING3 and COPYING.RUNTIME respectively. If not, see *
25 * <http://www.gnu.org/licenses/>. *
27 * GNAT was originally developed by the GNAT team at New York University. *
28 * Extensive contributions were provided by Ada Core Technologies Inc. *
30 ****************************************************************************/
38 #define ISDIGIT(c) isdigit(c)
39 #define PARMS(ARGS) ARGS
42 #include "adadecode.h"
44 static void add_verbose (const char *, char *);
45 static int has_prefix (const char *, const char *);
46 static int has_suffix (const char *, const char *);
48 /* This is a safe version of strcpy that can be used with overlapped
49 pointers. Does nothing if s2 <= s1. */
50 static void ostrcpy (char *s1, char *s2);
52 /* Set to nonzero if we have written any verbose info. */
53 static int verbose_info;
55 /* Add TEXT to end of ADA_NAME, putting a leading " (" or ", ", depending
58 static void add_verbose (const char *text, char *ada_name)
60 strcat (ada_name, verbose_info ? ", " : " (");
61 strcat (ada_name, text);
66 /* Returns 1 if NAME starts with PREFIX. */
69 has_prefix (const char *name, const char *prefix)
71 return strncmp (name, prefix, strlen (prefix)) == 0;
74 /* Returns 1 if NAME ends with SUFFIX. */
77 has_suffix (const char *name, const char *suffix)
79 int nlen = strlen (name);
80 int slen = strlen (suffix);
82 return nlen > slen && strncmp (name + nlen - slen, suffix, slen) == 0;
85 /* Safe overlapped pointers version of strcpy. */
88 ostrcpy (char *s1, char *s2)
92 while (*s2) *s1++ = *s2++;
97 /* This function will return the Ada name from the encoded form.
98 The Ada coding is done in exp_dbug.ads and this is the inverse function.
99 see exp_dbug.ads for full encoding rules, a short description is added
100 below. Right now only objects and routines are handled. Ada types are
101 stripped of their encodings.
103 CODED_NAME is the encoded entity name.
105 ADA_NAME is a pointer to a buffer, it will receive the Ada name. A safe
106 size for this buffer is: strlen (coded_name) * 2 + 60. (60 is for the
107 verbose information).
109 VERBOSE is nonzero if more information about the entity is to be
110 added at the end of the Ada name and surrounded by ( and ).
112 Coded name Ada name verbose info
113 ---------------------------------------------------------------------
114 _ada_xyz xyz library level
116 x__yTKB x.y task body
118 x__yX x.y body nested
119 x__yXb x.y body nested
121 x__y$2 x.y overloaded
122 x__y__3 x.y overloaded
144 __gnat_decode (const char *coded_name, char *ada_name, int verbose)
152 /* Check for library level subprogram. */
153 if (has_prefix (coded_name, "_ada_"))
155 strcpy (ada_name, coded_name + 5);
159 strcpy (ada_name, coded_name);
161 /* Check for the first triple underscore in the name. This indicates
162 that the name represents a type with encodings; in this case, we
163 need to strip the encodings. */
167 if ((encodings = (char *) strstr (ada_name, "___")) != NULL)
173 /* Check for task body. */
174 if (has_suffix (ada_name, "TKB"))
176 ada_name[strlen (ada_name) - 3] = '\0';
180 if (has_suffix (ada_name, "B"))
182 ada_name[strlen (ada_name) - 1] = '\0';
186 /* Check for body-nested entity: X[bn] */
187 if (has_suffix (ada_name, "X"))
189 ada_name[strlen (ada_name) - 1] = '\0';
193 if (has_suffix (ada_name, "Xb"))
195 ada_name[strlen (ada_name) - 2] = '\0';
199 if (has_suffix (ada_name, "Xn"))
201 ada_name[strlen (ada_name) - 2] = '\0';
205 /* Change instance of TK__ (object declared inside a task) to __. */
209 while ((tktoken = (char *) strstr (ada_name, "TK__")) != NULL)
211 ostrcpy (tktoken, tktoken + 2);
216 /* Check for overloading: name terminated by $nn or __nn. */
218 int len = strlen (ada_name);
222 while (ISDIGIT ((int) ada_name[(int) len - 1 - n_digits]))
225 /* Check if we have $ or __ before digits. */
226 if (ada_name[len - 1 - n_digits] == '$')
228 ada_name[len - 1 - n_digits] = '\0';
231 else if (ada_name[len - 1 - n_digits] == '_'
232 && ada_name[len - 1 - n_digits - 1] == '_')
234 ada_name[len - 1 - n_digits - 1] = '\0';
239 /* Change all "__" to ".". */
241 int len = strlen (ada_name);
246 if (ada_name[k] == '_' && ada_name[k+1] == '_')
249 ostrcpy (ada_name + k + 1, ada_name + k + 2);
256 /* Checks for operator name. */
258 const char *trans_table[][2]
259 = {{"Oabs", "\"abs\""}, {"Oand", "\"and\""}, {"Omod", "\"mod\""},
260 {"Onot", "\"not\""}, {"Oor", "\"or\""}, {"Orem", "\"rem\""},
261 {"Oxor", "\"xor\""}, {"Oeq", "\"=\""}, {"One", "\"/=\""},
262 {"Olt", "\"<\""}, {"Ole", "\"<=\""}, {"Ogt", "\">\""},
263 {"Oge", "\">=\""}, {"Oadd", "\"+\""}, {"Osubtract", "\"-\""},
264 {"Oconcat", "\"&\""}, {"Omultiply", "\"*\""}, {"Odivide", "\"/\""},
265 {"Oexpon", "\"**\""}, {NULL, NULL} };
272 if ((optoken = (char *) strstr (ada_name, trans_table[k][0])) != NULL)
274 int codedlen = strlen (trans_table[k][0]);
275 int oplen = strlen (trans_table[k][1]);
277 if (codedlen > oplen)
278 /* We shrink the space. */
279 ostrcpy (optoken, optoken + codedlen - oplen);
280 else if (oplen > codedlen)
282 /* We need more space. */
283 int len = strlen (ada_name);
284 int space = oplen - codedlen;
285 int num_to_move = &ada_name[len] - optoken;
288 for (t = 0; t < num_to_move; t++)
289 ada_name[len + space - t - 1] = ada_name[len - t - 1];
292 /* Write symbol in the space. */
293 strncpy (optoken, trans_table[k][1], oplen);
298 /* Check for table's ending. */
299 if (trans_table[k][0] == NULL)
304 /* If verbose mode is on, we add some information to the Ada name. */
308 add_verbose ("overloaded", ada_name);
311 add_verbose ("library level", ada_name);
314 add_verbose ("body nested", ada_name);
317 add_verbose ("in task", ada_name);
320 add_verbose ("task body", ada_name);
322 if (verbose_info == 1)
323 strcat (ada_name, ")");
328 ada_demangle (const char *coded_name)
332 __gnat_decode (coded_name, ada_name, 0);
333 return xstrdup (ada_name);
337 get_encoding (const char *coded_name, char *encoding)
339 char * dest_index = encoding;
344 /* The heuristics is the following: we assume that the first triple
345 underscore in an encoded name indicates the beginning of the
346 first encoding, and that subsequent triple underscores indicate
347 the next encodings. We assume that the encodings are always at the
348 end of encoded names. */
350 for (p = coded_name; *p != '\0'; p++)
361 dest_index = dest_index - 2;