Skip to content

Commit 6cb71c6

Browse files
[ASTPrinter] Print Clang type only if not derivable from Swift type.
1 parent 987d055 commit 6cb71c6

File tree

8 files changed

+82
-44
lines changed

8 files changed

+82
-44
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4037,7 +4037,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
40374037
visit(staticSelfT);
40384038
}
40394039

4040-
void printFunctionExtInfo(ASTContext &Ctx, AnyFunctionType::ExtInfo info) {
4040+
void printFunctionExtInfo(AnyFunctionType *fnType) {
4041+
auto &ctx = fnType->getASTContext();
4042+
auto info = fnType->getExtInfo();
40414043
if (Options.SkipAttributes)
40424044
return;
40434045

@@ -4074,13 +4076,13 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
40744076
break;
40754077
case SILFunctionType::Representation::Block:
40764078
Printer << "block";
4077-
if (printClangType && !info.getClangTypeInfo().empty())
4078-
printCType(Ctx, Printer, info);
4079+
if (printClangType && fnType->hasNonDerivableClangType())
4080+
printCType(ctx, Printer, info);
40794081
break;
40804082
case SILFunctionType::Representation::CFunctionPointer:
40814083
Printer << "c";
4082-
if (printClangType && !info.getClangTypeInfo().empty())
4083-
printCType(Ctx, Printer, info);
4084+
if (printClangType && fnType->hasNonDerivableClangType())
4085+
printCType(ctx, Printer, info);
40844086
break;
40854087
case SILFunctionType::Representation::Method:
40864088
Printer << "method";
@@ -4101,9 +4103,12 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
41014103
}
41024104
}
41034105

4104-
void printFunctionExtInfo(ASTContext &Ctx,
4105-
SILFunctionType::ExtInfo info,
4106-
ProtocolConformanceRef witnessMethodConformance) {
4106+
void printFunctionExtInfo(SILFunctionType *fnType) {
4107+
auto &Ctx = fnType->getASTContext();
4108+
auto info = fnType->getExtInfo();
4109+
auto witnessMethodConformance =
4110+
fnType->getWitnessMethodConformanceOrInvalid();
4111+
41074112
if (Options.SkipAttributes)
41084113
return;
41094114

@@ -4140,12 +4145,12 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
41404145
break;
41414146
case SILFunctionType::Representation::Block:
41424147
Printer << "block";
4143-
if (printClangType)
4148+
if (printClangType && fnType->hasNonDerivableClangType())
41444149
printCType(Ctx, Printer, info);
41454150
break;
41464151
case SILFunctionType::Representation::CFunctionPointer:
41474152
Printer << "c";
4148-
if (printClangType)
4153+
if (printClangType && fnType->hasNonDerivableClangType())
41494154
printCType(Ctx, Printer, info);
41504155
break;
41514156
case SILFunctionType::Representation::Method:
@@ -4220,7 +4225,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
42204225
Printer.printStructurePost(PrintStructureKind::FunctionType);
42214226
};
42224227

4223-
printFunctionExtInfo(T->getASTContext(), T->getExtInfo());
4228+
printFunctionExtInfo(T);
42244229

42254230
// If we're stripping argument labels from types, do it when printing.
42264231
visitAnyFunctionTypeParams(T->getParams(), /*printLabels*/false);
@@ -4260,7 +4265,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
42604265
Printer.printStructurePost(PrintStructureKind::FunctionType);
42614266
};
42624267

4263-
printFunctionExtInfo(T->getASTContext(), T->getExtInfo());
4268+
printFunctionExtInfo(T);
42644269
printGenericSignature(T->getGenericSignature(),
42654270
PrintAST::PrintParams |
42664271
PrintAST::PrintRequirements);
@@ -4322,8 +4327,7 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
43224327

43234328
void visitSILFunctionType(SILFunctionType *T) {
43244329
printSILCoroutineKind(T->getCoroutineKind());
4325-
printFunctionExtInfo(T->getASTContext(), T->getExtInfo(),
4326-
T->getWitnessMethodConformanceOrInvalid());
4330+
printFunctionExtInfo(T);
43274331
printCalleeConvention(T->getCalleeConvention());
43284332

43294333
if (auto sig = T->getInvocationGenericSignature()) {

test/ClangImporter/clang-function-types.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import ctypes
44

5-
// CHECK: f1: (@convention(c, cType: "int (*)(int)") (Swift.Int32) -> Swift.Int32)?
5+
// CHECK: f1: (@convention(c, cType: "size_t (*)(size_t)") (Swift.Int) -> Swift.Int)?
66
public let f1 = getFunctionPointer_()
77

8-
// CHECK: f2: (@convention(c, cType: "int (*(*)(int (*)(int)))(int)") ((@convention(c, cType: "int (*)(int)") (Swift.Int32) -> Swift.Int32)?) -> (@convention(c, cType: "int (*)(int)") (Swift.Int32) -> Swift.Int32)?)?
8+
// CHECK: f2: (@convention(c) ((@convention(c, cType: "size_t (*)(size_t)") (Swift.Int) -> Swift.Int)?) -> (@convention(c, cType: "size_t (*)(size_t)") (Swift.Int) -> Swift.Int)?)?
99
public let f2 = getHigherOrderFunctionPointer()
1010

11-
// CHECK: f3: () -> (@convention(c, cType: "Dummy *(*)(Dummy *)") (Swift.UnsafeMutablePointer<ctypes.Dummy>?) -> Swift.UnsafeMutablePointer<ctypes.Dummy>?)?
11+
// CHECK: f3: () -> (@convention(c) (Swift.UnsafeMutablePointer<ctypes.Dummy>?) -> Swift.UnsafeMutablePointer<ctypes.Dummy>?)?
1212
public let f3 = getFunctionPointer3

test/ClangImporter/ctypes_parse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func testFunctionPointers() {
205205
useFunctionPointer(wrapper.a)
206206
_ = wrapper.b as (@convention(c) (CInt) -> CInt)
207207

208-
var anotherFP: @convention(c) (CInt, CLong, UnsafeMutableRawPointer?) -> Void
208+
var anotherFP: @convention(c) (Int, CLong, UnsafeMutableRawPointer?) -> Void
209209
= getFunctionPointer2()
210210

211211
var sizedFP: (@convention(c) (CInt, CInt, UnsafeMutableRawPointer?) -> Void)?

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,18 @@ typedef int (*fptr)(int);
205205
fptr getFunctionPointer(void);
206206
void useFunctionPointer(fptr);
207207

208-
int (*getFunctionPointer_(void))(int);
208+
size_t (*getFunctionPointer_(void))(size_t);
209209

210210
struct FunctionPointerWrapper {
211211
fptr a;
212212
fptr b;
213213
};
214214

215-
typedef void (*fptr2)(int, long, void *);
215+
typedef void (*fptr2)(size_t, long, void *);
216216
fptr2 getFunctionPointer2(void);
217217
void useFunctionPointer2(fptr2);
218218

219-
int (*(*getHigherOrderFunctionPointer(void))(int (*)(int)))(int);
219+
size_t (*(*getHigherOrderFunctionPointer(void))(size_t (*)(size_t)))(size_t);
220220

221221
typedef struct Dummy {
222222
int x;
Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
1-
// RUN: %target-swift-frontend -typecheck -swift-version 5 -emit-module-interface-path - -enable-library-evolution %s -experimental-print-full-convention | %FileCheck %s
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -typecheck -swift-version 5 -emit-module-interface-path - -enable-library-evolution %s -experimental-print-full-convention | %FileCheck %s
2+
3+
import ctypes
24

35
public func f(
4-
// CHECK: g: @convention(c, cType: "void (*)(void)")
6+
// CHECK: g: @convention(c)
57
g: @convention(c) () -> (),
68

7-
// CHECK: h0: @convention(c, cType: "int (*)(long long)")
9+
// CHECK: h0: @convention(c)
810
h0: @convention(c) (Int64) -> Int32,
9-
// CHECK: h1: @convention(c, cType: "int (*)(long long)")
11+
// CHECK: h1: @convention(c)
1012
h1: @convention(c, cType: "int (*)(long long)") (Int64) -> Int32,
1113

12-
// CHECK: i0: @convention(c, cType: "int *(*)(long long, int)")
14+
// CHECK: h1c: @convention(c, cType: "intptr_t (*)(size_t)")
15+
h1c: @convention(c, cType: "intptr_t (*)(size_t)") (Int) -> Int,
16+
17+
// CHECK: i0: @convention(c)
1318
i0: @convention(c) (Int64, Int32) -> Optional<UnsafeMutablePointer<Int32>>,
14-
// CHECK: i1: @convention(c, cType: "int *(*)(long long, int)")
19+
// CHECK: i1: @convention(c)
1520
i1: @convention(c, cType: "int *(*)(long long, int)") (Int64, Int32) -> Optional<UnsafeMutablePointer<Int32>>,
1621

17-
// CHECK: p0: @convention(c, cType: "void (*)(void (*)(int))")
18-
// CHECK: @convention(c, cType: "void (*)(int)")
22+
// CHECK: i1c: @convention(c, cType: "size_t *(*)(intptr_t, ptrdiff_t)")
23+
i1c: @convention(c, cType: "size_t *(*)(intptr_t, ptrdiff_t)") (Int, Int) -> Optional<UnsafeMutablePointer<Int>>,
24+
25+
// CHECK: p0: @convention(c)
26+
// CHECK: @convention(c)
1927
p0: @convention(c) (@convention(c) (Int32) -> Void) -> Void,
2028

21-
// CHECK: p1: @convention(c, cType: "void (*)(void (*)(int))")
22-
// CHECK: @convention(c, cType: "void (*)(int)")
29+
// CHECK: p1: @convention(c)
30+
// CHECK: @convention(c)
2331
p1: @convention(c, cType: "void (*)(void (*)(int))") (@convention(c) (Int32) -> Void) -> Void,
2432

25-
// CHECK: p2: @convention(c, cType: "void (*)(void (*)(int))")
26-
// CHECK: @convention(c, cType: "void (*)(int)")
33+
// CHECK: p1c: @convention(c, cType: "void (*)(void (*)(size_t))")
34+
// CHECK: @convention(c)
35+
p1c: @convention(c, cType: "void (*)(void (*)(size_t))") (@convention(c) (Int) -> Void) -> Void,
36+
37+
// CHECK: p2: @convention(c)
38+
// CHECK: @convention(c)
2739
p2: @convention(c) (@convention(c, cType: "void (*)(int)") (Int32) -> Void) -> Void,
2840

29-
// CHECK: p3: @convention(c, cType: "void (*)(void (*)(int))")
30-
// CHECK: @convention(c, cType: "void (*)(int)")
31-
p3: @convention(c, cType: "void (*)(void (*)(int))") (@convention(c, cType: "void (*)(int)") (Int32) -> Void) -> Void
41+
// CHECK: p2c: @convention(c)
42+
// CHECK: @convention(c, cType: "void (*)(size_t)")
43+
p2c: @convention(c) (@convention(c, cType: "void (*)(size_t)") (Int) -> Void) -> Void,
44+
45+
// CHECK: p3: @convention(c)
46+
// CHECK: @convention(c)
47+
p3: @convention(c, cType: "void (*)(void (*)(int))") (@convention(c, cType: "void (*)(int)") (Int32) -> Void) -> Void,
48+
49+
// CHECK: p3c: @convention(c)
50+
// CHECK: @convention(c, cType: "void (*)(size_t)")
51+
p3c: @convention(c, cType: "void (*)(void (*)(size_t))") (@convention(c, cType: "void (*)(size_t)") (Int) -> Void) -> Void
3252
) {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) %s -emit-sil -swift-version 5 -use-clang-function-types -experimental-print-full-convention -o - | %FileCheck %s
2+
3+
// REQUIRES: OS=windows-msvc
4+
5+
import ctypes
6+
7+
public func f(g: @convention(c, cType: "void (*)(size_t)") (Int) -> ()) { g(0) }
8+
9+
// CHECK: sil @$s4main1f1gyySiXzC9_ZTSPFvyE_tF : $@convention(thin) (@convention(c, cType: "void (*)(unsigned long long)") @noescape (Int) -> ()) -> () {
10+
// CHECK: bb0(%0 : $@convention(c, cType: "void (*)(unsigned long long)") @noescape (Int) -> ()):
11+
// CHECK: debug_value %0 : $@convention(c, cType: "void (*)(unsigned long long)") @noescape (Int) -> (), let, name "g", argno 1 // id: %1
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) %s -emit-sil -swift-version 5 -use-clang-function-types -experimental-print-full-convention -o - | %FileCheck %s
2+
3+
// UNSUPPORTED: OS=windows-msvc
4+
5+
import ctypes
6+
7+
public func f(g: @convention(c, cType: "void (*)(size_t)") (Int) -> ()) { g(0) }
8+
9+
// CHECK: sil @$s4main1f1gyySiXzC9_ZTSPFvmE_tF : $@convention(thin) (@convention(c, cType: "void (*)(unsigned long)") @noescape (Int) -> ()) -> () {
10+
// CHECK: bb0(%0 : $@convention(c, cType: "void (*)(unsigned long)") @noescape (Int) -> ()):
11+
// CHECK: debug_value %0 : $@convention(c, cType: "void (*)(unsigned long)") @noescape (Int) -> (), let, name "g", argno 1 // id: %1

test/SIL/clang-function-types.swift

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)