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 | ||
c0cc3a76 | 98 | |
aff410f1 MS |
99 | /* Add a using directive to using_directives. If the using directive |
100 | in question has already been added, don't add it twice. | |
101 | ||
102 | Create a new struct using_direct which imports the namespace SRC | |
103 | into the scope DEST. ALIAS is the name of the imported namespace | |
104 | in the current scope. If ALIAS is NULL then the namespace is known | |
105 | by its original name. DECLARATION is the name if the imported | |
106 | varable if this is a declaration import (Eg. using A::x), otherwise | |
12aaed36 TT |
107 | it is NULL. EXCLUDES is a list of names not to import from an |
108 | imported module or NULL. If COPY_NAMES is non-zero, then the | |
109 | arguments are copied into newly allocated memory so they can be | |
110 | temporaries. For EXCLUDES the VEC pointers are copied but the | |
32019081 | 111 | pointed to characters are not copied. */ |
9219021c DC |
112 | |
113 | void | |
13387711 SW |
114 | cp_add_using_directive (const char *dest, |
115 | const char *src, | |
116 | const char *alias, | |
117 | const char *declaration, | |
32019081 | 118 | VEC (const_char_ptr) *excludes, |
12aaed36 | 119 | int copy_names, |
c0cc3a76 | 120 | struct obstack *obstack) |
9219021c DC |
121 | { |
122 | struct using_direct *current; | |
123 | struct using_direct *new; | |
13387711 | 124 | |
9219021c DC |
125 | /* Has it already been added? */ |
126 | ||
27aa8d6a | 127 | for (current = using_directives; current != NULL; current = current->next) |
9219021c | 128 | { |
32019081 JK |
129 | int ix; |
130 | const char *param; | |
131 | ||
70c622a3 JK |
132 | if (strcmp (current->import_src, src) != 0) |
133 | continue; | |
134 | if (strcmp (current->import_dest, dest) != 0) | |
135 | continue; | |
136 | if ((alias == NULL && current->alias != NULL) | |
137 | || (alias != NULL && current->alias == NULL) | |
138 | || (alias != NULL && current->alias != NULL | |
139 | && strcmp (alias, current->alias) != 0)) | |
140 | continue; | |
141 | if ((declaration == NULL && current->declaration != NULL) | |
142 | || (declaration != NULL && current->declaration == NULL) | |
143 | || (declaration != NULL && current->declaration != NULL | |
144 | && strcmp (declaration, current->declaration) != 0)) | |
145 | continue; | |
146 | ||
32019081 JK |
147 | /* Compare the contents of EXCLUDES. */ |
148 | for (ix = 0; VEC_iterate (const_char_ptr, excludes, ix, param); ix++) | |
149 | if (current->excludes[ix] == NULL | |
150 | || strcmp (param, current->excludes[ix]) != 0) | |
151 | break; | |
152 | if (ix < VEC_length (const_char_ptr, excludes) | |
153 | || current->excludes[ix] != NULL) | |
154 | continue; | |
155 | ||
70c622a3 JK |
156 | /* Parameters exactly match CURRENT. */ |
157 | return; | |
9219021c DC |
158 | } |
159 | ||
32019081 JK |
160 | new = obstack_alloc (obstack, (sizeof (*new) |
161 | + (VEC_length (const_char_ptr, excludes) | |
162 | * sizeof (*new->excludes)))); | |
163 | memset (new, 0, sizeof (*new)); | |
c0cc3a76 | 164 | |
12aaed36 TT |
165 | if (copy_names) |
166 | { | |
167 | new->import_src = obstack_copy0 (obstack, src, strlen (src)); | |
168 | new->import_dest = obstack_copy0 (obstack, dest, strlen (dest)); | |
169 | } | |
170 | else | |
171 | { | |
172 | new->import_src = src; | |
173 | new->import_dest = dest; | |
174 | } | |
794684b6 | 175 | |
12aaed36 | 176 | if (alias != NULL && copy_names) |
10f0c4bb | 177 | new->alias = obstack_copy0 (obstack, alias, strlen (alias)); |
12aaed36 TT |
178 | else |
179 | new->alias = alias; | |
c0cc3a76 | 180 | |
12aaed36 | 181 | if (declaration != NULL && copy_names) |
10f0c4bb TT |
182 | new->declaration = obstack_copy0 (obstack, |
183 | declaration, strlen (declaration)); | |
12aaed36 TT |
184 | else |
185 | new->declaration = declaration; | |
13387711 | 186 | |
32019081 JK |
187 | memcpy (new->excludes, VEC_address (const_char_ptr, excludes), |
188 | VEC_length (const_char_ptr, excludes) * sizeof (*new->excludes)); | |
189 | new->excludes[VEC_length (const_char_ptr, excludes)] = NULL; | |
190 | ||
c0cc3a76 SW |
191 | new->next = using_directives; |
192 | using_directives = new; | |
9219021c DC |
193 | } |
194 | ||
9219021c DC |
195 | /* Test whether or not NAMESPACE looks like it mentions an anonymous |
196 | namespace; return nonzero if so. */ | |
197 | ||
198 | int | |
59da4d04 | 199 | cp_is_in_anonymous (const char *symbol_name) |
9219021c | 200 | { |
59da4d04 | 201 | return (strstr (symbol_name, CP_ANONYMOUS_NAMESPACE_STR) |
9219021c DC |
202 | != NULL); |
203 | } | |
204 | ||
34ef8452 DE |
205 | /* Look up NAME in BLOCK's static block and in global blocks. If |
206 | ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located | |
207 | within an anonymous namespace. If SEARCH is non-zero, search through | |
208 | base classes for a matching symbol. Other arguments are as in | |
209 | cp_lookup_symbol_nonlocal. */ | |
210 | ||
211 | static struct symbol * | |
212 | lookup_symbol_file (const char *name, | |
213 | const struct block *block, | |
214 | const domain_enum domain, | |
215 | int anonymous_namespace, int search) | |
216 | { | |
217 | struct symbol *sym = NULL; | |
218 | ||
219 | sym = lookup_symbol_in_static_block (name, block, domain); | |
220 | if (sym != NULL) | |
221 | return sym; | |
222 | ||
223 | if (anonymous_namespace) | |
224 | { | |
225 | /* Symbols defined in anonymous namespaces have external linkage | |
226 | but should be treated as local to a single file nonetheless. | |
227 | So we only search the current file's global block. */ | |
228 | ||
229 | const struct block *global_block = block_global_block (block); | |
230 | ||
231 | if (global_block != NULL) | |
232 | sym = lookup_symbol_in_block (name, global_block, domain); | |
233 | } | |
234 | else | |
235 | { | |
236 | sym = lookup_global_symbol (name, block, domain); | |
237 | } | |
238 | ||
239 | if (sym != NULL) | |
240 | return sym; | |
241 | ||
242 | if (search) | |
243 | { | |
244 | char *klass, *nested; | |
245 | unsigned int prefix_len; | |
246 | struct cleanup *cleanup; | |
247 | struct symbol *klass_sym; | |
248 | ||
249 | /* A simple lookup failed. Check if the symbol was defined in | |
250 | a base class. */ | |
251 | ||
252 | cleanup = make_cleanup (null_cleanup, NULL); | |
253 | ||
254 | /* Find the name of the class and the name of the method, | |
255 | variable, etc. */ | |
256 | prefix_len = cp_entire_prefix_len (name); | |
257 | ||
258 | /* If no prefix was found, search "this". */ | |
259 | if (prefix_len == 0) | |
260 | { | |
261 | struct type *type; | |
262 | struct symbol *this; | |
263 | ||
264 | this = lookup_language_this (language_def (language_cplus), block); | |
265 | if (this == NULL) | |
266 | { | |
267 | do_cleanups (cleanup); | |
268 | return NULL; | |
269 | } | |
270 | ||
271 | type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (this))); | |
272 | /* If TYPE_NAME is NULL, abandon trying to find this symbol. | |
273 | This can happen for lambda functions compiled with clang++, | |
274 | which outputs no name for the container class. */ | |
275 | if (TYPE_NAME (type) == NULL) | |
276 | return NULL; | |
277 | klass = xstrdup (TYPE_NAME (type)); | |
278 | nested = xstrdup (name); | |
279 | } | |
280 | else | |
281 | { | |
282 | /* The class name is everything up to and including PREFIX_LEN. */ | |
283 | klass = savestring (name, prefix_len); | |
284 | ||
285 | /* The rest of the name is everything else past the initial scope | |
286 | operator. */ | |
287 | nested = xstrdup (name + prefix_len + 2); | |
288 | } | |
289 | ||
290 | /* Add cleanups to free memory for these strings. */ | |
291 | make_cleanup (xfree, klass); | |
292 | make_cleanup (xfree, nested); | |
293 | ||
294 | /* Lookup a class named KLASS. If none is found, there is nothing | |
295 | more that can be done. */ | |
296 | klass_sym = lookup_global_symbol (klass, block, domain); | |
297 | if (klass_sym == NULL) | |
298 | { | |
299 | do_cleanups (cleanup); | |
300 | return NULL; | |
301 | } | |
302 | ||
303 | /* Look for a symbol named NESTED in this class. */ | |
304 | sym = cp_lookup_nested_symbol (SYMBOL_TYPE (klass_sym), nested, block); | |
305 | do_cleanups (cleanup); | |
306 | } | |
307 | ||
308 | return sym; | |
309 | } | |
310 | ||
aff410f1 | 311 | /* Look up NAME in the C++ namespace NAMESPACE. Other arguments are |
8dea366b | 312 | as in cp_lookup_symbol_nonlocal. If SEARCH is non-zero, search |
4186eb54 | 313 | through base classes for a matching symbol. */ |
8540c487 SW |
314 | |
315 | static struct symbol * | |
316 | cp_lookup_symbol_in_namespace (const char *namespace, | |
317 | const char *name, | |
8540c487 | 318 | const struct block *block, |
8dea366b | 319 | const domain_enum domain, int search) |
8540c487 SW |
320 | { |
321 | if (namespace[0] == '\0') | |
322 | { | |
4186eb54 | 323 | return lookup_symbol_file (name, block, domain, 0, search); |
8540c487 SW |
324 | } |
325 | else | |
326 | { | |
aff410f1 MS |
327 | char *concatenated_name = alloca (strlen (namespace) + 2 |
328 | + strlen (name) + 1); | |
c5504eaf | 329 | |
8540c487 SW |
330 | strcpy (concatenated_name, namespace); |
331 | strcat (concatenated_name, "::"); | |
332 | strcat (concatenated_name, name); | |
4186eb54 | 333 | return lookup_symbol_file (concatenated_name, block, domain, |
59da4d04 | 334 | cp_is_in_anonymous (namespace), search); |
8540c487 SW |
335 | } |
336 | } | |
337 | ||
b14e635e SW |
338 | /* Used for cleanups to reset the "searched" flag incase |
339 | of an error. */ | |
340 | ||
341 | static void | |
342 | reset_directive_searched (void *data) | |
343 | { | |
344 | struct using_direct *direct = data; | |
345 | direct->searched = 0; | |
346 | } | |
347 | ||
aff410f1 MS |
348 | /* Search for NAME by applying all import statements belonging to |
349 | BLOCK which are applicable in SCOPE. If DECLARATION_ONLY the | |
350 | search is restricted to using declarations. | |
13387711 SW |
351 | Example: |
352 | ||
aff410f1 | 353 | namespace A { |
13387711 SW |
354 | int x; |
355 | } | |
356 | using A::x; | |
357 | ||
aff410f1 MS |
358 | If SEARCH_PARENTS the search will include imports which are |
359 | applicable in parents of SCOPE. | |
b14e635e SW |
360 | Example: |
361 | ||
aff410f1 | 362 | namespace A { |
b14e635e | 363 | using namespace X; |
aff410f1 | 364 | namespace B { |
b14e635e SW |
365 | using namespace Y; |
366 | } | |
367 | } | |
368 | ||
aff410f1 MS |
369 | If SCOPE is "A::B" and SEARCH_PARENTS is true the imports of |
370 | namespaces X and Y will be considered. If SEARCH_PARENTS is false | |
371 | only the import of Y is considered. */ | |
8540c487 | 372 | |
9a80057a | 373 | static struct symbol * |
55accf4a DE |
374 | cp_lookup_symbol_via_imports (const char *scope, |
375 | const char *name, | |
376 | const struct block *block, | |
377 | const domain_enum domain, | |
378 | const int declaration_only, | |
379 | const int search_parents) | |
8540c487 | 380 | { |
b14e635e | 381 | struct using_direct *current; |
13387711 | 382 | struct symbol *sym = NULL; |
8540c487 | 383 | int len; |
b14e635e SW |
384 | int directive_match; |
385 | struct cleanup *searched_cleanup; | |
8540c487 SW |
386 | |
387 | /* First, try to find the symbol in the given namespace. */ | |
13387711 | 388 | if (!declaration_only) |
aff410f1 | 389 | sym = cp_lookup_symbol_in_namespace (scope, name, |
8dea366b | 390 | block, domain, 1); |
13387711 | 391 | |
8540c487 SW |
392 | if (sym != NULL) |
393 | return sym; | |
394 | ||
aff410f1 MS |
395 | /* Go through the using directives. If any of them add new names to |
396 | the namespace we're searching in, see if we can find a match by | |
397 | applying them. */ | |
8540c487 SW |
398 | |
399 | for (current = block_using (block); | |
400 | current != NULL; | |
401 | current = current->next) | |
402 | { | |
32019081 JK |
403 | const char **excludep; |
404 | ||
b14e635e SW |
405 | len = strlen (current->import_dest); |
406 | directive_match = (search_parents | |
407 | ? (strncmp (scope, current->import_dest, | |
408 | strlen (current->import_dest)) == 0 | |
409 | && (len == 0 | |
aff410f1 MS |
410 | || scope[len] == ':' |
411 | || scope[len] == '\0')) | |
b14e635e | 412 | : strcmp (scope, current->import_dest) == 0); |
8540c487 | 413 | |
aff410f1 MS |
414 | /* If the import destination is the current scope or one of its |
415 | ancestors then it is applicable. */ | |
b14e635e | 416 | if (directive_match && !current->searched) |
8540c487 | 417 | { |
a1d705ee TT |
418 | /* Mark this import as searched so that the recursive call |
419 | does not search it again. */ | |
420 | current->searched = 1; | |
421 | searched_cleanup = make_cleanup (reset_directive_searched, | |
422 | current); | |
423 | ||
424 | /* If there is an import of a single declaration, compare the | |
425 | imported declaration (after optional renaming by its alias) | |
426 | with the sought out name. If there is a match pass | |
427 | current->import_src as NAMESPACE to direct the search | |
428 | towards the imported namespace. */ | |
429 | if (current->declaration | |
430 | && strcmp (name, current->alias | |
431 | ? current->alias : current->declaration) == 0) | |
432 | sym = cp_lookup_symbol_in_namespace (current->import_src, | |
433 | current->declaration, | |
8dea366b | 434 | block, domain, 1); |
a1d705ee TT |
435 | |
436 | /* If this is a DECLARATION_ONLY search or a symbol was found | |
437 | or this import statement was an import declaration, the | |
438 | search of this import is complete. */ | |
439 | if (declaration_only || sym != NULL || current->declaration) | |
440 | { | |
441 | current->searched = 0; | |
442 | discard_cleanups (searched_cleanup); | |
443 | ||
444 | if (sym != NULL) | |
445 | return sym; | |
446 | ||
447 | continue; | |
448 | } | |
449 | ||
450 | /* Do not follow CURRENT if NAME matches its EXCLUDES. */ | |
451 | for (excludep = current->excludes; *excludep; excludep++) | |
452 | if (strcmp (name, *excludep) == 0) | |
453 | break; | |
454 | if (*excludep) | |
455 | { | |
456 | discard_cleanups (searched_cleanup); | |
457 | continue; | |
458 | } | |
459 | ||
460 | if (current->alias != NULL | |
461 | && strcmp (name, current->alias) == 0) | |
462 | /* If the import is creating an alias and the alias matches | |
463 | the sought name. Pass current->import_src as the NAME to | |
464 | direct the search towards the aliased namespace. */ | |
465 | { | |
466 | sym = cp_lookup_symbol_in_namespace (scope, | |
467 | current->import_src, | |
8dea366b | 468 | block, domain, 1); |
a1d705ee TT |
469 | } |
470 | else if (current->alias == NULL) | |
471 | { | |
472 | /* If this import statement creates no alias, pass | |
473 | current->inner as NAMESPACE to direct the search | |
474 | towards the imported namespace. */ | |
55accf4a DE |
475 | sym = cp_lookup_symbol_via_imports (current->import_src, |
476 | name, block, | |
477 | domain, 0, 0); | |
a1d705ee TT |
478 | } |
479 | current->searched = 0; | |
480 | discard_cleanups (searched_cleanup); | |
481 | ||
482 | if (sym != NULL) | |
483 | return sym; | |
8540c487 SW |
484 | } |
485 | } | |
486 | ||
487 | return NULL; | |
488 | } | |
489 | ||
34eaf542 TT |
490 | /* Helper function that searches an array of symbols for one named |
491 | NAME. */ | |
492 | ||
493 | static struct symbol * | |
aff410f1 MS |
494 | search_symbol_list (const char *name, int num, |
495 | struct symbol **syms) | |
34eaf542 TT |
496 | { |
497 | int i; | |
498 | ||
499 | /* Maybe we should store a dictionary in here instead. */ | |
500 | for (i = 0; i < num; ++i) | |
501 | { | |
502 | if (strcmp (name, SYMBOL_NATURAL_NAME (syms[i])) == 0) | |
503 | return syms[i]; | |
504 | } | |
505 | return NULL; | |
506 | } | |
507 | ||
55accf4a | 508 | /* Like cp_lookup_symbol_via_imports, but if BLOCK is a function, it |
34eaf542 TT |
509 | searches through the template parameters of the function and the |
510 | function's type. */ | |
511 | ||
512 | struct symbol * | |
513 | cp_lookup_symbol_imports_or_template (const char *scope, | |
514 | const char *name, | |
515 | const struct block *block, | |
516 | const domain_enum domain) | |
517 | { | |
518 | struct symbol *function = BLOCK_FUNCTION (block); | |
cc485e62 DE |
519 | struct symbol *result; |
520 | ||
521 | if (symbol_lookup_debug) | |
522 | { | |
523 | fprintf_unfiltered (gdb_stdlog, | |
524 | "cp_lookup_symbol_imports_or_template" | |
525 | " (%s, %s, %s, %s)\n", | |
526 | scope, name, host_address_to_string (block), | |
527 | domain_name (domain)); | |
528 | } | |
34eaf542 TT |
529 | |
530 | if (function != NULL && SYMBOL_LANGUAGE (function) == language_cplus) | |
531 | { | |
34eaf542 TT |
532 | /* Search the function's template parameters. */ |
533 | if (SYMBOL_IS_CPLUS_TEMPLATE_FUNCTION (function)) | |
534 | { | |
aff410f1 MS |
535 | struct template_symbol *templ |
536 | = (struct template_symbol *) function; | |
34eaf542 TT |
537 | |
538 | result = search_symbol_list (name, | |
539 | templ->n_template_arguments, | |
540 | templ->template_arguments); | |
541 | if (result != NULL) | |
cc485e62 DE |
542 | { |
543 | if (symbol_lookup_debug) | |
544 | { | |
545 | fprintf_unfiltered (gdb_stdlog, | |
546 | "cp_lookup_symbol_imports_or_template" | |
547 | " (...) = %s\n", | |
548 | host_address_to_string (result)); | |
549 | } | |
550 | return result; | |
551 | } | |
34eaf542 TT |
552 | } |
553 | ||
554 | /* Search the template parameters of the function's defining | |
555 | context. */ | |
556 | if (SYMBOL_NATURAL_NAME (function)) | |
557 | { | |
558 | struct type *context; | |
559 | char *name_copy = xstrdup (SYMBOL_NATURAL_NAME (function)); | |
560 | struct cleanup *cleanups = make_cleanup (xfree, name_copy); | |
561 | const struct language_defn *lang = language_def (language_cplus); | |
df6d5441 | 562 | struct gdbarch *arch |
aff08958 | 563 | = get_objfile_arch (SYMBOL_OBJFILE (function)); |
34eaf542 TT |
564 | const struct block *parent = BLOCK_SUPERBLOCK (block); |
565 | ||
566 | while (1) | |
567 | { | |
34eaf542 TT |
568 | unsigned int prefix_len = cp_entire_prefix_len (name_copy); |
569 | ||
570 | if (prefix_len == 0) | |
571 | context = NULL; | |
572 | else | |
573 | { | |
574 | name_copy[prefix_len] = '\0'; | |
aff410f1 MS |
575 | context = lookup_typename (lang, arch, |
576 | name_copy, | |
577 | parent, 1); | |
34eaf542 TT |
578 | } |
579 | ||
580 | if (context == NULL) | |
581 | break; | |
582 | ||
aff410f1 MS |
583 | result |
584 | = search_symbol_list (name, | |
585 | TYPE_N_TEMPLATE_ARGUMENTS (context), | |
586 | TYPE_TEMPLATE_ARGUMENTS (context)); | |
34eaf542 | 587 | if (result != NULL) |
c27e16e3 TT |
588 | { |
589 | do_cleanups (cleanups); | |
cc485e62 DE |
590 | if (symbol_lookup_debug) |
591 | { | |
592 | fprintf_unfiltered (gdb_stdlog, | |
593 | "cp_lookup_symbol_imports_or_template" | |
594 | " (...) = %s\n", | |
595 | host_address_to_string (result)); | |
596 | } | |
c27e16e3 TT |
597 | return result; |
598 | } | |
34eaf542 TT |
599 | } |
600 | ||
601 | do_cleanups (cleanups); | |
602 | } | |
603 | } | |
604 | ||
cc485e62 DE |
605 | result = cp_lookup_symbol_via_imports (scope, name, block, domain, 1, 1); |
606 | if (symbol_lookup_debug) | |
607 | { | |
608 | fprintf_unfiltered (gdb_stdlog, | |
609 | "cp_lookup_symbol_imports_or_template (...) = %s\n", | |
610 | result != NULL | |
611 | ? host_address_to_string (result) : "NULL"); | |
612 | } | |
613 | return result; | |
34eaf542 TT |
614 | } |
615 | ||
aff410f1 MS |
616 | /* Searches for NAME in the current namespace, and by applying |
617 | relevant import statements belonging to BLOCK and its parents. | |
618 | SCOPE is the namespace scope of the context in which the search is | |
4186eb54 | 619 | being evaluated. */ |
8540c487 SW |
620 | |
621 | struct symbol* | |
622 | cp_lookup_symbol_namespace (const char *scope, | |
623 | const char *name, | |
8540c487 | 624 | const struct block *block, |
13387711 | 625 | const domain_enum domain) |
8540c487 SW |
626 | { |
627 | struct symbol *sym; | |
cc485e62 DE |
628 | |
629 | if (symbol_lookup_debug) | |
630 | { | |
631 | fprintf_unfiltered (gdb_stdlog, | |
632 | "cp_lookup_symbol_namespace (%s, %s, %s, %s)\n", | |
633 | scope, name, host_address_to_string (block), | |
634 | domain_name (domain)); | |
635 | } | |
636 | ||
13387711 | 637 | /* First, try to find the symbol in the given namespace. */ |
aff410f1 | 638 | sym = cp_lookup_symbol_in_namespace (scope, name, |
8dea366b | 639 | block, domain, 1); |
13387711 | 640 | if (sym != NULL) |
cc485e62 DE |
641 | { |
642 | if (symbol_lookup_debug) | |
643 | { | |
644 | fprintf_unfiltered (gdb_stdlog, | |
645 | "cp_lookup_symbol_namespace (...) = %s\n", | |
646 | host_address_to_string (sym)); | |
647 | } | |
648 | return sym; | |
649 | } | |
8540c487 | 650 | |
aff410f1 MS |
651 | /* Search for name in namespaces imported to this and parent |
652 | blocks. */ | |
8540c487 SW |
653 | while (block != NULL) |
654 | { | |
55accf4a DE |
655 | sym = cp_lookup_symbol_via_imports (scope, name, block, |
656 | domain, 0, 1); | |
8540c487 SW |
657 | |
658 | if (sym) | |
cc485e62 DE |
659 | { |
660 | if (symbol_lookup_debug) | |
661 | { | |
662 | fprintf_unfiltered (gdb_stdlog, | |
663 | "cp_lookup_symbol_namespace (...) = %s\n", | |
664 | host_address_to_string (sym)); | |
665 | } | |
666 | return sym; | |
667 | } | |
8540c487 SW |
668 | |
669 | block = BLOCK_SUPERBLOCK (block); | |
670 | } | |
671 | ||
cc485e62 DE |
672 | if (symbol_lookup_debug) |
673 | { | |
674 | fprintf_unfiltered (gdb_stdlog, | |
675 | "cp_lookup_symbol_namespace (...) = NULL\n"); | |
676 | } | |
8540c487 | 677 | return NULL; |
1fcb5155 DC |
678 | } |
679 | ||
680 | /* Lookup NAME at namespace scope (or, in C terms, in static and | |
681 | global variables). SCOPE is the namespace that the current | |
682 | function is defined within; only consider namespaces whose length | |
683 | is at least SCOPE_LEN. Other arguments are as in | |
684 | cp_lookup_symbol_nonlocal. | |
685 | ||
686 | For example, if we're within a function A::B::f and looking for a | |
3882f37a | 687 | symbol x, this will get called with NAME = "x", SCOPE = "A::B", and |
1fcb5155 DC |
688 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, |
689 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
690 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
691 | "A::B::x"; if it doesn't find it, then the second call looks for | |
692 | "A::x", and if that call fails, then the first call looks for | |
693 | "x". */ | |
694 | ||
695 | static struct symbol * | |
696 | lookup_namespace_scope (const char *name, | |
1fcb5155 DC |
697 | const struct block *block, |
698 | const domain_enum domain, | |
1fcb5155 DC |
699 | const char *scope, |
700 | int scope_len) | |
701 | { | |
702 | char *namespace; | |
703 | ||
704 | if (scope[scope_len] != '\0') | |
705 | { | |
706 | /* Recursively search for names in child namespaces first. */ | |
707 | ||
708 | struct symbol *sym; | |
709 | int new_scope_len = scope_len; | |
710 | ||
711 | /* If the current scope is followed by "::", skip past that. */ | |
712 | if (new_scope_len != 0) | |
713 | { | |
714 | gdb_assert (scope[new_scope_len] == ':'); | |
715 | new_scope_len += 2; | |
716 | } | |
717 | new_scope_len += cp_find_first_component (scope + new_scope_len); | |
aff410f1 MS |
718 | sym = lookup_namespace_scope (name, block, domain, |
719 | scope, new_scope_len); | |
1fcb5155 DC |
720 | if (sym != NULL) |
721 | return sym; | |
722 | } | |
723 | ||
724 | /* Okay, we didn't find a match in our children, so look for the | |
725 | name in the current namespace. */ | |
726 | ||
727 | namespace = alloca (scope_len + 1); | |
728 | strncpy (namespace, scope, scope_len); | |
729 | namespace[scope_len] = '\0'; | |
aff410f1 | 730 | return cp_lookup_symbol_in_namespace (namespace, name, |
8dea366b | 731 | block, domain, 1); |
1fcb5155 DC |
732 | } |
733 | ||
56286edf DE |
734 | /* The C++-specific version of name lookup for static and global |
735 | names. This makes sure that names get looked for in all namespaces | |
736 | that are in scope. NAME is the natural name of the symbol that | |
737 | we're looking for, BLOCK is the block that we're searching within, | |
738 | DOMAIN says what kind of symbols we're looking for. */ | |
739 | ||
740 | struct symbol * | |
741 | cp_lookup_symbol_nonlocal (const char *name, | |
742 | const struct block *block, | |
743 | const domain_enum domain) | |
744 | { | |
745 | struct symbol *sym; | |
746 | const char *scope = block_scope (block); | |
747 | ||
cc485e62 DE |
748 | if (symbol_lookup_debug) |
749 | { | |
750 | fprintf_unfiltered (gdb_stdlog, | |
751 | "cp_lookup_symbol_non_local" | |
752 | " (%s, %s (scope %s), %s)\n", | |
753 | name, host_address_to_string (block), scope, | |
754 | domain_name (domain)); | |
755 | } | |
756 | ||
757 | sym = lookup_namespace_scope (name, block, domain, scope, 0); | |
56286edf | 758 | if (sym != NULL) |
cc485e62 DE |
759 | { |
760 | if (symbol_lookup_debug) | |
761 | { | |
762 | fprintf_unfiltered (gdb_stdlog, | |
763 | "cp_lookup_symbol_nonlocal (...) = %s\n", | |
764 | host_address_to_string (sym)); | |
765 | } | |
766 | return sym; | |
767 | } | |
56286edf | 768 | |
cc485e62 DE |
769 | sym = cp_lookup_symbol_namespace (scope, name, block, domain); |
770 | if (symbol_lookup_debug) | |
771 | { | |
772 | fprintf_unfiltered (gdb_stdlog, | |
773 | "cp_lookup_symbol_nonlocal (...) = %s\n", | |
774 | sym != NULL ? host_address_to_string (sym) : "NULL"); | |
775 | } | |
776 | return sym; | |
56286edf DE |
777 | } |
778 | ||
f7e3ecae KS |
779 | /* Search through the base classes of PARENT_TYPE for a base class |
780 | named NAME and return its type. If not found, return NULL. */ | |
781 | ||
782 | struct type * | |
a07e3e18 | 783 | cp_find_type_baseclass_by_name (struct type *parent_type, const char *name) |
f7e3ecae KS |
784 | { |
785 | int i; | |
786 | ||
787 | CHECK_TYPEDEF (parent_type); | |
788 | for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i) | |
789 | { | |
790 | struct type *type = check_typedef (TYPE_BASECLASS (parent_type, i)); | |
791 | const char *base_name = TYPE_BASECLASS_NAME (parent_type, i); | |
792 | ||
793 | if (base_name == NULL) | |
794 | continue; | |
795 | ||
796 | if (streq (base_name, name)) | |
797 | return type; | |
798 | ||
a07e3e18 | 799 | type = cp_find_type_baseclass_by_name (type, name); |
f7e3ecae KS |
800 | if (type != NULL) |
801 | return type; | |
802 | } | |
803 | ||
804 | return NULL; | |
805 | } | |
806 | ||
8dea366b KS |
807 | /* Search through the base classes of PARENT_TYPE for a symbol named |
808 | NAME in block BLOCK. */ | |
809 | ||
810 | static struct symbol * | |
811 | find_symbol_in_baseclass (struct type *parent_type, const char *name, | |
812 | const struct block *block) | |
813 | { | |
814 | int i; | |
815 | struct symbol *sym; | |
816 | struct cleanup *cleanup; | |
817 | char *concatenated_name; | |
818 | ||
819 | sym = NULL; | |
820 | concatenated_name = NULL; | |
821 | cleanup = make_cleanup (free_current_contents, &concatenated_name); | |
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 | } |