Skip to content

Commit 3445b86

Browse files
committed
[SILGen] Switch @_extern(c) over to a foreign entrypoint
Rather than treating `@_extern(c)` functions like a Swift function with a C calling convention, treat them as foreign entrypoints, which better matches how we handle imported C functions.
1 parent 39a36d9 commit 3445b86

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ bool swift::requiresForeignEntryPoint(ValueDecl *vd) {
111111
if (vd->hasClangNode())
112112
return true;
113113

114+
if (ExternAttr::find(vd->getAttrs(), ExternKind::C))
115+
return true;
116+
114117
if (auto *accessor = dyn_cast<AccessorDecl>(vd)) {
115118
// Property accessors should be generated alongside the property.
116119
if (accessor->isGetterOrSetter()) {

lib/SIL/IR/SILFunctionType.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4474,8 +4474,6 @@ TypeConverter::getDeclRefRepresentation(SILDeclRef c) {
44744474
case SILDeclRef::Kind::Func:
44754475
if (c.getDecl()->getDeclContext()->isTypeContext())
44764476
return SILFunctionTypeRepresentation::Method;
4477-
if (ExternAttr::find(c.getDecl()->getAttrs(), ExternKind::C))
4478-
return SILFunctionTypeRepresentation::CFunctionPointer;
44794477
return SILFunctionTypeRepresentation::Thin;
44804478

44814479
case SILDeclRef::Kind::Destroyer:

test/SILGen/extern_c.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,44 @@
66
@_extern(c, "my_c_name")
77
func withCName(_ x: Int) -> Int
88

9-
// CHECK-DAG: sil hidden_external [asmname "take_c_func_ptr"] @$s8extern_c12takeCFuncPtryyS2iXCF : $@convention(c) (@convention(c) (Int) -> Int) -> ()
9+
// CHECK-DAG: sil hidden_external [asmname "take_c_func_ptr"] @$s8extern_c12takeCFuncPtryyS2iXCFTo : $@convention(c) (@convention(c) (Int) -> Int) -> ()
1010
@_extern(c, "take_c_func_ptr")
1111
func takeCFuncPtr(_ f: @convention(c) (Int) -> Int)
1212

13-
// CHECK-DAG: sil [asmname "public_visible"] @$s8extern_c16publicVisibilityyS2iF : $@convention(c) (Int) -> Int
13+
// CHECK-DAG: sil [serialized] [asmname "public_visible"] @$s8extern_c16publicVisibilityyS2iFTo : $@convention(c) (Int) -> Int
1414
@_extern(c, "public_visible")
1515
public func publicVisibility(_ x: Int) -> Int
1616

1717
// CHECK-DAG: sil [asmname "private_visible"] @$s8extern_c17privateVisibility{{.*}} : $@convention(c) (Int) -> Int
1818
@_extern(c, "private_visible")
1919
private func privateVisibility(_ x: Int) -> Int
2020

21-
// CHECK-DAG: sil hidden_external [asmname "withoutCName"] @$s8extern_c12withoutCNameSiyF : $@convention(c) () -> Int
21+
// CHECK-DAG: sil hidden_external [asmname "withoutCName"] @$s8extern_c12withoutCNameSiyFTo : $@convention(c) () -> Int
2222
@_extern(c)
2323
func withoutCName() -> Int
2424

2525
// CHECK-DAG: sil hidden [ossa] @$s8extern_c10defaultArgyySiFfA_ : $@convention(thin) () -> Int {
26-
// CHECK-DAG: sil hidden_external [asmname "default_arg"] @$s8extern_c10defaultArgyySiF : $@convention(c) (Int) -> ()
26+
// CHECK-DAG: sil hidden_external [asmname "default_arg"] @$s8extern_c10defaultArgyySiFTo : $@convention(c) (Int) -> ()
2727
@_extern(c, "default_arg")
2828
func defaultArg(_ x: Int = 42)
2929

3030
func main() {
3131
// CHECK-DAG: [[F1:%.+]] = function_ref @$s8extern_c9withCNameyS2iFTo : $@convention(c) (Int) -> Int
32-
// CHECK-DAG: [[F2:%.+]] = function_ref @$s8extern_c12takeCFuncPtryyS2iXCF : $@convention(c) (@convention(c) (Int) -> Int) -> ()
32+
// CHECK-DAG: [[F2:%.+]] = function_ref @$s8extern_c12takeCFuncPtryyS2iXCFTo : $@convention(c) (@convention(c) (Int) -> Int) -> ()
3333
// CHECK-DAG: apply [[F2]]([[F1]]) : $@convention(c) (@convention(c) (Int) -> Int) -> ()
3434
takeCFuncPtr(withCName)
35-
// CHECK-DAG: [[F3:%.+]] = function_ref @$s8extern_c16publicVisibilityyS2iF : $@convention(c) (Int) -> Int
35+
// CHECK-DAG: [[F3:%.+]] = function_ref @$s8extern_c16publicVisibilityyS2iFTo : $@convention(c) (Int) -> Int
3636
// CHECK-DAG: apply [[F3]]({{.*}}) : $@convention(c) (Int) -> Int
3737
_ = publicVisibility(42)
3838
// CHECK-DAG: [[F4:%.+]] = function_ref @$s8extern_c17privateVisibility{{.*}} : $@convention(c) (Int) -> Int
3939
// CHECK-DAG: apply [[F4]]({{.*}}) : $@convention(c) (Int) -> Int
4040
_ = privateVisibility(24)
41-
// CHECK-DAG: [[F5:%.+]] = function_ref @$s8extern_c12withoutCNameSiyF : $@convention(c) () -> Int
41+
// CHECK-DAG: [[F5:%.+]] = function_ref @$s8extern_c12withoutCNameSiyFTo : $@convention(c) () -> Int
4242
// CHECK-DAG: apply [[F5]]() : $@convention(c) () -> Int
4343
_ = withoutCName()
4444
// CHECK-DAG: [[F6:%.+]] = function_ref @$s8extern_c10defaultArgyySiFfA_ : $@convention(thin) () -> Int
4545
// CHECK-DAG: [[DEFAULT_V:%.+]] = apply [[F6]]() : $@convention(thin) () -> Int
46-
// CHECK-DAG: [[F7:%.+]] = function_ref @$s8extern_c10defaultArgyySiF : $@convention(c) (Int) -> ()
46+
// CHECK-DAG: [[F7:%.+]] = function_ref @$s8extern_c10defaultArgyySiFTo : $@convention(c) (Int) -> ()
4747
// CHECK-DAG: apply [[F7]]([[DEFAULT_V]]) : $@convention(c) (Int) -> ()
4848
defaultArg()
4949
}

0 commit comments

Comments
 (0)