1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003-2023 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 "gdbsupport/gdb_obstack.h"
29 #include "dictionary.h"
34 #include "namespace.h"
38 static struct block_symbol
39 cp_lookup_nested_symbol_1 (struct type
*container_type
,
40 const char *nested_name
,
41 const char *concatenated_name
,
42 const struct block
*block
,
43 const domain_enum domain
,
44 int basic_lookup
, int is_in_anonymous
);
46 static struct type
*cp_lookup_transparent_type_loop (const char *name
,
50 /* Check to see if SYMBOL refers to an object contained within an
51 anonymous namespace; if so, add an appropriate using directive. */
54 cp_scan_for_anonymous_namespaces (struct buildsym_compunit
*compunit
,
55 const struct symbol
*const symbol
,
56 struct objfile
*const objfile
)
58 if (symbol
->demangled_name () != NULL
)
60 const char *name
= symbol
->demangled_name ();
61 unsigned int previous_component
;
62 unsigned int next_component
;
64 /* Start with a quick-and-dirty check for mention of "(anonymous
67 if (!cp_is_in_anonymous (name
))
70 previous_component
= 0;
71 next_component
= cp_find_first_component (name
+ previous_component
);
73 while (name
[next_component
] == ':')
75 if (((next_component
- previous_component
)
76 == CP_ANONYMOUS_NAMESPACE_LEN
)
77 && strncmp (name
+ previous_component
,
78 CP_ANONYMOUS_NAMESPACE_STR
,
79 CP_ANONYMOUS_NAMESPACE_LEN
) == 0)
81 int dest_len
= (previous_component
== 0
82 ? 0 : previous_component
- 2);
83 int src_len
= next_component
;
85 char *dest
= (char *) alloca (dest_len
+ 1);
86 char *src
= (char *) alloca (src_len
+ 1);
88 memcpy (dest
, name
, dest_len
);
89 memcpy (src
, name
, src_len
);
91 dest
[dest_len
] = '\0';
94 /* We've found a component of the name that's an
95 anonymous namespace. So add symbols in it to the
96 namespace given by the previous component if there is
97 one, or to the global namespace if there isn't.
98 The declared line of this using directive can be set
99 to 0, this way it is always considered valid. */
100 std::vector
<const char *> excludes
;
101 add_using_directive (compunit
->get_local_using_directives (),
102 dest
, src
, NULL
, NULL
, excludes
, 0,
103 1, &objfile
->objfile_obstack
);
105 /* The "+ 2" is for the "::". */
106 previous_component
= next_component
+ 2;
107 next_component
= (previous_component
108 + cp_find_first_component (name
109 + previous_component
));
114 /* Test whether or not NAMESPACE looks like it mentions an anonymous
115 namespace; return nonzero if so. */
118 cp_is_in_anonymous (const char *symbol_name
)
120 return (strstr (symbol_name
, CP_ANONYMOUS_NAMESPACE_STR
)
124 /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
125 If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
126 within an anonymous namespace. */
128 static struct block_symbol
129 cp_basic_lookup_symbol (const char *name
, const struct block
*block
,
130 const domain_enum domain
, int is_in_anonymous
)
132 struct block_symbol sym
;
134 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
135 if (sym
.symbol
!= NULL
)
140 /* Symbols defined in anonymous namespaces have external linkage
141 but should be treated as local to a single file nonetheless.
142 So we only search the current file's global block. */
144 const struct block
*global_block
= block_global_block (block
);
146 if (global_block
!= NULL
)
148 sym
.symbol
= lookup_symbol_in_block (name
,
149 symbol_name_match_type::FULL
,
150 global_block
, domain
);
151 sym
.block
= global_block
;
155 sym
= lookup_global_symbol (name
, block
, domain
);
160 /* Search bare symbol NAME in DOMAIN in BLOCK.
161 NAME is guaranteed to not have any scope (no "::") in its name, though
162 if for example NAME is a template spec then "::" may appear in the
164 If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
165 that language. Normally we wouldn't need LANGDEF but fortran also uses
167 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
168 if so then also search for NAME in that class. */
170 static struct block_symbol
171 cp_lookup_bare_symbol (const struct language_defn
*langdef
,
172 const char *name
, const struct block
*block
,
173 const domain_enum domain
, int search
)
175 struct block_symbol sym
;
177 /* Note: We can't do a simple assert for ':' not being in NAME because
178 ':' may be in the args of a template spec. This isn't intended to be
179 a complete test, just cheap and documentary. */
180 if (strchr (name
, '<') == NULL
&& strchr (name
, '(') == NULL
)
181 gdb_assert (strstr (name
, "::") == NULL
);
183 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
184 if (sym
.symbol
!= NULL
)
187 /* If we didn't find a definition for a builtin type in the static block,
188 search for it now. This is actually the right thing to do and can be
189 a massive performance win. E.g., when debugging a program with lots of
190 shared libraries we could search all of them only to find out the
191 builtin type isn't defined in any of them. This is common for types
193 if (langdef
!= NULL
&& domain
== VAR_DOMAIN
)
195 struct gdbarch
*gdbarch
;
198 gdbarch
= target_gdbarch ();
200 gdbarch
= block
->gdbarch ();
202 = language_lookup_primitive_type_as_symbol (langdef
, gdbarch
, name
);
204 if (sym
.symbol
!= NULL
)
208 sym
= lookup_global_symbol (name
, block
, domain
);
209 if (sym
.symbol
!= NULL
)
214 struct block_symbol lang_this
;
217 lang_this
.symbol
= NULL
;
220 lang_this
= lookup_language_this (langdef
, block
);
222 if (lang_this
.symbol
== NULL
)
226 type
= check_typedef (lang_this
.symbol
->type ()->target_type ());
227 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
228 This can happen for lambda functions compiled with clang++,
229 which outputs no name for the container class. */
230 if (type
->name () == NULL
)
233 /* Look for symbol NAME in this class. */
234 sym
= cp_lookup_nested_symbol (type
, name
, block
, domain
);
240 /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
241 BLOCK specifies the context in which to perform the search.
242 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
243 the length of the entire scope of NAME (up to, but not including, the last
246 Note: At least in the case of Fortran, which also uses this code, there
247 may be no text after the last "::". */
249 static struct block_symbol
250 cp_search_static_and_baseclasses (const char *name
,
251 const struct block
*block
,
252 const domain_enum domain
,
253 unsigned int prefix_len
,
256 /* Check for malformed input. */
257 if (prefix_len
+ 2 > strlen (name
) || name
[prefix_len
+ 1] != ':')
260 /* The class, namespace or function name is everything up to and
261 including PREFIX_LEN. */
262 std::string
scope (name
, prefix_len
);
264 /* The rest of the name is everything else past the initial scope
266 const char *nested
= name
+ prefix_len
+ 2;
268 /* Lookup the scope symbol. If none is found, there is nothing more
269 that can be done. SCOPE could be a namespace, so always look in
270 VAR_DOMAIN. This works for classes too because of
271 symbol_matches_domain (which should be replaced with something
272 else, but it's what we have today). */
273 block_symbol scope_sym
= lookup_symbol_in_static_block (scope
.c_str (),
275 if (scope_sym
.symbol
== NULL
)
276 scope_sym
= lookup_global_symbol (scope
.c_str (), block
, VAR_DOMAIN
);
277 if (scope_sym
.symbol
== NULL
)
280 struct type
*scope_type
= scope_sym
.symbol
->type ();
282 /* If the scope is a function/method, then look up NESTED as a local
283 static variable. E.g., "print 'function()::static_var'". */
284 if ((scope_type
->code () == TYPE_CODE_FUNC
285 || scope_type
->code () == TYPE_CODE_METHOD
)
286 && domain
== VAR_DOMAIN
)
287 return lookup_symbol (nested
, scope_sym
.symbol
->value_block (),
290 /* Look for a symbol named NESTED in this class/namespace.
291 The caller is assumed to have already have done a basic lookup of NAME.
292 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
293 return cp_lookup_nested_symbol_1 (scope_type
, nested
, name
,
294 block
, domain
, 0, is_in_anonymous
);
297 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
298 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
299 through base classes for a matching symbol.
301 Note: Part of the complexity is because NAME may itself specify scope.
302 Part of the complexity is also because this handles the case where
303 there is no scoping in which case we also try looking in the class of
304 "this" if we can compute it. */
306 static struct block_symbol
307 cp_lookup_symbol_in_namespace (const char *the_namespace
, const char *name
,
308 const struct block
*block
,
309 const domain_enum domain
, int search
)
311 char *concatenated_name
= NULL
;
313 unsigned int prefix_len
;
314 struct block_symbol sym
;
316 if (the_namespace
[0] != '\0')
319 = (char *) alloca (strlen (the_namespace
) + 2 + strlen (name
) + 1);
320 strcpy (concatenated_name
, the_namespace
);
321 strcat (concatenated_name
, "::");
322 strcat (concatenated_name
, name
);
323 name
= concatenated_name
;
326 prefix_len
= cp_entire_prefix_len (name
);
328 return cp_lookup_bare_symbol (NULL
, name
, block
, domain
, search
);
330 /* This would be simpler if we just called cp_lookup_nested_symbol
331 at this point. But that would require first looking up the containing
332 class/namespace. Since we're only searching static and global blocks
333 there's often no need to first do that lookup. */
336 = the_namespace
[0] != '\0' && cp_is_in_anonymous (the_namespace
);
337 sym
= cp_basic_lookup_symbol (name
, block
, domain
, is_in_anonymous
);
338 if (sym
.symbol
!= NULL
)
342 sym
= cp_search_static_and_baseclasses (name
, block
, domain
, prefix_len
,
348 /* This version of the function is internal, use the wrapper unless
349 the list of ambiguous symbols is needed.
351 Search for NAME by applying all import statements belonging to
352 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
353 search is restricted to using declarations.
361 If SEARCH_PARENTS the search will include imports which are
362 applicable in parents of SCOPE.
372 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
373 namespaces X and Y will be considered. If SEARCH_PARENTS is false
374 only the import of Y is considered.
376 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
377 pass 0 for it. Internally we pass 1 when recursing. */
380 cp_lookup_symbol_via_imports (const char *scope
,
382 const struct block
*block
,
383 const domain_enum domain
,
384 const int search_scope_first
,
385 const int declaration_only
,
386 const int search_parents
,
387 std::map
<std::string
,
388 struct block_symbol
>& found_symbols
)
390 struct using_direct
*current
;
391 struct block_symbol sym
= {};
395 /* All the symbols we found will be kept in this relational map between
396 the mangled name and the block_symbol found. We do this so that GDB
397 won't incorrectly report an ambiguous symbol for finding the same
400 /* First, try to find the symbol in the given namespace if requested. */
401 if (search_scope_first
)
403 sym
= cp_lookup_symbol_in_namespace (scope
, name
,
405 if (sym
.symbol
!= nullptr)
406 found_symbols
[sym
.symbol
->m_name
] = sym
;
409 /* Due to a GCC bug, we need to know the boundaries of the current block
410 to know if a certain using directive is valid. */
411 symtab_and_line boundary_sal
= find_pc_line (block
->end () - 1, 0);
413 /* Go through the using directives. If any of them add new names to
414 the namespace we're searching in, see if we can find a match by
416 for (current
= block
->get_using ();
418 current
= current
->next
)
420 const char **excludep
;
422 /* If the using directive was below the place we are stopped at,
423 do not use this directive. */
424 if (!current
->valid_line (boundary_sal
.line
))
426 len
= strlen (current
->import_dest
);
427 directive_match
= (search_parents
428 ? (startswith (scope
, current
->import_dest
)
431 || scope
[len
] == '\0'))
432 : strcmp (scope
, current
->import_dest
) == 0);
434 /* If the import destination is the current scope or one of its
435 ancestors then it is applicable. */
436 if (directive_match
&& !current
->searched
)
438 /* Mark this import as searched so that the recursive call
439 does not search it again. */
440 scoped_restore reset_directive_searched
441 = make_scoped_restore (¤t
->searched
, 1);
443 /* If there is an import of a single declaration, compare the
444 imported declaration (after optional renaming by its alias)
445 with the sought out name. If there is a match pass
446 current->import_src as NAMESPACE to direct the search
447 towards the imported namespace. */
448 if (current
->declaration
449 && strcmp (name
, current
->alias
450 ? current
->alias
: current
->declaration
) == 0)
451 sym
= cp_lookup_symbol_in_namespace (current
->import_src
,
452 current
->declaration
,
455 /* If this is a DECLARATION_ONLY search or a symbol was found
456 or this import statement was an import declaration, the
457 search of this import is complete. */
458 if (declaration_only
|| sym
.symbol
!= NULL
|| current
->declaration
)
460 if (sym
.symbol
!= NULL
)
461 found_symbols
[sym
.symbol
->m_name
] = sym
;
466 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
467 for (excludep
= current
->excludes
; *excludep
; excludep
++)
468 if (strcmp (name
, *excludep
) == 0)
473 if (current
->alias
!= NULL
474 && strcmp (name
, current
->alias
) == 0)
475 /* If the import is creating an alias and the alias matches
476 the sought name. Pass current->import_src as the NAME to
477 direct the search towards the aliased namespace. */
479 sym
= cp_lookup_symbol_in_namespace (scope
,
482 found_symbols
[sym
.symbol
->m_name
] = sym
;
484 else if (current
->alias
== NULL
)
486 /* If this import statement creates no alias, pass
487 current->inner as NAMESPACE to direct the search
488 towards the imported namespace. */
489 cp_lookup_symbol_via_imports (current
->import_src
, name
,
490 block
, domain
, 1, 0, 0,
498 /* Wrapper for the actual cp_lookup_symbol_via_imports. This wrapper sets
499 search_scope_first correctly and handles errors if needed. */
500 static struct block_symbol
501 cp_lookup_symbol_via_imports (const char *scope
,
503 const struct block
*block
,
504 const domain_enum domain
,
505 const int declaration_only
,
506 const int search_parents
)
508 std::map
<std::string
, struct block_symbol
> found_symbols
;
510 cp_lookup_symbol_via_imports(scope
, name
, block
, domain
, 0,
511 declaration_only
, search_parents
,
514 if (found_symbols
.size () > 1)
516 auto itr
= found_symbols
.cbegin ();
517 std::string error_str
= "Reference to \"";
519 error_str
+= "\" is ambiguous, possibilities are: ";
520 error_str
+= itr
->second
.symbol
->print_name ();
521 for (itr
++; itr
!= found_symbols
.end (); itr
++)
523 error_str
+= " and ";
524 error_str
+= itr
->second
.symbol
->print_name ();
526 error (_("%s"), error_str
.c_str ());
529 if (found_symbols
.size() == 1)
530 return found_symbols
.cbegin ()->second
;
535 /* Helper function that searches an array of symbols for one named NAME. */
537 static struct symbol
*
538 search_symbol_list (const char *name
, int num
,
539 struct symbol
**syms
)
543 /* Maybe we should store a dictionary in here instead. */
544 for (i
= 0; i
< num
; ++i
)
546 if (strcmp (name
, syms
[i
]->natural_name ()) == 0)
552 /* Search for symbols whose name match NAME in the given SCOPE.
553 if BLOCK is a function, we'll search first through the template
554 parameters and function type. Afterwards (or if BLOCK is not a function)
555 search through imported directives using cp_lookup_symbol_via_imports. */
558 cp_lookup_symbol_imports_or_template (const char *scope
,
560 const struct block
*block
,
561 const domain_enum domain
)
563 struct symbol
*function
= block
->function ();
565 symbol_lookup_debug_printf
566 ("cp_lookup_symbol_imports_or_template (%s, %s, %s, %s)",
567 scope
, name
, host_address_to_string (block
), domain_name (domain
));
569 if (function
!= NULL
&& function
->language () == language_cplus
)
571 /* Search the function's template parameters. */
572 if (function
->is_cplus_template_function ())
574 struct template_symbol
*templ
575 = (struct template_symbol
*) function
;
576 struct symbol
*sym
= search_symbol_list (name
,
577 templ
->n_template_arguments
,
578 templ
->template_arguments
);
582 symbol_lookup_debug_printf
583 ("cp_lookup_symbol_imports_or_template (...) = %s",
584 host_address_to_string (sym
));
585 return (struct block_symbol
) {sym
, block
};
589 /* Search the template parameters of the function's defining
591 if (function
->natural_name ())
593 struct type
*context
;
594 std::string
name_copy (function
->natural_name ());
595 const struct language_defn
*lang
= language_def (language_cplus
);
596 const struct block
*parent
= block
->superblock ();
601 unsigned int prefix_len
602 = cp_entire_prefix_len (name_copy
.c_str ());
608 name_copy
.erase (prefix_len
);
609 context
= lookup_typename (lang
,
618 = search_symbol_list (name
,
619 TYPE_N_TEMPLATE_ARGUMENTS (context
),
620 TYPE_TEMPLATE_ARGUMENTS (context
));
623 symbol_lookup_debug_printf
624 ("cp_lookup_symbol_imports_or_template (...) = %s",
625 host_address_to_string (sym
));
626 return (struct block_symbol
) {sym
, parent
};
632 struct block_symbol result
633 = cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 1, 1);
634 symbol_lookup_debug_printf ("cp_lookup_symbol_imports_or_template (...) = %s\n",
635 result
.symbol
!= nullptr
636 ? host_address_to_string (result
.symbol
) : "NULL");
640 /* Search for NAME by applying relevant import statements belonging to BLOCK
641 and its parents. SCOPE is the namespace scope of the context in which the
642 search is being evaluated. */
644 static struct block_symbol
645 cp_lookup_symbol_via_all_imports (const char *scope
, const char *name
,
646 const struct block
*block
,
647 const domain_enum domain
)
649 struct block_symbol sym
;
651 while (block
!= NULL
)
653 sym
= cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 0, 1);
654 if (sym
.symbol
!= nullptr)
657 block
= block
->superblock ();
663 /* Searches for NAME in the current namespace, and by applying
664 relevant import statements belonging to BLOCK and its parents.
665 SCOPE is the namespace scope of the context in which the search is
669 cp_lookup_symbol_namespace (const char *scope
,
671 const struct block
*block
,
672 const domain_enum domain
)
674 struct block_symbol sym
;
676 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (%s, %s, %s, %s)",
677 scope
, name
, host_address_to_string (block
),
678 domain_name (domain
));
680 /* First, try to find the symbol in the given namespace. */
681 sym
= cp_lookup_symbol_in_namespace (scope
, name
, block
, domain
, 1);
683 /* Search for name in namespaces imported to this and parent blocks. */
684 if (sym
.symbol
== NULL
)
685 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
687 symbol_lookup_debug_printf ("cp_lookup_symbol_namespace (...) = %s",
689 ? host_address_to_string (sym
.symbol
) : "NULL");
693 /* Lookup NAME at namespace scope (or, in C terms, in static and
694 global variables). SCOPE is the namespace that the current
695 function is defined within; only consider namespaces whose length
696 is at least SCOPE_LEN. Other arguments are as in
697 cp_lookup_symbol_nonlocal.
699 For example, if we're within a function A::B::f and looking for a
700 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
701 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
702 but with SCOPE_LEN = 1. And then it calls itself with NAME and
703 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
704 "A::B::x"; if it doesn't find it, then the second call looks for
705 "A::x", and if that call fails, then the first call looks for
708 static struct block_symbol
709 lookup_namespace_scope (const struct language_defn
*langdef
,
711 const struct block
*block
,
712 const domain_enum domain
,
718 if (scope
[scope_len
] != '\0')
720 /* Recursively search for names in child namespaces first. */
722 struct block_symbol sym
;
723 int new_scope_len
= scope_len
;
725 /* If the current scope is followed by "::", skip past that. */
726 if (new_scope_len
!= 0)
728 gdb_assert (scope
[new_scope_len
] == ':');
731 new_scope_len
+= cp_find_first_component (scope
+ new_scope_len
);
732 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
,
733 scope
, new_scope_len
);
734 if (sym
.symbol
!= NULL
)
738 /* Okay, we didn't find a match in our children, so look for the
739 name in the current namespace.
741 If we there is no scope and we know we have a bare symbol, then short
742 circuit everything and call cp_lookup_bare_symbol directly.
743 This isn't an optimization, rather it allows us to pass LANGDEF which
744 is needed for primitive type lookup. The test doesn't have to be
745 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
746 template symbol with "::" in the argument list) then
747 cp_lookup_symbol_in_namespace will catch it. */
749 if (scope_len
== 0 && strchr (name
, ':') == NULL
)
750 return cp_lookup_bare_symbol (langdef
, name
, block
, domain
, 1);
752 the_namespace
= (char *) alloca (scope_len
+ 1);
753 strncpy (the_namespace
, scope
, scope_len
);
754 the_namespace
[scope_len
] = '\0';
755 return cp_lookup_symbol_in_namespace (the_namespace
, name
,
759 /* The C++-specific version of name lookup for static and global
760 names. This makes sure that names get looked for in all namespaces
761 that are in scope. NAME is the natural name of the symbol that
762 we're looking for, BLOCK is the block that we're searching within,
763 DOMAIN says what kind of symbols we're looking for. */
766 cp_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
768 const struct block
*block
,
769 const domain_enum domain
)
771 struct block_symbol sym
;
772 const char *scope
= block
== nullptr ? "" : block
->scope ();
774 symbol_lookup_debug_printf
775 ("cp_lookup_symbol_non_local (%s, %s (scope %s), %s)",
776 name
, host_address_to_string (block
), scope
, domain_name (domain
));
778 /* First, try to find the symbol in the given namespace, and all
779 containing namespaces. */
780 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
, scope
, 0);
782 /* Search for name in namespaces imported to this and parent blocks. */
783 if (sym
.symbol
== NULL
)
784 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
786 symbol_lookup_debug_printf ("cp_lookup_symbol_nonlocal (...) = %s",
788 ? host_address_to_string (sym
.symbol
)
793 /* Search through the base classes of PARENT_TYPE for a base class
794 named NAME and return its type. If not found, return NULL. */
797 cp_find_type_baseclass_by_name (struct type
*parent_type
, const char *name
)
801 parent_type
= check_typedef (parent_type
);
802 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
804 struct type
*type
= check_typedef (TYPE_BASECLASS (parent_type
, i
));
805 const char *tdef_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
806 const char *base_name
= type
->name ();
808 if (base_name
== NULL
)
811 if (streq (tdef_name
, name
) || streq (base_name
, name
))
814 type
= cp_find_type_baseclass_by_name (type
, name
);
822 /* Search through the base classes of PARENT_TYPE for a symbol named
823 NAME in block BLOCK. */
825 static struct block_symbol
826 find_symbol_in_baseclass (struct type
*parent_type
, const char *name
,
827 const struct block
*block
, const domain_enum domain
,
831 struct block_symbol sym
= {};
833 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
835 struct type
*base_type
= TYPE_BASECLASS (parent_type
, i
);
836 const char *base_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
838 if (base_name
== NULL
)
841 std::string concatenated_name
= std::string (base_name
) + "::" + name
;
843 sym
= cp_lookup_nested_symbol_1 (base_type
, name
,
844 concatenated_name
.c_str (),
845 block
, domain
, 1, is_in_anonymous
);
846 if (sym
.symbol
!= NULL
)
853 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
854 and within the context of BLOCK.
855 NESTED_NAME may have scope ("::").
856 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
857 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
858 passed as an argument so that callers can control how space for it is
860 If BASIC_LOOKUP is non-zero then perform a basic lookup of
861 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
862 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
865 static struct block_symbol
866 cp_lookup_nested_symbol_1 (struct type
*container_type
,
867 const char *nested_name
,
868 const char *concatenated_name
,
869 const struct block
*block
,
870 const domain_enum domain
,
871 int basic_lookup
, int is_in_anonymous
)
873 struct block_symbol sym
;
875 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
876 of classes like, say, data or function members. Instead,
877 they're just represented by symbols whose names are
878 qualified by the name of the surrounding class. This is
879 just like members of namespaces; in particular,
880 cp_basic_lookup_symbol works when looking them up. */
884 sym
= cp_basic_lookup_symbol (concatenated_name
, block
, domain
,
886 if (sym
.symbol
!= NULL
)
890 /* Now search all static file-level symbols. We have to do this for things
891 like typedefs in the class. We do not try to guess any imported
892 namespace as even the fully specified namespace search is already not
893 C++ compliant and more assumptions could make it too magic. */
895 /* First search in this symtab, what we want is possibly there. */
896 sym
= lookup_symbol_in_static_block (concatenated_name
, block
, domain
);
897 if (sym
.symbol
!= NULL
)
900 /* Nope. We now have to search all static blocks in all objfiles,
901 even if block != NULL, because there's no guarantees as to which
902 symtab the symbol we want is in. Except for symbols defined in
903 anonymous namespaces should be treated as local to a single file,
904 which we just searched. */
905 if (!is_in_anonymous
)
907 sym
= lookup_static_symbol (concatenated_name
, domain
);
908 if (sym
.symbol
!= NULL
)
912 /* If this is a class with baseclasses, search them next. */
913 container_type
= check_typedef (container_type
);
914 if (TYPE_N_BASECLASSES (container_type
) > 0)
916 sym
= find_symbol_in_baseclass (container_type
, nested_name
, block
,
917 domain
, is_in_anonymous
);
918 if (sym
.symbol
!= NULL
)
925 /* Look up a symbol named NESTED_NAME that is nested inside the C++
926 class or namespace given by PARENT_TYPE, from within the context
927 given by BLOCK, and in DOMAIN.
928 Return NULL if there is no such nested symbol. */
931 cp_lookup_nested_symbol (struct type
*parent_type
,
932 const char *nested_name
,
933 const struct block
*block
,
934 const domain_enum domain
)
936 /* type_name_or_error provides better error reporting using the
938 struct type
*saved_parent_type
= parent_type
;
940 parent_type
= check_typedef (parent_type
);
942 if (symbol_lookup_debug
)
944 const char *type_name
= saved_parent_type
->name ();
946 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (%s, %s, %s, %s)",
947 type_name
!= NULL
? type_name
: "unnamed",
948 nested_name
, host_address_to_string (block
),
949 domain_name (domain
));
952 switch (parent_type
->code ())
954 case TYPE_CODE_STRUCT
:
955 case TYPE_CODE_NAMESPACE
:
956 case TYPE_CODE_UNION
:
958 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
959 specific code to lookup nested symbols in modules, by calling the
960 method lookup_symbol_nonlocal, which ends up here. */
961 case TYPE_CODE_MODULE
:
964 const char *parent_name
= type_name_or_error (saved_parent_type
);
965 struct block_symbol sym
;
966 char *concatenated_name
;
969 size
= strlen (parent_name
) + 2 + strlen (nested_name
) + 1;
970 concatenated_name
= (char *) alloca (size
);
971 xsnprintf (concatenated_name
, size
, "%s::%s",
972 parent_name
, nested_name
);
973 is_in_anonymous
= cp_is_in_anonymous (concatenated_name
);
975 sym
= cp_lookup_nested_symbol_1 (parent_type
, nested_name
,
976 concatenated_name
, block
, domain
,
979 symbol_lookup_debug_printf ("cp_lookup_nested_symbol (...) = %s",
981 ? host_address_to_string (sym
.symbol
)
987 case TYPE_CODE_METHOD
:
988 symbol_lookup_debug_printf
989 ("cp_lookup_nested_symbol (...) = NULL (func/method)");
993 internal_error (_("cp_lookup_nested_symbol called "
994 "on a non-aggregate type."));
998 /* The C++-version of lookup_transparent_type. */
1000 /* FIXME: carlton/2004-01-16: The problem that this is trying to
1001 address is that, unfortunately, sometimes NAME is wrong: it may not
1002 include the name of namespaces enclosing the type in question.
1003 lookup_transparent_type gets called when the type in question
1004 is a declaration, and we're trying to find its definition; but, for
1005 declarations, our type name deduction mechanism doesn't work.
1006 There's nothing we can do to fix this in general, I think, in the
1007 absence of debug information about namespaces (I've filed PR
1008 gdb/1511 about this); until such debug information becomes more
1009 prevalent, one heuristic which sometimes looks is to search for the
1010 definition in namespaces containing the current namespace.
1012 We should delete this functions once the appropriate debug
1013 information becomes more widespread. (GCC 3.4 will be the first
1014 released version of GCC with such information.) */
1017 cp_lookup_transparent_type (const char *name
)
1019 /* First, try the honest way of looking up the definition. */
1020 struct type
*t
= basic_lookup_transparent_type (name
);
1026 /* If that doesn't work and we're within a namespace, look there
1028 scope
= get_selected_block (0)->scope ();
1030 if (scope
[0] == '\0')
1033 return cp_lookup_transparent_type_loop (name
, scope
, 0);
1036 /* Lookup the type definition associated to NAME in namespaces/classes
1037 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1038 must be the index of the start of a component of SCOPE. */
1040 static struct type
*
1041 cp_lookup_transparent_type_loop (const char *name
,
1045 int scope_length
= length
+ cp_find_first_component (scope
+ length
);
1048 /* If the current scope is followed by "::", look in the next
1050 if (scope
[scope_length
] == ':')
1053 = cp_lookup_transparent_type_loop (name
, scope
,
1060 full_name
= (char *) alloca (scope_length
+ 2 + strlen (name
) + 1);
1061 strncpy (full_name
, scope
, scope_length
);
1062 memcpy (full_name
+ scope_length
, "::", 2);
1063 strcpy (full_name
+ scope_length
+ 2, name
);
1065 return basic_lookup_transparent_type (full_name
);
1068 /* This used to do something but was removed when it became
1072 maintenance_cplus_namespace (const char *args
, int from_tty
)
1074 gdb_printf (_("The `maint namespace' command was removed.\n"));
1077 void _initialize_cp_namespace ();
1079 _initialize_cp_namespace ()
1081 struct cmd_list_element
*cmd
;
1083 cmd
= add_cmd ("namespace", class_maintenance
,
1084 maintenance_cplus_namespace
,
1085 _("Deprecated placeholder for removed functionality."),
1086 &maint_cplus_cmd_list
);
1087 deprecate_cmd (cmd
, NULL
);