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