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. 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
) != 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 using_direct
*current
;
398 struct block_symbol sym
= {};
402 /* All the symbols we found will be kept in this relational map between
403 the mangled name and the block_symbol found. We do this so that GDB
404 won't incorrectly report an ambiguous symbol for finding the same
407 /* First, try to find the symbol in the given namespace if requested. */
408 if (search_scope_first
)
410 sym
= cp_lookup_symbol_in_namespace (scope
, name
,
412 if (sym
.symbol
!= nullptr)
413 found_symbols
[sym
.symbol
->m_name
] = sym
;
416 /* Due to a GCC bug, we need to know the boundaries of the current block
417 to know if a certain using directive is valid. */
418 symtab_and_line boundary_sal
= find_pc_line (block
->end () - 1, 0);
420 /* Go through the using directives. If any of them add new names to
421 the namespace we're searching in, see if we can find a match by
423 for (current
= block
->get_using ();
425 current
= current
->next
)
427 const char **excludep
;
429 /* If the using directive was below the place we are stopped at,
430 do not use this directive. */
431 if (!current
->valid_line (boundary_sal
.line
))
433 len
= strlen (current
->import_dest
);
434 directive_match
= (search_parents
435 ? (startswith (scope
, current
->import_dest
)
438 || scope
[len
] == '\0'))
439 : strcmp (scope
, current
->import_dest
) == 0);
441 /* If the import destination is the current scope or one of its
442 ancestors then it is applicable. */
443 if (directive_match
&& !current
->searched
)
445 /* Mark this import as searched so that the recursive call
446 does not search it again. */
447 scoped_restore reset_directive_searched
448 = make_scoped_restore (¤t
->searched
, 1);
450 /* If there is an import of a single declaration, compare the
451 imported declaration (after optional renaming by its alias)
452 with the sought out name. If there is a match pass
453 current->import_src as NAMESPACE to direct the search
454 towards the imported namespace. */
455 if (current
->declaration
456 && strcmp (name
, current
->alias
457 ? current
->alias
: current
->declaration
) == 0)
458 sym
= cp_lookup_symbol_in_namespace (current
->import_src
,
459 current
->declaration
,
462 /* If this is a DECLARATION_ONLY search or a symbol was found
463 or this import statement was an import declaration, the
464 search of this import is complete. */
465 if (declaration_only
|| sym
.symbol
!= NULL
|| current
->declaration
)
467 if (sym
.symbol
!= NULL
)
468 found_symbols
[sym
.symbol
->m_name
] = sym
;
473 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
474 for (excludep
= current
->excludes
; *excludep
; excludep
++)
475 if (strcmp (name
, *excludep
) == 0)
480 if (current
->alias
!= NULL
481 && strcmp (name
, current
->alias
) == 0)
482 /* If the import is creating an alias and the alias matches
483 the sought name. Pass current->import_src as the NAME to
484 direct the search towards the aliased namespace. */
486 sym
= cp_lookup_symbol_in_namespace (scope
,
489 found_symbols
[sym
.symbol
->m_name
] = sym
;
491 else if (current
->alias
== NULL
)
493 /* If this import statement creates no alias, pass
494 current->inner as NAMESPACE to direct the search
495 towards the imported namespace. */
496 cp_lookup_symbol_via_imports (current
->import_src
, name
,
497 block
, domain
, 1, 0, 0,
505 /* Wrapper for the actual cp_lookup_symbol_via_imports. This wrapper sets
506 search_scope_first correctly and handles errors if needed. */
507 static struct block_symbol
508 cp_lookup_symbol_via_imports (const char *scope
,
510 const struct block
*block
,
511 const domain_search_flags domain
,
512 const int declaration_only
,
513 const int search_parents
)
515 std::map
<std::string
, struct block_symbol
> found_symbols
;
517 cp_lookup_symbol_via_imports(scope
, name
, block
, domain
, 0,
518 declaration_only
, search_parents
,
521 if (found_symbols
.size () > 1)
523 auto itr
= found_symbols
.cbegin ();
524 std::string error_str
= "Reference to \"";
526 error_str
+= "\" is ambiguous, possibilities are: ";
527 error_str
+= itr
->second
.symbol
->print_name ();
528 for (itr
++; itr
!= found_symbols
.end (); itr
++)
530 error_str
+= " and ";
531 error_str
+= itr
->second
.symbol
->print_name ();
533 error (_("%s"), error_str
.c_str ());
536 if (found_symbols
.size() == 1)
537 return found_symbols
.cbegin ()->second
;
542 /* Helper function that searches an array of symbols for one named NAME. */
544 static struct symbol
*
545 search_symbol_list (const char *name
, int num
,
546 struct symbol
**syms
)
550 /* Maybe we should store a dictionary in here instead. */
551 for (i
= 0; i
< num
; ++i
)
553 if (strcmp (name
, syms
[i
]->natural_name ()) == 0)
559 /* Search for symbols whose name match NAME in the given SCOPE.
560 if BLOCK is a function, we'll search first through the template
561 parameters and function type. Afterwards (or if BLOCK is not a function)
562 search through imported directives using cp_lookup_symbol_via_imports. */
565 cp_lookup_symbol_imports_or_template (const char *scope
,
567 const struct block
*block
,
568 const domain_search_flags domain
)
570 struct symbol
*function
= block
->function ();
572 symbol_lookup_debug_printf
573 ("cp_lookup_symbol_imports_or_template (%s, %s, %s, %s)",
574 scope
, name
, host_address_to_string (block
),
575 domain_name (domain
).c_str ());
577 if (function
!= NULL
&& function
->language () == language_cplus
)
579 /* Search the function's template parameters. */
580 if (function
->is_cplus_template_function ())
582 struct template_symbol
*templ
583 = (struct template_symbol
*) function
;
584 struct symbol
*sym
= search_symbol_list (name
,
585 templ
->n_template_arguments
,
586 templ
->template_arguments
);
590 symbol_lookup_debug_printf
591 ("cp_lookup_symbol_imports_or_template (...) = %s",
592 host_address_to_string (sym
));
593 return (struct block_symbol
) {sym
, block
};
597 /* Search the template parameters of the function's defining
599 if (function
->natural_name ())
601 struct type
*context
;
602 std::string
name_copy (function
->natural_name ());
603 const struct language_defn
*lang
= language_def (language_cplus
);
604 const struct block
*parent
= block
->superblock ();
609 unsigned int prefix_len
610 = cp_entire_prefix_len (name_copy
.c_str ());
616 name_copy
.erase (prefix_len
);
617 context
= lookup_typename (lang
,
626 = search_symbol_list (name
,
627 TYPE_N_TEMPLATE_ARGUMENTS (context
),
628 TYPE_TEMPLATE_ARGUMENTS (context
));
631 symbol_lookup_debug_printf
632 ("cp_lookup_symbol_imports_or_template (...) = %s",
633 host_address_to_string (sym
));
634 return (struct block_symbol
) {sym
, parent
};
640 struct block_symbol result
641 = cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 1, 1);
642 symbol_lookup_debug_printf ("cp_lookup_symbol_imports_or_template (...) = %s\n",
643 result
.symbol
!= nullptr
644 ? host_address_to_string (result
.symbol
) : "NULL");
648 /* Search for NAME by applying relevant import statements belonging to BLOCK
649 and its parents. SCOPE is the namespace scope of the context in which the
650 search is being evaluated. */
652 static struct block_symbol
653 cp_lookup_symbol_via_all_imports (const char *scope
, const char *name
,
654 const struct block
*block
,
655 const domain_search_flags domain
)
657 struct block_symbol sym
;
659 while (block
!= NULL
)
661 sym
= cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 0, 1);
662 if (sym
.symbol
!= nullptr)
665 block
= block
->superblock ();
671 /* Searches for NAME in the current namespace, and by applying
672 relevant import statements belonging to BLOCK and its parents.
673 SCOPE is the namespace scope of the context in which the search is
677 cp_lookup_symbol_namespace (const char *scope
,
679 const struct block
*block
,
680 const domain_search_flags domain
)
682 struct block_symbol sym
;
684 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (%s, %s, %s, %s)",
685 scope
, name
, host_address_to_string (block
),
686 domain_name (domain
).c_str ());
688 /* First, try to find the symbol in the given namespace. */
689 sym
= cp_lookup_symbol_in_namespace (scope
, name
, block
, domain
, 1);
691 /* Search for name in namespaces imported to this and parent blocks. */
692 if (sym
.symbol
== NULL
)
693 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
695 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (...) = %s",
697 ? host_address_to_string (sym
.symbol
) : "NULL");
701 /* Lookup NAME at namespace scope (or, in C terms, in static and
702 global variables). SCOPE is the namespace that the current
703 function is defined within; only consider namespaces whose length
704 is at least SCOPE_LEN. Other arguments are as in
705 cp_lookup_symbol_nonlocal.
707 For example, if we're within a function A::B::f and looking for a
708 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
709 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
710 but with SCOPE_LEN = 1. And then it calls itself with NAME and
711 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
712 "A::B::x"; if it doesn't find it, then the second call looks for
713 "A::x", and if that call fails, then the first call looks for
716 static struct block_symbol
717 lookup_namespace_scope (const struct language_defn
*langdef
,
719 const struct block
*block
,
720 const domain_search_flags domain
,
726 if (scope
[scope_len
] != '\0')
728 /* Recursively search for names in child namespaces first. */
730 struct block_symbol sym
;
731 int new_scope_len
= scope_len
;
733 /* If the current scope is followed by "::", skip past that. */
734 if (new_scope_len
!= 0)
736 gdb_assert (scope
[new_scope_len
] == ':');
739 new_scope_len
+= cp_find_first_component (scope
+ new_scope_len
);
740 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
,
741 scope
, new_scope_len
);
742 if (sym
.symbol
!= NULL
)
746 /* Okay, we didn't find a match in our children, so look for the
747 name in the current namespace.
749 If we there is no scope and we know we have a bare symbol, then short
750 circuit everything and call cp_lookup_bare_symbol directly.
751 This isn't an optimization, rather it allows us to pass LANGDEF which
752 is needed for primitive type lookup. The test doesn't have to be
753 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
754 template symbol with "::" in the argument list) then
755 cp_lookup_symbol_in_namespace will catch it. */
757 if (scope_len
== 0 && strchr (name
, ':') == NULL
)
758 return cp_lookup_bare_symbol (langdef
, name
, block
, domain
, 1);
760 the_namespace
= (char *) alloca (scope_len
+ 1);
761 strncpy (the_namespace
, scope
, scope_len
);
762 the_namespace
[scope_len
] = '\0';
763 return cp_lookup_symbol_in_namespace (the_namespace
, name
,
767 /* The C++-specific version of name lookup for static and global
768 names. This makes sure that names get looked for in all namespaces
769 that are in scope. NAME is the natural name of the symbol that
770 we're looking for, BLOCK is the block that we're searching within,
771 DOMAIN says what kind of symbols we're looking for. */
774 cp_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
776 const struct block
*block
,
777 const domain_search_flags domain
)
779 struct block_symbol sym
;
780 const char *scope
= block
== nullptr ? "" : block
->scope ();
782 symbol_lookup_debug_printf
783 ("cp_lookup_symbol_non_local (%s, %s (scope %s), %s)",
784 name
, host_address_to_string (block
), scope
,
785 domain_name (domain
).c_str ());
787 /* First, try to find the symbol in the given namespace, and all
788 containing namespaces. */
789 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
, scope
, 0);
791 /* Search for name in namespaces imported to this and parent blocks. */
792 if (sym
.symbol
== NULL
)
793 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
795 symbol_lookup_debug_printf ("cp_lookup_symbol_nonlocal (...) = %s",
797 ? host_address_to_string (sym
.symbol
)
802 /* Search through the base classes of PARENT_TYPE for a base class
803 named NAME and return its type. If not found, return NULL. */
806 cp_find_type_baseclass_by_name (struct type
*parent_type
, const char *name
)
810 parent_type
= check_typedef (parent_type
);
811 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
813 struct type
*type
= check_typedef (TYPE_BASECLASS (parent_type
, i
));
814 const char *tdef_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
815 const char *base_name
= type
->name ();
817 if (base_name
== NULL
)
820 if (streq (tdef_name
, name
) || streq (base_name
, name
))
823 type
= cp_find_type_baseclass_by_name (type
, name
);
831 /* Search through the base classes of PARENT_TYPE for a symbol named
832 NAME in block BLOCK. */
834 static struct block_symbol
835 find_symbol_in_baseclass (struct type
*parent_type
, const char *name
,
836 const struct block
*block
,
837 const domain_search_flags domain
,
841 struct block_symbol sym
= {};
843 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
845 struct type
*base_type
= TYPE_BASECLASS (parent_type
, i
);
846 const char *base_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
848 if (base_name
== NULL
)
851 std::string concatenated_name
= std::string (base_name
) + "::" + name
;
853 sym
= cp_lookup_nested_symbol_1 (base_type
, name
,
854 concatenated_name
.c_str (),
855 block
, domain
, 1, is_in_anonymous
);
856 if (sym
.symbol
!= NULL
)
863 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
864 and within the context of BLOCK.
865 NESTED_NAME may have scope ("::").
866 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
867 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
868 passed as an argument so that callers can control how space for it is
870 If BASIC_LOOKUP is non-zero then perform a basic lookup of
871 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
872 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
875 static struct block_symbol
876 cp_lookup_nested_symbol_1 (struct type
*container_type
,
877 const char *nested_name
,
878 const char *concatenated_name
,
879 const struct block
*block
,
880 const domain_search_flags domain
,
881 int basic_lookup
, int is_in_anonymous
)
883 struct block_symbol sym
;
885 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
886 of classes like, say, data or function members. Instead,
887 they're just represented by symbols whose names are
888 qualified by the name of the surrounding class. This is
889 just like members of namespaces; in particular,
890 cp_basic_lookup_symbol works when looking them up. */
894 sym
= cp_basic_lookup_symbol (concatenated_name
, block
, domain
,
896 if (sym
.symbol
!= NULL
)
900 /* Now search all static file-level symbols. We have to do this for things
901 like typedefs in the class. We do not try to guess any imported
902 namespace as even the fully specified namespace search is already not
903 C++ compliant and more assumptions could make it too magic. */
905 /* First search in this symtab, what we want is possibly there. */
906 sym
= lookup_symbol_in_static_block (concatenated_name
, block
, domain
);
907 if (sym
.symbol
!= NULL
)
910 /* Nope. We now have to search all static blocks in all objfiles,
911 even if block != NULL, because there's no guarantees as to which
912 symtab the symbol we want is in. Except for symbols defined in
913 anonymous namespaces should be treated as local to a single file,
914 which we just searched. */
915 if (!is_in_anonymous
)
917 sym
= lookup_static_symbol (concatenated_name
, domain
);
918 if (sym
.symbol
!= NULL
)
922 /* If this is a class with baseclasses, search them next. */
923 container_type
= check_typedef (container_type
);
924 if (TYPE_N_BASECLASSES (container_type
) > 0)
926 sym
= find_symbol_in_baseclass (container_type
, nested_name
, block
,
927 domain
, is_in_anonymous
);
928 if (sym
.symbol
!= NULL
)
935 /* Look up a symbol named NESTED_NAME that is nested inside the C++
936 class or namespace given by PARENT_TYPE, from within the context
937 given by BLOCK, and in DOMAIN.
938 Return NULL if there is no such nested symbol. */
941 cp_lookup_nested_symbol (struct type
*parent_type
,
942 const char *nested_name
,
943 const struct block
*block
,
944 const domain_search_flags domain
)
946 /* type_name_or_error provides better error reporting using the
948 struct type
*saved_parent_type
= parent_type
;
950 parent_type
= check_typedef (parent_type
);
952 if (symbol_lookup_debug
)
954 const char *type_name
= saved_parent_type
->name ();
956 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (%s, %s, %s, %s)",
957 type_name
!= NULL
? type_name
: "unnamed",
958 nested_name
, host_address_to_string (block
),
959 domain_name (domain
).c_str ());
962 switch (parent_type
->code ())
964 case TYPE_CODE_STRUCT
:
965 case TYPE_CODE_NAMESPACE
:
966 case TYPE_CODE_UNION
:
968 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
969 specific code to lookup nested symbols in modules, by calling the
970 method lookup_symbol_nonlocal, which ends up here. */
971 case TYPE_CODE_MODULE
:
974 const char *parent_name
= type_name_or_error (saved_parent_type
);
975 struct block_symbol sym
;
976 char *concatenated_name
;
979 size
= strlen (parent_name
) + 2 + strlen (nested_name
) + 1;
980 concatenated_name
= (char *) alloca (size
);
981 xsnprintf (concatenated_name
, size
, "%s::%s",
982 parent_name
, nested_name
);
983 is_in_anonymous
= cp_is_in_anonymous (concatenated_name
);
985 sym
= cp_lookup_nested_symbol_1 (parent_type
, nested_name
,
986 concatenated_name
, block
, domain
,
989 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (...) = %s",
991 ? host_address_to_string (sym
.symbol
)
997 case TYPE_CODE_METHOD
:
998 symbol_lookup_debug_printf
999 ("cp_lookup_nested_symbol (...) = NULL (func/method)");
1003 internal_error (_("cp_lookup_nested_symbol called "
1004 "on a non-aggregate type."));
1008 /* The C++-version of lookup_transparent_type. */
1010 /* FIXME: carlton/2004-01-16: The problem that this is trying to
1011 address is that, unfortunately, sometimes NAME is wrong: it may not
1012 include the name of namespaces enclosing the type in question.
1013 lookup_transparent_type gets called when the type in question
1014 is a declaration, and we're trying to find its definition; but, for
1015 declarations, our type name deduction mechanism doesn't work.
1016 There's nothing we can do to fix this in general, I think, in the
1017 absence of debug information about namespaces (I've filed PR
1018 gdb/1511 about this); until such debug information becomes more
1019 prevalent, one heuristic which sometimes looks is to search for the
1020 definition in namespaces containing the current namespace.
1022 We should delete this functions once the appropriate debug
1023 information becomes more widespread. (GCC 3.4 will be the first
1024 released version of GCC with such information.) */
1027 cp_lookup_transparent_type (const char *name
, domain_search_flags flags
)
1029 /* First, try the honest way of looking up the definition. */
1030 struct type
*t
= basic_lookup_transparent_type (name
, flags
);
1036 /* If that doesn't work and we're within a namespace, look there
1038 const block
*block
= get_selected_block (0);
1039 if (block
== nullptr)
1042 scope
= block
->scope ();
1044 if (scope
[0] == '\0')
1047 return cp_lookup_transparent_type_loop (name
, scope
, 0);
1050 /* Lookup the type definition associated to NAME in namespaces/classes
1051 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1052 must be the index of the start of a component of SCOPE. */
1054 static struct type
*
1055 cp_lookup_transparent_type_loop (const char *name
,
1059 int scope_length
= length
+ cp_find_first_component (scope
+ length
);
1062 /* If the current scope is followed by "::", look in the next
1064 if (scope
[scope_length
] == ':')
1067 = cp_lookup_transparent_type_loop (name
, scope
,
1074 full_name
= (char *) alloca (scope_length
+ 2 + strlen (name
) + 1);
1075 strncpy (full_name
, scope
, scope_length
);
1076 memcpy (full_name
+ scope_length
, "::", 2);
1077 strcpy (full_name
+ scope_length
+ 2, name
);
1079 return basic_lookup_transparent_type (full_name
);
1082 /* This used to do something but was removed when it became
1086 maintenance_cplus_namespace (const char *args
, int from_tty
)
1088 gdb_printf (_("The `maint namespace' command was removed.\n"));
1091 void _initialize_cp_namespace ();
1093 _initialize_cp_namespace ()
1095 struct cmd_list_element
*cmd
;
1097 cmd
= add_cmd ("namespace", class_maintenance
,
1098 maintenance_cplus_namespace
,
1099 _("Deprecated placeholder for removed functionality."),
1100 &maint_cplus_cmd_list
);
1101 deprecate_cmd (cmd
, NULL
);