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