balazske updated this revision to Diff 174168. balazske added a comment. - Small style corrections.
Repository: rC Clang https://reviews.llvm.org/D53818 Files: include/clang/AST/ASTImporter.h lib/AST/ASTImporter.cpp
Index: lib/AST/ASTImporter.cpp =================================================================== --- lib/AST/ASTImporter.cpp +++ lib/AST/ASTImporter.cpp @@ -154,25 +154,6 @@ 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>, public DeclVisitor<ASTNodeImporter, ExpectedDecl>, public StmtVisitor<ASTNodeImporter, ExpectedStmt> { @@ -7656,9 +7637,9 @@ ASTImporter::~ASTImporter() = default; -QualType ASTImporter::Import(QualType FromT) { +Expected<QualType> ASTImporter::Import_New(QualType FromT) { if (FromT.isNull()) - return {}; + return QualType{}; const Type *FromTy = FromT.getTypePtr(); @@ -7671,36 +7652,64 @@ // Import the type ASTNodeImporter Importer(*this); ExpectedType ToTOrErr = Importer.Visit(FromTy); - if (!ToTOrErr) { - llvm::consumeError(ToTOrErr.takeError()); - return {}; - } + if (!ToTOrErr) + return ToTOrErr.takeError(); // Record the imported type. ImportedTypes[FromTy] = (*ToTOrErr).getTypePtr(); 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 {}; +} -TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *FromTSI) { +Expected<TypeSourceInfo *> ASTImporter::Import_New(TypeSourceInfo *FromTSI) { if (!FromTSI) return FromTSI; // 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. - QualType T = Import(FromTSI->getType()); - if (T.isNull()) - return nullptr; + ExpectedType TOrErr = Import_New(FromTSI->getType()); + if (!TOrErr) + return TOrErr.takeError(); + ExpectedSLoc BeginLocOrErr = Import_New(FromTSI->getTypeLoc().getBeginLoc()); + if (!BeginLocOrErr) + return BeginLocOrErr.takeError(); - return ToContext.getTrivialTypeSourceInfo( - T, Import(FromTSI->getTypeLoc().getBeginLoc())); + return ToContext.getTrivialTypeSourceInfo(*TOrErr, *BeginLocOrErr); +} +TypeSourceInfo *ASTImporter::Import(TypeSourceInfo *From) { + llvm::Expected<TypeSourceInfo *> To = Import_New(From); + if (To) + return *To; + else + llvm::consumeError(To.takeError()); + return nullptr; } -Attr *ASTImporter::Import(const Attr *FromAttr) { +Expected<Attr *> ASTImporter::Import_New(const Attr *FromAttr) { Attr *ToAttr = FromAttr->clone(ToContext); - ToAttr->setRange(Import(FromAttr->getRange())); + if (auto ToRangeOrErr = Import_New(FromAttr->getRange())) + ToAttr->setRange(*ToRangeOrErr); + else + return ToRangeOrErr.takeError(); + 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) { llvm::DenseMap<Decl *, Decl *>::iterator Pos = ImportedDecls.find(FromD); @@ -7715,7 +7724,7 @@ } } -Decl *ASTImporter::Import(Decl *FromD) { +Expected<Decl *> ASTImporter::Import_New(Decl *FromD) { if (!FromD) return nullptr; @@ -7729,19 +7738,25 @@ return ToD; } - // Import the type. + // Import the declaration. ExpectedDecl ToDOrErr = Importer.Visit(FromD); - if (!ToDOrErr) { - llvm::consumeError(ToDOrErr.takeError()); - return nullptr; - } + if (!ToDOrErr) + return ToDOrErr; ToD = *ToDOrErr; // Notify subclasses. Imported(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) { @@ -7803,29 +7818,35 @@ return ToDC; } -Expr *ASTImporter::Import(Expr *FromE) { - if (!FromE) - return nullptr; - - return cast_or_null<Expr>(Import(cast<Stmt>(FromE))); +Expected<Expr *> ASTImporter::Import_New(Expr *FromE) { + if (ExpectedStmt ToSOrErr = Import_New(cast_or_null<Stmt>(FromE))) + return cast_or_null<Expr>(*ToSOrErr); + else + return ToSOrErr.takeError(); +} +Expr *ASTImporter::Import(Expr *From) { + llvm::Expected<Expr *> To = Import_New(From); + if (To) + return *To; + else + llvm::consumeError(To.takeError()); + return nullptr; } -Stmt *ASTImporter::Import(Stmt *FromS) { +Expected<Stmt *> ASTImporter::Import_New(Stmt *FromS) { if (!FromS) 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); if (Pos != ImportedStmts.end()) return Pos->second; // Import the statement. ASTNodeImporter Importer(*this); ExpectedStmt ToSOrErr = Importer.Visit(FromS); - if (!ToSOrErr) { - llvm::consumeError(ToSOrErr.takeError()); - return nullptr; - } + if (!ToSOrErr) + return ToSOrErr; if (auto *ToE = dyn_cast<Expr>(*ToSOrErr)) { auto *FromE = cast<Expr>(FromS); @@ -7840,65 +7861,84 @@ FromE->containsUnexpandedParameterPack()); } - // Record the imported declaration. + // Record the imported statement object. 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; } -NestedNameSpecifier *ASTImporter::Import(NestedNameSpecifier *FromNNS) { +Expected<NestedNameSpecifier *> +ASTImporter::Import_New(NestedNameSpecifier *FromNNS) { if (!FromNNS) return nullptr; - NestedNameSpecifier *prefix = Import(FromNNS->getPrefix()); + NestedNameSpecifier *Prefix; + if (Error Err = importInto(Prefix, FromNNS->getPrefix())) + return std::move(Err); switch (FromNNS->getKind()) { case NestedNameSpecifier::Identifier: - if (IdentifierInfo *II = Import(FromNNS->getAsIdentifier())) { - return NestedNameSpecifier::Create(ToContext, prefix, II); - } - return nullptr; + assert(FromNNS->getAsIdentifier() && "NNS should contain identifier."); + return NestedNameSpecifier::Create(ToContext, Prefix, + Import(FromNNS->getAsIdentifier())); case NestedNameSpecifier::Namespace: - if (auto *NS = - cast_or_null<NamespaceDecl>(Import(FromNNS->getAsNamespace()))) { - return NestedNameSpecifier::Create(ToContext, prefix, NS); - } - return nullptr; + if (ExpectedDecl NSOrErr = Import_New(FromNNS->getAsNamespace())) { + return NestedNameSpecifier::Create(ToContext, Prefix, + cast<NamespaceDecl>(*NSOrErr)); + } else + return NSOrErr.takeError(); case NestedNameSpecifier::NamespaceAlias: - if (auto *NSAD = - cast_or_null<NamespaceAliasDecl>(Import(FromNNS->getAsNamespaceAlias()))) { - return NestedNameSpecifier::Create(ToContext, prefix, NSAD); - } - return nullptr; + if (ExpectedDecl NSADOrErr = Import_New(FromNNS->getAsNamespaceAlias())) + return NestedNameSpecifier::Create(ToContext, Prefix, + cast<NamespaceAliasDecl>(*NSADOrErr)); + else + return NSADOrErr.takeError(); case NestedNameSpecifier::Global: return NestedNameSpecifier::GlobalSpecifier(ToContext); case NestedNameSpecifier::Super: - if (auto *RD = - cast_or_null<CXXRecordDecl>(Import(FromNNS->getAsRecordDecl()))) { - return NestedNameSpecifier::SuperSpecifier(ToContext, RD); - } - return nullptr; + if (ExpectedDecl RDOrErr = Import_New(FromNNS->getAsRecordDecl())) + return NestedNameSpecifier::SuperSpecifier(ToContext, + cast<CXXRecordDecl>(*RDOrErr)); + else + return RDOrErr.takeError(); case NestedNameSpecifier::TypeSpec: - case NestedNameSpecifier::TypeSpecWithTemplate: { - QualType T = Import(QualType(FromNNS->getAsType(), 0u)); - if (!T.isNull()) { - bool bTemplate = FromNNS->getKind() == - NestedNameSpecifier::TypeSpecWithTemplate; - return NestedNameSpecifier::Create(ToContext, prefix, - bTemplate, T.getTypePtr()); - } + case NestedNameSpecifier::TypeSpecWithTemplate: + if (Expected<QualType> TyOrErr = + Import_New(QualType(FromNNS->getAsType(), 0u))) { + bool TSTemplate = + FromNNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate; + return NestedNameSpecifier::Create(ToContext, Prefix, TSTemplate, + TyOrErr->getTypePtr()); + } else { + return TyOrErr.takeError(); } - return nullptr; } 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; +} -NestedNameSpecifierLoc ASTImporter::Import(NestedNameSpecifierLoc FromNNS) { +Expected<NestedNameSpecifierLoc> +ASTImporter::Import_New(NestedNameSpecifierLoc FromNNS) { // Copied from NestedNameSpecifier mostly. SmallVector<NestedNameSpecifierLoc , 8> NestedNames; NestedNameSpecifierLoc NNS = FromNNS; @@ -7914,170 +7954,209 @@ while (!NestedNames.empty()) { NNS = NestedNames.pop_back_val(); - NestedNameSpecifier *Spec = Import(NNS.getNestedNameSpecifier()); - if (!Spec) - return NestedNameSpecifierLoc(); + NestedNameSpecifier *Spec; + if (Error Err = importInto(Spec, NNS.getNestedNameSpecifier())) + return std::move(Err); 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) { case NestedNameSpecifier::Identifier: - Builder.Extend(getToContext(), - Spec->getAsIdentifier(), - Import(NNS.getLocalBeginLoc()), - Import(NNS.getLocalEndLoc())); + Builder.Extend(getToContext(), Spec->getAsIdentifier(), ToLocalBeginLoc, + ToLocalEndLoc); break; case NestedNameSpecifier::Namespace: - Builder.Extend(getToContext(), - Spec->getAsNamespace(), - Import(NNS.getLocalBeginLoc()), - Import(NNS.getLocalEndLoc())); + Builder.Extend(getToContext(), Spec->getAsNamespace(), ToLocalBeginLoc, + ToLocalEndLoc); break; case NestedNameSpecifier::NamespaceAlias: - Builder.Extend(getToContext(), - Spec->getAsNamespaceAlias(), - Import(NNS.getLocalBeginLoc()), - Import(NNS.getLocalEndLoc())); + Builder.Extend(getToContext(), Spec->getAsNamespaceAlias(), + ToLocalBeginLoc, ToLocalEndLoc); break; case NestedNameSpecifier::TypeSpec: case NestedNameSpecifier::TypeSpecWithTemplate: { TypeSourceInfo *TSI = getToContext().getTrivialTypeSourceInfo( QualType(Spec->getAsType(), 0)); - Builder.Extend(getToContext(), - Import(NNS.getLocalBeginLoc()), - TSI->getTypeLoc(), - Import(NNS.getLocalEndLoc())); + Builder.Extend(getToContext(), ToLocalBeginLoc, TSI->getTypeLoc(), + ToLocalEndLoc); break; } case NestedNameSpecifier::Global: - Builder.MakeGlobal(getToContext(), Import(NNS.getLocalBeginLoc())); + Builder.MakeGlobal(getToContext(), ToLocalBeginLoc); break; case NestedNameSpecifier::Super: { - SourceRange ToRange = Import(NNS.getSourceRange()); - Builder.MakeSuper(getToContext(), - Spec->getAsRecordDecl(), - ToRange.getBegin(), - ToRange.getEnd()); + auto ToSourceRangeOrErr = Import_New(NNS.getSourceRange()); + if (!ToSourceRangeOrErr) + return ToSourceRangeOrErr.takeError(); + + Builder.MakeSuper(getToContext(), Spec->getAsRecordDecl(), + ToSourceRangeOrErr->getBegin(), + ToSourceRangeOrErr->getEnd()); } } } 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 {}; +} -TemplateName ASTImporter::Import(TemplateName From) { +Expected<TemplateName> ASTImporter::Import_New(TemplateName From) { switch (From.getKind()) { case TemplateName::Template: - if (auto *ToTemplate = - cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) - return TemplateName(ToTemplate); - - return {}; + if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl())) + return TemplateName(cast<TemplateDecl>(*ToTemplateOrErr)); + else + return ToTemplateOrErr.takeError(); case TemplateName::OverloadedTemplate: { OverloadedTemplateStorage *FromStorage = From.getAsOverloadedTemplate(); UnresolvedSet<2> ToTemplates; for (auto *I : *FromStorage) { - if (auto *To = cast_or_null<NamedDecl>(Import(I))) - ToTemplates.addDecl(To); + if (auto ToOrErr = Import_New(I)) + ToTemplates.addDecl(cast<NamedDecl>(*ToOrErr)); else - return {}; + return ToOrErr.takeError(); } return ToContext.getOverloadedTemplateName(ToTemplates.begin(), ToTemplates.end()); } case TemplateName::QualifiedTemplate: { QualifiedTemplateName *QTN = From.getAsQualifiedTemplateName(); - NestedNameSpecifier *Qualifier = Import(QTN->getQualifier()); - if (!Qualifier) - return {}; - - if (auto *ToTemplate = - cast_or_null<TemplateDecl>(Import(From.getAsTemplateDecl()))) - return ToContext.getQualifiedTemplateName(Qualifier, - QTN->hasTemplateKeyword(), - ToTemplate); - - return {}; + auto QualifierOrErr = Import_New(QTN->getQualifier()); + if (!QualifierOrErr) + return QualifierOrErr.takeError(); + + if (ExpectedDecl ToTemplateOrErr = Import_New(From.getAsTemplateDecl())) + return ToContext.getQualifiedTemplateName( + *QualifierOrErr, QTN->hasTemplateKeyword(), + cast<TemplateDecl>(*ToTemplateOrErr)); + else + return ToTemplateOrErr.takeError(); } case TemplateName::DependentTemplate: { DependentTemplateName *DTN = From.getAsDependentTemplateName(); - NestedNameSpecifier *Qualifier = Import(DTN->getQualifier()); - if (!Qualifier) - return {}; + auto QualifierOrErr = Import_New(DTN->getQualifier()); + if (!QualifierOrErr) + return QualifierOrErr.takeError(); if (DTN->isIdentifier()) { - return ToContext.getDependentTemplateName(Qualifier, + return ToContext.getDependentTemplateName(*QualifierOrErr, Import(DTN->getIdentifier())); } - return ToContext.getDependentTemplateName(Qualifier, DTN->getOperator()); + return ToContext.getDependentTemplateName(*QualifierOrErr, + DTN->getOperator()); } case TemplateName::SubstTemplateTemplateParm: { - SubstTemplateTemplateParmStorage *subst - = From.getAsSubstTemplateTemplateParm(); - auto *param = - cast_or_null<TemplateTemplateParmDecl>(Import(subst->getParameter())); - if (!param) - return {}; + SubstTemplateTemplateParmStorage *Subst = + From.getAsSubstTemplateTemplateParm(); + ExpectedDecl ParamOrErr = Import_New(Subst->getParameter()); + if (!ParamOrErr) + return ParamOrErr.takeError(); - TemplateName replacement = Import(subst->getReplacement()); - if (replacement.isNull()) - return {}; + auto ReplacementOrErr = Import_New(Subst->getReplacement()); + if (!ReplacementOrErr) + return ReplacementOrErr.takeError(); - return ToContext.getSubstTemplateTemplateParm(param, replacement); + return ToContext.getSubstTemplateTemplateParm( + cast<TemplateTemplateParmDecl>(*ParamOrErr), *ReplacementOrErr); } case TemplateName::SubstTemplateTemplateParmPack: { SubstTemplateTemplateParmPackStorage *SubstPack = From.getAsSubstTemplateTemplateParmPack(); - auto *Param = - cast_or_null<TemplateTemplateParmDecl>( - Import(SubstPack->getParameterPack())); - if (!Param) - return {}; + ExpectedDecl ParamOrErr = Import_New(SubstPack->getParameterPack()); + if (!ParamOrErr) + return ParamOrErr.takeError(); ASTNodeImporter Importer(*this); - Expected<TemplateArgument> ArgPack - = Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); - if (!ArgPack) { - llvm::consumeError(ArgPack.takeError()); - return {}; - } + auto ArgPackOrErr = + Importer.ImportTemplateArgument(SubstPack->getArgumentPack()); + if (!ArgPackOrErr) + return ArgPackOrErr.takeError(); - return ToContext.getSubstTemplateTemplateParmPack(Param, *ArgPack); + return ToContext.getSubstTemplateTemplateParmPack( + cast<TemplateTemplateParmDecl>(*ParamOrErr), *ArgPackOrErr); } } 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 {}; +} -SourceLocation ASTImporter::Import(SourceLocation FromLoc) { +Expected<SourceLocation> ASTImporter::Import_New(SourceLocation FromLoc) { if (FromLoc.isInvalid()) - return {}; + return SourceLocation{}; SourceManager &FromSM = FromContext.getSourceManager(); std::pair<FileID, unsigned> Decomposed = FromSM.getDecomposedLoc(FromLoc); - FileID ToFileID = Import(Decomposed.first); - if (ToFileID.isInvalid()) - return {}; + Expected<FileID> ToFileIDOrErr = Import_New(Decomposed.first); + if (!ToFileIDOrErr) + return ToFileIDOrErr.takeError(); 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) { + SourceLocation ToBegin, ToEnd; + if (Error Err = importInto(ToBegin, FromRange.getBegin())) + return std::move(Err); + if (Error Err = importInto(ToEnd, FromRange.getEnd())) + return std::move(Err); -SourceRange ASTImporter::Import(SourceRange FromRange) { - return SourceRange(Import(FromRange.getBegin()), Import(FromRange.getEnd())); + return SourceRange(ToBegin, ToEnd); +} +SourceRange ASTImporter::Import(SourceRange From) { + llvm::Expected<SourceRange> To = Import_New(From); + if (To) + return *To; + else + llvm::consumeError(To.takeError()); + return {}; } -FileID ASTImporter::Import(FileID FromID) { +Expected<FileID> ASTImporter::Import_New(FileID FromID) { llvm::DenseMap<FileID, FileID>::iterator Pos = ImportedFileIDs.find(FromID); if (Pos != ImportedFileIDs.end()) return Pos->second; @@ -8090,21 +8169,29 @@ FileID ToID; if (FromSLoc.isExpansion()) { const SrcMgr::ExpansionInfo &FromEx = FromSLoc.getExpansion(); - SourceLocation ToSpLoc = Import(FromEx.getSpellingLoc()); - SourceLocation ToExLocS = Import(FromEx.getExpansionLocStart()); + ExpectedSLoc ToSpLoc = Import_New(FromEx.getSpellingLoc()); + if (!ToSpLoc) + return ToSpLoc.takeError(); + ExpectedSLoc ToExLocS = Import_New(FromEx.getExpansionLocStart()); + if (!ToExLocS) + return ToExLocS.takeError(); unsigned TokenLen = FromSM.getFileIDSize(FromID); SourceLocation MLoc; if (FromEx.isMacroArgExpansion()) { - MLoc = ToSM.createMacroArgExpansionLoc(ToSpLoc, ToExLocS, TokenLen); + MLoc = ToSM.createMacroArgExpansionLoc(*ToSpLoc, *ToExLocS, TokenLen); } else { - SourceLocation ToExLocE = Import(FromEx.getExpansionLocEnd()); - MLoc = ToSM.createExpansionLoc(ToSpLoc, ToExLocS, ToExLocE, TokenLen, - FromEx.isExpansionTokenRange()); + if (ExpectedSLoc ToExLocE = Import_New(FromEx.getExpansionLocEnd())) + MLoc = ToSM.createExpansionLoc(*ToSpLoc, *ToExLocS, *ToExLocE, TokenLen, + FromEx.isExpansionTokenRange()); + else + return ToExLocE.takeError(); } ToID = ToSM.getFileID(MLoc); } else { // 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(); if (Cache->OrigEntry && Cache->OrigEntry->getDir()) { @@ -8115,8 +8202,9 @@ const FileEntry *Entry = ToFileManager.getFile(Cache->OrigEntry->getName()); if (!Entry) - return {}; - ToID = ToSM.createFileID(Entry, ToIncludeLoc, + // FIXME: Use a new error kind? + return llvm::make_error<ImportError>(ImportError::Unknown); + ToID = ToSM.createFileID(Entry, *ToIncludeLoc, FromSLoc.getFile().getFileCharacteristic()); } else { // FIXME: We want to re-use the existing MemoryBuffer! @@ -8133,66 +8221,116 @@ ImportedFileIDs[FromID] = 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 {}; +} -CXXCtorInitializer *ASTImporter::Import(CXXCtorInitializer *From) { - Expr *ToExpr = Import(From->getInit()); - if (!ToExpr && From->getInit()) - return nullptr; +Expected<CXXCtorInitializer *> ASTImporter::Import_New(CXXCtorInitializer *From) { + ExpectedExpr ToExprOrErr = Import_New(From->getInit()); + if (!ToExprOrErr) + return ToExprOrErr.takeError(); + + auto LParenLocOrErr = Import_New(From->getLParenLoc()); + if (!LParenLocOrErr) + return LParenLocOrErr.takeError(); + + auto RParenLocOrErr = Import_New(From->getRParenLoc()); + if (!RParenLocOrErr) + return RParenLocOrErr.takeError(); if (From->isBaseInitializer()) { - TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); - if (!ToTInfo && From->getTypeSourceInfo()) - return nullptr; + auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo()); + if (!ToTInfoOrErr) + return ToTInfoOrErr.takeError(); + + SourceLocation EllipsisLoc; + if (From->isPackExpansion()) + if (Error Err = importInto(EllipsisLoc, From->getEllipsisLoc())) + return std::move(Err); return new (ToContext) CXXCtorInitializer( - ToContext, ToTInfo, From->isBaseVirtual(), Import(From->getLParenLoc()), - ToExpr, Import(From->getRParenLoc()), - From->isPackExpansion() ? Import(From->getEllipsisLoc()) - : SourceLocation()); + ToContext, *ToTInfoOrErr, From->isBaseVirtual(), *LParenLocOrErr, + *ToExprOrErr, *RParenLocOrErr, EllipsisLoc); } else if (From->isMemberInitializer()) { - auto *ToField = cast_or_null<FieldDecl>(Import(From->getMember())); - if (!ToField && From->getMember()) - return nullptr; + ExpectedDecl ToFieldOrErr = Import_New(From->getMember()); + if (!ToFieldOrErr) + return ToFieldOrErr.takeError(); + + auto MemberLocOrErr = Import_New(From->getMemberLocation()); + if (!MemberLocOrErr) + return MemberLocOrErr.takeError(); return new (ToContext) CXXCtorInitializer( - ToContext, ToField, Import(From->getMemberLocation()), - Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); + ToContext, cast_or_null<FieldDecl>(*ToFieldOrErr), *MemberLocOrErr, + *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); } else if (From->isIndirectMemberInitializer()) { - auto *ToIField = cast_or_null<IndirectFieldDecl>( - Import(From->getIndirectMember())); - if (!ToIField && From->getIndirectMember()) - return nullptr; + ExpectedDecl ToIFieldOrErr = Import_New(From->getIndirectMember()); + if (!ToIFieldOrErr) + return ToIFieldOrErr.takeError(); + + auto MemberLocOrErr = Import_New(From->getMemberLocation()); + if (!MemberLocOrErr) + return MemberLocOrErr.takeError(); return new (ToContext) CXXCtorInitializer( - ToContext, ToIField, Import(From->getMemberLocation()), - Import(From->getLParenLoc()), ToExpr, Import(From->getRParenLoc())); + ToContext, cast_or_null<IndirectFieldDecl>(*ToIFieldOrErr), + *MemberLocOrErr, *LParenLocOrErr, *ToExprOrErr, *RParenLocOrErr); } else if (From->isDelegatingInitializer()) { - TypeSourceInfo *ToTInfo = Import(From->getTypeSourceInfo()); - if (!ToTInfo && From->getTypeSourceInfo()) - return nullptr; + auto ToTInfoOrErr = Import_New(From->getTypeSourceInfo()); + if (!ToTInfoOrErr) + return ToTInfoOrErr.takeError(); return new (ToContext) - CXXCtorInitializer(ToContext, ToTInfo, Import(From->getLParenLoc()), - ToExpr, Import(From->getRParenLoc())); + CXXCtorInitializer(ToContext, *ToTInfoOrErr, *LParenLocOrErr, + *ToExprOrErr, *RParenLocOrErr); } 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; +} -CXXBaseSpecifier *ASTImporter::Import(const CXXBaseSpecifier *BaseSpec) { +Expected<CXXBaseSpecifier *> +ASTImporter::Import_New(const CXXBaseSpecifier *BaseSpec) { auto Pos = ImportedCXXBaseSpecifiers.find(BaseSpec); if (Pos != ImportedCXXBaseSpecifiers.end()) 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( - Import(BaseSpec->getSourceRange()), - BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), - BaseSpec->getAccessSpecifierAsWritten(), - Import(BaseSpec->getTypeSourceInfo()), - Import(BaseSpec->getEllipsisLoc())); + *ToSourceRange, BaseSpec->isVirtual(), BaseSpec->isBaseOfClass(), + BaseSpec->getAccessSpecifierAsWritten(), *ToTSI, *ToEllipsisLoc); ImportedCXXBaseSpecifiers[BaseSpec] = 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) { Decl *To = Import(From); @@ -8244,69 +8382,78 @@ llvm::consumeError(std::move(Err)); } -DeclarationName ASTImporter::Import(DeclarationName FromName) { +Expected<DeclarationName> ASTImporter::Import_New(DeclarationName FromName) { if (!FromName) - return {}; + return DeclarationName{}; switch (FromName.getNameKind()) { case DeclarationName::Identifier: - return Import(FromName.getAsIdentifierInfo()); + return DeclarationName(Import(FromName.getAsIdentifierInfo())); case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCOneArgSelector: case DeclarationName::ObjCMultiArgSelector: - return Import(FromName.getObjCSelector()); + if (auto ToSelOrErr = Import_New(FromName.getObjCSelector())) + return DeclarationName(*ToSelOrErr); + else + return ToSelOrErr.takeError(); case DeclarationName::CXXConstructorName: { - QualType T = Import(FromName.getCXXNameType()); - if (T.isNull()) - return {}; - - return ToContext.DeclarationNames.getCXXConstructorName( - ToContext.getCanonicalType(T)); + if (auto ToTyOrErr = Import_New(FromName.getCXXNameType())) + return ToContext.DeclarationNames.getCXXConstructorName( + ToContext.getCanonicalType(*ToTyOrErr)); + else + return ToTyOrErr.takeError(); } case DeclarationName::CXXDestructorName: { - QualType T = Import(FromName.getCXXNameType()); - if (T.isNull()) - return {}; - - return ToContext.DeclarationNames.getCXXDestructorName( - ToContext.getCanonicalType(T)); + if (auto ToTyOrErr = Import_New(FromName.getCXXNameType())) + return ToContext.DeclarationNames.getCXXDestructorName( + ToContext.getCanonicalType(*ToTyOrErr)); + else + return ToTyOrErr.takeError(); } case DeclarationName::CXXDeductionGuideName: { - auto *Template = cast_or_null<TemplateDecl>( - Import(FromName.getCXXDeductionGuideTemplate())); - if (!Template) - return {}; - return ToContext.DeclarationNames.getCXXDeductionGuideName(Template); + if (auto ToTemplateOrErr = + Import_New(FromName.getCXXDeductionGuideTemplate())) + return ToContext.DeclarationNames.getCXXDeductionGuideName( + cast<TemplateDecl>(*ToTemplateOrErr)); + else + return ToTemplateOrErr.takeError(); } case DeclarationName::CXXConversionFunctionName: { - QualType T = Import(FromName.getCXXNameType()); - if (T.isNull()) - return {}; - - return ToContext.DeclarationNames.getCXXConversionFunctionName( - ToContext.getCanonicalType(T)); + if (auto ToTyOrErr = Import_New(FromName.getCXXNameType())) + return ToContext.DeclarationNames.getCXXConversionFunctionName( + ToContext.getCanonicalType(*ToTyOrErr)); + else + return ToTyOrErr.takeError(); } case DeclarationName::CXXOperatorName: return ToContext.DeclarationNames.getCXXOperatorName( FromName.getCXXOverloadedOperator()); case DeclarationName::CXXLiteralOperatorName: return ToContext.DeclarationNames.getCXXLiteralOperatorName( - Import(FromName.getCXXLiteralIdentifier())); + Import(FromName.getCXXLiteralIdentifier())); case DeclarationName::CXXUsingDirective: // FIXME: STATICS! return DeclarationName::getUsingDirectiveName(); } 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) { if (!FromId) @@ -8320,9 +8467,9 @@ return ToId; } -Selector ASTImporter::Import(Selector FromSel) { +Expected<Selector> ASTImporter::Import_New(Selector FromSel) { if (FromSel.isNull()) - return {}; + return Selector{}; SmallVector<IdentifierInfo *, 4> Idents; Idents.push_back(Import(FromSel.getIdentifierInfoForSlot(0))); @@ -8338,6 +8485,14 @@ unsigned NumDecls) { 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) { if (LastDiagFromFrom) Index: include/clang/AST/ASTImporter.h =================================================================== --- include/clang/AST/ASTImporter.h +++ include/clang/AST/ASTImporter.h @@ -154,42 +154,48 @@ /// \return Error information (success or error). template <typename ImportT> LLVM_NODISCARD llvm::Error importInto(ImportT &To, const ImportT &From) { - To = Import(From); - if (From && !To) - return llvm::make_error<ImportError>(); - return llvm::Error::success(); - // FIXME: this should be the final code - //auto ToOrErr = Import(From); - //if (ToOrErr) - // To = *ToOrErr; - //return ToOrErr.takeError(); + auto ToOrErr = Import_New(From); + if (ToOrErr) + To = *ToOrErr; + return ToOrErr.takeError(); } /// Import the given type from the "from" context into the "to" - /// context. + /// context. A null type is imported as a null type (no error). /// - /// \returns the equivalent type in the "to" context, or a NULL type if - /// an error occurred. + /// \returns The equivalent type in the "to" context, or the import error. + llvm::Expected<QualType> Import_New(QualType FromT); + // FIXME: Remove this version. QualType Import(QualType FromT); /// Import the given type source information from the /// "from" context into the "to" context. /// - /// \returns the equivalent type source information in the "to" - /// context, or NULL if an error occurred. + /// \returns The equivalent type source information in the "to" + /// context, or the import error. + llvm::Expected<TypeSourceInfo *> Import_New(TypeSourceInfo *FromTSI); + // FIXME: Remove this version. TypeSourceInfo *Import(TypeSourceInfo *FromTSI); /// Import the given attribute from the "from" context into the /// "to" context. /// - /// \returns the equivalent attribute in the "to" context. + /// \returns The equivalent attribute in the "to" context, or the import + /// error. + llvm::Expected<Attr *> Import_New(const Attr *FromAttr); + // FIXME: Remove this version. Attr *Import(const Attr *FromAttr); /// Import the given declaration from the "from" context into the /// "to" context. /// - /// \returns the equivalent declaration in the "to" context, or a NULL type - /// if an error occurred. + /// \returns The equivalent declaration in the "to" context, or the import + /// error. + llvm::Expected<Decl *> Import_New(Decl *FromD); + llvm::Expected<Decl *> Import_New(const Decl *FromD) { + return Import_New(const_cast<Decl *>(FromD)); + } + // FIXME: Remove this version. Decl *Import(Decl *FromD); Decl *Import(const Decl *FromD) { return Import(const_cast<Decl *>(FromD)); @@ -210,87 +216,113 @@ /// Import the given expression from the "from" context into the /// "to" context. /// - /// \returns the equivalent expression in the "to" context, or NULL if - /// an error occurred. + /// \returns The equivalent expression in the "to" context, or the import + /// error. + llvm::Expected<Expr *> Import_New(Expr *FromE); + // FIXME: Remove this version. Expr *Import(Expr *FromE); /// Import the given statement from the "from" context into the /// "to" context. /// - /// \returns the equivalent statement in the "to" context, or NULL if - /// an error occurred. + /// \returns The equivalent statement in the "to" context, or the import + /// error. + llvm::Expected<Stmt *> Import_New(Stmt *FromS); + // FIXME: Remove this version. Stmt *Import(Stmt *FromS); /// Import the given nested-name-specifier from the "from" /// context into the "to" context. /// - /// \returns the equivalent nested-name-specifier in the "to" - /// context, or NULL if an error occurred. + /// \returns The equivalent nested-name-specifier in the "to" + /// context, or the import error. + llvm::Expected<NestedNameSpecifier *> Import_New(NestedNameSpecifier *FromNNS); + // FIXME: Remove this version. NestedNameSpecifier *Import(NestedNameSpecifier *FromNNS); - /// Import the given nested-name-specifier from the "from" + /// Import the given nested-name-specifier-loc from the "from" /// context into the "to" context. /// - /// \returns the equivalent nested-name-specifier in the "to" - /// context. + /// \returns The equivalent nested-name-specifier-loc in the "to" + /// context, or the import error. + llvm::Expected<NestedNameSpecifierLoc> Import_New(NestedNameSpecifierLoc FromNNS); + // FIXME: Remove this version. NestedNameSpecifierLoc Import(NestedNameSpecifierLoc FromNNS); - /// Import the goven template name from the "from" context into the - /// "to" context. + /// Import the given template name from the "from" context into the + /// "to" context, or the import error. + llvm::Expected<TemplateName> Import_New(TemplateName From); + // FIXME: Remove this version. TemplateName Import(TemplateName From); /// Import the given source location from the "from" context into /// the "to" context. /// - /// \returns the equivalent source location in the "to" context, or an - /// invalid source location if an error occurred. + /// \returns The equivalent source location in the "to" context, or the + /// import error. + llvm::Expected<SourceLocation> Import_New(SourceLocation FromLoc); + // FIXME: Remove this version. SourceLocation Import(SourceLocation FromLoc); /// Import the given source range from the "from" context into /// the "to" context. /// - /// \returns the equivalent source range in the "to" context, or an - /// invalid source location if an error occurred. + /// \returns The equivalent source range in the "to" context, or the import + /// error. + llvm::Expected<SourceRange> Import_New(SourceRange FromRange); + // FIXME: Remove this version. SourceRange Import(SourceRange FromRange); /// Import the given declaration name from the "from" /// context into the "to" context. /// - /// \returns the equivalent declaration name in the "to" context, - /// or an empty declaration name if an error occurred. + /// \returns The equivalent declaration name in the "to" context, or the + /// import error. + llvm::Expected<DeclarationName> Import_New(DeclarationName FromName); + // FIXME: Remove this version. DeclarationName Import(DeclarationName FromName); /// Import the given identifier from the "from" context /// into the "to" context. /// - /// \returns the equivalent identifier in the "to" context. Note: It + /// \returns The equivalent identifier in the "to" context. Note: It /// returns nullptr only if the FromId was nullptr. IdentifierInfo *Import(const IdentifierInfo *FromId); /// Import the given Objective-C selector from the "from" /// context into the "to" context. /// - /// \returns the equivalent selector in the "to" context. + /// \returns The equivalent selector in the "to" context, or the import + /// error. + llvm::Expected<Selector> Import_New(Selector FromSel); + // FIXME: Remove this version. Selector Import(Selector FromSel); /// Import the given file ID from the "from" context into the /// "to" context. /// - /// \returns the equivalent file ID in the source manager of the "to" - /// context. + /// \returns The equivalent file ID in the source manager of the "to" + /// context, or the import error. + llvm::Expected<FileID> Import_New(FileID); + // FIXME: Remove this version. FileID Import(FileID); /// Import the given C++ constructor initializer from the "from" /// context into the "to" context. /// - /// \returns the equivalent initializer in the "to" context. + /// \returns The equivalent initializer in the "to" context, or the import + /// error. + llvm::Expected<CXXCtorInitializer *> Import_New(CXXCtorInitializer *FromInit); + // FIXME: Remove this version. CXXCtorInitializer *Import(CXXCtorInitializer *FromInit); /// Import the given CXXBaseSpecifier from the "from" context into /// the "to" context. /// - /// \returns the equivalent CXXBaseSpecifier in the source manager of the - /// "to" context. + /// \returns The equivalent CXXBaseSpecifier in the source manager of the + /// "to" context, or the import error. + llvm::Expected<CXXBaseSpecifier *> Import_New(const CXXBaseSpecifier *FromSpec); + // FIXME: Remove this version. CXXBaseSpecifier *Import(const CXXBaseSpecifier *FromSpec); /// Import the definition of the given declaration, including all of
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits