1 /* Code dealing with blocks for GDB.
3 Copyright (C) 2003-2017 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 3 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, see <http://www.gnu.org/licenses/>. */
23 #include "dictionary.h"
25 /* Opaque declarations. */
28 struct compunit_symtab
;
29 struct block_namespace_info
;
34 /* All of the name-scope contours of the program
35 are represented by `struct block' objects.
36 All of these objects are pointed to by the blockvector.
38 Each block represents one name scope.
39 Each lexical context has its own block.
41 The blockvector begins with some special blocks.
42 The GLOBAL_BLOCK contains all the symbols defined in this compilation
43 whose scope is the entire program linked together.
44 The STATIC_BLOCK contains all the symbols whose scope is the
45 entire compilation excluding other separate compilations.
46 Blocks starting with the FIRST_LOCAL_BLOCK are not special.
48 Each block records a range of core addresses for the code that
49 is in the scope of the block. The STATIC_BLOCK and GLOBAL_BLOCK
50 give, for the range of code, the entire range of code produced
51 by the compilation that the symbol segment belongs to.
53 The blocks appear in the blockvector
54 in order of increasing starting-address,
55 and, within that, in order of decreasing ending-address.
57 This implies that within the body of one function
58 the blocks appear in the order of a depth-first tree walk. */
63 /* Addresses in the executable code that are in this block. */
68 /* The symbol that names this block, if the block is the body of a
69 function (real or inlined); otherwise, zero. */
71 struct symbol
*function
;
73 /* The `struct block' for the containing block, or 0 if none.
75 The superblock of a top-level local block (i.e. a function in the
76 case of C) is the STATIC_BLOCK. The superblock of the
77 STATIC_BLOCK is the GLOBAL_BLOCK. */
79 struct block
*superblock
;
81 /* This is used to store the symbols in the block. */
83 struct dictionary
*dict
;
85 /* Contains information about namespace-related info relevant to this block:
86 using directives and the current namespace scope. */
88 struct block_namespace_info
*namespace_info
;
91 /* The global block is singled out so that we can provide a back-link
92 to the compunit symtab. */
100 /* This holds a pointer to the compunit symtab holding this block. */
102 struct compunit_symtab
*compunit_symtab
;
105 #define BLOCK_START(bl) (bl)->startaddr
106 #define BLOCK_END(bl) (bl)->endaddr
107 #define BLOCK_FUNCTION(bl) (bl)->function
108 #define BLOCK_SUPERBLOCK(bl) (bl)->superblock
109 #define BLOCK_DICT(bl) (bl)->dict
110 #define BLOCK_NAMESPACE(bl) (bl)->namespace_info
114 /* Number of blocks in the list. */
116 /* An address map mapping addresses to blocks in this blockvector.
117 This pointer is zero if the blocks' start and end addresses are
120 /* The blocks themselves. */
121 struct block
*block
[1];
124 #define BLOCKVECTOR_NBLOCKS(blocklist) (blocklist)->nblocks
125 #define BLOCKVECTOR_BLOCK(blocklist,n) (blocklist)->block[n]
126 #define BLOCKVECTOR_MAP(blocklist) ((blocklist)->map)
128 /* Return the objfile of BLOCK, which must be non-NULL. */
130 extern struct objfile
*block_objfile (const struct block
*block
);
132 /* Return the architecture of BLOCK, which must be non-NULL. */
134 extern struct gdbarch
*block_gdbarch (const struct block
*block
);
136 extern struct symbol
*block_linkage_function (const struct block
*);
138 extern struct symbol
*block_containing_function (const struct block
*);
140 extern int block_inlined_p (const struct block
*block
);
142 extern int contained_in (const struct block
*, const struct block
*);
144 extern const struct blockvector
*blockvector_for_pc (CORE_ADDR
,
145 const struct block
**);
147 extern const struct blockvector
*
148 blockvector_for_pc_sect (CORE_ADDR
, struct obj_section
*,
149 const struct block
**, struct compunit_symtab
*);
151 extern int blockvector_contains_pc (const struct blockvector
*bv
, CORE_ADDR pc
);
153 extern struct call_site
*call_site_for_pc (struct gdbarch
*gdbarch
,
156 extern const struct block
*block_for_pc (CORE_ADDR
);
158 extern const struct block
*block_for_pc_sect (CORE_ADDR
, struct obj_section
*);
160 extern const char *block_scope (const struct block
*block
);
162 extern void block_set_scope (struct block
*block
, const char *scope
,
163 struct obstack
*obstack
);
165 extern struct using_direct
*block_using (const struct block
*block
);
167 extern void block_set_using (struct block
*block
,
168 struct using_direct
*using_decl
,
169 struct obstack
*obstack
);
171 extern const struct block
*block_static_block (const struct block
*block
);
173 extern const struct block
*block_global_block (const struct block
*block
);
175 extern struct block
*allocate_block (struct obstack
*obstack
);
177 extern struct block
*allocate_global_block (struct obstack
*obstack
);
179 extern void set_block_compunit_symtab (struct block
*,
180 struct compunit_symtab
*);
182 /* Return a property to evaluate the static link associated to BLOCK.
184 In the context of nested functions (available in Pascal, Ada and GNU C, for
185 instance), a static link (as in DWARF's DW_AT_static_link attribute) for a
186 function is a way to get the frame corresponding to the enclosing function.
188 Note that only objfile-owned and function-level blocks can have a static
189 link. Return NULL if there is no such property. */
191 extern struct dynamic_prop
*block_static_link (const struct block
*block
);
193 /* A block iterator. This structure should be treated as though it
194 were opaque; it is only defined here because we want to support
195 stack allocation of iterators. */
197 struct block_iterator
199 /* If we're iterating over a single block, this holds the block.
200 Otherwise, it holds the canonical compunit. */
204 struct compunit_symtab
*compunit_symtab
;
205 const struct block
*block
;
208 /* If we're iterating over a single block, this is always -1.
209 Otherwise, it holds the index of the current "included" symtab in
210 the canonical symtab (that is, d.symtab->includes[idx]), with -1
211 meaning the canonical symtab itself. */
215 /* Which block, either static or global, to iterate over. If this
216 is FIRST_LOCAL_BLOCK, then we are iterating over a single block.
217 This is used to select which field of 'd' is in use. */
219 enum block_enum which
;
221 /* The underlying dictionary iterator. */
223 struct dict_iterator dict_iter
;
226 /* Initialize ITERATOR to point at the first symbol in BLOCK, and
227 return that first symbol, or NULL if BLOCK is empty. */
229 extern struct symbol
*block_iterator_first (const struct block
*block
,
230 struct block_iterator
*iterator
);
232 /* Advance ITERATOR, and return the next symbol, or NULL if there are
233 no more symbols. Don't call this if you've previously received
234 NULL from block_iterator_first or block_iterator_next on this
237 extern struct symbol
*block_iterator_next (struct block_iterator
*iterator
);
239 /* Initialize ITERATOR to point at the first symbol in BLOCK whose
240 SYMBOL_SEARCH_NAME matches NAME, and return that first symbol, or
241 NULL if there are no such symbols. */
243 extern struct symbol
*block_iter_match_first (const struct block
*block
,
244 const lookup_name_info
&name
,
245 struct block_iterator
*iterator
);
247 /* Advance ITERATOR to point at the next symbol in BLOCK whose
248 SYMBOL_SEARCH_NAME matches NAME, or NULL if there are no more such
249 symbols. Don't call this if you've previously received NULL from
250 block_iterator_match_first or block_iterator_match_next on this
251 iteration. And don't call it unless ITERATOR was created by a
252 previous call to block_iter_match_first with the same NAME. */
254 extern struct symbol
*block_iter_match_next
255 (const lookup_name_info
&name
, struct block_iterator
*iterator
);
257 /* Search BLOCK for symbol NAME in DOMAIN. */
259 extern struct symbol
*block_lookup_symbol (const struct block
*block
,
261 const domain_enum domain
);
263 /* Search BLOCK for symbol NAME in DOMAIN but only in primary symbol table of
264 BLOCK. BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. Function is useful if
265 one iterates all global/static blocks of an objfile. */
267 extern struct symbol
*block_lookup_symbol_primary (const struct block
*block
,
269 const domain_enum domain
);
271 /* The type of the MATCHER argument to block_find_symbol. */
273 typedef int (block_symbol_matcher_ftype
) (struct symbol
*, void *);
275 /* Find symbol NAME in BLOCK and in DOMAIN that satisfies MATCHER.
276 DATA is passed unchanged to MATCHER.
277 BLOCK must be STATIC_BLOCK or GLOBAL_BLOCK. */
279 extern struct symbol
*block_find_symbol (const struct block
*block
,
281 const domain_enum domain
,
282 block_symbol_matcher_ftype
*matcher
,
285 /* A matcher function for block_find_symbol to find only symbols with
288 extern int block_find_non_opaque_type (struct symbol
*sym
, void *data
);
290 /* A matcher function for block_find_symbol to prefer symbols with
291 non-opaque types. The way to use this function is as follows:
293 struct symbol *with_opaque = NULL;
295 = block_find_symbol (block, name, domain,
296 block_find_non_opaque_type_preferred, &with_opaque);
298 At this point if SYM is non-NULL then a non-opaque type has been found.
299 Otherwise, if WITH_OPAQUE is non-NULL then an opaque type has been found.
300 Otherwise, the symbol was not found. */
302 extern int block_find_non_opaque_type_preferred (struct symbol
*sym
,
305 /* Macro to loop through all symbols in BLOCK, in no particular
306 order. ITER helps keep track of the iteration, and must be a
307 struct block_iterator. SYM points to the current symbol. */
309 #define ALL_BLOCK_SYMBOLS(block, iter, sym) \
310 for ((sym) = block_iterator_first ((block), &(iter)); \
312 (sym) = block_iterator_next (&(iter)))
314 /* Macro to loop through all symbols in BLOCK with a name that matches
315 NAME, in no particular order. ITER helps keep track of the
316 iteration, and must be a struct block_iterator. SYM points to the
319 #define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym) \
320 for ((sym) = block_iter_match_first ((block), (name), &(iter)); \
322 (sym) = block_iter_match_next ((name), &(iter)))