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