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