LLDB mainline
DWARFASTParserFortran.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the DWARF AST parser for the Fortran language.
11///
12//===----------------------------------------------------------------------===//
13
15#include "DWARFDIE.h"
16#include "DWARFDebugInfo.h"
17#include "DWARFDeclContext.h"
18#include "DWARFDefines.h"
19#include "LogChannelDWARF.h"
20#include "SymbolFileDWARF.h"
22#include "UniqueDWARFASTType.h"
23
25#include "lldb/Utility/Log.h"
27
28using namespace lldb;
29using namespace lldb_private;
30using namespace lldb_private::plugin::dwarf;
31using namespace llvm::dwarf;
32
34
38
40
42 const SymbolContext &sc, const DWARFDIE &die, TypeSP type_sp) {
43 if (!type_sp)
44 return type_sp;
45
47 dw_tag_t sc_parent_tag = sc_parent_die.Tag();
48
49 SymbolContextScope *symbol_context_scope = nullptr;
50 if (sc_parent_tag == DW_TAG_compile_unit ||
51 sc_parent_tag == DW_TAG_partial_unit) {
52 symbol_context_scope = sc.comp_unit;
53 } else if (!sc.function && sc_parent_die) {
54 symbol_context_scope =
55 sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
56 if (symbol_context_scope == nullptr)
57 symbol_context_scope = sc.function;
58 } else {
59 symbol_context_scope = sc.module_sp.get();
60 }
61
62 if (symbol_context_scope != nullptr)
63 type_sp->SetSymbolContextScope(symbol_context_scope);
64 return type_sp;
65}
66
68 const DWARFDIE &function_die,
71 if (!function_die)
72 return;
73
74 for (DWARFDIE die : function_die.children()) {
75 if (die.Tag() != DW_TAG_formal_parameter ||
76 die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
77 continue;
78
79 DWARFDIE param_type_die = die.GetAttributeValueAsReferenceDIE(DW_AT_type);
80 Type *type = die.ResolveTypeUID(param_type_die);
81 if (!type)
82 break;
83
84 param_names.emplace_back(die.GetName());
85 param_types.push_back(type->GetForwardCompilerType());
86 }
87 assert(param_names.size() == param_types.size());
88}
89
92 const lldb_private::plugin::dwarf::DWARFDIE &die, bool *type_is_new_ptr) {
93 TypeSP type_sp;
94 if (type_is_new_ptr)
95 *type_is_new_ptr = false;
96
98
99 if (!die)
100 return type_sp;
102 if (log) {
103 dwarf->GetObjectFile()->GetModule()->LogMessage(
104 log,
105 "DWARFASTParserFortran::ParseTypeFromDWARF (die = 0x%8.8x) %s name"
106 "= "
107 "'%s')",
109 die.GetName());
110 }
111
112 Type *type_ptr = dwarf->GetDIEToType().lookup(die.GetDIE());
113
114 if (type_ptr) {
115 if (type_ptr != DIE_IS_BEING_PARSED)
116 return type_ptr->shared_from_this();
117 else
118 return TypeSP();
119 }
120
121 if (type_is_new_ptr)
122 *type_is_new_ptr = true;
123
124 ConstString type_name;
125 const char *type_name_cstr = nullptr;
126 CompilerType compiler_type;
127 DWARFAttributes attributes;
128 DWARFFormValue form_value;
129 Declaration decl;
130 // We use 0x00 as a sentinel value, it doesn't map to any DW_ATE attribute
131 llvm::dwarf::TypeKind encoding = static_cast<llvm::dwarf::TypeKind>(0x00);
132
133 switch (const dw_tag_t tag = die.Tag()) {
134 case DW_TAG_base_type: {
135 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
136 attributes = die.GetAttributes();
137 uint64_t bit_size = 0;
138 for (size_t idx = 0; idx < attributes.Size(); idx++) {
139 if (attributes.ExtractFormValueAtIndex(idx, form_value)) {
140
141 switch (attributes.AttributeAtIndex(idx)) {
142 case DW_AT_name:
143 type_name_cstr = form_value.AsCString();
144
145 if (type_name_cstr && type_name_cstr[0] != '\0')
146 type_name.SetString(llvm::StringRef(type_name_cstr).upper());
147 else
148 type_name.SetCString("UNKNOWN_FORTRAN_TYPE");
149
150 break;
151 case DW_AT_encoding:
152 encoding = static_cast<llvm::dwarf::TypeKind>(form_value.Unsigned());
153 break;
154 case DW_AT_byte_size:
155 bit_size = form_value.Unsigned() * 8;
156 break;
157 case DW_AT_bit_size:
158 bit_size = form_value.Unsigned();
159 break;
160 default:
161 break;
162 }
163 }
164 }
165
166 compiler_type = m_ast.CreateBaseType(encoding, bit_size, type_name);
167 type_sp =
168 dwarf->MakeType(die.GetID(), type_name, llvm::divideCeil(bit_size, 8),
170 compiler_type, Type::ResolveState::Full);
171 break;
172 }
173 case DW_TAG_subprogram:
174 case DW_TAG_subroutine_type: {
175 CompilerType return_type;
176 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
177 attributes = die.GetAttributes();
178 size_t num_attr = attributes.Size();
179
180 for (size_t i = 0; i < num_attr; ++i) {
181 if (!attributes.ExtractFormValueAtIndex(i, form_value))
182 continue;
183 switch (attributes.AttributeAtIndex(i)) {
184 case DW_AT_name: {
185 const char *type_name_cstr = form_value.AsCString();
186 if (type_name_cstr && type_name_cstr[0] != '\0')
187 type_name.SetString(type_name_cstr);
188 else
189 type_name.SetCString("UNKNOWN_FORTRAN_FUNCTION");
190
191 break;
192 }
193 case DW_AT_type: {
194 if (DWARFDIE type_die = form_value.Reference())
195 if (Type *resolved_type = die.GetDWARF()->ResolveType(type_die))
196 return_type = resolved_type->GetForwardCompilerType();
197 break;
198 }
199
200 default:
201 break;
202 }
203 }
204
205 llvm::SmallVector<CompilerType, 4> param_types;
206 llvm::SmallVector<llvm::StringRef, 4> param_names;
207 ParseFunctionParameters(die, param_types, param_names);
208
209 compiler_type = m_ast.CreateFortranFunction(type_name, param_types,
210 param_names, return_type);
211
212 type_sp = dwarf->MakeType(die.GetID(), type_name, 0, nullptr,
214 compiler_type, Type::ResolveState::Full);
215 break;
216 }
217
218 default:
219 if (log)
220 dwarf->GetObjectFile()->GetModule()->LogMessage(
221 log, "[{0:x16}]: unhandled type tag {1:x4} ({2})", die.GetOffset(),
222 tag, DW_TAG_value_to_name(tag));
223
224 break;
225 }
226
227 UpdateSymbolContextScopeForType(sc, die, type_sp);
228 if (type_sp.get())
229 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
230
231 return type_sp;
232}
233
235 lldb_private::CompileUnit &comp_unit,
238 if (die.Tag() != DW_TAG_subprogram && die.Tag() != DW_TAG_subroutine_type)
239 return nullptr;
240
241 llvm::DWARFAddressRangesVector unused_func_ranges;
242 const char *name = nullptr;
243 const char *mangled = nullptr;
244 std::optional<int> decl_file = 0;
245 std::optional<int> decl_line = 0;
246 std::optional<int> decl_column = 0;
247 std::optional<int> call_file = 0;
248 std::optional<int> call_line = 0;
249 std::optional<int> call_column = 0;
250 DWARFExpressionList frame_base;
251 if (!die.GetDIENamesAndRanges(name, mangled, unused_func_ranges, decl_file,
252 decl_line, decl_column, call_file, call_line,
253 call_column, &frame_base))
254 return nullptr;
255
256 Mangled func_name;
257 // TODO: Once the Mangled class supports Fortran demangling, we should
258 // synthesize a demangled name here if GetDIENamesAndRanges fails to provide
259 // one.
260 if (mangled)
261 func_name.SetMangledName(ConstString(mangled));
262
263 if (name)
264 func_name.SetDemangledName(ConstString(name));
265
266 FunctionSP func_sp;
268 // Supply the type only if it has already been parsed
269 Type *func_type = dwarf->GetDIEToType().lookup(die.GetDIE());
270 assert(func_type == nullptr || func_type != DIE_IS_BEING_PARSED);
271 const user_id_t func_user_id = die.GetID();
272 Address func_addr = ranges[0].GetBaseAddress();
273
274 func_sp = std::make_shared<Function>(&comp_unit,
275 func_user_id, // UserID is the DIE offset
276 func_user_id, func_name, func_type,
277 std::move(func_addr), std::move(ranges));
278
279 if (func_sp.get() == nullptr)
280 return nullptr;
281
282 if (frame_base.IsValid())
283 func_sp->GetFrameBaseExpression() = frame_base;
284
285 comp_unit.AddFunction(func_sp);
286 return func_sp.get();
287}
288
This file defines the DWARF AST parser for the Fortran language.
#define DIE_IS_BEING_PARSED
bool CompleteTypeFromDWARF(const lldb_private::plugin::dwarf::DWARFDIE &die, lldb_private::Type *type, const lldb_private::CompilerType &compiler_type) override
void ParseFunctionParameters(const lldb_private::plugin::dwarf::DWARFDIE &function_die, llvm::SmallVectorImpl< lldb_private::CompilerType > &param_types, llvm::SmallVectorImpl< llvm::StringRef > &param_names)
lldb_private::Function * ParseFunctionFromDWARF(lldb_private::CompileUnit &comp_unit, const lldb_private::plugin::dwarf::DWARFDIE &die, lldb_private::AddressRanges ranges) override
DWARFASTParserFortran(lldb_private::TypeSystemFortran &m_ast)
lldb_private::TypeSystemFortran & m_ast
lldb::TypeSP UpdateSymbolContextScopeForType(const lldb_private::SymbolContext &sc, const lldb_private::plugin::dwarf::DWARFDIE &die, lldb::TypeSP type_sp)
If type_sp is valid, calculate and set its symbol context scope, and update the type list for its bac...
lldb::TypeSP ParseTypeFromDWARF(const lldb_private::SymbolContext &sc, const lldb_private::plugin::dwarf::DWARFDIE &die, bool *type_is_new_ptr) override
A section + offset based address class.
Definition Address.h:62
Block * FindBlockByID(lldb::user_id_t block_id)
Definition Block.cpp:113
A class that describes a compilation unit.
Definition CompileUnit.h:43
void AddFunction(lldb::FunctionSP &function_sp)
Add a function to this compile unit.
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
void SetCString(const char *cstr)
Set the C string value.
void SetString(llvm::StringRef s)
"lldb/Expression/DWARFExpressionList.h" Encapsulates a range map from file address range to a single ...
bool IsValid() const
Return true if the location expression contains data.
A class that describes the declaration location of a lldb object.
Definition Declaration.h:24
A class that describes a function.
Definition Function.h:386
Block & GetBlock(bool can_create)
Get accessor for the block list.
Definition Function.cpp:400
A class that handles mangled names.
Definition Mangled.h:36
void SetDemangledName(ConstString name)
Definition Mangled.h:161
void SetMangledName(ConstString name)
Definition Mangled.h:163
"lldb/Symbol/SymbolContextScope.h" Inherit from this if your object is part of a symbol context and c...
Defines a symbol context baton that can be handed other debug core functions.
Function * function
The Function for a given query.
lldb::ModuleSP module_sp
The Module for a given query.
CompileUnit * comp_unit
The CompileUnit for a given query.
CompilerType GetForwardCompilerType()
@ eEncodingIsUID
This type is the type whose UID is m_encoding_uid.
dw_attr_t AttributeAtIndex(uint32_t i) const
bool ExtractFormValueAtIndex(uint32_t i, DWARFFormValue &form_value) const
DWARFAttributes GetAttributes(Recurse recurse=Recurse::yes) const
bool GetDIENamesAndRanges(const char *&name, const char *&mangled, llvm::DWARFAddressRangesVector &ranges, std::optional< int > &decl_file, std::optional< int > &decl_line, std::optional< int > &decl_column, std::optional< int > &call_file, std::optional< int > &call_line, std::optional< int > &call_column, DWARFExpressionList *frame_base) const
Definition DWARFDIE.cpp:584
DWARFDIE GetDIE(dw_offset_t die_offset) const
Definition DWARFDIE.cpp:126
llvm::iterator_range< child_iterator > children() const
The range of all the children of this DIE.
Definition DWARFDIE.cpp:601
DWARFDIE GetAttributeValueAsReferenceDIE(const dw_attr_t attr) const
Definition DWARFDIE.cpp:134
Type * ResolveType(const DWARFDIE &die, bool assert_not_being_parsed=true, bool resolve_function_context=false)
static DWARFDIE GetParentSymbolContextDIE(const DWARFDIE &die)
llvm::dwarf::Tag dw_tag_t
Definition dwarf.h:19
#define LLDB_INVALID_UID
llvm::StringRef DW_TAG_value_to_name(dw_tag_t tag)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
std::shared_ptr< lldb_private::Function > FunctionSP
std::shared_ptr< lldb_private::Type > TypeSP
uint64_t user_id_t
Definition lldb-types.h:83