* language.h (struct language_defn): Remove SYMTAB parameter from
[external/binutils.git] / gdb / cp-namespace.c
1 /* Helper routines for C++ support in GDB.
2    Copyright (C) 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
3
4    Contributed by David Carlton and by Kealia, Inc.
5
6    This file is part of GDB.
7
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.
12
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.
17
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/>.  */
20
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"
33
34 /* When set, the file that we're processing is known to have debugging
35    info for C++ namespaces.  */
36
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.  */
40
41 unsigned char processing_has_namespace_info;
42
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:
46
47     namespace N {
48       class C {
49         void foo () {
50         }
51       };
52     }
53
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.  */
57
58 const char *processing_current_prefix;
59
60 /* List of using directives that are active in the current file.  */
61
62 static struct using_direct *using_list;
63
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);
68
69 static struct using_direct *cp_copy_usings (struct using_direct *using,
70                                             struct obstack *obstack);
71
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                                               const char *scope,
77                                               int scope_len);
78
79 static struct symbol *lookup_symbol_file (const char *name,
80                                           const char *linkage_name,
81                                           const struct block *block,
82                                           const domain_enum domain,
83                                           int anonymous_namespace);
84
85 static struct type *cp_lookup_transparent_type_loop (const char *name,
86                                                      const char *scope,
87                                                      int scope_len);
88
89 static void initialize_namespace_symtab (struct objfile *objfile);
90
91 static struct block *get_possible_namespace_block (struct objfile *objfile);
92
93 static void free_namespace_block (struct symtab *symtab);
94
95 static int check_possible_namespace_symbols_loop (const char *name,
96                                                   int len,
97                                                   struct objfile *objfile);
98
99 static int check_one_possible_namespace_symbol (const char *name,
100                                                 int len,
101                                                 struct objfile *objfile);
102
103 static struct symbol *lookup_possible_namespace_symbol (const char *name);
104
105 static void maintenance_cplus_namespace (char *args, int from_tty);
106
107 /* Set up support for dealing with C++ namespace info in the current
108    symtab.  */
109
110 void cp_initialize_namespace ()
111 {
112   processing_has_namespace_info = 0;
113   using_list = NULL;
114 }
115
116 /* Add all the using directives we've gathered to the current symtab.
117    STATIC_BLOCK should be the symtab's static block; OBSTACK is used
118    for allocation.  */
119
120 void
121 cp_finalize_namespace (struct block *static_block,
122                        struct obstack *obstack)
123 {
124   if (using_list != NULL)
125     {
126       block_set_using (static_block,
127                        cp_copy_usings (using_list, obstack),
128                        obstack);
129       using_list = NULL;
130     }
131 }
132
133 /* Check to see if SYMBOL refers to an object contained within an
134    anonymous namespace; if so, add an appropriate using directive.  */
135
136 /* Optimize away strlen ("(anonymous namespace)").  */
137
138 #define ANONYMOUS_NAMESPACE_LEN 21
139
140 void
141 cp_scan_for_anonymous_namespaces (const struct symbol *symbol)
142 {
143   if (!processing_has_namespace_info
144       && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
145     {
146       const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
147       unsigned int previous_component;
148       unsigned int next_component;
149       const char *len;
150
151       /* Start with a quick-and-dirty check for mention of "(anonymous
152          namespace)".  */
153
154       if (!cp_is_anonymous (name))
155         return;
156
157       previous_component = 0;
158       next_component = cp_find_first_component (name + previous_component);
159
160       while (name[next_component] == ':')
161         {
162           if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
163               && strncmp (name + previous_component,
164                           "(anonymous namespace)",
165                           ANONYMOUS_NAMESPACE_LEN) == 0)
166             {
167               /* We've found a component of the name that's an
168                  anonymous namespace.  So add symbols in it to the
169                  namespace given by the previous component if there is
170                  one, or to the global namespace if there isn't.  */
171               cp_add_using_directive (name,
172                                       previous_component == 0
173                                       ? 0 : previous_component - 2,
174                                       next_component);
175             }
176           /* The "+ 2" is for the "::".  */
177           previous_component = next_component + 2;
178           next_component = (previous_component
179                             + cp_find_first_component (name
180                                                        + previous_component));
181         }
182     }
183 }
184
185 /* Add a using directive to using_list.  NAME is the start of a string
186    that should contain the namespaces we want to add as initial
187    substrings, OUTER_LENGTH is the end of the outer namespace, and
188    INNER_LENGTH is the end of the inner namespace.  If the using
189    directive in question has already been added, don't add it
190    twice.  */
191
192 void
193 cp_add_using_directive (const char *name, unsigned int outer_length,
194                         unsigned int inner_length)
195 {
196   struct using_direct *current;
197   struct using_direct *new;
198
199   /* Has it already been added?  */
200
201   for (current = using_list; current != NULL; current = current->next)
202     {
203       if ((strncmp (current->inner, name, inner_length) == 0)
204           && (strlen (current->inner) == inner_length)
205           && (strlen (current->outer) == outer_length))
206         return;
207     }
208
209   using_list = cp_add_using (name, inner_length, outer_length,
210                              using_list);
211 }
212
213 /* Record the namespace that the function defined by SYMBOL was
214    defined in, if necessary.  BLOCK is the associated block; use
215    OBSTACK for allocation.  */
216
217 void
218 cp_set_block_scope (const struct symbol *symbol,
219                     struct block *block,
220                     struct obstack *obstack)
221 {
222   /* Make sure that the name was originally mangled: if not, there
223      certainly isn't any namespace information to worry about!  */
224
225   if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
226     {
227       if (processing_has_namespace_info)
228         {
229           block_set_scope
230             (block, obsavestring (processing_current_prefix,
231                                   strlen (processing_current_prefix),
232                                   obstack),
233              obstack);
234         }
235       else
236         {
237           /* Try to figure out the appropriate namespace from the
238              demangled name.  */
239
240           /* FIXME: carlton/2003-04-15: If the function in question is
241              a method of a class, the name will actually include the
242              name of the class as well.  This should be harmless, but
243              is a little unfortunate.  */
244
245           const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
246           unsigned int prefix_len = cp_entire_prefix_len (name);
247
248           block_set_scope (block,
249                            obsavestring (name, prefix_len, obstack),
250                            obstack);
251         }
252     }
253 }
254
255 /* Test whether or not NAMESPACE looks like it mentions an anonymous
256    namespace; return nonzero if so.  */
257
258 int
259 cp_is_anonymous (const char *namespace)
260 {
261   return (strstr (namespace, "(anonymous namespace)")
262           != NULL);
263 }
264
265 /* Create a new struct using direct whose inner namespace is the
266    initial substring of NAME of leng INNER_LEN and whose outer
267    namespace is the initial substring of NAME of length OUTER_LENGTH.
268    Set its next member in the linked list to NEXT; allocate all memory
269    using xmalloc.  It copies the strings, so NAME can be a temporary
270    string.  */
271
272 static struct using_direct *
273 cp_add_using (const char *name,
274               unsigned int inner_len,
275               unsigned int outer_len,
276               struct using_direct *next)
277 {
278   struct using_direct *retval;
279
280   gdb_assert (outer_len < inner_len);
281
282   retval = xmalloc (sizeof (struct using_direct));
283   retval->inner = savestring (name, inner_len);
284   retval->outer = savestring (name, outer_len);
285   retval->next = next;
286
287   return retval;
288 }
289
290 /* Make a copy of the using directives in the list pointed to by
291    USING, using OBSTACK to allocate memory.  Free all memory pointed
292    to by USING via xfree.  */
293
294 static struct using_direct *
295 cp_copy_usings (struct using_direct *using,
296                 struct obstack *obstack)
297 {
298   if (using == NULL)
299     {
300       return NULL;
301     }
302   else
303     {
304       struct using_direct *retval
305         = obstack_alloc (obstack, sizeof (struct using_direct));
306       retval->inner = obsavestring (using->inner, strlen (using->inner),
307                                     obstack);
308       retval->outer = obsavestring (using->outer, strlen (using->outer),
309                                     obstack);
310       retval->next = cp_copy_usings (using->next, obstack);
311
312       xfree (using->inner);
313       xfree (using->outer);
314       xfree (using);
315
316       return retval;
317     }
318 }
319
320 /* The C++-specific version of name lookup for static and global
321    names.  This makes sure that names get looked for in all namespaces
322    that are in scope.  NAME is the natural name of the symbol that
323    we're looking for, LINKAGE_NAME (which is optional) is its linkage
324    name, BLOCK is the block that we're searching within, DOMAIN says
325    what kind of symbols we're looking for, and if SYMTAB is non-NULL,
326    we should store the symtab where we found the symbol in it.  */
327
328 struct symbol *
329 cp_lookup_symbol_nonlocal (const char *name,
330                            const char *linkage_name,
331                            const struct block *block,
332                            const domain_enum domain)
333 {
334   return lookup_namespace_scope (name, linkage_name, block, domain,
335                                  block_scope (block), 0);
336 }
337
338 /* Lookup NAME at namespace scope (or, in C terms, in static and
339    global variables).  SCOPE is the namespace that the current
340    function is defined within; only consider namespaces whose length
341    is at least SCOPE_LEN.  Other arguments are as in
342    cp_lookup_symbol_nonlocal.
343
344    For example, if we're within a function A::B::f and looking for a
345    symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
346    SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
347    but with SCOPE_LEN = 1.  And then it calls itself with NAME and
348    SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
349    "A::B::x"; if it doesn't find it, then the second call looks for
350    "A::x", and if that call fails, then the first call looks for
351    "x".  */
352
353 static struct symbol *
354 lookup_namespace_scope (const char *name,
355                         const char *linkage_name,
356                         const struct block *block,
357                         const domain_enum domain,
358                         const char *scope,
359                         int scope_len)
360 {
361   char *namespace;
362
363   if (scope[scope_len] != '\0')
364     {
365       /* Recursively search for names in child namespaces first.  */
366
367       struct symbol *sym;
368       int new_scope_len = scope_len;
369
370       /* If the current scope is followed by "::", skip past that.  */
371       if (new_scope_len != 0)
372         {
373           gdb_assert (scope[new_scope_len] == ':');
374           new_scope_len += 2;
375         }
376       new_scope_len += cp_find_first_component (scope + new_scope_len);
377       sym = lookup_namespace_scope (name, linkage_name, block,
378                                     domain, scope, new_scope_len);
379       if (sym != NULL)
380         return sym;
381     }
382
383   /* Okay, we didn't find a match in our children, so look for the
384      name in the current namespace.  */
385
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);
391 }
392
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.  */
396
397 struct symbol *
398 cp_lookup_symbol_namespace (const char *namespace,
399                             const char *name,
400                             const char *linkage_name,
401                             const struct block *block,
402                             const domain_enum domain)
403 {
404   const struct using_direct *current;
405   struct symbol *sym;
406
407   /* First, go through the using directives.  If any of them add new
408      names to the namespace we're searching in, see if we can find a
409      match by applying them.  */
410
411   for (current = block_using (block);
412        current != NULL;
413        current = current->next)
414     {
415       if (strcmp (namespace, current->outer) == 0)
416         {
417           sym = cp_lookup_symbol_namespace (current->inner,
418                                             name,
419                                             linkage_name,
420                                             block,
421                                             domain);
422           if (sym != NULL)
423             return sym;
424         }
425     }
426
427   /* We didn't find anything by applying any of the using directives
428      that are still applicable; so let's see if we've got a match
429      using the current namespace.  */
430   
431   if (namespace[0] == '\0')
432     {
433       return lookup_symbol_file (name, linkage_name, block,
434                                  domain, 0);
435     }
436   else
437     {
438       char *concatenated_name
439         = alloca (strlen (namespace) + 2 + strlen (name) + 1);
440       strcpy (concatenated_name, namespace);
441       strcat (concatenated_name, "::");
442       strcat (concatenated_name, name);
443       sym = lookup_symbol_file (concatenated_name, linkage_name,
444                                 block, domain, 
445                                 cp_is_anonymous (namespace));
446       return sym;
447     }
448 }
449
450 /* Look up NAME in BLOCK's static block and in global blocks.  If
451    ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
452    within an anonymous namespace.  Other arguments are as in
453    cp_lookup_symbol_nonlocal.  */
454
455 static struct symbol *
456 lookup_symbol_file (const char *name,
457                     const char *linkage_name,
458                     const struct block *block,
459                     const domain_enum domain,
460                     int anonymous_namespace)
461 {
462   struct symbol *sym = NULL;
463
464   sym = lookup_symbol_static (name, linkage_name, block, domain);
465   if (sym != NULL)
466     return sym;
467
468   if (anonymous_namespace)
469     {
470       /* Symbols defined in anonymous namespaces have external linkage
471          but should be treated as local to a single file nonetheless.
472          So we only search the current file's global block.  */
473
474       const struct block *global_block = block_global_block (block);
475       
476       if (global_block != NULL)
477         sym = lookup_symbol_aux_block (name, linkage_name, global_block,
478                                        domain);
479     }
480   else
481     {
482       sym = lookup_symbol_global (name, linkage_name, block, domain);
483     }
484
485   if (sym != NULL)
486     return sym;
487
488   /* Now call "lookup_possible_namespace_symbol".  Symbols in here
489      claim to be associated to namespaces, but this claim might be
490      incorrect: the names in question might actually correspond to
491      classes instead of namespaces.  But if they correspond to
492      classes, then we should have found a match for them above.  So if
493      we find them now, they should be genuine.  */
494
495   /* FIXME: carlton/2003-06-12: This is a hack and should eventually
496      be deleted: see comments below.  */
497
498   if (domain == VAR_DOMAIN)
499     {
500       sym = lookup_possible_namespace_symbol (name);
501       if (sym != NULL)
502         return sym;
503     }
504
505   return NULL;
506 }
507
508 /* Look up a type named NESTED_NAME that is nested inside the C++
509    class or namespace given by PARENT_TYPE, from within the context
510    given by BLOCK.  Return NULL if there is no such nested type.  */
511
512 struct type *
513 cp_lookup_nested_type (struct type *parent_type,
514                        const char *nested_name,
515                        const struct block *block)
516 {
517   switch (TYPE_CODE (parent_type))
518     {
519     case TYPE_CODE_STRUCT:
520     case TYPE_CODE_NAMESPACE:
521       {
522         /* NOTE: carlton/2003-11-10: We don't treat C++ class members
523            of classes like, say, data or function members.  Instead,
524            they're just represented by symbols whose names are
525            qualified by the name of the surrounding class.  This is
526            just like members of namespaces; in particular,
527            lookup_symbol_namespace works when looking them up.  */
528
529         const char *parent_name = TYPE_TAG_NAME (parent_type);
530         struct symbol *sym = cp_lookup_symbol_namespace (parent_name,
531                                                          nested_name,
532                                                          NULL,
533                                                          block,
534                                                          VAR_DOMAIN);
535         if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
536           return NULL;
537         else
538           return SYMBOL_TYPE (sym);
539       }
540     default:
541       internal_error (__FILE__, __LINE__,
542                       _("cp_lookup_nested_type called on a non-aggregate type."));
543     }
544 }
545
546 /* The C++-version of lookup_transparent_type.  */
547
548 /* FIXME: carlton/2004-01-16: The problem that this is trying to
549    address is that, unfortunately, sometimes NAME is wrong: it may not
550    include the name of namespaces enclosing the type in question.
551    lookup_transparent_type gets called when the the type in question
552    is a declaration, and we're trying to find its definition; but, for
553    declarations, our type name deduction mechanism doesn't work.
554    There's nothing we can do to fix this in general, I think, in the
555    absence of debug information about namespaces (I've filed PR
556    gdb/1511 about this); until such debug information becomes more
557    prevalent, one heuristic which sometimes looks is to search for the
558    definition in namespaces containing the current namespace.
559
560    We should delete this functions once the appropriate debug
561    information becomes more widespread.  (GCC 3.4 will be the first
562    released version of GCC with such information.)  */
563
564 struct type *
565 cp_lookup_transparent_type (const char *name)
566 {
567   /* First, try the honest way of looking up the definition.  */
568   struct type *t = basic_lookup_transparent_type (name);
569   const char *scope;
570
571   if (t != NULL)
572     return t;
573
574   /* If that doesn't work and we're within a namespace, look there
575      instead.  */
576   scope = block_scope (get_selected_block (0));
577
578   if (scope[0] == '\0')
579     return NULL;
580
581   return cp_lookup_transparent_type_loop (name, scope, 0);
582 }
583
584 /* Lookup the the type definition associated to NAME in
585    namespaces/classes containing SCOPE whose name is strictly longer
586    than LENGTH.  LENGTH must be the index of the start of a
587    component of SCOPE.  */
588
589 static struct type *
590 cp_lookup_transparent_type_loop (const char *name, const char *scope,
591                                  int length)
592 {
593   int scope_length = length + cp_find_first_component (scope + length);
594   char *full_name;
595
596   /* If the current scope is followed by "::", look in the next
597      component.  */
598   if (scope[scope_length] == ':')
599     {
600       struct type *retval
601         = cp_lookup_transparent_type_loop (name, scope, scope_length + 2);
602       if (retval != NULL)
603         return retval;
604     }
605
606   full_name = alloca (scope_length + 2 + strlen (name) + 1);
607   strncpy (full_name, scope, scope_length);
608   strncpy (full_name + scope_length, "::", 2);
609   strcpy (full_name + scope_length + 2, name);
610
611   return basic_lookup_transparent_type (full_name);
612 }
613
614 /* Now come functions for dealing with symbols associated to
615    namespaces.  (They're used to store the namespaces themselves, not
616    objects that live in the namespaces.)  These symbols come in two
617    varieties: if we run into a DW_TAG_namespace DIE, then we know that
618    we have a namespace, so dwarf2read.c creates a symbol for it just
619    like normal.  But, unfortunately, versions of GCC through at least
620    3.3 don't generate those DIE's.  Our solution is to try to guess
621    their existence by looking at demangled names.  This might cause us
622    to misidentify classes as namespaces, however.  So we put those
623    symbols in a special block (one per objfile), and we only search
624    that block as a last resort.  */
625
626 /* FIXME: carlton/2003-06-12: Once versions of GCC that generate
627    DW_TAG_namespace have been out for a year or two, we should get rid
628    of all of this "possible namespace" nonsense.  */
629
630 /* Allocate everything necessary for the possible namespace block
631    associated to OBJFILE.  */
632
633 static void
634 initialize_namespace_symtab (struct objfile *objfile)
635 {
636   struct symtab *namespace_symtab;
637   struct blockvector *bv;
638   struct block *bl;
639
640   namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile);
641   namespace_symtab->language = language_cplus;
642   namespace_symtab->free_code = free_nothing;
643   namespace_symtab->dirname = NULL;
644
645   bv = obstack_alloc (&objfile->objfile_obstack,
646                       sizeof (struct blockvector)
647                       + FIRST_LOCAL_BLOCK * sizeof (struct block *));
648   BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1;
649   BLOCKVECTOR (namespace_symtab) = bv;
650   
651   /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */
652
653   bl = allocate_block (&objfile->objfile_obstack);
654   BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
655                                         NULL);
656   BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
657   bl = allocate_block (&objfile->objfile_obstack);
658   BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
659                                         NULL);
660   BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
661
662   /* Allocate the possible namespace block; we put it where the first
663      local block will live, though I don't think there's any need to
664      pretend that it's actually a local block (e.g. by setting
665      BLOCK_SUPERBLOCK appropriately).  We don't use the global or
666      static block because we don't want it searched during the normal
667      search of all global/static blocks in lookup_symbol: we only want
668      it used as a last resort.  */
669
670   /* NOTE: carlton/2003-09-11: I considered not associating the fake
671      symbols to a block/symtab at all.  But that would cause problems
672      with lookup_symbol's SYMTAB argument and with block_found, so
673      having a symtab/block for this purpose seems like the best
674      solution for now.  */
675
676   bl = allocate_block (&objfile->objfile_obstack);
677   BLOCK_DICT (bl) = dict_create_hashed_expandable ();
678   BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl;
679
680   namespace_symtab->free_func = free_namespace_block;
681
682   objfile->cp_namespace_symtab = namespace_symtab;
683 }
684
685 /* Locate the possible namespace block associated to OBJFILE,
686    allocating it if necessary.  */
687
688 static struct block *
689 get_possible_namespace_block (struct objfile *objfile)
690 {
691   if (objfile->cp_namespace_symtab == NULL)
692     initialize_namespace_symtab (objfile);
693
694   return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab),
695                             FIRST_LOCAL_BLOCK);
696 }
697
698 /* Free the dictionary associated to the possible namespace block.  */
699
700 static void
701 free_namespace_block (struct symtab *symtab)
702 {
703   struct block *possible_namespace_block;
704
705   possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
706                                                 FIRST_LOCAL_BLOCK);
707   gdb_assert (possible_namespace_block != NULL);
708   dict_free (BLOCK_DICT (possible_namespace_block));
709 }
710
711 /* Ensure that there are symbols in the possible namespace block
712    associated to OBJFILE for all initial substrings of NAME that look
713    like namespaces or classes.  NAME should end in a member variable:
714    it shouldn't consist solely of namespaces.  */
715
716 void
717 cp_check_possible_namespace_symbols (const char *name, struct objfile *objfile)
718 {
719   check_possible_namespace_symbols_loop (name,
720                                          cp_find_first_component (name),
721                                          objfile);
722 }
723
724 /* This is a helper loop for cp_check_possible_namespace_symbols; it
725    ensures that there are symbols in the possible namespace block
726    associated to OBJFILE for all namespaces that are initial
727    substrings of NAME of length at least LEN.  It returns 1 if a
728    previous loop had already created the shortest such symbol and 0
729    otherwise.
730
731    This function assumes that if there is already a symbol associated
732    to a substring of NAME of a given length, then there are already
733    symbols associated to all substrings of NAME whose length is less
734    than that length.  So if cp_check_possible_namespace_symbols has
735    been called once with argument "A::B::C::member", then that will
736    create symbols "A", "A::B", and "A::B::C".  If it is then later
737    called with argument "A::B::D::member", then the new call will
738    generate a new symbol for "A::B::D", but once it sees that "A::B"
739    has already been created, it doesn't bother checking to see if "A"
740    has also been created.  */
741
742 static int
743 check_possible_namespace_symbols_loop (const char *name, int len,
744                                        struct objfile *objfile)
745 {
746   if (name[len] == ':')
747     {
748       int done;
749       int next_len = len + 2;
750
751       next_len += cp_find_first_component (name + next_len);
752       done = check_possible_namespace_symbols_loop (name, next_len,
753                                                     objfile);
754
755       if (!done)
756         done = check_one_possible_namespace_symbol (name, len, objfile);
757
758       return done;
759     }
760   else
761     return 0;
762 }
763
764 /* Check to see if there's already a possible namespace symbol in
765    OBJFILE whose name is the initial substring of NAME of length LEN.
766    If not, create one and return 0; otherwise, return 1.  */
767
768 static int
769 check_one_possible_namespace_symbol (const char *name, int len,
770                                      struct objfile *objfile)
771 {
772   struct block *block = get_possible_namespace_block (objfile);
773   char *name_copy = alloca (len + 1);
774   struct symbol *sym;
775
776   memcpy (name_copy, name, len);
777   name_copy[len] = '\0';
778   sym = lookup_block_symbol (block, name_copy, NULL, VAR_DOMAIN);
779
780   if (sym == NULL)
781     {
782       struct type *type;
783       name_copy = obsavestring (name, len, &objfile->objfile_obstack);
784
785       type = init_type (TYPE_CODE_NAMESPACE, 0, 0, name_copy, objfile);
786
787       TYPE_TAG_NAME (type) = TYPE_NAME (type);
788
789       sym = obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol));
790       memset (sym, 0, sizeof (struct symbol));
791       SYMBOL_LANGUAGE (sym) = language_cplus;
792       SYMBOL_SET_NAMES (sym, name_copy, len, objfile);
793       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
794       SYMBOL_TYPE (sym) = type;
795       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
796
797       dict_add_symbol (BLOCK_DICT (block), sym);
798
799       return 0;
800     }
801   else
802     return 1;
803 }
804
805 /* Look for a symbol named NAME in all the possible namespace blocks.
806    If one is found, return it.  */
807
808 static struct symbol *
809 lookup_possible_namespace_symbol (const char *name)
810 {
811   struct objfile *objfile;
812
813   ALL_OBJFILES (objfile)
814     {
815       struct symbol *sym;
816
817       sym = lookup_block_symbol (get_possible_namespace_block (objfile),
818                                  name, NULL, VAR_DOMAIN);
819
820       if (sym != NULL)
821         return sym;
822     }
823
824   return NULL;
825 }
826
827 /* Print out all the possible namespace symbols.  */
828
829 static void
830 maintenance_cplus_namespace (char *args, int from_tty)
831 {
832   struct objfile *objfile;
833   printf_unfiltered (_("Possible namespaces:\n"));
834   ALL_OBJFILES (objfile)
835     {
836       struct dict_iterator iter;
837       struct symbol *sym;
838
839       ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym)
840         {
841           printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym));
842         }
843     }
844 }
845
846 void
847 _initialize_cp_namespace (void)
848 {
849   add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace,
850            _("Print the list of possible C++ namespaces."),
851            &maint_cplus_cmd_list);
852 }