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')
169 concatenated_name = alloca (strlen (module)
170 + 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 = 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 = 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:
311 case TYPE_CODE_MODULE:
314 const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
315 struct block_symbol sym
316 = d_lookup_symbol_in_module (parent_name, nested_name,
317 block, VAR_DOMAIN, 0);
318 char *concatenated_name;
320 if (sym.symbol != NULL)
323 /* Now search all static file-level symbols. We have to do this
324 for things like typedefs in the class. We do not try to
325 guess any imported module as even the fully specified
326 module search is already not D compliant and more assumptions
327 could make it too magic. */
328 size = strlen (parent_name) + strlen (nested_name) + 2;
329 concatenated_name = alloca (size);
331 xsnprintf (concatenated_name, size, "%s.%s",
332 parent_name, nested_name);
334 sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
335 if (sym.symbol != NULL)
338 /* If no matching symbols were found, try searching any
340 return find_symbol_in_baseclass (parent_type, nested_name, block);
344 case TYPE_CODE_METHOD:
345 return (struct block_symbol) {NULL, NULL};
348 gdb_assert_not_reached ("called with non-aggregate type.");
352 /* Used for cleanups to reset the "searched" flag incase
356 reset_directive_searched (void *data)
358 struct using_direct *direct = data;
359 direct->searched = 0;
362 /* Search for NAME by applying all import statements belonging to
363 BLOCK which are applicable in SCOPE.
365 If SEARCH_PARENTS the search will include imports which are
366 applicable in parents of SCOPE.
375 If SCOPE is "A.B" and SEARCH_PARENTS is true, the imports of
376 modules X and Y will be considered. If SEARCH_PARENTS is false
377 only the import of Y is considered. */
379 static struct block_symbol
380 d_lookup_symbol_imports (const char *scope, const char *name,
381 const struct block *block,
382 const domain_enum domain,
383 const int search_parents)
385 struct using_direct *current;
386 struct block_symbol sym;
388 struct cleanup *searched_cleanup;
390 /* First, try to find the symbol in the given module. */
391 sym = d_lookup_symbol_in_module (scope, name, block, domain, 1);
393 if (sym.symbol != NULL)
396 /* Go through the using directives. If any of them add new names to
397 the module we're searching in, see if we can find a match by
400 for (current = block_using (block);
402 current = current->next)
404 const char **excludep;
405 int len = strlen (current->import_dest);
407 directive_match = (search_parents
408 ? (strncmp (scope, current->import_dest, len) == 0
411 || scope[len] == '\0'))
412 : strcmp (scope, current->import_dest) == 0);
414 /* If the import destination is the current scope or one of its
415 ancestors then it is applicable. */
416 if (directive_match && !current->searched)
418 /* Mark this import as searched so that the recursive call
419 does not search it again. */
420 current->searched = 1;
421 searched_cleanup = make_cleanup (reset_directive_searched,
424 /* If there is an import of a single declaration, compare the
425 imported declaration (after optional renaming by its alias)
426 with the sought out name. If there is a match pass
427 current->import_src as MODULE to direct the search towards
428 the imported module. */
429 if (current->declaration
430 && strcmp (name, current->alias
431 ? current->alias : current->declaration) == 0)
432 sym = d_lookup_symbol_in_module (current->import_src,
433 current->declaration,
436 /* If a symbol was found or this import statement was an import
437 declaration, the search of this import is complete. */
438 if (sym.symbol != NULL || current->declaration)
440 current->searched = 0;
441 discard_cleanups (searched_cleanup);
443 if (sym.symbol != NULL)
449 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
450 for (excludep = current->excludes; *excludep; excludep++)
451 if (strcmp (name, *excludep) == 0)
455 discard_cleanups (searched_cleanup);
459 /* If the import statement is creating an alias. */
460 if (current->alias != NULL)
462 if (strcmp (name, current->alias) == 0)
464 /* If the alias matches the sought name. Pass
465 current->import_src as the NAME to direct the
466 search towards the aliased module. */
467 sym = lookup_module_scope (current->import_src, block,
472 /* If the alias matches the first component of the
473 sought name, pass current->import_src as MODULE
474 to direct the search, skipping over the aliased
475 component in NAME. */
476 int name_scope = d_find_first_component (name);
478 if (name[name_scope] != '\0'
479 && strncmp (name, current->alias, name_scope) == 0)
483 sym = d_lookup_symbol_imports (current->import_src,
491 /* If this import statement creates no alias, pass
492 current->import_src as MODULE to direct the search
493 towards the imported module. */
494 sym = d_lookup_symbol_imports (current->import_src,
495 name, block, domain, 0);
497 current->searched = 0;
498 discard_cleanups (searched_cleanup);
500 if (sym.symbol != NULL)
505 return (struct block_symbol) {NULL, NULL};
508 /* Searches for NAME in the current module, and by applying relevant
509 import statements belonging to BLOCK and its parents. SCOPE is the
510 module scope of the context in which the search is being evaluated. */
512 static struct block_symbol
513 d_lookup_symbol_module (const char *scope, const char *name,
514 const struct block *block,
515 const domain_enum domain)
517 struct block_symbol sym;
519 /* First, try to find the symbol in the given module. */
520 sym = d_lookup_symbol_in_module (scope, name,
522 if (sym.symbol != NULL)
525 /* Search for name in modules imported to this and parent
527 while (block != NULL)
529 sym = d_lookup_symbol_imports (scope, name, block, domain, 1);
531 if (sym.symbol != NULL)
534 block = BLOCK_SUPERBLOCK (block);
537 return (struct block_symbol) {NULL, NULL};
540 /* The D-specific version of name lookup for static and global names
541 This makes sure that names get looked for in all modules that are
542 in scope. NAME is the natural name of the symbol that we're looking
543 looking for, BLOCK is the block that we're searching within, DOMAIN
544 says what kind of symbols we're looking for, and if SYMTAB is non-NULL,
545 we should store the symtab where we found the symbol in it. */
548 d_lookup_symbol_nonlocal (const struct language_defn *langdef,
550 const struct block *block,
551 const domain_enum domain)
553 struct block_symbol sym;
554 const char *scope = block_scope (block);
556 sym = lookup_module_scope (name, block, domain, scope, 0);
557 if (sym.symbol != NULL)
560 return d_lookup_symbol_module (scope, name, block, domain);