Skip to content

Commit c356875

Browse files
Merge pull request #40856 from salinas-miguel/import-cgfloat
Import CGFloat as CoreFoundation.CGFloat if possible
2 parents 06ccb51 + 8f9871a commit c356875

File tree

6 files changed

+97
-1
lines changed

6 files changed

+97
-1
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ getSwiftStdlibType(const clang::TypedefNameDecl *D,
437437
M = Impl.getStdlibModule();
438438
else
439439
M = Impl.getNamedModule(SwiftModuleName);
440+
440441
if (!M) {
441442
// User did not import the library module that contains the type we want to
442443
// substitute.
@@ -445,8 +446,18 @@ getSwiftStdlibType(const clang::TypedefNameDecl *D,
445446
}
446447

447448
Type SwiftType = Impl.getNamedSwiftType(M, SwiftTypeName);
449+
450+
if (!SwiftType && CTypeKind == MappedCTypeKind::CGFloat) {
451+
// Look for CGFloat in CoreFoundation.
452+
M = Impl.getNamedModule("CoreFoundation");
453+
SwiftType = Impl.getNamedSwiftType(M, SwiftTypeName);
454+
}
455+
448456
if (!SwiftType && !CanBeMissing) {
449457
// The required type is not defined in the standard library.
458+
// The required type is not defined in the library, or the user has not
459+
// imported the library that defines it (so `M` was null and
460+
// `getNamedSwiftType()` returned early).
450461
*IsError = true;
451462
return std::make_pair(Type(), "");
452463
}

lib/ClangImporter/MappedTypes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ MAP_STDLIB_TYPE(
155155
Impl.SwiftContext.getSwiftName(KnownFoundationEntity::NSUInteger),
156156
UnsignedWord, 0, "Int", false, DoNothing)
157157

158-
// CoreGraphics types.
158+
// CGFloat.
159159
MAP_TYPE("CGFloat", CGFloat, 0, "CoreGraphics", "CGFloat", false, DoNothing)
160160

161161
// CoreFoundation types.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,51 @@
11
@_exported import CoreFoundation
22

33
protocol _CFObject: Hashable {}
4+
5+
#if CGFLOAT_IN_COREFOUNDATION
6+
public struct CGFloat {
7+
#if arch(i386) || arch(arm) || arch(arm64_32)
8+
public typealias UnderlyingType = Float
9+
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64le) || arch(s390x)
10+
public typealias UnderlyingType = Double
11+
#endif
12+
13+
public init() {
14+
self.value = 0.0
15+
}
16+
17+
public init(_ value: Int) {
18+
self.value = UnderlyingType(value)
19+
}
20+
21+
public init(_ value: Float) {
22+
self.value = UnderlyingType(value)
23+
}
24+
25+
public init(_ value: Double) {
26+
self.value = UnderlyingType(value)
27+
}
28+
29+
var value: UnderlyingType
30+
}
31+
32+
public func ==(lhs: CGFloat, rhs: CGFloat) -> Bool {
33+
return lhs.value == rhs.value
34+
}
35+
36+
extension CGFloat : ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral, Equatable {
37+
public init(integerLiteral value: UnderlyingType) {
38+
self.value = value
39+
}
40+
41+
public init(floatLiteral value: UnderlyingType) {
42+
self.value = value
43+
}
44+
}
45+
46+
public extension Double {
47+
init(_ value: CGFloat) {
48+
self = Double(value.value)
49+
}
50+
}
51+
#endif

test/Inputs/clang-importer-sdk/swift-modules/CoreGraphics.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public func == (lhs: CGPoint, rhs: CGPoint) -> Bool {
55
return lhs.x == rhs.x && lhs.y == rhs.y
66
}
77

8+
#if !CGFLOAT_IN_COREFOUNDATION
89
public struct CGFloat {
910
#if arch(i386) || arch(arm) || arch(arm64_32)
1011
public typealias UnderlyingType = Float
@@ -50,3 +51,4 @@ public extension Double {
5051
self = Double(value.value)
5152
}
5253
}
54+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Test that known types that conform to `_ObjectiveCBridgeable` import or
2+
// forward-declare based on the Clang type in their known type mapping, not
3+
// their bridged type.
4+
//
5+
// This is particularly important for `CGFloat`, which has a native Swift decl
6+
// in the CoreGraphics overlay that shadows the imported Clang decl, so relying
7+
// solely on whether or not the decl has a Clang node is not sufficient.
8+
9+
// RUN: %empty-directory(%t)
10+
// RUN: %target-swift-frontend -typecheck %s -parse-as-library -emit-objc-header-path %t/swift.h
11+
// RUN: %FileCheck %s < %t/swift.h
12+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -DCGFLOAT_IN_COREFOUNDATION -emit-module -o %t %clang-importer-sdk-path/swift-modules/CoreFoundation.swift
13+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource) -DCGFLOAT_IN_COREFOUNDATION -emit-module -o %t %clang-importer-sdk-path/swift-modules/CoreGraphics.swift
14+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -emit-module -o %t %clang-importer-sdk-path/swift-modules/Foundation.swift
15+
16+
// REQUIRES: objc_interop
17+
18+
import CoreGraphics
19+
import Foundation
20+
21+
// CHECK: @import CoreGraphics;
22+
23+
// CHECK-NOT: @class NSNumber;
24+
25+
// CHECK-LABEL: @interface Test : NSObject{{$}}
26+
public class Test: NSObject {
27+
// CHECK-NEXT: - (CGFloat)level
28+
@objc public func level() -> CGFloat { 9000.0 }
29+
// CHECK-NEXT: - (BOOL)isEnabled
30+
@objc public func isEnabled() -> Bool { true }
31+
// CHECK-NEXT: init
32+
} // CHECK-NEXT: @end

test/PrintAsObjC/bridged-known-types.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
// RUN: %empty-directory(%t)
1010
// RUN: %target-swift-frontend -typecheck %s -parse-as-library -emit-objc-header-path %t/swift.h
1111
// RUN: %FileCheck %s < %t/swift.h
12+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -emit-module -o %t %clang-importer-sdk-path/swift-modules/CoreFoundation.swift
13+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource) -emit-module -o %t %clang-importer-sdk-path/swift-modules/CoreGraphics.swift
14+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t) -emit-module -o %t %clang-importer-sdk-path/swift-modules/Foundation.swift
1215

1316
// REQUIRES: objc_interop
1417

0 commit comments

Comments
 (0)