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 f, this will get called with NAME = "f", 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 /* Now come functions for dealing with symbols associated to
512 namespaces. (They're used to store the namespaces themselves, not
513 objects that live in the namespaces.) These symbols come in two
514 varieties: if we run into a DW_TAG_namespace DIE, then we know that
515 we have a namespace, so dwarf2read.c creates a symbol for it just
516 like normal. But, unfortunately, versions of GCC through at least
517 3.3 don't generate those DIE's. Our solution is to try to guess
518 their existence by looking at demangled names. This might cause us
519 to misidentify classes as namespaces, however. So we put those
520 symbols in a special block (one per objfile), and we only search
521 that block as a last resort. */
523 /* FIXME: carlton/2003-06-12: Once versions of GCC that generate
524 DW_TAG_namespace have been out for a year or two, we should get rid
525 of all of this "possible namespace" nonsense. */
527 /* Allocate everything necessary for the possible namespace block
528 associated to OBJFILE. */
531 initialize_namespace_symtab (struct objfile
*objfile
)
533 struct symtab
*namespace_symtab
;
534 struct blockvector
*bv
;
537 namespace_symtab
= allocate_symtab ("<<C++-namespaces>>", objfile
);
538 namespace_symtab
->language
= language_cplus
;
539 namespace_symtab
->free_code
= free_nothing
;
540 namespace_symtab
->dirname
= NULL
;
542 bv
= obstack_alloc (&objfile
->symbol_obstack
,
543 sizeof (struct blockvector
)
544 + FIRST_LOCAL_BLOCK
* sizeof (struct block
*));
545 BLOCKVECTOR_NBLOCKS (bv
) = FIRST_LOCAL_BLOCK
+ 1;
546 BLOCKVECTOR (namespace_symtab
) = bv
;
548 /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */
550 bl
= allocate_block (&objfile
->symbol_obstack
);
551 BLOCK_DICT (bl
) = dict_create_linear (&objfile
->symbol_obstack
,
553 BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
) = bl
;
554 bl
= allocate_block (&objfile
->symbol_obstack
);
555 BLOCK_DICT (bl
) = dict_create_linear (&objfile
->symbol_obstack
,
557 BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
) = bl
;
559 /* Allocate the possible namespace block; we put it where the first
560 local block will live, though I don't think there's any need to
561 pretend that it's actually a local block (e.g. by setting
562 BLOCK_SUPERBLOCK appropriately). We don't use the global or
563 static block because we don't want it searched during the normal
564 search of all global/static blocks in lookup_symbol: we only want
565 it used as a last resort. */
567 /* NOTE: carlton/2003-09-11: I considered not associating the fake
568 symbols to a block/symtab at all. But that would cause problems
569 with lookup_symbol's SYMTAB argument and with block_found, so
570 having a symtab/block for this purpose seems like the best
573 bl
= allocate_block (&objfile
->symbol_obstack
);
574 BLOCK_DICT (bl
) = dict_create_hashed_expandable ();
575 BLOCKVECTOR_BLOCK (bv
, FIRST_LOCAL_BLOCK
) = bl
;
577 namespace_symtab
->free_func
= free_namespace_block
;
579 objfile
->cp_namespace_symtab
= namespace_symtab
;
582 /* Locate the possible namespace block associated to OBJFILE,
583 allocating it if necessary. */
585 static struct block
*
586 get_possible_namespace_block (struct objfile
*objfile
)
588 if (objfile
->cp_namespace_symtab
== NULL
)
589 initialize_namespace_symtab (objfile
);
591 return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile
->cp_namespace_symtab
),
595 /* Free the dictionary associated to the possible namespace block. */
598 free_namespace_block (struct symtab
*symtab
)
600 struct block
*possible_namespace_block
;
602 possible_namespace_block
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab
),
604 gdb_assert (possible_namespace_block
!= NULL
);
605 dict_free (BLOCK_DICT (possible_namespace_block
));
608 /* Ensure that there are symbols in the possible namespace block
609 associated to OBJFILE for all initial substrings of NAME that look
610 like namespaces or classes. NAME should end in a member variable:
611 it shouldn't consist solely of namespaces. */
614 cp_check_possible_namespace_symbols (const char *name
, struct objfile
*objfile
)
616 check_possible_namespace_symbols_loop (name
,
617 cp_find_first_component (name
),
621 /* This is a helper loop for cp_check_possible_namespace_symbols; it
622 ensures that there are symbols in the possible namespace block
623 associated to OBJFILE for all namespaces that are initial
624 substrings of NAME of length at least LEN. It returns 1 if a
625 previous loop had already created the shortest such symbol and 0
628 This function assumes that if there is already a symbol associated
629 to a substring of NAME of a given length, then there are already
630 symbols associated to all substrings of NAME whose length is less
631 than that length. So if cp_check_possible_namespace_symbols has
632 been called once with argument "A::B::C::member", then that will
633 create symbols "A", "A::B", and "A::B::C". If it is then later
634 called with argument "A::B::D::member", then the new call will
635 generate a new symbol for "A::B::D", but once it sees that "A::B"
636 has already been created, it doesn't bother checking to see if "A"
637 has also been created. */
640 check_possible_namespace_symbols_loop (const char *name
, int len
,
641 struct objfile
*objfile
)
643 if (name
[len
] == ':')
646 int next_len
= len
+ 2;
648 next_len
+= cp_find_first_component (name
+ next_len
);
649 done
= check_possible_namespace_symbols_loop (name
, next_len
,
653 done
= check_one_possible_namespace_symbol (name
, len
, objfile
);
661 /* Check to see if there's already a possible namespace symbol in
662 OBJFILE whose name is the initial substring of NAME of length LEN.
663 If not, create one and return 0; otherwise, return 1. */
666 check_one_possible_namespace_symbol (const char *name
, int len
,
667 struct objfile
*objfile
)
669 struct block
*block
= get_possible_namespace_block (objfile
);
670 char *name_copy
= obsavestring (name
, len
, &objfile
->symbol_obstack
);
671 struct symbol
*sym
= lookup_block_symbol (block
, name_copy
, NULL
,
676 struct type
*type
= init_type (TYPE_CODE_NAMESPACE
, 0, 0,
678 TYPE_TAG_NAME (type
) = TYPE_NAME (type
);
680 sym
= obstack_alloc (&objfile
->symbol_obstack
, sizeof (struct symbol
));
681 memset (sym
, 0, sizeof (struct symbol
));
682 SYMBOL_LANGUAGE (sym
) = language_cplus
;
683 SYMBOL_SET_NAMES (sym
, name_copy
, len
, objfile
);
684 SYMBOL_CLASS (sym
) = LOC_TYPEDEF
;
685 SYMBOL_TYPE (sym
) = type
;
686 SYMBOL_DOMAIN (sym
) = VAR_DOMAIN
;
688 dict_add_symbol (BLOCK_DICT (block
), sym
);
694 obstack_free (&objfile
->symbol_obstack
, name_copy
);
700 /* Look for a symbol named NAME in all the possible namespace blocks.
701 If one is found, return it; if SYMTAB is non-NULL, set *SYMTAB to
702 equal the symtab where it was found. */
704 static struct symbol
*
705 lookup_possible_namespace_symbol (const char *name
, struct symtab
**symtab
)
707 struct objfile
*objfile
;
709 ALL_OBJFILES (objfile
)
713 sym
= lookup_block_symbol (get_possible_namespace_block (objfile
),
714 name
, NULL
, VAR_DOMAIN
);
719 *symtab
= objfile
->cp_namespace_symtab
;
728 /* Print out all the possible namespace symbols. */
731 maintenance_cplus_namespace (char *args
, int from_tty
)
733 struct objfile
*objfile
;
734 printf_unfiltered ("Possible namespaces:\n");
735 ALL_OBJFILES (objfile
)
737 struct dict_iterator iter
;
740 ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile
), iter
, sym
)
742 printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym
));
748 _initialize_cp_namespace (void)
750 add_cmd ("namespace", class_maintenance
, maintenance_cplus_namespace
,
751 "Print the list of possible C++ namespaces.",
752 &maint_cplus_cmd_list
);