Author: Gabor Marton Date: 2021-10-01T10:16:11+02:00 New Revision: a3a0b066264fd132a2014edf2aef53072a1fe53a
URL: https://github.com/llvm/llvm-project/commit/a3a0b066264fd132a2014edf2aef53072a1fe53a DIFF: https://github.com/llvm/llvm-project/commit/a3a0b066264fd132a2014edf2aef53072a1fe53a.diff LOG: [clang][ASTImporter] Import InheritedConstructor and ConstructorUsingShadowDecl. Reviewed By: martong Differential Revision: https://reviews.llvm.org/D110395 Added: Modified: clang/include/clang/AST/ASTImporter.h clang/include/clang/ASTMatchers/ASTMatchers.h clang/lib/AST/ASTImporter.cpp clang/unittests/AST/ASTImporterTest.cpp Removed: ################################################################################ diff --git a/clang/include/clang/AST/ASTImporter.h b/clang/include/clang/AST/ASTImporter.h index 17e673a8471a7..c8bdae10a6e6c 100644 --- a/clang/include/clang/AST/ASTImporter.h +++ b/clang/include/clang/AST/ASTImporter.h @@ -379,6 +379,9 @@ class TypeSourceInfo; return Import(const_cast<Decl *>(FromD)); } + llvm::Expected<InheritedConstructor> + Import(const InheritedConstructor &From); + /// Return the copy of the given declaration in the "to" context if /// it has already been imported from the "from" context. Otherwise return /// nullptr. diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 2baa17b59da83..5887263a43e8c 100644 --- a/clang/include/clang/ASTMatchers/ASTMatchers.h +++ b/clang/include/clang/ASTMatchers/ASTMatchers.h @@ -5877,6 +5877,10 @@ AST_MATCHER(CXXMethodDecl, isVirtualAsWritten) { return Node.isVirtualAsWritten(); } +AST_MATCHER(CXXConstructorDecl, isInheritingConstructor) { + return Node.isInheritingConstructor(); +} + /// Matches if the given method or class declaration is final. /// /// Given: diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index a93049b766e74..0dbead08aca4c 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -464,6 +464,9 @@ namespace clang { Error ImportDefaultArgOfParmVarDecl(const ParmVarDecl *FromParam, ParmVarDecl *ToParam); + Expected<InheritedConstructor> + ImportInheritedConstructor(const InheritedConstructor &From); + template <typename T> bool hasSameVisibilityContextAndLinkage(T *Found, T *From); @@ -3479,13 +3482,19 @@ ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { importExplicitSpecifier(Err, FromConstructor->getExplicitSpecifier()); if (Err) return std::move(Err); + auto ToInheritedConstructor = InheritedConstructor(); + if (FromConstructor->isInheritingConstructor()) { + Expected<InheritedConstructor> ImportedInheritedCtor = + import(FromConstructor->getInheritedConstructor()); + if (!ImportedInheritedCtor) + return ImportedInheritedCtor.takeError(); + ToInheritedConstructor = *ImportedInheritedCtor; + } if (GetImportedOrCreateDecl<CXXConstructorDecl>( ToFunction, D, Importer.getToContext(), cast<CXXRecordDecl>(DC), ToInnerLocStart, NameInfo, T, TInfo, ESpec, D->UsesFPIntrin(), D->isInlineSpecified(), D->isImplicit(), D->getConstexprKind(), - InheritedConstructor(), // FIXME: Properly import inherited - // constructor info - TrailingRequiresClause)) + ToInheritedConstructor, TrailingRequiresClause)) return ToFunction; } else if (CXXDestructorDecl *FromDtor = dyn_cast<CXXDestructorDecl>(D)) { @@ -4219,6 +4228,17 @@ Error ASTNodeImporter::ImportDefaultArgOfParmVarDecl( return Error::success(); } +Expected<InheritedConstructor> +ASTNodeImporter::ImportInheritedConstructor(const InheritedConstructor &From) { + Error Err = Error::success(); + CXXConstructorDecl *ToBaseCtor = importChecked(Err, From.getConstructor()); + ConstructorUsingShadowDecl *ToShadow = + importChecked(Err, From.getShadowDecl()); + if (Err) + return std::move(Err); + return InheritedConstructor(ToShadow, ToBaseCtor); +} + ExpectedDecl ASTNodeImporter::VisitParmVarDecl(ParmVarDecl *D) { // Parameters are created in the translation unit's context, then moved // into the function declaration's context afterward. @@ -4758,9 +4778,23 @@ ExpectedDecl ASTNodeImporter::VisitUsingShadowDecl(UsingShadowDecl *D) { return ToTargetOrErr.takeError(); UsingShadowDecl *ToShadow; - if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc, - Name, *ToIntroducerOrErr, *ToTargetOrErr)) - return ToShadow; + if (auto *FromConstructorUsingShadow = + dyn_cast<ConstructorUsingShadowDecl>(D)) { + if (GetImportedOrCreateDecl<ConstructorUsingShadowDecl>( + ToShadow, D, Importer.getToContext(), DC, Loc, + cast<UsingDecl>(*ToIntroducerOrErr), *ToTargetOrErr, + FromConstructorUsingShadow->constructsVirtualBase())) + return ToShadow; + // FIXME import the below members! + // FromConstructorUsingShadow->getNominatedBaseClassShadowDecl(); + // FromConstructorUsingShadow->getConstructedBaseClassShadowDecl(); + // FromConstructorUsingShadow->getNominatedBaseClass(); + // FromConstructorUsingShadow->getConstructedBaseClass(); + } else { + if (GetImportedOrCreateDecl(ToShadow, D, Importer.getToContext(), DC, Loc, + Name, *ToIntroducerOrErr, *ToTargetOrErr)) + return ToShadow; + } ToShadow->setLexicalDeclContext(LexicalDC); ToShadow->setAccess(D->getAccess()); @@ -8761,6 +8795,11 @@ Expected<Decl *> ASTImporter::Import(Decl *FromD) { return ToDOrErr; } +llvm::Expected<InheritedConstructor> +ASTImporter::Import(const InheritedConstructor &From) { + return ASTNodeImporter(*this).ImportInheritedConstructor(From); +} + Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) { if (!FromDC) return FromDC; diff --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp index 66ec873ffb89f..b9641d77bea48 100644 --- a/clang/unittests/AST/ASTImporterTest.cpp +++ b/clang/unittests/AST/ASTImporterTest.cpp @@ -6881,6 +6881,31 @@ TEST_P(ASTImporterOptionSpecificTestBase, ImportEnumMemberSpecialization) { ToD->getTemplateSpecializationKind()); } +TEST_P(ASTImporterOptionSpecificTestBase, ImportIsInheritingConstructorBit) { + Decl *FromTU = getTuDecl( + R"( + class a { + public: + a(int); + }; + struct b : a { + using a::a; // Ihnerited ctor. + }; + void c() { + (b(0)); + } + )", + Lang_CXX11); + auto *FromD = FirstDeclMatcher<CXXConstructorDecl>().match( + FromTU, cxxConstructorDecl(isInheritingConstructor())); + ASSERT_TRUE(FromD); + ASSERT_TRUE(FromD->isInheritingConstructor()); + + auto *ToD = Import(FromD, Lang_CXX11); + ASSERT_TRUE(ToD); + EXPECT_TRUE(ToD->isInheritingConstructor()); +} + INSTANTIATE_TEST_SUITE_P(ParameterizedTests, ASTImporterLookupTableTest, DefaultTestValuesForRunOptions); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits