20 using namespace clang;
24 template <
typename T>
struct Source {
27 operator T() {
return t; }
28 template <
typename U = T> U &
get() {
return t; }
29 template <
typename U = T>
const U &
get()
const {
return t; }
30 template <
typename U>
operator Source<U>() {
return Source<U>(t); }
33 typedef std::pair<Source<NamedDecl *>,
ASTImporter *> Candidate;
39 :
ASTImporter(ToContext, ToFileManager, FromContext, FromFileManager,
42 if (
auto ToTag = dyn_cast<TagDecl>(To)) {
43 ToTag->setHasExternalLexicalStorage();
44 ToTag->setMustBuildLookupTable();
45 }
else if (
auto ToNamespace = dyn_cast<NamespaceDecl>(To)) {
46 ToNamespace->setHasExternalVisibleStorage();
52 Source<const DeclContext *>
53 LookupSameContext(Source<TranslationUnitDecl *> SourceTU,
const DeclContext *DC,
58 Source<const DeclContext *> SourceParentDC =
59 LookupSameContext(SourceTU, DC->
getParent(), ReverseImporter);
60 if (!SourceParentDC) {
64 auto ND = cast<NamedDecl>(DC);
66 Source<DeclarationName> SourceName = ReverseImporter.
Import(Name);
68 SourceParentDC.get()->lookup(SourceName.get());
69 size_t SearchResultSize = SearchResult.
size();
72 assert(SearchResultSize < 2);
73 if (SearchResultSize == 0) {
77 NamedDecl *SearchResultDecl = SearchResult[0];
82 bool IsForwardDeclaration(
Decl *D) {
83 assert(!isa<ObjCInterfaceDecl>(D));
84 if (
auto TD = dyn_cast<TagDecl>(D)) {
85 return !TD->isThisDeclarationADefinition();
86 }
else if (
auto FD = dyn_cast<FunctionDecl>(D)) {
87 return !FD->isThisDeclarationADefinition();
93 template <
typename CallbackType>
94 void ForEachMatchingDC(
99 Source<TranslationUnitDecl *> SourceTU =
100 IP.Forward->getFromContext().getTranslationUnitDecl();
101 if (
auto SourceDC = LookupSameContext(SourceTU, DC, *IP.Reverse))
107 return llvm::any_of(Decls, [&](
const Candidate &D) {
108 return C.first.get()->getKind() == D.first.get()->getKind();
117 {llvm::make_unique<LazyASTImporter>(Target.
AST, Target.
FM,
S.AST,
S.FM),
118 llvm::make_unique<ASTImporter>(
S.AST,
S.FM, Target.
AST, Target.
FM,
129 auto FilterFoundDecl = [&CompleteDecls, &ForwardDecls](
const Candidate &C) {
130 if (IsForwardDeclaration(C.first.get())) {
131 if (!HasDeclOfSameType(ForwardDecls, C)) {
132 ForwardDecls.push_back(C);
135 CompleteDecls.push_back(C);
141 [&](
const ImporterPair &IP, Source<const DeclContext *> SourceDC) {
145 FilterFoundDecl(std::make_pair(FromD, IP.
Forward.get()));
150 CompleteDecls.empty() ? ForwardDecls : CompleteDecls;
152 if (DeclsToReport.empty()) {
156 Decls.reserve(DeclsToReport.size());
157 for (
const Candidate &C : DeclsToReport) {
158 NamedDecl *d = cast<NamedDecl>(C.second->Import(C.first.get()));
171 [&](
const ImporterPair &IP, Source<const DeclContext *> SourceDC) {
172 for (
const Decl *SourceDecl : SourceDC.get()->decls()) {
173 if (IsKindWeWant(SourceDecl->getKind())) {
175 IP.
Forward->Import(const_cast<Decl *>(SourceDecl));
Defines the clang::ASTContext interface.
Implements support for file system lookup, file system caching, and directory search management...
Decl - This represents one declaration (or definition), e.g.
static DeclContextLookupResult SetExternalVisibleDeclsForName(const DeclContext *DC, DeclarationName Name, ArrayRef< NamedDecl * > Decls)
QualType Import(QualType FromT)
Import the given type from the "from" context into the "to" context.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
The results of name lookup within a DeclContext.
bool isTranslationUnit() const
std::unique_ptr< ASTImporter > Forward
bool FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) override
Find all declarations with the given name in the given context, and add them to the context by callin...
MatchFinder::MatchCallback * Callback
DeclContext * getDeclContext()
DeclContext * getParent()
getParent - Returns the containing DeclContext.
The result type of a method or function.
void FindExternalLexicalDecls(const DeclContext *DC, llvm::function_ref< bool(Decl::Kind)> IsKindWeWant, SmallVectorImpl< Decl * > &Result) override
Finds all declarations lexically contained within the given DeclContext, after applying an optional f...
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
virtual Decl * Imported(Decl *From, Decl *To)
Note that we have imported the "from" declaration by mapping it to the (potentially-newly-created) "t...
DeclarationName - The name of a declaration.
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
ExternalASTMerger(const ImporterEndpoint &Target, llvm::ArrayRef< ImporterEndpoint > Sources)
Kind
Lists the kind of concrete classes of Decl.
std::unique_ptr< ASTImporter > Reverse
NamedDecl - This represents a decl with a name.