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