1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003, 2007, 2008, 2009 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 using directives list associated with BLOCK, if
212 struct using_direct *
213 block_using (const struct block *block)
215 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
218 return BLOCK_NAMESPACE (block)->using;
221 /* Set BLOCK's using member to USING; if needed, allocate memory via
222 OBSTACK. (It won't make a copy of USING, however, so that already
223 has to be allocated correctly.) */
226 block_set_using (struct block *block,
227 struct using_direct *using,
228 struct obstack *obstack)
230 block_initialize_namespace (block, obstack);
232 BLOCK_NAMESPACE (block)->using = using;
235 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
236 ititialize its members to zero. */
239 block_initialize_namespace (struct block *block, struct obstack *obstack)
241 if (BLOCK_NAMESPACE (block) == NULL)
243 BLOCK_NAMESPACE (block)
244 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
245 BLOCK_NAMESPACE (block)->scope = NULL;
246 BLOCK_NAMESPACE (block)->using = NULL;
250 /* Return the static block associated to BLOCK. Return NULL if block
251 is NULL or if block is a global block. */
254 block_static_block (const struct block *block)
256 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
259 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
260 block = BLOCK_SUPERBLOCK (block);
265 /* Return the static block associated to BLOCK. Return NULL if block
269 block_global_block (const struct block *block)
274 while (BLOCK_SUPERBLOCK (block) != NULL)
275 block = BLOCK_SUPERBLOCK (block);
280 /* Allocate a block on OBSTACK, and initialize its elements to
281 zero/NULL. This is useful for creating "dummy" blocks that don't
282 correspond to actual source files.
284 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
285 valid value. If you really don't want the block to have a
286 dictionary, then you should subsequently set its BLOCK_DICT to
287 dict_create_linear (obstack, NULL). */
290 allocate_block (struct obstack *obstack)
292 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
294 BLOCK_START (bl) = 0;
296 BLOCK_FUNCTION (bl) = NULL;
297 BLOCK_SUPERBLOCK (bl) = NULL;
298 BLOCK_DICT (bl) = NULL;
299 BLOCK_NAMESPACE (bl) = NULL;