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