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