Skip to content

[cxx-interop] Instantiate C++ class templates from Swift #33284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
7eabc27
This change makes it possible to instantiate C++ class templates from…
hlopko Oct 7, 2020
c719f9c
Bump SWIFTMODULE_VERSION
hlopko Oct 8, 2020
0a96791
Remove unreachable diagnostics
hlopko Oct 8, 2020
e20c02a
Rename instantiateTemplates
hlopko Oct 8, 2020
2ef9409
Fix tests
hlopko Oct 8, 2020
d6db3b6
Add failing test for defaulted params
hlopko Oct 8, 2020
1298f8b
Add more failing tests
hlopko Oct 8, 2020
0194ace
Cleanup module interface test
hlopko Oct 12, 2020
7feb939
Merge branch 'main' into class_templates
hlopko Oct 27, 2020
53fcea6
Fixes
hlopko Oct 27, 2020
156f2ed
Use getClangTemplateArguments
hlopko Oct 28, 2020
5f2a2a5
Merge branch 'main' into class_templates
hlopko Nov 5, 2020
1d98588
Fix symbol visitibity test
hlopko Nov 5, 2020
712b0d6
Merge branch 'main' into class_templates
hlopko Nov 5, 2020
4cb3724
Dont use String because on mac it will turn to be NSString
hlopko Nov 5, 2020
4126fd9
Add CInt test
hlopko Nov 6, 2020
c75d5e9
Add test
hlopko Nov 6, 2020
2b01aec
Update include/swift/AST/DiagnosticsSema.def
Nov 23, 2020
dda24ec
Update lib/ClangImporter/ClangImporter.cpp
Nov 23, 2020
e79199a
Address comments
hlopko Nov 24, 2020
9f8f8da
Merge branch 'main' into class_templates
hlopko Dec 2, 2020
27b6990
Update test/Interop/Cxx/templates/Inputs/SwiftClassInstantiationModul…
Dec 2, 2020
fd1c618
Address comments
hlopko Dec 2, 2020
51ca547
Merge branch 'class_templates' of github.com:hlopko/swift into class_…
hlopko Dec 3, 2020
0101196
Merge main
hlopko Jan 5, 2021
16493b3
Merge branch 'main' into class_templates
hlopko Jan 18, 2021
fb7974a
Add some header guards
hlopko Jan 19, 2021
1ba9340
Fix bad merge
hlopko Jan 19, 2021
dafab23
Add simple tests for templates with namespaces
hlopko Jan 19, 2021
090f159
WIP
hlopko Jan 21, 2021
c89f07c
WIP
hlopko Jan 22, 2021
b5ecc40
Serialize clang template type before instantiation
hlopko Jan 25, 2021
e3641c1
Cleanup
hlopko Jan 25, 2021
4e5fb25
Polish
hlopko Jan 25, 2021
e9a44d4
Polish
hlopko Jan 25, 2021
71a9065
Add (failing) test for nested types
hlopko Jan 25, 2021
4d09706
Merge branch 'main' into class_templates
hlopko Jan 25, 2021
c082a8c
Fix test
hlopko Jan 25, 2021
3bfdce6
Merge branch 'main' into class_templates
hlopko Jan 26, 2021
22e8eda
Update lib/Serialization/Deserialization.cpp
Jan 26, 2021
435a796
Update test/Interop/Cxx/templates/class-template-instantiation-existi…
Jan 26, 2021
ff7cdc3
Addressing comments.
hlopko Jan 26, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/swift/AST/ClangModuleLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ class ClangModuleLoader : public ModuleLoader {
lookupRelatedEntity(StringRef clangName, ClangTypeKind kind,
StringRef relatedEntityKind,
llvm::function_ref<void(TypeDecl *)> receiver) = 0;
/// Instantiate and import class template.
virtual NominalTypeDecl *
instantiateCXXClassTemplate(clang::ClassTemplateDecl *decl,
ArrayRef<clang::TemplateArgument> arguments) = 0;

/// Lookup identifier for an already imported decl.
virtual Identifier
lookupIdentifier(const clang::IdentifierInfo* declName) = 0;

/// Try to parse the string as a Clang function type.
///
Expand Down
7 changes: 7 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,13 @@ ERROR(unexportable_clang_function_type,none,
"it may use anonymous types or types defined outside of a module",
(Type))

ERROR(cxx_class_instantiation_failed,none,
"clang couldn't instantiate class template",
())
ERROR(cxx_class_instantiation_non_cxx_argument,none,
"only C++ types supported",
())

WARNING(warn_implementation_only_conflict,none,
"%0 inconsistently imported as implementation-only",
(Identifier))
Expand Down
7 changes: 7 additions & 0 deletions include/swift/ClangImporter/ClangImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ class ClangImporter final : public ClangModuleLoader {
StringRef relatedEntityKind,
llvm::function_ref<void(TypeDecl *)> receiver) override;

NominalTypeDecl *
instantiateCXXClassTemplate(clang::ClassTemplateDecl *decl,
ArrayRef<clang::TemplateArgument> arguments) override;

Identifier
lookupIdentifier(const clang::IdentifierInfo* declName) override;

/// Just like Decl::getClangNode() except we look through to the 'Code'
/// enum of an error wrapper struct.
ClangNode getEffectiveClangNode(const Decl *decl) const;
Expand Down
30 changes: 30 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2743,6 +2743,36 @@ void ClangImporter::lookupRelatedEntity(
}
}

NominalTypeDecl *
ClangImporter::instantiateCXXClassTemplate(
clang::ClassTemplateDecl *decl,
ArrayRef<clang::TemplateArgument> arguments) {
void *InsertPos = nullptr;
auto *ctsd = decl->findSpecialization(arguments, InsertPos);
if (!ctsd) {
ctsd = clang::ClassTemplateSpecializationDecl::Create(
decl->getASTContext(), decl->getTemplatedDecl()->getTagKind(),
decl->getDeclContext(), decl->getTemplatedDecl()->getBeginLoc(),
decl->getLocation(), decl, arguments, nullptr);
decl->AddSpecialization(ctsd, InsertPos);
}

auto CanonType = decl->getASTContext().getTypeDeclType(ctsd);
assert(isa<clang::RecordType>(CanonType) &&
"type of non-dependent specialization is not a RecordType");

auto *swiftDecl = Impl.importDecl(ctsd, Impl.CurrentVersion);
if (swiftDecl) {
return dyn_cast<NominalTypeDecl>(swiftDecl);
}
return nullptr;
}

Identifier
ClangImporter::lookupIdentifier(const clang::IdentifierInfo* declName) {
return Impl.importIdentifier(declName);
}

void ClangModuleUnit::lookupVisibleDecls(ImportPath::Access accessPath,
VisibleDeclConsumer &consumer,
NLKind lookupKind) const {
Expand Down
38 changes: 27 additions & 11 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3988,6 +3988,10 @@ namespace {
if (correctSwiftName)
markAsVariant(result, *correctSwiftName);

if (decl->isInvalidDecl()) {
Impl.markUnavailable(result, "function marked as invalid by clang");
}

return result;
}

Expand Down Expand Up @@ -4186,19 +4190,31 @@ namespace {
return nullptr;
}

Decl *VisitFunctionTemplateDecl(const clang::FunctionTemplateDecl *decl) {
Decl *VisitClassTemplateDecl(const clang::ClassTemplateDecl *decl) {
Optional<ImportedName> correctSwiftName;
auto importedName =
importFullName(decl->getAsFunction(), correctSwiftName);
if (!importedName)
return nullptr;
// All template parameters must be template type parameters.
if (!llvm::all_of(*decl->getTemplateParameters(), [](auto param) {
return isa<clang::TemplateTypeParmDecl>(param);
}))
auto importedName = importFullName(decl, correctSwiftName);
auto name = importedName.getDeclName().getBaseIdentifier();
if (name.empty())
return nullptr;
return importFunctionDecl(decl->getAsFunction(), importedName,
correctSwiftName, None, decl);
auto loc = Impl.importSourceLoc(decl->getLocation());
auto dc = Impl.importDeclContextOf(
decl, importedName.getEffectiveContext());

SmallVector<GenericTypeParamDecl *, 4> genericParams;
for (auto &param : *decl->getTemplateParameters()) {
auto genericParamDecl = Impl.createDeclWithClangNode<GenericTypeParamDecl>(
param, AccessLevel::Public, dc,
Impl.SwiftContext.getIdentifier(param->getName()),
Impl.importSourceLoc(param->getLocation()),
/*depth*/ 0, /*index*/ genericParams.size());
genericParams.push_back(genericParamDecl);
}
auto genericParamList = GenericParamList::create(
Impl.SwiftContext, loc, genericParams, loc);

auto structDecl = Impl.createDeclWithClangNode<StructDecl>(
decl, AccessLevel::Public, loc, name, loc, None, genericParamList, dc);
return structDecl;
}

Decl *VisitUsingDecl(const clang::UsingDecl *decl) {
Expand Down
34 changes: 34 additions & 0 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
#include "swift/ClangImporter/ClangImporter.h"
#include "swift/Strings.h"
#include "swift/Subsystems.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclTemplate.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallString.h"
Expand Down Expand Up @@ -822,6 +825,37 @@ static Type applyGenericArguments(Type type, TypeResolution resolution,
diags.diagnose(loc, diag::use_of_void_pointer, "").
fixItReplace(generic->getSourceRange(), "UnsafeRawPointer");
}

if (auto clangDecl = decl->getClangDecl()) {
if (auto classTemplateDecl =
dyn_cast<clang::ClassTemplateDecl>(clangDecl)) {
SmallVector<clang::TemplateArgument, 2> templateArguments;
for (auto &argTypeRepr : generic->getGenericArgs()) {
Type argType = resolution.resolveType(argTypeRepr);
auto *clangDecl = argType->getAnyNominal()->getDecl()->getClangDecl();
if (clangDecl) {
auto *tagDecl = cast<clang::TagDecl>(clangDecl);
auto type =
classTemplateDecl->getASTContext().getTagDeclType(tagDecl);
templateArguments.push_back(clang::TemplateArgument(type));
} else {
diags.diagnose(loc, diag::cxx_class_instantiation_non_cxx_argument);
return ErrorType::get(ctx);
}
}

auto *clangModuleLoader = decl->getASTContext().getClangModuleLoader();
auto instantiatedDecl = clangModuleLoader->instantiateCXXClassTemplate(
const_cast<clang::ClassTemplateDecl *>(classTemplateDecl),
templateArguments);
if (instantiatedDecl) {
return instantiatedDecl->getDeclaredInterfaceType();
} else {
diags.diagnose(loc, diag::cxx_class_instantiation_failed);
return ErrorType::get(ctx);
}
}
}
return result;
}

Expand Down
1 change: 1 addition & 0 deletions lib/Serialization/DeclTypeRecordNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ PATTERN(ANY)
PATTERN(TYPED)
PATTERN(VAR)

OTHER(XREF_CLANG_TEMPLATE_INSTANTIATION, 209)
OTHER(PARAMETERLIST, 210)
// 211 is unused
OTHER(FOREIGN_ERROR_CONVENTION, 212)
Expand Down
39 changes: 39 additions & 0 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Statistic.h"
#include "clang/AST/DeclTemplate.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
Expand Down Expand Up @@ -1418,6 +1419,44 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
case XREF_INITIALIZER_PATH_PIECE:
llvm_unreachable("only in a nominal or function");

case XREF_CLANG_TEMPLATE_INSTANTIATION: {
auto &astContext = getContext();
auto clangModuleLoader = astContext.getClangModuleLoader();
auto &clangASTContext = clangModuleLoader->getClangASTContext();

IdentifierID templateId;
ArrayRef<uint64_t> argumentIds;
XRefClangTemplateInstantiationLayout::readRecord(scratch, templateId, argumentIds);

auto templateIdent = getIdentifier(templateId);
SmallVector<ValueDecl*, 4> templateDeclResults;
VectorDeclConsumer templateDeclConsumer(templateDeclResults);
clangModuleLoader->lookupValue(templateIdent, templateDeclConsumer);
assert(templateDeclResults.size() == 1);
auto *templateDecl = templateDeclResults[0];
auto *classTemplateDecl = const_cast<clang::ClassTemplateDecl *>(
dyn_cast<clang::ClassTemplateDecl>(templateDecl->getClangDecl()));
assert(classTemplateDecl);

SmallVector<ValueDecl*, 2> arguments;
VectorDeclConsumer argumentsConsumer(arguments);
unsigned counter = 0;
for (auto id : argumentIds) {
auto argIdent = getIdentifier(id);
clangModuleLoader->lookupValue(argIdent, argumentsConsumer);
assert(arguments.size() == ++counter);
}
SmallVector<clang::TemplateArgument, 2> templateArguments;
for (auto &decl : arguments) {
templateArguments.push_back(
clang::TemplateArgument(clangASTContext.getTagDeclType(
dyn_cast<clang::TagDecl>(decl->getClangDecl()))));
}

auto *instantiation = clangModuleLoader->instantiateCXXClassTemplate(
classTemplateDecl, templateArguments);
return instantiation;
}
default:
// Unknown xref kind.
pathTrace.addUnknown(recordID);
Expand Down
8 changes: 7 additions & 1 deletion lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 584; // builtin protocol conformances
const uint16_t SWIFTMODULE_VERSION_MINOR = 585; // XRefClangTemplateInstantiation added

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down Expand Up @@ -1711,6 +1711,12 @@ namespace decls_block {
BCVBR<5> // index
>;

using XRefClangTemplateInstantiationLayout = BCRecordLayout<
XREF_CLANG_TEMPLATE_INSTANTIATION,
IdentifierIDField, // template name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The template may be in a namespace.

BCArray<IdentifierIDField> // template args
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Template args are not necessarily single identifiers -- the types may be nested in namespaces, may be template specializations themselves, and even can be non-type template parameters.

Is there an existing way to serialize a Clang type?

>;

using SILGenNameDeclAttrLayout = BCRecordLayout<
SILGenName_DECL_ATTR,
BCFixed<1>, // implicit flag
Expand Down
23 changes: 23 additions & 0 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

#include "Serialization.h"
#include "ModuleFormat.h"
#include "SILFormat.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTMangler.h"
Expand Down Expand Up @@ -46,6 +47,7 @@
#include "swift/Demangling/ManglingMacros.h"
#include "swift/Serialization/SerializationOptions.h"
#include "swift/Strings.h"
#include "clang/AST/DeclTemplate.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
Expand Down Expand Up @@ -1840,6 +1842,26 @@ void Serializer::writeCrossReference(const Decl *D) {
return;
}

if (D->hasClangNode()) {
if (auto ctsd = dyn_cast<clang::ClassTemplateSpecializationDecl>(
D->getClangDecl())) {
auto *clangModuleLoader = D->getASTContext().getClangModuleLoader();
abbrCode = DeclTypeAbbrCodes[XRefClangTemplateInstantiationLayout::Code];
SmallVector<IdentifierID, 4> arguments;
for (auto &param : ctsd->getTemplateArgs().asArray()) {
arguments.push_back(
addDeclBaseNameRef(clangModuleLoader->lookupIdentifier(
param.getAsType().getBaseTypeIdentifier())));
}
XRefClangTemplateInstantiationLayout::emitRecord(
Out, ScratchRecord, abbrCode,
addDeclBaseNameRef(
clangModuleLoader->lookupIdentifier(ctsd->getDeclName().getAsIdentifierInfo())),
arguments);
return;
}
}

bool isProtocolExt = D->getDeclContext()->getExtendedProtocolDecl();
if (auto type = dyn_cast<TypeDecl>(D)) {
abbrCode = DeclTypeAbbrCodes[XRefTypePathPieceLayout::Code];
Expand Down Expand Up @@ -4590,6 +4612,7 @@ void Serializer::writeAllDeclsAndTypes() {
registerDeclTypeAbbr<XRefOperatorOrAccessorPathPieceLayout>();
registerDeclTypeAbbr<XRefGenericParamPathPieceLayout>();
registerDeclTypeAbbr<XRefInitializerPathPieceLayout>();
registerDeclTypeAbbr<XRefClangTemplateInstantiationLayout>();

registerDeclTypeAbbr<AbstractProtocolConformanceLayout>();
registerDeclTypeAbbr<NormalProtocolConformanceLayout>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import MagicWrapper

public func makeWrappedMagicNumber() -> MagicWrapper<IntWrapper> {
let t = IntWrapper(value: 42)
return MagicWrapper<IntWrapper>(t: t)
}

public func readWrappedMagicNumber(_ i : inout MagicWrapper<IntWrapper>) -> CInt {
return i.getValuePlusArg(13)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_INSTANTIATION_ERRORS_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_INSTANTIATION_ERRORS_H

template<class T>
struct MagicWrapper {
T t;
int getValuePlusArg(int arg) const { return t.getValue() + arg; }
};

struct IntWrapper {
int value;
int getValue() const { return value; }
};

template<class T>
struct CannotBeInstantianted {
void willFailInstantiating(T t) {
t.doesNotExist();
}
};

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_INSTANTIATION_ERRORS_H
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_NON_TYPE_PARAMETER_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_NON_TYPE_PARAMETER_H

template<class T, auto Size>
template<class T = bool, auto Size = 3>
struct MagicArray {
T t[Size];
};
Expand Down
15 changes: 15 additions & 0 deletions test/Interop/Cxx/templates/Inputs/magic-wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_MAGIC_WRAPPER_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_MAGIC_WRAPPER_H

template<class T>
struct MagicWrapper {
T t;
int getValuePlusArg(int arg) const { return t.getValue() + arg; }
};

struct IntWrapper {
int value;
int getValue() const { return value; }
};

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_MAGIC_WRAPPER_H
2 changes: 2 additions & 0 deletions test/Interop/Cxx/templates/Inputs/mangling.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
template<typename T>
struct MagicWrapper {};

struct IntWrapper {};

typedef MagicWrapper<int> WrappedMagicInt;
typedef MagicWrapper<bool> WrappedMagicBool;

Expand Down
Loading