Skip to content

Commit

Permalink
Rename Minion to ASTImporterDelegate
Browse files Browse the repository at this point in the history
Summary:
I think there universal agreement that Minion isn't the best name for this class. This patch renames the class
 to ASTImporterDelegate to better reflect it's goal of monitoring and extending the ASTImporter.

Reviewers: aprantl, shafik, martong, a.sidorin, davide

Reviewed By: aprantl, shafik, davide

Subscribers: rnkovacs, davide, abidh, lldb-commits

Tags: #lldb

Differential Revision: https://reviews.llvm.org/D61299

llvm-svn: 359777
  • Loading branch information
Teemperor committed May 2, 2019
1 parent 3d7b8fd commit 3356c32
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 78 deletions.
78 changes: 45 additions & 33 deletions lldb/include/lldb/Symbol/ClangASTImporter.h
Expand Up @@ -235,42 +235,52 @@ class ClangASTImporter {

typedef std::map<const clang::Decl *, DeclOrigin> OriginMap;

class Minion : public clang::ASTImporter {
/// ASTImporter that intercepts and records the import process of the
/// underlying ASTImporter.
///
/// This class updates the map from declarations to their original
/// declarations and can record and complete declarations that have been
/// imported in a certain interval.
///
/// When intercepting a declaration import, the ASTImporterDelegate uses the
/// CxxModuleHandler to replace any missing or malformed declarations with
/// their counterpart from a C++ module.
class ASTImporterDelegate : public clang::ASTImporter {
public:
Minion(ClangASTImporter &master, clang::ASTContext *target_ctx,
clang::ASTContext *source_ctx)
ASTImporterDelegate(ClangASTImporter &master, clang::ASTContext *target_ctx,
clang::ASTContext *source_ctx)
: clang::ASTImporter(*target_ctx, master.m_file_manager, *source_ctx,
master.m_file_manager, true /*minimal*/),
m_decls_to_deport(nullptr), m_decls_already_deported(nullptr),
m_master(master), m_source_ctx(source_ctx) {}

/// Scope guard that attaches a CxxModuleHandler to a Minion and deattaches
/// it at the end of the scope. Supports being used multiple times on the
/// same Minion instance in nested scopes.
/// Scope guard that attaches a CxxModuleHandler to an ASTImporterDelegate
/// and deattaches it at the end of the scope. Supports being used multiple
/// times on the same ASTImporterDelegate instance in nested scopes.
class CxxModuleScope {
/// The handler we attach to the Minion.
/// The handler we attach to the ASTImporterDelegate.
CxxModuleHandler m_handler;
/// The Minion we are supposed to attach the handler to.
Minion &m_minion;
/// True iff we attached the handler to the Minion.
/// The ASTImporterDelegate we are supposed to attach the handler to.
ASTImporterDelegate &m_delegate;
/// True iff we attached the handler to the ASTImporterDelegate.
bool m_valid = false;

public:
CxxModuleScope(Minion &minion, clang::ASTContext *dst_ctx)
: m_minion(minion) {
// If the minion doesn't have a CxxModuleHandler yet, create one
CxxModuleScope(ASTImporterDelegate &delegate, clang::ASTContext *dst_ctx)
: m_delegate(delegate) {
// If the delegate doesn't have a CxxModuleHandler yet, create one
// and attach it.
if (!minion.m_std_handler) {
m_handler = CxxModuleHandler(minion, dst_ctx);
if (!delegate.m_std_handler) {
m_handler = CxxModuleHandler(delegate, dst_ctx);
m_valid = true;
minion.m_std_handler = &m_handler;
delegate.m_std_handler = &m_handler;
}
}
~CxxModuleScope() {
if (m_valid) {
// Make sure no one messed with the handler we placed.
assert(m_minion.m_std_handler == &m_handler);
m_minion.m_std_handler = nullptr;
assert(m_delegate.m_std_handler == &m_handler);
m_delegate.m_std_handler = nullptr;
}
}
};
Expand All @@ -279,16 +289,16 @@ class ClangASTImporter {
llvm::Expected<clang::Decl *> ImportImpl(clang::Decl *From) override;

public:
// A call to "InitDeportWorkQueues" puts the minion into deport mode.
// A call to "InitDeportWorkQueues" puts the delegate into deport mode.
// In deport mode, every copied Decl that could require completion is
// recorded and placed into the decls_to_deport set.
//
// A call to "ExecuteDeportWorkQueues" completes all the Decls that
// are in decls_to_deport, adding any Decls it sees along the way that it
// hasn't already deported. It proceeds until decls_to_deport is empty.
//
// These calls must be paired. Leaving a minion in deport mode or trying
// to start deport minion with a new pair of queues will result in an
// These calls must be paired. Leaving a delegate in deport mode or trying
// to start deport delegate with a new pair of queues will result in an
// assertion failure.

void
Expand All @@ -314,18 +324,18 @@ class ClangASTImporter {
CxxModuleHandler *m_std_handler = nullptr;
};

typedef std::shared_ptr<Minion> MinionSP;
typedef std::map<clang::ASTContext *, MinionSP> MinionMap;
typedef std::shared_ptr<ASTImporterDelegate> ImporterDelegateSP;
typedef std::map<clang::ASTContext *, ImporterDelegateSP> DelegateMap;
typedef std::map<const clang::NamespaceDecl *, NamespaceMapSP>
NamespaceMetaMap;

struct ASTContextMetadata {
ASTContextMetadata(clang::ASTContext *dst_ctx)
: m_dst_ctx(dst_ctx), m_minions(), m_origins(), m_namespace_maps(),
: m_dst_ctx(dst_ctx), m_delegates(), m_origins(), m_namespace_maps(),
m_map_completer(nullptr) {}

clang::ASTContext *m_dst_ctx;
MinionMap m_minions;
DelegateMap m_delegates;
OriginMap m_origins;

NamespaceMetaMap m_namespace_maps;
Expand Down Expand Up @@ -360,18 +370,20 @@ class ClangASTImporter {
return ASTContextMetadataSP();
}

MinionSP GetMinion(clang::ASTContext *dst_ctx, clang::ASTContext *src_ctx) {
ImporterDelegateSP GetDelegate(clang::ASTContext *dst_ctx,
clang::ASTContext *src_ctx) {
ASTContextMetadataSP context_md = GetContextMetadata(dst_ctx);

MinionMap &minions = context_md->m_minions;
MinionMap::iterator minion_iter = minions.find(src_ctx);
DelegateMap &delegates = context_md->m_delegates;
DelegateMap::iterator delegate_iter = delegates.find(src_ctx);

if (minion_iter == minions.end()) {
MinionSP minion = MinionSP(new Minion(*this, dst_ctx, src_ctx));
minions[src_ctx] = minion;
return minion;
if (delegate_iter == delegates.end()) {
ImporterDelegateSP delegate =
ImporterDelegateSP(new ASTImporterDelegate(*this, dst_ctx, src_ctx));
delegates[src_ctx] = delegate;
return delegate;
} else {
return minion_iter->second;
return delegate_iter->second;
}
}

Expand Down
97 changes: 52 additions & 45 deletions lldb/source/Symbol/ClangASTImporter.cpp
Expand Up @@ -58,12 +58,12 @@ void ClangASTMetrics::DumpCounters(Log *log) {
clang::QualType ClangASTImporter::CopyType(clang::ASTContext *dst_ast,
clang::ASTContext *src_ast,
clang::QualType type) {
MinionSP minion_sp(GetMinion(dst_ast, src_ast));
ImporterDelegateSP delegate_sp(GetDelegate(dst_ast, src_ast));

Minion::CxxModuleScope std_scope(*minion_sp, dst_ast);
ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, dst_ast);

if (minion_sp)
return minion_sp->Import(type);
if (delegate_sp)
return delegate_sp->Import(type);

return QualType();
}
Expand Down Expand Up @@ -99,14 +99,14 @@ CompilerType ClangASTImporter::CopyType(ClangASTContext &dst_ast,
clang::Decl *ClangASTImporter::CopyDecl(clang::ASTContext *dst_ast,
clang::ASTContext *src_ast,
clang::Decl *decl) {
MinionSP minion_sp;
ImporterDelegateSP delegate_sp;

minion_sp = GetMinion(dst_ast, src_ast);
delegate_sp = GetDelegate(dst_ast, src_ast);

Minion::CxxModuleScope std_scope(*minion_sp, dst_ast);
ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, dst_ast);

if (minion_sp) {
clang::Decl *result = minion_sp->Import(decl);
if (delegate_sp) {
clang::Decl *result = delegate_sp->Import(decl);

if (!result) {
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Expand Down Expand Up @@ -254,9 +254,9 @@ ClangASTImporter::DeportType(clang::ASTContext *dst_ctx,
(unsigned long long)type, static_cast<void *>(src_ctx),
static_cast<void *>(dst_ctx));

MinionSP minion_sp(GetMinion(dst_ctx, src_ctx));
ImporterDelegateSP delegate_sp(GetDelegate(dst_ctx, src_ctx));

if (!minion_sp)
if (!delegate_sp)
return nullptr;

std::set<NamedDecl *> decls_to_deport;
Expand All @@ -270,11 +270,11 @@ ClangASTImporter::DeportType(clang::ASTContext *dst_ctx,
tag_type->getDecl());
}

minion_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);
delegate_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);

lldb::opaque_compiler_type_t result = CopyType(dst_ctx, src_ctx, type);

minion_sp->ExecuteDeportWorkQueues();
delegate_sp->ExecuteDeportWorkQueues();

if (!result)
return nullptr;
Expand All @@ -293,9 +293,9 @@ clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx,
decl->getDeclKindName(), static_cast<void *>(decl),
static_cast<void *>(src_ctx), static_cast<void *>(dst_ctx));

MinionSP minion_sp(GetMinion(dst_ctx, src_ctx));
ImporterDelegateSP delegate_sp(GetDelegate(dst_ctx, src_ctx));

if (!minion_sp)
if (!delegate_sp)
return nullptr;

std::set<NamedDecl *> decls_to_deport;
Expand All @@ -305,11 +305,11 @@ clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx,

decl_context_override.OverrideAllDeclsFromContainingFunction(decl);

minion_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);
delegate_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);

clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);

minion_sp->ExecuteDeportWorkQueues();
delegate_sp->ExecuteDeportWorkQueues();

if (!result)
return nullptr;
Expand Down Expand Up @@ -561,11 +561,13 @@ bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
return false;

MinionSP minion_sp(GetMinion(&decl->getASTContext(), decl_origin.ctx));
ImporterDelegateSP delegate_sp(
GetDelegate(&decl->getASTContext(), decl_origin.ctx));

Minion::CxxModuleScope std_scope(*minion_sp, &decl->getASTContext());
if (minion_sp)
minion_sp->ImportDefinitionTo(decl, decl_origin.decl);
ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
&decl->getASTContext());
if (delegate_sp)
delegate_sp->ImportDefinitionTo(decl, decl_origin.decl);

return true;
}
Expand All @@ -579,10 +581,11 @@ bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
return false;

MinionSP minion_sp(GetMinion(&decl->getASTContext(), origin_ast_ctx));
ImporterDelegateSP delegate_sp(
GetDelegate(&decl->getASTContext(), origin_ast_ctx));

if (minion_sp)
minion_sp->ImportDefinitionTo(decl, origin_decl);
if (delegate_sp)
delegate_sp->ImportDefinitionTo(decl, origin_decl);

ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());

Expand All @@ -605,11 +608,11 @@ bool ClangASTImporter::CompleteObjCInterfaceDecl(
if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
return false;

MinionSP minion_sp(
GetMinion(&interface_decl->getASTContext(), decl_origin.ctx));
ImporterDelegateSP delegate_sp(
GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx));

if (minion_sp)
minion_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
if (delegate_sp)
delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);

if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
Expand All @@ -629,14 +632,16 @@ bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
if (!decl_origin.Valid())
return false;

MinionSP minion_sp(GetMinion(&tag_decl->getASTContext(), decl_origin.ctx));
ImporterDelegateSP delegate_sp(
GetDelegate(&tag_decl->getASTContext(), decl_origin.ctx));

Minion::CxxModuleScope std_scope(*minion_sp, &tag_decl->getASTContext());
ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
&tag_decl->getASTContext());

TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);

for (Decl *origin_child_decl : origin_tag_decl->decls()) {
minion_sp->Import(origin_child_decl);
delegate_sp->Import(origin_child_decl);
}

if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl)) {
Expand All @@ -654,14 +659,14 @@ bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
if (!decl_origin.Valid())
return false;

MinionSP minion_sp(
GetMinion(&objc_interface_decl->getASTContext(), decl_origin.ctx));
ImporterDelegateSP delegate_sp(
GetDelegate(&objc_interface_decl->getASTContext(), decl_origin.ctx));

ObjCInterfaceDecl *origin_interface_decl =
llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);

for (Decl *origin_child_decl : origin_interface_decl->decls()) {
minion_sp->Import(origin_child_decl);
delegate_sp->Import(origin_child_decl);
}

return true;
Expand Down Expand Up @@ -812,7 +817,7 @@ void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
if (!md)
return;

md->m_minions.erase(src_ast);
md->m_delegates.erase(src_ast);

for (OriginMap::iterator iter = md->m_origins.begin();
iter != md->m_origins.end();) {
Expand All @@ -825,7 +830,8 @@ void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,

ClangASTImporter::MapCompleter::~MapCompleter() { return; }

llvm::Expected<Decl *> ClangASTImporter::Minion::ImportImpl(Decl *From) {
llvm::Expected<Decl *>
ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
if (m_std_handler) {
llvm::Optional<Decl *> D = m_std_handler->Import(From);
if (D) {
Expand All @@ -842,7 +848,7 @@ llvm::Expected<Decl *> ClangASTImporter::Minion::ImportImpl(Decl *From) {
return ASTImporter::ImportImpl(From);
}

void ClangASTImporter::Minion::InitDeportWorkQueues(
void ClangASTImporter::ASTImporterDelegate::InitDeportWorkQueues(
std::set<clang::NamedDecl *> *decls_to_deport,
std::set<clang::NamedDecl *> *decls_already_deported) {
assert(!m_decls_to_deport);
Expand All @@ -852,7 +858,7 @@ void ClangASTImporter::Minion::InitDeportWorkQueues(
m_decls_already_deported = decls_already_deported;
}

void ClangASTImporter::Minion::ExecuteDeportWorkQueues() {
void ClangASTImporter::ASTImporterDelegate::ExecuteDeportWorkQueues() {
assert(m_decls_to_deport);
assert(m_decls_already_deported);

Expand Down Expand Up @@ -899,8 +905,8 @@ void ClangASTImporter::Minion::ExecuteDeportWorkQueues() {
m_decls_already_deported = nullptr;
}

void ClangASTImporter::Minion::ImportDefinitionTo(clang::Decl *to,
clang::Decl *from) {
void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(
clang::Decl *to, clang::Decl *from) {
ASTImporter::Imported(from, to);

/*
Expand Down Expand Up @@ -963,8 +969,8 @@ void ClangASTImporter::Minion::ImportDefinitionTo(clang::Decl *to,
}
}

void ClangASTImporter::Minion::Imported(clang::Decl *from,
clang::Decl *to) {
void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
clang::Decl *to) {
ClangASTMetrics::RegisterClangImport();

Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
Expand Down Expand Up @@ -1015,8 +1021,8 @@ void ClangASTImporter::Minion::Imported(clang::Decl *from,
to_context_md->m_origins[to] = origin_iter->second;
}

MinionSP direct_completer =
m_master.GetMinion(&to->getASTContext(), origin_iter->second.ctx);
ImporterDelegateSP direct_completer =
m_master.GetDelegate(&to->getASTContext(), origin_iter->second.ctx);

if (direct_completer.get() != this)
direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
Expand Down Expand Up @@ -1129,7 +1135,8 @@ void ClangASTImporter::Minion::Imported(clang::Decl *from,
}
}

clang::Decl *ClangASTImporter::Minion::GetOriginalDecl(clang::Decl *To) {
clang::Decl *
ClangASTImporter::ASTImporterDelegate::GetOriginalDecl(clang::Decl *To) {
ASTContextMetadataSP to_context_md =
m_master.GetContextMetadata(&To->getASTContext());

Expand Down

0 comments on commit 3356c32

Please sign in to comment.