| 1 | //===-- DWARFASTParserClang.cpp -------------------------------------------===// |
| 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 | #include <cstdlib> |
| 10 | |
| 11 | #include "DWARFASTParser.h" |
| 12 | #include "DWARFASTParserClang.h" |
| 13 | #include "DWARFDebugInfo.h" |
| 14 | #include "DWARFDeclContext.h" |
| 15 | #include "DWARFDefines.h" |
| 16 | #include "SymbolFileDWARF.h" |
| 17 | #include "SymbolFileDWARFDebugMap.h" |
| 18 | #include "SymbolFileDWARFDwo.h" |
| 19 | #include "UniqueDWARFASTType.h" |
| 20 | |
| 21 | #include "Plugins/ExpressionParser/Clang/ClangASTImporter.h" |
| 22 | #include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h" |
| 23 | #include "Plugins/ExpressionParser/Clang/ClangUtil.h" |
| 24 | #include "Plugins/Language/ObjC/ObjCLanguage.h" |
| 25 | #include "lldb/Core/Module.h" |
| 26 | #include "lldb/Core/Value.h" |
| 27 | #include "lldb/Host/Host.h" |
| 28 | #include "lldb/Symbol/CompileUnit.h" |
| 29 | #include "lldb/Symbol/Function.h" |
| 30 | #include "lldb/Symbol/ObjectFile.h" |
| 31 | #include "lldb/Symbol/SymbolFile.h" |
| 32 | #include "lldb/Symbol/TypeList.h" |
| 33 | #include "lldb/Symbol/TypeMap.h" |
| 34 | #include "lldb/Symbol/VariableList.h" |
| 35 | #include "lldb/Target/Language.h" |
| 36 | #include "lldb/Utility/LLDBAssert.h" |
| 37 | #include "lldb/Utility/Log.h" |
| 38 | #include "lldb/Utility/StreamString.h" |
| 39 | |
| 40 | #include "clang/AST/CXXInheritance.h" |
| 41 | #include "clang/AST/DeclBase.h" |
| 42 | #include "clang/AST/DeclCXX.h" |
| 43 | #include "clang/AST/DeclObjC.h" |
| 44 | #include "clang/AST/DeclTemplate.h" |
| 45 | #include "clang/AST/Type.h" |
| 46 | #include "clang/Basic/Specifiers.h" |
| 47 | #include "llvm/ADT/StringExtras.h" |
| 48 | #include "llvm/DebugInfo/DWARF/DWARFAddressRange.h" |
| 49 | #include "llvm/DebugInfo/DWARF/DWARFTypePrinter.h" |
| 50 | #include "llvm/Demangle/Demangle.h" |
| 51 | |
| 52 | #include <map> |
| 53 | #include <memory> |
| 54 | #include <optional> |
| 55 | #include <vector> |
| 56 | |
| 57 | //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN |
| 58 | |
| 59 | #ifdef ENABLE_DEBUG_PRINTF |
| 60 | #include <cstdio> |
| 61 | #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__) |
| 62 | #else |
| 63 | #define DEBUG_PRINTF(fmt, ...) |
| 64 | #endif |
| 65 | |
| 66 | using namespace lldb; |
| 67 | using namespace lldb_private; |
| 68 | using namespace lldb_private::dwarf; |
| 69 | using namespace lldb_private::plugin::dwarf; |
| 70 | |
| 71 | DWARFASTParserClang::DWARFASTParserClang(TypeSystemClang &ast) |
| 72 | : DWARFASTParser(Kind::DWARFASTParserClang), m_ast(ast), |
| 73 | m_die_to_decl_ctx(), m_decl_ctx_to_die() {} |
| 74 | |
| 75 | DWARFASTParserClang::~DWARFASTParserClang() = default; |
| 76 | |
| 77 | static bool DeclKindIsCXXClass(clang::Decl::Kind decl_kind) { |
| 78 | switch (decl_kind) { |
| 79 | case clang::Decl::CXXRecord: |
| 80 | case clang::Decl::ClassTemplateSpecialization: |
| 81 | return true; |
| 82 | default: |
| 83 | break; |
| 84 | } |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | ClangASTImporter &DWARFASTParserClang::GetClangASTImporter() { |
| 90 | if (!m_clang_ast_importer_up) { |
| 91 | m_clang_ast_importer_up = std::make_unique<ClangASTImporter>(); |
| 92 | } |
| 93 | return *m_clang_ast_importer_up; |
| 94 | } |
| 95 | |
| 96 | /// Detect a forward declaration that is nested in a DW_TAG_module. |
| 97 | static bool IsClangModuleFwdDecl(const DWARFDIE &Die) { |
| 98 | if (!Die.GetAttributeValueAsUnsigned(attr: DW_AT_declaration, fail_value: 0)) |
| 99 | return false; |
| 100 | auto Parent = Die.GetParent(); |
| 101 | while (Parent.IsValid()) { |
| 102 | if (Parent.Tag() == DW_TAG_module) |
| 103 | return true; |
| 104 | Parent = Parent.GetParent(); |
| 105 | } |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | static DWARFDIE GetContainingClangModuleDIE(const DWARFDIE &die) { |
| 110 | if (die.IsValid()) { |
| 111 | DWARFDIE top_module_die; |
| 112 | // Now make sure this DIE is scoped in a DW_TAG_module tag and return true |
| 113 | // if so |
| 114 | for (DWARFDIE parent = die.GetParent(); parent.IsValid(); |
| 115 | parent = parent.GetParent()) { |
| 116 | const dw_tag_t tag = parent.Tag(); |
| 117 | if (tag == DW_TAG_module) |
| 118 | top_module_die = parent; |
| 119 | else if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit) |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | return top_module_die; |
| 124 | } |
| 125 | return DWARFDIE(); |
| 126 | } |
| 127 | |
| 128 | static lldb::ModuleSP GetContainingClangModule(const DWARFDIE &die) { |
| 129 | if (die.IsValid()) { |
| 130 | DWARFDIE clang_module_die = GetContainingClangModuleDIE(die); |
| 131 | |
| 132 | if (clang_module_die) { |
| 133 | const char *module_name = clang_module_die.GetName(); |
| 134 | if (module_name) |
| 135 | return die.GetDWARF()->GetExternalModule( |
| 136 | name: lldb_private::ConstString(module_name)); |
| 137 | } |
| 138 | } |
| 139 | return lldb::ModuleSP(); |
| 140 | } |
| 141 | |
| 142 | // Returns true if the given artificial field name should be ignored when |
| 143 | // parsing the DWARF. |
| 144 | static bool ShouldIgnoreArtificialField(llvm::StringRef FieldName) { |
| 145 | return FieldName.starts_with(Prefix: "_vptr$" ) |
| 146 | // gdb emit vtable pointer as "_vptr.classname" |
| 147 | || FieldName.starts_with(Prefix: "_vptr." ); |
| 148 | } |
| 149 | |
| 150 | /// Returns true for C++ constructs represented by clang::CXXRecordDecl |
| 151 | static bool TagIsRecordType(dw_tag_t tag) { |
| 152 | switch (tag) { |
| 153 | case DW_TAG_class_type: |
| 154 | case DW_TAG_structure_type: |
| 155 | case DW_TAG_union_type: |
| 156 | return true; |
| 157 | default: |
| 158 | return false; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /// Get the object parameter DIE if one exists, otherwise returns |
| 163 | /// a default DWARFDIE. If \c containing_decl_ctx is not a valid |
| 164 | /// C++ declaration context for class methods, assume no object |
| 165 | /// parameter exists for the given \c subprogram. |
| 166 | static DWARFDIE |
| 167 | GetCXXObjectParameter(const DWARFDIE &subprogram, |
| 168 | const clang::DeclContext &containing_decl_ctx) { |
| 169 | assert(subprogram.Tag() == DW_TAG_subprogram || |
| 170 | subprogram.Tag() == DW_TAG_inlined_subroutine || |
| 171 | subprogram.Tag() == DW_TAG_subroutine_type); |
| 172 | |
| 173 | if (!DeclKindIsCXXClass(decl_kind: containing_decl_ctx.getDeclKind())) |
| 174 | return {}; |
| 175 | |
| 176 | if (DWARFDIE object_parameter = |
| 177 | subprogram.GetAttributeValueAsReferenceDIE(attr: DW_AT_object_pointer)) |
| 178 | return object_parameter; |
| 179 | |
| 180 | // If no DW_AT_object_pointer was specified, assume the implicit object |
| 181 | // parameter is the first parameter to the function, is called "this" and is |
| 182 | // artificial (which is what most compilers would generate). |
| 183 | auto children = subprogram.children(); |
| 184 | auto it = llvm::find_if(Range&: children, P: [](const DWARFDIE &child) { |
| 185 | return child.Tag() == DW_TAG_formal_parameter; |
| 186 | }); |
| 187 | |
| 188 | if (it == children.end()) |
| 189 | return {}; |
| 190 | |
| 191 | DWARFDIE object_pointer = *it; |
| 192 | |
| 193 | if (!object_pointer.GetAttributeValueAsUnsigned(attr: DW_AT_artificial, fail_value: 0)) |
| 194 | return {}; |
| 195 | |
| 196 | // Often times compilers omit the "this" name for the |
| 197 | // specification DIEs, so we can't rely upon the name being in |
| 198 | // the formal parameter DIE... |
| 199 | if (const char *name = object_pointer.GetName(); |
| 200 | name && ::strcmp(s1: name, s2: "this" ) != 0) |
| 201 | return {}; |
| 202 | |
| 203 | return object_pointer; |
| 204 | } |
| 205 | |
| 206 | /// In order to determine the CV-qualifiers for a C++ class |
| 207 | /// method in DWARF, we have to look at the CV-qualifiers of |
| 208 | /// the object parameter's type. |
| 209 | static unsigned GetCXXMethodCVQuals(const DWARFDIE &subprogram, |
| 210 | const DWARFDIE &object_parameter) { |
| 211 | if (!subprogram || !object_parameter) |
| 212 | return 0; |
| 213 | |
| 214 | Type *this_type = subprogram.ResolveTypeUID( |
| 215 | die: object_parameter.GetAttributeValueAsReferenceDIE(attr: DW_AT_type)); |
| 216 | if (!this_type) |
| 217 | return 0; |
| 218 | |
| 219 | uint32_t encoding_mask = this_type->GetEncodingMask(); |
| 220 | unsigned cv_quals = 0; |
| 221 | if (encoding_mask & (1u << Type::eEncodingIsConstUID)) |
| 222 | cv_quals |= clang::Qualifiers::Const; |
| 223 | if (encoding_mask & (1u << Type::eEncodingIsVolatileUID)) |
| 224 | cv_quals |= clang::Qualifiers::Volatile; |
| 225 | |
| 226 | return cv_quals; |
| 227 | } |
| 228 | |
| 229 | TypeSP DWARFASTParserClang::ParseTypeFromClangModule(const SymbolContext &sc, |
| 230 | const DWARFDIE &die, |
| 231 | Log *log) { |
| 232 | ModuleSP clang_module_sp = GetContainingClangModule(die); |
| 233 | if (!clang_module_sp) |
| 234 | return TypeSP(); |
| 235 | |
| 236 | // If this type comes from a Clang module, recursively look in the |
| 237 | // DWARF section of the .pcm file in the module cache. Clang |
| 238 | // generates DWO skeleton units as breadcrumbs to find them. |
| 239 | std::vector<lldb_private::CompilerContext> die_context = die.GetDeclContext(); |
| 240 | TypeQuery query(die_context, TypeQueryOptions::e_module_search | |
| 241 | TypeQueryOptions::e_find_one); |
| 242 | TypeResults results; |
| 243 | |
| 244 | // The type in the Clang module must have the same language as the current CU. |
| 245 | query.AddLanguage(language: SymbolFileDWARF::GetLanguageFamily(unit&: *die.GetCU())); |
| 246 | clang_module_sp->FindTypes(query, results); |
| 247 | TypeSP pcm_type_sp = results.GetTypeMap().FirstType(); |
| 248 | if (!pcm_type_sp) { |
| 249 | // Since this type is defined in one of the Clang modules imported |
| 250 | // by this symbol file, search all of them. Instead of calling |
| 251 | // sym_file->FindTypes(), which would return this again, go straight |
| 252 | // to the imported modules. |
| 253 | auto &sym_file = die.GetCU()->GetSymbolFileDWARF(); |
| 254 | |
| 255 | // Well-formed clang modules never form cycles; guard against corrupted |
| 256 | // ones by inserting the current file. |
| 257 | results.AlreadySearched(sym_file: &sym_file); |
| 258 | sym_file.ForEachExternalModule( |
| 259 | *sc.comp_unit, results.GetSearchedSymbolFiles(), [&](Module &module) { |
| 260 | module.FindTypes(query, results); |
| 261 | pcm_type_sp = results.GetTypeMap().FirstType(); |
| 262 | return (bool)pcm_type_sp; |
| 263 | }); |
| 264 | } |
| 265 | |
| 266 | if (!pcm_type_sp) |
| 267 | return TypeSP(); |
| 268 | |
| 269 | // We found a real definition for this type in the Clang module, so lets use |
| 270 | // it and cache the fact that we found a complete type for this die. |
| 271 | lldb_private::CompilerType pcm_type = pcm_type_sp->GetForwardCompilerType(); |
| 272 | lldb_private::CompilerType type = |
| 273 | GetClangASTImporter().CopyType(dst&: m_ast, src_type: pcm_type); |
| 274 | |
| 275 | if (!type) |
| 276 | return TypeSP(); |
| 277 | |
| 278 | // Under normal operation pcm_type is a shallow forward declaration |
| 279 | // that gets completed later. This is necessary to support cyclic |
| 280 | // data structures. If, however, pcm_type is already complete (for |
| 281 | // example, because it was loaded for a different target before), |
| 282 | // the definition needs to be imported right away, too. |
| 283 | // Type::ResolveClangType() effectively ignores the ResolveState |
| 284 | // inside type_sp and only looks at IsDefined(), so it never calls |
| 285 | // ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(), |
| 286 | // which does extra work for Objective-C classes. This would result |
| 287 | // in only the forward declaration to be visible. |
| 288 | if (pcm_type.IsDefined()) |
| 289 | GetClangASTImporter().RequireCompleteType(type: ClangUtil::GetQualType(ct: type)); |
| 290 | |
| 291 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 292 | auto type_sp = dwarf->MakeType( |
| 293 | uid: die.GetID(), name: pcm_type_sp->GetName(), |
| 294 | byte_size: llvm::expectedToOptional(E: pcm_type_sp->GetByteSize(exe_scope: nullptr)), context: nullptr, |
| 295 | LLDB_INVALID_UID, encoding_uid_type: Type::eEncodingInvalid, decl: &pcm_type_sp->GetDeclaration(), |
| 296 | compiler_qual_type: type, compiler_type_resolve_state: Type::ResolveState::Forward, |
| 297 | opaque_payload: TypePayloadClang(GetOwningClangModule(die))); |
| 298 | clang::TagDecl *tag_decl = TypeSystemClang::GetAsTagDecl(type); |
| 299 | if (tag_decl) { |
| 300 | LinkDeclContextToDIE(tag_decl, die); |
| 301 | } else { |
| 302 | clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(die); |
| 303 | if (defn_decl_ctx) |
| 304 | LinkDeclContextToDIE(decl_ctx: defn_decl_ctx, die); |
| 305 | } |
| 306 | |
| 307 | return type_sp; |
| 308 | } |
| 309 | |
| 310 | /// This function ensures we are able to add members (nested types, functions, |
| 311 | /// etc.) to this type. It does so by starting its definition even if one cannot |
| 312 | /// be found in the debug info. This means the type may need to be "forcibly |
| 313 | /// completed" later -- see CompleteTypeFromDWARF). |
| 314 | static void PrepareContextToReceiveMembers(TypeSystemClang &ast, |
| 315 | ClangASTImporter &ast_importer, |
| 316 | clang::DeclContext *decl_ctx, |
| 317 | DWARFDIE die, |
| 318 | const char *type_name_cstr) { |
| 319 | auto *tag_decl_ctx = clang::dyn_cast<clang::TagDecl>(Val: decl_ctx); |
| 320 | if (!tag_decl_ctx) |
| 321 | return; // Non-tag context are always ready. |
| 322 | |
| 323 | // We have already completed the type or it is already prepared. |
| 324 | if (tag_decl_ctx->isCompleteDefinition() || tag_decl_ctx->isBeingDefined()) |
| 325 | return; |
| 326 | |
| 327 | // If this tag was imported from another AST context (in the gmodules case), |
| 328 | // we can complete the type by doing a full import. |
| 329 | |
| 330 | // If this type was not imported from an external AST, there's nothing to do. |
| 331 | CompilerType type = ast.GetTypeForDecl(decl: tag_decl_ctx); |
| 332 | if (type && ast_importer.CanImport(type)) { |
| 333 | auto qual_type = ClangUtil::GetQualType(ct: type); |
| 334 | if (ast_importer.RequireCompleteType(type: qual_type)) |
| 335 | return; |
| 336 | die.GetDWARF()->GetObjectFile()->GetModule()->ReportError( |
| 337 | format: "Unable to complete the Decl context for DIE {0} at offset " |
| 338 | "{1:x16}.\nPlease file a bug report." , |
| 339 | args: type_name_cstr ? type_name_cstr : "" , args: die.GetOffset()); |
| 340 | } |
| 341 | |
| 342 | // We don't have a type definition and/or the import failed, but we need to |
| 343 | // add members to it. Start the definition to make that possible. If the type |
| 344 | // has no external storage we also have to complete the definition. Otherwise, |
| 345 | // that will happen when we are asked to complete the type |
| 346 | // (CompleteTypeFromDWARF). |
| 347 | ast.StartTagDeclarationDefinition(type); |
| 348 | if (!tag_decl_ctx->hasExternalLexicalStorage()) { |
| 349 | ast.SetDeclIsForcefullyCompleted(tag_decl_ctx); |
| 350 | ast.CompleteTagDeclarationDefinition(type); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | ParsedDWARFTypeAttributes::ParsedDWARFTypeAttributes(const DWARFDIE &die) { |
| 355 | DWARFAttributes attributes = die.GetAttributes(); |
| 356 | for (size_t i = 0; i < attributes.Size(); ++i) { |
| 357 | dw_attr_t attr = attributes.AttributeAtIndex(i); |
| 358 | DWARFFormValue form_value; |
| 359 | if (!attributes.ExtractFormValueAtIndex(i, form_value)) |
| 360 | continue; |
| 361 | switch (attr) { |
| 362 | default: |
| 363 | break; |
| 364 | case DW_AT_abstract_origin: |
| 365 | abstract_origin = form_value; |
| 366 | break; |
| 367 | |
| 368 | case DW_AT_accessibility: |
| 369 | accessibility = |
| 370 | DWARFASTParser::GetAccessTypeFromDWARF(dwarf_accessibility: form_value.Unsigned()); |
| 371 | break; |
| 372 | |
| 373 | case DW_AT_artificial: |
| 374 | is_artificial = form_value.Boolean(); |
| 375 | break; |
| 376 | |
| 377 | case DW_AT_bit_stride: |
| 378 | bit_stride = form_value.Unsigned(); |
| 379 | break; |
| 380 | |
| 381 | case DW_AT_byte_size: |
| 382 | byte_size = form_value.Unsigned(); |
| 383 | break; |
| 384 | |
| 385 | case DW_AT_alignment: |
| 386 | alignment = form_value.Unsigned(); |
| 387 | break; |
| 388 | |
| 389 | case DW_AT_byte_stride: |
| 390 | byte_stride = form_value.Unsigned(); |
| 391 | break; |
| 392 | |
| 393 | case DW_AT_calling_convention: |
| 394 | calling_convention = form_value.Unsigned(); |
| 395 | break; |
| 396 | |
| 397 | case DW_AT_containing_type: |
| 398 | containing_type = form_value; |
| 399 | break; |
| 400 | |
| 401 | case DW_AT_decl_file: |
| 402 | // die.GetCU() can differ if DW_AT_specification uses DW_FORM_ref_addr. |
| 403 | decl.SetFile( |
| 404 | attributes.CompileUnitAtIndex(i)->GetFile(file_idx: form_value.Unsigned())); |
| 405 | break; |
| 406 | case DW_AT_decl_line: |
| 407 | decl.SetLine(form_value.Unsigned()); |
| 408 | break; |
| 409 | case DW_AT_decl_column: |
| 410 | decl.SetColumn(form_value.Unsigned()); |
| 411 | break; |
| 412 | |
| 413 | case DW_AT_declaration: |
| 414 | is_forward_declaration = form_value.Boolean(); |
| 415 | break; |
| 416 | |
| 417 | case DW_AT_encoding: |
| 418 | encoding = form_value.Unsigned(); |
| 419 | break; |
| 420 | |
| 421 | case DW_AT_enum_class: |
| 422 | is_scoped_enum = form_value.Boolean(); |
| 423 | break; |
| 424 | |
| 425 | case DW_AT_explicit: |
| 426 | is_explicit = form_value.Boolean(); |
| 427 | break; |
| 428 | |
| 429 | case DW_AT_external: |
| 430 | if (form_value.Unsigned()) |
| 431 | storage = clang::SC_Extern; |
| 432 | break; |
| 433 | |
| 434 | case DW_AT_inline: |
| 435 | is_inline = form_value.Boolean(); |
| 436 | break; |
| 437 | |
| 438 | case DW_AT_linkage_name: |
| 439 | case DW_AT_MIPS_linkage_name: |
| 440 | mangled_name = form_value.AsCString(); |
| 441 | break; |
| 442 | |
| 443 | case DW_AT_name: |
| 444 | name.SetCString(form_value.AsCString()); |
| 445 | break; |
| 446 | |
| 447 | case DW_AT_object_pointer: |
| 448 | // GetAttributes follows DW_AT_specification. |
| 449 | // DW_TAG_subprogram definitions and declarations may both |
| 450 | // have a DW_AT_object_pointer. Don't overwrite the one |
| 451 | // we parsed for the definition with the one from the declaration. |
| 452 | if (!object_pointer.IsValid()) |
| 453 | object_pointer = form_value.Reference(); |
| 454 | break; |
| 455 | |
| 456 | case DW_AT_signature: |
| 457 | signature = form_value; |
| 458 | break; |
| 459 | |
| 460 | case DW_AT_specification: |
| 461 | specification = form_value; |
| 462 | break; |
| 463 | |
| 464 | case DW_AT_type: |
| 465 | type = form_value; |
| 466 | break; |
| 467 | |
| 468 | case DW_AT_virtuality: |
| 469 | is_virtual = form_value.Boolean(); |
| 470 | break; |
| 471 | |
| 472 | case DW_AT_APPLE_objc_complete_type: |
| 473 | is_complete_objc_class = form_value.Signed(); |
| 474 | break; |
| 475 | |
| 476 | case DW_AT_APPLE_objc_direct: |
| 477 | is_objc_direct_call = true; |
| 478 | break; |
| 479 | |
| 480 | case DW_AT_APPLE_runtime_class: |
| 481 | class_language = (LanguageType)form_value.Signed(); |
| 482 | break; |
| 483 | |
| 484 | case DW_AT_GNU_vector: |
| 485 | is_vector = form_value.Boolean(); |
| 486 | break; |
| 487 | case DW_AT_export_symbols: |
| 488 | exports_symbols = form_value.Boolean(); |
| 489 | break; |
| 490 | case DW_AT_rvalue_reference: |
| 491 | ref_qual = clang::RQ_RValue; |
| 492 | break; |
| 493 | case DW_AT_reference: |
| 494 | ref_qual = clang::RQ_LValue; |
| 495 | break; |
| 496 | case DW_AT_APPLE_enum_kind: |
| 497 | enum_kind = static_cast<clang::EnumExtensibilityAttr::Kind>( |
| 498 | form_value.Unsigned()); |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static std::string GetUnitName(const DWARFDIE &die) { |
| 505 | if (DWARFUnit *unit = die.GetCU()) |
| 506 | return unit->GetAbsolutePath().GetPath(); |
| 507 | return "<missing DWARF unit path>" ; |
| 508 | } |
| 509 | |
| 510 | TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc, |
| 511 | const DWARFDIE &die, |
| 512 | bool *type_is_new_ptr) { |
| 513 | if (type_is_new_ptr) |
| 514 | *type_is_new_ptr = false; |
| 515 | |
| 516 | if (!die) |
| 517 | return nullptr; |
| 518 | |
| 519 | Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups); |
| 520 | |
| 521 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 522 | if (log) { |
| 523 | DWARFDIE context_die; |
| 524 | clang::DeclContext *context = |
| 525 | GetClangDeclContextContainingDIE(die, decl_ctx_die: &context_die); |
| 526 | |
| 527 | dwarf->GetObjectFile()->GetModule()->LogMessage( |
| 528 | log, |
| 529 | format: "DWARFASTParserClang::ParseTypeFromDWARF " |
| 530 | "(die = {0:x16}, decl_ctx = {1:p} (die " |
| 531 | "{2:x16})) {3} ({4}) name = '{5}')" , |
| 532 | args: die.GetOffset(), args: static_cast<void *>(context), args: context_die.GetOffset(), |
| 533 | args: DW_TAG_value_to_name(tag: die.Tag()), args: die.Tag(), args: die.GetName()); |
| 534 | } |
| 535 | |
| 536 | // Set a bit that lets us know that we are currently parsing this |
| 537 | if (auto [it, inserted] = |
| 538 | dwarf->GetDIEToType().try_emplace(Key: die.GetDIE(), DIE_IS_BEING_PARSED); |
| 539 | !inserted) { |
| 540 | if (it->getSecond() == nullptr || it->getSecond() == DIE_IS_BEING_PARSED) |
| 541 | return nullptr; |
| 542 | return it->getSecond()->shared_from_this(); |
| 543 | } |
| 544 | |
| 545 | ParsedDWARFTypeAttributes attrs(die); |
| 546 | |
| 547 | TypeSP type_sp; |
| 548 | if (DWARFDIE signature_die = attrs.signature.Reference()) { |
| 549 | type_sp = ParseTypeFromDWARF(sc, die: signature_die, type_is_new_ptr); |
| 550 | if (type_sp) { |
| 551 | if (clang::DeclContext *decl_ctx = |
| 552 | GetCachedClangDeclContextForDIE(die: signature_die)) |
| 553 | LinkDeclContextToDIE(decl_ctx, die); |
| 554 | } |
| 555 | } else { |
| 556 | if (type_is_new_ptr) |
| 557 | *type_is_new_ptr = true; |
| 558 | |
| 559 | const dw_tag_t tag = die.Tag(); |
| 560 | |
| 561 | switch (tag) { |
| 562 | case DW_TAG_typedef: |
| 563 | case DW_TAG_base_type: |
| 564 | case DW_TAG_pointer_type: |
| 565 | case DW_TAG_reference_type: |
| 566 | case DW_TAG_rvalue_reference_type: |
| 567 | case DW_TAG_const_type: |
| 568 | case DW_TAG_restrict_type: |
| 569 | case DW_TAG_volatile_type: |
| 570 | case DW_TAG_LLVM_ptrauth_type: |
| 571 | case DW_TAG_atomic_type: |
| 572 | case DW_TAG_unspecified_type: |
| 573 | type_sp = ParseTypeModifier(sc, die, attrs); |
| 574 | break; |
| 575 | case DW_TAG_structure_type: |
| 576 | case DW_TAG_union_type: |
| 577 | case DW_TAG_class_type: |
| 578 | type_sp = ParseStructureLikeDIE(sc, die, attrs); |
| 579 | break; |
| 580 | case DW_TAG_enumeration_type: |
| 581 | type_sp = ParseEnum(sc, die, attrs); |
| 582 | break; |
| 583 | case DW_TAG_inlined_subroutine: |
| 584 | case DW_TAG_subprogram: |
| 585 | case DW_TAG_subroutine_type: |
| 586 | type_sp = ParseSubroutine(die, attrs); |
| 587 | break; |
| 588 | case DW_TAG_array_type: |
| 589 | type_sp = ParseArrayType(die, attrs); |
| 590 | break; |
| 591 | case DW_TAG_ptr_to_member_type: |
| 592 | type_sp = ParsePointerToMemberType(die, attrs); |
| 593 | break; |
| 594 | default: |
| 595 | dwarf->GetObjectFile()->GetModule()->ReportError( |
| 596 | format: "[{0:x16}]: unhandled type tag {1:x4} ({2}), " |
| 597 | "please file a bug and " |
| 598 | "attach the file at the start of this error message" , |
| 599 | args: die.GetOffset(), args: tag, args: DW_TAG_value_to_name(tag)); |
| 600 | break; |
| 601 | } |
| 602 | UpdateSymbolContextScopeForType(sc, die, type_sp); |
| 603 | } |
| 604 | if (type_sp) { |
| 605 | dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get(); |
| 606 | } |
| 607 | return type_sp; |
| 608 | } |
| 609 | |
| 610 | static std::optional<uint32_t> |
| 611 | (DWARFDIE const &die, DWARFFormValue const &form_value, |
| 612 | ModuleSP module_sp) { |
| 613 | Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups); |
| 614 | |
| 615 | // With DWARF 3 and later, if the value is an integer constant, |
| 616 | // this form value is the offset in bytes from the beginning of |
| 617 | // the containing entity. |
| 618 | if (!form_value.BlockData()) |
| 619 | return form_value.Unsigned(); |
| 620 | |
| 621 | Value initialValue(0); |
| 622 | const DWARFDataExtractor &debug_info_data = die.GetData(); |
| 623 | uint32_t block_length = form_value.Unsigned(); |
| 624 | uint32_t block_offset = |
| 625 | form_value.BlockData() - debug_info_data.GetDataStart(); |
| 626 | |
| 627 | llvm::Expected<Value> memberOffset = DWARFExpression::Evaluate( |
| 628 | /*ExecutionContext=*/exe_ctx: nullptr, |
| 629 | /*RegisterContext=*/reg_ctx: nullptr, module_sp, |
| 630 | opcodes: DataExtractor(debug_info_data, block_offset, block_length), dwarf_cu: die.GetCU(), |
| 631 | reg_set: eRegisterKindDWARF, initial_value_ptr: &initialValue, object_address_ptr: nullptr); |
| 632 | if (!memberOffset) { |
| 633 | LLDB_LOG_ERROR(log, memberOffset.takeError(), |
| 634 | "ExtractDataMemberLocation failed: {0}" ); |
| 635 | return {}; |
| 636 | } |
| 637 | |
| 638 | return memberOffset->ResolveValue(exe_ctx: nullptr).UInt(); |
| 639 | } |
| 640 | |
| 641 | static TypePayloadClang GetPtrAuthMofidierPayload(const DWARFDIE &die) { |
| 642 | auto getAttr = [&](llvm::dwarf::Attribute Attr, unsigned defaultValue = 0) { |
| 643 | return die.GetAttributeValueAsUnsigned(attr: Attr, fail_value: defaultValue); |
| 644 | }; |
| 645 | const unsigned key = getAttr(DW_AT_LLVM_ptrauth_key); |
| 646 | const bool addr_disc = getAttr(DW_AT_LLVM_ptrauth_address_discriminated); |
| 647 | const unsigned = getAttr(DW_AT_LLVM_ptrauth_extra_discriminator); |
| 648 | const bool isapointer = getAttr(DW_AT_LLVM_ptrauth_isa_pointer); |
| 649 | const bool authenticates_null_values = |
| 650 | getAttr(DW_AT_LLVM_ptrauth_authenticates_null_values); |
| 651 | const unsigned authentication_mode_int = getAttr( |
| 652 | DW_AT_LLVM_ptrauth_authentication_mode, |
| 653 | static_cast<unsigned>(clang::PointerAuthenticationMode::SignAndAuth)); |
| 654 | clang::PointerAuthenticationMode authentication_mode = |
| 655 | clang::PointerAuthenticationMode::SignAndAuth; |
| 656 | if (authentication_mode_int >= |
| 657 | static_cast<unsigned>(clang::PointerAuthenticationMode::None) && |
| 658 | authentication_mode_int <= |
| 659 | static_cast<unsigned>( |
| 660 | clang::PointerAuthenticationMode::SignAndAuth)) { |
| 661 | authentication_mode = |
| 662 | static_cast<clang::PointerAuthenticationMode>(authentication_mode_int); |
| 663 | } else { |
| 664 | die.GetDWARF()->GetObjectFile()->GetModule()->ReportError( |
| 665 | format: "[{0:x16}]: invalid pointer authentication mode method {1:x4}" , |
| 666 | args: die.GetOffset(), args: authentication_mode_int); |
| 667 | } |
| 668 | auto ptr_auth = clang::PointerAuthQualifier::Create( |
| 669 | Key: key, IsAddressDiscriminated: addr_disc, ExtraDiscriminator: extra, AuthenticationMode: authentication_mode, IsIsaPointer: isapointer, |
| 670 | AuthenticatesNullValues: authenticates_null_values); |
| 671 | return TypePayloadClang(ptr_auth.getAsOpaqueValue()); |
| 672 | } |
| 673 | |
| 674 | lldb::TypeSP |
| 675 | DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc, |
| 676 | const DWARFDIE &die, |
| 677 | ParsedDWARFTypeAttributes &attrs) { |
| 678 | Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups); |
| 679 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 680 | const dw_tag_t tag = die.Tag(); |
| 681 | LanguageType cu_language = SymbolFileDWARF::GetLanguage(unit&: *die.GetCU()); |
| 682 | Type::ResolveState resolve_state = Type::ResolveState::Unresolved; |
| 683 | Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID; |
| 684 | TypePayloadClang payload(GetOwningClangModule(die)); |
| 685 | TypeSP type_sp; |
| 686 | CompilerType clang_type; |
| 687 | |
| 688 | if (tag == DW_TAG_typedef) { |
| 689 | // DeclContext will be populated when the clang type is materialized in |
| 690 | // Type::ResolveCompilerType. |
| 691 | PrepareContextToReceiveMembers( |
| 692 | ast&: m_ast, ast_importer&: GetClangASTImporter(), |
| 693 | decl_ctx: GetClangDeclContextContainingDIE(die, decl_ctx_die: nullptr), die, |
| 694 | type_name_cstr: attrs.name.GetCString()); |
| 695 | |
| 696 | if (attrs.type.IsValid()) { |
| 697 | // Try to parse a typedef from the (DWARF embedded in the) Clang |
| 698 | // module file first as modules can contain typedef'ed |
| 699 | // structures that have no names like: |
| 700 | // |
| 701 | // typedef struct { int a; } Foo; |
| 702 | // |
| 703 | // In this case we will have a structure with no name and a |
| 704 | // typedef named "Foo" that points to this unnamed |
| 705 | // structure. The name in the typedef is the only identifier for |
| 706 | // the struct, so always try to get typedefs from Clang modules |
| 707 | // if possible. |
| 708 | // |
| 709 | // The type_sp returned will be empty if the typedef doesn't |
| 710 | // exist in a module file, so it is cheap to call this function |
| 711 | // just to check. |
| 712 | // |
| 713 | // If we don't do this we end up creating a TypeSP that says |
| 714 | // this is a typedef to type 0x123 (the DW_AT_type value would |
| 715 | // be 0x123 in the DW_TAG_typedef), and this is the unnamed |
| 716 | // structure type. We will have a hard time tracking down an |
| 717 | // unnammed structure type in the module debug info, so we make |
| 718 | // sure we don't get into this situation by always resolving |
| 719 | // typedefs from the module. |
| 720 | const DWARFDIE encoding_die = attrs.type.Reference(); |
| 721 | |
| 722 | // First make sure that the die that this is typedef'ed to _is_ |
| 723 | // just a declaration (DW_AT_declaration == 1), not a full |
| 724 | // definition since template types can't be represented in |
| 725 | // modules since only concrete instances of templates are ever |
| 726 | // emitted and modules won't contain those |
| 727 | if (encoding_die && |
| 728 | encoding_die.GetAttributeValueAsUnsigned(attr: DW_AT_declaration, fail_value: 0) == 1) { |
| 729 | type_sp = ParseTypeFromClangModule(sc, die, log); |
| 730 | if (type_sp) |
| 731 | return type_sp; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n" , die.GetID(), |
| 737 | DW_TAG_value_to_name(tag), type_name_cstr, |
| 738 | encoding_uid.Reference()); |
| 739 | |
| 740 | switch (tag) { |
| 741 | default: |
| 742 | break; |
| 743 | |
| 744 | case DW_TAG_unspecified_type: |
| 745 | if (attrs.name == "nullptr_t" || attrs.name == "decltype(nullptr)" ) { |
| 746 | resolve_state = Type::ResolveState::Full; |
| 747 | clang_type = m_ast.GetBasicType(type: eBasicTypeNullPtr); |
| 748 | break; |
| 749 | } |
| 750 | // Fall through to base type below in case we can handle the type |
| 751 | // there... |
| 752 | [[fallthrough]]; |
| 753 | |
| 754 | case DW_TAG_base_type: |
| 755 | resolve_state = Type::ResolveState::Full; |
| 756 | clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize( |
| 757 | type_name: attrs.name.GetStringRef(), dw_ate: attrs.encoding, |
| 758 | bit_size: attrs.byte_size.value_or(u: 0) * 8); |
| 759 | break; |
| 760 | |
| 761 | case DW_TAG_pointer_type: |
| 762 | encoding_data_type = Type::eEncodingIsPointerUID; |
| 763 | break; |
| 764 | case DW_TAG_reference_type: |
| 765 | encoding_data_type = Type::eEncodingIsLValueReferenceUID; |
| 766 | break; |
| 767 | case DW_TAG_rvalue_reference_type: |
| 768 | encoding_data_type = Type::eEncodingIsRValueReferenceUID; |
| 769 | break; |
| 770 | case DW_TAG_typedef: |
| 771 | encoding_data_type = Type::eEncodingIsTypedefUID; |
| 772 | break; |
| 773 | case DW_TAG_const_type: |
| 774 | encoding_data_type = Type::eEncodingIsConstUID; |
| 775 | break; |
| 776 | case DW_TAG_restrict_type: |
| 777 | encoding_data_type = Type::eEncodingIsRestrictUID; |
| 778 | break; |
| 779 | case DW_TAG_volatile_type: |
| 780 | encoding_data_type = Type::eEncodingIsVolatileUID; |
| 781 | break; |
| 782 | case DW_TAG_LLVM_ptrauth_type: |
| 783 | encoding_data_type = Type::eEncodingIsLLVMPtrAuthUID; |
| 784 | payload = GetPtrAuthMofidierPayload(die); |
| 785 | break; |
| 786 | case DW_TAG_atomic_type: |
| 787 | encoding_data_type = Type::eEncodingIsAtomicUID; |
| 788 | break; |
| 789 | } |
| 790 | |
| 791 | if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || |
| 792 | encoding_data_type == Type::eEncodingIsTypedefUID)) { |
| 793 | if (tag == DW_TAG_pointer_type) { |
| 794 | DWARFDIE target_die = die.GetReferencedDIE(attr: DW_AT_type); |
| 795 | |
| 796 | if (target_die.GetAttributeValueAsUnsigned(attr: DW_AT_APPLE_block, fail_value: 0)) { |
| 797 | // Blocks have a __FuncPtr inside them which is a pointer to a |
| 798 | // function of the proper type. |
| 799 | |
| 800 | for (DWARFDIE child_die : target_die.children()) { |
| 801 | if (!strcmp(s1: child_die.GetAttributeValueAsString(attr: DW_AT_name, fail_value: "" ), |
| 802 | s2: "__FuncPtr" )) { |
| 803 | DWARFDIE function_pointer_type = |
| 804 | child_die.GetReferencedDIE(attr: DW_AT_type); |
| 805 | |
| 806 | if (function_pointer_type) { |
| 807 | DWARFDIE function_type = |
| 808 | function_pointer_type.GetReferencedDIE(attr: DW_AT_type); |
| 809 | |
| 810 | bool function_type_is_new_pointer; |
| 811 | TypeSP lldb_function_type_sp = ParseTypeFromDWARF( |
| 812 | sc, die: function_type, type_is_new_ptr: &function_type_is_new_pointer); |
| 813 | |
| 814 | if (lldb_function_type_sp) { |
| 815 | clang_type = m_ast.CreateBlockPointerType( |
| 816 | function_type: lldb_function_type_sp->GetForwardCompilerType()); |
| 817 | encoding_data_type = Type::eEncodingIsUID; |
| 818 | attrs.type.Clear(); |
| 819 | resolve_state = Type::ResolveState::Full; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | break; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | if (cu_language == eLanguageTypeObjC || |
| 830 | cu_language == eLanguageTypeObjC_plus_plus) { |
| 831 | if (attrs.name) { |
| 832 | if (attrs.name == "id" ) { |
| 833 | if (log) |
| 834 | dwarf->GetObjectFile()->GetModule()->LogMessage( |
| 835 | log, |
| 836 | format: "SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' " |
| 837 | "is Objective-C 'id' built-in type." , |
| 838 | args: die.GetOffset(), args: DW_TAG_value_to_name(tag: die.Tag()), args: die.Tag(), |
| 839 | args: die.GetName()); |
| 840 | clang_type = m_ast.GetBasicType(type: eBasicTypeObjCID); |
| 841 | encoding_data_type = Type::eEncodingIsUID; |
| 842 | attrs.type.Clear(); |
| 843 | resolve_state = Type::ResolveState::Full; |
| 844 | } else if (attrs.name == "Class" ) { |
| 845 | if (log) |
| 846 | dwarf->GetObjectFile()->GetModule()->LogMessage( |
| 847 | log, |
| 848 | format: "SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' " |
| 849 | "is Objective-C 'Class' built-in type." , |
| 850 | args: die.GetOffset(), args: DW_TAG_value_to_name(tag: die.Tag()), args: die.Tag(), |
| 851 | args: die.GetName()); |
| 852 | clang_type = m_ast.GetBasicType(type: eBasicTypeObjCClass); |
| 853 | encoding_data_type = Type::eEncodingIsUID; |
| 854 | attrs.type.Clear(); |
| 855 | resolve_state = Type::ResolveState::Full; |
| 856 | } else if (attrs.name == "SEL" ) { |
| 857 | if (log) |
| 858 | dwarf->GetObjectFile()->GetModule()->LogMessage( |
| 859 | log, |
| 860 | format: "SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' " |
| 861 | "is Objective-C 'selector' built-in type." , |
| 862 | args: die.GetOffset(), args: DW_TAG_value_to_name(tag: die.Tag()), args: die.Tag(), |
| 863 | args: die.GetName()); |
| 864 | clang_type = m_ast.GetBasicType(type: eBasicTypeObjCSel); |
| 865 | encoding_data_type = Type::eEncodingIsUID; |
| 866 | attrs.type.Clear(); |
| 867 | resolve_state = Type::ResolveState::Full; |
| 868 | } |
| 869 | } else if (encoding_data_type == Type::eEncodingIsPointerUID && |
| 870 | attrs.type.IsValid()) { |
| 871 | // Clang sometimes erroneously emits id as objc_object*. In that |
| 872 | // case we fix up the type to "id". |
| 873 | |
| 874 | const DWARFDIE encoding_die = attrs.type.Reference(); |
| 875 | |
| 876 | if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type) { |
| 877 | llvm::StringRef struct_name = encoding_die.GetName(); |
| 878 | if (struct_name == "objc_object" ) { |
| 879 | if (log) |
| 880 | dwarf->GetObjectFile()->GetModule()->LogMessage( |
| 881 | log, |
| 882 | format: "SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' " |
| 883 | "is 'objc_object*', which we overrode to 'id'." , |
| 884 | args: die.GetOffset(), args: DW_TAG_value_to_name(tag: die.Tag()), args: die.Tag(), |
| 885 | args: die.GetName()); |
| 886 | clang_type = m_ast.GetBasicType(type: eBasicTypeObjCID); |
| 887 | encoding_data_type = Type::eEncodingIsUID; |
| 888 | attrs.type.Clear(); |
| 889 | resolve_state = Type::ResolveState::Full; |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | return dwarf->MakeType(uid: die.GetID(), name: attrs.name, byte_size: attrs.byte_size, context: nullptr, |
| 897 | encoding_uid: attrs.type.Reference().GetID(), encoding_uid_type: encoding_data_type, |
| 898 | decl: &attrs.decl, compiler_qual_type: clang_type, compiler_type_resolve_state: resolve_state, opaque_payload: payload); |
| 899 | } |
| 900 | |
| 901 | std::string DWARFASTParserClang::GetDIEClassTemplateParams(DWARFDIE die) { |
| 902 | if (DWARFDIE signature_die = die.GetReferencedDIE(attr: DW_AT_signature)) |
| 903 | die = signature_die; |
| 904 | |
| 905 | if (llvm::StringRef(die.GetName()).contains(Other: "<" )) |
| 906 | return {}; |
| 907 | |
| 908 | std::string name; |
| 909 | llvm::raw_string_ostream os(name); |
| 910 | llvm::DWARFTypePrinter<DWARFDIE> type_printer(os); |
| 911 | type_printer.appendAndTerminateTemplateParameters(D: die); |
| 912 | return name; |
| 913 | } |
| 914 | |
| 915 | void DWARFASTParserClang::MapDeclDIEToDefDIE( |
| 916 | const lldb_private::plugin::dwarf::DWARFDIE &decl_die, |
| 917 | const lldb_private::plugin::dwarf::DWARFDIE &def_die) { |
| 918 | LinkDeclContextToDIE(decl_ctx: GetCachedClangDeclContextForDIE(die: decl_die), die: def_die); |
| 919 | SymbolFileDWARF *dwarf = def_die.GetDWARF(); |
| 920 | ParsedDWARFTypeAttributes decl_attrs(decl_die); |
| 921 | ParsedDWARFTypeAttributes def_attrs(def_die); |
| 922 | ConstString unique_typename(decl_attrs.name); |
| 923 | Declaration decl_declaration(decl_attrs.decl); |
| 924 | GetUniqueTypeNameAndDeclaration( |
| 925 | die: decl_die, language: SymbolFileDWARF::GetLanguage(unit&: *decl_die.GetCU()), |
| 926 | unique_typename, decl_declaration); |
| 927 | if (UniqueDWARFASTType *unique_ast_entry_type = |
| 928 | dwarf->GetUniqueDWARFASTTypeMap().Find( |
| 929 | name: unique_typename, die: decl_die, decl: decl_declaration, |
| 930 | byte_size: decl_attrs.byte_size.value_or(u: 0), |
| 931 | is_forward_declaration: decl_attrs.is_forward_declaration)) { |
| 932 | unique_ast_entry_type->UpdateToDefDIE(def_die, declaration&: def_attrs.decl, |
| 933 | byte_size: def_attrs.byte_size.value_or(u: 0)); |
| 934 | } else if (Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups)) { |
| 935 | const dw_tag_t tag = decl_die.Tag(); |
| 936 | LLDB_LOG(log, |
| 937 | "Failed to find {0:x16} {1} ({2}) type \"{3}\" in " |
| 938 | "UniqueDWARFASTTypeMap" , |
| 939 | decl_die.GetID(), DW_TAG_value_to_name(tag), tag, unique_typename); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc, |
| 944 | const DWARFDIE &decl_die, |
| 945 | ParsedDWARFTypeAttributes &attrs) { |
| 946 | Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups); |
| 947 | SymbolFileDWARF *dwarf = decl_die.GetDWARF(); |
| 948 | const dw_tag_t tag = decl_die.Tag(); |
| 949 | |
| 950 | DWARFDIE def_die; |
| 951 | if (attrs.is_forward_declaration) { |
| 952 | if (TypeSP type_sp = ParseTypeFromClangModule(sc, die: decl_die, log)) |
| 953 | return type_sp; |
| 954 | |
| 955 | def_die = dwarf->FindDefinitionDIE(die: decl_die); |
| 956 | |
| 957 | if (!def_die) { |
| 958 | SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile(); |
| 959 | if (debug_map_symfile) { |
| 960 | // We weren't able to find a full declaration in this DWARF, |
| 961 | // see if we have a declaration anywhere else... |
| 962 | def_die = debug_map_symfile->FindDefinitionDIE(die: decl_die); |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | if (log) { |
| 967 | dwarf->GetObjectFile()->GetModule()->LogMessage( |
| 968 | log, |
| 969 | format: "SymbolFileDWARF({0:p}) - {1:x16}}: {2} ({3}) type \"{4}\" is a " |
| 970 | "forward declaration, complete DIE is {5}" , |
| 971 | args: static_cast<void *>(this), args: decl_die.GetID(), args: DW_TAG_value_to_name(tag), |
| 972 | args: tag, args: attrs.name.GetCString(), |
| 973 | args: def_die ? llvm::utohexstr(X: def_die.GetID()) : "not found" ); |
| 974 | } |
| 975 | } |
| 976 | if (def_die) { |
| 977 | if (auto [it, inserted] = dwarf->GetDIEToType().try_emplace( |
| 978 | Key: def_die.GetDIE(), DIE_IS_BEING_PARSED); |
| 979 | !inserted) { |
| 980 | if (it->getSecond() == nullptr || it->getSecond() == DIE_IS_BEING_PARSED) |
| 981 | return nullptr; |
| 982 | return it->getSecond()->shared_from_this(); |
| 983 | } |
| 984 | attrs = ParsedDWARFTypeAttributes(def_die); |
| 985 | } else { |
| 986 | // No definition found. Proceed with the declaration die. We can use it to |
| 987 | // create a forward-declared type. |
| 988 | def_die = decl_die; |
| 989 | } |
| 990 | |
| 991 | CompilerType enumerator_clang_type; |
| 992 | if (attrs.type.IsValid()) { |
| 993 | Type *enumerator_type = |
| 994 | dwarf->ResolveTypeUID(die: attrs.type.Reference(), assert_not_being_parsed: true); |
| 995 | if (enumerator_type) |
| 996 | enumerator_clang_type = enumerator_type->GetFullCompilerType(); |
| 997 | } |
| 998 | |
| 999 | if (!enumerator_clang_type) { |
| 1000 | if (attrs.byte_size) { |
| 1001 | enumerator_clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize( |
| 1002 | type_name: "" , dw_ate: DW_ATE_signed, bit_size: *attrs.byte_size * 8); |
| 1003 | } else { |
| 1004 | enumerator_clang_type = m_ast.GetBasicType(type: eBasicTypeInt); |
| 1005 | } |
| 1006 | } |
| 1007 | |
| 1008 | CompilerType clang_type = m_ast.CreateEnumerationType( |
| 1009 | attrs.name.GetStringRef(), |
| 1010 | GetClangDeclContextContainingDIE(die: def_die, decl_ctx_die: nullptr), |
| 1011 | GetOwningClangModule(die: def_die), attrs.decl, enumerator_clang_type, |
| 1012 | attrs.is_scoped_enum, attrs.enum_kind); |
| 1013 | TypeSP type_sp = |
| 1014 | dwarf->MakeType(uid: def_die.GetID(), name: attrs.name, byte_size: attrs.byte_size, context: nullptr, |
| 1015 | encoding_uid: attrs.type.Reference().GetID(), encoding_uid_type: Type::eEncodingIsUID, |
| 1016 | decl: &attrs.decl, compiler_qual_type: clang_type, compiler_type_resolve_state: Type::ResolveState::Forward, |
| 1017 | opaque_payload: TypePayloadClang(GetOwningClangModule(die: def_die))); |
| 1018 | |
| 1019 | clang::DeclContext *type_decl_ctx = |
| 1020 | TypeSystemClang::GetDeclContextForType(type: clang_type); |
| 1021 | LinkDeclContextToDIE(decl_ctx: type_decl_ctx, die: decl_die); |
| 1022 | if (decl_die != def_die) { |
| 1023 | LinkDeclContextToDIE(decl_ctx: type_decl_ctx, die: def_die); |
| 1024 | dwarf->GetDIEToType()[def_die.GetDIE()] = type_sp.get(); |
| 1025 | // Declaration DIE is inserted into the type map in ParseTypeFromDWARF |
| 1026 | } |
| 1027 | |
| 1028 | if (!CompleteEnumType(die: def_die, type: type_sp.get(), clang_type)) { |
| 1029 | dwarf->GetObjectFile()->GetModule()->ReportError( |
| 1030 | format: "DWARF DIE at {0:x16} named \"{1}\" was not able to start its " |
| 1031 | "definition.\nPlease file a bug and attach the file at the " |
| 1032 | "start of this error message" , |
| 1033 | args: def_die.GetOffset(), args: attrs.name.GetCString()); |
| 1034 | } |
| 1035 | return type_sp; |
| 1036 | } |
| 1037 | |
| 1038 | static clang::CallingConv |
| 1039 | ConvertDWARFCallingConventionToClang(const ParsedDWARFTypeAttributes &attrs) { |
| 1040 | switch (attrs.calling_convention) { |
| 1041 | case llvm::dwarf::DW_CC_normal: |
| 1042 | return clang::CC_C; |
| 1043 | case llvm::dwarf::DW_CC_BORLAND_stdcall: |
| 1044 | return clang::CC_X86StdCall; |
| 1045 | case llvm::dwarf::DW_CC_BORLAND_msfastcall: |
| 1046 | return clang::CC_X86FastCall; |
| 1047 | case llvm::dwarf::DW_CC_LLVM_vectorcall: |
| 1048 | return clang::CC_X86VectorCall; |
| 1049 | case llvm::dwarf::DW_CC_BORLAND_pascal: |
| 1050 | return clang::CC_X86Pascal; |
| 1051 | case llvm::dwarf::DW_CC_LLVM_Win64: |
| 1052 | return clang::CC_Win64; |
| 1053 | case llvm::dwarf::DW_CC_LLVM_X86_64SysV: |
| 1054 | return clang::CC_X86_64SysV; |
| 1055 | case llvm::dwarf::DW_CC_LLVM_X86RegCall: |
| 1056 | return clang::CC_X86RegCall; |
| 1057 | default: |
| 1058 | break; |
| 1059 | } |
| 1060 | |
| 1061 | Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups); |
| 1062 | LLDB_LOG(log, "Unsupported DW_AT_calling_convention value: {0}" , |
| 1063 | attrs.calling_convention); |
| 1064 | // Use the default calling convention as a fallback. |
| 1065 | return clang::CC_C; |
| 1066 | } |
| 1067 | |
| 1068 | bool DWARFASTParserClang::ParseObjCMethod( |
| 1069 | const ObjCLanguage::ObjCMethodName &objc_method, const DWARFDIE &die, |
| 1070 | CompilerType clang_type, const ParsedDWARFTypeAttributes &attrs, |
| 1071 | bool is_variadic) { |
| 1072 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 1073 | assert(dwarf); |
| 1074 | |
| 1075 | const auto tag = die.Tag(); |
| 1076 | ConstString class_name(objc_method.GetClassName()); |
| 1077 | if (!class_name) |
| 1078 | return false; |
| 1079 | |
| 1080 | TypeSP complete_objc_class_type_sp = |
| 1081 | dwarf->FindCompleteObjCDefinitionTypeForDIE(die: DWARFDIE(), type_name: class_name, |
| 1082 | must_be_implementation: false); |
| 1083 | |
| 1084 | if (!complete_objc_class_type_sp) |
| 1085 | return false; |
| 1086 | |
| 1087 | CompilerType type_clang_forward_type = |
| 1088 | complete_objc_class_type_sp->GetForwardCompilerType(); |
| 1089 | |
| 1090 | if (!type_clang_forward_type) |
| 1091 | return false; |
| 1092 | |
| 1093 | if (!TypeSystemClang::IsObjCObjectOrInterfaceType(type: type_clang_forward_type)) |
| 1094 | return false; |
| 1095 | |
| 1096 | clang::ObjCMethodDecl *objc_method_decl = m_ast.AddMethodToObjCObjectType( |
| 1097 | type: type_clang_forward_type, name: attrs.name.GetCString(), method_compiler_type: clang_type, |
| 1098 | is_artificial: attrs.is_artificial, is_variadic, is_objc_direct_call: attrs.is_objc_direct_call); |
| 1099 | |
| 1100 | if (!objc_method_decl) { |
| 1101 | dwarf->GetObjectFile()->GetModule()->ReportError( |
| 1102 | format: "[{0:x16}]: invalid Objective-C method {1:x4} ({2}), " |
| 1103 | "please file a bug and attach the file at the start of " |
| 1104 | "this error message" , |
| 1105 | args: die.GetOffset(), args: tag, args: DW_TAG_value_to_name(tag)); |
| 1106 | return false; |
| 1107 | } |
| 1108 | |
| 1109 | LinkDeclContextToDIE(objc_method_decl, die); |
| 1110 | m_ast.SetMetadataAsUserID(objc_method_decl, die.GetID()); |
| 1111 | |
| 1112 | return true; |
| 1113 | } |
| 1114 | |
| 1115 | std::pair<bool, TypeSP> DWARFASTParserClang::ParseCXXMethod( |
| 1116 | const DWARFDIE &die, CompilerType clang_type, |
| 1117 | const ParsedDWARFTypeAttributes &attrs, const DWARFDIE &decl_ctx_die, |
| 1118 | bool is_static, bool &ignore_containing_context) { |
| 1119 | Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups); |
| 1120 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 1121 | assert(dwarf); |
| 1122 | |
| 1123 | Type *class_type = dwarf->ResolveType(die: decl_ctx_die); |
| 1124 | if (!class_type) |
| 1125 | return {}; |
| 1126 | |
| 1127 | if (class_type->GetID() != decl_ctx_die.GetID() || |
| 1128 | IsClangModuleFwdDecl(Die: decl_ctx_die)) { |
| 1129 | |
| 1130 | // We uniqued the parent class of this function to another |
| 1131 | // class so we now need to associate all dies under |
| 1132 | // "decl_ctx_die" to DIEs in the DIE for "class_type"... |
| 1133 | if (DWARFDIE class_type_die = dwarf->GetDIE(uid: class_type->GetID())) { |
| 1134 | std::vector<DWARFDIE> failures; |
| 1135 | |
| 1136 | CopyUniqueClassMethodTypes(src_class_die: decl_ctx_die, dst_class_die: class_type_die, class_type, |
| 1137 | failures); |
| 1138 | |
| 1139 | // FIXME do something with these failures that's |
| 1140 | // smarter than just dropping them on the ground. |
| 1141 | // Unfortunately classes don't like having stuff added |
| 1142 | // to them after their definitions are complete... |
| 1143 | |
| 1144 | Type *type_ptr = dwarf->GetDIEToType().lookup(Val: die.GetDIE()); |
| 1145 | if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) |
| 1146 | return {true, type_ptr->shared_from_this()}; |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | if (attrs.specification.IsValid()) { |
| 1151 | // We have a specification which we are going to base our |
| 1152 | // function prototype off of, so we need this type to be |
| 1153 | // completed so that the m_die_to_decl_ctx for the method in |
| 1154 | // the specification has a valid clang decl context. |
| 1155 | class_type->GetForwardCompilerType(); |
| 1156 | // If we have a specification, then the function type should |
| 1157 | // have been made with the specification and not with this |
| 1158 | // die. |
| 1159 | DWARFDIE spec_die = attrs.specification.Reference(); |
| 1160 | clang::DeclContext *spec_clang_decl_ctx = |
| 1161 | GetClangDeclContextForDIE(die: spec_die); |
| 1162 | if (spec_clang_decl_ctx) |
| 1163 | LinkDeclContextToDIE(decl_ctx: spec_clang_decl_ctx, die); |
| 1164 | else |
| 1165 | dwarf->GetObjectFile()->GetModule()->ReportWarning( |
| 1166 | format: "{0:x8}: DW_AT_specification({1:x16}" |
| 1167 | ") has no decl\n" , |
| 1168 | args: die.GetID(), args: spec_die.GetOffset()); |
| 1169 | |
| 1170 | return {true, nullptr}; |
| 1171 | } |
| 1172 | |
| 1173 | if (attrs.abstract_origin.IsValid()) { |
| 1174 | // We have a specification which we are going to base our |
| 1175 | // function prototype off of, so we need this type to be |
| 1176 | // completed so that the m_die_to_decl_ctx for the method in |
| 1177 | // the abstract origin has a valid clang decl context. |
| 1178 | class_type->GetForwardCompilerType(); |
| 1179 | |
| 1180 | DWARFDIE abs_die = attrs.abstract_origin.Reference(); |
| 1181 | clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE(die: abs_die); |
| 1182 | if (abs_clang_decl_ctx) |
| 1183 | LinkDeclContextToDIE(decl_ctx: abs_clang_decl_ctx, die); |
| 1184 | else |
| 1185 | dwarf->GetObjectFile()->GetModule()->ReportWarning( |
| 1186 | format: "{0:x8}: DW_AT_abstract_origin({1:x16}" |
| 1187 | ") has no decl\n" , |
| 1188 | args: die.GetID(), args: abs_die.GetOffset()); |
| 1189 | |
| 1190 | return {true, nullptr}; |
| 1191 | } |
| 1192 | |
| 1193 | CompilerType class_opaque_type = class_type->GetForwardCompilerType(); |
| 1194 | if (!TypeSystemClang::IsCXXClassType(type: class_opaque_type)) |
| 1195 | return {}; |
| 1196 | |
| 1197 | PrepareContextToReceiveMembers( |
| 1198 | ast&: m_ast, ast_importer&: GetClangASTImporter(), |
| 1199 | decl_ctx: TypeSystemClang::GetDeclContextForType(type: class_opaque_type), die, |
| 1200 | type_name_cstr: attrs.name.GetCString()); |
| 1201 | |
| 1202 | // We have a C++ member function with no children (this pointer!) and clang |
| 1203 | // will get mad if we try and make a function that isn't well formed in the |
| 1204 | // DWARF, so we will just skip it... |
| 1205 | if (!is_static && !die.HasChildren()) |
| 1206 | return {true, nullptr}; |
| 1207 | |
| 1208 | const bool is_attr_used = false; |
| 1209 | // Neither GCC 4.2 nor clang++ currently set a valid |
| 1210 | // accessibility in the DWARF for C++ methods... |
| 1211 | // Default to public for now... |
| 1212 | const auto accessibility = |
| 1213 | attrs.accessibility == eAccessNone ? eAccessPublic : attrs.accessibility; |
| 1214 | |
| 1215 | clang::CXXMethodDecl *cxx_method_decl = m_ast.AddMethodToCXXRecordType( |
| 1216 | type: class_opaque_type.GetOpaqueQualType(), name: attrs.name.GetCString(), |
| 1217 | mangled_name: attrs.mangled_name, method_type: clang_type, access: accessibility, is_virtual: attrs.is_virtual, |
| 1218 | is_static, is_inline: attrs.is_inline, is_explicit: attrs.is_explicit, is_attr_used, |
| 1219 | is_artificial: attrs.is_artificial); |
| 1220 | |
| 1221 | if (cxx_method_decl) { |
| 1222 | LinkDeclContextToDIE(cxx_method_decl, die); |
| 1223 | |
| 1224 | ClangASTMetadata metadata; |
| 1225 | metadata.SetUserID(die.GetID()); |
| 1226 | |
| 1227 | char const *object_pointer_name = |
| 1228 | attrs.object_pointer ? attrs.object_pointer.GetName() : nullptr; |
| 1229 | if (object_pointer_name) { |
| 1230 | metadata.SetObjectPtrName(object_pointer_name); |
| 1231 | LLDB_LOGF(log, "Setting object pointer name: %s on method object %p.\n" , |
| 1232 | object_pointer_name, static_cast<void *>(cxx_method_decl)); |
| 1233 | } |
| 1234 | m_ast.SetMetadata(cxx_method_decl, metadata); |
| 1235 | } else { |
| 1236 | ignore_containing_context = true; |
| 1237 | } |
| 1238 | |
| 1239 | // Artificial methods are always handled even when we |
| 1240 | // don't create a new declaration for them. |
| 1241 | const bool type_handled = cxx_method_decl != nullptr || attrs.is_artificial; |
| 1242 | |
| 1243 | return {type_handled, nullptr}; |
| 1244 | } |
| 1245 | |
| 1246 | TypeSP |
| 1247 | DWARFASTParserClang::ParseSubroutine(const DWARFDIE &die, |
| 1248 | const ParsedDWARFTypeAttributes &attrs) { |
| 1249 | Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups); |
| 1250 | |
| 1251 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 1252 | const dw_tag_t tag = die.Tag(); |
| 1253 | |
| 1254 | bool is_variadic = false; |
| 1255 | bool has_template_params = false; |
| 1256 | |
| 1257 | DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n" , die.GetID(), |
| 1258 | DW_TAG_value_to_name(tag), type_name_cstr); |
| 1259 | |
| 1260 | CompilerType return_clang_type; |
| 1261 | Type *func_type = nullptr; |
| 1262 | |
| 1263 | if (attrs.type.IsValid()) |
| 1264 | func_type = dwarf->ResolveTypeUID(die: attrs.type.Reference(), assert_not_being_parsed: true); |
| 1265 | |
| 1266 | if (func_type) |
| 1267 | return_clang_type = func_type->GetForwardCompilerType(); |
| 1268 | else |
| 1269 | return_clang_type = m_ast.GetBasicType(type: eBasicTypeVoid); |
| 1270 | |
| 1271 | std::vector<CompilerType> function_param_types; |
| 1272 | llvm::SmallVector<llvm::StringRef> function_param_names; |
| 1273 | |
| 1274 | // Parse the function children for the parameters |
| 1275 | |
| 1276 | DWARFDIE decl_ctx_die; |
| 1277 | clang::DeclContext *containing_decl_ctx = |
| 1278 | GetClangDeclContextContainingDIE(die, decl_ctx_die: &decl_ctx_die); |
| 1279 | assert(containing_decl_ctx); |
| 1280 | |
| 1281 | if (die.HasChildren()) { |
| 1282 | ParseChildParameters(containing_decl_ctx, parent_die: die, is_variadic, |
| 1283 | has_template_params, function_param_types, |
| 1284 | function_param_names); |
| 1285 | } |
| 1286 | |
| 1287 | bool is_cxx_method = DeclKindIsCXXClass(decl_kind: containing_decl_ctx->getDeclKind()); |
| 1288 | bool ignore_containing_context = false; |
| 1289 | // Check for templatized class member functions. If we had any |
| 1290 | // DW_TAG_template_type_parameter or DW_TAG_template_value_parameter |
| 1291 | // the DW_TAG_subprogram DIE, then we can't let this become a method in |
| 1292 | // a class. Why? Because templatized functions are only emitted if one |
| 1293 | // of the templatized methods is used in the current compile unit and |
| 1294 | // we will end up with classes that may or may not include these member |
| 1295 | // functions and this means one class won't match another class |
| 1296 | // definition and it affects our ability to use a class in the clang |
| 1297 | // expression parser. So for the greater good, we currently must not |
| 1298 | // allow any template member functions in a class definition. |
| 1299 | if (is_cxx_method && has_template_params) { |
| 1300 | ignore_containing_context = true; |
| 1301 | is_cxx_method = false; |
| 1302 | } |
| 1303 | |
| 1304 | clang::CallingConv calling_convention = |
| 1305 | ConvertDWARFCallingConventionToClang(attrs); |
| 1306 | |
| 1307 | const DWARFDIE object_parameter = |
| 1308 | GetCXXObjectParameter(subprogram: die, containing_decl_ctx: *containing_decl_ctx); |
| 1309 | |
| 1310 | // clang_type will get the function prototype clang type after this |
| 1311 | // call |
| 1312 | CompilerType clang_type = m_ast.CreateFunctionType( |
| 1313 | result_type: return_clang_type, args: function_param_types, is_variadic, |
| 1314 | type_quals: GetCXXMethodCVQuals(subprogram: die, object_parameter), cc: calling_convention, |
| 1315 | ref_qual: attrs.ref_qual); |
| 1316 | |
| 1317 | if (attrs.name) { |
| 1318 | bool type_handled = false; |
| 1319 | if (tag == DW_TAG_subprogram || tag == DW_TAG_inlined_subroutine) { |
| 1320 | if (std::optional<const ObjCLanguage::ObjCMethodName> objc_method = |
| 1321 | ObjCLanguage::ObjCMethodName::Create(name: attrs.name.GetStringRef(), |
| 1322 | strict: true)) { |
| 1323 | type_handled = |
| 1324 | ParseObjCMethod(objc_method: *objc_method, die, clang_type, attrs, is_variadic); |
| 1325 | } else if (is_cxx_method) { |
| 1326 | // In DWARF, a C++ method is static if it has no object parameter child. |
| 1327 | const bool is_static = !object_parameter.IsValid(); |
| 1328 | auto [handled, type_sp] = |
| 1329 | ParseCXXMethod(die, clang_type, attrs, decl_ctx_die, is_static, |
| 1330 | ignore_containing_context); |
| 1331 | if (type_sp) |
| 1332 | return type_sp; |
| 1333 | |
| 1334 | type_handled = handled; |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | if (!type_handled) { |
| 1339 | clang::FunctionDecl *function_decl = nullptr; |
| 1340 | clang::FunctionDecl *template_function_decl = nullptr; |
| 1341 | |
| 1342 | if (attrs.abstract_origin.IsValid()) { |
| 1343 | DWARFDIE abs_die = attrs.abstract_origin.Reference(); |
| 1344 | |
| 1345 | if (dwarf->ResolveType(die: abs_die)) { |
| 1346 | function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>( |
| 1347 | Val: GetCachedClangDeclContextForDIE(die: abs_die)); |
| 1348 | |
| 1349 | if (function_decl) { |
| 1350 | LinkDeclContextToDIE(function_decl, die); |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | if (!function_decl) { |
| 1356 | char *name_buf = nullptr; |
| 1357 | llvm::StringRef name = attrs.name.GetStringRef(); |
| 1358 | |
| 1359 | // We currently generate function templates with template parameters in |
| 1360 | // their name. In order to get closer to the AST that clang generates |
| 1361 | // we want to strip these from the name when creating the AST. |
| 1362 | if (attrs.mangled_name) { |
| 1363 | llvm::ItaniumPartialDemangler D; |
| 1364 | if (!D.partialDemangle(MangledName: attrs.mangled_name)) { |
| 1365 | name_buf = D.getFunctionBaseName(Buf: nullptr, N: nullptr); |
| 1366 | name = name_buf; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | // We just have a function that isn't part of a class |
| 1371 | function_decl = m_ast.CreateFunctionDeclaration( |
| 1372 | decl_ctx: ignore_containing_context ? m_ast.GetTranslationUnitDecl() |
| 1373 | : containing_decl_ctx, |
| 1374 | owning_module: GetOwningClangModule(die), name, function_Type: clang_type, storage: attrs.storage, |
| 1375 | is_inline: attrs.is_inline); |
| 1376 | std::free(ptr: name_buf); |
| 1377 | |
| 1378 | if (has_template_params) { |
| 1379 | TypeSystemClang::TemplateParameterInfos template_param_infos; |
| 1380 | ParseTemplateParameterInfos(parent_die: die, template_param_infos); |
| 1381 | template_function_decl = m_ast.CreateFunctionDeclaration( |
| 1382 | decl_ctx: ignore_containing_context ? m_ast.GetTranslationUnitDecl() |
| 1383 | : containing_decl_ctx, |
| 1384 | owning_module: GetOwningClangModule(die), name: attrs.name.GetStringRef(), function_Type: clang_type, |
| 1385 | storage: attrs.storage, is_inline: attrs.is_inline); |
| 1386 | clang::FunctionTemplateDecl *func_template_decl = |
| 1387 | m_ast.CreateFunctionTemplateDecl( |
| 1388 | decl_ctx: containing_decl_ctx, owning_module: GetOwningClangModule(die), |
| 1389 | func_decl: template_function_decl, infos: template_param_infos); |
| 1390 | m_ast.CreateFunctionTemplateSpecializationInfo( |
| 1391 | func_decl: template_function_decl, Template: func_template_decl, infos: template_param_infos); |
| 1392 | } |
| 1393 | |
| 1394 | lldbassert(function_decl); |
| 1395 | |
| 1396 | if (function_decl) { |
| 1397 | // Attach an asm(<mangled_name>) label to the FunctionDecl. |
| 1398 | // This ensures that clang::CodeGen emits function calls |
| 1399 | // using symbols that are mangled according to the DW_AT_linkage_name. |
| 1400 | // If we didn't do this, the external symbols wouldn't exactly |
| 1401 | // match the mangled name LLDB knows about and the IRExecutionUnit |
| 1402 | // would have to fall back to searching object files for |
| 1403 | // approximately matching function names. The motivating |
| 1404 | // example is generating calls to ABI-tagged template functions. |
| 1405 | // This is done separately for member functions in |
| 1406 | // AddMethodToCXXRecordType. |
| 1407 | if (attrs.mangled_name) |
| 1408 | function_decl->addAttr(clang::AsmLabelAttr::CreateImplicit( |
| 1409 | m_ast.getASTContext(), attrs.mangled_name, /*literal=*/false)); |
| 1410 | |
| 1411 | LinkDeclContextToDIE(function_decl, die); |
| 1412 | |
| 1413 | const clang::FunctionProtoType *function_prototype( |
| 1414 | llvm::cast<clang::FunctionProtoType>( |
| 1415 | Val: ClangUtil::GetQualType(ct: clang_type).getTypePtr())); |
| 1416 | const auto params = m_ast.CreateParameterDeclarations( |
| 1417 | context: function_decl, prototype: *function_prototype, param_names: function_param_names); |
| 1418 | function_decl->setParams(params); |
| 1419 | if (template_function_decl) |
| 1420 | template_function_decl->setParams(params); |
| 1421 | |
| 1422 | ClangASTMetadata metadata; |
| 1423 | metadata.SetUserID(die.GetID()); |
| 1424 | |
| 1425 | char const *object_pointer_name = |
| 1426 | attrs.object_pointer ? attrs.object_pointer.GetName() : nullptr; |
| 1427 | if (object_pointer_name) { |
| 1428 | metadata.SetObjectPtrName(object_pointer_name); |
| 1429 | LLDB_LOGF(log, |
| 1430 | "Setting object pointer name: %s on function " |
| 1431 | "object %p." , |
| 1432 | object_pointer_name, static_cast<void *>(function_decl)); |
| 1433 | } |
| 1434 | m_ast.SetMetadata(function_decl, metadata); |
| 1435 | } |
| 1436 | } |
| 1437 | } |
| 1438 | } |
| 1439 | return dwarf->MakeType( |
| 1440 | uid: die.GetID(), name: attrs.name, byte_size: std::nullopt, context: nullptr, LLDB_INVALID_UID, |
| 1441 | encoding_uid_type: Type::eEncodingIsUID, decl: &attrs.decl, compiler_qual_type: clang_type, compiler_type_resolve_state: Type::ResolveState::Full); |
| 1442 | } |
| 1443 | |
| 1444 | TypeSP |
| 1445 | DWARFASTParserClang::ParseArrayType(const DWARFDIE &die, |
| 1446 | const ParsedDWARFTypeAttributes &attrs) { |
| 1447 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 1448 | |
| 1449 | DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n" , die.GetID(), |
| 1450 | DW_TAG_value_to_name(tag), type_name_cstr); |
| 1451 | |
| 1452 | DWARFDIE type_die = attrs.type.Reference(); |
| 1453 | Type *element_type = dwarf->ResolveTypeUID(die: type_die, assert_not_being_parsed: true); |
| 1454 | |
| 1455 | if (!element_type) |
| 1456 | return nullptr; |
| 1457 | |
| 1458 | std::optional<SymbolFile::ArrayInfo> array_info = ParseChildArrayInfo(parent_die: die); |
| 1459 | uint32_t byte_stride = attrs.byte_stride; |
| 1460 | uint32_t bit_stride = attrs.bit_stride; |
| 1461 | if (array_info) { |
| 1462 | byte_stride = array_info->byte_stride; |
| 1463 | bit_stride = array_info->bit_stride; |
| 1464 | } |
| 1465 | if (byte_stride == 0 && bit_stride == 0) |
| 1466 | byte_stride = llvm::expectedToOptional(E: element_type->GetByteSize(exe_scope: nullptr)) |
| 1467 | .value_or(u: 0); |
| 1468 | CompilerType array_element_type = element_type->GetForwardCompilerType(); |
| 1469 | TypeSystemClang::RequireCompleteType(type: array_element_type); |
| 1470 | |
| 1471 | uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride; |
| 1472 | CompilerType clang_type; |
| 1473 | if (array_info && array_info->element_orders.size() > 0) { |
| 1474 | auto end = array_info->element_orders.rend(); |
| 1475 | for (auto pos = array_info->element_orders.rbegin(); pos != end; ++pos) { |
| 1476 | clang_type = m_ast.CreateArrayType( |
| 1477 | element_type: array_element_type, /*element_count=*/*pos, is_vector: attrs.is_vector); |
| 1478 | |
| 1479 | uint64_t num_elements = pos->value_or(u: 0); |
| 1480 | array_element_type = clang_type; |
| 1481 | array_element_bit_stride = num_elements |
| 1482 | ? array_element_bit_stride * num_elements |
| 1483 | : array_element_bit_stride; |
| 1484 | } |
| 1485 | } else { |
| 1486 | clang_type = m_ast.CreateArrayType( |
| 1487 | element_type: array_element_type, /*element_count=*/std::nullopt, is_vector: attrs.is_vector); |
| 1488 | } |
| 1489 | ConstString empty_name; |
| 1490 | TypeSP type_sp = |
| 1491 | dwarf->MakeType(uid: die.GetID(), name: empty_name, byte_size: array_element_bit_stride / 8, |
| 1492 | context: nullptr, encoding_uid: type_die.GetID(), encoding_uid_type: Type::eEncodingIsUID, |
| 1493 | decl: &attrs.decl, compiler_qual_type: clang_type, compiler_type_resolve_state: Type::ResolveState::Full); |
| 1494 | type_sp->SetEncodingType(element_type); |
| 1495 | const clang::Type *type = ClangUtil::GetQualType(ct: clang_type).getTypePtr(); |
| 1496 | m_ast.SetMetadataAsUserID(type, user_id: die.GetID()); |
| 1497 | return type_sp; |
| 1498 | } |
| 1499 | |
| 1500 | TypeSP DWARFASTParserClang::ParsePointerToMemberType( |
| 1501 | const DWARFDIE &die, const ParsedDWARFTypeAttributes &attrs) { |
| 1502 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 1503 | Type *pointee_type = dwarf->ResolveTypeUID(die: attrs.type.Reference(), assert_not_being_parsed: true); |
| 1504 | Type *class_type = |
| 1505 | dwarf->ResolveTypeUID(die: attrs.containing_type.Reference(), assert_not_being_parsed: true); |
| 1506 | |
| 1507 | // Check to make sure pointers are not NULL before attempting to |
| 1508 | // dereference them. |
| 1509 | if ((class_type == nullptr) || (pointee_type == nullptr)) |
| 1510 | return nullptr; |
| 1511 | |
| 1512 | CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType(); |
| 1513 | CompilerType class_clang_type = class_type->GetForwardCompilerType(); |
| 1514 | |
| 1515 | CompilerType clang_type = TypeSystemClang::CreateMemberPointerType( |
| 1516 | type: class_clang_type, pointee_type: pointee_clang_type); |
| 1517 | |
| 1518 | if (std::optional<uint64_t> clang_type_size = |
| 1519 | llvm::expectedToOptional(E: clang_type.GetByteSize(exe_scope: nullptr))) { |
| 1520 | return dwarf->MakeType(uid: die.GetID(), name: attrs.name, byte_size: *clang_type_size, context: nullptr, |
| 1521 | LLDB_INVALID_UID, encoding_uid_type: Type::eEncodingIsUID, decl: nullptr, |
| 1522 | compiler_qual_type: clang_type, compiler_type_resolve_state: Type::ResolveState::Forward); |
| 1523 | } |
| 1524 | return nullptr; |
| 1525 | } |
| 1526 | |
| 1527 | void DWARFASTParserClang::ParseInheritance( |
| 1528 | const DWARFDIE &die, const DWARFDIE &parent_die, |
| 1529 | const CompilerType class_clang_type, const AccessType default_accessibility, |
| 1530 | const lldb::ModuleSP &module_sp, |
| 1531 | std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes, |
| 1532 | ClangASTImporter::LayoutInfo &layout_info) { |
| 1533 | auto ast = class_clang_type.GetTypeSystem<TypeSystemClang>(); |
| 1534 | if (ast == nullptr) |
| 1535 | return; |
| 1536 | |
| 1537 | // TODO: implement DW_TAG_inheritance type parsing. |
| 1538 | DWARFAttributes attributes = die.GetAttributes(); |
| 1539 | if (attributes.Size() == 0) |
| 1540 | return; |
| 1541 | |
| 1542 | DWARFFormValue encoding_form; |
| 1543 | AccessType accessibility = default_accessibility; |
| 1544 | bool is_virtual = false; |
| 1545 | bool is_base_of_class = true; |
| 1546 | off_t member_byte_offset = 0; |
| 1547 | |
| 1548 | for (uint32_t i = 0; i < attributes.Size(); ++i) { |
| 1549 | const dw_attr_t attr = attributes.AttributeAtIndex(i); |
| 1550 | DWARFFormValue form_value; |
| 1551 | if (attributes.ExtractFormValueAtIndex(i, form_value)) { |
| 1552 | switch (attr) { |
| 1553 | case DW_AT_type: |
| 1554 | encoding_form = form_value; |
| 1555 | break; |
| 1556 | case DW_AT_data_member_location: |
| 1557 | if (auto maybe_offset = |
| 1558 | ExtractDataMemberLocation(die, form_value, module_sp)) |
| 1559 | member_byte_offset = *maybe_offset; |
| 1560 | break; |
| 1561 | |
| 1562 | case DW_AT_accessibility: |
| 1563 | accessibility = |
| 1564 | DWARFASTParser::GetAccessTypeFromDWARF(dwarf_accessibility: form_value.Unsigned()); |
| 1565 | break; |
| 1566 | |
| 1567 | case DW_AT_virtuality: |
| 1568 | is_virtual = form_value.Boolean(); |
| 1569 | break; |
| 1570 | |
| 1571 | default: |
| 1572 | break; |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | Type *base_class_type = die.ResolveTypeUID(die: encoding_form.Reference()); |
| 1578 | if (base_class_type == nullptr) { |
| 1579 | module_sp->ReportError(format: "{0:x16}: DW_TAG_inheritance failed to " |
| 1580 | "resolve the base class at {1:x16}" |
| 1581 | " from enclosing type {2:x16}. \nPlease file " |
| 1582 | "a bug and attach the file at the start of " |
| 1583 | "this error message" , |
| 1584 | args: die.GetOffset(), |
| 1585 | args: encoding_form.Reference().GetOffset(), |
| 1586 | args: parent_die.GetOffset()); |
| 1587 | return; |
| 1588 | } |
| 1589 | |
| 1590 | CompilerType base_class_clang_type = base_class_type->GetFullCompilerType(); |
| 1591 | assert(base_class_clang_type); |
| 1592 | if (TypeSystemClang::IsObjCObjectOrInterfaceType(type: class_clang_type)) { |
| 1593 | ast->SetObjCSuperClass(type: class_clang_type, superclass_compiler_type: base_class_clang_type); |
| 1594 | return; |
| 1595 | } |
| 1596 | std::unique_ptr<clang::CXXBaseSpecifier> result = |
| 1597 | ast->CreateBaseClassSpecifier(type: base_class_clang_type.GetOpaqueQualType(), |
| 1598 | access: accessibility, is_virtual, |
| 1599 | base_of_class: is_base_of_class); |
| 1600 | if (!result) |
| 1601 | return; |
| 1602 | |
| 1603 | base_classes.push_back(x: std::move(result)); |
| 1604 | |
| 1605 | if (is_virtual) { |
| 1606 | // Do not specify any offset for virtual inheritance. The DWARF |
| 1607 | // produced by clang doesn't give us a constant offset, but gives |
| 1608 | // us a DWARF expressions that requires an actual object in memory. |
| 1609 | // the DW_AT_data_member_location for a virtual base class looks |
| 1610 | // like: |
| 1611 | // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, |
| 1612 | // DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, |
| 1613 | // DW_OP_plus ) |
| 1614 | // Given this, there is really no valid response we can give to |
| 1615 | // clang for virtual base class offsets, and this should eventually |
| 1616 | // be removed from LayoutRecordType() in the external |
| 1617 | // AST source in clang. |
| 1618 | } else { |
| 1619 | layout_info.base_offsets.insert(KV: std::make_pair( |
| 1620 | x: ast->GetAsCXXRecordDecl(type: base_class_clang_type.GetOpaqueQualType()), |
| 1621 | y: clang::CharUnits::fromQuantity(Quantity: member_byte_offset))); |
| 1622 | } |
| 1623 | } |
| 1624 | |
| 1625 | TypeSP DWARFASTParserClang::UpdateSymbolContextScopeForType( |
| 1626 | const SymbolContext &sc, const DWARFDIE &die, TypeSP type_sp) { |
| 1627 | if (!type_sp) |
| 1628 | return type_sp; |
| 1629 | |
| 1630 | DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die); |
| 1631 | dw_tag_t sc_parent_tag = sc_parent_die.Tag(); |
| 1632 | |
| 1633 | SymbolContextScope *symbol_context_scope = nullptr; |
| 1634 | if (sc_parent_tag == DW_TAG_compile_unit || |
| 1635 | sc_parent_tag == DW_TAG_partial_unit) { |
| 1636 | symbol_context_scope = sc.comp_unit; |
| 1637 | } else if (sc.function != nullptr && sc_parent_die) { |
| 1638 | symbol_context_scope = |
| 1639 | sc.function->GetBlock(can_create: true).FindBlockByID(block_id: sc_parent_die.GetID()); |
| 1640 | if (symbol_context_scope == nullptr) |
| 1641 | symbol_context_scope = sc.function; |
| 1642 | } else { |
| 1643 | symbol_context_scope = sc.module_sp.get(); |
| 1644 | } |
| 1645 | |
| 1646 | if (symbol_context_scope != nullptr) |
| 1647 | type_sp->SetSymbolContextScope(symbol_context_scope); |
| 1648 | return type_sp; |
| 1649 | } |
| 1650 | |
| 1651 | void DWARFASTParserClang::GetUniqueTypeNameAndDeclaration( |
| 1652 | const lldb_private::plugin::dwarf::DWARFDIE &die, |
| 1653 | lldb::LanguageType language, lldb_private::ConstString &unique_typename, |
| 1654 | lldb_private::Declaration &decl_declaration) { |
| 1655 | // For C++, we rely solely upon the one definition rule that says |
| 1656 | // only one thing can exist at a given decl context. We ignore the |
| 1657 | // file and line that things are declared on. |
| 1658 | if (!die.IsValid() || !Language::LanguageIsCPlusPlus(language) || |
| 1659 | unique_typename.IsEmpty()) |
| 1660 | return; |
| 1661 | decl_declaration.Clear(); |
| 1662 | std::string qualified_name; |
| 1663 | DWARFDIE parent_decl_ctx_die = die.GetParentDeclContextDIE(); |
| 1664 | // TODO: change this to get the correct decl context parent.... |
| 1665 | while (parent_decl_ctx_die) { |
| 1666 | // The name may not contain template parameters due to |
| 1667 | // -gsimple-template-names; we must reconstruct the full name from child |
| 1668 | // template parameter dies via GetDIEClassTemplateParams(). |
| 1669 | const dw_tag_t parent_tag = parent_decl_ctx_die.Tag(); |
| 1670 | switch (parent_tag) { |
| 1671 | case DW_TAG_namespace: { |
| 1672 | if (const char *namespace_name = parent_decl_ctx_die.GetName()) { |
| 1673 | qualified_name.insert(pos: 0, s: "::" ); |
| 1674 | qualified_name.insert(pos: 0, s: namespace_name); |
| 1675 | } else { |
| 1676 | qualified_name.insert(pos: 0, s: "(anonymous namespace)::" ); |
| 1677 | } |
| 1678 | parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); |
| 1679 | break; |
| 1680 | } |
| 1681 | |
| 1682 | case DW_TAG_class_type: |
| 1683 | case DW_TAG_structure_type: |
| 1684 | case DW_TAG_union_type: { |
| 1685 | if (const char *class_union_struct_name = parent_decl_ctx_die.GetName()) { |
| 1686 | qualified_name.insert(pos: 0, s: "::" ); |
| 1687 | qualified_name.insert(pos1: 0, |
| 1688 | str: GetDIEClassTemplateParams(die: parent_decl_ctx_die)); |
| 1689 | qualified_name.insert(pos: 0, s: class_union_struct_name); |
| 1690 | } |
| 1691 | parent_decl_ctx_die = parent_decl_ctx_die.GetParentDeclContextDIE(); |
| 1692 | break; |
| 1693 | } |
| 1694 | |
| 1695 | default: |
| 1696 | parent_decl_ctx_die.Clear(); |
| 1697 | break; |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | if (qualified_name.empty()) |
| 1702 | qualified_name.append(s: "::" ); |
| 1703 | |
| 1704 | qualified_name.append(s: unique_typename.GetCString()); |
| 1705 | qualified_name.append(str: GetDIEClassTemplateParams(die)); |
| 1706 | |
| 1707 | unique_typename = ConstString(qualified_name); |
| 1708 | } |
| 1709 | |
| 1710 | TypeSP |
| 1711 | DWARFASTParserClang::ParseStructureLikeDIE(const SymbolContext &sc, |
| 1712 | const DWARFDIE &die, |
| 1713 | ParsedDWARFTypeAttributes &attrs) { |
| 1714 | CompilerType clang_type; |
| 1715 | const dw_tag_t tag = die.Tag(); |
| 1716 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 1717 | LanguageType cu_language = SymbolFileDWARF::GetLanguage(unit&: *die.GetCU()); |
| 1718 | Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups); |
| 1719 | |
| 1720 | ConstString unique_typename(attrs.name); |
| 1721 | Declaration unique_decl(attrs.decl); |
| 1722 | uint64_t byte_size = attrs.byte_size.value_or(u: 0); |
| 1723 | |
| 1724 | if (attrs.name) { |
| 1725 | GetUniqueTypeNameAndDeclaration(die, language: cu_language, unique_typename, |
| 1726 | decl_declaration&: unique_decl); |
| 1727 | if (log) { |
| 1728 | dwarf->GetObjectFile()->GetModule()->LogMessage( |
| 1729 | log, format: "SymbolFileDWARF({0:p}) - {1:x16}: {2} has unique name: {3} " , |
| 1730 | args: static_cast<void *>(this), args: die.GetID(), args: DW_TAG_value_to_name(tag), |
| 1731 | args: unique_typename.AsCString()); |
| 1732 | } |
| 1733 | if (UniqueDWARFASTType *unique_ast_entry_type = |
| 1734 | dwarf->GetUniqueDWARFASTTypeMap().Find( |
| 1735 | name: unique_typename, die, decl: unique_decl, byte_size, |
| 1736 | is_forward_declaration: attrs.is_forward_declaration)) { |
| 1737 | if (TypeSP type_sp = unique_ast_entry_type->m_type_sp) { |
| 1738 | dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get(); |
| 1739 | LinkDeclContextToDIE( |
| 1740 | decl_ctx: GetCachedClangDeclContextForDIE(die: unique_ast_entry_type->m_die), die); |
| 1741 | // If the DIE being parsed in this function is a definition and the |
| 1742 | // entry in the map is a declaration, then we need to update the entry |
| 1743 | // to point to the definition DIE. |
| 1744 | if (!attrs.is_forward_declaration && |
| 1745 | unique_ast_entry_type->m_is_forward_declaration) { |
| 1746 | unique_ast_entry_type->UpdateToDefDIE(def_die: die, declaration&: unique_decl, byte_size); |
| 1747 | clang_type = type_sp->GetForwardCompilerType(); |
| 1748 | |
| 1749 | CompilerType compiler_type_no_qualifiers = |
| 1750 | ClangUtil::RemoveFastQualifiers(ct: clang_type); |
| 1751 | dwarf->GetForwardDeclCompilerTypeToDIE().insert_or_assign( |
| 1752 | Key: compiler_type_no_qualifiers.GetOpaqueQualType(), |
| 1753 | Val: *die.GetDIERef()); |
| 1754 | } |
| 1755 | return type_sp; |
| 1756 | } |
| 1757 | } |
| 1758 | } |
| 1759 | |
| 1760 | DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\")\n" , die.GetID(), |
| 1761 | DW_TAG_value_to_name(tag), type_name_cstr); |
| 1762 | |
| 1763 | int tag_decl_kind = -1; |
| 1764 | AccessType default_accessibility = eAccessNone; |
| 1765 | if (tag == DW_TAG_structure_type) { |
| 1766 | tag_decl_kind = llvm::to_underlying(E: clang::TagTypeKind::Struct); |
| 1767 | default_accessibility = eAccessPublic; |
| 1768 | } else if (tag == DW_TAG_union_type) { |
| 1769 | tag_decl_kind = llvm::to_underlying(E: clang::TagTypeKind::Union); |
| 1770 | default_accessibility = eAccessPublic; |
| 1771 | } else if (tag == DW_TAG_class_type) { |
| 1772 | tag_decl_kind = llvm::to_underlying(E: clang::TagTypeKind::Class); |
| 1773 | default_accessibility = eAccessPrivate; |
| 1774 | } |
| 1775 | |
| 1776 | if ((attrs.class_language == eLanguageTypeObjC || |
| 1777 | attrs.class_language == eLanguageTypeObjC_plus_plus) && |
| 1778 | !attrs.is_complete_objc_class) { |
| 1779 | // We have a valid eSymbolTypeObjCClass class symbol whose name |
| 1780 | // matches the current objective C class that we are trying to find |
| 1781 | // and this DIE isn't the complete definition (we checked |
| 1782 | // is_complete_objc_class above and know it is false), so the real |
| 1783 | // definition is in here somewhere |
| 1784 | TypeSP type_sp = |
| 1785 | dwarf->FindCompleteObjCDefinitionTypeForDIE(die, type_name: attrs.name, must_be_implementation: true); |
| 1786 | |
| 1787 | if (!type_sp) { |
| 1788 | SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile(); |
| 1789 | if (debug_map_symfile) { |
| 1790 | // We weren't able to find a full declaration in this DWARF, |
| 1791 | // see if we have a declaration anywhere else... |
| 1792 | type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE( |
| 1793 | die, type_name: attrs.name, must_be_implementation: true); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | if (type_sp) { |
| 1798 | if (log) { |
| 1799 | dwarf->GetObjectFile()->GetModule()->LogMessage( |
| 1800 | log, |
| 1801 | format: "SymbolFileDWARF({0:p}) - {1:x16}: {2} ({3}) type \"{4}\" is an " |
| 1802 | "incomplete objc type, complete type is {5:x8}" , |
| 1803 | args: static_cast<void *>(this), args: die.GetID(), args: DW_TAG_value_to_name(tag), |
| 1804 | args: tag, args: attrs.name.GetCString(), args: type_sp->GetID()); |
| 1805 | } |
| 1806 | return type_sp; |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | if (attrs.is_forward_declaration) { |
| 1811 | // See if the type comes from a Clang module and if so, track down |
| 1812 | // that type. |
| 1813 | TypeSP type_sp = ParseTypeFromClangModule(sc, die, log); |
| 1814 | if (type_sp) |
| 1815 | return type_sp; |
| 1816 | } |
| 1817 | |
| 1818 | assert(tag_decl_kind != -1); |
| 1819 | UNUSED_IF_ASSERT_DISABLED(tag_decl_kind); |
| 1820 | clang::DeclContext *containing_decl_ctx = |
| 1821 | GetClangDeclContextContainingDIE(die, decl_ctx_die: nullptr); |
| 1822 | |
| 1823 | PrepareContextToReceiveMembers(ast&: m_ast, ast_importer&: GetClangASTImporter(), |
| 1824 | decl_ctx: containing_decl_ctx, die, |
| 1825 | type_name_cstr: attrs.name.GetCString()); |
| 1826 | |
| 1827 | if (attrs.accessibility == eAccessNone && containing_decl_ctx) { |
| 1828 | // Check the decl context that contains this class/struct/union. If |
| 1829 | // it is a class we must give it an accessibility. |
| 1830 | const clang::Decl::Kind containing_decl_kind = |
| 1831 | containing_decl_ctx->getDeclKind(); |
| 1832 | if (DeclKindIsCXXClass(decl_kind: containing_decl_kind)) |
| 1833 | attrs.accessibility = default_accessibility; |
| 1834 | } |
| 1835 | |
| 1836 | ClangASTMetadata metadata; |
| 1837 | metadata.SetUserID(die.GetID()); |
| 1838 | if (!attrs.is_forward_declaration) |
| 1839 | metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual(die)); |
| 1840 | |
| 1841 | TypeSystemClang::TemplateParameterInfos template_param_infos; |
| 1842 | if (ParseTemplateParameterInfos(parent_die: die, template_param_infos)) { |
| 1843 | clang::ClassTemplateDecl *class_template_decl = |
| 1844 | m_ast.ParseClassTemplateDecl( |
| 1845 | decl_ctx: containing_decl_ctx, owning_module: GetOwningClangModule(die), access_type: attrs.accessibility, |
| 1846 | parent_name: attrs.name.GetCString(), tag_decl_kind, template_param_infos); |
| 1847 | if (!class_template_decl) { |
| 1848 | if (log) { |
| 1849 | dwarf->GetObjectFile()->GetModule()->LogMessage( |
| 1850 | log, |
| 1851 | format: "SymbolFileDWARF({0:p}) - {1:x16}: {2} ({3}) type \"{4}\" " |
| 1852 | "clang::ClassTemplateDecl failed to return a decl." , |
| 1853 | args: static_cast<void *>(this), args: die.GetID(), args: DW_TAG_value_to_name(tag), |
| 1854 | args: tag, args: attrs.name.GetCString()); |
| 1855 | } |
| 1856 | return TypeSP(); |
| 1857 | } |
| 1858 | |
| 1859 | clang::ClassTemplateSpecializationDecl *class_specialization_decl = |
| 1860 | m_ast.CreateClassTemplateSpecializationDecl( |
| 1861 | decl_ctx: containing_decl_ctx, owning_module: GetOwningClangModule(die), class_template_decl, |
| 1862 | kind: tag_decl_kind, infos: template_param_infos); |
| 1863 | clang_type = |
| 1864 | m_ast.CreateClassTemplateSpecializationType(class_template_specialization_decl: class_specialization_decl); |
| 1865 | |
| 1866 | m_ast.SetMetadata(class_template_decl, metadata); |
| 1867 | m_ast.SetMetadata(class_specialization_decl, metadata); |
| 1868 | } |
| 1869 | |
| 1870 | if (!clang_type) { |
| 1871 | clang_type = m_ast.CreateRecordType( |
| 1872 | decl_ctx: containing_decl_ctx, owning_module: GetOwningClangModule(die), access_type: attrs.accessibility, |
| 1873 | name: attrs.name.GetCString(), kind: tag_decl_kind, language: attrs.class_language, metadata, |
| 1874 | exports_symbols: attrs.exports_symbols); |
| 1875 | } |
| 1876 | |
| 1877 | TypeSP type_sp = dwarf->MakeType( |
| 1878 | uid: die.GetID(), name: attrs.name, byte_size: attrs.byte_size, context: nullptr, LLDB_INVALID_UID, |
| 1879 | encoding_uid_type: Type::eEncodingIsUID, decl: &attrs.decl, compiler_qual_type: clang_type, |
| 1880 | compiler_type_resolve_state: Type::ResolveState::Forward, |
| 1881 | opaque_payload: TypePayloadClang(OptionalClangModuleID(), attrs.is_complete_objc_class)); |
| 1882 | |
| 1883 | // Store a forward declaration to this class type in case any |
| 1884 | // parameters in any class methods need it for the clang types for |
| 1885 | // function prototypes. |
| 1886 | clang::DeclContext *type_decl_ctx = |
| 1887 | TypeSystemClang::GetDeclContextForType(type: clang_type); |
| 1888 | LinkDeclContextToDIE(decl_ctx: type_decl_ctx, die); |
| 1889 | |
| 1890 | // UniqueDWARFASTType is large, so don't create a local variables on the |
| 1891 | // stack, put it on the heap. This function is often called recursively and |
| 1892 | // clang isn't good at sharing the stack space for variables in different |
| 1893 | // blocks. |
| 1894 | auto unique_ast_entry_up = std::make_unique<UniqueDWARFASTType>(); |
| 1895 | // Add our type to the unique type map so we don't end up creating many |
| 1896 | // copies of the same type over and over in the ASTContext for our |
| 1897 | // module |
| 1898 | unique_ast_entry_up->m_type_sp = type_sp; |
| 1899 | unique_ast_entry_up->m_die = die; |
| 1900 | unique_ast_entry_up->m_declaration = unique_decl; |
| 1901 | unique_ast_entry_up->m_byte_size = byte_size; |
| 1902 | unique_ast_entry_up->m_is_forward_declaration = attrs.is_forward_declaration; |
| 1903 | dwarf->GetUniqueDWARFASTTypeMap().Insert(name: unique_typename, |
| 1904 | entry: *unique_ast_entry_up); |
| 1905 | |
| 1906 | // Leave this as a forward declaration until we need to know the |
| 1907 | // details of the type. lldb_private::Type will automatically call |
| 1908 | // the SymbolFile virtual function |
| 1909 | // "SymbolFileDWARF::CompleteType(Type *)" When the definition |
| 1910 | // needs to be defined. |
| 1911 | bool inserted = |
| 1912 | dwarf->GetForwardDeclCompilerTypeToDIE() |
| 1913 | .try_emplace( |
| 1914 | Key: ClangUtil::RemoveFastQualifiers(ct: clang_type).GetOpaqueQualType(), |
| 1915 | Args: *die.GetDIERef()) |
| 1916 | .second; |
| 1917 | assert(inserted && "Type already in the forward declaration map!" ); |
| 1918 | (void)inserted; |
| 1919 | m_ast.SetHasExternalStorage(type: clang_type.GetOpaqueQualType(), has_extern: true); |
| 1920 | |
| 1921 | // If we made a clang type, set the trivial abi if applicable: We only |
| 1922 | // do this for pass by value - which implies the Trivial ABI. There |
| 1923 | // isn't a way to assert that something that would normally be pass by |
| 1924 | // value is pass by reference, so we ignore that attribute if set. |
| 1925 | if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_value) { |
| 1926 | clang::CXXRecordDecl *record_decl = |
| 1927 | m_ast.GetAsCXXRecordDecl(type: clang_type.GetOpaqueQualType()); |
| 1928 | if (record_decl && record_decl->getDefinition()) { |
| 1929 | record_decl->setHasTrivialSpecialMemberForCall(); |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | if (attrs.calling_convention == llvm::dwarf::DW_CC_pass_by_reference) { |
| 1934 | clang::CXXRecordDecl *record_decl = |
| 1935 | m_ast.GetAsCXXRecordDecl(type: clang_type.GetOpaqueQualType()); |
| 1936 | if (record_decl) |
| 1937 | record_decl->setArgPassingRestrictions( |
| 1938 | clang::RecordArgPassingKind::CannotPassInRegs); |
| 1939 | } |
| 1940 | return type_sp; |
| 1941 | } |
| 1942 | |
| 1943 | // DWARF parsing functions |
| 1944 | |
| 1945 | class DWARFASTParserClang::DelayedAddObjCClassProperty { |
| 1946 | public: |
| 1947 | DelayedAddObjCClassProperty( |
| 1948 | const CompilerType &class_opaque_type, const char *property_name, |
| 1949 | const CompilerType &property_opaque_type, // The property type is only |
| 1950 | // required if you don't have an |
| 1951 | // ivar decl |
| 1952 | const char *property_setter_name, const char *property_getter_name, |
| 1953 | uint32_t property_attributes, ClangASTMetadata metadata) |
| 1954 | : m_class_opaque_type(class_opaque_type), m_property_name(property_name), |
| 1955 | m_property_opaque_type(property_opaque_type), |
| 1956 | m_property_setter_name(property_setter_name), |
| 1957 | m_property_getter_name(property_getter_name), |
| 1958 | m_property_attributes(property_attributes), m_metadata(metadata) {} |
| 1959 | |
| 1960 | bool Finalize() { |
| 1961 | return TypeSystemClang::AddObjCClassProperty( |
| 1962 | type: m_class_opaque_type, property_name: m_property_name, property_compiler_type: m_property_opaque_type, |
| 1963 | /*ivar_decl=*/nullptr, property_setter_name: m_property_setter_name, property_getter_name: m_property_getter_name, |
| 1964 | property_attributes: m_property_attributes, metadata: m_metadata); |
| 1965 | } |
| 1966 | |
| 1967 | private: |
| 1968 | CompilerType m_class_opaque_type; |
| 1969 | const char *m_property_name; |
| 1970 | CompilerType m_property_opaque_type; |
| 1971 | const char *m_property_setter_name; |
| 1972 | const char *m_property_getter_name; |
| 1973 | uint32_t m_property_attributes; |
| 1974 | ClangASTMetadata m_metadata; |
| 1975 | }; |
| 1976 | |
| 1977 | static std::optional<clang::APValue> MakeAPValue(const clang::ASTContext &ast, |
| 1978 | CompilerType clang_type, |
| 1979 | uint64_t value) { |
| 1980 | std::optional<uint64_t> bit_width = |
| 1981 | llvm::expectedToOptional(E: clang_type.GetBitSize(exe_scope: nullptr)); |
| 1982 | if (!bit_width) |
| 1983 | return std::nullopt; |
| 1984 | |
| 1985 | bool is_signed = false; |
| 1986 | const bool is_integral = clang_type.IsIntegerOrEnumerationType(is_signed); |
| 1987 | |
| 1988 | llvm::APSInt apint(*bit_width, !is_signed); |
| 1989 | apint = value; |
| 1990 | |
| 1991 | if (is_integral) |
| 1992 | return clang::APValue(apint); |
| 1993 | |
| 1994 | uint32_t count; |
| 1995 | bool is_complex; |
| 1996 | // FIXME: we currently support a limited set of floating point types. |
| 1997 | // E.g., 16-bit floats are not supported. |
| 1998 | if (!clang_type.IsFloatingPointType(count, is_complex)) |
| 1999 | return std::nullopt; |
| 2000 | |
| 2001 | return clang::APValue(llvm::APFloat( |
| 2002 | ast.getFloatTypeSemantics(T: ClangUtil::GetQualType(ct: clang_type)), apint)); |
| 2003 | } |
| 2004 | |
| 2005 | bool DWARFASTParserClang::ParseTemplateDIE( |
| 2006 | const DWARFDIE &die, |
| 2007 | TypeSystemClang::TemplateParameterInfos &template_param_infos) { |
| 2008 | const dw_tag_t tag = die.Tag(); |
| 2009 | bool is_template_template_argument = false; |
| 2010 | |
| 2011 | switch (tag) { |
| 2012 | case DW_TAG_GNU_template_parameter_pack: { |
| 2013 | template_param_infos.SetParameterPack( |
| 2014 | std::make_unique<TypeSystemClang::TemplateParameterInfos>()); |
| 2015 | for (DWARFDIE child_die : die.children()) { |
| 2016 | if (!ParseTemplateDIE(die: child_die, template_param_infos&: template_param_infos.GetParameterPack())) |
| 2017 | return false; |
| 2018 | } |
| 2019 | if (const char *name = die.GetName()) { |
| 2020 | template_param_infos.SetPackName(name); |
| 2021 | } |
| 2022 | return true; |
| 2023 | } |
| 2024 | case DW_TAG_GNU_template_template_param: |
| 2025 | is_template_template_argument = true; |
| 2026 | [[fallthrough]]; |
| 2027 | case DW_TAG_template_type_parameter: |
| 2028 | case DW_TAG_template_value_parameter: { |
| 2029 | DWARFAttributes attributes = die.GetAttributes(); |
| 2030 | if (attributes.Size() == 0) |
| 2031 | return true; |
| 2032 | |
| 2033 | const char *name = nullptr; |
| 2034 | const char *template_name = nullptr; |
| 2035 | CompilerType clang_type; |
| 2036 | uint64_t uval64 = 0; |
| 2037 | bool uval64_valid = false; |
| 2038 | bool is_default_template_arg = false; |
| 2039 | DWARFFormValue form_value; |
| 2040 | for (size_t i = 0; i < attributes.Size(); ++i) { |
| 2041 | const dw_attr_t attr = attributes.AttributeAtIndex(i); |
| 2042 | |
| 2043 | switch (attr) { |
| 2044 | case DW_AT_name: |
| 2045 | if (attributes.ExtractFormValueAtIndex(i, form_value)) |
| 2046 | name = form_value.AsCString(); |
| 2047 | break; |
| 2048 | |
| 2049 | case DW_AT_GNU_template_name: |
| 2050 | if (attributes.ExtractFormValueAtIndex(i, form_value)) |
| 2051 | template_name = form_value.AsCString(); |
| 2052 | break; |
| 2053 | |
| 2054 | case DW_AT_type: |
| 2055 | if (attributes.ExtractFormValueAtIndex(i, form_value)) { |
| 2056 | Type *lldb_type = die.ResolveTypeUID(die: form_value.Reference()); |
| 2057 | if (lldb_type) |
| 2058 | clang_type = lldb_type->GetForwardCompilerType(); |
| 2059 | } |
| 2060 | break; |
| 2061 | |
| 2062 | case DW_AT_const_value: |
| 2063 | if (attributes.ExtractFormValueAtIndex(i, form_value)) { |
| 2064 | uval64_valid = true; |
| 2065 | uval64 = form_value.Unsigned(); |
| 2066 | } |
| 2067 | break; |
| 2068 | case DW_AT_default_value: |
| 2069 | if (attributes.ExtractFormValueAtIndex(i, form_value)) |
| 2070 | is_default_template_arg = form_value.Boolean(); |
| 2071 | break; |
| 2072 | default: |
| 2073 | break; |
| 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | clang::ASTContext &ast = m_ast.getASTContext(); |
| 2078 | if (!clang_type) |
| 2079 | clang_type = m_ast.GetBasicType(type: eBasicTypeVoid); |
| 2080 | |
| 2081 | if (!is_template_template_argument) { |
| 2082 | |
| 2083 | if (name && !name[0]) |
| 2084 | name = nullptr; |
| 2085 | |
| 2086 | if (tag == DW_TAG_template_value_parameter && uval64_valid) { |
| 2087 | if (auto value = MakeAPValue(ast, clang_type, value: uval64)) { |
| 2088 | template_param_infos.InsertArg( |
| 2089 | name, arg: clang::TemplateArgument( |
| 2090 | ast, ClangUtil::GetQualType(ct: clang_type), |
| 2091 | std::move(*value), is_default_template_arg)); |
| 2092 | return true; |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | // We get here if this is a type-template parameter or we couldn't create |
| 2097 | // a non-type template parameter. |
| 2098 | template_param_infos.InsertArg( |
| 2099 | name, arg: clang::TemplateArgument(ClangUtil::GetQualType(ct: clang_type), |
| 2100 | /*isNullPtr*/ false, |
| 2101 | is_default_template_arg)); |
| 2102 | } else { |
| 2103 | auto *tplt_type = m_ast.CreateTemplateTemplateParmDecl(template_name); |
| 2104 | template_param_infos.InsertArg( |
| 2105 | name, arg: clang::TemplateArgument(clang::TemplateName(tplt_type), |
| 2106 | is_default_template_arg)); |
| 2107 | } |
| 2108 | } |
| 2109 | return true; |
| 2110 | |
| 2111 | default: |
| 2112 | break; |
| 2113 | } |
| 2114 | return false; |
| 2115 | } |
| 2116 | |
| 2117 | bool DWARFASTParserClang::ParseTemplateParameterInfos( |
| 2118 | const DWARFDIE &parent_die, |
| 2119 | TypeSystemClang::TemplateParameterInfos &template_param_infos) { |
| 2120 | |
| 2121 | if (!parent_die) |
| 2122 | return false; |
| 2123 | |
| 2124 | for (DWARFDIE die : parent_die.children()) { |
| 2125 | const dw_tag_t tag = die.Tag(); |
| 2126 | |
| 2127 | switch (tag) { |
| 2128 | case DW_TAG_template_type_parameter: |
| 2129 | case DW_TAG_template_value_parameter: |
| 2130 | case DW_TAG_GNU_template_parameter_pack: |
| 2131 | case DW_TAG_GNU_template_template_param: |
| 2132 | ParseTemplateDIE(die, template_param_infos); |
| 2133 | break; |
| 2134 | |
| 2135 | default: |
| 2136 | break; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | return !template_param_infos.IsEmpty() || |
| 2141 | template_param_infos.hasParameterPack(); |
| 2142 | } |
| 2143 | |
| 2144 | bool DWARFASTParserClang::CompleteRecordType(const DWARFDIE &die, |
| 2145 | const CompilerType &clang_type) { |
| 2146 | const dw_tag_t tag = die.Tag(); |
| 2147 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 2148 | |
| 2149 | ClangASTImporter::LayoutInfo layout_info; |
| 2150 | std::vector<DWARFDIE> contained_type_dies; |
| 2151 | |
| 2152 | if (die.GetAttributeValueAsUnsigned(attr: DW_AT_declaration, fail_value: 0)) |
| 2153 | return false; // No definition, cannot complete. |
| 2154 | |
| 2155 | // Start the definition if the type is not being defined already. This can |
| 2156 | // happen (e.g.) when adding nested types to a class type -- see |
| 2157 | // PrepareContextToReceiveMembers. |
| 2158 | if (!clang_type.IsBeingDefined()) |
| 2159 | TypeSystemClang::StartTagDeclarationDefinition(type: clang_type); |
| 2160 | |
| 2161 | AccessType default_accessibility = eAccessNone; |
| 2162 | if (tag == DW_TAG_structure_type) { |
| 2163 | default_accessibility = eAccessPublic; |
| 2164 | } else if (tag == DW_TAG_union_type) { |
| 2165 | default_accessibility = eAccessPublic; |
| 2166 | } else if (tag == DW_TAG_class_type) { |
| 2167 | default_accessibility = eAccessPrivate; |
| 2168 | } |
| 2169 | |
| 2170 | std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> bases; |
| 2171 | // Parse members and base classes first |
| 2172 | std::vector<DWARFDIE> member_function_dies; |
| 2173 | |
| 2174 | DelayedPropertyList delayed_properties; |
| 2175 | ParseChildMembers(die, class_compiler_type: clang_type, base_classes&: bases, member_function_dies, |
| 2176 | contained_type_dies, delayed_properties, |
| 2177 | default_accessibility, layout_info); |
| 2178 | |
| 2179 | // Now parse any methods if there were any... |
| 2180 | for (const DWARFDIE &die : member_function_dies) |
| 2181 | dwarf->ResolveType(die); |
| 2182 | |
| 2183 | if (TypeSystemClang::IsObjCObjectOrInterfaceType(type: clang_type)) { |
| 2184 | ConstString class_name(clang_type.GetTypeName()); |
| 2185 | if (class_name) { |
| 2186 | dwarf->GetObjCMethods(class_name, callback: [&](DWARFDIE method_die) { |
| 2187 | method_die.ResolveType(); |
| 2188 | return true; |
| 2189 | }); |
| 2190 | |
| 2191 | for (DelayedAddObjCClassProperty &property : delayed_properties) |
| 2192 | property.Finalize(); |
| 2193 | } |
| 2194 | } |
| 2195 | |
| 2196 | if (!bases.empty()) { |
| 2197 | // Make sure all base classes refer to complete types and not forward |
| 2198 | // declarations. If we don't do this, clang will crash with an |
| 2199 | // assertion in the call to clang_type.TransferBaseClasses() |
| 2200 | for (const auto &base_class : bases) { |
| 2201 | clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo(); |
| 2202 | if (type_source_info) |
| 2203 | TypeSystemClang::RequireCompleteType( |
| 2204 | type: m_ast.GetType(qt: type_source_info->getType())); |
| 2205 | } |
| 2206 | |
| 2207 | m_ast.TransferBaseClasses(type: clang_type.GetOpaqueQualType(), bases: std::move(bases)); |
| 2208 | } |
| 2209 | |
| 2210 | m_ast.AddMethodOverridesForCXXRecordType(type: clang_type.GetOpaqueQualType()); |
| 2211 | TypeSystemClang::BuildIndirectFields(type: clang_type); |
| 2212 | TypeSystemClang::CompleteTagDeclarationDefinition(type: clang_type); |
| 2213 | |
| 2214 | layout_info.bit_size = |
| 2215 | die.GetAttributeValueAsUnsigned(attr: DW_AT_byte_size, fail_value: 0) * 8; |
| 2216 | layout_info.alignment = |
| 2217 | die.GetAttributeValueAsUnsigned(attr: llvm::dwarf::DW_AT_alignment, fail_value: 0) * 8; |
| 2218 | |
| 2219 | clang::CXXRecordDecl *record_decl = |
| 2220 | m_ast.GetAsCXXRecordDecl(type: clang_type.GetOpaqueQualType()); |
| 2221 | if (record_decl) |
| 2222 | GetClangASTImporter().SetRecordLayout(record_decl, layout_info); |
| 2223 | |
| 2224 | // DWARF doesn't have the attribute, but we can infer the value the same way |
| 2225 | // as Clang Sema does. It's required to calculate the size of pointers to |
| 2226 | // member functions of this type. |
| 2227 | if (m_ast.getASTContext().getTargetInfo().getCXXABI().isMicrosoft()) { |
| 2228 | auto IM = record_decl->calculateInheritanceModel(); |
| 2229 | record_decl->addAttr(clang::MSInheritanceAttr::CreateImplicit( |
| 2230 | m_ast.getASTContext(), true, {}, |
| 2231 | clang::MSInheritanceAttr::Spelling(IM))); |
| 2232 | } |
| 2233 | |
| 2234 | // Now parse all contained types inside of the class. We make forward |
| 2235 | // declarations to all classes, but we need the CXXRecordDecl to have decls |
| 2236 | // for all contained types because we don't get asked for them via the |
| 2237 | // external AST support. |
| 2238 | for (const DWARFDIE &die : contained_type_dies) |
| 2239 | dwarf->ResolveType(die); |
| 2240 | |
| 2241 | return (bool)clang_type; |
| 2242 | } |
| 2243 | |
| 2244 | bool DWARFASTParserClang::CompleteEnumType(const DWARFDIE &die, |
| 2245 | lldb_private::Type *type, |
| 2246 | const CompilerType &clang_type) { |
| 2247 | assert(clang_type.IsEnumerationType()); |
| 2248 | |
| 2249 | if (TypeSystemClang::StartTagDeclarationDefinition(type: clang_type)) { |
| 2250 | if (die.HasChildren()) |
| 2251 | ParseChildEnumerators( |
| 2252 | compiler_type: clang_type, is_signed: clang_type.IsEnumerationIntegerTypeSigned(), |
| 2253 | enumerator_byte_size: llvm::expectedToOptional(E: type->GetByteSize(exe_scope: nullptr)).value_or(u: 0), |
| 2254 | parent_die: die); |
| 2255 | |
| 2256 | TypeSystemClang::CompleteTagDeclarationDefinition(type: clang_type); |
| 2257 | } |
| 2258 | return (bool)clang_type; |
| 2259 | } |
| 2260 | |
| 2261 | bool DWARFASTParserClang::CompleteTypeFromDWARF( |
| 2262 | const DWARFDIE &die, lldb_private::Type *type, |
| 2263 | const CompilerType &clang_type) { |
| 2264 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 2265 | |
| 2266 | std::lock_guard<std::recursive_mutex> guard( |
| 2267 | dwarf->GetObjectFile()->GetModule()->GetMutex()); |
| 2268 | |
| 2269 | // Disable external storage for this type so we don't get anymore |
| 2270 | // clang::ExternalASTSource queries for this type. |
| 2271 | m_ast.SetHasExternalStorage(type: clang_type.GetOpaqueQualType(), has_extern: false); |
| 2272 | |
| 2273 | if (!die) |
| 2274 | return false; |
| 2275 | |
| 2276 | const dw_tag_t tag = die.Tag(); |
| 2277 | |
| 2278 | assert(clang_type); |
| 2279 | switch (tag) { |
| 2280 | case DW_TAG_structure_type: |
| 2281 | case DW_TAG_union_type: |
| 2282 | case DW_TAG_class_type: |
| 2283 | CompleteRecordType(die, clang_type); |
| 2284 | break; |
| 2285 | case DW_TAG_enumeration_type: |
| 2286 | CompleteEnumType(die, type, clang_type); |
| 2287 | break; |
| 2288 | default: |
| 2289 | assert(false && "not a forward clang type decl!" ); |
| 2290 | break; |
| 2291 | } |
| 2292 | |
| 2293 | // If the type is still not fully defined at this point, it means we weren't |
| 2294 | // able to find its definition. We must forcefully complete it to preserve |
| 2295 | // clang AST invariants. |
| 2296 | if (clang_type.IsBeingDefined()) { |
| 2297 | TypeSystemClang::CompleteTagDeclarationDefinition(type: clang_type); |
| 2298 | m_ast.SetDeclIsForcefullyCompleted(ClangUtil::GetAsTagDecl(type: clang_type)); |
| 2299 | } |
| 2300 | |
| 2301 | return true; |
| 2302 | } |
| 2303 | |
| 2304 | void DWARFASTParserClang::EnsureAllDIEsInDeclContextHaveBeenParsed( |
| 2305 | lldb_private::CompilerDeclContext decl_context) { |
| 2306 | auto opaque_decl_ctx = |
| 2307 | (clang::DeclContext *)decl_context.GetOpaqueDeclContext(); |
| 2308 | for (auto it = m_decl_ctx_to_die.find(x: opaque_decl_ctx); |
| 2309 | it != m_decl_ctx_to_die.end() && it->first == opaque_decl_ctx; |
| 2310 | it = m_decl_ctx_to_die.erase(position: it)) |
| 2311 | for (DWARFDIE decl : it->second.children()) |
| 2312 | GetClangDeclForDIE(die: decl); |
| 2313 | } |
| 2314 | |
| 2315 | CompilerDecl DWARFASTParserClang::GetDeclForUIDFromDWARF(const DWARFDIE &die) { |
| 2316 | clang::Decl *clang_decl = GetClangDeclForDIE(die); |
| 2317 | if (clang_decl != nullptr) |
| 2318 | return m_ast.GetCompilerDecl(decl: clang_decl); |
| 2319 | return {}; |
| 2320 | } |
| 2321 | |
| 2322 | CompilerDeclContext |
| 2323 | DWARFASTParserClang::GetDeclContextForUIDFromDWARF(const DWARFDIE &die) { |
| 2324 | clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE(die); |
| 2325 | if (clang_decl_ctx) |
| 2326 | return m_ast.CreateDeclContext(ctx: clang_decl_ctx); |
| 2327 | return {}; |
| 2328 | } |
| 2329 | |
| 2330 | CompilerDeclContext |
| 2331 | DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF(const DWARFDIE &die) { |
| 2332 | clang::DeclContext *clang_decl_ctx = |
| 2333 | GetClangDeclContextContainingDIE(die, decl_ctx_die: nullptr); |
| 2334 | if (clang_decl_ctx) |
| 2335 | return m_ast.CreateDeclContext(ctx: clang_decl_ctx); |
| 2336 | return {}; |
| 2337 | } |
| 2338 | |
| 2339 | size_t DWARFASTParserClang::ParseChildEnumerators( |
| 2340 | const lldb_private::CompilerType &clang_type, bool is_signed, |
| 2341 | uint32_t enumerator_byte_size, const DWARFDIE &parent_die) { |
| 2342 | if (!parent_die) |
| 2343 | return 0; |
| 2344 | |
| 2345 | size_t enumerators_added = 0; |
| 2346 | |
| 2347 | for (DWARFDIE die : parent_die.children()) { |
| 2348 | const dw_tag_t tag = die.Tag(); |
| 2349 | if (tag != DW_TAG_enumerator) |
| 2350 | continue; |
| 2351 | |
| 2352 | DWARFAttributes attributes = die.GetAttributes(); |
| 2353 | if (attributes.Size() == 0) |
| 2354 | continue; |
| 2355 | |
| 2356 | const char *name = nullptr; |
| 2357 | std::optional<uint64_t> enum_value; |
| 2358 | Declaration decl; |
| 2359 | |
| 2360 | for (size_t i = 0; i < attributes.Size(); ++i) { |
| 2361 | const dw_attr_t attr = attributes.AttributeAtIndex(i); |
| 2362 | DWARFFormValue form_value; |
| 2363 | if (attributes.ExtractFormValueAtIndex(i, form_value)) { |
| 2364 | switch (attr) { |
| 2365 | case DW_AT_const_value: |
| 2366 | if (is_signed) |
| 2367 | enum_value = form_value.Signed(); |
| 2368 | else |
| 2369 | enum_value = form_value.Unsigned(); |
| 2370 | break; |
| 2371 | |
| 2372 | case DW_AT_name: |
| 2373 | name = form_value.AsCString(); |
| 2374 | break; |
| 2375 | |
| 2376 | case DW_AT_description: |
| 2377 | default: |
| 2378 | case DW_AT_decl_file: |
| 2379 | decl.SetFile( |
| 2380 | attributes.CompileUnitAtIndex(i)->GetFile(file_idx: form_value.Unsigned())); |
| 2381 | break; |
| 2382 | case DW_AT_decl_line: |
| 2383 | decl.SetLine(form_value.Unsigned()); |
| 2384 | break; |
| 2385 | case DW_AT_decl_column: |
| 2386 | decl.SetColumn(form_value.Unsigned()); |
| 2387 | break; |
| 2388 | case DW_AT_sibling: |
| 2389 | break; |
| 2390 | } |
| 2391 | } |
| 2392 | } |
| 2393 | |
| 2394 | if (name && name[0] && enum_value) { |
| 2395 | m_ast.AddEnumerationValueToEnumerationType( |
| 2396 | enum_type: clang_type, decl, name, enum_value: *enum_value, enum_value_bit_size: enumerator_byte_size * 8); |
| 2397 | ++enumerators_added; |
| 2398 | } |
| 2399 | } |
| 2400 | return enumerators_added; |
| 2401 | } |
| 2402 | |
| 2403 | ConstString |
| 2404 | DWARFASTParserClang::ConstructDemangledNameFromDWARF(const DWARFDIE &die) { |
| 2405 | bool is_variadic = false; |
| 2406 | bool has_template_params = false; |
| 2407 | std::vector<CompilerType> param_types; |
| 2408 | llvm::SmallVector<llvm::StringRef> param_names; |
| 2409 | StreamString sstr; |
| 2410 | |
| 2411 | DWARFDeclContext decl_ctx = die.GetDWARFDeclContext(); |
| 2412 | sstr << decl_ctx.GetQualifiedName(); |
| 2413 | |
| 2414 | clang::DeclContext *containing_decl_ctx = |
| 2415 | GetClangDeclContextContainingDIE(die, decl_ctx_die: nullptr); |
| 2416 | assert(containing_decl_ctx); |
| 2417 | |
| 2418 | const unsigned cv_quals = GetCXXMethodCVQuals( |
| 2419 | subprogram: die, object_parameter: GetCXXObjectParameter(subprogram: die, containing_decl_ctx: *containing_decl_ctx)); |
| 2420 | |
| 2421 | ParseChildParameters(containing_decl_ctx, parent_die: die, is_variadic, |
| 2422 | has_template_params, function_param_types&: param_types, function_param_names&: param_names); |
| 2423 | sstr << "(" ; |
| 2424 | for (size_t i = 0; i < param_types.size(); i++) { |
| 2425 | if (i > 0) |
| 2426 | sstr << ", " ; |
| 2427 | sstr << param_types[i].GetTypeName(); |
| 2428 | } |
| 2429 | if (is_variadic) |
| 2430 | sstr << ", ..." ; |
| 2431 | sstr << ")" ; |
| 2432 | if (cv_quals & clang::Qualifiers::Const) |
| 2433 | sstr << " const" ; |
| 2434 | |
| 2435 | return ConstString(sstr.GetString()); |
| 2436 | } |
| 2437 | |
| 2438 | Function *DWARFASTParserClang::ParseFunctionFromDWARF( |
| 2439 | CompileUnit &comp_unit, const DWARFDIE &die, AddressRanges func_ranges) { |
| 2440 | llvm::DWARFAddressRangesVector unused_func_ranges; |
| 2441 | const char *name = nullptr; |
| 2442 | const char *mangled = nullptr; |
| 2443 | std::optional<int> decl_file; |
| 2444 | std::optional<int> decl_line; |
| 2445 | std::optional<int> decl_column; |
| 2446 | std::optional<int> call_file; |
| 2447 | std::optional<int> call_line; |
| 2448 | std::optional<int> call_column; |
| 2449 | DWARFExpressionList frame_base; |
| 2450 | |
| 2451 | const dw_tag_t tag = die.Tag(); |
| 2452 | |
| 2453 | if (tag != DW_TAG_subprogram) |
| 2454 | return nullptr; |
| 2455 | |
| 2456 | if (die.GetDIENamesAndRanges(name, mangled, ranges&: unused_func_ranges, decl_file, |
| 2457 | decl_line, decl_column, call_file, call_line, |
| 2458 | call_column, frame_base: &frame_base)) { |
| 2459 | Mangled func_name; |
| 2460 | if (mangled) |
| 2461 | func_name.SetValue(ConstString(mangled)); |
| 2462 | else if ((die.GetParent().Tag() == DW_TAG_compile_unit || |
| 2463 | die.GetParent().Tag() == DW_TAG_partial_unit) && |
| 2464 | Language::LanguageIsCPlusPlus( |
| 2465 | language: SymbolFileDWARF::GetLanguage(unit&: *die.GetCU())) && |
| 2466 | !Language::LanguageIsObjC( |
| 2467 | language: SymbolFileDWARF::GetLanguage(unit&: *die.GetCU())) && |
| 2468 | name && strcmp(s1: name, s2: "main" ) != 0) { |
| 2469 | // If the mangled name is not present in the DWARF, generate the |
| 2470 | // demangled name using the decl context. We skip if the function is |
| 2471 | // "main" as its name is never mangled. |
| 2472 | func_name.SetValue(ConstructDemangledNameFromDWARF(die)); |
| 2473 | } else |
| 2474 | func_name.SetValue(ConstString(name)); |
| 2475 | |
| 2476 | FunctionSP func_sp; |
| 2477 | std::unique_ptr<Declaration> decl_up; |
| 2478 | if (decl_file || decl_line || decl_column) |
| 2479 | decl_up = std::make_unique<Declaration>( |
| 2480 | args: die.GetCU()->GetFile(file_idx: decl_file.value_or(u: 0)), args: decl_line.value_or(u: 0), |
| 2481 | args: decl_column.value_or(u: 0)); |
| 2482 | |
| 2483 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 2484 | // Supply the type _only_ if it has already been parsed |
| 2485 | Type *func_type = dwarf->GetDIEToType().lookup(Val: die.GetDIE()); |
| 2486 | |
| 2487 | assert(func_type == nullptr || func_type != DIE_IS_BEING_PARSED); |
| 2488 | |
| 2489 | const user_id_t func_user_id = die.GetID(); |
| 2490 | |
| 2491 | // The base address of the scope for any of the debugging information |
| 2492 | // entries listed above is given by either the DW_AT_low_pc attribute or the |
| 2493 | // first address in the first range entry in the list of ranges given by the |
| 2494 | // DW_AT_ranges attribute. |
| 2495 | // -- DWARFv5, Section 2.17 Code Addresses, Ranges and Base Addresses |
| 2496 | // |
| 2497 | // If no DW_AT_entry_pc attribute is present, then the entry address is |
| 2498 | // assumed to be the same as the base address of the containing scope. |
| 2499 | // -- DWARFv5, Section 2.18 Entry Address |
| 2500 | // |
| 2501 | // We currently don't support Debug Info Entries with |
| 2502 | // DW_AT_low_pc/DW_AT_entry_pc and DW_AT_ranges attributes (the latter |
| 2503 | // attributes are ignored even though they should be used for the address of |
| 2504 | // the function), but compilers also don't emit that kind of information. If |
| 2505 | // this becomes a problem we need to plumb these attributes separately. |
| 2506 | Address func_addr = func_ranges[0].GetBaseAddress(); |
| 2507 | |
| 2508 | func_sp = std::make_shared<Function>( |
| 2509 | args: &comp_unit, |
| 2510 | args: func_user_id, // UserID is the DIE offset |
| 2511 | args: func_user_id, args&: func_name, args&: func_type, args: std::move(func_addr), |
| 2512 | args: std::move(func_ranges)); |
| 2513 | |
| 2514 | if (func_sp.get() != nullptr) { |
| 2515 | if (frame_base.IsValid()) |
| 2516 | func_sp->GetFrameBaseExpression() = frame_base; |
| 2517 | comp_unit.AddFunction(function_sp&: func_sp); |
| 2518 | return func_sp.get(); |
| 2519 | } |
| 2520 | } |
| 2521 | return nullptr; |
| 2522 | } |
| 2523 | |
| 2524 | namespace { |
| 2525 | /// Parsed form of all attributes that are relevant for parsing Objective-C |
| 2526 | /// properties. |
| 2527 | struct PropertyAttributes { |
| 2528 | explicit PropertyAttributes(const DWARFDIE &die); |
| 2529 | const char *prop_name = nullptr; |
| 2530 | const char *prop_getter_name = nullptr; |
| 2531 | const char *prop_setter_name = nullptr; |
| 2532 | /// \see clang::ObjCPropertyAttribute |
| 2533 | uint32_t prop_attributes = 0; |
| 2534 | }; |
| 2535 | |
| 2536 | struct DiscriminantValue { |
| 2537 | explicit DiscriminantValue(const DWARFDIE &die, ModuleSP module_sp); |
| 2538 | |
| 2539 | uint32_t byte_offset; |
| 2540 | uint32_t byte_size; |
| 2541 | DWARFFormValue type_ref; |
| 2542 | }; |
| 2543 | |
| 2544 | struct VariantMember { |
| 2545 | explicit VariantMember(DWARFDIE &die, ModuleSP module_sp); |
| 2546 | bool IsDefault() const; |
| 2547 | |
| 2548 | std::optional<uint32_t> discr_value; |
| 2549 | DWARFFormValue type_ref; |
| 2550 | ConstString variant_name; |
| 2551 | uint32_t byte_offset; |
| 2552 | ConstString GetName() const; |
| 2553 | }; |
| 2554 | |
| 2555 | struct VariantPart { |
| 2556 | explicit VariantPart(const DWARFDIE &die, const DWARFDIE &parent_die, |
| 2557 | ModuleSP module_sp); |
| 2558 | |
| 2559 | std::vector<VariantMember> &members(); |
| 2560 | |
| 2561 | DiscriminantValue &discriminant(); |
| 2562 | |
| 2563 | private: |
| 2564 | std::vector<VariantMember> _members; |
| 2565 | DiscriminantValue _discriminant; |
| 2566 | }; |
| 2567 | |
| 2568 | } // namespace |
| 2569 | |
| 2570 | ConstString VariantMember::GetName() const { return this->variant_name; } |
| 2571 | |
| 2572 | bool VariantMember::IsDefault() const { return !discr_value; } |
| 2573 | |
| 2574 | VariantMember::VariantMember(DWARFDIE &die, lldb::ModuleSP module_sp) { |
| 2575 | assert(die.Tag() == llvm::dwarf::DW_TAG_variant); |
| 2576 | this->discr_value = |
| 2577 | die.GetAttributeValueAsOptionalUnsigned(attr: DW_AT_discr_value); |
| 2578 | |
| 2579 | for (auto child_die : die.children()) { |
| 2580 | switch (child_die.Tag()) { |
| 2581 | case llvm::dwarf::DW_TAG_member: { |
| 2582 | DWARFAttributes attributes = child_die.GetAttributes(); |
| 2583 | for (std::size_t i = 0; i < attributes.Size(); ++i) { |
| 2584 | DWARFFormValue form_value; |
| 2585 | const dw_attr_t attr = attributes.AttributeAtIndex(i); |
| 2586 | if (attributes.ExtractFormValueAtIndex(i, form_value)) { |
| 2587 | switch (attr) { |
| 2588 | case DW_AT_name: |
| 2589 | variant_name = ConstString(form_value.AsCString()); |
| 2590 | break; |
| 2591 | case DW_AT_type: |
| 2592 | type_ref = form_value; |
| 2593 | break; |
| 2594 | |
| 2595 | case DW_AT_data_member_location: |
| 2596 | if (auto maybe_offset = |
| 2597 | ExtractDataMemberLocation(die, form_value, module_sp)) |
| 2598 | byte_offset = *maybe_offset; |
| 2599 | break; |
| 2600 | |
| 2601 | default: |
| 2602 | break; |
| 2603 | } |
| 2604 | } |
| 2605 | } |
| 2606 | break; |
| 2607 | } |
| 2608 | default: |
| 2609 | break; |
| 2610 | } |
| 2611 | break; |
| 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | DiscriminantValue::DiscriminantValue(const DWARFDIE &die, ModuleSP module_sp) { |
| 2616 | auto referenced_die = die.GetReferencedDIE(attr: DW_AT_discr); |
| 2617 | DWARFAttributes attributes = referenced_die.GetAttributes(); |
| 2618 | for (std::size_t i = 0; i < attributes.Size(); ++i) { |
| 2619 | const dw_attr_t attr = attributes.AttributeAtIndex(i); |
| 2620 | DWARFFormValue form_value; |
| 2621 | if (attributes.ExtractFormValueAtIndex(i, form_value)) { |
| 2622 | switch (attr) { |
| 2623 | case DW_AT_type: |
| 2624 | type_ref = form_value; |
| 2625 | break; |
| 2626 | case DW_AT_data_member_location: |
| 2627 | if (auto maybe_offset = |
| 2628 | ExtractDataMemberLocation(die, form_value, module_sp)) |
| 2629 | byte_offset = *maybe_offset; |
| 2630 | break; |
| 2631 | default: |
| 2632 | break; |
| 2633 | } |
| 2634 | } |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | VariantPart::VariantPart(const DWARFDIE &die, const DWARFDIE &parent_die, |
| 2639 | lldb::ModuleSP module_sp) |
| 2640 | : _members(), _discriminant(die, module_sp) { |
| 2641 | |
| 2642 | for (auto child : die.children()) { |
| 2643 | if (child.Tag() == llvm::dwarf::DW_TAG_variant) { |
| 2644 | _members.push_back(x: VariantMember(child, module_sp)); |
| 2645 | } |
| 2646 | } |
| 2647 | } |
| 2648 | |
| 2649 | std::vector<VariantMember> &VariantPart::members() { return this->_members; } |
| 2650 | |
| 2651 | DiscriminantValue &VariantPart::discriminant() { return this->_discriminant; } |
| 2652 | |
| 2653 | DWARFASTParserClang::MemberAttributes::MemberAttributes( |
| 2654 | const DWARFDIE &die, const DWARFDIE &parent_die, ModuleSP module_sp) { |
| 2655 | DWARFAttributes attributes = die.GetAttributes(); |
| 2656 | for (size_t i = 0; i < attributes.Size(); ++i) { |
| 2657 | const dw_attr_t attr = attributes.AttributeAtIndex(i); |
| 2658 | DWARFFormValue form_value; |
| 2659 | if (attributes.ExtractFormValueAtIndex(i, form_value)) { |
| 2660 | switch (attr) { |
| 2661 | case DW_AT_name: |
| 2662 | name = form_value.AsCString(); |
| 2663 | break; |
| 2664 | case DW_AT_type: |
| 2665 | encoding_form = form_value; |
| 2666 | break; |
| 2667 | case DW_AT_bit_offset: |
| 2668 | bit_offset = form_value.Signed(); |
| 2669 | break; |
| 2670 | case DW_AT_bit_size: |
| 2671 | bit_size = form_value.Unsigned(); |
| 2672 | break; |
| 2673 | case DW_AT_byte_size: |
| 2674 | byte_size = form_value.Unsigned(); |
| 2675 | break; |
| 2676 | case DW_AT_const_value: |
| 2677 | const_value_form = form_value; |
| 2678 | break; |
| 2679 | case DW_AT_data_bit_offset: |
| 2680 | data_bit_offset = form_value.Unsigned(); |
| 2681 | break; |
| 2682 | case DW_AT_data_member_location: |
| 2683 | if (auto maybe_offset = |
| 2684 | ExtractDataMemberLocation(die, form_value, module_sp)) |
| 2685 | member_byte_offset = *maybe_offset; |
| 2686 | break; |
| 2687 | |
| 2688 | case DW_AT_accessibility: |
| 2689 | accessibility = |
| 2690 | DWARFASTParser::GetAccessTypeFromDWARF(dwarf_accessibility: form_value.Unsigned()); |
| 2691 | break; |
| 2692 | case DW_AT_artificial: |
| 2693 | is_artificial = form_value.Boolean(); |
| 2694 | break; |
| 2695 | case DW_AT_declaration: |
| 2696 | is_declaration = form_value.Boolean(); |
| 2697 | break; |
| 2698 | default: |
| 2699 | break; |
| 2700 | } |
| 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | // Clang has a DWARF generation bug where sometimes it represents |
| 2705 | // fields that are references with bad byte size and bit size/offset |
| 2706 | // information such as: |
| 2707 | // |
| 2708 | // DW_AT_byte_size( 0x00 ) |
| 2709 | // DW_AT_bit_size( 0x40 ) |
| 2710 | // DW_AT_bit_offset( 0xffffffffffffffc0 ) |
| 2711 | // |
| 2712 | // So check the bit offset to make sure it is sane, and if the values |
| 2713 | // are not sane, remove them. If we don't do this then we will end up |
| 2714 | // with a crash if we try to use this type in an expression when clang |
| 2715 | // becomes unhappy with its recycled debug info. |
| 2716 | if (byte_size.value_or(u: 0) == 0 && bit_offset < 0) { |
| 2717 | bit_size = 0; |
| 2718 | bit_offset = 0; |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | PropertyAttributes::PropertyAttributes(const DWARFDIE &die) { |
| 2723 | |
| 2724 | DWARFAttributes attributes = die.GetAttributes(); |
| 2725 | for (size_t i = 0; i < attributes.Size(); ++i) { |
| 2726 | const dw_attr_t attr = attributes.AttributeAtIndex(i); |
| 2727 | DWARFFormValue form_value; |
| 2728 | if (attributes.ExtractFormValueAtIndex(i, form_value)) { |
| 2729 | switch (attr) { |
| 2730 | case DW_AT_APPLE_property_name: |
| 2731 | prop_name = form_value.AsCString(); |
| 2732 | break; |
| 2733 | case DW_AT_APPLE_property_getter: |
| 2734 | prop_getter_name = form_value.AsCString(); |
| 2735 | break; |
| 2736 | case DW_AT_APPLE_property_setter: |
| 2737 | prop_setter_name = form_value.AsCString(); |
| 2738 | break; |
| 2739 | case DW_AT_APPLE_property_attribute: |
| 2740 | prop_attributes = form_value.Unsigned(); |
| 2741 | break; |
| 2742 | default: |
| 2743 | break; |
| 2744 | } |
| 2745 | } |
| 2746 | } |
| 2747 | |
| 2748 | if (!prop_name) |
| 2749 | return; |
| 2750 | ConstString fixed_setter; |
| 2751 | |
| 2752 | // Check if the property getter/setter were provided as full names. |
| 2753 | // We want basenames, so we extract them. |
| 2754 | if (prop_getter_name && prop_getter_name[0] == '-') { |
| 2755 | std::optional<const ObjCLanguage::ObjCMethodName> prop_getter_method = |
| 2756 | ObjCLanguage::ObjCMethodName::Create(name: prop_getter_name, strict: true); |
| 2757 | if (prop_getter_method) |
| 2758 | prop_getter_name = |
| 2759 | ConstString(prop_getter_method->GetSelector()).GetCString(); |
| 2760 | } |
| 2761 | |
| 2762 | if (prop_setter_name && prop_setter_name[0] == '-') { |
| 2763 | std::optional<const ObjCLanguage::ObjCMethodName> prop_setter_method = |
| 2764 | ObjCLanguage::ObjCMethodName::Create(name: prop_setter_name, strict: true); |
| 2765 | if (prop_setter_method) |
| 2766 | prop_setter_name = |
| 2767 | ConstString(prop_setter_method->GetSelector()).GetCString(); |
| 2768 | } |
| 2769 | |
| 2770 | // If the names haven't been provided, they need to be filled in. |
| 2771 | if (!prop_getter_name) |
| 2772 | prop_getter_name = prop_name; |
| 2773 | if (!prop_setter_name && prop_name[0] && |
| 2774 | !(prop_attributes & DW_APPLE_PROPERTY_readonly)) { |
| 2775 | StreamString ss; |
| 2776 | |
| 2777 | ss.Printf(format: "set%c%s:" , toupper(c: prop_name[0]), &prop_name[1]); |
| 2778 | |
| 2779 | fixed_setter.SetString(ss.GetString()); |
| 2780 | prop_setter_name = fixed_setter.GetCString(); |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | void DWARFASTParserClang::ParseObjCProperty( |
| 2785 | const DWARFDIE &die, const DWARFDIE &parent_die, |
| 2786 | const lldb_private::CompilerType &class_clang_type, |
| 2787 | DelayedPropertyList &delayed_properties) { |
| 2788 | // This function can only parse DW_TAG_APPLE_property. |
| 2789 | assert(die.Tag() == DW_TAG_APPLE_property); |
| 2790 | |
| 2791 | ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule(); |
| 2792 | |
| 2793 | const MemberAttributes attrs(die, parent_die, module_sp); |
| 2794 | const PropertyAttributes propAttrs(die); |
| 2795 | |
| 2796 | if (!propAttrs.prop_name) { |
| 2797 | module_sp->ReportError(format: "{0:x8}: DW_TAG_APPLE_property has no name." , |
| 2798 | args: die.GetID()); |
| 2799 | return; |
| 2800 | } |
| 2801 | |
| 2802 | Type *member_type = die.ResolveTypeUID(die: attrs.encoding_form.Reference()); |
| 2803 | if (!member_type) { |
| 2804 | module_sp->ReportError( |
| 2805 | format: "{0:x8}: DW_TAG_APPLE_property '{1}' refers to type {2:x16}" |
| 2806 | " which was unable to be parsed" , |
| 2807 | args: die.GetID(), args: propAttrs.prop_name, |
| 2808 | args: attrs.encoding_form.Reference().GetOffset()); |
| 2809 | return; |
| 2810 | } |
| 2811 | |
| 2812 | ClangASTMetadata metadata; |
| 2813 | metadata.SetUserID(die.GetID()); |
| 2814 | delayed_properties.emplace_back( |
| 2815 | args: class_clang_type, args: propAttrs.prop_name, |
| 2816 | args: member_type->GetLayoutCompilerType(), args: propAttrs.prop_setter_name, |
| 2817 | args: propAttrs.prop_getter_name, args: propAttrs.prop_attributes, args&: metadata); |
| 2818 | } |
| 2819 | |
| 2820 | llvm::Expected<llvm::APInt> DWARFASTParserClang::( |
| 2821 | const CompilerType &int_type, const DWARFFormValue &form_value) const { |
| 2822 | clang::QualType qt = ClangUtil::GetQualType(ct: int_type); |
| 2823 | assert(qt->isIntegralOrEnumerationType()); |
| 2824 | auto ts_ptr = int_type.GetTypeSystem<TypeSystemClang>(); |
| 2825 | if (!ts_ptr) |
| 2826 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 2827 | S: "TypeSystem not clang" ); |
| 2828 | TypeSystemClang &ts = *ts_ptr; |
| 2829 | clang::ASTContext &ast = ts.getASTContext(); |
| 2830 | |
| 2831 | const unsigned type_bits = ast.getIntWidth(T: qt); |
| 2832 | const bool is_unsigned = qt->isUnsignedIntegerType(); |
| 2833 | |
| 2834 | // The maximum int size supported at the moment by this function. Limited |
| 2835 | // by the uint64_t return type of DWARFFormValue::Signed/Unsigned. |
| 2836 | constexpr std::size_t max_bit_size = 64; |
| 2837 | |
| 2838 | // For values bigger than 64 bit (e.g. __int128_t values), |
| 2839 | // DWARFFormValue's Signed/Unsigned functions will return wrong results so |
| 2840 | // emit an error for now. |
| 2841 | if (type_bits > max_bit_size) { |
| 2842 | auto msg = llvm::formatv(Fmt: "Can only parse integers with up to {0} bits, but " |
| 2843 | "given integer has {1} bits." , |
| 2844 | Vals: max_bit_size, Vals: type_bits); |
| 2845 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), S: msg.str()); |
| 2846 | } |
| 2847 | |
| 2848 | // Construct an APInt with the maximum bit size and the given integer. |
| 2849 | llvm::APInt result(max_bit_size, form_value.Unsigned(), !is_unsigned); |
| 2850 | |
| 2851 | // Calculate how many bits are required to represent the input value. |
| 2852 | // For unsigned types, take the number of active bits in the APInt. |
| 2853 | // For signed types, ask APInt how many bits are required to represent the |
| 2854 | // signed integer. |
| 2855 | const unsigned required_bits = |
| 2856 | is_unsigned ? result.getActiveBits() : result.getSignificantBits(); |
| 2857 | |
| 2858 | // If the input value doesn't fit into the integer type, return an error. |
| 2859 | if (required_bits > type_bits) { |
| 2860 | std::string value_as_str = is_unsigned |
| 2861 | ? std::to_string(val: form_value.Unsigned()) |
| 2862 | : std::to_string(val: form_value.Signed()); |
| 2863 | auto msg = llvm::formatv(Fmt: "Can't store {0} value {1} in integer with {2} " |
| 2864 | "bits." , |
| 2865 | Vals: (is_unsigned ? "unsigned" : "signed" ), |
| 2866 | Vals&: value_as_str, Vals: type_bits); |
| 2867 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), S: msg.str()); |
| 2868 | } |
| 2869 | |
| 2870 | // Trim the result to the bit width our the int type. |
| 2871 | if (result.getBitWidth() > type_bits) |
| 2872 | result = result.trunc(width: type_bits); |
| 2873 | return result; |
| 2874 | } |
| 2875 | |
| 2876 | void DWARFASTParserClang::CreateStaticMemberVariable( |
| 2877 | const DWARFDIE &die, const MemberAttributes &attrs, |
| 2878 | const lldb_private::CompilerType &class_clang_type) { |
| 2879 | Log *log = GetLog(mask: DWARFLog::TypeCompletion | DWARFLog::Lookups); |
| 2880 | assert(die.Tag() == DW_TAG_member || die.Tag() == DW_TAG_variable); |
| 2881 | |
| 2882 | Type *var_type = die.ResolveTypeUID(die: attrs.encoding_form.Reference()); |
| 2883 | |
| 2884 | if (!var_type) |
| 2885 | return; |
| 2886 | |
| 2887 | auto accessibility = |
| 2888 | attrs.accessibility == eAccessNone ? eAccessPublic : attrs.accessibility; |
| 2889 | |
| 2890 | CompilerType ct = var_type->GetForwardCompilerType(); |
| 2891 | clang::VarDecl *v = TypeSystemClang::AddVariableToRecordType( |
| 2892 | type: class_clang_type, name: attrs.name, var_type: ct, access: accessibility); |
| 2893 | if (!v) { |
| 2894 | LLDB_LOG(log, "Failed to add variable to the record type" ); |
| 2895 | return; |
| 2896 | } |
| 2897 | |
| 2898 | bool unused; |
| 2899 | // TODO: Support float/double static members as well. |
| 2900 | if (!ct.IsIntegerOrEnumerationType(is_signed&: unused) || !attrs.const_value_form) |
| 2901 | return; |
| 2902 | |
| 2903 | llvm::Expected<llvm::APInt> const_value_or_err = |
| 2904 | ExtractIntFromFormValue(int_type: ct, form_value: *attrs.const_value_form); |
| 2905 | if (!const_value_or_err) { |
| 2906 | LLDB_LOG_ERROR(log, const_value_or_err.takeError(), |
| 2907 | "Failed to add const value to variable {1}: {0}" , |
| 2908 | v->getQualifiedNameAsString()); |
| 2909 | return; |
| 2910 | } |
| 2911 | |
| 2912 | TypeSystemClang::SetIntegerInitializerForVariable(var: v, init_value: *const_value_or_err); |
| 2913 | } |
| 2914 | |
| 2915 | void DWARFASTParserClang::ParseSingleMember( |
| 2916 | const DWARFDIE &die, const DWARFDIE &parent_die, |
| 2917 | const lldb_private::CompilerType &class_clang_type, |
| 2918 | lldb::AccessType default_accessibility, |
| 2919 | lldb_private::ClangASTImporter::LayoutInfo &layout_info, |
| 2920 | FieldInfo &last_field_info) { |
| 2921 | // This function can only parse DW_TAG_member. |
| 2922 | assert(die.Tag() == DW_TAG_member); |
| 2923 | |
| 2924 | ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule(); |
| 2925 | const dw_tag_t tag = die.Tag(); |
| 2926 | // Get the parent byte size so we can verify any members will fit |
| 2927 | const uint64_t parent_byte_size = |
| 2928 | parent_die.GetAttributeValueAsUnsigned(attr: DW_AT_byte_size, UINT64_MAX); |
| 2929 | const uint64_t parent_bit_size = |
| 2930 | parent_byte_size == UINT64_MAX ? UINT64_MAX : parent_byte_size * 8; |
| 2931 | |
| 2932 | const MemberAttributes attrs(die, parent_die, module_sp); |
| 2933 | |
| 2934 | // Handle static members, which are typically members without |
| 2935 | // locations. However, GCC doesn't emit DW_AT_data_member_location |
| 2936 | // for any union members (regardless of linkage). |
| 2937 | // Non-normative text pre-DWARFv5 recommends marking static |
| 2938 | // data members with an DW_AT_external flag. Clang emits this consistently |
| 2939 | // whereas GCC emits it only for static data members if not part of an |
| 2940 | // anonymous namespace. The flag that is consistently emitted for static |
| 2941 | // data members is DW_AT_declaration, so we check it instead. |
| 2942 | // The following block is only necessary to support DWARFv4 and earlier. |
| 2943 | // Starting with DWARFv5, static data members are marked DW_AT_variable so we |
| 2944 | // can consistently detect them on both GCC and Clang without below heuristic. |
| 2945 | if (attrs.member_byte_offset == UINT32_MAX && |
| 2946 | attrs.data_bit_offset == UINT64_MAX && attrs.is_declaration) { |
| 2947 | CreateStaticMemberVariable(die, attrs, class_clang_type); |
| 2948 | return; |
| 2949 | } |
| 2950 | |
| 2951 | Type *member_type = die.ResolveTypeUID(die: attrs.encoding_form.Reference()); |
| 2952 | if (!member_type) { |
| 2953 | if (attrs.name) |
| 2954 | module_sp->ReportError( |
| 2955 | format: "{0:x8}: DW_TAG_member '{1}' refers to type {2:x16}" |
| 2956 | " which was unable to be parsed" , |
| 2957 | args: die.GetID(), args: attrs.name, args: attrs.encoding_form.Reference().GetOffset()); |
| 2958 | else |
| 2959 | module_sp->ReportError(format: "{0:x8}: DW_TAG_member refers to type {1:x16}" |
| 2960 | " which was unable to be parsed" , |
| 2961 | args: die.GetID(), |
| 2962 | args: attrs.encoding_form.Reference().GetOffset()); |
| 2963 | return; |
| 2964 | } |
| 2965 | |
| 2966 | const uint64_t character_width = 8; |
| 2967 | CompilerType member_clang_type = member_type->GetLayoutCompilerType(); |
| 2968 | |
| 2969 | const auto accessibility = attrs.accessibility == eAccessNone |
| 2970 | ? default_accessibility |
| 2971 | : attrs.accessibility; |
| 2972 | |
| 2973 | uint64_t field_bit_offset = (attrs.member_byte_offset == UINT32_MAX |
| 2974 | ? 0 |
| 2975 | : (attrs.member_byte_offset * 8ULL)); |
| 2976 | |
| 2977 | if (attrs.bit_size > 0) { |
| 2978 | FieldInfo this_field_info; |
| 2979 | this_field_info.bit_offset = field_bit_offset; |
| 2980 | this_field_info.bit_size = attrs.bit_size; |
| 2981 | |
| 2982 | if (attrs.data_bit_offset != UINT64_MAX) { |
| 2983 | this_field_info.bit_offset = attrs.data_bit_offset; |
| 2984 | } else { |
| 2985 | auto byte_size = attrs.byte_size; |
| 2986 | if (!byte_size) |
| 2987 | byte_size = llvm::expectedToOptional(E: member_type->GetByteSize(exe_scope: nullptr)); |
| 2988 | |
| 2989 | ObjectFile *objfile = die.GetDWARF()->GetObjectFile(); |
| 2990 | if (objfile->GetByteOrder() == eByteOrderLittle) { |
| 2991 | this_field_info.bit_offset += byte_size.value_or(u: 0) * 8; |
| 2992 | this_field_info.bit_offset -= (attrs.bit_offset + attrs.bit_size); |
| 2993 | } else { |
| 2994 | this_field_info.bit_offset += attrs.bit_offset; |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | // The ObjC runtime knows the byte offset but we still need to provide |
| 2999 | // the bit-offset in the layout. It just means something different then |
| 3000 | // what it does in C and C++. So we skip this check for ObjC types. |
| 3001 | // |
| 3002 | // We also skip this for fields of a union since they will all have a |
| 3003 | // zero offset. |
| 3004 | if (!TypeSystemClang::IsObjCObjectOrInterfaceType(type: class_clang_type) && |
| 3005 | !(parent_die.Tag() == DW_TAG_union_type && |
| 3006 | this_field_info.bit_offset == 0) && |
| 3007 | ((this_field_info.bit_offset >= parent_bit_size) || |
| 3008 | (last_field_info.IsBitfield() && |
| 3009 | !last_field_info.NextBitfieldOffsetIsValid( |
| 3010 | next_bit_offset: this_field_info.bit_offset)))) { |
| 3011 | ObjectFile *objfile = die.GetDWARF()->GetObjectFile(); |
| 3012 | objfile->GetModule()->ReportWarning( |
| 3013 | format: "{0:x16}: {1} ({2}) bitfield named \"{3}\" has invalid " |
| 3014 | "bit offset ({4:x8}) member will be ignored. Please file a bug " |
| 3015 | "against the " |
| 3016 | "compiler and include the preprocessed output for {5}\n" , |
| 3017 | args: die.GetID(), args: DW_TAG_value_to_name(tag), args: tag, args: attrs.name, |
| 3018 | args&: this_field_info.bit_offset, args: GetUnitName(die: parent_die).c_str()); |
| 3019 | return; |
| 3020 | } |
| 3021 | |
| 3022 | // Update the field bit offset we will report for layout |
| 3023 | field_bit_offset = this_field_info.bit_offset; |
| 3024 | |
| 3025 | // Objective-C has invalid DW_AT_bit_offset values in older |
| 3026 | // versions of clang, so we have to be careful and only insert |
| 3027 | // unnamed bitfields if we have a new enough clang. |
| 3028 | bool detect_unnamed_bitfields = true; |
| 3029 | |
| 3030 | if (TypeSystemClang::IsObjCObjectOrInterfaceType(type: class_clang_type)) |
| 3031 | detect_unnamed_bitfields = |
| 3032 | die.GetCU()->Supports_unnamed_objc_bitfields(); |
| 3033 | |
| 3034 | if (detect_unnamed_bitfields) |
| 3035 | AddUnnamedBitfieldToRecordTypeIfNeeded(class_layout_info&: layout_info, class_clang_type, |
| 3036 | previous_field: last_field_info, current_field: this_field_info); |
| 3037 | |
| 3038 | last_field_info = this_field_info; |
| 3039 | last_field_info.SetIsBitfield(true); |
| 3040 | } else { |
| 3041 | FieldInfo this_field_info; |
| 3042 | this_field_info.is_bitfield = false; |
| 3043 | this_field_info.bit_offset = field_bit_offset; |
| 3044 | |
| 3045 | // TODO: we shouldn't silently ignore the bit_size if we fail |
| 3046 | // to GetByteSize. |
| 3047 | if (std::optional<uint64_t> clang_type_size = |
| 3048 | llvm::expectedToOptional(E: member_type->GetByteSize(exe_scope: nullptr))) { |
| 3049 | this_field_info.bit_size = *clang_type_size * character_width; |
| 3050 | } |
| 3051 | |
| 3052 | if (this_field_info.GetFieldEnd() <= last_field_info.GetEffectiveFieldEnd()) |
| 3053 | this_field_info.SetEffectiveFieldEnd( |
| 3054 | last_field_info.GetEffectiveFieldEnd()); |
| 3055 | |
| 3056 | last_field_info = this_field_info; |
| 3057 | } |
| 3058 | |
| 3059 | // Don't turn artificial members such as vtable pointers into real FieldDecls |
| 3060 | // in our AST. Clang will re-create those articial members and they would |
| 3061 | // otherwise just overlap in the layout with the FieldDecls we add here. |
| 3062 | // This needs to be done after updating FieldInfo which keeps track of where |
| 3063 | // field start/end so we don't later try to fill the space of this |
| 3064 | // artificial member with (unnamed bitfield) padding. |
| 3065 | if (attrs.is_artificial && ShouldIgnoreArtificialField(FieldName: attrs.name)) { |
| 3066 | last_field_info.SetIsArtificial(true); |
| 3067 | return; |
| 3068 | } |
| 3069 | |
| 3070 | if (!member_clang_type.IsCompleteType()) |
| 3071 | member_clang_type.GetCompleteType(); |
| 3072 | |
| 3073 | { |
| 3074 | // Older versions of clang emit the same DWARF for array[0] and array[1]. If |
| 3075 | // the current field is at the end of the structure, then there is |
| 3076 | // definitely no room for extra elements and we override the type to |
| 3077 | // array[0]. This was fixed by f454dfb6b5af. |
| 3078 | CompilerType member_array_element_type; |
| 3079 | uint64_t member_array_size; |
| 3080 | bool member_array_is_incomplete; |
| 3081 | |
| 3082 | if (member_clang_type.IsArrayType(element_type: &member_array_element_type, |
| 3083 | size: &member_array_size, |
| 3084 | is_incomplete: &member_array_is_incomplete) && |
| 3085 | !member_array_is_incomplete) { |
| 3086 | uint64_t parent_byte_size = |
| 3087 | parent_die.GetAttributeValueAsUnsigned(attr: DW_AT_byte_size, UINT64_MAX); |
| 3088 | |
| 3089 | if (attrs.member_byte_offset >= parent_byte_size) { |
| 3090 | if (member_array_size != 1 && |
| 3091 | (member_array_size != 0 || |
| 3092 | attrs.member_byte_offset > parent_byte_size)) { |
| 3093 | module_sp->ReportError( |
| 3094 | format: "{0:x8}: DW_TAG_member '{1}' refers to type {2:x16}" |
| 3095 | " which extends beyond the bounds of {3:x8}" , |
| 3096 | args: die.GetID(), args: attrs.name, |
| 3097 | args: attrs.encoding_form.Reference().GetOffset(), args: parent_die.GetID()); |
| 3098 | } |
| 3099 | |
| 3100 | member_clang_type = |
| 3101 | m_ast.CreateArrayType(element_type: member_array_element_type, element_count: 0, is_vector: false); |
| 3102 | } |
| 3103 | } |
| 3104 | } |
| 3105 | |
| 3106 | TypeSystemClang::RequireCompleteType(type: member_clang_type); |
| 3107 | |
| 3108 | clang::FieldDecl *field_decl = TypeSystemClang::AddFieldToRecordType( |
| 3109 | type: class_clang_type, name: attrs.name, field_type: member_clang_type, access: accessibility, |
| 3110 | bitfield_bit_size: attrs.bit_size); |
| 3111 | |
| 3112 | m_ast.SetMetadataAsUserID(field_decl, die.GetID()); |
| 3113 | |
| 3114 | layout_info.field_offsets.insert( |
| 3115 | KV: std::make_pair(x&: field_decl, y&: field_bit_offset)); |
| 3116 | } |
| 3117 | |
| 3118 | bool DWARFASTParserClang::ParseChildMembers( |
| 3119 | const DWARFDIE &parent_die, const CompilerType &class_clang_type, |
| 3120 | std::vector<std::unique_ptr<clang::CXXBaseSpecifier>> &base_classes, |
| 3121 | std::vector<DWARFDIE> &member_function_dies, |
| 3122 | std::vector<DWARFDIE> &contained_type_dies, |
| 3123 | DelayedPropertyList &delayed_properties, |
| 3124 | const AccessType default_accessibility, |
| 3125 | ClangASTImporter::LayoutInfo &layout_info) { |
| 3126 | if (!parent_die) |
| 3127 | return false; |
| 3128 | |
| 3129 | FieldInfo last_field_info; |
| 3130 | |
| 3131 | ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule(); |
| 3132 | auto ast = class_clang_type.GetTypeSystem<TypeSystemClang>(); |
| 3133 | if (ast == nullptr) |
| 3134 | return false; |
| 3135 | |
| 3136 | for (DWARFDIE die : parent_die.children()) { |
| 3137 | dw_tag_t tag = die.Tag(); |
| 3138 | |
| 3139 | switch (tag) { |
| 3140 | case DW_TAG_APPLE_property: |
| 3141 | ParseObjCProperty(die, parent_die, class_clang_type, delayed_properties); |
| 3142 | break; |
| 3143 | |
| 3144 | case DW_TAG_variant_part: |
| 3145 | if (die.GetCU()->GetDWARFLanguageType() == eLanguageTypeRust) { |
| 3146 | ParseRustVariantPart(die, parent_die, class_clang_type, |
| 3147 | default_accesibility: default_accessibility, layout_info); |
| 3148 | } |
| 3149 | break; |
| 3150 | |
| 3151 | case DW_TAG_variable: { |
| 3152 | const MemberAttributes attrs(die, parent_die, module_sp); |
| 3153 | CreateStaticMemberVariable(die, attrs, class_clang_type); |
| 3154 | } break; |
| 3155 | case DW_TAG_member: |
| 3156 | ParseSingleMember(die, parent_die, class_clang_type, |
| 3157 | default_accessibility, layout_info, last_field_info); |
| 3158 | break; |
| 3159 | |
| 3160 | case DW_TAG_subprogram: |
| 3161 | // Let the type parsing code handle this one for us. |
| 3162 | member_function_dies.push_back(x: die); |
| 3163 | break; |
| 3164 | |
| 3165 | case DW_TAG_inheritance: |
| 3166 | ParseInheritance(die, parent_die, class_clang_type, default_accessibility, |
| 3167 | module_sp, base_classes, layout_info); |
| 3168 | break; |
| 3169 | |
| 3170 | default: |
| 3171 | if (llvm::dwarf::isType(T: tag)) |
| 3172 | contained_type_dies.push_back(x: die); |
| 3173 | break; |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | return true; |
| 3178 | } |
| 3179 | |
| 3180 | void DWARFASTParserClang::ParseChildParameters( |
| 3181 | clang::DeclContext *containing_decl_ctx, const DWARFDIE &parent_die, |
| 3182 | bool &is_variadic, bool &has_template_params, |
| 3183 | std::vector<CompilerType> &function_param_types, |
| 3184 | llvm::SmallVectorImpl<llvm::StringRef> &function_param_names) { |
| 3185 | if (!parent_die) |
| 3186 | return; |
| 3187 | |
| 3188 | for (DWARFDIE die : parent_die.children()) { |
| 3189 | const dw_tag_t tag = die.Tag(); |
| 3190 | switch (tag) { |
| 3191 | case DW_TAG_formal_parameter: { |
| 3192 | if (die.GetAttributeValueAsUnsigned(attr: DW_AT_artificial, fail_value: 0)) |
| 3193 | continue; |
| 3194 | |
| 3195 | DWARFDIE param_type_die = die.GetAttributeValueAsReferenceDIE(attr: DW_AT_type); |
| 3196 | |
| 3197 | Type *type = die.ResolveTypeUID(die: param_type_die); |
| 3198 | if (!type) |
| 3199 | break; |
| 3200 | |
| 3201 | function_param_names.emplace_back(Args: die.GetName()); |
| 3202 | function_param_types.push_back(x: type->GetForwardCompilerType()); |
| 3203 | } break; |
| 3204 | |
| 3205 | case DW_TAG_unspecified_parameters: |
| 3206 | is_variadic = true; |
| 3207 | break; |
| 3208 | |
| 3209 | case DW_TAG_template_type_parameter: |
| 3210 | case DW_TAG_template_value_parameter: |
| 3211 | case DW_TAG_GNU_template_parameter_pack: |
| 3212 | // The one caller of this was never using the template_param_infos, and |
| 3213 | // the local variable was taking up a large amount of stack space in |
| 3214 | // SymbolFileDWARF::ParseType() so this was removed. If we ever need the |
| 3215 | // template params back, we can add them back. |
| 3216 | // ParseTemplateDIE (dwarf_cu, die, template_param_infos); |
| 3217 | has_template_params = true; |
| 3218 | break; |
| 3219 | |
| 3220 | default: |
| 3221 | break; |
| 3222 | } |
| 3223 | } |
| 3224 | |
| 3225 | assert(function_param_names.size() == function_param_types.size()); |
| 3226 | } |
| 3227 | |
| 3228 | clang::Decl *DWARFASTParserClang::GetClangDeclForDIE(const DWARFDIE &die) { |
| 3229 | if (!die) |
| 3230 | return nullptr; |
| 3231 | |
| 3232 | switch (die.Tag()) { |
| 3233 | case DW_TAG_constant: |
| 3234 | case DW_TAG_formal_parameter: |
| 3235 | case DW_TAG_imported_declaration: |
| 3236 | case DW_TAG_imported_module: |
| 3237 | break; |
| 3238 | case DW_TAG_variable: |
| 3239 | // This means 'die' is a C++ static data member. |
| 3240 | // We don't want to create decls for such members |
| 3241 | // here. |
| 3242 | if (auto parent = die.GetParent(); |
| 3243 | parent.IsValid() && TagIsRecordType(tag: parent.Tag())) |
| 3244 | return nullptr; |
| 3245 | break; |
| 3246 | default: |
| 3247 | return nullptr; |
| 3248 | } |
| 3249 | |
| 3250 | DIEToDeclMap::iterator cache_pos = m_die_to_decl.find(Val: die.GetDIE()); |
| 3251 | if (cache_pos != m_die_to_decl.end()) |
| 3252 | return cache_pos->second; |
| 3253 | |
| 3254 | if (DWARFDIE spec_die = die.GetReferencedDIE(attr: DW_AT_specification)) { |
| 3255 | clang::Decl *decl = GetClangDeclForDIE(die: spec_die); |
| 3256 | m_die_to_decl[die.GetDIE()] = decl; |
| 3257 | return decl; |
| 3258 | } |
| 3259 | |
| 3260 | if (DWARFDIE abstract_origin_die = |
| 3261 | die.GetReferencedDIE(attr: DW_AT_abstract_origin)) { |
| 3262 | clang::Decl *decl = GetClangDeclForDIE(die: abstract_origin_die); |
| 3263 | m_die_to_decl[die.GetDIE()] = decl; |
| 3264 | return decl; |
| 3265 | } |
| 3266 | |
| 3267 | clang::Decl *decl = nullptr; |
| 3268 | switch (die.Tag()) { |
| 3269 | case DW_TAG_variable: |
| 3270 | case DW_TAG_constant: |
| 3271 | case DW_TAG_formal_parameter: { |
| 3272 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 3273 | Type *type = GetTypeForDIE(die); |
| 3274 | if (dwarf && type) { |
| 3275 | const char *name = die.GetName(); |
| 3276 | clang::DeclContext *decl_context = |
| 3277 | TypeSystemClang::DeclContextGetAsDeclContext( |
| 3278 | dc: dwarf->GetDeclContextContainingUID(uid: die.GetID())); |
| 3279 | decl = m_ast.CreateVariableDeclaration( |
| 3280 | decl_context, owning_module: GetOwningClangModule(die), name, |
| 3281 | type: ClangUtil::GetQualType(ct: type->GetForwardCompilerType())); |
| 3282 | } |
| 3283 | break; |
| 3284 | } |
| 3285 | case DW_TAG_imported_declaration: { |
| 3286 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 3287 | DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(attr: DW_AT_import); |
| 3288 | if (imported_uid) { |
| 3289 | CompilerDecl imported_decl = SymbolFileDWARF::GetDecl(die: imported_uid); |
| 3290 | if (imported_decl) { |
| 3291 | clang::DeclContext *decl_context = |
| 3292 | TypeSystemClang::DeclContextGetAsDeclContext( |
| 3293 | dc: dwarf->GetDeclContextContainingUID(uid: die.GetID())); |
| 3294 | if (clang::NamedDecl *clang_imported_decl = |
| 3295 | llvm::dyn_cast<clang::NamedDecl>( |
| 3296 | Val: (clang::Decl *)imported_decl.GetOpaqueDecl())) |
| 3297 | decl = m_ast.CreateUsingDeclaration( |
| 3298 | current_decl_ctx: decl_context, owning_module: OptionalClangModuleID(), target: clang_imported_decl); |
| 3299 | } |
| 3300 | } |
| 3301 | break; |
| 3302 | } |
| 3303 | case DW_TAG_imported_module: { |
| 3304 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 3305 | DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(attr: DW_AT_import); |
| 3306 | |
| 3307 | if (imported_uid) { |
| 3308 | CompilerDeclContext imported_decl_ctx = |
| 3309 | SymbolFileDWARF::GetDeclContext(die: imported_uid); |
| 3310 | if (imported_decl_ctx) { |
| 3311 | clang::DeclContext *decl_context = |
| 3312 | TypeSystemClang::DeclContextGetAsDeclContext( |
| 3313 | dc: dwarf->GetDeclContextContainingUID(uid: die.GetID())); |
| 3314 | if (clang::NamespaceDecl *ns_decl = |
| 3315 | TypeSystemClang::DeclContextGetAsNamespaceDecl( |
| 3316 | dc: imported_decl_ctx)) |
| 3317 | decl = m_ast.CreateUsingDirectiveDeclaration( |
| 3318 | decl_ctx: decl_context, owning_module: OptionalClangModuleID(), ns_decl); |
| 3319 | } |
| 3320 | } |
| 3321 | break; |
| 3322 | } |
| 3323 | default: |
| 3324 | break; |
| 3325 | } |
| 3326 | |
| 3327 | m_die_to_decl[die.GetDIE()] = decl; |
| 3328 | |
| 3329 | return decl; |
| 3330 | } |
| 3331 | |
| 3332 | clang::DeclContext * |
| 3333 | DWARFASTParserClang::GetClangDeclContextForDIE(const DWARFDIE &die) { |
| 3334 | if (die) { |
| 3335 | clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE(die); |
| 3336 | if (decl_ctx) |
| 3337 | return decl_ctx; |
| 3338 | |
| 3339 | bool try_parsing_type = true; |
| 3340 | switch (die.Tag()) { |
| 3341 | case DW_TAG_compile_unit: |
| 3342 | case DW_TAG_partial_unit: |
| 3343 | decl_ctx = m_ast.GetTranslationUnitDecl(); |
| 3344 | try_parsing_type = false; |
| 3345 | break; |
| 3346 | |
| 3347 | case DW_TAG_namespace: |
| 3348 | decl_ctx = ResolveNamespaceDIE(die); |
| 3349 | try_parsing_type = false; |
| 3350 | break; |
| 3351 | |
| 3352 | case DW_TAG_imported_declaration: |
| 3353 | decl_ctx = ResolveImportedDeclarationDIE(die); |
| 3354 | try_parsing_type = false; |
| 3355 | break; |
| 3356 | |
| 3357 | case DW_TAG_lexical_block: |
| 3358 | decl_ctx = GetDeclContextForBlock(die); |
| 3359 | try_parsing_type = false; |
| 3360 | break; |
| 3361 | |
| 3362 | default: |
| 3363 | break; |
| 3364 | } |
| 3365 | |
| 3366 | if (decl_ctx == nullptr && try_parsing_type) { |
| 3367 | Type *type = die.GetDWARF()->ResolveType(die); |
| 3368 | if (type) |
| 3369 | decl_ctx = GetCachedClangDeclContextForDIE(die); |
| 3370 | } |
| 3371 | |
| 3372 | if (decl_ctx) { |
| 3373 | LinkDeclContextToDIE(decl_ctx, die); |
| 3374 | return decl_ctx; |
| 3375 | } |
| 3376 | } |
| 3377 | return nullptr; |
| 3378 | } |
| 3379 | |
| 3380 | OptionalClangModuleID |
| 3381 | DWARFASTParserClang::GetOwningClangModule(const DWARFDIE &die) { |
| 3382 | if (!die.IsValid()) |
| 3383 | return {}; |
| 3384 | |
| 3385 | for (DWARFDIE parent = die.GetParent(); parent.IsValid(); |
| 3386 | parent = parent.GetParent()) { |
| 3387 | const dw_tag_t tag = parent.Tag(); |
| 3388 | if (tag == DW_TAG_module) { |
| 3389 | DWARFDIE module_die = parent; |
| 3390 | auto it = m_die_to_module.find(Val: module_die.GetDIE()); |
| 3391 | if (it != m_die_to_module.end()) |
| 3392 | return it->second; |
| 3393 | const char *name = |
| 3394 | module_die.GetAttributeValueAsString(attr: DW_AT_name, fail_value: nullptr); |
| 3395 | if (!name) |
| 3396 | return {}; |
| 3397 | |
| 3398 | OptionalClangModuleID id = |
| 3399 | m_ast.GetOrCreateClangModule(name, parent: GetOwningClangModule(die: module_die)); |
| 3400 | m_die_to_module.insert(KV: {module_die.GetDIE(), id}); |
| 3401 | return id; |
| 3402 | } |
| 3403 | } |
| 3404 | return {}; |
| 3405 | } |
| 3406 | |
| 3407 | static bool IsSubroutine(const DWARFDIE &die) { |
| 3408 | switch (die.Tag()) { |
| 3409 | case DW_TAG_subprogram: |
| 3410 | case DW_TAG_inlined_subroutine: |
| 3411 | return true; |
| 3412 | default: |
| 3413 | return false; |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | static DWARFDIE GetContainingFunctionWithAbstractOrigin(const DWARFDIE &die) { |
| 3418 | for (DWARFDIE candidate = die; candidate; candidate = candidate.GetParent()) { |
| 3419 | if (IsSubroutine(die: candidate)) { |
| 3420 | if (candidate.GetReferencedDIE(attr: DW_AT_abstract_origin)) { |
| 3421 | return candidate; |
| 3422 | } else { |
| 3423 | return DWARFDIE(); |
| 3424 | } |
| 3425 | } |
| 3426 | } |
| 3427 | assert(0 && "Shouldn't call GetContainingFunctionWithAbstractOrigin on " |
| 3428 | "something not in a function" ); |
| 3429 | return DWARFDIE(); |
| 3430 | } |
| 3431 | |
| 3432 | static DWARFDIE FindAnyChildWithAbstractOrigin(const DWARFDIE &context) { |
| 3433 | for (DWARFDIE candidate : context.children()) { |
| 3434 | if (candidate.GetReferencedDIE(attr: DW_AT_abstract_origin)) { |
| 3435 | return candidate; |
| 3436 | } |
| 3437 | } |
| 3438 | return DWARFDIE(); |
| 3439 | } |
| 3440 | |
| 3441 | static DWARFDIE FindFirstChildWithAbstractOrigin(const DWARFDIE &block, |
| 3442 | const DWARFDIE &function) { |
| 3443 | assert(IsSubroutine(function)); |
| 3444 | for (DWARFDIE context = block; context != function.GetParent(); |
| 3445 | context = context.GetParent()) { |
| 3446 | assert(!IsSubroutine(context) || context == function); |
| 3447 | if (DWARFDIE child = FindAnyChildWithAbstractOrigin(context)) { |
| 3448 | return child; |
| 3449 | } |
| 3450 | } |
| 3451 | return DWARFDIE(); |
| 3452 | } |
| 3453 | |
| 3454 | clang::DeclContext * |
| 3455 | DWARFASTParserClang::GetDeclContextForBlock(const DWARFDIE &die) { |
| 3456 | assert(die.Tag() == DW_TAG_lexical_block); |
| 3457 | DWARFDIE containing_function_with_abstract_origin = |
| 3458 | GetContainingFunctionWithAbstractOrigin(die); |
| 3459 | if (!containing_function_with_abstract_origin) { |
| 3460 | return (clang::DeclContext *)ResolveBlockDIE(die); |
| 3461 | } |
| 3462 | DWARFDIE child = FindFirstChildWithAbstractOrigin( |
| 3463 | block: die, function: containing_function_with_abstract_origin); |
| 3464 | CompilerDeclContext decl_context = |
| 3465 | GetDeclContextContainingUIDFromDWARF(die: child); |
| 3466 | return (clang::DeclContext *)decl_context.GetOpaqueDeclContext(); |
| 3467 | } |
| 3468 | |
| 3469 | clang::BlockDecl *DWARFASTParserClang::ResolveBlockDIE(const DWARFDIE &die) { |
| 3470 | if (die && die.Tag() == DW_TAG_lexical_block) { |
| 3471 | clang::BlockDecl *decl = |
| 3472 | llvm::cast_or_null<clang::BlockDecl>(Val: m_die_to_decl_ctx[die.GetDIE()]); |
| 3473 | |
| 3474 | if (!decl) { |
| 3475 | DWARFDIE decl_context_die; |
| 3476 | clang::DeclContext *decl_context = |
| 3477 | GetClangDeclContextContainingDIE(die, decl_ctx_die: &decl_context_die); |
| 3478 | decl = |
| 3479 | m_ast.CreateBlockDeclaration(ctx: decl_context, owning_module: GetOwningClangModule(die)); |
| 3480 | |
| 3481 | if (decl) |
| 3482 | LinkDeclContextToDIE(decl_ctx: (clang::DeclContext *)decl, die); |
| 3483 | } |
| 3484 | |
| 3485 | return decl; |
| 3486 | } |
| 3487 | return nullptr; |
| 3488 | } |
| 3489 | |
| 3490 | clang::NamespaceDecl * |
| 3491 | DWARFASTParserClang::ResolveNamespaceDIE(const DWARFDIE &die) { |
| 3492 | if (die && die.Tag() == DW_TAG_namespace) { |
| 3493 | // See if we already parsed this namespace DIE and associated it with a |
| 3494 | // uniqued namespace declaration |
| 3495 | clang::NamespaceDecl *namespace_decl = |
| 3496 | static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die.GetDIE()]); |
| 3497 | if (namespace_decl) |
| 3498 | return namespace_decl; |
| 3499 | else { |
| 3500 | const char *namespace_name = die.GetName(); |
| 3501 | clang::DeclContext *containing_decl_ctx = |
| 3502 | GetClangDeclContextContainingDIE(die, decl_ctx_die: nullptr); |
| 3503 | bool is_inline = |
| 3504 | die.GetAttributeValueAsUnsigned(attr: DW_AT_export_symbols, fail_value: 0) != 0; |
| 3505 | |
| 3506 | namespace_decl = m_ast.GetUniqueNamespaceDeclaration( |
| 3507 | name: namespace_name, decl_ctx: containing_decl_ctx, owning_module: GetOwningClangModule(die), |
| 3508 | is_inline); |
| 3509 | |
| 3510 | if (namespace_decl) |
| 3511 | LinkDeclContextToDIE(decl_ctx: (clang::DeclContext *)namespace_decl, die); |
| 3512 | return namespace_decl; |
| 3513 | } |
| 3514 | } |
| 3515 | return nullptr; |
| 3516 | } |
| 3517 | |
| 3518 | clang::NamespaceDecl * |
| 3519 | DWARFASTParserClang::ResolveImportedDeclarationDIE(const DWARFDIE &die) { |
| 3520 | assert(die && die.Tag() == DW_TAG_imported_declaration); |
| 3521 | |
| 3522 | // See if we cached a NamespaceDecl for this imported declaration |
| 3523 | // already |
| 3524 | auto it = m_die_to_decl_ctx.find(Val: die.GetDIE()); |
| 3525 | if (it != m_die_to_decl_ctx.end()) |
| 3526 | return static_cast<clang::NamespaceDecl *>(it->getSecond()); |
| 3527 | |
| 3528 | clang::NamespaceDecl *namespace_decl = nullptr; |
| 3529 | |
| 3530 | const DWARFDIE imported_uid = |
| 3531 | die.GetAttributeValueAsReferenceDIE(attr: DW_AT_import); |
| 3532 | if (!imported_uid) |
| 3533 | return nullptr; |
| 3534 | |
| 3535 | switch (imported_uid.Tag()) { |
| 3536 | case DW_TAG_imported_declaration: |
| 3537 | namespace_decl = ResolveImportedDeclarationDIE(die: imported_uid); |
| 3538 | break; |
| 3539 | case DW_TAG_namespace: |
| 3540 | namespace_decl = ResolveNamespaceDIE(die: imported_uid); |
| 3541 | break; |
| 3542 | default: |
| 3543 | return nullptr; |
| 3544 | } |
| 3545 | |
| 3546 | if (!namespace_decl) |
| 3547 | return nullptr; |
| 3548 | |
| 3549 | LinkDeclContextToDIE(namespace_decl, die); |
| 3550 | |
| 3551 | return namespace_decl; |
| 3552 | } |
| 3553 | |
| 3554 | clang::DeclContext *DWARFASTParserClang::GetClangDeclContextContainingDIE( |
| 3555 | const DWARFDIE &die, DWARFDIE *decl_ctx_die_copy) { |
| 3556 | SymbolFileDWARF *dwarf = die.GetDWARF(); |
| 3557 | |
| 3558 | DWARFDIE decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE(die); |
| 3559 | |
| 3560 | if (decl_ctx_die_copy) |
| 3561 | *decl_ctx_die_copy = decl_ctx_die; |
| 3562 | |
| 3563 | if (decl_ctx_die) { |
| 3564 | clang::DeclContext *clang_decl_ctx = |
| 3565 | GetClangDeclContextForDIE(die: decl_ctx_die); |
| 3566 | if (clang_decl_ctx) |
| 3567 | return clang_decl_ctx; |
| 3568 | } |
| 3569 | return m_ast.GetTranslationUnitDecl(); |
| 3570 | } |
| 3571 | |
| 3572 | clang::DeclContext * |
| 3573 | DWARFASTParserClang::GetCachedClangDeclContextForDIE(const DWARFDIE &die) { |
| 3574 | if (die) { |
| 3575 | DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(Val: die.GetDIE()); |
| 3576 | if (pos != m_die_to_decl_ctx.end()) |
| 3577 | return pos->second; |
| 3578 | } |
| 3579 | return nullptr; |
| 3580 | } |
| 3581 | |
| 3582 | void DWARFASTParserClang::LinkDeclContextToDIE(clang::DeclContext *decl_ctx, |
| 3583 | const DWARFDIE &die) { |
| 3584 | m_die_to_decl_ctx[die.GetDIE()] = decl_ctx; |
| 3585 | // There can be many DIEs for a single decl context |
| 3586 | // m_decl_ctx_to_die[decl_ctx].insert(die.GetDIE()); |
| 3587 | m_decl_ctx_to_die.insert(x: std::make_pair(x&: decl_ctx, y: die)); |
| 3588 | } |
| 3589 | |
| 3590 | bool DWARFASTParserClang::CopyUniqueClassMethodTypes( |
| 3591 | const DWARFDIE &src_class_die, const DWARFDIE &dst_class_die, |
| 3592 | lldb_private::Type *class_type, std::vector<DWARFDIE> &failures) { |
| 3593 | if (!class_type || !src_class_die || !dst_class_die) |
| 3594 | return false; |
| 3595 | if (src_class_die.Tag() != dst_class_die.Tag()) |
| 3596 | return false; |
| 3597 | |
| 3598 | // We need to complete the class type so we can get all of the method types |
| 3599 | // parsed so we can then unique those types to their equivalent counterparts |
| 3600 | // in "dst_cu" and "dst_class_die" |
| 3601 | class_type->GetFullCompilerType(); |
| 3602 | |
| 3603 | auto gather = [](DWARFDIE die, UniqueCStringMap<DWARFDIE> &map, |
| 3604 | UniqueCStringMap<DWARFDIE> &map_artificial) { |
| 3605 | if (die.Tag() != DW_TAG_subprogram) |
| 3606 | return; |
| 3607 | // Make sure this is a declaration and not a concrete instance by looking |
| 3608 | // for DW_AT_declaration set to 1. Sometimes concrete function instances are |
| 3609 | // placed inside the class definitions and shouldn't be included in the list |
| 3610 | // of things that are tracking here. |
| 3611 | if (die.GetAttributeValueAsUnsigned(attr: DW_AT_declaration, fail_value: 0) != 1) |
| 3612 | return; |
| 3613 | |
| 3614 | if (const char *name = die.GetMangledName()) { |
| 3615 | ConstString const_name(name); |
| 3616 | if (die.GetAttributeValueAsUnsigned(attr: DW_AT_artificial, fail_value: 0)) |
| 3617 | map_artificial.Append(unique_cstr: const_name, value: die); |
| 3618 | else |
| 3619 | map.Append(unique_cstr: const_name, value: die); |
| 3620 | } |
| 3621 | }; |
| 3622 | |
| 3623 | UniqueCStringMap<DWARFDIE> src_name_to_die; |
| 3624 | UniqueCStringMap<DWARFDIE> dst_name_to_die; |
| 3625 | UniqueCStringMap<DWARFDIE> src_name_to_die_artificial; |
| 3626 | UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial; |
| 3627 | for (DWARFDIE src_die = src_class_die.GetFirstChild(); src_die.IsValid(); |
| 3628 | src_die = src_die.GetSibling()) { |
| 3629 | gather(src_die, src_name_to_die, src_name_to_die_artificial); |
| 3630 | } |
| 3631 | for (DWARFDIE dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid(); |
| 3632 | dst_die = dst_die.GetSibling()) { |
| 3633 | gather(dst_die, dst_name_to_die, dst_name_to_die_artificial); |
| 3634 | } |
| 3635 | const uint32_t src_size = src_name_to_die.GetSize(); |
| 3636 | const uint32_t dst_size = dst_name_to_die.GetSize(); |
| 3637 | |
| 3638 | // Is everything kosher so we can go through the members at top speed? |
| 3639 | bool fast_path = true; |
| 3640 | |
| 3641 | if (src_size != dst_size) |
| 3642 | fast_path = false; |
| 3643 | |
| 3644 | uint32_t idx; |
| 3645 | |
| 3646 | if (fast_path) { |
| 3647 | for (idx = 0; idx < src_size; ++idx) { |
| 3648 | DWARFDIE src_die = src_name_to_die.GetValueAtIndexUnchecked(idx); |
| 3649 | DWARFDIE dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx); |
| 3650 | |
| 3651 | if (src_die.Tag() != dst_die.Tag()) |
| 3652 | fast_path = false; |
| 3653 | |
| 3654 | const char *src_name = src_die.GetMangledName(); |
| 3655 | const char *dst_name = dst_die.GetMangledName(); |
| 3656 | |
| 3657 | // Make sure the names match |
| 3658 | if (src_name == dst_name || (strcmp(s1: src_name, s2: dst_name) == 0)) |
| 3659 | continue; |
| 3660 | |
| 3661 | fast_path = false; |
| 3662 | } |
| 3663 | } |
| 3664 | |
| 3665 | DWARFASTParserClang *src_dwarf_ast_parser = |
| 3666 | static_cast<DWARFASTParserClang *>( |
| 3667 | SymbolFileDWARF::GetDWARFParser(unit&: *src_class_die.GetCU())); |
| 3668 | DWARFASTParserClang *dst_dwarf_ast_parser = |
| 3669 | static_cast<DWARFASTParserClang *>( |
| 3670 | SymbolFileDWARF::GetDWARFParser(unit&: *dst_class_die.GetCU())); |
| 3671 | auto link = [&](DWARFDIE src, DWARFDIE dst) { |
| 3672 | auto &die_to_type = dst_class_die.GetDWARF()->GetDIEToType(); |
| 3673 | clang::DeclContext *dst_decl_ctx = |
| 3674 | dst_dwarf_ast_parser->m_die_to_decl_ctx[dst.GetDIE()]; |
| 3675 | if (dst_decl_ctx) |
| 3676 | src_dwarf_ast_parser->LinkDeclContextToDIE(decl_ctx: dst_decl_ctx, die: src); |
| 3677 | |
| 3678 | if (Type *src_child_type = die_to_type.lookup(Val: src.GetDIE())) |
| 3679 | die_to_type[dst.GetDIE()] = src_child_type; |
| 3680 | }; |
| 3681 | |
| 3682 | // Now do the work of linking the DeclContexts and Types. |
| 3683 | if (fast_path) { |
| 3684 | // We can do this quickly. Just run across the tables index-for-index |
| 3685 | // since we know each node has matching names and tags. |
| 3686 | for (idx = 0; idx < src_size; ++idx) { |
| 3687 | link(src_name_to_die.GetValueAtIndexUnchecked(idx), |
| 3688 | dst_name_to_die.GetValueAtIndexUnchecked(idx)); |
| 3689 | } |
| 3690 | } else { |
| 3691 | // We must do this slowly. For each member of the destination, look up a |
| 3692 | // member in the source with the same name, check its tag, and unique them |
| 3693 | // if everything matches up. Report failures. |
| 3694 | |
| 3695 | if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty()) { |
| 3696 | src_name_to_die.Sort(); |
| 3697 | |
| 3698 | for (idx = 0; idx < dst_size; ++idx) { |
| 3699 | ConstString dst_name = dst_name_to_die.GetCStringAtIndex(idx); |
| 3700 | DWARFDIE dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx); |
| 3701 | DWARFDIE src_die = src_name_to_die.Find(unique_cstr: dst_name, fail_value: DWARFDIE()); |
| 3702 | |
| 3703 | if (src_die && (src_die.Tag() == dst_die.Tag())) |
| 3704 | link(src_die, dst_die); |
| 3705 | else |
| 3706 | failures.push_back(x: dst_die); |
| 3707 | } |
| 3708 | } |
| 3709 | } |
| 3710 | |
| 3711 | const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize(); |
| 3712 | const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize(); |
| 3713 | |
| 3714 | if (src_size_artificial && dst_size_artificial) { |
| 3715 | dst_name_to_die_artificial.Sort(); |
| 3716 | |
| 3717 | for (idx = 0; idx < src_size_artificial; ++idx) { |
| 3718 | ConstString src_name_artificial = |
| 3719 | src_name_to_die_artificial.GetCStringAtIndex(idx); |
| 3720 | DWARFDIE src_die = |
| 3721 | src_name_to_die_artificial.GetValueAtIndexUnchecked(idx); |
| 3722 | DWARFDIE dst_die = |
| 3723 | dst_name_to_die_artificial.Find(unique_cstr: src_name_artificial, fail_value: DWARFDIE()); |
| 3724 | |
| 3725 | // Both classes have the artificial types, link them |
| 3726 | if (dst_die) |
| 3727 | link(src_die, dst_die); |
| 3728 | } |
| 3729 | } |
| 3730 | |
| 3731 | if (dst_size_artificial) { |
| 3732 | for (idx = 0; idx < dst_size_artificial; ++idx) { |
| 3733 | failures.push_back( |
| 3734 | x: dst_name_to_die_artificial.GetValueAtIndexUnchecked(idx)); |
| 3735 | } |
| 3736 | } |
| 3737 | |
| 3738 | return !failures.empty(); |
| 3739 | } |
| 3740 | |
| 3741 | bool DWARFASTParserClang::ShouldCreateUnnamedBitfield( |
| 3742 | FieldInfo const &last_field_info, uint64_t last_field_end, |
| 3743 | FieldInfo const &this_field_info, |
| 3744 | lldb_private::ClangASTImporter::LayoutInfo const &layout_info) const { |
| 3745 | // If we have a gap between the last_field_end and the current |
| 3746 | // field we have an unnamed bit-field. |
| 3747 | if (this_field_info.bit_offset <= last_field_end) |
| 3748 | return false; |
| 3749 | |
| 3750 | // If we have a base class, we assume there is no unnamed |
| 3751 | // bit-field if either of the following is true: |
| 3752 | // (a) this is the first field since the gap can be |
| 3753 | // attributed to the members from the base class. |
| 3754 | // FIXME: This assumption is not correct if the first field of |
| 3755 | // the derived class is indeed an unnamed bit-field. We currently |
| 3756 | // do not have the machinary to track the offset of the last field |
| 3757 | // of classes we have seen before, so we are not handling this case. |
| 3758 | // (b) Or, the first member of the derived class was a vtable pointer. |
| 3759 | // In this case we don't want to create an unnamed bitfield either |
| 3760 | // since those will be inserted by clang later. |
| 3761 | const bool have_base = layout_info.base_offsets.size() != 0; |
| 3762 | const bool this_is_first_field = |
| 3763 | last_field_info.bit_offset == 0 && last_field_info.bit_size == 0; |
| 3764 | const bool first_field_is_vptr = |
| 3765 | last_field_info.bit_offset == 0 && last_field_info.IsArtificial(); |
| 3766 | |
| 3767 | if (have_base && (this_is_first_field || first_field_is_vptr)) |
| 3768 | return false; |
| 3769 | |
| 3770 | return true; |
| 3771 | } |
| 3772 | |
| 3773 | void DWARFASTParserClang::AddUnnamedBitfieldToRecordTypeIfNeeded( |
| 3774 | ClangASTImporter::LayoutInfo &class_layout_info, |
| 3775 | const CompilerType &class_clang_type, const FieldInfo &previous_field, |
| 3776 | const FieldInfo ¤t_field) { |
| 3777 | // TODO: get this value from target |
| 3778 | const uint64_t word_width = 32; |
| 3779 | uint64_t last_field_end = previous_field.GetEffectiveFieldEnd(); |
| 3780 | |
| 3781 | if (!previous_field.IsBitfield()) { |
| 3782 | // The last field was not a bit-field... |
| 3783 | // but if it did take up the entire word then we need to extend |
| 3784 | // last_field_end so the bit-field does not step into the last |
| 3785 | // fields padding. |
| 3786 | if (last_field_end != 0 && ((last_field_end % word_width) != 0)) |
| 3787 | last_field_end += word_width - (last_field_end % word_width); |
| 3788 | } |
| 3789 | |
| 3790 | // Nothing to be done. |
| 3791 | if (!ShouldCreateUnnamedBitfield(last_field_info: previous_field, last_field_end, |
| 3792 | this_field_info: current_field, layout_info: class_layout_info)) |
| 3793 | return; |
| 3794 | |
| 3795 | // Place the unnamed bitfield into the gap between the previous field's end |
| 3796 | // and the current field's start. |
| 3797 | const uint64_t unnamed_bit_size = current_field.bit_offset - last_field_end; |
| 3798 | const uint64_t unnamed_bit_offset = last_field_end; |
| 3799 | |
| 3800 | clang::FieldDecl *unnamed_bitfield_decl = |
| 3801 | TypeSystemClang::AddFieldToRecordType( |
| 3802 | type: class_clang_type, name: llvm::StringRef(), |
| 3803 | field_type: m_ast.GetBuiltinTypeForEncodingAndBitSize(encoding: eEncodingSint, bit_size: word_width), |
| 3804 | access: lldb::AccessType::eAccessPublic, bitfield_bit_size: unnamed_bit_size); |
| 3805 | |
| 3806 | class_layout_info.field_offsets.insert( |
| 3807 | KV: std::make_pair(x&: unnamed_bitfield_decl, y: unnamed_bit_offset)); |
| 3808 | } |
| 3809 | |
| 3810 | void DWARFASTParserClang::ParseRustVariantPart( |
| 3811 | DWARFDIE &die, const DWARFDIE &parent_die, |
| 3812 | const CompilerType &class_clang_type, |
| 3813 | const lldb::AccessType default_accesibility, |
| 3814 | ClangASTImporter::LayoutInfo &layout_info) { |
| 3815 | assert(die.Tag() == llvm::dwarf::DW_TAG_variant_part); |
| 3816 | assert(SymbolFileDWARF::GetLanguage(*die.GetCU()) == |
| 3817 | LanguageType::eLanguageTypeRust); |
| 3818 | |
| 3819 | ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule(); |
| 3820 | |
| 3821 | VariantPart variants(die, parent_die, module_sp); |
| 3822 | |
| 3823 | auto discriminant_type = |
| 3824 | die.ResolveTypeUID(die: variants.discriminant().type_ref.Reference()); |
| 3825 | |
| 3826 | auto decl_context = m_ast.GetDeclContextForType(type: class_clang_type); |
| 3827 | |
| 3828 | auto inner_holder = m_ast.CreateRecordType( |
| 3829 | decl_ctx: decl_context, owning_module: OptionalClangModuleID(), access_type: lldb::eAccessPublic, |
| 3830 | name: std::string( |
| 3831 | llvm::formatv(Fmt: "{0}$Inner" , Vals: class_clang_type.GetTypeName(BaseOnly: false))), |
| 3832 | kind: llvm::to_underlying(E: clang::TagTypeKind::Union), language: lldb::eLanguageTypeRust); |
| 3833 | m_ast.StartTagDeclarationDefinition(type: inner_holder); |
| 3834 | m_ast.SetIsPacked(inner_holder); |
| 3835 | |
| 3836 | for (auto member : variants.members()) { |
| 3837 | |
| 3838 | auto has_discriminant = !member.IsDefault(); |
| 3839 | |
| 3840 | auto member_type = die.ResolveTypeUID(die: member.type_ref.Reference()); |
| 3841 | |
| 3842 | auto field_type = m_ast.CreateRecordType( |
| 3843 | decl_ctx: m_ast.GetDeclContextForType(type: inner_holder), owning_module: OptionalClangModuleID(), |
| 3844 | access_type: lldb::eAccessPublic, |
| 3845 | name: std::string(llvm::formatv(Fmt: "{0}$Variant" , Vals: member.GetName())), |
| 3846 | kind: llvm::to_underlying(E: clang::TagTypeKind::Struct), |
| 3847 | language: lldb::eLanguageTypeRust); |
| 3848 | |
| 3849 | m_ast.StartTagDeclarationDefinition(type: field_type); |
| 3850 | auto offset = member.byte_offset; |
| 3851 | |
| 3852 | if (has_discriminant) { |
| 3853 | m_ast.AddFieldToRecordType( |
| 3854 | type: field_type, name: "$discr$" , field_type: discriminant_type->GetFullCompilerType(), |
| 3855 | access: lldb::eAccessPublic, bitfield_bit_size: variants.discriminant().byte_offset); |
| 3856 | offset += |
| 3857 | llvm::expectedToOptional(E: discriminant_type->GetByteSize(exe_scope: nullptr)) |
| 3858 | .value_or(u: 0); |
| 3859 | } |
| 3860 | |
| 3861 | m_ast.AddFieldToRecordType(type: field_type, name: "value" , |
| 3862 | field_type: member_type->GetFullCompilerType(), |
| 3863 | access: lldb::eAccessPublic, bitfield_bit_size: offset * 8); |
| 3864 | |
| 3865 | m_ast.CompleteTagDeclarationDefinition(type: field_type); |
| 3866 | |
| 3867 | auto name = has_discriminant |
| 3868 | ? llvm::formatv(Fmt: "$variant${0}" , Vals&: member.discr_value.value()) |
| 3869 | : std::string("$variant$" ); |
| 3870 | |
| 3871 | auto variant_decl = |
| 3872 | m_ast.AddFieldToRecordType(type: inner_holder, name: llvm::StringRef(name), |
| 3873 | field_type, access: default_accesibility, bitfield_bit_size: 0); |
| 3874 | |
| 3875 | layout_info.field_offsets.insert(KV: {variant_decl, 0}); |
| 3876 | } |
| 3877 | |
| 3878 | auto inner_field = m_ast.AddFieldToRecordType(type: class_clang_type, |
| 3879 | name: llvm::StringRef("$variants$" ), |
| 3880 | field_type: inner_holder, access: eAccessPublic, bitfield_bit_size: 0); |
| 3881 | |
| 3882 | m_ast.CompleteTagDeclarationDefinition(type: inner_holder); |
| 3883 | |
| 3884 | layout_info.field_offsets.insert(KV: {inner_field, 0}); |
| 3885 | } |
| 3886 | |