1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003-2019 Free Software Foundation, Inc.
4 Contributed by David Carlton and by Kealia, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "cp-support.h"
23 #include "gdb_obstack.h"
29 #include "dictionary.h"
34 #include "namespace.h"
37 static struct block_symbol
38 cp_lookup_nested_symbol_1 (struct type
*container_type
,
39 const char *nested_name
,
40 const char *concatenated_name
,
41 const struct block
*block
,
42 const domain_enum domain
,
43 int basic_lookup
, int is_in_anonymous
);
45 static struct type
*cp_lookup_transparent_type_loop (const char *name
,
49 /* Check to see if SYMBOL refers to an object contained within an
50 anonymous namespace; if so, add an appropriate using directive. */
53 cp_scan_for_anonymous_namespaces (struct buildsym_compunit
*compunit
,
54 const struct symbol
*const symbol
,
55 struct objfile
*const objfile
)
57 if (symbol
->demangled_name () != NULL
)
59 const char *name
= symbol
->demangled_name ();
60 unsigned int previous_component
;
61 unsigned int next_component
;
63 /* Start with a quick-and-dirty check for mention of "(anonymous
66 if (!cp_is_in_anonymous (name
))
69 previous_component
= 0;
70 next_component
= cp_find_first_component (name
+ previous_component
);
72 while (name
[next_component
] == ':')
74 if (((next_component
- previous_component
)
75 == CP_ANONYMOUS_NAMESPACE_LEN
)
76 && strncmp (name
+ previous_component
,
77 CP_ANONYMOUS_NAMESPACE_STR
,
78 CP_ANONYMOUS_NAMESPACE_LEN
) == 0)
80 int dest_len
= (previous_component
== 0
81 ? 0 : previous_component
- 2);
82 int src_len
= next_component
;
84 char *dest
= (char *) alloca (dest_len
+ 1);
85 char *src
= (char *) alloca (src_len
+ 1);
87 memcpy (dest
, name
, dest_len
);
88 memcpy (src
, name
, src_len
);
90 dest
[dest_len
] = '\0';
93 /* We've found a component of the name that's an
94 anonymous namespace. So add symbols in it to the
95 namespace given by the previous component if there is
96 one, or to the global namespace if there isn't. */
97 std::vector
<const char *> excludes
;
98 add_using_directive (compunit
->get_local_using_directives (),
99 dest
, src
, NULL
, NULL
, excludes
,
100 1, &objfile
->objfile_obstack
);
102 /* The "+ 2" is for the "::". */
103 previous_component
= next_component
+ 2;
104 next_component
= (previous_component
105 + cp_find_first_component (name
106 + previous_component
));
111 /* Test whether or not NAMESPACE looks like it mentions an anonymous
112 namespace; return nonzero if so. */
115 cp_is_in_anonymous (const char *symbol_name
)
117 return (strstr (symbol_name
, CP_ANONYMOUS_NAMESPACE_STR
)
121 /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
122 If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
123 within an anonymous namespace. */
125 static struct block_symbol
126 cp_basic_lookup_symbol (const char *name
, const struct block
*block
,
127 const domain_enum domain
, int is_in_anonymous
)
129 struct block_symbol sym
;
131 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
132 if (sym
.symbol
!= NULL
)
137 /* Symbols defined in anonymous namespaces have external linkage
138 but should be treated as local to a single file nonetheless.
139 So we only search the current file's global block. */
141 const struct block
*global_block
= block_global_block (block
);
143 if (global_block
!= NULL
)
145 sym
.symbol
= lookup_symbol_in_block (name
,
146 symbol_name_match_type::FULL
,
147 global_block
, domain
);
148 sym
.block
= global_block
;
152 sym
= lookup_global_symbol (name
, block
, domain
);
157 /* Search bare symbol NAME in DOMAIN in BLOCK.
158 NAME is guaranteed to not have any scope (no "::") in its name, though
159 if for example NAME is a template spec then "::" may appear in the
161 If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
162 that language. Normally we wouldn't need LANGDEF but fortran also uses
164 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
165 if so then also search for NAME in that class. */
167 static struct block_symbol
168 cp_lookup_bare_symbol (const struct language_defn
*langdef
,
169 const char *name
, const struct block
*block
,
170 const domain_enum domain
, int search
)
172 struct block_symbol sym
;
174 /* Note: We can't do a simple assert for ':' not being in NAME because
175 ':' may be in the args of a template spec. This isn't intended to be
176 a complete test, just cheap and documentary. */
177 if (strchr (name
, '<') == NULL
&& strchr (name
, '(') == NULL
)
178 gdb_assert (strstr (name
, "::") == NULL
);
180 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
181 if (sym
.symbol
!= NULL
)
184 /* If we didn't find a definition for a builtin type in the static block,
185 search for it now. This is actually the right thing to do and can be
186 a massive performance win. E.g., when debugging a program with lots of
187 shared libraries we could search all of them only to find out the
188 builtin type isn't defined in any of them. This is common for types
190 if (langdef
!= NULL
&& domain
== VAR_DOMAIN
)
192 struct gdbarch
*gdbarch
;
195 gdbarch
= target_gdbarch ();
197 gdbarch
= block_gdbarch (block
);
199 = language_lookup_primitive_type_as_symbol (langdef
, gdbarch
, name
);
201 if (sym
.symbol
!= NULL
)
205 sym
= lookup_global_symbol (name
, block
, domain
);
206 if (sym
.symbol
!= NULL
)
211 struct block_symbol lang_this
;
214 lang_this
.symbol
= NULL
;
217 lang_this
= lookup_language_this (langdef
, block
);
219 if (lang_this
.symbol
== NULL
)
223 type
= check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this
.symbol
)));
224 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
225 This can happen for lambda functions compiled with clang++,
226 which outputs no name for the container class. */
227 if (TYPE_NAME (type
) == NULL
)
230 /* Look for symbol NAME in this class. */
231 sym
= cp_lookup_nested_symbol (type
, name
, block
, domain
);
237 /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
238 BLOCK specifies the context in which to perform the search.
239 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
240 the length of the entire scope of NAME (up to, but not including, the last
243 Note: At least in the case of Fortran, which also uses this code, there
244 may be no text after the last "::". */
246 static struct block_symbol
247 cp_search_static_and_baseclasses (const char *name
,
248 const struct block
*block
,
249 const domain_enum domain
,
250 unsigned int prefix_len
,
253 /* Check for malformed input. */
254 if (prefix_len
+ 2 > strlen (name
) || name
[prefix_len
+ 1] != ':')
257 /* The class, namespace or function name is everything up to and
258 including PREFIX_LEN. */
259 std::string
scope (name
, prefix_len
);
261 /* The rest of the name is everything else past the initial scope
263 const char *nested
= name
+ prefix_len
+ 2;
265 /* Lookup the scope symbol. If none is found, there is nothing more
266 that can be done. SCOPE could be a namespace, so always look in
267 VAR_DOMAIN. This works for classes too because of
268 symbol_matches_domain (which should be replaced with something
269 else, but it's what we have today). */
270 block_symbol scope_sym
= lookup_symbol_in_static_block (scope
.c_str (),
272 if (scope_sym
.symbol
== NULL
)
273 scope_sym
= lookup_global_symbol (scope
.c_str (), block
, VAR_DOMAIN
);
274 if (scope_sym
.symbol
== NULL
)
277 struct type
*scope_type
= SYMBOL_TYPE (scope_sym
.symbol
);
279 /* If the scope is a function/method, then look up NESTED as a local
280 static variable. E.g., "print 'function()::static_var'". */
281 if ((TYPE_CODE (scope_type
) == TYPE_CODE_FUNC
282 || TYPE_CODE (scope_type
) == TYPE_CODE_METHOD
)
283 && domain
== VAR_DOMAIN
)
284 return lookup_symbol (nested
, SYMBOL_BLOCK_VALUE (scope_sym
.symbol
),
287 /* Look for a symbol named NESTED in this class/namespace.
288 The caller is assumed to have already have done a basic lookup of NAME.
289 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
290 return cp_lookup_nested_symbol_1 (scope_type
, nested
, name
,
291 block
, domain
, 0, is_in_anonymous
);
294 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
295 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
296 through base classes for a matching symbol.
298 Note: Part of the complexity is because NAME may itself specify scope.
299 Part of the complexity is also because this handles the case where
300 there is no scoping in which case we also try looking in the class of
301 "this" if we can compute it. */
303 static struct block_symbol
304 cp_lookup_symbol_in_namespace (const char *the_namespace
, const char *name
,
305 const struct block
*block
,
306 const domain_enum domain
, int search
)
308 char *concatenated_name
= NULL
;
310 unsigned int prefix_len
;
311 struct block_symbol sym
;
313 if (the_namespace
[0] != '\0')
316 = (char *) alloca (strlen (the_namespace
) + 2 + strlen (name
) + 1);
317 strcpy (concatenated_name
, the_namespace
);
318 strcat (concatenated_name
, "::");
319 strcat (concatenated_name
, name
);
320 name
= concatenated_name
;
323 prefix_len
= cp_entire_prefix_len (name
);
325 return cp_lookup_bare_symbol (NULL
, name
, block
, domain
, search
);
327 /* This would be simpler if we just called cp_lookup_nested_symbol
328 at this point. But that would require first looking up the containing
329 class/namespace. Since we're only searching static and global blocks
330 there's often no need to first do that lookup. */
333 = the_namespace
[0] != '\0' && cp_is_in_anonymous (the_namespace
);
334 sym
= cp_basic_lookup_symbol (name
, block
, domain
, is_in_anonymous
);
335 if (sym
.symbol
!= NULL
)
339 sym
= cp_search_static_and_baseclasses (name
, block
, domain
, prefix_len
,
345 /* Search for NAME by applying all import statements belonging to
346 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
347 search is restricted to using declarations.
355 If SEARCH_PARENTS the search will include imports which are
356 applicable in parents of SCOPE.
366 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
367 namespaces X and Y will be considered. If SEARCH_PARENTS is false
368 only the import of Y is considered.
370 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
371 pass 0 for it. Internally we pass 1 when recursing. */
373 static struct block_symbol
374 cp_lookup_symbol_via_imports (const char *scope
,
376 const struct block
*block
,
377 const domain_enum domain
,
378 const int search_scope_first
,
379 const int declaration_only
,
380 const int search_parents
)
382 struct using_direct
*current
;
383 struct block_symbol sym
= {};
387 /* First, try to find the symbol in the given namespace if requested. */
388 if (search_scope_first
)
389 sym
= cp_lookup_symbol_in_namespace (scope
, name
,
392 if (sym
.symbol
!= NULL
)
395 /* Go through the using directives. If any of them add new names to
396 the namespace we're searching in, see if we can find a match by
399 for (current
= block_using (block
);
401 current
= current
->next
)
403 const char **excludep
;
405 len
= strlen (current
->import_dest
);
406 directive_match
= (search_parents
407 ? (startswith (scope
, current
->import_dest
)
410 || scope
[len
] == '\0'))
411 : strcmp (scope
, current
->import_dest
) == 0);
413 /* If the import destination is the current scope or one of its
414 ancestors then it is applicable. */
415 if (directive_match
&& !current
->searched
)
417 /* Mark this import as searched so that the recursive call
418 does not search it again. */
419 scoped_restore reset_directive_searched
420 = make_scoped_restore (¤t
->searched
, 1);
422 /* If there is an import of a single declaration, compare the
423 imported declaration (after optional renaming by its alias)
424 with the sought out name. If there is a match pass
425 current->import_src as NAMESPACE to direct the search
426 towards the imported namespace. */
427 if (current
->declaration
428 && strcmp (name
, current
->alias
429 ? current
->alias
: current
->declaration
) == 0)
430 sym
= cp_lookup_symbol_in_namespace (current
->import_src
,
431 current
->declaration
,
434 /* If this is a DECLARATION_ONLY search or a symbol was found
435 or this import statement was an import declaration, the
436 search of this import is complete. */
437 if (declaration_only
|| sym
.symbol
!= NULL
|| current
->declaration
)
439 if (sym
.symbol
!= NULL
)
445 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
446 for (excludep
= current
->excludes
; *excludep
; excludep
++)
447 if (strcmp (name
, *excludep
) == 0)
452 if (current
->alias
!= NULL
453 && strcmp (name
, current
->alias
) == 0)
454 /* If the import is creating an alias and the alias matches
455 the sought name. Pass current->import_src as the NAME to
456 direct the search towards the aliased namespace. */
458 sym
= cp_lookup_symbol_in_namespace (scope
,
462 else if (current
->alias
== NULL
)
464 /* If this import statement creates no alias, pass
465 current->inner as NAMESPACE to direct the search
466 towards the imported namespace. */
467 sym
= cp_lookup_symbol_via_imports (current
->import_src
,
472 if (sym
.symbol
!= NULL
)
480 /* Helper function that searches an array of symbols for one named NAME. */
482 static struct symbol
*
483 search_symbol_list (const char *name
, int num
,
484 struct symbol
**syms
)
488 /* Maybe we should store a dictionary in here instead. */
489 for (i
= 0; i
< num
; ++i
)
491 if (strcmp (name
, syms
[i
]->natural_name ()) == 0)
497 /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it
498 searches through the template parameters of the function and the
502 cp_lookup_symbol_imports_or_template (const char *scope
,
504 const struct block
*block
,
505 const domain_enum domain
)
507 struct symbol
*function
= BLOCK_FUNCTION (block
);
508 struct block_symbol result
;
510 if (symbol_lookup_debug
)
512 fprintf_unfiltered (gdb_stdlog
,
513 "cp_lookup_symbol_imports_or_template"
514 " (%s, %s, %s, %s)\n",
515 scope
, name
, host_address_to_string (block
),
516 domain_name (domain
));
519 if (function
!= NULL
&& function
->language () == language_cplus
)
521 /* Search the function's template parameters. */
522 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function
))
524 struct template_symbol
*templ
525 = (struct template_symbol
*) function
;
526 struct symbol
*sym
= search_symbol_list (name
,
527 templ
->n_template_arguments
,
528 templ
->template_arguments
);
532 if (symbol_lookup_debug
)
534 fprintf_unfiltered (gdb_stdlog
,
535 "cp_lookup_symbol_imports_or_template"
537 host_address_to_string (sym
));
539 return (struct block_symbol
) {sym
, block
};
543 /* Search the template parameters of the function's defining
545 if (function
->natural_name ())
547 struct type
*context
;
548 std::string
name_copy (function
->natural_name ());
549 const struct language_defn
*lang
= language_def (language_cplus
);
550 const struct block
*parent
= BLOCK_SUPERBLOCK (block
);
555 unsigned int prefix_len
556 = cp_entire_prefix_len (name_copy
.c_str ());
562 name_copy
.erase (prefix_len
);
563 context
= lookup_typename (lang
,
572 = search_symbol_list (name
,
573 TYPE_N_TEMPLATE_ARGUMENTS (context
),
574 TYPE_TEMPLATE_ARGUMENTS (context
));
577 if (symbol_lookup_debug
)
581 "cp_lookup_symbol_imports_or_template (...) = %s\n",
582 host_address_to_string (sym
));
584 return (struct block_symbol
) {sym
, parent
};
590 result
= cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 0, 1, 1);
591 if (symbol_lookup_debug
)
593 fprintf_unfiltered (gdb_stdlog
,
594 "cp_lookup_symbol_imports_or_template (...) = %s\n",
595 result
.symbol
!= NULL
596 ? host_address_to_string (result
.symbol
) : "NULL");
601 /* Search for NAME by applying relevant import statements belonging to BLOCK
602 and its parents. SCOPE is the namespace scope of the context in which the
603 search is being evaluated. */
605 static struct block_symbol
606 cp_lookup_symbol_via_all_imports (const char *scope
, const char *name
,
607 const struct block
*block
,
608 const domain_enum domain
)
610 struct block_symbol sym
;
612 while (block
!= NULL
)
614 sym
= cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 0, 0, 1);
618 block
= BLOCK_SUPERBLOCK (block
);
624 /* Searches for NAME in the current namespace, and by applying
625 relevant import statements belonging to BLOCK and its parents.
626 SCOPE is the namespace scope of the context in which the search is
630 cp_lookup_symbol_namespace (const char *scope
,
632 const struct block
*block
,
633 const domain_enum domain
)
635 struct block_symbol sym
;
637 if (symbol_lookup_debug
)
639 fprintf_unfiltered (gdb_stdlog
,
640 "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n",
641 scope
, name
, host_address_to_string (block
),
642 domain_name (domain
));
645 /* First, try to find the symbol in the given namespace. */
646 sym
= cp_lookup_symbol_in_namespace (scope
, name
, block
, domain
, 1);
648 /* Search for name in namespaces imported to this and parent blocks. */
649 if (sym
.symbol
== NULL
)
650 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
652 if (symbol_lookup_debug
)
654 fprintf_unfiltered (gdb_stdlog
,
655 "cp_lookup_symbol_namespace (...) = %s\n",
657 ? host_address_to_string (sym
.symbol
) : "NULL");
662 /* Lookup NAME at namespace scope (or, in C terms, in static and
663 global variables). SCOPE is the namespace that the current
664 function is defined within; only consider namespaces whose length
665 is at least SCOPE_LEN. Other arguments are as in
666 cp_lookup_symbol_nonlocal.
668 For example, if we're within a function A::B::f and looking for a
669 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
670 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
671 but with SCOPE_LEN = 1. And then it calls itself with NAME and
672 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
673 "A::B::x"; if it doesn't find it, then the second call looks for
674 "A::x", and if that call fails, then the first call looks for
677 static struct block_symbol
678 lookup_namespace_scope (const struct language_defn
*langdef
,
680 const struct block
*block
,
681 const domain_enum domain
,
687 if (scope
[scope_len
] != '\0')
689 /* Recursively search for names in child namespaces first. */
691 struct block_symbol sym
;
692 int new_scope_len
= scope_len
;
694 /* If the current scope is followed by "::", skip past that. */
695 if (new_scope_len
!= 0)
697 gdb_assert (scope
[new_scope_len
] == ':');
700 new_scope_len
+= cp_find_first_component (scope
+ new_scope_len
);
701 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
,
702 scope
, new_scope_len
);
703 if (sym
.symbol
!= NULL
)
707 /* Okay, we didn't find a match in our children, so look for the
708 name in the current namespace.
710 If we there is no scope and we know we have a bare symbol, then short
711 circuit everything and call cp_lookup_bare_symbol directly.
712 This isn't an optimization, rather it allows us to pass LANGDEF which
713 is needed for primitive type lookup. The test doesn't have to be
714 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
715 template symbol with "::" in the argument list) then
716 cp_lookup_symbol_in_namespace will catch it. */
718 if (scope_len
== 0 && strchr (name
, ':') == NULL
)
719 return cp_lookup_bare_symbol (langdef
, name
, block
, domain
, 1);
721 the_namespace
= (char *) alloca (scope_len
+ 1);
722 strncpy (the_namespace
, scope
, scope_len
);
723 the_namespace
[scope_len
] = '\0';
724 return cp_lookup_symbol_in_namespace (the_namespace
, name
,
728 /* The C++-specific version of name lookup for static and global
729 names. This makes sure that names get looked for in all namespaces
730 that are in scope. NAME is the natural name of the symbol that
731 we're looking for, BLOCK is the block that we're searching within,
732 DOMAIN says what kind of symbols we're looking for. */
735 cp_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
737 const struct block
*block
,
738 const domain_enum domain
)
740 struct block_symbol sym
;
741 const char *scope
= block_scope (block
);
743 if (symbol_lookup_debug
)
745 fprintf_unfiltered (gdb_stdlog
,
746 "cp_lookup_symbol_non_local"
747 " (%s, %s (scope %s), %s)\n",
748 name
, host_address_to_string (block
), scope
,
749 domain_name (domain
));
752 /* First, try to find the symbol in the given namespace, and all
753 containing namespaces. */
754 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
, scope
, 0);
756 /* Search for name in namespaces imported to this and parent blocks. */
757 if (sym
.symbol
== NULL
)
758 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
760 if (symbol_lookup_debug
)
762 fprintf_unfiltered (gdb_stdlog
,
763 "cp_lookup_symbol_nonlocal (...) = %s\n",
765 ? host_address_to_string (sym
.symbol
)
771 /* Search through the base classes of PARENT_TYPE for a base class
772 named NAME and return its type. If not found, return NULL. */
775 cp_find_type_baseclass_by_name (struct type
*parent_type
, const char *name
)
779 parent_type
= check_typedef (parent_type
);
780 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
782 struct type
*type
= check_typedef (TYPE_BASECLASS (parent_type
, i
));
783 const char *base_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
785 if (base_name
== NULL
)
788 if (streq (base_name
, name
))
791 type
= cp_find_type_baseclass_by_name (type
, name
);
799 /* Search through the base classes of PARENT_TYPE for a symbol named
800 NAME in block BLOCK. */
802 static struct block_symbol
803 find_symbol_in_baseclass (struct type
*parent_type
, const char *name
,
804 const struct block
*block
, const domain_enum domain
,
808 struct block_symbol sym
= {};
810 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
812 struct type
*base_type
= TYPE_BASECLASS (parent_type
, i
);
813 const char *base_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
815 if (base_name
== NULL
)
818 std::string concatenated_name
= std::string (base_name
) + "::" + name
;
820 sym
= cp_lookup_nested_symbol_1 (base_type
, name
,
821 concatenated_name
.c_str (),
822 block
, domain
, 1, is_in_anonymous
);
823 if (sym
.symbol
!= NULL
)
830 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
831 and within the context of BLOCK.
832 NESTED_NAME may have scope ("::").
833 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
834 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
835 passed as an argument so that callers can control how space for it is
837 If BASIC_LOOKUP is non-zero then perform a basic lookup of
838 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
839 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
842 static struct block_symbol
843 cp_lookup_nested_symbol_1 (struct type
*container_type
,
844 const char *nested_name
,
845 const char *concatenated_name
,
846 const struct block
*block
,
847 const domain_enum domain
,
848 int basic_lookup
, int is_in_anonymous
)
850 struct block_symbol sym
;
852 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
853 of classes like, say, data or function members. Instead,
854 they're just represented by symbols whose names are
855 qualified by the name of the surrounding class. This is
856 just like members of namespaces; in particular,
857 cp_basic_lookup_symbol works when looking them up. */
861 sym
= cp_basic_lookup_symbol (concatenated_name
, block
, domain
,
863 if (sym
.symbol
!= NULL
)
867 /* Now search all static file-level symbols. We have to do this for things
868 like typedefs in the class. We do not try to guess any imported
869 namespace as even the fully specified namespace search is already not
870 C++ compliant and more assumptions could make it too magic. */
872 /* First search in this symtab, what we want is possibly there. */
873 sym
= lookup_symbol_in_static_block (concatenated_name
, block
, domain
);
874 if (sym
.symbol
!= NULL
)
877 /* Nope. We now have to search all static blocks in all objfiles,
878 even if block != NULL, because there's no guarantees as to which
879 symtab the symbol we want is in. Except for symbols defined in
880 anonymous namespaces should be treated as local to a single file,
881 which we just searched. */
882 if (!is_in_anonymous
)
884 sym
= lookup_static_symbol (concatenated_name
, domain
);
885 if (sym
.symbol
!= NULL
)
889 /* If this is a class with baseclasses, search them next. */
890 container_type
= check_typedef (container_type
);
891 if (TYPE_N_BASECLASSES (container_type
) > 0)
893 sym
= find_symbol_in_baseclass (container_type
, nested_name
, block
,
894 domain
, is_in_anonymous
);
895 if (sym
.symbol
!= NULL
)
902 /* Look up a symbol named NESTED_NAME that is nested inside the C++
903 class or namespace given by PARENT_TYPE, from within the context
904 given by BLOCK, and in DOMAIN.
905 Return NULL if there is no such nested symbol. */
908 cp_lookup_nested_symbol (struct type
*parent_type
,
909 const char *nested_name
,
910 const struct block
*block
,
911 const domain_enum domain
)
913 /* type_name_or_error provides better error reporting using the
915 struct type
*saved_parent_type
= parent_type
;
917 parent_type
= check_typedef (parent_type
);
919 if (symbol_lookup_debug
)
921 const char *type_name
= TYPE_NAME (saved_parent_type
);
923 fprintf_unfiltered (gdb_stdlog
,
924 "cp_lookup_nested_symbol (%s, %s, %s, %s)\n",
925 type_name
!= NULL
? type_name
: "unnamed",
926 nested_name
, host_address_to_string (block
),
927 domain_name (domain
));
930 switch (TYPE_CODE (parent_type
))
932 case TYPE_CODE_STRUCT
:
933 case TYPE_CODE_NAMESPACE
:
934 case TYPE_CODE_UNION
:
936 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
937 specific code to lookup nested symbols in modules, by calling the
938 function pointer la_lookup_symbol_nonlocal, which ends up here. */
939 case TYPE_CODE_MODULE
:
942 const char *parent_name
= type_name_or_error (saved_parent_type
);
943 struct block_symbol sym
;
944 char *concatenated_name
;
947 size
= strlen (parent_name
) + 2 + strlen (nested_name
) + 1;
948 concatenated_name
= (char *) alloca (size
);
949 xsnprintf (concatenated_name
, size
, "%s::%s",
950 parent_name
, nested_name
);
951 is_in_anonymous
= cp_is_in_anonymous (concatenated_name
);
953 sym
= cp_lookup_nested_symbol_1 (parent_type
, nested_name
,
954 concatenated_name
, block
, domain
,
957 if (symbol_lookup_debug
)
959 fprintf_unfiltered (gdb_stdlog
,
960 "cp_lookup_nested_symbol (...) = %s\n",
962 ? host_address_to_string (sym
.symbol
)
969 case TYPE_CODE_METHOD
:
970 if (symbol_lookup_debug
)
972 fprintf_unfiltered (gdb_stdlog
,
973 "cp_lookup_nested_symbol (...) = NULL"
979 internal_error (__FILE__
, __LINE__
,
980 _("cp_lookup_nested_symbol called "
981 "on a non-aggregate type."));
985 /* The C++-version of lookup_transparent_type. */
987 /* FIXME: carlton/2004-01-16: The problem that this is trying to
988 address is that, unfortunately, sometimes NAME is wrong: it may not
989 include the name of namespaces enclosing the type in question.
990 lookup_transparent_type gets called when the type in question
991 is a declaration, and we're trying to find its definition; but, for
992 declarations, our type name deduction mechanism doesn't work.
993 There's nothing we can do to fix this in general, I think, in the
994 absence of debug information about namespaces (I've filed PR
995 gdb/1511 about this); until such debug information becomes more
996 prevalent, one heuristic which sometimes looks is to search for the
997 definition in namespaces containing the current namespace.
999 We should delete this functions once the appropriate debug
1000 information becomes more widespread. (GCC 3.4 will be the first
1001 released version of GCC with such information.) */
1004 cp_lookup_transparent_type (const char *name
)
1006 /* First, try the honest way of looking up the definition. */
1007 struct type
*t
= basic_lookup_transparent_type (name
);
1013 /* If that doesn't work and we're within a namespace, look there
1015 scope
= block_scope (get_selected_block (0));
1017 if (scope
[0] == '\0')
1020 return cp_lookup_transparent_type_loop (name
, scope
, 0);
1023 /* Lookup the type definition associated to NAME in namespaces/classes
1024 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1025 must be the index of the start of a component of SCOPE. */
1027 static struct type
*
1028 cp_lookup_transparent_type_loop (const char *name
,
1032 int scope_length
= length
+ cp_find_first_component (scope
+ length
);
1035 /* If the current scope is followed by "::", look in the next
1037 if (scope
[scope_length
] == ':')
1040 = cp_lookup_transparent_type_loop (name
, scope
,
1047 full_name
= (char *) alloca (scope_length
+ 2 + strlen (name
) + 1);
1048 strncpy (full_name
, scope
, scope_length
);
1049 memcpy (full_name
+ scope_length
, "::", 2);
1050 strcpy (full_name
+ scope_length
+ 2, name
);
1052 return basic_lookup_transparent_type (full_name
);
1055 /* This used to do something but was removed when it became
1059 maintenance_cplus_namespace (const char *args
, int from_tty
)
1061 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
1065 _initialize_cp_namespace (void)
1067 struct cmd_list_element
*cmd
;
1069 cmd
= add_cmd ("namespace", class_maintenance
,
1070 maintenance_cplus_namespace
,
1071 _("Deprecated placeholder for removed functionality."),
1072 &maint_cplus_cmd_list
);
1073 deprecate_cmd (cmd
, NULL
);