1254721Semaste//===-- ClangASTImporter.cpp ------------------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste#include "clang/AST/Decl.h" 11254721Semaste#include "clang/AST/DeclCXX.h" 12254721Semaste#include "clang/AST/DeclObjC.h" 13254721Semaste#include "llvm/Support/raw_ostream.h" 14254721Semaste#include "lldb/Core/Log.h" 15254721Semaste#include "lldb/Core/Module.h" 16254721Semaste#include "lldb/Symbol/ClangASTContext.h" 17254721Semaste#include "lldb/Symbol/ClangASTImporter.h" 18254721Semaste#include "lldb/Symbol/ClangExternalASTSourceCommon.h" 19254721Semaste#include "lldb/Symbol/ClangNamespaceDecl.h" 20254721Semaste 21254721Semasteusing namespace lldb_private; 22254721Semasteusing namespace clang; 23254721Semaste 24254721SemasteClangASTMetrics::Counters ClangASTMetrics::global_counters = { 0, 0, 0, 0, 0, 0 }; 25254721SemasteClangASTMetrics::Counters ClangASTMetrics::local_counters = { 0, 0, 0, 0, 0, 0 }; 26254721Semaste 27254721Semastevoid ClangASTMetrics::DumpCounters (Log *log, ClangASTMetrics::Counters &counters) 28254721Semaste{ 29254721Semaste log->Printf(" Number of visible Decl queries by name : %" PRIu64, counters.m_visible_query_count); 30254721Semaste log->Printf(" Number of lexical Decl queries : %" PRIu64, counters.m_lexical_query_count); 31254721Semaste log->Printf(" Number of imports initiated by LLDB : %" PRIu64, counters.m_lldb_import_count); 32254721Semaste log->Printf(" Number of imports conducted by Clang : %" PRIu64, counters.m_clang_import_count); 33254721Semaste log->Printf(" Number of Decls completed : %" PRIu64, counters.m_decls_completed_count); 34254721Semaste log->Printf(" Number of records laid out : %" PRIu64, counters.m_record_layout_count); 35254721Semaste} 36254721Semaste 37254721Semastevoid ClangASTMetrics::DumpCounters (Log *log) 38254721Semaste{ 39254721Semaste if (!log) 40254721Semaste return; 41254721Semaste 42254721Semaste log->Printf("== ClangASTMetrics output =="); 43254721Semaste log->Printf("-- Global metrics --"); 44254721Semaste DumpCounters (log, global_counters); 45254721Semaste log->Printf("-- Local metrics --"); 46254721Semaste DumpCounters (log, local_counters); 47254721Semaste} 48254721Semaste 49254721Semasteclang::QualType 50254721SemasteClangASTImporter::CopyType (clang::ASTContext *dst_ast, 51254721Semaste clang::ASTContext *src_ast, 52254721Semaste clang::QualType type) 53254721Semaste{ 54254721Semaste MinionSP minion_sp (GetMinion(dst_ast, src_ast)); 55254721Semaste 56254721Semaste if (minion_sp) 57254721Semaste return minion_sp->Import(type); 58254721Semaste 59254721Semaste return QualType(); 60254721Semaste} 61254721Semaste 62254721Semastelldb::clang_type_t 63254721SemasteClangASTImporter::CopyType (clang::ASTContext *dst_ast, 64254721Semaste clang::ASTContext *src_ast, 65254721Semaste lldb::clang_type_t type) 66254721Semaste{ 67254721Semaste return CopyType (dst_ast, src_ast, QualType::getFromOpaquePtr(type)).getAsOpaquePtr(); 68254721Semaste} 69254721Semaste 70254721Semasteclang::Decl * 71254721SemasteClangASTImporter::CopyDecl (clang::ASTContext *dst_ast, 72254721Semaste clang::ASTContext *src_ast, 73254721Semaste clang::Decl *decl) 74254721Semaste{ 75254721Semaste MinionSP minion_sp; 76254721Semaste 77254721Semaste minion_sp = GetMinion(dst_ast, src_ast); 78254721Semaste 79254721Semaste if (minion_sp) 80254721Semaste { 81254721Semaste clang::Decl *result = minion_sp->Import(decl); 82254721Semaste 83254721Semaste if (!result) 84254721Semaste { 85254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 86254721Semaste 87254721Semaste if (log) 88254721Semaste { 89254721Semaste lldb::user_id_t user_id; 90254721Semaste ClangASTMetadata *metadata = GetDeclMetadata(decl); 91254721Semaste if (metadata) 92254721Semaste user_id = metadata->GetUserID(); 93254721Semaste 94254721Semaste if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl)) 95254721Semaste log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s '%s', metadata 0x%" PRIx64, 96254721Semaste decl->getDeclKindName(), 97254721Semaste named_decl->getNameAsString().c_str(), 98254721Semaste user_id); 99254721Semaste else 100254721Semaste log->Printf(" [ClangASTImporter] WARNING: Failed to import a %s, metadata 0x%" PRIx64, 101254721Semaste decl->getDeclKindName(), 102254721Semaste user_id); 103254721Semaste } 104254721Semaste } 105254721Semaste 106254721Semaste return result; 107254721Semaste } 108254721Semaste 109254721Semaste return NULL; 110254721Semaste} 111254721Semaste 112254721Semastelldb::clang_type_t 113254721SemasteClangASTImporter::DeportType (clang::ASTContext *dst_ctx, 114254721Semaste clang::ASTContext *src_ctx, 115254721Semaste lldb::clang_type_t type) 116254721Semaste{ 117254721Semaste MinionSP minion_sp (GetMinion (dst_ctx, src_ctx)); 118254721Semaste 119254721Semaste if (!minion_sp) 120254721Semaste return NULL; 121254721Semaste 122254721Semaste std::set<NamedDecl *> decls_to_deport; 123254721Semaste std::set<NamedDecl *> decls_already_deported; 124254721Semaste 125254721Semaste minion_sp->InitDeportWorkQueues(&decls_to_deport, 126254721Semaste &decls_already_deported); 127254721Semaste 128254721Semaste lldb::clang_type_t result = CopyType(dst_ctx, src_ctx, type); 129254721Semaste 130254721Semaste minion_sp->ExecuteDeportWorkQueues(); 131254721Semaste 132254721Semaste if (!result) 133254721Semaste return NULL; 134254721Semaste 135254721Semaste return result; 136254721Semaste 137254721Semaste} 138254721Semaste 139254721Semasteclang::Decl * 140254721SemasteClangASTImporter::DeportDecl (clang::ASTContext *dst_ctx, 141254721Semaste clang::ASTContext *src_ctx, 142254721Semaste clang::Decl *decl) 143254721Semaste{ 144254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 145254721Semaste 146254721Semaste if (log) 147254721Semaste log->Printf(" [ClangASTImporter] DeportDecl called on (%sDecl*)%p from (ASTContext*)%p to (ASTContex*)%p", 148254721Semaste decl->getDeclKindName(), 149254721Semaste decl, 150254721Semaste src_ctx, 151254721Semaste dst_ctx); 152254721Semaste 153254721Semaste MinionSP minion_sp (GetMinion (dst_ctx, src_ctx)); 154254721Semaste 155254721Semaste if (!minion_sp) 156254721Semaste return NULL; 157254721Semaste 158254721Semaste std::set<NamedDecl *> decls_to_deport; 159254721Semaste std::set<NamedDecl *> decls_already_deported; 160254721Semaste 161254721Semaste minion_sp->InitDeportWorkQueues(&decls_to_deport, 162254721Semaste &decls_already_deported); 163254721Semaste 164254721Semaste clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl); 165254721Semaste 166254721Semaste minion_sp->ExecuteDeportWorkQueues(); 167254721Semaste 168254721Semaste if (!result) 169254721Semaste return NULL; 170254721Semaste 171254721Semaste if (log) 172254721Semaste log->Printf(" [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p", 173254721Semaste decl->getDeclKindName(), 174254721Semaste decl, 175254721Semaste result->getDeclKindName(), 176254721Semaste result); 177254721Semaste 178254721Semaste return result; 179254721Semaste} 180254721Semaste 181254721Semastevoid 182254721SemasteClangASTImporter::CompleteDecl (clang::Decl *decl) 183254721Semaste{ 184254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 185254721Semaste 186254721Semaste if (log) 187254721Semaste log->Printf(" [ClangASTImporter] CompleteDecl called on (%sDecl*)%p", 188254721Semaste decl->getDeclKindName(), 189254721Semaste decl); 190254721Semaste 191254721Semaste if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl)) 192254721Semaste { 193254721Semaste if (!interface_decl->getDefinition()) 194254721Semaste { 195254721Semaste interface_decl->startDefinition(); 196254721Semaste CompleteObjCInterfaceDecl(interface_decl); 197254721Semaste } 198254721Semaste } 199254721Semaste else if (ObjCProtocolDecl *protocol_decl = dyn_cast<ObjCProtocolDecl>(decl)) 200254721Semaste { 201254721Semaste if (!protocol_decl->getDefinition()) 202254721Semaste protocol_decl->startDefinition(); 203254721Semaste } 204254721Semaste else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) 205254721Semaste { 206254721Semaste if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined()) 207254721Semaste { 208254721Semaste tag_decl->startDefinition(); 209254721Semaste CompleteTagDecl(tag_decl); 210254721Semaste tag_decl->setCompleteDefinition(true); 211254721Semaste } 212254721Semaste } 213254721Semaste else 214254721Semaste { 215254721Semaste assert (0 && "CompleteDecl called on a Decl that can't be completed"); 216254721Semaste } 217254721Semaste} 218254721Semaste 219254721Semastebool 220254721SemasteClangASTImporter::CompleteTagDecl (clang::TagDecl *decl) 221254721Semaste{ 222254721Semaste ClangASTMetrics::RegisterDeclCompletion(); 223254721Semaste 224254721Semaste DeclOrigin decl_origin = GetDeclOrigin(decl); 225254721Semaste 226254721Semaste if (!decl_origin.Valid()) 227254721Semaste return false; 228254721Semaste 229254721Semaste if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl)) 230254721Semaste return false; 231254721Semaste 232254721Semaste MinionSP minion_sp (GetMinion(&decl->getASTContext(), decl_origin.ctx)); 233254721Semaste 234254721Semaste if (minion_sp) 235254721Semaste minion_sp->ImportDefinitionTo(decl, decl_origin.decl); 236254721Semaste 237254721Semaste return true; 238254721Semaste} 239254721Semaste 240254721Semastebool 241254721SemasteClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl, clang::TagDecl *origin_decl) 242254721Semaste{ 243254721Semaste ClangASTMetrics::RegisterDeclCompletion(); 244254721Semaste 245254721Semaste clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext(); 246254721Semaste 247254721Semaste if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl)) 248254721Semaste return false; 249254721Semaste 250254721Semaste MinionSP minion_sp (GetMinion(&decl->getASTContext(), origin_ast_ctx)); 251254721Semaste 252254721Semaste if (minion_sp) 253254721Semaste minion_sp->ImportDefinitionTo(decl, origin_decl); 254254721Semaste 255254721Semaste ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); 256254721Semaste 257254721Semaste OriginMap &origins = context_md->m_origins; 258254721Semaste 259254721Semaste origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl); 260254721Semaste 261254721Semaste return true; 262254721Semaste} 263254721Semaste 264254721Semastebool 265254721SemasteClangASTImporter::CompleteObjCInterfaceDecl (clang::ObjCInterfaceDecl *interface_decl) 266254721Semaste{ 267254721Semaste ClangASTMetrics::RegisterDeclCompletion(); 268254721Semaste 269254721Semaste DeclOrigin decl_origin = GetDeclOrigin(interface_decl); 270254721Semaste 271254721Semaste if (!decl_origin.Valid()) 272254721Semaste return false; 273254721Semaste 274254721Semaste if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl)) 275254721Semaste return false; 276254721Semaste 277254721Semaste MinionSP minion_sp (GetMinion(&interface_decl->getASTContext(), decl_origin.ctx)); 278254721Semaste 279254721Semaste if (minion_sp) 280254721Semaste minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl); 281254721Semaste 282254721Semaste return true; 283254721Semaste} 284254721Semaste 285254721Semastebool 286254721SemasteClangASTImporter::RequireCompleteType (clang::QualType type) 287254721Semaste{ 288254721Semaste if (type.isNull()) 289254721Semaste return false; 290254721Semaste 291254721Semaste if (const TagType *tag_type = type->getAs<TagType>()) 292254721Semaste { 293254721Semaste return CompleteTagDecl(tag_type->getDecl()); 294254721Semaste } 295254721Semaste if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) 296254721Semaste { 297254721Semaste if (ObjCInterfaceDecl *objc_interface_decl = objc_object_type->getInterface()) 298254721Semaste return CompleteObjCInterfaceDecl(objc_interface_decl); 299254721Semaste else 300254721Semaste return false; 301254721Semaste } 302254721Semaste if (const ArrayType *array_type = type->getAsArrayTypeUnsafe()) 303254721Semaste { 304254721Semaste return RequireCompleteType(array_type->getElementType()); 305254721Semaste } 306254721Semaste if (const AtomicType *atomic_type = type->getAs<AtomicType>()) 307254721Semaste { 308254721Semaste return RequireCompleteType(atomic_type->getPointeeType()); 309254721Semaste } 310254721Semaste 311254721Semaste return true; 312254721Semaste} 313254721Semaste 314254721SemasteClangASTMetadata * 315254721SemasteClangASTImporter::GetDeclMetadata (const clang::Decl *decl) 316254721Semaste{ 317254721Semaste DeclOrigin decl_origin = GetDeclOrigin(decl); 318254721Semaste 319254721Semaste if (decl_origin.Valid()) 320254721Semaste return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl); 321254721Semaste else 322254721Semaste return ClangASTContext::GetMetadata(&decl->getASTContext(), decl); 323254721Semaste} 324254721Semaste 325254721SemasteClangASTImporter::DeclOrigin 326254721SemasteClangASTImporter::GetDeclOrigin(const clang::Decl *decl) 327254721Semaste{ 328254721Semaste ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); 329254721Semaste 330254721Semaste OriginMap &origins = context_md->m_origins; 331254721Semaste 332254721Semaste OriginMap::iterator iter = origins.find(decl); 333254721Semaste 334254721Semaste if (iter != origins.end()) 335254721Semaste return iter->second; 336254721Semaste else 337254721Semaste return DeclOrigin(); 338254721Semaste} 339254721Semaste 340254721Semastevoid 341254721SemasteClangASTImporter::SetDeclOrigin (const clang::Decl *decl, clang::Decl *original_decl) 342254721Semaste{ 343254721Semaste ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); 344254721Semaste 345254721Semaste OriginMap &origins = context_md->m_origins; 346254721Semaste 347254721Semaste OriginMap::iterator iter = origins.find(decl); 348254721Semaste 349254721Semaste if (iter != origins.end()) 350254721Semaste { 351254721Semaste iter->second.decl = original_decl; 352254721Semaste iter->second.ctx = &original_decl->getASTContext(); 353254721Semaste } 354254721Semaste else 355254721Semaste { 356254721Semaste origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl); 357254721Semaste } 358254721Semaste} 359254721Semaste 360254721Semastevoid 361254721SemasteClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl, 362254721Semaste NamespaceMapSP &namespace_map) 363254721Semaste{ 364254721Semaste ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); 365254721Semaste 366254721Semaste context_md->m_namespace_maps[decl] = namespace_map; 367254721Semaste} 368254721Semaste 369254721SemasteClangASTImporter::NamespaceMapSP 370254721SemasteClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl) 371254721Semaste{ 372254721Semaste ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); 373254721Semaste 374254721Semaste NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps; 375254721Semaste 376254721Semaste NamespaceMetaMap::iterator iter = namespace_maps.find(decl); 377254721Semaste 378254721Semaste if (iter != namespace_maps.end()) 379254721Semaste return iter->second; 380254721Semaste else 381254721Semaste return NamespaceMapSP(); 382254721Semaste} 383254721Semaste 384254721Semastevoid 385254721SemasteClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) 386254721Semaste{ 387254721Semaste assert (decl); 388254721Semaste ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext()); 389254721Semaste 390254721Semaste const DeclContext *parent_context = decl->getDeclContext(); 391254721Semaste const NamespaceDecl *parent_namespace = dyn_cast<NamespaceDecl>(parent_context); 392254721Semaste NamespaceMapSP parent_map; 393254721Semaste 394254721Semaste if (parent_namespace) 395254721Semaste parent_map = GetNamespaceMap(parent_namespace); 396254721Semaste 397254721Semaste NamespaceMapSP new_map; 398254721Semaste 399254721Semaste new_map.reset(new NamespaceMap); 400254721Semaste 401254721Semaste if (context_md->m_map_completer) 402254721Semaste { 403254721Semaste std::string namespace_string = decl->getDeclName().getAsString(); 404254721Semaste 405254721Semaste context_md->m_map_completer->CompleteNamespaceMap (new_map, ConstString(namespace_string.c_str()), parent_map); 406254721Semaste } 407254721Semaste 408254721Semaste context_md->m_namespace_maps[decl] = new_map; 409254721Semaste} 410254721Semaste 411254721Semastevoid 412254721SemasteClangASTImporter::ForgetDestination (clang::ASTContext *dst_ast) 413254721Semaste{ 414254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 415254721Semaste 416254721Semaste if (log) 417254721Semaste log->Printf(" [ClangASTImporter] Forgetting destination (ASTContext*)%p", dst_ast); 418254721Semaste 419254721Semaste m_metadata_map.erase(dst_ast); 420254721Semaste} 421254721Semaste 422254721Semastevoid 423254721SemasteClangASTImporter::ForgetSource (clang::ASTContext *dst_ast, clang::ASTContext *src_ast) 424254721Semaste{ 425254721Semaste ASTContextMetadataSP md = MaybeGetContextMetadata (dst_ast); 426254721Semaste 427254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 428254721Semaste 429254721Semaste if (log) 430254721Semaste log->Printf(" [ClangASTImporter] Forgetting source->dest (ASTContext*)%p->(ASTContext*)%p", src_ast, dst_ast); 431254721Semaste 432254721Semaste if (!md) 433254721Semaste return; 434254721Semaste 435254721Semaste md->m_minions.erase(src_ast); 436254721Semaste 437254721Semaste for (OriginMap::iterator iter = md->m_origins.begin(); 438254721Semaste iter != md->m_origins.end(); 439254721Semaste ) 440254721Semaste { 441254721Semaste if (iter->second.ctx == src_ast) 442254721Semaste md->m_origins.erase(iter++); 443254721Semaste else 444254721Semaste ++iter; 445254721Semaste } 446254721Semaste} 447254721Semaste 448254721SemasteClangASTImporter::MapCompleter::~MapCompleter () 449254721Semaste{ 450254721Semaste return; 451254721Semaste} 452254721Semaste 453254721Semastevoid 454254721SemasteClangASTImporter::Minion::InitDeportWorkQueues (std::set<clang::NamedDecl *> *decls_to_deport, 455254721Semaste std::set<clang::NamedDecl *> *decls_already_deported) 456254721Semaste{ 457254721Semaste assert(!m_decls_to_deport); // TODO make debug only 458254721Semaste assert(!m_decls_already_deported); 459254721Semaste 460254721Semaste m_decls_to_deport = decls_to_deport; 461254721Semaste m_decls_already_deported = decls_already_deported; 462254721Semaste} 463254721Semaste 464254721Semastevoid 465254721SemasteClangASTImporter::Minion::ExecuteDeportWorkQueues () 466254721Semaste{ 467254721Semaste assert(m_decls_to_deport); // TODO make debug only 468254721Semaste assert(m_decls_already_deported); 469254721Semaste 470254721Semaste ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&getToContext()); 471254721Semaste 472254721Semaste while (!m_decls_to_deport->empty()) 473254721Semaste { 474254721Semaste NamedDecl *decl = *m_decls_to_deport->begin(); 475254721Semaste 476254721Semaste m_decls_already_deported->insert(decl); 477254721Semaste m_decls_to_deport->erase(decl); 478254721Semaste 479254721Semaste DeclOrigin &origin = to_context_md->m_origins[decl]; 480254721Semaste 481254721Semaste assert (origin.ctx == m_source_ctx); // otherwise we should never have added this 482254721Semaste // because it doesn't need to be deported 483254721Semaste 484254721Semaste Decl *original_decl = to_context_md->m_origins[decl].decl; 485254721Semaste 486254721Semaste ClangASTContext::GetCompleteDecl (m_source_ctx, original_decl); 487254721Semaste 488254721Semaste if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) 489254721Semaste { 490254721Semaste if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) 491254721Semaste if (original_tag_decl->isCompleteDefinition()) 492254721Semaste ImportDefinitionTo(tag_decl, original_tag_decl); 493254721Semaste 494254721Semaste tag_decl->setHasExternalLexicalStorage(false); 495254721Semaste tag_decl->setHasExternalVisibleStorage(false); 496254721Semaste } 497254721Semaste else if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl)) 498254721Semaste { 499254721Semaste interface_decl->setHasExternalLexicalStorage(false); 500254721Semaste interface_decl->setHasExternalVisibleStorage(false); 501254721Semaste } 502254721Semaste 503254721Semaste to_context_md->m_origins.erase(decl); 504254721Semaste } 505254721Semaste 506254721Semaste m_decls_to_deport = NULL; 507254721Semaste m_decls_already_deported = NULL; 508254721Semaste} 509254721Semaste 510254721Semastevoid 511254721SemasteClangASTImporter::Minion::ImportDefinitionTo (clang::Decl *to, clang::Decl *from) 512254721Semaste{ 513254721Semaste ASTImporter::Imported(from, to); 514254721Semaste 515254721Semaste ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to); 516254721Semaste 517254721Semaste /* 518254721Semaste if (to_objc_interface) 519254721Semaste to_objc_interface->startDefinition(); 520254721Semaste 521254721Semaste CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to); 522254721Semaste 523254721Semaste if (to_cxx_record) 524254721Semaste to_cxx_record->startDefinition(); 525254721Semaste */ 526254721Semaste 527254721Semaste ImportDefinition(from); 528254721Semaste 529254721Semaste // If we're dealing with an Objective-C class, ensure that the inheritance has 530254721Semaste // been set up correctly. The ASTImporter may not do this correctly if the 531254721Semaste // class was originally sourced from symbols. 532254721Semaste 533254721Semaste if (to_objc_interface) 534254721Semaste { 535254721Semaste do 536254721Semaste { 537254721Semaste ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass(); 538254721Semaste 539254721Semaste if (to_superclass) 540254721Semaste break; // we're not going to override it if it's set 541254721Semaste 542254721Semaste ObjCInterfaceDecl *from_objc_interface = dyn_cast<ObjCInterfaceDecl>(from); 543254721Semaste 544254721Semaste if (!from_objc_interface) 545254721Semaste break; 546254721Semaste 547254721Semaste ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass(); 548254721Semaste 549254721Semaste if (!from_superclass) 550254721Semaste break; 551254721Semaste 552254721Semaste Decl *imported_from_superclass_decl = Import(from_superclass); 553254721Semaste 554254721Semaste if (!imported_from_superclass_decl) 555254721Semaste break; 556254721Semaste 557254721Semaste ObjCInterfaceDecl *imported_from_superclass = dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl); 558254721Semaste 559254721Semaste if (!imported_from_superclass) 560254721Semaste break; 561254721Semaste 562254721Semaste if (!to_objc_interface->hasDefinition()) 563254721Semaste to_objc_interface->startDefinition(); 564254721Semaste 565254721Semaste to_objc_interface->setSuperClass(imported_from_superclass); 566254721Semaste } 567254721Semaste while (0); 568254721Semaste } 569254721Semaste} 570254721Semaste 571254721Semasteclang::Decl * 572254721SemasteClangASTImporter::Minion::Imported (clang::Decl *from, clang::Decl *to) 573254721Semaste{ 574254721Semaste ClangASTMetrics::RegisterClangImport(); 575254721Semaste 576254721Semaste Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 577254721Semaste 578254721Semaste if (log) 579254721Semaste { 580254721Semaste lldb::user_id_t user_id; 581254721Semaste ClangASTMetadata *metadata = m_master.GetDeclMetadata(from); 582254721Semaste if (metadata) 583254721Semaste user_id = metadata->GetUserID(); 584254721Semaste 585254721Semaste if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) 586254721Semaste { 587254721Semaste std::string name_string; 588254721Semaste llvm::raw_string_ostream name_stream(name_string); 589254721Semaste from_named_decl->printName(name_stream); 590254721Semaste name_stream.flush(); 591254721Semaste 592254721Semaste log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p, named %s (from (Decl*)%p), metadata 0x%" PRIx64, 593254721Semaste from->getDeclKindName(), 594254721Semaste to, 595254721Semaste name_string.c_str(), 596254721Semaste from, 597254721Semaste user_id); 598254721Semaste } 599254721Semaste else 600254721Semaste { 601254721Semaste log->Printf(" [ClangASTImporter] Imported (%sDecl*)%p (from (Decl*)%p), metadata 0x%" PRIx64, 602254721Semaste from->getDeclKindName(), 603254721Semaste to, 604254721Semaste from, 605254721Semaste user_id); 606254721Semaste } 607254721Semaste } 608254721Semaste 609254721Semaste ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&to->getASTContext()); 610254721Semaste ASTContextMetadataSP from_context_md = m_master.MaybeGetContextMetadata(m_source_ctx); 611254721Semaste 612254721Semaste if (from_context_md) 613254721Semaste { 614254721Semaste OriginMap &origins = from_context_md->m_origins; 615254721Semaste 616254721Semaste OriginMap::iterator origin_iter = origins.find(from); 617254721Semaste 618254721Semaste if (origin_iter != origins.end()) 619254721Semaste { 620254721Semaste to_context_md->m_origins[to] = origin_iter->second; 621254721Semaste 622254721Semaste MinionSP direct_completer = m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx); 623254721Semaste 624254721Semaste if (direct_completer.get() != this) 625254721Semaste direct_completer->ASTImporter::Imported(origin_iter->second.decl, to); 626254721Semaste 627254721Semaste if (log) 628254721Semaste log->Printf(" [ClangASTImporter] Propagated origin (Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to (ASTContext*)%p", 629254721Semaste origin_iter->second.decl, 630254721Semaste origin_iter->second.ctx, 631254721Semaste &from->getASTContext(), 632254721Semaste &to->getASTContext()); 633254721Semaste } 634254721Semaste else 635254721Semaste { 636254721Semaste if (m_decls_to_deport && m_decls_already_deported) 637254721Semaste { 638254721Semaste if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to)) 639254721Semaste { 640254721Semaste NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to); 641254721Semaste 642254721Semaste if (!m_decls_already_deported->count(to_named_decl)) 643254721Semaste m_decls_to_deport->insert(to_named_decl); 644254721Semaste } 645254721Semaste 646254721Semaste } 647254721Semaste to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from); 648254721Semaste 649254721Semaste if (log) 650254721Semaste log->Printf(" [ClangASTImporter] Decl has no origin information in (ASTContext*)%p", 651254721Semaste &from->getASTContext()); 652254721Semaste } 653254721Semaste 654254721Semaste if (clang::NamespaceDecl *to_namespace = dyn_cast<clang::NamespaceDecl>(to)) 655254721Semaste { 656254721Semaste clang::NamespaceDecl *from_namespace = dyn_cast<clang::NamespaceDecl>(from); 657254721Semaste 658254721Semaste NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps; 659254721Semaste 660254721Semaste NamespaceMetaMap::iterator namespace_map_iter = namespace_maps.find(from_namespace); 661254721Semaste 662254721Semaste if (namespace_map_iter != namespace_maps.end()) 663254721Semaste to_context_md->m_namespace_maps[to_namespace] = namespace_map_iter->second; 664254721Semaste } 665254721Semaste } 666254721Semaste else 667254721Semaste { 668254721Semaste to_context_md->m_origins[to] = DeclOrigin (m_source_ctx, from); 669254721Semaste 670254721Semaste if (log) 671254721Semaste log->Printf(" [ClangASTImporter] Sourced origin (Decl*)%p/(ASTContext*)%p into (ASTContext*)%p", 672254721Semaste from, 673254721Semaste m_source_ctx, 674254721Semaste &to->getASTContext()); 675254721Semaste } 676254721Semaste 677254721Semaste if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from)) 678254721Semaste { 679254721Semaste TagDecl *to_tag_decl = dyn_cast<TagDecl>(to); 680254721Semaste 681254721Semaste to_tag_decl->setHasExternalLexicalStorage(); 682254721Semaste to_tag_decl->setMustBuildLookupTable(); 683254721Semaste 684254721Semaste if (log) 685254721Semaste log->Printf(" [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]", 686254721Semaste (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""), 687254721Semaste (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""), 688254721Semaste (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"), 689254721Semaste (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete")); 690254721Semaste } 691254721Semaste 692254721Semaste if (isa<NamespaceDecl>(from)) 693254721Semaste { 694254721Semaste NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to); 695254721Semaste 696254721Semaste m_master.BuildNamespaceMap(to_namespace_decl); 697254721Semaste 698254721Semaste to_namespace_decl->setHasExternalVisibleStorage(); 699254721Semaste } 700254721Semaste 701254721Semaste if (isa<ObjCInterfaceDecl>(from)) 702254721Semaste { 703254721Semaste ObjCInterfaceDecl *to_interface_decl = dyn_cast<ObjCInterfaceDecl>(to); 704254721Semaste 705254721Semaste to_interface_decl->setHasExternalLexicalStorage(); 706254721Semaste to_interface_decl->setHasExternalVisibleStorage(); 707254721Semaste 708254721Semaste /*to_interface_decl->setExternallyCompleted();*/ 709254721Semaste 710254721Semaste if (log) 711254721Semaste log->Printf(" [ClangASTImporter] To is an ObjCInterfaceDecl - attributes %s%s%s", 712254721Semaste (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""), 713254721Semaste (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""), 714254721Semaste (to_interface_decl->hasDefinition() ? " HasDefinition" : "")); 715254721Semaste } 716254721Semaste 717254721Semaste return clang::ASTImporter::Imported(from, to); 718254721Semaste} 719263363Semaste 720263363Semasteclang::Decl *ClangASTImporter::Minion::GetOriginalDecl (clang::Decl *To) 721263363Semaste{ 722263363Semaste ASTContextMetadataSP to_context_md = m_master.GetContextMetadata(&To->getASTContext()); 723263363Semaste 724263363Semaste if (!to_context_md) 725263363Semaste return NULL; 726263363Semaste 727263363Semaste OriginMap::iterator iter = to_context_md->m_origins.find(To); 728263363Semaste 729263363Semaste if (iter == to_context_md->m_origins.end()) 730263363Semaste return NULL; 731263363Semaste 732263363Semaste return const_cast<clang::Decl*>(iter->second.decl); 733263363Semaste} 734