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