Skip to content

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

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 4 commits into from
Aug 14, 2020

Conversation

hlopko
Copy link
Contributor

@hlopko hlopko commented Aug 13, 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.

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 changed the title Rollforward typedefs [cxx-interop] Import typedef-ed template instantiations #32950 Aug 13, 2020
@hlopko
Copy link
Contributor Author

hlopko commented Aug 13, 2020

@swift-ci please test

@hlopko hlopko requested a review from gribozavr August 13, 2020 13:26
@hlopko hlopko marked this pull request as ready for review August 13, 2020 13:26
@hlopko
Copy link
Contributor Author

hlopko commented Aug 13, 2020

CC @meg-gupta

@gribozavr gribozavr added the c++ interop Feature: Interoperability with C++ label Aug 13, 2020
@hlopko
Copy link
Contributor Author

hlopko commented Aug 13, 2020

@swift-ci please test

1 similar comment
@hlopko
Copy link
Contributor Author

hlopko commented Aug 13, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - b439fdf

@hlopko
Copy link
Contributor Author

hlopko commented Aug 14, 2020

@swift-ci please test

@hlopko
Copy link
Contributor Author

hlopko commented Aug 14, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b439fdf

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - b439fdf

@hlopko
Copy link
Contributor Author

hlopko commented Aug 14, 2020

@swift-ci Please test Windows platform

@hlopko
Copy link
Contributor Author

hlopko commented Aug 14, 2020

@swift-ci please test

@hlopko
Copy link
Contributor Author

hlopko commented Aug 14, 2020

@swift-ci Please test Windows platform

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - f4015a8

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f4015a8

@hlopko
Copy link
Contributor Author

hlopko commented Aug 14, 2020

@swift-ci Please test Windows platform

@hlopko hlopko merged commit cb537b4 into swiftlang:master Aug 14, 2020
@meg-gupta
Copy link
Contributor

@hlopko This is causing an error in https://ci.swift.org/job/oss-swift_tools-R_stdlib-RD_test-simulator/
Can you please take a look ? Please run the preset once you have a fix.

Failing test:
Swift(macosx-x86_64) :: Interop/Cxx/templates/decl-with-definition-irgen.swift

From the log:
/Users/buildnode/jenkins/workspace/oss-swift_tools-R_stdlib-RD_test-simulator/swift/test/Interop/Cxx/templates/decl-with-definition-irgen.swift:12:11: error: CHECK: expected string not found in input
21:03:05 // CHECK: %magicInt = alloca %TSo037__CxxTemplateInst12MagicWrapperI10IntE1EV, align 4
21:03:05 ^
21:03:05 :23:63: note: scanning from here
21:03:05 define swiftcc i32 @"$s4main18getWrappedMagicInts5Int32VyF"() #0 {
21:03:05 ^
21:03:05 :25:2: note: possible intended match here
21:03:05 %0 = alloca %TSo037__CxxTemplateInst12MagicWrapperI10IntE1EV, align 4
21:03:05

@meg-gupta
Copy link
Contributor

Most likely a minor filecheck mismatch. I created #33507 to disable the test

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.

5 participants