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