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