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