[cxx-interop] Import typedef-ed template instantiations (#32950)

In this PR we teach `ClangImporter` to import typedef statements with template instantiation as its underlying type.

```c++
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};

typedef MagicWrapper<MagicNumber> WrappedMagicNumber;
```

will be made available in Swift as if `WrappedMagicNumber` is a regular struct. 

In C++, multiple distinct typedeffed instantiations resolve to the same canonical type. We implement this by creating a hidden intermediate struct that typedef aliasses.

The struct is named as `__CxxTemplateInst` plus Itanium mangled type of the instantiation. For the example above the name of the hidden struct is `__CxxTemplateInst12MagicWrapperI11MagicNumberE`. Double underscore (denoting a reserved C++ identifier) is used to discourage direct usage. We chose Itanium mangling scheme because it produces valid Swift identifiers and covers all C++ edge cases.

Imported module interface of the example above:

```swift
struct __CxxTemplateInst12MagicWrapperI11MagicNumberE {
  var t: MagicNumber
}
struct MagicNumber {}
typealias WrappedMagicNumber = __CxxTemplateInst12MagicWrapperI11MagicNumberE
```

We modified the `SwiftLookupTable` logic to show hidden structs in `swift_ide_test` for convenience.

Resolves https://bugs.swift.org/browse/SR-12591.

Co-authored-by: Rosica Dejanovska <rosica@google.com>
Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
Co-authored-by: Robert Widmann <devteam.codafi@gmail.com>
This commit is contained in:
Marcel Hlopko
2020-08-12 16:54:22 +02:00
committed by GitHub
parent ef89b0dc51
commit 643aa2d896
36 changed files with 732 additions and 8 deletions

View File

@@ -38,6 +38,10 @@ constexpr static const StringLiteral MANGLING_MODULE_OBJC = "__C";
constexpr static const StringLiteral MANGLING_MODULE_CLANG_IMPORTER =
"__C_Synthesized";
/// The name prefix for C++ template instantiation imported as a Swift struct.
constexpr static const StringLiteral CXX_TEMPLATE_INST_PREFIX =
"__CxxTemplateInst";
constexpr static const StringLiteral SEMANTICS_PROGRAMTERMINATION_POINT =
"programtermination_point";