GDB (API)
|
00001 /* Helper routines for C++ support in GDB. 00002 Copyright (C) 2002-2013 Free Software Foundation, Inc. 00003 00004 Contributed by MontaVista Software. 00005 00006 This file is part of GDB. 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00020 00021 #include "defs.h" 00022 #include "cp-support.h" 00023 #include "gdb_string.h" 00024 #include "demangle.h" 00025 #include "gdb_assert.h" 00026 #include "gdbcmd.h" 00027 #include "dictionary.h" 00028 #include "objfiles.h" 00029 #include "frame.h" 00030 #include "symtab.h" 00031 #include "block.h" 00032 #include "complaints.h" 00033 #include "gdbtypes.h" 00034 #include "exceptions.h" 00035 #include "expression.h" 00036 #include "value.h" 00037 #include "cp-abi.h" 00038 00039 #include "safe-ctype.h" 00040 00041 #define d_left(dc) (dc)->u.s_binary.left 00042 #define d_right(dc) (dc)->u.s_binary.right 00043 00044 /* Functions related to demangled name parsing. */ 00045 00046 static unsigned int cp_find_first_component_aux (const char *name, 00047 int permissive); 00048 00049 static void demangled_name_complaint (const char *name); 00050 00051 /* Functions/variables related to overload resolution. */ 00052 00053 static int sym_return_val_size = -1; 00054 static int sym_return_val_index; 00055 static struct symbol **sym_return_val; 00056 00057 static void overload_list_add_symbol (struct symbol *sym, 00058 const char *oload_name); 00059 00060 static void make_symbol_overload_list_using (const char *func_name, 00061 const char *namespace); 00062 00063 static void make_symbol_overload_list_qualified (const char *func_name); 00064 00065 /* The list of "maint cplus" commands. */ 00066 00067 struct cmd_list_element *maint_cplus_cmd_list = NULL; 00068 00069 /* The actual commands. */ 00070 00071 static void maint_cplus_command (char *arg, int from_tty); 00072 static void first_component_command (char *arg, int from_tty); 00073 00074 /* A list of typedefs which should not be substituted by replace_typedefs. */ 00075 static const char * const ignore_typedefs[] = 00076 { 00077 "std::istream", "std::iostream", "std::ostream", "std::string" 00078 }; 00079 00080 static void 00081 replace_typedefs (struct demangle_parse_info *info, 00082 struct demangle_component *ret_comp, 00083 canonicalization_ftype *finder, 00084 void *data); 00085 00086 /* A convenience function to copy STRING into OBSTACK, returning a pointer 00087 to the newly allocated string and saving the number of bytes saved in LEN. 00088 00089 It does not copy the terminating '\0' byte! */ 00090 00091 static char * 00092 copy_string_to_obstack (struct obstack *obstack, const char *string, 00093 long *len) 00094 { 00095 *len = strlen (string); 00096 return obstack_copy (obstack, string, *len); 00097 } 00098 00099 /* A cleanup wrapper for cp_demangled_name_parse_free. */ 00100 00101 static void 00102 do_demangled_name_parse_free_cleanup (void *data) 00103 { 00104 struct demangle_parse_info *info = (struct demangle_parse_info *) data; 00105 00106 cp_demangled_name_parse_free (info); 00107 } 00108 00109 /* Create a cleanup for C++ name parsing. */ 00110 00111 struct cleanup * 00112 make_cleanup_cp_demangled_name_parse_free (struct demangle_parse_info *info) 00113 { 00114 return make_cleanup (do_demangled_name_parse_free_cleanup, info); 00115 } 00116 00117 /* Return 1 if STRING is clearly already in canonical form. This 00118 function is conservative; things which it does not recognize are 00119 assumed to be non-canonical, and the parser will sort them out 00120 afterwards. This speeds up the critical path for alphanumeric 00121 identifiers. */ 00122 00123 static int 00124 cp_already_canonical (const char *string) 00125 { 00126 /* Identifier start character [a-zA-Z_]. */ 00127 if (!ISIDST (string[0])) 00128 return 0; 00129 00130 /* These are the only two identifiers which canonicalize to other 00131 than themselves or an error: unsigned -> unsigned int and 00132 signed -> int. */ 00133 if (string[0] == 'u' && strcmp (&string[1], "nsigned") == 0) 00134 return 0; 00135 else if (string[0] == 's' && strcmp (&string[1], "igned") == 0) 00136 return 0; 00137 00138 /* Identifier character [a-zA-Z0-9_]. */ 00139 while (ISIDNUM (string[1])) 00140 string++; 00141 00142 if (string[1] == '\0') 00143 return 1; 00144 else 00145 return 0; 00146 } 00147 00148 /* Inspect the given RET_COMP for its type. If it is a typedef, 00149 replace the node with the typedef's tree. 00150 00151 Returns 1 if any typedef substitutions were made, 0 otherwise. */ 00152 00153 static int 00154 inspect_type (struct demangle_parse_info *info, 00155 struct demangle_component *ret_comp, 00156 canonicalization_ftype *finder, 00157 void *data) 00158 { 00159 int i; 00160 char *name; 00161 struct symbol *sym; 00162 volatile struct gdb_exception except; 00163 00164 /* Copy the symbol's name from RET_COMP and look it up 00165 in the symbol table. */ 00166 name = (char *) alloca (ret_comp->u.s_name.len + 1); 00167 memcpy (name, ret_comp->u.s_name.s, ret_comp->u.s_name.len); 00168 name[ret_comp->u.s_name.len] = '\0'; 00169 00170 /* Ignore any typedefs that should not be substituted. */ 00171 for (i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i) 00172 { 00173 if (strcmp (name, ignore_typedefs[i]) == 0) 00174 return 0; 00175 } 00176 00177 sym = NULL; 00178 TRY_CATCH (except, RETURN_MASK_ALL) 00179 { 00180 sym = lookup_symbol (name, 0, VAR_DOMAIN, 0); 00181 } 00182 00183 if (except.reason >= 0 && sym != NULL) 00184 { 00185 struct type *otype = SYMBOL_TYPE (sym); 00186 00187 if (finder != NULL) 00188 { 00189 const char *new_name = (*finder) (otype, data); 00190 00191 if (new_name != NULL) 00192 { 00193 ret_comp->u.s_name.s = new_name; 00194 ret_comp->u.s_name.len = strlen (new_name); 00195 return 1; 00196 } 00197 00198 return 0; 00199 } 00200 00201 /* If the type is a typedef, replace it. */ 00202 if (TYPE_CODE (otype) == TYPE_CODE_TYPEDEF) 00203 { 00204 long len; 00205 int is_anon; 00206 struct type *type; 00207 struct demangle_parse_info *i; 00208 struct ui_file *buf; 00209 00210 /* Get the real type of the typedef. */ 00211 type = check_typedef (otype); 00212 00213 is_anon = (TYPE_TAG_NAME (type) == NULL 00214 && (TYPE_CODE (type) == TYPE_CODE_ENUM 00215 || TYPE_CODE (type) == TYPE_CODE_STRUCT 00216 || TYPE_CODE (type) == TYPE_CODE_UNION)); 00217 if (is_anon) 00218 { 00219 struct type *last = otype; 00220 00221 /* Find the last typedef for the type. */ 00222 while (TYPE_TARGET_TYPE (last) != NULL 00223 && (TYPE_CODE (TYPE_TARGET_TYPE (last)) 00224 == TYPE_CODE_TYPEDEF)) 00225 last = TYPE_TARGET_TYPE (last); 00226 00227 /* If there is only one typedef for this anonymous type, 00228 do not substitute it. */ 00229 if (type == otype) 00230 return 0; 00231 else 00232 /* Use the last typedef seen as the type for this 00233 anonymous type. */ 00234 type = last; 00235 } 00236 00237 buf = mem_fileopen (); 00238 TRY_CATCH (except, RETURN_MASK_ERROR) 00239 { 00240 type_print (type, "", buf, -1); 00241 } 00242 00243 /* If type_print threw an exception, there is little point 00244 in continuing, so just bow out gracefully. */ 00245 if (except.reason < 0) 00246 { 00247 ui_file_delete (buf); 00248 return 0; 00249 } 00250 00251 name = ui_file_obsavestring (buf, &info->obstack, &len); 00252 ui_file_delete (buf); 00253 00254 /* Turn the result into a new tree. Note that this 00255 tree will contain pointers into NAME, so NAME cannot 00256 be free'd until all typedef conversion is done and 00257 the final result is converted into a string. */ 00258 i = cp_demangled_name_to_comp (name, NULL); 00259 if (i != NULL) 00260 { 00261 /* Merge the two trees. */ 00262 cp_merge_demangle_parse_infos (info, ret_comp, i); 00263 00264 /* Replace any newly introduced typedefs -- but not 00265 if the type is anonymous (that would lead to infinite 00266 looping). */ 00267 if (!is_anon) 00268 replace_typedefs (info, ret_comp, finder, data); 00269 } 00270 else 00271 { 00272 /* This shouldn't happen unless the type printer has 00273 output something that the name parser cannot grok. 00274 Nonetheless, an ounce of prevention... 00275 00276 Canonicalize the name again, and store it in the 00277 current node (RET_COMP). */ 00278 char *canon = cp_canonicalize_string_no_typedefs (name); 00279 00280 if (canon != NULL) 00281 { 00282 /* Copy the canonicalization into the obstack and 00283 free CANON. */ 00284 name = copy_string_to_obstack (&info->obstack, canon, &len); 00285 xfree (canon); 00286 } 00287 00288 ret_comp->u.s_name.s = name; 00289 ret_comp->u.s_name.len = len; 00290 } 00291 00292 return 1; 00293 } 00294 } 00295 00296 return 0; 00297 } 00298 00299 /* Replace any typedefs appearing in the qualified name 00300 (DEMANGLE_COMPONENT_QUAL_NAME) represented in RET_COMP for the name parse 00301 given in INFO. */ 00302 00303 static void 00304 replace_typedefs_qualified_name (struct demangle_parse_info *info, 00305 struct demangle_component *ret_comp, 00306 canonicalization_ftype *finder, 00307 void *data) 00308 { 00309 long len; 00310 char *name; 00311 struct ui_file *buf = mem_fileopen (); 00312 struct demangle_component *comp = ret_comp; 00313 00314 /* Walk each node of the qualified name, reconstructing the name of 00315 this element. With every node, check for any typedef substitutions. 00316 If a substitution has occurred, replace the qualified name node 00317 with a DEMANGLE_COMPONENT_NAME node representing the new, typedef- 00318 substituted name. */ 00319 while (comp->type == DEMANGLE_COMPONENT_QUAL_NAME) 00320 { 00321 if (d_left (comp)->type == DEMANGLE_COMPONENT_NAME) 00322 { 00323 struct demangle_component new; 00324 00325 ui_file_write (buf, d_left (comp)->u.s_name.s, 00326 d_left (comp)->u.s_name.len); 00327 name = ui_file_obsavestring (buf, &info->obstack, &len); 00328 new.type = DEMANGLE_COMPONENT_NAME; 00329 new.u.s_name.s = name; 00330 new.u.s_name.len = len; 00331 if (inspect_type (info, &new, finder, data)) 00332 { 00333 char *n, *s; 00334 long slen; 00335 00336 /* A typedef was substituted in NEW. Convert it to a 00337 string and replace the top DEMANGLE_COMPONENT_QUAL_NAME 00338 node. */ 00339 00340 ui_file_rewind (buf); 00341 n = cp_comp_to_string (&new, 100); 00342 if (n == NULL) 00343 { 00344 /* If something went astray, abort typedef substitutions. */ 00345 ui_file_delete (buf); 00346 return; 00347 } 00348 00349 s = copy_string_to_obstack (&info->obstack, n, &slen); 00350 xfree (n); 00351 00352 d_left (ret_comp)->type = DEMANGLE_COMPONENT_NAME; 00353 d_left (ret_comp)->u.s_name.s = s; 00354 d_left (ret_comp)->u.s_name.len = slen; 00355 d_right (ret_comp) = d_right (comp); 00356 comp = ret_comp; 00357 continue; 00358 } 00359 } 00360 else 00361 { 00362 /* The current node is not a name, so simply replace any 00363 typedefs in it. Then print it to the stream to continue 00364 checking for more typedefs in the tree. */ 00365 replace_typedefs (info, d_left (comp), finder, data); 00366 name = cp_comp_to_string (d_left (comp), 100); 00367 if (name == NULL) 00368 { 00369 /* If something went astray, abort typedef substitutions. */ 00370 ui_file_delete (buf); 00371 return; 00372 } 00373 fputs_unfiltered (name, buf); 00374 xfree (name); 00375 } 00376 00377 ui_file_write (buf, "::", 2); 00378 comp = d_right (comp); 00379 } 00380 00381 /* If the next component is DEMANGLE_COMPONENT_NAME, save the qualified 00382 name assembled above and append the name given by COMP. Then use this 00383 reassembled name to check for a typedef. */ 00384 00385 if (comp->type == DEMANGLE_COMPONENT_NAME) 00386 { 00387 ui_file_write (buf, comp->u.s_name.s, comp->u.s_name.len); 00388 name = ui_file_obsavestring (buf, &info->obstack, &len); 00389 00390 /* Replace the top (DEMANGLE_COMPONENT_QUAL_NAME) node 00391 with a DEMANGLE_COMPONENT_NAME node containing the whole 00392 name. */ 00393 ret_comp->type = DEMANGLE_COMPONENT_NAME; 00394 ret_comp->u.s_name.s = name; 00395 ret_comp->u.s_name.len = len; 00396 inspect_type (info, ret_comp, finder, data); 00397 } 00398 else 00399 replace_typedefs (info, comp, finder, data); 00400 00401 ui_file_delete (buf); 00402 } 00403 00404 00405 /* A function to check const and volatile qualifiers for argument types. 00406 00407 "Parameter declarations that differ only in the presence 00408 or absence of `const' and/or `volatile' are equivalent." 00409 C++ Standard N3290, clause 13.1.3 #4. */ 00410 00411 static void 00412 check_cv_qualifiers (struct demangle_component *ret_comp) 00413 { 00414 while (d_left (ret_comp) != NULL 00415 && (d_left (ret_comp)->type == DEMANGLE_COMPONENT_CONST 00416 || d_left (ret_comp)->type == DEMANGLE_COMPONENT_VOLATILE)) 00417 { 00418 d_left (ret_comp) = d_left (d_left (ret_comp)); 00419 } 00420 } 00421 00422 /* Walk the parse tree given by RET_COMP, replacing any typedefs with 00423 their basic types. */ 00424 00425 static void 00426 replace_typedefs (struct demangle_parse_info *info, 00427 struct demangle_component *ret_comp, 00428 canonicalization_ftype *finder, 00429 void *data) 00430 { 00431 if (ret_comp) 00432 { 00433 if (finder != NULL 00434 && (ret_comp->type == DEMANGLE_COMPONENT_NAME 00435 || ret_comp->type == DEMANGLE_COMPONENT_QUAL_NAME 00436 || ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE 00437 || ret_comp->type == DEMANGLE_COMPONENT_BUILTIN_TYPE)) 00438 { 00439 char *local_name = cp_comp_to_string (ret_comp, 10); 00440 00441 if (local_name != NULL) 00442 { 00443 struct symbol *sym; 00444 volatile struct gdb_exception except; 00445 00446 sym = NULL; 00447 TRY_CATCH (except, RETURN_MASK_ALL) 00448 { 00449 sym = lookup_symbol (local_name, 0, VAR_DOMAIN, 0); 00450 } 00451 xfree (local_name); 00452 00453 if (except.reason >= 0 && sym != NULL) 00454 { 00455 struct type *otype = SYMBOL_TYPE (sym); 00456 const char *new_name = (*finder) (otype, data); 00457 00458 if (new_name != NULL) 00459 { 00460 ret_comp->type = DEMANGLE_COMPONENT_NAME; 00461 ret_comp->u.s_name.s = new_name; 00462 ret_comp->u.s_name.len = strlen (new_name); 00463 return; 00464 } 00465 } 00466 } 00467 } 00468 00469 switch (ret_comp->type) 00470 { 00471 case DEMANGLE_COMPONENT_ARGLIST: 00472 check_cv_qualifiers (ret_comp); 00473 /* Fall through */ 00474 00475 case DEMANGLE_COMPONENT_FUNCTION_TYPE: 00476 case DEMANGLE_COMPONENT_TEMPLATE: 00477 case DEMANGLE_COMPONENT_TEMPLATE_ARGLIST: 00478 case DEMANGLE_COMPONENT_TYPED_NAME: 00479 replace_typedefs (info, d_left (ret_comp), finder, data); 00480 replace_typedefs (info, d_right (ret_comp), finder, data); 00481 break; 00482 00483 case DEMANGLE_COMPONENT_NAME: 00484 inspect_type (info, ret_comp, finder, data); 00485 break; 00486 00487 case DEMANGLE_COMPONENT_QUAL_NAME: 00488 replace_typedefs_qualified_name (info, ret_comp, finder, data); 00489 break; 00490 00491 case DEMANGLE_COMPONENT_LOCAL_NAME: 00492 case DEMANGLE_COMPONENT_CTOR: 00493 case DEMANGLE_COMPONENT_ARRAY_TYPE: 00494 case DEMANGLE_COMPONENT_PTRMEM_TYPE: 00495 replace_typedefs (info, d_right (ret_comp), finder, data); 00496 break; 00497 00498 case DEMANGLE_COMPONENT_CONST: 00499 case DEMANGLE_COMPONENT_RESTRICT: 00500 case DEMANGLE_COMPONENT_VOLATILE: 00501 case DEMANGLE_COMPONENT_VOLATILE_THIS: 00502 case DEMANGLE_COMPONENT_CONST_THIS: 00503 case DEMANGLE_COMPONENT_RESTRICT_THIS: 00504 case DEMANGLE_COMPONENT_POINTER: 00505 case DEMANGLE_COMPONENT_REFERENCE: 00506 replace_typedefs (info, d_left (ret_comp), finder, data); 00507 break; 00508 00509 default: 00510 break; 00511 } 00512 } 00513 } 00514 00515 /* Parse STRING and convert it to canonical form, resolving any typedefs. 00516 If parsing fails, or if STRING is already canonical, return NULL. 00517 Otherwise return the canonical form. The return value is allocated via 00518 xmalloc. If FINDER is not NULL, then type components are passed to 00519 FINDER to be looked up. DATA is passed verbatim to FINDER. */ 00520 00521 char * 00522 cp_canonicalize_string_full (const char *string, 00523 canonicalization_ftype *finder, 00524 void *data) 00525 { 00526 char *ret; 00527 unsigned int estimated_len; 00528 struct demangle_parse_info *info; 00529 00530 ret = NULL; 00531 estimated_len = strlen (string) * 2; 00532 info = cp_demangled_name_to_comp (string, NULL); 00533 if (info != NULL) 00534 { 00535 /* Replace all the typedefs in the tree. */ 00536 replace_typedefs (info, info->tree, finder, data); 00537 00538 /* Convert the tree back into a string. */ 00539 ret = cp_comp_to_string (info->tree, estimated_len); 00540 gdb_assert (ret != NULL); 00541 00542 /* Free the parse information. */ 00543 cp_demangled_name_parse_free (info); 00544 00545 /* Finally, compare the original string with the computed 00546 name, returning NULL if they are the same. */ 00547 if (strcmp (string, ret) == 0) 00548 { 00549 xfree (ret); 00550 return NULL; 00551 } 00552 } 00553 00554 return ret; 00555 } 00556 00557 /* Like cp_canonicalize_string_full, but always passes NULL for 00558 FINDER. */ 00559 00560 char * 00561 cp_canonicalize_string_no_typedefs (const char *string) 00562 { 00563 return cp_canonicalize_string_full (string, NULL, NULL); 00564 } 00565 00566 /* Parse STRING and convert it to canonical form. If parsing fails, 00567 or if STRING is already canonical, return NULL. Otherwise return 00568 the canonical form. The return value is allocated via xmalloc. */ 00569 00570 char * 00571 cp_canonicalize_string (const char *string) 00572 { 00573 struct demangle_parse_info *info; 00574 unsigned int estimated_len; 00575 char *ret; 00576 00577 if (cp_already_canonical (string)) 00578 return NULL; 00579 00580 info = cp_demangled_name_to_comp (string, NULL); 00581 if (info == NULL) 00582 return NULL; 00583 00584 estimated_len = strlen (string) * 2; 00585 ret = cp_comp_to_string (info->tree, estimated_len); 00586 cp_demangled_name_parse_free (info); 00587 00588 if (ret == NULL) 00589 { 00590 warning (_("internal error: string \"%s\" failed to be canonicalized"), 00591 string); 00592 return NULL; 00593 } 00594 00595 if (strcmp (string, ret) == 0) 00596 { 00597 xfree (ret); 00598 return NULL; 00599 } 00600 00601 return ret; 00602 } 00603 00604 /* Convert a mangled name to a demangle_component tree. *MEMORY is 00605 set to the block of used memory that should be freed when finished 00606 with the tree. DEMANGLED_P is set to the char * that should be 00607 freed when finished with the tree, or NULL if none was needed. 00608 OPTIONS will be passed to the demangler. */ 00609 00610 static struct demangle_parse_info * 00611 mangled_name_to_comp (const char *mangled_name, int options, 00612 void **memory, char **demangled_p) 00613 { 00614 char *demangled_name; 00615 struct demangle_parse_info *info; 00616 00617 /* If it looks like a v3 mangled name, then try to go directly 00618 to trees. */ 00619 if (mangled_name[0] == '_' && mangled_name[1] == 'Z') 00620 { 00621 struct demangle_component *ret; 00622 00623 ret = cplus_demangle_v3_components (mangled_name, 00624 options, memory); 00625 if (ret) 00626 { 00627 info = cp_new_demangle_parse_info (); 00628 info->tree = ret; 00629 *demangled_p = NULL; 00630 return info; 00631 } 00632 } 00633 00634 /* If it doesn't, or if that failed, then try to demangle the 00635 name. */ 00636 demangled_name = gdb_demangle (mangled_name, options); 00637 if (demangled_name == NULL) 00638 return NULL; 00639 00640 /* If we could demangle the name, parse it to build the component 00641 tree. */ 00642 info = cp_demangled_name_to_comp (demangled_name, NULL); 00643 00644 if (info == NULL) 00645 { 00646 xfree (demangled_name); 00647 return NULL; 00648 } 00649 00650 *demangled_p = demangled_name; 00651 return info; 00652 } 00653 00654 /* Return the name of the class containing method PHYSNAME. */ 00655 00656 char * 00657 cp_class_name_from_physname (const char *physname) 00658 { 00659 void *storage = NULL; 00660 char *demangled_name = NULL, *ret; 00661 struct demangle_component *ret_comp, *prev_comp, *cur_comp; 00662 struct demangle_parse_info *info; 00663 int done; 00664 00665 info = mangled_name_to_comp (physname, DMGL_ANSI, 00666 &storage, &demangled_name); 00667 if (info == NULL) 00668 return NULL; 00669 00670 done = 0; 00671 ret_comp = info->tree; 00672 00673 /* First strip off any qualifiers, if we have a function or 00674 method. */ 00675 while (!done) 00676 switch (ret_comp->type) 00677 { 00678 case DEMANGLE_COMPONENT_CONST: 00679 case DEMANGLE_COMPONENT_RESTRICT: 00680 case DEMANGLE_COMPONENT_VOLATILE: 00681 case DEMANGLE_COMPONENT_CONST_THIS: 00682 case DEMANGLE_COMPONENT_RESTRICT_THIS: 00683 case DEMANGLE_COMPONENT_VOLATILE_THIS: 00684 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 00685 ret_comp = d_left (ret_comp); 00686 break; 00687 default: 00688 done = 1; 00689 break; 00690 } 00691 00692 /* If what we have now is a function, discard the argument list. */ 00693 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) 00694 ret_comp = d_left (ret_comp); 00695 00696 /* If what we have now is a template, strip off the template 00697 arguments. The left subtree may be a qualified name. */ 00698 if (ret_comp->type == DEMANGLE_COMPONENT_TEMPLATE) 00699 ret_comp = d_left (ret_comp); 00700 00701 /* What we have now should be a name, possibly qualified. 00702 Additional qualifiers could live in the left subtree or the right 00703 subtree. Find the last piece. */ 00704 done = 0; 00705 prev_comp = NULL; 00706 cur_comp = ret_comp; 00707 while (!done) 00708 switch (cur_comp->type) 00709 { 00710 case DEMANGLE_COMPONENT_QUAL_NAME: 00711 case DEMANGLE_COMPONENT_LOCAL_NAME: 00712 prev_comp = cur_comp; 00713 cur_comp = d_right (cur_comp); 00714 break; 00715 case DEMANGLE_COMPONENT_TEMPLATE: 00716 case DEMANGLE_COMPONENT_NAME: 00717 case DEMANGLE_COMPONENT_CTOR: 00718 case DEMANGLE_COMPONENT_DTOR: 00719 case DEMANGLE_COMPONENT_OPERATOR: 00720 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: 00721 done = 1; 00722 break; 00723 default: 00724 done = 1; 00725 cur_comp = NULL; 00726 break; 00727 } 00728 00729 ret = NULL; 00730 if (cur_comp != NULL && prev_comp != NULL) 00731 { 00732 /* We want to discard the rightmost child of PREV_COMP. */ 00733 *prev_comp = *d_left (prev_comp); 00734 /* The ten is completely arbitrary; we don't have a good 00735 estimate. */ 00736 ret = cp_comp_to_string (ret_comp, 10); 00737 } 00738 00739 xfree (storage); 00740 xfree (demangled_name); 00741 cp_demangled_name_parse_free (info); 00742 return ret; 00743 } 00744 00745 /* Return the child of COMP which is the basename of a method, 00746 variable, et cetera. All scope qualifiers are discarded, but 00747 template arguments will be included. The component tree may be 00748 modified. */ 00749 00750 static struct demangle_component * 00751 unqualified_name_from_comp (struct demangle_component *comp) 00752 { 00753 struct demangle_component *ret_comp = comp, *last_template; 00754 int done; 00755 00756 done = 0; 00757 last_template = NULL; 00758 while (!done) 00759 switch (ret_comp->type) 00760 { 00761 case DEMANGLE_COMPONENT_QUAL_NAME: 00762 case DEMANGLE_COMPONENT_LOCAL_NAME: 00763 ret_comp = d_right (ret_comp); 00764 break; 00765 case DEMANGLE_COMPONENT_TYPED_NAME: 00766 ret_comp = d_left (ret_comp); 00767 break; 00768 case DEMANGLE_COMPONENT_TEMPLATE: 00769 gdb_assert (last_template == NULL); 00770 last_template = ret_comp; 00771 ret_comp = d_left (ret_comp); 00772 break; 00773 case DEMANGLE_COMPONENT_CONST: 00774 case DEMANGLE_COMPONENT_RESTRICT: 00775 case DEMANGLE_COMPONENT_VOLATILE: 00776 case DEMANGLE_COMPONENT_CONST_THIS: 00777 case DEMANGLE_COMPONENT_RESTRICT_THIS: 00778 case DEMANGLE_COMPONENT_VOLATILE_THIS: 00779 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 00780 ret_comp = d_left (ret_comp); 00781 break; 00782 case DEMANGLE_COMPONENT_NAME: 00783 case DEMANGLE_COMPONENT_CTOR: 00784 case DEMANGLE_COMPONENT_DTOR: 00785 case DEMANGLE_COMPONENT_OPERATOR: 00786 case DEMANGLE_COMPONENT_EXTENDED_OPERATOR: 00787 done = 1; 00788 break; 00789 default: 00790 return NULL; 00791 break; 00792 } 00793 00794 if (last_template) 00795 { 00796 d_left (last_template) = ret_comp; 00797 return last_template; 00798 } 00799 00800 return ret_comp; 00801 } 00802 00803 /* Return the name of the method whose linkage name is PHYSNAME. */ 00804 00805 char * 00806 method_name_from_physname (const char *physname) 00807 { 00808 void *storage = NULL; 00809 char *demangled_name = NULL, *ret; 00810 struct demangle_component *ret_comp; 00811 struct demangle_parse_info *info; 00812 00813 info = mangled_name_to_comp (physname, DMGL_ANSI, 00814 &storage, &demangled_name); 00815 if (info == NULL) 00816 return NULL; 00817 00818 ret_comp = unqualified_name_from_comp (info->tree); 00819 00820 ret = NULL; 00821 if (ret_comp != NULL) 00822 /* The ten is completely arbitrary; we don't have a good 00823 estimate. */ 00824 ret = cp_comp_to_string (ret_comp, 10); 00825 00826 xfree (storage); 00827 xfree (demangled_name); 00828 cp_demangled_name_parse_free (info); 00829 return ret; 00830 } 00831 00832 /* If FULL_NAME is the demangled name of a C++ function (including an 00833 arg list, possibly including namespace/class qualifications), 00834 return a new string containing only the function name (without the 00835 arg list/class qualifications). Otherwise, return NULL. The 00836 caller is responsible for freeing the memory in question. */ 00837 00838 char * 00839 cp_func_name (const char *full_name) 00840 { 00841 char *ret; 00842 struct demangle_component *ret_comp; 00843 struct demangle_parse_info *info; 00844 00845 info = cp_demangled_name_to_comp (full_name, NULL); 00846 if (!info) 00847 return NULL; 00848 00849 ret_comp = unqualified_name_from_comp (info->tree); 00850 00851 ret = NULL; 00852 if (ret_comp != NULL) 00853 ret = cp_comp_to_string (ret_comp, 10); 00854 00855 cp_demangled_name_parse_free (info); 00856 return ret; 00857 } 00858 00859 /* DEMANGLED_NAME is the name of a function, including parameters and 00860 (optionally) a return type. Return the name of the function without 00861 parameters or return type, or NULL if we can not parse the name. */ 00862 00863 char * 00864 cp_remove_params (const char *demangled_name) 00865 { 00866 int done = 0; 00867 struct demangle_component *ret_comp; 00868 struct demangle_parse_info *info; 00869 char *ret = NULL; 00870 00871 if (demangled_name == NULL) 00872 return NULL; 00873 00874 info = cp_demangled_name_to_comp (demangled_name, NULL); 00875 if (info == NULL) 00876 return NULL; 00877 00878 /* First strip off any qualifiers, if we have a function or method. */ 00879 ret_comp = info->tree; 00880 while (!done) 00881 switch (ret_comp->type) 00882 { 00883 case DEMANGLE_COMPONENT_CONST: 00884 case DEMANGLE_COMPONENT_RESTRICT: 00885 case DEMANGLE_COMPONENT_VOLATILE: 00886 case DEMANGLE_COMPONENT_CONST_THIS: 00887 case DEMANGLE_COMPONENT_RESTRICT_THIS: 00888 case DEMANGLE_COMPONENT_VOLATILE_THIS: 00889 case DEMANGLE_COMPONENT_VENDOR_TYPE_QUAL: 00890 ret_comp = d_left (ret_comp); 00891 break; 00892 default: 00893 done = 1; 00894 break; 00895 } 00896 00897 /* What we have now should be a function. Return its name. */ 00898 if (ret_comp->type == DEMANGLE_COMPONENT_TYPED_NAME) 00899 ret = cp_comp_to_string (d_left (ret_comp), 10); 00900 00901 cp_demangled_name_parse_free (info); 00902 return ret; 00903 } 00904 00905 /* Here are some random pieces of trivia to keep in mind while trying 00906 to take apart demangled names: 00907 00908 - Names can contain function arguments or templates, so the process 00909 has to be, to some extent recursive: maybe keep track of your 00910 depth based on encountering <> and (). 00911 00912 - Parentheses don't just have to happen at the end of a name: they 00913 can occur even if the name in question isn't a function, because 00914 a template argument might be a type that's a function. 00915 00916 - Conversely, even if you're trying to deal with a function, its 00917 demangled name might not end with ')': it could be a const or 00918 volatile class method, in which case it ends with "const" or 00919 "volatile". 00920 00921 - Parentheses are also used in anonymous namespaces: a variable 00922 'foo' in an anonymous namespace gets demangled as "(anonymous 00923 namespace)::foo". 00924 00925 - And operator names can contain parentheses or angle brackets. */ 00926 00927 /* FIXME: carlton/2003-03-13: We have several functions here with 00928 overlapping functionality; can we combine them? Also, do they 00929 handle all the above considerations correctly? */ 00930 00931 00932 /* This returns the length of first component of NAME, which should be 00933 the demangled name of a C++ variable/function/method/etc. 00934 Specifically, it returns the index of the first colon forming the 00935 boundary of the first component: so, given 'A::foo' or 'A::B::foo' 00936 it returns the 1, and given 'foo', it returns 0. */ 00937 00938 /* The character in NAME indexed by the return value is guaranteed to 00939 always be either ':' or '\0'. */ 00940 00941 /* NOTE: carlton/2003-03-13: This function is currently only intended 00942 for internal use: it's probably not entirely safe when called on 00943 user-generated input, because some of the 'index += 2' lines in 00944 cp_find_first_component_aux might go past the end of malformed 00945 input. */ 00946 00947 unsigned int 00948 cp_find_first_component (const char *name) 00949 { 00950 return cp_find_first_component_aux (name, 0); 00951 } 00952 00953 /* Helper function for cp_find_first_component. Like that function, 00954 it returns the length of the first component of NAME, but to make 00955 the recursion easier, it also stops if it reaches an unexpected ')' 00956 or '>' if the value of PERMISSIVE is nonzero. */ 00957 00958 /* Let's optimize away calls to strlen("operator"). */ 00959 00960 #define LENGTH_OF_OPERATOR 8 00961 00962 static unsigned int 00963 cp_find_first_component_aux (const char *name, int permissive) 00964 { 00965 unsigned int index = 0; 00966 /* Operator names can show up in unexpected places. Since these can 00967 contain parentheses or angle brackets, they can screw up the 00968 recursion. But not every string 'operator' is part of an 00969 operater name: e.g. you could have a variable 'cooperator'. So 00970 this variable tells us whether or not we should treat the string 00971 'operator' as starting an operator. */ 00972 int operator_possible = 1; 00973 00974 for (;; ++index) 00975 { 00976 switch (name[index]) 00977 { 00978 case '<': 00979 /* Template; eat it up. The calls to cp_first_component 00980 should only return (I hope!) when they reach the '>' 00981 terminating the component or a '::' between two 00982 components. (Hence the '+ 2'.) */ 00983 index += 1; 00984 for (index += cp_find_first_component_aux (name + index, 1); 00985 name[index] != '>'; 00986 index += cp_find_first_component_aux (name + index, 1)) 00987 { 00988 if (name[index] != ':') 00989 { 00990 demangled_name_complaint (name); 00991 return strlen (name); 00992 } 00993 index += 2; 00994 } 00995 operator_possible = 1; 00996 break; 00997 case '(': 00998 /* Similar comment as to '<'. */ 00999 index += 1; 01000 for (index += cp_find_first_component_aux (name + index, 1); 01001 name[index] != ')'; 01002 index += cp_find_first_component_aux (name + index, 1)) 01003 { 01004 if (name[index] != ':') 01005 { 01006 demangled_name_complaint (name); 01007 return strlen (name); 01008 } 01009 index += 2; 01010 } 01011 operator_possible = 1; 01012 break; 01013 case '>': 01014 case ')': 01015 if (permissive) 01016 return index; 01017 else 01018 { 01019 demangled_name_complaint (name); 01020 return strlen (name); 01021 } 01022 case '\0': 01023 case ':': 01024 return index; 01025 case 'o': 01026 /* Operator names can screw up the recursion. */ 01027 if (operator_possible 01028 && strncmp (name + index, "operator", 01029 LENGTH_OF_OPERATOR) == 0) 01030 { 01031 index += LENGTH_OF_OPERATOR; 01032 while (ISSPACE(name[index])) 01033 ++index; 01034 switch (name[index]) 01035 { 01036 /* Skip over one less than the appropriate number of 01037 characters: the for loop will skip over the last 01038 one. */ 01039 case '<': 01040 if (name[index + 1] == '<') 01041 index += 1; 01042 else 01043 index += 0; 01044 break; 01045 case '>': 01046 case '-': 01047 if (name[index + 1] == '>') 01048 index += 1; 01049 else 01050 index += 0; 01051 break; 01052 case '(': 01053 index += 1; 01054 break; 01055 default: 01056 index += 0; 01057 break; 01058 } 01059 } 01060 operator_possible = 0; 01061 break; 01062 case ' ': 01063 case ',': 01064 case '.': 01065 case '&': 01066 case '*': 01067 /* NOTE: carlton/2003-04-18: I'm not sure what the precise 01068 set of relevant characters are here: it's necessary to 01069 include any character that can show up before 'operator' 01070 in a demangled name, and it's safe to include any 01071 character that can't be part of an identifier's name. */ 01072 operator_possible = 1; 01073 break; 01074 default: 01075 operator_possible = 0; 01076 break; 01077 } 01078 } 01079 } 01080 01081 /* Complain about a demangled name that we don't know how to parse. 01082 NAME is the demangled name in question. */ 01083 01084 static void 01085 demangled_name_complaint (const char *name) 01086 { 01087 complaint (&symfile_complaints, 01088 "unexpected demangled name '%s'", name); 01089 } 01090 01091 /* If NAME is the fully-qualified name of a C++ 01092 function/variable/method/etc., this returns the length of its 01093 entire prefix: all of the namespaces and classes that make up its 01094 name. Given 'A::foo', it returns 1, given 'A::B::foo', it returns 01095 4, given 'foo', it returns 0. */ 01096 01097 unsigned int 01098 cp_entire_prefix_len (const char *name) 01099 { 01100 unsigned int current_len = cp_find_first_component (name); 01101 unsigned int previous_len = 0; 01102 01103 while (name[current_len] != '\0') 01104 { 01105 gdb_assert (name[current_len] == ':'); 01106 previous_len = current_len; 01107 /* Skip the '::'. */ 01108 current_len += 2; 01109 current_len += cp_find_first_component (name + current_len); 01110 } 01111 01112 return previous_len; 01113 } 01114 01115 /* Overload resolution functions. */ 01116 01117 /* Test to see if SYM is a symbol that we haven't seen corresponding 01118 to a function named OLOAD_NAME. If so, add it to the current 01119 completion list. */ 01120 01121 static void 01122 overload_list_add_symbol (struct symbol *sym, 01123 const char *oload_name) 01124 { 01125 int newsize; 01126 int i; 01127 char *sym_name; 01128 01129 /* If there is no type information, we can't do anything, so 01130 skip. */ 01131 if (SYMBOL_TYPE (sym) == NULL) 01132 return; 01133 01134 /* skip any symbols that we've already considered. */ 01135 for (i = 0; i < sym_return_val_index; ++i) 01136 if (strcmp (SYMBOL_LINKAGE_NAME (sym), 01137 SYMBOL_LINKAGE_NAME (sym_return_val[i])) == 0) 01138 return; 01139 01140 /* Get the demangled name without parameters */ 01141 sym_name = cp_remove_params (SYMBOL_NATURAL_NAME (sym)); 01142 if (!sym_name) 01143 return; 01144 01145 /* skip symbols that cannot match */ 01146 if (strcmp (sym_name, oload_name) != 0) 01147 { 01148 xfree (sym_name); 01149 return; 01150 } 01151 01152 xfree (sym_name); 01153 01154 /* We have a match for an overload instance, so add SYM to the 01155 current list of overload instances */ 01156 if (sym_return_val_index + 3 > sym_return_val_size) 01157 { 01158 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *); 01159 sym_return_val = (struct symbol **) 01160 xrealloc ((char *) sym_return_val, newsize); 01161 } 01162 sym_return_val[sym_return_val_index++] = sym; 01163 sym_return_val[sym_return_val_index] = NULL; 01164 } 01165 01166 /* Return a null-terminated list of pointers to function symbols that 01167 are named FUNC_NAME and are visible within NAMESPACE. */ 01168 01169 struct symbol ** 01170 make_symbol_overload_list (const char *func_name, 01171 const char *namespace) 01172 { 01173 struct cleanup *old_cleanups; 01174 const char *name; 01175 01176 sym_return_val_size = 100; 01177 sym_return_val_index = 0; 01178 sym_return_val = xmalloc ((sym_return_val_size + 1) * 01179 sizeof (struct symbol *)); 01180 sym_return_val[0] = NULL; 01181 01182 old_cleanups = make_cleanup (xfree, sym_return_val); 01183 01184 make_symbol_overload_list_using (func_name, namespace); 01185 01186 if (namespace[0] == '\0') 01187 name = func_name; 01188 else 01189 { 01190 char *concatenated_name 01191 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); 01192 strcpy (concatenated_name, namespace); 01193 strcat (concatenated_name, "::"); 01194 strcat (concatenated_name, func_name); 01195 name = concatenated_name; 01196 } 01197 01198 make_symbol_overload_list_qualified (name); 01199 01200 discard_cleanups (old_cleanups); 01201 01202 return sym_return_val; 01203 } 01204 01205 /* Add all symbols with a name matching NAME in BLOCK to the overload 01206 list. */ 01207 01208 static void 01209 make_symbol_overload_list_block (const char *name, 01210 const struct block *block) 01211 { 01212 struct block_iterator iter; 01213 struct symbol *sym; 01214 01215 for (sym = block_iter_name_first (block, name, &iter); 01216 sym != NULL; 01217 sym = block_iter_name_next (name, &iter)) 01218 overload_list_add_symbol (sym, name); 01219 } 01220 01221 /* Adds the function FUNC_NAME from NAMESPACE to the overload set. */ 01222 01223 static void 01224 make_symbol_overload_list_namespace (const char *func_name, 01225 const char *namespace) 01226 { 01227 const char *name; 01228 const struct block *block = NULL; 01229 01230 if (namespace[0] == '\0') 01231 name = func_name; 01232 else 01233 { 01234 char *concatenated_name 01235 = alloca (strlen (namespace) + 2 + strlen (func_name) + 1); 01236 01237 strcpy (concatenated_name, namespace); 01238 strcat (concatenated_name, "::"); 01239 strcat (concatenated_name, func_name); 01240 name = concatenated_name; 01241 } 01242 01243 /* Look in the static block. */ 01244 block = block_static_block (get_selected_block (0)); 01245 if (block) 01246 make_symbol_overload_list_block (name, block); 01247 01248 /* Look in the global block. */ 01249 block = block_global_block (block); 01250 if (block) 01251 make_symbol_overload_list_block (name, block); 01252 01253 } 01254 01255 /* Search the namespace of the given type and namespace of and public 01256 base types. */ 01257 01258 static void 01259 make_symbol_overload_list_adl_namespace (struct type *type, 01260 const char *func_name) 01261 { 01262 char *namespace; 01263 const char *type_name; 01264 int i, prefix_len; 01265 01266 while (TYPE_CODE (type) == TYPE_CODE_PTR 01267 || TYPE_CODE (type) == TYPE_CODE_REF 01268 || TYPE_CODE (type) == TYPE_CODE_ARRAY 01269 || TYPE_CODE (type) == TYPE_CODE_TYPEDEF) 01270 { 01271 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF) 01272 type = check_typedef(type); 01273 else 01274 type = TYPE_TARGET_TYPE (type); 01275 } 01276 01277 type_name = TYPE_NAME (type); 01278 01279 if (type_name == NULL) 01280 return; 01281 01282 prefix_len = cp_entire_prefix_len (type_name); 01283 01284 if (prefix_len != 0) 01285 { 01286 namespace = alloca (prefix_len + 1); 01287 strncpy (namespace, type_name, prefix_len); 01288 namespace[prefix_len] = '\0'; 01289 01290 make_symbol_overload_list_namespace (func_name, namespace); 01291 } 01292 01293 /* Check public base type */ 01294 if (TYPE_CODE (type) == TYPE_CODE_CLASS) 01295 for (i = 0; i < TYPE_N_BASECLASSES (type); i++) 01296 { 01297 if (BASETYPE_VIA_PUBLIC (type, i)) 01298 make_symbol_overload_list_adl_namespace (TYPE_BASECLASS (type, 01299 i), 01300 func_name); 01301 } 01302 } 01303 01304 /* Adds the overload list overload candidates for FUNC_NAME found 01305 through argument dependent lookup. */ 01306 01307 struct symbol ** 01308 make_symbol_overload_list_adl (struct type **arg_types, int nargs, 01309 const char *func_name) 01310 { 01311 int i; 01312 01313 gdb_assert (sym_return_val_size != -1); 01314 01315 for (i = 1; i <= nargs; i++) 01316 make_symbol_overload_list_adl_namespace (arg_types[i - 1], 01317 func_name); 01318 01319 return sym_return_val; 01320 } 01321 01322 /* Used for cleanups to reset the "searched" flag in case of an 01323 error. */ 01324 01325 static void 01326 reset_directive_searched (void *data) 01327 { 01328 struct using_direct *direct = data; 01329 direct->searched = 0; 01330 } 01331 01332 /* This applies the using directives to add namespaces to search in, 01333 and then searches for overloads in all of those namespaces. It 01334 adds the symbols found to sym_return_val. Arguments are as in 01335 make_symbol_overload_list. */ 01336 01337 static void 01338 make_symbol_overload_list_using (const char *func_name, 01339 const char *namespace) 01340 { 01341 struct using_direct *current; 01342 const struct block *block; 01343 01344 /* First, go through the using directives. If any of them apply, 01345 look in the appropriate namespaces for new functions to match 01346 on. */ 01347 01348 for (block = get_selected_block (0); 01349 block != NULL; 01350 block = BLOCK_SUPERBLOCK (block)) 01351 for (current = block_using (block); 01352 current != NULL; 01353 current = current->next) 01354 { 01355 /* Prevent recursive calls. */ 01356 if (current->searched) 01357 continue; 01358 01359 /* If this is a namespace alias or imported declaration ignore 01360 it. */ 01361 if (current->alias != NULL || current->declaration != NULL) 01362 continue; 01363 01364 if (strcmp (namespace, current->import_dest) == 0) 01365 { 01366 /* Mark this import as searched so that the recursive call 01367 does not search it again. */ 01368 struct cleanup *old_chain; 01369 current->searched = 1; 01370 old_chain = make_cleanup (reset_directive_searched, 01371 current); 01372 01373 make_symbol_overload_list_using (func_name, 01374 current->import_src); 01375 01376 current->searched = 0; 01377 discard_cleanups (old_chain); 01378 } 01379 } 01380 01381 /* Now, add names for this namespace. */ 01382 make_symbol_overload_list_namespace (func_name, namespace); 01383 } 01384 01385 /* This does the bulk of the work of finding overloaded symbols. 01386 FUNC_NAME is the name of the overloaded function we're looking for 01387 (possibly including namespace info). */ 01388 01389 static void 01390 make_symbol_overload_list_qualified (const char *func_name) 01391 { 01392 struct symtab *s; 01393 struct objfile *objfile; 01394 const struct block *b, *surrounding_static_block = 0; 01395 01396 /* Look through the partial symtabs for all symbols which begin by 01397 matching FUNC_NAME. Make sure we read that symbol table in. */ 01398 01399 ALL_OBJFILES (objfile) 01400 { 01401 if (objfile->sf) 01402 objfile->sf->qf->expand_symtabs_for_function (objfile, func_name); 01403 } 01404 01405 /* Search upwards from currently selected frame (so that we can 01406 complete on local vars. */ 01407 01408 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b)) 01409 make_symbol_overload_list_block (func_name, b); 01410 01411 surrounding_static_block = block_static_block (get_selected_block (0)); 01412 01413 /* Go through the symtabs and check the externs and statics for 01414 symbols which match. */ 01415 01416 ALL_PRIMARY_SYMTABS (objfile, s) 01417 { 01418 QUIT; 01419 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK); 01420 make_symbol_overload_list_block (func_name, b); 01421 } 01422 01423 ALL_PRIMARY_SYMTABS (objfile, s) 01424 { 01425 QUIT; 01426 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK); 01427 /* Don't do this block twice. */ 01428 if (b == surrounding_static_block) 01429 continue; 01430 make_symbol_overload_list_block (func_name, b); 01431 } 01432 } 01433 01434 /* Lookup the rtti type for a class name. */ 01435 01436 struct type * 01437 cp_lookup_rtti_type (const char *name, struct block *block) 01438 { 01439 struct symbol * rtti_sym; 01440 struct type * rtti_type; 01441 01442 rtti_sym = lookup_symbol (name, block, STRUCT_DOMAIN, NULL); 01443 01444 if (rtti_sym == NULL) 01445 { 01446 warning (_("RTTI symbol not found for class '%s'"), name); 01447 return NULL; 01448 } 01449 01450 if (SYMBOL_CLASS (rtti_sym) != LOC_TYPEDEF) 01451 { 01452 warning (_("RTTI symbol for class '%s' is not a type"), name); 01453 return NULL; 01454 } 01455 01456 rtti_type = SYMBOL_TYPE (rtti_sym); 01457 01458 switch (TYPE_CODE (rtti_type)) 01459 { 01460 case TYPE_CODE_CLASS: 01461 break; 01462 case TYPE_CODE_NAMESPACE: 01463 /* chastain/2003-11-26: the symbol tables often contain fake 01464 symbols for namespaces with the same name as the struct. 01465 This warning is an indication of a bug in the lookup order 01466 or a bug in the way that the symbol tables are populated. */ 01467 warning (_("RTTI symbol for class '%s' is a namespace"), name); 01468 return NULL; 01469 default: 01470 warning (_("RTTI symbol for class '%s' has bad type"), name); 01471 return NULL; 01472 } 01473 01474 return rtti_type; 01475 } 01476 01477 /* A wrapper for bfd_demangle. */ 01478 01479 char * 01480 gdb_demangle (const char *name, int options) 01481 { 01482 return bfd_demangle (NULL, name, options); 01483 } 01484 01485 /* Don't allow just "maintenance cplus". */ 01486 01487 static void 01488 maint_cplus_command (char *arg, int from_tty) 01489 { 01490 printf_unfiltered (_("\"maintenance cplus\" must be followed " 01491 "by the name of a command.\n")); 01492 help_list (maint_cplus_cmd_list, 01493 "maintenance cplus ", 01494 -1, gdb_stdout); 01495 } 01496 01497 /* This is a front end for cp_find_first_component, for unit testing. 01498 Be careful when using it: see the NOTE above 01499 cp_find_first_component. */ 01500 01501 static void 01502 first_component_command (char *arg, int from_tty) 01503 { 01504 int len; 01505 char *prefix; 01506 01507 if (!arg) 01508 return; 01509 01510 len = cp_find_first_component (arg); 01511 prefix = alloca (len + 1); 01512 01513 memcpy (prefix, arg, len); 01514 prefix[len] = '\0'; 01515 01516 printf_unfiltered ("%s\n", prefix); 01517 } 01518 01519 extern initialize_file_ftype _initialize_cp_support; /* -Wmissing-prototypes */ 01520 01521 01522 /* Implement "info vtbl". */ 01523 01524 static void 01525 info_vtbl_command (char *arg, int from_tty) 01526 { 01527 struct value *value; 01528 01529 value = parse_and_eval (arg); 01530 cplus_print_vtable (value); 01531 } 01532 01533 void 01534 _initialize_cp_support (void) 01535 { 01536 add_prefix_cmd ("cplus", class_maintenance, 01537 maint_cplus_command, 01538 _("C++ maintenance commands."), 01539 &maint_cplus_cmd_list, 01540 "maintenance cplus ", 01541 0, &maintenancelist); 01542 add_alias_cmd ("cp", "cplus", 01543 class_maintenance, 1, 01544 &maintenancelist); 01545 01546 add_cmd ("first_component", 01547 class_maintenance, 01548 first_component_command, 01549 _("Print the first class/namespace component of NAME."), 01550 &maint_cplus_cmd_list); 01551 01552 add_info ("vtbl", info_vtbl_command, 01553 _("Show the virtual function table for a C++ object.\n\ 01554 Usage: info vtbl EXPRESSION\n\ 01555 Evaluate EXPRESSION and display the virtual function table for the\n\ 01556 resulting object.")); 01557 }