1 /* Helper routines for C++ support in GDB.
2 Copyright 2003 Free Software Foundation, Inc.
4 Contributed by David Carlton and by Kealia, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 #include "cp-support.h"
25 #include "gdb_obstack.h"
28 #include "gdb_assert.h"
31 /* When set, the file that we're processing seems to have debugging
32 info for C++ namespaces, so cp-namespace.c shouldn't try to guess
33 namespace info itself. */
35 unsigned char processing_has_namespace_info;
37 /* If processing_has_namespace_info is nonzero, this string should
38 contain the name of the current namespace. The string is
39 temporary; copy it if you need it. */
41 /* FIXME: carlton/2003-06-12: This isn't entirely reliable: currently,
42 we get mislead by DW_AT_specification. */
44 const char *processing_current_namespace;
46 /* List of using directives that are active in the current file. */
48 static struct using_direct *using_list;
50 static struct using_direct *cp_add_using (const char *name,
51 unsigned int inner_len,
52 unsigned int outer_len,
53 struct using_direct *next);
55 static struct using_direct *cp_copy_usings (struct using_direct *using,
56 struct obstack *obstack);
58 static struct symbol *lookup_namespace_scope (const char *name,
59 const char *linkage_name,
60 const struct block *block,
61 const domain_enum domain,
62 struct symtab **symtab,
66 static struct symbol *lookup_symbol_file (const char *name,
67 const char *linkage_name,
68 const struct block *block,
69 const domain_enum domain,
70 struct symtab **symtab,
71 int anonymous_namespace);
73 /* Set up support for dealing with C++ namespace info in the current
76 void cp_initialize_namespace ()
78 processing_has_namespace_info = 0;
82 /* Add all the using directives we've gathered to the current symtab.
83 STATIC_BLOCK should be the symtab's static block; OBSTACK is used
87 cp_finalize_namespace (struct block *static_block,
88 struct obstack *obstack)
90 if (using_list != NULL)
92 block_set_using (static_block,
93 cp_copy_usings (using_list, obstack),
99 /* Check to see if SYMBOL refers to an object contained within an
100 anonymous namespace; if so, add an appropriate using directive. */
102 /* Optimize away strlen ("(anonymous namespace)"). */
104 #define ANONYMOUS_NAMESPACE_LEN 21
107 cp_scan_for_anonymous_namespaces (const struct symbol *symbol)
109 if (!processing_has_namespace_info
110 && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
112 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
113 unsigned int previous_component;
114 unsigned int next_component;
117 /* Start with a quick-and-dirty check for mention of "(anonymous
120 if (!cp_is_anonymous (name))
123 previous_component = 0;
124 next_component = cp_find_first_component (name + previous_component);
126 while (name[next_component] == ':')
128 if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
129 && strncmp (name + previous_component,
130 "(anonymous namespace)",
131 ANONYMOUS_NAMESPACE_LEN) == 0)
133 /* We've found a component of the name that's an
134 anonymous namespace. So add symbols in it to the
135 namespace given by the previous component if there is
136 one, or to the global namespace if there isn't. */
137 cp_add_using_directive (name,
138 previous_component == 0
139 ? 0 : previous_component - 2,
142 /* The "+ 2" is for the "::". */
143 previous_component = next_component + 2;
144 next_component = (previous_component
145 + cp_find_first_component (name
146 + previous_component));
151 /* Add a using directive to using_list. NAME is the start of a string
152 that should contain the namespaces we want to add as initial
153 substrings, OUTER_LENGTH is the end of the outer namespace, and
154 INNER_LENGTH is the end of the inner namespace. If the using
155 directive in question has already been added, don't add it
159 cp_add_using_directive (const char *name, unsigned int outer_length,
160 unsigned int inner_length)
162 struct using_direct *current;
163 struct using_direct *new;
165 /* Has it already been added? */
167 for (current = using_list; current != NULL; current = current->next)
169 if ((strncmp (current->inner, name, inner_length) == 0)
170 && (strlen (current->inner) == inner_length)
171 && (strlen (current->outer) == outer_length))
175 using_list = cp_add_using (name, inner_length, outer_length,
179 /* Record the namespace that the function defined by SYMBOL was
180 defined in, if necessary. BLOCK is the associated block; use
181 OBSTACK for allocation. */
184 cp_set_block_scope (const struct symbol *symbol,
186 struct obstack *obstack)
188 /* Make sure that the name was originally mangled: if not, there
189 certainly isn't any namespace information to worry about! */
191 if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
194 /* FIXME: carlton/2003-06-12: As mentioned above,
195 'processing_has_namespace_info' currently isn't entirely
196 reliable, so let's always use demangled names to get this
197 information for now. */
199 if (processing_has_namespace_info)
202 (block, obsavestring (processing_current_namespace,
203 strlen (processing_current_namespace),
210 /* Try to figure out the appropriate namespace from the
213 /* FIXME: carlton/2003-04-15: If the function in question is
214 a method of a class, the name will actually include the
215 name of the class as well. This should be harmless, but
216 is a little unfortunate. */
218 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
219 unsigned int prefix_len = cp_entire_prefix_len (name);
221 block_set_scope (block,
222 obsavestring (name, prefix_len, obstack),
228 /* Test whether or not NAMESPACE looks like it mentions an anonymous
229 namespace; return nonzero if so. */
232 cp_is_anonymous (const char *namespace)
234 return (strstr (namespace, "(anonymous namespace)")
238 /* Create a new struct using direct whose inner namespace is the
239 initial substring of NAME of leng INNER_LEN and whose outer
240 namespace is the initial substring of NAME of length OUTER_LENGTH.
241 Set its next member in the linked list to NEXT; allocate all memory
242 using xmalloc. It copies the strings, so NAME can be a temporary
245 static struct using_direct *
246 cp_add_using (const char *name,
247 unsigned int inner_len,
248 unsigned int outer_len,
249 struct using_direct *next)
251 struct using_direct *retval;
253 gdb_assert (outer_len < inner_len);
255 retval = xmalloc (sizeof (struct using_direct));
256 retval->inner = savestring (name, inner_len);
257 retval->outer = savestring (name, outer_len);
263 /* Make a copy of the using directives in the list pointed to by
264 USING, using OBSTACK to allocate memory. Free all memory pointed
265 to by USING via xfree. */
267 static struct using_direct *
268 cp_copy_usings (struct using_direct *using,
269 struct obstack *obstack)
277 struct using_direct *retval
278 = obstack_alloc (obstack, sizeof (struct using_direct));
279 retval->inner = obsavestring (using->inner, strlen (using->inner),
281 retval->outer = obsavestring (using->outer, strlen (using->outer),
283 retval->next = cp_copy_usings (using->next, obstack);
285 xfree (using->inner);
286 xfree (using->outer);
293 /* The C++-specific version of name lookup for static and global
294 names. This makes sure that names get looked for in all namespaces
295 that are in scope. NAME is the natural name of the symbol that
296 we're looking for, LINKAGE_NAME (which is optional) is its linkage
297 name, BLOCK is the block that we're searching within, DOMAIN says
298 what kind of symbols we're looking for, and if SYMTAB is non-NULL,
299 we should store the symtab where we found the symbol in it. */
302 cp_lookup_symbol_nonlocal (const char *name,
303 const char *linkage_name,
304 const struct block *block,
305 const domain_enum domain,
306 struct symtab **symtab)
308 return lookup_namespace_scope (name, linkage_name, block, domain,
309 symtab, block_scope (block), 0);
312 /* Lookup NAME at namespace scope (or, in C terms, in static and
313 global variables). SCOPE is the namespace that the current
314 function is defined within; only consider namespaces whose length
315 is at least SCOPE_LEN. Other arguments are as in
316 cp_lookup_symbol_nonlocal.
318 For example, if we're within a function A::B::f and looking for a
319 symbol f, this will get called with NAME = "f", SCOPE = "A::B", and
320 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
321 but with SCOPE_LEN = 1. And then it calls itself with NAME and
322 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
323 "A::B::x"; if it doesn't find it, then the second call looks for
324 "A::x", and if that call fails, then the first call looks for
327 static struct symbol *
328 lookup_namespace_scope (const char *name,
329 const char *linkage_name,
330 const struct block *block,
331 const domain_enum domain,
332 struct symtab **symtab,
338 if (scope[scope_len] != '\0')
340 /* Recursively search for names in child namespaces first. */
343 int new_scope_len = scope_len;
345 /* If the current scope is followed by "::", skip past that. */
346 if (new_scope_len != 0)
348 gdb_assert (scope[new_scope_len] == ':');
351 new_scope_len += cp_find_first_component (scope + new_scope_len);
352 sym = lookup_namespace_scope (name, linkage_name, block,
354 scope, new_scope_len);
359 /* Okay, we didn't find a match in our children, so look for the
360 name in the current namespace. */
362 namespace = alloca (scope_len + 1);
363 strncpy (namespace, scope, scope_len);
364 namespace[scope_len] = '\0';
365 return cp_lookup_symbol_namespace (namespace, name, linkage_name,
366 block, domain, symtab);
369 /* Look up NAME in the C++ namespace NAMESPACE, applying the using
370 directives that are active in BLOCK. Other arguments are as in
371 cp_lookup_symbol_nonlocal. */
374 cp_lookup_symbol_namespace (const char *namespace,
376 const char *linkage_name,
377 const struct block *block,
378 const domain_enum domain,
379 struct symtab **symtab)
381 const struct using_direct *current;
384 /* First, go through the using directives. If any of them add new
385 names to the namespace we're searching in, see if we can find a
386 match by applying them. */
388 for (current = block_using (block);
390 current = current->next)
392 if (strcmp (namespace, current->outer) == 0)
394 sym = cp_lookup_symbol_namespace (current->inner,
405 /* We didn't find anything by applying any of the using directives
406 that are still applicable; so let's see if we've got a match
407 using the current namespace. */
409 if (namespace[0] == '\0')
411 return lookup_symbol_file (name, linkage_name, block,
416 char *concatenated_name
417 = alloca (strlen (namespace) + 2 + strlen (name) + 1);
418 strcpy (concatenated_name, namespace);
419 strcat (concatenated_name, "::");
420 strcat (concatenated_name, name);
421 sym = lookup_symbol_file (concatenated_name, linkage_name,
422 block, domain, symtab,
423 cp_is_anonymous (namespace));
428 /* Look up NAME in BLOCK's static block and in global blocks. If
429 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
430 within an anonymous namespace. Other arguments are as in
431 cp_lookup_symbol_nonlocal. */
433 static struct symbol *
434 lookup_symbol_file (const char *name,
435 const char *linkage_name,
436 const struct block *block,
437 const domain_enum domain,
438 struct symtab **symtab,
439 int anonymous_namespace)
441 struct symbol *sym = NULL;
443 sym = lookup_symbol_static (name, linkage_name, block, domain, symtab);
447 if (anonymous_namespace)
449 /* Symbols defined in anonymous namespaces have external linkage
450 but should be treated as local to a single file nonetheless.
451 So we only search the current file's global block. */
453 const struct block *global_block = block_global_block (block);
455 if (global_block != NULL)
456 return lookup_symbol_aux_block (name, linkage_name, global_block,
463 return lookup_symbol_global (name, linkage_name, domain, symtab);