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