1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
4 Contributed by David Carlton and by Kealia, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "cp-support.h"
23 #include "gdb_obstack.h"
29 #include "dictionary.h"
35 static struct symbol *
36 cp_lookup_nested_symbol_1 (struct type *container_type,
37 const char *nested_name,
38 const char *concatenated_name,
39 const struct block *block,
40 const domain_enum domain,
41 int basic_lookup, int is_in_anonymous);
43 static struct type *cp_lookup_transparent_type_loop (const char *name,
47 /* Check to see if SYMBOL refers to an object contained within an
48 anonymous namespace; if so, add an appropriate using directive. */
51 cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
52 struct objfile *const objfile)
54 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
56 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
57 unsigned int previous_component;
58 unsigned int next_component;
60 /* Start with a quick-and-dirty check for mention of "(anonymous
63 if (!cp_is_in_anonymous (name))
66 previous_component = 0;
67 next_component = cp_find_first_component (name + previous_component);
69 while (name[next_component] == ':')
71 if (((next_component - previous_component)
72 == CP_ANONYMOUS_NAMESPACE_LEN)
73 && strncmp (name + previous_component,
74 CP_ANONYMOUS_NAMESPACE_STR,
75 CP_ANONYMOUS_NAMESPACE_LEN) == 0)
77 int dest_len = (previous_component == 0
78 ? 0 : previous_component - 2);
79 int src_len = next_component;
81 char *dest = alloca (dest_len + 1);
82 char *src = alloca (src_len + 1);
84 memcpy (dest, name, dest_len);
85 memcpy (src, name, src_len);
87 dest[dest_len] = '\0';
90 /* We've found a component of the name that's an
91 anonymous namespace. So add symbols in it to the
92 namespace given by the previous component if there is
93 one, or to the global namespace if there isn't. */
94 cp_add_using_directive (dest, src, NULL, NULL, NULL, 1,
95 &objfile->objfile_obstack);
97 /* The "+ 2" is for the "::". */
98 previous_component = next_component + 2;
99 next_component = (previous_component
100 + cp_find_first_component (name
101 + previous_component));
106 /* Add a using directive to using_directives. If the using directive
107 in question has already been added, don't add it twice.
109 Create a new struct using_direct which imports the namespace SRC
110 into the scope DEST. ALIAS is the name of the imported namespace
111 in the current scope. If ALIAS is NULL then the namespace is known
112 by its original name. DECLARATION is the name if the imported
113 varable if this is a declaration import (Eg. using A::x), otherwise
114 it is NULL. EXCLUDES is a list of names not to import from an
115 imported module or NULL. If COPY_NAMES is non-zero, then the
116 arguments are copied into newly allocated memory so they can be
117 temporaries. For EXCLUDES the VEC pointers are copied but the
118 pointed to characters are not copied. */
121 cp_add_using_directive (const char *dest,
124 const char *declaration,
125 VEC (const_char_ptr) *excludes,
127 struct obstack *obstack)
129 struct using_direct *current;
130 struct using_direct *newobj;
132 /* Has it already been added? */
134 for (current = using_directives; current != NULL; current = current->next)
139 if (strcmp (current->import_src, src) != 0)
141 if (strcmp (current->import_dest, dest) != 0)
143 if ((alias == NULL && current->alias != NULL)
144 || (alias != NULL && current->alias == NULL)
145 || (alias != NULL && current->alias != NULL
146 && strcmp (alias, current->alias) != 0))
148 if ((declaration == NULL && current->declaration != NULL)
149 || (declaration != NULL && current->declaration == NULL)
150 || (declaration != NULL && current->declaration != NULL
151 && strcmp (declaration, current->declaration) != 0))
154 /* Compare the contents of EXCLUDES. */
155 for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
156 if (current->excludes[ix] == NULL
157 || strcmp (param, current->excludes[ix]) != 0)
159 if (ix < VEC_length (const_char_ptr, excludes)
160 || current->excludes[ix] != NULL)
163 /* Parameters exactly match CURRENT. */
167 newobj = obstack_alloc (obstack, (sizeof (*newobj)
168 + (VEC_length (const_char_ptr, excludes)
169 * sizeof (*newobj->excludes))));
170 memset (newobj, 0, sizeof (*newobj));
174 newobj->import_src = obstack_copy0 (obstack, src, strlen (src));
175 newobj->import_dest = obstack_copy0 (obstack, dest, strlen (dest));
179 newobj->import_src = src;
180 newobj->import_dest = dest;
183 if (alias != NULL && copy_names)
184 newobj->alias = obstack_copy0 (obstack, alias, strlen (alias));
186 newobj->alias = alias;
188 if (declaration != NULL && copy_names)
189 newobj->declaration = obstack_copy0 (obstack,
190 declaration, strlen (declaration));
192 newobj->declaration = declaration;
194 memcpy (newobj->excludes, VEC_address (const_char_ptr, excludes),
195 VEC_length (const_char_ptr, excludes) * sizeof (*newobj->excludes));
196 newobj->excludes[VEC_length (const_char_ptr, excludes)] = NULL;
198 newobj->next = using_directives;
199 using_directives = newobj;
202 /* Test whether or not NAMESPACE looks like it mentions an anonymous
203 namespace; return nonzero if so. */
206 cp_is_in_anonymous (const char *symbol_name)
208 return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR)
212 /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
213 If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
214 within an anonymous namespace. */
216 static struct symbol *
217 cp_basic_lookup_symbol (const char *name, const struct block *block,
218 const domain_enum domain, int is_in_anonymous)
222 sym = lookup_symbol_in_static_block (name, block, domain);
228 /* Symbols defined in anonymous namespaces have external linkage
229 but should be treated as local to a single file nonetheless.
230 So we only search the current file's global block. */
232 const struct block *global_block = block_global_block (block);
234 if (global_block != NULL)
235 sym = lookup_symbol_in_block (name, global_block, domain);
239 sym = lookup_global_symbol (name, block, domain);
245 /* Search bare symbol NAME in DOMAIN in BLOCK.
246 NAME is guaranteed to not have any scope (no "::") in its name, though
247 if for example NAME is a template spec then "::" may appear in the
249 If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
250 that language. Normally we wouldn't need LANGDEF but fortran also uses
252 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
253 if so then also search for NAME in that class. */
255 static struct symbol *
256 cp_lookup_bare_symbol (const struct language_defn *langdef,
257 const char *name, const struct block *block,
258 const domain_enum domain, int search)
262 /* Note: We can't do a simple assert for ':' not being in NAME because
263 ':' may be in the args of a template spec. This isn't intended to be
264 a complete test, just cheap and documentary. */
265 if (strchr (name, '<') == NULL && strchr (name, '(') == NULL)
266 gdb_assert (strchr (name, ':') == NULL);
268 sym = lookup_symbol_in_static_block (name, block, domain);
272 /* If we didn't find a definition for a builtin type in the static block,
273 search for it now. This is actually the right thing to do and can be
274 a massive performance win. E.g., when debugging a program with lots of
275 shared libraries we could search all of them only to find out the
276 builtin type isn't defined in any of them. This is common for types
278 if (langdef != NULL && domain == VAR_DOMAIN)
280 struct gdbarch *gdbarch;
283 gdbarch = target_gdbarch ();
285 gdbarch = block_gdbarch (block);
286 sym = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
291 sym = lookup_global_symbol (name, block, domain);
297 struct symbol *lang_this;
300 lang_this = lookup_language_this (language_def (language_cplus), block);
301 if (lang_this == NULL)
304 type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this)));
305 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
306 This can happen for lambda functions compiled with clang++,
307 which outputs no name for the container class. */
308 if (TYPE_NAME (type) == NULL)
311 /* Look for symbol NAME in this class. */
312 sym = cp_lookup_nested_symbol (type, name, block, domain);
318 /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
319 BLOCK specifies the context in which to perform the search.
320 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
321 the length of the entire scope of NAME (up to, but not including, the last
324 Note: At least in the case of Fortran, which also uses this code, there
325 may be no text after the last "::". */
327 static struct symbol *
328 cp_search_static_and_baseclasses (const char *name,
329 const struct block *block,
330 const domain_enum domain,
331 unsigned int prefix_len,
335 char *klass, *nested;
336 struct cleanup *cleanup;
337 struct symbol *klass_sym;
338 struct type *klass_type;
340 /* The test here uses <= instead of < because Fortran also uses this,
341 and the module.exp testcase will pass "modmany::" for NAME here. */
342 gdb_assert (prefix_len + 2 <= strlen (name));
343 gdb_assert (name[prefix_len + 1] == ':');
345 /* Find the name of the class and the name of the method, variable, etc. */
347 /* The class name is everything up to and including PREFIX_LEN. */
348 klass = savestring (name, prefix_len);
350 /* The rest of the name is everything else past the initial scope
352 nested = xstrdup (name + prefix_len + 2);
354 /* Add cleanups to free memory for these strings. */
355 cleanup = make_cleanup (xfree, klass);
356 make_cleanup (xfree, nested);
358 /* Lookup a class named KLASS. If none is found, there is nothing
359 more that can be done. KLASS could be a namespace, so always look
360 in VAR_DOMAIN. This works for classes too because of
361 symbol_matches_domain (which should be replaced with something else,
362 but it's what we have today). */
363 klass_sym = lookup_global_symbol (klass, block, VAR_DOMAIN);
364 if (klass_sym == NULL)
366 do_cleanups (cleanup);
369 klass_type = SYMBOL_TYPE (klass_sym);
371 /* Look for a symbol named NESTED in this class.
372 The caller is assumed to have already have done a basic lookup of NAME.
373 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
374 sym = cp_lookup_nested_symbol_1 (klass_type, nested, name, block, domain,
377 do_cleanups (cleanup);
381 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
382 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
383 through base classes for a matching symbol.
385 Note: Part of the complexity is because NAME may itself specify scope.
386 Part of the complexity is also because this handles the case where
387 there is no scoping in which case we also try looking in the class of
388 "this" if we can compute it. */
390 static struct symbol *
391 cp_lookup_symbol_in_namespace (const char *the_namespace, const char *name,
392 const struct block *block,
393 const domain_enum domain, int search)
395 char *concatenated_name = NULL;
397 unsigned int prefix_len;
400 if (the_namespace[0] != '\0')
402 concatenated_name = alloca (strlen (the_namespace) + 2
403 + strlen (name) + 1);
404 strcpy (concatenated_name, the_namespace);
405 strcat (concatenated_name, "::");
406 strcat (concatenated_name, name);
407 name = concatenated_name;
410 prefix_len = cp_entire_prefix_len (name);
412 return cp_lookup_bare_symbol (NULL, name, block, domain, search);
414 /* This would be simpler if we just called cp_lookup_nested_symbol
415 at this point. But that would require first looking up the containing
416 class/namespace. Since we're only searching static and global blocks
417 there's often no need to first do that lookup. */
420 = the_namespace[0] != '\0' && cp_is_in_anonymous (the_namespace);
421 sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous);
426 sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len,
432 /* Used for cleanups to reset the "searched" flag in case of an error. */
435 reset_directive_searched (void *data)
437 struct using_direct *direct = data;
438 direct->searched = 0;
441 /* Search for NAME by applying all import statements belonging to
442 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
443 search is restricted to using declarations.
451 If SEARCH_PARENTS the search will include imports which are
452 applicable in parents of SCOPE.
462 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
463 namespaces X and Y will be considered. If SEARCH_PARENTS is false
464 only the import of Y is considered.
466 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
467 pass 0 for it. Internally we pass 1 when recursing. */
469 static struct symbol *
470 cp_lookup_symbol_via_imports (const char *scope,
472 const struct block *block,
473 const domain_enum domain,
474 const int search_scope_first,
475 const int declaration_only,
476 const int search_parents)
478 struct using_direct *current;
479 struct symbol *sym = NULL;
482 struct cleanup *searched_cleanup;
484 /* First, try to find the symbol in the given namespace if requested. */
485 if (search_scope_first)
486 sym = cp_lookup_symbol_in_namespace (scope, name,
492 /* Go through the using directives. If any of them add new names to
493 the namespace we're searching in, see if we can find a match by
496 for (current = block_using (block);
498 current = current->next)
500 const char **excludep;
502 len = strlen (current->import_dest);
503 directive_match = (search_parents
504 ? (startswith (scope, current->import_dest)
507 || scope[len] == '\0'))
508 : strcmp (scope, current->import_dest) == 0);
510 /* If the import destination is the current scope or one of its
511 ancestors then it is applicable. */
512 if (directive_match && !current->searched)
514 /* Mark this import as searched so that the recursive call
515 does not search it again. */
516 current->searched = 1;
517 searched_cleanup = make_cleanup (reset_directive_searched,
520 /* If there is an import of a single declaration, compare the
521 imported declaration (after optional renaming by its alias)
522 with the sought out name. If there is a match pass
523 current->import_src as NAMESPACE to direct the search
524 towards the imported namespace. */
525 if (current->declaration
526 && strcmp (name, current->alias
527 ? current->alias : current->declaration) == 0)
528 sym = cp_lookup_symbol_in_namespace (current->import_src,
529 current->declaration,
532 /* If this is a DECLARATION_ONLY search or a symbol was found
533 or this import statement was an import declaration, the
534 search of this import is complete. */
535 if (declaration_only || sym != NULL || current->declaration)
537 current->searched = 0;
538 discard_cleanups (searched_cleanup);
546 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
547 for (excludep = current->excludes; *excludep; excludep++)
548 if (strcmp (name, *excludep) == 0)
552 discard_cleanups (searched_cleanup);
556 if (current->alias != NULL
557 && strcmp (name, current->alias) == 0)
558 /* If the import is creating an alias and the alias matches
559 the sought name. Pass current->import_src as the NAME to
560 direct the search towards the aliased namespace. */
562 sym = cp_lookup_symbol_in_namespace (scope,
566 else if (current->alias == NULL)
568 /* If this import statement creates no alias, pass
569 current->inner as NAMESPACE to direct the search
570 towards the imported namespace. */
571 sym = cp_lookup_symbol_via_imports (current->import_src,
575 current->searched = 0;
576 discard_cleanups (searched_cleanup);
586 /* Helper function that searches an array of symbols for one named NAME. */
588 static struct symbol *
589 search_symbol_list (const char *name, int num,
590 struct symbol **syms)
594 /* Maybe we should store a dictionary in here instead. */
595 for (i = 0; i < num; ++i)
597 if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
603 /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it
604 searches through the template parameters of the function and the
608 cp_lookup_symbol_imports_or_template (const char *scope,
610 const struct block *block,
611 const domain_enum domain)
613 struct symbol *function = BLOCK_FUNCTION (block);
614 struct symbol *result;
616 if (symbol_lookup_debug)
618 fprintf_unfiltered (gdb_stdlog,
619 "cp_lookup_symbol_imports_or_template"
620 " (%s, %s, %s, %s)\n",
621 scope, name, host_address_to_string (block),
622 domain_name (domain));
625 if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
627 /* Search the function's template parameters. */
628 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
630 struct template_symbol *templ
631 = (struct template_symbol *) function;
633 result = search_symbol_list (name,
634 templ->n_template_arguments,
635 templ->template_arguments);
638 if (symbol_lookup_debug)
640 fprintf_unfiltered (gdb_stdlog,
641 "cp_lookup_symbol_imports_or_template"
643 host_address_to_string (result));
649 /* Search the template parameters of the function's defining
651 if (SYMBOL_NATURAL_NAME (function))
653 struct type *context;
654 char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
655 struct cleanup *cleanups = make_cleanup (xfree, name_copy);
656 const struct language_defn *lang = language_def (language_cplus);
657 struct gdbarch *arch = symbol_arch (function);
658 const struct block *parent = BLOCK_SUPERBLOCK (block);
662 unsigned int prefix_len = cp_entire_prefix_len (name_copy);
668 name_copy[prefix_len] = '\0';
669 context = lookup_typename (lang, arch,
678 = search_symbol_list (name,
679 TYPE_N_TEMPLATE_ARGUMENTS (context),
680 TYPE_TEMPLATE_ARGUMENTS (context));
683 do_cleanups (cleanups);
684 if (symbol_lookup_debug)
686 fprintf_unfiltered (gdb_stdlog,
687 "cp_lookup_symbol_imports_or_template"
689 host_address_to_string (result));
695 do_cleanups (cleanups);
699 result = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1, 1);
700 if (symbol_lookup_debug)
702 fprintf_unfiltered (gdb_stdlog,
703 "cp_lookup_symbol_imports_or_template (...) = %s\n",
705 ? host_address_to_string (result) : "NULL");
710 /* Search for NAME by applying relevant import statements belonging to BLOCK
711 and its parents. SCOPE is the namespace scope of the context in which the
712 search is being evaluated. */
714 static struct symbol *
715 cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
716 const struct block *block,
717 const domain_enum domain)
721 while (block != NULL)
723 sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 0, 1);
727 block = BLOCK_SUPERBLOCK (block);
733 /* Searches for NAME in the current namespace, and by applying
734 relevant import statements belonging to BLOCK and its parents.
735 SCOPE is the namespace scope of the context in which the search is
739 cp_lookup_symbol_namespace (const char *scope,
741 const struct block *block,
742 const domain_enum domain)
746 if (symbol_lookup_debug)
748 fprintf_unfiltered (gdb_stdlog,
749 "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n",
750 scope, name, host_address_to_string (block),
751 domain_name (domain));
754 /* First, try to find the symbol in the given namespace. */
755 sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);
757 /* Search for name in namespaces imported to this and parent blocks. */
759 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
761 if (symbol_lookup_debug)
763 fprintf_unfiltered (gdb_stdlog,
764 "cp_lookup_symbol_namespace (...) = %s\n",
765 sym != NULL ? host_address_to_string (sym) : "NULL");
770 /* Lookup NAME at namespace scope (or, in C terms, in static and
771 global variables). SCOPE is the namespace that the current
772 function is defined within; only consider namespaces whose length
773 is at least SCOPE_LEN. Other arguments are as in
774 cp_lookup_symbol_nonlocal.
776 For example, if we're within a function A::B::f and looking for a
777 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
778 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
779 but with SCOPE_LEN = 1. And then it calls itself with NAME and
780 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
781 "A::B::x"; if it doesn't find it, then the second call looks for
782 "A::x", and if that call fails, then the first call looks for
785 static struct symbol *
786 lookup_namespace_scope (const struct language_defn *langdef,
788 const struct block *block,
789 const domain_enum domain,
795 if (scope[scope_len] != '\0')
797 /* Recursively search for names in child namespaces first. */
800 int new_scope_len = scope_len;
802 /* If the current scope is followed by "::", skip past that. */
803 if (new_scope_len != 0)
805 gdb_assert (scope[new_scope_len] == ':');
808 new_scope_len += cp_find_first_component (scope + new_scope_len);
809 sym = lookup_namespace_scope (langdef, name, block, domain,
810 scope, new_scope_len);
815 /* Okay, we didn't find a match in our children, so look for the
816 name in the current namespace.
818 If we there is no scope and we know we have a bare symbol, then short
819 circuit everything and call cp_lookup_bare_symbol directly.
820 This isn't an optimization, rather it allows us to pass LANGDEF which
821 is needed for primitive type lookup. The test doesn't have to be
822 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
823 template symbol with "::" in the argument list) then
824 cp_lookup_symbol_in_namespace will catch it. */
826 if (scope_len == 0 && strchr (name, ':') == NULL)
827 return cp_lookup_bare_symbol (langdef, name, block, domain, 1);
829 the_namespace = alloca (scope_len + 1);
830 strncpy (the_namespace, scope, scope_len);
831 the_namespace[scope_len] = '\0';
832 return cp_lookup_symbol_in_namespace (the_namespace, name,
836 /* The C++-specific version of name lookup for static and global
837 names. This makes sure that names get looked for in all namespaces
838 that are in scope. NAME is the natural name of the symbol that
839 we're looking for, BLOCK is the block that we're searching within,
840 DOMAIN says what kind of symbols we're looking for. */
843 cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
845 const struct block *block,
846 const domain_enum domain)
849 const char *scope = block_scope (block);
851 if (symbol_lookup_debug)
853 fprintf_unfiltered (gdb_stdlog,
854 "cp_lookup_symbol_non_local"
855 " (%s, %s (scope %s), %s)\n",
856 name, host_address_to_string (block), scope,
857 domain_name (domain));
860 /* First, try to find the symbol in the given namespace, and all
861 containing namespaces. */
862 sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);
864 /* Search for name in namespaces imported to this and parent blocks. */
866 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
868 if (symbol_lookup_debug)
870 fprintf_unfiltered (gdb_stdlog,
871 "cp_lookup_symbol_nonlocal (...) = %s\n",
872 sym != NULL ? host_address_to_string (sym) : "NULL");
877 /* Search through the base classes of PARENT_TYPE for a base class
878 named NAME and return its type. If not found, return NULL. */
881 cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
885 CHECK_TYPEDEF (parent_type);
886 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
888 struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
889 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
891 if (base_name == NULL)
894 if (streq (base_name, name))
897 type = cp_find_type_baseclass_by_name (type, name);
905 /* Search through the base classes of PARENT_TYPE for a symbol named
906 NAME in block BLOCK. */
908 static struct symbol *
909 find_symbol_in_baseclass (struct type *parent_type, const char *name,
910 const struct block *block, const domain_enum domain,
915 struct cleanup *cleanup;
916 char *concatenated_name;
919 concatenated_name = NULL;
920 cleanup = make_cleanup (free_current_contents, &concatenated_name);
922 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
925 struct type *base_type = TYPE_BASECLASS (parent_type, i);
926 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
928 if (base_name == NULL)
931 len = strlen (base_name) + 2 + strlen (name) + 1;
932 concatenated_name = xrealloc (concatenated_name, len);
933 xsnprintf (concatenated_name, len, "%s::%s", base_name, name);
935 sym = cp_lookup_nested_symbol_1 (base_type, name, concatenated_name,
936 block, domain, 1, is_in_anonymous);
941 do_cleanups (cleanup);
945 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
946 and within the context of BLOCK.
947 NESTED_NAME may have scope ("::").
948 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
949 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
950 passed as an argument so that callers can control how space for it is
952 If BASIC_LOOKUP is non-zero then perform a basic lookup of
953 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
954 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
957 static struct symbol *
958 cp_lookup_nested_symbol_1 (struct type *container_type,
959 const char *nested_name,
960 const char *concatenated_name,
961 const struct block *block,
962 const domain_enum domain,
963 int basic_lookup, int is_in_anonymous)
967 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
968 of classes like, say, data or function members. Instead,
969 they're just represented by symbols whose names are
970 qualified by the name of the surrounding class. This is
971 just like members of namespaces; in particular,
972 cp_basic_lookup_symbol works when looking them up. */
976 sym = cp_basic_lookup_symbol (concatenated_name, block, domain,
982 /* Now search all static file-level symbols. We have to do this for things
983 like typedefs in the class. We do not try to guess any imported
984 namespace as even the fully specified namespace search is already not
985 C++ compliant and more assumptions could make it too magic. */
987 /* First search in this symtab, what we want is possibly there. */
988 sym = lookup_symbol_in_static_block (concatenated_name, block, domain);
992 /* Nope. We now have to search all static blocks in all objfiles,
993 even if block != NULL, because there's no guarantees as to which
994 symtab the symbol we want is in. Except for symbols defined in
995 anonymous namespaces should be treated as local to a single file,
996 which we just searched. */
997 if (!is_in_anonymous)
999 sym = lookup_static_symbol (concatenated_name, domain);
1004 /* If this is a class with baseclasses, search them next. */
1005 CHECK_TYPEDEF (container_type);
1006 if (TYPE_N_BASECLASSES (container_type) > 0)
1008 sym = find_symbol_in_baseclass (container_type, nested_name, block,
1009 domain, is_in_anonymous);
1017 /* Look up a symbol named NESTED_NAME that is nested inside the C++
1018 class or namespace given by PARENT_TYPE, from within the context
1019 given by BLOCK, and in DOMAIN.
1020 Return NULL if there is no such nested symbol. */
1023 cp_lookup_nested_symbol (struct type *parent_type,
1024 const char *nested_name,
1025 const struct block *block,
1026 const domain_enum domain)
1028 /* type_name_no_tag_or_error provides better error reporting using the
1030 struct type *saved_parent_type = parent_type;
1032 CHECK_TYPEDEF (parent_type);
1034 if (symbol_lookup_debug)
1036 const char *type_name = type_name_no_tag (saved_parent_type);
1038 fprintf_unfiltered (gdb_stdlog,
1039 "cp_lookup_nested_symbol (%s, %s, %s, %s)\n",
1040 type_name != NULL ? type_name : "unnamed",
1041 nested_name, host_address_to_string (block),
1042 domain_name (domain));
1045 switch (TYPE_CODE (parent_type))
1047 case TYPE_CODE_STRUCT:
1048 case TYPE_CODE_NAMESPACE:
1049 case TYPE_CODE_UNION:
1050 case TYPE_CODE_ENUM:
1051 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
1052 specific code to lookup nested symbols in modules, by calling the
1053 function pointer la_lookup_symbol_nonlocal, which ends up here. */
1054 case TYPE_CODE_MODULE:
1057 const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
1059 char *concatenated_name;
1060 int is_in_anonymous;
1062 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
1063 concatenated_name = alloca (size);
1064 xsnprintf (concatenated_name, size, "%s::%s",
1065 parent_name, nested_name);
1066 is_in_anonymous = cp_is_in_anonymous (concatenated_name);
1068 sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
1069 concatenated_name, block, domain,
1070 1, is_in_anonymous);
1072 if (symbol_lookup_debug)
1074 fprintf_unfiltered (gdb_stdlog,
1075 "cp_lookup_nested_symbol (...) = %s\n",
1077 ? host_address_to_string (sym) : "NULL");
1082 case TYPE_CODE_FUNC:
1083 case TYPE_CODE_METHOD:
1084 if (symbol_lookup_debug)
1086 fprintf_unfiltered (gdb_stdlog,
1087 "cp_lookup_nested_symbol (...) = NULL"
1088 " (func/method)\n");
1093 internal_error (__FILE__, __LINE__,
1094 _("cp_lookup_nested_symbol called "
1095 "on a non-aggregate type."));
1099 /* The C++-version of lookup_transparent_type. */
1101 /* FIXME: carlton/2004-01-16: The problem that this is trying to
1102 address is that, unfortunately, sometimes NAME is wrong: it may not
1103 include the name of namespaces enclosing the type in question.
1104 lookup_transparent_type gets called when the type in question
1105 is a declaration, and we're trying to find its definition; but, for
1106 declarations, our type name deduction mechanism doesn't work.
1107 There's nothing we can do to fix this in general, I think, in the
1108 absence of debug information about namespaces (I've filed PR
1109 gdb/1511 about this); until such debug information becomes more
1110 prevalent, one heuristic which sometimes looks is to search for the
1111 definition in namespaces containing the current namespace.
1113 We should delete this functions once the appropriate debug
1114 information becomes more widespread. (GCC 3.4 will be the first
1115 released version of GCC with such information.) */
1118 cp_lookup_transparent_type (const char *name)
1120 /* First, try the honest way of looking up the definition. */
1121 struct type *t = basic_lookup_transparent_type (name);
1127 /* If that doesn't work and we're within a namespace, look there
1129 scope = block_scope (get_selected_block (0));
1131 if (scope[0] == '\0')
1134 return cp_lookup_transparent_type_loop (name, scope, 0);
1137 /* Lookup the type definition associated to NAME in namespaces/classes
1138 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1139 must be the index of the start of a component of SCOPE. */
1141 static struct type *
1142 cp_lookup_transparent_type_loop (const char *name,
1146 int scope_length = length + cp_find_first_component (scope + length);
1149 /* If the current scope is followed by "::", look in the next
1151 if (scope[scope_length] == ':')
1154 = cp_lookup_transparent_type_loop (name, scope,
1161 full_name = alloca (scope_length + 2 + strlen (name) + 1);
1162 strncpy (full_name, scope, scope_length);
1163 strncpy (full_name + scope_length, "::", 2);
1164 strcpy (full_name + scope_length + 2, name);
1166 return basic_lookup_transparent_type (full_name);
1169 /* This used to do something but was removed when it became
1173 maintenance_cplus_namespace (char *args, int from_tty)
1175 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
1178 /* Provide a prototype to silence -Wmissing-prototypes. */
1179 extern initialize_file_ftype _initialize_cp_namespace;
1182 _initialize_cp_namespace (void)
1184 struct cmd_list_element *cmd;
1186 cmd = add_cmd ("namespace", class_maintenance,
1187 maintenance_cplus_namespace,
1188 _("Deprecated placeholder for removed functionality."),
1189 &maint_cplus_cmd_list);
1190 deprecate_cmd (cmd, NULL);