* block.c (contained_in): Return zero for nested functions.
[deliverable/binutils-gdb.git] / gdb / block.c
CommitLineData
fe898f56
DC
1/* Block-related functions for the GNU debugger, GDB.
2
0fb0cc75 3 Copyright (C) 2003, 2007, 2008, 2009 Free Software Foundation, Inc.
fe898f56
DC
4
5 This file is part of GDB.
6
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
fe898f56
DC
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
fe898f56
DC
19
20#include "defs.h"
21#include "block.h"
22#include "symtab.h"
23#include "symfile.h"
9219021c
DC
24#include "gdb_obstack.h"
25#include "cp-support.h"
801e3a5b 26#include "addrmap.h"
9219021c
DC
27
28/* This is used by struct block to store namespace-related info for
29 C++ files, namely using declarations and the current namespace in
30 scope. */
31
32struct block_namespace_info
33{
34 const char *scope;
35 struct using_direct *using;
36};
37
38static void block_initialize_namespace (struct block *block,
39 struct obstack *obstack);
fe898f56
DC
40
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. */
44
45int
0cf566ec 46contained_in (const struct block *a, const struct block *b)
fe898f56
DC
47{
48 if (!a || !b)
49 return 0;
edb3359d
DJ
50
51 do
52 {
53 if (a == b)
54 return 1;
49e794ac
JB
55 /* If A is a function block, then A cannot be contained in B,
56 except if A was inlined. */
57 if (BLOCK_FUNCTION (a) != NULL && !block_inlined_p (a))
58 return 0;
edb3359d
DJ
59 a = BLOCK_SUPERBLOCK (a);
60 }
61 while (a != NULL);
62
63 return 0;
fe898f56
DC
64}
65
66
67/* Return the symbol for the function which contains a specified
7f0df278
DJ
68 lexical block, described by a struct block BL. The return value
69 will not be an inlined function; the containing function will be
70 returned instead. */
fe898f56
DC
71
72struct symbol *
7f0df278 73block_linkage_function (const struct block *bl)
fe898f56 74{
edb3359d
DJ
75 while ((BLOCK_FUNCTION (bl) == NULL || block_inlined_p (bl))
76 && BLOCK_SUPERBLOCK (bl) != NULL)
fe898f56
DC
77 bl = BLOCK_SUPERBLOCK (bl);
78
79 return BLOCK_FUNCTION (bl);
80}
81
edb3359d
DJ
82/* Return one if BL represents an inlined function. */
83
84int
85block_inlined_p (const struct block *bl)
86{
87 return BLOCK_FUNCTION (bl) != NULL && SYMBOL_INLINED (BLOCK_FUNCTION (bl));
88}
89
801e3a5b
JB
90/* Return the blockvector immediately containing the innermost lexical
91 block containing the specified pc value and section, or 0 if there
92 is none. PBLOCK is a pointer to the block. If PBLOCK is NULL, we
93 don't pass this information back to the caller. */
fe898f56
DC
94
95struct blockvector *
714835d5 96blockvector_for_pc_sect (CORE_ADDR pc, struct obj_section *section,
801e3a5b 97 struct block **pblock, struct symtab *symtab)
fe898f56 98{
b59661bd
AC
99 struct block *b;
100 int bot, top, half;
fe898f56
DC
101 struct blockvector *bl;
102
103 if (symtab == 0) /* if no symtab specified by caller */
104 {
105 /* First search all symtabs for one whose file contains our pc */
b59661bd
AC
106 symtab = find_pc_sect_symtab (pc, section);
107 if (symtab == 0)
fe898f56
DC
108 return 0;
109 }
110
111 bl = BLOCKVECTOR (symtab);
fe898f56
DC
112
113 /* Then search that symtab for the smallest block that wins. */
fe898f56 114
801e3a5b
JB
115 /* If we have an addrmap mapping code addresses to blocks, then use
116 that. */
117 if (BLOCKVECTOR_MAP (bl))
118 {
119 b = addrmap_find (BLOCKVECTOR_MAP (bl), pc);
120 if (b)
121 {
122 if (pblock)
123 *pblock = b;
124 return bl;
125 }
126 else
127 return 0;
128 }
129
130
131 /* Otherwise, use binary search to find the last block that starts
132 before PC. */
fe898f56
DC
133 bot = 0;
134 top = BLOCKVECTOR_NBLOCKS (bl);
135
136 while (top - bot > 1)
137 {
138 half = (top - bot + 1) >> 1;
139 b = BLOCKVECTOR_BLOCK (bl, bot + half);
140 if (BLOCK_START (b) <= pc)
141 bot += half;
142 else
143 top = bot + half;
144 }
145
146 /* Now search backward for a block that ends after PC. */
147
148 while (bot >= 0)
149 {
150 b = BLOCKVECTOR_BLOCK (bl, bot);
151 if (BLOCK_END (b) > pc)
152 {
801e3a5b
JB
153 if (pblock)
154 *pblock = b;
fe898f56
DC
155 return bl;
156 }
157 bot--;
158 }
159 return 0;
160}
161
162/* Return the blockvector immediately containing the innermost lexical block
163 containing the specified pc value, or 0 if there is none.
164 Backward compatibility, no section. */
165
166struct blockvector *
801e3a5b 167blockvector_for_pc (CORE_ADDR pc, struct block **pblock)
fe898f56
DC
168{
169 return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
801e3a5b 170 pblock, NULL);
fe898f56
DC
171}
172
173/* Return the innermost lexical block containing the specified pc value
174 in the specified section, or 0 if there is none. */
175
176struct block *
714835d5 177block_for_pc_sect (CORE_ADDR pc, struct obj_section *section)
fe898f56 178{
b59661bd 179 struct blockvector *bl;
801e3a5b 180 struct block *b;
fe898f56 181
801e3a5b 182 bl = blockvector_for_pc_sect (pc, section, &b, NULL);
fe898f56 183 if (bl)
801e3a5b 184 return b;
fe898f56
DC
185 return 0;
186}
187
188/* Return the innermost lexical block containing the specified pc value,
189 or 0 if there is none. Backward compatibility, no section. */
190
191struct block *
b59661bd 192block_for_pc (CORE_ADDR pc)
fe898f56
DC
193{
194 return block_for_pc_sect (pc, find_pc_mapped_section (pc));
195}
9219021c 196
1fcb5155
DC
197/* Now come some functions designed to deal with C++ namespace issues.
198 The accessors are safe to use even in the non-C++ case. */
199
200/* This returns the namespace that BLOCK is enclosed in, or "" if it
201 isn't enclosed in a namespace at all. This travels the chain of
202 superblocks looking for a scope, if necessary. */
203
204const char *
205block_scope (const struct block *block)
206{
207 for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
208 {
209 if (BLOCK_NAMESPACE (block) != NULL
210 && BLOCK_NAMESPACE (block)->scope != NULL)
211 return BLOCK_NAMESPACE (block)->scope;
212 }
213
214 return "";
215}
9219021c
DC
216
217/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
218 OBSTACK. (It won't make a copy of SCOPE, however, so that already
219 has to be allocated correctly.) */
220
221void
222block_set_scope (struct block *block, const char *scope,
223 struct obstack *obstack)
224{
225 block_initialize_namespace (block, obstack);
226
227 BLOCK_NAMESPACE (block)->scope = scope;
228}
229
27aa8d6a 230/* This returns the using directives list associated with BLOCK, if
1fcb5155
DC
231 any. */
232
1fcb5155
DC
233struct using_direct *
234block_using (const struct block *block)
235{
27aa8d6a 236 if (block == NULL || BLOCK_NAMESPACE (block) == NULL)
1fcb5155
DC
237 return NULL;
238 else
27aa8d6a 239 return BLOCK_NAMESPACE (block)->using;
1fcb5155
DC
240}
241
9219021c
DC
242/* Set BLOCK's using member to USING; if needed, allocate memory via
243 OBSTACK. (It won't make a copy of USING, however, so that already
244 has to be allocated correctly.) */
245
246void
247block_set_using (struct block *block,
248 struct using_direct *using,
249 struct obstack *obstack)
250{
251 block_initialize_namespace (block, obstack);
252
253 BLOCK_NAMESPACE (block)->using = using;
254}
255
256/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
257 ititialize its members to zero. */
258
259static void
260block_initialize_namespace (struct block *block, struct obstack *obstack)
261{
262 if (BLOCK_NAMESPACE (block) == NULL)
263 {
264 BLOCK_NAMESPACE (block)
265 = obstack_alloc (obstack, sizeof (struct block_namespace_info));
266 BLOCK_NAMESPACE (block)->scope = NULL;
267 BLOCK_NAMESPACE (block)->using = NULL;
268 }
269}
89a9d1b1
DC
270
271/* Return the static block associated to BLOCK. Return NULL if block
272 is NULL or if block is a global block. */
273
274const struct block *
275block_static_block (const struct block *block)
276{
277 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
278 return NULL;
279
280 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
281 block = BLOCK_SUPERBLOCK (block);
282
283 return block;
284}
1fcb5155
DC
285
286/* Return the static block associated to BLOCK. Return NULL if block
287 is NULL. */
288
289const struct block *
290block_global_block (const struct block *block)
291{
292 if (block == NULL)
293 return NULL;
294
295 while (BLOCK_SUPERBLOCK (block) != NULL)
296 block = BLOCK_SUPERBLOCK (block);
297
298 return block;
299}
5c4e30ca
DC
300
301/* Allocate a block on OBSTACK, and initialize its elements to
302 zero/NULL. This is useful for creating "dummy" blocks that don't
303 correspond to actual source files.
304
305 Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
306 valid value. If you really don't want the block to have a
307 dictionary, then you should subsequently set its BLOCK_DICT to
308 dict_create_linear (obstack, NULL). */
309
310struct block *
311allocate_block (struct obstack *obstack)
312{
313 struct block *bl = obstack_alloc (obstack, sizeof (struct block));
314
315 BLOCK_START (bl) = 0;
316 BLOCK_END (bl) = 0;
317 BLOCK_FUNCTION (bl) = NULL;
318 BLOCK_SUPERBLOCK (bl) = NULL;
319 BLOCK_DICT (bl) = NULL;
320 BLOCK_NAMESPACE (bl) = NULL;
5c4e30ca
DC
321
322 return bl;
323}
This page took 0.432493 seconds and 4 git commands to generate.