Update
[gdb.git] / gdb / cp-namespace.c
blob9b3dd812c1787e2bfd31fe14ec4b4666536752a5
1 /* Helper routines for C++ support in GDB.
2 Copyright (C) 2003, 2004, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "cp-support.h"
23 #include "gdb_obstack.h"
24 #include "symtab.h"
25 #include "symfile.h"
26 #include "gdb_assert.h"
27 #include "block.h"
28 #include "objfiles.h"
29 #include "gdbtypes.h"
30 #include "dictionary.h"
31 #include "command.h"
32 #include "frame.h"
34 /* When set, the file that we're processing is known to have debugging
35 info for C++ namespaces. */
37 /* NOTE: carlton/2004-01-13: No currently released version of GCC (the
38 latest of which is 3.3.x at the time of this writing) produces this
39 debug info. GCC 3.4 should, however. */
41 unsigned char processing_has_namespace_info;
43 /* This contains our best guess as to the name of the current
44 enclosing namespace(s)/class(es), if any. For example, if we're
45 within the method foo() in the following code:
47 namespace N {
48 class C {
49 void foo () {
54 then processing_current_prefix should be set to "N::C". If
55 processing_has_namespace_info is false, then this variable might
56 not be reliable. */
58 const char *processing_current_prefix;
60 /* List of using directives that are active in the current file. */
62 static struct using_direct *using_list;
64 static struct using_direct *cp_add_using (const char *name,
65 unsigned int inner_len,
66 unsigned int outer_len,
67 struct using_direct *next);
69 static struct using_direct *cp_copy_usings (struct using_direct *using,
70 struct obstack *obstack);
72 static struct symbol *lookup_namespace_scope (const char *name,
73 const char *linkage_name,
74 const struct block *block,
75 const domain_enum domain,
76 struct symtab **symtab,
77 const char *scope,
78 int scope_len);
80 static struct symbol *lookup_symbol_file (const char *name,
81 const char *linkage_name,
82 const struct block *block,
83 const domain_enum domain,
84 struct symtab **symtab,
85 int anonymous_namespace);
87 static struct type *cp_lookup_transparent_type_loop (const char *name,
88 const char *scope,
89 int scope_len);
91 static void initialize_namespace_symtab (struct objfile *objfile);
93 static struct block *get_possible_namespace_block (struct objfile *objfile);
95 static void free_namespace_block (struct symtab *symtab);
97 static int check_possible_namespace_symbols_loop (const char *name,
98 int len,
99 struct objfile *objfile);
101 static int check_one_possible_namespace_symbol (const char *name,
102 int len,
103 struct objfile *objfile);
105 static
106 struct symbol *lookup_possible_namespace_symbol (const char *name,
107 struct symtab **symtab);
109 static void maintenance_cplus_namespace (char *args, int from_tty);
111 /* Set up support for dealing with C++ namespace info in the current
112 symtab. */
114 void cp_initialize_namespace ()
116 processing_has_namespace_info = 0;
117 using_list = NULL;
120 /* Add all the using directives we've gathered to the current symtab.
121 STATIC_BLOCK should be the symtab's static block; OBSTACK is used
122 for allocation. */
124 void
125 cp_finalize_namespace (struct block *static_block,
126 struct obstack *obstack)
128 if (using_list != NULL)
130 block_set_using (static_block,
131 cp_copy_usings (using_list, obstack),
132 obstack);
133 using_list = NULL;
137 /* Check to see if SYMBOL refers to an object contained within an
138 anonymous namespace; if so, add an appropriate using directive. */
140 /* Optimize away strlen ("(anonymous namespace)"). */
142 #define ANONYMOUS_NAMESPACE_LEN 21
144 void
145 cp_scan_for_anonymous_namespaces (const struct symbol *symbol)
147 if (!processing_has_namespace_info
148 && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
150 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
151 unsigned int previous_component;
152 unsigned int next_component;
153 const char *len;
155 /* Start with a quick-and-dirty check for mention of "(anonymous
156 namespace)". */
158 if (!cp_is_anonymous (name))
159 return;
161 previous_component = 0;
162 next_component = cp_find_first_component (name + previous_component);
164 while (name[next_component] == ':')
166 if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
167 && strncmp (name + previous_component,
168 "(anonymous namespace)",
169 ANONYMOUS_NAMESPACE_LEN) == 0)
171 /* We've found a component of the name that's an
172 anonymous namespace. So add symbols in it to the
173 namespace given by the previous component if there is
174 one, or to the global namespace if there isn't. */
175 cp_add_using_directive (name,
176 previous_component == 0
177 ? 0 : previous_component - 2,
178 next_component);
180 /* The "+ 2" is for the "::". */
181 previous_component = next_component + 2;
182 next_component = (previous_component
183 + cp_find_first_component (name
184 + previous_component));
189 /* Add a using directive to using_list. NAME is the start of a string
190 that should contain the namespaces we want to add as initial
191 substrings, OUTER_LENGTH is the end of the outer namespace, and
192 INNER_LENGTH is the end of the inner namespace. If the using
193 directive in question has already been added, don't add it
194 twice. */
196 void
197 cp_add_using_directive (const char *name, unsigned int outer_length,
198 unsigned int inner_length)
200 struct using_direct *current;
201 struct using_direct *new;
203 /* Has it already been added? */
205 for (current = using_list; current != NULL; current = current->next)
207 if ((strncmp (current->inner, name, inner_length) == 0)
208 && (strlen (current->inner) == inner_length)
209 && (strlen (current->outer) == outer_length))
210 return;
213 using_list = cp_add_using (name, inner_length, outer_length,
214 using_list);
217 /* Record the namespace that the function defined by SYMBOL was
218 defined in, if necessary. BLOCK is the associated block; use
219 OBSTACK for allocation. */
221 void
222 cp_set_block_scope (const struct symbol *symbol,
223 struct block *block,
224 struct obstack *obstack)
226 /* Make sure that the name was originally mangled: if not, there
227 certainly isn't any namespace information to worry about! */
229 if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
231 if (processing_has_namespace_info)
233 block_set_scope
234 (block, obsavestring (processing_current_prefix,
235 strlen (processing_current_prefix),
236 obstack),
237 obstack);
239 else
241 /* Try to figure out the appropriate namespace from the
242 demangled name. */
244 /* FIXME: carlton/2003-04-15: If the function in question is
245 a method of a class, the name will actually include the
246 name of the class as well. This should be harmless, but
247 is a little unfortunate. */
249 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
250 unsigned int prefix_len = cp_entire_prefix_len (name);
252 block_set_scope (block,
253 obsavestring (name, prefix_len, obstack),
254 obstack);
259 /* Test whether or not NAMESPACE looks like it mentions an anonymous
260 namespace; return nonzero if so. */
263 cp_is_anonymous (const char *namespace)
265 return (strstr (namespace, "(anonymous namespace)")
266 != NULL);
269 /* Create a new struct using direct whose inner namespace is the
270 initial substring of NAME of leng INNER_LEN and whose outer
271 namespace is the initial substring of NAME of length OUTER_LENGTH.
272 Set its next member in the linked list to NEXT; allocate all memory
273 using xmalloc. It copies the strings, so NAME can be a temporary
274 string. */
276 static struct using_direct *
277 cp_add_using (const char *name,
278 unsigned int inner_len,
279 unsigned int outer_len,
280 struct using_direct *next)
282 struct using_direct *retval;
284 gdb_assert (outer_len < inner_len);
286 retval = xmalloc (sizeof (struct using_direct));
287 retval->inner = savestring (name, inner_len);
288 retval->outer = savestring (name, outer_len);
289 retval->next = next;
291 return retval;
294 /* Make a copy of the using directives in the list pointed to by
295 USING, using OBSTACK to allocate memory. Free all memory pointed
296 to by USING via xfree. */
298 static struct using_direct *
299 cp_copy_usings (struct using_direct *using,
300 struct obstack *obstack)
302 if (using == NULL)
304 return NULL;
306 else
308 struct using_direct *retval
309 = obstack_alloc (obstack, sizeof (struct using_direct));
310 retval->inner = obsavestring (using->inner, strlen (using->inner),
311 obstack);
312 retval->outer = obsavestring (using->outer, strlen (using->outer),
313 obstack);
314 retval->next = cp_copy_usings (using->next, obstack);
316 xfree (using->inner);
317 xfree (using->outer);
318 xfree (using);
320 return retval;
324 /* The C++-specific version of name lookup for static and global
325 names. This makes sure that names get looked for in all namespaces
326 that are in scope. NAME is the natural name of the symbol that
327 we're looking for, LINKAGE_NAME (which is optional) is its linkage
328 name, BLOCK is the block that we're searching within, DOMAIN says
329 what kind of symbols we're looking for, and if SYMTAB is non-NULL,
330 we should store the symtab where we found the symbol in it. */
332 struct symbol *
333 cp_lookup_symbol_nonlocal (const char *name,
334 const char *linkage_name,
335 const struct block *block,
336 const domain_enum domain,
337 struct symtab **symtab)
339 return lookup_namespace_scope (name, linkage_name, block, domain,
340 symtab, block_scope (block), 0);
343 /* Lookup NAME at namespace scope (or, in C terms, in static and
344 global variables). SCOPE is the namespace that the current
345 function is defined within; only consider namespaces whose length
346 is at least SCOPE_LEN. Other arguments are as in
347 cp_lookup_symbol_nonlocal.
349 For example, if we're within a function A::B::f and looking for a
350 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
351 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
352 but with SCOPE_LEN = 1. And then it calls itself with NAME and
353 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
354 "A::B::x"; if it doesn't find it, then the second call looks for
355 "A::x", and if that call fails, then the first call looks for
356 "x". */
358 static struct symbol *
359 lookup_namespace_scope (const char *name,
360 const char *linkage_name,
361 const struct block *block,
362 const domain_enum domain,
363 struct symtab **symtab,
364 const char *scope,
365 int scope_len)
367 char *namespace;
369 if (scope[scope_len] != '\0')
371 /* Recursively search for names in child namespaces first. */
373 struct symbol *sym;
374 int new_scope_len = scope_len;
376 /* If the current scope is followed by "::", skip past that. */
377 if (new_scope_len != 0)
379 gdb_assert (scope[new_scope_len] == ':');
380 new_scope_len += 2;
382 new_scope_len += cp_find_first_component (scope + new_scope_len);
383 sym = lookup_namespace_scope (name, linkage_name, block,
384 domain, symtab,
385 scope, new_scope_len);
386 if (sym != NULL)
387 return sym;
390 /* Okay, we didn't find a match in our children, so look for the
391 name in the current namespace. */
393 namespace = alloca (scope_len + 1);
394 strncpy (namespace, scope, scope_len);
395 namespace[scope_len] = '\0';
396 return cp_lookup_symbol_namespace (namespace, name, linkage_name,
397 block, domain, symtab);
400 /* Look up NAME in the C++ namespace NAMESPACE, applying the using
401 directives that are active in BLOCK. Other arguments are as in
402 cp_lookup_symbol_nonlocal. */
404 struct symbol *
405 cp_lookup_symbol_namespace (const char *namespace,
406 const char *name,
407 const char *linkage_name,
408 const struct block *block,
409 const domain_enum domain,
410 struct symtab **symtab)
412 const struct using_direct *current;
413 struct symbol *sym;
415 /* First, go through the using directives. If any of them add new
416 names to the namespace we're searching in, see if we can find a
417 match by applying them. */
419 for (current = block_using (block);
420 current != NULL;
421 current = current->next)
423 if (strcmp (namespace, current->outer) == 0)
425 sym = cp_lookup_symbol_namespace (current->inner,
426 name,
427 linkage_name,
428 block,
429 domain,
430 symtab);
431 if (sym != NULL)
432 return sym;
436 /* We didn't find anything by applying any of the using directives
437 that are still applicable; so let's see if we've got a match
438 using the current namespace. */
440 if (namespace[0] == '\0')
442 return lookup_symbol_file (name, linkage_name, block,
443 domain, symtab, 0);
445 else
447 char *concatenated_name
448 = alloca (strlen (namespace) + 2 + strlen (name) + 1);
449 strcpy (concatenated_name, namespace);
450 strcat (concatenated_name, "::");
451 strcat (concatenated_name, name);
452 sym = lookup_symbol_file (concatenated_name, linkage_name,
453 block, domain, symtab,
454 cp_is_anonymous (namespace));
455 return sym;
459 /* Look up NAME in BLOCK's static block and in global blocks. If
460 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
461 within an anonymous namespace. Other arguments are as in
462 cp_lookup_symbol_nonlocal. */
464 static struct symbol *
465 lookup_symbol_file (const char *name,
466 const char *linkage_name,
467 const struct block *block,
468 const domain_enum domain,
469 struct symtab **symtab,
470 int anonymous_namespace)
472 struct symbol *sym = NULL;
474 sym = lookup_symbol_static (name, linkage_name, block, domain, symtab);
475 if (sym != NULL)
476 return sym;
478 if (anonymous_namespace)
480 /* Symbols defined in anonymous namespaces have external linkage
481 but should be treated as local to a single file nonetheless.
482 So we only search the current file's global block. */
484 const struct block *global_block = block_global_block (block);
486 if (global_block != NULL)
487 sym = lookup_symbol_aux_block (name, linkage_name, global_block,
488 domain, symtab);
490 else
492 sym = lookup_symbol_global (name, linkage_name, block, domain, symtab);
495 if (sym != NULL)
496 return sym;
498 /* Now call "lookup_possible_namespace_symbol". Symbols in here
499 claim to be associated to namespaces, but this claim might be
500 incorrect: the names in question might actually correspond to
501 classes instead of namespaces. But if they correspond to
502 classes, then we should have found a match for them above. So if
503 we find them now, they should be genuine. */
505 /* FIXME: carlton/2003-06-12: This is a hack and should eventually
506 be deleted: see comments below. */
508 if (domain == VAR_DOMAIN)
510 sym = lookup_possible_namespace_symbol (name, symtab);
511 if (sym != NULL)
512 return sym;
515 return NULL;
518 /* Look up a type named NESTED_NAME that is nested inside the C++
519 class or namespace given by PARENT_TYPE, from within the context
520 given by BLOCK. Return NULL if there is no such nested type. */
522 struct type *
523 cp_lookup_nested_type (struct type *parent_type,
524 const char *nested_name,
525 const struct block *block)
527 switch (TYPE_CODE (parent_type))
529 case TYPE_CODE_STRUCT:
530 case TYPE_CODE_NAMESPACE:
532 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
533 of classes like, say, data or function members. Instead,
534 they're just represented by symbols whose names are
535 qualified by the name of the surrounding class. This is
536 just like members of namespaces; in particular,
537 lookup_symbol_namespace works when looking them up. */
539 const char *parent_name = TYPE_TAG_NAME (parent_type);
540 struct symbol *sym = cp_lookup_symbol_namespace (parent_name,
541 nested_name,
542 NULL,
543 block,
544 VAR_DOMAIN,
545 NULL);
546 if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
547 return NULL;
548 else
549 return SYMBOL_TYPE (sym);
551 default:
552 internal_error (__FILE__, __LINE__,
553 _("cp_lookup_nested_type called on a non-aggregate type."));
557 /* The C++-version of lookup_transparent_type. */
559 /* FIXME: carlton/2004-01-16: The problem that this is trying to
560 address is that, unfortunately, sometimes NAME is wrong: it may not
561 include the name of namespaces enclosing the type in question.
562 lookup_transparent_type gets called when the the type in question
563 is a declaration, and we're trying to find its definition; but, for
564 declarations, our type name deduction mechanism doesn't work.
565 There's nothing we can do to fix this in general, I think, in the
566 absence of debug information about namespaces (I've filed PR
567 gdb/1511 about this); until such debug information becomes more
568 prevalent, one heuristic which sometimes looks is to search for the
569 definition in namespaces containing the current namespace.
571 We should delete this functions once the appropriate debug
572 information becomes more widespread. (GCC 3.4 will be the first
573 released version of GCC with such information.) */
575 struct type *
576 cp_lookup_transparent_type (const char *name)
578 /* First, try the honest way of looking up the definition. */
579 struct type *t = basic_lookup_transparent_type (name);
580 const char *scope;
582 if (t != NULL)
583 return t;
585 /* If that doesn't work and we're within a namespace, look there
586 instead. */
587 scope = block_scope (get_selected_block (0));
589 if (scope[0] == '\0')
590 return NULL;
592 return cp_lookup_transparent_type_loop (name, scope, 0);
595 /* Lookup the the type definition associated to NAME in
596 namespaces/classes containing SCOPE whose name is strictly longer
597 than LENGTH. LENGTH must be the index of the start of a
598 component of SCOPE. */
600 static struct type *
601 cp_lookup_transparent_type_loop (const char *name, const char *scope,
602 int length)
604 int scope_length = length + cp_find_first_component (scope + length);
605 char *full_name;
607 /* If the current scope is followed by "::", look in the next
608 component. */
609 if (scope[scope_length] == ':')
611 struct type *retval
612 = cp_lookup_transparent_type_loop (name, scope, scope_length + 2);
613 if (retval != NULL)
614 return retval;
617 full_name = alloca (scope_length + 2 + strlen (name) + 1);
618 strncpy (full_name, scope, scope_length);
619 strncpy (full_name + scope_length, "::", 2);
620 strcpy (full_name + scope_length + 2, name);
622 return basic_lookup_transparent_type (full_name);
625 /* Now come functions for dealing with symbols associated to
626 namespaces. (They're used to store the namespaces themselves, not
627 objects that live in the namespaces.) These symbols come in two
628 varieties: if we run into a DW_TAG_namespace DIE, then we know that
629 we have a namespace, so dwarf2read.c creates a symbol for it just
630 like normal. But, unfortunately, versions of GCC through at least
631 3.3 don't generate those DIE's. Our solution is to try to guess
632 their existence by looking at demangled names. This might cause us
633 to misidentify classes as namespaces, however. So we put those
634 symbols in a special block (one per objfile), and we only search
635 that block as a last resort. */
637 /* FIXME: carlton/2003-06-12: Once versions of GCC that generate
638 DW_TAG_namespace have been out for a year or two, we should get rid
639 of all of this "possible namespace" nonsense. */
641 /* Allocate everything necessary for the possible namespace block
642 associated to OBJFILE. */
644 static void
645 initialize_namespace_symtab (struct objfile *objfile)
647 struct symtab *namespace_symtab;
648 struct blockvector *bv;
649 struct block *bl;
651 namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile);
652 namespace_symtab->language = language_cplus;
653 namespace_symtab->free_code = free_nothing;
654 namespace_symtab->dirname = NULL;
656 bv = obstack_alloc (&objfile->objfile_obstack,
657 sizeof (struct blockvector)
658 + FIRST_LOCAL_BLOCK * sizeof (struct block *));
659 BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1;
660 BLOCKVECTOR (namespace_symtab) = bv;
662 /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */
664 bl = allocate_block (&objfile->objfile_obstack);
665 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
666 NULL);
667 BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
668 bl = allocate_block (&objfile->objfile_obstack);
669 BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
670 NULL);
671 BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
673 /* Allocate the possible namespace block; we put it where the first
674 local block will live, though I don't think there's any need to
675 pretend that it's actually a local block (e.g. by setting
676 BLOCK_SUPERBLOCK appropriately). We don't use the global or
677 static block because we don't want it searched during the normal
678 search of all global/static blocks in lookup_symbol: we only want
679 it used as a last resort. */
681 /* NOTE: carlton/2003-09-11: I considered not associating the fake
682 symbols to a block/symtab at all. But that would cause problems
683 with lookup_symbol's SYMTAB argument and with block_found, so
684 having a symtab/block for this purpose seems like the best
685 solution for now. */
687 bl = allocate_block (&objfile->objfile_obstack);
688 BLOCK_DICT (bl) = dict_create_hashed_expandable ();
689 BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl;
691 namespace_symtab->free_func = free_namespace_block;
693 objfile->cp_namespace_symtab = namespace_symtab;
696 /* Locate the possible namespace block associated to OBJFILE,
697 allocating it if necessary. */
699 static struct block *
700 get_possible_namespace_block (struct objfile *objfile)
702 if (objfile->cp_namespace_symtab == NULL)
703 initialize_namespace_symtab (objfile);
705 return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab),
706 FIRST_LOCAL_BLOCK);
709 /* Free the dictionary associated to the possible namespace block. */
711 static void
712 free_namespace_block (struct symtab *symtab)
714 struct block *possible_namespace_block;
716 possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
717 FIRST_LOCAL_BLOCK);
718 gdb_assert (possible_namespace_block != NULL);
719 dict_free (BLOCK_DICT (possible_namespace_block));
722 /* Ensure that there are symbols in the possible namespace block
723 associated to OBJFILE for all initial substrings of NAME that look
724 like namespaces or classes. NAME should end in a member variable:
725 it shouldn't consist solely of namespaces. */
727 void
728 cp_check_possible_namespace_symbols (const char *name, struct objfile *objfile)
730 check_possible_namespace_symbols_loop (name,
731 cp_find_first_component (name),
732 objfile);
735 /* This is a helper loop for cp_check_possible_namespace_symbols; it
736 ensures that there are symbols in the possible namespace block
737 associated to OBJFILE for all namespaces that are initial
738 substrings of NAME of length at least LEN. It returns 1 if a
739 previous loop had already created the shortest such symbol and 0
740 otherwise.
742 This function assumes that if there is already a symbol associated
743 to a substring of NAME of a given length, then there are already
744 symbols associated to all substrings of NAME whose length is less
745 than that length. So if cp_check_possible_namespace_symbols has
746 been called once with argument "A::B::C::member", then that will
747 create symbols "A", "A::B", and "A::B::C". If it is then later
748 called with argument "A::B::D::member", then the new call will
749 generate a new symbol for "A::B::D", but once it sees that "A::B"
750 has already been created, it doesn't bother checking to see if "A"
751 has also been created. */
753 static int
754 check_possible_namespace_symbols_loop (const char *name, int len,
755 struct objfile *objfile)
757 if (name[len] == ':')
759 int done;
760 int next_len = len + 2;
762 next_len += cp_find_first_component (name + next_len);
763 done = check_possible_namespace_symbols_loop (name, next_len,
764 objfile);
766 if (!done)
767 done = check_one_possible_namespace_symbol (name, len, objfile);
769 return done;
771 else
772 return 0;
775 /* Check to see if there's already a possible namespace symbol in
776 OBJFILE whose name is the initial substring of NAME of length LEN.
777 If not, create one and return 0; otherwise, return 1. */
779 static int
780 check_one_possible_namespace_symbol (const char *name, int len,
781 struct objfile *objfile)
783 struct block *block = get_possible_namespace_block (objfile);
784 char *name_copy = alloca (len + 1);
785 struct symbol *sym;
787 memcpy (name_copy, name, len);
788 name_copy[len] = '\0';
789 sym = lookup_block_symbol (block, name_copy, NULL, VAR_DOMAIN);
791 if (sym == NULL)
793 struct type *type;
794 name_copy = obsavestring (name, len, &objfile->objfile_obstack);
796 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, name_copy, objfile);
798 TYPE_TAG_NAME (type) = TYPE_NAME (type);
800 sym = obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol));
801 memset (sym, 0, sizeof (struct symbol));
802 SYMBOL_LANGUAGE (sym) = language_cplus;
803 SYMBOL_SET_NAMES (sym, name_copy, len, objfile);
804 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
805 SYMBOL_TYPE (sym) = type;
806 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
808 dict_add_symbol (BLOCK_DICT (block), sym);
810 return 0;
812 else
813 return 1;
816 /* Look for a symbol named NAME in all the possible namespace blocks.
817 If one is found, return it; if SYMTAB is non-NULL, set *SYMTAB to
818 equal the symtab where it was found. */
820 static struct symbol *
821 lookup_possible_namespace_symbol (const char *name, struct symtab **symtab)
823 struct objfile *objfile;
825 ALL_OBJFILES (objfile)
827 struct symbol *sym;
829 sym = lookup_block_symbol (get_possible_namespace_block (objfile),
830 name, NULL, VAR_DOMAIN);
832 if (sym != NULL)
834 if (symtab != NULL)
835 *symtab = objfile->cp_namespace_symtab;
837 return sym;
841 return NULL;
844 /* Print out all the possible namespace symbols. */
846 static void
847 maintenance_cplus_namespace (char *args, int from_tty)
849 struct objfile *objfile;
850 printf_unfiltered (_("Possible namespaces:\n"));
851 ALL_OBJFILES (objfile)
853 struct dict_iterator iter;
854 struct symbol *sym;
856 ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym)
858 printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym));
863 void
864 _initialize_cp_namespace (void)
866 add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace,
867 _("Print the list of possible C++ namespaces."),
868 &maint_cplus_cmd_list);