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) override;
51 
52   bool
53   CompleteTypeFromDWARF(const DWARFDIE &die, lldb_private::Type *type,
54                         lldb_private::CompilerType &compiler_type) override;
55 
56   lldb_private::CompilerDecl
57   GetDeclForUIDFromDWARF(const DWARFDIE &die) override;
58 
59   void EnsureAllDIEsInDeclContextHaveBeenParsed(
60       lldb_private::CompilerDeclContext decl_context) override;
61 
62   lldb_private::CompilerDeclContext
63   GetDeclContextForUIDFromDWARF(const DWARFDIE &die) override;
64 
65   lldb_private::CompilerDeclContext
66   GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) override;
67 
68   lldb_private::ClangASTImporter &GetClangASTImporter();
69 
70 protected:
71   /// Protected typedefs and members.
72   /// @{
73   class DelayedAddObjCClassProperty;
74   typedef std::vector<DelayedAddObjCClassProperty> DelayedPropertyList;
75 
76   typedef llvm::SmallPtrSet<const DWARFDebugInfoEntry *, 4> DIEPointerSet;
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   typedef llvm::DenseMap<const clang::Decl *, DIEPointerSet> DeclToDIEMap;
87 
88   lldb_private::TypeSystemClang &m_ast;
89   DIEToDeclMap m_die_to_decl;
90   DeclToDIEMap m_decl_to_die;
91   DIEToDeclContextMap m_die_to_decl_ctx;
92   DeclContextToDIEMap m_decl_ctx_to_die;
93   DIEToModuleMap m_die_to_module;
94   std::unique_ptr<lldb_private::ClangASTImporter> m_clang_ast_importer_up;
95   /// @}
96 
97   clang::DeclContext *GetDeclContextForBlock(const DWARFDIE &die);
98 
99   clang::BlockDecl *ResolveBlockDIE(const DWARFDIE &die);
100 
101   clang::NamespaceDecl *ResolveNamespaceDIE(const DWARFDIE &die);
102 
103   bool ParseTemplateDIE(const DWARFDIE &die,
104                         lldb_private::TypeSystemClang::TemplateParameterInfos
105                             &template_param_infos);
106   bool ParseTemplateParameterInfos(
107       const DWARFDIE &parent_die,
108       lldb_private::TypeSystemClang::TemplateParameterInfos
109           &template_param_infos);
110 
111   bool ParseChildMembers(
112       const DWARFDIE &die, lldb_private::CompilerType &class_compiler_type,
113       std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes,
114       std::vector<int> &member_accessibilities,
115       std::vector<DWARFDIE> &member_function_dies,
116       DelayedPropertyList &delayed_properties,
117       lldb::AccessType &default_accessibility, bool &is_a_class,
118       lldb_private::ClangASTImporter::LayoutInfo &layout_info);
119 
120   size_t
121   ParseChildParameters(clang::DeclContext *containing_decl_ctx,
122                        const DWARFDIE &parent_die, bool skip_artificial,
123                        bool &is_static, bool &is_variadic,
124                        bool &has_template_params,
125                        std::vector<lldb_private::CompilerType> &function_args,
126                        std::vector<clang::ParmVarDecl *> &function_param_decls,
127                        unsigned &type_quals);
128 
129   size_t ParseChildEnumerators(lldb_private::CompilerType &compiler_type,
130                                bool is_signed, uint32_t enumerator_byte_size,
131                                const DWARFDIE &parent_die);
132 
133   /// Parse a structure, class, or union type DIE.
134   lldb::TypeSP ParseStructureLikeDIE(const lldb_private::SymbolContext &sc,
135                                      const DWARFDIE &die,
136                                      ParsedDWARFTypeAttributes &attrs);
137 
138   lldb_private::Type *GetTypeForDIE(const DWARFDIE &die);
139 
140   clang::Decl *GetClangDeclForDIE(const DWARFDIE &die);
141 
142   clang::DeclContext *GetClangDeclContextForDIE(const DWARFDIE &die);
143 
144   clang::DeclContext *GetClangDeclContextContainingDIE(const DWARFDIE &die,
145                                                        DWARFDIE *decl_ctx_die);
146   lldb_private::OptionalClangModuleID GetOwningClangModule(const DWARFDIE &die);
147 
148   bool CopyUniqueClassMethodTypes(const DWARFDIE &src_class_die,
149                                   const DWARFDIE &dst_class_die,
150                                   lldb_private::Type *class_type,
151                                   std::vector<DWARFDIE> &failures);
152 
153   clang::DeclContext *GetCachedClangDeclContextForDIE(const DWARFDIE &die);
154 
155   void LinkDeclContextToDIE(clang::DeclContext *decl_ctx, const DWARFDIE &die);
156 
157   void LinkDeclToDIE(clang::Decl *decl, const DWARFDIE &die);
158 
159   /// If \p type_sp is valid, calculate and set its symbol context scope, and
160   /// update the type list for its backing symbol file.
161   ///
162   /// Returns \p type_sp.
163   lldb::TypeSP
164   UpdateSymbolContextScopeForType(const lldb_private::SymbolContext &sc,
165                                   const DWARFDIE &die, lldb::TypeSP type_sp);
166 
167   /// Follow Clang Module Skeleton CU references to find a type definition.
168   lldb::TypeSP ParseTypeFromClangModule(const lldb_private::SymbolContext &sc,
169                                         const DWARFDIE &die,
170                                         lldb_private::Log *log);
171 
172   // Return true if this type is a declaration to a type in an external
173   // module.
174   lldb::ModuleSP GetModuleForType(const DWARFDIE &die);
175 
176 private:
177   struct FieldInfo {
178     uint64_t bit_size = 0;
179     uint64_t bit_offset = 0;
180     bool is_bitfield = false;
181 
182     FieldInfo() = default;
183 
SetIsBitfieldFieldInfo184     void SetIsBitfield(bool flag) { is_bitfield = flag; }
IsBitfieldFieldInfo185     bool IsBitfield() { return is_bitfield; }
186 
NextBitfieldOffsetIsValidFieldInfo187     bool NextBitfieldOffsetIsValid(const uint64_t next_bit_offset) const {
188       // Any subsequent bitfields must not overlap and must be at a higher
189       // bit offset than any previous bitfield + size.
190       return (bit_size + bit_offset) <= next_bit_offset;
191     }
192   };
193 
194   void
195   ParseSingleMember(const DWARFDIE &die, const DWARFDIE &parent_die,
196                     const lldb_private::CompilerType &class_clang_type,
197                     std::vector<int> &member_accessibilities,
198                     lldb::AccessType default_accessibility,
199                     DelayedPropertyList &delayed_properties,
200                     lldb_private::ClangASTImporter::LayoutInfo &layout_info,
201                     FieldInfo &last_field_info);
202 
203   bool CompleteRecordType(const DWARFDIE &die, lldb_private::Type *type,
204                           lldb_private::CompilerType &clang_type);
205   bool CompleteEnumType(const DWARFDIE &die, lldb_private::Type *type,
206                         lldb_private::CompilerType &clang_type);
207 
208   lldb::TypeSP ParseTypeModifier(const lldb_private::SymbolContext &sc,
209                                  const DWARFDIE &die,
210                                  ParsedDWARFTypeAttributes &attrs);
211   lldb::TypeSP ParseEnum(const lldb_private::SymbolContext &sc,
212                          const DWARFDIE &die, ParsedDWARFTypeAttributes &attrs);
213   lldb::TypeSP ParseSubroutine(const DWARFDIE &die,
214                                ParsedDWARFTypeAttributes &attrs);
215   // FIXME: attrs should be passed as a const reference.
216   lldb::TypeSP ParseArrayType(const DWARFDIE &die,
217                               ParsedDWARFTypeAttributes &attrs);
218   lldb::TypeSP ParsePointerToMemberType(const DWARFDIE &die,
219                                         const ParsedDWARFTypeAttributes &attrs);
220 };
221 
222 /// Parsed form of all attributes that are relevant for type reconstruction.
223 /// Some attributes are relevant for all kinds of types (declaration), while
224 /// others are only meaningful to a specific type (is_virtual)
225 struct ParsedDWARFTypeAttributes {
226   explicit ParsedDWARFTypeAttributes(const DWARFDIE &die);
227 
228   lldb::AccessType accessibility = lldb::eAccessNone;
229   bool is_artificial = false;
230   bool is_complete_objc_class = false;
231   bool is_explicit = false;
232   bool is_forward_declaration = false;
233   bool is_inline = false;
234   bool is_scoped_enum = false;
235   bool is_vector = false;
236   bool is_virtual = false;
237   bool is_objc_direct_call = false;
238   bool exports_symbols = false;
239   clang::StorageClass storage = clang::SC_None;
240   const char *mangled_name = nullptr;
241   lldb_private::ConstString name;
242   lldb_private::Declaration decl;
243   DWARFDIE object_pointer;
244   DWARFFormValue abstract_origin;
245   DWARFFormValue containing_type;
246   DWARFFormValue signature;
247   DWARFFormValue specification;
248   DWARFFormValue type;
249   lldb::LanguageType class_language = lldb::eLanguageTypeUnknown;
250   llvm::Optional<uint64_t> byte_size;
251   size_t calling_convention = llvm::dwarf::DW_CC_normal;
252   uint32_t bit_stride = 0;
253   uint32_t byte_stride = 0;
254   uint32_t encoding = 0;
255 };
256 
257 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H
258