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