aboutsummaryrefslogtreecommitdiff
path: root/gdb/d-namespace.c
blob: 8b8c3cc96c71a3c8dcac1fda8640c4c500f9c83e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* Helper routines for D support in GDB.

   Copyright (C) 2014-2016 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "defs.h"
#include "symtab.h"
#include "block.h"
#include "language.h"
#include "namespace.h"
#include "d-lang.h"
#include "gdb_obstack.h"

/* This returns the length of first component of NAME, which should be
   the demangled name of a D variable/function/method/etc.
   Specifically, it returns the index of the first dot forming the
   boundary of the first component: so, given 'A.foo' or 'A.B.foo'
   it returns the 1, and given 'foo', it returns 0.  */

/* The character in NAME indexed by the return value is guaranteed to
   always be either '.' or '\0'.  */

static unsigned int
d_find_first_component (const char *name)
{
  unsigned int index = 0;

  for (;; ++index)
    {
      if (name[index] == '.' || name[index] == '\0')
	return index;
    }
}

/* If NAME is the fully-qualified name of a D function/variable/method,
   this returns the length of its entire prefix: all of the modules and
   classes that make up its name.  Given 'A.foo', it returns 1, given
   'A.B.foo', it returns 4, given 'foo', it returns 0.  */

static unsigned int
d_entire_prefix_len (const char *name)
{
  unsigned int current_len = d_find_first_component (name);
  unsigned int previous_len = 0;

  while (name[current_len] != '\0')
    {
      gdb_assert (name[current_len] == '.');
      previous_len = current_len;
      /* Skip the '.'  */
      current_len++;
      current_len += d_find_first_component (name + current_len);
    }

  return previous_len;
}

/* Look up NAME in BLOCK's static block and in global blocks.
   If SEARCH is non-zero, search through base classes for a matching
   symbol.  Other arguments are as in d_lookup_symbol_nonlocal.  */

static struct block_symbol
d_lookup_symbol (const struct language_defn *langdef,
		 const char *name, const struct block *block,
		 const domain_enum domain, int search)
{
  struct block_symbol sym;

  sym = lookup_symbol_in_static_block (name, block, domain);
  if (sym.symbol != NULL)
    return sym;

  /* If we didn't find a definition for a builtin type in the static block,
     such as "ucent" which is a specialist type, search for it now.  */
  if (langdef != NULL && domain == VAR_DOMAIN)
    {
      struct gdbarch *gdbarch;

      if (block == NULL)
	gdbarch = target_gdbarch ();
      else
	gdbarch = block_gdbarch (block);
      sym.symbol
	= language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
      sym.block = NULL;
      if (sym.symbol != NULL)
	return sym;
    }

  sym = lookup_global_symbol (name, block, domain);

  if (sym.symbol != NULL)
    return sym;

  if (search)
    {
      char *classname, *nested;
      unsigned int prefix_len;
      struct cleanup *cleanup;
      struct block_symbol class_sym;

      /* A simple lookup failed.  Check if the symbol was defined in
	 a base class.  */

      cleanup = make_cleanup (null_cleanup, NULL);

      /* Find the name of the class and the name of the method,
	 variable, etc.  */
      prefix_len = d_entire_prefix_len (name);

      /* If no prefix was found, search "this".  */
      if (prefix_len == 0)
	{
	  struct type *type;
	  struct block_symbol lang_this;

	  lang_this = lookup_language_this (language_def (language_d), block);
	  if (lang_this.symbol == NULL)
	    {
	      do_cleanups (cleanup);
	      return null_block_symbol;
	    }

	  type = check_typedef (TYPE_TARGET_TYPE (SYMBOL_TYPE (lang_this.symbol)));
	  classname = xstrdup (TYPE_NAME (type));
	  nested = xstrdup (name);
	}
      else
	{
	  /* The class name is everything up to and including PREFIX_LEN.  */
	  classname = savestring (name, prefix_len);

	  /* The rest of the name is everything else past the initial scope
	     operator.  */
	  nested = xstrdup (name + prefix_len + 1);
	}

      /* Add cleanups to free memory for these strings.  */
      make_cleanup (xfree, classname);
      make_cleanup (xfree, nested);

      /* Lookup a class named CLASSNAME.  If none is found, there is nothing
	 more that can be done.  */
      class_sym = lookup_global_symbol (classname, block, domain);
      if (class_sym.symbol == NULL)
	{
	  do_cleanups (cleanup);
	  return null_block_symbol;
	}

      /* Look for a symbol named NESTED in this class.  */
      sym = d_lookup_nested_symbol (SYMBOL_TYPE (class_sym.symbol),
				    nested, block);
      do_cleanups (cleanup);
    }

  return sym;
}

/* Look up NAME in the D module MODULE.  Other arguments are as in
   d_lookup_symbol_nonlocal.  If SEARCH is non-zero, search through
   base classes for a matching symbol.  */

static struct block_symbol
d_lookup_symbol_in_module (const char *module, const char *name,
			   const struct block *block,
			   const domain_enum domain, int search)
{
  char *concatenated_name = NULL;

  if (module[0] != '\0')
    {
      concatenated_name
	= (char *) alloca (strlen (module) + strlen (name) + 2);
      strcpy (concatenated_name, module);
      strcat (concatenated_name, ".");
      strcat (concatenated_name, name);
      name = concatenated_name;
    }

  return d_lookup_symbol (NULL, name, block, domain, search);
}

/* Lookup NAME at module scope.  SCOPE is the module that the current
   function is defined within; only consider modules whose length is at
   least SCOPE_LEN.  Other arguments are as in d_lookup_symbol_nonlocal.

   For example, if we're within a function A.B.f and looking for a
   symbol x, this will get called with NAME = "x", SCOPE = "A.B", and
   SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
   but with SCOPE_LEN = 1.  And then it calls itself with NAME and
   SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
   "A.B.x"; if it doesn't find it, then the second call looks for "A.x",
   and if that call fails, then the first call looks for "x".  */

static struct block_symbol
lookup_module_scope (const struct language_defn *langdef,
		     const char *name, const struct block *block,
		     const domain_enum domain, const char *scope,
		     int scope_len)
{
  char *module;

  if (scope[scope_len] != '\0')
    {
      /* Recursively search for names in child modules first.  */

      struct block_symbol sym;
      int new_scope_len = scope_len;

      /* If the current scope is followed by ".", skip past that.  */
      if (new_scope_len != 0)
	{
	  gdb_assert (scope[new_scope_len] == '.');
	  new_scope_len++;
	}
      new_scope_len += d_find_first_component (scope + new_scope_len);
      sym = lookup_module_scope (langdef, name, block, domain,
				 scope, new_scope_len);
      if (sym.symbol != NULL)
	return sym;
    }

  /* Okay, we didn't find a match in our children, so look for the
     name in the current module.

     If we there is no scope and we know we have a bare symbol, then short
     circuit everything and call d_lookup_symbol directly.
     This isn't an optimization, rather it allows us to pass LANGDEF which
     is needed for primitive type lookup.  */

  if (scope_len == 0 && strchr (name, '.') == NULL)
    return d_lookup_symbol (langdef, name, block, domain, 1);

  module = (char *) alloca (scope_len + 1);
  strncpy (module, scope, scope_len);
  module[scope_len] = '\0';
  return d_lookup_symbol_in_module (module, name,
				    block, domain, 1);
}

/* Search through the base classes of PARENT_TYPE for a symbol named
   NAME in block BLOCK.  */

static struct block_symbol
find_symbol_in_baseclass (struct type *parent_type, const char *name,
			  const struct block *block)
{
  char *concatenated_name = NULL;
  struct block_symbol sym;
  struct cleanup *cleanup;
  int i;

  sym.symbol = NULL;
  sym.block = NULL;
  cleanup = make_cleanup (free_current_contents, &concatenated_name);

  for (i = 0; i < TYPE_N_BASECLASSES (parent_type); ++i)
    {
      size_t len;
      struct type *base_type = TYPE_BASECLASS (parent_type, i);
      const char *base_name = TYPE_BASECLASS_NAME (parent_type, i);

      if (base_name == NULL)
	continue;

      /* Search this particular base class.  */
      sym = d_lookup_symbol_in_module (base_name, name, block,
				       VAR_DOMAIN, 0);
      if (sym.symbol != NULL)
	break;

      /* Now search all static file-level symbols.  We have to do this for
	 things like typedefs in the class.  First search in this symtab,
	 what we want is possibly there.  */
      len = strlen (base_name) + strlen (name) + 2;
      concatenated_name = (char *) xrealloc (concatenated_name, len);
      xsnprintf (concatenated_name, len, "%s.%s", base_name, name);
      sym = lookup_symbol_in_static_block (concatenated_name, block,
					   VAR_DOMAIN);
      if (sym.symbol != NULL)
	break;

      /* Nope.  We now have to search all static blocks in all objfiles,
	 even if block != NULL, because there's no guarantees as to which
	 symtab the symbol we want is in.  */
      sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
      if (sym.symbol != NULL)
	break;

      /* If this class has base classes, search them next.  */
      base_type = check_typedef (base_type);
      if (TYPE_N_BASECLASSES (base_type) > 0)
	{
	  sym = find_symbol_in_baseclass (base_type, name, block);
	  if (sym.symbol != NULL)
	    break;
	}
    }

  do_cleanups (cleanup);
  return sym;
}

/* Look up a symbol named NESTED_NAME that is nested inside the D
   class or module given by PARENT_TYPE, from within the context
   given by BLOCK.  Return NULL if there is no such nested type.  */

struct block_symbol
d_lookup_nested_symbol (struct type *parent_type,
			const char *nested_name,
			const struct block *block)
{
  /* type_name_no_tag_required provides better error reporting using the
     original type.  */
  struct type *saved_parent_type = parent_type;

  parent_type = check_typedef (parent_type);

  switch (TYPE_CODE (parent_type))
    {
    case TYPE_CODE_STRUCT:
    case TYPE_CODE_UNION:
    case TYPE_CODE_ENUM:
    case TYPE_CODE_MODULE:
	{
	  int size;
	  const char *parent_name = type_name_no_tag_or_error (saved_parent_type);
	  struct block_symbol sym
	    = d_lookup_symbol_in_module (parent_name, nested_name,
					 block, VAR_DOMAIN, 0);
	  char *concatenated_name;

	  if (sym.symbol != NULL)
	    return sym;

	  /* Now search all static file-level symbols.  We have to do this
	     for things like typedefs in the class.  We do not try to
	     guess any imported module as even the fully specified
	     module search is already not D compliant and more assumptions
	     could make it too magic.  */
	  size = strlen (parent_name) + strlen (nested_name) + 2;
	  concatenated_name = (char *) alloca (size);

	  xsnprintf (concatenated_name, size, "%s.%s",
		     parent_name, nested_name);

	  sym = lookup_static_symbol (concatenated_name, VAR_DOMAIN);
	  if (sym.symbol != NULL)
	    return sym;

	  /* If no matching symbols were found, try searching any
	     base classes.  */
	  return find_symbol_in_baseclass (parent_type, nested_name, block);
	}

    case TYPE_CODE_FUNC:
    case TYPE_CODE_METHOD:
      return null_block_symbol;

    default:
      gdb_assert_not_reached ("called with non-aggregate type.");
    }
}

/* Used for cleanups to reset the "searched" flag incase
   of an error.  */

static void
reset_directive_searched (void *data)
{
  struct using_direct *direct = (struct using_direct *) data;
  direct->searched = 0;
}

/* Search for NAME by applying all import statements belonging to
   BLOCK which are applicable in SCOPE.  */

static struct block_symbol
d_lookup_symbol_imports (const char *scope, const char *name,
			 const struct block *block,
			 const domain_enum domain)
{
  struct using_direct *current;
  struct block_symbol sym;
  struct cleanup *searched_cleanup;

  /* First, try to find the symbol in the given module.  */
  sym = d_lookup_symbol_in_module (scope, name, block, domain, 1);

  if (sym.symbol != NULL)
    return sym;

  /* Go through the using directives.  If any of them add new names to
     the module we're searching in, see if we can find a match by
     applying them.  */

  for (current = block_using (block);
       current != NULL;
       current = current->next)
    {
      const char **excludep;

      /* If the import destination is the current scope then search it.  */
      if (!current->searched && strcmp (scope, current->import_dest) == 0)
	{
	  /* Mark this import as searched so that the recursive call
	     does not search it again.  */
	  current->searched = 1;
	  searched_cleanup = make_cleanup (reset_directive_searched,
					   current);

	  /* If there is an import of a single declaration, compare the
	     imported declaration (after optional renaming by its alias)
	     with the sought out name.  If there is a match pass
	     current->import_src as MODULE to direct the search towards
	     the imported module.  */
	  if (current->declaration
	      && strcmp (name, current->alias
			 ? current->alias : current->declaration) == 0)
	    sym = d_lookup_symbol_in_module (current->import_src,
					     current->declaration,
					     block, domain, 1);

	  /* If a symbol was found or this import statement was an import
	     declaration, the search of this import is complete.  */
	  if (sym.symbol != NULL || current->declaration)
	    {
	      current->searched = 0;
	      discard_cleanups (searched_cleanup);

	      if (sym.symbol != NULL)
		return sym;

	      continue;
	    }

	  /* Do not follow CURRENT if NAME matches its EXCLUDES.  */
	  for (excludep = current->excludes; *excludep; excludep++)
	    if (strcmp (name, *excludep) == 0)
	      break;
	  if (*excludep)
	    {
	      discard_cleanups (searched_cleanup);
	      continue;
	    }

	  /* If the import statement is creating an alias.  */
	  if (current->alias != NULL)
	    {
	      if (strcmp (name, current->alias) == 0)
		{
		  /* If the alias matches the sought name.  Pass
		     current->import_src as the NAME to direct the
		     search towards the aliased module.  */
		  sym = lookup_module_scope (NULL, current->import_src, block,
					     domain, scope, 0);
		}
	      else
		{
		  /* If the alias matches the first component of the
		     sought name, pass current->import_src as MODULE
		     to direct the search, skipping over the aliased
		     component in NAME.  */
		  int name_scope = d_find_first_component (name);

		  if (name[name_scope] != '\0'
		      && strncmp (name, current->alias, name_scope) == 0)
		    {
		      /* Skip the '.'  */
		      name_scope++;
		      sym = d_lookup_symbol_in_module (current->import_src,
						       name + name_scope,
						       block, domain, 1);
		    }
		}
	    }
	  else
	    {
	      /* If this import statement creates no alias, pass
		 current->import_src as MODULE to direct the search
		 towards the imported module.  */
	      sym = d_lookup_symbol_in_module (current->import_src,
					       name, block, domain, 1);
	    }
	  current->searched = 0;
	  discard_cleanups (searched_cleanup);

	  if (sym.symbol != NULL)
	    return sym;
	}
    }

  return null_block_symbol;
}

/* Searches for NAME in the current module, and by applying relevant
   import statements belonging to BLOCK and its parents.  SCOPE is the
   module scope of the context in which the search is being evaluated.  */

static struct block_symbol
d_lookup_symbol_module (const char *scope, const char *name,
			const struct block *block,
			const domain_enum domain)
{
  struct block_symbol sym;

  /* First, try to find the symbol in the given module.  */
  sym = d_lookup_symbol_in_module (scope, name,
				   block, domain, 1);
  if (sym.symbol != NULL)
    return sym;

  /* Search for name in modules imported to this and parent
     blocks.  */
  while (block != NULL)
    {
      sym = d_lookup_symbol_imports (scope, name, block, domain);

      if (sym.symbol != NULL)
	return sym;

      block = BLOCK_SUPERBLOCK (block);
    }

  return null_block_symbol;
}

/* The D-specific version of name lookup for static and global names
   This makes sure that names get looked for in all modules that are
   in scope.  NAME is the natural name of the symbol that we're looking
   looking for, BLOCK is the block that we're searching within, DOMAIN
   says what kind of symbols we're looking for, and if SYMTAB is non-NULL,
   we should store the symtab where we found the symbol in it.  */

struct block_symbol
d_lookup_symbol_nonlocal (const struct language_defn *langdef,
			  const char *name,
			  const struct block *block,
			  const domain_enum domain)
{
  struct block_symbol sym;
  const char *scope = block_scope (block);

  sym = lookup_module_scope (langdef, name, block, domain, scope, 0);
  if (sym.symbol != NULL)
    return sym;

  return d_lookup_symbol_module (scope, name, block, domain);
}