Introduce buildsym-legacy.h
[external/binutils.git] / gdb / buildsym-legacy.c
1 /* Legacy support routines for building symbol tables in GDB's internal format.
2    Copyright (C) 1986-2018 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "defs.h"
20 #include "buildsym-legacy.h"
21
22 /* The work-in-progress of the compunit we are building.
23    This is created first, before any subfiles by start_symtab.  */
24
25 static struct buildsym_compunit *buildsym_compunit;
26
27 void
28 record_debugformat (const char *format)
29 {
30   buildsym_compunit->record_debugformat (format);
31 }
32
33 void
34 record_producer (const char *producer)
35 {
36   buildsym_compunit->record_producer (producer);
37 }
38
39 \f
40
41 /* See buildsym.h.  */
42
43 void
44 set_last_source_file (const char *name)
45 {
46   gdb_assert (buildsym_compunit != nullptr || name == nullptr);
47   if (buildsym_compunit != nullptr)
48     buildsym_compunit->set_last_source_file (name);
49 }
50
51 /* See buildsym.h.  */
52
53 const char *
54 get_last_source_file ()
55 {
56   if (buildsym_compunit == nullptr)
57     return nullptr;
58   return buildsym_compunit->get_last_source_file ();
59 }
60
61 /* See buildsym.h.  */
62
63 void
64 set_last_source_start_addr (CORE_ADDR addr)
65 {
66   gdb_assert (buildsym_compunit != nullptr);
67   buildsym_compunit->set_last_source_start_addr (addr);
68 }
69
70 /* See buildsym.h.  */
71
72 CORE_ADDR
73 get_last_source_start_addr ()
74 {
75   gdb_assert (buildsym_compunit != nullptr);
76   return buildsym_compunit->get_last_source_start_addr ();
77 }
78
79 /* See buildsym.h.  */
80
81 struct using_direct **
82 get_local_using_directives ()
83 {
84   gdb_assert (buildsym_compunit != nullptr);
85   return buildsym_compunit->get_local_using_directives ();
86 }
87
88 /* See buildsym.h.  */
89
90 void
91 set_local_using_directives (struct using_direct *new_local)
92 {
93   gdb_assert (buildsym_compunit != nullptr);
94   buildsym_compunit->set_local_using_directives (new_local);
95 }
96
97 /* See buildsym.h.  */
98
99 struct using_direct **
100 get_global_using_directives ()
101 {
102   gdb_assert (buildsym_compunit != nullptr);
103   return buildsym_compunit->get_global_using_directives ();
104 }
105
106 /* See buildsym.h.  */
107
108 bool
109 outermost_context_p ()
110 {
111   gdb_assert (buildsym_compunit != nullptr);
112   return buildsym_compunit->outermost_context_p ();
113 }
114
115 /* See buildsym.h.  */
116
117 struct context_stack *
118 get_current_context_stack ()
119 {
120   gdb_assert (buildsym_compunit != nullptr);
121   return buildsym_compunit->get_current_context_stack ();
122 }
123
124 /* See buildsym.h.  */
125
126 int
127 get_context_stack_depth ()
128 {
129   gdb_assert (buildsym_compunit != nullptr);
130   return buildsym_compunit->get_context_stack_depth ();
131 }
132
133 /* See buildsym.h.  */
134
135 struct subfile *
136 get_current_subfile ()
137 {
138   gdb_assert (buildsym_compunit != nullptr);
139   return buildsym_compunit->get_current_subfile ();
140 }
141
142 /* See buildsym.h.  */
143
144 struct pending **
145 get_local_symbols ()
146 {
147   gdb_assert (buildsym_compunit != nullptr);
148   return buildsym_compunit->get_local_symbols ();
149 }
150
151 /* See buildsym.h.  */
152
153 struct pending **
154 get_file_symbols ()
155 {
156   gdb_assert (buildsym_compunit != nullptr);
157   return buildsym_compunit->get_file_symbols ();
158 }
159
160 /* See buildsym.h.  */
161
162 struct pending **
163 get_global_symbols ()
164 {
165   gdb_assert (buildsym_compunit != nullptr);
166   return buildsym_compunit->get_global_symbols ();
167 }
168
169 void
170 start_subfile (const char *name)
171 {
172   gdb_assert (buildsym_compunit != nullptr);
173   buildsym_compunit->start_subfile (name);
174 }
175
176 void
177 patch_subfile_names (struct subfile *subfile, const char *name)
178 {
179   gdb_assert (buildsym_compunit != nullptr);
180   buildsym_compunit->patch_subfile_names (subfile, name);
181 }
182
183 void
184 push_subfile ()
185 {
186   gdb_assert (buildsym_compunit != nullptr);
187   buildsym_compunit->push_subfile ();
188 }
189
190 const char *
191 pop_subfile ()
192 {
193   gdb_assert (buildsym_compunit != nullptr);
194   return buildsym_compunit->pop_subfile ();
195 }
196
197 struct block *
198 end_symtab_get_static_block (CORE_ADDR end_addr, int expandable, int required)
199 {
200   gdb_assert (buildsym_compunit != nullptr);
201   return buildsym_compunit->end_symtab_get_static_block (end_addr, expandable,
202                                                          required);
203 }
204
205 /* Delete the buildsym compunit.  */
206
207 static void
208 free_buildsym_compunit (void)
209 {
210   if (buildsym_compunit == NULL)
211     return;
212   delete buildsym_compunit;
213   buildsym_compunit = NULL;
214 }
215
216 struct compunit_symtab *
217 end_symtab_from_static_block (struct block *static_block,
218                               int section, int expandable)
219 {
220   gdb_assert (buildsym_compunit != nullptr);
221   struct compunit_symtab *result
222     = buildsym_compunit->end_symtab_from_static_block (static_block,
223                                                        section, expandable);
224   free_buildsym_compunit ();
225   return result;
226 }
227
228 struct compunit_symtab *
229 end_symtab (CORE_ADDR end_addr, int section)
230 {
231   gdb_assert (buildsym_compunit != nullptr);
232   struct compunit_symtab *result
233     = buildsym_compunit->end_symtab (end_addr, section);
234   free_buildsym_compunit ();
235   return result;
236 }
237
238 struct compunit_symtab *
239 end_expandable_symtab (CORE_ADDR end_addr, int section)
240 {
241   gdb_assert (buildsym_compunit != nullptr);
242   struct compunit_symtab *result
243     = buildsym_compunit->end_expandable_symtab (end_addr, section);
244   free_buildsym_compunit ();
245   return result;
246 }
247
248 void
249 augment_type_symtab ()
250 {
251   gdb_assert (buildsym_compunit != nullptr);
252   buildsym_compunit->augment_type_symtab ();
253   free_buildsym_compunit ();
254 }
255
256 struct context_stack *
257 push_context (int desc, CORE_ADDR valu)
258 {
259   gdb_assert (buildsym_compunit != nullptr);
260   return buildsym_compunit->push_context (desc, valu);
261 }
262
263 struct context_stack
264 pop_context ()
265 {
266   gdb_assert (buildsym_compunit != nullptr);
267   return buildsym_compunit->pop_context ();
268 }
269
270 struct block *
271 finish_block (struct symbol *symbol, struct pending_block *old_blocks,
272               const struct dynamic_prop *static_link,
273               CORE_ADDR start, CORE_ADDR end)
274 {
275   gdb_assert (buildsym_compunit != nullptr);
276   return buildsym_compunit->finish_block (symbol, old_blocks, static_link,
277                                           start, end);
278 }
279
280 void
281 record_block_range (struct block *block, CORE_ADDR start,
282                     CORE_ADDR end_inclusive)
283 {
284   gdb_assert (buildsym_compunit != nullptr);
285   buildsym_compunit->record_block_range (block, start, end_inclusive);
286 }
287
288 void
289 record_line (struct subfile *subfile, int line, CORE_ADDR pc)
290 {
291   gdb_assert (buildsym_compunit != nullptr);
292   buildsym_compunit->record_line (subfile, line, pc);
293 }
294
295 /* Start a new symtab for a new source file in OBJFILE.  Called, for example,
296    when a stabs symbol of type N_SO is seen, or when a DWARF
297    TAG_compile_unit DIE is seen.  It indicates the start of data for
298    one original source file.
299
300    NAME is the name of the file (cannot be NULL).  COMP_DIR is the
301    directory in which the file was compiled (or NULL if not known).
302    START_ADDR is the lowest address of objects in the file (or 0 if
303    not known).  LANGUAGE is the language of the source file, or
304    language_unknown if not known, in which case it'll be deduced from
305    the filename.  */
306
307 struct compunit_symtab *
308 start_symtab (struct objfile *objfile, const char *name, const char *comp_dir,
309               CORE_ADDR start_addr, enum language language)
310 {
311   /* These should have been reset either by successful completion of building
312      a symtab, or by the scoped_free_pendings destructor.  */
313   gdb_assert (buildsym_compunit == nullptr);
314
315   buildsym_compunit = new struct buildsym_compunit (objfile, name, comp_dir,
316                                                     language, start_addr);
317
318   return buildsym_compunit->get_compunit_symtab ();
319 }
320
321 /* Restart compilation for a symtab.
322    CUST is the result of end_expandable_symtab.
323    NAME, START_ADDR are the source file we are resuming with.
324
325    This is used when a symtab is built from multiple sources.
326    The symtab is first built with start_symtab/end_expandable_symtab
327    and then for each additional piece call restart_symtab/augment_*_symtab.
328    Note: At the moment there is only augment_type_symtab.  */
329
330 void
331 restart_symtab (struct compunit_symtab *cust,
332                 const char *name, CORE_ADDR start_addr)
333 {
334   /* These should have been reset either by successful completion of building
335      a symtab, or by the scoped_free_pendings destructor.  */
336   gdb_assert (buildsym_compunit == nullptr);
337
338   buildsym_compunit
339     = new struct buildsym_compunit (COMPUNIT_OBJFILE (cust),
340                                     name,
341                                     COMPUNIT_DIRNAME (cust),
342                                     compunit_language (cust),
343                                     start_addr,
344                                     cust);
345 }
346
347 /* See buildsym.h.  */
348
349 struct compunit_symtab *
350 buildsym_compunit_symtab (void)
351 {
352   gdb_assert (buildsym_compunit != NULL);
353
354   return buildsym_compunit->get_compunit_symtab ();
355 }
356
357 /* See buildsym.h.  */
358
359 struct macro_table *
360 get_macro_table (void)
361 {
362   struct objfile *objfile;
363
364   gdb_assert (buildsym_compunit != NULL);
365   return buildsym_compunit->get_macro_table ();
366 }
367
368 /* At end of reading syms, or in case of quit, ensure everything
369    associated with building symtabs is freed.
370
371    N.B. This is *not* intended to be used when building psymtabs.  Some debug
372    info readers call this anyway, which is harmless if confusing.  */
373
374 scoped_free_pendings::~scoped_free_pendings ()
375 {
376   free_buildsym_compunit ();
377 }