Commit Graph

179 Commits

Author SHA1 Message Date
zoecarver
5511805ab2 [nfc] Use Int64 type so tests pass on Windows.
It looks like there's another problem converting Int -> Int64 on Windows. This test doesn't touch integer conversions, so I updated the tests to work around it.
2022-02-17 15:54:35 -08:00
zoecarver
5a08b91304 [nfc] Fix silgen test on Windows.
Add the windows mangling.
2022-02-17 13:32:48 -08:00
zoecarver
0d60c2e25d Revert "Revert "[cxx-interop] Start to import dependent types as Any""
This reverts commit fdb417a2e5.
2022-02-17 13:23:14 -08:00
Saleem Abdulrasool
fdb417a2e5 Revert "[cxx-interop] Start to import dependent types as Any" 2022-02-16 20:20:36 -08:00
zoecarver
9ff5bb1f45 [cxx-interop] Add support for dependent types as Any.
Dependent types are going to be very hard to support, especially in complex cases. This PR adds a workaround that people can use: `Any`. This requires manual type casting, but it does work.
2022-02-16 12:44:14 -08:00
zoecarver
e27ea330d4 [nfc] Update regex in test to be unconditional. 2022-02-15 20:12:58 -08:00
zoecarver
a1f9aae63f [cxx-interop] Fix integer substitution on watchOS.
Basically watchOS had the same issue as Windows.
2022-02-15 16:51:14 -08:00
zoecarver
283fc782ec Revert "Revert "[cxx-interop] A few cleanups and fixes for function templates.""
This reverts commit 5de09631ff.
2022-02-15 13:30:10 -08:00
Zoe Carver
5de09631ff Revert "[cxx-interop] A few cleanups and fixes for function templates." 2022-02-13 09:54:39 -08:00
zoecarver
2d4b2eba54 [nfc] [cxx-interop] Enable method tests on windows. 2022-02-12 00:04:40 -08:00
zoecarver
04d4e9dec3 [cxx-interop] Correctly map T& -> inout T.
This is important because the specialized template will be imported as `inout int` (for example) and we must make sure to match the signature.

Updates tests based on this and fixes a few tests that started failing after daceecfc75.
2022-02-04 16:58:31 -08:00
zoecarver
daceecfc75 [cxx-interop] Remove generateSpecializedCXXFunctionTemplate and just import the decl directly.
1) This unifies two code paths that do essentially the same thing.
2) This fixes how we import certain types (such as return types).
2022-02-04 16:18:02 -08:00
zoecarver
c6aee240e6 [cxx-interop] Fix crash when calling method that returns specialization of parent. 2022-02-03 10:47:00 -08:00
Zoe Carver
be39f38ed0 Merge pull request #40867 from zoecarver/interop-fix-linker-errors
[cxx-interop] Fix linker errors when using std-vector.
2022-01-17 16:38:17 -08:00
zoecarver
323e2b16a7 [cxx-interop] Fix linker errors when using std-vector.
Adds tests for using std-vector and some other interesting types.

This patch fixes four mis conceptions that the compiler was previously making:

	1. Implicit destructors have no side effects. (Yes, this means we were not cleaning up some objects.)
	2. Implicit destructors have bodies. (Technically they do, but the body doesn't include CallExprs that they make when lowered to IR.)
	3. Functions other than methods can be uninstantiated templates.
	4. Uninstantiated templates may have executable code. (I.e., we can never take the fast path.)

And makes sure that we visit the destructor of any VarDecl (including parameters).
2022-01-17 11:56:15 -08:00
Saleem Abdulrasool
cb9120d060 tests: enable a couple of C++ interop tests on Windows
These should pass on Windows now, so enable them on the test suite to
prevent regressions in the future.
2022-01-16 20:54:30 -08:00
Robert Widmann
0b4641d1fd Fixup Tests 2022-01-07 12:26:07 -08:00
zoecarver
036361d1e4 [cxx-interop] Add SIL function representation cxx_method; Support extending C++ types.
There are three major changes here:
    1. The addition of "SILFunctionTypeRepresentation::CXXMethod".
    2. C++ methods are imported with their members *last*. Then the arguments are switched when emitting the IR for an application of the function.
    3. Clang decls are now marked as foreign witnesses.

These are all steps towards being able to have C++ protocol conformance.
2022-01-06 14:26:47 -08:00
Omar Habra
35f8461b16 fixing tests for defaults when pointer is imported 2021-11-22 12:10:37 -08:00
Omar Habra
7802b5f726 Adding Tests. 2021-11-20 14:53:22 -08:00
zoecarver
b8e52a7ad2 [cxx-interop] Lazily import members of Clang namespaces and records via requests.
Also adds a ClangImporter request zone and move some requests into it.
2021-10-20 14:52:43 -07:00
Zoe Carver
40422f021b Merge pull request #39664 from zoecarver/lazy-pt9-use-lookup-table
[cxx-interop] Add members to the LookupTable where possible.
2021-10-13 15:29:31 -07:00
zoecarver
eeeb27d66e [cxx-interop] Add members to the LookupTable where possible.
If possible, add imported members to the StructDecl's LookupTable rather than adding them directly as members. This will fix the issues with ordering that #39436 poorly attempted to solve during IRGen.

This also allows us to break out most of the test changes from #39436.
2021-10-13 11:53:58 -07:00
Josh Learn
4758e6545d Fix a failing cxx template import test for Windows 2021-10-08 12:48:10 -07:00
Josh Learn
f433ac2d58 Allow importing templated functions when template args do not appear
in the function signature by adding explicit metatype parameters to
the function signature.
2021-10-06 13:35:12 -07:00
zoecarver
7a42b71bc0 [cxx-interop] Import reference types as inout not UnsafePointer. 2021-09-23 18:10:56 -07:00
zoecarver
d14281f643 [cxx-interop] Allow function templates with defaulted template type parameters to be called.
If a defaulted template type parameter is not used in the function's
signature, don't create a corresponding generic argument for that
template type. This allows us to call function templates with defaulted
template type parameters. This is very common in the standard library
for things like enable_if which is used to disable various
functions/overloads with SFINAE.

The biggest part of this change is going forward not all function
templates will be imported as generic functions in Swift. This should
work OK but we may discover there was some logic which only looked for
generic function when dealing with function templates.
2021-09-22 11:11:16 -07:00
Robert Widmann
6ffd6f5f8f [NFC] Strip Unnecessary %s's out of Tests
%target-typecheck-verify-swift already implies the current file being passed to the frontend - these just lead to duplicate input file errors.
2021-09-20 11:50:37 -07:00
Egor Zhdan
a8f126f7cd C++ Interop: import const methods as non-mutating
This change makes ClangImporter import some C++ member functions as non-mutating, given that they satisfy two requirements:
* the function itself is marked as `const`
* the parent struct doesn't contain any `mutable` members

`get` accessors of subscript operators are now also imported as non-mutating if the C++ `operator[]` satisfies the requirements above.

Fixes SR-12795.
2021-07-25 15:18:33 +03:00
Varun Gandhi
6202dd65ee Merge pull request #36553 from zoecarver/cxx/fix-deep-template-spec
[cxx-interop] Improve performance of deep template instantiations.
2021-03-23 22:44:45 -07:00
zoecarver
3cd6c80268 [cxx-interop] Improve performance of deep template instantiations.
If there are more than 10,000 instantiations of the same class template
then we need to bail because it will take to long to mangle,
instantiate, etc.
2021-03-23 16:58:39 -07:00
zoecarver
41743222a2 [cxx-interop] Don't instantiate empty class template specializations.
If we try to instantiate the static data members of an empty class
template specialization we'll crash because there is no template param
pattern.
2021-03-23 11:25:34 -07:00
zoecarver
58697677a4 [nfc] Fix class-template-uninstantiable-members-irgen for i386.
This is just a matter of changing the regex expression.
2021-03-01 12:41:38 -08:00
zoecarver
b280d26a7b [cxx-interop] Fix class template instantiation.
Lazily instantiate class template members. This means we no longer
reject some programs that clang accepts, such as the following:

```
template<class T> struct Foo { void fail(T value) { value.fail(); } };
using Bar = Foo<int>;
```

The above program will not error so long as `Bar::fail` isn't called.
(Previously, we'd fail to import `Bar`.)
2021-02-24 11:25:45 -08:00
zoecarver
2f0e7fc698 [cxx-interop] Bail on deep template specializations.
If a template specialization is more than 8 types deep, bail.

In future we could make this number (much) greater than 8 but first
we'll need to somehow make instantiating these types much fater.
Currently, I think there is some exponential type behavior happening so
this is super slow.
2021-02-16 10:34:52 -08:00
zoecarver
bd96959d14 [cxx-interop] Re-implement namespaces using enums + extensions.
C++ namespaces are module-independent, but enums are owned by their module's in Swift. So, to prevent declaring two enums with the same name, this patch implements a new approach to namespaces: enums with extensions.

Here's an example:
```
// Module A
namespace N { void test1(); }
// Module B
namespace N { void test2(); }
// __ObjC module
enum N { }
// Swift module A
extension N { func test1() }
// Swift module B
extension N { func test1() }
```

Thanks to @gribozavr for the great idea.
2021-02-14 16:54:24 -08:00
swift-ci
377520a031 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-31 18:52:19 -08:00
zoecarver
02b5118199 [cxx-interop] Correctly import static function templates.
Handle static members the same way we handle other member function
templates. Also, set the "static"ness of the new (specialized) function decl.
2021-01-31 15:49:33 -08:00
swift-ci
7b283ebb33 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-31 15:32:19 -08:00
Zoe Carver
06cd5bcaa0 Merge pull request #35547 from zoecarver/cxx/enable-member-templates-silgen-test
[nfc] Enable member function templates silgen test.
2021-01-31 15:28:39 -08:00
zoecarver
13ff0d63bb [nfc] Enable member function templates silgen test.
This test was dissabled until #30630 (support calling C++ constructors)
landed. That patch landed a while ago, so this test should be enabled.
2021-01-31 12:51:42 -08:00
zoecarver
de0871a654 [nfc] Fix member-templates-silgen.swift after c4da497.
c4da497 (#35416) changed the names of several member templates, this
commit fixes the disabled test to use those new names.
2021-01-31 12:51:41 -08:00
swift-ci
e349794517 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-27 04:12:41 -08:00
Marcel Hlopko
4f5c75a236 [cxx-interop] Instantiate C++ class templates from Swift (#33284)
This PR makes it possible to instantiate C++ class templates from Swift. Given a C++ header:

```c++
// C++ module `ClassTemplates`
template<class T>
struct MagicWrapper {
  T t;
};

struct MagicNumber {};
```

it is now possible to write in Swift:

```swift
import ClassTemplates

func x() -> MagicWrapper<MagicNumber> {
  return MagicWrapper<MagicNumber>()
}
```

This is achieved by importing C++ class templates as generic structs, and then when Swift type checker calls `applyGenericArguments` we detect when the generic struct is backed by the C++ class template and call Clang to instantiate the template. In order to make it possible to put class instantiations such as `MagicWrapper<MagicNumber>` into Swift signatures, we have created a new field in `StructDecl` named `TemplateInstantiationType` where the typechecker stores the `BoundGenericType` which we serialize. Deserializer then notices that the `BoundGenericType` is actually a C++ class template and performs the instantiation logic.

Depends on https://github.com/apple/swift/pull/33420.
Progress towards https://bugs.swift.org/browse/SR-13261.
Fixes https://bugs.swift.org/browse/SR-13775.

Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
Co-authored-by: Rosica Dejanovska <rosica@google.com>
2021-01-27 13:01:20 +01:00
swift-ci
a0145fe002 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-19 22:13:11 -08:00
zoecarver
c4da4975ed [NFC] Cleanup function templates implementation. Address post-commmit review comments from #33053.
Addresses the post-commit review comments from #33053. Just re-naming,
commenting, and some other small cleanups, nothing functionally
different.
2021-01-19 12:02:15 -08:00
swift-ci
bb58f9ff02 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-08 13:12:52 -08:00
Saleem Abdulrasool
e600816d50 ClangImporter: synchronize clang and Swift
This synchronizes the C/C++ standard that Swift uses for the clang
importer with the defaults of the clang compiler.

The default for the C standard remains the same at `gnu11`.  However, if
clang was built with a different default C standard, that will be
preferred.

The default for the C++ standard is moved to C++14.  Although this is a
reduced version, it matches what the current clang compiler defaults to.
Additionally, if the user built clang with a different C++ standard,
that standard would be preferred.

Although the C++ support is a bit more conservative, the idea is that we
can be certain that the clang version fully supports this standard as it
it the default version for clang.

The test change made here replaces the use of `auto` type parameters to
templates which is a C++17 feature.  Reduce the example to a C++14
equivalent.

The clang version seems to behave differently in preventing the
synthesis of the constexpr value.  This persists into the newer clang
releases as well.  This is reasonable as the value does not need to be
exported necessarily and users will be able to materialize the value.
We use the optimizer to ensure that the value is inlined.

This also repairs the C++ interop tests on Windows with a newer C++
runtime from Microsoft.
2021-01-07 14:44:49 -08:00
swift_jenkins
62343a4d44 Merge remote-tracking branch 'origin/main' into next 2021-01-07 11:34:13 -08:00
Saleem Abdulrasool
df9d1e815f Interop/Cxx: explicitly require C++ for modules
The C++ interop modules require C++ support.  Explicitly require C++ as
a feature when building these modules.  This has no impact on the
changes as all the tests enable C++ already.
2021-01-06 16:59:42 -08:00