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 /// Returns the namespace decl that a DW_TAG_imported_declaration imports. 136 /// 137 /// \param[in] die The import declaration to resolve. If the DIE is not a 138 /// DW_TAG_imported_declaration the behaviour is undefined. 139 /// 140 /// \returns The decl corresponding to the namespace that the specified 141 /// 'die' imports. If the imported entity is not a namespace 142 /// or another import declaration, returns nullptr. If an error 143 /// occurs, returns nullptr. 144 clang::NamespaceDecl *ResolveImportedDeclarationDIE(const DWARFDIE &die); 145 146 bool ParseTemplateDIE(const DWARFDIE &die, 147 lldb_private::TypeSystemClang::TemplateParameterInfos 148 &template_param_infos); 149 150 bool ParseTemplateParameterInfos( 151 const DWARFDIE &parent_die, 152 lldb_private::TypeSystemClang::TemplateParameterInfos 153 &template_param_infos); 154 155 std::string GetCPlusPlusQualifiedName(const DWARFDIE &die); 156 157 bool ParseChildMembers( 158 const DWARFDIE &die, lldb_private::CompilerType &class_compiler_type, 159 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes, 160 std::vector<DWARFDIE> &member_function_dies, 161 DelayedPropertyList &delayed_properties, 162 const lldb::AccessType default_accessibility, 163 lldb_private::ClangASTImporter::LayoutInfo &layout_info); 164 165 size_t 166 ParseChildParameters(clang::DeclContext *containing_decl_ctx, 167 const DWARFDIE &parent_die, bool skip_artificial, 168 bool &is_static, bool &is_variadic, 169 bool &has_template_params, 170 std::vector<lldb_private::CompilerType> &function_args, 171 std::vector<clang::ParmVarDecl *> &function_param_decls, 172 unsigned &type_quals); 173 174 size_t ParseChildEnumerators(lldb_private::CompilerType &compiler_type, 175 bool is_signed, uint32_t enumerator_byte_size, 176 const DWARFDIE &parent_die); 177 178 /// Parse a structure, class, or union type DIE. 179 lldb::TypeSP ParseStructureLikeDIE(const lldb_private::SymbolContext &sc, 180 const DWARFDIE &die, 181 ParsedDWARFTypeAttributes &attrs); 182 183 lldb_private::Type *GetTypeForDIE(const DWARFDIE &die); 184 185 clang::Decl *GetClangDeclForDIE(const DWARFDIE &die); 186 187 clang::DeclContext *GetClangDeclContextForDIE(const DWARFDIE &die); 188 189 clang::DeclContext *GetClangDeclContextContainingDIE(const DWARFDIE &die, 190 DWARFDIE *decl_ctx_die); 191 lldb_private::OptionalClangModuleID GetOwningClangModule(const DWARFDIE &die); 192 193 bool CopyUniqueClassMethodTypes(const DWARFDIE &src_class_die, 194 const DWARFDIE &dst_class_die, 195 lldb_private::Type *class_type, 196 std::vector<DWARFDIE> &failures); 197 198 clang::DeclContext *GetCachedClangDeclContextForDIE(const DWARFDIE &die); 199 200 void LinkDeclContextToDIE(clang::DeclContext *decl_ctx, const DWARFDIE &die); 201 202 void LinkDeclToDIE(clang::Decl *decl, const DWARFDIE &die); 203 204 /// If \p type_sp is valid, calculate and set its symbol context scope, and 205 /// update the type list for its backing symbol file. 206 /// 207 /// Returns \p type_sp. 208 lldb::TypeSP 209 UpdateSymbolContextScopeForType(const lldb_private::SymbolContext &sc, 210 const DWARFDIE &die, lldb::TypeSP type_sp); 211 212 /// Follow Clang Module Skeleton CU references to find a type definition. 213 lldb::TypeSP ParseTypeFromClangModule(const lldb_private::SymbolContext &sc, 214 const DWARFDIE &die, 215 lldb_private::Log *log); 216 217 // Return true if this type is a declaration to a type in an external 218 // module. 219 lldb::ModuleSP GetModuleForType(const DWARFDIE &die); 220 221 private: 222 struct FieldInfo { 223 uint64_t bit_size = 0; 224 uint64_t bit_offset = 0; 225 bool is_bitfield = false; 226 bool is_artificial = false; 227 228 FieldInfo() = default; 229 230 void SetIsBitfield(bool flag) { is_bitfield = flag; } 231 bool IsBitfield() { return is_bitfield; } 232 233 void SetIsArtificial(bool flag) { is_artificial = flag; } 234 bool IsArtificial() const { return is_artificial; } 235 236 bool NextBitfieldOffsetIsValid(const uint64_t next_bit_offset) const { 237 // Any subsequent bitfields must not overlap and must be at a higher 238 // bit offset than any previous bitfield + size. 239 return (bit_size + bit_offset) <= next_bit_offset; 240 } 241 }; 242 243 /// Returns 'true' if we should create an unnamed bitfield 244 /// and add it to the parser's current AST. 245 /// 246 /// \param[in] last_field_info FieldInfo of the previous DW_TAG_member 247 /// we parsed. 248 /// \param[in] last_field_end Offset (in bits) where the last parsed field 249 /// ended. 250 /// \param[in] this_field_info FieldInfo of the current DW_TAG_member 251 /// being parsed. 252 /// \param[in] layout_info Layout information of all decls parsed by the 253 /// current parser. 254 bool ShouldCreateUnnamedBitfield( 255 FieldInfo const &last_field_info, uint64_t last_field_end, 256 FieldInfo const &this_field_info, 257 lldb_private::ClangASTImporter::LayoutInfo const &layout_info) const; 258 259 /// Parses a DW_TAG_APPLE_property DIE and appends the parsed data to the 260 /// list of delayed Objective-C properties. 261 /// 262 /// Note: The delayed property needs to be finalized to actually create the 263 /// property declarations in the module AST. 264 /// 265 /// \param die The DW_TAG_APPLE_property DIE that will be parsed. 266 /// \param parent_die The parent DIE. 267 /// \param class_clang_type The Objective-C class that will contain the 268 /// created property. 269 /// \param delayed_properties The list of delayed properties that the result 270 /// will be appended to. 271 void ParseObjCProperty(const DWARFDIE &die, const DWARFDIE &parent_die, 272 const lldb_private::CompilerType &class_clang_type, 273 DelayedPropertyList &delayed_properties); 274 275 void 276 ParseSingleMember(const DWARFDIE &die, const DWARFDIE &parent_die, 277 const lldb_private::CompilerType &class_clang_type, 278 lldb::AccessType default_accessibility, 279 lldb_private::ClangASTImporter::LayoutInfo &layout_info, 280 FieldInfo &last_field_info); 281 282 bool CompleteRecordType(const DWARFDIE &die, lldb_private::Type *type, 283 lldb_private::CompilerType &clang_type); 284 bool CompleteEnumType(const DWARFDIE &die, lldb_private::Type *type, 285 lldb_private::CompilerType &clang_type); 286 287 lldb::TypeSP ParseTypeModifier(const lldb_private::SymbolContext &sc, 288 const DWARFDIE &die, 289 ParsedDWARFTypeAttributes &attrs); 290 lldb::TypeSP ParseEnum(const lldb_private::SymbolContext &sc, 291 const DWARFDIE &die, ParsedDWARFTypeAttributes &attrs); 292 lldb::TypeSP ParseSubroutine(const DWARFDIE &die, 293 ParsedDWARFTypeAttributes &attrs); 294 lldb::TypeSP ParseArrayType(const DWARFDIE &die, 295 const ParsedDWARFTypeAttributes &attrs); 296 lldb::TypeSP ParsePointerToMemberType(const DWARFDIE &die, 297 const ParsedDWARFTypeAttributes &attrs); 298 299 /// Parses a DW_TAG_inheritance DIE into a base/super class. 300 /// 301 /// \param die The DW_TAG_inheritance DIE to parse. 302 /// \param parent_die The parent DIE of the given DIE. 303 /// \param class_clang_type The C++/Objective-C class representing parent_die. 304 /// For an Objective-C class this method sets the super class on success. For 305 /// a C++ class this will *not* add the result as a base class. 306 /// \param default_accessibility The default accessibility that is given to 307 /// base classes if they don't have an explicit accessibility set. 308 /// \param module_sp The current Module. 309 /// \param base_classes The list of C++ base classes that will be appended 310 /// with the parsed base class on success. 311 /// \param layout_info The layout information that will be updated for C++ 312 /// base classes with the base offset. 313 void ParseInheritance( 314 const DWARFDIE &die, const DWARFDIE &parent_die, 315 const lldb_private::CompilerType class_clang_type, 316 const lldb::AccessType default_accessibility, 317 const lldb::ModuleSP &module_sp, 318 std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes, 319 lldb_private::ClangASTImporter::LayoutInfo &layout_info); 320 }; 321 322 /// Parsed form of all attributes that are relevant for type reconstruction. 323 /// Some attributes are relevant for all kinds of types (declaration), while 324 /// others are only meaningful to a specific type (is_virtual) 325 struct ParsedDWARFTypeAttributes { 326 explicit ParsedDWARFTypeAttributes(const DWARFDIE &die); 327 328 lldb::AccessType accessibility = lldb::eAccessNone; 329 bool is_artificial = false; 330 bool is_complete_objc_class = false; 331 bool is_explicit = false; 332 bool is_forward_declaration = false; 333 bool is_inline = false; 334 bool is_scoped_enum = false; 335 bool is_vector = false; 336 bool is_virtual = false; 337 bool is_objc_direct_call = false; 338 bool exports_symbols = false; 339 clang::StorageClass storage = clang::SC_None; 340 const char *mangled_name = nullptr; 341 lldb_private::ConstString name; 342 lldb_private::Declaration decl; 343 DWARFDIE object_pointer; 344 DWARFFormValue abstract_origin; 345 DWARFFormValue containing_type; 346 DWARFFormValue signature; 347 DWARFFormValue specification; 348 DWARFFormValue type; 349 lldb::LanguageType class_language = lldb::eLanguageTypeUnknown; 350 std::optional<uint64_t> byte_size; 351 size_t calling_convention = llvm::dwarf::DW_CC_normal; 352 uint32_t bit_stride = 0; 353 uint32_t byte_stride = 0; 354 uint32_t encoding = 0; 355 clang::RefQualifierKind ref_qual = 356 clang::RQ_None; ///< Indicates ref-qualifier of 357 ///< C++ member function if present. 358 ///< Is RQ_None otherwise. 359 }; 360 361 #endif // LLDB_SOURCE_PLUGINS_SYMBOLFILE_DWARF_DWARFASTPARSERCLANG_H 362