1 //===-- CompileUnit.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/CompileUnit.h"
11 #include "lldb/Core/Module.h"
12 #include "lldb/Core/Language.h"
13 #include "lldb/Symbol/LineTable.h"
14 #include "lldb/Symbol/SymbolVendor.h"
15 #include "lldb/Symbol/VariableList.h"
18 using namespace lldb_private;
20 CompileUnit::CompileUnit (const lldb::ModuleSP &module_sp, void *user_data, const char *pathname, const lldb::user_id_t cu_sym_id, lldb::LanguageType language) :
21 ModuleChild(module_sp),
22 FileSpec (pathname, false),
24 m_user_data (user_data),
25 m_language (language),
32 if (language != eLanguageTypeUnknown)
33 m_flags.Set(flagsParsedLanguage);
37 CompileUnit::CompileUnit (const lldb::ModuleSP &module_sp, void *user_data, const FileSpec &fspec, const lldb::user_id_t cu_sym_id, lldb::LanguageType language) :
38 ModuleChild(module_sp),
41 m_user_data (user_data),
42 m_language (language),
49 if (language != eLanguageTypeUnknown)
50 m_flags.Set(flagsParsedLanguage);
54 CompileUnit::~CompileUnit ()
59 CompileUnit::CalculateSymbolContext(SymbolContext* sc)
62 GetModule()->CalculateSymbolContext(sc);
66 CompileUnit::CalculateSymbolContextModule ()
72 CompileUnit::CalculateSymbolContextCompileUnit ()
78 CompileUnit::DumpSymbolContext(Stream *s)
80 GetModule()->DumpSymbolContext(s);
81 s->Printf(", CompileUnit{0x%8.8" PRIx64 "}", GetID());
86 CompileUnit::GetDescription(Stream *s, lldb::DescriptionLevel level) const
88 Language language(m_language);
89 *s << "id = " << (const UserID&)*this << ", file = \"" << (const FileSpec&)*this << "\", language = \"" << language << '"';
93 //----------------------------------------------------------------------
94 // Dump the current contents of this object. No functions that cause on
95 // demand parsing of functions, globals, statics are called, so this
96 // is a good function to call to get an idea of the current contents of
97 // the CompileUnit object.
98 //----------------------------------------------------------------------
100 CompileUnit::Dump(Stream *s, bool show_context) const
102 s->Printf("%p: ", this);
104 *s << "CompileUnit" << (const UserID&)*this
105 << ", language = \"" << (const Language&)*this
106 << "\", file = '" << (const FileSpec&)*this << "'\n";
110 if (m_variables.get())
113 m_variables->Dump(s, show_context);
117 if (!m_functions.empty())
120 std::vector<FunctionSP>::const_iterator pos;
121 std::vector<FunctionSP>::const_iterator end = m_functions.end();
122 for (pos = m_functions.begin(); pos != end; ++pos)
124 (*pos)->Dump(s, show_context);
132 //----------------------------------------------------------------------
133 // Add a function to this compile unit
134 //----------------------------------------------------------------------
136 CompileUnit::AddFunction(FunctionSP& funcSP)
138 // TODO: order these by address
139 m_functions.push_back(funcSP);
143 CompileUnit::GetFunctionAtIndex (size_t idx)
146 if (idx < m_functions.size())
147 funcSP = m_functions[idx];
151 //----------------------------------------------------------------------
152 // Find functions using the a Mangled::Tokens token list. This
153 // function currently implements an interative approach designed to find
154 // all instances of certain functions. It isn't designed to the the
155 // quickest way to lookup functions as it will need to iterate through
156 // all functions and see if they match, though it does provide a powerful
157 // and context sensitive way to search for all functions with a certain
158 // name, all functions in a namespace, or all functions of a template
159 // type. See Mangled::Tokens::Parse() comments for more information.
161 // The function prototype will need to change to return a list of
162 // results. It was originally used to help debug the Mangled class
163 // and the Mangled::Tokens::MatchesQuery() function and it currently
164 // will print out a list of matching results for the functions that
165 // are currently in this compile unit.
167 // A FindFunctions method should be called prior to this that takes
168 // a regular function name (const char * or ConstString as a parameter)
169 // before resorting to this slower but more complete function. The
170 // other FindFunctions method should be able to take advantage of any
171 // accelerator tables available in the debug information (which is
172 // parsed by the SymbolFile parser plug-ins and registered with each
174 //----------------------------------------------------------------------
176 //CompileUnit::FindFunctions(const Mangled::Tokens& tokens)
178 // if (!m_functions.empty())
181 // std::vector<FunctionSP>::const_iterator pos;
182 // std::vector<FunctionSP>::const_iterator end = m_functions.end();
183 // for (pos = m_functions.begin(); pos != end; ++pos)
185 // const ConstString& demangled = (*pos)->Mangled().Demangled();
188 // const Mangled::Tokens& func_tokens = (*pos)->Mangled().GetTokens();
189 // if (func_tokens.MatchesQuery (tokens))
190 // s << "demangled MATCH found: " << demangled << "\n";
197 CompileUnit::FindFunctionByUID (lldb::user_id_t func_uid)
200 if (!m_functions.empty())
202 std::vector<FunctionSP>::const_iterator pos;
203 std::vector<FunctionSP>::const_iterator end = m_functions.end();
204 for (pos = m_functions.begin(); pos != end; ++pos)
206 if ((*pos)->GetID() == func_uid)
218 CompileUnit::GetLanguage()
220 if (m_language == eLanguageTypeUnknown)
222 if (m_flags.IsClear(flagsParsedLanguage))
224 m_flags.Set(flagsParsedLanguage);
225 SymbolVendor* symbol_vendor = GetModule()->GetSymbolVendor();
229 CalculateSymbolContext(&sc);
230 m_language = symbol_vendor->ParseCompileUnitLanguage(sc);
238 CompileUnit::GetLineTable()
240 if (m_line_table_ap.get() == NULL)
242 if (m_flags.IsClear(flagsParsedLineTable))
244 m_flags.Set(flagsParsedLineTable);
245 SymbolVendor* symbol_vendor = GetModule()->GetSymbolVendor();
249 CalculateSymbolContext(&sc);
250 symbol_vendor->ParseCompileUnitLineTable(sc);
254 return m_line_table_ap.get();
258 CompileUnit::SetLineTable(LineTable* line_table)
260 if (line_table == NULL)
261 m_flags.Clear(flagsParsedLineTable);
263 m_flags.Set(flagsParsedLineTable);
264 m_line_table_ap.reset(line_table);
268 CompileUnit::GetVariableList(bool can_create)
270 if (m_variables.get() == NULL && can_create)
273 CalculateSymbolContext(&sc);
274 assert(sc.module_sp);
275 sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
282 CompileUnit::FindLineEntry (uint32_t start_idx, uint32_t line, const FileSpec* file_spec_ptr, bool exact, LineEntry *line_entry_ptr)
284 uint32_t file_idx = 0;
288 file_idx = GetSupportFiles().FindFileIndex (1, *file_spec_ptr, true);
289 if (file_idx == UINT32_MAX)
294 // All the line table entries actually point to the version of the Compile
295 // Unit that is in the support files (the one at 0 was artifically added.)
296 // So prefer the one further on in the support files if it exists...
297 FileSpecList &support_files = GetSupportFiles();
298 const bool full = true;
299 file_idx = support_files.FindFileIndex (1, support_files.GetFileSpecAtIndex(0), full);
300 if (file_idx == UINT32_MAX)
303 LineTable *line_table = GetLineTable();
305 return line_table->FindLineEntryIndexByFileIndex (start_idx, file_idx, line, exact, line_entry_ptr);
313 CompileUnit::ResolveSymbolContext
315 const FileSpec& file_spec,
319 uint32_t resolve_scope,
320 SymbolContextList &sc_list
323 // First find all of the file indexes that match our "file_spec". If
324 // "file_spec" has an empty directory, then only compare the basenames
325 // when finding file indexes
326 std::vector<uint32_t> file_indexes;
327 const bool full_match = (bool)file_spec.GetDirectory();
328 bool file_spec_matches_cu_file_spec = FileSpec::Equal(file_spec, *this, full_match);
330 // If we are not looking for inlined functions and our file spec doesn't
331 // match then we are done...
332 if (file_spec_matches_cu_file_spec == false && check_inlines == false)
335 uint32_t file_idx = GetSupportFiles().FindFileIndex (1, file_spec, true);
336 while (file_idx != UINT32_MAX)
338 file_indexes.push_back (file_idx);
339 file_idx = GetSupportFiles().FindFileIndex (file_idx + 1, file_spec, true);
342 const size_t num_file_indexes = file_indexes.size();
343 if (num_file_indexes == 0)
346 const uint32_t prev_size = sc_list.GetSize();
348 SymbolContext sc(GetModule());
354 LineTable *line_table = sc.comp_unit->GetLineTable();
356 if (line_table != NULL)
361 if (num_file_indexes == 1)
363 // We only have a single support file that matches, so use
364 // the line table function that searches for a line entries
365 // that match a single support file index
366 LineEntry line_entry;
367 line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_indexes.front(), line, exact, &line_entry);
369 // If "exact == true", then "found_line" will be the same
370 // as "line". If "exact == false", the "found_line" will be the
371 // closest line entry with a line number greater than "line" and
372 // we will use this for our subsequent line exact matches below.
373 found_line = line_entry.line;
375 while (line_idx != UINT32_MAX)
377 // If they only asked for the line entry, then we're done, we can just copy that over.
378 // But if they wanted more than just the line number, fill it in.
379 if (resolve_scope == eSymbolContextLineEntry)
381 sc.line_entry = line_entry;
385 line_entry.range.GetBaseAddress().CalculateSymbolContext(&sc, resolve_scope);
389 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_indexes.front(), found_line, true, &line_entry);
394 // We found multiple support files that match "file_spec" so use
395 // the line table function that searches for a line entries
396 // that match a multiple support file indexes.
397 LineEntry line_entry;
398 line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_indexes, line, exact, &line_entry);
400 // If "exact == true", then "found_line" will be the same
401 // as "line". If "exact == false", the "found_line" will be the
402 // closest line entry with a line number greater than "line" and
403 // we will use this for our subsequent line exact matches below.
404 found_line = line_entry.line;
406 while (line_idx != UINT32_MAX)
408 if (resolve_scope == eSymbolContextLineEntry)
410 sc.line_entry = line_entry;
414 line_entry.range.GetBaseAddress().CalculateSymbolContext(&sc, resolve_scope);
418 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_indexes, found_line, true, &line_entry);
423 else if (file_spec_matches_cu_file_spec && !check_inlines)
425 // only append the context if we aren't looking for inline call sites
426 // by file and line and if the file spec matches that of the compile unit
429 return sc_list.GetSize() - prev_size;
433 CompileUnit::SetVariableList(VariableListSP &variables)
435 m_variables = variables;
439 CompileUnit::GetSupportFiles ()
441 if (m_support_files.GetSize() == 0)
443 if (m_flags.IsClear(flagsParsedSupportFiles))
445 m_flags.Set(flagsParsedSupportFiles);
446 SymbolVendor* symbol_vendor = GetModule()->GetSymbolVendor();
450 CalculateSymbolContext(&sc);
451 symbol_vendor->ParseCompileUnitSupportFiles(sc, m_support_files);
455 return m_support_files;
459 CompileUnit::GetUserData () const