OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [gdb/] [cp-namespace.c] - Blame information for rev 822

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 jeremybenn
/* Helper routines for C++ support in GDB.
2
   Copyright (C) 2003, 2004, 2007, 2008 Free Software Foundation, Inc.
3
 
4
   Contributed by David Carlton and by Kealia, Inc.
5
 
6
   This file is part of GDB.
7
 
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
 
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
 
18
   You should have received a copy of the GNU General Public License
19
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
 
21
#include "defs.h"
22
#include "cp-support.h"
23
#include "gdb_obstack.h"
24
#include "symtab.h"
25
#include "symfile.h"
26
#include "gdb_assert.h"
27
#include "block.h"
28
#include "objfiles.h"
29
#include "gdbtypes.h"
30
#include "dictionary.h"
31
#include "command.h"
32
#include "frame.h"
33
 
34
/* When set, the file that we're processing is known to have debugging
35
   info for C++ namespaces.  */
36
 
37
/* NOTE: carlton/2004-01-13: No currently released version of GCC (the
38
   latest of which is 3.3.x at the time of this writing) produces this
39
   debug info.  GCC 3.4 should, however.  */
40
 
41
unsigned char processing_has_namespace_info;
42
 
43
/* This contains our best guess as to the name of the current
44
   enclosing namespace(s)/class(es), if any.  For example, if we're
45
   within the method foo() in the following code:
46
 
47
    namespace N {
48
      class C {
49
        void foo () {
50
        }
51
      };
52
    }
53
 
54
   then processing_current_prefix should be set to "N::C".  If
55
   processing_has_namespace_info is false, then this variable might
56
   not be reliable.  */
57
 
58
const char *processing_current_prefix;
59
 
60
/* List of using directives that are active in the current file.  */
61
 
62
static struct using_direct *using_list;
63
 
64
static struct using_direct *cp_add_using (const char *name,
65
                                          unsigned int inner_len,
66
                                          unsigned int outer_len,
67
                                          struct using_direct *next);
68
 
69
static struct using_direct *cp_copy_usings (struct using_direct *using,
70
                                            struct obstack *obstack);
71
 
72
static struct symbol *lookup_namespace_scope (const char *name,
73
                                              const char *linkage_name,
74
                                              const struct block *block,
75
                                              const domain_enum domain,
76
                                              struct symtab **symtab,
77
                                              const char *scope,
78
                                              int scope_len);
79
 
80
static struct symbol *lookup_symbol_file (const char *name,
81
                                          const char *linkage_name,
82
                                          const struct block *block,
83
                                          const domain_enum domain,
84
                                          struct symtab **symtab,
85
                                          int anonymous_namespace);
86
 
87
static struct type *cp_lookup_transparent_type_loop (const char *name,
88
                                                     const char *scope,
89
                                                     int scope_len);
90
 
91
static void initialize_namespace_symtab (struct objfile *objfile);
92
 
93
static struct block *get_possible_namespace_block (struct objfile *objfile);
94
 
95
static void free_namespace_block (struct symtab *symtab);
96
 
97
static int check_possible_namespace_symbols_loop (const char *name,
98
                                                  int len,
99
                                                  struct objfile *objfile);
100
 
101
static int check_one_possible_namespace_symbol (const char *name,
102
                                                int len,
103
                                                struct objfile *objfile);
104
 
105
static
106
struct symbol *lookup_possible_namespace_symbol (const char *name,
107
                                                 struct symtab **symtab);
108
 
109
static void maintenance_cplus_namespace (char *args, int from_tty);
110
 
111
/* Set up support for dealing with C++ namespace info in the current
112
   symtab.  */
113
 
114
void cp_initialize_namespace ()
115
{
116
  processing_has_namespace_info = 0;
117
  using_list = NULL;
118
}
119
 
120
/* Add all the using directives we've gathered to the current symtab.
121
   STATIC_BLOCK should be the symtab's static block; OBSTACK is used
122
   for allocation.  */
123
 
124
void
125
cp_finalize_namespace (struct block *static_block,
126
                       struct obstack *obstack)
127
{
128
  if (using_list != NULL)
129
    {
130
      block_set_using (static_block,
131
                       cp_copy_usings (using_list, obstack),
132
                       obstack);
133
      using_list = NULL;
134
    }
135
}
136
 
137
/* Check to see if SYMBOL refers to an object contained within an
138
   anonymous namespace; if so, add an appropriate using directive.  */
139
 
140
/* Optimize away strlen ("(anonymous namespace)").  */
141
 
142
#define ANONYMOUS_NAMESPACE_LEN 21
143
 
144
void
145
cp_scan_for_anonymous_namespaces (const struct symbol *symbol)
146
{
147
  if (!processing_has_namespace_info
148
      && SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
149
    {
150
      const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
151
      unsigned int previous_component;
152
      unsigned int next_component;
153
      const char *len;
154
 
155
      /* Start with a quick-and-dirty check for mention of "(anonymous
156
         namespace)".  */
157
 
158
      if (!cp_is_anonymous (name))
159
        return;
160
 
161
      previous_component = 0;
162
      next_component = cp_find_first_component (name + previous_component);
163
 
164
      while (name[next_component] == ':')
165
        {
166
          if ((next_component - previous_component) == ANONYMOUS_NAMESPACE_LEN
167
              && strncmp (name + previous_component,
168
                          "(anonymous namespace)",
169
                          ANONYMOUS_NAMESPACE_LEN) == 0)
170
            {
171
              /* We've found a component of the name that's an
172
                 anonymous namespace.  So add symbols in it to the
173
                 namespace given by the previous component if there is
174
                 one, or to the global namespace if there isn't.  */
175
              cp_add_using_directive (name,
176
                                      previous_component == 0
177
                                      ? 0 : previous_component - 2,
178
                                      next_component);
179
            }
180
          /* The "+ 2" is for the "::".  */
181
          previous_component = next_component + 2;
182
          next_component = (previous_component
183
                            + cp_find_first_component (name
184
                                                       + previous_component));
185
        }
186
    }
187
}
188
 
189
/* Add a using directive to using_list.  NAME is the start of a string
190
   that should contain the namespaces we want to add as initial
191
   substrings, OUTER_LENGTH is the end of the outer namespace, and
192
   INNER_LENGTH is the end of the inner namespace.  If the using
193
   directive in question has already been added, don't add it
194
   twice.  */
195
 
196
void
197
cp_add_using_directive (const char *name, unsigned int outer_length,
198
                        unsigned int inner_length)
199
{
200
  struct using_direct *current;
201
  struct using_direct *new;
202
 
203
  /* Has it already been added?  */
204
 
205
  for (current = using_list; current != NULL; current = current->next)
206
    {
207
      if ((strncmp (current->inner, name, inner_length) == 0)
208
          && (strlen (current->inner) == inner_length)
209
          && (strlen (current->outer) == outer_length))
210
        return;
211
    }
212
 
213
  using_list = cp_add_using (name, inner_length, outer_length,
214
                             using_list);
215
}
216
 
217
/* Record the namespace that the function defined by SYMBOL was
218
   defined in, if necessary.  BLOCK is the associated block; use
219
   OBSTACK for allocation.  */
220
 
221
void
222
cp_set_block_scope (const struct symbol *symbol,
223
                    struct block *block,
224
                    struct obstack *obstack)
225
{
226
  /* Make sure that the name was originally mangled: if not, there
227
     certainly isn't any namespace information to worry about!  */
228
 
229
  if (SYMBOL_CPLUS_DEMANGLED_NAME (symbol) != NULL)
230
    {
231
      if (processing_has_namespace_info)
232
        {
233
          block_set_scope
234
            (block, obsavestring (processing_current_prefix,
235
                                  strlen (processing_current_prefix),
236
                                  obstack),
237
             obstack);
238
        }
239
      else
240
        {
241
          /* Try to figure out the appropriate namespace from the
242
             demangled name.  */
243
 
244
          /* FIXME: carlton/2003-04-15: If the function in question is
245
             a method of a class, the name will actually include the
246
             name of the class as well.  This should be harmless, but
247
             is a little unfortunate.  */
248
 
249
          const char *name = SYMBOL_CPLUS_DEMANGLED_NAME (symbol);
250
          unsigned int prefix_len = cp_entire_prefix_len (name);
251
 
252
          block_set_scope (block,
253
                           obsavestring (name, prefix_len, obstack),
254
                           obstack);
255
        }
256
    }
257
}
258
 
259
/* Test whether or not NAMESPACE looks like it mentions an anonymous
260
   namespace; return nonzero if so.  */
261
 
262
int
263
cp_is_anonymous (const char *namespace)
264
{
265
  return (strstr (namespace, "(anonymous namespace)")
266
          != NULL);
267
}
268
 
269
/* Create a new struct using direct whose inner namespace is the
270
   initial substring of NAME of leng INNER_LEN and whose outer
271
   namespace is the initial substring of NAME of length OUTER_LENGTH.
272
   Set its next member in the linked list to NEXT; allocate all memory
273
   using xmalloc.  It copies the strings, so NAME can be a temporary
274
   string.  */
275
 
276
static struct using_direct *
277
cp_add_using (const char *name,
278
              unsigned int inner_len,
279
              unsigned int outer_len,
280
              struct using_direct *next)
281
{
282
  struct using_direct *retval;
283
 
284
  gdb_assert (outer_len < inner_len);
285
 
286
  retval = xmalloc (sizeof (struct using_direct));
287
  retval->inner = savestring (name, inner_len);
288
  retval->outer = savestring (name, outer_len);
289
  retval->next = next;
290
 
291
  return retval;
292
}
293
 
294
/* Make a copy of the using directives in the list pointed to by
295
   USING, using OBSTACK to allocate memory.  Free all memory pointed
296
   to by USING via xfree.  */
297
 
298
static struct using_direct *
299
cp_copy_usings (struct using_direct *using,
300
                struct obstack *obstack)
301
{
302
  if (using == NULL)
303
    {
304
      return NULL;
305
    }
306
  else
307
    {
308
      struct using_direct *retval
309
        = obstack_alloc (obstack, sizeof (struct using_direct));
310
      retval->inner = obsavestring (using->inner, strlen (using->inner),
311
                                    obstack);
312
      retval->outer = obsavestring (using->outer, strlen (using->outer),
313
                                    obstack);
314
      retval->next = cp_copy_usings (using->next, obstack);
315
 
316
      xfree (using->inner);
317
      xfree (using->outer);
318
      xfree (using);
319
 
320
      return retval;
321
    }
322
}
323
 
324
/* The C++-specific version of name lookup for static and global
325
   names.  This makes sure that names get looked for in all namespaces
326
   that are in scope.  NAME is the natural name of the symbol that
327
   we're looking for, LINKAGE_NAME (which is optional) is its linkage
328
   name, BLOCK is the block that we're searching within, DOMAIN says
329
   what kind of symbols we're looking for, and if SYMTAB is non-NULL,
330
   we should store the symtab where we found the symbol in it.  */
331
 
332
struct symbol *
333
cp_lookup_symbol_nonlocal (const char *name,
334
                           const char *linkage_name,
335
                           const struct block *block,
336
                           const domain_enum domain,
337
                           struct symtab **symtab)
338
{
339
  return lookup_namespace_scope (name, linkage_name, block, domain,
340
                                 symtab, block_scope (block), 0);
341
}
342
 
343
/* Lookup NAME at namespace scope (or, in C terms, in static and
344
   global variables).  SCOPE is the namespace that the current
345
   function is defined within; only consider namespaces whose length
346
   is at least SCOPE_LEN.  Other arguments are as in
347
   cp_lookup_symbol_nonlocal.
348
 
349
   For example, if we're within a function A::B::f and looking for a
350
   symbol x, this will get called with NAME = "x", SCOPE = "A::B", and
351
   SCOPE_LEN = 0.  It then calls itself with NAME and SCOPE the same,
352
   but with SCOPE_LEN = 1.  And then it calls itself with NAME and
353
   SCOPE the same, but with SCOPE_LEN = 4.  This third call looks for
354
   "A::B::x"; if it doesn't find it, then the second call looks for
355
   "A::x", and if that call fails, then the first call looks for
356
   "x".  */
357
 
358
static struct symbol *
359
lookup_namespace_scope (const char *name,
360
                        const char *linkage_name,
361
                        const struct block *block,
362
                        const domain_enum domain,
363
                        struct symtab **symtab,
364
                        const char *scope,
365
                        int scope_len)
366
{
367
  char *namespace;
368
 
369
  if (scope[scope_len] != '\0')
370
    {
371
      /* Recursively search for names in child namespaces first.  */
372
 
373
      struct symbol *sym;
374
      int new_scope_len = scope_len;
375
 
376
      /* If the current scope is followed by "::", skip past that.  */
377
      if (new_scope_len != 0)
378
        {
379
          gdb_assert (scope[new_scope_len] == ':');
380
          new_scope_len += 2;
381
        }
382
      new_scope_len += cp_find_first_component (scope + new_scope_len);
383
      sym = lookup_namespace_scope (name, linkage_name, block,
384
                                    domain, symtab,
385
                                    scope, new_scope_len);
386
      if (sym != NULL)
387
        return sym;
388
    }
389
 
390
  /* Okay, we didn't find a match in our children, so look for the
391
     name in the current namespace.  */
392
 
393
  namespace = alloca (scope_len + 1);
394
  strncpy (namespace, scope, scope_len);
395
  namespace[scope_len] = '\0';
396
  return cp_lookup_symbol_namespace (namespace, name, linkage_name,
397
                                     block, domain, symtab);
398
}
399
 
400
/* Look up NAME in the C++ namespace NAMESPACE, applying the using
401
   directives that are active in BLOCK.  Other arguments are as in
402
   cp_lookup_symbol_nonlocal.  */
403
 
404
struct symbol *
405
cp_lookup_symbol_namespace (const char *namespace,
406
                            const char *name,
407
                            const char *linkage_name,
408
                            const struct block *block,
409
                            const domain_enum domain,
410
                            struct symtab **symtab)
411
{
412
  const struct using_direct *current;
413
  struct symbol *sym;
414
 
415
  /* First, go through the using directives.  If any of them add new
416
     names to the namespace we're searching in, see if we can find a
417
     match by applying them.  */
418
 
419
  for (current = block_using (block);
420
       current != NULL;
421
       current = current->next)
422
    {
423
      if (strcmp (namespace, current->outer) == 0)
424
        {
425
          sym = cp_lookup_symbol_namespace (current->inner,
426
                                            name,
427
                                            linkage_name,
428
                                            block,
429
                                            domain,
430
                                            symtab);
431
          if (sym != NULL)
432
            return sym;
433
        }
434
    }
435
 
436
  /* We didn't find anything by applying any of the using directives
437
     that are still applicable; so let's see if we've got a match
438
     using the current namespace.  */
439
 
440
  if (namespace[0] == '\0')
441
    {
442
      return lookup_symbol_file (name, linkage_name, block,
443
                                 domain, symtab, 0);
444
    }
445
  else
446
    {
447
      char *concatenated_name
448
        = alloca (strlen (namespace) + 2 + strlen (name) + 1);
449
      strcpy (concatenated_name, namespace);
450
      strcat (concatenated_name, "::");
451
      strcat (concatenated_name, name);
452
      sym = lookup_symbol_file (concatenated_name, linkage_name,
453
                                block, domain, symtab,
454
                                cp_is_anonymous (namespace));
455
      return sym;
456
    }
457
}
458
 
459
/* Look up NAME in BLOCK's static block and in global blocks.  If
460
   ANONYMOUS_NAMESPACE is nonzero, the symbol in question is located
461
   within an anonymous namespace.  Other arguments are as in
462
   cp_lookup_symbol_nonlocal.  */
463
 
464
static struct symbol *
465
lookup_symbol_file (const char *name,
466
                    const char *linkage_name,
467
                    const struct block *block,
468
                    const domain_enum domain,
469
                    struct symtab **symtab,
470
                    int anonymous_namespace)
471
{
472
  struct symbol *sym = NULL;
473
 
474
  sym = lookup_symbol_static (name, linkage_name, block, domain, symtab);
475
  if (sym != NULL)
476
    return sym;
477
 
478
  if (anonymous_namespace)
479
    {
480
      /* Symbols defined in anonymous namespaces have external linkage
481
         but should be treated as local to a single file nonetheless.
482
         So we only search the current file's global block.  */
483
 
484
      const struct block *global_block = block_global_block (block);
485
 
486
      if (global_block != NULL)
487
        sym = lookup_symbol_aux_block (name, linkage_name, global_block,
488
                                       domain, symtab);
489
    }
490
  else
491
    {
492
      sym = lookup_symbol_global (name, linkage_name, block, domain, symtab);
493
    }
494
 
495
  if (sym != NULL)
496
    return sym;
497
 
498
  /* Now call "lookup_possible_namespace_symbol".  Symbols in here
499
     claim to be associated to namespaces, but this claim might be
500
     incorrect: the names in question might actually correspond to
501
     classes instead of namespaces.  But if they correspond to
502
     classes, then we should have found a match for them above.  So if
503
     we find them now, they should be genuine.  */
504
 
505
  /* FIXME: carlton/2003-06-12: This is a hack and should eventually
506
     be deleted: see comments below.  */
507
 
508
  if (domain == VAR_DOMAIN)
509
    {
510
      sym = lookup_possible_namespace_symbol (name, symtab);
511
      if (sym != NULL)
512
        return sym;
513
    }
514
 
515
  return NULL;
516
}
517
 
518
/* Look up a type named NESTED_NAME that is nested inside the C++
519
   class or namespace given by PARENT_TYPE, from within the context
520
   given by BLOCK.  Return NULL if there is no such nested type.  */
521
 
522
struct type *
523
cp_lookup_nested_type (struct type *parent_type,
524
                       const char *nested_name,
525
                       const struct block *block)
526
{
527
  switch (TYPE_CODE (parent_type))
528
    {
529
    case TYPE_CODE_STRUCT:
530
    case TYPE_CODE_NAMESPACE:
531
      {
532
        /* NOTE: carlton/2003-11-10: We don't treat C++ class members
533
           of classes like, say, data or function members.  Instead,
534
           they're just represented by symbols whose names are
535
           qualified by the name of the surrounding class.  This is
536
           just like members of namespaces; in particular,
537
           lookup_symbol_namespace works when looking them up.  */
538
 
539
        const char *parent_name = TYPE_TAG_NAME (parent_type);
540
        struct symbol *sym = cp_lookup_symbol_namespace (parent_name,
541
                                                         nested_name,
542
                                                         NULL,
543
                                                         block,
544
                                                         VAR_DOMAIN,
545
                                                         NULL);
546
        if (sym == NULL || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
547
          return NULL;
548
        else
549
          return SYMBOL_TYPE (sym);
550
      }
551
    default:
552
      internal_error (__FILE__, __LINE__,
553
                      _("cp_lookup_nested_type called on a non-aggregate type."));
554
    }
555
}
556
 
557
/* The C++-version of lookup_transparent_type.  */
558
 
559
/* FIXME: carlton/2004-01-16: The problem that this is trying to
560
   address is that, unfortunately, sometimes NAME is wrong: it may not
561
   include the name of namespaces enclosing the type in question.
562
   lookup_transparent_type gets called when the the type in question
563
   is a declaration, and we're trying to find its definition; but, for
564
   declarations, our type name deduction mechanism doesn't work.
565
   There's nothing we can do to fix this in general, I think, in the
566
   absence of debug information about namespaces (I've filed PR
567
   gdb/1511 about this); until such debug information becomes more
568
   prevalent, one heuristic which sometimes looks is to search for the
569
   definition in namespaces containing the current namespace.
570
 
571
   We should delete this functions once the appropriate debug
572
   information becomes more widespread.  (GCC 3.4 will be the first
573
   released version of GCC with such information.)  */
574
 
575
struct type *
576
cp_lookup_transparent_type (const char *name)
577
{
578
  /* First, try the honest way of looking up the definition.  */
579
  struct type *t = basic_lookup_transparent_type (name);
580
  const char *scope;
581
 
582
  if (t != NULL)
583
    return t;
584
 
585
  /* If that doesn't work and we're within a namespace, look there
586
     instead.  */
587
  scope = block_scope (get_selected_block (0));
588
 
589
  if (scope[0] == '\0')
590
    return NULL;
591
 
592
  return cp_lookup_transparent_type_loop (name, scope, 0);
593
}
594
 
595
/* Lookup the the type definition associated to NAME in
596
   namespaces/classes containing SCOPE whose name is strictly longer
597
   than LENGTH.  LENGTH must be the index of the start of a
598
   component of SCOPE.  */
599
 
600
static struct type *
601
cp_lookup_transparent_type_loop (const char *name, const char *scope,
602
                                 int length)
603
{
604
  int scope_length = length + cp_find_first_component (scope + length);
605
  char *full_name;
606
 
607
  /* If the current scope is followed by "::", look in the next
608
     component.  */
609
  if (scope[scope_length] == ':')
610
    {
611
      struct type *retval
612
        = cp_lookup_transparent_type_loop (name, scope, scope_length + 2);
613
      if (retval != NULL)
614
        return retval;
615
    }
616
 
617
  full_name = alloca (scope_length + 2 + strlen (name) + 1);
618
  strncpy (full_name, scope, scope_length);
619
  strncpy (full_name + scope_length, "::", 2);
620
  strcpy (full_name + scope_length + 2, name);
621
 
622
  return basic_lookup_transparent_type (full_name);
623
}
624
 
625
/* Now come functions for dealing with symbols associated to
626
   namespaces.  (They're used to store the namespaces themselves, not
627
   objects that live in the namespaces.)  These symbols come in two
628
   varieties: if we run into a DW_TAG_namespace DIE, then we know that
629
   we have a namespace, so dwarf2read.c creates a symbol for it just
630
   like normal.  But, unfortunately, versions of GCC through at least
631
   3.3 don't generate those DIE's.  Our solution is to try to guess
632
   their existence by looking at demangled names.  This might cause us
633
   to misidentify classes as namespaces, however.  So we put those
634
   symbols in a special block (one per objfile), and we only search
635
   that block as a last resort.  */
636
 
637
/* FIXME: carlton/2003-06-12: Once versions of GCC that generate
638
   DW_TAG_namespace have been out for a year or two, we should get rid
639
   of all of this "possible namespace" nonsense.  */
640
 
641
/* Allocate everything necessary for the possible namespace block
642
   associated to OBJFILE.  */
643
 
644
static void
645
initialize_namespace_symtab (struct objfile *objfile)
646
{
647
  struct symtab *namespace_symtab;
648
  struct blockvector *bv;
649
  struct block *bl;
650
 
651
  namespace_symtab = allocate_symtab ("<<C++-namespaces>>", objfile);
652
  namespace_symtab->language = language_cplus;
653
  namespace_symtab->free_code = free_nothing;
654
  namespace_symtab->dirname = NULL;
655
 
656
  bv = obstack_alloc (&objfile->objfile_obstack,
657
                      sizeof (struct blockvector)
658
                      + FIRST_LOCAL_BLOCK * sizeof (struct block *));
659
  BLOCKVECTOR_NBLOCKS (bv) = FIRST_LOCAL_BLOCK + 1;
660
  BLOCKVECTOR (namespace_symtab) = bv;
661
 
662
  /* Allocate empty GLOBAL_BLOCK and STATIC_BLOCK. */
663
 
664
  bl = allocate_block (&objfile->objfile_obstack);
665
  BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
666
                                        NULL);
667
  BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK) = bl;
668
  bl = allocate_block (&objfile->objfile_obstack);
669
  BLOCK_DICT (bl) = dict_create_linear (&objfile->objfile_obstack,
670
                                        NULL);
671
  BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK) = bl;
672
 
673
  /* Allocate the possible namespace block; we put it where the first
674
     local block will live, though I don't think there's any need to
675
     pretend that it's actually a local block (e.g. by setting
676
     BLOCK_SUPERBLOCK appropriately).  We don't use the global or
677
     static block because we don't want it searched during the normal
678
     search of all global/static blocks in lookup_symbol: we only want
679
     it used as a last resort.  */
680
 
681
  /* NOTE: carlton/2003-09-11: I considered not associating the fake
682
     symbols to a block/symtab at all.  But that would cause problems
683
     with lookup_symbol's SYMTAB argument and with block_found, so
684
     having a symtab/block for this purpose seems like the best
685
     solution for now.  */
686
 
687
  bl = allocate_block (&objfile->objfile_obstack);
688
  BLOCK_DICT (bl) = dict_create_hashed_expandable ();
689
  BLOCKVECTOR_BLOCK (bv, FIRST_LOCAL_BLOCK) = bl;
690
 
691
  namespace_symtab->free_func = free_namespace_block;
692
 
693
  objfile->cp_namespace_symtab = namespace_symtab;
694
}
695
 
696
/* Locate the possible namespace block associated to OBJFILE,
697
   allocating it if necessary.  */
698
 
699
static struct block *
700
get_possible_namespace_block (struct objfile *objfile)
701
{
702
  if (objfile->cp_namespace_symtab == NULL)
703
    initialize_namespace_symtab (objfile);
704
 
705
  return BLOCKVECTOR_BLOCK (BLOCKVECTOR (objfile->cp_namespace_symtab),
706
                            FIRST_LOCAL_BLOCK);
707
}
708
 
709
/* Free the dictionary associated to the possible namespace block.  */
710
 
711
static void
712
free_namespace_block (struct symtab *symtab)
713
{
714
  struct block *possible_namespace_block;
715
 
716
  possible_namespace_block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab),
717
                                                FIRST_LOCAL_BLOCK);
718
  gdb_assert (possible_namespace_block != NULL);
719
  dict_free (BLOCK_DICT (possible_namespace_block));
720
}
721
 
722
/* Ensure that there are symbols in the possible namespace block
723
   associated to OBJFILE for all initial substrings of NAME that look
724
   like namespaces or classes.  NAME should end in a member variable:
725
   it shouldn't consist solely of namespaces.  */
726
 
727
void
728
cp_check_possible_namespace_symbols (const char *name, struct objfile *objfile)
729
{
730
  check_possible_namespace_symbols_loop (name,
731
                                         cp_find_first_component (name),
732
                                         objfile);
733
}
734
 
735
/* This is a helper loop for cp_check_possible_namespace_symbols; it
736
   ensures that there are symbols in the possible namespace block
737
   associated to OBJFILE for all namespaces that are initial
738
   substrings of NAME of length at least LEN.  It returns 1 if a
739
   previous loop had already created the shortest such symbol and 0
740
   otherwise.
741
 
742
   This function assumes that if there is already a symbol associated
743
   to a substring of NAME of a given length, then there are already
744
   symbols associated to all substrings of NAME whose length is less
745
   than that length.  So if cp_check_possible_namespace_symbols has
746
   been called once with argument "A::B::C::member", then that will
747
   create symbols "A", "A::B", and "A::B::C".  If it is then later
748
   called with argument "A::B::D::member", then the new call will
749
   generate a new symbol for "A::B::D", but once it sees that "A::B"
750
   has already been created, it doesn't bother checking to see if "A"
751
   has also been created.  */
752
 
753
static int
754
check_possible_namespace_symbols_loop (const char *name, int len,
755
                                       struct objfile *objfile)
756
{
757
  if (name[len] == ':')
758
    {
759
      int done;
760
      int next_len = len + 2;
761
 
762
      next_len += cp_find_first_component (name + next_len);
763
      done = check_possible_namespace_symbols_loop (name, next_len,
764
                                                    objfile);
765
 
766
      if (!done)
767
        done = check_one_possible_namespace_symbol (name, len, objfile);
768
 
769
      return done;
770
    }
771
  else
772
    return 0;
773
}
774
 
775
/* Check to see if there's already a possible namespace symbol in
776
   OBJFILE whose name is the initial substring of NAME of length LEN.
777
   If not, create one and return 0; otherwise, return 1.  */
778
 
779
static int
780
check_one_possible_namespace_symbol (const char *name, int len,
781
                                     struct objfile *objfile)
782
{
783
  struct block *block = get_possible_namespace_block (objfile);
784
  char *name_copy = alloca (len + 1);
785
  struct symbol *sym;
786
 
787
  memcpy (name_copy, name, len);
788
  name_copy[len] = '\0';
789
  sym = lookup_block_symbol (block, name_copy, NULL, VAR_DOMAIN);
790
 
791
  if (sym == NULL)
792
    {
793
      struct type *type;
794
      name_copy = obsavestring (name, len, &objfile->objfile_obstack);
795
 
796
      type = init_type (TYPE_CODE_NAMESPACE, 0, 0, name_copy, objfile);
797
 
798
      TYPE_TAG_NAME (type) = TYPE_NAME (type);
799
 
800
      sym = obstack_alloc (&objfile->objfile_obstack, sizeof (struct symbol));
801
      memset (sym, 0, sizeof (struct symbol));
802
      SYMBOL_LANGUAGE (sym) = language_cplus;
803
      SYMBOL_SET_NAMES (sym, name_copy, len, objfile);
804
      SYMBOL_CLASS (sym) = LOC_TYPEDEF;
805
      SYMBOL_TYPE (sym) = type;
806
      SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
807
 
808
      dict_add_symbol (BLOCK_DICT (block), sym);
809
 
810
      return 0;
811
    }
812
  else
813
    return 1;
814
}
815
 
816
/* Look for a symbol named NAME in all the possible namespace blocks.
817
   If one is found, return it; if SYMTAB is non-NULL, set *SYMTAB to
818
   equal the symtab where it was found.  */
819
 
820
static struct symbol *
821
lookup_possible_namespace_symbol (const char *name, struct symtab **symtab)
822
{
823
  struct objfile *objfile;
824
 
825
  ALL_OBJFILES (objfile)
826
    {
827
      struct symbol *sym;
828
 
829
      sym = lookup_block_symbol (get_possible_namespace_block (objfile),
830
                                 name, NULL, VAR_DOMAIN);
831
 
832
      if (sym != NULL)
833
        {
834
          if (symtab != NULL)
835
            *symtab = objfile->cp_namespace_symtab;
836
 
837
          return sym;
838
        }
839
    }
840
 
841
  return NULL;
842
}
843
 
844
/* Print out all the possible namespace symbols.  */
845
 
846
static void
847
maintenance_cplus_namespace (char *args, int from_tty)
848
{
849
  struct objfile *objfile;
850
  printf_unfiltered (_("Possible namespaces:\n"));
851
  ALL_OBJFILES (objfile)
852
    {
853
      struct dict_iterator iter;
854
      struct symbol *sym;
855
 
856
      ALL_BLOCK_SYMBOLS (get_possible_namespace_block (objfile), iter, sym)
857
        {
858
          printf_unfiltered ("%s\n", SYMBOL_PRINT_NAME (sym));
859
        }
860
    }
861
}
862
 
863
void
864
_initialize_cp_namespace (void)
865
{
866
  add_cmd ("namespace", class_maintenance, maintenance_cplus_namespace,
867
           _("Print the list of possible C++ namespaces."),
868
           &maint_cplus_cmd_list);
869
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.