1 /* Block-related functions for the GNU debugger, GDB.
3 Copyright (C) 2003, 2007 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"
27 /* This is used by struct block to store namespace-related info for
28 C++ files, namely using declarations and the current namespace in
31 struct block_namespace_info
34 struct using_direct *using;
37 static void block_initialize_namespace (struct block *block,
38 struct obstack *obstack);
40 /* Return Nonzero if block a is lexically nested within block b,
41 or if a and b have the same pc range.
42 Return zero otherwise. */
45 contained_in (const struct block *a, const struct block *b)
49 return BLOCK_START (a) >= BLOCK_START (b)
50 && BLOCK_END (a) <= BLOCK_END (b);
54 /* Return the symbol for the function which contains a specified
55 lexical block, described by a struct block BL. */
58 block_function (const struct block *bl)
60 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
61 bl = BLOCK_SUPERBLOCK (bl);
63 return BLOCK_FUNCTION (bl);
66 /* Return the blockvector immediately containing the innermost lexical block
67 containing the specified pc value and section, or 0 if there is none.
68 PINDEX is a pointer to the index value of the block. If PINDEX
69 is NULL, we don't pass this information back to the caller. */
72 blockvector_for_pc_sect (CORE_ADDR pc, struct bfd_section *section,
73 int *pindex, struct symtab *symtab)
77 struct blockvector *bl;
79 if (symtab == 0) /* if no symtab specified by caller */
81 /* First search all symtabs for one whose file contains our pc */
82 symtab = find_pc_sect_symtab (pc, section);
87 bl = BLOCKVECTOR (symtab);
88 b = BLOCKVECTOR_BLOCK (bl, 0);
90 /* Then search that symtab for the smallest block that wins. */
91 /* Use binary search to find the last block that starts before PC. */
94 top = BLOCKVECTOR_NBLOCKS (bl);
98 half = (top - bot + 1) >> 1;
99 b = BLOCKVECTOR_BLOCK (bl, bot + half);
100 if (BLOCK_START (b) <= pc)
106 /* Now search backward for a block that ends after PC. */
110 b = BLOCKVECTOR_BLOCK (bl, bot);
111 if (BLOCK_END (b) > pc)
122 /* Return the blockvector immediately containing the innermost lexical block
123 containing the specified pc value, or 0 if there is none.
124 Backward compatibility, no section. */
127 blockvector_for_pc (CORE_ADDR pc, int *pindex)
129 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
133 /* Return the innermost lexical block containing the specified pc value
134 in the specified section, or 0 if there is none. */
137 block_for_pc_sect (CORE_ADDR pc, struct bfd_section *section)
139 struct blockvector *bl;
142 bl = blockvector_for_pc_sect (pc, section, &index, NULL);
144 return BLOCKVECTOR_BLOCK (bl, index);
148 /* Return the innermost lexical block containing the specified pc value,
149 or 0 if there is none. Backward compatibility, no section. */
152 block_for_pc (CORE_ADDR pc)
154 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
157 /* Now come some functions designed to deal with C++ namespace issues.
158 The accessors are safe to use even in the non-C++ case. */
160 /* This returns the namespace that BLOCK is enclosed in, or "" if it
161 isn't enclosed in a namespace at all. This travels the chain of
162 superblocks looking for a scope, if necessary. */
165 block_scope (const struct block *block)
167 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
169 if (BLOCK_NAMESPACE (block) != NULL
170 && BLOCK_NAMESPACE (block)->scope != NULL)
171 return BLOCK_NAMESPACE (block)->scope;
177 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
178 OBSTACK. (It won't make a copy of SCOPE, however, so that already
179 has to be allocated correctly.) */
182 block_set_scope (struct block *block, const char *scope,
183 struct obstack *obstack)
185 block_initialize_namespace (block, obstack);
187 BLOCK_NAMESPACE (block)->scope = scope;
190 /* This returns the first using directives associated to BLOCK, if
193 /* FIXME: carlton/2003-04-23: This uses the fact that we currently
194 only have using directives in static blocks, because we only
195 generate using directives from anonymous namespaces. Eventually,
196 when we support using directives everywhere, we'll want to replace
197 this by some iterator functions. */
199 struct using_direct *
200 block_using (const struct block *block)
202 const struct block *static_block = block_static_block (block);
204 if (static_block == NULL
205 || BLOCK_NAMESPACE (static_block) == NULL)
208 return BLOCK_NAMESPACE (static_block)->using;
211 /* Set BLOCK's using member to USING; if needed, allocate memory via
212 OBSTACK. (It won't make a copy of USING, however, so that already
213 has to be allocated correctly.) */
216 block_set_using (struct block *block,
217 struct using_direct *using,
218 struct obstack *obstack)
220 block_initialize_namespace (block, obstack);
222 BLOCK_NAMESPACE (block)->using = using;
225 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
226 ititialize its members to zero. */
229 block_initialize_namespace (struct block *block, struct obstack *obstack)
231 if (BLOCK_NAMESPACE (block) == NULL)
233 BLOCK_NAMESPACE (block)
234 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
235 BLOCK_NAMESPACE (block)->scope = NULL;
236 BLOCK_NAMESPACE (block)->using = NULL;
240 /* Return the static block associated to BLOCK. Return NULL if block
241 is NULL or if block is a global block. */
244 block_static_block (const struct block *block)
246 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
249 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
250 block = BLOCK_SUPERBLOCK (block);
255 /* Return the static block associated to BLOCK. Return NULL if block
259 block_global_block (const struct block *block)
264 while (BLOCK_SUPERBLOCK (block) != NULL)
265 block = BLOCK_SUPERBLOCK (block);
270 /* Allocate a block on OBSTACK, and initialize its elements to
271 zero/NULL. This is useful for creating "dummy" blocks that don't
272 correspond to actual source files.
274 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
275 valid value. If you really don't want the block to have a
276 dictionary, then you should subsequently set its BLOCK_DICT to
277 dict_create_linear (obstack, NULL). */
280 allocate_block (struct obstack *obstack)
282 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
284 BLOCK_START (bl) = 0;
286 BLOCK_FUNCTION (bl) = NULL;
287 BLOCK_SUPERBLOCK (bl) = NULL;
288 BLOCK_DICT (bl) = NULL;
289 BLOCK_NAMESPACE (bl) = NULL;
290 BLOCK_GCC_COMPILED (bl) = 0;