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