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 "llvm/ADT/DenseMap.h"
14 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/ADT/SmallVector.h"
16 
17 #include "DWARFASTParser.h"
18 #include "DWARFDIE.h"
19 #include "DWARFDefines.h"
20 #include "DWARFFormValue.h"
21 #include "LogChannelDWARF.h"
22 #include "lldb/Core/PluginInterface.h"
23 
24 #include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
25 #include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
26 
27 #include <vector>
28 
29 namespace lldb_private {
30 class CompileUnit;
31 }
32 class DWARFDebugInfoEntry;
33 class SymbolFileDWARF;
34 
35 struct ParsedDWARFTypeAttributes;
36 
37 class DWARFASTParserClang : public DWARFASTParser {
38 public:
39   DWARFASTParserClang(lldb_private::TypeSystemClang &ast);
40 
41   ~DWARFASTParserClang() override;
42 
43   // DWARFASTParser interface.
44   lldb::TypeSP ParseTypeFromDWARF(const lldb_private::SymbolContext &sc,
45                                   const DWARFDIE &die,
46                                   bool *type_is_new_ptr) override;
47 
48   lldb_private::Function *
49   ParseFunctionFromDWARF(lldb_private::CompileUnit &comp_unit,
50                          const DWARFDIE &die,
51                          const lldb_private::AddressRange &func_range) override;
52 
53   bool
54   CompleteTypeFromDWARF(const DWARFDIE &die, lldb_private::Type *type,
55                         lldb_private::CompilerType &compiler_type) override;
56 
57   lldb_private::CompilerDecl
58   GetDeclForUIDFromDWARF(const DWARFDIE &die) override;
59 
60   void EnsureAllDIEsInDeclContextHaveBeenParsed(
61       lldb_private::CompilerDeclContext decl_context) override;
62 
63   lldb_private::CompilerDeclContext
64   GetDeclContextForUIDFromDWARF(const DWARFDIE &die) override;
65 
66   lldb_private::CompilerDeclContext
67   GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) override;
68 
69   lldb_private::ClangASTImporter &GetClangASTImporter();
70 
71 protected:
72   /// Protected typedefs and members.
73   /// @{
74   class DelayedAddObjCClassProperty;
75   typedef std::vector<DelayedAddObjCClassProperty> DelayedPropertyList;
76 
77   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, clang::DeclContext *>
78       DIEToDeclContextMap;
79   typedef std::multimap<const clang::DeclContext *, const DWARFDIE>
80       DeclContextToDIEMap;
81   typedef llvm::DenseMap<const DWARFDebugInfoEntry *,
82                          lldb_private::OptionalClangModuleID>
83       DIEToModuleMap;
84   typedef llvm::DenseMap<const DWARFDebugInfoEntry *, clang::Decl *>
85       DIEToDeclMap;
86 
87   lldb_private::TypeSystemClang &m_ast;
88   DIEToDeclMap m_die_to_decl;
89   DIEToDeclContextMap m_die_to_decl_ctx;
90   DeclContextToDIEMap m_decl_ctx_to_die;
91   DIEToModuleMap m_die_to_module;
92   std::unique_ptr<lldb_private::ClangASTImporter> m_clang_ast_importer_up;
93   /// @}
94 
95   clang::DeclContext *GetDeclContextForBlock(const DWARFDIE &die);
96 
97   clang::BlockDecl *ResolveBlockDIE(const DWARFDIE &die);
98 
99   clang::NamespaceDecl *ResolveNamespaceDIE(const DWARFDIE &die);
100 
101   bool ParseTemplateDIE(const DWARFDIE &die,
102                         lldb_private::TypeSystemClang::TemplateParameterInfos
103                             &template_param_infos);
104   bool ParseTemplateParameterInfos(
105       const DWARFDIE &parent_die,
106       lldb_private::TypeSystemClang::TemplateParameterInfos
107           &template_param_infos);
108 
109   bool ParseChildMembers(
110       const DWARFDIE &die, lldb_private::CompilerType &class_compiler_type,
111       std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
112       std::vector<DWARFDIE> &member_function_dies,
113       DelayedPropertyList &delayed_properties,
114       const lldb::AccessType default_accessibility,
115       lldb_private::ClangASTImporter::LayoutInfo &layout_info);
116 
117   size_t
118   ParseChildParameters(clang::DeclContext *containing_decl_ctx,
119                        const DWARFDIE &parent_die, bool skip_artificial,
120                        bool &is_static, bool &is_variadic,
121                        bool &has_template_params,
122                        std::vector<lldb_private::CompilerType> &function_args,
123                        std::vector<clang::ParmVarDecl *> &function_param_decls,
124                        unsigned &type_quals);
125 
126   size_t ParseChildEnumerators(lldb_private::CompilerType &compiler_type,
127                                bool is_signed, uint32_t enumerator_byte_size,
128                                const DWARFDIE &parent_die);
129 
130   /// Parse a structure, class, or union type DIE.
131   lldb::TypeSP ParseStructureLikeDIE(const lldb_private::SymbolContext &sc,
132                                      const DWARFDIE &die,
133                                      ParsedDWARFTypeAttributes &attrs);
134 
135   lldb_private::Type *GetTypeForDIE(const DWARFDIE &die);
136 
137   clang::Decl *GetClangDeclForDIE(const DWARFDIE &die);
138 
139   clang::DeclContext *GetClangDeclContextForDIE(const DWARFDIE &die);
140 
141   clang::DeclContext *GetClangDeclContextContainingDIE(const DWARFDIE &die,
142                                                        DWARFDIE *decl_ctx_die);
143   lldb_private::OptionalClangModuleID GetOwningClangModule(const DWARFDIE &die);
144 
145   bool CopyUniqueClassMethodTypes(const DWARFDIE &src_class_die,
146                                   const DWARFDIE &dst_class_die,
147                                   lldb_private::Type *class_type,
148                                   std::vector<DWARFDIE> &failures);
149 
150   clang::DeclContext *GetCachedClangDeclContextForDIE(const DWARFDIE &die);
151 
152   void LinkDeclContextToDIE(clang::DeclContext *decl_ctx, const DWARFDIE &die);
153 
154   void LinkDeclToDIE(clang::Decl *decl, const DWARFDIE &die);
155 
156   /// If \p type_sp is valid, calculate and set its symbol context scope, and
157   /// update the type list for its backing symbol file.
158   ///
159   /// Returns \p type_sp.
160   lldb::TypeSP
161   UpdateSymbolContextScopeForType(const lldb_private::SymbolContext &sc,
162                                   const DWARFDIE &die, lldb::TypeSP type_sp);
163 
164   /// Follow Clang Module Skeleton CU references to find a type definition.
165   lldb::TypeSP ParseTypeFromClangModule(const lldb_private::SymbolContext &sc,
166                                         const DWARFDIE &die,
167                                         lldb_private::Log *log);
168 
169   // Return true if this type is a declaration to a type in an external
170   // module.
171   lldb::ModuleSP GetModuleForType(const DWARFDIE &die);
172 
173 private:
174   struct FieldInfo {
175     uint64_t bit_size = 0;
176     uint64_t bit_offset = 0;
177     bool is_bitfield = false;
178 
179     FieldInfo() = default;
180 
181     void SetIsBitfield(bool flag) { is_bitfield = flag; }
182     bool IsBitfield() { return is_bitfield; }
183 
184     bool NextBitfieldOffsetIsValid(const uint64_t next_bit_offset) const {
185       // Any subsequent bitfields must not overlap and must be at a higher
186       // bit offset than any previous bitfield + size.
187       return (bit_size + bit_offset) <= next_bit_offset;
188     }
189   };
190 
191   /// Parses a DW_TAG_APPLE_property DIE and appends the parsed data to the
192   /// list of delayed Objective-C properties.
193   ///
194   /// Note: The delayed property needs to be finalized to actually create the
195   /// property declarations in the module AST.
196   ///
197   /// \param die The DW_TAG_APPLE_property DIE that will be parsed.
198   /// \param parent_die The parent DIE.
199   /// \param class_clang_type The Objective-C class that will contain the
200   /// created property.
201   /// \param delayed_properties The list of delayed properties that the result
202   /// will be appended to.
203   void ParseObjCProperty(const DWARFDIE &die, const DWARFDIE &parent_die,
204                          const lldb_private::CompilerType &class_clang_type,
205                          DelayedPropertyList &delayed_properties);
206 
207   void
208   ParseSingleMember(const DWARFDIE &die, const DWARFDIE &parent_die,
209                     const lldb_private::CompilerType &class_clang_type,
210                     lldb::AccessType default_accessibility,
211                     lldb_private::ClangASTImporter::LayoutInfo &layout_info,
212                     FieldInfo &last_field_info);
213 
214   bool CompleteRecordType(const DWARFDIE &die, lldb_private::Type *type,
215                           lldb_private::CompilerType &clang_type);
216   bool CompleteEnumType(const DWARFDIE &die, lldb_private::Type *type,
217                         lldb_private::CompilerType &clang_type);
218 
219   lldb::TypeSP ParseTypeModifier(const lldb_private::SymbolContext &sc,
220                                  const DWARFDIE &die,
221                                  ParsedDWARFTypeAttributes &attrs);
222   lldb::TypeSP ParseEnum(const lldb_private::SymbolContext &sc,
223                          const DWARFDIE &die, ParsedDWARFTypeAttributes &attrs);
224   lldb::TypeSP ParseSubroutine(const DWARFDIE &die,
225                                ParsedDWARFTypeAttributes &attrs);
226   // FIXME: attrs should be passed as a const reference.
227   lldb::TypeSP ParseArrayType(const DWARFDIE &die,
228                               ParsedDWARFTypeAttributes &attrs);
229   lldb::TypeSP ParsePointerToMemberType(const DWARFDIE &die,
230                                         const ParsedDWARFTypeAttributes &attrs);
231 
232   /// Parses a DW_TAG_inheritance DIE into a base/super class.
233   ///
234   /// \param die The DW_TAG_inheritance DIE to parse.
235   /// \param parent_die The parent DIE of the given DIE.
236   /// \param class_clang_type The C++/Objective-C class representing parent_die.
237   /// For an Objective-C class this method sets the super class on success. For
238   /// a C++ class this will *not* add the result as a base class.
239   /// \param default_accessibility The default accessibility that is given to
240   /// base classes if they don't have an explicit accessibility set.
241   /// \param module_sp The current Module.
242   /// \param base_classes The list of C++ base classes that will be appended
243   /// with the parsed base class on success.
244   /// \param layout_info The layout information that will be updated for C++
245   /// base classes with the base offset.
246   void ParseInheritance(
247       const DWARFDIE &die, const DWARFDIE &parent_die,
248       const lldb_private::CompilerType class_clang_type,
249       const lldb::AccessType default_accessibility,
250       const lldb::ModuleSP &module_sp,
251       std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
252       lldb_private::ClangASTImporter::LayoutInfo &layout_info);
253 };
254 
255 /// Parsed form of all attributes that are relevant for type reconstruction.
256 /// Some attributes are relevant for all kinds of types (declaration), while
257 /// others are only meaningful to a specific type (is_virtual)
258 struct ParsedDWARFTypeAttributes {
259   explicit ParsedDWARFTypeAttributes(const DWARFDIE &die);
260 
261   lldb::AccessType accessibility = lldb::eAccessNone;
262   bool is_artificial = false;
263   bool is_complete_objc_class = false;
264   bool is_explicit = false;
265   bool is_forward_declaration = false;
266   bool is_inline = false;
267   bool is_scoped_enum = false;
268   bool is_vector = false;
269   bool is_virtual = false;
270   bool is_objc_direct_call = false;
271   bool exports_symbols = false;
272   clang::StorageClass storage = clang::SC_None;
273   const char *mangled_name = nullptr;
274   lldb_private::ConstString name;
275   lldb_private::Declaration decl;
276   DWARFDIE object_pointer;
277   DWARFFormValue abstract_origin;
278   DWARFFormValue containing_type;
279   DWARFFormValue signature;
280   DWARFFormValue specification;
281   DWARFFormValue type;
282   lldb::LanguageType class_language = lldb::eLanguageTypeUnknown;
283   llvm::Optional<uint64_t> byte_size;
284   size_t calling_convention = llvm::dwarf::DW_CC_normal;
285   uint32_t bit_stride = 0;
286   uint32_t byte_stride = 0;
287   uint32_t encoding = 0;
288 };
289 
290 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H
291