[gdb/ax] small "setv" fix and documentation's adjustment.
[deliverable/binutils-gdb.git] / gdb / cp-namespace.c
CommitLineData
9219021c 1/* Helper routines for C++ support in GDB.
32d0add0 2 Copyright (C) 2003-2015 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"
9219021c 26#include "block.h"
5c4e30ca
DC
27#include "objfiles.h"
28#include "gdbtypes.h"
29#include "dictionary.h"
30#include "command.h"
b368761e 31#include "frame.h"
27aa8d6a 32#include "buildsym.h"
34eaf542 33#include "language.h"
9219021c 34
6f27419a
DE
35static struct symbol *
36 cp_lookup_nested_symbol_1 (struct type *container_type,
37 const char *nested_name,
38 const char *concatenated_name,
39 const struct block *block,
40 int basic_lookup);
41
b368761e
DC
42static struct type *cp_lookup_transparent_type_loop (const char *name,
43 const char *scope,
44 int scope_len);
45
9219021c
DC
46/* Check to see if SYMBOL refers to an object contained within an
47 anonymous namespace; if so, add an appropriate using directive. */
48
9219021c 49void
a10964d1
AR
50cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
51 struct objfile *const objfile)
9219021c 52{
df8a16a1 53 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
9219021c 54 {
df8a16a1 55 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
9219021c
DC
56 unsigned int previous_component;
57 unsigned int next_component;
9219021c
DC
58
59 /* Start with a quick-and-dirty check for mention of "(anonymous
60 namespace)". */
61
59da4d04 62 if (!cp_is_in_anonymous (name))
9219021c
DC
63 return;
64
65 previous_component = 0;
66 next_component = cp_find_first_component (name + previous_component);
67
68 while (name[next_component] == ':')
69 {
2b1dbab0
KS
70 if (((next_component - previous_component)
71 == CP_ANONYMOUS_NAMESPACE_LEN)
9219021c 72 && strncmp (name + previous_component,
2b1dbab0
KS
73 CP_ANONYMOUS_NAMESPACE_STR,
74 CP_ANONYMOUS_NAMESPACE_LEN) == 0)
9219021c 75 {
aff410f1
MS
76 int dest_len = (previous_component == 0
77 ? 0 : previous_component - 2);
8c902bb1 78 int src_len = next_component;
794684b6 79
8c902bb1
SW
80 char *dest = alloca (dest_len + 1);
81 char *src = alloca (src_len + 1);
794684b6 82
8c902bb1
SW
83 memcpy (dest, name, dest_len);
84 memcpy (src, name, src_len);
794684b6 85
8c902bb1
SW
86 dest[dest_len] = '\0';
87 src[src_len] = '\0';
794684b6 88
9219021c
DC
89 /* We've found a component of the name that's an
90 anonymous namespace. So add symbols in it to the
91 namespace given by the previous component if there is
92 one, or to the global namespace if there isn't. */
12aaed36 93 cp_add_using_directive (dest, src, NULL, NULL, NULL, 1,
a10964d1 94 &objfile->objfile_obstack);
9219021c
DC
95 }
96 /* The "+ 2" is for the "::". */
97 previous_component = next_component + 2;
98 next_component = (previous_component
99 + cp_find_first_component (name
100 + previous_component));
101 }
102 }
103}
104
aff410f1
MS
105/* Add a using directive to using_directives. If the using directive
106 in question has already been added, don't add it twice.
107
108 Create a new struct using_direct which imports the namespace SRC
109 into the scope DEST. ALIAS is the name of the imported namespace
110 in the current scope. If ALIAS is NULL then the namespace is known
111 by its original name. DECLARATION is the name if the imported
112 varable if this is a declaration import (Eg. using A::x), otherwise
12aaed36
TT
113 it is NULL. EXCLUDES is a list of names not to import from an
114 imported module or NULL. If COPY_NAMES is non-zero, then the
115 arguments are copied into newly allocated memory so they can be
116 temporaries. For EXCLUDES the VEC pointers are copied but the
32019081 117 pointed to characters are not copied. */
9219021c
DC
118
119void
13387711
SW
120cp_add_using_directive (const char *dest,
121 const char *src,
122 const char *alias,
123 const char *declaration,
32019081 124 VEC (const_char_ptr) *excludes,
12aaed36 125 int copy_names,
c0cc3a76 126 struct obstack *obstack)
9219021c
DC
127{
128 struct using_direct *current;
129 struct using_direct *new;
791244be 130
9219021c
DC
131 /* Has it already been added? */
132
27aa8d6a 133 for (current = using_directives; current != NULL; current = current->next)
9219021c 134 {
32019081
JK
135 int ix;
136 const char *param;
137
70c622a3
JK
138 if (strcmp (current->import_src, src) != 0)
139 continue;
140 if (strcmp (current->import_dest, dest) != 0)
141 continue;
142 if ((alias == NULL && current->alias != NULL)
143 || (alias != NULL && current->alias == NULL)
144 || (alias != NULL && current->alias != NULL
145 && strcmp (alias, current->alias) != 0))
146 continue;
147 if ((declaration == NULL && current->declaration != NULL)
148 || (declaration != NULL && current->declaration == NULL)
149 || (declaration != NULL && current->declaration != NULL
150 && strcmp (declaration, current->declaration) != 0))
151 continue;
152
32019081
JK
153 /* Compare the contents of EXCLUDES. */
154 for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
155 if (current->excludes[ix] == NULL
156 || strcmp (param, current->excludes[ix]) != 0)
157 break;
158 if (ix < VEC_length (const_char_ptr, excludes)
159 || current->excludes[ix] != NULL)
160 continue;
161
70c622a3
JK
162 /* Parameters exactly match CURRENT. */
163 return;
9219021c
DC
164 }
165
32019081
JK
166 new = obstack_alloc (obstack, (sizeof (*new)
167 + (VEC_length (const_char_ptr, excludes)
168 * sizeof (*new->excludes))));
169 memset (new, 0, sizeof (*new));
c0cc3a76 170
12aaed36
TT
171 if (copy_names)
172 {
173 new->import_src = obstack_copy0 (obstack, src, strlen (src));
174 new->import_dest = obstack_copy0 (obstack, dest, strlen (dest));
175 }
176 else
177 {
178 new->import_src = src;
179 new->import_dest = dest;
180 }
794684b6 181
12aaed36 182 if (alias != NULL && copy_names)
10f0c4bb 183 new->alias = obstack_copy0 (obstack, alias, strlen (alias));
12aaed36
TT
184 else
185 new->alias = alias;
c0cc3a76 186
12aaed36 187 if (declaration != NULL && copy_names)
10f0c4bb
TT
188 new->declaration = obstack_copy0 (obstack,
189 declaration, strlen (declaration));
12aaed36
TT
190 else
191 new->declaration = declaration;
13387711 192
32019081
JK
193 memcpy (new->excludes, VEC_address (const_char_ptr, excludes),
194 VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes));
195 new->excludes[VEC_length (const_char_ptr, excludes)] = NULL;
196
c0cc3a76
SW
197 new->next = using_directives;
198 using_directives = new;
9219021c
DC
199}
200
9219021c
DC
201/* Test whether or not NAMESPACE looks like it mentions an anonymous
202 namespace; return nonzero if so. */
203
204int
59da4d04 205cp_is_in_anonymous (const char *symbol_name)
9219021c 206{
59da4d04 207 return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR)
9219021c
DC
208 != NULL);
209}
210
6f27419a 211/* Look up NAME in DOMAIN in BLOCK's static block and in global blocks.
d5ff0482 212 If IS_IN_ANONYMOUS is nonzero, the symbol in question is located
6f27419a 213 within an anonymous namespace. */
34ef8452
DE
214
215static struct symbol *
6f27419a 216cp_basic_lookup_symbol (const char *name, const struct block *block,
d5ff0482 217 const domain_enum domain, int is_in_anonymous)
34ef8452 218{
6f27419a 219 struct symbol *sym;
34ef8452
DE
220
221 sym = lookup_symbol_in_static_block (name, block, domain);
222 if (sym != NULL)
223 return sym;
224
d5ff0482 225 if (is_in_anonymous)
34ef8452
DE
226 {
227 /* Symbols defined in anonymous namespaces have external linkage
228 but should be treated as local to a single file nonetheless.
229 So we only search the current file's global block. */
230
231 const struct block *global_block = block_global_block (block);
791244be 232
34ef8452
DE
233 if (global_block != NULL)
234 sym = lookup_symbol_in_block (name, global_block, domain);
235 }
236 else
237 {
238 sym = lookup_global_symbol (name, block, domain);
239 }
240
6f27419a
DE
241 return sym;
242}
243
244/* Search bare symbol NAME in DOMAIN in BLOCK.
1994afbf
DE
245 NAME is guaranteed to not have any scope (no "::") in its name, though
246 if for example NAME is a template spec then "::" may appear in the
247 argument list.
248 If LANGDEF is non-NULL then try to lookup NAME as a primitive type in
249 that language. Normally we wouldn't need LANGDEF but fortran also uses
250 this code.
6f27419a
DE
251 If SEARCH is non-zero then see if we can determine "this" from BLOCK, and
252 if so then also search for NAME in that class. */
253
254static struct symbol *
1994afbf
DE
255cp_lookup_bare_symbol (const struct language_defn *langdef,
256 const char *name, const struct block *block,
6f27419a
DE
257 const domain_enum domain, int search)
258{
259 struct symbol *sym;
260
261 /* Note: We can't do a simple assert for ':' not being in NAME because
262 ':' may be in the args of a template spec. This isn't intended to be
263 a complete test, just cheap and documentary. */
264 if (strchr (name, '<') == NULL && strchr (name, '(') == NULL)
265 gdb_assert (strchr (name, ':') == NULL);
266
267 sym = lookup_symbol_in_static_block (name, block, domain);
268 if (sym != NULL)
269 return sym;
270
1994afbf
DE
271 /* If we didn't find a definition for a builtin type in the static block,
272 search for it now. This is actually the right thing to do and can be
273 a massive performance win. E.g., when debugging a program with lots of
274 shared libraries we could search all of them only to find out the
275 builtin type isn't defined in any of them. This is common for types
276 like "void". */
277 if (langdef != NULL && domain == VAR_DOMAIN)
278 {
279 struct gdbarch *gdbarch;
280
281 if (block == NULL)
282 gdbarch = target_gdbarch ();
283 else
284 gdbarch = block_gdbarch (block);
285 sym = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
286 if (sym != NULL)
287 return sym;
288 }
289
6f27419a 290 sym = lookup_global_symbol (name, block, domain);
34ef8452
DE
291 if (sym != NULL)
292 return sym;
293
294 if (search)
295 {
6f27419a
DE
296 struct symbol *this;
297 struct type *type;
34ef8452 298
6f27419a
DE
299 this = lookup_language_this (language_def (language_cplus), block);
300 if (this == NULL)
301 return NULL;
34ef8452 302
6f27419a
DE
303 type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this)));
304 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
305 This can happen for lambda functions compiled with clang++,
306 which outputs no name for the container class. */
307 if (TYPE_NAME (type) == NULL)
308 return NULL;
34ef8452 309
d5ff0482 310 /* Look for symbol NAME in this class. */
6f27419a
DE
311 sym = cp_lookup_nested_symbol (type, name, block);
312 }
34ef8452 313
6f27419a
DE
314 return sym;
315}
34ef8452 316
6f27419a
DE
317/* Search NAME in DOMAIN in all static blocks, and then in all baseclasses.
318 BLOCK specifies the context in which to perform the search.
319 NAME is guaranteed to have scope (contain "::") and PREFIX_LEN specifies
d5ff0482 320 the length of the entire scope of NAME (up to, but not including, the last
6f27419a 321 "::".
34ef8452 322
6f27419a
DE
323 Note: At least in the case of Fortran, which also uses this code, there
324 may be no text after the last "::". */
34ef8452 325
6f27419a
DE
326static struct symbol *
327cp_search_static_and_baseclasses (const char *name,
328 const struct block *block,
329 const domain_enum domain,
330 unsigned int prefix_len)
331{
332 struct symbol *sym;
333 char *klass, *nested;
334 struct cleanup *cleanup;
335 struct symbol *klass_sym;
336 struct type *klass_type;
34ef8452 337
6f27419a
DE
338 /* The test here uses <= instead of < because Fortran also uses this,
339 and the module.exp testcase will pass "modmany::" for NAME here. */
340 gdb_assert (prefix_len + 2 <= strlen (name));
341 gdb_assert (name[prefix_len + 1] == ':');
34ef8452 342
6f27419a 343 /* Find the name of the class and the name of the method, variable, etc. */
34ef8452 344
6f27419a
DE
345 /* The class name is everything up to and including PREFIX_LEN. */
346 klass = savestring (name, prefix_len);
347
348 /* The rest of the name is everything else past the initial scope
349 operator. */
350 nested = xstrdup (name + prefix_len + 2);
351
352 /* Add cleanups to free memory for these strings. */
353 cleanup = make_cleanup (xfree, klass);
354 make_cleanup (xfree, nested);
355
356 /* Lookup a class named KLASS. If none is found, there is nothing
357 more that can be done. */
358 klass_sym = lookup_global_symbol (klass, block, domain);
359 if (klass_sym == NULL)
360 {
34ef8452 361 do_cleanups (cleanup);
6f27419a 362 return NULL;
34ef8452 363 }
6f27419a
DE
364 klass_type = SYMBOL_TYPE (klass_sym);
365
366 /* Look for a symbol named NESTED in this class.
367 The caller is assumed to have already have done a basic lookup of NAME.
368 So we pass zero for BASIC_LOOKUP to cp_lookup_nested_symbol_1 here. */
369 sym = cp_lookup_nested_symbol_1 (klass_type, nested, name, block, 0);
34ef8452 370
6f27419a 371 do_cleanups (cleanup);
34ef8452
DE
372 return sym;
373}
374
aff410f1 375/* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
8dea366b 376 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
6f27419a
DE
377 through base classes for a matching symbol.
378
379 Note: Part of the complexity is because NAME may itself specify scope.
380 Part of the complexity is also because this handles the case where
381 there is no scoping in which case we also try looking in the class of
382 "this" if we can compute it. */
8540c487
SW
383
384static struct symbol *
6f27419a
DE
385cp_lookup_symbol_in_namespace (const char *namespace, const char *name,
386 const struct block *block,
387 const domain_enum domain, int search)
8540c487 388{
d2763117 389 char *concatenated_name = NULL;
6f27419a
DE
390 int is_in_anonymous;
391 unsigned int prefix_len;
392 struct symbol *sym;
c5504eaf 393
d2763117
DE
394 if (namespace[0] != '\0')
395 {
396 concatenated_name = alloca (strlen (namespace) + 2
397 + strlen (name) + 1);
8540c487
SW
398 strcpy (concatenated_name, namespace);
399 strcat (concatenated_name, "::");
400 strcat (concatenated_name, name);
d2763117 401 name = concatenated_name;
8540c487 402 }
d2763117 403
6f27419a
DE
404 prefix_len = cp_entire_prefix_len (name);
405 if (prefix_len == 0)
1994afbf 406 return cp_lookup_bare_symbol (NULL, name, block, domain, search);
6f27419a
DE
407
408 /* This would be simpler if we just called cp_lookup_nested_symbol
409 at this point. But that would require first looking up the containing
410 class/namespace. Since we're only searching static and global blocks
411 there's often no need to first do that lookup. */
412
413 is_in_anonymous = namespace[0] != '\0' && cp_is_in_anonymous (namespace);
414 sym = cp_basic_lookup_symbol (name, block, domain, is_in_anonymous);
415 if (sym != NULL)
416 return sym;
417
418 if (search)
419 sym = cp_search_static_and_baseclasses (name, block, domain, prefix_len);
420
421 return sym;
8540c487
SW
422}
423
d5ff0482 424/* Used for cleanups to reset the "searched" flag in case of an error. */
b14e635e
SW
425
426static void
427reset_directive_searched (void *data)
428{
429 struct using_direct *direct = data;
430 direct->searched = 0;
431}
432
aff410f1
MS
433/* Search for NAME by applying all import statements belonging to
434 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
435 search is restricted to using declarations.
13387711
SW
436 Example:
437
aff410f1 438 namespace A {
13387711
SW
439 int x;
440 }
441 using A::x;
442
aff410f1
MS
443 If SEARCH_PARENTS the search will include imports which are
444 applicable in parents of SCOPE.
b14e635e
SW
445 Example:
446
aff410f1 447 namespace A {
b14e635e 448 using namespace X;
aff410f1 449 namespace B {
b14e635e
SW
450 using namespace Y;
451 }
452 }
453
aff410f1
MS
454 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
455 namespaces X and Y will be considered. If SEARCH_PARENTS is false
4bd0864e
DE
456 only the import of Y is considered.
457
458 SEARCH_SCOPE_FIRST is an internal implementation detail: Callers must
459 pass 0 for it. Internally we pass 1 when recursing. */
8540c487 460
9a80057a 461static struct symbol *
55accf4a
DE
462cp_lookup_symbol_via_imports (const char *scope,
463 const char *name,
464 const struct block *block,
465 const domain_enum domain,
4bd0864e 466 const int search_scope_first,
55accf4a
DE
467 const int declaration_only,
468 const int search_parents)
8540c487 469{
b14e635e 470 struct using_direct *current;
13387711 471 struct symbol *sym = NULL;
8540c487 472 int len;
b14e635e
SW
473 int directive_match;
474 struct cleanup *searched_cleanup;
8540c487 475
4bd0864e
DE
476 /* First, try to find the symbol in the given namespace if requested. */
477 if (search_scope_first)
aff410f1 478 sym = cp_lookup_symbol_in_namespace (scope, name,
8dea366b 479 block, domain, 1);
791244be 480
8540c487
SW
481 if (sym != NULL)
482 return sym;
483
aff410f1
MS
484 /* Go through the using directives. If any of them add new names to
485 the namespace we're searching in, see if we can find a match by
486 applying them. */
8540c487
SW
487
488 for (current = block_using (block);
489 current != NULL;
490 current = current->next)
491 {
32019081
JK
492 const char **excludep;
493
b14e635e
SW
494 len = strlen (current->import_dest);
495 directive_match = (search_parents
496 ? (strncmp (scope, current->import_dest,
497 strlen (current->import_dest)) == 0
498 && (len == 0
aff410f1
MS
499 || scope[len] == ':'
500 || scope[len] == '\0'))
b14e635e 501 : strcmp (scope, current->import_dest) == 0);
8540c487 502
aff410f1
MS
503 /* If the import destination is the current scope or one of its
504 ancestors then it is applicable. */
b14e635e 505 if (directive_match && !current->searched)
8540c487 506 {
a1d705ee
TT
507 /* Mark this import as searched so that the recursive call
508 does not search it again. */
509 current->searched = 1;
510 searched_cleanup = make_cleanup (reset_directive_searched,
511 current);
512
513 /* If there is an import of a single declaration, compare the
514 imported declaration (after optional renaming by its alias)
515 with the sought out name. If there is a match pass
516 current->import_src as NAMESPACE to direct the search
517 towards the imported namespace. */
518 if (current->declaration
519 && strcmp (name, current->alias
520 ? current->alias : current->declaration) == 0)
521 sym = cp_lookup_symbol_in_namespace (current->import_src,
522 current->declaration,
8dea366b 523 block, domain, 1);
a1d705ee
TT
524
525 /* If this is a DECLARATION_ONLY search or a symbol was found
526 or this import statement was an import declaration, the
527 search of this import is complete. */
528 if (declaration_only || sym != NULL || current->declaration)
529 {
530 current->searched = 0;
531 discard_cleanups (searched_cleanup);
532
533 if (sym != NULL)
534 return sym;
535
536 continue;
537 }
538
539 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
540 for (excludep = current->excludes; *excludep; excludep++)
541 if (strcmp (name, *excludep) == 0)
542 break;
543 if (*excludep)
544 {
545 discard_cleanups (searched_cleanup);
546 continue;
547 }
548
549 if (current->alias != NULL
550 && strcmp (name, current->alias) == 0)
551 /* If the import is creating an alias and the alias matches
552 the sought name. Pass current->import_src as the NAME to
553 direct the search towards the aliased namespace. */
554 {
555 sym = cp_lookup_symbol_in_namespace (scope,
556 current->import_src,
8dea366b 557 block, domain, 1);
a1d705ee
TT
558 }
559 else if (current->alias == NULL)
560 {
561 /* If this import statement creates no alias, pass
562 current->inner as NAMESPACE to direct the search
563 towards the imported namespace. */
55accf4a
DE
564 sym = cp_lookup_symbol_via_imports (current->import_src,
565 name, block,
4bd0864e 566 domain, 1, 0, 0);
a1d705ee
TT
567 }
568 current->searched = 0;
569 discard_cleanups (searched_cleanup);
570
571 if (sym != NULL)
572 return sym;
8540c487
SW
573 }
574 }
575
576 return NULL;
577}
578
d5ff0482 579/* Helper function that searches an array of symbols for one named NAME. */
34eaf542
TT
580
581static struct symbol *
aff410f1
MS
582search_symbol_list (const char *name, int num,
583 struct symbol **syms)
34eaf542
TT
584{
585 int i;
586
587 /* Maybe we should store a dictionary in here instead. */
588 for (i = 0; i < num; ++i)
589 {
590 if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
591 return syms[i];
592 }
593 return NULL;
594}
595
55accf4a 596/* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it
34eaf542
TT
597 searches through the template parameters of the function and the
598 function's type. */
599
600struct symbol *
601cp_lookup_symbol_imports_or_template (const char *scope,
602 const char *name,
603 const struct block *block,
604 const domain_enum domain)
605{
606 struct symbol *function = BLOCK_FUNCTION (block);
cc485e62
DE
607 struct symbol *result;
608
609 if (symbol_lookup_debug)
610 {
611 fprintf_unfiltered (gdb_stdlog,
612 "cp_lookup_symbol_imports_or_template"
613 " (%s, %s, %s, %s)\n",
614 scope, name, host_address_to_string (block),
615 domain_name (domain));
616 }
34eaf542
TT
617
618 if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
619 {
34eaf542
TT
620 /* Search the function's template parameters. */
621 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
622 {
791244be 623 struct template_symbol *templ
aff410f1 624 = (struct template_symbol *) function;
34eaf542
TT
625
626 result = search_symbol_list (name,
627 templ->n_template_arguments,
628 templ->template_arguments);
629 if (result != NULL)
cc485e62
DE
630 {
631 if (symbol_lookup_debug)
632 {
633 fprintf_unfiltered (gdb_stdlog,
634 "cp_lookup_symbol_imports_or_template"
635 " (...) = %s\n",
636 host_address_to_string (result));
637 }
638 return result;
639 }
34eaf542
TT
640 }
641
642 /* Search the template parameters of the function's defining
643 context. */
644 if (SYMBOL_NATURAL_NAME (function))
645 {
646 struct type *context;
647 char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
648 struct cleanup *cleanups = make_cleanup (xfree, name_copy);
649 const struct language_defn *lang = language_def (language_cplus);
08be3fe3 650 struct gdbarch *arch = symbol_arch (function);
34eaf542
TT
651 const struct block *parent = BLOCK_SUPERBLOCK (block);
652
653 while (1)
654 {
34eaf542
TT
655 unsigned int prefix_len = cp_entire_prefix_len (name_copy);
656
657 if (prefix_len == 0)
658 context = NULL;
659 else
660 {
661 name_copy[prefix_len] = '\0';
aff410f1
MS
662 context = lookup_typename (lang, arch,
663 name_copy,
664 parent, 1);
34eaf542
TT
665 }
666
667 if (context == NULL)
668 break;
669
aff410f1
MS
670 result
671 = search_symbol_list (name,
672 TYPE_N_TEMPLATE_ARGUMENTS (context),
673 TYPE_TEMPLATE_ARGUMENTS (context));
34eaf542 674 if (result != NULL)
c27e16e3
TT
675 {
676 do_cleanups (cleanups);
cc485e62
DE
677 if (symbol_lookup_debug)
678 {
679 fprintf_unfiltered (gdb_stdlog,
680 "cp_lookup_symbol_imports_or_template"
681 " (...) = %s\n",
682 host_address_to_string (result));
683 }
c27e16e3
TT
684 return result;
685 }
34eaf542
TT
686 }
687
688 do_cleanups (cleanups);
689 }
690 }
691
4bd0864e 692 result = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 1, 1);
cc485e62
DE
693 if (symbol_lookup_debug)
694 {
695 fprintf_unfiltered (gdb_stdlog,
696 "cp_lookup_symbol_imports_or_template (...) = %s\n",
697 result != NULL
698 ? host_address_to_string (result) : "NULL");
699 }
700 return result;
34eaf542
TT
701}
702
f953163f
DE
703/* Search for NAME by applying relevant import statements belonging to BLOCK
704 and its parents. SCOPE is the namespace scope of the context in which the
705 search is being evaluated. */
706
707static struct symbol *
708cp_lookup_symbol_via_all_imports (const char *scope, const char *name,
709 const struct block *block,
710 const domain_enum domain)
711{
712 struct symbol *sym;
713
714 while (block != NULL)
715 {
716 sym = cp_lookup_symbol_via_imports (scope, name, block, domain, 0, 0, 1);
717 if (sym)
718 return sym;
719
720 block = BLOCK_SUPERBLOCK (block);
721 }
722
723 return NULL;
724}
725
791244be
DE
726/* Searches for NAME in the current namespace, and by applying
727 relevant import statements belonging to BLOCK and its parents.
728 SCOPE is the namespace scope of the context in which the search is
729 being evaluated. */
8540c487 730
f953163f 731struct symbol *
8540c487
SW
732cp_lookup_symbol_namespace (const char *scope,
733 const char *name,
8540c487 734 const struct block *block,
13387711 735 const domain_enum domain)
8540c487
SW
736{
737 struct symbol *sym;
cc485e62
DE
738
739 if (symbol_lookup_debug)
740 {
741 fprintf_unfiltered (gdb_stdlog,
742 "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n",
743 scope, name, host_address_to_string (block),
744 domain_name (domain));
745 }
746
13387711 747 /* First, try to find the symbol in the given namespace. */
f953163f 748 sym = cp_lookup_symbol_in_namespace (scope, name, block, domain, 1);
8540c487 749
f953163f
DE
750 /* Search for name in namespaces imported to this and parent blocks. */
751 if (sym == NULL)
752 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
8540c487 753
cc485e62
DE
754 if (symbol_lookup_debug)
755 {
756 fprintf_unfiltered (gdb_stdlog,
f953163f
DE
757 "cp_lookup_symbol_namespace (...) = %s\n",
758 sym != NULL ? host_address_to_string (sym) : "NULL");
cc485e62 759 }
f953163f 760 return sym;
1fcb5155
DC
761}
762
763/* Lookup NAME at namespace scope (or, in C terms, in static and
764 global variables). SCOPE is the namespace that the current
765 function is defined within; only consider namespaces whose length
766 is at least SCOPE_LEN. Other arguments are as in
767 cp_lookup_symbol_nonlocal.
768
769 For example, if we're within a function A::B::f and looking for a
3882f37a 770 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
1fcb5155
DC
771 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
772 but with SCOPE_LEN = 1. And then it calls itself with NAME and
773 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
774 "A::B::x"; if it doesn't find it, then the second call looks for
775 "A::x", and if that call fails, then the first call looks for
776 "x". */
777
778static struct symbol *
1994afbf
DE
779lookup_namespace_scope (const struct language_defn *langdef,
780 const char *name,
1fcb5155
DC
781 const struct block *block,
782 const domain_enum domain,
1fcb5155
DC
783 const char *scope,
784 int scope_len)
785{
786 char *namespace;
787
788 if (scope[scope_len] != '\0')
789 {
790 /* Recursively search for names in child namespaces first. */
791
792 struct symbol *sym;
793 int new_scope_len = scope_len;
794
795 /* If the current scope is followed by "::", skip past that. */
796 if (new_scope_len != 0)
797 {
798 gdb_assert (scope[new_scope_len] == ':');
799 new_scope_len += 2;
800 }
801 new_scope_len += cp_find_first_component (scope + new_scope_len);
1994afbf 802 sym = lookup_namespace_scope (langdef, name, block, domain,
aff410f1 803 scope, new_scope_len);
1fcb5155
DC
804 if (sym != NULL)
805 return sym;
806 }
807
808 /* Okay, we didn't find a match in our children, so look for the
1994afbf
DE
809 name in the current namespace.
810
811 If we there is no scope and we know we have a bare symbol, then short
812 circuit everything and call cp_lookup_bare_symbol directly.
813 This isn't an optimization, rather it allows us to pass LANGDEF which
814 is needed for primitive type lookup. The test doesn't have to be
815 perfect: if NAME is a bare symbol that our test doesn't catch (e.g., a
816 template symbol with "::" in the argument list) then
817 cp_lookup_symbol_in_namespace will catch it. */
818
819 if (scope_len == 0 && strchr (name, ':') == NULL)
820 return cp_lookup_bare_symbol (langdef, name, block, domain, 1);
1fcb5155
DC
821
822 namespace = alloca (scope_len + 1);
823 strncpy (namespace, scope, scope_len);
824 namespace[scope_len] = '\0';
aff410f1 825 return cp_lookup_symbol_in_namespace (namespace, name,
8dea366b 826 block, domain, 1);
1fcb5155
DC
827}
828
56286edf
DE
829/* The C++-specific version of name lookup for static and global
830 names. This makes sure that names get looked for in all namespaces
831 that are in scope. NAME is the natural name of the symbol that
832 we're looking for, BLOCK is the block that we're searching within,
833 DOMAIN says what kind of symbols we're looking for. */
834
835struct symbol *
f606139a
DE
836cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
837 const char *name,
56286edf
DE
838 const struct block *block,
839 const domain_enum domain)
840{
841 struct symbol *sym;
842 const char *scope = block_scope (block);
843
cc485e62
DE
844 if (symbol_lookup_debug)
845 {
846 fprintf_unfiltered (gdb_stdlog,
847 "cp_lookup_symbol_non_local"
848 " (%s, %s (scope %s), %s)\n",
849 name, host_address_to_string (block), scope,
850 domain_name (domain));
851 }
852
f953163f
DE
853 /* First, try to find the symbol in the given namespace, and all
854 containing namespaces. */
1994afbf 855 sym = lookup_namespace_scope (langdef, name, block, domain, scope, 0);
56286edf 856
f953163f
DE
857 /* Search for name in namespaces imported to this and parent blocks. */
858 if (sym == NULL)
859 sym = cp_lookup_symbol_via_all_imports (scope, name, block, domain);
860
cc485e62
DE
861 if (symbol_lookup_debug)
862 {
863 fprintf_unfiltered (gdb_stdlog,
864 "cp_lookup_symbol_nonlocal (...) = %s\n",
865 sym != NULL ? host_address_to_string (sym) : "NULL");
866 }
867 return sym;
56286edf
DE
868}
869
f7e3ecae
KS
870/* Search through the base classes of PARENT_TYPE for a base class
871 named NAME and return its type. If not found, return NULL. */
872
873struct type *
a07e3e18 874cp_find_type_baseclass_by_name (struct type *parent_type, const char *name)
f7e3ecae
KS
875{
876 int i;
877
878 CHECK_TYPEDEF (parent_type);
879 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
880 {
881 struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
882 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
883
884 if (base_name == NULL)
885 continue;
886
887 if (streq (base_name, name))
888 return type;
889
a07e3e18 890 type = cp_find_type_baseclass_by_name (type, name);
f7e3ecae
KS
891 if (type != NULL)
892 return type;
893 }
894
895 return NULL;
896}
897
8dea366b
KS
898/* Search through the base classes of PARENT_TYPE for a symbol named
899 NAME in block BLOCK. */
900
901static struct symbol *
902find_symbol_in_baseclass (struct type *parent_type, const char *name,
903 const struct block *block)
904{
905 int i;
906 struct symbol *sym;
907 struct cleanup *cleanup;
908 char *concatenated_name;
909
910 sym = NULL;
911 concatenated_name = NULL;
912 cleanup = make_cleanup (free_current_contents, &concatenated_name);
791244be 913
8dea366b
KS
914 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
915 {
916 size_t len;
69fc87c2 917 struct type *base_type = TYPE_BASECLASS (parent_type, i);
8dea366b
KS
918 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
919
920 if (base_name == NULL)
921 continue;
922
8dea366b
KS
923 len = strlen (base_name) + 2 + strlen (name) + 1;
924 concatenated_name = xrealloc (concatenated_name, len);
925 xsnprintf (concatenated_name, len, "%s::%s", base_name, name);
8dea366b 926
6f27419a
DE
927 sym = cp_lookup_nested_symbol_1 (base_type, name, concatenated_name,
928 block, 1);
69fc87c2
DE
929 if (sym != NULL)
930 break;
8dea366b
KS
931 }
932
933 do_cleanups (cleanup);
0c2e6019 934 return sym;
5c4e30ca
DC
935}
936
6f27419a
DE
937/* Helper function to look up NESTED_NAME in CONTAINER_TYPE within the
938 context of BLOCK.
d5ff0482 939 NESTED_NAME may have scope ("::").
6f27419a
DE
940 CONTAINER_TYPE needn't have been "check_typedef'd" yet.
941 CONCATENATED_NAME is the fully scoped spelling of NESTED_NAME, it is
942 passed as an argument so that callers can control how space for it is
943 allocated.
944 If BASIC_LOOKUP is non-zero then perform a basic lookup of
945 CONCATENATED_NAME. See cp_basic_lookup_symbol for details. */
946
947static struct symbol *
948cp_lookup_nested_symbol_1 (struct type *container_type,
949 const char *nested_name,
950 const char *concatenated_name,
951 const struct block *block,
952 int basic_lookup)
953{
954 int is_in_anonymous = cp_is_in_anonymous (concatenated_name);
955 struct symbol *sym;
956
957 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
958 of classes like, say, data or function members. Instead,
959 they're just represented by symbols whose names are
960 qualified by the name of the surrounding class. This is
961 just like members of namespaces; in particular,
962 cp_basic_lookup_symbol works when looking them up. */
963
964 if (basic_lookup)
965 {
966 sym = cp_basic_lookup_symbol (concatenated_name, block, VAR_DOMAIN,
967 is_in_anonymous);
968 if (sym != NULL)
969 return sym;
970 }
971
972 /* Now search all static file-level symbols. We have to do this for things
973 like typedefs in the class. We do not try to guess any imported
974 namespace as even the fully specified namespace search is already not
975 C++ compliant and more assumptions could make it too magic. */
976
977 /* First search in this symtab, what we want is possibly there. */
978 sym = lookup_symbol_in_static_block (concatenated_name, block, VAR_DOMAIN);
979 if (sym != NULL)
980 return sym;
981
982 /* Nope. We now have to search all static blocks in all objfiles,
983 even if block != NULL, because there's no guarantees as to which
984 symtab the symbol we want is in. */
985 sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
986 if (sym != NULL)
987 return sym;
988
989 /* If this is a class with baseclasses, search them next. */
990 CHECK_TYPEDEF (container_type);
991 if (TYPE_N_BASECLASSES (container_type) > 0)
992 {
993 sym = find_symbol_in_baseclass (container_type, nested_name, block);
994 if (sym != NULL)
995 return sym;
996 }
997
998 return NULL;
999}
1000
50af5481 1001/* Look up a symbol named NESTED_NAME that is nested inside the C++
79c2c32d 1002 class or namespace given by PARENT_TYPE, from within the context
05a6c3c8 1003 given by BLOCK. Return NULL if there is no such nested symbol. */
79c2c32d 1004
50af5481
JK
1005struct symbol *
1006cp_lookup_nested_symbol (struct type *parent_type,
1007 const char *nested_name,
1008 const struct block *block)
79c2c32d 1009{
05a6c3c8 1010 /* type_name_no_tag_or_error provides better error reporting using the
d8228535
JK
1011 original type. */
1012 struct type *saved_parent_type = parent_type;
1013
1014 CHECK_TYPEDEF (parent_type);
1015
cc485e62
DE
1016 if (symbol_lookup_debug)
1017 {
1018 const char *type_name = type_name_no_tag (saved_parent_type);
1019
1020 fprintf_unfiltered (gdb_stdlog,
1021 "cp_lookup_nested_symbol (%s, %s, %s)\n",
1022 type_name != NULL ? type_name : "unnamed",
1023 nested_name, host_address_to_string (block));
1024 }
1025
79c2c32d
DC
1026 switch (TYPE_CODE (parent_type))
1027 {
63d06c5c 1028 case TYPE_CODE_STRUCT:
79c2c32d 1029 case TYPE_CODE_NAMESPACE:
48e32051 1030 case TYPE_CODE_UNION:
3d567982 1031 case TYPE_CODE_ENUM:
530e8392
KB
1032 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
1033 specific code to lookup nested symbols in modules, by calling the
1034 function pointer la_lookup_symbol_nonlocal, which ends up here. */
1035 case TYPE_CODE_MODULE:
79c2c32d 1036 {
08850b56 1037 int size;
d8228535 1038 const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
6f27419a 1039 struct symbol *sym;
41f62f39 1040 char *concatenated_name;
c5504eaf 1041
08850b56
PM
1042 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
1043 concatenated_name = alloca (size);
1044 xsnprintf (concatenated_name, size, "%s::%s",
6f27419a
DE
1045 parent_name, nested_name);
1046
1047 sym = cp_lookup_nested_symbol_1 (parent_type, nested_name,
1048 concatenated_name, block, 1);
41f62f39 1049
cc485e62
DE
1050 if (symbol_lookup_debug)
1051 {
1052 fprintf_unfiltered (gdb_stdlog,
1053 "cp_lookup_nested_symbol (...) = %s\n",
1054 sym != NULL
1055 ? host_address_to_string (sym) : "NULL");
1056 }
1057 return sym;
79c2c32d 1058 }
bb869963
SDJ
1059
1060 case TYPE_CODE_FUNC:
1061 case TYPE_CODE_METHOD:
cc485e62
DE
1062 if (symbol_lookup_debug)
1063 {
1064 fprintf_unfiltered (gdb_stdlog,
1065 "cp_lookup_nested_symbol (...) = NULL"
1066 " (func/method)\n");
1067 }
bb869963
SDJ
1068 return NULL;
1069
79c2c32d
DC
1070 default:
1071 internal_error (__FILE__, __LINE__,
50af5481 1072 _("cp_lookup_nested_symbol called "
3e43a32a 1073 "on a non-aggregate type."));
79c2c32d
DC
1074 }
1075}
1076
b368761e
DC
1077/* The C++-version of lookup_transparent_type. */
1078
1079/* FIXME: carlton/2004-01-16: The problem that this is trying to
1080 address is that, unfortunately, sometimes NAME is wrong: it may not
1081 include the name of namespaces enclosing the type in question.
b021a221 1082 lookup_transparent_type gets called when the type in question
b368761e
DC
1083 is a declaration, and we're trying to find its definition; but, for
1084 declarations, our type name deduction mechanism doesn't work.
1085 There's nothing we can do to fix this in general, I think, in the
1086 absence of debug information about namespaces (I've filed PR
1087 gdb/1511 about this); until such debug information becomes more
1088 prevalent, one heuristic which sometimes looks is to search for the
1089 definition in namespaces containing the current namespace.
1090
1091 We should delete this functions once the appropriate debug
1092 information becomes more widespread. (GCC 3.4 will be the first
1093 released version of GCC with such information.) */
1094
1095struct type *
1096cp_lookup_transparent_type (const char *name)
1097{
1098 /* First, try the honest way of looking up the definition. */
1099 struct type *t = basic_lookup_transparent_type (name);
1100 const char *scope;
1101
1102 if (t != NULL)
1103 return t;
1104
1105 /* If that doesn't work and we're within a namespace, look there
1106 instead. */
1107 scope = block_scope (get_selected_block (0));
1108
1109 if (scope[0] == '\0')
1110 return NULL;
1111
1112 return cp_lookup_transparent_type_loop (name, scope, 0);
1113}
1114
b021a221
MS
1115/* Lookup the type definition associated to NAME in namespaces/classes
1116 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
1117 must be the index of the start of a component of SCOPE. */
b368761e
DC
1118
1119static struct type *
aff410f1
MS
1120cp_lookup_transparent_type_loop (const char *name,
1121 const char *scope,
b368761e
DC
1122 int length)
1123{
1198ecbe 1124 int scope_length = length + cp_find_first_component (scope + length);
b368761e
DC
1125 char *full_name;
1126
1127 /* If the current scope is followed by "::", look in the next
1128 component. */
1129 if (scope[scope_length] == ':')
1130 {
1131 struct type *retval
aff410f1
MS
1132 = cp_lookup_transparent_type_loop (name, scope,
1133 scope_length + 2);
c5504eaf 1134
b368761e
DC
1135 if (retval != NULL)
1136 return retval;
1137 }
1138
1139 full_name = alloca (scope_length + 2 + strlen (name) + 1);
1140 strncpy (full_name, scope, scope_length);
1141 strncpy (full_name + scope_length, "::", 2);
1142 strcpy (full_name + scope_length + 2, name);
1143
1144 return basic_lookup_transparent_type (full_name);
1145}
1146
0c2e6019
TT
1147/* This used to do something but was removed when it became
1148 obsolete. */
5c4e30ca
DC
1149
1150static void
1151maintenance_cplus_namespace (char *args, int from_tty)
1152{
0c2e6019 1153 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
5c4e30ca
DC
1154}
1155
2c0b251b
PA
1156/* Provide a prototype to silence -Wmissing-prototypes. */
1157extern initialize_file_ftype _initialize_cp_namespace;
1158
5c4e30ca
DC
1159void
1160_initialize_cp_namespace (void)
1161{
0c2e6019
TT
1162 struct cmd_list_element *cmd;
1163
1164 cmd = add_cmd ("namespace", class_maintenance,
1165 maintenance_cplus_namespace,
1166 _("Deprecated placeholder for removed functionality."),
1167 &maint_cplus_cmd_list);
1168 deprecate_cmd (cmd, NULL);
1fcb5155 1169}
This page took 1.115339 seconds and 4 git commands to generate.