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