1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003-2014 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"
35 static struct symbol
*
36 cp_lookup_nested_symbol_1 (struct type
*container_type
,
37 const char *nested_name
,
38 const char *concatenated_name
,
39 const struct block
*block
,
42 static struct type
*cp_lookup_transparent_type_loop (const char *name
,
46 /* Check to see if SYMBOL refers to an object contained within an
47 anonymous namespace; if so, add an appropriate using directive. */
50 cp_scan_for_anonymous_namespaces (const struct symbol
*const symbol
,
51 struct objfile
*const objfile
)
53 if (SYMBOL_DEMANGLED_NAME (symbol
) != NULL
)
55 const char *name
= SYMBOL_DEMANGLED_NAME (symbol
);
56 unsigned int previous_component
;
57 unsigned int next_component
;
59 /* Start with a quick-and-dirty check for mention of "(anonymous
62 if (!cp_is_in_anonymous (name
))
65 previous_component
= 0;
66 next_component
= cp_find_first_component (name
+ previous_component
);
68 while (name
[next_component
] == ':')
70 if (((next_component
- previous_component
)
71 == CP_ANONYMOUS_NAMESPACE_LEN
)
72 && strncmp (name
+ previous_component
,
73 CP_ANONYMOUS_NAMESPACE_STR
,
74 CP_ANONYMOUS_NAMESPACE_LEN
) == 0)
76 int dest_len
= (previous_component
== 0
77 ? 0 : previous_component
- 2);
78 int src_len
= next_component
;
80 char *dest
= alloca (dest_len
+ 1);
81 char *src
= alloca (src_len
+ 1);
83 memcpy (dest
, name
, dest_len
);
84 memcpy (src
, name
, src_len
);
86 dest
[dest_len
] = '\0';
89 /* We've found a component of the name that's an
90 anonymous namespace. So add symbols in it to the
91 namespace given by the previous component if there is
92 one, or to the global namespace if there isn't. */
93 cp_add_using_directive (dest
, src
, NULL
, NULL
, NULL
, 1,
94 &objfile
->objfile_obstack
);
96 /* The "+ 2" is for the "::". */
97 previous_component
= next_component
+ 2;
98 next_component
= (previous_component
99 + cp_find_first_component (name
100 + previous_component
));
105 /* Add a using directive to using_directives. If the using directive
106 in question has already been added, don't add it twice.
108 Create a new struct using_direct which imports the namespace SRC
109 into the scope DEST. ALIAS is the name of the imported namespace
110 in the current scope. If ALIAS is NULL then the namespace is known
111 by its original name. DECLARATION is the name if the imported
112 varable if this is a declaration import (Eg. using A::x), otherwise
113 it is NULL. EXCLUDES is a list of names not to import from an
114 imported module or NULL. If COPY_NAMES is non-zero, then the
115 arguments are copied into newly allocated memory so they can be
116 temporaries. For EXCLUDES the VEC pointers are copied but the
117 pointed to characters are not copied. */
120 cp_add_using_directive (const char *dest
,
123 const char *declaration
,
124 VEC (const_char_ptr
) *excludes
,
126 struct obstack
*obstack
)
128 struct using_direct
*current
;
129 struct using_direct
*new;
131 /* Has it already been added? */
133 for (current
= using_directives
; current
!= NULL
; current
= current
->next
)
138 if (strcmp (current
->import_src
, src
) != 0)
140 if (strcmp (current
->import_dest
, dest
) != 0)
142 if ((alias
== NULL
&& current
->alias
!= NULL
)
143 || (alias
!= NULL
&& current
->alias
== NULL
)
144 || (alias
!= NULL
&& current
->alias
!= NULL
145 && strcmp (alias
, current
->alias
) != 0))
147 if ((declaration
== NULL
&& current
->declaration
!= NULL
)
148 || (declaration
!= NULL
&& current
->declaration
== NULL
)
149 || (declaration
!= NULL
&& current
->declaration
!= NULL
150 && strcmp (declaration
, current
->declaration
) != 0))
153 /* Compare the contents of EXCLUDES. */
154 for (ix
= 0; VEC_iterate (const_char_ptr
, excludes
, ix
, param
); ix
++)
155 if (current
->excludes
[ix
] == NULL
156 || strcmp (param
, current
->excludes
[ix
]) != 0)
158 if (ix
< VEC_length (const_char_ptr
, excludes
)
159 || current
->excludes
[ix
] != NULL
)
162 /* Parameters exactly match CURRENT. */
166 new = obstack_alloc (obstack
, (sizeof (*new)
167 + (VEC_length (const_char_ptr
, excludes
)
168 * sizeof (*new->excludes
))));
169 memset (new, 0, sizeof (*new));
173 new->import_src
= obstack_copy0 (obstack
, src
, strlen (src
));
174 new->import_dest
= obstack_copy0 (obstack
, dest
, strlen (dest
));
178 new->import_src
= src
;
179 new->import_dest
= dest
;
182 if (alias
!= NULL
&& copy_names
)
183 new->alias
= obstack_copy0 (obstack
, alias
, strlen (alias
));
187 if (declaration
!= NULL
&& copy_names
)
188 new->declaration
= obstack_copy0 (obstack
,
189 declaration
, strlen (declaration
));
191 new->declaration
= declaration
;
193 memcpy (new->excludes
, VEC_address (const_char_ptr
, excludes
),
194 VEC_length (const_char_ptr
, excludes
) * sizeof (*new->excludes
));
195 new->excludes
[VEC_length (const_char_ptr
, excludes
)] = NULL
;
197 new->next
= using_directives
;
198 using_directives
= new;
201 /* Test whether or not NAMESPACE looks like it mentions an anonymous
202 namespace; return nonzero if so. */
205 cp_is_in_anonymous (const char *symbol_name
)
207 return (strstr (symbol_name
, CP_ANONYMOUS_NAMESPACE_STR
)
211 /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
212 If ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
213 within an anonymous namespace. */
215 static struct symbol
*
216 cp_basic_lookup_symbol (const char *name
, const struct block
*block
,
217 const domain_enum domain
, int anonymous_namespace
)
221 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
225 if (anonymous_namespace
)
227 /* Symbols defined in anonymous namespaces have external linkage
228 but should be treated as local to a single file nonetheless.
229 So we only search the current file's global block. */
231 const struct block
*global_block
= block_global_block (block
);
233 if (global_block
!= NULL
)
234 sym
= lookup_symbol_in_block (name
, global_block
, domain
);
238 sym
= lookup_global_symbol (name
, block
, domain
);
244 /* Search bare symbol NAME in DOMAIN in BLOCK.
245 NAME is guaranteed to not have any scope (no "::").
246 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
247 if so then also search for NAME in that class. */
249 static struct symbol
*
250 cp_lookup_bare_symbol (const char *name
, const struct block
*block
,
251 const domain_enum domain
, int search
)
255 /* Note: We can't do a simple assert for ':' not being in NAME because
256 ':' may be in the args of a template spec. This isn't intended to be
257 a complete test, just cheap and documentary. */
258 if (strchr (name
, '<') == NULL
&& strchr (name
, '(') == NULL
)
259 gdb_assert (strchr (name
, ':') == NULL
);
261 sym
= lookup_symbol_in_static_block (name
, block
, domain
);
265 sym
= lookup_global_symbol (name
, block
, domain
);
274 this = lookup_language_this (language_def (language_cplus
), block
);
278 type
= check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this)));
279 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
280 This can happen for lambda functions compiled with clang++,
281 which outputs no name for the container class. */
282 if (TYPE_NAME (type
) == NULL
)
285 /* Look for a symbol named NESTED in this class. */
286 sym
= cp_lookup_nested_symbol (type
, name
, block
);
292 /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
293 BLOCK specifies the context in which to perform the search.
294 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
295 then length the entire scope of NAME (up to, but not including, the last
298 Note: At least in the case of Fortran, which also uses this code, there
299 may be no text after the last "::". */
301 static struct symbol
*
302 cp_search_static_and_baseclasses (const char *name
,
303 const struct block
*block
,
304 const domain_enum domain
,
305 unsigned int prefix_len
)
308 char *klass
, *nested
;
309 struct cleanup
*cleanup
;
310 struct symbol
*klass_sym
;
311 struct type
*klass_type
;
313 /* The test here uses <= instead of < because Fortran also uses this,
314 and the module.exp testcase will pass "modmany::" for NAME here. */
315 gdb_assert (prefix_len
+ 2 <= strlen (name
));
316 gdb_assert (name
[prefix_len
+ 1] == ':');
318 /* Find the name of the class and the name of the method, variable, etc. */
320 /* The class name is everything up to and including PREFIX_LEN. */
321 klass
= savestring (name
, prefix_len
);
323 /* The rest of the name is everything else past the initial scope
325 nested
= xstrdup (name
+ prefix_len
+ 2);
327 /* Add cleanups to free memory for these strings. */
328 cleanup
= make_cleanup (xfree
, klass
);
329 make_cleanup (xfree
, nested
);
331 /* Lookup a class named KLASS. If none is found, there is nothing
332 more that can be done. */
333 klass_sym
= lookup_global_symbol (klass
, block
, domain
);
334 if (klass_sym
== NULL
)
336 do_cleanups (cleanup
);
339 klass_type
= SYMBOL_TYPE (klass_sym
);
341 /* Look for a symbol named NESTED in this class.
342 The caller is assumed to have already have done a basic lookup of NAME.
343 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
344 sym
= cp_lookup_nested_symbol_1 (klass_type
, nested
, name
, block
, 0);
346 do_cleanups (cleanup
);
350 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
351 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
352 through base classes for a matching symbol.
354 Note: Part of the complexity is because NAME may itself specify scope.
355 Part of the complexity is also because this handles the case where
356 there is no scoping in which case we also try looking in the class of
357 "this" if we can compute it. */
359 static struct symbol
*
360 cp_lookup_symbol_in_namespace (const char *namespace, const char *name
,
361 const struct block
*block
,
362 const domain_enum domain
, int search
)
364 char *concatenated_name
= NULL
;
366 unsigned int prefix_len
;
369 if (namespace[0] != '\0')
371 concatenated_name
= alloca (strlen (namespace) + 2
372 + strlen (name
) + 1);
373 strcpy (concatenated_name
, namespace);
374 strcat (concatenated_name
, "::");
375 strcat (concatenated_name
, name
);
376 name
= concatenated_name
;
379 prefix_len
= cp_entire_prefix_len (name
);
381 return cp_lookup_bare_symbol (name
, block
, domain
, search
);
383 /* This would be simpler if we just called cp_lookup_nested_symbol
384 at this point. But that would require first looking up the containing
385 class/namespace. Since we're only searching static and global blocks
386 there's often no need to first do that lookup. */
388 is_in_anonymous
= namespace[0] != '\0' && cp_is_in_anonymous (namespace);
389 sym
= cp_basic_lookup_symbol (name
, block
, domain
, is_in_anonymous
);
394 sym
= cp_search_static_and_baseclasses (name
, block
, domain
, prefix_len
);
399 /* Used for cleanups to reset the "searched" flag incase
403 reset_directive_searched (void *data
)
405 struct using_direct
*direct
= data
;
406 direct
->searched
= 0;
409 /* Search for NAME by applying all import statements belonging to
410 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
411 search is restricted to using declarations.
419 If SEARCH_PARENTS the search will include imports which are
420 applicable in parents of SCOPE.
430 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
431 namespaces X and Y will be considered. If SEARCH_PARENTS is false
432 only the import of Y is considered.
434 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
435 pass 0 for it. Internally we pass 1 when recursing. */
437 static struct symbol
*
438 cp_lookup_symbol_via_imports (const char *scope
,
440 const struct block
*block
,
441 const domain_enum domain
,
442 const int search_scope_first
,
443 const int declaration_only
,
444 const int search_parents
)
446 struct using_direct
*current
;
447 struct symbol
*sym
= NULL
;
450 struct cleanup
*searched_cleanup
;
452 /* First, try to find the symbol in the given namespace if requested. */
453 if (search_scope_first
)
454 sym
= cp_lookup_symbol_in_namespace (scope
, name
,
460 /* Go through the using directives. If any of them add new names to
461 the namespace we're searching in, see if we can find a match by
464 for (current
= block_using (block
);
466 current
= current
->next
)
468 const char **excludep
;
470 len
= strlen (current
->import_dest
);
471 directive_match
= (search_parents
472 ? (strncmp (scope
, current
->import_dest
,
473 strlen (current
->import_dest
)) == 0
476 || scope
[len
] == '\0'))
477 : strcmp (scope
, current
->import_dest
) == 0);
479 /* If the import destination is the current scope or one of its
480 ancestors then it is applicable. */
481 if (directive_match
&& !current
->searched
)
483 /* Mark this import as searched so that the recursive call
484 does not search it again. */
485 current
->searched
= 1;
486 searched_cleanup
= make_cleanup (reset_directive_searched
,
489 /* If there is an import of a single declaration, compare the
490 imported declaration (after optional renaming by its alias)
491 with the sought out name. If there is a match pass
492 current->import_src as NAMESPACE to direct the search
493 towards the imported namespace. */
494 if (current
->declaration
495 && strcmp (name
, current
->alias
496 ? current
->alias
: current
->declaration
) == 0)
497 sym
= cp_lookup_symbol_in_namespace (current
->import_src
,
498 current
->declaration
,
501 /* If this is a DECLARATION_ONLY search or a symbol was found
502 or this import statement was an import declaration, the
503 search of this import is complete. */
504 if (declaration_only
|| sym
!= NULL
|| current
->declaration
)
506 current
->searched
= 0;
507 discard_cleanups (searched_cleanup
);
515 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
516 for (excludep
= current
->excludes
; *excludep
; excludep
++)
517 if (strcmp (name
, *excludep
) == 0)
521 discard_cleanups (searched_cleanup
);
525 if (current
->alias
!= NULL
526 && strcmp (name
, current
->alias
) == 0)
527 /* If the import is creating an alias and the alias matches
528 the sought name. Pass current->import_src as the NAME to
529 direct the search towards the aliased namespace. */
531 sym
= cp_lookup_symbol_in_namespace (scope
,
535 else if (current
->alias
== NULL
)
537 /* If this import statement creates no alias, pass
538 current->inner as NAMESPACE to direct the search
539 towards the imported namespace. */
540 sym
= cp_lookup_symbol_via_imports (current
->import_src
,
544 current
->searched
= 0;
545 discard_cleanups (searched_cleanup
);
555 /* Helper function that searches an array of symbols for one named
558 static struct symbol
*
559 search_symbol_list (const char *name
, int num
,
560 struct symbol
**syms
)
564 /* Maybe we should store a dictionary in here instead. */
565 for (i
= 0; i
< num
; ++i
)
567 if (strcmp (name
, SYMBOL_NATURAL_NAME (syms
[i
])) == 0)
573 /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it
574 searches through the template parameters of the function and the
578 cp_lookup_symbol_imports_or_template (const char *scope
,
580 const struct block
*block
,
581 const domain_enum domain
)
583 struct symbol
*function
= BLOCK_FUNCTION (block
);
584 struct symbol
*result
;
586 if (symbol_lookup_debug
)
588 fprintf_unfiltered (gdb_stdlog
,
589 "cp_lookup_symbol_imports_or_template"
590 " (%s, %s, %s, %s)\n",
591 scope
, name
, host_address_to_string (block
),
592 domain_name (domain
));
595 if (function
!= NULL
&& SYMBOL_LANGUAGE (function
) == language_cplus
)
597 /* Search the function's template parameters. */
598 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function
))
600 struct template_symbol
*templ
601 = (struct template_symbol
*) function
;
603 result
= search_symbol_list (name
,
604 templ
->n_template_arguments
,
605 templ
->template_arguments
);
608 if (symbol_lookup_debug
)
610 fprintf_unfiltered (gdb_stdlog
,
611 "cp_lookup_symbol_imports_or_template"
613 host_address_to_string (result
));
619 /* Search the template parameters of the function's defining
621 if (SYMBOL_NATURAL_NAME (function
))
623 struct type
*context
;
624 char *name_copy
= xstrdup (SYMBOL_NATURAL_NAME (function
));
625 struct cleanup
*cleanups
= make_cleanup (xfree
, name_copy
);
626 const struct language_defn
*lang
= language_def (language_cplus
);
628 = get_objfile_arch (SYMBOL_OBJFILE (function
));
629 const struct block
*parent
= BLOCK_SUPERBLOCK (block
);
633 unsigned int prefix_len
= cp_entire_prefix_len (name_copy
);
639 name_copy
[prefix_len
] = '\0';
640 context
= lookup_typename (lang
, arch
,
649 = search_symbol_list (name
,
650 TYPE_N_TEMPLATE_ARGUMENTS (context
),
651 TYPE_TEMPLATE_ARGUMENTS (context
));
654 do_cleanups (cleanups
);
655 if (symbol_lookup_debug
)
657 fprintf_unfiltered (gdb_stdlog
,
658 "cp_lookup_symbol_imports_or_template"
660 host_address_to_string (result
));
666 do_cleanups (cleanups
);
670 result
= cp_lookup_symbol_via_imports (scope
, name
, block
, domain
, 0, 1, 1);
671 if (symbol_lookup_debug
)
673 fprintf_unfiltered (gdb_stdlog
,
674 "cp_lookup_symbol_imports_or_template (...) = %s\n",
676 ? host_address_to_string (result
) : "NULL");
681 /* Searches for NAME in the current namespace, and by applying
682 relevant import statements belonging to BLOCK and its parents.
683 SCOPE is the namespace scope of the context in which the search is
687 cp_lookup_symbol_namespace (const char *scope
,
689 const struct block
*block
,
690 const domain_enum domain
)
694 if (symbol_lookup_debug
)
696 fprintf_unfiltered (gdb_stdlog
,
697 "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n",
698 scope
, name
, host_address_to_string (block
),
699 domain_name (domain
));
702 /* First, try to find the symbol in the given namespace. */
703 sym
= cp_lookup_symbol_in_namespace (scope
, name
,
707 if (symbol_lookup_debug
)
709 fprintf_unfiltered (gdb_stdlog
,
710 "cp_lookup_symbol_namespace (...) = %s\n",
711 host_address_to_string (sym
));
716 /* Search for name in namespaces imported to this and parent
718 while (block
!= NULL
)
720 sym
= cp_lookup_symbol_via_imports (scope
, name
, block
,
725 if (symbol_lookup_debug
)
727 fprintf_unfiltered (gdb_stdlog
,
728 "cp_lookup_symbol_namespace (...) = %s\n",
729 host_address_to_string (sym
));
734 block
= BLOCK_SUPERBLOCK (block
);
737 if (symbol_lookup_debug
)
739 fprintf_unfiltered (gdb_stdlog
,
740 "cp_lookup_symbol_namespace (...) = NULL\n");
745 /* Lookup NAME at namespace scope (or, in C terms, in static and
746 global variables). SCOPE is the namespace that the current
747 function is defined within; only consider namespaces whose length
748 is at least SCOPE_LEN. Other arguments are as in
749 cp_lookup_symbol_nonlocal.
751 For example, if we're within a function A::B::f and looking for a
752 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
753 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
754 but with SCOPE_LEN = 1. And then it calls itself with NAME and
755 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
756 "A::B::x"; if it doesn't find it, then the second call looks for
757 "A::x", and if that call fails, then the first call looks for
760 static struct symbol
*
761 lookup_namespace_scope (const char *name
,
762 const struct block
*block
,
763 const domain_enum domain
,
769 if (scope
[scope_len
] != '\0')
771 /* Recursively search for names in child namespaces first. */
774 int new_scope_len
= scope_len
;
776 /* If the current scope is followed by "::", skip past that. */
777 if (new_scope_len
!= 0)
779 gdb_assert (scope
[new_scope_len
] == ':');
782 new_scope_len
+= cp_find_first_component (scope
+ new_scope_len
);
783 sym
= lookup_namespace_scope (name
, block
, domain
,
784 scope
, new_scope_len
);
789 /* Okay, we didn't find a match in our children, so look for the
790 name in the current namespace. */
792 namespace = alloca (scope_len
+ 1);
793 strncpy (namespace, scope
, scope_len
);
794 namespace[scope_len
] = '\0';
795 return cp_lookup_symbol_in_namespace (namespace, name
,
799 /* The C++-specific version of name lookup for static and global
800 names. This makes sure that names get looked for in all namespaces
801 that are in scope. NAME is the natural name of the symbol that
802 we're looking for, BLOCK is the block that we're searching within,
803 DOMAIN says what kind of symbols we're looking for. */
806 cp_lookup_symbol_nonlocal (const char *name
,
807 const struct block
*block
,
808 const domain_enum domain
)
811 const char *scope
= block_scope (block
);
813 if (symbol_lookup_debug
)
815 fprintf_unfiltered (gdb_stdlog
,
816 "cp_lookup_symbol_non_local"
817 " (%s, %s (scope %s), %s)\n",
818 name
, host_address_to_string (block
), scope
,
819 domain_name (domain
));
822 sym
= lookup_namespace_scope (name
, block
, domain
, scope
, 0);
825 if (symbol_lookup_debug
)
827 fprintf_unfiltered (gdb_stdlog
,
828 "cp_lookup_symbol_nonlocal (...) = %s\n",
829 host_address_to_string (sym
));
834 sym
= cp_lookup_symbol_namespace (scope
, name
, block
, domain
);
835 if (symbol_lookup_debug
)
837 fprintf_unfiltered (gdb_stdlog
,
838 "cp_lookup_symbol_nonlocal (...) = %s\n",
839 sym
!= NULL
? host_address_to_string (sym
) : "NULL");
844 /* Search through the base classes of PARENT_TYPE for a base class
845 named NAME and return its type. If not found, return NULL. */
848 cp_find_type_baseclass_by_name (struct type
*parent_type
, const char *name
)
852 CHECK_TYPEDEF (parent_type
);
853 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
855 struct type
*type
= check_typedef (TYPE_BASECLASS (parent_type
, i
));
856 const char *base_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
858 if (base_name
== NULL
)
861 if (streq (base_name
, name
))
864 type
= cp_find_type_baseclass_by_name (type
, name
);
872 /* Search through the base classes of PARENT_TYPE for a symbol named
873 NAME in block BLOCK. */
875 static struct symbol
*
876 find_symbol_in_baseclass (struct type
*parent_type
, const char *name
,
877 const struct block
*block
)
881 struct cleanup
*cleanup
;
882 char *concatenated_name
;
885 concatenated_name
= NULL
;
886 cleanup
= make_cleanup (free_current_contents
, &concatenated_name
);
888 for (i
= 0; i
< TYPE_N_BASECLASSES (parent_type
); ++i
)
891 struct type
*base_type
= TYPE_BASECLASS (parent_type
, i
);
892 const char *base_name
= TYPE_BASECLASS_NAME (parent_type
, i
);
894 if (base_name
== NULL
)
897 len
= strlen (base_name
) + 2 + strlen (name
) + 1;
898 concatenated_name
= xrealloc (concatenated_name
, len
);
899 xsnprintf (concatenated_name
, len
, "%s::%s", base_name
, name
);
901 sym
= cp_lookup_nested_symbol_1 (base_type
, name
, concatenated_name
,
907 do_cleanups (cleanup
);
911 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE within the
913 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
914 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
915 passed as an argument so that callers can control how space for it is
917 If BASIC_LOOKUP is non-zero then perform a basic lookup of
918 CONCATENATED_NAME. See cp_basic_lookup_symbol for details. */
920 static struct symbol
*
921 cp_lookup_nested_symbol_1 (struct type
*container_type
,
922 const char *nested_name
,
923 const char *concatenated_name
,
924 const struct block
*block
,
927 int is_in_anonymous
= cp_is_in_anonymous (concatenated_name
);
930 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
931 of classes like, say, data or function members. Instead,
932 they're just represented by symbols whose names are
933 qualified by the name of the surrounding class. This is
934 just like members of namespaces; in particular,
935 cp_basic_lookup_symbol works when looking them up. */
939 sym
= cp_basic_lookup_symbol (concatenated_name
, block
, VAR_DOMAIN
,
945 /* Now search all static file-level symbols. We have to do this for things
946 like typedefs in the class. We do not try to guess any imported
947 namespace as even the fully specified namespace search is already not
948 C++ compliant and more assumptions could make it too magic. */
950 /* First search in this symtab, what we want is possibly there. */
951 sym
= lookup_symbol_in_static_block (concatenated_name
, block
, VAR_DOMAIN
);
955 /* Nope. We now have to search all static blocks in all objfiles,
956 even if block != NULL, because there's no guarantees as to which
957 symtab the symbol we want is in. */
958 sym
= lookup_static_symbol (concatenated_name
, VAR_DOMAIN
);
962 /* If this is a class with baseclasses, search them next. */
963 CHECK_TYPEDEF (container_type
);
964 if (TYPE_N_BASECLASSES (container_type
) > 0)
966 sym
= find_symbol_in_baseclass (container_type
, nested_name
, block
);
974 /* Look up a symbol named NESTED_NAME that is nested inside the C++
975 class or namespace given by PARENT_TYPE, from within the context
976 given by BLOCK. Return NULL if there is no such nested symbol. */
979 cp_lookup_nested_symbol (struct type
*parent_type
,
980 const char *nested_name
,
981 const struct block
*block
)
983 /* type_name_no_tag_or_error provides better error reporting using the
985 struct type
*saved_parent_type
= parent_type
;
987 CHECK_TYPEDEF (parent_type
);
989 if (symbol_lookup_debug
)
991 const char *type_name
= type_name_no_tag (saved_parent_type
);
993 fprintf_unfiltered (gdb_stdlog
,
994 "cp_lookup_nested_symbol (%s, %s, %s)\n",
995 type_name
!= NULL
? type_name
: "unnamed",
996 nested_name
, host_address_to_string (block
));
999 switch (TYPE_CODE (parent_type
))
1001 case TYPE_CODE_STRUCT
:
1002 case TYPE_CODE_NAMESPACE
:
1003 case TYPE_CODE_UNION
:
1004 case TYPE_CODE_ENUM
:
1005 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
1006 specific code to lookup nested symbols in modules, by calling the
1007 function pointer la_lookup_symbol_nonlocal, which ends up here. */
1008 case TYPE_CODE_MODULE
:
1011 const char *parent_name
= type_name_no_tag_or_error (saved_parent_type
);
1013 char *concatenated_name
;
1015 size
= strlen (parent_name
) + 2 + strlen (nested_name
) + 1;
1016 concatenated_name
= alloca (size
);
1017 xsnprintf (concatenated_name
, size
, "%s::%s",
1018 parent_name
, nested_name
);
1020 sym
= cp_lookup_nested_symbol_1 (parent_type
, nested_name
,
1021 concatenated_name
, block
, 1);
1023 if (symbol_lookup_debug
)
1025 fprintf_unfiltered (gdb_stdlog
,
1026 "cp_lookup_nested_symbol (...) = %s\n",
1028 ? host_address_to_string (sym
) : "NULL");
1033 case TYPE_CODE_FUNC
:
1034 case TYPE_CODE_METHOD
:
1035 if (symbol_lookup_debug
)
1037 fprintf_unfiltered (gdb_stdlog
,
1038 "cp_lookup_nested_symbol (...) = NULL"
1039 " (func/method)\n");
1044 internal_error (__FILE__
, __LINE__
,
1045 _("cp_lookup_nested_symbol called "
1046 "on a non-aggregate type."));
1050 /* The C++-version of lookup_transparent_type. */
1052 /* FIXME: carlton/2004-01-16: The problem that this is trying to
1053 address is that, unfortunately, sometimes NAME is wrong: it may not
1054 include the name of namespaces enclosing the type in question.
1055 lookup_transparent_type gets called when the type in question
1056 is a declaration, and we're trying to find its definition; but, for
1057 declarations, our type name deduction mechanism doesn't work.
1058 There's nothing we can do to fix this in general, I think, in the
1059 absence of debug information about namespaces (I've filed PR
1060 gdb/1511 about this); until such debug information becomes more
1061 prevalent, one heuristic which sometimes looks is to search for the
1062 definition in namespaces containing the current namespace.
1064 We should delete this functions once the appropriate debug
1065 information becomes more widespread. (GCC 3.4 will be the first
1066 released version of GCC with such information.) */
1069 cp_lookup_transparent_type (const char *name
)
1071 /* First, try the honest way of looking up the definition. */
1072 struct type
*t
= basic_lookup_transparent_type (name
);
1078 /* If that doesn't work and we're within a namespace, look there
1080 scope
= block_scope (get_selected_block (0));
1082 if (scope
[0] == '\0')
1085 return cp_lookup_transparent_type_loop (name
, scope
, 0);
1088 /* Lookup the type definition associated to NAME in namespaces/classes
1089 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1090 must be the index of the start of a component of SCOPE. */
1092 static struct type
*
1093 cp_lookup_transparent_type_loop (const char *name
,
1097 int scope_length
= length
+ cp_find_first_component (scope
+ length
);
1100 /* If the current scope is followed by "::", look in the next
1102 if (scope
[scope_length
] == ':')
1105 = cp_lookup_transparent_type_loop (name
, scope
,
1112 full_name
= alloca (scope_length
+ 2 + strlen (name
) + 1);
1113 strncpy (full_name
, scope
, scope_length
);
1114 strncpy (full_name
+ scope_length
, "::", 2);
1115 strcpy (full_name
+ scope_length
+ 2, name
);
1117 return basic_lookup_transparent_type (full_name
);
1120 /* This used to do something but was removed when it became
1124 maintenance_cplus_namespace (char *args
, int from_tty
)
1126 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
1129 /* Provide a prototype to silence -Wmissing-prototypes. */
1130 extern initialize_file_ftype _initialize_cp_namespace
;
1133 _initialize_cp_namespace (void)
1135 struct cmd_list_element
*cmd
;
1137 cmd
= add_cmd ("namespace", class_maintenance
,
1138 maintenance_cplus_namespace
,
1139 _("Deprecated placeholder for removed functionality."),
1140 &maint_cplus_cmd_list
);
1141 deprecate_cmd (cmd
, NULL
);