Extend @_extern to global and static variables

Allow external declaration of global variables via `@_extern(c)`. Such
variables need to have types represented in C (of course), have only
storage (no accessors), and cannot have initializers. At the SIL
level, we use the SIL asmname attribute to get the appropriate C name.

While here, slightly shore up the `@_extern(c)` checking, which should
fix issue #70776 / rdar://153515764.
This commit is contained in:
Doug Gregor
2025-11-06 10:59:14 -08:00
parent b1c2bc1731
commit 5b642f548f
13 changed files with 171 additions and 89 deletions

View File

@@ -2,6 +2,17 @@
// REQUIRES: swift_feature_Extern
// CHECK: @pointer_c = external global
@_extern(c)
var pointer_c: UnsafeMutablePointer<Int>
// CHECK: @nullable_pointer_c = external global
@_extern(c)
var nullable_pointer_c: UnsafeMutablePointer<Int>?
func acceptInt(_: Int) { }
func test() {
// CHECK: call void @explicit_extern_c()
explicit_extern_c()
@@ -12,6 +23,11 @@ func test() {
default_arg_value()
// CHECK: call void @default_arg_value(i32 24)
default_arg_value(24)
// CHECK: call void @swift_beginAccess(ptr @pointer_c
// CHECK-NEXT: [[POINTEE_VAL:%[0-9]+]] = load ptr, ptr @pointer_c
// CHECK-NEXT: call void @swift_endAccess
acceptInt(pointer_c.pointee)
}
test()