1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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/>. */
25 #include "gdb_obstack.h"
26 #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)
56 /* If A is a function block, then A cannot be contained in B,
57 except if A was inlined. */
58 if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
60 a = BLOCK_SUPERBLOCK (a);
68 /* Return the symbol for the function which contains a specified
69 lexical block, described by a struct block BL. The return value
70 will not be an inlined function; the containing function will be
74 block_linkage_function (const struct block *bl)
76 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
77 && BLOCK_SUPERBLOCK (bl) != NULL)
78 bl = BLOCK_SUPERBLOCK (bl);
80 return BLOCK_FUNCTION (bl);
83 /* Return one if BL represents an inlined function. */
86 block_inlined_p (const struct block *bl)
88 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
91 /* Return the blockvector immediately containing the innermost lexical
92 block containing the specified pc value and section, or 0 if there
93 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
94 don't pass this information back to the caller. */
97 blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
98 struct block **pblock, struct symtab *symtab)
102 struct blockvector *bl;
104 if (symtab == 0) /* if no symtab specified by caller */
106 /* First search all symtabs for one whose file contains our pc */
107 symtab = find_pc_sect_symtab (pc, section);
112 bl = BLOCKVECTOR (symtab);
114 /* Then search that symtab for the smallest block that wins. */
116 /* If we have an addrmap mapping code addresses to blocks, then use
118 if (BLOCKVECTOR_MAP (bl))
120 b = addrmap_find (BLOCKVECTOR_MAP (bl), pc);
132 /* Otherwise, use binary search to find the last block that starts
135 top = BLOCKVECTOR_NBLOCKS (bl);
137 while (top - bot > 1)
139 half = (top - bot + 1) >> 1;
140 b = BLOCKVECTOR_BLOCK (bl, bot + half);
141 if (BLOCK_START (b) <= pc)
147 /* Now search backward for a block that ends after PC. */
151 b = BLOCKVECTOR_BLOCK (bl, bot);
152 if (BLOCK_END (b) > pc)
163 /* Return the blockvector immediately containing the innermost lexical block
164 containing the specified pc value, or 0 if there is none.
165 Backward compatibility, no section. */
168 blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
170 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
174 /* Return the innermost lexical block containing the specified pc value
175 in the specified section, or 0 if there is none. */
178 block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
180 struct blockvector *bl;
183 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
189 /* Return the innermost lexical block containing the specified pc value,
190 or 0 if there is none. Backward compatibility, no section. */
193 block_for_pc (CORE_ADDR pc)
195 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
198 /* Now come some functions designed to deal with C++ namespace issues.
199 The accessors are safe to use even in the non-C++ case. */
201 /* This returns the namespace that BLOCK is enclosed in, or "" if it
202 isn't enclosed in a namespace at all. This travels the chain of
203 superblocks looking for a scope, if necessary. */
206 block_scope (const struct block *block)
208 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
210 if (BLOCK_NAMESPACE (block) != NULL
211 && BLOCK_NAMESPACE (block)->scope != NULL)
212 return BLOCK_NAMESPACE (block)->scope;
218 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
219 OBSTACK. (It won't make a copy of SCOPE, however, so that already
220 has to be allocated correctly.) */
223 block_set_scope (struct block *block, const char *scope,
224 struct obstack *obstack)
226 block_initialize_namespace (block, obstack);
228 BLOCK_NAMESPACE (block)->scope = scope;
231 /* This returns the using directives list associated with BLOCK, if
234 struct using_direct *
235 block_using (const struct block *block)
237 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
240 return BLOCK_NAMESPACE (block)->using;
243 /* Set BLOCK's using member to USING; if needed, allocate memory via
244 OBSTACK. (It won't make a copy of USING, however, so that already
245 has to be allocated correctly.) */
248 block_set_using (struct block *block,
249 struct using_direct *using,
250 struct obstack *obstack)
252 block_initialize_namespace (block, obstack);
254 BLOCK_NAMESPACE (block)->using = using;
257 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
258 ititialize its members to zero. */
261 block_initialize_namespace (struct block *block, struct obstack *obstack)
263 if (BLOCK_NAMESPACE (block) == NULL)
265 BLOCK_NAMESPACE (block)
266 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
267 BLOCK_NAMESPACE (block)->scope = NULL;
268 BLOCK_NAMESPACE (block)->using = NULL;
272 /* Return the static block associated to BLOCK. Return NULL if block
273 is NULL or if block is a global block. */
276 block_static_block (const struct block *block)
278 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
281 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
282 block = BLOCK_SUPERBLOCK (block);
287 /* Return the static block associated to BLOCK. Return NULL if block
291 block_global_block (const struct block *block)
296 while (BLOCK_SUPERBLOCK (block) != NULL)
297 block = BLOCK_SUPERBLOCK (block);
302 /* Allocate a block on OBSTACK, and initialize its elements to
303 zero/NULL. This is useful for creating "dummy" blocks that don't
304 correspond to actual source files.
306 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
307 valid value. If you really don't want the block to have a
308 dictionary, then you should subsequently set its BLOCK_DICT to
309 dict_create_linear (obstack, NULL). */
312 allocate_block (struct obstack *obstack)
314 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
316 BLOCK_START (bl) = 0;
318 BLOCK_FUNCTION (bl) = NULL;
319 BLOCK_SUPERBLOCK (bl) = NULL;
320 BLOCK_DICT (bl) = NULL;
321 BLOCK_NAMESPACE (bl) = NULL;