Commit | Line | Data |
---|---|---|
de17c821 | 1 | /* Helper routines for C++ support in GDB. |
4c38e0a4 | 2 | Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010 |
9b254dd1 | 3 | Free Software Foundation, Inc. |
de17c821 DJ |
4 | |
5 | Contributed by MontaVista Software. | |
6 | ||
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
de17c821 DJ |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
de17c821 DJ |
21 | |
22 | #include "defs.h" | |
23 | #include "cp-support.h" | |
24 | #include "gdb_string.h" | |
25 | #include "demangle.h" | |
9219021c DC |
26 | #include "gdb_assert.h" |
27 | #include "gdbcmd.h" | |
b6429628 DC |
28 | #include "dictionary.h" |
29 | #include "objfiles.h" | |
30 | #include "frame.h" | |
31 | #include "symtab.h" | |
32 | #include "block.h" | |
b2a7f303 | 33 | #include "complaints.h" |
362ff856 | 34 | #include "gdbtypes.h" |
12907978 KS |
35 | #include "exceptions.h" |
36 | #include "expression.h" | |
37 | #include "value.h" | |
b2a7f303 | 38 | |
f88e9fd3 DJ |
39 | #include "safe-ctype.h" |
40 | ||
ccefe4c4 TT |
41 | #include "psymtab.h" |
42 | ||
fb4c6eba DJ |
43 | #define d_left(dc) (dc)->u.s_binary.left |
44 | #define d_right(dc) (dc)->u.s_binary.right | |
b2a7f303 | 45 | |
fb4c6eba | 46 | /* Functions related to demangled name parsing. */ |
b2a7f303 DC |
47 | |
48 | static unsigned int cp_find_first_component_aux (const char *name, | |
49 | int permissive); | |
50 | ||
51 | static void demangled_name_complaint (const char *name); | |
b6429628 DC |
52 | |
53 | /* Functions/variables related to overload resolution. */ | |
54 | ||
7322dca9 | 55 | static int sym_return_val_size = -1; |
b6429628 DC |
56 | static int sym_return_val_index; |
57 | static struct symbol **sym_return_val; | |
58 | ||
8d577d32 DC |
59 | static void overload_list_add_symbol (struct symbol *sym, |
60 | const char *oload_name); | |
61 | ||
62 | static void make_symbol_overload_list_using (const char *func_name, | |
63 | const char *namespace); | |
64 | ||
65 | static void make_symbol_overload_list_qualified (const char *func_name); | |
66 | ||
9219021c DC |
67 | /* The list of "maint cplus" commands. */ |
68 | ||
5c4e30ca | 69 | struct cmd_list_element *maint_cplus_cmd_list = NULL; |
9219021c DC |
70 | |
71 | /* The actual commands. */ | |
72 | ||
73 | static void maint_cplus_command (char *arg, int from_tty); | |
74 | static void first_component_command (char *arg, int from_tty); | |
75 | ||
12907978 KS |
76 | /* Operator validation. |
77 | NOTE: Multi-byte operators (usually the assignment variety operator) | |
78 | must appear before the single byte version, i.e., "+=" before "+". */ | |
79 | static const char *operator_tokens[] = | |
80 | { | |
81 | "++", "+=", "+", "->*", "->", "--", "-=", "-", "*=", "*", "/=", "/", | |
82 | "%=", "%", "!=", "==", "!", "&&", "<<=", "<<", ">>=", ">>", | |
83 | "<=", "<", ">=", ">", "~", "&=", "&", "|=", "||", "|", "^=", "^", | |
84 | "=", "()", "[]", ",", "new", "delete" | |
85 | /* new[] and delete[] require special whitespace handling */ | |
86 | }; | |
87 | ||
f88e9fd3 DJ |
88 | /* Return 1 if STRING is clearly already in canonical form. This |
89 | function is conservative; things which it does not recognize are | |
90 | assumed to be non-canonical, and the parser will sort them out | |
91 | afterwards. This speeds up the critical path for alphanumeric | |
92 | identifiers. */ | |
93 | ||
94 | static int | |
95 | cp_already_canonical (const char *string) | |
96 | { | |
97 | /* Identifier start character [a-zA-Z_]. */ | |
98 | if (!ISIDST (string[0])) | |
99 | return 0; | |
100 | ||
101 | /* These are the only two identifiers which canonicalize to other | |
102 | than themselves or an error: unsigned -> unsigned int and | |
103 | signed -> int. */ | |
104 | if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0) | |
105 | return 0; | |
106 | else if (string[0] == 's' && strcmp (&string[1], "igned") == 0) | |
107 | return 0; | |
108 | ||
109 | /* Identifier character [a-zA-Z0-9_]. */ | |
110 | while (ISIDNUM (string[1])) | |
111 | string++; | |
112 | ||
113 | if (string[1] == '\0') | |
114 | return 1; | |
115 | else | |
116 | return 0; | |
117 | } | |
9219021c | 118 | |
f88e9fd3 DJ |
119 | /* Parse STRING and convert it to canonical form. If parsing fails, |
120 | or if STRING is already canonical, return NULL. Otherwise return | |
121 | the canonical form. The return value is allocated via xmalloc. */ | |
9219021c | 122 | |
fb4c6eba DJ |
123 | char * |
124 | cp_canonicalize_string (const char *string) | |
125 | { | |
fb4c6eba | 126 | struct demangle_component *ret_comp; |
f88e9fd3 | 127 | unsigned int estimated_len; |
fb4c6eba | 128 | char *ret; |
9219021c | 129 | |
f88e9fd3 DJ |
130 | if (cp_already_canonical (string)) |
131 | return NULL; | |
9219021c | 132 | |
f88e9fd3 | 133 | ret_comp = cp_demangled_name_to_comp (string, NULL); |
fb4c6eba DJ |
134 | if (ret_comp == NULL) |
135 | return NULL; | |
9219021c | 136 | |
f88e9fd3 DJ |
137 | estimated_len = strlen (string) * 2; |
138 | ret = cp_comp_to_string (ret_comp, estimated_len); | |
9219021c | 139 | |
f88e9fd3 DJ |
140 | if (strcmp (string, ret) == 0) |
141 | { | |
142 | xfree (ret); | |
143 | return NULL; | |
144 | } | |
de17c821 | 145 | |
fb4c6eba DJ |
146 | return ret; |
147 | } | |
de17c821 | 148 | |
fb4c6eba DJ |
149 | /* Convert a mangled name to a demangle_component tree. *MEMORY is set to the |
150 | block of used memory that should be freed when finished with the tree. | |
151 | DEMANGLED_P is set to the char * that should be freed when finished with | |
152 | the tree, or NULL if none was needed. OPTIONS will be passed to the | |
153 | demangler. */ | |
de17c821 | 154 | |
fb4c6eba DJ |
155 | static struct demangle_component * |
156 | mangled_name_to_comp (const char *mangled_name, int options, | |
157 | void **memory, char **demangled_p) | |
de17c821 | 158 | { |
fb4c6eba DJ |
159 | struct demangle_component *ret; |
160 | char *demangled_name; | |
de17c821 | 161 | |
fb4c6eba DJ |
162 | /* If it looks like a v3 mangled name, then try to go directly |
163 | to trees. */ | |
164 | if (mangled_name[0] == '_' && mangled_name[1] == 'Z') | |
de17c821 | 165 | { |
fb4c6eba DJ |
166 | ret = cplus_demangle_v3_components (mangled_name, options, memory); |
167 | if (ret) | |
168 | { | |
169 | *demangled_p = NULL; | |
170 | return ret; | |
171 | } | |
de17c821 DJ |
172 | } |
173 | ||
fb4c6eba DJ |
174 | /* If it doesn't, or if that failed, then try to demangle the name. */ |
175 | demangled_name = cplus_demangle (mangled_name, options); | |
176 | if (demangled_name == NULL) | |
177 | return NULL; | |
178 | ||
179 | /* If we could demangle the name, parse it to build the component tree. */ | |
f88e9fd3 | 180 | ret = cp_demangled_name_to_comp (demangled_name, NULL); |
de17c821 | 181 | |
fb4c6eba DJ |
182 | if (ret == NULL) |
183 | { | |
6c761d9c | 184 | xfree (demangled_name); |
fb4c6eba DJ |
185 | return NULL; |
186 | } | |
de17c821 | 187 | |
fb4c6eba DJ |
188 | *demangled_p = demangled_name; |
189 | return ret; | |
de17c821 DJ |
190 | } |
191 | ||
192 | /* Return the name of the class containing method PHYSNAME. */ | |
193 | ||
194 | char * | |
31c27f77 | 195 | cp_class_name_from_physname (const char *physname) |
de17c821 | 196 | { |
de237128 | 197 | void *storage = NULL; |
fb4c6eba | 198 | char *demangled_name = NULL, *ret; |
5e5100cb | 199 | struct demangle_component *ret_comp, *prev_comp, *cur_comp; |
fb4c6eba DJ |
200 | int done; |
201 | ||
202 | ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage, | |
203 | &demangled_name); | |
204 | if (ret_comp == NULL) | |
de17c821 DJ |
205 | return NULL; |
206 | ||
fb4c6eba | 207 | done = 0; |
5e5100cb DJ |
208 | |
209 | /* First strip off any qualifiers, if we have a function or method. */ | |
fb4c6eba DJ |
210 | while (!done) |
211 | switch (ret_comp->type) | |
212 | { | |
fb4c6eba DJ |
213 | case DEMANGLE_COMPONENT_CONST: |
214 | case DEMANGLE_COMPONENT_RESTRICT: | |
215 | case DEMANGLE_COMPONENT_VOLATILE: | |
216 | case DEMANGLE_COMPONENT_CONST_THIS: | |
217 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
218 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
219 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
fb4c6eba DJ |
220 | ret_comp = d_left (ret_comp); |
221 | break; | |
5e5100cb DJ |
222 | default: |
223 | done = 1; | |
224 | break; | |
225 | } | |
226 | ||
227 | /* If what we have now is a function, discard the argument list. */ | |
228 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) | |
229 | ret_comp = d_left (ret_comp); | |
230 | ||
231 | /* If what we have now is a template, strip off the template | |
232 | arguments. The left subtree may be a qualified name. */ | |
233 | if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE) | |
234 | ret_comp = d_left (ret_comp); | |
235 | ||
236 | /* What we have now should be a name, possibly qualified. Additional | |
237 | qualifiers could live in the left subtree or the right subtree. Find | |
238 | the last piece. */ | |
239 | done = 0; | |
240 | prev_comp = NULL; | |
241 | cur_comp = ret_comp; | |
242 | while (!done) | |
243 | switch (cur_comp->type) | |
244 | { | |
245 | case DEMANGLE_COMPONENT_QUAL_NAME: | |
246 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
247 | prev_comp = cur_comp; | |
248 | cur_comp = d_right (cur_comp); | |
249 | break; | |
fb4c6eba | 250 | case DEMANGLE_COMPONENT_TEMPLATE: |
5e5100cb | 251 | case DEMANGLE_COMPONENT_NAME: |
fb4c6eba DJ |
252 | case DEMANGLE_COMPONENT_CTOR: |
253 | case DEMANGLE_COMPONENT_DTOR: | |
254 | case DEMANGLE_COMPONENT_OPERATOR: | |
255 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: | |
256 | done = 1; | |
257 | break; | |
258 | default: | |
259 | done = 1; | |
5e5100cb | 260 | cur_comp = NULL; |
fb4c6eba DJ |
261 | break; |
262 | } | |
263 | ||
264 | ret = NULL; | |
5e5100cb | 265 | if (cur_comp != NULL && prev_comp != NULL) |
de17c821 | 266 | { |
5e5100cb | 267 | /* We want to discard the rightmost child of PREV_COMP. */ |
fb4c6eba DJ |
268 | *prev_comp = *d_left (prev_comp); |
269 | /* The ten is completely arbitrary; we don't have a good estimate. */ | |
5e5100cb | 270 | ret = cp_comp_to_string (ret_comp, 10); |
de17c821 DJ |
271 | } |
272 | ||
fb4c6eba DJ |
273 | xfree (storage); |
274 | if (demangled_name) | |
275 | xfree (demangled_name); | |
de17c821 DJ |
276 | return ret; |
277 | } | |
278 | ||
5e5100cb DJ |
279 | /* Return the child of COMP which is the basename of a method, variable, |
280 | et cetera. All scope qualifiers are discarded, but template arguments | |
281 | will be included. The component tree may be modified. */ | |
de17c821 | 282 | |
5e5100cb DJ |
283 | static struct demangle_component * |
284 | unqualified_name_from_comp (struct demangle_component *comp) | |
de17c821 | 285 | { |
5e5100cb | 286 | struct demangle_component *ret_comp = comp, *last_template; |
fb4c6eba DJ |
287 | int done; |
288 | ||
fb4c6eba | 289 | done = 0; |
5e5100cb | 290 | last_template = NULL; |
fb4c6eba DJ |
291 | while (!done) |
292 | switch (ret_comp->type) | |
293 | { | |
294 | case DEMANGLE_COMPONENT_QUAL_NAME: | |
295 | case DEMANGLE_COMPONENT_LOCAL_NAME: | |
fb4c6eba DJ |
296 | ret_comp = d_right (ret_comp); |
297 | break; | |
5e5100cb DJ |
298 | case DEMANGLE_COMPONENT_TYPED_NAME: |
299 | ret_comp = d_left (ret_comp); | |
300 | break; | |
301 | case DEMANGLE_COMPONENT_TEMPLATE: | |
302 | gdb_assert (last_template == NULL); | |
303 | last_template = ret_comp; | |
304 | ret_comp = d_left (ret_comp); | |
305 | break; | |
fb4c6eba DJ |
306 | case DEMANGLE_COMPONENT_CONST: |
307 | case DEMANGLE_COMPONENT_RESTRICT: | |
308 | case DEMANGLE_COMPONENT_VOLATILE: | |
309 | case DEMANGLE_COMPONENT_CONST_THIS: | |
310 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
311 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
312 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
313 | ret_comp = d_left (ret_comp); | |
314 | break; | |
315 | case DEMANGLE_COMPONENT_NAME: | |
fb4c6eba DJ |
316 | case DEMANGLE_COMPONENT_CTOR: |
317 | case DEMANGLE_COMPONENT_DTOR: | |
318 | case DEMANGLE_COMPONENT_OPERATOR: | |
319 | case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: | |
320 | done = 1; | |
321 | break; | |
322 | default: | |
5e5100cb | 323 | return NULL; |
fb4c6eba DJ |
324 | break; |
325 | } | |
326 | ||
5e5100cb DJ |
327 | if (last_template) |
328 | { | |
329 | d_left (last_template) = ret_comp; | |
330 | return last_template; | |
331 | } | |
332 | ||
333 | return ret_comp; | |
334 | } | |
335 | ||
336 | /* Return the name of the method whose linkage name is PHYSNAME. */ | |
337 | ||
338 | char * | |
339 | method_name_from_physname (const char *physname) | |
340 | { | |
de237128 | 341 | void *storage = NULL; |
5e5100cb DJ |
342 | char *demangled_name = NULL, *ret; |
343 | struct demangle_component *ret_comp; | |
5e5100cb DJ |
344 | |
345 | ret_comp = mangled_name_to_comp (physname, DMGL_ANSI, &storage, | |
346 | &demangled_name); | |
347 | if (ret_comp == NULL) | |
348 | return NULL; | |
349 | ||
350 | ret_comp = unqualified_name_from_comp (ret_comp); | |
351 | ||
fb4c6eba DJ |
352 | ret = NULL; |
353 | if (ret_comp != NULL) | |
354 | /* The ten is completely arbitrary; we don't have a good estimate. */ | |
355 | ret = cp_comp_to_string (ret_comp, 10); | |
356 | ||
357 | xfree (storage); | |
358 | if (demangled_name) | |
359 | xfree (demangled_name); | |
360 | return ret; | |
361 | } | |
de17c821 | 362 | |
5e5100cb DJ |
363 | /* If FULL_NAME is the demangled name of a C++ function (including an |
364 | arg list, possibly including namespace/class qualifications), | |
365 | return a new string containing only the function name (without the | |
366 | arg list/class qualifications). Otherwise, return NULL. The | |
367 | caller is responsible for freeing the memory in question. */ | |
368 | ||
369 | char * | |
370 | cp_func_name (const char *full_name) | |
371 | { | |
5e5100cb DJ |
372 | char *ret; |
373 | struct demangle_component *ret_comp; | |
5e5100cb | 374 | |
f88e9fd3 | 375 | ret_comp = cp_demangled_name_to_comp (full_name, NULL); |
5e5100cb DJ |
376 | if (!ret_comp) |
377 | return NULL; | |
378 | ||
379 | ret_comp = unqualified_name_from_comp (ret_comp); | |
380 | ||
381 | ret = NULL; | |
382 | if (ret_comp != NULL) | |
383 | ret = cp_comp_to_string (ret_comp, 10); | |
384 | ||
5e5100cb DJ |
385 | return ret; |
386 | } | |
387 | ||
388 | /* DEMANGLED_NAME is the name of a function, including parameters and | |
389 | (optionally) a return type. Return the name of the function without | |
390 | parameters or return type, or NULL if we can not parse the name. */ | |
391 | ||
3567439c DJ |
392 | char * |
393 | cp_remove_params (const char *demangled_name) | |
5e5100cb DJ |
394 | { |
395 | int done = 0; | |
396 | struct demangle_component *ret_comp; | |
5e5100cb DJ |
397 | char *ret = NULL; |
398 | ||
399 | if (demangled_name == NULL) | |
400 | return NULL; | |
401 | ||
f88e9fd3 | 402 | ret_comp = cp_demangled_name_to_comp (demangled_name, NULL); |
5e5100cb DJ |
403 | if (ret_comp == NULL) |
404 | return NULL; | |
405 | ||
406 | /* First strip off any qualifiers, if we have a function or method. */ | |
407 | while (!done) | |
408 | switch (ret_comp->type) | |
409 | { | |
410 | case DEMANGLE_COMPONENT_CONST: | |
411 | case DEMANGLE_COMPONENT_RESTRICT: | |
412 | case DEMANGLE_COMPONENT_VOLATILE: | |
413 | case DEMANGLE_COMPONENT_CONST_THIS: | |
414 | case DEMANGLE_COMPONENT_RESTRICT_THIS: | |
415 | case DEMANGLE_COMPONENT_VOLATILE_THIS: | |
416 | case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: | |
417 | ret_comp = d_left (ret_comp); | |
418 | break; | |
419 | default: | |
420 | done = 1; | |
421 | break; | |
422 | } | |
423 | ||
424 | /* What we have now should be a function. Return its name. */ | |
425 | if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) | |
426 | ret = cp_comp_to_string (d_left (ret_comp), 10); | |
427 | ||
5e5100cb DJ |
428 | return ret; |
429 | } | |
430 | ||
fb4c6eba DJ |
431 | /* Here are some random pieces of trivia to keep in mind while trying |
432 | to take apart demangled names: | |
de17c821 | 433 | |
fb4c6eba DJ |
434 | - Names can contain function arguments or templates, so the process |
435 | has to be, to some extent recursive: maybe keep track of your | |
436 | depth based on encountering <> and (). | |
437 | ||
438 | - Parentheses don't just have to happen at the end of a name: they | |
439 | can occur even if the name in question isn't a function, because | |
440 | a template argument might be a type that's a function. | |
441 | ||
442 | - Conversely, even if you're trying to deal with a function, its | |
443 | demangled name might not end with ')': it could be a const or | |
444 | volatile class method, in which case it ends with "const" or | |
445 | "volatile". | |
446 | ||
447 | - Parentheses are also used in anonymous namespaces: a variable | |
448 | 'foo' in an anonymous namespace gets demangled as "(anonymous | |
449 | namespace)::foo". | |
450 | ||
451 | - And operator names can contain parentheses or angle brackets. */ | |
452 | ||
453 | /* FIXME: carlton/2003-03-13: We have several functions here with | |
454 | overlapping functionality; can we combine them? Also, do they | |
455 | handle all the above considerations correctly? */ | |
de17c821 | 456 | |
9219021c DC |
457 | |
458 | /* This returns the length of first component of NAME, which should be | |
459 | the demangled name of a C++ variable/function/method/etc. | |
460 | Specifically, it returns the index of the first colon forming the | |
461 | boundary of the first component: so, given 'A::foo' or 'A::B::foo' | |
462 | it returns the 1, and given 'foo', it returns 0. */ | |
463 | ||
b2a7f303 DC |
464 | /* The character in NAME indexed by the return value is guaranteed to |
465 | always be either ':' or '\0'. */ | |
9219021c DC |
466 | |
467 | /* NOTE: carlton/2003-03-13: This function is currently only intended | |
468 | for internal use: it's probably not entirely safe when called on | |
b2a7f303 DC |
469 | user-generated input, because some of the 'index += 2' lines in |
470 | cp_find_first_component_aux might go past the end of malformed | |
471 | input. */ | |
472 | ||
473 | unsigned int | |
474 | cp_find_first_component (const char *name) | |
475 | { | |
476 | return cp_find_first_component_aux (name, 0); | |
477 | } | |
478 | ||
479 | /* Helper function for cp_find_first_component. Like that function, | |
480 | it returns the length of the first component of NAME, but to make | |
481 | the recursion easier, it also stops if it reaches an unexpected ')' | |
482 | or '>' if the value of PERMISSIVE is nonzero. */ | |
9219021c DC |
483 | |
484 | /* Let's optimize away calls to strlen("operator"). */ | |
485 | ||
486 | #define LENGTH_OF_OPERATOR 8 | |
487 | ||
b2a7f303 DC |
488 | static unsigned int |
489 | cp_find_first_component_aux (const char *name, int permissive) | |
9219021c | 490 | { |
9219021c | 491 | unsigned int index = 0; |
0f20eeea DC |
492 | /* Operator names can show up in unexpected places. Since these can |
493 | contain parentheses or angle brackets, they can screw up the | |
494 | recursion. But not every string 'operator' is part of an | |
495 | operater name: e.g. you could have a variable 'cooperator'. So | |
496 | this variable tells us whether or not we should treat the string | |
497 | 'operator' as starting an operator. */ | |
498 | int operator_possible = 1; | |
9219021c DC |
499 | |
500 | for (;; ++index) | |
501 | { | |
502 | switch (name[index]) | |
503 | { | |
504 | case '<': | |
505 | /* Template; eat it up. The calls to cp_first_component | |
506 | should only return (I hope!) when they reach the '>' | |
507 | terminating the component or a '::' between two | |
508 | components. (Hence the '+ 2'.) */ | |
509 | index += 1; | |
b2a7f303 | 510 | for (index += cp_find_first_component_aux (name + index, 1); |
9219021c | 511 | name[index] != '>'; |
b2a7f303 | 512 | index += cp_find_first_component_aux (name + index, 1)) |
9219021c | 513 | { |
b2a7f303 DC |
514 | if (name[index] != ':') |
515 | { | |
516 | demangled_name_complaint (name); | |
517 | return strlen (name); | |
518 | } | |
9219021c DC |
519 | index += 2; |
520 | } | |
0f20eeea | 521 | operator_possible = 1; |
9219021c DC |
522 | break; |
523 | case '(': | |
524 | /* Similar comment as to '<'. */ | |
525 | index += 1; | |
b2a7f303 | 526 | for (index += cp_find_first_component_aux (name + index, 1); |
9219021c | 527 | name[index] != ')'; |
b2a7f303 | 528 | index += cp_find_first_component_aux (name + index, 1)) |
9219021c | 529 | { |
b2a7f303 DC |
530 | if (name[index] != ':') |
531 | { | |
532 | demangled_name_complaint (name); | |
533 | return strlen (name); | |
534 | } | |
9219021c DC |
535 | index += 2; |
536 | } | |
0f20eeea | 537 | operator_possible = 1; |
9219021c DC |
538 | break; |
539 | case '>': | |
540 | case ')': | |
b2a7f303 | 541 | if (permissive) |
7a20f2c2 | 542 | return index; |
b2a7f303 DC |
543 | else |
544 | { | |
545 | demangled_name_complaint (name); | |
546 | return strlen (name); | |
547 | } | |
9219021c DC |
548 | case '\0': |
549 | case ':': | |
550 | return index; | |
0f20eeea DC |
551 | case 'o': |
552 | /* Operator names can screw up the recursion. */ | |
553 | if (operator_possible | |
554 | && strncmp (name + index, "operator", LENGTH_OF_OPERATOR) == 0) | |
555 | { | |
556 | index += LENGTH_OF_OPERATOR; | |
f88e9fd3 | 557 | while (ISSPACE(name[index])) |
0f20eeea DC |
558 | ++index; |
559 | switch (name[index]) | |
560 | { | |
561 | /* Skip over one less than the appropriate number of | |
562 | characters: the for loop will skip over the last | |
563 | one. */ | |
564 | case '<': | |
565 | if (name[index + 1] == '<') | |
566 | index += 1; | |
567 | else | |
568 | index += 0; | |
569 | break; | |
570 | case '>': | |
571 | case '-': | |
572 | if (name[index + 1] == '>') | |
573 | index += 1; | |
574 | else | |
575 | index += 0; | |
576 | break; | |
577 | case '(': | |
578 | index += 1; | |
579 | break; | |
580 | default: | |
581 | index += 0; | |
582 | break; | |
583 | } | |
584 | } | |
585 | operator_possible = 0; | |
586 | break; | |
587 | case ' ': | |
588 | case ',': | |
589 | case '.': | |
590 | case '&': | |
591 | case '*': | |
592 | /* NOTE: carlton/2003-04-18: I'm not sure what the precise | |
593 | set of relevant characters are here: it's necessary to | |
594 | include any character that can show up before 'operator' | |
595 | in a demangled name, and it's safe to include any | |
596 | character that can't be part of an identifier's name. */ | |
597 | operator_possible = 1; | |
598 | break; | |
9219021c | 599 | default: |
0f20eeea | 600 | operator_possible = 0; |
9219021c DC |
601 | break; |
602 | } | |
603 | } | |
604 | } | |
605 | ||
b2a7f303 DC |
606 | /* Complain about a demangled name that we don't know how to parse. |
607 | NAME is the demangled name in question. */ | |
608 | ||
609 | static void | |
610 | demangled_name_complaint (const char *name) | |
611 | { | |
612 | complaint (&symfile_complaints, | |
613 | "unexpected demangled name '%s'", name); | |
614 | } | |
615 | ||
9219021c DC |
616 | /* If NAME is the fully-qualified name of a C++ |
617 | function/variable/method/etc., this returns the length of its | |
618 | entire prefix: all of the namespaces and classes that make up its | |
619 | name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns | |
620 | 4, given 'foo', it returns 0. */ | |
621 | ||
622 | unsigned int | |
623 | cp_entire_prefix_len (const char *name) | |
624 | { | |
625 | unsigned int current_len = cp_find_first_component (name); | |
626 | unsigned int previous_len = 0; | |
627 | ||
628 | while (name[current_len] != '\0') | |
629 | { | |
630 | gdb_assert (name[current_len] == ':'); | |
631 | previous_len = current_len; | |
632 | /* Skip the '::'. */ | |
633 | current_len += 2; | |
634 | current_len += cp_find_first_component (name + current_len); | |
635 | } | |
636 | ||
637 | return previous_len; | |
638 | } | |
639 | ||
b6429628 DC |
640 | /* Overload resolution functions. */ |
641 | ||
8d577d32 DC |
642 | /* Test to see if SYM is a symbol that we haven't seen corresponding |
643 | to a function named OLOAD_NAME. If so, add it to the current | |
644 | completion list. */ | |
b6429628 DC |
645 | |
646 | static void | |
8d577d32 | 647 | overload_list_add_symbol (struct symbol *sym, const char *oload_name) |
b6429628 DC |
648 | { |
649 | int newsize; | |
650 | int i; | |
651 | char *sym_name; | |
652 | ||
653 | /* If there is no type information, we can't do anything, so skip */ | |
654 | if (SYMBOL_TYPE (sym) == NULL) | |
655 | return; | |
656 | ||
657 | /* skip any symbols that we've already considered. */ | |
658 | for (i = 0; i < sym_return_val_index; ++i) | |
8d577d32 DC |
659 | if (strcmp (SYMBOL_LINKAGE_NAME (sym), |
660 | SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0) | |
b6429628 DC |
661 | return; |
662 | ||
663 | /* Get the demangled name without parameters */ | |
3567439c | 664 | sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym)); |
b6429628 DC |
665 | if (!sym_name) |
666 | return; | |
667 | ||
668 | /* skip symbols that cannot match */ | |
669 | if (strcmp (sym_name, oload_name) != 0) | |
670 | { | |
671 | xfree (sym_name); | |
672 | return; | |
673 | } | |
674 | ||
675 | xfree (sym_name); | |
676 | ||
677 | /* We have a match for an overload instance, so add SYM to the current list | |
678 | * of overload instances */ | |
679 | if (sym_return_val_index + 3 > sym_return_val_size) | |
680 | { | |
681 | newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *); | |
682 | sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize); | |
683 | } | |
684 | sym_return_val[sym_return_val_index++] = sym; | |
685 | sym_return_val[sym_return_val_index] = NULL; | |
686 | } | |
687 | ||
688 | /* Return a null-terminated list of pointers to function symbols that | |
8d577d32 | 689 | are named FUNC_NAME and are visible within NAMESPACE. */ |
b6429628 DC |
690 | |
691 | struct symbol ** | |
8d577d32 DC |
692 | make_symbol_overload_list (const char *func_name, |
693 | const char *namespace) | |
b6429628 | 694 | { |
8d577d32 | 695 | struct cleanup *old_cleanups; |
b6429628 | 696 | |
8d577d32 DC |
697 | sym_return_val_size = 100; |
698 | sym_return_val_index = 0; | |
699 | sym_return_val = xmalloc ((sym_return_val_size + 1) * | |
700 | sizeof (struct symbol *)); | |
701 | sym_return_val[0] = NULL; | |
b6429628 | 702 | |
8d577d32 DC |
703 | old_cleanups = make_cleanup (xfree, sym_return_val); |
704 | ||
705 | make_symbol_overload_list_using (func_name, namespace); | |
706 | ||
707 | discard_cleanups (old_cleanups); | |
708 | ||
709 | return sym_return_val; | |
710 | } | |
711 | ||
7322dca9 SW |
712 | /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */ |
713 | ||
714 | static void | |
715 | make_symbol_overload_list_namespace (const char *func_name, | |
716 | const char *namespace) | |
717 | { | |
7322dca9 SW |
718 | if (namespace[0] == '\0') |
719 | make_symbol_overload_list_qualified (func_name); | |
720 | else | |
721 | { | |
722 | char *concatenated_name | |
723 | = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); | |
c5504eaf | 724 | |
7322dca9 SW |
725 | strcpy (concatenated_name, namespace); |
726 | strcat (concatenated_name, "::"); | |
727 | strcat (concatenated_name, func_name); | |
728 | make_symbol_overload_list_qualified (concatenated_name); | |
729 | } | |
730 | } | |
731 | ||
732 | /* Search the namespace of the given type and namespace of and public base | |
733 | types. */ | |
734 | ||
735 | static void | |
736 | make_symbol_overload_list_adl_namespace (struct type *type, | |
737 | const char *func_name) | |
738 | { | |
739 | char *namespace; | |
740 | char *type_name; | |
741 | int i, prefix_len; | |
742 | ||
743 | while (TYPE_CODE (type) == TYPE_CODE_PTR || TYPE_CODE (type) == TYPE_CODE_REF | |
744 | || TYPE_CODE (type) == TYPE_CODE_ARRAY | |
745 | || TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
746 | { | |
747 | if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) | |
748 | type = check_typedef(type); | |
749 | else | |
750 | type = TYPE_TARGET_TYPE (type); | |
751 | } | |
752 | ||
753 | type_name = TYPE_NAME (type); | |
754 | ||
7d3fe98e SW |
755 | if (type_name == NULL) |
756 | return; | |
757 | ||
7322dca9 SW |
758 | prefix_len = cp_entire_prefix_len (type_name); |
759 | ||
760 | if (prefix_len != 0) | |
761 | { | |
762 | namespace = alloca (prefix_len + 1); | |
763 | strncpy (namespace, type_name, prefix_len); | |
764 | namespace[prefix_len] = '\0'; | |
765 | ||
766 | make_symbol_overload_list_namespace (func_name, namespace); | |
767 | } | |
768 | ||
769 | /* Check public base type */ | |
770 | if (TYPE_CODE (type) == TYPE_CODE_CLASS) | |
771 | for (i = 0; i < TYPE_N_BASECLASSES (type); i++) | |
772 | { | |
773 | if (BASETYPE_VIA_PUBLIC (type, i)) | |
774 | make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, i), | |
775 | func_name); | |
776 | } | |
777 | } | |
778 | ||
779 | /* Adds the the overload list overload candidates for FUNC_NAME found through | |
780 | argument dependent lookup. */ | |
781 | ||
782 | struct symbol ** | |
783 | make_symbol_overload_list_adl (struct type **arg_types, int nargs, | |
784 | const char *func_name) | |
785 | { | |
786 | int i; | |
787 | ||
788 | gdb_assert (sym_return_val_size != -1); | |
789 | ||
790 | for (i = 1; i <= nargs; i++) | |
791 | make_symbol_overload_list_adl_namespace (arg_types[i - 1], func_name); | |
792 | ||
793 | return sym_return_val; | |
794 | } | |
795 | ||
8d577d32 DC |
796 | /* This applies the using directives to add namespaces to search in, |
797 | and then searches for overloads in all of those namespaces. It | |
798 | adds the symbols found to sym_return_val. Arguments are as in | |
799 | make_symbol_overload_list. */ | |
800 | ||
801 | static void | |
802 | make_symbol_overload_list_using (const char *func_name, | |
803 | const char *namespace) | |
804 | { | |
805 | const struct using_direct *current; | |
806 | ||
807 | /* First, go through the using directives. If any of them apply, | |
808 | look in the appropriate namespaces for new functions to match | |
809 | on. */ | |
b6429628 | 810 | |
8d577d32 DC |
811 | for (current = block_using (get_selected_block (0)); |
812 | current != NULL; | |
813 | current = current->next) | |
814 | { | |
8c902bb1 | 815 | if (strcmp (namespace, current->import_dest) == 0) |
8d577d32 DC |
816 | { |
817 | make_symbol_overload_list_using (func_name, | |
8c902bb1 | 818 | current->import_src); |
8d577d32 | 819 | } |
b6429628 | 820 | } |
b6429628 | 821 | |
8d577d32 | 822 | /* Now, add names for this namespace. */ |
7322dca9 | 823 | make_symbol_overload_list_namespace (func_name, namespace); |
8d577d32 | 824 | } |
b6429628 | 825 | |
8d577d32 DC |
826 | /* This does the bulk of the work of finding overloaded symbols. |
827 | FUNC_NAME is the name of the overloaded function we're looking for | |
828 | (possibly including namespace info). */ | |
b6429628 | 829 | |
8d577d32 DC |
830 | static void |
831 | make_symbol_overload_list_qualified (const char *func_name) | |
832 | { | |
833 | struct symbol *sym; | |
834 | struct symtab *s; | |
835 | struct objfile *objfile; | |
836 | const struct block *b, *surrounding_static_block = 0; | |
837 | struct dict_iterator iter; | |
838 | const struct dictionary *dict; | |
b6429628 | 839 | |
8d577d32 DC |
840 | /* Look through the partial symtabs for all symbols which begin |
841 | by matching FUNC_NAME. Make sure we read that symbol table in. */ | |
b6429628 | 842 | |
ccefe4c4 TT |
843 | ALL_OBJFILES (objfile) |
844 | { | |
845 | if (objfile->sf) | |
846 | objfile->sf->qf->expand_symtabs_for_function (objfile, func_name); | |
847 | } | |
b6429628 DC |
848 | |
849 | /* Search upwards from currently selected frame (so that we can | |
850 | complete on local vars. */ | |
851 | ||
852 | for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) | |
853 | { | |
8d577d32 | 854 | dict = BLOCK_DICT (b); |
b6429628 | 855 | |
8d577d32 DC |
856 | for (sym = dict_iter_name_first (dict, func_name, &iter); |
857 | sym; | |
858 | sym = dict_iter_name_next (func_name, &iter)) | |
b6429628 | 859 | { |
8d577d32 | 860 | overload_list_add_symbol (sym, func_name); |
b6429628 DC |
861 | } |
862 | } | |
863 | ||
8d577d32 DC |
864 | surrounding_static_block = block_static_block (get_selected_block (0)); |
865 | ||
b6429628 DC |
866 | /* Go through the symtabs and check the externs and statics for |
867 | symbols which match. */ | |
868 | ||
11309657 | 869 | ALL_PRIMARY_SYMTABS (objfile, s) |
b6429628 DC |
870 | { |
871 | QUIT; | |
872 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); | |
8d577d32 DC |
873 | dict = BLOCK_DICT (b); |
874 | ||
875 | for (sym = dict_iter_name_first (dict, func_name, &iter); | |
876 | sym; | |
877 | sym = dict_iter_name_next (func_name, &iter)) | |
878 | { | |
879 | overload_list_add_symbol (sym, func_name); | |
880 | } | |
b6429628 DC |
881 | } |
882 | ||
11309657 | 883 | ALL_PRIMARY_SYMTABS (objfile, s) |
b6429628 DC |
884 | { |
885 | QUIT; | |
886 | b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); | |
887 | /* Don't do this block twice. */ | |
888 | if (b == surrounding_static_block) | |
889 | continue; | |
8d577d32 DC |
890 | dict = BLOCK_DICT (b); |
891 | ||
892 | for (sym = dict_iter_name_first (dict, func_name, &iter); | |
893 | sym; | |
894 | sym = dict_iter_name_next (func_name, &iter)) | |
895 | { | |
896 | overload_list_add_symbol (sym, func_name); | |
897 | } | |
b6429628 | 898 | } |
8d577d32 DC |
899 | } |
900 | ||
362ff856 MC |
901 | /* Lookup the rtti type for a class name. */ |
902 | ||
903 | struct type * | |
904 | cp_lookup_rtti_type (const char *name, struct block *block) | |
905 | { | |
906 | struct symbol * rtti_sym; | |
907 | struct type * rtti_type; | |
908 | ||
2570f2b7 | 909 | rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL); |
362ff856 MC |
910 | |
911 | if (rtti_sym == NULL) | |
912 | { | |
8a3fe4f8 | 913 | warning (_("RTTI symbol not found for class '%s'"), name); |
362ff856 MC |
914 | return NULL; |
915 | } | |
916 | ||
917 | if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF) | |
918 | { | |
8a3fe4f8 | 919 | warning (_("RTTI symbol for class '%s' is not a type"), name); |
362ff856 MC |
920 | return NULL; |
921 | } | |
922 | ||
923 | rtti_type = SYMBOL_TYPE (rtti_sym); | |
924 | ||
925 | switch (TYPE_CODE (rtti_type)) | |
926 | { | |
927 | case TYPE_CODE_CLASS: | |
928 | break; | |
929 | case TYPE_CODE_NAMESPACE: | |
930 | /* chastain/2003-11-26: the symbol tables often contain fake | |
931 | symbols for namespaces with the same name as the struct. | |
932 | This warning is an indication of a bug in the lookup order | |
933 | or a bug in the way that the symbol tables are populated. */ | |
8a3fe4f8 | 934 | warning (_("RTTI symbol for class '%s' is a namespace"), name); |
362ff856 MC |
935 | return NULL; |
936 | default: | |
8a3fe4f8 | 937 | warning (_("RTTI symbol for class '%s' has bad type"), name); |
362ff856 MC |
938 | return NULL; |
939 | } | |
940 | ||
941 | return rtti_type; | |
942 | } | |
b6429628 | 943 | |
9219021c DC |
944 | /* Don't allow just "maintenance cplus". */ |
945 | ||
946 | static void | |
947 | maint_cplus_command (char *arg, int from_tty) | |
948 | { | |
a3f17187 | 949 | printf_unfiltered (_("\"maintenance cplus\" must be followed by the name of a command.\n")); |
9219021c DC |
950 | help_list (maint_cplus_cmd_list, "maintenance cplus ", -1, gdb_stdout); |
951 | } | |
952 | ||
953 | /* This is a front end for cp_find_first_component, for unit testing. | |
954 | Be careful when using it: see the NOTE above | |
955 | cp_find_first_component. */ | |
956 | ||
957 | static void | |
958 | first_component_command (char *arg, int from_tty) | |
959 | { | |
c836824f AR |
960 | int len; |
961 | char *prefix; | |
962 | ||
963 | if (!arg) | |
964 | return; | |
965 | ||
966 | len = cp_find_first_component (arg); | |
967 | prefix = alloca (len + 1); | |
9219021c DC |
968 | |
969 | memcpy (prefix, arg, len); | |
970 | prefix[len] = '\0'; | |
971 | ||
972 | printf_unfiltered ("%s\n", prefix); | |
973 | } | |
974 | ||
b9362cc7 AC |
975 | extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */ |
976 | ||
12907978 KS |
977 | #define SKIP_SPACE(P) \ |
978 | do \ | |
979 | { \ | |
980 | while (*(P) == ' ' || *(P) == '\t') \ | |
981 | ++(P); \ | |
982 | } \ | |
983 | while (0) | |
984 | ||
985 | /* Returns the length of the operator name or 0 if INPUT does not | |
986 | point to a valid C++ operator. INPUT should start with "operator". */ | |
987 | int | |
988 | cp_validate_operator (const char *input) | |
989 | { | |
990 | int i; | |
991 | char *copy; | |
992 | const char *p; | |
993 | struct expression *expr; | |
994 | struct value *val; | |
995 | struct gdb_exception except; | |
12907978 KS |
996 | |
997 | p = input; | |
998 | ||
999 | if (strncmp (p, "operator", 8) == 0) | |
1000 | { | |
1001 | int valid = 0; | |
12907978 | 1002 | |
c5504eaf | 1003 | p += 8; |
12907978 KS |
1004 | SKIP_SPACE (p); |
1005 | for (i = 0; i < sizeof (operator_tokens) / sizeof (operator_tokens[0]); | |
1006 | ++i) | |
1007 | { | |
1008 | int length = strlen (operator_tokens[i]); | |
c5504eaf | 1009 | |
12907978 KS |
1010 | /* By using strncmp here, we MUST have operator_tokens ordered! |
1011 | See additional notes where operator_tokens is defined above. */ | |
1012 | if (strncmp (p, operator_tokens[i], length) == 0) | |
1013 | { | |
1014 | const char *op = p; | |
c5504eaf | 1015 | |
12907978 KS |
1016 | valid = 1; |
1017 | p += length; | |
1018 | ||
1019 | if (strncmp (op, "new", 3) == 0 | |
1020 | || strncmp (op, "delete", 6) == 0) | |
1021 | { | |
1022 | ||
1023 | /* Special case: new[] and delete[]. We must be careful | |
1024 | to swallow whitespace before/in "[]". */ | |
1025 | SKIP_SPACE (p); | |
1026 | ||
1027 | if (*p == '[') | |
1028 | { | |
1029 | ++p; | |
1030 | SKIP_SPACE (p); | |
1031 | if (*p == ']') | |
1032 | ++p; | |
1033 | else | |
1034 | valid = 0; | |
1035 | } | |
1036 | } | |
1037 | ||
1038 | if (valid) | |
1039 | return (p - input); | |
1040 | } | |
1041 | } | |
1042 | ||
1043 | /* Check input for a conversion operator. */ | |
1044 | ||
1045 | /* Skip past base typename */ | |
1046 | while (*p != '*' && *p != '&' && *p != 0 && *p != ' ') | |
1047 | ++p; | |
1048 | SKIP_SPACE (p); | |
1049 | ||
1050 | /* Add modifiers '*'/'&' */ | |
1051 | while (*p == '*' || *p == '&') | |
1052 | { | |
1053 | ++p; | |
1054 | SKIP_SPACE (p); | |
1055 | } | |
1056 | ||
1057 | /* Check for valid type. [Remember: input starts with | |
1058 | "operator".] */ | |
1059 | copy = savestring (input + 8, p - input - 8); | |
1060 | expr = NULL; | |
1061 | val = NULL; | |
1062 | TRY_CATCH (except, RETURN_MASK_ALL) | |
1063 | { | |
1064 | expr = parse_expression (copy); | |
1065 | val = evaluate_type (expr); | |
1066 | } | |
1067 | ||
1068 | xfree (copy); | |
1069 | if (expr) | |
1070 | xfree (expr); | |
1071 | ||
1072 | if (val != NULL && value_type (val) != NULL) | |
1073 | return (p - input); | |
1074 | } | |
1075 | ||
1076 | return 0; | |
1077 | } | |
1078 | ||
9219021c DC |
1079 | void |
1080 | _initialize_cp_support (void) | |
1081 | { | |
1082 | add_prefix_cmd ("cplus", class_maintenance, maint_cplus_command, | |
1bedd215 | 1083 | _("C++ maintenance commands."), &maint_cplus_cmd_list, |
9219021c DC |
1084 | "maintenance cplus ", 0, &maintenancelist); |
1085 | add_alias_cmd ("cp", "cplus", class_maintenance, 1, &maintenancelist); | |
1086 | ||
1087 | add_cmd ("first_component", class_maintenance, first_component_command, | |
1a966eab | 1088 | _("Print the first class/namespace component of NAME."), |
9219021c | 1089 | &maint_cplus_cmd_list); |
9219021c | 1090 | } |