]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/d-namespace.c
Add casts to memory allocation related calls
[thirdparty/binutils-gdb.git] / gdb / d-namespace.c
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
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
77 d_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
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)
164 {
165 char *concatenated_name = NULL;
166
167 if (module[0] != '\0')
168 {
169 concatenated_name
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;
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
192 static struct block_symbol
193 lookup_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 = (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,
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
232 static struct block_symbol
233 find_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 = (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,
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
296 struct block_symbol
297 d_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_ENUM:
312 case TYPE_CODE_MODULE:
313 {
314 int size;
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;
320
321 if (sym.symbol != NULL)
322 return sym;
323
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);
331
332 xsnprintf (concatenated_name, size, "%s.%s",
333 parent_name, nested_name);
334
335 sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
336 if (sym.symbol != NULL)
337 return sym;
338
339 /* If no matching symbols were found, try searching any
340 base classes. */
341 return find_symbol_in_baseclass (parent_type, nested_name, block);
342 }
343
344 case TYPE_CODE_FUNC:
345 case TYPE_CODE_METHOD:
346 return (struct block_symbol) {NULL, NULL};
347
348 default:
349 gdb_assert_not_reached ("called with non-aggregate type.");
350 }
351 }
352
353 /* Used for cleanups to reset the "searched" flag incase
354 of an error. */
355
356 static void
357 reset_directive_searched (void *data)
358 {
359 struct using_direct *direct = data;
360 direct->searched = 0;
361 }
362
363 /* Search for NAME by applying all import statements belonging to
364 BLOCK which are applicable in SCOPE.
365
366 If SEARCH_PARENTS the search will include imports which are
367 applicable in parents of SCOPE.
368 Example:
369
370 module A;
371 import X;
372 void B() {
373 import Y;
374 }
375
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. */
379
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)
385 {
386 struct using_direct *current;
387 struct block_symbol sym;
388 int directive_match;
389 struct cleanup *searched_cleanup;
390
391 /* First, try to find the symbol in the given module. */
392 sym = d_lookup_symbol_in_module (scope, name, block, domain, 1);
393
394 if (sym.symbol != NULL)
395 return sym;
396
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
399 applying them. */
400
401 for (current = block_using (block);
402 current != NULL;
403 current = current->next)
404 {
405 const char **excludep;
406 int len = strlen (current->import_dest);
407
408 directive_match = (search_parents
409 ? (strncmp (scope, current->import_dest, len) == 0
410 && (len == 0
411 || scope[len] == '.'
412 || scope[len] == '\0'))
413 : strcmp (scope, current->import_dest) == 0);
414
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)
418 {
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,
423 current);
424
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,
435 block, domain, 1);
436
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)
440 {
441 current->searched = 0;
442 discard_cleanups (searched_cleanup);
443
444 if (sym.symbol != NULL)
445 return sym;
446
447 continue;
448 }
449
450 /* Do not follow CURRENT if NAME matches its EXCLUDES. */
451 for (excludep = current->excludes; *excludep; excludep++)
452 if (strcmp (name, *excludep) == 0)
453 break;
454 if (*excludep)
455 {
456 discard_cleanups (searched_cleanup);
457 continue;
458 }
459
460 /* If the import statement is creating an alias. */
461 if (current->alias != NULL)
462 {
463 if (strcmp (name, current->alias) == 0)
464 {
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,
469 domain, scope, 0);
470 }
471 else
472 {
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);
478
479 if (name[name_scope] != '\0'
480 && strncmp (name, current->alias, name_scope) == 0)
481 {
482 /* Skip the '.' */
483 name_scope++;
484 sym = d_lookup_symbol_imports (current->import_src,
485 name + name_scope,
486 block, domain, 0);
487 }
488 }
489 }
490 else
491 {
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);
497 }
498 current->searched = 0;
499 discard_cleanups (searched_cleanup);
500
501 if (sym.symbol != NULL)
502 return sym;
503 }
504 }
505
506 return (struct block_symbol) {NULL, NULL};
507 }
508
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. */
512
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)
517 {
518 struct block_symbol sym;
519
520 /* First, try to find the symbol in the given module. */
521 sym = d_lookup_symbol_in_module (scope, name,
522 block, domain, 1);
523 if (sym.symbol != NULL)
524 return sym;
525
526 /* Search for name in modules imported to this and parent
527 blocks. */
528 while (block != NULL)
529 {
530 sym = d_lookup_symbol_imports (scope, name, block, domain, 1);
531
532 if (sym.symbol != NULL)
533 return sym;
534
535 block = BLOCK_SUPERBLOCK (block);
536 }
537
538 return (struct block_symbol) {NULL, NULL};
539 }
540
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. */
547
548 struct block_symbol
549 d_lookup_symbol_nonlocal (const struct language_defn *langdef,
550 const char *name,
551 const struct block *block,
552 const domain_enum domain)
553 {
554 struct block_symbol sym;
555 const char *scope = block_scope (block);
556
557 sym = lookup_module_scope (name, block, domain, scope, 0);
558 if (sym.symbol != NULL)
559 return sym;
560
561 return d_lookup_symbol_module (scope, name, block, domain);
562 }
563