1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003-2004, 2007-2012 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"
26 #include "gdb_assert.h"
30 #include "dictionary.h"
36 static struct symbol *lookup_namespace_scope (const char *name,
37 const struct block *block,
38 const domain_enum domain,
42 static struct symbol *lookup_symbol_file (const char *name,
43 const struct block *block,
44 const domain_enum domain,
45 int anonymous_namespace);
47 static struct type *cp_lookup_transparent_type_loop (const char *name,
51 /* Check to see if SYMBOL refers to an object contained within an
52 anonymous namespace; if so, add an appropriate using directive. */
55 cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
56 struct objfile *const objfile)
58 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
60 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
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_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 = alloca (dest_len + 1);
86 char *src = 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 cp_add_using_directive (dest, src, NULL, NULL, NULL,
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));
111 /* Add a using directive to using_directives. If the using directive
112 in question has already been added, don't add it twice.
114 Create a new struct using_direct which imports the namespace SRC
115 into the scope DEST. ALIAS is the name of the imported namespace
116 in the current scope. If ALIAS is NULL then the namespace is known
117 by its original name. DECLARATION is the name if the imported
118 varable if this is a declaration import (Eg. using A::x), otherwise
119 it is NULL. EXCLUDES is a list of names not to import from an imported
120 module or NULL. The arguments are copied into newly allocated memory so
121 they can be temporaries. For EXCLUDES the VEC pointers are copied but the
122 pointed to characters are not copied. */
125 cp_add_using_directive (const char *dest,
128 const char *declaration,
129 VEC (const_char_ptr) *excludes,
130 struct obstack *obstack)
132 struct using_direct *current;
133 struct using_direct *new;
135 /* Has it already been added? */
137 for (current = using_directives; current != NULL; current = current->next)
142 if (strcmp (current->import_src, src) != 0)
144 if (strcmp (current->import_dest, dest) != 0)
146 if ((alias == NULL && current->alias != NULL)
147 || (alias != NULL && current->alias == NULL)
148 || (alias != NULL && current->alias != NULL
149 && strcmp (alias, current->alias) != 0))
151 if ((declaration == NULL && current->declaration != NULL)
152 || (declaration != NULL && current->declaration == NULL)
153 || (declaration != NULL && current->declaration != NULL
154 && strcmp (declaration, current->declaration) != 0))
157 /* Compare the contents of EXCLUDES. */
158 for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
159 if (current->excludes[ix] == NULL
160 || strcmp (param, current->excludes[ix]) != 0)
162 if (ix < VEC_length (const_char_ptr, excludes)
163 || current->excludes[ix] != NULL)
166 /* Parameters exactly match CURRENT. */
170 new = obstack_alloc (obstack, (sizeof (*new)
171 + (VEC_length (const_char_ptr, excludes)
172 * sizeof (*new->excludes))));
173 memset (new, 0, sizeof (*new));
175 new->import_src = obsavestring (src, strlen (src), obstack);
176 new->import_dest = obsavestring (dest, strlen (dest), obstack);
179 new->alias = obsavestring (alias, strlen (alias), obstack);
181 if (declaration != NULL)
182 new->declaration = obsavestring (declaration, strlen (declaration),
185 memcpy (new->excludes, VEC_address (const_char_ptr, excludes),
186 VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes));
187 new->excludes[VEC_length (const_char_ptr, excludes)] = NULL;
189 new->next = using_directives;
190 using_directives = new;
193 /* Record the namespace that the function defined by SYMBOL was
194 defined in, if necessary. BLOCK is the associated block; use
195 OBSTACK for allocation. */
198 cp_set_block_scope (const struct symbol *symbol,
200 struct obstack *obstack,
201 const char *processing_current_prefix,
202 int processing_has_namespace_info)
204 if (processing_has_namespace_info)
207 (block, obsavestring (processing_current_prefix,
208 strlen (processing_current_prefix),
212 else if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
214 /* Try to figure out the appropriate namespace from the
217 /* FIXME: carlton/2003-04-15: If the function in question is
218 a method of a class, the name will actually include the
219 name of the class as well. This should be harmless, but
220 is a little unfortunate. */
222 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
223 unsigned int prefix_len = cp_entire_prefix_len (name);
225 block_set_scope (block,
226 obsavestring (name, prefix_len, obstack),
231 /* Test whether or not NAMESPACE looks like it mentions an anonymous
232 namespace; return nonzero if so. */
235 cp_is_anonymous (const char *namespace)
237 return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR)
241 /* The C++-specific version of name lookup for static and global
242 names. This makes sure that names get looked for in all namespaces
243 that are in scope. NAME is the natural name of the symbol that
244 we're looking for, BLOCK is the block that we're searching within,
245 DOMAIN says what kind of symbols we're looking for, and if SYMTAB
246 is non-NULL, we should store the symtab where we found the symbol
250 cp_lookup_symbol_nonlocal (const char *name,
251 const struct block *block,
252 const domain_enum domain)
255 const char *scope = block_scope (block);
257 sym = lookup_namespace_scope (name, block,
262 return cp_lookup_symbol_namespace (scope, name,
266 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
267 as in cp_lookup_symbol_nonlocal. */
269 static struct symbol *
270 cp_lookup_symbol_in_namespace (const char *namespace,
272 const struct block *block,
273 const domain_enum domain)
275 if (namespace[0] == '\0')
277 return lookup_symbol_file (name, block, domain, 0);
281 char *concatenated_name = alloca (strlen (namespace) + 2
282 + strlen (name) + 1);
284 strcpy (concatenated_name, namespace);
285 strcat (concatenated_name, "::");
286 strcat (concatenated_name, name);
287 return lookup_symbol_file (concatenated_name, block, domain,
288 cp_is_anonymous (namespace));
292 /* Used for cleanups to reset the "searched" flag incase
296 reset_directive_searched (void *data)
298 struct using_direct *direct = data;
299 direct->searched = 0;
302 /* Search for NAME by applying all import statements belonging to
303 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
304 search is restricted to using declarations.
312 If SEARCH_PARENTS the search will include imports which are
313 applicable in parents of SCOPE.
323 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
324 namespaces X and Y will be considered. If SEARCH_PARENTS is false
325 only the import of Y is considered. */
328 cp_lookup_symbol_imports (const char *scope,
330 const struct block *block,
331 const domain_enum domain,
332 const int declaration_only,
333 const int search_parents)
335 struct using_direct *current;
336 struct symbol *sym = NULL;
339 struct cleanup *searched_cleanup;
341 /* First, try to find the symbol in the given namespace. */
342 if (!declaration_only)
343 sym = cp_lookup_symbol_in_namespace (scope, name,
349 /* Go through the using directives. If any of them add new names to
350 the namespace we're searching in, see if we can find a match by
353 for (current = block_using (block);
355 current = current->next)
357 const char **excludep;
359 len = strlen (current->import_dest);
360 directive_match = (search_parents
361 ? (strncmp (scope, current->import_dest,
362 strlen (current->import_dest)) == 0
365 || scope[len] == '\0'))
366 : strcmp (scope, current->import_dest) == 0);
368 /* If the import destination is the current scope or one of its
369 ancestors then it is applicable. */
370 if (directive_match && !current->searched)
372 /* Mark this import as searched so that the recursive call
373 does not search it again. */
374 current->searched = 1;
375 searched_cleanup = make_cleanup (reset_directive_searched,
378 /* If there is an import of a single declaration, compare the
379 imported declaration (after optional renaming by its alias)
380 with the sought out name. If there is a match pass
381 current->import_src as NAMESPACE to direct the search
382 towards the imported namespace. */
383 if (current->declaration
384 && strcmp (name, current->alias
385 ? current->alias : current->declaration) == 0)
386 sym = cp_lookup_symbol_in_namespace (current->import_src,
387 current->declaration,
390 /* If this is a DECLARATION_ONLY search or a symbol was found
391 or this import statement was an import declaration, the
392 search of this import is complete. */
393 if (declaration_only || sym != NULL || current->declaration)
395 current->searched = 0;
396 discard_cleanups (searched_cleanup);
404 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
405 for (excludep = current->excludes; *excludep; excludep++)
406 if (strcmp (name, *excludep) == 0)
410 discard_cleanups (searched_cleanup);
414 if (current->alias != NULL
415 && strcmp (name, current->alias) == 0)
416 /* If the import is creating an alias and the alias matches
417 the sought name. Pass current->import_src as the NAME to
418 direct the search towards the aliased namespace. */
420 sym = cp_lookup_symbol_in_namespace (scope,
424 else if (current->alias == NULL)
426 /* If this import statement creates no alias, pass
427 current->inner as NAMESPACE to direct the search
428 towards the imported namespace. */
429 sym = cp_lookup_symbol_imports (current->import_src,
433 current->searched = 0;
434 discard_cleanups (searched_cleanup);
444 /* Helper function that searches an array of symbols for one named
447 static struct symbol *
448 search_symbol_list (const char *name, int num,
449 struct symbol **syms)
453 /* Maybe we should store a dictionary in here instead. */
454 for (i = 0; i < num; ++i)
456 if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
462 /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it
463 searches through the template parameters of the function and the
467 cp_lookup_symbol_imports_or_template (const char *scope,
469 const struct block *block,
470 const domain_enum domain)
472 struct symbol *function = BLOCK_FUNCTION (block);
474 if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
477 struct cplus_specific *cps
478 = function->ginfo.language_specific.cplus_specific;
480 /* Search the function's template parameters. */
481 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
483 struct template_symbol *templ
484 = (struct template_symbol *) function;
485 struct symbol *result;
487 result = search_symbol_list (name,
488 templ->n_template_arguments,
489 templ->template_arguments);
494 /* Search the template parameters of the function's defining
496 if (SYMBOL_NATURAL_NAME (function))
498 struct type *context;
499 char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
500 struct cleanup *cleanups = make_cleanup (xfree, name_copy);
501 const struct language_defn *lang = language_def (language_cplus);
502 struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch;
503 const struct block *parent = BLOCK_SUPERBLOCK (block);
507 struct symbol *result;
508 unsigned int prefix_len = cp_entire_prefix_len (name_copy);
514 name_copy[prefix_len] = '\0';
515 context = lookup_typename (lang, arch,
524 = search_symbol_list (name,
525 TYPE_N_TEMPLATE_ARGUMENTS (context),
526 TYPE_TEMPLATE_ARGUMENTS (context));
531 do_cleanups (cleanups);
535 return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1);
538 /* Searches for NAME in the current namespace, and by applying
539 relevant import statements belonging to BLOCK and its parents.
540 SCOPE is the namespace scope of the context in which the search is
544 cp_lookup_symbol_namespace (const char *scope,
546 const struct block *block,
547 const domain_enum domain)
551 /* First, try to find the symbol in the given namespace. */
552 sym = cp_lookup_symbol_in_namespace (scope, name,
557 /* Search for name in namespaces imported to this and parent
559 while (block != NULL)
561 sym = cp_lookup_symbol_imports (scope, name, block,
567 block = BLOCK_SUPERBLOCK (block);
573 /* Lookup NAME at namespace scope (or, in C terms, in static and
574 global variables). SCOPE is the namespace that the current
575 function is defined within; only consider namespaces whose length
576 is at least SCOPE_LEN. Other arguments are as in
577 cp_lookup_symbol_nonlocal.
579 For example, if we're within a function A::B::f and looking for a
580 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
581 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
582 but with SCOPE_LEN = 1. And then it calls itself with NAME and
583 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
584 "A::B::x"; if it doesn't find it, then the second call looks for
585 "A::x", and if that call fails, then the first call looks for
588 static struct symbol *
589 lookup_namespace_scope (const char *name,
590 const struct block *block,
591 const domain_enum domain,
597 if (scope[scope_len] != '\0')
599 /* Recursively search for names in child namespaces first. */
602 int new_scope_len = scope_len;
604 /* If the current scope is followed by "::", skip past that. */
605 if (new_scope_len != 0)
607 gdb_assert (scope[new_scope_len] == ':');
610 new_scope_len += cp_find_first_component (scope + new_scope_len);
611 sym = lookup_namespace_scope (name, block, domain,
612 scope, new_scope_len);
617 /* Okay, we didn't find a match in our children, so look for the
618 name in the current namespace. */
620 namespace = alloca (scope_len + 1);
621 strncpy (namespace, scope, scope_len);
622 namespace[scope_len] = '\0';
623 return cp_lookup_symbol_in_namespace (namespace, name,
627 /* Look up NAME in BLOCK's static block and in global blocks. If
628 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
629 within an anonymous namespace. Other arguments are as in
630 cp_lookup_symbol_nonlocal. */
632 static struct symbol *
633 lookup_symbol_file (const char *name,
634 const struct block *block,
635 const domain_enum domain,
636 int anonymous_namespace)
638 struct symbol *sym = NULL;
640 sym = lookup_symbol_static (name, block, domain);
644 if (anonymous_namespace)
646 /* Symbols defined in anonymous namespaces have external linkage
647 but should be treated as local to a single file nonetheless.
648 So we only search the current file's global block. */
650 const struct block *global_block = block_global_block (block);
652 if (global_block != NULL)
653 sym = lookup_symbol_aux_block (name, global_block, domain);
657 sym = lookup_symbol_global (name, block, domain);
663 /* Look up a symbol named NESTED_NAME that is nested inside the C++
664 class or namespace given by PARENT_TYPE, from within the context
665 given by BLOCK. Return NULL if there is no such nested type. */
668 cp_lookup_nested_symbol (struct type *parent_type,
669 const char *nested_name,
670 const struct block *block)
672 /* type_name_no_tag_required provides better error reporting using the
674 struct type *saved_parent_type = parent_type;
676 CHECK_TYPEDEF (parent_type);
678 switch (TYPE_CODE (parent_type))
680 case TYPE_CODE_STRUCT:
681 case TYPE_CODE_NAMESPACE:
682 case TYPE_CODE_UNION:
684 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
685 of classes like, say, data or function members. Instead,
686 they're just represented by symbols whose names are
687 qualified by the name of the surrounding class. This is
688 just like members of namespaces; in particular,
689 lookup_symbol_namespace works when looking them up. */
691 const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
693 = cp_lookup_symbol_in_namespace (parent_name, nested_name,
695 char *concatenated_name;
700 /* Now search all static file-level symbols. Not strictly
701 correct, but more useful than an error. We do not try to
702 guess any imported namespace as even the fully specified
703 namespace seach is is already not C++ compliant and more
704 assumptions could make it too magic. */
706 concatenated_name = alloca (strlen (parent_name) + 2
707 + strlen (nested_name) + 1);
708 sprintf (concatenated_name, "%s::%s",
709 parent_name, nested_name);
710 sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
717 internal_error (__FILE__, __LINE__,
718 _("cp_lookup_nested_symbol called "
719 "on a non-aggregate type."));
723 /* The C++-version of lookup_transparent_type. */
725 /* FIXME: carlton/2004-01-16: The problem that this is trying to
726 address is that, unfortunately, sometimes NAME is wrong: it may not
727 include the name of namespaces enclosing the type in question.
728 lookup_transparent_type gets called when the type in question
729 is a declaration, and we're trying to find its definition; but, for
730 declarations, our type name deduction mechanism doesn't work.
731 There's nothing we can do to fix this in general, I think, in the
732 absence of debug information about namespaces (I've filed PR
733 gdb/1511 about this); until such debug information becomes more
734 prevalent, one heuristic which sometimes looks is to search for the
735 definition in namespaces containing the current namespace.
737 We should delete this functions once the appropriate debug
738 information becomes more widespread. (GCC 3.4 will be the first
739 released version of GCC with such information.) */
742 cp_lookup_transparent_type (const char *name)
744 /* First, try the honest way of looking up the definition. */
745 struct type *t = basic_lookup_transparent_type (name);
751 /* If that doesn't work and we're within a namespace, look there
753 scope = block_scope (get_selected_block (0));
755 if (scope[0] == '\0')
758 return cp_lookup_transparent_type_loop (name, scope, 0);
761 /* Lookup the type definition associated to NAME in namespaces/classes
762 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
763 must be the index of the start of a component of SCOPE. */
766 cp_lookup_transparent_type_loop (const char *name,
770 int scope_length = length + cp_find_first_component (scope + length);
773 /* If the current scope is followed by "::", look in the next
775 if (scope[scope_length] == ':')
778 = cp_lookup_transparent_type_loop (name, scope,
785 full_name = alloca (scope_length + 2 + strlen (name) + 1);
786 strncpy (full_name, scope, scope_length);
787 strncpy (full_name + scope_length, "::", 2);
788 strcpy (full_name + scope_length + 2, name);
790 return basic_lookup_transparent_type (full_name);
793 /* This used to do something but was removed when it became
797 maintenance_cplus_namespace (char *args, int from_tty)
799 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
802 /* Provide a prototype to silence -Wmissing-prototypes. */
803 extern initialize_file_ftype _initialize_cp_namespace;
806 _initialize_cp_namespace (void)
808 struct cmd_list_element *cmd;
810 cmd = add_cmd ("namespace", class_maintenance,
811 maintenance_cplus_namespace,
812 _("Deprecated placeholder for removed functionality."),
813 &maint_cplus_cmd_list);
814 deprecate_cmd (cmd, NULL);