xref: /dragonfly/contrib/gdb-7/gdb/cp-namespace.c (revision ef5ccd6c)
15796c8dcSSimon Schubert /* Helper routines for C++ support in GDB.
2*ef5ccd6cSJohn Marino    Copyright (C) 2003-2013 Free Software Foundation, Inc.
35796c8dcSSimon Schubert 
45796c8dcSSimon Schubert    Contributed by David Carlton and by Kealia, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This file is part of GDB.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
95796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
105796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
115796c8dcSSimon Schubert    (at your option) any later version.
125796c8dcSSimon Schubert 
135796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
145796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
155796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
165796c8dcSSimon Schubert    GNU General Public License for more details.
175796c8dcSSimon Schubert 
185796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
195796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert #include "defs.h"
225796c8dcSSimon Schubert #include "cp-support.h"
235796c8dcSSimon Schubert #include "gdb_obstack.h"
245796c8dcSSimon Schubert #include "symtab.h"
255796c8dcSSimon Schubert #include "symfile.h"
265796c8dcSSimon Schubert #include "gdb_assert.h"
275796c8dcSSimon Schubert #include "block.h"
285796c8dcSSimon Schubert #include "objfiles.h"
295796c8dcSSimon Schubert #include "gdbtypes.h"
305796c8dcSSimon Schubert #include "dictionary.h"
315796c8dcSSimon Schubert #include "command.h"
325796c8dcSSimon Schubert #include "frame.h"
335796c8dcSSimon Schubert #include "buildsym.h"
34c50c785cSJohn Marino #include "language.h"
355796c8dcSSimon Schubert 
365796c8dcSSimon Schubert static struct symbol *lookup_namespace_scope (const char *name,
375796c8dcSSimon Schubert 					      const struct block *block,
385796c8dcSSimon Schubert 					      const domain_enum domain,
395796c8dcSSimon Schubert 					      const char *scope,
405796c8dcSSimon Schubert 					      int scope_len);
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert static struct symbol *lookup_symbol_file (const char *name,
435796c8dcSSimon Schubert 					  const struct block *block,
445796c8dcSSimon Schubert 					  const domain_enum domain,
45*ef5ccd6cSJohn Marino 					  int anonymous_namespace,
46*ef5ccd6cSJohn Marino 					  int search);
475796c8dcSSimon Schubert 
485796c8dcSSimon Schubert static struct type *cp_lookup_transparent_type_loop (const char *name,
495796c8dcSSimon Schubert 						     const char *scope,
505796c8dcSSimon Schubert 						     int scope_len);
515796c8dcSSimon Schubert 
525796c8dcSSimon Schubert /* Check to see if SYMBOL refers to an object contained within an
535796c8dcSSimon Schubert    anonymous namespace; if so, add an appropriate using directive.  */
545796c8dcSSimon Schubert 
555796c8dcSSimon Schubert void
cp_scan_for_anonymous_namespaces(const struct symbol * const symbol,struct objfile * const objfile)56a45ae5f8SJohn Marino cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
57a45ae5f8SJohn Marino 				  struct objfile *const objfile)
585796c8dcSSimon Schubert {
595796c8dcSSimon Schubert   if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
605796c8dcSSimon Schubert     {
615796c8dcSSimon Schubert       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
625796c8dcSSimon Schubert       unsigned int previous_component;
635796c8dcSSimon Schubert       unsigned int next_component;
645796c8dcSSimon Schubert 
655796c8dcSSimon Schubert       /* Start with a quick-and-dirty check for mention of "(anonymous
665796c8dcSSimon Schubert 	 namespace)".  */
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert       if (!cp_is_anonymous (name))
695796c8dcSSimon Schubert 	return;
705796c8dcSSimon Schubert 
715796c8dcSSimon Schubert       previous_component = 0;
725796c8dcSSimon Schubert       next_component = cp_find_first_component (name + previous_component);
735796c8dcSSimon Schubert 
745796c8dcSSimon Schubert       while (name[next_component] == ':')
755796c8dcSSimon Schubert 	{
76c50c785cSJohn Marino 	  if (((next_component - previous_component)
77c50c785cSJohn Marino 	       == CP_ANONYMOUS_NAMESPACE_LEN)
785796c8dcSSimon Schubert 	      && strncmp (name + previous_component,
79c50c785cSJohn Marino 			  CP_ANONYMOUS_NAMESPACE_STR,
80c50c785cSJohn Marino 			  CP_ANONYMOUS_NAMESPACE_LEN) == 0)
815796c8dcSSimon Schubert 	    {
82c50c785cSJohn Marino 	      int dest_len = (previous_component == 0
83c50c785cSJohn Marino 			      ? 0 : previous_component - 2);
845796c8dcSSimon Schubert 	      int src_len = next_component;
855796c8dcSSimon Schubert 
865796c8dcSSimon Schubert 	      char *dest = alloca (dest_len + 1);
875796c8dcSSimon Schubert 	      char *src = alloca (src_len + 1);
885796c8dcSSimon Schubert 
895796c8dcSSimon Schubert 	      memcpy (dest, name, dest_len);
905796c8dcSSimon Schubert 	      memcpy (src, name, src_len);
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert 	      dest[dest_len] = '\0';
935796c8dcSSimon Schubert 	      src[src_len] = '\0';
945796c8dcSSimon Schubert 
955796c8dcSSimon Schubert 	      /* We've found a component of the name that's an
965796c8dcSSimon Schubert 		 anonymous namespace.  So add symbols in it to the
975796c8dcSSimon Schubert 		 namespace given by the previous component if there is
985796c8dcSSimon Schubert 		 one, or to the global namespace if there isn't.  */
99*ef5ccd6cSJohn Marino 	      cp_add_using_directive (dest, src, NULL, NULL, NULL, 1,
100a45ae5f8SJohn Marino 	                              &objfile->objfile_obstack);
1015796c8dcSSimon Schubert 	    }
1025796c8dcSSimon Schubert 	  /* The "+ 2" is for the "::".  */
1035796c8dcSSimon Schubert 	  previous_component = next_component + 2;
1045796c8dcSSimon Schubert 	  next_component = (previous_component
1055796c8dcSSimon Schubert 			    + cp_find_first_component (name
1065796c8dcSSimon Schubert 						       + previous_component));
1075796c8dcSSimon Schubert 	}
1085796c8dcSSimon Schubert     }
1095796c8dcSSimon Schubert }
1105796c8dcSSimon Schubert 
111cf7f2e2dSJohn Marino 
112c50c785cSJohn Marino /* Add a using directive to using_directives.  If the using directive
113c50c785cSJohn Marino    in question has already been added, don't add it twice.
114c50c785cSJohn Marino 
115c50c785cSJohn Marino    Create a new struct using_direct which imports the namespace SRC
116c50c785cSJohn Marino    into the scope DEST.  ALIAS is the name of the imported namespace
117c50c785cSJohn Marino    in the current scope.  If ALIAS is NULL then the namespace is known
118c50c785cSJohn Marino    by its original name.  DECLARATION is the name if the imported
119c50c785cSJohn Marino    varable if this is a declaration import (Eg. using A::x), otherwise
120*ef5ccd6cSJohn Marino    it is NULL.  EXCLUDES is a list of names not to import from an
121*ef5ccd6cSJohn Marino    imported module or NULL.  If COPY_NAMES is non-zero, then the
122*ef5ccd6cSJohn Marino    arguments are copied into newly allocated memory so they can be
123*ef5ccd6cSJohn Marino    temporaries.  For EXCLUDES the VEC pointers are copied but the
124a45ae5f8SJohn Marino    pointed to characters are not copied.  */
1255796c8dcSSimon Schubert 
1265796c8dcSSimon Schubert void
cp_add_using_directive(const char * dest,const char * src,const char * alias,const char * declaration,VEC (const_char_ptr)* excludes,int copy_names,struct obstack * obstack)127cf7f2e2dSJohn Marino cp_add_using_directive (const char *dest,
128cf7f2e2dSJohn Marino 			const char *src,
129cf7f2e2dSJohn Marino 			const char *alias,
130cf7f2e2dSJohn Marino 			const char *declaration,
131a45ae5f8SJohn Marino 			VEC (const_char_ptr) *excludes,
132*ef5ccd6cSJohn Marino 			int copy_names,
133cf7f2e2dSJohn Marino                         struct obstack *obstack)
1345796c8dcSSimon Schubert {
1355796c8dcSSimon Schubert   struct using_direct *current;
1365796c8dcSSimon Schubert   struct using_direct *new;
1375796c8dcSSimon Schubert 
1385796c8dcSSimon Schubert   /* Has it already been added?  */
1395796c8dcSSimon Schubert 
1405796c8dcSSimon Schubert   for (current = using_directives; current != NULL; current = current->next)
1415796c8dcSSimon Schubert     {
142a45ae5f8SJohn Marino       int ix;
143a45ae5f8SJohn Marino       const char *param;
144a45ae5f8SJohn Marino 
145a45ae5f8SJohn Marino       if (strcmp (current->import_src, src) != 0)
146a45ae5f8SJohn Marino 	continue;
147a45ae5f8SJohn Marino       if (strcmp (current->import_dest, dest) != 0)
148a45ae5f8SJohn Marino 	continue;
149a45ae5f8SJohn Marino       if ((alias == NULL && current->alias != NULL)
150a45ae5f8SJohn Marino 	  || (alias != NULL && current->alias == NULL)
151cf7f2e2dSJohn Marino 	  || (alias != NULL && current->alias != NULL
152a45ae5f8SJohn Marino 	      && strcmp (alias, current->alias) != 0))
153a45ae5f8SJohn Marino 	continue;
154a45ae5f8SJohn Marino       if ((declaration == NULL && current->declaration != NULL)
155a45ae5f8SJohn Marino 	  || (declaration != NULL && current->declaration == NULL)
156cf7f2e2dSJohn Marino 	  || (declaration != NULL && current->declaration != NULL
157a45ae5f8SJohn Marino 	      && strcmp (declaration, current->declaration) != 0))
158a45ae5f8SJohn Marino 	continue;
159a45ae5f8SJohn Marino 
160a45ae5f8SJohn Marino       /* Compare the contents of EXCLUDES.  */
161a45ae5f8SJohn Marino       for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
162a45ae5f8SJohn Marino 	if (current->excludes[ix] == NULL
163a45ae5f8SJohn Marino 	    || strcmp (param, current->excludes[ix]) != 0)
164a45ae5f8SJohn Marino 	  break;
165a45ae5f8SJohn Marino       if (ix < VEC_length (const_char_ptr, excludes)
166a45ae5f8SJohn Marino 	  || current->excludes[ix] != NULL)
167a45ae5f8SJohn Marino 	continue;
168a45ae5f8SJohn Marino 
169a45ae5f8SJohn Marino       /* Parameters exactly match CURRENT.  */
1705796c8dcSSimon Schubert       return;
1715796c8dcSSimon Schubert     }
1725796c8dcSSimon Schubert 
173a45ae5f8SJohn Marino   new = obstack_alloc (obstack, (sizeof (*new)
174a45ae5f8SJohn Marino 				 + (VEC_length (const_char_ptr, excludes)
175a45ae5f8SJohn Marino 				    * sizeof (*new->excludes))));
176a45ae5f8SJohn Marino   memset (new, 0, sizeof (*new));
1775796c8dcSSimon Schubert 
178*ef5ccd6cSJohn Marino   if (copy_names)
179*ef5ccd6cSJohn Marino     {
180*ef5ccd6cSJohn Marino       new->import_src = obstack_copy0 (obstack, src, strlen (src));
181*ef5ccd6cSJohn Marino       new->import_dest = obstack_copy0 (obstack, dest, strlen (dest));
182*ef5ccd6cSJohn Marino     }
183*ef5ccd6cSJohn Marino   else
184*ef5ccd6cSJohn Marino     {
185*ef5ccd6cSJohn Marino       new->import_src = src;
186*ef5ccd6cSJohn Marino       new->import_dest = dest;
187*ef5ccd6cSJohn Marino     }
188cf7f2e2dSJohn Marino 
189*ef5ccd6cSJohn Marino   if (alias != NULL && copy_names)
190*ef5ccd6cSJohn Marino     new->alias = obstack_copy0 (obstack, alias, strlen (alias));
191*ef5ccd6cSJohn Marino   else
192*ef5ccd6cSJohn Marino     new->alias = alias;
193cf7f2e2dSJohn Marino 
194*ef5ccd6cSJohn Marino   if (declaration != NULL && copy_names)
195*ef5ccd6cSJohn Marino     new->declaration = obstack_copy0 (obstack,
196*ef5ccd6cSJohn Marino 				      declaration, strlen (declaration));
197*ef5ccd6cSJohn Marino   else
198*ef5ccd6cSJohn Marino     new->declaration = declaration;
199cf7f2e2dSJohn Marino 
200a45ae5f8SJohn Marino   memcpy (new->excludes, VEC_address (const_char_ptr, excludes),
201a45ae5f8SJohn Marino 	  VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes));
202a45ae5f8SJohn Marino   new->excludes[VEC_length (const_char_ptr, excludes)] = NULL;
203a45ae5f8SJohn Marino 
204cf7f2e2dSJohn Marino   new->next = using_directives;
205cf7f2e2dSJohn Marino   using_directives = new;
2065796c8dcSSimon Schubert }
2075796c8dcSSimon Schubert 
2085796c8dcSSimon Schubert /* Test whether or not NAMESPACE looks like it mentions an anonymous
2095796c8dcSSimon Schubert    namespace; return nonzero if so.  */
2105796c8dcSSimon Schubert 
2115796c8dcSSimon Schubert int
cp_is_anonymous(const char * namespace)2125796c8dcSSimon Schubert cp_is_anonymous (const char *namespace)
2135796c8dcSSimon Schubert {
214c50c785cSJohn Marino   return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR)
2155796c8dcSSimon Schubert 	  != NULL);
2165796c8dcSSimon Schubert }
2175796c8dcSSimon Schubert 
2185796c8dcSSimon Schubert /* The C++-specific version of name lookup for static and global
2195796c8dcSSimon Schubert    names.  This makes sure that names get looked for in all namespaces
2205796c8dcSSimon Schubert    that are in scope.  NAME is the natural name of the symbol that
221cf7f2e2dSJohn Marino    we're looking for, BLOCK is the block that we're searching within,
222c50c785cSJohn Marino    DOMAIN says what kind of symbols we're looking for, and if SYMTAB
223c50c785cSJohn Marino    is non-NULL, we should store the symtab where we found the symbol
224c50c785cSJohn Marino    in it.  */
2255796c8dcSSimon Schubert 
2265796c8dcSSimon Schubert struct symbol *
cp_lookup_symbol_nonlocal(const char * name,const struct block * block,const domain_enum domain)2275796c8dcSSimon Schubert cp_lookup_symbol_nonlocal (const char *name,
2285796c8dcSSimon Schubert 			   const struct block *block,
2295796c8dcSSimon Schubert 			   const domain_enum domain)
2305796c8dcSSimon Schubert {
231cf7f2e2dSJohn Marino   struct symbol *sym;
232cf7f2e2dSJohn Marino   const char *scope = block_scope (block);
233cf7f2e2dSJohn Marino 
234c50c785cSJohn Marino   sym = lookup_namespace_scope (name, block,
235c50c785cSJohn Marino 				domain, scope, 0);
236cf7f2e2dSJohn Marino   if (sym != NULL)
237cf7f2e2dSJohn Marino     return sym;
238cf7f2e2dSJohn Marino 
239c50c785cSJohn Marino   return cp_lookup_symbol_namespace (scope, name,
240c50c785cSJohn Marino 				     block, domain);
241cf7f2e2dSJohn Marino }
242cf7f2e2dSJohn Marino 
243c50c785cSJohn Marino /* Look up NAME in the C++ namespace NAMESPACE.  Other arguments are
244*ef5ccd6cSJohn Marino    as in cp_lookup_symbol_nonlocal.  If SEARCH is non-zero, search
245*ef5ccd6cSJohn Marino    through base classes for a matching symbol.  */
246cf7f2e2dSJohn Marino 
247cf7f2e2dSJohn Marino static struct symbol *
cp_lookup_symbol_in_namespace(const char * namespace,const char * name,const struct block * block,const domain_enum domain,int search)248cf7f2e2dSJohn Marino cp_lookup_symbol_in_namespace (const char *namespace,
249cf7f2e2dSJohn Marino                                const char *name,
250cf7f2e2dSJohn Marino                                const struct block *block,
251*ef5ccd6cSJohn Marino                                const domain_enum domain, int search)
252cf7f2e2dSJohn Marino {
253cf7f2e2dSJohn Marino   if (namespace[0] == '\0')
254cf7f2e2dSJohn Marino     {
255*ef5ccd6cSJohn Marino       return lookup_symbol_file (name, block, domain, 0, search);
256cf7f2e2dSJohn Marino     }
257cf7f2e2dSJohn Marino   else
258cf7f2e2dSJohn Marino     {
259c50c785cSJohn Marino       char *concatenated_name = alloca (strlen (namespace) + 2
260c50c785cSJohn Marino 					+ strlen (name) + 1);
261cf7f2e2dSJohn Marino 
262cf7f2e2dSJohn Marino       strcpy (concatenated_name, namespace);
263cf7f2e2dSJohn Marino       strcat (concatenated_name, "::");
264cf7f2e2dSJohn Marino       strcat (concatenated_name, name);
265c50c785cSJohn Marino       return lookup_symbol_file (concatenated_name, block, domain,
266*ef5ccd6cSJohn Marino 				 cp_is_anonymous (namespace), search);
267cf7f2e2dSJohn Marino     }
268cf7f2e2dSJohn Marino }
269cf7f2e2dSJohn Marino 
270cf7f2e2dSJohn Marino /* Used for cleanups to reset the "searched" flag incase
271cf7f2e2dSJohn Marino    of an error.  */
272cf7f2e2dSJohn Marino 
273cf7f2e2dSJohn Marino static void
reset_directive_searched(void * data)274cf7f2e2dSJohn Marino reset_directive_searched (void *data)
275cf7f2e2dSJohn Marino {
276cf7f2e2dSJohn Marino   struct using_direct *direct = data;
277cf7f2e2dSJohn Marino   direct->searched = 0;
278cf7f2e2dSJohn Marino }
279cf7f2e2dSJohn Marino 
280c50c785cSJohn Marino /* Search for NAME by applying all import statements belonging to
281c50c785cSJohn Marino    BLOCK which are applicable in SCOPE.  If DECLARATION_ONLY the
282c50c785cSJohn Marino    search is restricted to using declarations.
283cf7f2e2dSJohn Marino    Example:
284cf7f2e2dSJohn Marino 
285cf7f2e2dSJohn Marino      namespace A {
286cf7f2e2dSJohn Marino        int x;
287cf7f2e2dSJohn Marino      }
288cf7f2e2dSJohn Marino      using A::x;
289cf7f2e2dSJohn Marino 
290c50c785cSJohn Marino    If SEARCH_PARENTS the search will include imports which are
291c50c785cSJohn Marino    applicable in parents of SCOPE.
292cf7f2e2dSJohn Marino    Example:
293cf7f2e2dSJohn Marino 
294cf7f2e2dSJohn Marino      namespace A {
295cf7f2e2dSJohn Marino        using namespace X;
296cf7f2e2dSJohn Marino        namespace B {
297cf7f2e2dSJohn Marino          using namespace Y;
298cf7f2e2dSJohn Marino        }
299cf7f2e2dSJohn Marino      }
300cf7f2e2dSJohn Marino 
301c50c785cSJohn Marino    If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
302c50c785cSJohn Marino    namespaces X and Y will be considered.  If SEARCH_PARENTS is false
303c50c785cSJohn Marino    only the import of Y is considered.  */
304cf7f2e2dSJohn Marino 
305cf7f2e2dSJohn Marino struct symbol *
cp_lookup_symbol_imports(const char * scope,const char * name,const struct block * block,const domain_enum domain,const int declaration_only,const int search_parents)306cf7f2e2dSJohn Marino cp_lookup_symbol_imports (const char *scope,
307cf7f2e2dSJohn Marino                           const char *name,
308cf7f2e2dSJohn Marino                           const struct block *block,
309cf7f2e2dSJohn Marino                           const domain_enum domain,
310cf7f2e2dSJohn Marino                           const int declaration_only,
311cf7f2e2dSJohn Marino                           const int search_parents)
312cf7f2e2dSJohn Marino {
313cf7f2e2dSJohn Marino   struct using_direct *current;
314cf7f2e2dSJohn Marino   struct symbol *sym = NULL;
315cf7f2e2dSJohn Marino   int len;
316cf7f2e2dSJohn Marino   int directive_match;
317cf7f2e2dSJohn Marino   struct cleanup *searched_cleanup;
318cf7f2e2dSJohn Marino 
319cf7f2e2dSJohn Marino   /* First, try to find the symbol in the given namespace.  */
320cf7f2e2dSJohn Marino   if (!declaration_only)
321c50c785cSJohn Marino     sym = cp_lookup_symbol_in_namespace (scope, name,
322*ef5ccd6cSJohn Marino 					 block, domain, 1);
323cf7f2e2dSJohn Marino 
324cf7f2e2dSJohn Marino   if (sym != NULL)
325cf7f2e2dSJohn Marino     return sym;
326cf7f2e2dSJohn Marino 
327c50c785cSJohn Marino   /* Go through the using directives.  If any of them add new names to
328c50c785cSJohn Marino      the namespace we're searching in, see if we can find a match by
329c50c785cSJohn Marino      applying them.  */
330cf7f2e2dSJohn Marino 
331cf7f2e2dSJohn Marino   for (current = block_using (block);
332cf7f2e2dSJohn Marino        current != NULL;
333cf7f2e2dSJohn Marino        current = current->next)
334cf7f2e2dSJohn Marino     {
335a45ae5f8SJohn Marino       const char **excludep;
336a45ae5f8SJohn Marino 
337cf7f2e2dSJohn Marino       len = strlen (current->import_dest);
338cf7f2e2dSJohn Marino       directive_match = (search_parents
339cf7f2e2dSJohn Marino                          ? (strncmp (scope, current->import_dest,
340cf7f2e2dSJohn Marino                                      strlen (current->import_dest)) == 0
341cf7f2e2dSJohn Marino                             && (len == 0
342c50c785cSJohn Marino                                 || scope[len] == ':'
343c50c785cSJohn Marino 				|| scope[len] == '\0'))
344cf7f2e2dSJohn Marino                          : strcmp (scope, current->import_dest) == 0);
345cf7f2e2dSJohn Marino 
346c50c785cSJohn Marino       /* If the import destination is the current scope or one of its
347c50c785cSJohn Marino          ancestors then it is applicable.  */
348cf7f2e2dSJohn Marino       if (directive_match && !current->searched)
349cf7f2e2dSJohn Marino 	{
350c50c785cSJohn Marino 	  /* Mark this import as searched so that the recursive call
351c50c785cSJohn Marino 	     does not search it again.  */
352cf7f2e2dSJohn Marino 	  current->searched = 1;
353c50c785cSJohn Marino 	  searched_cleanup = make_cleanup (reset_directive_searched,
354c50c785cSJohn Marino 					   current);
355cf7f2e2dSJohn Marino 
356c50c785cSJohn Marino 	  /* If there is an import of a single declaration, compare the
357c50c785cSJohn Marino 	     imported declaration (after optional renaming by its alias)
358c50c785cSJohn Marino 	     with the sought out name.  If there is a match pass
359c50c785cSJohn Marino 	     current->import_src as NAMESPACE to direct the search
360c50c785cSJohn Marino 	     towards the imported namespace.  */
361cf7f2e2dSJohn Marino 	  if (current->declaration
362c50c785cSJohn Marino 	      && strcmp (name, current->alias
363c50c785cSJohn Marino 			 ? current->alias : current->declaration) == 0)
364cf7f2e2dSJohn Marino 	    sym = cp_lookup_symbol_in_namespace (current->import_src,
365cf7f2e2dSJohn Marino 						 current->declaration,
366*ef5ccd6cSJohn Marino 						 block, domain, 1);
367cf7f2e2dSJohn Marino 
368c50c785cSJohn Marino 	  /* If this is a DECLARATION_ONLY search or a symbol was found
369c50c785cSJohn Marino 	     or this import statement was an import declaration, the
370c50c785cSJohn Marino 	     search of this import is complete.  */
371cf7f2e2dSJohn Marino 	  if (declaration_only || sym != NULL || current->declaration)
372cf7f2e2dSJohn Marino 	    {
373cf7f2e2dSJohn Marino 	      current->searched = 0;
374cf7f2e2dSJohn Marino 	      discard_cleanups (searched_cleanup);
375cf7f2e2dSJohn Marino 
376cf7f2e2dSJohn Marino 	      if (sym != NULL)
377cf7f2e2dSJohn Marino 		return sym;
378cf7f2e2dSJohn Marino 
379cf7f2e2dSJohn Marino 	      continue;
380cf7f2e2dSJohn Marino 	    }
381cf7f2e2dSJohn Marino 
382a45ae5f8SJohn Marino 	  /* Do not follow CURRENT if NAME matches its EXCLUDES.  */
383a45ae5f8SJohn Marino 	  for (excludep = current->excludes; *excludep; excludep++)
384a45ae5f8SJohn Marino 	    if (strcmp (name, *excludep) == 0)
385a45ae5f8SJohn Marino 	      break;
386a45ae5f8SJohn Marino 	  if (*excludep)
387a45ae5f8SJohn Marino 	    {
388a45ae5f8SJohn Marino 	      discard_cleanups (searched_cleanup);
389a45ae5f8SJohn Marino 	      continue;
390a45ae5f8SJohn Marino 	    }
391a45ae5f8SJohn Marino 
392c50c785cSJohn Marino 	  if (current->alias != NULL
393c50c785cSJohn Marino 	      && strcmp (name, current->alias) == 0)
394c50c785cSJohn Marino 	    /* If the import is creating an alias and the alias matches
395c50c785cSJohn Marino 	       the sought name.  Pass current->import_src as the NAME to
396c50c785cSJohn Marino 	       direct the search towards the aliased namespace.  */
397cf7f2e2dSJohn Marino 	    {
398cf7f2e2dSJohn Marino 	      sym = cp_lookup_symbol_in_namespace (scope,
399cf7f2e2dSJohn Marino 						   current->import_src,
400*ef5ccd6cSJohn Marino 						   block, domain, 1);
401cf7f2e2dSJohn Marino 	    }
402cf7f2e2dSJohn Marino 	  else if (current->alias == NULL)
403cf7f2e2dSJohn Marino 	    {
404c50c785cSJohn Marino 	      /* If this import statement creates no alias, pass
405c50c785cSJohn Marino 		 current->inner as NAMESPACE to direct the search
406c50c785cSJohn Marino 		 towards the imported namespace.  */
407cf7f2e2dSJohn Marino 	      sym = cp_lookup_symbol_imports (current->import_src,
408c50c785cSJohn Marino 					      name, block,
409c50c785cSJohn Marino 					      domain, 0, 0);
410cf7f2e2dSJohn Marino 	    }
411cf7f2e2dSJohn Marino 	  current->searched = 0;
412cf7f2e2dSJohn Marino 	  discard_cleanups (searched_cleanup);
413cf7f2e2dSJohn Marino 
414cf7f2e2dSJohn Marino 	  if (sym != NULL)
415cf7f2e2dSJohn Marino 	    return sym;
416cf7f2e2dSJohn Marino 	}
417cf7f2e2dSJohn Marino     }
418cf7f2e2dSJohn Marino 
419cf7f2e2dSJohn Marino   return NULL;
420cf7f2e2dSJohn Marino }
421cf7f2e2dSJohn Marino 
422c50c785cSJohn Marino /* Helper function that searches an array of symbols for one named
423c50c785cSJohn Marino    NAME.  */
424c50c785cSJohn Marino 
425c50c785cSJohn Marino static struct symbol *
search_symbol_list(const char * name,int num,struct symbol ** syms)426c50c785cSJohn Marino search_symbol_list (const char *name, int num,
427c50c785cSJohn Marino 		    struct symbol **syms)
428c50c785cSJohn Marino {
429c50c785cSJohn Marino   int i;
430c50c785cSJohn Marino 
431c50c785cSJohn Marino   /* Maybe we should store a dictionary in here instead.  */
432c50c785cSJohn Marino   for (i = 0; i < num; ++i)
433c50c785cSJohn Marino     {
434c50c785cSJohn Marino       if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
435c50c785cSJohn Marino 	return syms[i];
436c50c785cSJohn Marino     }
437c50c785cSJohn Marino   return NULL;
438c50c785cSJohn Marino }
439c50c785cSJohn Marino 
440c50c785cSJohn Marino /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it
441c50c785cSJohn Marino    searches through the template parameters of the function and the
442c50c785cSJohn Marino    function's type.  */
443c50c785cSJohn Marino 
444c50c785cSJohn Marino struct symbol *
cp_lookup_symbol_imports_or_template(const char * scope,const char * name,const struct block * block,const domain_enum domain)445c50c785cSJohn Marino cp_lookup_symbol_imports_or_template (const char *scope,
446c50c785cSJohn Marino 				      const char *name,
447c50c785cSJohn Marino 				      const struct block *block,
448c50c785cSJohn Marino 				      const domain_enum domain)
449c50c785cSJohn Marino {
450c50c785cSJohn Marino   struct symbol *function = BLOCK_FUNCTION (block);
451c50c785cSJohn Marino 
452c50c785cSJohn Marino   if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
453c50c785cSJohn Marino     {
454c50c785cSJohn Marino       /* Search the function's template parameters.  */
455c50c785cSJohn Marino       if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
456c50c785cSJohn Marino 	{
457c50c785cSJohn Marino 	  struct template_symbol *templ
458c50c785cSJohn Marino 	    = (struct template_symbol *) function;
459c50c785cSJohn Marino 	  struct symbol *result;
460c50c785cSJohn Marino 
461c50c785cSJohn Marino 	  result = search_symbol_list (name,
462c50c785cSJohn Marino 				       templ->n_template_arguments,
463c50c785cSJohn Marino 				       templ->template_arguments);
464c50c785cSJohn Marino 	  if (result != NULL)
465c50c785cSJohn Marino 	    return result;
466c50c785cSJohn Marino 	}
467c50c785cSJohn Marino 
468c50c785cSJohn Marino       /* Search the template parameters of the function's defining
469c50c785cSJohn Marino 	 context.  */
470c50c785cSJohn Marino       if (SYMBOL_NATURAL_NAME (function))
471c50c785cSJohn Marino 	{
472c50c785cSJohn Marino 	  struct type *context;
473c50c785cSJohn Marino 	  char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
474c50c785cSJohn Marino 	  struct cleanup *cleanups = make_cleanup (xfree, name_copy);
475c50c785cSJohn Marino 	  const struct language_defn *lang = language_def (language_cplus);
476c50c785cSJohn Marino 	  struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch;
477c50c785cSJohn Marino 	  const struct block *parent = BLOCK_SUPERBLOCK (block);
478c50c785cSJohn Marino 
479c50c785cSJohn Marino 	  while (1)
480c50c785cSJohn Marino 	    {
481c50c785cSJohn Marino 	      struct symbol *result;
482c50c785cSJohn Marino 	      unsigned int prefix_len = cp_entire_prefix_len (name_copy);
483c50c785cSJohn Marino 
484c50c785cSJohn Marino 	      if (prefix_len == 0)
485c50c785cSJohn Marino 		context = NULL;
486c50c785cSJohn Marino 	      else
487c50c785cSJohn Marino 		{
488c50c785cSJohn Marino 		  name_copy[prefix_len] = '\0';
489c50c785cSJohn Marino 		  context = lookup_typename (lang, arch,
490c50c785cSJohn Marino 					     name_copy,
491c50c785cSJohn Marino 					     parent, 1);
492c50c785cSJohn Marino 		}
493c50c785cSJohn Marino 
494c50c785cSJohn Marino 	      if (context == NULL)
495c50c785cSJohn Marino 		break;
496c50c785cSJohn Marino 
497c50c785cSJohn Marino 	      result
498c50c785cSJohn Marino 		= search_symbol_list (name,
499c50c785cSJohn Marino 				      TYPE_N_TEMPLATE_ARGUMENTS (context),
500c50c785cSJohn Marino 				      TYPE_TEMPLATE_ARGUMENTS (context));
501c50c785cSJohn Marino 	      if (result != NULL)
502c50c785cSJohn Marino 		return result;
503c50c785cSJohn Marino 	    }
504c50c785cSJohn Marino 
505c50c785cSJohn Marino 	  do_cleanups (cleanups);
506c50c785cSJohn Marino 	}
507c50c785cSJohn Marino     }
508c50c785cSJohn Marino 
509c50c785cSJohn Marino   return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1);
510c50c785cSJohn Marino }
511c50c785cSJohn Marino 
512c50c785cSJohn Marino  /* Searches for NAME in the current namespace, and by applying
513c50c785cSJohn Marino     relevant import statements belonging to BLOCK and its parents.
514c50c785cSJohn Marino     SCOPE is the namespace scope of the context in which the search is
515c50c785cSJohn Marino     being evaluated.  */
516cf7f2e2dSJohn Marino 
517cf7f2e2dSJohn Marino struct symbol*
cp_lookup_symbol_namespace(const char * scope,const char * name,const struct block * block,const domain_enum domain)518cf7f2e2dSJohn Marino cp_lookup_symbol_namespace (const char *scope,
519cf7f2e2dSJohn Marino                             const char *name,
520cf7f2e2dSJohn Marino                             const struct block *block,
521cf7f2e2dSJohn Marino                             const domain_enum domain)
522cf7f2e2dSJohn Marino {
523cf7f2e2dSJohn Marino   struct symbol *sym;
524cf7f2e2dSJohn Marino 
525cf7f2e2dSJohn Marino   /* First, try to find the symbol in the given namespace.  */
526c50c785cSJohn Marino   sym = cp_lookup_symbol_in_namespace (scope, name,
527*ef5ccd6cSJohn Marino 				       block, domain, 1);
528cf7f2e2dSJohn Marino   if (sym != NULL)
529cf7f2e2dSJohn Marino     return sym;
530cf7f2e2dSJohn Marino 
531c50c785cSJohn Marino   /* Search for name in namespaces imported to this and parent
532c50c785cSJohn Marino      blocks.  */
533cf7f2e2dSJohn Marino   while (block != NULL)
534cf7f2e2dSJohn Marino     {
535c50c785cSJohn Marino       sym = cp_lookup_symbol_imports (scope, name, block,
536c50c785cSJohn Marino 				      domain, 0, 1);
537cf7f2e2dSJohn Marino 
538cf7f2e2dSJohn Marino       if (sym)
539cf7f2e2dSJohn Marino 	return sym;
540cf7f2e2dSJohn Marino 
541cf7f2e2dSJohn Marino       block = BLOCK_SUPERBLOCK (block);
542cf7f2e2dSJohn Marino     }
543cf7f2e2dSJohn Marino 
544cf7f2e2dSJohn Marino   return NULL;
5455796c8dcSSimon Schubert }
5465796c8dcSSimon Schubert 
5475796c8dcSSimon Schubert /* Lookup NAME at namespace scope (or, in C terms, in static and
5485796c8dcSSimon Schubert    global variables).  SCOPE is the namespace that the current
5495796c8dcSSimon Schubert    function is defined within; only consider namespaces whose length
5505796c8dcSSimon Schubert    is at least SCOPE_LEN.  Other arguments are as in
5515796c8dcSSimon Schubert    cp_lookup_symbol_nonlocal.
5525796c8dcSSimon Schubert 
5535796c8dcSSimon Schubert    For example, if we're within a function A::B::f and looking for a
5545796c8dcSSimon Schubert    symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
5555796c8dcSSimon Schubert    SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
5565796c8dcSSimon Schubert    but with SCOPE_LEN = 1.  And then it calls itself with NAME and
5575796c8dcSSimon Schubert    SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
5585796c8dcSSimon Schubert    "A::B::x"; if it doesn't find it, then the second call looks for
5595796c8dcSSimon Schubert    "A::x", and if that call fails, then the first call looks for
5605796c8dcSSimon Schubert    "x".  */
5615796c8dcSSimon Schubert 
5625796c8dcSSimon Schubert static struct symbol *
lookup_namespace_scope(const char * name,const struct block * block,const domain_enum domain,const char * scope,int scope_len)5635796c8dcSSimon Schubert lookup_namespace_scope (const char *name,
5645796c8dcSSimon Schubert 			const struct block *block,
5655796c8dcSSimon Schubert 			const domain_enum domain,
5665796c8dcSSimon Schubert 			const char *scope,
5675796c8dcSSimon Schubert 			int scope_len)
5685796c8dcSSimon Schubert {
5695796c8dcSSimon Schubert   char *namespace;
5705796c8dcSSimon Schubert 
5715796c8dcSSimon Schubert   if (scope[scope_len] != '\0')
5725796c8dcSSimon Schubert     {
5735796c8dcSSimon Schubert       /* Recursively search for names in child namespaces first.  */
5745796c8dcSSimon Schubert 
5755796c8dcSSimon Schubert       struct symbol *sym;
5765796c8dcSSimon Schubert       int new_scope_len = scope_len;
5775796c8dcSSimon Schubert 
5785796c8dcSSimon Schubert       /* If the current scope is followed by "::", skip past that.  */
5795796c8dcSSimon Schubert       if (new_scope_len != 0)
5805796c8dcSSimon Schubert 	{
5815796c8dcSSimon Schubert 	  gdb_assert (scope[new_scope_len] == ':');
5825796c8dcSSimon Schubert 	  new_scope_len += 2;
5835796c8dcSSimon Schubert 	}
5845796c8dcSSimon Schubert       new_scope_len += cp_find_first_component (scope + new_scope_len);
585c50c785cSJohn Marino       sym = lookup_namespace_scope (name, block, domain,
586c50c785cSJohn Marino 				    scope, new_scope_len);
5875796c8dcSSimon Schubert       if (sym != NULL)
5885796c8dcSSimon Schubert 	return sym;
5895796c8dcSSimon Schubert     }
5905796c8dcSSimon Schubert 
5915796c8dcSSimon Schubert   /* Okay, we didn't find a match in our children, so look for the
5925796c8dcSSimon Schubert      name in the current namespace.  */
5935796c8dcSSimon Schubert 
5945796c8dcSSimon Schubert   namespace = alloca (scope_len + 1);
5955796c8dcSSimon Schubert   strncpy (namespace, scope, scope_len);
5965796c8dcSSimon Schubert   namespace[scope_len] = '\0';
597c50c785cSJohn Marino   return cp_lookup_symbol_in_namespace (namespace, name,
598*ef5ccd6cSJohn Marino 					block, domain, 1);
5995796c8dcSSimon Schubert }
6005796c8dcSSimon Schubert 
6015796c8dcSSimon Schubert /* Look up NAME in BLOCK's static block and in global blocks.  If
6025796c8dcSSimon Schubert    ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
603*ef5ccd6cSJohn Marino    within an anonymous namespace.  If SEARCH is non-zero, search through
604*ef5ccd6cSJohn Marino    base classes for a matching symbol.  Other arguments are as in
6055796c8dcSSimon Schubert    cp_lookup_symbol_nonlocal.  */
6065796c8dcSSimon Schubert 
6075796c8dcSSimon Schubert static struct symbol *
lookup_symbol_file(const char * name,const struct block * block,const domain_enum domain,int anonymous_namespace,int search)6085796c8dcSSimon Schubert lookup_symbol_file (const char *name,
6095796c8dcSSimon Schubert 		    const struct block *block,
6105796c8dcSSimon Schubert 		    const domain_enum domain,
611*ef5ccd6cSJohn Marino 		    int anonymous_namespace, int search)
6125796c8dcSSimon Schubert {
6135796c8dcSSimon Schubert   struct symbol *sym = NULL;
6145796c8dcSSimon Schubert 
615cf7f2e2dSJohn Marino   sym = lookup_symbol_static (name, block, domain);
6165796c8dcSSimon Schubert   if (sym != NULL)
6175796c8dcSSimon Schubert     return sym;
6185796c8dcSSimon Schubert 
6195796c8dcSSimon Schubert   if (anonymous_namespace)
6205796c8dcSSimon Schubert     {
6215796c8dcSSimon Schubert       /* Symbols defined in anonymous namespaces have external linkage
6225796c8dcSSimon Schubert 	 but should be treated as local to a single file nonetheless.
6235796c8dcSSimon Schubert 	 So we only search the current file's global block.  */
6245796c8dcSSimon Schubert 
6255796c8dcSSimon Schubert       const struct block *global_block = block_global_block (block);
6265796c8dcSSimon Schubert 
6275796c8dcSSimon Schubert       if (global_block != NULL)
628cf7f2e2dSJohn Marino 	sym = lookup_symbol_aux_block (name, global_block, domain);
6295796c8dcSSimon Schubert     }
6305796c8dcSSimon Schubert   else
6315796c8dcSSimon Schubert     {
632cf7f2e2dSJohn Marino       sym = lookup_symbol_global (name, block, domain);
6335796c8dcSSimon Schubert     }
6345796c8dcSSimon Schubert 
635*ef5ccd6cSJohn Marino   if (sym != NULL)
636*ef5ccd6cSJohn Marino     return sym;
637*ef5ccd6cSJohn Marino 
638*ef5ccd6cSJohn Marino   if (search)
639*ef5ccd6cSJohn Marino     {
640*ef5ccd6cSJohn Marino       char *klass, *nested;
641*ef5ccd6cSJohn Marino       unsigned int prefix_len;
642*ef5ccd6cSJohn Marino       struct cleanup *cleanup;
643*ef5ccd6cSJohn Marino       struct symbol *klass_sym;
644*ef5ccd6cSJohn Marino 
645*ef5ccd6cSJohn Marino       /* A simple lookup failed.  Check if the symbol was defined in
646*ef5ccd6cSJohn Marino 	 a base class.  */
647*ef5ccd6cSJohn Marino 
648*ef5ccd6cSJohn Marino       cleanup = make_cleanup (null_cleanup, NULL);
649*ef5ccd6cSJohn Marino 
650*ef5ccd6cSJohn Marino       /* Find the name of the class and the name of the method,
651*ef5ccd6cSJohn Marino 	 variable, etc.  */
652*ef5ccd6cSJohn Marino       prefix_len = cp_entire_prefix_len (name);
653*ef5ccd6cSJohn Marino 
654*ef5ccd6cSJohn Marino       /* If no prefix was found, search "this".  */
655*ef5ccd6cSJohn Marino       if (prefix_len == 0)
656*ef5ccd6cSJohn Marino 	{
657*ef5ccd6cSJohn Marino 	  struct type *type;
658*ef5ccd6cSJohn Marino 	  struct symbol *this;
659*ef5ccd6cSJohn Marino 
660*ef5ccd6cSJohn Marino 	  this = lookup_language_this (language_def (language_cplus), block);
661*ef5ccd6cSJohn Marino 	  if (this == NULL)
662*ef5ccd6cSJohn Marino 	    {
663*ef5ccd6cSJohn Marino 	      do_cleanups (cleanup);
664*ef5ccd6cSJohn Marino 	      return NULL;
665*ef5ccd6cSJohn Marino 	    }
666*ef5ccd6cSJohn Marino 
667*ef5ccd6cSJohn Marino 	  type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this)));
668*ef5ccd6cSJohn Marino 	  klass = xstrdup (TYPE_NAME (type));
669*ef5ccd6cSJohn Marino 	  nested = xstrdup (name);
670*ef5ccd6cSJohn Marino 	}
671*ef5ccd6cSJohn Marino       else
672*ef5ccd6cSJohn Marino 	{
673*ef5ccd6cSJohn Marino 	  /* The class name is everything up to and including PREFIX_LEN.  */
674*ef5ccd6cSJohn Marino 	  klass = savestring (name, prefix_len);
675*ef5ccd6cSJohn Marino 
676*ef5ccd6cSJohn Marino 	  /* The rest of the name is everything else past the initial scope
677*ef5ccd6cSJohn Marino 	     operator.  */
678*ef5ccd6cSJohn Marino 	  nested = xstrdup (name + prefix_len + 2);
679*ef5ccd6cSJohn Marino 	}
680*ef5ccd6cSJohn Marino 
681*ef5ccd6cSJohn Marino       /* Add cleanups to free memory for these strings.  */
682*ef5ccd6cSJohn Marino       make_cleanup (xfree, klass);
683*ef5ccd6cSJohn Marino       make_cleanup (xfree, nested);
684*ef5ccd6cSJohn Marino 
685*ef5ccd6cSJohn Marino       /* Lookup a class named KLASS.  If none is found, there is nothing
686*ef5ccd6cSJohn Marino 	 more that can be done.  */
687*ef5ccd6cSJohn Marino       klass_sym = lookup_symbol_global (klass, block, domain);
688*ef5ccd6cSJohn Marino       if (klass_sym == NULL)
689*ef5ccd6cSJohn Marino 	{
690*ef5ccd6cSJohn Marino 	  do_cleanups (cleanup);
691*ef5ccd6cSJohn Marino 	  return NULL;
692*ef5ccd6cSJohn Marino 	}
693*ef5ccd6cSJohn Marino 
694*ef5ccd6cSJohn Marino       /* Look for a symbol named NESTED in this class.  */
695*ef5ccd6cSJohn Marino       sym = cp_lookup_nested_symbol (SYMBOL_TYPE (klass_sym), nested, block);
696*ef5ccd6cSJohn Marino       do_cleanups (cleanup);
697*ef5ccd6cSJohn Marino     }
698*ef5ccd6cSJohn Marino 
6995796c8dcSSimon Schubert   return sym;
7005796c8dcSSimon Schubert }
7015796c8dcSSimon Schubert 
702*ef5ccd6cSJohn Marino /* Search through the base classes of PARENT_TYPE for a symbol named
703*ef5ccd6cSJohn Marino    NAME in block BLOCK.  */
704*ef5ccd6cSJohn Marino 
705*ef5ccd6cSJohn Marino static struct symbol *
find_symbol_in_baseclass(struct type * parent_type,const char * name,const struct block * block)706*ef5ccd6cSJohn Marino find_symbol_in_baseclass (struct type *parent_type, const char *name,
707*ef5ccd6cSJohn Marino 			   const struct block *block)
708*ef5ccd6cSJohn Marino {
709*ef5ccd6cSJohn Marino   int i;
710*ef5ccd6cSJohn Marino   struct symbol *sym;
711*ef5ccd6cSJohn Marino   struct cleanup *cleanup;
712*ef5ccd6cSJohn Marino   char *concatenated_name;
713*ef5ccd6cSJohn Marino 
714*ef5ccd6cSJohn Marino   sym = NULL;
715*ef5ccd6cSJohn Marino   concatenated_name = NULL;
716*ef5ccd6cSJohn Marino   cleanup = make_cleanup (free_current_contents, &concatenated_name);
717*ef5ccd6cSJohn Marino   for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
718*ef5ccd6cSJohn Marino     {
719*ef5ccd6cSJohn Marino       size_t len;
720*ef5ccd6cSJohn Marino       struct type *base_type = TYPE_BASECLASS (parent_type, i);
721*ef5ccd6cSJohn Marino       const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
722*ef5ccd6cSJohn Marino 
723*ef5ccd6cSJohn Marino       if (base_name == NULL)
724*ef5ccd6cSJohn Marino 	continue;
725*ef5ccd6cSJohn Marino 
726*ef5ccd6cSJohn Marino       /* Search this particular base class.  */
727*ef5ccd6cSJohn Marino       sym = cp_lookup_symbol_in_namespace (base_name, name, block,
728*ef5ccd6cSJohn Marino 					   VAR_DOMAIN, 0);
729*ef5ccd6cSJohn Marino       if (sym != NULL)
730*ef5ccd6cSJohn Marino 	break;
731*ef5ccd6cSJohn Marino 
732*ef5ccd6cSJohn Marino       /* Now search all static file-level symbols.  We have to do this for
733*ef5ccd6cSJohn Marino 	 things like typedefs in the class.  First search in this symtab,
734*ef5ccd6cSJohn Marino 	 what we want is possibly there.  */
735*ef5ccd6cSJohn Marino       len = strlen (base_name) + 2 + strlen (name) + 1;
736*ef5ccd6cSJohn Marino       concatenated_name = xrealloc (concatenated_name, len);
737*ef5ccd6cSJohn Marino       xsnprintf (concatenated_name, len, "%s::%s", base_name, name);
738*ef5ccd6cSJohn Marino       sym = lookup_symbol_static (concatenated_name, block, VAR_DOMAIN);
739*ef5ccd6cSJohn Marino       if (sym != NULL)
740*ef5ccd6cSJohn Marino 	break;
741*ef5ccd6cSJohn Marino 
742*ef5ccd6cSJohn Marino       /* Nope.  We now have to search all static blocks in all objfiles,
743*ef5ccd6cSJohn Marino 	 even if block != NULL, because there's no guarantees as to which
744*ef5ccd6cSJohn Marino 	 symtab the symbol we want is in.  */
745*ef5ccd6cSJohn Marino       sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
746*ef5ccd6cSJohn Marino       if (sym != NULL)
747*ef5ccd6cSJohn Marino 	break;
748*ef5ccd6cSJohn Marino 
749*ef5ccd6cSJohn Marino       /* If this class has base classes, search them next.  */
750*ef5ccd6cSJohn Marino       CHECK_TYPEDEF (base_type);
751*ef5ccd6cSJohn Marino       if (TYPE_N_BASECLASSES (base_type) > 0)
752*ef5ccd6cSJohn Marino 	{
753*ef5ccd6cSJohn Marino 	  sym = find_symbol_in_baseclass (base_type, name, block);
754*ef5ccd6cSJohn Marino 	  if (sym != NULL)
755*ef5ccd6cSJohn Marino 	    break;
756*ef5ccd6cSJohn Marino 	}
757*ef5ccd6cSJohn Marino     }
758*ef5ccd6cSJohn Marino 
759*ef5ccd6cSJohn Marino   do_cleanups (cleanup);
760*ef5ccd6cSJohn Marino   return sym;
761*ef5ccd6cSJohn Marino }
762*ef5ccd6cSJohn Marino 
763*ef5ccd6cSJohn Marino /* Look up a symbol named NESTED_NAME that is nested inside the C++
7645796c8dcSSimon Schubert    class or namespace given by PARENT_TYPE, from within the context
7655796c8dcSSimon Schubert    given by BLOCK.  Return NULL if there is no such nested type.  */
7665796c8dcSSimon Schubert 
767*ef5ccd6cSJohn Marino struct symbol *
cp_lookup_nested_symbol(struct type * parent_type,const char * nested_name,const struct block * block)768*ef5ccd6cSJohn Marino cp_lookup_nested_symbol (struct type *parent_type,
7695796c8dcSSimon Schubert 			 const char *nested_name,
7705796c8dcSSimon Schubert 			 const struct block *block)
7715796c8dcSSimon Schubert {
772a45ae5f8SJohn Marino   /* type_name_no_tag_required provides better error reporting using the
773a45ae5f8SJohn Marino      original type.  */
774a45ae5f8SJohn Marino   struct type *saved_parent_type = parent_type;
775a45ae5f8SJohn Marino 
776a45ae5f8SJohn Marino   CHECK_TYPEDEF (parent_type);
777a45ae5f8SJohn Marino 
7785796c8dcSSimon Schubert   switch (TYPE_CODE (parent_type))
7795796c8dcSSimon Schubert     {
7805796c8dcSSimon Schubert     case TYPE_CODE_STRUCT:
7815796c8dcSSimon Schubert     case TYPE_CODE_NAMESPACE:
782cf7f2e2dSJohn Marino     case TYPE_CODE_UNION:
7835796c8dcSSimon Schubert       {
7845796c8dcSSimon Schubert 	/* NOTE: carlton/2003-11-10: We don't treat C++ class members
7855796c8dcSSimon Schubert 	   of classes like, say, data or function members.  Instead,
7865796c8dcSSimon Schubert 	   they're just represented by symbols whose names are
7875796c8dcSSimon Schubert 	   qualified by the name of the surrounding class.  This is
7885796c8dcSSimon Schubert 	   just like members of namespaces; in particular,
7895796c8dcSSimon Schubert 	   lookup_symbol_namespace works when looking them up.  */
7905796c8dcSSimon Schubert 
791*ef5ccd6cSJohn Marino 	int size;
792a45ae5f8SJohn Marino 	const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
793c50c785cSJohn Marino 	struct symbol *sym
794c50c785cSJohn Marino 	  = cp_lookup_symbol_in_namespace (parent_name, nested_name,
795*ef5ccd6cSJohn Marino 					   block, VAR_DOMAIN, 0);
796cf7f2e2dSJohn Marino 	char *concatenated_name;
797cf7f2e2dSJohn Marino 
798*ef5ccd6cSJohn Marino 	if (sym != NULL)
799*ef5ccd6cSJohn Marino 	  return sym;
800cf7f2e2dSJohn Marino 
801*ef5ccd6cSJohn Marino 	/* Now search all static file-level symbols.  We have to do this
802*ef5ccd6cSJohn Marino 	   for things like typedefs in the class.  We do not try to
803c50c785cSJohn Marino 	   guess any imported namespace as even the fully specified
804*ef5ccd6cSJohn Marino 	   namespace search is already not C++ compliant and more
805c50c785cSJohn Marino 	   assumptions could make it too magic.  */
806cf7f2e2dSJohn Marino 
807*ef5ccd6cSJohn Marino 	size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
808*ef5ccd6cSJohn Marino 	concatenated_name = alloca (size);
809*ef5ccd6cSJohn Marino 	xsnprintf (concatenated_name, size, "%s::%s",
810c50c785cSJohn Marino 		 parent_name, nested_name);
811*ef5ccd6cSJohn Marino 	sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
812*ef5ccd6cSJohn Marino 	if (sym != NULL)
813*ef5ccd6cSJohn Marino 	  return sym;
814cf7f2e2dSJohn Marino 
815*ef5ccd6cSJohn Marino 	/* If no matching symbols were found, try searching any
816*ef5ccd6cSJohn Marino 	   base classes.  */
817*ef5ccd6cSJohn Marino 	return find_symbol_in_baseclass (parent_type, nested_name, block);
8185796c8dcSSimon Schubert       }
819*ef5ccd6cSJohn Marino 
820*ef5ccd6cSJohn Marino     case TYPE_CODE_FUNC:
821*ef5ccd6cSJohn Marino     case TYPE_CODE_METHOD:
822*ef5ccd6cSJohn Marino       return NULL;
823*ef5ccd6cSJohn Marino 
8245796c8dcSSimon Schubert     default:
8255796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__,
826*ef5ccd6cSJohn Marino 		      _("cp_lookup_nested_symbol called "
827c50c785cSJohn Marino 			"on a non-aggregate type."));
8285796c8dcSSimon Schubert     }
8295796c8dcSSimon Schubert }
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert /* The C++-version of lookup_transparent_type.  */
8325796c8dcSSimon Schubert 
8335796c8dcSSimon Schubert /* FIXME: carlton/2004-01-16: The problem that this is trying to
8345796c8dcSSimon Schubert    address is that, unfortunately, sometimes NAME is wrong: it may not
8355796c8dcSSimon Schubert    include the name of namespaces enclosing the type in question.
836c50c785cSJohn Marino    lookup_transparent_type gets called when the type in question
8375796c8dcSSimon Schubert    is a declaration, and we're trying to find its definition; but, for
8385796c8dcSSimon Schubert    declarations, our type name deduction mechanism doesn't work.
8395796c8dcSSimon Schubert    There's nothing we can do to fix this in general, I think, in the
8405796c8dcSSimon Schubert    absence of debug information about namespaces (I've filed PR
8415796c8dcSSimon Schubert    gdb/1511 about this); until such debug information becomes more
8425796c8dcSSimon Schubert    prevalent, one heuristic which sometimes looks is to search for the
8435796c8dcSSimon Schubert    definition in namespaces containing the current namespace.
8445796c8dcSSimon Schubert 
8455796c8dcSSimon Schubert    We should delete this functions once the appropriate debug
8465796c8dcSSimon Schubert    information becomes more widespread.  (GCC 3.4 will be the first
8475796c8dcSSimon Schubert    released version of GCC with such information.)  */
8485796c8dcSSimon Schubert 
8495796c8dcSSimon Schubert struct type *
cp_lookup_transparent_type(const char * name)8505796c8dcSSimon Schubert cp_lookup_transparent_type (const char *name)
8515796c8dcSSimon Schubert {
8525796c8dcSSimon Schubert   /* First, try the honest way of looking up the definition.  */
8535796c8dcSSimon Schubert   struct type *t = basic_lookup_transparent_type (name);
8545796c8dcSSimon Schubert   const char *scope;
8555796c8dcSSimon Schubert 
8565796c8dcSSimon Schubert   if (t != NULL)
8575796c8dcSSimon Schubert     return t;
8585796c8dcSSimon Schubert 
8595796c8dcSSimon Schubert   /* If that doesn't work and we're within a namespace, look there
8605796c8dcSSimon Schubert      instead.  */
8615796c8dcSSimon Schubert   scope = block_scope (get_selected_block (0));
8625796c8dcSSimon Schubert 
8635796c8dcSSimon Schubert   if (scope[0] == '\0')
8645796c8dcSSimon Schubert     return NULL;
8655796c8dcSSimon Schubert 
8665796c8dcSSimon Schubert   return cp_lookup_transparent_type_loop (name, scope, 0);
8675796c8dcSSimon Schubert }
8685796c8dcSSimon Schubert 
869c50c785cSJohn Marino /* Lookup the type definition associated to NAME in namespaces/classes
870c50c785cSJohn Marino    containing SCOPE whose name is strictly longer than LENGTH.  LENGTH
871c50c785cSJohn Marino    must be the index of the start of a component of SCOPE.  */
8725796c8dcSSimon Schubert 
8735796c8dcSSimon Schubert static struct type *
cp_lookup_transparent_type_loop(const char * name,const char * scope,int length)874c50c785cSJohn Marino cp_lookup_transparent_type_loop (const char *name,
875c50c785cSJohn Marino 				 const char *scope,
8765796c8dcSSimon Schubert 				 int length)
8775796c8dcSSimon Schubert {
8785796c8dcSSimon Schubert   int scope_length = length + cp_find_first_component (scope + length);
8795796c8dcSSimon Schubert   char *full_name;
8805796c8dcSSimon Schubert 
8815796c8dcSSimon Schubert   /* If the current scope is followed by "::", look in the next
8825796c8dcSSimon Schubert      component.  */
8835796c8dcSSimon Schubert   if (scope[scope_length] == ':')
8845796c8dcSSimon Schubert     {
8855796c8dcSSimon Schubert       struct type *retval
886c50c785cSJohn Marino 	= cp_lookup_transparent_type_loop (name, scope,
887c50c785cSJohn Marino 					   scope_length + 2);
888cf7f2e2dSJohn Marino 
8895796c8dcSSimon Schubert       if (retval != NULL)
8905796c8dcSSimon Schubert 	return retval;
8915796c8dcSSimon Schubert     }
8925796c8dcSSimon Schubert 
8935796c8dcSSimon Schubert   full_name = alloca (scope_length + 2 + strlen (name) + 1);
8945796c8dcSSimon Schubert   strncpy (full_name, scope, scope_length);
8955796c8dcSSimon Schubert   strncpy (full_name + scope_length, "::", 2);
8965796c8dcSSimon Schubert   strcpy (full_name + scope_length + 2, name);
8975796c8dcSSimon Schubert 
8985796c8dcSSimon Schubert   return basic_lookup_transparent_type (full_name);
8995796c8dcSSimon Schubert }
9005796c8dcSSimon Schubert 
901a45ae5f8SJohn Marino /* This used to do something but was removed when it became
902a45ae5f8SJohn Marino    obsolete.  */
9035796c8dcSSimon Schubert 
9045796c8dcSSimon Schubert static void
maintenance_cplus_namespace(char * args,int from_tty)9055796c8dcSSimon Schubert maintenance_cplus_namespace (char *args, int from_tty)
9065796c8dcSSimon Schubert {
907a45ae5f8SJohn Marino   printf_unfiltered (_("The `maint namespace' command was removed.\n"));
9085796c8dcSSimon Schubert }
9095796c8dcSSimon Schubert 
9105796c8dcSSimon Schubert /* Provide a prototype to silence -Wmissing-prototypes.  */
9115796c8dcSSimon Schubert extern initialize_file_ftype _initialize_cp_namespace;
9125796c8dcSSimon Schubert 
9135796c8dcSSimon Schubert void
_initialize_cp_namespace(void)9145796c8dcSSimon Schubert _initialize_cp_namespace (void)
9155796c8dcSSimon Schubert {
916a45ae5f8SJohn Marino   struct cmd_list_element *cmd;
917a45ae5f8SJohn Marino 
918a45ae5f8SJohn Marino   cmd = add_cmd ("namespace", class_maintenance,
919c50c785cSJohn Marino 		 maintenance_cplus_namespace,
920a45ae5f8SJohn Marino 		 _("Deprecated placeholder for removed functionality."),
9215796c8dcSSimon Schubert 		 &maint_cplus_cmd_list);
922a45ae5f8SJohn Marino   deprecate_cmd (cmd, NULL);
9235796c8dcSSimon Schubert }
924