1 | //===-- DWARFASTParserClang.h -----------------------------------*- C++ -*-===// |
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 | #ifndef LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H |
10 | #define LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H |
11 | |
12 | #include "clang/AST/CharUnits.h" |
13 | #include "clang/AST/Type.h" |
14 | #include "llvm/ADT/DenseMap.h" |
15 | #include "llvm/ADT/SmallPtrSet.h" |
16 | #include "llvm/ADT/SmallVector.h" |
17 | |
18 | #include "DWARFASTParser.h" |
19 | #include "DWARFDIE.h" |
20 | #include "DWARFDefines.h" |
21 | #include "DWARFFormValue.h" |
22 | #include "LogChannelDWARF.h" |
23 | #include "lldb/Core/PluginInterface.h" |
24 | |
25 | #include "Plugins/ExpressionParser/Clang/ClangASTImporter.h" |
26 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
27 | |
28 | #include <optional> |
29 | #include <vector> |
30 | |
31 | namespace lldb_private { |
32 | class CompileUnit; |
33 | } |
34 | namespace lldb_private::plugin { |
35 | namespace dwarf { |
36 | class DWARFDebugInfoEntry; |
37 | class SymbolFileDWARF; |
38 | } // namespace dwarf |
39 | } // namespace lldb_private::plugin |
40 | |
41 | struct ParsedDWARFTypeAttributes; |
42 | |
43 | class DWARFASTParserClang : public lldb_private::plugin::dwarf::DWARFASTParser { |
44 | public: |
45 | DWARFASTParserClang(lldb_private::TypeSystemClang &ast); |
46 | |
47 | ~DWARFASTParserClang() override; |
48 | |
49 | // DWARFASTParser interface. |
50 | lldb::TypeSP |
51 | ParseTypeFromDWARF(const lldb_private::SymbolContext &sc, |
52 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
53 | bool *type_is_new_ptr) override; |
54 | |
55 | lldb_private::ConstString ConstructDemangledNameFromDWARF( |
56 | const lldb_private::plugin::dwarf::DWARFDIE &die) override; |
57 | |
58 | lldb_private::Function * |
59 | ParseFunctionFromDWARF(lldb_private::CompileUnit &comp_unit, |
60 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
61 | const lldb_private::AddressRange &func_range) override; |
62 | |
63 | bool |
64 | CompleteTypeFromDWARF(const lldb_private::plugin::dwarf::DWARFDIE &die, |
65 | lldb_private::Type *type, |
66 | lldb_private::CompilerType &compiler_type) override; |
67 | |
68 | lldb_private::CompilerDecl GetDeclForUIDFromDWARF( |
69 | const lldb_private::plugin::dwarf::DWARFDIE &die) override; |
70 | |
71 | void EnsureAllDIEsInDeclContextHaveBeenParsed( |
72 | lldb_private::CompilerDeclContext decl_context) override; |
73 | |
74 | lldb_private::CompilerDeclContext GetDeclContextForUIDFromDWARF( |
75 | const lldb_private::plugin::dwarf::DWARFDIE &die) override; |
76 | |
77 | lldb_private::CompilerDeclContext GetDeclContextContainingUIDFromDWARF( |
78 | const lldb_private::plugin::dwarf::DWARFDIE &die) override; |
79 | |
80 | lldb_private::ClangASTImporter &GetClangASTImporter(); |
81 | |
82 | /// Extracts an value for a given Clang integer type from a DWARFFormValue. |
83 | /// |
84 | /// \param int_type The Clang type that defines the bit size and signedness |
85 | /// of the integer that should be extracted. Has to be either |
86 | /// an integer type or an enum type. For enum types the |
87 | /// underlying integer type will be considered as the |
88 | /// expected integer type that should be extracted. |
89 | /// \param form_value The DWARFFormValue that contains the integer value. |
90 | /// \return An APInt containing the same integer value as the given |
91 | /// DWARFFormValue with the bit width of the given integer type. |
92 | /// Returns an error if the value in the DWARFFormValue does not fit |
93 | /// into the given integer type or the integer type isn't supported. |
94 | llvm::Expected<llvm::APInt> ( |
95 | const lldb_private::CompilerType &int_type, |
96 | const lldb_private::plugin::dwarf::DWARFFormValue &form_value) const; |
97 | |
98 | /// Returns the template parameters of a class DWARFDIE as a string. |
99 | /// |
100 | /// This is mostly useful for -gsimple-template-names which omits template |
101 | /// parameters from the DIE name and instead always adds template parameter |
102 | /// children DIEs. |
103 | /// |
104 | /// \param die The struct/class DWARFDIE containing template parameters. |
105 | /// \return A string, including surrounding '<>', of the template parameters. |
106 | /// If the DIE's name already has '<>', returns an empty ConstString because |
107 | /// it's assumed that the caller is using the DIE name anyway. |
108 | lldb_private::ConstString GetDIEClassTemplateParams( |
109 | const lldb_private::plugin::dwarf::DWARFDIE &die) override; |
110 | |
111 | protected: |
112 | /// Protected typedefs and members. |
113 | /// @{ |
114 | class DelayedAddObjCClassProperty; |
115 | typedef std::vector<DelayedAddObjCClassProperty> DelayedPropertyList; |
116 | |
117 | typedef llvm::DenseMap< |
118 | const lldb_private::plugin::dwarf::DWARFDebugInfoEntry *, |
119 | clang::DeclContext *> |
120 | DIEToDeclContextMap; |
121 | typedef std::multimap<const clang::DeclContext *, |
122 | const lldb_private::plugin::dwarf::DWARFDIE> |
123 | DeclContextToDIEMap; |
124 | typedef llvm::DenseMap< |
125 | const lldb_private::plugin::dwarf::DWARFDebugInfoEntry *, |
126 | lldb_private::OptionalClangModuleID> |
127 | DIEToModuleMap; |
128 | typedef llvm::DenseMap< |
129 | const lldb_private::plugin::dwarf::DWARFDebugInfoEntry *, clang::Decl *> |
130 | DIEToDeclMap; |
131 | |
132 | lldb_private::TypeSystemClang &m_ast; |
133 | DIEToDeclMap m_die_to_decl; |
134 | DIEToDeclContextMap m_die_to_decl_ctx; |
135 | DeclContextToDIEMap m_decl_ctx_to_die; |
136 | DIEToModuleMap m_die_to_module; |
137 | std::unique_ptr<lldb_private::ClangASTImporter> m_clang_ast_importer_up; |
138 | /// @} |
139 | |
140 | clang::DeclContext * |
141 | GetDeclContextForBlock(const lldb_private::plugin::dwarf::DWARFDIE &die); |
142 | |
143 | clang::BlockDecl * |
144 | ResolveBlockDIE(const lldb_private::plugin::dwarf::DWARFDIE &die); |
145 | |
146 | clang::NamespaceDecl * |
147 | ResolveNamespaceDIE(const lldb_private::plugin::dwarf::DWARFDIE &die); |
148 | |
149 | /// Returns the namespace decl that a DW_TAG_imported_declaration imports. |
150 | /// |
151 | /// \param[in] die The import declaration to resolve. If the DIE is not a |
152 | /// DW_TAG_imported_declaration the behaviour is undefined. |
153 | /// |
154 | /// \returns The decl corresponding to the namespace that the specified |
155 | /// 'die' imports. If the imported entity is not a namespace |
156 | /// or another import declaration, returns nullptr. If an error |
157 | /// occurs, returns nullptr. |
158 | clang::NamespaceDecl *ResolveImportedDeclarationDIE( |
159 | const lldb_private::plugin::dwarf::DWARFDIE &die); |
160 | |
161 | bool ParseTemplateDIE(const lldb_private::plugin::dwarf::DWARFDIE &die, |
162 | lldb_private::TypeSystemClang::TemplateParameterInfos |
163 | &template_param_infos); |
164 | |
165 | bool ParseTemplateParameterInfos( |
166 | const lldb_private::plugin::dwarf::DWARFDIE &parent_die, |
167 | lldb_private::TypeSystemClang::TemplateParameterInfos |
168 | &template_param_infos); |
169 | |
170 | std::string |
171 | GetCPlusPlusQualifiedName(const lldb_private::plugin::dwarf::DWARFDIE &die); |
172 | |
173 | bool ParseChildMembers( |
174 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
175 | lldb_private::CompilerType &class_compiler_type, |
176 | std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes, |
177 | std::vector<lldb_private::plugin::dwarf::DWARFDIE> &member_function_dies, |
178 | std::vector<lldb_private::plugin::dwarf::DWARFDIE> &contained_type_dies, |
179 | DelayedPropertyList &delayed_properties, |
180 | const lldb::AccessType default_accessibility, |
181 | lldb_private::ClangASTImporter::LayoutInfo &layout_info); |
182 | |
183 | size_t |
184 | ParseChildParameters(clang::DeclContext *containing_decl_ctx, |
185 | const lldb_private::plugin::dwarf::DWARFDIE &parent_die, |
186 | bool skip_artificial, bool &is_static, bool &is_variadic, |
187 | bool &has_template_params, |
188 | std::vector<lldb_private::CompilerType> &function_args, |
189 | std::vector<clang::ParmVarDecl *> &function_param_decls, |
190 | unsigned &type_quals); |
191 | |
192 | size_t ParseChildEnumerators( |
193 | lldb_private::CompilerType &compiler_type, bool is_signed, |
194 | uint32_t enumerator_byte_size, |
195 | const lldb_private::plugin::dwarf::DWARFDIE &parent_die); |
196 | |
197 | /// Parse a structure, class, or union type DIE. |
198 | lldb::TypeSP |
199 | ParseStructureLikeDIE(const lldb_private::SymbolContext &sc, |
200 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
201 | ParsedDWARFTypeAttributes &attrs); |
202 | |
203 | clang::Decl * |
204 | GetClangDeclForDIE(const lldb_private::plugin::dwarf::DWARFDIE &die); |
205 | |
206 | clang::DeclContext * |
207 | GetClangDeclContextForDIE(const lldb_private::plugin::dwarf::DWARFDIE &die); |
208 | |
209 | clang::DeclContext *GetClangDeclContextContainingDIE( |
210 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
211 | lldb_private::plugin::dwarf::DWARFDIE *decl_ctx_die); |
212 | lldb_private::OptionalClangModuleID |
213 | GetOwningClangModule(const lldb_private::plugin::dwarf::DWARFDIE &die); |
214 | |
215 | bool CopyUniqueClassMethodTypes( |
216 | const lldb_private::plugin::dwarf::DWARFDIE &src_class_die, |
217 | const lldb_private::plugin::dwarf::DWARFDIE &dst_class_die, |
218 | lldb_private::Type *class_type, |
219 | std::vector<lldb_private::plugin::dwarf::DWARFDIE> &failures); |
220 | |
221 | clang::DeclContext *GetCachedClangDeclContextForDIE( |
222 | const lldb_private::plugin::dwarf::DWARFDIE &die); |
223 | |
224 | void LinkDeclContextToDIE(clang::DeclContext *decl_ctx, |
225 | const lldb_private::plugin::dwarf::DWARFDIE &die); |
226 | |
227 | void LinkDeclToDIE(clang::Decl *decl, |
228 | const lldb_private::plugin::dwarf::DWARFDIE &die); |
229 | |
230 | /// If \p type_sp is valid, calculate and set its symbol context scope, and |
231 | /// update the type list for its backing symbol file. |
232 | /// |
233 | /// Returns \p type_sp. |
234 | lldb::TypeSP UpdateSymbolContextScopeForType( |
235 | const lldb_private::SymbolContext &sc, |
236 | const lldb_private::plugin::dwarf::DWARFDIE &die, lldb::TypeSP type_sp); |
237 | |
238 | /// Follow Clang Module Skeleton CU references to find a type definition. |
239 | lldb::TypeSP |
240 | ParseTypeFromClangModule(const lldb_private::SymbolContext &sc, |
241 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
242 | lldb_private::Log *log); |
243 | |
244 | // Return true if this type is a declaration to a type in an external |
245 | // module. |
246 | lldb::ModuleSP |
247 | GetModuleForType(const lldb_private::plugin::dwarf::DWARFDIE &die); |
248 | |
249 | static bool classof(const DWARFASTParser *Parser) { |
250 | return Parser->GetKind() == Kind::DWARFASTParserClang; |
251 | } |
252 | |
253 | private: |
254 | struct FieldInfo { |
255 | uint64_t bit_size = 0; |
256 | uint64_t bit_offset = 0; |
257 | bool is_bitfield = false; |
258 | bool is_artificial = false; |
259 | |
260 | FieldInfo() = default; |
261 | |
262 | void SetIsBitfield(bool flag) { is_bitfield = flag; } |
263 | bool IsBitfield() { return is_bitfield; } |
264 | |
265 | void SetIsArtificial(bool flag) { is_artificial = flag; } |
266 | bool IsArtificial() const { return is_artificial; } |
267 | |
268 | bool NextBitfieldOffsetIsValid(const uint64_t next_bit_offset) const { |
269 | // Any subsequent bitfields must not overlap and must be at a higher |
270 | // bit offset than any previous bitfield + size. |
271 | return (bit_size + bit_offset) <= next_bit_offset; |
272 | } |
273 | }; |
274 | |
275 | /// Parsed form of all attributes that are relevant for parsing type members. |
276 | struct MemberAttributes { |
277 | explicit MemberAttributes( |
278 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
279 | const lldb_private::plugin::dwarf::DWARFDIE &parent_die, |
280 | lldb::ModuleSP module_sp); |
281 | const char *name = nullptr; |
282 | /// Indicates how many bits into the word (according to the host endianness) |
283 | /// the low-order bit of the field starts. Can be negative. |
284 | int64_t bit_offset = 0; |
285 | /// Indicates the size of the field in bits. |
286 | size_t bit_size = 0; |
287 | uint64_t data_bit_offset = UINT64_MAX; |
288 | lldb::AccessType accessibility = lldb::eAccessNone; |
289 | std::optional<uint64_t> byte_size; |
290 | std::optional<lldb_private::plugin::dwarf::DWARFFormValue> const_value_form; |
291 | lldb_private::plugin::dwarf::DWARFFormValue encoding_form; |
292 | /// Indicates the byte offset of the word from the base address of the |
293 | /// structure. |
294 | uint32_t member_byte_offset = UINT32_MAX; |
295 | bool is_artificial = false; |
296 | bool is_declaration = false; |
297 | }; |
298 | |
299 | /// Returns 'true' if we should create an unnamed bitfield |
300 | /// and add it to the parser's current AST. |
301 | /// |
302 | /// \param[in] last_field_info FieldInfo of the previous DW_TAG_member |
303 | /// we parsed. |
304 | /// \param[in] last_field_end Offset (in bits) where the last parsed field |
305 | /// ended. |
306 | /// \param[in] this_field_info FieldInfo of the current DW_TAG_member |
307 | /// being parsed. |
308 | /// \param[in] layout_info Layout information of all decls parsed by the |
309 | /// current parser. |
310 | bool ShouldCreateUnnamedBitfield( |
311 | FieldInfo const &last_field_info, uint64_t last_field_end, |
312 | FieldInfo const &this_field_info, |
313 | lldb_private::ClangASTImporter::LayoutInfo const &layout_info) const; |
314 | |
315 | /// Parses a DW_TAG_APPLE_property DIE and appends the parsed data to the |
316 | /// list of delayed Objective-C properties. |
317 | /// |
318 | /// Note: The delayed property needs to be finalized to actually create the |
319 | /// property declarations in the module AST. |
320 | /// |
321 | /// \param die The DW_TAG_APPLE_property DIE that will be parsed. |
322 | /// \param parent_die The parent DIE. |
323 | /// \param class_clang_type The Objective-C class that will contain the |
324 | /// created property. |
325 | /// \param delayed_properties The list of delayed properties that the result |
326 | /// will be appended to. |
327 | void |
328 | ParseObjCProperty(const lldb_private::plugin::dwarf::DWARFDIE &die, |
329 | const lldb_private::plugin::dwarf::DWARFDIE &parent_die, |
330 | const lldb_private::CompilerType &class_clang_type, |
331 | DelayedPropertyList &delayed_properties); |
332 | |
333 | void |
334 | ParseSingleMember(const lldb_private::plugin::dwarf::DWARFDIE &die, |
335 | const lldb_private::plugin::dwarf::DWARFDIE &parent_die, |
336 | const lldb_private::CompilerType &class_clang_type, |
337 | lldb::AccessType default_accessibility, |
338 | lldb_private::ClangASTImporter::LayoutInfo &layout_info, |
339 | FieldInfo &last_field_info); |
340 | |
341 | /// If the specified 'die' represents a static data member, creates |
342 | /// a 'clang::VarDecl' for it and attaches it to specified parent |
343 | /// 'class_clang_type'. |
344 | /// |
345 | /// \param[in] die The member declaration we want to create a |
346 | /// clang::VarDecl for. |
347 | /// |
348 | /// \param[in] attrs The parsed attributes for the specified 'die'. |
349 | /// |
350 | /// \param[in] class_clang_type The parent RecordType of the static |
351 | /// member this function will create. |
352 | void CreateStaticMemberVariable( |
353 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
354 | const MemberAttributes &attrs, |
355 | const lldb_private::CompilerType &class_clang_type); |
356 | |
357 | bool CompleteRecordType(const lldb_private::plugin::dwarf::DWARFDIE &die, |
358 | lldb_private::Type *type, |
359 | lldb_private::CompilerType &clang_type); |
360 | bool CompleteEnumType(const lldb_private::plugin::dwarf::DWARFDIE &die, |
361 | lldb_private::Type *type, |
362 | lldb_private::CompilerType &clang_type); |
363 | |
364 | lldb::TypeSP |
365 | ParseTypeModifier(const lldb_private::SymbolContext &sc, |
366 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
367 | ParsedDWARFTypeAttributes &attrs); |
368 | lldb::TypeSP ParseEnum(const lldb_private::SymbolContext &sc, |
369 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
370 | ParsedDWARFTypeAttributes &attrs); |
371 | lldb::TypeSP ParseSubroutine(const lldb_private::plugin::dwarf::DWARFDIE &die, |
372 | const ParsedDWARFTypeAttributes &attrs); |
373 | lldb::TypeSP ParseArrayType(const lldb_private::plugin::dwarf::DWARFDIE &die, |
374 | const ParsedDWARFTypeAttributes &attrs); |
375 | lldb::TypeSP |
376 | ParsePointerToMemberType(const lldb_private::plugin::dwarf::DWARFDIE &die, |
377 | const ParsedDWARFTypeAttributes &attrs); |
378 | |
379 | /// Parses a DW_TAG_inheritance DIE into a base/super class. |
380 | /// |
381 | /// \param die The DW_TAG_inheritance DIE to parse. |
382 | /// \param parent_die The parent DIE of the given DIE. |
383 | /// \param class_clang_type The C++/Objective-C class representing parent_die. |
384 | /// For an Objective-C class this method sets the super class on success. For |
385 | /// a C++ class this will *not* add the result as a base class. |
386 | /// \param default_accessibility The default accessibility that is given to |
387 | /// base classes if they don't have an explicit accessibility set. |
388 | /// \param module_sp The current Module. |
389 | /// \param base_classes The list of C++ base classes that will be appended |
390 | /// with the parsed base class on success. |
391 | /// \param layout_info The layout information that will be updated for C++ |
392 | /// base classes with the base offset. |
393 | void ParseInheritance( |
394 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
395 | const lldb_private::plugin::dwarf::DWARFDIE &parent_die, |
396 | const lldb_private::CompilerType class_clang_type, |
397 | const lldb::AccessType default_accessibility, |
398 | const lldb::ModuleSP &module_sp, |
399 | std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes, |
400 | lldb_private::ClangASTImporter::LayoutInfo &layout_info); |
401 | |
402 | /// Parses DW_TAG_variant_part DIE into a structure that encodes all variants |
403 | /// Note that this is currently being emitted by rustc and not Clang |
404 | /// \param die DW_TAG_variant_part DIE to parse |
405 | /// \param parent_die The parent DW_TAG_structure_type to parse |
406 | /// \param class_clang_type The Rust struct representing parent_die. |
407 | /// \param default_accesibility The default accessibility that is given to |
408 | /// base classes if they don't have an explicit accessibility set |
409 | /// \param layout_info The layout information that will be updated for |
410 | // base classes with the base offset |
411 | void |
412 | ParseRustVariantPart(lldb_private::plugin::dwarf::DWARFDIE &die, |
413 | const lldb_private::plugin::dwarf::DWARFDIE &parent_die, |
414 | lldb_private::CompilerType &class_clang_type, |
415 | const lldb::AccessType default_accesibility, |
416 | lldb_private::ClangASTImporter::LayoutInfo &layout_info); |
417 | }; |
418 | |
419 | /// Parsed form of all attributes that are relevant for type reconstruction. |
420 | /// Some attributes are relevant for all kinds of types (declaration), while |
421 | /// others are only meaningful to a specific type (is_virtual) |
422 | struct ParsedDWARFTypeAttributes { |
423 | explicit ParsedDWARFTypeAttributes( |
424 | const lldb_private::plugin::dwarf::DWARFDIE &die); |
425 | |
426 | lldb::AccessType accessibility = lldb::eAccessNone; |
427 | bool is_artificial = false; |
428 | bool is_complete_objc_class = false; |
429 | bool is_explicit = false; |
430 | bool is_forward_declaration = false; |
431 | bool is_inline = false; |
432 | bool is_scoped_enum = false; |
433 | bool is_vector = false; |
434 | bool is_virtual = false; |
435 | bool is_objc_direct_call = false; |
436 | bool exports_symbols = false; |
437 | clang::StorageClass storage = clang::SC_None; |
438 | const char *mangled_name = nullptr; |
439 | lldb_private::ConstString name; |
440 | lldb_private::Declaration decl; |
441 | lldb_private::plugin::dwarf::DWARFDIE object_pointer; |
442 | lldb_private::plugin::dwarf::DWARFFormValue abstract_origin; |
443 | lldb_private::plugin::dwarf::DWARFFormValue containing_type; |
444 | lldb_private::plugin::dwarf::DWARFFormValue signature; |
445 | lldb_private::plugin::dwarf::DWARFFormValue specification; |
446 | lldb_private::plugin::dwarf::DWARFFormValue type; |
447 | lldb::LanguageType class_language = lldb::eLanguageTypeUnknown; |
448 | std::optional<uint64_t> byte_size; |
449 | std::optional<uint64_t> alignment; |
450 | size_t calling_convention = llvm::dwarf::DW_CC_normal; |
451 | uint32_t bit_stride = 0; |
452 | uint32_t byte_stride = 0; |
453 | uint32_t encoding = 0; |
454 | clang::RefQualifierKind ref_qual = |
455 | clang::RQ_None; ///< Indicates ref-qualifier of |
456 | ///< C++ member function if present. |
457 | ///< Is RQ_None otherwise. |
458 | }; |
459 | |
460 | #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H |
461 | |