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