1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003-2017 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 (const struct symbol
*const symbol
,
54 struct objfile
*const objfile
)
56 if (SYMBOL_DEMANGLED_NAME (symbol
) != NULL
)
58 const char *name
= SYMBOL_DEMANGLED_NAME (symbol
);
59 unsigned int previous_component
;
60 unsigned int next_component
;
62 /* Start with a quick-and-dirty check for mention of "(anonymous
65 if (!cp_is_in_anonymous (name
))
68 previous_component
= 0;
69 next_component
= cp_find_first_component (name
+ previous_component
);
71 while (name
[next_component
] == ':')
73 if (((next_component
- previous_component
)
74 == CP_ANONYMOUS_NAMESPACE_LEN
)
75 && strncmp (name
+ previous_component
,
76 CP_ANONYMOUS_NAMESPACE_STR
,
77 CP_ANONYMOUS_NAMESPACE_LEN
) == 0)
79 int dest_len
= (previous_component
== 0
80 ? 0 : previous_component
- 2);
81 int src_len
= next_component
;
83 char *dest
= (char *) alloca (dest_len
+ 1);
84 char *src
= (char *) alloca (src_len
+ 1);
86 memcpy (dest
, name
, dest_len
);
87 memcpy (src
, name
, src_len
);
89 dest
[dest_len
] = '\0';
92 /* We've found a component of the name that's an
93 anonymous namespace. So add symbols in it to the
94 namespace given by the previous component if there is
95 one, or to the global namespace if there isn't. */
96 std::vector
<const char *> excludes
;
97 add_using_directive (&local_using_directives
,
98 dest
, src
, NULL
, NULL
, excludes
, 1,
99 &objfile
->objfile_obstack
);
101 /* The "+ 2" is for the "::". */
102 previous_component
= next_component
+ 2;
103 next_component
= (previous_component
104 + cp_find_first_component (name
105 + previous_component
));
110 /* Test whether or not NAMESPACE looks like it mentions an anonymous
111 namespace; return nonzero if so. */
114 cp_is_in_anonymous (const char *symbol_name
)
116 return (strstr (symbol_name
, CP_ANONYMOUS_NAMESPACE_STR
)
120 /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
121 If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
122 within an anonymous namespace. */
124 static struct block_symbol
125 cp_basic_lookup_symbol (const char *name
, const struct block
*block
,
126 const domain_enum domain
, int is_in_anonymous
)
128 struct block_symbol sym
;
130 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
131 if (sym
.symbol
!= NULL
)
136 /* Symbols defined in anonymous namespaces have external linkage
137 but should be treated as local to a single file nonetheless.
138 So we only search the current file's global block. */
140 const struct block
*global_block
= block_global_block (block
);
142 if (global_block
!= NULL
)
144 sym
.symbol
= lookup_symbol_in_block (name
, global_block
, domain
);
145 sym
.block
= global_block
;
149 sym
= lookup_global_symbol (name
, block
, domain
);
154 /* Search bare symbol NAME in DOMAIN in BLOCK.
155 NAME is guaranteed to not have any scope (no "::") in its name, though
156 if for example NAME is a template spec then "::" may appear in the
158 If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
159 that language. Normally we wouldn't need LANGDEF but fortran also uses
161 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
162 if so then also search for NAME in that class. */
164 static struct block_symbol
165 cp_lookup_bare_symbol (const struct language_defn
*langdef
,
166 const char *name
, const struct block
*block
,
167 const domain_enum domain
, int search
)
169 struct block_symbol sym
;
171 /* Note: We can't do a simple assert for ':' not being in NAME because
172 ':' may be in the args of a template spec. This isn't intended to be
173 a complete test, just cheap and documentary. */
174 if (strchr (name
, '<') == NULL
&& strchr (name
, '(') == NULL
)
175 gdb_assert (strstr (name
, "::") == NULL
);
177 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
178 if (sym
.symbol
!= NULL
)
181 /* If we didn't find a definition for a builtin type in the static block,
182 search for it now. This is actually the right thing to do and can be
183 a massive performance win. E.g., when debugging a program with lots of
184 shared libraries we could search all of them only to find out the
185 builtin type isn't defined in any of them. This is common for types
187 if (langdef
!= NULL
&& domain
== VAR_DOMAIN
)
189 struct gdbarch
*gdbarch
;
192 gdbarch
= target_gdbarch ();
194 gdbarch
= block_gdbarch (block
);
196 = language_lookup_primitive_type_as_symbol (langdef
, gdbarch
, name
);
198 if (sym
.symbol
!= NULL
)
202 sym
= lookup_global_symbol (name
, block
, domain
);
203 if (sym
.symbol
!= NULL
)
208 struct block_symbol lang_this
;
211 lang_this
.symbol
= NULL
;
214 lang_this
= lookup_language_this (langdef
, block
);
216 if (lang_this
.symbol
== NULL
)
217 return null_block_symbol
;
220 type
= check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this
.symbol
)));
221 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
222 This can happen for lambda functions compiled with clang++,
223 which outputs no name for the container class. */
224 if (TYPE_NAME (type
) == NULL
)
225 return null_block_symbol
;
227 /* Look for symbol NAME in this class. */
228 sym
= cp_lookup_nested_symbol (type
, name
, block
, domain
);
234 /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
235 BLOCK specifies the context in which to perform the search.
236 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
237 the length of the entire scope of NAME (up to, but not including, the last
240 Note: At least in the case of Fortran, which also uses this code, there
241 may be no text after the last "::". */
243 static struct block_symbol
244 cp_search_static_and_baseclasses (const char *name
,
245 const struct block
*block
,
246 const domain_enum domain
,
247 unsigned int prefix_len
,
250 /* Check for malformed input. */
251 if (prefix_len
+ 2 > strlen (name
) || name
[prefix_len
+ 1] != ':')
252 return null_block_symbol
;
254 /* The class, namespace or function name is everything up to and
255 including PREFIX_LEN. */
256 std::string
scope (name
, prefix_len
);
258 /* The rest of the name is everything else past the initial scope
260 const char *nested
= name
+ prefix_len
+ 2;
262 /* Lookup the scope symbol. If none is found, there is nothing more
263 that can be done. SCOPE could be a namespace, so always look in
264 VAR_DOMAIN. This works for classes too because of
265 symbol_matches_domain (which should be replaced with something
266 else, but it's what we have today). */
267 block_symbol scope_sym
= lookup_symbol_in_static_block (scope
.c_str (),
269 if (scope_sym
.symbol
== NULL
)
270 scope_sym
= lookup_global_symbol (scope
.c_str (), block
, VAR_DOMAIN
);
271 if (scope_sym
.symbol
== NULL
)
272 return null_block_symbol
;
274 struct type
*scope_type
= SYMBOL_TYPE (scope_sym
.symbol
);
276 /* If the scope is a function/method, then look up NESTED as a local
277 static variable. E.g., "print 'function()::static_var'". */
278 if (TYPE_CODE (scope_type
) == TYPE_CODE_FUNC
279 || TYPE_CODE (scope_type
) == TYPE_CODE_METHOD
)
280 return lookup_symbol (nested
, SYMBOL_BLOCK_VALUE (scope_sym
.symbol
),
283 /* Look for a symbol named NESTED in this class/namespace.
284 The caller is assumed to have already have done a basic lookup of NAME.
285 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
286 return cp_lookup_nested_symbol_1 (scope_type
, nested
, name
,
287 block
, domain
, 0, is_in_anonymous
);
290 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
291 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
292 through base classes for a matching symbol.
294 Note: Part of the complexity is because NAME may itself specify scope.
295 Part of the complexity is also because this handles the case where
296 there is no scoping in which case we also try looking in the class of
297 "this" if we can compute it. */
299 static struct block_symbol
300 cp_lookup_symbol_in_namespace (const char *the_namespace
, const char *name
,
301 const struct block
*block
,
302 const domain_enum domain
, int search
)
304 char *concatenated_name
= NULL
;
306 unsigned int prefix_len
;
307 struct block_symbol sym
;
309 if (the_namespace
[0] != '\0')
312 = (char *) alloca (strlen (the_namespace
) + 2 + strlen (name
) + 1);
313 strcpy (concatenated_name
, the_namespace
);
314 strcat (concatenated_name
, "::");
315 strcat (concatenated_name
, name
);
316 name
= concatenated_name
;
319 prefix_len
= cp_entire_prefix_len (name
);
321 return cp_lookup_bare_symbol (NULL
, name
, block
, domain
, search
);
323 /* This would be simpler if we just called cp_lookup_nested_symbol
324 at this point. But that would require first looking up the containing
325 class/namespace. Since we're only searching static and global blocks
326 there's often no need to first do that lookup. */
329 = the_namespace
[0] != '\0' && cp_is_in_anonymous (the_namespace
);
330 sym
= cp_basic_lookup_symbol (name
, block
, domain
, is_in_anonymous
);
331 if (sym
.symbol
!= NULL
)
335 sym
= cp_search_static_and_baseclasses (name
, block
, domain
, prefix_len
,
341 /* Used for cleanups to reset the "searched" flag in case of an error. */
344 reset_directive_searched (void *data
)
346 struct using_direct
*direct
= (struct using_direct
*) data
;
347 direct
->searched
= 0;
350 /* Search for NAME by applying all import statements belonging to
351 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
352 search is restricted to using declarations.
360 If SEARCH_PARENTS the search will include imports which are
361 applicable in parents of SCOPE.
371 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
372 namespaces X and Y will be considered. If SEARCH_PARENTS is false
373 only the import of Y is considered.
375 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
376 pass 0 for it. Internally we pass 1 when recursing. */
378 static struct block_symbol
379 cp_lookup_symbol_via_imports (const char *scope
,
381 const struct block
*block
,
382 const domain_enum domain
,
383 const int search_scope_first
,
384 const int declaration_only
,
385 const int search_parents
)
387 struct using_direct
*current
;
388 struct block_symbol sym
;
391 struct cleanup
*searched_cleanup
;
396 /* First, try to find the symbol in the given namespace if requested. */
397 if (search_scope_first
)
398 sym
= cp_lookup_symbol_in_namespace (scope
, name
,
401 if (sym
.symbol
!= NULL
)
404 /* Go through the using directives. If any of them add new names to
405 the namespace we're searching in, see if we can find a match by
408 for (current
= block_using (block
);
410 current
= current
->next
)
412 const char **excludep
;
414 len
= strlen (current
->import_dest
);
415 directive_match
= (search_parents
416 ? (startswith (scope
, current
->import_dest
)
419 || scope
[len
] == '\0'))
420 : strcmp (scope
, current
->import_dest
) == 0);
422 /* If the import destination is the current scope or one of its
423 ancestors then it is applicable. */
424 if (directive_match
&& !current
->searched
)
426 /* Mark this import as searched so that the recursive call
427 does not search it again. */
428 current
->searched
= 1;
429 searched_cleanup
= make_cleanup (reset_directive_searched
,
432 /* If there is an import of a single declaration, compare the
433 imported declaration (after optional renaming by its alias)
434 with the sought out name. If there is a match pass
435 current->import_src as NAMESPACE to direct the search
436 towards the imported namespace. */
437 if (current
->declaration
438 && strcmp (name
, current
->alias
439 ? current
->alias
: current
->declaration
) == 0)
440 sym
= cp_lookup_symbol_in_namespace (current
->import_src
,
441 current
->declaration
,
444 /* If this is a DECLARATION_ONLY search or a symbol was found
445 or this import statement was an import declaration, the
446 search of this import is complete. */
447 if (declaration_only
|| sym
.symbol
!= NULL
|| current
->declaration
)
449 current
->searched
= 0;
450 discard_cleanups (searched_cleanup
);
452 if (sym
.symbol
!= NULL
)
458 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
459 for (excludep
= current
->excludes
; *excludep
; excludep
++)
460 if (strcmp (name
, *excludep
) == 0)
464 discard_cleanups (searched_cleanup
);
468 if (current
->alias
!= NULL
469 && strcmp (name
, current
->alias
) == 0)
470 /* If the import is creating an alias and the alias matches
471 the sought name. Pass current->import_src as the NAME to
472 direct the search towards the aliased namespace. */
474 sym
= cp_lookup_symbol_in_namespace (scope
,
478 else if (current
->alias
== NULL
)
480 /* If this import statement creates no alias, pass
481 current->inner as NAMESPACE to direct the search
482 towards the imported namespace. */
483 sym
= cp_lookup_symbol_via_imports (current
->import_src
,
487 current
->searched
= 0;
488 discard_cleanups (searched_cleanup
);
490 if (sym
.symbol
!= NULL
)
495 return null_block_symbol
;
498 /* Helper function that searches an array of symbols for one named NAME. */
500 static struct symbol
*
501 search_symbol_list (const char *name
, int num
,
502 struct symbol
**syms
)
506 /* Maybe we should store a dictionary in here instead. */
507 for (i
= 0; i
< num
; ++i
)
509 if (strcmp (name
, SYMBOL_NATURAL_NAME (syms
[i
])) == 0)
515 /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it
516 searches through the template parameters of the function and the
520 cp_lookup_symbol_imports_or_template (const char *scope
,
522 const struct block
*block
,
523 const domain_enum domain
)
525 struct symbol
*function
= BLOCK_FUNCTION (block
);
526 struct block_symbol result
;
528 if (symbol_lookup_debug
)
530 fprintf_unfiltered (gdb_stdlog
,
531 "cp_lookup_symbol_imports_or_template"
532 " (%s, %s, %s, %s)\n",
533 scope
, name
, host_address_to_string (block
),
534 domain_name (domain
));
537 if (function
!= NULL
&& SYMBOL_LANGUAGE (function
) == language_cplus
)
539 /* Search the function's template parameters. */
540 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function
))
542 struct template_symbol
*templ
543 = (struct template_symbol
*) function
;
544 struct symbol
*sym
= search_symbol_list (name
,
545 templ
->n_template_arguments
,
546 templ
->template_arguments
);
550 if (symbol_lookup_debug
)
552 fprintf_unfiltered (gdb_stdlog
,
553 "cp_lookup_symbol_imports_or_template"
555 host_address_to_string (sym
));
557 return (struct block_symbol
) {sym
, block
};
561 /* Search the template parameters of the function's defining
563 if (SYMBOL_NATURAL_NAME (function
))
565 struct type
*context
;
566 std::string
name_copy (SYMBOL_NATURAL_NAME (function
));
567 const struct language_defn
*lang
= language_def (language_cplus
);
568 struct gdbarch
*arch
= symbol_arch (function
);
569 const struct block
*parent
= BLOCK_SUPERBLOCK (block
);
574 unsigned int prefix_len
575 = cp_entire_prefix_len (name_copy
.c_str ());
581 name_copy
.erase (prefix_len
);
582 context
= lookup_typename (lang
, arch
,
591 = search_symbol_list (name
,
592 TYPE_N_TEMPLATE_ARGUMENTS (context
),
593 TYPE_TEMPLATE_ARGUMENTS (context
));
596 if (symbol_lookup_debug
)
600 "cp_lookup_symbol_imports_or_template (...) = %s\n",
601 host_address_to_string (sym
));
603 return (struct block_symbol
) {sym
, parent
};
609 result
= cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 0, 1, 1);
610 if (symbol_lookup_debug
)
612 fprintf_unfiltered (gdb_stdlog
,
613 "cp_lookup_symbol_imports_or_template (...) = %s\n",
614 result
.symbol
!= NULL
615 ? host_address_to_string (result
.symbol
) : "NULL");
620 /* Search for NAME by applying relevant import statements belonging to BLOCK
621 and its parents. SCOPE is the namespace scope of the context in which the
622 search is being evaluated. */
624 static struct block_symbol
625 cp_lookup_symbol_via_all_imports (const char *scope
, const char *name
,
626 const struct block
*block
,
627 const domain_enum domain
)
629 struct block_symbol sym
;
631 while (block
!= NULL
)
633 sym
= cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 0, 0, 1);
637 block
= BLOCK_SUPERBLOCK (block
);
640 return null_block_symbol
;
643 /* Searches for NAME in the current namespace, and by applying
644 relevant import statements belonging to BLOCK and its parents.
645 SCOPE is the namespace scope of the context in which the search is
649 cp_lookup_symbol_namespace (const char *scope
,
651 const struct block
*block
,
652 const domain_enum domain
)
654 struct block_symbol sym
;
656 if (symbol_lookup_debug
)
658 fprintf_unfiltered (gdb_stdlog
,
659 "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n",
660 scope
, name
, host_address_to_string (block
),
661 domain_name (domain
));
664 /* First, try to find the symbol in the given namespace. */
665 sym
= cp_lookup_symbol_in_namespace (scope
, name
, block
, domain
, 1);
667 /* Search for name in namespaces imported to this and parent blocks. */
668 if (sym
.symbol
== NULL
)
669 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
671 if (symbol_lookup_debug
)
673 fprintf_unfiltered (gdb_stdlog
,
674 "cp_lookup_symbol_namespace (...) = %s\n",
676 ? host_address_to_string (sym
.symbol
) : "NULL");
681 /* Lookup NAME at namespace scope (or, in C terms, in static and
682 global variables). SCOPE is the namespace that the current
683 function is defined within; only consider namespaces whose length
684 is at least SCOPE_LEN. Other arguments are as in
685 cp_lookup_symbol_nonlocal.
687 For example, if we're within a function A::B::f and looking for a
688 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
689 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
690 but with SCOPE_LEN = 1. And then it calls itself with NAME and
691 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
692 "A::B::x"; if it doesn't find it, then the second call looks for
693 "A::x", and if that call fails, then the first call looks for
696 static struct block_symbol
697 lookup_namespace_scope (const struct language_defn
*langdef
,
699 const struct block
*block
,
700 const domain_enum domain
,
706 if (scope
[scope_len
] != '\0')
708 /* Recursively search for names in child namespaces first. */
710 struct block_symbol sym
;
711 int new_scope_len
= scope_len
;
713 /* If the current scope is followed by "::", skip past that. */
714 if (new_scope_len
!= 0)
716 gdb_assert (scope
[new_scope_len
] == ':');
719 new_scope_len
+= cp_find_first_component (scope
+ new_scope_len
);
720 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
,
721 scope
, new_scope_len
);
722 if (sym
.symbol
!= NULL
)
726 /* Okay, we didn't find a match in our children, so look for the
727 name in the current namespace.
729 If we there is no scope and we know we have a bare symbol, then short
730 circuit everything and call cp_lookup_bare_symbol directly.
731 This isn't an optimization, rather it allows us to pass LANGDEF which
732 is needed for primitive type lookup. The test doesn't have to be
733 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
734 template symbol with "::" in the argument list) then
735 cp_lookup_symbol_in_namespace will catch it. */
737 if (scope_len
== 0 && strchr (name
, ':') == NULL
)
738 return cp_lookup_bare_symbol (langdef
, name
, block
, domain
, 1);
740 the_namespace
= (char *) alloca (scope_len
+ 1);
741 strncpy (the_namespace
, scope
, scope_len
);
742 the_namespace
[scope_len
] = '\0';
743 return cp_lookup_symbol_in_namespace (the_namespace
, name
,
747 /* The C++-specific version of name lookup for static and global
748 names. This makes sure that names get looked for in all namespaces
749 that are in scope. NAME is the natural name of the symbol that
750 we're looking for, BLOCK is the block that we're searching within,
751 DOMAIN says what kind of symbols we're looking for. */
754 cp_lookup_symbol_nonlocal (const struct language_defn
*langdef
,
756 const struct block
*block
,
757 const domain_enum domain
)
759 struct block_symbol sym
;
760 const char *scope
= block_scope (block
);
762 if (symbol_lookup_debug
)
764 fprintf_unfiltered (gdb_stdlog
,
765 "cp_lookup_symbol_non_local"
766 " (%s, %s (scope %s), %s)\n",
767 name
, host_address_to_string (block
), scope
,
768 domain_name (domain
));
771 /* First, try to find the symbol in the given namespace, and all
772 containing namespaces. */
773 sym
= lookup_namespace_scope (langdef
, name
, block
, domain
, scope
, 0);
775 /* Search for name in namespaces imported to this and parent blocks. */
776 if (sym
.symbol
== NULL
)
777 sym
= cp_lookup_symbol_via_all_imports (scope
, name
, block
, domain
);
779 if (symbol_lookup_debug
)
781 fprintf_unfiltered (gdb_stdlog
,
782 "cp_lookup_symbol_nonlocal (...) = %s\n",
784 ? host_address_to_string (sym
.symbol
)
790 /* Search through the base classes of PARENT_TYPE for a base class
791 named NAME and return its type. If not found, return NULL. */
794 cp_find_type_baseclass_by_name (struct type
*parent_type
, const char *name
)
798 parent_type
= check_typedef (parent_type
);
799 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
801 struct type
*type
= check_typedef (TYPE_BASECLASS (parent_type
, i
));
802 const char *base_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
804 if (base_name
== NULL
)
807 if (streq (base_name
, name
))
810 type
= cp_find_type_baseclass_by_name (type
, name
);
818 /* Search through the base classes of PARENT_TYPE for a symbol named
819 NAME in block BLOCK. */
821 static struct block_symbol
822 find_symbol_in_baseclass (struct type
*parent_type
, const char *name
,
823 const struct block
*block
, const domain_enum domain
,
827 struct block_symbol sym
;
832 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
834 struct type
*base_type
= TYPE_BASECLASS (parent_type
, i
);
835 const char *base_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
837 if (base_name
== NULL
)
840 std::string concatenated_name
= std::string (base_name
) + "::" + name
;
842 sym
= cp_lookup_nested_symbol_1 (base_type
, name
,
843 concatenated_name
.c_str (),
844 block
, domain
, 1, is_in_anonymous
);
845 if (sym
.symbol
!= NULL
)
852 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
853 and within the context of BLOCK.
854 NESTED_NAME may have scope ("::").
855 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
856 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
857 passed as an argument so that callers can control how space for it is
859 If BASIC_LOOKUP is non-zero then perform a basic lookup of
860 CONCATENATED_NAME. See cp_basic_lookup_symbol for details.
861 If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
864 static struct block_symbol
865 cp_lookup_nested_symbol_1 (struct type
*container_type
,
866 const char *nested_name
,
867 const char *concatenated_name
,
868 const struct block
*block
,
869 const domain_enum domain
,
870 int basic_lookup
, int is_in_anonymous
)
872 struct block_symbol sym
;
874 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
875 of classes like, say, data or function members. Instead,
876 they're just represented by symbols whose names are
877 qualified by the name of the surrounding class. This is
878 just like members of namespaces; in particular,
879 cp_basic_lookup_symbol works when looking them up. */
883 sym
= cp_basic_lookup_symbol (concatenated_name
, block
, domain
,
885 if (sym
.symbol
!= NULL
)
889 /* Now search all static file-level symbols. We have to do this for things
890 like typedefs in the class. We do not try to guess any imported
891 namespace as even the fully specified namespace search is already not
892 C++ compliant and more assumptions could make it too magic. */
894 /* First search in this symtab, what we want is possibly there. */
895 sym
= lookup_symbol_in_static_block (concatenated_name
, block
, domain
);
896 if (sym
.symbol
!= NULL
)
899 /* Nope. We now have to search all static blocks in all objfiles,
900 even if block != NULL, because there's no guarantees as to which
901 symtab the symbol we want is in. Except for symbols defined in
902 anonymous namespaces should be treated as local to a single file,
903 which we just searched. */
904 if (!is_in_anonymous
)
906 sym
= lookup_static_symbol (concatenated_name
, domain
);
907 if (sym
.symbol
!= NULL
)
911 /* If this is a class with baseclasses, search them next. */
912 container_type
= check_typedef (container_type
);
913 if (TYPE_N_BASECLASSES (container_type
) > 0)
915 sym
= find_symbol_in_baseclass (container_type
, nested_name
, block
,
916 domain
, is_in_anonymous
);
917 if (sym
.symbol
!= NULL
)
921 return null_block_symbol
;
924 /* Look up a symbol named NESTED_NAME that is nested inside the C++
925 class or namespace given by PARENT_TYPE, from within the context
926 given by BLOCK, and in DOMAIN.
927 Return NULL if there is no such nested symbol. */
930 cp_lookup_nested_symbol (struct type
*parent_type
,
931 const char *nested_name
,
932 const struct block
*block
,
933 const domain_enum domain
)
935 /* type_name_no_tag_or_error provides better error reporting using the
937 struct type
*saved_parent_type
= parent_type
;
939 parent_type
= check_typedef (parent_type
);
941 if (symbol_lookup_debug
)
943 const char *type_name
= type_name_no_tag (saved_parent_type
);
945 fprintf_unfiltered (gdb_stdlog
,
946 "cp_lookup_nested_symbol (%s, %s, %s, %s)\n",
947 type_name
!= NULL
? type_name
: "unnamed",
948 nested_name
, host_address_to_string (block
),
949 domain_name (domain
));
952 switch (TYPE_CODE (parent_type
))
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 function pointer la_lookup_symbol_nonlocal, which ends up here. */
961 case TYPE_CODE_MODULE
:
964 const char *parent_name
= type_name_no_tag_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 if (symbol_lookup_debug
)
981 fprintf_unfiltered (gdb_stdlog
,
982 "cp_lookup_nested_symbol (...) = %s\n",
984 ? host_address_to_string (sym
.symbol
)
991 case TYPE_CODE_METHOD
:
992 if (symbol_lookup_debug
)
994 fprintf_unfiltered (gdb_stdlog
,
995 "cp_lookup_nested_symbol (...) = NULL"
998 return null_block_symbol
;
1001 internal_error (__FILE__
, __LINE__
,
1002 _("cp_lookup_nested_symbol called "
1003 "on a non-aggregate type."));
1007 /* The C++-version of lookup_transparent_type. */
1009 /* FIXME: carlton/2004-01-16: The problem that this is trying to
1010 address is that, unfortunately, sometimes NAME is wrong: it may not
1011 include the name of namespaces enclosing the type in question.
1012 lookup_transparent_type gets called when the type in question
1013 is a declaration, and we're trying to find its definition; but, for
1014 declarations, our type name deduction mechanism doesn't work.
1015 There's nothing we can do to fix this in general, I think, in the
1016 absence of debug information about namespaces (I've filed PR
1017 gdb/1511 about this); until such debug information becomes more
1018 prevalent, one heuristic which sometimes looks is to search for the
1019 definition in namespaces containing the current namespace.
1021 We should delete this functions once the appropriate debug
1022 information becomes more widespread. (GCC 3.4 will be the first
1023 released version of GCC with such information.) */
1026 cp_lookup_transparent_type (const char *name
)
1028 /* First, try the honest way of looking up the definition. */
1029 struct type
*t
= basic_lookup_transparent_type (name
);
1035 /* If that doesn't work and we're within a namespace, look there
1037 scope
= block_scope (get_selected_block (0));
1039 if (scope
[0] == '\0')
1042 return cp_lookup_transparent_type_loop (name
, scope
, 0);
1045 /* Lookup the type definition associated to NAME in namespaces/classes
1046 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1047 must be the index of the start of a component of SCOPE. */
1049 static struct type
*
1050 cp_lookup_transparent_type_loop (const char *name
,
1054 int scope_length
= length
+ cp_find_first_component (scope
+ length
);
1057 /* If the current scope is followed by "::", look in the next
1059 if (scope
[scope_length
] == ':')
1062 = cp_lookup_transparent_type_loop (name
, scope
,
1069 full_name
= (char *) alloca (scope_length
+ 2 + strlen (name
) + 1);
1070 strncpy (full_name
, scope
, scope_length
);
1071 strncpy (full_name
+ scope_length
, "::", 2);
1072 strcpy (full_name
+ scope_length
+ 2, name
);
1074 return basic_lookup_transparent_type (full_name
);
1077 /* This used to do something but was removed when it became
1081 maintenance_cplus_namespace (const char *args
, int from_tty
)
1083 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
1087 _initialize_cp_namespace (void)
1089 struct cmd_list_element
*cmd
;
1091 cmd
= add_cmd ("namespace", class_maintenance
,
1092 maintenance_cplus_namespace
,
1093 _("Deprecated placeholder for removed functionality."),
1094 &maint_cplus_cmd_list
);
1095 deprecate_cmd (cmd
, NULL
);