1 //===-- DWARFASTParserClang.cpp ---------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "DWARFASTParserClang.h" 11 #include "DWARFCompileUnit.h" 12 #include "DWARFDebugInfo.h" 13 #include "DWARFDeclContext.h" 14 #include "DWARFDefines.h" 15 #include "DWARFDIE.h" 16 #include "DWARFDIECollection.h" 17 #include "SymbolFileDWARF.h" 18 #include "SymbolFileDWARFDebugMap.h" 19 #include "UniqueDWARFASTType.h" 20 21 #include "lldb/Interpreter/Args.h" 22 #include "lldb/Core/Log.h" 23 #include "lldb/Core/Module.h" 24 #include "lldb/Core/StreamString.h" 25 #include "lldb/Core/Value.h" 26 #include "lldb/Host/Host.h" 27 #include "lldb/Symbol/ClangExternalASTSourceCommon.h" 28 #include "lldb/Symbol/CompileUnit.h" 29 #include "lldb/Symbol/Function.h" 30 #include "lldb/Symbol/ObjectFile.h" 31 #include "lldb/Symbol/TypeList.h" 32 #include "lldb/Target/Language.h" 33 #include "Plugins/Language/ObjC/ObjCLanguage.h" 34 35 #include "clang/AST/DeclCXX.h" 36 #include "clang/AST/DeclObjC.h" 37 38 #include <map> 39 #include <vector> 40 41 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN 42 43 #ifdef ENABLE_DEBUG_PRINTF 44 #include <stdio.h> 45 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__) 46 #else 47 #define DEBUG_PRINTF(fmt, ...) 48 #endif 49 50 51 using namespace lldb; 52 using namespace lldb_private; 53 DWARFASTParserClang::DWARFASTParserClang (ClangASTContext &ast) : 54 m_ast (ast), 55 m_die_to_decl_ctx (), 56 m_decl_ctx_to_die () 57 { 58 } 59 60 DWARFASTParserClang::~DWARFASTParserClang () 61 { 62 } 63 64 65 static AccessType 66 DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility) 67 { 68 switch (dwarf_accessibility) 69 { 70 case DW_ACCESS_public: return eAccessPublic; 71 case DW_ACCESS_private: return eAccessPrivate; 72 case DW_ACCESS_protected: return eAccessProtected; 73 default: break; 74 } 75 return eAccessNone; 76 } 77 78 static bool 79 DeclKindIsCXXClass (clang::Decl::Kind decl_kind) 80 { 81 switch (decl_kind) 82 { 83 case clang::Decl::CXXRecord: 84 case clang::Decl::ClassTemplateSpecialization: 85 return true; 86 default: 87 break; 88 } 89 return false; 90 } 91 92 struct BitfieldInfo 93 { 94 uint64_t bit_size; 95 uint64_t bit_offset; 96 97 BitfieldInfo () : 98 bit_size (LLDB_INVALID_ADDRESS), 99 bit_offset (LLDB_INVALID_ADDRESS) 100 { 101 } 102 103 void 104 Clear() 105 { 106 bit_size = LLDB_INVALID_ADDRESS; 107 bit_offset = LLDB_INVALID_ADDRESS; 108 } 109 110 bool IsValid () 111 { 112 return (bit_size != LLDB_INVALID_ADDRESS) && 113 (bit_offset != LLDB_INVALID_ADDRESS); 114 } 115 }; 116 117 TypeSP 118 DWARFASTParserClang::ParseTypeFromDWARF (const SymbolContext& sc, 119 const DWARFDIE &die, 120 Log *log, 121 bool *type_is_new_ptr) 122 { 123 TypeSP type_sp; 124 125 if (type_is_new_ptr) 126 *type_is_new_ptr = false; 127 128 AccessType accessibility = eAccessNone; 129 if (die) 130 { 131 SymbolFileDWARF *dwarf = die.GetDWARF(); 132 if (log) 133 { 134 DWARFDIE context_die; 135 clang::DeclContext *context = GetClangDeclContextContainingDIE (die, &context_die); 136 137 dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')", 138 die.GetOffset(), 139 static_cast<void*>(context), 140 context_die.GetOffset(), 141 die.GetTagAsCString(), 142 die.GetName()); 143 144 } 145 // 146 // Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 147 // if (log && dwarf_cu) 148 // { 149 // StreamString s; 150 // die->DumpLocation (this, dwarf_cu, s); 151 // dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData()); 152 // 153 // } 154 155 Type *type_ptr = dwarf->GetDIEToType().lookup (die.GetDIE()); 156 TypeList* type_list = dwarf->GetTypeList(); 157 if (type_ptr == NULL) 158 { 159 if (type_is_new_ptr) 160 *type_is_new_ptr = true; 161 162 const dw_tag_t tag = die.Tag(); 163 164 bool is_forward_declaration = false; 165 DWARFAttributes attributes; 166 const char *type_name_cstr = NULL; 167 ConstString type_name_const_str; 168 Type::ResolveState resolve_state = Type::eResolveStateUnresolved; 169 uint64_t byte_size = 0; 170 Declaration decl; 171 172 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID; 173 CompilerType clang_type; 174 DWARFFormValue form_value; 175 176 dw_attr_t attr; 177 178 switch (tag) 179 { 180 case DW_TAG_base_type: 181 case DW_TAG_pointer_type: 182 case DW_TAG_reference_type: 183 case DW_TAG_rvalue_reference_type: 184 case DW_TAG_typedef: 185 case DW_TAG_const_type: 186 case DW_TAG_restrict_type: 187 case DW_TAG_volatile_type: 188 case DW_TAG_unspecified_type: 189 { 190 // Set a bit that lets us know that we are currently parsing this 191 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED; 192 193 const size_t num_attributes = die.GetAttributes (attributes); 194 uint32_t encoding = 0; 195 lldb::user_id_t encoding_uid = LLDB_INVALID_UID; 196 197 if (num_attributes > 0) 198 { 199 uint32_t i; 200 for (i=0; i<num_attributes; ++i) 201 { 202 attr = attributes.AttributeAtIndex(i); 203 if (attributes.ExtractFormValueAtIndex(i, form_value)) 204 { 205 switch (attr) 206 { 207 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 208 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 209 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 210 case DW_AT_name: 211 212 type_name_cstr = form_value.AsCString(); 213 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't 214 // include the "&"... 215 if (tag == DW_TAG_reference_type) 216 { 217 if (strchr (type_name_cstr, '&') == NULL) 218 type_name_cstr = NULL; 219 } 220 if (type_name_cstr) 221 type_name_const_str.SetCString(type_name_cstr); 222 break; 223 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break; 224 case DW_AT_encoding: encoding = form_value.Unsigned(); break; 225 case DW_AT_type: encoding_uid = DIERef(form_value).GetUID(); break; 226 default: 227 case DW_AT_sibling: 228 break; 229 } 230 } 231 } 232 } 233 234 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid); 235 236 switch (tag) 237 { 238 default: 239 break; 240 241 case DW_TAG_unspecified_type: 242 if (strcmp(type_name_cstr, "nullptr_t") == 0 || 243 strcmp(type_name_cstr, "decltype(nullptr)") == 0 ) 244 { 245 resolve_state = Type::eResolveStateFull; 246 clang_type = m_ast.GetBasicType(eBasicTypeNullPtr); 247 break; 248 } 249 // Fall through to base type below in case we can handle the type there... 250 251 case DW_TAG_base_type: 252 resolve_state = Type::eResolveStateFull; 253 clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr, 254 encoding, 255 byte_size * 8); 256 break; 257 258 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break; 259 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break; 260 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break; 261 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break; 262 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break; 263 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break; 264 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break; 265 } 266 267 if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL) 268 { 269 bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus); 270 271 if (translation_unit_is_objc) 272 { 273 if (type_name_cstr != NULL) 274 { 275 static ConstString g_objc_type_name_id("id"); 276 static ConstString g_objc_type_name_Class("Class"); 277 static ConstString g_objc_type_name_selector("SEL"); 278 279 if (type_name_const_str == g_objc_type_name_id) 280 { 281 if (log) 282 dwarf->GetObjectFile()->GetModule()->LogMessage (log, 283 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.", 284 die.GetOffset(), 285 die.GetTagAsCString(), 286 die.GetName()); 287 clang_type = m_ast.GetBasicType(eBasicTypeObjCID); 288 encoding_data_type = Type::eEncodingIsUID; 289 encoding_uid = LLDB_INVALID_UID; 290 resolve_state = Type::eResolveStateFull; 291 292 } 293 else if (type_name_const_str == g_objc_type_name_Class) 294 { 295 if (log) 296 dwarf->GetObjectFile()->GetModule()->LogMessage (log, 297 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.", 298 die.GetOffset(), 299 die.GetTagAsCString(), 300 die.GetName()); 301 clang_type = m_ast.GetBasicType(eBasicTypeObjCClass); 302 encoding_data_type = Type::eEncodingIsUID; 303 encoding_uid = LLDB_INVALID_UID; 304 resolve_state = Type::eResolveStateFull; 305 } 306 else if (type_name_const_str == g_objc_type_name_selector) 307 { 308 if (log) 309 dwarf->GetObjectFile()->GetModule()->LogMessage (log, 310 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.", 311 die.GetOffset(), 312 die.GetTagAsCString(), 313 die.GetName()); 314 clang_type = m_ast.GetBasicType(eBasicTypeObjCSel); 315 encoding_data_type = Type::eEncodingIsUID; 316 encoding_uid = LLDB_INVALID_UID; 317 resolve_state = Type::eResolveStateFull; 318 } 319 } 320 else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid != LLDB_INVALID_UID) 321 { 322 // Clang sometimes erroneously emits id as objc_object*. In that case we fix up the type to "id". 323 324 const DWARFDIE encoding_die = die.GetDIE(encoding_uid); 325 326 if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type) 327 { 328 if (const char *struct_name = encoding_die.GetName()) 329 { 330 if (!strcmp(struct_name, "objc_object")) 331 { 332 if (log) 333 dwarf->GetObjectFile()->GetModule()->LogMessage (log, 334 "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.", 335 die.GetOffset(), 336 die.GetTagAsCString(), 337 die.GetName()); 338 clang_type = m_ast.GetBasicType(eBasicTypeObjCID); 339 encoding_data_type = Type::eEncodingIsUID; 340 encoding_uid = LLDB_INVALID_UID; 341 resolve_state = Type::eResolveStateFull; 342 } 343 } 344 } 345 } 346 } 347 } 348 349 type_sp.reset( new Type (die.GetID(), 350 dwarf, 351 type_name_const_str, 352 byte_size, 353 NULL, 354 encoding_uid, 355 encoding_data_type, 356 &decl, 357 clang_type, 358 resolve_state)); 359 360 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get(); 361 362 // Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false); 363 // if (encoding_type != NULL) 364 // { 365 // if (encoding_type != DIE_IS_BEING_PARSED) 366 // type_sp->SetEncodingType(encoding_type); 367 // else 368 // m_indirect_fixups.push_back(type_sp.get()); 369 // } 370 } 371 break; 372 373 case DW_TAG_structure_type: 374 case DW_TAG_union_type: 375 case DW_TAG_class_type: 376 { 377 // Set a bit that lets us know that we are currently parsing this 378 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED; 379 bool byte_size_valid = false; 380 381 LanguageType class_language = eLanguageTypeUnknown; 382 bool is_complete_objc_class = false; 383 //bool struct_is_class = false; 384 const size_t num_attributes = die.GetAttributes (attributes); 385 if (num_attributes > 0) 386 { 387 uint32_t i; 388 for (i=0; i<num_attributes; ++i) 389 { 390 attr = attributes.AttributeAtIndex(i); 391 if (attributes.ExtractFormValueAtIndex(i, form_value)) 392 { 393 switch (attr) 394 { 395 case DW_AT_decl_file: 396 if (die.GetCU()->DW_AT_decl_file_attributes_are_invalid()) 397 { 398 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always 399 // point to the compile unit file, so we clear this invalid value 400 // so that we can still unique types efficiently. 401 decl.SetFile(FileSpec ("<invalid>", false)); 402 } 403 else 404 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); 405 break; 406 407 case DW_AT_decl_line: 408 decl.SetLine(form_value.Unsigned()); 409 break; 410 411 case DW_AT_decl_column: 412 decl.SetColumn(form_value.Unsigned()); 413 break; 414 415 case DW_AT_name: 416 type_name_cstr = form_value.AsCString(); 417 type_name_const_str.SetCString(type_name_cstr); 418 break; 419 420 case DW_AT_byte_size: 421 byte_size = form_value.Unsigned(); 422 byte_size_valid = true; 423 break; 424 425 case DW_AT_accessibility: 426 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); 427 break; 428 429 case DW_AT_declaration: 430 is_forward_declaration = form_value.Boolean(); 431 break; 432 433 case DW_AT_APPLE_runtime_class: 434 class_language = (LanguageType)form_value.Signed(); 435 break; 436 437 case DW_AT_APPLE_objc_complete_type: 438 is_complete_objc_class = form_value.Signed(); 439 break; 440 441 case DW_AT_allocated: 442 case DW_AT_associated: 443 case DW_AT_data_location: 444 case DW_AT_description: 445 case DW_AT_start_scope: 446 case DW_AT_visibility: 447 default: 448 case DW_AT_sibling: 449 break; 450 } 451 } 452 } 453 } 454 455 // UniqueDWARFASTType is large, so don't create a local variables on the 456 // stack, put it on the heap. This function is often called recursively 457 // and clang isn't good and sharing the stack space for variables in different blocks. 458 std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType()); 459 460 // Only try and unique the type if it has a name. 461 if (type_name_const_str && 462 dwarf->GetUniqueDWARFASTTypeMap().Find (type_name_const_str, 463 die, 464 decl, 465 byte_size_valid ? byte_size : -1, 466 *unique_ast_entry_ap)) 467 { 468 // We have already parsed this type or from another 469 // compile unit. GCC loves to use the "one definition 470 // rule" which can result in multiple definitions 471 // of the same class over and over in each compile 472 // unit. 473 type_sp = unique_ast_entry_ap->m_type_sp; 474 if (type_sp) 475 { 476 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get(); 477 return type_sp; 478 } 479 } 480 481 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr); 482 483 int tag_decl_kind = -1; 484 AccessType default_accessibility = eAccessNone; 485 if (tag == DW_TAG_structure_type) 486 { 487 tag_decl_kind = clang::TTK_Struct; 488 default_accessibility = eAccessPublic; 489 } 490 else if (tag == DW_TAG_union_type) 491 { 492 tag_decl_kind = clang::TTK_Union; 493 default_accessibility = eAccessPublic; 494 } 495 else if (tag == DW_TAG_class_type) 496 { 497 tag_decl_kind = clang::TTK_Class; 498 default_accessibility = eAccessPrivate; 499 } 500 501 if (byte_size_valid && byte_size == 0 && type_name_cstr && 502 die.HasChildren() == false && 503 sc.comp_unit->GetLanguage() == eLanguageTypeObjC) 504 { 505 // Work around an issue with clang at the moment where 506 // forward declarations for objective C classes are emitted 507 // as: 508 // DW_TAG_structure_type [2] 509 // DW_AT_name( "ForwardObjcClass" ) 510 // DW_AT_byte_size( 0x00 ) 511 // DW_AT_decl_file( "..." ) 512 // DW_AT_decl_line( 1 ) 513 // 514 // Note that there is no DW_AT_declaration and there are 515 // no children, and the byte size is zero. 516 is_forward_declaration = true; 517 } 518 519 if (class_language == eLanguageTypeObjC || 520 class_language == eLanguageTypeObjC_plus_plus) 521 { 522 if (!is_complete_objc_class && die.Supports_DW_AT_APPLE_objc_complete_type()) 523 { 524 // We have a valid eSymbolTypeObjCClass class symbol whose 525 // name matches the current objective C class that we 526 // are trying to find and this DIE isn't the complete 527 // definition (we checked is_complete_objc_class above and 528 // know it is false), so the real definition is in here somewhere 529 type_sp = dwarf->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true); 530 531 if (!type_sp) 532 { 533 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile(); 534 if (debug_map_symfile) 535 { 536 // We weren't able to find a full declaration in 537 // this DWARF, see if we have a declaration anywhere 538 // else... 539 type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true); 540 } 541 } 542 543 if (type_sp) 544 { 545 if (log) 546 { 547 dwarf->GetObjectFile()->GetModule()->LogMessage (log, 548 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64, 549 static_cast<void*>(this), 550 die.GetOffset(), 551 DW_TAG_value_to_name(tag), 552 type_name_cstr, 553 type_sp->GetID()); 554 } 555 556 // We found a real definition for this type elsewhere 557 // so lets use it and cache the fact that we found 558 // a complete type for this die 559 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get(); 560 return type_sp; 561 } 562 } 563 } 564 565 566 if (is_forward_declaration) 567 { 568 // We have a forward declaration to a type and we need 569 // to try and find a full declaration. We look in the 570 // current type index just in case we have a forward 571 // declaration followed by an actual declarations in the 572 // DWARF. If this fails, we need to look elsewhere... 573 if (log) 574 { 575 dwarf->GetObjectFile()->GetModule()->LogMessage (log, 576 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type", 577 static_cast<void*>(this), 578 die.GetOffset(), 579 DW_TAG_value_to_name(tag), 580 type_name_cstr); 581 } 582 583 DWARFDeclContext die_decl_ctx; 584 die.GetDWARFDeclContext(die_decl_ctx); 585 586 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str); 587 type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx); 588 589 if (!type_sp) 590 { 591 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile(); 592 if (debug_map_symfile) 593 { 594 // We weren't able to find a full declaration in 595 // this DWARF, see if we have a declaration anywhere 596 // else... 597 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx); 598 } 599 } 600 601 if (type_sp) 602 { 603 if (log) 604 { 605 dwarf->GetObjectFile()->GetModule()->LogMessage (log, 606 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64, 607 static_cast<void*>(this), 608 die.GetOffset(), 609 DW_TAG_value_to_name(tag), 610 type_name_cstr, 611 type_sp->GetID()); 612 } 613 614 // We found a real definition for this type elsewhere 615 // so lets use it and cache the fact that we found 616 // a complete type for this die 617 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get(); 618 return type_sp; 619 } 620 } 621 assert (tag_decl_kind != -1); 622 bool clang_type_was_created = false; 623 clang_type.SetCompilerType(&m_ast, dwarf->GetForwardDeclDieToClangType().lookup (die.GetDIE())); 624 if (!clang_type) 625 { 626 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (die, nullptr); 627 if (accessibility == eAccessNone && decl_ctx) 628 { 629 // Check the decl context that contains this class/struct/union. 630 // If it is a class we must give it an accessibility. 631 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind(); 632 if (DeclKindIsCXXClass (containing_decl_kind)) 633 accessibility = default_accessibility; 634 } 635 636 ClangASTMetadata metadata; 637 metadata.SetUserID(die.GetID()); 638 metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual (die)); 639 640 if (type_name_cstr && strchr (type_name_cstr, '<')) 641 { 642 ClangASTContext::TemplateParameterInfos template_param_infos; 643 if (ParseTemplateParameterInfos (die, template_param_infos)) 644 { 645 clang::ClassTemplateDecl *class_template_decl = m_ast.ParseClassTemplateDecl (decl_ctx, 646 accessibility, 647 type_name_cstr, 648 tag_decl_kind, 649 template_param_infos); 650 651 clang::ClassTemplateSpecializationDecl *class_specialization_decl = m_ast.CreateClassTemplateSpecializationDecl (decl_ctx, 652 class_template_decl, 653 tag_decl_kind, 654 template_param_infos); 655 clang_type = m_ast.CreateClassTemplateSpecializationType (class_specialization_decl); 656 clang_type_was_created = true; 657 658 m_ast.SetMetadata (class_template_decl, metadata); 659 m_ast.SetMetadata (class_specialization_decl, metadata); 660 } 661 } 662 663 if (!clang_type_was_created) 664 { 665 clang_type_was_created = true; 666 clang_type = m_ast.CreateRecordType (decl_ctx, 667 accessibility, 668 type_name_cstr, 669 tag_decl_kind, 670 class_language, 671 &metadata); 672 } 673 } 674 675 // Store a forward declaration to this class type in case any 676 // parameters in any class methods need it for the clang 677 // types for function prototypes. 678 LinkDeclContextToDIE(m_ast.GetDeclContextForType(clang_type), die); 679 type_sp.reset (new Type (die.GetID(), 680 dwarf, 681 type_name_const_str, 682 byte_size, 683 NULL, 684 LLDB_INVALID_UID, 685 Type::eEncodingIsUID, 686 &decl, 687 clang_type, 688 Type::eResolveStateForward)); 689 690 type_sp->SetIsCompleteObjCClass(is_complete_objc_class); 691 692 693 // Add our type to the unique type map so we don't 694 // end up creating many copies of the same type over 695 // and over in the ASTContext for our module 696 unique_ast_entry_ap->m_type_sp = type_sp; 697 unique_ast_entry_ap->m_die = die; 698 unique_ast_entry_ap->m_declaration = decl; 699 unique_ast_entry_ap->m_byte_size = byte_size; 700 dwarf->GetUniqueDWARFASTTypeMap().Insert (type_name_const_str, 701 *unique_ast_entry_ap); 702 703 if (is_forward_declaration && die.HasChildren()) 704 { 705 // Check to see if the DIE actually has a definition, some version of GCC will 706 // emit DIEs with DW_AT_declaration set to true, but yet still have subprogram, 707 // members, or inheritance, so we can't trust it 708 DWARFDIE child_die = die.GetFirstChild(); 709 while (child_die) 710 { 711 switch (child_die.Tag()) 712 { 713 case DW_TAG_inheritance: 714 case DW_TAG_subprogram: 715 case DW_TAG_member: 716 case DW_TAG_APPLE_property: 717 case DW_TAG_class_type: 718 case DW_TAG_structure_type: 719 case DW_TAG_enumeration_type: 720 case DW_TAG_typedef: 721 case DW_TAG_union_type: 722 child_die.Clear(); 723 is_forward_declaration = false; 724 break; 725 default: 726 child_die = child_die.GetSibling(); 727 break; 728 } 729 } 730 } 731 732 if (!is_forward_declaration) 733 { 734 // Always start the definition for a class type so that 735 // if the class has child classes or types that require 736 // the class to be created for use as their decl contexts 737 // the class will be ready to accept these child definitions. 738 if (die.HasChildren() == false) 739 { 740 // No children for this struct/union/class, lets finish it 741 ClangASTContext::StartTagDeclarationDefinition (clang_type); 742 ClangASTContext::CompleteTagDeclarationDefinition (clang_type); 743 744 if (tag == DW_TAG_structure_type) // this only applies in C 745 { 746 clang::RecordDecl *record_decl = ClangASTContext::GetAsRecordDecl(clang_type); 747 748 if (record_decl) 749 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, LayoutInfo())); 750 } 751 } 752 else if (clang_type_was_created) 753 { 754 // Start the definition if the class is not objective C since 755 // the underlying decls respond to isCompleteDefinition(). Objective 756 // C decls don't respond to isCompleteDefinition() so we can't 757 // start the declaration definition right away. For C++ class/union/structs 758 // we want to start the definition in case the class is needed as the 759 // declaration context for a contained class or type without the need 760 // to complete that type.. 761 762 if (class_language != eLanguageTypeObjC && 763 class_language != eLanguageTypeObjC_plus_plus) 764 ClangASTContext::StartTagDeclarationDefinition (clang_type); 765 766 // Leave this as a forward declaration until we need 767 // to know the details of the type. lldb_private::Type 768 // will automatically call the SymbolFile virtual function 769 // "SymbolFileDWARF::CompleteType(Type *)" 770 // When the definition needs to be defined. 771 dwarf->GetForwardDeclDieToClangType()[die.GetDIE()] = clang_type.GetOpaqueQualType(); 772 dwarf->GetForwardDeclClangTypeToDie()[ClangASTContext::RemoveFastQualifiers(clang_type).GetOpaqueQualType()] = die.GetDIERef(); 773 m_ast.SetHasExternalStorage (clang_type.GetOpaqueQualType(), true); 774 } 775 } 776 } 777 break; 778 779 case DW_TAG_enumeration_type: 780 { 781 // Set a bit that lets us know that we are currently parsing this 782 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED; 783 784 DWARFFormValue encoding_form; 785 786 const size_t num_attributes = die.GetAttributes (attributes); 787 if (num_attributes > 0) 788 { 789 uint32_t i; 790 791 for (i=0; i<num_attributes; ++i) 792 { 793 attr = attributes.AttributeAtIndex(i); 794 if (attributes.ExtractFormValueAtIndex(i, form_value)) 795 { 796 switch (attr) 797 { 798 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 799 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 800 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 801 case DW_AT_name: 802 type_name_cstr = form_value.AsCString(); 803 type_name_const_str.SetCString(type_name_cstr); 804 break; 805 case DW_AT_type: encoding_form = form_value; break; 806 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break; 807 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 808 case DW_AT_declaration: break; //is_forward_declaration = form_value.Boolean(); break; 809 case DW_AT_allocated: 810 case DW_AT_associated: 811 case DW_AT_bit_stride: 812 case DW_AT_byte_stride: 813 case DW_AT_data_location: 814 case DW_AT_description: 815 case DW_AT_start_scope: 816 case DW_AT_visibility: 817 case DW_AT_specification: 818 case DW_AT_abstract_origin: 819 case DW_AT_sibling: 820 break; 821 } 822 } 823 } 824 825 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr); 826 827 CompilerType enumerator_clang_type; 828 clang_type.SetCompilerType (&m_ast, dwarf->GetForwardDeclDieToClangType().lookup (die.GetDIE())); 829 if (!clang_type) 830 { 831 if (encoding_form.IsValid()) 832 { 833 Type *enumerator_type = dwarf->ResolveTypeUID(DIERef(encoding_form).GetUID()); 834 if (enumerator_type) 835 enumerator_clang_type = enumerator_type->GetFullCompilerType (); 836 } 837 838 if (!enumerator_clang_type) 839 enumerator_clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL, 840 DW_ATE_signed, 841 byte_size * 8); 842 843 clang_type = m_ast.CreateEnumerationType (type_name_cstr, 844 GetClangDeclContextContainingDIE (die, nullptr), 845 decl, 846 enumerator_clang_type); 847 } 848 else 849 { 850 enumerator_clang_type = m_ast.GetEnumerationIntegerType (clang_type.GetOpaqueQualType()); 851 } 852 853 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die); 854 855 type_sp.reset( new Type (die.GetID(), 856 dwarf, 857 type_name_const_str, 858 byte_size, 859 NULL, 860 DIERef(encoding_form).GetUID(), 861 Type::eEncodingIsUID, 862 &decl, 863 clang_type, 864 Type::eResolveStateForward)); 865 866 ClangASTContext::StartTagDeclarationDefinition (clang_type); 867 if (die.HasChildren()) 868 { 869 SymbolContext cu_sc(die.GetLLDBCompileUnit()); 870 bool is_signed = false; 871 enumerator_clang_type.IsIntegerType(is_signed); 872 ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), die); 873 } 874 ClangASTContext::CompleteTagDeclarationDefinition (clang_type); 875 } 876 } 877 break; 878 879 case DW_TAG_inlined_subroutine: 880 case DW_TAG_subprogram: 881 case DW_TAG_subroutine_type: 882 { 883 // Set a bit that lets us know that we are currently parsing this 884 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED; 885 886 DWARFFormValue type_die_form; 887 bool is_variadic = false; 888 bool is_inline = false; 889 bool is_static = false; 890 bool is_virtual = false; 891 bool is_explicit = false; 892 bool is_artificial = false; 893 DWARFFormValue specification_die_form; 894 DWARFFormValue abstract_origin_die_form; 895 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET; 896 897 unsigned type_quals = 0; 898 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern 899 900 901 const size_t num_attributes = die.GetAttributes (attributes); 902 if (num_attributes > 0) 903 { 904 uint32_t i; 905 for (i=0; i<num_attributes; ++i) 906 { 907 attr = attributes.AttributeAtIndex(i); 908 if (attributes.ExtractFormValueAtIndex(i, form_value)) 909 { 910 switch (attr) 911 { 912 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 913 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 914 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 915 case DW_AT_name: 916 type_name_cstr = form_value.AsCString(); 917 type_name_const_str.SetCString(type_name_cstr); 918 break; 919 920 case DW_AT_linkage_name: 921 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&dwarf->get_debug_str_data()); break; 922 case DW_AT_type: type_die_form = form_value; break; 923 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 924 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break; 925 case DW_AT_inline: is_inline = form_value.Boolean(); break; 926 case DW_AT_virtuality: is_virtual = form_value.Boolean(); break; 927 case DW_AT_explicit: is_explicit = form_value.Boolean(); break; 928 case DW_AT_artificial: is_artificial = form_value.Boolean(); break; 929 930 931 case DW_AT_external: 932 if (form_value.Unsigned()) 933 { 934 if (storage == clang::SC_None) 935 storage = clang::SC_Extern; 936 else 937 storage = clang::SC_PrivateExtern; 938 } 939 break; 940 941 case DW_AT_specification: 942 specification_die_form = form_value; 943 break; 944 945 case DW_AT_abstract_origin: 946 abstract_origin_die_form = form_value; 947 break; 948 949 case DW_AT_object_pointer: 950 object_pointer_die_offset = form_value.Reference(); 951 break; 952 953 case DW_AT_allocated: 954 case DW_AT_associated: 955 case DW_AT_address_class: 956 case DW_AT_calling_convention: 957 case DW_AT_data_location: 958 case DW_AT_elemental: 959 case DW_AT_entry_pc: 960 case DW_AT_frame_base: 961 case DW_AT_high_pc: 962 case DW_AT_low_pc: 963 case DW_AT_prototyped: 964 case DW_AT_pure: 965 case DW_AT_ranges: 966 case DW_AT_recursive: 967 case DW_AT_return_addr: 968 case DW_AT_segment: 969 case DW_AT_start_scope: 970 case DW_AT_static_link: 971 case DW_AT_trampoline: 972 case DW_AT_visibility: 973 case DW_AT_vtable_elem_location: 974 case DW_AT_description: 975 case DW_AT_sibling: 976 break; 977 } 978 } 979 } 980 } 981 982 std::string object_pointer_name; 983 if (object_pointer_die_offset != DW_INVALID_OFFSET) 984 { 985 DWARFDIE object_pointer_die = die.GetDIE (object_pointer_die_offset); 986 if (object_pointer_die) 987 { 988 const char *object_pointer_name_cstr = object_pointer_die.GetName(); 989 if (object_pointer_name_cstr) 990 object_pointer_name = object_pointer_name_cstr; 991 } 992 } 993 994 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr); 995 996 CompilerType return_clang_type; 997 Type *func_type = NULL; 998 999 if (type_die_form.IsValid()) 1000 func_type = dwarf->ResolveTypeUID(DIERef(type_die_form).GetUID()); 1001 1002 if (func_type) 1003 return_clang_type = func_type->GetForwardCompilerType (); 1004 else 1005 return_clang_type = m_ast.GetBasicType(eBasicTypeVoid); 1006 1007 1008 std::vector<CompilerType> function_param_types; 1009 std::vector<clang::ParmVarDecl*> function_param_decls; 1010 1011 // Parse the function children for the parameters 1012 1013 DWARFDIE decl_ctx_die; 1014 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (die, &decl_ctx_die); 1015 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind(); 1016 1017 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind); 1018 // Start off static. This will be set to false in ParseChildParameters(...) 1019 // if we find a "this" parameters as the first parameter 1020 if (is_cxx_method) 1021 is_static = true; 1022 1023 if (die.HasChildren()) 1024 { 1025 bool skip_artificial = true; 1026 ParseChildParameters (sc, 1027 containing_decl_ctx, 1028 die, 1029 skip_artificial, 1030 is_static, 1031 is_variadic, 1032 function_param_types, 1033 function_param_decls, 1034 type_quals); 1035 } 1036 1037 // clang_type will get the function prototype clang type after this call 1038 clang_type = m_ast.CreateFunctionType (return_clang_type, 1039 function_param_types.data(), 1040 function_param_types.size(), 1041 is_variadic, 1042 type_quals); 1043 1044 bool ignore_containing_context = false; 1045 1046 if (type_name_cstr) 1047 { 1048 bool type_handled = false; 1049 if (tag == DW_TAG_subprogram || 1050 tag == DW_TAG_inlined_subroutine) 1051 { 1052 ObjCLanguage::MethodName objc_method (type_name_cstr, true); 1053 if (objc_method.IsValid(true)) 1054 { 1055 CompilerType class_opaque_type; 1056 ConstString class_name(objc_method.GetClassName()); 1057 if (class_name) 1058 { 1059 TypeSP complete_objc_class_type_sp (dwarf->FindCompleteObjCDefinitionTypeForDIE (DWARFDIE(), class_name, false)); 1060 1061 if (complete_objc_class_type_sp) 1062 { 1063 CompilerType type_clang_forward_type = complete_objc_class_type_sp->GetForwardCompilerType (); 1064 if (ClangASTContext::IsObjCObjectOrInterfaceType(type_clang_forward_type)) 1065 class_opaque_type = type_clang_forward_type; 1066 } 1067 } 1068 1069 if (class_opaque_type) 1070 { 1071 // If accessibility isn't set to anything valid, assume public for 1072 // now... 1073 if (accessibility == eAccessNone) 1074 accessibility = eAccessPublic; 1075 1076 clang::ObjCMethodDecl *objc_method_decl = m_ast.AddMethodToObjCObjectType (class_opaque_type, 1077 type_name_cstr, 1078 clang_type, 1079 accessibility, 1080 is_artificial); 1081 type_handled = objc_method_decl != NULL; 1082 if (type_handled) 1083 { 1084 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die); 1085 m_ast.SetMetadataAsUserID (objc_method_decl, die.GetID()); 1086 } 1087 else 1088 { 1089 dwarf->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), please file a bug and attach the file at the start of this error message", 1090 die.GetOffset(), 1091 tag, 1092 DW_TAG_value_to_name(tag)); 1093 } 1094 } 1095 } 1096 else if (is_cxx_method) 1097 { 1098 // Look at the parent of this DIE and see if is is 1099 // a class or struct and see if this is actually a 1100 // C++ method 1101 Type *class_type = dwarf->ResolveType (decl_ctx_die); 1102 if (class_type) 1103 { 1104 if (class_type->GetID() != decl_ctx_die.GetID()) 1105 { 1106 // We uniqued the parent class of this function to another class 1107 // so we now need to associate all dies under "decl_ctx_die" to 1108 // DIEs in the DIE for "class_type"... 1109 SymbolFileDWARF *class_symfile = NULL; 1110 DWARFDIE class_type_die; 1111 1112 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile(); 1113 if (debug_map_symfile) 1114 { 1115 class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID())); 1116 class_type_die = class_symfile->DebugInfo()->GetDIE (DIERef(class_type->GetID())); 1117 } 1118 else 1119 { 1120 class_symfile = dwarf; 1121 class_type_die = dwarf->DebugInfo()->GetDIE (DIERef(class_type->GetID())); 1122 } 1123 if (class_type_die) 1124 { 1125 DWARFDIECollection failures; 1126 1127 CopyUniqueClassMethodTypes (decl_ctx_die, 1128 class_type_die, 1129 class_type, 1130 failures); 1131 1132 // FIXME do something with these failures that's smarter than 1133 // just dropping them on the ground. Unfortunately classes don't 1134 // like having stuff added to them after their definitions are 1135 // complete... 1136 1137 type_ptr = dwarf->GetDIEToType()[die.GetDIE()]; 1138 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) 1139 { 1140 type_sp = type_ptr->shared_from_this(); 1141 break; 1142 } 1143 } 1144 } 1145 1146 if (specification_die_form.IsValid()) 1147 { 1148 // We have a specification which we are going to base our function 1149 // prototype off of, so we need this type to be completed so that the 1150 // m_die_to_decl_ctx for the method in the specification has a valid 1151 // clang decl context. 1152 class_type->GetForwardCompilerType (); 1153 // If we have a specification, then the function type should have been 1154 // made with the specification and not with this die. 1155 DWARFDIE spec_die = dwarf->DebugInfo()->GetDIE(DIERef(specification_die_form)); 1156 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (spec_die); 1157 if (spec_clang_decl_ctx) 1158 { 1159 LinkDeclContextToDIE(spec_clang_decl_ctx, die); 1160 } 1161 else 1162 { 1163 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8" PRIx64 ") has no decl\n", 1164 die.GetID(), 1165 specification_die_form.Reference()); 1166 } 1167 type_handled = true; 1168 } 1169 else if (abstract_origin_die_form.IsValid()) 1170 { 1171 // We have a specification which we are going to base our function 1172 // prototype off of, so we need this type to be completed so that the 1173 // m_die_to_decl_ctx for the method in the abstract origin has a valid 1174 // clang decl context. 1175 class_type->GetForwardCompilerType (); 1176 1177 DWARFDIE abs_die = dwarf->DebugInfo()->GetDIE (DIERef(abstract_origin_die_form)); 1178 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (abs_die); 1179 if (abs_clang_decl_ctx) 1180 { 1181 LinkDeclContextToDIE (abs_clang_decl_ctx, die); 1182 } 1183 else 1184 { 1185 dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8" PRIx64 ") has no decl\n", 1186 die.GetID(), 1187 abstract_origin_die_form.Reference()); 1188 } 1189 type_handled = true; 1190 } 1191 else 1192 { 1193 CompilerType class_opaque_type = class_type->GetForwardCompilerType (); 1194 if (ClangASTContext::IsCXXClassType(class_opaque_type)) 1195 { 1196 if (class_opaque_type.IsBeingDefined ()) 1197 { 1198 // Neither GCC 4.2 nor clang++ currently set a valid accessibility 1199 // in the DWARF for C++ methods... Default to public for now... 1200 if (accessibility == eAccessNone) 1201 accessibility = eAccessPublic; 1202 1203 if (!is_static && !die.HasChildren()) 1204 { 1205 // We have a C++ member function with no children (this pointer!) 1206 // and clang will get mad if we try and make a function that isn't 1207 // well formed in the DWARF, so we will just skip it... 1208 type_handled = true; 1209 } 1210 else 1211 { 1212 clang::CXXMethodDecl *cxx_method_decl; 1213 // REMOVE THE CRASH DESCRIPTION BELOW 1214 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s", 1215 type_name_cstr, 1216 class_type->GetName().GetCString(), 1217 die.GetID(), 1218 dwarf->GetObjectFile()->GetFileSpec().GetPath().c_str()); 1219 1220 const bool is_attr_used = false; 1221 1222 cxx_method_decl = m_ast.AddMethodToCXXRecordType (class_opaque_type.GetOpaqueQualType(), 1223 type_name_cstr, 1224 clang_type, 1225 accessibility, 1226 is_virtual, 1227 is_static, 1228 is_inline, 1229 is_explicit, 1230 is_attr_used, 1231 is_artificial); 1232 1233 type_handled = cxx_method_decl != NULL; 1234 1235 if (type_handled) 1236 { 1237 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die); 1238 1239 Host::SetCrashDescription (NULL); 1240 1241 1242 ClangASTMetadata metadata; 1243 metadata.SetUserID(die.GetID()); 1244 1245 if (!object_pointer_name.empty()) 1246 { 1247 metadata.SetObjectPtrName(object_pointer_name.c_str()); 1248 if (log) 1249 log->Printf ("Setting object pointer name: %s on method object %p.\n", 1250 object_pointer_name.c_str(), 1251 static_cast<void*>(cxx_method_decl)); 1252 } 1253 m_ast.SetMetadata (cxx_method_decl, metadata); 1254 } 1255 else 1256 { 1257 ignore_containing_context = true; 1258 } 1259 } 1260 } 1261 else 1262 { 1263 // We were asked to parse the type for a method in a class, yet the 1264 // class hasn't been asked to complete itself through the 1265 // clang::ExternalASTSource protocol, so we need to just have the 1266 // class complete itself and do things the right way, then our 1267 // DIE should then have an entry in the dwarf->GetDIEToType() map. First 1268 // we need to modify the dwarf->GetDIEToType() so it doesn't think we are 1269 // trying to parse this DIE anymore... 1270 dwarf->GetDIEToType()[die.GetDIE()] = NULL; 1271 1272 // Now we get the full type to force our class type to complete itself 1273 // using the clang::ExternalASTSource protocol which will parse all 1274 // base classes and all methods (including the method for this DIE). 1275 class_type->GetFullCompilerType (); 1276 1277 // The type for this DIE should have been filled in the function call above 1278 type_ptr = dwarf->GetDIEToType()[die.GetDIE()]; 1279 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED) 1280 { 1281 type_sp = type_ptr->shared_from_this(); 1282 break; 1283 } 1284 1285 // FIXME This is fixing some even uglier behavior but we really need to 1286 // uniq the methods of each class as well as the class itself. 1287 // <rdar://problem/11240464> 1288 type_handled = true; 1289 } 1290 } 1291 } 1292 } 1293 } 1294 } 1295 1296 if (!type_handled) 1297 { 1298 // We just have a function that isn't part of a class 1299 clang::FunctionDecl *function_decl = m_ast.CreateFunctionDeclaration (ignore_containing_context ? m_ast.GetTranslationUnitDecl() : containing_decl_ctx, 1300 type_name_cstr, 1301 clang_type, 1302 storage, 1303 is_inline); 1304 1305 // if (template_param_infos.GetSize() > 0) 1306 // { 1307 // clang::FunctionTemplateDecl *func_template_decl = CreateFunctionTemplateDecl (containing_decl_ctx, 1308 // function_decl, 1309 // type_name_cstr, 1310 // template_param_infos); 1311 // 1312 // CreateFunctionTemplateSpecializationInfo (function_decl, 1313 // func_template_decl, 1314 // template_param_infos); 1315 // } 1316 // Add the decl to our DIE to decl context map 1317 assert (function_decl); 1318 LinkDeclContextToDIE(function_decl, die); 1319 if (!function_param_decls.empty()) 1320 m_ast.SetFunctionParameters (function_decl, 1321 &function_param_decls.front(), 1322 function_param_decls.size()); 1323 1324 ClangASTMetadata metadata; 1325 metadata.SetUserID(die.GetID()); 1326 1327 if (!object_pointer_name.empty()) 1328 { 1329 metadata.SetObjectPtrName(object_pointer_name.c_str()); 1330 if (log) 1331 log->Printf ("Setting object pointer name: %s on function object %p.", 1332 object_pointer_name.c_str(), 1333 static_cast<void*>(function_decl)); 1334 } 1335 m_ast.SetMetadata (function_decl, metadata); 1336 } 1337 } 1338 type_sp.reset( new Type (die.GetID(), 1339 dwarf, 1340 type_name_const_str, 1341 0, 1342 NULL, 1343 LLDB_INVALID_UID, 1344 Type::eEncodingIsUID, 1345 &decl, 1346 clang_type, 1347 Type::eResolveStateFull)); 1348 assert(type_sp.get()); 1349 } 1350 break; 1351 1352 case DW_TAG_array_type: 1353 { 1354 // Set a bit that lets us know that we are currently parsing this 1355 dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED; 1356 1357 DWARFFormValue type_die_form; 1358 int64_t first_index = 0; 1359 uint32_t byte_stride = 0; 1360 uint32_t bit_stride = 0; 1361 bool is_vector = false; 1362 const size_t num_attributes = die.GetAttributes (attributes); 1363 1364 if (num_attributes > 0) 1365 { 1366 uint32_t i; 1367 for (i=0; i<num_attributes; ++i) 1368 { 1369 attr = attributes.AttributeAtIndex(i); 1370 if (attributes.ExtractFormValueAtIndex(i, form_value)) 1371 { 1372 switch (attr) 1373 { 1374 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 1375 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 1376 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 1377 case DW_AT_name: 1378 type_name_cstr = form_value.AsCString(); 1379 type_name_const_str.SetCString(type_name_cstr); 1380 break; 1381 1382 case DW_AT_type: type_die_form = form_value; break; 1383 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break; 1384 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break; 1385 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break; 1386 case DW_AT_GNU_vector: is_vector = form_value.Boolean(); break; 1387 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break; 1388 case DW_AT_declaration: break; // is_forward_declaration = form_value.Boolean(); break; 1389 case DW_AT_allocated: 1390 case DW_AT_associated: 1391 case DW_AT_data_location: 1392 case DW_AT_description: 1393 case DW_AT_ordering: 1394 case DW_AT_start_scope: 1395 case DW_AT_visibility: 1396 case DW_AT_specification: 1397 case DW_AT_abstract_origin: 1398 case DW_AT_sibling: 1399 break; 1400 } 1401 } 1402 } 1403 1404 DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr); 1405 1406 Type *element_type = dwarf->ResolveTypeUID(DIERef(type_die_form).GetUID()); 1407 1408 if (element_type) 1409 { 1410 std::vector<uint64_t> element_orders; 1411 ParseChildArrayInfo(sc, die, first_index, element_orders, byte_stride, bit_stride); 1412 if (byte_stride == 0 && bit_stride == 0) 1413 byte_stride = element_type->GetByteSize(); 1414 CompilerType array_element_type = element_type->GetForwardCompilerType (); 1415 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride; 1416 if (element_orders.size() > 0) 1417 { 1418 uint64_t num_elements = 0; 1419 std::vector<uint64_t>::const_reverse_iterator pos; 1420 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend(); 1421 for (pos = element_orders.rbegin(); pos != end; ++pos) 1422 { 1423 num_elements = *pos; 1424 clang_type = m_ast.CreateArrayType (array_element_type, 1425 num_elements, 1426 is_vector); 1427 array_element_type = clang_type; 1428 array_element_bit_stride = num_elements ? 1429 array_element_bit_stride * num_elements : 1430 array_element_bit_stride; 1431 } 1432 } 1433 else 1434 { 1435 clang_type = m_ast.CreateArrayType (array_element_type, 0, is_vector); 1436 } 1437 ConstString empty_name; 1438 type_sp.reset( new Type (die.GetID(), 1439 dwarf, 1440 empty_name, 1441 array_element_bit_stride / 8, 1442 NULL, 1443 DIERef(type_die_form).GetUID(), 1444 Type::eEncodingIsUID, 1445 &decl, 1446 clang_type, 1447 Type::eResolveStateFull)); 1448 type_sp->SetEncodingType (element_type); 1449 } 1450 } 1451 } 1452 break; 1453 1454 case DW_TAG_ptr_to_member_type: 1455 { 1456 DWARFFormValue type_die_form; 1457 DWARFFormValue containing_type_die_form; 1458 1459 const size_t num_attributes = die.GetAttributes (attributes); 1460 1461 if (num_attributes > 0) { 1462 uint32_t i; 1463 for (i=0; i<num_attributes; ++i) 1464 { 1465 attr = attributes.AttributeAtIndex(i); 1466 if (attributes.ExtractFormValueAtIndex(i, form_value)) 1467 { 1468 switch (attr) 1469 { 1470 case DW_AT_type: 1471 type_die_form = form_value; break; 1472 case DW_AT_containing_type: 1473 containing_type_die_form = form_value; break; 1474 } 1475 } 1476 } 1477 1478 Type *pointee_type = dwarf->ResolveTypeUID(DIERef(type_die_form).GetUID()); 1479 Type *class_type = dwarf->ResolveTypeUID(DIERef(containing_type_die_form).GetUID()); 1480 1481 CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType (); 1482 CompilerType class_clang_type = class_type->GetLayoutCompilerType (); 1483 1484 clang_type = ClangASTContext::CreateMemberPointerType(pointee_clang_type, class_clang_type); 1485 1486 byte_size = clang_type.GetByteSize(nullptr); 1487 1488 type_sp.reset( new Type (die.GetID(), 1489 dwarf, 1490 type_name_const_str, 1491 byte_size, 1492 NULL, 1493 LLDB_INVALID_UID, 1494 Type::eEncodingIsUID, 1495 NULL, 1496 clang_type, 1497 Type::eResolveStateForward)); 1498 } 1499 1500 break; 1501 } 1502 default: 1503 dwarf->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and attach the file at the start of this error message", 1504 die.GetOffset(), 1505 tag, 1506 DW_TAG_value_to_name(tag)); 1507 break; 1508 } 1509 1510 if (type_sp.get()) 1511 { 1512 DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die); 1513 dw_tag_t sc_parent_tag = sc_parent_die.Tag(); 1514 1515 SymbolContextScope * symbol_context_scope = NULL; 1516 if (sc_parent_tag == DW_TAG_compile_unit) 1517 { 1518 symbol_context_scope = sc.comp_unit; 1519 } 1520 else if (sc.function != NULL && sc_parent_die) 1521 { 1522 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID()); 1523 if (symbol_context_scope == NULL) 1524 symbol_context_scope = sc.function; 1525 } 1526 1527 if (symbol_context_scope != NULL) 1528 { 1529 type_sp->SetSymbolContextScope(symbol_context_scope); 1530 } 1531 1532 // We are ready to put this type into the uniqued list up at the module level 1533 type_list->Insert (type_sp); 1534 1535 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get(); 1536 } 1537 } 1538 else if (type_ptr != DIE_IS_BEING_PARSED) 1539 { 1540 type_sp = type_ptr->shared_from_this(); 1541 } 1542 } 1543 return type_sp; 1544 } 1545 1546 // DWARF parsing functions 1547 1548 class DWARFASTParserClang::DelayedAddObjCClassProperty 1549 { 1550 public: 1551 DelayedAddObjCClassProperty(const CompilerType &class_opaque_type, 1552 const char *property_name, 1553 const CompilerType &property_opaque_type, // The property type is only required if you don't have an ivar decl 1554 clang::ObjCIvarDecl *ivar_decl, 1555 const char *property_setter_name, 1556 const char *property_getter_name, 1557 uint32_t property_attributes, 1558 const ClangASTMetadata *metadata) : 1559 m_class_opaque_type (class_opaque_type), 1560 m_property_name (property_name), 1561 m_property_opaque_type (property_opaque_type), 1562 m_ivar_decl (ivar_decl), 1563 m_property_setter_name (property_setter_name), 1564 m_property_getter_name (property_getter_name), 1565 m_property_attributes (property_attributes) 1566 { 1567 if (metadata != NULL) 1568 { 1569 m_metadata_ap.reset(new ClangASTMetadata()); 1570 *m_metadata_ap = *metadata; 1571 } 1572 } 1573 1574 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs) 1575 { 1576 *this = rhs; 1577 } 1578 1579 DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs) 1580 { 1581 m_class_opaque_type = rhs.m_class_opaque_type; 1582 m_property_name = rhs.m_property_name; 1583 m_property_opaque_type = rhs.m_property_opaque_type; 1584 m_ivar_decl = rhs.m_ivar_decl; 1585 m_property_setter_name = rhs.m_property_setter_name; 1586 m_property_getter_name = rhs.m_property_getter_name; 1587 m_property_attributes = rhs.m_property_attributes; 1588 1589 if (rhs.m_metadata_ap.get()) 1590 { 1591 m_metadata_ap.reset (new ClangASTMetadata()); 1592 *m_metadata_ap = *rhs.m_metadata_ap; 1593 } 1594 return *this; 1595 } 1596 1597 bool 1598 Finalize() 1599 { 1600 return ClangASTContext::AddObjCClassProperty (m_class_opaque_type, 1601 m_property_name, 1602 m_property_opaque_type, 1603 m_ivar_decl, 1604 m_property_setter_name, 1605 m_property_getter_name, 1606 m_property_attributes, 1607 m_metadata_ap.get()); 1608 } 1609 1610 private: 1611 CompilerType m_class_opaque_type; 1612 const char *m_property_name; 1613 CompilerType m_property_opaque_type; 1614 clang::ObjCIvarDecl *m_ivar_decl; 1615 const char *m_property_setter_name; 1616 const char *m_property_getter_name; 1617 uint32_t m_property_attributes; 1618 std::unique_ptr<ClangASTMetadata> m_metadata_ap; 1619 }; 1620 1621 bool 1622 DWARFASTParserClang::ParseTemplateDIE (const DWARFDIE &die, 1623 ClangASTContext::TemplateParameterInfos &template_param_infos) 1624 { 1625 const dw_tag_t tag = die.Tag(); 1626 1627 switch (tag) 1628 { 1629 case DW_TAG_template_type_parameter: 1630 case DW_TAG_template_value_parameter: 1631 { 1632 DWARFAttributes attributes; 1633 const size_t num_attributes = die.GetAttributes (attributes); 1634 const char *name = NULL; 1635 Type *lldb_type = NULL; 1636 CompilerType clang_type; 1637 uint64_t uval64 = 0; 1638 bool uval64_valid = false; 1639 if (num_attributes > 0) 1640 { 1641 DWARFFormValue form_value; 1642 for (size_t i=0; i<num_attributes; ++i) 1643 { 1644 const dw_attr_t attr = attributes.AttributeAtIndex(i); 1645 1646 switch (attr) 1647 { 1648 case DW_AT_name: 1649 if (attributes.ExtractFormValueAtIndex(i, form_value)) 1650 name = form_value.AsCString(); 1651 break; 1652 1653 case DW_AT_type: 1654 if (attributes.ExtractFormValueAtIndex(i, form_value)) 1655 { 1656 lldb_type = die.ResolveTypeUID(DIERef(form_value).GetUID()); 1657 if (lldb_type) 1658 clang_type = lldb_type->GetForwardCompilerType (); 1659 } 1660 break; 1661 1662 case DW_AT_const_value: 1663 if (attributes.ExtractFormValueAtIndex(i, form_value)) 1664 { 1665 uval64_valid = true; 1666 uval64 = form_value.Unsigned(); 1667 } 1668 break; 1669 default: 1670 break; 1671 } 1672 } 1673 1674 clang::ASTContext *ast = m_ast.getASTContext(); 1675 if (!clang_type) 1676 clang_type = m_ast.GetBasicType(eBasicTypeVoid); 1677 1678 if (clang_type) 1679 { 1680 bool is_signed = false; 1681 if (name && name[0]) 1682 template_param_infos.names.push_back(name); 1683 else 1684 template_param_infos.names.push_back(NULL); 1685 1686 if (tag == DW_TAG_template_value_parameter && 1687 lldb_type != NULL && 1688 clang_type.IsIntegerType (is_signed) && 1689 uval64_valid) 1690 { 1691 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed); 1692 template_param_infos.args.push_back (clang::TemplateArgument (*ast, 1693 llvm::APSInt(apint), 1694 ClangASTContext::GetQualType(clang_type))); 1695 } 1696 else 1697 { 1698 template_param_infos.args.push_back (clang::TemplateArgument (ClangASTContext::GetQualType(clang_type))); 1699 } 1700 } 1701 else 1702 { 1703 return false; 1704 } 1705 1706 } 1707 } 1708 return true; 1709 1710 default: 1711 break; 1712 } 1713 return false; 1714 } 1715 1716 bool 1717 DWARFASTParserClang::ParseTemplateParameterInfos (const DWARFDIE &parent_die, 1718 ClangASTContext::TemplateParameterInfos &template_param_infos) 1719 { 1720 1721 if (!parent_die) 1722 return false; 1723 1724 Args template_parameter_names; 1725 for (DWARFDIE die = parent_die.GetFirstChild(); 1726 die.IsValid(); 1727 die = die.GetSibling()) 1728 { 1729 const dw_tag_t tag = die.Tag(); 1730 1731 switch (tag) 1732 { 1733 case DW_TAG_template_type_parameter: 1734 case DW_TAG_template_value_parameter: 1735 ParseTemplateDIE (die, template_param_infos); 1736 break; 1737 1738 default: 1739 break; 1740 } 1741 } 1742 if (template_param_infos.args.empty()) 1743 return false; 1744 return template_param_infos.args.size() == template_param_infos.names.size(); 1745 } 1746 1747 bool 1748 DWARFASTParserClang::CompleteTypeFromDWARF (const DWARFDIE &die, 1749 lldb_private::Type *type, 1750 CompilerType &clang_type) 1751 { 1752 // Disable external storage for this type so we don't get anymore 1753 // clang::ExternalASTSource queries for this type. 1754 m_ast.SetHasExternalStorage (clang_type.GetOpaqueQualType(), false); 1755 1756 if (!die) 1757 return false; 1758 1759 const dw_tag_t tag = die.Tag(); 1760 1761 SymbolFileDWARF *dwarf = die.GetDWARF(); 1762 1763 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION)); 1764 if (log) 1765 dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log, 1766 "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...", 1767 die.GetID(), 1768 die.GetTagAsCString(), 1769 type->GetName().AsCString()); 1770 assert (clang_type); 1771 DWARFAttributes attributes; 1772 switch (tag) 1773 { 1774 case DW_TAG_structure_type: 1775 case DW_TAG_union_type: 1776 case DW_TAG_class_type: 1777 { 1778 LayoutInfo layout_info; 1779 1780 { 1781 if (die.HasChildren()) 1782 { 1783 LanguageType class_language = eLanguageTypeUnknown; 1784 if (ClangASTContext::IsObjCObjectOrInterfaceType(clang_type)) 1785 { 1786 class_language = eLanguageTypeObjC; 1787 // For objective C we don't start the definition when 1788 // the class is created. 1789 ClangASTContext::StartTagDeclarationDefinition (clang_type); 1790 } 1791 1792 int tag_decl_kind = -1; 1793 AccessType default_accessibility = eAccessNone; 1794 if (tag == DW_TAG_structure_type) 1795 { 1796 tag_decl_kind = clang::TTK_Struct; 1797 default_accessibility = eAccessPublic; 1798 } 1799 else if (tag == DW_TAG_union_type) 1800 { 1801 tag_decl_kind = clang::TTK_Union; 1802 default_accessibility = eAccessPublic; 1803 } 1804 else if (tag == DW_TAG_class_type) 1805 { 1806 tag_decl_kind = clang::TTK_Class; 1807 default_accessibility = eAccessPrivate; 1808 } 1809 1810 SymbolContext sc(die.GetLLDBCompileUnit()); 1811 std::vector<clang::CXXBaseSpecifier *> base_classes; 1812 std::vector<int> member_accessibilities; 1813 bool is_a_class = false; 1814 // Parse members and base classes first 1815 DWARFDIECollection member_function_dies; 1816 1817 DelayedPropertyList delayed_properties; 1818 if (!ParseChildMembers (sc, 1819 die, 1820 clang_type, 1821 class_language, 1822 base_classes, 1823 member_accessibilities, 1824 member_function_dies, 1825 delayed_properties, 1826 default_accessibility, 1827 is_a_class, 1828 layout_info)) 1829 { 1830 auto module = dwarf->GetObjectFile()->GetModule(); 1831 module->ReportError (":: Class %s has members with incomplete type.", die.GetName()); 1832 if (die.GetCU()->GetProducer() == DWARFCompileUnit::eProducerClang) 1833 module->ReportError(":: Try compiling the source file with -fno-limit-debug-info."); 1834 1835 return false; 1836 } 1837 1838 // Now parse any methods if there were any... 1839 size_t num_functions = member_function_dies.Size(); 1840 if (num_functions > 0) 1841 { 1842 for (size_t i=0; i<num_functions; ++i) 1843 { 1844 dwarf->ResolveType(member_function_dies.GetDIEAtIndex(i)); 1845 } 1846 } 1847 1848 if (class_language == eLanguageTypeObjC) 1849 { 1850 ConstString class_name (clang_type.GetTypeName()); 1851 if (class_name) 1852 { 1853 DIEArray method_die_offsets; 1854 dwarf->GetObjCMethodDIEOffsets(class_name, method_die_offsets); 1855 1856 if (!method_die_offsets.empty()) 1857 { 1858 DWARFDebugInfo* debug_info = dwarf->DebugInfo(); 1859 1860 const size_t num_matches = method_die_offsets.size(); 1861 for (size_t i=0; i<num_matches; ++i) 1862 { 1863 const DIERef& die_ref = method_die_offsets[i]; 1864 DWARFDIE method_die = debug_info->GetDIE (die_ref); 1865 1866 if (method_die) 1867 method_die.ResolveType (); 1868 } 1869 } 1870 1871 for (DelayedPropertyList::iterator pi = delayed_properties.begin(), pe = delayed_properties.end(); 1872 pi != pe; 1873 ++pi) 1874 pi->Finalize(); 1875 } 1876 } 1877 1878 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we 1879 // need to tell the clang type it is actually a class. 1880 if (class_language != eLanguageTypeObjC) 1881 { 1882 if (is_a_class && tag_decl_kind != clang::TTK_Class) 1883 m_ast.SetTagTypeKind (ClangASTContext::GetQualType(clang_type), clang::TTK_Class); 1884 } 1885 1886 // Since DW_TAG_structure_type gets used for both classes 1887 // and structures, we may need to set any DW_TAG_member 1888 // fields to have a "private" access if none was specified. 1889 // When we parsed the child members we tracked that actual 1890 // accessibility value for each DW_TAG_member in the 1891 // "member_accessibilities" array. If the value for the 1892 // member is zero, then it was set to the "default_accessibility" 1893 // which for structs was "public". Below we correct this 1894 // by setting any fields to "private" that weren't correctly 1895 // set. 1896 if (is_a_class && !member_accessibilities.empty()) 1897 { 1898 // This is a class and all members that didn't have 1899 // their access specified are private. 1900 m_ast.SetDefaultAccessForRecordFields (m_ast.GetAsRecordDecl(clang_type), 1901 eAccessPrivate, 1902 &member_accessibilities.front(), 1903 member_accessibilities.size()); 1904 } 1905 1906 if (!base_classes.empty()) 1907 { 1908 // Make sure all base classes refer to complete types and not 1909 // forward declarations. If we don't do this, clang will crash 1910 // with an assertion in the call to clang_type.SetBaseClassesForClassType() 1911 for (auto &base_class : base_classes) 1912 { 1913 clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo(); 1914 if (type_source_info) 1915 { 1916 CompilerType base_class_type (&m_ast, type_source_info->getType().getAsOpaquePtr()); 1917 if (base_class_type.GetCompleteType() == false) 1918 { 1919 auto module = dwarf->GetObjectFile()->GetModule(); 1920 module->ReportError ( 1921 ":: Class '%s' has a base class '%s' which does not have a complete definition.", 1922 die.GetName(), 1923 base_class_type.GetTypeName().GetCString()); 1924 if (die.GetCU()->GetProducer() == DWARFCompileUnit::eProducerClang) 1925 module->ReportError (":: Try compiling the source file with -fno-limit-debug-info."); 1926 1927 return false; 1928 } 1929 } 1930 } 1931 m_ast.SetBaseClassesForClassType (clang_type.GetOpaqueQualType(), 1932 &base_classes.front(), 1933 base_classes.size()); 1934 1935 // Clang will copy each CXXBaseSpecifier in "base_classes" 1936 // so we have to free them all. 1937 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(), 1938 base_classes.size()); 1939 } 1940 } 1941 } 1942 1943 ClangASTContext::BuildIndirectFields (clang_type); 1944 ClangASTContext::CompleteTagDeclarationDefinition (clang_type); 1945 1946 if (!layout_info.field_offsets.empty() || 1947 !layout_info.base_offsets.empty() || 1948 !layout_info.vbase_offsets.empty() ) 1949 { 1950 if (type) 1951 layout_info.bit_size = type->GetByteSize() * 8; 1952 if (layout_info.bit_size == 0) 1953 layout_info.bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8; 1954 1955 clang::CXXRecordDecl *record_decl = m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType()); 1956 if (record_decl) 1957 { 1958 if (log) 1959 { 1960 ModuleSP module_sp = dwarf->GetObjectFile()->GetModule(); 1961 1962 if (module_sp) 1963 { 1964 module_sp->LogMessage (log, 1965 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) caching layout info for record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])", 1966 static_cast<void*>(clang_type.GetOpaqueQualType()), 1967 static_cast<void*>(record_decl), 1968 layout_info.bit_size, 1969 layout_info.alignment, 1970 static_cast<uint32_t>(layout_info.field_offsets.size()), 1971 static_cast<uint32_t>(layout_info.base_offsets.size()), 1972 static_cast<uint32_t>(layout_info.vbase_offsets.size())); 1973 1974 uint32_t idx; 1975 { 1976 llvm::DenseMap<const clang::FieldDecl *, uint64_t>::const_iterator pos, 1977 end = layout_info.field_offsets.end(); 1978 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx) 1979 { 1980 module_sp->LogMessage(log, 1981 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }", 1982 static_cast<void *>(clang_type.GetOpaqueQualType()), 1983 idx, 1984 static_cast<uint32_t>(pos->second), 1985 pos->first->getNameAsString().c_str()); 1986 } 1987 } 1988 1989 { 1990 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos, 1991 base_end = layout_info.base_offsets.end(); 1992 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx) 1993 { 1994 module_sp->LogMessage(log, 1995 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }", 1996 clang_type.GetOpaqueQualType(), idx, (uint32_t)base_pos->second.getQuantity(), 1997 base_pos->first->getNameAsString().c_str()); 1998 } 1999 } 2000 { 2001 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos, 2002 vbase_end = layout_info.vbase_offsets.end(); 2003 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx) 2004 { 2005 module_sp->LogMessage(log, 2006 "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }", 2007 static_cast<void *>(clang_type.GetOpaqueQualType()), idx, 2008 static_cast<uint32_t>(vbase_pos->second.getQuantity()), 2009 vbase_pos->first->getNameAsString().c_str()); 2010 } 2011 } 2012 2013 } 2014 } 2015 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info)); 2016 } 2017 } 2018 } 2019 2020 return (bool)clang_type; 2021 2022 case DW_TAG_enumeration_type: 2023 ClangASTContext::StartTagDeclarationDefinition (clang_type); 2024 if (die.HasChildren()) 2025 { 2026 SymbolContext sc(die.GetLLDBCompileUnit()); 2027 bool is_signed = false; 2028 clang_type.IsIntegerType(is_signed); 2029 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), die); 2030 } 2031 ClangASTContext::CompleteTagDeclarationDefinition (clang_type); 2032 return (bool)clang_type; 2033 2034 default: 2035 assert(false && "not a forward clang type decl!"); 2036 break; 2037 } 2038 2039 return false; 2040 } 2041 2042 std::vector<DWARFDIE> 2043 DWARFASTParserClang::GetDIEForDeclContext (lldb_private::CompilerDeclContext decl_context) 2044 { 2045 std::vector<DWARFDIE> result; 2046 for (auto it = m_decl_ctx_to_die.find((clang::DeclContext *)decl_context.GetOpaqueDeclContext()); it != m_decl_ctx_to_die.end(); it++) 2047 result.push_back(it->second); 2048 return result; 2049 } 2050 2051 CompilerDecl 2052 DWARFASTParserClang::GetDeclForUIDFromDWARF (const DWARFDIE &die) 2053 { 2054 clang::Decl *clang_decl = GetClangDeclForDIE(die); 2055 if (clang_decl != nullptr) 2056 return CompilerDecl(&m_ast, clang_decl); 2057 return CompilerDecl(); 2058 } 2059 2060 CompilerDeclContext 2061 DWARFASTParserClang::GetDeclContextForUIDFromDWARF (const DWARFDIE &die) 2062 { 2063 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE (die); 2064 if (clang_decl_ctx) 2065 return CompilerDeclContext(&m_ast, clang_decl_ctx); 2066 return CompilerDeclContext(); 2067 } 2068 2069 CompilerDeclContext 2070 DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF (const DWARFDIE &die) 2071 { 2072 clang::DeclContext *clang_decl_ctx = GetClangDeclContextContainingDIE (die, nullptr); 2073 if (clang_decl_ctx) 2074 return CompilerDeclContext(&m_ast, clang_decl_ctx); 2075 return CompilerDeclContext(); 2076 } 2077 2078 size_t 2079 DWARFASTParserClang::ParseChildEnumerators (const SymbolContext& sc, 2080 lldb_private::CompilerType &clang_type, 2081 bool is_signed, 2082 uint32_t enumerator_byte_size, 2083 const DWARFDIE &parent_die) 2084 { 2085 if (!parent_die) 2086 return 0; 2087 2088 size_t enumerators_added = 0; 2089 2090 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling()) 2091 { 2092 const dw_tag_t tag = die.Tag(); 2093 if (tag == DW_TAG_enumerator) 2094 { 2095 DWARFAttributes attributes; 2096 const size_t num_child_attributes = die.GetAttributes(attributes); 2097 if (num_child_attributes > 0) 2098 { 2099 const char *name = NULL; 2100 bool got_value = false; 2101 int64_t enum_value = 0; 2102 Declaration decl; 2103 2104 uint32_t i; 2105 for (i=0; i<num_child_attributes; ++i) 2106 { 2107 const dw_attr_t attr = attributes.AttributeAtIndex(i); 2108 DWARFFormValue form_value; 2109 if (attributes.ExtractFormValueAtIndex(i, form_value)) 2110 { 2111 switch (attr) 2112 { 2113 case DW_AT_const_value: 2114 got_value = true; 2115 if (is_signed) 2116 enum_value = form_value.Signed(); 2117 else 2118 enum_value = form_value.Unsigned(); 2119 break; 2120 2121 case DW_AT_name: 2122 name = form_value.AsCString(); 2123 break; 2124 2125 case DW_AT_description: 2126 default: 2127 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 2128 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 2129 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 2130 case DW_AT_sibling: 2131 break; 2132 } 2133 } 2134 } 2135 2136 if (name && name[0] && got_value) 2137 { 2138 m_ast.AddEnumerationValueToEnumerationType (clang_type.GetOpaqueQualType(), 2139 m_ast.GetEnumerationIntegerType(clang_type.GetOpaqueQualType()), 2140 decl, 2141 name, 2142 enum_value, 2143 enumerator_byte_size * 8); 2144 ++enumerators_added; 2145 } 2146 } 2147 } 2148 } 2149 return enumerators_added; 2150 } 2151 2152 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE) 2153 2154 class DIEStack 2155 { 2156 public: 2157 2158 void Push (const DWARFDIE &die) 2159 { 2160 m_dies.push_back (die); 2161 } 2162 2163 2164 void LogDIEs (Log *log) 2165 { 2166 StreamString log_strm; 2167 const size_t n = m_dies.size(); 2168 log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n); 2169 for (size_t i=0; i<n; i++) 2170 { 2171 std::string qualified_name; 2172 const DWARFDIE &die = m_dies[i]; 2173 die.GetQualifiedName(qualified_name); 2174 log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n", 2175 (uint64_t)i, 2176 die.GetOffset(), 2177 die.GetTagAsCString(), 2178 qualified_name.c_str()); 2179 } 2180 log->PutCString(log_strm.GetData()); 2181 } 2182 void Pop () 2183 { 2184 m_dies.pop_back(); 2185 } 2186 2187 class ScopedPopper 2188 { 2189 public: 2190 ScopedPopper (DIEStack &die_stack) : 2191 m_die_stack (die_stack), 2192 m_valid (false) 2193 { 2194 } 2195 2196 void 2197 Push (const DWARFDIE &die) 2198 { 2199 m_valid = true; 2200 m_die_stack.Push (die); 2201 } 2202 2203 ~ScopedPopper () 2204 { 2205 if (m_valid) 2206 m_die_stack.Pop(); 2207 } 2208 2209 2210 2211 protected: 2212 DIEStack &m_die_stack; 2213 bool m_valid; 2214 }; 2215 2216 protected: 2217 typedef std::vector<DWARFDIE> Stack; 2218 Stack m_dies; 2219 }; 2220 #endif 2221 2222 Function * 2223 DWARFASTParserClang::ParseFunctionFromDWARF (const SymbolContext& sc, 2224 const DWARFDIE &die) 2225 { 2226 DWARFRangeList func_ranges; 2227 const char *name = NULL; 2228 const char *mangled = NULL; 2229 int decl_file = 0; 2230 int decl_line = 0; 2231 int decl_column = 0; 2232 int call_file = 0; 2233 int call_line = 0; 2234 int call_column = 0; 2235 DWARFExpression frame_base(die.GetCU()); 2236 2237 const dw_tag_t tag = die.Tag(); 2238 2239 if (tag != DW_TAG_subprogram) 2240 return NULL; 2241 2242 if (die.GetDIENamesAndRanges (name, 2243 mangled, 2244 func_ranges, 2245 decl_file, 2246 decl_line, 2247 decl_column, 2248 call_file, 2249 call_line, 2250 call_column, 2251 &frame_base)) 2252 { 2253 2254 // Union of all ranges in the function DIE (if the function is discontiguous) 2255 AddressRange func_range; 2256 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0); 2257 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0); 2258 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr) 2259 { 2260 ModuleSP module_sp (die.GetModule()); 2261 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, module_sp->GetSectionList()); 2262 if (func_range.GetBaseAddress().IsValid()) 2263 func_range.SetByteSize(highest_func_addr - lowest_func_addr); 2264 } 2265 2266 if (func_range.GetBaseAddress().IsValid()) 2267 { 2268 Mangled func_name; 2269 if (mangled) 2270 func_name.SetValue(ConstString(mangled), true); 2271 else if (die.GetParent().Tag() == DW_TAG_compile_unit && 2272 Language::LanguageIsCPlusPlus(die.GetLanguage()) && 2273 name && strcmp(name, "main") != 0) 2274 { 2275 // If the mangled name is not present in the DWARF, generate the demangled name 2276 // using the decl context. We skip if the function is "main" as its name is 2277 // never mangled. 2278 bool is_static = false; 2279 bool is_variadic = false; 2280 unsigned type_quals = 0; 2281 std::vector<CompilerType> param_types; 2282 std::vector<clang::ParmVarDecl*> param_decls; 2283 DWARFDeclContext decl_ctx; 2284 StreamString sstr; 2285 2286 die.GetDWARFDeclContext(decl_ctx); 2287 sstr << decl_ctx.GetQualifiedName(); 2288 2289 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE(die, nullptr); 2290 ParseChildParameters(sc, 2291 containing_decl_ctx, 2292 die, 2293 true, 2294 is_static, 2295 is_variadic, 2296 param_types, 2297 param_decls, 2298 type_quals); 2299 sstr << "("; 2300 for (size_t i = 0; i < param_types.size(); i++) 2301 { 2302 if (i > 0) 2303 sstr << ", "; 2304 sstr << param_types[i].GetTypeName(); 2305 } 2306 if (is_variadic) 2307 sstr << ", ..."; 2308 sstr << ")"; 2309 if (type_quals & clang::Qualifiers::Const) 2310 sstr << " const"; 2311 2312 func_name.SetValue(ConstString(sstr.GetData()), false); 2313 } 2314 else 2315 func_name.SetValue(ConstString(name), false); 2316 2317 FunctionSP func_sp; 2318 std::unique_ptr<Declaration> decl_ap; 2319 if (decl_file != 0 || decl_line != 0 || decl_column != 0) 2320 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file), 2321 decl_line, 2322 decl_column)); 2323 2324 SymbolFileDWARF *dwarf = die.GetDWARF(); 2325 // Supply the type _only_ if it has already been parsed 2326 Type *func_type = dwarf->GetDIEToType().lookup (die.GetDIE()); 2327 2328 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED); 2329 2330 if (dwarf->FixupAddress (func_range.GetBaseAddress())) 2331 { 2332 const user_id_t func_user_id = die.GetID(); 2333 func_sp.reset(new Function (sc.comp_unit, 2334 func_user_id, // UserID is the DIE offset 2335 func_user_id, 2336 func_name, 2337 func_type, 2338 func_range)); // first address range 2339 2340 if (func_sp.get() != NULL) 2341 { 2342 if (frame_base.IsValid()) 2343 func_sp->GetFrameBaseExpression() = frame_base; 2344 sc.comp_unit->AddFunction(func_sp); 2345 return func_sp.get(); 2346 } 2347 } 2348 } 2349 } 2350 return NULL; 2351 } 2352 2353 2354 bool 2355 DWARFASTParserClang::ParseChildMembers (const SymbolContext& sc, 2356 const DWARFDIE &parent_die, 2357 CompilerType &class_clang_type, 2358 const LanguageType class_language, 2359 std::vector<clang::CXXBaseSpecifier *>& base_classes, 2360 std::vector<int>& member_accessibilities, 2361 DWARFDIECollection& member_function_dies, 2362 DelayedPropertyList& delayed_properties, 2363 AccessType& default_accessibility, 2364 bool &is_a_class, 2365 LayoutInfo &layout_info) 2366 { 2367 if (!parent_die) 2368 return 0; 2369 2370 uint32_t incomplete_member_info_count = 0; 2371 uint32_t member_idx = 0; 2372 BitfieldInfo last_field_info; 2373 2374 ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule(); 2375 ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(class_clang_type.GetTypeSystem()); 2376 if (ast == nullptr) 2377 return 0; 2378 2379 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling()) 2380 { 2381 dw_tag_t tag = die.Tag(); 2382 2383 switch (tag) 2384 { 2385 case DW_TAG_member: 2386 case DW_TAG_APPLE_property: 2387 { 2388 DWARFAttributes attributes; 2389 const size_t num_attributes = die.GetAttributes (attributes); 2390 if (num_attributes > 0) 2391 { 2392 Declaration decl; 2393 //DWARFExpression location; 2394 const char *name = NULL; 2395 const char *prop_name = NULL; 2396 const char *prop_getter_name = NULL; 2397 const char *prop_setter_name = NULL; 2398 uint32_t prop_attributes = 0; 2399 2400 2401 bool is_artificial = false; 2402 DWARFFormValue encoding_form; 2403 AccessType accessibility = eAccessNone; 2404 uint32_t member_byte_offset = UINT32_MAX; 2405 size_t byte_size = 0; 2406 size_t bit_offset = 0; 2407 size_t bit_size = 0; 2408 bool is_external = false; // On DW_TAG_members, this means the member is static 2409 uint32_t i; 2410 for (i=0; i<num_attributes && !is_artificial; ++i) 2411 { 2412 const dw_attr_t attr = attributes.AttributeAtIndex(i); 2413 DWARFFormValue form_value; 2414 if (attributes.ExtractFormValueAtIndex(i, form_value)) 2415 { 2416 switch (attr) 2417 { 2418 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 2419 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 2420 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 2421 case DW_AT_name: name = form_value.AsCString(); break; 2422 case DW_AT_type: encoding_form = form_value; break; 2423 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break; 2424 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break; 2425 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break; 2426 case DW_AT_data_member_location: 2427 if (form_value.BlockData()) 2428 { 2429 Value initialValue(0); 2430 Value memberOffset(0); 2431 const DWARFDataExtractor& debug_info_data = die.GetDWARF()->get_debug_info_data(); 2432 uint32_t block_length = form_value.Unsigned(); 2433 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 2434 if (DWARFExpression::Evaluate(NULL, // ExecutionContext * 2435 NULL, // ClangExpressionVariableList * 2436 NULL, // ClangExpressionDeclMap * 2437 NULL, // RegisterContext * 2438 module_sp, 2439 debug_info_data, 2440 die.GetCU(), 2441 block_offset, 2442 block_length, 2443 eRegisterKindDWARF, 2444 &initialValue, 2445 memberOffset, 2446 NULL)) 2447 { 2448 member_byte_offset = memberOffset.ResolveValue(NULL).UInt(); 2449 } 2450 } 2451 else 2452 { 2453 // With DWARF 3 and later, if the value is an integer constant, 2454 // this form value is the offset in bytes from the beginning 2455 // of the containing entity. 2456 member_byte_offset = form_value.Unsigned(); 2457 } 2458 break; 2459 2460 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break; 2461 case DW_AT_artificial: is_artificial = form_value.Boolean(); break; 2462 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(); 2463 break; 2464 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(); 2465 break; 2466 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(); 2467 break; 2468 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break; 2469 case DW_AT_external: is_external = form_value.Boolean(); break; 2470 2471 default: 2472 case DW_AT_declaration: 2473 case DW_AT_description: 2474 case DW_AT_mutable: 2475 case DW_AT_visibility: 2476 case DW_AT_sibling: 2477 break; 2478 } 2479 } 2480 } 2481 2482 if (prop_name) 2483 { 2484 ConstString fixed_getter; 2485 ConstString fixed_setter; 2486 2487 // Check if the property getter/setter were provided as full 2488 // names. We want basenames, so we extract them. 2489 2490 if (prop_getter_name && prop_getter_name[0] == '-') 2491 { 2492 ObjCLanguage::MethodName prop_getter_method(prop_getter_name, true); 2493 prop_getter_name = prop_getter_method.GetSelector().GetCString(); 2494 } 2495 2496 if (prop_setter_name && prop_setter_name[0] == '-') 2497 { 2498 ObjCLanguage::MethodName prop_setter_method(prop_setter_name, true); 2499 prop_setter_name = prop_setter_method.GetSelector().GetCString(); 2500 } 2501 2502 // If the names haven't been provided, they need to be 2503 // filled in. 2504 2505 if (!prop_getter_name) 2506 { 2507 prop_getter_name = prop_name; 2508 } 2509 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly)) 2510 { 2511 StreamString ss; 2512 2513 ss.Printf("set%c%s:", 2514 toupper(prop_name[0]), 2515 &prop_name[1]); 2516 2517 fixed_setter.SetCString(ss.GetData()); 2518 prop_setter_name = fixed_setter.GetCString(); 2519 } 2520 } 2521 2522 // Clang has a DWARF generation bug where sometimes it 2523 // represents fields that are references with bad byte size 2524 // and bit size/offset information such as: 2525 // 2526 // DW_AT_byte_size( 0x00 ) 2527 // DW_AT_bit_size( 0x40 ) 2528 // DW_AT_bit_offset( 0xffffffffffffffc0 ) 2529 // 2530 // So check the bit offset to make sure it is sane, and if 2531 // the values are not sane, remove them. If we don't do this 2532 // then we will end up with a crash if we try to use this 2533 // type in an expression when clang becomes unhappy with its 2534 // recycled debug info. 2535 2536 if (bit_offset > 128) 2537 { 2538 bit_size = 0; 2539 bit_offset = 0; 2540 } 2541 2542 // FIXME: Make Clang ignore Objective-C accessibility for expressions 2543 if (class_language == eLanguageTypeObjC || 2544 class_language == eLanguageTypeObjC_plus_plus) 2545 accessibility = eAccessNone; 2546 2547 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name)) 2548 { 2549 // Not all compilers will mark the vtable pointer 2550 // member as artificial (llvm-gcc). We can't have 2551 // the virtual members in our classes otherwise it 2552 // throws off all child offsets since we end up 2553 // having and extra pointer sized member in our 2554 // class layouts. 2555 is_artificial = true; 2556 } 2557 2558 // Handle static members 2559 if (is_external && member_byte_offset == UINT32_MAX) 2560 { 2561 Type *var_type = die.ResolveTypeUID(DIERef(encoding_form).GetUID()); 2562 2563 if (var_type) 2564 { 2565 if (accessibility == eAccessNone) 2566 accessibility = eAccessPublic; 2567 ClangASTContext::AddVariableToRecordType (class_clang_type, 2568 name, 2569 var_type->GetLayoutCompilerType (), 2570 accessibility); 2571 } 2572 break; 2573 } 2574 2575 if (is_artificial == false) 2576 { 2577 Type *member_type = die.ResolveTypeUID(DIERef(encoding_form).GetUID()); 2578 2579 clang::FieldDecl *field_decl = NULL; 2580 if (tag == DW_TAG_member) 2581 { 2582 if (member_type) 2583 { 2584 if (accessibility == eAccessNone) 2585 accessibility = default_accessibility; 2586 member_accessibilities.push_back(accessibility); 2587 2588 uint64_t field_bit_offset = (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8)); 2589 if (bit_size > 0) 2590 { 2591 2592 BitfieldInfo this_field_info; 2593 this_field_info.bit_offset = field_bit_offset; 2594 this_field_info.bit_size = bit_size; 2595 2596 ///////////////////////////////////////////////////////////// 2597 // How to locate a field given the DWARF debug information 2598 // 2599 // AT_byte_size indicates the size of the word in which the 2600 // bit offset must be interpreted. 2601 // 2602 // AT_data_member_location indicates the byte offset of the 2603 // word from the base address of the structure. 2604 // 2605 // AT_bit_offset indicates how many bits into the word 2606 // (according to the host endianness) the low-order bit of 2607 // the field starts. AT_bit_offset can be negative. 2608 // 2609 // AT_bit_size indicates the size of the field in bits. 2610 ///////////////////////////////////////////////////////////// 2611 2612 if (byte_size == 0) 2613 byte_size = member_type->GetByteSize(); 2614 2615 if (die.GetDWARF()->GetObjectFile()->GetByteOrder() == eByteOrderLittle) 2616 { 2617 this_field_info.bit_offset += byte_size * 8; 2618 this_field_info.bit_offset -= (bit_offset + bit_size); 2619 } 2620 else 2621 { 2622 this_field_info.bit_offset += bit_offset; 2623 } 2624 2625 // Update the field bit offset we will report for layout 2626 field_bit_offset = this_field_info.bit_offset; 2627 2628 // If the member to be emitted did not start on a character boundary and there is 2629 // empty space between the last field and this one, then we need to emit an 2630 // anonymous member filling up the space up to its start. There are three cases 2631 // here: 2632 // 2633 // 1 If the previous member ended on a character boundary, then we can emit an 2634 // anonymous member starting at the most recent character boundary. 2635 // 2636 // 2 If the previous member did not end on a character boundary and the distance 2637 // from the end of the previous member to the current member is less than a 2638 // word width, then we can emit an anonymous member starting right after the 2639 // previous member and right before this member. 2640 // 2641 // 3 If the previous member did not end on a character boundary and the distance 2642 // from the end of the previous member to the current member is greater than 2643 // or equal a word width, then we act as in Case 1. 2644 2645 const uint64_t character_width = 8; 2646 const uint64_t word_width = 32; 2647 2648 // Objective-C has invalid DW_AT_bit_offset values in older versions 2649 // of clang, so we have to be careful and only insert unnamed bitfields 2650 // if we have a new enough clang. 2651 bool detect_unnamed_bitfields = true; 2652 2653 if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus) 2654 detect_unnamed_bitfields = die.GetCU()->Supports_unnamed_objc_bitfields (); 2655 2656 if (detect_unnamed_bitfields) 2657 { 2658 BitfieldInfo anon_field_info; 2659 2660 if ((this_field_info.bit_offset % character_width) != 0) // not char aligned 2661 { 2662 uint64_t last_field_end = 0; 2663 2664 if (last_field_info.IsValid()) 2665 last_field_end = last_field_info.bit_offset + last_field_info.bit_size; 2666 2667 if (this_field_info.bit_offset != last_field_end) 2668 { 2669 if (((last_field_end % character_width) == 0) || // case 1 2670 (this_field_info.bit_offset - last_field_end >= word_width)) // case 3 2671 { 2672 anon_field_info.bit_size = this_field_info.bit_offset % character_width; 2673 anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size; 2674 } 2675 else // case 2 2676 { 2677 anon_field_info.bit_size = this_field_info.bit_offset - last_field_end; 2678 anon_field_info.bit_offset = last_field_end; 2679 } 2680 } 2681 } 2682 2683 if (anon_field_info.IsValid()) 2684 { 2685 clang::FieldDecl *unnamed_bitfield_decl = 2686 ClangASTContext::AddFieldToRecordType (class_clang_type, 2687 NULL, 2688 m_ast.GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width), 2689 accessibility, 2690 anon_field_info.bit_size); 2691 2692 layout_info.field_offsets.insert( 2693 std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset)); 2694 } 2695 } 2696 last_field_info = this_field_info; 2697 } 2698 else 2699 { 2700 last_field_info.Clear(); 2701 } 2702 2703 CompilerType member_clang_type = member_type->GetLayoutCompilerType (); 2704 if (!member_clang_type.IsCompleteType() && !member_clang_type.GetCompleteType()) 2705 incomplete_member_info_count += 1; 2706 2707 { 2708 // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>). 2709 // If the current field is at the end of the structure, then there is definitely no room for extra 2710 // elements and we override the type to array[0]. 2711 2712 CompilerType member_array_element_type; 2713 uint64_t member_array_size; 2714 bool member_array_is_incomplete; 2715 2716 if (member_clang_type.IsArrayType(&member_array_element_type, 2717 &member_array_size, 2718 &member_array_is_incomplete) && 2719 !member_array_is_incomplete) 2720 { 2721 uint64_t parent_byte_size = parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX); 2722 2723 if (member_byte_offset >= parent_byte_size) 2724 { 2725 if (member_array_size != 1) 2726 { 2727 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which extends beyond the bounds of 0x%8.8" PRIx64, 2728 die.GetID(), 2729 name, 2730 encoding_form.Reference(), 2731 parent_die.GetID()); 2732 } 2733 2734 member_clang_type = m_ast.CreateArrayType(member_array_element_type, 0, false); 2735 } 2736 } 2737 } 2738 2739 field_decl = ClangASTContext::AddFieldToRecordType (class_clang_type, 2740 name, 2741 member_clang_type, 2742 accessibility, 2743 bit_size); 2744 2745 m_ast.SetMetadataAsUserID (field_decl, die.GetID()); 2746 2747 layout_info.field_offsets.insert(std::make_pair(field_decl, field_bit_offset)); 2748 } 2749 else 2750 { 2751 if (name) 2752 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed", 2753 die.GetID(), 2754 name, 2755 encoding_form.Reference()); 2756 else 2757 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed", 2758 die.GetID(), 2759 encoding_form.Reference()); 2760 } 2761 } 2762 2763 if (prop_name != NULL && member_type) 2764 { 2765 clang::ObjCIvarDecl *ivar_decl = NULL; 2766 2767 if (field_decl) 2768 { 2769 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl); 2770 assert (ivar_decl != NULL); 2771 } 2772 2773 ClangASTMetadata metadata; 2774 metadata.SetUserID (die.GetID()); 2775 delayed_properties.push_back(DelayedAddObjCClassProperty(class_clang_type, 2776 prop_name, 2777 member_type->GetLayoutCompilerType (), 2778 ivar_decl, 2779 prop_setter_name, 2780 prop_getter_name, 2781 prop_attributes, 2782 &metadata)); 2783 2784 if (ivar_decl) 2785 m_ast.SetMetadataAsUserID (ivar_decl, die.GetID()); 2786 } 2787 } 2788 } 2789 ++member_idx; 2790 } 2791 break; 2792 2793 case DW_TAG_subprogram: 2794 // Let the type parsing code handle this one for us. 2795 member_function_dies.Append (die); 2796 break; 2797 2798 case DW_TAG_inheritance: 2799 { 2800 is_a_class = true; 2801 if (default_accessibility == eAccessNone) 2802 default_accessibility = eAccessPrivate; 2803 // TODO: implement DW_TAG_inheritance type parsing 2804 DWARFAttributes attributes; 2805 const size_t num_attributes = die.GetAttributes (attributes); 2806 if (num_attributes > 0) 2807 { 2808 Declaration decl; 2809 DWARFExpression location(die.GetCU()); 2810 DWARFFormValue encoding_form; 2811 AccessType accessibility = default_accessibility; 2812 bool is_virtual = false; 2813 bool is_base_of_class = true; 2814 off_t member_byte_offset = 0; 2815 uint32_t i; 2816 for (i=0; i<num_attributes; ++i) 2817 { 2818 const dw_attr_t attr = attributes.AttributeAtIndex(i); 2819 DWARFFormValue form_value; 2820 if (attributes.ExtractFormValueAtIndex(i, form_value)) 2821 { 2822 switch (attr) 2823 { 2824 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 2825 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 2826 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 2827 case DW_AT_type: encoding_form = form_value; break; 2828 case DW_AT_data_member_location: 2829 if (form_value.BlockData()) 2830 { 2831 Value initialValue(0); 2832 Value memberOffset(0); 2833 const DWARFDataExtractor& debug_info_data = die.GetDWARF()->get_debug_info_data(); 2834 uint32_t block_length = form_value.Unsigned(); 2835 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart(); 2836 if (DWARFExpression::Evaluate (NULL, 2837 NULL, 2838 NULL, 2839 NULL, 2840 module_sp, 2841 debug_info_data, 2842 die.GetCU(), 2843 block_offset, 2844 block_length, 2845 eRegisterKindDWARF, 2846 &initialValue, 2847 memberOffset, 2848 NULL)) 2849 { 2850 member_byte_offset = memberOffset.ResolveValue(NULL).UInt(); 2851 } 2852 } 2853 else 2854 { 2855 // With DWARF 3 and later, if the value is an integer constant, 2856 // this form value is the offset in bytes from the beginning 2857 // of the containing entity. 2858 member_byte_offset = form_value.Unsigned(); 2859 } 2860 break; 2861 2862 case DW_AT_accessibility: 2863 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); 2864 break; 2865 2866 case DW_AT_virtuality: 2867 is_virtual = form_value.Boolean(); 2868 break; 2869 2870 case DW_AT_sibling: 2871 break; 2872 2873 default: 2874 break; 2875 } 2876 } 2877 } 2878 2879 Type *base_class_type = die.ResolveTypeUID(DIERef(encoding_form).GetUID()); 2880 if (base_class_type == NULL) 2881 { 2882 module_sp->ReportError("0x%8.8x: DW_TAG_inheritance failed to resolve the base class at 0x%8.8" PRIx64 " from enclosing type 0x%8.8x. \nPlease file a bug and attach the file at the start of this error message", 2883 die.GetOffset(), 2884 encoding_form.Reference(), 2885 parent_die.GetOffset()); 2886 break; 2887 } 2888 2889 CompilerType base_class_clang_type = base_class_type->GetFullCompilerType (); 2890 assert (base_class_clang_type); 2891 if (class_language == eLanguageTypeObjC) 2892 { 2893 ast->SetObjCSuperClass(class_clang_type, base_class_clang_type); 2894 } 2895 else 2896 { 2897 base_classes.push_back (ast->CreateBaseClassSpecifier (base_class_clang_type.GetOpaqueQualType(), 2898 accessibility, 2899 is_virtual, 2900 is_base_of_class)); 2901 2902 if (is_virtual) 2903 { 2904 // Do not specify any offset for virtual inheritance. The DWARF produced by clang doesn't 2905 // give us a constant offset, but gives us a DWARF expressions that requires an actual object 2906 // in memory. the DW_AT_data_member_location for a virtual base class looks like: 2907 // DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, DW_OP_plus ) 2908 // Given this, there is really no valid response we can give to clang for virtual base 2909 // class offsets, and this should eventually be removed from LayoutRecordType() in the external 2910 // AST source in clang. 2911 } 2912 else 2913 { 2914 layout_info.base_offsets.insert( 2915 std::make_pair(ast->GetAsCXXRecordDecl(base_class_clang_type.GetOpaqueQualType()), 2916 clang::CharUnits::fromQuantity(member_byte_offset))); 2917 } 2918 } 2919 } 2920 } 2921 break; 2922 2923 default: 2924 break; 2925 } 2926 } 2927 2928 return incomplete_member_info_count == 0; 2929 } 2930 2931 2932 size_t 2933 DWARFASTParserClang::ParseChildParameters (const SymbolContext& sc, 2934 clang::DeclContext *containing_decl_ctx, 2935 const DWARFDIE &parent_die, 2936 bool skip_artificial, 2937 bool &is_static, 2938 bool &is_variadic, 2939 std::vector<CompilerType>& function_param_types, 2940 std::vector<clang::ParmVarDecl*>& function_param_decls, 2941 unsigned &type_quals) 2942 { 2943 if (!parent_die) 2944 return 0; 2945 2946 size_t arg_idx = 0; 2947 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling()) 2948 { 2949 const dw_tag_t tag = die.Tag(); 2950 switch (tag) 2951 { 2952 case DW_TAG_formal_parameter: 2953 { 2954 DWARFAttributes attributes; 2955 const size_t num_attributes = die.GetAttributes(attributes); 2956 if (num_attributes > 0) 2957 { 2958 const char *name = NULL; 2959 Declaration decl; 2960 DWARFFormValue param_type_die_form; 2961 bool is_artificial = false; 2962 // one of None, Auto, Register, Extern, Static, PrivateExtern 2963 2964 clang::StorageClass storage = clang::SC_None; 2965 uint32_t i; 2966 for (i=0; i<num_attributes; ++i) 2967 { 2968 const dw_attr_t attr = attributes.AttributeAtIndex(i); 2969 DWARFFormValue form_value; 2970 if (attributes.ExtractFormValueAtIndex(i, form_value)) 2971 { 2972 switch (attr) 2973 { 2974 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break; 2975 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break; 2976 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break; 2977 case DW_AT_name: name = form_value.AsCString(); 2978 break; 2979 case DW_AT_type: param_type_die_form = form_value; break; 2980 case DW_AT_artificial: is_artificial = form_value.Boolean(); break; 2981 case DW_AT_location: 2982 // if (form_value.BlockData()) 2983 // { 2984 // const DWARFDataExtractor& debug_info_data = debug_info(); 2985 // uint32_t block_length = form_value.Unsigned(); 2986 // DWARFDataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length); 2987 // } 2988 // else 2989 // { 2990 // } 2991 // break; 2992 case DW_AT_const_value: 2993 case DW_AT_default_value: 2994 case DW_AT_description: 2995 case DW_AT_endianity: 2996 case DW_AT_is_optional: 2997 case DW_AT_segment: 2998 case DW_AT_variable_parameter: 2999 default: 3000 case DW_AT_abstract_origin: 3001 case DW_AT_sibling: 3002 break; 3003 } 3004 } 3005 } 3006 3007 bool skip = false; 3008 if (skip_artificial) 3009 { 3010 if (is_artificial) 3011 { 3012 // In order to determine if a C++ member function is 3013 // "const" we have to look at the const-ness of "this"... 3014 // Ugly, but that 3015 if (arg_idx == 0) 3016 { 3017 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind())) 3018 { 3019 // Often times compilers omit the "this" name for the 3020 // specification DIEs, so we can't rely upon the name 3021 // being in the formal parameter DIE... 3022 if (name == NULL || ::strcmp(name, "this")==0) 3023 { 3024 Type *this_type = die.ResolveTypeUID (DIERef(param_type_die_form).GetUID()); 3025 if (this_type) 3026 { 3027 uint32_t encoding_mask = this_type->GetEncodingMask(); 3028 if (encoding_mask & Type::eEncodingIsPointerUID) 3029 { 3030 is_static = false; 3031 3032 if (encoding_mask & (1u << Type::eEncodingIsConstUID)) 3033 type_quals |= clang::Qualifiers::Const; 3034 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID)) 3035 type_quals |= clang::Qualifiers::Volatile; 3036 } 3037 } 3038 } 3039 } 3040 } 3041 skip = true; 3042 } 3043 else 3044 { 3045 3046 // HACK: Objective C formal parameters "self" and "_cmd" 3047 // are not marked as artificial in the DWARF... 3048 CompileUnit *comp_unit = die.GetLLDBCompileUnit(); 3049 if (comp_unit) 3050 { 3051 switch (comp_unit->GetLanguage()) 3052 { 3053 case eLanguageTypeObjC: 3054 case eLanguageTypeObjC_plus_plus: 3055 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0)) 3056 skip = true; 3057 break; 3058 default: 3059 break; 3060 } 3061 } 3062 } 3063 } 3064 3065 if (!skip) 3066 { 3067 Type *type = die.ResolveTypeUID(DIERef(param_type_die_form).GetUID()); 3068 if (type) 3069 { 3070 function_param_types.push_back (type->GetForwardCompilerType ()); 3071 3072 clang::ParmVarDecl *param_var_decl = m_ast.CreateParameterDeclaration (name, 3073 type->GetForwardCompilerType (), 3074 storage); 3075 assert(param_var_decl); 3076 function_param_decls.push_back(param_var_decl); 3077 3078 m_ast.SetMetadataAsUserID (param_var_decl, die.GetID()); 3079 } 3080 } 3081 } 3082 arg_idx++; 3083 } 3084 break; 3085 3086 case DW_TAG_unspecified_parameters: 3087 is_variadic = true; 3088 break; 3089 3090 case DW_TAG_template_type_parameter: 3091 case DW_TAG_template_value_parameter: 3092 // The one caller of this was never using the template_param_infos, 3093 // and the local variable was taking up a large amount of stack space 3094 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need 3095 // the template params back, we can add them back. 3096 // ParseTemplateDIE (dwarf_cu, die, template_param_infos); 3097 break; 3098 3099 default: 3100 break; 3101 } 3102 } 3103 return arg_idx; 3104 } 3105 3106 void 3107 DWARFASTParserClang::ParseChildArrayInfo (const SymbolContext& sc, 3108 const DWARFDIE &parent_die, 3109 int64_t& first_index, 3110 std::vector<uint64_t>& element_orders, 3111 uint32_t& byte_stride, 3112 uint32_t& bit_stride) 3113 { 3114 if (!parent_die) 3115 return; 3116 3117 for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling()) 3118 { 3119 const dw_tag_t tag = die.Tag(); 3120 switch (tag) 3121 { 3122 case DW_TAG_subrange_type: 3123 { 3124 DWARFAttributes attributes; 3125 const size_t num_child_attributes = die.GetAttributes(attributes); 3126 if (num_child_attributes > 0) 3127 { 3128 uint64_t num_elements = 0; 3129 uint64_t lower_bound = 0; 3130 uint64_t upper_bound = 0; 3131 bool upper_bound_valid = false; 3132 uint32_t i; 3133 for (i=0; i<num_child_attributes; ++i) 3134 { 3135 const dw_attr_t attr = attributes.AttributeAtIndex(i); 3136 DWARFFormValue form_value; 3137 if (attributes.ExtractFormValueAtIndex(i, form_value)) 3138 { 3139 switch (attr) 3140 { 3141 case DW_AT_name: 3142 break; 3143 3144 case DW_AT_count: 3145 num_elements = form_value.Unsigned(); 3146 break; 3147 3148 case DW_AT_bit_stride: 3149 bit_stride = form_value.Unsigned(); 3150 break; 3151 3152 case DW_AT_byte_stride: 3153 byte_stride = form_value.Unsigned(); 3154 break; 3155 3156 case DW_AT_lower_bound: 3157 lower_bound = form_value.Unsigned(); 3158 break; 3159 3160 case DW_AT_upper_bound: 3161 upper_bound_valid = true; 3162 upper_bound = form_value.Unsigned(); 3163 break; 3164 3165 default: 3166 case DW_AT_abstract_origin: 3167 case DW_AT_accessibility: 3168 case DW_AT_allocated: 3169 case DW_AT_associated: 3170 case DW_AT_data_location: 3171 case DW_AT_declaration: 3172 case DW_AT_description: 3173 case DW_AT_sibling: 3174 case DW_AT_threads_scaled: 3175 case DW_AT_type: 3176 case DW_AT_visibility: 3177 break; 3178 } 3179 } 3180 } 3181 3182 if (num_elements == 0) 3183 { 3184 if (upper_bound_valid && upper_bound >= lower_bound) 3185 num_elements = upper_bound - lower_bound + 1; 3186 } 3187 3188 element_orders.push_back (num_elements); 3189 } 3190 } 3191 break; 3192 } 3193 } 3194 } 3195 3196 Type * 3197 DWARFASTParserClang::GetTypeForDIE (const DWARFDIE &die) 3198 { 3199 if (die) 3200 { 3201 SymbolFileDWARF *dwarf = die.GetDWARF(); 3202 DWARFAttributes attributes; 3203 const size_t num_attributes = die.GetAttributes(attributes); 3204 if (num_attributes > 0) 3205 { 3206 DWARFFormValue type_die_form; 3207 for (size_t i = 0; i < num_attributes; ++i) 3208 { 3209 dw_attr_t attr = attributes.AttributeAtIndex(i); 3210 DWARFFormValue form_value; 3211 3212 if (attr == DW_AT_type && attributes.ExtractFormValueAtIndex(i, form_value)) 3213 return dwarf->ResolveTypeUID(DIERef(form_value).GetUID()); 3214 } 3215 } 3216 } 3217 3218 return nullptr; 3219 } 3220 3221 clang::Decl * 3222 DWARFASTParserClang::GetClangDeclForDIE (const DWARFDIE &die) 3223 { 3224 if (!die) 3225 return nullptr; 3226 3227 switch (die.Tag()) 3228 { 3229 case DW_TAG_variable: 3230 case DW_TAG_constant: 3231 case DW_TAG_formal_parameter: 3232 case DW_TAG_imported_declaration: 3233 case DW_TAG_imported_module: 3234 break; 3235 default: 3236 return nullptr; 3237 } 3238 3239 DIEToDeclMap::iterator cache_pos = m_die_to_decl.find(die.GetDIE()); 3240 if (cache_pos != m_die_to_decl.end()) 3241 return cache_pos->second; 3242 3243 if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification)) 3244 { 3245 clang::Decl *decl = GetClangDeclForDIE(spec_die); 3246 m_die_to_decl[die.GetDIE()] = decl; 3247 m_decl_to_die[decl].insert(die.GetDIE()); 3248 return decl; 3249 } 3250 3251 clang::Decl *decl = nullptr; 3252 switch (die.Tag()) 3253 { 3254 case DW_TAG_variable: 3255 case DW_TAG_constant: 3256 case DW_TAG_formal_parameter: 3257 { 3258 SymbolFileDWARF *dwarf = die.GetDWARF(); 3259 Type *type = GetTypeForDIE(die); 3260 const char *name = die.GetName(); 3261 clang::DeclContext *decl_context = ClangASTContext::DeclContextGetAsDeclContext(dwarf->GetDeclContextContainingUID(die.GetID())); 3262 decl = m_ast.CreateVariableDeclaration( 3263 decl_context, 3264 name, 3265 ClangASTContext::GetQualType(type->GetForwardCompilerType())); 3266 break; 3267 } 3268 case DW_TAG_imported_declaration: 3269 { 3270 SymbolFileDWARF *dwarf = die.GetDWARF(); 3271 lldb::user_id_t imported_uid = die.GetAttributeValueAsReference(DW_AT_import, DW_INVALID_OFFSET); 3272 3273 if (dwarf->UserIDMatches(imported_uid)) 3274 { 3275 CompilerDecl imported_decl = dwarf->GetDeclForUID(imported_uid); 3276 if (imported_decl) 3277 { 3278 clang::DeclContext *decl_context = ClangASTContext::DeclContextGetAsDeclContext(dwarf->GetDeclContextContainingUID(die.GetID())); 3279 if (clang::NamedDecl *clang_imported_decl = llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)imported_decl.GetOpaqueDecl())) 3280 decl = m_ast.CreateUsingDeclaration(decl_context, clang_imported_decl); 3281 } 3282 } 3283 break; 3284 } 3285 case DW_TAG_imported_module: 3286 { 3287 SymbolFileDWARF *dwarf = die.GetDWARF(); 3288 lldb::user_id_t imported_uid = die.GetAttributeValueAsReference(DW_AT_import, DW_INVALID_OFFSET); 3289 3290 if (dwarf->UserIDMatches(imported_uid)) 3291 { 3292 CompilerDeclContext imported_decl = dwarf->GetDeclContextForUID(imported_uid); 3293 if (imported_decl) 3294 { 3295 clang::DeclContext *decl_context = ClangASTContext::DeclContextGetAsDeclContext(dwarf->GetDeclContextContainingUID(die.GetID())); 3296 if (clang::NamespaceDecl *ns_decl = ClangASTContext::DeclContextGetAsNamespaceDecl(imported_decl)) 3297 decl = m_ast.CreateUsingDirectiveDeclaration(decl_context, ns_decl); 3298 } 3299 } 3300 break; 3301 } 3302 default: 3303 break; 3304 } 3305 3306 m_die_to_decl[die.GetDIE()] = decl; 3307 m_decl_to_die[decl].insert(die.GetDIE()); 3308 3309 return decl; 3310 } 3311 3312 clang::DeclContext * 3313 DWARFASTParserClang::GetClangDeclContextForDIE (const DWARFDIE &die) 3314 { 3315 if (die) 3316 { 3317 clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE (die); 3318 if (decl_ctx) 3319 return decl_ctx; 3320 3321 bool try_parsing_type = true; 3322 switch (die.Tag()) 3323 { 3324 case DW_TAG_compile_unit: 3325 decl_ctx = m_ast.GetTranslationUnitDecl(); 3326 try_parsing_type = false; 3327 break; 3328 3329 case DW_TAG_namespace: 3330 decl_ctx = ResolveNamespaceDIE (die); 3331 try_parsing_type = false; 3332 break; 3333 3334 case DW_TAG_lexical_block: 3335 decl_ctx = (clang::DeclContext *)ResolveBlockDIE(die); 3336 try_parsing_type = false; 3337 break; 3338 3339 default: 3340 break; 3341 } 3342 3343 if (decl_ctx == nullptr && try_parsing_type) 3344 { 3345 Type* type = die.GetDWARF()->ResolveType (die); 3346 if (type) 3347 decl_ctx = GetCachedClangDeclContextForDIE (die); 3348 } 3349 3350 if (decl_ctx) 3351 { 3352 LinkDeclContextToDIE (decl_ctx, die); 3353 return decl_ctx; 3354 } 3355 } 3356 return nullptr; 3357 } 3358 3359 clang::BlockDecl * 3360 DWARFASTParserClang::ResolveBlockDIE (const DWARFDIE &die) 3361 { 3362 if (die && die.Tag() == DW_TAG_lexical_block) 3363 { 3364 clang::BlockDecl *decl = llvm::cast_or_null<clang::BlockDecl>(m_die_to_decl_ctx[die.GetDIE()]); 3365 3366 if (!decl) 3367 { 3368 DWARFDIE decl_context_die; 3369 clang::DeclContext *decl_context = GetClangDeclContextContainingDIE(die, &decl_context_die); 3370 decl = m_ast.CreateBlockDeclaration(decl_context); 3371 3372 if (decl) 3373 LinkDeclContextToDIE((clang::DeclContext *)decl, die); 3374 } 3375 3376 return decl; 3377 } 3378 return nullptr; 3379 } 3380 3381 clang::NamespaceDecl * 3382 DWARFASTParserClang::ResolveNamespaceDIE (const DWARFDIE &die) 3383 { 3384 if (die && die.Tag() == DW_TAG_namespace) 3385 { 3386 // See if we already parsed this namespace DIE and associated it with a 3387 // uniqued namespace declaration 3388 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die.GetDIE()]); 3389 if (namespace_decl) 3390 return namespace_decl; 3391 else 3392 { 3393 const char *namespace_name = die.GetName(); 3394 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (die, nullptr); 3395 namespace_decl = m_ast.GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx); 3396 Log *log = nullptr;// (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO)); 3397 if (log) 3398 { 3399 SymbolFileDWARF *dwarf = die.GetDWARF(); 3400 if (namespace_name) 3401 { 3402 dwarf->GetObjectFile()->GetModule()->LogMessage (log, 3403 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)", 3404 static_cast<void*>(m_ast.getASTContext()), 3405 die.GetID(), 3406 namespace_name, 3407 static_cast<void*>(namespace_decl), 3408 static_cast<void*>(namespace_decl->getOriginalNamespace())); 3409 } 3410 else 3411 { 3412 dwarf->GetObjectFile()->GetModule()->LogMessage (log, 3413 "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)", 3414 static_cast<void*>(m_ast.getASTContext()), 3415 die.GetID(), 3416 static_cast<void*>(namespace_decl), 3417 static_cast<void*>(namespace_decl->getOriginalNamespace())); 3418 } 3419 } 3420 3421 if (namespace_decl) 3422 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die); 3423 return namespace_decl; 3424 } 3425 } 3426 return nullptr; 3427 } 3428 3429 clang::DeclContext * 3430 DWARFASTParserClang::GetClangDeclContextContainingDIE (const DWARFDIE &die, 3431 DWARFDIE *decl_ctx_die_copy) 3432 { 3433 SymbolFileDWARF *dwarf = die.GetDWARF(); 3434 3435 DWARFDIE decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE (die); 3436 3437 if (decl_ctx_die_copy) 3438 *decl_ctx_die_copy = decl_ctx_die; 3439 3440 if (decl_ctx_die) 3441 { 3442 clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE (decl_ctx_die); 3443 if (clang_decl_ctx) 3444 return clang_decl_ctx; 3445 } 3446 return m_ast.GetTranslationUnitDecl(); 3447 } 3448 3449 clang::DeclContext * 3450 DWARFASTParserClang::GetCachedClangDeclContextForDIE (const DWARFDIE &die) 3451 { 3452 if (die) 3453 { 3454 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die.GetDIE()); 3455 if (pos != m_die_to_decl_ctx.end()) 3456 return pos->second; 3457 } 3458 return nullptr; 3459 } 3460 3461 void 3462 DWARFASTParserClang::LinkDeclContextToDIE (clang::DeclContext *decl_ctx, const DWARFDIE &die) 3463 { 3464 m_die_to_decl_ctx[die.GetDIE()] = decl_ctx; 3465 // There can be many DIEs for a single decl context 3466 //m_decl_ctx_to_die[decl_ctx].insert(die.GetDIE()); 3467 m_decl_ctx_to_die.insert(std::make_pair(decl_ctx, die)); 3468 } 3469 3470 bool 3471 DWARFASTParserClang::CopyUniqueClassMethodTypes (const DWARFDIE &src_class_die, 3472 const DWARFDIE &dst_class_die, 3473 lldb_private::Type *class_type, 3474 DWARFDIECollection &failures) 3475 { 3476 if (!class_type || !src_class_die || !dst_class_die) 3477 return false; 3478 if (src_class_die.Tag() != dst_class_die.Tag()) 3479 return false; 3480 3481 // We need to complete the class type so we can get all of the method types 3482 // parsed so we can then unique those types to their equivalent counterparts 3483 // in "dst_cu" and "dst_class_die" 3484 class_type->GetFullCompilerType (); 3485 3486 DWARFDIE src_die; 3487 DWARFDIE dst_die; 3488 UniqueCStringMap<DWARFDIE> src_name_to_die; 3489 UniqueCStringMap<DWARFDIE> dst_name_to_die; 3490 UniqueCStringMap<DWARFDIE> src_name_to_die_artificial; 3491 UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial; 3492 for (src_die = src_class_die.GetFirstChild(); src_die.IsValid(); src_die = src_die.GetSibling()) 3493 { 3494 if (src_die.Tag() == DW_TAG_subprogram) 3495 { 3496 // Make sure this is a declaration and not a concrete instance by looking 3497 // for DW_AT_declaration set to 1. Sometimes concrete function instances 3498 // are placed inside the class definitions and shouldn't be included in 3499 // the list of things are are tracking here. 3500 if (src_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) 3501 { 3502 const char *src_name = src_die.GetMangledName (); 3503 if (src_name) 3504 { 3505 ConstString src_const_name(src_name); 3506 if (src_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0)) 3507 src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die); 3508 else 3509 src_name_to_die.Append(src_const_name.GetCString(), src_die); 3510 } 3511 } 3512 } 3513 } 3514 for (dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid(); dst_die = dst_die.GetSibling()) 3515 { 3516 if (dst_die.Tag() == DW_TAG_subprogram) 3517 { 3518 // Make sure this is a declaration and not a concrete instance by looking 3519 // for DW_AT_declaration set to 1. Sometimes concrete function instances 3520 // are placed inside the class definitions and shouldn't be included in 3521 // the list of things are are tracking here. 3522 if (dst_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) 3523 { 3524 const char *dst_name = dst_die.GetMangledName (); 3525 if (dst_name) 3526 { 3527 ConstString dst_const_name(dst_name); 3528 if ( dst_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0)) 3529 dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die); 3530 else 3531 dst_name_to_die.Append(dst_const_name.GetCString(), dst_die); 3532 } 3533 } 3534 } 3535 } 3536 const uint32_t src_size = src_name_to_die.GetSize (); 3537 const uint32_t dst_size = dst_name_to_die.GetSize (); 3538 Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION)); 3539 3540 // Is everything kosher so we can go through the members at top speed? 3541 bool fast_path = true; 3542 3543 if (src_size != dst_size) 3544 { 3545 if (src_size != 0 && dst_size != 0) 3546 { 3547 if (log) 3548 log->Printf("warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)", 3549 src_class_die.GetOffset(), 3550 dst_class_die.GetOffset(), 3551 src_size, 3552 dst_size); 3553 } 3554 3555 fast_path = false; 3556 } 3557 3558 uint32_t idx; 3559 3560 if (fast_path) 3561 { 3562 for (idx = 0; idx < src_size; ++idx) 3563 { 3564 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx); 3565 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx); 3566 3567 if (src_die.Tag() != dst_die.Tag()) 3568 { 3569 if (log) 3570 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)", 3571 src_class_die.GetOffset(), 3572 dst_class_die.GetOffset(), 3573 src_die.GetOffset(), 3574 src_die.GetTagAsCString(), 3575 dst_die.GetOffset(), 3576 dst_die.GetTagAsCString()); 3577 fast_path = false; 3578 } 3579 3580 const char *src_name = src_die.GetMangledName (); 3581 const char *dst_name = dst_die.GetMangledName (); 3582 3583 // Make sure the names match 3584 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0)) 3585 continue; 3586 3587 if (log) 3588 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)", 3589 src_class_die.GetOffset(), 3590 dst_class_die.GetOffset(), 3591 src_die.GetOffset(), 3592 src_name, 3593 dst_die.GetOffset(), 3594 dst_name); 3595 3596 fast_path = false; 3597 } 3598 } 3599 3600 DWARFASTParserClang *src_dwarf_ast_parser = (DWARFASTParserClang *)src_die.GetDWARFParser(); 3601 DWARFASTParserClang *dst_dwarf_ast_parser = (DWARFASTParserClang *)dst_die.GetDWARFParser(); 3602 3603 // Now do the work of linking the DeclContexts and Types. 3604 if (fast_path) 3605 { 3606 // We can do this quickly. Just run across the tables index-for-index since 3607 // we know each node has matching names and tags. 3608 for (idx = 0; idx < src_size; ++idx) 3609 { 3610 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx); 3611 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx); 3612 3613 clang::DeclContext *src_decl_ctx = src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()]; 3614 if (src_decl_ctx) 3615 { 3616 if (log) 3617 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", 3618 static_cast<void*>(src_decl_ctx), 3619 src_die.GetOffset(), dst_die.GetOffset()); 3620 dst_dwarf_ast_parser->LinkDeclContextToDIE (src_decl_ctx, dst_die); 3621 } 3622 else 3623 { 3624 if (log) 3625 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", 3626 src_die.GetOffset(), dst_die.GetOffset()); 3627 } 3628 3629 Type *src_child_type = dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()]; 3630 if (src_child_type) 3631 { 3632 if (log) 3633 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", 3634 static_cast<void*>(src_child_type), 3635 src_child_type->GetID(), 3636 src_die.GetOffset(), dst_die.GetOffset()); 3637 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type; 3638 } 3639 else 3640 { 3641 if (log) 3642 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset()); 3643 } 3644 } 3645 } 3646 else 3647 { 3648 // We must do this slowly. For each member of the destination, look 3649 // up a member in the source with the same name, check its tag, and 3650 // unique them if everything matches up. Report failures. 3651 3652 if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty()) 3653 { 3654 src_name_to_die.Sort(); 3655 3656 for (idx = 0; idx < dst_size; ++idx) 3657 { 3658 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx); 3659 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx); 3660 src_die = src_name_to_die.Find(dst_name, DWARFDIE()); 3661 3662 if (src_die && (src_die.Tag() == dst_die.Tag())) 3663 { 3664 clang::DeclContext *src_decl_ctx = src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()]; 3665 if (src_decl_ctx) 3666 { 3667 if (log) 3668 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", 3669 static_cast<void*>(src_decl_ctx), 3670 src_die.GetOffset(), 3671 dst_die.GetOffset()); 3672 dst_dwarf_ast_parser->LinkDeclContextToDIE (src_decl_ctx, dst_die); 3673 } 3674 else 3675 { 3676 if (log) 3677 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset()); 3678 } 3679 3680 Type *src_child_type = dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()]; 3681 if (src_child_type) 3682 { 3683 if (log) 3684 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", 3685 static_cast<void*>(src_child_type), 3686 src_child_type->GetID(), 3687 src_die.GetOffset(), 3688 dst_die.GetOffset()); 3689 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type; 3690 } 3691 else 3692 { 3693 if (log) 3694 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset()); 3695 } 3696 } 3697 else 3698 { 3699 if (log) 3700 log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die.GetOffset()); 3701 3702 failures.Append(dst_die); 3703 } 3704 } 3705 } 3706 } 3707 3708 const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize (); 3709 const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize (); 3710 3711 if (src_size_artificial && dst_size_artificial) 3712 { 3713 dst_name_to_die_artificial.Sort(); 3714 3715 for (idx = 0; idx < src_size_artificial; ++idx) 3716 { 3717 const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx); 3718 src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx); 3719 dst_die = dst_name_to_die_artificial.Find(src_name_artificial, DWARFDIE()); 3720 3721 if (dst_die) 3722 { 3723 // Both classes have the artificial types, link them 3724 clang::DeclContext *src_decl_ctx = src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()]; 3725 if (src_decl_ctx) 3726 { 3727 if (log) 3728 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x", 3729 static_cast<void*>(src_decl_ctx), 3730 src_die.GetOffset(), dst_die.GetOffset()); 3731 dst_dwarf_ast_parser->LinkDeclContextToDIE (src_decl_ctx, dst_die); 3732 } 3733 else 3734 { 3735 if (log) 3736 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset()); 3737 } 3738 3739 Type *src_child_type = dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()]; 3740 if (src_child_type) 3741 { 3742 if (log) 3743 log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x", 3744 static_cast<void*>(src_child_type), 3745 src_child_type->GetID(), 3746 src_die.GetOffset(), dst_die.GetOffset()); 3747 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type; 3748 } 3749 else 3750 { 3751 if (log) 3752 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset()); 3753 } 3754 } 3755 } 3756 } 3757 3758 if (dst_size_artificial) 3759 { 3760 for (idx = 0; idx < dst_size_artificial; ++idx) 3761 { 3762 const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx); 3763 dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx); 3764 if (log) 3765 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die.GetOffset(), dst_name_artificial); 3766 3767 failures.Append(dst_die); 3768 } 3769 } 3770 3771 return (failures.Size() != 0); 3772 } 3773 3774 3775 bool 3776 DWARFASTParserClang::LayoutRecordType(const clang::RecordDecl *record_decl, 3777 uint64_t &bit_size, 3778 uint64_t &alignment, 3779 llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, 3780 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets, 3781 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets) 3782 { 3783 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl); 3784 bool success = false; 3785 base_offsets.clear(); 3786 vbase_offsets.clear(); 3787 if (pos != m_record_decl_to_layout_map.end()) 3788 { 3789 bit_size = pos->second.bit_size; 3790 alignment = pos->second.alignment; 3791 field_offsets.swap(pos->second.field_offsets); 3792 base_offsets.swap (pos->second.base_offsets); 3793 vbase_offsets.swap (pos->second.vbase_offsets); 3794 m_record_decl_to_layout_map.erase(pos); 3795 success = true; 3796 } 3797 else 3798 { 3799 bit_size = 0; 3800 alignment = 0; 3801 field_offsets.clear(); 3802 } 3803 return success; 3804 } 3805