1130803Smarcel/* Block-related functions for the GNU debugger, GDB.
2130803Smarcel
3130803Smarcel   Copyright 2003 Free Software Foundation, Inc.
4130803Smarcel
5130803Smarcel   This file is part of GDB.
6130803Smarcel
7130803Smarcel   This program is free software; you can redistribute it and/or modify
8130803Smarcel   it under the terms of the GNU General Public License as published by
9130803Smarcel   the Free Software Foundation; either version 2 of the License, or
10130803Smarcel   (at your option) any later version.
11130803Smarcel
12130803Smarcel   This program is distributed in the hope that it will be useful,
13130803Smarcel   but WITHOUT ANY WARRANTY; without even the implied warranty of
14130803Smarcel   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15130803Smarcel   GNU General Public License for more details.
16130803Smarcel
17130803Smarcel   You should have received a copy of the GNU General Public License
18130803Smarcel   along with this program; if not, write to the Free Software
19130803Smarcel   Foundation, Inc., 59 Temple Place - Suite 330,
20130803Smarcel   Boston, MA 02111-1307, USA.  */
21130803Smarcel
22130803Smarcel#include "defs.h"
23130803Smarcel#include "block.h"
24130803Smarcel#include "symtab.h"
25130803Smarcel#include "symfile.h"
26130803Smarcel#include "gdb_obstack.h"
27130803Smarcel#include "cp-support.h"
28130803Smarcel
29130803Smarcel/* This is used by struct block to store namespace-related info for
30130803Smarcel   C++ files, namely using declarations and the current namespace in
31130803Smarcel   scope.  */
32130803Smarcel
33130803Smarcelstruct block_namespace_info
34130803Smarcel{
35130803Smarcel  const char *scope;
36130803Smarcel  struct using_direct *using;
37130803Smarcel};
38130803Smarcel
39130803Smarcelstatic void block_initialize_namespace (struct block *block,
40130803Smarcel					struct obstack *obstack);
41130803Smarcel
42130803Smarcel/* Return Nonzero if block a is lexically nested within block b,
43130803Smarcel   or if a and b have the same pc range.
44130803Smarcel   Return zero otherwise. */
45130803Smarcel
46130803Smarcelint
47130803Smarcelcontained_in (const struct block *a, const struct block *b)
48130803Smarcel{
49130803Smarcel  if (!a || !b)
50130803Smarcel    return 0;
51130803Smarcel  return BLOCK_START (a) >= BLOCK_START (b)
52130803Smarcel    && BLOCK_END (a) <= BLOCK_END (b);
53130803Smarcel}
54130803Smarcel
55130803Smarcel
56130803Smarcel/* Return the symbol for the function which contains a specified
57130803Smarcel   lexical block, described by a struct block BL.  */
58130803Smarcel
59130803Smarcelstruct symbol *
60130803Smarcelblock_function (const struct block *bl)
61130803Smarcel{
62130803Smarcel  while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
63130803Smarcel    bl = BLOCK_SUPERBLOCK (bl);
64130803Smarcel
65130803Smarcel  return BLOCK_FUNCTION (bl);
66130803Smarcel}
67130803Smarcel
68130803Smarcel/* Return the blockvector immediately containing the innermost lexical block
69130803Smarcel   containing the specified pc value and section, or 0 if there is none.
70130803Smarcel   PINDEX is a pointer to the index value of the block.  If PINDEX
71130803Smarcel   is NULL, we don't pass this information back to the caller.  */
72130803Smarcel
73130803Smarcelstruct blockvector *
74130803Smarcelblockvector_for_pc_sect (CORE_ADDR pc, struct bfd_section *section,
75130803Smarcel			 int *pindex, struct symtab *symtab)
76130803Smarcel{
77130803Smarcel  struct block *b;
78130803Smarcel  int bot, top, half;
79130803Smarcel  struct blockvector *bl;
80130803Smarcel
81130803Smarcel  if (symtab == 0)		/* if no symtab specified by caller */
82130803Smarcel    {
83130803Smarcel      /* First search all symtabs for one whose file contains our pc */
84130803Smarcel      symtab = find_pc_sect_symtab (pc, section);
85130803Smarcel      if (symtab == 0)
86130803Smarcel	return 0;
87130803Smarcel    }
88130803Smarcel
89130803Smarcel  bl = BLOCKVECTOR (symtab);
90130803Smarcel  b = BLOCKVECTOR_BLOCK (bl, 0);
91130803Smarcel
92130803Smarcel  /* Then search that symtab for the smallest block that wins.  */
93130803Smarcel  /* Use binary search to find the last block that starts before PC.  */
94130803Smarcel
95130803Smarcel  bot = 0;
96130803Smarcel  top = BLOCKVECTOR_NBLOCKS (bl);
97130803Smarcel
98130803Smarcel  while (top - bot > 1)
99130803Smarcel    {
100130803Smarcel      half = (top - bot + 1) >> 1;
101130803Smarcel      b = BLOCKVECTOR_BLOCK (bl, bot + half);
102130803Smarcel      if (BLOCK_START (b) <= pc)
103130803Smarcel	bot += half;
104130803Smarcel      else
105130803Smarcel	top = bot + half;
106130803Smarcel    }
107130803Smarcel
108130803Smarcel  /* Now search backward for a block that ends after PC.  */
109130803Smarcel
110130803Smarcel  while (bot >= 0)
111130803Smarcel    {
112130803Smarcel      b = BLOCKVECTOR_BLOCK (bl, bot);
113130803Smarcel      if (BLOCK_END (b) > pc)
114130803Smarcel	{
115130803Smarcel	  if (pindex)
116130803Smarcel	    *pindex = bot;
117130803Smarcel	  return bl;
118130803Smarcel	}
119130803Smarcel      bot--;
120130803Smarcel    }
121130803Smarcel  return 0;
122130803Smarcel}
123130803Smarcel
124130803Smarcel/* Return the blockvector immediately containing the innermost lexical block
125130803Smarcel   containing the specified pc value, or 0 if there is none.
126130803Smarcel   Backward compatibility, no section.  */
127130803Smarcel
128130803Smarcelstruct blockvector *
129130803Smarcelblockvector_for_pc (CORE_ADDR pc, int *pindex)
130130803Smarcel{
131130803Smarcel  return blockvector_for_pc_sect (pc, find_pc_mapped_section (pc),
132130803Smarcel				  pindex, NULL);
133130803Smarcel}
134130803Smarcel
135130803Smarcel/* Return the innermost lexical block containing the specified pc value
136130803Smarcel   in the specified section, or 0 if there is none.  */
137130803Smarcel
138130803Smarcelstruct block *
139130803Smarcelblock_for_pc_sect (CORE_ADDR pc, struct bfd_section *section)
140130803Smarcel{
141130803Smarcel  struct blockvector *bl;
142130803Smarcel  int index;
143130803Smarcel
144130803Smarcel  bl = blockvector_for_pc_sect (pc, section, &index, NULL);
145130803Smarcel  if (bl)
146130803Smarcel    return BLOCKVECTOR_BLOCK (bl, index);
147130803Smarcel  return 0;
148130803Smarcel}
149130803Smarcel
150130803Smarcel/* Return the innermost lexical block containing the specified pc value,
151130803Smarcel   or 0 if there is none.  Backward compatibility, no section.  */
152130803Smarcel
153130803Smarcelstruct block *
154130803Smarcelblock_for_pc (CORE_ADDR pc)
155130803Smarcel{
156130803Smarcel  return block_for_pc_sect (pc, find_pc_mapped_section (pc));
157130803Smarcel}
158130803Smarcel
159130803Smarcel/* Now come some functions designed to deal with C++ namespace issues.
160130803Smarcel   The accessors are safe to use even in the non-C++ case.  */
161130803Smarcel
162130803Smarcel/* This returns the namespace that BLOCK is enclosed in, or "" if it
163130803Smarcel   isn't enclosed in a namespace at all.  This travels the chain of
164130803Smarcel   superblocks looking for a scope, if necessary.  */
165130803Smarcel
166130803Smarcelconst char *
167130803Smarcelblock_scope (const struct block *block)
168130803Smarcel{
169130803Smarcel  for (; block != NULL; block = BLOCK_SUPERBLOCK (block))
170130803Smarcel    {
171130803Smarcel      if (BLOCK_NAMESPACE (block) != NULL
172130803Smarcel	  && BLOCK_NAMESPACE (block)->scope != NULL)
173130803Smarcel	return BLOCK_NAMESPACE (block)->scope;
174130803Smarcel    }
175130803Smarcel
176130803Smarcel  return "";
177130803Smarcel}
178130803Smarcel
179130803Smarcel/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
180130803Smarcel   OBSTACK.  (It won't make a copy of SCOPE, however, so that already
181130803Smarcel   has to be allocated correctly.)  */
182130803Smarcel
183130803Smarcelvoid
184130803Smarcelblock_set_scope (struct block *block, const char *scope,
185130803Smarcel		 struct obstack *obstack)
186130803Smarcel{
187130803Smarcel  block_initialize_namespace (block, obstack);
188130803Smarcel
189130803Smarcel  BLOCK_NAMESPACE (block)->scope = scope;
190130803Smarcel}
191130803Smarcel
192130803Smarcel/* This returns the first using directives associated to BLOCK, if
193130803Smarcel   any.  */
194130803Smarcel
195130803Smarcel/* FIXME: carlton/2003-04-23: This uses the fact that we currently
196130803Smarcel   only have using directives in static blocks, because we only
197130803Smarcel   generate using directives from anonymous namespaces.  Eventually,
198130803Smarcel   when we support using directives everywhere, we'll want to replace
199130803Smarcel   this by some iterator functions.  */
200130803Smarcel
201130803Smarcelstruct using_direct *
202130803Smarcelblock_using (const struct block *block)
203130803Smarcel{
204130803Smarcel  const struct block *static_block = block_static_block (block);
205130803Smarcel
206130803Smarcel  if (static_block == NULL
207130803Smarcel      || BLOCK_NAMESPACE (static_block) == NULL)
208130803Smarcel    return NULL;
209130803Smarcel  else
210130803Smarcel    return BLOCK_NAMESPACE (static_block)->using;
211130803Smarcel}
212130803Smarcel
213130803Smarcel/* Set BLOCK's using member to USING; if needed, allocate memory via
214130803Smarcel   OBSTACK.  (It won't make a copy of USING, however, so that already
215130803Smarcel   has to be allocated correctly.)  */
216130803Smarcel
217130803Smarcelvoid
218130803Smarcelblock_set_using (struct block *block,
219130803Smarcel		 struct using_direct *using,
220130803Smarcel		 struct obstack *obstack)
221130803Smarcel{
222130803Smarcel  block_initialize_namespace (block, obstack);
223130803Smarcel
224130803Smarcel  BLOCK_NAMESPACE (block)->using = using;
225130803Smarcel}
226130803Smarcel
227130803Smarcel/* If BLOCK_NAMESPACE (block) is NULL, allocate it via OBSTACK and
228130803Smarcel   ititialize its members to zero.  */
229130803Smarcel
230130803Smarcelstatic void
231130803Smarcelblock_initialize_namespace (struct block *block, struct obstack *obstack)
232130803Smarcel{
233130803Smarcel  if (BLOCK_NAMESPACE (block) == NULL)
234130803Smarcel    {
235130803Smarcel      BLOCK_NAMESPACE (block)
236130803Smarcel	= obstack_alloc (obstack, sizeof (struct block_namespace_info));
237130803Smarcel      BLOCK_NAMESPACE (block)->scope = NULL;
238130803Smarcel      BLOCK_NAMESPACE (block)->using = NULL;
239130803Smarcel    }
240130803Smarcel}
241130803Smarcel
242130803Smarcel/* Return the static block associated to BLOCK.  Return NULL if block
243130803Smarcel   is NULL or if block is a global block.  */
244130803Smarcel
245130803Smarcelconst struct block *
246130803Smarcelblock_static_block (const struct block *block)
247130803Smarcel{
248130803Smarcel  if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
249130803Smarcel    return NULL;
250130803Smarcel
251130803Smarcel  while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
252130803Smarcel    block = BLOCK_SUPERBLOCK (block);
253130803Smarcel
254130803Smarcel  return block;
255130803Smarcel}
256130803Smarcel
257130803Smarcel/* Return the static block associated to BLOCK.  Return NULL if block
258130803Smarcel   is NULL.  */
259130803Smarcel
260130803Smarcelconst struct block *
261130803Smarcelblock_global_block (const struct block *block)
262130803Smarcel{
263130803Smarcel  if (block == NULL)
264130803Smarcel    return NULL;
265130803Smarcel
266130803Smarcel  while (BLOCK_SUPERBLOCK (block) != NULL)
267130803Smarcel    block = BLOCK_SUPERBLOCK (block);
268130803Smarcel
269130803Smarcel  return block;
270130803Smarcel}
271130803Smarcel
272130803Smarcel/* Allocate a block on OBSTACK, and initialize its elements to
273130803Smarcel   zero/NULL.  This is useful for creating "dummy" blocks that don't
274130803Smarcel   correspond to actual source files.
275130803Smarcel
276130803Smarcel   Warning: it sets the block's BLOCK_DICT to NULL, which isn't a
277130803Smarcel   valid value.  If you really don't want the block to have a
278130803Smarcel   dictionary, then you should subsequently set its BLOCK_DICT to
279130803Smarcel   dict_create_linear (obstack, NULL).  */
280130803Smarcel
281130803Smarcelstruct block *
282130803Smarcelallocate_block (struct obstack *obstack)
283130803Smarcel{
284130803Smarcel  struct block *bl = obstack_alloc (obstack, sizeof (struct block));
285130803Smarcel
286130803Smarcel  BLOCK_START (bl) = 0;
287130803Smarcel  BLOCK_END (bl) = 0;
288130803Smarcel  BLOCK_FUNCTION (bl) = NULL;
289130803Smarcel  BLOCK_SUPERBLOCK (bl) = NULL;
290130803Smarcel  BLOCK_DICT (bl) = NULL;
291130803Smarcel  BLOCK_NAMESPACE (bl) = NULL;
292130803Smarcel  BLOCK_GCC_COMPILED (bl) = 0;
293130803Smarcel
294130803Smarcel  return bl;
295130803Smarcel}
296