1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
5 Contributed by David Carlton and by Kealia, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "cp-support.h"
24 #include "gdb_obstack.h"
27 #include "gdb_assert.h"
31 #include "dictionary.h"
36 static struct symbol *lookup_namespace_scope (const char *name,
37 const char *linkage_name,
38 const struct block *block,
39 const domain_enum domain,
43 static struct symbol *lookup_symbol_file (const char *name,
44 const char *linkage_name,
45 const struct block *block,
46 const domain_enum domain,
47 int anonymous_namespace);
49 static struct type *cp_lookup_transparent_type_loop (const char *name,
53 static void initialize_namespace_symtab (struct objfile *objfile);
55 static struct block *get_possible_namespace_block (struct objfile *objfile);
57 static void free_namespace_block (struct symtab *symtab);
59 static int check_possible_namespace_symbols_loop (const char *name,
61 struct objfile *objfile);
63 static int check_one_possible_namespace_symbol (const char *name,
65 struct objfile *objfile);
67 static struct symbol *lookup_possible_namespace_symbol (const char *name);
69 static void maintenance_cplus_namespace (char *args, int from_tty);
71 /* Check to see if SYMBOL refers to an object contained within an
72 anonymous namespace; if so, add an appropriate using directive. */
74 /* Optimize away strlen ("(anonymous namespace)"). */
76 #define ANONYMOUS_NAMESPACE_LEN 21
79 cp_scan_for_anonymous_namespaces (const struct symbol *symbol)
81 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
83 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
84 unsigned int previous_component;
85 unsigned int next_component;
88 /* Start with a quick-and-dirty check for mention of "(anonymous
91 if (!cp_is_anonymous (name))
94 previous_component = 0;
95 next_component = cp_find_first_component (name + previous_component);
97 while (name[next_component] == ':')
99 if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
100 && strncmp (name + previous_component,
101 "(anonymous namespace)",
102 ANONYMOUS_NAMESPACE_LEN) == 0)
104 int dest_len = (previous_component == 0 ? 0 : previous_component - 2);
105 int src_len = next_component;
107 char *dest = alloca (dest_len + 1);
108 char *src = alloca (src_len + 1);
110 memcpy (dest, name, dest_len);
111 memcpy (src, name, src_len);
113 dest[dest_len] = '\0';
116 /* We've found a component of the name that's an
117 anonymous namespace. So add symbols in it to the
118 namespace given by the previous component if there is
119 one, or to the global namespace if there isn't. */
120 cp_add_using_directive (dest, src, NULL);
122 /* The "+ 2" is for the "::". */
123 previous_component = next_component + 2;
124 next_component = (previous_component
125 + cp_find_first_component (name
126 + previous_component));
131 /* Add a using directive to using_list. If the using directive in question
132 has already been added, don't add it twice. */
135 cp_add_using_directive (const char *dest, const char *src, const char *alias)
137 struct using_direct *current;
138 struct using_direct *new;
140 /* Has it already been added? */
142 for (current = using_directives; current != NULL; current = current->next)
144 if (strcmp (current->import_src, src) == 0
145 && strcmp (current->import_dest, dest) == 0)
149 using_directives = cp_add_using (dest, src, alias, using_directives);
153 /* Record the namespace that the function defined by SYMBOL was
154 defined in, if necessary. BLOCK is the associated block; use
155 OBSTACK for allocation. */
158 cp_set_block_scope (const struct symbol *symbol,
160 struct obstack *obstack,
161 const char *processing_current_prefix,
162 int processing_has_namespace_info)
164 if (processing_has_namespace_info)
167 (block, obsavestring (processing_current_prefix,
168 strlen (processing_current_prefix),
172 else if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
174 /* Try to figure out the appropriate namespace from the
177 /* FIXME: carlton/2003-04-15: If the function in question is
178 a method of a class, the name will actually include the
179 name of the class as well. This should be harmless, but
180 is a little unfortunate. */
182 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
183 unsigned int prefix_len = cp_entire_prefix_len (name);
185 block_set_scope (block,
186 obsavestring (name, prefix_len, obstack),
191 /* Test whether or not NAMESPACE looks like it mentions an anonymous
192 namespace; return nonzero if so. */
195 cp_is_anonymous (const char *namespace)
197 return (strstr (namespace, "(anonymous namespace)")
201 /* Create a new struct using direct which imports the namespace SRC into the
202 scope DEST. ALIAS is the name of the imported namespace in the current
203 scope. If ALIAS is NULL then the namespace is known by its original name.
204 Set its next member in the linked list to NEXT; allocate all memory
205 using xmalloc. It copies the strings, so NAME can be a temporary
208 struct using_direct *
209 cp_add_using (const char *dest,
212 struct using_direct *next)
214 struct using_direct *retval;
216 retval = xmalloc (sizeof (struct using_direct));
217 retval->import_src = savestring (src, strlen(src));
218 retval->import_dest = savestring (dest, strlen(dest));
221 retval->alias = savestring (alias, strlen (alias));
223 retval->alias = NULL;
226 retval->searched = 0;
231 /* The C++-specific version of name lookup for static and global
232 names. This makes sure that names get looked for in all namespaces
233 that are in scope. NAME is the natural name of the symbol that
234 we're looking for, LINKAGE_NAME (which is optional) is its linkage
235 name, BLOCK is the block that we're searching within, DOMAIN says
236 what kind of symbols we're looking for, and if SYMTAB is non-NULL,
237 we should store the symtab where we found the symbol in it. */
240 cp_lookup_symbol_nonlocal (const char *name,
241 const char *linkage_name,
242 const struct block *block,
243 const domain_enum domain)
246 const char *scope = block_scope (block);
248 sym = lookup_namespace_scope (name, linkage_name, block, domain, scope, 0);
252 return cp_lookup_symbol_namespace (scope, name, linkage_name, block, domain,
256 /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are as in
257 cp_lookup_symbol_nonlocal. */
259 static struct symbol *
260 cp_lookup_symbol_in_namespace (const char *namespace,
262 const char *linkage_name,
263 const struct block *block,
264 const domain_enum domain)
266 if (namespace[0] == '\0')
268 return lookup_symbol_file (name, linkage_name, block,
273 char *concatenated_name = alloca (strlen (namespace) + 2 +
275 strcpy (concatenated_name, namespace);
276 strcat (concatenated_name, "::");
277 strcat (concatenated_name, name);
278 return lookup_symbol_file (concatenated_name, linkage_name,
279 block, domain,cp_is_anonymous (namespace));
283 /* Used for cleanups to reset the "searched" flag incase
287 reset_directive_searched (void *data)
289 struct using_direct *direct = data;
290 direct->searched = 0;
293 /* Search for NAME by applying all import statements belonging
294 to BLOCK which are applicable in SCOPE.
295 If SEARCH_PARENTS the search will include imports which are applicable in
306 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of namespaces X
307 and Y will be considered. If SEARCH_PARENTS is false only the import of Y
310 static struct symbol *
311 cp_lookup_symbol_imports (const char *scope,
313 const char *linkage_name,
314 const struct block *block,
315 const domain_enum domain,
316 const int search_parents)
318 struct using_direct *current;
322 struct cleanup *searched_cleanup;
324 /* First, try to find the symbol in the given namespace. */
325 sym = cp_lookup_symbol_in_namespace (scope, name, linkage_name, block,
330 /* Go through the using directives. If any of them add new
331 names to the namespace we're searching in, see if we can find a
332 match by applying them. */
334 for (current = block_using (block);
336 current = current->next)
338 len = strlen (current->import_dest);
339 directive_match = (search_parents
340 ? (strncmp (scope, current->import_dest,
341 strlen (current->import_dest)) == 0
343 || scope[len] == ':' || scope[len] == '\0'))
344 : strcmp (scope, current->import_dest) == 0);
346 /* If the import destination is the current scope or one of its ancestors then
348 if (directive_match && !current->searched)
350 /* Mark this import as searched so that the recursive call does not
352 current->searched = 1;
353 searched_cleanup = make_cleanup (reset_directive_searched, current);
355 if (current->alias != NULL && strcmp (name, current->alias) == 0)
356 /* If the import is creating an alias and the alias matches the
357 sought name. Pass current->import_src as the NAME to direct the
358 search towards the aliased namespace. */
360 sym = cp_lookup_symbol_in_namespace (scope,
366 else if (current->alias == NULL)
368 /* If this import statement creates no alias, pass current->inner as
369 NAMESPACE to direct the search towards the imported namespace. */
370 sym = cp_lookup_symbol_imports (current->import_src,
377 current->searched = 0;
378 discard_cleanups (searched_cleanup);
388 /* Searches for NAME in the current namespace, and by applying relevant import
389 statements belonging to BLOCK and its parents. SCOPE is the namespace scope
390 of the context in which the search is being evaluated. */
393 cp_lookup_symbol_namespace (const char *scope,
395 const char *linkage_name,
396 const struct block *block,
397 const domain_enum domain,
398 const int search_parents)
402 /* Search for name in namespaces imported to this and parent blocks. */
403 while (block != NULL)
405 sym = cp_lookup_symbol_imports (scope, name, linkage_name, block, domain,
411 block = BLOCK_SUPERBLOCK (block);
417 /* Lookup NAME at namespace scope (or, in C terms, in static and
418 global variables). SCOPE is the namespace that the current
419 function is defined within; only consider namespaces whose length
420 is at least SCOPE_LEN. Other arguments are as in
421 cp_lookup_symbol_nonlocal.
423 For example, if we're within a function A::B::f and looking for a
424 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
425 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
426 but with SCOPE_LEN = 1. And then it calls itself with NAME and
427 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
428 "A::B::x"; if it doesn't find it, then the second call looks for
429 "A::x", and if that call fails, then the first call looks for
432 static struct symbol *
433 lookup_namespace_scope (const char *name,
434 const char *linkage_name,
435 const struct block *block,
436 const domain_enum domain,
442 if (scope[scope_len] != '\0')
444 /* Recursively search for names in child namespaces first. */
447 int new_scope_len = scope_len;
449 /* If the current scope is followed by "::", skip past that. */
450 if (new_scope_len != 0)
452 gdb_assert (scope[new_scope_len] == ':');
455 new_scope_len += cp_find_first_component (scope + new_scope_len);
456 sym = lookup_namespace_scope (name, linkage_name, block,
457 domain, scope, new_scope_len);
462 /* Okay, we didn't find a match in our children, so look for the
463 name in the current namespace. */
465 namespace = alloca (scope_len + 1);
466 strncpy (namespace, scope, scope_len);
467 namespace[scope_len] = '\0';
468 return cp_lookup_symbol_in_namespace (namespace, name, linkage_name,
472 /* Look up NAME in BLOCK's static block and in global blocks. If
473 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
474 within an anonymous namespace. Other arguments are as in
475 cp_lookup_symbol_nonlocal. */
477 static struct symbol *
478 lookup_symbol_file (const char *name,
479 const char *linkage_name,
480 const struct block *block,
481 const domain_enum domain,
482 int anonymous_namespace)
484 struct symbol *sym = NULL;
486 sym = lookup_symbol_static (name, linkage_name, block, domain);
490 if (anonymous_namespace)
492 /* Symbols defined in anonymous namespaces have external linkage
493 but should be treated as local to a single file nonetheless.
494 So we only search the current file's global block. */
496 const struct block *global_block = block_global_block (block);
498 if (global_block != NULL)
499 sym = lookup_symbol_aux_block (name, linkage_name, global_block,
504 sym = lookup_symbol_global (name, linkage_name, block, domain);
510 /* Now call "lookup_possible_namespace_symbol". Symbols in here
511 claim to be associated to namespaces, but this claim might be
512 incorrect: the names in question might actually correspond to
513 classes instead of namespaces. But if they correspond to
514 classes, then we should have found a match for them above. So if
515 we find them now, they should be genuine. */
517 /* FIXME: carlton/2003-06-12: This is a hack and should eventually
518 be deleted: see comments below. */
520 if (domain == VAR_DOMAIN)
522 sym = lookup_possible_namespace_symbol (name);
530 /* Look up a type named NESTED_NAME that is nested inside the C++
531 class or namespace given by PARENT_TYPE, from within the context
532 given by BLOCK. Return NULL if there is no such nested type. */
535 cp_lookup_nested_type (struct type *parent_type,
536 const char *nested_name,
537 const struct block *block)
539 switch (TYPE_CODE (parent_type))
541 case TYPE_CODE_STRUCT:
542 case TYPE_CODE_NAMESPACE:
544 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
545 of classes like, say, data or function members. Instead,
546 they're just represented by symbols whose names are
547 qualified by the name of the surrounding class. This is
548 just like members of namespaces; in particular,
549 lookup_symbol_namespace works when looking them up. */
551 const char *parent_name = TYPE_TAG_NAME (parent_type);
552 struct symbol *sym = cp_lookup_symbol_in_namespace (parent_name,
557 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
560 return SYMBOL_TYPE (sym);
563 internal_error (__FILE__, __LINE__,
564 _("cp_lookup_nested_type called on a non-aggregate type."));
568 /* The C++-version of lookup_transparent_type. */
570 /* FIXME: carlton/2004-01-16: The problem that this is trying to
571 address is that, unfortunately, sometimes NAME is wrong: it may not
572 include the name of namespaces enclosing the type in question.
573 lookup_transparent_type gets called when the the type in question
574 is a declaration, and we're trying to find its definition; but, for
575 declarations, our type name deduction mechanism doesn't work.
576 There's nothing we can do to fix this in general, I think, in the
577 absence of debug information about namespaces (I've filed PR
578 gdb/1511 about this); until such debug information becomes more
579 prevalent, one heuristic which sometimes looks is to search for the
580 definition in namespaces containing the current namespace.
582 We should delete this functions once the appropriate debug
583 information becomes more widespread. (GCC 3.4 will be the first
584 released version of GCC with such information.) */
587 cp_lookup_transparent_type (const char *name)
589 /* First, try the honest way of looking up the definition. */
590 struct type *t = basic_lookup_transparent_type (name);
596 /* If that doesn't work and we're within a namespace, look there
598 scope = block_scope (get_selected_block (0));
600 if (scope[0] == '\0')
603 return cp_lookup_transparent_type_loop (name, scope, 0);
606 /* Lookup the the type definition associated to NAME in
607 namespaces/classes containing SCOPE whose name is strictly longer
608 than LENGTH. LENGTH must be the index of the start of a
609 component of SCOPE. */
612 cp_lookup_transparent_type_loop (const char *name, const char *scope,
615 int scope_length = length + cp_find_first_component (scope + length);
618 /* If the current scope is followed by "::", look in the next
620 if (scope[scope_length] == ':')
623 = cp_lookup_transparent_type_loop (name, scope, scope_length + 2);
628 full_name = alloca (scope_length + 2 + strlen (name) + 1);
629 strncpy (full_name, scope, scope_length);
630 strncpy (full_name + scope_length, "::", 2);
631 strcpy (full_name + scope_length + 2, name);
633 return basic_lookup_transparent_type (full_name);
636 /* Now come functions for dealing with symbols associated to
637 namespaces. (They're used to store the namespaces themselves, not
638 objects that live in the namespaces.) These symbols come in two
639 varieties: if we run into a DW_TAG_namespace DIE, then we know that
640 we have a namespace, so dwarf2read.c creates a symbol for it just
641 like normal. But, unfortunately, versions of GCC through at least
642 3.3 don't generate those DIE's. Our solution is to try to guess
643 their existence by looking at demangled names. This might cause us
644 to misidentify classes as namespaces, however. So we put those
645 symbols in a special block (one per objfile), and we only search
646 that block as a last resort. */
648 /* FIXME: carlton/2003-06-12: Once versions of GCC that generate
649 DW_TAG_namespace have been out for a year or two, we should get rid
650 of all of this "possible namespace" nonsense. */
652 /* Allocate everything necessary for the possible namespace block
653 associated to OBJFILE. */
656 initialize_namespace_symtab (struct objfile *objfile)
658 struct symtab *namespace_symtab;
659 struct blockvector *bv;
662 namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile);
663 namespace_symtab->language = language_cplus;
664 namespace_symtab->free_code = free_nothing;
665 namespace_symtab->dirname = NULL;
667 bv = obstack_alloc (&objfile->objfile_obstack,
668 sizeof (struct blockvector)
669 + FIRST_LOCAL_BLOCK * sizeof (struct block *));
670 BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1;
671 BLOCKVECTOR (namespace_symtab) = bv;
673 /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */
675 bl = allocate_block (&objfile->objfile_obstack);
676 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
678 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
679 bl = allocate_block (&objfile->objfile_obstack);
680 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
682 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
684 /* Allocate the possible namespace block; we put it where the first
685 local block will live, though I don't think there's any need to
686 pretend that it's actually a local block (e.g. by setting
687 BLOCK_SUPERBLOCK appropriately). We don't use the global or
688 static block because we don't want it searched during the normal
689 search of all global/static blocks in lookup_symbol: we only want
690 it used as a last resort. */
692 /* NOTE: carlton/2003-09-11: I considered not associating the fake
693 symbols to a block/symtab at all. But that would cause problems
694 with lookup_symbol's SYMTAB argument and with block_found, so
695 having a symtab/block for this purpose seems like the best
698 bl = allocate_block (&objfile->objfile_obstack);
699 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
700 BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl;
702 namespace_symtab->free_func = free_namespace_block;
704 objfile->cp_namespace_symtab = namespace_symtab;
707 /* Locate the possible namespace block associated to OBJFILE,
708 allocating it if necessary. */
710 static struct block *
711 get_possible_namespace_block (struct objfile *objfile)
713 if (objfile->cp_namespace_symtab == NULL)
714 initialize_namespace_symtab (objfile);
716 return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab),
720 /* Free the dictionary associated to the possible namespace block. */
723 free_namespace_block (struct symtab *symtab)
725 struct block *possible_namespace_block;
727 possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
729 gdb_assert (possible_namespace_block != NULL);
730 dict_free (BLOCK_DICT (possible_namespace_block));
733 /* Ensure that there are symbols in the possible namespace block
734 associated to OBJFILE for all initial substrings of NAME that look
735 like namespaces or classes. NAME should end in a member variable:
736 it shouldn't consist solely of namespaces. */
739 cp_check_possible_namespace_symbols (const char *name, struct objfile *objfile)
741 check_possible_namespace_symbols_loop (name,
742 cp_find_first_component (name),
746 /* This is a helper loop for cp_check_possible_namespace_symbols; it
747 ensures that there are symbols in the possible namespace block
748 associated to OBJFILE for all namespaces that are initial
749 substrings of NAME of length at least LEN. It returns 1 if a
750 previous loop had already created the shortest such symbol and 0
753 This function assumes that if there is already a symbol associated
754 to a substring of NAME of a given length, then there are already
755 symbols associated to all substrings of NAME whose length is less
756 than that length. So if cp_check_possible_namespace_symbols has
757 been called once with argument "A::B::C::member", then that will
758 create symbols "A", "A::B", and "A::B::C". If it is then later
759 called with argument "A::B::D::member", then the new call will
760 generate a new symbol for "A::B::D", but once it sees that "A::B"
761 has already been created, it doesn't bother checking to see if "A"
762 has also been created. */
765 check_possible_namespace_symbols_loop (const char *name, int len,
766 struct objfile *objfile)
768 if (name[len] == ':')
771 int next_len = len + 2;
773 next_len += cp_find_first_component (name + next_len);
774 done = check_possible_namespace_symbols_loop (name, next_len,
778 done = check_one_possible_namespace_symbol (name, len, objfile);
786 /* Check to see if there's already a possible namespace symbol in
787 OBJFILE whose name is the initial substring of NAME of length LEN.
788 If not, create one and return 0; otherwise, return 1. */
791 check_one_possible_namespace_symbol (const char *name, int len,
792 struct objfile *objfile)
794 struct block *block = get_possible_namespace_block (objfile);
795 char *name_copy = alloca (len + 1);
798 memcpy (name_copy, name, len);
799 name_copy[len] = '\0';
800 sym = lookup_block_symbol (block, name_copy, NULL, VAR_DOMAIN);
806 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, name_copy, objfile);
808 TYPE_TAG_NAME (type) = TYPE_NAME (type);
810 sym = obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol));
811 memset (sym, 0, sizeof (struct symbol));
812 SYMBOL_LANGUAGE (sym) = language_cplus;
813 /* Note that init_type copied the name to the objfile's
815 SYMBOL_SET_NAMES (sym, TYPE_NAME (type), len, 0, objfile);
816 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
817 SYMBOL_TYPE (sym) = type;
818 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
820 dict_add_symbol (BLOCK_DICT (block), sym);
828 /* Look for a symbol named NAME in all the possible namespace blocks.
829 If one is found, return it. */
831 static struct symbol *
832 lookup_possible_namespace_symbol (const char *name)
834 struct objfile *objfile;
836 ALL_OBJFILES (objfile)
840 sym = lookup_block_symbol (get_possible_namespace_block (objfile),
841 name, NULL, VAR_DOMAIN);
850 /* Print out all the possible namespace symbols. */
853 maintenance_cplus_namespace (char *args, int from_tty)
855 struct objfile *objfile;
856 printf_unfiltered (_("Possible namespaces:\n"));
857 ALL_OBJFILES (objfile)
859 struct dict_iterator iter;
862 ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym)
864 printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym));
869 /* Provide a prototype to silence -Wmissing-prototypes. */
870 extern initialize_file_ftype _initialize_cp_namespace;
873 _initialize_cp_namespace (void)
875 add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace,
876 _("Print the list of possible C++ namespaces."),
877 &maint_cplus_cmd_list);