1 /* Helper routines for C++ support in GDB.
2 Copyright 2003 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 /* If processing_has_namespace_info is nonzero, this string should
42 contain the name of the current namespace. The string is
43 temporary; copy it if you need it. */
45 /* FIXME: carlton/2003-06-12: This isn't entirely reliable: currently,
46 we get mislead by DW_AT_specification. */
48 const char *processing_current_namespace
;
50 /* List of using directives that are active in the current file. */
52 static struct using_direct
*using_list
;
54 static struct using_direct
*cp_add_using (const char *name
,
55 unsigned int inner_len
,
56 unsigned int outer_len
,
57 struct using_direct
*next
);
59 static struct using_direct
*cp_copy_usings (struct using_direct
*using,
60 struct obstack
*obstack
);
62 static struct symbol
*lookup_namespace_scope (const char *name
,
63 const char *linkage_name
,
64 const struct block
*block
,
65 const domain_enum domain
,
66 struct symtab
**symtab
,
70 static struct symbol
*lookup_symbol_file (const char *name
,
71 const char *linkage_name
,
72 const struct block
*block
,
73 const domain_enum domain
,
74 struct symtab
**symtab
,
75 int anonymous_namespace
);
77 static void initialize_namespace_symtab (struct objfile
*objfile
);
79 static struct block
*get_possible_namespace_block (struct objfile
*objfile
);
81 static void free_namespace_block (struct symtab
*symtab
);
83 static int check_possible_namespace_symbols_loop (const char *name
,
85 struct objfile
*objfile
);
87 static int check_one_possible_namespace_symbol (const char *name
,
89 struct objfile
*objfile
);
92 struct symbol
*lookup_possible_namespace_symbol (const char *name
,
93 struct symtab
**symtab
);
95 static void maintenance_cplus_namespace (char *args
, int from_tty
);
97 /* Set up support for dealing with C++ namespace info in the current
100 void cp_initialize_namespace ()
102 processing_has_namespace_info
= 0;
106 /* Add all the using directives we've gathered to the current symtab.
107 STATIC_BLOCK should be the symtab's static block; OBSTACK is used
111 cp_finalize_namespace (struct block
*static_block
,
112 struct obstack
*obstack
)
114 if (using_list
!= NULL
)
116 block_set_using (static_block
,
117 cp_copy_usings (using_list
, obstack
),
123 /* Check to see if SYMBOL refers to an object contained within an
124 anonymous namespace; if so, add an appropriate using directive. */
126 /* Optimize away strlen ("(anonymous namespace)"). */
128 #define ANONYMOUS_NAMESPACE_LEN 21
131 cp_scan_for_anonymous_namespaces (const struct symbol
*symbol
)
133 if (!processing_has_namespace_info
134 && SYMBOL_CPLUS_DEMANGLED_NAME (symbol
) != NULL
)
136 const char *name
= SYMBOL_CPLUS_DEMANGLED_NAME (symbol
);
137 unsigned int previous_component
;
138 unsigned int next_component
;
141 /* Start with a quick-and-dirty check for mention of "(anonymous
144 if (!cp_is_anonymous (name
))
147 previous_component
= 0;
148 next_component
= cp_find_first_component (name
+ previous_component
);
150 while (name
[next_component
] == ':')
152 if ((next_component
- previous_component
) == ANONYMOUS_NAMESPACE_LEN
153 && strncmp (name
+ previous_component
,
154 "(anonymous namespace)",
155 ANONYMOUS_NAMESPACE_LEN
) == 0)
157 /* We've found a component of the name that's an
158 anonymous namespace. So add symbols in it to the
159 namespace given by the previous component if there is
160 one, or to the global namespace if there isn't. */
161 cp_add_using_directive (name
,
162 previous_component
== 0
163 ? 0 : previous_component
- 2,
166 /* The "+ 2" is for the "::". */
167 previous_component
= next_component
+ 2;
168 next_component
= (previous_component
169 + cp_find_first_component (name
170 + previous_component
));
175 /* Add a using directive to using_list. NAME is the start of a string
176 that should contain the namespaces we want to add as initial
177 substrings, OUTER_LENGTH is the end of the outer namespace, and
178 INNER_LENGTH is the end of the inner namespace. If the using
179 directive in question has already been added, don't add it
183 cp_add_using_directive (const char *name
, unsigned int outer_length
,
184 unsigned int inner_length
)
186 struct using_direct
*current
;
187 struct using_direct
*new;
189 /* Has it already been added? */
191 for (current
= using_list
; current
!= NULL
; current
= current
->next
)
193 if ((strncmp (current
->inner
, name
, inner_length
) == 0)
194 && (strlen (current
->inner
) == inner_length
)
195 && (strlen (current
->outer
) == outer_length
))
199 using_list
= cp_add_using (name
, inner_length
, outer_length
,
203 /* Record the namespace that the function defined by SYMBOL was
204 defined in, if necessary. BLOCK is the associated block; use
205 OBSTACK for allocation. */
208 cp_set_block_scope (const struct symbol
*symbol
,
210 struct obstack
*obstack
)
212 /* Make sure that the name was originally mangled: if not, there
213 certainly isn't any namespace information to worry about! */
215 if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol
) != NULL
)
218 /* FIXME: carlton/2003-06-12: As mentioned above,
219 'processing_has_namespace_info' currently isn't entirely
220 reliable, so let's always use demangled names to get this
221 information for now. */
223 if (processing_has_namespace_info
)
226 (block
, obsavestring (processing_current_namespace
,
227 strlen (processing_current_namespace
),
234 /* Try to figure out the appropriate namespace from the
237 /* FIXME: carlton/2003-04-15: If the function in question is
238 a method of a class, the name will actually include the
239 name of the class as well. This should be harmless, but
240 is a little unfortunate. */
242 const char *name
= SYMBOL_CPLUS_DEMANGLED_NAME (symbol
);
243 unsigned int prefix_len
= cp_entire_prefix_len (name
);
245 block_set_scope (block
,
246 obsavestring (name
, prefix_len
, obstack
),
252 /* Test whether or not NAMESPACE looks like it mentions an anonymous
253 namespace; return nonzero if so. */
256 cp_is_anonymous (const char *namespace)
258 return (strstr (namespace, "(anonymous namespace)")
262 /* Create a new struct using direct whose inner namespace is the
263 initial substring of NAME of leng INNER_LEN and whose outer
264 namespace is the initial substring of NAME of length OUTER_LENGTH.
265 Set its next member in the linked list to NEXT; allocate all memory
266 using xmalloc. It copies the strings, so NAME can be a temporary
269 static struct using_direct
*
270 cp_add_using (const char *name
,
271 unsigned int inner_len
,
272 unsigned int outer_len
,
273 struct using_direct
*next
)
275 struct using_direct
*retval
;
277 gdb_assert (outer_len
< inner_len
);
279 retval
= xmalloc (sizeof (struct using_direct
));
280 retval
->inner
= savestring (name
, inner_len
);
281 retval
->outer
= savestring (name
, outer_len
);
287 /* Make a copy of the using directives in the list pointed to by
288 USING, using OBSTACK to allocate memory. Free all memory pointed
289 to by USING via xfree. */
291 static struct using_direct
*
292 cp_copy_usings (struct using_direct
*using,
293 struct obstack
*obstack
)
301 struct using_direct
*retval
302 = obstack_alloc (obstack
, sizeof (struct using_direct
));
303 retval
->inner
= obsavestring (using->inner
, strlen (using->inner
),
305 retval
->outer
= obsavestring (using->outer
, strlen (using->outer
),
307 retval
->next
= cp_copy_usings (using->next
, obstack
);
309 xfree (using->inner
);
310 xfree (using->outer
);
317 /* The C++-specific version of name lookup for static and global
318 names. This makes sure that names get looked for in all namespaces
319 that are in scope. NAME is the natural name of the symbol that
320 we're looking for, LINKAGE_NAME (which is optional) is its linkage
321 name, BLOCK is the block that we're searching within, DOMAIN says
322 what kind of symbols we're looking for, and if SYMTAB is non-NULL,
323 we should store the symtab where we found the symbol in it. */
326 cp_lookup_symbol_nonlocal (const char *name
,
327 const char *linkage_name
,
328 const struct block
*block
,
329 const domain_enum domain
,
330 struct symtab
**symtab
)
332 return lookup_namespace_scope (name
, linkage_name
, block
, domain
,
333 symtab
, block_scope (block
), 0);
336 /* Lookup NAME at namespace scope (or, in C terms, in static and
337 global variables). SCOPE is the namespace that the current
338 function is defined within; only consider namespaces whose length
339 is at least SCOPE_LEN. Other arguments are as in
340 cp_lookup_symbol_nonlocal.
342 For example, if we're within a function A::B::f and looking for a
343 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
344 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
345 but with SCOPE_LEN = 1. And then it calls itself with NAME and
346 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
347 "A::B::x"; if it doesn't find it, then the second call looks for
348 "A::x", and if that call fails, then the first call looks for
351 static struct symbol
*
352 lookup_namespace_scope (const char *name
,
353 const char *linkage_name
,
354 const struct block
*block
,
355 const domain_enum domain
,
356 struct symtab
**symtab
,
362 if (scope
[scope_len
] != '\0')
364 /* Recursively search for names in child namespaces first. */
367 int new_scope_len
= scope_len
;
369 /* If the current scope is followed by "::", skip past that. */
370 if (new_scope_len
!= 0)
372 gdb_assert (scope
[new_scope_len
] == ':');
375 new_scope_len
+= cp_find_first_component (scope
+ new_scope_len
);
376 sym
= lookup_namespace_scope (name
, linkage_name
, block
,
378 scope
, new_scope_len
);
383 /* Okay, we didn't find a match in our children, so look for the
384 name in the current namespace. */
386 namespace = alloca (scope_len
+ 1);
387 strncpy (namespace, scope
, scope_len
);
388 namespace[scope_len
] = '\0';
389 return cp_lookup_symbol_namespace (namespace, name
, linkage_name
,
390 block
, domain
, symtab
);
393 /* Look up NAME in the C++ namespace NAMESPACE, applying the using
394 directives that are active in BLOCK. Other arguments are as in
395 cp_lookup_symbol_nonlocal. */
398 cp_lookup_symbol_namespace (const char *namespace,
400 const char *linkage_name
,
401 const struct block
*block
,
402 const domain_enum domain
,
403 struct symtab
**symtab
)
405 const struct using_direct
*current
;
408 /* First, go through the using directives. If any of them add new
409 names to the namespace we're searching in, see if we can find a
410 match by applying them. */
412 for (current
= block_using (block
);
414 current
= current
->next
)
416 if (strcmp (namespace, current
->outer
) == 0)
418 sym
= cp_lookup_symbol_namespace (current
->inner
,
429 /* We didn't find anything by applying any of the using directives
430 that are still applicable; so let's see if we've got a match
431 using the current namespace. */
433 if (namespace[0] == '\0')
435 return lookup_symbol_file (name
, linkage_name
, block
,
440 char *concatenated_name
441 = alloca (strlen (namespace) + 2 + strlen (name
) + 1);
442 strcpy (concatenated_name
, namespace);
443 strcat (concatenated_name
, "::");
444 strcat (concatenated_name
, name
);
445 sym
= lookup_symbol_file (concatenated_name
, linkage_name
,
446 block
, domain
, symtab
,
447 cp_is_anonymous (namespace));
452 /* Look up NAME in BLOCK's static block and in global blocks. If
453 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
454 within an anonymous namespace. Other arguments are as in
455 cp_lookup_symbol_nonlocal. */
457 static struct symbol
*
458 lookup_symbol_file (const char *name
,
459 const char *linkage_name
,
460 const struct block
*block
,
461 const domain_enum domain
,
462 struct symtab
**symtab
,
463 int anonymous_namespace
)
465 struct symbol
*sym
= NULL
;
467 sym
= lookup_symbol_static (name
, linkage_name
, block
, domain
, symtab
);
471 if (anonymous_namespace
)
473 /* Symbols defined in anonymous namespaces have external linkage
474 but should be treated as local to a single file nonetheless.
475 So we only search the current file's global block. */
477 const struct block
*global_block
= block_global_block (block
);
479 if (global_block
!= NULL
)
480 sym
= lookup_symbol_aux_block (name
, linkage_name
, global_block
,
485 sym
= lookup_symbol_global (name
, linkage_name
, domain
, symtab
);
491 /* Now call "lookup_possible_namespace_symbol". Symbols in here
492 claim to be associated to namespaces, but this claim might be
493 incorrect: the names in question might actually correspond to
494 classes instead of namespaces. But if they correspond to
495 classes, then we should have found a match for them above. So if
496 we find them now, they should be genuine. */
498 /* FIXME: carlton/2003-06-12: This is a hack and should eventually
499 be deleted: see comments below. */
501 if (domain
== VAR_DOMAIN
)
503 sym
= lookup_possible_namespace_symbol (name
, symtab
);
511 /* Look up a type named NESTED_NAME that is nested inside the C++
512 class or namespace given by PARENT_TYPE, from within the context
513 given by BLOCK. Return NULL if there is no such nested type. */
515 /* FIXME: carlton/2003-09-24: For now, this only works for nested
516 namespaces; the patch to make this work on other sorts of nested
517 types is next on my TODO list. */
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_NAMESPACE
:
528 const char *parent_name
= TYPE_TAG_NAME (parent_type
);
529 struct symbol
*sym
= cp_lookup_symbol_namespace (parent_name
,
535 if (sym
== NULL
|| SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
538 return SYMBOL_TYPE (sym
);
541 internal_error (__FILE__
, __LINE__
,
542 "cp_lookup_nested_type called on a non-namespace.");
546 /* Now come functions for dealing with symbols associated to
547 namespaces. (They're used to store the namespaces themselves, not
548 objects that live in the namespaces.) These symbols come in two
549 varieties: if we run into a DW_TAG_namespace DIE, then we know that
550 we have a namespace, so dwarf2read.c creates a symbol for it just
551 like normal. But, unfortunately, versions of GCC through at least
552 3.3 don't generate those DIE's. Our solution is to try to guess
553 their existence by looking at demangled names. This might cause us
554 to misidentify classes as namespaces, however. So we put those
555 symbols in a special block (one per objfile), and we only search
556 that block as a last resort. */
558 /* FIXME: carlton/2003-06-12: Once versions of GCC that generate
559 DW_TAG_namespace have been out for a year or two, we should get rid
560 of all of this "possible namespace" nonsense. */
562 /* Allocate everything necessary for the possible namespace block
563 associated to OBJFILE. */
566 initialize_namespace_symtab (struct objfile
*objfile
)
568 struct symtab
*namespace_symtab
;
569 struct blockvector
*bv
;
572 namespace_symtab
= allocate_symtab ("<<C++-namespaces>>", objfile
);
573 namespace_symtab
->language
= language_cplus
;
574 namespace_symtab
->free_code
= free_nothing
;
575 namespace_symtab
->dirname
= NULL
;
577 bv
= obstack_alloc (&objfile
->symbol_obstack
,
578 sizeof (struct blockvector
)
579 + FIRST_LOCAL_BLOCK
* sizeof (struct block
*));
580 BLOCKVECTOR_NBLOCKS (bv
) = FIRST_LOCAL_BLOCK
+ 1;
581 BLOCKVECTOR (namespace_symtab
) = bv
;
583 /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */
585 bl
= allocate_block (&objfile
->symbol_obstack
);
586 BLOCK_DICT (bl
) = dict_create_linear (&objfile
->symbol_obstack
,
588 BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
) = bl
;
589 bl
= allocate_block (&objfile
->symbol_obstack
);
590 BLOCK_DICT (bl
) = dict_create_linear (&objfile
->symbol_obstack
,
592 BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
) = bl
;
594 /* Allocate the possible namespace block; we put it where the first
595 local block will live, though I don't think there's any need to
596 pretend that it's actually a local block (e.g. by setting
597 BLOCK_SUPERBLOCK appropriately). We don't use the global or
598 static block because we don't want it searched during the normal
599 search of all global/static blocks in lookup_symbol: we only want
600 it used as a last resort. */
602 /* NOTE: carlton/2003-09-11: I considered not associating the fake
603 symbols to a block/symtab at all. But that would cause problems
604 with lookup_symbol's SYMTAB argument and with block_found, so
605 having a symtab/block for this purpose seems like the best
608 bl
= allocate_block (&objfile
->symbol_obstack
);
609 BLOCK_DICT (bl
) = dict_create_hashed_expandable ();
610 BLOCKVECTOR_BLOCK (bv
, FIRST_LOCAL_BLOCK
) = bl
;
612 namespace_symtab
->free_func
= free_namespace_block
;
614 objfile
->cp_namespace_symtab
= namespace_symtab
;
617 /* Locate the possible namespace block associated to OBJFILE,
618 allocating it if necessary. */
620 static struct block
*
621 get_possible_namespace_block (struct objfile
*objfile
)
623 if (objfile
->cp_namespace_symtab
== NULL
)
624 initialize_namespace_symtab (objfile
);
626 return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile
->cp_namespace_symtab
),
630 /* Free the dictionary associated to the possible namespace block. */
633 free_namespace_block (struct symtab
*symtab
)
635 struct block
*possible_namespace_block
;
637 possible_namespace_block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
),
639 gdb_assert (possible_namespace_block
!= NULL
);
640 dict_free (BLOCK_DICT (possible_namespace_block
));
643 /* Ensure that there are symbols in the possible namespace block
644 associated to OBJFILE for all initial substrings of NAME that look
645 like namespaces or classes. NAME should end in a member variable:
646 it shouldn't consist solely of namespaces. */
649 cp_check_possible_namespace_symbols (const char *name
, struct objfile
*objfile
)
651 check_possible_namespace_symbols_loop (name
,
652 cp_find_first_component (name
),
656 /* This is a helper loop for cp_check_possible_namespace_symbols; it
657 ensures that there are symbols in the possible namespace block
658 associated to OBJFILE for all namespaces that are initial
659 substrings of NAME of length at least LEN. It returns 1 if a
660 previous loop had already created the shortest such symbol and 0
663 This function assumes that if there is already a symbol associated
664 to a substring of NAME of a given length, then there are already
665 symbols associated to all substrings of NAME whose length is less
666 than that length. So if cp_check_possible_namespace_symbols has
667 been called once with argument "A::B::C::member", then that will
668 create symbols "A", "A::B", and "A::B::C". If it is then later
669 called with argument "A::B::D::member", then the new call will
670 generate a new symbol for "A::B::D", but once it sees that "A::B"
671 has already been created, it doesn't bother checking to see if "A"
672 has also been created. */
675 check_possible_namespace_symbols_loop (const char *name
, int len
,
676 struct objfile
*objfile
)
678 if (name
[len
] == ':')
681 int next_len
= len
+ 2;
683 next_len
+= cp_find_first_component (name
+ next_len
);
684 done
= check_possible_namespace_symbols_loop (name
, next_len
,
688 done
= check_one_possible_namespace_symbol (name
, len
, objfile
);
696 /* Check to see if there's already a possible namespace symbol in
697 OBJFILE whose name is the initial substring of NAME of length LEN.
698 If not, create one and return 0; otherwise, return 1. */
701 check_one_possible_namespace_symbol (const char *name
, int len
,
702 struct objfile
*objfile
)
704 struct block
*block
= get_possible_namespace_block (objfile
);
705 char *name_copy
= obsavestring (name
, len
, &objfile
->symbol_obstack
);
706 struct symbol
*sym
= lookup_block_symbol (block
, name_copy
, NULL
,
711 struct type
*type
= init_type (TYPE_CODE_NAMESPACE
, 0, 0,
713 TYPE_TAG_NAME (type
) = TYPE_NAME (type
);
715 sym
= obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct symbol
));
716 memset (sym
, 0, sizeof (struct symbol
));
717 SYMBOL_LANGUAGE (sym
) = language_cplus
;
718 SYMBOL_SET_NAMES (sym
, name_copy
, len
, objfile
);
719 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
720 SYMBOL_TYPE (sym
) = type
;
721 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
723 dict_add_symbol (BLOCK_DICT (block
), sym
);
729 obstack_free (&objfile
->symbol_obstack
, name_copy
);
735 /* Look for a symbol named NAME in all the possible namespace blocks.
736 If one is found, return it; if SYMTAB is non-NULL, set *SYMTAB to
737 equal the symtab where it was found. */
739 static struct symbol
*
740 lookup_possible_namespace_symbol (const char *name
, struct symtab
**symtab
)
742 struct objfile
*objfile
;
744 ALL_OBJFILES (objfile
)
748 sym
= lookup_block_symbol (get_possible_namespace_block (objfile
),
749 name
, NULL
, VAR_DOMAIN
);
754 *symtab
= objfile
->cp_namespace_symtab
;
763 /* Print out all the possible namespace symbols. */
766 maintenance_cplus_namespace (char *args
, int from_tty
)
768 struct objfile
*objfile
;
769 printf_unfiltered ("Possible namespaces:\n");
770 ALL_OBJFILES (objfile
)
772 struct dict_iterator iter
;
775 ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile
), iter
, sym
)
777 printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym
));
783 _initialize_cp_namespace (void)
785 add_cmd ("namespace", class_maintenance
, maintenance_cplus_namespace
,
786 "Print the list of possible C++ namespaces.",
787 &maint_cplus_cmd_list
);