1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003, 2007, 2008 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 3 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, see <http://www.gnu.org/licenses/>. */
24 #include "gdb_obstack.h"
25 #include "cp-support.h"
28 /* This is used by struct block to store namespace-related info for
29 C++ files, namely using declarations and the current namespace in
32 struct block_namespace_info
35 struct using_direct *using;
38 static void block_initialize_namespace (struct block *block,
39 struct obstack *obstack);
41 /* Return Nonzero if block a is lexically nested within block b,
42 or if a and b have the same pc range.
43 Return zero otherwise. */
46 contained_in (const struct block *a, const struct block *b)
50 return BLOCK_START (a) >= BLOCK_START (b)
51 && BLOCK_END (a) <= BLOCK_END (b);
55 /* Return the symbol for the function which contains a specified
56 lexical block, described by a struct block BL. The return value
57 will not be an inlined function; the containing function will be
61 block_linkage_function (const struct block *bl)
63 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
64 bl = BLOCK_SUPERBLOCK (bl);
66 return BLOCK_FUNCTION (bl);
69 /* Return the blockvector immediately containing the innermost lexical
70 block containing the specified pc value and section, or 0 if there
71 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
72 don't pass this information back to the caller. */
75 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
76 struct block **pblock, struct symtab *symtab)
80 struct blockvector *bl;
82 if (symtab == 0) /* if no symtab specified by caller */
84 /* First search all symtabs for one whose file contains our pc */
85 symtab = find_pc_sect_symtab (pc, section);
90 bl = BLOCKVECTOR (symtab);
92 /* Then search that symtab for the smallest block that wins. */
94 /* If we have an addrmap mapping code addresses to blocks, then use
96 if (BLOCKVECTOR_MAP (bl))
98 b = addrmap_find (BLOCKVECTOR_MAP (bl), pc);
110 /* Otherwise, use binary search to find the last block that starts
113 top = BLOCKVECTOR_NBLOCKS (bl);
115 while (top - bot > 1)
117 half = (top - bot + 1) >> 1;
118 b = BLOCKVECTOR_BLOCK (bl, bot + half);
119 if (BLOCK_START (b) <= pc)
125 /* Now search backward for a block that ends after PC. */
129 b = BLOCKVECTOR_BLOCK (bl, bot);
130 if (BLOCK_END (b) > pc)
141 /* Return the blockvector immediately containing the innermost lexical block
142 containing the specified pc value, or 0 if there is none.
143 Backward compatibility, no section. */
146 blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
148 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
152 /* Return the innermost lexical block containing the specified pc value
153 in the specified section, or 0 if there is none. */
156 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
158 struct blockvector *bl;
161 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
167 /* Return the innermost lexical block containing the specified pc value,
168 or 0 if there is none. Backward compatibility, no section. */
171 block_for_pc (CORE_ADDR pc)
173 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
176 /* Now come some functions designed to deal with C++ namespace issues.
177 The accessors are safe to use even in the non-C++ case. */
179 /* This returns the namespace that BLOCK is enclosed in, or "" if it
180 isn't enclosed in a namespace at all. This travels the chain of
181 superblocks looking for a scope, if necessary. */
184 block_scope (const struct block *block)
186 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
188 if (BLOCK_NAMESPACE (block) != NULL
189 && BLOCK_NAMESPACE (block)->scope != NULL)
190 return BLOCK_NAMESPACE (block)->scope;
196 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
197 OBSTACK. (It won't make a copy of SCOPE, however, so that already
198 has to be allocated correctly.) */
201 block_set_scope (struct block *block, const char *scope,
202 struct obstack *obstack)
204 block_initialize_namespace (block, obstack);
206 BLOCK_NAMESPACE (block)->scope = scope;
209 /* This returns the first using directives associated to BLOCK, if
212 /* FIXME: carlton/2003-04-23: This uses the fact that we currently
213 only have using directives in static blocks, because we only
214 generate using directives from anonymous namespaces. Eventually,
215 when we support using directives everywhere, we'll want to replace
216 this by some iterator functions. */
218 struct using_direct *
219 block_using (const struct block *block)
221 const struct block *static_block = block_static_block (block);
223 if (static_block == NULL
224 || BLOCK_NAMESPACE (static_block) == NULL)
227 return BLOCK_NAMESPACE (static_block)->using;
230 /* Set BLOCK's using member to USING; if needed, allocate memory via
231 OBSTACK. (It won't make a copy of USING, however, so that already
232 has to be allocated correctly.) */
235 block_set_using (struct block *block,
236 struct using_direct *using,
237 struct obstack *obstack)
239 block_initialize_namespace (block, obstack);
241 BLOCK_NAMESPACE (block)->using = using;
244 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
245 ititialize its members to zero. */
248 block_initialize_namespace (struct block *block, struct obstack *obstack)
250 if (BLOCK_NAMESPACE (block) == NULL)
252 BLOCK_NAMESPACE (block)
253 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
254 BLOCK_NAMESPACE (block)->scope = NULL;
255 BLOCK_NAMESPACE (block)->using = NULL;
259 /* Return the static block associated to BLOCK. Return NULL if block
260 is NULL or if block is a global block. */
263 block_static_block (const struct block *block)
265 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
268 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
269 block = BLOCK_SUPERBLOCK (block);
274 /* Return the static block associated to BLOCK. Return NULL if block
278 block_global_block (const struct block *block)
283 while (BLOCK_SUPERBLOCK (block) != NULL)
284 block = BLOCK_SUPERBLOCK (block);
289 /* Allocate a block on OBSTACK, and initialize its elements to
290 zero/NULL. This is useful for creating "dummy" blocks that don't
291 correspond to actual source files.
293 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
294 valid value. If you really don't want the block to have a
295 dictionary, then you should subsequently set its BLOCK_DICT to
296 dict_create_linear (obstack, NULL). */
299 allocate_block (struct obstack *obstack)
301 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
303 BLOCK_START (bl) = 0;
305 BLOCK_FUNCTION (bl) = NULL;
306 BLOCK_SUPERBLOCK (bl) = NULL;
307 BLOCK_DICT (bl) = NULL;
308 BLOCK_NAMESPACE (bl) = NULL;