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