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