Skip to content

Commit 59f29f0

Browse files
Merge pull request #41197 from salinas-miguel/import-CGFloat-APIs-correctly
Import CGFloat APIs correctly
2 parents abd0582 + 2cd0d9b commit 59f29f0

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

lib/AST/ASTContext.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4880,6 +4880,10 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
48804880
addTrivial(Id_CGFloat, coreGraphics);
48814881
}
48824882

4883+
if (auto coreFoundation = getLoadedModule(getIdentifier("CoreFoundation"))) {
4884+
addTrivial(Id_CGFloat, coreFoundation);
4885+
}
4886+
48834887
// Pull SIMD types of size 2...4 from the SIMD module, if it exists.
48844888
// FIXME: Layering violation to use the ClangImporter's define.
48854889
const unsigned SWIFT_MAX_IMPORTED_SIMD_ELEMENTS = 4;
@@ -4928,6 +4932,7 @@ ASTContext::getForeignRepresentationInfo(NominalTypeDecl *nominal,
49284932
conditionallyAddTrivial(nominal, getIdentifier("ObjCBool"), Id_ObjectiveC);
49294933
conditionallyAddTrivial(nominal, getSwiftId(KnownFoundationEntity::NSZone), Id_ObjectiveC, true);
49304934
conditionallyAddTrivial(nominal, Id_CGFloat, getIdentifier("CoreGraphics"));
4935+
conditionallyAddTrivial(nominal, Id_CGFloat, getIdentifier("CoreFoundation"));
49314936
const unsigned SWIFT_MAX_IMPORTED_SIMD_ELEMENTS = 4;
49324937
#define MAP_SIMD_TYPE(BASENAME, _, __) \
49334938
{ \

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,8 +1474,10 @@ class DeclAndTypePrinter::Implementation
14741474
specialNames[{ctx.Id_Darwin, ctx.getIdentifier("DarwinBoolean")}]
14751475
= { "Boolean", false};
14761476

1477-
Identifier ID_CoreGraphics = ctx.getIdentifier("CoreGraphics");
1478-
specialNames[{ID_CoreGraphics, ctx.getIdentifier("CGFloat")}]
1477+
specialNames[{ctx.Id_CoreGraphics, ctx.Id_CGFloat}]
1478+
= { "CGFloat", false };
1479+
1480+
specialNames[{ctx.Id_CoreFoundation, ctx.Id_CGFloat}]
14791481
= { "CGFloat", false };
14801482

14811483
// Use typedefs we set up for SIMD vector types.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: mkdir -p %t/modules
3+
4+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t/modules) -Xcc -DCGFLOAT_IN_COREFOUNDATION -DCGFLOAT_IN_COREFOUNDATION -emit-module -o %t/modules/CoreFoundation.swiftmodule %clang-importer-sdk-path/swift-modules/CoreFoundation.swift
5+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t/modules) -Xcc -DCGFLOAT_IN_COREFOUNDATION -DCGFLOAT_IN_COREFOUNDATION -emit-module -o %t/modules/CoreGraphics.swiftmodule %clang-importer-sdk-path/swift-modules/CoreGraphics.swift
6+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t/modules) -Xcc -DCGFLOAT_IN_COREFOUNDATION -DCGFLOAT_IN_COREFOUNDATION -emit-module -o %t/modules/Foundation.swiftmodule %clang-importer-sdk-path/swift-modules/Foundation.swift
7+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk-nosource -I %t/modules) -typecheck -Xcc -DCGFLOAT_IN_COREFOUNDATION -DCGFLOAT_IN_COREFOUNDATION %s
8+
9+
// REQUIRES: objc_interop
10+
11+
import CoreFoundation
12+
import Foundation
13+
import CoreGraphics
14+
15+
func test() -> UnsafeMutablePointer<CGFloat>? {
16+
let color = CGColor(gray: 1.0, alpha: 1.0)
17+
return CGColorGetComponents(color)
18+
}
19+

test/Inputs/clang-importer-sdk/usr/include/CoreFoundation.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,41 @@ extern CFIndex CFIndex_test;
4040

4141
#define CF_NOESCAPE __attribute__((noescape))
4242

43+
#ifdef CGFLOAT_IN_COREFOUNDATION
44+
#if defined(__LP64__) && __LP64__
45+
# define CGFLOAT_TYPE double
46+
# define CGFLOAT_IS_DOUBLE 1
47+
# define CGFLOAT_MIN DBL_MIN
48+
# define CGFLOAT_MAX DBL_MAX
49+
#else
50+
# define CGFLOAT_TYPE float
51+
# define CGFLOAT_IS_DOUBLE 0
52+
# define CGFLOAT_MIN FLT_MIN
53+
# define CGFLOAT_MAX FLT_MAX
54+
#endif
55+
56+
typedef CGFLOAT_TYPE CGFloat;
57+
#define CGFLOAT_DEFINED 1
58+
59+
struct CGPoint {
60+
CGFloat x;
61+
CGFloat y;
62+
};
63+
typedef struct CGPoint CGPoint;
64+
65+
struct CGSize {
66+
CGFloat width;
67+
CGFloat height;
68+
};
69+
typedef struct CGSize CGSize;
70+
71+
struct CGRect {
72+
CGPoint origin;
73+
CGSize size;
74+
};
75+
typedef struct CGRect CGRect;
76+
77+
typedef CGRect CGRectTy;
78+
#endif //CGFLOAT_IN_COREFOUNDATION
79+
4380
#endif

test/Inputs/clang-importer-sdk/usr/include/CoreGraphics.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#ifndef CGFLOAT_IN_COREFOUNDATION
12
#if defined(__LP64__) && __LP64__
23
# define CGFLOAT_TYPE double
34
# define CGFLOAT_IS_DOUBLE 1
@@ -32,7 +33,12 @@ struct CGRect {
3233
typedef struct CGRect CGRect;
3334

3435
typedef CGRect CGRectTy;
36+
#else
37+
#import "CoreFoundation.h"
38+
#endif //CGFLOAT_IN_COREFOUNDATION
3539

3640
typedef struct CGColor *CGColorRef;
3741

3842
CGColorRef CGColorCreateGenericGray(CGFloat gray, CGFloat alpha);
43+
44+
CGFloat *CGColorGetComponents(CGColorRef color);

0 commit comments

Comments
 (0)