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