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"
35 /* When set, the file that we're processing seems to have debugging
36 info for C++ namespaces, so cp-namespace.c shouldn't try to guess
37 namespace info itself. */
39 unsigned char processing_has_namespace_info
;
41 /* This contains our best guess as to the name of the current
42 enclosing namespace(s)/class(es), if any. For example, if we're
43 within the method foo() in the following code:
52 then processing_current_prefix should be set to "N::C". If
53 processing_has_namespace_info is false, then this variable might
56 const char *processing_current_prefix
;
58 /* List of using directives that are active in the current file. */
60 static struct using_direct
*using_list
;
62 static struct using_direct
*cp_add_using (const char *name
,
63 unsigned int inner_len
,
64 unsigned int outer_len
,
65 struct using_direct
*next
);
67 static struct using_direct
*cp_copy_usings (struct using_direct
*using,
68 struct obstack
*obstack
);
70 static struct symbol
*lookup_namespace_scope (const char *name
,
71 const char *linkage_name
,
72 const struct block
*block
,
73 const domain_enum domain
,
74 struct symtab
**symtab
,
78 static struct symbol
*lookup_symbol_file (const char *name
,
79 const char *linkage_name
,
80 const struct block
*block
,
81 const domain_enum domain
,
82 struct symtab
**symtab
,
83 int anonymous_namespace
);
85 static void initialize_namespace_symtab (struct objfile
*objfile
);
87 static struct block
*get_possible_namespace_block (struct objfile
*objfile
);
89 static void free_namespace_block (struct symtab
*symtab
);
91 static int check_possible_namespace_symbols_loop (const char *name
,
93 struct objfile
*objfile
);
95 static int check_one_possible_namespace_symbol (const char *name
,
97 struct objfile
*objfile
);
100 struct symbol
*lookup_possible_namespace_symbol (const char *name
,
101 struct symtab
**symtab
);
103 static void maintenance_cplus_namespace (char *args
, int from_tty
);
105 /* Set up support for dealing with C++ namespace info in the current
108 void cp_initialize_namespace ()
110 processing_has_namespace_info
= 0;
114 /* Add all the using directives we've gathered to the current symtab.
115 STATIC_BLOCK should be the symtab's static block; OBSTACK is used
119 cp_finalize_namespace (struct block
*static_block
,
120 struct obstack
*obstack
)
122 if (using_list
!= NULL
)
124 block_set_using (static_block
,
125 cp_copy_usings (using_list
, obstack
),
131 /* Check to see if SYMBOL refers to an object contained within an
132 anonymous namespace; if so, add an appropriate using directive. */
134 /* Optimize away strlen ("(anonymous namespace)"). */
136 #define ANONYMOUS_NAMESPACE_LEN 21
139 cp_scan_for_anonymous_namespaces (const struct symbol
*symbol
)
141 if (!processing_has_namespace_info
142 && SYMBOL_CPLUS_DEMANGLED_NAME (symbol
) != NULL
)
144 const char *name
= SYMBOL_CPLUS_DEMANGLED_NAME (symbol
);
145 unsigned int previous_component
;
146 unsigned int next_component
;
149 /* Start with a quick-and-dirty check for mention of "(anonymous
152 if (!cp_is_anonymous (name
))
155 previous_component
= 0;
156 next_component
= cp_find_first_component (name
+ previous_component
);
158 while (name
[next_component
] == ':')
160 if ((next_component
- previous_component
) == ANONYMOUS_NAMESPACE_LEN
161 && strncmp (name
+ previous_component
,
162 "(anonymous namespace)",
163 ANONYMOUS_NAMESPACE_LEN
) == 0)
165 /* We've found a component of the name that's an
166 anonymous namespace. So add symbols in it to the
167 namespace given by the previous component if there is
168 one, or to the global namespace if there isn't. */
169 cp_add_using_directive (name
,
170 previous_component
== 0
171 ? 0 : previous_component
- 2,
174 /* The "+ 2" is for the "::". */
175 previous_component
= next_component
+ 2;
176 next_component
= (previous_component
177 + cp_find_first_component (name
178 + previous_component
));
183 /* Add a using directive to using_list. NAME is the start of a string
184 that should contain the namespaces we want to add as initial
185 substrings, OUTER_LENGTH is the end of the outer namespace, and
186 INNER_LENGTH is the end of the inner namespace. If the using
187 directive in question has already been added, don't add it
191 cp_add_using_directive (const char *name
, unsigned int outer_length
,
192 unsigned int inner_length
)
194 struct using_direct
*current
;
195 struct using_direct
*new;
197 /* Has it already been added? */
199 for (current
= using_list
; current
!= NULL
; current
= current
->next
)
201 if ((strncmp (current
->inner
, name
, inner_length
) == 0)
202 && (strlen (current
->inner
) == inner_length
)
203 && (strlen (current
->outer
) == outer_length
))
207 using_list
= cp_add_using (name
, inner_length
, outer_length
,
211 /* Record the namespace that the function defined by SYMBOL was
212 defined in, if necessary. BLOCK is the associated block; use
213 OBSTACK for allocation. */
216 cp_set_block_scope (const struct symbol
*symbol
,
218 struct obstack
*obstack
)
220 /* Make sure that the name was originally mangled: if not, there
221 certainly isn't any namespace information to worry about! */
223 if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol
) != NULL
)
226 /* FIXME: carlton/2003-06-12: As mentioned above,
227 'processing_has_namespace_info' currently isn't entirely
228 reliable, so let's always use demangled names to get this
229 information for now. */
231 if (processing_has_namespace_info
)
234 (block
, obsavestring (processing_current_prefix
,
235 strlen (processing_current_prefix
),
242 /* Try to figure out the appropriate namespace from the
245 /* FIXME: carlton/2003-04-15: If the function in question is
246 a method of a class, the name will actually include the
247 name of the class as well. This should be harmless, but
248 is a little unfortunate. */
250 const char *name
= SYMBOL_CPLUS_DEMANGLED_NAME (symbol
);
251 unsigned int prefix_len
= cp_entire_prefix_len (name
);
253 block_set_scope (block
,
254 obsavestring (name
, prefix_len
, obstack
),
260 /* Test whether or not NAMESPACE looks like it mentions an anonymous
261 namespace; return nonzero if so. */
264 cp_is_anonymous (const char *namespace)
266 return (strstr (namespace, "(anonymous namespace)")
270 /* Create a new struct using direct whose inner namespace is the
271 initial substring of NAME of leng INNER_LEN and whose outer
272 namespace is the initial substring of NAME of length OUTER_LENGTH.
273 Set its next member in the linked list to NEXT; allocate all memory
274 using xmalloc. It copies the strings, so NAME can be a temporary
277 static struct using_direct
*
278 cp_add_using (const char *name
,
279 unsigned int inner_len
,
280 unsigned int outer_len
,
281 struct using_direct
*next
)
283 struct using_direct
*retval
;
285 gdb_assert (outer_len
< inner_len
);
287 retval
= xmalloc (sizeof (struct using_direct
));
288 retval
->inner
= savestring (name
, inner_len
);
289 retval
->outer
= savestring (name
, outer_len
);
295 /* Make a copy of the using directives in the list pointed to by
296 USING, using OBSTACK to allocate memory. Free all memory pointed
297 to by USING via xfree. */
299 static struct using_direct
*
300 cp_copy_usings (struct using_direct
*using,
301 struct obstack
*obstack
)
309 struct using_direct
*retval
310 = obstack_alloc (obstack
, sizeof (struct using_direct
));
311 retval
->inner
= obsavestring (using->inner
, strlen (using->inner
),
313 retval
->outer
= obsavestring (using->outer
, strlen (using->outer
),
315 retval
->next
= cp_copy_usings (using->next
, obstack
);
317 xfree (using->inner
);
318 xfree (using->outer
);
325 /* The C++-specific version of name lookup for static and global
326 names. This makes sure that names get looked for in all namespaces
327 that are in scope. NAME is the natural name of the symbol that
328 we're looking for, LINKAGE_NAME (which is optional) is its linkage
329 name, BLOCK is the block that we're searching within, DOMAIN says
330 what kind of symbols we're looking for, and if SYMTAB is non-NULL,
331 we should store the symtab where we found the symbol in it. */
334 cp_lookup_symbol_nonlocal (const char *name
,
335 const char *linkage_name
,
336 const struct block
*block
,
337 const domain_enum domain
,
338 struct symtab
**symtab
)
340 return lookup_namespace_scope (name
, linkage_name
, block
, domain
,
341 symtab
, block_scope (block
), 0);
344 /* Lookup NAME at namespace scope (or, in C terms, in static and
345 global variables). SCOPE is the namespace that the current
346 function is defined within; only consider namespaces whose length
347 is at least SCOPE_LEN. Other arguments are as in
348 cp_lookup_symbol_nonlocal.
350 For example, if we're within a function A::B::f and looking for a
351 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
352 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
353 but with SCOPE_LEN = 1. And then it calls itself with NAME and
354 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
355 "A::B::x"; if it doesn't find it, then the second call looks for
356 "A::x", and if that call fails, then the first call looks for
359 static struct symbol
*
360 lookup_namespace_scope (const char *name
,
361 const char *linkage_name
,
362 const struct block
*block
,
363 const domain_enum domain
,
364 struct symtab
**symtab
,
370 if (scope
[scope_len
] != '\0')
372 /* Recursively search for names in child namespaces first. */
375 int new_scope_len
= scope_len
;
377 /* If the current scope is followed by "::", skip past that. */
378 if (new_scope_len
!= 0)
380 gdb_assert (scope
[new_scope_len
] == ':');
383 new_scope_len
+= cp_find_first_component (scope
+ new_scope_len
);
384 sym
= lookup_namespace_scope (name
, linkage_name
, block
,
386 scope
, new_scope_len
);
391 /* Okay, we didn't find a match in our children, so look for the
392 name in the current namespace. */
394 namespace = alloca (scope_len
+ 1);
395 strncpy (namespace, scope
, scope_len
);
396 namespace[scope_len
] = '\0';
397 return cp_lookup_symbol_namespace (namespace, name
, linkage_name
,
398 block
, domain
, symtab
);
401 /* Look up NAME in the C++ namespace NAMESPACE, applying the using
402 directives that are active in BLOCK. Other arguments are as in
403 cp_lookup_symbol_nonlocal. */
406 cp_lookup_symbol_namespace (const char *namespace,
408 const char *linkage_name
,
409 const struct block
*block
,
410 const domain_enum domain
,
411 struct symtab
**symtab
)
413 const struct using_direct
*current
;
416 /* First, go through the using directives. If any of them add new
417 names to the namespace we're searching in, see if we can find a
418 match by applying them. */
420 for (current
= block_using (block
);
422 current
= current
->next
)
424 if (strcmp (namespace, current
->outer
) == 0)
426 sym
= cp_lookup_symbol_namespace (current
->inner
,
437 /* We didn't find anything by applying any of the using directives
438 that are still applicable; so let's see if we've got a match
439 using the current namespace. */
441 if (namespace[0] == '\0')
443 return lookup_symbol_file (name
, linkage_name
, block
,
448 char *concatenated_name
449 = alloca (strlen (namespace) + 2 + strlen (name
) + 1);
450 strcpy (concatenated_name
, namespace);
451 strcat (concatenated_name
, "::");
452 strcat (concatenated_name
, name
);
453 sym
= lookup_symbol_file (concatenated_name
, linkage_name
,
454 block
, domain
, symtab
,
455 cp_is_anonymous (namespace));
460 /* Look up NAME in BLOCK's static block and in global blocks. If
461 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
462 within an anonymous namespace. Other arguments are as in
463 cp_lookup_symbol_nonlocal. */
465 static struct symbol
*
466 lookup_symbol_file (const char *name
,
467 const char *linkage_name
,
468 const struct block
*block
,
469 const domain_enum domain
,
470 struct symtab
**symtab
,
471 int anonymous_namespace
)
473 struct symbol
*sym
= NULL
;
475 sym
= lookup_symbol_static (name
, linkage_name
, block
, domain
, symtab
);
479 if (anonymous_namespace
)
481 /* Symbols defined in anonymous namespaces have external linkage
482 but should be treated as local to a single file nonetheless.
483 So we only search the current file's global block. */
485 const struct block
*global_block
= block_global_block (block
);
487 if (global_block
!= NULL
)
488 sym
= lookup_symbol_aux_block (name
, linkage_name
, global_block
,
493 sym
= lookup_symbol_global (name
, linkage_name
, domain
, symtab
);
499 /* Now call "lookup_possible_namespace_symbol". Symbols in here
500 claim to be associated to namespaces, but this claim might be
501 incorrect: the names in question might actually correspond to
502 classes instead of namespaces. But if they correspond to
503 classes, then we should have found a match for them above. So if
504 we find them now, they should be genuine. */
506 /* FIXME: carlton/2003-06-12: This is a hack and should eventually
507 be deleted: see comments below. */
509 if (domain
== VAR_DOMAIN
)
511 sym
= lookup_possible_namespace_symbol (name
, symtab
);
519 /* Look up a type named NESTED_NAME that is nested inside the C++
520 class or namespace given by PARENT_TYPE, from within the context
521 given by BLOCK. Return NULL if there is no such nested type. */
523 /* FIXME: carlton/2003-09-24: For now, this only works for nested
524 namespaces; the patch to make this work on other sorts of nested
525 types is next on my TODO list. */
528 cp_lookup_nested_type (struct type
*parent_type
,
529 const char *nested_name
,
530 const struct block
*block
)
532 switch (TYPE_CODE (parent_type
))
534 case TYPE_CODE_NAMESPACE
:
536 const char *parent_name
= TYPE_TAG_NAME (parent_type
);
537 struct symbol
*sym
= cp_lookup_symbol_namespace (parent_name
,
543 if (sym
== NULL
|| SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
546 return SYMBOL_TYPE (sym
);
549 internal_error (__FILE__
, __LINE__
,
550 "cp_lookup_nested_type called on a non-namespace.");
554 /* Now come functions for dealing with symbols associated to
555 namespaces. (They're used to store the namespaces themselves, not
556 objects that live in the namespaces.) These symbols come in two
557 varieties: if we run into a DW_TAG_namespace DIE, then we know that
558 we have a namespace, so dwarf2read.c creates a symbol for it just
559 like normal. But, unfortunately, versions of GCC through at least
560 3.3 don't generate those DIE's. Our solution is to try to guess
561 their existence by looking at demangled names. This might cause us
562 to misidentify classes as namespaces, however. So we put those
563 symbols in a special block (one per objfile), and we only search
564 that block as a last resort. */
566 /* FIXME: carlton/2003-06-12: Once versions of GCC that generate
567 DW_TAG_namespace have been out for a year or two, we should get rid
568 of all of this "possible namespace" nonsense. */
570 /* Allocate everything necessary for the possible namespace block
571 associated to OBJFILE. */
574 initialize_namespace_symtab (struct objfile
*objfile
)
576 struct symtab
*namespace_symtab
;
577 struct blockvector
*bv
;
580 namespace_symtab
= allocate_symtab ("<<C++-namespaces>>", objfile
);
581 namespace_symtab
->language
= language_cplus
;
582 namespace_symtab
->free_code
= free_nothing
;
583 namespace_symtab
->dirname
= NULL
;
585 bv
= obstack_alloc (&objfile
->symbol_obstack
,
586 sizeof (struct blockvector
)
587 + FIRST_LOCAL_BLOCK
* sizeof (struct block
*));
588 BLOCKVECTOR_NBLOCKS (bv
) = FIRST_LOCAL_BLOCK
+ 1;
589 BLOCKVECTOR (namespace_symtab
) = bv
;
591 /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */
593 bl
= allocate_block (&objfile
->symbol_obstack
);
594 BLOCK_DICT (bl
) = dict_create_linear (&objfile
->symbol_obstack
,
596 BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
) = bl
;
597 bl
= allocate_block (&objfile
->symbol_obstack
);
598 BLOCK_DICT (bl
) = dict_create_linear (&objfile
->symbol_obstack
,
600 BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
) = bl
;
602 /* Allocate the possible namespace block; we put it where the first
603 local block will live, though I don't think there's any need to
604 pretend that it's actually a local block (e.g. by setting
605 BLOCK_SUPERBLOCK appropriately). We don't use the global or
606 static block because we don't want it searched during the normal
607 search of all global/static blocks in lookup_symbol: we only want
608 it used as a last resort. */
610 /* NOTE: carlton/2003-09-11: I considered not associating the fake
611 symbols to a block/symtab at all. But that would cause problems
612 with lookup_symbol's SYMTAB argument and with block_found, so
613 having a symtab/block for this purpose seems like the best
616 bl
= allocate_block (&objfile
->symbol_obstack
);
617 BLOCK_DICT (bl
) = dict_create_hashed_expandable ();
618 BLOCKVECTOR_BLOCK (bv
, FIRST_LOCAL_BLOCK
) = bl
;
620 namespace_symtab
->free_func
= free_namespace_block
;
622 objfile
->cp_namespace_symtab
= namespace_symtab
;
625 /* Locate the possible namespace block associated to OBJFILE,
626 allocating it if necessary. */
628 static struct block
*
629 get_possible_namespace_block (struct objfile
*objfile
)
631 if (objfile
->cp_namespace_symtab
== NULL
)
632 initialize_namespace_symtab (objfile
);
634 return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile
->cp_namespace_symtab
),
638 /* Free the dictionary associated to the possible namespace block. */
641 free_namespace_block (struct symtab
*symtab
)
643 struct block
*possible_namespace_block
;
645 possible_namespace_block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
),
647 gdb_assert (possible_namespace_block
!= NULL
);
648 dict_free (BLOCK_DICT (possible_namespace_block
));
651 /* Ensure that there are symbols in the possible namespace block
652 associated to OBJFILE for all initial substrings of NAME that look
653 like namespaces or classes. NAME should end in a member variable:
654 it shouldn't consist solely of namespaces. */
657 cp_check_possible_namespace_symbols (const char *name
, struct objfile
*objfile
)
659 check_possible_namespace_symbols_loop (name
,
660 cp_find_first_component (name
),
664 /* This is a helper loop for cp_check_possible_namespace_symbols; it
665 ensures that there are symbols in the possible namespace block
666 associated to OBJFILE for all namespaces that are initial
667 substrings of NAME of length at least LEN. It returns 1 if a
668 previous loop had already created the shortest such symbol and 0
671 This function assumes that if there is already a symbol associated
672 to a substring of NAME of a given length, then there are already
673 symbols associated to all substrings of NAME whose length is less
674 than that length. So if cp_check_possible_namespace_symbols has
675 been called once with argument "A::B::C::member", then that will
676 create symbols "A", "A::B", and "A::B::C". If it is then later
677 called with argument "A::B::D::member", then the new call will
678 generate a new symbol for "A::B::D", but once it sees that "A::B"
679 has already been created, it doesn't bother checking to see if "A"
680 has also been created. */
683 check_possible_namespace_symbols_loop (const char *name
, int len
,
684 struct objfile
*objfile
)
686 if (name
[len
] == ':')
689 int next_len
= len
+ 2;
691 next_len
+= cp_find_first_component (name
+ next_len
);
692 done
= check_possible_namespace_symbols_loop (name
, next_len
,
696 done
= check_one_possible_namespace_symbol (name
, len
, objfile
);
704 /* Check to see if there's already a possible namespace symbol in
705 OBJFILE whose name is the initial substring of NAME of length LEN.
706 If not, create one and return 0; otherwise, return 1. */
709 check_one_possible_namespace_symbol (const char *name
, int len
,
710 struct objfile
*objfile
)
712 struct block
*block
= get_possible_namespace_block (objfile
);
713 char *name_copy
= obsavestring (name
, len
, &objfile
->symbol_obstack
);
714 struct symbol
*sym
= lookup_block_symbol (block
, name_copy
, NULL
,
719 struct type
*type
= init_type (TYPE_CODE_NAMESPACE
, 0, 0,
721 TYPE_TAG_NAME (type
) = TYPE_NAME (type
);
723 sym
= obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct symbol
));
724 memset (sym
, 0, sizeof (struct symbol
));
725 SYMBOL_LANGUAGE (sym
) = language_cplus
;
726 SYMBOL_SET_NAMES (sym
, name_copy
, len
, objfile
);
727 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
728 SYMBOL_TYPE (sym
) = type
;
729 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
731 dict_add_symbol (BLOCK_DICT (block
), sym
);
737 obstack_free (&objfile
->symbol_obstack
, name_copy
);
743 /* Look for a symbol named NAME in all the possible namespace blocks.
744 If one is found, return it; if SYMTAB is non-NULL, set *SYMTAB to
745 equal the symtab where it was found. */
747 static struct symbol
*
748 lookup_possible_namespace_symbol (const char *name
, struct symtab
**symtab
)
750 struct objfile
*objfile
;
752 ALL_OBJFILES (objfile
)
756 sym
= lookup_block_symbol (get_possible_namespace_block (objfile
),
757 name
, NULL
, VAR_DOMAIN
);
762 *symtab
= objfile
->cp_namespace_symtab
;
771 /* Print out all the possible namespace symbols. */
774 maintenance_cplus_namespace (char *args
, int from_tty
)
776 struct objfile
*objfile
;
777 printf_unfiltered ("Possible namespaces:\n");
778 ALL_OBJFILES (objfile
)
780 struct dict_iterator iter
;
783 ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile
), iter
, sym
)
785 printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym
));
791 _initialize_cp_namespace (void)
793 add_cmd ("namespace", class_maintenance
, maintenance_cplus_namespace
,
794 "Print the list of possible C++ namespaces.",
795 &maint_cplus_cmd_list
);