blob: 3665f5b8f474a8621f37568c9e771d1db3478094 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CompileUnit.cpp -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Symbol/CompileUnit.h"
11#include "lldb/Symbol/LineTable.h"
12#include "lldb/Core/Module.h"
13#include "lldb/Symbol/SymbolVendor.h"
14#include "lldb/Symbol/VariableList.h"
15
16using namespace lldb;
17using namespace lldb_private;
18
Greg Clayton9488b742010-07-28 02:04:09 +000019CompileUnit::CompileUnit (Module *module, void *user_data, const char *pathname, const lldb::user_id_t cu_sym_id, lldb::LanguageType language) :
Chris Lattner24943d22010-06-08 16:52:24 +000020 ModuleChild(module),
21 FileSpec (pathname),
22 UserID(cu_sym_id),
23 Language (language),
24 m_user_data (user_data),
25 m_flags (0),
26 m_functions (),
27 m_support_files (),
28 m_line_table_ap (),
29 m_variables()
30{
31 assert(module != NULL);
32}
33
Greg Clayton9488b742010-07-28 02:04:09 +000034CompileUnit::CompileUnit (Module *module, void *user_data, const FileSpec &fspec, const lldb::user_id_t cu_sym_id, lldb::LanguageType language) :
Chris Lattner24943d22010-06-08 16:52:24 +000035 ModuleChild(module),
36 FileSpec (fspec),
37 UserID(cu_sym_id),
38 Language (language),
39 m_user_data (user_data),
40 m_flags (0),
41 m_functions (),
42 m_support_files (),
43 m_line_table_ap (),
44 m_variables()
45{
46 assert(module != NULL);
47}
48
49CompileUnit::~CompileUnit ()
50{
51}
52
53void
54CompileUnit::CalculateSymbolContext(SymbolContext* sc)
55{
56 sc->comp_unit = this;
57 GetModule()->CalculateSymbolContext(sc);
58}
59
60void
61CompileUnit::DumpSymbolContext(Stream *s)
62{
63 GetModule()->DumpSymbolContext(s);
64 s->Printf(", CompileUnit{0x%8.8x}", GetID());
65}
66
67
Greg Clayton12bec712010-06-28 21:30:43 +000068void
69CompileUnit::GetDescription(Stream *s, lldb::DescriptionLevel level) const
70{
Greg Claytonc67b7d12010-09-10 01:30:46 +000071 *s << "id = " << (const UserID&)*this << ", file = \"" << (const FileSpec&)*this << "\", language = \"" << (const Language&)*this << '"';
Greg Clayton12bec712010-06-28 21:30:43 +000072}
73
Chris Lattner24943d22010-06-08 16:52:24 +000074
75//----------------------------------------------------------------------
76// Dump the current contents of this object. No functions that cause on
77// demand parsing of functions, globals, statics are called, so this
78// is a good function to call to get an idea of the current contents of
79// the CompileUnit object.
80//----------------------------------------------------------------------
81void
82CompileUnit::Dump(Stream *s, bool show_context) const
83{
84 s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
85 s->Indent();
86 *s << "CompileUnit" << (const UserID&)*this
Greg Claytonc67b7d12010-09-10 01:30:46 +000087 << ", language = \"" << (const Language&)*this
88 << "\", file = '" << (const FileSpec&)*this << "'\n";
Chris Lattner24943d22010-06-08 16:52:24 +000089
90// m_types.Dump(s);
91
92 if (m_variables.get())
93 {
94 s->IndentMore();
95 m_variables->Dump(s, show_context);
96 s->IndentLess();
97 }
98
99 if (!m_functions.empty())
100 {
101 s->IndentMore();
102 std::vector<FunctionSP>::const_iterator pos;
103 std::vector<FunctionSP>::const_iterator end = m_functions.end();
104 for (pos = m_functions.begin(); pos != end; ++pos)
105 {
106 (*pos)->Dump(s, show_context);
107 }
108
109 s->IndentLess();
110 s->EOL();
111 }
112}
113
114//----------------------------------------------------------------------
115// Add a function to this compile unit
116//----------------------------------------------------------------------
117void
118CompileUnit::AddFunction(FunctionSP& funcSP)
119{
120 // TODO: order these by address
121 m_functions.push_back(funcSP);
122}
123
124FunctionSP
125CompileUnit::GetFunctionAtIndex (size_t idx)
126{
127 FunctionSP funcSP;
128 if (idx < m_functions.size())
129 funcSP = m_functions[idx];
130 return funcSP;
131}
132
133//----------------------------------------------------------------------
134// Find functions using the a Mangled::Tokens token list. This
135// function currently implements an interative approach designed to find
136// all instances of certain functions. It isn't designed to the the
137// quickest way to lookup functions as it will need to iterate through
138// all functions and see if they match, though it does provide a powerful
139// and context sensitive way to search for all functions with a certain
140// name, all functions in a namespace, or all functions of a template
141// type. See Mangled::Tokens::Parse() comments for more information.
142//
143// The function prototype will need to change to return a list of
144// results. It was originally used to help debug the Mangled class
145// and the Mangled::Tokens::MatchesQuery() function and it currently
146// will print out a list of matching results for the functions that
147// are currently in this compile unit.
148//
149// A FindFunctions method should be called prior to this that takes
150// a regular function name (const char * or ConstString as a parameter)
151// before resorting to this slower but more complete function. The
152// other FindFunctions method should be able to take advantage of any
153// accelerator tables available in the debug information (which is
154// parsed by the SymbolFile parser plug-ins and registered with each
155// Module).
156//----------------------------------------------------------------------
157//void
158//CompileUnit::FindFunctions(const Mangled::Tokens& tokens)
159//{
160// if (!m_functions.empty())
161// {
162// Stream s(stdout);
163// std::vector<FunctionSP>::const_iterator pos;
164// std::vector<FunctionSP>::const_iterator end = m_functions.end();
165// for (pos = m_functions.begin(); pos != end; ++pos)
166// {
167// const ConstString& demangled = (*pos)->Mangled().Demangled();
168// if (demangled)
169// {
170// const Mangled::Tokens& func_tokens = (*pos)->Mangled().GetTokens();
171// if (func_tokens.MatchesQuery (tokens))
172// s << "demangled MATCH found: " << demangled << "\n";
173// }
174// }
175// }
176//}
177
178FunctionSP
179CompileUnit::FindFunctionByUID (lldb::user_id_t func_uid)
180{
181 FunctionSP funcSP;
182 if (!m_functions.empty())
183 {
184 std::vector<FunctionSP>::const_iterator pos;
185 std::vector<FunctionSP>::const_iterator end = m_functions.end();
186 for (pos = m_functions.begin(); pos != end; ++pos)
187 {
188 if ((*pos)->GetID() == func_uid)
189 {
190 funcSP = *pos;
191 break;
192 }
193 }
194 }
195 return funcSP;
196}
197
198
199LineTable*
200CompileUnit::GetLineTable()
201{
202 if (m_line_table_ap.get() == NULL)
203 {
204 if (m_flags.IsClear(flagsParsedLineTable))
205 {
206 m_flags.Set(flagsParsedLineTable);
207 SymbolVendor* symbol_vendor = GetModule()->GetSymbolVendor();
208 if (symbol_vendor)
209 {
210 SymbolContext sc;
211 CalculateSymbolContext(&sc);
212 symbol_vendor->ParseCompileUnitLineTable(sc);
213 }
214 }
215 }
216 return m_line_table_ap.get();
217}
218
219void
220CompileUnit::SetLineTable(LineTable* line_table)
221{
222 if (line_table == NULL)
223 m_flags.Clear(flagsParsedLineTable);
224 else
225 m_flags.Set(flagsParsedLineTable);
226 m_line_table_ap.reset(line_table);
227}
228
229VariableListSP
230CompileUnit::GetVariableList(bool can_create)
231{
232 if (m_variables.get() == NULL && can_create)
233 {
234 SymbolContext sc;
235 CalculateSymbolContext(&sc);
236 assert(sc.module_sp);
237 sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
238 }
239
240 return m_variables;
241}
242
243uint32_t
244CompileUnit::FindLineEntry (uint32_t start_idx, uint32_t line, const FileSpec* file_spec_ptr, LineEntry *line_entry_ptr)
245{
246 uint32_t file_idx = 0;
247
248 if (file_spec_ptr)
249 {
250 file_idx = GetSupportFiles().FindFileIndex (1, *file_spec_ptr);
251 if (file_idx == UINT32_MAX)
252 return UINT32_MAX;
253 }
254 else
255 {
256 // All the line table entries actually point to the version of the Compile
257 // Unit that is in the support files (the one at 0 was artifically added.)
258 // So prefer the one further on in the support files if it exists...
259 FileSpecList &support_files = GetSupportFiles();
260 file_idx = support_files.FindFileIndex (1, support_files.GetFileSpecAtIndex(0));
261 if (file_idx == UINT32_MAX)
262 file_idx = 0;
263 }
264 LineTable *line_table = GetLineTable();
265 if (line_table)
266 return line_table->FindLineEntryIndexByFileIndex (start_idx, file_idx, line, true, line_entry_ptr);
267 return UINT32_MAX;
268}
269
270
271
272
273uint32_t
274CompileUnit::ResolveSymbolContext
275(
276 const FileSpec& file_spec,
277 uint32_t line,
278 bool check_inlines,
279 bool exact,
280 uint32_t resolve_scope,
281 SymbolContextList &sc_list
282)
283{
284 const uint32_t prev_size = sc_list.GetSize();
285 bool file_spec_matches_cu_file_spec = FileSpec::Compare(file_spec, this, !file_spec.GetDirectory().IsEmpty()) == 0;
286 if (check_inlines || file_spec_matches_cu_file_spec)
287 {
288 SymbolContext sc(GetModule());
289 sc.comp_unit = this;
290
291 uint32_t file_idx = UINT32_MAX;
292
293 // If we are looking for inline functions only and we don't
294 // find it in the support files, we are done.
295
296 if (check_inlines)
297 {
298 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec);
299 if (file_idx == UINT32_MAX)
300 return 0;
301 }
302
303 if (line != 0)
304 {
305 LineTable *line_table = sc.comp_unit->GetLineTable();
306
307 if (line_table != NULL)
308 {
309 // We will have already looked up the file index if
310 // we are searching for inline entries.
311 if (!check_inlines)
312 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec);
313
314 if (file_idx != UINT32_MAX)
315 {
316 uint32_t found_line;
317
318 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, exact, &sc.line_entry);
319 found_line = sc.line_entry.line;
320
321 while (line_idx != UINT_MAX)
322 {
323 sc_list.Append(sc);
324 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
325 }
326 }
327 }
328 }
329 else if (file_spec_matches_cu_file_spec && !check_inlines)
330 {
331 // only append the context if we aren't looking for inline call sites
332 // by file and line and if the file spec matches that of the compile unit
333 sc_list.Append(sc);
334 }
335
336 }
337 return sc_list.GetSize() - prev_size;
338}
339
340void
341CompileUnit::SetVariableList(VariableListSP &variables)
342{
343 m_variables = variables;
344}
345
346FileSpecList&
347CompileUnit::GetSupportFiles ()
348{
349 if (m_support_files.GetSize() == 0)
350 {
351 if (m_flags.IsClear(flagsParsedSupportFiles))
352 {
353 m_flags.Set(flagsParsedSupportFiles);
354 SymbolVendor* symbol_vendor = GetModule()->GetSymbolVendor();
355 if (symbol_vendor)
356 {
357 SymbolContext sc;
358 CalculateSymbolContext(&sc);
359 symbol_vendor->ParseCompileUnitSupportFiles(sc, m_support_files);
360 }
361 }
362 }
363 return m_support_files;
364}
365
366void *
367CompileUnit::GetUserData () const
368{
369 return m_user_data;
370}
371
372