| 1 | /* Helper routines for C++ support in GDB. |
| 2 | Copyright (C) 2003-2004, 2007-2012 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 "gdb_assert.h" |
| 27 | #include "block.h" |
| 28 | #include "objfiles.h" |
| 29 | #include "gdbtypes.h" |
| 30 | #include "dictionary.h" |
| 31 | #include "command.h" |
| 32 | #include "frame.h" |
| 33 | #include "buildsym.h" |
| 34 | #include "language.h" |
| 35 | |
| 36 | static struct symbol *lookup_namespace_scope (const char *name, |
| 37 | const struct block *block, |
| 38 | const domain_enum domain, |
| 39 | const char *scope, |
| 40 | int scope_len); |
| 41 | |
| 42 | static struct symbol *lookup_symbol_file (const char *name, |
| 43 | const struct block *block, |
| 44 | const domain_enum domain, |
| 45 | int anonymous_namespace); |
| 46 | |
| 47 | static struct type *cp_lookup_transparent_type_loop (const char *name, |
| 48 | const char *scope, |
| 49 | int scope_len); |
| 50 | |
| 51 | /* Check to see if SYMBOL refers to an object contained within an |
| 52 | anonymous namespace; if so, add an appropriate using directive. */ |
| 53 | |
| 54 | void |
| 55 | cp_scan_for_anonymous_namespaces (const struct symbol *const symbol, |
| 56 | struct objfile *const objfile) |
| 57 | { |
| 58 | if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) |
| 59 | { |
| 60 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
| 61 | unsigned int previous_component; |
| 62 | unsigned int next_component; |
| 63 | |
| 64 | /* Start with a quick-and-dirty check for mention of "(anonymous |
| 65 | namespace)". */ |
| 66 | |
| 67 | if (!cp_is_anonymous (name)) |
| 68 | return; |
| 69 | |
| 70 | previous_component = 0; |
| 71 | next_component = cp_find_first_component (name + previous_component); |
| 72 | |
| 73 | while (name[next_component] == ':') |
| 74 | { |
| 75 | if (((next_component - previous_component) |
| 76 | == CP_ANONYMOUS_NAMESPACE_LEN) |
| 77 | && strncmp (name + previous_component, |
| 78 | CP_ANONYMOUS_NAMESPACE_STR, |
| 79 | CP_ANONYMOUS_NAMESPACE_LEN) == 0) |
| 80 | { |
| 81 | int dest_len = (previous_component == 0 |
| 82 | ? 0 : previous_component - 2); |
| 83 | int src_len = next_component; |
| 84 | |
| 85 | char *dest = alloca (dest_len + 1); |
| 86 | char *src = alloca (src_len + 1); |
| 87 | |
| 88 | memcpy (dest, name, dest_len); |
| 89 | memcpy (src, name, src_len); |
| 90 | |
| 91 | dest[dest_len] = '\0'; |
| 92 | src[src_len] = '\0'; |
| 93 | |
| 94 | /* We've found a component of the name that's an |
| 95 | anonymous namespace. So add symbols in it to the |
| 96 | namespace given by the previous component if there is |
| 97 | one, or to the global namespace if there isn't. */ |
| 98 | cp_add_using_directive (dest, src, NULL, NULL, NULL, |
| 99 | &objfile->objfile_obstack); |
| 100 | } |
| 101 | /* The "+ 2" is for the "::". */ |
| 102 | previous_component = next_component + 2; |
| 103 | next_component = (previous_component |
| 104 | + cp_find_first_component (name |
| 105 | + previous_component)); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /* Add a using directive to using_directives. If the using directive |
| 112 | in question has already been added, don't add it twice. |
| 113 | |
| 114 | Create a new struct using_direct which imports the namespace SRC |
| 115 | into the scope DEST. ALIAS is the name of the imported namespace |
| 116 | in the current scope. If ALIAS is NULL then the namespace is known |
| 117 | by its original name. DECLARATION is the name if the imported |
| 118 | varable if this is a declaration import (Eg. using A::x), otherwise |
| 119 | it is NULL. EXCLUDES is a list of names not to import from an imported |
| 120 | module or NULL. The arguments are copied into newly allocated memory so |
| 121 | they can be temporaries. For EXCLUDES the VEC pointers are copied but the |
| 122 | pointed to characters are not copied. */ |
| 123 | |
| 124 | void |
| 125 | cp_add_using_directive (const char *dest, |
| 126 | const char *src, |
| 127 | const char *alias, |
| 128 | const char *declaration, |
| 129 | VEC (const_char_ptr) *excludes, |
| 130 | struct obstack *obstack) |
| 131 | { |
| 132 | struct using_direct *current; |
| 133 | struct using_direct *new; |
| 134 | |
| 135 | /* Has it already been added? */ |
| 136 | |
| 137 | for (current = using_directives; current != NULL; current = current->next) |
| 138 | { |
| 139 | int ix; |
| 140 | const char *param; |
| 141 | |
| 142 | if (strcmp (current->import_src, src) != 0) |
| 143 | continue; |
| 144 | if (strcmp (current->import_dest, dest) != 0) |
| 145 | continue; |
| 146 | if ((alias == NULL && current->alias != NULL) |
| 147 | || (alias != NULL && current->alias == NULL) |
| 148 | || (alias != NULL && current->alias != NULL |
| 149 | && strcmp (alias, current->alias) != 0)) |
| 150 | continue; |
| 151 | if ((declaration == NULL && current->declaration != NULL) |
| 152 | || (declaration != NULL && current->declaration == NULL) |
| 153 | || (declaration != NULL && current->declaration != NULL |
| 154 | && strcmp (declaration, current->declaration) != 0)) |
| 155 | continue; |
| 156 | |
| 157 | /* Compare the contents of EXCLUDES. */ |
| 158 | for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++) |
| 159 | if (current->excludes[ix] == NULL |
| 160 | || strcmp (param, current->excludes[ix]) != 0) |
| 161 | break; |
| 162 | if (ix < VEC_length (const_char_ptr, excludes) |
| 163 | || current->excludes[ix] != NULL) |
| 164 | continue; |
| 165 | |
| 166 | /* Parameters exactly match CURRENT. */ |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | new = obstack_alloc (obstack, (sizeof (*new) |
| 171 | + (VEC_length (const_char_ptr, excludes) |
| 172 | * sizeof (*new->excludes)))); |
| 173 | memset (new, 0, sizeof (*new)); |
| 174 | |
| 175 | new->import_src = obsavestring (src, strlen (src), obstack); |
| 176 | new->import_dest = obsavestring (dest, strlen (dest), obstack); |
| 177 | |
| 178 | if (alias != NULL) |
| 179 | new->alias = obsavestring (alias, strlen (alias), obstack); |
| 180 | |
| 181 | if (declaration != NULL) |
| 182 | new->declaration = obsavestring (declaration, strlen (declaration), |
| 183 | obstack); |
| 184 | |
| 185 | memcpy (new->excludes, VEC_address (const_char_ptr, excludes), |
| 186 | VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes)); |
| 187 | new->excludes[VEC_length (const_char_ptr, excludes)] = NULL; |
| 188 | |
| 189 | new->next = using_directives; |
| 190 | using_directives = new; |
| 191 | } |
| 192 | |
| 193 | /* Record the namespace that the function defined by SYMBOL was |
| 194 | defined in, if necessary. BLOCK is the associated block; use |
| 195 | OBSTACK for allocation. */ |
| 196 | |
| 197 | void |
| 198 | cp_set_block_scope (const struct symbol *symbol, |
| 199 | struct block *block, |
| 200 | struct obstack *obstack, |
| 201 | const char *processing_current_prefix, |
| 202 | int processing_has_namespace_info) |
| 203 | { |
| 204 | if (processing_has_namespace_info) |
| 205 | { |
| 206 | block_set_scope |
| 207 | (block, obsavestring (processing_current_prefix, |
| 208 | strlen (processing_current_prefix), |
| 209 | obstack), |
| 210 | obstack); |
| 211 | } |
| 212 | else if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) |
| 213 | { |
| 214 | /* Try to figure out the appropriate namespace from the |
| 215 | demangled name. */ |
| 216 | |
| 217 | /* FIXME: carlton/2003-04-15: If the function in question is |
| 218 | a method of a class, the name will actually include the |
| 219 | name of the class as well. This should be harmless, but |
| 220 | is a little unfortunate. */ |
| 221 | |
| 222 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
| 223 | unsigned int prefix_len = cp_entire_prefix_len (name); |
| 224 | |
| 225 | block_set_scope (block, |
| 226 | obsavestring (name, prefix_len, obstack), |
| 227 | obstack); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /* Test whether or not NAMESPACE looks like it mentions an anonymous |
| 232 | namespace; return nonzero if so. */ |
| 233 | |
| 234 | int |
| 235 | cp_is_anonymous (const char *namespace) |
| 236 | { |
| 237 | return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR) |
| 238 | != NULL); |
| 239 | } |
| 240 | |
| 241 | /* The C++-specific version of name lookup for static and global |
| 242 | names. This makes sure that names get looked for in all namespaces |
| 243 | that are in scope. NAME is the natural name of the symbol that |
| 244 | we're looking for, BLOCK is the block that we're searching within, |
| 245 | DOMAIN says what kind of symbols we're looking for, and if SYMTAB |
| 246 | is non-NULL, we should store the symtab where we found the symbol |
| 247 | in it. */ |
| 248 | |
| 249 | struct symbol * |
| 250 | cp_lookup_symbol_nonlocal (const char *name, |
| 251 | const struct block *block, |
| 252 | const domain_enum domain) |
| 253 | { |
| 254 | struct symbol *sym; |
| 255 | const char *scope = block_scope (block); |
| 256 | |
| 257 | sym = lookup_namespace_scope (name, block, |
| 258 | domain, scope, 0); |
| 259 | if (sym != NULL) |
| 260 | return sym; |
| 261 | |
| 262 | return cp_lookup_symbol_namespace (scope, name, |
| 263 | block, domain); |
| 264 | } |
| 265 | |
| 266 | /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are |
| 267 | as in cp_lookup_symbol_nonlocal. */ |
| 268 | |
| 269 | static struct symbol * |
| 270 | cp_lookup_symbol_in_namespace (const char *namespace, |
| 271 | const char *name, |
| 272 | const struct block *block, |
| 273 | const domain_enum domain) |
| 274 | { |
| 275 | if (namespace[0] == '\0') |
| 276 | { |
| 277 | return lookup_symbol_file (name, block, domain, 0); |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | char *concatenated_name = alloca (strlen (namespace) + 2 |
| 282 | + strlen (name) + 1); |
| 283 | |
| 284 | strcpy (concatenated_name, namespace); |
| 285 | strcat (concatenated_name, "::"); |
| 286 | strcat (concatenated_name, name); |
| 287 | return lookup_symbol_file (concatenated_name, block, domain, |
| 288 | cp_is_anonymous (namespace)); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | /* Used for cleanups to reset the "searched" flag incase |
| 293 | of an error. */ |
| 294 | |
| 295 | static void |
| 296 | reset_directive_searched (void *data) |
| 297 | { |
| 298 | struct using_direct *direct = data; |
| 299 | direct->searched = 0; |
| 300 | } |
| 301 | |
| 302 | /* Search for NAME by applying all import statements belonging to |
| 303 | BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the |
| 304 | search is restricted to using declarations. |
| 305 | Example: |
| 306 | |
| 307 | namespace A { |
| 308 | int x; |
| 309 | } |
| 310 | using A::x; |
| 311 | |
| 312 | If SEARCH_PARENTS the search will include imports which are |
| 313 | applicable in parents of SCOPE. |
| 314 | Example: |
| 315 | |
| 316 | namespace A { |
| 317 | using namespace X; |
| 318 | namespace B { |
| 319 | using namespace Y; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of |
| 324 | namespaces X and Y will be considered. If SEARCH_PARENTS is false |
| 325 | only the import of Y is considered. */ |
| 326 | |
| 327 | struct symbol * |
| 328 | cp_lookup_symbol_imports (const char *scope, |
| 329 | const char *name, |
| 330 | const struct block *block, |
| 331 | const domain_enum domain, |
| 332 | const int declaration_only, |
| 333 | const int search_parents) |
| 334 | { |
| 335 | struct using_direct *current; |
| 336 | struct symbol *sym = NULL; |
| 337 | int len; |
| 338 | int directive_match; |
| 339 | struct cleanup *searched_cleanup; |
| 340 | |
| 341 | /* First, try to find the symbol in the given namespace. */ |
| 342 | if (!declaration_only) |
| 343 | sym = cp_lookup_symbol_in_namespace (scope, name, |
| 344 | block, domain); |
| 345 | |
| 346 | if (sym != NULL) |
| 347 | return sym; |
| 348 | |
| 349 | /* Go through the using directives. If any of them add new names to |
| 350 | the namespace we're searching in, see if we can find a match by |
| 351 | applying them. */ |
| 352 | |
| 353 | for (current = block_using (block); |
| 354 | current != NULL; |
| 355 | current = current->next) |
| 356 | { |
| 357 | const char **excludep; |
| 358 | |
| 359 | len = strlen (current->import_dest); |
| 360 | directive_match = (search_parents |
| 361 | ? (strncmp (scope, current->import_dest, |
| 362 | strlen (current->import_dest)) == 0 |
| 363 | && (len == 0 |
| 364 | || scope[len] == ':' |
| 365 | || scope[len] == '\0')) |
| 366 | : strcmp (scope, current->import_dest) == 0); |
| 367 | |
| 368 | /* If the import destination is the current scope or one of its |
| 369 | ancestors then it is applicable. */ |
| 370 | if (directive_match && !current->searched) |
| 371 | { |
| 372 | /* Mark this import as searched so that the recursive call |
| 373 | does not search it again. */ |
| 374 | current->searched = 1; |
| 375 | searched_cleanup = make_cleanup (reset_directive_searched, |
| 376 | current); |
| 377 | |
| 378 | /* If there is an import of a single declaration, compare the |
| 379 | imported declaration (after optional renaming by its alias) |
| 380 | with the sought out name. If there is a match pass |
| 381 | current->import_src as NAMESPACE to direct the search |
| 382 | towards the imported namespace. */ |
| 383 | if (current->declaration |
| 384 | && strcmp (name, current->alias |
| 385 | ? current->alias : current->declaration) == 0) |
| 386 | sym = cp_lookup_symbol_in_namespace (current->import_src, |
| 387 | current->declaration, |
| 388 | block, domain); |
| 389 | |
| 390 | /* If this is a DECLARATION_ONLY search or a symbol was found |
| 391 | or this import statement was an import declaration, the |
| 392 | search of this import is complete. */ |
| 393 | if (declaration_only || sym != NULL || current->declaration) |
| 394 | { |
| 395 | current->searched = 0; |
| 396 | discard_cleanups (searched_cleanup); |
| 397 | |
| 398 | if (sym != NULL) |
| 399 | return sym; |
| 400 | |
| 401 | continue; |
| 402 | } |
| 403 | |
| 404 | /* Do not follow CURRENT if NAME matches its EXCLUDES. */ |
| 405 | for (excludep = current->excludes; *excludep; excludep++) |
| 406 | if (strcmp (name, *excludep) == 0) |
| 407 | break; |
| 408 | if (*excludep) |
| 409 | { |
| 410 | discard_cleanups (searched_cleanup); |
| 411 | continue; |
| 412 | } |
| 413 | |
| 414 | if (current->alias != NULL |
| 415 | && strcmp (name, current->alias) == 0) |
| 416 | /* If the import is creating an alias and the alias matches |
| 417 | the sought name. Pass current->import_src as the NAME to |
| 418 | direct the search towards the aliased namespace. */ |
| 419 | { |
| 420 | sym = cp_lookup_symbol_in_namespace (scope, |
| 421 | current->import_src, |
| 422 | block, domain); |
| 423 | } |
| 424 | else if (current->alias == NULL) |
| 425 | { |
| 426 | /* If this import statement creates no alias, pass |
| 427 | current->inner as NAMESPACE to direct the search |
| 428 | towards the imported namespace. */ |
| 429 | sym = cp_lookup_symbol_imports (current->import_src, |
| 430 | name, block, |
| 431 | domain, 0, 0); |
| 432 | } |
| 433 | current->searched = 0; |
| 434 | discard_cleanups (searched_cleanup); |
| 435 | |
| 436 | if (sym != NULL) |
| 437 | return sym; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return NULL; |
| 442 | } |
| 443 | |
| 444 | /* Helper function that searches an array of symbols for one named |
| 445 | NAME. */ |
| 446 | |
| 447 | static struct symbol * |
| 448 | search_symbol_list (const char *name, int num, |
| 449 | struct symbol **syms) |
| 450 | { |
| 451 | int i; |
| 452 | |
| 453 | /* Maybe we should store a dictionary in here instead. */ |
| 454 | for (i = 0; i < num; ++i) |
| 455 | { |
| 456 | if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0) |
| 457 | return syms[i]; |
| 458 | } |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it |
| 463 | searches through the template parameters of the function and the |
| 464 | function's type. */ |
| 465 | |
| 466 | struct symbol * |
| 467 | cp_lookup_symbol_imports_or_template (const char *scope, |
| 468 | const char *name, |
| 469 | const struct block *block, |
| 470 | const domain_enum domain) |
| 471 | { |
| 472 | struct symbol *function = BLOCK_FUNCTION (block); |
| 473 | |
| 474 | if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus) |
| 475 | { |
| 476 | int i; |
| 477 | struct cplus_specific *cps |
| 478 | = function->ginfo.language_specific.cplus_specific; |
| 479 | |
| 480 | /* Search the function's template parameters. */ |
| 481 | if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function)) |
| 482 | { |
| 483 | struct template_symbol *templ |
| 484 | = (struct template_symbol *) function; |
| 485 | struct symbol *result; |
| 486 | |
| 487 | result = search_symbol_list (name, |
| 488 | templ->n_template_arguments, |
| 489 | templ->template_arguments); |
| 490 | if (result != NULL) |
| 491 | return result; |
| 492 | } |
| 493 | |
| 494 | /* Search the template parameters of the function's defining |
| 495 | context. */ |
| 496 | if (SYMBOL_NATURAL_NAME (function)) |
| 497 | { |
| 498 | struct type *context; |
| 499 | char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function)); |
| 500 | struct cleanup *cleanups = make_cleanup (xfree, name_copy); |
| 501 | const struct language_defn *lang = language_def (language_cplus); |
| 502 | struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch; |
| 503 | const struct block *parent = BLOCK_SUPERBLOCK (block); |
| 504 | |
| 505 | while (1) |
| 506 | { |
| 507 | struct symbol *result; |
| 508 | unsigned int prefix_len = cp_entire_prefix_len (name_copy); |
| 509 | |
| 510 | if (prefix_len == 0) |
| 511 | context = NULL; |
| 512 | else |
| 513 | { |
| 514 | name_copy[prefix_len] = '\0'; |
| 515 | context = lookup_typename (lang, arch, |
| 516 | name_copy, |
| 517 | parent, 1); |
| 518 | } |
| 519 | |
| 520 | if (context == NULL) |
| 521 | break; |
| 522 | |
| 523 | result |
| 524 | = search_symbol_list (name, |
| 525 | TYPE_N_TEMPLATE_ARGUMENTS (context), |
| 526 | TYPE_TEMPLATE_ARGUMENTS (context)); |
| 527 | if (result != NULL) |
| 528 | return result; |
| 529 | } |
| 530 | |
| 531 | do_cleanups (cleanups); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1); |
| 536 | } |
| 537 | |
| 538 | /* Searches for NAME in the current namespace, and by applying |
| 539 | relevant import statements belonging to BLOCK and its parents. |
| 540 | SCOPE is the namespace scope of the context in which the search is |
| 541 | being evaluated. */ |
| 542 | |
| 543 | struct symbol* |
| 544 | cp_lookup_symbol_namespace (const char *scope, |
| 545 | const char *name, |
| 546 | const struct block *block, |
| 547 | const domain_enum domain) |
| 548 | { |
| 549 | struct symbol *sym; |
| 550 | |
| 551 | /* First, try to find the symbol in the given namespace. */ |
| 552 | sym = cp_lookup_symbol_in_namespace (scope, name, |
| 553 | block, domain); |
| 554 | if (sym != NULL) |
| 555 | return sym; |
| 556 | |
| 557 | /* Search for name in namespaces imported to this and parent |
| 558 | blocks. */ |
| 559 | while (block != NULL) |
| 560 | { |
| 561 | sym = cp_lookup_symbol_imports (scope, name, block, |
| 562 | domain, 0, 1); |
| 563 | |
| 564 | if (sym) |
| 565 | return sym; |
| 566 | |
| 567 | block = BLOCK_SUPERBLOCK (block); |
| 568 | } |
| 569 | |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | /* Lookup NAME at namespace scope (or, in C terms, in static and |
| 574 | global variables). SCOPE is the namespace that the current |
| 575 | function is defined within; only consider namespaces whose length |
| 576 | is at least SCOPE_LEN. Other arguments are as in |
| 577 | cp_lookup_symbol_nonlocal. |
| 578 | |
| 579 | For example, if we're within a function A::B::f and looking for a |
| 580 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
| 581 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
| 582 | but with SCOPE_LEN = 1. And then it calls itself with NAME and |
| 583 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for |
| 584 | "A::B::x"; if it doesn't find it, then the second call looks for |
| 585 | "A::x", and if that call fails, then the first call looks for |
| 586 | "x". */ |
| 587 | |
| 588 | static struct symbol * |
| 589 | lookup_namespace_scope (const char *name, |
| 590 | const struct block *block, |
| 591 | const domain_enum domain, |
| 592 | const char *scope, |
| 593 | int scope_len) |
| 594 | { |
| 595 | char *namespace; |
| 596 | |
| 597 | if (scope[scope_len] != '\0') |
| 598 | { |
| 599 | /* Recursively search for names in child namespaces first. */ |
| 600 | |
| 601 | struct symbol *sym; |
| 602 | int new_scope_len = scope_len; |
| 603 | |
| 604 | /* If the current scope is followed by "::", skip past that. */ |
| 605 | if (new_scope_len != 0) |
| 606 | { |
| 607 | gdb_assert (scope[new_scope_len] == ':'); |
| 608 | new_scope_len += 2; |
| 609 | } |
| 610 | new_scope_len += cp_find_first_component (scope + new_scope_len); |
| 611 | sym = lookup_namespace_scope (name, block, domain, |
| 612 | scope, new_scope_len); |
| 613 | if (sym != NULL) |
| 614 | return sym; |
| 615 | } |
| 616 | |
| 617 | /* Okay, we didn't find a match in our children, so look for the |
| 618 | name in the current namespace. */ |
| 619 | |
| 620 | namespace = alloca (scope_len + 1); |
| 621 | strncpy (namespace, scope, scope_len); |
| 622 | namespace[scope_len] = '\0'; |
| 623 | return cp_lookup_symbol_in_namespace (namespace, name, |
| 624 | block, domain); |
| 625 | } |
| 626 | |
| 627 | /* Look up NAME in BLOCK's static block and in global blocks. If |
| 628 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located |
| 629 | within an anonymous namespace. Other arguments are as in |
| 630 | cp_lookup_symbol_nonlocal. */ |
| 631 | |
| 632 | static struct symbol * |
| 633 | lookup_symbol_file (const char *name, |
| 634 | const struct block *block, |
| 635 | const domain_enum domain, |
| 636 | int anonymous_namespace) |
| 637 | { |
| 638 | struct symbol *sym = NULL; |
| 639 | |
| 640 | sym = lookup_symbol_static (name, block, domain); |
| 641 | if (sym != NULL) |
| 642 | return sym; |
| 643 | |
| 644 | if (anonymous_namespace) |
| 645 | { |
| 646 | /* Symbols defined in anonymous namespaces have external linkage |
| 647 | but should be treated as local to a single file nonetheless. |
| 648 | So we only search the current file's global block. */ |
| 649 | |
| 650 | const struct block *global_block = block_global_block (block); |
| 651 | |
| 652 | if (global_block != NULL) |
| 653 | sym = lookup_symbol_aux_block (name, global_block, domain); |
| 654 | } |
| 655 | else |
| 656 | { |
| 657 | sym = lookup_symbol_global (name, block, domain); |
| 658 | } |
| 659 | |
| 660 | return sym; |
| 661 | } |
| 662 | |
| 663 | /* Look up a symbol named NESTED_NAME that is nested inside the C++ |
| 664 | class or namespace given by PARENT_TYPE, from within the context |
| 665 | given by BLOCK. Return NULL if there is no such nested type. */ |
| 666 | |
| 667 | struct symbol * |
| 668 | cp_lookup_nested_symbol (struct type *parent_type, |
| 669 | const char *nested_name, |
| 670 | const struct block *block) |
| 671 | { |
| 672 | /* type_name_no_tag_required provides better error reporting using the |
| 673 | original type. */ |
| 674 | struct type *saved_parent_type = parent_type; |
| 675 | |
| 676 | CHECK_TYPEDEF (parent_type); |
| 677 | |
| 678 | switch (TYPE_CODE (parent_type)) |
| 679 | { |
| 680 | case TYPE_CODE_STRUCT: |
| 681 | case TYPE_CODE_NAMESPACE: |
| 682 | case TYPE_CODE_UNION: |
| 683 | { |
| 684 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members |
| 685 | of classes like, say, data or function members. Instead, |
| 686 | they're just represented by symbols whose names are |
| 687 | qualified by the name of the surrounding class. This is |
| 688 | just like members of namespaces; in particular, |
| 689 | lookup_symbol_namespace works when looking them up. */ |
| 690 | |
| 691 | const char *parent_name = type_name_no_tag_or_error (saved_parent_type); |
| 692 | struct symbol *sym |
| 693 | = cp_lookup_symbol_in_namespace (parent_name, nested_name, |
| 694 | block, VAR_DOMAIN); |
| 695 | char *concatenated_name; |
| 696 | |
| 697 | if (sym != NULL) |
| 698 | return sym; |
| 699 | |
| 700 | /* Now search all static file-level symbols. Not strictly |
| 701 | correct, but more useful than an error. We do not try to |
| 702 | guess any imported namespace as even the fully specified |
| 703 | namespace seach is is already not C++ compliant and more |
| 704 | assumptions could make it too magic. */ |
| 705 | |
| 706 | concatenated_name = alloca (strlen (parent_name) + 2 |
| 707 | + strlen (nested_name) + 1); |
| 708 | sprintf (concatenated_name, "%s::%s", |
| 709 | parent_name, nested_name); |
| 710 | sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN); |
| 711 | if (sym != NULL) |
| 712 | return sym; |
| 713 | |
| 714 | return NULL; |
| 715 | } |
| 716 | default: |
| 717 | internal_error (__FILE__, __LINE__, |
| 718 | _("cp_lookup_nested_symbol called " |
| 719 | "on a non-aggregate type.")); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | /* The C++-version of lookup_transparent_type. */ |
| 724 | |
| 725 | /* FIXME: carlton/2004-01-16: The problem that this is trying to |
| 726 | address is that, unfortunately, sometimes NAME is wrong: it may not |
| 727 | include the name of namespaces enclosing the type in question. |
| 728 | lookup_transparent_type gets called when the type in question |
| 729 | is a declaration, and we're trying to find its definition; but, for |
| 730 | declarations, our type name deduction mechanism doesn't work. |
| 731 | There's nothing we can do to fix this in general, I think, in the |
| 732 | absence of debug information about namespaces (I've filed PR |
| 733 | gdb/1511 about this); until such debug information becomes more |
| 734 | prevalent, one heuristic which sometimes looks is to search for the |
| 735 | definition in namespaces containing the current namespace. |
| 736 | |
| 737 | We should delete this functions once the appropriate debug |
| 738 | information becomes more widespread. (GCC 3.4 will be the first |
| 739 | released version of GCC with such information.) */ |
| 740 | |
| 741 | struct type * |
| 742 | cp_lookup_transparent_type (const char *name) |
| 743 | { |
| 744 | /* First, try the honest way of looking up the definition. */ |
| 745 | struct type *t = basic_lookup_transparent_type (name); |
| 746 | const char *scope; |
| 747 | |
| 748 | if (t != NULL) |
| 749 | return t; |
| 750 | |
| 751 | /* If that doesn't work and we're within a namespace, look there |
| 752 | instead. */ |
| 753 | scope = block_scope (get_selected_block (0)); |
| 754 | |
| 755 | if (scope[0] == '\0') |
| 756 | return NULL; |
| 757 | |
| 758 | return cp_lookup_transparent_type_loop (name, scope, 0); |
| 759 | } |
| 760 | |
| 761 | /* Lookup the type definition associated to NAME in namespaces/classes |
| 762 | containing SCOPE whose name is strictly longer than LENGTH. LENGTH |
| 763 | must be the index of the start of a component of SCOPE. */ |
| 764 | |
| 765 | static struct type * |
| 766 | cp_lookup_transparent_type_loop (const char *name, |
| 767 | const char *scope, |
| 768 | int length) |
| 769 | { |
| 770 | int scope_length = length + cp_find_first_component (scope + length); |
| 771 | char *full_name; |
| 772 | |
| 773 | /* If the current scope is followed by "::", look in the next |
| 774 | component. */ |
| 775 | if (scope[scope_length] == ':') |
| 776 | { |
| 777 | struct type *retval |
| 778 | = cp_lookup_transparent_type_loop (name, scope, |
| 779 | scope_length + 2); |
| 780 | |
| 781 | if (retval != NULL) |
| 782 | return retval; |
| 783 | } |
| 784 | |
| 785 | full_name = alloca (scope_length + 2 + strlen (name) + 1); |
| 786 | strncpy (full_name, scope, scope_length); |
| 787 | strncpy (full_name + scope_length, "::", 2); |
| 788 | strcpy (full_name + scope_length + 2, name); |
| 789 | |
| 790 | return basic_lookup_transparent_type (full_name); |
| 791 | } |
| 792 | |
| 793 | /* This used to do something but was removed when it became |
| 794 | obsolete. */ |
| 795 | |
| 796 | static void |
| 797 | maintenance_cplus_namespace (char *args, int from_tty) |
| 798 | { |
| 799 | printf_unfiltered (_("The `maint namespace' command was removed.\n")); |
| 800 | } |
| 801 | |
| 802 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
| 803 | extern initialize_file_ftype _initialize_cp_namespace; |
| 804 | |
| 805 | void |
| 806 | _initialize_cp_namespace (void) |
| 807 | { |
| 808 | struct cmd_list_element *cmd; |
| 809 | |
| 810 | cmd = add_cmd ("namespace", class_maintenance, |
| 811 | maintenance_cplus_namespace, |
| 812 | _("Deprecated placeholder for removed functionality."), |
| 813 | &maint_cplus_cmd_list); |
| 814 | deprecate_cmd (cmd, NULL); |
| 815 | } |