Fix: strip --strip-debug breaks relocations
[binutils-gdb.git] / gdb / d-namespace.c
blob69474ba08d92ae5ccf6d0473633e326b62a744bc
1 /* Helper routines for D support in GDB.
3 Copyright (C) 2014-2023 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "symtab.h"
22 #include "block.h"
23 #include "language.h"
24 #include "namespace.h"
25 #include "d-lang.h"
26 #include "gdbsupport/gdb_obstack.h"
27 #include "gdbarch.h"
28 #include "inferior.h"
30 /* This returns the length of first component of NAME, which should be
31 the demangled name of a D variable/function/method/etc.
32 Specifically, it returns the index of the first dot forming the
33 boundary of the first component: so, given 'A.foo' or 'A.B.foo'
34 it returns the 1, and given 'foo', it returns 0. */
36 /* The character in NAME indexed by the return value is guaranteed to
37 always be either '.' or '\0'. */
39 static unsigned int
40 d_find_first_component (const char *name)
42 unsigned int index = 0;
44 for (;; ++index)
46 if (name[index] == '.' || name[index] == '\0')
47 return index;
51 /* If NAME is the fully-qualified name of a D function/variable/method,
52 this returns the length of its entire prefix: all of the modules and
53 classes that make up its name. Given 'A.foo', it returns 1, given
54 'A.B.foo', it returns 4, given 'foo', it returns 0. */
56 static unsigned int
57 d_entire_prefix_len (const char *name)
59 unsigned int current_len = d_find_first_component (name);
60 unsigned int previous_len = 0;
62 while (name[current_len] != '\0')
64 gdb_assert (name[current_len] == '.');
65 previous_len = current_len;
66 /* Skip the '.' */
67 current_len++;
68 current_len += d_find_first_component (name + current_len);
71 return previous_len;
74 /* Look up NAME in BLOCK's static block and in global blocks.
75 If SEARCH is non-zero, search through base classes for a matching
76 symbol. Other arguments are as in d_lookup_symbol_nonlocal. */
78 static struct block_symbol
79 d_lookup_symbol (const struct language_defn *langdef,
80 const char *name, const struct block *block,
81 const domain_enum domain, int search)
83 struct block_symbol sym;
85 sym = lookup_symbol_in_static_block (name, block, domain);
86 if (sym.symbol != NULL)
87 return sym;
89 /* If we didn't find a definition for a builtin type in the static block,
90 such as "ucent" which is a specialist type, search for it now. */
91 if (langdef != NULL && domain == VAR_DOMAIN)
93 struct gdbarch *gdbarch;
95 if (block == NULL)
96 gdbarch = current_inferior ()->arch ();
97 else
98 gdbarch = block->gdbarch ();
99 sym.symbol
100 = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
101 sym.block = NULL;
102 if (sym.symbol != NULL)
103 return sym;
106 sym = lookup_global_symbol (name, block, domain);
108 if (sym.symbol != NULL)
109 return sym;
111 if (search)
113 std::string classname, nested;
114 unsigned int prefix_len;
115 struct block_symbol class_sym;
117 /* A simple lookup failed. Check if the symbol was defined in
118 a base class. */
120 /* Find the name of the class and the name of the method,
121 variable, etc. */
122 prefix_len = d_entire_prefix_len (name);
124 /* If no prefix was found, search "this". */
125 if (prefix_len == 0)
127 struct type *type;
128 struct block_symbol lang_this;
130 lang_this = lookup_language_this (language_def (language_d), block);
131 if (lang_this.symbol == NULL)
132 return {};
134 type = check_typedef (lang_this.symbol->type ()->target_type ());
135 classname = type->name ();
136 nested = name;
138 else
140 /* The class name is everything up to and including PREFIX_LEN. */
141 classname = std::string (name, prefix_len);
143 /* The rest of the name is everything else past the initial scope
144 operator. */
145 nested = std::string (name + prefix_len + 1);
148 /* Lookup a class named CLASSNAME. If none is found, there is nothing
149 more that can be done. */
150 class_sym = lookup_global_symbol (classname.c_str (), block, domain);
151 if (class_sym.symbol == NULL)
152 return {};
154 /* Look for a symbol named NESTED in this class. */
155 sym = d_lookup_nested_symbol (class_sym.symbol->type (),
156 nested.c_str (), block);
159 return sym;
162 /* Look up NAME in the D module MODULE. Other arguments are as in
163 d_lookup_symbol_nonlocal. If SEARCH is non-zero, search through
164 base classes for a matching symbol. */
166 static struct block_symbol
167 d_lookup_symbol_in_module (const char *module, const char *name,
168 const struct block *block,
169 const domain_enum domain, int search)
171 char *concatenated_name = NULL;
173 if (module[0] != '\0')
175 concatenated_name
176 = (char *) alloca (strlen (module) + strlen (name) + 2);
177 strcpy (concatenated_name, module);
178 strcat (concatenated_name, ".");
179 strcat (concatenated_name, name);
180 name = concatenated_name;
183 return d_lookup_symbol (NULL, name, block, domain, search);
186 /* Lookup NAME at module scope. SCOPE is the module that the current
187 function is defined within; only consider modules whose length is at
188 least SCOPE_LEN. Other arguments are as in d_lookup_symbol_nonlocal.
190 For example, if we're within a function A.B.f and looking for a
191 symbol x, this will get called with NAME = "x", SCOPE = "A.B", and
192 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
193 but with SCOPE_LEN = 1. And then it calls itself with NAME and
194 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
195 "A.B.x"; if it doesn't find it, then the second call looks for "A.x",
196 and if that call fails, then the first call looks for "x". */
198 static struct block_symbol
199 lookup_module_scope (const struct language_defn *langdef,
200 const char *name, const struct block *block,
201 const domain_enum domain, const char *scope,
202 int scope_len)
204 char *module;
206 if (scope[scope_len] != '\0')
208 /* Recursively search for names in child modules first. */
210 struct block_symbol sym;
211 int new_scope_len = scope_len;
213 /* If the current scope is followed by ".", skip past that. */
214 if (new_scope_len != 0)
216 gdb_assert (scope[new_scope_len] == '.');
217 new_scope_len++;
219 new_scope_len += d_find_first_component (scope + new_scope_len);
220 sym = lookup_module_scope (langdef, name, block, domain,
221 scope, new_scope_len);
222 if (sym.symbol != NULL)
223 return sym;
226 /* Okay, we didn't find a match in our children, so look for the
227 name in the current module.
229 If we there is no scope and we know we have a bare symbol, then short
230 circuit everything and call d_lookup_symbol directly.
231 This isn't an optimization, rather it allows us to pass LANGDEF which
232 is needed for primitive type lookup. */
234 if (scope_len == 0 && strchr (name, '.') == NULL)
235 return d_lookup_symbol (langdef, name, block, domain, 1);
237 module = (char *) alloca (scope_len + 1);
238 strncpy (module, scope, scope_len);
239 module[scope_len] = '\0';
240 return d_lookup_symbol_in_module (module, name,
241 block, domain, 1);
244 /* Search through the base classes of PARENT_TYPE for a symbol named
245 NAME in block BLOCK. */
247 static struct block_symbol
248 find_symbol_in_baseclass (struct type *parent_type, const char *name,
249 const struct block *block)
251 struct block_symbol sym = {};
252 int i;
254 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
256 struct type *base_type = TYPE_BASECLASS (parent_type, i);
257 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
259 if (base_name == NULL)
260 continue;
262 /* Search this particular base class. */
263 sym = d_lookup_symbol_in_module (base_name, name, block,
264 VAR_DOMAIN, 0);
265 if (sym.symbol != NULL)
266 break;
268 /* Now search all static file-level symbols. We have to do this for
269 things like typedefs in the class. First search in this symtab,
270 what we want is possibly there. */
271 std::string concatenated_name = std::string (base_name) + "." + name;
272 sym = lookup_symbol_in_static_block (concatenated_name.c_str (), block,
273 VAR_DOMAIN);
274 if (sym.symbol != NULL)
275 break;
277 /* Nope. We now have to search all static blocks in all objfiles,
278 even if block != NULL, because there's no guarantees as to which
279 symtab the symbol we want is in. */
280 sym = lookup_static_symbol (concatenated_name.c_str (), VAR_DOMAIN);
281 if (sym.symbol != NULL)
282 break;
284 /* If this class has base classes, search them next. */
285 base_type = check_typedef (base_type);
286 if (TYPE_N_BASECLASSES (base_type) > 0)
288 sym = find_symbol_in_baseclass (base_type, name, block);
289 if (sym.symbol != NULL)
290 break;
294 return sym;
297 /* Look up a symbol named NESTED_NAME that is nested inside the D
298 class or module given by PARENT_TYPE, from within the context
299 given by BLOCK. Return NULL if there is no such nested type. */
301 struct block_symbol
302 d_lookup_nested_symbol (struct type *parent_type,
303 const char *nested_name,
304 const struct block *block)
306 /* type_name_no_tag_required provides better error reporting using the
307 original type. */
308 struct type *saved_parent_type = parent_type;
310 parent_type = check_typedef (parent_type);
312 switch (parent_type->code ())
314 case TYPE_CODE_STRUCT:
315 case TYPE_CODE_UNION:
316 case TYPE_CODE_ENUM:
317 case TYPE_CODE_MODULE:
319 int size;
320 const char *parent_name = type_name_or_error (saved_parent_type);
321 struct block_symbol sym
322 = d_lookup_symbol_in_module (parent_name, nested_name,
323 block, VAR_DOMAIN, 0);
324 char *concatenated_name;
326 if (sym.symbol != NULL)
327 return sym;
329 /* Now search all static file-level symbols. We have to do this
330 for things like typedefs in the class. We do not try to
331 guess any imported module as even the fully specified
332 module search is already not D compliant and more assumptions
333 could make it too magic. */
334 size = strlen (parent_name) + strlen (nested_name) + 2;
335 concatenated_name = (char *) alloca (size);
337 xsnprintf (concatenated_name, size, "%s.%s",
338 parent_name, nested_name);
340 sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
341 if (sym.symbol != NULL)
342 return sym;
344 /* If no matching symbols were found, try searching any
345 base classes. */
346 return find_symbol_in_baseclass (parent_type, nested_name, block);
349 case TYPE_CODE_FUNC:
350 case TYPE_CODE_METHOD:
351 return {};
353 default:
354 gdb_assert_not_reached ("called with non-aggregate type.");
358 /* Search for NAME by applying all import statements belonging to
359 BLOCK which are applicable in SCOPE. */
361 static struct block_symbol
362 d_lookup_symbol_imports (const char *scope, const char *name,
363 const struct block *block,
364 const domain_enum domain)
366 struct using_direct *current;
367 struct block_symbol sym;
369 /* First, try to find the symbol in the given module. */
370 sym = d_lookup_symbol_in_module (scope, name, block, domain, 1);
372 if (sym.symbol != NULL)
373 return sym;
375 /* Go through the using directives. If any of them add new names to
376 the module we're searching in, see if we can find a match by
377 applying them. */
379 for (current = block->get_using ();
380 current != NULL;
381 current = current->next)
383 const char **excludep;
385 /* If the import destination is the current scope then search it. */
386 if (!current->searched && strcmp (scope, current->import_dest) == 0)
388 /* Mark this import as searched so that the recursive call
389 does not search it again. */
390 scoped_restore restore_searched
391 = make_scoped_restore (&current->searched, 1);
393 /* If there is an import of a single declaration, compare the
394 imported declaration (after optional renaming by its alias)
395 with the sought out name. If there is a match pass
396 current->import_src as MODULE to direct the search towards
397 the imported module. */
398 if (current->declaration
399 && strcmp (name, current->alias
400 ? current->alias : current->declaration) == 0)
401 sym = d_lookup_symbol_in_module (current->import_src,
402 current->declaration,
403 block, domain, 1);
405 /* If a symbol was found or this import statement was an import
406 declaration, the search of this import is complete. */
407 if (sym.symbol != NULL || current->declaration)
409 if (sym.symbol != NULL)
410 return sym;
412 continue;
415 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
416 for (excludep = current->excludes; *excludep; excludep++)
417 if (strcmp (name, *excludep) == 0)
418 break;
419 if (*excludep)
420 continue;
422 /* If the import statement is creating an alias. */
423 if (current->alias != NULL)
425 if (strcmp (name, current->alias) == 0)
427 /* If the alias matches the sought name. Pass
428 current->import_src as the NAME to direct the
429 search towards the aliased module. */
430 sym = lookup_module_scope (NULL, current->import_src, block,
431 domain, scope, 0);
433 else
435 /* If the alias matches the first component of the
436 sought name, pass current->import_src as MODULE
437 to direct the search, skipping over the aliased
438 component in NAME. */
439 int name_scope = d_find_first_component (name);
441 if (name[name_scope] != '\0'
442 && strncmp (name, current->alias, name_scope) == 0)
444 /* Skip the '.' */
445 name_scope++;
446 sym = d_lookup_symbol_in_module (current->import_src,
447 name + name_scope,
448 block, domain, 1);
452 else
454 /* If this import statement creates no alias, pass
455 current->import_src as MODULE to direct the search
456 towards the imported module. */
457 sym = d_lookup_symbol_in_module (current->import_src,
458 name, block, domain, 1);
461 if (sym.symbol != NULL)
462 return sym;
466 return {};
469 /* Searches for NAME in the current module, and by applying relevant
470 import statements belonging to BLOCK and its parents. SCOPE is the
471 module scope of the context in which the search is being evaluated. */
473 static struct block_symbol
474 d_lookup_symbol_module (const char *scope, const char *name,
475 const struct block *block,
476 const domain_enum domain)
478 struct block_symbol sym;
480 /* First, try to find the symbol in the given module. */
481 sym = d_lookup_symbol_in_module (scope, name,
482 block, domain, 1);
483 if (sym.symbol != NULL)
484 return sym;
486 /* Search for name in modules imported to this and parent
487 blocks. */
488 while (block != NULL)
490 sym = d_lookup_symbol_imports (scope, name, block, domain);
492 if (sym.symbol != NULL)
493 return sym;
495 block = block->superblock ();
498 return {};
501 /* The D-specific version of name lookup for static and global names
502 This makes sure that names get looked for in all modules that are
503 in scope. NAME is the natural name of the symbol that we're looking
504 looking for, BLOCK is the block that we're searching within, DOMAIN
505 says what kind of symbols we're looking for, and if SYMTAB is non-NULL,
506 we should store the symtab where we found the symbol in it. */
508 struct block_symbol
509 d_lookup_symbol_nonlocal (const struct language_defn *langdef,
510 const char *name,
511 const struct block *block,
512 const domain_enum domain)
514 struct block_symbol sym;
515 const char *scope = block == nullptr ? "" : block->scope ();
517 sym = lookup_module_scope (langdef, name, block, domain, scope, 0);
518 if (sym.symbol != NULL)
519 return sym;
521 return d_lookup_symbol_module (scope, name, block, domain);