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