1 /* Helper routines for D support in GDB.
3 Copyright (C) 2014-2015 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/>. */
24 #include "namespace.h"
26 #include "gdb_obstack.h"
28 /* This returns the length of first component of NAME, which should be
29 the demangled name of a D variable/function/method/etc.
30 Specifically, it returns the index of the first dot forming the
31 boundary of the first component: so, given 'A.foo' or 'A.B.foo'
32 it returns the 1, and given 'foo', it returns 0. */
34 /* The character in NAME indexed by the return value is guaranteed to
35 always be either '.' or '\0'. */
38 d_find_first_component (const char *name)
40 unsigned int index = 0;
44 if (name[index] == '.' || name[index] == '\0')
49 /* If NAME is the fully-qualified name of a D function/variable/method,
50 this returns the length of its entire prefix: all of the modules and
51 classes that make up its name. Given 'A.foo', it returns 1, given
52 'A.B.foo', it returns 4, given 'foo', it returns 0. */
55 d_entire_prefix_len (const char *name)
57 unsigned int current_len = d_find_first_component (name);
58 unsigned int previous_len = 0;
60 while (name[current_len] != '\0')
62 gdb_assert (name[current_len] == '.');
63 previous_len = current_len;
66 current_len += d_find_first_component (name + current_len);
72 /* Look up NAME in BLOCK's static block and in global blocks.
73 If SEARCH is non-zero, search through base classes for a matching
74 symbol. Other arguments are as in d_lookup_symbol_nonlocal. */
76 static struct block_symbol
77 d_lookup_symbol (const char *name, const struct block *block,
78 const domain_enum domain, int search)
80 struct block_symbol sym;
82 sym = lookup_symbol_in_static_block (name, block, domain);
83 if (sym.symbol != NULL)
86 sym = lookup_global_symbol (name, block, domain);
88 if (sym.symbol != NULL)
93 char *classname, *nested;
94 unsigned int prefix_len;
95 struct cleanup *cleanup;
96 struct block_symbol class_sym;
98 /* A simple lookup failed. Check if the symbol was defined in
101 cleanup = make_cleanup (null_cleanup, NULL);
103 /* Find the name of the class and the name of the method,
105 prefix_len = d_entire_prefix_len (name);
107 /* If no prefix was found, search "this". */
111 struct block_symbol lang_this;
113 lang_this = lookup_language_this (language_def (language_d), block);
114 if (lang_this.symbol == NULL)
116 do_cleanups (cleanup);
117 return (struct block_symbol) {NULL, NULL};
120 type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this.symbol)));
121 classname = xstrdup (TYPE_NAME (type));
122 nested = xstrdup (name);
126 /* The class name is everything up to and including PREFIX_LEN. */
127 classname = savestring (name, prefix_len);
129 /* The rest of the name is everything else past the initial scope
131 nested = xstrdup (name + prefix_len + 1);
134 /* Add cleanups to free memory for these strings. */
135 make_cleanup (xfree, classname);
136 make_cleanup (xfree, nested);
138 /* Lookup a class named CLASSNAME. If none is found, there is nothing
139 more that can be done. */
140 class_sym = lookup_global_symbol (classname, block, domain);
141 if (class_sym.symbol == NULL)
143 do_cleanups (cleanup);
144 return (struct block_symbol) {NULL, NULL};
147 /* Look for a symbol named NESTED in this class. */
148 sym = d_lookup_nested_symbol (SYMBOL_TYPE (class_sym.symbol),
150 do_cleanups (cleanup);
156 /* Look up NAME in the D module MODULE. Other arguments are as in
157 d_lookup_symbol_nonlocal. If SEARCH is non-zero, search through
158 base classes for a matching symbol. */
160 static struct block_symbol
161 d_lookup_symbol_in_module (const char *module, const char *name,
162 const struct block *block,
163 const domain_enum domain, int search)
165 char *concatenated_name = NULL;
167 if (module[0] != '\0')
170 = (char *) alloca (strlen (module) + strlen (name) + 2);
171 strcpy (concatenated_name, module);
172 strcat (concatenated_name, ".");
173 strcat (concatenated_name, name);
174 name = concatenated_name;
177 return d_lookup_symbol (name, block, domain, search);
180 /* Lookup NAME at module scope. SCOPE is the module that the current
181 function is defined within; only consider modules whose length is at
182 least SCOPE_LEN. Other arguments are as in d_lookup_symbol_nonlocal.
184 For example, if we're within a function A.B.f and looking for a
185 symbol x, this will get called with NAME = "x", SCOPE = "A.B", and
186 SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same,
187 but with SCOPE_LEN = 1. And then it calls itself with NAME and
188 SCOPE the same, but with SCOPE_LEN = 4. This third call looks for
189 "A.B.x"; if it doesn't find it, then the second call looks for "A.x",
190 and if that call fails, then the first call looks for "x". */
192 static struct block_symbol
193 lookup_module_scope (const char *name, const struct block *block,
194 const domain_enum domain, const char *scope,
199 if (scope[scope_len] != '\0')
201 /* Recursively search for names in child modules first. */
203 struct block_symbol sym;
204 int new_scope_len = scope_len;
206 /* If the current scope is followed by ".", skip past that. */
207 if (new_scope_len != 0)
209 gdb_assert (scope[new_scope_len] == '.');
212 new_scope_len += d_find_first_component (scope + new_scope_len);
213 sym = lookup_module_scope (name, block, domain,
214 scope, new_scope_len);
215 if (sym.symbol != NULL)
219 /* Okay, we didn't find a match in our children, so look for the
220 name in the current module. */
222 module = (char *) alloca (scope_len + 1);
223 strncpy (module, scope, scope_len);
224 module[scope_len] = '\0';
225 return d_lookup_symbol_in_module (module, name,
229 /* Search through the base classes of PARENT_TYPE for a symbol named
230 NAME in block BLOCK. */
232 static struct block_symbol
233 find_symbol_in_baseclass (struct type *parent_type, const char *name,
234 const struct block *block)
236 char *concatenated_name = NULL;
237 struct block_symbol sym;
238 struct cleanup *cleanup;
243 cleanup = make_cleanup (free_current_contents, &concatenated_name);
245 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
248 struct type *base_type = TYPE_BASECLASS (parent_type, i);
249 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
251 if (base_name == NULL)
254 /* Search this particular base class. */
255 sym = d_lookup_symbol_in_module (base_name, name, block,
257 if (sym.symbol != NULL)
260 /* Now search all static file-level symbols. We have to do this for
261 things like typedefs in the class. First search in this symtab,
262 what we want is possibly there. */
263 len = strlen (base_name) + strlen (name) + 2;
264 concatenated_name = (char *) xrealloc (concatenated_name, len);
265 xsnprintf (concatenated_name, len, "%s.%s", base_name, name);
266 sym = lookup_symbol_in_static_block (concatenated_name, block,
268 if (sym.symbol != NULL)
271 /* Nope. We now have to search all static blocks in all objfiles,
272 even if block != NULL, because there's no guarantees as to which
273 symtab the symbol we want is in. */
274 sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
275 if (sym.symbol != NULL)
278 /* If this class has base classes, search them next. */
279 base_type = check_typedef (base_type);
280 if (TYPE_N_BASECLASSES (base_type) > 0)
282 sym = find_symbol_in_baseclass (base_type, name, block);
283 if (sym.symbol != NULL)
288 do_cleanups (cleanup);
292 /* Look up a symbol named NESTED_NAME that is nested inside the D
293 class or module given by PARENT_TYPE, from within the context
294 given by BLOCK. Return NULL if there is no such nested type. */
297 d_lookup_nested_symbol (struct type *parent_type,
298 const char *nested_name,
299 const struct block *block)
301 /* type_name_no_tag_required provides better error reporting using the
303 struct type *saved_parent_type = parent_type;
305 parent_type = check_typedef (parent_type);
307 switch (TYPE_CODE (parent_type))
309 case TYPE_CODE_STRUCT:
310 case TYPE_CODE_UNION:
312 case TYPE_CODE_MODULE:
315 const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
316 struct block_symbol sym
317 = d_lookup_symbol_in_module (parent_name, nested_name,
318 block, VAR_DOMAIN, 0);
319 char *concatenated_name;
321 if (sym.symbol != NULL)
324 /* Now search all static file-level symbols. We have to do this
325 for things like typedefs in the class. We do not try to
326 guess any imported module as even the fully specified
327 module search is already not D compliant and more assumptions
328 could make it too magic. */
329 size = strlen (parent_name) + strlen (nested_name) + 2;
330 concatenated_name = (char *) alloca (size);
332 xsnprintf (concatenated_name, size, "%s.%s",
333 parent_name, nested_name);
335 sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
336 if (sym.symbol != NULL)
339 /* If no matching symbols were found, try searching any
341 return find_symbol_in_baseclass (parent_type, nested_name, block);
345 case TYPE_CODE_METHOD:
346 return (struct block_symbol) {NULL, NULL};
349 gdb_assert_not_reached ("called with non-aggregate type.");
353 /* Used for cleanups to reset the "searched" flag incase
357 reset_directive_searched (void *data)
359 struct using_direct *direct = (struct using_direct *) data;
360 direct->searched = 0;
363 /* Search for NAME by applying all import statements belonging to
364 BLOCK which are applicable in SCOPE.
366 If SEARCH_PARENTS the search will include imports which are
367 applicable in parents of SCOPE.
376 If SCOPE is "A.B" and SEARCH_PARENTS is true, the imports of
377 modules X and Y will be considered. If SEARCH_PARENTS is false
378 only the import of Y is considered. */
380 static struct block_symbol
381 d_lookup_symbol_imports (const char *scope, const char *name,
382 const struct block *block,
383 const domain_enum domain,
384 const int search_parents)
386 struct using_direct *current;
387 struct block_symbol sym;
389 struct cleanup *searched_cleanup;
391 /* First, try to find the symbol in the given module. */
392 sym = d_lookup_symbol_in_module (scope, name, block, domain, 1);
394 if (sym.symbol != NULL)
397 /* Go through the using directives. If any of them add new names to
398 the module we're searching in, see if we can find a match by
401 for (current = block_using (block);
403 current = current->next)
405 const char **excludep;
406 int len = strlen (current->import_dest);
408 directive_match = (search_parents
409 ? (strncmp (scope, current->import_dest, len) == 0
412 || scope[len] == '\0'))
413 : strcmp (scope, current->import_dest) == 0);
415 /* If the import destination is the current scope or one of its
416 ancestors then it is applicable. */
417 if (directive_match && !current->searched)
419 /* Mark this import as searched so that the recursive call
420 does not search it again. */
421 current->searched = 1;
422 searched_cleanup = make_cleanup (reset_directive_searched,
425 /* If there is an import of a single declaration, compare the
426 imported declaration (after optional renaming by its alias)
427 with the sought out name. If there is a match pass
428 current->import_src as MODULE to direct the search towards
429 the imported module. */
430 if (current->declaration
431 && strcmp (name, current->alias
432 ? current->alias : current->declaration) == 0)
433 sym = d_lookup_symbol_in_module (current->import_src,
434 current->declaration,
437 /* If a symbol was found or this import statement was an import
438 declaration, the search of this import is complete. */
439 if (sym.symbol != NULL || current->declaration)
441 current->searched = 0;
442 discard_cleanups (searched_cleanup);
444 if (sym.symbol != NULL)
450 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
451 for (excludep = current->excludes; *excludep; excludep++)
452 if (strcmp (name, *excludep) == 0)
456 discard_cleanups (searched_cleanup);
460 /* If the import statement is creating an alias. */
461 if (current->alias != NULL)
463 if (strcmp (name, current->alias) == 0)
465 /* If the alias matches the sought name. Pass
466 current->import_src as the NAME to direct the
467 search towards the aliased module. */
468 sym = lookup_module_scope (current->import_src, block,
473 /* If the alias matches the first component of the
474 sought name, pass current->import_src as MODULE
475 to direct the search, skipping over the aliased
476 component in NAME. */
477 int name_scope = d_find_first_component (name);
479 if (name[name_scope] != '\0'
480 && strncmp (name, current->alias, name_scope) == 0)
484 sym = d_lookup_symbol_imports (current->import_src,
492 /* If this import statement creates no alias, pass
493 current->import_src as MODULE to direct the search
494 towards the imported module. */
495 sym = d_lookup_symbol_imports (current->import_src,
496 name, block, domain, 0);
498 current->searched = 0;
499 discard_cleanups (searched_cleanup);
501 if (sym.symbol != NULL)
506 return (struct block_symbol) {NULL, NULL};
509 /* Searches for NAME in the current module, and by applying relevant
510 import statements belonging to BLOCK and its parents. SCOPE is the
511 module scope of the context in which the search is being evaluated. */
513 static struct block_symbol
514 d_lookup_symbol_module (const char *scope, const char *name,
515 const struct block *block,
516 const domain_enum domain)
518 struct block_symbol sym;
520 /* First, try to find the symbol in the given module. */
521 sym = d_lookup_symbol_in_module (scope, name,
523 if (sym.symbol != NULL)
526 /* Search for name in modules imported to this and parent
528 while (block != NULL)
530 sym = d_lookup_symbol_imports (scope, name, block, domain, 1);
532 if (sym.symbol != NULL)
535 block = BLOCK_SUPERBLOCK (block);
538 return (struct block_symbol) {NULL, NULL};
541 /* The D-specific version of name lookup for static and global names
542 This makes sure that names get looked for in all modules that are
543 in scope. NAME is the natural name of the symbol that we're looking
544 looking for, BLOCK is the block that we're searching within, DOMAIN
545 says what kind of symbols we're looking for, and if SYMTAB is non-NULL,
546 we should store the symtab where we found the symbol in it. */
549 d_lookup_symbol_nonlocal (const struct language_defn *langdef,
551 const struct block *block,
552 const domain_enum domain)
554 struct block_symbol sym;
555 const char *scope = block_scope (block);
557 sym = lookup_module_scope (name, block, domain, scope, 0);
558 if (sym.symbol != NULL)
561 return d_lookup_symbol_module (scope, name, block, domain);