2003-07-16 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / cp-namespace.c
CommitLineData
9219021c
DC
1/* Helper routines for C++ support in GDB.
2 Copyright 2003 Free Software Foundation, Inc.
3
1fcb5155 4 Contributed by David Carlton and by Kealia, Inc.
9219021c
DC
5
6 This file is part of GDB.
7
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.
12
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.
17
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. */
22
23#include "defs.h"
24#include "cp-support.h"
25#include "gdb_obstack.h"
26#include "symtab.h"
27#include "symfile.h"
28#include "gdb_assert.h"
29#include "block.h"
30
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. */
34
35unsigned char processing_has_namespace_info;
36
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. */
40
0fc9922a
DC
41/* FIXME: carlton/2003-06-12: This isn't entirely reliable: currently,
42 we get mislead by DW_AT_specification. */
43
9219021c
DC
44const char *processing_current_namespace;
45
46/* List of using directives that are active in the current file. */
47
48static struct using_direct *using_list;
49
50static struct using_direct *cp_add_using (const char *name,
51 unsigned int inner_len,
52 unsigned int outer_len,
53 struct using_direct *next);
54
55static struct using_direct *cp_copy_usings (struct using_direct *using,
56 struct obstack *obstack);
57
1fcb5155
DC
58static 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,
63 const char *scope,
64 int scope_len);
65
66static 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);
72
9219021c
DC
73/* Set up support for dealing with C++ namespace info in the current
74 symtab. */
75
76void cp_initialize_namespace ()
77{
78 processing_has_namespace_info = 0;
79 using_list = NULL;
80}
81
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
84 for allocation. */
85
86void
87cp_finalize_namespace (struct block *static_block,
88 struct obstack *obstack)
89{
90 if (using_list != NULL)
91 {
92 block_set_using (static_block,
93 cp_copy_usings (using_list, obstack),
94 obstack);
95 using_list = NULL;
96 }
97}
98
99/* Check to see if SYMBOL refers to an object contained within an
100 anonymous namespace; if so, add an appropriate using directive. */
101
102/* Optimize away strlen ("(anonymous namespace)"). */
103
104#define ANONYMOUS_NAMESPACE_LEN 21
105
106void
107cp_scan_for_anonymous_namespaces (const struct symbol *symbol)
108{
109 if (!processing_has_namespace_info
110 && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
111 {
112 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
113 unsigned int previous_component;
114 unsigned int next_component;
115 const char *len;
116
117 /* Start with a quick-and-dirty check for mention of "(anonymous
118 namespace)". */
119
120 if (!cp_is_anonymous (name))
121 return;
122
123 previous_component = 0;
124 next_component = cp_find_first_component (name + previous_component);
125
126 while (name[next_component] == ':')
127 {
128 if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
129 && strncmp (name + previous_component,
130 "(anonymous namespace)",
131 ANONYMOUS_NAMESPACE_LEN) == 0)
132 {
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,
140 next_component);
141 }
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));
147 }
148 }
149}
150
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
156 twice. */
157
158void
159cp_add_using_directive (const char *name, unsigned int outer_length,
160 unsigned int inner_length)
161{
162 struct using_direct *current;
163 struct using_direct *new;
164
165 /* Has it already been added? */
166
167 for (current = using_list; current != NULL; current = current->next)
168 {
169 if ((strncmp (current->inner, name, inner_length) == 0)
170 && (strlen (current->inner) == inner_length)
171 && (strlen (current->outer) == outer_length))
172 return;
173 }
174
175 using_list = cp_add_using (name, inner_length, outer_length,
176 using_list);
177}
178
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. */
182
183void
184cp_set_block_scope (const struct symbol *symbol,
185 struct block *block,
186 struct obstack *obstack)
187{
188 /* Make sure that the name was originally mangled: if not, there
189 certainly isn't any namespace information to worry about! */
190
191 if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
192 {
0fc9922a
DC
193#if 0
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. */
198
9219021c
DC
199 if (processing_has_namespace_info)
200 {
201 block_set_scope
202 (block, obsavestring (processing_current_namespace,
203 strlen (processing_current_namespace),
204 obstack),
205 obstack);
206 }
207 else
0fc9922a 208#endif
9219021c
DC
209 {
210 /* Try to figure out the appropriate namespace from the
211 demangled name. */
212
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. */
217
218 const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
219 unsigned int prefix_len = cp_entire_prefix_len (name);
220
221 block_set_scope (block,
222 obsavestring (name, prefix_len, obstack),
223 obstack);
224 }
225 }
226}
227
228/* Test whether or not NAMESPACE looks like it mentions an anonymous
229 namespace; return nonzero if so. */
230
231int
232cp_is_anonymous (const char *namespace)
233{
234 return (strstr (namespace, "(anonymous namespace)")
235 != NULL);
236}
237
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
243 string. */
244
245static struct using_direct *
246cp_add_using (const char *name,
247 unsigned int inner_len,
248 unsigned int outer_len,
249 struct using_direct *next)
250{
251 struct using_direct *retval;
252
253 gdb_assert (outer_len < inner_len);
254
255 retval = xmalloc (sizeof (struct using_direct));
256 retval->inner = savestring (name, inner_len);
257 retval->outer = savestring (name, outer_len);
258 retval->next = next;
259
260 return retval;
261}
262
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. */
266
267static struct using_direct *
268cp_copy_usings (struct using_direct *using,
269 struct obstack *obstack)
270{
271 if (using == NULL)
272 {
273 return NULL;
274 }
275 else
276 {
277 struct using_direct *retval
278 = obstack_alloc (obstack, sizeof (struct using_direct));
279 retval->inner = obsavestring (using->inner, strlen (using->inner),
280 obstack);
281 retval->outer = obsavestring (using->outer, strlen (using->outer),
282 obstack);
283 retval->next = cp_copy_usings (using->next, obstack);
284
285 xfree (using->inner);
286 xfree (using->outer);
287 xfree (using);
288
289 return retval;
290 }
291}
1fcb5155
DC
292
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. */
300
301struct symbol *
302cp_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)
307{
308 return lookup_namespace_scope (name, linkage_name, block, domain,
309 symtab, block_scope (block), 0);
310}
311
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.
317
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
325 "x". */
326
327static struct symbol *
328lookup_namespace_scope (const char *name,
329 const char *linkage_name,
330 const struct block *block,
331 const domain_enum domain,
332 struct symtab **symtab,
333 const char *scope,
334 int scope_len)
335{
336 char *namespace;
337
338 if (scope[scope_len] != '\0')
339 {
340 /* Recursively search for names in child namespaces first. */
341
342 struct symbol *sym;
343 int new_scope_len = scope_len;
344
345 /* If the current scope is followed by "::", skip past that. */
346 if (new_scope_len != 0)
347 {
348 gdb_assert (scope[new_scope_len] == ':');
349 new_scope_len += 2;
350 }
351 new_scope_len += cp_find_first_component (scope + new_scope_len);
352 sym = lookup_namespace_scope (name, linkage_name, block,
353 domain, symtab,
354 scope, new_scope_len);
355 if (sym != NULL)
356 return sym;
357 }
358
359 /* Okay, we didn't find a match in our children, so look for the
360 name in the current namespace. */
361
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);
367}
368
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. */
372
373struct symbol *
374cp_lookup_symbol_namespace (const char *namespace,
375 const char *name,
376 const char *linkage_name,
377 const struct block *block,
378 const domain_enum domain,
379 struct symtab **symtab)
380{
381 const struct using_direct *current;
382 struct symbol *sym;
383
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. */
387
388 for (current = block_using (block);
389 current != NULL;
390 current = current->next)
391 {
392 if (strcmp (namespace, current->outer) == 0)
393 {
394 sym = cp_lookup_symbol_namespace (current->inner,
395 name,
396 linkage_name,
397 block,
398 domain,
399 symtab);
400 if (sym != NULL)
401 return sym;
402 }
403 }
404
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. */
408
409 if (namespace[0] == '\0')
410 {
411 return lookup_symbol_file (name, linkage_name, block,
412 domain, symtab, 0);
413 }
414 else
415 {
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));
424 return sym;
425 }
426}
427
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. */
432
433static struct symbol *
434lookup_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)
440{
441 struct symbol *sym = NULL;
442
443 sym = lookup_symbol_static (name, linkage_name, block, domain, symtab);
444 if (sym != NULL)
445 return sym;
446
447 if (anonymous_namespace)
448 {
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. */
452
453 const struct block *global_block = block_global_block (block);
454
455 if (global_block != NULL)
456 return lookup_symbol_aux_block (name, linkage_name, global_block,
457 domain, symtab);
458 else
459 return NULL;
460 }
461 else
462 {
463 return lookup_symbol_global (name, linkage_name, domain, symtab);
464 }
465}
This page took 0.060336 seconds and 4 git commands to generate.