GDB (API)
|
00001 /* Helper routines for C++ support in GDB. 00002 Copyright (C) 2003-2013 Free Software Foundation, Inc. 00003 00004 Contributed by David Carlton and by Kealia, Inc. 00005 00006 This file is part of GDB. 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00020 00021 #include "defs.h" 00022 #include "cp-support.h" 00023 #include "gdb_obstack.h" 00024 #include "symtab.h" 00025 #include "symfile.h" 00026 #include "gdb_assert.h" 00027 #include "block.h" 00028 #include "objfiles.h" 00029 #include "gdbtypes.h" 00030 #include "dictionary.h" 00031 #include "command.h" 00032 #include "frame.h" 00033 #include "buildsym.h" 00034 #include "language.h" 00035 00036 static struct symbol *lookup_namespace_scope (const char *name, 00037 const struct block *block, 00038 const domain_enum domain, 00039 const char *scope, 00040 int scope_len); 00041 00042 static struct symbol *lookup_symbol_file (const char *name, 00043 const struct block *block, 00044 const domain_enum domain, 00045 int anonymous_namespace, 00046 int search); 00047 00048 static struct type *cp_lookup_transparent_type_loop (const char *name, 00049 const char *scope, 00050 int scope_len); 00051 00052 /* Check to see if SYMBOL refers to an object contained within an 00053 anonymous namespace; if so, add an appropriate using directive. */ 00054 00055 void 00056 cp_scan_for_anonymous_namespaces (const struct symbol *const symbol, 00057 struct objfile *const objfile) 00058 { 00059 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) 00060 { 00061 const char *name = SYMBOL_DEMANGLED_NAME (symbol); 00062 unsigned int previous_component; 00063 unsigned int next_component; 00064 00065 /* Start with a quick-and-dirty check for mention of "(anonymous 00066 namespace)". */ 00067 00068 if (!cp_is_anonymous (name)) 00069 return; 00070 00071 previous_component = 0; 00072 next_component = cp_find_first_component (name + previous_component); 00073 00074 while (name[next_component] == ':') 00075 { 00076 if (((next_component - previous_component) 00077 == CP_ANONYMOUS_NAMESPACE_LEN) 00078 && strncmp (name + previous_component, 00079 CP_ANONYMOUS_NAMESPACE_STR, 00080 CP_ANONYMOUS_NAMESPACE_LEN) == 0) 00081 { 00082 int dest_len = (previous_component == 0 00083 ? 0 : previous_component - 2); 00084 int src_len = next_component; 00085 00086 char *dest = alloca (dest_len + 1); 00087 char *src = alloca (src_len + 1); 00088 00089 memcpy (dest, name, dest_len); 00090 memcpy (src, name, src_len); 00091 00092 dest[dest_len] = '\0'; 00093 src[src_len] = '\0'; 00094 00095 /* We've found a component of the name that's an 00096 anonymous namespace. So add symbols in it to the 00097 namespace given by the previous component if there is 00098 one, or to the global namespace if there isn't. */ 00099 cp_add_using_directive (dest, src, NULL, NULL, NULL, 1, 00100 &objfile->objfile_obstack); 00101 } 00102 /* The "+ 2" is for the "::". */ 00103 previous_component = next_component + 2; 00104 next_component = (previous_component 00105 + cp_find_first_component (name 00106 + previous_component)); 00107 } 00108 } 00109 } 00110 00111 00112 /* Add a using directive to using_directives. If the using directive 00113 in question has already been added, don't add it twice. 00114 00115 Create a new struct using_direct which imports the namespace SRC 00116 into the scope DEST. ALIAS is the name of the imported namespace 00117 in the current scope. If ALIAS is NULL then the namespace is known 00118 by its original name. DECLARATION is the name if the imported 00119 varable if this is a declaration import (Eg. using A::x), otherwise 00120 it is NULL. EXCLUDES is a list of names not to import from an 00121 imported module or NULL. If COPY_NAMES is non-zero, then the 00122 arguments are copied into newly allocated memory so they can be 00123 temporaries. For EXCLUDES the VEC pointers are copied but the 00124 pointed to characters are not copied. */ 00125 00126 void 00127 cp_add_using_directive (const char *dest, 00128 const char *src, 00129 const char *alias, 00130 const char *declaration, 00131 VEC (const_char_ptr) *excludes, 00132 int copy_names, 00133 struct obstack *obstack) 00134 { 00135 struct using_direct *current; 00136 struct using_direct *new; 00137 00138 /* Has it already been added? */ 00139 00140 for (current = using_directives; current != NULL; current = current->next) 00141 { 00142 int ix; 00143 const char *param; 00144 00145 if (strcmp (current->import_src, src) != 0) 00146 continue; 00147 if (strcmp (current->import_dest, dest) != 0) 00148 continue; 00149 if ((alias == NULL && current->alias != NULL) 00150 || (alias != NULL && current->alias == NULL) 00151 || (alias != NULL && current->alias != NULL 00152 && strcmp (alias, current->alias) != 0)) 00153 continue; 00154 if ((declaration == NULL && current->declaration != NULL) 00155 || (declaration != NULL && current->declaration == NULL) 00156 || (declaration != NULL && current->declaration != NULL 00157 && strcmp (declaration, current->declaration) != 0)) 00158 continue; 00159 00160 /* Compare the contents of EXCLUDES. */ 00161 for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++) 00162 if (current->excludes[ix] == NULL 00163 || strcmp (param, current->excludes[ix]) != 0) 00164 break; 00165 if (ix < VEC_length (const_char_ptr, excludes) 00166 || current->excludes[ix] != NULL) 00167 continue; 00168 00169 /* Parameters exactly match CURRENT. */ 00170 return; 00171 } 00172 00173 new = obstack_alloc (obstack, (sizeof (*new) 00174 + (VEC_length (const_char_ptr, excludes) 00175 * sizeof (*new->excludes)))); 00176 memset (new, 0, sizeof (*new)); 00177 00178 if (copy_names) 00179 { 00180 new->import_src = obstack_copy0 (obstack, src, strlen (src)); 00181 new->import_dest = obstack_copy0 (obstack, dest, strlen (dest)); 00182 } 00183 else 00184 { 00185 new->import_src = src; 00186 new->import_dest = dest; 00187 } 00188 00189 if (alias != NULL && copy_names) 00190 new->alias = obstack_copy0 (obstack, alias, strlen (alias)); 00191 else 00192 new->alias = alias; 00193 00194 if (declaration != NULL && copy_names) 00195 new->declaration = obstack_copy0 (obstack, 00196 declaration, strlen (declaration)); 00197 else 00198 new->declaration = declaration; 00199 00200 memcpy (new->excludes, VEC_address (const_char_ptr, excludes), 00201 VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes)); 00202 new->excludes[VEC_length (const_char_ptr, excludes)] = NULL; 00203 00204 new->next = using_directives; 00205 using_directives = new; 00206 } 00207 00208 /* Test whether or not NAMESPACE looks like it mentions an anonymous 00209 namespace; return nonzero if so. */ 00210 00211 int 00212 cp_is_anonymous (const char *namespace) 00213 { 00214 return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR) 00215 != NULL); 00216 } 00217 00218 /* The C++-specific version of name lookup for static and global 00219 names. This makes sure that names get looked for in all namespaces 00220 that are in scope. NAME is the natural name of the symbol that 00221 we're looking for, BLOCK is the block that we're searching within, 00222 DOMAIN says what kind of symbols we're looking for, and if SYMTAB 00223 is non-NULL, we should store the symtab where we found the symbol 00224 in it. */ 00225 00226 struct symbol * 00227 cp_lookup_symbol_nonlocal (const char *name, 00228 const struct block *block, 00229 const domain_enum domain) 00230 { 00231 struct symbol *sym; 00232 const char *scope = block_scope (block); 00233 00234 sym = lookup_namespace_scope (name, block, 00235 domain, scope, 0); 00236 if (sym != NULL) 00237 return sym; 00238 00239 return cp_lookup_symbol_namespace (scope, name, 00240 block, domain); 00241 } 00242 00243 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are 00244 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search 00245 through base classes for a matching symbol. */ 00246 00247 static struct symbol * 00248 cp_lookup_symbol_in_namespace (const char *namespace, 00249 const char *name, 00250 const struct block *block, 00251 const domain_enum domain, int search) 00252 { 00253 if (namespace[0] == '\0') 00254 { 00255 return lookup_symbol_file (name, block, domain, 0, search); 00256 } 00257 else 00258 { 00259 char *concatenated_name = alloca (strlen (namespace) + 2 00260 + strlen (name) + 1); 00261 00262 strcpy (concatenated_name, namespace); 00263 strcat (concatenated_name, "::"); 00264 strcat (concatenated_name, name); 00265 return lookup_symbol_file (concatenated_name, block, domain, 00266 cp_is_anonymous (namespace), search); 00267 } 00268 } 00269 00270 /* Used for cleanups to reset the "searched" flag incase 00271 of an error. */ 00272 00273 static void 00274 reset_directive_searched (void *data) 00275 { 00276 struct using_direct *direct = data; 00277 direct->searched = 0; 00278 } 00279 00280 /* Search for NAME by applying all import statements belonging to 00281 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the 00282 search is restricted to using declarations. 00283 Example: 00284 00285 namespace A { 00286 int x; 00287 } 00288 using A::x; 00289 00290 If SEARCH_PARENTS the search will include imports which are 00291 applicable in parents of SCOPE. 00292 Example: 00293 00294 namespace A { 00295 using namespace X; 00296 namespace B { 00297 using namespace Y; 00298 } 00299 } 00300 00301 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of 00302 namespaces X and Y will be considered. If SEARCH_PARENTS is false 00303 only the import of Y is considered. */ 00304 00305 struct symbol * 00306 cp_lookup_symbol_imports (const char *scope, 00307 const char *name, 00308 const struct block *block, 00309 const domain_enum domain, 00310 const int declaration_only, 00311 const int search_parents) 00312 { 00313 struct using_direct *current; 00314 struct symbol *sym = NULL; 00315 int len; 00316 int directive_match; 00317 struct cleanup *searched_cleanup; 00318 00319 /* First, try to find the symbol in the given namespace. */ 00320 if (!declaration_only) 00321 sym = cp_lookup_symbol_in_namespace (scope, name, 00322 block, domain, 1); 00323 00324 if (sym != NULL) 00325 return sym; 00326 00327 /* Go through the using directives. If any of them add new names to 00328 the namespace we're searching in, see if we can find a match by 00329 applying them. */ 00330 00331 for (current = block_using (block); 00332 current != NULL; 00333 current = current->next) 00334 { 00335 const char **excludep; 00336 00337 len = strlen (current->import_dest); 00338 directive_match = (search_parents 00339 ? (strncmp (scope, current->import_dest, 00340 strlen (current->import_dest)) == 0 00341 && (len == 0 00342 || scope[len] == ':' 00343 || scope[len] == '\0')) 00344 : strcmp (scope, current->import_dest) == 0); 00345 00346 /* If the import destination is the current scope or one of its 00347 ancestors then it is applicable. */ 00348 if (directive_match && !current->searched) 00349 { 00350 /* Mark this import as searched so that the recursive call 00351 does not search it again. */ 00352 current->searched = 1; 00353 searched_cleanup = make_cleanup (reset_directive_searched, 00354 current); 00355 00356 /* If there is an import of a single declaration, compare the 00357 imported declaration (after optional renaming by its alias) 00358 with the sought out name. If there is a match pass 00359 current->import_src as NAMESPACE to direct the search 00360 towards the imported namespace. */ 00361 if (current->declaration 00362 && strcmp (name, current->alias 00363 ? current->alias : current->declaration) == 0) 00364 sym = cp_lookup_symbol_in_namespace (current->import_src, 00365 current->declaration, 00366 block, domain, 1); 00367 00368 /* If this is a DECLARATION_ONLY search or a symbol was found 00369 or this import statement was an import declaration, the 00370 search of this import is complete. */ 00371 if (declaration_only || sym != NULL || current->declaration) 00372 { 00373 current->searched = 0; 00374 discard_cleanups (searched_cleanup); 00375 00376 if (sym != NULL) 00377 return sym; 00378 00379 continue; 00380 } 00381 00382 /* Do not follow CURRENT if NAME matches its EXCLUDES. */ 00383 for (excludep = current->excludes; *excludep; excludep++) 00384 if (strcmp (name, *excludep) == 0) 00385 break; 00386 if (*excludep) 00387 { 00388 discard_cleanups (searched_cleanup); 00389 continue; 00390 } 00391 00392 if (current->alias != NULL 00393 && strcmp (name, current->alias) == 0) 00394 /* If the import is creating an alias and the alias matches 00395 the sought name. Pass current->import_src as the NAME to 00396 direct the search towards the aliased namespace. */ 00397 { 00398 sym = cp_lookup_symbol_in_namespace (scope, 00399 current->import_src, 00400 block, domain, 1); 00401 } 00402 else if (current->alias == NULL) 00403 { 00404 /* If this import statement creates no alias, pass 00405 current->inner as NAMESPACE to direct the search 00406 towards the imported namespace. */ 00407 sym = cp_lookup_symbol_imports (current->import_src, 00408 name, block, 00409 domain, 0, 0); 00410 } 00411 current->searched = 0; 00412 discard_cleanups (searched_cleanup); 00413 00414 if (sym != NULL) 00415 return sym; 00416 } 00417 } 00418 00419 return NULL; 00420 } 00421 00422 /* Helper function that searches an array of symbols for one named 00423 NAME. */ 00424 00425 static struct symbol * 00426 search_symbol_list (const char *name, int num, 00427 struct symbol **syms) 00428 { 00429 int i; 00430 00431 /* Maybe we should store a dictionary in here instead. */ 00432 for (i = 0; i < num; ++i) 00433 { 00434 if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0) 00435 return syms[i]; 00436 } 00437 return NULL; 00438 } 00439 00440 /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it 00441 searches through the template parameters of the function and the 00442 function's type. */ 00443 00444 struct symbol * 00445 cp_lookup_symbol_imports_or_template (const char *scope, 00446 const char *name, 00447 const struct block *block, 00448 const domain_enum domain) 00449 { 00450 struct symbol *function = BLOCK_FUNCTION (block); 00451 00452 if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus) 00453 { 00454 /* Search the function's template parameters. */ 00455 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function)) 00456 { 00457 struct template_symbol *templ 00458 = (struct template_symbol *) function; 00459 struct symbol *result; 00460 00461 result = search_symbol_list (name, 00462 templ->n_template_arguments, 00463 templ->template_arguments); 00464 if (result != NULL) 00465 return result; 00466 } 00467 00468 /* Search the template parameters of the function's defining 00469 context. */ 00470 if (SYMBOL_NATURAL_NAME (function)) 00471 { 00472 struct type *context; 00473 char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function)); 00474 struct cleanup *cleanups = make_cleanup (xfree, name_copy); 00475 const struct language_defn *lang = language_def (language_cplus); 00476 struct gdbarch *arch 00477 = get_objfile_arch (SYMBOL_SYMTAB (function)->objfile); 00478 const struct block *parent = BLOCK_SUPERBLOCK (block); 00479 00480 while (1) 00481 { 00482 struct symbol *result; 00483 unsigned int prefix_len = cp_entire_prefix_len (name_copy); 00484 00485 if (prefix_len == 0) 00486 context = NULL; 00487 else 00488 { 00489 name_copy[prefix_len] = '\0'; 00490 context = lookup_typename (lang, arch, 00491 name_copy, 00492 parent, 1); 00493 } 00494 00495 if (context == NULL) 00496 break; 00497 00498 result 00499 = search_symbol_list (name, 00500 TYPE_N_TEMPLATE_ARGUMENTS (context), 00501 TYPE_TEMPLATE_ARGUMENTS (context)); 00502 if (result != NULL) 00503 { 00504 do_cleanups (cleanups); 00505 return result; 00506 } 00507 } 00508 00509 do_cleanups (cleanups); 00510 } 00511 } 00512 00513 return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1); 00514 } 00515 00516 /* Searches for NAME in the current namespace, and by applying 00517 relevant import statements belonging to BLOCK and its parents. 00518 SCOPE is the namespace scope of the context in which the search is 00519 being evaluated. */ 00520 00521 struct symbol* 00522 cp_lookup_symbol_namespace (const char *scope, 00523 const char *name, 00524 const struct block *block, 00525 const domain_enum domain) 00526 { 00527 struct symbol *sym; 00528 00529 /* First, try to find the symbol in the given namespace. */ 00530 sym = cp_lookup_symbol_in_namespace (scope, name, 00531 block, domain, 1); 00532 if (sym != NULL) 00533 return sym; 00534 00535 /* Search for name in namespaces imported to this and parent 00536 blocks. */ 00537 while (block != NULL) 00538 { 00539 sym = cp_lookup_symbol_imports (scope, name, block, 00540 domain, 0, 1); 00541 00542 if (sym) 00543 return sym; 00544 00545 block = BLOCK_SUPERBLOCK (block); 00546 } 00547 00548 return NULL; 00549 } 00550 00551 /* Lookup NAME at namespace scope (or, in C terms, in static and 00552 global variables). SCOPE is the namespace that the current 00553 function is defined within; only consider namespaces whose length 00554 is at least SCOPE_LEN. Other arguments are as in 00555 cp_lookup_symbol_nonlocal. 00556 00557 For example, if we're within a function A::B::f and looking for a 00558 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and 00559 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, 00560 but with SCOPE_LEN = 1. And then it calls itself with NAME and 00561 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for 00562 "A::B::x"; if it doesn't find it, then the second call looks for 00563 "A::x", and if that call fails, then the first call looks for 00564 "x". */ 00565 00566 static struct symbol * 00567 lookup_namespace_scope (const char *name, 00568 const struct block *block, 00569 const domain_enum domain, 00570 const char *scope, 00571 int scope_len) 00572 { 00573 char *namespace; 00574 00575 if (scope[scope_len] != '\0') 00576 { 00577 /* Recursively search for names in child namespaces first. */ 00578 00579 struct symbol *sym; 00580 int new_scope_len = scope_len; 00581 00582 /* If the current scope is followed by "::", skip past that. */ 00583 if (new_scope_len != 0) 00584 { 00585 gdb_assert (scope[new_scope_len] == ':'); 00586 new_scope_len += 2; 00587 } 00588 new_scope_len += cp_find_first_component (scope + new_scope_len); 00589 sym = lookup_namespace_scope (name, block, domain, 00590 scope, new_scope_len); 00591 if (sym != NULL) 00592 return sym; 00593 } 00594 00595 /* Okay, we didn't find a match in our children, so look for the 00596 name in the current namespace. */ 00597 00598 namespace = alloca (scope_len + 1); 00599 strncpy (namespace, scope, scope_len); 00600 namespace[scope_len] = '\0'; 00601 return cp_lookup_symbol_in_namespace (namespace, name, 00602 block, domain, 1); 00603 } 00604 00605 /* Look up NAME in BLOCK's static block and in global blocks. If 00606 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located 00607 within an anonymous namespace. If SEARCH is non-zero, search through 00608 base classes for a matching symbol. Other arguments are as in 00609 cp_lookup_symbol_nonlocal. */ 00610 00611 static struct symbol * 00612 lookup_symbol_file (const char *name, 00613 const struct block *block, 00614 const domain_enum domain, 00615 int anonymous_namespace, int search) 00616 { 00617 struct symbol *sym = NULL; 00618 00619 sym = lookup_symbol_static (name, block, domain); 00620 if (sym != NULL) 00621 return sym; 00622 00623 if (anonymous_namespace) 00624 { 00625 /* Symbols defined in anonymous namespaces have external linkage 00626 but should be treated as local to a single file nonetheless. 00627 So we only search the current file's global block. */ 00628 00629 const struct block *global_block = block_global_block (block); 00630 00631 if (global_block != NULL) 00632 sym = lookup_symbol_aux_block (name, global_block, domain); 00633 } 00634 else 00635 { 00636 sym = lookup_symbol_global (name, block, domain); 00637 } 00638 00639 if (sym != NULL) 00640 return sym; 00641 00642 if (search) 00643 { 00644 char *klass, *nested; 00645 unsigned int prefix_len; 00646 struct cleanup *cleanup; 00647 struct symbol *klass_sym; 00648 00649 /* A simple lookup failed. Check if the symbol was defined in 00650 a base class. */ 00651 00652 cleanup = make_cleanup (null_cleanup, NULL); 00653 00654 /* Find the name of the class and the name of the method, 00655 variable, etc. */ 00656 prefix_len = cp_entire_prefix_len (name); 00657 00658 /* If no prefix was found, search "this". */ 00659 if (prefix_len == 0) 00660 { 00661 struct type *type; 00662 struct symbol *this; 00663 00664 this = lookup_language_this (language_def (language_cplus), block); 00665 if (this == NULL) 00666 { 00667 do_cleanups (cleanup); 00668 return NULL; 00669 } 00670 00671 type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this))); 00672 klass = xstrdup (TYPE_NAME (type)); 00673 nested = xstrdup (name); 00674 } 00675 else 00676 { 00677 /* The class name is everything up to and including PREFIX_LEN. */ 00678 klass = savestring (name, prefix_len); 00679 00680 /* The rest of the name is everything else past the initial scope 00681 operator. */ 00682 nested = xstrdup (name + prefix_len + 2); 00683 } 00684 00685 /* Add cleanups to free memory for these strings. */ 00686 make_cleanup (xfree, klass); 00687 make_cleanup (xfree, nested); 00688 00689 /* Lookup a class named KLASS. If none is found, there is nothing 00690 more that can be done. */ 00691 klass_sym = lookup_symbol_global (klass, block, domain); 00692 if (klass_sym == NULL) 00693 { 00694 do_cleanups (cleanup); 00695 return NULL; 00696 } 00697 00698 /* Look for a symbol named NESTED in this class. */ 00699 sym = cp_lookup_nested_symbol (SYMBOL_TYPE (klass_sym), nested, block); 00700 do_cleanups (cleanup); 00701 } 00702 00703 return sym; 00704 } 00705 00706 /* Search through the base classes of PARENT_TYPE for a symbol named 00707 NAME in block BLOCK. */ 00708 00709 static struct symbol * 00710 find_symbol_in_baseclass (struct type *parent_type, const char *name, 00711 const struct block *block) 00712 { 00713 int i; 00714 struct symbol *sym; 00715 struct cleanup *cleanup; 00716 char *concatenated_name; 00717 00718 sym = NULL; 00719 concatenated_name = NULL; 00720 cleanup = make_cleanup (free_current_contents, &concatenated_name); 00721 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i) 00722 { 00723 size_t len; 00724 struct type *base_type = TYPE_BASECLASS (parent_type, i); 00725 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i); 00726 00727 if (base_name == NULL) 00728 continue; 00729 00730 /* Search this particular base class. */ 00731 sym = cp_lookup_symbol_in_namespace (base_name, name, block, 00732 VAR_DOMAIN, 0); 00733 if (sym != NULL) 00734 break; 00735 00736 /* Now search all static file-level symbols. We have to do this for 00737 things like typedefs in the class. First search in this symtab, 00738 what we want is possibly there. */ 00739 len = strlen (base_name) + 2 + strlen (name) + 1; 00740 concatenated_name = xrealloc (concatenated_name, len); 00741 xsnprintf (concatenated_name, len, "%s::%s", base_name, name); 00742 sym = lookup_symbol_static (concatenated_name, block, VAR_DOMAIN); 00743 if (sym != NULL) 00744 break; 00745 00746 /* Nope. We now have to search all static blocks in all objfiles, 00747 even if block != NULL, because there's no guarantees as to which 00748 symtab the symbol we want is in. */ 00749 sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN); 00750 if (sym != NULL) 00751 break; 00752 00753 /* If this class has base classes, search them next. */ 00754 CHECK_TYPEDEF (base_type); 00755 if (TYPE_N_BASECLASSES (base_type) > 0) 00756 { 00757 sym = find_symbol_in_baseclass (base_type, name, block); 00758 if (sym != NULL) 00759 break; 00760 } 00761 } 00762 00763 do_cleanups (cleanup); 00764 return sym; 00765 } 00766 00767 /* Look up a symbol named NESTED_NAME that is nested inside the C++ 00768 class or namespace given by PARENT_TYPE, from within the context 00769 given by BLOCK. Return NULL if there is no such nested type. */ 00770 00771 struct symbol * 00772 cp_lookup_nested_symbol (struct type *parent_type, 00773 const char *nested_name, 00774 const struct block *block) 00775 { 00776 /* type_name_no_tag_required provides better error reporting using the 00777 original type. */ 00778 struct type *saved_parent_type = parent_type; 00779 00780 CHECK_TYPEDEF (parent_type); 00781 00782 switch (TYPE_CODE (parent_type)) 00783 { 00784 case TYPE_CODE_STRUCT: 00785 case TYPE_CODE_NAMESPACE: 00786 case TYPE_CODE_UNION: 00787 { 00788 /* NOTE: carlton/2003-11-10: We don't treat C++ class members 00789 of classes like, say, data or function members. Instead, 00790 they're just represented by symbols whose names are 00791 qualified by the name of the surrounding class. This is 00792 just like members of namespaces; in particular, 00793 lookup_symbol_namespace works when looking them up. */ 00794 00795 int size; 00796 const char *parent_name = type_name_no_tag_or_error (saved_parent_type); 00797 struct symbol *sym 00798 = cp_lookup_symbol_in_namespace (parent_name, nested_name, 00799 block, VAR_DOMAIN, 0); 00800 char *concatenated_name; 00801 00802 if (sym != NULL) 00803 return sym; 00804 00805 /* Now search all static file-level symbols. We have to do this 00806 for things like typedefs in the class. We do not try to 00807 guess any imported namespace as even the fully specified 00808 namespace search is already not C++ compliant and more 00809 assumptions could make it too magic. */ 00810 00811 size = strlen (parent_name) + 2 + strlen (nested_name) + 1; 00812 concatenated_name = alloca (size); 00813 xsnprintf (concatenated_name, size, "%s::%s", 00814 parent_name, nested_name); 00815 sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN); 00816 if (sym != NULL) 00817 return sym; 00818 00819 /* If no matching symbols were found, try searching any 00820 base classes. */ 00821 return find_symbol_in_baseclass (parent_type, nested_name, block); 00822 } 00823 00824 case TYPE_CODE_FUNC: 00825 case TYPE_CODE_METHOD: 00826 return NULL; 00827 00828 default: 00829 internal_error (__FILE__, __LINE__, 00830 _("cp_lookup_nested_symbol called " 00831 "on a non-aggregate type.")); 00832 } 00833 } 00834 00835 /* The C++-version of lookup_transparent_type. */ 00836 00837 /* FIXME: carlton/2004-01-16: The problem that this is trying to 00838 address is that, unfortunately, sometimes NAME is wrong: it may not 00839 include the name of namespaces enclosing the type in question. 00840 lookup_transparent_type gets called when the type in question 00841 is a declaration, and we're trying to find its definition; but, for 00842 declarations, our type name deduction mechanism doesn't work. 00843 There's nothing we can do to fix this in general, I think, in the 00844 absence of debug information about namespaces (I've filed PR 00845 gdb/1511 about this); until such debug information becomes more 00846 prevalent, one heuristic which sometimes looks is to search for the 00847 definition in namespaces containing the current namespace. 00848 00849 We should delete this functions once the appropriate debug 00850 information becomes more widespread. (GCC 3.4 will be the first 00851 released version of GCC with such information.) */ 00852 00853 struct type * 00854 cp_lookup_transparent_type (const char *name) 00855 { 00856 /* First, try the honest way of looking up the definition. */ 00857 struct type *t = basic_lookup_transparent_type (name); 00858 const char *scope; 00859 00860 if (t != NULL) 00861 return t; 00862 00863 /* If that doesn't work and we're within a namespace, look there 00864 instead. */ 00865 scope = block_scope (get_selected_block (0)); 00866 00867 if (scope[0] == '\0') 00868 return NULL; 00869 00870 return cp_lookup_transparent_type_loop (name, scope, 0); 00871 } 00872 00873 /* Lookup the type definition associated to NAME in namespaces/classes 00874 containing SCOPE whose name is strictly longer than LENGTH. LENGTH 00875 must be the index of the start of a component of SCOPE. */ 00876 00877 static struct type * 00878 cp_lookup_transparent_type_loop (const char *name, 00879 const char *scope, 00880 int length) 00881 { 00882 int scope_length = length + cp_find_first_component (scope + length); 00883 char *full_name; 00884 00885 /* If the current scope is followed by "::", look in the next 00886 component. */ 00887 if (scope[scope_length] == ':') 00888 { 00889 struct type *retval 00890 = cp_lookup_transparent_type_loop (name, scope, 00891 scope_length + 2); 00892 00893 if (retval != NULL) 00894 return retval; 00895 } 00896 00897 full_name = alloca (scope_length + 2 + strlen (name) + 1); 00898 strncpy (full_name, scope, scope_length); 00899 strncpy (full_name + scope_length, "::", 2); 00900 strcpy (full_name + scope_length + 2, name); 00901 00902 return basic_lookup_transparent_type (full_name); 00903 } 00904 00905 /* This used to do something but was removed when it became 00906 obsolete. */ 00907 00908 static void 00909 maintenance_cplus_namespace (char *args, int from_tty) 00910 { 00911 printf_unfiltered (_("The `maint namespace' command was removed.\n")); 00912 } 00913 00914 /* Provide a prototype to silence -Wmissing-prototypes. */ 00915 extern initialize_file_ftype _initialize_cp_namespace; 00916 00917 void 00918 _initialize_cp_namespace (void) 00919 { 00920 struct cmd_list_element *cmd; 00921 00922 cmd = add_cmd ("namespace", class_maintenance, 00923 maintenance_cplus_namespace, 00924 _("Deprecated placeholder for removed functionality."), 00925 &maint_cplus_cmd_list); 00926 deprecate_cmd (cmd, NULL); 00927 }