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
161 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
162 OBSTACK. (It won't make a copy of SCOPE, however, so that already
163 has to be allocated correctly.) */
166 block_set_scope (struct block *block, const char *scope,
167 struct obstack *obstack)
169 block_initialize_namespace (block, obstack);
171 BLOCK_NAMESPACE (block)->scope = scope;
174 /* Set BLOCK's using member to USING; if needed, allocate memory via
175 OBSTACK. (It won't make a copy of USING, however, so that already
176 has to be allocated correctly.) */
179 block_set_using (struct block *block,
180 struct using_direct *using,
181 struct obstack *obstack)
183 block_initialize_namespace (block, obstack);
185 BLOCK_NAMESPACE (block)->using = using;
188 /* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
189 ititialize its members to zero. */
192 block_initialize_namespace (struct block *block, struct obstack *obstack)
194 if (BLOCK_NAMESPACE (block) == NULL)
196 BLOCK_NAMESPACE (block)
197 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
198 BLOCK_NAMESPACE (block)->scope = NULL;
199 BLOCK_NAMESPACE (block)->using = NULL;