cp-namespace.c (cp_lookup_nested_symbol): Fix comments.
[external/binutils.git] / gdb / cp-namespace.c
1 /* Helper routines for C++ support in GDB.
2    Copyright (C) 2003-2014 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 "block.h"
27 #include "objfiles.h"
28 #include "gdbtypes.h"
29 #include "dictionary.h"
30 #include "command.h"
31 #include "frame.h"
32 #include "buildsym.h"
33 #include "language.h"
34
35 static struct type *cp_lookup_transparent_type_loop (const char *name,
36                                                      const char *scope,
37                                                      int scope_len);
38
39 /* Check to see if SYMBOL refers to an object contained within an
40    anonymous namespace; if so, add an appropriate using directive.  */
41
42 void
43 cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
44                                   struct objfile *const objfile)
45 {
46   if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
47     {
48       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
49       unsigned int previous_component;
50       unsigned int next_component;
51
52       /* Start with a quick-and-dirty check for mention of "(anonymous
53          namespace)".  */
54
55       if (!cp_is_anonymous (name))
56         return;
57
58       previous_component = 0;
59       next_component = cp_find_first_component (name + previous_component);
60
61       while (name[next_component] == ':')
62         {
63           if (((next_component - previous_component)
64                == CP_ANONYMOUS_NAMESPACE_LEN)
65               && strncmp (name + previous_component,
66                           CP_ANONYMOUS_NAMESPACE_STR,
67                           CP_ANONYMOUS_NAMESPACE_LEN) == 0)
68             {
69               int dest_len = (previous_component == 0
70                               ? 0 : previous_component - 2);
71               int src_len = next_component;
72
73               char *dest = alloca (dest_len + 1);
74               char *src = alloca (src_len + 1);
75
76               memcpy (dest, name, dest_len);
77               memcpy (src, name, src_len);
78
79               dest[dest_len] = '\0';
80               src[src_len] = '\0';
81
82               /* We've found a component of the name that's an
83                  anonymous namespace.  So add symbols in it to the
84                  namespace given by the previous component if there is
85                  one, or to the global namespace if there isn't.  */
86               cp_add_using_directive (dest, src, NULL, NULL, NULL, 1,
87                                       &objfile->objfile_obstack);
88             }
89           /* The "+ 2" is for the "::".  */
90           previous_component = next_component + 2;
91           next_component = (previous_component
92                             + cp_find_first_component (name
93                                                        + previous_component));
94         }
95     }
96 }
97
98
99 /* Add a using directive to using_directives.  If the using directive
100    in question has already been added, don't add it twice.
101
102    Create a new struct using_direct which imports the namespace SRC
103    into the scope DEST.  ALIAS is the name of the imported namespace
104    in the current scope.  If ALIAS is NULL then the namespace is known
105    by its original name.  DECLARATION is the name if the imported
106    varable if this is a declaration import (Eg. using A::x), otherwise
107    it is NULL.  EXCLUDES is a list of names not to import from an
108    imported module or NULL.  If COPY_NAMES is non-zero, then the
109    arguments are copied into newly allocated memory so they can be
110    temporaries.  For EXCLUDES the VEC pointers are copied but the
111    pointed to characters are not copied.  */
112
113 void
114 cp_add_using_directive (const char *dest,
115                         const char *src,
116                         const char *alias,
117                         const char *declaration,
118                         VEC (const_char_ptr) *excludes,
119                         int copy_names,
120                         struct obstack *obstack)
121 {
122   struct using_direct *current;
123   struct using_direct *new;
124   
125   /* Has it already been added?  */
126
127   for (current = using_directives; current != NULL; current = current->next)
128     {
129       int ix;
130       const char *param;
131
132       if (strcmp (current->import_src, src) != 0)
133         continue;
134       if (strcmp (current->import_dest, dest) != 0)
135         continue;
136       if ((alias == NULL && current->alias != NULL)
137           || (alias != NULL && current->alias == NULL)
138           || (alias != NULL && current->alias != NULL
139               && strcmp (alias, current->alias) != 0))
140         continue;
141       if ((declaration == NULL && current->declaration != NULL)
142           || (declaration != NULL && current->declaration == NULL)
143           || (declaration != NULL && current->declaration != NULL
144               && strcmp (declaration, current->declaration) != 0))
145         continue;
146
147       /* Compare the contents of EXCLUDES.  */
148       for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
149         if (current->excludes[ix] == NULL
150             || strcmp (param, current->excludes[ix]) != 0)
151           break;
152       if (ix < VEC_length (const_char_ptr, excludes)
153           || current->excludes[ix] != NULL)
154         continue;
155
156       /* Parameters exactly match CURRENT.  */
157       return;
158     }
159
160   new = obstack_alloc (obstack, (sizeof (*new)
161                                  + (VEC_length (const_char_ptr, excludes)
162                                     * sizeof (*new->excludes))));
163   memset (new, 0, sizeof (*new));
164
165   if (copy_names)
166     {
167       new->import_src = obstack_copy0 (obstack, src, strlen (src));
168       new->import_dest = obstack_copy0 (obstack, dest, strlen (dest));
169     }
170   else
171     {
172       new->import_src = src;
173       new->import_dest = dest;
174     }
175
176   if (alias != NULL && copy_names)
177     new->alias = obstack_copy0 (obstack, alias, strlen (alias));
178   else
179     new->alias = alias;
180
181   if (declaration != NULL && copy_names)
182     new->declaration = obstack_copy0 (obstack,
183                                       declaration, strlen (declaration));
184   else
185     new->declaration = declaration;
186
187   memcpy (new->excludes, VEC_address (const_char_ptr, excludes),
188           VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes));
189   new->excludes[VEC_length (const_char_ptr, excludes)] = NULL;
190
191   new->next = using_directives;
192   using_directives = new;
193 }
194
195 /* Test whether or not NAMESPACE looks like it mentions an anonymous
196    namespace; return nonzero if so.  */
197
198 int
199 cp_is_anonymous (const char *namespace)
200 {
201   return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR)
202           != NULL);
203 }
204
205 /* Look up NAME in BLOCK's static block and in global blocks.  If
206    ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
207    within an anonymous namespace.  If SEARCH is non-zero, search through
208    base classes for a matching symbol.  Other arguments are as in
209    cp_lookup_symbol_nonlocal.  */
210
211 static struct symbol *
212 lookup_symbol_file (const char *name,
213                     const struct block *block,
214                     const domain_enum domain,
215                     int anonymous_namespace, int search)
216 {
217   struct symbol *sym = NULL;
218
219   sym = lookup_symbol_in_static_block (name, block, domain);
220   if (sym != NULL)
221     return sym;
222
223   if (anonymous_namespace)
224     {
225       /* Symbols defined in anonymous namespaces have external linkage
226          but should be treated as local to a single file nonetheless.
227          So we only search the current file's global block.  */
228
229       const struct block *global_block = block_global_block (block);
230       
231       if (global_block != NULL)
232         sym = lookup_symbol_in_block (name, global_block, domain);
233     }
234   else
235     {
236       sym = lookup_global_symbol (name, block, domain);
237     }
238
239   if (sym != NULL)
240     return sym;
241
242   if (search)
243     {
244       char *klass, *nested;
245       unsigned int prefix_len;
246       struct cleanup *cleanup;
247       struct symbol *klass_sym;
248
249       /* A simple lookup failed.  Check if the symbol was defined in
250          a base class.  */
251
252       cleanup = make_cleanup (null_cleanup, NULL);
253
254       /* Find the name of the class and the name of the method,
255          variable, etc.  */
256       prefix_len = cp_entire_prefix_len (name);
257
258       /* If no prefix was found, search "this".  */
259       if (prefix_len == 0)
260         {
261           struct type *type;
262           struct symbol *this;
263
264           this = lookup_language_this (language_def (language_cplus), block);
265           if (this == NULL)
266             {
267               do_cleanups (cleanup);
268               return NULL;
269             }
270
271           type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this)));
272           /* If TYPE_NAME is NULL, abandon trying to find this symbol.
273              This can happen for lambda functions compiled with clang++,
274              which outputs no name for the container class.  */
275           if (TYPE_NAME (type) == NULL)
276             return NULL;
277           klass = xstrdup (TYPE_NAME (type));
278           nested = xstrdup (name);
279         }
280       else
281         {
282           /* The class name is everything up to and including PREFIX_LEN.  */
283           klass = savestring (name, prefix_len);
284
285           /* The rest of the name is everything else past the initial scope
286              operator.  */
287           nested = xstrdup (name + prefix_len + 2);
288         }
289
290       /* Add cleanups to free memory for these strings.  */
291       make_cleanup (xfree, klass);
292       make_cleanup (xfree, nested);
293
294       /* Lookup a class named KLASS.  If none is found, there is nothing
295          more that can be done.  */
296       klass_sym = lookup_global_symbol (klass, block, domain);
297       if (klass_sym == NULL)
298         {
299           do_cleanups (cleanup);
300           return NULL;
301         }
302
303       /* Look for a symbol named NESTED in this class.  */
304       sym = cp_lookup_nested_symbol (SYMBOL_TYPE (klass_sym), nested, block);
305       do_cleanups (cleanup);
306     }
307
308   return sym;
309 }
310
311 /* Look up NAME in the C++ namespace NAMESPACE.  Other arguments are
312    as in cp_lookup_symbol_nonlocal.  If SEARCH is non-zero, search
313    through base classes for a matching symbol.  */
314
315 static struct symbol *
316 cp_lookup_symbol_in_namespace (const char *namespace,
317                                const char *name,
318                                const struct block *block,
319                                const domain_enum domain, int search)
320 {
321   if (namespace[0] == '\0')
322     {
323       return lookup_symbol_file (name, block, domain, 0, search);
324     }
325   else
326     {
327       char *concatenated_name = alloca (strlen (namespace) + 2
328                                         + strlen (name) + 1);
329
330       strcpy (concatenated_name, namespace);
331       strcat (concatenated_name, "::");
332       strcat (concatenated_name, name);
333       return lookup_symbol_file (concatenated_name, block, domain,
334                                  cp_is_anonymous (namespace), search);
335     }
336 }
337
338 /* Used for cleanups to reset the "searched" flag incase
339    of an error.  */
340
341 static void
342 reset_directive_searched (void *data)
343 {
344   struct using_direct *direct = data;
345   direct->searched = 0;
346 }
347
348 /* Search for NAME by applying all import statements belonging to
349    BLOCK which are applicable in SCOPE.  If DECLARATION_ONLY the
350    search is restricted to using declarations.
351    Example:
352
353      namespace A {
354        int x;
355      }
356      using A::x;
357
358    If SEARCH_PARENTS the search will include imports which are
359    applicable in parents of SCOPE.
360    Example:
361
362      namespace A {
363        using namespace X;
364        namespace B {
365          using namespace Y;
366        }
367      }
368
369    If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
370    namespaces X and Y will be considered.  If SEARCH_PARENTS is false
371    only the import of Y is considered.  */
372
373 static struct symbol *
374 cp_lookup_symbol_imports (const char *scope,
375                           const char *name,
376                           const struct block *block,
377                           const domain_enum domain,
378                           const int declaration_only,
379                           const int search_parents)
380 {
381   struct using_direct *current;
382   struct symbol *sym = NULL;
383   int len;
384   int directive_match;
385   struct cleanup *searched_cleanup;
386
387   /* First, try to find the symbol in the given namespace.  */
388   if (!declaration_only)
389     sym = cp_lookup_symbol_in_namespace (scope, name,
390                                          block, domain, 1);
391   
392   if (sym != NULL)
393     return sym;
394
395   /* Go through the using directives.  If any of them add new names to
396      the namespace we're searching in, see if we can find a match by
397      applying them.  */
398
399   for (current = block_using (block);
400        current != NULL;
401        current = current->next)
402     {
403       const char **excludep;
404
405       len = strlen (current->import_dest);
406       directive_match = (search_parents
407                          ? (strncmp (scope, current->import_dest,
408                                      strlen (current->import_dest)) == 0
409                             && (len == 0
410                                 || scope[len] == ':'
411                                 || scope[len] == '\0'))
412                          : strcmp (scope, current->import_dest) == 0);
413
414       /* If the import destination is the current scope or one of its
415          ancestors then it is applicable.  */
416       if (directive_match && !current->searched)
417         {
418           /* Mark this import as searched so that the recursive call
419              does not search it again.  */
420           current->searched = 1;
421           searched_cleanup = make_cleanup (reset_directive_searched,
422                                            current);
423
424           /* If there is an import of a single declaration, compare the
425              imported declaration (after optional renaming by its alias)
426              with the sought out name.  If there is a match pass
427              current->import_src as NAMESPACE to direct the search
428              towards the imported namespace.  */
429           if (current->declaration
430               && strcmp (name, current->alias
431                          ? current->alias : current->declaration) == 0)
432             sym = cp_lookup_symbol_in_namespace (current->import_src,
433                                                  current->declaration,
434                                                  block, domain, 1);
435
436           /* If this is a DECLARATION_ONLY search or a symbol was found
437              or this import statement was an import declaration, the
438              search of this import is complete.  */
439           if (declaration_only || sym != NULL || current->declaration)
440             {
441               current->searched = 0;
442               discard_cleanups (searched_cleanup);
443
444               if (sym != NULL)
445                 return sym;
446
447               continue;
448             }
449
450           /* Do not follow CURRENT if NAME matches its EXCLUDES.  */
451           for (excludep = current->excludes; *excludep; excludep++)
452             if (strcmp (name, *excludep) == 0)
453               break;
454           if (*excludep)
455             {
456               discard_cleanups (searched_cleanup);
457               continue;
458             }
459
460           if (current->alias != NULL
461               && strcmp (name, current->alias) == 0)
462             /* If the import is creating an alias and the alias matches
463                the sought name.  Pass current->import_src as the NAME to
464                direct the search towards the aliased namespace.  */
465             {
466               sym = cp_lookup_symbol_in_namespace (scope,
467                                                    current->import_src,
468                                                    block, domain, 1);
469             }
470           else if (current->alias == NULL)
471             {
472               /* If this import statement creates no alias, pass
473                  current->inner as NAMESPACE to direct the search
474                  towards the imported namespace.  */
475               sym = cp_lookup_symbol_imports (current->import_src,
476                                               name, block,
477                                               domain, 0, 0);
478             }
479           current->searched = 0;
480           discard_cleanups (searched_cleanup);
481
482           if (sym != NULL)
483             return sym;
484         }
485     }
486
487   return NULL;
488 }
489
490 /* Helper function that searches an array of symbols for one named
491    NAME.  */
492
493 static struct symbol *
494 search_symbol_list (const char *name, int num,
495                     struct symbol **syms)
496 {
497   int i;
498
499   /* Maybe we should store a dictionary in here instead.  */
500   for (i = 0; i < num; ++i)
501     {
502       if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
503         return syms[i];
504     }
505   return NULL;
506 }
507
508 /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it
509    searches through the template parameters of the function and the
510    function's type.  */
511
512 struct symbol *
513 cp_lookup_symbol_imports_or_template (const char *scope,
514                                       const char *name,
515                                       const struct block *block,
516                                       const domain_enum domain)
517 {
518   struct symbol *function = BLOCK_FUNCTION (block);
519
520   if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
521     {
522       /* Search the function's template parameters.  */
523       if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
524         {
525           struct template_symbol *templ 
526             = (struct template_symbol *) function;
527           struct symbol *result;
528
529           result = search_symbol_list (name,
530                                        templ->n_template_arguments,
531                                        templ->template_arguments);
532           if (result != NULL)
533             return result;
534         }
535
536       /* Search the template parameters of the function's defining
537          context.  */
538       if (SYMBOL_NATURAL_NAME (function))
539         {
540           struct type *context;
541           char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
542           struct cleanup *cleanups = make_cleanup (xfree, name_copy);
543           const struct language_defn *lang = language_def (language_cplus);
544           struct gdbarch *arch
545             = get_objfile_arch (SYMBOL_OBJFILE (function));
546           const struct block *parent = BLOCK_SUPERBLOCK (block);
547
548           while (1)
549             {
550               struct symbol *result;
551               unsigned int prefix_len = cp_entire_prefix_len (name_copy);
552
553               if (prefix_len == 0)
554                 context = NULL;
555               else
556                 {
557                   name_copy[prefix_len] = '\0';
558                   context = lookup_typename (lang, arch,
559                                              name_copy,
560                                              parent, 1);
561                 }
562
563               if (context == NULL)
564                 break;
565
566               result
567                 = search_symbol_list (name,
568                                       TYPE_N_TEMPLATE_ARGUMENTS (context),
569                                       TYPE_TEMPLATE_ARGUMENTS (context));
570               if (result != NULL)
571                 {
572                   do_cleanups (cleanups);
573                   return result;
574                 }
575             }
576
577           do_cleanups (cleanups);
578         }
579     }
580
581   return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1);
582 }
583
584  /* Searches for NAME in the current namespace, and by applying
585     relevant import statements belonging to BLOCK and its parents.
586     SCOPE is the namespace scope of the context in which the search is
587     being evaluated.  */
588
589 struct symbol*
590 cp_lookup_symbol_namespace (const char *scope,
591                             const char *name,
592                             const struct block *block,
593                             const domain_enum domain)
594 {
595   struct symbol *sym;
596   
597   /* First, try to find the symbol in the given namespace.  */
598   sym = cp_lookup_symbol_in_namespace (scope, name,
599                                        block, domain, 1);
600   if (sym != NULL)
601     return sym;
602
603   /* Search for name in namespaces imported to this and parent
604      blocks.  */
605   while (block != NULL)
606     {
607       sym = cp_lookup_symbol_imports (scope, name, block,
608                                       domain, 0, 1);
609
610       if (sym)
611         return sym;
612
613       block = BLOCK_SUPERBLOCK (block);
614     }
615
616   return NULL;
617 }
618
619 /* Lookup NAME at namespace scope (or, in C terms, in static and
620    global variables).  SCOPE is the namespace that the current
621    function is defined within; only consider namespaces whose length
622    is at least SCOPE_LEN.  Other arguments are as in
623    cp_lookup_symbol_nonlocal.
624
625    For example, if we're within a function A::B::f and looking for a
626    symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
627    SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
628    but with SCOPE_LEN = 1.  And then it calls itself with NAME and
629    SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
630    "A::B::x"; if it doesn't find it, then the second call looks for
631    "A::x", and if that call fails, then the first call looks for
632    "x".  */
633
634 static struct symbol *
635 lookup_namespace_scope (const char *name,
636                         const struct block *block,
637                         const domain_enum domain,
638                         const char *scope,
639                         int scope_len)
640 {
641   char *namespace;
642
643   if (scope[scope_len] != '\0')
644     {
645       /* Recursively search for names in child namespaces first.  */
646
647       struct symbol *sym;
648       int new_scope_len = scope_len;
649
650       /* If the current scope is followed by "::", skip past that.  */
651       if (new_scope_len != 0)
652         {
653           gdb_assert (scope[new_scope_len] == ':');
654           new_scope_len += 2;
655         }
656       new_scope_len += cp_find_first_component (scope + new_scope_len);
657       sym = lookup_namespace_scope (name, block, domain,
658                                     scope, new_scope_len);
659       if (sym != NULL)
660         return sym;
661     }
662
663   /* Okay, we didn't find a match in our children, so look for the
664      name in the current namespace.  */
665
666   namespace = alloca (scope_len + 1);
667   strncpy (namespace, scope, scope_len);
668   namespace[scope_len] = '\0';
669   return cp_lookup_symbol_in_namespace (namespace, name,
670                                         block, domain, 1);
671 }
672
673 /* The C++-specific version of name lookup for static and global
674    names.  This makes sure that names get looked for in all namespaces
675    that are in scope.  NAME is the natural name of the symbol that
676    we're looking for, BLOCK is the block that we're searching within,
677    DOMAIN says what kind of symbols we're looking for.  */
678
679 struct symbol *
680 cp_lookup_symbol_nonlocal (const char *name,
681                            const struct block *block,
682                            const domain_enum domain)
683 {
684   struct symbol *sym;
685   const char *scope = block_scope (block);
686
687   sym = lookup_namespace_scope (name, block,
688                                 domain, scope, 0);
689   if (sym != NULL)
690     return sym;
691
692   return cp_lookup_symbol_namespace (scope, name,
693                                      block, domain);
694 }
695
696 /* Search through the base classes of PARENT_TYPE for a base class
697    named NAME and return its type.  If not found, return NULL.  */
698
699 struct type *
700 find_type_baseclass_by_name (struct type *parent_type, const char *name)
701 {
702   int i;
703
704   CHECK_TYPEDEF (parent_type);
705   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
706     {
707       struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
708       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
709
710       if (base_name == NULL)
711         continue;
712
713       if (streq (base_name, name))
714         return type;
715
716       type = find_type_baseclass_by_name (type, name);
717       if (type != NULL)
718         return type;
719     }
720
721   return NULL;
722 }
723
724 /* Search through the base classes of PARENT_TYPE for a symbol named
725    NAME in block BLOCK.  */
726
727 static struct symbol *
728 find_symbol_in_baseclass (struct type *parent_type, const char *name,
729                            const struct block *block)
730 {
731   int i;
732   struct symbol *sym;
733   struct cleanup *cleanup;
734   char *concatenated_name;
735
736   sym = NULL;
737   concatenated_name = NULL;
738   cleanup = make_cleanup (free_current_contents, &concatenated_name);
739   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
740     {
741       size_t len;
742       struct type *base_type = TYPE_BASECLASS (parent_type, i);
743       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
744
745       if (base_name == NULL)
746         continue;
747
748       /* Search this particular base class.  */
749       sym = cp_lookup_symbol_in_namespace (base_name, name, block,
750                                            VAR_DOMAIN, 0);
751       if (sym != NULL)
752         break;
753
754       /* Now search all static file-level symbols.  We have to do this for
755          things like typedefs in the class.  First search in this symtab,
756          what we want is possibly there.  */
757       len = strlen (base_name) + 2 + strlen (name) + 1;
758       concatenated_name = xrealloc (concatenated_name, len);
759       xsnprintf (concatenated_name, len, "%s::%s", base_name, name);
760       sym = lookup_symbol_in_static_block (concatenated_name, block,
761                                            VAR_DOMAIN);
762       if (sym != NULL)
763         break;
764
765       /* Nope.  We now have to search all static blocks in all objfiles,
766          even if block != NULL, because there's no guarantees as to which
767          symtab the symbol we want is in.  */
768       sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
769       if (sym != NULL)
770         break;
771
772       /* If this class has base classes, search them next.  */
773       CHECK_TYPEDEF (base_type);
774       if (TYPE_N_BASECLASSES (base_type) > 0)
775         {
776           sym = find_symbol_in_baseclass (base_type, name, block);
777           if (sym != NULL)
778             break;
779         }
780     }
781
782   do_cleanups (cleanup);
783   return sym;
784 }
785
786 /* Look up a symbol named NESTED_NAME that is nested inside the C++
787    class or namespace given by PARENT_TYPE, from within the context
788    given by BLOCK.  Return NULL if there is no such nested symbol.  */
789
790 struct symbol *
791 cp_lookup_nested_symbol (struct type *parent_type,
792                          const char *nested_name,
793                          const struct block *block)
794 {
795   /* type_name_no_tag_or_error provides better error reporting using the
796      original type.  */
797   struct type *saved_parent_type = parent_type;
798
799   CHECK_TYPEDEF (parent_type);
800
801   switch (TYPE_CODE (parent_type))
802     {
803     case TYPE_CODE_STRUCT:
804     case TYPE_CODE_NAMESPACE:
805     case TYPE_CODE_UNION:
806     case TYPE_CODE_ENUM:
807     /* NOTE: Handle modules here as well, because Fortran is re-using the C++
808        specific code to lookup nested symbols in modules, by calling the
809        function pointer la_lookup_symbol_nonlocal, which ends up here.  */
810     case TYPE_CODE_MODULE:
811       {
812         /* NOTE: carlton/2003-11-10: We don't treat C++ class members
813            of classes like, say, data or function members.  Instead,
814            they're just represented by symbols whose names are
815            qualified by the name of the surrounding class.  This is
816            just like members of namespaces; in particular,
817            lookup_symbol_namespace works when looking them up.  */
818
819         int size;
820         const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
821         struct symbol *sym
822           = cp_lookup_symbol_in_namespace (parent_name, nested_name,
823                                            block, VAR_DOMAIN, 0);
824         char *concatenated_name;
825
826         if (sym != NULL)
827           return sym;
828
829         /* Now search all static file-level symbols.  We have to do this
830            for things like typedefs in the class.  We do not try to
831            guess any imported namespace as even the fully specified
832            namespace search is already not C++ compliant and more
833            assumptions could make it too magic.  */
834
835         size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
836         concatenated_name = alloca (size);
837         xsnprintf (concatenated_name, size, "%s::%s",
838                  parent_name, nested_name);
839         sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
840         if (sym != NULL)
841           return sym;
842
843         /* If no matching symbols were found, try searching any
844            base classes.  */
845         return find_symbol_in_baseclass (parent_type, nested_name, block);
846       }
847
848     case TYPE_CODE_FUNC:
849     case TYPE_CODE_METHOD:
850       return NULL;
851
852     default:
853       internal_error (__FILE__, __LINE__,
854                       _("cp_lookup_nested_symbol called "
855                         "on a non-aggregate type."));
856     }
857 }
858
859 /* The C++-version of lookup_transparent_type.  */
860
861 /* FIXME: carlton/2004-01-16: The problem that this is trying to
862    address is that, unfortunately, sometimes NAME is wrong: it may not
863    include the name of namespaces enclosing the type in question.
864    lookup_transparent_type gets called when the type in question
865    is a declaration, and we're trying to find its definition; but, for
866    declarations, our type name deduction mechanism doesn't work.
867    There's nothing we can do to fix this in general, I think, in the
868    absence of debug information about namespaces (I've filed PR
869    gdb/1511 about this); until such debug information becomes more
870    prevalent, one heuristic which sometimes looks is to search for the
871    definition in namespaces containing the current namespace.
872
873    We should delete this functions once the appropriate debug
874    information becomes more widespread.  (GCC 3.4 will be the first
875    released version of GCC with such information.)  */
876
877 struct type *
878 cp_lookup_transparent_type (const char *name)
879 {
880   /* First, try the honest way of looking up the definition.  */
881   struct type *t = basic_lookup_transparent_type (name);
882   const char *scope;
883
884   if (t != NULL)
885     return t;
886
887   /* If that doesn't work and we're within a namespace, look there
888      instead.  */
889   scope = block_scope (get_selected_block (0));
890
891   if (scope[0] == '\0')
892     return NULL;
893
894   return cp_lookup_transparent_type_loop (name, scope, 0);
895 }
896
897 /* Lookup the type definition associated to NAME in namespaces/classes
898    containing SCOPE whose name is strictly longer than LENGTH.  LENGTH
899    must be the index of the start of a component of SCOPE.  */
900
901 static struct type *
902 cp_lookup_transparent_type_loop (const char *name,
903                                  const char *scope,
904                                  int length)
905 {
906   int scope_length = length + cp_find_first_component (scope + length);
907   char *full_name;
908
909   /* If the current scope is followed by "::", look in the next
910      component.  */
911   if (scope[scope_length] == ':')
912     {
913       struct type *retval
914         = cp_lookup_transparent_type_loop (name, scope,
915                                            scope_length + 2);
916
917       if (retval != NULL)
918         return retval;
919     }
920
921   full_name = alloca (scope_length + 2 + strlen (name) + 1);
922   strncpy (full_name, scope, scope_length);
923   strncpy (full_name + scope_length, "::", 2);
924   strcpy (full_name + scope_length + 2, name);
925
926   return basic_lookup_transparent_type (full_name);
927 }
928
929 /* This used to do something but was removed when it became
930    obsolete.  */
931
932 static void
933 maintenance_cplus_namespace (char *args, int from_tty)
934 {
935   printf_unfiltered (_("The `maint namespace' command was removed.\n"));
936 }
937
938 /* Provide a prototype to silence -Wmissing-prototypes.  */
939 extern initialize_file_ftype _initialize_cp_namespace;
940
941 void
942 _initialize_cp_namespace (void)
943 {
944   struct cmd_list_element *cmd;
945
946   cmd = add_cmd ("namespace", class_maintenance,
947                  maintenance_cplus_namespace,
948                  _("Deprecated placeholder for removed functionality."),
949                  &maint_cplus_cmd_list);
950   deprecate_cmd (cmd, NULL);
951 }