Add AVX512 registers support to GDB and GDBserver.
[deliverable/binutils-gdb.git] / gdb / cp-namespace.c
CommitLineData
9219021c 1/* Helper routines for C++ support in GDB.
ecd75fc8 2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
9219021c 3
1fcb5155 4 Contributed by David Carlton and by Kealia, Inc.
9219021c
DC
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
9219021c
DC
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
9219021c
DC
20
21#include "defs.h"
22#include "cp-support.h"
23#include "gdb_obstack.h"
24#include "symtab.h"
25#include "symfile.h"
26#include "gdb_assert.h"
27#include "block.h"
5c4e30ca
DC
28#include "objfiles.h"
29#include "gdbtypes.h"
30#include "dictionary.h"
31#include "command.h"
b368761e 32#include "frame.h"
27aa8d6a 33#include "buildsym.h"
34eaf542 34#include "language.h"
9219021c 35
1fcb5155 36static struct symbol *lookup_namespace_scope (const char *name,
1fcb5155
DC
37 const struct block *block,
38 const domain_enum domain,
1fcb5155
DC
39 const char *scope,
40 int scope_len);
41
42static struct symbol *lookup_symbol_file (const char *name,
1fcb5155
DC
43 const struct block *block,
44 const domain_enum domain,
8dea366b
KS
45 int anonymous_namespace,
46 int search);
1fcb5155 47
b368761e
DC
48static struct type *cp_lookup_transparent_type_loop (const char *name,
49 const char *scope,
50 int scope_len);
51
9219021c
DC
52/* Check to see if SYMBOL refers to an object contained within an
53 anonymous namespace; if so, add an appropriate using directive. */
54
9219021c 55void
a10964d1
AR
56cp_scan_for_anonymous_namespaces (const struct symbol *const symbol,
57 struct objfile *const objfile)
9219021c 58{
df8a16a1 59 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL)
9219021c 60 {
df8a16a1 61 const char *name = SYMBOL_DEMANGLED_NAME (symbol);
9219021c
DC
62 unsigned int previous_component;
63 unsigned int next_component;
9219021c
DC
64
65 /* Start with a quick-and-dirty check for mention of "(anonymous
66 namespace)". */
67
68 if (!cp_is_anonymous (name))
69 return;
70
71 previous_component = 0;
72 next_component = cp_find_first_component (name + previous_component);
73
74 while (name[next_component] == ':')
75 {
2b1dbab0
KS
76 if (((next_component - previous_component)
77 == CP_ANONYMOUS_NAMESPACE_LEN)
9219021c 78 && strncmp (name + previous_component,
2b1dbab0
KS
79 CP_ANONYMOUS_NAMESPACE_STR,
80 CP_ANONYMOUS_NAMESPACE_LEN) == 0)
9219021c 81 {
aff410f1
MS
82 int dest_len = (previous_component == 0
83 ? 0 : previous_component - 2);
8c902bb1 84 int src_len = next_component;
794684b6 85
8c902bb1
SW
86 char *dest = alloca (dest_len + 1);
87 char *src = alloca (src_len + 1);
794684b6 88
8c902bb1
SW
89 memcpy (dest, name, dest_len);
90 memcpy (src, name, src_len);
794684b6 91
8c902bb1
SW
92 dest[dest_len] = '\0';
93 src[src_len] = '\0';
794684b6 94
9219021c
DC
95 /* We've found a component of the name that's an
96 anonymous namespace. So add symbols in it to the
97 namespace given by the previous component if there is
98 one, or to the global namespace if there isn't. */
12aaed36 99 cp_add_using_directive (dest, src, NULL, NULL, NULL, 1,
a10964d1 100 &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
c0cc3a76 111
aff410f1
MS
112/* Add a using directive to using_directives. If the using directive
113 in question has already been added, don't add it twice.
114
115 Create a new struct using_direct which imports the namespace SRC
116 into the scope DEST. ALIAS is the name of the imported namespace
117 in the current scope. If ALIAS is NULL then the namespace is known
118 by its original name. DECLARATION is the name if the imported
119 varable if this is a declaration import (Eg. using A::x), otherwise
12aaed36
TT
120 it is NULL. EXCLUDES is a list of names not to import from an
121 imported module or NULL. If COPY_NAMES is non-zero, then the
122 arguments are copied into newly allocated memory so they can be
123 temporaries. For EXCLUDES the VEC pointers are copied but the
32019081 124 pointed to characters are not copied. */
9219021c
DC
125
126void
13387711
SW
127cp_add_using_directive (const char *dest,
128 const char *src,
129 const char *alias,
130 const char *declaration,
32019081 131 VEC (const_char_ptr) *excludes,
12aaed36 132 int copy_names,
c0cc3a76 133 struct obstack *obstack)
9219021c
DC
134{
135 struct using_direct *current;
136 struct using_direct *new;
13387711 137
9219021c
DC
138 /* Has it already been added? */
139
27aa8d6a 140 for (current = using_directives; current != NULL; current = current->next)
9219021c 141 {
32019081
JK
142 int ix;
143 const char *param;
144
70c622a3
JK
145 if (strcmp (current->import_src, src) != 0)
146 continue;
147 if (strcmp (current->import_dest, dest) != 0)
148 continue;
149 if ((alias == NULL && current->alias != NULL)
150 || (alias != NULL && current->alias == NULL)
151 || (alias != NULL && current->alias != NULL
152 && strcmp (alias, current->alias) != 0))
153 continue;
154 if ((declaration == NULL && current->declaration != NULL)
155 || (declaration != NULL && current->declaration == NULL)
156 || (declaration != NULL && current->declaration != NULL
157 && strcmp (declaration, current->declaration) != 0))
158 continue;
159
32019081
JK
160 /* Compare the contents of EXCLUDES. */
161 for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++)
162 if (current->excludes[ix] == NULL
163 || strcmp (param, current->excludes[ix]) != 0)
164 break;
165 if (ix < VEC_length (const_char_ptr, excludes)
166 || current->excludes[ix] != NULL)
167 continue;
168
70c622a3
JK
169 /* Parameters exactly match CURRENT. */
170 return;
9219021c
DC
171 }
172
32019081
JK
173 new = obstack_alloc (obstack, (sizeof (*new)
174 + (VEC_length (const_char_ptr, excludes)
175 * sizeof (*new->excludes))));
176 memset (new, 0, sizeof (*new));
c0cc3a76 177
12aaed36
TT
178 if (copy_names)
179 {
180 new->import_src = obstack_copy0 (obstack, src, strlen (src));
181 new->import_dest = obstack_copy0 (obstack, dest, strlen (dest));
182 }
183 else
184 {
185 new->import_src = src;
186 new->import_dest = dest;
187 }
794684b6 188
12aaed36 189 if (alias != NULL && copy_names)
10f0c4bb 190 new->alias = obstack_copy0 (obstack, alias, strlen (alias));
12aaed36
TT
191 else
192 new->alias = alias;
c0cc3a76 193
12aaed36 194 if (declaration != NULL && copy_names)
10f0c4bb
TT
195 new->declaration = obstack_copy0 (obstack,
196 declaration, strlen (declaration));
12aaed36
TT
197 else
198 new->declaration = declaration;
13387711 199
32019081
JK
200 memcpy (new->excludes, VEC_address (const_char_ptr, excludes),
201 VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes));
202 new->excludes[VEC_length (const_char_ptr, excludes)] = NULL;
203
c0cc3a76
SW
204 new->next = using_directives;
205 using_directives = new;
9219021c
DC
206}
207
9219021c
DC
208/* Test whether or not NAMESPACE looks like it mentions an anonymous
209 namespace; return nonzero if so. */
210
211int
212cp_is_anonymous (const char *namespace)
213{
2b1dbab0 214 return (strstr (namespace, CP_ANONYMOUS_NAMESPACE_STR)
9219021c
DC
215 != NULL);
216}
217
1fcb5155
DC
218/* The C++-specific version of name lookup for static and global
219 names. This makes sure that names get looked for in all namespaces
220 that are in scope. NAME is the natural name of the symbol that
94af9270 221 we're looking for, BLOCK is the block that we're searching within,
aff410f1
MS
222 DOMAIN says what kind of symbols we're looking for, and if SYMTAB
223 is non-NULL, we should store the symtab where we found the symbol
b50c8614
KS
224 in it.
225
226 Class, union, and enum tag names may be used in C++ without specifying
227 class-key or enum. If searching for a VAR_DOMAIN symbol fails,
228 this function will search STRUCT_DOMAIN. [This is actually done in
229 cp_lookup_symbol_in_namespace.] */
1fcb5155
DC
230
231struct symbol *
232cp_lookup_symbol_nonlocal (const char *name,
1fcb5155 233 const struct block *block,
21b556f4 234 const domain_enum domain)
1fcb5155 235{
8540c487
SW
236 struct symbol *sym;
237 const char *scope = block_scope (block);
238
aff410f1
MS
239 sym = lookup_namespace_scope (name, block,
240 domain, scope, 0);
8540c487
SW
241 if (sym != NULL)
242 return sym;
243
aff410f1
MS
244 return cp_lookup_symbol_namespace (scope, name,
245 block, domain);
8540c487
SW
246}
247
aff410f1 248/* Look up NAME in the C++ namespace NAMESPACE. Other arguments are
8dea366b 249 as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search
b50c8614
KS
250 through base classes for a matching symbol.
251
252 If DOMAIN is VAR_DOMAIN and no matching symbol exists in that domain,
253 this function will search STRUCT_DOMAIN for a match. */
8540c487
SW
254
255static struct symbol *
256cp_lookup_symbol_in_namespace (const char *namespace,
257 const char *name,
8540c487 258 const struct block *block,
8dea366b 259 const domain_enum domain, int search)
8540c487
SW
260{
261 if (namespace[0] == '\0')
262 {
b50c8614
KS
263 struct symbol *sym = lookup_symbol_file (name, block, domain, 0, search);
264
265 if (sym == NULL && domain == VAR_DOMAIN)
266 sym = lookup_symbol_file (name, block, STRUCT_DOMAIN, 0, search);
267
268 return sym;
8540c487
SW
269 }
270 else
271 {
b50c8614 272 struct symbol *sym;
aff410f1
MS
273 char *concatenated_name = alloca (strlen (namespace) + 2
274 + strlen (name) + 1);
c5504eaf 275
8540c487
SW
276 strcpy (concatenated_name, namespace);
277 strcat (concatenated_name, "::");
278 strcat (concatenated_name, name);
b50c8614
KS
279 sym = lookup_symbol_file (concatenated_name, block, domain,
280 cp_is_anonymous (namespace), search);
281
282 if (sym == NULL && domain == VAR_DOMAIN)
283 sym = lookup_symbol_file (concatenated_name, block, STRUCT_DOMAIN,
284 cp_is_anonymous (namespace), search);
285
286 return sym;
8540c487
SW
287 }
288}
289
b14e635e
SW
290/* Used for cleanups to reset the "searched" flag incase
291 of an error. */
292
293static void
294reset_directive_searched (void *data)
295{
296 struct using_direct *direct = data;
297 direct->searched = 0;
298}
299
aff410f1
MS
300/* Search for NAME by applying all import statements belonging to
301 BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the
302 search is restricted to using declarations.
13387711
SW
303 Example:
304
aff410f1 305 namespace A {
13387711
SW
306 int x;
307 }
308 using A::x;
309
aff410f1
MS
310 If SEARCH_PARENTS the search will include imports which are
311 applicable in parents of SCOPE.
b14e635e
SW
312 Example:
313
aff410f1 314 namespace A {
b14e635e 315 using namespace X;
aff410f1 316 namespace B {
b14e635e
SW
317 using namespace Y;
318 }
319 }
320
aff410f1
MS
321 If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of
322 namespaces X and Y will be considered. If SEARCH_PARENTS is false
323 only the import of Y is considered. */
8540c487 324
13387711 325struct symbol *
8540c487
SW
326cp_lookup_symbol_imports (const char *scope,
327 const char *name,
8540c487 328 const struct block *block,
b14e635e 329 const domain_enum domain,
13387711 330 const int declaration_only,
b14e635e 331 const int search_parents)
8540c487 332{
b14e635e 333 struct using_direct *current;
13387711 334 struct symbol *sym = NULL;
8540c487 335 int len;
b14e635e
SW
336 int directive_match;
337 struct cleanup *searched_cleanup;
8540c487
SW
338
339 /* First, try to find the symbol in the given namespace. */
13387711 340 if (!declaration_only)
aff410f1 341 sym = cp_lookup_symbol_in_namespace (scope, name,
8dea366b 342 block, domain, 1);
13387711 343
8540c487
SW
344 if (sym != NULL)
345 return sym;
346
aff410f1
MS
347 /* Go through the using directives. If any of them add new names to
348 the namespace we're searching in, see if we can find a match by
349 applying them. */
8540c487
SW
350
351 for (current = block_using (block);
352 current != NULL;
353 current = current->next)
354 {
32019081
JK
355 const char **excludep;
356
b14e635e
SW
357 len = strlen (current->import_dest);
358 directive_match = (search_parents
359 ? (strncmp (scope, current->import_dest,
360 strlen (current->import_dest)) == 0
361 && (len == 0
aff410f1
MS
362 || scope[len] == ':'
363 || scope[len] == '\0'))
b14e635e 364 : strcmp (scope, current->import_dest) == 0);
8540c487 365
aff410f1
MS
366 /* If the import destination is the current scope or one of its
367 ancestors then it is applicable. */
b14e635e 368 if (directive_match && !current->searched)
8540c487 369 {
a1d705ee
TT
370 /* Mark this import as searched so that the recursive call
371 does not search it again. */
372 current->searched = 1;
373 searched_cleanup = make_cleanup (reset_directive_searched,
374 current);
375
376 /* If there is an import of a single declaration, compare the
377 imported declaration (after optional renaming by its alias)
378 with the sought out name. If there is a match pass
379 current->import_src as NAMESPACE to direct the search
380 towards the imported namespace. */
381 if (current->declaration
382 && strcmp (name, current->alias
383 ? current->alias : current->declaration) == 0)
384 sym = cp_lookup_symbol_in_namespace (current->import_src,
385 current->declaration,
8dea366b 386 block, domain, 1);
a1d705ee
TT
387
388 /* If this is a DECLARATION_ONLY search or a symbol was found
389 or this import statement was an import declaration, the
390 search of this import is complete. */
391 if (declaration_only || sym != NULL || current->declaration)
392 {
393 current->searched = 0;
394 discard_cleanups (searched_cleanup);
395
396 if (sym != NULL)
397 return sym;
398
399 continue;
400 }
401
402 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
403 for (excludep = current->excludes; *excludep; excludep++)
404 if (strcmp (name, *excludep) == 0)
405 break;
406 if (*excludep)
407 {
408 discard_cleanups (searched_cleanup);
409 continue;
410 }
411
412 if (current->alias != NULL
413 && strcmp (name, current->alias) == 0)
414 /* If the import is creating an alias and the alias matches
415 the sought name. Pass current->import_src as the NAME to
416 direct the search towards the aliased namespace. */
417 {
418 sym = cp_lookup_symbol_in_namespace (scope,
419 current->import_src,
8dea366b 420 block, domain, 1);
a1d705ee
TT
421 }
422 else if (current->alias == NULL)
423 {
424 /* If this import statement creates no alias, pass
425 current->inner as NAMESPACE to direct the search
426 towards the imported namespace. */
427 sym = cp_lookup_symbol_imports (current->import_src,
428 name, block,
429 domain, 0, 0);
430 }
431 current->searched = 0;
432 discard_cleanups (searched_cleanup);
433
434 if (sym != NULL)
435 return sym;
8540c487
SW
436 }
437 }
438
439 return NULL;
440}
441
34eaf542
TT
442/* Helper function that searches an array of symbols for one named
443 NAME. */
444
445static struct symbol *
aff410f1
MS
446search_symbol_list (const char *name, int num,
447 struct symbol **syms)
34eaf542
TT
448{
449 int i;
450
451 /* Maybe we should store a dictionary in here instead. */
452 for (i = 0; i < num; ++i)
453 {
454 if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0)
455 return syms[i];
456 }
457 return NULL;
458}
459
460/* Like cp_lookup_symbol_imports, but if BLOCK is a function, it
461 searches through the template parameters of the function and the
462 function's type. */
463
464struct symbol *
465cp_lookup_symbol_imports_or_template (const char *scope,
466 const char *name,
467 const struct block *block,
468 const domain_enum domain)
469{
470 struct symbol *function = BLOCK_FUNCTION (block);
471
472 if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus)
473 {
34eaf542
TT
474 /* Search the function's template parameters. */
475 if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function))
476 {
aff410f1
MS
477 struct template_symbol *templ
478 = (struct template_symbol *) function;
34eaf542
TT
479 struct symbol *result;
480
481 result = search_symbol_list (name,
482 templ->n_template_arguments,
483 templ->template_arguments);
484 if (result != NULL)
485 return result;
486 }
487
488 /* Search the template parameters of the function's defining
489 context. */
490 if (SYMBOL_NATURAL_NAME (function))
491 {
492 struct type *context;
493 char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function));
494 struct cleanup *cleanups = make_cleanup (xfree, name_copy);
495 const struct language_defn *lang = language_def (language_cplus);
df6d5441
TT
496 struct gdbarch *arch
497 = get_objfile_arch (SYMBOL_SYMTAB (function)->objfile);
34eaf542
TT
498 const struct block *parent = BLOCK_SUPERBLOCK (block);
499
500 while (1)
501 {
502 struct symbol *result;
503 unsigned int prefix_len = cp_entire_prefix_len (name_copy);
504
505 if (prefix_len == 0)
506 context = NULL;
507 else
508 {
509 name_copy[prefix_len] = '\0';
aff410f1
MS
510 context = lookup_typename (lang, arch,
511 name_copy,
512 parent, 1);
34eaf542
TT
513 }
514
515 if (context == NULL)
516 break;
517
aff410f1
MS
518 result
519 = search_symbol_list (name,
520 TYPE_N_TEMPLATE_ARGUMENTS (context),
521 TYPE_TEMPLATE_ARGUMENTS (context));
34eaf542 522 if (result != NULL)
c27e16e3
TT
523 {
524 do_cleanups (cleanups);
525 return result;
526 }
34eaf542
TT
527 }
528
529 do_cleanups (cleanups);
530 }
531 }
532
533 return cp_lookup_symbol_imports (scope, name, block, domain, 1, 1);
534}
535
aff410f1
MS
536 /* Searches for NAME in the current namespace, and by applying
537 relevant import statements belonging to BLOCK and its parents.
538 SCOPE is the namespace scope of the context in which the search is
b50c8614
KS
539 being evaluated.
540
541 Class, union, and enum tag names may be used in C++ without specifying
542 class-key or enum. If searching for a VAR_DOMAIN symbol fails,
543 this function will search STRUCT_DOMAIN. [This is done in
544 cp_lookup_symbol_in_namespace.] */
8540c487
SW
545
546struct symbol*
547cp_lookup_symbol_namespace (const char *scope,
548 const char *name,
8540c487 549 const struct block *block,
13387711 550 const domain_enum domain)
8540c487
SW
551{
552 struct symbol *sym;
13387711
SW
553
554 /* First, try to find the symbol in the given namespace. */
aff410f1 555 sym = cp_lookup_symbol_in_namespace (scope, name,
8dea366b 556 block, domain, 1);
13387711
SW
557 if (sym != NULL)
558 return sym;
8540c487 559
aff410f1
MS
560 /* Search for name in namespaces imported to this and parent
561 blocks. */
8540c487
SW
562 while (block != NULL)
563 {
aff410f1
MS
564 sym = cp_lookup_symbol_imports (scope, name, block,
565 domain, 0, 1);
8540c487
SW
566
567 if (sym)
568 return sym;
569
570 block = BLOCK_SUPERBLOCK (block);
571 }
572
573 return NULL;
1fcb5155
DC
574}
575
576/* Lookup NAME at namespace scope (or, in C terms, in static and
577 global variables). SCOPE is the namespace that the current
578 function is defined within; only consider namespaces whose length
579 is at least SCOPE_LEN. Other arguments are as in
580 cp_lookup_symbol_nonlocal.
581
582 For example, if we're within a function A::B::f and looking for a
3882f37a 583 symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
1fcb5155
DC
584 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
585 but with SCOPE_LEN = 1. And then it calls itself with NAME and
586 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
587 "A::B::x"; if it doesn't find it, then the second call looks for
588 "A::x", and if that call fails, then the first call looks for
589 "x". */
590
591static struct symbol *
592lookup_namespace_scope (const char *name,
1fcb5155
DC
593 const struct block *block,
594 const domain_enum domain,
1fcb5155
DC
595 const char *scope,
596 int scope_len)
597{
598 char *namespace;
599
600 if (scope[scope_len] != '\0')
601 {
602 /* Recursively search for names in child namespaces first. */
603
604 struct symbol *sym;
605 int new_scope_len = scope_len;
606
607 /* If the current scope is followed by "::", skip past that. */
608 if (new_scope_len != 0)
609 {
610 gdb_assert (scope[new_scope_len] == ':');
611 new_scope_len += 2;
612 }
613 new_scope_len += cp_find_first_component (scope + new_scope_len);
aff410f1
MS
614 sym = lookup_namespace_scope (name, block, domain,
615 scope, new_scope_len);
1fcb5155
DC
616 if (sym != NULL)
617 return sym;
618 }
619
620 /* Okay, we didn't find a match in our children, so look for the
621 name in the current namespace. */
622
623 namespace = alloca (scope_len + 1);
624 strncpy (namespace, scope, scope_len);
625 namespace[scope_len] = '\0';
aff410f1 626 return cp_lookup_symbol_in_namespace (namespace, name,
8dea366b 627 block, domain, 1);
1fcb5155
DC
628}
629
630/* Look up NAME in BLOCK's static block and in global blocks. If
631 ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
8dea366b
KS
632 within an anonymous namespace. If SEARCH is non-zero, search through
633 base classes for a matching symbol. Other arguments are as in
1fcb5155
DC
634 cp_lookup_symbol_nonlocal. */
635
636static struct symbol *
637lookup_symbol_file (const char *name,
1fcb5155
DC
638 const struct block *block,
639 const domain_enum domain,
8dea366b 640 int anonymous_namespace, int search)
1fcb5155
DC
641{
642 struct symbol *sym = NULL;
643
94af9270 644 sym = lookup_symbol_static (name, block, domain);
1fcb5155
DC
645 if (sym != NULL)
646 return sym;
647
648 if (anonymous_namespace)
649 {
650 /* Symbols defined in anonymous namespaces have external linkage
651 but should be treated as local to a single file nonetheless.
652 So we only search the current file's global block. */
653
654 const struct block *global_block = block_global_block (block);
655
656 if (global_block != NULL)
94af9270 657 sym = lookup_symbol_aux_block (name, global_block, domain);
1fcb5155
DC
658 }
659 else
660 {
94af9270 661 sym = lookup_symbol_global (name, block, domain);
5c4e30ca
DC
662 }
663
8dea366b
KS
664 if (sym != NULL)
665 return sym;
666
667 if (search)
668 {
669 char *klass, *nested;
670 unsigned int prefix_len;
671 struct cleanup *cleanup;
672 struct symbol *klass_sym;
673
674 /* A simple lookup failed. Check if the symbol was defined in
675 a base class. */
676
677 cleanup = make_cleanup (null_cleanup, NULL);
678
679 /* Find the name of the class and the name of the method,
680 variable, etc. */
681 prefix_len = cp_entire_prefix_len (name);
682
683 /* If no prefix was found, search "this". */
684 if (prefix_len == 0)
685 {
686 struct type *type;
687 struct symbol *this;
688
689 this = lookup_language_this (language_def (language_cplus), block);
690 if (this == NULL)
691 {
692 do_cleanups (cleanup);
693 return NULL;
694 }
695
696 type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this)));
c4f87ca6
KS
697 /* If TYPE_NAME is NULL, abandon trying to find this symbol.
698 This can happen for lambda functions compiled with clang++,
699 which outputs no name for the container class. */
700 if (TYPE_NAME (type) == NULL)
701 return NULL;
8dea366b
KS
702 klass = xstrdup (TYPE_NAME (type));
703 nested = xstrdup (name);
704 }
705 else
706 {
707 /* The class name is everything up to and including PREFIX_LEN. */
708 klass = savestring (name, prefix_len);
709
710 /* The rest of the name is everything else past the initial scope
711 operator. */
712 nested = xstrdup (name + prefix_len + 2);
713 }
714
715 /* Add cleanups to free memory for these strings. */
716 make_cleanup (xfree, klass);
717 make_cleanup (xfree, nested);
718
719 /* Lookup a class named KLASS. If none is found, there is nothing
720 more that can be done. */
b50c8614 721 klass_sym = lookup_symbol_global (klass, block, STRUCT_DOMAIN);
8dea366b
KS
722 if (klass_sym == NULL)
723 {
724 do_cleanups (cleanup);
725 return NULL;
726 }
727
728 /* Look for a symbol named NESTED in this class. */
729 sym = cp_lookup_nested_symbol (SYMBOL_TYPE (klass_sym), nested, block);
730 do_cleanups (cleanup);
731 }
732
733 return sym;
734}
735
f7e3ecae
KS
736/* Search through the base classes of PARENT_TYPE for a base class
737 named NAME and return its type. If not found, return NULL. */
738
739struct type *
740find_type_baseclass_by_name (struct type *parent_type, const char *name)
741{
742 int i;
743
744 CHECK_TYPEDEF (parent_type);
745 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
746 {
747 struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i));
748 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
749
750 if (base_name == NULL)
751 continue;
752
753 if (streq (base_name, name))
754 return type;
755
756 type = find_type_baseclass_by_name (type, name);
757 if (type != NULL)
758 return type;
759 }
760
761 return NULL;
762}
763
8dea366b
KS
764/* Search through the base classes of PARENT_TYPE for a symbol named
765 NAME in block BLOCK. */
766
767static struct symbol *
768find_symbol_in_baseclass (struct type *parent_type, const char *name,
769 const struct block *block)
770{
771 int i;
772 struct symbol *sym;
773 struct cleanup *cleanup;
774 char *concatenated_name;
775
776 sym = NULL;
777 concatenated_name = NULL;
778 cleanup = make_cleanup (free_current_contents, &concatenated_name);
779 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
780 {
781 size_t len;
69fc87c2 782 struct type *base_type = TYPE_BASECLASS (parent_type, i);
8dea366b
KS
783 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
784
785 if (base_name == NULL)
786 continue;
787
788 /* Search this particular base class. */
69fc87c2
DE
789 sym = cp_lookup_symbol_in_namespace (base_name, name, block,
790 VAR_DOMAIN, 0);
8dea366b
KS
791 if (sym != NULL)
792 break;
793
69fc87c2
DE
794 /* Now search all static file-level symbols. We have to do this for
795 things like typedefs in the class. First search in this symtab,
796 what we want is possibly there. */
8dea366b
KS
797 len = strlen (base_name) + 2 + strlen (name) + 1;
798 concatenated_name = xrealloc (concatenated_name, len);
799 xsnprintf (concatenated_name, len, "%s::%s", base_name, name);
800 sym = lookup_symbol_static (concatenated_name, block, VAR_DOMAIN);
69fc87c2
DE
801 if (sym != NULL)
802 break;
8dea366b 803
69fc87c2
DE
804 /* Nope. We now have to search all static blocks in all objfiles,
805 even if block != NULL, because there's no guarantees as to which
806 symtab the symbol we want is in. */
807 sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
808 if (sym != NULL)
809 break;
8dea366b
KS
810
811 /* If this class has base classes, search them next. */
69fc87c2
DE
812 CHECK_TYPEDEF (base_type);
813 if (TYPE_N_BASECLASSES (base_type) > 0)
8dea366b 814 {
69fc87c2 815 sym = find_symbol_in_baseclass (base_type, name, block);
8dea366b
KS
816 if (sym != NULL)
817 break;
818 }
819 }
820
821 do_cleanups (cleanup);
0c2e6019 822 return sym;
5c4e30ca
DC
823}
824
50af5481 825/* Look up a symbol named NESTED_NAME that is nested inside the C++
79c2c32d
DC
826 class or namespace given by PARENT_TYPE, from within the context
827 given by BLOCK. Return NULL if there is no such nested type. */
828
50af5481
JK
829struct symbol *
830cp_lookup_nested_symbol (struct type *parent_type,
831 const char *nested_name,
832 const struct block *block)
79c2c32d 833{
d8228535
JK
834 /* type_name_no_tag_required provides better error reporting using the
835 original type. */
836 struct type *saved_parent_type = parent_type;
837
838 CHECK_TYPEDEF (parent_type);
839
79c2c32d
DC
840 switch (TYPE_CODE (parent_type))
841 {
63d06c5c 842 case TYPE_CODE_STRUCT:
79c2c32d 843 case TYPE_CODE_NAMESPACE:
48e32051 844 case TYPE_CODE_UNION:
3d567982 845 case TYPE_CODE_ENUM:
530e8392
KB
846 /* NOTE: Handle modules here as well, because Fortran is re-using the C++
847 specific code to lookup nested symbols in modules, by calling the
848 function pointer la_lookup_symbol_nonlocal, which ends up here. */
849 case TYPE_CODE_MODULE:
79c2c32d 850 {
63d06c5c
DC
851 /* NOTE: carlton/2003-11-10: We don't treat C++ class members
852 of classes like, say, data or function members. Instead,
853 they're just represented by symbols whose names are
854 qualified by the name of the surrounding class. This is
855 just like members of namespaces; in particular,
856 lookup_symbol_namespace works when looking them up. */
857
08850b56 858 int size;
d8228535 859 const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
aff410f1
MS
860 struct symbol *sym
861 = cp_lookup_symbol_in_namespace (parent_name, nested_name,
8dea366b 862 block, VAR_DOMAIN, 0);
41f62f39 863 char *concatenated_name;
c5504eaf 864
50af5481
JK
865 if (sym != NULL)
866 return sym;
41f62f39 867
69fc87c2
DE
868 /* Now search all static file-level symbols. We have to do this
869 for things like typedefs in the class. We do not try to
aff410f1 870 guess any imported namespace as even the fully specified
8dea366b 871 namespace search is already not C++ compliant and more
aff410f1 872 assumptions could make it too magic. */
41f62f39 873
08850b56
PM
874 size = strlen (parent_name) + 2 + strlen (nested_name) + 1;
875 concatenated_name = alloca (size);
876 xsnprintf (concatenated_name, size, "%s::%s",
aff410f1 877 parent_name, nested_name);
50af5481
JK
878 sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
879 if (sym != NULL)
880 return sym;
41f62f39 881
8dea366b
KS
882 /* If no matching symbols were found, try searching any
883 base classes. */
884 return find_symbol_in_baseclass (parent_type, nested_name, block);
79c2c32d 885 }
bb869963
SDJ
886
887 case TYPE_CODE_FUNC:
888 case TYPE_CODE_METHOD:
889 return NULL;
890
79c2c32d
DC
891 default:
892 internal_error (__FILE__, __LINE__,
50af5481 893 _("cp_lookup_nested_symbol called "
3e43a32a 894 "on a non-aggregate type."));
79c2c32d
DC
895 }
896}
897
b368761e
DC
898/* The C++-version of lookup_transparent_type. */
899
900/* FIXME: carlton/2004-01-16: The problem that this is trying to
901 address is that, unfortunately, sometimes NAME is wrong: it may not
902 include the name of namespaces enclosing the type in question.
b021a221 903 lookup_transparent_type gets called when the type in question
b368761e
DC
904 is a declaration, and we're trying to find its definition; but, for
905 declarations, our type name deduction mechanism doesn't work.
906 There's nothing we can do to fix this in general, I think, in the
907 absence of debug information about namespaces (I've filed PR
908 gdb/1511 about this); until such debug information becomes more
909 prevalent, one heuristic which sometimes looks is to search for the
910 definition in namespaces containing the current namespace.
911
912 We should delete this functions once the appropriate debug
913 information becomes more widespread. (GCC 3.4 will be the first
914 released version of GCC with such information.) */
915
916struct type *
917cp_lookup_transparent_type (const char *name)
918{
919 /* First, try the honest way of looking up the definition. */
920 struct type *t = basic_lookup_transparent_type (name);
921 const char *scope;
922
923 if (t != NULL)
924 return t;
925
926 /* If that doesn't work and we're within a namespace, look there
927 instead. */
928 scope = block_scope (get_selected_block (0));
929
930 if (scope[0] == '\0')
931 return NULL;
932
933 return cp_lookup_transparent_type_loop (name, scope, 0);
934}
935
b021a221
MS
936/* Lookup the type definition associated to NAME in namespaces/classes
937 containing SCOPE whose name is strictly longer than LENGTH. LENGTH
938 must be the index of the start of a component of SCOPE. */
b368761e
DC
939
940static struct type *
aff410f1
MS
941cp_lookup_transparent_type_loop (const char *name,
942 const char *scope,
b368761e
DC
943 int length)
944{
1198ecbe 945 int scope_length = length + cp_find_first_component (scope + length);
b368761e
DC
946 char *full_name;
947
948 /* If the current scope is followed by "::", look in the next
949 component. */
950 if (scope[scope_length] == ':')
951 {
952 struct type *retval
aff410f1
MS
953 = cp_lookup_transparent_type_loop (name, scope,
954 scope_length + 2);
c5504eaf 955
b368761e
DC
956 if (retval != NULL)
957 return retval;
958 }
959
960 full_name = alloca (scope_length + 2 + strlen (name) + 1);
961 strncpy (full_name, scope, scope_length);
962 strncpy (full_name + scope_length, "::", 2);
963 strcpy (full_name + scope_length + 2, name);
964
965 return basic_lookup_transparent_type (full_name);
966}
967
0c2e6019
TT
968/* This used to do something but was removed when it became
969 obsolete. */
5c4e30ca
DC
970
971static void
972maintenance_cplus_namespace (char *args, int from_tty)
973{
0c2e6019 974 printf_unfiltered (_("The `maint namespace' command was removed.\n"));
5c4e30ca
DC
975}
976
2c0b251b
PA
977/* Provide a prototype to silence -Wmissing-prototypes. */
978extern initialize_file_ftype _initialize_cp_namespace;
979
5c4e30ca
DC
980void
981_initialize_cp_namespace (void)
982{
0c2e6019
TT
983 struct cmd_list_element *cmd;
984
985 cmd = add_cmd ("namespace", class_maintenance,
986 maintenance_cplus_namespace,
987 _("Deprecated placeholder for removed functionality."),
988 &maint_cplus_cmd_list);
989 deprecate_cmd (cmd, NULL);
1fcb5155 990}
This page took 0.792933 seconds and 4 git commands to generate.