1130803Smarcel/* Helper routines for C++ support in GDB.
2130803Smarcel   Copyright 2003, 2004 Free Software Foundation, Inc.
3130803Smarcel
4130803Smarcel   Contributed by David Carlton and by Kealia, Inc.
5130803Smarcel
6130803Smarcel   This file is part of GDB.
7130803Smarcel
8130803Smarcel   This program is free software; you can redistribute it and/or modify
9130803Smarcel   it under the terms of the GNU General Public License as published by
10130803Smarcel   the Free Software Foundation; either version 2 of the License, or
11130803Smarcel   (at your option) any later version.
12130803Smarcel
13130803Smarcel   This program is distributed in the hope that it will be useful,
14130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
15130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16130803Smarcel   GNU General Public License for more details.
17130803Smarcel
18130803Smarcel   You should have received a copy of the GNU General Public License
19130803Smarcel   along with this program; if not, write to the Free Software
20130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
21130803Smarcel   Boston, MA 02111-1307, USA.  */
22130803Smarcel
23130803Smarcel#include "defs.h"
24130803Smarcel#include "cp-support.h"
25130803Smarcel#include "gdb_obstack.h"
26130803Smarcel#include "symtab.h"
27130803Smarcel#include "symfile.h"
28130803Smarcel#include "gdb_assert.h"
29130803Smarcel#include "block.h"
30130803Smarcel#include "objfiles.h"
31130803Smarcel#include "gdbtypes.h"
32130803Smarcel#include "dictionary.h"
33130803Smarcel#include "command.h"
34130803Smarcel#include "frame.h"
35130803Smarcel
36130803Smarcel/* When set, the file that we're processing is known to have debugging
37130803Smarcel   info for C++ namespaces.  */
38130803Smarcel
39130803Smarcel/* NOTE: carlton/2004-01-13: No currently released version of GCC (the
40130803Smarcel   latest of which is 3.3.x at the time of this writing) produces this
41130803Smarcel   debug info.  GCC 3.4 should, however.  */
42130803Smarcel
43130803Smarcelunsigned char processing_has_namespace_info;
44130803Smarcel
45130803Smarcel/* This contains our best guess as to the name of the current
46130803Smarcel   enclosing namespace(s)/class(es), if any.  For example, if we're
47130803Smarcel   within the method foo() in the following code:
48130803Smarcel
49130803Smarcel    namespace N {
50130803Smarcel      class C {
51130803Smarcel	void foo () {
52130803Smarcel	}
53130803Smarcel      };
54130803Smarcel    }
55130803Smarcel
56130803Smarcel   then processing_current_prefix should be set to "N::C".  If
57130803Smarcel   processing_has_namespace_info is false, then this variable might
58130803Smarcel   not be reliable.  */
59130803Smarcel
60130803Smarcelconst char *processing_current_prefix;
61130803Smarcel
62130803Smarcel/* List of using directives that are active in the current file.  */
63130803Smarcel
64130803Smarcelstatic struct using_direct *using_list;
65130803Smarcel
66130803Smarcelstatic struct using_direct *cp_add_using (const char *name,
67130803Smarcel					  unsigned int inner_len,
68130803Smarcel					  unsigned int outer_len,
69130803Smarcel					  struct using_direct *next);
70130803Smarcel
71130803Smarcelstatic struct using_direct *cp_copy_usings (struct using_direct *using,
72130803Smarcel					    struct obstack *obstack);
73130803Smarcel
74130803Smarcelstatic struct symbol *lookup_namespace_scope (const char *name,
75130803Smarcel					      const char *linkage_name,
76130803Smarcel					      const struct block *block,
77130803Smarcel					      const domain_enum domain,
78130803Smarcel					      struct symtab **symtab,
79130803Smarcel					      const char *scope,
80130803Smarcel					      int scope_len);
81130803Smarcel
82130803Smarcelstatic struct symbol *lookup_symbol_file (const char *name,
83130803Smarcel					  const char *linkage_name,
84130803Smarcel					  const struct block *block,
85130803Smarcel					  const domain_enum domain,
86130803Smarcel					  struct symtab **symtab,
87130803Smarcel					  int anonymous_namespace);
88130803Smarcel
89130803Smarcelstatic struct type *cp_lookup_transparent_type_loop (const char *name,
90130803Smarcel						     const char *scope,
91130803Smarcel						     int scope_len);
92130803Smarcel
93130803Smarcelstatic void initialize_namespace_symtab (struct objfile *objfile);
94130803Smarcel
95130803Smarcelstatic struct block *get_possible_namespace_block (struct objfile *objfile);
96130803Smarcel
97130803Smarcelstatic void free_namespace_block (struct symtab *symtab);
98130803Smarcel
99130803Smarcelstatic int check_possible_namespace_symbols_loop (const char *name,
100130803Smarcel						  int len,
101130803Smarcel						  struct objfile *objfile);
102130803Smarcel
103130803Smarcelstatic int check_one_possible_namespace_symbol (const char *name,
104130803Smarcel						int len,
105130803Smarcel						struct objfile *objfile);
106130803Smarcel
107130803Smarcelstatic
108130803Smarcelstruct symbol *lookup_possible_namespace_symbol (const char *name,
109130803Smarcel						 struct symtab **symtab);
110130803Smarcel
111130803Smarcelstatic void maintenance_cplus_namespace (char *args, int from_tty);
112130803Smarcel
113130803Smarcel/* Set up support for dealing with C++ namespace info in the current
114130803Smarcel   symtab.  */
115130803Smarcel
116130803Smarcelvoid cp_initialize_namespace ()
117130803Smarcel{
118130803Smarcel  processing_has_namespace_info = 0;
119130803Smarcel  using_list = NULL;
120130803Smarcel}
121130803Smarcel
122130803Smarcel/* Add all the using directives we've gathered to the current symtab.
123130803Smarcel   STATIC_BLOCK should be the symtab's static block; OBSTACK is used
124130803Smarcel   for allocation.  */
125130803Smarcel
126130803Smarcelvoid
127130803Smarcelcp_finalize_namespace (struct block *static_block,
128130803Smarcel		       struct obstack *obstack)
129130803Smarcel{
130130803Smarcel  if (using_list != NULL)
131130803Smarcel    {
132130803Smarcel      block_set_using (static_block,
133130803Smarcel		       cp_copy_usings (using_list, obstack),
134130803Smarcel		       obstack);
135130803Smarcel      using_list = NULL;
136130803Smarcel    }
137130803Smarcel}
138130803Smarcel
139130803Smarcel/* Check to see if SYMBOL refers to an object contained within an
140130803Smarcel   anonymous namespace; if so, add an appropriate using directive.  */
141130803Smarcel
142130803Smarcel/* Optimize away strlen ("(anonymous namespace)").  */
143130803Smarcel
144130803Smarcel#define ANONYMOUS_NAMESPACE_LEN 21
145130803Smarcel
146130803Smarcelvoid
147130803Smarcelcp_scan_for_anonymous_namespaces (const struct symbol *symbol)
148130803Smarcel{
149130803Smarcel  if (!processing_has_namespace_info
150130803Smarcel      && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
151130803Smarcel    {
152130803Smarcel      const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
153130803Smarcel      unsigned int previous_component;
154130803Smarcel      unsigned int next_component;
155130803Smarcel      const char *len;
156130803Smarcel
157130803Smarcel      /* Start with a quick-and-dirty check for mention of "(anonymous
158130803Smarcel	 namespace)".  */
159130803Smarcel
160130803Smarcel      if (!cp_is_anonymous (name))
161130803Smarcel	return;
162130803Smarcel
163130803Smarcel      previous_component = 0;
164130803Smarcel      next_component = cp_find_first_component (name + previous_component);
165130803Smarcel
166130803Smarcel      while (name[next_component] == ':')
167130803Smarcel	{
168130803Smarcel	  if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
169130803Smarcel	      && strncmp (name + previous_component,
170130803Smarcel			  "(anonymous namespace)",
171130803Smarcel			  ANONYMOUS_NAMESPACE_LEN) == 0)
172130803Smarcel	    {
173130803Smarcel	      /* We've found a component of the name that's an
174130803Smarcel		 anonymous namespace.  So add symbols in it to the
175130803Smarcel		 namespace given by the previous component if there is
176130803Smarcel		 one, or to the global namespace if there isn't.  */
177130803Smarcel	      cp_add_using_directive (name,
178130803Smarcel				      previous_component == 0
179130803Smarcel				      ? 0 : previous_component - 2,
180130803Smarcel				      next_component);
181130803Smarcel	    }
182130803Smarcel	  /* The "+ 2" is for the "::".  */
183130803Smarcel	  previous_component = next_component + 2;
184130803Smarcel	  next_component = (previous_component
185130803Smarcel			    + cp_find_first_component (name
186130803Smarcel						       + previous_component));
187130803Smarcel	}
188130803Smarcel    }
189130803Smarcel}
190130803Smarcel
191130803Smarcel/* Add a using directive to using_list.  NAME is the start of a string
192130803Smarcel   that should contain the namespaces we want to add as initial
193130803Smarcel   substrings, OUTER_LENGTH is the end of the outer namespace, and
194130803Smarcel   INNER_LENGTH is the end of the inner namespace.  If the using
195130803Smarcel   directive in question has already been added, don't add it
196130803Smarcel   twice.  */
197130803Smarcel
198130803Smarcelvoid
199130803Smarcelcp_add_using_directive (const char *name, unsigned int outer_length,
200130803Smarcel			unsigned int inner_length)
201130803Smarcel{
202130803Smarcel  struct using_direct *current;
203130803Smarcel  struct using_direct *new;
204130803Smarcel
205130803Smarcel  /* Has it already been added?  */
206130803Smarcel
207130803Smarcel  for (current = using_list; current != NULL; current = current->next)
208130803Smarcel    {
209130803Smarcel      if ((strncmp (current->inner, name, inner_length) == 0)
210130803Smarcel	  && (strlen (current->inner) == inner_length)
211130803Smarcel	  && (strlen (current->outer) == outer_length))
212130803Smarcel	return;
213130803Smarcel    }
214130803Smarcel
215130803Smarcel  using_list = cp_add_using (name, inner_length, outer_length,
216130803Smarcel			     using_list);
217130803Smarcel}
218130803Smarcel
219130803Smarcel/* Record the namespace that the function defined by SYMBOL was
220130803Smarcel   defined in, if necessary.  BLOCK is the associated block; use
221130803Smarcel   OBSTACK for allocation.  */
222130803Smarcel
223130803Smarcelvoid
224130803Smarcelcp_set_block_scope (const struct symbol *symbol,
225130803Smarcel		    struct block *block,
226130803Smarcel		    struct obstack *obstack)
227130803Smarcel{
228130803Smarcel  /* Make sure that the name was originally mangled: if not, there
229130803Smarcel     certainly isn't any namespace information to worry about!  */
230130803Smarcel
231130803Smarcel  if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
232130803Smarcel    {
233130803Smarcel      if (processing_has_namespace_info)
234130803Smarcel	{
235130803Smarcel	  block_set_scope
236130803Smarcel	    (block, obsavestring (processing_current_prefix,
237130803Smarcel				  strlen (processing_current_prefix),
238130803Smarcel				  obstack),
239130803Smarcel	     obstack);
240130803Smarcel	}
241130803Smarcel      else
242130803Smarcel	{
243130803Smarcel	  /* Try to figure out the appropriate namespace from the
244130803Smarcel	     demangled name.  */
245130803Smarcel
246130803Smarcel	  /* FIXME: carlton/2003-04-15: If the function in question is
247130803Smarcel	     a method of a class, the name will actually include the
248130803Smarcel	     name of the class as well.  This should be harmless, but
249130803Smarcel	     is a little unfortunate.  */
250130803Smarcel
251130803Smarcel	  const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
252130803Smarcel	  unsigned int prefix_len = cp_entire_prefix_len (name);
253130803Smarcel
254130803Smarcel	  block_set_scope (block,
255130803Smarcel			   obsavestring (name, prefix_len, obstack),
256130803Smarcel			   obstack);
257130803Smarcel	}
258130803Smarcel    }
259130803Smarcel}
260130803Smarcel
261130803Smarcel/* Test whether or not NAMESPACE looks like it mentions an anonymous
262130803Smarcel   namespace; return nonzero if so.  */
263130803Smarcel
264130803Smarcelint
265130803Smarcelcp_is_anonymous (const char *namespace)
266130803Smarcel{
267130803Smarcel  return (strstr (namespace, "(anonymous namespace)")
268130803Smarcel	  != NULL);
269130803Smarcel}
270130803Smarcel
271130803Smarcel/* Create a new struct using direct whose inner namespace is the
272130803Smarcel   initial substring of NAME of leng INNER_LEN and whose outer
273130803Smarcel   namespace is the initial substring of NAME of length OUTER_LENGTH.
274130803Smarcel   Set its next member in the linked list to NEXT; allocate all memory
275130803Smarcel   using xmalloc.  It copies the strings, so NAME can be a temporary
276130803Smarcel   string.  */
277130803Smarcel
278130803Smarcelstatic struct using_direct *
279130803Smarcelcp_add_using (const char *name,
280130803Smarcel	      unsigned int inner_len,
281130803Smarcel	      unsigned int outer_len,
282130803Smarcel	      struct using_direct *next)
283130803Smarcel{
284130803Smarcel  struct using_direct *retval;
285130803Smarcel
286130803Smarcel  gdb_assert (outer_len < inner_len);
287130803Smarcel
288130803Smarcel  retval = xmalloc (sizeof (struct using_direct));
289130803Smarcel  retval->inner = savestring (name, inner_len);
290130803Smarcel  retval->outer = savestring (name, outer_len);
291130803Smarcel  retval->next = next;
292130803Smarcel
293130803Smarcel  return retval;
294130803Smarcel}
295130803Smarcel
296130803Smarcel/* Make a copy of the using directives in the list pointed to by
297130803Smarcel   USING, using OBSTACK to allocate memory.  Free all memory pointed
298130803Smarcel   to by USING via xfree.  */
299130803Smarcel
300130803Smarcelstatic struct using_direct *
301130803Smarcelcp_copy_usings (struct using_direct *using,
302130803Smarcel		struct obstack *obstack)
303130803Smarcel{
304130803Smarcel  if (using == NULL)
305130803Smarcel    {
306130803Smarcel      return NULL;
307130803Smarcel    }
308130803Smarcel  else
309130803Smarcel    {
310130803Smarcel      struct using_direct *retval
311130803Smarcel	= obstack_alloc (obstack, sizeof (struct using_direct));
312130803Smarcel      retval->inner = obsavestring (using->inner, strlen (using->inner),
313130803Smarcel				    obstack);
314130803Smarcel      retval->outer = obsavestring (using->outer, strlen (using->outer),
315130803Smarcel				    obstack);
316130803Smarcel      retval->next = cp_copy_usings (using->next, obstack);
317130803Smarcel
318130803Smarcel      xfree (using->inner);
319130803Smarcel      xfree (using->outer);
320130803Smarcel      xfree (using);
321130803Smarcel
322130803Smarcel      return retval;
323130803Smarcel    }
324130803Smarcel}
325130803Smarcel
326130803Smarcel/* The C++-specific version of name lookup for static and global
327130803Smarcel   names.  This makes sure that names get looked for in all namespaces
328130803Smarcel   that are in scope.  NAME is the natural name of the symbol that
329130803Smarcel   we're looking for, LINKAGE_NAME (which is optional) is its linkage
330130803Smarcel   name, BLOCK is the block that we're searching within, DOMAIN says
331130803Smarcel   what kind of symbols we're looking for, and if SYMTAB is non-NULL,
332130803Smarcel   we should store the symtab where we found the symbol in it.  */
333130803Smarcel
334130803Smarcelstruct symbol *
335130803Smarcelcp_lookup_symbol_nonlocal (const char *name,
336130803Smarcel			   const char *linkage_name,
337130803Smarcel			   const struct block *block,
338130803Smarcel			   const domain_enum domain,
339130803Smarcel			   struct symtab **symtab)
340130803Smarcel{
341130803Smarcel  return lookup_namespace_scope (name, linkage_name, block, domain,
342130803Smarcel				 symtab, block_scope (block), 0);
343130803Smarcel}
344130803Smarcel
345130803Smarcel/* Lookup NAME at namespace scope (or, in C terms, in static and
346130803Smarcel   global variables).  SCOPE is the namespace that the current
347130803Smarcel   function is defined within; only consider namespaces whose length
348130803Smarcel   is at least SCOPE_LEN.  Other arguments are as in
349130803Smarcel   cp_lookup_symbol_nonlocal.
350130803Smarcel
351130803Smarcel   For example, if we're within a function A::B::f and looking for a
352130803Smarcel   symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
353130803Smarcel   SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
354130803Smarcel   but with SCOPE_LEN = 1.  And then it calls itself with NAME and
355130803Smarcel   SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
356130803Smarcel   "A::B::x"; if it doesn't find it, then the second call looks for
357130803Smarcel   "A::x", and if that call fails, then the first call looks for
358130803Smarcel   "x".  */
359130803Smarcel
360130803Smarcelstatic struct symbol *
361130803Smarcellookup_namespace_scope (const char *name,
362130803Smarcel			const char *linkage_name,
363130803Smarcel			const struct block *block,
364130803Smarcel			const domain_enum domain,
365130803Smarcel			struct symtab **symtab,
366130803Smarcel			const char *scope,
367130803Smarcel			int scope_len)
368130803Smarcel{
369130803Smarcel  char *namespace;
370130803Smarcel
371130803Smarcel  if (scope[scope_len] != '\0')
372130803Smarcel    {
373130803Smarcel      /* Recursively search for names in child namespaces first.  */
374130803Smarcel
375130803Smarcel      struct symbol *sym;
376130803Smarcel      int new_scope_len = scope_len;
377130803Smarcel
378130803Smarcel      /* If the current scope is followed by "::", skip past that.  */
379130803Smarcel      if (new_scope_len != 0)
380130803Smarcel	{
381130803Smarcel	  gdb_assert (scope[new_scope_len] == ':');
382130803Smarcel	  new_scope_len += 2;
383130803Smarcel	}
384130803Smarcel      new_scope_len += cp_find_first_component (scope + new_scope_len);
385130803Smarcel      sym = lookup_namespace_scope (name, linkage_name, block,
386130803Smarcel				    domain, symtab,
387130803Smarcel				    scope, new_scope_len);
388130803Smarcel      if (sym != NULL)
389130803Smarcel	return sym;
390130803Smarcel    }
391130803Smarcel
392130803Smarcel  /* Okay, we didn't find a match in our children, so look for the
393130803Smarcel     name in the current namespace.  */
394130803Smarcel
395130803Smarcel  namespace = alloca (scope_len + 1);
396130803Smarcel  strncpy (namespace, scope, scope_len);
397130803Smarcel  namespace[scope_len] = '\0';
398130803Smarcel  return cp_lookup_symbol_namespace (namespace, name, linkage_name,
399130803Smarcel				     block, domain, symtab);
400130803Smarcel}
401130803Smarcel
402130803Smarcel/* Look up NAME in the C++ namespace NAMESPACE, applying the using
403130803Smarcel   directives that are active in BLOCK.  Other arguments are as in
404130803Smarcel   cp_lookup_symbol_nonlocal.  */
405130803Smarcel
406130803Smarcelstruct symbol *
407130803Smarcelcp_lookup_symbol_namespace (const char *namespace,
408130803Smarcel			    const char *name,
409130803Smarcel			    const char *linkage_name,
410130803Smarcel			    const struct block *block,
411130803Smarcel			    const domain_enum domain,
412130803Smarcel			    struct symtab **symtab)
413130803Smarcel{
414130803Smarcel  const struct using_direct *current;
415130803Smarcel  struct symbol *sym;
416130803Smarcel
417130803Smarcel  /* First, go through the using directives.  If any of them add new
418130803Smarcel     names to the namespace we're searching in, see if we can find a
419130803Smarcel     match by applying them.  */
420130803Smarcel
421130803Smarcel  for (current = block_using (block);
422130803Smarcel       current != NULL;
423130803Smarcel       current = current->next)
424130803Smarcel    {
425130803Smarcel      if (strcmp (namespace, current->outer) == 0)
426130803Smarcel	{
427130803Smarcel	  sym = cp_lookup_symbol_namespace (current->inner,
428130803Smarcel					    name,
429130803Smarcel					    linkage_name,
430130803Smarcel					    block,
431130803Smarcel					    domain,
432130803Smarcel					    symtab);
433130803Smarcel	  if (sym != NULL)
434130803Smarcel	    return sym;
435130803Smarcel	}
436130803Smarcel    }
437130803Smarcel
438130803Smarcel  /* We didn't find anything by applying any of the using directives
439130803Smarcel     that are still applicable; so let's see if we've got a match
440130803Smarcel     using the current namespace.  */
441130803Smarcel
442130803Smarcel  if (namespace[0] == '\0')
443130803Smarcel    {
444130803Smarcel      return lookup_symbol_file (name, linkage_name, block,
445130803Smarcel				 domain, symtab, 0);
446130803Smarcel    }
447130803Smarcel  else
448130803Smarcel    {
449130803Smarcel      char *concatenated_name
450130803Smarcel	= alloca (strlen (namespace) + 2 + strlen (name) + 1);
451130803Smarcel      strcpy (concatenated_name, namespace);
452130803Smarcel      strcat (concatenated_name, "::");
453130803Smarcel      strcat (concatenated_name, name);
454130803Smarcel      sym = lookup_symbol_file (concatenated_name, linkage_name,
455130803Smarcel				block, domain, symtab,
456130803Smarcel				cp_is_anonymous (namespace));
457130803Smarcel      return sym;
458130803Smarcel    }
459130803Smarcel}
460130803Smarcel
461130803Smarcel/* Look up NAME in BLOCK's static block and in global blocks.  If
462130803Smarcel   ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
463130803Smarcel   within an anonymous namespace.  Other arguments are as in
464130803Smarcel   cp_lookup_symbol_nonlocal.  */
465130803Smarcel
466130803Smarcelstatic struct symbol *
467130803Smarcellookup_symbol_file (const char *name,
468130803Smarcel		    const char *linkage_name,
469130803Smarcel		    const struct block *block,
470130803Smarcel		    const domain_enum domain,
471130803Smarcel		    struct symtab **symtab,
472130803Smarcel		    int anonymous_namespace)
473130803Smarcel{
474130803Smarcel  struct symbol *sym = NULL;
475130803Smarcel
476130803Smarcel  sym = lookup_symbol_static (name, linkage_name, block, domain, symtab);
477130803Smarcel  if (sym != NULL)
478130803Smarcel    return sym;
479130803Smarcel
480130803Smarcel  if (anonymous_namespace)
481130803Smarcel    {
482130803Smarcel      /* Symbols defined in anonymous namespaces have external linkage
483130803Smarcel	 but should be treated as local to a single file nonetheless.
484130803Smarcel	 So we only search the current file's global block.  */
485130803Smarcel
486130803Smarcel      const struct block *global_block = block_global_block (block);
487130803Smarcel
488130803Smarcel      if (global_block != NULL)
489130803Smarcel	sym = lookup_symbol_aux_block (name, linkage_name, global_block,
490130803Smarcel				       domain, symtab);
491130803Smarcel    }
492130803Smarcel  else
493130803Smarcel    {
494130803Smarcel      sym = lookup_symbol_global (name, linkage_name, domain, symtab);
495130803Smarcel    }
496130803Smarcel
497130803Smarcel  if (sym != NULL)
498130803Smarcel    return sym;
499130803Smarcel
500130803Smarcel  /* Now call "lookup_possible_namespace_symbol".  Symbols in here
501130803Smarcel     claim to be associated to namespaces, but this claim might be
502130803Smarcel     incorrect: the names in question might actually correspond to
503130803Smarcel     classes instead of namespaces.  But if they correspond to
504130803Smarcel     classes, then we should have found a match for them above.  So if
505130803Smarcel     we find them now, they should be genuine.  */
506130803Smarcel
507130803Smarcel  /* FIXME: carlton/2003-06-12: This is a hack and should eventually
508130803Smarcel     be deleted: see comments below.  */
509130803Smarcel
510130803Smarcel  if (domain == VAR_DOMAIN)
511130803Smarcel    {
512130803Smarcel      sym = lookup_possible_namespace_symbol (name, symtab);
513130803Smarcel      if (sym != NULL)
514130803Smarcel	return sym;
515130803Smarcel    }
516130803Smarcel
517130803Smarcel  return NULL;
518130803Smarcel}
519130803Smarcel
520130803Smarcel/* Look up a type named NESTED_NAME that is nested inside the C++
521130803Smarcel   class or namespace given by PARENT_TYPE, from within the context
522130803Smarcel   given by BLOCK.  Return NULL if there is no such nested type.  */
523130803Smarcel
524130803Smarcelstruct type *
525130803Smarcelcp_lookup_nested_type (struct type *parent_type,
526130803Smarcel		       const char *nested_name,
527130803Smarcel		       const struct block *block)
528130803Smarcel{
529130803Smarcel  switch (TYPE_CODE (parent_type))
530130803Smarcel    {
531130803Smarcel    case TYPE_CODE_STRUCT:
532130803Smarcel    case TYPE_CODE_NAMESPACE:
533130803Smarcel      {
534130803Smarcel	/* NOTE: carlton/2003-11-10: We don't treat C++ class members
535130803Smarcel	   of classes like, say, data or function members.  Instead,
536130803Smarcel	   they're just represented by symbols whose names are
537130803Smarcel	   qualified by the name of the surrounding class.  This is
538130803Smarcel	   just like members of namespaces; in particular,
539130803Smarcel	   lookup_symbol_namespace works when looking them up.  */
540130803Smarcel
541130803Smarcel	const char *parent_name = TYPE_TAG_NAME (parent_type);
542130803Smarcel	struct symbol *sym = cp_lookup_symbol_namespace (parent_name,
543130803Smarcel							 nested_name,
544130803Smarcel							 NULL,
545130803Smarcel							 block,
546130803Smarcel							 VAR_DOMAIN,
547130803Smarcel							 NULL);
548130803Smarcel	if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
549130803Smarcel	  return NULL;
550130803Smarcel	else
551130803Smarcel	  return SYMBOL_TYPE (sym);
552130803Smarcel      }
553130803Smarcel    default:
554130803Smarcel      internal_error (__FILE__, __LINE__,
555130803Smarcel		      "cp_lookup_nested_type called on a non-aggregate type.");
556130803Smarcel    }
557130803Smarcel}
558130803Smarcel
559130803Smarcel/* The C++-version of lookup_transparent_type.  */
560130803Smarcel
561130803Smarcel/* FIXME: carlton/2004-01-16: The problem that this is trying to
562130803Smarcel   address is that, unfortunately, sometimes NAME is wrong: it may not
563130803Smarcel   include the name of namespaces enclosing the type in question.
564130803Smarcel   lookup_transparent_type gets called when the the type in question
565130803Smarcel   is a declaration, and we're trying to find its definition; but, for
566130803Smarcel   declarations, our type name deduction mechanism doesn't work.
567130803Smarcel   There's nothing we can do to fix this in general, I think, in the
568130803Smarcel   absence of debug information about namespaces (I've filed PR
569130803Smarcel   gdb/1511 about this); until such debug information becomes more
570130803Smarcel   prevalent, one heuristic which sometimes looks is to search for the
571130803Smarcel   definition in namespaces containing the current namespace.
572130803Smarcel
573130803Smarcel   We should delete this functions once the appropriate debug
574130803Smarcel   information becomes more widespread.  (GCC 3.4 will be the first
575130803Smarcel   released version of GCC with such information.)  */
576130803Smarcel
577130803Smarcelstruct type *
578130803Smarcelcp_lookup_transparent_type (const char *name)
579130803Smarcel{
580130803Smarcel  /* First, try the honest way of looking up the definition.  */
581130803Smarcel  struct type *t = basic_lookup_transparent_type (name);
582130803Smarcel  const char *scope;
583130803Smarcel
584130803Smarcel  if (t != NULL)
585130803Smarcel    return t;
586130803Smarcel
587130803Smarcel  /* If that doesn't work and we're within a namespace, look there
588130803Smarcel     instead.  */
589130803Smarcel  scope = block_scope (get_selected_block (0));
590130803Smarcel
591130803Smarcel  if (scope[0] == '\0')
592130803Smarcel    return NULL;
593130803Smarcel
594130803Smarcel  return cp_lookup_transparent_type_loop (name, scope, 0);
595130803Smarcel}
596130803Smarcel
597130803Smarcel/* Lookup the the type definition associated to NAME in
598130803Smarcel   namespaces/classes containing SCOPE whose name is strictly longer
599130803Smarcel   than LENGTH.  LENGTH must be the index of the start of a
600130803Smarcel   component of SCOPE.  */
601130803Smarcel
602130803Smarcelstatic struct type *
603130803Smarcelcp_lookup_transparent_type_loop (const char *name, const char *scope,
604130803Smarcel				 int length)
605130803Smarcel{
606130803Smarcel  int scope_length = length + cp_find_first_component (scope + length);
607130803Smarcel  char *full_name;
608130803Smarcel
609130803Smarcel  /* If the current scope is followed by "::", look in the next
610130803Smarcel     component.  */
611130803Smarcel  if (scope[scope_length] == ':')
612130803Smarcel    {
613130803Smarcel      struct type *retval
614130803Smarcel	= cp_lookup_transparent_type_loop (name, scope, scope_length + 2);
615130803Smarcel      if (retval != NULL)
616130803Smarcel	return retval;
617130803Smarcel    }
618130803Smarcel
619130803Smarcel  full_name = alloca (scope_length + 2 + strlen (name) + 1);
620130803Smarcel  strncpy (full_name, scope, scope_length);
621130803Smarcel  strncpy (full_name + scope_length, "::", 2);
622130803Smarcel  strcpy (full_name + scope_length + 2, name);
623130803Smarcel
624130803Smarcel  return basic_lookup_transparent_type (full_name);
625130803Smarcel}
626130803Smarcel
627130803Smarcel/* Now come functions for dealing with symbols associated to
628130803Smarcel   namespaces.  (They're used to store the namespaces themselves, not
629130803Smarcel   objects that live in the namespaces.)  These symbols come in two
630130803Smarcel   varieties: if we run into a DW_TAG_namespace DIE, then we know that
631130803Smarcel   we have a namespace, so dwarf2read.c creates a symbol for it just
632130803Smarcel   like normal.  But, unfortunately, versions of GCC through at least
633130803Smarcel   3.3 don't generate those DIE's.  Our solution is to try to guess
634130803Smarcel   their existence by looking at demangled names.  This might cause us
635130803Smarcel   to misidentify classes as namespaces, however.  So we put those
636130803Smarcel   symbols in a special block (one per objfile), and we only search
637130803Smarcel   that block as a last resort.  */
638130803Smarcel
639130803Smarcel/* FIXME: carlton/2003-06-12: Once versions of GCC that generate
640130803Smarcel   DW_TAG_namespace have been out for a year or two, we should get rid
641130803Smarcel   of all of this "possible namespace" nonsense.  */
642130803Smarcel
643130803Smarcel/* Allocate everything necessary for the possible namespace block
644130803Smarcel   associated to OBJFILE.  */
645130803Smarcel
646130803Smarcelstatic void
647130803Smarcelinitialize_namespace_symtab (struct objfile *objfile)
648130803Smarcel{
649130803Smarcel  struct symtab *namespace_symtab;
650130803Smarcel  struct blockvector *bv;
651130803Smarcel  struct block *bl;
652130803Smarcel
653130803Smarcel  namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile);
654130803Smarcel  namespace_symtab->language = language_cplus;
655130803Smarcel  namespace_symtab->free_code = free_nothing;
656130803Smarcel  namespace_symtab->dirname = NULL;
657130803Smarcel
658130803Smarcel  bv = obstack_alloc (&objfile->objfile_obstack,
659130803Smarcel		      sizeof (struct blockvector)
660130803Smarcel		      + FIRST_LOCAL_BLOCK * sizeof (struct block *));
661130803Smarcel  BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1;
662130803Smarcel  BLOCKVECTOR (namespace_symtab) = bv;
663130803Smarcel
664130803Smarcel  /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */
665130803Smarcel
666130803Smarcel  bl = allocate_block (&objfile->objfile_obstack);
667130803Smarcel  BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
668130803Smarcel					NULL);
669130803Smarcel  BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
670130803Smarcel  bl = allocate_block (&objfile->objfile_obstack);
671130803Smarcel  BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
672130803Smarcel					NULL);
673130803Smarcel  BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
674130803Smarcel
675130803Smarcel  /* Allocate the possible namespace block; we put it where the first
676130803Smarcel     local block will live, though I don't think there's any need to
677130803Smarcel     pretend that it's actually a local block (e.g. by setting
678130803Smarcel     BLOCK_SUPERBLOCK appropriately).  We don't use the global or
679130803Smarcel     static block because we don't want it searched during the normal
680130803Smarcel     search of all global/static blocks in lookup_symbol: we only want
681130803Smarcel     it used as a last resort.  */
682130803Smarcel
683130803Smarcel  /* NOTE: carlton/2003-09-11: I considered not associating the fake
684130803Smarcel     symbols to a block/symtab at all.  But that would cause problems
685130803Smarcel     with lookup_symbol's SYMTAB argument and with block_found, so
686130803Smarcel     having a symtab/block for this purpose seems like the best
687130803Smarcel     solution for now.  */
688130803Smarcel
689130803Smarcel  bl = allocate_block (&objfile->objfile_obstack);
690130803Smarcel  BLOCK_DICT (bl) = dict_create_hashed_expandable ();
691130803Smarcel  BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl;
692130803Smarcel
693130803Smarcel  namespace_symtab->free_func = free_namespace_block;
694130803Smarcel
695130803Smarcel  objfile->cp_namespace_symtab = namespace_symtab;
696130803Smarcel}
697130803Smarcel
698130803Smarcel/* Locate the possible namespace block associated to OBJFILE,
699130803Smarcel   allocating it if necessary.  */
700130803Smarcel
701130803Smarcelstatic struct block *
702130803Smarcelget_possible_namespace_block (struct objfile *objfile)
703130803Smarcel{
704130803Smarcel  if (objfile->cp_namespace_symtab == NULL)
705130803Smarcel    initialize_namespace_symtab (objfile);
706130803Smarcel
707130803Smarcel  return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab),
708130803Smarcel			    FIRST_LOCAL_BLOCK);
709130803Smarcel}
710130803Smarcel
711130803Smarcel/* Free the dictionary associated to the possible namespace block.  */
712130803Smarcel
713130803Smarcelstatic void
714130803Smarcelfree_namespace_block (struct symtab *symtab)
715130803Smarcel{
716130803Smarcel  struct block *possible_namespace_block;
717130803Smarcel
718130803Smarcel  possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
719130803Smarcel						FIRST_LOCAL_BLOCK);
720130803Smarcel  gdb_assert (possible_namespace_block != NULL);
721130803Smarcel  dict_free (BLOCK_DICT (possible_namespace_block));
722130803Smarcel}
723130803Smarcel
724130803Smarcel/* Ensure that there are symbols in the possible namespace block
725130803Smarcel   associated to OBJFILE for all initial substrings of NAME that look
726130803Smarcel   like namespaces or classes.  NAME should end in a member variable:
727130803Smarcel   it shouldn't consist solely of namespaces.  */
728130803Smarcel
729130803Smarcelvoid
730130803Smarcelcp_check_possible_namespace_symbols (const char *name, struct objfile *objfile)
731130803Smarcel{
732130803Smarcel  check_possible_namespace_symbols_loop (name,
733130803Smarcel					 cp_find_first_component (name),
734130803Smarcel					 objfile);
735130803Smarcel}
736130803Smarcel
737130803Smarcel/* This is a helper loop for cp_check_possible_namespace_symbols; it
738130803Smarcel   ensures that there are symbols in the possible namespace block
739130803Smarcel   associated to OBJFILE for all namespaces that are initial
740130803Smarcel   substrings of NAME of length at least LEN.  It returns 1 if a
741130803Smarcel   previous loop had already created the shortest such symbol and 0
742130803Smarcel   otherwise.
743130803Smarcel
744130803Smarcel   This function assumes that if there is already a symbol associated
745130803Smarcel   to a substring of NAME of a given length, then there are already
746130803Smarcel   symbols associated to all substrings of NAME whose length is less
747130803Smarcel   than that length.  So if cp_check_possible_namespace_symbols has
748130803Smarcel   been called once with argument "A::B::C::member", then that will
749130803Smarcel   create symbols "A", "A::B", and "A::B::C".  If it is then later
750130803Smarcel   called with argument "A::B::D::member", then the new call will
751130803Smarcel   generate a new symbol for "A::B::D", but once it sees that "A::B"
752130803Smarcel   has already been created, it doesn't bother checking to see if "A"
753130803Smarcel   has also been created.  */
754130803Smarcel
755130803Smarcelstatic int
756130803Smarcelcheck_possible_namespace_symbols_loop (const char *name, int len,
757130803Smarcel				       struct objfile *objfile)
758130803Smarcel{
759130803Smarcel  if (name[len] == ':')
760130803Smarcel    {
761130803Smarcel      int done;
762130803Smarcel      int next_len = len + 2;
763130803Smarcel
764130803Smarcel      next_len += cp_find_first_component (name + next_len);
765130803Smarcel      done = check_possible_namespace_symbols_loop (name, next_len,
766130803Smarcel						    objfile);
767130803Smarcel
768130803Smarcel      if (!done)
769130803Smarcel	done = check_one_possible_namespace_symbol (name, len, objfile);
770130803Smarcel
771130803Smarcel      return done;
772130803Smarcel    }
773130803Smarcel  else
774130803Smarcel    return 0;
775130803Smarcel}
776130803Smarcel
777130803Smarcel/* Check to see if there's already a possible namespace symbol in
778130803Smarcel   OBJFILE whose name is the initial substring of NAME of length LEN.
779130803Smarcel   If not, create one and return 0; otherwise, return 1.  */
780130803Smarcel
781130803Smarcelstatic int
782130803Smarcelcheck_one_possible_namespace_symbol (const char *name, int len,
783130803Smarcel				     struct objfile *objfile)
784130803Smarcel{
785130803Smarcel  struct block *block = get_possible_namespace_block (objfile);
786130803Smarcel  char *name_copy = alloca (len + 1);
787130803Smarcel  struct symbol *sym;
788130803Smarcel
789130803Smarcel  memcpy (name_copy, name, len);
790130803Smarcel  name_copy[len] = '\0';
791130803Smarcel  sym = lookup_block_symbol (block, name_copy, NULL, VAR_DOMAIN);
792130803Smarcel
793130803Smarcel  if (sym == NULL)
794130803Smarcel    {
795130803Smarcel      struct type *type;
796130803Smarcel      name_copy = obsavestring (name, len, &objfile->objfile_obstack);
797130803Smarcel
798130803Smarcel      type = init_type (TYPE_CODE_NAMESPACE, 0, 0, name_copy, objfile);
799130803Smarcel
800130803Smarcel      TYPE_TAG_NAME (type) = TYPE_NAME (type);
801130803Smarcel
802130803Smarcel      sym = obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol));
803130803Smarcel      memset (sym, 0, sizeof (struct symbol));
804130803Smarcel      SYMBOL_LANGUAGE (sym) = language_cplus;
805130803Smarcel      SYMBOL_SET_NAMES (sym, name_copy, len, objfile);
806130803Smarcel      SYMBOL_CLASS (sym) = LOC_TYPEDEF;
807130803Smarcel      SYMBOL_TYPE (sym) = type;
808130803Smarcel      SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
809130803Smarcel
810130803Smarcel      dict_add_symbol (BLOCK_DICT (block), sym);
811130803Smarcel
812130803Smarcel      return 0;
813130803Smarcel    }
814130803Smarcel  else
815130803Smarcel    return 1;
816130803Smarcel}
817130803Smarcel
818130803Smarcel/* Look for a symbol named NAME in all the possible namespace blocks.
819130803Smarcel   If one is found, return it; if SYMTAB is non-NULL, set *SYMTAB to
820130803Smarcel   equal the symtab where it was found.  */
821130803Smarcel
822130803Smarcelstatic struct symbol *
823130803Smarcellookup_possible_namespace_symbol (const char *name, struct symtab **symtab)
824130803Smarcel{
825130803Smarcel  struct objfile *objfile;
826130803Smarcel
827130803Smarcel  ALL_OBJFILES (objfile)
828130803Smarcel    {
829130803Smarcel      struct symbol *sym;
830130803Smarcel
831130803Smarcel      sym = lookup_block_symbol (get_possible_namespace_block (objfile),
832130803Smarcel				 name, NULL, VAR_DOMAIN);
833130803Smarcel
834130803Smarcel      if (sym != NULL)
835130803Smarcel	{
836130803Smarcel	  if (symtab != NULL)
837130803Smarcel	    *symtab = objfile->cp_namespace_symtab;
838130803Smarcel
839130803Smarcel	  return sym;
840130803Smarcel	}
841130803Smarcel    }
842130803Smarcel
843130803Smarcel  return NULL;
844130803Smarcel}
845130803Smarcel
846130803Smarcel/* Print out all the possible namespace symbols.  */
847130803Smarcel
848130803Smarcelstatic void
849130803Smarcelmaintenance_cplus_namespace (char *args, int from_tty)
850130803Smarcel{
851130803Smarcel  struct objfile *objfile;
852130803Smarcel  printf_unfiltered ("Possible namespaces:\n");
853130803Smarcel  ALL_OBJFILES (objfile)
854130803Smarcel    {
855130803Smarcel      struct dict_iterator iter;
856130803Smarcel      struct symbol *sym;
857130803Smarcel
858130803Smarcel      ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym)
859130803Smarcel	{
860130803Smarcel	  printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym));
861130803Smarcel	}
862130803Smarcel    }
863130803Smarcel}
864130803Smarcel
865130803Smarcelvoid
866130803Smarcel_initialize_cp_namespace (void)
867130803Smarcel{
868130803Smarcel  add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace,
869130803Smarcel	   "Print the list of possible C++ namespaces.",
870130803Smarcel	   &maint_cplus_cmd_list);
871130803Smarcel}
872