]>
Commit | Line | Data |
---|---|---|
bc7c9fab IB |
1 | /* Helper routines for D support in GDB. |
2 | ||
618f726f | 3 | Copyright (C) 2014-2016 Free Software Foundation, Inc. |
bc7c9fab IB |
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 | ||
37 | static unsigned int | |
38 | d_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 | ||
54 | static unsigned int | |
55 | d_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 | ||
76 | static struct block_symbol | |
ba587d55 IB |
77 | d_lookup_symbol (const struct language_defn *langdef, |
78 | const char *name, const struct block *block, | |
bc7c9fab IB |
79 | const domain_enum domain, int search) |
80 | { | |
81 | struct block_symbol sym; | |
82 | ||
83 | sym = lookup_symbol_in_static_block (name, block, domain); | |
84 | if (sym.symbol != NULL) | |
85 | return sym; | |
86 | ||
ba587d55 IB |
87 | /* If we didn't find a definition for a builtin type in the static block, |
88 | such as "ucent" which is a specialist type, search for it now. */ | |
89 | if (langdef != NULL && domain == VAR_DOMAIN) | |
90 | { | |
91 | struct gdbarch *gdbarch; | |
92 | ||
93 | if (block == NULL) | |
94 | gdbarch = target_gdbarch (); | |
95 | else | |
96 | gdbarch = block_gdbarch (block); | |
97 | sym.symbol | |
98 | = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name); | |
99 | sym.block = NULL; | |
100 | if (sym.symbol != NULL) | |
101 | return sym; | |
102 | } | |
103 | ||
bc7c9fab IB |
104 | sym = lookup_global_symbol (name, block, domain); |
105 | ||
106 | if (sym.symbol != NULL) | |
107 | return sym; | |
108 | ||
109 | if (search) | |
110 | { | |
111 | char *classname, *nested; | |
112 | unsigned int prefix_len; | |
113 | struct cleanup *cleanup; | |
114 | struct block_symbol class_sym; | |
115 | ||
116 | /* A simple lookup failed. Check if the symbol was defined in | |
117 | a base class. */ | |
118 | ||
119 | cleanup = make_cleanup (null_cleanup, NULL); | |
120 | ||
121 | /* Find the name of the class and the name of the method, | |
122 | variable, etc. */ | |
123 | prefix_len = d_entire_prefix_len (name); | |
124 | ||
125 | /* If no prefix was found, search "this". */ | |
126 | if (prefix_len == 0) | |
127 | { | |
128 | struct type *type; | |
129 | struct block_symbol lang_this; | |
130 | ||
131 | lang_this = lookup_language_this (language_def (language_d), block); | |
132 | if (lang_this.symbol == NULL) | |
133 | { | |
134 | do_cleanups (cleanup); | |
b6b80672 | 135 | return null_block_symbol; |
bc7c9fab IB |
136 | } |
137 | ||
138 | type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this.symbol))); | |
139 | classname = xstrdup (TYPE_NAME (type)); | |
140 | nested = xstrdup (name); | |
141 | } | |
142 | else | |
143 | { | |
144 | /* The class name is everything up to and including PREFIX_LEN. */ | |
145 | classname = savestring (name, prefix_len); | |
146 | ||
147 | /* The rest of the name is everything else past the initial scope | |
148 | operator. */ | |
149 | nested = xstrdup (name + prefix_len + 1); | |
150 | } | |
151 | ||
152 | /* Add cleanups to free memory for these strings. */ | |
153 | make_cleanup (xfree, classname); | |
154 | make_cleanup (xfree, nested); | |
155 | ||
156 | /* Lookup a class named CLASSNAME. If none is found, there is nothing | |
157 | more that can be done. */ | |
158 | class_sym = lookup_global_symbol (classname, block, domain); | |
159 | if (class_sym.symbol == NULL) | |
160 | { | |
161 | do_cleanups (cleanup); | |
b6b80672 | 162 | return null_block_symbol; |
bc7c9fab IB |
163 | } |
164 | ||
165 | /* Look for a symbol named NESTED in this class. */ | |
166 | sym = d_lookup_nested_symbol (SYMBOL_TYPE (class_sym.symbol), | |
167 | nested, block); | |
168 | do_cleanups (cleanup); | |
169 | } | |
170 | ||
171 | return sym; | |
172 | } | |
173 | ||
174 | /* Look up NAME in the D module MODULE. Other arguments are as in | |
175 | d_lookup_symbol_nonlocal. If SEARCH is non-zero, search through | |
176 | base classes for a matching symbol. */ | |
177 | ||
178 | static struct block_symbol | |
179 | d_lookup_symbol_in_module (const char *module, const char *name, | |
180 | const struct block *block, | |
181 | const domain_enum domain, int search) | |
182 | { | |
183 | char *concatenated_name = NULL; | |
184 | ||
185 | if (module[0] != '\0') | |
186 | { | |
224c3ddb SM |
187 | concatenated_name |
188 | = (char *) alloca (strlen (module) + strlen (name) + 2); | |
bc7c9fab IB |
189 | strcpy (concatenated_name, module); |
190 | strcat (concatenated_name, "."); | |
191 | strcat (concatenated_name, name); | |
192 | name = concatenated_name; | |
193 | } | |
194 | ||
ba587d55 | 195 | return d_lookup_symbol (NULL, name, block, domain, search); |
bc7c9fab IB |
196 | } |
197 | ||
198 | /* Lookup NAME at module scope. SCOPE is the module that the current | |
199 | function is defined within; only consider modules whose length is at | |
200 | least SCOPE_LEN. Other arguments are as in d_lookup_symbol_nonlocal. | |
201 | ||
202 | For example, if we're within a function A.B.f and looking for a | |
203 | symbol x, this will get called with NAME = "x", SCOPE = "A.B", and | |
204 | SCOPE_LEN = 0. It then calls itself with NAME and SCOPE the same, | |
205 | but with SCOPE_LEN = 1. And then it calls itself with NAME and | |
206 | SCOPE the same, but with SCOPE_LEN = 4. This third call looks for | |
207 | "A.B.x"; if it doesn't find it, then the second call looks for "A.x", | |
208 | and if that call fails, then the first call looks for "x". */ | |
209 | ||
210 | static struct block_symbol | |
ba587d55 IB |
211 | lookup_module_scope (const struct language_defn *langdef, |
212 | const char *name, const struct block *block, | |
bc7c9fab IB |
213 | const domain_enum domain, const char *scope, |
214 | int scope_len) | |
215 | { | |
216 | char *module; | |
217 | ||
218 | if (scope[scope_len] != '\0') | |
219 | { | |
220 | /* Recursively search for names in child modules first. */ | |
221 | ||
222 | struct block_symbol sym; | |
223 | int new_scope_len = scope_len; | |
224 | ||
225 | /* If the current scope is followed by ".", skip past that. */ | |
226 | if (new_scope_len != 0) | |
227 | { | |
228 | gdb_assert (scope[new_scope_len] == '.'); | |
229 | new_scope_len++; | |
230 | } | |
231 | new_scope_len += d_find_first_component (scope + new_scope_len); | |
ba587d55 | 232 | sym = lookup_module_scope (langdef, name, block, domain, |
bc7c9fab IB |
233 | scope, new_scope_len); |
234 | if (sym.symbol != NULL) | |
235 | return sym; | |
236 | } | |
237 | ||
238 | /* Okay, we didn't find a match in our children, so look for the | |
ba587d55 IB |
239 | name in the current module. |
240 | ||
241 | If we there is no scope and we know we have a bare symbol, then short | |
242 | circuit everything and call d_lookup_symbol directly. | |
243 | This isn't an optimization, rather it allows us to pass LANGDEF which | |
244 | is needed for primitive type lookup. */ | |
245 | ||
246 | if (scope_len == 0 && strchr (name, '.') == NULL) | |
247 | return d_lookup_symbol (langdef, name, block, domain, 1); | |
bc7c9fab | 248 | |
224c3ddb | 249 | module = (char *) alloca (scope_len + 1); |
bc7c9fab IB |
250 | strncpy (module, scope, scope_len); |
251 | module[scope_len] = '\0'; | |
252 | return d_lookup_symbol_in_module (module, name, | |
253 | block, domain, 1); | |
254 | } | |
255 | ||
256 | /* Search through the base classes of PARENT_TYPE for a symbol named | |
257 | NAME in block BLOCK. */ | |
258 | ||
259 | static struct block_symbol | |
260 | find_symbol_in_baseclass (struct type *parent_type, const char *name, | |
261 | const struct block *block) | |
262 | { | |
263 | char *concatenated_name = NULL; | |
264 | struct block_symbol sym; | |
265 | struct cleanup *cleanup; | |
266 | int i; | |
267 | ||
268 | sym.symbol = NULL; | |
269 | sym.block = NULL; | |
270 | cleanup = make_cleanup (free_current_contents, &concatenated_name); | |
271 | ||
272 | for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i) | |
273 | { | |
274 | size_t len; | |
275 | struct type *base_type = TYPE_BASECLASS (parent_type, i); | |
276 | const char *base_name = TYPE_BASECLASS_NAME (parent_type, i); | |
277 | ||
278 | if (base_name == NULL) | |
279 | continue; | |
280 | ||
281 | /* Search this particular base class. */ | |
282 | sym = d_lookup_symbol_in_module (base_name, name, block, | |
283 | VAR_DOMAIN, 0); | |
284 | if (sym.symbol != NULL) | |
285 | break; | |
286 | ||
287 | /* Now search all static file-level symbols. We have to do this for | |
288 | things like typedefs in the class. First search in this symtab, | |
289 | what we want is possibly there. */ | |
290 | len = strlen (base_name) + strlen (name) + 2; | |
224c3ddb | 291 | concatenated_name = (char *) xrealloc (concatenated_name, len); |
bc7c9fab IB |
292 | xsnprintf (concatenated_name, len, "%s.%s", base_name, name); |
293 | sym = lookup_symbol_in_static_block (concatenated_name, block, | |
294 | VAR_DOMAIN); | |
295 | if (sym.symbol != NULL) | |
296 | break; | |
297 | ||
298 | /* Nope. We now have to search all static blocks in all objfiles, | |
299 | even if block != NULL, because there's no guarantees as to which | |
300 | symtab the symbol we want is in. */ | |
301 | sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN); | |
302 | if (sym.symbol != NULL) | |
303 | break; | |
304 | ||
305 | /* If this class has base classes, search them next. */ | |
306 | base_type = check_typedef (base_type); | |
307 | if (TYPE_N_BASECLASSES (base_type) > 0) | |
308 | { | |
309 | sym = find_symbol_in_baseclass (base_type, name, block); | |
310 | if (sym.symbol != NULL) | |
311 | break; | |
312 | } | |
313 | } | |
314 | ||
315 | do_cleanups (cleanup); | |
316 | return sym; | |
317 | } | |
318 | ||
319 | /* Look up a symbol named NESTED_NAME that is nested inside the D | |
320 | class or module given by PARENT_TYPE, from within the context | |
321 | given by BLOCK. Return NULL if there is no such nested type. */ | |
322 | ||
323 | struct block_symbol | |
324 | d_lookup_nested_symbol (struct type *parent_type, | |
325 | const char *nested_name, | |
326 | const struct block *block) | |
327 | { | |
328 | /* type_name_no_tag_required provides better error reporting using the | |
329 | original type. */ | |
330 | struct type *saved_parent_type = parent_type; | |
331 | ||
332 | parent_type = check_typedef (parent_type); | |
333 | ||
334 | switch (TYPE_CODE (parent_type)) | |
335 | { | |
336 | case TYPE_CODE_STRUCT: | |
337 | case TYPE_CODE_UNION: | |
7f3706eb | 338 | case TYPE_CODE_ENUM: |
bc7c9fab IB |
339 | case TYPE_CODE_MODULE: |
340 | { | |
341 | int size; | |
342 | const char *parent_name = type_name_no_tag_or_error (saved_parent_type); | |
343 | struct block_symbol sym | |
344 | = d_lookup_symbol_in_module (parent_name, nested_name, | |
345 | block, VAR_DOMAIN, 0); | |
346 | char *concatenated_name; | |
347 | ||
348 | if (sym.symbol != NULL) | |
349 | return sym; | |
350 | ||
351 | /* Now search all static file-level symbols. We have to do this | |
352 | for things like typedefs in the class. We do not try to | |
353 | guess any imported module as even the fully specified | |
354 | module search is already not D compliant and more assumptions | |
355 | could make it too magic. */ | |
356 | size = strlen (parent_name) + strlen (nested_name) + 2; | |
224c3ddb | 357 | concatenated_name = (char *) alloca (size); |
bc7c9fab IB |
358 | |
359 | xsnprintf (concatenated_name, size, "%s.%s", | |
360 | parent_name, nested_name); | |
361 | ||
362 | sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN); | |
363 | if (sym.symbol != NULL) | |
364 | return sym; | |
365 | ||
366 | /* If no matching symbols were found, try searching any | |
367 | base classes. */ | |
368 | return find_symbol_in_baseclass (parent_type, nested_name, block); | |
369 | } | |
370 | ||
371 | case TYPE_CODE_FUNC: | |
372 | case TYPE_CODE_METHOD: | |
b6b80672 | 373 | return null_block_symbol; |
bc7c9fab IB |
374 | |
375 | default: | |
376 | gdb_assert_not_reached ("called with non-aggregate type."); | |
377 | } | |
378 | } | |
379 | ||
380 | /* Used for cleanups to reset the "searched" flag incase | |
381 | of an error. */ | |
382 | ||
383 | static void | |
384 | reset_directive_searched (void *data) | |
385 | { | |
9a3c8263 | 386 | struct using_direct *direct = (struct using_direct *) data; |
bc7c9fab IB |
387 | direct->searched = 0; |
388 | } | |
389 | ||
390 | /* Search for NAME by applying all import statements belonging to | |
30a6a7f0 | 391 | BLOCK which are applicable in SCOPE. */ |
bc7c9fab IB |
392 | |
393 | static struct block_symbol | |
394 | d_lookup_symbol_imports (const char *scope, const char *name, | |
395 | const struct block *block, | |
30a6a7f0 | 396 | const domain_enum domain) |
bc7c9fab IB |
397 | { |
398 | struct using_direct *current; | |
399 | struct block_symbol sym; | |
bc7c9fab IB |
400 | struct cleanup *searched_cleanup; |
401 | ||
402 | /* First, try to find the symbol in the given module. */ | |
403 | sym = d_lookup_symbol_in_module (scope, name, block, domain, 1); | |
404 | ||
405 | if (sym.symbol != NULL) | |
406 | return sym; | |
407 | ||
408 | /* Go through the using directives. If any of them add new names to | |
409 | the module we're searching in, see if we can find a match by | |
410 | applying them. */ | |
411 | ||
412 | for (current = block_using (block); | |
413 | current != NULL; | |
414 | current = current->next) | |
415 | { | |
416 | const char **excludep; | |
30a6a7f0 IB |
417 | |
418 | /* If the import destination is the current scope then search it. */ | |
419 | if (!current->searched && strcmp (scope, current->import_dest) == 0) | |
bc7c9fab IB |
420 | { |
421 | /* Mark this import as searched so that the recursive call | |
422 | does not search it again. */ | |
423 | current->searched = 1; | |
424 | searched_cleanup = make_cleanup (reset_directive_searched, | |
425 | current); | |
426 | ||
427 | /* If there is an import of a single declaration, compare the | |
428 | imported declaration (after optional renaming by its alias) | |
429 | with the sought out name. If there is a match pass | |
430 | current->import_src as MODULE to direct the search towards | |
431 | the imported module. */ | |
432 | if (current->declaration | |
433 | && strcmp (name, current->alias | |
434 | ? current->alias : current->declaration) == 0) | |
435 | sym = d_lookup_symbol_in_module (current->import_src, | |
436 | current->declaration, | |
437 | block, domain, 1); | |
438 | ||
439 | /* If a symbol was found or this import statement was an import | |
440 | declaration, the search of this import is complete. */ | |
441 | if (sym.symbol != NULL || current->declaration) | |
442 | { | |
443 | current->searched = 0; | |
444 | discard_cleanups (searched_cleanup); | |
445 | ||
446 | if (sym.symbol != NULL) | |
447 | return sym; | |
448 | ||
449 | continue; | |
450 | } | |
451 | ||
452 | /* Do not follow CURRENT if NAME matches its EXCLUDES. */ | |
453 | for (excludep = current->excludes; *excludep; excludep++) | |
454 | if (strcmp (name, *excludep) == 0) | |
455 | break; | |
456 | if (*excludep) | |
457 | { | |
458 | discard_cleanups (searched_cleanup); | |
459 | continue; | |
460 | } | |
461 | ||
462 | /* If the import statement is creating an alias. */ | |
463 | if (current->alias != NULL) | |
464 | { | |
465 | if (strcmp (name, current->alias) == 0) | |
466 | { | |
467 | /* If the alias matches the sought name. Pass | |
468 | current->import_src as the NAME to direct the | |
469 | search towards the aliased module. */ | |
ba587d55 | 470 | sym = lookup_module_scope (NULL, current->import_src, block, |
bc7c9fab IB |
471 | domain, scope, 0); |
472 | } | |
473 | else | |
474 | { | |
475 | /* If the alias matches the first component of the | |
476 | sought name, pass current->import_src as MODULE | |
477 | to direct the search, skipping over the aliased | |
478 | component in NAME. */ | |
479 | int name_scope = d_find_first_component (name); | |
480 | ||
481 | if (name[name_scope] != '\0' | |
482 | && strncmp (name, current->alias, name_scope) == 0) | |
483 | { | |
484 | /* Skip the '.' */ | |
485 | name_scope++; | |
38899f16 IB |
486 | sym = d_lookup_symbol_in_module (current->import_src, |
487 | name + name_scope, | |
488 | block, domain, 1); | |
bc7c9fab IB |
489 | } |
490 | } | |
491 | } | |
492 | else | |
493 | { | |
494 | /* If this import statement creates no alias, pass | |
495 | current->import_src as MODULE to direct the search | |
496 | towards the imported module. */ | |
38899f16 IB |
497 | sym = d_lookup_symbol_in_module (current->import_src, |
498 | name, block, domain, 1); | |
bc7c9fab IB |
499 | } |
500 | current->searched = 0; | |
501 | discard_cleanups (searched_cleanup); | |
502 | ||
503 | if (sym.symbol != NULL) | |
504 | return sym; | |
505 | } | |
506 | } | |
507 | ||
b6b80672 | 508 | return null_block_symbol; |
bc7c9fab IB |
509 | } |
510 | ||
511 | /* Searches for NAME in the current module, and by applying relevant | |
512 | import statements belonging to BLOCK and its parents. SCOPE is the | |
513 | module scope of the context in which the search is being evaluated. */ | |
514 | ||
515 | static struct block_symbol | |
516 | d_lookup_symbol_module (const char *scope, const char *name, | |
517 | const struct block *block, | |
518 | const domain_enum domain) | |
519 | { | |
520 | struct block_symbol sym; | |
521 | ||
522 | /* First, try to find the symbol in the given module. */ | |
523 | sym = d_lookup_symbol_in_module (scope, name, | |
524 | block, domain, 1); | |
525 | if (sym.symbol != NULL) | |
526 | return sym; | |
527 | ||
528 | /* Search for name in modules imported to this and parent | |
529 | blocks. */ | |
530 | while (block != NULL) | |
531 | { | |
30a6a7f0 | 532 | sym = d_lookup_symbol_imports (scope, name, block, domain); |
bc7c9fab IB |
533 | |
534 | if (sym.symbol != NULL) | |
535 | return sym; | |
536 | ||
537 | block = BLOCK_SUPERBLOCK (block); | |
538 | } | |
539 | ||
b6b80672 | 540 | return null_block_symbol; |
bc7c9fab IB |
541 | } |
542 | ||
543 | /* The D-specific version of name lookup for static and global names | |
544 | This makes sure that names get looked for in all modules that are | |
545 | in scope. NAME is the natural name of the symbol that we're looking | |
546 | looking for, BLOCK is the block that we're searching within, DOMAIN | |
547 | says what kind of symbols we're looking for, and if SYMTAB is non-NULL, | |
548 | we should store the symtab where we found the symbol in it. */ | |
549 | ||
550 | struct block_symbol | |
551 | d_lookup_symbol_nonlocal (const struct language_defn *langdef, | |
552 | const char *name, | |
553 | const struct block *block, | |
554 | const domain_enum domain) | |
555 | { | |
556 | struct block_symbol sym; | |
557 | const char *scope = block_scope (block); | |
558 | ||
ba587d55 | 559 | sym = lookup_module_scope (langdef, name, block, domain, scope, 0); |
bc7c9fab IB |
560 | if (sym.symbol != NULL) |
561 | return sym; | |
562 | ||
563 | return d_lookup_symbol_module (scope, name, block, domain); | |
564 | } | |
565 |