1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright 2003 Free Software Foundation, Inc.
5 This file is part of GDB.
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "gdb_obstack.h"
27 #include "cp-support.h"
29 /* This is used by struct block to store namespace-related info for
30 C++ files, namely using declarations and the current namespace in
33 struct block_namespace_info
36 struct using_direct *using;
39 static void block_initialize_namespace (struct block *block,
40 struct obstack *obstack);
42 /* Return Nonzero if block a is lexically nested within block b,
43 or if a and b have the same pc range.
44 Return zero otherwise. */
47 contained_in (const struct block *a, const struct block *b)
51 return BLOCK_START (a) >= BLOCK_START (b)
52 && BLOCK_END (a) <= BLOCK_END (b);
56 /* Return the symbol for the function which contains a specified
57 lexical block, described by a struct block BL. */
60 block_function (const struct block *bl)
62 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
63 bl = BLOCK_SUPERBLOCK (bl);
65 return BLOCK_FUNCTION (bl);
68 /* Return the blockvector immediately containing the innermost lexical block
69 containing the specified pc value and section, or 0 if there is none.
70 PINDEX is a pointer to the index value of the block. If PINDEX
71 is NULL, we don't pass this information back to the caller. */
74 blockvector_for_pc_sect (CORE_ADDR pc, struct bfd_section *section,
75 int *pindex, struct symtab *symtab)
79 struct blockvector *bl;
81 if (symtab == 0) /* if no symtab specified by caller */
83 /* First search all symtabs for one whose file contains our pc */
84 symtab = find_pc_sect_symtab (pc, section);
89 bl = BLOCKVECTOR (symtab);
90 b = BLOCKVECTOR_BLOCK (bl, 0);
92 /* Then search that symtab for the smallest block that wins. */
93 /* Use binary search to find the last block that starts before PC. */
96 top = BLOCKVECTOR_NBLOCKS (bl);
100 half = (top - bot + 1) >> 1;
101 b = BLOCKVECTOR_BLOCK (bl, bot + half);
102 if (BLOCK_START (b) <= pc)
108 /* Now search backward for a block that ends after PC. */
112 b = BLOCKVECTOR_BLOCK (bl, bot);
113 if (BLOCK_END (b) > pc)
124 /* Return the blockvector immediately containing the innermost lexical block
125 containing the specified pc value, or 0 if there is none.
126 Backward compatibility, no section. */
129 blockvector_for_pc (CORE_ADDR pc, int *pindex)
131 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
135 /* Return the innermost lexical block containing the specified pc value
136 in the specified section, or 0 if there is none. */
139 block_for_pc_sect (CORE_ADDR pc, struct bfd_section *section)
141 struct blockvector *bl;
144 bl = blockvector_for_pc_sect (pc, section, &index, NULL);
146 return BLOCKVECTOR_BLOCK (bl, index);
150 /* Return the innermost lexical block containing the specified pc value,
151 or 0 if there is none. Backward compatibility, no section. */
154 block_for_pc (CORE_ADDR pc)
156 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
159 /* Now come some functions designed to deal with C++ namespace issues.
160 The accessors are safe to use even in the non-C++ case. */
162 /* This returns the namespace that BLOCK is enclosed in, or "" if it
163 isn't enclosed in a namespace at all. This travels the chain of
164 superblocks looking for a scope, if necessary. */
167 block_scope (const struct block *block)
169 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
171 if (BLOCK_NAMESPACE (block) != NULL
172 && BLOCK_NAMESPACE (block)->scope != NULL)
173 return BLOCK_NAMESPACE (block)->scope;
179 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
180 OBSTACK. (It won't make a copy of SCOPE, however, so that already
181 has to be allocated correctly.) */
184 block_set_scope (struct block *block, const char *scope,
185 struct obstack *obstack)
187 block_initialize_namespace (block, obstack);
189 BLOCK_NAMESPACE (block)->scope = scope;
192 /* This returns the first using directives associated to BLOCK, if
195 /* FIXME: carlton/2003-04-23: This uses the fact that we currently
196 only have using directives in static blocks, because we only
197 generate using directives from anonymous namespaces. Eventually,
198 when we support using directives everywhere, we'll want to replace
199 this by some iterator functions. */
201 struct using_direct *
202 block_using (const struct block *block)
204 const struct block *static_block = block_static_block (block);
206 if (static_block == NULL
207 || BLOCK_NAMESPACE (static_block) == NULL)
210 return BLOCK_NAMESPACE (static_block)->using;
213 /* Set BLOCK's using member to USING; if needed, allocate memory via
214 OBSTACK. (It won't make a copy of USING, however, so that already
215 has to be allocated correctly.) */
218 block_set_using (struct block *block,
219 struct using_direct *using,
220 struct obstack *obstack)
222 block_initialize_namespace (block, obstack);
224 BLOCK_NAMESPACE (block)->using = using;
227 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
228 ititialize its members to zero. */
231 block_initialize_namespace (struct block *block, struct obstack *obstack)
233 if (BLOCK_NAMESPACE (block) == NULL)
235 BLOCK_NAMESPACE (block)
236 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
237 BLOCK_NAMESPACE (block)->scope = NULL;
238 BLOCK_NAMESPACE (block)->using = NULL;
242 /* Return the static block associated to BLOCK. Return NULL if block
243 is NULL or if block is a global block. */
246 block_static_block (const struct block *block)
248 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
251 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
252 block = BLOCK_SUPERBLOCK (block);
257 /* Return the static block associated to BLOCK. Return NULL if block
261 block_global_block (const struct block *block)
266 while (BLOCK_SUPERBLOCK (block) != NULL)
267 block = BLOCK_SUPERBLOCK (block);
272 /* Allocate a block on OBSTACK, and initialize its elements to
273 zero/NULL. This is useful for creating "dummy" blocks that don't
274 correspond to actual source files.
276 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
277 valid value. If you really don't want the block to have a
278 dictionary, then you should subsequently set its BLOCK_DICT to
279 dict_create_linear (obstack, NULL). */
282 allocate_block (struct obstack *obstack)
284 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
286 BLOCK_START (bl) = 0;
288 BLOCK_FUNCTION (bl) = NULL;
289 BLOCK_SUPERBLOCK (bl) = NULL;
290 BLOCK_DICT (bl) = NULL;
291 BLOCK_NAMESPACE (bl) = NULL;
292 BLOCK_GCC_COMPILED (bl) = 0;