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