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