| 1 | /* Helper routines for C++ support in GDB. |
| 2 | Copyright (C) 2003, 2004, 2007, 2008, 2009 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 | |
| 34 | /* List of using directives that are active in the current file. */ |
| 35 | |
| 36 | static struct using_direct *using_list; |
| 37 | |
| 38 | static struct using_direct *cp_add_using (const char *name, |
| 39 | unsigned int inner_len, |
| 40 | unsigned int outer_len, |
| 41 | struct using_direct *next); |
| 42 | |
| 43 | static struct using_direct *cp_copy_usings (struct using_direct *using, |
| 44 | struct obstack *obstack); |
| 45 | |
| 46 | static struct symbol *lookup_namespace_scope (const char *name, |
| 47 | const char *linkage_name, |
| 48 | const struct block *block, |
| 49 | const domain_enum domain, |
| 50 | const char *scope, |
| 51 | int scope_len); |
| 52 | |
| 53 | static struct symbol *lookup_symbol_file (const char *name, |
| 54 | const char *linkage_name, |
| 55 | const struct block *block, |
| 56 | const domain_enum domain, |
| 57 | int anonymous_namespace); |
| 58 | |
| 59 | static struct type *cp_lookup_transparent_type_loop (const char *name, |
| 60 | const char *scope, |
| 61 | int scope_len); |
| 62 | |
| 63 | static void initialize_namespace_symtab (struct objfile *objfile); |
| 64 | |
| 65 | static struct block *get_possible_namespace_block (struct objfile *objfile); |
| 66 | |
| 67 | static void free_namespace_block (struct symtab *symtab); |
| 68 | |
| 69 | static int check_possible_namespace_symbols_loop (const char *name, |
| 70 | int len, |
| 71 | struct objfile *objfile); |
| 72 | |
| 73 | static int check_one_possible_namespace_symbol (const char *name, |
| 74 | int len, |
| 75 | struct objfile *objfile); |
| 76 | |
| 77 | static struct symbol *lookup_possible_namespace_symbol (const char *name); |
| 78 | |
| 79 | static void maintenance_cplus_namespace (char *args, int from_tty); |
| 80 | |
| 81 | /* Set up support for dealing with C++ namespace info in the current |
| 82 | symtab. */ |
| 83 | |
| 84 | void cp_initialize_namespace () |
| 85 | { |
| 86 | using_list = NULL; |
| 87 | } |
| 88 | |
| 89 | /* Add all the using directives we've gathered to the current symtab. |
| 90 | STATIC_BLOCK should be the symtab's static block; OBSTACK is used |
| 91 | for allocation. */ |
| 92 | |
| 93 | void |
| 94 | cp_finalize_namespace (struct block *static_block, |
| 95 | struct obstack *obstack) |
| 96 | { |
| 97 | if (using_list != NULL) |
| 98 | { |
| 99 | block_set_using (static_block, |
| 100 | cp_copy_usings (using_list, obstack), |
| 101 | obstack); |
| 102 | using_list = NULL; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* Check to see if SYMBOL refers to an object contained within an |
| 107 | anonymous namespace; if so, add an appropriate using directive. */ |
| 108 | |
| 109 | /* Optimize away strlen ("(anonymous namespace)"). */ |
| 110 | |
| 111 | #define ANONYMOUS_NAMESPACE_LEN 21 |
| 112 | |
| 113 | void |
| 114 | cp_scan_for_anonymous_namespaces (const struct symbol *symbol) |
| 115 | { |
| 116 | if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) |
| 117 | { |
| 118 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
| 119 | unsigned int previous_component; |
| 120 | unsigned int next_component; |
| 121 | const char *len; |
| 122 | |
| 123 | /* Start with a quick-and-dirty check for mention of "(anonymous |
| 124 | namespace)". */ |
| 125 | |
| 126 | if (!cp_is_anonymous (name)) |
| 127 | return; |
| 128 | |
| 129 | previous_component = 0; |
| 130 | next_component = cp_find_first_component (name + previous_component); |
| 131 | |
| 132 | while (name[next_component] == ':') |
| 133 | { |
| 134 | if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN |
| 135 | && strncmp (name + previous_component, |
| 136 | "(anonymous namespace)", |
| 137 | ANONYMOUS_NAMESPACE_LEN) == 0) |
| 138 | { |
| 139 | /* We've found a component of the name that's an |
| 140 | anonymous namespace. So add symbols in it to the |
| 141 | namespace given by the previous component if there is |
| 142 | one, or to the global namespace if there isn't. */ |
| 143 | cp_add_using_directive (name, |
| 144 | previous_component == 0 |
| 145 | ? 0 : previous_component - 2, |
| 146 | next_component); |
| 147 | } |
| 148 | /* The "+ 2" is for the "::". */ |
| 149 | previous_component = next_component + 2; |
| 150 | next_component = (previous_component |
| 151 | + cp_find_first_component (name |
| 152 | + previous_component)); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* Add a using directive to using_list. NAME is the start of a string |
| 158 | that should contain the namespaces we want to add as initial |
| 159 | substrings, OUTER_LENGTH is the end of the outer namespace, and |
| 160 | INNER_LENGTH is the end of the inner namespace. If the using |
| 161 | directive in question has already been added, don't add it |
| 162 | twice. */ |
| 163 | |
| 164 | void |
| 165 | cp_add_using_directive (const char *name, unsigned int outer_length, |
| 166 | unsigned int inner_length) |
| 167 | { |
| 168 | struct using_direct *current; |
| 169 | struct using_direct *new; |
| 170 | |
| 171 | /* Has it already been added? */ |
| 172 | |
| 173 | for (current = using_list; current != NULL; current = current->next) |
| 174 | { |
| 175 | if ((strncmp (current->inner, name, inner_length) == 0) |
| 176 | && (strlen (current->inner) == inner_length) |
| 177 | && (strlen (current->outer) == outer_length)) |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | using_list = cp_add_using (name, inner_length, outer_length, |
| 182 | using_list); |
| 183 | } |
| 184 | |
| 185 | /* Record the namespace that the function defined by SYMBOL was |
| 186 | defined in, if necessary. BLOCK is the associated block; use |
| 187 | OBSTACK for allocation. */ |
| 188 | |
| 189 | void |
| 190 | cp_set_block_scope (const struct symbol *symbol, |
| 191 | struct block *block, |
| 192 | struct obstack *obstack, |
| 193 | const char *processing_current_prefix, |
| 194 | int processing_has_namespace_info) |
| 195 | { |
| 196 | if (processing_has_namespace_info) |
| 197 | { |
| 198 | block_set_scope |
| 199 | (block, obsavestring (processing_current_prefix, |
| 200 | strlen (processing_current_prefix), |
| 201 | obstack), |
| 202 | obstack); |
| 203 | } |
| 204 | else if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) |
| 205 | { |
| 206 | /* Try to figure out the appropriate namespace from the |
| 207 | demangled name. */ |
| 208 | |
| 209 | /* FIXME: carlton/2003-04-15: If the function in question is |
| 210 | a method of a class, the name will actually include the |
| 211 | name of the class as well. This should be harmless, but |
| 212 | is a little unfortunate. */ |
| 213 | |
| 214 | const char *name = SYMBOL_DEMANGLED_NAME (symbol); |
| 215 | unsigned int prefix_len = cp_entire_prefix_len (name); |
| 216 | |
| 217 | block_set_scope (block, |
| 218 | obsavestring (name, prefix_len, obstack), |
| 219 | obstack); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /* Test whether or not NAMESPACE looks like it mentions an anonymous |
| 224 | namespace; return nonzero if so. */ |
| 225 | |
| 226 | int |
| 227 | cp_is_anonymous (const char *namespace) |
| 228 | { |
| 229 | return (strstr (namespace, "(anonymous namespace)") |
| 230 | != NULL); |
| 231 | } |
| 232 | |
| 233 | /* Create a new struct using direct whose inner namespace is the |
| 234 | initial substring of NAME of leng INNER_LEN and whose outer |
| 235 | namespace is the initial substring of NAME of length OUTER_LENGTH. |
| 236 | Set its next member in the linked list to NEXT; allocate all memory |
| 237 | using xmalloc. It copies the strings, so NAME can be a temporary |
| 238 | string. */ |
| 239 | |
| 240 | static struct using_direct * |
| 241 | cp_add_using (const char *name, |
| 242 | unsigned int inner_len, |
| 243 | unsigned int outer_len, |
| 244 | struct using_direct *next) |
| 245 | { |
| 246 | struct using_direct *retval; |
| 247 | |
| 248 | gdb_assert (outer_len < inner_len); |
| 249 | |
| 250 | retval = xmalloc (sizeof (struct using_direct)); |
| 251 | retval->inner = savestring (name, inner_len); |
| 252 | retval->outer = savestring (name, outer_len); |
| 253 | retval->next = next; |
| 254 | |
| 255 | return retval; |
| 256 | } |
| 257 | |
| 258 | /* Make a copy of the using directives in the list pointed to by |
| 259 | USING, using OBSTACK to allocate memory. Free all memory pointed |
| 260 | to by USING via xfree. */ |
| 261 | |
| 262 | static struct using_direct * |
| 263 | cp_copy_usings (struct using_direct *using, |
| 264 | struct obstack *obstack) |
| 265 | { |
| 266 | if (using == NULL) |
| 267 | { |
| 268 | return NULL; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | struct using_direct *retval |
| 273 | = obstack_alloc (obstack, sizeof (struct using_direct)); |
| 274 | retval->inner = obsavestring (using->inner, strlen (using->inner), |
| 275 | obstack); |
| 276 | retval->outer = obsavestring (using->outer, strlen (using->outer), |
| 277 | obstack); |
| 278 | retval->next = cp_copy_usings (using->next, obstack); |
| 279 | |
| 280 | xfree (using->inner); |
| 281 | xfree (using->outer); |
| 282 | xfree (using); |
| 283 | |
| 284 | return retval; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* The C++-specific version of name lookup for static and global |
| 289 | names. This makes sure that names get looked for in all namespaces |
| 290 | that are in scope. NAME is the natural name of the symbol that |
| 291 | we're looking for, LINKAGE_NAME (which is optional) is its linkage |
| 292 | name, BLOCK is the block that we're searching within, DOMAIN says |
| 293 | what kind of symbols we're looking for, and if SYMTAB is non-NULL, |
| 294 | we should store the symtab where we found the symbol in it. */ |
| 295 | |
| 296 | struct symbol * |
| 297 | cp_lookup_symbol_nonlocal (const char *name, |
| 298 | const char *linkage_name, |
| 299 | const struct block *block, |
| 300 | const domain_enum domain) |
| 301 | { |
| 302 | return lookup_namespace_scope (name, linkage_name, block, domain, |
| 303 | block_scope (block), 0); |
| 304 | } |
| 305 | |
| 306 | /* Lookup NAME at namespace scope (or, in C terms, in static and |
| 307 | global variables). SCOPE is the namespace that the current |
| 308 | function is defined within; only consider namespaces whose length |
| 309 | is at least SCOPE_LEN. Other arguments are as in |
| 310 | cp_lookup_symbol_nonlocal. |
| 311 | |
| 312 | For example, if we're within a function A::B::f and looking for a |
| 313 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
| 314 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
| 315 | but with SCOPE_LEN = 1. And then it calls itself with NAME and |
| 316 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for |
| 317 | "A::B::x"; if it doesn't find it, then the second call looks for |
| 318 | "A::x", and if that call fails, then the first call looks for |
| 319 | "x". */ |
| 320 | |
| 321 | static struct symbol * |
| 322 | lookup_namespace_scope (const char *name, |
| 323 | const char *linkage_name, |
| 324 | const struct block *block, |
| 325 | const domain_enum domain, |
| 326 | const char *scope, |
| 327 | int scope_len) |
| 328 | { |
| 329 | char *namespace; |
| 330 | |
| 331 | if (scope[scope_len] != '\0') |
| 332 | { |
| 333 | /* Recursively search for names in child namespaces first. */ |
| 334 | |
| 335 | struct symbol *sym; |
| 336 | int new_scope_len = scope_len; |
| 337 | |
| 338 | /* If the current scope is followed by "::", skip past that. */ |
| 339 | if (new_scope_len != 0) |
| 340 | { |
| 341 | gdb_assert (scope[new_scope_len] == ':'); |
| 342 | new_scope_len += 2; |
| 343 | } |
| 344 | new_scope_len += cp_find_first_component (scope + new_scope_len); |
| 345 | sym = lookup_namespace_scope (name, linkage_name, block, |
| 346 | domain, scope, new_scope_len); |
| 347 | if (sym != NULL) |
| 348 | return sym; |
| 349 | } |
| 350 | |
| 351 | /* Okay, we didn't find a match in our children, so look for the |
| 352 | name in the current namespace. */ |
| 353 | |
| 354 | namespace = alloca (scope_len + 1); |
| 355 | strncpy (namespace, scope, scope_len); |
| 356 | namespace[scope_len] = '\0'; |
| 357 | return cp_lookup_symbol_namespace (namespace, name, linkage_name, |
| 358 | block, domain); |
| 359 | } |
| 360 | |
| 361 | /* Look up NAME in the C++ namespace NAMESPACE, applying the using |
| 362 | directives that are active in BLOCK. Other arguments are as in |
| 363 | cp_lookup_symbol_nonlocal. */ |
| 364 | |
| 365 | struct symbol * |
| 366 | cp_lookup_symbol_namespace (const char *namespace, |
| 367 | const char *name, |
| 368 | const char *linkage_name, |
| 369 | const struct block *block, |
| 370 | const domain_enum domain) |
| 371 | { |
| 372 | const struct using_direct *current; |
| 373 | struct symbol *sym; |
| 374 | |
| 375 | /* First, go through the using directives. If any of them add new |
| 376 | names to the namespace we're searching in, see if we can find a |
| 377 | match by applying them. */ |
| 378 | |
| 379 | for (current = block_using (block); |
| 380 | current != NULL; |
| 381 | current = current->next) |
| 382 | { |
| 383 | if (strcmp (namespace, current->outer) == 0) |
| 384 | { |
| 385 | sym = cp_lookup_symbol_namespace (current->inner, |
| 386 | name, |
| 387 | linkage_name, |
| 388 | block, |
| 389 | domain); |
| 390 | if (sym != NULL) |
| 391 | return sym; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /* We didn't find anything by applying any of the using directives |
| 396 | that are still applicable; so let's see if we've got a match |
| 397 | using the current namespace. */ |
| 398 | |
| 399 | if (namespace[0] == '\0') |
| 400 | { |
| 401 | return lookup_symbol_file (name, linkage_name, block, |
| 402 | domain, 0); |
| 403 | } |
| 404 | else |
| 405 | { |
| 406 | char *concatenated_name |
| 407 | = alloca (strlen (namespace) + 2 + strlen (name) + 1); |
| 408 | strcpy (concatenated_name, namespace); |
| 409 | strcat (concatenated_name, "::"); |
| 410 | strcat (concatenated_name, name); |
| 411 | sym = lookup_symbol_file (concatenated_name, linkage_name, |
| 412 | block, domain, |
| 413 | cp_is_anonymous (namespace)); |
| 414 | return sym; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* Look up NAME in BLOCK's static block and in global blocks. If |
| 419 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located |
| 420 | within an anonymous namespace. Other arguments are as in |
| 421 | cp_lookup_symbol_nonlocal. */ |
| 422 | |
| 423 | static struct symbol * |
| 424 | lookup_symbol_file (const char *name, |
| 425 | const char *linkage_name, |
| 426 | const struct block *block, |
| 427 | const domain_enum domain, |
| 428 | int anonymous_namespace) |
| 429 | { |
| 430 | struct symbol *sym = NULL; |
| 431 | |
| 432 | sym = lookup_symbol_static (name, linkage_name, block, domain); |
| 433 | if (sym != NULL) |
| 434 | return sym; |
| 435 | |
| 436 | if (anonymous_namespace) |
| 437 | { |
| 438 | /* Symbols defined in anonymous namespaces have external linkage |
| 439 | but should be treated as local to a single file nonetheless. |
| 440 | So we only search the current file's global block. */ |
| 441 | |
| 442 | const struct block *global_block = block_global_block (block); |
| 443 | |
| 444 | if (global_block != NULL) |
| 445 | sym = lookup_symbol_aux_block (name, linkage_name, global_block, |
| 446 | domain); |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | sym = lookup_symbol_global (name, linkage_name, block, domain); |
| 451 | } |
| 452 | |
| 453 | if (sym != NULL) |
| 454 | return sym; |
| 455 | |
| 456 | /* Now call "lookup_possible_namespace_symbol". Symbols in here |
| 457 | claim to be associated to namespaces, but this claim might be |
| 458 | incorrect: the names in question might actually correspond to |
| 459 | classes instead of namespaces. But if they correspond to |
| 460 | classes, then we should have found a match for them above. So if |
| 461 | we find them now, they should be genuine. */ |
| 462 | |
| 463 | /* FIXME: carlton/2003-06-12: This is a hack and should eventually |
| 464 | be deleted: see comments below. */ |
| 465 | |
| 466 | if (domain == VAR_DOMAIN) |
| 467 | { |
| 468 | sym = lookup_possible_namespace_symbol (name); |
| 469 | if (sym != NULL) |
| 470 | return sym; |
| 471 | } |
| 472 | |
| 473 | return NULL; |
| 474 | } |
| 475 | |
| 476 | /* Look up a type named NESTED_NAME that is nested inside the C++ |
| 477 | class or namespace given by PARENT_TYPE, from within the context |
| 478 | given by BLOCK. Return NULL if there is no such nested type. */ |
| 479 | |
| 480 | struct type * |
| 481 | cp_lookup_nested_type (struct type *parent_type, |
| 482 | const char *nested_name, |
| 483 | const struct block *block) |
| 484 | { |
| 485 | switch (TYPE_CODE (parent_type)) |
| 486 | { |
| 487 | case TYPE_CODE_STRUCT: |
| 488 | case TYPE_CODE_NAMESPACE: |
| 489 | { |
| 490 | /* NOTE: carlton/2003-11-10: We don't treat C++ class members |
| 491 | of classes like, say, data or function members. Instead, |
| 492 | they're just represented by symbols whose names are |
| 493 | qualified by the name of the surrounding class. This is |
| 494 | just like members of namespaces; in particular, |
| 495 | lookup_symbol_namespace works when looking them up. */ |
| 496 | |
| 497 | const char *parent_name = TYPE_TAG_NAME (parent_type); |
| 498 | struct symbol *sym = cp_lookup_symbol_namespace (parent_name, |
| 499 | nested_name, |
| 500 | NULL, |
| 501 | block, |
| 502 | VAR_DOMAIN); |
| 503 | if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF) |
| 504 | return NULL; |
| 505 | else |
| 506 | return SYMBOL_TYPE (sym); |
| 507 | } |
| 508 | default: |
| 509 | internal_error (__FILE__, __LINE__, |
| 510 | _("cp_lookup_nested_type called on a non-aggregate type.")); |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | /* The C++-version of lookup_transparent_type. */ |
| 515 | |
| 516 | /* FIXME: carlton/2004-01-16: The problem that this is trying to |
| 517 | address is that, unfortunately, sometimes NAME is wrong: it may not |
| 518 | include the name of namespaces enclosing the type in question. |
| 519 | lookup_transparent_type gets called when the the type in question |
| 520 | is a declaration, and we're trying to find its definition; but, for |
| 521 | declarations, our type name deduction mechanism doesn't work. |
| 522 | There's nothing we can do to fix this in general, I think, in the |
| 523 | absence of debug information about namespaces (I've filed PR |
| 524 | gdb/1511 about this); until such debug information becomes more |
| 525 | prevalent, one heuristic which sometimes looks is to search for the |
| 526 | definition in namespaces containing the current namespace. |
| 527 | |
| 528 | We should delete this functions once the appropriate debug |
| 529 | information becomes more widespread. (GCC 3.4 will be the first |
| 530 | released version of GCC with such information.) */ |
| 531 | |
| 532 | struct type * |
| 533 | cp_lookup_transparent_type (const char *name) |
| 534 | { |
| 535 | /* First, try the honest way of looking up the definition. */ |
| 536 | struct type *t = basic_lookup_transparent_type (name); |
| 537 | const char *scope; |
| 538 | |
| 539 | if (t != NULL) |
| 540 | return t; |
| 541 | |
| 542 | /* If that doesn't work and we're within a namespace, look there |
| 543 | instead. */ |
| 544 | scope = block_scope (get_selected_block (0)); |
| 545 | |
| 546 | if (scope[0] == '\0') |
| 547 | return NULL; |
| 548 | |
| 549 | return cp_lookup_transparent_type_loop (name, scope, 0); |
| 550 | } |
| 551 | |
| 552 | /* Lookup the the type definition associated to NAME in |
| 553 | namespaces/classes containing SCOPE whose name is strictly longer |
| 554 | than LENGTH. LENGTH must be the index of the start of a |
| 555 | component of SCOPE. */ |
| 556 | |
| 557 | static struct type * |
| 558 | cp_lookup_transparent_type_loop (const char *name, const char *scope, |
| 559 | int length) |
| 560 | { |
| 561 | int scope_length = length + cp_find_first_component (scope + length); |
| 562 | char *full_name; |
| 563 | |
| 564 | /* If the current scope is followed by "::", look in the next |
| 565 | component. */ |
| 566 | if (scope[scope_length] == ':') |
| 567 | { |
| 568 | struct type *retval |
| 569 | = cp_lookup_transparent_type_loop (name, scope, scope_length + 2); |
| 570 | if (retval != NULL) |
| 571 | return retval; |
| 572 | } |
| 573 | |
| 574 | full_name = alloca (scope_length + 2 + strlen (name) + 1); |
| 575 | strncpy (full_name, scope, scope_length); |
| 576 | strncpy (full_name + scope_length, "::", 2); |
| 577 | strcpy (full_name + scope_length + 2, name); |
| 578 | |
| 579 | return basic_lookup_transparent_type (full_name); |
| 580 | } |
| 581 | |
| 582 | /* Now come functions for dealing with symbols associated to |
| 583 | namespaces. (They're used to store the namespaces themselves, not |
| 584 | objects that live in the namespaces.) These symbols come in two |
| 585 | varieties: if we run into a DW_TAG_namespace DIE, then we know that |
| 586 | we have a namespace, so dwarf2read.c creates a symbol for it just |
| 587 | like normal. But, unfortunately, versions of GCC through at least |
| 588 | 3.3 don't generate those DIE's. Our solution is to try to guess |
| 589 | their existence by looking at demangled names. This might cause us |
| 590 | to misidentify classes as namespaces, however. So we put those |
| 591 | symbols in a special block (one per objfile), and we only search |
| 592 | that block as a last resort. */ |
| 593 | |
| 594 | /* FIXME: carlton/2003-06-12: Once versions of GCC that generate |
| 595 | DW_TAG_namespace have been out for a year or two, we should get rid |
| 596 | of all of this "possible namespace" nonsense. */ |
| 597 | |
| 598 | /* Allocate everything necessary for the possible namespace block |
| 599 | associated to OBJFILE. */ |
| 600 | |
| 601 | static void |
| 602 | initialize_namespace_symtab (struct objfile *objfile) |
| 603 | { |
| 604 | struct symtab *namespace_symtab; |
| 605 | struct blockvector *bv; |
| 606 | struct block *bl; |
| 607 | |
| 608 | namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile); |
| 609 | namespace_symtab->language = language_cplus; |
| 610 | namespace_symtab->free_code = free_nothing; |
| 611 | namespace_symtab->dirname = NULL; |
| 612 | |
| 613 | bv = obstack_alloc (&objfile->objfile_obstack, |
| 614 | sizeof (struct blockvector) |
| 615 | + FIRST_LOCAL_BLOCK * sizeof (struct block *)); |
| 616 | BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1; |
| 617 | BLOCKVECTOR (namespace_symtab) = bv; |
| 618 | |
| 619 | /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */ |
| 620 | |
| 621 | bl = allocate_block (&objfile->objfile_obstack); |
| 622 | BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack, |
| 623 | NULL); |
| 624 | BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl; |
| 625 | bl = allocate_block (&objfile->objfile_obstack); |
| 626 | BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack, |
| 627 | NULL); |
| 628 | BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl; |
| 629 | |
| 630 | /* Allocate the possible namespace block; we put it where the first |
| 631 | local block will live, though I don't think there's any need to |
| 632 | pretend that it's actually a local block (e.g. by setting |
| 633 | BLOCK_SUPERBLOCK appropriately). We don't use the global or |
| 634 | static block because we don't want it searched during the normal |
| 635 | search of all global/static blocks in lookup_symbol: we only want |
| 636 | it used as a last resort. */ |
| 637 | |
| 638 | /* NOTE: carlton/2003-09-11: I considered not associating the fake |
| 639 | symbols to a block/symtab at all. But that would cause problems |
| 640 | with lookup_symbol's SYMTAB argument and with block_found, so |
| 641 | having a symtab/block for this purpose seems like the best |
| 642 | solution for now. */ |
| 643 | |
| 644 | bl = allocate_block (&objfile->objfile_obstack); |
| 645 | BLOCK_DICT (bl) = dict_create_hashed_expandable (); |
| 646 | BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl; |
| 647 | |
| 648 | namespace_symtab->free_func = free_namespace_block; |
| 649 | |
| 650 | objfile->cp_namespace_symtab = namespace_symtab; |
| 651 | } |
| 652 | |
| 653 | /* Locate the possible namespace block associated to OBJFILE, |
| 654 | allocating it if necessary. */ |
| 655 | |
| 656 | static struct block * |
| 657 | get_possible_namespace_block (struct objfile *objfile) |
| 658 | { |
| 659 | if (objfile->cp_namespace_symtab == NULL) |
| 660 | initialize_namespace_symtab (objfile); |
| 661 | |
| 662 | return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab), |
| 663 | FIRST_LOCAL_BLOCK); |
| 664 | } |
| 665 | |
| 666 | /* Free the dictionary associated to the possible namespace block. */ |
| 667 | |
| 668 | static void |
| 669 | free_namespace_block (struct symtab *symtab) |
| 670 | { |
| 671 | struct block *possible_namespace_block; |
| 672 | |
| 673 | possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), |
| 674 | FIRST_LOCAL_BLOCK); |
| 675 | gdb_assert (possible_namespace_block != NULL); |
| 676 | dict_free (BLOCK_DICT (possible_namespace_block)); |
| 677 | } |
| 678 | |
| 679 | /* Ensure that there are symbols in the possible namespace block |
| 680 | associated to OBJFILE for all initial substrings of NAME that look |
| 681 | like namespaces or classes. NAME should end in a member variable: |
| 682 | it shouldn't consist solely of namespaces. */ |
| 683 | |
| 684 | void |
| 685 | cp_check_possible_namespace_symbols (const char *name, struct objfile *objfile) |
| 686 | { |
| 687 | check_possible_namespace_symbols_loop (name, |
| 688 | cp_find_first_component (name), |
| 689 | objfile); |
| 690 | } |
| 691 | |
| 692 | /* This is a helper loop for cp_check_possible_namespace_symbols; it |
| 693 | ensures that there are symbols in the possible namespace block |
| 694 | associated to OBJFILE for all namespaces that are initial |
| 695 | substrings of NAME of length at least LEN. It returns 1 if a |
| 696 | previous loop had already created the shortest such symbol and 0 |
| 697 | otherwise. |
| 698 | |
| 699 | This function assumes that if there is already a symbol associated |
| 700 | to a substring of NAME of a given length, then there are already |
| 701 | symbols associated to all substrings of NAME whose length is less |
| 702 | than that length. So if cp_check_possible_namespace_symbols has |
| 703 | been called once with argument "A::B::C::member", then that will |
| 704 | create symbols "A", "A::B", and "A::B::C". If it is then later |
| 705 | called with argument "A::B::D::member", then the new call will |
| 706 | generate a new symbol for "A::B::D", but once it sees that "A::B" |
| 707 | has already been created, it doesn't bother checking to see if "A" |
| 708 | has also been created. */ |
| 709 | |
| 710 | static int |
| 711 | check_possible_namespace_symbols_loop (const char *name, int len, |
| 712 | struct objfile *objfile) |
| 713 | { |
| 714 | if (name[len] == ':') |
| 715 | { |
| 716 | int done; |
| 717 | int next_len = len + 2; |
| 718 | |
| 719 | next_len += cp_find_first_component (name + next_len); |
| 720 | done = check_possible_namespace_symbols_loop (name, next_len, |
| 721 | objfile); |
| 722 | |
| 723 | if (!done) |
| 724 | done = check_one_possible_namespace_symbol (name, len, objfile); |
| 725 | |
| 726 | return done; |
| 727 | } |
| 728 | else |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | /* Check to see if there's already a possible namespace symbol in |
| 733 | OBJFILE whose name is the initial substring of NAME of length LEN. |
| 734 | If not, create one and return 0; otherwise, return 1. */ |
| 735 | |
| 736 | static int |
| 737 | check_one_possible_namespace_symbol (const char *name, int len, |
| 738 | struct objfile *objfile) |
| 739 | { |
| 740 | struct block *block = get_possible_namespace_block (objfile); |
| 741 | char *name_copy = alloca (len + 1); |
| 742 | struct symbol *sym; |
| 743 | |
| 744 | memcpy (name_copy, name, len); |
| 745 | name_copy[len] = '\0'; |
| 746 | sym = lookup_block_symbol (block, name_copy, NULL, VAR_DOMAIN); |
| 747 | |
| 748 | if (sym == NULL) |
| 749 | { |
| 750 | struct type *type; |
| 751 | name_copy = obsavestring (name, len, &objfile->objfile_obstack); |
| 752 | |
| 753 | type = init_type (TYPE_CODE_NAMESPACE, 0, 0, name_copy, objfile); |
| 754 | |
| 755 | TYPE_TAG_NAME (type) = TYPE_NAME (type); |
| 756 | |
| 757 | sym = obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol)); |
| 758 | memset (sym, 0, sizeof (struct symbol)); |
| 759 | SYMBOL_LANGUAGE (sym) = language_cplus; |
| 760 | SYMBOL_SET_NAMES (sym, name_copy, len, objfile); |
| 761 | SYMBOL_CLASS (sym) = LOC_TYPEDEF; |
| 762 | SYMBOL_TYPE (sym) = type; |
| 763 | SYMBOL_DOMAIN (sym) = VAR_DOMAIN; |
| 764 | |
| 765 | dict_add_symbol (BLOCK_DICT (block), sym); |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | else |
| 770 | return 1; |
| 771 | } |
| 772 | |
| 773 | /* Look for a symbol named NAME in all the possible namespace blocks. |
| 774 | If one is found, return it. */ |
| 775 | |
| 776 | static struct symbol * |
| 777 | lookup_possible_namespace_symbol (const char *name) |
| 778 | { |
| 779 | struct objfile *objfile; |
| 780 | |
| 781 | ALL_OBJFILES (objfile) |
| 782 | { |
| 783 | struct symbol *sym; |
| 784 | |
| 785 | sym = lookup_block_symbol (get_possible_namespace_block (objfile), |
| 786 | name, NULL, VAR_DOMAIN); |
| 787 | |
| 788 | if (sym != NULL) |
| 789 | return sym; |
| 790 | } |
| 791 | |
| 792 | return NULL; |
| 793 | } |
| 794 | |
| 795 | /* Print out all the possible namespace symbols. */ |
| 796 | |
| 797 | static void |
| 798 | maintenance_cplus_namespace (char *args, int from_tty) |
| 799 | { |
| 800 | struct objfile *objfile; |
| 801 | printf_unfiltered (_("Possible namespaces:\n")); |
| 802 | ALL_OBJFILES (objfile) |
| 803 | { |
| 804 | struct dict_iterator iter; |
| 805 | struct symbol *sym; |
| 806 | |
| 807 | ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym) |
| 808 | { |
| 809 | printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym)); |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | void |
| 815 | _initialize_cp_namespace (void) |
| 816 | { |
| 817 | add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace, |
| 818 | _("Print the list of possible C++ namespaces."), |
| 819 | &maint_cplus_cmd_list); |
| 820 | } |