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