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 const char *processing_current_namespace;
43 /* List of using directives that are active in the current file. */
45 static struct using_direct *using_list;
47 static struct using_direct *cp_add_using (const char *name,
48 unsigned int inner_len,
49 unsigned int outer_len,
50 struct using_direct *next);
52 static struct using_direct *cp_copy_usings (struct using_direct *using,
53 struct obstack *obstack);
55 static struct symbol *lookup_namespace_scope (const char *name,
56 const char *linkage_name,
57 const struct block *block,
58 const domain_enum domain,
59 struct symtab **symtab,
63 static struct symbol *lookup_symbol_file (const char *name,
64 const char *linkage_name,
65 const struct block *block,
66 const domain_enum domain,
67 struct symtab **symtab,
68 int anonymous_namespace);
70 /* Set up support for dealing with C++ namespace info in the current
73 void cp_initialize_namespace ()
75 processing_has_namespace_info = 0;
79 /* Add all the using directives we've gathered to the current symtab.
80 STATIC_BLOCK should be the symtab's static block; OBSTACK is used
84 cp_finalize_namespace (struct block *static_block,
85 struct obstack *obstack)
87 if (using_list != NULL)
89 block_set_using (static_block,
90 cp_copy_usings (using_list, obstack),
96 /* Check to see if SYMBOL refers to an object contained within an
97 anonymous namespace; if so, add an appropriate using directive. */
99 /* Optimize away strlen ("(anonymous namespace)"). */
101 #define ANONYMOUS_NAMESPACE_LEN 21
104 cp_scan_for_anonymous_namespaces (const struct symbol *symbol)
106 if (!processing_has_namespace_info
107 && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
109 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
110 unsigned int previous_component;
111 unsigned int next_component;
114 /* Start with a quick-and-dirty check for mention of "(anonymous
117 if (!cp_is_anonymous (name))
120 previous_component = 0;
121 next_component = cp_find_first_component (name + previous_component);
123 while (name[next_component] == ':')
125 if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
126 && strncmp (name + previous_component,
127 "(anonymous namespace)",
128 ANONYMOUS_NAMESPACE_LEN) == 0)
130 /* We've found a component of the name that's an
131 anonymous namespace. So add symbols in it to the
132 namespace given by the previous component if there is
133 one, or to the global namespace if there isn't. */
134 cp_add_using_directive (name,
135 previous_component == 0
136 ? 0 : previous_component - 2,
139 /* The "+ 2" is for the "::". */
140 previous_component = next_component + 2;
141 next_component = (previous_component
142 + cp_find_first_component (name
143 + previous_component));
148 /* Add a using directive to using_list. NAME is the start of a string
149 that should contain the namespaces we want to add as initial
150 substrings, OUTER_LENGTH is the end of the outer namespace, and
151 INNER_LENGTH is the end of the inner namespace. If the using
152 directive in question has already been added, don't add it
156 cp_add_using_directive (const char *name, unsigned int outer_length,
157 unsigned int inner_length)
159 struct using_direct *current;
160 struct using_direct *new;
162 /* Has it already been added? */
164 for (current = using_list; current != NULL; current = current->next)
166 if ((strncmp (current->inner, name, inner_length) == 0)
167 && (strlen (current->inner) == inner_length)
168 && (strlen (current->outer) == outer_length))
172 using_list = cp_add_using (name, inner_length, outer_length,
176 /* Record the namespace that the function defined by SYMBOL was
177 defined in, if necessary. BLOCK is the associated block; use
178 OBSTACK for allocation. */
181 cp_set_block_scope (const struct symbol *symbol,
183 struct obstack *obstack)
185 /* Make sure that the name was originally mangled: if not, there
186 certainly isn't any namespace information to worry about! */
188 if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
190 if (processing_has_namespace_info)
193 (block, obsavestring (processing_current_namespace,
194 strlen (processing_current_namespace),
200 /* Try to figure out the appropriate namespace from the
203 /* FIXME: carlton/2003-04-15: If the function in question is
204 a method of a class, the name will actually include the
205 name of the class as well. This should be harmless, but
206 is a little unfortunate. */
208 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
209 unsigned int prefix_len = cp_entire_prefix_len (name);
211 block_set_scope (block,
212 obsavestring (name, prefix_len, obstack),
218 /* Test whether or not NAMESPACE looks like it mentions an anonymous
219 namespace; return nonzero if so. */
222 cp_is_anonymous (const char *namespace)
224 return (strstr (namespace, "(anonymous namespace)")
228 /* Create a new struct using direct whose inner namespace is the
229 initial substring of NAME of leng INNER_LEN and whose outer
230 namespace is the initial substring of NAME of length OUTER_LENGTH.
231 Set its next member in the linked list to NEXT; allocate all memory
232 using xmalloc. It copies the strings, so NAME can be a temporary
235 static struct using_direct *
236 cp_add_using (const char *name,
237 unsigned int inner_len,
238 unsigned int outer_len,
239 struct using_direct *next)
241 struct using_direct *retval;
243 gdb_assert (outer_len < inner_len);
245 retval = xmalloc (sizeof (struct using_direct));
246 retval->inner = savestring (name, inner_len);
247 retval->outer = savestring (name, outer_len);
253 /* Make a copy of the using directives in the list pointed to by
254 USING, using OBSTACK to allocate memory. Free all memory pointed
255 to by USING via xfree. */
257 static struct using_direct *
258 cp_copy_usings (struct using_direct *using,
259 struct obstack *obstack)
267 struct using_direct *retval
268 = obstack_alloc (obstack, sizeof (struct using_direct));
269 retval->inner = obsavestring (using->inner, strlen (using->inner),
271 retval->outer = obsavestring (using->outer, strlen (using->outer),
273 retval->next = cp_copy_usings (using->next, obstack);
275 xfree (using->inner);
276 xfree (using->outer);
283 /* The C++-specific version of name lookup for static and global
284 names. This makes sure that names get looked for in all namespaces
285 that are in scope. NAME is the natural name of the symbol that
286 we're looking for, LINKAGE_NAME (which is optional) is its linkage
287 name, BLOCK is the block that we're searching within, DOMAIN says
288 what kind of symbols we're looking for, and if SYMTAB is non-NULL,
289 we should store the symtab where we found the symbol in it. */
292 cp_lookup_symbol_nonlocal (const char *name,
293 const char *linkage_name,
294 const struct block *block,
295 const domain_enum domain,
296 struct symtab **symtab)
298 return lookup_namespace_scope (name, linkage_name, block, domain,
299 symtab, block_scope (block), 0);
302 /* Lookup NAME at namespace scope (or, in C terms, in static and
303 global variables). SCOPE is the namespace that the current
304 function is defined within; only consider namespaces whose length
305 is at least SCOPE_LEN. Other arguments are as in
306 cp_lookup_symbol_nonlocal.
308 For example, if we're within a function A::B::f and looking for a
309 symbol f, this will get called with NAME = "f", SCOPE = "A::B", and
310 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
311 but with SCOPE_LEN = 1. And then it calls itself with NAME and
312 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
313 "A::B::x"; if it doesn't find it, then the second call looks for
314 "A::x", and if that call fails, then the first call looks for
317 static struct symbol *
318 lookup_namespace_scope (const char *name,
319 const char *linkage_name,
320 const struct block *block,
321 const domain_enum domain,
322 struct symtab **symtab,
328 if (scope[scope_len] != '\0')
330 /* Recursively search for names in child namespaces first. */
333 int new_scope_len = scope_len;
335 /* If the current scope is followed by "::", skip past that. */
336 if (new_scope_len != 0)
338 gdb_assert (scope[new_scope_len] == ':');
341 new_scope_len += cp_find_first_component (scope + new_scope_len);
342 sym = lookup_namespace_scope (name, linkage_name, block,
344 scope, new_scope_len);
349 /* Okay, we didn't find a match in our children, so look for the
350 name in the current namespace. */
352 namespace = alloca (scope_len + 1);
353 strncpy (namespace, scope, scope_len);
354 namespace[scope_len] = '\0';
355 return cp_lookup_symbol_namespace (namespace, name, linkage_name,
356 block, domain, symtab);
359 /* Look up NAME in the C++ namespace NAMESPACE, applying the using
360 directives that are active in BLOCK. Other arguments are as in
361 cp_lookup_symbol_nonlocal. */
364 cp_lookup_symbol_namespace (const char *namespace,
366 const char *linkage_name,
367 const struct block *block,
368 const domain_enum domain,
369 struct symtab **symtab)
371 const struct using_direct *current;
374 /* First, go through the using directives. If any of them add new
375 names to the namespace we're searching in, see if we can find a
376 match by applying them. */
378 for (current = block_using (block);
380 current = current->next)
382 if (strcmp (namespace, current->outer) == 0)
384 sym = cp_lookup_symbol_namespace (current->inner,
395 /* We didn't find anything by applying any of the using directives
396 that are still applicable; so let's see if we've got a match
397 using the current namespace. */
399 if (namespace[0] == '\0')
401 return lookup_symbol_file (name, linkage_name, block,
406 char *concatenated_name
407 = alloca (strlen (namespace) + 2 + strlen (name) + 1);
408 strcpy (concatenated_name, namespace);
409 strcat (concatenated_name, "::");
410 strcat (concatenated_name, name);
411 sym = lookup_symbol_file (concatenated_name, linkage_name,
412 block, domain, symtab,
413 cp_is_anonymous (namespace));
418 /* Look up NAME in BLOCK's static block and in global blocks. If
419 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
420 within an anonymous namespace. Other arguments are as in
421 cp_lookup_symbol_nonlocal. */
423 static struct symbol *
424 lookup_symbol_file (const char *name,
425 const char *linkage_name,
426 const struct block *block,
427 const domain_enum domain,
428 struct symtab **symtab,
429 int anonymous_namespace)
431 struct symbol *sym = NULL;
433 sym = lookup_symbol_static (name, linkage_name, block, domain, symtab);
437 if (anonymous_namespace)
439 /* Symbols defined in anonymous namespaces have external linkage
440 but should be treated as local to a single file nonetheless.
441 So we only search the current file's global block. */
443 const struct block *global_block = block_global_block (block);
445 if (global_block != NULL)
446 return lookup_symbol_aux_block (name, linkage_name, global_block,
453 return lookup_symbol_global (name, linkage_name, domain, symtab);