* completer.c (location_completer): Fix typo in comment.
[deliverable/binutils-gdb.git] / gdb / cp-namespace.c
CommitLineData
9219021c 1/* Helper routines for C++ support in GDB.
28e7fd62 2 Copyright (C) 2003-2013 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"
34eaf542 34#include "language.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,
8dea366b
KS
45 int anonymous_namespace,
46 int search);
1fcb5155 47
b368761e
DC
48static struct type *cp_lookup_transparent_type_loop (const char *name,
49 const char *scope,
50 int scope_len);
51
9219021c
DC
52/* Check to see if SYMBOL refers to an object contained within an
53 anonymous namespace; if so, add an appropriate using directive. */
54
9219021c 55void
a10964d1
AR
56cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
57 struct objfile *const objfile)
9219021c 58{
df8a16a1 59 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
9219021c 60 {
df8a16a1 61 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
9219021c
DC
62 unsigned int previous_component;
63 unsigned int next_component;
9219021c
DC
64
65 /* Start with a quick-and-dirty check for mention of "(anonymous
66 namespace)". */
67
68 if (!cp_is_anonymous (name))
69 return;
70
71 previous_component = 0;
72 next_component = cp_find_first_component (name + previous_component);
73
74 while (name[next_component] == ':')
75 {
2b1dbab0
KS
76 if (((next_component - previous_component)
77 == CP_ANONYMOUS_NAMESPACE_LEN)
9219021c 78 && strncmp (name + previous_component,
2b1dbab0
KS
79 CP_ANONYMOUS_NAMESPACE_STR,
80 CP_ANONYMOUS_NAMESPACE_LEN) == 0)
9219021c 81 {
aff410f1
MS
82 int dest_len = (previous_component == 0
83 ? 0 : previous_component - 2);
8c902bb1 84 int src_len = next_component;
794684b6 85
8c902bb1
SW
86 char *dest = alloca (dest_len + 1);
87 char *src = alloca (src_len + 1);
794684b6 88
8c902bb1
SW
89 memcpy (dest, name, dest_len);
90 memcpy (src, name, src_len);
794684b6 91
8c902bb1
SW
92 dest[dest_len] = '\0';
93 src[src_len] = '\0';
794684b6 94
9219021c
DC
95 /* We've found a component of the name that's an
96 anonymous namespace. So add symbols in it to the
97 namespace given by the previous component if there is
98 one, or to the global namespace if there isn't. */
12aaed36 99 cp_add_using_directive (dest, src, NULL, NULL, NULL, 1,
a10964d1 100 &objfile->objfile_obstack);
9219021c
DC
101 }
102 /* The "+ 2" is for the "::". */
103 previous_component = next_component + 2;
104 next_component = (previous_component
105 + cp_find_first_component (name
106 + previous_component));
107 }
108 }
109}
110
c0cc3a76 111
aff410f1
MS
112/* Add a using directive to using_directives. If the using directive
113 in question has already been added, don't add it twice.
114
115 Create a new struct using_direct which imports the namespace SRC
116 into the scope DEST. ALIAS is the name of the imported namespace
117 in the current scope. If ALIAS is NULL then the namespace is known
118 by its original name. DECLARATION is the name if the imported
119 varable if this is a declaration import (Eg. using A::x), otherwise
12aaed36
TT
120 it is NULL. EXCLUDES is a list of names not to import from an
121 imported module or NULL. If COPY_NAMES is non-zero, then the
122 arguments are copied into newly allocated memory so they can be
123 temporaries. For EXCLUDES the VEC pointers are copied but the
32019081 124 pointed to characters are not copied. */
9219021c
DC
125
126void
13387711
SW
127cp_add_using_directive (const char *dest,
128 const char *src,
129 const char *alias,
130 const char *declaration,
32019081 131 VEC (const_char_ptr) *excludes,
12aaed36 132 int copy_names,
c0cc3a76 133 struct obstack *obstack)
9219021c
DC
134{
135 struct using_direct *current;
136 struct using_direct *new;
13387711 137
9219021c
DC
138 /* Has it already been added? */
139
27aa8d6a 140 for (current = using_directives; current != NULL; current = current->next)
9219021c 141 {
32019081
JK
142 int ix;
143 const char *param;
144
70c622a3
JK
145 if (strcmp (current->import_src, src) != 0)
146 continue;
147 if (strcmp (current->import_dest, dest) != 0)
148 continue;
149 if ((alias == NULL && current->alias != NULL)
150 || (alias != NULL && current->alias == NULL)
151 || (alias != NULL && current->alias != NULL
152 && strcmp (alias, current->alias) != 0))
153 continue;
154 if ((declaration == NULL && current->declaration != NULL)
155 || (declaration != NULL && current->declaration == NULL)
156 || (declaration != NULL && current->declaration != NULL
157 && strcmp (declaration, current->declaration) != 0))
158 continue;
159
32019081
JK
160 /* Compare the contents of EXCLUDES. */
161 for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
162 if (current->excludes[ix] == NULL
163 || strcmp (param, current->excludes[ix]) != 0)
164 break;
165 if (ix < VEC_length (const_char_ptr, excludes)
166 || current->excludes[ix] != NULL)
167 continue;
168
70c622a3
JK
169 /* Parameters exactly match CURRENT. */
170 return;
9219021c
DC
171 }
172
32019081
JK
173 new = obstack_alloc (obstack, (sizeof (*new)
174 + (VEC_length (const_char_ptr, excludes)
175 * sizeof (*new->excludes))));
176 memset (new, 0, sizeof (*new));
c0cc3a76 177
12aaed36
TT
178 if (copy_names)
179 {
180 new->import_src = obstack_copy0 (obstack, src, strlen (src));
181 new->import_dest = obstack_copy0 (obstack, dest, strlen (dest));
182 }
183 else
184 {
185 new->import_src = src;
186 new->import_dest = dest;
187 }
794684b6 188
12aaed36 189 if (alias != NULL && copy_names)
10f0c4bb 190 new->alias = obstack_copy0 (obstack, alias, strlen (alias));
12aaed36
TT
191 else
192 new->alias = alias;
c0cc3a76 193
12aaed36 194 if (declaration != NULL && copy_names)
10f0c4bb
TT
195 new->declaration = obstack_copy0 (obstack,
196 declaration, strlen (declaration));
12aaed36
TT
197 else
198 new->declaration = declaration;
13387711 199
32019081
JK
200 memcpy (new->excludes, VEC_address (const_char_ptr, excludes),
201 VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes));
202 new->excludes[VEC_length (const_char_ptr, excludes)] = NULL;
203
c0cc3a76
SW
204 new->next = using_directives;
205 using_directives = new;
9219021c
DC
206}
207
9219021c
DC
208/* Test whether or not NAMESPACE looks like it mentions an anonymous
209 namespace; return nonzero if so. */
210
211int
212cp_is_anonymous (const char *namespace)
213{
2b1dbab0 214 return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR)
9219021c
DC
215 != NULL);
216}
217
1fcb5155
DC
218/* The C++-specific version of name lookup for static and global
219 names. This makes sure that names get looked for in all namespaces
220 that are in scope. NAME is the natural name of the symbol that
94af9270 221 we're looking for, BLOCK is the block that we're searching within,
aff410f1
MS
222 DOMAIN says what kind of symbols we're looking for, and if SYMTAB
223 is non-NULL, we should store the symtab where we found the symbol
224 in it. */
1fcb5155
DC
225
226struct symbol *
227cp_lookup_symbol_nonlocal (const char *name,
1fcb5155 228 const struct block *block,
21b556f4 229 const domain_enum domain)
1fcb5155 230{
8540c487
SW
231 struct symbol *sym;
232 const char *scope = block_scope (block);
233
aff410f1
MS
234 sym = lookup_namespace_scope (name, block,
235 domain, scope, 0);
8540c487
SW
236 if (sym != NULL)
237 return sym;
238
aff410f1
MS
239 return cp_lookup_symbol_namespace (scope, name,
240 block, domain);
8540c487
SW
241}
242
aff410f1 243/* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
8dea366b
KS
244 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
245 through base classes for a matching symbol. */
8540c487
SW
246
247static struct symbol *
248cp_lookup_symbol_in_namespace (const char *namespace,
249 const char *name,
8540c487 250 const struct block *block,
8dea366b 251 const domain_enum domain, int search)
8540c487
SW
252{
253 if (namespace[0] == '\0')
254 {
8dea366b 255 return lookup_symbol_file (name, block, domain, 0, search);
8540c487
SW
256 }
257 else
258 {
aff410f1
MS
259 char *concatenated_name = alloca (strlen (namespace) + 2
260 + strlen (name) + 1);
c5504eaf 261
8540c487
SW
262 strcpy (concatenated_name, namespace);
263 strcat (concatenated_name, "::");
264 strcat (concatenated_name, name);
aff410f1 265 return lookup_symbol_file (concatenated_name, block, domain,
8dea366b 266 cp_is_anonymous (namespace), search);
8540c487
SW
267 }
268}
269
b14e635e
SW
270/* Used for cleanups to reset the "searched" flag incase
271 of an error. */
272
273static void
274reset_directive_searched (void *data)
275{
276 struct using_direct *direct = data;
277 direct->searched = 0;
278}
279
aff410f1
MS
280/* Search for NAME by applying all import statements belonging to
281 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
282 search is restricted to using declarations.
13387711
SW
283 Example:
284
aff410f1 285 namespace A {
13387711
SW
286 int x;
287 }
288 using A::x;
289
aff410f1
MS
290 If SEARCH_PARENTS the search will include imports which are
291 applicable in parents of SCOPE.
b14e635e
SW
292 Example:
293
aff410f1 294 namespace A {
b14e635e 295 using namespace X;
aff410f1 296 namespace B {
b14e635e
SW
297 using namespace Y;
298 }
299 }
300
aff410f1
MS
301 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
302 namespaces X and Y will be considered. If SEARCH_PARENTS is false
303 only the import of Y is considered. */
8540c487 304
13387711 305struct symbol *
8540c487
SW
306cp_lookup_symbol_imports (const char *scope,
307 const char *name,
8540c487 308 const struct block *block,
b14e635e 309 const domain_enum domain,
13387711 310 const int declaration_only,
b14e635e 311 const int search_parents)
8540c487 312{
b14e635e 313 struct using_direct *current;
13387711 314 struct symbol *sym = NULL;
8540c487 315 int len;
b14e635e
SW
316 int directive_match;
317 struct cleanup *searched_cleanup;
8540c487
SW
318
319 /* First, try to find the symbol in the given namespace. */
13387711 320 if (!declaration_only)
aff410f1 321 sym = cp_lookup_symbol_in_namespace (scope, name,
8dea366b 322 block, domain, 1);
13387711 323
8540c487
SW
324 if (sym != NULL)
325 return sym;
326
aff410f1
MS
327 /* Go through the using directives. If any of them add new names to
328 the namespace we're searching in, see if we can find a match by
329 applying them. */
8540c487
SW
330
331 for (current = block_using (block);
332 current != NULL;
333 current = current->next)
334 {
32019081
JK
335 const char **excludep;
336
b14e635e
SW
337 len = strlen (current->import_dest);
338 directive_match = (search_parents
339 ? (strncmp (scope, current->import_dest,
340 strlen (current->import_dest)) == 0
341 && (len == 0
aff410f1
MS
342 || scope[len] == ':'
343 || scope[len] == '\0'))
b14e635e 344 : strcmp (scope, current->import_dest) == 0);
8540c487 345
aff410f1
MS
346 /* If the import destination is the current scope or one of its
347 ancestors then it is applicable. */
b14e635e 348 if (directive_match && !current->searched)
8540c487 349 {
a1d705ee
TT
350 /* Mark this import as searched so that the recursive call
351 does not search it again. */
352 current->searched = 1;
353 searched_cleanup = make_cleanup (reset_directive_searched,
354 current);
355
356 /* If there is an import of a single declaration, compare the
357 imported declaration (after optional renaming by its alias)
358 with the sought out name. If there is a match pass
359 current->import_src as NAMESPACE to direct the search
360 towards the imported namespace. */
361 if (current->declaration
362 && strcmp (name, current->alias
363 ? current->alias : current->declaration) == 0)
364 sym = cp_lookup_symbol_in_namespace (current->import_src,
365 current->declaration,
8dea366b 366 block, domain, 1);
a1d705ee
TT
367
368 /* If this is a DECLARATION_ONLY search or a symbol was found
369 or this import statement was an import declaration, the
370 search of this import is complete. */
371 if (declaration_only || sym != NULL || current->declaration)
372 {
373 current->searched = 0;
374 discard_cleanups (searched_cleanup);
375
376 if (sym != NULL)
377 return sym;
378
379 continue;
380 }
381
382 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
383 for (excludep = current->excludes; *excludep; excludep++)
384 if (strcmp (name, *excludep) == 0)
385 break;
386 if (*excludep)
387 {
388 discard_cleanups (searched_cleanup);
389 continue;
390 }
391
392 if (current->alias != NULL
393 && strcmp (name, current->alias) == 0)
394 /* If the import is creating an alias and the alias matches
395 the sought name. Pass current->import_src as the NAME to
396 direct the search towards the aliased namespace. */
397 {
398 sym = cp_lookup_symbol_in_namespace (scope,
399 current->import_src,
8dea366b 400 block, domain, 1);
a1d705ee
TT
401 }
402 else if (current->alias == NULL)
403 {
404 /* If this import statement creates no alias, pass
405 current->inner as NAMESPACE to direct the search
406 towards the imported namespace. */
407 sym = cp_lookup_symbol_imports (current->import_src,
408 name, block,
409 domain, 0, 0);
410 }
411 current->searched = 0;
412 discard_cleanups (searched_cleanup);
413
414 if (sym != NULL)
415 return sym;
8540c487
SW
416 }
417 }
418
419 return NULL;
420}
421
34eaf542
TT
422/* Helper function that searches an array of symbols for one named
423 NAME. */
424
425static struct symbol *
aff410f1
MS
426search_symbol_list (const char *name, int num,
427 struct symbol **syms)
34eaf542
TT
428{
429 int i;
430
431 /* Maybe we should store a dictionary in here instead. */
432 for (i = 0; i < num; ++i)
433 {
434 if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
435 return syms[i];
436 }
437 return NULL;
438}
439
440/* Like cp_lookup_symbol_imports, but if BLOCK is a function, it
441 searches through the template parameters of the function and the
442 function's type. */
443
444struct symbol *
445cp_lookup_symbol_imports_or_template (const char *scope,
446 const char *name,
447 const struct block *block,
448 const domain_enum domain)
449{
450 struct symbol *function = BLOCK_FUNCTION (block);
451
452 if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
453 {
34eaf542
TT
454 /* Search the function's template parameters. */
455 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
456 {
aff410f1
MS
457 struct template_symbol *templ
458 = (struct template_symbol *) function;
34eaf542
TT
459 struct symbol *result;
460
461 result = search_symbol_list (name,
462 templ->n_template_arguments,
463 templ->template_arguments);
464 if (result != NULL)
465 return result;
466 }
467
468 /* Search the template parameters of the function's defining
469 context. */
470 if (SYMBOL_NATURAL_NAME (function))
471 {
472 struct type *context;
473 char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
474 struct cleanup *cleanups = make_cleanup (xfree, name_copy);
475 const struct language_defn *lang = language_def (language_cplus);
476 struct gdbarch *arch = SYMBOL_SYMTAB (function)->objfile->gdbarch;
477 const struct block *parent = BLOCK_SUPERBLOCK (block);
478
479 while (1)
480 {
481 struct symbol *result;
482 unsigned int prefix_len = cp_entire_prefix_len (name_copy);
483
484 if (prefix_len == 0)
485 context = NULL;
486 else
487 {
488 name_copy[prefix_len] = '\0';
aff410f1
MS
489 context = lookup_typename (lang, arch,
490 name_copy,
491 parent, 1);
34eaf542
TT
492 }
493
494 if (context == NULL)
495 break;
496
aff410f1
MS
497 result
498 = search_symbol_list (name,
499 TYPE_N_TEMPLATE_ARGUMENTS (context),
500 TYPE_TEMPLATE_ARGUMENTS (context));
34eaf542
TT
501 if (result != NULL)
502 return result;
503 }
504
505 do_cleanups (cleanups);
506 }
507 }
508
509 return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1);
510}
511
aff410f1
MS
512 /* Searches for NAME in the current namespace, and by applying
513 relevant import statements belonging to BLOCK and its parents.
514 SCOPE is the namespace scope of the context in which the search is
515 being evaluated. */
8540c487
SW
516
517struct symbol*
518cp_lookup_symbol_namespace (const char *scope,
519 const char *name,
8540c487 520 const struct block *block,
13387711 521 const domain_enum domain)
8540c487
SW
522{
523 struct symbol *sym;
13387711
SW
524
525 /* First, try to find the symbol in the given namespace. */
aff410f1 526 sym = cp_lookup_symbol_in_namespace (scope, name,
8dea366b 527 block, domain, 1);
13387711
SW
528 if (sym != NULL)
529 return sym;
8540c487 530
aff410f1
MS
531 /* Search for name in namespaces imported to this and parent
532 blocks. */
8540c487
SW
533 while (block != NULL)
534 {
aff410f1
MS
535 sym = cp_lookup_symbol_imports (scope, name, block,
536 domain, 0, 1);
8540c487
SW
537
538 if (sym)
539 return sym;
540
541 block = BLOCK_SUPERBLOCK (block);
542 }
543
544 return NULL;
1fcb5155
DC
545}
546
547/* Lookup NAME at namespace scope (or, in C terms, in static and
548 global variables). SCOPE is the namespace that the current
549 function is defined within; only consider namespaces whose length
550 is at least SCOPE_LEN. Other arguments are as in
551 cp_lookup_symbol_nonlocal.
552
553 For example, if we're within a function A::B::f and looking for a
3882f37a 554 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
1fcb5155
DC
555 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
556 but with SCOPE_LEN = 1. And then it calls itself with NAME and
557 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
558 "A::B::x"; if it doesn't find it, then the second call looks for
559 "A::x", and if that call fails, then the first call looks for
560 "x". */
561
562static struct symbol *
563lookup_namespace_scope (const char *name,
1fcb5155
DC
564 const struct block *block,
565 const domain_enum domain,
1fcb5155
DC
566 const char *scope,
567 int scope_len)
568{
569 char *namespace;
570
571 if (scope[scope_len] != '\0')
572 {
573 /* Recursively search for names in child namespaces first. */
574
575 struct symbol *sym;
576 int new_scope_len = scope_len;
577
578 /* If the current scope is followed by "::", skip past that. */
579 if (new_scope_len != 0)
580 {
581 gdb_assert (scope[new_scope_len] == ':');
582 new_scope_len += 2;
583 }
584 new_scope_len += cp_find_first_component (scope + new_scope_len);
aff410f1
MS
585 sym = lookup_namespace_scope (name, block, domain,
586 scope, new_scope_len);
1fcb5155
DC
587 if (sym != NULL)
588 return sym;
589 }
590
591 /* Okay, we didn't find a match in our children, so look for the
592 name in the current namespace. */
593
594 namespace = alloca (scope_len + 1);
595 strncpy (namespace, scope, scope_len);
596 namespace[scope_len] = '\0';
aff410f1 597 return cp_lookup_symbol_in_namespace (namespace, name,
8dea366b 598 block, domain, 1);
1fcb5155
DC
599}
600
601/* Look up NAME in BLOCK's static block and in global blocks. If
602 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
8dea366b
KS
603 within an anonymous namespace. If SEARCH is non-zero, search through
604 base classes for a matching symbol. Other arguments are as in
1fcb5155
DC
605 cp_lookup_symbol_nonlocal. */
606
607static struct symbol *
608lookup_symbol_file (const char *name,
1fcb5155
DC
609 const struct block *block,
610 const domain_enum domain,
8dea366b 611 int anonymous_namespace, int search)
1fcb5155
DC
612{
613 struct symbol *sym = NULL;
614
94af9270 615 sym = lookup_symbol_static (name, block, domain);
1fcb5155
DC
616 if (sym != NULL)
617 return sym;
618
619 if (anonymous_namespace)
620 {
621 /* Symbols defined in anonymous namespaces have external linkage
622 but should be treated as local to a single file nonetheless.
623 So we only search the current file's global block. */
624
625 const struct block *global_block = block_global_block (block);
626
627 if (global_block != NULL)
94af9270 628 sym = lookup_symbol_aux_block (name, global_block, domain);
1fcb5155
DC
629 }
630 else
631 {
94af9270 632 sym = lookup_symbol_global (name, block, domain);
5c4e30ca
DC
633 }
634
8dea366b
KS
635 if (sym != NULL)
636 return sym;
637
638 if (search)
639 {
640 char *klass, *nested;
641 unsigned int prefix_len;
642 struct cleanup *cleanup;
643 struct symbol *klass_sym;
644
645 /* A simple lookup failed. Check if the symbol was defined in
646 a base class. */
647
648 cleanup = make_cleanup (null_cleanup, NULL);
649
650 /* Find the name of the class and the name of the method,
651 variable, etc. */
652 prefix_len = cp_entire_prefix_len (name);
653
654 /* If no prefix was found, search "this". */
655 if (prefix_len == 0)
656 {
657 struct type *type;
658 struct symbol *this;
659
660 this = lookup_language_this (language_def (language_cplus), block);
661 if (this == NULL)
662 {
663 do_cleanups (cleanup);
664 return NULL;
665 }
666
667 type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this)));
668 klass = xstrdup (TYPE_NAME (type));
669 nested = xstrdup (name);
670 }
671 else
672 {
673 /* The class name is everything up to and including PREFIX_LEN. */
674 klass = savestring (name, prefix_len);
675
676 /* The rest of the name is everything else past the initial scope
677 operator. */
678 nested = xstrdup (name + prefix_len + 2);
679 }
680
681 /* Add cleanups to free memory for these strings. */
682 make_cleanup (xfree, klass);
683 make_cleanup (xfree, nested);
684
685 /* Lookup a class named KLASS. If none is found, there is nothing
686 more that can be done. */
687 klass_sym = lookup_symbol_global (klass, block, domain);
688 if (klass_sym == NULL)
689 {
690 do_cleanups (cleanup);
691 return NULL;
692 }
693
694 /* Look for a symbol named NESTED in this class. */
695 sym = cp_lookup_nested_symbol (SYMBOL_TYPE (klass_sym), nested, block);
696 do_cleanups (cleanup);
697 }
698
699 return sym;
700}
701
702/* Search through the base classes of PARENT_TYPE for a symbol named
703 NAME in block BLOCK. */
704
705static struct symbol *
706find_symbol_in_baseclass (struct type *parent_type, const char *name,
707 const struct block *block)
708{
709 int i;
710 struct symbol *sym;
711 struct cleanup *cleanup;
712 char *concatenated_name;
713
714 sym = NULL;
715 concatenated_name = NULL;
716 cleanup = make_cleanup (free_current_contents, &concatenated_name);
717 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
718 {
719 size_t len;
720 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
721
722 if (base_name == NULL)
723 continue;
724
725 /* Search this particular base class. */
726 sym = cp_lookup_symbol_namespace (base_name, name, block, VAR_DOMAIN);
727 if (sym != NULL)
728 break;
729
730 len = strlen (base_name) + 2 + strlen (name) + 1;
731 concatenated_name = xrealloc (concatenated_name, len);
732 xsnprintf (concatenated_name, len, "%s::%s", base_name, name);
733 sym = lookup_symbol_static (concatenated_name, block, VAR_DOMAIN);
734
735 /* If there is currently no BLOCK, e.g., the inferior hasn't yet
736 been started, then try searching all STATIC_BLOCK symbols in
737 all objfiles. */
738 if (block == NULL)
739 {
740 sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
741 if (sym != NULL)
742 break;
743 }
744
745 /* If this class has base classes, search them next. */
746 if (TYPE_N_BASECLASSES (TYPE_BASECLASS (parent_type, i)) > 0)
747 {
748 sym = find_symbol_in_baseclass (TYPE_BASECLASS (parent_type, i),
749 name, block);
750 if (sym != NULL)
751 break;
752 }
753 }
754
755 do_cleanups (cleanup);
0c2e6019 756 return sym;
5c4e30ca
DC
757}
758
50af5481 759/* Look up a symbol named NESTED_NAME that is nested inside the C++
79c2c32d
DC
760 class or namespace given by PARENT_TYPE, from within the context
761 given by BLOCK. Return NULL if there is no such nested type. */
762
50af5481
JK
763struct symbol *
764cp_lookup_nested_symbol (struct type *parent_type,
765 const char *nested_name,
766 const struct block *block)
79c2c32d 767{
d8228535
JK
768 /* type_name_no_tag_required provides better error reporting using the
769 original type. */
770 struct type *saved_parent_type = parent_type;
771
772 CHECK_TYPEDEF (parent_type);
773
79c2c32d
DC
774 switch (TYPE_CODE (parent_type))
775 {
63d06c5c 776 case TYPE_CODE_STRUCT:
79c2c32d 777 case TYPE_CODE_NAMESPACE:
48e32051 778 case TYPE_CODE_UNION:
79c2c32d 779 {
63d06c5c
DC
780 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
781 of classes like, say, data or function members. Instead,
782 they're just represented by symbols whose names are
783 qualified by the name of the surrounding class. This is
784 just like members of namespaces; in particular,
785 lookup_symbol_namespace works when looking them up. */
786
08850b56 787 int size;
d8228535 788 const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
aff410f1
MS
789 struct symbol *sym
790 = cp_lookup_symbol_in_namespace (parent_name, nested_name,
8dea366b 791 block, VAR_DOMAIN, 0);
41f62f39 792 char *concatenated_name;
c5504eaf 793
50af5481
JK
794 if (sym != NULL)
795 return sym;
41f62f39 796
aff410f1
MS
797 /* Now search all static file-level symbols. Not strictly
798 correct, but more useful than an error. We do not try to
799 guess any imported namespace as even the fully specified
8dea366b 800 namespace search is already not C++ compliant and more
aff410f1 801 assumptions could make it too magic. */
41f62f39 802
08850b56
PM
803 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
804 concatenated_name = alloca (size);
805 xsnprintf (concatenated_name, size, "%s::%s",
aff410f1 806 parent_name, nested_name);
50af5481
JK
807 sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
808 if (sym != NULL)
809 return sym;
41f62f39 810
8dea366b
KS
811 /* If no matching symbols were found, try searching any
812 base classes. */
813 return find_symbol_in_baseclass (parent_type, nested_name, block);
79c2c32d
DC
814 }
815 default:
816 internal_error (__FILE__, __LINE__,
50af5481 817 _("cp_lookup_nested_symbol called "
3e43a32a 818 "on a non-aggregate type."));
79c2c32d
DC
819 }
820}
821
b368761e
DC
822/* The C++-version of lookup_transparent_type. */
823
824/* FIXME: carlton/2004-01-16: The problem that this is trying to
825 address is that, unfortunately, sometimes NAME is wrong: it may not
826 include the name of namespaces enclosing the type in question.
b021a221 827 lookup_transparent_type gets called when the type in question
b368761e
DC
828 is a declaration, and we're trying to find its definition; but, for
829 declarations, our type name deduction mechanism doesn't work.
830 There's nothing we can do to fix this in general, I think, in the
831 absence of debug information about namespaces (I've filed PR
832 gdb/1511 about this); until such debug information becomes more
833 prevalent, one heuristic which sometimes looks is to search for the
834 definition in namespaces containing the current namespace.
835
836 We should delete this functions once the appropriate debug
837 information becomes more widespread. (GCC 3.4 will be the first
838 released version of GCC with such information.) */
839
840struct type *
841cp_lookup_transparent_type (const char *name)
842{
843 /* First, try the honest way of looking up the definition. */
844 struct type *t = basic_lookup_transparent_type (name);
845 const char *scope;
846
847 if (t != NULL)
848 return t;
849
850 /* If that doesn't work and we're within a namespace, look there
851 instead. */
852 scope = block_scope (get_selected_block (0));
853
854 if (scope[0] == '\0')
855 return NULL;
856
857 return cp_lookup_transparent_type_loop (name, scope, 0);
858}
859
b021a221
MS
860/* Lookup the type definition associated to NAME in namespaces/classes
861 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
862 must be the index of the start of a component of SCOPE. */
b368761e
DC
863
864static struct type *
aff410f1
MS
865cp_lookup_transparent_type_loop (const char *name,
866 const char *scope,
b368761e
DC
867 int length)
868{
1198ecbe 869 int scope_length = length + cp_find_first_component (scope + length);
b368761e
DC
870 char *full_name;
871
872 /* If the current scope is followed by "::", look in the next
873 component. */
874 if (scope[scope_length] == ':')
875 {
876 struct type *retval
aff410f1
MS
877 = cp_lookup_transparent_type_loop (name, scope,
878 scope_length + 2);
c5504eaf 879
b368761e
DC
880 if (retval != NULL)
881 return retval;
882 }
883
884 full_name = alloca (scope_length + 2 + strlen (name) + 1);
885 strncpy (full_name, scope, scope_length);
886 strncpy (full_name + scope_length, "::", 2);
887 strcpy (full_name + scope_length + 2, name);
888
889 return basic_lookup_transparent_type (full_name);
890}
891
0c2e6019
TT
892/* This used to do something but was removed when it became
893 obsolete. */
5c4e30ca
DC
894
895static void
896maintenance_cplus_namespace (char *args, int from_tty)
897{
0c2e6019 898 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
5c4e30ca
DC
899}
900
2c0b251b
PA
901/* Provide a prototype to silence -Wmissing-prototypes. */
902extern initialize_file_ftype _initialize_cp_namespace;
903
5c4e30ca
DC
904void
905_initialize_cp_namespace (void)
906{
0c2e6019
TT
907 struct cmd_list_element *cmd;
908
909 cmd = add_cmd ("namespace", class_maintenance,
910 maintenance_cplus_namespace,
911 _("Deprecated placeholder for removed functionality."),
912 &maint_cplus_cmd_list);
913 deprecate_cmd (cmd, NULL);
1fcb5155 914}
This page took 0.780656 seconds and 4 git commands to generate.