]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/SymbolFile/DWARF/DWARFASTParserOCaml.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / source / Plugins / SymbolFile / DWARF / DWARFASTParserOCaml.cpp
1 //===-- DWARFASTParserOCaml.cpp ---------------------------------*- C++ -*-===//
2
3 #include "DWARFASTParserOCaml.h"
4
5 #include "lldb/Core/Error.h"
6 #include "lldb/Core/Log.h"
7 #include "lldb/Core/Module.h"
8 #include "lldb/Symbol/CompileUnit.h"
9 #include "lldb/Symbol/Function.h"
10 #include "lldb/Symbol/ObjectFile.h"
11 #include "lldb/Symbol/Type.h"
12 #include "lldb/Symbol/TypeList.h"
13
14 using namespace lldb;
15 using namespace lldb_private;
16
17 DWARFASTParserOCaml::DWARFASTParserOCaml(OCamlASTContext &ast) : m_ast(ast) {}
18
19 DWARFASTParserOCaml::~DWARFASTParserOCaml() {}
20
21 TypeSP DWARFASTParserOCaml::ParseBaseTypeFromDIE(const DWARFDIE &die) {
22   SymbolFileDWARF *dwarf = die.GetDWARF();
23   dwarf->m_die_to_type[die.GetDIE()] = DIE_IS_BEING_PARSED;
24
25   ConstString type_name;
26   uint64_t byte_size = 0;
27
28   DWARFAttributes attributes;
29   const size_t num_attributes = die.GetAttributes(attributes);
30   for (uint32_t i = 0; i < num_attributes; ++i) {
31     DWARFFormValue form_value;
32     dw_attr_t attr = attributes.AttributeAtIndex(i);
33     if (attributes.ExtractFormValueAtIndex(i, form_value)) {
34       switch (attr) {
35       case DW_AT_name:
36         type_name.SetCString(form_value.AsCString());
37         break;
38       case DW_AT_byte_size:
39         byte_size = form_value.Unsigned();
40         break;
41       case DW_AT_encoding:
42         break;
43       default:
44         assert(false && "Unsupported attribute for DW_TAG_base_type");
45       }
46     }
47   }
48
49   Declaration decl;
50   CompilerType compiler_type = m_ast.CreateBaseType(type_name, byte_size);
51   return std::make_shared<Type>(die.GetID(), dwarf, type_name, byte_size,
52                                 nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID,
53                                 decl, compiler_type, Type::eResolveStateFull);
54 }
55
56 lldb::TypeSP DWARFASTParserOCaml::ParseTypeFromDWARF(const SymbolContext &sc,
57                                                      const DWARFDIE &die,
58                                                      Log *log,
59                                                      bool *type_is_new_ptr) {
60   if (type_is_new_ptr)
61     *type_is_new_ptr = false;
62
63   if (!die)
64     return nullptr;
65
66   SymbolFileDWARF *dwarf = die.GetDWARF();
67
68   Type *type_ptr = dwarf->m_die_to_type.lookup(die.GetDIE());
69   if (type_ptr == DIE_IS_BEING_PARSED)
70     return nullptr;
71   if (type_ptr != nullptr)
72     return type_ptr->shared_from_this();
73
74   TypeSP type_sp;
75   if (type_is_new_ptr)
76     *type_is_new_ptr = true;
77
78   switch (die.Tag()) {
79   case DW_TAG_base_type: {
80     type_sp = ParseBaseTypeFromDIE(die);
81     break;
82   }
83   case DW_TAG_array_type: {
84     break;
85   }
86   case DW_TAG_class_type: {
87     break;
88   }
89   case DW_TAG_reference_type: {
90     break;
91   }
92   }
93
94   if (!type_sp)
95     return nullptr;
96
97   DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
98   dw_tag_t sc_parent_tag = sc_parent_die.Tag();
99
100   SymbolContextScope *symbol_context_scope = nullptr;
101   if (sc_parent_tag == DW_TAG_compile_unit) {
102     symbol_context_scope = sc.comp_unit;
103   } else if (sc.function != nullptr && sc_parent_die) {
104     symbol_context_scope =
105         sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
106     if (symbol_context_scope == nullptr)
107       symbol_context_scope = sc.function;
108   }
109
110   if (symbol_context_scope != nullptr)
111     type_sp->SetSymbolContextScope(symbol_context_scope);
112
113   dwarf->GetTypeList()->Insert(type_sp);
114   dwarf->m_die_to_type[die.GetDIE()] = type_sp.get();
115
116   return type_sp;
117 }
118
119 Function *DWARFASTParserOCaml::ParseFunctionFromDWARF(const SymbolContext &sc,
120                                                       const DWARFDIE &die) {
121   DWARFRangeList func_ranges;
122   const char *name = NULL;
123   const char *mangled = NULL;
124   int decl_file = 0;
125   int decl_line = 0;
126   int decl_column = 0;
127   int call_file = 0;
128   int call_line = 0;
129   int call_column = 0;
130   DWARFExpression frame_base(die.GetCU());
131
132   Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_LANGUAGE));
133
134   if (die) {
135     SymbolFileDWARF *dwarf = die.GetDWARF();
136     if (log) {
137       dwarf->GetObjectFile()->GetModule()->LogMessage(
138           log, "DWARFASTParserOCaml::ParseFunctionFromDWARF (die = 0x%8.8x) %s "
139                "name = '%s')",
140           die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.GetName());
141     }
142   }
143
144   assert(die.Tag() == DW_TAG_subprogram);
145
146   if (die.Tag() != DW_TAG_subprogram)
147     return NULL;
148
149   if (die.GetDIENamesAndRanges(name, mangled, func_ranges, decl_file, decl_line,
150                                decl_column, call_file, call_line, call_column,
151                                &frame_base)) {
152     AddressRange func_range;
153     lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase(0);
154     lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd(0);
155     if (lowest_func_addr != LLDB_INVALID_ADDRESS &&
156         lowest_func_addr <= highest_func_addr) {
157       ModuleSP module_sp(die.GetModule());
158       func_range.GetBaseAddress().ResolveAddressUsingFileSections(
159           lowest_func_addr, module_sp->GetSectionList());
160       if (func_range.GetBaseAddress().IsValid())
161         func_range.SetByteSize(highest_func_addr - lowest_func_addr);
162     }
163
164     if (func_range.GetBaseAddress().IsValid()) {
165       Mangled func_name;
166
167       func_name.SetValue(ConstString(name), true);
168
169       FunctionSP func_sp;
170       std::unique_ptr<Declaration> decl_ap;
171       if (decl_file != 0 || decl_line != 0 || decl_column != 0)
172         decl_ap.reset(new Declaration(
173             sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
174             decl_line, decl_column));
175
176       SymbolFileDWARF *dwarf = die.GetDWARF();
177       Type *func_type = dwarf->m_die_to_type.lookup(die.GetDIE());
178
179       assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
180
181       if (dwarf->FixupAddress(func_range.GetBaseAddress())) {
182         const user_id_t func_user_id = die.GetID();
183         func_sp.reset(new Function(sc.comp_unit,
184                                    func_user_id, // UserID is the DIE offset
185                                    func_user_id, func_name, func_type,
186                                    func_range)); // first address range
187
188         if (func_sp.get() != NULL) {
189           if (frame_base.IsValid())
190             func_sp->GetFrameBaseExpression() = frame_base;
191           sc.comp_unit->AddFunction(func_sp);
192           return func_sp.get();
193         }
194       }
195     }
196   }
197
198   return NULL;
199 }
200
201 lldb_private::CompilerDeclContext
202 DWARFASTParserOCaml::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) {
203   return CompilerDeclContext();
204 }
205
206 lldb_private::CompilerDeclContext
207 DWARFASTParserOCaml::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) {
208   return CompilerDeclContext();
209 }