| 1 | /* Helper routines for C++ support in GDB. |
| 2 | Copyright (C) 2003-2015 Free Software Foundation, Inc. |
| 3 | |
| 4 | Contributed by David Carlton and by Kealia, Inc. |
| 5 | |
| 6 | This file is part of GDB. |
| 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 3 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 20 | |
| 21 | #include "defs.h" |
| 22 | #include "cp-support.h" |
| 23 | #include "gdb_obstack.h" |
| 24 | #include "symtab.h" |
| 25 | #include "symfile.h" |
| 26 | #include "block.h" |
| 27 | #include "objfiles.h" |
| 28 | #include "gdbtypes.h" |
| 29 | #include "dictionary.h" |
| 30 | #include "command.h" |
| 31 | #include "frame.h" |
| 32 | #include "buildsym.h" |
| 33 | #include "language.h" |
| 34 | #include "namespace.h" |
| 35 | |
| 36 | static struct block_symbol |
| 37 | cp_lookup_nested_symbol_1 (struct type *container_type, |
| 38 | const char *nested_name, |
| 39 | const char *concatenated_name, |
| 40 | const struct block *block, |
| 41 | const domain_enum domain, |
| 42 | int basic_lookup, int is_in_anonymous); |
| 43 | |
| 44 | static struct type *cp_lookup_transparent_type_loop (const char *name, |
| 45 | const char *scope, |
| 46 | int scope_len); |
| 47 | |
| 48 | /* Check to see if SYMBOL refers to an object contained within an |
| 49 | anonymous namespace; if so, add an appropriate using directive. */ |
| 50 | |
| 51 | void |
| 52 | cp_scan_for_anonymous_namespaces (const struct symbol *const symbol, |
| 53 | struct objfile *const objfile) |
| 54 | { |
| 55 | if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) |
| 56 | { |
| 57 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
| 58 | unsigned int previous_component; |
| 59 | unsigned int next_component; |
| 60 | |
| 61 | /* Start with a quick-and-dirty check for mention of "(anonymous |
| 62 | namespace)". */ |
| 63 | |
| 64 | if (!cp_is_in_anonymous (name)) |
| 65 | return; |
| 66 | |
| 67 | previous_component = 0; |
| 68 | next_component = cp_find_first_component (name + previous_component); |
| 69 | |
| 70 | while (name[next_component] == ':') |
| 71 | { |
| 72 | if (((next_component - previous_component) |
| 73 | == CP_ANONYMOUS_NAMESPACE_LEN) |
| 74 | && strncmp (name + previous_component, |
| 75 | CP_ANONYMOUS_NAMESPACE_STR, |
| 76 | CP_ANONYMOUS_NAMESPACE_LEN) == 0) |
| 77 | { |
| 78 | int dest_len = (previous_component == 0 |
| 79 | ? 0 : previous_component - 2); |
| 80 | int src_len = next_component; |
| 81 | |
| 82 | char *dest = alloca (dest_len + 1); |
| 83 | char *src = alloca (src_len + 1); |
| 84 | |
| 85 | memcpy (dest, name, dest_len); |
| 86 | memcpy (src, name, src_len); |
| 87 | |
| 88 | dest[dest_len] = '\0'; |
| 89 | src[src_len] = '\0'; |
| 90 | |
| 91 | /* We've found a component of the name that's an |
| 92 | anonymous namespace. So add symbols in it to the |
| 93 | namespace given by the previous component if there is |
| 94 | one, or to the global namespace if there isn't. */ |
| 95 | add_using_directive (&local_using_directives, |
| 96 | dest, src, NULL, NULL, NULL, 1, |
| 97 | &objfile->objfile_obstack); |
| 98 | } |
| 99 | /* The "+ 2" is for the "::". */ |
| 100 | previous_component = next_component + 2; |
| 101 | next_component = (previous_component |
| 102 | + cp_find_first_component (name |
| 103 | + previous_component)); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /* Test whether or not NAMESPACE looks like it mentions an anonymous |
| 109 | namespace; return nonzero if so. */ |
| 110 | |
| 111 | int |
| 112 | cp_is_in_anonymous (const char *symbol_name) |
| 113 | { |
| 114 | return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR) |
| 115 | != NULL); |
| 116 | } |
| 117 | |
| 118 | /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks. |
| 119 | If IS_IN_ANONYMOUS is nonzero, the symbol in question is located |
| 120 | within an anonymous namespace. */ |
| 121 | |
| 122 | static struct block_symbol |
| 123 | cp_basic_lookup_symbol (const char *name, const struct block *block, |
| 124 | const domain_enum domain, int is_in_anonymous) |
| 125 | { |
| 126 | struct block_symbol sym; |
| 127 | |
| 128 | sym = lookup_symbol_in_static_block (name, block, domain); |
| 129 | if (sym.symbol != NULL) |
| 130 | return sym; |
| 131 | |
| 132 | if (is_in_anonymous) |
| 133 | { |
| 134 | /* Symbols defined in anonymous namespaces have external linkage |
| 135 | but should be treated as local to a single file nonetheless. |
| 136 | So we only search the current file's global block. */ |
| 137 | |
| 138 | const struct block *global_block = block_global_block (block); |
| 139 | |
| 140 | if (global_block != NULL) |
| 141 | { |
| 142 | sym.symbol = lookup_symbol_in_block (name, global_block, domain); |
| 143 | sym.block = global_block; |
| 144 | } |
| 145 | } |
| 146 | else |
| 147 | sym = lookup_global_symbol (name, block, domain); |
| 148 | |
| 149 | return sym; |
| 150 | } |
| 151 | |
| 152 | /* Search bare symbol NAME in DOMAIN in BLOCK. |
| 153 | NAME is guaranteed to not have any scope (no "::") in its name, though |
| 154 | if for example NAME is a template spec then "::" may appear in the |
| 155 | argument list. |
| 156 | If LANGDEF is non-NULL then try to lookup NAME as a primitive type in |
| 157 | that language. Normally we wouldn't need LANGDEF but fortran also uses |
| 158 | this code. |
| 159 | If SEARCH is non-zero then see if we can determine "this" from BLOCK, and |
| 160 | if so then also search for NAME in that class. */ |
| 161 | |
| 162 | static struct block_symbol |
| 163 | cp_lookup_bare_symbol (const struct language_defn *langdef, |
| 164 | const char *name, const struct block *block, |
| 165 | const domain_enum domain, int search) |
| 166 | { |
| 167 | struct block_symbol sym; |
| 168 | |
| 169 | /* Note: We can't do a simple assert for ':' not being in NAME because |
| 170 | ':' may be in the args of a template spec. This isn't intended to be |
| 171 | a complete test, just cheap and documentary. */ |
| 172 | if (strchr (name, '<') == NULL && strchr (name, '(') == NULL) |
| 173 | gdb_assert (strchr (name, ':') == NULL); |
| 174 | |
| 175 | sym = lookup_symbol_in_static_block (name, block, domain); |
| 176 | if (sym.symbol != NULL) |
| 177 | return sym; |
| 178 | |
| 179 | /* If we didn't find a definition for a builtin type in the static block, |
| 180 | search for it now. This is actually the right thing to do and can be |
| 181 | a massive performance win. E.g., when debugging a program with lots of |
| 182 | shared libraries we could search all of them only to find out the |
| 183 | builtin type isn't defined in any of them. This is common for types |
| 184 | like "void". */ |
| 185 | if (langdef != NULL && domain == VAR_DOMAIN) |
| 186 | { |
| 187 | struct gdbarch *gdbarch; |
| 188 | |
| 189 | if (block == NULL) |
| 190 | gdbarch = target_gdbarch (); |
| 191 | else |
| 192 | gdbarch = block_gdbarch (block); |
| 193 | sym.symbol |
| 194 | = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name); |
| 195 | sym.block = NULL; |
| 196 | if (sym.symbol != NULL) |
| 197 | return sym; |
| 198 | } |
| 199 | |
| 200 | sym = lookup_global_symbol (name, block, domain); |
| 201 | if (sym.symbol != NULL) |
| 202 | return sym; |
| 203 | |
| 204 | if (search) |
| 205 | { |
| 206 | struct block_symbol lang_this; |
| 207 | struct type *type; |
| 208 | |
| 209 | lang_this = lookup_language_this (language_def (language_cplus), block); |
| 210 | if (lang_this.symbol == NULL) |
| 211 | return (struct block_symbol) {NULL, NULL}; |
| 212 | |
| 213 | type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this.symbol))); |
| 214 | /* If TYPE_NAME is NULL, abandon trying to find this symbol. |
| 215 | This can happen for lambda functions compiled with clang++, |
| 216 | which outputs no name for the container class. */ |
| 217 | if (TYPE_NAME (type) == NULL) |
| 218 | return (struct block_symbol) {NULL, NULL}; |
| 219 | |
| 220 | /* Look for symbol NAME in this class. */ |
| 221 | sym = cp_lookup_nested_symbol (type, name, block, domain); |
| 222 | } |
| 223 | |
| 224 | return sym; |
| 225 | } |
| 226 | |
| 227 | /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses. |
| 228 | BLOCK specifies the context in which to perform the search. |
| 229 | NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies |
| 230 | the length of the entire scope of NAME (up to, but not including, the last |
| 231 | "::". |
| 232 | |
| 233 | Note: At least in the case of Fortran, which also uses this code, there |
| 234 | may be no text after the last "::". */ |
| 235 | |
| 236 | static struct block_symbol |
| 237 | cp_search_static_and_baseclasses (const char *name, |
| 238 | const struct block *block, |
| 239 | const domain_enum domain, |
| 240 | unsigned int prefix_len, |
| 241 | int is_in_anonymous) |
| 242 | { |
| 243 | struct block_symbol sym; |
| 244 | char *klass, *nested; |
| 245 | struct cleanup *cleanup; |
| 246 | struct block_symbol klass_sym; |
| 247 | struct type *klass_type; |
| 248 | |
| 249 | /* The test here uses <= instead of < because Fortran also uses this, |
| 250 | and the module.exp testcase will pass "modmany::" for NAME here. */ |
| 251 | gdb_assert (prefix_len + 2 <= strlen (name)); |
| 252 | gdb_assert (name[prefix_len + 1] == ':'); |
| 253 | |
| 254 | /* Find the name of the class and the name of the method, variable, etc. */ |
| 255 | |
| 256 | /* The class name is everything up to and including PREFIX_LEN. */ |
| 257 | klass = savestring (name, prefix_len); |
| 258 | |
| 259 | /* The rest of the name is everything else past the initial scope |
| 260 | operator. */ |
| 261 | nested = xstrdup (name + prefix_len + 2); |
| 262 | |
| 263 | /* Add cleanups to free memory for these strings. */ |
| 264 | cleanup = make_cleanup (xfree, klass); |
| 265 | make_cleanup (xfree, nested); |
| 266 | |
| 267 | /* Lookup a class named KLASS. If none is found, there is nothing |
| 268 | more that can be done. KLASS could be a namespace, so always look |
| 269 | in VAR_DOMAIN. This works for classes too because of |
| 270 | symbol_matches_domain (which should be replaced with something else, |
| 271 | but it's what we have today). */ |
| 272 | klass_sym = lookup_global_symbol (klass, block, VAR_DOMAIN); |
| 273 | if (klass_sym.symbol == NULL) |
| 274 | { |
| 275 | do_cleanups (cleanup); |
| 276 | return (struct block_symbol) {NULL, NULL}; |
| 277 | } |
| 278 | klass_type = SYMBOL_TYPE (klass_sym.symbol); |
| 279 | |
| 280 | /* Look for a symbol named NESTED in this class. |
| 281 | The caller is assumed to have already have done a basic lookup of NAME. |
| 282 | So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */ |
| 283 | sym = cp_lookup_nested_symbol_1 (klass_type, nested, name, block, domain, |
| 284 | 0, is_in_anonymous); |
| 285 | |
| 286 | do_cleanups (cleanup); |
| 287 | return sym; |
| 288 | } |
| 289 | |
| 290 | /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are |
| 291 | as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search |
| 292 | through base classes for a matching symbol. |
| 293 | |
| 294 | Note: Part of the complexity is because NAME may itself specify scope. |
| 295 | Part of the complexity is also because this handles the case where |
| 296 | there is no scoping in which case we also try looking in the class of |
| 297 | "this" if we can compute it. */ |
| 298 | |
| 299 | static struct block_symbol |
| 300 | cp_lookup_symbol_in_namespace (const char *the_namespace, const char *name, |
| 301 | const struct block *block, |
| 302 | const domain_enum domain, int search) |
| 303 | { |
| 304 | char *concatenated_name = NULL; |
| 305 | int is_in_anonymous; |
| 306 | unsigned int prefix_len; |
| 307 | struct block_symbol sym; |
| 308 | |
| 309 | if (the_namespace[0] != '\0') |
| 310 | { |
| 311 | concatenated_name = alloca (strlen (the_namespace) + 2 |
| 312 | + strlen (name) + 1); |
| 313 | strcpy (concatenated_name, the_namespace); |
| 314 | strcat (concatenated_name, "::"); |
| 315 | strcat (concatenated_name, name); |
| 316 | name = concatenated_name; |
| 317 | } |
| 318 | |
| 319 | prefix_len = cp_entire_prefix_len (name); |
| 320 | if (prefix_len == 0) |
| 321 | return cp_lookup_bare_symbol (NULL, name, block, domain, search); |
| 322 | |
| 323 | /* This would be simpler if we just called cp_lookup_nested_symbol |
| 324 | at this point. But that would require first looking up the containing |
| 325 | class/namespace. Since we're only searching static and global blocks |
| 326 | there's often no need to first do that lookup. */ |
| 327 | |
| 328 | is_in_anonymous |
| 329 | = the_namespace[0] != '\0' && cp_is_in_anonymous (the_namespace); |
| 330 | sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous); |
| 331 | if (sym.symbol != NULL) |
| 332 | return sym; |
| 333 | |
| 334 | if (search) |
| 335 | sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len, |
| 336 | is_in_anonymous); |
| 337 | |
| 338 | return sym; |
| 339 | } |
| 340 | |
| 341 | /* Used for cleanups to reset the "searched" flag in case of an error. */ |
| 342 | |
| 343 | static void |
| 344 | reset_directive_searched (void *data) |
| 345 | { |
| 346 | struct using_direct *direct = data; |
| 347 | direct->searched = 0; |
| 348 | } |
| 349 | |
| 350 | /* Search for NAME by applying all import statements belonging to |
| 351 | BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the |
| 352 | search is restricted to using declarations. |
| 353 | Example: |
| 354 | |
| 355 | namespace A { |
| 356 | int x; |
| 357 | } |
| 358 | using A::x; |
| 359 | |
| 360 | If SEARCH_PARENTS the search will include imports which are |
| 361 | applicable in parents of SCOPE. |
| 362 | Example: |
| 363 | |
| 364 | namespace A { |
| 365 | using namespace X; |
| 366 | namespace B { |
| 367 | using namespace Y; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of |
| 372 | namespaces X and Y will be considered. If SEARCH_PARENTS is false |
| 373 | only the import of Y is considered. |
| 374 | |
| 375 | SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must |
| 376 | pass 0 for it. Internally we pass 1 when recursing. */ |
| 377 | |
| 378 | static struct block_symbol |
| 379 | cp_lookup_symbol_via_imports (const char *scope, |
| 380 | const char *name, |
| 381 | const struct block *block, |
| 382 | const domain_enum domain, |
| 383 | const int search_scope_first, |
| 384 | const int declaration_only, |
| 385 | const int search_parents) |
| 386 | { |
| 387 | struct using_direct *current; |
| 388 | struct block_symbol sym; |
| 389 | int len; |
| 390 | int directive_match; |
| 391 | struct cleanup *searched_cleanup; |
| 392 | |
| 393 | sym.symbol = NULL; |
| 394 | sym.block = NULL; |
| 395 | |
| 396 | /* First, try to find the symbol in the given namespace if requested. */ |
| 397 | if (search_scope_first) |
| 398 | sym = cp_lookup_symbol_in_namespace (scope, name, |
| 399 | block, domain, 1); |
| 400 | |
| 401 | if (sym.symbol != NULL) |
| 402 | return sym; |
| 403 | |
| 404 | /* Go through the using directives. If any of them add new names to |
| 405 | the namespace we're searching in, see if we can find a match by |
| 406 | applying them. */ |
| 407 | |
| 408 | for (current = block_using (block); |
| 409 | current != NULL; |
| 410 | current = current->next) |
| 411 | { |
| 412 | const char **excludep; |
| 413 | |
| 414 | len = strlen (current->import_dest); |
| 415 | directive_match = (search_parents |
| 416 | ? (startswith (scope, current->import_dest) |
| 417 | && (len == 0 |
| 418 | || scope[len] == ':' |
| 419 | || scope[len] == '\0')) |
| 420 | : strcmp (scope, current->import_dest) == 0); |
| 421 | |
| 422 | /* If the import destination is the current scope or one of its |
| 423 | ancestors then it is applicable. */ |
| 424 | if (directive_match && !current->searched) |
| 425 | { |
| 426 | /* Mark this import as searched so that the recursive call |
| 427 | does not search it again. */ |
| 428 | current->searched = 1; |
| 429 | searched_cleanup = make_cleanup (reset_directive_searched, |
| 430 | current); |
| 431 | |
| 432 | /* If there is an import of a single declaration, compare the |
| 433 | imported declaration (after optional renaming by its alias) |
| 434 | with the sought out name. If there is a match pass |
| 435 | current->import_src as NAMESPACE to direct the search |
| 436 | towards the imported namespace. */ |
| 437 | if (current->declaration |
| 438 | && strcmp (name, current->alias |
| 439 | ? current->alias : current->declaration) == 0) |
| 440 | sym = cp_lookup_symbol_in_namespace (current->import_src, |
| 441 | current->declaration, |
| 442 | block, domain, 1); |
| 443 | |
| 444 | /* If this is a DECLARATION_ONLY search or a symbol was found |
| 445 | or this import statement was an import declaration, the |
| 446 | search of this import is complete. */ |
| 447 | if (declaration_only || sym.symbol != NULL || current->declaration) |
| 448 | { |
| 449 | current->searched = 0; |
| 450 | discard_cleanups (searched_cleanup); |
| 451 | |
| 452 | if (sym.symbol != NULL) |
| 453 | return sym; |
| 454 | |
| 455 | continue; |
| 456 | } |
| 457 | |
| 458 | /* Do not follow CURRENT if NAME matches its EXCLUDES. */ |
| 459 | for (excludep = current->excludes; *excludep; excludep++) |
| 460 | if (strcmp (name, *excludep) == 0) |
| 461 | break; |
| 462 | if (*excludep) |
| 463 | { |
| 464 | discard_cleanups (searched_cleanup); |
| 465 | continue; |
| 466 | } |
| 467 | |
| 468 | if (current->alias != NULL |
| 469 | && strcmp (name, current->alias) == 0) |
| 470 | /* If the import is creating an alias and the alias matches |
| 471 | the sought name. Pass current->import_src as the NAME to |
| 472 | direct the search towards the aliased namespace. */ |
| 473 | { |
| 474 | sym = cp_lookup_symbol_in_namespace (scope, |
| 475 | current->import_src, |
| 476 | block, domain, 1); |
| 477 | } |
| 478 | else if (current->alias == NULL) |
| 479 | { |
| 480 | /* If this import statement creates no alias, pass |
| 481 | current->inner as NAMESPACE to direct the search |
| 482 | towards the imported namespace. */ |
| 483 | sym = cp_lookup_symbol_via_imports (current->import_src, |
| 484 | name, block, |
| 485 | domain, 1, 0, 0); |
| 486 | } |
| 487 | current->searched = 0; |
| 488 | discard_cleanups (searched_cleanup); |
| 489 | |
| 490 | if (sym.symbol != NULL) |
| 491 | return sym; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | return (struct block_symbol) {NULL, NULL}; |
| 496 | } |
| 497 | |
| 498 | /* Helper function that searches an array of symbols for one named NAME. */ |
| 499 | |
| 500 | static struct symbol * |
| 501 | search_symbol_list (const char *name, int num, |
| 502 | struct symbol **syms) |
| 503 | { |
| 504 | int i; |
| 505 | |
| 506 | /* Maybe we should store a dictionary in here instead. */ |
| 507 | for (i = 0; i < num; ++i) |
| 508 | { |
| 509 | if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0) |
| 510 | return syms[i]; |
| 511 | } |
| 512 | return NULL; |
| 513 | } |
| 514 | |
| 515 | /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it |
| 516 | searches through the template parameters of the function and the |
| 517 | function's type. */ |
| 518 | |
| 519 | struct block_symbol |
| 520 | cp_lookup_symbol_imports_or_template (const char *scope, |
| 521 | const char *name, |
| 522 | const struct block *block, |
| 523 | const domain_enum domain) |
| 524 | { |
| 525 | struct symbol *function = BLOCK_FUNCTION (block); |
| 526 | struct block_symbol result; |
| 527 | |
| 528 | if (symbol_lookup_debug) |
| 529 | { |
| 530 | fprintf_unfiltered (gdb_stdlog, |
| 531 | "cp_lookup_symbol_imports_or_template" |
| 532 | " (%s, %s, %s, %s)\n", |
| 533 | scope, name, host_address_to_string (block), |
| 534 | domain_name (domain)); |
| 535 | } |
| 536 | |
| 537 | if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus) |
| 538 | { |
| 539 | /* Search the function's template parameters. */ |
| 540 | if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function)) |
| 541 | { |
| 542 | struct template_symbol *templ |
| 543 | = (struct template_symbol *) function; |
| 544 | struct symbol *sym = search_symbol_list (name, |
| 545 | templ->n_template_arguments, |
| 546 | templ->template_arguments); |
| 547 | |
| 548 | if (sym != NULL) |
| 549 | { |
| 550 | if (symbol_lookup_debug) |
| 551 | { |
| 552 | fprintf_unfiltered (gdb_stdlog, |
| 553 | "cp_lookup_symbol_imports_or_template" |
| 554 | " (...) = %s\n", |
| 555 | host_address_to_string (sym)); |
| 556 | } |
| 557 | return (struct block_symbol) {sym, block}; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | /* Search the template parameters of the function's defining |
| 562 | context. */ |
| 563 | if (SYMBOL_NATURAL_NAME (function)) |
| 564 | { |
| 565 | struct type *context; |
| 566 | char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function)); |
| 567 | struct cleanup *cleanups = make_cleanup (xfree, name_copy); |
| 568 | const struct language_defn *lang = language_def (language_cplus); |
| 569 | struct gdbarch *arch = symbol_arch (function); |
| 570 | const struct block *parent = BLOCK_SUPERBLOCK (block); |
| 571 | struct symbol *sym; |
| 572 | |
| 573 | while (1) |
| 574 | { |
| 575 | unsigned int prefix_len = cp_entire_prefix_len (name_copy); |
| 576 | |
| 577 | if (prefix_len == 0) |
| 578 | context = NULL; |
| 579 | else |
| 580 | { |
| 581 | name_copy[prefix_len] = '\0'; |
| 582 | context = lookup_typename (lang, arch, |
| 583 | name_copy, |
| 584 | parent, 1); |
| 585 | } |
| 586 | |
| 587 | if (context == NULL) |
| 588 | break; |
| 589 | |
| 590 | sym |
| 591 | = search_symbol_list (name, |
| 592 | TYPE_N_TEMPLATE_ARGUMENTS (context), |
| 593 | TYPE_TEMPLATE_ARGUMENTS (context)); |
| 594 | if (sym != NULL) |
| 595 | { |
| 596 | do_cleanups (cleanups); |
| 597 | if (symbol_lookup_debug) |
| 598 | { |
| 599 | fprintf_unfiltered |
| 600 | (gdb_stdlog, |
| 601 | "cp_lookup_symbol_imports_or_template (...) = %s\n", |
| 602 | host_address_to_string (sym)); |
| 603 | } |
| 604 | return (struct block_symbol) {sym, parent}; |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | do_cleanups (cleanups); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | result = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1, 1); |
| 613 | if (symbol_lookup_debug) |
| 614 | { |
| 615 | fprintf_unfiltered (gdb_stdlog, |
| 616 | "cp_lookup_symbol_imports_or_template (...) = %s\n", |
| 617 | result.symbol != NULL |
| 618 | ? host_address_to_string (result.symbol) : "NULL"); |
| 619 | } |
| 620 | return result; |
| 621 | } |
| 622 | |
| 623 | /* Search for NAME by applying relevant import statements belonging to BLOCK |
| 624 | and its parents. SCOPE is the namespace scope of the context in which the |
| 625 | search is being evaluated. */ |
| 626 | |
| 627 | static struct block_symbol |
| 628 | cp_lookup_symbol_via_all_imports (const char *scope, const char *name, |
| 629 | const struct block *block, |
| 630 | const domain_enum domain) |
| 631 | { |
| 632 | struct block_symbol sym; |
| 633 | |
| 634 | while (block != NULL) |
| 635 | { |
| 636 | sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 0, 1); |
| 637 | if (sym.symbol) |
| 638 | return sym; |
| 639 | |
| 640 | block = BLOCK_SUPERBLOCK (block); |
| 641 | } |
| 642 | |
| 643 | return (struct block_symbol) {NULL, NULL}; |
| 644 | } |
| 645 | |
| 646 | /* Searches for NAME in the current namespace, and by applying |
| 647 | relevant import statements belonging to BLOCK and its parents. |
| 648 | SCOPE is the namespace scope of the context in which the search is |
| 649 | being evaluated. */ |
| 650 | |
| 651 | struct block_symbol |
| 652 | cp_lookup_symbol_namespace (const char *scope, |
| 653 | const char *name, |
| 654 | const struct block *block, |
| 655 | const domain_enum domain) |
| 656 | { |
| 657 | struct block_symbol sym; |
| 658 | |
| 659 | if (symbol_lookup_debug) |
| 660 | { |
| 661 | fprintf_unfiltered (gdb_stdlog, |
| 662 | "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n", |
| 663 | scope, name, host_address_to_string (block), |
| 664 | domain_name (domain)); |
| 665 | } |
| 666 | |
| 667 | /* First, try to find the symbol in the given namespace. */ |
| 668 | sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1); |
| 669 | |
| 670 | /* Search for name in namespaces imported to this and parent blocks. */ |
| 671 | if (sym.symbol == NULL) |
| 672 | sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain); |
| 673 | |
| 674 | if (symbol_lookup_debug) |
| 675 | { |
| 676 | fprintf_unfiltered (gdb_stdlog, |
| 677 | "cp_lookup_symbol_namespace (...) = %s\n", |
| 678 | sym.symbol != NULL |
| 679 | ? host_address_to_string (sym.symbol) : "NULL"); |
| 680 | } |
| 681 | return sym; |
| 682 | } |
| 683 | |
| 684 | /* Lookup NAME at namespace scope (or, in C terms, in static and |
| 685 | global variables). SCOPE is the namespace that the current |
| 686 | function is defined within; only consider namespaces whose length |
| 687 | is at least SCOPE_LEN. Other arguments are as in |
| 688 | cp_lookup_symbol_nonlocal. |
| 689 | |
| 690 | For example, if we're within a function A::B::f and looking for a |
| 691 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
| 692 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
| 693 | but with SCOPE_LEN = 1. And then it calls itself with NAME and |
| 694 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for |
| 695 | "A::B::x"; if it doesn't find it, then the second call looks for |
| 696 | "A::x", and if that call fails, then the first call looks for |
| 697 | "x". */ |
| 698 | |
| 699 | static struct block_symbol |
| 700 | lookup_namespace_scope (const struct language_defn *langdef, |
| 701 | const char *name, |
| 702 | const struct block *block, |
| 703 | const domain_enum domain, |
| 704 | const char *scope, |
| 705 | int scope_len) |
| 706 | { |
| 707 | char *the_namespace; |
| 708 | |
| 709 | if (scope[scope_len] != '\0') |
| 710 | { |
| 711 | /* Recursively search for names in child namespaces first. */ |
| 712 | |
| 713 | struct block_symbol sym; |
| 714 | int new_scope_len = scope_len; |
| 715 | |
| 716 | /* If the current scope is followed by "::", skip past that. */ |
| 717 | if (new_scope_len != 0) |
| 718 | { |
| 719 | gdb_assert (scope[new_scope_len] == ':'); |
| 720 | new_scope_len += 2; |
| 721 | } |
| 722 | new_scope_len += cp_find_first_component (scope + new_scope_len); |
| 723 | sym = lookup_namespace_scope (langdef, name, block, domain, |
| 724 | scope, new_scope_len); |
| 725 | if (sym.symbol != NULL) |
| 726 | return sym; |
| 727 | } |
| 728 | |
| 729 | /* Okay, we didn't find a match in our children, so look for the |
| 730 | name in the current namespace. |
| 731 | |
| 732 | If we there is no scope and we know we have a bare symbol, then short |
| 733 | circuit everything and call cp_lookup_bare_symbol directly. |
| 734 | This isn't an optimization, rather it allows us to pass LANGDEF which |
| 735 | is needed for primitive type lookup. The test doesn't have to be |
| 736 | perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a |
| 737 | template symbol with "::" in the argument list) then |
| 738 | cp_lookup_symbol_in_namespace will catch it. */ |
| 739 | |
| 740 | if (scope_len == 0 && strchr (name, ':') == NULL) |
| 741 | return cp_lookup_bare_symbol (langdef, name, block, domain, 1); |
| 742 | |
| 743 | the_namespace = alloca (scope_len + 1); |
| 744 | strncpy (the_namespace, scope, scope_len); |
| 745 | the_namespace[scope_len] = '\0'; |
| 746 | return cp_lookup_symbol_in_namespace (the_namespace, name, |
| 747 | block, domain, 1); |
| 748 | } |
| 749 | |
| 750 | /* The C++-specific version of name lookup for static and global |
| 751 | names. This makes sure that names get looked for in all namespaces |
| 752 | that are in scope. NAME is the natural name of the symbol that |
| 753 | we're looking for, BLOCK is the block that we're searching within, |
| 754 | DOMAIN says what kind of symbols we're looking for. */ |
| 755 | |
| 756 | struct block_symbol |
| 757 | cp_lookup_symbol_nonlocal (const struct language_defn *langdef, |
| 758 | const char *name, |
| 759 | const struct block *block, |
| 760 | const domain_enum domain) |
| 761 | { |
| 762 | struct block_symbol sym; |
| 763 | const char *scope = block_scope (block); |
| 764 | |
| 765 | if (symbol_lookup_debug) |
| 766 | { |
| 767 | fprintf_unfiltered (gdb_stdlog, |
| 768 | "cp_lookup_symbol_non_local" |
| 769 | " (%s, %s (scope %s), %s)\n", |
| 770 | name, host_address_to_string (block), scope, |
| 771 | domain_name (domain)); |
| 772 | } |
| 773 | |
| 774 | /* First, try to find the symbol in the given namespace, and all |
| 775 | containing namespaces. */ |
| 776 | sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0); |
| 777 | |
| 778 | /* Search for name in namespaces imported to this and parent blocks. */ |
| 779 | if (sym.symbol == NULL) |
| 780 | sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain); |
| 781 | |
| 782 | if (symbol_lookup_debug) |
| 783 | { |
| 784 | fprintf_unfiltered (gdb_stdlog, |
| 785 | "cp_lookup_symbol_nonlocal (...) = %s\n", |
| 786 | (sym.symbol != NULL |
| 787 | ? host_address_to_string (sym.symbol) |
| 788 | : "NULL")); |
| 789 | } |
| 790 | return sym; |
| 791 | } |
| 792 | |
| 793 | /* Search through the base classes of PARENT_TYPE for a base class |
| 794 | named NAME and return its type. If not found, return NULL. */ |
| 795 | |
| 796 | struct type * |
| 797 | cp_find_type_baseclass_by_name (struct type *parent_type, const char *name) |
| 798 | { |
| 799 | int i; |
| 800 | |
| 801 | parent_type = check_typedef (parent_type); |
| 802 | for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i) |
| 803 | { |
| 804 | struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i)); |
| 805 | const char *base_name = TYPE_BASECLASS_NAME (parent_type, i); |
| 806 | |
| 807 | if (base_name == NULL) |
| 808 | continue; |
| 809 | |
| 810 | if (streq (base_name, name)) |
| 811 | return type; |
| 812 | |
| 813 | type = cp_find_type_baseclass_by_name (type, name); |
| 814 | if (type != NULL) |
| 815 | return type; |
| 816 | } |
| 817 | |
| 818 | return NULL; |
| 819 | } |
| 820 | |
| 821 | /* Search through the base classes of PARENT_TYPE for a symbol named |
| 822 | NAME in block BLOCK. */ |
| 823 | |
| 824 | static struct block_symbol |
| 825 | find_symbol_in_baseclass (struct type *parent_type, const char *name, |
| 826 | const struct block *block, const domain_enum domain, |
| 827 | int is_in_anonymous) |
| 828 | { |
| 829 | int i; |
| 830 | struct block_symbol sym; |
| 831 | struct cleanup *cleanup; |
| 832 | char *concatenated_name; |
| 833 | |
| 834 | sym.symbol = NULL; |
| 835 | sym.block = NULL; |
| 836 | concatenated_name = NULL; |
| 837 | cleanup = make_cleanup (free_current_contents, &concatenated_name); |
| 838 | |
| 839 | for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i) |
| 840 | { |
| 841 | size_t len; |
| 842 | struct type *base_type = TYPE_BASECLASS (parent_type, i); |
| 843 | const char *base_name = TYPE_BASECLASS_NAME (parent_type, i); |
| 844 | |
| 845 | if (base_name == NULL) |
| 846 | continue; |
| 847 | |
| 848 | len = strlen (base_name) + 2 + strlen (name) + 1; |
| 849 | concatenated_name = xrealloc (concatenated_name, len); |
| 850 | xsnprintf (concatenated_name, len, "%s::%s", base_name, name); |
| 851 | |
| 852 | sym = cp_lookup_nested_symbol_1 (base_type, name, concatenated_name, |
| 853 | block, domain, 1, is_in_anonymous); |
| 854 | if (sym.symbol != NULL) |
| 855 | break; |
| 856 | } |
| 857 | |
| 858 | do_cleanups (cleanup); |
| 859 | return sym; |
| 860 | } |
| 861 | |
| 862 | /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN |
| 863 | and within the context of BLOCK. |
| 864 | NESTED_NAME may have scope ("::"). |
| 865 | CONTAINER_TYPE needn't have been "check_typedef'd" yet. |
| 866 | CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is |
| 867 | passed as an argument so that callers can control how space for it is |
| 868 | allocated. |
| 869 | If BASIC_LOOKUP is non-zero then perform a basic lookup of |
| 870 | CONCATENATED_NAME. See cp_basic_lookup_symbol for details. |
| 871 | If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous |
| 872 | namespace. */ |
| 873 | |
| 874 | static struct block_symbol |
| 875 | cp_lookup_nested_symbol_1 (struct type *container_type, |
| 876 | const char *nested_name, |
| 877 | const char *concatenated_name, |
| 878 | const struct block *block, |
| 879 | const domain_enum domain, |
| 880 | int basic_lookup, int is_in_anonymous) |
| 881 | { |
| 882 | struct block_symbol sym; |
| 883 | |
| 884 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members |
| 885 | of classes like, say, data or function members. Instead, |
| 886 | they're just represented by symbols whose names are |
| 887 | qualified by the name of the surrounding class. This is |
| 888 | just like members of namespaces; in particular, |
| 889 | cp_basic_lookup_symbol works when looking them up. */ |
| 890 | |
| 891 | if (basic_lookup) |
| 892 | { |
| 893 | sym = cp_basic_lookup_symbol (concatenated_name, block, domain, |
| 894 | is_in_anonymous); |
| 895 | if (sym.symbol != NULL) |
| 896 | return sym; |
| 897 | } |
| 898 | |
| 899 | /* Now search all static file-level symbols. We have to do this for things |
| 900 | like typedefs in the class. We do not try to guess any imported |
| 901 | namespace as even the fully specified namespace search is already not |
| 902 | C++ compliant and more assumptions could make it too magic. */ |
| 903 | |
| 904 | /* First search in this symtab, what we want is possibly there. */ |
| 905 | sym = lookup_symbol_in_static_block (concatenated_name, block, domain); |
| 906 | if (sym.symbol != NULL) |
| 907 | return sym; |
| 908 | |
| 909 | /* Nope. We now have to search all static blocks in all objfiles, |
| 910 | even if block != NULL, because there's no guarantees as to which |
| 911 | symtab the symbol we want is in. Except for symbols defined in |
| 912 | anonymous namespaces should be treated as local to a single file, |
| 913 | which we just searched. */ |
| 914 | if (!is_in_anonymous) |
| 915 | { |
| 916 | sym = lookup_static_symbol (concatenated_name, domain); |
| 917 | if (sym.symbol != NULL) |
| 918 | return sym; |
| 919 | } |
| 920 | |
| 921 | /* If this is a class with baseclasses, search them next. */ |
| 922 | container_type = check_typedef (container_type); |
| 923 | if (TYPE_N_BASECLASSES (container_type) > 0) |
| 924 | { |
| 925 | sym = find_symbol_in_baseclass (container_type, nested_name, block, |
| 926 | domain, is_in_anonymous); |
| 927 | if (sym.symbol != NULL) |
| 928 | return sym; |
| 929 | } |
| 930 | |
| 931 | return (struct block_symbol) {NULL, NULL}; |
| 932 | } |
| 933 | |
| 934 | /* Look up a symbol named NESTED_NAME that is nested inside the C++ |
| 935 | class or namespace given by PARENT_TYPE, from within the context |
| 936 | given by BLOCK, and in DOMAIN. |
| 937 | Return NULL if there is no such nested symbol. */ |
| 938 | |
| 939 | struct block_symbol |
| 940 | cp_lookup_nested_symbol (struct type *parent_type, |
| 941 | const char *nested_name, |
| 942 | const struct block *block, |
| 943 | const domain_enum domain) |
| 944 | { |
| 945 | /* type_name_no_tag_or_error provides better error reporting using the |
| 946 | original type. */ |
| 947 | struct type *saved_parent_type = parent_type; |
| 948 | |
| 949 | parent_type = check_typedef (parent_type); |
| 950 | |
| 951 | if (symbol_lookup_debug) |
| 952 | { |
| 953 | const char *type_name = type_name_no_tag (saved_parent_type); |
| 954 | |
| 955 | fprintf_unfiltered (gdb_stdlog, |
| 956 | "cp_lookup_nested_symbol (%s, %s, %s, %s)\n", |
| 957 | type_name != NULL ? type_name : "unnamed", |
| 958 | nested_name, host_address_to_string (block), |
| 959 | domain_name (domain)); |
| 960 | } |
| 961 | |
| 962 | switch (TYPE_CODE (parent_type)) |
| 963 | { |
| 964 | case TYPE_CODE_STRUCT: |
| 965 | case TYPE_CODE_NAMESPACE: |
| 966 | case TYPE_CODE_UNION: |
| 967 | case TYPE_CODE_ENUM: |
| 968 | /* NOTE: Handle modules here as well, because Fortran is re-using the C++ |
| 969 | specific code to lookup nested symbols in modules, by calling the |
| 970 | function pointer la_lookup_symbol_nonlocal, which ends up here. */ |
| 971 | case TYPE_CODE_MODULE: |
| 972 | { |
| 973 | int size; |
| 974 | const char *parent_name = type_name_no_tag_or_error (saved_parent_type); |
| 975 | struct block_symbol sym; |
| 976 | char *concatenated_name; |
| 977 | int is_in_anonymous; |
| 978 | |
| 979 | size = strlen (parent_name) + 2 + strlen (nested_name) + 1; |
| 980 | concatenated_name = alloca (size); |
| 981 | xsnprintf (concatenated_name, size, "%s::%s", |
| 982 | parent_name, nested_name); |
| 983 | is_in_anonymous = cp_is_in_anonymous (concatenated_name); |
| 984 | |
| 985 | sym = cp_lookup_nested_symbol_1 (parent_type, nested_name, |
| 986 | concatenated_name, block, domain, |
| 987 | 1, is_in_anonymous); |
| 988 | |
| 989 | if (symbol_lookup_debug) |
| 990 | { |
| 991 | fprintf_unfiltered (gdb_stdlog, |
| 992 | "cp_lookup_nested_symbol (...) = %s\n", |
| 993 | (sym.symbol != NULL |
| 994 | ? host_address_to_string (sym.symbol) |
| 995 | : "NULL")); |
| 996 | } |
| 997 | return sym; |
| 998 | } |
| 999 | |
| 1000 | case TYPE_CODE_FUNC: |
| 1001 | case TYPE_CODE_METHOD: |
| 1002 | if (symbol_lookup_debug) |
| 1003 | { |
| 1004 | fprintf_unfiltered (gdb_stdlog, |
| 1005 | "cp_lookup_nested_symbol (...) = NULL" |
| 1006 | " (func/method)\n"); |
| 1007 | } |
| 1008 | return (struct block_symbol) {NULL, NULL}; |
| 1009 | |
| 1010 | default: |
| 1011 | internal_error (__FILE__, __LINE__, |
| 1012 | _("cp_lookup_nested_symbol called " |
| 1013 | "on a non-aggregate type.")); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /* The C++-version of lookup_transparent_type. */ |
| 1018 | |
| 1019 | /* FIXME: carlton/2004-01-16: The problem that this is trying to |
| 1020 | address is that, unfortunately, sometimes NAME is wrong: it may not |
| 1021 | include the name of namespaces enclosing the type in question. |
| 1022 | lookup_transparent_type gets called when the type in question |
| 1023 | is a declaration, and we're trying to find its definition; but, for |
| 1024 | declarations, our type name deduction mechanism doesn't work. |
| 1025 | There's nothing we can do to fix this in general, I think, in the |
| 1026 | absence of debug information about namespaces (I've filed PR |
| 1027 | gdb/1511 about this); until such debug information becomes more |
| 1028 | prevalent, one heuristic which sometimes looks is to search for the |
| 1029 | definition in namespaces containing the current namespace. |
| 1030 | |
| 1031 | We should delete this functions once the appropriate debug |
| 1032 | information becomes more widespread. (GCC 3.4 will be the first |
| 1033 | released version of GCC with such information.) */ |
| 1034 | |
| 1035 | struct type * |
| 1036 | cp_lookup_transparent_type (const char *name) |
| 1037 | { |
| 1038 | /* First, try the honest way of looking up the definition. */ |
| 1039 | struct type *t = basic_lookup_transparent_type (name); |
| 1040 | const char *scope; |
| 1041 | |
| 1042 | if (t != NULL) |
| 1043 | return t; |
| 1044 | |
| 1045 | /* If that doesn't work and we're within a namespace, look there |
| 1046 | instead. */ |
| 1047 | scope = block_scope (get_selected_block (0)); |
| 1048 | |
| 1049 | if (scope[0] == '\0') |
| 1050 | return NULL; |
| 1051 | |
| 1052 | return cp_lookup_transparent_type_loop (name, scope, 0); |
| 1053 | } |
| 1054 | |
| 1055 | /* Lookup the type definition associated to NAME in namespaces/classes |
| 1056 | containing SCOPE whose name is strictly longer than LENGTH. LENGTH |
| 1057 | must be the index of the start of a component of SCOPE. */ |
| 1058 | |
| 1059 | static struct type * |
| 1060 | cp_lookup_transparent_type_loop (const char *name, |
| 1061 | const char *scope, |
| 1062 | int length) |
| 1063 | { |
| 1064 | int scope_length = length + cp_find_first_component (scope + length); |
| 1065 | char *full_name; |
| 1066 | |
| 1067 | /* If the current scope is followed by "::", look in the next |
| 1068 | component. */ |
| 1069 | if (scope[scope_length] == ':') |
| 1070 | { |
| 1071 | struct type *retval |
| 1072 | = cp_lookup_transparent_type_loop (name, scope, |
| 1073 | scope_length + 2); |
| 1074 | |
| 1075 | if (retval != NULL) |
| 1076 | return retval; |
| 1077 | } |
| 1078 | |
| 1079 | full_name = alloca (scope_length + 2 + strlen (name) + 1); |
| 1080 | strncpy (full_name, scope, scope_length); |
| 1081 | strncpy (full_name + scope_length, "::", 2); |
| 1082 | strcpy (full_name + scope_length + 2, name); |
| 1083 | |
| 1084 | return basic_lookup_transparent_type (full_name); |
| 1085 | } |
| 1086 | |
| 1087 | /* This used to do something but was removed when it became |
| 1088 | obsolete. */ |
| 1089 | |
| 1090 | static void |
| 1091 | maintenance_cplus_namespace (char *args, int from_tty) |
| 1092 | { |
| 1093 | printf_unfiltered (_("The `maint namespace' command was removed.\n")); |
| 1094 | } |
| 1095 | |
| 1096 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
| 1097 | extern initialize_file_ftype _initialize_cp_namespace; |
| 1098 | |
| 1099 | void |
| 1100 | _initialize_cp_namespace (void) |
| 1101 | { |
| 1102 | struct cmd_list_element *cmd; |
| 1103 | |
| 1104 | cmd = add_cmd ("namespace", class_maintenance, |
| 1105 | maintenance_cplus_namespace, |
| 1106 | _("Deprecated placeholder for removed functionality."), |
| 1107 | &maint_cplus_cmd_list); |
| 1108 | deprecate_cmd (cmd, NULL); |
| 1109 | } |