xref: /dragonfly/contrib/gdb-7/gdb/cp-namespace.c (revision 783d47c4)
1 /* Helper routines for C++ support in GDB.
2    Copyright (C) 2003, 2004, 2007, 2008, 2009, 2010, 2011
3    Free Software Foundation, Inc.
4 
5    Contributed by David Carlton and by Kealia, Inc.
6 
7    This file is part of GDB.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21 
22 #include "defs.h"
23 #include "cp-support.h"
24 #include "gdb_obstack.h"
25 #include "symtab.h"
26 #include "symfile.h"
27 #include "gdb_assert.h"
28 #include "block.h"
29 #include "objfiles.h"
30 #include "gdbtypes.h"
31 #include "dictionary.h"
32 #include "command.h"
33 #include "frame.h"
34 #include "buildsym.h"
35 #include "language.h"
36 
37 static struct symbol *lookup_namespace_scope (const char *name,
38 					      const struct block *block,
39 					      const domain_enum domain,
40 					      const char *scope,
41 					      int scope_len);
42 
43 static struct symbol *lookup_symbol_file (const char *name,
44 					  const struct block *block,
45 					  const domain_enum domain,
46 					  int anonymous_namespace);
47 
48 static struct type *cp_lookup_transparent_type_loop (const char *name,
49 						     const char *scope,
50 						     int scope_len);
51 
52 static void initialize_namespace_symtab (struct objfile *objfile);
53 
54 static struct block *get_possible_namespace_block (struct objfile *objfile);
55 
56 static void free_namespace_block (struct symtab *symtab);
57 
58 static int check_possible_namespace_symbols_loop (const char *name,
59 						  int len,
60 						  struct objfile *objfile);
61 
62 static int check_one_possible_namespace_symbol (const char *name,
63 						int len,
64 						struct objfile *objfile);
65 
66 static struct symbol *lookup_possible_namespace_symbol (const char *name);
67 
68 static void maintenance_cplus_namespace (char *args, int from_tty);
69 
70 /* Check to see if SYMBOL refers to an object contained within an
71    anonymous namespace; if so, add an appropriate using directive.  */
72 
73 void
74 cp_scan_for_anonymous_namespaces (const struct symbol *symbol)
75 {
76   if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
77     {
78       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
79       unsigned int previous_component;
80       unsigned int next_component;
81 
82       /* Start with a quick-and-dirty check for mention of "(anonymous
83 	 namespace)".  */
84 
85       if (!cp_is_anonymous (name))
86 	return;
87 
88       previous_component = 0;
89       next_component = cp_find_first_component (name + previous_component);
90 
91       while (name[next_component] == ':')
92 	{
93 	  if (((next_component - previous_component)
94 	       == CP_ANONYMOUS_NAMESPACE_LEN)
95 	      && strncmp (name + previous_component,
96 			  CP_ANONYMOUS_NAMESPACE_STR,
97 			  CP_ANONYMOUS_NAMESPACE_LEN) == 0)
98 	    {
99 	      int dest_len = (previous_component == 0
100 			      ? 0 : previous_component - 2);
101 	      int src_len = next_component;
102 
103 	      char *dest = alloca (dest_len + 1);
104 	      char *src = alloca (src_len + 1);
105 
106 	      memcpy (dest, name, dest_len);
107 	      memcpy (src, name, src_len);
108 
109 	      dest[dest_len] = '\0';
110 	      src[src_len] = '\0';
111 
112 	      /* We've found a component of the name that's an
113 		 anonymous namespace.  So add symbols in it to the
114 		 namespace given by the previous component if there is
115 		 one, or to the global namespace if there isn't.  */
116 	      cp_add_using_directive (dest, src, NULL, NULL,
117 	                              &SYMBOL_SYMTAB (symbol)->objfile->objfile_obstack);
118 	    }
119 	  /* The "+ 2" is for the "::".  */
120 	  previous_component = next_component + 2;
121 	  next_component = (previous_component
122 			    + cp_find_first_component (name
123 						       + previous_component));
124 	}
125     }
126 }
127 
128 
129 /* Add a using directive to using_directives.  If the using directive
130    in question has already been added, don't add it twice.
131 
132    Create a new struct using_direct which imports the namespace SRC
133    into the scope DEST.  ALIAS is the name of the imported namespace
134    in the current scope.  If ALIAS is NULL then the namespace is known
135    by its original name.  DECLARATION is the name if the imported
136    varable if this is a declaration import (Eg. using A::x), otherwise
137    it is NULL.  The arguments are copied into newly allocated memory
138    so they can be temporaries.  */
139 
140 void
141 cp_add_using_directive (const char *dest,
142 			const char *src,
143 			const char *alias,
144 			const char *declaration,
145                         struct obstack *obstack)
146 {
147   struct using_direct *current;
148   struct using_direct *new;
149 
150   /* Has it already been added?  */
151 
152   for (current = using_directives; current != NULL; current = current->next)
153     {
154       if (strcmp (current->import_src, src) == 0
155           && strcmp (current->import_dest, dest) == 0
156           && ((alias == NULL && current->alias == NULL)
157               || (alias != NULL && current->alias != NULL
158         	  && strcmp (alias, current->alias) == 0))
159 	  && ((declaration == NULL && current->declaration == NULL)
160 	      || (declaration != NULL && current->declaration != NULL
161 		  && strcmp (declaration, current->declaration) == 0)))
162 	return;
163     }
164 
165   new = OBSTACK_ZALLOC (obstack, struct using_direct);
166 
167   new->import_src = obsavestring (src, strlen (src), obstack);
168   new->import_dest = obsavestring (dest, strlen (dest), obstack);
169 
170   if (alias != NULL)
171     new->alias = obsavestring (alias, strlen (alias), obstack);
172 
173   if (declaration != NULL)
174     new->declaration = obsavestring (declaration, strlen (declaration),
175                                      obstack);
176 
177   new->next = using_directives;
178   using_directives = new;
179 }
180 
181 /* Record the namespace that the function defined by SYMBOL was
182    defined in, if necessary.  BLOCK is the associated block; use
183    OBSTACK for allocation.  */
184 
185 void
186 cp_set_block_scope (const struct symbol *symbol,
187 		    struct block *block,
188 		    struct obstack *obstack,
189 		    const char *processing_current_prefix,
190 		    int processing_has_namespace_info)
191 {
192   if (processing_has_namespace_info)
193     {
194       block_set_scope
195 	(block, obsavestring (processing_current_prefix,
196 			      strlen (processing_current_prefix),
197 			      obstack),
198 	 obstack);
199     }
200   else if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
201     {
202       /* Try to figure out the appropriate namespace from the
203 	 demangled name.  */
204 
205       /* FIXME: carlton/2003-04-15: If the function in question is
206 	 a method of a class, the name will actually include the
207 	 name of the class as well.  This should be harmless, but
208 	 is a little unfortunate.  */
209 
210       const char *name = SYMBOL_DEMANGLED_NAME (symbol);
211       unsigned int prefix_len = cp_entire_prefix_len (name);
212 
213       block_set_scope (block,
214 		       obsavestring (name, prefix_len, obstack),
215 		       obstack);
216     }
217 }
218 
219 /* Test whether or not NAMESPACE looks like it mentions an anonymous
220    namespace; return nonzero if so.  */
221 
222 int
223 cp_is_anonymous (const char *namespace)
224 {
225   return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR)
226 	  != NULL);
227 }
228 
229 /* The C++-specific version of name lookup for static and global
230    names.  This makes sure that names get looked for in all namespaces
231    that are in scope.  NAME is the natural name of the symbol that
232    we're looking for, BLOCK is the block that we're searching within,
233    DOMAIN says what kind of symbols we're looking for, and if SYMTAB
234    is non-NULL, we should store the symtab where we found the symbol
235    in it.  */
236 
237 struct symbol *
238 cp_lookup_symbol_nonlocal (const char *name,
239 			   const struct block *block,
240 			   const domain_enum domain)
241 {
242   struct symbol *sym;
243   const char *scope = block_scope (block);
244 
245   sym = lookup_namespace_scope (name, block,
246 				domain, scope, 0);
247   if (sym != NULL)
248     return sym;
249 
250   return cp_lookup_symbol_namespace (scope, name,
251 				     block, domain);
252 }
253 
254 /* Look up NAME in the C++ namespace NAMESPACE.  Other arguments are
255    as in cp_lookup_symbol_nonlocal.  */
256 
257 static struct symbol *
258 cp_lookup_symbol_in_namespace (const char *namespace,
259                                const char *name,
260                                const struct block *block,
261                                const domain_enum domain)
262 {
263   if (namespace[0] == '\0')
264     {
265       return lookup_symbol_file (name, block, domain, 0);
266     }
267   else
268     {
269       char *concatenated_name = alloca (strlen (namespace) + 2
270 					+ strlen (name) + 1);
271 
272       strcpy (concatenated_name, namespace);
273       strcat (concatenated_name, "::");
274       strcat (concatenated_name, name);
275       return lookup_symbol_file (concatenated_name, block, domain,
276 				 cp_is_anonymous (namespace));
277     }
278 }
279 
280 /* Used for cleanups to reset the "searched" flag incase
281    of an error.  */
282 
283 static void
284 reset_directive_searched (void *data)
285 {
286   struct using_direct *direct = data;
287   direct->searched = 0;
288 }
289 
290 /* Search for NAME by applying all import statements belonging to
291    BLOCK which are applicable in SCOPE.  If DECLARATION_ONLY the
292    search is restricted to using declarations.
293    Example:
294 
295      namespace A {
296        int x;
297      }
298      using A::x;
299 
300    If SEARCH_PARENTS the search will include imports which are
301    applicable in parents of SCOPE.
302    Example:
303 
304      namespace A {
305        using namespace X;
306        namespace B {
307          using namespace Y;
308        }
309      }
310 
311    If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
312    namespaces X and Y will be considered.  If SEARCH_PARENTS is false
313    only the import of Y is considered.  */
314 
315 struct symbol *
316 cp_lookup_symbol_imports (const char *scope,
317                           const char *name,
318                           const struct block *block,
319                           const domain_enum domain,
320                           const int declaration_only,
321                           const int search_parents)
322 {
323   struct using_direct *current;
324   struct symbol *sym = NULL;
325   int len;
326   int directive_match;
327   struct cleanup *searched_cleanup;
328 
329   /* First, try to find the symbol in the given namespace.  */
330   if (!declaration_only)
331     sym = cp_lookup_symbol_in_namespace (scope, name,
332 					 block, domain);
333 
334   if (sym != NULL)
335     return sym;
336 
337   /* Go through the using directives.  If any of them add new names to
338      the namespace we're searching in, see if we can find a match by
339      applying them.  */
340 
341   for (current = block_using (block);
342        current != NULL;
343        current = current->next)
344     {
345       len = strlen (current->import_dest);
346       directive_match = (search_parents
347                          ? (strncmp (scope, current->import_dest,
348                                      strlen (current->import_dest)) == 0
349                             && (len == 0
350                                 || scope[len] == ':'
351 				|| scope[len] == '\0'))
352                          : strcmp (scope, current->import_dest) == 0);
353 
354       /* If the import destination is the current scope or one of its
355          ancestors then it is applicable.  */
356       if (directive_match && !current->searched)
357 	{
358 	/* Mark this import as searched so that the recursive call
359            does not search it again.  */
360 	current->searched = 1;
361 	searched_cleanup = make_cleanup (reset_directive_searched,
362 					 current);
363 
364 	/* If there is an import of a single declaration, compare the
365 	   imported declaration (after optional renaming by its alias)
366 	   with the sought out name.  If there is a match pass
367 	   current->import_src as NAMESPACE to direct the search
368 	   towards the imported namespace.  */
369 	if (current->declaration
370 	    && strcmp (name, current->alias
371 		       ? current->alias : current->declaration) == 0)
372 	  sym = cp_lookup_symbol_in_namespace (current->import_src,
373 					       current->declaration,
374 					       block, domain);
375 
376 	/* If this is a DECLARATION_ONLY search or a symbol was found
377 	   or this import statement was an import declaration, the
378 	   search of this import is complete.  */
379         if (declaration_only || sym != NULL || current->declaration)
380           {
381             current->searched = 0;
382             discard_cleanups (searched_cleanup);
383 
384             if (sym != NULL)
385               return sym;
386 
387             continue;
388           }
389 
390 	if (current->alias != NULL
391 	    && strcmp (name, current->alias) == 0)
392 	  /* If the import is creating an alias and the alias matches
393 	     the sought name.  Pass current->import_src as the NAME to
394 	     direct the search towards the aliased namespace.  */
395 	  {
396 	    sym = cp_lookup_symbol_in_namespace (scope,
397 						 current->import_src,
398 						 block, domain);
399 	  }
400 	else if (current->alias == NULL)
401 	  {
402 	    /* If this import statement creates no alias, pass
403                current->inner as NAMESPACE to direct the search
404                towards the imported namespace.  */
405 	    sym = cp_lookup_symbol_imports (current->import_src,
406 					    name, block,
407 					    domain, 0, 0);
408 	  }
409 	current->searched = 0;
410 	discard_cleanups (searched_cleanup);
411 
412 	if (sym != NULL)
413 	  return sym;
414 	}
415     }
416 
417   return NULL;
418 }
419 
420 /* Helper function that searches an array of symbols for one named
421    NAME.  */
422 
423 static struct symbol *
424 search_symbol_list (const char *name, int num,
425 		    struct symbol **syms)
426 {
427   int i;
428 
429   /* Maybe we should store a dictionary in here instead.  */
430   for (i = 0; i < num; ++i)
431     {
432       if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
433 	return syms[i];
434     }
435   return NULL;
436 }
437 
438 /* Like cp_lookup_symbol_imports, but if BLOCK is a function, it
439    searches through the template parameters of the function and the
440    function's type.  */
441 
442 struct symbol *
443 cp_lookup_symbol_imports_or_template (const char *scope,
444 				      const char *name,
445 				      const struct block *block,
446 				      const domain_enum domain)
447 {
448   struct symbol *function = BLOCK_FUNCTION (block);
449 
450   if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
451     {
452       int i;
453       struct cplus_specific *cps
454 	= function->ginfo.language_specific.cplus_specific;
455 
456       /* Search the function's template parameters.  */
457       if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
458 	{
459 	  struct template_symbol *templ
460 	    = (struct template_symbol *) function;
461 	  struct symbol *result;
462 
463 	  result = search_symbol_list (name,
464 				       templ->n_template_arguments,
465 				       templ->template_arguments);
466 	  if (result != NULL)
467 	    return result;
468 	}
469 
470       /* Search the template parameters of the function's defining
471 	 context.  */
472       if (SYMBOL_NATURAL_NAME (function))
473 	{
474 	  struct type *context;
475 	  char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
476 	  struct cleanup *cleanups = make_cleanup (xfree, name_copy);
477 	  const struct language_defn *lang = language_def (language_cplus);
478 	  struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch;
479 	  const struct block *parent = BLOCK_SUPERBLOCK (block);
480 
481 	  while (1)
482 	    {
483 	      struct symbol *result;
484 	      unsigned int prefix_len = cp_entire_prefix_len (name_copy);
485 
486 	      if (prefix_len == 0)
487 		context = NULL;
488 	      else
489 		{
490 		  name_copy[prefix_len] = '\0';
491 		  context = lookup_typename (lang, arch,
492 					     name_copy,
493 					     parent, 1);
494 		}
495 
496 	      if (context == NULL)
497 		break;
498 
499 	      result
500 		= search_symbol_list (name,
501 				      TYPE_N_TEMPLATE_ARGUMENTS (context),
502 				      TYPE_TEMPLATE_ARGUMENTS (context));
503 	      if (result != NULL)
504 		return result;
505 	    }
506 
507 	  do_cleanups (cleanups);
508 	}
509     }
510 
511   return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1);
512 }
513 
514  /* Searches for NAME in the current namespace, and by applying
515     relevant import statements belonging to BLOCK and its parents.
516     SCOPE is the namespace scope of the context in which the search is
517     being evaluated.  */
518 
519 struct symbol*
520 cp_lookup_symbol_namespace (const char *scope,
521                             const char *name,
522                             const struct block *block,
523                             const domain_enum domain)
524 {
525   struct symbol *sym;
526 
527   /* First, try to find the symbol in the given namespace.  */
528   sym = cp_lookup_symbol_in_namespace (scope, name,
529 				       block, domain);
530   if (sym != NULL)
531     return sym;
532 
533   /* Search for name in namespaces imported to this and parent
534      blocks.  */
535   while (block != NULL)
536     {
537       sym = cp_lookup_symbol_imports (scope, name, block,
538 				      domain, 0, 1);
539 
540       if (sym)
541 	return sym;
542 
543       block = BLOCK_SUPERBLOCK (block);
544     }
545 
546   return NULL;
547 }
548 
549 /* Lookup NAME at namespace scope (or, in C terms, in static and
550    global variables).  SCOPE is the namespace that the current
551    function is defined within; only consider namespaces whose length
552    is at least SCOPE_LEN.  Other arguments are as in
553    cp_lookup_symbol_nonlocal.
554 
555    For example, if we're within a function A::B::f and looking for a
556    symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
557    SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
558    but with SCOPE_LEN = 1.  And then it calls itself with NAME and
559    SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
560    "A::B::x"; if it doesn't find it, then the second call looks for
561    "A::x", and if that call fails, then the first call looks for
562    "x".  */
563 
564 static struct symbol *
565 lookup_namespace_scope (const char *name,
566 			const struct block *block,
567 			const domain_enum domain,
568 			const char *scope,
569 			int scope_len)
570 {
571   char *namespace;
572 
573   if (scope[scope_len] != '\0')
574     {
575       /* Recursively search for names in child namespaces first.  */
576 
577       struct symbol *sym;
578       int new_scope_len = scope_len;
579 
580       /* If the current scope is followed by "::", skip past that.  */
581       if (new_scope_len != 0)
582 	{
583 	  gdb_assert (scope[new_scope_len] == ':');
584 	  new_scope_len += 2;
585 	}
586       new_scope_len += cp_find_first_component (scope + new_scope_len);
587       sym = lookup_namespace_scope (name, block, domain,
588 				    scope, new_scope_len);
589       if (sym != NULL)
590 	return sym;
591     }
592 
593   /* Okay, we didn't find a match in our children, so look for the
594      name in the current namespace.  */
595 
596   namespace = alloca (scope_len + 1);
597   strncpy (namespace, scope, scope_len);
598   namespace[scope_len] = '\0';
599   return cp_lookup_symbol_in_namespace (namespace, name,
600 					block, domain);
601 }
602 
603 /* Look up NAME in BLOCK's static block and in global blocks.  If
604    ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
605    within an anonymous namespace.  Other arguments are as in
606    cp_lookup_symbol_nonlocal.  */
607 
608 static struct symbol *
609 lookup_symbol_file (const char *name,
610 		    const struct block *block,
611 		    const domain_enum domain,
612 		    int anonymous_namespace)
613 {
614   struct symbol *sym = NULL;
615 
616   sym = lookup_symbol_static (name, block, domain);
617   if (sym != NULL)
618     return sym;
619 
620   if (anonymous_namespace)
621     {
622       /* Symbols defined in anonymous namespaces have external linkage
623 	 but should be treated as local to a single file nonetheless.
624 	 So we only search the current file's global block.  */
625 
626       const struct block *global_block = block_global_block (block);
627 
628       if (global_block != NULL)
629 	sym = lookup_symbol_aux_block (name, global_block, domain);
630     }
631   else
632     {
633       sym = lookup_symbol_global (name, block, domain);
634     }
635 
636   if (sym != NULL)
637     return sym;
638 
639   /* Now call "lookup_possible_namespace_symbol".  Symbols in here
640      claim to be associated to namespaces, but this claim might be
641      incorrect: the names in question might actually correspond to
642      classes instead of namespaces.  But if they correspond to
643      classes, then we should have found a match for them above.  So if
644      we find them now, they should be genuine.  */
645 
646   /* FIXME: carlton/2003-06-12: This is a hack and should eventually
647      be deleted: see comments below.  */
648 
649   if (domain == VAR_DOMAIN)
650     {
651       sym = lookup_possible_namespace_symbol (name);
652       if (sym != NULL)
653 	return sym;
654     }
655 
656   return NULL;
657 }
658 
659 /* Look up a type named NESTED_NAME that is nested inside the C++
660    class or namespace given by PARENT_TYPE, from within the context
661    given by BLOCK.  Return NULL if there is no such nested type.  */
662 
663 struct type *
664 cp_lookup_nested_type (struct type *parent_type,
665 		       const char *nested_name,
666 		       const struct block *block)
667 {
668   switch (TYPE_CODE (parent_type))
669     {
670     case TYPE_CODE_STRUCT:
671     case TYPE_CODE_NAMESPACE:
672     case TYPE_CODE_UNION:
673       {
674 	/* NOTE: carlton/2003-11-10: We don't treat C++ class members
675 	   of classes like, say, data or function members.  Instead,
676 	   they're just represented by symbols whose names are
677 	   qualified by the name of the surrounding class.  This is
678 	   just like members of namespaces; in particular,
679 	   lookup_symbol_namespace works when looking them up.  */
680 
681 	const char *parent_name = TYPE_TAG_NAME (parent_type);
682 	struct symbol *sym
683 	  = cp_lookup_symbol_in_namespace (parent_name, nested_name,
684 					   block, VAR_DOMAIN);
685 	char *concatenated_name;
686 
687 	if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
688 	  return SYMBOL_TYPE (sym);
689 
690 	/* Now search all static file-level symbols.  Not strictly
691 	   correct, but more useful than an error.  We do not try to
692 	   guess any imported namespace as even the fully specified
693 	   namespace seach is is already not C++ compliant and more
694 	   assumptions could make it too magic.  */
695 
696 	concatenated_name = alloca (strlen (parent_name) + 2
697 				    + strlen (nested_name) + 1);
698 	sprintf (concatenated_name, "%s::%s",
699 		 parent_name, nested_name);
700 	sym = lookup_static_symbol_aux (concatenated_name,
701 					VAR_DOMAIN);
702 	if (sym != NULL && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
703 	  return SYMBOL_TYPE (sym);
704 
705 	return NULL;
706       }
707     default:
708       internal_error (__FILE__, __LINE__,
709 		      _("cp_lookup_nested_type called "
710 			"on a non-aggregate type."));
711     }
712 }
713 
714 /* The C++-version of lookup_transparent_type.  */
715 
716 /* FIXME: carlton/2004-01-16: The problem that this is trying to
717    address is that, unfortunately, sometimes NAME is wrong: it may not
718    include the name of namespaces enclosing the type in question.
719    lookup_transparent_type gets called when the type in question
720    is a declaration, and we're trying to find its definition; but, for
721    declarations, our type name deduction mechanism doesn't work.
722    There's nothing we can do to fix this in general, I think, in the
723    absence of debug information about namespaces (I've filed PR
724    gdb/1511 about this); until such debug information becomes more
725    prevalent, one heuristic which sometimes looks is to search for the
726    definition in namespaces containing the current namespace.
727 
728    We should delete this functions once the appropriate debug
729    information becomes more widespread.  (GCC 3.4 will be the first
730    released version of GCC with such information.)  */
731 
732 struct type *
733 cp_lookup_transparent_type (const char *name)
734 {
735   /* First, try the honest way of looking up the definition.  */
736   struct type *t = basic_lookup_transparent_type (name);
737   const char *scope;
738 
739   if (t != NULL)
740     return t;
741 
742   /* If that doesn't work and we're within a namespace, look there
743      instead.  */
744   scope = block_scope (get_selected_block (0));
745 
746   if (scope[0] == '\0')
747     return NULL;
748 
749   return cp_lookup_transparent_type_loop (name, scope, 0);
750 }
751 
752 /* Lookup the type definition associated to NAME in namespaces/classes
753    containing SCOPE whose name is strictly longer than LENGTH.  LENGTH
754    must be the index of the start of a component of SCOPE.  */
755 
756 static struct type *
757 cp_lookup_transparent_type_loop (const char *name,
758 				 const char *scope,
759 				 int length)
760 {
761   int scope_length = length + cp_find_first_component (scope + length);
762   char *full_name;
763 
764   /* If the current scope is followed by "::", look in the next
765      component.  */
766   if (scope[scope_length] == ':')
767     {
768       struct type *retval
769 	= cp_lookup_transparent_type_loop (name, scope,
770 					   scope_length + 2);
771 
772       if (retval != NULL)
773 	return retval;
774     }
775 
776   full_name = alloca (scope_length + 2 + strlen (name) + 1);
777   strncpy (full_name, scope, scope_length);
778   strncpy (full_name + scope_length, "::", 2);
779   strcpy (full_name + scope_length + 2, name);
780 
781   return basic_lookup_transparent_type (full_name);
782 }
783 
784 /* Now come functions for dealing with symbols associated to
785    namespaces.  (They're used to store the namespaces themselves, not
786    objects that live in the namespaces.)  These symbols come in two
787    varieties: if we run into a DW_TAG_namespace DIE, then we know that
788    we have a namespace, so dwarf2read.c creates a symbol for it just
789    like normal.  But, unfortunately, versions of GCC through at least
790    3.3 don't generate those DIE's.  Our solution is to try to guess
791    their existence by looking at demangled names.  This might cause us
792    to misidentify classes as namespaces, however.  So we put those
793    symbols in a special block (one per objfile), and we only search
794    that block as a last resort.  */
795 
796 /* FIXME: carlton/2003-06-12: Once versions of GCC that generate
797    DW_TAG_namespace have been out for a year or two, we should get rid
798    of all of this "possible namespace" nonsense.  */
799 
800 /* Allocate everything necessary for the possible namespace block
801    associated to OBJFILE.  */
802 
803 static void
804 initialize_namespace_symtab (struct objfile *objfile)
805 {
806   struct symtab *namespace_symtab;
807   struct blockvector *bv;
808   struct block *bl;
809 
810   namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile);
811   namespace_symtab->language = language_cplus;
812   namespace_symtab->free_code = free_nothing;
813   namespace_symtab->dirname = NULL;
814 
815   bv = obstack_alloc (&objfile->objfile_obstack,
816 		      sizeof (struct blockvector)
817 		      + FIRST_LOCAL_BLOCK * sizeof (struct block *));
818   BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1;
819   BLOCKVECTOR (namespace_symtab) = bv;
820 
821   /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK.  */
822 
823   bl = allocate_block (&objfile->objfile_obstack);
824   BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
825 					NULL);
826   BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
827   bl = allocate_block (&objfile->objfile_obstack);
828   BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
829 					NULL);
830   BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
831 
832   /* Allocate the possible namespace block; we put it where the first
833      local block will live, though I don't think there's any need to
834      pretend that it's actually a local block (e.g. by setting
835      BLOCK_SUPERBLOCK appropriately).  We don't use the global or
836      static block because we don't want it searched during the normal
837      search of all global/static blocks in lookup_symbol: we only want
838      it used as a last resort.  */
839 
840   /* NOTE: carlton/2003-09-11: I considered not associating the fake
841      symbols to a block/symtab at all.  But that would cause problems
842      with lookup_symbol's SYMTAB argument and with block_found, so
843      having a symtab/block for this purpose seems like the best
844      solution for now.  */
845 
846   bl = allocate_block (&objfile->objfile_obstack);
847   BLOCK_DICT (bl) = dict_create_hashed_expandable ();
848   BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl;
849 
850   namespace_symtab->free_func = free_namespace_block;
851 
852   objfile->cp_namespace_symtab = namespace_symtab;
853 }
854 
855 /* Locate the possible namespace block associated to OBJFILE,
856    allocating it if necessary.  */
857 
858 static struct block *
859 get_possible_namespace_block (struct objfile *objfile)
860 {
861   if (objfile->cp_namespace_symtab == NULL)
862     initialize_namespace_symtab (objfile);
863 
864   return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab),
865 			    FIRST_LOCAL_BLOCK);
866 }
867 
868 /* Free the dictionary associated to the possible namespace block.  */
869 
870 static void
871 free_namespace_block (struct symtab *symtab)
872 {
873   struct block *possible_namespace_block;
874 
875   possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
876 						FIRST_LOCAL_BLOCK);
877   gdb_assert (possible_namespace_block != NULL);
878   dict_free (BLOCK_DICT (possible_namespace_block));
879 }
880 
881 /* Ensure that there are symbols in the possible namespace block
882    associated to OBJFILE for all initial substrings of NAME that look
883    like namespaces or classes.  NAME should end in a member variable:
884    it shouldn't consist solely of namespaces.  */
885 
886 void
887 cp_check_possible_namespace_symbols (const char *name,
888 				     struct objfile *objfile)
889 {
890   check_possible_namespace_symbols_loop (name,
891 					 cp_find_first_component (name),
892 					 objfile);
893 }
894 
895 /* This is a helper loop for cp_check_possible_namespace_symbols; it
896    ensures that there are symbols in the possible namespace block
897    associated to OBJFILE for all namespaces that are initial
898    substrings of NAME of length at least LEN.  It returns 1 if a
899    previous loop had already created the shortest such symbol and 0
900    otherwise.
901 
902    This function assumes that if there is already a symbol associated
903    to a substring of NAME of a given length, then there are already
904    symbols associated to all substrings of NAME whose length is less
905    than that length.  So if cp_check_possible_namespace_symbols has
906    been called once with argument "A::B::C::member", then that will
907    create symbols "A", "A::B", and "A::B::C".  If it is then later
908    called with argument "A::B::D::member", then the new call will
909    generate a new symbol for "A::B::D", but once it sees that "A::B"
910    has already been created, it doesn't bother checking to see if "A"
911    has also been created.  */
912 
913 static int
914 check_possible_namespace_symbols_loop (const char *name, int len,
915 				       struct objfile *objfile)
916 {
917   if (name[len] == ':')
918     {
919       int done;
920       int next_len = len + 2;
921 
922       next_len += cp_find_first_component (name + next_len);
923       done = check_possible_namespace_symbols_loop (name, next_len,
924 						    objfile);
925 
926       if (!done)
927 	done = check_one_possible_namespace_symbol (name, len,
928 						    objfile);
929 
930       return done;
931     }
932   else
933     return 0;
934 }
935 
936 /* Check to see if there's already a possible namespace symbol in
937    OBJFILE whose name is the initial substring of NAME of length LEN.
938    If not, create one and return 0; otherwise, return 1.  */
939 
940 static int
941 check_one_possible_namespace_symbol (const char *name, int len,
942 				     struct objfile *objfile)
943 {
944   struct block *block = get_possible_namespace_block (objfile);
945   char *name_copy = alloca (len + 1);
946   struct symbol *sym;
947 
948   memcpy (name_copy, name, len);
949   name_copy[len] = '\0';
950   sym = lookup_block_symbol (block, name_copy, VAR_DOMAIN);
951 
952   if (sym == NULL)
953     {
954       struct type *type;
955 
956       type = init_type (TYPE_CODE_NAMESPACE, 0, 0,
957 			name_copy, objfile);
958 
959       TYPE_TAG_NAME (type) = TYPE_NAME (type);
960 
961       sym = obstack_alloc (&objfile->objfile_obstack,
962 			   sizeof (struct symbol));
963       memset (sym, 0, sizeof (struct symbol));
964       SYMBOL_SET_LANGUAGE (sym, language_cplus);
965       /* Note that init_type copied the name to the objfile's
966 	 obstack.  */
967       SYMBOL_SET_NAMES (sym, TYPE_NAME (type), len, 0, objfile);
968       SYMBOL_CLASS (sym) = LOC_TYPEDEF;
969       SYMBOL_TYPE (sym) = type;
970       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
971 
972       dict_add_symbol (BLOCK_DICT (block), sym);
973 
974       return 0;
975     }
976   else
977     return 1;
978 }
979 
980 /* Look for a symbol named NAME in all the possible namespace blocks.
981    If one is found, return it.  */
982 
983 static struct symbol *
984 lookup_possible_namespace_symbol (const char *name)
985 {
986   struct objfile *objfile;
987 
988   ALL_OBJFILES (objfile)
989     {
990       struct symbol *sym;
991 
992       sym = lookup_block_symbol (get_possible_namespace_block (objfile),
993 				 name, VAR_DOMAIN);
994 
995       if (sym != NULL)
996 	return sym;
997     }
998 
999   return NULL;
1000 }
1001 
1002 /* Print out all the possible namespace symbols.  */
1003 
1004 static void
1005 maintenance_cplus_namespace (char *args, int from_tty)
1006 {
1007   struct objfile *objfile;
1008 
1009   printf_unfiltered (_("Possible namespaces:\n"));
1010   ALL_OBJFILES (objfile)
1011     {
1012       struct dict_iterator iter;
1013       struct symbol *sym;
1014 
1015       ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile),
1016 			 iter, sym)
1017 	{
1018 	  printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym));
1019 	}
1020     }
1021 }
1022 
1023 /* Provide a prototype to silence -Wmissing-prototypes.  */
1024 extern initialize_file_ftype _initialize_cp_namespace;
1025 
1026 void
1027 _initialize_cp_namespace (void)
1028 {
1029   add_cmd ("namespace", class_maintenance,
1030 	   maintenance_cplus_namespace,
1031 	   _("Print the list of possible C++ namespaces."),
1032 	   &maint_cplus_cmd_list);
1033 }
1034