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 (struct block *a, 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 (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 (register CORE_ADDR pc, struct sec *section,
75 int *pindex, struct symtab *symtab)
77 register struct block *b;
78 register int bot, top, half;
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 if ((symtab = find_pc_sect_symtab (pc, section)) == 0)
88 bl = BLOCKVECTOR (symtab);
89 b = BLOCKVECTOR_BLOCK (bl, 0);
91 /* Then search that symtab for the smallest block that wins. */
92 /* Use binary search to find the last block that starts before PC. */
95 top = BLOCKVECTOR_NBLOCKS (bl);
99 half = (top - bot + 1) >> 1;
100 b = BLOCKVECTOR_BLOCK (bl, bot + half);
101 if (BLOCK_START (b) <= pc)
107 /* Now search backward for a block that ends after PC. */
111 b = BLOCKVECTOR_BLOCK (bl, bot);
112 if (BLOCK_END (b) > pc)
123 /* Return the blockvector immediately containing the innermost lexical block
124 containing the specified pc value, or 0 if there is none.
125 Backward compatibility, no section. */
128 blockvector_for_pc (register CORE_ADDR pc, int *pindex)
130 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
134 /* Return the innermost lexical block containing the specified pc value
135 in the specified section, or 0 if there is none. */
138 block_for_pc_sect (register CORE_ADDR pc, struct sec *section)
140 register struct blockvector *bl;
143 bl = blockvector_for_pc_sect (pc, section, &index, NULL);
145 return BLOCKVECTOR_BLOCK (bl, index);
149 /* Return the innermost lexical block containing the specified pc value,
150 or 0 if there is none. Backward compatibility, no section. */
153 block_for_pc (register CORE_ADDR pc)
155 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
158 /* Now come some functions designed to deal with C++ namespace issues.
159 The accessors are safe to use even in the non-C++ case. */
161 /* This returns the namespace that BLOCK is enclosed in, or "" if it
162 isn't enclosed in a namespace at all. This travels the chain of
163 superblocks looking for a scope, if necessary. */
166 block_scope (const struct block *block)
168 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
170 if (BLOCK_NAMESPACE (block) != NULL
171 && BLOCK_NAMESPACE (block)->scope != NULL)
172 return BLOCK_NAMESPACE (block)->scope;
178 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
179 OBSTACK. (It won't make a copy of SCOPE, however, so that already
180 has to be allocated correctly.) */
183 block_set_scope (struct block *block, const char *scope,
184 struct obstack *obstack)
186 block_initialize_namespace (block, obstack);
188 BLOCK_NAMESPACE (block)->scope = scope;
191 /* This returns the first using directives associated to BLOCK, if
194 /* FIXME: carlton/2003-04-23: This uses the fact that we currently
195 only have using directives in static blocks, because we only
196 generate using directives from anonymous namespaces. Eventually,
197 when we support using directives everywhere, we'll want to replace
198 this by some iterator functions. */
200 struct using_direct *
201 block_using (const struct block *block)
203 const struct block *static_block = block_static_block (block);
205 if (static_block == NULL
206 || BLOCK_NAMESPACE (static_block) == NULL)
209 return BLOCK_NAMESPACE (static_block)->using;
212 /* Set BLOCK's using member to USING; if needed, allocate memory via
213 OBSTACK. (It won't make a copy of USING, however, so that already
214 has to be allocated correctly.) */
217 block_set_using (struct block *block,
218 struct using_direct *using,
219 struct obstack *obstack)
221 block_initialize_namespace (block, obstack);
223 BLOCK_NAMESPACE (block)->using = using;
226 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
227 ititialize its members to zero. */
230 block_initialize_namespace (struct block *block, struct obstack *obstack)
232 if (BLOCK_NAMESPACE (block) == NULL)
234 BLOCK_NAMESPACE (block)
235 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
236 BLOCK_NAMESPACE (block)->scope = NULL;
237 BLOCK_NAMESPACE (block)->using = NULL;
241 /* Return the static block associated to BLOCK. Return NULL if block
242 is NULL or if block is a global block. */
245 block_static_block (const struct block *block)
247 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
250 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
251 block = BLOCK_SUPERBLOCK (block);
256 /* Return the static block associated to BLOCK. Return NULL if block
260 block_global_block (const struct block *block)
265 while (BLOCK_SUPERBLOCK (block) != NULL)
266 block = BLOCK_SUPERBLOCK (block);