Skip to content

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

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 80 commits into from
Aug 12, 2020

Conversation

hlopko
Copy link
Contributor

@hlopko hlopko commented Jul 17, 2020

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

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:

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.

@hlopko hlopko requested review from MForster and gribozavr July 17, 2020 15:24
@hlopko
Copy link
Contributor Author

hlopko commented Jul 17, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 4174cb0

@hlopko
Copy link
Contributor Author

hlopko commented Jul 20, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 4174cb0

Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
@hlopko
Copy link
Contributor Author

hlopko commented Aug 11, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - e7c5172

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - e7c5172

@hlopko
Copy link
Contributor Author

hlopko commented Aug 11, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 3eeaabc

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 3eeaabc

@hlopko
Copy link
Contributor Author

hlopko commented Aug 11, 2020

@swift-ci please test

@hlopko hlopko merged commit 643aa2d into swiftlang:master Aug 12, 2020
@shahmishal
Copy link
Member

We are seeing following test failures:

01:09:52.018 Failing Tests (8):
01:09:52.018     Swift(macosx-x86_64) :: Interop/Cxx/templates/using-directive.swift
01:09:52.018     Swift(macosx-x86_64) :: Interop/Cxx/templates/explicit-specialization.swift
01:09:52.018     Swift(macosx-x86_64) :: Interop/Cxx/templates/eager-instatiation-problems.swift
01:09:52.018     Swift(macosx-x86_64) :: Interop/Cxx/templates/decl-without-definition.swift
01:09:52.018     Swift(macosx-x86_64) :: Interop/Cxx/templates/decl-with-primitive-argument.swift
01:09:52.018     Swift(macosx-x86_64) :: Interop/Cxx/templates/decl-with-definition.swift
01:09:52.018     Swift(macosx-x86_64) :: Interop/Cxx/templates/decl-with-definition-including-members.swift
01:09:52.018     Swift(macosx-x86_64) :: Interop/Cxx/templates/canonical-types.swift
01:09:52.018 

https://ci.swift.org/job/oss-swift-incremental-RA-osx/12557/console

@hlopko
Copy link
Contributor Author

hlopko commented Aug 12, 2020

I'm investigating, but it's getting late here in Europe, so the fix might get submitted tomorrow morning. Feel free to rollback if that's not soon enough.

@hlopko
Copy link
Contributor Author

hlopko commented Aug 12, 2020

This is the error from the log:

error: invalid argument '-std=c++17' not allowed with 'Objective-C'
19:30:23 /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/usr/lib/swift/XPC.swiftmodule/x86_64-apple-macos.swiftinterface:59:31: error: no type named 'xpc_object_t' in module 'XPC'
19:30:23 public var XPC_BOOL_TRUE: XPC.xpc_object_t {
19:30:23                               ^
19:30:23 /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.0.sdk/usr/lib/swift/XPC.swiftmodule/x86_64-apple-macos.swiftinterface:62:32: error: no type named 'xpc_object_t' in module 'XPC'

Building on my mac in the meantime.

@meg-gupta
Copy link
Contributor

@hlopko I created #33429 to revert the PR. Please run the preset when you have the fix.

### Class templates: Importing full class template instantiations

A class template instantiation could be imported as a struct named
`__CxxTemplateInst` plus Itanium mangled type of the instantiation (see the
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of prefixing each declaration name, could we add another special module context like So and SC for ObjC/C synthesized declarations?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes we could. The benefit would be having shorter mangled names and smaller metadata. The downside is slight increase in implementation complexity. For my use cases, I'd definitely appreciate any shortening of mangled names I can get. However, we biased towards reducing implementation complexity.

What's your preference?

hlopko added a commit that referenced this pull request Aug 14, 2020
)

This is a roll-forward of #32950, with explicit c++17 version removed from tests. This is not needed since C++17 is the default anyway.

--

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.

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>
hlopko added a commit to hlopko/swift that referenced this pull request Oct 7, 2020
…2950.

Specifically:

* class template with variadic parameter
* class template with non-type parameter
* class template with template template parameter
hlopko added a commit that referenced this pull request Oct 26, 2020
…3420)

Specifically:

* class template with variadic parameter
* class template with non-type parameter
* class template with template template parameter
@hlopko hlopko deleted the typedefs branch February 22, 2022 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ interop Feature: Interoperability with C++
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants