Skip to content

Commit b5ecc40

Browse files
committed
Serialize clang template type before instantiation
1 parent c89f07c commit b5ecc40

6 files changed

+36
-54
lines changed

lib/Serialization/Deserialization.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,6 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
13011301
importedFromClang, isStatic);
13021302

13031303
DeclBaseName name = getDeclBaseName(IID);
1304-
llvm::errs() << "YOYOYOOOOO " << name <<"\n";
13051304
pathTrace.addValue(name);
13061305
if (privateDiscriminator)
13071306
pathTrace.addValue(getIdentifier(privateDiscriminator));
@@ -5389,16 +5388,12 @@ class TypeDeserializer {
53895388
}
53905389

53915390
if (auto clangDecl = nominal->getClangDecl()) {
5392-
nominal->dump();
53935391
if (auto ctd = dyn_cast<clang::ClassTemplateDecl>(clangDecl)) {
5394-
ctd->dump();
5395-
ctd->dump();
53965392
auto clangImporter = static_cast<ClangImporter *>(nominal->getASTContext().getClangModuleLoader());
5397-
auto genericParams = nominal->getGenericParams();
53985393

53995394
SmallVector<Type, 2> typesOfgenericArgs;
5400-
for (auto arg : *genericParams) {
5401-
typesOfgenericArgs.push_back(arg->getDeclaredInterfaceType());
5395+
for (auto arg : genericArgs) {
5396+
typesOfgenericArgs.push_back(arg);
54025397
}
54035398

54045399
SmallVector<clang::TemplateArgument, 2> templateArguments;

lib/Serialization/Serialization.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@ using namespace llvm::support;
7575
using swift::version::Version;
7676
using llvm::BCBlockRAII;
7777

78+
79+
static Type getResultInterfaceTypeForSerialization(Type t) {
80+
if (!t)
81+
return t;
82+
if (auto nominalDecl = t->getAnyNominal()) {
83+
if (auto structDecl = dyn_cast<StructDecl>(nominalDecl)) {
84+
if (auto templateInstantiationType =
85+
structDecl->getTemplateInstantiationType()) {
86+
return templateInstantiationType;
87+
}
88+
}
89+
}
90+
return t;
91+
}
92+
7893
ASTContext &SerializerBase::getASTContext() {
7994
return M->getASTContext();
8095
}
@@ -628,13 +643,14 @@ DeclID Serializer::addDeclRef(const Decl *D, bool allowTypeAliasXRef) {
628643
}
629644

630645
serialization::TypeID Serializer::addTypeRef(Type ty) {
646+
auto tyToSerialize = getResultInterfaceTypeForSerialization(ty);
631647
#ifndef NDEBUG
632-
PrettyStackTraceType trace(M->getASTContext(), "serializing", ty);
648+
PrettyStackTraceType trace(M->getASTContext(), "serializing", tyToSerialize);
633649
assert(M->getASTContext().LangOpts.AllowModuleWithCompilerErrors ||
634-
!ty || !ty->hasError() && "serializing type with an error");
650+
!tyToSerialize || !ty->hasError() && "serializing type with an error");
635651
#endif
636652

637-
return TypesToSerialize.addRef(ty);
653+
return TypesToSerialize.addRef(tyToSerialize);
638654
}
639655

640656
serialization::ClangTypeID Serializer::addClangTypeRef(const clang::Type *ty) {
@@ -1882,19 +1898,6 @@ void Serializer::writeCrossReference(const Decl *D) {
18821898
return;
18831899
}
18841900

1885-
if (D->hasClangNode()) {
1886-
if (auto ctsd = dyn_cast<clang::ClassTemplateSpecializationDecl>(
1887-
D->getClangDecl())) {
1888-
abbrCode = DeclTypeAbbrCodes[XRefClangTemplateInstantiationLayout::Code];
1889-
// auto clangTypeID = addClangTypeRef(ctsd->getTypeForDecl());
1890-
1891-
// XRefClangTemplateInstantiationLayout::emitRecord(
1892-
// Out, ScratchRecord, abbrCode, clangTypeID);
1893-
1894-
// return;
1895-
}
1896-
}
1897-
18981901
bool isProtocolExt = D->getDeclContext()->getExtendedProtocolDecl();
18991902
if (auto type = dyn_cast<TypeDecl>(D)) {
19001903
abbrCode = DeclTypeAbbrCodes[XRefTypePathPieceLayout::Code];
@@ -2218,13 +2221,6 @@ static void collectDependenciesFromType(llvm::SmallSetVector<Type, 4> &seen,
22182221
return;
22192222
if (contextDependsOn(nominal, excluding))
22202223
return;
2221-
if (auto clangDecl = nominal->getClangDecl()) {
2222-
auto &clangASTContext = nominal->getClangDecl()->getASTContext();
2223-
if (auto ctsd = dyn_cast<clang::ClassTemplateSpecializationDecl>(clangDecl)) {
2224-
seen.insert(dyn_cast<StructDecl>(nominal)->getTemplateInstantiationType());
2225-
return;
2226-
}
2227-
}
22282224
seen.insert(nominal->getDeclaredInterfaceType());
22292225
});
22302226
}
@@ -4688,7 +4684,6 @@ void Serializer::writeAllDeclsAndTypes() {
46884684
registerDeclTypeAbbr<XRefOperatorOrAccessorPathPieceLayout>();
46894685
registerDeclTypeAbbr<XRefGenericParamPathPieceLayout>();
46904686
registerDeclTypeAbbr<XRefInitializerPathPieceLayout>();
4691-
registerDeclTypeAbbr<XRefClangTemplateInstantiationLayout>();
46924687

46934688
registerDeclTypeAbbr<AbstractProtocolConformanceLayout>();
46944689
registerDeclTypeAbbr<NormalProtocolConformanceLayout>();
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import ClassTemplateInNamespace
22

3-
public func receiveShip(_ i: inout Space.Ship<Yolo>) {}
3+
public func receiveShip(_ i: inout Space.Ship<Bool>) {}
44

5-
public func returnShip() -> Space.Ship<Yolo> {
6-
return Space.Ship<Yolo>()
5+
public func returnShip() -> Space.Ship<Bool> {
6+
return Space.Ship<Bool>()
77
}
88

9-
// public func receiveShipWithEngine(_ i: inout Space.Ship<Engine.Turbojet>) {}
9+
public func receiveShipWithEngine(_ i: inout Space.Ship<Engine.Turbojet>) {}
1010

11-
// public func returnShipWithEngine() -> Space.Ship<Engine.Turbojet> {
12-
// return Space.Ship<Engine.Turbojet>()
13-
// }
11+
public func returnShipWithEngine() -> Space.Ship<Engine.Turbojet> {
12+
return Space.Ship<Engine.Turbojet>()
13+
}
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_IN_NAMESPACE_H
22
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_IN_NAMESPACE_H
33

4-
struct Yolo {};
5-
64
namespace Space {
7-
8-
template <class T> struct Ship {T t;};
9-
5+
template <class T> struct Ship { T t; };
106
} // namespace Space
117

128
namespace Engine {
139
struct Turbojet {};
14-
}
10+
} // namespace Engine
1511

1612
#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_CLASS_TEMPLATE_IN_NAMESPACE_H

test/Interop/Cxx/templates/class-template-in-namespace-module-interface.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
// RUN: %target-swift-ide-test -print-module -module-to-print=ClassTemplateInNamespace -I %S/Inputs -source-filename=x -enable-cxx-interop | %FileCheck %s
22

33
// CHECK: enum Space {
4-
// CHECK: struct __CxxTemplateInstN5Space4ShipIiEE {
5-
// CHECK: var t: Int32
6-
// CHECK: init()
7-
// CHECK: init(t: Int32)
8-
// CHECK: }
94
// CHECK: struct Ship<T> {
105
// CHECK: }
11-
// CHECK: typealias Orbiter = Space.__CxxTemplateInstN5Space4ShipIiEE
126
// CHECK: }
137
// CHECK: enum Engine {
148
// CHECK: struct Turbojet {

test/Interop/Cxx/templates/swift-class-instantiation-in-namespace-module-interface.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
// RUN: %target-swift-ide-test -print-module -module-to-print=SwiftClassTemplateInNamespaceModule -I %t/ -source-filename=x -enable-cxx-interop | %FileCheck %s
44

55
// CHECK: import ClassTemplateInNamespace
6-
// CHECK: public func receiveShip(_ i: inout ClassTemplateInNamespace.Space.__CxxTemplateInstN5Space4ShipIbEE)
7-
// CHECK: public func returnShip() -> ClassTemplateInNamespace.Space.__CxxTemplateInstN5Space4ShipIbEE
8-
// CHECK: public func receiveShipWithEngine(_ i: inout ClassTemplateInNamespace.Space.__CxxTemplateInstN5Space4ShipIN6Engine8TurbojetEEE)
9-
// CHECK: public func returnShipWithEngine() -> ClassTemplateInNamespace.Space.__CxxTemplateInstN5Space4ShipIN6Engine8TurbojetEEE
6+
// CHECK: func receiveShip(_ i: inout Space.__CxxTemplateInstN5Space4ShipIbEE)
7+
// CHECK: func receiveShipWithEngine(_ i: inout Space.__CxxTemplateInstN5Space4ShipIN6Engine8TurbojetEEE)
8+
9+
// CHECK: func returnShip() -> Space.__CxxTemplateInstN5Space4ShipIbEE
10+
// CHECK: func returnShipWithEngine() -> Space.__CxxTemplateInstN5Space4ShipIN6Engine8TurbojetEEE
11+

0 commit comments

Comments
 (0)