Remove type_name_no_tag and rename type_name_no_tag_or_error
[external/binutils.git] / gdb / cp-namespace.c
1 /* Helper routines for C++ support in GDB.
2    Copyright (C) 2003-2018 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 #include "namespace.h"
35 #include <string>
36
37 static struct block_symbol
38   cp_lookup_nested_symbol_1 (struct type *container_type,
39                              const char *nested_name,
40                              const char *concatenated_name,
41                              const struct block *block,
42                              const domain_enum domain,
43                              int basic_lookup, int is_in_anonymous);
44
45 static struct type *cp_lookup_transparent_type_loop (const char *name,
46                                                      const char *scope,
47                                                      int scope_len);
48
49 /* Check to see if SYMBOL refers to an object contained within an
50    anonymous namespace; if so, add an appropriate using directive.  */
51
52 void
53 cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
54                                   struct objfile *const objfile)
55 {
56   if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
57     {
58       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
59       unsigned int previous_component;
60       unsigned int next_component;
61
62       /* Start with a quick-and-dirty check for mention of "(anonymous
63          namespace)".  */
64
65       if (!cp_is_in_anonymous (name))
66         return;
67
68       previous_component = 0;
69       next_component = cp_find_first_component (name + previous_component);
70
71       while (name[next_component] == ':')
72         {
73           if (((next_component - previous_component)
74                == CP_ANONYMOUS_NAMESPACE_LEN)
75               && strncmp (name + previous_component,
76                           CP_ANONYMOUS_NAMESPACE_STR,
77                           CP_ANONYMOUS_NAMESPACE_LEN) == 0)
78             {
79               int dest_len = (previous_component == 0
80                               ? 0 : previous_component - 2);
81               int src_len = next_component;
82
83               char *dest = (char *) alloca (dest_len + 1);
84               char *src = (char *) alloca (src_len + 1);
85
86               memcpy (dest, name, dest_len);
87               memcpy (src, name, src_len);
88
89               dest[dest_len] = '\0';
90               src[src_len] = '\0';
91
92               /* We've found a component of the name that's an
93                  anonymous namespace.  So add symbols in it to the
94                  namespace given by the previous component if there is
95                  one, or to the global namespace if there isn't.  */
96               std::vector<const char *> excludes;
97               add_using_directive (&local_using_directives,
98                                    dest, src, NULL, NULL, excludes, 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 /* Test whether or not NAMESPACE looks like it mentions an anonymous
111    namespace; return nonzero if so.  */
112
113 int
114 cp_is_in_anonymous (const char *symbol_name)
115 {
116   return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR)
117           != NULL);
118 }
119
120 /* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
121    If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
122    within an anonymous namespace.  */
123
124 static struct block_symbol
125 cp_basic_lookup_symbol (const char *name, const struct block *block,
126                         const domain_enum domain, int is_in_anonymous)
127 {
128   struct block_symbol sym;
129
130   sym = lookup_symbol_in_static_block (name, block, domain);
131   if (sym.symbol != NULL)
132     return sym;
133
134   if (is_in_anonymous)
135     {
136       /* Symbols defined in anonymous namespaces have external linkage
137          but should be treated as local to a single file nonetheless.
138          So we only search the current file's global block.  */
139
140       const struct block *global_block = block_global_block (block);
141
142       if (global_block != NULL)
143         {
144           sym.symbol = lookup_symbol_in_block (name,
145                                                symbol_name_match_type::FULL,
146                                                global_block, domain);
147           sym.block = global_block;
148         }
149     }
150   else
151     sym = lookup_global_symbol (name, block, domain);
152
153   return sym;
154 }
155
156 /* Search bare symbol NAME in DOMAIN in BLOCK.
157    NAME is guaranteed to not have any scope (no "::") in its name, though
158    if for example NAME is a template spec then "::" may appear in the
159    argument list.
160    If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
161    that language.  Normally we wouldn't need LANGDEF but fortran also uses
162    this code.
163    If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
164    if so then also search for NAME in that class.  */
165
166 static struct block_symbol
167 cp_lookup_bare_symbol (const struct language_defn *langdef,
168                        const char *name, const struct block *block,
169                        const domain_enum domain, int search)
170 {
171   struct block_symbol sym;
172
173   /* Note: We can't do a simple assert for ':' not being in NAME because
174      ':' may be in the args of a template spec.  This isn't intended to be
175      a complete test, just cheap and documentary.  */
176   if (strchr (name, '<') == NULL && strchr (name, '(') == NULL)
177     gdb_assert (strstr (name, "::") == NULL);
178
179   sym = lookup_symbol_in_static_block (name, block, domain);
180   if (sym.symbol != NULL)
181     return sym;
182
183   /* If we didn't find a definition for a builtin type in the static block,
184      search for it now.  This is actually the right thing to do and can be
185      a massive performance win.  E.g., when debugging a program with lots of
186      shared libraries we could search all of them only to find out the
187      builtin type isn't defined in any of them.  This is common for types
188      like "void".  */
189   if (langdef != NULL && domain == VAR_DOMAIN)
190     {
191       struct gdbarch *gdbarch;
192
193       if (block == NULL)
194         gdbarch = target_gdbarch ();
195       else
196         gdbarch = block_gdbarch (block);
197       sym.symbol
198         = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
199       sym.block = NULL;
200       if (sym.symbol != NULL)
201         return sym;
202     }
203
204   sym = lookup_global_symbol (name, block, domain);
205   if (sym.symbol != NULL)
206     return sym;
207
208   if (search)
209     {
210       struct block_symbol lang_this;
211       struct type *type;
212
213       lang_this.symbol = NULL;
214
215       if (langdef != NULL)
216         lang_this = lookup_language_this (langdef, block);
217
218       if (lang_this.symbol == NULL)
219         return null_block_symbol;
220
221
222       type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this.symbol)));
223       /* If TYPE_NAME is NULL, abandon trying to find this symbol.
224          This can happen for lambda functions compiled with clang++,
225          which outputs no name for the container class.  */
226       if (TYPE_NAME (type) == NULL)
227         return null_block_symbol;
228
229       /* Look for symbol NAME in this class.  */
230       sym = cp_lookup_nested_symbol (type, name, block, domain);
231     }
232
233   return sym;
234 }
235
236 /* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
237    BLOCK specifies the context in which to perform the search.
238    NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
239    the length of the entire scope of NAME (up to, but not including, the last
240    "::".
241
242    Note: At least in the case of Fortran, which also uses this code, there
243    may be no text after the last "::".  */
244
245 static struct block_symbol
246 cp_search_static_and_baseclasses (const char *name,
247                                   const struct block *block,
248                                   const domain_enum domain,
249                                   unsigned int prefix_len,
250                                   int is_in_anonymous)
251 {
252   /* Check for malformed input.  */
253   if (prefix_len + 2 > strlen (name) || name[prefix_len + 1] != ':')
254     return null_block_symbol;
255
256   /* The class, namespace or function name is everything up to and
257      including PREFIX_LEN.  */
258   std::string scope (name, prefix_len);
259
260   /* The rest of the name is everything else past the initial scope
261      operator.  */
262   const char *nested = name + prefix_len + 2;
263
264   /* Lookup the scope symbol.  If none is found, there is nothing more
265      that can be done.  SCOPE could be a namespace, so always look in
266      VAR_DOMAIN.  This works for classes too because of
267      symbol_matches_domain (which should be replaced with something
268      else, but it's what we have today).  */
269   block_symbol scope_sym = lookup_symbol_in_static_block (scope.c_str (),
270                                                           block, VAR_DOMAIN);
271   if (scope_sym.symbol == NULL)
272     scope_sym = lookup_global_symbol (scope.c_str (), block, VAR_DOMAIN);
273   if (scope_sym.symbol == NULL)
274     return null_block_symbol;
275
276   struct type *scope_type = SYMBOL_TYPE (scope_sym.symbol);
277
278   /* If the scope is a function/method, then look up NESTED as a local
279      static variable.  E.g., "print 'function()::static_var'".  */
280   if (TYPE_CODE (scope_type) == TYPE_CODE_FUNC
281       || TYPE_CODE (scope_type) == TYPE_CODE_METHOD)
282     return lookup_symbol (nested, SYMBOL_BLOCK_VALUE (scope_sym.symbol),
283                           VAR_DOMAIN, NULL);
284
285   /* Look for a symbol named NESTED in this class/namespace.
286      The caller is assumed to have already have done a basic lookup of NAME.
287      So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here.  */
288   return cp_lookup_nested_symbol_1 (scope_type, nested, name,
289                                     block, domain, 0, is_in_anonymous);
290 }
291
292 /* Look up NAME in the C++ namespace NAMESPACE.  Other arguments are
293    as in cp_lookup_symbol_nonlocal.  If SEARCH is non-zero, search
294    through base classes for a matching symbol.
295
296    Note: Part of the complexity is because NAME may itself specify scope.
297    Part of the complexity is also because this handles the case where
298    there is no scoping in which case we also try looking in the class of
299    "this" if we can compute it.  */
300
301 static struct block_symbol
302 cp_lookup_symbol_in_namespace (const char *the_namespace, const char *name,
303                                const struct block *block,
304                                const domain_enum domain, int search)
305 {
306   char *concatenated_name = NULL;
307   int is_in_anonymous;
308   unsigned int prefix_len;
309   struct block_symbol sym;
310
311   if (the_namespace[0] != '\0')
312     {
313       concatenated_name
314         = (char *) alloca (strlen (the_namespace) + 2 + strlen (name) + 1);
315       strcpy (concatenated_name, the_namespace);
316       strcat (concatenated_name, "::");
317       strcat (concatenated_name, name);
318       name = concatenated_name;
319     }
320
321   prefix_len = cp_entire_prefix_len (name);
322   if (prefix_len == 0)
323     return cp_lookup_bare_symbol (NULL, name, block, domain, search);
324
325   /* This would be simpler if we just called cp_lookup_nested_symbol
326      at this point.  But that would require first looking up the containing
327      class/namespace.  Since we're only searching static and global blocks
328      there's often no need to first do that lookup.  */
329
330   is_in_anonymous
331     = the_namespace[0] != '\0' && cp_is_in_anonymous (the_namespace);
332   sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous);
333   if (sym.symbol != NULL)
334     return sym;
335
336   if (search)
337     sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len,
338                                             is_in_anonymous);
339
340   return sym;
341 }
342
343 /* Search for NAME by applying all import statements belonging to
344    BLOCK which are applicable in SCOPE.  If DECLARATION_ONLY the
345    search is restricted to using declarations.
346    Example:
347
348      namespace A {
349        int x;
350      }
351      using A::x;
352
353    If SEARCH_PARENTS the search will include imports which are
354    applicable in parents of SCOPE.
355    Example:
356
357      namespace A {
358        using namespace X;
359        namespace B {
360          using namespace Y;
361        }
362      }
363
364    If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
365    namespaces X and Y will be considered.  If SEARCH_PARENTS is false
366    only the import of Y is considered.
367
368    SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
369    pass 0 for it.  Internally we pass 1 when recursing.  */
370
371 static struct block_symbol
372 cp_lookup_symbol_via_imports (const char *scope,
373                               const char *name,
374                               const struct block *block,
375                               const domain_enum domain,
376                               const int search_scope_first,
377                               const int declaration_only,
378                               const int search_parents)
379 {
380   struct using_direct *current;
381   struct block_symbol sym;
382   int len;
383   int directive_match;
384
385   sym.symbol = NULL;
386   sym.block = NULL;
387
388   /* First, try to find the symbol in the given namespace if requested.  */
389   if (search_scope_first)
390     sym = cp_lookup_symbol_in_namespace (scope, name,
391                                          block, domain, 1);
392
393   if (sym.symbol != NULL)
394     return sym;
395
396   /* Go through the using directives.  If any of them add new names to
397      the namespace we're searching in, see if we can find a match by
398      applying them.  */
399
400   for (current = block_using (block);
401        current != NULL;
402        current = current->next)
403     {
404       const char **excludep;
405
406       len = strlen (current->import_dest);
407       directive_match = (search_parents
408                          ? (startswith (scope, current->import_dest)
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           scoped_restore reset_directive_searched
421             = make_scoped_restore (&current->searched, 1);
422
423           /* If there is an import of a single declaration, compare the
424              imported declaration (after optional renaming by its alias)
425              with the sought out name.  If there is a match pass
426              current->import_src as NAMESPACE to direct the search
427              towards the imported namespace.  */
428           if (current->declaration
429               && strcmp (name, current->alias
430                          ? current->alias : current->declaration) == 0)
431             sym = cp_lookup_symbol_in_namespace (current->import_src,
432                                                  current->declaration,
433                                                  block, domain, 1);
434
435           /* If this is a DECLARATION_ONLY search or a symbol was found
436              or this import statement was an import declaration, the
437              search of this import is complete.  */
438           if (declaration_only || sym.symbol != NULL || current->declaration)
439             {
440               if (sym.symbol != NULL)
441                 return sym;
442
443               continue;
444             }
445
446           /* Do not follow CURRENT if NAME matches its EXCLUDES.  */
447           for (excludep = current->excludes; *excludep; excludep++)
448             if (strcmp (name, *excludep) == 0)
449               break;
450           if (*excludep)
451             continue;
452
453           if (current->alias != NULL
454               && strcmp (name, current->alias) == 0)
455             /* If the import is creating an alias and the alias matches
456                the sought name.  Pass current->import_src as the NAME to
457                direct the search towards the aliased namespace.  */
458             {
459               sym = cp_lookup_symbol_in_namespace (scope,
460                                                    current->import_src,
461                                                    block, domain, 1);
462             }
463           else if (current->alias == NULL)
464             {
465               /* If this import statement creates no alias, pass
466                  current->inner as NAMESPACE to direct the search
467                  towards the imported namespace.  */
468               sym = cp_lookup_symbol_via_imports (current->import_src,
469                                                   name, block,
470                                                   domain, 1, 0, 0);
471             }
472
473           if (sym.symbol != NULL)
474             return sym;
475         }
476     }
477
478   return null_block_symbol;
479 }
480
481 /* Helper function that searches an array of symbols for one named NAME.  */
482
483 static struct symbol *
484 search_symbol_list (const char *name, int num,
485                     struct symbol **syms)
486 {
487   int i;
488
489   /* Maybe we should store a dictionary in here instead.  */
490   for (i = 0; i < num; ++i)
491     {
492       if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
493         return syms[i];
494     }
495   return NULL;
496 }
497
498 /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it
499    searches through the template parameters of the function and the
500    function's type.  */
501
502 struct block_symbol
503 cp_lookup_symbol_imports_or_template (const char *scope,
504                                       const char *name,
505                                       const struct block *block,
506                                       const domain_enum domain)
507 {
508   struct symbol *function = BLOCK_FUNCTION (block);
509   struct block_symbol result;
510
511   if (symbol_lookup_debug)
512     {
513       fprintf_unfiltered (gdb_stdlog,
514                           "cp_lookup_symbol_imports_or_template"
515                           " (%s, %s, %s, %s)\n",
516                           scope, name, host_address_to_string (block),
517                           domain_name (domain));
518     }
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 *sym = search_symbol_list (name,
528                                                    templ->n_template_arguments,
529                                                    templ->template_arguments);
530
531           if (sym != NULL)
532             {
533               if (symbol_lookup_debug)
534                 {
535                   fprintf_unfiltered (gdb_stdlog,
536                                       "cp_lookup_symbol_imports_or_template"
537                                       " (...) = %s\n",
538                                       host_address_to_string (sym));
539                 }
540               return (struct block_symbol) {sym, block};
541             }
542         }
543
544       /* Search the template parameters of the function's defining
545          context.  */
546       if (SYMBOL_NATURAL_NAME (function))
547         {
548           struct type *context;
549           std::string name_copy (SYMBOL_NATURAL_NAME (function));
550           const struct language_defn *lang = language_def (language_cplus);
551           struct gdbarch *arch = symbol_arch (function);
552           const struct block *parent = BLOCK_SUPERBLOCK (block);
553           struct symbol *sym;
554
555           while (1)
556             {
557               unsigned int prefix_len
558                 = cp_entire_prefix_len (name_copy.c_str ());
559
560               if (prefix_len == 0)
561                 context = NULL;
562               else
563                 {
564                   name_copy.erase (prefix_len);
565                   context = lookup_typename (lang, arch,
566                                              name_copy.c_str (),
567                                              parent, 1);
568                 }
569
570               if (context == NULL)
571                 break;
572
573               sym
574                 = search_symbol_list (name,
575                                       TYPE_N_TEMPLATE_ARGUMENTS (context),
576                                       TYPE_TEMPLATE_ARGUMENTS (context));
577               if (sym != NULL)
578                 {
579                   if (symbol_lookup_debug)
580                     {
581                       fprintf_unfiltered
582                         (gdb_stdlog,
583                          "cp_lookup_symbol_imports_or_template (...) = %s\n",
584                          host_address_to_string (sym));
585                     }
586                   return (struct block_symbol) {sym, parent};
587                 }
588             }
589         }
590     }
591
592   result = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1, 1);
593   if (symbol_lookup_debug)
594     {
595       fprintf_unfiltered (gdb_stdlog,
596                           "cp_lookup_symbol_imports_or_template (...) = %s\n",
597                           result.symbol != NULL
598                           ? host_address_to_string (result.symbol) : "NULL");
599     }
600   return result;
601 }
602
603 /* Search for NAME by applying relevant import statements belonging to BLOCK
604    and its parents.  SCOPE is the namespace scope of the context in which the
605    search is being evaluated.  */
606
607 static struct block_symbol
608 cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
609                                   const struct block *block,
610                                   const domain_enum domain)
611 {
612   struct block_symbol sym;
613
614   while (block != NULL)
615     {
616       sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 0, 1);
617       if (sym.symbol)
618         return sym;
619
620       block = BLOCK_SUPERBLOCK (block);
621     }
622
623   return null_block_symbol;
624 }
625
626 /* Searches for NAME in the current namespace, and by applying
627    relevant import statements belonging to BLOCK and its parents.
628    SCOPE is the namespace scope of the context in which the search is
629    being evaluated.  */
630
631 struct block_symbol
632 cp_lookup_symbol_namespace (const char *scope,
633                             const char *name,
634                             const struct block *block,
635                             const domain_enum domain)
636 {
637   struct block_symbol sym;
638
639   if (symbol_lookup_debug)
640     {
641       fprintf_unfiltered (gdb_stdlog,
642                           "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n",
643                           scope, name, host_address_to_string (block),
644                           domain_name (domain));
645     }
646
647   /* First, try to find the symbol in the given namespace.  */
648   sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);
649
650   /* Search for name in namespaces imported to this and parent blocks.  */
651   if (sym.symbol == NULL)
652     sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
653
654   if (symbol_lookup_debug)
655     {
656       fprintf_unfiltered (gdb_stdlog,
657                           "cp_lookup_symbol_namespace (...) = %s\n",
658                           sym.symbol != NULL
659                             ? host_address_to_string (sym.symbol) : "NULL");
660     }
661   return sym;
662 }
663
664 /* Lookup NAME at namespace scope (or, in C terms, in static and
665    global variables).  SCOPE is the namespace that the current
666    function is defined within; only consider namespaces whose length
667    is at least SCOPE_LEN.  Other arguments are as in
668    cp_lookup_symbol_nonlocal.
669
670    For example, if we're within a function A::B::f and looking for a
671    symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
672    SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
673    but with SCOPE_LEN = 1.  And then it calls itself with NAME and
674    SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
675    "A::B::x"; if it doesn't find it, then the second call looks for
676    "A::x", and if that call fails, then the first call looks for
677    "x".  */
678
679 static struct block_symbol
680 lookup_namespace_scope (const struct language_defn *langdef,
681                         const char *name,
682                         const struct block *block,
683                         const domain_enum domain,
684                         const char *scope,
685                         int scope_len)
686 {
687   char *the_namespace;
688
689   if (scope[scope_len] != '\0')
690     {
691       /* Recursively search for names in child namespaces first.  */
692
693       struct block_symbol sym;
694       int new_scope_len = scope_len;
695
696       /* If the current scope is followed by "::", skip past that.  */
697       if (new_scope_len != 0)
698         {
699           gdb_assert (scope[new_scope_len] == ':');
700           new_scope_len += 2;
701         }
702       new_scope_len += cp_find_first_component (scope + new_scope_len);
703       sym = lookup_namespace_scope (langdef, name, block, domain,
704                                     scope, new_scope_len);
705       if (sym.symbol != NULL)
706         return sym;
707     }
708
709   /* Okay, we didn't find a match in our children, so look for the
710      name in the current namespace.
711
712      If we there is no scope and we know we have a bare symbol, then short
713      circuit everything and call cp_lookup_bare_symbol directly.
714      This isn't an optimization, rather it allows us to pass LANGDEF which
715      is needed for primitive type lookup.  The test doesn't have to be
716      perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
717      template symbol with "::" in the argument list) then
718      cp_lookup_symbol_in_namespace will catch it.  */
719
720   if (scope_len == 0 && strchr (name, ':') == NULL)
721     return cp_lookup_bare_symbol (langdef, name, block, domain, 1);
722
723   the_namespace = (char *) alloca (scope_len + 1);
724   strncpy (the_namespace, scope, scope_len);
725   the_namespace[scope_len] = '\0';
726   return cp_lookup_symbol_in_namespace (the_namespace, name,
727                                         block, domain, 1);
728 }
729
730 /* The C++-specific version of name lookup for static and global
731    names.  This makes sure that names get looked for in all namespaces
732    that are in scope.  NAME is the natural name of the symbol that
733    we're looking for, BLOCK is the block that we're searching within,
734    DOMAIN says what kind of symbols we're looking for.  */
735
736 struct block_symbol
737 cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
738                            const char *name,
739                            const struct block *block,
740                            const domain_enum domain)
741 {
742   struct block_symbol sym;
743   const char *scope = block_scope (block);
744
745   if (symbol_lookup_debug)
746     {
747       fprintf_unfiltered (gdb_stdlog,
748                           "cp_lookup_symbol_non_local"
749                           " (%s, %s (scope %s), %s)\n",
750                           name, host_address_to_string (block), scope,
751                           domain_name (domain));
752     }
753
754   /* First, try to find the symbol in the given namespace, and all
755      containing namespaces.  */
756   sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);
757
758   /* Search for name in namespaces imported to this and parent blocks.  */
759   if (sym.symbol == NULL)
760     sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
761
762   if (symbol_lookup_debug)
763     {
764       fprintf_unfiltered (gdb_stdlog,
765                           "cp_lookup_symbol_nonlocal (...) = %s\n",
766                           (sym.symbol != NULL
767                            ? host_address_to_string (sym.symbol)
768                            : "NULL"));
769     }
770   return sym;
771 }
772
773 /* Search through the base classes of PARENT_TYPE for a base class
774    named NAME and return its type.  If not found, return NULL.  */
775
776 struct type *
777 cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
778 {
779   int i;
780
781   parent_type = check_typedef (parent_type);
782   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
783     {
784       struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
785       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
786
787       if (base_name == NULL)
788         continue;
789
790       if (streq (base_name, name))
791         return type;
792
793       type = cp_find_type_baseclass_by_name (type, name);
794       if (type != NULL)
795         return type;
796     }
797
798   return NULL;
799 }
800
801 /* Search through the base classes of PARENT_TYPE for a symbol named
802    NAME in block BLOCK.  */
803
804 static struct block_symbol
805 find_symbol_in_baseclass (struct type *parent_type, const char *name,
806                           const struct block *block, const domain_enum domain,
807                           int is_in_anonymous)
808 {
809   int i;
810   struct block_symbol sym;
811
812   sym.symbol = NULL;
813   sym.block = NULL;
814
815   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
816     {
817       struct type *base_type = TYPE_BASECLASS (parent_type, i);
818       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
819
820       if (base_name == NULL)
821         continue;
822
823       std::string concatenated_name = std::string (base_name) + "::" + name;
824
825       sym = cp_lookup_nested_symbol_1 (base_type, name,
826                                        concatenated_name.c_str (),
827                                        block, domain, 1, is_in_anonymous);
828       if (sym.symbol != NULL)
829         break;
830     }
831
832   return sym;
833 }
834
835 /* Helper function to look up NESTED_NAME in CONTAINER_TYPE and in DOMAIN
836    and within the context of BLOCK.
837    NESTED_NAME may have scope ("::").
838    CONTAINER_TYPE needn't have been "check_typedef'd" yet.
839    CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
840    passed as an argument so that callers can control how space for it is
841    allocated.
842    If BASIC_LOOKUP is non-zero then perform a basic lookup of
843    CONCATENATED_NAME.  See cp_basic_lookup_symbol for details.
844    If IS_IN_ANONYMOUS is non-zero then CONCATENATED_NAME is in an anonymous
845    namespace.  */
846
847 static struct block_symbol
848 cp_lookup_nested_symbol_1 (struct type *container_type,
849                            const char *nested_name,
850                            const char *concatenated_name,
851                            const struct block *block,
852                            const domain_enum domain,
853                            int basic_lookup, int is_in_anonymous)
854 {
855   struct block_symbol sym;
856
857   /* NOTE: carlton/2003-11-10: We don't treat C++ class members
858      of classes like, say, data or function members.  Instead,
859      they're just represented by symbols whose names are
860      qualified by the name of the surrounding class.  This is
861      just like members of namespaces; in particular,
862      cp_basic_lookup_symbol works when looking them up.  */
863
864   if (basic_lookup)
865     {
866       sym = cp_basic_lookup_symbol (concatenated_name, block, domain,
867                                     is_in_anonymous);
868       if (sym.symbol != NULL)
869         return sym;
870     }
871
872   /* Now search all static file-level symbols.  We have to do this for things
873      like typedefs in the class.  We do not try to guess any imported
874      namespace as even the fully specified namespace search is already not
875      C++ compliant and more assumptions could make it too magic.  */
876
877   /* First search in this symtab, what we want is possibly there.  */
878   sym = lookup_symbol_in_static_block (concatenated_name, block, domain);
879   if (sym.symbol != NULL)
880     return sym;
881
882   /* Nope.  We now have to search all static blocks in all objfiles,
883      even if block != NULL, because there's no guarantees as to which
884      symtab the symbol we want is in.  Except for symbols defined in
885      anonymous namespaces should be treated as local to a single file,
886      which we just searched.  */
887   if (!is_in_anonymous)
888     {
889       sym = lookup_static_symbol (concatenated_name, domain);
890       if (sym.symbol != NULL)
891         return sym;
892     }
893
894   /* If this is a class with baseclasses, search them next.  */
895   container_type = check_typedef (container_type);
896   if (TYPE_N_BASECLASSES (container_type) > 0)
897     {
898       sym = find_symbol_in_baseclass (container_type, nested_name, block,
899                                       domain, is_in_anonymous);
900       if (sym.symbol != NULL)
901         return sym;
902     }
903
904   return null_block_symbol;
905 }
906
907 /* Look up a symbol named NESTED_NAME that is nested inside the C++
908    class or namespace given by PARENT_TYPE, from within the context
909    given by BLOCK, and in DOMAIN.
910    Return NULL if there is no such nested symbol.  */
911
912 struct block_symbol
913 cp_lookup_nested_symbol (struct type *parent_type,
914                          const char *nested_name,
915                          const struct block *block,
916                          const domain_enum domain)
917 {
918   /* type_name_or_error provides better error reporting using the
919      original type.  */
920   struct type *saved_parent_type = parent_type;
921
922   parent_type = check_typedef (parent_type);
923
924   if (symbol_lookup_debug)
925     {
926       const char *type_name = TYPE_NAME (saved_parent_type);
927
928       fprintf_unfiltered (gdb_stdlog,
929                           "cp_lookup_nested_symbol (%s, %s, %s, %s)\n",
930                           type_name != NULL ? type_name : "unnamed",
931                           nested_name, host_address_to_string (block),
932                           domain_name (domain));
933     }
934
935   switch (TYPE_CODE (parent_type))
936     {
937     case TYPE_CODE_STRUCT:
938     case TYPE_CODE_NAMESPACE:
939     case TYPE_CODE_UNION:
940     case TYPE_CODE_ENUM:
941     /* NOTE: Handle modules here as well, because Fortran is re-using the C++
942        specific code to lookup nested symbols in modules, by calling the
943        function pointer la_lookup_symbol_nonlocal, which ends up here.  */
944     case TYPE_CODE_MODULE:
945       {
946         int size;
947         const char *parent_name = type_name_or_error (saved_parent_type);
948         struct block_symbol sym;
949         char *concatenated_name;
950         int is_in_anonymous;
951
952         size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
953         concatenated_name = (char *) alloca (size);
954         xsnprintf (concatenated_name, size, "%s::%s",
955                    parent_name, nested_name);
956         is_in_anonymous = cp_is_in_anonymous (concatenated_name);
957
958         sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
959                                          concatenated_name, block, domain,
960                                          1, is_in_anonymous);
961
962         if (symbol_lookup_debug)
963           {
964             fprintf_unfiltered (gdb_stdlog,
965                                 "cp_lookup_nested_symbol (...) = %s\n",
966                                 (sym.symbol != NULL
967                                  ? host_address_to_string (sym.symbol)
968                                  : "NULL"));
969           }
970         return sym;
971       }
972
973     case TYPE_CODE_FUNC:
974     case TYPE_CODE_METHOD:
975       if (symbol_lookup_debug)
976         {
977           fprintf_unfiltered (gdb_stdlog,
978                               "cp_lookup_nested_symbol (...) = NULL"
979                               " (func/method)\n");
980         }
981       return null_block_symbol;
982
983     default:
984       internal_error (__FILE__, __LINE__,
985                       _("cp_lookup_nested_symbol called "
986                         "on a non-aggregate type."));
987     }
988 }
989
990 /* The C++-version of lookup_transparent_type.  */
991
992 /* FIXME: carlton/2004-01-16: The problem that this is trying to
993    address is that, unfortunately, sometimes NAME is wrong: it may not
994    include the name of namespaces enclosing the type in question.
995    lookup_transparent_type gets called when the type in question
996    is a declaration, and we're trying to find its definition; but, for
997    declarations, our type name deduction mechanism doesn't work.
998    There's nothing we can do to fix this in general, I think, in the
999    absence of debug information about namespaces (I've filed PR
1000    gdb/1511 about this); until such debug information becomes more
1001    prevalent, one heuristic which sometimes looks is to search for the
1002    definition in namespaces containing the current namespace.
1003
1004    We should delete this functions once the appropriate debug
1005    information becomes more widespread.  (GCC 3.4 will be the first
1006    released version of GCC with such information.)  */
1007
1008 struct type *
1009 cp_lookup_transparent_type (const char *name)
1010 {
1011   /* First, try the honest way of looking up the definition.  */
1012   struct type *t = basic_lookup_transparent_type (name);
1013   const char *scope;
1014
1015   if (t != NULL)
1016     return t;
1017
1018   /* If that doesn't work and we're within a namespace, look there
1019      instead.  */
1020   scope = block_scope (get_selected_block (0));
1021
1022   if (scope[0] == '\0')
1023     return NULL;
1024
1025   return cp_lookup_transparent_type_loop (name, scope, 0);
1026 }
1027
1028 /* Lookup the type definition associated to NAME in namespaces/classes
1029    containing SCOPE whose name is strictly longer than LENGTH.  LENGTH
1030    must be the index of the start of a component of SCOPE.  */
1031
1032 static struct type *
1033 cp_lookup_transparent_type_loop (const char *name,
1034                                  const char *scope,
1035                                  int length)
1036 {
1037   int scope_length = length + cp_find_first_component (scope + length);
1038   char *full_name;
1039
1040   /* If the current scope is followed by "::", look in the next
1041      component.  */
1042   if (scope[scope_length] == ':')
1043     {
1044       struct type *retval
1045         = cp_lookup_transparent_type_loop (name, scope,
1046                                            scope_length + 2);
1047
1048       if (retval != NULL)
1049         return retval;
1050     }
1051
1052   full_name = (char *) alloca (scope_length + 2 + strlen (name) + 1);
1053   strncpy (full_name, scope, scope_length);
1054   memcpy (full_name + scope_length, "::", 2);
1055   strcpy (full_name + scope_length + 2, name);
1056
1057   return basic_lookup_transparent_type (full_name);
1058 }
1059
1060 /* This used to do something but was removed when it became
1061    obsolete.  */
1062
1063 static void
1064 maintenance_cplus_namespace (const char *args, int from_tty)
1065 {
1066   printf_unfiltered (_("The `maint namespace' command was removed.\n"));
1067 }
1068
1069 void
1070 _initialize_cp_namespace (void)
1071 {
1072   struct cmd_list_element *cmd;
1073
1074   cmd = add_cmd ("namespace", class_maintenance,
1075                  maintenance_cplus_namespace,
1076                  _("Deprecated placeholder for removed functionality."),
1077                  &maint_cplus_cmd_list);
1078   deprecate_cmd (cmd, NULL);
1079 }