1 /****************************************************************************
3 * GNAT COMPILER COMPONENTS *
7 * C Implementation File *
9 * Copyright (C) 2001-2003, 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 2, 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. See the GNU General Public License *
17 * for more details. You should have received a copy of the GNU General *
18 * Public License distributed with GNAT; see file COPYING. If not, write *
19 * to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, *
20 * MA 02111-1307, USA. *
22 * As a special exception, if you link this file with other files to *
23 * produce an executable, this file does not by itself cause the resulting *
24 * executable to be covered by the GNU General Public License. This except- *
25 * ion does not however invalidate any other reasons why the executable *
26 * file might be covered by the GNU Public License. *
28 * GNAT was originally developed by the GNAT team at New York University. *
29 * Extensive contributions were provided by Ada Core Technologies Inc. *
31 ****************************************************************************/
38 #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. There is no support
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 task body. */
162 if (has_suffix (ada_name, "TKB"))
164 ada_name[strlen (ada_name) - 3] = '\0';
168 if (has_suffix (ada_name, "B"))
170 ada_name[strlen (ada_name) - 1] = '\0';
174 /* Check for body-nested entity: X[bn] */
175 if (has_suffix (ada_name, "X"))
177 ada_name[strlen (ada_name) - 1] = '\0';
181 if (has_suffix (ada_name, "Xb"))
183 ada_name[strlen (ada_name) - 2] = '\0';
187 if (has_suffix (ada_name, "Xn"))
189 ada_name[strlen (ada_name) - 2] = '\0';
193 /* Change instance of TK__ (object declared inside a task) to __. */
197 while ((tktoken = (char *) strstr (ada_name, "TK__")) != NULL)
199 ostrcpy (tktoken, tktoken + 2);
204 /* Check for overloading: name terminated by $nn or __nn. */
206 int len = strlen (ada_name);
210 while (isdigit ((int) ada_name[(int) len - 1 - n_digits]))
213 /* Check if we have $ or __ before digits. */
214 if (ada_name[len - 1 - n_digits] == '$')
216 ada_name[len - 1 - n_digits] = '\0';
219 else if (ada_name[len - 1 - n_digits] == '_'
220 && ada_name[len - 1 - n_digits - 1] == '_')
222 ada_name[len - 1 - n_digits - 1] = '\0';
227 /* Change all "__" to ".". */
229 int len = strlen (ada_name);
234 if (ada_name[k] == '_' && ada_name[k+1] == '_')
237 ostrcpy (ada_name + k + 1, ada_name + k + 2);
244 /* Checks for operator name. */
246 const char *trans_table[][2]
247 = {{"Oabs", "\"abs\""}, {"Oand", "\"and\""}, {"Omod", "\"mod\""},
248 {"Onot", "\"not\""}, {"Oor", "\"or\""}, {"Orem", "\"rem\""},
249 {"Oxor", "\"xor\""}, {"Oeq", "\"=\""}, {"One", "\"/=\""},
250 {"Olt", "\"<\""}, {"Ole", "\"<=\""}, {"Ogt", "\">\""},
251 {"Oge", "\">=\""}, {"Oadd", "\"+\""}, {"Osubtract", "\"-\""},
252 {"Oconcat", "\"&\""}, {"Omultiply", "\"*\""}, {"Odivide", "\"/\""},
253 {"Oexpon", "\"**\""}, {NULL, NULL} };
260 if ((optoken = (char *) strstr (ada_name, trans_table[k][0])) != NULL)
262 int codedlen = strlen (trans_table[k][0]);
263 int oplen = strlen (trans_table[k][1]);
265 if (codedlen > oplen)
266 /* We shrink the space. */
267 ostrcpy (optoken, optoken + codedlen - oplen);
268 else if (oplen > codedlen)
270 /* We need more space. */
271 int len = strlen (ada_name);
272 int space = oplen - codedlen;
273 int num_to_move = &ada_name[len] - optoken;
276 for (t = 0; t < num_to_move; t++)
277 ada_name[len + space - t - 1] = ada_name[len - t - 1];
280 /* Write symbol in the space. */
281 strncpy (optoken, trans_table[k][1], oplen);
286 /* Check for table's ending. */
287 if (trans_table[k][0] == NULL)
292 /* If verbose mode is on, we add some information to the Ada name. */
296 add_verbose ("overloaded", ada_name);
299 add_verbose ("library level", ada_name);
302 add_verbose ("body nested", ada_name);
305 add_verbose ("in task", ada_name);
308 add_verbose ("task body", ada_name);
310 if (verbose_info == 1)
311 strcat (ada_name, ")");
316 ada_demangle (const char *coded_name)
320 __gnat_decode (coded_name, ada_name, 0);
321 return xstrdup (ada_name);