all remaining *.c *.h files from hp merge.
[platform/upstream/binutils.git] / gdb / demangle.c
1 /* Basic C++ demangling support for GDB.
2    Copyright 1991, 1992, 1996 Free Software Foundation, Inc.
3    Written by Fred Fish at Cygnus Support.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21
22 /*  This file contains support code for C++ demangling that is common
23     to a styles of demangling, and GDB specific. */
24
25 #include "defs.h"
26 #include "command.h"
27 #include "gdbcmd.h"
28 #include "demangle.h"
29 #include "gdb_string.h"
30
31 /* Select the default C++ demangling style to use.  The default is "auto",
32    which allows gdb to attempt to pick an appropriate demangling style for
33    the executable it has loaded.  It can be set to a specific style ("gnu",
34    "lucid", "arm", "hp", etc.) in which case gdb will never attempt to do auto
35    selection of the style unless you do an explicit "set demangle auto".
36    To select one of these as the default, set DEFAULT_DEMANGLING_STYLE in
37    the appropriate target configuration file. */
38
39 #ifndef DEFAULT_DEMANGLING_STYLE
40 # define DEFAULT_DEMANGLING_STYLE AUTO_DEMANGLING_STYLE_STRING
41 #endif
42
43 /* String name for the current demangling style.  Set by the "set demangling"
44    command, printed as part of the output by the "show demangling" command. */
45
46 static char *current_demangling_style_string;
47
48 /* List of supported demangling styles.  Contains the name of the style as
49    seen by the user, and the enum value that corresponds to that style. */
50    
51 static const struct demangler
52 {
53   char *demangling_style_name;
54   enum demangling_styles demangling_style;
55   char *demangling_style_doc;
56 } demanglers [] =
57 {
58   {AUTO_DEMANGLING_STYLE_STRING,
59      auto_demangling,
60      "Automatic selection based on executable"},
61   {GNU_DEMANGLING_STYLE_STRING,
62      gnu_demangling,
63      "GNU (g++) style demangling"},
64   {LUCID_DEMANGLING_STYLE_STRING,
65      lucid_demangling,
66      "Lucid (lcc) style demangling"},
67   {ARM_DEMANGLING_STYLE_STRING,
68      arm_demangling,
69      "ARM style demangling"},
70   {HP_DEMANGLING_STYLE_STRING,
71      hp_demangling,
72      "HP (aCC) style demangling"},
73   {EDG_DEMANGLING_STYLE_STRING,
74      edg_demangling,
75      "EDG style demangling"},
76   {NULL, unknown_demangling, NULL}
77 };
78
79 static void
80 set_demangling_command PARAMS ((char *, int, struct cmd_list_element *));
81
82 /* set current demangling style.  called by the "set demangling" command
83    after it has updated the current_demangling_style_string to match
84    what the user has entered.
85
86    if the user has entered a string that matches a known demangling style
87    name in the demanglers[] array then just leave the string alone and update
88    the current_demangling_style enum value to match.
89
90    if the user has entered a string that doesn't match, including an empty
91    string, then print a list of the currently known styles and restore
92    the current_demangling_style_string to match the current_demangling_style
93    enum value.
94
95    Note:  Assumes that current_demangling_style_string always points to
96    a malloc'd string, even if it is a null-string. */
97
98 static void
99 set_demangling_command (ignore, from_tty, c)
100      char *ignore;
101      int from_tty;
102      struct cmd_list_element *c;
103 {
104   const struct demangler *dem;
105
106   /*  First just try to match whatever style name the user supplied with
107       one of the known ones.  Don't bother special casing for an empty
108       name, we just treat it as any other style name that doesn't match.
109       If we match, update the current demangling style enum. */
110
111   for (dem = demanglers; dem -> demangling_style_name != NULL; dem++)
112     {
113       if (STREQ (current_demangling_style_string,
114                   dem -> demangling_style_name))
115         {
116           current_demangling_style = dem -> demangling_style;
117           break;
118         }
119     }
120
121   /* Check to see if we found a match.  If not, gripe about any non-empty
122      style name and supply a list of valid ones.  FIXME:  This should
123      probably be done with some sort of completion and with help. */
124
125   if (dem -> demangling_style_name == NULL)
126     {
127       if (*current_demangling_style_string != '\0')
128         {
129           printf_unfiltered ("Unknown demangling style `%s'.\n",
130                   current_demangling_style_string);
131         }
132       printf_unfiltered ("The currently understood settings are:\n\n");
133       for (dem = demanglers; dem -> demangling_style_name != NULL; dem++)
134         {
135           printf_unfiltered ("%-10s %s\n", dem -> demangling_style_name,
136                   dem -> demangling_style_doc);
137           if (dem -> demangling_style == current_demangling_style)
138             {
139               free (current_demangling_style_string);
140               current_demangling_style_string =
141                 savestring (dem -> demangling_style_name,
142                             strlen (dem -> demangling_style_name));
143             }
144         }
145       if (current_demangling_style == unknown_demangling)
146         {
147           /* This can happen during initialization if gdb is compiled with
148              a DEMANGLING_STYLE value that is unknown, so pick the first
149              one as the default. */
150           current_demangling_style = demanglers[0].demangling_style;
151           current_demangling_style_string =
152             savestring (demanglers[0].demangling_style_name,
153                         strlen (demanglers[0].demangling_style_name));
154           warning ("`%s' style demangling chosen as the default.\n",
155                    current_demangling_style_string);
156         }
157     }
158 }
159
160 /* Fake a "set demangling" command. */
161
162 void
163 set_demangling_style (style)
164      char *style;
165 {
166   if (current_demangling_style_string != NULL)
167     {
168       free (current_demangling_style_string);
169     }
170   current_demangling_style_string = savestring (style, strlen (style));
171   set_demangling_command ((char *) NULL, 0, (struct cmd_list_element *) NULL);
172 }
173
174 /* In order to allow a single demangler executable to demangle strings
175    using various common values of CPLUS_MARKER, as well as any specific
176    one set at compile time, we maintain a string containing all the
177    commonly used ones, and check to see if the marker we are looking for
178    is in that string.  CPLUS_MARKER is usually '$' on systems where the
179    assembler can deal with that.  Where the assembler can't, it's usually
180    '.' (but on many systems '.' is used for other things).  We put the
181    current defined CPLUS_MARKER first (which defaults to '$'), followed
182    by the next most common value, followed by an explicit '$' in case
183    the value of CPLUS_MARKER is not '$'.
184
185    We could avoid this if we could just get g++ to tell us what the actual
186    cplus marker character is as part of the debug information, perhaps by
187    ensuring that it is the character that terminates the gcc<n>_compiled
188    marker symbol (FIXME). */
189
190 static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
191
192 int
193 is_cplus_marker (c)
194      int c;
195 {
196   return c && strchr (cplus_markers, c) != NULL;
197 }
198
199 void
200 _initialize_demangler ()
201 {
202    struct cmd_list_element *set, *show;
203
204    set = add_set_cmd ("demangle-style", class_support, var_string_noescape,
205                       (char *) &current_demangling_style_string,
206                       "Set the current C++ demangling style.\n\
207 Use `set demangle-style' without arguments for a list of demangling styles.",
208                       &setlist);
209    show = add_show_from_set (set, &showlist);
210    set -> function.sfunc = set_demangling_command;
211
212    /* Set the default demangling style chosen at compilation time. */
213    set_demangling_style (DEFAULT_DEMANGLING_STYLE);
214    set_cplus_marker_for_demangling (CPLUS_MARKER);
215 }