[ASTImporter] Changed use of Import to Import_New in ASTImporter.

Reviewers: a.sidorin, shafik, a_sidorin

Reviewed By: a_sidorin

Subscribers: gamesh411, a_sidorin, dkrupp, martong, Szelethus, cfe-commits

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

llvm-svn: 347752
This commit is contained in:
Balazs Keri 2018-11-28 13:21:26 +00:00
parent ac6e1fb89d
commit deaf7ab810
2 changed files with 376 additions and 319 deletions

View File

@ -154,15 +154,10 @@ class Attr;
/// \return Error information (success or error). /// \return Error information (success or error).
template <typename ImportT> template <typename ImportT>
LLVM_NODISCARD llvm::Error importInto(ImportT &To, const ImportT &From) { LLVM_NODISCARD llvm::Error importInto(ImportT &To, const ImportT &From) {
To = Import(From); auto ToOrErr = Import_New(From);
if (From && !To) if (ToOrErr)
return llvm::make_error<ImportError>(); To = *ToOrErr;
return llvm::Error::success(); return ToOrErr.takeError();
// FIXME: this should be the final code
//auto ToOrErr = Import(From);
//if (ToOrErr)
// To = *ToOrErr;
//return ToOrErr.takeError();
} }
/// Import the given type from the "from" context into the "to" /// Import the given type from the "from" context into the "to"

View File

@ -154,25 +154,6 @@ namespace clang {
return None; return None;
} }
// FIXME: Temporary until every import returns Expected.
template <>
LLVM_NODISCARD Error
ASTImporter::importInto(SourceLocation &To, const SourceLocation &From) {
To = Import(From);
if (From.isValid() && To.isInvalid())
return llvm::make_error<ImportError>();
return Error::success();
}
// FIXME: Temporary until every import returns Expected.
template <>
LLVM_NODISCARD Error
ASTImporter::importInto(QualType &To, const QualType &From) {
To = Import(From);
if (!From.isNull() && To.isNull())
return llvm::make_error<ImportError>();
return Error::success();
}
class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>, class ASTNodeImporter : public TypeVisitor<ASTNodeImporter, ExpectedType>,
public DeclVisitor<ASTNodeImporter, ExpectedDecl>, public DeclVisitor<ASTNodeImporter, ExpectedDecl>,
public StmtVisitor<ASTNodeImporter, ExpectedStmt> { public StmtVisitor<ASTNodeImporter, ExpectedStmt> {
@ -7684,14 +7665,8 @@ ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
ASTImporter::~ASTImporter() = default; ASTImporter::~ASTImporter() = default;
Expected<QualType> ASTImporter::Import_New(QualType FromT) { Expected<QualType> ASTImporter::Import_New(QualType FromT) {
QualType ToT = Import(FromT);
if (ToT.isNull() && !FromT.isNull())
return make_error<ImportError>();
return ToT;
}
QualType ASTImporter::Import(QualType FromT) {
if (FromT.isNull()) if (FromT.isNull())
return {}; return QualType{};
const Type *FromTy = FromT.getTypePtr(); const Type *FromTy = FromT.getTypePtr();
@ -7704,46 +7679,64 @@ QualType ASTImporter::Import(QualType FromT) {
// Import the type // Import the type
ASTNodeImporter Importer(*this); ASTNodeImporter Importer(*this);
ExpectedType ToTOrErr = Importer.Visit(FromTy); ExpectedType ToTOrErr = Importer.Visit(FromTy);
if (!ToTOrErr) { if (!ToTOrErr)
llvm::consumeError(ToTOrErr.takeError()); return ToTOrErr.takeError();
return {};
}
// Record the imported type. // Record the imported type.
ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr(); ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr();
return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers()); return ToContext.getQualifiedType(*ToTOrErr, FromT.getLocalQualifiers());
} }
QualType ASTImporter::Import(QualType From) {
llvm::Expected<QualType> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return {};
}
Expected<TypeSourceInfo *> ASTImporter::Import_New(TypeSourceInfo *FromTSI) { Expected<TypeSourceInfo *> ASTImporter::Import_New(TypeSourceInfo *FromTSI) {
TypeSourceInfo *ToTSI = Import(FromTSI);
if (!ToTSI && FromTSI)
return llvm::make_error<ImportError>();
return ToTSI;
}
TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) {
if (!FromTSI) if (!FromTSI)
return FromTSI; return FromTSI;
// FIXME: For now we just create a "trivial" type source info based // FIXME: For now we just create a "trivial" type source info based
// on the type and a single location. Implement a real version of this. // on the type and a single location. Implement a real version of this.
QualType T = Import(FromTSI->getType()); ExpectedType TOrErr = Import_New(FromTSI->getType());
if (T.isNull()) if (!TOrErr)
return nullptr; return TOrErr.takeError();
ExpectedSLoc BeginLocOrErr = Import_New(FromTSI->getTypeLoc().getBeginLoc());
if (!BeginLocOrErr)
return BeginLocOrErr.takeError();
return ToContext.getTrivialTypeSourceInfo( return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr);
T, Import(FromTSI->getTypeLoc().getBeginLoc())); }
TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *From) {
llvm::Expected<TypeSourceInfo *> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return nullptr;
} }
Expected<Attr *> ASTImporter::Import_New(const Attr *FromAttr) { Expected<Attr *> ASTImporter::Import_New(const Attr *FromAttr) {
return Import(FromAttr);
}
Attr *ASTImporter::Import(const Attr *FromAttr) {
Attr *ToAttr = FromAttr->clone(ToContext); Attr *ToAttr = FromAttr->clone(ToContext);
// NOTE: Import of SourceRange may fail. if (auto ToRangeOrErr = Import_New(FromAttr->getRange()))
ToAttr->setRange(Import(FromAttr->getRange())); ToAttr->setRange(*ToRangeOrErr);
else
return ToRangeOrErr.takeError();
return ToAttr; return ToAttr;
} }
Attr *ASTImporter::Import(const Attr *From) {
llvm::Expected<Attr *> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return nullptr;
}
Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) { Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD);
@ -7759,12 +7752,6 @@ Decl *ASTImporter::GetAlreadyImportedOrNull(Decl *FromD) {
} }
Expected<Decl *> ASTImporter::Import_New(Decl *FromD) { Expected<Decl *> ASTImporter::Import_New(Decl *FromD) {
Decl *ToD = Import(FromD);
if (!ToD && FromD)
return llvm::make_error<ImportError>();
return ToD;
}
Decl *ASTImporter::Import(Decl *FromD) {
if (!FromD) if (!FromD)
return nullptr; return nullptr;
@ -7778,19 +7765,25 @@ Decl *ASTImporter::Import(Decl *FromD) {
return ToD; return ToD;
} }
// Import the type. // Import the declaration.
ExpectedDecl ToDOrErr = Importer.Visit(FromD); ExpectedDecl ToDOrErr = Importer.Visit(FromD);
if (!ToDOrErr) { if (!ToDOrErr)
llvm::consumeError(ToDOrErr.takeError()); return ToDOrErr;
return nullptr;
}
ToD = *ToDOrErr; ToD = *ToDOrErr;
// Notify subclasses. // Notify subclasses.
Imported(FromD, ToD); Imported(FromD, ToD);
updateFlags(FromD, ToD); updateFlags(FromD, ToD);
return ToD; return ToDOrErr;
}
Decl *ASTImporter::Import(Decl *From) {
llvm::Expected<Decl *> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return nullptr;
} }
Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) { Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
@ -7853,29 +7846,25 @@ Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
} }
Expected<Expr *> ASTImporter::Import_New(Expr *FromE) { Expected<Expr *> ASTImporter::Import_New(Expr *FromE) {
Expr *ToE = Import(FromE); if (ExpectedStmt ToSOrErr = Import_New(cast_or_null<Stmt>(FromE)))
if (!ToE && FromE) return cast_or_null<Expr>(*ToSOrErr);
return llvm::make_error<ImportError>(); else
return ToE; return ToSOrErr.takeError();
} }
Expr *ASTImporter::Import(Expr *FromE) { Expr *ASTImporter::Import(Expr *From) {
if (!FromE) llvm::Expected<Expr *> To = Import_New(From);
return nullptr; if (To)
return *To;
return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); else
llvm::consumeError(To.takeError());
return nullptr;
} }
Expected<Stmt *> ASTImporter::Import_New(Stmt *FromS) { Expected<Stmt *> ASTImporter::Import_New(Stmt *FromS) {
Stmt *ToS = Import(FromS);
if (!ToS && FromS)
return llvm::make_error<ImportError>();
return ToS;
}
Stmt *ASTImporter::Import(Stmt *FromS) {
if (!FromS) if (!FromS)
return nullptr; return nullptr;
// Check whether we've already imported this declaration. // Check whether we've already imported this statement.
llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS); llvm::DenseMap<Stmt *, Stmt *>::iterator Pos = ImportedStmts.find(FromS);
if (Pos != ImportedStmts.end()) if (Pos != ImportedStmts.end())
return Pos->second; return Pos->second;
@ -7883,10 +7872,8 @@ Stmt *ASTImporter::Import(Stmt *FromS) {
// Import the statement. // Import the statement.
ASTNodeImporter Importer(*this); ASTNodeImporter Importer(*this);
ExpectedStmt ToSOrErr = Importer.Visit(FromS); ExpectedStmt ToSOrErr = Importer.Visit(FromS);
if (!ToSOrErr) { if (!ToSOrErr)
llvm::consumeError(ToSOrErr.takeError()); return ToSOrErr;
return nullptr;
}
if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) { if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) {
auto *FromE = cast<Expr>(FromS); auto *FromE = cast<Expr>(FromS);
@ -7901,77 +7888,84 @@ Stmt *ASTImporter::Import(Stmt *FromS) {
FromE->containsUnexpandedParameterPack()); FromE->containsUnexpandedParameterPack());
} }
// Record the imported declaration. // Record the imported statement object.
ImportedStmts[FromS] = *ToSOrErr; ImportedStmts[FromS] = *ToSOrErr;
return *ToSOrErr; return ToSOrErr;
}
Stmt *ASTImporter::Import(Stmt *From) {
llvm::Expected<Stmt *> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return nullptr;
} }
Expected<NestedNameSpecifier *> Expected<NestedNameSpecifier *>
ASTImporter::Import_New(NestedNameSpecifier *FromNNS) { ASTImporter::Import_New(NestedNameSpecifier *FromNNS) {
NestedNameSpecifier *ToNNS = Import(FromNNS);
if (!ToNNS && FromNNS)
return llvm::make_error<ImportError>();
return ToNNS;
}
NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) {
if (!FromNNS) if (!FromNNS)
return nullptr; return nullptr;
NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); NestedNameSpecifier *Prefix;
if (Error Err = importInto(Prefix, FromNNS->getPrefix()))
return std::move(Err);
switch (FromNNS->getKind()) { switch (FromNNS->getKind()) {
case NestedNameSpecifier::Identifier: case NestedNameSpecifier::Identifier:
if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { assert(FromNNS->getAsIdentifier() && "NNS should contain identifier.");
return NestedNameSpecifier::Create(ToContext, prefix, II); return NestedNameSpecifier::Create(ToContext, Prefix,
} Import(FromNNS->getAsIdentifier()));
return nullptr;
case NestedNameSpecifier::Namespace: case NestedNameSpecifier::Namespace:
if (auto *NS = if (ExpectedDecl NSOrErr = Import_New(FromNNS->getAsNamespace())) {
cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { return NestedNameSpecifier::Create(ToContext, Prefix,
return NestedNameSpecifier::Create(ToContext, prefix, NS); cast<NamespaceDecl>(*NSOrErr));
} } else
return nullptr; return NSOrErr.takeError();
case NestedNameSpecifier::NamespaceAlias: case NestedNameSpecifier::NamespaceAlias:
if (auto *NSAD = if (ExpectedDecl NSADOrErr = Import_New(FromNNS->getAsNamespaceAlias()))
cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { return NestedNameSpecifier::Create(ToContext, Prefix,
return NestedNameSpecifier::Create(ToContext, prefix, NSAD); cast<NamespaceAliasDecl>(*NSADOrErr));
} else
return nullptr; return NSADOrErr.takeError();
case NestedNameSpecifier::Global: case NestedNameSpecifier::Global:
return NestedNameSpecifier::GlobalSpecifier(ToContext); return NestedNameSpecifier::GlobalSpecifier(ToContext);
case NestedNameSpecifier::Super: case NestedNameSpecifier::Super:
if (auto *RD = if (ExpectedDecl RDOrErr = Import_New(FromNNS->getAsRecordDecl()))
cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { return NestedNameSpecifier::SuperSpecifier(ToContext,
return NestedNameSpecifier::SuperSpecifier(ToContext, RD); cast<CXXRecordDecl>(*RDOrErr));
} else
return nullptr; return RDOrErr.takeError();
case NestedNameSpecifier::TypeSpec: case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate: { case NestedNameSpecifier::TypeSpecWithTemplate:
QualType T = Import(QualType(FromNNS->getAsType(), 0u)); if (Expected<QualType> TyOrErr =
if (!T.isNull()) { Import_New(QualType(FromNNS->getAsType(), 0u))) {
bool bTemplate = FromNNS->getKind() == bool TSTemplate =
NestedNameSpecifier::TypeSpecWithTemplate; FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate;
return NestedNameSpecifier::Create(ToContext, prefix, return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate,
bTemplate, T.getTypePtr()); TyOrErr->getTypePtr());
} } else {
return TyOrErr.takeError();
} }
return nullptr;
} }
llvm_unreachable("Invalid nested name specifier kind"); llvm_unreachable("Invalid nested name specifier kind");
} }
NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *From) {
llvm::Expected<NestedNameSpecifier *> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return nullptr;
}
Expected<NestedNameSpecifierLoc> Expected<NestedNameSpecifierLoc>
ASTImporter::Import_New(NestedNameSpecifierLoc FromNNS) { ASTImporter::Import_New(NestedNameSpecifierLoc FromNNS) {
NestedNameSpecifierLoc ToNNS = Import(FromNNS);
return ToNNS;
}
NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
// Copied from NestedNameSpecifier mostly. // Copied from NestedNameSpecifier mostly.
SmallVector<NestedNameSpecifierLoc , 8> NestedNames; SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
NestedNameSpecifierLoc NNS = FromNNS; NestedNameSpecifierLoc NNS = FromNNS;
@ -7987,84 +7981,90 @@ NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) {
while (!NestedNames.empty()) { while (!NestedNames.empty()) {
NNS = NestedNames.pop_back_val(); NNS = NestedNames.pop_back_val();
NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier()); NestedNameSpecifier *Spec;
if (!Spec) if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier()))
return NestedNameSpecifierLoc(); return std::move(Err);
NestedNameSpecifier::SpecifierKind Kind = Spec->getKind(); NestedNameSpecifier::SpecifierKind Kind = Spec->getKind();
SourceLocation ToLocalBeginLoc, ToLocalEndLoc;
if (Kind != NestedNameSpecifier::Super) {
if (Error Err = importInto(ToLocalBeginLoc, NNS.getLocalBeginLoc()))
return std::move(Err);
if (Kind != NestedNameSpecifier::Global)
if (Error Err = importInto(ToLocalEndLoc, NNS.getLocalEndLoc()))
return std::move(Err);
}
switch (Kind) { switch (Kind) {
case NestedNameSpecifier::Identifier: case NestedNameSpecifier::Identifier:
Builder.Extend(getToContext(), Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc,
Spec->getAsIdentifier(), ToLocalEndLoc);
Import(NNS.getLocalBeginLoc()),
Import(NNS.getLocalEndLoc()));
break; break;
case NestedNameSpecifier::Namespace: case NestedNameSpecifier::Namespace:
Builder.Extend(getToContext(), Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc,
Spec->getAsNamespace(), ToLocalEndLoc);
Import(NNS.getLocalBeginLoc()),
Import(NNS.getLocalEndLoc()));
break; break;
case NestedNameSpecifier::NamespaceAlias: case NestedNameSpecifier::NamespaceAlias:
Builder.Extend(getToContext(), Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(),
Spec->getAsNamespaceAlias(), ToLocalBeginLoc, ToLocalEndLoc);
Import(NNS.getLocalBeginLoc()),
Import(NNS.getLocalEndLoc()));
break; break;
case NestedNameSpecifier::TypeSpec: case NestedNameSpecifier::TypeSpec:
case NestedNameSpecifier::TypeSpecWithTemplate: { case NestedNameSpecifier::TypeSpecWithTemplate: {
TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo(
QualType(Spec->getAsType(), 0)); QualType(Spec->getAsType(), 0));
Builder.Extend(getToContext(), Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(),
Import(NNS.getLocalBeginLoc()), ToLocalEndLoc);
TSI->getTypeLoc(),
Import(NNS.getLocalEndLoc()));
break; break;
} }
case NestedNameSpecifier::Global: case NestedNameSpecifier::Global:
Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc())); Builder.MakeGlobal(getToContext(), ToLocalBeginLoc);
break; break;
case NestedNameSpecifier::Super: { case NestedNameSpecifier::Super: {
SourceRange ToRange = Import(NNS.getSourceRange()); auto ToSourceRangeOrErr = Import_New(NNS.getSourceRange());
Builder.MakeSuper(getToContext(), if (!ToSourceRangeOrErr)
Spec->getAsRecordDecl(), return ToSourceRangeOrErr.takeError();
ToRange.getBegin(),
ToRange.getEnd()); Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(),
ToSourceRangeOrErr->getBegin(),
ToSourceRangeOrErr->getEnd());
} }
} }
} }
return Builder.getWithLocInContext(getToContext()); return Builder.getWithLocInContext(getToContext());
} }
NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc From) {
llvm::Expected<NestedNameSpecifierLoc> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return {};
}
Expected<TemplateName> ASTImporter::Import_New(TemplateName From) { Expected<TemplateName> ASTImporter::Import_New(TemplateName From) {
TemplateName To = Import(From);
if (To.isNull() && !From.isNull())
return llvm::make_error<ImportError>();
return To;
}
TemplateName ASTImporter::Import(TemplateName From) {
switch (From.getKind()) { switch (From.getKind()) {
case TemplateName::Template: case TemplateName::Template:
if (auto *ToTemplate = if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr));
return TemplateName(ToTemplate); else
return ToTemplateOrErr.takeError();
return {};
case TemplateName::OverloadedTemplate: { case TemplateName::OverloadedTemplate: {
OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate();
UnresolvedSet<2> ToTemplates; UnresolvedSet<2> ToTemplates;
for (auto *I : *FromStorage) { for (auto *I : *FromStorage) {
if (auto *To = cast_or_null<NamedDecl>(Import(I))) if (auto ToOrErr = Import_New(I))
ToTemplates.addDecl(To); ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr));
else else
return {}; return ToOrErr.takeError();
} }
return ToContext.getOverloadedTemplateName(ToTemplates.begin(), return ToContext.getOverloadedTemplateName(ToTemplates.begin(),
ToTemplates.end()); ToTemplates.end());
@ -8072,107 +8072,118 @@ TemplateName ASTImporter::Import(TemplateName From) {
case TemplateName::QualifiedTemplate: { case TemplateName::QualifiedTemplate: {
QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName();
NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); auto QualifierOrErr = Import_New(QTN->getQualifier());
if (!Qualifier) if (!QualifierOrErr)
return {}; return QualifierOrErr.takeError();
if (auto *ToTemplate = if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl()))
cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) return ToContext.getQualifiedTemplateName(
return ToContext.getQualifiedTemplateName(Qualifier, *QualifierOrErr, QTN->hasTemplateKeyword(),
QTN->hasTemplateKeyword(), cast<TemplateDecl>(*ToTemplateOrErr));
ToTemplate); else
return ToTemplateOrErr.takeError();
return {};
} }
case TemplateName::DependentTemplate: { case TemplateName::DependentTemplate: {
DependentTemplateName *DTN = From.getAsDependentTemplateName(); DependentTemplateName *DTN = From.getAsDependentTemplateName();
NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); auto QualifierOrErr = Import_New(DTN->getQualifier());
if (!Qualifier) if (!QualifierOrErr)
return {}; return QualifierOrErr.takeError();
if (DTN->isIdentifier()) { if (DTN->isIdentifier()) {
return ToContext.getDependentTemplateName(Qualifier, return ToContext.getDependentTemplateName(*QualifierOrErr,
Import(DTN->getIdentifier())); Import(DTN->getIdentifier()));
} }
return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); return ToContext.getDependentTemplateName(*QualifierOrErr,
DTN->getOperator());
} }
case TemplateName::SubstTemplateTemplateParm: { case TemplateName::SubstTemplateTemplateParm: {
SubstTemplateTemplateParmStorage *subst SubstTemplateTemplateParmStorage *Subst =
= From.getAsSubstTemplateTemplateParm(); From.getAsSubstTemplateTemplateParm();
auto *param = ExpectedDecl ParamOrErr = Import_New(Subst->getParameter());
cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); if (!ParamOrErr)
if (!param) return ParamOrErr.takeError();
return {};
TemplateName replacement = Import(subst->getReplacement()); auto ReplacementOrErr = Import_New(Subst->getReplacement());
if (replacement.isNull()) if (!ReplacementOrErr)
return {}; return ReplacementOrErr.takeError();
return ToContext.getSubstTemplateTemplateParm(param, replacement); return ToContext.getSubstTemplateTemplateParm(
cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr);
} }
case TemplateName::SubstTemplateTemplateParmPack: { case TemplateName::SubstTemplateTemplateParmPack: {
SubstTemplateTemplateParmPackStorage *SubstPack SubstTemplateTemplateParmPackStorage *SubstPack
= From.getAsSubstTemplateTemplateParmPack(); = From.getAsSubstTemplateTemplateParmPack();
auto *Param = ExpectedDecl ParamOrErr = Import_New(SubstPack->getParameterPack());
cast_or_null<TemplateTemplateParmDecl>( if (!ParamOrErr)
Import(SubstPack->getParameterPack())); return ParamOrErr.takeError();
if (!Param)
return {};
ASTNodeImporter Importer(*this); ASTNodeImporter Importer(*this);
Expected<TemplateArgument> ArgPack auto ArgPackOrErr =
= Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); Importer.ImportTemplateArgument(SubstPack->getArgumentPack());
if (!ArgPack) { if (!ArgPackOrErr)
llvm::consumeError(ArgPack.takeError()); return ArgPackOrErr.takeError();
return {};
}
return ToContext.getSubstTemplateTemplateParmPack(Param, *ArgPack); return ToContext.getSubstTemplateTemplateParmPack(
cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr);
} }
} }
llvm_unreachable("Invalid template name kind"); llvm_unreachable("Invalid template name kind");
} }
TemplateName ASTImporter::Import(TemplateName From) {
llvm::Expected<TemplateName> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return {};
}
Expected<SourceLocation> ASTImporter::Import_New(SourceLocation FromLoc) { Expected<SourceLocation> ASTImporter::Import_New(SourceLocation FromLoc) {
SourceLocation ToLoc = Import(FromLoc);
if (ToLoc.isInvalid() && !FromLoc.isInvalid())
return llvm::make_error<ImportError>();
return ToLoc;
}
SourceLocation ASTImporter::Import(SourceLocation FromLoc) {
if (FromLoc.isInvalid()) if (FromLoc.isInvalid())
return {}; return SourceLocation{};
SourceManager &FromSM = FromContext.getSourceManager(); SourceManager &FromSM = FromContext.getSourceManager();
std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc);
FileID ToFileID = Import(Decomposed.first); Expected<FileID> ToFileIDOrErr = Import_New(Decomposed.first);
if (ToFileID.isInvalid()) if (!ToFileIDOrErr)
return {}; return ToFileIDOrErr.takeError();
SourceManager &ToSM = ToContext.getSourceManager(); SourceManager &ToSM = ToContext.getSourceManager();
return ToSM.getComposedLoc(ToFileID, Decomposed.second); return ToSM.getComposedLoc(*ToFileIDOrErr, Decomposed.second);
}
SourceLocation ASTImporter::Import(SourceLocation From) {
llvm::Expected<SourceLocation> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return {};
} }
Expected<SourceRange> ASTImporter::Import_New(SourceRange FromRange) { Expected<SourceRange> ASTImporter::Import_New(SourceRange FromRange) {
SourceRange ToRange = Import(FromRange); SourceLocation ToBegin, ToEnd;
return ToRange; if (Error Err = importInto(ToBegin, FromRange.getBegin()))
return std::move(Err);
if (Error Err = importInto(ToEnd, FromRange.getEnd()))
return std::move(Err);
return SourceRange(ToBegin, ToEnd);
} }
SourceRange ASTImporter::Import(SourceRange FromRange) { SourceRange ASTImporter::Import(SourceRange From) {
return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); llvm::Expected<SourceRange> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return {};
} }
Expected<FileID> ASTImporter::Import_New(FileID FromID) { Expected<FileID> ASTImporter::Import_New(FileID FromID) {
FileID ToID = Import(FromID);
if (ToID.isInvalid() && FromID.isValid())
return llvm::make_error<ImportError>();
return ToID;
}
FileID ASTImporter::Import(FileID FromID) {
llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID); llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID);
if (Pos != ImportedFileIDs.end()) if (Pos != ImportedFileIDs.end())
return Pos->second; return Pos->second;
@ -8185,21 +8196,29 @@ FileID ASTImporter::Import(FileID FromID) {
FileID ToID; FileID ToID;
if (FromSLoc.isExpansion()) { if (FromSLoc.isExpansion()) {
const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion(); const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion();
SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc()); ExpectedSLoc ToSpLoc = Import_New(FromEx.getSpellingLoc());
SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart()); if (!ToSpLoc)
return ToSpLoc.takeError();
ExpectedSLoc ToExLocS = Import_New(FromEx.getExpansionLocStart());
if (!ToExLocS)
return ToExLocS.takeError();
unsigned TokenLen = FromSM.getFileIDSize(FromID); unsigned TokenLen = FromSM.getFileIDSize(FromID);
SourceLocation MLoc; SourceLocation MLoc;
if (FromEx.isMacroArgExpansion()) { if (FromEx.isMacroArgExpansion()) {
MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen); MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen);
} else { } else {
SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd()); if (ExpectedSLoc ToExLocE = Import_New(FromEx.getExpansionLocEnd()))
MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen, MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen,
FromEx.isExpansionTokenRange()); FromEx.isExpansionTokenRange());
else
return ToExLocE.takeError();
} }
ToID = ToSM.getFileID(MLoc); ToID = ToSM.getFileID(MLoc);
} else { } else {
// Include location of this file. // Include location of this file.
SourceLocation ToIncludeLoc = Import(FromSLoc.getFile().getIncludeLoc()); ExpectedSLoc ToIncludeLoc = Import_New(FromSLoc.getFile().getIncludeLoc());
if (!ToIncludeLoc)
return ToIncludeLoc.takeError();
const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache(); const SrcMgr::ContentCache *Cache = FromSLoc.getFile().getContentCache();
if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { if (Cache->OrigEntry && Cache->OrigEntry->getDir()) {
@ -8210,8 +8229,9 @@ FileID ASTImporter::Import(FileID FromID) {
const FileEntry *Entry = const FileEntry *Entry =
ToFileManager.getFile(Cache->OrigEntry->getName()); ToFileManager.getFile(Cache->OrigEntry->getName());
if (!Entry) if (!Entry)
return {}; // FIXME: Use a new error kind?
ToID = ToSM.createFileID(Entry, ToIncludeLoc, return llvm::make_error<ImportError>(ImportError::Unknown);
ToID = ToSM.createFileID(Entry, *ToIncludeLoc,
FromSLoc.getFile().getFileCharacteristic()); FromSLoc.getFile().getFileCharacteristic());
} else { } else {
// FIXME: We want to re-use the existing MemoryBuffer! // FIXME: We want to re-use the existing MemoryBuffer!
@ -8228,80 +8248,117 @@ FileID ASTImporter::Import(FileID FromID) {
ImportedFileIDs[FromID] = ToID; ImportedFileIDs[FromID] = ToID;
return ToID; return ToID;
} }
FileID ASTImporter::Import(FileID From) {
llvm::Expected<FileID> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return {};
}
Expected<CXXCtorInitializer *> Expected<CXXCtorInitializer *>
ASTImporter::Import_New(CXXCtorInitializer *From) { ASTImporter::Import_New(CXXCtorInitializer *From) {
CXXCtorInitializer *To = Import(From); ExpectedExpr ToExprOrErr = Import_New(From->getInit());
if (!To && From) if (!ToExprOrErr)
return llvm::make_error<ImportError>(); return ToExprOrErr.takeError();
return To;
} auto LParenLocOrErr = Import_New(From->getLParenLoc());
CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) { if (!LParenLocOrErr)
Expr *ToExpr = Import(From->getInit()); return LParenLocOrErr.takeError();
if (!ToExpr && From->getInit())
return nullptr; auto RParenLocOrErr = Import_New(From->getRParenLoc());
if (!RParenLocOrErr)
return RParenLocOrErr.takeError();
if (From->isBaseInitializer()) { if (From->isBaseInitializer()) {
TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
if (!ToTInfo && From->getTypeSourceInfo()) if (!ToTInfoOrErr)
return nullptr; return ToTInfoOrErr.takeError();
SourceLocation EllipsisLoc;
if (From->isPackExpansion())
if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc()))
return std::move(Err);
return new (ToContext) CXXCtorInitializer( return new (ToContext) CXXCtorInitializer(
ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()), ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr,
ToExpr, Import(From->getRParenLoc()), *ToExprOrErr, *RParenLocOrErr, EllipsisLoc);
From->isPackExpansion() ? Import(From->getEllipsisLoc())
: SourceLocation());
} else if (From->isMemberInitializer()) { } else if (From->isMemberInitializer()) {
auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember())); ExpectedDecl ToFieldOrErr = Import_New(From->getMember());
if (!ToField && From->getMember()) if (!ToFieldOrErr)
return nullptr; return ToFieldOrErr.takeError();
auto MemberLocOrErr = Import_New(From->getMemberLocation());
if (!MemberLocOrErr)
return MemberLocOrErr.takeError();
return new (ToContext) CXXCtorInitializer( return new (ToContext) CXXCtorInitializer(
ToContext, ToField, Import(From->getMemberLocation()), ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr,
Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
} else if (From->isIndirectMemberInitializer()) { } else if (From->isIndirectMemberInitializer()) {
auto *ToIField = cast_or_null<IndirectFieldDecl>( ExpectedDecl ToIFieldOrErr = Import_New(From->getIndirectMember());
Import(From->getIndirectMember())); if (!ToIFieldOrErr)
if (!ToIField && From->getIndirectMember()) return ToIFieldOrErr.takeError();
return nullptr;
auto MemberLocOrErr = Import_New(From->getMemberLocation());
if (!MemberLocOrErr)
return MemberLocOrErr.takeError();
return new (ToContext) CXXCtorInitializer( return new (ToContext) CXXCtorInitializer(
ToContext, ToIField, Import(From->getMemberLocation()), ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr),
Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr);
} else if (From->isDelegatingInitializer()) { } else if (From->isDelegatingInitializer()) {
TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo());
if (!ToTInfo && From->getTypeSourceInfo()) if (!ToTInfoOrErr)
return nullptr; return ToTInfoOrErr.takeError();
return new (ToContext) return new (ToContext)
CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()), CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr,
ToExpr, Import(From->getRParenLoc())); *ToExprOrErr, *RParenLocOrErr);
} else { } else {
return nullptr; // FIXME: assert?
return make_error<ImportError>();
} }
} }
CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) {
llvm::Expected<CXXCtorInitializer *> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return nullptr;
}
Expected<CXXBaseSpecifier *> Expected<CXXBaseSpecifier *>
ASTImporter::Import_New(const CXXBaseSpecifier *From) { ASTImporter::Import_New(const CXXBaseSpecifier *BaseSpec) {
CXXBaseSpecifier *To = Import(From);
if (!To && From)
return llvm::make_error<ImportError>();
return To;
}
CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) {
auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec); auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec);
if (Pos != ImportedCXXBaseSpecifiers.end()) if (Pos != ImportedCXXBaseSpecifiers.end())
return Pos->second; return Pos->second;
Expected<SourceRange> ToSourceRange = Import_New(BaseSpec->getSourceRange());
if (!ToSourceRange)
return ToSourceRange.takeError();
Expected<TypeSourceInfo *> ToTSI = Import_New(BaseSpec->getTypeSourceInfo());
if (!ToTSI)
return ToTSI.takeError();
ExpectedSLoc ToEllipsisLoc = Import_New(BaseSpec->getEllipsisLoc());
if (!ToEllipsisLoc)
return ToEllipsisLoc.takeError();
CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier( CXXBaseSpecifier *Imported = new (ToContext) CXXBaseSpecifier(
Import(BaseSpec->getSourceRange()), *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(),
BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc);
BaseSpec->getAccessSpecifierAsWritten(),
Import(BaseSpec->getTypeSourceInfo()),
Import(BaseSpec->getEllipsisLoc()));
ImportedCXXBaseSpecifiers[BaseSpec] = Imported; ImportedCXXBaseSpecifiers[BaseSpec] = Imported;
return Imported; return Imported;
} }
CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *From) {
llvm::Expected<CXXBaseSpecifier *> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return nullptr;
}
Error ASTImporter::ImportDefinition_New(Decl *From) { Error ASTImporter::ImportDefinition_New(Decl *From) {
Decl *To = Import(From); Decl *To = Import(From);
@ -8354,57 +8411,52 @@ void ASTImporter::ImportDefinition(Decl *From) {
} }
Expected<DeclarationName> ASTImporter::Import_New(DeclarationName FromName) { Expected<DeclarationName> ASTImporter::Import_New(DeclarationName FromName) {
DeclarationName ToName = Import(FromName);
if (!ToName && FromName)
return llvm::make_error<ImportError>();
return ToName;
}
DeclarationName ASTImporter::Import(DeclarationName FromName) {
if (!FromName) if (!FromName)
return {}; return DeclarationName{};
switch (FromName.getNameKind()) { switch (FromName.getNameKind()) {
case DeclarationName::Identifier: case DeclarationName::Identifier:
return Import(FromName.getAsIdentifierInfo()); return DeclarationName(Import(FromName.getAsIdentifierInfo()));
case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCZeroArgSelector:
case DeclarationName::ObjCOneArgSelector: case DeclarationName::ObjCOneArgSelector:
case DeclarationName::ObjCMultiArgSelector: case DeclarationName::ObjCMultiArgSelector:
return Import(FromName.getObjCSelector()); if (auto ToSelOrErr = Import_New(FromName.getObjCSelector()))
return DeclarationName(*ToSelOrErr);
else
return ToSelOrErr.takeError();
case DeclarationName::CXXConstructorName: { case DeclarationName::CXXConstructorName: {
QualType T = Import(FromName.getCXXNameType()); if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
if (T.isNull()) return ToContext.DeclarationNames.getCXXConstructorName(
return {}; ToContext.getCanonicalType(*ToTyOrErr));
else
return ToContext.DeclarationNames.getCXXConstructorName( return ToTyOrErr.takeError();
ToContext.getCanonicalType(T));
} }
case DeclarationName::CXXDestructorName: { case DeclarationName::CXXDestructorName: {
QualType T = Import(FromName.getCXXNameType()); if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
if (T.isNull()) return ToContext.DeclarationNames.getCXXDestructorName(
return {}; ToContext.getCanonicalType(*ToTyOrErr));
else
return ToContext.DeclarationNames.getCXXDestructorName( return ToTyOrErr.takeError();
ToContext.getCanonicalType(T));
} }
case DeclarationName::CXXDeductionGuideName: { case DeclarationName::CXXDeductionGuideName: {
auto *Template = cast_or_null<TemplateDecl>( if (auto ToTemplateOrErr =
Import(FromName.getCXXDeductionGuideTemplate())); Import_New(FromName.getCXXDeductionGuideTemplate()))
if (!Template) return ToContext.DeclarationNames.getCXXDeductionGuideName(
return {}; cast<TemplateDecl>(*ToTemplateOrErr));
return ToContext.DeclarationNames.getCXXDeductionGuideName(Template); else
return ToTemplateOrErr.takeError();
} }
case DeclarationName::CXXConversionFunctionName: { case DeclarationName::CXXConversionFunctionName: {
QualType T = Import(FromName.getCXXNameType()); if (auto ToTyOrErr = Import_New(FromName.getCXXNameType()))
if (T.isNull()) return ToContext.DeclarationNames.getCXXConversionFunctionName(
return {}; ToContext.getCanonicalType(*ToTyOrErr));
else
return ToContext.DeclarationNames.getCXXConversionFunctionName( return ToTyOrErr.takeError();
ToContext.getCanonicalType(T));
} }
case DeclarationName::CXXOperatorName: case DeclarationName::CXXOperatorName:
@ -8413,7 +8465,7 @@ DeclarationName ASTImporter::Import(DeclarationName FromName) {
case DeclarationName::CXXLiteralOperatorName: case DeclarationName::CXXLiteralOperatorName:
return ToContext.DeclarationNames.getCXXLiteralOperatorName( return ToContext.DeclarationNames.getCXXLiteralOperatorName(
Import(FromName.getCXXLiteralIdentifier())); Import(FromName.getCXXLiteralIdentifier()));
case DeclarationName::CXXUsingDirective: case DeclarationName::CXXUsingDirective:
// FIXME: STATICS! // FIXME: STATICS!
@ -8422,6 +8474,14 @@ DeclarationName ASTImporter::Import(DeclarationName FromName) {
llvm_unreachable("Invalid DeclarationName Kind!"); llvm_unreachable("Invalid DeclarationName Kind!");
} }
DeclarationName ASTImporter::Import(DeclarationName From) {
llvm::Expected<DeclarationName> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return {};
}
IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) { IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
if (!FromId) if (!FromId)
@ -8436,14 +8496,8 @@ IdentifierInfo *ASTImporter::Import(const IdentifierInfo *FromId) {
} }
Expected<Selector> ASTImporter::Import_New(Selector FromSel) { Expected<Selector> ASTImporter::Import_New(Selector FromSel) {
Selector ToSel = Import(FromSel);
if (ToSel.isNull() && !FromSel.isNull())
return llvm::make_error<ImportError>();
return ToSel;
}
Selector ASTImporter::Import(Selector FromSel) {
if (FromSel.isNull()) if (FromSel.isNull())
return {}; return Selector{};
SmallVector<IdentifierInfo *, 4> Idents; SmallVector<IdentifierInfo *, 4> Idents;
Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0)));
@ -8459,6 +8513,14 @@ DeclarationName ASTImporter::HandleNameConflict(DeclarationName Name,
unsigned NumDecls) { unsigned NumDecls) {
return Name; return Name;
} }
Selector ASTImporter::Import(Selector From) {
llvm::Expected<Selector> To = Import_New(From);
if (To)
return *To;
else
llvm::consumeError(To.takeError());
return {};
}
DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) { DiagnosticBuilder ASTImporter::ToDiag(SourceLocation Loc, unsigned DiagID) {
if (LastDiagFromFrom) if (LastDiagFromFrom)