1/* Block-related functions for the GNU debugger, GDB.
2
3   Copyright (C) 2003, 2007 Free Software Foundation, Inc.
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
9   the Free Software Foundation; either version 3 of the License, or
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
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20#include "defs.h"
21#include "block.h"
22#include "symtab.h"
23#include "symfile.h"
24#include "gdb_obstack.h"
25#include "cp-support.h"
26
27/* This is used by struct block to store namespace-related info for
28   C++ files, namely using declarations and the current namespace in
29   scope.  */
30
31struct block_namespace_info
32{
33  const char *scope;
34  struct using_direct *using;
35};
36
37static void block_initialize_namespace (struct block *block,
38					struct obstack *obstack);
39
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. */
43
44int
45contained_in (const struct block *a, const struct block *b)
46{
47  if (!a || !b)
48    return 0;
49  return BLOCK_START (a) >= BLOCK_START (b)
50    && BLOCK_END (a) <= BLOCK_END (b);
51}
52
53
54/* Return the symbol for the function which contains a specified
55   lexical block, described by a struct block BL.  */
56
57struct symbol *
58block_function (const struct block *bl)
59{
60  while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
61    bl = BLOCK_SUPERBLOCK (bl);
62
63  return BLOCK_FUNCTION (bl);
64}
65
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.  */
70
71struct blockvector *
72blockvector_for_pc_sect (CORE_ADDR pc, struct bfd_section *section,
73			 int *pindex, struct symtab *symtab)
74{
75  struct block *b;
76  int bot, top, half;
77  struct blockvector *bl;
78
79  if (symtab == 0)		/* if no symtab specified by caller */
80    {
81      /* First search all symtabs for one whose file contains our pc */
82      symtab = find_pc_sect_symtab (pc, section);
83      if (symtab == 0)
84	return 0;
85    }
86
87  bl = BLOCKVECTOR (symtab);
88  b = BLOCKVECTOR_BLOCK (bl, 0);
89
90  /* Then search that symtab for the smallest block that wins.  */
91  /* Use binary search to find the last block that starts before PC.  */
92
93  bot = 0;
94  top = BLOCKVECTOR_NBLOCKS (bl);
95
96  while (top - bot > 1)
97    {
98      half = (top - bot + 1) >> 1;
99      b = BLOCKVECTOR_BLOCK (bl, bot + half);
100      if (BLOCK_START (b) <= pc)
101	bot += half;
102      else
103	top = bot + half;
104    }
105
106  /* Now search backward for a block that ends after PC.  */
107
108  while (bot >= 0)
109    {
110      b = BLOCKVECTOR_BLOCK (bl, bot);
111      if (BLOCK_END (b) > pc)
112	{
113	  if (pindex)
114	    *pindex = bot;
115	  return bl;
116	}
117      bot--;
118    }
119  return 0;
120}
121
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.  */
125
126struct blockvector *
127blockvector_for_pc (CORE_ADDR pc, int *pindex)
128{
129  return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
130				  pindex, NULL);
131}
132
133/* Return the innermost lexical block containing the specified pc value
134   in the specified section, or 0 if there is none.  */
135
136struct block *
137block_for_pc_sect (CORE_ADDR pc, struct bfd_section *section)
138{
139  struct blockvector *bl;
140  int index;
141
142  bl = blockvector_for_pc_sect (pc, section, &index, NULL);
143  if (bl)
144    return BLOCKVECTOR_BLOCK (bl, index);
145  return 0;
146}
147
148/* Return the innermost lexical block containing the specified pc value,
149   or 0 if there is none.  Backward compatibility, no section.  */
150
151struct block *
152block_for_pc (CORE_ADDR pc)
153{
154  return block_for_pc_sect (pc, find_pc_mapped_section (pc));
155}
156
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.  */
159
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.  */
163
164const char *
165block_scope (const struct block *block)
166{
167  for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
168    {
169      if (BLOCK_NAMESPACE (block) != NULL
170	  && BLOCK_NAMESPACE (block)->scope != NULL)
171	return BLOCK_NAMESPACE (block)->scope;
172    }
173
174  return "";
175}
176
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.)  */
180
181void
182block_set_scope (struct block *block, const char *scope,
183		 struct obstack *obstack)
184{
185  block_initialize_namespace (block, obstack);
186
187  BLOCK_NAMESPACE (block)->scope = scope;
188}
189
190/* This returns the first using directives associated to BLOCK, if
191   any.  */
192
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.  */
198
199struct using_direct *
200block_using (const struct block *block)
201{
202  const struct block *static_block = block_static_block (block);
203
204  if (static_block == NULL
205      || BLOCK_NAMESPACE (static_block) == NULL)
206    return NULL;
207  else
208    return BLOCK_NAMESPACE (static_block)->using;
209}
210
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.)  */
214
215void
216block_set_using (struct block *block,
217		 struct using_direct *using,
218		 struct obstack *obstack)
219{
220  block_initialize_namespace (block, obstack);
221
222  BLOCK_NAMESPACE (block)->using = using;
223}
224
225/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
226   ititialize its members to zero.  */
227
228static void
229block_initialize_namespace (struct block *block, struct obstack *obstack)
230{
231  if (BLOCK_NAMESPACE (block) == NULL)
232    {
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;
237    }
238}
239
240/* Return the static block associated to BLOCK.  Return NULL if block
241   is NULL or if block is a global block.  */
242
243const struct block *
244block_static_block (const struct block *block)
245{
246  if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
247    return NULL;
248
249  while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
250    block = BLOCK_SUPERBLOCK (block);
251
252  return block;
253}
254
255/* Return the static block associated to BLOCK.  Return NULL if block
256   is NULL.  */
257
258const struct block *
259block_global_block (const struct block *block)
260{
261  if (block == NULL)
262    return NULL;
263
264  while (BLOCK_SUPERBLOCK (block) != NULL)
265    block = BLOCK_SUPERBLOCK (block);
266
267  return block;
268}
269
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.
273
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).  */
278
279struct block *
280allocate_block (struct obstack *obstack)
281{
282  struct block *bl = obstack_alloc (obstack, sizeof (struct block));
283
284  BLOCK_START (bl) = 0;
285  BLOCK_END (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;
291
292  return bl;
293}
294