[D] Support looking up symbols in the current and imported modules.
[deliverable/binutils-gdb.git] / gdb / d-namespace.c
CommitLineData
bc7c9fab
IB
1/* Helper routines for D support in GDB.
2
3 Copyright (C) 2014-2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
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 "gdb_obstack.h"
27
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. */
33
34/* The character in NAME indexed by the return value is guaranteed to
35 always be either '.' or '\0'. */
36
37static unsigned int
38d_find_first_component (const char *name)
39{
40 unsigned int index = 0;
41
42 for (;; ++index)
43 {
44 if (name[index] == '.' || name[index] == '\0')
45 return index;
46 }
47}
48
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. */
53
54static unsigned int
55d_entire_prefix_len (const char *name)
56{
57 unsigned int current_len = d_find_first_component (name);
58 unsigned int previous_len = 0;
59
60 while (name[current_len] != '\0')
61 {
62 gdb_assert (name[current_len] == '.');
63 previous_len = current_len;
64 /* Skip the '.' */
65 current_len++;
66 current_len += d_find_first_component (name + current_len);
67 }
68
69 return previous_len;
70}
71
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. */
75
76static struct block_symbol
77d_lookup_symbol (const char *name, const struct block *block,
78 const domain_enum domain, int search)
79{
80 struct block_symbol sym;
81
82 sym = lookup_symbol_in_static_block (name, block, domain);
83 if (sym.symbol != NULL)
84 return sym;
85
86 sym = lookup_global_symbol (name, block, domain);
87
88 if (sym.symbol != NULL)
89 return sym;
90
91 if (search)
92 {
93 char *classname, *nested;
94 unsigned int prefix_len;
95 struct cleanup *cleanup;
96 struct block_symbol class_sym;
97
98 /* A simple lookup failed. Check if the symbol was defined in
99 a base class. */
100
101 cleanup = make_cleanup (null_cleanup, NULL);
102
103 /* Find the name of the class and the name of the method,
104 variable, etc. */
105 prefix_len = d_entire_prefix_len (name);
106
107 /* If no prefix was found, search "this". */
108 if (prefix_len == 0)
109 {
110 struct type *type;
111 struct block_symbol lang_this;
112
113 lang_this = lookup_language_this (language_def (language_d), block);
114 if (lang_this.symbol == NULL)
115 {
116 do_cleanups (cleanup);
117 return (struct block_symbol) {NULL, NULL};
118 }
119
120 type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this.symbol)));
121 classname = xstrdup (TYPE_NAME (type));
122 nested = xstrdup (name);
123 }
124 else
125 {
126 /* The class name is everything up to and including PREFIX_LEN. */
127 classname = savestring (name, prefix_len);
128
129 /* The rest of the name is everything else past the initial scope
130 operator. */
131 nested = xstrdup (name + prefix_len + 1);
132 }
133
134 /* Add cleanups to free memory for these strings. */
135 make_cleanup (xfree, classname);
136 make_cleanup (xfree, nested);
137
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)
142 {
143 do_cleanups (cleanup);
144 return (struct block_symbol) {NULL, NULL};
145 }
146
147 /* Look for a symbol named NESTED in this class. */
148 sym = d_lookup_nested_symbol (SYMBOL_TYPE (class_sym.symbol),
149 nested, block);
150 do_cleanups (cleanup);
151 }
152
153 return sym;
154}
155
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. */
159
160static struct block_symbol
161d_lookup_symbol_in_module (const char *module, const char *name,
162 const struct block *block,
163 const domain_enum domain, int search)
164{
165 char *concatenated_name = NULL;
166
167 if (module[0] != '\0')
168 {
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;
175 }
176
177 return d_lookup_symbol (name, block, domain, search);
178}
179
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.
183
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". */
191
192static struct block_symbol
193lookup_module_scope (const char *name, const struct block *block,
194 const domain_enum domain, const char *scope,
195 int scope_len)
196{
197 char *module;
198
199 if (scope[scope_len] != '\0')
200 {
201 /* Recursively search for names in child modules first. */
202
203 struct block_symbol sym;
204 int new_scope_len = scope_len;
205
206 /* If the current scope is followed by ".", skip past that. */
207 if (new_scope_len != 0)
208 {
209 gdb_assert (scope[new_scope_len] == '.');
210 new_scope_len++;
211 }
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)
216 return sym;
217 }
218
219 /* Okay, we didn't find a match in our children, so look for the
220 name in the current module. */
221
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,
226 block, domain, 1);
227}
228
229/* Search through the base classes of PARENT_TYPE for a symbol named
230 NAME in block BLOCK. */
231
232static struct block_symbol
233find_symbol_in_baseclass (struct type *parent_type, const char *name,
234 const struct block *block)
235{
236 char *concatenated_name = NULL;
237 struct block_symbol sym;
238 struct cleanup *cleanup;
239 int i;
240
241 sym.symbol = NULL;
242 sym.block = NULL;
243 cleanup = make_cleanup (free_current_contents, &concatenated_name);
244
245 for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
246 {
247 size_t len;
248 struct type *base_type = TYPE_BASECLASS (parent_type, i);
249 const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);
250
251 if (base_name == NULL)
252 continue;
253
254 /* Search this particular base class. */
255 sym = d_lookup_symbol_in_module (base_name, name, block,
256 VAR_DOMAIN, 0);
257 if (sym.symbol != NULL)
258 break;
259
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,
267 VAR_DOMAIN);
268 if (sym.symbol != NULL)
269 break;
270
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)
276 break;
277
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)
281 {
282 sym = find_symbol_in_baseclass (base_type, name, block);
283 if (sym.symbol != NULL)
284 break;
285 }
286 }
287
288 do_cleanups (cleanup);
289 return sym;
290}
291
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. */
295
296struct block_symbol
297d_lookup_nested_symbol (struct type *parent_type,
298 const char *nested_name,
299 const struct block *block)
300{
301 /* type_name_no_tag_required provides better error reporting using the
302 original type. */
303 struct type *saved_parent_type = parent_type;
304
305 parent_type = check_typedef (parent_type);
306
307 switch (TYPE_CODE (parent_type))
308 {
309 case TYPE_CODE_STRUCT:
310 case TYPE_CODE_UNION:
311 case TYPE_CODE_MODULE:
312 {
313 int size;
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;
319
320 if (sym.symbol != NULL)
321 return sym;
322
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);
330
331 xsnprintf (concatenated_name, size, "%s.%s",
332 parent_name, nested_name);
333
334 sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
335 if (sym.symbol != NULL)
336 return sym;
337
338 /* If no matching symbols were found, try searching any
339 base classes. */
340 return find_symbol_in_baseclass (parent_type, nested_name, block);
341 }
342
343 case TYPE_CODE_FUNC:
344 case TYPE_CODE_METHOD:
345 return (struct block_symbol) {NULL, NULL};
346
347 default:
348 gdb_assert_not_reached ("called with non-aggregate type.");
349 }
350}
351
352/* Used for cleanups to reset the "searched" flag incase
353 of an error. */
354
355static void
356reset_directive_searched (void *data)
357{
358 struct using_direct *direct = data;
359 direct->searched = 0;
360}
361
362/* Search for NAME by applying all import statements belonging to
363 BLOCK which are applicable in SCOPE.
364
365 If SEARCH_PARENTS the search will include imports which are
366 applicable in parents of SCOPE.
367 Example:
368
369 module A;
370 import X;
371 void B() {
372 import Y;
373 }
374
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. */
378
379static struct block_symbol
380d_lookup_symbol_imports (const char *scope, const char *name,
381 const struct block *block,
382 const domain_enum domain,
383 const int search_parents)
384{
385 struct using_direct *current;
386 struct block_symbol sym;
387 int directive_match;
388 struct cleanup *searched_cleanup;
389
390 /* First, try to find the symbol in the given module. */
391 sym = d_lookup_symbol_in_module (scope, name, block, domain, 1);
392
393 if (sym.symbol != NULL)
394 return sym;
395
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
398 applying them. */
399
400 for (current = block_using (block);
401 current != NULL;
402 current = current->next)
403 {
404 const char **excludep;
405 int len = strlen (current->import_dest);
406
407 directive_match = (search_parents
408 ? (strncmp (scope, current->import_dest, len) == 0
409 && (len == 0
410 || scope[len] == '.'
411 || scope[len] == '\0'))
412 : strcmp (scope, current->import_dest) == 0);
413
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)
417 {
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,
422 current);
423
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,
434 block, domain, 1);
435
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)
439 {
440 current->searched = 0;
441 discard_cleanups (searched_cleanup);
442
443 if (sym.symbol != NULL)
444 return sym;
445
446 continue;
447 }
448
449 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
450 for (excludep = current->excludes; *excludep; excludep++)
451 if (strcmp (name, *excludep) == 0)
452 break;
453 if (*excludep)
454 {
455 discard_cleanups (searched_cleanup);
456 continue;
457 }
458
459 /* If the import statement is creating an alias. */
460 if (current->alias != NULL)
461 {
462 if (strcmp (name, current->alias) == 0)
463 {
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,
468 domain, scope, 0);
469 }
470 else
471 {
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);
477
478 if (name[name_scope] != '\0'
479 && strncmp (name, current->alias, name_scope) == 0)
480 {
481 /* Skip the '.' */
482 name_scope++;
483 sym = d_lookup_symbol_imports (current->import_src,
484 name + name_scope,
485 block, domain, 0);
486 }
487 }
488 }
489 else
490 {
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);
496 }
497 current->searched = 0;
498 discard_cleanups (searched_cleanup);
499
500 if (sym.symbol != NULL)
501 return sym;
502 }
503 }
504
505 return (struct block_symbol) {NULL, NULL};
506}
507
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. */
511
512static struct block_symbol
513d_lookup_symbol_module (const char *scope, const char *name,
514 const struct block *block,
515 const domain_enum domain)
516{
517 struct block_symbol sym;
518
519 /* First, try to find the symbol in the given module. */
520 sym = d_lookup_symbol_in_module (scope, name,
521 block, domain, 1);
522 if (sym.symbol != NULL)
523 return sym;
524
525 /* Search for name in modules imported to this and parent
526 blocks. */
527 while (block != NULL)
528 {
529 sym = d_lookup_symbol_imports (scope, name, block, domain, 1);
530
531 if (sym.symbol != NULL)
532 return sym;
533
534 block = BLOCK_SUPERBLOCK (block);
535 }
536
537 return (struct block_symbol) {NULL, NULL};
538}
539
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. */
546
547struct block_symbol
548d_lookup_symbol_nonlocal (const struct language_defn *langdef,
549 const char *name,
550 const struct block *block,
551 const domain_enum domain)
552{
553 struct block_symbol sym;
554 const char *scope = block_scope (block);
555
556 sym = lookup_module_scope (name, block, domain, scope, 0);
557 if (sym.symbol != NULL)
558 return sym;
559
560 return d_lookup_symbol_module (scope, name, block, domain);
561}
562
This page took 0.043833 seconds and 4 git commands to generate.