1 /* Helper routines for C++ support in GDB.
2 Copyright 2003, 2004 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 2 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, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "cp-support.h"
25 #include "gdb_obstack.h"
28 #include "gdb_assert.h"
32 #include "dictionary.h"
36 /* When set, the file that we're processing is known to have debugging
37 info for C++ namespaces. */
39 /* NOTE: carlton/2004-01-13: No currently released version of GCC (the
40 latest of which is 3.3.x at the time of this writing) produces this
41 debug info. GCC 3.4 should, however. */
43 unsigned char processing_has_namespace_info;
45 /* This contains our best guess as to the name of the current
46 enclosing namespace(s)/class(es), if any. For example, if we're
47 within the method foo() in the following code:
56 then processing_current_prefix should be set to "N::C". If
57 processing_has_namespace_info is false, then this variable might
60 const char *processing_current_prefix;
62 /* List of using directives that are active in the current file. */
64 static struct using_direct *using_list;
66 static struct using_direct *cp_add_using (const char *name,
67 unsigned int inner_len,
68 unsigned int outer_len,
69 struct using_direct *next);
71 static struct using_direct *cp_copy_usings (struct using_direct *using,
72 struct obstack *obstack);
74 static struct symbol *lookup_namespace_scope (const char *name,
75 const char *linkage_name,
76 const struct block *block,
77 const domain_enum domain,
78 struct symtab **symtab,
82 static struct symbol *lookup_symbol_file (const char *name,
83 const char *linkage_name,
84 const struct block *block,
85 const domain_enum domain,
86 struct symtab **symtab,
87 int anonymous_namespace);
89 static struct type *cp_lookup_transparent_type_loop (const char *name,
93 static void initialize_namespace_symtab (struct objfile *objfile);
95 static struct block *get_possible_namespace_block (struct objfile *objfile);
97 static void free_namespace_block (struct symtab *symtab);
99 static int check_possible_namespace_symbols_loop (const char *name,
101 struct objfile *objfile);
103 static int check_one_possible_namespace_symbol (const char *name,
105 struct objfile *objfile);
108 struct symbol *lookup_possible_namespace_symbol (const char *name,
109 struct symtab **symtab);
111 static void maintenance_cplus_namespace (char *args, int from_tty);
113 /* Set up support for dealing with C++ namespace info in the current
116 void cp_initialize_namespace ()
118 processing_has_namespace_info = 0;
122 /* Add all the using directives we've gathered to the current symtab.
123 STATIC_BLOCK should be the symtab's static block; OBSTACK is used
127 cp_finalize_namespace (struct block *static_block,
128 struct obstack *obstack)
130 if (using_list != NULL)
132 block_set_using (static_block,
133 cp_copy_usings (using_list, obstack),
139 /* Check to see if SYMBOL refers to an object contained within an
140 anonymous namespace; if so, add an appropriate using directive. */
142 /* Optimize away strlen ("(anonymous namespace)"). */
144 #define ANONYMOUS_NAMESPACE_LEN 21
147 cp_scan_for_anonymous_namespaces (const struct symbol *symbol)
149 if (!processing_has_namespace_info
150 && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
152 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
153 unsigned int previous_component;
154 unsigned int next_component;
157 /* Start with a quick-and-dirty check for mention of "(anonymous
160 if (!cp_is_anonymous (name))
163 previous_component = 0;
164 next_component = cp_find_first_component (name + previous_component);
166 while (name[next_component] == ':')
168 if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
169 && strncmp (name + previous_component,
170 "(anonymous namespace)",
171 ANONYMOUS_NAMESPACE_LEN) == 0)
173 /* We've found a component of the name that's an
174 anonymous namespace. So add symbols in it to the
175 namespace given by the previous component if there is
176 one, or to the global namespace if there isn't. */
177 cp_add_using_directive (name,
178 previous_component == 0
179 ? 0 : previous_component - 2,
182 /* The "+ 2" is for the "::". */
183 previous_component = next_component + 2;
184 next_component = (previous_component
185 + cp_find_first_component (name
186 + previous_component));
191 /* Add a using directive to using_list. NAME is the start of a string
192 that should contain the namespaces we want to add as initial
193 substrings, OUTER_LENGTH is the end of the outer namespace, and
194 INNER_LENGTH is the end of the inner namespace. If the using
195 directive in question has already been added, don't add it
199 cp_add_using_directive (const char *name, unsigned int outer_length,
200 unsigned int inner_length)
202 struct using_direct *current;
203 struct using_direct *new;
205 /* Has it already been added? */
207 for (current = using_list; current != NULL; current = current->next)
209 if ((strncmp (current->inner, name, inner_length) == 0)
210 && (strlen (current->inner) == inner_length)
211 && (strlen (current->outer) == outer_length))
215 using_list = cp_add_using (name, inner_length, outer_length,
219 /* Record the namespace that the function defined by SYMBOL was
220 defined in, if necessary. BLOCK is the associated block; use
221 OBSTACK for allocation. */
224 cp_set_block_scope (const struct symbol *symbol,
226 struct obstack *obstack)
228 /* Make sure that the name was originally mangled: if not, there
229 certainly isn't any namespace information to worry about! */
231 if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
233 if (processing_has_namespace_info)
236 (block, obsavestring (processing_current_prefix,
237 strlen (processing_current_prefix),
243 /* Try to figure out the appropriate namespace from the
246 /* FIXME: carlton/2003-04-15: If the function in question is
247 a method of a class, the name will actually include the
248 name of the class as well. This should be harmless, but
249 is a little unfortunate. */
251 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
252 unsigned int prefix_len = cp_entire_prefix_len (name);
254 block_set_scope (block,
255 obsavestring (name, prefix_len, obstack),
261 /* Test whether or not NAMESPACE looks like it mentions an anonymous
262 namespace; return nonzero if so. */
265 cp_is_anonymous (const char *namespace)
267 return (strstr (namespace, "(anonymous namespace)")
271 /* Create a new struct using direct whose inner namespace is the
272 initial substring of NAME of leng INNER_LEN and whose outer
273 namespace is the initial substring of NAME of length OUTER_LENGTH.
274 Set its next member in the linked list to NEXT; allocate all memory
275 using xmalloc. It copies the strings, so NAME can be a temporary
278 static struct using_direct *
279 cp_add_using (const char *name,
280 unsigned int inner_len,
281 unsigned int outer_len,
282 struct using_direct *next)
284 struct using_direct *retval;
286 gdb_assert (outer_len < inner_len);
288 retval = xmalloc (sizeof (struct using_direct));
289 retval->inner = savestring (name, inner_len);
290 retval->outer = savestring (name, outer_len);
296 /* Make a copy of the using directives in the list pointed to by
297 USING, using OBSTACK to allocate memory. Free all memory pointed
298 to by USING via xfree. */
300 static struct using_direct *
301 cp_copy_usings (struct using_direct *using,
302 struct obstack *obstack)
310 struct using_direct *retval
311 = obstack_alloc (obstack, sizeof (struct using_direct));
312 retval->inner = obsavestring (using->inner, strlen (using->inner),
314 retval->outer = obsavestring (using->outer, strlen (using->outer),
316 retval->next = cp_copy_usings (using->next, obstack);
318 xfree (using->inner);
319 xfree (using->outer);
326 /* The C++-specific version of name lookup for static and global
327 names. This makes sure that names get looked for in all namespaces
328 that are in scope. NAME is the natural name of the symbol that
329 we're looking for, LINKAGE_NAME (which is optional) is its linkage
330 name, BLOCK is the block that we're searching within, DOMAIN says
331 what kind of symbols we're looking for, and if SYMTAB is non-NULL,
332 we should store the symtab where we found the symbol in it. */
335 cp_lookup_symbol_nonlocal (const char *name,
336 const char *linkage_name,
337 const struct block *block,
338 const domain_enum domain,
339 struct symtab **symtab)
341 return lookup_namespace_scope (name, linkage_name, block, domain,
342 symtab, block_scope (block), 0);
345 /* Lookup NAME at namespace scope (or, in C terms, in static and
346 global variables). SCOPE is the namespace that the current
347 function is defined within; only consider namespaces whose length
348 is at least SCOPE_LEN. Other arguments are as in
349 cp_lookup_symbol_nonlocal.
351 For example, if we're within a function A::B::f and looking for a
352 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
353 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
354 but with SCOPE_LEN = 1. And then it calls itself with NAME and
355 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
356 "A::B::x"; if it doesn't find it, then the second call looks for
357 "A::x", and if that call fails, then the first call looks for
360 static struct symbol *
361 lookup_namespace_scope (const char *name,
362 const char *linkage_name,
363 const struct block *block,
364 const domain_enum domain,
365 struct symtab **symtab,
371 if (scope[scope_len] != '\0')
373 /* Recursively search for names in child namespaces first. */
376 int new_scope_len = scope_len;
378 /* If the current scope is followed by "::", skip past that. */
379 if (new_scope_len != 0)
381 gdb_assert (scope[new_scope_len] == ':');
384 new_scope_len += cp_find_first_component (scope + new_scope_len);
385 sym = lookup_namespace_scope (name, linkage_name, block,
387 scope, new_scope_len);
392 /* Okay, we didn't find a match in our children, so look for the
393 name in the current namespace. */
395 namespace = alloca (scope_len + 1);
396 strncpy (namespace, scope, scope_len);
397 namespace[scope_len] = '\0';
398 return cp_lookup_symbol_namespace (namespace, name, linkage_name,
399 block, domain, symtab);
402 /* Look up NAME in the C++ namespace NAMESPACE, applying the using
403 directives that are active in BLOCK. Other arguments are as in
404 cp_lookup_symbol_nonlocal. */
407 cp_lookup_symbol_namespace (const char *namespace,
409 const char *linkage_name,
410 const struct block *block,
411 const domain_enum domain,
412 struct symtab **symtab)
414 const struct using_direct *current;
417 /* First, go through the using directives. If any of them add new
418 names to the namespace we're searching in, see if we can find a
419 match by applying them. */
421 for (current = block_using (block);
423 current = current->next)
425 if (strcmp (namespace, current->outer) == 0)
427 sym = cp_lookup_symbol_namespace (current->inner,
438 /* We didn't find anything by applying any of the using directives
439 that are still applicable; so let's see if we've got a match
440 using the current namespace. */
442 if (namespace[0] == '\0')
444 return lookup_symbol_file (name, linkage_name, block,
449 char *concatenated_name
450 = alloca (strlen (namespace) + 2 + strlen (name) + 1);
451 strcpy (concatenated_name, namespace);
452 strcat (concatenated_name, "::");
453 strcat (concatenated_name, name);
454 sym = lookup_symbol_file (concatenated_name, linkage_name,
455 block, domain, symtab,
456 cp_is_anonymous (namespace));
461 /* Look up NAME in BLOCK's static block and in global blocks. If
462 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
463 within an anonymous namespace. Other arguments are as in
464 cp_lookup_symbol_nonlocal. */
466 static struct symbol *
467 lookup_symbol_file (const char *name,
468 const char *linkage_name,
469 const struct block *block,
470 const domain_enum domain,
471 struct symtab **symtab,
472 int anonymous_namespace)
474 struct symbol *sym = NULL;
476 sym = lookup_symbol_static (name, linkage_name, block, domain, symtab);
480 if (anonymous_namespace)
482 /* Symbols defined in anonymous namespaces have external linkage
483 but should be treated as local to a single file nonetheless.
484 So we only search the current file's global block. */
486 const struct block *global_block = block_global_block (block);
488 if (global_block != NULL)
489 sym = lookup_symbol_aux_block (name, linkage_name, global_block,
494 sym = lookup_symbol_global (name, linkage_name, domain, symtab);
500 /* Now call "lookup_possible_namespace_symbol". Symbols in here
501 claim to be associated to namespaces, but this claim might be
502 incorrect: the names in question might actually correspond to
503 classes instead of namespaces. But if they correspond to
504 classes, then we should have found a match for them above. So if
505 we find them now, they should be genuine. */
507 /* FIXME: carlton/2003-06-12: This is a hack and should eventually
508 be deleted: see comments below. */
510 if (domain == VAR_DOMAIN)
512 sym = lookup_possible_namespace_symbol (name, symtab);
520 /* Look up a type named NESTED_NAME that is nested inside the C++
521 class or namespace given by PARENT_TYPE, from within the context
522 given by BLOCK. Return NULL if there is no such nested type. */
525 cp_lookup_nested_type (struct type *parent_type,
526 const char *nested_name,
527 const struct block *block)
529 switch (TYPE_CODE (parent_type))
531 case TYPE_CODE_STRUCT:
532 case TYPE_CODE_NAMESPACE:
534 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
535 of classes like, say, data or function members. Instead,
536 they're just represented by symbols whose names are
537 qualified by the name of the surrounding class. This is
538 just like members of namespaces; in particular,
539 lookup_symbol_namespace works when looking them up. */
541 const char *parent_name = TYPE_TAG_NAME (parent_type);
542 struct symbol *sym = cp_lookup_symbol_namespace (parent_name,
548 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
551 return SYMBOL_TYPE (sym);
554 internal_error (__FILE__, __LINE__,
555 _("cp_lookup_nested_type called on a non-aggregate type."));
559 /* The C++-version of lookup_transparent_type. */
561 /* FIXME: carlton/2004-01-16: The problem that this is trying to
562 address is that, unfortunately, sometimes NAME is wrong: it may not
563 include the name of namespaces enclosing the type in question.
564 lookup_transparent_type gets called when the the type in question
565 is a declaration, and we're trying to find its definition; but, for
566 declarations, our type name deduction mechanism doesn't work.
567 There's nothing we can do to fix this in general, I think, in the
568 absence of debug information about namespaces (I've filed PR
569 gdb/1511 about this); until such debug information becomes more
570 prevalent, one heuristic which sometimes looks is to search for the
571 definition in namespaces containing the current namespace.
573 We should delete this functions once the appropriate debug
574 information becomes more widespread. (GCC 3.4 will be the first
575 released version of GCC with such information.) */
578 cp_lookup_transparent_type (const char *name)
580 /* First, try the honest way of looking up the definition. */
581 struct type *t = basic_lookup_transparent_type (name);
587 /* If that doesn't work and we're within a namespace, look there
589 scope = block_scope (get_selected_block (0));
591 if (scope[0] == '\0')
594 return cp_lookup_transparent_type_loop (name, scope, 0);
597 /* Lookup the the type definition associated to NAME in
598 namespaces/classes containing SCOPE whose name is strictly longer
599 than LENGTH. LENGTH must be the index of the start of a
600 component of SCOPE. */
603 cp_lookup_transparent_type_loop (const char *name, const char *scope,
606 int scope_length = length + cp_find_first_component (scope + length);
609 /* If the current scope is followed by "::", look in the next
611 if (scope[scope_length] == ':')
614 = cp_lookup_transparent_type_loop (name, scope, scope_length + 2);
619 full_name = alloca (scope_length + 2 + strlen (name) + 1);
620 strncpy (full_name, scope, scope_length);
621 strncpy (full_name + scope_length, "::", 2);
622 strcpy (full_name + scope_length + 2, name);
624 return basic_lookup_transparent_type (full_name);
627 /* Now come functions for dealing with symbols associated to
628 namespaces. (They're used to store the namespaces themselves, not
629 objects that live in the namespaces.) These symbols come in two
630 varieties: if we run into a DW_TAG_namespace DIE, then we know that
631 we have a namespace, so dwarf2read.c creates a symbol for it just
632 like normal. But, unfortunately, versions of GCC through at least
633 3.3 don't generate those DIE's. Our solution is to try to guess
634 their existence by looking at demangled names. This might cause us
635 to misidentify classes as namespaces, however. So we put those
636 symbols in a special block (one per objfile), and we only search
637 that block as a last resort. */
639 /* FIXME: carlton/2003-06-12: Once versions of GCC that generate
640 DW_TAG_namespace have been out for a year or two, we should get rid
641 of all of this "possible namespace" nonsense. */
643 /* Allocate everything necessary for the possible namespace block
644 associated to OBJFILE. */
647 initialize_namespace_symtab (struct objfile *objfile)
649 struct symtab *namespace_symtab;
650 struct blockvector *bv;
653 namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile);
654 namespace_symtab->language = language_cplus;
655 namespace_symtab->free_code = free_nothing;
656 namespace_symtab->dirname = NULL;
658 bv = obstack_alloc (&objfile->objfile_obstack,
659 sizeof (struct blockvector)
660 + FIRST_LOCAL_BLOCK * sizeof (struct block *));
661 BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1;
662 BLOCKVECTOR (namespace_symtab) = bv;
664 /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */
666 bl = allocate_block (&objfile->objfile_obstack);
667 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
669 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
670 bl = allocate_block (&objfile->objfile_obstack);
671 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
673 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
675 /* Allocate the possible namespace block; we put it where the first
676 local block will live, though I don't think there's any need to
677 pretend that it's actually a local block (e.g. by setting
678 BLOCK_SUPERBLOCK appropriately). We don't use the global or
679 static block because we don't want it searched during the normal
680 search of all global/static blocks in lookup_symbol: we only want
681 it used as a last resort. */
683 /* NOTE: carlton/2003-09-11: I considered not associating the fake
684 symbols to a block/symtab at all. But that would cause problems
685 with lookup_symbol's SYMTAB argument and with block_found, so
686 having a symtab/block for this purpose seems like the best
689 bl = allocate_block (&objfile->objfile_obstack);
690 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
691 BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl;
693 namespace_symtab->free_func = free_namespace_block;
695 objfile->cp_namespace_symtab = namespace_symtab;
698 /* Locate the possible namespace block associated to OBJFILE,
699 allocating it if necessary. */
701 static struct block *
702 get_possible_namespace_block (struct objfile *objfile)
704 if (objfile->cp_namespace_symtab == NULL)
705 initialize_namespace_symtab (objfile);
707 return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab),
711 /* Free the dictionary associated to the possible namespace block. */
714 free_namespace_block (struct symtab *symtab)
716 struct block *possible_namespace_block;
718 possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
720 gdb_assert (possible_namespace_block != NULL);
721 dict_free (BLOCK_DICT (possible_namespace_block));
724 /* Ensure that there are symbols in the possible namespace block
725 associated to OBJFILE for all initial substrings of NAME that look
726 like namespaces or classes. NAME should end in a member variable:
727 it shouldn't consist solely of namespaces. */
730 cp_check_possible_namespace_symbols (const char *name, struct objfile *objfile)
732 check_possible_namespace_symbols_loop (name,
733 cp_find_first_component (name),
737 /* This is a helper loop for cp_check_possible_namespace_symbols; it
738 ensures that there are symbols in the possible namespace block
739 associated to OBJFILE for all namespaces that are initial
740 substrings of NAME of length at least LEN. It returns 1 if a
741 previous loop had already created the shortest such symbol and 0
744 This function assumes that if there is already a symbol associated
745 to a substring of NAME of a given length, then there are already
746 symbols associated to all substrings of NAME whose length is less
747 than that length. So if cp_check_possible_namespace_symbols has
748 been called once with argument "A::B::C::member", then that will
749 create symbols "A", "A::B", and "A::B::C". If it is then later
750 called with argument "A::B::D::member", then the new call will
751 generate a new symbol for "A::B::D", but once it sees that "A::B"
752 has already been created, it doesn't bother checking to see if "A"
753 has also been created. */
756 check_possible_namespace_symbols_loop (const char *name, int len,
757 struct objfile *objfile)
759 if (name[len] == ':')
762 int next_len = len + 2;
764 next_len += cp_find_first_component (name + next_len);
765 done = check_possible_namespace_symbols_loop (name, next_len,
769 done = check_one_possible_namespace_symbol (name, len, objfile);
777 /* Check to see if there's already a possible namespace symbol in
778 OBJFILE whose name is the initial substring of NAME of length LEN.
779 If not, create one and return 0; otherwise, return 1. */
782 check_one_possible_namespace_symbol (const char *name, int len,
783 struct objfile *objfile)
785 struct block *block = get_possible_namespace_block (objfile);
786 char *name_copy = alloca (len + 1);
789 memcpy (name_copy, name, len);
790 name_copy[len] = '\0';
791 sym = lookup_block_symbol (block, name_copy, NULL, VAR_DOMAIN);
796 name_copy = obsavestring (name, len, &objfile->objfile_obstack);
798 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, name_copy, objfile);
800 TYPE_TAG_NAME (type) = TYPE_NAME (type);
802 sym = obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol));
803 memset (sym, 0, sizeof (struct symbol));
804 SYMBOL_LANGUAGE (sym) = language_cplus;
805 SYMBOL_SET_NAMES (sym, name_copy, len, objfile);
806 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
807 SYMBOL_TYPE (sym) = type;
808 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
810 dict_add_symbol (BLOCK_DICT (block), sym);
818 /* Look for a symbol named NAME in all the possible namespace blocks.
819 If one is found, return it; if SYMTAB is non-NULL, set *SYMTAB to
820 equal the symtab where it was found. */
822 static struct symbol *
823 lookup_possible_namespace_symbol (const char *name, struct symtab **symtab)
825 struct objfile *objfile;
827 ALL_OBJFILES (objfile)
831 sym = lookup_block_symbol (get_possible_namespace_block (objfile),
832 name, NULL, VAR_DOMAIN);
837 *symtab = objfile->cp_namespace_symtab;
846 /* Print out all the possible namespace symbols. */
849 maintenance_cplus_namespace (char *args, int from_tty)
851 struct objfile *objfile;
852 printf_unfiltered (_("Possible namespaces:\n"));
853 ALL_OBJFILES (objfile)
855 struct dict_iterator iter;
858 ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym)
860 printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym));
866 _initialize_cp_namespace (void)
868 add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace,
869 "Print the list of possible C++ namespaces.",
870 &maint_cplus_cmd_list);