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