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