Skip to content

Commit 6b61818

Browse files
authored
Revert "[cxx-interop] Import typedef-ed template instantiations (#32950)"
This reverts commit 643aa2d.
1 parent 4b8bbf7 commit 6b61818

36 files changed

+8
-732
lines changed

docs/ABI/Mangling.rst

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,40 +1107,3 @@ nominal type descriptor symbol for ``CxxStruct`` while compiling the ``main`` mo
11071107
.. code::
11081108
11091109
sSo9CxxStructVMn // -> nominal type descriptor for __C.CxxStruct
1110-
1111-
Importing C++ class template instantiations
1112-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1113-
1114-
A class template instantiation is imported as a struct named
1115-
``__CxxTemplateInst`` plus Itanium mangled type of the instantiation (see the
1116-
``type`` production in the Itanium specification). Note that Itanium mangling is
1117-
used on all platforms, regardless of the ABI of the C++ toolchain, to ensure
1118-
that the mangled name is a valid Swift type name (this is not the case for MSVC
1119-
mangled names). A prefix with a double underscore (to ensure we have a reserved
1120-
C++ identifier) is added to limit the possibility for conflicts with names of
1121-
user-defined structs. The struct is notionally defined in the ``__C`` module,
1122-
similarly to regular C and C++ structs and classes. Consider the following C++
1123-
module:
1124-
1125-
.. code-block:: c++
1126-
1127-
template<class T>
1128-
struct MagicWrapper {
1129-
T t;
1130-
};
1131-
1132-
struct MagicNumber {};
1133-
1134-
typedef MagicWrapper<MagicNumber> WrappedMagicNumber;
1135-
1136-
``WrappedMagicNumber`` is imported as a typealias for struct
1137-
``__CxxTemplateInst12MagicWrapperI11MagicNumberE``. Interface of the imported
1138-
module looks as follows:
1139-
1140-
.. code-block:: swift
1141-
1142-
struct __CxxTemplateInst12MagicWrapperI11MagicNumberE {
1143-
var t: MagicNumber
1144-
}
1145-
struct MagicNumber {}
1146-
typealias WrappedMagicNumber = __CxxTemplateInst12MagicWrapperI11MagicNumberE

docs/CppInteroperabilityManifesto.md

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ Assumptions:
6767
+ [Function templates: calls with generic type parameters](#function-templates-calls-with-generic-type-parameters)
6868
+ [Function templates: importing as real generic functions](#function-templates-importing-as-real-generic-functions)
6969
+ [Class templates](#class-templates)
70-
+ [Class templates: importing instantiation behind typedef](#class-templates-importing-instantiation-behind-typedef)
7170
+ [Class templates: importing specific specilalizations](#class-templates-importing-specific-specilalizations)
7271
+ [Class templates: using with generic type parameters](#class-templates-using-with-generic-type-parameters)
7372
+ [Class templates: using in generic code through a synthesized protocol](#class-templates-using-in-generic-code-through-a-synthesized-protocol)
@@ -2576,45 +2575,6 @@ We could ignore explicit specializations of function templates, because they
25762575
don't affect the API. Explicit specializations of class templates can
25772576
dramatically change the API of the type.
25782577

2579-
### Class templates: Importing full class template instantiations
2580-
2581-
A class template instantiation could be imported as a struct named
2582-
`__CxxTemplateInst` plus Itanium mangled type of the instantiation (see the
2583-
`type` production in the Itanium specification). Note that Itanium mangling is
2584-
used on all platforms, regardless of the ABI of the C++ toolchain, to ensure
2585-
that the mangled name is a valid Swift type name (this is not the case for MSVC
2586-
mangled names). A prefix with a double underscore (to ensure we have a reserved
2587-
C++ identifier) is added to limit the possibility for conflicts with names of
2588-
user-defined structs. The struct is notionally defined in the `__C` module,
2589-
similarly to regular C and C++ structs and classes. Consider the following C++
2590-
module:
2591-
2592-
```c++
2593-
// C++ header.
2594-
2595-
template<class T>
2596-
struct MagicWrapper {
2597-
T t;
2598-
};
2599-
struct MagicNumber {};
2600-
2601-
typedef MagicWrapper<MagicNumber> WrappedMagicNumber;
2602-
```
2603-
2604-
`WrappedMagicNumber` will be imported as a typealias for a struct
2605-
`__CxxTemplateInst12MagicWrapperI11MagicNumberE`. Interface of the imported
2606-
module will look as follows:
2607-
2608-
```swift
2609-
// C++ header imported to Swift.
2610-
2611-
struct __CxxTemplateInst12MagicWrapperI11MagicNumberE {
2612-
var t: MagicNumber
2613-
}
2614-
struct MagicNumber {}
2615-
typealias WrappedMagicNumber = __CxxTemplateInst12MagicWrapperI11MagicNumberE
2616-
```
2617-
26182578
### Class templates: importing specific specilalizations
26192579

26202580
Just like with calls to C++ function templates, it is easy to compile a use of a
@@ -2792,7 +2752,7 @@ func useConcrete() {
27922752

27932753
### Class templates: importing as real generic structs
27942754

2795-
If we know the complete set of allowed type arguments to a C++ struct
2755+
If we know the complete set of allowed type arguments to a C++ function
27962756
template, we could import it as an actual Swift generic struct. Every method of
27972757
that struct will perform dynamic dispatch based on type parameters. See
27982758
the section about function templates for more details.

include/swift/Strings.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ constexpr static const StringLiteral MANGLING_MODULE_OBJC = "__C";
3838
constexpr static const StringLiteral MANGLING_MODULE_CLANG_IMPORTER =
3939
"__C_Synthesized";
4040

41-
/// The name prefix for C++ template instantiation imported as a Swift struct.
42-
constexpr static const StringLiteral CXX_TEMPLATE_INST_PREFIX =
43-
"__CxxTemplateInst";
44-
4541
constexpr static const StringLiteral SEMANTICS_PROGRAMTERMINATION_POINT =
4642
"programtermination_point";
4743

lib/AST/ASTMangler.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#include "clang/AST/Attr.h"
4040
#include "clang/AST/Decl.h"
4141
#include "clang/AST/DeclObjC.h"
42-
#include "clang/AST/DeclTemplate.h"
4342
#include "clang/AST/Mangle.h"
4443
#include "llvm/ADT/DenseMap.h"
4544
#include "llvm/ADT/SmallString.h"
@@ -2115,7 +2114,6 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
21152114
// Always use Clang names for imported Clang declarations, unless they don't
21162115
// have one.
21172116
auto tryAppendClangName = [this, decl]() -> bool {
2118-
auto *nominal = dyn_cast<NominalTypeDecl>(decl);
21192117
auto namedDecl = getClangDeclForMangling(decl);
21202118
if (!namedDecl)
21212119
return false;
@@ -2128,13 +2126,6 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl) {
21282126
appendIdentifier(interface->getObjCRuntimeNameAsString());
21292127
} else if (UseObjCRuntimeNames && protocol) {
21302128
appendIdentifier(protocol->getObjCRuntimeNameAsString());
2131-
} else if (auto ctsd = dyn_cast<clang::ClassTemplateSpecializationDecl>(namedDecl)) {
2132-
// If this is a `ClassTemplateSpecializationDecl`, it was
2133-
// imported as a Swift decl with `__CxxTemplateInst...` name.
2134-
// `ClassTemplateSpecializationDecl`'s name does not include information about
2135-
// template arguments, and in order to prevent name clashes we use the
2136-
// name of the Swift decl which does include template arguments.
2137-
appendIdentifier(nominal->getName().str());
21382129
} else {
21392130
appendIdentifier(namedDecl->getName());
21402131
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3477,33 +3477,14 @@ namespace {
34773477

34783478
Decl *VisitClassTemplateSpecializationDecl(
34793479
const clang::ClassTemplateSpecializationDecl *decl) {
3480-
// `Sema::isCompleteType` will try to instantiate the class template as a
3481-
// side-effect and we rely on this here. `decl->getDefinition()` can
3482-
// return nullptr before the call to sema and return its definition
3483-
// afterwards.
3484-
if (!Impl.getClangSema().isCompleteType(
3485-
decl->getLocation(),
3486-
Impl.getClangASTContext().getRecordType(decl))) {
3487-
// If we got nullptr definition now it means the type is not complete.
3488-
// We don't import incomplete types.
3489-
return nullptr;
3490-
}
3491-
auto def = dyn_cast<clang::ClassTemplateSpecializationDecl>(
3492-
decl->getDefinition());
3493-
assert(def && "Class template instantiation didn't have definition");
3494-
// FIXME: This will instantiate all members of the specialization (and detect
3495-
// instantiation failures in them), which can be more than is necessary
3496-
// and is more than what Clang does. As a result we reject some C++
3497-
// programs that Clang accepts.
3498-
Impl.getClangSema().InstantiateClassTemplateSpecializationMembers(
3499-
def->getLocation(), def, clang::TSK_ExplicitInstantiationDefinition);
3500-
3501-
return VisitRecordDecl(def);
3480+
// FIXME: We could import specializations, but perhaps only as unnamed
3481+
// structural types.
3482+
return nullptr;
35023483
}
35033484

35043485
Decl *VisitClassTemplatePartialSpecializationDecl(
3505-
const clang::ClassTemplatePartialSpecializationDecl *decl) {
3506-
// Note: partial template specializations are not imported.
3486+
const clang::ClassTemplatePartialSpecializationDecl *decl) {
3487+
// Note: templates are not imported.
35073488
return nullptr;
35083489
}
35093490

lib/ClangImporter/ImportName.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@
3131
#include "swift/Basic/StringExtras.h"
3232
#include "swift/ClangImporter/ClangImporterOptions.h"
3333
#include "swift/Parse/Parser.h"
34-
#include "swift/Strings.h"
3534
#include "clang/AST/ASTContext.h"
3635
#include "clang/AST/DeclCXX.h"
37-
#include "clang/AST/Mangle.h"
3836
#include "clang/Basic/IdentifierTable.h"
3937
#include "clang/Basic/Module.h"
4038
#include "clang/Basic/OperatorKinds.h"
@@ -1700,34 +1698,6 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
17001698
}
17011699
}
17021700

1703-
if (auto classTemplateSpecDecl =
1704-
dyn_cast<clang::ClassTemplateSpecializationDecl>(D)) {
1705-
if (!isa<clang::ClassTemplatePartialSpecializationDecl>(D)) {
1706-
1707-
auto &astContext = classTemplateSpecDecl->getASTContext();
1708-
// Itanium mangler produces valid Swift identifiers, use it to generate a name for
1709-
// this instantiation.
1710-
clang::MangleContext *mangler = clang::ItaniumMangleContext::create(
1711-
astContext, astContext.getDiagnostics());
1712-
llvm::SmallString<128> storage;
1713-
llvm::raw_svector_ostream buffer(storage);
1714-
mangler->mangleTypeName(astContext.getRecordType(classTemplateSpecDecl),
1715-
buffer);
1716-
1717-
// The Itanium mangler does not provide a way to get the mangled
1718-
// representation of a type. Instead, we call mangleTypeName() that
1719-
// returns the name of the RTTI typeinfo symbol, and remove the _ZTS
1720-
// prefix. Then we prepend __CxxTemplateInst to reduce chances of conflict
1721-
// with regular C and C++ structs.
1722-
llvm::SmallString<128> mangledNameStorage;
1723-
llvm::raw_svector_ostream mangledName(mangledNameStorage);
1724-
assert(buffer.str().take_front(4) == "_ZTS");
1725-
mangledName << CXX_TEMPLATE_INST_PREFIX << buffer.str().drop_front(4);
1726-
1727-
baseName = swiftCtx.getIdentifier(mangledName.str()).get();
1728-
}
1729-
}
1730-
17311701
// swift_newtype-ed declarations may have common words with the type name
17321702
// stripped.
17331703
if (auto newtypeDecl = findSwiftNewtype(D, clangSema, version)) {

lib/ClangImporter/SwiftLookupTable.cpp

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,16 +1881,10 @@ void importer::addEntryToLookupTable(SwiftLookupTable &table,
18811881
// struct names when relevant, not just pointer names. That way we can check
18821882
// both CFDatabase.def and the objc_bridge attribute and cover all our bases.
18831883
if (auto *tagDecl = dyn_cast<clang::TagDecl>(named)) {
1884-
// We add entries for ClassTemplateSpecializations that don't have
1885-
// definition. It's possible that the decl will be instantiated by
1886-
// SwiftDeclConverter later on. We cannot force instantiating
1887-
// ClassTemplateSPecializations here because we're currently writing the
1888-
// AST, so we cannot modify it.
1889-
if (!isa<clang::ClassTemplateSpecializationDecl>(named) &&
1890-
!tagDecl->getDefinition()) {
1884+
if (!tagDecl->getDefinition())
18911885
return;
1892-
}
18931886
}
1887+
18941888
// If we have a name to import as, add this entry to the table.
18951889
auto currentVersion =
18961890
ImportNameVersion::fromOptions(nameImporter.getLangOpts());
@@ -2083,19 +2077,6 @@ void SwiftLookupTableWriter::populateTableWithDecl(SwiftLookupTable &table,
20832077

20842078
// Add this entry to the lookup table.
20852079
addEntryToLookupTable(table, named, nameImporter);
2086-
if (auto typedefDecl = dyn_cast<clang::TypedefNameDecl>(named)) {
2087-
if (auto typedefType = dyn_cast<clang::TemplateSpecializationType>(
2088-
typedefDecl->getUnderlyingType())) {
2089-
if (auto CTSD = dyn_cast<clang::ClassTemplateSpecializationDecl>(
2090-
typedefType->getAsTagDecl())) {
2091-
// Adding template instantiation behind typedef as a top-level entry
2092-
// so the instantiation appears in the API.
2093-
assert(!isa<clang::ClassTemplatePartialSpecializationDecl>(CTSD) &&
2094-
"Class template partial specialization cannot appear behind typedef");
2095-
addEntryToLookupTable(table, CTSD, nameImporter);
2096-
}
2097-
}
2098-
}
20992080
}
21002081

21012082
void SwiftLookupTableWriter::populateTable(SwiftLookupTable &table,

test/Interop/Cxx/templates/Inputs/canonical-types.h

Lines changed: 0 additions & 18 deletions
This file was deleted.

test/Interop/Cxx/templates/Inputs/decl-with-definition-including-members.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

test/Interop/Cxx/templates/Inputs/decl-with-definition.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

test/Interop/Cxx/templates/Inputs/decl-with-primitive-argument.h

Lines changed: 0 additions & 12 deletions
This file was deleted.

test/Interop/Cxx/templates/Inputs/decl-without-definition.h

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/Interop/Cxx/templates/Inputs/eager-instantiation-problems.h

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)