1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003-2024 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/>. */
21 #include "cp-support.h"
22 #include "gdbsupport/gdb_obstack.h"
28 #include "dictionary.h"
33 #include "namespace.h"
39 static struct block_symbol
40 cp_lookup_nested_symbol_1 (struct type
*container_type
,
41 const char *nested_name
,
42 const char *concatenated_name
,
43 const struct block
*block
,
44 const domain_search_flags domain
,
45 int basic_lookup
, int is_in_anonymous
);
47 static struct type
*cp_lookup_transparent_type_loop (const char *name
,
51 /* Check to see if SYMBOL refers to an object contained within an
52 anonymous namespace; if so, add an appropriate using directive. */
55 cp_scan_for_anonymous_namespaces (struct buildsym_compunit
*compunit
,
56 const struct symbol
*const symbol
,
57 struct objfile
*const objfile
)
59 if (symbol
->demangled_name () != NULL
)
61 const char *name
= symbol
->demangled_name ();
62 unsigned int previous_component
;
63 unsigned int next_component
;
65 /* Start with a quick-and-dirty check for mention of "(anonymous
68 if (!cp_is_in_anonymous (name
))
71 previous_component
= 0;
72 next_component
= cp_find_first_component (name
+ previous_component
);
74 while (name
[next_component
] == ':')
76 if (((next_component
- previous_component
)
77 == CP_ANONYMOUS_NAMESPACE_LEN
)
78 && strncmp (name
+ previous_component
,
79 CP_ANONYMOUS_NAMESPACE_STR
,
80 CP_ANONYMOUS_NAMESPACE_LEN
) == 0)
82 int dest_len
= (previous_component
== 0
83 ? 0 : previous_component
- 2);
84 int src_len
= next_component
;
86 char *dest
= (char *) alloca (dest_len
+ 1);
87 char *src
= (char *) alloca (src_len
+ 1);
89 memcpy (dest
, name
, dest_len
);
90 memcpy (src
, name
, src_len
);
92 dest
[dest_len
] = '\0';
95 /* We've found a component of the name that's an
96 anonymous namespace. So add symbols in it to the
97 namespace given by the previous component if there is
98 one, or to the global namespace if there isn't.
99 The declared line of this using directive can be set
100 to 0, this way it is always considered valid. */
101 std::vector
<const char *> excludes
;
102 add_using_directive (compunit
->get_local_using_directives (),
103 objfile
->intern (dest
), objfile
->intern (src
),
104 nullptr, nullptr, excludes
, 0,
105 &objfile
->objfile_obstack
);
107 /* The "+ 2" is for the "::". */
108 previous_component
= next_component
+ 2;
109 next_component
= (previous_component
110 + cp_find_first_component (name
111 + previous_component
));
116 /* Test whether or not NAMESPACE looks like it mentions an anonymous
117 namespace; return nonzero if so. */
120 cp_is_in_anonymous (const char *symbol_name
)
122 return (strstr (symbol_name
, CP_ANONYMOUS_NAMESPACE_STR
)
126 /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
127 If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
128 within an anonymous namespace. */
130 static struct block_symbol
131 cp_basic_lookup_symbol (const char *name
, const struct block
*block
,
132 const domain_search_flags domain
, int is_in_anonymous
)
134 struct block_symbol sym
;
136 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
137 if (sym
.symbol
!= NULL
)
142 /* Symbols defined in anonymous namespaces have external linkage
143 but should be treated as local to a single file nonetheless.
144 So we only search the current file's global block. */
146 const struct block
*global_block
= block
->global_block ();
148 if (global_block
!= NULL
)
150 sym
.symbol
= lookup_symbol_in_block (name
,
151 symbol_name_match_type::FULL
,
152 global_block
, domain
);
153 sym
.block
= global_block
;
157 sym
= lookup_global_symbol (name
, block
, domain
);
162 /* Search bare symbol NAME in DOMAIN in BLOCK.
163 NAME is guaranteed to not have any scope (no "::") in its name, though
164 if for example NAME is a template spec then "::" may appear in the
166 If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
167 that language. Normally we wouldn't need LANGDEF but fortran also uses
169 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
170 if so then also search for NAME in that class. */
172 static struct block_symbol
173 cp_lookup_bare_symbol (const struct language_defn
*langdef
,
174 const char *name
, const struct block
*block
,
175 const domain_search_flags domain
, int search
)
177 struct block_symbol sym
;
179 /* Note: We can't do a simple assert for ':' not being in NAME because
180 ':' may be in the args of a template spec. This isn't intended to be
181 a complete test, just cheap and documentary. */
182 gdb_assert (strpbrk ("<>()", name
) != nullptr
183 || strstr (name
, "::") == nullptr);
185 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
186 if (sym
.symbol
!= NULL
)
189 /* If we didn't find a definition for a builtin type in the static block,
190 search for it now. This is actually the right thing to do and can be
191 a massive performance win. E.g., when debugging a program with lots of
192 shared libraries we could search all of them only to find out the
193 builtin type isn't defined in any of them. This is common for types
195 if (langdef
!= nullptr && (domain
& SEARCH_TYPE_DOMAIN
) != 0)
197 struct gdbarch
*gdbarch
;
200 gdbarch
= current_inferior ()->arch ();
202 gdbarch
= block
->gdbarch ();
204 = language_lookup_primitive_type_as_symbol (langdef
, gdbarch
, name
);
206 if (sym
.symbol
!= NULL
)
210 sym
= lookup_global_symbol (name
, block
, domain
);
211 if (sym
.symbol
!= NULL
)
216 struct block_symbol lang_this
;
219 lang_this
.symbol
= NULL
;
222 lang_this
= lookup_language_this (langdef
, block
);
224 if (lang_this
.symbol
== NULL
)
228 type
= check_typedef (lang_this
.symbol
->type ()->target_type ());
229 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
230 This can happen for lambda functions compiled with clang++,
231 which outputs no name for the container class. */
232 if (type
->name () == NULL
)
235 /* Look for symbol NAME in this class. */
236 sym
= cp_lookup_nested_symbol (type
, name
, block
, domain
);
242 /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
243 BLOCK specifies the context in which to perform the search.
244 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
245 the length of the entire scope of NAME (up to, but not including, the last
248 Note: At least in the case of Fortran, which also uses this code, there
249 may be no text after the last "::". */
251 static struct block_symbol
252 cp_search_static_and_baseclasses (const char *name
,
253 const struct block
*block
,
254 const domain_search_flags domain
,
255 unsigned int prefix_len
,
258 /* Check for malformed input. */
259 if (prefix_len
+ 2 > strlen (name
) || name
[prefix_len
+ 1] != ':')
262 /* The class, namespace or function name is everything up to and
263 including PREFIX_LEN. */
264 std::string
scope (name
, prefix_len
);
266 /* The rest of the name is everything else past the initial scope
268 const char *nested
= name
+ prefix_len
+ 2;
270 /* Lookup the scope symbol. If none is found, there is nothing more
271 that can be done. SCOPE could be a namespace, a class, or even a
272 function. This code is also used by Fortran, so modules are
273 included in the search as well. */
274 block_symbol scope_sym
275 = lookup_symbol_in_static_block (scope
.c_str (), block
,
277 | SEARCH_FUNCTION_DOMAIN
278 | SEARCH_MODULE_DOMAIN
);
279 if (scope_sym
.symbol
== NULL
)
280 scope_sym
= lookup_global_symbol (scope
.c_str (), block
,
282 | SEARCH_FUNCTION_DOMAIN
283 | SEARCH_MODULE_DOMAIN
);
284 if (scope_sym
.symbol
== NULL
)
287 struct type
*scope_type
= scope_sym
.symbol
->type ();
289 /* If the scope is a function/method, then look up NESTED as a local
290 static variable or type. E.g., "print 'function()::static_var'". */
291 if ((scope_type
->code () == TYPE_CODE_FUNC
292 || scope_type
->code () == TYPE_CODE_METHOD
)
293 && (domain
& (SEARCH_VAR_DOMAIN
| SEARCH_TYPE_DOMAIN
)) != 0)
294 return lookup_symbol (nested
, scope_sym
.symbol
->value_block (),
297 /* Look for a symbol named NESTED in this class/namespace.
298 The caller is assumed to have already have done a basic lookup of NAME.
299 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
300 return cp_lookup_nested_symbol_1 (scope_type
, nested
, name
,
301 block
, domain
, 0, is_in_anonymous
);
304 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
305 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
306 through base classes for a matching symbol.
308 Note: Part of the complexity is because NAME may itself specify scope.
309 Part of the complexity is also because this handles the case where
310 there is no scoping in which case we also try looking in the class of
311 "this" if we can compute it. */
313 static struct block_symbol
314 cp_lookup_symbol_in_namespace (const char *the_namespace
, const char *name
,
315 const struct block
*block
,
316 const domain_search_flags domain
, int search
)
318 char *concatenated_name
= NULL
;
320 unsigned int prefix_len
;
321 struct block_symbol sym
;
323 if (the_namespace
[0] != '\0')
326 = (char *) alloca (strlen (the_namespace
) + 2 + strlen (name
) + 1);
327 strcpy (concatenated_name
, the_namespace
);
328 strcat (concatenated_name
, "::");
329 strcat (concatenated_name
, name
);
330 name
= concatenated_name
;
333 prefix_len
= cp_entire_prefix_len (name
);
335 return cp_lookup_bare_symbol (NULL
, name
, block
, domain
, search
);
337 /* This would be simpler if we just called cp_lookup_nested_symbol
338 at this point. But that would require first looking up the containing
339 class/namespace. Since we're only searching static and global blocks
340 there's often no need to first do that lookup. */
343 = the_namespace
[0] != '\0' && cp_is_in_anonymous (the_namespace
);
344 sym
= cp_basic_lookup_symbol (name
, block
, domain
, is_in_anonymous
);
345 if (sym
.symbol
!= NULL
)
349 sym
= cp_search_static_and_baseclasses (name
, block
, domain
, prefix_len
,
355 /* This version of the function is internal, use the wrapper unless
356 the list of ambiguous symbols is needed.
358 Search for NAME by applying all import statements belonging to
359 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
360 search is restricted to using declarations.
368 If SEARCH_PARENTS the search will include imports which are
369 applicable in parents of SCOPE.
379 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
380 namespaces X and Y will be considered. If SEARCH_PARENTS is false
381 only the import of Y is considered.
383 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
384 pass 0 for it. Internally we pass 1 when recursing. */
387 cp_lookup_symbol_via_imports (const char *scope
,
389 const struct block
*block
,
390 const domain_search_flags domain
,
391 const int search_scope_first
,
392 const int declaration_only
,
393 const int search_parents
,
394 std::map
<std::string
,
395 struct block_symbol
>& found_symbols
)
397 struct block_symbol sym
= {};
401 /* All the symbols we found will be kept in this relational map between
402 the mangled name and the block_symbol found. We do this so that GDB
403 won't incorrectly report an ambiguous symbol for finding the same
406 /* First, try to find the symbol in the given namespace if requested. */
407 if (search_scope_first
)
409 sym
= cp_lookup_symbol_in_namespace (scope
, name
,
411 if (sym
.symbol
!= nullptr)
412 found_symbols
[sym
.symbol
->m_name
] = sym
;
415 /* Due to a GCC bug, we need to know the boundaries of the current block
416 to know if a certain using directive is valid. */
417 symtab_and_line boundary_sal
= find_pc_line (block
->end () - 1, 0);
419 /* Go through the using directives. If any of them add new names to
420 the namespace we're searching in, see if we can find a match by
422 for (using_direct
*current
: block
->get_using ())
424 const char **excludep
;
426 /* If the using directive was below the place we are stopped at,
427 do not use this directive. */
428 if (!current
->valid_line (boundary_sal
.line
))
430 len
= strlen (current
->import_dest
);
431 directive_match
= (search_parents
432 ? (startswith (scope
, current
->import_dest
)
435 || scope
[len
] == '\0'))
436 : strcmp (scope
, current
->import_dest
) == 0);
438 /* If the import destination is the current scope or one of its
439 ancestors then it is applicable. */
440 if (directive_match
&& !current
->searched
)
442 /* Mark this import as searched so that the recursive call
443 does not search it again. */
444 scoped_restore reset_directive_searched
445 = make_scoped_restore (¤t
->searched
, 1);
447 /* If there is an import of a single declaration, compare the
448 imported declaration (after optional renaming by its alias)
449 with the sought out name. If there is a match pass
450 current->import_src as NAMESPACE to direct the search
451 towards the imported namespace. */
452 if (current
->declaration
453 && strcmp (name
, current
->alias
454 ? current
->alias
: current
->declaration
) == 0)
455 sym
= cp_lookup_symbol_in_namespace (current
->import_src
,
456 current
->declaration
,
459 /* If this is a DECLARATION_ONLY search or a symbol was found
460 or this import statement was an import declaration, the
461 search of this import is complete. */
462 if (declaration_only
|| sym
.symbol
!= NULL
|| current
->declaration
)
464 if (sym
.symbol
!= NULL
)
465 found_symbols
[sym
.symbol
->m_name
] = sym
;
470 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
471 for (excludep
= current
->excludes
; *excludep
; excludep
++)
472 if (strcmp (name
, *excludep
) == 0)
477 if (current
->alias
!= NULL
478 && strcmp (name
, current
->alias
) == 0)
479 /* If the import is creating an alias and the alias matches
480 the sought name. Pass current->import_src as the NAME to
481 direct the search towards the aliased namespace. */
483 sym
= cp_lookup_symbol_in_namespace (scope
,
486 found_symbols
[sym
.symbol
->m_name
] = sym
;
488 else if (current
->alias
== NULL
)
490 /* If this import statement creates no alias, pass
491 current->inner as NAMESPACE to direct the search
492 towards the imported namespace. */
493 cp_lookup_symbol_via_imports (current
->import_src
, name
,
494 block
, domain
, 1, 0, 0,
502 /* Wrapper for the actual cp_lookup_symbol_via_imports. This wrapper sets
503 search_scope_first correctly and handles errors if needed. */
504 static struct block_symbol
505 cp_lookup_symbol_via_imports (const char *scope
,
507 const struct block
*block
,
508 const domain_search_flags domain
,
509 const int declaration_only
,
510 const int search_parents
)
512 std::map
<std::string
, struct block_symbol
> found_symbols
;
514 cp_lookup_symbol_via_imports(scope
, name
, block
, domain
, 0,
515 declaration_only
, search_parents
,
518 if (found_symbols
.size () > 1)
520 auto itr
= found_symbols
.cbegin ();
521 std::string error_str
= "Reference to \"";
523 error_str
+= "\" is ambiguous, possibilities are: ";
524 error_str
+= itr
->second
.symbol
->print_name ();
525 for (itr
++; itr
!= found_symbols
.end (); itr
++)
527 error_str
+= " and ";
528 error_str
+= itr
->second
.symbol
->print_name ();
530 error (_("%s"), error_str
.c_str ());
533 if (found_symbols
.size() == 1)
534 return found_symbols
.cbegin ()->second
;
539 /* Search for symbols whose name match NAME in the given SCOPE. */
542 cp_lookup_symbol_imports (const char *scope
,
544 const struct block
*block
,
545 const domain_search_flags domain
)
547 struct symbol
*function
= block
->function ();
549 symbol_lookup_debug_printf
550 ("cp_lookup_symbol_imports (%s, %s, %s, %s)",
551 scope
, name
, host_address_to_string (block
),
552 domain_name (domain
).c_str ());
554 if (function
!= NULL
&& function
->language () == language_cplus
)
556 /* Search the template parameters of the function's defining
558 if (function
->natural_name ())
560 struct type
*context
;
561 std::string
name_copy (function
->natural_name ());
562 const struct language_defn
*lang
= language_def (language_cplus
);
563 const struct block
*parent
= block
->superblock ();
568 unsigned int prefix_len
569 = cp_entire_prefix_len (name_copy
.c_str ());
575 name_copy
.erase (prefix_len
);
576 context
= lookup_typename (lang
,
585 = search_symbol_list (name
,
586 TYPE_N_TEMPLATE_ARGUMENTS (context
),
587 TYPE_TEMPLATE_ARGUMENTS (context
));
590 symbol_lookup_debug_printf
591 ("cp_lookup_symbol_imports (...) = %s",
592 host_address_to_string (sym
));
593 return (struct block_symbol
) {sym
, parent
};
599 struct block_symbol result
600 = cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 1, 1);
601 symbol_lookup_debug_printf ("cp_lookup_symbol_imports (...) = %s\n",
602 result
.symbol
!= nullptr
603 ? host_address_to_string (result
.symbol
) : "NULL");
607 /* Search for NAME by applying relevant import statements belonging to BLOCK
608 and its parents. SCOPE is the namespace scope of the context in which the
609 search is being evaluated. */
611 static struct block_symbol
612 cp_lookup_symbol_via_all_imports (const char *scope
, const char *name
,
613 const struct block
*block
,
614 const domain_search_flags domain
)
616 struct block_symbol sym
;
618 while (block
!= NULL
)
620 sym
= cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 0, 1);
621 if (sym
.symbol
!= nullptr)
624 block
= block
->superblock ();
630 /* Searches for NAME in the current namespace, and by applying
631 relevant import statements belonging to BLOCK and its parents.
632 SCOPE is the namespace scope of the context in which the search is
636 cp_lookup_symbol_namespace (const char *scope
,
638 const struct block
*block
,
639 const domain_search_flags domain
)
641 struct block_symbol sym
;
643 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (%s, %s, %s, %s)",
644 scope
, name
, host_address_to_string (block
),
645 domain_name (domain
).c_str ());
647 /* First, try to find the symbol in the given namespace. */
648 sym
= cp_lookup_symbol_in_namespace (scope
, name
, block
, domain
, 1);
650 /* Search for name in namespaces imported to this and parent blocks. */
651 if (sym
.symbol
== NULL
)
652 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
654 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (...) = %s",
656 ? host_address_to_string (sym
.symbol
) : "NULL");
660 /* Lookup NAME at namespace scope (or, in C terms, in static and
661 global variables). SCOPE is the namespace that the current
662 function is defined within; only consider namespaces whose length
663 is at least SCOPE_LEN. Other arguments are as in
664 cp_lookup_symbol_nonlocal.
666 For example, if we're within a function A::B::f and looking for a
667 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
668 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
669 but with SCOPE_LEN = 1. And then it calls itself with NAME and
670 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
671 "A::B::x"; if it doesn't find it, then the second call looks for
672 "A::x", and if that call fails, then the first call looks for
675 static struct block_symbol
676 lookup_namespace_scope (const struct language_defn
*langdef
,
678 const struct block
*block
,
679 const domain_search_flags domain
,
685 if (scope
[scope_len
] != '\0')
687 /* Recursively search for names in child namespaces first. */
689 struct block_symbol sym
;
690 int new_scope_len
= scope_len
;
692 /* If the current scope is followed by "::", skip past that. */
693 if (new_scope_len
!= 0)
695 gdb_assert (scope
[new_scope_len
] == ':');
698 new_scope_len
+= cp_find_first_component (scope
+ new_scope_len
);
699 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
,
700 scope
, new_scope_len
);
701 if (sym
.symbol
!= NULL
)
705 /* Okay, we didn't find a match in our children, so look for the
706 name in the current namespace.
708 If we there is no scope and we know we have a bare symbol, then short
709 circuit everything and call cp_lookup_bare_symbol directly.
710 This isn't an optimization, rather it allows us to pass LANGDEF which
711 is needed for primitive type lookup. The test doesn't have to be
712 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
713 template symbol with "::" in the argument list) then
714 cp_lookup_symbol_in_namespace will catch it. */
716 if (scope_len
== 0 && strchr (name
, ':') == NULL
)
717 return cp_lookup_bare_symbol (langdef
, name
, block
, domain
, 1);
719 the_namespace
= (char *) alloca (scope_len
+ 1);
720 strncpy (the_namespace
, scope
, scope_len
);
721 the_namespace
[scope_len
] = '\0';
722 return cp_lookup_symbol_in_namespace (the_namespace
, name
,
726 /* The C++-specific version of name lookup for static and global
727 names. This makes sure that names get looked for in all namespaces
728 that are in scope. NAME is the natural name of the symbol that
729 we're looking for, BLOCK is the block that we're searching within,
730 DOMAIN says what kind of symbols we're looking for. */
733 cp_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
735 const struct block
*block
,
736 const domain_search_flags domain
)
738 struct block_symbol sym
;
739 const char *scope
= block
== nullptr ? "" : block
->scope ();
741 symbol_lookup_debug_printf
742 ("cp_lookup_symbol_non_local (%s, %s (scope %s), %s)",
743 name
, host_address_to_string (block
), scope
,
744 domain_name (domain
).c_str ());
746 /* First, try to find the symbol in the given namespace, and all
747 containing namespaces. */
748 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
, scope
, 0);
750 /* Search for name in namespaces imported to this and parent blocks. */
751 if (sym
.symbol
== NULL
)
752 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
754 symbol_lookup_debug_printf ("cp_lookup_symbol_nonlocal (...) = %s",
756 ? host_address_to_string (sym
.symbol
)
761 /* Search through the base classes of PARENT_TYPE for a base class
762 named NAME and return its type. If not found, return NULL. */
765 cp_find_type_baseclass_by_name (struct type
*parent_type
, const char *name
)
769 parent_type
= check_typedef (parent_type
);
770 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
772 struct type
*type
= check_typedef (TYPE_BASECLASS (parent_type
, i
));
773 const char *tdef_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
774 const char *base_name
= type
->name ();
776 if (base_name
== NULL
)
779 if (streq (tdef_name
, name
) || streq (base_name
, name
))
782 type
= cp_find_type_baseclass_by_name (type
, name
);
790 /* Search through the base classes of PARENT_TYPE for a symbol named
791 NAME in block BLOCK. */
793 static struct block_symbol
794 find_symbol_in_baseclass (struct type
*parent_type
, const char *name
,
795 const struct block
*block
,
796 const domain_search_flags domain
,
800 struct block_symbol sym
= {};
802 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
804 struct type
*base_type
= TYPE_BASECLASS (parent_type
, i
);
805 const char *base_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
807 if (base_name
== NULL
)
810 std::string concatenated_name
= std::string (base_name
) + "::" + name
;
812 sym
= cp_lookup_nested_symbol_1 (base_type
, name
,
813 concatenated_name
.c_str (),
814 block
, domain
, 1, is_in_anonymous
);
815 if (sym
.symbol
!= NULL
)
822 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
823 and within the context of BLOCK.
824 NESTED_NAME may have scope ("::").
825 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
826 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
827 passed as an argument so that callers can control how space for it is
829 If BASIC_LOOKUP is non-zero then perform a basic lookup of
830 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
831 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
834 static struct block_symbol
835 cp_lookup_nested_symbol_1 (struct type
*container_type
,
836 const char *nested_name
,
837 const char *concatenated_name
,
838 const struct block
*block
,
839 const domain_search_flags domain
,
840 int basic_lookup
, int is_in_anonymous
)
842 struct block_symbol sym
;
844 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
845 of classes like, say, data or function members. Instead,
846 they're just represented by symbols whose names are
847 qualified by the name of the surrounding class. This is
848 just like members of namespaces; in particular,
849 cp_basic_lookup_symbol works when looking them up. */
853 sym
= cp_basic_lookup_symbol (concatenated_name
, block
, domain
,
855 if (sym
.symbol
!= NULL
)
859 /* Now search all static file-level symbols. We have to do this for things
860 like typedefs in the class. We do not try to guess any imported
861 namespace as even the fully specified namespace search is already not
862 C++ compliant and more assumptions could make it too magic. */
864 /* First search in this symtab, what we want is possibly there. */
865 sym
= lookup_symbol_in_static_block (concatenated_name
, block
, domain
);
866 if (sym
.symbol
!= NULL
)
869 /* Nope. We now have to search all static blocks in all objfiles,
870 even if block != NULL, because there's no guarantees as to which
871 symtab the symbol we want is in. Except for symbols defined in
872 anonymous namespaces should be treated as local to a single file,
873 which we just searched. */
874 if (!is_in_anonymous
)
876 sym
= lookup_static_symbol (concatenated_name
, domain
);
877 if (sym
.symbol
!= NULL
)
881 /* If this is a class with baseclasses, search them next. */
882 container_type
= check_typedef (container_type
);
883 if (TYPE_N_BASECLASSES (container_type
) > 0)
885 sym
= find_symbol_in_baseclass (container_type
, nested_name
, block
,
886 domain
, is_in_anonymous
);
887 if (sym
.symbol
!= NULL
)
894 /* Look up a symbol named NESTED_NAME that is nested inside the C++
895 class or namespace given by PARENT_TYPE, from within the context
896 given by BLOCK, and in DOMAIN.
897 Return NULL if there is no such nested symbol. */
900 cp_lookup_nested_symbol (struct type
*parent_type
,
901 const char *nested_name
,
902 const struct block
*block
,
903 const domain_search_flags domain
)
905 /* type_name_or_error provides better error reporting using the
907 struct type
*saved_parent_type
= parent_type
;
909 parent_type
= check_typedef (parent_type
);
911 if (symbol_lookup_debug
)
913 const char *type_name
= saved_parent_type
->name ();
915 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (%s, %s, %s, %s)",
916 type_name
!= NULL
? type_name
: "unnamed",
917 nested_name
, host_address_to_string (block
),
918 domain_name (domain
).c_str ());
921 switch (parent_type
->code ())
923 case TYPE_CODE_STRUCT
:
924 case TYPE_CODE_NAMESPACE
:
925 case TYPE_CODE_UNION
:
927 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
928 specific code to lookup nested symbols in modules, by calling the
929 method lookup_symbol_nonlocal, which ends up here. */
930 case TYPE_CODE_MODULE
:
933 const char *parent_name
= type_name_or_error (saved_parent_type
);
934 struct block_symbol sym
;
935 char *concatenated_name
;
938 size
= strlen (parent_name
) + 2 + strlen (nested_name
) + 1;
939 concatenated_name
= (char *) alloca (size
);
940 xsnprintf (concatenated_name
, size
, "%s::%s",
941 parent_name
, nested_name
);
942 is_in_anonymous
= cp_is_in_anonymous (concatenated_name
);
944 sym
= cp_lookup_nested_symbol_1 (parent_type
, nested_name
,
945 concatenated_name
, block
, domain
,
948 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (...) = %s",
950 ? host_address_to_string (sym
.symbol
)
956 case TYPE_CODE_METHOD
:
957 symbol_lookup_debug_printf
958 ("cp_lookup_nested_symbol (...) = NULL (func/method)");
962 internal_error (_("cp_lookup_nested_symbol called "
963 "on a non-aggregate type."));
967 /* The C++-version of lookup_transparent_type. */
969 /* FIXME: carlton/2004-01-16: The problem that this is trying to
970 address is that, unfortunately, sometimes NAME is wrong: it may not
971 include the name of namespaces enclosing the type in question.
972 lookup_transparent_type gets called when the type in question
973 is a declaration, and we're trying to find its definition; but, for
974 declarations, our type name deduction mechanism doesn't work.
975 There's nothing we can do to fix this in general, I think, in the
976 absence of debug information about namespaces (I've filed PR
977 gdb/1511 about this); until such debug information becomes more
978 prevalent, one heuristic which sometimes looks is to search for the
979 definition in namespaces containing the current namespace.
981 We should delete this functions once the appropriate debug
982 information becomes more widespread. (GCC 3.4 will be the first
983 released version of GCC with such information.) */
986 cp_lookup_transparent_type (const char *name
, domain_search_flags flags
)
988 /* First, try the honest way of looking up the definition. */
989 struct type
*t
= basic_lookup_transparent_type (name
, flags
);
995 /* If that doesn't work and we're within a namespace, look there
997 const block
*block
= get_selected_block (0);
998 if (block
== nullptr)
1001 scope
= block
->scope ();
1003 if (scope
[0] == '\0')
1006 return cp_lookup_transparent_type_loop (name
, scope
, 0);
1009 /* Lookup the type definition associated to NAME in namespaces/classes
1010 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1011 must be the index of the start of a component of SCOPE. */
1013 static struct type
*
1014 cp_lookup_transparent_type_loop (const char *name
,
1018 int scope_length
= length
+ cp_find_first_component (scope
+ length
);
1021 /* If the current scope is followed by "::", look in the next
1023 if (scope
[scope_length
] == ':')
1026 = cp_lookup_transparent_type_loop (name
, scope
,
1033 full_name
= (char *) alloca (scope_length
+ 2 + strlen (name
) + 1);
1034 strncpy (full_name
, scope
, scope_length
);
1035 memcpy (full_name
+ scope_length
, "::", 2);
1036 strcpy (full_name
+ scope_length
+ 2, name
);
1038 return basic_lookup_transparent_type (full_name
);
1041 /* This used to do something but was removed when it became
1045 maintenance_cplus_namespace (const char *args
, int from_tty
)
1047 gdb_printf (_("The `maint namespace' command was removed.\n"));
1050 void _initialize_cp_namespace ();
1052 _initialize_cp_namespace ()
1054 struct cmd_list_element
*cmd
;
1056 cmd
= add_cmd ("namespace", class_maintenance
,
1057 maintenance_cplus_namespace
,
1058 _("Deprecated placeholder for removed functionality."),
1059 &maint_cplus_cmd_list
);
1060 deprecate_cmd (cmd
, NULL
);