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 class DWARFDebugInfoEntry;
35 class SymbolFileDWARF;
36 
37 struct ParsedDWARFTypeAttributes;
38 
39 class DWARFASTParserClang : public DWARFASTParser {
40 public:
41   DWARFASTParserClang(lldb_private::TypeSystemClang &ast);
42 
43   ~DWARFASTParserClang() override;
44 
45   // DWARFASTParser interface.
46   lldb::TypeSP ParseTypeFromDWARF(const lldb_private::SymbolContext &sc,
47                                   const DWARFDIE &die,
48                                   bool *type_is_new_ptr) override;
49 
50   lldb_private::ConstString
51   ConstructDemangledNameFromDWARF(const DWARFDIE &die) override;
52 
53   lldb_private::Function *
54   ParseFunctionFromDWARF(lldb_private::CompileUnit &comp_unit,
55                          const DWARFDIE &die,
56                          const lldb_private::AddressRange &func_range) override;
57 
58   bool
59   CompleteTypeFromDWARF(const DWARFDIE &die, lldb_private::Type *type,
60                         lldb_private::CompilerType &compiler_type) override;
61 
62   lldb_private::CompilerDecl
63   GetDeclForUIDFromDWARF(const DWARFDIE &die) override;
64 
65   void EnsureAllDIEsInDeclContextHaveBeenParsed(
66       lldb_private::CompilerDeclContext decl_context) override;
67 
68   lldb_private::CompilerDeclContext
69   GetDeclContextForUIDFromDWARF(const DWARFDIE &die) override;
70 
71   lldb_private::CompilerDeclContext
72   GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) override;
73 
74   lldb_private::ClangASTImporter &GetClangASTImporter();
75 
76   /// Extracts an value for a given Clang integer type from a DWARFFormValue.
77   ///
78   /// \param int_type The Clang type that defines the bit size and signedness
79   ///                 of the integer that should be extracted. Has to be either
80   ///                 an integer type or an enum type. For enum types the
81   ///                 underlying integer type will be considered as the
82   ///                 expected integer type that should be extracted.
83   /// \param form_value The DWARFFormValue that contains the integer value.
84   /// \return An APInt containing the same integer value as the given
85   ///         DWARFFormValue with the bit width of the given integer type.
86   ///         Returns an error if the value in the DWARFFormValue does not fit
87   ///         into the given integer type or the integer type isn't supported.
88   llvm::Expected<llvm::APInt>
89   ExtractIntFromFormValue(const lldb_private::CompilerType &int_type,
90                           const DWARFFormValue &form_value) const;
91 
92   /// Returns the template parameters of a class DWARFDIE as a string.
93   ///
94   /// This is mostly useful for -gsimple-template-names which omits template
95   /// parameters from the DIE name and instead always adds template parameter
96   /// children DIEs.
97   ///
98   /// \param die The struct/class DWARFDIE containing template parameters.
99   /// \return A string, including surrounding '<>', of the template parameters.
100   /// If the DIE's name already has '<>', returns an empty ConstString because
101   /// it's assumed that the caller is using the DIE name anyway.
102   lldb_private::ConstString
103   GetDIEClassTemplateParams(const DWARFDIE &die) override;
104 
105 protected:
106   /// Protected typedefs and members.
107   /// @{
108   class DelayedAddObjCClassProperty;
109   typedef std::vector<DelayedAddObjCClassProperty> DelayedPropertyList;
110 
111   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, clang::DeclContext *>
112       DIEToDeclContextMap;
113   typedef std::multimap<const clang::DeclContext *, const DWARFDIE>
114       DeclContextToDIEMap;
115   typedef llvm::DenseMap<const DWARFDebugInfoEntry *,
116                          lldb_private::OptionalClangModuleID>
117       DIEToModuleMap;
118   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, clang::Decl *>
119       DIEToDeclMap;
120 
121   lldb_private::TypeSystemClang &m_ast;
122   DIEToDeclMap m_die_to_decl;
123   DIEToDeclContextMap m_die_to_decl_ctx;
124   DeclContextToDIEMap m_decl_ctx_to_die;
125   DIEToModuleMap m_die_to_module;
126   std::unique_ptr<lldb_private::ClangASTImporter> m_clang_ast_importer_up;
127   /// @}
128 
129   clang::DeclContext *GetDeclContextForBlock(const DWARFDIE &die);
130 
131   clang::BlockDecl *ResolveBlockDIE(const DWARFDIE &die);
132 
133   clang::NamespaceDecl *ResolveNamespaceDIE(const DWARFDIE &die);
134 
135   bool ParseTemplateDIE(const DWARFDIE &die,
136                         lldb_private::TypeSystemClang::TemplateParameterInfos
137                             &template_param_infos);
138 
139   bool ParseTemplateParameterInfos(
140       const DWARFDIE &parent_die,
141       lldb_private::TypeSystemClang::TemplateParameterInfos
142           &template_param_infos);
143 
144   std::string GetCPlusPlusQualifiedName(const DWARFDIE &die);
145 
146   bool ParseChildMembers(
147       const DWARFDIE &die, lldb_private::CompilerType &class_compiler_type,
148       std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
149       std::vector<DWARFDIE> &member_function_dies,
150       DelayedPropertyList &delayed_properties,
151       const lldb::AccessType default_accessibility,
152       lldb_private::ClangASTImporter::LayoutInfo &layout_info);
153 
154   size_t
155   ParseChildParameters(clang::DeclContext *containing_decl_ctx,
156                        const DWARFDIE &parent_die, bool skip_artificial,
157                        bool &is_static, bool &is_variadic,
158                        bool &has_template_params,
159                        std::vector<lldb_private::CompilerType> &function_args,
160                        std::vector<clang::ParmVarDecl *> &function_param_decls,
161                        unsigned &type_quals);
162 
163   size_t ParseChildEnumerators(lldb_private::CompilerType &compiler_type,
164                                bool is_signed, uint32_t enumerator_byte_size,
165                                const DWARFDIE &parent_die);
166 
167   /// Parse a structure, class, or union type DIE.
168   lldb::TypeSP ParseStructureLikeDIE(const lldb_private::SymbolContext &sc,
169                                      const DWARFDIE &die,
170                                      ParsedDWARFTypeAttributes &attrs);
171 
172   lldb_private::Type *GetTypeForDIE(const DWARFDIE &die);
173 
174   clang::Decl *GetClangDeclForDIE(const DWARFDIE &die);
175 
176   clang::DeclContext *GetClangDeclContextForDIE(const DWARFDIE &die);
177 
178   clang::DeclContext *GetClangDeclContextContainingDIE(const DWARFDIE &die,
179                                                        DWARFDIE *decl_ctx_die);
180   lldb_private::OptionalClangModuleID GetOwningClangModule(const DWARFDIE &die);
181 
182   bool CopyUniqueClassMethodTypes(const DWARFDIE &src_class_die,
183                                   const DWARFDIE &dst_class_die,
184                                   lldb_private::Type *class_type,
185                                   std::vector<DWARFDIE> &failures);
186 
187   clang::DeclContext *GetCachedClangDeclContextForDIE(const DWARFDIE &die);
188 
189   void LinkDeclContextToDIE(clang::DeclContext *decl_ctx, const DWARFDIE &die);
190 
191   void LinkDeclToDIE(clang::Decl *decl, const DWARFDIE &die);
192 
193   /// If \p type_sp is valid, calculate and set its symbol context scope, and
194   /// update the type list for its backing symbol file.
195   ///
196   /// Returns \p type_sp.
197   lldb::TypeSP
198   UpdateSymbolContextScopeForType(const lldb_private::SymbolContext &sc,
199                                   const DWARFDIE &die, lldb::TypeSP type_sp);
200 
201   /// Follow Clang Module Skeleton CU references to find a type definition.
202   lldb::TypeSP ParseTypeFromClangModule(const lldb_private::SymbolContext &sc,
203                                         const DWARFDIE &die,
204                                         lldb_private::Log *log);
205 
206   // Return true if this type is a declaration to a type in an external
207   // module.
208   lldb::ModuleSP GetModuleForType(const DWARFDIE &die);
209 
210 private:
211   struct FieldInfo {
212     uint64_t bit_size = 0;
213     uint64_t bit_offset = 0;
214     bool is_bitfield = false;
215 
216     FieldInfo() = default;
217 
SetIsBitfieldFieldInfo218     void SetIsBitfield(bool flag) { is_bitfield = flag; }
IsBitfieldFieldInfo219     bool IsBitfield() { return is_bitfield; }
220 
NextBitfieldOffsetIsValidFieldInfo221     bool NextBitfieldOffsetIsValid(const uint64_t next_bit_offset) const {
222       // Any subsequent bitfields must not overlap and must be at a higher
223       // bit offset than any previous bitfield + size.
224       return (bit_size + bit_offset) <= next_bit_offset;
225     }
226   };
227 
228   /// Parses a DW_TAG_APPLE_property DIE and appends the parsed data to the
229   /// list of delayed Objective-C properties.
230   ///
231   /// Note: The delayed property needs to be finalized to actually create the
232   /// property declarations in the module AST.
233   ///
234   /// \param die The DW_TAG_APPLE_property DIE that will be parsed.
235   /// \param parent_die The parent DIE.
236   /// \param class_clang_type The Objective-C class that will contain the
237   /// created property.
238   /// \param delayed_properties The list of delayed properties that the result
239   /// will be appended to.
240   void ParseObjCProperty(const DWARFDIE &die, const DWARFDIE &parent_die,
241                          const lldb_private::CompilerType &class_clang_type,
242                          DelayedPropertyList &delayed_properties);
243 
244   void
245   ParseSingleMember(const DWARFDIE &die, const DWARFDIE &parent_die,
246                     const lldb_private::CompilerType &class_clang_type,
247                     lldb::AccessType default_accessibility,
248                     lldb_private::ClangASTImporter::LayoutInfo &layout_info,
249                     FieldInfo &last_field_info);
250 
251   bool CompleteRecordType(const DWARFDIE &die, lldb_private::Type *type,
252                           lldb_private::CompilerType &clang_type);
253   bool CompleteEnumType(const DWARFDIE &die, lldb_private::Type *type,
254                         lldb_private::CompilerType &clang_type);
255 
256   lldb::TypeSP ParseTypeModifier(const lldb_private::SymbolContext &sc,
257                                  const DWARFDIE &die,
258                                  ParsedDWARFTypeAttributes &attrs);
259   lldb::TypeSP ParseEnum(const lldb_private::SymbolContext &sc,
260                          const DWARFDIE &die, ParsedDWARFTypeAttributes &attrs);
261   lldb::TypeSP ParseSubroutine(const DWARFDIE &die,
262                                ParsedDWARFTypeAttributes &attrs);
263   lldb::TypeSP ParseArrayType(const DWARFDIE &die,
264                               const ParsedDWARFTypeAttributes &attrs);
265   lldb::TypeSP ParsePointerToMemberType(const DWARFDIE &die,
266                                         const ParsedDWARFTypeAttributes &attrs);
267 
268   /// Parses a DW_TAG_inheritance DIE into a base/super class.
269   ///
270   /// \param die The DW_TAG_inheritance DIE to parse.
271   /// \param parent_die The parent DIE of the given DIE.
272   /// \param class_clang_type The C++/Objective-C class representing parent_die.
273   /// For an Objective-C class this method sets the super class on success. For
274   /// a C++ class this will *not* add the result as a base class.
275   /// \param default_accessibility The default accessibility that is given to
276   /// base classes if they don't have an explicit accessibility set.
277   /// \param module_sp The current Module.
278   /// \param base_classes The list of C++ base classes that will be appended
279   /// with the parsed base class on success.
280   /// \param layout_info The layout information that will be updated for C++
281   /// base classes with the base offset.
282   void ParseInheritance(
283       const DWARFDIE &die, const DWARFDIE &parent_die,
284       const lldb_private::CompilerType class_clang_type,
285       const lldb::AccessType default_accessibility,
286       const lldb::ModuleSP &module_sp,
287       std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
288       lldb_private::ClangASTImporter::LayoutInfo &layout_info);
289 };
290 
291 /// Parsed form of all attributes that are relevant for type reconstruction.
292 /// Some attributes are relevant for all kinds of types (declaration), while
293 /// others are only meaningful to a specific type (is_virtual)
294 struct ParsedDWARFTypeAttributes {
295   explicit ParsedDWARFTypeAttributes(const DWARFDIE &die);
296 
297   lldb::AccessType accessibility = lldb::eAccessNone;
298   bool is_artificial = false;
299   bool is_complete_objc_class = false;
300   bool is_explicit = false;
301   bool is_forward_declaration = false;
302   bool is_inline = false;
303   bool is_scoped_enum = false;
304   bool is_vector = false;
305   bool is_virtual = false;
306   bool is_objc_direct_call = false;
307   bool exports_symbols = false;
308   clang::StorageClass storage = clang::SC_None;
309   const char *mangled_name = nullptr;
310   lldb_private::ConstString name;
311   lldb_private::Declaration decl;
312   DWARFDIE object_pointer;
313   DWARFFormValue abstract_origin;
314   DWARFFormValue containing_type;
315   DWARFFormValue signature;
316   DWARFFormValue specification;
317   DWARFFormValue type;
318   lldb::LanguageType class_language = lldb::eLanguageTypeUnknown;
319   std::optional<uint64_t> byte_size;
320   size_t calling_convention = llvm::dwarf::DW_CC_normal;
321   uint32_t bit_stride = 0;
322   uint32_t byte_stride = 0;
323   uint32_t encoding = 0;
324   clang::RefQualifierKind ref_qual =
325       clang::RQ_None; ///< Indicates ref-qualifier of
326                       ///< C++ member function if present.
327                       ///< Is RQ_None otherwise.
328 };
329 
330 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H
331