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