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