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 is known to have debugging
36 info for C++ namespaces. */
38 /* NOTE: carlton/2004-01-13: No currently released version of GCC (the
39 latest of which is 3.3.x at the time of this writing) produces this
40 debug info. GCC 3.4 should, however. */
42 unsigned char processing_has_namespace_info
;
44 /* This contains our best guess as to the name of the current
45 enclosing namespace(s)/class(es), if any. For example, if we're
46 within the method foo() in the following code:
55 then processing_current_prefix should be set to "N::C". If
56 processing_has_namespace_info is false, then this variable might
59 const char *processing_current_prefix
;
61 /* List of using directives that are active in the current file. */
63 static struct using_direct
*using_list
;
65 static struct using_direct
*cp_add_using (const char *name
,
66 unsigned int inner_len
,
67 unsigned int outer_len
,
68 struct using_direct
*next
);
70 static struct using_direct
*cp_copy_usings (struct using_direct
*using,
71 struct obstack
*obstack
);
73 static struct symbol
*lookup_namespace_scope (const char *name
,
74 const char *linkage_name
,
75 const struct block
*block
,
76 const domain_enum domain
,
77 struct symtab
**symtab
,
81 static struct symbol
*lookup_symbol_file (const char *name
,
82 const char *linkage_name
,
83 const struct block
*block
,
84 const domain_enum domain
,
85 struct symtab
**symtab
,
86 int anonymous_namespace
);
88 static void initialize_namespace_symtab (struct objfile
*objfile
);
90 static struct block
*get_possible_namespace_block (struct objfile
*objfile
);
92 static void free_namespace_block (struct symtab
*symtab
);
94 static int check_possible_namespace_symbols_loop (const char *name
,
96 struct objfile
*objfile
);
98 static int check_one_possible_namespace_symbol (const char *name
,
100 struct objfile
*objfile
);
103 struct symbol
*lookup_possible_namespace_symbol (const char *name
,
104 struct symtab
**symtab
);
106 static void maintenance_cplus_namespace (char *args
, int from_tty
);
108 /* Set up support for dealing with C++ namespace info in the current
111 void cp_initialize_namespace ()
113 processing_has_namespace_info
= 0;
117 /* Add all the using directives we've gathered to the current symtab.
118 STATIC_BLOCK should be the symtab's static block; OBSTACK is used
122 cp_finalize_namespace (struct block
*static_block
,
123 struct obstack
*obstack
)
125 if (using_list
!= NULL
)
127 block_set_using (static_block
,
128 cp_copy_usings (using_list
, obstack
),
134 /* Check to see if SYMBOL refers to an object contained within an
135 anonymous namespace; if so, add an appropriate using directive. */
137 /* Optimize away strlen ("(anonymous namespace)"). */
139 #define ANONYMOUS_NAMESPACE_LEN 21
142 cp_scan_for_anonymous_namespaces (const struct symbol
*symbol
)
144 if (!processing_has_namespace_info
145 && SYMBOL_CPLUS_DEMANGLED_NAME (symbol
) != NULL
)
147 const char *name
= SYMBOL_CPLUS_DEMANGLED_NAME (symbol
);
148 unsigned int previous_component
;
149 unsigned int next_component
;
152 /* Start with a quick-and-dirty check for mention of "(anonymous
155 if (!cp_is_anonymous (name
))
158 previous_component
= 0;
159 next_component
= cp_find_first_component (name
+ previous_component
);
161 while (name
[next_component
] == ':')
163 if ((next_component
- previous_component
) == ANONYMOUS_NAMESPACE_LEN
164 && strncmp (name
+ previous_component
,
165 "(anonymous namespace)",
166 ANONYMOUS_NAMESPACE_LEN
) == 0)
168 /* We've found a component of the name that's an
169 anonymous namespace. So add symbols in it to the
170 namespace given by the previous component if there is
171 one, or to the global namespace if there isn't. */
172 cp_add_using_directive (name
,
173 previous_component
== 0
174 ? 0 : previous_component
- 2,
177 /* The "+ 2" is for the "::". */
178 previous_component
= next_component
+ 2;
179 next_component
= (previous_component
180 + cp_find_first_component (name
181 + previous_component
));
186 /* Add a using directive to using_list. NAME is the start of a string
187 that should contain the namespaces we want to add as initial
188 substrings, OUTER_LENGTH is the end of the outer namespace, and
189 INNER_LENGTH is the end of the inner namespace. If the using
190 directive in question has already been added, don't add it
194 cp_add_using_directive (const char *name
, unsigned int outer_length
,
195 unsigned int inner_length
)
197 struct using_direct
*current
;
198 struct using_direct
*new;
200 /* Has it already been added? */
202 for (current
= using_list
; current
!= NULL
; current
= current
->next
)
204 if ((strncmp (current
->inner
, name
, inner_length
) == 0)
205 && (strlen (current
->inner
) == inner_length
)
206 && (strlen (current
->outer
) == outer_length
))
210 using_list
= cp_add_using (name
, inner_length
, outer_length
,
214 /* Record the namespace that the function defined by SYMBOL was
215 defined in, if necessary. BLOCK is the associated block; use
216 OBSTACK for allocation. */
219 cp_set_block_scope (const struct symbol
*symbol
,
221 struct obstack
*obstack
)
223 /* Make sure that the name was originally mangled: if not, there
224 certainly isn't any namespace information to worry about! */
226 if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol
) != NULL
)
228 if (processing_has_namespace_info
)
231 (block
, obsavestring (processing_current_prefix
,
232 strlen (processing_current_prefix
),
238 /* Try to figure out the appropriate namespace from the
241 /* FIXME: carlton/2003-04-15: If the function in question is
242 a method of a class, the name will actually include the
243 name of the class as well. This should be harmless, but
244 is a little unfortunate. */
246 const char *name
= SYMBOL_CPLUS_DEMANGLED_NAME (symbol
);
247 unsigned int prefix_len
= cp_entire_prefix_len (name
);
249 block_set_scope (block
,
250 obsavestring (name
, prefix_len
, obstack
),
256 /* Test whether or not NAMESPACE looks like it mentions an anonymous
257 namespace; return nonzero if so. */
260 cp_is_anonymous (const char *namespace)
262 return (strstr (namespace, "(anonymous namespace)")
266 /* Create a new struct using direct whose inner namespace is the
267 initial substring of NAME of leng INNER_LEN and whose outer
268 namespace is the initial substring of NAME of length OUTER_LENGTH.
269 Set its next member in the linked list to NEXT; allocate all memory
270 using xmalloc. It copies the strings, so NAME can be a temporary
273 static struct using_direct
*
274 cp_add_using (const char *name
,
275 unsigned int inner_len
,
276 unsigned int outer_len
,
277 struct using_direct
*next
)
279 struct using_direct
*retval
;
281 gdb_assert (outer_len
< inner_len
);
283 retval
= xmalloc (sizeof (struct using_direct
));
284 retval
->inner
= savestring (name
, inner_len
);
285 retval
->outer
= savestring (name
, outer_len
);
291 /* Make a copy of the using directives in the list pointed to by
292 USING, using OBSTACK to allocate memory. Free all memory pointed
293 to by USING via xfree. */
295 static struct using_direct
*
296 cp_copy_usings (struct using_direct
*using,
297 struct obstack
*obstack
)
305 struct using_direct
*retval
306 = obstack_alloc (obstack
, sizeof (struct using_direct
));
307 retval
->inner
= obsavestring (using->inner
, strlen (using->inner
),
309 retval
->outer
= obsavestring (using->outer
, strlen (using->outer
),
311 retval
->next
= cp_copy_usings (using->next
, obstack
);
313 xfree (using->inner
);
314 xfree (using->outer
);
321 /* The C++-specific version of name lookup for static and global
322 names. This makes sure that names get looked for in all namespaces
323 that are in scope. NAME is the natural name of the symbol that
324 we're looking for, LINKAGE_NAME (which is optional) is its linkage
325 name, BLOCK is the block that we're searching within, DOMAIN says
326 what kind of symbols we're looking for, and if SYMTAB is non-NULL,
327 we should store the symtab where we found the symbol in it. */
330 cp_lookup_symbol_nonlocal (const char *name
,
331 const char *linkage_name
,
332 const struct block
*block
,
333 const domain_enum domain
,
334 struct symtab
**symtab
)
336 return lookup_namespace_scope (name
, linkage_name
, block
, domain
,
337 symtab
, block_scope (block
), 0);
340 /* Lookup NAME at namespace scope (or, in C terms, in static and
341 global variables). SCOPE is the namespace that the current
342 function is defined within; only consider namespaces whose length
343 is at least SCOPE_LEN. Other arguments are as in
344 cp_lookup_symbol_nonlocal.
346 For example, if we're within a function A::B::f and looking for a
347 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
348 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
349 but with SCOPE_LEN = 1. And then it calls itself with NAME and
350 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
351 "A::B::x"; if it doesn't find it, then the second call looks for
352 "A::x", and if that call fails, then the first call looks for
355 static struct symbol
*
356 lookup_namespace_scope (const char *name
,
357 const char *linkage_name
,
358 const struct block
*block
,
359 const domain_enum domain
,
360 struct symtab
**symtab
,
366 if (scope
[scope_len
] != '\0')
368 /* Recursively search for names in child namespaces first. */
371 int new_scope_len
= scope_len
;
373 /* If the current scope is followed by "::", skip past that. */
374 if (new_scope_len
!= 0)
376 gdb_assert (scope
[new_scope_len
] == ':');
379 new_scope_len
+= cp_find_first_component (scope
+ new_scope_len
);
380 sym
= lookup_namespace_scope (name
, linkage_name
, block
,
382 scope
, new_scope_len
);
387 /* Okay, we didn't find a match in our children, so look for the
388 name in the current namespace. */
390 namespace = alloca (scope_len
+ 1);
391 strncpy (namespace, scope
, scope_len
);
392 namespace[scope_len
] = '\0';
393 return cp_lookup_symbol_namespace (namespace, name
, linkage_name
,
394 block
, domain
, symtab
);
397 /* Look up NAME in the C++ namespace NAMESPACE, applying the using
398 directives that are active in BLOCK. Other arguments are as in
399 cp_lookup_symbol_nonlocal. */
402 cp_lookup_symbol_namespace (const char *namespace,
404 const char *linkage_name
,
405 const struct block
*block
,
406 const domain_enum domain
,
407 struct symtab
**symtab
)
409 const struct using_direct
*current
;
412 /* First, go through the using directives. If any of them add new
413 names to the namespace we're searching in, see if we can find a
414 match by applying them. */
416 for (current
= block_using (block
);
418 current
= current
->next
)
420 if (strcmp (namespace, current
->outer
) == 0)
422 sym
= cp_lookup_symbol_namespace (current
->inner
,
433 /* We didn't find anything by applying any of the using directives
434 that are still applicable; so let's see if we've got a match
435 using the current namespace. */
437 if (namespace[0] == '\0')
439 return lookup_symbol_file (name
, linkage_name
, block
,
444 char *concatenated_name
445 = alloca (strlen (namespace) + 2 + strlen (name
) + 1);
446 strcpy (concatenated_name
, namespace);
447 strcat (concatenated_name
, "::");
448 strcat (concatenated_name
, name
);
449 sym
= lookup_symbol_file (concatenated_name
, linkage_name
,
450 block
, domain
, symtab
,
451 cp_is_anonymous (namespace));
456 /* Look up NAME in BLOCK's static block and in global blocks. If
457 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
458 within an anonymous namespace. Other arguments are as in
459 cp_lookup_symbol_nonlocal. */
461 static struct symbol
*
462 lookup_symbol_file (const char *name
,
463 const char *linkage_name
,
464 const struct block
*block
,
465 const domain_enum domain
,
466 struct symtab
**symtab
,
467 int anonymous_namespace
)
469 struct symbol
*sym
= NULL
;
471 sym
= lookup_symbol_static (name
, linkage_name
, block
, domain
, symtab
);
475 if (anonymous_namespace
)
477 /* Symbols defined in anonymous namespaces have external linkage
478 but should be treated as local to a single file nonetheless.
479 So we only search the current file's global block. */
481 const struct block
*global_block
= block_global_block (block
);
483 if (global_block
!= NULL
)
484 sym
= lookup_symbol_aux_block (name
, linkage_name
, global_block
,
489 sym
= lookup_symbol_global (name
, linkage_name
, domain
, symtab
);
495 /* Now call "lookup_possible_namespace_symbol". Symbols in here
496 claim to be associated to namespaces, but this claim might be
497 incorrect: the names in question might actually correspond to
498 classes instead of namespaces. But if they correspond to
499 classes, then we should have found a match for them above. So if
500 we find them now, they should be genuine. */
502 /* FIXME: carlton/2003-06-12: This is a hack and should eventually
503 be deleted: see comments below. */
505 if (domain
== VAR_DOMAIN
)
507 sym
= lookup_possible_namespace_symbol (name
, symtab
);
515 /* Look up a type named NESTED_NAME that is nested inside the C++
516 class or namespace given by PARENT_TYPE, from within the context
517 given by BLOCK. Return NULL if there is no such nested type. */
520 cp_lookup_nested_type (struct type
*parent_type
,
521 const char *nested_name
,
522 const struct block
*block
)
524 switch (TYPE_CODE (parent_type
))
526 case TYPE_CODE_STRUCT
:
527 case TYPE_CODE_NAMESPACE
:
529 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
530 of classes like, say, data or function members. Instead,
531 they're just represented by symbols whose names are
532 qualified by the name of the surrounding class. This is
533 just like members of namespaces; in particular,
534 lookup_symbol_namespace works when looking them up. */
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-aggregate type.");
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
);