mirror of
https://github.com/apple/swift.git
synced 2026-10-26 20:15:53 +01:00
This adds support for calling virtual methods of C++ foreign reference types with static dispatch.
In C++, one can do:
```cpp
this->Base::virtualMethod()
```
Swift doesn't allow calling a particular overload of an overridden method, however, it allows calling an overload from the superclass with `super`:
```swift
extension Derived {
public func callSuper() {
return super.foo()
}
}
```
For inherited FRTs, the example above behaved in an unexpected way: the call would still be dispatched dynamically, despite using the syntax reserved for static dispatch. This change makes it behave similarly to pure-Swift classes.
rdar://177619427
40 lines
1.2 KiB
Swift
40 lines
1.2 KiB
Swift
// RUN: %target-swift-emit-silgen %s -I %S/Inputs -cxx-interoperability-mode=default -enable-experimental-feature ForeignReferenceTypeInheritance -disable-availability-checking | %FileCheck %s
|
|
|
|
// REQUIRES: swift_feature_ForeignReferenceTypeInheritance
|
|
|
|
import SuperVirtualDispatch
|
|
|
|
extension Derived {
|
|
public func callSuperVirtual() -> Int32 {
|
|
return super.virtualMethod()
|
|
}
|
|
}
|
|
|
|
extension LeafOverNoOverride {
|
|
public func callSuperVirtualFromLeafOverNoOverride() -> Int32 {
|
|
return super.virtualMethod()
|
|
}
|
|
}
|
|
|
|
extension DerivedWithUnrelatedBase {
|
|
public func callSuperVirtualFromMultiBase() -> Int32 {
|
|
return super.virtualMethod()
|
|
}
|
|
}
|
|
|
|
// CHECK-LABEL: Derived.callSuperVirtual()
|
|
// CHECK-NOT: super_method
|
|
// CHECK-NOT: objc_super_method
|
|
// CHECK-NOT: _ZNK7Derived
|
|
// CHECK-NOT: __synthesizedVirtualCall
|
|
// CHECK: function_ref {{.*}}Base{{.*}}__staticCall_virtualMethod
|
|
// CHECK: end sil function
|
|
|
|
// CHECK-LABEL: LeafOverNoOverride.callSuperVirtualFromLeafOverNoOverride()
|
|
// CHECK-NOT: super_method
|
|
// CHECK-NOT: objc_super_method
|
|
// CHECK-NOT: _ZNK18LeafOverNoOverride
|
|
// CHECK-NOT: __synthesizedVirtualCall
|
|
// CHECK: function_ref {{.*}}Base{{.*}}__staticCall_virtualMethod
|
|
// CHECK: end sil function
|